HHH-8003 Create "sqlDropString" method in Dialect to handle "if exists"

correctly
This commit is contained in:
Brett Meyer 2013-02-13 23:08:44 -05:00
parent e25211380d
commit 7fe238ac70
9 changed files with 39 additions and 60 deletions

View File

@ -1933,6 +1933,18 @@ public abstract class Dialect implements ConversionContext {
return false; 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? * Does this dialect support column-level check constraints?
* *

View File

@ -134,15 +134,7 @@ public class MultipleHiLoPerTableGenerator implements PersistentIdentifierGenera
} }
public String[] sqlDropStrings(Dialect dialect) throws HibernateException { public String[] sqlDropStrings(Dialect dialect) throws HibernateException {
StringBuilder sqlDropString = new StringBuilder( "drop table " ); return new String[] { dialect.getDropTableString( tableName ) };
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() };
} }
public Object generatorKey() { public Object generatorKey() {

View File

@ -211,15 +211,7 @@ public class TableGenerator implements PersistentIdentifierGenerator, Configurab
} }
public String[] sqlDropStrings(Dialect dialect) { public String[] sqlDropStrings(Dialect dialect) {
StringBuilder sqlDropString = new StringBuilder( "drop table " ); return new String[] { dialect.getDropTableString( tableName ) };
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() };
} }
public Object generatorKey() { public Object generatorKey() {

View File

@ -571,14 +571,6 @@ public class TableGenerator implements PersistentIdentifierGenerator, Configurab
@Override @Override
public String[] sqlDropStrings(Dialect dialect) throws HibernateException { public String[] sqlDropStrings(Dialect dialect) throws HibernateException {
StringBuilder sqlDropString = new StringBuilder().append( "drop table " ); return new String[] { dialect.getDropTableString( tableName ) };
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() };
} }
} }

View File

@ -187,15 +187,7 @@ public class TableStructure implements DatabaseStructure {
@Override @Override
public String[] sqlDropStrings(Dialect dialect) throws HibernateException { public String[] sqlDropStrings(Dialect dialect) throws HibernateException {
StringBuilder sqlDropString = new StringBuilder().append( "drop table " ); return new String[] { dialect.getDropTableString( tableName ) };
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() };
} }
@Override @Override

View File

@ -572,16 +572,7 @@ public class Table implements RelationalModel, Serializable {
} }
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) { public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) {
StringBuilder buf = new StringBuilder( "drop table " ); return dialect.getDropTableString( getQualifiedName( dialect, defaultCatalog, defaultSchema ) );
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();
} }
public PrimaryKey getPrimaryKey() { public PrimaryKey getPrimaryKey() {

View File

@ -268,16 +268,7 @@ public class Table extends AbstractTableSpecification implements Exportable {
@Override @Override
public String[] sqlDropStrings(Dialect dialect) { public String[] sqlDropStrings(Dialect dialect) {
StringBuilder buf = new StringBuilder( "drop table " ); return new String[] { dialect.getDropTableString( getQualifiedName( dialect ) ) };
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() };
} }
@Override @Override

View File

@ -72,7 +72,13 @@ public class BasicConnectionTest extends BaseCoreFunctionalTestCase {
try { try {
Statement statement = jdbcCoord.getStatementPreparer().createStatement(); 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, jdbcCoord.getResultSetReturn().execute( statement,
"create table SANDBOX_JDBC_TST ( ID integer, NAME varchar(100) )" ); "create table SANDBOX_JDBC_TST ( ID integer, NAME varchar(100) )" );
assertTrue( jdbcCoord.hasRegisteredResources() ); assertTrue( jdbcCoord.hasRegisteredResources() );

View File

@ -83,7 +83,13 @@ public class BatchingTest extends BaseCoreFunctionalTestCase implements BatchKey
// set up some tables to use // set up some tables to use
Statement statement = jdbcCoordinator.getStatementPreparer().createStatement(); 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) )" ); jdbcCoordinator.getResultSetReturn().execute( statement, "create table SANDBOX_JDBC_TST ( ID integer, NAME varchar(100) )" );
assertTrue( jdbcCoordinator.hasRegisteredResources() ); assertTrue( jdbcCoordinator.hasRegisteredResources() );
assertTrue( logicalConnection.isPhysicallyConnected() ); assertTrue( logicalConnection.isPhysicallyConnected() );
@ -141,8 +147,13 @@ public class BatchingTest extends BaseCoreFunctionalTestCase implements BatchKey
// set up some tables to use // set up some tables to use
Statement statement = jdbcCoordinator.getStatementPreparer().createStatement(); Statement statement = jdbcCoordinator.getStatementPreparer().createStatement();
jdbcCoordinator.getResultSetReturn().execute( statement, "drop table SANDBOX_JDBC_TST if exists" ); String dropSql = getDialect().getDropTableString( "SANDBOX_JDBC_TST" );
jdbcCoordinator.getResultSetReturn().execute( statement, "create table SANDBOX_JDBC_TST ( ID integer, NAME varchar(100) )" ); 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( jdbcCoordinator.hasRegisteredResources() );
assertTrue( logicalConnection.isPhysicallyConnected() ); assertTrue( logicalConnection.isPhysicallyConnected() );
jdbcCoordinator.release( statement ); jdbcCoordinator.release( statement );