1 | /* |
2 | |
3 | Derby - Class org.apache.derby.client.net.NetResultSetReply |
4 | |
5 | Copyright (c) 2001, 2005 The Apache Software Foundation or its licensors, where applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.client.net; |
22 | |
23 | import org.apache.derby.client.am.DisconnectException; |
24 | import org.apache.derby.client.am.ResultSet; |
25 | import org.apache.derby.client.am.ResultSetCallbackInterface; |
26 | import org.apache.derby.client.am.SqlException; |
27 | import org.apache.derby.client.am.ClientMessageId; |
28 | |
29 | import org.apache.derby.shared.common.reference.SQLState; |
30 | import org.apache.derby.shared.common.reference.MessageId; |
31 | |
32 | public class NetResultSetReply extends NetStatementReply implements ResultSetReplyInterface { |
33 | public NetResultSetReply(NetAgent netAgent, int bufferSize) { |
34 | super(netAgent, bufferSize); |
35 | } |
36 | |
37 | //----------------------------- entry points --------------------------------- |
38 | |
39 | public void readFetch(ResultSetCallbackInterface resultSet) throws DisconnectException { |
40 | startSameIdChainParse(); |
41 | parseCNTQRYreply(resultSet, true); // true means we expect row data |
42 | endOfSameIdChainData(); |
43 | } |
44 | |
45 | public void readPositioningFetch(ResultSetCallbackInterface resultSet) throws DisconnectException { |
46 | startSameIdChainParse(); |
47 | parseCNTQRYreply(resultSet, false); // false means return data is not expected |
48 | endOfSameIdChainData(); |
49 | } |
50 | |
51 | public void readScrollableFetch(ResultSetCallbackInterface resultSet) throws DisconnectException { |
52 | startSameIdChainParse(); |
53 | parseCNTQRYreply(resultSet, true); // true means return data is expected |
54 | endOfSameIdChainData(); |
55 | } |
56 | |
57 | public void readCursorClose(ResultSetCallbackInterface resultSet) throws DisconnectException { |
58 | startSameIdChainParse(); |
59 | parseCLSQRYreply(resultSet); |
60 | endOfSameIdChainData(); |
61 | } |
62 | |
63 | //----------------------helper methods---------------------------------------- |
64 | |
65 | //------------------parse reply for specific command-------------------------- |
66 | |
67 | // These methods are "private protected", which is not a recognized java privilege, |
68 | // but means that these methods are private to this class and to subclasses, |
69 | // and should not be used as package-wide friendly methods. |
70 | |
71 | // Parse the reply for the Close Query Command. |
72 | // This method handles the parsing of all command replies and reply data |
73 | // for the clsqry command. |
74 | private void parseCLSQRYreply(ResultSetCallbackInterface resultSet) throws DisconnectException { |
75 | int peekCP = parseTypdefsOrMgrlvlovrs(); |
76 | |
77 | if (peekCP == CodePoint.SQLCARD) { |
78 | NetSqlca netSqlca = parseSQLCARD(null); //@f48553sxg - null means rowsetSqlca_ is null |
79 | // Set the cursor state if null SQLCA or sqlcode is equal to 0. |
80 | resultSet.completeSqlca(netSqlca); |
81 | } else { |
82 | parseCloseError(resultSet); |
83 | } |
84 | } |
85 | |
86 | // Parse the reply for the Continue Query Command. |
87 | // This method handles the parsing of all command replies and reply data for the cntqry command. |
88 | // If doCopyQrydta==false, then there is no data, and we're only parsing out the sqlca to get the row count. |
89 | private void parseCNTQRYreply(ResultSetCallbackInterface resultSetI, |
90 | boolean doCopyQrydta) throws DisconnectException { |
91 | boolean found = false; |
92 | int peekCP = peekCodePoint(); |
93 | if (peekCP == CodePoint.RDBUPDRM) { |
94 | found = true; |
95 | parseRDBUPDRM(); |
96 | peekCP = peekCodePoint(); |
97 | } |
98 | |
99 | if (peekCP == CodePoint.QRYDTA) { |
100 | found = true; |
101 | if (!doCopyQrydta) { |
102 | parseLengthAndMatchCodePoint(CodePoint.QRYDTA); |
103 | //we don't need to copy QRYDTA since there is no data |
104 | if (longValueForDecryption_ != null) { |
105 | longValueForDecryption_ = null; |
106 | } |
107 | if (longBufferForDecryption_ != null) { |
108 | longBufferForDecryption_ = null; |
109 | } |
110 | |
111 | int ddmLength = getDdmLength(); |
112 | ensureBLayerDataInBuffer(ddmLength); |
113 | ((ResultSet) resultSetI).expandRowsetSqlca(); |
114 | NetSqlca sqlca = parseSQLCARDrow(((ResultSet) resultSetI).rowsetSqlca_); |
115 | int daNullIndicator = readFastByte(); |
116 | adjustLengths(getDdmLength()); |
117 | // define event interface and use the event method |
118 | // only get the rowCount_ if sqlca is not null and rowCount_ is unknown |
119 | if (sqlca != null && sqlca.containsSqlcax()) { |
120 | ((ResultSet) resultSetI).setRowCountEvent(sqlca.getRowCount(netAgent_.targetTypdef_)); |
121 | } |
122 | |
123 | peekCP = peekCodePoint(); |
124 | if (peekCP == CodePoint.RDBUPDRM) { |
125 | parseRDBUPDRM(); |
126 | peekCP = peekCodePoint(); |
127 | } |
128 | return; |
129 | } |
130 | do { |
131 | parseQRYDTA((NetResultSet) resultSetI); |
132 | peekCP = peekCodePoint(); |
133 | } while (peekCP == CodePoint.QRYDTA); |
134 | } |
135 | |
136 | if (peekCP == CodePoint.EXTDTA) { |
137 | found = true; |
138 | do { |
139 | copyEXTDTA((NetCursor) ((ResultSet) resultSetI).cursor_); |
140 | if (longBufferForDecryption_ != null) {//encrypted EXTDTA |
141 | buffer_ = longBufferForDecryption_; |
142 | pos_ = longPosForDecryption_; |
143 | if (longBufferForDecryption_ != null && count_ > longBufferForDecryption_.length) { |
144 | count_ = longBufferForDecryption_.length; |
145 | } |
146 | } |
147 | |
148 | peekCP = peekCodePoint(); |
149 | } while (peekCP == CodePoint.EXTDTA); |
150 | } |
151 | |
152 | if (peekCP == CodePoint.SQLCARD) { |
153 | found = true; |
154 | ((ResultSet) resultSetI).expandRowsetSqlca(); |
155 | NetSqlca netSqlca = parseSQLCARD(((ResultSet) resultSetI).rowsetSqlca_); |
156 | // for an atomic operation, the SQLCA contains the sqlcode for the first (statement |
157 | // terminating)error, the last warning, or zero. all multi-row fetch operatons are |
158 | // atomic. (the only operation that is not atomic is multi-row insert). |
159 | if (((ResultSet) resultSetI).sensitivity_ != ResultSet.sensitivity_sensitive_dynamic__) { |
160 | if (netSqlca != null && netSqlca.containsSqlcax() && netSqlca.getRowsetRowCount() == 0) { |
161 | ((ResultSet) resultSetI).setRowCountEvent(netSqlca.getRowCount(netAgent_.targetTypdef_)); |
162 | } |
163 | } |
164 | resultSetI.completeSqlca(netSqlca); |
165 | peekCP = peekCodePoint(); |
166 | } |
167 | |
168 | if (peekCP == CodePoint.ENDQRYRM) { |
169 | found = true; |
170 | parseEndQuery(resultSetI); |
171 | peekCP = peekCodePoint(); |
172 | } |
173 | |
174 | if (peekCP == CodePoint.RDBUPDRM) { |
175 | found = true; |
176 | parseRDBUPDRM(); |
177 | } |
178 | |
179 | if (!found) { |
180 | parseFetchError(resultSetI); |
181 | } |
182 | |
183 | if (longBufferForDecryption_ != null) { |
184 | // Not a good idea to create a new buffer_ |
185 | buffer_ = new byte[DEFAULT_BUFFER_SIZE]; |
186 | longBufferForDecryption_ = null; |
187 | } |
188 | } |
189 | |
190 | void parseCloseError(ResultSetCallbackInterface resultSetI) throws DisconnectException { |
191 | int peekCP = peekCodePoint(); |
192 | switch (peekCP) { |
193 | case CodePoint.ABNUOWRM: |
194 | { |
195 | NetSqlca sqlca = parseAbnormalEndUow(resultSetI.getConnectionCallbackInterface()); |
196 | resultSetI.completeSqlca(sqlca); |
197 | break; |
198 | } |
199 | case CodePoint.CMDCHKRM: |
200 | parseCMDCHKRM(); |
201 | break; |
202 | case CodePoint.QRYNOPRM: |
203 | parseQRYNOPRM(resultSetI); |
204 | break; |
205 | case CodePoint.RDBNACRM: |
206 | parseRDBNACRM(); |
207 | break; |
208 | default: |
209 | parseCommonError(peekCP); |
210 | } |
211 | } |
212 | |
213 | void parseFetchError(ResultSetCallbackInterface resultSetI) throws DisconnectException { |
214 | int peekCP = peekCodePoint(); |
215 | switch (peekCP) { |
216 | case CodePoint.ABNUOWRM: |
217 | { |
218 | NetSqlca sqlca = parseAbnormalEndUow(resultSetI.getConnectionCallbackInterface()); |
219 | resultSetI.completeSqlca(sqlca); |
220 | break; |
221 | } |
222 | case CodePoint.CMDCHKRM: |
223 | parseCMDCHKRM(); |
224 | break; |
225 | case CodePoint.CMDNSPRM: |
226 | parseCMDNSPRM(); |
227 | break; |
228 | case CodePoint.QRYNOPRM: |
229 | parseQRYNOPRM(resultSetI); |
230 | break; |
231 | case CodePoint.RDBNACRM: |
232 | parseRDBNACRM(); |
233 | break; |
234 | default: |
235 | parseCommonError(peekCP); |
236 | } |
237 | } |
238 | |
239 | //-----------------------------parse DDM Reply Messages----------------------- |
240 | |
241 | // Query Not Opened Reply Message is issued if a CNTQRY or CLSQRY |
242 | // command is issued for a query that is not open. A previous |
243 | // ENDQRYRM, ENDUOWRM, or ABNUOWRM reply message might have |
244 | // terminated the command. |
245 | // PROTOCOL architects the SQLSTATE value depending on SVRCOD |
246 | // SVRCOD 4 -> SQLSTATE is 24501 |
247 | // SVRCOD 8 -> SQLSTATE of 58008 or 58009 |
248 | // |
249 | // if SVRCOD is 4 then SQLSTATE 24501, SQLCODE -501 |
250 | // else SQLSTATE 58009, SQLCODE -30020 |
251 | // |
252 | // Messages |
253 | // SQLSTATE : 24501 |
254 | // The identified cursor is not open. |
255 | // SQLCODE : -501 |
256 | // The cursor specified in a FETCH or CLOSE statement is not open. |
257 | // The statement cannot be processed. |
258 | // SQLSTATE : 58009 |
259 | // Execution failed due to a distribution protocol error that caused deallocation of the conversation. |
260 | // SQLCODE : -30020 |
261 | // Execution failed because of a Distributed Protocol |
262 | // Error that will affect the successful execution of subsequent |
263 | // commands and SQL statements: Reason Code <reason-code>. |
264 | // Some possible reason codes include: |
265 | // 121C Indicates that the user is not authorized to perform the requested command. |
266 | // 1232 The command could not be completed because of a permanent error. |
267 | // In most cases, the server will be in the process of an abend. |
268 | // 220A The target server has received an invalid data description. |
269 | // If a user SQLDA is specified, ensure that the fields are |
270 | // initialized correctly. Also, ensure that the length does not exceed |
271 | // the maximum allowed length for the data type being used. |
272 | // |
273 | // The command or statement cannot be processed. The current |
274 | // transaction is rolled back and the application is disconnected |
275 | // from the remote database. |
276 | // |
277 | // Returned from Server: |
278 | // SVRCOD - required (4 - WARNING, 8 - ERROR) |
279 | // RDBNAM - required |
280 | // PKGNAMCSN - required |
281 | // |
282 | private void parseQRYNOPRM(ResultSetCallbackInterface resultSet) throws DisconnectException { |
283 | boolean svrcodReceived = false; |
284 | int svrcod = CodePoint.SVRCOD_INFO; |
285 | boolean rdbnamReceived = false; |
286 | String rdbnam = null; |
287 | boolean pkgnamcsnReceived = false; |
288 | Object pkgnamcsn = null; |
289 | |
290 | parseLengthAndMatchCodePoint(CodePoint.QRYNOPRM); |
291 | pushLengthOnCollectionStack(); |
292 | int peekCP = peekCodePoint(); |
293 | |
294 | while (peekCP != Reply.END_OF_COLLECTION) { |
295 | |
296 | boolean foundInPass = false; |
297 | |
298 | if (peekCP == CodePoint.SVRCOD) { |
299 | foundInPass = true; |
300 | svrcodReceived = checkAndGetReceivedFlag(svrcodReceived); |
301 | svrcod = parseSVRCOD(CodePoint.SVRCOD_WARNING, CodePoint.SVRCOD_ERROR); |
302 | peekCP = peekCodePoint(); |
303 | } |
304 | |
305 | if (peekCP == CodePoint.RDBNAM) { |
306 | foundInPass = true; |
307 | rdbnamReceived = checkAndGetReceivedFlag(rdbnamReceived); |
308 | rdbnam = parseRDBNAM(true); |
309 | peekCP = peekCodePoint(); |
310 | } |
311 | |
312 | if (peekCP == CodePoint.PKGNAMCSN) { |
313 | foundInPass = true; |
314 | pkgnamcsnReceived = checkAndGetReceivedFlag(pkgnamcsnReceived); |
315 | pkgnamcsn = parsePKGNAMCSN(true); |
316 | peekCP = peekCodePoint(); |
317 | } |
318 | |
319 | if (!foundInPass) { |
320 | doPrmnsprmSemantics(peekCP); |
321 | } |
322 | |
323 | } |
324 | popCollectionStack(); |
325 | checkRequiredObjects(svrcodReceived, rdbnamReceived, pkgnamcsnReceived); |
326 | |
327 | // move into a method |
328 | netAgent_.setSvrcod(svrcod); |
329 | if (svrcod == CodePoint.SVRCOD_WARNING) { |
330 | netAgent_.accumulateReadException(new SqlException(netAgent_.logWriter_, |
331 | new ClientMessageId(SQLState.DRDA_CURSOR_NOT_OPEN))); |
332 | } else { |
333 | agent_.accumulateChainBreakingReadExceptionAndThrow(new DisconnectException(agent_, |
334 | new ClientMessageId(SQLState.DRDA_CONNECTION_TERMINATED), |
335 | SqlException.getMessageUtil(). |
336 | getTextMessage(MessageId.CONN_CURSOR_NOT_OPEN))); |
337 | } |
338 | } |
339 | } |
340 | |