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.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.unique.AlterTableUniqueDelegate;
import org.hibernate.dialect.unique.SkipNullableUniqueDelegate;
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
*/
public class InformixUniqueDelegate extends AlterTableUniqueDelegate {
public class InformixUniqueDelegate extends SkipNullableUniqueDelegate {
public InformixUniqueDelegate( Dialect 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
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata,
SqlStringGenerationContext context) {
// Do this here, rather than allowing UniqueKey/Constraint to do it.
// We need full, simplified control over whether it happens.
if (uniqueKey.hasNullableColumn() || !context.isMigration()){
return "";
} else {
final String tableName = context.format( uniqueKey.getTable().getQualifiedTableName() );
final String constraintName = dialect.quote( uniqueKey.getName() );
return dialect.getAlterTableString( tableName )
+ " add constraint " + uniqueConstraintSql( uniqueKey ) + " constraint " + constraintName;
}
}
}