HHH-7797 Finished first take on UniqueDelegate and rolled into .cfg and
.mapping
This commit is contained in:
parent
0a2b639808
commit
1416126a00
|
@ -1045,17 +1045,15 @@ public class Configuration implements Serializable {
|
|||
Table table = (Table) iter.next();
|
||||
if ( table.isPhysicalTable() ) {
|
||||
|
||||
if ( !dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
||||
Iterator subIter = table.getUniqueKeyIterator();
|
||||
while ( subIter.hasNext() ) {
|
||||
UniqueKey uk = (UniqueKey) subIter.next();
|
||||
String constraintString = uk.sqlCreateString( dialect, mapping, defaultCatalog, defaultSchema );
|
||||
if (constraintString != null) script.add( constraintString );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Iterator subIter = table.getIndexIterator();
|
||||
subIter = table.getIndexIterator();
|
||||
while ( subIter.hasNext() ) {
|
||||
Index index = (Index) subIter.next();
|
||||
script.add(
|
||||
|
|
|
@ -30,7 +30,6 @@ import java.sql.Types;
|
|||
|
||||
import org.hibernate.JDBCException;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.dialect.function.AnsiTrimEmulationFunction;
|
||||
import org.hibernate.dialect.function.AvgWithArgumentCastFunction;
|
||||
import org.hibernate.dialect.function.NoArgSQLFunction;
|
||||
import org.hibernate.dialect.function.SQLFunctionTemplate;
|
||||
|
@ -279,10 +278,6 @@ public class DB2Dialect extends Dialect {
|
|||
return false;
|
||||
}
|
||||
@Override
|
||||
public boolean supportsNotNullUnique() {
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public boolean supportsExistsInSelect() {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
package org.hibernate.dialect.unique;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.mapping.Column;
|
||||
|
@ -43,7 +42,7 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void applyUnique( Table table, Column column, StringBuilder sb ) {
|
||||
public String applyUniqueToColumn( Table table, Column column ) {
|
||||
// if ( column.isUnique()
|
||||
// && ( column.isNullable()
|
||||
// || dialect.supportsNotNullUnique() ) ) {
|
||||
|
@ -61,29 +60,78 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
|||
UniqueKey uk = table.getOrCreateUniqueKey(
|
||||
column.getQuotedName( dialect ) + '_' );
|
||||
uk.addColumn( column );
|
||||
return "";
|
||||
}
|
||||
|
||||
@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() ) {
|
||||
// Iterator ukiter = getUniqueKeyIterator();
|
||||
// while ( ukiter.hasNext() ) {
|
||||
// UniqueKey uk = (UniqueKey) ukiter.next();
|
||||
// String constraint = uk.sqlConstraintString( dialect );
|
||||
// if ( constraint != null ) {
|
||||
// buf.append( ", " ).append( constraint );
|
||||
// }
|
||||
// return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema );
|
||||
// }
|
||||
// else {
|
||||
// return Index.buildSqlCreateIndexString( dialect, getName(), getTable(), getColumnIterator(), true,
|
||||
// defaultCatalog, defaultSchema );
|
||||
// }
|
||||
|
||||
Iterator ukiter = table.getUniqueKeyIterator();
|
||||
while ( ukiter.hasNext() ) {
|
||||
UniqueKey uk = (UniqueKey) ukiter.next();
|
||||
String constraint = uk.sqlConstraintString( dialect );
|
||||
if ( constraint != null ) {
|
||||
sb.append( ", " ).append( constraint );
|
||||
return new StringBuilder( "alter table " )
|
||||
.append( uniqueKey.getTable().getQualifiedName(
|
||||
dialect, defaultCatalog, defaultSchema ) )
|
||||
.append( " add constraint " )
|
||||
.append( uniqueKey.getName() )
|
||||
.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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ package org.hibernate.dialect.unique;
|
|||
|
||||
import org.hibernate.mapping.Column;
|
||||
import org.hibernate.mapping.Table;
|
||||
import org.hibernate.mapping.UniqueKey;
|
||||
|
||||
/**
|
||||
* Dialect-level delegate in charge of applying "uniqueness" to a column.
|
||||
|
@ -39,7 +40,57 @@ import org.hibernate.mapping.Table;
|
|||
*/
|
||||
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 );
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@ package org.hibernate.mapping;
|
|||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
|
@ -423,7 +422,9 @@ public class Table implements RelationalModel, Serializable {
|
|||
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() ) {
|
||||
alter.append( " check(" )
|
||||
|
@ -520,7 +521,9 @@ 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() ) {
|
||||
buf.append( " check (" )
|
||||
|
@ -543,7 +546,7 @@ public class Table implements RelationalModel, Serializable {
|
|||
.append( getPrimaryKey().sqlConstraintString( dialect ) );
|
||||
}
|
||||
|
||||
dialect.getUniqueDelegate().createUniqueConstraint( this, buf );
|
||||
buf.append( dialect.getUniqueDelegate().applyUniquesToTable( this ) );
|
||||
|
||||
if ( dialect.supportsTableCheck() ) {
|
||||
Iterator chiter = checkConstraints.iterator();
|
||||
|
|
|
@ -22,89 +22,50 @@
|
|||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.mapping;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.engine.spi.Mapping;
|
||||
|
||||
/**
|
||||
* A relational unique key constraint
|
||||
*
|
||||
* @author Gavin King
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
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
|
||||
public String sqlConstraintString(
|
||||
Dialect dialect,
|
||||
String constraintName,
|
||||
String defaultCatalog,
|
||||
String defaultSchema) {
|
||||
// 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(
|
||||
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();
|
||||
return dialect.getUniqueDelegate().uniqueConstraintSql( this );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) {
|
||||
if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
||||
return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema );
|
||||
}
|
||||
else {
|
||||
return Index.buildSqlCreateIndexString( dialect, getName(), getTable(), getColumnIterator(), true,
|
||||
defaultCatalog, defaultSchema );
|
||||
}
|
||||
public String sqlCreateString(Dialect dialect, Mapping p,
|
||||
String defaultCatalog, String defaultSchema) {
|
||||
return dialect.getUniqueDelegate().applyUniquesOnAlter(
|
||||
this, defaultCatalog, defaultSchema );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) {
|
||||
if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
||||
return super.sqlDropString( dialect, defaultCatalog, defaultSchema );
|
||||
}
|
||||
else {
|
||||
return Index.buildSqlDropIndexString( dialect, getTable(), getName(), defaultCatalog, defaultSchema );
|
||||
}
|
||||
public String sqlDropString(Dialect dialect, String defaultCatalog,
|
||||
String defaultSchema) {
|
||||
return dialect.getUniqueDelegate().dropUniquesOnAlter(
|
||||
this, defaultCatalog, defaultSchema );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isGenerated(Dialect dialect) {
|
||||
if ( !dialect.supportsUniqueConstraintInCreateAlterTable() ) return false;
|
||||
if ( dialect.supportsNotNullUnique() ) return true;
|
||||
|
||||
Iterator iter = getColumnIterator();
|
||||
while ( iter.hasNext() ) {
|
||||
// Dialect does not support "not null unique" and this column is not null.
|
||||
if ( ! ( (Column) iter.next() ).isNullable() ) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// @Override
|
||||
// public boolean isGenerated(Dialect dialect) {
|
||||
// if ( !dialect.supportsUniqueConstraintInCreateAlterTable() ) return false;
|
||||
// if ( dialect.supportsNotNullUnique() ) return true;
|
||||
//
|
||||
// Iterator iter = getColumnIterator();
|
||||
// while ( iter.hasNext() ) {
|
||||
// // Dialect does not support "not null unique" and this column is not null.
|
||||
// if ( ! ( (Column) iter.next() ).isNullable() ) return false;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue