forked from pingcap/parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddlmerge.go
194 lines (187 loc) · 4.22 KB
/
ddlmerge.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
package ast
import (
"fmt"
)
var (
ErrMergeNotSupportDDL = fmt.Errorf("can not support ddl")
)
//Merge other ddl to generate final create sql
func (n *CreateTableStmt) Merge(other DDLNode) error {
if at, ok := other.(*AlterTableStmt); ok {
return n.MergeAlterTableStmt(at)
}
if at, ok := other.(*CreateIndexStmt); ok {
tp := ConstraintIndex
if at.KeyType == IndexKeyTypeUnique {
tp = ConstraintUniqIndex
}
n.Constraints = append(n.Constraints, &Constraint{
node: at.node,
IfNotExists: at.IfNotExists,
Tp: tp,
Name: at.IndexName,
Keys: at.IndexPartSpecifications,
Refer: nil,
Option: at.IndexOption,
Expr: nil,
Enforced: false,
InColumn: false,
InColumnName: "",
IsEmptyIndex: false,
})
return nil
}
return ErrMergeNotSupportDDL
}
func (n *CreateTableStmt) MergeAlterTableStmt(at *AlterTableStmt) error {
for _, v := range at.Specs {
err := n.mergeAlterTableSpec(v)
if err != nil {
return err
}
}
return nil
}
func (n *CreateTableStmt) mergeAlterTableSpec(spec *AlterTableSpec) error {
switch spec.Tp {
case AlterTableOption:
{
for _, v := range spec.Options {
for ii, vv := range n.Options {
if vv.Tp == v.Tp {
if v.Tp == TableOptionCharset {
v.UintValue = TableOptionCharsetWithoutConvertTo
}
n.Options[ii] = v
}
}
}
}
case AlterTableAddColumns:
{
switch spec.Position.Tp {
case ColumnPositionFirst:
n.Cols = append(append([]*ColumnDef{}, spec.NewColumns...), n.Cols...)
case ColumnPositionNone:
n.Cols = append(n.Cols, spec.NewColumns...)
case ColumnPositionAfter:
relativeColumn := spec.Position.RelativeColumn
for i, v := range n.Cols {
if v.Name.Name.L == relativeColumn.Name.L {
n.Cols = append(append(append([]*ColumnDef{}, n.Cols[:i+1]...), spec.NewColumns...), n.Cols[i+1:]...)
break
}
}
}
}
case AlterTableAddConstraint:
{
n.Constraints = append(n.Constraints, spec.Constraint)
}
case AlterTableDropColumn:
{
for i, v := range n.Cols {
if v.Name.Name.L == spec.OldColumnName.Name.L {
n.Cols = append(n.Cols[:i], n.Cols[i+1:]...)
break
}
}
}
case AlterTableDropPrimaryKey:
{
for i, v := range n.Constraints {
if v.Tp == ConstraintPrimaryKey {
n.Constraints = append(n.Constraints[:i], n.Constraints[i+1:]...)
break
}
}
}
case AlterTableDropIndex:
{
for i, v := range n.Constraints {
if v.Name == spec.Name {
n.Constraints = append(n.Constraints[:i], n.Constraints[i+1:]...)
break
}
}
}
case AlterTableModifyColumn:
{
for _, v := range spec.NewColumns {
for ii, vv := range n.Cols {
if vv.Name.Name.L == v.Name.Name.L {
n.Cols[ii] = v
break
}
}
}
}
case AlterTableChangeColumn:
{
oldName := spec.OldColumnName.Name
for i, v := range n.Cols {
if v.Name.Name.L == oldName.L {
n.Cols[i] = spec.NewColumns[0]
break
}
}
}
case AlterTableRenameColumn:
{
oldName := spec.OldColumnName.Name
for _, v := range n.Cols {
if v.Name.Name.L == oldName.L {
v.Name = spec.NewColumnName
break
}
}
}
case AlterTableRenameTable:
{
n.Table = spec.NewTable
}
case AlterTableAlterColumn:
{
for _, v := range spec.NewColumns {
for ii, vv := range n.Cols {
if vv.Name.Name.L == v.Name.Name.L {
n.Cols[ii] = v
break
}
}
}
}
case AlterTableRenameIndex:
{
oldKey := spec.FromKey.L
for _, v := range n.Constraints {
if v.Tp == ConstraintIndex && v.Name == oldKey {
v.Name = spec.ToKey.L
break
}
}
}
case AlterTableAddPartitions:
{
n.Partition.Definitions = append(n.Partition.Definitions, spec.PartDefinitions...)
}
case AlterTableDropPartition:
{
for _, v := range spec.PartitionNames {
for ii, vv := range n.Partition.Definitions {
if vv.Name.L == v.L {
n.Partition.Definitions = append(n.Partition.Definitions[:ii], n.Partition.Definitions[ii+1:]...)
break
}
}
}
}
case AlterTableReorganizePartition:
{
n.Partition.Definitions = spec.PartDefinitions
}
default:
return fmt.Errorf("can not support type:%v", spec.Tp.String())
}
return nil
}