64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
package tracking
|
|
|
|
import (
|
|
"text/template"
|
|
|
|
"git.sr.ht/~cco/go-scopes/storage"
|
|
)
|
|
|
|
const (
|
|
sql_table = `
|
|
{{- $tablename := or (and .schema (printf "%s.%s" .schema .tablename)) .tablename -}}
|
|
{{- $json := or .params.jsonType "json" -}}
|
|
create table if not exists {{ $tablename }} (
|
|
trackid {{ or .params.idType "integer" }} primary key,
|
|
{{ range .headFields -}}{{ toLower . }} text default '',
|
|
{{ end -}}
|
|
timestamp timestamptz default current_timestamp,
|
|
data {{ $json }}
|
|
);
|
|
|
|
{{- range $i, $cols := .indexes }}
|
|
create index idx_{{ $.tablename }}_{{ add1 $i }} on {{ $tablename }} (
|
|
{{- range $j, $c := $cols -}}
|
|
{{- if ne $j 0 -}}, {{ end }}{{ toLower $c }}
|
|
{{- end -}}
|
|
);
|
|
{{- end }}
|
|
create index idx_{{ $.tablename }}_ts on {{ $tablename }} (timestamp);
|
|
`
|
|
|
|
sql_insert = `
|
|
{{- $tablename := or (and .schema (printf "%s.%s" .schema .tablename)) .tablename -}}
|
|
insert into {{ $tablename }} (
|
|
{{- range $j, $c := .columns -}}
|
|
{{- if ne $j 0 -}}, {{ end }}{{ toLower $c }}
|
|
{{- end -}})
|
|
values (
|
|
{{- range $j, $c := .columns -}}
|
|
{{- if ne $j 0 -}}, {{ end }}${{ add1 $j }}
|
|
{{- end -}})
|
|
returning trackid, timestamp`
|
|
|
|
sql_select = `
|
|
{{- $tablename := or (and .schema (printf "%s.%s" .schema .tablename)) .tablename -}}
|
|
select {{ range $j, $c := .scols -}}
|
|
{{- if ne $j 0 -}}, {{ end }}{{ toLower $c }}{{- end }}
|
|
from {{ $tablename }}
|
|
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 -}}
|
|
{{- end }}
|
|
{{ with .limit }}limit {{ . }}{{ end }}
|
|
`
|
|
)
|
|
|
|
var SqlCreate, SqlInsert, SqlSelect *template.Template
|
|
|
|
func init() {
|
|
SqlCreate = storage.Template("create_table", sql_table)
|
|
SqlInsert = storage.Template("insert_track", sql_insert)
|
|
SqlSelect = storage.Template("select_track", sql_select)
|
|
}
|