Added Funcs.countStar() because it's such a common SQL operation.

This commit is contained in:
Michael Bayne
2010-12-18 18:01:57 +00:00
parent 5dc96f1ac0
commit 1b21df9994
2 changed files with 10 additions and 1 deletions
@@ -53,6 +53,15 @@ public class Funcs
return new Average<Number>(expr, true); return new Average<Number>(expr, true);
} }
/**
* Creates an aggregate expression that counts the number of rows that match the other clauses
* in this query.
*/
public static FluentExp<Integer> countStar ()
{
return new Count(Exps.literal("*"));
}
/** /**
* Creates an aggregate expression that counts the number of rows from the supplied * Creates an aggregate expression that counts the number of rows from the supplied
* expression. This would usually be used in a FieldOverride and supplied with a ColumnExp. * expression. This would usually be used in a FieldOverride and supplied with a ColumnExp.
@@ -373,7 +373,7 @@ public class Query<T extends PersistentRecord>
*/ */
public int selectCount () public int selectCount ()
{ {
checkState(_groupBy == null, "Do you mean to select(Funcs.count(Exps.literal(\"*\")))?"); checkState(_groupBy == null, "Do you mean to select(Funcs.countStar())?");
_fromOverride = new FromOverride(_pclass); _fromOverride = new FromOverride(_pclass);
return _repo.load(CountRecord.class, _cache, getClauseArray()).count; return _repo.load(CountRecord.class, _cache, getClauseArray()).count;
} }