1 21 package org.apache.derby.client.net; 22 23 import org.apache.derby.iapi.reference.DRDAConstants; 24 import org.apache.derby.client.am.Blob; 25 import org.apache.derby.client.am.Clob; 26 import org.apache.derby.client.am.ColumnMetaData; 27 import org.apache.derby.client.am.ResultSet; 28 import org.apache.derby.client.am.Section; 29 import org.apache.derby.client.am.SqlException; 30 import org.apache.derby.client.am.Types; 31 import org.apache.derby.client.am.ClientMessageId; 32 import org.apache.derby.shared.common.reference.SQLState; 33 34 36 public class NetStatementRequest extends NetPackageRequest implements StatementRequestInterface { 37 java.util.ArrayList extdtaPositions_ = null; int overrideLid_ = FdocaConstants.FIRST_OVERRIDE_LID; 39 40 java.util.HashMap promototedParameters_ = new java.util.HashMap (); 47 48 NetStatementRequest(NetAgent netAgent, CcsidManager ccsidManager, int bufferSize) { 49 super(netAgent, ccsidManager, bufferSize); 50 } 51 52 54 public void writeExecuteImmediate(NetStatement materialStatement, 60 String sql, 61 Section section) throws SqlException { 62 buildEXCSQLIMM(section, 63 false, 0); buildSQLSTTcommandData(sql); } 67 68 public void writePrepareDescribeOutput(NetStatement materialStatement, 75 String sql, 76 Section section) throws SqlException { 77 buildPRPSQLSTT(section, 78 sql, 79 true, true, CodePoint.TYPSQLDA_X_OUTPUT); 83 if (((NetStatement) materialStatement).statement_.cursorAttributesToSendOnPrepare_ != null) { 84 buildSQLATTRcommandData(((NetStatement) materialStatement).statement_.cursorAttributesToSendOnPrepare_); 85 } 86 87 buildSQLSTTcommandData(sql); } 89 90 public void writePrepare(NetStatement materialStatement, 94 String sql, 95 Section section) throws SqlException { 96 buildPRPSQLSTT(section, 97 sql, 98 false, false, 0); 102 if (((NetStatement) materialStatement).statement_.cursorAttributesToSendOnPrepare_ != null) { 103 buildSQLATTRcommandData(((NetStatement) materialStatement).statement_.cursorAttributesToSendOnPrepare_); 104 } 105 106 buildSQLSTTcommandData(sql); } 108 109 public void writeExecute(NetPreparedStatement materialPreparedStatement, 113 Section section, 114 ColumnMetaData parameterMetaData, 115 Object [] inputs, 116 int numInputColumns, 117 boolean outputExpected, 118 boolean chained) throws SqlException { 120 buildEXCSQLSTT(section, 121 true, outputExpected, false, null, false, false, 0, false, 0, false, 0, false, 0); 135 if (numInputColumns > 0) { 136 if ((extdtaPositions_ != null) && (!extdtaPositions_.isEmpty())) { 137 extdtaPositions_.clear(); } 139 140 boolean overrideExists = buildSQLDTAcommandData(numInputColumns, 141 parameterMetaData, 142 inputs); 143 144 buildEXTDTA(parameterMetaData, inputs, chained); 146 } 147 } 148 149 150 public void writeOpenQuery(NetPreparedStatement materialPreparedStatement, 155 Section section, 156 int fetchSize, 157 int resultSetType, 158 int numInputColumns, 159 org.apache.derby.client.am.ColumnMetaData parameterMetaData, 160 Object [] inputs) throws SqlException { 161 boolean sendQryrowset = checkSendQryrowset(fetchSize, resultSetType); 162 fetchSize = checkFetchsize(fetchSize, resultSetType); 163 167 buildOPNQRY(section, 168 sendQryrowset, 169 fetchSize); 170 171 172 ((NetStatement) materialPreparedStatement).qryrowsetSentOnOpnqry_ = sendQryrowset; 174 175 if (numInputColumns > 0) { 176 if ((extdtaPositions_ != null) && (!extdtaPositions_.isEmpty())) { 177 extdtaPositions_.clear(); } 179 182 boolean overrideExists = buildSQLDTAcommandData(numInputColumns, 185 parameterMetaData, 186 inputs); 187 188 buildEXTDTA(parameterMetaData, 191 inputs, 192 false); } 194 } 195 196 public void writeOpenQuery(NetStatement materialStatement, 199 Section section, 200 int fetchSize, 201 int resultSetType) throws SqlException { 202 boolean sendQryrowset = checkSendQryrowset(fetchSize, resultSetType); 203 fetchSize = checkFetchsize(fetchSize, resultSetType); 204 205 buildOPNQRY(section, 209 sendQryrowset, 210 fetchSize); 211 212 213 ((NetStatement) materialStatement).qryrowsetSentOnOpnqry_ = sendQryrowset; 216 217 } 218 219 222 public void writeDescribeInput(NetPreparedStatement materialPreparedStatement, 223 Section section) throws SqlException { 224 int typsqlda = CodePoint.TYPSQLDA_X_INPUT; 225 226 buildDSCSQLSTT(section, 227 true, typsqlda); 229 } 230 231 public void writeDescribeOutput(NetPreparedStatement materialPreparedStatement, 235 Section section) throws SqlException { 236 buildDSCSQLSTT(section, 243 true, CodePoint.TYPSQLDA_X_OUTPUT); } 246 247 public void writeExecuteCall(NetStatement materialStatement, 251 boolean outputExpected, 252 String procedureName, 253 Section section, 254 int fetchSize, 255 boolean suppressResultSets, int resultSetType, 257 ColumnMetaData parameterMetaData, 258 Object [] inputs) throws SqlException { 260 boolean sendQryrowset = true; 262 fetchSize = (fetchSize == 0) ? org.apache.derby.client.am.Configuration.defaultFetchSize : fetchSize; 263 264 boolean sendPrcnam = (procedureName != null) ? true : false; 265 int numParameters = (parameterMetaData != null) ? parameterMetaData.columns_ : 0; 266 outputExpected = numParameters > 0; 267 268 buildEXCSQLSTT(section, 270 true, outputExpected, sendPrcnam, procedureName, true, !suppressResultSets, CodePoint.MAXRSLCNT_NOLIMIT, true, -1, true, calculateResultSetFlags(), sendQryrowset, fetchSize); 284 if (numParameters > 0) { 285 if ((extdtaPositions_ != null) && (!extdtaPositions_.isEmpty())) { 286 extdtaPositions_.clear(); } 288 290 boolean overrideExists = buildSQLDTAcommandData(numParameters, 293 parameterMetaData, 294 inputs); 295 296 buildEXTDTA(parameterMetaData, inputs, false); } 298 299 ((NetStatement) materialStatement).qryrowsetSentOnOpnqry_ = sendQryrowset; 300 } 301 302 318 323 void buildOPNQRY(Section section, 332 boolean sendQueryRowSet, 333 int fetchSize) throws SqlException { 334 createCommand(); 335 markLengthBytes(CodePoint.OPNQRY); 336 337 buildPKGNAMCSN(section); 338 buildQRYBLKSZ(); 340 if (sendQueryRowSet) { 341 buildMAXBLKEXT(-1); 342 buildQRYROWSET(fetchSize); 343 } 344 345 if (netAgent_.netConnection_.serverSupportsQryclsimp()) { 349 buildQRYCLSIMP(); 350 } 351 352 updateLengthBytes(); } 354 355 void buildEXCSQLIMM(Section section, 360 boolean sendQryinsid, 361 long qryinsid) throws SqlException { 362 createCommand(); 363 markLengthBytes(CodePoint.EXCSQLIMM); 364 365 buildPKGNAMCSN(section); 366 buildRDBCMTOK(); 367 if (sendQryinsid) { 368 buildQRYINSID(qryinsid); 369 } 370 371 updateLengthBytes(); 372 } 373 374 void buildPRPSQLSTT(Section section, 381 String sql, 382 boolean sendRtnsqlda, 383 boolean sendTypsqlda, 384 int typsqlda) throws SqlException { 385 createCommand(); 386 markLengthBytes(CodePoint.PRPSQLSTT); 387 388 buildPKGNAMCSN(section); 389 if (sendRtnsqlda) { 390 buildRTNSQLDA(); 391 } 392 if (sendTypsqlda) { 393 buildTYPSQLDA(typsqlda); 394 } 395 396 updateLengthBytes(); 397 } 398 399 void buildEXCSQLSET(Section section) 406 throws SqlException { 407 createCommand(); 408 markLengthBytes(CodePoint.EXCSQLSET); 409 buildPKGNAMCSN(section); updateLengthBytes(); 411 } 412 413 void buildEXCSQLSTT(Section section, 441 boolean sendOutexp, 442 boolean outexp, 443 boolean sendPrcnam, 444 String prcnam, 445 boolean sendQryblksz, 446 boolean sendMaxrslcnt, 447 int maxrslcnt, 448 boolean sendMaxblkext, 449 int maxblkext, 450 boolean sendRslsetflg, 451 int resultSetFlag, 452 boolean sendQryrowset, 453 int qryrowset) throws SqlException { 454 createCommand(); 455 markLengthBytes(CodePoint.EXCSQLSTT); 456 457 buildPKGNAMCSN(section); 458 buildRDBCMTOK(); 459 if (sendOutexp) { 460 buildOUTEXP(outexp); 461 } 462 if (sendQryblksz) { 463 buildQRYBLKSZ(); 464 } 465 if (sendQryrowset && sendMaxblkext) { 466 buildMAXBLKEXT(maxblkext); 467 } 468 if (sendMaxrslcnt) { 469 buildMAXRSLCNT(maxrslcnt); 470 } 471 if (sendRslsetflg) { 472 buildRSLSETFLG(resultSetFlag); 473 } 474 if (sendQryrowset) { 475 buildQRYROWSET(qryrowset); 476 } 477 if (sendPrcnam) { 478 buildPRCNAM(prcnam); 479 } 480 481 updateLengthBytes(); } 483 484 void buildDSCSQLSTT(Section section, 490 boolean sendTypsqlda, 491 int typsqlda) throws SqlException { 492 createCommand(); 493 markLengthBytes(CodePoint.DSCSQLSTT); 494 495 buildPKGNAMCSN(section); 496 if (sendTypsqlda) { 497 buildTYPSQLDA(typsqlda); 498 } 499 500 updateLengthBytes(); 501 } 502 503 boolean buildSQLDTAcommandData(int numInputColumns, 509 ColumnMetaData parameterMetaData, 510 Object [] inputRow) throws SqlException { 511 createEncryptedCommandData(); 512 513 int loc = offset_; 514 515 markLengthBytes(CodePoint.SQLDTA); 516 517 int[][] protocolTypesAndLengths = allocateLidAndLengthsArray(parameterMetaData); 518 519 java.util.Hashtable protocolTypeToOverrideLidMapping = null; 520 java.util.ArrayList mddOverrideArray = null; 521 protocolTypeToOverrideLidMapping = 522 computeProtocolTypesAndLengths(inputRow, parameterMetaData, protocolTypesAndLengths, 523 protocolTypeToOverrideLidMapping); 524 525 boolean overrideExists = false; 526 527 buildFDODSC(numInputColumns, 528 protocolTypesAndLengths, 529 overrideExists, 530 protocolTypeToOverrideLidMapping, 531 mddOverrideArray); 532 533 buildFDODTA(numInputColumns, 534 protocolTypesAndLengths, 535 inputRow); 536 537 updateLengthBytes(); if (netAgent_.netConnection_.getSecurityMechanism() == 539 NetConfiguration.SECMEC_EUSRIDDTA || 540 netAgent_.netConnection_.getSecurityMechanism() == 541 NetConfiguration.SECMEC_EUSRPWDDTA) { 542 encryptDataStream(loc); 543 } 544 545 return overrideExists; 546 } 547 548 private void buildFDODSC(int numColumns, 553 int[][] protocolTypesAndLengths, 554 boolean overrideExists, 555 java.util.Hashtable overrideMap, 556 java.util.ArrayList overrideArray) throws SqlException { 557 markLengthBytes(CodePoint.FDODSC); 558 buildSQLDTA(numColumns, protocolTypesAndLengths, overrideExists, overrideMap, overrideArray); 559 updateLengthBytes(); 560 } 561 562 protected void buildSQLDTA(int numColumns, 566 int[][] lidAndLengthOverrides, 567 boolean overrideExists, 568 java.util.Hashtable overrideMap, 569 java.util.ArrayList overrideArray) throws SqlException { 570 if (overrideExists) { 572 buildMddOverrides(overrideArray); 573 writeBytes(FdocaConstants.MDD_SQLDTAGRP_TOSEND); 574 } 575 576 buildSQLDTAGRP(numColumns, lidAndLengthOverrides, overrideExists, overrideMap); 577 578 if (overrideExists) { 579 writeBytes(FdocaConstants.MDD_SQLDTA_TOSEND); 580 } 581 writeBytes(FdocaConstants.SQLDTA_RLO_TOSEND); 582 } 583 584 protected void buildSQLDTAGRP(int numVars, 587 int[][] lidAndLengthOverrides, 588 boolean mddRequired, 589 java.util.Hashtable overrideMap) throws SqlException { 590 int n = 0; 591 int offset = 0; 592 593 n = calculateColumnsInSQLDTAGRPtriplet(numVars); 594 buildTripletHeader(((3 * n) + 3), 595 FdocaConstants.NGDA_TRIPLET_TYPE, 596 FdocaConstants.SQLDTAGRP_LID); 597 598 do { 599 writeLidAndLengths(lidAndLengthOverrides, n, offset, mddRequired, overrideMap); 600 numVars -= n; 601 if (numVars == 0) { 602 break; 603 } 604 605 offset += n; 606 n = calculateColumnsInSQLDTAGRPtriplet(numVars); 607 buildTripletHeader(((3 * n) + 3), 608 FdocaConstants.CPT_TRIPLET_TYPE, 609 0x00); 610 } while (true); 611 } 612 613 615 616 protected void buildOUTOVR(ResultSet resultSet, 617 ColumnMetaData resultSetMetaData) throws SqlException { 618 createCommandData(); 619 markLengthBytes(CodePoint.OUTOVR); 620 int[][] outputOverrides = 621 calculateOUTOVRLidAndLengthOverrides(resultSet, resultSetMetaData); 622 buildSQLDTARD(resultSetMetaData.columns_, outputOverrides); 623 updateLengthBytes(); 624 } 625 626 private int[][] calculateOUTOVRLidAndLengthOverrides(ResultSet resultSet, 627 ColumnMetaData resultSetMetaData) { 628 int numVars = resultSetMetaData.columns_; 629 int[][] lidAndLengths = new int[numVars][2]; 631 return lidAndLengths; 632 } 633 634 protected void buildSQLDTARD(int numColumns, int[][] lidAndLengthOverrides) throws SqlException { 635 buildSQLCADTA(numColumns, lidAndLengthOverrides); 636 writeBytes(FdocaConstants.SQLDTARD_RLO_TOSEND); 637 } 638 639 protected void buildSQLCADTA(int numColumns, int[][] lidAndLengthOverrides) throws SqlException { 640 buildSQLDTAGRP(numColumns, lidAndLengthOverrides, false, null); writeBytes(FdocaConstants.SQLCADTA_RLO_TOSEND); 642 } 643 644 private void buildFDODTA(int numVars, 645 int[][] protocolTypesAndLengths, 646 Object [] inputs) throws SqlException { 647 try 648 { 649 long dataLength = 0; 650 Object o = null; 651 652 markLengthBytes(CodePoint.FDODTA); 653 write1Byte(FdocaConstants.NULL_LID); 655 for (int i = 0; i < numVars; i++) { 657 if (inputs[i] == null) { 658 if ((protocolTypesAndLengths[i][0] % 2) == 1) { 659 write1Byte(FdocaConstants.NULL_DATA); 660 } else { 661 } 663 } else { 664 if ((protocolTypesAndLengths[i][0] % 2) == 1) { 665 write1Byte(FdocaConstants.INDICATOR_NULLABLE); 666 } 667 668 switch (protocolTypesAndLengths[i][0] | 0x01) { case DRDAConstants.DRDA_TYPE_NVARMIX: 670 case DRDAConstants.DRDA_TYPE_NLONGMIX: 671 o = retrievePromotedParameterIfExists(i); 674 if (o == null) { 675 writeSingleorMixedCcsidLDString((String ) inputs[i], netAgent_.typdef_.getCcsidMbcEncoding()); 676 } else { Clob c = (Clob) o; 678 dataLength = c.length(); 679 setFDODTALobLength(protocolTypesAndLengths, i, dataLength); 680 } 681 break; 682 683 case DRDAConstants.DRDA_TYPE_NVARCHAR: 684 case DRDAConstants.DRDA_TYPE_NLONG: 685 o = retrievePromotedParameterIfExists(i); 686 if (o == null) { 687 688 } else { dataLength = ((Clob) o).length(); 690 setFDODTALobLength(protocolTypesAndLengths, i, dataLength); 691 } 692 break; 693 694 case DRDAConstants.DRDA_TYPE_NINTEGER: 695 writeIntFdocaData(((Integer ) inputs[i]).intValue()); 696 break; 697 case DRDAConstants.DRDA_TYPE_NSMALL: 698 writeShortFdocaData(((Short ) inputs[i]).shortValue()); 699 break; 700 case DRDAConstants.DRDA_TYPE_NFLOAT4: 701 writeFloat(((Float ) inputs[i]).floatValue()); 702 break; 703 case DRDAConstants.DRDA_TYPE_NFLOAT8: 704 writeDouble(((Double ) inputs[i]).doubleValue()); 705 break; 706 case DRDAConstants.DRDA_TYPE_NDECIMAL: 707 writeBigDecimal((java.math.BigDecimal ) inputs[i], 708 (protocolTypesAndLengths[i][1] >> 8) & 0xff, protocolTypesAndLengths[i][1] & 0xff); break; 711 case DRDAConstants.DRDA_TYPE_NDATE: 712 writeDate((java.sql.Date ) inputs[i]); 713 break; 714 case DRDAConstants.DRDA_TYPE_NTIME: 715 writeTime((java.sql.Time ) inputs[i]); 716 break; 717 case DRDAConstants.DRDA_TYPE_NTIMESTAMP: 718 writeTimestamp((java.sql.Timestamp ) inputs[i]); 719 break; 720 case DRDAConstants.DRDA_TYPE_NINTEGER8: 721 writeLongFdocaData(((Long ) inputs[i]).longValue()); 722 break; 723 case DRDAConstants.DRDA_TYPE_NVARBYTE: 724 case DRDAConstants.DRDA_TYPE_NLONGVARBYTE: 725 o = retrievePromotedParameterIfExists(i); 726 if (o == null) { 727 writeLDBytes((byte[]) inputs[i]); 728 } else { Blob b = (Blob) o; 730 dataLength = b.length(); 731 setFDODTALobLength(protocolTypesAndLengths, i, dataLength); 732 } 733 break; 734 case DRDAConstants.DRDA_TYPE_NLOBCSBCS: 735 case DRDAConstants.DRDA_TYPE_NLOBCDBCS: 736 o = retrievePromotedParameterIfExists(i); 738 if (o == null) { 739 try { 740 dataLength = ((java.sql.Clob ) inputs[i]).length(); 741 } catch (java.sql.SQLException e) { 742 throw new SqlException(netAgent_.logWriter_, 743 new ClientMessageId(SQLState.NET_ERROR_GETTING_BLOB_LENGTH), 744 e); 745 } 746 } else { 747 dataLength = ((Clob) o).length(); 748 } 749 setFDODTALobLength(protocolTypesAndLengths, i, dataLength); 750 break; 751 case DRDAConstants.DRDA_TYPE_NLOBBYTES: 752 o = retrievePromotedParameterIfExists(i); 754 if (o == null) { 755 try { 756 dataLength = ((java.sql.Blob ) inputs[i]).length(); 757 } catch (java.sql.SQLException e) { 758 throw new SqlException(netAgent_.logWriter_, 759 new ClientMessageId(SQLState.NET_ERROR_GETTING_BLOB_LENGTH), 760 e); 761 } 762 } else { dataLength = ((Blob) o).length(); 764 } 765 setFDODTALobLength(protocolTypesAndLengths, i, dataLength); 766 break; 767 case DRDAConstants.DRDA_TYPE_NLOBCMIXED: 768 o = retrievePromotedParameterIfExists(i); 770 if (o == null) { 771 if (((Clob) inputs[i]).isString()) { 772 dataLength = ((Clob) inputs[i]).getUTF8Length(); 773 } else { 775 dataLength = ((Clob) inputs[i]).length(); 776 } 777 } else { dataLength = ((Clob) o).length(); 779 } 780 setFDODTALobLength(protocolTypesAndLengths, i, dataLength); 781 break; 782 default: 783 throw new SqlException(netAgent_.logWriter_, 784 new ClientMessageId(SQLState.NET_UNRECOGNIZED_JDBC_TYPE), 785 new Integer (protocolTypesAndLengths[i][0]), 786 new Integer (numVars), new Integer (i)); 787 } 788 } 789 } 790 updateLengthBytes(); } 792 catch ( java.sql.SQLException se ) 793 { 794 throw new SqlException(se); 795 } 796 } 797 798 private void buildEXTDTA(ColumnMetaData parameterMetaData, 800 Object [] inputRow, 801 boolean chained) throws SqlException { 802 try 803 { 804 if (extdtaPositions_ != null) { 806 boolean chainFlag, chainedWithSameCorrelator; 807 808 for (int i = 0; i < extdtaPositions_.size(); i++) { 809 int index = ((Integer ) extdtaPositions_.get(i)).intValue(); 810 811 if (i != extdtaPositions_.size() - 1) { chainFlag = true; 814 chainedWithSameCorrelator = true; 815 } else { chainFlag = chained; 817 chainedWithSameCorrelator = false; 818 } 819 820 boolean writeNullByte = false; 822 if (parameterMetaData.nullable_[index]) { 823 writeNullByte = true; 824 } 825 int parameterType = parameterMetaData.clientParamtertype_[index]; 828 if (parameterType == 0) { 829 parameterType = parameterMetaData.types_[index]; 830 } 831 832 if (parameterType == Types.BLOB 834 || parameterType == Types.BINARY 835 || parameterType == Types.VARBINARY 836 || parameterType == Types.LONGVARBINARY) { 837 Blob o = (Blob) retrievePromotedParameterIfExists(index); 838 java.sql.Blob b = (o == null) ? (java.sql.Blob ) inputRow[index] : o; 839 boolean isExternalBlob = !(b instanceof org.apache.derby.client.am.Blob); 840 if (isExternalBlob) { 841 try { 842 writeScalarStream(chainFlag, 843 chainedWithSameCorrelator, 844 CodePoint.EXTDTA, 845 (int) b.length(), 846 b.getBinaryStream(), 847 writeNullByte, 848 index + 1); 849 } catch (java.sql.SQLException e) { 850 throw new SqlException(netAgent_.logWriter_, 851 new ClientMessageId(SQLState.NET_ERROR_GETTING_BLOB_LENGTH), 852 e); 853 } 854 } else if (((Blob) b).isBinaryStream()) { 855 writeScalarStream(chainFlag, 856 chainedWithSameCorrelator, 857 CodePoint.EXTDTA, 858 (int) ((Blob) b).length(), 859 ((Blob) b).getBinaryStream(), 860 writeNullByte, 861 index + 1); 862 } else { writeScalarStream(chainFlag, 869 chainedWithSameCorrelator, 870 CodePoint.EXTDTA, 871 (int) ((Blob) b).length(), 872 ((Blob) b).getBinaryStream(), 873 writeNullByte, 874 index + 1); 875 } 876 } 877 else if ( 879 parameterType == Types.CLOB 880 || parameterType == Types.CHAR 881 || parameterType == Types.VARCHAR 882 || parameterType == Types.LONGVARCHAR) { 883 Clob o = (Clob) retrievePromotedParameterIfExists(index); 884 java.sql.Clob c = (o == null) ? (java.sql.Clob ) inputRow[index] : o; 885 boolean isExternalClob = !(c instanceof org.apache.derby.client.am.Clob); 886 887 if (isExternalClob) { 888 try { 889 writeScalarStream(chainFlag, 890 chainedWithSameCorrelator, 891 CodePoint.EXTDTA, 892 (int) c.length(), 893 c.getCharacterStream(), 894 writeNullByte, 895 index + 1); 896 } catch (java.sql.SQLException e) { 897 throw new SqlException(netAgent_.logWriter_, 898 new ClientMessageId(SQLState.NET_ERROR_GETTING_BLOB_LENGTH), 899 e); 900 } 901 } else if (((Clob) c).isCharacterStream()) { 902 writeScalarStream(chainFlag, 903 chainedWithSameCorrelator, 904 CodePoint.EXTDTA, 905 (int) ((Clob) c).length(), 906 ((Clob) c).getCharacterStream(), 907 writeNullByte, 908 index + 1); 909 } else if (((Clob) c).isAsciiStream()) { 910 writeScalarStream(chainFlag, 911 chainedWithSameCorrelator, 912 CodePoint.EXTDTA, 913 (int) ((Clob) c).length(), 914 ((Clob) c).getAsciiStream(), 915 writeNullByte, 916 index + 1); 917 } else if (((Clob) c).isUnicodeStream()) { 918 writeScalarStream(chainFlag, 919 chainedWithSameCorrelator, 920 CodePoint.EXTDTA, 921 (int) ((Clob) c).length(), 922 ((Clob) c).getUnicodeStream(), 923 writeNullByte, 924 index + 1); 925 } else { writeScalarStream(chainFlag, 931 chainedWithSameCorrelator, 932 CodePoint.EXTDTA, 933 (int) ((Clob) c).getUTF8Length(), 934 new java.io.ByteArrayInputStream (((Clob) c).getUtf8String()), 935 writeNullByte, 936 index + 1); 937 } 938 } 939 } 940 } 941 } 942 catch ( java.sql.SQLException se ) 943 { 944 throw new SqlException(se); 945 } 946 } 947 948 949 private Object retrievePromotedParameterIfExists(int index) { 952 953 if (promototedParameters_.isEmpty()) { 955 return null; 956 } 957 return promototedParameters_.get(new Integer (index)); 958 } 959 960 private int calculateColumnsInSQLDTAGRPtriplet(int numVars) { 961 if (numVars > FdocaConstants.MAX_VARS_IN_NGDA) { 963 return FdocaConstants.MAX_VARS_IN_NGDA; 964 } 965 return numVars; 966 } 967 968 969 private java.util.Hashtable computeProtocolTypesAndLengths(Object [] inputRow, 974 ColumnMetaData parameterMetaData, 975 int[][] lidAndLengths, 976 java.util.Hashtable overrideMap) throws SqlException { 977 try 978 { 979 int numVars = parameterMetaData.columns_; 980 String s = null; 981 if (!promototedParameters_.isEmpty()) { 982 promototedParameters_.clear(); 983 } 984 985 for (int i = 0; i < numVars; i++) { 986 987 int jdbcType; 988 jdbcType = parameterMetaData.clientParamtertype_[i]; 991 if (jdbcType == 0) { 992 jdbcType = parameterMetaData.types_[i]; 993 } 994 995 1001 if (jdbcType == 0) { 1002 throw new SqlException(netAgent_.logWriter_, 1003 new ClientMessageId(SQLState.NET_INVALID_JDBC_TYPE_FOR_PARAM), 1004 new Integer (i)); 1005 } 1006 1007 switch (jdbcType) { 1008 case java.sql.Types.CHAR: 1009 case java.sql.Types.VARCHAR: 1010 s = (String ) inputRow[i]; 1014 if (s == null || s.length() <= 32767 / 3) { 1017 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NVARMIX; 1018 lidAndLengths[i][1] = 32767; 1019 } else { 1020 java.io.ByteArrayInputStream bais = null; 1022 byte[] ba = null; 1023 try { 1024 ba = s.getBytes("UTF-8"); 1025 bais = new java.io.ByteArrayInputStream (ba); 1026 Clob c = new Clob(netAgent_, bais, "UTF-8", ba.length); 1027 promototedParameters_.put(new Integer (i), c); 1031 1032 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NLOBCMIXED; 1033 lidAndLengths[i][1] = buildPlaceholderLength(c.length()); 1034 } catch (java.io.UnsupportedEncodingException e) { 1035 throw new SqlException(netAgent_.logWriter_, 1036 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 1037 "byte array", "Clob", e); 1038 } 1039 } 1040 break; 1041 case java.sql.Types.INTEGER: 1042 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NINTEGER; 1045 lidAndLengths[i][1] = 4; 1046 break; 1047 case java.sql.Types.BOOLEAN: 1048 case java.sql.Types.SMALLINT: 1049 case java.sql.Types.TINYINT: 1050 case java.sql.Types.BIT: 1051 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NSMALL; 1054 lidAndLengths[i][1] = 2; 1055 break; 1056 case java.sql.Types.REAL: 1057 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NFLOAT4; 1060 lidAndLengths[i][1] = 4; 1061 break; 1062 case java.sql.Types.DOUBLE: 1063 case java.sql.Types.FLOAT: 1064 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NFLOAT8; 1067 lidAndLengths[i][1] = 8; 1068 break; 1069 case java.sql.Types.NUMERIC: 1070 case java.sql.Types.DECIMAL: 1071 1092 int precision = parameterMetaData.sqlPrecision_[i]; 1096 int scale = parameterMetaData.sqlScale_[i]; 1097 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NDECIMAL; 1098 lidAndLengths[i][1] = (precision << 8) + (scale << 0); 1099 break; 1100 case java.sql.Types.DATE: 1101 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NDATE; 1105 lidAndLengths[i][1] = 10; 1106 break; 1107 case java.sql.Types.TIME: 1108 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NTIME; 1112 lidAndLengths[i][1] = 8; 1113 break; 1114 case java.sql.Types.TIMESTAMP: 1115 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NTIMESTAMP; 1119 lidAndLengths[i][1] = 26; 1120 break; 1121 case java.sql.Types.BIGINT: 1122 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NINTEGER8; 1126 lidAndLengths[i][1] = 8; 1127 break; 1128 case java.sql.Types.LONGVARCHAR: 1129 s = (String ) inputRow[i]; 1131 if (s == null || s.length() <= 32767 / 3) { 1132 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NLONGMIX; 1133 lidAndLengths[i][1] = 32767; 1134 } else { 1135 java.io.ByteArrayInputStream bais = null; 1137 byte[] ba = null; 1138 try { 1139 ba = s.getBytes("UTF-8"); 1140 bais = new java.io.ByteArrayInputStream (ba); 1141 Clob c = new Clob(netAgent_, bais, "UTF-8", ba.length); 1142 1143 promototedParameters_.put(new Integer (i), c); 1147 1148 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NLOBCMIXED; 1149 lidAndLengths[i][1] = buildPlaceholderLength(c.length()); 1150 } catch (java.io.UnsupportedEncodingException e) { 1151 throw new SqlException(netAgent_.logWriter_, 1152 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 1153 "byte array", "Clob"); 1154 } 1155 } 1156 break; 1157 case java.sql.Types.BINARY: 1158 case java.sql.Types.VARBINARY: 1159 byte[] ba = (byte[]) inputRow[i]; 1160 if (ba == null) { 1161 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NVARBYTE; 1162 lidAndLengths[i][1] = 32767; 1163 } else if (ba.length <= 32767) { 1164 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NVARBYTE; 1165 lidAndLengths[i][1] = 32767; 1166 } else { 1167 Blob b = new Blob(ba, netAgent_, 0); 1169 1170 promototedParameters_.put(new Integer (i), b); 1174 1175 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NLOBBYTES; 1176 lidAndLengths[i][1] = buildPlaceholderLength(ba.length); 1177 } 1178 break; 1179 case java.sql.Types.LONGVARBINARY: 1180 ba = (byte[]) inputRow[i]; 1181 if (ba == null) { 1182 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NLONGVARBYTE; 1183 lidAndLengths[i][1] = 32767; 1184 } else if (ba.length <= 32767) { 1185 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NLONGVARBYTE; 1186 lidAndLengths[i][1] = 32767; 1187 } else { 1188 Blob b = new Blob(ba, netAgent_, 0); 1190 1191 promototedParameters_.put(new Integer (i), b); 1195 1196 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NLOBBYTES; 1197 lidAndLengths[i][1] = buildPlaceholderLength(ba.length); 1198 } 1199 break; 1200 case java.sql.Types.BLOB: 1201 java.sql.Blob b = (java.sql.Blob ) inputRow[i]; 1202 if (b == null) { 1203 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NLOBBYTES; 1204 lidAndLengths[i][1] = 1205 buildPlaceholderLength(parameterMetaData.sqlLength_[i]); 1206 } else { 1207 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NLOBBYTES; 1208 try { 1209 lidAndLengths[i][1] = buildPlaceholderLength(b.length()); 1210 } catch (java.sql.SQLException e) { 1211 throw new SqlException(netAgent_.logWriter_, 1212 new ClientMessageId(SQLState.NET_ERROR_GETTING_BLOB_LENGTH), e); 1213 } 1214 } 1215 break; 1216 case java.sql.Types.CLOB: 1217 { 1218 java.sql.Clob c = (java.sql.Clob ) inputRow[i]; 1220 boolean isExternalClob = !(c instanceof org.apache.derby.client.am.Clob); 1221 long lobLength = 0; 1222 if (c == null) { 1223 lobLength = parameterMetaData.sqlLength_[i]; 1224 } else if (isExternalClob) { 1225 try { 1226 lobLength = c.length(); 1227 } catch (java.sql.SQLException e) { 1228 throw new SqlException(netAgent_.logWriter_, 1229 new ClientMessageId(SQLState.NET_ERROR_GETTING_BLOB_LENGTH), 1230 e); 1231 } 1232 } else { 1233 lobLength = ((Clob) c).length(); 1234 } 1235 if (c == null) { 1236 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NLOBCMIXED; 1237 lidAndLengths[i][1] = buildPlaceholderLength(lobLength); 1238 } else if (isExternalClob) { 1239 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NLOBCDBCS; 1240 lidAndLengths[i][1] = buildPlaceholderLength(lobLength); 1241 } else if (((Clob) c).isCharacterStream()) { 1242 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NLOBCDBCS; 1243 lidAndLengths[i][1] = buildPlaceholderLength(lobLength); 1244 } else if (((Clob) c).isUnicodeStream()) { 1245 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NLOBCMIXED; 1246 lidAndLengths[i][1] = buildPlaceholderLength(lobLength); 1247 } else if (((Clob) c).isAsciiStream()) { 1248 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NLOBCSBCS; 1249 lidAndLengths[i][1] = buildPlaceholderLength(lobLength); 1250 } else if (((Clob) c).isString()) { 1251 lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NLOBCMIXED; 1252 lidAndLengths[i][1] = buildPlaceholderLength(((Clob) c).getUTF8Length()); 1253 } 1254 } 1255 break; 1256 default : 1257 throw new SqlException(netAgent_.logWriter_, 1258 new ClientMessageId(SQLState.UNRECOGNIZED_JAVA_SQL_TYPE), 1259 new Integer (jdbcType)); 1260 } 1261 1262 if (!parameterMetaData.nullable_[i]) { 1263 lidAndLengths[i][0]--; 1264 } 1265 } 1266 return overrideMap; 1267 } 1268 catch ( java.sql.SQLException se ) 1269 { 1270 throw new SqlException(se); 1271 } 1272 } 1273 1274 private int buildPlaceholderLength(long totalLength) { 1275 if (totalLength < 0x7fff) { 1276 return 0x8002; } else if (totalLength < 0x7fffffff) { 1278 return 0x8004; } else if (totalLength < 0x7fffffffffffL) { 1280 return 0x8006; 1281 } else { 1282 return 0x8008; } 1284 } 1285 1286 private void buildOUTEXP(boolean outputExpected) throws SqlException { 1298 if (outputExpected) { 1299 writeScalar1Byte(CodePoint.OUTEXP, CodePoint.TRUE); 1300 } 1301 } 1302 1303 void buildMAXBLKEXT(int maxNumOfExtraBlocks) throws SqlException { 1317 if (maxNumOfExtraBlocks != 0) { 1318 writeScalar2Bytes(CodePoint.MAXBLKEXT, maxNumOfExtraBlocks); 1319 } 1320 } 1321 1322 void buildQRYROWSET(int fetchSize) throws SqlException { 1324 writeScalar4Bytes(CodePoint.QRYROWSET, fetchSize); 1325 } 1326 1327 private void buildPRCNAM(String prcnam) throws SqlException { 1342 if (prcnam == null) { 1343 throw new SqlException(netAgent_.logWriter_, 1344 new ClientMessageId(SQLState.NET_NULL_PROCEDURE_NAME)); 1345 } 1346 1347 int prcnamLength = prcnam.length(); 1348 if ((prcnamLength == 0) || (prcnamLength > 255)) { 1349 throw new SqlException(netAgent_.logWriter_, 1350 new ClientMessageId(SQLState.NET_PROCEDURE_NAME_LENGTH_OUT_OF_RANGE), 1351 new Integer (prcnamLength), new Integer (255)); 1352 } 1353 1354 writeScalarString(CodePoint.PRCNAM, prcnam); 1355 } 1356 1357 1358 void buildQRYBLKSZ() throws SqlException { 1369 writeScalar4Bytes(CodePoint.QRYBLKSZ, DssConstants.MAX_DSS_LEN); 1370 } 1371 1372 private void buildMAXRSLCNT(int maxResultSetCount) throws SqlException { 1388 if (maxResultSetCount == 0) { 1389 return; 1390 } 1391 writeScalar2Bytes(CodePoint.MAXRSLCNT, maxResultSetCount); 1392 } 1393 1394 private void buildRDBCMTOK() throws SqlException { 1398 writeScalar1Byte(CodePoint.RDBCMTOK, CodePoint.TRUE); 1399 } 1400 1401 private void buildRSLSETFLG(int resultSetFlag) throws SqlException { 1431 writeScalar1Byte(CodePoint.RSLSETFLG, resultSetFlag); 1432 } 1433 1434 void buildQRYINSID(long qryinsid) throws SqlException { 1435 markLengthBytes(CodePoint.QRYINSID); 1436 writeLong(qryinsid); 1437 updateLengthBytes(); 1438 } 1439 1440 1441 private void buildRTNSQLDA() throws SqlException { 1452 writeScalar1Byte(CodePoint.RTNSQLDA, CodePoint.TRUE); 1453 } 1454 1455 private void buildTYPSQLDA(int typeSqlda) throws SqlException { 1474 if (typeSqlda != CodePoint.TYPSQLDA_STD_OUTPUT) { 1476 writeScalar1Byte(CodePoint.TYPSQLDA, typeSqlda); 1477 } 1478 } 1479 1480 1485 private void buildQRYCLSIMP() { 1486 writeScalar1Byte(CodePoint.QRYCLSIMP, CodePoint.QRYCLSIMP_YES); 1487 } 1488 1489 private void setFDODTALobLength(int[][] protocolTypesAndLengths, int i, long dataLength) throws SqlException { 1491 if (protocolTypesAndLengths[i][1] == 0x8002) { 1492 writeShort((short) dataLength); 1493 } else if (protocolTypesAndLengths[i][1] == 0x8004) { 1494 writeInt((int) dataLength); } else if (protocolTypesAndLengths[i][1] == 0x8006) { 1497 writeLong(dataLength); 1498 } 1499 else if (protocolTypesAndLengths[i][1] == 0x8008) { 1502 writeLong(dataLength); 1503 } 1504 1505 if (dataLength != 0) { 1506 if (extdtaPositions_ == null) { 1507 extdtaPositions_ = new java.util.ArrayList (); 1508 } 1509 extdtaPositions_.add(new Integer (i)); 1510 } 1511 } 1512 1513 private boolean checkSendQryrowset(int fetchSize, 1514 int resultSetType) { 1515 boolean sendQryrowset = false; 1519 if (resultSetType != java.sql.ResultSet.TYPE_FORWARD_ONLY) { 1520 sendQryrowset = true; 1521 } 1522 return sendQryrowset; 1523 } 1524 1525 private int checkFetchsize(int fetchSize, int resultSetType) { 1526 if (resultSetType != java.sql.ResultSet.TYPE_FORWARD_ONLY && fetchSize == 0) { 1528 fetchSize = org.apache.derby.client.am.Configuration.defaultFetchSize; 1529 } 1530 return fetchSize; 1531 } 1532 1533 private int calculateResultSetFlags() { 1534 return CodePoint.RSLSETFLG_EXTENDED_SQLDA; 1535 } 1536 1537 public void writeSetSpecialRegister(java.util.ArrayList sqlsttList) throws SqlException { 1538 Section section = 1539 netAgent_.sectionManager_.getDynamicSection(java.sql.ResultSet.HOLD_CURSORS_OVER_COMMIT); 1540 1541 buildEXCSQLSET(section); 1542 1543 for (int i = 0; i < sqlsttList.size(); i++) { 1545 buildSQLSTTcommandData((String ) sqlsttList.get(i)); 1546 } 1547 } 1548 1549 private int[][] allocateLidAndLengthsArray(ColumnMetaData parameterMetaData) { 1550 int numVars = parameterMetaData.columns_; 1551 int[][] lidAndLengths = parameterMetaData.protocolTypesCache_; 1552 if ((lidAndLengths) == null || (lidAndLengths.length != numVars)) { 1553 lidAndLengths = new int[numVars][2]; 1554 parameterMetaData.protocolTypesCache_ = lidAndLengths; 1555 } 1556 return lidAndLengths; 1557 } 1558 1559 private void buildMddOverrides(java.util.ArrayList sdaOverrides) throws SqlException { 1560 byte[] mddBytes; 1561 for (int i = 0; i < sdaOverrides.size(); i++) { 1562 mddBytes = (byte[]) (sdaOverrides.get(i)); 1563 writeBytes(mddBytes); 1564 } 1565 } 1566 1567 private int getNextOverrideLid() { 1568 return overrideLid_++; 1569 } 1570} 1571 1572 1573 | Popular Tags |