HHH-18153 Fix for Informix unique constraints on nullness columns

This commit is contained in:
Vladimír Kuruc 2024-05-22 15:06:25 +02:00 committed by Christian Beikov
parent 86d40a4f87
commit a3f8addd51
1 changed files with 21 additions and 10 deletions

View File

@ -9,31 +9,42 @@ package org.hibernate.community.dialect.unique;
import org.hibernate.boot.Metadata; import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.SqlStringGenerationContext; import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.unique.AlterTableUniqueDelegate; import org.hibernate.dialect.unique.SkipNullableUniqueDelegate;
import org.hibernate.mapping.UniqueKey; import org.hibernate.mapping.UniqueKey;
/** /**
* Informix requires the constraint name to come last on the alter table. * Informix requires the constraint name to come last.
* *
* @author Brett Meyer * @author Brett Meyer
*/ */
public class InformixUniqueDelegate extends AlterTableUniqueDelegate { public class InformixUniqueDelegate extends SkipNullableUniqueDelegate {
public InformixUniqueDelegate( Dialect dialect ) { public InformixUniqueDelegate( Dialect dialect ) {
super( dialect ); super( dialect );
} }
// legacy model ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @Override
protected void appendUniqueConstraint(StringBuilder fragment, UniqueKey uniqueKey) {
if ( !uniqueKey.hasNullableColumn() ) {
fragment.append( ", " );
fragment.append( uniqueConstraintSql(uniqueKey) );
if ( uniqueKey.isNameExplicit() ) {
fragment.append( " constraint " ).append( uniqueKey.getName() );
}
}
}
@Override @Override
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata, public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata,
SqlStringGenerationContext context) { SqlStringGenerationContext context) {
// Do this here, rather than allowing UniqueKey/Constraint to do it. if (uniqueKey.hasNullableColumn() || !context.isMigration()){
// We need full, simplified control over whether it happens. return "";
} else {
final String tableName = context.format( uniqueKey.getTable().getQualifiedTableName() ); final String tableName = context.format( uniqueKey.getTable().getQualifiedTableName() );
final String constraintName = dialect.quote( uniqueKey.getName() ); final String constraintName = dialect.quote( uniqueKey.getName() );
return dialect.getAlterTableString( tableName ) return dialect.getAlterTableString( tableName )
+ " add constraint " + uniqueConstraintSql( uniqueKey ) + " constraint " + constraintName; + " add constraint " + uniqueConstraintSql( uniqueKey ) + " constraint " + constraintName;
} }
}
} }