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
This commit is contained in:
Joel Bernstein 2016-01-20 02:12:30 +00:00
parent 2d2582e4f4
commit eae126ac1c
2 changed files with 61 additions and 4 deletions

View File

@ -45,6 +45,8 @@ class StatementImpl implements Statement {
private final ConnectionImpl connection; private final ConnectionImpl connection;
private SolrStream solrStream; private SolrStream solrStream;
private boolean closed; private boolean closed;
private String currentSQL;
private ResultSetImpl currentResultSet;
StatementImpl(ConnectionImpl connection) { StatementImpl(ConnectionImpl connection) {
this.connection = connection; this.connection = connection;
@ -56,14 +58,22 @@ class StatementImpl implements Statement {
@Override @Override
public ResultSet executeQuery(String sql) throws SQLException { public ResultSet executeQuery(String sql) throws SQLException {
try { try {
if(this.currentResultSet != null) {
this.currentResultSet.close();
this.currentResultSet = null;
this.solrStream.close();
}
closed = false; // If closed reopen so Statement can be reused. closed = false; // If closed reopen so Statement can be reused.
this.solrStream = constructStream(sql); this.solrStream = constructStream(sql);
StreamContext context = new StreamContext(); StreamContext context = new StreamContext();
context.setSolrClientCache(this.connection.getSolrClientCache()); context.setSolrClientCache(this.connection.getSolrClientCache());
this.solrStream.setStreamContext(context); this.solrStream.setStreamContext(context);
this.solrStream.open(); this.solrStream.open();
return new ResultSetImpl(this); this.currentResultSet = new ResultSetImpl(this);
return this.currentResultSet;
} catch(Exception e) { } catch(Exception e) {
throw new SQLException(e); throw new SQLException(e);
} }
@ -183,17 +193,30 @@ class StatementImpl implements Statement {
@Override @Override
public boolean execute(String sql) throws SQLException { 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 @Override
public ResultSet getResultSet() throws SQLException { public ResultSet getResultSet() throws SQLException {
throw new UnsupportedOperationException(); return this.executeQuery(this.currentSQL);
} }
@Override @Override
public int getUpdateCount() throws SQLException { 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 @Override

View File

@ -22,6 +22,7 @@ import java.sql.Connection;
import java.sql.DatabaseMetaData; import java.sql.DatabaseMetaData;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement; import java.sql.Statement;
import java.util.Properties; import java.util.Properties;
@ -324,8 +325,41 @@ public class JdbcTest extends AbstractFullDistribZkTestBase {
try (ResultSet rs = statement.executeQuery(sql)) { try (ResultSet rs = statement.executeQuery(sql)) {
assertEquals(statement, rs.getStatement()); 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());
}
} }