cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r439457 - in /incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src: main/java/org/apache/cayenne/access/QueryLogger.java main/java/org/apache/cayenne/access/types/CalendarType.java test/java/org/apache/cayenne/access/DateTimeTypesTst.java
Date Fri, 01 Sep 2006 20:17:53 GMT
Author: aadamchik
Date: Fri Sep  1 13:17:52 2006
New Revision: 439457

URL: http://svn.apache.org/viewvc?rev=439457&view=rev
Log:
CAY-645 - improving calendar logging and adding handling of TIME and DATE mappings

Modified:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/QueryLogger.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/CalendarType.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DateTimeTypesTst.java

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/QueryLogger.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/QueryLogger.java?rev=439457&r1=439456&r2=439457&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/QueryLogger.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/QueryLogger.java
Fri Sep  1 13:17:52 2006
@@ -107,6 +107,10 @@
             long time = ((java.util.Date) anObject).getTime();
             buf.append('\'').append(new java.sql.Timestamp(time)).append('\'');
         }
+        else if (anObject instanceof java.util.Calendar) {
+            long time = ((java.util.Calendar) anObject).getTimeInMillis();
+            buf.append("calendar<").append(new java.sql.Timestamp(time)).append('>');
+        }
         // 6. byte[]
         else if (anObject instanceof byte[]) {
             buf.append("< ");

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/CalendarType.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/CalendarType.java?rev=439457&r1=439456&r2=439457&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/CalendarType.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/CalendarType.java
Fri Sep  1 13:17:52 2006
@@ -21,7 +21,6 @@
 import java.sql.CallableStatement;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
-import java.sql.Timestamp;
 import java.sql.Types;
 import java.util.Calendar;
 import java.util.Date;
@@ -38,9 +37,9 @@
  * @author Andrus Adamchik
  */
 public class CalendarType implements ExtendedType {
-    
+
     protected Class calendarClass;
-    
+
     public CalendarType(Class calendarClass) {
         if (calendarClass == null) {
             throw new IllegalArgumentException("Null calendar class");
@@ -50,7 +49,7 @@
             throw new IllegalArgumentException(
                     "Must be a java.util.Calendar or a subclass: " + calendarClass);
         }
-    
+
         this.calendarClass = calendarClass;
     }
 
@@ -89,10 +88,10 @@
                 break;
         }
 
-        if(rs.wasNull()) {
+        if (rs.wasNull()) {
             return null;
         }
-        
+
         GregorianCalendar calendar = new GregorianCalendar();
         calendar.setTime(val);
         return calendar;
@@ -129,10 +128,10 @@
                 break;
         }
 
-        if(rs.wasNull()) {
+        if (rs.wasNull()) {
             return null;
         }
-        
+
         GregorianCalendar calendar = new GregorianCalendar();
         calendar.setTime(val);
         return calendar;
@@ -146,16 +145,32 @@
             int precision) throws Exception {
 
         if (value == null) {
-            statement.setNull(pos, Types.TIMESTAMP);
+            statement.setNull(pos, type);
         }
         else if (value instanceof Calendar) {
+
             Calendar calendar = (Calendar) value;
-            statement.setTimestamp(pos, new Timestamp(calendar.getTimeInMillis()));
+            statement.setObject(pos, convertToJdbcObject(calendar, type));
         }
         else {
             throw new IllegalArgumentException("Expected java.util.Calendar, got "
                     + value.getClass().getName());
         }
+    }
+
+    protected Object convertToJdbcObject(Calendar value, int type) throws Exception {
+        Calendar calendar = (Calendar) value;
+
+        if (type == Types.DATE)
+            return new java.sql.Date(calendar.getTimeInMillis());
+        else if (type == Types.TIME)
+            return new java.sql.Time(calendar.getTimeInMillis());
+        else if (type == Types.TIMESTAMP)
+            return new java.sql.Timestamp(calendar.getTimeInMillis());
+        else
+            throw new IllegalArgumentException("Only date/time types can be used for '"
+                    + getClassName()
+                    + "'.");
     }
 
     public boolean validateProperty(

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DateTimeTypesTst.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DateTimeTypesTst.java?rev=439457&r1=439456&r2=439457&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DateTimeTypesTst.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DateTimeTypesTst.java
Fri Sep  1 13:17:52 2006
@@ -46,18 +46,21 @@
     public void testCalendar() throws Exception {
 
         CalendarTest test = (CalendarTest) context.newObject(CalendarTest.class);
-        
+
         Calendar cal = Calendar.getInstance();
         cal.clear();
         cal.set(2002, 1, 1);
-        
+
         test.setCalendarField(cal);
         context.commitChanges();
-        
+
         SelectQuery q = new SelectQuery(CalendarTest.class);
         CalendarTest testRead = (CalendarTest) context.performQuery(q).get(0);
         assertNotNull(testRead.getCalendarField());
         assertEquals(cal, testRead.getCalendarField());
+
+        test.setCalendarField(null);
+        context.commitChanges();
     }
 
     public void testDate() throws Exception {



Mime
View raw message