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 -}}
|
||||
{{- if ne $j 0 -}}, {{ end }}{{ toLower $c }}{{- end }}
|
||||
from {{ .Table }}
|
||||
where {{ range $j, $c := .Qucols -}}
|
||||
{{- if ne $j 0 }} and {{ end }}{{ toLower $c }} = ${{ add1 $j }}{{- end }}
|
||||
{{ with .Ordcols -}}
|
||||
order by {{ range $j, $c := . -}}
|
||||
{{- if ne $j 0 -}}, {{ end }}{{ toLower $c }}{{- end -}}
|
||||
where {{ range $j, $s := .Quspecs -}}
|
||||
{{- if ne $j 0 }} and {{ end -}}
|
||||
{{- toLower $s.Col }} {{ $s.Op }} ${{ add1 $j }}{{ end }}
|
||||
{{ with .Ordspecs -}}
|
||||
order by {{ range $j, $s := . -}}
|
||||
{{- if ne $j 0 -}}, {{ end }}{{ toLower $s.Col }}
|
||||
{{- if $s.Desc }} desc{{ end }}
|
||||
{{- end -}}
|
||||
{{- end }}
|
||||
{{ with .Limit }}limit {{ . }}{{ end }}
|
||||
`
|
||||
|
|
|
@ -93,7 +93,23 @@ func Tracks(db *sql.Storage) *Container {
|
|||
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 == "" {
|
||||
spec.Table = cont.TableName
|
||||
} else {
|
||||
|
@ -108,21 +124,12 @@ func (cont *Container) setupQuerySpec(spec *querySpec) *querySpec {
|
|||
spec.Quvals = append(spec.Quvals, v)
|
||||
}
|
||||
}
|
||||
return spec
|
||||
}
|
||||
|
||||
type querySpec struct {
|
||||
Table string
|
||||
Headvals lib.StrSlice
|
||||
Scols lib.StrSlice
|
||||
Qucols, Ordcols lib.StrSlice
|
||||
Quspecs []struct{ col, op string }
|
||||
Ordspecs []struct {
|
||||
Col string
|
||||
Desc bool
|
||||
for _, c := range spec.Qucols {
|
||||
spec.Quspecs = append(spec.Quspecs, struct{ Col, Op string }{c, "="})
|
||||
}
|
||||
for _, c := range spec.Ordcols {
|
||||
spec.Ordspecs = append(spec.Ordspecs, ordSpec{c, false})
|
||||
}
|
||||
Limit int
|
||||
Quvals []any
|
||||
}
|
||||
|
||||
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 {
|
||||
quSpec := &querySpec{
|
||||
Headvals: hv,
|
||||
Ordcols: lib.StrSlice{"timestamp"},
|
||||
Limit: 1,
|
||||
}
|
||||
quSpec.Ordspecs = append(quSpec.Ordspecs, ordSpec{"timestamp", true})
|
||||
return cont.QueryOne(quSpec)
|
||||
}
|
||||
|
||||
|
@ -149,7 +156,7 @@ func (cont *Container) QueryOne(quSpec *querySpec) *Track {
|
|||
tr = cont.ItemFactory(cont)
|
||||
return tr.ScanP(r)
|
||||
}
|
||||
cont.setupQuerySpec(quSpec)
|
||||
quSpec.setup(cont)
|
||||
sql := storage.BuildSql(SqlSelect, quSpec)
|
||||
db.Query(proc, sql, quSpec.Quvals...)
|
||||
return tr
|
||||
|
|
Loading…
Add table
Reference in a new issue