KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > SubqueryList


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.SubqueryList
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
25
26 import org.apache.derby.iapi.error.StandardException;
27
28 import org.apache.derby.iapi.services.sanity.SanityManager;
29
30 /**
31  * A SubqueryList represents a list of subquerys within a specific clause
32  * (select, where or having) in a DML statement. It extends QueryTreeNodeVector.
33  *
34  * @author Jerry Brenner
35  */

36
37 public class SubqueryList extends QueryTreeNodeVector
38 {
39
40     /**
41      * Prints the sub-nodes of this object. See QueryTreeNode.java for
42      * how tree printing is supposed to work.
43      *
44      * @param depth The depth of this node in the tree
45      */

46
47     public void printSubNodes(int depth)
48     {
49         if (SanityManager.DEBUG)
50         {
51             SubqueryNode subqueryNode;
52
53             super.printSubNodes(depth);
54
55             for (int index = 0; index < size(); index++)
56             {
57                 subqueryNode = (SubqueryNode) elementAt(index);
58                 subqueryNode.treePrint(depth + 1);
59             }
60         }
61     }
62
63     /**
64      * Add a subquery to the list.
65      *
66      * @param subqueryNode A SubqueryNode to add to the list
67      *
68      */

69
70     public void addSubqueryNode(SubqueryNode subqueryNode) throws StandardException
71     {
72         addElement(subqueryNode);
73     }
74
75     /**
76      * Preprocess a SubqueryList. For now, we just preprocess each SubqueryNode
77      * in the list.
78      *
79      * @param numTables Number of tables in the DML Statement
80      * @param outerFromList FromList from outer query block
81      * @param outerSubqueryList SubqueryList from outer query block
82      * @param outerPredicateList PredicateList from outer query block
83      *
84      * @exception StandardException Thrown on error
85      */

86     public void preprocess(int numTables,
87                             FromList outerFromList,
88                             SubqueryList outerSubqueryList,
89                             PredicateList outerPredicateList)
90                 throws StandardException
91     {
92         SubqueryNode subqueryNode;
93
94         int size = size();
95         for (int index = 0; index < size; index++)
96         {
97             subqueryNode = (SubqueryNode) elementAt(index);
98             subqueryNode.preprocess(numTables, outerFromList,
99                                     outerSubqueryList,
100                                     outerPredicateList);
101         }
102     }
103
104     /**
105      * Optimize the subqueries in this list.
106      *
107      * @param dataDictionary The data dictionary to use for optimization
108      * @param outerRows The optimizer's estimate of the number of
109      * times this subquery will be executed.
110      *
111      * @exception StandardException Thrown on error
112      */

113
114     public void optimize(DataDictionary dataDictionary, double outerRows)
115             throws StandardException
116     {
117         int size = size();
118         for (int index = 0; index < size; index++)
119         {
120             SubqueryNode subqueryNode;
121             subqueryNode = (SubqueryNode) elementAt(index);
122             subqueryNode.optimize(dataDictionary, outerRows);
123         }
124     }
125
126     /**
127      * Modify the access paths for all subqueries in this list.
128      *
129      * @see ResultSetNode#modifyAccessPaths
130      *
131      * @exception StandardException Thrown on error
132      */

133     public void modifyAccessPaths()
134             throws StandardException
135     {
136         int size = size();
137         for (int index = 0; index < size; index++)
138         {
139             SubqueryNode subqueryNode;
140             subqueryNode = (SubqueryNode) elementAt(index);
141             subqueryNode.modifyAccessPaths();
142         }
143     }
144
145     /**
146      * Search to see if a query references the specifed table name.
147      *
148      * @param name Table name (String) to search for.
149      * @param baseTable Whether or not name is for a base table
150      *
151      * @return true if found, else false
152      *
153      * @exception StandardException Thrown on error
154      */

155     public boolean referencesTarget(String JavaDoc name, boolean baseTable)
156         throws StandardException
157     {
158         int size = size();
159         for (int index = 0; index < size; index++)
160         {
161             SubqueryNode subqueryNode;
162
163             subqueryNode = (SubqueryNode) elementAt(index);
164             if (subqueryNode.isMaterializable())
165             {
166                 continue;
167             }
168
169             if (subqueryNode.getResultSet().referencesTarget(name, baseTable))
170             {
171                 return true;
172             }
173         }
174
175         return false;
176     }
177
178     /**
179      * Return true if the node references SESSION schema tables (temporary or permanent)
180      *
181      * @return true if references SESSION schema tables, else false
182      *
183      * @exception StandardException Thrown on error
184      */

185     public boolean referencesSessionSchema()
186         throws StandardException
187     {
188         int size = size();
189         for (int index = 0; index < size; index++)
190         {
191             SubqueryNode subqueryNode;
192
193             subqueryNode = (SubqueryNode) elementAt(index);
194
195             if (subqueryNode.getResultSet().referencesSessionSchema())
196             {
197                 return true;
198             }
199         }
200
201         return false;
202     }
203
204     /**
205      * Set the point of attachment in all subqueries in this list.
206      *
207      * @param pointOfAttachment The point of attachment
208      *
209      * @exception StandardException Thrown on error
210      */

211     public void setPointOfAttachment(int pointOfAttachment)
212         throws StandardException
213     {
214         int size = size();
215
216         for (int index = 0; index < size; index++)
217         {
218             SubqueryNode subqueryNode;
219
220             subqueryNode = (SubqueryNode) elementAt(index);
221             subqueryNode.setPointOfAttachment(pointOfAttachment);
222         }
223     }
224
225     /**
226      * Decrement (query block) level (0-based) for
227      * all of the tables in this subquery list.
228      * This is useful when flattening a subquery.
229      *
230      * @param decrement The amount to decrement by.
231      */

232     void decrementLevel(int decrement)
233     {
234         int size = size();
235
236         for (int index = 0; index < size; index++)
237         {
238             ((SubqueryNode) elementAt(index)).getResultSet().decrementLevel(decrement);
239         }
240     }
241 }
242
243
Popular Tags