experimental: build query functionality: now with one template with define-d sub-templates
This commit is contained in:
parent
3c81a179cc
commit
59e6516333
2 changed files with 12 additions and 15 deletions
|
@ -1,8 +1,6 @@
|
||||||
package msgstore
|
package msgstore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"text/template"
|
|
||||||
|
|
||||||
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"
|
||||||
)
|
)
|
||||||
|
@ -13,17 +11,6 @@ var Store = func(db *sql.Storage, msg lib.Message) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildQuery(db *sql.Storage, qname, tname string) string {
|
func buildQuery(db *sql.Storage, qname, tname string) string {
|
||||||
t, ok := sql_templates[qname]
|
t := db.Sql.Lookup(qname)
|
||||||
if !ok {
|
|
||||||
t = db.ParseTemplate(sql_sources[qname])
|
|
||||||
sql_templates[qname] = t
|
|
||||||
}
|
|
||||||
return db.SetTable(t, tname)
|
return db.SetTable(t, tname)
|
||||||
}
|
}
|
||||||
|
|
||||||
var sql_templates = map[string]*template.Template{}
|
|
||||||
|
|
||||||
var sql_sources = map[string]string{
|
|
||||||
"insert_msg": `insert into {{ .table }} (domain, action, class, item, payload)
|
|
||||||
values ($1, $2, $3, $4, $5)`,
|
|
||||||
}
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ type Cfg struct {
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
*sql.DB
|
*sql.DB
|
||||||
*Cfg
|
*Cfg
|
||||||
|
Sql *template.Template
|
||||||
Errors []error
|
Errors []error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,9 +37,18 @@ func Open(cfg *Cfg) *Storage {
|
||||||
log.Error(err).Msg("sql.Open")
|
log.Error(err).Msg("sql.Open")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &Storage{DB: db, Cfg: cfg}
|
storage := Storage{DB: db, Cfg: cfg}
|
||||||
|
storage.Sql = storage.ParseTemplate(SqlSources)
|
||||||
|
return &storage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const SqlSources = `
|
||||||
|
{{- define "insert_msg" -}}
|
||||||
|
insert into {{ .table }} (domain, action, class, item, payload)
|
||||||
|
values ($1, $2, $3, $4, $5)
|
||||||
|
{{- end }}
|
||||||
|
`
|
||||||
|
|
||||||
func QueryData[T scn[T]](db *Storage, q string, args ...interface{}) []T {
|
func QueryData[T scn[T]](db *Storage, q string, args ...interface{}) []T {
|
||||||
var data []T
|
var data []T
|
||||||
var rec T
|
var rec T
|
||||||
|
|
Loading…
Add table
Reference in a new issue