/
tables.go
415 lines (350 loc) · 7.52 KB
/
tables.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
package sdata
import (
"database/sql"
"fmt"
"regexp"
"strings"
"github.com/mitchellh/hashstructure/v2"
"golang.org/x/sync/errgroup"
)
type DBInfo struct {
Type string
Version int
Schema string
Name string
Tables []DBTable `hash:"set"`
Functions []DBFunction `hash:"set"`
VTables []VirtualTable `hash:"set"`
colMap map[string]int `hash:"-"`
tableMap map[string]int `hash:"-"`
hash uint64 `hash:"-"`
}
type DBTable struct {
Schema string
Name string
Type string
Columns []DBColumn `hash:"set"`
PrimaryCol DBColumn
SecondaryCol DBColumn
FullText []DBColumn `hash:"set"`
Blocked bool
Func DBFunction
colMap map[string]int `hash:"-"`
}
type VirtualTable struct {
Name string
IDColumn string
TypeColumn string
FKeyColumn string
}
type st struct {
schema, table string
}
func GetDBInfo(
db *sql.DB,
dbType string,
blockList []string) (*DBInfo, error) {
var dbVersion int
var dbSchema, dbName string
var cols []DBColumn
var funcs []DBFunction
var err error
g := errgroup.Group{}
g.Go(func() error {
var row *sql.Row
switch dbType {
case "mysql":
row = db.QueryRow(mysqlInfo)
default:
row = db.QueryRow(postgresInfo)
}
if err := row.Scan(&dbVersion, &dbSchema, &dbName); err != nil {
return err
}
return nil
})
g.Go(func() error {
var err error
if cols, err = DiscoverColumns(db, dbType, blockList); err != nil {
return err
}
if funcs, err = DiscoverFunctions(db, dbType, blockList); err != nil {
return err
}
return nil
})
if err := g.Wait(); err != nil {
return nil, err
}
di := NewDBInfo(
dbType,
dbVersion,
dbSchema,
dbName,
cols,
funcs,
blockList)
di.hash, err = hashstructure.Hash(di, hashstructure.FormatV2, nil)
if err != nil {
return nil, err
}
return di, nil
}
func NewDBInfo(
dbType string,
dbVersion int,
dbSchema string,
dbName string,
cols []DBColumn,
funcs []DBFunction,
blockList []string) *DBInfo {
di := &DBInfo{
Type: dbType,
Version: dbVersion,
Schema: dbSchema,
Name: dbName,
Functions: funcs,
colMap: make(map[string]int),
tableMap: make(map[string]int),
}
tm := make(map[st][]DBColumn)
for i := range cols {
c := cols[i]
di.colMap[(c.Schema + ":" + c.Table + ":" + c.Name)] = i
k1 := st{c.Schema, c.Table}
tm[k1] = append(tm[k1], c)
}
for k, tcols := range tm {
ti := NewDBTable(k.schema, k.table, "", tcols)
if strings.HasPrefix(ti.Name, "_gj_") {
continue
}
ti.Blocked = isInList(ti.Name, blockList)
di.AddTable(ti)
}
for _, f := range funcs {
if f.Type != "record" || len(f.Outputs) == 0 {
continue
}
var cols []DBColumn
for _, v := range f.Outputs {
cols = append(cols, DBColumn{
ID: int32(v.ID),
Name: v.Name,
Type: v.Type,
})
}
t := NewDBTable(f.Schema, f.Name, "function", cols)
t.Func = f
di.AddTable(t)
}
return di
}
func NewDBTable(schema, name, _type string, cols []DBColumn) DBTable {
ti := DBTable{
Schema: schema,
Name: name,
Type: _type,
Columns: cols,
colMap: make(map[string]int, len(cols)),
}
for i, c := range cols {
cols[i].Schema = schema
cols[i].Table = name
switch {
case c.FullText:
ti.FullText = append(ti.FullText, c)
case c.PrimaryKey:
ti.PrimaryCol = c
}
ti.colMap[c.Name] = i
}
return ti
}
func (di *DBInfo) AddTable(t DBTable) {
for i, c := range t.Columns {
di.colMap[(c.Schema + ":" + c.Table + ":" + c.Name)] = i
}
i := len(di.Tables)
di.Tables = append(di.Tables, t)
di.tableMap[(t.Schema + ":" + t.Name)] = i
}
func (di *DBInfo) GetColumn(schema, table, column string) (*DBColumn, error) {
t, err := di.GetTable(schema, table)
if err != nil {
return nil, err
}
cid, ok := t.colMap[column]
if !ok {
return nil, fmt.Errorf("column: '%s.%s.%s' not found", schema, table, column)
}
return &t.Columns[cid], nil
}
func (di *DBInfo) GetTable(schema, table string) (*DBTable, error) {
tid, ok := di.tableMap[(schema + ":" + table)]
if !ok {
return nil, fmt.Errorf("table: '%s.%s' not found", schema, table)
}
return &di.Tables[tid], nil
}
type DBColumn struct {
ID int32
Name string
Type string
Array bool
NotNull bool
PrimaryKey bool
UniqueKey bool
FullText bool
FKeySchema string
FKeyTable string
FKeyCol string
Blocked bool
Table string
Schema string
}
func DiscoverColumns(db *sql.DB, dbtype string, blockList []string) ([]DBColumn, error) {
var sqlStmt string
switch dbtype {
case "mysql":
sqlStmt = mysqlColumnsStmt
default:
sqlStmt = postgresColumnsStmt
}
rows, err := db.Query(sqlStmt)
if err != nil {
return nil, fmt.Errorf("error fetching columns: %s", err)
}
defer rows.Close()
cmap := make(map[string]DBColumn)
for rows.Next() {
var c DBColumn
err = rows.Scan(&c.Schema, &c.Table, &c.Name, &c.Type, &c.NotNull, &c.PrimaryKey, &c.UniqueKey, &c.Array, &c.FullText, &c.FKeySchema, &c.FKeyTable, &c.FKeyCol)
if err != nil {
return nil, err
}
k := (c.Schema + ":" + c.Table + ":" + c.Name)
v, ok := cmap[k]
if !ok {
v = c
v.ID = int32(len(cmap))
if strings.HasPrefix(v.Table, "_gj_") {
continue
}
v.Blocked = isInList(v.Name, blockList)
}
if c.Type != "" {
v.Type = c.Type
}
if c.PrimaryKey {
v.PrimaryKey = true
v.UniqueKey = true
}
if c.NotNull {
v.NotNull = true
}
if c.UniqueKey {
v.UniqueKey = true
}
if c.Array {
v.Array = true
}
if c.FullText {
v.FullText = true
}
if c.FKeySchema != "" {
v.FKeySchema = c.FKeySchema
}
if c.FKeyTable != "" {
v.FKeyTable = c.FKeyTable
}
if c.FKeyCol != "" {
v.FKeyCol = c.FKeyCol
}
cmap[k] = v
}
var cols []DBColumn
for _, c := range cmap {
cols = append(cols, c)
}
return cols, nil
}
type DBFunction struct {
Schema string
Name string
Type string
Agg bool
Inputs []DBFuncParam
Outputs []DBFuncParam
}
type DBFuncParam struct {
ID int
Name string
Type string
IsArray bool
}
func DiscoverFunctions(db *sql.DB, dbtype string, blockList []string) ([]DBFunction, error) {
var sqlStmt string
switch dbtype {
case "mysql":
sqlStmt = mysqlFunctionsStmt
default:
sqlStmt = postgresFunctionsStmt
}
rows, err := db.Query(sqlStmt)
if err != nil {
return nil, fmt.Errorf("error fetching functions: %s", err)
}
defer rows.Close()
var funcs []DBFunction
fm := make(map[string]int)
for rows.Next() {
var fid, fs, fn, ft string
var pn, pt, pk string
var pid int
err = rows.Scan(&fid, &fs, &fn, &ft, &pid, &pn, &pt, &pk)
if err != nil {
return nil, err
}
if isInList(fn, blockList) {
continue
}
i, ok := fm[fid]
if !ok {
funcs = append(funcs, DBFunction{Schema: fs, Name: fn, Type: ft})
i = len(funcs) - 1
fm[fid] = i
}
param := DBFuncParam{ID: pid, Name: pn, Type: pt}
if strings.HasSuffix(pt, "[]") {
param.IsArray = true
}
switch pk {
case "IN", "in":
funcs[i].Inputs = append(funcs[i].Inputs, param)
case "OUT", "out":
funcs[i].Outputs = append(funcs[i].Outputs, param)
}
}
return funcs, nil
}
func (fn *DBFunction) GetInput(name string) (ret DBFuncParam, err error) {
for _, in := range fn.Inputs {
if in.Name == name {
return in, nil
}
}
return ret, fmt.Errorf("function input '%s' not found", name)
}
func (di *DBInfo) Hash() uint64 {
return di.hash
}
func isInList(val string, s []string) bool {
for _, v := range s {
regex := fmt.Sprintf("^%s$", v)
if matched, _ := regexp.MatchString(regex, val); matched {
return true
}
}
return false
}