001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
005: *
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common Development
009: * and Distribution License("CDDL") (collectively, the "License"). You
010: * may not use this file except in compliance with the License. You can obtain
011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
013: * language governing permissions and limitations under the License.
014: *
015: * When distributing the software, include this License Header Notice in each
016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
017: * Sun designates this particular file as subject to the "Classpath" exception
018: * as provided by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the License
020: * Header, with the fields enclosed by brackets [] replaced by your own
021: * identifying information: "Portions Copyrighted [year]
022: * [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * If you wish your version of this file to be governed by only the CDDL or
027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
028: * elects to include this software in this distribution under the [CDDL or GPL
029: * Version 2] license." If you don't indicate a single choice of license, a
030: * recipient has the option to distribute your version of this file under
031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
032: * its licensees as provided above. However, if you add GPL Version 2 code
033: * and therefore, elected the GPL Version 2 license, then the option applies
034: * only if the new code is made subject to such option by the copyright
035: * holder.
036: */
037: package oracle.toplink.essentials.internal.parsing;
038:
039: // Java imports
040: import java.util.*;
041:
042: // TopLink Imports
043: import oracle.toplink.essentials.exceptions.EJBQLException;
044: import oracle.toplink.essentials.queryframework.ReportQuery;
045: import oracle.toplink.essentials.queryframework.ObjectLevelReadQuery;
046:
047: /**
048: * INTERNAL
049: * <p><b>Purpose</b>: Represent an GROUP BY
050: * <p><b>Responsibilities</b>:<ul>
051: * <li> Generate the correct expression for an GROUP BY
052: * </ul>
053: */
054: public class GroupByNode extends MajorNode {
055:
056: List groupByItems = null;
057:
058: /**
059: * Return a new GroupByNode.
060: */
061: public GroupByNode() {
062: super ();
063: }
064:
065: /**
066: * INTERNAL
067: * Validate the current node.
068: */
069: public void validate(ParseTreeContext context, SelectNode selectNode) {
070: for (Iterator i = groupByItems.iterator(); i.hasNext();) {
071: Node item = (Node) i.next();
072: item.validate(context);
073: }
074:
075: List selectExprs = selectNode.getSelectExpressions();
076: // check select expressions
077: for (Iterator i = selectExprs.iterator(); i.hasNext();) {
078: Node selectExpr = (Node) i.next();
079: if (!isValidSelectExpr(selectExpr)) {
080: throw EJBQLException.invalidSelectForGroupByQuery(
081: context.getQueryInfo(), selectExpr.getLine(),
082: selectExpr.getColumn(), selectExpr
083: .getAsString(), getAsString());
084: }
085: }
086: }
087:
088: /**
089: * INTERNAL
090: * Add an Group By Item to this node
091: */
092: private void addGroupByItem(Object theNode) {
093: getGroupByItems().add(theNode);
094: }
095:
096: /**
097: * INTERNAL
098: * Add the grouping expressions to the passed query
099: */
100: public void addGroupingToQuery(ObjectLevelReadQuery theQuery,
101: GenerationContext context) {
102: if (theQuery.isReportQuery()) {
103: Iterator iter = getGroupByItems().iterator();
104: while (iter.hasNext()) {
105: Node nextNode = (Node) iter.next();
106: ((ReportQuery) theQuery).addGrouping(nextNode
107: .generateExpression(context));
108: }
109: }
110: }
111:
112: /**
113: * INTERNAL
114: * Returns true if the sp
115: */
116: public boolean isValidHavingExpr(Node expr) {
117: if (expr.isDotNode() || expr.isVariableNode()) {
118: return isGroupbyItem(expr);
119: } else {
120: // delegate to child node if any
121: Node left = expr.getLeft();
122: Node right = expr.getRight();
123: return ((left == null) || isValidHavingExpr(left))
124: && ((right == null) || isValidHavingExpr(right));
125: }
126: }
127:
128: /**
129: * INTERNAL
130: * Returns true if the specified expr is a valid SELECT clause expression.
131: */
132: private boolean isValidSelectExpr(Node expr) {
133: if (expr.isAggregateNode()) {
134: return true;
135: } else if (expr.isConstructorNode()) {
136: List args = ((ConstructorNode) expr).getConstructorItems();
137: for (Iterator i = args.iterator(); i.hasNext();) {
138: Node arg = (Node) i.next();
139: if (!isValidSelectExpr(arg)) {
140: return false;
141: }
142: }
143: return true;
144: }
145: return isGroupbyItem(expr);
146: }
147:
148: /**
149: * INTERNAL
150: * Return true if the specified expr is a groupby item.
151: */
152: private boolean isGroupbyItem(Node expr) {
153: if (expr.isDotNode() || expr.isVariableNode()) {
154: String exprRepr = expr.getAsString();
155: for (Iterator i = groupByItems.iterator(); i.hasNext();) {
156: Node item = (Node) i.next();
157: String itemRepr = item.getAsString();
158: if (exprRepr.equals(itemRepr)) {
159: return true;
160: }
161: }
162: }
163: return false;
164: }
165:
166: /**
167: * INTERNAL
168: * Return the GROUP BY statements
169: */
170: public List getGroupByItems() {
171: if (groupByItems == null) {
172: setGroupByItems(new Vector());
173: }
174: return groupByItems;
175: }
176:
177: /**
178: * INTERNAL
179: * Set the GROUP BY statements
180: */
181: public void setGroupByItems(List newItems) {
182: groupByItems = newItems;
183: }
184:
185: /**
186: * INTERNAL
187: * Get the string representation of this node.
188: */
189: public String getAsString() {
190: StringBuffer repr = new StringBuffer();
191: for (Iterator i = groupByItems.iterator(); i.hasNext();) {
192: Node expr = (Node) i.next();
193: if (repr.length() > 0) {
194: repr.append(", ");
195: }
196: repr.append(expr.getAsString());
197: }
198: return "GROUP BY " + repr.toString();
199: }
200:
201: }
|