move template handling to separate source file; parse templates on init
This commit is contained in:
parent
8d022e1930
commit
4787e4ffce
4 changed files with 59 additions and 31 deletions
|
@ -45,7 +45,7 @@ var SqlSources = ""
|
|||
func Open(cfg *Cfg) *Storage {
|
||||
db, err := sql.Open(cfg.Driver, cfg.Connstr)
|
||||
if err != nil {
|
||||
log.Error(err).Msg("sql.Open")
|
||||
log.Error(err).Msg("storage.Open")
|
||||
return nil
|
||||
}
|
||||
storage := Storage{
|
||||
|
@ -95,7 +95,7 @@ func QueryCol[T any](db *Storage, q string, args ...interface{}) []T {
|
|||
}
|
||||
|
||||
func (db *Storage) Query(process rowsProc, q string, args ...interface{}) error {
|
||||
info := "sql.Storage.Query"
|
||||
info := "storage.Query"
|
||||
//log.Debug().Str("query", q).Msg(info)
|
||||
rows, err := db.DB.Query(q, args...)
|
||||
if err != nil {
|
||||
|
|
34
storage/template.go
Normal file
34
storage/template.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"git.sr.ht/~cco/go-scopes/logging/log"
|
||||
)
|
||||
|
||||
var TemplateFunctions = map[string]any{
|
||||
"toLower": strings.ToLower,
|
||||
"add1": func(i int) int { return i + 1 },
|
||||
}
|
||||
|
||||
func Template(name, src string) *template.Template {
|
||||
t := template.New(name).Funcs(TemplateFunctions)
|
||||
t, err := t.Parse(src)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
//logger.LogErr(err, info, fmt.Sprintf("%+v", t))
|
||||
//return nil
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func BuildSql(t *template.Template, data map[string]any) string {
|
||||
var out strings.Builder
|
||||
err := t.Execute(&out, data)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return ""
|
||||
}
|
||||
return out.String()
|
||||
}
|
|
@ -1,21 +1,27 @@
|
|||
package tracking
|
||||
|
||||
import (
|
||||
"text/template"
|
||||
|
||||
"git.sr.ht/~cco/go-scopes/storage"
|
||||
)
|
||||
|
||||
const (
|
||||
sql_table = `
|
||||
{{- $tablename := or (and .schema (printf "%s.%s" .schema .tablename)) .tablename -}}
|
||||
{{- $json := or .jsonType "json" -}}
|
||||
{{- $json := or .params.jsonType "json" -}}
|
||||
create table if not exists {{ $tablename }} (
|
||||
trackid {{ or .idType "integer" }} primary key,
|
||||
{{ range .headFields -}}{{ ToLower . }} text default '',
|
||||
trackid {{ or .params.idType "integer" }} primary key,
|
||||
{{ range .headFields -}}{{ toLower . }} text default '',
|
||||
{{ end -}}
|
||||
timestamp timestamptz default current_timestamp,
|
||||
data {{ $json }}
|
||||
);
|
||||
|
||||
{{- range $i, $cols := .indexes }}
|
||||
create index idx_{{ $.tablename }}_{{ Add1 $i }} on {{ $tablename }} (
|
||||
create index idx_{{ $.tablename }}_{{ add1 $i }} on {{ $tablename }} (
|
||||
{{- range $j, $c := $cols -}}
|
||||
{{- if ne $j 0 -}}, {{ end }}{{ ToLower $c }}
|
||||
{{- if ne $j 0 -}}, {{ end }}{{ toLower $c }}
|
||||
{{- end -}}
|
||||
);
|
||||
{{- end }}
|
||||
|
@ -26,7 +32,7 @@ create index idx_{{ $.tablename }}_ts on {{ $tablename }} (timestamp);
|
|||
{{- $tablename := or (and .schema (printf "%s.%s" .schema .tablename)) .tablename -}}
|
||||
insert into {{ $tablename }} (
|
||||
{{- range $j, $c := .columns -}}
|
||||
{{- if ne $j 0 -}}, {{ end }}{{ ToLower $c }}
|
||||
{{- if ne $j 0 -}}, {{ end }}{{ toLower $c }}
|
||||
{{- end -}})
|
||||
values (
|
||||
{{- range $j, $c := .columns -}}
|
||||
|
@ -34,3 +40,10 @@ insert into {{ $tablename }} (
|
|||
{{- end -}})
|
||||
`
|
||||
)
|
||||
|
||||
var SqlCreate, SqlInsert *template.Template
|
||||
|
||||
func init() {
|
||||
SqlCreate = storage.Template("create_table", sql_table)
|
||||
SqlInsert = storage.Template("insert_track", sql_insert)
|
||||
}
|
||||
|
|
|
@ -3,12 +3,10 @@
|
|||
package tracking
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/storage"
|
||||
sql "git.sr.ht/~cco/go-scopes/storage"
|
||||
)
|
||||
|
||||
|
@ -36,33 +34,16 @@ func Container(db *sql.Storage) *container {
|
|||
|
||||
func Create(cont *container) *container {
|
||||
db := cont.storage
|
||||
t := template.New("create_table")
|
||||
t = t.Funcs(map[string]any{
|
||||
"ToLower": strings.ToLower,
|
||||
"Add1": func(i int) int { return i + 1 },
|
||||
})
|
||||
t, err := t.Parse(sql_table)
|
||||
if err != nil {
|
||||
db.LogErr(err, "storage.tracking.Create", fmt.Sprintf("%+v", t))
|
||||
return nil
|
||||
}
|
||||
var out strings.Builder
|
||||
data := map[string]interface{}{
|
||||
"schema": db.Schema,
|
||||
"tablename": "tracks",
|
||||
"headFields": cont.headFields,
|
||||
"indexes": cont.indexes,
|
||||
"idType": db.Params["idType"],
|
||||
"jsonType": db.Params["jsonType"],
|
||||
"params": db.Params,
|
||||
}
|
||||
err = t.Execute(&out, data)
|
||||
if err != nil {
|
||||
db.LogErr(err, "storage.tracking.Create", fmt.Sprintf("%+v", t))
|
||||
return nil
|
||||
}
|
||||
sql := out.String()
|
||||
sql := storage.BuildSql(SqlCreate, data)
|
||||
println(sql)
|
||||
_, err = db.Exec(sql)
|
||||
_, err := db.Exec(sql)
|
||||
if err == nil {
|
||||
return cont
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue