-
Notifications
You must be signed in to change notification settings - Fork 221
/
Db2DdlBuilder.java
299 lines (261 loc) · 12.1 KB
/
Db2DdlBuilder.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
package org.jumpmind.db.platform.db2;
/*
* 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 java.sql.Types;
import java.util.Iterator;
import java.util.List;
import org.jumpmind.db.alter.AddColumnChange;
import org.jumpmind.db.alter.AddPrimaryKeyChange;
import org.jumpmind.db.alter.ColumnDataTypeChange;
import org.jumpmind.db.alter.CopyColumnValueChange;
import org.jumpmind.db.alter.PrimaryKeyChange;
import org.jumpmind.db.alter.RemoveColumnChange;
import org.jumpmind.db.alter.RemovePrimaryKeyChange;
import org.jumpmind.db.alter.TableChange;
import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.Database;
import org.jumpmind.db.model.IIndex;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.model.TypeMap;
import org.jumpmind.db.platform.AbstractDdlBuilder;
import org.jumpmind.db.platform.DatabaseNamesConstants;
import org.jumpmind.db.platform.PlatformUtils;
/*
* The DDL Builder for DB2.
*/
public class Db2DdlBuilder extends AbstractDdlBuilder {
public Db2DdlBuilder() {
super(DatabaseNamesConstants.DB2);
databaseInfo.addNativeTypeMapping(Types.ARRAY, "BLOB", Types.BLOB);
databaseInfo.addNativeTypeMapping(Types.BINARY, "CHAR {0} FOR BIT DATA");
databaseInfo.addNativeTypeMapping(Types.BIT, "SMALLINT", Types.SMALLINT);
databaseInfo.addNativeTypeMapping(Types.FLOAT, "DOUBLE", Types.DOUBLE);
databaseInfo.addNativeTypeMapping(Types.JAVA_OBJECT, "BLOB", Types.BLOB);
databaseInfo.addNativeTypeMapping(Types.LONGVARBINARY, "LONG VARCHAR FOR BIT DATA");
databaseInfo.addNativeTypeMapping(Types.LONGVARCHAR, "LONG VARCHAR");
databaseInfo.addNativeTypeMapping(Types.NULL, "LONG VARCHAR FOR BIT DATA",
Types.LONGVARBINARY);
databaseInfo.addNativeTypeMapping(Types.NUMERIC, "DECIMAL", Types.DECIMAL);
databaseInfo.addNativeTypeMapping(Types.OTHER, "BLOB", Types.BLOB);
databaseInfo.addNativeTypeMapping(Types.STRUCT, "BLOB", Types.BLOB);
databaseInfo.addNativeTypeMapping(Types.TINYINT, "SMALLINT", Types.SMALLINT);
databaseInfo.addNativeTypeMapping(Types.VARBINARY, "VARCHAR {0} FOR BIT DATA");
databaseInfo.addNativeTypeMapping("BOOLEAN", "SMALLINT", "SMALLINT");
databaseInfo.setDefaultSize(Types.CHAR, 254);
databaseInfo.setDefaultSize(Types.VARCHAR, 254);
databaseInfo.setDefaultSize(Types.BINARY, 254);
databaseInfo.setDefaultSize(Types.VARBINARY, 254);
databaseInfo.setMaxIdentifierLength(128);
databaseInfo.setMaxColumnNameLength(128);
databaseInfo.setMaxConstraintNameLength(128);
databaseInfo.setMaxForeignKeyNameLength(128);
databaseInfo.setNonBlankCharColumnSpacePadded(true);
databaseInfo.setBlankCharColumnSpacePadded(true);
databaseInfo.setCharColumnSpaceTrimmed(false);
databaseInfo.setEmptyStringNulled(false);
addEscapedCharSequence("'", "''");
}
@Override
protected String getNativeDefaultValue(Column column) {
if ((column.getMappedTypeCode() == Types.BIT)
|| (PlatformUtils.supportsJava14JdbcTypes() && (column.getMappedTypeCode() == PlatformUtils
.determineBooleanTypeCode()))) {
return getDefaultValueHelper().convert(column.getDefaultValue(),
column.getMappedTypeCode(), Types.SMALLINT).toString();
} else {
return super.getNativeDefaultValue(column);
}
}
@Override
protected void writeColumnAutoIncrementStmt(Table table, Column column, StringBuilder ddl) {
ddl.append("GENERATED BY DEFAULT AS IDENTITY");
}
@Override
public String getSelectLastIdentityValues(Table table) {
return "VALUES IDENTITY_VAL_LOCAL()";
}
@Override
public void writeExternalIndexDropStmt(Table table, IIndex index, StringBuilder ddl) {
// Index names in DB2 are unique to a schema and hence Derby does not
// use the ON <tablename> clause
ddl.append("DROP INDEX ");
printIdentifier(getIndexName(index), ddl);
printEndOfStatement(ddl);
}
@Override
protected void writeCastExpression(Column sourceColumn, Column targetColumn, StringBuilder ddl) {
String sourceNativeType = getBareNativeType(sourceColumn);
String targetNativeType = getBareNativeType(targetColumn);
if (sourceNativeType.equals(targetNativeType)) {
printIdentifier(getColumnName(sourceColumn), ddl);
} else {
String type = getSqlType(targetColumn);
// DB2 has the limitation that it cannot convert numeric values
// to VARCHAR, though it can convert them to CHAR
if (TypeMap.isNumericType(sourceColumn.getMappedTypeCode())
&& "VARCHAR".equalsIgnoreCase(targetNativeType)) {
Object sizeSpec = targetColumn.getSize();
if (sizeSpec == null) {
sizeSpec = databaseInfo.getDefaultSize(targetColumn.getMappedTypeCode());
}
type = "CHAR(" + sizeSpec.toString() + ")";
}
ddl.append("CAST(");
printIdentifier(getColumnName(sourceColumn), ddl);
ddl.append(" AS ");
ddl.append(type);
ddl.append(")");
}
}
@Override
protected void processTableStructureChanges(Database currentModel, Database desiredModel,
Table sourceTable, Table targetTable, List<TableChange> changes, StringBuilder ddl) {
// DB2 provides only limited ways to alter a column, so we don't use
// them
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext();) {
TableChange change = changeIt.next();
if (change instanceof AddColumnChange) {
AddColumnChange addColumnChange = (AddColumnChange) change;
/*
* DB2 does not allow the GENERATED BY DEFAULT AS IDENTITY
* clause in the ALTER TABLE ADD COLUMN statement, so we have to
* rebuild the table instead
*/
if (!addColumnChange.getNewColumn().isAutoIncrement()) {
processChange(currentModel, desiredModel, addColumnChange, ddl);
changeIt.remove();
} else {
return;
}
}
}
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext();) {
TableChange change = changeIt.next();
if (change instanceof PrimaryKeyChange) {
processChange(currentModel, desiredModel, (PrimaryKeyChange) change, ddl);
changeIt.remove();
} else if (change instanceof RemovePrimaryKeyChange) {
processChange(currentModel, desiredModel, (RemovePrimaryKeyChange) change, ddl);
changeIt.remove();
}
}
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext();) {
TableChange change = changeIt.next();
if (change instanceof RemoveColumnChange) {
processChange(currentModel, desiredModel, (RemoveColumnChange) change, ddl);
changeIt.remove();
} else if (change instanceof CopyColumnValueChange) {
CopyColumnValueChange copyColumnChange = (CopyColumnValueChange) change;
processChange(currentModel, desiredModel, copyColumnChange, ddl);
changeIt.remove();
}
}
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext();) {
TableChange change = changeIt.next();
if (change instanceof AddPrimaryKeyChange) {
processChange(currentModel, desiredModel, (AddPrimaryKeyChange) change, ddl);
changeIt.remove();
}
}
super.processTableStructureChanges(currentModel, desiredModel, sourceTable, targetTable,
changes, ddl);
}
@Override
protected boolean writeAlterColumnDataType(ColumnDataTypeChange change, StringBuilder ddl) {
if (!change.getChangedColumn().isPrimaryKey()) {
writeTableAlterStmt(change.getChangedTable(), ddl);
ddl.append(" ALTER COLUMN ");
Column column = change.getChangedColumn();
column.setTypeCode(change.getNewTypeCode());
printIdentifier(getColumnName(column), ddl);
ddl.append(" SET DATA TYPE ");
ddl.append(getSqlType(column));
printEndOfStatement(ddl);
return true;
} else {
return false;
}
}
/*
* Processes the addition of a column to a table.
*/
protected void processChange(Database currentModel, Database desiredModel,
AddColumnChange change, StringBuilder ddl) {
ddl.append("ALTER TABLE ");
ddl.append(getFullyQualifiedTableNameShorten(change.getChangedTable()));
printIndent(ddl);
ddl.append("ADD COLUMN ");
writeColumn(change.getChangedTable(), change.getNewColumn(), ddl);
printEndOfStatement(ddl);
change.apply(currentModel, delimitedIdentifierModeOn);
}
/*
* Processes the removal of a column from a table.
*/
protected void processChange(Database currentModel, Database desiredModel,
RemoveColumnChange change, StringBuilder ddl) {
ddl.append("ALTER TABLE ");
ddl.append(getFullyQualifiedTableNameShorten(change.getChangedTable()));
printIndent(ddl);
ddl.append("DROP COLUMN ");
printIdentifier(getColumnName(change.getColumn()), ddl);
printEndOfStatement(ddl);
writeReorgStmt(change.getChangedTable(), ddl);
printEndOfStatement(ddl);
change.apply(currentModel, delimitedIdentifierModeOn);
}
/*
* Processes the removal of a primary key from a table.
*/
protected void processChange(Database currentModel, Database desiredModel,
RemovePrimaryKeyChange change, StringBuilder ddl) {
ddl.append("ALTER TABLE ");
ddl.append(getFullyQualifiedTableNameShorten(change.getChangedTable()));
printIndent(ddl);
ddl.append("DROP PRIMARY KEY");
printEndOfStatement(ddl);
change.apply(currentModel, delimitedIdentifierModeOn);
}
/*
* Processes the change of the primary key of a table.
*/
protected void processChange(Database currentModel, Database desiredModel,
PrimaryKeyChange change, StringBuilder ddl) {
ddl.append("ALTER TABLE ");
ddl.append(getFullyQualifiedTableNameShorten(change.getChangedTable()));
printIndent(ddl);
ddl.append("DROP PRIMARY KEY");
printEndOfStatement(ddl);
writeExternalPrimaryKeysCreateStmt(change.getChangedTable(),
change.getNewPrimaryKeyColumns(), ddl);
change.apply(currentModel, delimitedIdentifierModeOn);
}
@Override
protected void writeExternalPrimaryKeysCreateStmt(Table table, Column[] primaryKeyColumns,
StringBuilder ddl) {
super.writeExternalPrimaryKeysCreateStmt(table, primaryKeyColumns, ddl);
writeReorgStmt(table, ddl);
printEndOfStatement(ddl);
}
protected void writeReorgStmt(Table table, StringBuilder ddl) {
ddl.append("CALL SYSPROC.ADMIN_CMD('REORG TABLE ");
ddl.append(getFullyQualifiedTableNameShorten(table));
ddl.append("')");
}
}