Added Exps.count() and Exps.countDistinct() and the necessary plumbing to sneak
a 'distinct' into the argument list of the count() invocation.
This commit is contained in:
@@ -116,11 +116,28 @@ public class Exps
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an expression that computes the sum of the supplied expression. This would usually
|
||||
* be used in a FieldOverride and supplied with a ColumnExp.
|
||||
* Creates an expression that computes the absolute value of the supplied expression.
|
||||
*/
|
||||
public static FunctionExp abs (SQLExpression expr)
|
||||
{
|
||||
return new FunctionExp("abs", expr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an expression that counts the number of rows that match the supplied expression.
|
||||
* This would usually be used in a FieldOverride and supplied with a ColumnExp.
|
||||
*/
|
||||
public static FunctionExp count (SQLExpression expr)
|
||||
{
|
||||
return new FunctionExp("count", expr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an expression that counts the number of distinct values that match the supplied
|
||||
* expression. This would usually be used in a FieldOverride and supplied with a ColumnExp.
|
||||
*/
|
||||
public static FunctionExp countDistinct (SQLExpression expr)
|
||||
{
|
||||
return new FunctionExp("count", "distinct", expr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,8 +36,18 @@ public class FunctionExp extends FluentExp
|
||||
* Create a new FunctionExp with the given function and arguments.
|
||||
*/
|
||||
public FunctionExp (String function, SQLExpression... arguments)
|
||||
{
|
||||
this(function, null, arguments);
|
||||
_arguments = arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new FunctionExp with the given function, expression annotation and arguments.
|
||||
*/
|
||||
public FunctionExp (String function, String annotation, SQLExpression... arguments)
|
||||
{
|
||||
_function = function;
|
||||
_annotation = annotation;
|
||||
_arguments = arguments;
|
||||
}
|
||||
|
||||
@@ -60,6 +70,11 @@ public class FunctionExp extends FluentExp
|
||||
return _function;
|
||||
}
|
||||
|
||||
public String getAnnotation ()
|
||||
{
|
||||
return _annotation;
|
||||
}
|
||||
|
||||
public SQLExpression[] getArguments ()
|
||||
{
|
||||
return _arguments;
|
||||
@@ -68,12 +83,16 @@ public class FunctionExp extends FluentExp
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
return _function + "(" + StringUtil.join(_arguments, ", ") + ")";
|
||||
return _function + "(" + (_annotation == null ? "" : (_annotation + " ")) +
|
||||
StringUtil.join(_arguments, ", ") + ")";
|
||||
}
|
||||
|
||||
/** The literal name of this function, e.g. FLOOR */
|
||||
protected String _function;
|
||||
|
||||
/** An annotation that goes on the function's expression (only used for count(distinct X)) */
|
||||
protected String _annotation;
|
||||
|
||||
/** The arguments to this function */
|
||||
protected SQLExpression[] _arguments;
|
||||
}
|
||||
|
||||
@@ -146,6 +146,9 @@ public abstract class BuildVisitor implements ExpressionVisitor<Void>
|
||||
{
|
||||
_builder.append(functionExp.getFunction());
|
||||
_builder.append("(");
|
||||
if (functionExp.getAnnotation() != null) {
|
||||
_builder.append(functionExp.getAnnotation()).append(" ");
|
||||
}
|
||||
SQLExpression[] arguments = functionExp.getArguments();
|
||||
for (int ii = 0; ii < arguments.length; ii ++) {
|
||||
if (ii > 0) {
|
||||
|
||||
Reference in New Issue
Block a user