Author: tomdz Date: Mon Dec 4 23:10:41 2006 New Revision: 482531 URL: http://svn.apache.org/viewvc?view=rev&rev=482531 Log: Added convenience method to drop a single table
Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java?view=diff&rev=482531&r1=482530&r2=482531 ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java Mon Dec 4 23:10:41 2006 @@ -520,6 +520,35 @@ public String getAlterTablesSql(Connection connection, String catalog, String schema, String[] tableTypes, Database desiredDb, CreationParameters params) throws DatabaseOperationException; /** + * Drops the specified table and all foreign keys pointing to it. + * + * @param model The database model + * @param table The table to drop + * @param continueOnError Whether to continue executing the sql commands when an error occurred + */ + public void dropTable(Database model, Table table, boolean continueOnError) throws DatabaseOperationException; + + /** + * Returns the SQL for dropping the given table and all foreign keys pointing to it. + * + * @param model The database model + * @param table The table to drop + * @param continueOnError Whether to continue executing the sql commands when an error occurred + * @return The SQL statements + */ + public String getDropTableSql(Database model, Table table, boolean continueOnError); + + /** + * Drops the specified table and all foreign keys pointing to it. + * + * @param connection The connection to the database + * @param model The database model + * @param table The table to drop + * @param continueOnError Whether to continue executing the sql commands when an error occurred + */ + public void dropTable(Connection connection, Database model, Table table, boolean continueOnError) throws DatabaseOperationException; + + /** * Drops the tables defined in the given database. * * @param model The database model Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java?view=diff&rev=482531&r1=482530&r2=482531 ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java Mon Dec 4 23:10:41 2006 @@ -748,6 +748,55 @@ /** * [EMAIL PROTECTED] */ + public void dropTable(Connection connection, Database model, Table table, boolean continueOnError) throws DatabaseOperationException + { + String sql = getDropTableSql(model, table, continueOnError); + + evaluateBatch(connection, sql, continueOnError); + } + + /** + * [EMAIL PROTECTED] + */ + public void dropTable(Database model, Table table, boolean continueOnError) throws DatabaseOperationException + { + Connection connection = borrowConnection(); + + try + { + dropTable(connection, model, table, continueOnError); + } + finally + { + returnConnection(connection); + } + } + + /** + * [EMAIL PROTECTED] + */ + public String getDropTableSql(Database model, Table table, boolean continueOnError) + { + String sql = null; + + try + { + StringWriter buffer = new StringWriter(); + + getSqlBuilder().setWriter(buffer); + getSqlBuilder().dropTable(model, table); + sql = buffer.toString(); + } + catch (IOException e) + { + // won't happen because we're using a string writer + } + return sql; + } + + /** + * [EMAIL PROTECTED] + */ public void dropTables(Database model, boolean continueOnError) throws DatabaseOperationException { Connection connection = borrowConnection(); 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?view=diff&rev=482531&r1=482530&r2=482531 ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java Mon Dec 4 23:10:41 2006 @@ -1321,7 +1321,37 @@ } /** - * Outputs the DDL to drop the table. + * Outputs the DDL required to drop the given table. This method also + * drops foreign keys to the table. + * + * @param database The database + * @param table The table + */ + public void dropTable(Database database, Table table) throws IOException + { + // we're dropping the foreignkeys to the table first + for (int idx = database.getTableCount() - 1; idx >= 0; idx--) + { + Table otherTable = database.getTable(idx); + ForeignKey[] fks = otherTable.getForeignKeys(); + + for (int fkIdx = 0; (fks != null) && (fkIdx < fks.length); fkIdx++) + { + if (fks[fkIdx].getForeignTable().equals(table)) + { + writeExternalForeignKeyDropStmt(otherTable, fks[fkIdx]); + } + } + } + + writeTableComment(table); + dropTable(table); + } + + /** + * Outputs the DDL to drop the table. Note that this method does not drop + * foreign keys to this table. Use [EMAIL PROTECTED] #dropTable(Database, Table)} + * if you want that. * * @param table The table to drop */