[libpostgresql-jdbc-java] 05/12: The driver was incorrectly parsing identifiers that had parts that look like dollar quotes. Things like a$b$c are valid identifiers, not dollar quotes.

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


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

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

commit d96bbf262fa45a36c89fddb4d5fea85d1c7b5818
Author: Kris Jurka <books at ejurka.com>
Date:   Sat Oct 20 17:01:55 2007 +0000

    The driver was incorrectly parsing identifiers that had parts that
    look like dollar quotes.  Things like a$b$c are valid identifiers,
    not dollar quotes.
    
    When determining if a $ we've found is a dollar quote start, look at
    the preceding character and see if it is a valid identifier part to
    determine if we're in the midst of an identifier or are starting a
    new token.
    
    Michael Paesold
---
 org/postgresql/core/Parser.java                    | 38 ++++++++++++++++++++--
 .../test/jdbc2/PreparedStatementTest.java          | 23 ++++++++++++-
 2 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/org/postgresql/core/Parser.java b/org/postgresql/core/Parser.java
index bf1f122..11b4ec2 100644
--- a/org/postgresql/core/Parser.java
+++ b/org/postgresql/core/Parser.java
@@ -3,7 +3,7 @@
 * Copyright (c) 2006, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/core/Parser.java,v 1.1 2006/11/02 15:31:14 jurka Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/core/Parser.java,v 1.2 2006/12/01 08:53:45 jurka Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -89,7 +89,8 @@ public class Parser {
      * character.
      */
     public static int parseDollarQuotes(final char[] query, int offset) {
-        if (offset + 1 < query.length)
+        if (offset + 1 < query.length
+                && (offset == 0 || !isIdentifierContChar(query[offset-1])))
         {
             int endIdx = -1;
             if (query[offset + 1] == '$')
@@ -205,6 +206,37 @@ public class Parser {
     }
 
     /**
+     * Checks if a character is valid as the start of an identifier.
+     * 
+     * @param c the character to check
+     * @return true if valid as first character of an identifier; false if not
+     */
+    public static boolean isIdentifierStartChar(char c) {
+        /*
+         * Extracted from {ident_start} and {ident_cont} in
+         * pgsql/src/backend/parser/scan.l:
+         * ident_start    [A-Za-z\200-\377_]
+         * ident_cont     [A-Za-z\200-\377_0-9\$]
+         */
+        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
+                || c == '_' || c > 127 ;
+    }
+    
+    /**
+     * Checks if a character is valid as the second or later character of an
+     * identifier.
+     * 
+     * @param c the character to check
+     * @return true if valid as second or later character of an identifier; false if not
+     */
+    public static boolean isIdentifierContChar(char c) {
+        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
+                || c == '_' || c > 127
+                || (c >= '0' && c <= '9')
+                || c == '$';
+    }
+    
+    /**
      * @return true if the character terminates an identifier
      */
     public static boolean charTerminatesIdentifier(char c) {
@@ -228,7 +260,7 @@ public class Parser {
     }
 
     /**
-     * Checks if a character is valid as the second or latter character of a
+     * Checks if a character is valid as the second or later character of a
      * dollar quoting tag.
      * 
      * @param c the character to check
diff --git a/org/postgresql/test/jdbc2/PreparedStatementTest.java b/org/postgresql/test/jdbc2/PreparedStatementTest.java
index 60c3690..8dc8c10 100644
--- a/org/postgresql/test/jdbc2/PreparedStatementTest.java
+++ b/org/postgresql/test/jdbc2/PreparedStatementTest.java
@@ -3,7 +3,7 @@
 * Copyright (c) 2004-2005, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/PreparedStatementTest.java,v 1.17 2006/11/02 15:31:14 jurka Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/PreparedStatementTest.java,v 1.18 2006/12/01 08:53:46 jurka Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -405,6 +405,27 @@ public class PreparedStatementTest extends TestCase
         st.close();
     }
     
+    public void testDollarQuotesAndIdentifiers() throws SQLException {
+        // dollar-quotes are supported in the backend since version 8.0
+        if (!TestUtil.haveMinimumServerVersion(conn, "8.0"))
+            return;
+        
+        PreparedStatement st;
+        
+        conn.createStatement().execute("CREATE TEMP TABLE a$b$c(a varchar, b varchar)");
+        st = conn.prepareStatement("INSERT INTO a$b$c (a, b) VALUES (?, ?)");
+        st.setString(1, "a");
+        st.setString(2, "b");
+        st.executeUpdate();
+        st.close();
+
+        conn.createStatement().execute("CREATE TEMP TABLE e$f$g(h varchar, e$f$g varchar) ");
+        st = conn.prepareStatement("UPDATE e$f$g SET h = ? || e$f$g");
+        st.setString(1, "a");
+        st.executeUpdate();
+        st.close();
+    }
+    
     public void testComments() throws SQLException {
         PreparedStatement st;
         ResultSet rs;

-- 
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