KickJava   Java API By Example, From Geeks To Geeks.

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


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.Hashtable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import oracle.toplink.essentials.expressions.Expression;
29 import oracle.toplink.essentials.internal.sessions.AbstractSession;
30
31 /**
32  * INTERNAL:
33  * An extension of GenerationContext the provides SELECT specfic behavior.
34  * Used when building the query features that are not usable in other types of queries
35  */

36 public class SelectGenerationContext extends GenerationContext {
37     //if a 1:1 is SELECTed in the EJBQL, then we need to use parallel expressions
38
//with each ExpressionBuilder created using "new ExpressionBuilder(MyClass.class)"
39
private boolean useParallelExpressions = false;
40
41     //BUG 3105651: If a variable is SELECTed, and it's in an ORDER BY, then
42
//we want the ExpressionBuilder to be instantiated using an empty constructor
43
private boolean shouldCheckSelectNodeBeforeResolving = false;
44     private boolean isNotIndicatedInMemberOf = false;
45
46     //If a NOT MEMBER OF is encountered, we need to store the MEMBER OF
47
//so that the right side of the member of can use the stored expression
48
//from the left
49
private MemberOfNode memberOfNode = null;
50
51     //Do we want to use outer joins? get("address") vs getAllowingNull("address")
52
private boolean shouldUseOuterJoins = false;
53
54     //Outer SelectGenerationContext
55
private GenerationContext outer = null;
56
57     public SelectGenerationContext() {
58         super();
59     }
60
61     /**
62      * Constructor used to create the context for a subquery.
63      */

64     public SelectGenerationContext(GenerationContext outer, ParseTree newParseTree) {
65         this(outer.getParseTreeContext(), outer.getSession(), newParseTree);
66         this.outer = outer;
67     }
68
69     public SelectGenerationContext(ParseTreeContext newContext, AbstractSession newSession, ParseTree newParseTree) {
70         super(newContext, newSession, newParseTree);
71
72         //indicate if we want parallel expressions or not
73
useParallelExpressions = this.computeUseParallelExpressions();
74     }
75
76     //Set and get the contained MemberOfNode. This is for handling NOT MEMBER OF.
77
public void setMemberOfNode(MemberOfNode newMemberOfNode) {
78         memberOfNode = newMemberOfNode;
79     }
80
81     public MemberOfNode getMemberOfNode() {
82         return memberOfNode;
83     }
84
85     private boolean computeUseParallelExpressions() {
86         boolean computedUseParallelExpressions;
87
88         //use parallel expressions if I have a 1:1 selected, and the same class isn't
89
//declared in the FROM
90
computedUseParallelExpressions = ((SelectNode)this.parseTree.getQueryNode()).hasOneToOneSelected(this);
91         //check if they've SELECTed a variable declared in the IN clause in the FROM,
92
//or they've mapped more than one variable to the same type in the FROM
93
computedUseParallelExpressions = computedUseParallelExpressions || ((SelectNode)this.parseTree.getQueryNode()).isVariableInINClauseSelected(this) || this.parseTree.getContext().hasMoreThanOneVariablePerType() || this.parseTree.getContext().hasMoreThanOneAliasInFrom();
94         return computedUseParallelExpressions;
95     }
96
97     //Answer true if we need to use parallel expressions
98
//This will be the case if a 1:1 is SELECTed in the EJBQL.
99
public boolean useParallelExpressions() {
100         return useParallelExpressions;
101     }
102
103     //Indicate that we want VariableNodes to check if they're
104
//SELECTed first, to determine how to instantiate the ExpressionBuilder
105
public void checkSelectNodeBeforeResolving(boolean shouldCheck) {
106         shouldCheckSelectNodeBeforeResolving = shouldCheck;
107     }
108
109     //Answer true if we want VariableNodes to check if they're
110
//SELECTed first, to determine how to instantiate the ExpressionBuilder
111
public boolean shouldCheckSelectNodeBeforeResolving() {
112         return shouldCheckSelectNodeBeforeResolving;
113     }
114
115     //Answer true if we should use outer joins in our get() (vs getAllowingNull())
116
public boolean shouldUseOuterJoins() {
117         return shouldUseOuterJoins;
118     }
119
120     public void useOuterJoins() {
121         shouldUseOuterJoins = true;
122     }
123
124     public void dontUseOuterJoins() {
125         shouldUseOuterJoins = false;
126     }
127
128     //Answer true if we have a MemberOfNode contained. This is for handling NOT MEMBER OF
129
public boolean hasMemberOfNode() {
130         return memberOfNode != null;
131     }
132
133     /** */
134     public GenerationContext getOuterContext() {
135         return outer;
136     }
137     
138     /**
139      * Iterate the set of variables declared in an outer scope and
140      * connect the inner varaible expression with the outer one.
141      */

142     public Expression joinVariables(Set JavaDoc variables) {
143         if ((outer == null) || (variables == null) || variables.isEmpty()) {
144             // not an inner query or no variables to join
145
return null;
146         }
147         Expression expr = null;
148         for (Iterator JavaDoc i = variables.iterator(); i.hasNext(); ) {
149             String JavaDoc name = (String JavaDoc)i.next();
150             VariableNode var = new VariableNode(name);
151             Expression innerExpr = var.generateExpression(this);
152             Expression outerExpr = var.generateExpression(outer);
153             Expression join = innerExpr.equal(outerExpr);
154             expr = var.appendExpression(expr, join);
155         }
156         return expr;
157     }
158 }
159
Popular Tags