From 7fe238ac70f84fe316ef63bbde06a4deb64f6298 Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Wed, 13 Feb 2013 23:08:44 -0500 Subject: [PATCH] HHH-8003 Create "sqlDropString" method in Dialect to handle "if exists" correctly --- .../java/org/hibernate/dialect/Dialect.java | 12 ++++++++++++ .../id/MultipleHiLoPerTableGenerator.java | 10 +--------- .../java/org/hibernate/id/TableGenerator.java | 10 +--------- .../hibernate/id/enhanced/TableGenerator.java | 10 +--------- .../hibernate/id/enhanced/TableStructure.java | 10 +--------- .../main/java/org/hibernate/mapping/Table.java | 11 +---------- .../hibernate/metamodel/relational/Table.java | 11 +---------- .../test/jdbc/internal/BasicConnectionTest.java | 8 +++++++- .../test/jdbc/internal/BatchingTest.java | 17 ++++++++++++++--- 9 files changed, 39 insertions(+), 60 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java index dcaf3e5038..c7f71b123f 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java @@ -1932,6 +1932,18 @@ public abstract class Dialect implements ConversionContext { public boolean supportsIfExistsAfterTableName() { return false; } + + public String getDropTableString( String tableName ) { + StringBuilder buf = new StringBuilder( "drop table " ); + if ( supportsIfExistsBeforeTableName() ) { + buf.append( "if exists " ); + } + buf.append( tableName ).append( getCascadeConstraintsString() ); + if ( supportsIfExistsAfterTableName() ) { + buf.append( " if exists" ); + } + return buf.toString(); + } /** * Does this dialect support column-level check constraints? diff --git a/hibernate-core/src/main/java/org/hibernate/id/MultipleHiLoPerTableGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/MultipleHiLoPerTableGenerator.java index 6457cd6a5e..8562cf7f98 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/MultipleHiLoPerTableGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/MultipleHiLoPerTableGenerator.java @@ -134,15 +134,7 @@ public class MultipleHiLoPerTableGenerator implements PersistentIdentifierGenera } public String[] sqlDropStrings(Dialect dialect) throws HibernateException { - StringBuilder sqlDropString = new StringBuilder( "drop table " ); - if ( dialect.supportsIfExistsBeforeTableName() ) { - sqlDropString.append( "if exists " ); - } - sqlDropString.append( tableName ).append( dialect.getCascadeConstraintsString() ); - if ( dialect.supportsIfExistsAfterTableName() ) { - sqlDropString.append( " if exists" ); - } - return new String[] { sqlDropString.toString() }; + return new String[] { dialect.getDropTableString( tableName ) }; } public Object generatorKey() { diff --git a/hibernate-core/src/main/java/org/hibernate/id/TableGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/TableGenerator.java index 7a543af5cd..4beca131f5 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/TableGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/TableGenerator.java @@ -211,15 +211,7 @@ public class TableGenerator implements PersistentIdentifierGenerator, Configurab } public String[] sqlDropStrings(Dialect dialect) { - StringBuilder sqlDropString = new StringBuilder( "drop table " ); - if ( dialect.supportsIfExistsBeforeTableName() ) { - sqlDropString.append( "if exists " ); - } - sqlDropString.append( tableName ).append( dialect.getCascadeConstraintsString() ); - if ( dialect.supportsIfExistsAfterTableName() ) { - sqlDropString.append( " if exists" ); - } - return new String[] { sqlDropString.toString() }; + return new String[] { dialect.getDropTableString( tableName ) }; } public Object generatorKey() { diff --git a/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java index 565f2bf446..b52aad778a 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java @@ -571,14 +571,6 @@ public class TableGenerator implements PersistentIdentifierGenerator, Configurab @Override public String[] sqlDropStrings(Dialect dialect) throws HibernateException { - StringBuilder sqlDropString = new StringBuilder().append( "drop table " ); - if ( dialect.supportsIfExistsBeforeTableName() ) { - sqlDropString.append( "if exists " ); - } - sqlDropString.append( tableName ).append( dialect.getCascadeConstraintsString() ); - if ( dialect.supportsIfExistsAfterTableName() ) { - sqlDropString.append( " if exists" ); - } - return new String[] { sqlDropString.toString() }; + return new String[] { dialect.getDropTableString( tableName ) }; } } diff --git a/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableStructure.java b/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableStructure.java index b86f723d8f..dd99e25c10 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableStructure.java +++ b/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableStructure.java @@ -187,15 +187,7 @@ public class TableStructure implements DatabaseStructure { @Override public String[] sqlDropStrings(Dialect dialect) throws HibernateException { - StringBuilder sqlDropString = new StringBuilder().append( "drop table " ); - if ( dialect.supportsIfExistsBeforeTableName() ) { - sqlDropString.append( "if exists " ); - } - sqlDropString.append( tableName ).append( dialect.getCascadeConstraintsString() ); - if ( dialect.supportsIfExistsAfterTableName() ) { - sqlDropString.append( " if exists" ); - } - return new String[] { sqlDropString.toString() }; + return new String[] { dialect.getDropTableString( tableName ) }; } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Table.java b/hibernate-core/src/main/java/org/hibernate/mapping/Table.java index 3c7c35f8b8..9382f30677 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Table.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Table.java @@ -572,16 +572,7 @@ public class Table implements RelationalModel, Serializable { } public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) { - StringBuilder buf = new StringBuilder( "drop table " ); - if ( dialect.supportsIfExistsBeforeTableName() ) { - buf.append( "if exists " ); - } - buf.append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) ) - .append( dialect.getCascadeConstraintsString() ); - if ( dialect.supportsIfExistsAfterTableName() ) { - buf.append( " if exists" ); - } - return buf.toString(); + return dialect.getDropTableString( getQualifiedName( dialect, defaultCatalog, defaultSchema ) ); } public PrimaryKey getPrimaryKey() { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Table.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Table.java index d106685c3c..b252c01951 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Table.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Table.java @@ -268,16 +268,7 @@ public class Table extends AbstractTableSpecification implements Exportable { @Override public String[] sqlDropStrings(Dialect dialect) { - StringBuilder buf = new StringBuilder( "drop table " ); - if ( dialect.supportsIfExistsBeforeTableName() ) { - buf.append( "if exists " ); - } - buf.append( getQualifiedName( dialect ) ) - .append( dialect.getCascadeConstraintsString() ); - if ( dialect.supportsIfExistsAfterTableName() ) { - buf.append( " if exists" ); - } - return new String[] { buf.toString() }; + return new String[] { dialect.getDropTableString( getQualifiedName( dialect ) ) }; } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/BasicConnectionTest.java b/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/BasicConnectionTest.java index 855b12279f..5c5befed5c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/BasicConnectionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/BasicConnectionTest.java @@ -72,7 +72,13 @@ public class BasicConnectionTest extends BaseCoreFunctionalTestCase { try { Statement statement = jdbcCoord.getStatementPreparer().createStatement(); - jdbcCoord.getResultSetReturn().execute( statement, "drop table SANDBOX_JDBC_TST if exists" ); + String dropSql = getDialect().getDropTableString( "SANDBOX_JDBC_TST" ); + try { + jdbcCoord.getResultSetReturn().execute( statement, dropSql ); + } + catch ( Exception e ) { + // ignore if the DB doesn't support "if exists" and the table doesn't exist + } jdbcCoord.getResultSetReturn().execute( statement, "create table SANDBOX_JDBC_TST ( ID integer, NAME varchar(100) )" ); assertTrue( jdbcCoord.hasRegisteredResources() ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/BatchingTest.java b/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/BatchingTest.java index 5c36cb0b8b..c54bd015a2 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/BatchingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/BatchingTest.java @@ -83,7 +83,13 @@ public class BatchingTest extends BaseCoreFunctionalTestCase implements BatchKey // set up some tables to use Statement statement = jdbcCoordinator.getStatementPreparer().createStatement(); - jdbcCoordinator.getResultSetReturn().execute( statement, "drop table SANDBOX_JDBC_TST if exists" ); + String dropSql = getDialect().getDropTableString( "SANDBOX_JDBC_TST" ); + try { + jdbcCoordinator.getResultSetReturn().execute( statement, dropSql ); + } + catch ( Exception e ) { + // ignore if the DB doesn't support "if exists" and the table doesn't exist + } jdbcCoordinator.getResultSetReturn().execute( statement, "create table SANDBOX_JDBC_TST ( ID integer, NAME varchar(100) )" ); assertTrue( jdbcCoordinator.hasRegisteredResources() ); assertTrue( logicalConnection.isPhysicallyConnected() ); @@ -141,8 +147,13 @@ public class BatchingTest extends BaseCoreFunctionalTestCase implements BatchKey // set up some tables to use Statement statement = jdbcCoordinator.getStatementPreparer().createStatement(); - jdbcCoordinator.getResultSetReturn().execute( statement, "drop table SANDBOX_JDBC_TST if exists" ); - jdbcCoordinator.getResultSetReturn().execute( statement, "create table SANDBOX_JDBC_TST ( ID integer, NAME varchar(100) )" ); + String dropSql = getDialect().getDropTableString( "SANDBOX_JDBC_TST" ); + try { + jdbcCoordinator.getResultSetReturn().execute( statement, dropSql ); + } + catch ( Exception e ) { + // ignore if the DB doesn't support "if exists" and the table doesn't exist + } jdbcCoordinator.getResultSetReturn().execute( statement, "create table SANDBOX_JDBC_TST ( ID integer, NAME varchar(100) )" ); assertTrue( jdbcCoordinator.hasRegisteredResources() ); assertTrue( logicalConnection.isPhysicallyConnected() ); jdbcCoordinator.release( statement );