-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
AlterExpression.java
772 lines (630 loc) · 21 KB
/
AlterExpression.java
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.alter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import net.sf.jsqlparser.statement.ReferentialAction;
import net.sf.jsqlparser.statement.ReferentialAction.Action;
import net.sf.jsqlparser.statement.ReferentialAction.Type;
import net.sf.jsqlparser.statement.create.table.ColDataType;
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
import net.sf.jsqlparser.statement.create.table.Index;
import net.sf.jsqlparser.statement.select.PlainSelect;
@SuppressWarnings({"PMD.CyclomaticComplexity"})
public class AlterExpression {
private AlterOperation operation;
private String optionalSpecifier;
private String newTableName;
private String columnName;
private String columnOldName;
// private ColDataType dataType;
private List<ColumnDataType> colDataTypeList;
private List<ColumnDropNotNull> columnDropNotNullList;
private List<ColumnDropDefault> columnDropDefaultList;
private List<String> pkColumns;
private List<String> ukColumns;
private String ukName;
private Index index = null;
private String constraintName;
private boolean usingIfExists;
private Set<ReferentialAction> referentialActions = new LinkedHashSet<>(2);
private List<String> fkColumns;
private String fkSourceSchema;
private String fkSourceTable;
private List<String> fkSourceColumns;
private boolean uk;
private boolean useEqual;
private List<ConstraintState> constraints;
private List<String> parameters;
private String commentText;
private boolean hasColumn = false;
private boolean useBrackets=false;
public boolean hasColumn() {
return hasColumn;
}
public boolean useBrackets() {
return useBrackets;
}
public void useBrackets(boolean useBrackets) {
this.useBrackets = useBrackets;
}
public void hasColumn(boolean hasColumn) {
this.hasColumn = hasColumn;
}
public String getFkSourceSchema() {
return fkSourceSchema;
}
public void setFkSourceSchema(String fkSourceSchema) {
this.fkSourceSchema = fkSourceSchema;
}
public String getCommentText() {
return commentText;
}
public void setCommentText(String commentText) {
this.commentText = commentText;
}
public AlterOperation getOperation() {
return operation;
}
public void setOperation(AlterOperation operation) {
this.operation = operation;
}
public String getOptionalSpecifier() {
return optionalSpecifier;
}
public void setOptionalSpecifier(String optionalSpecifier) {
this.optionalSpecifier = optionalSpecifier;
}
/**
* @param type
* @param action
*/
public void setReferentialAction(Type type, Action action) {
setReferentialAction(type, action, true);
}
public AlterExpression withReferentialAction(Type type, Action action) {
setReferentialAction(type, action);
return this;
}
/** @param type */
public void removeReferentialAction(Type type) {
setReferentialAction(type, null, false);
}
/**
* @param type
* @return
*/
public ReferentialAction getReferentialAction(Type type) {
return referentialActions.stream()
.filter(ra -> type.equals(ra.getType()))
.findFirst()
.orElse(null);
}
private void setReferentialAction(Type type, Action action, boolean set) {
ReferentialAction found = getReferentialAction(type);
if (set) {
if (found == null) {
referentialActions.add(new ReferentialAction(type, action));
} else {
found.setAction(action);
}
} else if (found != null) {
referentialActions.remove(found);
}
}
/**
* @return
* @deprecated use {@link #getReferentialAction(ReferentialAction.Type)}
*/
@Deprecated
public boolean isOnDeleteCascade() {
ReferentialAction found = getReferentialAction(Type.DELETE);
return found != null && Action.CASCADE.equals(found.getAction());
}
/**
* @param onDeleteCascade
* @deprecated use {@link #setReferentialAction(ReferentialAction.Type, ReferentialAction.Action, boolean)}
*/
@Deprecated
public void setOnDeleteCascade(boolean onDeleteCascade) {
setReferentialAction(Type.DELETE, Action.CASCADE, onDeleteCascade);
}
/**
* @return
* @deprecated use {@link #getReferentialAction(ReferentialAction.Type)}
*/
@Deprecated
public boolean isOnDeleteRestrict() {
ReferentialAction found = getReferentialAction(Type.DELETE);
return found != null && Action.RESTRICT.equals(found.getAction());
}
/**
* @param onDeleteRestrict
* @deprecated use {@link #setReferentialAction(ReferentialAction.Type, ReferentialAction.Action, boolean)}
*/
@Deprecated
public void setOnDeleteRestrict(boolean onDeleteRestrict) {
setReferentialAction(Type.DELETE, Action.RESTRICT, onDeleteRestrict);
}
/**
* @return
* @deprecated use {@link #getReferentialAction(ReferentialAction.Type)}
*/
@Deprecated
public boolean isOnDeleteSetNull() {
ReferentialAction found = getReferentialAction(Type.DELETE);
return found != null && Action.SET_NULL.equals(found.getAction());
}
/**
* @param onDeleteSetNull
* @deprecated use {@link #setReferentialAction(ReferentialAction.Type, ReferentialAction.Action, boolean)}
*/
@Deprecated
public void setOnDeleteSetNull(boolean onDeleteSetNull) {
setReferentialAction(Type.DELETE, Action.SET_NULL, onDeleteSetNull);
}
public List<String> getFkColumns() {
return fkColumns;
}
public void setFkColumns(List<String> fkColumns) {
this.fkColumns = fkColumns;
}
public String getFkSourceTable() {
return fkSourceTable;
}
public void setFkSourceTable(String fkSourceTable) {
this.fkSourceTable = fkSourceTable;
}
public List<ColumnDataType> getColDataTypeList() {
return colDataTypeList;
}
public void addColDataType(String columnName, ColDataType colDataType) {
addColDataType(new ColumnDataType(columnName, false, colDataType, null));
}
public void addColDataType(ColumnDataType columnDataType) {
if (colDataTypeList == null) {
colDataTypeList = new ArrayList<>();
}
colDataTypeList.add(columnDataType);
}
public void addColDropNotNull(ColumnDropNotNull columnDropNotNull) {
if (columnDropNotNullList == null) {
columnDropNotNullList = new ArrayList<>();
}
columnDropNotNullList.add(columnDropNotNull);
}
public void addColDropDefault(ColumnDropDefault columnDropDefault) {
if (columnDropDefaultList == null) {
columnDropDefaultList = new ArrayList<>();
}
columnDropDefaultList.add(columnDropDefault);
}
public List<String> getFkSourceColumns() {
return fkSourceColumns;
}
public void setFkSourceColumns(List<String> fkSourceColumns) {
this.fkSourceColumns = fkSourceColumns;
}
public String getNewTableName() {
return newTableName;
}
public void setNewTableName(String newTableName) {
this.newTableName = newTableName;
}
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
@Deprecated
public String getColOldName() {
return getColumnOldName();
}
@Deprecated
public void setColOldName(String columnOldName) {
setColumnOldName(columnOldName);
}
public String getColumnOldName() {
return columnOldName;
}
public void setColumnOldName(String columnOldName) {
this.columnOldName = columnOldName;
}
public String getConstraintName() {
return this.constraintName;
}
public void setConstraintName(final String constraintName) {
this.constraintName = constraintName;
}
public boolean isUsingIfExists() {
return usingIfExists;
}
public void setUsingIfExists(boolean usingIfExists) {
this.usingIfExists = usingIfExists;
}
public List<String> getPkColumns() {
return pkColumns;
}
public void setPkColumns(List<String> pkColumns) {
this.pkColumns = pkColumns;
}
public List<String> getUkColumns() {
return ukColumns;
}
public void setUkColumns(List<String> ukColumns) {
this.ukColumns = ukColumns;
}
public String getUkName() {
return ukName;
}
public void setUkName(String ukName) {
this.ukName = ukName;
}
public Index getIndex() {
return index;
}
public void setIndex(Index index) {
this.index = index;
}
public List<ConstraintState> getConstraints() {
return constraints;
}
public void setConstraints(List<ConstraintState> constraints) {
this.constraints = constraints;
}
public List<ColumnDropNotNull> getColumnDropNotNullList() {
return columnDropNotNullList;
}
public void addParameters(String... params) {
if (parameters == null) {
parameters = new ArrayList<>();
}
parameters.addAll(Arrays.asList(params));
}
public List<String> getParameters() {
return parameters;
}
public boolean getUseEqual() {
return useEqual;
}
public void setUseEqual(boolean useEqual) {
this.useEqual = useEqual;
}
public boolean getUk() {
return uk;
}
public void setUk(boolean uk) {
this.uk = uk;
}
@Override
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity", "PMD.ExcessiveMethodLength"})
public String toString() {
StringBuilder b = new StringBuilder();
if (operation== AlterOperation.UNSPECIFIC) {
b.append(optionalSpecifier);
} else if (operation== AlterOperation.RENAME_TABLE) {
b.append("RENAME TO ").append(newTableName);
} else if (operation== AlterOperation.DROP_PRIMARY_KEY) {
b.append("DROP PRIMARY KEY ");
} else if (operation== AlterOperation.DROP_UNIQUE) {
b.append("DROP UNIQUE (").append(PlainSelect.getStringList(pkColumns)).append(')');
} else if (operation== AlterOperation.DROP_FOREIGN_KEY) {
b.append("DROP FOREIGN KEY (").append(PlainSelect.getStringList(pkColumns)).append(')');
} else if (operation== AlterOperation.DROP && columnName==null && pkColumns!=null && pkColumns.size()>0) {
// Oracle Multi Column Drop
b.append("DROP (").append(PlainSelect.getStringList(pkColumns)).append(')');
} else {
b.append(operation).append(" ");
if (commentText != null) {
if (columnName != null) {
b.append(columnName).append(" COMMENT ");
}
b.append(commentText);
} else if (columnName != null) {
if (hasColumn) {
b.append("COLUMN ");
}
if (usingIfExists) {
b.append("IF EXISTS ");
}
if (operation == AlterOperation.RENAME) {
b.append(columnOldName).append(" TO ");
}
b.append(columnName);
} else if (getColDataTypeList() != null) {
if (operation == AlterOperation.CHANGE) {
if (optionalSpecifier != null) {
b.append(optionalSpecifier).append(" ");
}
b.append(columnOldName).append(" ");
} else if (colDataTypeList.size() > 1) {
b.append("(");
} else {
if (hasColumn) {
b.append("COLUMN ");
}
}
if (useBrackets && colDataTypeList.size() == 1){
b.append(" ( ");
}
b.append(PlainSelect.getStringList(colDataTypeList));
if (useBrackets && colDataTypeList.size() == 1 ){
b.append(" ) ");
}
if (colDataTypeList.size() > 1) {
b.append(")");
}
} else if (getColumnDropNotNullList() != null) {
b.append("COLUMN ");
b.append(PlainSelect.getStringList(columnDropNotNullList));
} else if ( columnDropDefaultList != null && !columnDropDefaultList.isEmpty() ) {
b.append("COLUMN ");
b.append(PlainSelect.getStringList(columnDropDefaultList));
} else if (constraintName != null) {
b.append("CONSTRAINT ");
if (usingIfExists) {
b.append("IF EXISTS ");
}
b.append(constraintName);
} else if (pkColumns != null) {
b.append("PRIMARY KEY (").append(PlainSelect.getStringList(pkColumns)).append(')');
} else if (ukColumns != null) {
b.append("UNIQUE");
if (ukName != null) {
if (getUk()) {
b.append(" KEY ");
} else {
b.append(" INDEX ");
}
b.append(ukName);
}
b.append(" (").append(PlainSelect.getStringList(ukColumns)).append(")");
} else if (fkColumns != null) {
b.append("FOREIGN KEY (")
.append(PlainSelect.getStringList(fkColumns))
.append(") REFERENCES ")
.append(
fkSourceSchema != null && fkSourceSchema.trim().length() > 0
? fkSourceSchema + "."
: "")
.append(fkSourceTable)
.append(" (")
.append(PlainSelect.getStringList(fkSourceColumns))
.append(")");
referentialActions.forEach(b::append);
} else if (index != null) {
b.append(index);
}
if (getConstraints() != null && !getConstraints().isEmpty()) {
b.append(' ').append(PlainSelect.getStringList(constraints, false, false));
}
if (getUseEqual()) {
b.append('=');
}
}
if (parameters != null && !parameters.isEmpty()) {
b.append(' ').append(PlainSelect.getStringList(parameters, false, false));
}
return b.toString();
}
public AlterExpression withOperation(AlterOperation operation) {
this.setOperation(operation);
return this;
}
public AlterExpression withOptionalSpecifier(String optionalSpecifier) {
this.setOptionalSpecifier(optionalSpecifier);
return this;
}
public AlterExpression withColumnName(String columnName) {
this.setColumnName(columnName);
return this;
}
public AlterExpression withPkColumns(List<String> pkColumns) {
this.setPkColumns(pkColumns);
return this;
}
public AlterExpression withUkColumns(List<String> ukColumns) {
this.setUkColumns(ukColumns);
return this;
}
public AlterExpression withUkName(String ukName) {
this.setUkName(ukName);
return this;
}
public AlterExpression withIndex(Index index) {
this.setIndex(index);
return this;
}
public AlterExpression withConstraintName(String constraintName) {
this.setConstraintName(constraintName);
return this;
}
public AlterExpression withUsingIfExists(boolean usingIfExists) {
this.setUsingIfExists(usingIfExists);
return this;
}
public AlterExpression withOnDeleteRestrict(boolean onDeleteRestrict) {
this.setOnDeleteRestrict(onDeleteRestrict);
return this;
}
public AlterExpression withOnDeleteSetNull(boolean onDeleteSetNull) {
this.setOnDeleteSetNull(onDeleteSetNull);
return this;
}
public AlterExpression withOnDeleteCascade(boolean onDeleteCascade) {
this.setOnDeleteCascade(onDeleteCascade);
return this;
}
public AlterExpression withFkColumns(List<String> fkColumns) {
this.setFkColumns(fkColumns);
return this;
}
public AlterExpression withFkSourceSchema(String fkSourceSchema) {
this.setFkSourceTable(fkSourceSchema);
return this;
}
public AlterExpression withFkSourceTable(String fkSourceTable) {
this.setFkSourceTable(fkSourceTable);
return this;
}
public AlterExpression withFkSourceColumns(List<String> fkSourceColumns) {
this.setFkSourceColumns(fkSourceColumns);
return this;
}
public AlterExpression withUk(boolean uk) {
this.setUk(uk);
return this;
}
public AlterExpression withUseEqual(boolean useEqual) {
this.setUseEqual(useEqual);
return this;
}
public AlterExpression withConstraints(List<ConstraintState> constraints) {
this.setConstraints(constraints);
return this;
}
public AlterExpression withCommentText(String commentText) {
this.setCommentText(commentText);
return this;
}
public AlterExpression withColumnOldName(String columnOldName) {
setColumnOldName(columnOldName);
return this;
}
public AlterExpression addPkColumns(String... pkColumns) {
List<String> collection = Optional.ofNullable(getPkColumns()).orElseGet(ArrayList::new);
Collections.addAll(collection, pkColumns);
return this.withPkColumns(collection);
}
public AlterExpression addPkColumns(Collection<String> pkColumns) {
List<String> collection = Optional.ofNullable(getPkColumns()).orElseGet(ArrayList::new);
collection.addAll(pkColumns);
return this.withPkColumns(collection);
}
public AlterExpression addUkColumns(String... ukColumns) {
List<String> collection = Optional.ofNullable(getUkColumns()).orElseGet(ArrayList::new);
Collections.addAll(collection, ukColumns);
return this.withUkColumns(collection);
}
public AlterExpression addUkColumns(Collection<String> ukColumns) {
List<String> collection = Optional.ofNullable(getUkColumns()).orElseGet(ArrayList::new);
collection.addAll(ukColumns);
return this.withUkColumns(collection);
}
public AlterExpression addFkColumns(String... fkColumns) {
List<String> collection = Optional.ofNullable(getFkColumns()).orElseGet(ArrayList::new);
Collections.addAll(collection, fkColumns);
return this.withFkColumns(collection);
}
public AlterExpression addFkColumns(Collection<String> fkColumns) {
List<String> collection = Optional.ofNullable(getFkColumns()).orElseGet(ArrayList::new);
collection.addAll(fkColumns);
return this.withFkColumns(collection);
}
public AlterExpression addFkSourceColumns(String... fkSourceColumns) {
List<String> collection = Optional.ofNullable(getFkSourceColumns()).orElseGet(ArrayList::new);
Collections.addAll(collection, fkSourceColumns);
return this.withFkSourceColumns(collection);
}
public AlterExpression addFkSourceColumns(Collection<String> fkSourceColumns) {
List<String> collection = Optional.ofNullable(getFkSourceColumns()).orElseGet(ArrayList::new);
collection.addAll(fkSourceColumns);
return this.withFkSourceColumns(collection);
}
public AlterExpression addConstraints(ConstraintState... constraints) {
List<ConstraintState> collection =
Optional.ofNullable(getConstraints()).orElseGet(ArrayList::new);
Collections.addAll(collection, constraints);
return this.withConstraints(collection);
}
public AlterExpression addConstraints(Collection<? extends ConstraintState> constraints) {
List<ConstraintState> collection =
Optional.ofNullable(getConstraints()).orElseGet(ArrayList::new);
collection.addAll(constraints);
return this.withConstraints(collection);
}
public static final class ColumnDataType extends ColumnDefinition {
private final boolean withType;
public ColumnDataType(boolean withType) {
super();
this.withType = withType;
}
public ColumnDataType(
String columnName, boolean withType, ColDataType colDataType, List<String> columnSpecs) {
super(columnName, colDataType, columnSpecs);
this.withType = withType;
}
@Override
public String toString() {
return getColumnName() + (withType ? " TYPE " : " ") + toStringDataTypeAndSpec();
}
@Override
public ColumnDataType withColDataType(ColDataType colDataType) {
return (ColumnDataType) super.withColDataType(colDataType);
}
@Override
public ColumnDataType withColumnName(String columnName) {
return (ColumnDataType) super.withColumnName(columnName);
}
@Override
public ColumnDataType addColumnSpecs(String... columnSpecs) {
return (ColumnDataType) super.addColumnSpecs(columnSpecs);
}
@Override
public ColumnDataType addColumnSpecs(Collection<String> columnSpecs) {
return (ColumnDataType) super.addColumnSpecs(columnSpecs);
}
@Override
public ColumnDataType withColumnSpecs(List<String> columnSpecs) {
return (ColumnDataType) super.withColumnSpecs(columnSpecs);
}
}
public static final class ColumnDropNotNull {
private final String columnName;
private final boolean withNot;
public ColumnDropNotNull(String columnName) {
this(columnName, false);
}
public ColumnDropNotNull(String columnName, boolean withNot) {
this.columnName = columnName;
this.withNot = withNot;
}
public String getColumnName() {
return columnName;
}
public boolean isWithNot() {
return withNot;
}
@Override
public String toString() {
return columnName + " DROP" + (withNot ? " NOT " : " ") + "NULL";
}
}
public static final class ColumnDropDefault {
private final String columnName;
public ColumnDropDefault(String columnName) {
this.columnName = columnName;
}
public String getColumnName() {
return columnName;
}
@Override
public String toString() {
return columnName + " DROP DEFAULT";
}
}
}