-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathtableparser.go
375 lines (329 loc) · 9.96 KB
/
tableparser.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
package tableparser
import (
"database/sql"
"database/sql/driver"
"fmt"
"net/url"
"regexp"
"strings"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
type NullTime struct {
Time time.Time
Valid bool // Valid is true if Time is not NULL
}
// Scan implements the Scanner interface.
func (nt *NullTime) Scan(value interface{}) error {
nt.Time, nt.Valid = value.(time.Time)
return nil
}
// Value implements the driver Valuer interface.
func (nt NullTime) Value() (driver.Value, error) {
if !nt.Valid {
return nil, nil
}
return nt.Time, nil
}
// Table holds the table definition with all fields, indexes and triggers
type Table struct {
Schema string
Name string
Fields []Field
Indexes map[string]Index
//TODO Include complete indexes information
Constraints []Constraint
Triggers []Trigger
//
conn *sql.DB
}
// Index holds the basic index information
type Index struct {
Name string
Fields []string
Unique bool
Visible bool
Expression string // MySQL 8.0.16+
}
// IndexField holds raw index information as defined in INFORMATION_SCHEMA table
type IndexField struct {
KeyName string
SeqInIndex int
ColumnName string
Collation sql.NullString
Cardinality sql.NullInt64
SubPart sql.NullInt64
Packed sql.NullString
Null string
IndexType string
Comment string
IndexComment string
NonUnique bool
Visible string // MySQL 8.0+
Expression sql.NullString // MySQL 8.0.16+
}
// Constraint holds Foreign Keys information
type Constraint struct {
ConstraintName string
ColumnName string
ReferencedTableSchema string
ReferencedTableName string
ReferencedColumnName string
}
// Field holds raw field information as defined in INFORMATION_SCHEMA
type Field struct {
TableCatalog string
TableSchema string
TableName string
ColumnName string
OrdinalPosition int
ColumnDefault sql.NullString
IsNullable bool
DataType string
CharacterMaximumLength sql.NullInt64
CharacterOctetLength sql.NullInt64
NumericPrecision sql.NullInt64
NumericScale sql.NullInt64
DatetimePrecision sql.NullInt64
CharacterSetName sql.NullString
CollationName sql.NullString
ColumnType string
ColumnKey string
Extra string
Privileges string
ColumnComment string
GenerationExpression string
SetEnumVals []string
Constraint *Constraint
SrsID sql.NullString
}
// Trigger holds raw trigger information as defined in INFORMATION_SCHEMA
type Trigger struct {
Trigger string
Event string
Table string
Statement string
Timing string
Created NullTime
SQLMode string
Definer string
CharacterSetClient string
CollationConnection string
DatabaseCollation string
}
func NewTable(db *sql.DB, schema, tableName string) (*Table, error) {
table := &Table{
Schema: url.QueryEscape(schema),
Name: url.QueryEscape(tableName),
conn: db,
}
var err error
table.Indexes, err = getIndexes(db, table.Schema, table.Name)
if err != nil {
return nil, err
}
table.Constraints, err = getConstraints(db, table.Schema, table.Name)
if err != nil {
return nil, err
}
table.Triggers, err = getTriggers(db, table.Schema, table.Name)
if err != nil {
return nil, err
}
err = table.parse()
if err != nil {
return nil, err
}
table.conn = nil // to save memory since it is not going to be used again
return table, nil
}
func (t *Table) parse() error {
// +--------------------------- field type
// | +---------------- field size / enum values: decimal(10,2) or enum('a','b')
// | | +---------- extra info (unsigned, etc)
// | | |
re := regexp.MustCompile(`^(.*?)(?:\((.*?)\)(.*))?$`)
query := "SELECT * FROM `information_schema`.`COLUMNS` WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? ORDER BY ORDINAL_POSITION"
constraints := constraintsAsMap(t.Constraints)
rows, err := t.conn.Query(query, t.Schema, t.Name)
if err != nil {
return err
}
defer rows.Close()
cols, err := rows.Columns()
if err != nil {
return errors.Wrap(err, "Cannot get column names")
}
for rows.Next() {
var f Field
var allowNull string
fields := makeScanRecipients(&f, &allowNull, cols)
err := rows.Scan(fields...)
if err != nil {
log.Errorf("Cannot get table fields: %s", err)
continue
}
allowedValues := []string{}
if f.DataType == "enum" || f.DataType == "set" {
m := re.FindStringSubmatch(f.ColumnType)
if len(m) < 2 {
continue
}
vals := strings.Split(m[2], ",")
for _, val := range vals {
val = strings.TrimPrefix(val, "'")
val = strings.TrimSuffix(val, "'")
allowedValues = append(allowedValues, val)
}
}
f.SetEnumVals = allowedValues
f.IsNullable = allowNull == "YES"
f.Constraint = constraints[f.ColumnName]
t.Fields = append(t.Fields, f)
}
if rows.Err() != nil {
return rows.Err()
}
return nil
}
func makeScanRecipients(f *Field, allowNull *string, cols []string) []interface{} {
fields := []interface{}{
&f.TableCatalog,
&f.TableSchema,
&f.TableName,
&f.ColumnName,
&f.OrdinalPosition,
&f.ColumnDefault,
&allowNull,
&f.DataType,
&f.CharacterMaximumLength,
&f.CharacterOctetLength,
&f.NumericPrecision,
&f.NumericScale,
}
if len(cols) > 19 { // MySQL 5.5 does not have "DATETIME_PRECISION" field
fields = append(fields, &f.DatetimePrecision)
}
fields = append(fields, &f.CharacterSetName, &f.CollationName, &f.ColumnType, &f.ColumnKey, &f.Extra, &f.Privileges, &f.ColumnComment)
if len(cols) > 20 && cols[20] == "GENERATION_EXPRESSION" { // MySQL 5.7+ "GENERATION_EXPRESSION" field
fields = append(fields, &f.GenerationExpression)
}
if len(cols) > 21 && cols[21] == "SRS_ID" { // MySQL 8.0+ "SRS ID" field
fields = append(fields, &f.SrsID)
}
return fields
}
// FieldNames returns an string array with the table's field names
func (t *Table) FieldNames() []string {
fields := []string{}
for _, field := range t.Fields {
fields = append(fields, field.ColumnName)
}
return fields
}
func getIndexes(db *sql.DB, schema, tableName string) (map[string]Index, error) {
query := fmt.Sprintf("SHOW INDEXES FROM `%s`.`%s`", schema, tableName)
rows, err := db.Query(query)
if err != nil {
return nil, err
}
indexes := make(map[string]Index)
for rows.Next() {
var i IndexField
var table string
fields := []interface{}{&table, &i.NonUnique, &i.KeyName, &i.SeqInIndex,
&i.ColumnName, &i.Collation, &i.Cardinality, &i.SubPart,
&i.Packed, &i.Null, &i.IndexType, &i.Comment, &i.IndexComment,
}
cols, err := rows.Columns()
if err == nil && len(cols) >= 14 && cols[13] == "Visible" {
fields = append(fields, &i.Visible)
}
if err == nil && len(cols) >= 15 && cols[14] == "Expression" {
fields = append(fields, &i.Expression)
}
err = rows.Scan(fields...)
if err != nil {
return nil, fmt.Errorf("cannot read indexes: %s", err)
}
if index, ok := indexes[i.KeyName]; !ok {
indexes[i.KeyName] = Index{
Name: i.KeyName,
Unique: !i.NonUnique,
Fields: []string{i.ColumnName},
Visible: i.Visible == "YES" || i.Visible == "",
Expression: i.Expression.String,
}
} else {
index.Fields = append(index.Fields, i.ColumnName)
index.Unique = index.Unique || !i.NonUnique
}
}
if err := rows.Close(); err != nil {
return nil, errors.Wrap(err, "Cannot close query rows at getIndexes")
}
return indexes, nil
}
func getConstraints(db *sql.DB, schema, tableName string) ([]Constraint, error) {
query := "SELECT tc.CONSTRAINT_NAME, " +
"kcu.COLUMN_NAME, " +
"kcu.REFERENCED_TABLE_SCHEMA, " +
"kcu.REFERENCED_TABLE_NAME, " +
"kcu.REFERENCED_COLUMN_NAME " +
"FROM information_schema.TABLE_CONSTRAINTS tc " +
"LEFT JOIN information_schema.KEY_COLUMN_USAGE kcu " +
"ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME " +
"WHERE tc.CONSTRAINT_TYPE = 'FOREIGN KEY' " +
fmt.Sprintf("AND tc.TABLE_SCHEMA = '%s' ", schema) +
fmt.Sprintf("AND tc.TABLE_NAME = '%s'", tableName)
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
constraints := []Constraint{}
for rows.Next() {
var c Constraint
err := rows.Scan(&c.ConstraintName, &c.ColumnName, &c.ReferencedTableSchema,
&c.ReferencedTableName, &c.ReferencedColumnName)
if err != nil {
return nil, fmt.Errorf("cannot read constraints: %s", err)
}
constraints = append(constraints, c)
}
return constraints, nil
}
func getTriggers(db *sql.DB, schema, tableName string) ([]Trigger, error) {
query := fmt.Sprintf("SHOW TRIGGERS FROM `%s` LIKE '%s'", schema, tableName)
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
triggers := []Trigger{}
for rows.Next() {
var t Trigger
err := rows.Scan(&t.Trigger, &t.Event, &t.Table, &t.Statement, &t.Timing,
&t.Created, &t.SQLMode, &t.Definer, &t.CharacterSetClient, &t.CollationConnection,
&t.DatabaseCollation)
if err != nil {
return nil, fmt.Errorf("cannot read trigger: %s", err)
}
triggers = append(triggers, t)
}
return triggers, nil
}
func constraintsAsMap(constraints []Constraint) map[string]*Constraint {
m := make(map[string]*Constraint)
for _, c := range constraints {
m[c.ColumnName] = &Constraint{
ConstraintName: c.ConstraintName,
ColumnName: c.ColumnName,
ReferencedTableSchema: c.ReferencedTableSchema,
ReferencedTableName: c.ReferencedTableName,
ReferencedColumnName: c.ReferencedColumnName,
}
}
return m
}