svn commit: r949599 - in /ofbiz/trunk/framework/sql/src/org/ofbiz/sql: CountAllFunction.java CountFunction.java Parser.jj Value.java test/GoodParseAll.sql test/SQLTest.java

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

svn commit: r949599 - in /ofbiz/trunk/framework/sql/src/org/ofbiz/sql: CountAllFunction.java CountFunction.java Parser.jj Value.java test/GoodParseAll.sql test/SQLTest.java

doogie-3
Author: doogie
Date: Sun May 30 22:06:11 2010
New Revision: 949599

URL: http://svn.apache.org/viewvc?rev=949599&view=rev
Log:
Add support for several count function variants.

Added:
    ofbiz/trunk/framework/sql/src/org/ofbiz/sql/CountAllFunction.java
      - copied, changed from r949465, ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Value.java
    ofbiz/trunk/framework/sql/src/org/ofbiz/sql/CountFunction.java
      - copied, changed from r949465, ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Value.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/GoodParseAll.sql
    ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java

Copied: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/CountAllFunction.java (from r949465, ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Value.java)
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/CountAllFunction.java?p2=ofbiz/trunk/framework/sql/src/org/ofbiz/sql/CountAllFunction.java&p1=ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Value.java&r1=949465&r2=949599&rev=949599&view=diff
==============================================================================
--- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Value.java (original)
+++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/CountAllFunction.java Sun May 30 22:06:11 2010
@@ -18,30 +18,17 @@
  */
 package org.ofbiz.sql;
 
-public abstract class Value extends Atom {
-    public static final class Null extends Value {
-        private Null() {
-        }
-        public void accept(Visitor visitor) {
-            visitor.visit(this);
-        }
-
-        public StringBuilder appendTo(StringBuilder sb) {
-            return sb.append("NULL");
-        }
+public final class CountAllFunction extends StaticValue {
+    public void accept(Visitor visitor) {
+        visitor.visit(this);
     }
 
-    public static final Null NULL = new Null();
-
-    public interface Visitor {
-        void visit(FieldValue value);
-        void visit(FunctionCall value);
-        void visit(MathValue value);
-        void visit(Null value);
-        void visit(NumberValue value);
-        void visit(ParameterValue value);
-        void visit(StringValue value);
+    public String getDefaultName() {
+        return "COUNT";
     }
 
-    public abstract void accept(Visitor visitor);
+    public StringBuilder appendTo(StringBuilder sb) {
+        sb.append("COUNT(*)");
+        return sb;
+    }
 }

Copied: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/CountFunction.java (from r949465, ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Value.java)
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/CountFunction.java?p2=ofbiz/trunk/framework/sql/src/org/ofbiz/sql/CountFunction.java&p1=ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Value.java&r1=949465&r2=949599&rev=949599&view=diff
==============================================================================
--- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Value.java (original)
+++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/CountFunction.java Sun May 30 22:06:11 2010
@@ -18,30 +18,36 @@
  */
 package org.ofbiz.sql;
 
-public abstract class Value extends Atom {
-    public static final class Null extends Value {
-        private Null() {
-        }
-        public void accept(Visitor visitor) {
-            visitor.visit(this);
-        }
-
-        public StringBuilder appendTo(StringBuilder sb) {
-            return sb.append("NULL");
-        }
-    }
-
-    public static final Null NULL = new Null();
-
-    public interface Visitor {
-        void visit(FieldValue value);
-        void visit(FunctionCall value);
-        void visit(MathValue value);
-        void visit(Null value);
-        void visit(NumberValue value);
-        void visit(ParameterValue value);
-        void visit(StringValue value);
+public final class CountFunction extends StaticValue {
+    private final boolean isDistinct;
+    private final FieldValue field;
+
+    public CountFunction(boolean isDistinct, FieldValue field) {
+        this.isDistinct = isDistinct;
+        this.field = field;
+    }
+
+    public void accept(Visitor visitor) {
+        visitor.visit(this);
+    }
+
+    public String getDefaultName() {
+        return "COUNT";
     }
 
-    public abstract void accept(Visitor visitor);
+    public boolean isDistinct() {
+        return isDistinct;
+    }
+
+    public FieldValue getField() {
+        return field;
+    }
+
+    public StringBuilder appendTo(StringBuilder sb) {
+        sb.append("COUNT(");
+        if (isDistinct) sb.append("DISTINCT ");
+        field.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=949599&r1=949598&r2=949599&view=diff
==============================================================================
--- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj (original)
+++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj Sun May 30 22:06:11 2010
@@ -93,6 +93,8 @@ TOKEN: {
 |    <JOIN: "JOIN">
 |    <LEFT: "LEFT">
 |    <AS: "AS">
+|    <COUNT: "COUNT">
+|    <DISTINCT: "DISTINCT">
 |    <WHERE: "WHERE">
 |    <HAVING: "HAVING">
 |    <GROUP: "GROUP">
@@ -557,12 +559,34 @@ private void FieldDef(Map<String, FieldD
         |    ( <AS> fieldAlias=NamePart() )? { def = new FieldDefFieldValue(new FieldValue(null, n), fieldAlias); }
         )
     |    v=MathValue() ( <AS> fieldAlias=NamePart() )? { def = new FieldDefValue(v, fieldAlias); }
+    |   v=Count() ( <AS> fieldAlias=NamePart() )? { def = new FieldDefValue(v, fieldAlias); }
     ) {
         if (fieldDefs.containsKey(def.getAlias())) throw new ParseException("duplicate alias(" + def.getAlias() + ")");
         fieldDefs.put(def.getAlias(), def);
     }
 }
 
+private StaticValue Count():
+{
+    String n, fieldName;
+}
+{
+    <COUNT> <OPEN_PAREN>
+    (
+        n=NamePart() (
+            <PERIOD> (
+                <STAR> <CLOSE_PAREN> { return new CountAllFunction(); }
+            | fieldName=NamePart() <CLOSE_PAREN> { return new CountFunction(false, new FieldValue(n, fieldName)); }
+            )
+        | <CLOSE_PAREN> { return new CountFunction(false, new FieldValue(null, n)); }
+        )
+    |   <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)); }
+        )
+    )
+}
+
 private StaticValue MathValue():
 {
     ConstantValue v;

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=949599&r1=949598&r2=949599&view=diff
==============================================================================
--- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Value.java (original)
+++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Value.java Sun May 30 22:06:11 2010
@@ -41,6 +41,8 @@ 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 abstract void accept(Visitor visitor);

Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/GoodParseAll.sql
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/GoodParseAll.sql?rev=949599&r1=949598&r2=949599&view=diff
==============================================================================
--- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/GoodParseAll.sql (original)
+++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/GoodParseAll.sql Sun May 30 22:06:11 2010
@@ -25,7 +25,10 @@ SELECT
     d.description AS roleDescription,
     SUM(a.partyId),
     FOO(a.partyId, 1) AS baz,
-    (a.partyId || '-' || a.partyTypeId) AS one
+    (a.partyId || '-' || a.partyTypeId) AS one,
+    COUNT(a.partyId) AS cnt1,
+    COUNT(partyId) AS cnt2,
+    COUNT(DISTINCT a.partyId) AS cnt3
 FROM
     Party a LEFT JOIN Person b USING partyId
     LEFT JOIN PartyGroup c ON b.partyId = c.partyId

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=949599&r1=949598&r2=949599&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 Sun May 30 22:06:11 2010
@@ -128,6 +128,24 @@ public class SQLTest extends GenericTest
             assertTrue("fifth is FieldDefValue", fieldDef instanceof FieldDefValue);
             fdv = (FieldDefValue) fieldDef;
 
+            assertTrue("has sixth field def", fieldDefIt.hasNext());
+            fieldDef = fieldDefIt.next();
+            assertEquals("sixth fieldDef.alias", "cnt1", fieldDef.getAlias());
+            assertTrue("sixth is FieldDefValue", fieldDef instanceof FieldDefValue);
+            fdv = (FieldDefValue) fieldDef;
+
+            assertTrue("has seventh field def", fieldDefIt.hasNext());
+            fieldDef = fieldDefIt.next();
+            assertEquals("seventh fieldDef.alias", "cnt2", fieldDef.getAlias());
+            assertTrue("seventh is FieldDefValue", fieldDef instanceof FieldDefValue);
+            fdv = (FieldDefValue) fieldDef;
+
+            assertTrue("has eighth field def", fieldDefIt.hasNext());
+            fieldDef = fieldDefIt.next();
+            assertEquals("eighth fieldDef.alias", "cnt3", fieldDef.getAlias());
+            assertTrue("eighth is FieldDefValue", fieldDef instanceof FieldDefValue);
+            fdv = (FieldDefValue) fieldDef;
+
             assertFalse("has no more field def", fieldDefIt.hasNext());
 
         }