package tracking import ( "text/template" "git.sr.ht/~cco/go-scopes/storage" ) const ( sql_table = ` {{- $params := .Cont.Storage.Params -}} {{- $json := or $params.jsonType "json" -}} create table if not exists {{ .Table }} ( trackid {{ or $params.idType "integer" }} primary key, {{ range .Cont.HeadFields -}}{{ toLower . }} text default '', {{ end -}} timestamp timestamptz default current_timestamp, data {{ $json }} ); {{- 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 }} {{- end -}} ); {{- end }} 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 }} {{- end -}}) values ( {{- range $j, $c := .Scols -}} {{- if ne $j 0 -}}, {{ end }}${{ add1 $j }} {{- end -}}) returning trackid, timestamp` 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 }} from {{ .Table }} 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 }} ` ) var SqlCreate, SqlInsert, SqlUpdate, SqlSelect *template.Template func init() { SqlCreate = storage.Template("create_table", sql_table) SqlInsert = storage.Template("insert_track", sql_insert) SqlUpdate = storage.Template("update_track", sql_update) SqlSelect = storage.Template("select_track", sql_select) }