-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathtype_converter.go
More file actions
101 lines (94 loc) · 2.38 KB
/
type_converter.go
File metadata and controls
101 lines (94 loc) · 2.38 KB
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
package sqlparser
import (
"bytes"
"github.com/blastrain/vitess-sqlparser/tidbparser/ast"
)
func convertFromCreateTableStmt(stmt *ast.CreateTableStmt, ddl *DDL) Statement {
columns := []*ColumnDef{}
for _, col := range stmt.Cols {
options := []*ColumnOption{}
for _, option := range col.Options {
expr := ""
if option.Expr != nil {
var buf bytes.Buffer
option.Expr.Format(&buf)
expr = buf.String()
}
options = append(options, &ColumnOption{
Type: ColumnOptionType(option.Tp),
Value: expr,
})
}
columns = append(columns, &ColumnDef{
Name: col.Name.Name.String(),
Type: col.Tp.String(),
Elems: col.Tp.Elems,
Options: options,
})
}
constraints := []*Constraint{}
for _, constraint := range stmt.Constraints {
keys := []ColIdent{}
for _, key := range constraint.Keys {
keys = append(keys, NewColIdent(key.Column.Name.String()))
}
constraints = append(constraints, &Constraint{
Type: ConstraintType(constraint.Tp),
Name: constraint.Name,
Keys: keys,
})
}
options := []*TableOption{}
for _, option := range stmt.Options {
options = append(options, &TableOption{
Type: TableOptionType(option.Tp),
StrValue: option.StrValue,
UintValue: option.UintValue,
})
}
return &CreateTable{
DDL: ddl,
Columns: columns,
Constraints: constraints,
Options: options,
}
}
func convertFromTruncateTableStmt(stmt *ast.TruncateTableStmt) Statement {
return &TruncateTable{Table: TableName{Name: TableIdent{v: stmt.Table.Name.String()}}}
}
func convertTiDBStmtToVitessOtherAdmin(stmts []ast.StmtNode, admin *OtherAdmin) Statement {
for _, stmt := range stmts {
switch adminStmt := stmt.(type) {
case *ast.TruncateTableStmt:
return convertFromTruncateTableStmt(adminStmt)
default:
return admin
}
}
return nil
}
func convertTiDBStmtToVitessDDL(stmts []ast.StmtNode, ddl *DDL) Statement {
for _, stmt := range stmts {
switch ddlStmt := stmt.(type) {
case *ast.CreateTableStmt:
return convertFromCreateTableStmt(ddlStmt, ddl)
default:
return ddl
}
}
return nil
}
func convertTiDBStmtToVitessShow(stmts []ast.StmtNode, show *Show) Statement {
for _, stmt := range stmts {
switch showStmt := stmt.(type) {
case *ast.ShowStmt:
if showStmt.Table != nil {
return &Show{TableName: showStmt.Table.Name.String()}
}
return show
default:
return show
}
}
return nil
}