Author: tomdz Date: Sat Mar 17 20:58:27 2007 New Revision: 519537 URL: http://svn.apache.org/viewvc?view=rev&rev=519537 Log: Created a proper SapDb platform
Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/maxdb/MaxDbBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/maxdb/MaxDbModelReader.java db/ddlutils/trunk/src/test/jdbc.properties.sapdb Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformUtils.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/maxdb/MaxDbPlatform.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sapdb/SapDbBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sapdb/SapDbModelReader.java db/ddlutils/trunk/src/test/jdbc.properties.maxdb db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestPlatformUtils.java db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestSapDbPlatform.java Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformUtils.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformUtils.java?view=diff&rev=519537&r1=519536&r2=519537 ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformUtils.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformUtils.java Sat Mar 17 20:58:27 2007 @@ -35,7 +35,6 @@ import org.apache.ddlutils.platform.firebird.FirebirdPlatform; import org.apache.ddlutils.platform.hsqldb.HsqlDbPlatform; import org.apache.ddlutils.platform.interbase.InterbasePlatform; -import org.apache.ddlutils.platform.maxdb.MaxDbPlatform; import org.apache.ddlutils.platform.mckoi.MckoiPlatform; import org.apache.ddlutils.platform.mssql.MSSqlPlatform; import org.apache.ddlutils.platform.mysql.MySqlPlatform; @@ -150,7 +149,7 @@ jdbcSubProtocolToPlatform.put(FirebirdPlatform.JDBC_SUBPROTOCOL, FirebirdPlatform.DATABASENAME); jdbcSubProtocolToPlatform.put(HsqlDbPlatform.JDBC_SUBPROTOCOL, HsqlDbPlatform.DATABASENAME); jdbcSubProtocolToPlatform.put(InterbasePlatform.JDBC_SUBPROTOCOL, InterbasePlatform.DATABASENAME); - jdbcSubProtocolToPlatform.put(SapDbPlatform.JDBC_SUBPROTOCOL, MaxDbPlatform.DATABASENAME); + jdbcSubProtocolToPlatform.put(SapDbPlatform.JDBC_SUBPROTOCOL, SapDbPlatform.DATABASENAME); jdbcSubProtocolToPlatform.put(MckoiPlatform.JDBC_SUBPROTOCOL, MckoiPlatform.DATABASENAME); jdbcSubProtocolToPlatform.put(MSSqlPlatform.JDBC_SUBPROTOCOL, MSSqlPlatform.DATABASENAME); jdbcSubProtocolToPlatform.put(MSSqlPlatform.JDBC_SUBPROTOCOL_NEW, MSSqlPlatform.DATABASENAME); @@ -196,7 +195,7 @@ jdbcDriverToPlatform.put(FirebirdPlatform.JDBC_DRIVER, FirebirdPlatform.DATABASENAME); jdbcDriverToPlatform.put(HsqlDbPlatform.JDBC_DRIVER, HsqlDbPlatform.DATABASENAME); jdbcDriverToPlatform.put(InterbasePlatform.JDBC_DRIVER, InterbasePlatform.DATABASENAME); - jdbcDriverToPlatform.put(SapDbPlatform.JDBC_DRIVER, MaxDbPlatform.DATABASENAME); + jdbcDriverToPlatform.put(SapDbPlatform.JDBC_DRIVER, SapDbPlatform.DATABASENAME); jdbcDriverToPlatform.put(MckoiPlatform.JDBC_DRIVER, MckoiPlatform.DATABASENAME); jdbcDriverToPlatform.put(MSSqlPlatform.JDBC_DRIVER, MSSqlPlatform.DATABASENAME); jdbcDriverToPlatform.put(MSSqlPlatform.JDBC_DRIVER_NEW, MSSqlPlatform.DATABASENAME); Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/maxdb/MaxDbBuilder.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/maxdb/MaxDbBuilder.java?view=auto&rev=519537 ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/maxdb/MaxDbBuilder.java (added) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/maxdb/MaxDbBuilder.java Sat Mar 17 20:58:27 2007 @@ -0,0 +1,102 @@ +package org.apache.ddlutils.platform.maxdb; + +/* + * 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.io.IOException; + +import org.apache.ddlutils.Platform; +import org.apache.ddlutils.model.Column; +import org.apache.ddlutils.model.Database; +import org.apache.ddlutils.model.ForeignKey; +import org.apache.ddlutils.model.Table; +import org.apache.ddlutils.platform.sapdb.SapDbBuilder; + +/** + * The SQL Builder for MaxDB. + * + * @version $Revision: $ + */ +public class MaxDbBuilder extends SapDbBuilder +{ + /** + * Creates a new builder instance. + * + * @param platform The plaftform this builder belongs to + */ + public MaxDbBuilder(Platform platform) + { + super(platform); + } + + /** + * [EMAIL PROTECTED] + */ + protected void writeExternalPrimaryKeysCreateStmt(Table table, Column[] primaryKeyColumns) throws IOException + { + if ((primaryKeyColumns.length > 0) && shouldGeneratePrimaryKeys(primaryKeyColumns)) + { + print("ALTER TABLE "); + printlnIdentifier(getTableName(table)); + printIndent(); + print("ADD CONSTRAINT "); + printIdentifier(getConstraintName(null, table, "PK", null)); + print(" "); + writePrimaryKeyStmt(table, primaryKeyColumns); + printEndOfStatement(); + } + } + + /** + * [EMAIL PROTECTED] + */ + protected void writeExternalForeignKeyCreateStmt(Database database, Table table, ForeignKey key) throws IOException + { + if (key.getForeignTableName() == null) + { + _log.warn("Foreign key table is null for key " + key); + } + else + { + writeTableAlterStmt(table); + + print("ADD CONSTRAINT "); + printIdentifier(getForeignKeyName(table, key)); + print(" FOREIGN KEY ("); + writeLocalReferences(key); + print(") REFERENCES "); + printIdentifier(getTableName(database.findTable(key.getForeignTableName()))); + print(" ("); + writeForeignReferences(key); + print(")"); + printEndOfStatement(); + } + } + + /** + * [EMAIL PROTECTED] + */ + protected void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey) throws IOException + { + writeTableAlterStmt(table); + print("DROP CONSTRAINT "); + printIdentifier(getForeignKeyName(table, foreignKey)); + printEndOfStatement(); + } +} Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/maxdb/MaxDbModelReader.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/maxdb/MaxDbModelReader.java?view=auto&rev=519537 ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/maxdb/MaxDbModelReader.java (added) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/maxdb/MaxDbModelReader.java Sat Mar 17 20:58:27 2007 @@ -0,0 +1,85 @@ +package org.apache.ddlutils.platform.maxdb; + +/* + * 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.SQLException; +import java.sql.Types; +import java.util.Map; + +import org.apache.ddlutils.Platform; +import org.apache.ddlutils.model.Column; +import org.apache.ddlutils.platform.DatabaseMetaDataWrapper; +import org.apache.ddlutils.platform.JdbcModelReader; + +/** + * Reads a database model from a MaxDb database. + * + * @version $Revision: $ + */ +public class MaxDbModelReader extends JdbcModelReader +{ + /** + * Creates a new model reader for MaxDb databases. + * + * @param platform The platform that this model reader belongs to + */ + public MaxDbModelReader(Platform platform) + { + super(platform); + setDefaultCatalogPattern(null); + setDefaultSchemaPattern(null); + setDefaultTablePattern("%"); + } + + /** + * [EMAIL PROTECTED] + */ + protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException + { + Column column = super.readColumn(metaData, values); + + if (column.getDefaultValue() != null) + { + // SapDb pads the default value with spaces + column.setDefaultValue(column.getDefaultValue().trim()); + // SapDb uses the default value for the auto-increment specification + if (column.getDefaultValue().startsWith("DEFAULT SERIAL")) + { + column.setAutoIncrement(true); + column.setDefaultValue(null); + } + } + if (column.getTypeCode() == Types.DECIMAL) + { + // For some reason, the size will be reported with 2 byte more, e.g. 17 instead of 15 + // So we have to adjust the size here + if (column.getSizeAsInt() > 2) + { + column.setSizeAndScale(column.getSizeAsInt() - 2, column.getScale()); + } + // We also perform back-mapping to BIGINT + if ((column.getSizeAsInt() == 38) && (column.getScale() == 0)) + { + column.setTypeCode(Types.BIGINT); + } + } + return column; + } +} Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/maxdb/MaxDbPlatform.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/maxdb/MaxDbPlatform.java?view=diff&rev=519537&r1=519536&r2=519537 ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/maxdb/MaxDbPlatform.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/maxdb/MaxDbPlatform.java Sat Mar 17 20:58:27 2007 @@ -35,6 +35,16 @@ public static final String DATABASENAME = "MaxDB"; /** + * Creates a new platform instance. + */ + public MaxDbPlatform() + { + super(); + setSqlBuilder(new MaxDbBuilder(this)); + setModelReader(new MaxDbModelReader(this)); + } + + /** * [EMAIL PROTECTED] */ public String getName() Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sapdb/SapDbBuilder.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sapdb/SapDbBuilder.java?view=diff&rev=519537&r1=519536&r2=519537 ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sapdb/SapDbBuilder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sapdb/SapDbBuilder.java Sat Mar 17 20:58:27 2007 @@ -35,6 +35,7 @@ import org.apache.ddlutils.alteration.TableChange; import org.apache.ddlutils.model.Column; import org.apache.ddlutils.model.Database; +import org.apache.ddlutils.model.ForeignKey; import org.apache.ddlutils.model.Table; import org.apache.ddlutils.platform.SqlBuilder; @@ -73,6 +74,60 @@ protected void writeColumnAutoIncrementStmt(Table table, Column column) throws IOException { print("DEFAULT SERIAL(1)"); + } + + /** + * [EMAIL PROTECTED] + */ + protected void writeExternalPrimaryKeysCreateStmt(Table table, Column[] primaryKeyColumns) throws IOException + { + // Note that SapDB does not support the addition of named primary keys + if ((primaryKeyColumns.length > 0) && shouldGeneratePrimaryKeys(primaryKeyColumns)) + { + print("ALTER TABLE "); + printlnIdentifier(getTableName(table)); + printIndent(); + print("ADD "); + writePrimaryKeyStmt(table, primaryKeyColumns); + printEndOfStatement(); + } + } + + /** + * [EMAIL PROTECTED] + */ + protected void writeExternalForeignKeyCreateStmt(Database database, Table table, ForeignKey key) throws IOException + { + if (key.getForeignTableName() == null) + { + _log.warn("Foreign key table is null for key " + key); + } + else + { + writeTableAlterStmt(table); + + print(" ADD FOREIGN KEY "); + printIdentifier(getForeignKeyName(table, key)); + print(" ("); + writeLocalReferences(key); + print(") REFERENCES "); + printIdentifier(getTableName(database.findTable(key.getForeignTableName()))); + print(" ("); + writeForeignReferences(key); + print(")"); + printEndOfStatement(); + } + } + + /** + * [EMAIL PROTECTED] + */ + protected void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey) throws IOException + { + writeTableAlterStmt(table); + print("DROP FOREIGN KEY "); + printIdentifier(getForeignKeyName(table, foreignKey)); + printEndOfStatement(); } /** Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sapdb/SapDbModelReader.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sapdb/SapDbModelReader.java?view=diff&rev=519537&r1=519536&r2=519537 ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sapdb/SapDbModelReader.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/sapdb/SapDbModelReader.java Sat Mar 17 20:58:27 2007 @@ -68,12 +68,6 @@ } if (column.getTypeCode() == Types.DECIMAL) { - // For some reason, the size will be reported with 2 byte more, e.g. 17 instead of 15 - // So we have to adjust the size here - if (column.getSizeAsInt() > 2) - { - column.setSizeAndScale(column.getSizeAsInt() - 2, column.getScale()); - } // We also perform back-mapping to BIGINT if ((column.getSizeAsInt() == 38) && (column.getScale() == 0)) { Modified: db/ddlutils/trunk/src/test/jdbc.properties.maxdb URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/jdbc.properties.maxdb?view=diff&rev=519537&r1=519536&r2=519537 ============================================================================== --- db/ddlutils/trunk/src/test/jdbc.properties.maxdb (original) +++ db/ddlutils/trunk/src/test/jdbc.properties.maxdb Sat Mar 17 20:58:27 2007 @@ -1,9 +1,9 @@ -# JDBC properties for SapDB/MaxDB +# JDBC properties for MaxDB # Note: Properties starting with "datasource." will be fed into the datasource instance of the # class configured via the datasource.class property -# Use this property if ddlutils does not recognize the platform from the settings -#ddlutils.platform=SapDB +# You need to use this property, otherwise DdlUtils will detect SapDB instead (since the JDBC driver is the same) +ddlutils.platform=MaxDB # # Using the plain DBCP datasource Added: db/ddlutils/trunk/src/test/jdbc.properties.sapdb URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/jdbc.properties.sapdb?view=auto&rev=519537 ============================================================================== --- db/ddlutils/trunk/src/test/jdbc.properties.sapdb (added) +++ db/ddlutils/trunk/src/test/jdbc.properties.sapdb Sat Mar 17 20:58:27 2007 @@ -0,0 +1,16 @@ +# JDBC properties for SapDB +# Note: Properties starting with "datasource." will be fed into the datasource instance of the +# class configured via the datasource.class property + +# Use this property if ddlutils does not recognize the platform from the settings +#ddlutils.platform=SapDB + +# +# Using the plain DBCP datasource +# + +datasource.class=org.apache.commons.dbcp.BasicDataSource +datasource.driverClassName=com.sap.dbtech.jdbc.DriverSapDB +datasource.url=jdbc:sapdb://localhost/ddlutils +datasource.username=dba +datasource.password=dba Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestPlatformUtils.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestPlatformUtils.java?view=diff&rev=519537&r1=519536&r2=519537 ============================================================================== --- db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestPlatformUtils.java (original) +++ db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestPlatformUtils.java Sat Mar 17 20:58:27 2007 @@ -19,6 +19,8 @@ * under the License. */ +import junit.framework.TestCase; + import org.apache.ddlutils.PlatformUtils; import org.apache.ddlutils.platform.axion.AxionPlatform; import org.apache.ddlutils.platform.cloudscape.CloudscapePlatform; @@ -27,16 +29,14 @@ import org.apache.ddlutils.platform.firebird.FirebirdPlatform; import org.apache.ddlutils.platform.hsqldb.HsqlDbPlatform; import org.apache.ddlutils.platform.interbase.InterbasePlatform; -import org.apache.ddlutils.platform.maxdb.MaxDbPlatform; import org.apache.ddlutils.platform.mckoi.MckoiPlatform; import org.apache.ddlutils.platform.mssql.MSSqlPlatform; import org.apache.ddlutils.platform.mysql.MySqlPlatform; import org.apache.ddlutils.platform.oracle.Oracle8Platform; import org.apache.ddlutils.platform.postgresql.PostgreSqlPlatform; +import org.apache.ddlutils.platform.sapdb.SapDbPlatform; import org.apache.ddlutils.platform.sybase.SybasePlatform; -import junit.framework.TestCase; - /** * Tests the [EMAIL PROTECTED] org.apache.ddlutils.PlatformUtils} class. * @@ -372,20 +372,20 @@ } /** - * Tests the determination of the MaxDb platform via its JDBC driver. + * Tests the determination of the SapDb platform via its JDBC driver. */ - public void testMaxDbDriver() + public void testSapDbDriver() { - assertEquals(MaxDbPlatform.DATABASENAME, + assertEquals(SapDbPlatform.DATABASENAME, _platformUtils.determineDatabaseType("com.sap.dbtech.jdbc.DriverSapDB", null)); } /** - * Tests the determination of the MaxDb platform via JDBC connection urls. + * Tests the determination of the SapDb platform via JDBC connection urls. */ - public void testMaxDbUrl() + public void testSapDbUrl() { - assertEquals(MaxDbPlatform.DATABASENAME, + assertEquals(SapDbPlatform.DATABASENAME, _platformUtils.determineDatabaseType(null, "jdbc:sapdb://servermachine:9876/TST")); } Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestSapDbPlatform.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestSapDbPlatform.java?view=diff&rev=519537&r1=519536&r2=519537 ============================================================================== --- db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestSapDbPlatform.java (original) +++ db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestSapDbPlatform.java Sat Mar 17 20:58:27 2007 @@ -117,8 +117,8 @@ public void testTableConstraints() throws Exception { assertEqualsIgnoringWhitespaces( - "ALTER TABLE \"table3\" DROP CONSTRAINT \"testfk\";\n"+ - "ALTER TABLE \"table2\" DROP CONSTRAINT \"table2_FK_COL_FK_COL_FK_2_table1\";\n"+ + "ALTER TABLE \"table3\" DROP FOREIGN KEY \"testfk\";\n"+ + "ALTER TABLE \"table2\" DROP FOREIGN KEY \"table2_FK_COL_FK_COL_FK_2_table1\";\n"+ "DROP TABLE \"table3\" CASCADE;\n"+ "DROP TABLE \"table2\" CASCADE;\n"+ "DROP TABLE \"table1\" CASCADE;\n"+ @@ -146,8 +146,8 @@ " \"COL_FK\" INTEGER NOT NULL,\n"+ " PRIMARY KEY (\"COL_PK\")\n"+ ");\n"+ - "ALTER TABLE \"table2\" ADD CONSTRAINT \"table2_FK_COL_FK_COL_FK_2_table1\" FOREIGN KEY (\"COL_FK_1\", \"COL_FK_2\") REFERENCES \"table1\" (\"COL_PK_2\", \"COL_PK_1\");\n"+ - "ALTER TABLE \"table3\" ADD CONSTRAINT \"testfk\" FOREIGN KEY (\"COL_FK\") REFERENCES \"table2\" (\"COL_PK\");\n", + "ALTER TABLE \"table2\" ADD FOREIGN KEY \"table2_FK_COL_FK_COL_FK_2_table1\" (\"COL_FK_1\", \"COL_FK_2\") REFERENCES \"table1\" (\"COL_PK_2\", \"COL_PK_1\");\n"+ + "ALTER TABLE \"table3\" ADD FOREIGN KEY \"testfk\" (\"COL_FK\") REFERENCES \"table2\" (\"COL_PK\");\n", createTestDatabase(TABLE_CONSTRAINT_TEST_SCHEMA)); }