[libpostgresql-jdbc-java] 12/13: Updatable ResultSets did not work when updating bytea data and then retrieving it because we send the data to the server in binary format, but the ResultSet was expecting to read it in text format. So we need to convert the data from binary to text format before stuffing it into the ResultSet.

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


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

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

commit 849e95a6d577c58f779f9725adfd520840f7d27a
Author: Kris Jurka <books at ejurka.com>
Date:   Fri Jul 27 22:33:48 2007 +0000

    Updatable ResultSets did not work when updating bytea data and then
    retrieving it because we send the data to the server in binary
    format, but the ResultSet was expecting to read it in text format.
    So we need to convert the data from binary to text format before
    stuffing it into the ResultSet.
    
    Mikko Tiihonen
---
 org/postgresql/jdbc2/AbstractJdbc2ResultSet.java   | 16 +++++++++-
 .../test/jdbc2/UpdateableResultTest.java           | 36 +++++++++++++++-------
 2 files changed, 40 insertions(+), 12 deletions(-)

diff --git a/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java b/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
index 664f241..f45d1a9 100644
--- a/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
+++ b/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
@@ -3,7 +3,7 @@
 * Copyright (c) 2003-2005, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java,v 1.80.2.3 2007/02/19 06:05:01 jurka Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java,v 1.80.2.4 2007/04/16 16:36:56 jurka Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -1739,6 +1739,20 @@ public abstract class AbstractJdbc2ResultSet implements BaseResultSet, org.postg
                     // Should never happen?
                     break;
 
+                case Types.BINARY:
+                case Types.LONGVARBINARY:
+                case Types.VARBINARY:
+                    if (fields[columnIndex].getFormat() == Field.BINARY_FORMAT) {
+                        rowBuffer[columnIndex] = (byte[]) valueObject;
+                    } else {
+                        try {
+                            rowBuffer[columnIndex] = PGbytea.toPGString((byte[]) valueObject).getBytes("ISO-8859-1");
+                        } catch (UnsupportedEncodingException e) {
+                            throw new PSQLException(GT.tr("The JVM claims not to support the encoding: {0}", "ISO-8859-1"), PSQLState.UNEXPECTED_ERROR, e);
+                        }
+                    }
+                    break;
+ 
                 default:
                     rowBuffer[columnIndex] = (byte[]) valueObject;
                 }
diff --git a/org/postgresql/test/jdbc2/UpdateableResultTest.java b/org/postgresql/test/jdbc2/UpdateableResultTest.java
index 7be8b80..6280c1c 100644
--- a/org/postgresql/test/jdbc2/UpdateableResultTest.java
+++ b/org/postgresql/test/jdbc2/UpdateableResultTest.java
@@ -3,13 +3,15 @@
 * Copyright (c) 2001-2005, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/UpdateableResultTest.java,v 1.22.2.1 2007/01/05 00:34:20 jurka Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/UpdateableResultTest.java,v 1.22.2.2 2007/04/16 16:36:56 jurka Exp $
 *
 *-------------------------------------------------------------------------
 */
 package org.postgresql.test.jdbc2;
 
 import java.sql.*;
+import java.util.Arrays;
+
 import junit.framework.TestCase;
 
 import java.io.InputStream;
@@ -217,6 +219,9 @@ public class UpdateableResultTest extends TestCase
 
     public void testUpdateStreams() throws SQLException, UnsupportedEncodingException
     {
+        String string = "Hello";
+        byte[] bytes = new byte[]{0,'\\',(byte) 128,(byte) 255};
+
         Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
         ResultSet rs = stmt.executeQuery("SELECT id, asi, chr, bin FROM stream");
 
@@ -227,6 +232,13 @@ public class UpdateableResultTest extends TestCase
         rs.updateBinaryStream("bin", null, 0);
         rs.insertRow();
 
+        rs.moveToInsertRow();
+        rs.updateInt(1, 3);
+        rs.updateAsciiStream("asi", new ByteArrayInputStream(string.getBytes("US-ASCII")), 5);
+        rs.updateCharacterStream("chr", new StringReader(string), 5);
+        rs.updateBinaryStream("bin", new ByteArrayInputStream(bytes), bytes.length);
+        rs.insertRow();
+
         rs.beforeFirst();
         rs.next();
 
@@ -235,29 +247,31 @@ public class UpdateableResultTest extends TestCase
         assertNull(rs.getString(3));
         assertNull(rs.getBytes(4));
 
-        String string = "Hello";
-        InputStream asi = new ByteArrayInputStream(string.getBytes("US-ASCII"));
-        Reader chr = new StringReader(string);
-        InputStream bin = new ByteArrayInputStream(string.getBytes("US-ASCII"));
-
         rs.updateInt("id", 2);
-        rs.updateAsciiStream("asi", asi, 5);
-        rs.updateCharacterStream("chr", chr, 5);
-        rs.updateBinaryStream("bin", bin, 5);
+        rs.updateAsciiStream("asi", new ByteArrayInputStream(string.getBytes("US-ASCII")), 5);
+        rs.updateCharacterStream("chr", new StringReader(string), 5);
+        rs.updateBinaryStream("bin", new ByteArrayInputStream(bytes), bytes.length);
         rs.updateRow();
 
         assertEquals(2, rs.getInt(1));
         assertEquals(string, rs.getString(2));
         assertEquals(string, rs.getString(3));
-        assertEquals(string, rs.getString(4));
+        assertEquals(Arrays.toString(bytes), Arrays.toString(rs.getBytes(4)));
 
         rs.refreshRow();
 
         assertEquals(2, rs.getInt(1));
         assertEquals(string, rs.getString(2));
         assertEquals(string, rs.getString(3));
-        assertEquals(string, rs.getString(4));
+        assertEquals(Arrays.toString(bytes), Arrays.toString(rs.getBytes(4)));
+
+        rs.next();
 
+        assertEquals(3, rs.getInt(1));
+        assertEquals(string, rs.getString(2));
+        assertEquals(string, rs.getString(3));
+        assertEquals(Arrays.toString(bytes), Arrays.toString(rs.getBytes(4)));
+        
         rs.close();
         stmt.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