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
