31
31
import java .sql .PreparedStatement ;
32
32
import java .sql .ResultSet ;
33
33
import java .sql .SQLException ;
34
+ import java .sql .SQLFeatureNotSupportedException ;
34
35
35
36
/**
36
37
*
@@ -169,7 +170,7 @@ private X fromOson(InputStream osonBytes, WrapperOptions options) throws Excepti
169
170
170
171
private boolean useUtf8 (WrapperOptions options ) {
171
172
return getEmbeddableMappingType () == null
172
- && !options .getJsonFormatMapper ().supportsTargetType ( OracleOsonJacksonHelper .READER_CLASS );
173
+ && !options .getJsonFormatMapper ().supportsSourceType ( OracleOsonJacksonHelper .READER_CLASS );
173
174
}
174
175
175
176
private X doExtraction (OracleJsonDatum datum , WrapperOptions options ) throws SQLException {
@@ -196,74 +197,106 @@ private X fromString(byte[] json, WrapperOptions options) throws SQLException {
196
197
);
197
198
}
198
199
200
+ private byte [] getBytesFromResultSetByIndex (ResultSet rs , int index ) throws SQLException {
201
+ // This can be a BLOB or a CLOB. getBytes is not supported on CLOB
202
+ // and getString is not supported on BLOB. W have to try both
203
+ try {
204
+ return rs .getBytes ( index );
205
+ } catch (SQLFeatureNotSupportedException nse ) {
206
+ return rs .getString ( index ).getBytes ();
207
+ }
208
+ }
209
+
210
+ private byte [] getBytesFromStatementByIndex (CallableStatement st , int index ) throws SQLException {
211
+ // This can be a BLOB or a CLOB. getBytes is not supported on CLOB
212
+ // and getString is not supported on BLOB. W have to try both
213
+ try {
214
+ return st .getBytes ( index );
215
+ } catch (SQLFeatureNotSupportedException nse ) {
216
+
217
+ return st .getString ( index ).getBytes ();
218
+ }
219
+ }
220
+ private byte [] getBytesFromStatementByName (CallableStatement st , String columnName ) throws SQLException {
221
+ // This can be a BLOB or a CLOB. getBytes is not supported on CLOB
222
+ // and getString is not supported on BLOB. W have to try both
223
+ try {
224
+ return st .getBytes ( columnName );
225
+ } catch (SQLFeatureNotSupportedException nse ) {
226
+ return st .getString ( columnName ).getBytes ();
227
+ }
228
+ }
229
+
199
230
@ Override
200
231
protected X doExtract (ResultSet rs , int paramIndex , WrapperOptions options ) throws SQLException {
201
- try {
202
232
if ( useUtf8 ( options ) ) {
203
- return fromString ( rs . getBytes ( paramIndex ), options );
233
+ return fromString (getBytesFromResultSetByIndex ( rs , paramIndex ), options );
204
234
}
205
235
else {
236
+ try {
206
237
OracleJsonDatum ojd = rs .getObject ( paramIndex , OracleJsonDatum .class );
207
238
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
+ }
249
+ }
208
250
}
209
- } catch (SQLException exc ) {
210
- if ( exc .getErrorCode () == DatabaseError .JDBC_ERROR_BASE + DatabaseError .EOJ_INVALID_COLUMN_TYPE ) {
211
- // This may happen if we are fetching data from an existing schema
212
- // that uses BLOB for JSON column In that case we assume bytes are
213
- // UTF-8 bytes (i.e not OSON) and we fall back to previous String-based implementation
214
- LOG .invalidJSONColumnType ( OracleType .CLOB .getName (), OracleType .JSON .getName () );
215
- return fromString ( rs .getBytes ( paramIndex ), options );
216
- } else {
217
- throw exc ;
218
- }
219
- }
220
251
}
221
252
222
253
@ Override
223
254
protected X doExtract (CallableStatement statement , int index , WrapperOptions options ) throws SQLException {
224
- try {
225
255
if ( useUtf8 ( options ) ) {
226
- return fromString ( statement . getBytes ( index ), options );
256
+ return fromString (getBytesFromStatementByIndex ( statement , index ), options );
227
257
}
228
258
else {
259
+ try {
229
260
OracleJsonDatum ojd = statement .getObject ( index , OracleJsonDatum .class );
230
261
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
+ }
272
+ }
231
273
}
232
- } catch (SQLException exc ) {
233
- if ( exc .getErrorCode () == DatabaseError .JDBC_ERROR_BASE + DatabaseError .EOJ_INVALID_COLUMN_TYPE ) {
234
- // This may happen if we are fetching data from an existing schema
235
- // that uses BLOB for JSON column In that case we assume bytes are
236
- // UTF-8 bytes (i.e not OSON) and we fall back to previous String-based implementation
237
- LOG .invalidJSONColumnType ( OracleType .CLOB .getName (), OracleType .JSON .getName () );
238
- return fromString ( statement .getBytes ( index ), options );
239
- } else {
240
- throw exc ;
241
- }
242
- }
243
274
}
244
275
245
276
@ Override
246
277
protected X doExtract (CallableStatement statement , String name , WrapperOptions options )
247
278
throws SQLException {
248
- try {
279
+
249
280
if ( useUtf8 ( options ) ) {
250
- return fromString ( statement . getBytes ( name ), options );
281
+ return fromString (getBytesFromStatementByName ( statement , name ), options );
251
282
}
252
283
else {
284
+ try {
253
285
OracleJsonDatum ojd = statement .getObject ( name , OracleJsonDatum .class );
254
286
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
+ }
297
+ }
255
298
}
256
- } catch (SQLException exc ) {
257
- if ( exc .getErrorCode () == DatabaseError .JDBC_ERROR_BASE + DatabaseError .EOJ_INVALID_COLUMN_TYPE ) {
258
- // This may happen if we are fetching data from an existing schema
259
- // that uses BLOB for JSON column In that case we assume bytes are
260
- // UTF-8 bytes (i.e not OSON) and we fall back to previous String-based implementation
261
- LOG .invalidJSONColumnType ( OracleType .CLOB .getName (), OracleType .JSON .getName () );
262
- return fromString ( statement .getBytes ( name ), options );
263
- } else {
264
- throw exc ;
265
- }
266
- }
299
+
267
300
268
301
}
269
302
0 commit comments