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{
"toLower": strings.ToLower,
"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 {

View file

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

View file

@ -43,15 +43,6 @@ func MakeTrack(cont *Container, h ...string) *Track {
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) {
for i, k := range tr.container.HeadFields {
if i >= len(h) {
@ -112,7 +103,6 @@ type querySpec struct {
Limit int
Quvals []any
Cont *Container
Params lib.StrMap
}
type ordSpec struct {
@ -233,12 +223,23 @@ func (cont *Container) Insert(tr *Track) *Track {
}
func (cont *Container) Update(tr *Track) *Track {
if tr.TimeStamp == nil {
ts := time.Now()
tr.TimeStamp = &ts
}
quSpec := &querySpec{
Qucols: lib.StrSlice{"trackid"},
Scols: append(cont.HeadFields, "timestamp", "data"),
}
quSpec.setup(cont)
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 {
return tr
}
@ -246,7 +247,7 @@ func (cont *Container) Update(tr *Track) *Track {
}
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 {
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")
tr3 := cont.QueryLast("", "john")
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) {