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

commit    f7d553c160ce791c15bb3fe9aab25ebce030cd0f
Author:   Kris Jurka <books@ejurka.com>
Date:     Thu Oct 14 10:09:15 2004 +0000

    In 8.0 arrays with non-standard bounds (beginning index != 1) the
    dimensions are returned in the data like so "[0:3]={0,1,2,3,4}".
    Add some code to ignore these bounds and match previous server
    version behavior.
    
    We could consider allowing these index values to be used though
    the JDBC spec says 1 is the first index.  I'm unsure if this
    would be a useful extension or if it would just cause confustion.


org/postgresql/jdbc2/AbstractJdbc2Array.java | 25 +++++++++++++++++++++++-- org/postgresql/test/jdbc2/ArrayTest.java | 20 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/org/postgresql/jdbc2/AbstractJdbc2Array.java b/org/postgresql/jdbc2/AbstractJdbc2Array.java index 5a03ab2c..c5dfe3b3 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2Array.java +++ b/org/postgresql/jdbc2/AbstractJdbc2Array.java @@ -93,7 +93,28 @@ public class AbstractJdbc2Array StringBuffer sbuf = new StringBuffer(); boolean foundOpen = false; boolean insideString = false; - for ( int i = 0; i < chars.length; i++ ) + + /** + * Starting with 8.0 non-standard (beginning index + * isn't 1) bounds the dimensions are returned in the + * data formatted like so "[0:3]={0,1,2,3,4}". + * Older versions simply do not return the bounds. + * + * Right now we ignore these bounds, but we could + * consider allowing these index values to be used + * even though the JDBC spec says 1 is the first + * index. I'm not sure what a client would like + * to see, so we just retain the old behavior. + */ + int startOffset = 0; + if (chars[0] == '[') { + while(chars[startOffset] != '=') { + startOffset++; + } + startOffset++; // skip = + } + + for ( int i = startOffset; i < chars.length; i++ ) { if ( chars[i] == '\\' ) //escape character that we need to skip @@ -101,7 +122,7 @@ public class AbstractJdbc2Array else if (!insideString && chars[i] == '{' ) { if ( foundOpen ) // Only supports 1-D arrays for now - throw org.postgresql.Driver.notImplemented(); + throw new PSQLException("Multi-dimensional arrays are currently not supported."); foundOpen = true; continue; } diff --git a/org/postgresql/test/jdbc2/ArrayTest.java b/org/postgresql/test/jdbc2/ArrayTest.java index 379d59b6..a0d960a7 100644 --- a/org/postgresql/test/jdbc2/ArrayTest.java +++ b/org/postgresql/test/jdbc2/ArrayTest.java @@ -162,5 +162,25 @@ public class ArrayTest extends TestCase assertEquals(2,intarr[1]); assertEquals(3,intarr[2]); } + + /** + * Starting with 8.0 non-standard (beginning index isn't 1) bounds + * the dimensions are returned in the data. The following should + * return "[0:3]={0,1,2,3,4}" when queried. Older versions simply + * do not return the bounds. + */ + public void testNonStandardBounds() throws SQLException { + Statement stmt = conn.createStatement(); + stmt.executeUpdate("INSERT INTO arrtest (intarr) VALUES ('{1,2,3}')"); + stmt.executeUpdate("UPDATE arrtest SET intarr[0] = 0"); + ResultSet rs = stmt.executeQuery("SELECT intarr FROM arrtest"); + assertTrue(rs.next()); + Array result = rs.getArray(1); + int intarr[] = (int[])result.getArray(); + assertEquals(4, intarr.length); + for (int i=0; i<intarr.length; i++) { + assertEquals(i, intarr[i]); + } + } } [parent: 4da04d1e8011]