HHH-7002 added support for "if exists" on "drop constraint"

This commit is contained in:
Brett Meyer 2013-06-23 13:17:28 -04:00
parent edf1fa9fbc
commit 81affe3afe
4 changed files with 49 additions and 7 deletions

View File

@ -2141,6 +2141,28 @@ public abstract class Dialect implements ConversionContext {
return false;
}
/**
* For dropping a constraint with an "alter table", can the phrase "if exists" be applied before the constraint name?
* <p/>
* NOTE : Only one or the other (or neither) of this and {@link #supportsIfExistsAfterConstraintName} should return true
*
* @return {@code true} if the "if exists" can be applied before the constraint name
*/
public boolean supportsIfExistsBeforeConstraintName() {
return false;
}
/**
* For dropping a constraint with an "alter table", can the phrase "if exists" be applied after the constraint name?
* <p/>
* NOTE : Only one or the other (or neither) of this and {@link #supportsIfExistsBeforeConstraintName} should return true
*
* @return {@code true} if the "if exists" can be applied after the constraint name
*/
public boolean supportsIfExistsAfterConstraintName() {
return false;
}
/**
* Generate a DROP TABLE statement
*

View File

@ -255,6 +255,11 @@ public class H2Dialect extends Dialect {
return true;
}
@Override
public boolean supportsIfExistsAfterConstraintName() {
return true;
}
@Override
public boolean supportsSequences() {
return true;

View File

@ -94,9 +94,17 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
String defaultSchema) {
// Do this here, rather than allowing UniqueKey/Constraint to do it.
// We need full, simplified control over whether or not it happens.
final String tableName = uniqueKey.getTable().getQualifiedName( dialect, defaultCatalog, defaultSchema );
final String constraintName = dialect.quote( uniqueKey.getName() );
return "alter table " + tableName + " drop constraint " + constraintName;
final StringBuilder buf = new StringBuilder( "alter table " );
buf.append( uniqueKey.getTable().getQualifiedName( dialect, defaultCatalog, defaultSchema ) );
buf.append(" drop constraint " );
if ( dialect.supportsIfExistsBeforeConstraintName() ) {
buf.append( "if exists " );
}
buf.append( dialect.quote( uniqueKey.getName() ) );
if ( dialect.supportsIfExistsAfterConstraintName() ) {
buf.append( " if exists" );
}
return buf.toString();
}

View File

@ -127,10 +127,17 @@ public class ForeignKey extends Constraint {
}
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) {
return "alter table " +
getTable().getQualifiedName(dialect, defaultCatalog, defaultSchema) +
dialect.getDropForeignKeyString() +
getName();
final StringBuilder buf = new StringBuilder( "alter table " );
buf.append( getTable().getQualifiedName(dialect, defaultCatalog, defaultSchema) );
buf.append( dialect.getDropForeignKeyString() );
if ( dialect.supportsIfExistsBeforeConstraintName() ) {
buf.append( "if exists " );
}
buf.append( getName() );
if ( dialect.supportsIfExistsAfterConstraintName() ) {
buf.append( " if exists" );
}
return buf.toString();
}
public boolean isCascadeDeleteEnabled() {