KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > parsing > SubqueryNode


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.parsing;
23
24 import java.util.Set JavaDoc;
25
26 import oracle.toplink.essentials.expressions.*;
27 import oracle.toplink.essentials.queryframework.ReportQuery;
28
29 /**
30  * INTERNAL
31  * <p><b>Purpose</b>: Represent a subquery.
32  */

33 public class SubqueryNode extends Node {
34
35     private EJBQLParseTree subqueryParseTree;
36
37     /** Set of names of variables declared in an outer scope and used in teh
38      * subquery. */

39     private Set JavaDoc outerVars;
40
41     /**
42      * Return a new SubqueryNode.
43      */

44     public SubqueryNode() {
45         super();
46     }
47
48     /** */
49     public ReportQuery getReportQuery(GenerationContext context) {
50         ReportQuery innerQuery = new ReportQuery();
51         GenerationContext innerContext =
52             subqueryParseTree.populateSubquery(innerQuery, context);
53         Expression joins = innerContext.joinVariables(outerVars);
54         if (joins != null) {
55             Expression where = innerQuery.getSelectionCriteria();
56             where = appendExpression(where, joins);
57             innerQuery.setSelectionCriteria(where);
58         }
59         return innerQuery;
60     }
61
62     /**
63      * INTERNAL
64      * Validate node and calculate its type.
65      */

66     public void validate(ParseTreeContext context) {
67         subqueryParseTree.validate(context);
68         outerVars = context.getOuterScopeVariables();
69         SelectNode selectNode = (SelectNode)subqueryParseTree.getQueryNode();
70         // Get the select expression, subqueries only have one
71
Node selectExpr = (Node)selectNode.getSelectExpressions().get(0);
72         setType(selectExpr.getType());
73     }
74
75     /**
76      * INTERNAL
77      * Generate the TopLink expression for this node
78      */

79     public Expression generateExpression(GenerationContext context) {
80         Expression base = context.getBaseExpression();
81         ReportQuery innerQuery = getReportQuery(context);
82         return base.subQuery(innerQuery);
83     }
84     
85     /**
86      * INTERNAL
87      * Is this node a SubqueryNode
88      */

89     public boolean isSubqueryNode() {
90         return true;
91     }
92
93     /** */
94     public void setParseTree(EJBQLParseTree parseTree) {
95         this.subqueryParseTree = parseTree;
96     }
97
98     /** */
99     public EJBQLParseTree getParseTree() {
100         return subqueryParseTree;
101     }
102     
103 }
104
105
Popular Tags