KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > am > Cursor


1 /*
2
3    Derby - Class org.apache.derby.client.am.Cursor
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.am;
23 import org.apache.derby.shared.common.reference.SQLState;
24
25 import java.sql.SQLException JavaDoc;
26 import java.io.UnsupportedEncodingException JavaDoc;
27
28 // When we calculate column offsets make sure we calculate the correct offsets for double byte charactr5er data
29
// length from server is number of chars, not bytes
30
// Direct byte-level converters are called directly by this class, cross converters are deferred to the CrossConverters class.
31

32 public abstract class Cursor {
33     protected Agent agent_;
34
35     //-----------------------------varchar representations------------------------
36

37     public final static int STRING = 0;
38     public final static int VARIABLE_STRING = 2; // uses a 2-byte length indicator
39
public final static int VARIABLE_SHORT_STRING = 1; // aka Pascal L; uses a 1-byte length indicator
40
public final static int NULL_TERMINATED_STRING = 3;
41
42     public final static int BYTES = 4;
43     public final static int VARIABLE_BYTES = 5;
44     public final static int VARIABLE_SHORT_BYTES = 6;
45     public final static int NULL_TERMINATED_BYTES = 7;
46
47     public final static int SBCS_CLOB = 8;
48     public final static int MBCS_CLOB = 9;
49     public final static int DBCS_CLOB = 10;
50     //-----------------------------internal state---------------------------------
51

52     //-------------Structures for holding and scrolling the data -----------------
53
public byte[] dataBuffer_;
54     public java.io.ByteArrayOutputStream JavaDoc dataBufferStream_;
55     public int position_; // This is the read head
56
public int lastValidBytePosition_;
57     public boolean hasLobs_; // is there at least one LOB column?
58

59     // Current row positioning
60
protected int currentRowPosition_;
61     private int nextRowPosition_;
62     // Let's new up a 2-dimensional array based on fetch-size and reuse so that
63
protected int[] columnDataPosition_;
64
65     // This is the actual, computed lengths of varchar fields, not the max length from query descriptor or DA
66
protected int[] columnDataComputedLength_;
67     // populate this for
68

69     // All the data is in the buffers, but user may not have necessarily stepped to the last row yet.
70
// This flag indicates that the server has returned all the rows, and is positioned
71
// after last, for both scrollable and forward-only cursors.
72
// For singleton cursors, this memeber will be set to true as soon as next is called.
73
private boolean allRowsReceivedFromServer_;
74
75     // Total number of rows read so far.
76
// This should never exceed this.statement.maxRows
77
int rowsRead_;
78
79     // Maximum column size limit in bytes.
80
int maxFieldSize_ = 0;
81
82     // Row positioning for all cached rows
83
// For scrollable result sets, these lists hold the offsets into the cached rowset buffer for each row of data.
84
protected java.util.ArrayList JavaDoc columnDataPositionCache_ = new java.util.ArrayList JavaDoc();
85     protected java.util.ArrayList JavaDoc columnDataLengthCache_ = new java.util.ArrayList JavaDoc();
86     protected java.util.ArrayList JavaDoc columnDataIsNullCache_ = new java.util.ArrayList JavaDoc();
87     public java.util.ArrayList JavaDoc isUpdateDeleteHoleCache_ = new java.util.ArrayList JavaDoc();
88     public boolean isUpdateDeleteHole_;
89
90     // State to keep track of when a row has been updated,
91
// cf. corresponding set and get accessors. Only implemented for
92
// scrollable updatable insensitive result sets for now.
93
private boolean isRowUpdated_;
94
95     final static public java.lang.Boolean JavaDoc ROW_IS_NULL = new Boolean JavaDoc(true);
96     final static public java.lang.Boolean JavaDoc ROW_IS_NOT_NULL = new Boolean JavaDoc(false);
97
98     java.sql.Date JavaDoc recyclableDate_ = null;
99     java.sql.Time JavaDoc recyclableTime_ = null;
100     java.sql.Timestamp JavaDoc recyclableTimestamp_ = null;
101
102     // For the net, this data comes from the query descriptor.
103

104     public int[] jdbcTypes_;
105     public int columns_;
106     public boolean[] nullable_;
107     public String JavaDoc[] charsetName_;
108     public boolean[] isNull_;
109     public int[] fdocaLength_; // this is the max length for
110

111     //----------------------------------------------------------------------------
112

113     public int[] ccsid_;
114     char[] charBuffer_;
115
116     //---------------------constructors/finalizer---------------------------------
117

118     public Cursor(Agent agent) {
119         agent_ = agent;
120         isRowUpdated_ = false;
121         dataBufferStream_ = new java.io.ByteArrayOutputStream JavaDoc();
122     }
123
124     public Cursor(Agent agent, byte[] dataBuffer) {
125         this(agent);
126         dataBuffer_ = dataBuffer;
127         setAllRowsReceivedFromServer(false);
128     }
129
130     public void setNumberOfColumns(int numberOfColumns) {
131         columnDataPosition_ = new int[numberOfColumns];
132         columnDataComputedLength_ = new int[numberOfColumns];
133
134         columns_ = numberOfColumns;
135         nullable_ = new boolean[numberOfColumns];
136         charsetName_ = new String JavaDoc[numberOfColumns];
137
138         ccsid_ = new int[numberOfColumns];
139
140         isNull_ = new boolean[numberOfColumns];
141         jdbcTypes_ = new int[numberOfColumns];
142     }
143
144     /**
145      * Makes the next row the current row. Returns true if the current
146      * row position is a valid row position.
147      *
148      * @param allowServerFetch if false, don't fetch more data from
149      * the server even if more data is needed
150      * @return <code>true</code> if current row position is valid
151      * @exception SqlException if an error occurs
152      */

153     protected boolean stepNext(boolean allowServerFetch) throws SqlException {
154         // local variable usd to hold the returned value from calculateColumnOffsetsForRow()
155
boolean rowPositionIsValid = true;
156
157         // reset lob data
158
// clears out Cursor.lobs_ calculated for the current row when cursor is moved.
159
clearLobData_();
160
161         // mark the start of a new row.
162
makeNextRowPositionCurrent();
163         
164         // Moving out of the hole, set isUpdateDeleteHole to false
165
isUpdateDeleteHole_ = false;
166
167         isRowUpdated_ = false;
168
169         // Drive the CNTQRY outside of calculateColumnOffsetsForRow() if the dataBuffer_
170
// contains no data since it has no abilities to handle replies other than
171
// the QRYDTA, i.e. ENDQRYRM when the result set contains no more rows.
172
while (!dataBufferHasUnprocessedData()) {
173             if (allRowsReceivedFromServer_) {
174                 return false;
175             }
176             getMoreData_();
177         }
178
179         // The parameter passed in here is used as an index into the cached rowset for
180
// scrollable cursors, for the arrays to be reused. It is not used for forward-only
181
// cursors, so just pass in 0.
182
rowPositionIsValid = calculateColumnOffsetsForRow_(0, allowServerFetch);
183         markNextRowPosition();
184         return rowPositionIsValid;
185     }
186
187     /**
188      * Makes the next row the current row. Returns true if the current
189      * row position is a valid row position.
190      *
191      * @return <code>true</code> if current row position is valid
192      * @exception SqlException if an error occurs
193      */

194     public boolean next() throws SqlException {
195         return stepNext(true);
196     }
197
198     //--------------------------reseting cursor state-----------------------------
199

200     /**
201      * Set the value of value of allRowsReceivedFromServer_.
202      *
203      * @param b a <code>boolean</code> value indicating whether all
204      * rows are received from the server
205      */

206     public void setAllRowsReceivedFromServer(boolean b) {
207         allRowsReceivedFromServer_ = b;
208     }
209
210     /**
211      * Return <code>true</code> if all rows are received from the
212      * server.
213      *
214      * @return <code>true</code> if all rows are received from the
215      * server.
216      */

217     public final boolean allRowsReceivedFromServer() {
218         return allRowsReceivedFromServer_;
219     }
220
221     public final boolean currentRowPositionIsEqualToNextRowPosition() {
222         return (currentRowPosition_ == nextRowPosition_);
223     }
224
225     // reset the beginning and ending position in the data buffer to 0
226
// reset the currentRowPosition and nextRowPosition to 0
227
// reset lastRowReached and sqlcode100Received to false
228
// clear the column data offsets cache
229
public final void resetDataBuffer() {
230         position_ = 0;
231         lastValidBytePosition_ = 0;
232         currentRowPosition_ = 0;
233         nextRowPosition_ = 0;
234         setAllRowsReceivedFromServer(false);
235         dataBufferStream_.reset();
236     }
237
238     public final boolean dataBufferHasUnprocessedData() {
239         return (lastValidBytePosition_ - position_) > 0;
240     }
241
242     /**
243      * Calculate the column offsets for a row.
244      *
245      * @param row row index
246      * @param allowServerFetch if true, allow fetching more data from
247      * server
248      * @return <code>true</code> if the current row position is a
249      * valid row position.
250      * @exception SqlException
251      * @exception DisconnectException
252      */

253     protected abstract boolean
254         calculateColumnOffsetsForRow_(int row, boolean allowServerFetch)
255         throws SqlException, DisconnectException;
256
257     protected abstract void clearLobData_();
258
259     protected abstract void getMoreData_() throws SqlException;
260
261     // Associate a new underlying COM or SQLDA output data buffer for this converter.
262
public final void setBuffer(byte[] dataBuffer) {
263         dataBuffer_ = dataBuffer;
264     }
265
266     public final void setIsUpdataDeleteHole(int row, boolean isRowNull) {
267         isUpdateDeleteHole_ = isRowNull;
268         Boolean JavaDoc nullIndicator = (isUpdateDeleteHole_ == true) ? ROW_IS_NULL : ROW_IS_NOT_NULL;
269         if (isUpdateDeleteHoleCache_.size() == row) {
270             isUpdateDeleteHoleCache_.add(nullIndicator);
271         } else {
272             isUpdateDeleteHoleCache_.set(row, nullIndicator);
273         }
274     }
275
276     /**
277      * Keep track of updated status for this row.
278      *
279      * @param isRowUpdated true if row has been updated
280      *
281      * @see Cursor#getIsRowUpdated
282      */

283     public final void setIsRowUpdated(boolean isRowUpdated) {
284         isRowUpdated_ = isRowUpdated;
285     }
286
287     /**
288      * Get updated status for this row.
289      * Minion of ResultSet#rowUpdated.
290      *
291      * @see Cursor#setIsRowUpdated
292      */

293     public final boolean getIsRowUpdated() {
294         return isRowUpdated_;
295     }
296
297     /**
298      * Get deleted status for this row.
299      * Minion of ResultSet#rowDeleted.
300      *
301      * @see Cursor#setIsUpdataDeleteHole
302      */

303     public final boolean getIsUpdateDeleteHole() {
304         return isUpdateDeleteHole_;
305     }
306     
307     //---------------------------cursor positioning-------------------------------
308

309     final int getPosition() {
310         return position_;
311     }
312
313     final void setPosition(int newPosition) {
314         position_ = newPosition;
315     }
316
317     public final void markCurrentRowPosition() {
318         currentRowPosition_ = position_;
319     }
320
321     public final void markNextRowPosition() {
322         nextRowPosition_ = position_;
323     }
324
325     public final void makeNextRowPositionCurrent() {
326         currentRowPosition_ = nextRowPosition_;
327     }
328
329     final void repositionCursorToCurrentRow() {
330         position_ = currentRowPosition_;
331     }
332
333     final void repositionCursorToNextRow() {
334         position_ = nextRowPosition_;
335     }
336
337     public final byte[] getDataBuffer() {
338         return dataBuffer_;
339     }
340
341     public final int getDataBufferLength() {
342         return dataBuffer_.length;
343     }
344
345     public final int getLastValidBytePosition() {
346         return lastValidBytePosition_;
347     }
348
349     // This tracks the total number of rows read into the client side buffer for
350
// this result set, irregardless of scrolling.
351
// Per jdbc semantics, this should never exceed statement.maxRows.
352
// This event should be generated in the materialized cursor's implementation
353
// of calculateColumnOffsetsForRow().
354
public final void incrementRowsReadEvent() {
355         rowsRead_++;
356     }
357
358     //------- the following getters are called on known column types -------------
359
// Direct conversions only, cross conversions are handled by another set of getters.
360

361     // Build a Java short from a 2-byte signed binary representation.
362
private final short get_SMALLINT(int column) {
363         return org.apache.derby.client.am.SignedBinary.getShort(dataBuffer_,
364                 columnDataPosition_[column - 1]);
365     }
366
367     // Build a Java int from a 4-byte signed binary representation.
368
private final int get_INTEGER(int column) {
369         return org.apache.derby.client.am.SignedBinary.getInt(dataBuffer_,
370                 columnDataPosition_[column - 1]);
371     }
372
373     // Build a Java long from an 8-byte signed binary representation.
374
private final long get_BIGINT(int column) {
375         return org.apache.derby.client.am.SignedBinary.getLong(dataBuffer_,
376                 columnDataPosition_[column - 1]);
377     }
378
379     // Build a Java float from a 4-byte floating point representation.
380
private final float get_FLOAT(int column) {
381         return org.apache.derby.client.am.FloatingPoint.getFloat(dataBuffer_,
382                 columnDataPosition_[column - 1]);
383     }
384
385     // Build a Java double from an 8-byte floating point representation.
386
private final double get_DOUBLE(int column) {
387         return org.apache.derby.client.am.FloatingPoint.getDouble(dataBuffer_,
388                 columnDataPosition_[column - 1]);
389     }
390
391     // Build a java.math.BigDecimal from a fixed point decimal byte representation.
392
private final java.math.BigDecimal JavaDoc get_DECIMAL(int column) throws SqlException {
393         try {
394             return org.apache.derby.client.am.Decimal.getBigDecimal(dataBuffer_,
395                     columnDataPosition_[column - 1],
396                     getColumnPrecision(column - 1),
397                     getColumnScale(column - 1));
398         } catch (java.io.UnsupportedEncodingException JavaDoc e) {
399             throw new SqlException(agent_.logWriter_,
400                 new ClientMessageId (SQLState.UNSUPPORTED_ENCODING),
401                 "DECIMAL", "java.math.BigDecimal", e);
402         }
403     }
404
405
406     // Build a Java double from a fixed point decimal byte representation.
407
private final double getDoubleFromDECIMAL(int column) throws SqlException {
408         try {
409             return org.apache.derby.client.am.Decimal.getDouble(dataBuffer_,
410                     columnDataPosition_[column - 1],
411                     getColumnPrecision(column - 1),
412                     getColumnScale(column - 1));
413         } catch (java.lang.IllegalArgumentException JavaDoc e) {
414             throw new SqlException(agent_.logWriter_,
415                 new ClientMessageId (SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE),
416                 "double", e);
417         } catch (java.io.UnsupportedEncodingException JavaDoc e) {
418             throw new SqlException(agent_.logWriter_,
419                 new ClientMessageId (SQLState.UNSUPPORTED_ENCODING),
420                 "DECIMAL", "double", e);
421         }
422     }
423
424     // Build a Java long from a fixed point decimal byte representation.
425
private final long getLongFromDECIMAL(int column) throws SqlException {
426         try {
427             return org.apache.derby.client.am.Decimal.getLong(dataBuffer_,
428                     columnDataPosition_[column - 1],
429                     getColumnPrecision(column - 1),
430                     getColumnScale(column - 1));
431         } catch (java.lang.IllegalArgumentException JavaDoc e) {
432             throw new SqlException(agent_.logWriter_,
433                 new ClientMessageId (SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE),
434                 "long", e);
435         } catch (java.io.UnsupportedEncodingException JavaDoc e) {
436             throw new SqlException(agent_.logWriter_,
437                 new ClientMessageId (SQLState.UNSUPPORTED_ENCODING),
438                 "DECIMAL", "long", e);
439         }
440     }
441
442     // Build a Java String from a database VARCHAR or LONGVARCHAR field.
443
//
444
// Depending on the ccsid, length is the number of chars or number of bytes.
445
// For 2-byte character ccsids, length is the number of characters,
446
// for all other cases length is the number of bytes.
447
// The length does not include the null terminator.
448
private final String JavaDoc getVARCHAR(int column) throws SqlException {
449         String JavaDoc tempString = null;
450         try {
451             if (ccsid_[column - 1] == 1200) {
452                 return getStringWithoutConvert(columnDataPosition_[column - 1] + 2, columnDataComputedLength_[column - 1] - 2);
453             }
454
455             // check for null encoding is needed because the net layer
456
// will no longer throw an exception if the server didn't specify
457
// a mixed or double byte ccsid (ccsid = 0). this check for null in the
458
// cursor is only required for types which can have mixed or double
459
// byte ccsids.
460
if (charsetName_[column - 1] == null) {
461                 throw new SqlException(agent_.logWriter_,
462                     new ClientMessageId(SQLState.CHARACTER_CONVERTER_NOT_AVAILABLE));
463             }
464
465             tempString = new String JavaDoc(dataBuffer_,
466                     columnDataPosition_[column - 1] + 2,
467                     columnDataComputedLength_[column - 1] - 2,
468                     charsetName_[column - 1]);
469             return (maxFieldSize_ == 0) ? tempString :
470                     tempString.substring(0, java.lang.Math.min(maxFieldSize_, tempString.length()));
471         } catch (java.io.UnsupportedEncodingException JavaDoc e) {
472             throw new SqlException(agent_.logWriter_,
473                     new ClientMessageId (SQLState.UNSUPPORTED_ENCODING),
474                     "VARCHAR", "String", e);
475         }
476     }
477
478     // Build a Java String from a database CHAR field.
479
private final String JavaDoc getCHAR(int column) throws SqlException {
480         String JavaDoc tempString = null;
481         if (ccsid_[column - 1] == 1200) {
482             return getStringWithoutConvert(columnDataPosition_[column - 1], columnDataComputedLength_[column - 1]);
483         }
484
485         try {
486             // check for null encoding is needed because the net layer
487
// will no longer throw an exception if the server didn't specify
488
// a mixed or double byte ccsid (ccsid = 0). this check for null in the
489
// cursor is only required for types which can have mixed or double
490
// byte ccsids.
491
if (charsetName_[column - 1] == null) {
492                 throw new SqlException(agent_.logWriter_,
493                     new ClientMessageId(SQLState.CHARACTER_CONVERTER_NOT_AVAILABLE));
494             }
495
496             tempString = new String JavaDoc(dataBuffer_,
497                     columnDataPosition_[column - 1],
498                     columnDataComputedLength_[column - 1],
499                     charsetName_[column - 1]);
500             return (maxFieldSize_ == 0) ? tempString :
501                     tempString.substring(0, java.lang.Math.min(maxFieldSize_, tempString.length()));
502         } catch (java.io.UnsupportedEncodingException JavaDoc e) {
503             throw new SqlException(agent_.logWriter_,
504                 new ClientMessageId (SQLState.UNSUPPORTED_ENCODING),
505                 "CHAR", "String", e);
506         }
507     }
508
509     // Build a JDBC Date object from the DERBY ISO DATE field.
510
private final java.sql.Date JavaDoc getDATE(int column) throws SqlException {
511         try {
512             return org.apache.derby.client.am.DateTime.dateBytesToDate(dataBuffer_,
513                 columnDataPosition_[column - 1],
514                 recyclableDate_,
515                 charsetName_[column - 1]);
516         }catch (UnsupportedEncodingException JavaDoc e) {
517              throw new SqlException(agent_.logWriter_,
518                  new ClientMessageId(SQLState.UNSUPPORTED_ENCODING),
519                  "DATE", "java.sql.Date", e);
520         }
521
522         
523     }
524
525     // Build a JDBC Time object from the DERBY ISO TIME field.
526
private final java.sql.Time JavaDoc getTIME(int column) throws SqlException {
527         try {
528             return org.apache.derby.client.am.DateTime.timeBytesToTime(dataBuffer_,
529                     columnDataPosition_[column - 1],
530                     recyclableTime_,
531                     charsetName_[column - 1]);
532         } catch (UnsupportedEncodingException JavaDoc e) {
533              throw new SqlException(agent_.logWriter_,
534                  new ClientMessageId(SQLState.UNSUPPORTED_ENCODING),
535                  "TIME", "java.sql.Time", e);
536         }
537     }
538
539     // Build a JDBC Timestamp object from the DERBY ISO TIMESTAMP field.
540
private final java.sql.Timestamp JavaDoc getTIMESTAMP(int column) throws SqlException {
541
542         try {
543         return org.apache.derby.client.am.DateTime.timestampBytesToTimestamp(
544                 dataBuffer_,
545                 columnDataPosition_[column - 1],
546                 recyclableTimestamp_,
547                 charsetName_[column - 1]);
548     } catch (java.io.UnsupportedEncodingException JavaDoc e) {
549              throw new SqlException(agent_.logWriter_,
550                  new ClientMessageId(SQLState.UNSUPPORTED_ENCODING),
551                  "TIMESTAMP", "java.sql.Timestamp", e);
552     }
553     }
554
555     // Build a JDBC Timestamp object from the DERBY ISO DATE field.
556
private final java.sql.Timestamp JavaDoc getTimestampFromDATE(int column) throws SqlException {
557         try {
558             return org.apache.derby.client.am.DateTime.dateBytesToTimestamp(dataBuffer_,
559                     columnDataPosition_[column - 1],
560                     recyclableTimestamp_,
561                     charsetName_[column -1]);
562         } catch (UnsupportedEncodingException JavaDoc e) {
563              throw new SqlException(agent_.logWriter_,
564                  new ClientMessageId(SQLState.UNSUPPORTED_ENCODING),
565                  "DATE", "java.sql.Timestamp", e);
566         }
567     }
568
569     // Build a JDBC Timestamp object from the DERBY ISO TIME field.
570
private final java.sql.Timestamp JavaDoc getTimestampFromTIME(int column) throws SqlException {
571         try {
572             return org.apache.derby.client.am.DateTime.timeBytesToTimestamp(dataBuffer_,
573                     columnDataPosition_[column - 1],
574                     recyclableTimestamp_,
575                     charsetName_[column -1]);
576         } catch (UnsupportedEncodingException JavaDoc e) {
577              throw new SqlException(agent_.logWriter_,
578                  new ClientMessageId(SQLState.UNSUPPORTED_ENCODING),
579                  "TIME", "java.sql.Timestamp", e);
580         }
581     }
582
583     // Build a JDBC Date object from the DERBY ISO TIMESTAMP field.
584
private final java.sql.Date JavaDoc getDateFromTIMESTAMP(int column) throws SqlException {
585         try {
586             return org.apache.derby.client.am.DateTime.timestampBytesToDate(dataBuffer_,
587                     columnDataPosition_[column - 1],
588                     recyclableDate_,
589                     charsetName_[column -1]);
590         } catch (UnsupportedEncodingException JavaDoc e) {
591              throw new SqlException(agent_.logWriter_,
592                  new ClientMessageId(SQLState.UNSUPPORTED_ENCODING),
593                  "TIMESTAMP", "java.sql.Date", e);
594         }
595     }
596
597     // Build a JDBC Time object from the DERBY ISO TIMESTAMP field.
598
private final java.sql.Time JavaDoc getTimeFromTIMESTAMP(int column) throws SqlException {
599         try {
600             return org.apache.derby.client.am.DateTime.timestampBytesToTime(dataBuffer_,
601                     columnDataPosition_[column - 1],
602                     recyclableTime_,
603                     charsetName_[column -1]);
604         } catch (UnsupportedEncodingException JavaDoc e) {
605              throw new SqlException(agent_.logWriter_,
606                  new ClientMessageId(SQLState.UNSUPPORTED_ENCODING),
607                  "TIMESTAMP", "java.sql.Time", e);
608         }
609     }
610
611     private final String JavaDoc getStringFromDATE(int column) throws SqlException {
612         return getDATE(column).toString();
613     }
614
615     // Build a string object from the DERBY byte TIME representation.
616
private final String JavaDoc getStringFromTIME(int column) throws SqlException {
617         return getTIME(column).toString();
618     }
619
620     // Build a string object from the DERBY byte TIMESTAMP representation.
621
private final String JavaDoc getStringFromTIMESTAMP(int column) throws SqlException {
622         return getTIMESTAMP(column).toString();
623     }
624
625     // Extract bytes from a database java.sql.Types.BINARY field.
626
// This is the DERBY type CHAR(n) FOR BIT DATA.
627
private final byte[] get_CHAR_FOR_BIT_DATA(int column) throws SqlException {
628         // There is no limit to the size of a column if maxFieldSize is zero.
629
// Otherwise, use the smaller of maxFieldSize and the actual column length.
630
int columnLength = (maxFieldSize_ == 0) ? columnDataComputedLength_[column - 1] :
631                 java.lang.Math.min(maxFieldSize_, columnDataComputedLength_[column - 1]);
632
633         byte[] bytes = new byte[columnLength];
634         System.arraycopy(dataBuffer_, columnDataPosition_[column - 1], bytes, 0, columnLength);
635         return bytes;
636     }
637
638     // Extract bytes from a database java.sql.Types.VARBINARY or LONGVARBINARY field.
639
// This includes the DERBY types:
640
// VARCHAR(n) FOR BIT DATA
641
// LONG VARCHAR(n) FOR BIT DATA
642
private final byte[] get_VARCHAR_FOR_BIT_DATA(int column) throws SqlException {
643         byte[] bytes;
644         int columnLength = 0;
645         columnLength = (maxFieldSize_ == 0) ? columnDataComputedLength_[column - 1] - 2 :
646                 java.lang.Math.min(maxFieldSize_, columnDataComputedLength_[column - 1] - 2);
647         bytes = new byte[columnLength];
648         System.arraycopy(dataBuffer_, columnDataPosition_[column - 1] + 2, bytes, 0, bytes.length);
649         return bytes;
650     }
651
652     abstract public Blob getBlobColumn_(int column, Agent agent) throws SqlException;
653
654     abstract public Clob getClobColumn_(int column, Agent agent) throws SqlException;
655
656     // get the raw clob bytes, without translation. dataOffset must be int[1]
657
abstract public byte[] getClobBytes_(int column, int[] dataOffset /*output*/) throws SqlException;
658
659     //------- the following getters perform any necessary cross-conversion _------
660

661     final boolean getBoolean(int column) throws SqlException {
662         switch (jdbcTypes_[column - 1]) {
663         case java.sql.Types.SMALLINT:
664             return agent_.crossConverters_.getBooleanFromShort(get_SMALLINT(column));
665         case java.sql.Types.INTEGER:
666             return agent_.crossConverters_.getBooleanFromInt(get_INTEGER(column));
667         case java.sql.Types.BIGINT:
668             return agent_.crossConverters_.getBooleanFromLong(get_BIGINT(column));
669         case java.sql.Types.REAL:
670             return agent_.crossConverters_.getBooleanFromFloat(get_FLOAT(column));
671         case java.sql.Types.DOUBLE:
672             return agent_.crossConverters_.getBooleanFromDouble(get_DOUBLE(column));
673         case java.sql.Types.DECIMAL:
674             // For performance we don't materialize the BigDecimal, but convert directly from decimal bytes to a long.
675
return agent_.crossConverters_.getBooleanFromLong(getLongFromDECIMAL(column));
676         case java.sql.Types.CHAR:
677             return agent_.crossConverters_.getBooleanFromString(getCHAR(column));
678         case java.sql.Types.VARCHAR:
679         case java.sql.Types.LONGVARCHAR:
680             return agent_.crossConverters_.getBooleanFromString(getVARCHAR(column));
681         default:
682             throw new ColumnTypeConversionException(agent_.logWriter_,
683                 "java.sql.Types " + jdbcTypes_[column -1], "boolean");
684         }
685     }
686
687     final byte getByte(int column) throws SqlException {
688         // This needs to be changed to use jdbcTypes[]
689
switch (jdbcTypes_[column - 1]) {
690         case java.sql.Types.SMALLINT:
691             return agent_.crossConverters_.getByteFromShort(get_SMALLINT(column));
692         case java.sql.Types.INTEGER:
693             return agent_.crossConverters_.getByteFromInt(get_INTEGER(column));
694         case java.sql.Types.BIGINT:
695             return agent_.crossConverters_.getByteFromLong(get_BIGINT(column));
696         case java.sql.Types.REAL:
697             return agent_.crossConverters_.getByteFromFloat(get_FLOAT(column));
698         case java.sql.Types.DOUBLE:
699             return agent_.crossConverters_.getByteFromDouble(get_DOUBLE(column));
700         case java.sql.Types.DECIMAL:
701             // For performance we don't materialize the BigDecimal, but convert directly from decimal bytes to a long.
702
return agent_.crossConverters_.getByteFromLong(getLongFromDECIMAL(column));
703         case java.sql.Types.CHAR:
704             return agent_.crossConverters_.getByteFromString(getCHAR(column));
705         case java.sql.Types.VARCHAR:
706         case java.sql.Types.LONGVARCHAR:
707             return agent_.crossConverters_.getByteFromString(getVARCHAR(column));
708         default:
709             throw new ColumnTypeConversionException(agent_.logWriter_,
710                 "java.sql.Types " + jdbcTypes_[column -1], "byte");
711         }
712     }
713
714     final short getShort(int column) throws SqlException {
715         switch (jdbcTypes_[column - 1]) {
716         case java.sql.Types.SMALLINT:
717             return get_SMALLINT(column);
718         case java.sql.Types.INTEGER:
719             return agent_.crossConverters_.getShortFromInt(get_INTEGER(column));
720         case java.sql.Types.BIGINT:
721             return agent_.crossConverters_.getShortFromLong(get_BIGINT(column));
722         case java.sql.Types.REAL:
723             return agent_.crossConverters_.getShortFromFloat(get_FLOAT(column));
724         case java.sql.Types.DOUBLE:
725             return agent_.crossConverters_.getShortFromDouble(get_DOUBLE(column));
726         case java.sql.Types.DECIMAL:
727             // For performance we don't materialize the BigDecimal, but convert directly from decimal bytes to a long.
728
return agent_.crossConverters_.getShortFromLong(getLongFromDECIMAL(column));
729         case java.sql.Types.CHAR:
730             return agent_.crossConverters_.getShortFromString(getCHAR(column));
731         case java.sql.Types.VARCHAR:
732         case java.sql.Types.LONGVARCHAR:
733             return agent_.crossConverters_.getShortFromString(getVARCHAR(column));
734         default:
735             throw new ColumnTypeConversionException(agent_.logWriter_,
736                 "java.sql.Types " + jdbcTypes_[column -1], "short");
737         }
738     }
739
740     final int getInt(int column) throws SqlException {
741         switch (jdbcTypes_[column - 1]) {
742         case java.sql.Types.SMALLINT:
743             return (int) get_SMALLINT(column);
744         case java.sql.Types.INTEGER:
745             return get_INTEGER(column);
746         case java.sql.Types.BIGINT:
747             return agent_.crossConverters_.getIntFromLong(get_BIGINT(column));
748         case java.sql.Types.REAL:
749             return agent_.crossConverters_.getIntFromFloat(get_FLOAT(column));
750         case java.sql.Types.DOUBLE:
751             return agent_.crossConverters_.getIntFromDouble(get_DOUBLE(column));
752         case java.sql.Types.DECIMAL:
753             // For performance we don't materialize the BigDecimal, but convert directly from decimal bytes to a long.
754
return agent_.crossConverters_.getIntFromLong(getLongFromDECIMAL(column));
755         case java.sql.Types.CHAR:
756             return agent_.crossConverters_.getIntFromString(getCHAR(column));
757         case java.sql.Types.VARCHAR:
758         case java.sql.Types.LONGVARCHAR:
759             return agent_.crossConverters_.getIntFromString(getVARCHAR(column));
760         default:
761             throw new ColumnTypeConversionException(agent_.logWriter_,
762                 "java.sql.Types " + jdbcTypes_[column -1], "int");
763         }
764     }
765
766     final long getLong(int column) throws SqlException {
767         switch (jdbcTypes_[column - 1]) {
768         case java.sql.Types.SMALLINT:
769             return (long) get_SMALLINT(column);
770         case java.sql.Types.INTEGER:
771             return (long) get_INTEGER(column);
772         case java.sql.Types.BIGINT:
773             return get_BIGINT(column);
774         case java.sql.Types.REAL:
775             return agent_.crossConverters_.getLongFromFloat(get_FLOAT(column));
776         case java.sql.Types.DOUBLE:
777             return agent_.crossConverters_.getLongFromDouble(get_DOUBLE(column));
778         case java.sql.Types.DECIMAL:
779             // For performance we don't materialize the BigDecimal, but convert directly from decimal bytes to a long.
780
return getLongFromDECIMAL(column);
781         case java.sql.Types.CHAR:
782             return agent_.crossConverters_.getLongFromString(getCHAR(column));
783         case java.sql.Types.VARCHAR:
784         case java.sql.Types.LONGVARCHAR:
785             return agent_.crossConverters_.getLongFromString(getVARCHAR(column));
786         default:
787             throw new ColumnTypeConversionException(agent_.logWriter_,
788                 "java.sql.Types " + jdbcTypes_[column -1], "long");
789         }
790     }
791
792     final float getFloat(int column) throws SqlException {
793         switch (jdbcTypes_[column - 1]) {
794         case java.sql.Types.REAL:
795             return get_FLOAT(column);
796         case java.sql.Types.DOUBLE:
797             return agent_.crossConverters_.getFloatFromDouble(get_DOUBLE(column));
798         case java.sql.Types.DECIMAL:
799             // For performance we don't materialize the BigDecimal, but convert directly from decimal bytes to a long.
800
return agent_.crossConverters_.getFloatFromDouble(getDoubleFromDECIMAL(column));
801         case java.sql.Types.SMALLINT:
802             return (float) get_SMALLINT(column);
803         case java.sql.Types.INTEGER:
804             return (float) get_INTEGER(column);
805         case java.sql.Types.BIGINT:
806             return (float) get_BIGINT(column);
807         case java.sql.Types.CHAR:
808             return agent_.crossConverters_.getFloatFromString(getCHAR(column));
809         case java.sql.Types.VARCHAR:
810         case java.sql.Types.LONGVARCHAR:
811             return agent_.crossConverters_.getFloatFromString(getVARCHAR(column));
812         default:
813             throw new ColumnTypeConversionException(agent_.logWriter_,
814                 "java.sql.Types " + jdbcTypes_[column -1], "float");
815         }
816     }
817
818     final double getDouble(int column) throws SqlException {
819         switch (jdbcTypes_[column - 1]) {
820         case java.sql.Types.REAL:
821             double d = (double) get_FLOAT(column);
822             return d;
823             //return (double) get_FLOAT (column);
824
case java.sql.Types.DOUBLE:
825             return get_DOUBLE(column);
826         case java.sql.Types.DECIMAL:
827             // For performance we don't materialize the BigDecimal, but convert directly from decimal bytes to a long.
828
return getDoubleFromDECIMAL(column);
829         case java.sql.Types.SMALLINT:
830             return (double) get_SMALLINT(column);
831         case java.sql.Types.INTEGER:
832             return (double) get_INTEGER(column);
833         case java.sql.Types.BIGINT:
834             return (double) get_BIGINT(column);
835         case java.sql.Types.CHAR:
836             return agent_.crossConverters_.getDoubleFromString(getCHAR(column));
837         case java.sql.Types.VARCHAR:
838         case java.sql.Types.LONGVARCHAR:
839             return agent_.crossConverters_.getDoubleFromString(getVARCHAR(column));
840         default:
841             throw new ColumnTypeConversionException(agent_.logWriter_,
842                 "java.sql.Types " + jdbcTypes_[column -1], "double");
843         }
844     }
845
846     final java.math.BigDecimal JavaDoc getBigDecimal(int column) throws SqlException {
847         switch (jdbcTypes_[column - 1]) {
848         case java.sql.Types.DECIMAL:
849             return get_DECIMAL(column);
850         case java.sql.Types.REAL:
851             // Can't use the following commented out line because it changes precision of the result.
852
//return new java.math.BigDecimal (get_FLOAT (column));
853
float f = get_FLOAT(column);
854             return new java.math.BigDecimal JavaDoc(String.valueOf(f));
855         case java.sql.Types.DOUBLE:
856             // Can't use the following commented out line because it changes precision of the result.
857
return new java.math.BigDecimal JavaDoc(String.valueOf(get_DOUBLE(column)));
858         case java.sql.Types.SMALLINT:
859             return java.math.BigDecimal.valueOf(get_SMALLINT(column));
860         case java.sql.Types.INTEGER:
861             return java.math.BigDecimal.valueOf(get_INTEGER(column));
862         case java.sql.Types.BIGINT:
863             return java.math.BigDecimal.valueOf(get_BIGINT(column));
864         case java.sql.Types.CHAR:
865             return agent_.crossConverters_.getBigDecimalFromString(getCHAR(column));
866         case java.sql.Types.VARCHAR:
867         case java.sql.Types.LONGVARCHAR:
868             return agent_.crossConverters_.getBigDecimalFromString(getVARCHAR(column));
869         default:
870             throw new ColumnTypeConversionException(agent_.logWriter_,
871                 "java.sql.Types " + jdbcTypes_[column -1], "java.math.BigDecimal");
872         }
873     }
874
875     final java.sql.Date JavaDoc getDate(int column) throws SqlException {
876         switch (jdbcTypes_[column - 1]) {
877         case java.sql.Types.DATE:
878             return getDATE(column);
879         case java.sql.Types.TIMESTAMP:
880             return getDateFromTIMESTAMP(column);
881         case java.sql.Types.CHAR:
882             return agent_.crossConverters_.getDateFromString(getCHAR(column));
883         case java.sql.Types.VARCHAR:
884         case java.sql.Types.LONGVARCHAR:
885             return agent_.crossConverters_.getDateFromString(getVARCHAR(column));
886         default:
887             throw new ColumnTypeConversionException(agent_.logWriter_,
888                 "java.sql.Types " + jdbcTypes_[column -1], "java.sql.Date");
889         }
890     }
891
892     final java.sql.Time JavaDoc getTime(int column) throws SqlException {
893         switch (jdbcTypes_[column - 1]) {
894         case java.sql.Types.TIME:
895             return getTIME(column);
896         case java.sql.Types.TIMESTAMP:
897             return getTimeFromTIMESTAMP(column);
898         case java.sql.Types.CHAR:
899             return agent_.crossConverters_.getTimeFromString(getCHAR(column));
900         case java.sql.Types.VARCHAR:
901         case java.sql.Types.LONGVARCHAR:
902             return agent_.crossConverters_.getTimeFromString(getVARCHAR(column));
903         default:
904             throw new ColumnTypeConversionException(agent_.logWriter_,
905                 "java.sql.Types " + jdbcTypes_[column -1], "java.sql.Time");
906         }
907     }
908
909     final java.sql.Timestamp JavaDoc getTimestamp(int column) throws SqlException {
910         switch (jdbcTypes_[column - 1]) {
911         case java.sql.Types.TIMESTAMP:
912             return getTIMESTAMP(column);
913         case java.sql.Types.DATE:
914             return getTimestampFromDATE(column);
915         case java.sql.Types.TIME:
916             return getTimestampFromTIME(column);
917         case java.sql.Types.CHAR:
918             return agent_.crossConverters_.getTimestampFromString(getCHAR(column));
919         case java.sql.Types.VARCHAR:
920         case java.sql.Types.LONGVARCHAR:
921             return agent_.crossConverters_.getTimestampFromString(getVARCHAR(column));
922         default:
923             throw new ColumnTypeConversionException(agent_.logWriter_,
924                 "java.sql.Types " + jdbcTypes_[column -1], "java.sql.Timestamp");
925         }
926     }
927
928     final String JavaDoc getString(int column) throws SqlException {
929         try {
930             String JavaDoc tempString = null;
931             switch (jdbcTypes_[column - 1]) {
932             case java.sql.Types.CHAR:
933                 return getCHAR(column);
934             case java.sql.Types.VARCHAR:
935             case java.sql.Types.LONGVARCHAR:
936                 return getVARCHAR(column);
937
938             case java.sql.Types.SMALLINT:
939                 return String.valueOf(get_SMALLINT(column));
940             case java.sql.Types.INTEGER:
941                 return String.valueOf(get_INTEGER(column));
942             case java.sql.Types.BIGINT:
943                 return String.valueOf(get_BIGINT(column));
944             case java.sql.Types.REAL:
945                 return String.valueOf(get_FLOAT(column));
946             case java.sql.Types.DOUBLE:
947                 return String.valueOf(get_DOUBLE(column));
948             case java.sql.Types.DECIMAL:
949                 // We could get better performance here if we didn't materialize the BigDecimal,
950
// but converted directly from decimal bytes to a string.
951
return String.valueOf(get_DECIMAL(column));
952             case java.sql.Types.DATE:
953                 return getStringFromDATE(column);
954             case java.sql.Types.TIME:
955                 return getStringFromTIME(column);
956             case java.sql.Types.TIMESTAMP:
957                 return getStringFromTIMESTAMP(column);
958             case Types.BINARY:
959                 tempString =
960                         agent_.crossConverters_.getStringFromBytes(get_CHAR_FOR_BIT_DATA(column));
961                 return (maxFieldSize_ == 0) ? tempString :
962                         tempString.substring(0, java.lang.Math.min(maxFieldSize_, tempString.length()));
963             case java.sql.Types.VARBINARY:
964             case java.sql.Types.LONGVARBINARY:
965                 tempString =
966                         agent_.crossConverters_.getStringFromBytes(get_VARCHAR_FOR_BIT_DATA(column));
967                 return (maxFieldSize_ == 0) ? tempString :
968                         tempString.substring(0, java.lang.Math.min(maxFieldSize_, tempString.length()));
969             case java.sql.Types.BLOB:
970                 Blob b = (Blob) getBlobColumn_(column, agent_);
971                 return agent_.crossConverters_.getStringFromBytes(b.getBytes(1, (int) b.length()));
972             case java.sql.Types.CLOB:
973                 Clob c = getClobColumn_(column, agent_);
974                 return c.getSubString(1, (int) c.length());
975             default:
976                 throw new ColumnTypeConversionException(agent_.logWriter_,
977                     "java.sql.Types " + jdbcTypes_[column -1], "String");
978             }
979         } catch ( SQLException JavaDoc se ) {
980             throw new SqlException(se);
981         }
982     }
983
984     final byte[] getBytes(int column) throws SqlException {
985         try {
986             switch (jdbcTypes_[column - 1]) {
987             case java.sql.Types.BINARY:
988                 return get_CHAR_FOR_BIT_DATA(column);
989             case java.sql.Types.VARBINARY:
990             case java.sql.Types.LONGVARBINARY:
991                 return get_VARCHAR_FOR_BIT_DATA(column);
992             case java.sql.Types.BLOB:
993                 Blob b = (Blob) getBlobColumn_(column, agent_);
994                 return b.getBytes(1, (int) b.length());
995             default:
996                 throw new ColumnTypeConversionException(agent_.logWriter_,
997                     "java.sql.Types " + jdbcTypes_[column -1], "byte[]");
998             }
999         } catch ( SQLException JavaDoc se ) {
1000            throw new SqlException(se);
1001        }
1002    }
1003
1004    public final java.io.InputStream JavaDoc getBinaryStream(int column) throws SqlException {
1005        try {
1006            switch (jdbcTypes_[column - 1]) {
1007            case java.sql.Types.BINARY:
1008                return new java.io.ByteArrayInputStream JavaDoc(get_CHAR_FOR_BIT_DATA(column));
1009            case java.sql.Types.VARBINARY:
1010            case java.sql.Types.LONGVARBINARY:
1011                return new java.io.ByteArrayInputStream JavaDoc(get_VARCHAR_FOR_BIT_DATA(column));
1012            case java.sql.Types.BLOB:
1013                Blob b = (Blob) getBlobColumn_(column, agent_);
1014                return b.getBinaryStream();
1015            default:
1016                throw new ColumnTypeConversionException(agent_.logWriter_,
1017                    "java.sql.Types " + jdbcTypes_[column -1], "java.io.InputStream");
1018            }
1019        } catch ( SQLException JavaDoc se ) {
1020            throw new SqlException(se);
1021        }
1022    }
1023
1024    public final java.io.InputStream JavaDoc getAsciiStream(int column) throws SqlException {
1025        try {
1026            switch (jdbcTypes_[column - 1]) {
1027            case java.sql.Types.CLOB:
1028                Clob c = getClobColumn_(column, agent_);
1029                return c.getAsciiStream();
1030            case java.sql.Types.CHAR:
1031                try {
1032                    return new java.io.ByteArrayInputStream JavaDoc(getCHAR(column).getBytes("US-ASCII"));
1033                } catch (java.io.UnsupportedEncodingException JavaDoc e) {
1034                    throw new SqlException(agent_.logWriter_,
1035                            new ClientMessageId (SQLState.UNSUPPORTED_ENCODING),
1036                            "CHAR", "java.io.InputStream", e);
1037                }
1038            case java.sql.Types.VARCHAR:
1039            case java.sql.Types.LONGVARCHAR:
1040                try {
1041                    return new java.io.ByteArrayInputStream JavaDoc(getVARCHAR(column).getBytes("US-ASCII"));
1042                } catch (java.io.UnsupportedEncodingException JavaDoc e) {
1043                    throw new SqlException(agent_.logWriter_,
1044                            new ClientMessageId (SQLState.UNSUPPORTED_ENCODING),
1045                            "VARCHAR/LONGVARCHAR", "java.io.InputStream", e);
1046                }
1047            case java.sql.Types.BINARY:
1048                return new java.io.ByteArrayInputStream JavaDoc(get_CHAR_FOR_BIT_DATA(column));
1049            case java.sql.Types.VARBINARY:
1050            case java.sql.Types.LONGVARBINARY:
1051                return new java.io.ByteArrayInputStream JavaDoc(get_VARCHAR_FOR_BIT_DATA(column));
1052            case java.sql.Types.BLOB:
1053                Blob b = (Blob) getBlobColumn_(column, agent_);
1054                return b.getBinaryStream();
1055            default:
1056                throw new ColumnTypeConversionException(agent_.logWriter_,
1057                    "java.sql.Types " + jdbcTypes_[column -1], "java.io.InputStream");
1058            }
1059        }
1060        catch ( SQLException JavaDoc se ) {
1061            throw new SqlException(se);
1062        }
1063    }
1064
1065    public final java.io.InputStream JavaDoc getUnicodeStream(int column) throws SqlException {
1066        try {
1067            switch (jdbcTypes_[column - 1]) {
1068            case java.sql.Types.CLOB:
1069                {
1070                    Clob c = getClobColumn_(column, agent_);
1071                    String JavaDoc s = c.getSubString(1L, (int) c.length());
1072                    try {
1073                        return new java.io.ByteArrayInputStream JavaDoc(s.getBytes("UTF-8"));
1074                    } catch (java.io.UnsupportedEncodingException JavaDoc e) {
1075                        throw new SqlException(agent_.logWriter_,
1076                                new ClientMessageId (SQLState.UNSUPPORTED_ENCODING),
1077                                "CLOB", "UnicodeStream", e);
1078                    }
1079                }
1080            case java.sql.Types.CHAR:
1081                {
1082                    try {
1083                        return new java.io.ByteArrayInputStream JavaDoc(getCHAR(column).getBytes("UTF-8"));
1084                    } catch (java.io.UnsupportedEncodingException JavaDoc e) {
1085                        throw new SqlException(agent_.logWriter_,
1086                                new ClientMessageId (SQLState.UNSUPPORTED_ENCODING),
1087                                "CHAR", "UnicodeStream", e);
1088                    }
1089                }
1090            case java.sql.Types.VARCHAR:
1091            case java.sql.Types.LONGVARCHAR:
1092                try {
1093                    return new java.io.ByteArrayInputStream JavaDoc(getVARCHAR(column).getBytes("UTF-8"));
1094                } catch (java.io.UnsupportedEncodingException JavaDoc e) {
1095                    throw new SqlException(agent_.logWriter_,
1096                            new ClientMessageId (SQLState.UNSUPPORTED_ENCODING),
1097                            "VARCHAR/LONGVARCHAR", "UnicodeStream", e);
1098                }
1099            case java.sql.Types.BINARY:
1100                return new java.io.ByteArrayInputStream JavaDoc(get_CHAR_FOR_BIT_DATA(column));
1101            case java.sql.Types.VARBINARY:
1102            case java.sql.Types.LONGVARBINARY:
1103                return new java.io.ByteArrayInputStream JavaDoc(get_VARCHAR_FOR_BIT_DATA(column));
1104            case java.sql.Types.BLOB:
1105                Blob b = (Blob) getBlobColumn_(column, agent_);
1106                return b.getBinaryStream();
1107            default:
1108                throw new ColumnTypeConversionException(agent_.logWriter_,
1109                    "java.sql.Types " + jdbcTypes_[column -1], "UnicodeStream");
1110            }
1111        } catch ( SQLException JavaDoc se ) {
1112            throw new SqlException(se);
1113        }
1114    }
1115
1116    public final java.io.Reader JavaDoc getCharacterStream(int column) throws SqlException {
1117        try {
1118            switch (jdbcTypes_[column - 1]) {
1119            case java.sql.Types.CLOB:
1120                Clob c = getClobColumn_(column, agent_);
1121                return c.getCharacterStream();
1122            case java.sql.Types.CHAR:
1123                return new java.io.StringReader JavaDoc(getCHAR(column));
1124            case java.sql.Types.VARCHAR:
1125            case java.sql.Types.LONGVARCHAR:
1126                return new java.io.StringReader JavaDoc(getVARCHAR(column));
1127            case java.sql.Types.BINARY:
1128                try {
1129                    return new java.io.InputStreamReader JavaDoc(new java.io.ByteArrayInputStream JavaDoc(get_CHAR_FOR_BIT_DATA(column)), "UTF-16BE");
1130                } catch (java.io.UnsupportedEncodingException JavaDoc e) {
1131                    throw new SqlException(agent_.logWriter_,
1132                            new ClientMessageId (SQLState.UNSUPPORTED_ENCODING),
1133                            "BINARY", "java.io.Reader", e);
1134                }
1135            case java.sql.Types.VARBINARY:
1136            case java.sql.Types.LONGVARBINARY:
1137                try {
1138                    return new java.io.InputStreamReader JavaDoc(new java.io.ByteArrayInputStream JavaDoc(get_VARCHAR_FOR_BIT_DATA(column)), "UTF-16BE");
1139                } catch (java.io.UnsupportedEncodingException JavaDoc e) {
1140                    throw new SqlException(agent_.logWriter_,
1141                            new ClientMessageId (SQLState.UNSUPPORTED_ENCODING),
1142                            "VARBINARY/LONGVARBINARY", "java.io.Reader", e);
1143                }
1144            case java.sql.Types.BLOB:
1145                try {
1146                    Blob b = (Blob) getBlobColumn_(column, agent_);
1147                    return new java.io.InputStreamReader JavaDoc(b.getBinaryStream(), "UTF-16BE");
1148                } catch (java.io.UnsupportedEncodingException JavaDoc e) {
1149                    throw new SqlException(agent_.logWriter_,
1150                            new ClientMessageId (SQLState.UNSUPPORTED_ENCODING),
1151                            "BLOB", "java.io.Reader", e);
1152                }
1153            default:
1154                throw new ColumnTypeConversionException(agent_.logWriter_,
1155                    "java.sql.Types " + jdbcTypes_[column -1], "java.io.Reader");
1156            }
1157        } catch ( SQLException JavaDoc se ) {
1158            throw new SqlException(se);
1159        }
1160    }
1161
1162    public final java.sql.Blob JavaDoc getBlob(int column) throws SqlException {
1163        switch (jdbcTypes_[column - 1]) {
1164        case Types.BLOB:
1165            return getBlobColumn_(column, agent_);
1166        default:
1167            throw new ColumnTypeConversionException(agent_.logWriter_,
1168                "java.sql.Types " + jdbcTypes_[column -1], "java.sql.Blob");
1169        }
1170    }
1171
1172    public final java.sql.Clob JavaDoc getClob(int column) throws SqlException {
1173        switch (jdbcTypes_[column - 1]) {
1174        case Types.CLOB:
1175            return getClobColumn_(column, agent_);
1176        default:
1177            throw new ColumnTypeConversionException(agent_.logWriter_,
1178                "java.sql.Types " + jdbcTypes_[column -1], "java.sql.Clob");
1179        }
1180    }
1181
1182    public final java.sql.Array JavaDoc getArray(int column) throws SqlException {
1183        throw new SqlException(agent_.logWriter_,
1184            new ClientMessageId (SQLState.NOT_IMPLEMENTED),
1185            "getArray(int)");
1186    }
1187
1188    public final java.sql.Ref JavaDoc getRef(int column) throws SqlException {
1189        throw new SqlException(agent_.logWriter_,
1190            new ClientMessageId (SQLState.NOT_IMPLEMENTED), "getRef(int)");
1191    }
1192
1193    public final Object JavaDoc getObject(int column) throws SqlException {
1194        switch (jdbcTypes_[column - 1]) {
1195        case java.sql.Types.SMALLINT:
1196            return new Integer JavaDoc(get_SMALLINT(column)); // See Table 4 in JDBC 1 spec (pg. 932 in jdbc book)
1197
case java.sql.Types.INTEGER:
1198            return new Integer JavaDoc(get_INTEGER(column));
1199        case java.sql.Types.BIGINT:
1200            return new Long JavaDoc(get_BIGINT(column));
1201        case java.sql.Types.REAL:
1202            return new Float JavaDoc(get_FLOAT(column));
1203        case java.sql.Types.DOUBLE:
1204            return new Double JavaDoc(get_DOUBLE(column));
1205        case java.sql.Types.DECIMAL:
1206            return get_DECIMAL(column);
1207        case java.sql.Types.DATE:
1208            return getDATE(column);
1209        case java.sql.Types.TIME:
1210            return getTIME(column);
1211        case java.sql.Types.TIMESTAMP:
1212            return getTIMESTAMP(column);
1213        case java.sql.Types.CHAR:
1214            return getCHAR(column);
1215        case java.sql.Types.VARCHAR:
1216        case java.sql.Types.LONGVARCHAR:
1217            return getVARCHAR(column);
1218        case Types.BINARY:
1219            return get_CHAR_FOR_BIT_DATA(column);
1220        case java.sql.Types.VARBINARY:
1221        case java.sql.Types.LONGVARBINARY:
1222            return get_VARCHAR_FOR_BIT_DATA(column);
1223        case java.sql.Types.BLOB:
1224            return getBlobColumn_(column, agent_);
1225        case java.sql.Types.CLOB:
1226            return getClobColumn_(column, agent_);
1227        default:
1228            throw new ColumnTypeConversionException(agent_.logWriter_,
1229                "java.sql.Types " + jdbcTypes_[column -1], "Object");
1230        }
1231    }
1232
1233    public final void allocateCharBuffer() {
1234        // compute the maximum char length
1235
int maxCharLength = 0;
1236        for (int i = 0; i < columns_; i++) {
1237            switch (jdbcTypes_[i]) {
1238            case Types.CHAR:
1239            case Types.VARCHAR:
1240            case Types.LONGVARCHAR:
1241                if (fdocaLength_[i] > maxCharLength) {
1242                    maxCharLength = fdocaLength_[i];
1243                }
1244            }
1245        }
1246
1247        // allocate char buffer to accomodate largest result column
1248
charBuffer_ = new char[maxCharLength];
1249    }
1250
1251    private final String JavaDoc getStringWithoutConvert(int position, int actualLength) throws SqlException {
1252        int start = position;
1253        int end = position + actualLength;
1254
1255        int charCount = 0;
1256        while (start < end) {
1257            charBuffer_[charCount++] = (char) (((dataBuffer_[start] & 0xff) << 8) | (dataBuffer_[start + 1] & 0xff));
1258            start += 2;
1259        }
1260
1261        return new String JavaDoc(charBuffer_, 0, charCount);
1262    }
1263
1264    public void nullDataForGC() {
1265        dataBuffer_ = null;
1266        dataBufferStream_ = null;
1267        columnDataPosition_ = null;
1268        columnDataComputedLength_ = null;
1269        columnDataPositionCache_ = null;
1270        columnDataLengthCache_ = null;
1271        columnDataIsNullCache_ = null;
1272        jdbcTypes_ = null;
1273        nullable_ = null;
1274        charsetName_ = null;
1275        this.ccsid_ = null;
1276        isUpdateDeleteHoleCache_ = null;
1277        isNull_ = null;
1278        fdocaLength_ = null;
1279        charBuffer_ = null;
1280    }
1281
1282    private final int getColumnPrecision(int column) {
1283        return ((fdocaLength_[column] >> 8) & 0xff);
1284    }
1285
1286    private final int getColumnScale(int column) {
1287        return (fdocaLength_[column] & 0xff);
1288    }
1289
1290    // Only used by Sqlca.getMessage() when using a locale encoding
1291
// to convert errror message text instead of relying on server encoding as usual.
1292
final byte[] getBytesFromVARCHAR(int column) throws SqlException {
1293        byte[] bytes;
1294        bytes = new byte[columnDataComputedLength_[column - 1] - 2];
1295        System.arraycopy(dataBuffer_, columnDataPosition_[column - 1] + 2, bytes, 0, bytes.length);
1296        return bytes;
1297    }
1298}
1299
Popular Tags