diff --git itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java index 7b1c9da..1ba8ad3 100644 --- itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java +++ itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java @@ -1315,6 +1315,10 @@ public void testDatabaseMetaData() throws SQLException { assertFalse(meta.supportsMultipleResultSets()); assertFalse(meta.supportsStoredProcedures()); assertTrue(meta.supportsAlterTableWithAddColumn()); + + //-1 indicates malformed version. + assertTrue(meta.getDatabaseMajorVersion() > -1); + assertTrue(meta.getDatabaseMinorVersion() > -1); } @Test diff --git jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java index ef39573..96bd724 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java @@ -540,6 +540,9 @@ public int getHoldability() throws SQLException { */ public DatabaseMetaData getMetaData() throws SQLException { + if (isClosed) { + throw new SQLException("Connection is closed"); + } return new HiveDatabaseMetaData(this, client, sessHandle); } diff --git jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java index c447d44..5087ded 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java @@ -65,6 +65,9 @@ // The maximum column length = MFieldSchema.FNAME in metastore/src/model/package.jdo private static final int maxColumnNameLength = 128; + // Cached values, to save on round trips to database. + private String dbVersion = null; + /** * */ @@ -254,11 +257,11 @@ public ResultSet getCrossReference(String primaryCatalog, } public int getDatabaseMajorVersion() throws SQLException { - throw new SQLException("Method not supported"); + return Utils.getVersionPart(getDatabaseProductVersion(), 1); } public int getDatabaseMinorVersion() throws SQLException { - throw new SQLException("Method not supported"); + return Utils.getVersionPart(getDatabaseProductVersion(), 2); } public String getDatabaseProductName() throws SQLException { @@ -266,6 +269,9 @@ public String getDatabaseProductName() throws SQLException { } public String getDatabaseProductVersion() throws SQLException { + if (dbVersion != null) { //lazy-caching of the version. + return dbVersion; + } TGetInfoReq req = new TGetInfoReq(sessHandle, GetInfoType.CLI_DBMS_VER.toTGetInfoType()); TGetInfoResp resp; @@ -276,7 +282,8 @@ public String getDatabaseProductVersion() throws SQLException { } Utils.verifySuccess(resp.getStatus()); - return resp.getInfoValue().getStringValue(); + this.dbVersion = resp.getInfoValue().getStringValue(); + return dbVersion; } public int getDefaultTransactionIsolation() throws SQLException { diff --git jdbc/src/java/org/apache/hive/jdbc/Utils.java jdbc/src/java/org/apache/hive/jdbc/Utils.java index 4d75d98..913dc46 100644 --- jdbc/src/java/org/apache/hive/jdbc/Utils.java +++ jdbc/src/java/org/apache/hive/jdbc/Utils.java @@ -270,4 +270,30 @@ public static JdbcConnectionParams parseURL(String uri) throws IllegalArgumentEx return connParams; } + + /** + * Takes a version string delmited by '.' and '-' characters + * and returns a partial version. + * + * @param fullVersion + * version string. + * @param tokenPosition + * position of version string to get starting at 1. eg, for a X.x.xxx + * string, 1 will return the major version, 2 will return minor + * version. + * @return version part, or -1 if version string was malformed. + */ + static int getVersionPart(String fullVersion, int position) { + int version = -1; + try { + String[] tokens = fullVersion.split("[\\.-]"); //$NON-NLS-1$ + + if (tokens != null && tokens.length > 1 && tokens[position] != null) { + version = Integer.parseInt(tokens[position]); + } + } catch (Exception e) { + version = -1; + } + return version; + } }