34
34
import java .sql .SQLFeatureNotSupportedException ;
35
35
36
36
/**
37
- *
38
37
* Type mapping JSON SQL data type for Oracle database.
39
38
* This implementation is used when the JDBC OSON extension is available.
40
39
*
@@ -67,15 +66,15 @@ public AggregateJdbcType resolveAggregateJdbcType(
67
66
@ Override
68
67
public <X > ValueBinder <X > getBinder (JavaType <X > javaType ) {
69
68
70
- if ( javaType .getJavaType () == String .class || javaType .getJavaType () == Object .class ) {
69
+ if ( javaType .getJavaType () == String .class || javaType .getJavaType () == Object .class ) {
71
70
return super .getBinder ( javaType );
72
71
}
73
72
74
73
return new BasicBinder <>( javaType , this ) {
75
74
76
75
private <T > byte [] toOson (T value , JavaType <T > javaType , WrapperOptions options ) throws Exception {
77
76
final ByteArrayOutputStream out = new ByteArrayOutputStream ();
78
- if (getEmbeddableMappingType () != null ) {
77
+ if ( getEmbeddableMappingType () != null ) {
79
78
// OracleJsonFactory is used and not OracleOsonFactory as Jackson is not involved here
80
79
try (OracleJsonGenerator generator = OSON_JSON_FACTORY .createJsonBinaryGenerator ( out )) {
81
80
JsonHelper .serialize (
@@ -146,7 +145,7 @@ protected void doBind(CallableStatement st, X value, String name, WrapperOptions
146
145
@ Override
147
146
public <X > ValueExtractor <X > getExtractor (JavaType <X > javaType ) {
148
147
149
- if ( javaType .getJavaType () == String .class || javaType .getJavaType () == Object .class ) {
148
+ if ( javaType .getJavaType () == String .class || javaType .getJavaType () == Object .class ) {
150
149
return super .getExtractor ( javaType );
151
150
}
152
151
@@ -170,16 +169,16 @@ private X fromOson(InputStream osonBytes, WrapperOptions options) throws Excepti
170
169
171
170
private boolean useUtf8 (WrapperOptions options ) {
172
171
return getEmbeddableMappingType () == null
173
- && !options .getJsonFormatMapper ().supportsSourceType (OracleOsonJacksonHelper .READER_CLASS );
172
+ && !options .getJsonFormatMapper ().supportsSourceType ( OracleOsonJacksonHelper .READER_CLASS );
174
173
}
175
174
176
- private X doExtraction (OracleJsonDatum datum , WrapperOptions options ) throws SQLException {
175
+ private X doExtraction (OracleJsonDatum datum , WrapperOptions options ) throws SQLException {
177
176
if ( datum == null ) {
178
177
return null ;
179
178
}
180
179
InputStream osonBytes = datum .getStream ();
181
180
try {
182
- return fromOson ( osonBytes , options );
181
+ return fromOson ( osonBytes , options );
183
182
}
184
183
catch (Exception e ) {
185
184
throw new SQLException ( e );
@@ -201,107 +200,111 @@ private byte[] getBytesFromResultSetByIndex(ResultSet rs, int index) throws SQLE
201
200
// This can be a BLOB or a CLOB. getBytes is not supported on CLOB
202
201
// and getString is not supported on BLOB. W have to try both
203
202
try {
204
- return rs .getBytes ( index );
205
- } catch (SQLFeatureNotSupportedException nse ) {
206
- return rs .getString ( index ).getBytes ();
203
+ return rs .getBytes ( index );
204
+ }
205
+ catch (SQLFeatureNotSupportedException nse ) {
206
+ return rs .getString ( index ).getBytes ();
207
207
}
208
208
}
209
209
210
210
private byte [] getBytesFromStatementByIndex (CallableStatement st , int index ) throws SQLException {
211
211
// This can be a BLOB or a CLOB. getBytes is not supported on CLOB
212
212
// and getString is not supported on BLOB. W have to try both
213
213
try {
214
- return st .getBytes ( index );
215
- } catch (SQLFeatureNotSupportedException nse ) {
214
+ return st .getBytes ( index );
215
+ }
216
+ catch (SQLFeatureNotSupportedException nse ) {
216
217
217
- return st .getString ( index ).getBytes ();
218
+ return st .getString ( index ).getBytes ();
218
219
}
219
220
}
221
+
220
222
private byte [] getBytesFromStatementByName (CallableStatement st , String columnName ) throws SQLException {
221
223
// This can be a BLOB or a CLOB. getBytes is not supported on CLOB
222
224
// and getString is not supported on BLOB. W have to try both
223
225
try {
224
- return st .getBytes ( columnName );
225
- } catch (SQLFeatureNotSupportedException nse ) {
226
- return st .getString ( columnName ).getBytes ();
226
+ return st .getBytes ( columnName );
227
+ }
228
+ catch (SQLFeatureNotSupportedException nse ) {
229
+ return st .getString ( columnName ).getBytes ();
227
230
}
228
231
}
229
232
230
233
@ Override
231
234
protected X doExtract (ResultSet rs , int paramIndex , WrapperOptions options ) throws SQLException {
232
- if ( useUtf8 ( options ) ) {
233
- return fromString (getBytesFromResultSetByIndex (rs , paramIndex ), options );
234
- }
235
- else {
236
- try {
235
+ if ( useUtf8 ( options ) ) {
236
+ return fromString ( getBytesFromResultSetByIndex ( rs , paramIndex ), options );
237
+ }
238
+ else {
239
+ try {
237
240
OracleJsonDatum ojd = rs .getObject ( paramIndex , OracleJsonDatum .class );
238
241
return doExtraction ( ojd , options );
239
- } catch (SQLException exc ) {
240
- if ( exc .getErrorCode () == DatabaseError .JDBC_ERROR_BASE + DatabaseError .EOJ_INVALID_COLUMN_TYPE ) {
241
- // This may happen if we are fetching data from an existing schema
242
- // that uses BLOB for JSON column In that case we assume bytes are
243
- // UTF-8 bytes (i.e not OSON) and we fall back to previous String-based implementation
244
- LOG .invalidJSONColumnType ( OracleType .BLOB .getName (), OracleType .JSON .getName () );
245
- return fromString (getBytesFromResultSetByIndex (rs , paramIndex ), options );
246
- } else {
247
- throw exc ;
248
- }
242
+ }
243
+ catch (SQLException exc ) {
244
+ if ( exc .getErrorCode () == DatabaseError .JDBC_ERROR_BASE + DatabaseError .EOJ_INVALID_COLUMN_TYPE ) {
245
+ // This may happen if we are fetching data from an existing schema
246
+ // that uses BLOB for JSON column. In that case we assume bytes are
247
+ // UTF-8 bytes (i.e not OSON) and we fall back to previous String-based implementation
248
+ LOG .invalidJSONColumnType ( OracleType .BLOB .getName (), OracleType .JSON .getName () );
249
+ return fromString ( getBytesFromResultSetByIndex ( rs , paramIndex ), options );
250
+ }
251
+ else {
252
+ throw exc ;
249
253
}
250
254
}
255
+ }
251
256
}
252
257
253
258
@ Override
254
259
protected X doExtract (CallableStatement statement , int index , WrapperOptions options ) throws SQLException {
255
- if ( useUtf8 ( options ) ) {
256
- return fromString (getBytesFromStatementByIndex (statement , index ), options );
257
- }
258
- else {
259
- try {
260
+ if ( useUtf8 ( options ) ) {
261
+ return fromString ( getBytesFromStatementByIndex ( statement , index ), options );
262
+ }
263
+ else {
264
+ try {
260
265
OracleJsonDatum ojd = statement .getObject ( index , OracleJsonDatum .class );
261
266
return doExtraction ( ojd , options );
262
- } catch (SQLException exc ) {
263
- if ( exc .getErrorCode () == DatabaseError .JDBC_ERROR_BASE + DatabaseError .EOJ_INVALID_COLUMN_TYPE ) {
264
- // This may happen if we are fetching data from an existing schema
265
- // that uses BLOB for JSON column In that case we assume bytes are
266
- // UTF-8 bytes (i.e not OSON) and we fall back to previous String-based implementation
267
- LOG .invalidJSONColumnType ( OracleType .CLOB .getName (), OracleType .JSON .getName () );
268
- return fromString (getBytesFromStatementByIndex (statement , index ), options );
269
- } else {
270
- throw exc ;
271
- }
267
+ }
268
+ catch (SQLException exc ) {
269
+ if ( exc .getErrorCode () == DatabaseError .JDBC_ERROR_BASE + DatabaseError .EOJ_INVALID_COLUMN_TYPE ) {
270
+ // This may happen if we are fetching data from an existing schema
271
+ // that uses BLOB for JSON column In that case we assume bytes are
272
+ // UTF-8 bytes (i.e not OSON) and we fall back to previous String-based implementation
273
+ LOG .invalidJSONColumnType ( OracleType .CLOB .getName (), OracleType .JSON .getName () );
274
+ return fromString ( getBytesFromStatementByIndex ( statement , index ), options );
275
+ }
276
+ else {
277
+ throw exc ;
272
278
}
273
279
}
280
+ }
274
281
}
275
282
276
283
@ Override
277
284
protected X doExtract (CallableStatement statement , String name , WrapperOptions options )
278
285
throws SQLException {
279
-
280
- if ( useUtf8 ( options ) ) {
281
- return fromString (getBytesFromStatementByName (statement , name ), options );
282
- }
283
- else {
284
- try {
286
+ if ( useUtf8 ( options ) ) {
287
+ return fromString ( getBytesFromStatementByName ( statement , name ), options );
288
+ }
289
+ else {
290
+ try {
285
291
OracleJsonDatum ojd = statement .getObject ( name , OracleJsonDatum .class );
286
292
return doExtraction ( ojd , options );
287
- } catch (SQLException exc ) {
288
- if ( exc .getErrorCode () == DatabaseError .JDBC_ERROR_BASE + DatabaseError .EOJ_INVALID_COLUMN_TYPE ) {
289
- // This may happen if we are fetching data from an existing schema
290
- // that uses BLOB for JSON column In that case we assume bytes are
291
- // UTF-8 bytes (i.e not OSON) and we fall back to previous String-based implementation
292
- LOG .invalidJSONColumnType ( OracleType .CLOB .getName (), OracleType .JSON .getName () );
293
- return fromString (getBytesFromStatementByName (statement , name ), options );
294
- } else {
295
- throw exc ;
296
- }
293
+ }
294
+ catch (SQLException exc ) {
295
+ if ( exc .getErrorCode () == DatabaseError .JDBC_ERROR_BASE + DatabaseError .EOJ_INVALID_COLUMN_TYPE ) {
296
+ // This may happen if we are fetching data from an existing schema
297
+ // that uses BLOB for JSON column In that case we assume bytes are
298
+ // UTF-8 bytes (i.e not OSON) and we fall back to previous String-based implementation
299
+ LOG .invalidJSONColumnType ( OracleType .CLOB .getName (), OracleType .JSON .getName () );
300
+ return fromString ( getBytesFromStatementByName ( statement , name ), options );
301
+ }
302
+ else {
303
+ throw exc ;
297
304
}
298
305
}
299
-
300
-
306
+ }
301
307
}
302
-
303
308
};
304
309
}
305
-
306
-
307
310
}
0 commit comments