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 bcd790eb7a..05669ecb79 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java @@ -2011,6 +2011,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 2f9aeccfe1..013291fd9f 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Table.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Table.java @@ -571,16 +571,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 9f504c0f46..67e7507f24 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 @@ -80,7 +80,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() ); @@ -138,8 +144,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 );