-
Notifications
You must be signed in to change notification settings - Fork 2
/
compiler.go
243 lines (216 loc) · 6.49 KB
/
compiler.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package goen
import (
"reflect"
sqr "github.com/Masterminds/squirrel"
"github.com/kamichidu/goen/dialect"
)
var (
DefaultCompiler PatchCompiler = &defaultCompiler{}
BulkCompiler PatchCompiler = &BulkCompilerOptions{}
)
type CompilerHook interface {
PostInsertBuilder(sqr.InsertBuilder) sqr.Sqlizer
PostUpdateBuilder(sqr.UpdateBuilder) sqr.Sqlizer
PostDeleteBuilder(sqr.DeleteBuilder) sqr.Sqlizer
}
type CompilerOptions struct {
Dialect dialect.Dialect
Patches *PatchList
Hook CompilerHook
}
type PatchCompiler interface {
Compile(*CompilerOptions) *SqlizerList
}
func CompilerWithHook(c PatchCompiler, v CompilerHook) PatchCompiler {
return PatchCompilerFunc(func(opts *CompilerOptions) *SqlizerList {
opts.Hook = v
return c.Compile(opts)
})
}
type PatchCompilerFunc func(*CompilerOptions) *SqlizerList
func (fn PatchCompilerFunc) Compile(opts *CompilerOptions) (sqlizers *SqlizerList) {
return fn(opts)
}
type compilerOptionsUtils CompilerOptions
func (opts *compilerOptionsUtils) StatementBuilder() sqr.StatementBuilderType {
v := sqr.StatementBuilder
if opts.Dialect != nil {
v = v.PlaceholderFormat(opts.Dialect.PlaceholderFormat())
}
return v
}
func (opts *compilerOptionsUtils) Quote(s string) string {
if opts.Dialect != nil {
s = opts.Dialect.Quote(s)
}
return s
}
func (opts *compilerOptionsUtils) Quotes(l []string) []string {
out := make([]string, len(l))
for i := range l {
out[i] = opts.Quote(l[i])
}
return out
}
func (opts *compilerOptionsUtils) RowKeyToSqlizer(rowKey RowKey) sqr.Sqlizer {
if rowKey == nil {
// always true
return sqr.Eq{}
}
if opts.Dialect != nil {
return rowKey.ToSqlizerWithDialect(opts.Dialect)
} else {
return rowKey
}
}
func (opts *compilerOptionsUtils) PostInsertBuilder(stmt sqr.InsertBuilder) sqr.Sqlizer {
if opts.Hook != nil {
return opts.Hook.PostInsertBuilder(stmt)
} else {
return stmt
}
}
func (opts *compilerOptionsUtils) PostUpdateBuilder(stmt sqr.UpdateBuilder) sqr.Sqlizer {
if opts.Hook != nil {
return opts.Hook.PostUpdateBuilder(stmt)
} else {
return stmt
}
}
func (opts *compilerOptionsUtils) PostDeleteBuilder(stmt sqr.DeleteBuilder) sqr.Sqlizer {
if opts.Hook != nil {
return opts.Hook.PostDeleteBuilder(stmt)
} else {
return stmt
}
}
type defaultCompiler struct{}
func (*defaultCompiler) Compile(options *CompilerOptions) (sqlizers *SqlizerList) {
opts := (*compilerOptionsUtils)(options)
stmtBuilder := opts.StatementBuilder()
sqlizers = NewSqlizerList()
for curr := opts.Patches.Front(); curr != nil; curr = curr.Next() {
patch := curr.GetValue()
if len(patch.Columns) != len(patch.Values) {
panic("goen: number of columns and values are mismatched")
}
switch patch.Kind {
case PatchInsert:
stmt := stmtBuilder.Insert(opts.Quote(patch.TableName)).
Columns(opts.Quotes(patch.Columns)...).
Values(patch.Values...)
sqlizers.PushBack(opts.PostInsertBuilder(stmt))
case PatchUpdate:
stmt := stmtBuilder.Update(opts.Quote(patch.TableName))
for i := range patch.Columns {
stmt = stmt.Set(opts.Quote(patch.Columns[i]), patch.Values[i])
}
stmt = stmt.Where(opts.RowKeyToSqlizer(patch.RowKey))
sqlizers.PushBack(opts.PostUpdateBuilder(stmt))
case PatchDelete:
stmt := stmtBuilder.Delete(opts.Quote(patch.TableName))
stmt = stmt.Where(opts.RowKeyToSqlizer(patch.RowKey))
sqlizers.PushBack(opts.PostDeleteBuilder(stmt))
default:
panic("goen: unable to make sql statement for unknown kind (" + string(patch.Kind) + ")")
}
}
return sqlizers
}
type BulkCompilerOptions struct {
// MaxPatches limits values per bulk operations.
// MaxPatches<=0 means unlimited.
MaxPatches int
}
func (c *BulkCompilerOptions) Compile(options *CompilerOptions) (sqlizers *SqlizerList) {
opts := (*compilerOptionsUtils)(options)
stmtBuilder := opts.StatementBuilder()
sqlizers = NewSqlizerList()
for curr := opts.Patches.Front(); curr != nil; curr = curr.Next() {
patch := curr.GetValue()
if len(patch.Columns) != len(patch.Values) {
panic("goen: number of columns and values are mismatched")
}
switch patch.Kind {
case PatchInsert:
stmt := stmtBuilder.Insert(opts.Quote(patch.TableName)).Columns(opts.Quotes(patch.Columns)...).Values(patch.Values...)
chunks := 1
for c.canTakeMoreChunks(chunks) && curr.Next() != nil && c.isCompat(patch, curr.Next().GetValue()) {
curr = curr.Next()
stmt = stmt.Values(curr.GetValue().Values...)
chunks++
}
sqlizers.PushBack(opts.PostInsertBuilder(stmt))
case PatchDelete:
stmt := stmtBuilder.Delete(opts.Quote(patch.TableName))
cond := sqr.Or{}
cond = append(cond, opts.RowKeyToSqlizer(patch.RowKey))
chunks := 1
for c.canTakeMoreChunks(chunks) && curr.Next() != nil && c.isCompat(patch, curr.Next().GetValue()) {
curr = curr.Next()
cond = append(cond, opts.RowKeyToSqlizer(curr.GetValue().RowKey))
chunks++
}
stmt = stmt.Where(cond)
sqlizers.PushBack(opts.PostDeleteBuilder(stmt))
case PatchUpdate:
stmt := stmtBuilder.Update(opts.Quote(patch.TableName))
for i := range patch.Columns {
stmt = stmt.Set(opts.Quote(patch.Columns[i]), patch.Values[i])
}
cond := sqr.Or{}
cond = append(cond, opts.RowKeyToSqlizer(patch.RowKey))
chunks := 1
for c.canTakeMoreChunks(chunks) && curr.Next() != nil && c.isCompat(patch, curr.Next().GetValue()) {
curr = curr.Next()
cond = append(cond, opts.RowKeyToSqlizer(curr.GetValue().RowKey))
chunks++
}
stmt = stmt.Where(cond)
sqlizers.PushBack(opts.PostUpdateBuilder(stmt))
default:
fallbackOpts := &CompilerOptions{
Dialect: opts.Dialect,
Patches: NewPatchList(),
Hook: opts.Hook,
}
// fallback a patch by patch, for keeping its order
fallbackOpts.Patches.PushBack(patch)
sqlizers.PushBackList(DefaultCompiler.Compile(fallbackOpts))
}
}
return sqlizers
}
func (c *BulkCompilerOptions) isCompat(p1, p2 *Patch) bool {
if p1.Kind != p2.Kind {
return false
}
if p1.TableName != p2.TableName {
return false
}
if len(p1.Columns) != len(p2.Columns) {
return false
} else {
for i := range p1.Columns {
if p1.Columns[i] != p2.Columns[i] {
return false
}
}
}
switch p1.Kind {
case PatchUpdate:
// do not use "database/sql/driver".Valuer.
// it's for converting go type to sql type; type converting.
// if converts the actual value, maybe illegal implementation.
if !reflect.DeepEqual(p1.Values, p2.Values) {
return false
}
}
return true
}
func (c *BulkCompilerOptions) canTakeMoreChunks(chunks int) bool {
if c.MaxPatches <= 0 {
return true
}
return chunks < c.MaxPatches
}