Commit 66d00417 authored by Barry Lind's avatar Barry Lind

Applied jdbc patch from Kim Ho at RedHat to improve boolean support, as well

as some additional regression tests for this an other recent changes.

 Modified Files:
 	jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java
 	jdbc/org/postgresql/test/jdbc2/DateTest.java
 	jdbc/org/postgresql/test/jdbc2/ResultSetTest.java
 	jdbc/org/postgresql/test/jdbc2/TimeTest.java
 	jdbc/org/postgresql/test/jdbc2/TimestampTest.java
parent f03d2284
......@@ -9,7 +9,7 @@
* Copyright (c) 2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1ResultSet.java,v 1.20 2003/09/18 04:09:02 barry Exp $
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1ResultSet.java,v 1.21 2003/09/22 04:54:59 barry Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -201,7 +201,11 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet
case Types.DOUBLE:
case Types.FLOAT:
case Types.DECIMAL:
s = (s.indexOf(".")==-1) ? s : s.substring(0,s.indexOf("."));
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();
......@@ -232,7 +236,11 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet
case Types.DOUBLE:
case Types.FLOAT:
case Types.DECIMAL:
s = (s.indexOf(".")==-1) ? s : s.substring(0,s.indexOf("."));
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();
......
......@@ -5,7 +5,7 @@ import junit.framework.TestCase;
import java.sql.*;
/*
* $Id: DateTest.java,v 1.5 2002/08/14 20:35:40 barry Exp $
* $Id: DateTest.java,v 1.6 2003/09/22 04:55:00 barry Exp $
*
* Some simple tests based on problems reported by users. Hopefully these will
* help prevent previous problems from re-occuring ;-)
......@@ -15,6 +15,7 @@ public class DateTest extends TestCase
{
private Connection con;
private boolean testingSetDate = false;
public DateTest(String name)
{
......@@ -46,11 +47,23 @@ public class DateTest extends TestCase
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1970-06-02'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1999-08-11'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'2001-02-13'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1950-04-02'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1970-11-30'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1988-01-01'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'2003-07-09'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1934-02-28'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1969-04-03'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1982-08-03'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'2012-03-15'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1912-05-01'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1971-12-15'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1984-12-03'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'2000-01-01'")));
/* dateTest() contains all of the tests */
dateTest();
assertEquals(4, stmt.executeUpdate("DELETE FROM " + "testdate"));
assertEquals(16, stmt.executeUpdate("DELETE FROM " + "testdate"));
stmt.close();
}
catch (Exception ex)
......@@ -80,13 +93,64 @@ public class DateTest extends TestCase
ps.setDate(1, makeDate(2001, 2, 13));
assertEquals(1, ps.executeUpdate());
ps.setObject(1, java.sql.Timestamp.valueOf("1950-04-02 12:00:00"), java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, java.sql.Timestamp.valueOf("1970-11-30 3:00:00"), java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, java.sql.Timestamp.valueOf("1988-1-1 13:00:00"), java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, java.sql.Timestamp.valueOf("2003-07-09 12:00:00"), java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, "1934-02-28", java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, "1969-04-3", java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, "1982-08-03", java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, "2012-3-15", java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, java.sql.Date.valueOf("1912-5-1"), java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, java.sql.Date.valueOf("1971-12-15"), java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, java.sql.Date.valueOf("1984-12-03"), java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, java.sql.Date.valueOf("2000-1-1"), java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, "1944-4-04-01", java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, "1970-01-1-10", java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, "1982-12-14+13", java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, "2010-08-3+05", java.sql.Types.DATE);
assertEquals(1, ps.executeUpdate());
ps.close();
// Need to set a flag so that the method knows there is an extra test.
testingSetDate = true;
// Fall through helper
dateTest();
testingSetDate = false;
assertEquals(4, stmt.executeUpdate("DELETE FROM testdate"));
assertEquals(20, stmt.executeUpdate("DELETE FROM testdate"));
stmt.close();
}
catch (Exception ex)
......@@ -127,6 +191,122 @@ public class DateTest extends TestCase
assertNotNull(d);
assertEquals(d, makeDate(2001, 2, 13));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(1950, 4, 2));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(1970, 11, 30));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(1988, 1, 1));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(2003, 7, 9));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(1934, 2, 28));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(1969, 4, 3));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(1982, 8, 3));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(2012, 3, 15));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(1912, 5, 1));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(1971, 12, 15));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(1984, 12, 3));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(2000, 1, 1));
//now we have to convert the date, cause I fed it a timezone. IF it used it. hence the check
if (testingSetDate)
{
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
java.sql.Date tmpDate = java.sql.Date.valueOf("1944-4-4");
int localoffset = java.util.Calendar.getInstance().getTimeZone().getRawOffset();
if (java.util.Calendar.getInstance().getTimeZone().inDaylightTime(tmpDate))
{
localoffset += 60 * 60 * 1000;
}
int Dateoffset = 60 * 60 * 1000;
tmpDate.setTime(tmpDate.getTime() + Dateoffset + localoffset);
assertEquals(d, makeDate(tmpDate.getYear() + 1900, tmpDate.getMonth()+1, tmpDate.getDate()));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
tmpDate = java.sql.Date.valueOf("1970-1-1");
localoffset = java.util.Calendar.getInstance().getTimeZone().getRawOffset();
if (java.util.Calendar.getInstance().getTimeZone().inDaylightTime(tmpDate))
{
localoffset += 60 * 60 * 1000;
}
Dateoffset = 10 * 60 * 60 * 1000;
tmpDate.setTime(tmpDate.getTime() + Dateoffset + localoffset);
assertEquals(d, makeDate(tmpDate.getYear() + 1900, tmpDate.getMonth()+1, tmpDate.getDate()));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
tmpDate = java.sql.Date.valueOf("1982-12-14");
localoffset = java.util.Calendar.getInstance().getTimeZone().getRawOffset();
if (java.util.Calendar.getInstance().getTimeZone().inDaylightTime(tmpDate))
{
localoffset += 60 * 60 * 1000;
}
Dateoffset = -13 * 60 * 60 * 1000;
tmpDate.setTime(tmpDate.getTime() + Dateoffset + localoffset);
assertEquals(d, makeDate(tmpDate.getYear() + 1900, tmpDate.getMonth()+1, tmpDate.getDate()));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
tmpDate = java.sql.Date.valueOf("2010-08-03");
localoffset = java.util.Calendar.getInstance().getTimeZone().getRawOffset();
if (java.util.Calendar.getInstance().getTimeZone().inDaylightTime(tmpDate))
{
localoffset += 60 * 60 * 1000;
}
Dateoffset = -5 * 60 * 60 * 1000;
tmpDate.setTime(tmpDate.getTime() + Dateoffset + localoffset);
assertEquals(d, makeDate(tmpDate.getYear() + 1900, tmpDate.getMonth()+1, tmpDate.getDate()));
}
assertTrue(!rs.next());
rs.close();
......
......@@ -38,6 +38,35 @@ public class ResultSetTest extends TestCase
TestUtil.createTable(con, "testint", "a int");
stmt.executeUpdate("INSERT INTO testint VALUES (12345)");
TestUtil.createTable(con, "testbool", "a boolean");
TestUtil.createTable(con, "testbit", "a bit");
TestUtil.createTable(con, "testboolstring", "a varchar(30)");
stmt.executeUpdate("INSERT INTO testboolstring VALUES('true')");
stmt.executeUpdate("INSERT INTO testboolstring VALUES('false')");
stmt.executeUpdate("INSERT INTO testboolstring VALUES('t')");
stmt.executeUpdate("INSERT INTO testboolstring VALUES('f')");
stmt.executeUpdate("INSERT INTO testboolstring VALUES('1.0')");
stmt.executeUpdate("INSERT INTO testboolstring VALUES('0.0')");
stmt.executeUpdate("INSERT INTO testboolstring VALUES('TRUE')");
stmt.executeUpdate("INSERT INTO testboolstring VALUES('this is not true')");
TestUtil.createTable(con, "testnumeric", "a numeric");
stmt.executeUpdate("INSERT INTO testnumeric VALUES('1.0')");
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();
stmt.close();
}
......@@ -47,6 +76,10 @@ public class ResultSetTest extends TestCase
TestUtil.dropTable(con, "testrs");
TestUtil.dropTable(con, "teststring");
TestUtil.dropTable(con, "testint");
TestUtil.dropTable(con, "testbool");
TestUtil.dropTable(con, "testbit");
TestUtil.dropTable(con, "testboolstring");
TestUtil.dropTable(con, "testnumeric");
TestUtil.closeDB(con);
}
......@@ -114,4 +147,139 @@ public class ResultSetTest extends TestCase
assertEquals(rs.getString(1), "12");
assertEquals(new String(rs.getBytes(1)), "12");
}
public void booleanTests(boolean useServerPrepare) throws Exception
{
java.sql.PreparedStatement pstmt = con.prepareStatement("insert into testbool values (?)");
if (useServerPrepare)
((org.postgresql.PGStatement)pstmt).setUseServerPrepare(true);
pstmt.setObject(1, new Float(0), java.sql.Types.BIT);
pstmt.executeUpdate();
pstmt.setObject(1, new Float(1), java.sql.Types.BIT);
pstmt.executeUpdate();
pstmt.setObject(1, "False", java.sql.Types.BIT);
pstmt.executeUpdate();
pstmt.setObject(1, "True", java.sql.Types.BIT);
pstmt.executeUpdate();
ResultSet rs = con.createStatement().executeQuery("select * from testbool");
for (int i = 0; i<2; i++)
{
assertTrue(rs.next());
assertEquals(false, rs.getBoolean(1));
assertTrue(rs.next());
assertEquals(true, rs.getBoolean(1));
}
pstmt = con.prepareStatement("insert into testbit values (?)");
pstmt.setObject(1, new Float(0), java.sql.Types.BIT);
pstmt.executeUpdate();
pstmt.setObject(1, new Float(1), java.sql.Types.BIT);
pstmt.executeUpdate();
pstmt.setObject(1, "false", java.sql.Types.BIT);
pstmt.executeUpdate();
pstmt.setObject(1, "true", java.sql.Types.BIT);
pstmt.executeUpdate();
rs = con.createStatement().executeQuery("select * from testbit");
for (int i = 0;i<2; i++)
{
assertTrue(rs.next());
assertEquals(false, rs.getBoolean(1));
assertTrue(rs.next());
assertEquals(true, rs.getBoolean(1));
}
rs = con.createStatement().executeQuery("select * from testboolstring");
for (int i = 0;i<4; i++)
{
assertTrue(rs.next());
assertEquals(true, rs.getBoolean(1));
assertTrue(rs.next());
assertEquals(false, rs.getBoolean(1));
}
}
public void testBoolean() throws Exception
{
booleanTests(true);
booleanTests(false);
}
public void testgetByte() throws Exception
{
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));
int count = 3;
while (rs.next())
{
try
{
rs.getByte(1);
}
catch (Exception e)
{
thrown = true;
}
if (!thrown)
fail("Exception expected.");
count++;
}
if (count != 9)
fail("Exception expected.");
}
public void testgetShort() throws Exception
{
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));
int count = 3;
while (rs.next())
{
try
{
rs.getShort(1);
}
catch (Exception e)
{
thrown = true;
}
if (!thrown)
fail("Exception expected.");
count++;
}
if (count != 9)
fail("Exception expected.");
}
}
......@@ -5,7 +5,7 @@ import junit.framework.TestCase;
import java.sql.*;
/*
* $Id: TimeTest.java,v 1.5 2002/08/14 20:35:40 barry Exp $
* $Id: TimeTest.java,v 1.6 2003/09/22 04:55:00 barry Exp $
*
* Some simple tests based on problems reported by users. Hopefully these will
* help prevent previous problems from re-occuring ;-)
......@@ -15,6 +15,7 @@ public class TimeTest extends TestCase
{
private Connection con;
private boolean testSetTime = false;
public TimeTest(String name)
{
......@@ -44,11 +45,18 @@ public class TimeTest extends TestCase
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'01:02:03'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'23:59:59'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'12:00:00'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'05:15:21'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'16:21:51'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'12:15:12'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'22:12:01'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'08:46:44'")));
// Fall through helper
timeTest();
assertEquals(2, stmt.executeUpdate("DELETE FROM testtime"));
assertEquals(8, stmt.executeUpdate("DELETE FROM testtime"));
stmt.close();
}
catch (Exception ex)
......@@ -72,11 +80,38 @@ public class TimeTest extends TestCase
ps.setTime(1, makeTime(23, 59, 59));
assertEquals(1, ps.executeUpdate());
ps.setObject(1, java.sql.Time.valueOf("12:00:00"), java.sql.Types.TIME);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, java.sql.Time.valueOf("05:15:21"), java.sql.Types.TIME);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, java.sql.Time.valueOf("16:21:51"), java.sql.Types.TIME);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, java.sql.Time.valueOf("12:15:12"), java.sql.Types.TIME);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, "22:12:1", java.sql.Types.TIME);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, "8:46:44", java.sql.Types.TIME);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, "5:1:2-03", java.sql.Types.TIME);
assertEquals(1, ps.executeUpdate());
ps.setObject(1, "23:59:59+11", java.sql.Types.TIME);
assertEquals(1, ps.executeUpdate());
// Need to let the test know this one has extra test cases.
testSetTime = true;
// Fall through helper
timeTest();
testSetTime = false;
assertEquals(2, stmt.executeUpdate("DELETE FROM testtime"));
assertEquals(10, stmt.executeUpdate("DELETE FROM testtime"));
stmt.close();
ps.close();
}
......@@ -108,6 +143,66 @@ public class TimeTest extends TestCase
assertNotNull(t);
assertEquals(makeTime(23, 59, 59), t);
assertTrue(rs.next());
t = rs.getTime(1);
assertNotNull(t);
assertEquals(makeTime(12, 0, 0), t);
assertTrue(rs.next());
t = rs.getTime(1);
assertNotNull(t);
assertEquals(makeTime(5, 15, 21), t);
assertTrue(rs.next());
t = rs.getTime(1);
assertNotNull(t);
assertEquals(makeTime(16, 21, 51), t);
assertTrue(rs.next());
t = rs.getTime(1);
assertNotNull(t);
assertEquals(makeTime(12, 15, 12), t);
assertTrue(rs.next());
t = rs.getTime(1);
assertNotNull(t);
assertEquals(makeTime(22, 12, 1), t);
assertTrue(rs.next());
t = rs.getTime(1);
assertNotNull(t);
assertEquals(makeTime(8, 46, 44), t);
// If we're checking for timezones.
if (testSetTime)
{
assertTrue(rs.next());
t = rs.getTime(1);
assertNotNull(t);
java.sql.Time tmpTime = java.sql.Time.valueOf("5:1:2");
int localoffset = java.util.Calendar.getInstance().getTimeZone().getRawOffset();
if (java.util.Calendar.getInstance().getTimeZone().inDaylightTime(tmpTime))
{
localoffset += 60 * 60 * 1000;
}
int Timeoffset = 3 * 60 * 60 * 1000;
tmpTime.setTime(tmpTime.getTime() + Timeoffset + localoffset);
assertEquals(t, makeTime(tmpTime.getHours(), tmpTime.getMinutes(), tmpTime.getSeconds()));
assertTrue(rs.next());
t = rs.getTime(1);
assertNotNull(t);
tmpTime= java.sql.Time.valueOf("23:59:59");
localoffset = java.util.Calendar.getInstance().getTimeZone().getRawOffset();
if (java.util.Calendar.getInstance().getTimeZone().inDaylightTime(tmpTime))
{
localoffset += 60 * 60 * 1000;
}
Timeoffset = -11 * 60 * 60 * 1000;
tmpTime.setTime(tmpTime.getTime() + Timeoffset + localoffset);
assertEquals(t, makeTime(tmpTime.getHours(), tmpTime.getMinutes(), tmpTime.getSeconds()));
}
assertTrue(! rs.next());
rs.close();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment