Author: tomdz Date: Wed Feb 20 23:57:25 2008 New Revision: 629708 URL: http://svn.apache.org/viewvc?rev=629708&view=rev Log: Fixes for Hsqldb, Derby, MySql, Oracle platforms for the change column tests
Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySql50Builder.java Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/derby/DerbyBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbPlatform.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySql50Platform.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlPlatform.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/oracle/Oracle8Builder.java db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestAddColumn.java db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestChangeColumn.java Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java?rev=629708&r1=629707&r2=629708&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java Wed Feb 20 23:57:25 2008 @@ -1379,9 +1379,21 @@ */ protected String getSqlType(Column column) { - String nativeType = getNativeType(column); - int sizePos = nativeType.indexOf(SIZE_PLACEHOLDER); - StringBuffer sqlType = new StringBuffer(); + return getSqlType(column, getNativeType(column)); + } + + /** + * Returns the full SQL type specification (including size and precision/scale) for the + * given column. + * + * @param column The column + * @param nativeType Overrides the native type of the column; can include the size placeholder + * @return The full SQL type string including the size + */ + protected String getSqlType(Column column, String nativeType) + { + int sizePos = nativeType.indexOf(SIZE_PLACEHOLDER); + StringBuffer sqlType = new StringBuffer(); sqlType.append(sizePos >= 0 ? nativeType.substring(0, sizePos) : nativeType); Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/derby/DerbyBuilder.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/derby/DerbyBuilder.java?rev=629708&r1=629707&r2=629708&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/derby/DerbyBuilder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/derby/DerbyBuilder.java Wed Feb 20 23:57:25 2008 @@ -23,6 +23,7 @@ import java.sql.Types; import org.apache.ddlutils.Platform; +import org.apache.ddlutils.alteration.ColumnDefinitionChange; import org.apache.ddlutils.model.Column; import org.apache.ddlutils.model.Index; import org.apache.ddlutils.model.Table; @@ -88,15 +89,11 @@ */ protected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException { - String sourceNativeType = getBareNativeType(sourceColumn); - String targetNativeType = getBareNativeType(targetColumn); - - if (sourceNativeType.equals(targetNativeType)) - { - printIdentifier(getColumnName(sourceColumn)); - } - else + if (ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn) || + ColumnDefinitionChange.isTypeChanged(getPlatformInfo(), sourceColumn, targetColumn)) { + String targetNativeType = getNativeType(targetColumn); + // Derby currently has the limitation that it cannot convert numeric values // to VARCHAR, though it can convert them to CHAR if (TypeMap.isNumericType(sourceColumn.getTypeCode()) && @@ -105,10 +102,15 @@ targetNativeType = "CHAR"; } - print(targetNativeType); - print("("); + print("CAST ("); printIdentifier(getColumnName(sourceColumn)); + print(" AS "); + print(getSqlType(targetColumn, targetNativeType)); print(")"); + } + else + { + printIdentifier(getColumnName(sourceColumn)); } } } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbBuilder.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbBuilder.java?rev=629708&r1=629707&r2=629708&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbBuilder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbBuilder.java Wed Feb 20 23:57:25 2008 @@ -22,7 +22,9 @@ import java.io.IOException; import org.apache.ddlutils.Platform; +import org.apache.ddlutils.alteration.ColumnDefinitionChange; import org.apache.ddlutils.model.Column; +import org.apache.ddlutils.model.ModelException; import org.apache.ddlutils.model.Table; import org.apache.ddlutils.platform.SqlBuilder; @@ -101,4 +103,68 @@ printIdentifier(getColumnName(column)); printEndOfStatement(); } + + /** + * [EMAIL PROTECTED] + */ + protected void writeColumn(Table table, Column column) throws IOException + { + //see comments in columnsDiffer about null/"" defaults + printIdentifier(getColumnName(column)); + print(" "); + print(getSqlType(column)); + if (column.isAutoIncrement()) + { + if (!column.isPrimaryKey()) + { + throw new ModelException("Column "+column.getName()+" in table "+table.getName()+" is auto-incrementing but not a primary key column, which is not supported by the platform"); + } + print(" "); + writeColumnAutoIncrementStmt(table, column); + } + else + { + writeColumnDefaultValueStmt(table, column); + } + if (column.isRequired()) + { + print(" "); + writeColumnNotNullableStmt(); + } + else if (getPlatformInfo().isNullAsDefaultValueRequired() && + getPlatformInfo().hasNullDefault(column.getTypeCode())) + { + print(" "); + writeColumnNullableStmt(); + } + } + + /** + * [EMAIL PROTECTED] + */ + protected void writeColumnAutoIncrementStmt(Table table, Column column) throws IOException + { + print("GENERATED BY DEFAULT AS IDENTITY(START WITH 1)"); + } + + /** + * [EMAIL PROTECTED] + */ + protected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException + { + if (ColumnDefinitionChange.isTypeChanged(getPlatformInfo(), sourceColumn, targetColumn) || + ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn)) + { + print("CAST("); + printIdentifier(getColumnName(sourceColumn)); + print(" AS "); + print(getSqlType(targetColumn)); + print(")"); + } + else { + super.writeCastExpression(sourceColumn, targetColumn); + } + } + + } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbPlatform.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbPlatform.java?rev=629708&r1=629707&r2=629708&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbPlatform.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/hsqldb/HsqlDbPlatform.java Wed Feb 20 23:57:25 2008 @@ -60,6 +60,7 @@ { PlatformInfo info = getPlatformInfo(); + info.setDefaultValueUsedForIdentitySpec(true); info.setNonPrimaryKeyIdentityColumnsSupported(false); info.setIdentityOverrideAllowed(false); info.setSystemForeignKeyIndicesAlwaysNonUnique(true); Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySql50Builder.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySql50Builder.java?rev=629708&view=auto ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySql50Builder.java (added) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySql50Builder.java Wed Feb 20 23:57:25 2008 @@ -0,0 +1,53 @@ +package org.apache.ddlutils.platform.mysql; + +import java.io.IOException; + +import org.apache.ddlutils.Platform; +import org.apache.ddlutils.model.Table; + +/* + * 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. + */ + +/** + * The SQL Builder for MySQL version 5 and above. + * + * @version $Revision: $ + */ +public class MySql50Builder extends MySqlBuilder +{ + /** + * Creates a new builder instance. + * + * @param platform The plaftform this builder belongs to + */ + public MySql50Builder(Platform platform) + { + super(platform); + } + + /** + * [EMAIL PROTECTED] + */ + protected void copyData(Table sourceTable, Table targetTable) throws IOException + { + print("SET sql_mode=''"); + printEndOfStatement(); + super.copyData(sourceTable, targetTable); + } +} Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySql50Platform.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySql50Platform.java?rev=629708&r1=629707&r2=629708&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySql50Platform.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySql50Platform.java Wed Feb 20 23:57:25 2008 @@ -44,6 +44,7 @@ // which is different from the MySql 4 behaviour info.setSyntheticDefaultValueForRequiredReturned(false); + setSqlBuilder(new MySql50Builder(this)); setModelReader(new MySql50ModelReader(this)); } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlBuilder.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlBuilder.java?rev=629708&r1=629707&r2=629708&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlBuilder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlBuilder.java Wed Feb 20 23:57:25 2008 @@ -20,14 +20,18 @@ */ import java.io.IOException; +import java.sql.Types; import java.util.Iterator; import java.util.Map; import org.apache.ddlutils.Platform; +import org.apache.ddlutils.alteration.ColumnDefinitionChange; import org.apache.ddlutils.model.Column; import org.apache.ddlutils.model.ForeignKey; import org.apache.ddlutils.model.Table; +import org.apache.ddlutils.model.TypeMap; import org.apache.ddlutils.platform.SqlBuilder; +import org.apache.ddlutils.util.Jdbc3Utils; /** * The SQL Builder for MySQL. @@ -223,5 +227,83 @@ print("MODIFY COLUMN "); writeColumn(table, column); printEndOfStatement(); + } + + /** + * [EMAIL PROTECTED] + */ + protected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException + { + boolean sizeChanged = ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn); + boolean typeChanged = ColumnDefinitionChange.isTypeChanged(getPlatformInfo(), sourceColumn, targetColumn); + + if (sizeChanged || typeChanged) + { + String targetNativeType = getNativeType(targetColumn); + + switch (targetColumn.getTypeCode()) + { + case Types.BIT: + case Types.TINYINT: + case Types.SMALLINT: + case Types.INTEGER: + case Types.BIGINT: + targetNativeType = "SIGNED"; + break; + case Types.FLOAT: + case Types.REAL: + case Types.DOUBLE: + targetNativeType = "SIGNED"; // ? + break; + case Types.DECIMAL: + case Types.NUMERIC: + targetNativeType = "DECIMAL"; + break; + case Types.DATE: + targetNativeType = "DATE"; + break; + case Types.TIMESTAMP: + targetNativeType = "DATETIME"; + break; + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + case Types.CLOB: + targetNativeType = "CHAR"; + break; + default: + if (Jdbc3Utils.supportsJava14JdbcTypes() && + (targetColumn.getTypeCode() == Jdbc3Utils.determineBooleanTypeCode())) + { + targetNativeType = "SIGNED"; + } + else + { + targetNativeType = "BINARY"; + } + break; + } + + print("CAST("); + if (TypeMap.isTextType(sourceColumn.getTypeCode()) && TypeMap.isTextType(targetColumn.getTypeCode()) && sizeChanged) + { + print("LEFT("); + printIdentifier(getColumnName(sourceColumn)); + print(","); + print(targetColumn.getSize()); + print(")"); + } + else + { + printIdentifier(getColumnName(sourceColumn)); + } + print(" AS "); + print(getSqlType(targetColumn, targetNativeType)); + print(")"); + } + else + { + printIdentifier(getColumnName(sourceColumn)); + } } } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlPlatform.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlPlatform.java?rev=629708&r1=629707&r2=629708&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlPlatform.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySqlPlatform.java Wed Feb 20 23:57:25 2008 @@ -140,10 +140,17 @@ return !addColumnChange.getNewColumn().isAutoIncrement(); } + else if (change instanceof ColumnDefinitionChange) + { + ColumnDefinitionChange colDefChange = (ColumnDefinitionChange)change; + Column sourceColumn = intermediateTable.findColumn(colDefChange.getChangedColumn(), isDelimitedIdentifierModeOn()); + + return !ColumnDefinitionChange.isTypeChanged(getPlatformInfo(), sourceColumn, colDefChange.getNewColumn()) && + !ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, colDefChange.getNewColumn()); + } else { - return (change instanceof ColumnDefinitionChange) || - (change instanceof RemoveColumnChange) || + return (change instanceof RemoveColumnChange) || (change instanceof AddPrimaryKeyChange) || (change instanceof PrimaryKeyChange) || (change instanceof RemovePrimaryKeyChange); Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/oracle/Oracle8Builder.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/oracle/Oracle8Builder.java?rev=629708&r1=629707&r2=629708&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/oracle/Oracle8Builder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/oracle/Oracle8Builder.java Wed Feb 20 23:57:25 2008 @@ -25,6 +25,7 @@ import org.apache.ddlutils.DdlUtilsException; import org.apache.ddlutils.Platform; +import org.apache.ddlutils.alteration.ColumnDefinitionChange; import org.apache.ddlutils.model.Column; import org.apache.ddlutils.model.Database; import org.apache.ddlutils.model.Index; @@ -32,6 +33,7 @@ import org.apache.ddlutils.model.TypeMap; import org.apache.ddlutils.platform.SqlBuilder; import org.apache.ddlutils.util.Jdbc3Utils; +import org.apache.ddlutils.util.StringUtils; import org.apache.oro.text.regex.MalformedPatternException; import org.apache.oro.text.regex.Pattern; import org.apache.oro.text.regex.PatternCompiler; @@ -405,5 +407,38 @@ printIndent(); print("DROP PRIMARY KEY"); printEndOfStatement(); + } + + /** + * [EMAIL PROTECTED] + */ + protected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException + { + boolean sizeChanged = TypeMap.isTextType(targetColumn.getTypeCode()) && + ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn) && + !StringUtils.isEmpty(targetColumn.getSize()); + + if (sizeChanged) + { + print("SUBSTR("); + } + if (ColumnDefinitionChange.isTypeChanged(getPlatformInfo(), sourceColumn, targetColumn)) + { + print("CAST ("); + printIdentifier(getColumnName(sourceColumn)); + print(" AS "); + print(getSqlType(targetColumn)); + print(")"); + } + else + { + printIdentifier(getColumnName(sourceColumn)); + } + if (sizeChanged) + { + print(",0,"); + print(targetColumn.getSize()); + print(")"); + } } } Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestAddColumn.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestAddColumn.java?rev=629708&r1=629707&r2=629708&view=diff ============================================================================== --- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestAddColumn.java (original) +++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestAddColumn.java Wed Feb 20 23:57:25 2008 @@ -1454,6 +1454,7 @@ List beans = getRows("roundtrip"); + // TODO if (MySqlPlatform.DATABASENAME.equals(getPlatform().getName()) || MySql50Platform.DATABASENAME.equals(getPlatform().getName())) {