Skip to content

Commit

Permalink
Implement some functions in JDBC driver
Browse files Browse the repository at this point in the history
  • Loading branch information
nileema committed Jan 7, 2014
1 parent 47082a4 commit 8ed7041
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -916,14 +916,18 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
public ResultSet getSchemas()
throws SQLException
{
throw new UnsupportedOperationException("getSchemas");
return select("" +
"SELECT schema_name TABLE_SCHEM, catalog_name TABLE_CATALOG " +
"FROM default.information_schema.schemata");
}

@Override
public ResultSet getCatalogs()
throws SQLException
{
throw new UnsupportedOperationException("getCatalogs");
return select("" +
"SELECT catalog_name TABLE_CAT " +
"FROM default.sys.catalog");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public class PrestoResultSet
{
private static final int VARIABLE_BINARY_MAX = 1024 * 1024 * 1024;

private final AtomicBoolean closed = new AtomicBoolean();
private final StatementClient client;
private final Iterator<List<Object>> results;
private final Map<String, Integer> fieldMap;
Expand Down Expand Up @@ -1124,7 +1123,7 @@ public int getHoldability()
public boolean isClosed()
throws SQLException
{
return closed.get();
return client.isClosed();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class PrestoStatement
private final AtomicBoolean escapeProcessing = new AtomicBoolean(true);
private final AtomicBoolean closeOnCompletion = new AtomicBoolean();
private final AtomicReference<PrestoConnection> connection;
private AtomicReference<ResultSet> currentResult = new AtomicReference<>();

PrestoStatement(PrestoConnection connection)
{
Expand All @@ -45,7 +46,9 @@ public ResultSet executeQuery(String sql)
throws SQLException
{
try {
return new PrestoResultSet(connection().startQuery(sql));
ResultSet result = new PrestoResultSet(connection().startQuery(sql));
currentResult.set(result);
return result;
}
catch (RuntimeException e) {
throw new SQLException("Error executing query", e);
Expand Down Expand Up @@ -167,28 +170,36 @@ public void setCursorName(String name)
public boolean execute(String sql)
throws SQLException
{
throw new UnsupportedOperationException("execute");
checkOpen();
// Only support returning a single result set
currentResult.set(executeQuery(sql));
return true;
}

@Override
public ResultSet getResultSet()
throws SQLException
{
throw new UnsupportedOperationException("getResultSet");
checkOpen();
return currentResult.get();
}

@Override
public int getUpdateCount()
throws SQLException
{
throw new UnsupportedOperationException("getUpdateCount");
checkOpen();
// Updates are not allowed yet so return -1
return -1;
}

@Override
public boolean getMoreResults()
throws SQLException
{
throw new UnsupportedOperationException("getMoreResults");
checkOpen();
currentResult.get().close();
return false;
}

@Override
Expand Down Expand Up @@ -280,7 +291,18 @@ public Connection getConnection()
public boolean getMoreResults(int current)
throws SQLException
{
throw new UnsupportedOperationException("getMoreResults");
checkOpen();

if (current == CLOSE_CURRENT_RESULT) {
currentResult.get().close();
return false;
}

if (current != KEEP_CURRENT_RESULT && current != CLOSE_ALL_RESULTS) {
throw new SQLException("Invalid argument: " + current);
}

throw new SQLFeatureNotSupportedException("Multiple open results not supported");
}

@Override
Expand Down
120 changes: 119 additions & 1 deletion presto-jdbc/src/test/java/com/facebook/presto/jdbc/TestDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
Expand All @@ -31,6 +32,7 @@
import static java.lang.String.format;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

Expand Down Expand Up @@ -61,7 +63,7 @@ public void testDriverManager()
}

try (Statement statement = connection.createStatement()) {
try (ResultSet rs = statement.executeQuery("SELECT 123 x, 'foo' y FROM dual")) {
try (ResultSet rs = statement.executeQuery("SELECT 123 x, 'foo' y")) {
ResultSetMetaData metadata = rs.getMetaData();

assertEquals(metadata.getColumnCount(), 2);
Expand All @@ -84,6 +86,122 @@ public void testDriverManager()
}
}

@Test
public void testGetCatalogs()
throws Exception
{
try (Connection connection = createConnection()) {
try (ResultSet rs = connection.getMetaData().getCatalogs()) {
assertRowCount(rs, 2);
ResultSetMetaData metadata = rs.getMetaData();
assertEquals(metadata.getColumnCount(), 1);
assertEquals(metadata.getColumnLabel(1), "TABLE_CAT");
assertEquals(metadata.getColumnType(1), Types.LONGNVARCHAR);
}
}
}

@Test
public void testGetSchemas()
throws Exception
{
try (Connection connection = createConnection()) {
try (ResultSet rs = connection.getMetaData().getSchemas()) {
assertRowCount(rs, 2);

ResultSetMetaData metadata = rs.getMetaData();
assertEquals(metadata.getColumnCount(), 2);

assertEquals(metadata.getColumnLabel(1), "TABLE_SCHEM");
assertEquals(metadata.getColumnType(1), Types.LONGNVARCHAR);

assertEquals(metadata.getColumnLabel(2), "TABLE_CATALOG");
assertEquals(metadata.getColumnType(2), Types.LONGNVARCHAR);
}
}
}

@Test
public void testExecute()
throws Exception
{
try (Connection connection = createConnection()) {
try (Statement statement = connection.createStatement()) {
assertTrue(statement.execute("SELECT 123 x, 'foo' y"));
ResultSet rs = statement.getResultSet();
assertTrue(rs.next());
assertEquals(rs.getLong(1), 123);
assertEquals(rs.getLong("x"), 123);
assertEquals(rs.getString(2), "foo");
assertEquals(rs.getString("y"), "foo");
assertFalse(rs.next());
}
}
}

@Test
public void testGetUpdateCount()
throws Exception
{
try (Connection connection = createConnection()) {
try (Statement statement = connection.createStatement()) {
assertTrue(statement.execute("SELECT 123 x, 'foo' y"));
assertEquals(statement.getUpdateCount(), -1);
}
}
}

@Test
public void testResultSetClose()
throws Exception
{
try (Connection connection = createConnection()) {
try (Statement statement = connection.createStatement()) {
assertTrue(statement.execute("SELECT 123 x, 'foo' y"));
ResultSet result = statement.getResultSet();
assertFalse(result.isClosed());
result.close();
assertTrue(result.isClosed());
}
}
}

@Test
public void testGetResultSet()
throws Exception
{
try (Connection connection = createConnection()) {
try (Statement statement = connection.createStatement()) {
assertTrue(statement.execute("SELECT 123 x, 'foo' y"));
ResultSet result = statement.getResultSet();
assertNotNull(result);
assertFalse(result.isClosed());
statement.getMoreResults();
assertTrue(result.isClosed());

assertTrue(statement.execute("SELECT 123 x, 'foo' y"));
result = statement.getResultSet();
assertNotNull(result);
assertFalse(result.isClosed());

assertTrue(statement.execute("SELECT 123 x, 'foo' y"));
assertFalse(statement.getMoreResults(Statement.CLOSE_CURRENT_RESULT));
}
}
}

@Test(expectedExceptions = SQLFeatureNotSupportedException.class, expectedExceptionsMessageRegExp = "Multiple open results not supported")
public void testGetMoreResultsException()
throws Exception
{
try (Connection connection = createConnection()) {
try (Statement statement = connection.createStatement()) {
assertTrue(statement.execute("SELECT 123 x, 'foo' y"));
statement.getMoreResults(Statement.KEEP_CURRENT_RESULT);
}
}
}

@Test
public void testConnectionResourceHandling()
throws Exception
Expand Down

0 comments on commit 8ed7041

Please sign in to comment.