forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boilingcore.go
317 lines (260 loc) · 7.93 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
// Package boilingcore has types and methods useful for generating code that
// acts as a fully dynamic ORM might.
package boilingcore
import (
"encoding/json"
"fmt"
"go/build"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
"github.com/pkg/errors"
"github.com/vattle/sqlboiler/bdb"
"github.com/vattle/sqlboiler/bdb/drivers"
"github.com/vattle/sqlboiler/queries"
"github.com/vattle/sqlboiler/strmangle"
)
const (
templatesDirectory = "templates"
templatesSingletonDirectory = "templates/singleton"
templatesTestDirectory = "templates_test"
templatesSingletonTestDirectory = "templates_test/singleton"
templatesTestMainDirectory = "templates_test/main_test"
)
// State holds the global data needed by most pieces to run
type State struct {
Config *Config
Driver bdb.Interface
Tables []bdb.Table
Dialect queries.Dialect
Templates *templateList
TestTemplates *templateList
SingletonTemplates *templateList
SingletonTestTemplates *templateList
TestMainTemplate *template.Template
}
// New creates a new state based off of the config
func New(config *Config) (*State, error) {
s := &State{
Config: config,
}
err := s.initDriver(config.DriverName)
if err != nil {
return nil, err
}
// Connect to the driver database
if err = s.Driver.Open(); err != nil {
return nil, errors.Wrap(err, "unable to connect to the database")
}
err = s.initTables(config.Schema, config.WhitelistTables, config.BlacklistTables)
if err != nil {
return nil, errors.Wrap(err, "unable to initialize tables")
}
if s.Config.Debug {
b, err := json.Marshal(s.Tables)
if err != nil {
return nil, errors.Wrap(err, "unable to json marshal tables")
}
fmt.Printf("%s\n", b)
}
err = s.initOutFolder()
if err != nil {
return nil, errors.Wrap(err, "unable to initialize the output folder")
}
err = s.initTemplates()
if err != nil {
return nil, errors.Wrap(err, "unable to initialize templates")
}
err = s.initTags(config.Tags)
if err != nil {
return nil, errors.Wrap(err, "unable to initialize struct tags")
}
return s, nil
}
// Run executes the sqlboiler templates and outputs them to files based on the
// state given.
func (s *State) Run(includeTests bool) error {
singletonData := &templateData{
Tables: s.Tables,
Schema: s.Config.Schema,
DriverName: s.Config.DriverName,
UseLastInsertID: s.Driver.UseLastInsertID(),
PkgName: s.Config.PkgName,
NoHooks: s.Config.NoHooks,
NoAutoTimestamps: s.Config.NoAutoTimestamps,
Dialect: s.Dialect,
LQ: strmangle.QuoteCharacter(s.Dialect.LQ),
RQ: strmangle.QuoteCharacter(s.Dialect.RQ),
StringFuncs: templateStringMappers,
}
if err := generateSingletonOutput(s, singletonData); err != nil {
return errors.Wrap(err, "singleton template output")
}
if !s.Config.NoTests && includeTests {
if err := generateTestMainOutput(s, singletonData); err != nil {
return errors.Wrap(err, "unable to generate TestMain output")
}
if err := generateSingletonTestOutput(s, singletonData); err != nil {
return errors.Wrap(err, "unable to generate singleton test template output")
}
}
for _, table := range s.Tables {
if table.IsJoinTable {
continue
}
data := &templateData{
Tables: s.Tables,
Table: table,
Schema: s.Config.Schema,
DriverName: s.Config.DriverName,
UseLastInsertID: s.Driver.UseLastInsertID(),
PkgName: s.Config.PkgName,
NoHooks: s.Config.NoHooks,
NoAutoTimestamps: s.Config.NoAutoTimestamps,
Tags: s.Config.Tags,
Dialect: s.Dialect,
LQ: strmangle.QuoteCharacter(s.Dialect.LQ),
RQ: strmangle.QuoteCharacter(s.Dialect.RQ),
StringFuncs: templateStringMappers,
}
// Generate the regular templates
if err := generateOutput(s, data); err != nil {
return errors.Wrap(err, "unable to generate output")
}
// Generate the test templates
if !s.Config.NoTests && includeTests {
if err := generateTestOutput(s, 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 {
s.Driver.Close()
return nil
}
// initTemplates loads all template folders into the state object.
func (s *State) initTemplates() error {
var err error
basePath, err := getBasePath(s.Config.BaseDir)
if err != nil {
return err
}
s.Templates, err = loadTemplates(filepath.Join(basePath, templatesDirectory))
if err != nil {
return err
}
s.SingletonTemplates, err = loadTemplates(filepath.Join(basePath, templatesSingletonDirectory))
if err != nil {
return err
}
if !s.Config.NoTests {
s.TestTemplates, err = loadTemplates(filepath.Join(basePath, templatesTestDirectory))
if err != nil {
return err
}
s.SingletonTestTemplates, err = loadTemplates(filepath.Join(basePath, templatesSingletonTestDirectory))
if err != nil {
return err
}
s.TestMainTemplate, err = loadTemplate(filepath.Join(basePath, templatesTestMainDirectory), s.Config.DriverName+"_main.tpl")
if err != nil {
return err
}
}
return nil
}
var basePackage = "github.com/vattle/sqlboiler"
func getBasePath(baseDirConfig string) (string, error) {
if len(baseDirConfig) > 0 {
return baseDirConfig, nil
}
p, _ := build.Default.Import(basePackage, "", build.FindOnly)
if p != nil && len(p.Dir) > 0 {
return p.Dir, nil
}
return os.Getwd()
}
// initDriver attempts to set the state Interface based off the passed in
// driver flag value. If an invalid flag string is provided an error is returned.
func (s *State) initDriver(driverName string) error {
// Create a driver based off driver flag
switch driverName {
case "postgres":
s.Driver = drivers.NewPostgresDriver(
s.Config.Postgres.User,
s.Config.Postgres.Pass,
s.Config.Postgres.DBName,
s.Config.Postgres.Host,
s.Config.Postgres.Port,
s.Config.Postgres.SSLMode,
)
case "mysql":
s.Driver = drivers.NewMySQLDriver(
s.Config.MySQL.User,
s.Config.MySQL.Pass,
s.Config.MySQL.DBName,
s.Config.MySQL.Host,
s.Config.MySQL.Port,
s.Config.MySQL.SSLMode,
)
case "mock":
s.Driver = &drivers.MockDriver{}
}
if s.Driver == nil {
return errors.New("An invalid driver name was provided")
}
s.Dialect.LQ = s.Driver.LeftQuote()
s.Dialect.RQ = s.Driver.RightQuote()
s.Dialect.IndexPlaceholders = s.Driver.IndexPlaceholders()
return nil
}
// initTables retrieves all "public" schema table names from the database.
func (s *State) initTables(schema string, whitelist, blacklist []string) error {
var err error
s.Tables, err = bdb.Tables(s.Driver, schema, whitelist, blacklist)
if err != nil {
return errors.Wrap(err, "unable to fetch table data")
}
if len(s.Tables) == 0 {
return errors.New("no tables found in database")
}
if err := checkPKeys(s.Tables); err != nil {
return err
}
return nil
}
// Tags must be in a format like: json, xml, etc.
var rgxValidTag = regexp.MustCompile(`[a-zA-Z_\.]+`)
// 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 = 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
}
// initOutFolder creates the folder that will hold the generated output.
func (s *State) initOutFolder() error {
return os.MkdirAll(s.Config.OutFolder, os.ModePerm)
}
// checkPKeys ensures every table has a primary key column
func checkPKeys(tables []bdb.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
}