openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r885989 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/ openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/ openjpa-persistence-jdbc/...
Date Tue, 01 Dec 2009 22:51:01 GMT
Author: ppoddar
Date: Tue Dec  1 22:51:01 2009
New Revision: 885989

URL: http://svn.apache.org/viewvc?rev=885989&view=rev
Log:
OPENJPA-1409: Preserve type for currentDate/Time expressions

Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/JDBCExpressionFactory.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/CurrentDate.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/ExpressionFactory.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/InMemoryExpressionFactory.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/JDBCExpressionFactory.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/JDBCExpressionFactory.java?rev=885989&r1=885988&r2=885989&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/JDBCExpressionFactory.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/JDBCExpressionFactory.java
Tue Dec  1 22:51:01 2009
@@ -19,6 +19,7 @@
 package org.apache.openjpa.jdbc.kernel.exps;
 
 import java.io.Serializable;
+import java.util.Date;
 
 import org.apache.openjpa.jdbc.meta.ClassMapping;
 import org.apache.openjpa.jdbc.meta.JavaSQLTypes;
@@ -49,13 +50,8 @@
     implements ExpressionFactory, Serializable {
 
     private static final Val NULL = new Null();
-    private static final Val CURRENT_DATE = new CurrentDate(JavaSQLTypes.DATE);
-    private static final Val CURRENT_TIME = new CurrentDate(JavaSQLTypes.TIME);
-    private static final Val CURRENT_TIMESTAMP =
-        new CurrentDate(JavaSQLTypes.TIMESTAMP);
 
-    private static final Localizer _loc = Localizer.forPackage
-        (JDBCExpressionFactory.class);
+    private static final Localizer _loc = Localizer.forPackage(JDBCExpressionFactory.class);
 
     private final ClassMapping _type;
     private final SelectConstructor _cons = new SelectConstructor();
@@ -263,16 +259,16 @@
         return NULL;
     }
 
-    public Value getCurrentDate() {
-        return CURRENT_DATE;
+    public <T extends Date> Value getCurrentDate(Class<T> dateType) {
+        return new CurrentDate(dateType);
     }
 
-    public Value getCurrentTime() {
-        return CURRENT_TIME;
+    public <T extends Date> Value getCurrentTime(Class<T> dateType) {
+        return  new CurrentDate(dateType);
     }
 
-    public Value getCurrentTimestamp() {
-        return CURRENT_TIMESTAMP;
+    public <T extends Date> Value getCurrentTimestamp(Class<T> dateType) {
+        return  new CurrentDate(dateType);
     }
 
     public Parameter newParameter(Object name, Class type) {

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/CurrentDate.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/CurrentDate.java?rev=885989&r1=885988&r2=885989&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/CurrentDate.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/CurrentDate.java
Tue Dec  1 22:51:01 2009
@@ -29,16 +29,25 @@
  */
 class CurrentDate
     extends Val {
-
+    private final Class<? extends Date> _type;
+    
+    public CurrentDate(Class<? extends Date> type) {
+        _type = type;
+    }
+    
     public Class getType() {
-        return Date.class;
+        return _type;
     }
 
     public void setImplicitType(Class type) {
     }
 
-    protected Object eval(Object candidate, Object orig,
-        StoreContext ctx, Object[] params) {
-        return new Date();
+    protected Object eval(Object candidate, Object orig, StoreContext ctx, Object[] params)
{
+        try {
+            _type.getConstructor(long.class).newInstance(System.currentTimeMillis());
+        } catch (Exception e) {
+            return new Date();
+        }
+        return null;
     }
 }

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/ExpressionFactory.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/ExpressionFactory.java?rev=885989&r1=885988&r2=885989&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/ExpressionFactory.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/ExpressionFactory.java
Tue Dec  1 22:51:01 2009
@@ -18,6 +18,10 @@
  */
 package org.apache.openjpa.kernel.exps;
 
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.util.Date;
+
 import org.apache.openjpa.meta.ClassMetaData;
 
 /**
@@ -229,17 +233,17 @@
     /**
      * Return a value representing the current date.
      */
-    public Value getCurrentDate();
+    public <T extends Date> Value getCurrentDate(Class<T> dateType);
 
     /**
      * Return a value representing the current time.
      */
-    public Value getCurrentTime();
+    public <T extends Date> Value getCurrentTime(Class<T> timeType);
 
     /**
      * Return a value representing the current timestamp.
      */
-    public Value getCurrentTimestamp();
+    public <T extends Date> Value getCurrentTimestamp(Class<T> timestampType);
 
     /**
      * Return a value representing a parameter for the given value. The

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/InMemoryExpressionFactory.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/InMemoryExpressionFactory.java?rev=885989&r1=885988&r2=885989&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/InMemoryExpressionFactory.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/InMemoryExpressionFactory.java
Tue Dec  1 22:51:01 2009
@@ -23,6 +23,7 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.Date;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
@@ -49,7 +50,6 @@
     implements ExpressionFactory {
 
     private static final Value NULL = new Null();
-    private static final Value CURRENT_DATE = new CurrentDate();
     private static final Object UNIQUE = new Object();
 
     // list of unbound variables in this query
@@ -493,16 +493,16 @@
         return NULL;
     }
 
-    public Value getCurrentDate() {
-        return CURRENT_DATE;
+    public <T extends Date> Value getCurrentDate(Class<T> dateType) {
+        return new CurrentDate(dateType);
     }
 
-    public Value getCurrentTime() {
-        return CURRENT_DATE;
+    public  <T extends Date> Value getCurrentTime(Class<T> dateType) {
+        return new CurrentDate(dateType);
     }
 
-    public Value getCurrentTimestamp() {
-        return CURRENT_DATE;
+    public <T extends Date> Value getCurrentTimestamp(Class<T> dateType) {
+        return new CurrentDate(dateType);
     }
 
     public Parameter newParameter(Object name, Class type) {

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java?rev=885989&r1=885988&r2=885989&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
Tue Dec  1 22:51:01 2009
@@ -22,9 +22,12 @@
 import java.io.Serializable;
 import java.lang.reflect.Field;
 import java.math.BigDecimal;
+import java.sql.Time;
+import java.sql.Timestamp;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Date;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
@@ -1367,13 +1370,13 @@
                 return evalNot(not, factory.contains(val2, val1));
 
             case JJTCURRENTDATE:
-                return factory.getCurrentDate();
+                return factory.getCurrentDate(Date.class);
 
             case JJTCURRENTTIME:
-                return factory.getCurrentTime();
+                return factory.getCurrentTime(Time.class);
 
             case JJTCURRENTTIMESTAMP:
-                return factory.getCurrentTimestamp();
+                return factory.getCurrentTimestamp(Timestamp.class);
 
             case JJTSELECTEXTENSION:
                 assertQueryExtensions("SELECT");

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java?rev=885989&r1=885988&r2=885989&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
Tue Dec  1 22:51:01 2009
@@ -19,6 +19,7 @@
 package org.apache.openjpa.persistence.criteria;
 
 import java.math.BigDecimal;
+import java.sql.Time;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -27,6 +28,7 @@
 import javax.persistence.Parameter;
 import javax.persistence.Query;
 import javax.persistence.Tuple;
+import javax.persistence.TypedQuery;
 import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.criteria.Expression;
 import javax.persistence.criteria.Join;
@@ -1456,4 +1458,23 @@
 
        List result = q.getResultList();        
     }
+    
+    public void testCurrentTimeReturnsSQLTypes() {
+        em.getTransaction().begin();
+        Product pc = new Product();
+        em.persist(pc);
+        em.getTransaction().commit();
+        
+        int pid = pc.getPid();
+        
+        CriteriaQuery<Time> cquery = cb.createQuery(Time.class);
+        Root<Product> product = cquery.from(Product.class);
+        cquery.select(cb.currentTime());
+        cquery.where(cb.equal(product.get(Product_.pid), pid));
+
+        TypedQuery<Time> tq = em.createQuery(cquery);
+        Object result = tq.getSingleResult();
+        assertTrue(result.getClass() + " not instance of Time", result instanceof Time);
 
+        
+    }
 }

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java?rev=885989&r1=885988&r2=885989&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java
Tue Dec  1 22:51:01 2009
@@ -792,7 +792,7 @@
 
         @Override
         public Value toValue(ExpressionFactory factory, CriteriaQueryImpl<?> q) {
-            return factory.getCurrentDate();
+            return factory.getCurrentDate(getJavaType());
         }
         
         @Override
@@ -808,7 +808,7 @@
 
         @Override
         public Value toValue(ExpressionFactory factory, CriteriaQueryImpl<?> q) {
-            return factory.getCurrentTime();
+            return factory.getCurrentTime(getJavaType());
         }
         
         @Override
@@ -824,7 +824,7 @@
 
         @Override
         public Value toValue(ExpressionFactory factory, CriteriaQueryImpl<?> q) {
-            return factory.getCurrentTimestamp();
+            return factory.getCurrentTimestamp(getJavaType());
         }
         
         @Override



Mime
View raw message