openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r776435 - in /openjpa/trunk: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/
Date Tue, 19 May 2009 20:04:10 GMT
Author: faywang
Date: Tue May 19 20:04:10 2009
New Revision: 776435

URL: http://svn.apache.org/viewvc?rev=776435&view=rev
Log:
OPENJPA-1013: NOT IN/ IS (NOT) NULL/LIKE support

Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypeSafeCondExpression.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/CriteriaQueryImpl.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypeSafeCondExpression.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypeSafeCondExpression.java?rev=776435&r1=776434&r2=776435&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypeSafeCondExpression.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypeSafeCondExpression.java
Tue May 19 20:04:10 2009
@@ -54,8 +54,6 @@
  * The test scenarios are from TestEJBQLCondExpression in 
  * org.apache.openjpa.persistence.jpql.expressions. 
  * 
- * @author Fay Wang
- *
  */
 public class TestTypeSafeCondExpression extends SQLListenerTestCase {
 
@@ -114,7 +112,7 @@
         String query = "SELECT o FROM CompUser o";
         CriteriaQuery c = cb.create();
         c.from(CompUser.class);
-
+        assertEquivalence(c, query);
         List result = em.createQuery(c).getResultList();
 
         assertNotNull("the list is null", result);
@@ -132,7 +130,7 @@
         q.where(cb.and(cb.between(c.get(CompUser_.age), 19, 40), 
                 cb.equal(c.get(CompUser_.computerName), "PC")));
         q.select(c.get(CompUser_.name));
-
+        assertEquivalence(q, query);
         List result = em.createQuery(q).getResultList();
 
         assertNotNull("the list is null", result);
@@ -153,6 +151,7 @@
         q.where(cb.and(cb.between(c.get(CompUser_.age), 19, 40).negate(), 
                 cb.equal(c.get(CompUser_.computerName), "PC")));
         q.select(c.get(CompUser_.name));
+        assertEquivalence(q, query);
         List result = em.createQuery(q).getResultList();
 
         assertNotNull("the list is null", result);
@@ -168,7 +167,7 @@
         Root<CompUser> c = q.from(CompUser.class);
         q.where(cb.in(c.get(CompUser_.age)).value(29).value(40).value(10));
         q.select(c.get(CompUser_.name));
-
+        assertEquivalence(q, query);
         List result = em.createQuery(q).getResultList();
 
         assertNotNull("the list is null", result);
@@ -188,6 +187,7 @@
         Root<CompUser> c = q.from(CompUser.class);
         q.where(cb.in(c.get(CompUser_.age)).value(29).value(40).value(10).negate());
         q.select(c.get(CompUser_.name));
+        assertEquivalence(q, query);
         List result = em.createQuery(q).getResultList();
 
         assertNotNull(result);
@@ -198,7 +198,6 @@
         em.clear();
     }
 
-    @AllowFailure
     public void testLikeExprUsingCriteria1() {
         String query =
             "SELECT o.computerName FROM CompUser o WHERE o.name LIKE 'Sha%' AND " + 
@@ -206,10 +205,13 @@
 
         CriteriaQuery q = cb.create();
         Root<CompUser> c = q.from(CompUser.class);
-        q.where(cb.and(cb.like(c.get(CompUser_.name),"Sha%")), 
-                cb.in(c.get(CompUser_.computerName)).value("PC").negate());
-        q.select(c.get(CompUser_.computerName));
+        q.where(cb.and(
+                    cb.like(c.get(CompUser_.name),"Sha%"), 
+                    cb.in(c.get(CompUser_.computerName)).value("PC").negate()
+                ));
         
+        q.select(c.get(CompUser_.computerName));
+        assertEquivalence(q, query);
         List result = em.createQuery(q).getResultList();
 
         assertNotNull(result);
@@ -225,10 +227,12 @@
 
         CriteriaQuery q = cb.create();
         Root<CompUser> c = q.from(CompUser.class);
-        q.where(cb.and(cb.like(c.get(CompUser_.name),"Sha%o_")), 
-                cb.in(c.get(CompUser_.computerName)).value("PC").negate());
+        q.where(cb.and(
+                    cb.like(c.get(CompUser_.name),"Sha%o_"), 
+                    cb.in(c.get(CompUser_.computerName)).value("PC").negate()
+                ));
         q.select(c.get(CompUser_.computerName));
-
+        assertEquivalence(q, query);
         List result = em.createQuery(q).getResultList();
 
         assertNotNull(result);
@@ -244,6 +248,7 @@
         Root<CompUser> c = q.from(CompUser.class);
         q.where(cb.like(c.get(CompUser_.name),"_J%"));
         q.select(c.get(CompUser_.name));
+        assertEquivalence(q, query);
         List result = em.createQuery(q).getResultList();
 
         assertNotNull(result);
@@ -259,7 +264,7 @@
         Parameter<String> param = cb.parameter(String.class);
         q.where(cb.like(c.get(CompUser_.name), param, '|'));
         q.select(c.get(CompUser_.name));
-
+        assertEquivalence(q, query);
         List result = em.createQuery(q).setParameter(1, "%|_%").getResultList();
 
         assertNotNull(result);
@@ -268,7 +273,6 @@
         em.clear();
     }
 
-    @AllowFailure
     public void testNullExprUsingCriteria() {
         String query =
             "SELECT o.name FROM CompUser o WHERE o.age IS NOT NULL AND o.computerName = 'PC'
";
@@ -278,7 +282,7 @@
         q.where(cb.and(cb.notEqual(c.get(CompUser_.age), null), 
                 cb.equal(c.get(CompUser_.computerName), "PC")));
         q.select(c.get(CompUser_.name));
-
+        assertEquivalence(q, query);
         List result = em.createQuery(q).getResultList();
 
         assertNotNull("the list is null", result);
@@ -290,7 +294,6 @@
         em.clear();
     }
     
-    @AllowFailure
     public void testNullExpr2UsingCriteria() {
         String query =
             "SELECT o.name FROM CompUser o WHERE o.address.country IS NULL";
@@ -300,7 +303,7 @@
         Parameter<String> param = cb.parameter(String.class);
         q.where(cb.equal(c.get(CompUser_.address).get(Address_.country), null));
         q.select(c.get(CompUser_.name));
-
+        assertEquivalence(q, query);
         List result = em.createQuery(q).getResultList();
 
         assertNotNull("the list is null", result);
@@ -319,7 +322,7 @@
         Root<CompUser> c = q.from(CompUser.class);
         //q.where(cb.isNotEmpty(c.get(CompUser_.nicknames)));
         q.select(c);
-
+        assertEquivalence(q, query);
         List result = em.createQuery(q).getResultList();
 
         assertNotNull("the list is null", result);
@@ -342,7 +345,7 @@
         sq.where(cb.equal(c, o.get(CompUser_.address)));
         q.where(cb.exists(sq));
         q.select(o.get(CompUser_.name)).distinct(true);
-                
+        assertEquivalence(q, query);
         List result = em.createQuery(q).getResultList();
 
         assertNotNull("the list is null", result);
@@ -371,7 +374,7 @@
                 o.get(CompUser_.address).get(Address_.country)));
         q.where(cb.exists(sq).negate());
         q.select(o.get(CompUser_.name)).distinct(true);
-
+        assertEquivalence(q, query);
         List result = em.createQuery(q).getResultList();
 
         assertNotNull("list is null", result);
@@ -394,6 +397,7 @@
         sq.select(s.get(CompUser_.computerName));
         sq.where(cb.notEqual(s.get(CompUser_.address).get(Address_.country), null));
         q.where(cb.equal(o.get(CompUser_.address).get(Address_.zipCode), cb.any(sq)));
+        assertEquivalence(q, query);
         List result = em.createQuery(q).getResultList();
 
         assertNotNull("list is null", result);
@@ -434,6 +438,21 @@
         em.getTransaction().commit();
     }
     
+    void assertEquivalence(CriteriaQuery c, String jpql) {
+        
+        sql.clear(); 
+        List cList = em.createQuery(c).getResultList();
+        assertEquals(1, sql.size()); 
+        String cSQL = sql.get(0);
+        
+        sql.clear();
+        List jList = em.createQuery(jpql).getResultList();
+        assertEquals(1, sql.size());
+        String jSQL = sql.get(0);
+
+        assertEquals(jSQL, cSQL);
+    }
+
     public CompUser createUser(String name, String cName, Address add, int age,
         boolean isMale) {
         CompUser user = null;

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=776435&r1=776434&r2=776435&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 May 19 20:04:10 2009
@@ -46,9 +46,6 @@
  * asserting that the resultant SQL queries for these two alternative form
  * of executing a query are the same.
  * 
- * 
- *  
- * @author Pinaki Poddar
  *
  */
 public class TestTypesafeCriteria extends SQLListenerTestCase {

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java?rev=776435&r1=776434&r2=776435&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java
Tue May 19 20:04:10 2009
@@ -288,8 +288,9 @@
     }
 
     void setImplicitTypes(Value val1, Value val2, Class<?> expected) {
-        Class<?> c1 = val1.getType();
-        Class<?> c2 = val2.getType();
+        Class<?> c1 = (val1 == null ? null : val1.getType());
+        Class<?> c2 = (val2 == null ? null : val2.getType());
+        
         boolean o1 = c1 == AbstractExpressionBuilder.TYPE_OBJECT;
         boolean o2 = c2 == AbstractExpressionBuilder.TYPE_OBJECT;
 
@@ -305,8 +306,9 @@
             // we never expect a pc type, so don't bother with metadata
             val1.setImplicitType(expected);
             val2.setImplicitType(expected);
-        } else if (AbstractExpressionBuilder.isNumeric(val1.getType()) 
-            != AbstractExpressionBuilder.isNumeric(val2.getType())) {
+        } else if (c1 != null && c2 != null && 
+            AbstractExpressionBuilder.isNumeric(c1) 
+            != AbstractExpressionBuilder.isNumeric(c2)) {
             AbstractExpressionBuilder.convertTypes(val1, val2);
         }
 

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=776435&r1=776434&r2=776435&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 May 19 20:04:10 2009
@@ -40,8 +40,8 @@
 	
 	static Value toValue(ExpressionImpl<?> e, ExpressionFactory factory, 
 		MetamodelImpl model, CriteriaQuery q) {
-		Value v = e.toValue(factory, model, q);
-		return v;
+		return (e == null ? factory.getNull() : 
+		    e.toValue(factory, model, q));
 	}
 	
 	/**
@@ -695,8 +695,6 @@
     	ExpressionImpl<String> str;
     	ExpressionImpl<String> pattern;
     	ExpressionImpl<Character> escapeChar;
-    	static ExpressionImpl<Character> defaultEscape = 
-    		new Constant<Character>(new Character(' '));
     	
     	public Like(Expression<String> x, Expression<String> pattern,
     	        Expression<Character> escapeChar) {
@@ -711,11 +709,11 @@
     	}
     	
     	public Like(Expression<String> x, Expression<String> pattern) {
-    		this(x, pattern, defaultEscape);
+    		this(x, pattern, null);
     	}
     	
     	public Like(Expression<String> x, String pattern) {
-    		this(x, new Constant<String>(pattern), defaultEscape);
+    		this(x, new Constant<String>(pattern), null);
     	}
     	public Like(Expression<String> x, String pat,  
     		Expression<Character> esc) {
@@ -729,10 +727,12 @@
         public org.apache.openjpa.kernel.exps.Expression toKernelExpression(
         	ExpressionFactory factory, MetamodelImpl model, 
         	CriteriaQuery q) {
+    	    String escapeStr = escapeChar == null ? null :
+    	        Expressions.toValue(escapeChar, factory, model, q).toString();
             return factory.matches(
             	Expressions.toValue(str, factory, model, q), 
             	Expressions.toValue(pattern, factory, model, q), "_", "%", 
-            	Expressions.toValue(escapeChar, factory, model, q).toString());
+            	escapeStr);
         }
     }
     
@@ -808,6 +808,7 @@
     public static class In<T> extends PredicateImpl.Or 
     	implements QueryBuilder.In<T> {
     	ExpressionImpl<?> e;
+    	boolean negate;
     	public In(Expression<?> e) {
     		super((Predicate[])null);
     		this.e = (ExpressionImpl<?>)e;
@@ -826,13 +827,23 @@
     		add(new Expressions.Equal(e,value));
         	return this;
     	}
+    	
+    	public In<T> negate() {
+    	    this.negate = true;
+    	    return this;
+    	}
     
     	@Override
         org.apache.openjpa.kernel.exps.Expression toKernelExpression(
             ExpressionFactory factory, MetamodelImpl model, CriteriaQuery q) {
+    	    org.apache.openjpa.kernel.exps.Expression inExpr = 
+    	        super.toKernelExpression(factory, model, q); 
     		IsNotNull notNull = new Expressions.IsNotNull(e);
+    		if (negate) 
+    		    inExpr = factory.not(inExpr);
+    		
     		return factory.and(
-     		    super.toKernelExpression(factory, model, q),
+    		    inExpr,
     		    notNull.toKernelExpression(factory, model, q));
     	}
     }



Mime
View raw message