You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by na...@apache.org on 2014/02/17 02:37:54 UTC

svn commit: r1568870 - in /hive/trunk: itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java

Author: navis
Date: Mon Feb 17 01:37:53 2014
New Revision: 1568870

URL: http://svn.apache.org/r1568870
Log:
HIVE-6339 : Implement new JDK7 schema management APIs in java.sql.Connection (Prasad Mujumdar via Navis)

Modified:
    hive/trunk/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java
    hive/trunk/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java

Modified: hive/trunk/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java
URL: http://svn.apache.org/viewvc/hive/trunk/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java?rev=1568870&r1=1568869&r2=1568870&view=diff
==============================================================================
--- hive/trunk/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java (original)
+++ hive/trunk/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java Mon Feb 17 01:37:53 2014
@@ -192,6 +192,37 @@ import org.junit.Test;
      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 <db>.tab and then 
     * describe that table to check if <db> is the current database
@@ -208,4 +239,4 @@ import org.junit.Test;
      stmt.execute("DROP TABLE IF EXISTS " + expectedDbName + "." + verifyTab);
      stmt.close();
    }
-}
+  }

Modified: hive/trunk/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
URL: http://svn.apache.org/viewvc/hive/trunk/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java?rev=1568870&r1=1568869&r2=1568870&view=diff
==============================================================================
--- hive/trunk/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java (original)
+++ hive/trunk/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java Mon Feb 17 01:37:53 2014
@@ -667,10 +667,19 @@ public class HiveConnection implements j
     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 class HiveConnection implements j
 
   @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 class HiveConnection implements j
 
   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();
   }
 
   /*