Container.Update() working

This commit is contained in:
Helmut Merz 2024-04-14 09:04:48 +02:00
parent 45ab1200e2
commit 872a692d88
4 changed files with 25 additions and 17 deletions

View file

@ -10,6 +10,8 @@ import (
var TemplateFunctions = map[string]any{ var TemplateFunctions = map[string]any{
"toLower": strings.ToLower, "toLower": strings.ToLower,
"add1": func(i int) int { return i + 1 }, "add1": func(i int) int { return i + 1 },
"add2": func(i int) int { return i + 2 },
"add": func(i, j int) int { return i + j },
} }
func Template(name, src string) *template.Template { func Template(name, src string) *template.Template {

View file

@ -21,7 +21,7 @@ create table if not exists {{ .Table }} (
{{- range $i, $cols := .Cont.Indexes }} {{- range $i, $cols := .Cont.Indexes }}
create index idx_{{ $.Cont.TableName }}_{{ add1 $i }} on {{ $.Table }} ( 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 }}
@ -31,7 +31,7 @@ create index idx_{{ $.Cont.TableName }}_ts on {{ .Table }} (timestamp);
sql_insert = ` sql_insert = `
insert into {{ .Table }} ( insert into {{ .Table }} (
{{- range $j, $c := .Scols -}} {{- range $j, $c := .Scols -}}
{{- if ne $j 0 -}}, {{ end }}{{ toLower $c }} {{- if ne $j 0 }}, {{ end }}{{ toLower $c }}
{{- end -}}) {{- end -}})
values ( values (
{{- range $j, $c := .Scols -}} {{- range $j, $c := .Scols -}}
@ -39,11 +39,14 @@ insert into {{ .Table }} (
{{- end -}}) {{- end -}})
returning trackid, timestamp` returning trackid, timestamp`
sql_update = `` sql_update = `
update {{ .Table }} set {{- range $j, $c := .Scols -}}
{{- if ne $j 0 }},{{ end }} {{ toLower $c }} = ${{ add $j 2 }}{{ end }}
where trackid = $1`
sql_select = ` sql_select = `
select {{ range $j, $c := .Scols -}} select {{ range $j, $c := .Scols -}}
{{- if ne $j 0 -}}, {{ end }}{{ toLower $c }}{{- end }} {{- if ne $j 0 }}, {{ end }}{{ toLower $c }}{{ end }}
from {{ .Table }} from {{ .Table }}
where {{ range $j, $s := .Quspecs -}} where {{ range $j, $s := .Quspecs -}}
{{- if ne $j 0 }} and {{ end -}} {{- if ne $j 0 }} and {{ end -}}

View file

@ -43,15 +43,6 @@ func MakeTrack(cont *Container, h ...string) *Track {
return &tr return &tr
} }
func (tr *Track) AsSlice() []any {
var sl []any
for _, k := range tr.container.HeadFields {
sl = append(sl, tr.Head[k])
}
sl = append(sl, tr.TimeStamp, tr.Data)
return sl
}
func (tr *Track) SetHead(h ...string) { func (tr *Track) SetHead(h ...string) {
for i, k := range tr.container.HeadFields { for i, k := range tr.container.HeadFields {
if i >= len(h) { if i >= len(h) {
@ -112,7 +103,6 @@ type querySpec struct {
Limit int Limit int
Quvals []any Quvals []any
Cont *Container Cont *Container
Params lib.StrMap
} }
type ordSpec struct { type ordSpec struct {
@ -233,12 +223,23 @@ func (cont *Container) Insert(tr *Track) *Track {
} }
func (cont *Container) Update(tr *Track) *Track { func (cont *Container) Update(tr *Track) *Track {
if tr.TimeStamp == nil {
ts := time.Now()
tr.TimeStamp = &ts
}
quSpec := &querySpec{ quSpec := &querySpec{
Qucols: lib.StrSlice{"trackid"}, Scols: append(cont.HeadFields, "timestamp", "data"),
} }
quSpec.setup(cont) quSpec.setup(cont)
sql := storage.BuildSql(SqlUpdate, quSpec) sql := storage.BuildSql(SqlUpdate, quSpec)
n, _ := cont.Storage.Exec(sql, append(tr.AsSlice(), tr.trackId)...) vals := []any{tr.trackId}
for _, k := range cont.HeadFields {
vals = append(vals, tr.Head[k])
}
vals = append(vals, tr.TimeStamp)
b, _ := json.Marshal(tr.Data)
vals = append(vals, b)
n, _ := cont.Storage.Exec(sql, vals...)
if n == 1 { if n == 1 {
return tr return tr
} }
@ -246,7 +247,7 @@ func (cont *Container) Update(tr *Track) *Track {
} }
func ParseDateTime(inp string) *time.Time { func ParseDateTime(inp string) *time.Time {
ts, err := time.Parse("2006-01-02T15:04:05-07:00", inp) ts, err := time.Parse(time.RFC3339, inp)
if err == nil { if err == nil {
return &ts return &ts
} }

View file

@ -76,6 +76,8 @@ func TrackingTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
t.AssertEqual(tr2.Head["userName"], "john") t.AssertEqual(tr2.Head["userName"], "john")
tr3 := cont.QueryLast("", "john") tr3 := cont.QueryLast("", "john")
t.AssertEqual(tr3.Head["taskId"], "t01") t.AssertEqual(tr3.Head["taskId"], "t01")
tr3.Data = lib.Map{"desc": "go-scopes documentation"}
cont.Update(tr3)
} }
func MessageTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) { func MessageTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {