Fix check constraint generation

This commit is contained in:
Christian Beikov 2021-02-24 10:30:06 +01:00
parent 0fd5fe7d5e
commit 9ce2fa9cb4
2 changed files with 53 additions and 17 deletions

View File

@ -6,6 +6,8 @@
*/
package org.hibernate.type.descriptor.java;
import java.sql.Types;
import org.hibernate.dialect.Dialect;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.spi.Primitive;
@ -69,7 +71,7 @@ public class BooleanTypeDescriptor extends AbstractClassTypeDescriptor<Boolean>
return (X) toInteger( value );
}
if ( Long.class.isAssignableFrom( type ) ) {
return (X) toInteger( value );
return (X) toLong( value );
}
if ( Character.class.isAssignableFrom( type ) ) {
return (X) Character.valueOf( value ? characterValueTrue : characterValueFalse );
@ -158,8 +160,28 @@ public class BooleanTypeDescriptor extends AbstractClassTypeDescriptor<Boolean>
@Override
public String getCheckCondition(String columnName, SqlTypeDescriptor sqlTypeDescriptor, Dialect dialect) {
return dialect.toBooleanValueString(true).equals("1") && !dialect.supportsBitType()
? columnName + " in (0,1)"
: null;
switch ( sqlTypeDescriptor.getSqlType() ) {
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
case Types.NCHAR:
case Types.NVARCHAR:
case Types.LONGNVARCHAR:
return columnName + " in ('" + characterValueFalse + "','" + characterValueTrue + "')";
case Types.BIT:
case Types.SMALLINT:
case Types.TINYINT:
case Types.INTEGER:
case Types.BIGINT:
case Types.DOUBLE:
case Types.REAL:
case Types.FLOAT:
case Types.NUMERIC:
case Types.DECIMAL:
if ( !dialect.supportsBitType() ) {
return columnName + " in (0,1)";
}
}
return null;
}
}

View File

@ -218,20 +218,34 @@ public class EnumJavaTypeDescriptor<T extends Enum<T>> extends AbstractClassType
@Override
public String getCheckCondition(String columnName, SqlTypeDescriptor sqlTypeDescriptor, Dialect dialect) {
if (sqlTypeDescriptor instanceof TinyIntTypeDescriptor
|| sqlTypeDescriptor instanceof IntegerTypeDescriptor) {
int last = getJavaTypeClass().getEnumConstants().length - 1;
return columnName + " between 0 and " + last;
}
else if (sqlTypeDescriptor instanceof VarcharTypeDescriptor) {
StringBuilder types = new StringBuilder();
for ( Enum<T> value : getJavaTypeClass().getEnumConstants() ) {
if (types.length() != 0) {
types.append(", ");
switch ( sqlTypeDescriptor.getSqlType() ) {
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
case Types.NCHAR:
case Types.NVARCHAR:
case Types.LONGNVARCHAR:
final StringBuilder sb = new StringBuilder();
sb.append( columnName ).append( " in (" );
String separator = "";
for ( Enum<T> value : getJavaTypeClass().getEnumConstants() ) {
sb.append( separator );
sb.append('\'').append( value.name() ).append('\'');
separator = ",";
}
types.append("'").append( value.name() ).append("'");
}
return columnName + " in (" + types + ")";
return sb.append( ')' ).toString();
case Types.BIT:
case Types.SMALLINT:
case Types.TINYINT:
case Types.INTEGER:
case Types.BIGINT:
case Types.DOUBLE:
case Types.REAL:
case Types.FLOAT:
case Types.NUMERIC:
case Types.DECIMAL:
int last = getJavaTypeClass().getEnumConstants().length - 1;
return columnName + " between 0 and " + last;
}
return null;
}