From 1ee8def3b0d8bdcceb8f4346d6b5157cb8b35f0f Mon Sep 17 00:00:00 2001 From: David Hait Date: Thu, 28 May 2020 17:33:28 -0400 Subject: [PATCH 1/2] Added DatabaseMetaData.getDatabaseCompatabilityLevel --- .../jdbc/SQLServerDatabaseMetaData.java | 16 ++++++++++++++++ .../databasemetadata/DatabaseMetaDataTest.java | 10 ++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDatabaseMetaData.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDatabaseMetaData.java index ae98a220c..fb11f76c2 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDatabaseMetaData.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDatabaseMetaData.java @@ -2537,6 +2537,22 @@ public boolean locatorsUpdateCopy() throws SQLException { checkClosed(); return true; } + + /* -------------- MSSQL-JDBC Extension methods ---------------- */ + + public int getDatabaseCompatibilityLevel() throws SQLException { + checkClosed(); + String database = connection.getCatalog(); + try (SQLServerResultSet rs = getResultSetFromInternalQueries(null, + "select name, compatibility_level from sys.databases where name = '" + database + "'")) { + if (!rs.next()) { + return 0; + } + return rs.getInt("compatibility_level"); + } catch (SQLServerException e) { + return 0; + } + } } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java index 21e382705..f3d7c65e6 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java @@ -120,6 +120,16 @@ public void testDriverVersion() throws SQLException, IOException { } } + @Test + public void testDatabaseCompatibilityLevel() throws SQLException { + try (Connection conn = getConnection()) { + SQLServerDatabaseMetaData dbmData = (SQLServerDatabaseMetaData) conn.getMetaData(); + int compatibilityLevel = dbmData.getDatabaseCompatibilityLevel(); + assertTrue(compatibilityLevel > 0); + assertTrue(compatibilityLevel < dbmData.getDatabaseMajorVersion()*100); + } + } + /** * Your password should not be in getURL method. * From 97bf26036cf6cd0aaed4a1cd0bdbc8ba9680d6ac Mon Sep 17 00:00:00 2001 From: David Hait Date: Sat, 30 May 2020 12:16:29 -0400 Subject: [PATCH 2/2] Incorporated reviewer comments --- .../jdbc/SQLServerDatabaseMetaData.java | 21 ++++++++++++------- .../DatabaseMetaDataTest.java | 1 - 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDatabaseMetaData.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDatabaseMetaData.java index fb11f76c2..fe89eb806 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDatabaseMetaData.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDatabaseMetaData.java @@ -2538,20 +2538,25 @@ public boolean locatorsUpdateCopy() throws SQLException { return true; } - /* -------------- MSSQL-JDBC Extension methods ---------------- */ + /* -------------- MSSQL-JDBC Extension methods start here --------------- */ + /** + * Returns the database compatibility level setting for the current database. This is useful if the + * database's compatibility level is lower than the engine version. In this case the database will only + * support SQL commands at its compatibility level, and not the wider set of commands accepted by the + * engine. + * + * @return the database compatibility level value (from sys.databases table). + */ public int getDatabaseCompatibilityLevel() throws SQLException { checkClosed(); String database = connection.getCatalog(); - try (SQLServerResultSet rs = getResultSetFromInternalQueries(null, - "select name, compatibility_level from sys.databases where name = '" + database + "'")) { - if (!rs.next()) { - return 0; - } - return rs.getInt("compatibility_level"); - } catch (SQLServerException e) { + SQLServerResultSet rs = getResultSetFromInternalQueries(null, + "select name, compatibility_level from sys.databases where name = '" + database + "'"); + if (!rs.next()) { return 0; } + return rs.getInt("compatibility_level"); } } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java index f3d7c65e6..ebe0eb25d 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java @@ -126,7 +126,6 @@ public void testDatabaseCompatibilityLevel() throws SQLException { SQLServerDatabaseMetaData dbmData = (SQLServerDatabaseMetaData) conn.getMetaData(); int compatibilityLevel = dbmData.getDatabaseCompatibilityLevel(); assertTrue(compatibilityLevel > 0); - assertTrue(compatibilityLevel < dbmData.getDatabaseMajorVersion()*100); } }