/
ColumnConfig.java
963 lines (816 loc) · 35.8 KB
/
ColumnConfig.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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
package liquibase.change;
import liquibase.exception.DateParseException;
import liquibase.parser.core.ParsedNode;
import liquibase.parser.core.ParsedNodeException;
import liquibase.resource.ResourceAccessor;
import liquibase.serializer.AbstractLiquibaseSerializable;
import liquibase.serializer.ReflectionSerializer;
import liquibase.statement.DatabaseFunction;
import liquibase.statement.NotNullConstraint;
import liquibase.statement.SequenceCurrentValueFunction;
import liquibase.statement.SequenceNextValueFunction;
import liquibase.structure.core.Column;
import liquibase.structure.core.ForeignKey;
import liquibase.structure.core.PrimaryKey;
import liquibase.structure.core.Table;
import liquibase.structure.core.UniqueConstraint;
import liquibase.util.*;
import java.math.BigInteger;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
* The standard configuration used by Change classes to represent a column.
* It is not required that a column-based Change uses this class, but parsers should look for it so it is a helpful
* convenience. The definitions of "defaultValue" and "value" will vary based on the Change and may not be applicable
* in all cases.
*/
public class ColumnConfig extends AbstractLiquibaseSerializable {
private String name;
private Boolean computed;
private String type;
private String value;
private Number valueNumeric;
private Date valueDate;
private Boolean valueBoolean;
private String valueBlobFile;
private String valueClobFile;
private String encoding;
private DatabaseFunction valueComputed;
private SequenceNextValueFunction valueSequenceNext;
private SequenceCurrentValueFunction valueSequenceCurrent;
private String defaultValue;
private Number defaultValueNumeric;
private Date defaultValueDate;
private Boolean defaultValueBoolean;
private DatabaseFunction defaultValueComputed;
private SequenceNextValueFunction defaultValueSequenceNext;
private String defaultValueConstraintName;
private ConstraintsConfig constraints;
private Boolean autoIncrement;
private String generationType;
private Boolean defaultOnNull;
private BigInteger startWith;
private BigInteger incrementBy;
private String remarks;
private Boolean descending;
/**
* Create a ColumnConfig object based on a {@link Column} snapshot.
* It will attempt to set as much as possible based on the information in the snapshot.
*/
public ColumnConfig(Column columnSnapshot) {
setName(columnSnapshot.getName());
setComputed(BooleanUtils.isTrue(columnSnapshot.getComputed()) ? Boolean.TRUE : null);
setDescending(BooleanUtils.isTrue(columnSnapshot.getDescending()) ? Boolean.TRUE : null);
if (columnSnapshot.getType() != null) {
setType(columnSnapshot.getType().toString());
}
if (columnSnapshot.getDefaultValue() != null) {
Object defaultValue = columnSnapshot.getDefaultValue();
if (defaultValue instanceof Boolean) {
setDefaultValueBoolean((Boolean) defaultValue);
} else if (defaultValue instanceof Number) {
setDefaultValueNumeric(defaultValue.toString());
} else if (defaultValue instanceof SequenceNextValueFunction) {
setDefaultValueSequenceNext((SequenceNextValueFunction) defaultValue);
} else if (defaultValue instanceof DatabaseFunction) {
setDefaultValueComputed((DatabaseFunction) defaultValue);
} else if (defaultValue instanceof Date) {
setDefaultValueDate((Date) defaultValue);
} else {
setDefaultValue(defaultValue.toString());
}
}
setDefaultValueConstraintName(columnSnapshot.getDefaultValueConstraintName());
boolean nonDefaultConstraints = false;
ConstraintsConfig constraints = new ConstraintsConfig();
if ((columnSnapshot.isNullable() != null) && !columnSnapshot.isNullable()) {
constraints.setNullable(columnSnapshot.isNullable());
constraints.setShouldValidateNullable(columnSnapshot.shouldValidateNullable());
nonDefaultConstraints = true;
}
if ((columnSnapshot.getRelation() != null) && (columnSnapshot.getRelation() instanceof Table)) {
Table table = (Table) columnSnapshot.getRelation();
List<NotNullConstraint> notNullConstraints = table.getNotNullConstraints();
if (notNullConstraints != null) {
for (NotNullConstraint constraint : notNullConstraints) {
if (constraint.getColumnName().equals(getName())) {
constraints.setNullable(false);
constraints.setNotNullConstraintName(constraint.getConstraintName());
nonDefaultConstraints = true;
}
}
}
if (columnSnapshot.isAutoIncrement()) {
setAutoIncrement(true);
setStartWith(columnSnapshot.getAutoIncrementInformation().getStartWith());
setIncrementBy(columnSnapshot.getAutoIncrementInformation().getIncrementBy());
} else {
setAutoIncrement(false);
}
PrimaryKey primaryKey = table.getPrimaryKey();
if ((primaryKey != null) && primaryKey.getColumnNamesAsList().contains(columnSnapshot.getName())) {
constraints.setPrimaryKey(true);
constraints.setPrimaryKeyName(primaryKey.getName());
constraints.setPrimaryKeyTablespace(primaryKey.getTablespace());
nonDefaultConstraints = true;
}
List<UniqueConstraint> uniqueConstraints = table.getUniqueConstraints();
if (uniqueConstraints != null) {
for (UniqueConstraint constraint : uniqueConstraints) {
if (constraint.getColumnNames().contains(getName())) {
constraints.setUnique(true);
constraints.setUniqueConstraintName(constraint.getName());
nonDefaultConstraints = true;
}
}
}
List<ForeignKey> fks = table.getOutgoingForeignKeys();
if (fks != null) {
for (ForeignKey fk : fks) {
if ((fk.getForeignKeyColumns() != null) && (fk.getForeignKeyColumns().size() == 1) && fk
.getForeignKeyColumns().get(0).getName().equals(getName())) {
constraints.setForeignKeyName(fk.getName());
constraints.setReferences(fk.getPrimaryKeyTable().getName() +
"(" +
fk.getPrimaryKeyColumns().get(0).getName() +
")");
nonDefaultConstraints = true;
}
}
}
if (nonDefaultConstraints) {
setConstraints(constraints);
}
}
setRemarks(columnSnapshot.getRemarks());
}
/**
* Create am empty ColumnConfig object. Boolean and other object values will default to null.
*/
public ColumnConfig() {
}
public static ColumnConfig fromName(String name) {
name = name.trim();
Boolean descending = null;
if (name.matches("(?i).*\\s+DESC")) {
name = name.replaceFirst("(?i)\\s+DESC$", "");
descending = true;
} else if (name.matches("(?i).*\\s+ASC")) {
name = name.replaceFirst("(?i)\\s+ASC$", "");
descending = false;
}
return new ColumnConfig()
.setName(name)
.setDescending(descending);
}
public static ColumnConfig[] arrayFromNames(String names) {
if (names == null) {
return null;
}
List<String> nameArray = StringUtils.splitAndTrim(names, ",");
ColumnConfig[] returnArray = new ColumnConfig[nameArray.size()];
for (int i = 0; i < nameArray.size(); i++) {
returnArray[i] = fromName(nameArray.get(i));
}
return returnArray;
}
/**
* The name of the column.
*/
public String getName() {
return name;
}
public ColumnConfig setName(String name) {
this.name = name;
return this;
}
public ColumnConfig setName(String name, boolean computed) {
setComputed(computed);
return setName(name);
}
public Boolean getComputed() {
return computed;
}
public ColumnConfig setComputed(Boolean computed) {
this.computed = computed;
return this;
}
/**
* The data type fof the column.
* This value will pass through {@link liquibase.datatype.DataTypeFactory#fromDescription(String, liquibase.database.Database)} before being included in SQL.
*/
public String getType() {
return type;
}
public ColumnConfig setType(String type) {
this.type = type;
return this;
}
/**
* The String value to set this column to. If you do not want the value set by {@link #setValue(String)}
* use a more specific function like {@link #getValueNumeric()} or the more generic {@link #getValueObject()}
* <p></p>
* If performing an data manipulation operation, the setValue* functions should be used to set what the columns
* should be set to. If performing a data definition operation, this setValue* functions should be used to set
* what existing rows should be set to (may be different than the default value for new rows)
*/
public String getValue() {
return value;
}
/**
* Sets the string value this column should be set to.
* If you are trying to set a value type other than a string, use the more specific functions like {@link #setValueNumeric(Number)}.
* This method does no processing of the string. Any trimming is expected to be done beforehand. It does not conver the string "null" to null
* so that you can set the string "null" as a value if you are feeling particularly crazy.
*/
public ColumnConfig setValue(String value) {
this.value = value;
return this;
}
/**
* Return the numeric value this column should be set to.
* @see #setValue(String)
*/
public Number getValueNumeric() {
return valueNumeric;
}
public ColumnConfig setValueNumeric(Number valueNumeric) {
this.valueNumeric = valueNumeric;
return this;
}
/**
* Set the number this column should be set to. Supports integers and decimals, and strips off any wrapping
* parentheses. If the passed value cannot be parsed as a number in US locale, it is assumed to be a function
* that returns a number. If the value "null" is passed, it will set a null value.
*/
public ColumnConfig setValueNumeric(String valueNumeric) {
if ((valueNumeric == null) || "null".equalsIgnoreCase(valueNumeric)) {
this.valueNumeric = null;
} else {
String saved = valueNumeric;
if (valueNumeric.startsWith("(")) {
valueNumeric = valueNumeric.replaceFirst("^\\(", "");
valueNumeric = valueNumeric.replaceFirst("\\)$", "");
}
try {
this.valueNumeric = ValueNumeric.of(Locale.US, valueNumeric);
} catch (ParseException e) {
this.valueComputed = new DatabaseFunction(saved);
}
}
return this;
}
/**
* Return the boolean value this column should be set to.
* @see #setValue(String)
*/
public Boolean getValueBoolean() {
return valueBoolean;
}
/**
* Set the valueBoolean based on a given string.
* If the passed value cannot be parsed as a date, it is assumed to be a function that returns a boolean.
* If the string "null" or an empty string is passed, it will set a null value.
* If "1" is passed, defaultValueBoolean is set to true. If 0 is passed, defaultValueBoolean is set to false
*/
public ColumnConfig setValueBoolean(String valueBoolean) {
valueBoolean = StringUtils.trimToNull(valueBoolean);
if ((valueBoolean == null) || "null".equalsIgnoreCase(valueBoolean)) {
this.valueBoolean = null;
} else {
if ("true".equalsIgnoreCase(valueBoolean) || "1".equals(valueBoolean)) {
this.valueBoolean = true;
} else if ("false".equalsIgnoreCase(valueBoolean) || "0".equals(valueBoolean)) {
this.valueBoolean = false;
} else {
this.valueComputed = new DatabaseFunction(valueBoolean);
}
}
return this;
}
public ColumnConfig setValueBoolean(Boolean valueBoolean) {
this.valueBoolean = valueBoolean;
return this;
}
/**
* Return the function this column should be set from.
* @see #setValue(String)
*/
public DatabaseFunction getValueComputed() {
return valueComputed;
}
public ColumnConfig setValueComputed(DatabaseFunction valueComputed) {
this.valueComputed = valueComputed;
return this;
}
public SequenceNextValueFunction getValueSequenceNext() {
return valueSequenceNext;
}
public ColumnConfig setValueSequenceNext(SequenceNextValueFunction valueSequenceNext) {
this.valueSequenceNext = valueSequenceNext;
return this;
}
public SequenceCurrentValueFunction getValueSequenceCurrent() {
return valueSequenceCurrent;
}
public ColumnConfig setValueSequenceCurrent(SequenceCurrentValueFunction valueSequenceCurrent) {
this.valueSequenceCurrent = valueSequenceCurrent;
return this;
}
/**
* Return the date value this column should be set to.
* @see #setValue(String)
*/
public Date getValueDate() {
return valueDate;
}
/**
* Set the date this column should be set to. Supports any of the date or datetime formats handled by {@link ISODateFormat}.
* If the passed value cannot be parsed as a date, it is assumed to be a function that returns a date.
* If the string "null" is passed, it will set a null value.
* @param valueDate the Date Value to use (may be null or "null", or start with "now" or "today".
* @throws DateParseException if the columnType isn't supported for "now" or "today" values.
*/
public ColumnConfig setValueDate(String valueDate) throws DateParseException {
if ((valueDate == null) || "null".equalsIgnoreCase(valueDate)) {
this.valueDate = null;
} else if (NowAndTodayUtil.isNowOrTodayFormat(valueDate)) {
this.valueDate = NowAndTodayUtil.doNowOrToday(valueDate, this.getType());
} else {
try {
this.valueDate = new ISODateFormat().parse(valueDate);
} catch (ParseException e) {
//probably a function
this.valueComputed = new DatabaseFunction(valueDate);
}
}
return this;
}
public ColumnConfig setValueDate(Date valueDate) {
this.valueDate = valueDate;
return this;
}
/**
* Return the file containing the data to load into a BLOB.
* @see #setValue(String)
*/
public String getValueBlobFile() {
return valueBlobFile;
}
public ColumnConfig setValueBlobFile(String valueBlobFile) {
this.valueBlobFile = valueBlobFile;
return this;
}
/**
* Return the file containing the data to load into a CLOB.
* @see #setValue(String)
*/
public String getValueClobFile() {
return valueClobFile;
}
public ColumnConfig setValueClobFile(String valueClobFile) {
this.valueClobFile = valueClobFile;
return this;
}
/**
* Return encoding of a file, referenced via {@link #valueClobFile}.
*/
public String getEncoding() {
return encoding;
}
public ColumnConfig setEncoding(String encoding) {
this.encoding = encoding;
return this;
}
/**
* Return the value from whatever setValue* function was called. Will return null if none were set.
*/
public Object getValueObject() {
if (getValue() != null) {
return getValue();
} else if (getValueBoolean() != null) {
return getValueBoolean();
} else if (getValueNumeric() != null) {
return getValueNumeric();
} else if (getValueDate() != null) {
return getValueDate();
} else if (getValueComputed() != null) {
return getValueComputed();
} else if (getValueClobFile() != null) {
return getValueClobFile();
} else if (getValueBlobFile() != null) {
return getValueBlobFile();
} else if (getValueSequenceNext() != null) {
return getValueSequenceNext();
} else if (getValueSequenceCurrent() != null) {
return getValueSequenceCurrent();
}
return null;
}
/**
* The String default value to assign to this column. If you do not want the default set by {@link #setDefaultValue(String)}
* use a more specific function like {@link #getDefaultValueNumeric()} or the more generic {@link #getDefaultValueObject()}
*/
public String getDefaultValue() {
return defaultValue;
}
/**
* Sets the string default value to assign to this column. If you are trying to set a default value type other than a string, use the more specific functions
* like {@link #setDefaultValueNumeric(Number)}.
* This method does no processing of the string. Any trimming is expected to be done beforehand. It does not convert the string "null" to null
* so that you can set the string "null" as a value if you are feeling particularly crazy.
*/
public ColumnConfig setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
return this;
}
/**
* Return the numeric value this column should default to.
* @see #setDefaultValue(String)
*/
public Number getDefaultValueNumeric() {
return defaultValueNumeric;
}
/**
* Set the number this column should use as its default. Supports integers and decimals, and strips off any wrapping parentheses.
* If the passed value cannot be parsed as a number, it is assumed to be a function that returns a number.
* If the value "null" is passed, it will set a null value.
* <p></p>
* A special case is made for "GENERATED_BY_DEFAULT" which simply sets the ColumnConfig object to autoIncrement.
*/
public ColumnConfig setDefaultValueNumeric(String defaultValueNumeric) {
if ((defaultValueNumeric == null) || "null".equalsIgnoreCase(defaultValueNumeric)) {
this.defaultValueNumeric = null;
} else {
if ("GENERATED_BY_DEFAULT".equals(defaultValueNumeric)) {
setAutoIncrement(true);
} else {
if (defaultValueNumeric.startsWith("(")) {
defaultValueNumeric = defaultValueNumeric.replaceFirst("^\\(", "");
defaultValueNumeric = defaultValueNumeric.replaceFirst("\\)$", "");
}
try {
this.defaultValueNumeric = ValueNumeric.of(Locale.US, defaultValueNumeric);
} catch (ParseException e) {
this.defaultValueComputed = new DatabaseFunction(defaultValueNumeric);
}
}
}
return this;
}
public ColumnConfig setDefaultValueNumeric(Number defaultValueNumeric) {
this.defaultValueNumeric = defaultValueNumeric;
return this;
}
/**
* Return the date value this column should default to.
* @see #setDefaultValue(String)
*/
public Date getDefaultValueDate() {
return defaultValueDate;
}
public ColumnConfig setDefaultValueDate(Date defaultValueDate) {
this.defaultValueDate = defaultValueDate;
return this;
}
/**
* Set the date this column should default to. Supports any of the date or datetime formats handled by {@link ISODateFormat}.
* If the passed value cannot be parsed as a date, it is assumed to be a function that returns a date.
* If the string "null" or an empty string is passed, it will set a null value.
*/
public ColumnConfig setDefaultValueDate(String defaultValueDate) {
defaultValueDate = StringUtils.trimToNull(defaultValueDate);
if ((defaultValueDate == null) || "null".equalsIgnoreCase(defaultValueDate)) {
this.defaultValueDate = null;
} else {
try {
this.defaultValueDate = new ISODateFormat().parse(defaultValueDate);
} catch (ParseException e) {
//probably a computed date
this.defaultValueComputed = new DatabaseFunction(defaultValueDate);
}
}
return this;
}
/**
* Return the boolean value this column should default to.
* @see #setDefaultValue(String)
*/
public Boolean getDefaultValueBoolean() {
return defaultValueBoolean;
}
/**
* Set the defaultValueBoolean based on a given string.
* If the passed value cannot be parsed as a date, it is assumed to be a function that returns a boolean.
* If the string "null" or an empty string is passed, it will set a null value.
* If "1" is passed, defaultValueBoolean is set to true. If 0 is passed, defaultValueBoolean is set to false
*/
public ColumnConfig setDefaultValueBoolean(String defaultValueBoolean) {
defaultValueBoolean = StringUtils.trimToNull(defaultValueBoolean);
if ((defaultValueBoolean == null) || "null".equalsIgnoreCase(defaultValueBoolean)) {
this.defaultValueBoolean = null;
} else {
if ("true".equalsIgnoreCase(defaultValueBoolean) || "1".equals(defaultValueBoolean)) {
this.defaultValueBoolean = true;
} else if ("false".equalsIgnoreCase(defaultValueBoolean) || "0".equals(defaultValueBoolean)) {
this.defaultValueBoolean = false;
} else {
this.defaultValueComputed = new DatabaseFunction(defaultValueBoolean);
}
}
return this;
}
public ColumnConfig setDefaultValueBoolean(Boolean defaultValueBoolean) {
this.defaultValueBoolean = defaultValueBoolean;
return this;
}
/**
* Return the function whose value should generate this column's default.
* @see #setDefaultValue(String)
*/
public DatabaseFunction getDefaultValueComputed() {
return defaultValueComputed;
}
public ColumnConfig setDefaultValueComputed(DatabaseFunction defaultValueComputed) {
this.defaultValueComputed = defaultValueComputed;
return this;
}
/**
* Return the value to set this column's default to according to the setDefaultValue* function that was called.
* If none were called, this function returns null.
*/
public Object getDefaultValueObject() {
if (getDefaultValue() != null) {
return getDefaultValue();
} else if (getDefaultValueBoolean() != null) {
return getDefaultValueBoolean();
} else if (getDefaultValueNumeric() != null) {
return getDefaultValueNumeric();
} else if (getDefaultValueDate() != null) {
return getDefaultValueDate();
} else if (getDefaultValueComputed() != null) {
return getDefaultValueComputed();
} else if (getDefaultValueSequenceNext() != null) {
return getDefaultValueSequenceNext();
}
return null;
}
/**
* Returns the ConstraintsConfig this ColumnConfig is using. Returns null if nho constraints have been assigned yet.
*/
public ConstraintsConfig getConstraints() {
return constraints;
}
public ColumnConfig setConstraints(ConstraintsConfig constraints) {
this.constraints = constraints;
return this;
}
/**
* Returns true if this Column should be set to be auto increment. Returns null if auto-increment hasn't been explicitly assigned.
*/
public Boolean isAutoIncrement() {
return autoIncrement;
}
public ColumnConfig setAutoIncrement(Boolean autoIncrement) {
this.autoIncrement = autoIncrement;
return this;
}
/**
* Return the number to start auto incrementing with.
*/
public BigInteger getStartWith() {
return startWith;
}
public ColumnConfig setStartWith(BigInteger startWith) {
this.startWith = startWith;
return this;
}
/**
* Return the amount to auto increment by.
*/
public BigInteger getIncrementBy() {
return incrementBy;
}
public ColumnConfig setIncrementBy(BigInteger incrementBy) {
this.incrementBy = incrementBy;
return this;
}
/**
* Returns true if any of the setDefaultValue* functions have had a non-null value set
*/
public boolean hasDefaultValue() {
return (this.getDefaultValue() != null) || (this.getDefaultValueBoolean() != null) || (this
.getDefaultValueDate() != null) || (this.getDefaultValueNumeric() != null) || (this
.getDefaultValueComputed() != null) || (this.getDefaultValueSequenceNext() != null);
}
/**
* Return the remarks to apply to this column.
*/
public String getRemarks() {
return remarks;
}
public ColumnConfig setRemarks(String remarks) {
this.remarks = remarks;
return this;
}
public Boolean getDescending() {
return descending;
}
public ColumnConfig setDescending(Boolean descending) {
this.descending = descending;
return this;
}
public Boolean getDefaultOnNull() {
return defaultOnNull;
}
public ColumnConfig setDefaultOnNull(Boolean defaultOnNull) {
this.defaultOnNull = defaultOnNull;
return this;
}
public String getGenerationType() {
return generationType;
}
public ColumnConfig setGenerationType(String generationType) {
this.generationType = generationType;
return this;
}
@Override
public String getSerializedObjectName() {
return "column";
}
public SequenceNextValueFunction getDefaultValueSequenceNext() {
return defaultValueSequenceNext;
}
public ColumnConfig setDefaultValueSequenceNext(SequenceNextValueFunction defaultValueSequenceNext) {
this.defaultValueSequenceNext = defaultValueSequenceNext;
return this;
}
public String getDefaultValueConstraintName() {
return defaultValueConstraintName;
}
public void setDefaultValueConstraintName(String defaultValueConstraintName) {
this.defaultValueConstraintName = defaultValueConstraintName;
}
@Override
public SerializationType getSerializableFieldType(String field) {
return SerializationType.NAMED_FIELD;
}
@Override
public String getSerializedObjectNamespace() {
return STANDARD_CHANGELOG_NAMESPACE;
}
@Override
public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
for (ParsedNode child : parsedNode.getChildren()) {
if (!ObjectUtil.hasProperty(this, child.getName())) {
throw new ParsedNodeException("Unexpected node: "+child.getName());
}
}
name = parsedNode.getChildValue(null, "name", String.class);
computed = parsedNode.getChildValue(null, "computed", Boolean.class);
type = parsedNode.getChildValue(null, "type", String.class);
encoding = parsedNode.getChildValue(null, "encoding", String.class);
autoIncrement = parsedNode.getChildValue(null, "autoIncrement", Boolean.class);
startWith = parsedNode.getChildValue(null, "startWith", BigInteger.class);
incrementBy = parsedNode.getChildValue(null, "incrementBy", BigInteger.class);
remarks = parsedNode.getChildValue(null, "remarks", String.class);
descending = parsedNode.getChildValue(null, "descending", Boolean.class);
value = parsedNode.getChildValue(null, "value", String.class);
if (value == null) {
value = StringUtils.trimToNull((String) parsedNode.getValue());
}
setValueNumeric(parsedNode.getChildValue(null, "valueNumeric", String.class));
try {
valueDate = parsedNode.getChildValue(null, "valueDate", Date.class);
} catch (ParsedNodeException e) {
valueComputed = new DatabaseFunction(parsedNode.getChildValue(null, "valueDate", String.class));
}
valueBoolean = parsedNode.getChildValue(null, "valueBoolean", Boolean.class);
valueBlobFile = parsedNode.getChildValue(null, "valueBlobFile", String.class);
valueClobFile = parsedNode.getChildValue(null, "valueClobFile", String.class);
String valueComputedString = parsedNode.getChildValue(null, "valueComputed", String.class);
if (valueComputedString != null) {
valueComputed = new DatabaseFunction(valueComputedString);
}
String valueSequenceNextString = parsedNode.getChildValue(null, "valueSequenceNext", String.class);
if (valueSequenceNextString != null) {
valueSequenceNext = new SequenceNextValueFunction(valueSequenceNextString);
}
String valueSequenceCurrentString = parsedNode.getChildValue(null, "valueSequenceCurrent", String.class);
if (valueSequenceCurrentString != null) {
valueSequenceCurrent = new SequenceCurrentValueFunction(valueSequenceCurrentString);
}
defaultValueConstraintName = parsedNode.getChildValue(null, "defaultValueConstraintName", String.class);
defaultValue = parsedNode.getChildValue(null, "defaultValue", String.class);
setDefaultValueNumeric(parsedNode.getChildValue(null, "defaultValueNumeric", String.class));
try {
defaultValueDate = parsedNode.getChildValue(null, "defaultValueDate", Date.class);
} catch (ParsedNodeException e) {
defaultValueComputed = new DatabaseFunction(parsedNode.getChildValue(null, "defaultValueDate", String.class));
}
defaultValueBoolean = parsedNode.getChildValue(null, "defaultValueBoolean", Boolean.class);
String defaultValueComputedString = parsedNode.getChildValue(null, "defaultValueComputed", String.class);
if (defaultValueComputedString != null) {
defaultValueComputed = new DatabaseFunction(defaultValueComputedString);
}
String defaultValueSequenceNextString = parsedNode.getChildValue(null, "defaultValueSequenceNext", String.class);
if (defaultValueSequenceNextString != null) {
defaultValueSequenceNext = new SequenceNextValueFunction(defaultValueSequenceNextString);
}
defaultOnNull = parsedNode.getChildValue(null, "defaultOnNull", Boolean.class);
generationType = parsedNode.getChildValue(null, "generationType", String.class);
loadConstraints(parsedNode.getChild(null, "constraints"));
}
protected void loadConstraints(ParsedNode constraintsNode) throws ParsedNodeException {
if (constraintsNode == null) {
return;
}
ConstraintsConfig constraints = new ConstraintsConfig();
constraints.setNullable(constraintsNode.getChildValue(null, "nullable", Boolean.class));
constraints.setNotNullConstraintName(constraintsNode.getChildValue(null, "notNullConstraintName", String.class));
constraints.setPrimaryKey(constraintsNode.getChildValue(null, "primaryKey", Boolean.class));
constraints.setPrimaryKeyName(constraintsNode.getChildValue(null, "primaryKeyName", String.class));
constraints.setPrimaryKeyTablespace(constraintsNode.getChildValue(null, "primaryKeyTablespace", String.class));
constraints.setReferences(constraintsNode.getChildValue(null, "references", String.class));
constraints.setReferencedTableCatalogName(constraintsNode.getChildValue(null, "referencedTableCatalogName", String.class));
constraints.setReferencedTableSchemaName(constraintsNode.getChildValue(null, "referencedTableSchemaName", String.class));
constraints.setReferencedTableName(constraintsNode.getChildValue(null, "referencedTableName", String.class));
constraints.setReferencedColumnNames(constraintsNode.getChildValue(null, "referencedColumnNames", String.class));
constraints.setUnique(constraintsNode.getChildValue(null, "unique", Boolean.class));
constraints.setUniqueConstraintName(constraintsNode.getChildValue(null, "uniqueConstraintName", String.class));
constraints.setNotNullConstraintName(constraintsNode.getChildValue(null, "notNullConstraintName", String.class));
constraints.setCheckConstraint(constraintsNode.getChildValue(null, "checkConstraint", String.class));
constraints.setDeleteCascade(constraintsNode.getChildValue(null, "deleteCascade", Boolean.class));
constraints.setForeignKeyName(constraintsNode.getChildValue(null, "foreignKeyName", String.class));
constraints.setInitiallyDeferred(constraintsNode.getChildValue(null, "initiallyDeferred", Boolean.class));
constraints.setDeferrable(constraintsNode.getChildValue(null, "deferrable", Boolean.class));
constraints.setShouldValidateNullable(constraintsNode.getChildValue(null, "validateNullable", Boolean.class));
constraints.setShouldValidateUnique(constraintsNode.getChildValue(null, "validateUnique", Boolean.class));
constraints.setShouldValidatePrimaryKey(constraintsNode.getChildValue(null, "validatePrimaryKey", Boolean.class));
constraints.setShouldValidateForeignKey(constraintsNode.getChildValue(null, "validateForeignKey", Boolean.class));
setConstraints(constraints);
}
public static class ValueNumeric extends Number {
private static final long serialVersionUID = 1381154777956917462L;
private final Number delegate;
private final String value;
private ValueNumeric(final String value, final Number numeric) {
this.delegate = numeric;
this.value = value;
}
private static ValueNumeric of(Locale locale, String value) throws ParseException {
final Number parsedNumber = NumberFormat.getInstance(locale)
.parse(value);
return new ValueNumeric(value, parsedNumber);
}
@Override
public double doubleValue() {
return delegate.doubleValue();
}
@Override
public float floatValue() {
return delegate.floatValue();
}
@Override
public int intValue() {
return delegate.intValue();
}
@Override
public long longValue() {
return delegate.longValue();
}
@Override
public String toString() {
return value;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof Number)) {
return false;
}
return obj.toString().equals(this.toString());
}
@Override
public int hashCode() {
return this.toString().hashCode();
}
public Number getDelegate() {
return delegate;
}
}
@Override
public Object getSerializableFieldValue(String field) {
Object o = ReflectionSerializer.getInstance().getValue(this, field);
if (field.equals("valueDate") || field.equals("defaultValueDate")) {
return new ISODateFormat().format((Date)o);
} else {
return o;
}
}
}