KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.client.net.NetResultSetReply
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.DisconnectException;
25 import org.apache.derby.client.am.ResultSet;
26 import org.apache.derby.client.am.ResultSetCallbackInterface;
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 import org.apache.derby.shared.common.reference.MessageId;
32
33 public class NetResultSetReply extends NetStatementReply implements ResultSetReplyInterface {
34     public NetResultSetReply(NetAgent netAgent, int bufferSize) {
35         super(netAgent, bufferSize);
36     }
37
38     //----------------------------- entry points ---------------------------------
39

40     public void readFetch(ResultSetCallbackInterface resultSet) throws DisconnectException {
41         startSameIdChainParse();
42         parseCNTQRYreply(resultSet, true); // true means we expect row data
43
endOfSameIdChainData();
44     }
45
46     public void readPositioningFetch(ResultSetCallbackInterface resultSet) throws DisconnectException {
47         startSameIdChainParse();
48         parseCNTQRYreply(resultSet, false); // false means return data is not expected
49
endOfSameIdChainData();
50     }
51
52     public void readScrollableFetch(ResultSetCallbackInterface resultSet) throws DisconnectException {
53         startSameIdChainParse();
54         parseCNTQRYreply(resultSet, true); // true means return data is expected
55
endOfSameIdChainData();
56     }
57
58     public void readCursorClose(ResultSetCallbackInterface resultSet) throws DisconnectException {
59         startSameIdChainParse();
60         parseCLSQRYreply(resultSet);
61         endOfSameIdChainData();
62     }
63
64     //----------------------helper methods----------------------------------------
65

66     //------------------parse reply for specific command--------------------------
67

68     // These methods are "private protected", which is not a recognized java privilege,
69
// but means that these methods are private to this class and to subclasses,
70
// and should not be used as package-wide friendly methods.
71

72     // Parse the reply for the Close Query Command.
73
// This method handles the parsing of all command replies and reply data
74
// for the clsqry command.
75
private void parseCLSQRYreply(ResultSetCallbackInterface resultSet) throws DisconnectException {
76         int peekCP = parseTypdefsOrMgrlvlovrs();
77
78         if (peekCP == CodePoint.SQLCARD) {
79             NetSqlca netSqlca = parseSQLCARD(null); //@f48553sxg - null means rowsetSqlca_ is null
80
// Set the cursor state if null SQLCA or sqlcode is equal to 0.
81
resultSet.completeSqlca(netSqlca);
82         } else {
83             parseCloseError(resultSet);
84         }
85     }
86
87     // Parse the reply for the Continue Query Command.
88
// This method handles the parsing of all command replies and reply data for the cntqry command.
89
// If doCopyQrydta==false, then there is no data, and we're only parsing out the sqlca to get the row count.
90
private void parseCNTQRYreply(ResultSetCallbackInterface resultSetI,
91                                   boolean doCopyQrydta) throws DisconnectException {
92         boolean found = false;
93         int peekCP = peekCodePoint();
94         if (peekCP == CodePoint.RDBUPDRM) {
95             found = true;
96             parseRDBUPDRM();
97             peekCP = peekCodePoint();
98         }
99
100         if (peekCP == CodePoint.QRYDTA) {
101             found = true;
102             if (!doCopyQrydta) {
103                 parseLengthAndMatchCodePoint(CodePoint.QRYDTA);
104                 //we don't need to copy QRYDTA since there is no data
105
if (longValueForDecryption_ != null) {
106                     longValueForDecryption_ = null;
107                 }
108                 if (longBufferForDecryption_ != null) {
109                     longBufferForDecryption_ = null;
110                 }
111
112                 int ddmLength = getDdmLength();
113                 ensureBLayerDataInBuffer(ddmLength);
114                 ((ResultSet) resultSetI).expandRowsetSqlca();
115                 NetSqlca sqlca = parseSQLCARDrow(((ResultSet) resultSetI).rowsetSqlca_);
116                 int daNullIndicator = readFastByte();
117                 adjustLengths(getDdmLength());
118                 // define event interface and use the event method
119
// only get the rowCount_ if sqlca is not null and rowCount_ is unknown
120
if (sqlca != null && sqlca.containsSqlcax()) {
121                     ((ResultSet) resultSetI).setRowCountEvent(sqlca.getRowCount(netAgent_.targetTypdef_));
122                 }
123
124                 peekCP = peekCodePoint();
125                 if (peekCP == CodePoint.RDBUPDRM) {
126                     parseRDBUPDRM();
127                     peekCP = peekCodePoint();
128                 }
129                 return;
130             }
131             do {
132                 parseQRYDTA((NetResultSet) resultSetI);
133                 peekCP = peekCodePoint();
134             } while (peekCP == CodePoint.QRYDTA);
135         }
136
137         if (peekCP == CodePoint.EXTDTA) {
138             found = true;
139             do {
140                 copyEXTDTA((NetCursor) ((ResultSet) resultSetI).cursor_);
141                 if (longBufferForDecryption_ != null) {//encrypted EXTDTA
142
buffer_ = longBufferForDecryption_;
143                     pos_ = longPosForDecryption_;
144                     if (longBufferForDecryption_ != null && count_ > longBufferForDecryption_.length) {
145                         count_ = longBufferForDecryption_.length;
146                     }
147                 }
148
149                 peekCP = peekCodePoint();
150             } while (peekCP == CodePoint.EXTDTA);
151         }
152
153         if (peekCP == CodePoint.SQLCARD) {
154             found = true;
155             ((ResultSet) resultSetI).expandRowsetSqlca();
156             NetSqlca netSqlca = parseSQLCARD(((ResultSet) resultSetI).rowsetSqlca_);
157             // for an atomic operation, the SQLCA contains the sqlcode for the first (statement
158
// terminating)error, the last warning, or zero. all multi-row fetch operatons are
159
// atomic. (the only operation that is not atomic is multi-row insert).
160
if (((ResultSet) resultSetI).sensitivity_ != ResultSet.sensitivity_sensitive_dynamic__) {
161                 if (netSqlca != null && netSqlca.containsSqlcax() && netSqlca.getRowsetRowCount() == 0) {
162                     ((ResultSet) resultSetI).setRowCountEvent(netSqlca.getRowCount(netAgent_.targetTypdef_));
163                 }
164             }
165             resultSetI.completeSqlca(netSqlca);
166             peekCP = peekCodePoint();
167         }
168
169         if (peekCP == CodePoint.ENDQRYRM) {
170             found = true;
171             parseEndQuery(resultSetI);
172             peekCP = peekCodePoint();
173         }
174
175         if (peekCP == CodePoint.RDBUPDRM) {
176             found = true;
177             parseRDBUPDRM();
178         }
179
180         if (!found) {
181             parseFetchError(resultSetI);
182         }
183
184         if (longBufferForDecryption_ != null) {
185             // Not a good idea to create a new buffer_
186
buffer_ = new byte[DEFAULT_BUFFER_SIZE];
187             longBufferForDecryption_ = null;
188         }
189     }
190
191     void parseCloseError(ResultSetCallbackInterface resultSetI) throws DisconnectException {
192         int peekCP = peekCodePoint();
193         switch (peekCP) {
194         case CodePoint.ABNUOWRM:
195             {
196                 //passing the ResultSetCallbackInterface implementation will
197
//help in retrieving the the UnitOfWorkListener that needs to
198
//be rolled back
199
NetSqlca sqlca = parseAbnormalEndUow(resultSetI);
200                 resultSetI.completeSqlca(sqlca);
201                 break;
202             }
203         case CodePoint.CMDCHKRM:
204             parseCMDCHKRM();
205             break;
206         case CodePoint.QRYNOPRM:
207             parseQRYNOPRM(resultSetI);
208             break;
209         case CodePoint.RDBNACRM:
210             parseRDBNACRM();
211             break;
212         default:
213             parseCommonError(peekCP);
214         }
215     }
216
217     void parseFetchError(ResultSetCallbackInterface resultSetI) throws DisconnectException {
218         int peekCP = peekCodePoint();
219         switch (peekCP) {
220         case CodePoint.ABNUOWRM:
221             {
222                 //passing the ResultSetCallbackInterface implementation will
223
//help in retrieving the the UnitOfWorkListener that needs to
224
//be rolled back
225
NetSqlca sqlca = parseAbnormalEndUow(resultSetI);
226                 resultSetI.completeSqlca(sqlca);
227                 break;
228             }
229         case CodePoint.CMDCHKRM:
230             parseCMDCHKRM();
231             break;
232         case CodePoint.CMDNSPRM:
233             parseCMDNSPRM();
234             break;
235         case CodePoint.QRYNOPRM:
236             parseQRYNOPRM(resultSetI);
237             break;
238         case CodePoint.RDBNACRM:
239             parseRDBNACRM();
240             break;
241         default:
242             parseCommonError(peekCP);
243         }
244     }
245
246     //-----------------------------parse DDM Reply Messages-----------------------
247

248     // Query Not Opened Reply Message is issued if a CNTQRY or CLSQRY
249
// command is issued for a query that is not open. A previous
250
// ENDQRYRM, ENDUOWRM, or ABNUOWRM reply message might have
251
// terminated the command.
252
// PROTOCOL architects the SQLSTATE value depending on SVRCOD
253
// SVRCOD 4 -> SQLSTATE is 24501
254
// SVRCOD 8 -> SQLSTATE of 58008 or 58009
255
//
256
// if SVRCOD is 4 then SQLSTATE 24501, SQLCODE -501
257
// else SQLSTATE 58009, SQLCODE -30020
258
//
259
// Messages
260
// SQLSTATE : 24501
261
// The identified cursor is not open.
262
// SQLCODE : -501
263
// The cursor specified in a FETCH or CLOSE statement is not open.
264
// The statement cannot be processed.
265
// SQLSTATE : 58009
266
// Execution failed due to a distribution protocol error that caused deallocation of the conversation.
267
// SQLCODE : -30020
268
// Execution failed because of a Distributed Protocol
269
// Error that will affect the successful execution of subsequent
270
// commands and SQL statements: Reason Code <reason-code>.
271
// Some possible reason codes include:
272
// 121C Indicates that the user is not authorized to perform the requested command.
273
// 1232 The command could not be completed because of a permanent error.
274
// In most cases, the server will be in the process of an abend.
275
// 220A The target server has received an invalid data description.
276
// If a user SQLDA is specified, ensure that the fields are
277
// initialized correctly. Also, ensure that the length does not exceed
278
// the maximum allowed length for the data type being used.
279
//
280
// The command or statement cannot be processed. The current
281
// transaction is rolled back and the application is disconnected
282
// from the remote database.
283
//
284
// Returned from Server:
285
// SVRCOD - required (4 - WARNING, 8 - ERROR)
286
// RDBNAM - required
287
// PKGNAMCSN - required
288
//
289
private void parseQRYNOPRM(ResultSetCallbackInterface resultSet) throws DisconnectException {
290         boolean svrcodReceived = false;
291         int svrcod = CodePoint.SVRCOD_INFO;
292         boolean rdbnamReceived = false;
293         String JavaDoc rdbnam = null;
294         boolean pkgnamcsnReceived = false;
295         Object JavaDoc pkgnamcsn = null;
296
297         parseLengthAndMatchCodePoint(CodePoint.QRYNOPRM);
298         pushLengthOnCollectionStack();
299         int peekCP = peekCodePoint();
300
301         while (peekCP != Reply.END_OF_COLLECTION) {
302
303             boolean foundInPass = false;
304
305             if (peekCP == CodePoint.SVRCOD) {
306                 foundInPass = true;
307                 svrcodReceived = checkAndGetReceivedFlag(svrcodReceived);
308                 svrcod = parseSVRCOD(CodePoint.SVRCOD_WARNING, CodePoint.SVRCOD_ERROR);
309                 peekCP = peekCodePoint();
310             }
311
312             if (peekCP == CodePoint.RDBNAM) {
313                 foundInPass = true;
314                 rdbnamReceived = checkAndGetReceivedFlag(rdbnamReceived);
315                 rdbnam = parseRDBNAM(true);
316                 peekCP = peekCodePoint();
317             }
318
319             if (peekCP == CodePoint.PKGNAMCSN) {
320                 foundInPass = true;
321                 pkgnamcsnReceived = checkAndGetReceivedFlag(pkgnamcsnReceived);
322                 pkgnamcsn = parsePKGNAMCSN(true);
323                 peekCP = peekCodePoint();
324             }
325
326             if (!foundInPass) {
327                 doPrmnsprmSemantics(peekCP);
328             }
329
330         }
331         popCollectionStack();
332         checkRequiredObjects(svrcodReceived, rdbnamReceived, pkgnamcsnReceived);
333
334         // move into a method
335
netAgent_.setSvrcod(svrcod);
336         if (svrcod == CodePoint.SVRCOD_WARNING) {
337             netAgent_.accumulateReadException(new SqlException(netAgent_.logWriter_,
338                 new ClientMessageId(SQLState.DRDA_CURSOR_NOT_OPEN)));
339         } else {
340             agent_.accumulateChainBreakingReadExceptionAndThrow(new DisconnectException(agent_,
341                 new ClientMessageId(SQLState.DRDA_CONNECTION_TERMINATED),
342                     SqlException.getMessageUtil().
343                     getTextMessage(MessageId.CONN_CURSOR_NOT_OPEN)));
344         }
345     }
346 }
347
348
Popular Tags