postgresql-jdbc.git / summary / log / commit / refs

commit    a9f57e4202e6cfb7779a7ffac69f5cb65d4039b9
Author:   Kris Jurka <books@ejurka.com>
Date:     Fri Jul 27 22:32:29 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 18bb5d52..104f7c8e 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.95 2007/07/27 08:54:54 jurka Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java,v 1.96 2007/07/27 10:15:32 jurka Exp $ * *------------------------------------------------------------------------- */ @@ -1716,6 +1716,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 95adfe5c..c9ff5ca1 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.23 2007/01/05 00:34:06 jurka Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/UpdateableResultTest.java,v 1.24 2007/04/16 16:36:41 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(); } [parent: e1bcdb9acfb9]