-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
init.go
439 lines (366 loc) · 9.08 KB
/
init.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package core
import (
"fmt"
"strings"
"unicode"
"github.com/dosco/graphjin/core/internal/qcode"
"github.com/dosco/graphjin/core/internal/sdata"
"github.com/gobuffalo/flect"
)
func (gj *GraphJin) initConfig() error {
c := gj.conf
if gj.conf.EnableInflection {
if err := initInflection(c); err != nil {
return err
}
}
tm := make(map[string]struct{})
for _, t := range c.Tables {
k := strings.ToLower(t.Schema + t.Name)
if _, ok := tm[k]; ok {
return fmt.Errorf("duplicate table found: %s", t.Name)
}
tm[k] = struct{}{}
}
for k, v := range c.Vars {
if v == "" || !strings.HasPrefix(v, "sql:") {
continue
}
if n, ok := isASCII(v); !ok {
return fmt.Errorf("variables: %s: invalid character (%s) at %d",
k, c.RolesQuery[:n+1], n+1)
}
}
gj.roles = make(map[string]*Role)
for i, role := range c.Roles {
k := strings.ToLower(role.Name)
if _, ok := gj.roles[k]; ok {
return fmt.Errorf("duplicate role found: %s", role.Name)
}
role.Match = sanitize(role.Match)
role.tm = make(map[string]*RoleTable)
for n, t := range role.Tables {
role.tm[t.Schema+t.Name] = &role.Tables[n]
}
gj.roles[k] = &c.Roles[i]
}
// If user role not defined then create it
if _, ok := gj.roles["user"]; !ok {
ur := Role{
Name: "user",
tm: make(map[string]*RoleTable),
}
gj.roles["user"] = &ur
}
// If anon role is not defined then create it
if _, ok := gj.roles["anon"]; !ok {
ur := Role{
Name: "anon",
tm: make(map[string]*RoleTable),
}
gj.roles["anon"] = &ur
}
if c.RolesQuery != "" {
if n, ok := isASCII(c.RolesQuery); !ok {
return fmt.Errorf("roles_query: invalid character (%s) at %d",
c.RolesQuery[:n+1], n+1)
}
// More than 2 roles tell us that custom roles have been added
// hence ABAC is handled
gj.abacEnabled = (len(gj.roles) > 2)
}
return nil
}
func initInflection(c *Config) error {
for _, v := range c.Inflections {
kv := strings.SplitN(v, ":", 2)
if kv[0] == "" || kv[1] == "" {
return fmt.Errorf("inflections: should be an array of singular:plural values: %s", v)
}
flect.AddPlural(kv[0], kv[1])
flect.AddSingular(kv[1], kv[0])
}
return nil
}
func addTableInfo(c *Config, t Table) error {
obm := map[string][][2]string{}
for k, ob := range t.OrderBy {
for _, v := range ob {
vals := strings.SplitN(v, " ", 2)
obm[k] = append(obm[k], [2]string{vals[0], vals[1]})
}
}
if c.tmap == nil {
c.tmap = make(map[string]qcode.TConfig)
}
c.tmap[(t.Schema + t.Name)] = qcode.TConfig{OrderBy: obm}
return nil
}
func getDBTableAliases(c *Config) map[string][]string {
m := make(map[string][]string, len(c.Tables))
for i := range c.Tables {
t := c.Tables[i]
if t.Table != "" && t.Type == "" {
m[t.Table] = append(m[t.Table], t.Name)
}
}
return m
}
func addTables(conf *Config, di *sdata.DBInfo) error {
var err error
for _, t := range conf.Tables {
// skip aliases
if t.Table != "" && t.Type == "" {
continue
}
switch t.Type {
case "json", "jsonb":
err = addJsonTable(conf, di, t)
case "polymorphic":
err = addVirtualTable(conf, di, t)
default:
err = updateTable(conf, di, t)
}
if err != nil {
return err
}
}
return nil
}
func updateTable(conf *Config, di *sdata.DBInfo, t Table) error {
t1, err := di.GetTable(t.Schema, t.Name)
if err != nil {
return fmt.Errorf("table: %w", err)
}
for _, c := range t.Columns {
c1, err := di.GetColumn(t.Schema, t.Name, c.Name)
if err != nil {
return err
}
if c.Primary {
c1.PrimaryKey = true
t1.PrimaryCol = *c1
}
if c.Array {
c1.Array = true
}
}
return nil
}
func addJsonTable(conf *Config, di *sdata.DBInfo, t Table) error {
// This is for jsonb column that want to be a table.
if t.Table == "" {
return fmt.Errorf("json table: set the 'table' for column '%s'", t.Name)
}
bc, err := di.GetColumn(t.Schema, t.Table, t.Name)
if err != nil {
return fmt.Errorf("json table: %w", err)
}
bt, err := di.GetTable(bc.Schema, bc.Table)
if err != nil {
return fmt.Errorf("json table: %w", err)
}
if bc.Type != "json" && bc.Type != "jsonb" {
return fmt.Errorf(
"json table: column '%s' in table '%s' is of type '%s'. Only JSON or JSONB is valid",
t.Name, t.Table, bc.Type)
}
columns := make([]sdata.DBColumn, 0, len(t.Columns))
for i := range t.Columns {
c := t.Columns[i]
columns = append(columns, sdata.DBColumn{
ID: c.ID,
Schema: bc.Schema,
Table: t.Name,
Name: c.Name,
Type: c.Type,
})
if c.Type == "" {
return fmt.Errorf("json table: type parameter missing for column: %s.%s'",
t.Name, c.Name)
}
}
col1 := sdata.DBColumn{
ID: bc.ID,
PrimaryKey: true,
Schema: bc.Schema,
Table: bc.Table,
Name: bc.Name,
Type: bc.Type,
}
nt := sdata.NewDBTable(bc.Schema, t.Name, bc.Type, columns)
nt.PrimaryCol = col1
nt.SecondaryCol = bt.PrimaryCol
di.AddTable(nt)
return nil
}
func addVirtualTable(conf *Config, di *sdata.DBInfo, t Table) error {
if len(t.Columns) == 0 {
return fmt.Errorf("polymorphic table: no id column specified")
}
c := t.Columns[0]
if c.ForeignKey == "" {
return fmt.Errorf("polymorphic table: no 'related_to' specified on id column")
}
s, ok := c.getFK(di.Schema)
if !ok {
return fmt.Errorf("polymorphic table: foreign key must be <type column>.<foreign key column>")
}
di.VTables = append(di.VTables, sdata.VirtualTable{
Name: t.Name,
IDColumn: c.Name,
TypeColumn: s[1],
FKeyColumn: s[2],
})
return nil
}
func addForeignKeys(conf *Config, di *sdata.DBInfo) error {
for _, t := range conf.Tables {
if t.Type == "polymorphic" {
continue
}
for _, c := range t.Columns {
if c.ForeignKey == "" {
continue
}
if err := addForeignKey(conf, di, c, t); err != nil {
return err
}
}
}
return nil
}
func addForeignKey(conf *Config, di *sdata.DBInfo, c Column, t Table) error {
c1, err := di.GetColumn(t.Schema, t.Name, c.Name)
if err != nil {
return fmt.Errorf("config: add foreign key: %w", err)
}
v, ok := c.getFK(di.Schema)
if !ok {
return fmt.Errorf(
"config: invalid foreign key defined for table '%s' and column '%s': %s",
t.Name, c.Name, c.ForeignKey)
}
// check if it's a polymorphic foreign key
if _, err := di.GetColumn(t.Schema, t.Name, v[1]); err == nil {
c2, err := di.GetColumn(t.Schema, t.Name, v[2])
if err != nil {
return fmt.Errorf(
"config: invalid column '%s' for polymorphic relationship on table '%s' and column '%s'",
v[2], t.Name, c.Name)
}
c1.FKeySchema = t.Schema
c1.FKeyTable = v[1]
c1.FKeyCol = c2.Name
return nil
}
fks, fkt, fkc := v[0], v[1], v[2]
c3, err := di.GetColumn(fks, fkt, fkc)
if err != nil {
return fmt.Errorf(
"config: foreign key for table '%s' and column '%s' points to unknown table '%s.%s' and column '%s'",
t.Name, c.Name, fks, fkt, fkc)
}
c1.FKeySchema = fks
c1.FKeyTable = fkt
c1.FKeyCol = c3.Name
return nil
}
func addRoles(c *Config, qc *qcode.Compiler) error {
for _, r := range c.Roles {
for _, t := range r.Tables {
if err := addRole(qc, r, t, c.DefaultBlock); err != nil {
return err
}
}
}
return nil
}
func addRole(qc *qcode.Compiler, r Role, t RoleTable, defaultBlock bool) error {
ro := false // read-only
if defaultBlock && r.Name == "anon" {
ro = true
}
if t.ReadOnly {
ro = true
}
query := qcode.QueryConfig{Block: false}
insert := qcode.InsertConfig{Block: ro}
update := qcode.UpdateConfig{Block: ro}
upsert := qcode.UpsertConfig{Block: ro}
del := qcode.DeleteConfig{Block: ro}
if t.Query != nil {
query = qcode.QueryConfig{
Limit: t.Query.Limit,
Filters: t.Query.Filters,
Columns: t.Query.Columns,
DisableFunctions: t.Query.DisableFunctions,
Block: t.Query.Block,
}
}
if t.Insert != nil {
insert = qcode.InsertConfig{
Columns: t.Insert.Columns,
Presets: t.Insert.Presets,
Block: t.Insert.Block,
}
}
if t.Update != nil {
update = qcode.UpdateConfig{
Filters: t.Update.Filters,
Columns: t.Update.Columns,
Presets: t.Update.Presets,
Block: t.Update.Block,
}
}
if t.Upsert != nil {
upsert = qcode.UpsertConfig{
Filters: t.Update.Filters,
Columns: t.Update.Columns,
Presets: t.Update.Presets,
Block: t.Update.Block,
}
}
if t.Delete != nil {
del = qcode.DeleteConfig{
Filters: t.Delete.Filters,
Columns: t.Delete.Columns,
Block: t.Delete.Block,
}
}
return qc.AddRole(r.Name, t.Schema, t.Name, qcode.TRConfig{
Query: query,
Insert: insert,
Update: update,
Upsert: upsert,
Delete: del,
})
}
func (r *Role) GetTable(schema, name string) *RoleTable {
return r.tm[name]
}
func (c *Column) getFK(defaultSchema string) ([3]string, bool) {
var ret [3]string
var ok bool
v := strings.SplitN(c.ForeignKey, ".", 3)
if len(v) == 2 {
ret = [3]string{defaultSchema, v[0], v[1]}
ok = true
}
if len(v) == 3 {
ret = [3]string{v[0], v[1], v[2]}
ok = true
}
return ret, ok
}
func sanitize(value string) string {
return strings.ToLower(strings.TrimSpace(value))
}
func isASCII(s string) (int, bool) {
for i := 0; i < len(s); i++ {
if s[i] > unicode.MaxASCII {
return i, false
}
}
return -1, true
}