Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 7D8C2B5DC02 for ; Wed, 28 Sep 2011 15:43:09 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 32158-07 for ; Wed, 28 Sep 2011 18:43:02 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from auth-smtp.nebula.fi (auth-smtp.nebula.fi [217.30.180.105]) by mail.postgresql.org (Postfix) with ESMTP id 5E7BBB5DC01 for ; Wed, 28 Sep 2011 15:43:00 -0300 (ADT) Received: from quadvadis.localdomain (gw.nitor.fi [83.145.228.24]) (authenticated bits=0) by auth-smtp.nebula.fi (8.13.4/8.13.4) with ESMTP id p8SIgtKD018902 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 28 Sep 2011 21:42:55 +0300 Message-ID: <4E836AB2.9030201@nitorcreations.com> Date: Wed, 28 Sep 2011 21:42:58 +0300 From: Mikko Tiihonen User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:6.0.2) Gecko/20110906 Thunderbird/6.0.2 MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Collection of small fixes to binary transfer code and unit tests Content-Type: multipart/mixed; boundary="------------050500040301050900050409" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.001 tagged_above=-10 required=5 tests=BAYES_20=-0.001 X-Spam-Level: X-Archive-Number: 201109/127 X-Sequence-Number: 22245 This is a multi-part message in MIME format. --------------050500040301050900050409 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hi, I just tried to run the unit tests with forcebinary=true and preparethreshold=1 and got 10 new failures. Of them 3 are real bugs (corner cases, but still bugs), rest are just problems in tests. So here are some patches to fix them: binary-force-harder-tests.patch - forces prepare threshold to 1 when forcebinary=true and thus makes the bugs visible binary-fix-some-serverprepared-tests-with-force.patch - the previous patch forcing prepare threshold of 1 breaks 2 tests that test disabling of preparethreshold - just skip part of the tests when forcebinary=true binary-fix-maxfieldlength-test.patch - test used getBytes on integer field which returns different bytes for txt vs bin transfers - fixes the unit test to cope with both bin and txt encoding binary-fix-date-infinity.patch (bug) - the backend sends a smaller infinity value for DATE than TIMESTAMP fields (4 bytes vs 8 bytes) - to ensure compatibility with text protocol convert smaller infinity values to larger infinity values when receiving DATE from backend binary-fix-arrays-with-zero-dimensions.patch (bug) - the new binary code for arrays did not handle arrays with dimension of zero - fix it and add a new test case for array.getResultSet() binary-fix-batch-with-generated-keys.patch (bug) - when executing batch with generated keys returned for each insert the same Query object is shared by the statements, and along with it the field definitions - when the prepare threshold is exceeded during the batch execution the first row of generated keys return text values and latter rows binary values - to fix we detect the situation and force a separate describe round-trip when batch is about to exceed the prepare threshold limit so that all generated in the same result set will be binary binary-fix-timezonetests.patch - based on my patch from 2007 to make the timezonetests 'fair' so that binary transfer has a chance of passing them - some tests in TimezoneTest never actually exercised the correct code paths because they just used getString which just returns the text from the server and does not cause the jdbc driver to try to parse the output - it is not possible in binary transfer mode for the jdbc driver to generate the exactly same getString output for all time values than what the server does - instead it uses the toString() method of the official java.sql classes - fixes the testSetTimestampOnTime, testSetTimestamp, testSetDate, testSetTime to use proper getTime/getTimestamp etc methods when fetching columns instead of relying on the getString - also changes the code verifying that inserts worked correctly to request the server for the actual values in the database casted to text type so that both binary and text transfer get the same results -Mikko --------------050500040301050900050409 Content-Type: text/plain; name="binary-force-harder-tests.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="binary-force-harder-tests.patch" Index: org/postgresql/jdbc2/AbstractJdbc2Statement.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java,v retrieving revision 1.126 diff -u -r1.126 AbstractJdbc2Statement.java --- org/postgresql/jdbc2/AbstractJdbc2Statement.java 26 Sep 2011 12:52:30 -0000 1.126 +++ org/postgresql/jdbc2/AbstractJdbc2Statement.java 28 Sep 2011 17:41:50 -0000 @@ -2588,6 +2588,9 @@ public void setPrepareThreshold(int newThreshold) throws SQLException { checkClosed(); + + if (ForceBinaryTransfers) + newThreshold = 1; if (newThreshold < 0) newThreshold = 0; --------------050500040301050900050409 Content-Type: text/plain; name="binary-fix-some-serverprepared-tests-with-force.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="binary-fix-some-serverprepared-tests-with-force.patch" Index: org/postgresql/test/jdbc2/ServerPreparedStmtTest.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java,v retrieving revision 1.19 diff -u -r1.19 ServerPreparedStmtTest.java --- org/postgresql/test/jdbc2/ServerPreparedStmtTest.java 22 Sep 2011 12:53:25 -0000 1.19 +++ org/postgresql/test/jdbc2/ServerPreparedStmtTest.java 25 Sep 2011 13:15:20 -0000 @@ -10,6 +10,7 @@ package org.postgresql.test.jdbc2; import org.postgresql.PGStatement; +import org.postgresql.jdbc2.AbstractJdbc2Statement; import org.postgresql.test.TestUtil; import java.sql.Connection; import java.sql.PreparedStatement; @@ -95,7 +96,9 @@ rs.close(); //Verify that using the statement still works after turning off prepares - + if (AbstractJdbc2Statement.ForceBinaryTransfers) { + return; + } ((PGStatement)pstmt).setUseServerPrepare(false); assertTrue(!((PGStatement)pstmt).isUseServerPrepare()); @@ -127,6 +130,9 @@ rs.close(); //Verify that using the statement still works after turning off prepares + if (AbstractJdbc2Statement.ForceBinaryTransfers) { + return; + } ((PGStatement)pstmt).setUseServerPrepare(false); assertTrue(!((PGStatement)pstmt).isUseServerPrepare()); --------------050500040301050900050409 Content-Type: text/plain; name="binary-fix-maxfieldlength-test.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="binary-fix-maxfieldlength-test.patch" Index: org/postgresql/test/jdbc2/ResultSetTest.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/test/jdbc2/ResultSetTest.java,v retrieving revision 1.31 diff -u -r1.31 ResultSetTest.java --- org/postgresql/test/jdbc2/ResultSetTest.java 2 Aug 2011 13:50:29 -0000 1.31 +++ org/postgresql/test/jdbc2/ResultSetTest.java 25 Sep 2011 11:28:49 -0000 @@ -171,7 +171,8 @@ //it should apply only to binary and char/varchar columns rs.next(); assertEquals("12345", rs.getString(1)); - assertEquals("12345", new String(rs.getBytes(1))); + // getBytes returns 5 bytes for txt transfer, 4 for bin transfer + assertTrue(rs.getBytes(1).length >= 4); //max should apply to the following since the column is //a varchar column --------------050500040301050900050409 Content-Type: text/plain; name="binary-fix-date-infinity.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="binary-fix-date-infinity.patch" Index: org/postgresql/PGStatement.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/PGStatement.java,v retrieving revision 1.16 diff -u -r1.16 PGStatement.java --- org/postgresql/PGStatement.java 2 Aug 2011 13:40:12 -0000 1.16 +++ org/postgresql/PGStatement.java 25 Sep 2011 12:42:43 -0000 @@ -26,6 +26,8 @@ // -infinity / infinity representation in Java public static final long DATE_POSITIVE_INFINITY = 9223372036825200000l; public static final long DATE_NEGATIVE_INFINITY = -9223372036832400000l; + public static final long DATE_POSITIVE_SMALLER_INFINITY = 185543533774800000l; + public static final long DATE_NEGATIVE_SMALLER_INFINITY = -185543533774800000l; /** Index: org/postgresql/jdbc2/TimestampUtils.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc2/TimestampUtils.java,v retrieving revision 1.25 diff -u -r1.25 TimestampUtils.java --- org/postgresql/jdbc2/TimestampUtils.java 22 Sep 2011 12:53:25 -0000 1.25 +++ org/postgresql/jdbc2/TimestampUtils.java 25 Sep 2011 12:42:43 -0000 @@ -657,6 +657,13 @@ long secs = toJavaSecs(days * 86400L); long millis = secs * 1000L; int offset = tz.getOffset(millis); + if (millis <= PGStatement.DATE_NEGATIVE_SMALLER_INFINITY) { + millis = PGStatement.DATE_NEGATIVE_INFINITY; + offset = 0; + } else if (millis >= PGStatement.DATE_POSITIVE_SMALLER_INFINITY) { + millis = PGStatement.DATE_POSITIVE_INFINITY; + offset = 0; + } return new Date(millis - offset); } --------------050500040301050900050409 Content-Type: text/plain; name="binary-fix-arrays-with-zero-dimensions.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="binary-fix-arrays-with-zero-dimensions.patch" Index: org/postgresql/jdbc2/AbstractJdbc2Array.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Array.java,v retrieving revision 1.28 diff -u -r1.28 AbstractJdbc2Array.java --- org/postgresql/jdbc2/AbstractJdbc2Array.java 27 Sep 2011 11:15:23 -0000 1.28 +++ org/postgresql/jdbc2/AbstractJdbc2Array.java 27 Sep 2011 16:12:35 -0000 @@ -183,7 +183,10 @@ dims[d] = ByteConverter.int4(fieldBytes, pos); pos += 4; /*int lbound = ByteConverter.int4(fieldBytes, pos);*/ pos += 4; } - if (count > 0 && dimensions > 0) { + if (dimensions == 0) { + return java.lang.reflect.Array.newInstance(elementOidToClass(elementOid), 0); + } + if (count > 0) { dims[0] = Math.min(count, dims[0]); } Object arr = java.lang.reflect.Array.newInstance(elementOidToClass(elementOid), dims); @@ -258,7 +261,9 @@ } Vector rows = new Vector(); Field[] fields = new Field[2]; - storeValues(rows, fields, elementOid, dims, pos, 0, index); + if (dimensions > 0) { + storeValues(rows, fields, elementOid, dims, pos, 0, index); + } BaseStatement stat = (BaseStatement) connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); return stat.createDriverResultSet(fields, rows); } Index: org/postgresql/test/jdbc4/ArrayTest.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/test/jdbc4/ArrayTest.java,v retrieving revision 1.5 diff -u -r1.5 ArrayTest.java --- org/postgresql/test/jdbc4/ArrayTest.java 2 Aug 2011 13:50:29 -0000 1.5 +++ org/postgresql/test/jdbc4/ArrayTest.java 27 Sep 2011 16:12:35 -0000 @@ -107,6 +107,9 @@ Integer out[] = (Integer [])arr.getArray(); assertEquals(0, out.length); + + ResultSet arrRs = arr.getResultSet(); + assertFalse(arrRs.next()); } public void testCreateArrayWithoutServer() throws SQLException { --------------050500040301050900050409 Content-Type: text/plain; name="binary-fix-batch-with-generated-keys.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="binary-fix-batch-with-generated-keys.patch" Index: org/postgresql/core/Query.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/Query.java,v retrieving revision 1.6 diff -u -r1.6 Query.java --- org/postgresql/core/Query.java 2 Aug 2011 13:40:12 -0000 1.6 +++ org/postgresql/core/Query.java 25 Sep 2011 12:01:10 -0000 @@ -54,4 +54,6 @@ * A closed Query should not be executed. */ void close(); + + boolean isStatementDescribed(); } Index: org/postgresql/core/v2/V2Query.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/v2/V2Query.java,v retrieving revision 1.9 diff -u -r1.9 V2Query.java --- org/postgresql/core/v2/V2Query.java 2 Aug 2011 13:40:12 -0000 1.9 +++ org/postgresql/core/v2/V2Query.java 25 Sep 2011 12:01:10 -0000 @@ -104,6 +104,10 @@ return fragments; } + public boolean isStatementDescribed() { + return false; + } + private static final ParameterList NO_PARAMETERS = new SimpleParameterList(0, false); private final String[] fragments; // Query fragments, length == # of parameters + 1 Index: org/postgresql/core/v3/CompositeQuery.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/v3/CompositeQuery.java,v retrieving revision 1.6 diff -u -r1.6 CompositeQuery.java --- org/postgresql/core/v3/CompositeQuery.java 2 Aug 2011 13:40:12 -0000 1.6 +++ org/postgresql/core/v3/CompositeQuery.java 25 Sep 2011 12:01:10 -0000 @@ -56,6 +56,14 @@ return subqueries; } + public boolean isStatementDescribed() { + for (int i = 0; i < subqueries.length; ++i) + if (!subqueries[i].isStatementDescribed()) { + return false; + } + return true; + } + private final SimpleQuery[] subqueries; private final int[] offsets; } Index: org/postgresql/core/v3/SimpleQuery.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/v3/SimpleQuery.java,v retrieving revision 1.16 diff -u -r1.16 SimpleQuery.java --- org/postgresql/core/v3/SimpleQuery.java 22 Sep 2011 12:53:24 -0000 1.16 +++ org/postgresql/core/v3/SimpleQuery.java 25 Sep 2011 12:01:10 -0000 @@ -149,7 +149,7 @@ // Have we sent a Describe Statement message for this query yet? // Note that we might not have need to, so this may always be false. - boolean isStatementDescribed() { + public boolean isStatementDescribed() { return statementDescribed; } void setStatementDescribed(boolean statementDescribed) { Index: org/postgresql/jdbc2/AbstractJdbc2Statement.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java,v retrieving revision 1.125 diff -u -r1.125 AbstractJdbc2Statement.java --- org/postgresql/jdbc2/AbstractJdbc2Statement.java 23 Sep 2011 17:36:19 -0000 1.125 +++ org/postgresql/jdbc2/AbstractJdbc2Statement.java 25 Sep 2011 12:09:18 -0000 @@ -2817,6 +2825,7 @@ batchParameters.clear(); int flags; + boolean preDescribe = false; if (wantsGeneratedKeysAlways) { flags = QueryExecutor.QUERY_BOTH_ROWS_AND_STATUS | QueryExecutor.QUERY_DISALLOW_BATCHING; @@ -2829,12 +2838,25 @@ { m_useCount += queries.length; } - if (m_prepareThreshold == 0 || m_useCount < m_prepareThreshold) + if (m_prepareThreshold == 0 || m_useCount < m_prepareThreshold) { flags |= QueryExecutor.QUERY_ONESHOT; + } else { + preDescribe = wantsGeneratedKeysAlways && !queries[0].isStatementDescribed(); + } if (connection.getAutoCommit()) flags |= QueryExecutor.QUERY_SUPPRESS_BEGIN; + if (preDescribe || ForceBinaryTransfers) { + int flags2 = flags | QueryExecutor.QUERY_DESCRIBE_ONLY; + StatementResultHandler handler2 = new StatementResultHandler(); + connection.getQueryExecutor().execute(queries[0], parameterLists[0], handler2, 0, 0, flags2); + ResultWrapper result2 = handler2.getResults(); + if (result2 != null) { + result2.getResultSet().close(); + } + } + result = null; ResultHandler handler; --------------050500040301050900050409 Content-Type: text/plain; name="binary-fix-timezonetests.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="binary-fix-timezonetests.patch" Index: org/postgresql/test/jdbc2/TimezoneTest.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/test/jdbc2/TimezoneTest.java,v retrieving revision 1.8 diff -u -r1.8 TimezoneTest.java --- org/postgresql/test/jdbc2/TimezoneTest.java 2 Aug 2011 13:50:29 -0000 1.8 +++ org/postgresql/test/jdbc2/TimezoneTest.java 28 Sep 2011 17:38:51 -0000 @@ -34,8 +34,9 @@ */ public class TimezoneTest extends TestCase { + private static final int DAY = 24 * 3600 * 1000; private Connection con; - private TimeZone saveTZ; + private static final TimeZone saveTZ = TimeZone.getDefault();; // // We set up everything in different timezones to try to exercise many cases: @@ -70,7 +71,6 @@ protected void setUp() throws Exception { // We must change the default TZ before establishing the connection. - saveTZ = TimeZone.getDefault(); TimeZone.setDefault(TimeZone.getTimeZone("GMT+01")); // Arbitary timezone that doesn't match our test timezones con = TestUtil.openDB(); @@ -103,11 +103,12 @@ ResultSet rs = con.createStatement().executeQuery("SELECT tstz,ts,t,tz,d from testtimezone"); assertTrue(rs.next()); - assertEquals("2005-01-01 15:00:00+03", rs.getString(1)); - assertEquals("2005-01-01 15:00:00", rs.getString(2)); - assertEquals("15:00:00", rs.getString(3)); - assertEquals("15:00:00+03", rs.getString(4)); - assertEquals("2005-01-01", rs.getString(5)); + checkDatabaseContents("SELECT tstz::text,ts::text,t::text,tz::text,d::text from testtimezone", + new String[]{"2005-01-01 12:00:00+00", + "2005-01-01 15:00:00", + "15:00:00", + "15:00:00+03", + "2005-01-01"}); Timestamp ts; @@ -181,9 +182,10 @@ ResultSet rs = con.createStatement().executeQuery("SELECT tstz,ts,d from testtimezone"); assertTrue(rs.next()); - assertEquals("2005-01-01 15:00:00+03", rs.getString(1)); - assertEquals("2005-01-01 15:00:00", rs.getString(2)); - assertEquals("2005-01-01", rs.getString(3)); + checkDatabaseContents("SELECT tstz::text,ts::text,d::text from testtimezone", + new String[]{"2005-01-01 12:00:00+00", + "2005-01-01 15:00:00", + "2005-01-01"}); Date d; @@ -234,10 +236,11 @@ ResultSet rs = con.createStatement().executeQuery("SELECT tstz,ts,t,tz from testtimezone"); assertTrue(rs.next()); - assertEquals("2005-01-01 15:00:00+03", rs.getString(1)); - assertEquals("2005-01-01 15:00:00", rs.getString(2)); - assertEquals("15:00:00", rs.getString(3)); - assertEquals("15:00:00+03", rs.getString(4)); + checkDatabaseContents("SELECT tstz::text,ts::text,t::text,tz::text,d::text from testtimezone", + new String[]{"2005-01-01 12:00:00+00", + "2005-01-01 15:00:00", + "15:00:00", + "15:00:00+03"}); Time t; @@ -306,6 +309,7 @@ int seq = 1; Timestamp instant = new Timestamp(1104580800000L); // 2005-01-01 12:00:00 UTC + Timestamp instantTime = new Timestamp(instant.getTime() % DAY); // +0100 (JVM default) insertTimestamp.setInt(1, seq++); @@ -334,30 +338,35 @@ insertTimestamp.executeUpdate(); } + checkDatabaseContents("SELECT seq::text,t::text from testtimezone ORDER BY seq", + new String[][]{new String[]{"1", "13:00:00"}, + new String[]{"2", "12:00:00"}, + new String[]{"3", "15:00:00"}, + new String[]{"4", "07:00:00"}, + new String[]{"5", "01:00:00"}}); + seq = 1; ResultSet rs = con.createStatement().executeQuery("SELECT seq,t FROM testtimezone ORDER BY seq"); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("13:00:00", rs.getString(2)); + assertEquals(instantTime, rs.getTimestamp(2)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("12:00:00", rs.getString(2)); + assertEquals(instantTime, rs.getTimestamp(2, cUTC)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); + assertEquals(instantTime, rs.getTimestamp(2, cGMT03)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("07:00:00", rs.getString(2)); + assertEquals(instantTime, rs.getTimestamp(2, cGMT05)); - if (min73) { - assertTrue(rs.next()); - assertEquals(seq++, rs.getInt(1)); - assertEquals("01:00:00", rs.getString(2)); - } + assertTrue(rs.next()); + assertEquals(seq++, rs.getInt(1)); + assertEquals(normalizeTimeOfDayPart(instantTime, cGMT13), rs.getTimestamp(2, cGMT13)); assertTrue(!rs.next()); } @@ -369,6 +378,12 @@ int seq = 1; Timestamp instant = new Timestamp(1104580800000L); // 2005-01-01 12:00:00 UTC + Timestamp instantTime = new Timestamp(instant.getTime() % DAY); + Timestamp instantDateJVM = new Timestamp(instant.getTime() - (instant.getTime() % DAY) - TimeZone.getDefault().getRawOffset()); + Timestamp instantDateUTC = new Timestamp(instant.getTime() - (instant.getTime() % DAY) - cUTC.getTimeZone().getRawOffset()); + Timestamp instantDateGMT03 = new Timestamp(instant.getTime() - (instant.getTime() % DAY) - cGMT03.getTimeZone().getRawOffset()); + Timestamp instantDateGMT05 = new Timestamp(instant.getTime() - (instant.getTime() % DAY) - cGMT05.getTimeZone().getRawOffset()); + Timestamp instantDateGMT13 = new Timestamp(instant.getTime() - (instant.getTime() % DAY) - cGMT13.getTimeZone().getRawOffset() + DAY); // +0100 (JVM default) insertTimestamp.setInt(1,seq++); @@ -414,6 +429,14 @@ insertTimestamp.close(); + // check that insert went correctly by parsing the raw contents in UTC + checkDatabaseContents("SELECT seq::text,tstz::text,ts::text,tz::text,d::text from testtimezone ORDER BY seq", + new String[][]{new String[]{"1", "2005-01-01 12:00:00+00", "2005-01-01 13:00:00", "13:00:00+01", "2005-01-01"}, + new String[]{"2", "2005-01-01 12:00:00+00", "2005-01-01 12:00:00", "12:00:00+00", "2005-01-01"}, + new String[]{"3", "2005-01-01 12:00:00+00", "2005-01-01 15:00:00", "15:00:00+03", "2005-01-01"}, + new String[]{"4", "2005-01-01 12:00:00+00", "2005-01-01 07:00:00", "07:00:00-05", "2005-01-01"}, + new String[]{"5", "2005-01-01 12:00:00+00", "2005-01-02 01:00:00", "01:00:00+13", "2005-01-02"}}); + // // check results // @@ -423,39 +446,39 @@ assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 15:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 13:00:00", rs.getString(3)); - assertEquals("13:00:00+01", rs.getString(4)); - assertEquals("2005-01-01", rs.getString(5)); + assertEquals(instant, rs.getTimestamp(2)); + assertEquals(instant, rs.getTimestamp(3)); + assertEquals(instantTime, rs.getTimestamp(4)); + assertEquals(instantDateJVM, rs.getTimestamp(5)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 15:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 12:00:00", rs.getString(3)); - assertEquals("12:00:00+00", rs.getString(4)); - assertEquals("2005-01-01", rs.getString(5)); + assertEquals(instant, rs.getTimestamp(2, cUTC)); + assertEquals(instant, rs.getTimestamp(3, cUTC)); + assertEquals(instantTime, rs.getTimestamp(4, cUTC)); + assertEquals(instantDateUTC, rs.getTimestamp(5, cUTC)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 15:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 15:00:00", rs.getString(3)); - assertEquals("15:00:00+03", rs.getString(4)); - assertEquals("2005-01-01", rs.getString(5)); + assertEquals(instant, rs.getTimestamp(2, cGMT03)); + assertEquals(instant, rs.getTimestamp(3, cGMT03)); + assertEquals(instantTime, rs.getTimestamp(4, cGMT03)); + assertEquals(instantDateGMT03, rs.getTimestamp(5, cGMT03)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 15:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 07:00:00", rs.getString(3)); - assertEquals("07:00:00-05", rs.getString(4)); - assertEquals("2005-01-01", rs.getString(5)); + assertEquals(instant, rs.getTimestamp(2, cGMT05)); + assertEquals(instant, rs.getTimestamp(3, cGMT05)); + assertEquals(instantTime, rs.getTimestamp(4, cGMT05)); + assertEquals(instantDateGMT05, rs.getTimestamp(5, cGMT05)); if (min73) { assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 15:00:00+03", rs.getString(2)); - assertEquals("2005-01-02 01:00:00", rs.getString(3)); - assertEquals("01:00:00+13", rs.getString(4)); - assertEquals("2005-01-02", rs.getString(5)); + assertEquals(instant, rs.getTimestamp(2, cGMT13)); + assertEquals(instant, rs.getTimestamp(3, cGMT13)); + assertEquals(normalizeTimeOfDayPart(instantTime, cGMT13), rs.getTimestamp(4, cGMT13)); + assertEquals(instantDateGMT13, rs.getTimestamp(5, cGMT13)); } assertTrue(!rs.next()); @@ -467,52 +490,59 @@ int seq = 1; - Date d; + Date dJVM, dUTC, dGMT03, dGMT05, dGMT13 = null; // +0100 (JVM default) - d = new Date(1104534000000L); // 2005-01-01 00:00:00 +0100 + dJVM = new Date(1104534000000L); // 2005-01-01 00:00:00 +0100 insertTimestamp.setInt(1,seq++); - insertTimestamp.setDate(2,d); // 2005-01-01 00:00:00 +0100 - insertTimestamp.setDate(3,d); // 2005-01-01 00:00:00 - insertTimestamp.setDate(4,d); // 2005-01-01 + insertTimestamp.setDate(2,dJVM); // 2005-01-01 00:00:00 +0100 + insertTimestamp.setDate(3,dJVM); // 2005-01-01 00:00:00 + insertTimestamp.setDate(4,dJVM); // 2005-01-01 insertTimestamp.executeUpdate(); // UTC - d = new Date(1104537600000L); // 2005-01-01 00:00:00 +0000 + dUTC = new Date(1104537600000L); // 2005-01-01 00:00:00 +0000 insertTimestamp.setInt(1,seq++); - insertTimestamp.setDate(2,d,cUTC); // 2005-01-01 00:00:00 +0000 - insertTimestamp.setDate(3,d,cUTC); // 2005-01-01 00:00:00 - insertTimestamp.setDate(4,d,cUTC); // 2005-01-01 + insertTimestamp.setDate(2,dUTC,cUTC); // 2005-01-01 00:00:00 +0000 + insertTimestamp.setDate(3,dUTC,cUTC); // 2005-01-01 00:00:00 + insertTimestamp.setDate(4,dUTC,cUTC); // 2005-01-01 insertTimestamp.executeUpdate(); // +0300 - d = new Date(1104526800000L); // 2005-01-01 00:00:00 +0300 + dGMT03 = new Date(1104526800000L); // 2005-01-01 00:00:00 +0300 insertTimestamp.setInt(1,seq++); - insertTimestamp.setDate(2,d,cGMT03); // 2005-01-01 00:00:00 +0300 - insertTimestamp.setDate(3,d,cGMT03); // 2005-01-01 00:00:00 - insertTimestamp.setDate(4,d,cGMT03); // 2005-01-01 + insertTimestamp.setDate(2,dGMT03,cGMT03); // 2005-01-01 00:00:00 +0300 + insertTimestamp.setDate(3,dGMT03,cGMT03); // 2005-01-01 00:00:00 + insertTimestamp.setDate(4,dGMT03,cGMT03); // 2005-01-01 insertTimestamp.executeUpdate(); // -0500 - d = new Date(1104555600000L); // 2005-01-01 00:00:00 -0500 + dGMT05 = new Date(1104555600000L); // 2005-01-01 00:00:00 -0500 insertTimestamp.setInt(1,seq++); - insertTimestamp.setDate(2,d,cGMT05); // 2005-01-01 00:00:00 -0500 - insertTimestamp.setDate(3,d,cGMT05); // 2005-01-01 00:00:00 - insertTimestamp.setDate(4,d,cGMT05); // 2005-01-01 + insertTimestamp.setDate(2,dGMT05,cGMT05); // 2005-01-01 00:00:00 -0500 + insertTimestamp.setDate(3,dGMT05,cGMT05); // 2005-01-01 00:00:00 + insertTimestamp.setDate(4,dGMT05,cGMT05); // 2005-01-01 insertTimestamp.executeUpdate(); if (min73) { // +1300 - d = new Date(1104490800000L); // 2005-01-01 00:00:00 +1300 + dGMT13 = new Date(1104490800000L); // 2005-01-01 00:00:00 +1300 insertTimestamp.setInt(1,seq++); - insertTimestamp.setDate(2,d,cGMT13); // 2005-01-01 00:00:00 +1300 - insertTimestamp.setDate(3,d,cGMT13); // 2005-01-01 00:00:00 - insertTimestamp.setDate(4,d,cGMT13); // 2005-01-01 + insertTimestamp.setDate(2,dGMT13,cGMT13); // 2005-01-01 00:00:00 +1300 + insertTimestamp.setDate(3,dGMT13,cGMT13); // 2005-01-01 00:00:00 + insertTimestamp.setDate(4,dGMT13,cGMT13); // 2005-01-01 insertTimestamp.executeUpdate(); } insertTimestamp.close(); + // check that insert went correctly by parsing the raw contents in UTC + checkDatabaseContents("SELECT seq::text,tstz::text,ts::text,d::text from testtimezone ORDER BY seq", + new String[][]{new String[]{"1", "2004-12-31 23:00:00+00", "2005-01-01 00:00:00", "2005-01-01"}, + new String[]{"2", "2005-01-01 00:00:00+00", "2005-01-01 00:00:00", "2005-01-01"}, + new String[]{"3", "2004-12-31 21:00:00+00", "2005-01-01 00:00:00", "2005-01-01"}, + new String[]{"4", "2005-01-01 05:00:00+00", "2005-01-01 00:00:00", "2005-01-01"}, + new String[]{"5", "2004-12-31 11:00:00+00", "2005-01-01 00:00:00", "2005-01-01"}}); // // check results // @@ -522,34 +552,34 @@ assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 02:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 00:00:00", rs.getString(3)); - assertEquals("2005-01-01", rs.getString(4)); + assertEquals(dJVM, rs.getDate(2)); + assertEquals(dJVM, rs.getDate(3)); + assertEquals(dJVM, rs.getDate(4)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 03:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 00:00:00", rs.getString(3)); - assertEquals("2005-01-01", rs.getString(4)); + assertEquals(dUTC, rs.getDate(2, cUTC)); + assertEquals(dUTC, rs.getDate(3, cUTC)); + assertEquals(dUTC, rs.getDate(4, cUTC)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 00:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 00:00:00", rs.getString(3)); - assertEquals("2005-01-01", rs.getString(4)); + assertEquals(dGMT03, rs.getDate(2, cGMT03)); + assertEquals(dGMT03, rs.getDate(3, cGMT03)); + assertEquals(dGMT03, rs.getDate(4, cGMT03)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 08:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 00:00:00", rs.getString(3)); - assertEquals("2005-01-01", rs.getString(4)); + assertEquals(dGMT05, rs.getDate(2, cGMT05)); + assertEquals(dGMT05, rs.getDate(3, cGMT05)); + assertEquals(dGMT05, rs.getDate(4, cGMT05)); if (min73) { assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2004-12-31 14:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 00:00:00", rs.getString(3)); - assertEquals("2005-01-01", rs.getString(4)); + assertEquals(dGMT13, rs.getDate(2, cGMT13)); + assertEquals(dGMT13, rs.getDate(3, cGMT13)); + assertEquals(dGMT13, rs.getDate(4, cGMT13)); } assertTrue(!rs.next()); @@ -567,45 +597,53 @@ int seq = 1; - Time t; + Time tJVM, tUTC, tGMT03, tGMT05, tGMT13; // +0100 (JVM default) - t = new Time(50400000L); // 1970-01-01 15:00:00 +0100 + tJVM = new Time(50400000L); // 1970-01-01 15:00:00 +0100 insertTimestamp.setInt(1,seq++); - insertTimestamp.setTime(2,t); // 15:00:00 - insertTimestamp.setTime(3,t); // 15:00:00+03 + insertTimestamp.setTime(2,tJVM); // 15:00:00 + insertTimestamp.setTime(3,tJVM); // 15:00:00+03 insertTimestamp.executeUpdate(); // UTC - t = new Time(54000000L); // 1970-01-01 15:00:00 +0000 + tUTC = new Time(54000000L); // 1970-01-01 15:00:00 +0000 insertTimestamp.setInt(1,seq++); - insertTimestamp.setTime(2,t,cUTC); // 15:00:00 - insertTimestamp.setTime(3,t,cUTC); // 15:00:00+00 + insertTimestamp.setTime(2,tUTC,cUTC); // 15:00:00 + insertTimestamp.setTime(3,tUTC,cUTC); // 15:00:00+00 insertTimestamp.executeUpdate(); // +0300 - t = new Time(43200000L); // 1970-01-01 15:00:00 +0300 + tGMT03 = new Time(43200000L); // 1970-01-01 15:00:00 +0300 insertTimestamp.setInt(1,seq++); - insertTimestamp.setTime(2,t,cGMT03); // 15:00:00 - insertTimestamp.setTime(3,t,cGMT03); // 15:00:00+03 + insertTimestamp.setTime(2,tGMT03,cGMT03); // 15:00:00 + insertTimestamp.setTime(3,tGMT03,cGMT03); // 15:00:00+03 insertTimestamp.executeUpdate(); // -0500 - t = new Time(72000000L); // 1970-01-01 15:00:00 -0500 + tGMT05 = new Time(72000000L); // 1970-01-01 15:00:00 -0500 insertTimestamp.setInt(1,seq++); - insertTimestamp.setTime(2,t,cGMT05); // 15:00:00 - insertTimestamp.setTime(3,t,cGMT05); // 15:00:00-05 + insertTimestamp.setTime(2,tGMT05,cGMT05); // 15:00:00 + insertTimestamp.setTime(3,tGMT05,cGMT05); // 15:00:00-05 insertTimestamp.executeUpdate(); // +1300 - t = new Time(7200000L); // 1970-01-01 15:00:00 +1300 + tGMT13 = new Time(7200000L); // 1970-01-01 15:00:00 +1300 insertTimestamp.setInt(1,seq++); - insertTimestamp.setTime(2,t,cGMT13); // 15:00:00 - insertTimestamp.setTime(3,t,cGMT13); // 15:00:00+13 + insertTimestamp.setTime(2,tGMT13,cGMT13); // 15:00:00 + insertTimestamp.setTime(3,tGMT13,cGMT13); // 15:00:00+13 insertTimestamp.executeUpdate(); insertTimestamp.close(); + // check that insert went correctly by parsing the raw contents in UTC + checkDatabaseContents("SELECT seq::text,t::text,tz::text from testtimezone ORDER BY seq", + new String[][]{new String[]{"1", "15:00:00", "15:00:00+01",}, + new String[]{"2", "15:00:00", "15:00:00+00",}, + new String[]{"3", "15:00:00", "15:00:00+03",}, + new String[]{"4", "15:00:00", "15:00:00-05",}, + new String[]{"5", "15:00:00", "15:00:00+13",}}); + // // check results // @@ -615,28 +653,28 @@ assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); - assertEquals("15:00:00+01", rs.getString(3)); + assertEquals(tJVM, rs.getTime(2)); + assertEquals(tJVM, rs.getTime(3)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); - assertEquals("15:00:00+00", rs.getString(3)); + assertEquals(tUTC, rs.getTime(2, cUTC)); + assertEquals(tUTC, rs.getTime(2, cUTC)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); - assertEquals("15:00:00+03", rs.getString(3)); + assertEquals(tGMT03, rs.getTime(2, cGMT03)); + assertEquals(tGMT03, rs.getTime(2, cGMT03)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); - assertEquals("15:00:00-05", rs.getString(3)); + assertEquals(tGMT05, rs.getTime(2, cGMT05)); + assertEquals(tGMT05, rs.getTime(2, cGMT05)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); - assertEquals("15:00:00+13", rs.getString(3)); + assertEquals(tGMT13, rs.getTime(2, cGMT13)); + assertEquals(tGMT13, rs.getTime(2, cGMT13)); assertTrue(!rs.next()); } @@ -664,4 +702,55 @@ assertEquals(-1577929192000L, rs.getTimestamp(1).getTime()); } + /** + * Does a query in UTC time zone to database to check that the + * inserted values are correct. + * + * @param query The query to run. + * @param correct The correct answers in UTC time zone as formatted by backend. + */ + private void checkDatabaseContents(String query, String[] correct) throws Exception { + checkDatabaseContents(query, new String[][]{correct}); + } + + private void checkDatabaseContents(String query, String[][] correct) throws Exception { + Connection con2 = TestUtil.openDB(); + Statement s = con2.createStatement(); + assertFalse(s.execute("set time zone 'UTC'")); + assertTrue(s.execute(query)); + ResultSet rs = s.getResultSet(); + for (int j = 0; j < correct.length; ++j) { + assertTrue(rs.next()); + for (int i = 0; i < correct[j].length; ++i) { + assertEquals("On row " + (j+1), correct[j][i], rs.getString(i+1)); + } + } + assertFalse(rs.next()); + rs.close(); + s.close(); + con2.close(); + } + + /** + * Converts the given time + * + * @param t The time of day. Must be within -24 and + 24 hours of epoc. + * @param tz The timezone to normalize to. + * @return the Time nomralized to 0 to 24 hours of epoc adjusted with given timezone. + */ + private Timestamp normalizeTimeOfDayPart(Timestamp t, Calendar tz) { + return new Timestamp(normalizeTimeOfDayPart(t.getTime(), tz.getTimeZone())); + } + + private long normalizeTimeOfDayPart(long t, TimeZone tz) { + long millis = t; + long low = - tz.getOffset(millis); + long high = low + DAY; + if (millis < low) { + do { millis += DAY; } while (millis < low); + } else if (millis >= high) { + do { millis -= DAY; } while (millis > high); + } + return millis; + } } --------------050500040301050900050409--