SOLR-8518: Implement ResultSetMetaDataImpl getColumnType and getColumnTypeName

This commit is contained in:
jbernste 2016-01-26 10:55:32 -05:00
parent 0ff8d11367
commit 3654fc33b9
4 changed files with 52 additions and 7 deletions

View File

@ -41,13 +41,14 @@ import java.util.Calendar;
import java.util.Map; import java.util.Map;
import org.apache.solr.client.solrj.io.Tuple; 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 { class ResultSetImpl implements ResultSet {
private final StatementImpl statement; private final StatementImpl statement;
private final SolrStream solrStream; private final PushBackStream solrStream;
private final ResultSetMetaData resultSetMetaData; private final ResultSetMetaData resultSetMetaData;
private final Tuple metadataTuple; private final Tuple metadataTuple;
private final Tuple firstTuple;
private Tuple tuple; private Tuple tuple;
private boolean done; private boolean done;
private boolean closed; private boolean closed;
@ -55,7 +56,7 @@ class ResultSetImpl implements ResultSet {
ResultSetImpl(StatementImpl statement) { ResultSetImpl(StatementImpl statement) {
this.statement = statement; this.statement = statement;
this.solrStream = statement.getSolrStream(); this.solrStream = new PushBackStream(statement.getSolrStream());
// Read the first tuple so that metadata can be gathered // Read the first tuple so that metadata can be gathered
try { try {
@ -69,6 +70,13 @@ class ResultSetImpl implements ResultSet {
throw new RuntimeException("Couldn't get metadata tuple"); 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); this.resultSetMetaData = new ResultSetMetaDataImpl(this);
} }
@ -76,6 +84,10 @@ class ResultSetImpl implements ResultSet {
return this.metadataTuple; return this.metadataTuple;
} }
Tuple getFirstTuple() {
return this.firstTuple;
}
private void checkClosed() throws SQLException { private void checkClosed() throws SQLException {
if(isClosed()) { if(isClosed()) {
throw new SQLException("ResultSet is closed."); throw new SQLException("ResultSet is closed.");

View File

@ -19,6 +19,7 @@ package org.apache.solr.client.solrj.io.sql;
import java.sql.ResultSetMetaData; import java.sql.ResultSetMetaData;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Types;
import java.util.List; import java.util.List;
import org.apache.solr.client.solrj.io.Tuple; import org.apache.solr.client.solrj.io.Tuple;
@ -26,10 +27,21 @@ import org.apache.solr.client.solrj.io.Tuple;
class ResultSetMetaDataImpl implements ResultSetMetaData { class ResultSetMetaDataImpl implements ResultSetMetaData {
private final ResultSetImpl resultSet; private final ResultSetImpl resultSet;
private final Tuple metadataTuple; private final Tuple metadataTuple;
private final Tuple firstTuple;
ResultSetMetaDataImpl(ResultSetImpl resultSet) { ResultSetMetaDataImpl(ResultSetImpl resultSet) {
this.resultSet = resultSet; this.resultSet = resultSet;
this.metadataTuple = this.resultSet.getMetadataTuple(); 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 @Override
@ -117,12 +129,23 @@ class ResultSetMetaDataImpl implements ResultSetMetaData {
@Override @Override
public int getColumnType(int column) throws SQLException { 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 @Override
public String getColumnTypeName(int column) throws SQLException { public String getColumnTypeName(int column) throws SQLException {
return null; return this.getColumnClass(column).getSimpleName();
} }
@Override @Override
@ -142,7 +165,7 @@ class ResultSetMetaDataImpl implements ResultSetMetaData {
@Override @Override
public String getColumnClassName(int column) throws SQLException { public String getColumnClassName(int column) throws SQLException {
return null; return this.getColumnClass(column).getTypeName();
} }
@Override @Override

View File

@ -59,7 +59,6 @@ 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) { if(this.currentResultSet != null) {
this.currentResultSet.close(); this.currentResultSet.close();

View File

@ -24,6 +24,7 @@ import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.ResultSetMetaData; import java.sql.ResultSetMetaData;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Types;
import java.util.Properties; import java.util.Properties;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
@ -403,6 +404,16 @@ public class JdbcTest extends AbstractFullDistribZkTestBase {
assertNotNull(resultSetMetaData); assertNotNull(resultSetMetaData);
assertEquals(4, resultSetMetaData.getColumnCount()); 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 { private void checkResultSet(ResultSet rs) throws Exception {