From eae126ac1c205b31b0841ec3a649b2bd56b29559 Mon Sep 17 00:00:00 2001 From: Joel Bernstein Date: Wed, 20 Jan 2016 02:12:30 +0000 Subject: [PATCH] SOLR-8514, SOLR-8502: Implement StatementImpl.execute(String sql), StatementImpl.getResultSet(), and StatementImpl.getUpdateCount() git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1725662 13f79535-47bb-0310-9956-ffa450edef68 --- .../client/solrj/io/sql/StatementImpl.java | 31 ++++++++++++++--- .../solr/client/solrj/io/sql/JdbcTest.java | 34 +++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --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.java index 5d9acba9eb1..762f74c65b6 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 @@ -45,6 +45,8 @@ class StatementImpl implements Statement { private final ConnectionImpl connection; private SolrStream solrStream; private boolean closed; + private String currentSQL; + private ResultSetImpl currentResultSet; StatementImpl(ConnectionImpl connection) { this.connection = connection; @@ -56,14 +58,22 @@ class StatementImpl implements Statement { @Override public ResultSet executeQuery(String sql) throws SQLException { + try { + if(this.currentResultSet != null) { + this.currentResultSet.close(); + this.currentResultSet = null; + this.solrStream.close(); + } + closed = false; // If closed reopen so Statement can be reused. this.solrStream = constructStream(sql); StreamContext context = new StreamContext(); context.setSolrClientCache(this.connection.getSolrClientCache()); this.solrStream.setStreamContext(context); this.solrStream.open(); - return new ResultSetImpl(this); + this.currentResultSet = new ResultSetImpl(this); + return this.currentResultSet; } catch(Exception e) { throw new SQLException(e); } @@ -183,17 +193,30 @@ class StatementImpl implements Statement { @Override public boolean execute(String sql) throws SQLException { - throw new UnsupportedOperationException(); + + if(this.currentResultSet != null) { + this.currentResultSet.close(); + this.currentResultSet = null; + } + + // TODO Add logic when update statements are added to JDBC. + this.currentSQL = sql; + return true; } @Override public ResultSet getResultSet() throws SQLException { - throw new UnsupportedOperationException(); + return this.executeQuery(this.currentSQL); } @Override public int getUpdateCount() throws SQLException { - throw new UnsupportedOperationException(); + if(isClosed()) { + throw new SQLException("Statement is closed"); + } + + // TODO Add logic when update statements are added to JDBC. + return -1; } @Override diff --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.java index 98d32e9704f..7e76ce0be4d 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 @@ -22,6 +22,7 @@ import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; +import java.sql.ResultSetMetaData; import java.sql.Statement; import java.util.Properties; @@ -324,8 +325,41 @@ public class JdbcTest extends AbstractFullDistribZkTestBase { try (ResultSet rs = statement.executeQuery(sql)) { assertEquals(statement, rs.getStatement()); + + checkResultSetMetadata(rs); + checkResultSet(rs); + } + + assertTrue(statement.execute(sql)); + assertEquals(-1, statement.getUpdateCount()); + + try (ResultSet rs = statement.getResultSet()) { + assertEquals(statement, rs.getStatement()); + + checkResultSetMetadata(rs); + checkResultSet(rs); } } } } + + private void checkResultSetMetadata(ResultSet rs) throws Exception { + ResultSetMetaData resultSetMetaData = rs.getMetaData(); + + assertNotNull(resultSetMetaData); + } + + private void checkResultSet(ResultSet rs) throws Exception { + assertTrue(rs.next()); + assertEquals(14, rs.getLong("a_i")); + assertEquals("hello0", rs.getString("a_s")); + assertEquals(10, rs.getDouble("a_f"), 0); + + assertTrue(rs.next()); + assertEquals(13, rs.getLong("a_i")); + assertEquals("hello3", rs.getString("a_s")); + assertEquals(9, rs.getDouble("a_f"), 0); + + assertFalse(rs.next()); + } }