From 37295dbcb3c58ac6d694b2ae661e52d8cee70165 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 7 Aug 2009 01:58:58 +0000 Subject: [PATCH] Added Exps.count() and Exps.countDistinct() and the necessary plumbing to sneak a 'distinct' into the argument list of the count() invocation. --- src/java/com/samskivert/depot/Exps.java | 21 +++++++++++++++++-- .../depot/expression/FunctionExp.java | 21 ++++++++++++++++++- .../samskivert/depot/impl/BuildVisitor.java | 3 +++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/java/com/samskivert/depot/Exps.java b/src/java/com/samskivert/depot/Exps.java index 05718ee..6db1fad 100644 --- a/src/java/com/samskivert/depot/Exps.java +++ b/src/java/com/samskivert/depot/Exps.java @@ -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); + } } diff --git a/src/java/com/samskivert/depot/expression/FunctionExp.java b/src/java/com/samskivert/depot/expression/FunctionExp.java index 81886d4..a86c6ba 100644 --- a/src/java/com/samskivert/depot/expression/FunctionExp.java +++ b/src/java/com/samskivert/depot/expression/FunctionExp.java @@ -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; } diff --git a/src/java/com/samskivert/depot/impl/BuildVisitor.java b/src/java/com/samskivert/depot/impl/BuildVisitor.java index 757a2be..1c1a6b5 100644 --- a/src/java/com/samskivert/depot/impl/BuildVisitor.java +++ b/src/java/com/samskivert/depot/impl/BuildVisitor.java @@ -146,6 +146,9 @@ public abstract class BuildVisitor implements ExpressionVisitor { _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) {