Skip to content

Commit 1b789ff

Browse files
committed
HHH-17276 Be more forgiving when determining lobness
1 parent e507f59 commit 1b789ff

File tree

14 files changed

+130
-47
lines changed

14 files changed

+130
-47
lines changed

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/MariaDBLegacySqlAstTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private boolean supportsWindowFunctions() {
242242

243243
@Override
244244
public void visitCastTarget(CastTarget castTarget) {
245-
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, dialect );
245+
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, getSessionFactory() );
246246
if ( sqlType != null ) {
247247
appendSql( sqlType );
248248
}

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/MySQLLegacySqlAstTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public MySQLLegacyDialect getDialect() {
250250

251251
@Override
252252
public void visitCastTarget(CastTarget castTarget) {
253-
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, getDialect() );
253+
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, getSessionFactory() );
254254
if ( sqlType != null ) {
255255
appendSql( sqlType );
256256
}

hibernate-core/src/main/java/org/hibernate/dialect/MariaDBSqlAstTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ private boolean supportsWindowFunctions() {
236236

237237
@Override
238238
public void visitCastTarget(CastTarget castTarget) {
239-
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, dialect );
239+
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, getSessionFactory() );
240240
if ( sqlType != null ) {
241241
appendSql( sqlType );
242242
}

hibernate-core/src/main/java/org/hibernate/dialect/MySQLSqlAstTranslator.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,20 @@ public MySQLSqlAstTranslator(SessionFactoryImplementor sessionFactory, Statement
3737
super( sessionFactory, statement );
3838
}
3939

40+
/**
41+
* @deprecated Use {@link #getSqlType(CastTarget, SessionFactoryImplementor)} instead
42+
*/
43+
@Deprecated(forRemoval = true)
4044
public static String getSqlType(CastTarget castTarget, Dialect dialect) {
41-
final String sqlType = castTarget.getSqlType();
45+
return getSqlType( castTarget, castTarget.getSqlType(), dialect );
46+
}
47+
48+
public static String getSqlType(CastTarget castTarget, SessionFactoryImplementor factory) {
49+
final String sqlType = getSqlTypeName( castTarget, factory );
50+
return getSqlType( castTarget, sqlType, factory.getJdbcServices().getDialect() );
51+
}
4252

53+
private static String getSqlType(CastTarget castTarget, String sqlType, Dialect dialect) {
4354
if ( sqlType != null ) {
4455
int parenthesesIndex = sqlType.indexOf( '(' );
4556
final String baseName = parenthesesIndex == -1 ? sqlType : sqlType.substring( 0, parenthesesIndex );
@@ -63,10 +74,14 @@ public static String getSqlType(CastTarget castTarget, Dialect dialect) {
6374
case "varchar":
6475
case "nchar":
6576
case "nvarchar":
66-
return "char";
77+
return castTarget.getLength() == null
78+
? "char"
79+
: ( "char(" + castTarget.getLength() + ")" );
6780
case "binary":
6881
case "varbinary":
69-
return "binary";
82+
return castTarget.getLength() == null
83+
? "binary"
84+
: ( "binary(" + castTarget.getLength() + ")" );
7085
}
7186
}
7287
return sqlType;
@@ -286,7 +301,7 @@ public MySQLDialect getDialect() {
286301

287302
@Override
288303
public void visitCastTarget(CastTarget castTarget) {
289-
String sqlType = getSqlType( castTarget, getDialect() );
304+
String sqlType = getSqlType( castTarget, getSessionFactory() );
290305
if ( sqlType != null ) {
291306
appendSql( sqlType );
292307
}

hibernate-core/src/main/java/org/hibernate/dialect/TiDBSqlAstTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public TiDBDialect getDialect() {
193193

194194
@Override
195195
public void visitCastTarget(CastTarget castTarget) {
196-
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, dialect );
196+
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, getSessionFactory() );
197197
if ( sqlType != null ) {
198198
appendSql( sqlType );
199199
}

hibernate-core/src/main/java/org/hibernate/dialect/aggregate/OracleAggregateSupport.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.hibernate.tool.schema.extract.spi.ColumnTypeInformation;
2222
import org.hibernate.type.SqlTypes;
2323
import org.hibernate.type.descriptor.jdbc.AggregateJdbcType;
24+
import org.hibernate.type.descriptor.sql.DdlType;
2425
import org.hibernate.type.spi.TypeConfiguration;
2526

2627
import static org.hibernate.type.SqlTypes.BIGINT;
@@ -232,7 +233,7 @@ public WriteExpressionRenderer aggregateCustomWriteExpressionRenderer(
232233
final int aggregateSqlTypeCode = aggregateColumn.getJdbcMapping().getJdbcType().getDefaultSqlTypeCode();
233234
switch ( aggregateSqlTypeCode ) {
234235
case JSON:
235-
return jsonAggregateColumnWriter( aggregateColumn, columnsToUpdate );
236+
return jsonAggregateColumnWriter( aggregateColumn, columnsToUpdate, typeConfiguration );
236237
}
237238
throw new IllegalArgumentException( "Unsupported aggregate SQL type: " + aggregateSqlTypeCode );
238239
}
@@ -270,8 +271,9 @@ enum JsonSupport {
270271

271272
private WriteExpressionRenderer jsonAggregateColumnWriter(
272273
SelectableMapping aggregateColumn,
273-
SelectableMapping[] columns) {
274-
return new RootJsonWriteExpression( aggregateColumn, columns, this );
274+
SelectableMapping[] columns,
275+
TypeConfiguration typeConfiguration) {
276+
return new RootJsonWriteExpression( aggregateColumn, columns, this, typeConfiguration );
275277
}
276278

277279
interface JsonWriteExpression {
@@ -294,7 +296,10 @@ public AggregateJsonWriteExpression(
294296
this.ddlTypeName = aggregateSupport.determineJsonTypeName( selectableMapping );
295297
}
296298

297-
protected void initializeSubExpressions(SelectableMapping[] columns, OracleAggregateSupport aggregateSupport) {
299+
protected void initializeSubExpressions(
300+
SelectableMapping[] columns,
301+
OracleAggregateSupport aggregateSupport,
302+
TypeConfiguration typeConfiguration) {
298303
for ( SelectableMapping column : columns ) {
299304
final SelectablePath selectablePath = column.getSelectablePath();
300305
final SelectablePath[] parts = selectablePath.getParts();
@@ -319,13 +324,33 @@ protected void initializeSubExpressions(SelectableMapping[] columns, OracleAggre
319324
aggregateSupport.jsonCustomWriteExpression(
320325
customWriteExpression,
321326
sqlTypeCode,
322-
column.getColumnDefinition()
327+
determineTypeName( column, typeConfiguration )
323328
)
324329
)
325330
);
326331
}
327332
}
328333

334+
private static String determineTypeName(SelectableMapping column, TypeConfiguration typeConfiguration) {
335+
final String typeName;
336+
if ( column.getColumnDefinition() == null ) {
337+
final DdlType ddlType = typeConfiguration.getDdlTypeRegistry().getDescriptor(
338+
column.getJdbcMapping().getJdbcType().getDefaultSqlTypeCode()
339+
);
340+
return ddlType.getCastTypeName(
341+
column.getJdbcMapping().getJdbcType(),
342+
column.getJdbcMapping().getJavaTypeDescriptor(),
343+
column.getLength(),
344+
column.getPrecision(),
345+
column.getScale()
346+
);
347+
}
348+
else{
349+
typeName = column.getColumnDefinition();
350+
}
351+
return typeName;
352+
}
353+
329354
@Override
330355
public void append(
331356
SqlAppender sb,
@@ -364,11 +389,12 @@ private static class RootJsonWriteExpression extends AggregateJsonWriteExpressio
364389
RootJsonWriteExpression(
365390
SelectableMapping aggregateColumn,
366391
SelectableMapping[] columns,
367-
OracleAggregateSupport aggregateSupport) {
392+
OracleAggregateSupport aggregateSupport,
393+
TypeConfiguration typeConfiguration) {
368394
super( aggregateColumn, aggregateSupport );
369395
this.nullable = aggregateColumn.isNullable();
370396
this.path = aggregateColumn.getSelectionExpression();
371-
initializeSubExpressions( columns, aggregateSupport );
397+
initializeSubExpressions( columns, aggregateSupport, typeConfiguration );
372398
}
373399

374400
@Override

hibernate-core/src/main/java/org/hibernate/mapping/Column.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.hibernate.type.Type;
3333
import org.hibernate.type.descriptor.jdbc.ArrayJdbcType;
3434
import org.hibernate.type.descriptor.JdbcTypeNameMapper;
35+
import org.hibernate.type.descriptor.jdbc.JdbcType;
3536
import org.hibernate.type.descriptor.sql.DdlType;
3637
import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry;
3738
import org.hibernate.type.spi.TypeConfiguration;
@@ -60,7 +61,7 @@ public class Column implements Selectable, Serializable, Cloneable, ColumnTypeIn
6061
private boolean unique;
6162
private String sqlTypeName;
6263
private Integer sqlTypeCode;
63-
private boolean sqlTypeLob;
64+
private Boolean sqlTypeLob;
6465
private boolean quoted;
6566
int uniqueInteger;
6667
private String comment;
@@ -494,6 +495,46 @@ public void setSqlType(String typeName) {
494495
}
495496

496497
public boolean isSqlTypeLob() {
498+
return sqlTypeLob != null && sqlTypeLob;
499+
}
500+
501+
public boolean isSqlTypeLob(Metadata mapping) {
502+
final Database database = mapping.getDatabase();
503+
final DdlTypeRegistry ddlTypeRegistry = database.getTypeConfiguration().getDdlTypeRegistry();
504+
final Dialect dialect = database.getDialect();
505+
if ( sqlTypeLob == null ) {
506+
final Type type = getValue().getType();
507+
try {
508+
if ( isArray( type ) ) {
509+
sqlTypeLob = false;
510+
}
511+
else {
512+
final int typeCode = getSqlTypeCode( mapping );
513+
final DdlType descriptor = ddlTypeRegistry.getDescriptor( typeCode );
514+
if ( descriptor == null ) {
515+
sqlTypeLob = JdbcType.isLob( typeCode );
516+
}
517+
else {
518+
final Size size = getColumnSize( dialect, mapping );
519+
sqlTypeLob = descriptor.isLob( size );
520+
}
521+
}
522+
}
523+
catch ( MappingException cause ) {
524+
throw cause;
525+
}
526+
catch ( Exception cause ) {
527+
throw new MappingException(
528+
String.format(
529+
Locale.ROOT,
530+
"Unable to determine SQL type name for column '%s' of table '%s'",
531+
getName(),
532+
getValue().getTable().getName()
533+
),
534+
cause
535+
);
536+
}
537+
}
497538
return sqlTypeLob;
498539
}
499540

hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/AbstractEmbeddableMapping.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,12 @@ protected boolean finishInitialization(
287287
final boolean nullable;
288288
if ( selectable instanceof Column ) {
289289
final Column column = (Column) selectable;
290-
columnDefinition = column.getSqlType( creationProcess.getCreationContext().getMetadata() );
290+
columnDefinition = column.getSqlType();
291291
length = column.getLength();
292292
precision = column.getPrecision();
293293
scale = column.getScale();
294294
nullable = column.isNullable();
295-
isLob = column.isSqlTypeLob();
295+
isLob = column.isSqlTypeLob( creationProcess.getCreationContext().getMetadata() );
296296
selectablePath = basicValue.createSelectablePath( column.getQuotedName( dialect ) );
297297
}
298298
else {

hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/DiscriminatedAssociationMapping.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static DiscriminatedAssociationMapping from(
8787
declaringModelPart,
8888
tableName,
8989
metaColumn.getText( dialect ),
90-
metaColumn.getSqlType( creationProcess.getCreationContext().getMetadata() ),
90+
metaColumn.getSqlType(),
9191
metaColumn.getLength(),
9292
metaColumn.getPrecision(),
9393
metaColumn.getScale(),
@@ -106,7 +106,7 @@ public static DiscriminatedAssociationMapping from(
106106
declaringModelPart,
107107
tableName,
108108
keyColumn.getText( dialect ),
109-
keyColumn.getSqlType( creationProcess.getCreationContext().getMetadata() ),
109+
keyColumn.getSqlType(),
110110
keyColumn.getLength(),
111111
keyColumn.getPrecision(),
112112
keyColumn.getScale(),

hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddableMappingTypeImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,11 @@ private boolean finishInitialization(
383383
final boolean nullable;
384384
if ( selectable instanceof Column ) {
385385
final Column column = (Column) selectable;
386-
columnDefinition = column.getSqlType( creationProcess.getCreationContext().getMetadata() );
386+
columnDefinition = column.getSqlType();
387387
length = column.getLength();
388388
precision = column.getPrecision();
389389
scale = column.getScale();
390-
isLob = column.isSqlTypeLob();
390+
isLob = column.isSqlTypeLob( creationProcess.getCreationContext().getMetadata() );
391391
nullable = bootPropertyDescriptor.isOptional() && column.isNullable() ;
392392
selectablePath = basicValue.createSelectablePath( column.getQuotedName( dialect ) );
393393
}

0 commit comments

Comments
 (0)