svn commit: r981639 - in /ofbiz/trunk/framework/sql/src/org/ofbiz/sql: AggregateFunction.java CountFunction.java Parser.jj Value.java test/SQLTest.java test/SelectTest.java test/ValuesTest.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r981639 - in /ofbiz/trunk/framework/sql/src/org/ofbiz/sql: AggregateFunction.java CountFunction.java Parser.jj Value.java test/SQLTest.java test/SelectTest.java test/ValuesTest.java

doogie-3
Author: doogie
Date: Mon Aug  2 17:44:12 2010
New Revision: 981639

URL: http://svn.apache.org/viewvc?rev=981639&view=rev
Log:
FEATURE: Start of aggregate function support.

Added:
    ofbiz/trunk/framework/sql/src/org/ofbiz/sql/AggregateFunction.java
      - copied, changed from r981638, ofbiz/trunk/framework/sql/src/org/ofbiz/sql/CountFunction.java
Removed:
    ofbiz/trunk/framework/sql/src/org/ofbiz/sql/CountFunction.java
Modified:
    ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj
    ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Value.java
    ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java
    ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SelectTest.java
    ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/ValuesTest.java

Copied: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/AggregateFunction.java (from r981638, ofbiz/trunk/framework/sql/src/org/ofbiz/sql/CountFunction.java)
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/AggregateFunction.java?p2=ofbiz/trunk/framework/sql/src/org/ofbiz/sql/AggregateFunction.java&p1=ofbiz/trunk/framework/sql/src/org/ofbiz/sql/CountFunction.java&r1=981638&r2=981639&rev=981639&view=diff
==============================================================================
--- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/CountFunction.java (original)
+++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/AggregateFunction.java Mon Aug  2 17:44:12 2010
@@ -18,47 +18,61 @@
  */
 package org.ofbiz.sql;
 
+import java.util.Iterator;
+import java.util.List;
+
 import org.ofbiz.base.lang.SourceMonitored;
+import org.ofbiz.base.util.StringUtil;
 
 @SourceMonitored
-public final class CountFunction extends StaticValue {
+public final class AggregateFunction extends StaticValue {
+    public static final String module = AggregateFunction.class.getName();
+
+    private final String name;
     private final boolean isDistinct;
-    private final FieldValue field;
+    private final StaticValue value;
 
-    public CountFunction(boolean isDistinct, FieldValue field) {
+    public AggregateFunction(String name, boolean isDistinct, StaticValue value) {
+        this.name = name;
         this.isDistinct = isDistinct;
-        this.field = field;
+        this.value = value;
     }
 
     public void accept(Visitor visitor) {
         visitor.visit(this);
     }
 
-    public String getDefaultName() {
-        return "COUNT";
+    public String getName() {
+        return name;
     }
 
     public boolean isDistinct() {
         return isDistinct;
     }
 
-    public FieldValue getField() {
-        return field;
+    public String getDefaultName() {
+        return name;
+    }
+
+    public StaticValue getValue() {
+        return value;
     }
 
     public boolean equals(Object o) {
-        if (o instanceof CountFunction) {
-            CountFunction other = (CountFunction) o;
-            return isDistinct == other.isDistinct && field.equals(other.field);
+        if (o instanceof AggregateFunction) {
+            AggregateFunction other = (AggregateFunction) o;
+            return name.equals(other.name) && isDistinct == other.isDistinct && value.equals(other.value);
         } else {
             return false;
         }
     }
 
     public StringBuilder appendTo(StringBuilder sb) {
-        sb.append("COUNT(");
-        if (isDistinct) sb.append("DISTINCT ");
-        field.appendTo(sb);
+        sb.append(name).append('(');
+        if (isDistinct) {
+            sb.append("DISTINCT ");
+        }
+        value.appendTo(sb);
         sb.append(')');
         return sb;
     }

Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj?rev=981639&r1=981638&r2=981639&view=diff
==============================================================================
--- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj (original)
+++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj Mon Aug  2 17:44:12 2010
@@ -40,6 +40,7 @@ PARSER_BEGIN(Parser)
 
 package org.ofbiz.sql;
 
+import java.util.EnumSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -94,6 +95,10 @@ TOKEN: {
     | <LEFT: "LEFT">
     | <AS: "AS">
     | <COUNT: "COUNT">
+    | <MAX: "MAX">
+    | <MIN: "MIN">
+    | <SUM: "SUM">
+    | <AVG: "AVG">
     | <DISTINCT: "DISTINCT">
     | <WHERE: "WHERE">
     | <HAVING: "HAVING">
@@ -583,7 +588,8 @@ private StaticValue StaticValue(): {
             | { v = new FieldValue(null, n); }
           )
         | v=MathValue()
-        | v=Count()
+        | LOOKAHEAD(5) v=CountAll()
+        | v=AggregateFunction()
     ) { return v; }
 }
 
@@ -596,23 +602,14 @@ private FieldDef FieldDef(): {
     }
 }
 
-private StaticValue Count(): {
-    String n, fieldName;
+private CountAllFunction CountAll(): {
+    String n;
 } {
     <COUNT> <OPEN_PAREN> (
-          n=NamePart() (
-              <PERIOD> (
-                  <STAR> <CLOSE_PAREN> { return new CountAllFunction(n); }
-                | fieldName=NamePart() <CLOSE_PAREN> { return new CountFunction(false, new FieldValue(n, fieldName)); }
-              )
-            | <CLOSE_PAREN> { return new CountFunction(false, new FieldValue(null, n)); }
-          )
-        | <STAR> <CLOSE_PAREN> { return new CountAllFunction(null); }
-        | <DISTINCT> n=NamePart() (
-              <PERIOD> fieldName=NamePart() <CLOSE_PAREN> { return new CountFunction(true, new FieldValue(n, fieldName)); }
-            | <CLOSE_PAREN> { return new CountFunction(true, new FieldValue(null, n)); }
-        )
-    )
+          n=NamePart()
+        | { n = null; }
+    ) <PERIOD> <STAR> <CLOSE_PAREN>
+    { return new CountAllFunction(n); }
 }
 
 private StaticValue MathValue(): {
@@ -636,6 +633,23 @@ private StaticValue MathValue(): {
     }
 }
 
+private AggregateFunction AggregateFunction(): {
+    StaticValue v;
+    String name, n, fieldName;
+    boolean isDistinct = false;
+} {
+    name=AggregateName() <OPEN_PAREN>
+    ( <DISTINCT> { isDistinct = true; } )? (
+          n=NamePart() (
+              <PERIOD> fieldName=NamePart() { v = new FieldValue(n, fieldName); }
+            | v=FunctionCallRest(n)
+            | { v = new FieldValue(null, n); }
+          )
+        | v=MathValue()
+    ) <CLOSE_PAREN>
+    { return new AggregateFunction(name, isDistinct, v); }
+}
+
 private FunctionCall FunctionCallRest(String name): {
     Value arg;
     List<Value> args = FastList.newInstance();
@@ -852,3 +866,14 @@ private String MathOperator(): {
       s=Text() { return s; }
     | <NAME> { return getToken(0).image; }
 }
+
+private String AggregateName(): {
+} {
+      <COUNT> { return getToken(0).image; }
+    | <MAX> { return getToken(0).image; }
+    | <MIN> { return getToken(0).image; }
+    | <SUM> { return getToken(0).image; }
+    | <AVG> { return getToken(0).image; }
+    | <FIRST> { return getToken(0).image; }
+    | <LAST> { return getToken(0).image; }
+}

Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Value.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Value.java?rev=981639&r1=981638&r2=981639&view=diff
==============================================================================
--- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Value.java (original)
+++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Value.java Mon Aug  2 17:44:12 2010
@@ -34,6 +34,7 @@ public abstract class Value extends Atom
     public static final Null NULL = new Null();
 
     public interface Visitor {
+        void visit(AggregateFunction value);
         void visit(FieldValue value);
         void visit(FunctionCall value);
         void visit(MathValue value);
@@ -41,11 +42,13 @@ public abstract class Value extends Atom
         void visit(NumberValue value);
         void visit(ParameterValue value);
         void visit(StringValue value);
-        void visit(CountFunction value);
         void visit(CountAllFunction value);
     }
 
     public static class BaseVisitor implements Visitor {
+        public void visit(AggregateFunction value) {
+        }
+
         public void visit(FieldValue value) {
         }
 
@@ -67,9 +70,6 @@ public abstract class Value extends Atom
         public void visit(StringValue value) {
         }
 
-        public void visit(CountFunction value) {
-        }
-
         public void visit(CountAllFunction value) {
         }
     }

Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java?rev=981639&r1=981638&r2=981639&view=diff
==============================================================================
--- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java (original)
+++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java Mon Aug  2 17:44:12 2010
@@ -25,12 +25,12 @@ import java.util.Iterator;
 import java.util.List;
 
 import org.ofbiz.base.test.GenericTestCaseBase;
+import org.ofbiz.sql.AggregateFunction;
 import org.ofbiz.sql.BetweenCondition;
 import org.ofbiz.sql.BooleanCondition;
 import org.ofbiz.sql.Condition;
 import org.ofbiz.sql.ConditionList;
 import org.ofbiz.sql.ConstantValue;
-import org.ofbiz.sql.CountFunction;
 import org.ofbiz.sql.FieldAll;
 import org.ofbiz.sql.FieldDef;
 import org.ofbiz.sql.FieldValue;
@@ -88,12 +88,12 @@ public class SQLTest extends GenericTest
                 GenericTestCaseBase.<String, FieldDef>map(
                     "roleTypeId", new FieldDef(new FieldValue("d", "roleTypeId"), null),
                     "roleDescription", new FieldDef(new FieldValue("d", "description"), "roleDescription"),
-                    "SUM",  new FieldDef(new FunctionCall("SUM", GenericTestCaseBase.<Value>list(new FieldValue("a", "partyId"))), null),
+                    "SUM",  new FieldDef(new AggregateFunction("SUM", false, new FieldValue("a", "partyId")), null),
                     "baz",  new FieldDef(new FunctionCall("FOO", GenericTestCaseBase.<Value>list(new FieldValue("a", "partyId"), new NumberValue<Integer>(Integer.valueOf(1)))), "baz"),
                     "one",  new FieldDef(new MathValue("||", list(new FieldValue("a", "partyId"), new StringValue("-"), new FieldValue("a", "partyTypeId"))), "one"),
-                    "cnt1", new FieldDef(new CountFunction(false, new FieldValue("a", "partyId")), "cnt1"),
-                    "cnt2", new FieldDef(new CountFunction(false, new FieldValue(null, "partyId")), "cnt2"),
-                    "cnt3", new FieldDef(new CountFunction(true, new FieldValue("a", "partyId")), "cnt3")
+                    "cnt1", new FieldDef(new AggregateFunction("COUNT", false, new FieldValue("a", "partyId")), "cnt1"),
+                    "cnt2", new FieldDef(new AggregateFunction("COUNT", false, new FieldValue(null, "partyId")), "cnt2"),
+                    "cnt3", new FieldDef(new AggregateFunction("COUNT", true, new FieldValue("a", "partyId")), "cnt3")
                 ),
                 new Table(
                     new TableName("Party", "a"),

Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SelectTest.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SelectTest.java?rev=981639&r1=981638&r2=981639&view=diff
==============================================================================
--- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SelectTest.java (original)
+++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SelectTest.java Mon Aug  2 17:44:12 2010
@@ -28,7 +28,6 @@ import junit.framework.TestCase;
 
 import org.ofbiz.sql.ConstantValue;
 import org.ofbiz.sql.CountAllFunction;
-import org.ofbiz.sql.CountFunction;
 import org.ofbiz.sql.FieldValue;
 import org.ofbiz.sql.FunctionCall;
 import org.ofbiz.sql.MathValue;

Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/ValuesTest.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/ValuesTest.java?rev=981639&r1=981638&r2=981639&view=diff
==============================================================================
--- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/ValuesTest.java (original)
+++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/ValuesTest.java Mon Aug  2 17:44:12 2010
@@ -25,7 +25,7 @@ import junit.framework.TestCase;
 
 import org.ofbiz.sql.ConstantValue;
 import org.ofbiz.sql.CountAllFunction;
-import org.ofbiz.sql.CountFunction;
+import org.ofbiz.sql.AggregateFunction;
 import org.ofbiz.sql.FieldValue;
 import org.ofbiz.sql.FunctionCall;
 import org.ofbiz.sql.MathValue;
@@ -86,23 +86,26 @@ public class ValuesTest extends GenericT
         countAllFunctionTest("v3", v3, "a", "COUNT(a.*)", v1, true);
     }
 
-    private static void countFunctionTest(String label, CountFunction v, boolean isDistinct, FieldValue fv, String s, CountFunction o, boolean matches) {
+    private static void aggregateFunctionTest(String label, AggregateFunction v, String name, boolean isDistinct, FieldValue fv, String s, AggregateFunction o, boolean matches) {
+        assertEquals(label + ":name", name, v.getName());
         assertEquals(label + ":left", isDistinct, v.isDistinct());
-        assertEquals(label + ":field-value", fv, v.getField());
-        basicTest(label, CountFunction.class, v, "COUNT", s, o, matches);
+        assertEquals(label + ":field-value", fv, v.getValue());
+        basicTest(label, AggregateFunction.class, v, name, s, o, matches);
     }
 
-    public void testCountFunction() {
-        CountFunction v1 = new CountFunction(false, fv2);
-        countFunctionTest("v1", v1, false, fv2, "COUNT(a.partyId)", null, false);
-        CountFunction v2 = new CountFunction(true, fv2);
-        countFunctionTest("v2", v2, true, fv2, "COUNT(DISTINCT a.partyId)", v1, false);
-        CountFunction v3 = new CountFunction(true, fv1);
-        countFunctionTest("v3", v3, true, fv1, "COUNT(DISTINCT partyId)", v1, false);
-        CountFunction v4 = new CountFunction(false, fv1);
-        countFunctionTest("v4", v4, false, fv1, "COUNT(partyId)", v1, false);
-        CountFunction v5 = new CountFunction(false, fv2);
-        countFunctionTest("v5", v5, false, fv2, "COUNT(a.partyId)", v1, true);
+    public void testAggregateFunction() {
+        AggregateFunction v1 = new AggregateFunction("COUNT", false, fv2);
+        aggregateFunctionTest("v1", v1, "COUNT", false, fv2, "COUNT(a.partyId)", null, false);
+        AggregateFunction v2 = new AggregateFunction("COUNT", true, fv2);
+        aggregateFunctionTest("v2", v2, "COUNT", true, fv2, "COUNT(DISTINCT a.partyId)", v1, false);
+        AggregateFunction v3 = new AggregateFunction("COUNT", true, fv1);
+        aggregateFunctionTest("v3", v3, "COUNT", true, fv1, "COUNT(DISTINCT partyId)", v1, false);
+        AggregateFunction v4 = new AggregateFunction("COUNT", false, fv1);
+        aggregateFunctionTest("v4", v4, "COUNT", false, fv1, "COUNT(partyId)", v1, false);
+        AggregateFunction v5 = new AggregateFunction("MAX", false, fv2);
+        aggregateFunctionTest("v5", v5, "MAX", false, fv2, "MAX(a.partyId)", v1, false);
+        AggregateFunction v6 = new AggregateFunction("COUNT", false, fv2);
+        aggregateFunctionTest("v6", v6, "COUNT", false, fv2, "COUNT(a.partyId)", v1, true);
     }
 
     private static void fieldValueTest(String label, FieldValue v, String tableName, String fieldName, String s, FieldValue o, boolean matches) {
@@ -198,6 +201,11 @@ public class ValuesTest extends GenericT
     }
 
     public static class ValueVisitorRecorder extends Recorder<Class<? extends Value>> implements Value.Visitor {
+
+        public void visit(AggregateFunction value) {
+            record(AggregateFunction.class);
+        }
+
         public void visit(FieldValue value) {
             record(FieldValue.class);
         }
@@ -226,10 +234,6 @@ public class ValuesTest extends GenericT
             record(StringValue.class);
         }
 
-        public void visit(CountFunction value) {
-            record(CountFunction.class);
-        }
-
         public void visit(CountAllFunction value) {
             record(CountAllFunction.class);
         }