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
|
// CreateTableStmt is a statement to create a table.
// See https://dev.mysql.com/doc/refman/5.7/en/create-table.html
type CreateTableStmt struct {
ddlNode
IfNotExists bool
TemporaryKeyword
// Meanless when TemporaryKeyword is not TemporaryGlobal.
// ON COMMIT DELETE ROWS => true
// ON COMMIT PRESERVE ROW => false
OnCommitDelete bool
Table *TableName
ReferTable *TableName
Cols []*ColumnDef
Constraints []*Constraint
Options []*TableOption
Partition *PartitionOptions
OnDuplicate OnDuplicateKeyHandlingType
Select ResultSetNode
}
// Restore implements Node interface.
func (n *CreateTableStmt) Restore(ctx *format.RestoreCtx) error {
switch n.TemporaryKeyword {
case TemporaryNone:
ctx.WriteKeyWord("CREATE TABLE ")
case TemporaryGlobal:
ctx.WriteKeyWord("CREATE GLOBAL TEMPORARY TABLE ")
case TemporaryLocal:
ctx.WriteKeyWord("CREATE TEMPORARY TABLE ")
}
if n.IfNotExists {
ctx.WriteKeyWord("IF NOT EXISTS ")
}
if err := n.Table.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while splicing CreateTableStmt Table")
}
if n.ReferTable != nil {
ctx.WriteKeyWord(" LIKE ")
if err := n.ReferTable.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while splicing CreateTableStmt ReferTable")
}
}
lenCols := len(n.Cols)
lenConstraints := len(n.Constraints)
if lenCols+lenConstraints > 0 {
ctx.WritePlain(" (")
for i, col := range n.Cols {
if i > 0 {
ctx.WritePlain(",")
}
if err := col.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while splicing CreateTableStmt ColumnDef: [%v]", i)
}
}
for i, constraint := range n.Constraints {
if i > 0 || lenCols >= 1 {
ctx.WritePlain(",")
}
if err := constraint.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while splicing CreateTableStmt Constraints: [%v]", i)
}
}
ctx.WritePlain(")")
}
for i, option := range n.Options {
ctx.WritePlain(" ")
if err := option.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while splicing CreateTableStmt TableOption: [%v]", i)
}
}
if n.Partition != nil {
ctx.WritePlain(" ")
if err := n.Partition.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while splicing CreateTableStmt Partition")
}
}
if n.Select != nil {
switch n.OnDuplicate {
case OnDuplicateKeyHandlingError:
ctx.WriteKeyWord(" AS ")
case OnDuplicateKeyHandlingIgnore:
ctx.WriteKeyWord(" IGNORE AS ")
case OnDuplicateKeyHandlingReplace:
ctx.WriteKeyWord(" REPLACE AS ")
}
if err := n.Select.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while splicing CreateTableStmt Select")
}
}
if n.TemporaryKeyword == TemporaryGlobal {
if n.OnCommitDelete {
ctx.WriteKeyWord(" ON COMMIT DELETE ROWS")
} else {
ctx.WriteKeyWord(" ON COMMIT PRESERVE ROWS")
}
}
return nil
}
// Accept implements Node Accept interface.
func (n *CreateTableStmt) Accept(v Visitor) (Node, bool) {
newNode, skipChildren := v.Enter(n)
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*CreateTableStmt)
node, ok := n.Table.Accept(v)
if !ok {
return n, false
}
n.Table = node.(*TableName)
if n.ReferTable != nil {
node, ok = n.ReferTable.Accept(v)
if !ok {
return n, false
}
n.ReferTable = node.(*TableName)
}
for i, val := range n.Cols {
node, ok = val.Accept(v)
if !ok {
return n, false
}
n.Cols[i] = node.(*ColumnDef)
}
for i, val := range n.Constraints {
node, ok = val.Accept(v)
if !ok {
return n, false
}
n.Constraints[i] = node.(*Constraint)
}
if n.Select != nil {
node, ok := n.Select.Accept(v)
if !ok {
return n, false
}
n.Select = node.(ResultSetNode)
}
if n.Partition != nil {
node, ok := n.Partition.Accept(v)
if !ok {
return n, false
}
n.Partition = node.(*PartitionOptions)
}
return v.Leave(n)
}
|