1 25 package com.mysql.jdbc; 26 27 import java.io.InputStream ; 28 import java.io.Reader ; 29 30 import java.math.BigDecimal ; 31 32 import java.net.URL ; 33 34 import java.sql.Array ; 35 import java.sql.Blob ; 36 import java.sql.Clob ; 37 import java.sql.Date ; 38 import java.sql.ParameterMetaData ; 39 import java.sql.Ref ; 40 import java.sql.SQLException ; 41 import java.sql.Time ; 42 import java.sql.Timestamp ; 43 import java.sql.Types ; 44 45 import java.util.ArrayList ; 46 import java.util.Calendar ; 47 import java.util.HashMap ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 import java.util.Locale ; 51 import java.util.Map ; 52 53 60 public class CallableStatement extends PreparedStatement implements 61 java.sql.CallableStatement { 62 class CallableStatementParam { 63 int desiredJdbcType; 64 65 int index; 66 67 boolean isIn; 68 69 boolean isOut; 70 71 String paramName; 72 73 int jdbcType; 74 String typeName; 75 int precision; 76 int scale; 77 short nullability; 78 int inOutModifier; 79 80 CallableStatementParam(String name, int idx, boolean in, 81 boolean out, int jdbcType, String typeName, 82 int precision, int scale, short nullability, 83 int inOutModifier) { 84 this.paramName = name; 85 this.isIn = in; 86 this.isOut = out; 87 this.index = idx; 88 89 this.jdbcType = jdbcType; 90 this.typeName = typeName; 91 this.precision = precision; 92 this.scale = scale; 93 this.nullability = nullability; 94 this.inOutModifier = inOutModifier; 95 } 96 97 102 protected Object clone() throws CloneNotSupportedException { 103 return super.clone(); 104 } 105 } 106 107 class CallableStatementParamInfo implements ParameterMetaData { 108 String catalogInUse; 109 110 boolean isFunctionCall; 111 112 String nativeSql; 113 114 int numParameters; 115 116 List parameterList; 117 118 Map parameterMap; 119 120 CallableStatementParamInfo(java.sql.ResultSet paramTypesRs) 121 throws SQLException { 122 boolean hadRows = paramTypesRs.last(); 123 124 this.nativeSql = originalSql; 125 this.catalogInUse = currentCatalog; 126 isFunctionCall = callingStoredFunction; 127 128 if (hadRows) { 129 this.numParameters = paramTypesRs.getRow(); 130 131 this.parameterList = new ArrayList (this.numParameters); 132 this.parameterMap = new HashMap (this.numParameters); 133 134 paramTypesRs.beforeFirst(); 135 136 addParametersFromDBMD(paramTypesRs); 137 } else { 138 this.numParameters = 0; 139 } 140 } 141 142 private void addParametersFromDBMD(java.sql.ResultSet paramTypesRs) 143 throws SQLException { 144 int i = 0; 145 146 if (isFunctionCall) { 147 paramTypesRs.next(); 149 } 150 151 while (paramTypesRs.next()) { 152 String paramName = paramTypesRs.getString(4); 153 int inOutModifier = paramTypesRs.getInt(5); 154 155 boolean isOutParameter = false; 156 boolean isInParameter = false; 157 158 if (inOutModifier == DatabaseMetaData.procedureColumnInOut) { 159 isOutParameter = true; 160 isInParameter = true; 161 } else if (inOutModifier == DatabaseMetaData.procedureColumnIn) { 162 isOutParameter = false; 163 isInParameter = true; 164 } else if (inOutModifier == DatabaseMetaData.procedureColumnOut) { 165 isOutParameter = true; 166 isInParameter = false; 167 } 168 169 int jdbcType = paramTypesRs.getInt(6); 170 String typeName = paramTypesRs.getString(7); 171 int precision = paramTypesRs.getInt(8); 172 int scale = paramTypesRs.getInt(10); 173 short nullability = paramTypesRs.getShort(12); 174 175 CallableStatementParam paramInfoToAdd = new CallableStatementParam( 176 paramName, i++, isInParameter, isOutParameter, 177 jdbcType, typeName, precision, scale, nullability, 178 inOutModifier); 179 180 this.parameterList.add(paramInfoToAdd); 181 this.parameterMap.put(paramName, paramInfoToAdd); 182 } 183 } 184 185 190 protected Object clone() throws CloneNotSupportedException { 191 return super.clone(); 193 } 194 195 CallableStatementParam getParameter(int index) { 196 return (CallableStatementParam) this.parameterList.get(index); 197 } 198 199 CallableStatementParam getParameter(String name) { 200 return (CallableStatementParam) this.parameterMap.get(name); 201 } 202 203 Iterator iterator() { 204 return this.parameterList.iterator(); 205 } 206 207 int numberOfParameters() { 208 return this.numParameters; 209 } 210 211 public int getParameterCount() throws SQLException { 212 return this.parameterList.size(); 213 } 214 215 public int isNullable(int arg0) throws SQLException { 216 checkBounds(arg0); 217 218 return getParameter(arg0 - 1).nullability; 219 } 220 221 public boolean isSigned(int arg0) throws SQLException { 222 checkBounds(arg0); 223 224 return false; 225 } 226 227 public int getPrecision(int arg0) throws SQLException { 228 checkBounds(arg0); 229 230 return getParameter(arg0 - 1).precision; 231 } 232 233 public int getScale(int arg0) throws SQLException { 234 checkBounds(arg0); 235 236 return getParameter(arg0 - 1).scale; 237 } 238 239 public int getParameterType(int arg0) throws SQLException { 240 checkBounds(arg0); 241 242 return getParameter(arg0 - 1).jdbcType; 243 } 244 245 public String getParameterTypeName(int arg0) throws SQLException { 246 checkBounds(arg0); 247 248 return getParameter(arg0 - 1).typeName; 249 } 250 251 public String getParameterClassName(int arg0) throws SQLException { 252 return null; 254 } 255 256 public int getParameterMode(int arg0) throws SQLException { 257 checkBounds(arg0); 258 259 return getParameter(arg0 - 1).inOutModifier; 260 } 261 262 protected void checkBounds(int paramIndex) throws SQLException { 263 int localParamIndex = paramIndex - 1; 264 265 if ((paramIndex < 0) 266 || (localParamIndex >= this.numParameters)) { 267 throw new SQLException ( 268 Messages.getString("CallableStatement.11") + paramIndex + Messages.getString("CallableStatement.12") + numParameters + Messages.getString("CallableStatement.13"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } 272 } 273 } 274 275 private final static int NOT_OUTPUT_PARAMETER_INDICATOR = Integer.MIN_VALUE; 276 277 private final static String PARAMETER_NAMESPACE_PREFIX = "@com_mysql_jdbc_outparam_"; 279 private static String mangleParameterName(String origParameterName) { 280 if (origParameterName == null) { 281 return null; 282 } 283 284 int offset = 0; 285 286 if (origParameterName.length() > 0 287 && origParameterName.charAt(0) == '@') { 288 offset = 1; 289 } 290 291 StringBuffer paramNameBuf = new StringBuffer (PARAMETER_NAMESPACE_PREFIX 292 .length() 293 + origParameterName.length()); 294 paramNameBuf.append(PARAMETER_NAMESPACE_PREFIX); 295 paramNameBuf.append(origParameterName.substring(offset)); 296 297 return paramNameBuf.toString(); 298 } 299 300 private boolean callingStoredFunction = false; 301 302 private ResultSet functionReturnValueResults; 303 304 private boolean hasOutputParams = false; 305 306 private ResultSet outputParameterResults; 309 310 private boolean outputParamWasNull = false; 311 312 private int[] parameterIndexToRsIndex; 313 314 protected CallableStatementParamInfo paramInfo; 315 316 private CallableStatementParam returnValueParam; 317 318 329 public CallableStatement(Connection conn, 330 CallableStatementParamInfo paramInfo) throws SQLException { 331 super(conn, paramInfo.nativeSql, paramInfo.catalogInUse); 332 333 this.paramInfo = paramInfo; 334 this.callingStoredFunction = this.paramInfo.isFunctionCall; 335 } 336 337 348 public CallableStatement(Connection conn, String catalog) 349 throws SQLException { 350 super(conn, catalog, null); 351 352 determineParameterTypes(); 353 } 354 355 368 public CallableStatement(Connection conn, String sql, String catalog, 369 boolean isFunctionCall) throws SQLException { 370 super(conn, sql, catalog); 371 372 this.callingStoredFunction = isFunctionCall; 373 374 determineParameterTypes(); 375 } 376 377 382 public void addBatch() throws SQLException { 383 setOutParams(); 384 385 super.addBatch(); 386 } 387 388 private CallableStatementParam checkIsOutputParam(int paramIndex) 389 throws SQLException { 390 391 if (this.callingStoredFunction) { 392 if (paramIndex == 1) { 393 394 if (this.returnValueParam == null) { 395 this.returnValueParam = new CallableStatementParam("", 0, 396 false, true, Types.VARCHAR, "VARCHAR", 0, 0, 397 DatabaseMetaData.attributeNullableUnknown, 398 DatabaseMetaData.procedureColumnReturn); 399 } 400 401 return this.returnValueParam; 402 } 403 404 paramIndex--; 406 } 407 408 checkParameterIndexBounds(paramIndex); 409 410 int localParamIndex = paramIndex - 1; 411 412 CallableStatementParam paramDescriptor = this.paramInfo 413 .getParameter(localParamIndex); 414 415 if (!paramDescriptor.isOut) { 416 throw new SQLException ( 417 Messages.getString("CallableStatement.9") + paramIndex + Messages.getString("CallableStatement.10"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 420 } 421 422 this.hasOutputParams = true; 423 424 return paramDescriptor; 425 } 426 427 434 private void checkParameterIndexBounds(int paramIndex) throws SQLException { 435 this.paramInfo.checkBounds(paramIndex); 436 } 437 438 446 private void checkStreamability() throws SQLException { 447 if (this.hasOutputParams && createStreamingResultSet()) { 448 throw new SQLException (Messages.getString("CallableStatement.14"), SQLError.SQL_STATE_DRIVER_NOT_CAPABLE); 450 } 451 } 452 453 private void determineParameterTypes() throws SQLException { 454 java.sql.ResultSet paramTypesRs = null; 455 456 try { 457 String procName = extractProcedureName(); 458 459 java.sql.DatabaseMetaData dbmd = this.connection.getMetaData(); 460 461 boolean useCatalog = false; 462 463 if (procName.indexOf(".") == -1) { 464 useCatalog = true; 465 } 466 467 paramTypesRs = dbmd.getProcedureColumns(this.connection 468 .versionMeetsMinimum(5, 0, 2) 469 & useCatalog ? this.currentCatalog : null, null, procName, 470 "%"); 472 this.paramInfo = new CallableStatementParamInfo(paramTypesRs); 473 } finally { 474 SQLException sqlExRethrow = null; 475 476 if (paramTypesRs != null) { 477 try { 478 paramTypesRs.close(); 479 } catch (SQLException sqlEx) { 480 sqlExRethrow = sqlEx; 481 } 482 483 paramTypesRs = null; 484 } 485 486 if (sqlExRethrow != null) { 487 throw sqlExRethrow; 488 } 489 } 490 } 491 492 497 public boolean execute() throws SQLException { 498 boolean returnVal = false; 499 500 checkClosed(); 501 502 checkStreamability(); 503 504 synchronized (this.connection.getMutex()) { 505 setInOutParamsOnServer(); 506 setOutParams(); 507 508 returnVal = super.execute(); 509 510 if (this.callingStoredFunction) { 511 this.functionReturnValueResults = this.results; 512 this.functionReturnValueResults.next(); 513 this.results = null; 514 } 515 516 retrieveOutParams(); 517 } 518 519 if (!this.callingStoredFunction) { 520 return returnVal; 521 } 522 523 return false; 525 } 526 527 532 public synchronized java.sql.ResultSet executeQuery() throws SQLException { 533 checkClosed(); 534 535 checkStreamability(); 536 537 java.sql.ResultSet execResults = null; 538 539 synchronized (this.connection.getMutex()) { 540 setInOutParamsOnServer(); 541 setOutParams(); 542 543 execResults = super.executeQuery(); 544 545 retrieveOutParams(); 546 } 547 548 return execResults; 549 } 550 551 556 public synchronized int executeUpdate() throws SQLException { 557 int returnVal = -1; 558 559 checkClosed(); 560 561 checkStreamability(); 562 563 if (this.callingStoredFunction) { 564 execute(); 565 566 return -1; 567 } 568 569 synchronized (this.connection.getMutex()) { 570 setInOutParamsOnServer(); 571 setOutParams(); 572 573 returnVal = super.executeUpdate(); 574 575 retrieveOutParams(); 576 } 577 578 return returnVal; 579 } 580 581 private String extractProcedureName() throws SQLException { 582 int endCallIndex = StringUtils.indexOfIgnoreCase(this.originalSql, 584 "CALL "); int offset = 5; 586 587 if (endCallIndex == -1) { 588 endCallIndex = StringUtils.indexOfIgnoreCase(this.originalSql, 589 "SELECT "); 590 offset = 7; 591 } 592 593 if (endCallIndex != -1) { 594 StringBuffer nameBuf = new StringBuffer (); 595 596 String trimmedStatement = this.originalSql.substring( 597 endCallIndex + offset).trim(); 598 599 int statementLength = trimmedStatement.length(); 600 601 for (int i = 0; i < statementLength; i++) { 602 char c = trimmedStatement.charAt(i); 603 604 if (Character.isWhitespace(c) || (c == '(') || (c == '?')) { 605 break; 606 } 607 nameBuf.append(c); 608 609 } 610 611 return nameBuf.toString(); 612 } 613 throw new SQLException (Messages.getString("CallableStatement.1"), SQLError.SQL_STATE_GENERAL_ERROR); 615 616 } 617 618 629 private String fixParameterName(String paramNameIn) throws SQLException { 630 if ((paramNameIn == null) || (paramNameIn.length() == 0)) { 631 throw new SQLException ( 632 ((Messages.getString("CallableStatement.0") + paramNameIn) == null) ? Messages.getString("CallableStatement.15") : Messages.getString("CallableStatement.16"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } 635 636 return mangleParameterName(paramNameIn); 637 638 645 } 646 647 650 public synchronized Array getArray(int i) throws SQLException { 651 ResultSet rs = getOutputParameters(i); 652 653 Array retValue = rs.getArray(mapOutputParameterIndexToRsIndex(i)); 654 655 this.outputParamWasNull = rs.wasNull(); 656 657 return retValue; 658 } 659 660 663 public synchronized Array getArray(String parameterName) 664 throws SQLException { 665 ResultSet rs = getOutputParameters(0); 668 Array retValue = rs.getArray(fixParameterName(parameterName)); 669 670 this.outputParamWasNull = rs.wasNull(); 671 672 return retValue; 673 } 674 675 678 public synchronized BigDecimal getBigDecimal(int parameterIndex) 679 throws SQLException { 680 ResultSet rs = getOutputParameters(parameterIndex); 681 682 BigDecimal retValue = rs 683 .getBigDecimal(mapOutputParameterIndexToRsIndex(parameterIndex)); 684 685 this.outputParamWasNull = rs.wasNull(); 686 687 return retValue; 688 } 689 690 706 public synchronized BigDecimal getBigDecimal(int parameterIndex, int scale) 707 throws SQLException { 708 ResultSet rs = getOutputParameters(parameterIndex); 709 710 BigDecimal retValue = rs.getBigDecimal( 711 mapOutputParameterIndexToRsIndex(parameterIndex), scale); 712 713 this.outputParamWasNull = rs.wasNull(); 714 715 return retValue; 716 } 717 718 721 public synchronized BigDecimal getBigDecimal(String parameterName) 722 throws SQLException { 723 ResultSet rs = getOutputParameters(0); 726 BigDecimal retValue = rs.getBigDecimal(fixParameterName(parameterName)); 727 728 this.outputParamWasNull = rs.wasNull(); 729 730 return retValue; 731 } 732 733 736 public synchronized Blob getBlob(int parameterIndex) throws SQLException { 737 ResultSet rs = getOutputParameters(parameterIndex); 738 739 Blob retValue = rs 740 .getBlob(mapOutputParameterIndexToRsIndex(parameterIndex)); 741 742 this.outputParamWasNull = rs.wasNull(); 743 744 return retValue; 745 } 746 747 750 public synchronized Blob getBlob(String parameterName) throws SQLException { 751 ResultSet rs = getOutputParameters(0); 754 Blob retValue = rs.getBlob(fixParameterName(parameterName)); 755 756 this.outputParamWasNull = rs.wasNull(); 757 758 return retValue; 759 } 760 761 764 public synchronized boolean getBoolean(int parameterIndex) 765 throws SQLException { 766 ResultSet rs = getOutputParameters(parameterIndex); 767 768 boolean retValue = rs 769 .getBoolean(mapOutputParameterIndexToRsIndex(parameterIndex)); 770 771 this.outputParamWasNull = rs.wasNull(); 772 773 return retValue; 774 } 775 776 779 public synchronized boolean getBoolean(String parameterName) 780 throws SQLException { 781 ResultSet rs = getOutputParameters(0); 784 boolean retValue = rs.getBoolean(fixParameterName(parameterName)); 785 786 this.outputParamWasNull = rs.wasNull(); 787 788 return retValue; 789 } 790 791 794 public synchronized byte getByte(int parameterIndex) throws SQLException { 795 ResultSet rs = getOutputParameters(parameterIndex); 796 797 byte retValue = rs 798 .getByte(mapOutputParameterIndexToRsIndex(parameterIndex)); 799 800 this.outputParamWasNull = rs.wasNull(); 801 802 return retValue; 803 } 804 805 808 public synchronized byte getByte(String parameterName) throws SQLException { 809 ResultSet rs = getOutputParameters(0); 812 byte retValue = rs.getByte(fixParameterName(parameterName)); 813 814 this.outputParamWasNull = rs.wasNull(); 815 816 return retValue; 817 } 818 819 822 public synchronized byte[] getBytes(int parameterIndex) throws SQLException { 823 ResultSet rs = getOutputParameters(parameterIndex); 824 825 byte[] retValue = rs 826 .getBytes(mapOutputParameterIndexToRsIndex(parameterIndex)); 827 828 this.outputParamWasNull = rs.wasNull(); 829 830 return retValue; 831 } 832 833 836 public synchronized byte[] getBytes(String parameterName) 837 throws SQLException { 838 ResultSet rs = getOutputParameters(0); 841 byte[] retValue = rs.getBytes(fixParameterName(parameterName)); 842 843 this.outputParamWasNull = rs.wasNull(); 844 845 return retValue; 846 } 847 848 851 public synchronized Clob getClob(int parameterIndex) throws SQLException { 852 ResultSet rs = getOutputParameters(parameterIndex); 853 854 Clob retValue = rs 855 .getClob(mapOutputParameterIndexToRsIndex(parameterIndex)); 856 857 this.outputParamWasNull = rs.wasNull(); 858 859 return retValue; 860 } 861 862 865 public synchronized Clob getClob(String parameterName) throws SQLException { 866 ResultSet rs = getOutputParameters(0); 869 Clob retValue = rs.getClob(fixParameterName(parameterName)); 870 871 this.outputParamWasNull = rs.wasNull(); 872 873 return retValue; 874 } 875 876 879 public synchronized Date getDate(int parameterIndex) throws SQLException { 880 ResultSet rs = getOutputParameters(parameterIndex); 881 882 Date retValue = rs 883 .getDate(mapOutputParameterIndexToRsIndex(parameterIndex)); 884 885 this.outputParamWasNull = rs.wasNull(); 886 887 return retValue; 888 } 889 890 893 public synchronized Date getDate(int parameterIndex, Calendar cal) 894 throws SQLException { 895 ResultSet rs = getOutputParameters(parameterIndex); 896 897 Date retValue = rs.getDate( 898 mapOutputParameterIndexToRsIndex(parameterIndex), cal); 899 900 this.outputParamWasNull = rs.wasNull(); 901 902 return retValue; 903 } 904 905 908 public synchronized Date getDate(String parameterName) throws SQLException { 909 ResultSet rs = getOutputParameters(0); 912 Date retValue = rs.getDate(fixParameterName(parameterName)); 913 914 this.outputParamWasNull = rs.wasNull(); 915 916 return retValue; 917 } 918 919 923 public synchronized Date getDate(String parameterName, Calendar cal) 924 throws SQLException { 925 ResultSet rs = getOutputParameters(0); 928 Date retValue = rs.getDate(fixParameterName(parameterName), cal); 929 930 this.outputParamWasNull = rs.wasNull(); 931 932 return retValue; 933 } 934 935 938 public synchronized double getDouble(int parameterIndex) 939 throws SQLException { 940 ResultSet rs = getOutputParameters(parameterIndex); 941 942 double retValue = rs 943 .getDouble(mapOutputParameterIndexToRsIndex(parameterIndex)); 944 945 this.outputParamWasNull = rs.wasNull(); 946 947 return retValue; 948 } 949 950 953 public synchronized double getDouble(String parameterName) 954 throws SQLException { 955 ResultSet rs = getOutputParameters(0); 958 double retValue = rs.getDouble(fixParameterName(parameterName)); 959 960 this.outputParamWasNull = rs.wasNull(); 961 962 return retValue; 963 } 964 965 968 public synchronized float getFloat(int parameterIndex) throws SQLException { 969 ResultSet rs = getOutputParameters(parameterIndex); 970 971 float retValue = rs 972 .getFloat(mapOutputParameterIndexToRsIndex(parameterIndex)); 973 974 this.outputParamWasNull = rs.wasNull(); 975 976 return retValue; 977 } 978 979 982 public synchronized float getFloat(String parameterName) 983 throws SQLException { 984 ResultSet rs = getOutputParameters(0); 987 float retValue = rs.getFloat(fixParameterName(parameterName)); 988 989 this.outputParamWasNull = rs.wasNull(); 990 991 return retValue; 992 } 993 994 997 public synchronized int getInt(int parameterIndex) throws SQLException { 998 ResultSet rs = getOutputParameters(parameterIndex); 999 1000 int retValue = rs 1001 .getInt(mapOutputParameterIndexToRsIndex(parameterIndex)); 1002 1003 this.outputParamWasNull = rs.wasNull(); 1004 1005 return retValue; 1006 } 1007 1008 1011 public synchronized int getInt(String parameterName) throws SQLException { 1012 ResultSet rs = getOutputParameters(0); 1015 int retValue = rs.getInt(fixParameterName(parameterName)); 1016 1017 this.outputParamWasNull = rs.wasNull(); 1018 1019 return retValue; 1020 } 1021 1022 1025 public synchronized long getLong(int parameterIndex) throws SQLException { 1026 ResultSet rs = getOutputParameters(parameterIndex); 1027 1028 long retValue = rs 1029 .getLong(mapOutputParameterIndexToRsIndex(parameterIndex)); 1030 1031 this.outputParamWasNull = rs.wasNull(); 1032 1033 return retValue; 1034 } 1035 1036 1039 public synchronized long getLong(String parameterName) throws SQLException { 1040 ResultSet rs = getOutputParameters(0); 1043 long retValue = rs.getLong(fixParameterName(parameterName)); 1044 1045 this.outputParamWasNull = rs.wasNull(); 1046 1047 return retValue; 1048 } 1049 1050 private int getNamedParamIndex(String paramName, boolean forOut) 1051 throws SQLException { 1052 if ((paramName == null) || (paramName.length() == 0)) { 1053 throw new SQLException (Messages.getString("CallableStatement.2"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 1055 } 1056 1057 CallableStatementParam namedParamInfo = this.paramInfo 1058 .getParameter(paramName); 1059 1060 if (this.paramInfo == null) { 1061 throw new SQLException ( 1062 Messages.getString("CallableStatement.3") + paramName + Messages.getString("CallableStatement.4"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 1064 } 1065 1066 if (forOut && !namedParamInfo.isOut) { 1067 throw new SQLException ( 1068 Messages.getString("CallableStatement.5") + paramName + Messages.getString("CallableStatement.6"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 1071 } 1072 1073 return namedParamInfo.index + 1; } 1075 1076 1079 public synchronized Object getObject(int parameterIndex) 1080 throws SQLException { 1081 CallableStatementParam paramDescriptor = checkIsOutputParam(parameterIndex); 1082 1083 ResultSet rs = getOutputParameters(parameterIndex); 1084 1085 Object retVal = rs.getObjectStoredProc( 1086 mapOutputParameterIndexToRsIndex(parameterIndex), 1087 paramDescriptor.desiredJdbcType); 1088 1089 this.outputParamWasNull = rs.wasNull(); 1090 1091 return retVal; 1092 } 1093 1094 1097 public synchronized Object getObject(int parameterIndex, Map map) 1098 throws SQLException { 1099 ResultSet rs = getOutputParameters(parameterIndex); 1100 1101 Object retVal = rs.getObject( 1102 mapOutputParameterIndexToRsIndex(parameterIndex), map); 1103 1104 this.outputParamWasNull = rs.wasNull(); 1105 1106 return retVal; 1107 } 1108 1109 1112 public synchronized Object getObject(String parameterName) 1113 throws SQLException { 1114 ResultSet rs = getOutputParameters(0); 1117 Object retValue = rs.getObject(fixParameterName(parameterName)); 1118 1119 this.outputParamWasNull = rs.wasNull(); 1120 1121 return retValue; 1122 } 1123 1124 1128 public synchronized Object getObject(String parameterName, Map map) 1129 throws SQLException { 1130 ResultSet rs = getOutputParameters(0); 1133 Object retValue = rs.getObject(fixParameterName(parameterName), map); 1134 1135 this.outputParamWasNull = rs.wasNull(); 1136 1137 return retValue; 1138 } 1139 1140 1150 private ResultSet getOutputParameters(int paramIndex) throws SQLException { 1151 this.outputParamWasNull = false; 1152 1153 if (paramIndex == 1 && this.callingStoredFunction 1154 && this.returnValueParam != null) { 1155 return this.functionReturnValueResults; 1156 } 1157 1158 if (this.outputParameterResults == null) { 1159 if (this.paramInfo.numberOfParameters() == 0) { 1160 throw new SQLException (Messages 1161 .getString("CallableStatement.7"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 1163 } 1164 throw new SQLException (Messages.getString("CallableStatement.8"), SQLError.SQL_STATE_GENERAL_ERROR); 1166 } 1167 1168 return this.outputParameterResults; 1169 1170 } 1171 1172 1175 public synchronized Ref getRef(int parameterIndex) throws SQLException { 1176 ResultSet rs = getOutputParameters(parameterIndex); 1177 1178 Ref retValue = rs 1179 .getRef(mapOutputParameterIndexToRsIndex(parameterIndex)); 1180 1181 this.outputParamWasNull = rs.wasNull(); 1182 1183 return retValue; 1184 } 1185 1186 1189 public synchronized Ref getRef(String parameterName) throws SQLException { 1190 ResultSet rs = getOutputParameters(0); 1193 Ref retValue = rs.getRef(fixParameterName(parameterName)); 1194 1195 this.outputParamWasNull = rs.wasNull(); 1196 1197 return retValue; 1198 } 1199 1200 1203 public synchronized short getShort(int parameterIndex) throws SQLException { 1204 ResultSet rs = getOutputParameters(parameterIndex); 1205 1206 short retValue = rs 1207 .getShort(mapOutputParameterIndexToRsIndex(parameterIndex)); 1208 1209 this.outputParamWasNull = rs.wasNull(); 1210 1211 return retValue; 1212 } 1213 1214 1217 public synchronized short getShort(String parameterName) 1218 throws SQLException { 1219 ResultSet rs = getOutputParameters(0); 1222 short retValue = rs.getShort(fixParameterName(parameterName)); 1223 1224 this.outputParamWasNull = rs.wasNull(); 1225 1226 return retValue; 1227 } 1228 1229 1232 public synchronized String getString(int parameterIndex) 1233 throws SQLException { 1234 ResultSet rs = getOutputParameters(parameterIndex); 1235 1236 String retValue = rs 1237 .getString(mapOutputParameterIndexToRsIndex(parameterIndex)); 1238 1239 this.outputParamWasNull = rs.wasNull(); 1240 1241 return retValue; 1242 } 1243 1244 1247 public synchronized String getString(String parameterName) 1248 throws SQLException { 1249 ResultSet rs = getOutputParameters(0); 1252 String retValue = rs.getString(fixParameterName(parameterName)); 1253 1254 this.outputParamWasNull = rs.wasNull(); 1255 1256 return retValue; 1257 } 1258 1259 1262 public synchronized Time getTime(int parameterIndex) throws SQLException { 1263 ResultSet rs = getOutputParameters(parameterIndex); 1264 1265 Time retValue = rs 1266 .getTime(mapOutputParameterIndexToRsIndex(parameterIndex)); 1267 1268 this.outputParamWasNull = rs.wasNull(); 1269 1270 return retValue; 1271 } 1272 1273 1276 public synchronized Time getTime(int parameterIndex, Calendar cal) 1277 throws SQLException { 1278 ResultSet rs = getOutputParameters(parameterIndex); 1279 1280 Time retValue = rs.getTime( 1281 mapOutputParameterIndexToRsIndex(parameterIndex), cal); 1282 1283 this.outputParamWasNull = rs.wasNull(); 1284 1285 return retValue; 1286 } 1287 1288 1291 public synchronized Time getTime(String parameterName) throws SQLException { 1292 ResultSet rs = getOutputParameters(0); 1295 Time retValue = rs.getTime(fixParameterName(parameterName)); 1296 1297 this.outputParamWasNull = rs.wasNull(); 1298 1299 return retValue; 1300 } 1301 1302 1306 public synchronized Time getTime(String parameterName, Calendar cal) 1307 throws SQLException { 1308 ResultSet rs = getOutputParameters(0); 1311 Time retValue = rs.getTime(fixParameterName(parameterName), cal); 1312 1313 this.outputParamWasNull = rs.wasNull(); 1314 1315 return retValue; 1316 } 1317 1318 1321 public synchronized Timestamp getTimestamp(int parameterIndex) 1322 throws SQLException { 1323 ResultSet rs = getOutputParameters(parameterIndex); 1324 1325 Timestamp retValue = rs 1326 .getTimestamp(mapOutputParameterIndexToRsIndex(parameterIndex)); 1327 1328 this.outputParamWasNull = rs.wasNull(); 1329 1330 return retValue; 1331 } 1332 1333 1336 public synchronized Timestamp getTimestamp(int parameterIndex, Calendar cal) 1337 throws SQLException { 1338 ResultSet rs = getOutputParameters(parameterIndex); 1339 1340 Timestamp retValue = rs.getTimestamp( 1341 mapOutputParameterIndexToRsIndex(parameterIndex), cal); 1342 1343 this.outputParamWasNull = rs.wasNull(); 1344 1345 return retValue; 1346 } 1347 1348 1351 public synchronized Timestamp getTimestamp(String parameterName) 1352 throws SQLException { 1353 ResultSet rs = getOutputParameters(0); 1356 Timestamp retValue = rs.getTimestamp(fixParameterName(parameterName)); 1357 1358 this.outputParamWasNull = rs.wasNull(); 1359 1360 return retValue; 1361 } 1362 1363 1367 public synchronized Timestamp getTimestamp(String parameterName, 1368 Calendar cal) throws SQLException { 1369 ResultSet rs = getOutputParameters(0); 1372 Timestamp retValue = rs.getTimestamp(fixParameterName(parameterName), 1373 cal); 1374 1375 this.outputParamWasNull = rs.wasNull(); 1376 1377 return retValue; 1378 } 1379 1380 1383 public synchronized URL getURL(int parameterIndex) throws SQLException { 1384 ResultSet rs = getOutputParameters(parameterIndex); 1385 1386 URL retValue = rs 1387 .getURL(mapOutputParameterIndexToRsIndex(parameterIndex)); 1388 1389 this.outputParamWasNull = rs.wasNull(); 1390 1391 return retValue; 1392 } 1393 1394 1397 public synchronized URL getURL(String parameterName) throws SQLException { 1398 ResultSet rs = getOutputParameters(0); 1401 URL retValue = rs.getURL(fixParameterName(parameterName)); 1402 1403 this.outputParamWasNull = rs.wasNull(); 1404 1405 return retValue; 1406 } 1407 1408 private int mapOutputParameterIndexToRsIndex(int paramIndex) 1409 throws SQLException { 1410 1411 if (this.returnValueParam != null && paramIndex == 1) { 1412 return 1; 1413 } 1414 1415 checkParameterIndexBounds(paramIndex); 1416 1417 int localParamIndex = paramIndex - 1; 1418 1419 int rsIndex = this.parameterIndexToRsIndex[localParamIndex]; 1420 1421 if (rsIndex == NOT_OUTPUT_PARAMETER_INDICATOR) { 1422 throw new SQLException ( 1423 Messages.getString("CallableStatement.21") + paramIndex + Messages.getString("CallableStatement.22"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 1426 } 1427 1428 return rsIndex + 1; 1429 } 1430 1431 1434 public void registerOutParameter(int parameterIndex, int sqlType) 1435 throws SQLException { 1436 CallableStatementParam paramDescriptor = checkIsOutputParam(parameterIndex); 1437 paramDescriptor.desiredJdbcType = sqlType; 1438 } 1439 1440 1443 public void registerOutParameter(int parameterIndex, int sqlType, int scale) 1444 throws SQLException { 1445 registerOutParameter(parameterIndex, sqlType); 1446 } 1447 1448 1452 public void registerOutParameter(int parameterIndex, int sqlType, 1453 String typeName) throws SQLException { 1454 checkIsOutputParam(parameterIndex); 1455 } 1456 1457 1461 public synchronized void registerOutParameter(String parameterName, 1462 int sqlType) throws SQLException { 1463 registerOutParameter(getNamedParamIndex(parameterName, true), sqlType); 1464 } 1465 1466 1470 public void registerOutParameter(String parameterName, int sqlType, 1471 int scale) throws SQLException { 1472 registerOutParameter(getNamedParamIndex(parameterName, true), sqlType); 1473 } 1474 1475 1479 public void registerOutParameter(String parameterName, int sqlType, 1480 String typeName) throws SQLException { 1481 registerOutParameter(getNamedParamIndex(parameterName, true), sqlType, 1482 typeName); 1483 } 1484 1485 1491 private void retrieveOutParams() throws SQLException { 1492 int numParameters = this.paramInfo.numberOfParameters(); 1493 1494 this.parameterIndexToRsIndex = new int[numParameters]; 1495 1496 for (int i = 0; i < numParameters; i++) { 1497 this.parameterIndexToRsIndex[i] = NOT_OUTPUT_PARAMETER_INDICATOR; 1498 } 1499 1500 int localParamIndex = 0; 1501 1502 if (numParameters > 0) { 1503 StringBuffer outParameterQuery = new StringBuffer ("SELECT "); 1505 boolean firstParam = true; 1506 boolean hadOutputParams = false; 1507 1508 for (Iterator paramIter = this.paramInfo.iterator(); paramIter 1509 .hasNext();) { 1510 CallableStatementParam retrParamInfo = (CallableStatementParam) paramIter 1511 .next(); 1512 1513 if (retrParamInfo.isOut) { 1514 hadOutputParams = true; 1515 1516 this.parameterIndexToRsIndex[retrParamInfo.index] = localParamIndex++; 1517 1518 String outParameterName = mangleParameterName(retrParamInfo.paramName); 1519 1520 if (!firstParam) { 1521 outParameterQuery.append(","); } else { 1523 firstParam = false; 1524 } 1525 1526 if (!outParameterName.startsWith("@")) { outParameterQuery.append('@'); 1528 } 1529 1530 outParameterQuery.append(outParameterName); 1531 } 1532 } 1533 1534 if (hadOutputParams) { 1535 java.sql.Statement outParameterStmt = null; 1538 java.sql.ResultSet outParamRs = null; 1539 1540 try { 1541 outParameterStmt = this.connection.createStatement(); 1542 outParamRs = outParameterStmt 1543 .executeQuery(outParameterQuery.toString()); 1544 this.outputParameterResults = ((com.mysql.jdbc.ResultSet) outParamRs) 1545 .copy(); 1546 1547 if (!this.outputParameterResults.next()) { 1548 this.outputParameterResults.close(); 1549 this.outputParameterResults = null; 1550 } 1551 } finally { 1552 if (outParameterStmt != null) { 1553 outParameterStmt.close(); 1554 } 1555 } 1556 } else { 1557 this.outputParameterResults = null; 1558 } 1559 } else { 1560 this.outputParameterResults = null; 1561 } 1562 } 1563 1564 1568 public void setAsciiStream(String parameterName, InputStream x, int length) 1569 throws SQLException { 1570 setAsciiStream(getNamedParamIndex(parameterName, false), x, length); 1571 } 1572 1573 1577 public void setBigDecimal(String parameterName, BigDecimal x) 1578 throws SQLException { 1579 setBigDecimal(getNamedParamIndex(parameterName, false), x); 1580 } 1581 1582 1586 public void setBinaryStream(String parameterName, InputStream x, int length) 1587 throws SQLException { 1588 setBinaryStream(getNamedParamIndex(parameterName, false), x, length); 1589 } 1590 1591 1594 public void setBoolean(String parameterName, boolean x) throws SQLException { 1595 setBoolean(getNamedParamIndex(parameterName, false), x); 1596 } 1597 1598 1601 public void setByte(String parameterName, byte x) throws SQLException { 1602 setByte(getNamedParamIndex(parameterName, false), x); 1603 } 1604 1605 1608 public void setBytes(String parameterName, byte[] x) throws SQLException { 1609 setBytes(getNamedParamIndex(parameterName, false), x); 1610 } 1611 1612 1616 public void setCharacterStream(String parameterName, Reader reader, 1617 int length) throws SQLException { 1618 setCharacterStream(getNamedParamIndex(parameterName, false), reader, 1619 length); 1620 } 1621 1622 1625 public void setDate(String parameterName, Date x) throws SQLException { 1626 setDate(getNamedParamIndex(parameterName, false), x); 1627 } 1628 1629 1633 public void setDate(String parameterName, Date x, Calendar cal) 1634 throws SQLException { 1635 setDate(getNamedParamIndex(parameterName, false), x, cal); 1636 } 1637 1638 1641 public void setDouble(String parameterName, double x) throws SQLException { 1642 setDouble(getNamedParamIndex(parameterName, false), x); 1643 } 1644 1645 1648 public void setFloat(String parameterName, float x) throws SQLException { 1649 setFloat(getNamedParamIndex(parameterName, false), x); 1650 } 1651 1652 1655 private void setInOutParamsOnServer() throws SQLException { 1656 if (this.paramInfo.numParameters > 0) { 1657 int parameterIndex = 0; 1658 1659 for (Iterator paramIter = this.paramInfo.iterator(); paramIter 1660 .hasNext();) { 1661 1662 CallableStatementParam inParamInfo = (CallableStatementParam) paramIter 1663 .next(); 1664 1665 if (inParamInfo.isOut && inParamInfo.isIn) { 1666 String inOutParameterName = mangleParameterName(inParamInfo.paramName); 1667 StringBuffer queryBuf = new StringBuffer ( 1668 4 + inOutParameterName.length() + 1 + 1); 1669 queryBuf.append("SET "); queryBuf.append(inOutParameterName); 1671 queryBuf.append("=?"); 1673 PreparedStatement setPstmt = null; 1674 1675 try { 1676 setPstmt = this.connection 1677 .clientPrepareStatement(queryBuf.toString()); 1678 1679 byte[] parameterAsBytes = this 1680 .getBytesRepresentation(parameterIndex); 1681 1682 if (parameterAsBytes != null) { 1683 if (parameterAsBytes.length > 8 1684 && parameterAsBytes[0] == '_' 1685 && parameterAsBytes[1] == 'b' 1686 && parameterAsBytes[2] == 'i' 1687 && parameterAsBytes[3] == 'n' 1688 && parameterAsBytes[4] == 'a' 1689 && parameterAsBytes[5] == 'r' 1690 && parameterAsBytes[6] == 'y' 1691 && parameterAsBytes[7] == '\'') { 1692 setPstmt.setBytesNoEscapeNoQuotes(1, 1693 parameterAsBytes); 1694 } else { 1695 setPstmt.setBytes(1, parameterAsBytes); 1696 } 1697 } else { 1698 setPstmt.setNull(1, Types.NULL); 1699 } 1700 1701 setPstmt.executeUpdate(); 1702 } finally { 1703 if (setPstmt != null) { 1704 setPstmt.close(); 1705 } 1706 } 1707 1708 1717 } 1718 } 1719 1720 parameterIndex++; 1721 } 1722 1723 } 1724 1725 1728 public void setInt(String parameterName, int x) throws SQLException { 1729 setInt(getNamedParamIndex(parameterName, false), x); 1730 } 1731 1732 1735 public void setLong(String parameterName, long x) throws SQLException { 1736 } 1737 1738 1741 public void setNull(String parameterName, int sqlType) throws SQLException { 1742 } 1743 1744 1748 public void setNull(String parameterName, int sqlType, String typeName) 1749 throws SQLException { 1750 } 1751 1752 1756 public void setObject(String parameterName, Object x) throws SQLException { 1757 setObject(getNamedParamIndex(parameterName, false), x); 1758 } 1759 1760 1764 public void setObject(String parameterName, Object x, int targetSqlType) 1765 throws SQLException { 1766 setObject(getNamedParamIndex(parameterName, false), x, targetSqlType); 1767 } 1768 1769 1773 public void setObject(String parameterName, Object x, int targetSqlType, 1774 int scale) throws SQLException { 1775 } 1776 1777 private void setOutParams() throws SQLException { 1778 if (this.paramInfo.numParameters > 0) { 1779 for (Iterator paramIter = this.paramInfo.iterator(); paramIter 1780 .hasNext();) { 1781 CallableStatementParam outParamInfo = (CallableStatementParam) paramIter 1782 .next(); 1783 1784 if (outParamInfo.isOut) { 1785 String outParameterName = mangleParameterName(outParamInfo.paramName); 1786 1787 this.setBytesNoEscapeNoQuotes(outParamInfo.index + 1, 1788 StringUtils.getBytes(outParameterName, 1789 this.charConverter, this.charEncoding, 1790 this.connection 1791 .getServerCharacterEncoding(), 1792 this.connection.parserKnowsUnicode())); 1793 } 1794 } 1795 } 1796 } 1797 1798 1801 public void setShort(String parameterName, short x) throws SQLException { 1802 setShort(getNamedParamIndex(parameterName, false), x); 1803 } 1804 1805 1809 public void setString(String parameterName, String x) throws SQLException { 1810 setString(getNamedParamIndex(parameterName, false), x); 1811 } 1812 1813 1816 public void setTime(String parameterName, Time x) throws SQLException { 1817 setTime(getNamedParamIndex(parameterName, false), x); 1818 } 1819 1820 1824 public void setTime(String parameterName, Time x, Calendar cal) 1825 throws SQLException { 1826 setTime(getNamedParamIndex(parameterName, false), x, cal); 1827 } 1828 1829 1833 public void setTimestamp(String parameterName, Timestamp x) 1834 throws SQLException { 1835 setTimestamp(getNamedParamIndex(parameterName, false), x); 1836 } 1837 1838 1842 public void setTimestamp(String parameterName, Timestamp x, Calendar cal) 1843 throws SQLException { 1844 setTimestamp(getNamedParamIndex(parameterName, false), x, cal); 1845 } 1846 1847 1850 public void setURL(String parameterName, URL val) throws SQLException { 1851 setURL(getNamedParamIndex(parameterName, false), val); 1852 } 1853 1854 1857 public synchronized boolean wasNull() throws SQLException { 1858 return this.outputParamWasNull; 1859 } 1860} 1861 | Popular Tags |