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 b5de4cda15
commit 18f0bd2f4a
9 changed files with 39 additions and 60 deletions

View File

@ -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?

View File

@ -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() {

View File

@ -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() {

View File

@ -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 ) };
}
}

View File

@ -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

View File

@ -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() {

View File

@ -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

View File

@ -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() );

View File

@ -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 );