KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27
28 import oracle.toplink.essentials.expressions.*;
29 import oracle.toplink.essentials.internal.expressions.ConstantExpression;
30 import oracle.toplink.essentials.internal.queryframework.ReportItem;
31 import oracle.toplink.essentials.queryframework.ReportQuery;
32
33 /**
34  * INTERNAL
35  * <p><b>Purpose</b>: Represent an EXISTS subquery.
36  */

37 public class ExistsNode extends Node {
38
39     /** True in case of a NOT EXISTS (...) query. */
40     private boolean notIndicated = false;
41
42     /**
43      * Return a new ExistsNode.
44      */

45     public ExistsNode() {
46         super();
47     }
48
49     /**
50      * INTERNAL
51      * Validate node and calculate its type.
52      * Change subquery SELECT clause.
53      */

54     public void validate(ParseTreeContext context) {
55         if (left != null) {
56             
57             // change SELECT clause of subquery
58
SubqueryNode subqueryNode = (SubqueryNode)getLeft();
59             // validate changed subquery
60
subqueryNode.validate(context);
61
62             TypeHelper typeHelper = context.getTypeHelper();
63             setType(typeHelper.getBooleanType());
64         }
65     }
66
67     /**
68      * INTERNAL
69      * Generate the TopLink expression for this node
70      */

71     public Expression generateExpression(GenerationContext context) {
72         SubqueryNode subqueryNode = (SubqueryNode)getLeft();
73         ReportQuery reportQuery = subqueryNode.getReportQuery(context);
74         // Replace the SELECT clause of the exists subquery by SELECT 1 to
75
// avoid problems with databases not supporting mutiple columns in the
76
// subquery SELECT clause in SQL.
77
// The original select clause expressions might include relationship
78
// navigations which should result in FK joins in the generated SQL,
79
// e.g. ... EXISTS (SELECT o.customer FROM Order o ...). Add the
80
// select clause expressions as non fetch join attributes to the
81
// ReportQuery representing the subquery. This make sure the FK joins
82
// get generated.
83
List JavaDoc items = reportQuery.getItems();
84         for (Iterator JavaDoc i = items.iterator(); i.hasNext();) {
85             ReportItem item = (ReportItem)i.next();
86             Expression expr = item.getAttributeExpression();
87             reportQuery.addNonFetchJoinedAttribute(expr);
88         }
89         reportQuery.clearItems();
90         Expression one = new ConstantExpression(new Integer JavaDoc(1), new ExpressionBuilder());
91         reportQuery.addItem("one", one);
92         reportQuery.dontUseDistinct();
93         Expression expr = context.getBaseExpression();
94         return notIndicated() ? expr.notExists(reportQuery) :
95             expr.exists(reportQuery);
96     }
97
98     /**
99      * INTERNAL
100      * Indicate if a NOT was found in the WHERE clause.
101      * Examples: WHERE ... NOT EXISTS(...)
102      */

103     public void indicateNot() {
104         notIndicated = true;
105     }
106
107     public boolean notIndicated() {
108         return notIndicated;
109     }
110
111 }
112
Popular Tags