Skip to content

Commit 775fdeb

Browse files
committed
HHH-17404 rename oson flag in oracle dialect
1 parent bcf3488 commit 775fdeb

File tree

4 files changed

+80
-24
lines changed

4 files changed

+80
-24
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,16 +409,25 @@ public static <X> X deserialize(
409409
return (X) values;
410410
}
411411

412+
412413
// This is also used by Hibernate Reactive
413414
public static <X> X arrayFromString(
414415
JavaType<X> javaType,
415416
JdbcType elementJdbcType,
416417
String string,
417418
WrapperOptions options) throws SQLException {
418-
419419
if ( string == null ) {
420420
return null;
421421
}
422+
return deserializeArray( javaType, elementJdbcType, new StringJsonDocumentReader( string ), options );
423+
}
424+
425+
public static <X> X deserializeArray(
426+
JavaType<X> javaType,
427+
JdbcType elementJdbcType,
428+
JsonDocumentReader reader,
429+
WrapperOptions options) throws SQLException {
430+
422431

423432
final CustomArrayList arrayList = new CustomArrayList();
424433
final JavaType<?> elementJavaType = ((BasicPluralJavaType<?>) javaType).getElementJavaType();
@@ -430,7 +439,7 @@ public static <X> X arrayFromString(
430439
else {
431440
jdbcJavaType = options.getTypeConfiguration().getJavaTypeRegistry().resolveDescriptor( preferredJavaTypeClass );
432441
}
433-
JsonDocumentReader reader = new StringJsonDocumentReader(string);
442+
434443
JsonValueJDBCTypeAdapter adapter = JsonValueJDBCTypeAdapterFactory.getAdapter(reader,false);
435444

436445
assert reader.hasNext():"Invalid array string";

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public class OracleDialect extends Dialect {
188188

189189
private static final DatabaseVersion MINIMUM_VERSION = DatabaseVersion.make( 19 );
190190

191-
private boolean OracleOsonExtensionUsed = false;
191+
private boolean osonExtensionEnabled = false;
192192

193193
private final OracleUserDefinedTypeExporter userDefinedTypeExporter = new OracleUserDefinedTypeExporter( this );
194194
private final UniqueDelegate uniqueDelegate = new CreateTableUniqueDelegate(this);
@@ -1006,7 +1006,7 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry
10061006
DIALECT_MESSAGE_LOGGER.DIALECT_LOGGER.log( Logger.Level.DEBUG,
10071007
"Oracle OSON Jackson extension used" );
10081008
// as we speak, this is not supported by OSON extension
1009-
OracleOsonExtensionUsed = true;
1009+
osonExtensionEnabled = true;
10101010
}
10111011
else {
10121012
if (DIALECT_MESSAGE_LOGGER.DIALECT_LOGGER.isDebugEnabled()) {
@@ -1065,7 +1065,7 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry
10651065

10661066
@Override
10671067
public AggregateSupport getAggregateSupport() {
1068-
return OracleAggregateSupport.valueOf( this ,!OracleOsonExtensionUsed);
1068+
return OracleAggregateSupport.valueOf( this ,!osonExtensionEnabled );
10691069
}
10701070

10711071
@Override

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

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
import com.fasterxml.jackson.core.JsonParser;
88
import oracle.jdbc.OracleType;
9+
import oracle.jdbc.driver.DatabaseError;
910
import oracle.jdbc.provider.oson.OsonFactory;
1011
import oracle.sql.json.OracleJsonDatum;
1112
import oracle.sql.json.OracleJsonFactory;
1213
import oracle.sql.json.OracleJsonGenerator;
1314

15+
import org.hibernate.internal.CoreLogging;
16+
import org.hibernate.internal.CoreMessageLogger;
1417
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
1518
import org.hibernate.type.descriptor.ValueBinder;
1619
import org.hibernate.type.descriptor.ValueExtractor;
@@ -27,6 +30,7 @@
2730

2831
import java.io.ByteArrayOutputStream;
2932
import java.io.InputStream;
33+
import java.nio.charset.StandardCharsets;
3034
import java.sql.CallableStatement;
3135
import java.sql.PreparedStatement;
3236
import java.sql.ResultSet;
@@ -43,6 +47,7 @@
4347
*/
4448
public class OracleOsonJacksonArrayJdbcType extends OracleJsonArrayJdbcType {
4549

50+
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( OracleOsonJacksonArrayJdbcType.class );
4651

4752
private static final OsonFactory osonFactory = new OsonFactory();
4853

@@ -121,6 +126,7 @@ public <X> ValueExtractor<X> getExtractor(JavaType<X> javaType) {
121126
private X fromOson(InputStream osonBytes, WrapperOptions options) throws Exception {
122127
FormatMapper mapper = options.getJsonFormatMapper();
123128
JsonParser osonParser = osonFactory.createParser( osonBytes );
129+
124130
return mapper.readFromSource( getJavaType(), osonParser, options);
125131
}
126132

@@ -139,25 +145,66 @@ private X doExtraction(OracleJsonDatum datum, WrapperOptions options) throws SQ
139145

140146
@Override
141147
protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException {
142-
143-
OracleJsonDatum ojd = rs.getObject( paramIndex, OracleJsonDatum.class );
144-
return doExtraction( ojd, options);
148+
try {
149+
OracleJsonDatum ojd = rs.getObject( paramIndex, OracleJsonDatum.class );
150+
return doExtraction( ojd, options);
151+
} catch (SQLException exc) {
152+
if ( exc.getErrorCode() == DatabaseError.JDBC_ERROR_BASE + DatabaseError.EOJ_INVALID_COLUMN_TYPE) {
153+
// This may happen if we are fetching data from an existing schema
154+
// that uses BLOB for JSON column In that case we assume bytes are
155+
// UTF-8 bytes (i.e not OSON) and we fall back to previous String-based implementation
156+
LOG.invalidJSONColumnType( OracleType.CLOB.getName(), OracleType.JSON.getName() );
157+
return OracleOsonJacksonArrayJdbcType.this.fromString(
158+
new String( rs.getBytes( paramIndex ), StandardCharsets.UTF_8 ),
159+
getJavaType(),
160+
options);
161+
} else {
162+
throw exc;
163+
}
164+
}
145165
}
146166

147167
@Override
148168
protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
149-
150-
151-
OracleJsonDatum ojd = statement.getObject( index, OracleJsonDatum.class );
152-
return doExtraction( ojd, options);
169+
try {
170+
OracleJsonDatum ojd = statement.getObject( index, OracleJsonDatum.class );
171+
return doExtraction( ojd, options);
172+
} catch (SQLException exc) {
173+
if ( exc.getErrorCode() == DatabaseError.JDBC_ERROR_BASE + DatabaseError.EOJ_INVALID_COLUMN_TYPE) {
174+
// This may happen if we are fetching data from an existing schema
175+
// that uses BLOB for JSON column In that case we assume bytes are
176+
// UTF-8 bytes (i.e not OSON) and we fall back to previous String-based implementation
177+
LOG.invalidJSONColumnType( OracleType.CLOB.getName(), OracleType.JSON.getName() );
178+
return OracleOsonJacksonArrayJdbcType.this.fromString(
179+
new String( statement.getBytes( index ), StandardCharsets.UTF_8 ),
180+
getJavaType(),
181+
options);
182+
} else {
183+
throw exc;
184+
}
185+
}
153186
}
154187

155188
@Override
156189
protected X doExtract(CallableStatement statement, String name, WrapperOptions options)
157190
throws SQLException {
158-
159-
OracleJsonDatum ojd = statement.getObject( name, OracleJsonDatum.class );
160-
return doExtraction( ojd, options);
191+
try {
192+
OracleJsonDatum ojd = statement.getObject( name, OracleJsonDatum.class );
193+
return doExtraction( ojd, options);
194+
} catch (SQLException exc) {
195+
if ( exc.getErrorCode() == DatabaseError.JDBC_ERROR_BASE + DatabaseError.EOJ_INVALID_COLUMN_TYPE) {
196+
// This may happen if we are fetching data from an existing schema
197+
// that uses BLOB for JSON column In that case we assume bytes are
198+
// UTF-8 bytes (i.e not OSON) and we fall back to previous String-based implementation
199+
LOG.invalidJSONColumnType( OracleType.CLOB.getName(), OracleType.JSON.getName() );
200+
return OracleOsonJacksonArrayJdbcType.this.fromString(
201+
new String( statement.getBytes( name ), StandardCharsets.UTF_8 ),
202+
getJavaType(),
203+
options);
204+
} else {
205+
throw exc;
206+
}
207+
}
161208
}
162209
};
163210
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) thro
183183
return doExtraction(ojd,options);
184184
} catch (SQLException exc) {
185185
if ( exc.getErrorCode() == DatabaseError.JDBC_ERROR_BASE + DatabaseError.EOJ_INVALID_COLUMN_TYPE) {
186-
// this may happen if we are fetching data from an existing schema
187-
// that use CBLOB for JSON column In that case we assume byte are
188-
// UTF-8 bytes (i.e not OSON)
186+
// This may happen if we are fetching data from an existing schema
187+
// that uses BLOB for JSON column In that case we assume bytes are
188+
// UTF-8 bytes (i.e not OSON) and we fall back to previous String-based implementation
189189
LOG.invalidJSONColumnType( OracleType.CLOB.getName(), OracleType.JSON.getName() );
190190
return OracleOsonJacksonJdbcType.this.fromString(
191191
new String( rs.getBytes( paramIndex ), StandardCharsets.UTF_8 ),
@@ -204,9 +204,9 @@ protected X doExtract(CallableStatement statement, int index, WrapperOptions opt
204204
return doExtraction(ojd,options);
205205
} catch (SQLException exc) {
206206
if ( exc.getErrorCode() == DatabaseError.JDBC_ERROR_BASE + DatabaseError.EOJ_INVALID_COLUMN_TYPE) {
207-
// this may happen if we are fetching data from an existing schema
208-
// that use CBLOB for JSON column. In that case we assume byte are
209-
// UTF-8 bytes (i.e not OSON)
207+
// This may happen if we are fetching data from an existing schema
208+
// that uses BLOB for JSON column In that case we assume bytes are
209+
// UTF-8 bytes (i.e not OSON) and we fall back to previous String-based implementation
210210
LOG.invalidJSONColumnType( OracleType.CLOB.getName(), OracleType.JSON.getName() );
211211
return OracleOsonJacksonJdbcType.this.fromString(
212212
new String( statement.getBytes( index ), StandardCharsets.UTF_8 ),
@@ -226,9 +226,9 @@ protected X doExtract(CallableStatement statement, String name, WrapperOptions o
226226
return doExtraction(ojd,options);
227227
} catch (SQLException exc) {
228228
if ( exc.getErrorCode() == DatabaseError.JDBC_ERROR_BASE + DatabaseError.EOJ_INVALID_COLUMN_TYPE) {
229-
// this may happen if we are fetching data from an existing schema
230-
// that use CBLOB for JSON column In that case we assume byte are
231-
// // UTF-8 bytes (i.e not OSON)
229+
// This may happen if we are fetching data from an existing schema
230+
// that uses BLOB for JSON column In that case we assume bytes are
231+
// UTF-8 bytes (i.e not OSON) and we fall back to previous String-based implementation
232232
LOG.invalidJSONColumnType( OracleType.CLOB.getName(), OracleType.JSON.getName() );
233233
return OracleOsonJacksonJdbcType.this.fromString(
234234
new String( statement.getBytes( name ), StandardCharsets.UTF_8 ),

0 commit comments

Comments
 (0)