db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r1000811 - in /db/derby/code/trunk/java: engine/org/apache/derby/iapi/types/SQLChar.java testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java
Date Fri, 24 Sep 2010 11:24:34 GMT
Author: kahatlen
Date: Fri Sep 24 11:24:33 2010
New Revision: 1000811

URL: http://svn.apache.org/viewvc?rev=1000811&view=rev
Log:
DERBY-4810: setTimestamp() methods don't agree on trailing zeros

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java?rev=1000811&r1=1000810&r2=1000811&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java Fri Sep 24 11:24:33
2010
@@ -1575,23 +1575,24 @@ readingLoop:
                 }
                 else if (nanos > 0)
                 {
-                    String microsStr = Integer.toString( nanos);
-                    if(microsStr.length() > SQLTimestamp.MAX_FRACTION_DIGITS)
+                    String nanoString = Integer.toString(nanos);
+                    int len = nanoString.length();
+
+                    // Add leading zeros if nanoString is shorter than
+                    // MAX_FRACTION_DIGITS.
+                    for (int i = len;
+                         i < SQLTimestamp.MAX_FRACTION_DIGITS; i++)
                     {
-                        sb.append(
-                            microsStr.substring(
-                                0, SQLTimestamp.MAX_FRACTION_DIGITS));
+                        sb.append('0');
                     }
-                    else
-                    {
-                        for(int i = microsStr.length(); 
-                            i < SQLTimestamp.MAX_FRACTION_DIGITS ; i++)
-                        {
-                            sb.append( '0');
-                        }
 
-                        sb.append( microsStr);
+                    // Remove trailing zeros to match the format from
+                    // Timestamp.toString().
+                    while (nanoString.charAt(len - 1) == '0') {
+                        len--;
                     }
+
+                    sb.append(nanoString.substring(0, len));
                 }
                 strValue= sb.toString();
             }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java?rev=1000811&r1=1000810&r2=1000811&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java
Fri Sep 24 11:24:33 2010
@@ -981,7 +981,41 @@ public final class DateTimeTest extends 
         ps.setDate(1, d, Calendar.getInstance());
         JDBC.assertSingleValueResultSet(ps.executeQuery(), dateString);
     }
-    
+
+    /**
+     * Test that trailing zeros in the nanoseconds component of a timestamp
+     * are handled the same way by
+     * {@code PreparedStatement.setTimestamp(int,Timestamp)} and
+     * {@code PreparedStatement.setTimestamp(int,Timestamp, Calendar)}
+     * when converting the timestamp to a VARCHAR. (DERBY-4810)
+     */
+    public void testTrailingZeros() throws SQLException {
+        PreparedStatement ps =
+                prepareStatement("values cast(? as varchar(29))");
+
+        String[] tsStrings = {
+            "2010-09-22 14:40:33.000000000",
+            "2010-09-22 14:40:33.012000000",
+            "2010-09-22 14:40:33.123456000",
+            "2010-09-22 14:40:33.139990900",
+            "2010-09-22 14:40:33.139990983",
+        };
+
+        for (int i = 0; i < tsStrings.length; i++) {
+            Timestamp ts = Timestamp.valueOf(tsStrings[i]);
+
+            // We expect the converted value to have the same format as
+            // what Timestamp.toString() returns.
+            String expected = ts.toString();
+
+            ps.setTimestamp(1, ts);
+            JDBC.assertSingleValueResultSet(ps.executeQuery(), expected);
+
+            ps.setTimestamp(1, ts, Calendar.getInstance());
+            JDBC.assertSingleValueResultSet(ps.executeQuery(), expected);
+        }
+    }
+
     public void testConversion_Aggregates() throws SQLException{
         Statement st = createStatement();
         



Mime
View raw message