[libpostgresql-jdbc-java] 08/13: Change DatabaseMetaData.getSearchStringEscape to always return "\\" instead of "\\\\". Previously it was assuming that it would be fed directly into a query and it needed to escape itself for the backend's input parser. This doesn't work for things like PreparedStatement parameters or DatabaseMetaData methods that take patterns. Backpatch only to 8.2 because it is a compatability problem and this keeps the old behavior for older drivers and gets it right in the new ones.

Emmanuel Bourg ebourg-guest at moszumanska.debian.org
Mon Jan 9 10:20:23 UTC 2017


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

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

commit 4ca84c0f626aed951eb9a06baeab14754d28ec42
Author: Kris Jurka <books at ejurka.com>
Date:   Wed Apr 11 07:33:26 2007 +0000

    Change DatabaseMetaData.getSearchStringEscape to always return "\\"
    instead of "\\\\".  Previously it was assuming that it would be fed
    directly into a query and it needed to escape itself for the
    backend's input parser.  This doesn't work for things like
    PreparedStatement parameters or DatabaseMetaData methods that take
    patterns.  Backpatch only to 8.2 because it is a compatability
    problem and this keeps the old behavior for older drivers and gets
    it right in the new ones.
---
 .../jdbc2/AbstractJdbc2DatabaseMetaData.java       | 28 ++++++++++------------
 .../test/jdbc2/DatabaseMetaDataTest.java           | 12 ++++++----
 2 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java b/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java
index 7848c47..b8f67fb 100644
--- a/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java
+++ b/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java
@@ -3,7 +3,7 @@
 * Copyright (c) 2004-2005, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java,v 1.32 2006/11/29 04:47:32 jurka Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java,v 1.33 2006/12/01 08:53:45 jurka Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -497,20 +497,18 @@ public abstract class AbstractJdbc2DatabaseMetaData
      */
     public String getSearchStringEscape() throws SQLException
     {
-        // Java's parse takes off two backslashes
-        // and then pg's input parser takes off another layer
-        // so we need many backslashes here.
-        //
-        // This would work differently if you used a PreparedStatement
-        // and " mycol LIKE ? " which using the V3 protocol would skip
-        // pg's input parser, but I don't know what we can do about that.
-        //
-        // Since PostgreSQL 8.2, the option standard_conforming_strings
-        // controls whether backslashes are treated as escape characters.
-        // If this option is on, pg's parser takes a backslash literally
-        // even in string constants embedded into the query text.
-        //
-        return connection.getStandardConformingStrings() ? "\\" : "\\\\";
+        // This method originally returned "\\\\" assuming that it
+        // would be fed directly into pg's input parser so it would
+        // need two backslashes.  This isn't how it's supposed to be
+        // used though.  If passed as a PreparedStatement parameter
+        // or fed to a DatabaseMetaData method then double backslashes
+        // are incorrect.  If you're feeding something directly into
+        // a query you are responsible for correctly escaping it.
+        // With 8.2+ this escaping is a little trickier because you
+        // must know the setting of standard_conforming_strings, but
+        // that's not our problem.
+
+        return "\\";
     }
 
     /*
diff --git a/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java b/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java
index ec3472c..26139b7 100644
--- a/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java
+++ b/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java
@@ -3,7 +3,7 @@
 * Copyright (c) 2004-2005, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java,v 1.35 2005/11/24 02:31:43 oliver Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java,v 1.36 2006/02/03 21:10:15 jurka Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -524,12 +524,16 @@ public class DatabaseMetaDataTest extends TestCase
 
     public void testSearchStringEscape() throws Exception {
         DatabaseMetaData dbmd = con.getMetaData();
-        Statement stmt = con.createStatement();
-        ResultSet rs = stmt.executeQuery("SELECT 'a' LIKE '" + dbmd.getSearchStringEscape() + "_'");
+        String pattern = dbmd.getSearchStringEscape() + "_";
+        PreparedStatement pstmt = con.prepareStatement("SELECT 'a' LIKE ?, '_' LIKE ?");
+        pstmt.setString(1, pattern);
+        pstmt.setString(2, pattern);
+        ResultSet rs = pstmt.executeQuery();
         assertTrue (rs.next());
         assertTrue(!rs.getBoolean(1));
+        assertTrue(rs.getBoolean(2));
         rs.close();
-        stmt.close();
+        pstmt.close();
     }
 
     public void testGetUDTQualified() 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