Skip to content

Commit 9f877a5

Browse files
committed
Add a proper implementation for wasNull method
Closes: #179
1 parent d4d62e9 commit 9f877a5

File tree

3 files changed

+101
-27
lines changed

3 files changed

+101
-27
lines changed

Diff for: src/main/java/org/tarantool/jdbc/SQLResultSet.java

+46-24
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class SQLResultSet implements ResultSet {
4242
private final SQLResultSetMetaData metaData;
4343

4444
private Map<String, Integer> columnByNameLookups;
45+
private boolean lastColumnWasNull;
4546

4647
private final Statement statement;
4748
private final int maxRows;
@@ -86,6 +87,24 @@ public List<Object> getCurrentRow() throws SQLException {
8687
return iterator.getItem();
8788
}
8889

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+
89108
@Override
90109
public void close() throws SQLException {
91110
if (isClosed.compareAndSet(false, true)) {
@@ -95,14 +114,8 @@ public void close() throws SQLException {
95114

96115
@Override
97116
public boolean wasNull() throws SQLException {
98-
return false;
99-
}
100-
101-
protected Object getRaw(int columnIndex) throws SQLException {
102117
checkNotClosed();
103-
metaData.checkColumnIndex(columnIndex);
104-
List<Object> row = getCurrentRow();
105-
return row.get(columnIndex - 1);
118+
return lastColumnWasNull;
106119
}
107120

108121
@Override
@@ -156,11 +169,6 @@ public int getInt(String columnLabel) throws SQLException {
156169
return getInt(findColumn(columnLabel));
157170
}
158171

159-
private Number getNumber(int columnIndex) throws SQLException {
160-
Number raw = (Number) getRaw(columnIndex);
161-
return raw == null ? 0 : raw;
162-
}
163-
164172
@Override
165173
public long getLong(int columnIndex) throws SQLException {
166174
return (getNumber(columnIndex)).longValue();
@@ -193,13 +201,17 @@ public double getDouble(String columnLabel) throws SQLException {
193201

194202
@Override
195203
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);
197209
return scale > -1 ? bigDecimal.setScale(scale) : bigDecimal;
198210
}
199211

200212
@Override
201213
public BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException {
202-
return getBigDecimal(findColumn(columnLabel));
214+
return getBigDecimal(findColumn(columnLabel), scale);
203215
}
204216

205217
@Override
@@ -224,7 +236,8 @@ public byte[] getBytes(String columnLabel) throws SQLException {
224236

225237
@Override
226238
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());
228241
}
229242

230243
@Override
@@ -244,7 +257,8 @@ public Date getDate(String columnLabel, Calendar cal) throws SQLException {
244257

245258
@Override
246259
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());
248262
}
249263

250264
@Override
@@ -264,7 +278,8 @@ public Time getTime(String columnLabel, Calendar cal) throws SQLException {
264278

265279
@Override
266280
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());
268283
}
269284

270285
@Override
@@ -285,7 +300,8 @@ public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLExcept
285300

286301
@Override
287302
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")));
289305
}
290306

291307
@Override
@@ -306,7 +322,8 @@ public InputStream getUnicodeStream(String columnLabel) throws SQLException {
306322

307323
@Override
308324
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);
310327
}
311328

312329
@Override
@@ -316,12 +333,13 @@ public InputStream getBinaryStream(String columnLabel) throws SQLException {
316333

317334
@Override
318335
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);
320338
}
321339

322340
@Override
323341
public Reader getCharacterStream(String columnLabel) throws SQLException {
324-
return new StringReader(getString(columnLabel));
342+
return getCharacterStream(findColumn(columnLabel));
325343
}
326344

327345
@Override
@@ -331,7 +349,7 @@ public Object getObject(int columnIndex) throws SQLException {
331349

332350
@Override
333351
public Object getObject(String columnLabel) throws SQLException {
334-
return getRaw(findColumn(columnLabel));
352+
return getObject(findColumn(columnLabel));
335353
}
336354

337355
@Override
@@ -346,12 +364,16 @@ public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQ
346364

347365
@Override
348366
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+
}
350372
}
351373

352374
@Override
353375
public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
354-
return type.cast(getRaw(findColumn(columnLabel)));
376+
return getObject(findColumn(columnLabel), type);
355377
}
356378

357379
@Override

Diff for: src/test/java/org/tarantool/jdbc/AbstractJdbcIT.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ public abstract class AbstractJdbcIT {
3939
"CREATE TABLE test(id INT PRIMARY KEY, val VARCHAR(100))",
4040
"INSERT INTO test VALUES (1, 'one'), (2, 'two'), (3, 'three')",
4141
"CREATE TABLE test_compound(id1 INT, id2 INT, val VARCHAR(100), PRIMARY KEY (id2, id1))",
42-
"CREATE TABLE test_nulls(id INT PRIMARY KEY, val VARCHAR(100))",
43-
"INSERT INTO test_nulls VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, NULL), (5, NULL), (6, NULL)",
42+
"CREATE TABLE test_nulls(id INT PRIMARY KEY, val VARCHAR(100), dig INTEGER, bin SCALAR)",
43+
"INSERT INTO test_nulls VALUES (1, 'a', 10, 'aa'), (2, 'b', 20, 'bb'), (3, 'c', 30, 'cc'), " +
44+
"(4, NULL, NULL, NULL), (5, NULL, NULL, NULL), (6, NULL, NULL, NULL)",
4445
getCreateTableSQL("test_types", TntSqlType.values())
4546
};
4647

Diff for: src/test/java/org/tarantool/jdbc/JdbcResultSetIT.java

+52-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public void testNullsSortingAsc() throws SQLException {
208208

209209
@Test
210210
public void testNullsSortingDesc() throws SQLException {
211-
ResultSet resultSet = stmt.executeQuery("SELECT * FROM test_nulls ORDER BY val DESC");
211+
ResultSet resultSet = stmt.executeQuery("SELECT id, dig FROM test_nulls ORDER BY val DESC");
212212
for (int i = 0; i < 3; i++) {
213213
assertTrue(resultSet.next());
214214
assertNotNull(resultSet.getString(2));
@@ -220,6 +220,57 @@ public void testNullsSortingDesc() throws SQLException {
220220
assertFalse(resultSet.next());
221221
}
222222

223+
@Test
224+
void testObjectWasNullColumn() throws SQLException {
225+
ResultSet resultSet = stmt.executeQuery("SELECT id, dig FROM test_nulls WHERE val IS NULL");
226+
resultSet.next();
227+
228+
resultSet.getInt(1);
229+
assertFalse(resultSet.wasNull());
230+
assertNull(resultSet.getString(2));
231+
assertTrue(resultSet.wasNull());
232+
}
233+
234+
@Test
235+
void testBinaryWasNullColumn() throws SQLException {
236+
ResultSet resultSet = stmt.executeQuery("SELECT id, bin FROM test_nulls WHERE bin IS NULL");
237+
resultSet.next();
238+
239+
resultSet.getInt(1);
240+
assertFalse(resultSet.wasNull());
241+
assertNull(resultSet.getString(2));
242+
assertTrue(resultSet.wasNull());
243+
assertNull(resultSet.getAsciiStream(2));
244+
assertTrue(resultSet.wasNull());
245+
assertNull(resultSet.getBinaryStream(2));
246+
assertTrue(resultSet.wasNull());
247+
assertNull(resultSet.getUnicodeStream(2));
248+
assertTrue(resultSet.wasNull());
249+
assertNull(resultSet.getCharacterStream(2));
250+
assertTrue(resultSet.wasNull());
251+
}
252+
253+
@Test
254+
void testNumberWasNullColumn() throws SQLException {
255+
ResultSet resultSet = stmt.executeQuery("SELECT id, dig FROM test_nulls WHERE dig IS NULL");
256+
resultSet.next();
257+
258+
resultSet.getInt(1);
259+
assertFalse(resultSet.wasNull());
260+
assertEquals(0, resultSet.getInt(2));
261+
assertTrue(resultSet.wasNull());
262+
assertEquals(0, resultSet.getShort(2));
263+
assertTrue(resultSet.wasNull());
264+
assertEquals(0, resultSet.getByte(2));
265+
assertTrue(resultSet.wasNull());
266+
assertEquals(0, resultSet.getLong(2));
267+
assertTrue(resultSet.wasNull());
268+
assertEquals(0, resultSet.getDouble(2));
269+
assertTrue(resultSet.wasNull());
270+
assertEquals(0, resultSet.getFloat(2));
271+
assertTrue(resultSet.wasNull());
272+
}
273+
223274
@Test
224275
public void testFindUniqueColumnLabels() throws SQLException {
225276
ResultSet resultSet = stmt.executeQuery("SELECT id as f1, val as f2 FROM test");

0 commit comments

Comments
 (0)