Add support for several count function variants.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@949599 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adam Heath
2010-05-30 22:06:11 +00:00
parent 6723df5c71
commit 36bed586b9
6 changed files with 135 additions and 1 deletions
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.ofbiz.sql;
public final class CountAllFunction extends StaticValue {
public void accept(Visitor visitor) {
visitor.visit(this);
}
public String getDefaultName() {
return "COUNT";
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append("COUNT(*)");
return sb;
}
}
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.ofbiz.sql;
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 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;
}
}
+24
View File
@@ -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, FieldDef> fieldDefs, List<FieldAll> fieldAlls,
| ( <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;
@@ -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);
@@ -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
@@ -128,6 +128,24 @@ public class SQLTest extends GenericTestCaseBase {
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());
}