Skip to content

Commit c1d6329

Browse files
committed
Fix multiple issues related to the method findColumn(String) in ResultSets
- Fix issue #31 complement. - Return a result even if there's no row in the result set but the column exist in the statement. - Fix the exception thrown by the method when the given column name does not exist in the result set.
1 parent 9cc93b3 commit c1d6329

File tree

8 files changed

+90
-10
lines changed

8 files changed

+90
-10
lines changed

Diff for: CHANGELOG.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to
55
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## Unreleased
8+
### Fixed
9+
- Fix multiple issues related the method `findColumn(String)` of `CassandraResultSet` and `CassandraMetadataResultSet`:
10+
- Fix issue [#31](https://github.com/ing-bank/cassandra-jdbc-wrapper/issues/31) to return a 1-based index value.
11+
- Return a result even if there's no row in the result set but the column exist in the statement.
12+
- Fix the exception thrown by the method when the given column name does not exist in the result set (was an
13+
`IllegalArgumentException` instead of an `SQLException`.
14+
715
## [4.10.0] - 2023-09-30
816
### Added
917
- Add support for new [`vector` CQL type](https://datastax-oss.atlassian.net/browse/JAVA-3060)
10-
defined in [CEP-30](https://cwiki.apache.org/confluence/x/OQ40Dw)
18+
defined in [CEP-30](https://cwiki.apache.org/confluence/x/OQ40Dw).
1119
Also see PR [#27](https://github.com/ing-bank/cassandra-jdbc-wrapper/pull/27).
1220
- Implement the method `getWarnings()` in `CassandraResultSet`.
1321
- Implement the following methods of `CassandraDatabaseMetaData`:
@@ -151,10 +159,7 @@ For this version, the changelog lists the main changes comparatively to the late
151159
- Fix logs in `CassandraConnection` constructor.
152160

153161
[original project]: https://github.com/adejanovski/cassandra-jdbc-wrapper/
154-
<<<<<<< HEAD
155162
[4.10.0]: https://github.com/ing-bank/cassandra-jdbc-wrapper/compare/v4.9.1...v4.10.0
156-
=======
157-
>>>>>>> b66035e (Fix issue #25 (#26))
158163
[4.9.1]: https://github.com/ing-bank/cassandra-jdbc-wrapper/compare/v4.9.0...v4.9.1
159164
[4.9.0]: https://github.com/ing-bank/cassandra-jdbc-wrapper/compare/v4.8.0...v4.9.0
160165
[4.8.0]: https://github.com/ing-bank/cassandra-jdbc-wrapper/compare/v4.7.0...v4.8.0

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ We use [SemVer](http://semver.org/) for versioning.
715715
* Marius Jokubauskas - **[@mjok](https://github.com/mjok)**
716716
* Sualeh Fatehi - **[@sualeh](https://github.com/sualeh)**
717717
* Cedrick Lunven - **[@clun](https://github.com/clun)**
718+
* Stefano Fornari - **[@stefanofornari](https://github.com/stefanofornari)**
718719

719720
And special thanks to the developer of the original project on which is based this one:
720721
* Alexander Dejanovski - **[@adejanovski](https://github.com/adejanovski)**

Diff for: pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@
7474
<role>developer</role>
7575
</roles>
7676
</contributor>
77+
<contributor>
78+
<name>Stefano Fornari</name>
79+
<url>https://github.com/stefanofornari</url>
80+
<roles>
81+
<role>developer</role>
82+
</roles>
83+
</contributor>
7784
</contributors>
7885

7986
<scm>

Diff for: src/main/java/com/ing/data/cassandra/jdbc/CassandraMetadataResultSet.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,13 @@ public void beforeFirst() throws SQLException {
226226

227227
private void checkIndex(final int index) throws SQLException {
228228
if (this.currentRow != null) {
229-
this.wasNull = this.currentRow.isNull(index - 1);
230229
if (this.currentRow.getColumnDefinitions() != null) {
231230
if (index < 1 || index > this.currentRow.getColumnDefinitions().asList().size()) {
232231
throw new SQLSyntaxErrorException(String.format(MUST_BE_POSITIVE, index) + StringUtils.SPACE
233232
+ this.currentRow.getColumnDefinitions().asList().size());
234233
}
235234
}
235+
this.wasNull = this.currentRow.isNull(index - 1);
236236
} else if (this.driverResultSet != null) {
237237
if (this.driverResultSet.getColumnDefinitions() != null) {
238238
if (index < 1 || index > this.driverResultSet.getColumnDefinitions().asList().size()) {
@@ -245,10 +245,10 @@ private void checkIndex(final int index) throws SQLException {
245245

246246
private void checkName(final String name) throws SQLException {
247247
if (this.currentRow != null) {
248-
this.wasNull = this.currentRow.isNull(name);
249248
if (!this.currentRow.getColumnDefinitions().contains(name)) {
250249
throw new SQLSyntaxErrorException(String.format(VALID_LABELS, name));
251250
}
251+
this.wasNull = this.currentRow.isNull(name);
252252
} else if (this.driverResultSet != null) {
253253
if (this.driverResultSet.getColumnDefinitions() != null) {
254254
if (!this.driverResultSet.getColumnDefinitions().contains(name)) {
@@ -282,7 +282,12 @@ public void close() throws SQLException {
282282
public int findColumn(final String columnLabel) throws SQLException {
283283
checkNotClosed();
284284
checkName(columnLabel);
285-
return this.currentRow.getColumnDefinitions().getIndexOf(columnLabel);
285+
if (this.currentRow != null) {
286+
return this.currentRow.getColumnDefinitions().getIndexOf(columnLabel) + 1;
287+
} else if (this.driverResultSet != null) {
288+
return this.driverResultSet.getColumnDefinitions().getIndexOf(columnLabel) + 1;
289+
}
290+
throw new SQLSyntaxErrorException(String.format(VALID_LABELS, columnLabel));
286291
}
287292

288293
@Override

Diff for: src/main/java/com/ing/data/cassandra/jdbc/CassandraResultSet.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,11 @@ public void beforeFirst() throws SQLException {
293293

294294
private void checkIndex(final int index) throws SQLException {
295295
if (this.currentRow != null) {
296-
this.wasNull = this.currentRow.isNull(index - 1);
297296
if (index < 1 || index > this.currentRow.getColumnDefinitions().size()) {
298297
throw new SQLSyntaxErrorException(String.format(MUST_BE_POSITIVE, index) + StringUtils.SPACE
299298
+ this.currentRow.getColumnDefinitions().size());
300299
}
300+
this.wasNull = this.currentRow.isNull(index - 1);
301301
} else if (this.driverResultSet != null) {
302302
if (index < 1 || index > this.driverResultSet.getColumnDefinitions().size()) {
303303
throw new SQLSyntaxErrorException(String.format(MUST_BE_POSITIVE, index) + StringUtils.SPACE
@@ -309,10 +309,10 @@ private void checkIndex(final int index) throws SQLException {
309309

310310
private void checkName(final String name) throws SQLException {
311311
if (this.currentRow != null) {
312-
this.wasNull = this.currentRow.isNull(name);
313312
if (!this.currentRow.getColumnDefinitions().contains(name)) {
314313
throw new SQLSyntaxErrorException(String.format(VALID_LABELS, name));
315314
}
315+
this.wasNull = this.currentRow.isNull(name);
316316
} else if (this.driverResultSet != null) {
317317
if (!this.driverResultSet.getColumnDefinitions().contains(name)) {
318318
throw new SQLSyntaxErrorException(String.format(VALID_LABELS, name));
@@ -344,7 +344,12 @@ public void close() throws SQLException {
344344
public int findColumn(final String columnLabel) throws SQLException {
345345
checkNotClosed();
346346
checkName(columnLabel);
347-
return this.currentRow.getColumnDefinitions().firstIndexOf(columnLabel)+1;
347+
if (this.currentRow != null) {
348+
return this.currentRow.getColumnDefinitions().firstIndexOf(columnLabel) + 1;
349+
} else if (this.driverResultSet != null) {
350+
return this.driverResultSet.getColumnDefinitions().firstIndexOf(columnLabel) + 1;
351+
}
352+
throw new SQLSyntaxErrorException(String.format(VALID_LABELS, columnLabel));
348353
}
349354

350355
@Override

Diff for: src/test/java/com/ing/data/cassandra/jdbc/MetadataResultSetsUnitTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.sql.DatabaseMetaData;
3030
import java.sql.ResultSet;
3131
import java.sql.SQLException;
32+
import java.sql.SQLSyntaxErrorException;
3233
import java.sql.Statement;
3334
import java.sql.Types;
3435
import java.util.ArrayList;
@@ -397,6 +398,25 @@ void givenStatement_whenGetMetadataIsSearchable_returnExpectedValues() throws Ex
397398
stmt.close();
398399
}
399400

401+
@Test
402+
void givenMetadataResultSet_whenFindColumns_returnExpectedIndex() throws Exception {
403+
final CassandraStatement statement = (CassandraStatement) sqlConnection.createStatement();
404+
final CassandraMetadataResultSet metadataResultSet =
405+
new TableMetadataResultSetBuilder(statement).buildTables(KEYSPACE, "cf_test1");
406+
assertEquals(3, metadataResultSet.findColumn("TABLE_NAME"));
407+
final SQLSyntaxErrorException exception = assertThrows(SQLSyntaxErrorException.class,
408+
() -> metadataResultSet.findColumn("CATALOG"));
409+
assertEquals("Name provided was not in the list of valid column labels: CATALOG", exception.getMessage());
410+
}
411+
412+
@Test
413+
void givenIncompleteMetadataResultSet_whenFindColumns_throwException() {
414+
final CassandraMetadataResultSet metadataResultSet = new CassandraMetadataResultSet();
415+
final SQLSyntaxErrorException exception = assertThrows(SQLSyntaxErrorException.class,
416+
() -> metadataResultSet.findColumn("COLUMN_NAME"));
417+
assertEquals("Name provided was not in the list of valid column labels: COLUMN_NAME", exception.getMessage());
418+
}
419+
400420
/*
401421
* Types metadata
402422
*/

Diff for: src/test/java/com/ing/data/cassandra/jdbc/ResultSetUnitTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
import org.junit.jupiter.api.Test;
1919

2020
import java.sql.ResultSet;
21+
import java.sql.SQLSyntaxErrorException;
2122
import java.sql.SQLWarning;
23+
import java.sql.Statement;
2224
import java.util.Arrays;
2325

2426
import static org.junit.jupiter.api.Assertions.assertEquals;
2527
import static org.junit.jupiter.api.Assertions.assertNotNull;
28+
import static org.junit.jupiter.api.Assertions.assertThrows;
2629
import static org.mockito.ArgumentMatchers.anyString;
2730
import static org.mockito.Mockito.mock;
2831
import static org.mockito.Mockito.when;
@@ -39,6 +42,38 @@ static void finalizeSetUpTests() throws Exception {
3942
initConnection(KEYSPACE, "version=3.0.0", "localdatacenter=datacenter1");
4043
}
4144

45+
@Test
46+
void givenResultSetWithRows_whenFindColumns_returnExpectedIndex() throws Exception {
47+
final String cql = "SELECT keyname, t1iValue FROM cf_test1";
48+
final Statement statement = sqlConnection.createStatement();
49+
final ResultSet rs = statement.executeQuery(cql);
50+
assertEquals(1, rs.findColumn("keyname"));
51+
assertEquals(2, rs.findColumn("t1iValue"));
52+
final SQLSyntaxErrorException exception = assertThrows(SQLSyntaxErrorException.class,
53+
() -> rs.findColumn("t1bValue"));
54+
assertEquals("Name provided was not in the list of valid column labels: t1bValue", exception.getMessage());
55+
}
56+
57+
@Test
58+
void givenResultSetWithoutRows_whenFindColumns_returnExpectedIndex() throws Exception {
59+
final String cql = "SELECT keyname, t2iValue FROM cf_test2";
60+
final Statement statement = sqlConnection.createStatement();
61+
final ResultSet rs = statement.executeQuery(cql);
62+
assertEquals(1, rs.findColumn("keyname"));
63+
assertEquals(2, rs.findColumn("t2iValue"));
64+
final SQLSyntaxErrorException exception = assertThrows(SQLSyntaxErrorException.class,
65+
() -> rs.findColumn("t2bValue"));
66+
assertEquals("Name provided was not in the list of valid column labels: t2bValue", exception.getMessage());
67+
}
68+
69+
@Test
70+
void givenIncompleteResultSet_whenFindColumns_throwException() {
71+
final CassandraResultSet rs = new CassandraResultSet();
72+
final SQLSyntaxErrorException exception = assertThrows(SQLSyntaxErrorException.class,
73+
() -> rs.findColumn("keyname"));
74+
assertEquals("Name provided was not in the list of valid column labels: keyname", exception.getMessage());
75+
}
76+
4277
@Test
4378
void givenSelectStatementGeneratingWarning_whenGetWarnings_returnExpectedWarning() throws Exception {
4479
final CassandraStatement mockStmt = mock(CassandraStatement.class);

Diff for: src/test/resources/initEmbeddedCassandra.cql

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ t1bValue boolean,
1212
t1iValue int)
1313
WITH comment = 'First table in the keyspace';
1414

15+
INSERT INTO cf_test1 (keyname, t1bValue, t1iValue) VALUES('key1', true, 1);
16+
1517
CREATE COLUMNFAMILY cf_test2 (
1618
keyname text PRIMARY KEY,
1719
t2bValue boolean,

0 commit comments

Comments
 (0)