-
Notifications
You must be signed in to change notification settings - Fork 0
/
alter_table.go
249 lines (226 loc) · 6.73 KB
/
alter_table.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
package gosql
import "strings"
// ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
//
// action [, ... ]
//
// ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
//
// RENAME [ COLUMN ] column_name TO new_column_name
//
// ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
//
// RENAME CONSTRAINT constraint_name TO new_constraint_name
//
// ALTER TABLE [ IF EXISTS ] name
//
// RENAME TO new_name
//
// ALTER TABLE [ IF EXISTS ] name
//
// SET SCHEMA new_schema
//
// ALTER TABLE ALL IN TABLESPACE name [ OWNED BY role_name [, ... ] ]
//
// SET TABLESPACE new_tablespace [ NOWAIT ]
//
// ALTER TABLE [ IF EXISTS ] name
//
// ATTACH PARTITION partition_name { FOR VALUES partition_bound_spec | DEFAULT }
//
// ALTER TABLE [ IF EXISTS ] name
//
// DETACH PARTITION partition_name [ CONCURRENTLY | FINALIZE ]
//
// and partition_bound_spec is:
//
// IN ( partition_bound_expr [, ...] ) |
// FROM ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] )
//
// TO ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] ) |
//
// WITH ( MODULUS numeric_literal, REMAINDER numeric_literal )
//
// and column_constraint is:
//
// [ CONSTRAINT constraint_name ]
// { NOT NULL |
//
// NULL |
// CHECK ( expression ) [ NO INHERIT ] |
// DEFAULT default_expr |
// GENERATED ALWAYS AS ( generation_expr ) STORED |
// GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ] |
// UNIQUE [ NULLS [ NOT ] DISTINCT ] index_parameters |
// PRIMARY KEY index_parameters |
// REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
// [ ON DELETE referential_action ] [ ON UPDATE referential_action ] }
//
// [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
//
// and table_constraint is:
//
// [ CONSTRAINT constraint_name ]
// { CHECK ( expression ) [ NO INHERIT ] |
//
// UNIQUE [ NULLS [ NOT ] DISTINCT ] ( column_name [, ... ] ) index_parameters |
// PRIMARY KEY ( column_name [, ... ] ) index_parameters |
// EXCLUDE [ USING index_method ] ( exclude_element WITH operator [, ... ] ) index_parameters [ WHERE ( predicate ) ] |
// FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ]
// [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ] }
//
// [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
//
// and table_constraint_using_index is:
//
// [ CONSTRAINT constraint_name ]
// { UNIQUE | PRIMARY KEY } USING INDEX index_name
// [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
//
// index_parameters in UNIQUE, PRIMARY KEY, and EXCLUDE constraints are:
//
// [ INCLUDE ( column_name [, ... ] ) ]
// [ WITH ( storage_parameter [= value] [, ... ] ) ]
// [ USING INDEX TABLESPACE tablespace_name ]
//
// exclude_element in an EXCLUDE constraint is:
//
// { column_name | ( expression ) } [ opclass ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]
//
// referential_action in a FOREIGN KEY/REFERENCES constraint is:
//
// { NO ACTION | RESTRICT | CASCADE | SET NULL [ ( column_name [, ... ] ) ] | SET DEFAULT [ ( column_name [, ... ] ) ] }
type Alter struct {
// ordered expression
ordered orderedExpression
// partition bound
bound partitionBound
// action
actions []*alterTableAction
}
// Action get action
func (a *Alter) Action() *alterTableAction {
action := &alterTableAction{}
a.actions = append(a.actions, action)
return action
}
// DetachPartition name
func (a *Alter) DetachPartition(name string) *Alter {
a.ordered.Add(3, a.ordered.Concat("DETACH PARTITION ", name))
return a
}
// DetachPartitionConcurrently name
func (a *Alter) DetachPartitionConcurrently(name string) *Alter {
a.ordered.Add(3, a.ordered.Concat("DETACH PARTITION ", name, " CONCURRENTLY"))
return a
}
// DetachPartitionFinalize name
func (a *Alter) DetachPartitionFinalize(name string) *Alter {
a.ordered.Add(3, a.ordered.Concat("DETACH PARTITION ", name, " FINALIZE"))
return a
}
// AttachDefaultPartition name
func (a *Alter) AttachDefaultPartition(name string) *Alter {
a.ordered.Add(3, a.ordered.Concat("ATTACH PARTITION ", name, " DEFAULT"))
return a
}
// AttachPartition name
func (a *Alter) AttachPartition(name string) *partitionBound {
a.ordered.Add(3, a.ordered.Concat("ATTACH PARTITION ", name))
return &a.bound
}
// AllInTableSpace name
func (a *Alter) AllInTableSpace(name string) *Alter {
a.ordered.Add(0, a.ordered.Concat("ALL IN TABLESPACE ", name))
return a
}
// OwnedBy role
func (a *Alter) OwnedBy(role ...string) *Alter {
a.ordered.Add(1, a.ordered.Concat("OWNED BY ", strings.Join(role, ", ")))
return a
}
// SetTableSpace name
func (a *Alter) SetTableSpace(name string) *Alter {
a.ordered.Add(3, a.ordered.Concat("SET TABLESPACE ", name))
return a
}
// SetTableSpaceNoWait name
func (a *Alter) SetTableSpaceNoWait(name string) *Alter {
a.ordered.Add(3, a.ordered.Concat("SET TABLESPACE ", name, " NOWAIT"))
return a
}
// SetSchema table
func (a *Alter) SetSchema(name string) *Alter {
a.ordered.Add(3, a.ordered.Concat("SET SCHEMA ", name))
return a
}
// Rename table
func (a *Alter) Rename(name string) *Alter {
a.ordered.Add(3, a.ordered.Concat("RENAME TO ", name))
return a
}
// RenameConstraint rename constraint
func (a *Alter) RenameConstraint(old, new string) *Alter {
a.ordered.Add(3, a.ordered.Concat("RENAME CONSTRAINT ", old, " TO ", new))
return a
}
// RenameColumn rename column
func (a *Alter) RenameColumn(old, new string) *Alter {
a.ordered.Add(3, a.ordered.Concat("RENAME COLUMN ", old, " TO ", new))
return a
}
// IfExists set if exists
func (a *Alter) IfExists() *Alter {
a.ordered.Add(0, a.ordered.Concat("IF EXISTS"))
return a
}
// Only set
func (a *Alter) Only() *Alter {
a.ordered.Add(1, a.ordered.Concat("ONLY"))
return a
}
// Name set name
func (a *Alter) Name(name string) *Alter {
a.ordered.Add(2, a.ordered.Concat(name))
return a
}
// IsEmpty check if empty
func (a *Alter) IsEmpty() bool {
return a == nil || a.ordered.IsEmpty() && len(a.actions) == 0
}
// String render alter table query
func (a *Alter) String() string {
if a.IsEmpty() {
return ""
}
b := strings.Builder{}
b.WriteString("ALTER TABLE ")
b.WriteString(a.ordered.String())
if !a.bound.IsEmpty() {
b.WriteString(" FOR VALUES " + a.bound.String())
}
for i := range a.actions {
if i > 0 {
b.WriteString(",")
}
b.WriteString(" " + a.actions[i].String())
}
return b.String() + ";"
}
// SQL common sql interface
func (a *Alter) SQL() (query string, params []any, returning []any) {
query = a.String()
params = a.ordered.GetArguments()
for i := range a.actions {
params = append(params, a.actions[i].GetArguments()...)
}
return
}
// AlterTable table constructor
func AlterTable(args ...string) *Alter {
alter := &Alter{}
if len(args) > 0 {
alter.ordered.Add(2, alter.ordered.Concat(args[0]))
}
return alter
}