move the code for generating check constraints to Dialect

This commit is contained in:
Gavin King 2021-02-24 16:13:45 +01:00
parent e4111a5453
commit e4f1461e17
3 changed files with 88 additions and 55 deletions

View File

@ -264,6 +264,86 @@ public abstract class Dialect implements ConversionContext {
: size;
}
/**
* Does the given JDBC type code represent some sort of
* numeric type?
* @param sqlType a JDBC type code from {@link Types}
*/
private static boolean isNumericType(int sqlType) {
switch (sqlType) {
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:
return true;
default:
return false;
}
}
/**
* Does the given JDBC type code represent some sort of
* string type?
* @param sqlType a JDBC type code from {@link Types}
*/
private static boolean isCharacterType(int sqlType) {
switch (sqlType) {
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
case Types.NCHAR:
case Types.NVARCHAR:
case Types.LONGNVARCHAR:
return true;
default:
return false;
}
}
/**
* Render a SQL check condition for a column that represents a boolean value.
*/
public String getBooleanCheckCondition(String columnName, int sqlType, char falseChar, char trueChar) {
if ( isCharacterType(sqlType) ) {
return columnName + " in ('" + falseChar + "','" + trueChar + "')";
}
else if ( isNumericType(sqlType) && !supportsBitType() ) {
return columnName + " in (0,1)";
}
else {
return null;
}
}
/**
* Render a SQL check condition for a column that represents an enumerated value.
*/
public String getEnumCheckCondition(String columnName, int sqlType, Class<? extends Enum<?>> enumClass) {
if ( isCharacterType(sqlType) ) {
StringBuilder check = new StringBuilder();
check.append( columnName ).append( " in (" );
String separator = "";
for ( Enum<?> value : enumClass.getEnumConstants() ) {
check.append( separator ).append('\'').append( value.name() ).append('\'');
separator = ",";
}
return check.append( ')' ).toString();
}
else if ( isNumericType(sqlType) ) {
int last = enumClass.getEnumConstants().length - 1;
return columnName + " between 0 and " + last;
}
else {
return null;
}
}
public abstract int getVersion();
/**

View File

@ -6,8 +6,6 @@
*/
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;
@ -160,28 +158,12 @@ public class BooleanTypeDescriptor extends AbstractClassTypeDescriptor<Boolean>
@Override
public String getCheckCondition(String columnName, SqlTypeDescriptor sqlTypeDescriptor, Dialect dialect) {
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;
return dialect.getBooleanCheckCondition(
columnName,
sqlTypeDescriptor.getSqlType(),
characterValueFalse,
characterValueTrue
);
}
}

View File

@ -218,35 +218,6 @@ public class EnumJavaTypeDescriptor<T extends Enum<T>> extends AbstractClassType
@Override
public String getCheckCondition(String columnName, SqlTypeDescriptor sqlTypeDescriptor, Dialect dialect) {
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 = ",";
}
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;
return dialect.getEnumCheckCondition( columnName, sqlTypeDescriptor.getSqlType(), getJavaTypeClass() );
}
}