-
Notifications
You must be signed in to change notification settings - Fork 33
/
dgw.go
395 lines (364 loc) · 10.2 KB
/
dgw.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
// go:generate go-bindata -o bindata.go template mapconfig
package main
import (
"bytes"
"context"
"database/sql"
"fmt"
"go/format"
"io/ioutil"
"sort"
"strings"
"text/template"
"github.com/BurntSushi/toml"
"github.com/achiku/varfmt"
_ "github.com/lib/pq" // postgres
"github.com/pkg/errors"
)
// Queryer database/sql compatible query interface
type Queryer interface {
Exec(string, ...interface{}) (sql.Result, error)
Query(string, ...interface{}) (*sql.Rows, error)
QueryRow(string, ...interface{}) *sql.Row
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
// OpenDB opens database connection
func OpenDB(connStr string) (*sql.DB, error) {
conn, err := sql.Open("postgres", connStr)
if err != nil {
return nil, errors.WithStack(err)
}
return conn, nil
}
const queryInterface = `
// Queryer database/sql compatible query interface
type Queryer interface {
Exec(string, ...interface{}) (sql.Result, error)
Query(string, ...interface{}) (*sql.Rows, error)
QueryRow(string, ...interface{}) *sql.Row
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
`
const pgLoadColumnDef = `
SELECT
a.attnum AS field_ordinal,
a.attname AS column_name,
format_type(a.atttypid, a.atttypmod) AS data_type,
a.attnotnull AS not_null,
COALESCE(pg_get_expr(ad.adbin, ad.adrelid), '') AS default_value,
COALESCE(ct.contype = 'p', false) AS is_primary_key,
CASE
WHEN a.atttypid = ANY ('{int,int8,int2}'::regtype[])
AND EXISTS (
SELECT 1 FROM pg_attrdef ad
WHERE ad.adrelid = a.attrelid
AND ad.adnum = a.attnum
AND pg_get_expr(ad.adbin, ad.adrelid) = 'nextval('''
|| (pg_get_serial_sequence (a.attrelid::regclass::text
, a.attname))::regclass
|| '''::regclass)'
)
THEN CASE a.atttypid
WHEN 'int'::regtype THEN 'serial'
WHEN 'int8'::regtype THEN 'bigserial'
WHEN 'int2'::regtype THEN 'smallserial'
END
WHEN a.atttypid = ANY ('{uuid}'::regtype[]) AND COALESCE(pg_get_expr(ad.adbin, ad.adrelid), '') != ''
THEN 'autogenuuid'
ELSE format_type(a.atttypid, a.atttypmod)
END AS data_type
FROM pg_attribute a
JOIN ONLY pg_class c ON c.oid = a.attrelid
JOIN ONLY pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_constraint ct ON ct.conrelid = c.oid
AND a.attnum = ANY(ct.conkey) AND ct.contype = 'p'
LEFT JOIN pg_attrdef ad ON ad.adrelid = c.oid AND ad.adnum = a.attnum
WHERE a.attisdropped = false
AND n.nspname = $1
AND c.relname = $2
AND a.attnum > 0
ORDER BY a.attnum
`
const pgLoadTableDef = `
SELECT
c.relkind AS type,
c.relname AS table_name
FROM pg_class c
JOIN ONLY pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = $1
AND c.relkind = 'r'
ORDER BY c.relname
`
// TypeMap go/db type map struct
type TypeMap struct {
DBTypes []string `toml:"db_types"`
NotNullGoType string `toml:"notnull_go_type"`
NullableGoType string `toml:"nullable_go_type"`
}
// AutoKeyMap auto generating key config
type AutoKeyMap struct {
Types []string `toml:"db_types"`
}
// PgTypeMapConfig go/db type map struct toml config
type PgTypeMapConfig map[string]TypeMap
// PgTable postgres table
type PgTable struct {
Schema string
Name string
DataType string
AutoGenPk bool
PrimaryKeys []*PgColumn
Columns []*PgColumn
}
var autoGenKeyCfg = &AutoKeyMap{
Types: []string{"smallserial", "serial", "bigserial", "autogenuuid"},
}
func (t *PgTable) setPrimaryKeyInfo(cfg *AutoKeyMap) {
t.AutoGenPk = false
for _, c := range t.Columns {
if c.IsPrimaryKey {
t.PrimaryKeys = append(t.PrimaryKeys, c)
for _, typ := range cfg.Types {
if c.DDLType == typ {
t.AutoGenPk = true
}
}
}
}
}
// PgColumn postgres columns
type PgColumn struct {
FieldOrdinal int
Name string
DataType string
DDLType string
NotNull bool
DefaultValue sql.NullString
IsPrimaryKey bool
}
// Struct go struct
type Struct struct {
Name string
Table *PgTable
Comment string
Fields []*StructField
}
// StructTmpl go struct passed to template
type StructTmpl struct {
Struct *Struct
}
// StructField go struct field
type StructField struct {
Name string
Type string
Tag string
Column *PgColumn
}
// PgLoadTypeMapFromFile load type map from toml file
func PgLoadTypeMapFromFile(filePath string) (*PgTypeMapConfig, error) {
var conf PgTypeMapConfig
if _, err := toml.DecodeFile(filePath, &conf); err != nil {
return nil, errors.WithStack(err)
}
return &conf, nil
}
// PgLoadColumnDef load Postgres column definition
func PgLoadColumnDef(db Queryer, schema string, table string) ([]*PgColumn, error) {
colDefs, err := db.Query(pgLoadColumnDef, schema, table)
if err != nil {
return nil, errors.WithStack(err)
}
cols := []*PgColumn{}
for colDefs.Next() {
c := &PgColumn{}
err := colDefs.Scan(
&c.FieldOrdinal,
&c.Name,
&c.DataType,
&c.NotNull,
&c.DefaultValue,
&c.IsPrimaryKey,
&c.DDLType,
)
if err != nil {
return nil, errors.WithStack(err)
}
// Some data types have an extra part e.g, "character varying(16)" and
// "numeric(10, 5)". We want to drop the extra part.
if i := strings.Index(c.DataType, "("); i > 0 {
c.DataType = c.DataType[0:i]
}
cols = append(cols, c)
}
return cols, nil
}
// PgLoadTableDef load Postgres table definition
func PgLoadTableDef(db Queryer, schema string) ([]*PgTable, error) {
tbDefs, err := db.Query(pgLoadTableDef, schema)
if err != nil {
return nil, errors.WithStack(err)
}
tbs := []*PgTable{}
for tbDefs.Next() {
t := &PgTable{Schema: schema}
err := tbDefs.Scan(
&t.DataType,
&t.Name,
)
if err != nil {
return nil, errors.WithStack(err)
}
cols, err := PgLoadColumnDef(db, schema, t.Name)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to get columns of %s", t.Name))
}
t.Columns = cols
tbs = append(tbs, t)
}
return tbs, nil
}
func contains(v string, l []string) bool {
sort.Strings(l)
i := sort.SearchStrings(l, v)
if i < len(l) && l[i] == v {
return true
}
return false
}
// PgConvertType converts type
func PgConvertType(col *PgColumn, typeCfg *PgTypeMapConfig) string {
cfg := map[string]TypeMap(*typeCfg)
typ := cfg["default"].NotNullGoType
for _, v := range cfg {
if contains(col.DataType, v.DBTypes) {
if col.NotNull {
return v.NotNullGoType
}
return v.NullableGoType
}
}
return typ
}
// PgColToField converts pg column to go struct field
func PgColToField(col *PgColumn, typeCfg *PgTypeMapConfig) (*StructField, error) {
stfType := PgConvertType(col, typeCfg)
stf := &StructField{
Name: varfmt.PublicVarName(col.Name),
Type: stfType,
Column: col,
}
return stf, nil
}
// PgTableToStruct converts table def to go struct
func PgTableToStruct(t *PgTable, typeCfg *PgTypeMapConfig, keyConfig *AutoKeyMap) (*Struct, error) {
t.setPrimaryKeyInfo(keyConfig)
s := &Struct{
Name: varfmt.PublicVarName(t.Name),
Table: t,
}
var fs []*StructField
for _, c := range t.Columns {
f, err := PgColToField(c, typeCfg)
if err != nil {
return nil, errors.WithStack(err)
}
fs = append(fs, f)
}
s.Fields = fs
return s, nil
}
// PgExecuteDefaultTmpl execute struct template with *Struct
func PgExecuteDefaultTmpl(st *StructTmpl, path string) ([]byte, error) {
var src []byte
d, err := Asset(path)
if err != nil {
return src, errors.WithStack(err)
}
tpl, err := template.New("struct").Funcs(tmplFuncMap).Parse(string(d))
if err != nil {
return src, errors.WithStack(err)
}
buf := new(bytes.Buffer)
if err := tpl.Execute(buf, st); err != nil {
return src, errors.Wrap(err, fmt.Sprintf("failed to execute template:\n%s", src))
}
src, err = format.Source(buf.Bytes())
if err != nil {
return src, errors.Wrap(err, fmt.Sprintf("failed to format code:\n%s", src))
}
return src, nil
}
// PgExecuteCustomTmpl execute custom template
func PgExecuteCustomTmpl(st *StructTmpl, customTmpl string) ([]byte, error) {
var src []byte
tpl, err := template.New("struct").Funcs(tmplFuncMap).Parse(customTmpl)
if err != nil {
return src, errors.WithStack(err)
}
buf := new(bytes.Buffer)
if err := tpl.Execute(buf, st); err != nil {
return src, errors.Wrap(err, fmt.Sprintf("failed to execute custom template:\n%s", src))
}
src, err = format.Source(buf.Bytes())
if err != nil {
return src, errors.Wrap(err, fmt.Sprintf("failed to format code:\n%s", src))
}
return src, nil
}
// PgCreateStruct creates struct from given schema
func PgCreateStruct(
db Queryer, schema, typeMapPath, pkgName, customTmpl string, exTbls []string) ([]byte, error) {
var src []byte
pkgDef := []byte(fmt.Sprintf("package %s\n\n", pkgName))
src = append(src, pkgDef...)
tbls, err := PgLoadTableDef(db, schema)
if err != nil {
return src, errors.WithStack(err)
}
cfg := &PgTypeMapConfig{}
if typeMapPath == "" {
if _, err := toml.Decode(typeMap, cfg); err != nil {
return src, errors.WithStack(err)
}
} else {
if _, err := toml.DecodeFile(typeMapPath, cfg); err != nil {
return src, errors.Wrap(err, fmt.Sprintf("failed to decode type map file %s", typeMapPath))
}
}
for _, tbl := range tbls {
if contains(tbl.Name, exTbls) {
continue
}
st, err := PgTableToStruct(tbl, cfg, autoGenKeyCfg)
if err != nil {
return src, errors.WithStack(err)
}
if customTmpl != "" {
tmpl, err := ioutil.ReadFile(customTmpl)
if err != nil {
return nil, err
}
s, err := PgExecuteCustomTmpl(&StructTmpl{Struct: st}, string(tmpl))
if err != nil {
return nil, errors.WithStack(err)
}
src = append(src, s...)
} else {
s, err := PgExecuteDefaultTmpl(&StructTmpl{Struct: st}, "template/struct.tmpl")
if err != nil {
return src, errors.WithStack(err)
}
m, err := PgExecuteDefaultTmpl(&StructTmpl{Struct: st}, "template/method.tmpl")
if err != nil {
return src, errors.WithStack(err)
}
src = append(src, s...)
src = append(src, m...)
}
}
return src, nil
}