HHH-7797 Initial attempt at using UniqueDelegate within metamodel
This commit is contained in:
parent
1416126a00
commit
7254d465ae
|
@ -23,9 +23,9 @@ package org.hibernate.dialect.unique;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
import org.hibernate.dialect.Dialect;
|
import org.hibernate.dialect.Dialect;
|
||||||
import org.hibernate.mapping.Column;
|
import org.hibernate.metamodel.relational.Column;
|
||||||
import org.hibernate.mapping.Table;
|
import org.hibernate.metamodel.relational.Table;
|
||||||
import org.hibernate.mapping.UniqueKey;
|
import org.hibernate.metamodel.relational.UniqueKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default UniqueDelegate implementation for most dialects. Uses
|
* The default UniqueDelegate implementation for most dialects. Uses
|
||||||
|
@ -41,6 +41,29 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
||||||
this.dialect = dialect;
|
this.dialect = dialect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String applyUniqueToColumn( org.hibernate.mapping.Table table,
|
||||||
|
org.hibernate.mapping.Column column ) {
|
||||||
|
// if ( column.isUnique()
|
||||||
|
// && ( column.isNullable()
|
||||||
|
// || dialect.supportsNotNullUnique() ) ) {
|
||||||
|
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
||||||
|
// // If the constraint is supported, do not add to the column syntax.
|
||||||
|
// UniqueKey uk = getOrCreateUniqueKey( column.getQuotedName( dialect ) + '_' );
|
||||||
|
// uk.addColumn( column );
|
||||||
|
// }
|
||||||
|
// else if ( dialect.supportsUnique() ) {
|
||||||
|
// // Otherwise, add to the column syntax if supported.
|
||||||
|
// sb.append( " unique" );
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
org.hibernate.mapping.UniqueKey uk = table.getOrCreateUniqueKey(
|
||||||
|
column.getQuotedName( dialect ) + '_' );
|
||||||
|
uk.addColumn( column );
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String applyUniqueToColumn( Table table, Column column ) {
|
public String applyUniqueToColumn( Table table, Column column ) {
|
||||||
// if ( column.isUnique()
|
// if ( column.isUnique()
|
||||||
|
@ -57,12 +80,25 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
UniqueKey uk = table.getOrCreateUniqueKey(
|
UniqueKey uk = table.getOrCreateUniqueKey( column.getColumnName()
|
||||||
column.getQuotedName( dialect ) + '_' );
|
.encloseInQuotesIfQuoted( dialect ) + '_' );
|
||||||
uk.addColumn( column );
|
uk.addColumn( column );
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String applyUniquesToTable( org.hibernate.mapping.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
|
@Override
|
||||||
public String applyUniquesToTable( Table table ) {
|
public String applyUniquesToTable( Table table ) {
|
||||||
// TODO: Am I correct that this shouldn't be done unless the constraint
|
// TODO: Am I correct that this shouldn't be done unless the constraint
|
||||||
|
@ -77,7 +113,7 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String applyUniquesOnAlter( UniqueKey uniqueKey,
|
public String applyUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey,
|
||||||
String defaultCatalog, String defaultSchema ) {
|
String defaultCatalog, String defaultSchema ) {
|
||||||
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
||||||
// return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema );
|
// return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema );
|
||||||
|
@ -97,7 +133,25 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String dropUniquesOnAlter( UniqueKey uniqueKey,
|
public String applyUniquesOnAlter( UniqueKey uniqueKey ) {
|
||||||
|
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
||||||
|
// return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema );
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// return Index.buildSqlCreateIndexString( dialect, getName(), getTable(), getColumnIterator(), true,
|
||||||
|
// defaultCatalog, defaultSchema );
|
||||||
|
// }
|
||||||
|
|
||||||
|
return new StringBuilder( "alter table " )
|
||||||
|
.append( uniqueKey.getTable().getQualifiedName( dialect ) )
|
||||||
|
.append( " add constraint " )
|
||||||
|
.append( uniqueKey.getName() )
|
||||||
|
.append( uniqueConstraintSql( uniqueKey ) )
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String dropUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey,
|
||||||
String defaultCatalog, String defaultSchema ) {
|
String defaultCatalog, String defaultSchema ) {
|
||||||
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
||||||
// return super.sqlDropString( dialect, defaultCatalog, defaultSchema );
|
// return super.sqlDropString( dialect, defaultCatalog, defaultSchema );
|
||||||
|
@ -115,7 +169,23 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String uniqueConstraintSql( UniqueKey uniqueKey ) {
|
public String dropUniquesOnAlter( UniqueKey uniqueKey ) {
|
||||||
|
// 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 ) )
|
||||||
|
.append( " drop constraint " )
|
||||||
|
.append( dialect.quote( uniqueKey.getName() ) )
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String uniqueConstraintSql( org.hibernate.mapping.UniqueKey uniqueKey ) {
|
||||||
// TODO: This may not be necessary, but not all callers currently
|
// TODO: This may not be necessary, but not all callers currently
|
||||||
// check it on their own. Go through their logic.
|
// check it on their own. Go through their logic.
|
||||||
// if ( !isGenerated( dialect ) ) return null;
|
// if ( !isGenerated( dialect ) ) return null;
|
||||||
|
@ -124,7 +194,29 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
||||||
sb.append( " unique (" );
|
sb.append( " unique (" );
|
||||||
Iterator columnIterator = uniqueKey.getColumnIterator();
|
Iterator columnIterator = uniqueKey.getColumnIterator();
|
||||||
while ( columnIterator.hasNext() ) {
|
while ( columnIterator.hasNext() ) {
|
||||||
Column column = (Column) columnIterator.next();
|
org.hibernate.mapping.Column column
|
||||||
|
= (org.hibernate.mapping.Column) columnIterator.next();
|
||||||
|
sb.append( column.getQuotedName( dialect ) );
|
||||||
|
if ( columnIterator.hasNext() ) {
|
||||||
|
sb.append( ", " );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.append( ')' ).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.getColumns().iterator();
|
||||||
|
while ( columnIterator.hasNext() ) {
|
||||||
|
org.hibernate.mapping.Column column
|
||||||
|
= (org.hibernate.mapping.Column) columnIterator.next();
|
||||||
sb.append( column.getQuotedName( dialect ) );
|
sb.append( column.getQuotedName( dialect ) );
|
||||||
if ( columnIterator.hasNext() ) {
|
if ( columnIterator.hasNext() ) {
|
||||||
sb.append( ", " );
|
sb.append( ", " );
|
||||||
|
|
|
@ -20,9 +20,9 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.dialect.unique;
|
package org.hibernate.dialect.unique;
|
||||||
|
|
||||||
import org.hibernate.mapping.Column;
|
import org.hibernate.metamodel.relational.Column;
|
||||||
import org.hibernate.mapping.Table;
|
import org.hibernate.metamodel.relational.Table;
|
||||||
import org.hibernate.mapping.UniqueKey;
|
import org.hibernate.metamodel.relational.UniqueKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dialect-level delegate in charge of applying "uniqueness" to a column.
|
* Dialect-level delegate in charge of applying "uniqueness" to a column.
|
||||||
|
@ -40,6 +40,18 @@ import org.hibernate.mapping.UniqueKey;
|
||||||
*/
|
*/
|
||||||
public interface UniqueDelegate {
|
public interface UniqueDelegate {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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( org.hibernate.mapping.Table table,
|
||||||
|
org.hibernate.mapping.Column column );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the delegate supports unique constraints, this method should simply
|
* If the delegate supports unique constraints, this method should simply
|
||||||
* create the UniqueKey on the Table. Otherwise, the constraint isn't
|
* create the UniqueKey on the Table. Otherwise, the constraint isn't
|
||||||
|
@ -51,6 +63,16 @@ public interface UniqueDelegate {
|
||||||
*/
|
*/
|
||||||
public String applyUniqueToColumn( Table table, Column column );
|
public String applyUniqueToColumn( Table table, Column column );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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( org.hibernate.mapping.Table table );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If creating unique constraints in separate alter statements are not
|
* If creating unique constraints in separate alter statements are not
|
||||||
* supported, this method should return the syntax necessary to create
|
* supported, this method should return the syntax necessary to create
|
||||||
|
@ -70,9 +92,18 @@ public interface UniqueDelegate {
|
||||||
* @param defaultSchema
|
* @param defaultSchema
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
public String applyUniquesOnAlter( UniqueKey uniqueKey,
|
public String applyUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey,
|
||||||
String defaultCatalog, String defaultSchema );
|
String defaultCatalog, String defaultSchema );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If creating unique constraints in separate alter statements is
|
||||||
|
* supported, generate the necessary "alter" syntax for the given key.
|
||||||
|
*
|
||||||
|
* @param uniqueKey
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
public String applyUniquesOnAlter( UniqueKey uniqueKey );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If dropping unique constraints in separate alter statements is
|
* If dropping unique constraints in separate alter statements is
|
||||||
* supported, generate the necessary "alter" syntax for the given key.
|
* supported, generate the necessary "alter" syntax for the given key.
|
||||||
|
@ -82,9 +113,27 @@ public interface UniqueDelegate {
|
||||||
* @param defaultSchema
|
* @param defaultSchema
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
public String dropUniquesOnAlter( UniqueKey uniqueKey,
|
public String dropUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey,
|
||||||
String defaultCatalog, String defaultSchema );
|
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
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
public String dropUniquesOnAlter( UniqueKey uniqueKey );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates the syntax necessary to create the unique constraint (reused
|
||||||
|
* by all methods). Ex: "unique (column1, column2, ...)"
|
||||||
|
*
|
||||||
|
* @param uniqueKey
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
public String uniqueConstraintSql( org.hibernate.mapping.UniqueKey uniqueKey );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates the syntax necessary to create the unique constraint (reused
|
* Generates the syntax necessary to create the unique constraint (reused
|
||||||
* by all methods). Ex: "unique (column1, column2, ...)"
|
* by all methods). Ex: "unique (column1, column2, ...)"
|
||||||
|
|
|
@ -38,7 +38,9 @@ public class UniqueKey extends Constraint {
|
||||||
String constraintName,
|
String constraintName,
|
||||||
String defaultCatalog,
|
String defaultCatalog,
|
||||||
String defaultSchema) {
|
String defaultSchema) {
|
||||||
return dialect.getUniqueDelegate().uniqueConstraintSql( this );
|
// return dialect.getUniqueDelegate().uniqueConstraintSql( this );
|
||||||
|
// Not used.
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -115,10 +115,8 @@ public class Database {
|
||||||
for ( Schema schema : schemaMap.values() ) {
|
for ( Schema schema : schemaMap.values() ) {
|
||||||
for ( Table table : schema.getTables() ) {
|
for ( Table table : schema.getTables() ) {
|
||||||
|
|
||||||
if ( ! dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
for ( UniqueKey uniqueKey : table.getUniqueKeys() ) {
|
||||||
for ( UniqueKey uniqueKey : table.getUniqueKeys() ) {
|
addSqlCreateStrings( dialect, exportIdentifiers, script, uniqueKey );
|
||||||
addSqlCreateStrings( dialect, exportIdentifiers, script, uniqueKey );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( Index index : table.getIndexes() ) {
|
for ( Index index : table.getIndexes() ) {
|
||||||
|
|
|
@ -199,20 +199,8 @@ public class Table extends AbstractTableSpecification implements Exportable {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the column is 1.) unique and nullable or 2.) unique,
|
if ( col.isUnique() ) {
|
||||||
// not null, and the dialect supports unique not null
|
buf.append( dialect.getUniqueDelegate().applyUniqueToColumn( this, col ) );
|
||||||
if ( col.isUnique()
|
|
||||||
&& ( col.isNullable()
|
|
||||||
|| dialect.supportsNotNullUnique() ) ) {
|
|
||||||
if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
|
||||||
// If the constraint is supported, do not add to the column syntax.
|
|
||||||
UniqueKey uk = getOrCreateUniqueKey( col.getColumnName().encloseInQuotesIfQuoted( dialect ) + '_' );
|
|
||||||
uk.addColumn( col );
|
|
||||||
}
|
|
||||||
else if ( dialect.supportsUnique() ) {
|
|
||||||
// Otherwise, add to the column syntax if supported.
|
|
||||||
buf.append( " unique" );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( col.getCheckCondition() != null && dialect.supportsColumnCheck() ) {
|
if ( col.getCheckCondition() != null && dialect.supportsColumnCheck() ) {
|
||||||
|
@ -231,14 +219,7 @@ public class Table extends AbstractTableSpecification implements Exportable {
|
||||||
.append( getPrimaryKey().sqlConstraintStringInCreateTable( dialect ) );
|
.append( getPrimaryKey().sqlConstraintStringInCreateTable( dialect ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
buf.append( dialect.getUniqueDelegate().applyUniquesToTable( this ) );
|
||||||
for ( UniqueKey uk : uniqueKeys.values() ) {
|
|
||||||
String constraint = uk.sqlConstraintStringInCreateTable( dialect );
|
|
||||||
if ( constraint != null ) {
|
|
||||||
buf.append( ", " ).append( constraint );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( dialect.supportsTableCheck() ) {
|
if ( dialect.supportsTableCheck() ) {
|
||||||
for ( CheckConstraint checkConstraint : checkConstraints ) {
|
for ( CheckConstraint checkConstraint : checkConstraints ) {
|
||||||
|
|
|
@ -47,54 +47,22 @@ public class UniqueKey extends AbstractConstraint implements Constraint {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isCreationVetoed(Dialect dialect) {
|
public String[] sqlCreateStrings(Dialect dialect) {
|
||||||
if ( !dialect.supportsUniqueConstraintInCreateAlterTable() ) return true;
|
return new String[] {
|
||||||
if ( dialect.supportsNotNullUnique() ) return false;
|
dialect.getUniqueDelegate().applyUniquesOnAlter( this )
|
||||||
|
};
|
||||||
for ( Column column : getColumns() ) {
|
|
||||||
// Dialect does not support "not null unique" and this column is not null.
|
|
||||||
if ( ! column.isNullable() ) return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String sqlConstraintStringInCreateTable(Dialect dialect) {
|
|
||||||
// TODO: This may not be necessary, but not all callers currently
|
|
||||||
// check it on their own. Go through their logic.
|
|
||||||
if ( isCreationVetoed( dialect ) ) return null;
|
|
||||||
|
|
||||||
StringBuilder buf = new StringBuilder( "unique (" );
|
|
||||||
boolean first = true;
|
|
||||||
for ( Column column : getColumns() ) {
|
|
||||||
if ( first ) {
|
|
||||||
first = false;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
buf.append( ", " );
|
|
||||||
}
|
|
||||||
buf.append( column.getColumnName().encloseInQuotesIfQuoted( dialect ) );
|
|
||||||
}
|
|
||||||
return buf.append( ')' ).toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String sqlConstraintStringInAlterTable(Dialect dialect) {
|
public String[] sqlDropStrings(Dialect dialect) {
|
||||||
// TODO: This may not be necessary, but not all callers currently
|
return new String[] {
|
||||||
// check it on their own. Go through their logic.
|
dialect.getUniqueDelegate().dropUniquesOnAlter( this )
|
||||||
if ( isCreationVetoed( dialect ) ) return null;
|
};
|
||||||
|
}
|
||||||
StringBuilder buf = new StringBuilder(
|
|
||||||
dialect.getAddUniqueConstraintString( getName() ) ).append( '(' );
|
@Override
|
||||||
boolean first = true;
|
protected String sqlConstraintStringInAlterTable(Dialect dialect) {
|
||||||
for ( Column column : getColumns() ) {
|
// not used
|
||||||
if ( first ) {
|
return "";
|
||||||
first = false;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
buf.append( ", " );
|
|
||||||
}
|
|
||||||
buf.append( column.getColumnName().encloseInQuotesIfQuoted( dialect ) );
|
|
||||||
}
|
|
||||||
return buf.append( ')' ).toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue