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: import java.util.Set;
040:
041: import oracle.toplink.essentials.expressions.*;
042: import oracle.toplink.essentials.queryframework.ReportQuery;
043:
044: /**
045: * INTERNAL
046: * <p><b>Purpose</b>: Represent a subquery.
047: */
048: public class SubqueryNode extends Node {
049:
050: private EJBQLParseTree subqueryParseTree;
051:
052: /** Set of names of variables declared in an outer scope and used in teh
053: * subquery. */
054: private Set outerVars;
055:
056: /**
057: * Return a new SubqueryNode.
058: */
059: public SubqueryNode() {
060: super ();
061: }
062:
063: /** */
064: public ReportQuery getReportQuery(GenerationContext context) {
065: ReportQuery innerQuery = new ReportQuery();
066: GenerationContext innerContext = subqueryParseTree
067: .populateSubquery(innerQuery, context);
068: Expression joins = innerContext.joinVariables(outerVars);
069: if (joins != null) {
070: Expression where = innerQuery.getSelectionCriteria();
071: where = appendExpression(where, joins);
072: innerQuery.setSelectionCriteria(where);
073: }
074: return innerQuery;
075: }
076:
077: /**
078: * INTERNAL
079: * If called the subquery is part of the WHERE clause of an UPDATE or
080: * DELETE statement that does not define an identification variable.
081: * The method checks the clauses of the subquery for unqualified fields
082: * accesses.
083: */
084: public Node qualifyAttributeAccess(ParseTreeContext context) {
085: subqueryParseTree.getFromNode().qualifyAttributeAccess(context);
086: subqueryParseTree.getQueryNode()
087: .qualifyAttributeAccess(context);
088: if (subqueryParseTree.getWhereNode() != null) {
089: subqueryParseTree.getWhereNode().qualifyAttributeAccess(
090: context);
091: }
092: if (subqueryParseTree.getGroupByNode() != null) {
093: subqueryParseTree.getGroupByNode().qualifyAttributeAccess(
094: context);
095: }
096: if (subqueryParseTree.getHavingNode() != null) {
097: subqueryParseTree.getHavingNode().qualifyAttributeAccess(
098: context);
099: }
100: return this ;
101: }
102:
103: /**
104: * INTERNAL
105: * Validate node and calculate its type.
106: */
107: public void validate(ParseTreeContext context) {
108: subqueryParseTree.validate(context);
109: outerVars = context.getOuterScopeVariables();
110: SelectNode selectNode = (SelectNode) subqueryParseTree
111: .getQueryNode();
112: // Get the select expression, subqueries only have one
113: Node selectExpr = (Node) selectNode.getSelectExpressions().get(
114: 0);
115: setType(selectExpr.getType());
116: }
117:
118: /**
119: * INTERNAL
120: * Generate the TopLink expression for this node
121: */
122: public Expression generateExpression(GenerationContext context) {
123: Expression base = context.getBaseExpression();
124: ReportQuery innerQuery = getReportQuery(context);
125: return base.subQuery(innerQuery);
126: }
127:
128: /**
129: * INTERNAL
130: * Is this node a SubqueryNode
131: */
132: public boolean isSubqueryNode() {
133: return true;
134: }
135:
136: /** */
137: public void setParseTree(EJBQLParseTree parseTree) {
138: this .subqueryParseTree = parseTree;
139: }
140:
141: /** */
142: public EJBQLParseTree getParseTree() {
143: return subqueryParseTree;
144: }
145:
146: }
|