parameterize where clause operators and order by 'desc' attribute
This commit is contained in:
parent
53e0759e9f
commit
43dfd41e5d
2 changed files with 32 additions and 22 deletions
|
@ -44,11 +44,14 @@ insert into {{ $tablename }} (
|
||||||
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, $c := .Qucols -}}
|
where {{ range $j, $s := .Quspecs -}}
|
||||||
{{- if ne $j 0 }} and {{ end }}{{ toLower $c }} = ${{ add1 $j }}{{- end }}
|
{{- if ne $j 0 }} and {{ end -}}
|
||||||
{{ with .Ordcols -}}
|
{{- toLower $s.Col }} {{ $s.Op }} ${{ add1 $j }}{{ end }}
|
||||||
order by {{ range $j, $c := . -}}
|
{{ with .Ordspecs -}}
|
||||||
{{- if ne $j 0 -}}, {{ end }}{{ toLower $c }}{{- end -}}
|
order by {{ range $j, $s := . -}}
|
||||||
|
{{- if ne $j 0 -}}, {{ end }}{{ toLower $s.Col }}
|
||||||
|
{{- if $s.Desc }} desc{{ end }}
|
||||||
|
{{- end -}}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{ with .Limit }}limit {{ . }}{{ end }}
|
{{ with .Limit }}limit {{ . }}{{ end }}
|
||||||
`
|
`
|
||||||
|
|
|
@ -93,7 +93,23 @@ func Tracks(db *sql.Storage) *Container {
|
||||||
return &Container{container_definition, db}
|
return &Container{container_definition, db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cont *Container) setupQuerySpec(spec *querySpec) *querySpec {
|
type querySpec struct {
|
||||||
|
Table string
|
||||||
|
Headvals lib.StrSlice
|
||||||
|
Scols lib.StrSlice
|
||||||
|
Qucols, Ordcols lib.StrSlice
|
||||||
|
Quspecs []struct{ Col, Op string }
|
||||||
|
Ordspecs []ordSpec
|
||||||
|
Limit int
|
||||||
|
Quvals []any
|
||||||
|
}
|
||||||
|
|
||||||
|
type ordSpec struct {
|
||||||
|
Col string
|
||||||
|
Desc bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (spec *querySpec) setup(cont *Container) {
|
||||||
if cont.Storage.Schema == "" {
|
if cont.Storage.Schema == "" {
|
||||||
spec.Table = cont.TableName
|
spec.Table = cont.TableName
|
||||||
} else {
|
} else {
|
||||||
|
@ -108,21 +124,12 @@ func (cont *Container) setupQuerySpec(spec *querySpec) *querySpec {
|
||||||
spec.Quvals = append(spec.Quvals, v)
|
spec.Quvals = append(spec.Quvals, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return spec
|
for _, c := range spec.Qucols {
|
||||||
}
|
spec.Quspecs = append(spec.Quspecs, struct{ Col, Op string }{c, "="})
|
||||||
|
}
|
||||||
type querySpec struct {
|
for _, c := range spec.Ordcols {
|
||||||
Table string
|
spec.Ordspecs = append(spec.Ordspecs, ordSpec{c, false})
|
||||||
Headvals lib.StrSlice
|
|
||||||
Scols lib.StrSlice
|
|
||||||
Qucols, Ordcols lib.StrSlice
|
|
||||||
Quspecs []struct{ col, op string }
|
|
||||||
Ordspecs []struct {
|
|
||||||
Col string
|
|
||||||
Desc bool
|
|
||||||
}
|
}
|
||||||
Limit int
|
|
||||||
Quvals []any
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cont *Container) Get(id lib.Ident) *Track {
|
func (cont *Container) Get(id lib.Ident) *Track {
|
||||||
|
@ -136,9 +143,9 @@ func (cont *Container) Get(id lib.Ident) *Track {
|
||||||
func (cont *Container) QueryLast(hv ...string) *Track {
|
func (cont *Container) QueryLast(hv ...string) *Track {
|
||||||
quSpec := &querySpec{
|
quSpec := &querySpec{
|
||||||
Headvals: hv,
|
Headvals: hv,
|
||||||
Ordcols: lib.StrSlice{"timestamp"},
|
|
||||||
Limit: 1,
|
Limit: 1,
|
||||||
}
|
}
|
||||||
|
quSpec.Ordspecs = append(quSpec.Ordspecs, ordSpec{"timestamp", true})
|
||||||
return cont.QueryOne(quSpec)
|
return cont.QueryOne(quSpec)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,7 +156,7 @@ func (cont *Container) QueryOne(quSpec *querySpec) *Track {
|
||||||
tr = cont.ItemFactory(cont)
|
tr = cont.ItemFactory(cont)
|
||||||
return tr.ScanP(r)
|
return tr.ScanP(r)
|
||||||
}
|
}
|
||||||
cont.setupQuerySpec(quSpec)
|
quSpec.setup(cont)
|
||||||
sql := storage.BuildSql(SqlSelect, quSpec)
|
sql := storage.BuildSql(SqlSelect, quSpec)
|
||||||
db.Query(proc, sql, quSpec.Quvals...)
|
db.Query(proc, sql, quSpec.Quvals...)
|
||||||
return tr
|
return tr
|
||||||
|
|
Loading…
Add table
Reference in a new issue