diff --git a/CHANGELOG.md b/CHANGELOG.md index 28de2a0..39ba186 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,10 +4,21 @@ 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). +## [4.10.1] - 2023-10-07 +### Changed +- Update Apache Commons IO to version 2.14.0. +- Harmonize logging. +### Fixed +- Fix multiple issues related to 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`: @@ -151,6 +162,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/ +[4.10.1]: https://github.com/ing-bank/cassandra-jdbc-wrapper/compare/v4.10.0...v4.10.1 [4.10.0]: https://github.com/ing-bank/cassandra-jdbc-wrapper/compare/v4.9.1...v4.10.0 [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 diff --git a/README.md b/README.md index 3924a0a..5128dd4 100644 --- a/README.md +++ b/README.md @@ -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)** diff --git a/pom.xml b/pom.xml index f79a208..9583377 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.ing.data cassandra-jdbc-wrapper - 4.10.0 + 4.10.1 jar Cassandra JDBC Wrapper @@ -74,6 +74,13 @@ developer + + Stefano Fornari + https://github.com/stefanofornari + + developer + + @@ -101,7 +108,7 @@ 9.3 2.9.3 4.4 - 2.13.0 + 2.14.0 3.13.0 4.17.0 2.15.2 diff --git a/src/main/java/com/ing/data/cassandra/jdbc/CassandraMetadataResultSet.java b/src/main/java/com/ing/data/cassandra/jdbc/CassandraMetadataResultSet.java index 564288c..a9211cb 100644 --- a/src/main/java/com/ing/data/cassandra/jdbc/CassandraMetadataResultSet.java +++ b/src/main/java/com/ing/data/cassandra/jdbc/CassandraMetadataResultSet.java @@ -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()) { @@ -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)) { @@ -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 diff --git a/src/main/java/com/ing/data/cassandra/jdbc/CassandraPreparedStatement.java b/src/main/java/com/ing/data/cassandra/jdbc/CassandraPreparedStatement.java index 3f189f7..5454038 100644 --- a/src/main/java/com/ing/data/cassandra/jdbc/CassandraPreparedStatement.java +++ b/src/main/java/com/ing/data/cassandra/jdbc/CassandraPreparedStatement.java @@ -189,7 +189,7 @@ public void close() { try { this.connection.removeStatement(this); } catch (final Exception e) { - LOG.warn("Unable to close the prepared statement: " + e.getMessage()); + LOG.warn("Unable to close the prepared statement: {}", e.getMessage()); } } @@ -198,7 +198,7 @@ private void doExecute() throws SQLException { try { resetResults(); if (LOG.isTraceEnabled() || this.connection.isDebugMode()) { - LOG.trace("CQL: " + this.cql); + LOG.trace("CQL: {}", this.cql); } // Force paging to avoid timeout and node harm. if (this.boundStatement.getPageSize() == 0) { @@ -248,7 +248,7 @@ public int[] executeBatch() throws SQLException { try { final List> futures = new ArrayList<>(); if (LOG.isTraceEnabled() || this.connection.isDebugMode()) { - LOG.trace("CQL statements: " + this.batchStatements.size()); + LOG.trace("CQL statements: {}", this.batchStatements.size()); } for (final BoundStatement statement : this.batchStatements) { for (int i = 0; i < statement.getPreparedStatement().getVariableDefinitions().size(); i++) { @@ -258,7 +258,7 @@ public int[] executeBatch() throws SQLException { } } if (LOG.isTraceEnabled() || this.connection.isDebugMode()) { - LOG.trace("CQL: " + this.cql); + LOG.trace("CQL: {}", this.cql); } final BoundStatement boundStatement = statement.setConsistencyLevel( this.connection.getDefaultConsistencyLevel()); diff --git a/src/main/java/com/ing/data/cassandra/jdbc/CassandraResultSet.java b/src/main/java/com/ing/data/cassandra/jdbc/CassandraResultSet.java index 07edc07..c7e338f 100644 --- a/src/main/java/com/ing/data/cassandra/jdbc/CassandraResultSet.java +++ b/src/main/java/com/ing/data/cassandra/jdbc/CassandraResultSet.java @@ -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 @@ -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)); @@ -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); + 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 @@ -1025,7 +1030,7 @@ public Object getObject(final String columnLabel) throws SQLException { @Override public T getObject(final String columnLabel, final Class type) throws SQLException { final int index = findColumn(columnLabel); - return getObject(index + 1, type); + return getObject(index, type); } @Override @@ -1149,7 +1154,7 @@ public T getObjectFromJson(final int columnIndex, final Class type) throw @Override public T getObjectFromJson(final String columnLabel, final Class type) throws SQLException { final int index = findColumn(columnLabel); - return getObjectFromJson(index + 1, type); + return getObjectFromJson(index, type); } @Override diff --git a/src/main/java/com/ing/data/cassandra/jdbc/CassandraStatement.java b/src/main/java/com/ing/data/cassandra/jdbc/CassandraStatement.java index 1bae7d1..248794c 100644 --- a/src/main/java/com/ing/data/cassandra/jdbc/CassandraStatement.java +++ b/src/main/java/com/ing/data/cassandra/jdbc/CassandraStatement.java @@ -370,7 +370,7 @@ private void doExecute(final String cql) throws SQLException { private com.datastax.oss.driver.api.core.cql.ResultSet executeSingleStatement(final String cql) { if (LOG.isTraceEnabled() || this.connection.isDebugMode()) { - LOG.debug("CQL: " + cql); + LOG.debug("CQL: {}", cql); } SimpleStatement stmt = SimpleStatement.newInstance(cql) .setConsistencyLevel(this.connection.getDefaultConsistencyLevel()) @@ -407,12 +407,12 @@ public int[] executeBatch() throws SQLException { final int[] returnCounts = new int[this.batchQueries.size()]; final List> futures = new ArrayList<>(); if (LOG.isTraceEnabled() || this.connection.isDebugMode()) { - LOG.debug("CQL statements: " + this.batchQueries.size()); + LOG.debug("CQL statements: {}", this.batchQueries.size()); } for (final String query : this.batchQueries) { if (LOG.isTraceEnabled() || this.connection.isDebugMode()) { - LOG.debug("CQL: " + query); + LOG.debug("CQL: {}", query); } SimpleStatement stmt = SimpleStatement.newInstance(query) .setConsistencyLevel(this.connection.getDefaultConsistencyLevel()); diff --git a/src/test/java/com/ing/data/cassandra/jdbc/ConnectionUnitTest.java b/src/test/java/com/ing/data/cassandra/jdbc/ConnectionUnitTest.java index 0d11b2a..579734c 100644 --- a/src/test/java/com/ing/data/cassandra/jdbc/ConnectionUnitTest.java +++ b/src/test/java/com/ing/data/cassandra/jdbc/ConnectionUnitTest.java @@ -77,7 +77,7 @@ import static org.mockito.Mockito.when; class ConnectionUnitTest extends UsingCassandraContainerTest { - private static final Logger log = LoggerFactory.getLogger(ConnectionUnitTest.class); + private static final Logger LOG = LoggerFactory.getLogger(ConnectionUnitTest.class); private static final String KEYSPACE = "system"; @@ -427,15 +427,15 @@ void givenConnection_whenGetMetaData_getExpectedResultSet() throws Exception { assertNotNull(sqlConnection.getMetaData()); final DatabaseMetaData dbMetadata = sqlConnection.getMetaData(); - log.debug("===================================================="); - log.debug("Connection Metadata"); - log.debug("===================================================="); - log.debug("Driver name: {}", dbMetadata.getDriverName()); - log.debug("Driver version: {}", dbMetadata.getDriverVersion()); - log.debug("DB name: {}", dbMetadata.getDatabaseProductName()); - log.debug("DB version: {}", dbMetadata.getDatabaseProductVersion()); - log.debug("JDBC version: {}.{}", dbMetadata.getJDBCMajorVersion(), dbMetadata.getJDBCMinorVersion()); - log.debug("===================================================="); + LOG.debug("===================================================="); + LOG.debug("Connection Metadata"); + LOG.debug("===================================================="); + LOG.debug("Driver name: {}", dbMetadata.getDriverName()); + LOG.debug("Driver version: {}", dbMetadata.getDriverVersion()); + LOG.debug("DB name: {}", dbMetadata.getDatabaseProductName()); + LOG.debug("DB version: {}", dbMetadata.getDatabaseProductVersion()); + LOG.debug("JDBC version: {}.{}", dbMetadata.getJDBCMajorVersion(), dbMetadata.getJDBCMinorVersion()); + LOG.debug("===================================================="); assertEquals("Cassandra JDBC Driver", dbMetadata.getDriverName()); assertNotEquals(0, dbMetadata.getDriverMajorVersion()); diff --git a/src/test/java/com/ing/data/cassandra/jdbc/DbaasAstraIntegrationTest.java b/src/test/java/com/ing/data/cassandra/jdbc/DbaasAstraIntegrationTest.java index 27252f0..f90d804 100644 --- a/src/test/java/com/ing/data/cassandra/jdbc/DbaasAstraIntegrationTest.java +++ b/src/test/java/com/ing/data/cassandra/jdbc/DbaasAstraIntegrationTest.java @@ -40,7 +40,7 @@ @TestMethodOrder(org.junit.jupiter.api.MethodOrderer.OrderAnnotation.class) class DbaasAstraIntegrationTest { - private static final Logger log = LoggerFactory.getLogger(DbaasAstraIntegrationTest.class); + private static final Logger LOG = LoggerFactory.getLogger(DbaasAstraIntegrationTest.class); private static final String ASTRA_DB_TOKEN_ENV_VARIABLE = "ASTRA_DB_APPLICATION_TOKEN"; private static final String ASTRA_DB_TOKEN_PATTERN = "Astra.*"; private static final String DATABASE_NAME = "test_cassandra_jdbc"; @@ -51,13 +51,13 @@ class DbaasAstraIntegrationTest { @BeforeAll static void setupAstra() throws Exception { if (System.getenv(ASTRA_DB_TOKEN_ENV_VARIABLE) != null) { - log.debug("ASTRA_DB_APPLICATION_TOKEN is provided, AstraDB tests are executed."); + LOG.debug("ASTRA_DB_APPLICATION_TOKEN is provided, AstraDB tests are executed."); /* * Devops API Client (create database, resume, delete) */ final AstraDbClient astraDbClient = new AstraDbClient(TestUtils.getAstraToken()); - log.debug("Connected the DBaaS API."); + LOG.debug("Connected the DBaaS API."); /* * Set up a Database in Astra: create if not exist, resume if needed. @@ -67,7 +67,7 @@ static void setupAstra() throws Exception { String dbId = TestUtils.setupVectorDatabase(DATABASE_NAME, KEYSPACE_NAME); Assertions.assertTrue(astraDbClient.findById(dbId).isPresent()); Assertions.assertEquals(DatabaseStatusType.ACTIVE, astraDbClient.findById(dbId).get().getStatus()); - log.debug("Database ready."); + LOG.debug("Database ready."); /* * Download cloud secure bundle to connect to the database. @@ -77,7 +77,7 @@ static void setupAstra() throws Exception { astraDbClient .database(dbId) .downloadDefaultSecureConnectBundle("/tmp/" + DATABASE_NAME + "_scb.zip"); - log.debug("Connection bundle downloaded."); + LOG.debug("Connection bundle downloaded."); /* * Building jdbcUrl and sqlConnection. @@ -90,7 +90,7 @@ static void setupAstra() throws Exception { "&consistency=" + "LOCAL_QUORUM" + "&secureconnectbundle=/tmp/" + DATABASE_NAME + "_scb.zip"); } else { - log.debug("ASTRA_DB_APPLICATION_TOKEN is not defined, skipping AstraDB tests."); + LOG.debug("ASTRA_DB_APPLICATION_TOKEN is not defined, skipping AstraDB tests."); } } diff --git a/src/test/java/com/ing/data/cassandra/jdbc/JdbcRegressionUnitTest.java b/src/test/java/com/ing/data/cassandra/jdbc/JdbcRegressionUnitTest.java index 9ff30c5..cd93aba 100644 --- a/src/test/java/com/ing/data/cassandra/jdbc/JdbcRegressionUnitTest.java +++ b/src/test/java/com/ing/data/cassandra/jdbc/JdbcRegressionUnitTest.java @@ -65,7 +65,6 @@ * original project from GitHub. */ class JdbcRegressionUnitTest extends UsingCassandraContainerTest { - private static final Logger log = LoggerFactory.getLogger(JdbcRegressionUnitTest.class); private static final String KEYSPACE = "test_keyspace3"; private static final String TABLE = "regressions_test"; diff --git a/src/test/java/com/ing/data/cassandra/jdbc/MetadataResultSetsUnitTest.java b/src/test/java/com/ing/data/cassandra/jdbc/MetadataResultSetsUnitTest.java index a2812ab..f2a49db 100644 --- a/src/test/java/com/ing/data/cassandra/jdbc/MetadataResultSetsUnitTest.java +++ b/src/test/java/com/ing/data/cassandra/jdbc/MetadataResultSetsUnitTest.java @@ -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; @@ -47,7 +48,7 @@ class MetadataResultSetsUnitTest extends UsingCassandraContainerTest { - private static final Logger log = LoggerFactory.getLogger(MetadataResultSetsUnitTest.class); + private static final Logger LOG = LoggerFactory.getLogger(MetadataResultSetsUnitTest.class); private static final String KEYSPACE = "test_keyspace"; private static final String ANOTHER_KEYSPACE = "test_keyspace2"; @@ -284,18 +285,18 @@ void givenStatement_whenGetResultMetadata_returnExpectedValues() throws Exceptio assertTrue(result.next()); assertEquals(6, result.getMetaData().getColumnCount()); for (int i = 1; i <= result.getMetaData().getColumnCount(); i++) { - log.debug("getColumnName : " + result.getMetaData().getColumnName(i)); - log.debug("getCatalogName : " + result.getMetaData().getCatalogName(i)); - log.debug("getColumnClassName : " + result.getMetaData().getColumnClassName(i)); - log.debug("getColumnDisplaySize : " + result.getMetaData().getColumnDisplaySize(i)); - log.debug("getColumnLabel : " + result.getMetaData().getColumnLabel(i)); - log.debug("getColumnType : " + result.getMetaData().getColumnType(i)); - log.debug("getColumnTypeName : " + result.getMetaData().getColumnTypeName(i)); - log.debug("getPrecision : " + result.getMetaData().getPrecision(i)); - log.debug("getScale : " + result.getMetaData().getScale(i)); - log.debug("getSchemaName : " + result.getMetaData().getSchemaName(i)); - log.debug("getTableName : " + result.getMetaData().getTableName(i)); - log.debug("=========================="); + LOG.debug("getColumnName : {}", result.getMetaData().getColumnName(i)); + LOG.debug("getCatalogName : {}", result.getMetaData().getCatalogName(i)); + LOG.debug("getColumnClassName : {}", result.getMetaData().getColumnClassName(i)); + LOG.debug("getColumnDisplaySize : {}", result.getMetaData().getColumnDisplaySize(i)); + LOG.debug("getColumnLabel : {}", result.getMetaData().getColumnLabel(i)); + LOG.debug("getColumnType : {}", result.getMetaData().getColumnType(i)); + LOG.debug("getColumnTypeName : {}", result.getMetaData().getColumnTypeName(i)); + LOG.debug("getPrecision : {}", result.getMetaData().getPrecision(i)); + LOG.debug("getScale : {}", result.getMetaData().getScale(i)); + LOG.debug("getSchemaName : {}", result.getMetaData().getSchemaName(i)); + LOG.debug("getTableName : {}", result.getMetaData().getTableName(i)); + LOG.debug("=========================="); } assertEquals("part_key", result.getMetaData().getColumnName(1)); @@ -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 */ diff --git a/src/test/java/com/ing/data/cassandra/jdbc/PreparedStatementsUnitTest.java b/src/test/java/com/ing/data/cassandra/jdbc/PreparedStatementsUnitTest.java index 80997c3..7baad38 100644 --- a/src/test/java/com/ing/data/cassandra/jdbc/PreparedStatementsUnitTest.java +++ b/src/test/java/com/ing/data/cassandra/jdbc/PreparedStatementsUnitTest.java @@ -40,7 +40,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; class PreparedStatementsUnitTest extends UsingCassandraContainerTest { - private static final Logger log = LoggerFactory.getLogger(PreparedStatementsUnitTest.class); private static final String KEYSPACE = "test_prep_stmt"; diff --git a/src/test/java/com/ing/data/cassandra/jdbc/ResultSetUnitTest.java b/src/test/java/com/ing/data/cassandra/jdbc/ResultSetUnitTest.java index 2f816ad..5abcd4a 100644 --- a/src/test/java/com/ing/data/cassandra/jdbc/ResultSetUnitTest.java +++ b/src/test/java/com/ing/data/cassandra/jdbc/ResultSetUnitTest.java @@ -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; @@ -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); diff --git a/src/test/java/com/ing/data/cassandra/jdbc/metadata/AbstractMetadataResultSetBuilderUnitTest.java b/src/test/java/com/ing/data/cassandra/jdbc/metadata/AbstractMetadataResultSetBuilderUnitTest.java index 37620bd..f30ff9e 100644 --- a/src/test/java/com/ing/data/cassandra/jdbc/metadata/AbstractMetadataResultSetBuilderUnitTest.java +++ b/src/test/java/com/ing/data/cassandra/jdbc/metadata/AbstractMetadataResultSetBuilderUnitTest.java @@ -43,7 +43,7 @@ class AbstractMetadataResultSetBuilderUnitTest { - private static final Logger log = LoggerFactory.getLogger(AbstractMetadataResultSetBuilderUnitTest.class); + private static final Logger LOG = LoggerFactory.getLogger(AbstractMetadataResultSetBuilderUnitTest.class); static KeyspaceMetadata generateTestKeyspaceMetadata(final String keyspaceName) { final KeyspaceMetadata mockKeyspaceMetadata = mock(KeyspaceMetadata.class); @@ -87,34 +87,34 @@ void givenSchemaPattern_whenApplySchemaFiltering_returnExpectedResultSet() throw final Set filteredSchemas = new HashSet<>(); sut.filterBySchemaNamePattern(StringUtils.EMPTY, keyspaceMetadata -> filteredSchemas.add(keyspaceMetadata.getName().asInternal()), null); - log.info("Schemas matching '': {}", filteredSchemas); + LOG.info("Schemas matching '': {}", filteredSchemas); assertThat(filteredSchemas, hasSize(4)); assertThat(filteredSchemas, hasItems("ks1", "ks2", "test_ks", "another")); filteredSchemas.clear(); sut.filterBySchemaNamePattern(null, keyspaceMetadata -> filteredSchemas.add(keyspaceMetadata.getName().asInternal()), null); - log.info("Schemas matching null: {}", filteredSchemas); + LOG.info("Schemas matching null: {}", filteredSchemas); assertThat(filteredSchemas, hasSize(4)); assertThat(filteredSchemas, hasItems("ks1", "ks2", "test_ks", "another")); filteredSchemas.clear(); sut.filterBySchemaNamePattern("ks", keyspaceMetadata -> filteredSchemas.add(keyspaceMetadata.getName().asInternal()), null); - log.info("Schemas matching 'ks': {}", filteredSchemas); + LOG.info("Schemas matching 'ks': {}", filteredSchemas); assertThat(filteredSchemas, hasSize(0)); filteredSchemas.clear(); sut.filterBySchemaNamePattern("ks%", keyspaceMetadata -> filteredSchemas.add(keyspaceMetadata.getName().asInternal()), null); - log.info("Schemas matching 'ks%': {}", filteredSchemas); + LOG.info("Schemas matching 'ks%': {}", filteredSchemas); assertThat(filteredSchemas, hasSize(2)); assertThat(filteredSchemas, hasItems("ks1", "ks2")); filteredSchemas.clear(); sut.filterBySchemaNamePattern("%ks%", keyspaceMetadata -> filteredSchemas.add(keyspaceMetadata.getName().asInternal()), null); - log.info("Schemas matching '%ks%': {}", filteredSchemas); + LOG.info("Schemas matching '%ks%': {}", filteredSchemas); assertThat(filteredSchemas, hasSize(3)); assertThat(filteredSchemas, hasItems("ks1", "ks2", "test_ks")); } @@ -138,33 +138,33 @@ void givenTablePattern_whenApplyTableFiltering_returnExpectedResultSet() throws final Set filteredTables = new HashSet<>(); sut.filterByTableNamePattern(StringUtils.EMPTY, ksTestMetadata, tableMetadata -> filteredTables.add(tableMetadata.getName().asInternal()), null); - log.info("Tables matching '': {}", filteredTables); + LOG.info("Tables matching '': {}", filteredTables); assertThat(filteredTables, empty()); filteredTables.clear(); sut.filterByTableNamePattern(null, ksTestMetadata, tableMetadata -> filteredTables.add(tableMetadata.getName().asInternal()), null); - log.info("Tables matching null: {}", filteredTables); + LOG.info("Tables matching null: {}", filteredTables); assertThat(filteredTables, hasSize(4)); assertThat(filteredTables, hasItems("cf1", "cf2", "another_table", "test_cf")); filteredTables.clear(); sut.filterByTableNamePattern("cf", ksTestMetadata, tableMetadata -> filteredTables.add(tableMetadata.getName().asInternal()), null); - log.info("Tables matching 'cf': {}", filteredTables); + LOG.info("Tables matching 'cf': {}", filteredTables); assertThat(filteredTables, empty()); filteredTables.clear(); sut.filterByTableNamePattern("cf%", ksTestMetadata, tableMetadata -> filteredTables.add(tableMetadata.getName().asInternal()), null); - log.info("Tables matching 'cf%': {}", filteredTables); + LOG.info("Tables matching 'cf%': {}", filteredTables); assertThat(filteredTables, hasSize(2)); assertThat(filteredTables, hasItems("cf1", "cf2")); filteredTables.clear(); sut.filterByTableNamePattern("%cf%", ksTestMetadata, tableMetadata -> filteredTables.add(tableMetadata.getName().asInternal()), null); - log.info("Tables matching '%cf%': {}", filteredTables); + LOG.info("Tables matching '%cf%': {}", filteredTables); assertThat(filteredTables, hasSize(3)); assertThat(filteredTables, hasItems("cf1", "cf2", "test_cf")); } @@ -195,33 +195,33 @@ void givenColumnPattern_whenApplyColumnFiltering_returnExpectedResultSet() throw final Set filteredColumns = new HashSet<>(); sut.filterByColumnNamePattern(StringUtils.EMPTY, tableTestMetadata, columnMetadata -> filteredColumns.add(columnMetadata.getName().asInternal()), null); - log.info("Columns matching '': {}", filteredColumns); + LOG.info("Columns matching '': {}", filteredColumns); assertThat(filteredColumns, empty()); filteredColumns.clear(); sut.filterByColumnNamePattern(null, tableTestMetadata, tableMetadata -> filteredColumns.add(tableMetadata.getName().asInternal()), null); - log.info("Columns matching null: {}", filteredColumns); + LOG.info("Columns matching null: {}", filteredColumns); assertThat(filteredColumns, hasSize(4)); assertThat(filteredColumns, hasItems("col1", "col2", "clmn_test", "test_col")); filteredColumns.clear(); sut.filterByColumnNamePattern("col", tableTestMetadata, tableMetadata -> filteredColumns.add(tableMetadata.getName().asInternal()), null); - log.info("Columns matching 'col': {}", filteredColumns); + LOG.info("Columns matching 'col': {}", filteredColumns); assertThat(filteredColumns, empty()); filteredColumns.clear(); sut.filterByColumnNamePattern("col%", tableTestMetadata, tableMetadata -> filteredColumns.add(tableMetadata.getName().asInternal()), null); - log.info("Columns matching 'col%': {}", filteredColumns); + LOG.info("Columns matching 'col%': {}", filteredColumns); assertThat(filteredColumns, hasSize(2)); assertThat(filteredColumns, hasItems("col1", "col2")); filteredColumns.clear(); sut.filterByColumnNamePattern("%col%", tableTestMetadata, tableMetadata -> filteredColumns.add(tableMetadata.getName().asInternal()), null); - log.info("Columns matching '%col%': {}", filteredColumns); + LOG.info("Columns matching '%col%': {}", filteredColumns); assertThat(filteredColumns, hasSize(3)); assertThat(filteredColumns, hasItems("col1", "col2", "test_col")); } @@ -249,33 +249,33 @@ void givenFunctionNamePattern_whenApplyFunctionFiltering_returnExpectedResultSet final Set filteredFunctions = new HashSet<>(); sut.filterByFunctionNamePattern(StringUtils.EMPTY, ksTestMetadata, (signature, functionMetadata) -> filteredFunctions.add(signature.getName().asInternal())); - log.info("Functions matching '': {}", filteredFunctions); + LOG.info("Functions matching '': {}", filteredFunctions); assertThat(filteredFunctions, empty()); filteredFunctions.clear(); sut.filterByFunctionNamePattern(null, ksTestMetadata, (signature, functionMetadata) -> filteredFunctions.add(signature.getName().asInternal())); - log.info("Functions matching null: {}", filteredFunctions); + LOG.info("Functions matching null: {}", filteredFunctions); assertThat(filteredFunctions, hasSize(4)); assertThat(filteredFunctions, hasItems("func1", "func2", "another_function", "another_test")); filteredFunctions.clear(); sut.filterByFunctionNamePattern("func", ksTestMetadata, (signature, functionMetadata) -> filteredFunctions.add(signature.getName().asInternal())); - log.info("Functions matching 'func': {}", filteredFunctions); + LOG.info("Functions matching 'func': {}", filteredFunctions); assertThat(filteredFunctions, empty()); filteredFunctions.clear(); sut.filterByFunctionNamePattern("func%", ksTestMetadata, (signature, functionMetadata) -> filteredFunctions.add(signature.getName().asInternal())); - log.info("Functions matching 'func%': {}", filteredFunctions); + LOG.info("Functions matching 'func%': {}", filteredFunctions); assertThat(filteredFunctions, hasSize(2)); assertThat(filteredFunctions, hasItems("func1", "func2")); filteredFunctions.clear(); sut.filterByFunctionNamePattern("%func%", ksTestMetadata, (signature, functionMetadata) -> filteredFunctions.add(signature.getName().asInternal())); - log.info("Functions matching '%func%': {}", filteredFunctions); + LOG.info("Functions matching '%func%': {}", filteredFunctions); assertThat(filteredFunctions, hasSize(3)); assertThat(filteredFunctions, hasItems("func1", "func2", "another_function")); } diff --git a/src/test/resources/initEmbeddedCassandra.cql b/src/test/resources/initEmbeddedCassandra.cql index 044bc24..0a8d1a6 100644 --- a/src/test/resources/initEmbeddedCassandra.cql +++ b/src/test/resources/initEmbeddedCassandra.cql @@ -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,