package org.hibernate.dialect.unique;
import java.util.Iterator;
import org.hibernate.boot.Metadata;
import org.hibernate.dialect.Dialect;
import org.hibernate.mapping.UniqueKey;
public class DB2UniqueDelegate extends DefaultUniqueDelegate {
public DB2UniqueDelegate( Dialect dialect ) {
super( dialect );
}
@Override
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
if ( hasNullable( uniqueKey ) ) {
return org.hibernate.mapping.Index.buildSqlCreateIndexString(
dialect,
uniqueKey.getName(),
uniqueKey.getTable(),
uniqueKey.columnIterator(),
uniqueKey.getColumnOrderMap(),
true,
metadata
);
}
else {
return super.getAlterTableToAddUniqueKeyCommand( uniqueKey, metadata );
}
}
@Override
public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
if ( hasNullable( uniqueKey ) ) {
return org.hibernate.mapping.Index.buildSqlDropIndexString(
uniqueKey.getName(),
metadata.getDatabase().getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
uniqueKey.getTable().getQualifiedTableName(),
metadata.getDatabase().getJdbcEnvironment().getDialect()
)
);
}
else {
return super.getAlterTableToDropUniqueKeyCommand( uniqueKey, metadata );
}
}
private boolean hasNullable(org.hibernate.mapping.UniqueKey uniqueKey) {
final Iterator<org.hibernate.mapping.Column> iter = uniqueKey.columnIterator();
while ( iter.hasNext() ) {
if ( iter.next().isNullable() ) {
return true;
}
}
return false;
}
}