You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by ma...@apache.org on 2010/07/08 07:37:58 UTC

svn commit: r961573 - in /db/derby/code/branches/10.5: ./ java/engine/org/apache/derby/iapi/types/SQLChar.java java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java

Author: mamta
Date: Thu Jul  8 05:37:58 2010
New Revision: 961573

URL: http://svn.apache.org/viewvc?rev=961573&view=rev
Log:
Merging DERBY-4621 (Invalid conversion from Timestamp to String when calling setTimestamp() with Calendar) into 10.5 codeline


Modified:
    db/derby/code/branches/10.5/   (props changed)
    db/derby/code/branches/10.5/java/engine/org/apache/derby/iapi/types/SQLChar.java
    db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java

Propchange: db/derby/code/branches/10.5/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Jul  8 05:37:58 2010
@@ -1,2 +1,2 @@
 /db/derby/code/branches/10.6:957000
-/db/derby/code/trunk:757811,769596,769602,769606,769962,772090,772337,772449,772534,774281,777105,779681,782991,785131,785139,785163,785570,785662,788369,788670,788674,788968,789264,790218,791027,792434,793089,793588,794106,794303,794955,795166,795459,796020,796027,796316,796372,797147,798347,798742,800523,803548,803948,805696,808494,808850,809643,810860,812669,816531,816536,819006,822289,823659,824694,829022,829410,831304,831319,832379,833430,835286,881074,881444,882732,884163,885421,885659,887246,888311,892912,897161,898635,901165,901648,901760,903108,905224,908418,908586,909176,911315,915733,916075,916897,918359,921028,927430,928065,942286,942476,942480,942587,946794,948045,948069,951346,952138,954748,955001,955634,956075,956445,956659,958163,959550
+/db/derby/code/trunk:757811,769596,769602,769606,769962,772090,772337,772449,772534,774281,777105,779681,782991,785131,785139,785163,785570,785662,788369,788670,788674,788968,789264,790218,791027,792434,793089,793588,794106,794303,794955,795166,795459,796020,796027,796316,796372,797147,798347,798742,800523,803548,803948,805696,808494,808850,809643,810860,812669,816531,816536,819006,822289,823659,824694,829022,829410,831304,831319,832379,833430,835286,881074,881444,882732,884163,885421,885659,887246,888311,892912,897161,898635,901165,901648,901760,903108,905224,908418,908586,909176,911315,915733,916075,916897,918359,921028,927430,928065,936215,942286,942476,942480,942587,946794,948045,948069,951346,952138,954748,955001,955634,956075,956445,956659,958163,959550

Modified: db/derby/code/branches/10.5/java/engine/org/apache/derby/iapi/types/SQLChar.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.5/java/engine/org/apache/derby/iapi/types/SQLChar.java?rev=961573&r1=961572&r2=961573&view=diff
==============================================================================
--- db/derby/code/branches/10.5/java/engine/org/apache/derby/iapi/types/SQLChar.java (original)
+++ db/derby/code/branches/10.5/java/engine/org/apache/derby/iapi/types/SQLChar.java Thu Jul  8 05:37:58 2010
@@ -1567,13 +1567,20 @@ readingLoop:
                 formatJDBCDate( cal, sb);
                 sb.append( ' ');
                 formatJDBCTime( cal, sb);
+                sb.append('.');
+
                 int micros = 
                     (theValue.getNanos() + SQLTimestamp.FRACTION_TO_NANO/2) / 
                         SQLTimestamp.FRACTION_TO_NANO;
 
-                if( micros > 0)
+                if (micros == 0)
+                {
+                    // Add a single zero after the decimal point to match
+                    // the format from Timestamp.toString().
+                    sb.append('0');
+                }
+                else if (micros > 0)
                 {
-                    sb.append( '.');
                     String microsStr = Integer.toString( micros);
                     if(microsStr.length() > SQLTimestamp.MAX_FRACTION_DIGITS)
                     {
@@ -1609,7 +1616,7 @@ readingLoop:
     private void formatJDBCTime( Calendar cal, StringBuffer sb)
     {
         SQLTime.timeToString(
-            cal.get(Calendar.HOUR), 
+            cal.get(Calendar.HOUR_OF_DAY),
             cal.get(Calendar.MINUTE), 
             cal.get(Calendar.SECOND), 
             sb);

Modified: db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java?rev=961573&r1=961572&r2=961573&view=diff
==============================================================================
--- db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java (original)
+++ db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java Thu Jul  8 05:37:58 2010
@@ -21,11 +21,15 @@
 */
 package org.apache.derbyTesting.functionTests.tests.lang;
 
+import java.sql.Date;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
 import java.sql.Statement;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.util.Calendar;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
@@ -909,6 +913,44 @@ public final class DateTimeTest extends 
         
         st.close();
     }
+
+    /**
+     * Regression test case for DERBY-4621, which caused the conversion of
+     * timestamp and time values to varchar to generate wrong results when
+     * a Calendar object was supplied.
+     */
+    public void testConversionToString() throws SQLException {
+        String timestampString = "2010-04-20 15:17:36.0";
+        String timeString = "15:17:36";
+        String dateString = "2010-04-20";
+
+        Timestamp ts = Timestamp.valueOf(timestampString);
+        Time t = Time.valueOf(timeString);
+        Date d = Date.valueOf(dateString);
+
+        PreparedStatement ps =
+                prepareStatement("VALUES CAST(? AS VARCHAR(40))");
+
+        ps.setTimestamp(1, ts);
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), timestampString);
+
+        // Used to give wrong result - 2010-04-20 03:17:36
+        ps.setTimestamp(1, ts, Calendar.getInstance());
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), timestampString);
+
+        ps.setTime(1, t);
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), timeString);
+
+        // Used to give wrong result - 03:17:36
+        ps.setTime(1, t, Calendar.getInstance());
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), timeString);
+
+        ps.setDate(1, d);
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), dateString);
+
+        ps.setDate(1, d, Calendar.getInstance());
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), dateString);
+    }
     
     public void testConversion_Aggregates() throws SQLException{
         Statement st = createStatement();