-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
CreateTableStatement.java
267 lines (215 loc) · 11 KB
/
CreateTableStatement.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
package liquibase.statement.core;
import liquibase.ContextExpression;
import liquibase.datatype.LiquibaseDataType;
import liquibase.statement.*;
import java.util.*;
public class CreateTableStatement extends AbstractSqlStatement implements CompoundStatement {
private String catalogName;
private String schemaName;
private String tableName;
private String tablespace;
private String remarks;
private List<String> columns = new ArrayList<>();
private Set<AutoIncrementConstraint> autoIncrementConstraints = new HashSet<>();
private Map<String, LiquibaseDataType> columnTypes = new HashMap<>();
private Map<String, Object> defaultValues = new HashMap<>();
private Map<String, String> defaultValueConstraintNames = new HashMap<>();
private Map<String, String> columnRemarks = new HashMap<>();
private PrimaryKeyConstraint primaryKeyConstraint;
private Map<String, NotNullConstraint> notNullConstraints = new HashMap<String, NotNullConstraint>();
private Set<ForeignKeyConstraint> foreignKeyConstraints = new HashSet<ForeignKeyConstraint>();
/* NOT NULL constraints in RDBMSs are curious beasts. In some RDBMS, they do not exist as constraints at all, i.e.
they are merely a property of the column. In others, like Oracle DB, they can exist in both forms, and to be
able to give the NN constraint a name in CREATE TABLE, we need to save both the NN property as well as the NN constraint. To make things even more complicated, you cannot just add a NN constraint after the list
of columns, like you could do with UNIQUE, CHECK or FOREIGN KEY constraints. They must be defined
in line with the column (this implies that a NN constraint can always affects exactly one column). */
private HashMap<String, NotNullConstraint> notNullColumns = new HashMap<>();
private Set<UniqueConstraint> uniqueConstraints = new LinkedHashSet<>();
private Set<String> computedColumns = new HashSet<>();
public CreateTableStatement(String catalogName, String schemaName, String tableName) {
this.catalogName = catalogName;
this.schemaName = schemaName;
this.tableName = tableName;
}
public CreateTableStatement(String catalogName, String schemaName, String tableName, String remarks) {
this(catalogName, schemaName, tableName);
this.remarks = remarks;
}
public String getCatalogName() {
return catalogName;
}
public String getSchemaName() {
return schemaName;
}
public String getTableName() {
return tableName;
}
public List<String> getColumns() {
return columns;
}
public String getTablespace() {
return tablespace;
}
public CreateTableStatement setTablespace(String tablespace) {
this.tablespace = tablespace;
return this;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public PrimaryKeyConstraint getPrimaryKeyConstraint() {
return primaryKeyConstraint;
}
public Set<ForeignKeyConstraint> getForeignKeyConstraints() {
return foreignKeyConstraints;
}
public Set<UniqueConstraint> getUniqueConstraints() {
return uniqueConstraints;
}
@java.lang.SuppressWarnings("squid:S4275")
public Map<String, NotNullConstraint> getNotNullColumns() {
return notNullConstraints;
}
public CreateTableStatement addPrimaryKeyColumn(String columnName, LiquibaseDataType columnType, Object defaultValue, String keyName,
String tablespace, ColumnConstraint... constraints) {
PrimaryKeyConstraint pkConstraint = new PrimaryKeyConstraint(keyName);
pkConstraint.addColumns(columnName);
pkConstraint.setTablespace(tablespace);
List<ColumnConstraint> allConstraints = new ArrayList<>();
allConstraints.addAll(Arrays.asList(constraints));
allConstraints.add(new NotNullConstraint(columnName));
allConstraints.add(pkConstraint);
addColumn(columnName, columnType, defaultValue, allConstraints.toArray(new ColumnConstraint[allConstraints.size()]));
return this;
}
public CreateTableStatement addPrimaryKeyColumn(String columnName, LiquibaseDataType columnType, Object defaultValue,
Boolean validate, String keyName, String tablespace, ColumnConstraint... constraints) {
return addPrimaryKeyColumn(columnName, columnType, defaultValue, validate, false, false, keyName, tablespace, constraints);
}
public CreateTableStatement addPrimaryKeyColumn(String columnName, LiquibaseDataType columnType, Object defaultValue,
Boolean validate, boolean deferrable, boolean initiallyDeferred, String keyName, String tablespace, ColumnConstraint... constraints) {
PrimaryKeyConstraint pkConstraint = new PrimaryKeyConstraint(keyName);
if (validate != null) {
pkConstraint.setValidatePrimaryKey(validate);
}
pkConstraint.addColumns(columnName);
pkConstraint.setTablespace(tablespace);
pkConstraint.setDeferrable(deferrable);
pkConstraint.setInitiallyDeferred(initiallyDeferred);
List<ColumnConstraint> allConstraints = new ArrayList<ColumnConstraint>(Arrays.asList(constraints));
allConstraints.add(new NotNullConstraint(columnName));
allConstraints.add(pkConstraint);
addColumn(columnName, columnType, defaultValue, allConstraints.toArray(new ColumnConstraint[allConstraints.size()]));
return this;
}
public CreateTableStatement addColumn(String columnName, LiquibaseDataType columnType) {
return addColumn(columnName, columnType, null, new ColumnConstraint[0]);
}
public CreateTableStatement addColumn(String columnName, LiquibaseDataType columnType, Object defaultValue) {
if (defaultValue instanceof ColumnConstraint) {
return addColumn(columnName, columnType, null, new ColumnConstraint[]{(ColumnConstraint) defaultValue});
}
return addColumn(columnName, columnType, defaultValue, new ColumnConstraint[0]);
}
public CreateTableStatement addColumn(String columnName, LiquibaseDataType columnType, ColumnConstraint[] constraints) {
return addColumn(columnName, columnType, null, constraints);
}
public CreateTableStatement addColumn(String columnName, LiquibaseDataType columnType, Object defaultValue, ColumnConstraint[] constraints) {
return addColumn(columnName, columnType, defaultValue, null, constraints);
}
public CreateTableStatement addColumn(String columnName, LiquibaseDataType columnType, Object defaultValue, String remarks, ColumnConstraint... constraints) {
return addColumn(columnName, columnType, null, defaultValue, remarks, constraints);
}
public CreateTableStatement addColumn(String columnName, LiquibaseDataType columnType, String defaultValueConstraintName, Object defaultValue, String remarks, ColumnConstraint... constraints) {
this.getColumns().add(columnName);
this.columnTypes.put(columnName, columnType);
if (defaultValue != null) {
defaultValues.put(columnName, defaultValue);
}
if (defaultValueConstraintName != null) {
defaultValueConstraintNames.put(columnName, defaultValueConstraintName);
}
if (remarks != null) {
this.columnRemarks.put(columnName, remarks);
}
if (constraints != null) {
for (ColumnConstraint constraint : constraints) {
if (constraint == null) {
continue;
}
if (constraint instanceof PrimaryKeyConstraint) {
if (this.getPrimaryKeyConstraint() == null) {
this.primaryKeyConstraint = (PrimaryKeyConstraint) constraint;
} else {
for (String column : ((PrimaryKeyConstraint) constraint).getColumns()) {
this.getPrimaryKeyConstraint().addColumns(column);
}
}
} else if (constraint instanceof NotNullConstraint) {
((NotNullConstraint) constraint).setColumnName(columnName);
getNotNullColumns().put(columnName, (NotNullConstraint) constraint);
} else if (constraint instanceof ForeignKeyConstraint) {
((ForeignKeyConstraint) constraint).setColumn(columnName);
getForeignKeyConstraints().add(((ForeignKeyConstraint) constraint));
} else if (constraint instanceof UniqueConstraint) {
((UniqueConstraint) constraint).addColumns(columnName);
getUniqueConstraints().add(((UniqueConstraint) constraint));
} else if (constraint instanceof AutoIncrementConstraint) {
autoIncrementConstraints.add((AutoIncrementConstraint) constraint);
} else {
throw new RuntimeException("Unknown constraint type: " + constraint.getClass().getName());
}
}
}
return this;
}
public Object getDefaultValue(String column) {
return defaultValues.get(column);
}
public String getDefaultValueConstraintName(String column) {
return defaultValueConstraintNames.get(column);
}
public String getColumnRemarks(String column) {
return columnRemarks.get(column);
}
public CreateTableStatement addColumnConstraint(NotNullConstraint notNullConstraint) {
getNotNullColumns().put(notNullConstraint.getColumnName(), notNullConstraint);
return this;
}
public CreateTableStatement addColumnConstraint(ForeignKeyConstraint fkConstraint) {
getForeignKeyConstraints().add(fkConstraint);
return this;
}
public CreateTableStatement addColumnConstraint(UniqueConstraint uniqueConstraint) {
getUniqueConstraints().add(uniqueConstraint);
return this;
}
public CreateTableStatement addColumnConstraint(AutoIncrementConstraint autoIncrementConstraint) {
getAutoIncrementConstraints().add(autoIncrementConstraint);
return this;
}
public Set<AutoIncrementConstraint> getAutoIncrementConstraints() {
return autoIncrementConstraints;
}
public Map<String, LiquibaseDataType> getColumnTypes() {
return columnTypes;
}
public Map<String, Object> getDefaultValues() {
return defaultValues;
}
public Map<String, String> getDefaultValueConstraintNames() {
return defaultValueConstraintNames;
}
public void setSchemaName(String schemaName) {
this.schemaName = schemaName;
}
public void setComputed(String columnName) {
this.computedColumns.add(columnName);
}
public boolean isComputed(String columnName) {
return this.computedColumns.contains(columnName);
}
}