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..b9bb259 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,37 @@ public void testURIDatabaseName() throws Exception{ hs2Conn.close(); } + @Test + public void testConnectionSchemaAPIs() throws Exception { + String db1 = "DB1"; + /** + * get/set Schema are new in JDK7 and not available in java.sql.Connection in JDK6. + * Hence the test uses HiveConnection object to call these methods so that test will run with older JDKs + */ + HiveConnection hiveConn = (HiveConnection)hs2Conn; + + assertEquals("default", hiveConn.getSchema()); + Statement stmt = hs2Conn.createStatement(); + stmt.execute("DROP DATABASE IF EXISTS " + db1 + " CASCADE"); + stmt.execute("CREATE DATABASE " + db1); + assertEquals("default", hiveConn.getSchema()); + + stmt.execute("USE " + db1); + assertEquals(db1, hiveConn.getSchema()); + + stmt.execute("USE default"); + assertEquals("default", hiveConn.getSchema()); + + hiveConn.setSchema(db1); + assertEquals(db1, hiveConn.getSchema()); + hiveConn.setSchema("default"); + assertEquals("default", hiveConn.getSchema()); + + assertTrue(hiveConn.getCatalog().isEmpty()); + hiveConn.setCatalog("foo"); + assertTrue(hiveConn.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 +239,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..4102d7a 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java @@ -667,10 +667,19 @@ 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"); + } + String schemaName = res.getString(1); + res.close(); + stmt.close(); + return schemaName; } /* @@ -923,8 +932,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 +1021,15 @@ 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); + stmt.close(); } /*