X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 3B725D1B1AB for ; Fri, 9 Jul 2004 18:09:06 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 01326-06 for ; Fri, 9 Jul 2004 21:09:06 +0000 (GMT) Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by svr1.postgresql.org (Postfix) with SMTP id 248DBD1B19B for ; Fri, 9 Jul 2004 18:09:03 -0300 (ADT) Received: (qmail 26304 invoked from network); 9 Jul 2004 21:09:03 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.36?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 9 Jul 2004 21:09:03 -0000 Subject: patch for getXXX methods From: Dave Cramer Reply-To: pg@fastcrypt.com To: "pgsql-jdbc@postgresql.org" Content-Type: multipart/mixed; boundary="=-FtchMX+xHMR/BltZohWa" Organization: Cramer Consulting Message-Id: <1089407362.1508.292.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 Date: Fri, 09 Jul 2004 17:09:22 -0400 X-AntiVirus: scanned for viruses by AMaViS 0.2.1 (http://amavis.org/) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests=TO_ADDRESS_EQ_REAL X-Spam-Level: X-Archive-Number: 200407/70 X-Sequence-Number: 10454 --=-FtchMX+xHMR/BltZohWa Content-Type: text/plain Content-Transfer-Encoding: 7bit this patch addresses the issue of using getXXX recommended and secondary methods Please review and comment. Dave -- Dave Cramer 519 939 0336 ICQ # 14675561 --=-FtchMX+xHMR/BltZohWa Content-Disposition: attachment; filename=getxxx.patch Content-Type: text/x-patch; name=getxxx.patch; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Index: org/postgresql/test/jdbc2/ResultSetTest.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/test/jdbc2/ResultSetTest.java,v retrieving revision 1.18 diff -c -r1.18 ResultSetTest.java *** org/postgresql/test/jdbc2/ResultSetTest.java 29 Jun 2004 06:43:28 -0000 1.18 --- org/postgresql/test/jdbc2/ResultSetTest.java 9 Jul 2004 21:06:30 -0000 *************** *** 61,72 **** stmt.executeUpdate("INSERT INTO testnumeric VALUES('0.0')"); stmt.executeUpdate("INSERT INTO testnumeric VALUES('-1.0')"); stmt.executeUpdate("INSERT INTO testnumeric VALUES('1.2')"); stmt.executeUpdate("INSERT INTO testnumeric VALUES('99999.2')"); stmt.executeUpdate("INSERT INTO testnumeric VALUES('99999')"); - stmt.executeUpdate("INSERT INTO testnumeric VALUES('-2.5')"); stmt.executeUpdate("INSERT INTO testnumeric VALUES('-99999.2')"); stmt.executeUpdate("INSERT INTO testnumeric VALUES('-99999')"); stmt.close(); --- 61,90 ---- stmt.executeUpdate("INSERT INTO testnumeric VALUES('0.0')"); stmt.executeUpdate("INSERT INTO testnumeric VALUES('-1.0')"); stmt.executeUpdate("INSERT INTO testnumeric VALUES('1.2')"); + stmt.executeUpdate("INSERT INTO testnumeric VALUES('-2.5')"); stmt.executeUpdate("INSERT INTO testnumeric VALUES('99999.2')"); stmt.executeUpdate("INSERT INTO testnumeric VALUES('99999')"); stmt.executeUpdate("INSERT INTO testnumeric VALUES('-99999.2')"); stmt.executeUpdate("INSERT INTO testnumeric VALUES('-99999')"); + // Integer.MaxValue + stmt.execute("INSERT INTO testnumeric VALUES('2147483647')"); + + // Integer.MinValue + stmt.execute("INSERT INTO testnumeric VALUES('-2147483648')"); + + stmt.executeUpdate("INSERT INTO testnumeric VALUES('2147483648')"); + stmt.executeUpdate("INSERT INTO testnumeric VALUES('-2147483649')"); + + // Long.MaxValue + stmt.executeUpdate("INSERT INTO testnumeric VALUES('9223372036854775807')"); + + // Long.MinValue + stmt.executeUpdate("INSERT INTO testnumeric VALUES('-9223372036854775808')"); + + stmt.executeUpdate("INSERT INTO testnumeric VALUES('9223372036854775808')"); + stmt.executeUpdate("INSERT INTO testnumeric VALUES('-9223372036854775809')"); + stmt.close(); *************** *** 225,287 **** public void testgetByte() throws SQLException { ! ResultSet rs = con.createStatement().executeQuery("select * from testnumeric"); ! boolean thrown = false; ! assertTrue(rs.next()); ! assertEquals(1,rs.getByte(1)); ! assertTrue(rs.next()); ! assertEquals(0,rs.getByte(1)); ! assertTrue(rs.next()); ! assertEquals(-1,rs.getByte(1)); ! while (rs.next()) ! { ! thrown = false; ! try ! { ! rs.getByte(1); ! } ! catch (Exception e) ! { ! thrown = true; ! } ! if (!thrown) ! fail("Exception expected."); ! } ! } ! public void testgetShort() throws SQLException ! { ! ResultSet rs = con.createStatement().executeQuery("select * from testnumeric"); ! boolean thrown = false; ! assertTrue(rs.next()); ! assertEquals(1,rs.getShort(1)); ! assertTrue(rs.next()); ! assertEquals(0,rs.getShort(1)); ! assertTrue(rs.next()); ! assertEquals(-1,rs.getShort(1)); ! while (rs.next()) ! { ! thrown = false; ! try ! { ! rs.getShort(1); ! } ! catch (Exception e) ! { ! thrown = true; ! } ! if (!thrown) ! fail("Exception expected."); ! } ! } public void testParameters() throws SQLException { --- 243,445 ---- public void testgetByte() throws SQLException { ! ResultSet rs = con.createStatement().executeQuery( ! "select * from testnumeric"); ! boolean thrown = false; ! ! assertTrue(rs.next()); ! assertEquals(1, rs.getByte(1)); ! ! assertTrue(rs.next()); ! assertEquals(0, rs.getByte(1)); ! ! assertTrue(rs.next()); ! assertEquals( -1, rs.getByte(1)); ! ! assertTrue(rs.next()); ! assertEquals(1, rs.getByte(1)); ! ! assertTrue(rs.next()); ! assertEquals(-2, rs.getByte(1)); ! ! while (rs.next()) ! { ! thrown = false; ! try ! { ! rs.getByte(1); ! } ! catch (Exception e) ! { ! thrown = true; ! } ! if (!thrown) ! fail("Exception expected."); ! } ! rs.close(); ! } ! public void testgetShort() throws SQLException ! { ! ResultSet rs = con.createStatement().executeQuery( ! "select * from testnumeric"); ! boolean thrown = false; ! ! assertTrue(rs.next()); ! assertEquals(1, rs.getShort(1)); ! ! assertTrue(rs.next()); ! assertEquals(0, rs.getShort(1)); ! ! assertTrue(rs.next()); ! assertEquals( -1, rs.getShort(1)); ! ! assertTrue(rs.next()); ! assertEquals(1, rs.getShort(1)); ! ! assertTrue(rs.next()); ! assertEquals(-2, rs.getShort(1)); ! ! while (rs.next()) ! { ! thrown = false; ! try ! { ! rs.getShort(1); ! } ! catch (Exception e) ! { ! thrown = true; ! } ! if (!thrown) ! fail("Exception expected."); ! } ! rs.close(); ! } ! public void testgetInt() throws SQLException ! { ! ResultSet rs = con.createStatement().executeQuery( ! "select * from testnumeric"); ! boolean thrown = false; ! assertTrue(rs.next()); ! assertEquals(1, rs.getInt(1)); ! assertTrue(rs.next()); ! assertEquals(0, rs.getInt(1)); ! assertTrue(rs.next()); ! assertEquals( -1, rs.getInt(1)); ! assertTrue(rs.next()); ! assertEquals(1, rs.getInt(1)); ! assertTrue(rs.next()); ! assertEquals( -2, rs.getInt(1)); ! assertTrue(rs.next()); ! assertEquals(99999, rs.getInt(1)); ! assertTrue(rs.next()); ! assertEquals(99999, rs.getInt(1)); ! ! assertTrue(rs.next()); ! assertEquals(-99999, rs.getInt(1)); ! ! assertTrue(rs.next()); ! assertEquals(-99999, rs.getInt(1)); ! ! assertTrue(rs.next()); ! assertEquals(Integer.MAX_VALUE, rs.getInt(1)); ! ! assertTrue(rs.next()); ! assertEquals(Integer.MIN_VALUE, rs.getInt(1)); ! ! while (rs.next()) ! { ! thrown = false; ! try ! { ! rs.getInt(1); ! } ! catch (Exception e) ! { ! thrown = true; ! } ! if (!thrown) ! fail("Exception expected." + rs.getString(1)); ! } ! rs.close(); ! } ! ! public void testgetLong() throws SQLException ! { ! ResultSet rs = con.createStatement().executeQuery( ! "select * from testnumeric"); ! boolean thrown = false; ! ! assertTrue(rs.next()); ! assertEquals(1, rs.getLong(1)); ! ! assertTrue(rs.next()); ! assertEquals(0, rs.getLong(1)); ! ! assertTrue(rs.next()); ! assertEquals( -1, rs.getLong(1)); ! ! assertTrue(rs.next()); ! assertEquals(1, rs.getLong(1)); ! ! assertTrue(rs.next()); ! assertEquals( -2, rs.getLong(1)); ! ! assertTrue(rs.next()); ! assertEquals(99999, rs.getLong(1)); ! ! assertTrue(rs.next()); ! assertEquals(99999, rs.getLong(1)); ! ! assertTrue(rs.next()); ! assertEquals(-99999, rs.getLong(1)); ! ! assertTrue(rs.next()); ! assertEquals(-99999, rs.getLong(1)); ! ! assertTrue(rs.next()); ! assertEquals( ((long)Integer.MAX_VALUE), rs.getLong(1)); ! ! assertTrue(rs.next()); ! assertEquals( ((long)Integer.MIN_VALUE), rs.getLong(1)); ! ! assertTrue(rs.next()); ! assertEquals( ((long)Integer.MAX_VALUE)+1, rs.getLong(1)); ! ! assertTrue(rs.next()); ! assertEquals( ((long)Integer.MIN_VALUE)-1, rs.getLong(1)); ! ! assertTrue(rs.next()); ! assertEquals( Long.MAX_VALUE, rs.getLong(1)); ! ! assertTrue(rs.next()); ! assertEquals( Long.MIN_VALUE, rs.getLong(1)); ! ! while (rs.next()) ! { ! thrown = false; ! try ! { ! rs.getLong(1); ! } ! catch (Exception e) ! { ! thrown = true; ! } ! if (!thrown) ! fail("Exception expected." + rs.getString(1) ); ! } ! rs.close(); ! } public void testParameters() throws SQLException { Index: org/postgresql/jdbc1/AbstractJdbc1ResultSet.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java,v retrieving revision 1.35 diff -c -r1.35 AbstractJdbc1ResultSet.java *** org/postgresql/jdbc1/AbstractJdbc1ResultSet.java 29 Jun 2004 06:43:26 -0000 1.35 --- org/postgresql/jdbc1/AbstractJdbc1ResultSet.java 9 Jul 2004 21:06:30 -0000 *************** *** 28,33 **** --- 28,34 ---- import org.postgresql.util.PGtokenizer; import org.postgresql.util.PSQLException; import org.postgresql.util.PSQLState; + import java.math.BigInteger; public abstract class AbstractJdbc1ResultSet implements BaseResultSet, org.postgresql.PGRefCursorResultSet { *************** *** 61,92 **** public class CursorResultHandler implements ResultHandler { private SQLException error; ! public void handleResultRows(Query fromQuery, Field[] fields, Vector tuples, ResultCursor cursor) { AbstractJdbc1ResultSet.this.rows = tuples; AbstractJdbc1ResultSet.this.cursor = cursor; } ! public void handleCommandStatus(String status, int updateCount, long insertOID) { handleError(new SQLException("unexpected command status")); } ! public void handleWarning(SQLWarning warning) { AbstractJdbc1ResultSet.this.addWarning(warning); } ! public void handleError(SQLException newError) { if (error == null) error = newError; else error.setNextException(newError); } ! public void handleCompletion() throws SQLException { if (error != null) throw error; } ! }; public AbstractJdbc1ResultSet(Query originalQuery, BaseStatement statement, --- 62,93 ---- public class CursorResultHandler implements ResultHandler { private SQLException error; ! public void handleResultRows(Query fromQuery, Field[] fields, Vector tuples, ResultCursor cursor) { AbstractJdbc1ResultSet.this.rows = tuples; AbstractJdbc1ResultSet.this.cursor = cursor; } ! public void handleCommandStatus(String status, int updateCount, long insertOID) { handleError(new SQLException("unexpected command status")); } ! public void handleWarning(SQLWarning warning) { AbstractJdbc1ResultSet.this.addWarning(warning); } ! public void handleError(SQLException newError) { if (error == null) error = newError; else error.setNextException(newError); } ! public void handleCompletion() throws SQLException { if (error != null) throw error; } ! }; public AbstractJdbc1ResultSet(Query originalQuery, BaseStatement statement, *************** *** 146,152 **** { if (rows == null) throw new PSQLException("postgresql.con.closed", PSQLState.CONNECTION_DOES_NOT_EXIST); ! if (onInsertRow) throw new PSQLException("postgresql.res.oninsertrow"); --- 147,153 ---- { if (rows == null) throw new PSQLException("postgresql.con.closed", PSQLState.CONNECTION_DOES_NOT_EXIST); ! if (onInsertRow) throw new PSQLException("postgresql.res.oninsertrow"); *************** *** 231,261 **** if (s != null ) { try { ! switch(getSQLType(columnIndex)) ! { ! case Types.NUMERIC: ! case Types.REAL: ! case Types.DOUBLE: ! case Types.FLOAT: ! case Types.DECIMAL: ! int loc = s.indexOf("."); ! if (loc!=-1 && Integer.parseInt(s.substring(loc+1,s.length()))==0) ! { ! s = s.substring(0,loc); ! } ! break; ! case Types.CHAR: ! s = s.trim(); ! break; ! } ! if ( s.length() == 0 ) return 0; ! return Byte.parseByte(s); } catch (NumberFormatException e) { ! throw new PSQLException("postgresql.res.badbyte", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } } return 0; // SQL NULL --- 232,268 ---- if (s != null ) { + s= s.trim(); + if ( s.length() == 0 ) return 0; try { ! // try the optimal parse ! return Byte.parseByte(s); } catch (NumberFormatException e) { ! // didn't work, assume the column is not a byte ! try ! { ! double d = Double.parseDouble(s); ! // check the size ! if (d > Byte.MAX_VALUE || d < Byte.MIN_VALUE) ! { ! throw new PSQLException("postgresql.res.badbyte", ! PSQLState. ! NUMERIC_VALUE_OUT_OF_RANGE, s); ! } ! else ! { ! return (byte)d; ! } ! } ! catch( NumberFormatException ex ) ! { ! throw new PSQLException("postgresql.res.badbyte", ! PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, ! s); ! } } } return 0; // SQL NULL *************** *** 267,296 **** if (s != null) { try { - switch(getSQLType(columnIndex)) - { - case Types.NUMERIC: - case Types.REAL: - case Types.DOUBLE: - case Types.FLOAT: - case Types.DECIMAL: - int loc = s.indexOf("."); - if (loc!=-1 && Integer.parseInt(s.substring(loc+1,s.length()))==0) - { - s = s.substring(0,loc); - } - break; - case Types.CHAR: - s = s.trim(); - break; - } return Short.parseShort(s); } catch (NumberFormatException e) { ! throw new PSQLException("postgresql.res.badshort", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } } return 0; // SQL NULL --- 274,308 ---- if (s != null) { + s = s.trim(); try { return Short.parseShort(s); } catch (NumberFormatException e) { ! try ! { ! double d = Double.parseDouble(s); ! // check the size ! if (d > Short.MAX_VALUE || d < Short.MIN_VALUE) ! { ! throw new PSQLException("postgresql.res.badshort", ! PSQLState. ! NUMERIC_VALUE_OUT_OF_RANGE, s); ! } ! else ! { ! return (short)d; ! } ! ! } ! catch ( NumberFormatException ne ) ! { ! throw new PSQLException("postgresql.res.badshort", ! PSQLState. ! NUMERIC_VALUE_OUT_OF_RANGE, s); ! } } } return 0; // SQL NULL *************** *** 634,647 **** wasNullFlag = true; return null; } ! Object result = internalGetObject(columnIndex, field); if (result != null) return result; return connection.getObject(getPGType(columnIndex), getString(columnIndex)); } ! protected Object internalGetObject(int columnIndex, Field field) throws SQLException { switch (getSQLType(columnIndex)) { --- 646,659 ---- wasNullFlag = true; return null; } ! Object result = internalGetObject(columnIndex, field); if (result != null) return result; return connection.getObject(getPGType(columnIndex), getString(columnIndex)); } ! protected Object internalGetObject(int columnIndex, Field field) throws SQLException { switch (getSQLType(columnIndex)) { *************** *** 811,821 **** } catch (NumberFormatException e) { ! throw new PSQLException ("postgresql.res.badint", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } } return 0; // SQL NULL } public static long toLong(String s) throws SQLException { --- 823,854 ---- } catch (NumberFormatException e) { ! try ! { ! double d = Double.parseDouble(s); ! if( d > Integer.MAX_VALUE || d < Integer.MIN_VALUE ) ! { ! throw new PSQLException("postgresql.res.badint", ! PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, ! s); ! } ! else ! { ! return (int)d; ! } ! } ! catch( NumberFormatException ne ) ! { ! throw new PSQLException("postgresql.res.badint", ! PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, ! s); ! } } } return 0; // SQL NULL } + private static BigInteger LONGMAX = new BigInteger(Long.toString(Long.MAX_VALUE)); + private static BigInteger LONGMIN = new BigInteger(Long.toString(Long.MIN_VALUE)); public static long toLong(String s) throws SQLException { *************** *** 828,834 **** } catch (NumberFormatException e) { ! throw new PSQLException ("postgresql.res.badlong", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } } return 0; // SQL NULL --- 861,888 ---- } catch (NumberFormatException e) { ! try ! { ! BigDecimal n = new BigDecimal(s); ! BigInteger i = n.toBigInteger(); ! int gt = i.compareTo(LONGMAX); ! int lt = i.compareTo(LONGMIN); ! ! if ( gt > 0 || lt < 0 ) ! { ! throw new PSQLException("postgresql.res.badint", ! PSQLState. ! NUMERIC_VALUE_OUT_OF_RANGE, ! s); ! } ! return i.longValue(); ! } ! catch( NumberFormatException ne ) ! { ! throw new PSQLException("postgresql.res.badint", ! PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, ! s); ! } } } return 0; // SQL NULL *************** *** 846,852 **** } catch (NumberFormatException e) { ! throw new PSQLException ("postgresql.res.badbigdec", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } if (scale == -1) return val; --- 900,907 ---- } catch (NumberFormatException e) { ! throw new PSQLException("postgresql.res.badbigdec", ! PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } if (scale == -1) return val; *************** *** 856,862 **** } catch (ArithmeticException e) { ! throw new PSQLException ("postgresql.res.badbigdec", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } } return null; // SQL NULL --- 911,918 ---- } catch (ArithmeticException e) { ! throw new PSQLException("postgresql.res.badbigdec", ! PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } } return null; // SQL NULL *************** *** 873,879 **** } catch (NumberFormatException e) { ! throw new PSQLException ("postgresql.res.badfloat", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } } return 0; // SQL NULL --- 929,936 ---- } catch (NumberFormatException e) { ! throw new PSQLException("postgresql.res.badfloat", ! PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } } return 0; // SQL NULL *************** *** 890,896 **** } catch (NumberFormatException e) { ! throw new PSQLException ("postgresql.res.baddouble", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } } return 0; // SQL NULL --- 947,954 ---- } catch (NumberFormatException e) { ! throw new PSQLException("postgresql.res.baddouble", ! PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, s); } } return 0; // SQL NULL --=-FtchMX+xHMR/BltZohWa--