Skip to content

Commit 0604ac5

Browse files
committed
added tests for wasNull. Fixed bug in statement with close on complete
1 parent 798d4d5 commit 0604ac5

File tree

4 files changed

+158
-14
lines changed

4 files changed

+158
-14
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ public class ResultSetImpl implements ResultSet, JdbcV2Wrapper {
5151

5252
private static final int AFTER_LAST = -1;
5353
private static final int BEFORE_FIRST = 0;
54+
private static final int FIRST_ROW = 1;
5455
private int rowPos;
5556

57+
private int fetchSize;
58+
5659
public ResultSetImpl(StatementImpl parentStatement, QueryResponse response, ClickHouseBinaryFormatReader reader) throws SQLException {
5760
this.parentStatement = parentStatement;
5861
this.response = response;
@@ -68,6 +71,7 @@ public ResultSetImpl(StatementImpl parentStatement, QueryResponse response, Clic
6871
this.wasNull = false;
6972
this.defaultCalendar = parentStatement.connection.defaultCalendar;
7073
this.rowPos = BEFORE_FIRST;
74+
this.fetchSize = parentStatement.getFetchSize();
7175
}
7276

7377
protected ResultSetImpl(ResultSetImpl resultSet) throws SQLException{
@@ -534,13 +538,13 @@ public boolean isAfterLast() throws SQLException {
534538
@Override
535539
public boolean isFirst() throws SQLException {
536540
checkClosed();
537-
return rowPos == 0;
541+
return rowPos == FIRST_ROW;
538542
}
539543

540544
@Override
541545
public boolean isLast() throws SQLException {
542546
checkClosed();
543-
return reader.hasNext();
547+
return !reader.hasNext() && rowPos != AFTER_LAST;
544548
}
545549

546550
@Override
@@ -574,7 +578,7 @@ public boolean last() throws SQLException {
574578
@Override
575579
public int getRow() throws SQLException {
576580
checkClosed();
577-
return rowPos;
581+
return rowPos == AFTER_LAST ? 0 : rowPos;
578582
}
579583

580584
@Override
@@ -610,18 +614,24 @@ public int getFetchDirection() throws SQLException {
610614
@Override
611615
public void setFetchDirection(int direction) throws SQLException {
612616
checkClosed();
613-
featureManager.unsupportedFeatureThrow("setFetchDirection");
617+
if (direction != ResultSet.FETCH_FORWARD) {
618+
throw new SQLException("This result set object is of FORWARD ONLY type. Only ResultSet.FETCH_FORWARD is allowed as fetchDirection.");
619+
}
614620
}
615621

616622
@Override
617623
public int getFetchSize() throws SQLException {
618624
checkClosed();
619-
return 0;
625+
return fetchSize;
620626
}
621627

622628
@Override
623629
public void setFetchSize(int rows) throws SQLException {
624630
checkClosed();
631+
if (rows < 0) {
632+
throw new SQLException("Number of rows should be a positive integer");
633+
}
634+
fetchSize = rows;
625635
}
626636

627637
@Override
@@ -1141,6 +1151,7 @@ public RowId getRowId(int columnIndex) throws SQLException {
11411151
@Override
11421152
public RowId getRowId(String columnLabel) throws SQLException {
11431153
checkClosed();
1154+
featureManager.unsupportedFeatureThrow("getRowId");
11441155
return null;
11451156
}
11461157

jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public class StatementImpl implements Statement, JdbcV2Wrapper {
4444
private int maxFieldSize;
4545
private boolean escapeProcessingEnabled;
4646

47+
private int fetchSize = 1;
48+
4749
// settings local to a statement
4850
protected QuerySettings localSettings;
4951

@@ -129,6 +131,7 @@ protected ResultSetImpl executeQueryImpl(String sql, QuerySettings settings) thr
129131
// release before this one completes.
130132
if (resultSetAutoClose) {
131133
closeCurrentResultSet();
134+
this.closed = false; // restore state because we are going to create a new result set.
132135
}
133136

134137
QuerySettings mergedSettings = QuerySettings.merge(settings, new QuerySettings());
@@ -362,12 +365,16 @@ public int getFetchDirection() throws SQLException {
362365
@Override
363366
public void setFetchSize(int rows) throws SQLException {
364367
ensureOpen();
368+
if (rows < 0) {
369+
throw new SQLException("rows should be greater than 0.");
370+
}
371+
this.fetchSize = rows;
365372
}
366373

367374
@Override
368375
public int getFetchSize() throws SQLException {
369376
ensureOpen();
370-
return 0;
377+
return fetchSize;
371378
}
372379

373380
@Override

jdbc-v2/src/test/java/com/clickhouse/jdbc/ResultSetImplTest.java

Lines changed: 133 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.math.BigDecimal;
1010
import java.sql.Array;
1111
import java.sql.Blob;
12+
import java.sql.Clob;
1213
import java.sql.Connection;
1314
import java.sql.Date;
1415
import java.sql.JDBCType;
@@ -52,10 +53,10 @@ public void shouldReturnColumnIndex() throws SQLException {
5253
}
5354
}
5455

55-
@Test(groups = { "integration" })
56+
@Test(groups = {"integration"})
5657
public void testUnsupportedOperations() throws Throwable {
5758

58-
boolean[] throwUnsupportedException = new boolean[] {false, true};
59+
boolean[] throwUnsupportedException = new boolean[]{false, true};
5960

6061
for (boolean flag : throwUnsupportedException) {
6162
Properties props = new Properties();
@@ -64,7 +65,7 @@ public void testUnsupportedOperations() throws Throwable {
6465
}
6566

6667
try (Connection conn = this.getJdbcConnection(props); Statement stmt = conn.createStatement();
67-
ResultSet rs = stmt.executeQuery("SELECT 1")) {
68+
ResultSet rs = stmt.executeQuery("SELECT 1")) {
6869
Assert.ThrowingRunnable[] rsUnsupportedMethods = new Assert.ThrowingRunnable[]{
6970
() -> rs.first(),
7071
() -> rs.afterLast(),
@@ -92,8 +93,8 @@ public void testUnsupportedOperations() throws Throwable {
9293
() -> rs.updateTimestamp("col1", Timestamp.valueOf("2020-01-01 12:34:56.789123")),
9394
() -> rs.updateBlob("col1", (Blob) null),
9495
() -> rs.updateClob("col1", new StringReader("test")),
95-
() -> rs.updateNClob("col1", new StringReader("test")),
96-
96+
() -> rs.updateNClob("col1", new StringReader("test")),
97+
9798
() -> rs.updateBoolean(1, true),
9899
() -> rs.updateByte(1, (byte) 1),
99100
() -> rs.updateShort(1, (short) 1),
@@ -176,10 +177,15 @@ public void testUnsupportedOperations() throws Throwable {
176177
() -> rs.getNClob("col1"),
177178
() -> rs.getRef(1),
178179
() -> rs.getRef("col1"),
180+
() -> rs.getRowId(1),
181+
() -> rs.getRowId("col1"),
179182
() -> rs.cancelRowUpdates(),
180183
() -> rs.updateNull(1),
181184
() -> rs.updateNull("col1"),
182-
185+
() -> rs.updateRowId(1, null),
186+
() -> rs.updateRowId("col1", null),
187+
() -> rs.updateClob(1, (Clob) null),
188+
() -> rs.updateClob("col1", (Clob) null),
183189
() -> rs.updateRow(),
184190
() -> rs.insertRow(),
185191
() -> rs.deleteRow(),
@@ -190,12 +196,132 @@ public void testUnsupportedOperations() throws Throwable {
190196

191197
for (Assert.ThrowingRunnable op : rsUnsupportedMethods) {
192198
if (!flag) {
193-
Assert.assertThrows(SQLFeatureNotSupportedException.class, op );
199+
Assert.assertThrows(SQLFeatureNotSupportedException.class, op);
194200
} else {
195201
op.run();
196202
}
197203
}
198204
}
199205
}
200206
}
207+
208+
209+
@Test(groups = {"integration"})
210+
public void testCursorPosition() throws SQLException {
211+
try (Connection conn = getJdbcConnection(); Statement stmt = conn.createStatement()) {
212+
try (ResultSet rs = stmt.executeQuery("select number from system.numbers LIMIT 2")) {
213+
Assert.assertTrue(rs.isBeforeFirst());
214+
Assert.assertFalse(rs.isAfterLast());
215+
Assert.assertFalse(rs.isFirst());
216+
Assert.assertFalse(rs.isLast());
217+
Assert.assertEquals(rs.getRow(), 0);
218+
219+
rs.next();
220+
221+
Assert.assertFalse(rs.isBeforeFirst());
222+
Assert.assertFalse(rs.isAfterLast());
223+
Assert.assertTrue(rs.isFirst());
224+
Assert.assertFalse(rs.isLast());
225+
Assert.assertEquals(rs.getRow(), 1);
226+
227+
rs.next();
228+
229+
Assert.assertFalse(rs.isBeforeFirst());
230+
Assert.assertFalse(rs.isAfterLast());
231+
Assert.assertFalse(rs.isFirst());
232+
Assert.assertTrue(rs.isLast());
233+
Assert.assertEquals(rs.getRow(), 2);
234+
235+
rs.next();
236+
237+
Assert.assertFalse(rs.isBeforeFirst());
238+
Assert.assertTrue(rs.isAfterLast());
239+
Assert.assertFalse(rs.isFirst());
240+
Assert.assertFalse(rs.isLast());
241+
Assert.assertEquals(rs.getRow(), 0);
242+
243+
}
244+
}
245+
}
246+
247+
248+
@Test(groups = {"integration"})
249+
public void testFetchDirectionsAndSize() throws SQLException {
250+
try (Connection conn = getJdbcConnection(); Statement stmt = conn.createStatement()) {
251+
try (ResultSet rs = stmt.executeQuery("select number from system.numbers LIMIT 2")) {
252+
Assert.assertEquals(rs.getFetchDirection(), ResultSet.FETCH_FORWARD);
253+
Assert.expectThrows(SQLException.class, () -> rs.setFetchDirection(ResultSet.FETCH_REVERSE));
254+
Assert.expectThrows(SQLException.class, () -> rs.setFetchDirection(ResultSet.FETCH_UNKNOWN));
255+
rs.setFetchDirection(ResultSet.FETCH_FORWARD);
256+
257+
Assert.assertEquals(rs.getFetchSize(), 1);
258+
rs.setFetchSize(10);
259+
Assert.assertEquals(rs.getFetchSize(), 10);
260+
Assert.expectThrows(SQLException.class, () -> rs.setFetchSize(-10));
261+
}
262+
}
263+
}
264+
265+
@Test(groups = {"integration"})
266+
public void testConstants() throws SQLException {
267+
try (Connection conn = getJdbcConnection(); Statement stmt = conn.createStatement()) {
268+
try (ResultSet rs = stmt.executeQuery("select number from system.numbers LIMIT 2")) {
269+
Assert.assertEquals(rs.getType(), ResultSet.TYPE_FORWARD_ONLY);
270+
Assert.assertEquals(rs.getConcurrency(), ResultSet.CONCUR_READ_ONLY);
271+
Assert.assertEquals(rs.getHoldability(), ResultSet.HOLD_CURSORS_OVER_COMMIT);
272+
}
273+
}
274+
}
275+
276+
@Test(groups = {"integration"})
277+
public void testWasNull() throws SQLException {
278+
try (Connection conn = getJdbcConnection(); Statement stmt = conn.createStatement()) {
279+
final String sql = "select NULL::Nullable(%s) as v1";
280+
281+
try (ResultSet rs = stmt.executeQuery(sql.formatted("Int64"))) {
282+
rs.next();
283+
Assert.assertFalse(rs.wasNull());
284+
285+
Assert.assertEquals(rs.getByte(1), (byte) 0);
286+
Assert.assertTrue(rs.wasNull());
287+
Assert.assertEquals(rs.getByte("v1"), (byte) 0);
288+
Assert.assertTrue(rs.wasNull());
289+
290+
Assert.assertEquals(rs.getShort(1), (short) 0);
291+
Assert.assertTrue(rs.wasNull());
292+
Assert.assertEquals(rs.getShort("v1"), (short) 0);
293+
Assert.assertTrue(rs.wasNull());
294+
295+
Assert.assertEquals(rs.getInt(1), 0);
296+
Assert.assertTrue(rs.wasNull());
297+
Assert.assertEquals(rs.getInt("v1"), 0);
298+
Assert.assertTrue(rs.wasNull());
299+
300+
Assert.assertEquals(rs.getLong(1), 0L);
301+
Assert.assertTrue(rs.wasNull());
302+
Assert.assertEquals(rs.getLong("v1"), 0L);
303+
Assert.assertTrue(rs.wasNull());
304+
305+
Assert.assertNull(rs.getBigDecimal(1));
306+
Assert.assertTrue(rs.wasNull());
307+
Assert.assertNull(rs.getBigDecimal("v1"));
308+
Assert.assertTrue(rs.wasNull());
309+
310+
Assert.assertEquals(rs.getFloat(1), 0f);
311+
Assert.assertTrue(rs.wasNull());
312+
Assert.assertEquals(rs.getFloat("v1"), 0f);
313+
Assert.assertTrue(rs.wasNull());
314+
315+
Assert.assertEquals(rs.getDouble(1), 0d);
316+
Assert.assertTrue(rs.wasNull());
317+
Assert.assertEquals(rs.getDouble("v1"), 0d);
318+
Assert.assertTrue(rs.wasNull());
319+
320+
Assert.assertEquals(rs.getBoolean(1), false);
321+
Assert.assertTrue(rs.wasNull());
322+
Assert.assertEquals(rs.getBoolean("v1"), false);
323+
Assert.assertTrue(rs.wasNull());
324+
}
325+
}
326+
}
201327
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ public void testVariousSimpleMethods() throws Exception {
958958
stmt.setQueryTimeout(100);
959959
Assert.assertEquals(stmt.getQueryTimeout(), 100);
960960
stmt.setFetchSize(100);
961-
Assert.assertEquals(stmt.getFetchSize(), 0); // we ignore this hint
961+
Assert.assertEquals(stmt.getFetchSize(), 100); // we ignore this hint
962962
Assert.assertEquals(stmt.getResultSetConcurrency(), ResultSet.CONCUR_READ_ONLY);
963963
Assert.assertEquals(stmt.getResultSetType(), ResultSet.TYPE_FORWARD_ONLY);
964964
Assert.assertNotNull(stmt.getConnection());

0 commit comments

Comments
 (0)