From 36bed586b97cee32918d9c9866cdabcc7b1da986 Mon Sep 17 00:00:00 2001 From: Adam Heath Date: Sun, 30 May 2010 22:06:11 +0000 Subject: [PATCH] Add support for several count function variants. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@949599 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/org/ofbiz/sql/CountAllFunction.java | 34 ++++++++++++ .../sql/src/org/ofbiz/sql/CountFunction.java | 53 +++++++++++++++++++ framework/sql/src/org/ofbiz/sql/Parser.jj | 24 +++++++++ framework/sql/src/org/ofbiz/sql/Value.java | 2 + .../src/org/ofbiz/sql/test/GoodParseAll.sql | 5 +- .../sql/src/org/ofbiz/sql/test/SQLTest.java | 18 +++++++ 6 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 framework/sql/src/org/ofbiz/sql/CountAllFunction.java create mode 100644 framework/sql/src/org/ofbiz/sql/CountFunction.java diff --git a/framework/sql/src/org/ofbiz/sql/CountAllFunction.java b/framework/sql/src/org/ofbiz/sql/CountAllFunction.java new file mode 100644 index 0000000000..327d2845de --- /dev/null +++ b/framework/sql/src/org/ofbiz/sql/CountAllFunction.java @@ -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; + } +} diff --git a/framework/sql/src/org/ofbiz/sql/CountFunction.java b/framework/sql/src/org/ofbiz/sql/CountFunction.java new file mode 100644 index 0000000000..ed53222f6a --- /dev/null +++ b/framework/sql/src/org/ofbiz/sql/CountFunction.java @@ -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; + } +} diff --git a/framework/sql/src/org/ofbiz/sql/Parser.jj b/framework/sql/src/org/ofbiz/sql/Parser.jj index 1b08a8d820..b8e1fb9a1d 100644 --- a/framework/sql/src/org/ofbiz/sql/Parser.jj +++ b/framework/sql/src/org/ofbiz/sql/Parser.jj @@ -93,6 +93,8 @@ TOKEN: { | | | +| +| | | | @@ -557,12 +559,34 @@ private void FieldDef(Map fieldDefs, List fieldAlls, | ( fieldAlias=NamePart() )? { def = new FieldDefFieldValue(new FieldValue(null, n), fieldAlias); } ) | v=MathValue() ( fieldAlias=NamePart() )? { def = new FieldDefValue(v, fieldAlias); } + | v=Count() ( 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; +} +{ + + ( + n=NamePart() ( + ( + { return new CountAllFunction(); } + | fieldName=NamePart() { return new CountFunction(false, new FieldValue(n, fieldName)); } + ) + | { return new CountFunction(false, new FieldValue(null, n)); } + ) + | n=NamePart() ( + fieldName=NamePart() { return new CountFunction(true, new FieldValue(n, fieldName)); } + | { return new CountFunction(true, new FieldValue(null, n)); } + ) + ) +} + private StaticValue MathValue(): { ConstantValue v; diff --git a/framework/sql/src/org/ofbiz/sql/Value.java b/framework/sql/src/org/ofbiz/sql/Value.java index 7c5054cb28..9ea5b3e3c6 100644 --- a/framework/sql/src/org/ofbiz/sql/Value.java +++ b/framework/sql/src/org/ofbiz/sql/Value.java @@ -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); diff --git a/framework/sql/src/org/ofbiz/sql/test/GoodParseAll.sql b/framework/sql/src/org/ofbiz/sql/test/GoodParseAll.sql index 7ba02582c4..2f12490f28 100644 --- a/framework/sql/src/org/ofbiz/sql/test/GoodParseAll.sql +++ b/framework/sql/src/org/ofbiz/sql/test/GoodParseAll.sql @@ -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 diff --git a/framework/sql/src/org/ofbiz/sql/test/SQLTest.java b/framework/sql/src/org/ofbiz/sql/test/SQLTest.java index c8be3b38ea..9d118edd8d 100644 --- a/framework/sql/src/org/ofbiz/sql/test/SQLTest.java +++ b/framework/sql/src/org/ofbiz/sql/test/SQLTest.java @@ -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()); }