@@ -42,6 +42,7 @@ public class SQLResultSet implements ResultSet {
42
42
private final SQLResultSetMetaData metaData ;
43
43
44
44
private Map <String , Integer > columnByNameLookups ;
45
+ private boolean lastColumnWasNull ;
45
46
46
47
private final Statement statement ;
47
48
private final int maxRows ;
@@ -86,6 +87,24 @@ public List<Object> getCurrentRow() throws SQLException {
86
87
return iterator .getItem ();
87
88
}
88
89
90
+ protected Object getRaw (int columnIndex ) throws SQLException {
91
+ checkNotClosed ();
92
+ metaData .checkColumnIndex (columnIndex );
93
+ List <Object > row = getCurrentRow ();
94
+ Object value = row .get (columnIndex - 1 );
95
+ lastColumnWasNull = (value == null );
96
+ return value ;
97
+ }
98
+
99
+ protected Number getNumber (int columnIndex ) throws SQLException {
100
+ Number raw = (Number ) getRaw (columnIndex );
101
+ return raw == null ? 0 : raw ;
102
+ }
103
+
104
+ protected Number getNullableNumber (int columnIndex ) throws SQLException {
105
+ return (Number ) getRaw (columnIndex );
106
+ }
107
+
89
108
@ Override
90
109
public void close () throws SQLException {
91
110
if (isClosed .compareAndSet (false , true )) {
@@ -95,14 +114,8 @@ public void close() throws SQLException {
95
114
96
115
@ Override
97
116
public boolean wasNull () throws SQLException {
98
- return false ;
99
- }
100
-
101
- protected Object getRaw (int columnIndex ) throws SQLException {
102
117
checkNotClosed ();
103
- metaData .checkColumnIndex (columnIndex );
104
- List <Object > row = getCurrentRow ();
105
- return row .get (columnIndex - 1 );
118
+ return lastColumnWasNull ;
106
119
}
107
120
108
121
@ Override
@@ -156,11 +169,6 @@ public int getInt(String columnLabel) throws SQLException {
156
169
return getInt (findColumn (columnLabel ));
157
170
}
158
171
159
- private Number getNumber (int columnIndex ) throws SQLException {
160
- Number raw = (Number ) getRaw (columnIndex );
161
- return raw == null ? 0 : raw ;
162
- }
163
-
164
172
@ Override
165
173
public long getLong (int columnIndex ) throws SQLException {
166
174
return (getNumber (columnIndex )).longValue ();
@@ -193,13 +201,17 @@ public double getDouble(String columnLabel) throws SQLException {
193
201
194
202
@ Override
195
203
public BigDecimal getBigDecimal (int columnIndex , int scale ) throws SQLException {
196
- BigDecimal bigDecimal = new BigDecimal (getString (columnIndex ));
204
+ String raw = getString (columnIndex );
205
+ if (raw == null ) {
206
+ return null ;
207
+ }
208
+ BigDecimal bigDecimal = new BigDecimal (raw );
197
209
return scale > -1 ? bigDecimal .setScale (scale ) : bigDecimal ;
198
210
}
199
211
200
212
@ Override
201
213
public BigDecimal getBigDecimal (String columnLabel , int scale ) throws SQLException {
202
- return getBigDecimal (findColumn (columnLabel ));
214
+ return getBigDecimal (findColumn (columnLabel ), scale );
203
215
}
204
216
205
217
@ Override
@@ -224,7 +236,8 @@ public byte[] getBytes(String columnLabel) throws SQLException {
224
236
225
237
@ Override
226
238
public Date getDate (int columnIndex ) throws SQLException {
227
- return new java .sql .Date (getLong (columnIndex ));
239
+ Number time = getNullableNumber (columnIndex );
240
+ return time == null ? null : new java .sql .Date (time .longValue ());
228
241
}
229
242
230
243
@ Override
@@ -244,7 +257,8 @@ public Date getDate(String columnLabel, Calendar cal) throws SQLException {
244
257
245
258
@ Override
246
259
public Time getTime (int columnIndex ) throws SQLException {
247
- return new java .sql .Time (getLong (columnIndex ));
260
+ Number time = getNullableNumber (columnIndex );
261
+ return time == null ? null : new java .sql .Time (time .longValue ());
248
262
}
249
263
250
264
@ Override
@@ -264,7 +278,8 @@ public Time getTime(String columnLabel, Calendar cal) throws SQLException {
264
278
265
279
@ Override
266
280
public Timestamp getTimestamp (int columnIndex ) throws SQLException {
267
- return new java .sql .Timestamp (getLong (columnIndex ));
281
+ Number time = getNullableNumber (columnIndex );
282
+ return time == null ? null : new java .sql .Timestamp (time .longValue ());
268
283
}
269
284
270
285
@ Override
@@ -285,7 +300,8 @@ public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLExcept
285
300
286
301
@ Override
287
302
public InputStream getAsciiStream (int columnIndex ) throws SQLException {
288
- return new ByteArrayInputStream (getString (columnIndex ).getBytes (Charset .forName ("ASCII" )));
303
+ String string = getString (columnIndex );
304
+ return string == null ? null : new ByteArrayInputStream (string .getBytes (Charset .forName ("ASCII" )));
289
305
}
290
306
291
307
@ Override
@@ -306,7 +322,8 @@ public InputStream getUnicodeStream(String columnLabel) throws SQLException {
306
322
307
323
@ Override
308
324
public InputStream getBinaryStream (int columnIndex ) throws SQLException {
309
- return new ByteArrayInputStream (getBytes (columnIndex ));
325
+ byte [] bytes = getBytes (columnIndex );
326
+ return bytes == null ? null : new ByteArrayInputStream (bytes );
310
327
}
311
328
312
329
@ Override
@@ -316,12 +333,13 @@ public InputStream getBinaryStream(String columnLabel) throws SQLException {
316
333
317
334
@ Override
318
335
public Reader getCharacterStream (int columnIndex ) throws SQLException {
319
- return new StringReader (getString (columnIndex ));
336
+ String value = getString (columnIndex );
337
+ return value == null ? null : new StringReader (value );
320
338
}
321
339
322
340
@ Override
323
341
public Reader getCharacterStream (String columnLabel ) throws SQLException {
324
- return new StringReader ( getString (columnLabel ));
342
+ return getCharacterStream ( findColumn (columnLabel ));
325
343
}
326
344
327
345
@ Override
@@ -331,7 +349,7 @@ public Object getObject(int columnIndex) throws SQLException {
331
349
332
350
@ Override
333
351
public Object getObject (String columnLabel ) throws SQLException {
334
- return getRaw (findColumn (columnLabel ));
352
+ return getObject (findColumn (columnLabel ));
335
353
}
336
354
337
355
@ Override
@@ -346,12 +364,16 @@ public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQ
346
364
347
365
@ Override
348
366
public <T > T getObject (int columnIndex , Class <T > type ) throws SQLException {
349
- return type .cast (getRaw (columnIndex ));
367
+ try {
368
+ return type .cast (getRaw (columnIndex ));
369
+ } catch (Exception e ) {
370
+ throw new SQLNonTransientException (e );
371
+ }
350
372
}
351
373
352
374
@ Override
353
375
public <T > T getObject (String columnLabel , Class <T > type ) throws SQLException {
354
- return type . cast ( getRaw ( findColumn (columnLabel )) );
376
+ return getObject ( findColumn (columnLabel ), type );
355
377
}
356
378
357
379
@ Override
0 commit comments