HHH-6439: Added getAddUniqueConstraintString method to Dialect and updated UniqueKey classes to use it

This commit is contained in:
John Verhaeg 2011-09-09 10:30:01 -05:00
parent 88b6b4c67b
commit f4c36a10f8
3 changed files with 27 additions and 19 deletions

View File

@ -482,7 +482,7 @@ public abstract class Dialect {
} }
try { try {
LobCreator lobCreator = session.getFactory().getJdbcServices().getLobCreator( session ); LobCreator lobCreator = session.getFactory().getJdbcServices().getLobCreator( session );
return original == null return original == null
? lobCreator.createBlob( ArrayHelper.EMPTY_BYTE_ARRAY ) ? lobCreator.createBlob( ArrayHelper.EMPTY_BYTE_ARRAY )
: lobCreator.createBlob( original.getBinaryStream(), original.length() ); : lobCreator.createBlob( original.getBinaryStream(), original.length() );
} }
@ -1793,6 +1793,16 @@ public abstract class Dialect {
return " add constraint " + constraintName + " primary key "; return " add constraint " + constraintName + " primary key ";
} }
/**
* The syntax used to add a unique constraint to a table.
*
* @param constraintName The name of the unique constraint.
* @return The "add unique" fragment
*/
public String getAddUniqueConstraintString(String constraintName) {
return " add constraint " + constraintName + " unique ";
}
public boolean hasSelfReferentialForeignKeyBug() { public boolean hasSelfReferentialForeignKeyBug() {
return false; return false;
} }

View File

@ -25,7 +25,6 @@ package org.hibernate.mapping;
import java.util.Iterator; import java.util.Iterator;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.Mapping; import org.hibernate.engine.spi.Mapping;
import org.hibernate.internal.util.StringHelper;
/** /**
* A relational unique key constraint * A relational unique key constraint
@ -54,13 +53,14 @@ public class UniqueKey extends Constraint {
null; null;
} }
public String sqlConstraintString( @Override
public String sqlConstraintString(
Dialect dialect, Dialect dialect,
String constraintName, String constraintName,
String defaultCatalog, String defaultCatalog,
String defaultSchema) { String defaultSchema) {
StringBuffer buf = new StringBuffer( StringBuffer buf = new StringBuffer(
dialect.getAddPrimaryKeyConstraintString( constraintName ) dialect.getAddUniqueConstraintString( constraintName )
).append( '(' ); ).append( '(' );
Iterator iter = getColumnIterator(); Iterator iter = getColumnIterator();
boolean nullable = false; boolean nullable = false;
@ -70,13 +70,11 @@ public class UniqueKey extends Constraint {
buf.append( column.getQuotedName( dialect ) ); buf.append( column.getQuotedName( dialect ) );
if ( iter.hasNext() ) buf.append( ", " ); if ( iter.hasNext() ) buf.append( ", " );
} }
return !nullable || dialect.supportsNotNullUnique() ? return !nullable || dialect.supportsNotNullUnique() ? buf.append( ')' ).toString() : null;
StringHelper.replace( buf.append( ')' ).toString(), "primary key", "unique" ) :
//TODO: improve this hack!
null;
} }
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) { @Override
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) {
if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) { if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema ); return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema );
} }
@ -86,7 +84,8 @@ public class UniqueKey extends Constraint {
} }
} }
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) { @Override
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) {
if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) { if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
return super.sqlDropString( dialect, defaultCatalog, defaultSchema ); return super.sqlDropString( dialect, defaultCatalog, defaultSchema );
} }
@ -95,7 +94,8 @@ public class UniqueKey extends Constraint {
} }
} }
public boolean isGenerated(Dialect dialect) { @Override
public boolean isGenerated(Dialect dialect) {
if ( dialect.supportsNotNullUnique() ) return true; if ( dialect.supportsNotNullUnique() ) return true;
Iterator iter = getColumnIterator(); Iterator iter = getColumnIterator();
while ( iter.hasNext() ) { while ( iter.hasNext() ) {

View File

@ -24,7 +24,6 @@
package org.hibernate.metamodel.relational; package org.hibernate.metamodel.relational;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.StringHelper;
/** /**
* Models a SQL <tt>INDEX</tt> defined as UNIQUE * Models a SQL <tt>INDEX</tt> defined as UNIQUE
@ -47,7 +46,8 @@ public class UniqueKey extends AbstractConstraint implements Constraint {
return sb.toString(); return sb.toString();
} }
public boolean isCreationVetoed(Dialect dialect) { @Override
public boolean isCreationVetoed(Dialect dialect) {
if ( dialect.supportsNotNullUnique() ) { if ( dialect.supportsNotNullUnique() ) {
return false; return false;
} }
@ -82,9 +82,10 @@ public class UniqueKey extends AbstractConstraint implements Constraint {
null; null;
} }
public String sqlConstraintStringInAlterTable(Dialect dialect) { @Override
public String sqlConstraintStringInAlterTable(Dialect dialect) {
StringBuffer buf = new StringBuffer( StringBuffer buf = new StringBuffer(
dialect.getAddPrimaryKeyConstraintString( getName() ) dialect.getAddUniqueConstraintString( getName() )
).append( '(' ); ).append( '(' );
boolean nullable = false; boolean nullable = false;
boolean first = true; boolean first = true;
@ -100,9 +101,6 @@ public class UniqueKey extends AbstractConstraint implements Constraint {
} }
buf.append( column.getColumnName().encloseInQuotesIfQuoted( dialect ) ); buf.append( column.getColumnName().encloseInQuotesIfQuoted( dialect ) );
} }
return !nullable || dialect.supportsNotNullUnique() ? return !nullable || dialect.supportsNotNullUnique() ? buf.append( ')' ).toString() : null;
StringHelper.replace( buf.append( ')' ).toString(), "primary key", "unique" ) :
//TODO: improve this hack!
null;
} }
} }