Skip to content

Commit

Permalink
Fix multiple issues related to the method findColumn(String) in Resul…
Browse files Browse the repository at this point in the history
…tSets

- 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.
  • Loading branch information
maximevw committed Oct 1, 2023
1 parent 9cc93b3 commit c1d6329
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 10 deletions.
13 changes: 9 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Fixed
- Fix multiple issues related the method `findColumn(String)` of `CassandraResultSet` and `CassandraMetadataResultSet`:
- Fix issue [#31](https://github.com/ing-bank/cassandra-jdbc-wrapper/issues/31) to return a 1-based index value.
- 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 (was an
`IllegalArgumentException` instead of an `SQLException`.

## [4.10.0] - 2023-09-30
### Added
- Add support for new [`vector` CQL type](https://datastax-oss.atlassian.net/browse/JAVA-3060)
defined in [CEP-30](https://cwiki.apache.org/confluence/x/OQ40Dw)
defined in [CEP-30](https://cwiki.apache.org/confluence/x/OQ40Dw).
Also see PR [#27](https://github.com/ing-bank/cassandra-jdbc-wrapper/pull/27).
- Implement the method `getWarnings()` in `CassandraResultSet`.
- Implement the following methods of `CassandraDatabaseMetaData`:
Expand Down Expand Up @@ -151,10 +159,7 @@ For this version, the changelog lists the main changes comparatively to the late
- Fix logs in `CassandraConnection` constructor.

[original project]: https://github.com/adejanovski/cassandra-jdbc-wrapper/
<<<<<<< HEAD
[4.10.0]: https://github.com/ing-bank/cassandra-jdbc-wrapper/compare/v4.9.1...v4.10.0
=======
>>>>>>> b66035e (Fix issue #25 (#26))
[4.9.1]: https://github.com/ing-bank/cassandra-jdbc-wrapper/compare/v4.9.0...v4.9.1
[4.9.0]: https://github.com/ing-bank/cassandra-jdbc-wrapper/compare/v4.8.0...v4.9.0
[4.8.0]: https://github.com/ing-bank/cassandra-jdbc-wrapper/compare/v4.7.0...v4.8.0
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ We use [SemVer](http://semver.org/) for versioning.
* Marius Jokubauskas - **[@mjok](https://github.com/mjok)**
* Sualeh Fatehi - **[@sualeh](https://github.com/sualeh)**
* Cedrick Lunven - **[@clun](https://github.com/clun)**
* Stefano Fornari - **[@stefanofornari](https://github.com/stefanofornari)**

And special thanks to the developer of the original project on which is based this one:
* Alexander Dejanovski - **[@adejanovski](https://github.com/adejanovski)**
Expand Down
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@
<role>developer</role>
</roles>
</contributor>
<contributor>
<name>Stefano Fornari</name>
<url>https://github.com/stefanofornari</url>
<roles>
<role>developer</role>
</roles>
</contributor>
</contributors>

<scm>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ public void beforeFirst() throws SQLException {

private void checkIndex(final int index) throws SQLException {
if (this.currentRow != null) {
this.wasNull = this.currentRow.isNull(index - 1);
if (this.currentRow.getColumnDefinitions() != null) {
if (index < 1 || index > this.currentRow.getColumnDefinitions().asList().size()) {
throw new SQLSyntaxErrorException(String.format(MUST_BE_POSITIVE, index) + StringUtils.SPACE
+ this.currentRow.getColumnDefinitions().asList().size());
}
}
this.wasNull = this.currentRow.isNull(index - 1);
} else if (this.driverResultSet != null) {
if (this.driverResultSet.getColumnDefinitions() != null) {
if (index < 1 || index > this.driverResultSet.getColumnDefinitions().asList().size()) {
Expand All @@ -245,10 +245,10 @@ private void checkIndex(final int index) throws SQLException {

private void checkName(final String name) throws SQLException {
if (this.currentRow != null) {
this.wasNull = this.currentRow.isNull(name);
if (!this.currentRow.getColumnDefinitions().contains(name)) {
throw new SQLSyntaxErrorException(String.format(VALID_LABELS, name));
}
this.wasNull = this.currentRow.isNull(name);
} else if (this.driverResultSet != null) {
if (this.driverResultSet.getColumnDefinitions() != null) {
if (!this.driverResultSet.getColumnDefinitions().contains(name)) {
Expand Down Expand Up @@ -282,7 +282,12 @@ public void close() throws SQLException {
public int findColumn(final String columnLabel) throws SQLException {
checkNotClosed();
checkName(columnLabel);
return this.currentRow.getColumnDefinitions().getIndexOf(columnLabel);
if (this.currentRow != null) {
return this.currentRow.getColumnDefinitions().getIndexOf(columnLabel) + 1;
} else if (this.driverResultSet != null) {
return this.driverResultSet.getColumnDefinitions().getIndexOf(columnLabel) + 1;
}
throw new SQLSyntaxErrorException(String.format(VALID_LABELS, columnLabel));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,11 @@ public void beforeFirst() throws SQLException {

private void checkIndex(final int index) throws SQLException {
if (this.currentRow != null) {
this.wasNull = this.currentRow.isNull(index - 1);
if (index < 1 || index > this.currentRow.getColumnDefinitions().size()) {
throw new SQLSyntaxErrorException(String.format(MUST_BE_POSITIVE, index) + StringUtils.SPACE
+ this.currentRow.getColumnDefinitions().size());
}
this.wasNull = this.currentRow.isNull(index - 1);
} else if (this.driverResultSet != null) {
if (index < 1 || index > this.driverResultSet.getColumnDefinitions().size()) {
throw new SQLSyntaxErrorException(String.format(MUST_BE_POSITIVE, index) + StringUtils.SPACE
Expand All @@ -309,10 +309,10 @@ private void checkIndex(final int index) throws SQLException {

private void checkName(final String name) throws SQLException {
if (this.currentRow != null) {
this.wasNull = this.currentRow.isNull(name);
if (!this.currentRow.getColumnDefinitions().contains(name)) {
throw new SQLSyntaxErrorException(String.format(VALID_LABELS, name));
}
this.wasNull = this.currentRow.isNull(name);
} else if (this.driverResultSet != null) {
if (!this.driverResultSet.getColumnDefinitions().contains(name)) {
throw new SQLSyntaxErrorException(String.format(VALID_LABELS, name));
Expand Down Expand Up @@ -344,7 +344,12 @@ public void close() throws SQLException {
public int findColumn(final String columnLabel) throws SQLException {
checkNotClosed();
checkName(columnLabel);
return this.currentRow.getColumnDefinitions().firstIndexOf(columnLabel)+1;
if (this.currentRow != null) {
return this.currentRow.getColumnDefinitions().firstIndexOf(columnLabel) + 1;
} else if (this.driverResultSet != null) {
return this.driverResultSet.getColumnDefinitions().firstIndexOf(columnLabel) + 1;
}
throw new SQLSyntaxErrorException(String.format(VALID_LABELS, columnLabel));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLSyntaxErrorException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
Expand Down Expand Up @@ -397,6 +398,25 @@ void givenStatement_whenGetMetadataIsSearchable_returnExpectedValues() throws Ex
stmt.close();
}

@Test
void givenMetadataResultSet_whenFindColumns_returnExpectedIndex() throws Exception {
final CassandraStatement statement = (CassandraStatement) sqlConnection.createStatement();
final CassandraMetadataResultSet metadataResultSet =
new TableMetadataResultSetBuilder(statement).buildTables(KEYSPACE, "cf_test1");
assertEquals(3, metadataResultSet.findColumn("TABLE_NAME"));
final SQLSyntaxErrorException exception = assertThrows(SQLSyntaxErrorException.class,
() -> metadataResultSet.findColumn("CATALOG"));
assertEquals("Name provided was not in the list of valid column labels: CATALOG", exception.getMessage());
}

@Test
void givenIncompleteMetadataResultSet_whenFindColumns_throwException() {
final CassandraMetadataResultSet metadataResultSet = new CassandraMetadataResultSet();
final SQLSyntaxErrorException exception = assertThrows(SQLSyntaxErrorException.class,
() -> metadataResultSet.findColumn("COLUMN_NAME"));
assertEquals("Name provided was not in the list of valid column labels: COLUMN_NAME", exception.getMessage());
}

/*
* Types metadata
*/
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/com/ing/data/cassandra/jdbc/ResultSetUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
import org.junit.jupiter.api.Test;

import java.sql.ResultSet;
import java.sql.SQLSyntaxErrorException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -39,6 +42,38 @@ static void finalizeSetUpTests() throws Exception {
initConnection(KEYSPACE, "version=3.0.0", "localdatacenter=datacenter1");
}

@Test
void givenResultSetWithRows_whenFindColumns_returnExpectedIndex() throws Exception {
final String cql = "SELECT keyname, t1iValue FROM cf_test1";
final Statement statement = sqlConnection.createStatement();
final ResultSet rs = statement.executeQuery(cql);
assertEquals(1, rs.findColumn("keyname"));
assertEquals(2, rs.findColumn("t1iValue"));
final SQLSyntaxErrorException exception = assertThrows(SQLSyntaxErrorException.class,
() -> rs.findColumn("t1bValue"));
assertEquals("Name provided was not in the list of valid column labels: t1bValue", exception.getMessage());
}

@Test
void givenResultSetWithoutRows_whenFindColumns_returnExpectedIndex() throws Exception {
final String cql = "SELECT keyname, t2iValue FROM cf_test2";
final Statement statement = sqlConnection.createStatement();
final ResultSet rs = statement.executeQuery(cql);
assertEquals(1, rs.findColumn("keyname"));
assertEquals(2, rs.findColumn("t2iValue"));
final SQLSyntaxErrorException exception = assertThrows(SQLSyntaxErrorException.class,
() -> rs.findColumn("t2bValue"));
assertEquals("Name provided was not in the list of valid column labels: t2bValue", exception.getMessage());
}

@Test
void givenIncompleteResultSet_whenFindColumns_throwException() {
final CassandraResultSet rs = new CassandraResultSet();
final SQLSyntaxErrorException exception = assertThrows(SQLSyntaxErrorException.class,
() -> rs.findColumn("keyname"));
assertEquals("Name provided was not in the list of valid column labels: keyname", exception.getMessage());
}

@Test
void givenSelectStatementGeneratingWarning_whenGetWarnings_returnExpectedWarning() throws Exception {
final CassandraStatement mockStmt = mock(CassandraStatement.class);
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/initEmbeddedCassandra.cql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ t1bValue boolean,
t1iValue int)
WITH comment = 'First table in the keyspace';

INSERT INTO cf_test1 (keyname, t1bValue, t1iValue) VALUES('key1', true, 1);

CREATE COLUMNFAMILY cf_test2 (
keyname text PRIMARY KEY,
t2bValue boolean,
Expand Down

0 comments on commit c1d6329

Please sign in to comment.