From 3654fc33b9e2ac0066d03ae864dea418d27f8841 Mon Sep 17 00:00:00 2001 From: jbernste Date: Tue, 26 Jan 2016 10:55:32 -0500 Subject: [PATCH] SOLR-8518: Implement ResultSetMetaDataImpl getColumnType and getColumnTypeName --- .../client/solrj/io/sql/ResultSetImpl.java | 18 ++++++++++-- .../solrj/io/sql/ResultSetMetaDataImpl.java | 29 +++++++++++++++++-- .../client/solrj/io/sql/StatementImpl.java | 1 - .../solr/client/solrj/io/sql/JdbcTest.java | 11 +++++++ 4 files changed, 52 insertions(+), 7 deletions(-) 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.java index d1edc0c82f5..31fc39fff36 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 @@ -41,13 +41,14 @@ import java.util.Calendar; import java.util.Map; import org.apache.solr.client.solrj.io.Tuple; -import org.apache.solr.client.solrj.io.stream.SolrStream; +import org.apache.solr.client.solrj.io.stream.PushBackStream; class ResultSetImpl implements ResultSet { private final StatementImpl statement; - private final SolrStream solrStream; + private final PushBackStream solrStream; private final ResultSetMetaData resultSetMetaData; private final Tuple metadataTuple; + private final Tuple firstTuple; private Tuple tuple; private boolean done; private boolean closed; @@ -55,7 +56,7 @@ class ResultSetImpl implements ResultSet { ResultSetImpl(StatementImpl statement) { this.statement = statement; - this.solrStream = statement.getSolrStream(); + this.solrStream = new PushBackStream(statement.getSolrStream()); // Read the first tuple so that metadata can be gathered try { @@ -69,6 +70,13 @@ class ResultSetImpl implements ResultSet { throw new RuntimeException("Couldn't get metadata tuple"); } + try { + this.firstTuple = this.solrStream.read(); + this.solrStream.pushBack(firstTuple); + } catch (IOException e) { + throw new RuntimeException("Couldn't get first tuple."); + } + this.resultSetMetaData = new ResultSetMetaDataImpl(this); } @@ -76,6 +84,10 @@ class ResultSetImpl implements ResultSet { return this.metadataTuple; } + Tuple getFirstTuple() { + return this.firstTuple; + } + private void checkClosed() throws SQLException { if(isClosed()) { throw new SQLException("ResultSet is closed."); diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ResultSetMetaDataImpl.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ResultSetMetaDataImpl.java index 8351140d622..d59db8753ff 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ResultSetMetaDataImpl.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ResultSetMetaDataImpl.java @@ -19,6 +19,7 @@ package org.apache.solr.client.solrj.io.sql; import java.sql.ResultSetMetaData; import java.sql.SQLException; +import java.sql.Types; import java.util.List; import org.apache.solr.client.solrj.io.Tuple; @@ -26,10 +27,21 @@ import org.apache.solr.client.solrj.io.Tuple; class ResultSetMetaDataImpl implements ResultSetMetaData { private final ResultSetImpl resultSet; private final Tuple metadataTuple; + private final Tuple firstTuple; ResultSetMetaDataImpl(ResultSetImpl resultSet) { this.resultSet = resultSet; this.metadataTuple = this.resultSet.getMetadataTuple(); + this.firstTuple = this.resultSet.getFirstTuple(); + } + + private Class getColumnClass(int column) throws SQLException { + Object o = this.firstTuple.get(this.getColumnName(column)); + if(o == null) { + return String.class; //Nulls will only be present with Strings. + } else { + return o.getClass(); + } } @Override @@ -117,12 +129,23 @@ class ResultSetMetaDataImpl implements ResultSetMetaData { @Override public int getColumnType(int column) throws SQLException { - return 0; + switch (getColumnTypeName(column)) { + case "String": + return Types.VARCHAR; + case "Integer": + return Types.INTEGER; + case "Long": + return Types.DOUBLE; + case "Double": + return Types.DOUBLE; + default: + return Types.JAVA_OBJECT; + } } @Override public String getColumnTypeName(int column) throws SQLException { - return null; + return this.getColumnClass(column).getSimpleName(); } @Override @@ -142,7 +165,7 @@ class ResultSetMetaDataImpl implements ResultSetMetaData { @Override public String getColumnClassName(int column) throws SQLException { - return null; + return this.getColumnClass(column).getTypeName(); } @Override 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 bea6488efcb..1c1bc2166da 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 @@ -59,7 +59,6 @@ class StatementImpl implements Statement { @Override public ResultSet executeQuery(String sql) throws SQLException { - try { if(this.currentResultSet != null) { this.currentResultSet.close(); 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 617399b20e8..9fe7226e4f2 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 @@ -24,6 +24,7 @@ import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; +import java.sql.Types; import java.util.Properties; import org.apache.lucene.util.LuceneTestCase; @@ -403,6 +404,16 @@ public class JdbcTest extends AbstractFullDistribZkTestBase { assertNotNull(resultSetMetaData); assertEquals(4, resultSetMetaData.getColumnCount()); + + assertEquals("Long", resultSetMetaData.getColumnTypeName(1)); + assertEquals("Long", resultSetMetaData.getColumnTypeName(2)); + assertEquals("String", resultSetMetaData.getColumnTypeName(3)); + assertEquals("Double", resultSetMetaData.getColumnTypeName(4)); + + assertEquals(Types.DOUBLE, resultSetMetaData.getColumnType(1)); + assertEquals(Types.DOUBLE, resultSetMetaData.getColumnType(2)); + assertEquals(Types.VARCHAR, resultSetMetaData.getColumnType(3)); + assertEquals(Types.DOUBLE, resultSetMetaData.getColumnType(4)); } private void checkResultSet(ResultSet rs) throws Exception {