KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.client.net.NetResultSetRequest
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.client.net;
23
24 import org.apache.derby.client.am.ColumnMetaData;
25 import org.apache.derby.client.am.ResultSet;
26 import org.apache.derby.client.am.Section;
27 import org.apache.derby.client.am.SqlException;
28 import org.apache.derby.client.am.ClientMessageId;
29
30 import org.apache.derby.shared.common.reference.SQLState;
31
32 public class NetResultSetRequest extends NetStatementRequest
33         implements ResultSetRequestInterface {
34     public NetResultSetRequest(NetAgent netAgent, CcsidManager ccsidManager, int bufferSize) {
35         super(netAgent, ccsidManager, bufferSize);
36     }
37
38     //----------------------------- entry points ---------------------------------
39
public void writeFetch(NetResultSet resultSet,
40                            Section section,
41                            int fetchSize) throws SqlException {
42         // - for forward-only cursors we do not send qryrowset on OPNQRY, fetchSize is ignored.
43
// but qryrowset is sent on EXCSQLSTT for a stored procedure call.
44
boolean sendQryrowset =
45                 ((NetStatement) resultSet.statement_.materialStatement_).qryrowsetSentOnOpnqry_;
46
47         boolean sendRtnextdta = false;
48         if (sendQryrowset && resultSet.resultSetType_ == java.sql.ResultSet.TYPE_FORWARD_ONLY &&
49                 ((NetCursor) resultSet.cursor_).hasLobs_) {
50             fetchSize = 1;
51             resultSet.fetchSize_ = 1;
52             sendRtnextdta = true;
53             ((NetCursor) resultSet.cursor_).rtnextrow_ = false;
54         }
55         // if one of the result sets returned from a stored procedure is scrollable,
56
// then we set netStatement_.qryrowsetSentOnOpnqry_ to true even though we didn't really
57
// send a qryrowset on excsqlstt for sqlam >= 7. this is ok for scrollable cursors,
58
// but will cause a problem for forward-only cursors. Because if fetchSize was never
59
// set, we will send qryrowset(0), which will cause a syntaxrm.
60
else if (resultSet.fetchSize_ == 0) {
61             sendQryrowset = false;
62         }
63
64         buildCNTQRY(section,
65                 sendQryrowset,
66                 resultSet.queryInstanceIdentifier_,
67                 fetchSize,
68                 sendRtnextdta);
69
70         buildOUTOVR(resultSet,
71                 resultSet.resultSetMetaData_,
72                 resultSet.firstOutovrBuilt_,
73                 ((NetCursor) resultSet.cursor_).hasLobs_);
74     }
75
76     public void writeScrollableFetch(NetResultSet resultSet,
77                                      Section section,
78                                      int fetchSize,
79                                      int orientation,
80                                      long rowToFetch,
81                                      boolean resetQueryBlocks) throws SqlException {
82         int protocolOrientation = computePROTOCOLOrientation(orientation);
83
84         // - for sensitive-static cursors:
85
// * qryrowset must be sent on opnqry to indicate to the server that the cursor is
86
// going to be used in a scrollable fashion. (sqlam<7)
87
// * if qryrowset is sent on opnqry, then it must be sent on all subsequent cntqry's
88
// - for sensitive-dynamic non-rowset cursors: (we should never be in this case)
89
// * qryrowset is NOT ALLOWED on cntqry's
90
// - for rowset cursors:
91
// * qryrowset is optional. it is ignored on opnqry. if not sent on cntqry,
92
// then the fetch is going fetch next row as opposed to fetch next rowset.
93
boolean sendQryrowset =
94                 (resultSet.isRowsetCursor_ ||
95                 (((NetStatement) resultSet.statement_.materialStatement_).qryrowsetSentOnOpnqry_ &&
96                 (resultSet.sensitivity_ == ResultSet.sensitivity_sensitive_static__ ||
97                 ((NetCursor) resultSet.cursor_).blocking_)));
98
99         buildScrollCNTQRY(protocolOrientation,
100                 rowToFetch,
101                 section,
102                 sendQryrowset,
103                 resultSet.queryInstanceIdentifier_,
104                 fetchSize,
105                 resetQueryBlocks);
106
107         buildOUTOVR(resultSet,
108                 resultSet.resultSetMetaData_,
109                 resultSet.firstOutovrBuilt_,
110                 ((NetCursor) resultSet.cursor_).hasLobs_);
111     }
112
113     public void writePositioningFetch(NetResultSet resultSet,
114                                       Section section,
115                                       int orientation,
116                                       long rowToFetch) throws SqlException {
117         int protocolOrientation = computePROTOCOLOrientation(orientation);
118
119         // do not send qryrowste if the cursor is a non-rowset, sensitive dynamic cursor
120
boolean sendQryrowset =
121                 resultSet.isRowsetCursor_ ||
122                 (((NetStatement) resultSet.statement_.materialStatement_).qryrowsetSentOnOpnqry_ &&
123                 resultSet.sensitivity_ != resultSet.sensitivity_sensitive_dynamic__);
124
125         buildPositioningCNTQRY(protocolOrientation,
126                 rowToFetch,
127                 section,
128                 sendQryrowset,
129                 resultSet.queryInstanceIdentifier_,
130                 resultSet.fetchSize_);
131
132         buildOUTOVR(resultSet,
133                 resultSet.resultSetMetaData_,
134                 resultSet.firstOutovrBuilt_,
135                 ((NetCursor) resultSet.cursor_).hasLobs_);
136     }
137
138     public void writeCursorClose(NetResultSet resultSet,
139                                  Section section) throws SqlException {
140         buildCLSQRY(section,
141                 resultSet.queryInstanceIdentifier_);
142     }
143
144     //----------------------helper methods----------------------------------------
145
// These methods are "private protected", which is not a recognized java privilege,
146
// but means that these methods are private to this class and to subclasses,
147
// and should not be used as package-wide friendly methods.
148

149     private void buildCLSQRY(Section section,
150                              long queryInstanceIdentifier)
151             throws SqlException {
152         createCommand();
153         markLengthBytes(CodePoint.CLSQRY);
154         buildPKGNAMCSN(section);
155         buildQRYINSID(queryInstanceIdentifier);
156         updateLengthBytes();
157     }
158
159     private void buildCNTQRY(Section section,
160                              boolean sendQryrowset,
161                              long queryInstanceIdentifier,
162                              int qryrowsetSize,
163                              boolean sendRtnextdta) throws SqlException {
164         buildCoreCNTQRY(section,
165                 sendQryrowset,
166                 queryInstanceIdentifier,
167                 qryrowsetSize);
168
169         // We will always let RTNEXTDTA default to RTNEXTROW. The only time we need to send
170
// RTNEXTDTA RTNEXTALL is for a stored procedure returned forward-only ResultSet
171
// that has LOB columns. Since there are LOBs in the
172
// ResultSet, no QRYDTA is returned on execute. On the CNTQRY's, we will
173
// send qryrowset(1) and rtnextall.
174
if (sendRtnextdta) {
175             buildRTNEXTDTA(CodePoint.RTNEXTALL);
176         }
177
178
179         updateLengthBytes();
180     }
181
182     // buildCoreCntqry builds the common parameters
183
private void buildCoreCNTQRY(Section section,
184                                  boolean sendQryrowset,
185                                  long queryInstanceIdentifier,
186                                  int qryrowsetSize)
187             throws SqlException {
188         createCommand();
189         markLengthBytes(CodePoint.CNTQRY);
190
191         buildPKGNAMCSN(section); // 1. packageNameAndConsistencyToken
192
buildQRYBLKSZ(); // 2. qryblksz
193

194         // maxblkext (-1) tells the server that the client is capable of receiving any number of query blocks
195
if (sendQryrowset) {
196             buildMAXBLKEXT(-1); // 3. maxblkext
197
}
198
199         // 4. qryinsid
200
buildQRYINSID(queryInstanceIdentifier);
201
202         if (sendQryrowset) {
203             buildQRYROWSET(qryrowsetSize); // 5. qryrowset
204
}
205     }
206
207     // Send CNTQRY to get a new rowset from the target server.
208
private void buildScrollCNTQRY(int scrollOrientation,
209                                    long rowNumber,
210                                    Section section,
211                                    boolean sendQryrowset,
212                                    long queryInstanceIdentifier,
213                                    int qryrowsetSize,
214                                    boolean resetQueryBlocks)
215             throws SqlException {
216         buildCoreCNTQRY(section,
217                 sendQryrowset,
218                 queryInstanceIdentifier,
219                 qryrowsetSize);
220
221         buildQRYSCRORN(scrollOrientation); // qryscrorn
222

223         if (scrollOrientation == CodePoint.QRYSCRABS || scrollOrientation == CodePoint.QRYSCRREL) {
224             buildQRYROWNBR(rowNumber);
225         }
226
227         if (resetQueryBlocks) {
228             buildQRYBLKRST(0xF1); // do reset the rowset
229
} else {
230             buildQRYBLKRST(0xF0); // do not reset the rowset
231
}
232
233         buildQRYRTNDTA(0xF1); // do return data
234

235         updateLengthBytes();
236     }
237
238     // Send CTNQRY to reposition the cursor on the target server.
239
private void buildPositioningCNTQRY(int scrollOrientation,
240                                         long rowNumber,
241                                         Section section,
242                                         boolean sendQryrowset,
243                                         long queryInstanceIdentifier,
244                                         int qryrowsetSize)
245             throws SqlException {
246         createCommand();
247         markLengthBytes(CodePoint.CNTQRY);
248
249         buildPKGNAMCSN(section); // 1. pkgnamcsn
250
buildQRYBLKSZ(); // 2. qryblksz
251

252         buildQRYINSID(queryInstanceIdentifier); // 3. qryinsid
253

254         if (sendQryrowset) {
255             buildQRYROWSET(qryrowsetSize); // 4. qryrowset
256
}
257
258         buildQRYSCRORN(scrollOrientation); // 5. qryscrorn
259

260         if (scrollOrientation == CodePoint.QRYSCRABS || scrollOrientation == CodePoint.QRYSCRREL) {
261             buildQRYROWNBR(rowNumber); // 6. qryrownbr
262
}
263
264         buildQRYBLKRST(0xF1); // 7. do reset the rowset
265
buildQRYRTNDTA(0xF0); // 8. do not return data
266

267
268         updateLengthBytes(); // for cntqry
269
}
270
271     private void buildOUTOVR(ResultSet resultSet,
272                              ColumnMetaData resultSetMetaData,
273                              boolean firstOutovrBuilt,
274                              boolean hasLobs) throws SqlException {
275         return;
276     }
277
278     private void buildRTNEXTDTA(int rtnextdta) throws SqlException {
279         writeScalar1Byte(CodePoint.RTNEXTDTA, rtnextdta);
280     }
281
282     private void buildQRYSCRORN(int scrollOrientation) throws SqlException {
283         writeScalar1Byte(CodePoint.QRYSCRORN, scrollOrientation);
284     }
285
286     private void buildQRYBLKRST(int qryblkrst) throws SqlException {
287         writeScalar1Byte(CodePoint.QRYBLKRST, qryblkrst);
288     }
289
290     private void buildQRYROWNBR(long rowNumber) throws SqlException {
291         writeScalar8Bytes(CodePoint.QRYROWNBR, rowNumber);
292     }
293
294     private void buildQRYRTNDTA(int qryrtndta) throws SqlException {
295         writeScalar1Byte(CodePoint.QRYRTNDTA, qryrtndta);
296     }
297
298     //----------------------non-parsing computational helper methods--------------
299
// These methods are "private protected", which is not a recognized java privilege,
300
// but means that these methods are private to this class and to subclasses,
301
// and should not be used as package-wide friendly methods.
302

303     // Called by NetResultSetRequest.writeScrollableFetch()
304
private int computePROTOCOLOrientation(int orientation) throws SqlException {
305         switch (orientation) {
306         case ResultSet.scrollOrientation_absolute__:
307             return CodePoint.QRYSCRABS;
308
309         case ResultSet.scrollOrientation_after__:
310             return CodePoint.QRYSCRAFT;
311
312         case ResultSet.scrollOrientation_before__:
313             return CodePoint.QRYSCRBEF;
314
315         case ResultSet.scrollOrientation_relative__:
316             return CodePoint.QRYSCRREL;
317
318         default:
319             throw new SqlException(netAgent_.logWriter_,
320                 new ClientMessageId(SQLState.NET_INVALID_SCROLL_ORIENTATION));
321         }
322     }
323
324 }
325
Popular Tags