[libpostgresql-jdbc-java] 16/24: Unescape/unquote result of getSchema

Emmanuel Bourg ebourg-guest at moszumanska.debian.org
Mon Jan 9 21:17:40 UTC 2017


This is an automated email from the git hooks/post-receive script.

ebourg-guest pushed a commit to tag REL9_3_1103
in repository libpostgresql-jdbc-java.

commit 86994e0147df9fe5a9d3d6c5a9d01799bdebfbc8
Author: Alexis Meneses <alexismeneses at users.noreply.github.com>
Date:   Thu Dec 4 21:33:57 2014 +0100

    Unescape/unquote result of getSchema
    
    (cherry picked from commit f97294a37e19ab1776ac60768ba335950c0fcaad)
    
    Conflicts:
    	org/postgresql/jdbc4/AbstractJdbc4Connection.java
---
 org/postgresql/jdbc4/AbstractJdbc4Connection.java | 27 ++++++++++++++++++-----
 org/postgresql/test/jdbc4/jdbc41/SchemaTest.java  | 27 +++++++++++++++++++----
 2 files changed, 45 insertions(+), 9 deletions(-)

diff --git a/org/postgresql/jdbc4/AbstractJdbc4Connection.java b/org/postgresql/jdbc4/AbstractJdbc4Connection.java
index 2f0cdf5..f0ed27c 100644
--- a/org/postgresql/jdbc4/AbstractJdbc4Connection.java
+++ b/org/postgresql/jdbc4/AbstractJdbc4Connection.java
@@ -13,6 +13,8 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Properties;
 import java.util.concurrent.Executor;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.postgresql.core.Oid;
 import org.postgresql.core.Utils;
@@ -25,6 +27,11 @@ import org.postgresql.jdbc2.AbstractJdbc2Array;
 
 abstract class AbstractJdbc4Connection extends org.postgresql.jdbc3g.AbstractJdbc3gConnection
 {
+    /**
+     * Pattern used to unquote the result of {@link #getSchema()}
+     */
+    private static final Pattern PATTERN_GET_SCHEMA = Pattern.compile("^\\\"(.*)\\\"(?!\\\")");
+
     private final Properties _clientInfo;
 
     public AbstractJdbc4Connection(HostSpec[] hostSpecs, String user, String database, Properties info, String url) throws SQLException {
@@ -255,15 +262,25 @@ abstract class AbstractJdbc4Connection extends org.postgresql.jdbc3g.AbstractJdb
             stmt.close();
         }
 
-        // keep only the first schema of the search path if there are many
-        int commaIndex = searchPath.indexOf(',');
-        if (commaIndex == -1)
+        if (searchPath.startsWith("\""))
         {
-            return searchPath;
+            // unquote the result if it's a quoted string
+            Matcher matcher = PATTERN_GET_SCHEMA.matcher(searchPath);
+            matcher.find();
+            return matcher.group(1).replaceAll("\"\"", "\"");
         }
         else
         {
-            return searchPath.substring(0, commaIndex);
+            // keep only the first schema of the search path if there are many
+            int commaIndex = searchPath.indexOf(',');
+            if (commaIndex == -1)
+            {
+                return searchPath;
+            }
+            else
+            {
+                return searchPath.substring(0, commaIndex);
+            }
         }
     }
 
diff --git a/org/postgresql/test/jdbc4/jdbc41/SchemaTest.java b/org/postgresql/test/jdbc4/jdbc41/SchemaTest.java
index 4a544ee..dea4679 100644
--- a/org/postgresql/test/jdbc4/jdbc41/SchemaTest.java
+++ b/org/postgresql/test/jdbc4/jdbc41/SchemaTest.java
@@ -34,6 +34,7 @@ public class SchemaTest extends TestCase
         stmt.execute("CREATE SCHEMA \"schema 3\"");
         stmt.execute("CREATE SCHEMA \"schema \"\"4\"");
         stmt.execute("CREATE SCHEMA \"schema '5\"");
+        stmt.execute("CREATE SCHEMA \"schema ,6\"");
         stmt.execute("CREATE SCHEMA \"UpperCase\"");
         TestUtil.createTable(_conn, "schema1.table1", "id integer");
         TestUtil.createTable(_conn, "schema2.table2", "id integer");
@@ -49,6 +50,7 @@ public class SchemaTest extends TestCase
         stmt.execute("DROP SCHEMA \"schema 3\" CASCADE");
         stmt.execute("DROP SCHEMA \"schema \"\"4\" CASCADE");
         stmt.execute("DROP SCHEMA \"schema '5\" CASCADE");
+        stmt.execute("DROP SCHEMA \"schema ,6\"");
         stmt.execute("DROP SCHEMA \"UpperCase\" CASCADE");
         TestUtil.closeDB(_conn);
     }
@@ -63,13 +65,13 @@ public class SchemaTest extends TestCase
         _conn.setSchema("schema2");
         assertEquals("schema2", _conn.getSchema());
         _conn.setSchema("schema 3");
-        assertEquals("\"schema 3\"", _conn.getSchema());
+        assertEquals("schema 3", _conn.getSchema());
         _conn.setSchema("schema \"4");
-        assertEquals("\"schema \"\"4\"", _conn.getSchema());
+        assertEquals("schema \"4", _conn.getSchema());
         _conn.setSchema("schema '5");
-        assertEquals("\"schema '5\"", _conn.getSchema());
+        assertEquals("schema '5", _conn.getSchema());
         _conn.setSchema("UpperCase");
-        assertEquals("\"UpperCase\"", _conn.getSchema());
+        assertEquals("UpperCase", _conn.getSchema());
     }
 
     /**
@@ -162,6 +164,23 @@ public class SchemaTest extends TestCase
             }
         }
         assertEquals("schema1", _conn.getSchema());
+
+        stmt = _conn.createStatement();
+        try
+        {
+            stmt.execute("SET search_path TO \"schema ,6\",schema2");
+        }
+        finally
+        {
+            try
+            {
+                stmt.close();
+            }
+            catch (SQLException e)
+            {
+            }
+        }
+        assertEquals("schema ,6", _conn.getSchema());
     }
 
     public void testSchemaInProperties() throws Exception

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/libpostgresql-jdbc-java.git



More information about the pkg-java-commits mailing list