[Commits] Rev 396: CONJ-16 : Introduced nullCatalogMeansCurrent parameter, compatibly to ConnectorJ. in file:///H:/bzr/mariadb-jdbc-push/

Vladislav Vaintroub wlad at montyprogram.com
Sat Feb 2 14:36:19 EET 2013


At file:///H:/bzr/mariadb-jdbc-push/

------------------------------------------------------------
revno: 396
revision-id: wlad at montyprogram.com-20130202123504-i0oociz534qp0xk0
parent: wlad at montyprogram.com-20130117223426-abpxyg0y9t5t8k1c
fixes bug: https://mariadb.atlassian.net/browse/CONJ-16
committer: Vladislav Vaintroub <wlad at montyprogram.com>
branch nick: mariadb-jdbc-push
timestamp: Sat 2013-02-02 13:35:04 +0100
message:
  CONJ-16 : Introduced nullCatalogMeansCurrent parameter, compatibly to ConnectorJ.
  
  There are applications that depend on ConnectorJ default behavior, even if this behavior deviates from standard.
-------------- next part --------------
=== modified file 'src/main/java/org/mariadb/jdbc/MySQLConnection.java'
--- a/src/main/java/org/mariadb/jdbc/MySQLConnection.java	2013-01-13 23:15:13 +0000
+++ b/src/main/java/org/mariadb/jdbc/MySQLConnection.java	2013-02-02 12:35:04 +0000
@@ -88,6 +88,7 @@ public final class MySQLConnection
 
     private boolean warningsCleared;
     boolean noBackslashEscapes;
+    boolean nullCatalogMeansCurrent = true;
 
 
     /**
@@ -113,22 +114,27 @@ public final class MySQLConnection
         String sessionVariables = protocol.getInfo().getProperty("sessionVariables");
         
         if (fastConnect && (sessionVariables == null))
-                return connection;
+            return connection;
         
         Statement st = null;
         try {
-                st = connection.createStatement();                      
-                if (sessionVariables != null) {
-                  st.executeUpdate("set session " + sessionVariables);
-                }
-                if (!fastConnect) {
-                        ResultSet rs = st.executeQuery("select @@sql_mode");
-                        rs.next();
-                        String sqlMode = rs.getString(1);
-                        if (sqlMode.contains("NO_BACKSLASH_ESCAPES")) {
-                                connection.noBackslashEscapes = true;
-                        }
-                }
+                st = connection.createStatement();                      
+                if (sessionVariables != null) {
+                    st.executeUpdate("set session " + sessionVariables);
+                }
+                if (!fastConnect) {
+                    ResultSet rs = st.executeQuery("select @@sql_mode");
+                    rs.next();
+                    String sqlMode = rs.getString(1);
+                    if (sqlMode.contains("NO_BACKSLASH_ESCAPES")) {
+                        connection.noBackslashEscapes = true;
+                    }
+                }
+                String nullCatalogMeansCurrentString = protocol.getInfo().getProperty("nullCatalogMeansCurrent");
+                if (nullCatalogMeansCurrentString != null && nullCatalogMeansCurrentString.equals("false")) {
+                    connection.nullCatalogMeansCurrent = false;
+                }
+                
         } finally {
             if (st != null)
                 st.close();

=== modified file 'src/main/java/org/mariadb/jdbc/MySQLDatabaseMetaData.java'
--- a/src/main/java/org/mariadb/jdbc/MySQLDatabaseMetaData.java	2013-01-15 01:18:28 +0000
+++ b/src/main/java/org/mariadb/jdbc/MySQLDatabaseMetaData.java	2013-02-02 12:35:04 +0000
@@ -127,9 +127,36 @@ public class MySQLDatabaseMetaData imple
             return "NULL";    
         return "'" + Utils.escapeString(s, connection.noBackslashEscapes) + "'";
     }
-    
+
+    /**
+     * Generate part of the information schema query that restricts catalog names
+     * In the driver, catalogs is the equivalent to MySQL schemas.
+     * 
+     * @param columnName - column name in the information schema table
+     * @param catalog - catalog name.
+     * 
+     * This driver does not (always) follow JDBC standard for following special values, due 
+     * to ConnectorJ compatibility
+     * 
+     * 1. empty string ("") - matches current catalog (i.e database).JDBC standard says
+     * only tables without catalog should be returned - such tables do not exist in MySQL.
+     * If there is no current catalog, then empty string matches any catalog.
+     *   
+     * 2. null  - if nullCatalogMeansCurrent=true (which is the default), then the handling is the same
+     * as for "" . i.e return current catalog.
+     *   
+     * JDBC-conforming way would be to match any catalog with null parameter. This 
+     * can be switched with nullCatalogMeansCurrent=false in the connection URL.
+     * 
+     * @return part of SQL query ,that restricts search for the catalog.
+     */
     String catalogCond(String columnName, String catalog) {
-        if (catalog == null){
+        if (catalog == null && connection.nullCatalogMeansCurrent) {
+            /* Treat null catalog as current */
+            catalog = "";
+        }
+        
+        if (catalog == null) {
             return "(1 = 1)"; 
         }
         if (catalog.equals("")) {

=== modified file 'src/test/java/org/mariadb/jdbc/DatabaseMetadataTest.java'
--- a/src/test/java/org/mariadb/jdbc/DatabaseMetadataTest.java	2013-01-15 01:18:28 +0000
+++ b/src/test/java/org/mariadb/jdbc/DatabaseMetadataTest.java	2013-02-02 12:35:04 +0000
@@ -387,6 +387,35 @@ public class DatabaseMetadataTest extend
     }
     
 
+    /* Verify default behavior for nullCatalogMeansCurrent (=true) */
+    @Test 
+    public void nullCatalogMeansCurrent() throws Exception {
+        String catalog = connection.getCatalog();
+        ResultSet rs = connection.getMetaData().getColumns(null, null, null, null);
+        while(rs.next()) {
+            assertTrue(rs.getString("TABLE_CAT").equalsIgnoreCase(catalog));
+        }
+    }
+    
+    /* Verify that "nullCatalogMeansCurrent=false" works (i.e information_schema columns are returned)*/
+    @Test 
+    public void nullCatalogMeansCurrent2() throws Exception {
+        Connection c = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&nullCatalogMeansCurrent=false");
+        boolean haveInformationSchema = false;
+        try {
+            ResultSet rs = c.getMetaData().getColumns(null, null, null, null);
+            while(rs.next()) {
+                if (rs.getString("TABLE_CAT").equalsIgnoreCase("information_schema")) {
+                    haveInformationSchema = true;
+                    break;
+                }
+            }
+        }  finally {
+            c.close();
+        }
+        assertTrue(haveInformationSchema);
+    }
+ 
     @Test 
     public void testGetTypeInfoBasic() throws SQLException {
         testResultSetColumns(



More information about the commits mailing list