This repository has been archived by the owner on Jul 10, 2018. It is now read-only.
/
db_meta.go
350 lines (295 loc) · 8.14 KB
/
db_meta.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
package context
import (
"fmt"
"github.com/huangjunwen/JustSQL/utils"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/util/types"
)
// This file contains xxxMeta types. They are used to store extracted
// meta information from tidb model.xxxInfo.
// All names used here should use the lower case version.
// DBMeta contains meta information of a database.
type DBMeta struct {
*model.DBInfo
Name string
PascalName string
Tables map[string]*TableMeta
}
func NewDBMeta(ctx *Context, dbInfo *model.DBInfo) (*DBMeta, error) {
ret := &DBMeta{
DBInfo: dbInfo,
Name: dbInfo.Name.L,
PascalName: utils.PascalCase(dbInfo.Name.L),
Tables: make(map[string]*TableMeta),
}
for _, tableInfo := range dbInfo.Tables {
tableMeta, err := NewTableMeta(ctx, ret, tableInfo)
if err != nil {
return nil, err
}
ret.Tables[tableMeta.Name] = tableMeta
}
return ret, nil
}
// TableMeta contains meta information of a table.
type TableMeta struct {
*model.TableInfo
DB *DBMeta
Name string
PascalName string
Columns []*ColumnMeta
Indices []*IndexMeta
ForeignKeys []*FKMeta
// Shortcut
primaryIndex *IndexMeta
autoIncColumn *ColumnMeta
// column name -> column index
columnByName map[string]*ColumnMeta
}
func NewTableMeta(ctx *Context, dbMeta *DBMeta, tableInfo *model.TableInfo) (*TableMeta, error) {
ret := &TableMeta{
TableInfo: tableInfo,
DB: dbMeta,
Name: tableInfo.Name.L,
PascalName: utils.PascalCase(tableInfo.Name.L),
Columns: make([]*ColumnMeta, 0, len(tableInfo.Columns)),
Indices: make([]*IndexMeta, 0, len(tableInfo.Indices)),
ForeignKeys: make([]*FKMeta, 0, len(tableInfo.ForeignKeys)),
columnByName: make(map[string]*ColumnMeta),
}
for _, columnInfo := range tableInfo.Columns {
columnMeta, err := NewColumnMeta(ctx, ret, columnInfo)
if err != nil {
return nil, err
}
ret.Columns = append(ret.Columns, columnMeta)
ret.columnByName[columnMeta.Name] = columnMeta
if columnMeta.IsAutoInc() {
if ret.autoIncColumn != nil {
panic(fmt.Errorf("Multiple auto increment columns found in table %q", ret.Name))
}
ret.autoIncColumn = columnMeta
}
}
if indexMeta := NewIndexMetaFromPKHandler(ctx, ret); indexMeta != nil {
ret.Indices = append(ret.Indices, indexMeta)
ret.primaryIndex = indexMeta
}
for _, indexInfo := range tableInfo.Indices {
indexMeta, err := NewIndexMeta(ctx, ret, indexInfo)
if err != nil {
return nil, err
}
ret.Indices = append(ret.Indices, indexMeta)
if indexMeta.Primary {
if ret.primaryIndex != nil {
panic(fmt.Errorf("Multiple primary index found in table %q", ret.Name))
}
ret.primaryIndex = indexMeta
}
}
for _, fkInfo := range tableInfo.ForeignKeys {
fkMeta, err := NewFKMeta(ctx, ret, fkInfo)
if err != nil {
return nil, err
}
ret.ForeignKeys = append(ret.ForeignKeys, fkMeta)
}
return ret, nil
}
// Return primary key columns if exists.
func (t *TableMeta) PrimaryColumns() []*ColumnMeta {
if t.primaryIndex == nil {
return nil
}
ret := make([]*ColumnMeta, 0, len(t.primaryIndex.ColumnIndices))
for _, col_idx := range t.primaryIndex.ColumnIndices {
ret = append(ret, t.Columns[col_idx])
}
return ret
}
// Return non-primary columns if exists.
func (t *TableMeta) NonPrimaryColumns() []*ColumnMeta {
if t.primaryIndex == nil {
return t.Columns
}
is_primary := make([]bool, len(t.Columns))
for _, col_idx := range t.primaryIndex.ColumnIndices {
is_primary[col_idx] = true
}
ret := make([]*ColumnMeta, 0, len(t.Columns)-len(t.primaryIndex.ColumnIndices))
for i, b := range is_primary {
if b {
continue
}
ret = append(ret, t.Columns[i])
}
return ret
}
// Return auto increment column if exists.
func (t *TableMeta) AutoIncColumn() *ColumnMeta {
return t.autoIncColumn
}
// Retrive column by its name.
func (t *TableMeta) ColumnByName(name string) *ColumnMeta {
ret, ok := t.columnByName[name]
if !ok {
return nil
}
return ret
}
// ColumnMeta contains meta data of a column.
type ColumnMeta struct {
*model.ColumnInfo
Table *TableMeta
Name string
PascalName string
Offset int
// Column field type.
Type *types.FieldType
}
func NewColumnMeta(ctx *Context, tableMeta *TableMeta, columnInfo *model.ColumnInfo) (*ColumnMeta, error) {
return &ColumnMeta{
ColumnInfo: columnInfo,
Table: tableMeta,
Name: columnInfo.Name.L,
PascalName: utils.PascalCase(columnInfo.Name.L),
Offset: columnInfo.Offset,
Type: &columnInfo.FieldType,
}, nil
}
func (c *ColumnMeta) IsEnum() bool {
return c.Type.Tp == mysql.TypeEnum
}
func (c *ColumnMeta) IsSet() bool {
return c.Type.Tp == mysql.TypeSet
}
func (c *ColumnMeta) Elems() []string {
return c.Type.Elems
}
func (c *ColumnMeta) IsNotNULL() bool {
return mysql.HasNotNullFlag(c.Type.Flag)
}
func (c *ColumnMeta) IsAutoInc() bool {
return mysql.HasAutoIncrementFlag(c.Type.Flag)
}
func (c *ColumnMeta) IsOnUpdateNow() bool {
return mysql.HasOnUpdateNowFlag(c.Type.Flag)
}
func (c *ColumnMeta) DefaultValue() interface{} {
return c.ColumnInfo.DefaultValue
}
// IndexMeta contains meta data of an index.
type IndexMeta struct {
// Nil if it is created from PKHandler.
*model.IndexInfo
Table *TableMeta
Name string
PascalName string
Unique bool
Primary bool
ColumnIndices []int
}
func NewIndexMeta(ctx *Context, tableMeta *TableMeta, indexInfo *model.IndexInfo) (*IndexMeta, error) {
ret := &IndexMeta{
IndexInfo: indexInfo,
Table: tableMeta,
Name: indexInfo.Name.L,
PascalName: utils.PascalCase(indexInfo.Name.L),
Unique: indexInfo.Unique,
Primary: indexInfo.Primary,
ColumnIndices: make([]int, 0, len(indexInfo.Columns)),
}
for _, column := range indexInfo.Columns {
ret.ColumnIndices = append(ret.ColumnIndices, column.Offset)
}
return ret, nil
}
// see: https://github.com/pingcap/tidb/issues/3746
func NewIndexMetaFromPKHandler(ctx *Context, tableMeta *TableMeta) *IndexMeta {
tableInfo := tableMeta.TableInfo
if !tableInfo.PKIsHandle {
return nil
}
columnInfo := tableInfo.GetPkColInfo()
return &IndexMeta{
Table: tableMeta,
Name: "primary",
PascalName: "Primary",
Unique: true,
Primary: true,
ColumnIndices: []int{columnInfo.Offset},
}
}
func (i *IndexMeta) Columns() []*ColumnMeta {
ret := []*ColumnMeta{}
for _, idx := range i.ColumnIndices {
ret = append(ret, i.Table.Columns[idx])
}
return ret
}
// FKMeta contains meta data of a foreign key.
type FKMeta struct {
*model.FKInfo
Table *TableMeta
Name string
PascalName string
ColNames []string
RefTableName string
RefColNames []string
}
func NewFKMeta(ctx *Context, tableMeta *TableMeta, fkInfo *model.FKInfo) (*FKMeta, error) {
ret := &FKMeta{
FKInfo: fkInfo,
Table: tableMeta,
Name: fkInfo.Name.L,
PascalName: utils.PascalCase(fkInfo.Name.L),
ColNames: make([]string, 0, len(fkInfo.Cols)),
RefTableName: fkInfo.RefTable.L,
RefColNames: make([]string, 0, len(fkInfo.RefCols)),
}
for _, col := range fkInfo.Cols {
ret.ColNames = append(ret.ColNames, col.L)
}
for _, refcol := range fkInfo.RefCols {
ret.RefColNames = append(ret.RefColNames, refcol.L)
}
return ret, nil
}
func (f *FKMeta) Columns() []*ColumnMeta {
ret := []*ColumnMeta{}
for _, colName := range f.ColNames {
ret = append(ret, f.Table.columnByName[colName])
}
return ret
}
func (f *FKMeta) RefTable() *TableMeta {
// XXX: Ref to another database?
return f.Table.DB.Tables[f.RefTableName]
}
func (f *FKMeta) RefColumns() []*ColumnMeta {
refTable := f.RefTable()
ret := []*ColumnMeta{}
for _, colName := range f.RefColNames {
ret = append(ret, refTable.columnByName[colName])
}
return ret
}
// RefIndex find the first index in ref table that this foreign key use.
func (f *FKMeta) RefIndex() *IndexMeta {
for _, index := range f.RefTable().Indices {
indexColumns := index.Columns()
mismatch := false
for i, refColName := range f.RefColNames {
if refColName != indexColumns[i].Name {
mismatch = true
break
}
}
if !mismatch {
return index
}
}
panic(fmt.Errorf("Can't find an index for fk %s.%s", f.Table.Name, f.Name))
}