db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From abr...@apache.org
Subject svn commit: r540740 - in /db/derby/code/trunk/java/client/org/apache/derby/client/am: Cursor.java DateTime.java
Date Tue, 22 May 2007 20:38:08 GMT
Author: abrown
Date: Tue May 22 13:38:08 2007
New Revision: 540740

URL: http://svn.apache.org/viewvc?view=rev&rev=540740
Log:
DERBY-1816 (partial): Pre-patch "cleanup" that does the following:

  1) Replaces each of the recyclable Date, Time, and Timestamp arguments
     with a recyclable java.util.Calendar object in client/am/Cursor.java.

  2) Modifies the relevant code in client/am/DateTime.java to call methods
     on the recyclable Calendar object instead of on Date, Time, and
     Timestamp objects. The benefit to doing this is that we are now using
     non-deprecated methods.

Note that even with this patch we are still creating a new instance of
Time/Timestamp/Date for each method--the cleanup patch does not change that.
Instead, the cleanup patch adds the instantiation of a new Calendar object
(one per client/am/Cursor) and then (re-)uses that object to replace the
deprecated calls.

Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/Cursor.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/DateTime.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/Cursor.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/Cursor.java?view=diff&rev=540740&r1=540739&r2=540740
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/Cursor.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/Cursor.java Tue May 22 13:38:08
2007
@@ -95,9 +95,7 @@
     final static public java.lang.Boolean ROW_IS_NULL = new Boolean(true);
     final static public java.lang.Boolean ROW_IS_NOT_NULL = new Boolean(false);
 
-    java.sql.Date recyclableDate_ = null;
-    java.sql.Time recyclableTime_ = null;
-    java.sql.Timestamp recyclableTimestamp_ = null;
+    java.util.Calendar recyclableCalendar_ = null;
 
     // For the net, this data comes from the query descriptor.
 
@@ -511,7 +509,7 @@
         try {
             return org.apache.derby.client.am.DateTime.dateBytesToDate(dataBuffer_,
                 columnDataPosition_[column - 1],
-                recyclableDate_, 
+                getRecyclableCalendar(), 
                 charsetName_[column - 1]);
         }catch (UnsupportedEncodingException e) {
              throw new SqlException(agent_.logWriter_, 
@@ -527,7 +525,7 @@
         try {
             return org.apache.derby.client.am.DateTime.timeBytesToTime(dataBuffer_,
                     columnDataPosition_[column - 1],
-                    recyclableTime_,
+                    getRecyclableCalendar(),
                     charsetName_[column - 1]);
         } catch (UnsupportedEncodingException e) {
              throw new SqlException(agent_.logWriter_, 
@@ -540,10 +538,10 @@
     private final java.sql.Timestamp getTIMESTAMP(int column) throws SqlException {
 
         try {
-        return org.apache.derby.client.am.DateTime.timestampBytesToTimestamp(
+            return org.apache.derby.client.am.DateTime.timestampBytesToTimestamp(
                 dataBuffer_,
                 columnDataPosition_[column - 1],
-                recyclableTimestamp_, 
+                getRecyclableCalendar(), 
                 charsetName_[column - 1]);
     } catch (java.io.UnsupportedEncodingException e) {
              throw new SqlException(agent_.logWriter_, 
@@ -557,7 +555,7 @@
         try {
             return org.apache.derby.client.am.DateTime.dateBytesToTimestamp(dataBuffer_,
                     columnDataPosition_[column - 1],
-                    recyclableTimestamp_, 
+                    getRecyclableCalendar(), 
                     charsetName_[column -1]);
         } catch (UnsupportedEncodingException e) {
              throw new SqlException(agent_.logWriter_, 
@@ -571,7 +569,7 @@
         try {
             return org.apache.derby.client.am.DateTime.timeBytesToTimestamp(dataBuffer_,
                     columnDataPosition_[column - 1],
-                    recyclableTimestamp_,
+                    getRecyclableCalendar(),
                     charsetName_[column -1]);
         } catch (UnsupportedEncodingException e) {
              throw new SqlException(agent_.logWriter_, 
@@ -585,7 +583,7 @@
         try {
             return org.apache.derby.client.am.DateTime.timestampBytesToDate(dataBuffer_,
                     columnDataPosition_[column - 1],
-                    recyclableDate_,
+                    getRecyclableCalendar(),
                     charsetName_[column -1]);
         } catch (UnsupportedEncodingException e) {
              throw new SqlException(agent_.logWriter_, 
@@ -599,7 +597,7 @@
         try {
             return org.apache.derby.client.am.DateTime.timestampBytesToTime(dataBuffer_,
                     columnDataPosition_[column - 1],
-                    recyclableTime_,
+                    getRecyclableCalendar(),
                     charsetName_[column -1]);
         } catch (UnsupportedEncodingException e) {
              throw new SqlException(agent_.logWriter_, 
@@ -647,6 +645,20 @@
         bytes = new byte[columnLength];
         System.arraycopy(dataBuffer_, columnDataPosition_[column - 1] + 2, bytes, 0, bytes.length);
         return bytes;
+    }
+
+    /**
+     * Instantiate an instance of Calendar that can be re-used for getting
+     * Time, Timestamp, and Date values from this cursor.  Assumption is
+     * that all users of the returned Calendar object will clear it as
+     * appropriate before using it.
+     */
+    private java.util.Calendar getRecyclableCalendar()
+    {
+        if (recyclableCalendar_ == null)
+            recyclableCalendar_ = new java.util.GregorianCalendar();
+
+        return recyclableCalendar_;
     }
 
     abstract public Blob getBlobColumn_(int column, Agent agent) throws SqlException;

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/DateTime.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/DateTime.java?view=diff&rev=540740&r1=540739&r2=540740
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/DateTime.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/DateTime.java Tue May 22 13:38:08
2007
@@ -24,6 +24,7 @@
 import org.apache.derby.shared.common.reference.SQLState;
 
 import java.io.UnsupportedEncodingException;
+import java.util.Calendar;
 import org.apache.derby.client.net.Typdef;
 
 
@@ -56,14 +57,14 @@
      * 
      * @param buffer    
      * @param offset    
-     * @param recyclableDate
+     * @param recyclableCal
      * @param encoding            encoding of buffer data
      * @return  Date translated from  buffer with specified encoding
      * @throws UnsupportedEncodingException
      */
     public static final java.sql.Date dateBytesToDate(byte[] buffer,
                                                       int offset,
-                                                      java.sql.Date recyclableDate, 
+                                                      Calendar recyclableCal, 
                                                       String encoding) 
     throws UnsupportedEncodingException {
         int year, month, day;
@@ -89,8 +90,8 @@
                 1000 * (((int) date.charAt(yearIndx)) - zeroBase) +
                 100 * (((int) date.charAt(yearIndx + 1)) - zeroBase) +
                 10 * (((int) date.charAt(yearIndx + 2)) - zeroBase) +
-                (((int) date.charAt(yearIndx + 3)) - zeroBase) -
-                1900;
+                (((int) date.charAt(yearIndx + 3)) - zeroBase);
+
         month =
                 10 * (((int) date.charAt(monthIndx)) - zeroBase) +
                 (((int) date.charAt(monthIndx + 1)) - zeroBase) -
@@ -99,14 +100,9 @@
                 10 * (((int) date.charAt(dayIndx)) - zeroBase) +
                 (((int) date.charAt(dayIndx + 1)) - zeroBase);
 
-        if (recyclableDate == null) {
-            return new java.sql.Date(year, month, day);
-        } else {
-            recyclableDate.setYear(year);
-            recyclableDate.setMonth(month);
-            recyclableDate.setDate(day);
-            return recyclableDate;
-        }
+        Calendar cal = getCleanCalendar(recyclableCal);
+        cal.set(year, month, day);
+        return new java.sql.Date(cal.getTimeInMillis());
     }
 
     
@@ -115,14 +111,14 @@
      * which is in the format: <code> hh.mm.ss </code>
      * @param buffer
      * @param offset
-     * @param recyclableTime
+     * @param recyclableCal
      * @param encoding           encoding of buffer
      * @return  Time translated from buffer with specified encoding
      * @throws UnsupportedEncodingException
      */
     public static final java.sql.Time timeBytesToTime(byte[] buffer,
                                                       int offset,
-                                                      java.sql.Time recyclableTime,
+                                                      Calendar recyclableCal,
                                                       String encoding) 
     throws UnsupportedEncodingException {
         int hour, minute, second;
@@ -144,14 +140,9 @@
                 10 * (((int) time.charAt(6)) - zeroBase) +
                 (((int) time.charAt(7)) - zeroBase);
 
-        if (recyclableTime == null) {
-            return new java.sql.Time(hour, minute, second);
-        } else {
-            recyclableTime.setHours(hour);
-            recyclableTime.setMinutes(minute);
-            recyclableTime.setSeconds(second);
-            return recyclableTime;
-        }
+        Calendar cal = getCleanCalendar(recyclableCal);
+        cal.set(1970, Calendar.JANUARY, 1, hour, minute, second);
+        return new java.sql.Time(cal.getTimeInMillis());
     }
 
     /**
@@ -160,14 +151,14 @@
      * 
      * @param buffer
      * @param offset
-     * @param recyclableTimestamp
+     * @param recyclableCal
      * @param encoding                encoding of buffer
      * @return TimeStamp translated from buffer with specified encoding
      * @throws UnsupportedEncodingException
      */
     public static final java.sql.Timestamp timestampBytesToTimestamp(byte[] buffer,
                                                                      int offset,
-                                                                     java.sql.Timestamp recyclableTimestamp,

+                                                                     Calendar recyclableCal,

                                                                      String encoding) 
     throws UnsupportedEncodingException
     {
@@ -181,8 +172,8 @@
                 1000 * (((int) timestamp.charAt(0)) - zeroBase) +
                 100 * (((int) timestamp.charAt(1)) - zeroBase) +
                 10 * (((int) timestamp.charAt(2)) - zeroBase) +
-                (((int) timestamp.charAt(3)) - zeroBase) -
-                1900;
+                (((int) timestamp.charAt(3)) - zeroBase);
+
         month =
                 10 * (((int) timestamp.charAt(5)) - zeroBase) +
                 (((int) timestamp.charAt(6)) - zeroBase) -
@@ -207,18 +198,11 @@
                 10 * (((int) timestamp.charAt(24)) - zeroBase) +
                 (((int) timestamp.charAt(25)) - zeroBase);
 
-        if (recyclableTimestamp == null) {
-            return new java.sql.Timestamp(year, month, day, hour, minute, second, fraction
* 1000);
-        } else {
-            recyclableTimestamp.setYear(year);
-            recyclableTimestamp.setMonth(month);
-            recyclableTimestamp.setDate(day);
-            recyclableTimestamp.setHours(hour);
-            recyclableTimestamp.setMinutes(minute);
-            recyclableTimestamp.setSeconds(second);
-            recyclableTimestamp.setNanos(fraction * 1000);
-            return recyclableTimestamp;
-        }
+        Calendar cal = getCleanCalendar(recyclableCal);
+        cal.set(year, month, day, hour, minute, second);
+        java.sql.Timestamp ts = new java.sql.Timestamp(cal.getTimeInMillis());
+        ts.setNanos(fraction * 1000);
+        return ts;
     }
 
     // ********************************************************
@@ -389,14 +373,14 @@
      * 
      * @param buffer
      * @param offset
-     * @param recyclableTimestamp
+     * @param recyclableCal
      * @param encoding                encoding of buffer
      * @return Timestamp translated from buffer with specified encoding
      * @throws UnsupportedEncodingException
      */
     public static final java.sql.Timestamp dateBytesToTimestamp(byte[] buffer,
                                                                 int offset,
-                                                                java.sql.Timestamp recyclableTimestamp,
+                                                                Calendar recyclableCal,
                                                                 String encoding) 
     throws UnsupportedEncodingException {
         int year, month, day;
@@ -416,8 +400,8 @@
                 1000 * (((int) date.charAt(yearIndx)) - zeroBase) +
                 100 * (((int) date.charAt(yearIndx + 1)) - zeroBase) +
                 10 * (((int) date.charAt(yearIndx + 2)) - zeroBase) +
-                (((int) date.charAt(yearIndx + 3)) - zeroBase) -
-                1900;
+                (((int) date.charAt(yearIndx + 3)) - zeroBase);
+
         month =
                 10 * (((int) date.charAt(monthIndx)) - zeroBase) +
                 (((int) date.charAt(monthIndx + 1)) - zeroBase) -
@@ -426,18 +410,11 @@
                 10 * (((int) date.charAt(dayIndx)) - zeroBase) +
                 (((int) date.charAt(dayIndx + 1)) - zeroBase);
 
-        if (recyclableTimestamp == null) {
-            return new java.sql.Timestamp(year, month, day, 0, 0, 0, 0);
-        } else {
-            recyclableTimestamp.setYear(year);
-            recyclableTimestamp.setMonth(month);
-            recyclableTimestamp.setDate(day);
-            recyclableTimestamp.setHours(0);
-            recyclableTimestamp.setMinutes(0);
-            recyclableTimestamp.setSeconds(0);
-            recyclableTimestamp.setNanos(0);
-            return recyclableTimestamp;
-        }
+        Calendar cal = getCleanCalendar(recyclableCal);
+        cal.set(year, month, day, 0, 0, 0);
+        java.sql.Timestamp ts = new java.sql.Timestamp(cal.getTimeInMillis());
+        ts.setNanos(0);
+        return ts;
     }
 
     
@@ -447,7 +424,7 @@
      * 
      * @param buffer
      * @param offset
-     * @param recyclableTimestamp
+     * @param recyclableCal
      * @param encoding                 encoding of buffer
      * @return Timestamp translated from buffer with specified encoding 
      * @throws UnsupportedEncodingException
@@ -455,7 +432,7 @@
      */
     public static final java.sql.Timestamp timeBytesToTimestamp(byte[] buffer,
                                                                 int offset,
-                                                                java.sql.Timestamp recyclableTimestamp,

+                                                                Calendar recyclableCal, 
                                                                 String encoding)
     throws UnsupportedEncodingException {
         int hour, minute, second;
@@ -480,18 +457,17 @@
         // The SQL standard specifies that the date portion of the returned
         // timestamp should be set to the current date. See DERBY-889 for
         // more details.
-        java.util.Date today = new java.util.Date();
-        if (recyclableTimestamp == null) {
-            recyclableTimestamp = new java.sql.Timestamp(today.getTime());
-        }
-        else {
-            recyclableTimestamp.setTime(today.getTime());
-        }
-        recyclableTimestamp.setHours(hour);
-        recyclableTimestamp.setMinutes(minute);
-        recyclableTimestamp.setSeconds(second);
-        recyclableTimestamp.setNanos(0);
-        return recyclableTimestamp;
+        Calendar cal = getCleanCalendar(recyclableCal);
+        cal.setTime(new java.util.Date());
+
+        // Now override the time fields with the values we parsed.
+        cal.set(Calendar.HOUR_OF_DAY, hour);
+        cal.set(Calendar.MINUTE, minute);
+        cal.set(Calendar.SECOND, second);
+
+        // Derby's resolution for the TIME type is only seconds.
+        cal.set(Calendar.MILLISECOND, 0);
+        return new java.sql.Timestamp(cal.getTimeInMillis());
     }
     
     
@@ -501,14 +477,14 @@
      * 
      * @param buffer
      * @param offset
-     * @param recyclableDate
+     * @param recyclableCal
      * @param encoding             encoding of buffer
      * @return Date translated from buffer with specified encoding
      * @throws UnsupportedEncodingException
      */
     public static final java.sql.Date timestampBytesToDate(byte[] buffer,
                                                            int offset,
-                                                           java.sql.Date recyclableDate,

+                                                           Calendar recyclableCal, 
                                                            String encoding) 
     throws UnsupportedEncodingException 
      {
@@ -522,8 +498,8 @@
                 1000 * (((int) timestamp.charAt(0)) - zeroBase) +
                 100 * (((int) timestamp.charAt(1)) - zeroBase) +
                 10 * (((int) timestamp.charAt(2)) - zeroBase) +
-                (((int) timestamp.charAt(3)) - zeroBase) -
-                1900;
+                (((int) timestamp.charAt(3)) - zeroBase);
+
         month =
                 10 * (((int) timestamp.charAt(5)) - zeroBase) +
                 (((int) timestamp.charAt(6)) - zeroBase) -
@@ -532,14 +508,9 @@
                 10 * (((int) timestamp.charAt(8)) - zeroBase) +
                 (((int) timestamp.charAt(9)) - zeroBase);
 
-        if (recyclableDate == null) {
-            return new java.sql.Date(year, month, day);
-        } else {
-            recyclableDate.setYear(year);
-            recyclableDate.setMonth(month);
-            recyclableDate.setDate(day);
-            return recyclableDate;
-        }
+        Calendar cal = getCleanCalendar(recyclableCal);
+        cal.set(year, month, day);
+        return new java.sql.Date(cal.getTimeInMillis());
     }
 
    
@@ -549,14 +520,14 @@
      * 
      * @param buffer
      * @param offset
-     * @param recyclableTime
+     * @param recyclableCal
      * @param encoding            encoding of buffer
      * @return  Time translated from buffer with specified Encoding
      * @throws UnsupportedEncodingException
      */
     public static final java.sql.Time timestampBytesToTime(byte[] buffer,
                                                            int offset,
-                                                           java.sql.Time recyclableTime,

+                                                           Calendar recyclableCal, 
                                                            String encoding) 
     throws  UnsupportedEncodingException {
         int hour, minute, second;
@@ -575,14 +546,34 @@
                 10 * (((int) timestamp.charAt(17)) - zeroBase) +
                 (((int) timestamp.charAt(18)) - zeroBase);
 
-        if (recyclableTime == null) {
-            return new java.sql.Time(hour, minute, second);
-        } else {
-            recyclableTime.setYear(hour);
-            recyclableTime.setMonth(minute);
-            recyclableTime.setDate(second);
-            return recyclableTime;
+        Calendar cal = getCleanCalendar(recyclableCal);
+        cal.set(1970, Calendar.JANUARY, 1, hour, minute, second);
+        return new java.sql.Time(cal.getTimeInMillis());
+    }
+
+    /**
+     * Return a clean (i.e. all values cleared out) Calendar object
+     * that can be used for creating Time, Timestamp, and Date objects.
+     * If the received Calendar object is non-null, then just clear
+     * that and return it.
+     *
+     * @param recyclableCal Calendar object to use if non-null.
+     */
+    private static Calendar getCleanCalendar(Calendar recyclableCal)
+    {
+        if (recyclableCal != null)
+        {
+            recyclableCal.clear();
+            return recyclableCal;
         }
+
+        /* Default GregorianCalendar initializes to current time.
+         * Make sure we clear that out before returning, per the
+         * contract of this method.
+         */
+        Calendar result = new java.util.GregorianCalendar();
+        result.clear();
+        return result;
     }
 
     // *********************************************************



Mime
View raw message