openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r881638 - in /openjpa/trunk: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java
Date Wed, 18 Nov 2009 00:53:03 GMT
Author: ppoddar
Date: Wed Nov 18 00:53:03 2009
New Revision: 881638

URL: http://svn.apache.org/viewvc?rev=881638&view=rev
Log:
OPENJPA-1392, OPENJPA-1393: countDistinct() and integer return type for size()

Modified:
    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-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=881638&r1=881637&r2=881638&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
Wed Nov 18 00:53:03 2009
@@ -1403,6 +1403,30 @@
                 cb.nullLiteral(String.class)));
 
         Query q = em.createQuery(cquery);
-
+    }
+    
+    public void testCountDistinct() {
+        // JPQL Parser does not do well with the following 
+        String jpql = "select DISTINCT COUNT(a.name) from Account a";
+        
+        CriteriaQuery<Long> c = cb.createQuery(Long.class);
+        Root<Account> a = c.from(Account.class);
+        c.select(cb.countDistinct(a.get(Account_.name)));
+        
+        // hence we do not check equivalence against JPQL 
+        // assertEquivalence(c, jpql);
+        // but check against SQL
+        String expectedSQL = "SELECT COUNT(DISTINCT t0.name) FROM CR_ACCT t0";
+        executeAndCompareSQL(c, expectedSQL);
+    }
+    
+    public void testSizeReturnsInteger() {
+        String jpql = "select SIZE(c.accounts) from Customer c";
+        CriteriaQuery<Integer> c = cb.createQuery(Integer.class);
+        Root<Customer> customer = c.from(Customer.class);
+        c.select(cb.size(customer.get(Customer_.accounts)));
+        
+        assertEquivalence(c, jpql);
+        
     }
 }

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=881638&r1=881637&r2=881638&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
Wed Nov 18 00:53:03 2009
@@ -303,8 +303,8 @@
 
         @Override
         public Value toValue(ExpressionFactory factory, CriteriaQueryImpl<?> q) {
-            Value v = factory.count(Expressions.toValue(e, factory, q));
-            return _distinct ? factory.distinct(v) : v;
+            Value v = Expressions.toValue(e, factory, q);
+            return _distinct ? factory.count(factory.distinct(v)) : factory.count(v);
         }
         
         @Override
@@ -387,11 +387,14 @@
         @Override
         public Value toValue(ExpressionFactory factory, CriteriaQueryImpl<?> q) {
             Value val = Expressions.toValue(e, factory, q);
+            Value result;
             if (val instanceof Literal && ((Literal)val).getParseType() == Literal.TYPE_COLLECTION)
-                return factory.newLiteral(((Collection)((Literal)val).getValue()).size(),

+                result = factory.newLiteral(((Collection)((Literal)val).getValue()).size(),

                     Literal.TYPE_NUMBER);
-                
-            return factory.size(val);
+            else
+                result = factory.size(val);
+            result.setImplicitType(Integer.class);
+            return result;
         }
         
         public StringBuilder asValue(AliasContext q) {



Mime
View raw message