3131import java .sql .PreparedStatement ;
3232import java .sql .ResultSet ;
3333import java .sql .SQLException ;
34+ import java .sql .SQLFeatureNotSupportedException ;
3435
3536/**
3637 *
@@ -169,7 +170,7 @@ private X fromOson(InputStream osonBytes, WrapperOptions options) throws Excepti
169170
170171 private boolean useUtf8 (WrapperOptions options ) {
171172 return getEmbeddableMappingType () == null
172- && !options .getJsonFormatMapper ().supportsTargetType ( OracleOsonJacksonHelper .READER_CLASS );
173+ && !options .getJsonFormatMapper ().supportsSourceType ( OracleOsonJacksonHelper .READER_CLASS );
173174 }
174175
175176 private X doExtraction (OracleJsonDatum datum , WrapperOptions options ) throws SQLException {
@@ -196,74 +197,106 @@ private X fromString(byte[] json, WrapperOptions options) throws SQLException {
196197 );
197198 }
198199
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+
199230 @ Override
200231 protected X doExtract (ResultSet rs , int paramIndex , WrapperOptions options ) throws SQLException {
201- try {
202232 if ( useUtf8 ( options ) ) {
203- return fromString ( rs . getBytes ( paramIndex ), options );
233+ return fromString (getBytesFromResultSetByIndex ( rs , paramIndex ), options );
204234 }
205235 else {
236+ try {
206237 OracleJsonDatum ojd = rs .getObject ( paramIndex , OracleJsonDatum .class );
207238 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+ }
208250 }
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- }
220251 }
221252
222253 @ Override
223254 protected X doExtract (CallableStatement statement , int index , WrapperOptions options ) throws SQLException {
224- try {
225255 if ( useUtf8 ( options ) ) {
226- return fromString ( statement . getBytes ( index ), options );
256+ return fromString (getBytesFromStatementByIndex ( statement , index ), options );
227257 }
228258 else {
259+ try {
229260 OracleJsonDatum ojd = statement .getObject ( index , OracleJsonDatum .class );
230261 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+ }
231273 }
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- }
243274 }
244275
245276 @ Override
246277 protected X doExtract (CallableStatement statement , String name , WrapperOptions options )
247278 throws SQLException {
248- try {
279+
249280 if ( useUtf8 ( options ) ) {
250- return fromString ( statement . getBytes ( name ), options );
281+ return fromString (getBytesFromStatementByName ( statement , name ), options );
251282 }
252283 else {
284+ try {
253285 OracleJsonDatum ojd = statement .getObject ( name , OracleJsonDatum .class );
254286 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+ }
255298 }
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+
267300
268301 }
269302
0 commit comments