KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.RowResultSet
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 import org.apache.derby.iapi.services.stream.InfoStreams;
30
31 import org.apache.derby.iapi.sql.execute.CursorResultSet;
32 import org.apache.derby.iapi.sql.execute.ExecRow;
33 import org.apache.derby.iapi.sql.execute.NoPutResultSet;
34
35 import org.apache.derby.iapi.sql.Activation;
36 import org.apache.derby.iapi.sql.ResultDescription;
37
38 import org.apache.derby.iapi.error.StandardException;
39
40 import org.apache.derby.iapi.services.loader.GeneratedMethod;
41
42 import org.apache.derby.iapi.types.RowLocation;
43
44 /**
45  * Takes a constant row value and returns it as
46  * a result set.
47  * <p>
48  * This class actually probably never underlies a select statement,
49  * but in case it might and because it has the same behavior as the
50  * ones that do, we have it implement CursorResultSet and give
51  * reasonable answers.
52  *
53  * @author ames
54  */

55 class RowResultSet extends NoPutResultSetImpl
56     implements CursorResultSet {
57
58     /* Run time statistics variables */
59     public int rowsReturned;
60
61     private boolean canCacheRow;
62     private boolean next;
63     private GeneratedMethod row;
64     private ExecRow cachedRow;
65
66     //
67
// class interface
68
//
69
RowResultSet
70     (
71         Activation activation,
72         GeneratedMethod row,
73         boolean canCacheRow,
74         int resultSetNumber,
75         double optimizerEstimatedRowCount,
76         double optimizerEstimatedCost
77     )
78     {
79         super(activation, resultSetNumber,
80               optimizerEstimatedRowCount, optimizerEstimatedCost);
81
82         this.row = row;
83         this.canCacheRow = canCacheRow;
84         constructorTime += getElapsedMillis(beginTime);
85     }
86
87     /* This constructor takes in a constant row value, as the cache row. See the
88      * usage in beetle 4373 for materializing subquery.
89      */

90     RowResultSet
91     (
92         Activation activation,
93         ExecRow constantRow,
94         boolean canCacheRow,
95         int resultSetNumber,
96         double optimizerEstimatedRowCount,
97         double optimizerEstimatedCost
98     )
99     {
100         super(activation, resultSetNumber,
101               optimizerEstimatedRowCount, optimizerEstimatedCost);
102
103         beginTime = getCurrentTimeMillis();
104         this.cachedRow = constantRow;
105         this.canCacheRow = canCacheRow;
106         constructorTime += getElapsedMillis(beginTime);
107     }
108
109     //
110
// ResultSet interface (leftover from NoPutResultSet)
111
//
112

113     /**
114      * Sets state to 'open'.
115      *
116      * @exception StandardException thrown if activation closed.
117      */

118     public void openCore() throws StandardException
119     {
120         next = false;
121         beginTime = getCurrentTimeMillis();
122         isOpen = true;
123         numOpens++;
124
125         openTime += getElapsedMillis(beginTime);
126     }
127
128     /**
129      * If open and not returned yet, returns the row
130      * after plugging the parameters into the expressions.
131      *
132      * @exception StandardException thrown on failure.
133      */

134     public ExecRow getNextRowCore() throws StandardException {
135
136         currentRow = null;
137         beginTime = getCurrentTimeMillis();
138         if (isOpen)
139         {
140             if (!next)
141             {
142                 next = true;
143                 if (currentRow == null)
144                 {
145                     if (cachedRow != null)
146                     {
147                         currentRow = cachedRow;
148                     }
149                     else if (row != null)
150                     {
151                         currentRow = (ExecRow) row.invoke(activation);
152                         if (canCacheRow)
153                         {
154                             cachedRow = currentRow;
155                         }
156                     }
157                 }
158                 rowsReturned++;
159             }
160             setCurrentRow(currentRow);
161
162             nextTime += getElapsedMillis(beginTime);
163         }
164         return currentRow;
165     }
166
167     /**
168      * @see org.apache.derby.iapi.sql.ResultSet#close
169      *
170      * @exception StandardException thrown on error
171      */

172     public void close() throws StandardException
173     {
174         beginTime = getCurrentTimeMillis();
175         if (isOpen) {
176
177             // we don't want to keep around a pointer to the
178
// row ... so it can be thrown away.
179
// REVISIT: does this need to be in a finally
180
// block, to ensure that it is executed?
181
clearCurrentRow();
182             next = false;
183
184             super.close();
185         }
186         else
187             if (SanityManager.DEBUG)
188                 SanityManager.DEBUG("CloseRepeatInfo","Close of RowResultSet repeated");
189
190         closeTime += getElapsedMillis(beginTime);
191     }
192
193     /**
194      * Return the total amount of time spent in this ResultSet
195      *
196      * @param type CURRENT_RESULTSET_ONLY - time spent only in this ResultSet
197      * ENTIRE_RESULTSET_TREE - time spent in this ResultSet and below.
198      *
199      * @return long The total amount of time spent (in milliseconds).
200      */

201     public long getTimeSpent(int type)
202     {
203         long totTime = constructorTime + openTime + nextTime + closeTime;
204         return totTime;
205     }
206
207     //
208
// CursorResultSet interface
209
//
210

211     /**
212      * This is not operating against a stored table,
213      * so it has no row location to report.
214      *
215      * @see CursorResultSet
216      *
217      * @return a null.
218      */

219     public RowLocation getRowLocation() {
220         if (SanityManager.DEBUG)
221             SanityManager.THROWASSERT("RowResultSet used in positioned update/delete");
222         return null;
223     }
224
225     /**
226      * This is not used in positioned update and delete,
227      * so just return a null.
228      *
229      * @see CursorResultSet
230      *
231      * @return a null.
232      */

233     public ExecRow getCurrentRow() {
234         if (SanityManager.DEBUG)
235             SanityManager.THROWASSERT("RowResultSet used in positioned update/delete");
236         return null;
237     }
238 }
239
Popular Tags