1 21 22 package org.apache.derby.client.am; 23 import org.apache.derby.shared.common.reference.SQLState; 24 25 import java.sql.SQLException ; 26 import java.io.UnsupportedEncodingException ; 27 28 32 public abstract class Cursor { 33 protected Agent agent_; 34 35 37 public final static int STRING = 0; 38 public final static int VARIABLE_STRING = 2; public final static int VARIABLE_SHORT_STRING = 1; 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 52 public byte[] dataBuffer_; 54 public java.io.ByteArrayOutputStream dataBufferStream_; 55 public int position_; public int lastValidBytePosition_; 57 public boolean hasLobs_; 59 protected int currentRowPosition_; 61 private int nextRowPosition_; 62 protected int[] columnDataPosition_; 64 65 protected int[] columnDataComputedLength_; 67 69 private boolean allRowsReceivedFromServer_; 74 75 int rowsRead_; 78 79 int maxFieldSize_ = 0; 81 82 protected java.util.ArrayList columnDataPositionCache_ = new java.util.ArrayList (); 85 protected java.util.ArrayList columnDataLengthCache_ = new java.util.ArrayList (); 86 protected java.util.ArrayList columnDataIsNullCache_ = new java.util.ArrayList (); 87 public java.util.ArrayList isUpdateDeleteHoleCache_ = new java.util.ArrayList (); 88 public boolean isUpdateDeleteHole_; 89 90 private boolean isRowUpdated_; 94 95 final static public java.lang.Boolean ROW_IS_NULL = new Boolean (true); 96 final static public java.lang.Boolean ROW_IS_NOT_NULL = new Boolean (false); 97 98 java.sql.Date recyclableDate_ = null; 99 java.sql.Time recyclableTime_ = null; 100 java.sql.Timestamp recyclableTimestamp_ = null; 101 102 104 public int[] jdbcTypes_; 105 public int columns_; 106 public boolean[] nullable_; 107 public String [] charsetName_; 108 public boolean[] isNull_; 109 public int[] fdocaLength_; 111 113 public int[] ccsid_; 114 char[] charBuffer_; 115 116 118 public Cursor(Agent agent) { 119 agent_ = agent; 120 isRowUpdated_ = false; 121 dataBufferStream_ = new java.io.ByteArrayOutputStream (); 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 [numberOfColumns]; 137 138 ccsid_ = new int[numberOfColumns]; 139 140 isNull_ = new boolean[numberOfColumns]; 141 jdbcTypes_ = new int[numberOfColumns]; 142 } 143 144 153 protected boolean stepNext(boolean allowServerFetch) throws SqlException { 154 boolean rowPositionIsValid = true; 156 157 clearLobData_(); 160 161 makeNextRowPositionCurrent(); 163 164 isUpdateDeleteHole_ = false; 166 167 isRowUpdated_ = false; 168 169 while (!dataBufferHasUnprocessedData()) { 173 if (allRowsReceivedFromServer_) { 174 return false; 175 } 176 getMoreData_(); 177 } 178 179 rowPositionIsValid = calculateColumnOffsetsForRow_(0, allowServerFetch); 183 markNextRowPosition(); 184 return rowPositionIsValid; 185 } 186 187 194 public boolean next() throws SqlException { 195 return stepNext(true); 196 } 197 198 200 206 public void setAllRowsReceivedFromServer(boolean b) { 207 allRowsReceivedFromServer_ = b; 208 } 209 210 217 public final boolean allRowsReceivedFromServer() { 218 return allRowsReceivedFromServer_; 219 } 220 221 public final boolean currentRowPositionIsEqualToNextRowPosition() { 222 return (currentRowPosition_ == nextRowPosition_); 223 } 224 225 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 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 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 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 283 public final void setIsRowUpdated(boolean isRowUpdated) { 284 isRowUpdated_ = isRowUpdated; 285 } 286 287 293 public final boolean getIsRowUpdated() { 294 return isRowUpdated_; 295 } 296 297 303 public final boolean getIsUpdateDeleteHole() { 304 return isUpdateDeleteHole_; 305 } 306 307 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 public final void incrementRowsReadEvent() { 355 rowsRead_++; 356 } 357 358 361 private final short get_SMALLINT(int column) { 363 return org.apache.derby.client.am.SignedBinary.getShort(dataBuffer_, 364 columnDataPosition_[column - 1]); 365 } 366 367 private final int get_INTEGER(int column) { 369 return org.apache.derby.client.am.SignedBinary.getInt(dataBuffer_, 370 columnDataPosition_[column - 1]); 371 } 372 373 private final long get_BIGINT(int column) { 375 return org.apache.derby.client.am.SignedBinary.getLong(dataBuffer_, 376 columnDataPosition_[column - 1]); 377 } 378 379 private final float get_FLOAT(int column) { 381 return org.apache.derby.client.am.FloatingPoint.getFloat(dataBuffer_, 382 columnDataPosition_[column - 1]); 383 } 384 385 private final double get_DOUBLE(int column) { 387 return org.apache.derby.client.am.FloatingPoint.getDouble(dataBuffer_, 388 columnDataPosition_[column - 1]); 389 } 390 391 private final java.math.BigDecimal 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 e) { 399 throw new SqlException(agent_.logWriter_, 400 new ClientMessageId (SQLState.UNSUPPORTED_ENCODING), 401 "DECIMAL", "java.math.BigDecimal", e); 402 } 403 } 404 405 406 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 e) { 414 throw new SqlException(agent_.logWriter_, 415 new ClientMessageId (SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE), 416 "double", e); 417 } catch (java.io.UnsupportedEncodingException e) { 418 throw new SqlException(agent_.logWriter_, 419 new ClientMessageId (SQLState.UNSUPPORTED_ENCODING), 420 "DECIMAL", "double", e); 421 } 422 } 423 424 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 e) { 432 throw new SqlException(agent_.logWriter_, 433 new ClientMessageId (SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE), 434 "long", e); 435 } catch (java.io.UnsupportedEncodingException e) { 436 throw new SqlException(agent_.logWriter_, 437 new ClientMessageId (SQLState.UNSUPPORTED_ENCODING), 438 "DECIMAL", "long", e); 439 } 440 } 441 442 private final String getVARCHAR(int column) throws SqlException { 449 String tempString = null; 450 try { 451 if (ccsid_[column - 1] == 1200) { 452 return getStringWithoutConvert(columnDataPosition_[column - 1] + 2, columnDataComputedLength_[column - 1] - 2); 453 } 454 455 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 (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 e) { 472 throw new SqlException(agent_.logWriter_, 473 new ClientMessageId (SQLState.UNSUPPORTED_ENCODING), 474 "VARCHAR", "String", e); 475 } 476 } 477 478 private final String getCHAR(int column) throws SqlException { 480 String tempString = null; 481 if (ccsid_[column - 1] == 1200) { 482 return getStringWithoutConvert(columnDataPosition_[column - 1], columnDataComputedLength_[column - 1]); 483 } 484 485 try { 486 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 (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 e) { 503 throw new SqlException(agent_.logWriter_, 504 new ClientMessageId (SQLState.UNSUPPORTED_ENCODING), 505 "CHAR", "String", e); 506 } 507 } 508 509 private final java.sql.Date 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 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 private final java.sql.Time 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 e) { 533 throw new SqlException(agent_.logWriter_, 534 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 535 "TIME", "java.sql.Time", e); 536 } 537 } 538 539 private final java.sql.Timestamp 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 e) { 549 throw new SqlException(agent_.logWriter_, 550 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 551 "TIMESTAMP", "java.sql.Timestamp", e); 552 } 553 } 554 555 private final java.sql.Timestamp 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 e) { 563 throw new SqlException(agent_.logWriter_, 564 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 565 "DATE", "java.sql.Timestamp", e); 566 } 567 } 568 569 private final java.sql.Timestamp 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 e) { 577 throw new SqlException(agent_.logWriter_, 578 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 579 "TIME", "java.sql.Timestamp", e); 580 } 581 } 582 583 private final java.sql.Date 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 e) { 591 throw new SqlException(agent_.logWriter_, 592 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 593 "TIMESTAMP", "java.sql.Date", e); 594 } 595 } 596 597 private final java.sql.Time 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 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 getStringFromDATE(int column) throws SqlException { 612 return getDATE(column).toString(); 613 } 614 615 private final String getStringFromTIME(int column) throws SqlException { 617 return getTIME(column).toString(); 618 } 619 620 private final String getStringFromTIMESTAMP(int column) throws SqlException { 622 return getTIMESTAMP(column).toString(); 623 } 624 625 private final byte[] get_CHAR_FOR_BIT_DATA(int column) throws SqlException { 628 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 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 abstract public byte[] getClobBytes_(int column, int[] dataOffset ) throws SqlException; 658 659 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 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 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 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 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 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 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 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 case java.sql.Types.DOUBLE: 825 return get_DOUBLE(column); 826 case java.sql.Types.DECIMAL: 827 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 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 float f = get_FLOAT(column); 854 return new java.math.BigDecimal (String.valueOf(f)); 855 case java.sql.Types.DOUBLE: 856 return new java.math.BigDecimal (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 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 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 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 getString(int column) throws SqlException { 929 try { 930 String 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 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 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 se ) { 1000 throw new SqlException(se); 1001 } 1002 } 1003 1004 public final java.io.InputStream getBinaryStream(int column) throws SqlException { 1005 try { 1006 switch (jdbcTypes_[column - 1]) { 1007 case java.sql.Types.BINARY: 1008 return new java.io.ByteArrayInputStream (get_CHAR_FOR_BIT_DATA(column)); 1009 case java.sql.Types.VARBINARY: 1010 case java.sql.Types.LONGVARBINARY: 1011 return new java.io.ByteArrayInputStream (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 se ) { 1020 throw new SqlException(se); 1021 } 1022 } 1023 1024 public final java.io.InputStream 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 (getCHAR(column).getBytes("US-ASCII")); 1033 } catch (java.io.UnsupportedEncodingException 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 (getVARCHAR(column).getBytes("US-ASCII")); 1042 } catch (java.io.UnsupportedEncodingException 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 (get_CHAR_FOR_BIT_DATA(column)); 1049 case java.sql.Types.VARBINARY: 1050 case java.sql.Types.LONGVARBINARY: 1051 return new java.io.ByteArrayInputStream (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 se ) { 1061 throw new SqlException(se); 1062 } 1063 } 1064 1065 public final java.io.InputStream 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 s = c.getSubString(1L, (int) c.length()); 1072 try { 1073 return new java.io.ByteArrayInputStream (s.getBytes("UTF-8")); 1074 } catch (java.io.UnsupportedEncodingException 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 (getCHAR(column).getBytes("UTF-8")); 1084 } catch (java.io.UnsupportedEncodingException 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 (getVARCHAR(column).getBytes("UTF-8")); 1094 } catch (java.io.UnsupportedEncodingException 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 (get_CHAR_FOR_BIT_DATA(column)); 1101 case java.sql.Types.VARBINARY: 1102 case java.sql.Types.LONGVARBINARY: 1103 return new java.io.ByteArrayInputStream (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 se ) { 1112 throw new SqlException(se); 1113 } 1114 } 1115 1116 public final java.io.Reader 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 (getCHAR(column)); 1124 case java.sql.Types.VARCHAR: 1125 case java.sql.Types.LONGVARCHAR: 1126 return new java.io.StringReader (getVARCHAR(column)); 1127 case java.sql.Types.BINARY: 1128 try { 1129 return new java.io.InputStreamReader (new java.io.ByteArrayInputStream (get_CHAR_FOR_BIT_DATA(column)), "UTF-16BE"); 1130 } catch (java.io.UnsupportedEncodingException 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 (new java.io.ByteArrayInputStream (get_VARCHAR_FOR_BIT_DATA(column)), "UTF-16BE"); 1139 } catch (java.io.UnsupportedEncodingException 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 (b.getBinaryStream(), "UTF-16BE"); 1148 } catch (java.io.UnsupportedEncodingException 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 se ) { 1158 throw new SqlException(se); 1159 } 1160 } 1161 1162 public final java.sql.Blob 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 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 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 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 getObject(int column) throws SqlException { 1194 switch (jdbcTypes_[column - 1]) { 1195 case java.sql.Types.SMALLINT: 1196 return new Integer (get_SMALLINT(column)); case java.sql.Types.INTEGER: 1198 return new Integer (get_INTEGER(column)); 1199 case java.sql.Types.BIGINT: 1200 return new Long (get_BIGINT(column)); 1201 case java.sql.Types.REAL: 1202 return new Float (get_FLOAT(column)); 1203 case java.sql.Types.DOUBLE: 1204 return new Double (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 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 charBuffer_ = new char[maxCharLength]; 1249 } 1250 1251 private final String 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 (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 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 |