-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtable_command.go
207 lines (161 loc) · 4.28 KB
/
table_command.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
package migrator
import (
"fmt"
"strings"
)
// TableCommands is a pool of commands to be executed on the table.
// https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
type TableCommands []command
func (tc TableCommands) toSQL() string {
rows := []string{}
for _, c := range tc {
rows = append(rows, c.toSQL())
}
return strings.Join(rows, ", ")
}
// AddColumnCommand is a command to add the column to the table.
type AddColumnCommand struct {
Name string
Column columnType
After string
First bool
}
func (c AddColumnCommand) toSQL() string {
if c.Column == nil {
return ""
}
definition := c.Column.buildRow()
if c.Name == "" || definition == "" {
return ""
}
sql := "ADD COLUMN `" + c.Name + "` " + definition
if c.After != "" {
sql += " AFTER " + c.After
} else if c.First {
sql += " FIRST"
}
return sql
}
// RenameColumnCommand is a command to rename a column in the table.
// Warning ⚠️ BC incompatible!
//
// Info ℹ️ extension for Oracle compatibility.
type RenameColumnCommand struct {
Old string
New string
}
func (c RenameColumnCommand) toSQL() string {
if c.Old == "" || c.New == "" {
return ""
}
return fmt.Sprintf("RENAME COLUMN `%s` TO `%s`", c.Old, c.New)
}
// ModifyColumnCommand is a command to modify column type.
// Warning ⚠️ BC incompatible!
//
// Info ℹ️ extension for Oracle compatibility.
type ModifyColumnCommand struct {
Name string
Column columnType
}
func (c ModifyColumnCommand) toSQL() string {
if c.Column == nil {
return ""
}
definition := c.Column.buildRow()
if c.Name == "" || definition == "" {
return ""
}
return fmt.Sprintf("MODIFY `%s` %s", c.Name, definition)
}
// ChangeColumnCommand is a default command to change column.
// Warning ⚠️ BC incompatible!
type ChangeColumnCommand struct {
From string
To string
Column columnType
}
func (c ChangeColumnCommand) toSQL() string {
if c.Column == nil {
return ""
}
definition := c.Column.buildRow()
if c.From == "" || c.To == "" || definition == "" {
return ""
}
return fmt.Sprintf("CHANGE `%s` `%s` %s", c.From, c.To, c.Column.buildRow())
}
// DropColumnCommand is a command to drop a column from the table.
// Warning ⚠️ BC incompatible!
type DropColumnCommand string
// Info ℹ️ campatible with Oracle
func (c DropColumnCommand) toSQL() string {
if c == "" {
return ""
}
return fmt.Sprintf("DROP COLUMN `%s`", c)
}
// AddIndexCommand adds a key to the table.
type AddIndexCommand struct {
Name string
Columns []string
}
func (c AddIndexCommand) toSQL() string {
if c.Name == "" || len(c.Columns) == 0 {
return ""
}
return fmt.Sprintf("ADD KEY `%s` (`%s`)", c.Name, strings.Join(c.Columns, "`, `"))
}
// DropIndexCommand removes the key from the table.
type DropIndexCommand string
func (c DropIndexCommand) toSQL() string {
if c == "" {
return ""
}
return fmt.Sprintf("DROP KEY `%s`", c)
}
// AddForeignCommand adds the foreign key constraint to the table.
type AddForeignCommand struct {
Foreign Foreign
}
func (c AddForeignCommand) toSQL() string {
if c.Foreign.render() == "" {
return ""
}
return "ADD " + c.Foreign.render()
}
// DropForeignCommand is a command to remove a foreign key constraint.
type DropForeignCommand string
func (c DropForeignCommand) toSQL() string {
if c == "" {
return ""
}
return fmt.Sprintf("DROP FOREIGN KEY `%s`", c)
}
// AddUniqueIndexCommand is a command to add a unique key to the table on some columns.
type AddUniqueIndexCommand struct {
Key string
Columns []string
}
func (c AddUniqueIndexCommand) toSQL() string {
if c.Key == "" || len(c.Columns) == 0 {
return ""
}
return fmt.Sprintf("ADD UNIQUE KEY `%s` (`%s`)", c.Key, strings.Join(c.Columns, "`, `"))
}
// AddPrimaryIndexCommand is a command to add a primary key.
type AddPrimaryIndexCommand string
func (c AddPrimaryIndexCommand) toSQL() string {
if c == "" {
return ""
}
return fmt.Sprintf("ADD PRIMARY KEY (`%s`)", c)
}
// DropPrimaryIndexCommand is a command to remove the primary key from the table.
type DropPrimaryIndexCommand struct{}
func (c DropPrimaryIndexCommand) toSQL() string {
return "DROP PRIMARY KEY"
}
// ADD {FULLTEXT | SPATIAL} [INDEX | KEY] [index_name] (key_part,...) [index_option] ...
// DROP {CHECK | CONSTRAINT} symbol
// RENAME {INDEX | KEY} old_index_name TO new_index_name