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.Iterator;
040: import java.util.List;
041: import java.util.ArrayList;
042:
043: import oracle.toplink.essentials.expressions.*;
044: import oracle.toplink.essentials.internal.expressions.ConstantExpression;
045: import oracle.toplink.essentials.internal.queryframework.ReportItem;
046: import oracle.toplink.essentials.queryframework.ReportQuery;
047:
048: /**
049: * INTERNAL
050: * <p><b>Purpose</b>: Represent an EXISTS subquery.
051: */
052: public class ExistsNode extends Node {
053:
054: /** True in case of a NOT EXISTS (...) query. */
055: private boolean notIndicated = false;
056:
057: /**
058: * Return a new ExistsNode.
059: */
060: public ExistsNode() {
061: super ();
062: }
063:
064: /**
065: * INTERNAL
066: * Validate node and calculate its type.
067: * Change subquery SELECT clause.
068: */
069: public void validate(ParseTreeContext context) {
070: if (left != null) {
071:
072: // change SELECT clause of subquery
073: SubqueryNode subqueryNode = (SubqueryNode) getLeft();
074: // validate changed subquery
075: subqueryNode.validate(context);
076:
077: TypeHelper typeHelper = context.getTypeHelper();
078: setType(typeHelper.getBooleanType());
079: }
080: }
081:
082: /**
083: * INTERNAL
084: * Generate the TopLink expression for this node
085: */
086: public Expression generateExpression(GenerationContext context) {
087: SubqueryNode subqueryNode = (SubqueryNode) getLeft();
088: ReportQuery reportQuery = subqueryNode.getReportQuery(context);
089: // Replace the SELECT clause of the exists subquery by SELECT 1 to
090: // avoid problems with databases not supporting mutiple columns in the
091: // subquery SELECT clause in SQL.
092: // The original select clause expressions might include relationship
093: // navigations which should result in FK joins in the generated SQL,
094: // e.g. ... EXISTS (SELECT o.customer FROM Order o ...). Add the
095: // select clause expressions as non fetch join attributes to the
096: // ReportQuery representing the subquery. This make sure the FK joins
097: // get generated.
098: List items = reportQuery.getItems();
099: for (Iterator i = items.iterator(); i.hasNext();) {
100: ReportItem item = (ReportItem) i.next();
101: Expression expr = item.getAttributeExpression();
102: reportQuery.addNonFetchJoinedAttribute(expr);
103: }
104: reportQuery.clearItems();
105: Expression one = new ConstantExpression(new Integer(1),
106: new ExpressionBuilder());
107: reportQuery.addItem("one", one);
108: reportQuery.dontUseDistinct();
109: Expression expr = context.getBaseExpression();
110: return notIndicated() ? expr.notExists(reportQuery) : expr
111: .exists(reportQuery);
112: }
113:
114: /**
115: * INTERNAL
116: * Indicate if a NOT was found in the WHERE clause.
117: * Examples: WHERE ... NOT EXISTS(...)
118: */
119: public void indicateNot() {
120: notIndicated = true;
121: }
122:
123: public boolean notIndicated() {
124: return notIndicated;
125: }
126:
127: }
|