Author: tomdz Date: Sat Nov 26 10:33:39 2005 New Revision: 349134 URL: http://svn.apache.org/viewcvs?rev=349134&view=rev Log: Implemented DDLUTILS-32
Fixed some bugs Updated docs Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/CreationParameters.java db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommandWithCreationParameters.java db/ddlutils/trunk/src/java/org/apache/ddlutils/task/TableSpecificParameter.java Modified: db/ddlutils/trunk/src/doc/src/documentation/content/xdocs/ant-tasks.xml db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java db/ddlutils/trunk/src/java/org/apache/ddlutils/io/package.html db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/InterbaseBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MSSqlBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/OracleBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PostgreSqlBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SybaseBuilder.java db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToDatabaseCommand.java db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestMySqlPlatform.java Modified: db/ddlutils/trunk/src/doc/src/documentation/content/xdocs/ant-tasks.xml URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/doc/src/documentation/content/xdocs/ant-tasks.xml?rev=349134&r1=349133&r2=349134&view=diff ============================================================================== --- db/ddlutils/trunk/src/doc/src/documentation/content/xdocs/ant-tasks.xml (original) +++ db/ddlutils/trunk/src/doc/src/documentation/content/xdocs/ant-tasks.xml Sat Nov 26 10:33:39 2005 @@ -358,6 +358,66 @@ <td>See above.</td> </tr> </table> + <section> + <title>Subelement: parameter</title> + <p> + Specifies a parameter for the creation of the tables. These are usually platform specific. + If no table name is specified, the parameter is applied to all tables. + </p> + <note> + Parameters are only applied when creating new tables, not when altering existing ones. + </note> + <table> + <tr> + <th>Attribute</th> + <th>Required ?</th> + <th>Possible values</th> + <th>Default value</th> + <th>Meaning</th> + </tr> + <tr> + <td>name</td> + <td>yes</td> + <td></td> + <td></td> + <td>Specifies the name of the parameter. See <a href="site:documentation/database-support">here</a> + for the parameters supported by the individual platforms.</td> + </tr> + <tr> + <td>table</td> + <td>no</td> + <td></td> + <td></td> + <td>Specifies the name of the table where this parameter shall be applied.</td> + </tr> + <tr> + <td>tables</td> + <td>no</td> + <td></td> + <td></td> + <td>Specifies the comma-separated list of table names where this parameter shall be applied.</td> + </tr> + <tr> + <td>value</td> + <td>no</td> + <td></td> + <td></td> + <td>The parameter value. If none is given, <code>null</code> is used.</td> + </tr> + <tr> + <td>platforms</td> + <td>no</td> + <td></td> + <td></td> + <td> + Comma-separated list of platforms where the parameter shall be processed (see + <code>databaseType</code> attribute above for the possible values). For every platform + not in this list, the parameter is ignored. If none is given, then the parameter + is processed for every platform. + </td> + </tr> + </table> + </section> </section> <section> <title>Subtask: writeSchemaSqlToFile</title> @@ -404,6 +464,12 @@ <td>The name of the file to write the SQL commands to.</td> </tr> </table> + <section> + <title>Subelement: parameter</title> + <p> + Same as for <code>writeSchemaToDatabase</code>. + </p> + </section> </section> <section> <title>Subtask: writeDataToDatabase</title> @@ -473,7 +539,7 @@ <td></td> <td></td> <td> - Specifies the fully qualified class name of the parameter. Note that the class is + Specifies the fully qualified class name of the converter. Note that the class is required to implement <a href="ext:ddlutils/javadoc/converter">org.apache.ddlutils.io.converters.SqlTypeConverter</a>. </td> Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java?rev=349134&r1=349133&r2=349134&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java Sat Nov 26 10:33:39 2005 @@ -27,6 +27,7 @@ import org.apache.commons.beanutils.DynaBean; import org.apache.ddlutils.model.Database; import org.apache.ddlutils.model.Table; +import org.apache.ddlutils.platform.CreationParameters; import org.apache.ddlutils.platform.JdbcModelReader; import org.apache.ddlutils.platform.SqlBuilder; @@ -198,6 +199,27 @@ public void createTables(Connection connection, Database model, boolean dropTablesFirst, boolean continueOnError) throws DynaSqlException; /** + * Creates the tables defined in the database model. + * + * @param model The database model + * @param params The parameters used in the creation + * @param dropTablesFirst Whether to drop the tables prior to creating them (anew) + * @param continueOnError Whether to continue executing the sql commands when an error occurred + */ + public void createTables(Database model, CreationParameters params, boolean dropTablesFirst, boolean continueOnError) throws DynaSqlException; + + /** + * Creates the tables defined in the database model. + * + * @param connection The connection to the database + * @param model The database model + * @param params The parameters used in the creation + * @param dropTablesFirst Whether to drop the tables prior to creating them (anew) + * @param continueOnError Whether to continue executing the sql commands when an error occurred + */ + public void createTables(Connection connection, Database model, CreationParameters params, boolean dropTablesFirst, boolean continueOnError) throws DynaSqlException; + + /** * Alters the database schema so that it match the given model. Drops and table modifications will * not be made. * @@ -221,6 +243,28 @@ * Alters the database schema so that it match the given model. Drops and table modifications will * not be made. * + * @param desiredDb The desired database schema + * @param params The parameters used in the creation + * @param continueOnError Whether to continue with the next sql statement when an error occurred + */ + public void alterTables(Database desiredDb, CreationParameters params, boolean continueOnError) throws DynaSqlException; + + /** + * Alters the database schema so that it match the given model. + * + * @param desiredDb The desired database schema + * @param params The parameters used in the creation + * @param doDrops Whether columns, tables and indexes should be dropped if not in the + * new schema + * @param modifyColumns Whether columns should be altered for datatype, size as required + * @param continueOnError Whether to continue with the next sql statement when an error occurred + */ + public void alterTables(Database desiredDb, CreationParameters params, boolean doDrops, boolean modifyColumns, boolean continueOnError) throws DynaSqlException; + + /** + * Alters the database schema so that it match the given model. Drops and table modifications will + * not be made. + * * @param connection A connection to the existing database that shall be modified * @param desiredDb The desired database schema * @param continueOnError Whether to continue with the next sql statement when an error occurred @@ -238,6 +282,30 @@ * @param continueOnError Whether to continue with the next sql statement when an error occurred */ public void alterTables(Connection connection, Database desiredDb, boolean doDrops, boolean modifyColumns, boolean continueOnError) throws DynaSqlException; + + /** + * Alters the database schema so that it match the given model. Drops and table modifications will + * not be made. + * + * @param connection A connection to the existing database that shall be modified + * @param desiredDb The desired database schema + * @param params The parameters used in the creation + * @param continueOnError Whether to continue with the next sql statement when an error occurred + */ + public void alterTables(Connection connection, Database desiredDb, CreationParameters params, boolean continueOnError) throws DynaSqlException; + + /** + * Alters the database schema so that it match the given model. + * + * @param connection A connection to the existing database that shall be modified + * @param desiredDb The desired database schema + * @param params The parameters used in the creation + * @param doDrops Whether columns, tables and indexes should be dropped if not in the + * new schema + * @param modifyColumns Whether columns should be altered for datatype, size as required + * @param continueOnError Whether to continue with the next sql statement when an error occurred + */ + public void alterTables(Connection connection, Database desiredDb, CreationParameters params, boolean doDrops, boolean modifyColumns, boolean continueOnError) throws DynaSqlException; /** * Drops the tables defined in the given database. Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/io/package.html URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/io/package.html?rev=349134&r1=349133&r2=349134&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/io/package.html (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/io/package.html Sat Nov 26 10:33:39 2005 @@ -28,10 +28,5 @@ The DTD for these data XML files is generated via the [EMAIL PROTECTED] org.apache.ddlutils.io.DataDtdWriter} class. </p> - <p> - Also of interest is the [EMAIL PROTECTED] org.apache.ddlutils.io.JdbcModelReader} class, - which retrieves the database model from a live database. -<!-- TODO: Add reference to Platform facade method --> - </p> </body> </html> Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/CreationParameters.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/CreationParameters.java?rev=349134&view=auto ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/CreationParameters.java (added) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/CreationParameters.java Sat Nov 26 10:33:39 2005 @@ -0,0 +1,80 @@ +package org.apache.ddlutils.platform; + +/* + * Copyright 1999-2005 The Apache Software Foundation. + * + * Licensed 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.util.HashMap; +import java.util.Map; + +import org.apache.commons.collections.map.ListOrderedMap; +import org.apache.ddlutils.model.Table; + +/** + * Contains parameters used in the table creation. Note that the definition + * order is retained (per table), so if a parameter should be applied before + * some other parameter, then add it before the other one. + * + * @author Thomas Dudziak + * @version $Revision: 331006 $ + */ +public class CreationParameters +{ + /** The parameter maps keyed by the tables. */ + private Map _parametersPerTable = new HashMap(); + + /** + * Returns the parameters for the given table. + * + * @param table The table + * @return The parameters + */ + public Map getParametersFor(Table table) + { + ListOrderedMap result = new ListOrderedMap(); + Map globalParams = (Map)_parametersPerTable.get(null); + Map tableParams = (Map)_parametersPerTable.get(table); + + if (globalParams != null) + { + result.putAll(globalParams); + } + if (tableParams != null) + { + result.putAll(tableParams); + } + return result; + } + + /** + * Adds a parameter. + * + * @param table The table; if <code>null</code> then the parameter is for all tables + * @param paramName The name of the parameter + * @param paramValue The value of the parameter + */ + public void addParameter(Table table, String paramName, String paramValue) + { + Map params = (Map)_parametersPerTable.get(table); + + if (params == null) + { + // we're using a list orderered map to retain the order + params = new ListOrderedMap(); + _parametersPerTable.put(table, params); + } + params.put(paramName, paramValue); + } +} Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/InterbaseBuilder.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/InterbaseBuilder.java?rev=349134&r1=349133&r2=349134&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/InterbaseBuilder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/InterbaseBuilder.java Sat Nov 26 10:33:39 2005 @@ -18,6 +18,7 @@ import java.io.IOException; import java.sql.Types; +import java.util.Map; import org.apache.ddlutils.PlatformInfo; import org.apache.ddlutils.model.Column; @@ -69,9 +70,9 @@ /** * [EMAIL PROTECTED] */ - public void createTable(Database database, Table table) throws IOException + public void createTable(Database database, Table table, Map parameters) throws IOException { - super.createTable(database, table); + super.createTable(database, table, parameters); print("COMMIT"); printEndOfStatement(); Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MSSqlBuilder.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MSSqlBuilder.java?rev=349134&r1=349133&r2=349134&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MSSqlBuilder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MSSqlBuilder.java Sat Nov 26 10:33:39 2005 @@ -17,7 +17,7 @@ */ import java.io.IOException; -import java.util.HashMap; +import java.util.Map; import org.apache.ddlutils.PlatformInfo; import org.apache.ddlutils.model.Column; @@ -48,10 +48,10 @@ /** * [EMAIL PROTECTED] */ - public void createTable(Database database, Table table) throws IOException + public void createTable(Database database, Table table, Map parameters) throws IOException { writeQuotationOnStatement(); - super.createTable(database, table); + super.createTable(database, table, parameters); } /** @@ -187,7 +187,7 @@ /** * [EMAIL PROTECTED] */ - public String getDeleteSql(Table table, HashMap pkValues, boolean genPlaceholders) + public String getDeleteSql(Table table, Map pkValues, boolean genPlaceholders) { return getQuotationOnStatement() + super.getDeleteSql(table, pkValues, genPlaceholders); } @@ -195,7 +195,7 @@ /** * [EMAIL PROTECTED] */ - public String getInsertSql(Table table, HashMap columnValues, boolean genPlaceholders) + public String getInsertSql(Table table, Map columnValues, boolean genPlaceholders) { return getQuotationOnStatement() + super.getInsertSql(table, columnValues, genPlaceholders); } @@ -203,7 +203,7 @@ /** * [EMAIL PROTECTED] */ - public String getUpdateSql(Table table, HashMap columnValues, boolean genPlaceholders) + public String getUpdateSql(Table table, Map columnValues, boolean genPlaceholders) { return getQuotationOnStatement() + super.getUpdateSql(table, columnValues, genPlaceholders); } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlBuilder.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlBuilder.java?rev=349134&r1=349133&r2=349134&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlBuilder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlBuilder.java Sat Nov 26 10:33:39 2005 @@ -18,6 +18,8 @@ import java.io.IOException; import java.sql.Types; +import java.util.Iterator; +import java.util.Map; import org.apache.ddlutils.PlatformInfo; import org.apache.ddlutils.model.Column; @@ -106,4 +108,35 @@ { return "SELECT LAST_INSERT_ID()"; } + + /** + * [EMAIL PROTECTED] + */ + protected void writeTableCreationStmtEnding(Table table, Map parameters) throws IOException + { + if (parameters != null) + { + print(" "); + // MySql supports additional table creation options which are appended + // at the end of the CREATE TABLE statement + for (Iterator it = parameters.entrySet().iterator(); it.hasNext();) + { + Map.Entry entry = (Map.Entry)it.next(); + + print(entry.getKey().toString()); + if (entry.getValue() != null) + { + print("="); + print(entry.getValue().toString()); + } + if (it.hasNext()) + { + print(" "); + } + } + } + super.writeTableCreationStmtEnding(table, parameters); + } + + } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/OracleBuilder.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/OracleBuilder.java?rev=349134&r1=349133&r2=349134&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/OracleBuilder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/OracleBuilder.java Sat Nov 26 10:33:39 2005 @@ -18,6 +18,7 @@ import java.io.IOException; import java.sql.Types; +import java.util.Map; import org.apache.ddlutils.PlatformInfo; import org.apache.ddlutils.model.Column; @@ -77,7 +78,7 @@ /** * [EMAIL PROTECTED] */ - public void createTable(Database database, Table table) throws IOException + public void createTable(Database database, Table table, Map parameters) throws IOException { // lets create any sequences Column[] columns = table.getAutoIncrementColumns(); @@ -89,7 +90,7 @@ printEndOfStatement(); } - super.createTable(database, table); + super.createTable(database, table, parameters); for (int idx = 0; idx < columns.length; idx++) { Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java?rev=349134&r1=349133&r2=349134&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java Sat Nov 26 10:33:39 2005 @@ -234,6 +234,22 @@ /** * [EMAIL PROTECTED] */ + public void createDatabase(String jdbcDriverClassName, String connectionUrl, String username, String password, Map parameters) throws DynaSqlException, UnsupportedOperationException + { + throw new UnsupportedOperationException("Database creation is not supported for the database platform "+getName()); + } + + /** + * [EMAIL PROTECTED] + */ + public void dropDatabase(String jdbcDriverClassName, String connectionUrl, String username, String password) throws DynaSqlException, UnsupportedOperationException + { + throw new UnsupportedOperationException("Database deletion is not supported for the database platform "+getName()); + } + + /** + * [EMAIL PROTECTED] + */ public void createTables(Database model, boolean dropTablesFirst, boolean continueOnError) throws DynaSqlException { Connection connection = borrowConnection(); @@ -251,23 +267,46 @@ /** * [EMAIL PROTECTED] */ - public void createDatabase(String jdbcDriverClassName, String connectionUrl, String username, String password, Map parameters) throws DynaSqlException, UnsupportedOperationException + public void createTables(Connection connection, Database model, boolean dropTablesFirst, boolean continueOnError) throws DynaSqlException { - throw new UnsupportedOperationException("Database creation is not supported for the database platform "+getName()); + String sql = null; + + try + { + StringWriter buffer = new StringWriter(); + + getSqlBuilder().setWriter(buffer); + getSqlBuilder().createTables(model, dropTablesFirst); + sql = buffer.toString(); + } + catch (IOException e) + { + // won't happen because we're using a string writer + } + evaluateBatch(connection, sql, continueOnError); } /** * [EMAIL PROTECTED] */ - public void dropDatabase(String jdbcDriverClassName, String connectionUrl, String username, String password) throws DynaSqlException, UnsupportedOperationException + public void createTables(Database model, CreationParameters params, boolean dropTablesFirst, boolean continueOnError) throws DynaSqlException { - throw new UnsupportedOperationException("Database deletion is not supported for the database platform "+getName()); + Connection connection = borrowConnection(); + + try + { + createTables(connection, model, params, dropTablesFirst, continueOnError); + } + finally + { + returnConnection(connection); + } } /** * [EMAIL PROTECTED] */ - public void createTables(Connection connection, Database model, boolean dropTablesFirst, boolean continueOnError) throws DynaSqlException + public void createTables(Connection connection, Database model, CreationParameters params, boolean dropTablesFirst, boolean continueOnError) throws DynaSqlException { String sql = null; @@ -276,7 +315,7 @@ StringWriter buffer = new StringWriter(); getSqlBuilder().setWriter(buffer); - getSqlBuilder().createTables(model, dropTablesFirst); + getSqlBuilder().createTables(model, params, dropTablesFirst); sql = buffer.toString(); } catch (IOException e) @@ -314,6 +353,39 @@ /** * [EMAIL PROTECTED] */ + public void alterTables(Database desiredDb, CreationParameters params, boolean continueOnError) throws DynaSqlException + { + alterTables(desiredDb, params, false, false, continueOnError); + } + + /** + * [EMAIL PROTECTED] + */ + public void alterTables(Database desiredDb, CreationParameters params, boolean doDrops, boolean modifyColumns, boolean continueOnError) throws DynaSqlException + { + Connection connection = borrowConnection(); + + try + { + alterTables(connection, desiredDb, params, doDrops, modifyColumns, continueOnError); + } + finally + { + returnConnection(connection); + } + } + + /** + * [EMAIL PROTECTED] + */ + public void alterTables(Connection connection, Database desiredDb, boolean continueOnError) throws DynaSqlException + { + alterTables(connection, desiredDb, false, false, continueOnError); + } + + /** + * [EMAIL PROTECTED] + */ public void alterTables(Connection connection, Database desiredModel, boolean doDrops, boolean modifyColumns, boolean continueOnError) throws DynaSqlException { String sql = null; @@ -337,9 +409,32 @@ /** * [EMAIL PROTECTED] */ - public void alterTables(Connection connection, Database desiredDb, boolean continueOnError) throws DynaSqlException + public void alterTables(Connection connection, Database desiredDb, CreationParameters params, boolean continueOnError) throws DynaSqlException { - alterTables(connection, desiredDb, false, false, continueOnError); + alterTables(connection, desiredDb, params, false, false, continueOnError); + } + + /** + * [EMAIL PROTECTED] + */ + public void alterTables(Connection connection, Database desiredModel, CreationParameters params, boolean doDrops, boolean modifyColumns, boolean continueOnError) throws DynaSqlException + { + String sql = null; + Database currentModel = readModelFromDatabase(connection); + + try + { + StringWriter buffer = new StringWriter(); + + getSqlBuilder().setWriter(buffer); + getSqlBuilder().alterDatabase(currentModel, desiredModel, params, doDrops, modifyColumns); + sql = buffer.toString(); + } + catch (IOException ex) + { + // won't happen because we're using a string writer + } + evaluateBatch(connection, sql, continueOnError); } /** Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PostgreSqlBuilder.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PostgreSqlBuilder.java?rev=349134&r1=349133&r2=349134&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PostgreSqlBuilder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PostgreSqlBuilder.java Sat Nov 26 10:33:39 2005 @@ -17,6 +17,7 @@ */ import java.io.IOException; +import java.util.Map; import org.apache.ddlutils.PlatformInfo; import org.apache.ddlutils.model.Column; @@ -65,7 +66,7 @@ /** * [EMAIL PROTECTED] */ - public void createTable(Database database, Table table) throws IOException + public void createTable(Database database, Table table, Map parameters) throws IOException { for (int idx = 0; idx < table.getColumnCount(); idx++) { @@ -76,7 +77,7 @@ createAutoIncrementSequence(table, column); } } - super.createTable(database, table); + super.createTable(database, table, parameters); } /** Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java?rev=349134&r1=349133&r2=349134&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 Sat Nov 26 10:33:39 2005 @@ -232,7 +232,7 @@ */ public void createTables(Database database) throws IOException { - createTables(database, true); + createTables(database, null, true); } /** @@ -243,6 +243,18 @@ */ public void createTables(Database database, boolean dropTables) throws IOException { + createTables(database, null, dropTables); + } + + /** + * Outputs the DDL required to drop (if requested) and (re)create all tables in the database model. + * + * @param database The database + * @param params The parameters used in the creation + * @param dropTables Whether to drop tables before creating them + */ + public void createTables(Database database, CreationParameters params, boolean dropTables) throws IOException + { if (dropTables) { dropTables(database); @@ -253,7 +265,7 @@ Table table = database.getTable(idx); writeTableComment(table); - createTable(database, table); + createTable(database, table, (params == null ? null : params.getParametersFor(table))); } // we're writing the external foreignkeys last to ensure that all referenced tables are already defined @@ -285,6 +297,23 @@ */ public void alterDatabase(Database currentModel, Database desiredModel, boolean doDrops, boolean modifyColumns) throws IOException { + alterDatabase(currentModel, desiredModel, null, doDrops, modifyColumns); + } + + /** + * Generates the DDL to modify an existing database so the schema matches + * the current specified database schema. + * + * @param currentModel The current database schema + * @param desiredModel The desired database schema + * @param params The parameters used in the creation of new tables. Note that for existing + * tables, parameters won't be applied + * @param doDrops Whether columns and indexes should be dropped if not in the + * new schema + * @param modifyColumns Whether columns should be altered for datatype, size as required + */ + public void alterDatabase(Database currentModel, Database desiredModel, CreationParameters params, boolean doDrops, boolean modifyColumns) throws IOException + { ArrayList newTables = new ArrayList(); for (int tableIdx = 0; tableIdx < desiredModel.getTableCount(); tableIdx++) @@ -298,7 +327,7 @@ { _log.info("Creating table " + desiredTable.getName()); } - createTable(desiredModel, desiredTable); + createTable(desiredModel, desiredTable, params.getParametersFor(desiredTable)); // we're deferring foreignkey generation newTables.add(desiredTable); } @@ -527,6 +556,19 @@ */ public void createTable(Database database, Table table) throws IOException { + createTable(database, table, null); + } + + /** + * Outputs the DDL to create the table along with any non-external constraints as well + * as with external primary keys and indices (but not foreign keys). + * + * @param database The database model + * @param table The table + * @param parameters Additional platform-specific parameters for the table creation + */ + public void createTable(Database database, Table table, Map parameters) throws IOException + { print("CREATE TABLE "); printlnIdentifier(getTableName(table)); println("("); @@ -547,7 +589,7 @@ } println(); print(")"); - printEndOfStatement(); + writeTableCreationStmtEnding(table, parameters); if (!getPlatformInfo().isPrimaryKeyEmbedded()) { @@ -926,6 +968,19 @@ print("ALTER TABLE "); printlnIdentifier(getTableName(table)); printIndent(); + } + + /** + * Writes the end of the table creation statement. Per default, + * only the end of the statement is written, but this can be changed + * in subclasses. + * + * @param table The table + * @param parameters Additional platform-specific parameters for the table creation + */ + protected void writeTableCreationStmtEnding(Table table, Map parameters) throws IOException + { + printEndOfStatement(); } /** Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SybaseBuilder.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SybaseBuilder.java?rev=349134&r1=349133&r2=349134&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SybaseBuilder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SybaseBuilder.java Sat Nov 26 10:33:39 2005 @@ -17,7 +17,7 @@ */ import java.io.IOException; -import java.util.HashMap; +import java.util.Map; import org.apache.ddlutils.PlatformInfo; import org.apache.ddlutils.model.Database; @@ -46,10 +46,10 @@ /** * [EMAIL PROTECTED] */ - public void createTable(Database database, Table table) throws IOException + public void createTable(Database database, Table table, Map parameters) throws IOException { writeQuotationOnStatement(); - super.createTable(database, table); + super.createTable(database, table, parameters); } /** @@ -120,7 +120,7 @@ /** * [EMAIL PROTECTED] */ - public String getDeleteSql(Table table, HashMap pkValues, boolean genPlaceholders) + public String getDeleteSql(Table table, Map pkValues, boolean genPlaceholders) { return getQuotationOnStatement() + super.getDeleteSql(table, pkValues, genPlaceholders); } @@ -128,7 +128,7 @@ /** * [EMAIL PROTECTED] */ - public String getInsertSql(Table table, HashMap columnValues, boolean genPlaceholders) + public String getInsertSql(Table table, Map columnValues, boolean genPlaceholders) { return getQuotationOnStatement() + super.getInsertSql(table, columnValues, genPlaceholders); } @@ -136,7 +136,7 @@ /** * [EMAIL PROTECTED] */ - public String getUpdateSql(Table table, HashMap columnValues, boolean genPlaceholders) + public String getUpdateSql(Table table, Map columnValues, boolean genPlaceholders) { return getQuotationOnStatement() + super.getUpdateSql(table, columnValues, genPlaceholders); } Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommandWithCreationParameters.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommandWithCreationParameters.java?rev=349134&view=auto ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommandWithCreationParameters.java (added) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommandWithCreationParameters.java Sat Nov 26 10:33:39 2005 @@ -0,0 +1,78 @@ +package org.apache.ddlutils.task; + +/* + * Copyright 1999-2005 The Apache Software Foundation. + * + * Licensed 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.util.ArrayList; +import java.util.Iterator; + +import org.apache.ddlutils.model.Database; +import org.apache.ddlutils.model.Table; +import org.apache.ddlutils.platform.CreationParameters; + +/** + * Base type for database commands that use creation parameters. + * + * @author Thomas Dudziak + * @version $Revision: 289996 $ + */ +public abstract class DatabaseCommandWithCreationParameters extends DatabaseCommand +{ + /** The additional creation parameters. */ + private ArrayList _parameters = new ArrayList(); + + /** + * Adds a parameter which is a name-value pair. + * + * @param param The parameter + */ + public void addConfiguredParameter(TableSpecificParameter param) + { + _parameters.add(param); + } + + /** + * Filters the parameters for the given model and platform. + * + * @param model The database model + * @param platformName The name of the platform + * @param isCaseSensitive Whether case is relevant when comparing names of tables + * @return The filtered parameters + */ + protected CreationParameters getFilteredParameters(Database model, String platformName, boolean isCaseSensitive) + { + CreationParameters parameters = new CreationParameters(); + + for (Iterator it = _parameters.iterator(); it.hasNext();) + { + TableSpecificParameter param = (TableSpecificParameter)it.next(); + + if (param.isForPlatform(platformName)) + { + for (int idx = 0; idx < model.getTableCount(); idx++) + { + Table table = model.getTable(idx); + + if (param.isForTable(table, isCaseSensitive)) + { + parameters.addParameter(table, param.getName(), param.getValue()); + } + } + } + } + return parameters; + } +} Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/TableSpecificParameter.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/TableSpecificParameter.java?rev=349134&view=auto ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/TableSpecificParameter.java (added) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/TableSpecificParameter.java Sat Nov 26 10:33:39 2005 @@ -0,0 +1,91 @@ +package org.apache.ddlutils.task; + +/* + * Copyright 1999-2005 The Apache Software Foundation. + * + * Licensed 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.util.ArrayList; +import java.util.Iterator; +import java.util.StringTokenizer; + +import org.apache.ddlutils.model.Table; + +/** + * A [EMAIL PROTECTED] org.apache.ddlutils.task.Parameter} intended for specific tables. + * + * TODO: Some wildcard/regular expression mechanism would be useful + * + * @author Thomas Dudziak + * @version $Revision: 231306 $ + */ +public class TableSpecificParameter extends Parameter +{ + /** The tables for which this parameter is applicable. */ + private ArrayList _tables = new ArrayList(); + + /** + * Sets the tables as a comma-separated list. + * + * @param tableList The tables + */ + public void setTables(String tableList) + { + StringTokenizer tokenizer = new StringTokenizer(tableList, ","); + + while (tokenizer.hasMoreTokens()) + { + String tableName = tokenizer.nextToken().trim(); + + // TODO: Quotation, escaped characters ? + _tables.add(tableName); + } + } + + /** + * Sets the single table. + * + * @param tableName The table + */ + public void setTable(String tableName) + { + _tables.add(tableName); + } + + /** + * Determines whether this parameter is applicable to the given table. + * + * @param table The table + * @param caseSensitive Whether the case of the table name is relevant + * @return <code>true</code> if this parameter is applicable to the table + */ + public boolean isForTable(Table table, boolean caseSensitive) + { + if (_tables.isEmpty()) + { + return true; + } + for (Iterator it = _tables.iterator(); it.hasNext();) + { + String tableName = (String)it.next(); + + if ((caseSensitive && tableName.equals(table.getName())) || + (!caseSensitive && tableName.equalsIgnoreCase(table.getName()))) + { + return true; + } + } + return false; + } +} Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java?rev=349134&r1=349133&r2=349134&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java Sat Nov 26 10:33:39 2005 @@ -23,6 +23,7 @@ import org.apache.ddlutils.Platform; import org.apache.ddlutils.model.Database; +import org.apache.ddlutils.platform.CreationParameters; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; @@ -33,7 +34,7 @@ * @author Thomas Dudziak * @version $Revision: 289996 $ */ -public class WriteSchemaSqlToFileCommand extends DatabaseCommand +public class WriteSchemaSqlToFileCommand extends DatabaseCommandWithCreationParameters { /** The file to output the DTD to. */ private File _outputFile; @@ -106,8 +107,10 @@ throw new BuildException("Cannot overwrite output file "+_outputFile.getAbsolutePath()); } - Platform platform = getPlatform(); - Connection connection = null; + Platform platform = getPlatform(); + boolean isCaseSensitive = platform.getPlatformInfo().isUseDelimitedIdentifiers(); + CreationParameters params = getFilteredParameters(model, platform.getName(), isCaseSensitive); + Connection connection = null; try { @@ -123,11 +126,11 @@ Database currentModel = platform.readModelFromDatabase(); - platform.getSqlBuilder().alterDatabase(currentModel, model, _doDrops, true); + platform.getSqlBuilder().alterDatabase(currentModel, model, params, _doDrops, true); } else { - platform.getSqlBuilder().createTables(model, _doDrops); + platform.getSqlBuilder().createTables(model, params, _doDrops); } writer.close(); task.log("Written SQL to "+_outputFile.getAbsolutePath(), Project.MSG_INFO); Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToDatabaseCommand.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToDatabaseCommand.java?rev=349134&r1=349133&r2=349134&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToDatabaseCommand.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToDatabaseCommand.java Sat Nov 26 10:33:39 2005 @@ -18,6 +18,7 @@ import org.apache.ddlutils.Platform; import org.apache.ddlutils.model.Database; +import org.apache.ddlutils.platform.CreationParameters; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; @@ -28,7 +29,7 @@ * @author Thomas Dudziak * @version $Revision: 289996 $ */ -public class WriteSchemaToDatabaseCommand extends DatabaseCommand +public class WriteSchemaToDatabaseCommand extends DatabaseCommandWithCreationParameters { /** Whether to alter or re-set the database if it already exists. */ private boolean _alterDb = true; @@ -85,17 +86,19 @@ throw new BuildException("No database specified."); } - Platform platform = getPlatform(); + Platform platform = getPlatform(); + boolean isCaseSensitive = platform.getPlatformInfo().isUseDelimitedIdentifiers(); + CreationParameters params = getFilteredParameters(model, platform.getName(), isCaseSensitive); try { if (isAlterDatabase()) { - platform.alterTables(model, _doDrops, true, true); + platform.alterTables(model, params, _doDrops, true, true); } else { - platform.createTables(model, _doDrops, true); + platform.createTables(model, params, _doDrops, true); } task.log("Written schema to database", Project.MSG_INFO); Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestMySqlPlatform.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestMySqlPlatform.java?rev=349134&r1=349133&r2=349134&view=diff ============================================================================== --- db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestMySqlPlatform.java (original) +++ db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestMySqlPlatform.java Sat Nov 26 10:33:39 2005 @@ -17,6 +17,7 @@ */ import org.apache.ddlutils.TestPlatformBase; +import org.apache.ddlutils.model.Database; import org.apache.ddlutils.platform.MySqlPlatform; /** @@ -137,5 +138,38 @@ "ALTER TABLE `table2` ADD CONSTRAINT `table2_FK_COL_FK_1_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", createTestDatabase(TABLE_CONSTRAINT_TEST_SCHEMA)); + } + + /** + * Tests the usage of creation parameters. + */ + public void testCreationParameters1() throws Exception + { + Database testDb = parseDatabaseFromString(COLUMN_CONSTRAINT_TEST_SCHEMA); + CreationParameters params = new CreationParameters(); + + params.addParameter(testDb.getTable(0), + "ROW_FORMAT", + "COMPRESSED"); + params.addParameter(null, + "ENGINE", + "INNODB"); + + getPlatformInfo().setCommentsSupported(false); + getPlatform().getSqlBuilder().createTables(testDb, params, true); + + assertEqualsIgnoringWhitespaces( + "DROP TABLE IF EXISTS `constraints`;\n" + + "CREATE TABLE `constraints`\n"+ + "(\n"+ + " `COL_PK` VARCHAR(32),\n"+ + " `COL_PK_AUTO_INCR` INTEGER AUTO_INCREMENT,\n"+ + " `COL_NOT_NULL` CHAR(100) BINARY NOT NULL,\n"+ + " `COL_NOT_NULL_DEFAULT` DOUBLE DEFAULT '-2.0' NOT NULL,\n"+ + " `COL_DEFAULT` CHAR(4) DEFAULT 'test',\n"+ + " `COL_AUTO_INCR` BIGINT AUTO_INCREMENT,\n"+ + " PRIMARY KEY (`COL_PK`, `COL_PK_AUTO_INCR`)\n"+ + ") ENGINE=INNODB ROW_FORMAT=COMPRESSED;\n", + getBuilderOutput()); } }