openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r810685 - 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 Wed, 02 Sep 2009 19:47:11 GMT
Author: ppoddar
Date: Wed Sep  2 19:47:10 2009
New Revision: 810685

URL: http://svn.apache.org/viewvc?rev=810685&view=rev
Log:
OPENJPA-1276: preserve precedence order while rendering logical operators/operands

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestCQL.java
  (with props)
Modified:
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PredicateImpl.java

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestCQL.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestCQL.java?rev=810685&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestCQL.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestCQL.java
Wed Sep  2 19:47:10 2009
@@ -0,0 +1,67 @@
+/*
+ * 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.criteria;
+
+import javax.persistence.criteria.Predicate;
+import javax.persistence.criteria.Root;
+
+/**
+ * Test logical predicates precedence is preserved and rendered correctly in CQL.
+ * 
+ * @author Pinaki Poddar
+ *
+ */
+public class TestCQL extends CriteriaTest {
+    public void testLogicalPrecedence() {
+        OpenJPACriteriaQuery<Person> q = cb.createQuery(Person.class);
+        Root<Person> p = q.from(Person.class);
+        q.select(p);
+        Predicate a = cb.equal(p.get(Person_.name), "A");
+        Predicate b = cb.equal(p.get(Person_.name), "B");
+        Predicate c = cb.equal(p.get(Person_.name), "C");
+        Predicate d = cb.equal(p.get(Person_.name), "D");
+        
+        // (a OR b) AND (c or D)
+        q.where(cb.or(a,b), cb.or(c,d));
+        
+        String jpql = "";
+        // The strings are compared for exact match so be careful about spaces and such...

+        jpql = "SELECT p FROM Person p WHERE ((p.name = 'A' OR p.name = 'B') AND (p.name
= 'C' OR p.name = 'D'))";
+        assertEquivalence(q, jpql);
+        assertEquals(jpql, q.toCQL());
+        
+        // (a OR b or C) AND D
+        q.where(cb.or(a,b,c), d);
+        jpql = "SELECT p FROM Person p WHERE ((p.name = 'A' OR p.name = 'B' OR p.name = 'C')
AND p.name = 'D')";
+        assertEquivalence(q, jpql);
+        assertEquals(jpql, q.toCQL());
+        
+        // a AND (b OR c) AND d 
+        q.where(a, cb.or(b,c), d);
+        jpql = "SELECT p FROM Person p WHERE (p.name = 'A' AND (p.name = 'B' OR p.name =
'C') AND p.name = 'D')";
+        assertEquivalence(q, jpql);
+        assertEquals(jpql, q.toCQL());
+        
+        // a OR (b AND (c OR d)) 
+        q.where(cb.or(a, cb.and(b, cb.or(c,d))));
+        jpql = "SELECT p FROM Person p WHERE (p.name = 'A' OR (p.name = 'B' AND (p.name =
'C' OR p.name = 'D')))";
+        assertEquivalence(q, jpql);
+        assertEquals(jpql, q.toCQL());
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestCQL.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java?rev=810685&r1=810684&r2=810685&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java
Wed Sep  2 19:47:10 2009
@@ -92,7 +92,7 @@
      *  @param resultClass  type of the query result
      *  @return query object
      */
-    public <T> CriteriaQuery<T> createQuery(Class<T> resultClass) {
+    public <T> OpenJPACriteriaQuery<T> createQuery(Class<T> resultClass)
{
         return new CriteriaQueryImpl<T>(_model, resultClass);
     }
 
@@ -101,7 +101,7 @@
      *  objects as its result.
      *  @return query object
      */
-    public CriteriaQuery<Tuple> createTupleQuery() {
+    public OpenJPACriteriaQuery<Tuple> createTupleQuery() {
         return new CriteriaQueryImpl<Tuple>(_model, Tuple.class);
     }
 
@@ -498,7 +498,7 @@
     }
 
     public Predicate or(Predicate... restrictions) {
-        return new PredicateImpl.Or();
+        return new PredicateImpl.Or(restrictions);
     }
 
     public Predicate or(Expression<Boolean> x, Expression<Boolean> y) {

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=810685&r1=810684&r2=810685&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
Wed Sep  2 19:47:10 2009
@@ -141,8 +141,11 @@
     }
     
     public StringBuilder asValue(CriteriaQueryImpl<?> q) {
-        return Expressions.asValue(q, _exps == null ? null : _exps.toArray(new Expression<?>[_exps.size()]),

-            " " +_op + " ");
+        boolean braces = _exps != null && _exps.size() > 1;
+        StringBuilder buffer =  Expressions.asValue(q, _exps == null ? null : 
+            _exps.toArray(new Expression<?>[_exps.size()]), " " +_op + " ");
+        if (braces) buffer.insert(0, "(").append(")");
+        return buffer;
     }
 
     



Mime
View raw message