KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > AnyResultSet


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.AnyResultSet
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.execute;
23
24 import org.apache.derby.iapi.services.monitor.Monitor;
25
26 import org.apache.derby.iapi.services.sanity.SanityManager;
27
28 import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
29
30 import org.apache.derby.iapi.sql.conn.StatementContext;
31
32 import org.apache.derby.iapi.sql.execute.NoPutResultSet;
33 import org.apache.derby.iapi.sql.ResultSet;
34 import org.apache.derby.iapi.sql.execute.ExecRow;
35 import org.apache.derby.iapi.types.DataValueDescriptor;
36 import org.apache.derby.iapi.sql.Activation;
37
38 import org.apache.derby.iapi.services.loader.GeneratedMethod;
39
40 import org.apache.derby.iapi.error.StandardException;
41
42 /**
43  * Takes a quantified predicate subquery's result set.
44  * NOTE: A row with a single column containing null will be returned from
45  * getNextRow() if the underlying subquery ResultSet is empty.
46  *
47  * @author jerry
48  */

49 public class AnyResultSet extends NoPutResultSetImpl
50 {
51
52     /* Used to cache row with nulls for case when subquery result set
53      * is empty.
54      */

55     private ExecRow rowWithNulls;
56
57     /* Used to cache the StatementContext */
58     private StatementContext statementContext;
59
60     // set in constructor and not altered during
61
// life of object.
62
public final NoPutResultSet source;
63     private GeneratedMethod emptyRowFun;
64     public int subqueryNumber;
65     public int pointOfAttachment;
66
67     //
68
// class interface
69
//
70
public AnyResultSet(NoPutResultSet s, Activation a, GeneratedMethod emptyRowFun,
71                         int resultSetNumber, int subqueryNumber,
72                         int pointOfAttachment,
73                         double optimizerEstimatedRowCount,
74                         double optimizerEstimatedCost)
75     {
76         super(a, resultSetNumber, optimizerEstimatedRowCount, optimizerEstimatedCost);
77         source = s;
78         this.emptyRowFun = emptyRowFun;
79         this.subqueryNumber = subqueryNumber;
80         this.pointOfAttachment = pointOfAttachment;
81         constructorTime += getElapsedMillis(beginTime);
82     }
83
84     //
85
// ResultSet interface (leftover from NoPutResultSet)
86
//
87

88     /**
89      * open a scan on the table. scan parameters are evaluated
90      * at each open, so there is probably some way of altering
91      * their values...
92      *
93      * @exception StandardException thrown if cursor finished.
94      */

95     public void openCore() throws StandardException
96     {
97         /* NOTE: We can't get code generation
98          * to generate calls to reopenCore() for
99          * subsequent probes, so we just handle
100          * it here.
101          */

102         if (isOpen)
103         {
104             reopenCore();
105             return;
106         }
107
108         beginTime = getCurrentTimeMillis();
109
110         source.openCore();
111
112         /* Notify StatementContext about ourself so that we can
113          * get closed down, if necessary, on an exception.
114          */

115         if (statementContext == null)
116         {
117             statementContext = getLanguageConnectionContext().getStatementContext();
118         }
119         statementContext.setSubqueryResultSet(subqueryNumber, this,
120                                               activation.getNumSubqueries());
121
122         numOpens++;
123         isOpen = true;
124         openTime += getElapsedMillis(beginTime);
125     }
126
127     /**
128      * reopen a scan on the table. scan parameters are evaluated
129      * at each open, so there is probably some way of altering
130      * their values...
131      *
132      * @exception StandardException thrown if cursor finished.
133      */

134     public void reopenCore() throws StandardException
135     {
136         beginTime = getCurrentTimeMillis();
137         if (SanityManager.DEBUG)
138             SanityManager.ASSERT(isOpen, "AnyResultSet already open");
139
140         source.reopenCore();
141         numOpens++;
142
143         openTime += getElapsedMillis(beginTime);
144     }
145
146     public void finish() throws StandardException
147     {
148         source.finish();
149         finishAndRTS();
150     }
151
152     /**
153      * Return the requested value computed from the next row.
154      *
155      * @exception StandardException thrown on failure.
156      */

157     public ExecRow getNextRowCore() throws StandardException
158     {
159         ExecRow candidateRow = null;
160         ExecRow secondRow = null;
161         ExecRow result = null;
162
163         beginTime = getCurrentTimeMillis();
164         // This is an ASSERT and not a real error because this is never
165
// outermost in the tree and so a next call when closed will not occur.
166
if (SanityManager.DEBUG) {
167             SanityManager.ASSERT( isOpen, "AnyResultSet not open");
168         }
169
170         if ( isOpen )
171         {
172             candidateRow = source.getNextRowCore();
173             if (candidateRow != null)
174             {
175                 result = candidateRow;
176             }
177             else if (rowWithNulls == null)
178             {
179                 rowWithNulls = (ExecRow) emptyRowFun.invoke(activation);
180                 result = rowWithNulls;
181             }
182             else
183             {
184                 result = rowWithNulls;
185             }
186         }
187
188         currentRow = result;
189         setCurrentRow(result);
190         rowsSeen++;
191
192         nextTime += getElapsedMillis(beginTime);
193         return result;
194     }
195
196     /**
197      * If the result set has been opened,
198      * close the open scan.
199      *
200      * @exception StandardException thrown on error
201      */

202     public void close() throws StandardException
203     {
204         beginTime = getCurrentTimeMillis();
205         if ( isOpen )
206         {
207             // we don't want to keep around a pointer to the
208
// row ... so it can be thrown away.
209
// REVISIT: does this need to be in a finally
210
// block, to ensure that it is executed?
211
clearCurrentRow();
212
213             source.close();
214
215             super.close();
216         }
217         else
218             if (SanityManager.DEBUG)
219                 SanityManager.DEBUG("CloseRepeatInfo","Close of AnyResultSet repeated");
220
221         closeTime += getElapsedMillis(beginTime);
222     }
223
224     /**
225      * @see NoPutResultSet#getPointOfAttachment
226      */

227     public int getPointOfAttachment()
228     {
229         return pointOfAttachment;
230     }
231
232     /**
233      * Return the total amount of time spent in this ResultSet
234      *
235      * @param type CURRENT_RESULTSET_ONLY - time spent only in this ResultSet
236      * ENTIRE_RESULTSET_TREE - time spent in this ResultSet and below.
237      *
238      * @return long The total amount of time spent (in milliseconds).
239      */

240     public long getTimeSpent(int type)
241     {
242         long totTime = constructorTime + openTime + nextTime + closeTime;
243
244         if (type == NoPutResultSet.CURRENT_RESULTSET_ONLY)
245         {
246             return totTime - source.getTimeSpent(ENTIRE_RESULTSET_TREE);
247         }
248         else
249         {
250             return totTime;
251         }
252     }
253 }
254
Popular Tags