KickJava   Java API By Example, From Geeks To Geeks.

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


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 oracle.toplink.essentials.queryframework.*;
25 import oracle.toplink.essentials.internal.sessions.AbstractSession;
26
27 /**
28  * INTERNAL
29  * <p><b>Purpose</b>: This represents an EJBQL parse tre
30  * <p><b>Responsibilities</b>:<ul>
31  * <li> Maintain the context for the expression generation
32  * <li> Build an initial expression
33  * <li> Return a reference class for the expression
34  * <li> Maintain the root node for the query
35  * </ul>
36  * @author Jon Driscoll and Joel Lucuik
37  * @since TopLink 4.0
38  */

39 public class EJBQLParseTree extends ParseTree {
40
41     /**
42      * EJBQLParseTree constructor comment.
43      */

44     public EJBQLParseTree() {
45         super();
46     }
47
48     /**
49      * INTERNAL
50      * Build the context to be used when generating the expression from the parse tree
51      */

52     public GenerationContext buildContext(ReadQuery readQuery, AbstractSession session) {
53         GenerationContext contextForGeneration = super.buildContext(readQuery, session);
54
55         contextForGeneration.setBaseQueryClass(readQuery.getReferenceClass());
56         return contextForGeneration;
57     }
58
59     /**
60      * INTERNAL
61      * Build the context to be used when generating the expression from the
62      * subquery parse tree.
63      */

64     private GenerationContext buildSubqueryContext(ReadQuery readQuery, GenerationContext outer) {
65         GenerationContext context = new SelectGenerationContext(outer, this);
66         context.setBaseQueryClass(readQuery.getReferenceClass());
67         return context;
68     }
69
70     /**
71      * Add all of the relevant query settings from an EJBQLParseTree to the given
72      * database query.
73      * @param query The query to populate
74      * @param outer the GenerationContext of the outer EJBQL query.
75      * @return the GenerationContext for the subquery
76      */

77     public GenerationContext populateSubquery(ObjectLevelReadQuery readQuery, GenerationContext outer) {
78         GenerationContext innerContext = buildSubqueryContext(readQuery, outer);
79         populateReadQueryInternal(readQuery, innerContext);
80         return innerContext;
81     }
82     
83     /**
84      * Add all of the relevant query settings from an EJBQLParseTree to the given
85      * database query.
86      * @param query The query to populate
87      * @param session The sessionto use to information such as descriptors.
88      */

89     public void populateQuery(DatabaseQuery query, AbstractSession session) {
90         if (query.isObjectLevelReadQuery()) {
91             ObjectLevelReadQuery objectQuery = (ObjectLevelReadQuery)query;
92             GenerationContext generationContext = buildContext(objectQuery, session);
93             populateReadQueryInternal(objectQuery, generationContext);
94         } else if (query.isUpdateAllQuery()) {
95             UpdateAllQuery updateQuery = (UpdateAllQuery)query;
96             GenerationContext generationContext = buildContext(updateQuery, session);
97             populateModifyQueryInternal(updateQuery, generationContext);
98             addUpdatesToQuery(updateQuery, generationContext);
99         } else if (query.isDeleteAllQuery()) {
100             DeleteAllQuery deleteQuery = (DeleteAllQuery)query;
101             GenerationContext generationContext = buildContext(deleteQuery, session);
102             populateModifyQueryInternal(deleteQuery, generationContext);
103         }
104     }
105
106     private void populateReadQueryInternal(ObjectLevelReadQuery objectQuery,
107                                            GenerationContext generationContext) {
108         // Get the reference class if it does not exist. This is done
109
// for dynamic queries in EJBQL 3.0
110
if (objectQuery.getReferenceClass() == null) {
111             // Adjust the reference class if necessary
112
adjustReferenceClassForQuery(objectQuery, generationContext);
113         }
114
115         // Initialize the base expression in the generation context
116
initBaseExpression(objectQuery, generationContext);
117         
118         // Validate parse tree
119
validate(generationContext.getSession(), getClassLoader());
120
121         // Apply the query node to the query (this will be a SelectNode for a read query)
122
applyQueryNodeToQuery(objectQuery, generationContext);
123         
124         // Verify the SELECT is valid (valid alias, etc)
125
verifySelect(objectQuery, generationContext);
126         
127         // This is what it's all about...
128
setSelectionCriteriaForQuery(objectQuery, generationContext);
129         
130         // Add any ordering
131
addOrderingToQuery(objectQuery, generationContext);
132
133         // Add any grouping
134
addGroupingToQuery(objectQuery, generationContext);
135         
136         // Add having
137
addHavingToQuery(objectQuery, generationContext);
138         
139         // Add non fetch joined variables
140
addNonFetchJoinAttributes(objectQuery, generationContext);
141     }
142
143     private void populateModifyQueryInternal(ModifyAllQuery query,
144                                              GenerationContext generationContext) {
145         if (query.getReferenceClass() == null) {
146             // Adjust the reference class if necessary
147
adjustReferenceClassForQuery(query, generationContext);
148         }
149         query.setSession(generationContext.getSession());
150
151         // Initialize the base expression in the generation context
152
initBaseExpression(query, generationContext);
153
154         // Validate parse tree
155
validate(generationContext.getSession(), getClassLoader());
156
157         // Apply the query node to the query
158
applyQueryNodeToQuery(query, generationContext);
159         
160         setSelectionCriteriaForQuery(query, generationContext);
161     }
162
163 }
164
Popular Tags