/
MySqlDdlBuilder.java
328 lines (296 loc) · 14.5 KB
/
MySqlDdlBuilder.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
package org.jumpmind.db.platform.mysql;
/*
* 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.ArrayList;
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.ColumnAutoIncrementChange;
import org.jumpmind.db.alter.ColumnChange;
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.ColumnTypes;
import org.jumpmind.db.model.Database;
import org.jumpmind.db.model.ForeignKey;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.platform.AbstractDdlBuilder;
import org.jumpmind.db.platform.DatabaseNamesConstants;
/*
* The SQL Builder for MySQL.
*/
public class MySqlDdlBuilder extends AbstractDdlBuilder {
public MySqlDdlBuilder() {
super(DatabaseNamesConstants.MYSQL);
databaseInfo.setSystemForeignKeyIndicesAlwaysNonUnique(true);
databaseInfo.setMaxIdentifierLength(64);
databaseInfo.setNullAsDefaultValueRequired(true);
databaseInfo.setDefaultValuesForLongTypesSupported(false);
// see
// http://dev.mysql.com/doc/refman/4.1/en/example-auto-increment.html
databaseInfo.setNonPKIdentityColumnsSupported(false);
// MySql returns synthetic default values for pk columns
databaseInfo.setSyntheticDefaultValueForRequiredReturned(true);
databaseInfo.setCommentPrefix("#");
// Double quotes are only allowed for delimiting identifiers if the
// server SQL mode includes ANSI_QUOTES
databaseInfo.setDelimiterToken("`");
databaseInfo.addNativeTypeMapping(Types.ARRAY, "LONGBLOB", Types.LONGVARBINARY);
databaseInfo.addNativeTypeMapping(Types.BIT, "BIT");
databaseInfo.addNativeTypeMapping(Types.BLOB, "LONGBLOB", Types.LONGVARBINARY);
databaseInfo.addNativeTypeMapping(Types.CLOB, "LONGTEXT", Types.LONGVARCHAR);
databaseInfo.addNativeTypeMapping(Types.DISTINCT, "LONGBLOB", Types.LONGVARBINARY);
databaseInfo.addNativeTypeMapping(Types.FLOAT, "DOUBLE", Types.DOUBLE);
databaseInfo.addNativeTypeMapping(Types.JAVA_OBJECT, "LONGBLOB", Types.LONGVARBINARY);
databaseInfo.addNativeTypeMapping(Types.LONGVARBINARY, "MEDIUMBLOB");
databaseInfo.addNativeTypeMapping(Types.LONGVARCHAR, "MEDIUMTEXT", Types.LONGVARCHAR);
databaseInfo.addNativeTypeMapping(Types.NULL, "MEDIUMBLOB", Types.LONGVARBINARY);
databaseInfo.addNativeTypeMapping(Types.NUMERIC, "DECIMAL", Types.DECIMAL);
databaseInfo.addNativeTypeMapping(Types.OTHER, "LONGBLOB", Types.LONGVARBINARY);
databaseInfo.addNativeTypeMapping(Types.REAL, "FLOAT");
databaseInfo.addNativeTypeMapping(Types.REF, "MEDIUMBLOB", Types.LONGVARBINARY);
databaseInfo.addNativeTypeMapping(Types.STRUCT, "LONGBLOB", Types.LONGVARBINARY);
// Since TIMESTAMP is not a stable datatype yet, and does not support a
// higher precision
// than DATETIME (year to seconds) as of MySQL 5, we map the JDBC type
// here to DATETIME
// TODO: Make this configurable
databaseInfo.addNativeTypeMapping(Types.TIMESTAMP, "DATETIME");
// In MySql, TINYINT has only a range of -128 to 127
databaseInfo.addNativeTypeMapping(Types.TINYINT, "SMALLINT", Types.SMALLINT);
databaseInfo.addNativeTypeMapping("BOOLEAN", "BIT", "BIT");
databaseInfo.addNativeTypeMapping("DATALINK", "MEDIUMBLOB", "LONGVARBINARY");
databaseInfo.addNativeTypeMapping(ColumnTypes.NVARCHAR, "VARCHAR", Types.VARCHAR);
databaseInfo.addNativeTypeMapping(ColumnTypes.LONGNVARCHAR, "VARCHAR", Types.VARCHAR);
databaseInfo.addNativeTypeMapping(ColumnTypes.NCHAR, "CHAR", Types.CHAR);
databaseInfo.setDefaultSize(Types.CHAR, 254);
databaseInfo.setDefaultSize(Types.VARCHAR, 254);
databaseInfo.setDefaultSize(Types.BINARY, 254);
databaseInfo.setDefaultSize(Types.VARBINARY, 254);
databaseInfo.setNonBlankCharColumnSpacePadded(false);
databaseInfo.setBlankCharColumnSpacePadded(false);
databaseInfo.setCharColumnSpaceTrimmed(true);
databaseInfo.setEmptyStringNulled(false);
// MySql 5.0 returns an empty string for default values for pk columns
// which is different from the MySql 4 behaviour
databaseInfo.setSyntheticDefaultValueForRequiredReturned(false);
// we need to handle the backslash first otherwise the other
// already escaped sequences would be affected
addEscapedCharSequence("\\", "\\\\");
addEscapedCharSequence("\0", "\\0");
addEscapedCharSequence("\"", "\\\"");
addEscapedCharSequence("\b", "\\b");
addEscapedCharSequence("\n", "\\n");
addEscapedCharSequence("\r", "\\r");
addEscapedCharSequence("\t", "\\t");
addEscapedCharSequence("\u001A", "\\Z");
}
@Override
protected void dropTable(Table table, StringBuilder ddl, boolean temporary, boolean recreate) {
ddl.append("DROP TABLE IF EXISTS ");
ddl.append(getFullyQualifiedTableNameShorten(table));
printEndOfStatement(ddl);
}
@Override
protected void writeColumnAutoIncrementStmt(Table table, Column column, StringBuilder ddl) {
ddl.append("AUTO_INCREMENT");
}
@Override
protected boolean shouldGeneratePrimaryKeys(Column[] primaryKeyColumns) {
// mySQL requires primary key indication for auto increment key columns
// I'm not sure why the default skips the pk statement if all are
// identity
return true;
}
/*
* Normally mysql will return the LAST_INSERT_ID as the column name for the
* inserted id. Since ddlutils expects the real column name of the field
* that is autoincrementing, the column has an alias of that column name.
*/
@Override
public String getSelectLastIdentityValues(Table table) {
String autoIncrementKeyName = "";
if (table.getAutoIncrementColumns().length > 0) {
autoIncrementKeyName = table.getAutoIncrementColumns()[0].getName();
}
return "SELECT LAST_INSERT_ID() " + autoIncrementKeyName;
}
@Override
protected void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey,
StringBuilder ddl) {
writeTableAlterStmt(table, ddl);
ddl.append("DROP FOREIGN KEY ");
printIdentifier(getForeignKeyName(table, foreignKey), ddl);
printEndOfStatement(ddl);
if (foreignKey.isAutoIndexPresent()) {
writeTableAlterStmt(table, ddl);
ddl.append("DROP INDEX ");
printIdentifier(getForeignKeyName(table, foreignKey), ddl);
printEndOfStatement(ddl);
}
}
@Override
protected void processTableStructureChanges(Database currentModel, Database desiredModel,
Table sourceTable, Table targetTable, List<TableChange> changes, StringBuilder ddl) {
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext();) {
TableChange change = changeIt.next();
if (change instanceof AddColumnChange) {
processChange(currentModel, desiredModel, (AddColumnChange) change, ddl);
changeIt.remove();
} else if (change instanceof ColumnAutoIncrementChange) {
/**
* This has to happen before any primary key changes because if
* a column is bring dropped as auto increment and being dropped
* from the primary key, an auto increment column can't be a non
* primary key column on mysql.
*/
try {
Column sourceColumn = ((ColumnAutoIncrementChange) change).getColumn();
Column targetColumn = (Column) sourceColumn.clone();
targetColumn.setAutoIncrement(!sourceColumn.isAutoIncrement());
processColumnChange(sourceTable, targetTable, sourceColumn, targetColumn, ddl);
changeIt.remove();
} catch (CloneNotSupportedException e) {
log.error("", e);
}
}
}
List<Column> changedColumns = new ArrayList<Column>();
// we don't have to care about the order because the comparator will
// have ensured that a add primary key change comes after all necessary
// columns are present
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();
} else if (change instanceof AddPrimaryKeyChange) {
processChange(currentModel, desiredModel, (AddPrimaryKeyChange) change, ddl);
changeIt.remove();
} else 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();
} else if (change instanceof ColumnChange) {
/*
* we gather all changed columns because we can use the ALTER
* TABLE MODIFY COLUMN statement for them
*/
Column column = ((ColumnChange) change).getChangedColumn();
if (!changedColumns.contains(column)) {
changedColumns.add(column);
}
changeIt.remove();
}
}
for (Iterator<Column> columnIt = changedColumns.iterator(); columnIt.hasNext();) {
Column sourceColumn = columnIt.next();
Column targetColumn = targetTable.findColumn(sourceColumn.getName(),
delimitedIdentifierModeOn);
processColumnChange(sourceTable, targetTable, sourceColumn, targetColumn, ddl);
}
}
/*
* 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);
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);
}
/*
* Processes a change to a column.
*/
protected void processColumnChange(Table sourceTable, Table targetTable, Column sourceColumn,
Column targetColumn, StringBuilder ddl) {
ddl.append("ALTER TABLE ");
ddl.append(getFullyQualifiedTableNameShorten(sourceTable));
printIndent(ddl);
ddl.append("MODIFY COLUMN ");
writeColumn(targetTable, targetColumn, ddl);
printEndOfStatement(ddl);
}
@Override
protected String getSqlType(Column column) {
String sqlType = super.getSqlType(column);
if (column.isAutoIncrement()
&& (column.getMappedTypeCode() == Types.DECIMAL || column.getMappedTypeCode() == Types.NUMERIC)) {
sqlType = "BIGINT";
}
if (column.getMappedTypeCode() == Types.TIMESTAMP && column.getScale() > 0) {
sqlType = "DATETIME(" + column.getScale() + ")";
}
return sqlType;
}
}