simplify createTable - use querySpec

This commit is contained in:
Helmut Merz 2024-04-13 15:54:08 +02:00
parent 572f1bf80d
commit ab5082ee89
3 changed files with 19 additions and 25 deletions

View file

@ -18,7 +18,7 @@ type Storage struct {
*sql.DB
*Cfg
Schema string
Params map[string]string
Params lib.StrMap
Errors []error
}

View file

@ -8,24 +8,23 @@ import (
const (
sql_table = `
{{- $tablename := or (and .schema (printf "%s.%s" .schema .tablename)) .tablename -}}
{{- $json := or .params.jsonType "json" -}}
create table if not exists {{ $tablename }} (
trackid {{ or .params.idType "integer" }} primary key,
{{ range .headFields -}}{{ toLower . }} text default '',
{{- $json := or .Params.jsonType "json" -}}
create table if not exists {{ .Table }} (
trackid {{ or .Params.idType "integer" }} primary key,
{{ range .Cont.HeadFields -}}{{ toLower . }} text default '',
{{ end -}}
timestamp timestamptz default current_timestamp,
data {{ $json }}
);
{{- range $i, $cols := .indexes }}
create index idx_{{ $.tablename }}_{{ add1 $i }} on {{ $tablename }} (
{{- range $i, $cols := .Cont.Indexes }}
create index idx_{{ $.Cont.TableName }}_{{ add1 $i }} on {{ $.Table }} (
{{- range $j, $c := $cols -}}
{{- if ne $j 0 -}}, {{ end }}{{ toLower $c }}
{{- end -}}
);
{{- end }}
create index idx_{{ $.tablename }}_ts on {{ $tablename }} (timestamp);
create index idx_{{ $.Cont.TableName }}_ts on {{ .Table }} (timestamp);
`
sql_insert = `

View file

@ -102,6 +102,8 @@ type querySpec struct {
Ordspecs []ordSpec
Limit int
Quvals []any
Cont *Container
Params lib.StrMap
}
type ordSpec struct {
@ -138,6 +140,8 @@ func (spec *querySpec) setup(cont *Container) {
for _, c := range spec.Ordcols {
spec.AddOrd(c, false)
}
spec.Cont = cont
spec.Params = cont.Storage.Params
}
func (cont *Container) Get(id lib.Ident) *Track {
@ -158,7 +162,6 @@ func (cont *Container) QueryLast(hv ...string) *Track {
}
func (cont *Container) QueryOne(quSpec *querySpec) *Track {
db := cont.Storage
var tr *Track
proc := func(r *sql.Rows) error {
tr = cont.ItemFactory(cont)
@ -166,12 +169,11 @@ func (cont *Container) QueryOne(quSpec *querySpec) *Track {
}
quSpec.setup(cont)
sql := storage.BuildSql(SqlSelect, quSpec)
db.Query(proc, sql, quSpec.Quvals...)
cont.Storage.Query(proc, sql, quSpec.Quvals...)
return tr
}
func (cont *Container) Query(quSpec *querySpec) []*Track {
db := cont.Storage
var trs []*Track
proc := func(r *sql.Rows) error {
tr := cont.ItemFactory(cont)
@ -183,7 +185,7 @@ func (cont *Container) Query(quSpec *querySpec) []*Track {
}
quSpec.setup(cont)
sql := storage.BuildSql(SqlSelect, quSpec)
db.Query(proc, sql, quSpec.Quvals...)
cont.Storage.Query(proc, sql, quSpec.Quvals...)
return trs
}
@ -199,7 +201,6 @@ func (cont *Container) Save(t *Track) lib.Ident {
}
func (cont *Container) Insert(tr *Track) lib.Ident {
db := cont.Storage
quSpec := &querySpec{
Scols: append(cont.HeadFields, "Data"),
}
@ -217,7 +218,7 @@ func (cont *Container) Insert(tr *Track) lib.Ident {
tr.TimeStamp = ParseDateTime(tsstr)
return err
}
if err := db.Query(proc, sql, values...); err == nil {
if err := cont.Storage.Query(proc, sql, values...); err == nil {
return tr.trackId
}
return 0
@ -237,16 +238,10 @@ func ParseDateTime(inp string) *time.Time {
}
func (cont *Container) CreateTable() {
db := cont.Storage
data := lib.Map{
"schema": db.Schema,
"tablename": cont.TableName,
"headFields": cont.HeadFields,
"indexes": cont.Indexes,
"params": db.Params,
}
sql := storage.BuildSql(SqlCreate, data)
if _, err := db.Exec(sql); err != nil {
spec := querySpec{}
spec.setup(cont)
sql := storage.BuildSql(SqlCreate, spec)
if _, err := cont.Storage.Exec(sql); err != nil {
panic(err)
}
}