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 oracle.toplink.essentials.queryframework.*;
040: import oracle.toplink.essentials.internal.sessions.AbstractSession;
041:
042: /**
043: * INTERNAL
044: * <p><b>Purpose</b>: This represents an EJBQL parse tre
045: * <p><b>Responsibilities</b>:<ul>
046: * <li> Maintain the context for the expression generation
047: * <li> Build an initial expression
048: * <li> Return a reference class for the expression
049: * <li> Maintain the root node for the query
050: * </ul>
051: * @author Jon Driscoll and Joel Lucuik
052: * @since TopLink 4.0
053: */
054: public class EJBQLParseTree extends ParseTree {
055:
056: /**
057: * EJBQLParseTree constructor comment.
058: */
059: public EJBQLParseTree() {
060: super ();
061: }
062:
063: /**
064: * INTERNAL
065: * Build the context to be used when generating the expression from the parse tree
066: */
067: public GenerationContext buildContext(ReadQuery readQuery,
068: AbstractSession session) {
069: GenerationContext contextForGeneration = super .buildContext(
070: readQuery, session);
071:
072: contextForGeneration.setBaseQueryClass(readQuery
073: .getReferenceClass());
074: return contextForGeneration;
075: }
076:
077: /**
078: * INTERNAL
079: * Build the context to be used when generating the expression from the
080: * subquery parse tree.
081: */
082: private GenerationContext buildSubqueryContext(ReadQuery readQuery,
083: GenerationContext outer) {
084: GenerationContext context = new SelectGenerationContext(outer,
085: this );
086: context.setBaseQueryClass(readQuery.getReferenceClass());
087: return context;
088: }
089:
090: /**
091: * Add all of the relevant query settings from an EJBQLParseTree to the given
092: * database query.
093: * @param query The query to populate
094: * @param outer the GenerationContext of the outer EJBQL query.
095: * @return the GenerationContext for the subquery
096: */
097: public GenerationContext populateSubquery(
098: ObjectLevelReadQuery readQuery, GenerationContext outer) {
099: GenerationContext innerContext = buildSubqueryContext(
100: readQuery, outer);
101: populateReadQueryInternal(readQuery, innerContext);
102: return innerContext;
103: }
104:
105: /**
106: * Add all of the relevant query settings from an EJBQLParseTree to the given
107: * database query.
108: * @param query The query to populate
109: * @param session The sessionto use to information such as descriptors.
110: */
111: public void populateQuery(DatabaseQuery query,
112: AbstractSession session) {
113: if (query.isObjectLevelReadQuery()) {
114: ObjectLevelReadQuery objectQuery = (ObjectLevelReadQuery) query;
115: GenerationContext generationContext = buildContext(
116: objectQuery, session);
117: populateReadQueryInternal(objectQuery, generationContext);
118: } else if (query.isUpdateAllQuery()) {
119: UpdateAllQuery updateQuery = (UpdateAllQuery) query;
120: GenerationContext generationContext = buildContext(
121: updateQuery, session);
122: populateModifyQueryInternal(updateQuery, generationContext);
123: addUpdatesToQuery(updateQuery, generationContext);
124: } else if (query.isDeleteAllQuery()) {
125: DeleteAllQuery deleteQuery = (DeleteAllQuery) query;
126: GenerationContext generationContext = buildContext(
127: deleteQuery, session);
128: populateModifyQueryInternal(deleteQuery, generationContext);
129: }
130: }
131:
132: private void populateReadQueryInternal(
133: ObjectLevelReadQuery objectQuery,
134: GenerationContext generationContext) {
135: // Get the reference class if it does not exist. This is done
136: // for dynamic queries in EJBQL 3.0
137: if (objectQuery.getReferenceClass() == null) {
138: // Adjust the reference class if necessary
139: adjustReferenceClassForQuery(objectQuery, generationContext);
140: }
141:
142: // Initialize the base expression in the generation context
143: initBaseExpression(objectQuery, generationContext);
144:
145: // Validate parse tree
146: validate(generationContext.getSession(), getClassLoader());
147:
148: // Apply the query node to the query (this will be a SelectNode for a read query)
149: applyQueryNodeToQuery(objectQuery, generationContext);
150:
151: // Verify the SELECT is valid (valid alias, etc)
152: verifySelect(objectQuery, generationContext);
153:
154: // This is what it's all about...
155: setSelectionCriteriaForQuery(objectQuery, generationContext);
156:
157: // Add any ordering
158: addOrderingToQuery(objectQuery, generationContext);
159:
160: // Add any grouping
161: addGroupingToQuery(objectQuery, generationContext);
162:
163: // Add having
164: addHavingToQuery(objectQuery, generationContext);
165:
166: // Add non fetch joined variables
167: addNonFetchJoinAttributes(objectQuery, generationContext);
168: }
169:
170: private void populateModifyQueryInternal(ModifyAllQuery query,
171: GenerationContext generationContext) {
172: if (query.getReferenceClass() == null) {
173: // Adjust the reference class if necessary
174: adjustReferenceClassForQuery(query, generationContext);
175: }
176: query.setSession(generationContext.getSession());
177:
178: // Initialize the base expression in the generation context
179: initBaseExpression(query, generationContext);
180:
181: // Validate parse tree
182: validate(generationContext.getSession(), getClassLoader());
183:
184: // Apply the query node to the query
185: applyQueryNodeToQuery(query, generationContext);
186:
187: setSelectionCriteriaForQuery(query, generationContext);
188: }
189:
190: }
|