/
loader.go
775 lines (643 loc) · 17.7 KB
/
loader.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
package internal
import (
"errors"
"fmt"
"strings"
"github.com/gedex/inflector"
"github.com/knq/snaker"
"github.com/xo/xo/models"
)
// Loader is the common interface for database drivers that can generate code
// from a database schema.
type Loader interface {
// NthParam returns the 0-based Nth param for the Loader.
NthParam(i int) string
// Mask returns the mask.
Mask() string
// Escape escapes the passed identifier based on its EscType.
Escape(EscType, string) string
// Relkind returns the schema's relkind identifier (ie, TABLE, VIEW, BASE TABLE, etc).
Relkind(RelType) string
// SchemaName loads the active schema name from the database if not provided on the cli.
SchemaName(*ArgType) (string, error)
// ParseQuery parses the ArgType.Query and writes any necessary type(s) to
// the ArgType from the opened database handle.
ParseQuery(*ArgType) error
// LoadSchema loads the ArgType.Schema from the opened database handle,
// writing any generated types to ArgType.
LoadSchema(*ArgType) error
}
// SchemaLoaders are the available schema loaders.
var SchemaLoaders = map[string]Loader{}
// TypeLoader provides a common Loader implementation used by the built in
// schema/query loaders.
type TypeLoader struct {
ParamN func(int) string
MaskFunc func() string
Esc map[EscType]func(string) string
ProcessRelkind func(RelType) string
Schema func(*ArgType) (string, error)
ParseType func(*ArgType, string, bool) (int, string, string)
EnumList func(models.XODB, string) ([]*models.Enum, error)
EnumValueList func(models.XODB, string, string) ([]*models.EnumValue, error)
ProcList func(models.XODB, string) ([]*models.Proc, error)
ProcParamList func(models.XODB, string, string) ([]*models.ProcParam, error)
TableList func(models.XODB, string, string) ([]*models.Table, error)
ColumnList func(models.XODB, string, string) ([]*models.Column, error)
ForeignKeyList func(models.XODB, string, string) ([]*models.ForeignKey, error)
IndexList func(models.XODB, string, string) ([]*models.Index, error)
IndexColumnList func(models.XODB, string, string, string) ([]*models.IndexColumn, error)
QueryStrip func([]string, []string)
QueryColumnList func(*ArgType, []string) ([]*models.Column, error)
}
// NthParam satisifies Loader's NthParam.
func (tl TypeLoader) NthParam(i int) string {
if tl.ParamN != nil {
return tl.ParamN(i)
}
return fmt.Sprintf("$%d", i+1)
}
// Mask returns the parameter mask.
func (tl TypeLoader) Mask() string {
if tl.MaskFunc != nil {
return tl.MaskFunc()
}
return "$%d"
}
// Escape escapes the provided identifier based on the EscType.
func (tl TypeLoader) Escape(typ EscType, s string) string {
if e, ok := tl.Esc[typ]; ok && e != nil {
return e(s)
}
return `"` + s + `"`
}
// Relkind satisfies Loader's Relkind.
func (tl TypeLoader) Relkind(rt RelType) string {
if tl.ProcessRelkind != nil {
return tl.ProcessRelkind(rt)
}
return rt.String()
}
// SchemaName returns the active schema name.
func (tl TypeLoader) SchemaName(args *ArgType) (string, error) {
if tl.Schema != nil {
return tl.Schema(args)
}
return "", nil
}
// ParseQuery satisfies Loader's ParseQuery.
func (tl TypeLoader) ParseQuery(args *ArgType) error {
var err error
// parse supplied query
queryStr, params := args.ParseQuery(tl.Mask(), true)
inspectStr, _ := args.ParseQuery("NULL", false)
// split up query and inspect based on lines
query := strings.Split(queryStr, "\n")
inspect := strings.Split(inspectStr, "\n")
// query comment placeholder
queryComments := make([]string, len(query)+1)
// trim whitespace if applicable
if args.QueryTrim {
for n, l := range query {
query[n] = strings.TrimSpace(l)
if n < len(query)-1 {
query[n] = query[n] + " "
}
}
for n, l := range inspect {
inspect[n] = strings.TrimSpace(l)
if n < len(inspect)-1 {
inspect[n] = inspect[n] + " "
}
}
}
// query strip
if args.QueryStrip && tl.QueryStrip != nil {
tl.QueryStrip(query, queryComments)
}
// create template for query type
typeTpl := &Type{
Name: args.QueryType,
RelType: Table,
Fields: []*Field{},
Table: &models.Table{
TableName: "[custom " + strings.ToLower(snaker.CamelToSnake(args.QueryType)) + "]",
},
Comment: args.QueryTypeComment,
}
if args.QueryFields == "" {
// if no query fields specified, then pass to inspector
colList, err := tl.QueryColumnList(args, inspect)
if err != nil {
return err
}
// process columns
for _, c := range colList {
f := &Field{
Name: snaker.SnakeToCamelIdentifier(c.ColumnName),
Col: c,
}
f.Len, f.NilType, f.Type = tl.ParseType(args, c.DataType, args.QueryAllowNulls && !c.NotNull)
typeTpl.Fields = append(typeTpl.Fields, f)
}
} else {
// extract fields from query fields
for _, qf := range strings.Split(args.QueryFields, ",") {
qf = strings.TrimSpace(qf)
colName := qf
colType := "string"
i := strings.Index(qf, " ")
if i != -1 {
colName = qf[:i]
colType = qf[i+1:]
}
typeTpl.Fields = append(typeTpl.Fields, &Field{
Name: colName,
Type: colType,
Col: &models.Column{
ColumnName: snaker.CamelToSnake(colName),
},
})
}
}
// generate query type template
err = args.ExecuteTemplate(QueryTypeTemplate, args.QueryType, "", typeTpl)
if err != nil {
return err
}
// build func name
funcName := args.QueryFunc
if funcName == "" {
// no func name specified, so generate based on type
if args.QueryOnlyOne {
funcName = args.QueryType
} else {
funcName = inflector.Pluralize(args.QueryType)
}
// affix any params
if len(params) == 0 {
funcName = "Get" + funcName
} else {
funcName = funcName + "By"
for _, p := range params {
funcName = funcName + strings.ToUpper(p.Name[:1]) + p.Name[1:]
}
}
}
// create func template
queryTpl := &Query{
Name: funcName,
Query: query,
QueryComments: queryComments,
QueryParams: params,
OnlyOne: args.QueryOnlyOne,
Interpolate: args.QueryInterpolate,
Type: typeTpl,
Comment: args.QueryFuncComment,
}
// generate template
err = args.ExecuteTemplate(QueryTemplate, args.QueryType, "", queryTpl)
if err != nil {
return err
}
return nil
}
// LoadSchema loads schema definitions.
func (tl TypeLoader) LoadSchema(args *ArgType) error {
var err error
// load enums
_, err = tl.LoadEnums(args)
if err != nil {
return err
}
// load procs
_, err = tl.LoadProcs(args)
if err != nil {
return err
}
// load tables
tableMap, err := tl.LoadRelkind(args, Table)
if err != nil {
return err
}
// load views
viewMap, err := tl.LoadRelkind(args, View)
if err != nil {
return err
}
// merge views with the tableMap
for k, v := range viewMap {
tableMap[k] = v
}
// load foreign keys
_, err = tl.LoadForeignKeys(args, tableMap)
if err != nil {
return err
}
// load indexes
_, err = tl.LoadIndexes(args, tableMap)
if err != nil {
return err
}
return nil
}
// LoadEnums loads schema enums.
func (tl TypeLoader) LoadEnums(args *ArgType) (map[string]*Enum, error) {
var err error
// not supplied, so bail
if tl.EnumList == nil {
return nil, nil
}
// load enums
enumList, err := tl.EnumList(args.DB, args.Schema)
if err != nil {
return nil, err
}
// process enums
enumMap := map[string]*Enum{}
for _, e := range enumList {
enumTpl := &Enum{
Name: SingularizeIdentifier(e.EnumName),
Schema: args.Schema,
Values: []*EnumValue{},
Enum: e,
ReverseConstNames: args.UseReversedEnumConstNames,
}
err = tl.LoadEnumValues(args, enumTpl)
if err != nil {
return nil, err
}
enumMap[enumTpl.Name] = enumTpl
args.KnownTypeMap[enumTpl.Name] = true
}
// generate enum templates
for _, e := range enumMap {
err = args.ExecuteTemplate(EnumTemplate, e.Name, "", e)
if err != nil {
return nil, err
}
}
return enumMap, nil
}
// LoadEnumValues loads schema enum values.
func (tl TypeLoader) LoadEnumValues(args *ArgType, enumTpl *Enum) error {
var err error
// load enum values
enumValues, err := tl.EnumValueList(args.DB, args.Schema, enumTpl.Enum.EnumName)
if err != nil {
return err
}
// process enum values
for _, ev := range enumValues {
// chop off redundant enum name if applicable
name := snaker.SnakeToCamelIdentifier(ev.EnumValue)
if strings.HasSuffix(strings.ToLower(name), strings.ToLower(enumTpl.Name)) {
n := name[:len(name)-len(enumTpl.Name)]
if len(n) > 0 {
name = n
}
}
enumTpl.Values = append(enumTpl.Values, &EnumValue{
Name: name,
Val: ev,
})
}
return nil
}
// LoadProcs loads schema stored procedures definitions.
func (tl TypeLoader) LoadProcs(args *ArgType) (map[string]*Proc, error) {
var err error
// not supplied, so bail
if tl.ProcList == nil {
return nil, nil
}
// load procs
procList, err := tl.ProcList(args.DB, args.Schema)
if err != nil {
return nil, err
}
// process procs
procMap := map[string]*Proc{}
for _, p := range procList {
// fix the name if it starts with one or more underscores
name := p.ProcName
for strings.HasPrefix(name, "_") {
name = name[1:]
}
// create template
procTpl := &Proc{
Name: snaker.SnakeToCamelIdentifier(name),
Schema: args.Schema,
Params: []*Field{},
Return: &Field{},
Proc: p,
}
// parse return type into template
// TODO: fix this so that nullable types can be returned
_, procTpl.Return.NilType, procTpl.Return.Type = tl.ParseType(args, p.ReturnType, false)
// load proc parameters
err = tl.LoadProcParams(args, procTpl)
if err != nil {
return nil, err
}
procMap[p.ProcName] = procTpl
}
// generate proc templates
for _, p := range procMap {
err = args.ExecuteTemplate(ProcTemplate, "sp_"+p.Name, "", p)
if err != nil {
return nil, err
}
}
return procMap, nil
}
// LoadProcParams loads schema stored procedure parameters.
func (tl TypeLoader) LoadProcParams(args *ArgType, procTpl *Proc) error {
var err error
// load proc params
paramList, err := tl.ProcParamList(args.DB, args.Schema, procTpl.Proc.ProcName)
if err != nil {
return err
}
// process params
for i, p := range paramList {
// TODO: some databases support named parameters in procs (MySQL)
paramTpl := &Field{
Name: fmt.Sprintf("v%d", i),
}
// TODO: fix this so that nullable types can be used as parameters
_, _, paramTpl.Type = tl.ParseType(args, strings.TrimSpace(p.ParamType), false)
// add to proc params
if procTpl.ProcParams != "" {
procTpl.ProcParams = procTpl.ProcParams + ", "
}
procTpl.ProcParams = procTpl.ProcParams + p.ParamType
procTpl.Params = append(procTpl.Params, paramTpl)
}
return nil
}
// LoadRelkind loads a schema table/view definition.
func (tl TypeLoader) LoadRelkind(args *ArgType, relType RelType) (map[string]*Type, error) {
var err error
// load tables
tableList, err := tl.TableList(args.DB, args.Schema, tl.Relkind(relType))
if err != nil {
return nil, err
}
// tables
tableMap := make(map[string]*Type)
for _, ti := range tableList {
// create template
typeTpl := &Type{
Name: SingularizeIdentifier(ti.TableName),
Schema: args.Schema,
RelType: relType,
Fields: []*Field{},
Table: ti,
}
// process columns
err = tl.LoadColumns(args, typeTpl)
if err != nil {
return nil, err
}
tableMap[ti.TableName] = typeTpl
}
// generate table templates
for _, t := range tableMap {
err = args.ExecuteTemplate(TypeTemplate, t.Name, "", t)
if err != nil {
return nil, err
}
}
return tableMap, nil
}
// LoadColumns loads schema table/view columns.
func (tl TypeLoader) LoadColumns(args *ArgType, typeTpl *Type) error {
var err error
// load columns
columnList, err := tl.ColumnList(args.DB, args.Schema, typeTpl.Table.TableName)
if err != nil {
return err
}
// process columns
for _, c := range columnList {
ignore := false
for _, ignoreField := range args.IgnoreFields {
if ignoreField == c.ColumnName {
// Skip adding this field if user has specified they are not
// interested.
//
// This could be useful for fields which are managed by the
// database (e.g. automatically updated timestamps) instead of
// via Go code.
ignore = true
}
}
if ignore {
continue
}
// set col info
f := &Field{
Name: snaker.SnakeToCamelIdentifier(c.ColumnName),
Col: c,
}
f.Len, f.NilType, f.Type = tl.ParseType(args, c.DataType, !c.NotNull)
// set primary key
if c.IsPrimaryKey {
typeTpl.PrimaryKeyFields = append(typeTpl.PrimaryKeyFields, f)
// This is retained for backward compatibility in the templates.
typeTpl.PrimaryKey = f
}
// append col to template fields
typeTpl.Fields = append(typeTpl.Fields, f)
}
return nil
}
// LoadForeignKeys loads foreign keys.
func (tl TypeLoader) LoadForeignKeys(args *ArgType, tableMap map[string]*Type) (map[string]*ForeignKey, error) {
var err error
fkMap := map[string]*ForeignKey{}
for _, t := range tableMap {
// load keys per table
err = tl.LoadTableForeignKeys(args, tableMap, t, fkMap)
if err != nil {
return nil, err
}
}
// determine foreign key names
for _, fk := range fkMap {
fk.Name = args.ForeignKeyName(fkMap, fk)
}
// generate templates
for _, fk := range fkMap {
err = args.ExecuteTemplate(ForeignKeyTemplate, fk.Type.Name, fk.ForeignKey.ForeignKeyName, fk)
if err != nil {
return nil, err
}
}
return fkMap, nil
}
// LoadTableForeignKeys loads schema foreign key definitions per table.
func (tl TypeLoader) LoadTableForeignKeys(args *ArgType, tableMap map[string]*Type, typeTpl *Type, fkMap map[string]*ForeignKey) error {
var err error
// load foreign keys
foreignKeyList, err := tl.ForeignKeyList(args.DB, args.Schema, typeTpl.Table.TableName)
if err != nil {
return err
}
// loop over foreign keys for table
for _, fk := range foreignKeyList {
var refTpl *Type
var col, refCol *Field
colLoop:
// find column
for _, f := range typeTpl.Fields {
if f.Col.ColumnName == fk.ColumnName {
col = f
break colLoop
}
}
refTplLoop:
// find ref table
for _, t := range tableMap {
if t.Table.TableName == fk.RefTableName {
refTpl = t
break refTplLoop
}
}
refColLoop:
// find ref column
for _, f := range refTpl.Fields {
if f.Col.ColumnName == fk.RefColumnName {
refCol = f
break refColLoop
}
}
// no ref col, but have ref tpl, so use primary key
if refTpl != nil && refCol == nil {
refCol = refTpl.PrimaryKey
}
// check everything was found
if col == nil || refTpl == nil || refCol == nil {
return errors.New("could not find col, refTpl, or refCol")
}
// foreign key name
if fk.ForeignKeyName == "" {
fk.ForeignKeyName = typeTpl.Table.TableName + "_" + col.Col.ColumnName + "_fkey"
}
// create foreign key template
fkMap[fk.ForeignKeyName] = &ForeignKey{
Schema: args.Schema,
Type: typeTpl,
Field: col,
RefType: refTpl,
RefField: refCol,
ForeignKey: fk,
}
}
return nil
}
// LoadIndexes loads schema index definitions.
func (tl TypeLoader) LoadIndexes(args *ArgType, tableMap map[string]*Type) (map[string]*Index, error) {
var err error
ixMap := map[string]*Index{}
for _, t := range tableMap {
// load table indexes
err = tl.LoadTableIndexes(args, t, ixMap)
if err != nil {
return nil, err
}
}
// generate templates
for _, ix := range ixMap {
err = args.ExecuteTemplate(IndexTemplate, ix.Type.Name, ix.Index.IndexName, ix)
if err != nil {
return nil, err
}
}
return ixMap, nil
}
// LoadTableIndexes loads schema index definitions per table.
func (tl TypeLoader) LoadTableIndexes(args *ArgType, typeTpl *Type, ixMap map[string]*Index) error {
var err error
var priIxLoaded bool
// load indexes
indexList, err := tl.IndexList(args.DB, args.Schema, typeTpl.Table.TableName)
if err != nil {
return err
}
// process indexes
for _, ix := range indexList {
// save whether or not the primary key index was processed
priIxLoaded = priIxLoaded || ix.IsPrimary || (ix.Origin == "pk")
// create index template
ixTpl := &Index{
Schema: args.Schema,
Type: typeTpl,
Fields: []*Field{},
Index: ix,
}
// load index columns
err = tl.LoadIndexColumns(args, ixTpl)
if err != nil {
return err
}
// build func name
args.BuildIndexFuncName(ixTpl)
ixMap[typeTpl.Table.TableName+"_"+ix.IndexName] = ixTpl
}
// search for primary key if it was skipped being set in the type
pk := typeTpl.PrimaryKey
if pk == nil {
for _, f := range typeTpl.Fields {
if f.Col.IsPrimaryKey {
pk = f
break
}
}
}
// if no primary key index loaded, but a primary key column was defined in
// the type, then create the definition here. this is needed for sqlite, as
// sqlite doesn't define primary keys in its index list
if args.LoaderType != "ora" && !priIxLoaded && pk != nil {
ixName := typeTpl.Table.TableName + "_" + pk.Col.ColumnName + "_pkey"
ixMap[ixName] = &Index{
FuncName: typeTpl.Name + "By" + pk.Name,
Schema: args.Schema,
Type: typeTpl,
Fields: []*Field{pk},
Index: &models.Index{
IndexName: ixName,
IsUnique: true,
IsPrimary: true,
},
}
}
return nil
}
// LoadIndexColumns loads the index column information.
func (tl TypeLoader) LoadIndexColumns(args *ArgType, ixTpl *Index) error {
var err error
// load index columns
indexCols, err := tl.IndexColumnList(args.DB, args.Schema, ixTpl.Type.Table.TableName, ixTpl.Index.IndexName)
if err != nil {
return err
}
// process index columns
for _, ic := range indexCols {
var field *Field
fieldLoop:
// find field
for _, f := range ixTpl.Type.Fields {
if f.Col.ColumnName == ic.ColumnName {
field = f
break fieldLoop
}
}
if field == nil {
continue
}
ixTpl.Fields = append(ixTpl.Fields, field)
}
return nil
}