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 *sql.DB
*Cfg *Cfg
Schema string Schema string
Params map[string]string Params lib.StrMap
Errors []error Errors []error
} }

View file

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

View file

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