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 {
|
func Open(cfg *Cfg) *Storage {
|
||||||
db, err := sql.Open(cfg.Driver, cfg.Connstr)
|
db, err := sql.Open(cfg.Driver, cfg.Connstr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err).Msg("sql.Open")
|
log.Error(err).Msg("storage.Open")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
storage := Storage{
|
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 {
|
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)
|
//log.Debug().Str("query", q).Msg(info)
|
||||||
rows, err := db.DB.Query(q, args...)
|
rows, err := db.DB.Query(q, args...)
|
||||||
if err != nil {
|
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
|
package tracking
|
||||||
|
|
||||||
|
import (
|
||||||
|
"text/template"
|
||||||
|
|
||||||
|
"git.sr.ht/~cco/go-scopes/storage"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
sql_table = `
|
sql_table = `
|
||||||
{{- $tablename := or (and .schema (printf "%s.%s" .schema .tablename)) .tablename -}}
|
{{- $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 }} (
|
create table if not exists {{ $tablename }} (
|
||||||
trackid {{ or .idType "integer" }} primary key,
|
trackid {{ or .params.idType "integer" }} primary key,
|
||||||
{{ range .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 := .indexes }}
|
||||||
create index idx_{{ $.tablename }}_{{ Add1 $i }} on {{ $tablename }} (
|
create index idx_{{ $.tablename }}_{{ add1 $i }} on {{ $tablename }} (
|
||||||
{{- 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 }}
|
||||||
|
@ -26,7 +32,7 @@ create index idx_{{ $.tablename }}_ts on {{ $tablename }} (timestamp);
|
||||||
{{- $tablename := or (and .schema (printf "%s.%s" .schema .tablename)) .tablename -}}
|
{{- $tablename := or (and .schema (printf "%s.%s" .schema .tablename)) .tablename -}}
|
||||||
insert into {{ $tablename }} (
|
insert into {{ $tablename }} (
|
||||||
{{- range $j, $c := .columns -}}
|
{{- range $j, $c := .columns -}}
|
||||||
{{- if ne $j 0 -}}, {{ end }}{{ ToLower $c }}
|
{{- if ne $j 0 -}}, {{ end }}{{ toLower $c }}
|
||||||
{{- end -}})
|
{{- end -}})
|
||||||
values (
|
values (
|
||||||
{{- range $j, $c := .columns -}}
|
{{- range $j, $c := .columns -}}
|
||||||
|
@ -34,3 +40,10 @@ insert into {{ $tablename }} (
|
||||||
{{- end -}})
|
{{- 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
|
package tracking
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"text/template"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
lib "git.sr.ht/~cco/go-scopes"
|
lib "git.sr.ht/~cco/go-scopes"
|
||||||
|
"git.sr.ht/~cco/go-scopes/storage"
|
||||||
sql "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 {
|
func Create(cont *container) *container {
|
||||||
db := cont.storage
|
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{}{
|
data := map[string]interface{}{
|
||||||
"schema": db.Schema,
|
"schema": db.Schema,
|
||||||
"tablename": "tracks",
|
"tablename": "tracks",
|
||||||
"headFields": cont.headFields,
|
"headFields": cont.headFields,
|
||||||
"indexes": cont.indexes,
|
"indexes": cont.indexes,
|
||||||
"idType": db.Params["idType"],
|
"params": db.Params,
|
||||||
"jsonType": db.Params["jsonType"],
|
|
||||||
}
|
}
|
||||||
err = t.Execute(&out, data)
|
sql := storage.BuildSql(SqlCreate, data)
|
||||||
if err != nil {
|
|
||||||
db.LogErr(err, "storage.tracking.Create", fmt.Sprintf("%+v", t))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
sql := out.String()
|
|
||||||
println(sql)
|
println(sql)
|
||||||
_, err = db.Exec(sql)
|
_, err := db.Exec(sql)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return cont
|
return cont
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue