[libpostgresql-jdbc-java] 06/22: The 8.4 release added some code to avoid re-describing a statement if we already had the type information available by copying the resolved type information from the query to the parameters. Its goal was just to overwrite parameters without a type (unknown), but it was actually overwriting all types which could change the query's desired behavior.

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


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

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

commit 6b478e961aea2da2fae2f326ec880de6ff8a9d13
Author: Kris Jurka <books at ejurka.com>
Date:   Mon Dec 7 22:03:14 2009 +0000

    The 8.4 release added some code to avoid re-describing a statement
    if we already had the type information available by copying the
    resolved type information from the query to the parameters.  Its
    goal was just to overwrite parameters without a type (unknown), but
    it was actually overwriting all types which could change the query's
    desired behavior.
    
    Per an example from Hiroshi Saito.
---
 org/postgresql/core/v3/QueryExecutorImpl.java      | 13 ++++++++----
 .../test/jdbc3/ParameterMetaDataTest.java          | 23 ++++++++++++++++++++--
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/org/postgresql/core/v3/QueryExecutorImpl.java b/org/postgresql/core/v3/QueryExecutorImpl.java
index f37b79c..0415c2d 100644
--- a/org/postgresql/core/v3/QueryExecutorImpl.java
+++ b/org/postgresql/core/v3/QueryExecutorImpl.java
@@ -4,7 +4,7 @@
 * Copyright (c) 2004, Open Cloud Limited.
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/core/v3/QueryExecutorImpl.java,v 1.45 2009/07/01 05:00:40 jurka Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/core/v3/QueryExecutorImpl.java,v 1.45.2.1 2009/12/04 19:53:27 jurka Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -1492,10 +1492,15 @@ public class QueryExecutorImpl implements QueryExecutor {
 
         if (!describeStatement && paramsHasUnknown && !queryHasUnknown)
         {
-            int numParams = params.getParameterCount();
             int queryOIDs[] = query.getStatementTypes();
-            for (int i=1; i<=numParams; i++) {
-                params.setResolvedType(i, queryOIDs[i-1]);
+            int paramOIDs[] = params.getTypeOIDs();
+            for (int i=0; i<paramOIDs.length; i++) {
+                // Only supply type information when there isn't any
+                // already, don't arbitrarily overwrite user supplied
+                // type information.
+                if (paramOIDs[i] == Oid.UNSPECIFIED) {
+                    params.setResolvedType(i+1, queryOIDs[i]);
+                }
             }
         }
 
diff --git a/org/postgresql/test/jdbc3/ParameterMetaDataTest.java b/org/postgresql/test/jdbc3/ParameterMetaDataTest.java
index 1c871e2..625044b 100644
--- a/org/postgresql/test/jdbc3/ParameterMetaDataTest.java
+++ b/org/postgresql/test/jdbc3/ParameterMetaDataTest.java
@@ -3,7 +3,7 @@
 * Copyright (c) 2005-2008, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc3/ParameterMetaDataTest.java,v 1.4 2006/05/15 09:35:57 jurka Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc3/ParameterMetaDataTest.java,v 1.5 2008/01/08 06:56:31 jurka Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -23,7 +23,7 @@ public class ParameterMetaDataTest extends TestCase {
 
     protected void setUp() throws Exception {
         _conn = TestUtil.openDB();
-        TestUtil.createTable(_conn, "parametertest", "a int4, b float8, c text, d point");
+        TestUtil.createTable(_conn, "parametertest", "a int4, b float8, c text, d point, e timestamp with time zone");
     }
 
     protected void tearDown() throws SQLException {
@@ -87,4 +87,23 @@ public class ParameterMetaDataTest extends TestCase {
         pstmt.close();
 
     }
+
+    // Here we test that we can legally change the resolved type
+    // from text to varchar with the complicating factor that there
+    // is also an unknown parameter.
+    // 
+    public void testTypeChangeWithUnknown() throws SQLException {
+        if (!TestUtil.isProtocolVersion(_conn, 3))
+            return;
+
+        PreparedStatement pstmt = _conn.prepareStatement("SELECT a FROM parametertest WHERE c = ? AND e = ?");
+        ParameterMetaData pmd = pstmt.getParameterMetaData();
+
+        pstmt.setString(1, "Hi");
+        pstmt.setTimestamp(2, new Timestamp(0L));
+
+        ResultSet rs = pstmt.executeQuery();
+        rs.close();
+    }
+
 }

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