KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > net > NetResultSet


1 /*
2
3    Derby - Class org.apache.derby.client.net.NetResultSet
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 package org.apache.derby.client.net;
22
23 import org.apache.derby.client.am.Cursor;
24 import org.apache.derby.client.am.DisconnectException;
25 import org.apache.derby.client.am.Section;
26 import org.apache.derby.client.am.SqlException;
27
28
29 public class NetResultSet extends org.apache.derby.client.am.ResultSet {
30     // Alias for (NetConnection) super.statement.connection
31
private final NetConnection netConnection_;
32
33     // Alias for (NetStatement) super.statement
34
private final NetStatement netStatement_;
35
36     // Alias for (NetCursor) super.cursor
37
final NetCursor netCursor_;
38
39     // Alias for (NetAgent) super.agent
40
final private NetAgent netAgent_;
41
42     // Indicates whether the fixed row protocol is being used. If so,
43
// the fetch size will always be 1.
44
private boolean isFixedRowProtocol = false;
45     
46     //-----------------------------state------------------------------------------
47

48     // This is used to avoid sending multiple outovr over subsequent next()'s
49
public boolean firstOutovrBuilt_ = false;
50
51     //---------------------constructors/finalizer---------------------------------
52

53     // parseOpnqrym() is called right after this constructor is called.
54

55     NetResultSet(NetAgent netAgent,
56                  NetStatement netStatement,
57                  Cursor cursor,
58                  int qryprctyp, //protocolType, CodePoint.FIXROWPRC |
59
// CodePoint.LMTBLKPRC
60
int sqlcsrhld, // holdOption, 0xF0 for false (default) | 0xF1 for true.
61
int qryattscr, // scrollOption, 0xF0 for false (default) | 0xF1 for true.
62
int qryattsns, // sensitivity, CodePoint.QRYUNK |
63
// CodePoint.QRYINS |
64
// CodePoint.QRYSNSSTC
65
int qryattset, // rowsetCursor, 0xF0 for false (default) | 0xF1 for true.
66
long qryinsid, // instanceIdentifier, 0 (if not returned, check default) or number
67
int actualResultSetType,
68                  int actualResultSetConcurrency,
69                  int actualResultSetHoldability) //throws DisconnectException
70
{
71         super(netAgent,
72                 netStatement.statement_,
73                 //new NetCursor (netAgent, qryprctyp),
74
cursor,
75                 // call the constructor with the real resultSetType and resultSetConcurrency
76
// returned from the server
77
actualResultSetType,
78                 actualResultSetConcurrency,
79                 actualResultSetHoldability);
80
81         netAgent_ = netAgent;
82
83         // Set up cheat-links
84
netCursor_ = (NetCursor) cursor_;
85         netStatement_ = netStatement;
86         netConnection_ = netStatement.netConnection_;
87
88         netCursor_.netResultSet_ = this;
89
90         cursorHold_ = (sqlcsrhld != 0xf0);
91         if (qryattscr == 0xF1) {
92             scrollable_ = true;
93         }
94
95         // The number of rows returned by the server will always be 1 when the
96
// Fixed Row Protocol is being used.
97
if (qryprctyp == CodePoint.FIXROWPRC) {
98             isFixedRowProtocol = true;
99             fetchSize_ = 1;
100         } else {
101             fetchSize_ = suggestedFetchSize_;
102         }
103
104         switch (qryattsns) {
105         case CodePoint.QRYUNK:
106             sensitivity_ = sensitivity_unknown__;
107             break;
108         case CodePoint.QRYINS:
109             sensitivity_ = sensitivity_insensitive__;
110             break;
111         case CodePoint.QRYSNSSTC:
112             sensitivity_ = sensitivity_sensitive_static__;
113             break;
114         default: // shouldn't happen
115
break;
116         }
117
118         if (qryattset == 0xF1) {
119             isRowsetCursor_ = true;
120         }
121
122         queryInstanceIdentifier_ = qryinsid;
123         nestingLevel_ = (int) ((queryInstanceIdentifier_ >>> 48) & 0xFFFF);
124     }
125
126
127     //-------------------------------flow methods---------------------------------
128

129     // Go through the QRYDTA's received, and calculate the column offsets for each row.
130
protected void parseRowset_() throws SqlException {
131         int row = 0;
132         // Parse all the rows received in the rowset
133
// The index we are passing will keep track of which row in the rowset we are parsing
134
// so we can reuse the columnDataPosition/Length/IsNull arrays.
135
while (netCursor_.calculateColumnOffsetsForRow_(row, true)) {
136             rowsReceivedInCurrentRowset_++;
137             row++;
138         }
139
140         // if rowset is not complete and an endqryrm was received, will skip the while loop
141
// and go to the checkAndThrow method. otherwise flow an cntqry to try to complete
142
// the rowset.
143
// -- there is no need to complete the rowset for rowset cursors. fetching stops when
144
// the end of data is returned or when an error occurs. all successfully fetched rows
145
// are returned to the user. the specific error is not returned until the next fetch.
146
while (rowsReceivedInCurrentRowset_ != fetchSize_ &&
147                 !netCursor_.allRowsReceivedFromServer() && !isRowsetCursor_ &&
148                 sensitivity_ != sensitivity_sensitive_dynamic__ &&
149                 sensitivity_ != sensitivity_sensitive_static__) {
150             flowFetchToCompleteRowset();
151             while (netCursor_.calculateColumnOffsetsForRow_(row, true)) {
152                 rowsReceivedInCurrentRowset_++;
153                 row++;
154             }
155         }
156         checkAndThrowReceivedQueryTerminatingException();
157     }
158
159     public void setFetchSize_(int rows) {
160         // Do not change the fetchSize for Fixed Row Protocol
161
suggestedFetchSize_ = (rows == 0) ? 64 : rows;
162         if (!isFixedRowProtocol) {
163             fetchSize_ = suggestedFetchSize_;
164         }
165     }
166
167     //-----------------------------helper methods---------------------------------
168

169     void flowFetchToCompleteRowset() throws DisconnectException {
170         try {
171             agent_.beginWriteChain(statement_);
172
173             writeScrollableFetch_((generatedSection_ == null) ? statement_.section_ : generatedSection_,
174                     fetchSize_ - rowsReceivedInCurrentRowset_,
175                     scrollOrientation_relative__,
176                     1,
177                     false); // false means do not disard pending
178
// partial row and pending query blocks
179

180             agent_.flow(statement_);
181             readScrollableFetch_();
182             agent_.endReadChain();
183         } catch (SqlException e) {
184             throw new DisconnectException(agent_, e);
185         }
186     }
187
188     void queryDataWasReturnedOnOpen() throws DisconnectException {
189     }
190
191     // ------------------------------- abstract box car methods --------------------------------------
192
public void writeFetch_(Section section) throws SqlException {
193         if (resultSetType_ == java.sql.ResultSet.TYPE_FORWARD_ONLY && fetchSize_ != 0 &&
194                 rowsYetToBeReceivedForRowset_ > 0) {
195             netAgent_.resultSetRequest_.writeFetch(this,
196                     section,
197                     rowsYetToBeReceivedForRowset_);
198         } else {
199             netAgent_.resultSetRequest_.writeFetch(this,
200                     section,
201                     fetchSize_);
202         }
203     }
204
205     public void readFetch_() throws SqlException {
206         netAgent_.resultSetReply_.readFetch(this);
207     }
208
209     public void writeScrollableFetch_(Section section,
210                                       int fetchSize,
211                                       int orientation,
212                                       long rowToFetch,
213                                       boolean resetQueryBlocks) throws SqlException {
214         netAgent_.resultSetRequest_.writeScrollableFetch(this,
215                 section,
216                 fetchSize,
217                 orientation,
218                 rowToFetch,
219                 resetQueryBlocks);
220     }
221
222     // think about splitting out the position cursor stuff from the fetch stuff
223
// use commented out abstract position cursor methods above
224
public void readScrollableFetch_() throws SqlException {
225         netAgent_.resultSetReply_.readScrollableFetch(this);
226     }
227
228     public void writePositioningFetch_(Section section,
229                                        int orientation,
230                                        long rowToFetch) throws SqlException {
231         netAgent_.resultSetRequest_.writePositioningFetch(this,
232                 section,
233                 orientation,
234                 rowToFetch);
235     }
236
237     public void readPositioningFetch_() throws SqlException {
238         netAgent_.resultSetReply_.readPositioningFetch(this);
239     }
240
241     public void writeCursorClose_(Section section) throws SqlException {
242         netAgent_.resultSetRequest_.writeCursorClose(this, section);
243     }
244
245     public void readCursorClose_() throws SqlException {
246         netAgent_.resultSetReply_.readCursorClose(this);
247     }
248
249     /**
250      * Method that is invoked by <code>closeX()</code> before the
251      * result set is actually being closed. If QRYCLSIMP is enabled on
252      * the cursor, scan data buffer for end of data (SQL state
253      * 02000). If end of data is received, the result set is closed on
254      * the server.
255      *
256      * @exception SqlException
257      */

258     protected void preClose_() throws SqlException {
259         if (netCursor_.getQryclsimpEnabled()) {
260             netCursor_.scanDataBufferForEndOfData();
261         }
262     }
263 }
264
Popular Tags