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 package com.healthmarketscience.sqlbuilder;
29
30 import java.io.IOException;
31
32 import com.healthmarketscience.common.util.AppendableExt;
33 import com.healthmarketscience.sqlbuilder.dbspec.Column;
34 import com.healthmarketscience.sqlbuilder.dbspec.Constraint;
35 import com.healthmarketscience.sqlbuilder.dbspec.Table;
36
37
38
39
40
41
42 public class AlterTableQuery extends Query<AlterTableQuery>
43 {
44 private SqlObject _table;
45 private SqlObject _action;
46
47 public AlterTableQuery(Table table) {
48 this((Object)table);
49 }
50
51
52
53
54
55 public AlterTableQuery(Object tableStr) {
56 _table = Converter.toCustomTableSqlObject(tableStr);
57 }
58
59
60
61
62
63
64
65 public AlterTableQuery setAction(Object action) {
66 _action = Converter.toCustomSqlObject(action);
67 return this;
68 }
69
70
71
72
73 public AlterTableQuery setAddConstraint(Constraint constraint) {
74 return setAddCustomConstraint(constraint);
75 }
76
77
78
79
80
81
82
83 public AlterTableQuery setAddCustomConstraint(Object constraint) {
84 return setAction(new AddConstraintAction(constraint));
85 }
86
87
88
89
90 public AlterTableQuery setAddColumn(Column column) {
91 return setAddCustomColumn(column);
92 }
93
94
95
96
97
98
99
100 public AlterTableQuery setAddCustomColumn(Object column) {
101 return setAction(new AddColumnAction(column));
102 }
103
104 @Override
105 protected void collectSchemaObjects(ValidationContext vContext) {
106 super.collectSchemaObjects(vContext);
107 _table.collectSchemaObjects(vContext);
108 _action.collectSchemaObjects(vContext);
109 }
110
111 @Override
112 protected void appendTo(AppendableExt app, SqlContext newContext)
113 throws IOException
114 {
115 newContext.setUseTableAliases(false);
116
117 app.append("ALTER TABLE ").append(_table).append(_action);
118 }
119
120
121
122
123
124 public static class AddConstraintAction extends SqlObject
125 {
126 protected SqlObject _constraint;
127
128 public AddConstraintAction(Object constraint) {
129 _constraint = Converter.toCustomConstraintClause(constraint);
130 }
131
132 protected ConstraintClause getConstraint() {
133 return (ConstraintClause)_constraint;
134 }
135
136 @Override
137 protected void collectSchemaObjects(ValidationContext vContext) {
138 _constraint.collectSchemaObjects(vContext);
139 }
140
141 @Override
142 public void appendTo(AppendableExt app) throws IOException {
143 app.append(" ADD ").append(_constraint);
144 }
145 }
146
147
148
149
150
151
152
153
154 public static class AddColumnAction extends SqlObject
155 {
156 private SqlObject _column;
157
158 public AddColumnAction(Object column) {
159 _column = Converter.toCustomTypedColumnSqlObject(column);
160 }
161
162 @Override
163 protected void collectSchemaObjects(ValidationContext vContext) {
164 _column.collectSchemaObjects(vContext);
165 }
166
167 @Override
168 public void appendTo(AppendableExt app) throws IOException {
169 app.append(" ADD ").append(_column);
170 }
171
172
173
174
175
176
177
178 public AddColumnAction addConstraint(Object constraint) {
179 if(_column instanceof TypedColumnObject) {
180 ((TypedColumnObject)_column).addConstraint(constraint);
181 }
182 return this;
183 }
184
185
186
187
188
189
190
191 public AddColumnAction setDefaultValue(Object defaultValue) {
192 if(_column instanceof TypedColumnObject) {
193 ((TypedColumnObject)_column).setDefaultValue(defaultValue);
194 }
195 return this;
196 }
197 }
198
199
200
201
202
203
204
205 @Deprecated
206 public static class AddUniqueConstraintAction extends AddConstraintAction
207 {
208 public AddUniqueConstraintAction() {
209 super(ConstraintClause.unique());
210 }
211
212
213
214
215 public AddUniqueConstraintAction addColumns(Column... columns) {
216 return addCustomColumns((Object[])columns);
217 }
218
219
220
221
222
223
224
225 public AddUniqueConstraintAction addCustomColumns(Object... columnStrs) {
226 getConstraint().addCustomColumns(columnStrs);
227 return this;
228 }
229 }
230
231
232
233
234
235
236 @Deprecated
237 public static class AddPrimaryConstraintAction extends AddConstraintAction
238 {
239 public AddPrimaryConstraintAction() {
240 super(ConstraintClause.primaryKey());
241 }
242
243
244
245
246 public AddPrimaryConstraintAction addColumns(Column... columns) {
247 return addCustomColumns((Object[])columns);
248 }
249
250
251
252
253
254
255
256 public AddPrimaryConstraintAction addCustomColumns(Object... columnStrs) {
257 getConstraint().addCustomColumns(columnStrs);
258 return this;
259 }
260 }
261
262
263
264
265
266
267
268
269 @Deprecated
270 public static class AddForeignConstraintAction extends AddConstraintAction
271 {
272
273
274
275
276 public AddForeignConstraintAction(Table table) {
277 this((Object)table);
278 }
279
280
281
282
283
284
285
286
287 public AddForeignConstraintAction(Object table) {
288 super(ConstraintClause.foreignKey(table));
289 }
290
291 @Override
292 protected ForeignKeyConstraintClause getConstraint() {
293 return (ForeignKeyConstraintClause)super.getConstraint();
294 }
295
296
297
298
299
300 public AddForeignConstraintAction addPrimaryKeyReference(Column col) {
301 return addReference(col, null);
302 }
303
304
305
306
307
308
309
310
311 public AddForeignConstraintAction addCustomPrimaryKeyReference(Object col){
312 return addCustomReference(col, null);
313 }
314
315
316
317
318
319 public AddForeignConstraintAction addReference(
320 Column col, Column referencedCol) {
321 return addCustomReference(col, referencedCol);
322 }
323
324
325
326
327
328
329
330
331
332 public AddForeignConstraintAction addCustomReference(
333 Object col, Object referencedCol) {
334 getConstraint().addCustomColumns(col);
335 if (referencedCol != null) {
336 getConstraint().addCustomRefColumns(referencedCol);
337 }
338 return this;
339 }
340 }
341
342 }