1 21 22 package org.apache.derby.impl.drda; 23 24 import java.io.UnsupportedEncodingException ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.Method ; 27 import java.math.BigInteger ; 28 import java.sql.CallableStatement ; 29 import java.sql.Connection ; 30 import java.sql.PreparedStatement ; 31 import java.sql.ResultSet ; 32 import java.sql.SQLException ; 33 import java.sql.Statement ; 34 import java.util.ArrayList ; 35 import java.util.Hashtable ; 36 import java.util.StringTokenizer ; 37 import java.util.Vector ; 38 39 import org.apache.derby.iapi.jdbc.BrokeredConnection; 40 import org.apache.derby.iapi.jdbc.BrokeredPreparedStatement; 41 import org.apache.derby.iapi.jdbc.EngineConnection; 42 import org.apache.derby.iapi.jdbc.EngineStatement; 43 import org.apache.derby.iapi.jdbc.EnginePreparedStatement; 44 import org.apache.derby.iapi.jdbc.EngineParameterMetaData; 45 import org.apache.derby.iapi.reference.JDBC30Translation; 46 import org.apache.derby.iapi.sql.execute.ExecutionContext; 47 import org.apache.derby.iapi.util.StringUtil; 48 import org.apache.derby.impl.jdbc.Util; 49 50 53 class DRDAStatement 54 { 55 56 62 63 protected String typDefNam; protected int byteOrder; protected int ccsidSBC; protected int ccsidDBC; protected int ccsidMBC; protected String ccsidSBCEncoding; protected String ccsidDBCEncoding; protected String ccsidMBCEncoding; 72 protected Database database; private Pkgnamcsn pkgnamcsn; protected ConsistencyToken pkgcnstkn; protected String pkgid; protected int pkgsn; int withHoldCursor = -1; protected int isolationLevel; protected String cursorName; 80 protected int scrollType = ResultSet.TYPE_FORWARD_ONLY; protected int concurType = ResultSet.CONCUR_READ_ONLY;; protected long rowCount; protected byte [] rslsetflg; protected int maxrslcnt; protected PreparedStatement ps; protected EngineParameterMetaData stmtPmeta; protected boolean isCall; 88 protected String procName; private int[] outputTypes; protected static int NOT_OUTPUT_PARAM = -100000; 92 protected boolean outputExpected; private Statement stmt; 95 96 private DRDAResultSet currentDrdaRs; private Hashtable resultSetTable; private ArrayList resultSetKeyList; private int numResultSets = 0; 100 101 protected Vector cliParamDrdaTypes = new Vector (); 103 protected Vector cliParamLens = new Vector (); 104 protected ArrayList cliParamExtPositions = null; 105 106 protected int nbrrow; protected int qryrowset; protected int blksize; protected int maxblkext; protected int outovropt; protected boolean qryrfrtbl; private int qryprctyp = CodePoint.QRYBLKCTL_DEFAULT; 117 118 119 boolean needsToSendParamData = false; 120 boolean explicitlyPrepared = false; 122 129 DRDAStatement (Database database) 130 { 131 this.database = database; 132 setTypDefValues(); 133 this.currentDrdaRs = new DRDAResultSet(); 134 } 135 136 140 protected void setTypDefValues() 141 { 142 this.typDefNam = database.typDefNam; 144 this.byteOrder = database.byteOrder; 145 this.ccsidSBC = database.ccsidSBC; 146 this.ccsidDBC = database.ccsidDBC; 147 this.ccsidMBC = database.ccsidMBC; 148 this.ccsidSBCEncoding = database.ccsidSBCEncoding; 149 this.ccsidDBCEncoding = database.ccsidDBCEncoding; 150 this.ccsidMBCEncoding = database.ccsidMBCEncoding; 151 } 152 157 protected void setDatabase(Database database) 158 { 159 this.database = database; 160 setTypDefValues(); 161 } 162 168 protected void setStatement(Connection conn) 169 throws SQLException 170 { 171 stmt = conn.createStatement(); 172 if (cursorName != null) 174 stmt.setCursorName(cursorName); 175 } 176 182 protected Statement getStatement() 183 throws SQLException 184 { 185 return stmt; 186 } 187 188 192 193 protected void setRsDefaultOptions(DRDAResultSet drs) 194 { 195 drs.nbrrow = nbrrow; 196 drs.qryrowset = qryrowset; 197 drs.blksize = blksize; 198 drs.maxblkext = maxblkext; 199 drs.outovropt = outovropt; 200 drs.rslsetflg = rslsetflg; 201 drs.scrollType = scrollType; 202 drs.concurType = concurType; 203 drs.setQryprctyp(qryprctyp); 204 drs.qryrowset = qryrowset; 205 } 206 207 212 protected ArrayList getExtDtaObjects() 213 { 214 return currentDrdaRs.getExtDtaObjects(); 215 } 216 217 220 protected void setExtDtaObjects(ArrayList a) 221 { 222 currentDrdaRs.setExtDtaObjects(a); 223 } 224 225 public void setSplitQRYDTA(byte []data) 226 { 227 currentDrdaRs.setSplitQRYDTA(data); 228 } 229 public byte[]getSplitQRYDTA() 230 { 231 return currentDrdaRs.getSplitQRYDTA(); 232 } 233 234 239 protected void addExtDtaObject (Object o, int jdbcIndex ) 240 { 241 currentDrdaRs.addExtDtaObject(o,jdbcIndex); 242 } 243 244 245 248 protected void clearExtDtaObjects () 249 { 250 currentDrdaRs.clearExtDtaObjects(); 251 } 252 253 260 protected int getResultSetHoldability() throws SQLException 261 { 262 Statement rsstmt; 263 ResultSet rs = getResultSet(); 264 265 if (rs != null) 266 rsstmt = rs.getStatement(); 267 else 268 rsstmt = getPreparedStatement(); 269 270 int holdValue = 271 ((EngineStatement) rsstmt).getResultSetHoldability(); 272 273 return holdValue; 274 } 275 276 284 int getResultSetHoldability(ResultSet rs) throws SQLException 285 { 286 Statement rsstmt; 287 288 if (rs != null) 289 rsstmt = rs.getStatement(); 290 else 291 rsstmt = getPreparedStatement(); 292 293 int holdValue = 294 ((EngineStatement) rsstmt).getResultSetHoldability(); 295 296 return holdValue; 297 } 298 299 304 protected boolean isExtDtaValueNullable(int index) 305 { 306 return currentDrdaRs.isExtDtaValueNullable(index); 307 } 308 309 310 322 protected void setOPNQRYOptions(int blksize, int qryblkctl, 323 int maxblkext, int outovropt,int qryrowset,int qryclsimpl) 324 { 325 this.blksize = blksize; 326 this.qryprctyp = qryblkctl; 327 this.maxblkext = maxblkext; 328 this.outovropt = outovropt; 329 this.qryrowset = qryrowset; 330 currentDrdaRs.setOPNQRYOptions( blksize, qryblkctl, maxblkext, 331 outovropt, qryrowset, qryclsimpl); 332 } 333 334 337 protected void setQueryOptions(int blksize, boolean qryrelscr, 338 long qryrownbr, 339 boolean qryfrtbl,int nbrrow,int maxblkext, 340 int qryscrorn, boolean qryrowsns, 341 boolean qryblkrst, 342 boolean qryrtndta,int qryrowset, 343 int rtnextdta) 344 { 345 currentDrdaRs.blksize = blksize; 346 currentDrdaRs.qryrelscr = qryrelscr; 347 currentDrdaRs.qryrownbr = qryrownbr; 348 currentDrdaRs.qryrfrtbl = qryrfrtbl; 349 currentDrdaRs.nbrrow = nbrrow; 350 currentDrdaRs.maxblkext = maxblkext; 351 currentDrdaRs.qryscrorn = qryscrorn; 352 currentDrdaRs.qryrowsns = qryrowsns; 353 currentDrdaRs.qryblkrst = qryblkrst; 354 currentDrdaRs.qryrtndta = qryrtndta; 355 currentDrdaRs.qryrowset = qryrowset; 356 currentDrdaRs.rtnextdta = rtnextdta; 357 } 358 359 360 361 protected void setQryprctyp(int qryprctyp) 362 { 363 this.qryprctyp = qryprctyp; 364 currentDrdaRs.setQryprctyp(qryprctyp); 365 } 366 367 protected int getQryprctyp() 368 throws SQLException 369 { 370 return currentDrdaRs.getQryprctyp(); 371 } 372 373 protected void setQryrownbr(long qryrownbr) 374 { 375 currentDrdaRs.qryrownbr = qryrownbr; 376 } 377 378 protected long getQryrownbr() 379 { 380 return currentDrdaRs.qryrownbr; 381 } 382 383 384 protected int getQryrowset() 385 { 386 return currentDrdaRs.qryrowset; 387 } 388 389 390 protected int getBlksize() 391 { 392 return currentDrdaRs.blksize; 393 } 394 395 protected void setQryrtndta(boolean qryrtndta) 396 { 397 currentDrdaRs.qryrtndta = qryrtndta; 398 } 399 400 protected boolean getQryrtndta() 401 { 402 return currentDrdaRs.qryrtndta; 403 } 404 405 406 protected void setQryscrorn(int qryscrorn) 407 { 408 currentDrdaRs.qryscrorn = qryscrorn; 409 } 410 411 protected int getQryscrorn() 412 { 413 return currentDrdaRs.qryscrorn; 414 } 415 416 protected void setScrollType(int scrollType) 417 { 418 currentDrdaRs.scrollType = scrollType; 419 } 420 421 protected int getScrollType() 422 { 423 return currentDrdaRs.scrollType; 424 } 425 426 430 protected boolean isScrollable() 431 { 432 return (getScrollType() != ResultSet.TYPE_FORWARD_ONLY); 433 } 434 435 protected void setConcurType(int scrollType) 436 { 437 currentDrdaRs.concurType = scrollType; 438 } 439 440 protected int getConcurType() 441 { 442 return currentDrdaRs.concurType; 443 } 444 445 protected void setOutovr_drdaType(int[] outovr_drdaType) 446 { 447 currentDrdaRs.outovr_drdaType = outovr_drdaType; 448 } 449 450 451 protected int[] getOutovr_drdaType() 452 { 453 return currentDrdaRs.outovr_drdaType; 454 } 455 456 protected boolean hasdata() 457 { 458 return currentDrdaRs.hasdata; 459 } 460 461 protected void setHasdata(boolean hasdata) 462 { 463 currentDrdaRs.hasdata = hasdata; 464 } 465 466 475 protected void initialize() 476 { 477 setTypDefValues(); 478 } 479 480 481 protected PreparedStatement explicitPrepare(String sqlStmt) throws SQLException 482 { 483 explicitlyPrepared = true; 484 return prepare(sqlStmt); 485 } 486 487 protected boolean wasExplicitlyPrepared() 488 { 489 return explicitlyPrepared; 490 } 491 492 499 protected PreparedStatement prepare(String sqlStmt) throws SQLException 500 { 501 int saveIsolationLevel = -1; 503 boolean isolationSet = false; 504 if (pkgnamcsn !=null && 505 isolationLevel != Connection.TRANSACTION_NONE) 506 { 507 saveIsolationLevel = database.getPrepareIsolation(); 508 database.setPrepareIsolation(isolationLevel); 509 isolationSet = true; 510 } 511 512 if (isCallableSQL(sqlStmt)) 513 { 514 isCall = true; 515 ps = database.getConnection().prepareCall(sqlStmt); 516 setupCallableStatementParams((CallableStatement )ps); 517 if (isolationSet) 518 database.setPrepareIsolation(saveIsolationLevel); 519 return ps; 520 } 521 parsePkgidToFindHoldability(); 522 ps = prepareStatementJDBC3(sqlStmt, scrollType, concurType, 523 withHoldCursor); 524 if (cursorName != null) ps.setCursorName(cursorName); 530 if (isolationSet) 531 database.setPrepareIsolation(saveIsolationLevel); 532 return ps; 533 } 534 535 540 protected PreparedStatement getPreparedStatement() throws SQLException 541 { 542 return ps; 543 } 544 545 546 555 protected boolean execute() throws SQLException 556 { 557 boolean hasResultSet = ps.execute(); 558 559 numResultSets = 0; 565 566 ResultSet rs = null; 567 boolean isCallable = (ps instanceof java.sql.CallableStatement ); 568 if (isCallable) 569 needsToSendParamData = true; 570 571 do { 572 rs = ps.getResultSet(); 573 if (rs !=null) 574 { 575 if(isCallable) 577 addResultSet(rs,getResultSetHoldability(rs)); 578 else 579 addResultSet(rs,withHoldCursor); 580 hasResultSet = true; 581 } 582 }while (isCallable && getMoreResults(JDBC30Translation.KEEP_CURRENT_RESULT)); 585 586 return hasResultSet; 587 588 } 589 590 596 protected void finishParams() 597 { 598 needsToSendParamData = false; 599 } 600 601 630 protected void setPkgnamcsn(Pkgnamcsn pkgnamcsn) 631 { 632 this.pkgnamcsn = pkgnamcsn; 633 pkgid = pkgnamcsn.getPkgid(); 637 638 if (isDynamicPkgid(pkgid)) 639 { 640 isolationLevel = Integer.parseInt(pkgid.substring(5,6)); 641 642 643 654 655 656 657 String shortPkgid = pkgid.substring(pkgid.length() -5 , pkgid.length()); 660 pkgsn = pkgnamcsn.getPkgsn(); 661 this.cursorName = "SQL_CUR" + shortPkgid + "C" + pkgsn ; 662 } 663 else { 665 isolationLevel = getStaticPackageIsolation(pkgid); 666 } 667 668 this.pkgcnstkn = pkgnamcsn.getPkgcnstkn(); 669 670 } 671 672 673 678 private int getStaticPackageIsolation(String pkgid) 679 { 680 if (pkgid.equals("SYSSTAT")) 684 return ExecutionContext.READ_UNCOMMITTED_ISOLATION_LEVEL; 685 else 686 return ExecutionContext.UNSPECIFIED_ISOLATION_LEVEL; 687 } 688 689 694 protected Pkgnamcsn getPkgnamcsn() 695 { 696 return pkgnamcsn; 697 698 } 699 704 protected ResultSet getResultSet() 705 { 706 return currentDrdaRs.getResultSet(); 707 } 708 709 710 719 private ResultSet getResultSet(int rsNum) 720 { 721 if (rsNum == 0) 722 return currentDrdaRs.getResultSet(); 723 else 724 { 725 ConsistencyToken key = (ConsistencyToken) resultSetKeyList.get(rsNum); 726 return ((DRDAResultSet) (resultSetTable.get( key))).getResultSet(); 727 } 728 } 729 730 735 protected void setResultSet(ResultSet value) throws SQLException 736 { 737 if (currentDrdaRs.getResultSet() == null) 738 numResultSets = 1; 739 currentDrdaRs.setResultSet(value); 740 setRsDefaultOptions(currentDrdaRs); 741 } 742 743 748 protected DRDAResultSet getCurrentDrdaResultSet() 749 { 750 return currentDrdaRs ; 751 } 752 753 759 protected void setCurrentDrdaResultSet(int rsNum) 760 { 761 ConsistencyToken consistToken = getResultSetPkgcnstkn(rsNum); 762 if (currentDrdaRs.pkgcnstkn == consistToken) 763 return; 764 currentDrdaRs = getDrdaResultSet(consistToken); 765 766 } 767 768 775 protected void setCurrentDrdaResultSet(Pkgnamcsn pkgnamcsn) 776 { 777 pkgid = pkgnamcsn.getPkgid(); 778 pkgsn = pkgnamcsn.getPkgsn(); 779 ConsistencyToken consistToken = pkgnamcsn.getPkgcnstkn(); 780 DRDAResultSet newDrdaRs = getDrdaResultSet(consistToken); 781 if (newDrdaRs != null) 782 currentDrdaRs = newDrdaRs; 783 } 784 785 786 790 private DRDAResultSet getDrdaResultSet(ConsistencyToken consistToken) 791 { 792 if ( resultSetTable == null || 793 (currentDrdaRs != null && 794 currentDrdaRs.pkgcnstkn == consistToken )) 795 { 796 return currentDrdaRs; 797 } 798 else 799 { 800 return (DRDAResultSet) (resultSetTable.get(consistToken)); 801 } 802 } 803 804 808 private DRDAResultSet getDrdaResultSet(int rsNum) 809 { 810 ConsistencyToken consistToken = getResultSetPkgcnstkn(rsNum); 811 return getDrdaResultSet(consistToken); 812 } 813 814 823 protected ConsistencyToken addResultSet(ResultSet value, int holdValue) throws SQLException 824 { 825 826 DRDAResultSet newDrdaRs = null; 827 828 int rsNum = numResultSets; 829 ConsistencyToken newRsPkgcnstkn = calculateResultSetPkgcnstkn(rsNum); 830 831 if (rsNum == 0) 832 newDrdaRs = currentDrdaRs; 833 834 else 835 { 836 newDrdaRs = new DRDAResultSet(); 837 838 if (resultSetTable == null) 840 { 841 resultSetTable = new Hashtable (); 845 resultSetTable.put(pkgcnstkn, currentDrdaRs); 846 resultSetKeyList = new ArrayList (); 847 resultSetKeyList.add(0, pkgcnstkn); 848 } 849 850 resultSetTable.put(newRsPkgcnstkn, newDrdaRs); 851 resultSetKeyList.add(rsNum, newRsPkgcnstkn); 852 } 853 854 newDrdaRs.setResultSet(value); 855 newDrdaRs.setPkgcnstkn(newRsPkgcnstkn); 856 newDrdaRs.withHoldCursor = holdValue; 857 setRsDefaultOptions(newDrdaRs); 858 newDrdaRs.suspend(); 859 numResultSets++; 860 return newRsPkgcnstkn; 861 } 862 863 867 protected int getNumResultSets() 868 { 869 return numResultSets; 870 } 871 872 873 877 protected ConsistencyToken getResultSetPkgcnstkn(int rsNum) 878 { 879 if (rsNum == 0) 880 return pkgcnstkn; 881 else 882 return (ConsistencyToken) resultSetKeyList.get(rsNum); 883 } 884 885 886 890 protected void setRsDRDATypes(int [] value) 891 { 892 currentDrdaRs.setRsDRDATypes(value); 893 } 894 895 898 899 protected int[] getRsDRDATypes() 900 { 901 return currentDrdaRs.getRsDRDATypes(); 902 903 } 904 905 906 910 protected void setRsLens(int [] value) 911 { 912 currentDrdaRs.rsLens = value; 913 914 } 915 916 919 920 protected int[] getRsLens() 921 { 922 return currentDrdaRs.rsLens; 923 } 924 925 928 protected void rsClose() throws SQLException 929 { 930 if (currentDrdaRs.getResultSet() == null) 931 return; 932 933 currentDrdaRs.close(); 934 needsToSendParamData = false; 935 numResultSets--; 936 } 937 938 942 protected void CLSQRY() 943 { 944 currentDrdaRs.CLSQRY(); 945 } 946 947 951 protected boolean wasExplicitlyClosed() 952 { 953 return currentDrdaRs.wasExplicitlyClosed(); 954 } 955 956 962 protected void close() throws SQLException 963 { 964 if (ps != null) 965 ps.close(); 966 if (stmt != null) 967 stmt.close(); 968 currentDrdaRs.close(); 969 resultSetTable = null; 970 resultSetKeyList = null; 971 ps = null; 972 stmtPmeta = null; 973 stmt = null; 974 rslsetflg = null; 975 procName = null; 976 outputTypes = null; 977 cliParamDrdaTypes = null; 978 cliParamLens = null; 979 cliParamExtPositions = null; 980 981 } 982 983 995 protected void reset() 996 { 997 setTypDefValues(); 998 999 withHoldCursor = -1; 1000 scrollType = ResultSet.TYPE_FORWARD_ONLY; 1001 concurType = ResultSet.CONCUR_READ_ONLY;; 1002 rowCount = 0; 1003 rslsetflg = null; 1004 maxrslcnt = 0; 1005 ps = null; 1006 stmtPmeta = null; 1007 isCall = false; 1008 procName = null; 1009 outputTypes = null; 1010 outputExpected = false; 1011 stmt = null; 1012 1013 currentDrdaRs.reset(); 1014 resultSetTable = null; 1015 resultSetKeyList = null; 1016 numResultSets = 0; 1017 1018 cliParamDrdaTypes = new Vector (); 1019 cliParamLens = new Vector (); 1020 cliParamExtPositions = null; 1021 1022 nbrrow = 0; 1023 qryrowset = 0; 1024 blksize = 0; 1025 maxblkext = 0; 1026 outovropt = 0; 1027 qryrfrtbl = false; 1028 qryprctyp = CodePoint.QRYBLKCTL_DEFAULT; 1029 1030 needsToSendParamData = false; 1031 explicitlyPrepared = false; 1032 } 1033 1034 1038 protected boolean rsIsClosed() 1039 { 1040 return currentDrdaRs.isClosed(); 1041 } 1042 1043 1046 protected void rsSuspend() 1047 { 1048 currentDrdaRs.suspend(); 1049 } 1050 1051 1052 1058 protected void setRsPrecision(int index, int precision) 1059 { 1060 currentDrdaRs.setRsPrecision(index,precision); 1061 } 1062 1063 1068 protected int getRsPrecision(int index) 1069 { 1070 return currentDrdaRs.getRsPrecision(index); 1071 } 1072 1073 1079 protected void setRsScale(int index, int scale) 1080 { 1081 currentDrdaRs.setRsScale(index, scale); 1082 } 1083 1084 1089 protected int getRsScale(int index) 1090 { 1091 return currentDrdaRs.getRsScale(index); 1092 } 1093 1094 1095 1101 protected void setRsDRDAType(int index, int type) 1102 { 1103 currentDrdaRs.setRsDRDAType(index,type); 1104 1105 } 1106 1107 1108 1114 protected int getParamDRDAType(int index) 1115 { 1116 1117 return ((Byte )cliParamDrdaTypes.get(index -1)).intValue(); 1118 } 1119 1120 1121 1127 protected void setParamDRDAType(int index, byte type) 1128 { 1129 cliParamDrdaTypes.addElement(new Byte (type)); 1130 1131 } 1132 1138 1139 protected int getParamLen(int index) 1140 { 1141 return ((Integer ) cliParamLens.elementAt(index -1)).intValue(); 1142 } 1143 1150 protected int getParamPrecision(int index) throws SQLException 1151 { 1152 if (ps != null && ps instanceof CallableStatement ) 1153 { 1154 EngineParameterMetaData pmeta = getParameterMetaData(); 1155 1156 return Math.min(pmeta.getPrecision(index), 1157 FdocaConstants.NUMERIC_MAX_PRECISION); 1158 1159 } 1160 else 1161 return -1; 1162 } 1163 1164 1171 protected int getParamScale(int index) throws SQLException 1172 { 1173 if (ps != null && ps instanceof CallableStatement ) 1174 { 1175 EngineParameterMetaData pmeta = getParameterMetaData(); 1176 return Math.min(pmeta.getScale(index),FdocaConstants.NUMERIC_MAX_PRECISION); 1177 } 1178 else 1179 return -1; 1180 } 1181 1182 1188 protected void setParamLen(int index, int value) 1189 { 1190 cliParamLens.add(index -1, new Integer (value)); 1191 } 1192 1193 1198 protected int getNumParams() 1199 { 1200 if (cliParamDrdaTypes != null) 1201 return cliParamDrdaTypes.size(); 1202 else 1203 return 0; 1204 } 1205 1206 1211 1212 protected int getNumRsCols() 1213 { 1214 int[] rsDrdaTypes = getRsDRDATypes(); 1215 if (rsDrdaTypes != null) 1216 return rsDrdaTypes.length; 1217 else 1218 return 0; 1219 } 1220 1221 1227 protected int getRsDRDAType(int index) 1228 { 1229 return currentDrdaRs.getRsDRDAType(index); 1230 } 1231 1232 1238 1239 protected int getRsLen(int index) 1240 { 1241 return currentDrdaRs.getRsLen(index); 1242 } 1243 1244 1249 protected void setRsLen(int index, int value) 1250 { 1251 currentDrdaRs.setRsLen(index,value); 1252 } 1253 1254 1257 public String getResultSetCursorName(int rsNum) throws SQLException 1258 { 1259 DRDAResultSet drdaRs = getDrdaResultSet(rsNum); 1260 return drdaRs.getResultSetCursorName(); 1261 1262 } 1263 1264 1265 protected String toDebugString(String indent) 1266 { 1267 ResultSet rs = currentDrdaRs.getResultSet(); 1268 1269 String s =""; 1270 if (ps == null) 1271 s += indent + ps; 1272 else 1273 { 1274 s += indent + pkgid + pkgsn ; 1275 s += "\t" + getSQLText(); 1276 } 1277 return s; 1278 } 1279 1280 1288 1289 protected ConsistencyToken calculateResultSetPkgcnstkn(int rsNum) 1290 { 1291 ConsistencyToken consistToken = pkgcnstkn; 1292 1293 if (rsNum == 0 || pkgcnstkn == null) 1294 return consistToken; 1295 else 1296 { 1297 BigInteger consistTokenBi = 1298 new BigInteger (consistToken.getBytes()); 1299 BigInteger rsNumBi = BigInteger.valueOf(rsNum); 1300 consistTokenBi = consistTokenBi.subtract(rsNumBi); 1301 consistToken = new ConsistencyToken(consistTokenBi.toByteArray()); 1302 } 1303 return consistToken; 1304 } 1305 1306 protected boolean isCallableStatement() 1307 { 1308 return isCall; 1309 } 1310 1311 private boolean isCallableSQL(String sql) 1312 { 1313 java.util.StringTokenizer tokenizer = new java.util.StringTokenizer 1314 (sql, "\t\n\r\f=? ("); 1315 String firstToken = tokenizer.nextToken(); 1316 if (StringUtil.SQLEqualsIgnoreCase(firstToken, 1317 "call")) return true; 1319 return false; 1320 1321 } 1322 1323 private void setupCallableStatementParams(CallableStatement cs) throws SQLException 1324 { 1325 EngineParameterMetaData pmeta = getParameterMetaData(); 1326 int numElems = pmeta.getParameterCount(); 1327 1328 for ( int i = 0; i < numElems; i ++) 1329 { 1330 boolean outputFlag = false; 1331 1332 int parameterMode = pmeta.getParameterMode(i + 1); 1333 int parameterType = pmeta.getParameterType(i + 1); 1334 1335 switch (parameterMode) { 1336 case JDBC30Translation.PARAMETER_MODE_IN: 1337 break; 1338 case JDBC30Translation.PARAMETER_MODE_OUT: 1339 case JDBC30Translation.PARAMETER_MODE_IN_OUT: 1340 outputFlag = true; 1341 break; 1342 case JDBC30Translation.PARAMETER_MODE_UNKNOWN: 1343 String objectType = pmeta.getParameterClassName(i+1); 1345 parameterType = 1346 getOutputParameterTypeFromClassName(objectType); 1347 if (parameterType != NOT_OUTPUT_PARAM) 1348 outputFlag = true; 1349 } 1350 1351 if (outputFlag) 1352 { 1353 if (outputTypes == null) { 1355 outputTypes = new int[numElems]; 1356 for (int j = 0; j < numElems; j++) 1357 outputTypes[j] = NOT_OUTPUT_PARAM; } 1359 outputTypes[i] = parameterType; 1362 } 1363 1364 } 1365 } 1366 1367 1368 1369 1380 1381 protected static int getOutputParameterTypeFromClassName(String 1382 objectName) 1383 { 1384 1385 if (objectName.endsWith("[]")) 1386 { 1387 if (objectName.equals("byte[]")) 1391 { 1392 return NOT_OUTPUT_PARAM; 1393 1394 } 1397 1398 if (objectName.equals("java.lang.Byte[]")) 1401 return java.sql.Types.TINYINT; 1402 1403 if (objectName.equals("byte[][]")) 1404 return java.sql.Types.VARBINARY; 1405 if (objectName.equals("java.lang.String[]")) 1406 return java.sql.Types.VARCHAR; 1407 if (objectName.equals("int[]") || 1408 objectName.equals("java.lang.Integer[]")) 1409 return java.sql.Types.INTEGER; 1410 else if (objectName.equals("long[]") 1411 || objectName.equals("java.lang.Long[]")) 1412 return java.sql.Types.BIGINT; 1413 else if (objectName.equals("java.math.BigDecimal[]")) 1414 return java.sql.Types.NUMERIC; 1415 else if (objectName.equals("boolean[]") || 1416 objectName.equals("java.lang.Boolean[]")) 1417 return java.sql.Types.BIT; 1418 else if (objectName.equals("short[]")) 1419 return java.sql.Types.SMALLINT; 1420 else if (objectName.equals("float[]") || 1421 objectName.equals("java.lang.Float[]")) 1422 return java.sql.Types.REAL; 1423 else if (objectName.equals("double[]") || 1424 objectName.equals("java.lang.Double[]")) 1425 return java.sql.Types.DOUBLE; 1426 else if (objectName.equals("java.sql.Date[]")) 1427 return java.sql.Types.DATE; 1428 else if (objectName.equals("java.sql.Time[]")) 1429 return java.sql.Types.TIME; 1430 else if (objectName.equals("java.sql.Timestamp[]")) 1431 return java.sql.Types.TIMESTAMP; 1432 } 1433 return NOT_OUTPUT_PARAM; 1435 1438 } 1439 1440 1441 public void registerAllOutParams() throws SQLException 1442 { 1443 if (isCall && (outputTypes != null)) 1444 for (int i = 1; i <= outputTypes.length; i ++) 1445 registerOutParam(i); 1446 1447 } 1448 1449 public void registerOutParam(int paramNum) throws SQLException 1450 { 1451 CallableStatement cs; 1452 if (isOutputParam(paramNum)) 1453 { 1454 cs = (CallableStatement ) ps; 1455 cs.registerOutParameter(paramNum, getOutputParamType(paramNum)); 1456 } 1457 } 1458 1459 protected boolean hasOutputParams() 1460 { 1461 return (outputTypes != null); 1462 } 1463 1464 1469 boolean isOutputParam(int paramNum) 1470 { 1471 if (outputTypes != null) 1472 return (outputTypes[paramNum - 1] != NOT_OUTPUT_PARAM); 1473 return false; 1474 1475 } 1476 1482 int getOutputParamType(int paramNum) 1483 { 1484 if (outputTypes != null) 1485 return (outputTypes[ paramNum - 1 ]); 1486 return NOT_OUTPUT_PARAM; 1487 } 1488 1489 private boolean isDynamicPkgid(String pkgid) 1490 { 1491 char size = pkgid.charAt(3); 1492 1493 char holdability = pkgid.charAt(4); 1496 return (pkgid.substring(0,3).equals("SYS") && (size == 'S' || 1497 size == 'L') 1498 && (holdability == 'H' || holdability == 'N')); 1499 1500 } 1501 1502 1503 private void parsePkgidToFindHoldability() 1504 { 1505 if (withHoldCursor != -1) 1506 return; 1507 1508 if (isDynamicPkgid(pkgid)) 1511 { 1512 if(pkgid.charAt(4) == 'N') 1513 withHoldCursor = JDBC30Translation.CLOSE_CURSORS_AT_COMMIT; 1514 else 1515 withHoldCursor = JDBC30Translation.HOLD_CURSORS_OVER_COMMIT; 1516 } 1517 else 1518 { 1519 withHoldCursor = JDBC30Translation.HOLD_CURSORS_OVER_COMMIT; 1520 1521 } 1522 } 1523 1524 1525 1537 private PreparedStatement prepareStatementJDBC3(String sqlStmt, int 1538 scrollType, int concurType, 1539 int withHoldCursor) throws SQLException 1540 { 1541 EngineConnection conn = database.getConnection(); 1542 if (withHoldCursor == -1) { 1543 return conn.prepareStatement(sqlStmt, 1546 scrollType, concurType); 1547 } 1548 1549 return conn.prepareStatement(sqlStmt, 1551 scrollType, concurType, withHoldCursor); 1552 } 1553 1554 1555 1563 protected EngineParameterMetaData getParameterMetaData() throws SQLException 1564 { 1565 if (stmtPmeta != null) 1566 return stmtPmeta; 1567 1568 stmtPmeta = ((EnginePreparedStatement)ps).getEmbedParameterSetMetaData(); 1569 1570 return stmtPmeta; 1571 } 1572 1573 1581 private boolean getMoreResults(int current) throws SQLException 1582 { 1583 return 1584 ((EngineStatement) getPreparedStatement()).getMoreResults(current); 1585 } 1586 1587 1592 private String getSQLText() 1593 { 1594 String retVal = null; 1595 Class [] emptyPARAM = {}; 1596 Object [] args = null; 1597 try { 1598 Method sh = getPreparedStatement().getClass().getMethod("getSQLText",emptyPARAM); 1599 retVal = (String ) sh.invoke(getPreparedStatement(),args); 1600 } 1601 catch (Exception e) 1602 { 1603 } 1605 return retVal; 1606 1607 } 1608 1609 1614 private void handleReflectionException(Exception e) throws SQLException 1615 { 1616 if (e instanceof InvocationTargetException ) 1617 { 1618 Throwable t = ((InvocationTargetException ) e).getTargetException(); 1619 1620 if (t instanceof SQLException ) 1621 { 1622 throw (SQLException ) t; 1623 } 1624 else 1625 { 1626 t.printStackTrace(); 1627 throw Util.javaException(t); 1628 } 1629 } 1630 else 1631 throw Util.javaException(e); 1635 1636 } 1637 1638 1650 boolean isRSCloseImplicit(boolean lmtblkprcOK) throws SQLException { 1651 return 1652 (currentDrdaRs.qryclsimp == CodePoint.QRYCLSIMP_YES) && 1653 !isScrollable() && 1654 (lmtblkprcOK || 1655 (currentDrdaRs.getQryprctyp() != CodePoint.LMTBLKPRC)); 1656 } 1657} 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 | Popular Tags |