-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
189 lines (158 loc) · 4.87 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package recordlite
import (
"bytes"
"crypto/sha1"
"fmt"
"strings"
"text/template"
)
type (
ColumnDef struct {
// Name of the column to use.
Name string
// Expression of the column.
Expr string
// Indicates if the column's expression should be indexed in the raw table.
// The expression's string is hashed to avoid re-creating indices. This has
// the side effect that simple aesthetic change to the expression will
// trigger a full index rebuild for said column.
WithIndex bool `json:"with_index"`
}
ViewDef struct {
// Name of the view. The raw sources's table will be named `${Name}_raw`.
Name string
Columns []ColumnDef
// If enabled, the statements for the view's triggers will not be generated.
SkipTriggers bool `json:"skip_triggers"`
// If enabled, the statements for the columns' index will not be generated.
SkipIndices bool `json:"skip_indices"`
// If enabled, indices not defined by the columns will be dropped.
UnsafeDropOrphanIndices bool `json:"unsafe_drop_orphan_indices"`
}
)
// CompileViewDef returns an SQL statement
func CompileViewDef(def *ViewDef) (string, error) {
buf := bytes.NewBufferString("")
if err := root.Execute(buf, def); err != nil {
return "", fmt.Errorf("pbql: failed executing template: %w", err)
}
return strings.Trim(buf.String(), "\n"), nil
}
func (t *ViewDef) Table() string {
return fmt.Sprintf("%s_raw", t.Name)
}
func (t *ViewDef) View() string {
return t.Name
}
func (t *ViewDef) IndexNames() string {
var names []string
for _, col := range t.Columns {
if col.WithIndex {
names = append(names, fmt.Sprintf("'%s'", col.IndexName()))
}
}
return strings.Join(names, ",\n ")
}
func (c *ColumnDef) SelectExpr(last bool) string {
comma := ","
if last {
comma = ""
}
return fmt.Sprintf("%s AS %s%s\n", c.Expr, c.Name, comma)
}
const indexPrefix = "_col_expr"
func (c *ColumnDef) IndexName() string {
return fmt.Sprintf("%s_%s_%x", indexPrefix, c.Name, sha1.Sum([]byte(c.Expr)))
}
func (c *ColumnDef) CreateIndexStatement(table string) string {
return fmt.Sprintf("CREATE INDEX IF NOT EXISTS %s\n ON %s(%s);\n\n", c.IndexName(), table, c.Expr)
}
var funcs = map[string]interface{}{
// A tiny function helper that allows handling commas.
"last": func(index, size int) bool { return (index + 1) == size },
"indexPrefix": func() string { return indexPrefix },
}
func mustParse(tpl *template.Template, payload string) *template.Template {
tpl, err := tpl.Parse(strings.TrimSpace(payload))
if err != nil {
panic(fmt.Sprintf("failed parsing: %s", err.Error()))
}
return tpl
}
var (
root = mustParse(template.New("root"), rootTemplateStr).Funcs(funcs)
tableTemplate = mustParse(root.New("table"), tableTemplateStr)
viewTemplate = mustParse(root.New("view"), viewTemplateStr)
triggersTemplate = mustParse(root.New("triggers"), triggersTemplateStr)
indicesTemplate = mustParse(root.New("indices"), indicesTemplateStr)
)
// Global definitions used by sub-templates.
var rootTemplateStr = `
BEGIN EXCLUSIVE;
{{template "table" .}}
--
-- View
--
{{template "view" .}}
{{if not .SkipTriggers -}}
--
-- Triggers
--
-- The trigger helpers enable applications to write in the view and avoid
-- the writing in the raw table. The restriction is that they can only reference
-- the 'raw' column.
{{template "triggers" .}}
{{end -}}
{{if not .SkipIndices -}}
--
-- Indices
--
{{template "indices" . }}
{{end -}}
COMMIT;
`
var tableTemplateStr = `
CREATE TABLE IF NOT EXISTS {{.Table}} (
id INTEGER PRIMARY KEY NOT NULL,
raw BLOB NOT NULL
);
`
var viewTemplateStr = `
{{ $nCols := (.Columns | len) -}}
DROP VIEW IF EXISTS {{.View}};
CREATE VIEW IF NOT EXISTS {{.View}} AS
SELECT
id,
raw {{- if .Columns}},{{end}}
{{range $index, $val := .Columns}} {{last $index $nCols | .SelectExpr}}{{end -}}
FROM {{.Table}};`
var triggersTemplateStr = `
DROP TRIGGER IF EXISTS {{.View}}_insert;
CREATE TRIGGER IF NOT EXISTS {{.View}}_insert INSTEAD OF INSERT ON {{.View}}
BEGIN
INSERT INTO {{.Table}}(raw) VALUES(NEW.raw);
END;
DROP TRIGGER IF EXISTS {{.View}}_update;
CREATE TRIGGER IF NOT EXISTS {{.View}}_update INSTEAD OF UPDATE ON {{.View}}
BEGIN
UPDATE {{.Table}} SET raw = NEW.raw WHERE id = OLD.id;
END;
DROP TRIGGER IF EXISTS {{.View}}_delete;
CREATE TRIGGER IF NOT EXISTS {{.View}}_delete INSTEAD OF DELETE ON {{.View}}
BEGIN
DELETE FROM {{.Table}} WHERE id = OLD.id;
END;
`
var indicesTemplateStr = `
{{$table := .Table }}{{$unsafe_drop := .UnsafeDropOrphanIndices}}
{{- range .Columns }}{{if .WithIndex}}{{$table | .CreateIndexStatement}}{{end}}{{end}}
{{- if $unsafe_drop -}}
-- Delete all indices that were previously defined by us, but are not included
-- in the lasted definition.
PRAGMA writable_schema = 1;
DELETE FROM sqlite_master
WHERE type = 'index' AND tbl_name = '{{.Table}}' AND
(name LIKE '{{indexPrefix}}_%' AND name NOT IN ({{.IndexNames}}));
PRAGMA writable_schema = 0;
{{end}}
`