Apache Lucene open-source search software
SOLR-9020: Implement StatementImpl/ResultSetImpl get/set fetch* methods and proper errors for traversal methods
Kevin Risdena9a842f05dce
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 8630ea4a66c..31ae74c9b87 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -109,6 +109,8 @@ New Features* SOLR-8809: Implement Connection.prepareStatement (Kevin Risden)+* SOLR-9020: Implement StatementImpl/ResultSetImpl get/set fetch* methods and proper errors for traversal methods (Kevin Risden)+Bug Fixes----------------------diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ResultSetImpl.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ResultSetImpl.javaindex e4dcaed8200..91b99b9b837 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ResultSetImpl.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ResultSetImpl.java@@ -31,6 +31,7 @@ import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.RowId;import java.sql.SQLException;+import java.sql.SQLFeatureNotSupportedException;import java.sql.SQLWarning;import java.sql.SQLXML;import java.sql.Statement;@@ -480,92 +481,133 @@ class ResultSetImpl implements ResultSet {@Overridepublic boolean isBeforeFirst() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ throw new SQLFeatureNotSupportedException();}@Overridepublic boolean isAfterLast() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ throw new SQLFeatureNotSupportedException();}@Overridepublic boolean isFirst() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ throw new SQLFeatureNotSupportedException();}@Overridepublic boolean isLast() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ throw new SQLFeatureNotSupportedException();}@Overridepublic void beforeFirst() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ throw new SQLException("beforeFirst() not supported on ResultSet with type TYPE_FORWARD_ONLY");}@Overridepublic void afterLast() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ throw new SQLException("afterLast() not supported on ResultSet with type TYPE_FORWARD_ONLY");}@Overridepublic boolean first() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ throw new SQLException("first() not supported on ResultSet with type TYPE_FORWARD_ONLY");}@Overridepublic boolean last() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ throw new SQLException("last() not supported on ResultSet with type TYPE_FORWARD_ONLY");}@Overridepublic int getRow() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ throw new SQLFeatureNotSupportedException();}@Overridepublic boolean absolute(int row) throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ throw new SQLException("absolute() not supported on ResultSet with type TYPE_FORWARD_ONLY");}@Overridepublic boolean relative(int rows) throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ throw new SQLException("relative() not supported on ResultSet with type TYPE_FORWARD_ONLY");}@Overridepublic boolean previous() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ throw new SQLException("previous() not supported on ResultSet with type TYPE_FORWARD_ONLY");}@Overridepublic void setFetchDirection(int direction) throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ if(direction != ResultSet.FETCH_FORWARD) {+ throw new SQLException("Direction must be FETCH_FORWARD since ResultSet " ++ "type is TYPE_FORWARD_ONLY");+ }}@Overridepublic int getFetchDirection() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ return ResultSet.FETCH_FORWARD;}@Overridepublic void setFetchSize(int rows) throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ if(rows < 0) {+ throw new SQLException("Rows must be >= 0");+ }}@Overridepublic int getFetchSize() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ return 0;}@Overridepublic int getType() throws SQLException {+ checkClosed();+return ResultSet.TYPE_FORWARD_ONLY;}@Overridepublic int getConcurrency() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ return ResultSet.CONCUR_READ_ONLY;}@Overridediff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/StatementImpl.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/StatementImpl.javaindex 24b20a40f84..1b1200d2842 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/StatementImpl.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/StatementImpl.java@@ -51,6 +51,12 @@ class StatementImpl implements Statement {this.connection = connection;}+ private void checkClosed() throws SQLException {+ if(isClosed()) {+ throw new SQLException("Statement is closed.");+ }+ }+private ResultSet executeQueryImpl(String sql) throws SQLException {try {if(this.currentResultSet != null) {@@ -171,18 +177,14 @@ class StatementImpl implements Statement {@Overridepublic SQLWarning getWarnings() throws SQLException {- if(isClosed()) {- throw new SQLException("Statement is closed.");- }+ checkClosed();return this.currentWarning;}@Overridepublic void clearWarnings() throws SQLException {- if(isClosed()) {- throw new SQLException("Statement is closed.");- }+ checkClosed();this.currentWarning = null;}@@ -212,9 +214,7 @@ class StatementImpl implements Statement {@Overridepublic int getUpdateCount() throws SQLException {- if(isClosed()) {- throw new SQLException("Statement is closed");- }+ checkClosed();// TODO Add logic when update statements are added to JDBC.return -1;@@ -222,9 +222,7 @@ class StatementImpl implements Statement {@Overridepublic boolean getMoreResults() throws SQLException {- if(isClosed()) {- throw new SQLException("Statement is closed");- }+ checkClosed();// Currently multiple result sets are not possible yetthis.currentResultSet.close();@@ -233,32 +231,48 @@ class StatementImpl implements Statement {@Overridepublic void setFetchDirection(int direction) throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ if(direction != ResultSet.FETCH_FORWARD) {+ throw new SQLException("Direction must be ResultSet.FETCH_FORWARD currently");+ }}@Overridepublic int getFetchDirection() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ return ResultSet.FETCH_FORWARD;}@Overridepublic void setFetchSize(int rows) throws SQLException {+ checkClosed();+ if(rows < 0) {+ throw new SQLException("Rows must be >= 0");+ }}@Overridepublic int getFetchSize() throws SQLException {+ checkClosed();+return 0;}@Overridepublic int getResultSetConcurrency() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ return ResultSet.CONCUR_READ_ONLY;}@Overridepublic int getResultSetType() throws SQLException {- throw new UnsupportedOperationException();+ checkClosed();++ return ResultSet.TYPE_FORWARD_ONLY;}@Overridediff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.javaindex 40d14a113a0..90500927f26 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java@@ -501,16 +501,9 @@ public class JdbcTest extends AbstractFullDistribZkTestBase {con.clearWarnings();assertNull(con.getWarnings());- try (Statement statement = con.createStatement()) {- assertEquals(con, statement.getConnection());-- assertNull(statement.getWarnings());- statement.clearWarnings();- assertNull(statement.getWarnings());- assertEquals(0, statement.getFetchSize());- statement.setFetchSize(0);- assertEquals(0, statement.getFetchSize());+ try (Statement statement = con.createStatement()) {+ checkStatement(con, statement);try (ResultSet rs = statement.executeQuery(sql)) {assertEquals(statement, rs.getStatement());@@ -533,15 +526,7 @@ public class JdbcTest extends AbstractFullDistribZkTestBase {}try (PreparedStatement statement = con.prepareStatement(sql)) {- assertEquals(con, statement.getConnection());-- assertNull(statement.getWarnings());- statement.clearWarnings();- assertNull(statement.getWarnings());-- assertEquals(0, statement.getFetchSize());- statement.setFetchSize(0);- assertEquals(0, statement.getFetchSize());+ checkStatement(con, statement);try (ResultSet rs = statement.executeQuery()) {assertEquals(statement, rs.getStatement());@@ -565,6 +550,25 @@ public class JdbcTest extends AbstractFullDistribZkTestBase {}}+ private void checkStatement(Connection con, Statement statement) throws Exception {+ assertEquals(con, statement.getConnection());++ assertNull(statement.getWarnings());+ statement.clearWarnings();+ assertNull(statement.getWarnings());++ assertEquals(ResultSet.TYPE_FORWARD_ONLY, statement.getResultSetType());+ assertEquals(ResultSet.CONCUR_READ_ONLY, statement.getResultSetConcurrency());++ assertEquals(ResultSet.FETCH_FORWARD, statement.getFetchDirection());+ statement.setFetchDirection(ResultSet.FETCH_FORWARD);+ assertEquals(ResultSet.FETCH_FORWARD, statement.getFetchDirection());++ assertEquals(0, statement.getFetchSize());+ statement.setFetchSize(0);+ assertEquals(0, statement.getFetchSize());+ }+private void checkResultSetMetadata(ResultSet rs) throws Exception {ResultSetMetaData resultSetMetaData = rs.getMetaData();@@ -604,12 +608,21 @@ public class JdbcTest extends AbstractFullDistribZkTestBase {}private void checkResultSet(ResultSet rs) throws Exception {- assertEquals(ResultSet.TYPE_FORWARD_ONLY, rs.getType());-assertNull(rs.getWarnings());rs.clearWarnings();assertNull(rs.getWarnings());+ assertEquals(ResultSet.TYPE_FORWARD_ONLY, rs.getType());+ assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());++ assertEquals(ResultSet.FETCH_FORWARD, rs.getFetchDirection());+ rs.setFetchDirection(ResultSet.FETCH_FORWARD);+ assertEquals(ResultSet.FETCH_FORWARD, rs.getFetchDirection());++ assertEquals(0, rs.getFetchSize());+ rs.setFetchSize(10);+ assertEquals(0, rs.getFetchSize());+assertTrue(rs.next());assertEquals(14L, rs.getObject("a_i"));