HHH-7797 Finished first take on UniqueDelegate and rolled into .cfg and

.mapping
This commit is contained in:
brmeyer 2012-12-11 14:00:48 -05:00 committed by Brett Meyer
parent 0a2b639808
commit 1416126a00
6 changed files with 154 additions and 98 deletions

View File

@ -1045,17 +1045,15 @@ public class Configuration implements Serializable {
Table table = (Table) iter.next(); Table table = (Table) iter.next();
if ( table.isPhysicalTable() ) { if ( table.isPhysicalTable() ) {
if ( !dialect.supportsUniqueConstraintInCreateAlterTable() ) { Iterator subIter = table.getUniqueKeyIterator();
Iterator subIter = table.getUniqueKeyIterator(); while ( subIter.hasNext() ) {
while ( subIter.hasNext() ) { UniqueKey uk = (UniqueKey) subIter.next();
UniqueKey uk = (UniqueKey) subIter.next(); String constraintString = uk.sqlCreateString( dialect, mapping, defaultCatalog, defaultSchema );
String constraintString = uk.sqlCreateString( dialect, mapping, defaultCatalog, defaultSchema ); if (constraintString != null) script.add( constraintString );
if (constraintString != null) script.add( constraintString );
}
} }
Iterator subIter = table.getIndexIterator(); subIter = table.getIndexIterator();
while ( subIter.hasNext() ) { while ( subIter.hasNext() ) {
Index index = (Index) subIter.next(); Index index = (Index) subIter.next();
script.add( script.add(

View File

@ -30,7 +30,6 @@ import java.sql.Types;
import org.hibernate.JDBCException; import org.hibernate.JDBCException;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.dialect.function.AnsiTrimEmulationFunction;
import org.hibernate.dialect.function.AvgWithArgumentCastFunction; import org.hibernate.dialect.function.AvgWithArgumentCastFunction;
import org.hibernate.dialect.function.NoArgSQLFunction; import org.hibernate.dialect.function.NoArgSQLFunction;
import org.hibernate.dialect.function.SQLFunctionTemplate; import org.hibernate.dialect.function.SQLFunctionTemplate;
@ -279,10 +278,6 @@ public class DB2Dialect extends Dialect {
return false; return false;
} }
@Override @Override
public boolean supportsNotNullUnique() {
return false;
}
@Override
public boolean supportsExistsInSelect() { public boolean supportsExistsInSelect() {
return false; return false;
} }

View File

@ -21,7 +21,6 @@
package org.hibernate.dialect.unique; package org.hibernate.dialect.unique;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.mapping.Column; import org.hibernate.mapping.Column;
@ -43,7 +42,7 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
} }
@Override @Override
public void applyUnique( Table table, Column column, StringBuilder sb ) { public String applyUniqueToColumn( Table table, Column column ) {
// if ( column.isUnique() // if ( column.isUnique()
// && ( column.isNullable() // && ( column.isNullable()
// || dialect.supportsNotNullUnique() ) ) { // || dialect.supportsNotNullUnique() ) ) {
@ -61,29 +60,78 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
UniqueKey uk = table.getOrCreateUniqueKey( UniqueKey uk = table.getOrCreateUniqueKey(
column.getQuotedName( dialect ) + '_' ); column.getQuotedName( dialect ) + '_' );
uk.addColumn( column ); uk.addColumn( column );
return "";
} }
@Override @Override
public void createUniqueConstraint( Table table, StringBuilder sb ) { public String applyUniquesToTable( Table table ) {
// TODO: Am I correct that this shouldn't be done unless the constraint
// isn't created in an alter table?
// Iterator uniqueKeyIterator = table.getUniqueKeyIterator();
// while ( uniqueKeyIterator.hasNext() ) {
// UniqueKey uniqueKey = (UniqueKey) uniqueKeyIterator.next();
//
// sb.append( ", " ).append( createUniqueConstraint( uniqueKey) );
// }
return "";
}
@Override
public String applyUniquesOnAlter( UniqueKey uniqueKey,
String defaultCatalog, String defaultSchema ) {
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) { // if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
// Iterator ukiter = getUniqueKeyIterator(); // return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema );
// while ( ukiter.hasNext() ) { // }
// UniqueKey uk = (UniqueKey) ukiter.next(); // else {
// String constraint = uk.sqlConstraintString( dialect ); // return Index.buildSqlCreateIndexString( dialect, getName(), getTable(), getColumnIterator(), true,
// if ( constraint != null ) { // defaultCatalog, defaultSchema );
// buf.append( ", " ).append( constraint );
// }
// }
// } // }
Iterator ukiter = table.getUniqueKeyIterator(); return new StringBuilder( "alter table " )
while ( ukiter.hasNext() ) { .append( uniqueKey.getTable().getQualifiedName(
UniqueKey uk = (UniqueKey) ukiter.next(); dialect, defaultCatalog, defaultSchema ) )
String constraint = uk.sqlConstraintString( dialect ); .append( " add constraint " )
if ( constraint != null ) { .append( uniqueKey.getName() )
sb.append( ", " ).append( constraint ); .append( uniqueConstraintSql( uniqueKey ) )
.toString();
}
@Override
public String dropUniquesOnAlter( UniqueKey uniqueKey,
String defaultCatalog, String defaultSchema ) {
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
// return super.sqlDropString( dialect, defaultCatalog, defaultSchema );
// }
// else {
// return Index.buildSqlDropIndexString( dialect, getTable(), getName(), defaultCatalog, defaultSchema );
// }
return new StringBuilder( "alter table " )
.append( uniqueKey.getTable().getQualifiedName(
dialect, defaultCatalog, defaultSchema ) )
.append( " drop constraint " )
.append( dialect.quote( uniqueKey.getName() ) )
.toString();
}
@Override
public String uniqueConstraintSql( UniqueKey uniqueKey ) {
// TODO: This may not be necessary, but not all callers currently
// check it on their own. Go through their logic.
// if ( !isGenerated( dialect ) ) return null;
StringBuilder sb = new StringBuilder();
sb.append( " unique (" );
Iterator columnIterator = uniqueKey.getColumnIterator();
while ( columnIterator.hasNext() ) {
Column column = (Column) columnIterator.next();
sb.append( column.getQuotedName( dialect ) );
if ( columnIterator.hasNext() ) {
sb.append( ", " );
} }
} }
return sb.append( ')' ).toString();
} }
} }

View File

@ -22,6 +22,7 @@ package org.hibernate.dialect.unique;
import org.hibernate.mapping.Column; import org.hibernate.mapping.Column;
import org.hibernate.mapping.Table; import org.hibernate.mapping.Table;
import org.hibernate.mapping.UniqueKey;
/** /**
* Dialect-level delegate in charge of applying "uniqueness" to a column. * Dialect-level delegate in charge of applying "uniqueness" to a column.
@ -39,7 +40,57 @@ import org.hibernate.mapping.Table;
*/ */
public interface UniqueDelegate { public interface UniqueDelegate {
public void applyUnique( Table table, Column column, StringBuilder sb ); /**
* If the delegate supports unique constraints, this method should simply
* create the UniqueKey on the Table. Otherwise, the constraint isn't
* supported and "unique" should be added to the column definition.
*
* @param table
* @param column
* @return String
*/
public String applyUniqueToColumn( Table table, Column column );
public void createUniqueConstraint( Table table, StringBuilder sb ); /**
* If creating unique constraints in separate alter statements are not
* supported, this method should return the syntax necessary to create
* the constraint on the original create table statement.
*
* @param table
* @return String
*/
public String applyUniquesToTable( Table table );
/**
* If creating unique constraints in separate alter statements is
* supported, generate the necessary "alter" syntax for the given key.
*
* @param uniqueKey
* @param defaultCatalog
* @param defaultSchema
* @return String
*/
public String applyUniquesOnAlter( UniqueKey uniqueKey,
String defaultCatalog, String defaultSchema );
/**
* If dropping unique constraints in separate alter statements is
* supported, generate the necessary "alter" syntax for the given key.
*
* @param uniqueKey
* @param defaultCatalog
* @param defaultSchema
* @return String
*/
public String dropUniquesOnAlter( UniqueKey uniqueKey,
String defaultCatalog, String defaultSchema );
/**
* Generates the syntax necessary to create the unique constraint (reused
* by all methods). Ex: "unique (column1, column2, ...)"
*
* @param uniqueKey
* @return String
*/
public String uniqueConstraintSql( UniqueKey uniqueKey );
} }

View File

@ -26,7 +26,6 @@ package org.hibernate.mapping;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
@ -423,7 +422,9 @@ public class Table implements RelationalModel, Serializable {
alter.append( " not null" ); alter.append( " not null" );
} }
dialect.getUniqueDelegate().applyUnique( this, column, alter ); if ( column.isUnique() ) {
alter.append( dialect.getUniqueDelegate().applyUniqueToColumn( this, column ) );
}
if ( column.hasCheckConstraint() && dialect.supportsColumnCheck() ) { if ( column.hasCheckConstraint() && dialect.supportsColumnCheck() ) {
alter.append( " check(" ) alter.append( " check(" )
@ -520,8 +521,10 @@ public class Table implements RelationalModel, Serializable {
} }
dialect.getUniqueDelegate().applyUnique( this, col, buf ); if ( col.isUnique() ) {
buf.append( dialect.getUniqueDelegate().applyUniqueToColumn( this, col ) );
}
if ( col.hasCheckConstraint() && dialect.supportsColumnCheck() ) { if ( col.hasCheckConstraint() && dialect.supportsColumnCheck() ) {
buf.append( " check (" ) buf.append( " check (" )
.append( col.getCheckConstraint() ) .append( col.getCheckConstraint() )
@ -543,7 +546,7 @@ public class Table implements RelationalModel, Serializable {
.append( getPrimaryKey().sqlConstraintString( dialect ) ); .append( getPrimaryKey().sqlConstraintString( dialect ) );
} }
dialect.getUniqueDelegate().createUniqueConstraint( this, buf ); buf.append( dialect.getUniqueDelegate().applyUniquesToTable( this ) );
if ( dialect.supportsTableCheck() ) { if ( dialect.supportsTableCheck() ) {
Iterator chiter = checkConstraints.iterator(); Iterator chiter = checkConstraints.iterator();

View File

@ -22,89 +22,50 @@
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.mapping; package org.hibernate.mapping;
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;
/** /**
* A relational unique key constraint * A relational unique key constraint
* *
* @author Gavin King * @author Brett Meyer
*/ */
public class UniqueKey extends Constraint { public class UniqueKey extends Constraint {
public String sqlConstraintString(Dialect dialect) {
// TODO: This may not be necessary, but not all callers currently
// check it on their own. Go through their logic.
if ( !isGenerated( dialect ) ) return null;
StringBuilder buf = new StringBuilder( "unique (" );
Iterator iter = getColumnIterator();
while ( iter.hasNext() ) {
Column column = (Column) iter.next();
buf.append( column.getQuotedName( dialect ) );
if ( iter.hasNext() ) {
buf.append( ", " );
}
}
return buf.append( ')' ).toString();
}
@Override @Override
public String sqlConstraintString( public String sqlConstraintString(
Dialect dialect, Dialect dialect,
String constraintName, String constraintName,
String defaultCatalog, String defaultCatalog,
String defaultSchema) { String defaultSchema) {
// TODO: This may not be necessary, but not all callers currently return dialect.getUniqueDelegate().uniqueConstraintSql( this );
// check it on their own. Go through their logic.
if ( !isGenerated( dialect ) ) return null;
StringBuilder buf = new StringBuilder(
dialect.getAddUniqueConstraintString( constraintName ) ).append( '(' );
Iterator iter = getColumnIterator();
while ( iter.hasNext() ) {
Column column = (Column) iter.next();
buf.append( column.getQuotedName( dialect ) );
if ( iter.hasNext() ) buf.append( ", " );
}
return buf.append( ')' ).toString();
} }
@Override @Override
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) { public String sqlCreateString(Dialect dialect, Mapping p,
if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) { String defaultCatalog, String defaultSchema) {
return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema ); return dialect.getUniqueDelegate().applyUniquesOnAlter(
} this, defaultCatalog, defaultSchema );
else {
return Index.buildSqlCreateIndexString( dialect, getName(), getTable(), getColumnIterator(), true,
defaultCatalog, defaultSchema );
}
} }
@Override @Override
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) { public String sqlDropString(Dialect dialect, String defaultCatalog,
if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) { String defaultSchema) {
return super.sqlDropString( dialect, defaultCatalog, defaultSchema ); return dialect.getUniqueDelegate().dropUniquesOnAlter(
} this, defaultCatalog, defaultSchema );
else {
return Index.buildSqlDropIndexString( dialect, getTable(), getName(), defaultCatalog, defaultSchema );
}
} }
@Override // @Override
public boolean isGenerated(Dialect dialect) { // public boolean isGenerated(Dialect dialect) {
if ( !dialect.supportsUniqueConstraintInCreateAlterTable() ) return false; // if ( !dialect.supportsUniqueConstraintInCreateAlterTable() ) return false;
if ( dialect.supportsNotNullUnique() ) return true; // if ( dialect.supportsNotNullUnique() ) return true;
//
Iterator iter = getColumnIterator(); // Iterator iter = getColumnIterator();
while ( iter.hasNext() ) { // while ( iter.hasNext() ) {
// Dialect does not support "not null unique" and this column is not null. // // Dialect does not support "not null unique" and this column is not null.
if ( ! ( (Column) iter.next() ).isNullable() ) return false; // if ( ! ( (Column) iter.next() ).isNullable() ) return false;
} // }
return true; // return true;
} // }
} }