OPENJPA-1392, OPENJPA-1393: countDistinct() and integer return type for size()

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@881638 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2009-11-18 00:53:03 +00:00
parent 27996efb48
commit f1451e5fd8
2 changed files with 33 additions and 6 deletions

View File

@ -1403,6 +1403,30 @@ public class TestTypesafeCriteria extends CriteriaTest {
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);
}
}

View File

@ -303,8 +303,8 @@ class Expressions {
@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 @@ class Expressions {
@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) {