/
Column.java
764 lines (698 loc) · 23.4 KB
/
Column.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
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.db.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import static org.jumpmind.db.model.ColumnTypes.ORACLE_TIMESTAMPLTZ;
import static org.jumpmind.db.model.ColumnTypes.ORACLE_TIMESTAMPTZ;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.jumpmind.db.platform.PlatformUtils;
import org.jumpmind.util.FormatUtils;
/**
* Represents a column in the database model.
*/
public class Column implements Cloneable, Serializable {
/** Unique ID for serialization purposes. */
private static final long serialVersionUID = 1L;
/** The name of the column. */
private String name;
/**
* The java name of the column (optional and unused by DdlUtils, for Torque compatibility).
*/
private String javaName;
/** The column's description. */
private String description;
/** Whether the column is a primary key column. */
private boolean primaryKey;
/**
* Whether the column is required, ie. it must not contain <code>NULL</code>
*/
private boolean required;
/** Whether the column's value is incremented automatically. */
private boolean autoIncrement;
/** Whether the column's value is unique using the unique constraint keyword (e.g. SQLite). */
private boolean unique;
/**
* The mapped JDBC type code
*/
private int mappedTypeCode;
/**
* The mapped JDBC type
*/
private String mappedType;
/** The size of the column for JDBC types that require/support this. */
private String size;
/** The size of the column for JDBC types that require/support this. */
private Integer sizeAsInt;
private int charOctetLength;
/** The scale of the column for JDBC types that require/support this. */
private int scale;
/** The default value. */
private String defaultValue;
/**
* The actual JDBC type code.
*/
private int jdbcTypeCode = Integer.MIN_VALUE;
/**
* The actual JDBC type name.
*/
private String jdbcTypeName;
private boolean distributionKey;
private int precisionRadix;
private Map<String, PlatformColumn> platformColumns;
private int primaryKeySequence;
// private String[] enumValues;
public Column() {
}
public Column(String name) {
this(name, false);
}
public Column(String name, boolean primaryKey) {
this.name = name;
this.primaryKey = primaryKey;
}
public Column(String name, boolean primaryKey, int typeCode, int size, int scale) {
this.name = name;
this.primaryKey = primaryKey;
this.required = primaryKey;
setTypeCode(typeCode);
setSizeAndScale(size, scale);
}
/**
* Returns the name of the column.
*
* @return The name
*/
public String getName() {
return name;
}
/**
* Sets the name of the column.
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns the java name of the column. This property is unused by DdlUtils and only for Torque compatibility.
*
* @return The java name
*/
public String getJavaName() {
return javaName;
}
/**
* Sets the java name of the column. This property is unused by DdlUtils and only for Torque compatibility.
*
* @param javaName
* The java name
*/
public void setJavaName(String javaName) {
this.javaName = javaName;
}
/**
* Returns the description of the column.
*
* @return The description
*/
public String getDescription() {
return description;
}
/**
* Sets the description of the column.
*
* @param description
* The description
*/
public void setDescription(String description) {
this.description = description;
}
/**
* Determines whether this column is a primary key column.
*
* @return <code>true</code> if this column is a primary key column
*/
public boolean isPrimaryKey() {
return primaryKey;
}
/**
* Specifies whether this column is a primary key column.
*
* @param primaryKey
* <code>true</code> if this column is a primary key column
*/
public void setPrimaryKey(boolean primaryKey) {
this.primaryKey = primaryKey;
}
/**
* Determines whether this column is a required column, ie. that it is not allowed to contain <code>NULL</code> values.
*
* @return <code>true</code> if this column is a required column
*/
public boolean isRequired() {
return required;
}
/**
* Specifies whether this column is a required column, ie. that it is not allowed to contain <code>NULL</code> values.
*
* @param required
* <code>true</code> if this column is a required column
*/
public void setRequired(boolean required) {
this.required = required;
}
/**
* Determines whether this column is an auto-increment column.
*
* @return <code>true</code> if this column is an auto-increment column
*/
public boolean isAutoIncrement() {
return autoIncrement;
}
/**
* Specifies whether this column is an auto-increment column.
*
* @param autoIncrement
* <code>true</code> if this column is an auto-increment column
*/
public void setAutoIncrement(boolean autoIncrement) {
this.autoIncrement = autoIncrement;
}
/**
* Determines whether this column is an unique column.
*
* @return <code>true</code> if this column is an unique column
*/
public boolean isUnique() {
return unique;
}
/**
* Specifies whether this column is an unique column.
*
* @param autoIncrement
* <code>true</code> if this column is a unique column
*/
public void setUnique(boolean unique) {
this.unique = unique;
}
/**
* Returns the code (one of the constants in {@link java.sql.Types}) of the JDBC type of the column.
*
* @return The type code
*/
public int getMappedTypeCode() {
return mappedTypeCode;
}
/**
* Sets the code (one of the constants in {@link java.sql.Types}) of the JDBC type of the column.
*
* @param typeCode
* The type code
*/
public final void setMappedTypeCode(int typeCode) {
this.mappedType = TypeMap.getJdbcTypeName(typeCode);
if (this.mappedType == null) {
throw new ModelException("Unknown JDBC type code " + typeCode + " for : " + toString());
}
this.mappedTypeCode = typeCode;
}
/**
* Returns the JDBC type of the column.
*
* @return The type
*/
public String getMappedType() {
return mappedType;
}
/**
* Sets the JDBC type of the column.
*
* @param type
* The type
*/
public void setMappedType(String type) {
Integer typeCode = TypeMap.getJdbcTypeCode(type);
if (typeCode == null) {
throw new ModelException("Unknown JDBC type " + type + " for : " + toString());
} else {
this.mappedTypeCode = typeCode.intValue();
// we get the corresponding string value from the TypeMap in order
// to detect extension types which we don't want in the model
this.mappedType = TypeMap.getJdbcTypeName(typeCode);
}
}
/**
* Determines whether this column is of a numeric type.
*
* @return <code>true</code> if this column is of a numeric type
*/
public boolean isOfNumericType() {
return TypeMap.isNumericType(getMappedTypeCode());
}
/**
* Determines whether this column is of a text type.
*
* @return <code>true</code> if this column is of a text type
*/
public boolean isOfTextType() {
return TypeMap.isTextType(getMappedTypeCode());
}
/**
* Determines whether this column is of a binary type.
*
* @return <code>true</code> if this column is of a binary type
*/
public boolean isOfBinaryType() {
return TypeMap.isBinaryType(getMappedTypeCode());
}
/**
* Determines whether this column is of a special type.
*
* @return <code>true</code> if this column is of a special type
*/
public boolean isOfSpecialType() {
return TypeMap.isSpecialType(getMappedTypeCode());
}
/**
* Returns the size of the column.
*
* @return The size
*/
public String getSize() {
return size;
}
/**
* Returns the size of the column as an integer.
*
* @return The size as an integer
*/
public int getSizeAsInt() {
return sizeAsInt == null ? 0 : sizeAsInt.intValue();
}
/**
* Sets the size of the column. This is either a simple integer value or a comma-separated pair of integer values specifying the size and scale.
*
* @param size
* The size
*/
public void setSize(String size) {
if (size != null) {
int pos = size.indexOf(",");
this.size = size;
if (pos < 0) {
scale = 0;
sizeAsInt = Integer.valueOf(size);
} else {
sizeAsInt = Integer.valueOf(size.substring(0, pos));
scale = Integer.parseInt(size.substring(pos + 1));
}
} else {
this.size = null;
sizeAsInt = null;
scale = 0;
}
if (platformColumns != null) {
for (PlatformColumn platformColumn : platformColumns.values()) {
platformColumn.setSize(sizeAsInt == null ? -1 : sizeAsInt);
platformColumn.setDecimalDigits(size == null || size.indexOf(",") < 0 ? -1 : scale);
}
}
}
/**
* Returns the scale of the column.
*
* @return The scale
*/
public int getScale() {
return scale;
}
/**
* Sets the scale of the column.
*
* @param scale
* The scale
*/
public void setScale(int scale) {
setSizeAndScale(getSizeAsInt(), scale);
}
/**
* Sets both the size and scale.
*
* @param size
* The size
* @param scale
* The scale
*/
public final void setSizeAndScale(int size, int scale) {
sizeAsInt = Integer.valueOf(size);
this.scale = scale;
this.size = String.valueOf(size);
if (scale > 0) {
this.size += "," + scale;
}
if (platformColumns != null) {
for (PlatformColumn platformColumn : platformColumns.values()) {
platformColumn.setSize(size);
platformColumn.setDecimalDigits(scale);
}
}
}
/**
* Returns the precision radix of the column.
*
* @return The precision radix
*/
public int getPrecisionRadix() {
return this.precisionRadix;
}
/**
* Sets the precision radix of the column.
*
* @param precisionRadix
* The precision radix
*/
public void setPrecisionRadix(int precisionRadix) {
this.precisionRadix = precisionRadix;
}
/**
* Returns the default value of the column.
*
* @return The default value
*/
public String getDefaultValue() {
return defaultValue;
}
/**
* Tries to parse the default value of the column and returns it as an object of the corresponding java type. If the value could not be parsed, then the
* original definition is returned.
*
* @return The parsed default value
*/
public Object getParsedDefaultValue() {
if ((defaultValue != null) && (defaultValue.length() > 0)) {
try {
switch (mappedTypeCode) {
case Types.TINYINT:
case Types.SMALLINT:
return Short.valueOf(getCleanDefaultValue());
case Types.INTEGER:
try {
return Integer.valueOf(getCleanDefaultValue());
} catch (NumberFormatException e) {
return Long.valueOf(getCleanDefaultValue());
}
case Types.BIGINT:
return Long.valueOf(getCleanDefaultValue());
case Types.DECIMAL:
case Types.NUMERIC:
return new BigDecimal(getCleanDefaultValue());
case Types.REAL:
return Float.valueOf(getCleanDefaultValue());
case Types.DOUBLE:
case Types.FLOAT:
return Double.valueOf(getCleanDefaultValue());
case Types.DATE:
return Date.valueOf(defaultValue);
case Types.TIME:
return Time.valueOf(defaultValue);
case Types.TIMESTAMP:
return Timestamp.valueOf(defaultValue);
case Types.BIT:
return FormatUtils.toBoolean(defaultValue);
default:
if (PlatformUtils.supportsJava14JdbcTypes()
&& (mappedTypeCode == PlatformUtils.determineBooleanTypeCode())) {
return FormatUtils.toBoolean(defaultValue);
}
break;
}
} catch (NumberFormatException ex) {
return null;
} catch (IllegalArgumentException ex) {
return null;
}
}
return defaultValue;
}
private String getCleanDefaultValue() {
return defaultValue.replace("'", "");
}
public void removePlatformColumn(String databaseName) {
if (platformColumns != null) {
platformColumns.remove(databaseName);
}
}
public void addPlatformColumn(PlatformColumn platformColumn) {
if (platformColumns == null) {
platformColumns = new HashMap<String, PlatformColumn>();
}
platformColumns.put(platformColumn.getName(), platformColumn);
}
public Map<String, PlatformColumn> getPlatformColumns() {
return platformColumns;
}
public PlatformColumn findPlatformColumn(String name) {
PlatformColumn platformColumn = null;
if (platformColumns != null) {
platformColumn = platformColumns.get(name);
}
return platformColumn;
}
/**
* Sets the default value of the column. Note that this expression will be used within quotation marks when generating the column, and thus is subject to
* the conversion rules of the target database.
*
* @param defaultValue
* The default value
*/
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
if (platformColumns != null) {
Collection<PlatformColumn> cols = platformColumns.values();
for (PlatformColumn platformColumn : cols) {
platformColumn.setDefaultValue(defaultValue);
}
}
}
/**
* {@inheritDoc}
*/
public Object clone() throws CloneNotSupportedException {
Column result = (Column) super.clone();
result.name = name;
result.javaName = javaName;
result.primaryKey = primaryKey;
result.required = required;
result.autoIncrement = autoIncrement;
result.mappedTypeCode = mappedTypeCode;
result.mappedType = mappedType;
result.size = size;
result.defaultValue = defaultValue;
result.scale = scale;
result.size = size;
result.sizeAsInt = sizeAsInt;
if (platformColumns != null) {
result.platformColumns = new HashMap<String, PlatformColumn>(platformColumns.size());
for (Map.Entry<String, PlatformColumn> entry : platformColumns.entrySet()) {
result.platformColumns.put(entry.getKey(), (PlatformColumn) entry.getValue().clone());
}
}
return result;
}
/**
* {@inheritDoc}
*/
public boolean equals(Object obj) {
if (obj instanceof Column) {
Column other = (Column) obj;
EqualsBuilder comparator = new EqualsBuilder();
// Note that this compares case sensitive
comparator.append(name, other.name);
comparator.append(primaryKey, other.primaryKey);
comparator.append(required, other.required);
comparator.append(autoIncrement, other.autoIncrement);
comparator.append(mappedTypeCode, other.mappedTypeCode);
comparator.append(getParsedDefaultValue(), other.getParsedDefaultValue());
// comparing the size makes only sense for types where it is
// relevant
if ((mappedTypeCode == Types.NUMERIC) || (mappedTypeCode == Types.DECIMAL)) {
comparator.append(size, other.size);
comparator.append(scale, other.scale);
} else if ((mappedTypeCode == Types.CHAR) || (mappedTypeCode == Types.VARCHAR)
|| (mappedTypeCode == Types.BINARY) || (mappedTypeCode == Types.VARBINARY)) {
comparator.append(size, other.size);
}
return comparator.isEquals();
} else {
return false;
}
}
public boolean equalsByName(Column other) {
return StringUtils.equalsIgnoreCase(name, other.name) && primaryKey == other.primaryKey;
}
/**
* {@inheritDoc}
*/
public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder(17, 37);
builder.append(name);
builder.append(primaryKey);
builder.append(required);
builder.append(autoIncrement);
builder.append(mappedTypeCode);
builder.append(mappedType);
builder.append(scale);
builder.append(getParsedDefaultValue());
if (!TypeMap.isNumericType(mappedTypeCode)) {
builder.append(size);
}
return builder.toHashCode();
}
/**
* {@inheritDoc}
*/
public String toString() {
StringBuilder result = new StringBuilder();
result.append("Column [name=");
result.append(getName());
result.append("; jdbcType=");
result.append(getJdbcTypeName());
result.append("; mappedType=");
result.append(getMappedType());
result.append("]");
return result.toString();
}
/**
* Returns a verbose string representation of this column.
*
* @return The string representation
*/
public String toVerboseString() {
StringBuilder result = new StringBuilder();
result.append("Column [name=");
result.append(getName());
result.append("; javaName=");
result.append(getJavaName());
result.append("; type=");
result.append(getMappedType());
result.append("; typeCode=");
result.append(getMappedTypeCode());
result.append("; size=");
result.append(getSize());
result.append("; required=");
result.append(isRequired());
result.append("; primaryKey=");
result.append(isPrimaryKey());
result.append("; autoIncrement=");
result.append(isAutoIncrement());
result.append("; defaultValue=");
result.append(getDefaultValue());
result.append("; precisionRadix=");
result.append(getPrecisionRadix());
result.append("; scale=");
result.append(getScale());
result.append("]");
return result.toString();
}
public void setJdbcTypeName(String jdbcTypeName) {
this.jdbcTypeName = jdbcTypeName;
}
public String getJdbcTypeName() {
return jdbcTypeName;
}
public boolean isDistributionKey() {
return distributionKey;
}
public void setDistributionKey(boolean distributionKey) {
this.distributionKey = distributionKey;
}
public final void setTypeCode(int typeCode) {
this.setMappedTypeCode(typeCode);
this.setJdbcTypeCode(typeCode);
}
public final void setJdbcTypeCode(int jdbcTypeCode) {
this.jdbcTypeCode = jdbcTypeCode;
}
public int getJdbcTypeCode() {
return jdbcTypeCode;
}
public boolean isTimestampWithTimezone() {
return mappedTypeCode == ORACLE_TIMESTAMPLTZ || mappedTypeCode == ORACLE_TIMESTAMPTZ ||
jdbcTypeCode == ORACLE_TIMESTAMPLTZ || jdbcTypeCode == ORACLE_TIMESTAMPTZ ||
(jdbcTypeName != null && jdbcTypeName.equals("timestamptz"));
}
public boolean containsJdbcTypes() {
return jdbcTypeCode != Integer.MIN_VALUE && jdbcTypeName != null;
}
//
// public void setEnumValues(String[] enumValues) {
// this.enumValues = enumValues;
// }
//
// public String[] getEnumValues() {
// return enumValues;
// }
//
// public boolean isEnum() {
// return enumValues != null && enumValues.length > 0;
// }
public int getCharOctetLength() {
return charOctetLength;
}
public void setCharOctetLength(int charOctetLength) {
this.charOctetLength = charOctetLength;
}
public int getPrimaryKeySequence() {
return primaryKeySequence;
}
public void setPrimaryKeySequence(int primaryKeySequence) {
this.primaryKeySequence = primaryKeySequence;
}
}