forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boilingcore.go
590 lines (492 loc) · 14.8 KB
/
boilingcore.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
// Package boilingcore has types and methods useful for generating code that
// acts as a fully dynamic ORM might.
package boilingcore
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/volatiletech/sqlboiler/drivers"
"github.com/volatiletech/sqlboiler/importers"
"github.com/volatiletech/sqlboiler/strmangle"
"github.com/volatiletech/sqlboiler/templatebin"
)
const (
templatesDirectory = "templates"
templatesSingletonDirectory = "templates/singleton"
templatesTestDirectory = "templates_test"
templatesSingletonTestDirectory = "templates_test/singleton"
templatesTestMainDirectory = "templates_test/main_test"
)
var (
// Tags must be in a format like: json, xml, etc.
rgxValidTag = regexp.MustCompile(`[a-zA-Z_\.]+`)
)
// State holds the global data needed by most pieces to run
type State struct {
Config *Config
Driver drivers.Interface
Schema string
Tables []drivers.Table
Dialect drivers.Dialect
Templates *templateList
TestTemplates *templateList
}
// New creates a new state based off of the config
func New(config *Config) (*State, error) {
s := &State{
Config: config,
}
var templates []lazyTemplate
defer func() {
if s.Config.Debug {
debugOut := struct {
Config *Config `json:"config"`
DriverConfig drivers.Config `json:"driver_config"`
Schema string `json:"schema"`
Dialect drivers.Dialect `json:"dialect"`
Tables []drivers.Table `json:"tables"`
Templates []lazyTemplate `json:"templates"`
}{
Config: s.Config,
DriverConfig: s.Config.DriverConfig,
Schema: s.Schema,
Dialect: s.Dialect,
Tables: s.Tables,
Templates: templates,
}
b, err := json.Marshal(debugOut)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", b)
}
}()
s.Driver = drivers.GetDriver(config.DriverName)
err := s.initDBInfo(config.DriverConfig)
if err != nil {
return nil, errors.Wrap(err, "unable to initialize tables")
}
if err := s.mergeDriverImports(); err != nil {
return nil, errors.Wrap(err, "unable to merge imports from driver")
}
if !s.Config.NoContext {
s.Config.Imports.All.Standard = append(s.Config.Imports.All.Standard, `"context"`)
s.Config.Imports.Test.Standard = append(s.Config.Imports.Test.Standard, `"context"`)
}
if err := s.processTypeReplacements(); err != nil {
return nil, err
}
templates, err = s.initTemplates()
if err != nil {
return nil, errors.Wrap(err, "unable to initialize templates")
}
err = s.initOutFolders(templates)
if err != nil {
return nil, errors.Wrap(err, "unable to initialize the output folders")
}
err = s.initTags(config.Tags)
if err != nil {
return nil, errors.Wrap(err, "unable to initialize struct tags")
}
err = s.initAliases(&config.Aliases)
if err != nil {
return nil, errors.Wrap(err, "unable to initialize aliases")
}
return s, nil
}
// Run executes the sqlboiler templates and outputs them to files based on the
// state given.
func (s *State) Run() error {
data := &templateData{
Tables: s.Tables,
Aliases: s.Config.Aliases,
DriverName: s.Config.DriverName,
PkgName: s.Config.PkgName,
AddGlobal: s.Config.AddGlobal,
AddPanic: s.Config.AddPanic,
NoContext: s.Config.NoContext,
NoHooks: s.Config.NoHooks,
NoAutoTimestamps: s.Config.NoAutoTimestamps,
NoRowsAffected: s.Config.NoRowsAffected,
StructTagCasing: s.Config.StructTagCasing,
Tags: s.Config.Tags,
Dialect: s.Dialect,
Schema: s.Schema,
LQ: strmangle.QuoteCharacter(s.Dialect.LQ),
RQ: strmangle.QuoteCharacter(s.Dialect.RQ),
StringFuncs: templateStringMappers,
}
if err := generateSingletonOutput(s, data); err != nil {
return errors.Wrap(err, "singleton template output")
}
if !s.Config.NoTests {
if err := generateSingletonTestOutput(s, data); err != nil {
return errors.Wrap(err, "unable to generate singleton test template output")
}
}
var regularDirExtMap, testDirExtMap dirExtMap
regularDirExtMap = groupTemplates(s.Templates)
if !s.Config.NoTests {
testDirExtMap = groupTemplates(s.TestTemplates)
}
for _, table := range s.Tables {
if table.IsJoinTable {
continue
}
data.Table = table
// Generate the regular templates
if err := generateOutput(s, regularDirExtMap, data); err != nil {
return errors.Wrap(err, "unable to generate output")
}
// Generate the test templates
if !s.Config.NoTests {
if err := generateTestOutput(s, testDirExtMap, data); err != nil {
return errors.Wrap(err, "unable to generate test output")
}
}
}
return nil
}
// Cleanup closes any resources that must be closed
func (s *State) Cleanup() error {
// Nothing here atm, used to close the driver
return nil
}
// initTemplates loads all template folders into the state object.
//
// If TemplateDirs is set it uses those, else it pulls from assets.
// Then it allows drivers to override, followed by replacements.
//
// Because there's the chance for windows paths to jumped in
// all paths are converted to the native OS's slash style.
//
// Later, in order to properly look up imports the paths will
// be forced back to linux style paths.
func (s *State) initTemplates() ([]lazyTemplate, error) {
var err error
templates := make(map[string]templateLoader)
if len(s.Config.TemplateDirs) != 0 {
for _, dir := range s.Config.TemplateDirs {
abs, err := filepath.Abs(dir)
if err != nil {
return nil, errors.Wrap(err, "could not find abs dir of templates directory")
}
base := filepath.Base(abs)
root := filepath.Dir(abs)
tpls, err := findTemplates(root, base)
if err != nil {
return nil, err
}
mergeTemplates(templates, tpls)
}
} else {
for _, a := range templatebin.AssetNames() {
if strings.HasSuffix(a, ".tpl") {
templates[normalizeSlashes(a)] = assetLoader(a)
}
}
}
driverTemplates, err := s.Driver.Templates()
if err != nil {
return nil, err
}
for template, contents := range driverTemplates {
templates[normalizeSlashes(template)] = base64Loader(contents)
}
for _, replace := range s.Config.Replacements {
splits := strings.Split(replace, ";")
if len(splits) != 2 {
return nil, errors.Errorf("replace parameters must have 2 arguments, given: %s", replace)
}
original, replacement := normalizeSlashes(splits[0]), splits[1]
_, ok := templates[original]
if !ok {
return nil, errors.Errorf("replace can only replace existing templates, %s does not exist", original)
}
templates[original] = fileLoader(replacement)
}
// For stability, sort keys to traverse the map and turn it into a slice
keys := make([]string, 0, len(templates))
for k := range templates {
keys = append(keys, k)
}
sort.Strings(keys)
lazyTemplates := make([]lazyTemplate, 0, len(templates))
for _, k := range keys {
lazyTemplates = append(lazyTemplates, lazyTemplate{
Name: k,
Loader: templates[k],
})
}
s.Templates, err = loadTemplates(lazyTemplates, false)
if err != nil {
return nil, err
}
if !s.Config.NoTests {
s.TestTemplates, err = loadTemplates(lazyTemplates, true)
if err != nil {
return nil, err
}
}
return lazyTemplates, nil
}
type dirExtMap map[string]map[string][]string
// groupTemplates takes templates and groups them according to their output directory
// and file extension.
func groupTemplates(templates *templateList) dirExtMap {
tplNames := templates.Templates()
dirs := make(map[string]map[string][]string)
for _, tplName := range tplNames {
normalized, isSingleton, _, _ := outputFilenameParts(tplName)
if isSingleton {
continue
}
dir := filepath.Dir(normalized)
if dir == "." {
dir = ""
}
extensions, ok := dirs[dir]
if !ok {
extensions = make(map[string][]string)
dirs[dir] = extensions
}
ext := getLongExt(tplName)
ext = strings.TrimSuffix(ext, ".tpl")
slice := extensions[ext]
extensions[ext] = append(slice, tplName)
}
return dirs
}
// findTemplates uses a root path: (/home/user/gopath/src/../sqlboiler/)
// and a base path: /templates
// to create a bunch of file loaders of the form:
// templates/00_struct.tpl -> /absolute/path/to/that/file
// Note the missing leading slash, this is important for the --replace argument
func findTemplates(root, base string) (map[string]templateLoader, error) {
templates := make(map[string]templateLoader)
rootBase := filepath.Join(root, base)
err := filepath.Walk(rootBase, func(path string, fi os.FileInfo, err error) error {
if fi.IsDir() {
return nil
}
ext := filepath.Ext(path)
if ext != ".tpl" {
return nil
}
relative, err := filepath.Rel(root, path)
if err != nil {
return errors.Wrapf(err, "could not find relative path to base root: %s", rootBase)
}
relative = strings.TrimLeft(relative, string(os.PathSeparator))
templates[relative] = fileLoader(path)
return nil
})
if err != nil {
return nil, err
}
return templates, nil
}
// initDBInfo retrieves information about the database
func (s *State) initDBInfo(config map[string]interface{}) error {
dbInfo, err := s.Driver.Assemble(config)
if err != nil {
return errors.Wrap(err, "unable to fetch table data")
}
if len(dbInfo.Tables) == 0 {
return errors.New("no tables found in database")
}
if err := checkPKeys(dbInfo.Tables); err != nil {
return err
}
s.Schema = dbInfo.Schema
s.Tables = dbInfo.Tables
s.Dialect = dbInfo.Dialect
return nil
}
// mergeDriverImports calls the driver and asks for its set
// of imports, then merges it into the current configuration's
// imports.
func (s *State) mergeDriverImports() error {
drivers, err := s.Driver.Imports()
if err != nil {
return errors.Wrap(err, "failed to fetch driver's imports")
}
s.Config.Imports = importers.Merge(s.Config.Imports, drivers)
return nil
}
// processTypeReplacements checks the config for type replacements
// and performs them.
func (s *State) processTypeReplacements() error {
for _, r := range s.Config.TypeReplaces {
for i := range s.Tables {
t := s.Tables[i]
for j := range t.Columns {
c := t.Columns[j]
if matchColumn(c, r.Match) {
t.Columns[j] = columnMerge(c, r.Replace)
if len(r.Imports.Standard) != 0 || len(r.Imports.ThirdParty) != 0 {
s.Config.Imports.BasedOnType[t.Columns[j].Type] = importers.Set{
Standard: r.Imports.Standard,
ThirdParty: r.Imports.ThirdParty,
}
}
}
}
}
}
return nil
}
// matchColumn checks if a column 'c' matches specifiers in 'm'.
// Anything defined in m is checked against a's values, the
// match is a done using logical and (all specifiers must match).
// Bool fields are only checked if a string type field matched first
// and if a string field matched they are always checked (must be defined).
//
// Doesn't care about Unique columns since those can vary independent of type.
func matchColumn(c, m drivers.Column) bool {
matchedSomething := false
// return true if we matched, or we don't have to match
// if we actually matched against something, then additionally set
// matchedSomething so we can check boolean values too.
matches := func(matcher, value string) bool {
if len(matcher) != 0 && matcher != value {
return false
}
matchedSomething = true
return true
}
if !matches(m.Name, c.Name) {
return false
}
if !matches(m.Type, c.Type) {
return false
}
if !matches(m.DBType, c.DBType) {
return false
}
if !matches(m.UDTName, c.UDTName) {
return false
}
if !matches(m.FullDBType, c.FullDBType) {
return false
}
if m.ArrType != nil && !matches(*m.ArrType, *c.ArrType) {
return false
}
if !matchedSomething {
return false
}
if m.AutoGenerated != c.AutoGenerated {
return false
}
if m.Nullable != c.Nullable {
return false
}
return true
}
// columnMerge merges values from src into dst. Bools are copied regardless
// strings are copied if they have values. Name is excluded because it doesn't make
// sense to non-programatically replace a name.
func columnMerge(dst, src drivers.Column) drivers.Column {
ret := dst
if len(src.Type) != 0 {
ret.Type = src.Type
}
if len(src.DBType) != 0 {
ret.DBType = src.DBType
}
if len(src.UDTName) != 0 {
ret.UDTName = src.UDTName
}
if len(src.FullDBType) != 0 {
ret.FullDBType = src.FullDBType
}
if src.ArrType != nil && len(*src.ArrType) != 0 {
ret.ArrType = new(string)
*ret.ArrType = *src.ArrType
}
return ret
}
// initOutFolders creates the folders that will hold the generated output.
func (s *State) initOutFolders(lazyTemplates []lazyTemplate) error {
if s.Config.Wipe {
if err := os.RemoveAll(s.Config.OutFolder); err != nil {
return err
}
}
newDirs := make(map[string]struct{})
for _, t := range lazyTemplates {
// templates/js/00_struct.js.tpl
// templates/js/singleton/00_struct.js.tpl
// we want the js part only
fragments := strings.Split(t.Name, string(os.PathSeparator))
// Throw away the root dir and filename
fragments = fragments[1 : len(fragments)-1]
if len(fragments) != 0 && fragments[len(fragments)-1] == "singleton" {
fragments = fragments[:len(fragments)-1]
}
if len(fragments) == 0 {
continue
}
newDirs[strings.Join(fragments, string(os.PathSeparator))] = struct{}{}
}
if err := os.MkdirAll(s.Config.OutFolder, os.ModePerm); err != nil {
return err
}
for d := range newDirs {
if err := os.MkdirAll(filepath.Join(s.Config.OutFolder, d), os.ModePerm); err != nil {
return err
}
}
return nil
}
// initTags removes duplicate tags and validates the format
// of all user tags are simple strings without quotes: [a-zA-Z_\.]+
func (s *State) initTags(tags []string) error {
s.Config.Tags = strmangle.RemoveDuplicates(s.Config.Tags)
for _, v := range s.Config.Tags {
if !rgxValidTag.MatchString(v) {
return errors.New("Invalid tag format %q supplied, only specify name, eg: xml")
}
}
return nil
}
func (s *State) initAliases(a *Aliases) error {
FillAliases(a, s.Tables)
return nil
}
// checkPKeys ensures every table has a primary key column
func checkPKeys(tables []drivers.Table) error {
var missingPkey []string
for _, t := range tables {
if t.PKey == nil {
missingPkey = append(missingPkey, t.Name)
}
}
if len(missingPkey) != 0 {
return errors.Errorf("primary key missing in tables (%s)", strings.Join(missingPkey, ", "))
}
return nil
}
func mergeTemplates(dst, src map[string]templateLoader) {
for k, v := range src {
dst[k] = v
}
}
// normalizeSlashes takes a path that was made on linux or windows and converts it
// to a native path.
func normalizeSlashes(path string) string {
path = strings.Replace(path, `/`, string(os.PathSeparator), -1)
path = strings.Replace(path, `\`, string(os.PathSeparator), -1)
return path
}
// denormalizeSlashes takes any backslashes and converts them to linux style slashes
func denormalizeSlashes(path string) string {
path = strings.Replace(path, `\`, `/`, -1)
return path
}