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:
Michael Bayne
2009-08-07 01:58:58 +00:00
parent a52abcc7b2
commit 37295dbcb3
3 changed files with 42 additions and 3 deletions
+19 -2
View File
@@ -116,11 +116,28 @@ public class Exps
} }
/** /**
* Creates an expression that computes the sum of the supplied expression. This would usually * Creates an expression that computes the absolute value of the supplied expression.
* be used in a FieldOverride and supplied with a ColumnExp.
*/ */
public static FunctionExp abs (SQLExpression expr) public static FunctionExp abs (SQLExpression expr)
{ {
return new FunctionExp("abs", 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. * Create a new FunctionExp with the given function and arguments.
*/ */
public FunctionExp (String function, SQLExpression... 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; _function = function;
_annotation = annotation;
_arguments = arguments; _arguments = arguments;
} }
@@ -60,6 +70,11 @@ public class FunctionExp extends FluentExp
return _function; return _function;
} }
public String getAnnotation ()
{
return _annotation;
}
public SQLExpression[] getArguments () public SQLExpression[] getArguments ()
{ {
return _arguments; return _arguments;
@@ -68,12 +83,16 @@ public class FunctionExp extends FluentExp
@Override @Override
public String toString () 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 */ /** The literal name of this function, e.g. FLOOR */
protected String _function; 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 */ /** The arguments to this function */
protected SQLExpression[] _arguments; protected SQLExpression[] _arguments;
} }
@@ -146,6 +146,9 @@ public abstract class BuildVisitor implements ExpressionVisitor<Void>
{ {
_builder.append(functionExp.getFunction()); _builder.append(functionExp.getFunction());
_builder.append("("); _builder.append("(");
if (functionExp.getAnnotation() != null) {
_builder.append(functionExp.getAnnotation()).append(" ");
}
SQLExpression[] arguments = functionExp.getArguments(); SQLExpression[] arguments = functionExp.getArguments();
for (int ii = 0; ii < arguments.length; ii ++) { for (int ii = 0; ii < arguments.length; ii ++) {
if (ii > 0) { if (ii > 0) {