KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > drda > DRDAResultSet


1 /*
2
3    Derby - Class org.apache.derby.impl.drda.DRDAResultSet
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.drda;
23
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.ResultSetMetaData JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Types JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.apache.derby.iapi.jdbc.EngineResultSet;
31
32 /**
33     DRDAResultSet holds result set information
34 */

35 class DRDAResultSet
36 {
37     //NOTE!
38
//
39
// Since DRDAResultSets are reused, ALL variables should be set
40
// to their default values in reset().
41

42     // resultSet states are NOT_OPENED and SUSPENDED
43
protected static final int NOT_OPENED = 1;
44     protected static final int SUSPENDED = 2;
45     public static final int QRYCLSIMP_DEFAULT = CodePoint.QRYCLSIMP_NO;
46     
47     boolean explicitlyClosed = false;
48
49     int state;
50     protected boolean hasdata = true;
51     protected int[] rsLens; // result length for each column
52
private int[] rsDRDATypes; // DRDA Types of the result set columns
53
private int[] rsPrecision; // result precision for Decimal types
54
private int[] rsScale; // result sale for Decimal types
55

56     protected int [] outovr_drdaType; // Output override DRDA type and length
57

58     protected int withHoldCursor; // hold cursor after commit attribute
59
protected int scrollType = ResultSet.TYPE_FORWARD_ONLY; // Sensitive or Insensitive scroll attribute
60
protected int concurType; // Concurency type
61
protected long rowCount; // Number of rows we have processed
62
private ResultSet JavaDoc rs; // Current ResultSet
63

64     protected int blksize; // Query block size
65
protected int maxblkext; // Maximum number of extra blocks
66
protected int outovropt; // Output Override option
67
protected int qryclsimp; // Implicit Query Close Setting
68
protected boolean qryrelscr; // Query relative scrolling
69
protected long qryrownbr; // Query row number
70
protected boolean qryrfrtbl; // Query refresh answer set table
71
protected int qryscrorn; // Query scroll orientation
72
protected boolean qryrowsns; // Query row sensitivity
73
protected boolean qryblkrst; // Query block reset
74
protected boolean qryrtndta; // Query returns data
75
protected int qryrowset; // Query row set
76
private int qryprctyp; // Protocol type
77
private boolean gotPrctyp; // save the result, for performance
78
protected int rtnextdta; // Return of EXTDTA option
79
protected int nbrrow; // number of fetch or insert rows
80
protected byte [] rslsetflg; // Result Set Flags
81

82     private ArrayList JavaDoc extDtaObjects; // Arraylist of Blobs and Clobs
83
// Return Values to
84
// send with extdta objects.
85

86     private ArrayList JavaDoc rsExtPositions;
87
88     protected ConsistencyToken pkgcnstkn; // Unique consistency token for ResultSet 0
89

90     // splitQRYDTA is normally null. If it is non-null, it means that
91
// the last QRYDTA response which was sent for this statement was
92
// split according to the LMTBLKPRC protocol, and this array contains
93
// the bytes that didn't fit. These bytes should be the first bytes
94
// emitted in the next QRYDTA response to a CNTQRY request.
95
private byte []splitQRYDTA;
96
97     DRDAResultSet()
98     {
99         state = NOT_OPENED;
100         // Initialize qryclsimp to NO. Only result sets requested by
101
// an OPNQRY command should be implicitly closed. OPNQRY will
102
// set qryclsimp later in setOPNQRYOptions().
103
qryclsimp = CodePoint.QRYCLSIMP_NO;
104     }
105
106     /**
107      * Set result set and initialize type array.
108      *
109      * @param value
110      *
111      */

112
113     protected void setResultSet(ResultSet JavaDoc value) throws SQLException JavaDoc
114     {
115         int numCols;
116         rs = value;
117         gotPrctyp = false;
118         if (value != null)
119         {
120             numCols= rs.getMetaData().getColumnCount();
121             rsDRDATypes = new int[numCols];
122         }
123         explicitlyClosed = false;
124     }
125
126
127     /**
128      * set consistency token for this resultSet
129      *
130      */

131     protected void setPkgcnstkn(ConsistencyToken pkgcnstkn)
132     {
133         this.pkgcnstkn = pkgcnstkn;
134     }
135
136
137     /**
138      *
139      * @return the underlying java.sql.ResultSet
140      */

141     protected ResultSet JavaDoc getResultSet()
142     {
143         return rs;
144     }
145
146     public void setSplitQRYDTA(byte []data)
147     {
148         splitQRYDTA = data;
149     }
150     public byte[]getSplitQRYDTA()
151     {
152         return splitQRYDTA;
153     }
154
155     /**
156      * Set ResultSet DRDA DataTypes
157      * @param value drdaTypes for columns.
158      **/

159     protected void setRsDRDATypes(int [] value)
160     {
161         rsDRDATypes = value;
162
163     }
164
165     /**
166      *@return ResultSet DRDA DataTypes
167      **/

168
169     protected int[] getRsDRDATypes()
170     {
171         // use the given override if it is present
172
if (outovr_drdaType != null)
173             return outovr_drdaType;
174         return rsDRDATypes;
175     }
176
177     /**
178      * set resultset/out parameter precision
179      *
180      * @param index - starting with 1
181      * @param precision
182      */

183     protected void setRsPrecision(int index, int precision)
184     {
185         if (rsPrecision == null)
186             rsPrecision = new int[rsDRDATypes.length];
187         rsPrecision[index -1] = precision;
188     }
189
190     /**
191      * get resultset /out paramter precision
192      * @param index -starting with 1
193      * @return precision of column
194      */

195     protected int getRsPrecision(int index)
196     {
197         if (rsPrecision == null)
198             return 0;
199         return rsPrecision[index-1];
200     }
201
202     /**
203      * set resultset/out parameter scale
204      *
205      * @param index - starting with 1
206      * @param scale
207      */

208     protected void setRsScale(int index, int scale)
209     {
210         if (rsScale == null)
211             rsScale = new int[rsDRDATypes.length];
212         rsScale[index-1] = scale;
213     }
214
215     /**
216      * get resultset /out paramter scale
217      * @param index -starting with 1
218      * @return scale of column
219      */

220     protected int getRsScale(int index)
221     {
222         if (rsScale == null)
223             return 0;
224         
225         return rsScale[index -1];
226     }
227     
228     
229     /**
230      * set resultset/out parameter DRDAType
231      *
232      * @param index - starting with 1
233      * @param type
234      */

235     protected void setRsDRDAType(int index, int type)
236     {
237         rsDRDATypes[index -1] = type;
238         
239     }
240     
241     /**
242      * get resultset/out parameter DRDAType
243      *
244      * @param index - starting with 1
245      * @return DRDA Type of column
246      */

247     protected int getRsDRDAType(int index)
248     {
249         return rsDRDATypes[index -1];
250     }
251     
252
253     /**
254      * set resultset DRDA Len
255      *
256      * @param index - starting with 1
257      * @param value
258      */

259     protected void setRsLen(int index, int value)
260     {
261         if (rsLens == null)
262             rsLens = new int[rsDRDATypes.length];
263         rsLens[index -1] = value;
264         
265     }
266     
267     /**
268      * get resultset DRDALen
269      * @param index - starting with 1
270      * @return length of column value
271      */

272     protected int getRsLen(int index)
273     {
274         return rsLens[index -1];
275     }
276     
277
278     /**
279      * Add extDtaObject
280      * @param o - object to add
281      */

282     protected void addExtDtaObject (Object JavaDoc o, int jdbcIndex )
283     {
284         if (extDtaObjects == null)
285             extDtaObjects = new java.util.ArrayList JavaDoc();
286         extDtaObjects.add (o);
287
288         if (rsExtPositions == null)
289             rsExtPositions = new java.util.ArrayList JavaDoc();
290         
291         // need to record the 0 based position so subtract 1
292
rsExtPositions.add (new Integer JavaDoc(jdbcIndex -1 ));
293
294     }
295
296
297     /**
298      * Clear externalized lob objects in current result set
299      */

300     protected void clearExtDtaObjects ()
301     {
302         if (extDtaObjects != null)
303             extDtaObjects.clear();
304         if (rsExtPositions != null)
305             rsExtPositions.clear();
306         
307     }
308     
309     /*
310      * Is lob object nullable
311      * @param index - offset starting with 0
312      * @return true if object is nullable
313      */

314     protected boolean isExtDtaValueNullable(int index)
315     {
316         if ((rsExtPositions == null) ||
317             rsExtPositions.get(index) == null)
318             return false;
319         
320
321         int colnum = ((Integer JavaDoc) rsExtPositions.get(index)).intValue();
322         
323         if (FdocaConstants.isNullable((getRsDRDATypes())[colnum]))
324             return true;
325         else
326             return false;
327     }
328     
329
330     /**
331      * Get the extData Objects
332      *
333      * @return ArrayList with extdta
334      */

335     protected ArrayList JavaDoc getExtDtaObjects()
336     {
337         return extDtaObjects;
338     }
339
340     /**
341      * Set the extData Objects
342      */

343     protected void setExtDtaObjects(ArrayList JavaDoc a)
344     {
345         extDtaObjects =a;
346     }
347     
348     
349     /**
350      * This method closes the JDBC objects and frees up all references held by
351      * this object.
352      *
353      * @throws SQLException
354      */

355     protected void close() throws SQLException JavaDoc
356     {
357         if (rs != null)
358             rs.close();
359         rs = null;
360         outovr_drdaType = null;
361         rsLens = null;
362         rsDRDATypes = null;
363         rsPrecision = null;
364         rsScale = null;
365         extDtaObjects = null;
366         splitQRYDTA = null;
367         rsExtPositions = null;
368     }
369     
370     /**
371      * This method resets the state of this DRDAResultset object so that it can
372      * be re-used. This method should reset all variables of this class.
373      *
374      */

375     protected void reset() {
376         explicitlyClosed = false;
377         state = NOT_OPENED;
378         hasdata = true;
379         rsLens = null;
380         rsDRDATypes = null;
381         rsPrecision = null;
382         rsScale = null;
383         
384         outovr_drdaType = null;
385         
386         withHoldCursor = 0;
387         scrollType = ResultSet.TYPE_FORWARD_ONLY;
388         concurType = 0;
389         rowCount = 0;
390         rs = null;
391         
392         blksize = 0;
393         maxblkext = 0;
394         outovropt = 0;
395         qryclsimp = CodePoint.QRYCLSIMP_NO;
396         qryrelscr = false;
397         qryrownbr = 0;
398         qryrfrtbl = false;
399         qryscrorn = 0;
400         qryrowsns = false;
401         qryblkrst = false;
402         qryrtndta = false;
403         qryrowset = 0;
404         qryprctyp = 0;
405         gotPrctyp = false;
406         rtnextdta = 0;
407         nbrrow = 0;
408         rslsetflg = null;
409
410         extDtaObjects = null;
411         rsExtPositions = null;
412         pkgcnstkn = null;
413         splitQRYDTA = null;
414     }
415
416
417     /**
418      * Explicitly close the result set by CLSQRY
419      * needed to check for double close.
420      */

421     protected void CLSQRY()
422     {
423         explicitlyClosed = true;
424     }
425
426     /*
427      * @return whether CLSQRY has been called on the
428      * current result set.
429      */

430     protected boolean wasExplicitlyClosed()
431     {
432         return explicitlyClosed;
433     }
434
435
436     /****
437      * Check to see if the result set for this statement
438      * has at least one column that is BLOB/CLOB.
439      * @return True if the result has at least one blob/clob
440      * column; false otherwise.
441      ****/

442  
443     protected boolean hasLobColumns() throws SQLException JavaDoc
444     {
445         ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
446         int ncols = rsmd.getColumnCount();
447         for (int i = 1; i <= ncols; i++)
448         {
449             int type = rsmd.getColumnType(i);
450             if (type == Types.BLOB || type == Types.CLOB)
451                 return true;
452         }
453         return false;
454     }
455
456     /**
457      * Get the cursor name for the ResultSet
458      */

459     public String JavaDoc getResultSetCursorName() throws SQLException JavaDoc
460     {
461
462         if (rs != null)
463             return rs.getCursorName();
464         else
465             return null;
466     }
467
468     protected int getQryprctyp()
469         throws SQLException JavaDoc
470     {
471         if (!gotPrctyp && qryprctyp == CodePoint.LMTBLKPRC)
472         {
473             gotPrctyp = true;
474             if (rs == null || ((EngineResultSet)rs).isForUpdate() ||
475                 /* for now we are not supporting LOB under LMTBLKPRC. drda spec only
476                  * disallows LOB under LMTBLKPRC if OUTOVR is also for ANY CNTQRY reply.
477                  * To support LOB, QRYDTA protocols for LOB will need to be changed.
478                  */

479                 hasLobColumns())
480             {
481                 qryprctyp = CodePoint.FIXROWPRC;
482             }
483         }
484         return qryprctyp;
485     }
486
487     protected void setQryprctyp(int qryprctyp)
488     {
489         this.qryprctyp = qryprctyp;
490     }
491
492     /**
493      * is ResultSet closed
494      * @return whether the resultSet is closed
495      */

496     protected boolean isClosed()
497     {
498         return (state == NOT_OPENED);
499     }
500
501     /**
502      * Set state to SUSPENDED (result set is opened)
503      */

504     protected void suspend()
505     {
506         state = SUSPENDED;
507     }
508
509
510     protected String JavaDoc toDebugString(String JavaDoc indent)
511     {
512         String JavaDoc s = indent + "***** DRDASResultSet toDebugString ******\n";
513         s += indent + "State:" + getStateString(state)+ "\n";
514         s += indent + "pkgcnstkn: {" + pkgcnstkn + "}\n";
515         s += indent + "cursor Name: ";
516         String JavaDoc cursorName = null;
517         try {
518             if (rs != null)
519                 cursorName = rs.getCursorName();
520         }
521         catch (SQLException JavaDoc se )
522         {
523             cursorName = "invalid rs";
524         }
525         s += indent + cursorName + "\n";
526            
527         return s;
528     }
529
530
531     private String JavaDoc getStateString( int i )
532     {
533         switch (i)
534         {
535             case NOT_OPENED:
536                 return "NOT_OPENED";
537             case SUSPENDED:
538                 return "SUSPENDED";
539             default:
540                 return "UNKNOWN_STATE";
541         }
542
543     }
544     
545     /**
546      * Sets the OPNQRYOptions. For more information on the meaning of these
547      * values consult the DRDA Technical Standard document.
548      *
549      * @param blksize Query block Size
550      * @param qryblkctl Use to set the query protocol type
551      * @param maxblkext Maximum number of extra blocks
552      * @param outovropt Output override option
553      * @param qryrowset Query row set
554      * @param qryclsimpl Implicit query close setting
555      */

556     protected void setOPNQRYOptions(int blksize, int qryblkctl,
557             int maxblkext, int outovropt,int qryrowset,int qryclsimpl)
558     {
559         this.blksize = blksize;
560         setQryprctyp(qryblkctl);
561         this.maxblkext = maxblkext;
562         this.outovropt = outovropt;
563         this.qryrowset = qryrowset;
564         this.qryclsimp = (qryclsimpl == CodePoint.QRYCLSIMP_SERVER_CHOICE)
565             ? DRDAResultSet.QRYCLSIMP_DEFAULT : qryclsimpl;
566
567         // Assume that we are returning data until a CNTQRY command
568
// tells us otherwise. (DERBY-822)
569
qryrtndta = true;
570
571         // For scrollable result sets, we don't know the fetch
572
// orientation until we get a CNTQRY command. Set orientation
573
// and row number to make pre-fetching possible. (DERBY-822)
574
qryscrorn = CodePoint.QRYSCRREL;
575         qryrownbr = 1;
576     }
577 }
578
Popular Tags