-
-
Notifications
You must be signed in to change notification settings - Fork 177
/
config.go
304 lines (262 loc) · 6.06 KB
/
config.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package qcode
import (
"github.com/gobuffalo/flect"
)
type Config struct {
Vars map[string]string
TConfig map[string]TConfig
FragmentFetcher func(namespace string) func(name string) (string, error)
DefaultBlock bool
DefaultLimit int
DisableAgg bool
DisableFuncs bool
EnableCamelcase bool
EnableInflection bool
DBSchema string
defTrv trval
}
type TConfig struct {
OrderBy map[string][][2]string
}
type TRConfig struct {
Query QueryConfig
Insert InsertConfig
Update UpdateConfig
Upsert UpsertConfig
Delete DeleteConfig
}
type QueryConfig struct {
Limit int
Filters []string
Columns []string
DisableFunctions bool
Block bool
}
type InsertConfig struct {
Columns []string
Presets map[string]string
Block bool
}
type UpdateConfig struct {
Filters []string
Columns []string
Presets map[string]string
Block bool
}
type UpsertConfig struct {
Filters []string
Columns []string
Presets map[string]string
Block bool
}
type DeleteConfig struct {
Filters []string
Columns []string
Block bool
}
type trval struct {
role string
query struct {
limit int32
fil *Exp
filNU bool
cols map[string]struct{}
disable struct{ funcs bool }
block bool
}
insert struct {
cols map[string]struct{}
presets map[string]string
block bool
}
update struct {
fil *Exp
filNU bool
cols map[string]struct{}
presets map[string]string
block bool
}
upsert struct {
fil *Exp
filNU bool
cols map[string]struct{}
presets map[string]string
block bool
}
delete struct {
fil *Exp
filNU bool
cols map[string]struct{}
block bool
}
}
func (co *Compiler) AddRole(role, schema, table string, trc TRConfig) error {
var err error
ti, err := co.s.Find(schema, table)
if err != nil {
return err
}
trv := trval{role: role}
// query config
trv.query.fil, trv.query.filNU, err = compileFilter(co.s, ti, trc.Query.Filters, false)
if err != nil {
return err
}
if trc.Query.Limit > 0 {
trv.query.limit = int32(trc.Query.Limit)
}
trv.query.cols = makeSet(trc.Query.Columns)
trv.query.disable.funcs = trc.Query.DisableFunctions
trv.query.block = trc.Query.Block
// insert config
trv.insert.cols = makeSet(trc.Insert.Columns)
trv.insert.presets = trc.Insert.Presets
trv.insert.block = trc.Insert.Block
// update config
trv.update.fil, trv.update.filNU, err = compileFilter(co.s, ti, trc.Update.Filters, false)
if err != nil {
return err
}
trv.update.cols = makeSet(trc.Update.Columns)
trv.update.presets = trc.Update.Presets
trv.update.block = trc.Update.Block
// upsert config
trv.upsert.fil, trv.upsert.filNU, err = compileFilter(co.s, ti, trc.Upsert.Filters, false)
if err != nil {
return err
}
trv.upsert.cols = makeSet(trc.Upsert.Columns)
trv.upsert.presets = trc.Upsert.Presets
trv.upsert.block = trc.Upsert.Block
// delete config
trv.delete.fil, trv.delete.filNU, err = compileFilter(co.s, ti, trc.Delete.Filters, false)
if err != nil {
return err
}
trv.delete.cols = makeSet(trc.Delete.Columns)
trv.delete.block = trc.Delete.Block
if schema == "" {
schema = co.s.DBSchema()
}
if co.c.EnableInflection {
singular := flect.Singularize(table)
plural := flect.Pluralize(table)
co.tr[(role + ":" + schema + ":" + singular)] = trv
co.tr[(role + ":" + schema + ":" + plural)] = trv
} else {
co.tr[(role + ":" + schema + ":" + table)] = trv
}
return nil
}
func (co *Compiler) getRole(role, schema, table, field string) trval {
var k string
if co.s.IsAlias(field) {
k = (role + ":" + schema + ":" + field)
} else {
k = (role + ":" + schema + ":" + table)
}
// For anon roles when a trval is not found return the default trval
tr, ok := co.tr[k]
tr.role = role
if !ok && role == "anon" {
return co.c.defTrv
}
return tr
}
func (co *Compiler) getTConfig(schema, name string) TConfig {
return co.c.TConfig[(schema + name)]
}
func (trv *trval) filter(qt QType) (*Exp, bool) {
switch qt {
case QTQuery:
return trv.query.fil, trv.query.filNU
case QTInsert:
return nil, false
case QTUpdate:
return trv.update.fil, trv.update.filNU
case QTUpsert:
return trv.upsert.fil, trv.upsert.filNU
case QTDelete:
return trv.delete.fil, trv.delete.filNU
}
return nil, false
}
func (trv *trval) columnAllowed(qt *QCode, name string) bool {
switch qt.SType {
case QTQuery:
_, ok := trv.query.cols[name]
return ok || len(trv.query.cols) == 0
case QTInsert:
_, ok := trv.insert.cols[name]
return ok || len(trv.insert.cols) == 0
case QTUpdate:
_, ok := trv.update.cols[name]
return ok || len(trv.update.cols) == 0
case QTUpsert:
_, ok := trv.upsert.cols[name]
return ok || len(trv.upsert.cols) == 0
case QTDelete:
_, ok := trv.delete.cols[name]
return ok || len(trv.delete.cols) == 0
}
return false
}
func (trv *trval) limit(qt QType) int32 {
if qt == QTQuery && trv.query.limit != 0 {
return trv.query.limit
}
return 0
}
func (trv *trval) isBlocked(qt QType) bool {
switch qt {
case QTQuery:
return trv.query.block
case QTInsert:
return trv.insert.block
case QTUpdate:
return trv.update.block
case QTUpsert:
return trv.upsert.block
case QTDelete:
return trv.delete.block
}
return false
}
func (trv *trval) isFuncsBlocked() bool {
return trv.query.disable.funcs
}
// func (trv *trval) isMutationBlocked(mt MType, name string) error {
// var blocked bool
// switch mt {
// case MTInsert:
// blocked = trv.insert.block
// case MTUpdate:
// blocked = trv.update.block
// case MTUpset:
// blocked = trv.upsert.block
// case MTDelete:
// blocked = trv.upsert.block
// }
// if blocked {
// return fmt.Errorf("%s blocked: %s", item.Key)
// }
// return nil
// }
func (trv *trval) getPresets(mt MType) map[string]string {
switch mt {
case MTInsert:
return trv.insert.presets
case MTUpdate:
return trv.update.presets
case MTUpsert:
return trv.upsert.presets
}
return nil
}
func makeSet(list []string) map[string]struct{} {
m := make(map[string]struct{}, len(list))
for i := range list {
m[list[i]] = struct{}{}
}
return m
}