HHH-17276 Be more forgiving when determining lobness

This commit is contained in:
Christian Beikov 2023-10-09 12:49:22 +02:00
parent df5fc98abd
commit e424eea9e9
14 changed files with 124 additions and 47 deletions

View File

@ -242,7 +242,7 @@ public class MariaDBLegacySqlAstTranslator<T extends JdbcOperation> extends Abst
@Override
public void visitCastTarget(CastTarget castTarget) {
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, dialect );
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, getSessionFactory() );
if ( sqlType != null ) {
appendSql( sqlType );
}

View File

@ -250,7 +250,7 @@ public class MySQLLegacySqlAstTranslator<T extends JdbcOperation> extends Abstra
@Override
public void visitCastTarget(CastTarget castTarget) {
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, getDialect() );
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, getSessionFactory() );
if ( sqlType != null ) {
appendSql( sqlType );
}

View File

@ -236,7 +236,7 @@ public class MariaDBSqlAstTranslator<T extends JdbcOperation> extends AbstractSq
@Override
public void visitCastTarget(CastTarget castTarget) {
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, dialect );
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, getSessionFactory() );
if ( sqlType != null ) {
appendSql( sqlType );
}

View File

@ -37,9 +37,20 @@ public class MySQLSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlA
super( sessionFactory, statement );
}
/**
* @deprecated Use {@link #getSqlType(CastTarget, SessionFactoryImplementor)} instead
*/
@Deprecated(forRemoval = true)
public static String getSqlType(CastTarget castTarget, Dialect dialect) {
final String sqlType = castTarget.getSqlType();
return getSqlType( castTarget, castTarget.getSqlType(), dialect );
}
public static String getSqlType(CastTarget castTarget, SessionFactoryImplementor factory) {
final String sqlType = getSqlTypeName( castTarget, factory );
return getSqlType( castTarget, sqlType, factory.getJdbcServices().getDialect() );
}
private static String getSqlType(CastTarget castTarget, String sqlType, Dialect dialect) {
if ( sqlType != null ) {
int parenthesesIndex = sqlType.indexOf( '(' );
final String baseName = parenthesesIndex == -1 ? sqlType : sqlType.substring( 0, parenthesesIndex );
@ -63,10 +74,14 @@ public class MySQLSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlA
case "varchar":
case "nchar":
case "nvarchar":
return "char";
return castTarget.getLength() == null
? "char"
: ( "char(" + castTarget.getLength() + ")" );
case "binary":
case "varbinary":
return "binary";
return castTarget.getLength() == null
? "binary"
: ( "binary(" + castTarget.getLength() + ")" );
}
}
return sqlType;
@ -286,7 +301,7 @@ public class MySQLSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlA
@Override
public void visitCastTarget(CastTarget castTarget) {
String sqlType = getSqlType( castTarget, getDialect() );
String sqlType = getSqlType( castTarget, getSessionFactory() );
if ( sqlType != null ) {
appendSql( sqlType );
}

View File

@ -193,7 +193,7 @@ public class TiDBSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAs
@Override
public void visitCastTarget(CastTarget castTarget) {
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, dialect );
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, getSessionFactory() );
if ( sqlType != null ) {
appendSql( sqlType );
}

View File

@ -20,6 +20,7 @@ import org.hibernate.sql.ast.SqlAstTranslator;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.tool.schema.extract.spi.ColumnTypeInformation;
import org.hibernate.type.descriptor.jdbc.AggregateJdbcType;
import org.hibernate.type.descriptor.sql.DdlType;
import org.hibernate.type.spi.TypeConfiguration;
import static org.hibernate.type.SqlTypes.BIGINT;
@ -231,7 +232,7 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
final int aggregateSqlTypeCode = aggregateColumn.getJdbcMapping().getJdbcType().getDefaultSqlTypeCode();
switch ( aggregateSqlTypeCode ) {
case JSON:
return jsonAggregateColumnWriter( aggregateColumn, columnsToUpdate );
return jsonAggregateColumnWriter( aggregateColumn, columnsToUpdate, typeConfiguration );
}
throw new IllegalArgumentException( "Unsupported aggregate SQL type: " + aggregateSqlTypeCode );
}
@ -269,8 +270,9 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
private WriteExpressionRenderer jsonAggregateColumnWriter(
SelectableMapping aggregateColumn,
SelectableMapping[] columns) {
return new RootJsonWriteExpression( aggregateColumn, columns, this );
SelectableMapping[] columns,
TypeConfiguration typeConfiguration) {
return new RootJsonWriteExpression( aggregateColumn, columns, this, typeConfiguration );
}
interface JsonWriteExpression {
@ -293,7 +295,10 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
this.ddlTypeName = aggregateSupport.determineJsonTypeName( selectableMapping );
}
protected void initializeSubExpressions(SelectableMapping[] columns, OracleAggregateSupport aggregateSupport) {
protected void initializeSubExpressions(
SelectableMapping[] columns,
OracleAggregateSupport aggregateSupport,
TypeConfiguration typeConfiguration) {
for ( SelectableMapping column : columns ) {
final SelectablePath selectablePath = column.getSelectablePath();
final SelectablePath[] parts = selectablePath.getParts();
@ -318,13 +323,33 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
aggregateSupport.jsonCustomWriteExpression(
customWriteExpression,
sqlTypeCode,
column.getColumnDefinition()
determineTypeName( column, typeConfiguration )
)
)
);
}
}
private static String determineTypeName(SelectableMapping column, TypeConfiguration typeConfiguration) {
final String typeName;
if ( column.getColumnDefinition() == null ) {
final DdlType ddlType = typeConfiguration.getDdlTypeRegistry().getDescriptor(
column.getJdbcMapping().getJdbcType().getDefaultSqlTypeCode()
);
return ddlType.getCastTypeName(
column.getJdbcMapping().getJdbcType(),
column.getJdbcMapping().getJavaTypeDescriptor(),
column.getLength(),
column.getPrecision(),
column.getScale()
);
}
else{
typeName = column.getColumnDefinition();
}
return typeName;
}
@Override
public void append(
SqlAppender sb,
@ -363,11 +388,12 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
RootJsonWriteExpression(
SelectableMapping aggregateColumn,
SelectableMapping[] columns,
OracleAggregateSupport aggregateSupport) {
OracleAggregateSupport aggregateSupport,
TypeConfiguration typeConfiguration) {
super( aggregateColumn, aggregateSupport );
this.nullable = aggregateColumn.isNullable();
this.path = aggregateColumn.getSelectionExpression();
initializeSubExpressions( columns, aggregateSupport );
initializeSubExpressions( columns, aggregateSupport, typeConfiguration );
}
@Override

View File

@ -29,6 +29,7 @@ import org.hibernate.type.ComponentType;
import org.hibernate.type.EntityType;
import org.hibernate.type.Type;
import org.hibernate.type.descriptor.JdbcTypeNameMapper;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.sql.DdlType;
import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry;
import org.hibernate.type.spi.TypeConfiguration;
@ -58,7 +59,7 @@ public class Column implements Selectable, Serializable, Cloneable, ColumnTypeIn
private boolean unique;
private String sqlTypeName;
private Integer sqlTypeCode;
private boolean sqlTypeLob;
private Boolean sqlTypeLob;
private boolean quoted;
private boolean explicit;
int uniqueInteger;
@ -509,6 +510,40 @@ public class Column implements Selectable, Serializable, Cloneable, ColumnTypeIn
}
public boolean isSqlTypeLob() {
return sqlTypeLob != null && sqlTypeLob;
}
public boolean isSqlTypeLob(Metadata mapping) {
final Database database = mapping.getDatabase();
final DdlTypeRegistry ddlTypeRegistry = database.getTypeConfiguration().getDdlTypeRegistry();
final Dialect dialect = database.getDialect();
if ( sqlTypeLob == null ) {
try {
final int typeCode = getSqlTypeCode( mapping );
final DdlType descriptor = ddlTypeRegistry.getDescriptor( typeCode );
if ( descriptor == null ) {
sqlTypeLob = JdbcType.isLob( typeCode );
}
else {
final Size size = getColumnSize( dialect, mapping );
sqlTypeLob = descriptor.isLob( size );
}
}
catch ( MappingException cause ) {
throw cause;
}
catch ( Exception cause ) {
throw new MappingException(
String.format(
Locale.ROOT,
"Unable to determine SQL type name for column '%s' of table '%s'",
getName(),
getValue().getTable().getName()
),
cause
);
}
}
return sqlTypeLob;
}

View File

@ -287,12 +287,12 @@ public abstract class AbstractEmbeddableMapping implements EmbeddableMappingType
final boolean nullable;
if ( selectable instanceof Column ) {
final Column column = (Column) selectable;
columnDefinition = column.getSqlType( creationProcess.getCreationContext().getMetadata() );
columnDefinition = column.getSqlType();
length = column.getLength();
precision = column.getPrecision();
scale = column.getScale();
nullable = column.isNullable();
isLob = column.isSqlTypeLob();
isLob = column.isSqlTypeLob( creationProcess.getCreationContext().getMetadata() );
selectablePath = basicValue.createSelectablePath( column.getQuotedName( dialect ) );
}
else {

View File

@ -86,7 +86,7 @@ public class DiscriminatedAssociationMapping implements MappingType, FetchOption
declaringModelPart,
tableName,
metaColumn.getText( dialect ),
metaColumn.getSqlType( creationProcess.getCreationContext().getMetadata() ),
metaColumn.getSqlType(),
metaColumn.getLength(),
metaColumn.getPrecision(),
metaColumn.getScale(),
@ -105,7 +105,7 @@ public class DiscriminatedAssociationMapping implements MappingType, FetchOption
declaringModelPart,
tableName,
keyColumn.getText( dialect ),
keyColumn.getSqlType( creationProcess.getCreationContext().getMetadata() ),
keyColumn.getSqlType(),
keyColumn.getLength(),
keyColumn.getPrecision(),
keyColumn.getScale(),

View File

@ -383,11 +383,11 @@ public class EmbeddableMappingTypeImpl extends AbstractEmbeddableMapping impleme
final boolean nullable;
if ( selectable instanceof Column ) {
final Column column = (Column) selectable;
columnDefinition = column.getSqlType( creationProcess.getCreationContext().getMetadata() );
columnDefinition = column.getSqlType();
length = column.getLength();
precision = column.getPrecision();
scale = column.getScale();
isLob = column.isSqlTypeLob();
isLob = column.isSqlTypeLob( creationProcess.getCreationContext().getMetadata() );
nullable = bootPropertyDescriptor.isOptional() && column.isNullable() ;
selectablePath = basicValue.createSelectablePath( column.getQuotedName( dialect ) );
}

View File

@ -184,13 +184,13 @@ public class SelectableMappingImpl extends SqlTypedMappingImpl implements Select
else {
Column column = (Column) selectable;
columnExpression = selectable.getText( dialect );
columnDefinition = column.getSqlType( creationContext.getMetadata() );
columnDefinition = column.getSqlType();
length = column.getLength();
precision = column.getPrecision();
scale = column.getScale();
isNullable = forceNotNullable ? false : column.isNullable();
isLob = column.isSqlTypeLob();
isLob = column.isSqlTypeLob( creationContext.getMetadata() );
selectableName = column.getQuotedName( dialect );
}
return new SelectableMappingImpl(

View File

@ -5202,7 +5202,7 @@ public abstract class AbstractEntityPersister
scale = null;
}
else {
columnDefinition = column.getSqlType( modelCreationProcess.getCreationContext().getMetadata() );
columnDefinition = column.getSqlType();
length = column.getLength();
precision = column.getPrecision();
scale = column.getScale();
@ -5379,7 +5379,7 @@ public abstract class AbstractEntityPersister
}
else {
Column column = bootEntityDescriptor.getIdentifier().getColumns().get( 0 );
columnDefinition = column.getSqlType( creationProcess.getCreationContext().getMetadata() );
columnDefinition = column.getSqlType();
length = column.getLength();
precision = column.getPrecision();
scale = column.getScale();
@ -5438,7 +5438,7 @@ public abstract class AbstractEntityPersister
bootModelRootEntityDescriptor.getVersion().getName(),
entityPersister.getTableName(),
column.getText( dialect ),
column.getSqlType( creationProcess.getCreationContext().getMetadata() ),
column.getSqlType(),
column.getLength(),
column.getPrecision(),
column.getScale(),
@ -5483,11 +5483,11 @@ public abstract class AbstractEntityPersister
false,
null,
"?",
column.getSqlType( creationProcess.getCreationContext().getMetadata() ),
column.getSqlType(),
column.getLength(),
column.getPrecision(),
column.getScale(),
column.isSqlTypeLob(),
column.isSqlTypeLob( creationProcess.getCreationContext().getMetadata() ),
column.isNullable(),
value.isColumnInsertable( 0 ),
value.isColumnUpdateable( 0 ),
@ -5515,11 +5515,11 @@ public abstract class AbstractEntityPersister
customReadExpr = null;
customWriteExpr = "?";
Column column = value.getColumns().get( 0 );
columnDefinition = column.getSqlType( creationProcess.getCreationContext().getMetadata() );
columnDefinition = column.getSqlType();
length = column.getLength();
precision = column.getPrecision();
scale = column.getScale();
isLob = column.isSqlTypeLob();
isLob = column.isSqlTypeLob( creationProcess.getCreationContext().getMetadata() );
nullable = column.isNullable();
}
else {
@ -5542,12 +5542,12 @@ public abstract class AbstractEntityPersister
);
customWriteExpr = selectable.getWriteExpr( (JdbcMapping) attrType, creationContext.getDialect() );
Column column = value.getColumns().get( 0 );
columnDefinition = column.getSqlType( creationContext.getMetadata() );
columnDefinition = column.getSqlType();
length = column.getLength();
precision = column.getPrecision();
scale = column.getScale();
nullable = column.isNullable();
isLob = column.isSqlTypeLob();
isLob = column.isSqlTypeLob( creationContext.getMetadata() );
}
else {
final String[] attrColumnFormulaTemplate = propertyColumnFormulaTemplates[ propertyIndex ];

View File

@ -1187,7 +1187,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
}
else {
final Column column = bootEntityDescriptor.getIdentifier().getColumns().get( 0 );
columnDefinition = column.getSqlType( creationProcess.getCreationContext().getMetadata() );
columnDefinition = column.getSqlType();
length = column.getLength();
precision = column.getPrecision();
scale = column.getScale();

View File

@ -6229,8 +6229,12 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
@Override
public void visitCastTarget(CastTarget castTarget) {
appendSql( getSqlTypeName( castTarget, sessionFactory ) );
}
public static String getSqlTypeName(CastTarget castTarget, SessionFactoryImplementor factory) {
if ( castTarget.getSqlType() != null ) {
appendSql( castTarget.getSqlType() );
return castTarget.getSqlType();
}
else {
final SqlExpressible expressionType = (SqlExpressible) castTarget.getExpressionType();
@ -6238,7 +6242,7 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
final BasicPluralType<?, ?> containerType = (BasicPluralType<?, ?>) expressionType;
final BasicPluralJavaType<?> javaTypeDescriptor = (BasicPluralJavaType<?>) containerType.getJavaTypeDescriptor();
final BasicType<?> elementType = containerType.getElementType();
final String elementTypeName = sessionFactory.getTypeConfiguration().getDdlTypeRegistry()
final String elementTypeName = factory.getTypeConfiguration().getDdlTypeRegistry()
.getDescriptor( elementType.getJdbcType().getDdlTypeCode() )
.getCastTypeName(
elementType,
@ -6246,17 +6250,16 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
castTarget.getPrecision(),
castTarget.getScale()
);
final String arrayTypeName = dialect.getArrayTypeName(
final String arrayTypeName = factory.getJdbcServices().getDialect().getArrayTypeName(
javaTypeDescriptor.getElementJavaType().getJavaTypeClass().getSimpleName(),
elementTypeName,
null
);
if ( arrayTypeName != null ) {
appendSql( arrayTypeName );
return;
return arrayTypeName;
}
}
final DdlTypeRegistry ddlTypeRegistry = getSessionFactory().getTypeConfiguration().getDdlTypeRegistry();
final DdlTypeRegistry ddlTypeRegistry = factory.getTypeConfiguration().getDdlTypeRegistry();
DdlType ddlType = ddlTypeRegistry
.getDescriptor( expressionType.getJdbcMapping().getJdbcType().getDdlTypeCode() );
if ( ddlType == null ) {
@ -6265,13 +6268,11 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
ddlType = ddlTypeRegistry.getDescriptor( SqlTypes.INTEGER );
}
appendSql(
ddlType.getCastTypeName(
expressionType,
castTarget.getLength(),
castTarget.getPrecision(),
castTarget.getScale()
)
return ddlType.getCastTypeName(
expressionType,
castTarget.getLength(),
castTarget.getPrecision(),
castTarget.getScale()
);
}
}