openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r908150 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdb...
Date Tue, 09 Feb 2010 18:36:12 GMT
Author: faywang
Date: Tue Feb  9 18:36:10 2010
New Revision: 908150

URL: http://svn.apache.org/viewvc?rev=908150&view=rev
Log:
OPENJPA-1496: fix temporal parameter processing

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/TimeEntity.java
  (with props)
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestTemporalTypeQueryParameterBinding.java

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java?rev=908150&r1=908149&r2=908150&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java Tue
Feb  9 18:36:10 2010
@@ -238,6 +238,12 @@
             return !strict;
         if (c1 == String.class && isTemporalType(c2))
             return true;
+        if ((c1 == java.util.Date.class ||c1 == java.sql.Time.class) && c2 == java.sql.Timestamp.class)
+            return false;
+        if ((c1 == java.util.Date.class ||c1 == java.sql.Timestamp.class) && c2 ==
java.sql.Time.class)
+            return false;
+        if (isTemporalType(c1) && isTemporalType(c2))
+            return true;
         return false;
     }
 
@@ -972,7 +978,8 @@
         return c != null 
             && (Date.class.isAssignableFrom(c) 
              || Time.class.isAssignableFrom(c) 
-             || Timestamp.class.isAssignableFrom(c));
+             || Timestamp.class.isAssignableFrom(c)
+             || Calendar.class.isAssignableFrom(c));
     }
     
     public static Object getDefaultForNull(Class<?> nType) {

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestTemporalTypeQueryParameterBinding.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestTemporalTypeQueryParameterBinding.java?rev=908150&r1=908149&r2=908150&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestTemporalTypeQueryParameterBinding.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestTemporalTypeQueryParameterBinding.java
Tue Feb  9 18:36:10 2010
@@ -28,6 +28,7 @@
 import javax.persistence.TemporalType;
 
 import org.apache.openjpa.persistence.ArgumentException;
+import org.apache.openjpa.persistence.jdbc.query.domain.TimeEntity;
 import org.apache.openjpa.persistence.jdbc.query.domain.TimeKeeper;
 import org.apache.openjpa.persistence.test.SingleEMFTestCase;
 
@@ -63,16 +64,27 @@
 	private EntityManager em;
 	@Override
 	public void setUp() throws Exception {
-		super.setUp(CLEAR_TABLES, TimeKeeper.class);
+		super.setUp(CLEAR_TABLES, TimeKeeper.class, TimeEntity.class);
 		em = emf.createEntityManager();
 		
 		TimeKeeper pc = new TimeKeeper();
 		pc.setDate(VALUE_DATE);
 		pc.setTime(VALUE_TIME);
 		pc.setTstamp(VALUE_TSTAMP);
-		
+
+        TimeEntity te = new TimeEntity();
+        te.setName("name1");
+        te.setValue(1);
+        te.setCal2Timestamp(PARAM_CALENDAR);
+        te.setCal2Time(PARAM_CALENDAR);
+        te.setCal2Date(PARAM_CALENDAR);
+		te.setUDate2SDate(VALUE_DATE);
+		te.setUDate2Time(VALUE_DATE);
+		te.setUDate2Timestamp(VALUE_DATE);
+        
 		em.getTransaction().begin();
 		em.persist(pc);
+		em.persist(te);
 		em.getTransaction().commit();
 	}
 	
@@ -173,7 +185,61 @@
         }
 	}
 	
-	void verifyParams(String jpql, Class<? extends Exception> error,
+    public void testTemporalType() {
+        EntityManager em = emf.createEntityManager(); 
+        Calendar endTime = PARAM_CALENDAR;
+        Calendar startTime = PARAM_CALENDAR;
+        startTime.add(14, -4);
+        
+        //(1) Calendar to Timestamp
+        String jpql = "SELECT COUNT(a) FROM TimeEntity a WHERE a.cal2Timestamp BETWEEN ?1
AND ?2";
+        Query q = em.createQuery(jpql);
+        assertSetTemporalParameter(q, 1, startTime, TemporalType.TIMESTAMP);
+        assertSetTemporalParameter(q, 2, endTime, TemporalType.TIMESTAMP);
+        assertEquals(1, q.getResultList().size());
+         
+        //(2) Calendar to Time
+        jpql = "SELECT COUNT(a) FROM TimeEntity a WHERE a.cal2Time BETWEEN ?1 AND ?2";
+        q = em.createQuery(jpql);
+        assertSetTemporalParameter(q, 1, startTime, TemporalType.TIME);
+        assertSetTemporalParameter(q, 2, endTime, TemporalType.TIME);
+        assertEquals(1, q.getResultList().size());
+        
+        //(3)Calendar to Date
+        jpql = "SELECT COUNT(a) FROM TimeEntity a WHERE a.cal2Date BETWEEN ?1 AND ?2";
+        q = em.createQuery(jpql);
+        assertSetTemporalParameter(q, 1, startTime, TemporalType.DATE);
+        assertSetTemporalParameter(q, 2, endTime, TemporalType.DATE);
+        q.setParameter(1, startTime, TemporalType.DATE);
+        q.setParameter(2, endTime, TemporalType.DATE);
+        assertEquals(1, q.getResultList().size());
+ 
+        //(4)Date to Timestamp
+        java.util.Date endDate = VALUE_DATE;
+        java.util.Date startDate = new Date(T1 - 1000);
+        jpql = "SELECT COUNT(a) FROM TimeEntity a WHERE a.udate2Timestamp BETWEEN ?1 AND
?2";
+        q = em.createQuery(jpql);
+        assertSetTemporalParameter(q, 1, startDate, TemporalType.TIMESTAMP);
+        assertSetTemporalParameter(q, 2, endDate, TemporalType.TIMESTAMP);
+        assertEquals(1, q.getResultList().size());
+        
+        //(5) Date to Time
+        jpql = "SELECT COUNT(a) FROM TimeEntity a WHERE a.udate2Time BETWEEN ?1 AND ?2";
+        q = em.createQuery(jpql);
+        assertSetTemporalParameter(q, 1, startDate, TemporalType.TIME);
+        assertSetTemporalParameter(q, 2, endDate, TemporalType.TIME);
+        assertEquals(1, q.getResultList().size());
+        
+        //(6) Date to Date
+        jpql = "SELECT COUNT(a) FROM TimeEntity a WHERE a.udate2SDate BETWEEN ?1 AND ?2";
+        q = em.createQuery(jpql);
+        assertSetTemporalParameter(q, 1, startDate, TemporalType.DATE);
+        assertSetTemporalParameter(q, 2, endDate, TemporalType.DATE);
+        assertEquals(1, q.getResultList().size());
+    }
+    
+
+    void verifyParams(String jpql, Class<? extends Exception> error,
         Object... params) {
         EntityManager em = emf.createEntityManager();
         em.getTransaction().begin();
@@ -198,4 +264,22 @@
 		}
 		em.getTransaction().commit();
 	}
+    
+    void assertSetTemporalParameter(Query q, int pos, Date v, TemporalType temporalType)
{
+        try {
+            q.setParameter(pos, v, temporalType);
+        } catch (Exception e) {
+            fail("Fail in setting positional parameter [" + pos + "] to a value of " + v);
+        }
+    }
+
+    void assertSetTemporalParameter(Query q, int pos, Calendar v, TemporalType temporalType)
{
+        try {
+            q.setParameter(pos, v, temporalType);
+        } catch (Exception e) {
+            fail("Fail in setting positional parameter [" + pos + "] to a value of " + v);
+        }
+    }
+
+    
 }

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/TimeEntity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/TimeEntity.java?rev=908150&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/TimeEntity.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/TimeEntity.java
Tue Feb  9 18:36:10 2010
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.jdbc.query.domain;
+
+import java.util.Calendar;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+
+@Entity
+public class TimeEntity {
+	@Id
+	@GeneratedValue
+	private long id;
+	
+    String name;
+    
+    int value;
+    
+    @Temporal(TemporalType.TIMESTAMP)
+    private java.util.Calendar cal2Timestamp;
+
+    @Temporal(TemporalType.TIME)
+    private java.util.Calendar cal2Time;
+
+    @Temporal(TemporalType.DATE)
+    private java.util.Calendar cal2Date;
+
+    @Temporal(TemporalType.TIMESTAMP)
+    private java.util.Date udate2Timestamp;
+
+    @Temporal(TemporalType.TIME)
+    private java.util.Date udate2Time;
+
+    @Temporal(TemporalType.DATE)
+    private java.util.Date udate2SDate;
+
+    public TimeEntity() {
+    }
+
+    public TimeEntity(int id, String name, int value) {
+        this.id = id;
+        this.name = name;
+        this.value = value;
+    }
+
+    public long getId() {
+        return id;
+    }
+    
+    public String getName() {
+        return name;
+    }
+    
+    public void setName(String name) {
+        this.name = name;
+    }
+    
+    public int getValue() {
+        return value;
+    }
+    
+    public void setValue(int value) {
+        this.value = value;
+    }
+    
+    public Calendar getCal2Timestamp() {
+        return cal2Timestamp;
+    }
+
+    public void setCal2Timestamp(Calendar cal2Timestamp) {
+        this.cal2Timestamp = cal2Timestamp;
+    }
+
+    public Calendar getCal2Time() {
+        return cal2Time;
+    }
+
+    public void setCal2Time(Calendar cal2Time) {
+        this.cal2Time = cal2Time;
+    }
+
+    public Calendar getCal2Date() {
+        return cal2Date;
+    }
+
+    public void setCal2Date(Calendar cal2Date) {
+        this.cal2Date = cal2Date;
+    }
+
+    public java.util.Date getUDate2Timestamp() {
+        return udate2Timestamp;
+    }
+
+    public void setUDate2Timestamp(java.util.Date udate2Timestamp) {
+        this.udate2Timestamp = udate2Timestamp;
+    }
+
+    public java.util.Date getUDate2Time() {
+        return udate2Time;
+    }
+
+    public void setUDate2Time(java.util.Date udate2Time) {
+        this.udate2Time = udate2Time;
+    }
+
+    public java.util.Date getUDate2SDate() {
+        return udate2SDate;
+    }
+
+    public void setUDate2SDate(java.util.Date udate2SDate) {
+        this.udate2SDate = udate2SDate;
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/TimeEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message