Skip to content

Commit f00a843

Browse files
committed
Add a proper implementation for wasNull method
Closes: #179
1 parent 494292b commit f00a843

File tree

3 files changed

+84
-28
lines changed

3 files changed

+84
-28
lines changed

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

+48-25
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
@@ -295,7 +311,8 @@ public InputStream getAsciiStream(String columnLabel) throws SQLException {
295311

296312
@Override
297313
public InputStream getUnicodeStream(int columnIndex) throws SQLException {
298-
return new ByteArrayInputStream(getString(columnIndex).getBytes(Charset.forName("UTF-8")));
314+
String string = getString(columnIndex);
315+
return string == null ? null : new ByteArrayInputStream(string.getBytes(Charset.forName("UTF-16")));
299316
}
300317

301318
@Override
@@ -305,7 +322,8 @@ public InputStream getUnicodeStream(String columnLabel) throws SQLException {
305322

306323
@Override
307324
public InputStream getBinaryStream(int columnIndex) throws SQLException {
308-
return new ByteArrayInputStream(getBytes(columnIndex));
325+
byte[] bytes = getBytes(columnIndex);
326+
return bytes == null ? null : new ByteArrayInputStream(bytes);
309327
}
310328

311329
@Override
@@ -315,12 +333,13 @@ public InputStream getBinaryStream(String columnLabel) throws SQLException {
315333

316334
@Override
317335
public Reader getCharacterStream(int columnIndex) throws SQLException {
318-
return new StringReader(getString(columnIndex));
336+
String value = getString(columnIndex);
337+
return value == null ? null : new StringReader(value);
319338
}
320339

321340
@Override
322341
public Reader getCharacterStream(String columnLabel) throws SQLException {
323-
return new StringReader(getString(columnLabel));
342+
return getCharacterStream(findColumn(columnLabel));
324343
}
325344

326345
@Override
@@ -330,7 +349,7 @@ public Object getObject(int columnIndex) throws SQLException {
330349

331350
@Override
332351
public Object getObject(String columnLabel) throws SQLException {
333-
return getRaw(findColumn(columnLabel));
352+
return getObject(findColumn(columnLabel));
334353
}
335354

336355
@Override
@@ -345,12 +364,16 @@ public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQ
345364

346365
@Override
347366
public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
348-
return type.cast(getRaw(columnIndex));
367+
try {
368+
return type.cast(getRaw(columnIndex));
369+
} catch (Exception e) {
370+
throw new SQLNonTransientException(e);
371+
}
349372
}
350373

351374
@Override
352375
public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
353-
return type.cast(getRaw(findColumn(columnLabel)));
376+
return getObject(findColumn(columnLabel), type);
354377
}
355378

356379
@Override

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ public abstract class AbstractJdbcIT {
3636
"CREATE TABLE test(id INT PRIMARY KEY, val VARCHAR(100))",
3737
"INSERT INTO test VALUES (1, 'one'), (2, 'two'), (3, 'three')",
3838
"CREATE TABLE test_compound(id1 INT, id2 INT, val VARCHAR(100), PRIMARY KEY (id2, id1))",
39-
"CREATE TABLE test_nulls(id INT PRIMARY KEY, val VARCHAR(100))",
40-
"INSERT INTO test_nulls VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, NULL), (5, NULL), (6, NULL)",
39+
"CREATE TABLE test_nulls(id INT PRIMARY KEY, val VARCHAR(100), dig INTEGER)",
40+
"INSERT INTO test_nulls VALUES (1, 'a', 10), (2, 'b', 20), (3, 'c', 30), " +
41+
"(4, NULL, NULL), (5, NULL, NULL), (6, NULL, NULL)",
4142
getCreateTableSQL("test_types", TntSqlType.values())
4243
};
4344

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

+33-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,38 @@ 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 testNumberWasNullColumn() throws SQLException {
236+
ResultSet resultSet = stmt.executeQuery("SELECT id, dig FROM test_nulls WHERE dig IS NULL");
237+
resultSet.next();
238+
239+
resultSet.getInt(1);
240+
assertFalse(resultSet.wasNull());
241+
assertEquals(0, resultSet.getInt(2));
242+
assertTrue(resultSet.wasNull());
243+
assertEquals(0, resultSet.getShort(2));
244+
assertTrue(resultSet.wasNull());
245+
assertEquals(0, resultSet.getByte(2));
246+
assertTrue(resultSet.wasNull());
247+
assertEquals(0, resultSet.getLong(2));
248+
assertTrue(resultSet.wasNull());
249+
assertEquals(0, resultSet.getDouble(2));
250+
assertTrue(resultSet.wasNull());
251+
assertEquals(0, resultSet.getFloat(2));
252+
assertTrue(resultSet.wasNull());
253+
}
254+
223255
@Test
224256
public void testFindUniqueColumnLabels() throws SQLException {
225257
ResultSet resultSet = stmt.executeQuery("SELECT id as f1, val as f2 FROM test");

0 commit comments

Comments
 (0)