openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r836023 - 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 Fri, 13 Nov 2009 22:18:33 GMT
Author: ppoddar
Date: Fri Nov 13 22:18:32 2009
New Revision: 836023

URL: http://svn.apache.org/viewvc?rev=836023&view=rev
Log:
Literal value for empty disjunction and conjunction 

Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/AbstractCriteriaTestCase.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestEdit.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/CriteriaBuilderImpl.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PredicateImpl.java

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/AbstractCriteriaTestCase.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/AbstractCriteriaTestCase.java?rev=836023&r1=836022&r2=836023&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/AbstractCriteriaTestCase.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/AbstractCriteriaTestCase.java
Fri Nov 13 22:18:32 2009
@@ -103,51 +103,41 @@
     /**
      * Executes the given CriteriaQuery and JPQL string and compare their respective SQLs
for equality.
      */
-    void assertEquivalence(CriteriaQuery<?> c, String jpql, String expectedSQL) {
-        assertEquivalence(c, jpql, null, null, expectedSQL);
-    }
-
-    /**
-     * Executes the given CriteriaQuery and JPQL string and compare their respective SQLs
for equality.
-     */
     void assertEquivalence(CriteriaQuery<?> c, String jpql) {
-        System.err.println("JPQL :" + jpql);
-        System.err.println("CJQL :" + ((CriteriaQueryImpl<?>)c).toCQL());
-        assertEquivalence(c, jpql, null, null, null);
+        assertEquivalence(null, c, jpql, null);
     }
-
+    
     /**
-     * Executes the given CriteriaQuery and JPQL string and compare their respective SQLs
for equality. Sets the
-     * supplied parameters, if any.
+     * Executes the given CriteriaQuery and JPQL string and compare their respective SQLs
for equality
+     * with the expected SQL.
      */
-    void assertEquivalence(CriteriaQuery<?> c, String jpql, String[] paramNames, Object[]
params) {
-        assertEquivalence(c, jpql, paramNames, params, null);
+    void assertEquivalence(CriteriaQuery<?> c, String jpql, String expectedSQL) {
+        assertEquivalence(null, c, jpql, expectedSQL);
     }
     
     /**
-     * Executes the given CriteriaQuery and JPQL string and compare their respective SQLs
for equality. Sets the
-     * supplied parameters, if any.
+     * Executes the given CriteriaQuery and JPQL string after decorating with the given decorator,
+     * and then compare their respective SQLs for equality.
      */
-    void assertEquivalence(CriteriaQuery<?> c, String jpql, String[] paramNames, Object[]
params,
-        String expectedSQL) {
-        Query cQ = getEntityManager().createQuery(c);
-        Query jQ = getEntityManager().createQuery(jpql);
-        setParameters(cQ, paramNames, params);
-        setParameters(jQ, paramNames, params);
-
-        executeAndCompareSQL(jpql, cQ, jQ, expectedSQL);
+    void assertEquivalence(QueryDecorator decorator, CriteriaQuery<?> c, String jpql)
{
+        assertEquivalence(decorator, c, jpql, null);
     }
 
     /**
-     * Executes the given CriteriaQuery and JPQL string and compare their respective SQLs
for equality.
+     * Executes the given CriteriaQuery and JPQL string and compare their respective SQLs
for equality. 
+     * Decorates the query with the given decorator before execution.
+     * supplied parameters, if any.
      */
-    void assertEquivalence(CriteriaQuery<?> c, String jpql, Object[] params) {
-        Query jQ = getEntityManager().createQuery(jpql);
+    void assertEquivalence(QueryDecorator decorator, CriteriaQuery<?> c, String jpql,
String expectedSQL) {
+        System.err.println("JPQL:[" + jpql + "]");
+        System.err.println("CQL :[" + ((OpenJPACriteriaQuery<?>)c).toCQL());
         Query cQ = getEntityManager().createQuery(c);
-        setParameters(cQ, params);
-        setParameters(jQ, params);
-
-        executeAndCompareSQL(jpql, cQ, jQ, null);
+        Query jQ = getEntityManager().createQuery(jpql);
+        if (decorator != null) {
+            decorator.decorate(cQ);
+            decorator.decorate(jQ);
+        }
+        executeAndCompareSQL(jpql, cQ, jQ, expectedSQL);
     }
 
     /**
@@ -308,15 +298,6 @@
         }
     }
 
-    void setParameters(Query q, String[] paramNames, Object[] params) {
-        for (int i = 0; paramNames != null && i < paramNames.length; i++)
-            q.setParameter(paramNames[i], params[i]);
-    }
-
-    void setParameters(Query q, Object[] params) {
-        for (int i = 0; params != null && i < params.length; i++)
-            q.setParameter(i + 1, params[i]);
-    }
 
     /**
      * Execute the given query and return the generated SQL. If the query execution fail
because the generated SQL is
@@ -405,4 +386,12 @@
             return new ArrayList<String>(sqls);
         }
     }
+    
+    /**
+     * Interface to decorate a query such as set parameter or range before execution.
+     *
+     */
+    public interface QueryDecorator {
+        void decorate(Query q);
+    }
 }

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestEdit.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestEdit.java?rev=836023&r1=836022&r2=836023&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestEdit.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestEdit.java
Fri Nov 13 22:18:32 2009
@@ -198,12 +198,21 @@
         Root<Person> p = c.from(Person.class);
         c.where(cb.equal(p.get(Person_.name), cb.parameter(String.class, "p1")));
         
-        assertEquivalence(c, jpql, new String[]{"p1"}, new String[]{"XYZ"});
+        assertEquivalence(new QueryDecorator() {
+            public void decorate(Query q) {
+                q.setParameter("p1", "XYZ");
+            }
+        }, c, jpql);
         
         Predicate where = c.getRestriction();
         c.where(cb.and(where, cb.equal(p.get(Person_.name), cb.parameter(String.class, "p2"))));
         
-        assertEquivalence(c, editedjpql, new String[]{"p1", "p2"}, new String[]{"MNO", "ABC"});
+        assertEquivalence(new QueryDecorator() {
+            public void decorate(Query q) {
+                q.setParameter("p1", "MNO");
+                q.setParameter("p2", "ABC");
+            }
+        }, c, editedjpql);
     }
     
     public void testEditParameterizedPredicateReplaced() {
@@ -215,11 +224,20 @@
         c.where(cb.and(cb.equal(p.get(Person_.name), cb.parameter(String.class, "p1")),
                        cb.equal(p.get(Person_.name), cb.parameter(String.class, "p2"))));
         assertEquals(2,c.getParameters().size());
-        assertEquivalence(c, jpql, new String[]{"p1", "p2"}, new String[]{"XYZ", "ABC"});
+        assertEquivalence(new QueryDecorator() {
+            public void decorate(Query q) {
+                q.setParameter("p1", "XYZ");
+                q.setParameter("p2", "ABC");
+            }
+        }, c, jpql);
         
         c.where(cb.equal(p.get(Person_.name), cb.parameter(String.class, "p3")));
         
-        assertEquivalence(c, editedjpql, new String[]{"p3"}, new String[]{"MNO"});
+        assertEquivalence(new QueryDecorator() {
+            public void decorate(Query q) {
+                q.setParameter("p3", "MNO");
+            }
+        }, c, editedjpql);
     }
     
     public void testEditParameterizedPredicateRemoved() {
@@ -231,11 +249,20 @@
         c.where(cb.and(cb.equal(p.get(Person_.name), cb.parameter(String.class, "p1")),
                        cb.equal(p.get(Person_.name), cb.parameter(String.class, "p2"))));
         assertEquals(2,c.getParameters().size());
-        assertEquivalence(c, jpql, new String[]{"p1", "p2"}, new String[]{"XYZ", "ABC"});
+        assertEquivalence(new QueryDecorator() {
+            public void decorate(Query q) {
+                q.setParameter("p1", "XYZ");
+                q.setParameter("p2", "ABC");
+            }
+        }, c, jpql);
         
         c.where(cb.equal(p.get(Person_.name), cb.parameter(String.class, "p1")));
         
-        assertEquivalence(c, editedjpql, new String[]{"p1"}, new String[]{"MNO"});
+        assertEquivalence(new QueryDecorator() {
+            public void decorate(Query q) {
+                q.setParameter("p1", "MNO");
+            }
+        }, c, editedjpql);
     }
     
     public void testSerachWithinResult() {

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=836023&r1=836022&r2=836023&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
Fri Nov 13 22:18:32 2009
@@ -1374,4 +1374,19 @@
         }, c, jpql);
     }
 
+    public void testEmptyAnd() {
+        CriteriaQuery<Order> c = cb.createQuery(Order.class);
+        Root<Order> order = c.from(Order.class);
+        c.where(cb.and(cb.not(cb.equal(order.get(Order_.customer).get(Customer_.name), "Robert
E. Bissett")),
+                cb.isTrue(cb.conjunction())));
+        em.createQuery(c).getResultList();
+    }
+    
+    public void testEmptyOr() {
+        CriteriaQuery<Order> c = cb.createQuery(Order.class);
+        Root<Order> order = c.from(Order.class);
+        c.where(cb.and(cb.not(cb.equal(order.get(Order_.customer).get(Customer_.name), "Robert
E. Bissett")),
+                cb.isTrue(cb.disjunction())));
+        em.createQuery(c).getResultList();
+    }
 }

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilderImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilderImpl.java?rev=836023&r1=836022&r2=836023&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilderImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilderImpl.java
Fri Nov 13 22:18:32 2009
@@ -39,6 +39,7 @@
 import javax.persistence.criteria.Predicate;
 import javax.persistence.criteria.Selection;
 import javax.persistence.criteria.Subquery;
+import javax.persistence.criteria.Predicate.BooleanOperator;
 import javax.persistence.metamodel.Attribute;
 import javax.persistence.metamodel.ManagedType;
 import javax.persistence.metamodel.Metamodel;
@@ -328,7 +329,13 @@
     }
 
     public Predicate isTrue(Expression<Boolean> x) {
-        return new Expressions.Equal(x, false);
+        if (x instanceof PredicateImpl) {
+            PredicateImpl predicate = (PredicateImpl)x; 
+            if (predicate.isEmpty()) {
+                return predicate.getOperator() == BooleanOperator.AND ? PredicateImpl.TRUE
: PredicateImpl.FALSE;
+            }
+        }
+        return new Expressions.Equal(x, true);
     }
 
     public <K, M extends Map<K, ?>> Expression<Set<K>> keys(M map)
{

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PredicateImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PredicateImpl.java?rev=836023&r1=836022&r2=836023&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PredicateImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PredicateImpl.java
Fri Nov 13 22:18:32 2009
@@ -26,6 +26,7 @@
 import javax.persistence.criteria.Predicate;
 
 import org.apache.openjpa.kernel.exps.ExpressionFactory;
+import org.apache.openjpa.kernel.exps.Literal;
 
 /**
  * Predicate is a expression that evaluates to true or false.
@@ -96,6 +97,10 @@
     public final BooleanOperator getOperator() {
         return _op;
     }
+    
+    public final boolean isEmpty() {
+        return _exps.isEmpty();
+    }
 
     /**
      * Is this predicate created by negating another predicate?
@@ -123,6 +128,9 @@
     
     @Override
     org.apache.openjpa.kernel.exps.Value toValue(ExpressionFactory factory, CriteriaQueryImpl<?>
q) {
+        if (_exps.isEmpty()) {
+            return factory.newLiteral(_op == BooleanOperator.AND, Literal.TYPE_BOOLEAN);
+        }
         throw new AbstractMethodError(this.getClass().getName());
     }
     



Mime
View raw message