diff --git itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java index e68a5a9..98ea79b 100644 --- itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java +++ itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java @@ -192,6 +192,32 @@ public void testURIDatabaseName() throws Exception{ hs2Conn.close(); } + @Test + public void testConnectionSchemaAPIs() throws Exception { + String db1 = "DB1"; + + assertEquals("default", hs2Conn.getSchema()); + Statement stmt = hs2Conn.createStatement(); + stmt.execute("DROP DATABASE IF EXISTS " + db1 + " CASCADE"); + stmt.execute("CREATE DATABASE " + db1); + assertEquals("default", hs2Conn.getSchema()); + + stmt.execute("USE " + db1); + assertEquals(db1, hs2Conn.getSchema()); + + stmt.execute("USE default"); + assertEquals("default", hs2Conn.getSchema()); + + hs2Conn.setSchema(db1); + assertEquals(db1, hs2Conn.getSchema()); + hs2Conn.setSchema("default"); + assertEquals("default", hs2Conn.getSchema()); + + assertTrue(hs2Conn.getCatalog().isEmpty()); + hs2Conn.setCatalog("foo"); + assertTrue(hs2Conn.getCatalog().isEmpty()); + } + /** * verify that the current db is the one expected. first create table as .tab and then * describe that table to check if is the current database @@ -208,4 +234,4 @@ private void verifyCurrentDB(String expectedDbName, Connection hs2Conn) throws E stmt.execute("DROP TABLE IF EXISTS " + expectedDbName + "." + verifyTab); stmt.close(); } -} + } diff --git jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java index 13fc19b..32950e9 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java @@ -667,10 +667,16 @@ public int getNetworkTimeout() throws SQLException { throw new SQLException("Method not supported"); } - public String getSchema() throws SQLException { - // JDK 1.7 - throw new SQLException("Method not supported"); + if (isClosed) { + throw new SQLException("Connection is closed"); + } + Statement stmt = createStatement(); + ResultSet res = stmt.executeQuery("SELECT current_database()"); + if (!res.next()) { + throw new SQLException("Failed to get schema information"); + } + return res.getString(1); } /* @@ -923,8 +929,12 @@ public void setAutoCommit(boolean autoCommit) throws SQLException { @Override public void setCatalog(String catalog) throws SQLException { - // TODO Auto-generated method stub - throw new SQLException("Method not supported"); + // Per JDBC spec, if the driver does not support catalogs, + // it will silently ignore this request. + if (isClosed) { + throw new SQLException("Connection is closed"); + } + return; } /* @@ -1008,7 +1018,14 @@ public Savepoint setSavepoint(String name) throws SQLException { public void setSchema(String schema) throws SQLException { // JDK 1.7 - throw new SQLException("Method not supported"); + if (isClosed) { + throw new SQLException("Connection is closed"); + } + if (schema == null || schema.isEmpty()) { + throw new SQLException("Schema name is null or empty"); + } + Statement stmt = createStatement(); + stmt.execute("use " + schema); } /*