Re: getArray() fails to handle some string array cases

From: Dima Tkach <dmitry(at)openratings(dot)com>
To: Oliver Jowett <oliver(at)opencloud(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: getArray() fails to handle some string array cases
Date: 2003-07-20 16:33:19
Message-ID: 3F1AC44F.1000701@openratings.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Yeah...

I sent the patch for this two months ago, and then again around the
beginning of July but never got any reply :-(
It is still in the archives (search for Array.getArray () subject around
07/09/03), you can get it and apply locally to your jdbc source... it
should fix your problem (and many other similar ones)...
If you can't find it in the archives, let me know, I canre-send a copy
to you...

I hope, it helps...

Dima

Oliver Jowett wrote:

>While playing with an implementation of setArray() I've found a case where
>the existing getArray() implementation does not parse an array result
>correctly. The attached testcase produces:
>
> [junit] Testcase: testStringArray(org.postgresql.test.jdbc2.GetArrayTest): FAILED
> [junit] driver element 2: expected:<"> but was:<,',\>
> [junit] junit.framework.AssertionFailedError: driver element 2: expected:<"> but was:<,',\>
> [junit] at org.postgresql.test.jdbc2.GetArrayTest.testStringArray(GetArrayTest.java:125)
> [junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> [junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> [junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>
>I'm not going to have time to track the cause of this down, but maybe the
>testcase is useful to someone else.
>
>-O
>
>
>
>------------------------------------------------------------------------
>
>package org.postgresql.test.jdbc2;
>
>import org.postgresql.test.TestUtil;
>import junit.framework.Test;
>import junit.framework.TestCase;
>import junit.framework.TestSuite;
>import java.sql.*;
>
>/*
> * Test case for getArray()
> */
>public class GetArrayTest extends TestCase {
> private Connection con;
> private Statement stmt;
>
> public GetArrayTest(String name) {
> super(name);
> }
>
> protected void setUp() throws Exception {
> con = TestUtil.openDB();
> stmt = con.createStatement();
>
> // Drop the test table if it already exists for some
> // reason. It is not an error if it doesn't exist.
> try {
> stmt.executeUpdate("DROP TABLE testarray");
> } catch (SQLException e) {
> // Intentionally ignore. We cannot distinguish
> // "table does not exist" from other errors, since
> // PostgreSQL doesn't support error codes yet.
> }
>
> stmt.executeUpdate(
> "CREATE TABLE testarray(intarray INTEGER[], strarray TEXT[])");
> }
>
> protected void tearDown() throws Exception {
> con.setAutoCommit(true);
> if (stmt != null) {
> stmt.executeUpdate("DROP TABLE testarray");
> stmt.close();
> }
> if (con != null) {
> TestUtil.closeDB(con);
> }
> }
>
> public void testEmptyIntArray() throws SQLException {
> stmt.executeUpdate("INSERT INTO testarray(intarray) VALUES ('{}')");
>
> // Check results.
> ResultSet result = stmt.executeQuery("SELECT intarray FROM testarray");
> assertTrue(result.next());
> assertEquals("{}", result.getString(1));
> int[] array = (int[])result.getArray(1).getArray();
> assertEquals(0, array.length);
> assertTrue(!result.next());
> result.close();
> }
>
> public void testIntArray() throws SQLException {
> stmt.executeUpdate("INSERT INTO testarray(intarray) VALUES ('{1,2,3,4}')");
>
> // Check results.
> ResultSet result = stmt.executeQuery("SELECT intarray[1], intarray[2], intarray[3], intarray[4] FROM testarray");
> assertTrue(result.next());
> assertEquals("backend element 1:", 1, result.getInt(1));
> assertEquals("backend element 2:", 2, result.getInt(2));
> assertEquals("backend element 3:", 3, result.getInt(3));
> assertEquals("backend element 4:", 4, result.getInt(4));
> assertTrue(!result.next());
> result.close();
>
> result = stmt.executeQuery("SELECT intarray FROM testarray");
> assertTrue(result.next());
> int[] driverArray = (int[])result.getArray(1).getArray();
> assertEquals("driver element 1:", 1, driverArray[0]);
> assertEquals("driver element 2:", 2, driverArray[1]);
> assertEquals("driver element 3:", 3, driverArray[2]);
> assertEquals("driver element 4:", 4, driverArray[3]);
> assertTrue(!result.next());
> result.close();
> }
>
> public void testEmptyStringArray() throws SQLException {
> stmt.executeUpdate("INSERT INTO testarray(strarray) VALUES ('{}')");
>
> // Check results.
> ResultSet result = stmt.executeQuery("SELECT strarray FROM testarray");
> assertTrue(result.next());
> assertEquals("{}", result.getString(1));
> Object[] array = (Object[])result.getArray(1).getArray();
> assertEquals(0, array.length);
> result.close();
> }
>
> public void testStringArray() throws SQLException {
> // this is a four-element string array with values:
> // 1: abcd
> // 2: "
> // 3: '
> // 4: \
> // array parser value: {abcd,"\"",',"\\"}
> // as a string literal: '{abcd,\"\\\"\",\',\"\\\\\"}'
> // as a Java string: "'{abcd,\\\"\\\\\\\"\\\",\\',\\\"\\\\\\\\\\\"}'"
> // (ow!)
>
> stmt.executeUpdate("INSERT INTO testarray(strarray) VALUES('{abcd,\\\"\\\\\\\"\\\",\\',\\\"\\\\\\\\\\\"}')");
>
> // Check results.
> ResultSet result = stmt.executeQuery("SELECT strarray[1], strarray[2], strarray[3], strarray[4] FROM testarray");
> assertTrue(result.next());
> assertEquals("backend element 1:", "abcd", result.getString(1));
> assertEquals("backend element 2:", "\"", result.getString(2));
> assertEquals("backend element 3:", "'", result.getString(3));
> assertEquals("backend element 4:", "\\", result.getString(4));
> assertTrue(!result.next());
> result.close();
>
> result = stmt.executeQuery("SELECT strarray FROM testarray");
> assertTrue(result.next());
> Object[] driverArray = (Object[])result.getArray(1).getArray();
> assertEquals("driver element 1:", "abcd", driverArray[0]);
> assertEquals("driver element 2:", "\"", driverArray[1]);
> assertEquals("driver element 3:", "'", driverArray[2]);
> assertEquals("driver element 4:", "\\", driverArray[3]);
> assertTrue(!result.next());
> result.close();
> }
>}
>
>
>------------------------------------------------------------------------
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 1: subscribe and unsubscribe commands go to majordomo(at)postgresql(dot)org
>
>

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Dima Tkach 2003-07-20 16:39:45 Re: Prepared Statements
Previous Message Oliver Jowett 2003-07-20 13:33:27 getArray() fails to handle some string array cases