| 1 | /* |
| 2 | Copyright (C) 2002-2004 MySQL AB |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of version 2 of the GNU General Public License as |
| 6 | published by the Free Software Foundation. |
| 7 | |
| 8 | There are special exceptions to the terms and conditions of the GPL |
| 9 | as it is applied to this software. View the full text of the |
| 10 | exception in file EXCEPTIONS-CONNECTOR-J in the directory of this |
| 11 | software distribution. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | |
| 22 | |
| 23 | |
| 24 | */ |
| 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 | /** |
| 54 | * Representation of stored procedures for JDBC |
| 55 | * |
| 56 | * @author Mark Matthews |
| 57 | * @version $Id: CallableStatement.java,v 1.1.2.1 2005/05/13 18:58:38 mmatthews |
| 58 | * Exp $ |
| 59 | */ |
| 60 | public class CallableStatement extends PreparedStatement implements |
| 61 | java.sql.CallableStatement { |
| 62 | class CallableStatementParam { |
| 63 | int desiredJdbcType; |
| 64 | |
| 65 | int index; |
| 66 | |
| 67 | int inOutModifier; |
| 68 | |
| 69 | boolean isIn; |
| 70 | |
| 71 | boolean isOut; |
| 72 | |
| 73 | int jdbcType; |
| 74 | short nullability; |
| 75 | String paramName; |
| 76 | int precision; |
| 77 | int scale; |
| 78 | String typeName; |
| 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 | /* |
| 98 | * (non-Javadoc) |
| 99 | * |
| 100 | * @see java.lang.Object#clone() |
| 101 | */ |
| 102 | protected Object clone() throws CloneNotSupportedException { |
| 103 | return super.clone(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | class CallableStatementParamInfo { |
| 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 | // first row will be return value parameter |
| 148 | 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 | protected void checkBounds(int paramIndex) throws SQLException { |
| 186 | int localParamIndex = paramIndex - 1; |
| 187 | |
| 188 | if ((paramIndex < 0) || (localParamIndex >= this.numParameters)) { |
| 189 | throw new SQLException ( |
| 190 | Messages.getString("CallableStatement.11") + paramIndex //$NON-NLS-1$ |
| 191 | + Messages.getString("CallableStatement.12") + numParameters //$NON-NLS-1$ |
| 192 | + Messages.getString("CallableStatement.13"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * (non-Javadoc) |
| 198 | * |
| 199 | * @see java.lang.Object#clone() |
| 200 | */ |
| 201 | protected Object clone() throws CloneNotSupportedException { |
| 202 | // TODO Auto-generated method stub |
| 203 | return super.clone(); |
| 204 | } |
| 205 | |
| 206 | CallableStatementParam getParameter(int index) { |
| 207 | return (CallableStatementParam) this.parameterList.get(index); |
| 208 | } |
| 209 | |
| 210 | CallableStatementParam getParameter(String name) { |
| 211 | return (CallableStatementParam) this.parameterMap.get(name); |
| 212 | } |
| 213 | |
| 214 | public String getParameterClassName(int arg0) throws SQLException { |
| 215 | String mysqlTypeName = getParameterTypeName(arg0); |
| 216 | |
| 217 | boolean isBinaryOrBlob = StringUtils.indexOfIgnoreCase(mysqlTypeName, "BLOB") != -1 || |
| 218 | StringUtils.indexOfIgnoreCase(mysqlTypeName, "BINARY") != -1; |
| 219 | |
| 220 | boolean isUnsigned = StringUtils.indexOfIgnoreCase(mysqlTypeName, "UNSIGNED") != -1; |
| 221 | |
| 222 | int mysqlTypeIfKnown = 0; |
| 223 | |
| 224 | if (StringUtils.startsWithIgnoreCase(mysqlTypeName, "MEDIUMINT")) { |
| 225 | mysqlTypeIfKnown = MysqlDefs.FIELD_TYPE_INT24; |
| 226 | } |
| 227 | |
| 228 | return ResultSetMetaData.getClassNameForJavaType(getParameterType(arg0), |
| 229 | isUnsigned, mysqlTypeIfKnown, isBinaryOrBlob, false); |
| 230 | } |
| 231 | |
| 232 | public int getParameterCount() throws SQLException { |
| 233 | if (this.parameterList == null) { |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | return this.parameterList.size(); |
| 238 | } |
| 239 | |
| 240 | public int getParameterMode(int arg0) throws SQLException { |
| 241 | checkBounds(arg0); |
| 242 | |
| 243 | return getParameter(arg0 - 1).inOutModifier; |
| 244 | } |
| 245 | |
| 246 | public int getParameterType(int arg0) throws SQLException { |
| 247 | checkBounds(arg0); |
| 248 | |
| 249 | return getParameter(arg0 - 1).jdbcType; |
| 250 | } |
| 251 | |
| 252 | public String getParameterTypeName(int arg0) throws SQLException { |
| 253 | checkBounds(arg0); |
| 254 | |
| 255 | return getParameter(arg0 - 1).typeName; |
| 256 | } |
| 257 | |
| 258 | public int getPrecision(int arg0) throws SQLException { |
| 259 | checkBounds(arg0); |
| 260 | |
| 261 | return getParameter(arg0 - 1).precision; |
| 262 | } |
| 263 | |
| 264 | public int getScale(int arg0) throws SQLException { |
| 265 | checkBounds(arg0); |
| 266 | |
| 267 | return getParameter(arg0 - 1).scale; |
| 268 | } |
| 269 | |
| 270 | public int isNullable(int arg0) throws SQLException { |
| 271 | checkBounds(arg0); |
| 272 | |
| 273 | return getParameter(arg0 - 1).nullability; |
| 274 | } |
| 275 | |
| 276 | public boolean isSigned(int arg0) throws SQLException { |
| 277 | checkBounds(arg0); |
| 278 | |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | Iterator iterator() { |
| 283 | return this.parameterList.iterator(); |
| 284 | } |
| 285 | |
| 286 | int numberOfParameters() { |
| 287 | return this.numParameters; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Can't implement this directly, as then you can't use callable statements |
| 293 | * on JDK-1.3.1, which unfortunately isn't EOL'd yet, and still present |
| 294 | * quite a bit out there in the wild (Websphere, FreeBSD, anyone?) |
| 295 | */ |
| 296 | |
| 297 | class CallableStatementParamInfoJDBC3 extends CallableStatementParamInfo |
| 298 | implements ParameterMetaData { |
| 299 | |
| 300 | CallableStatementParamInfoJDBC3(java.sql.ResultSet paramTypesRs) |
| 301 | throws SQLException { |
| 302 | super(paramTypesRs); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | private final static int NOT_OUTPUT_PARAMETER_INDICATOR = Integer.MIN_VALUE; |
| 307 | |
| 308 | private final static String PARAMETER_NAMESPACE_PREFIX = "@com_mysql_jdbc_outparam_"; //$NON-NLS-1$ |
| 309 | |
| 310 | private static String mangleParameterName(String origParameterName) { |
| 311 | if (origParameterName == null) { |
| 312 | return null; |
| 313 | } |
| 314 | |
| 315 | int offset = 0; |
| 316 | |
| 317 | if (origParameterName.length() > 0 |
| 318 | && origParameterName.charAt(0) == '@') { |
| 319 | offset = 1; |
| 320 | } |
| 321 | |
| 322 | StringBuffer paramNameBuf = new StringBuffer(PARAMETER_NAMESPACE_PREFIX |
| 323 | .length() |
| 324 | + origParameterName.length()); |
| 325 | paramNameBuf.append(PARAMETER_NAMESPACE_PREFIX); |
| 326 | paramNameBuf.append(origParameterName.substring(offset)); |
| 327 | |
| 328 | return paramNameBuf.toString(); |
| 329 | } |
| 330 | |
| 331 | private boolean callingStoredFunction = false; |
| 332 | |
| 333 | private ResultSet functionReturnValueResults; |
| 334 | |
| 335 | private boolean hasOutputParams = false; |
| 336 | |
| 337 | // private List parameterList; |
| 338 | // private Map parameterMap; |
| 339 | private ResultSet outputParameterResults; |
| 340 | |
| 341 | private boolean outputParamWasNull = false; |
| 342 | |
| 343 | private int[] parameterIndexToRsIndex; |
| 344 | |
| 345 | protected CallableStatementParamInfo paramInfo; |
| 346 | |
| 347 | private CallableStatementParam returnValueParam; |
| 348 | |
| 349 | /** |
| 350 | * Creates a new CallableStatement |
| 351 | * |
| 352 | * @param conn |
| 353 | * the connection creating this statement |
| 354 | * @param paramInfo |
| 355 | * the SQL to prepare |
| 356 | * |
| 357 | * @throws SQLException |
| 358 | * if an error occurs |
| 359 | */ |
| 360 | public CallableStatement(Connection conn, |
| 361 | CallableStatementParamInfo paramInfo) throws SQLException { |
| 362 | super(conn, paramInfo.nativeSql, paramInfo.catalogInUse); |
| 363 | |
| 364 | this.paramInfo = paramInfo; |
| 365 | this.callingStoredFunction = this.paramInfo.isFunctionCall; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Creates a new CallableStatement |
| 370 | * |
| 371 | * @param conn |
| 372 | * the connection creating this statement |
| 373 | * @param catalog |
| 374 | * catalog the current catalog |
| 375 | * |
| 376 | * @throws SQLException |
| 377 | * if an error occurs |
| 378 | */ |
| 379 | public CallableStatement(Connection conn, String catalog) |
| 380 | throws SQLException { |
| 381 | super(conn, catalog, null); |
| 382 | |
| 383 | determineParameterTypes(); |
| 384 | generateParameterMap(); |
| 385 | } |
| 386 | |
| 387 | private int[] placeholderToParameterIndexMap; |
| 388 | |
| 389 | private void generateParameterMap() throws SQLException { |
| 390 | // if the user specified some parameters as literals, we need to |
| 391 | // provide a map from the specified placeholders to the actual |
| 392 | // parameter numbers |
| 393 | |
| 394 | if (this.paramInfo != null && |
| 395 | this.parameterCount != this.paramInfo.getParameterCount()) { |
| 396 | this.placeholderToParameterIndexMap = new int[this.parameterCount]; |
| 397 | |
| 398 | int startPos = StringUtils.indexOfIgnoreCase(this.originalSql, |
| 399 | "CALL"); |
| 400 | if (startPos != -1) { |
| 401 | int parenOpenPos = this.originalSql.indexOf('(', startPos + 4); |
| 402 | |
| 403 | if (parenOpenPos != -1) { |
| 404 | int parenClosePos = StringUtils.indexOfIgnoreCaseRespectQuotes(parenOpenPos, |
| 405 | this.originalSql, ")", '\'', true); |
| 406 | |
| 407 | if (parenClosePos != -1) { |
| 408 | List parsedParameters = StringUtils.split(this.originalSql.substring(parenOpenPos + 1, parenClosePos), ",", "'\"", "'\"", true); |
| 409 | |
| 410 | int numParsedParameters = parsedParameters.size(); |
| 411 | |
| 412 | // sanity check |
| 413 | |
| 414 | if (numParsedParameters != this.parameterCount) { |
| 415 | // bail? |
| 416 | } |
| 417 | |
| 418 | int placeholderCount = 0; |
| 419 | |
| 420 | for (int i = 0; i < numParsedParameters; i++) { |
| 421 | if (((String)parsedParameters.get(i)).equals("?")) { |
| 422 | this.placeholderToParameterIndexMap[placeholderCount++] = i; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Creates a new CallableStatement |
| 433 | * |
| 434 | * @param conn |
| 435 | * the connection creating this statement |
| 436 | * @param sql |
| 437 | * the SQL to prepare |
| 438 | * @param catalog |
| 439 | * the current catalog |
| 440 | * |
| 441 | * @throws SQLException |
| 442 | * if an error occurs |
| 443 | */ |
| 444 | public CallableStatement(Connection conn, String sql, String catalog, |
| 445 | boolean isFunctionCall) throws SQLException { |
| 446 | super(conn, sql, catalog); |
| 447 | |
| 448 | this.callingStoredFunction = isFunctionCall; |
| 449 | |
| 450 | determineParameterTypes(); |
| 451 | generateParameterMap(); |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * (non-Javadoc) |
| 456 | * |
| 457 | * @see java.sql.PreparedStatement#addBatch() |
| 458 | */ |
| 459 | public void addBatch() throws SQLException { |
| 460 | setOutParams(); |
| 461 | |
| 462 | super.addBatch(); |
| 463 | } |
| 464 | |
| 465 | private CallableStatementParam checkIsOutputParam(int paramIndex) |
| 466 | throws SQLException { |
| 467 | |
| 468 | if (this.callingStoredFunction) { |
| 469 | if (paramIndex == 1) { |
| 470 | |
| 471 | if (this.returnValueParam == null) { |
| 472 | this.returnValueParam = new CallableStatementParam("", 0, |
| 473 | false, true, Types.VARCHAR, "VARCHAR", 0, 0, |
| 474 | DatabaseMetaData.attributeNullableUnknown, |
| 475 | DatabaseMetaData.procedureColumnReturn); |
| 476 | } |
| 477 | |
| 478 | return this.returnValueParam; |
| 479 | } |
| 480 | |
| 481 | // Move to position in output result set |
| 482 | paramIndex--; |
| 483 | } |
| 484 | |
| 485 | checkParameterIndexBounds(paramIndex); |
| 486 | |
| 487 | int localParamIndex = paramIndex - 1; |
| 488 | |
| 489 | if (this.placeholderToParameterIndexMap != null) { |
| 490 | localParamIndex = this.placeholderToParameterIndexMap[localParamIndex]; |
| 491 | } |
| 492 | |
| 493 | CallableStatementParam paramDescriptor = this.paramInfo |
| 494 | .getParameter(localParamIndex); |
| 495 | |
| 496 | if (!paramDescriptor.isOut) { |
| 497 | throw new SQLException( |
| 498 | Messages.getString("CallableStatement.9") + paramIndex //$NON-NLS-1$ |
| 499 | + Messages.getString("CallableStatement.10"), //$NON-NLS-1$ |
| 500 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 501 | } |
| 502 | |
| 503 | this.hasOutputParams = true; |
| 504 | |
| 505 | return paramDescriptor; |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * DOCUMENT ME! |
| 510 | * |
| 511 | * @param paramIndex |
| 512 | * |
| 513 | * @throws SQLException |
| 514 | */ |
| 515 | private void checkParameterIndexBounds(int paramIndex) throws SQLException { |
| 516 | this.paramInfo.checkBounds(paramIndex); |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Checks whether or not this statement is supposed to be providing |
| 521 | * streamable result sets...If output parameters are registered, the driver |
| 522 | * can not stream the results. |
| 523 | * |
| 524 | * @throws SQLException |
| 525 | * DOCUMENT ME! |
| 526 | */ |
| 527 | private void checkStreamability() throws SQLException { |
| 528 | if (this.hasOutputParams && createStreamingResultSet()) { |
| 529 | throw new SQLException(Messages.getString("CallableStatement.14"), //$NON-NLS-1$ |
| 530 | SQLError.SQL_STATE_DRIVER_NOT_CAPABLE); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | public synchronized void clearParameters() throws SQLException { |
| 535 | super.clearParameters(); |
| 536 | |
| 537 | try { |
| 538 | if (this.outputParameterResults != null) { |
| 539 | this.outputParameterResults.close(); |
| 540 | } |
| 541 | } finally { |
| 542 | this.outputParameterResults = null; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | private void determineParameterTypes() throws SQLException { |
| 547 | java.sql.ResultSet paramTypesRs = null; |
| 548 | |
| 549 | try { |
| 550 | String procName = extractProcedureName(); |
| 551 | |
| 552 | java.sql.DatabaseMetaData dbmd = this.connection.getMetaData(); |
| 553 | |
| 554 | boolean useCatalog = false; |
| 555 | |
| 556 | if (procName.indexOf(".") == -1) { |
| 557 | useCatalog = true; |
| 558 | } |
| 559 | |
| 560 | paramTypesRs = dbmd.getProcedureColumns(this.connection |
| 561 | .versionMeetsMinimum(5, 0, 2) |
| 562 | & useCatalog ? this.currentCatalog : null, null, procName, |
| 563 | "%"); //$NON-NLS-1$ |
| 564 | |
| 565 | if (!this.connection.isRunningOnJDK13()) { |
| 566 | this.paramInfo = new CallableStatementParamInfoJDBC3( |
| 567 | paramTypesRs); |
| 568 | } else { |
| 569 | this.paramInfo = new CallableStatementParamInfo(paramTypesRs); |
| 570 | } |
| 571 | } finally { |
| 572 | SQLException sqlExRethrow = null; |
| 573 | |
| 574 | if (paramTypesRs != null) { |
| 575 | try { |
| 576 | paramTypesRs.close(); |
| 577 | } catch (SQLException sqlEx) { |
| 578 | sqlExRethrow = sqlEx; |
| 579 | } |
| 580 | |
| 581 | paramTypesRs = null; |
| 582 | } |
| 583 | |
| 584 | if (sqlExRethrow != null) { |
| 585 | throw sqlExRethrow; |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * (non-Javadoc) |
| 592 | * |
| 593 | * @see java.sql.PreparedStatement#execute() |
| 594 | */ |
| 595 | public boolean execute() throws SQLException { |
| 596 | boolean returnVal = false; |
| 597 | |
| 598 | checkClosed(); |
| 599 | |
| 600 | checkStreamability(); |
| 601 | |
| 602 | synchronized (this.connection.getMutex()) { |
| 603 | setInOutParamsOnServer(); |
| 604 | setOutParams(); |
| 605 | |
| 606 | returnVal = super.execute(); |
| 607 | |
| 608 | if (this.callingStoredFunction) { |
| 609 | this.functionReturnValueResults = this.results; |
| 610 | this.functionReturnValueResults.next(); |
| 611 | this.results = null; |
| 612 | } |
| 613 | |
| 614 | retrieveOutParams(); |
| 615 | } |
| 616 | |
| 617 | if (!this.callingStoredFunction) { |
| 618 | return returnVal; |
| 619 | } |
| 620 | |
| 621 | // Functions can't return results |
| 622 | return false; |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | * (non-Javadoc) |
| 627 | * |
| 628 | * @see java.sql.PreparedStatement#executeQuery() |
| 629 | */ |
| 630 | public synchronized java.sql.ResultSet executeQuery() throws SQLException { |
| 631 | checkClosed(); |
| 632 | |
| 633 | checkStreamability(); |
| 634 | |
| 635 | java.sql.ResultSet execResults = null; |
| 636 | |
| 637 | synchronized (this.connection.getMutex()) { |
| 638 | setInOutParamsOnServer(); |
| 639 | setOutParams(); |
| 640 | |
| 641 | execResults = super.executeQuery(); |
| 642 | |
| 643 | retrieveOutParams(); |
| 644 | } |
| 645 | |
| 646 | return execResults; |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * (non-Javadoc) |
| 651 | * |
| 652 | * @see java.sql.PreparedStatement#executeUpdate() |
| 653 | */ |
| 654 | public synchronized int executeUpdate() throws SQLException { |
| 655 | int returnVal = -1; |
| 656 | |
| 657 | checkClosed(); |
| 658 | |
| 659 | checkStreamability(); |
| 660 | |
| 661 | if (this.callingStoredFunction) { |
| 662 | execute(); |
| 663 | |
| 664 | return -1; |
| 665 | } |
| 666 | |
| 667 | synchronized (this.connection.getMutex()) { |
| 668 | setInOutParamsOnServer(); |
| 669 | setOutParams(); |
| 670 | |
| 671 | returnVal = super.executeUpdate(); |
| 672 | |
| 673 | retrieveOutParams(); |
| 674 | } |
| 675 | |
| 676 | return returnVal; |
| 677 | } |
| 678 | |
| 679 | private String extractProcedureName() throws SQLException { |
| 680 | // TODO: Do this with less memory allocation |
| 681 | int endCallIndex = StringUtils.indexOfIgnoreCase(this.originalSql, |
| 682 | "CALL "); //$NON-NLS-1$ |
| 683 | int offset = 5; |
| 684 | |
| 685 | if (endCallIndex == -1) { |
| 686 | endCallIndex = StringUtils.indexOfIgnoreCase(this.originalSql, |
| 687 | "SELECT "); |
| 688 | offset = 7; |
| 689 | } |
| 690 | |
| 691 | if (endCallIndex != -1) { |
| 692 | StringBuffer nameBuf = new StringBuffer(); |
| 693 | |
| 694 | String trimmedStatement = this.originalSql.substring( |
| 695 | endCallIndex + offset).trim(); |
| 696 | |
| 697 | int statementLength = trimmedStatement.length(); |
| 698 | |
| 699 | for (int i = 0; i < statementLength; i++) { |
| 700 | char c = trimmedStatement.charAt(i); |
| 701 | |
| 702 | if (Character.isWhitespace(c) || (c == '(') || (c == '?')) { |
| 703 | break; |
| 704 | } |
| 705 | nameBuf.append(c); |
| 706 | |
| 707 | } |
| 708 | |
| 709 | return nameBuf.toString(); |
| 710 | } |
| 711 | throw new SQLException(Messages.getString("CallableStatement.1"), //$NON-NLS-1$ |
| 712 | SQLError.SQL_STATE_GENERAL_ERROR); |
| 713 | |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * Adds 'at' symbol to beginning of parameter names if needed. |
| 718 | * |
| 719 | * @param paramNameIn |
| 720 | * the parameter name to 'fix' |
| 721 | * |
| 722 | * @return the parameter name with an 'a' prepended, if needed |
| 723 | * |
| 724 | * @throws SQLException |
| 725 | * if the parameter name is null or empty. |
| 726 | */ |
| 727 | private String fixParameterName(String paramNameIn) throws SQLException { |
| 728 | if ((paramNameIn == null) || (paramNameIn.length() == 0)) { |
| 729 | throw new SQLException( |
| 730 | ((Messages.getString("CallableStatement.0") + paramNameIn) == null) //$NON-NLS-1$ |
| 731 | ? Messages.getString("CallableStatement.15") : Messages.getString("CallableStatement.16"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ //$NON-NLS-2$ |
| 732 | } |
| 733 | |
| 734 | return mangleParameterName(paramNameIn); |
| 735 | |
| 736 | /* |
| 737 | * if (paramNameIn.startsWith("@")) { return paramNameIn; } else { |
| 738 | * StringBuffer paramNameBuf = new StringBuffer("@"); |
| 739 | * paramNameBuf.append(paramNameIn); |
| 740 | * |
| 741 | * return paramNameBuf.toString(); } |
| 742 | */ |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * @see java.sql.CallableStatement#getArray(int) |
| 747 | */ |
| 748 | public synchronized Array getArray(int i) throws SQLException { |
| 749 | ResultSet rs = getOutputParameters(i); |
| 750 | |
| 751 | Array retValue = rs.getArray(mapOutputParameterIndexToRsIndex(i)); |
| 752 | |
| 753 | this.outputParamWasNull = rs.wasNull(); |
| 754 | |
| 755 | return retValue; |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * @see java.sql.CallableStatement#getArray(java.lang.String) |
| 760 | */ |
| 761 | public synchronized Array getArray(String parameterName) |
| 762 | throws SQLException { |
| 763 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 764 | // from ?= |
| 765 | |
| 766 | Array retValue = rs.getArray(fixParameterName(parameterName)); |
| 767 | |
| 768 | this.outputParamWasNull = rs.wasNull(); |
| 769 | |
| 770 | return retValue; |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * @see java.sql.CallableStatement#getBigDecimal(int) |
| 775 | */ |
| 776 | public synchronized BigDecimal getBigDecimal(int parameterIndex) |
| 777 | throws SQLException { |
| 778 | ResultSet rs = getOutputParameters(parameterIndex); |
| 779 | |
| 780 | BigDecimal retValue = rs |
| 781 | .getBigDecimal(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 782 | |
| 783 | this.outputParamWasNull = rs.wasNull(); |
| 784 | |
| 785 | return retValue; |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * DOCUMENT ME! |
| 790 | * |
| 791 | * @param parameterIndex |
| 792 | * DOCUMENT ME! |
| 793 | * @param scale |
| 794 | * DOCUMENT ME! |
| 795 | * |
| 796 | * @return DOCUMENT ME! |
| 797 | * |
| 798 | * @throws SQLException |
| 799 | * DOCUMENT ME! |
| 800 | * |
| 801 | * @see java.sql.CallableStatement#getBigDecimal(int, int) |
| 802 | * @deprecated |
| 803 | */ |
| 804 | public synchronized BigDecimal getBigDecimal(int parameterIndex, int scale) |
| 805 | throws SQLException { |
| 806 | ResultSet rs = getOutputParameters(parameterIndex); |
| 807 | |
| 808 | BigDecimal retValue = rs.getBigDecimal( |
| 809 | mapOutputParameterIndexToRsIndex(parameterIndex), scale); |
| 810 | |
| 811 | this.outputParamWasNull = rs.wasNull(); |
| 812 | |
| 813 | return retValue; |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * @see java.sql.CallableStatement#getBigDecimal(java.lang.String) |
| 818 | */ |
| 819 | public synchronized BigDecimal getBigDecimal(String parameterName) |
| 820 | throws SQLException { |
| 821 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 822 | // from ?= |
| 823 | |
| 824 | BigDecimal retValue = rs.getBigDecimal(fixParameterName(parameterName)); |
| 825 | |
| 826 | this.outputParamWasNull = rs.wasNull(); |
| 827 | |
| 828 | return retValue; |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * @see java.sql.CallableStatement#getBlob(int) |
| 833 | */ |
| 834 | public synchronized Blob getBlob(int parameterIndex) throws SQLException { |
| 835 | ResultSet rs = getOutputParameters(parameterIndex); |
| 836 | |
| 837 | Blob retValue = rs |
| 838 | .getBlob(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 839 | |
| 840 | this.outputParamWasNull = rs.wasNull(); |
| 841 | |
| 842 | return retValue; |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * @see java.sql.CallableStatement#getBlob(java.lang.String) |
| 847 | */ |
| 848 | public synchronized Blob getBlob(String parameterName) throws SQLException { |
| 849 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 850 | // from ?= |
| 851 | |
| 852 | Blob retValue = rs.getBlob(fixParameterName(parameterName)); |
| 853 | |
| 854 | this.outputParamWasNull = rs.wasNull(); |
| 855 | |
| 856 | return retValue; |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * @see java.sql.CallableStatement#getBoolean(int) |
| 861 | */ |
| 862 | public synchronized boolean getBoolean(int parameterIndex) |
| 863 | throws SQLException { |
| 864 | ResultSet rs = getOutputParameters(parameterIndex); |
| 865 | |
| 866 | boolean retValue = rs |
| 867 | .getBoolean(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 868 | |
| 869 | this.outputParamWasNull = rs.wasNull(); |
| 870 | |
| 871 | return retValue; |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * @see java.sql.CallableStatement#getBoolean(java.lang.String) |
| 876 | */ |
| 877 | public synchronized boolean getBoolean(String parameterName) |
| 878 | throws SQLException { |
| 879 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 880 | // from ?= |
| 881 | |
| 882 | boolean retValue = rs.getBoolean(fixParameterName(parameterName)); |
| 883 | |
| 884 | this.outputParamWasNull = rs.wasNull(); |
| 885 | |
| 886 | return retValue; |
| 887 | } |
| 888 | |
| 889 | /** |
| 890 | * @see java.sql.CallableStatement#getByte(int) |
| 891 | */ |
| 892 | public synchronized byte getByte(int parameterIndex) throws SQLException { |
| 893 | ResultSet rs = getOutputParameters(parameterIndex); |
| 894 | |
| 895 | byte retValue = rs |
| 896 | .getByte(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 897 | |
| 898 | this.outputParamWasNull = rs.wasNull(); |
| 899 | |
| 900 | return retValue; |
| 901 | } |
| 902 | |
| 903 | /** |
| 904 | * @see java.sql.CallableStatement#getByte(java.lang.String) |
| 905 | */ |
| 906 | public synchronized byte getByte(String parameterName) throws SQLException { |
| 907 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 908 | // from ?= |
| 909 | |
| 910 | byte retValue = rs.getByte(fixParameterName(parameterName)); |
| 911 | |
| 912 | this.outputParamWasNull = rs.wasNull(); |
| 913 | |
| 914 | return retValue; |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * @see java.sql.CallableStatement#getBytes(int) |
| 919 | */ |
| 920 | public synchronized byte[] getBytes(int parameterIndex) throws SQLException { |
| 921 | ResultSet rs = getOutputParameters(parameterIndex); |
| 922 | |
| 923 | byte[] retValue = rs |
| 924 | .getBytes(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 925 | |
| 926 | this.outputParamWasNull = rs.wasNull(); |
| 927 | |
| 928 | return retValue; |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * @see java.sql.CallableStatement#getBytes(java.lang.String) |
| 933 | */ |
| 934 | public synchronized byte[] getBytes(String parameterName) |
| 935 | throws SQLException { |
| 936 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 937 | // from ?= |
| 938 | |
| 939 | byte[] retValue = rs.getBytes(fixParameterName(parameterName)); |
| 940 | |
| 941 | this.outputParamWasNull = rs.wasNull(); |
| 942 | |
| 943 | return retValue; |
| 944 | } |
| 945 | |
| 946 | /** |
| 947 | * @see java.sql.CallableStatement#getClob(int) |
| 948 | */ |
| 949 | public synchronized Clob getClob(int parameterIndex) throws SQLException { |
| 950 | ResultSet rs = getOutputParameters(parameterIndex); |
| 951 | |
| 952 | Clob retValue = rs |
| 953 | .getClob(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 954 | |
| 955 | this.outputParamWasNull = rs.wasNull(); |
| 956 | |
| 957 | return retValue; |
| 958 | } |
| 959 | |
| 960 | /** |
| 961 | * @see java.sql.CallableStatement#getClob(java.lang.String) |
| 962 | */ |
| 963 | public synchronized Clob getClob(String parameterName) throws SQLException { |
| 964 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 965 | // from ?= |
| 966 | |
| 967 | Clob retValue = rs.getClob(fixParameterName(parameterName)); |
| 968 | |
| 969 | this.outputParamWasNull = rs.wasNull(); |
| 970 | |
| 971 | return retValue; |
| 972 | } |
| 973 | |
| 974 | /** |
| 975 | * @see java.sql.CallableStatement#getDate(int) |
| 976 | */ |
| 977 | public synchronized Date getDate(int parameterIndex) throws SQLException { |
| 978 | ResultSet rs = getOutputParameters(parameterIndex); |
| 979 | |
| 980 | Date retValue = rs |
| 981 | .getDate(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 982 | |
| 983 | this.outputParamWasNull = rs.wasNull(); |
| 984 | |
| 985 | return retValue; |
| 986 | } |
| 987 | |
| 988 | /** |
| 989 | * @see java.sql.CallableStatement#getDate(int, java.util.Calendar) |
| 990 | */ |
| 991 | public synchronized Date getDate(int parameterIndex, Calendar cal) |
| 992 | throws SQLException { |
| 993 | ResultSet rs = getOutputParameters(parameterIndex); |
| 994 | |
| 995 | Date retValue = rs.getDate( |
| 996 | mapOutputParameterIndexToRsIndex(parameterIndex), cal); |
| 997 | |
| 998 | this.outputParamWasNull = rs.wasNull(); |
| 999 | |
| 1000 | return retValue; |
| 1001 | } |
| 1002 | |
| 1003 | /** |
| 1004 | * @see java.sql.CallableStatement#getDate(java.lang.String) |
| 1005 | */ |
| 1006 | public synchronized Date getDate(String parameterName) throws SQLException { |
| 1007 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1008 | // from ?= |
| 1009 | |
| 1010 | Date retValue = rs.getDate(fixParameterName(parameterName)); |
| 1011 | |
| 1012 | this.outputParamWasNull = rs.wasNull(); |
| 1013 | |
| 1014 | return retValue; |
| 1015 | } |
| 1016 | |
| 1017 | /** |
| 1018 | * @see java.sql.CallableStatement#getDate(java.lang.String, |
| 1019 | * java.util.Calendar) |
| 1020 | */ |
| 1021 | public synchronized Date getDate(String parameterName, Calendar cal) |
| 1022 | throws SQLException { |
| 1023 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1024 | // from ?= |
| 1025 | |
| 1026 | Date retValue = rs.getDate(fixParameterName(parameterName), cal); |
| 1027 | |
| 1028 | this.outputParamWasNull = rs.wasNull(); |
| 1029 | |
| 1030 | return retValue; |
| 1031 | } |
| 1032 | |
| 1033 | /** |
| 1034 | * @see java.sql.CallableStatement#getDouble(int) |
| 1035 | */ |
| 1036 | public synchronized double getDouble(int parameterIndex) |
| 1037 | throws SQLException { |
| 1038 | ResultSet rs = getOutputParameters(parameterIndex); |
| 1039 | |
| 1040 | double retValue = rs |
| 1041 | .getDouble(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 1042 | |
| 1043 | this.outputParamWasNull = rs.wasNull(); |
| 1044 | |
| 1045 | return retValue; |
| 1046 | } |
| 1047 | |
| 1048 | /** |
| 1049 | * @see java.sql.CallableStatement#getDouble(java.lang.String) |
| 1050 | */ |
| 1051 | public synchronized double getDouble(String parameterName) |
| 1052 | throws SQLException { |
| 1053 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1054 | // from ?= |
| 1055 | |
| 1056 | double retValue = rs.getDouble(fixParameterName(parameterName)); |
| 1057 | |
| 1058 | this.outputParamWasNull = rs.wasNull(); |
| 1059 | |
| 1060 | return retValue; |
| 1061 | } |
| 1062 | |
| 1063 | /** |
| 1064 | * @see java.sql.CallableStatement#getFloat(int) |
| 1065 | */ |
| 1066 | public synchronized float getFloat(int parameterIndex) throws SQLException { |
| 1067 | ResultSet rs = getOutputParameters(parameterIndex); |
| 1068 | |
| 1069 | float retValue = rs |
| 1070 | .getFloat(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 1071 | |
| 1072 | this.outputParamWasNull = rs.wasNull(); |
| 1073 | |
| 1074 | return retValue; |
| 1075 | } |
| 1076 | |
| 1077 | /** |
| 1078 | * @see java.sql.CallableStatement#getFloat(java.lang.String) |
| 1079 | */ |
| 1080 | public synchronized float getFloat(String parameterName) |
| 1081 | throws SQLException { |
| 1082 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1083 | // from ?= |
| 1084 | |
| 1085 | float retValue = rs.getFloat(fixParameterName(parameterName)); |
| 1086 | |
| 1087 | this.outputParamWasNull = rs.wasNull(); |
| 1088 | |
| 1089 | return retValue; |
| 1090 | } |
| 1091 | |
| 1092 | /** |
| 1093 | * @see java.sql.CallableStatement#getInt(int) |
| 1094 | */ |
| 1095 | public synchronized int getInt(int parameterIndex) throws SQLException { |
| 1096 | ResultSet rs = getOutputParameters(parameterIndex); |
| 1097 | |
| 1098 | int retValue = rs |
| 1099 | .getInt(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 1100 | |
| 1101 | this.outputParamWasNull = rs.wasNull(); |
| 1102 | |
| 1103 | return retValue; |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * @see java.sql.CallableStatement#getInt(java.lang.String) |
| 1108 | */ |
| 1109 | public synchronized int getInt(String parameterName) throws SQLException { |
| 1110 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1111 | // from ?= |
| 1112 | |
| 1113 | int retValue = rs.getInt(fixParameterName(parameterName)); |
| 1114 | |
| 1115 | this.outputParamWasNull = rs.wasNull(); |
| 1116 | |
| 1117 | return retValue; |
| 1118 | } |
| 1119 | |
| 1120 | /** |
| 1121 | * @see java.sql.CallableStatement#getLong(int) |
| 1122 | */ |
| 1123 | public synchronized long getLong(int parameterIndex) throws SQLException { |
| 1124 | ResultSet rs = getOutputParameters(parameterIndex); |
| 1125 | |
| 1126 | long retValue = rs |
| 1127 | .getLong(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 1128 | |
| 1129 | this.outputParamWasNull = rs.wasNull(); |
| 1130 | |
| 1131 | return retValue; |
| 1132 | } |
| 1133 | |
| 1134 | /** |
| 1135 | * @see java.sql.CallableStatement#getLong(java.lang.String) |
| 1136 | */ |
| 1137 | public synchronized long getLong(String parameterName) throws SQLException { |
| 1138 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1139 | // from ?= |
| 1140 | |
| 1141 | long retValue = rs.getLong(fixParameterName(parameterName)); |
| 1142 | |
| 1143 | this.outputParamWasNull = rs.wasNull(); |
| 1144 | |
| 1145 | return retValue; |
| 1146 | } |
| 1147 | |
| 1148 | private int getNamedParamIndex(String paramName, boolean forOut) |
| 1149 | throws SQLException { |
| 1150 | if ((paramName == null) || (paramName.length() == 0)) { |
| 1151 | throw new SQLException(Messages.getString("CallableStatement.2"), //$NON-NLS-1$ |
| 1152 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 1153 | } |
| 1154 | |
| 1155 | CallableStatementParam namedParamInfo = this.paramInfo |
| 1156 | .getParameter(paramName); |
| 1157 | |
| 1158 | if (this.paramInfo == null) { |
| 1159 | throw new SQLException( |
| 1160 | Messages.getString("CallableStatement.3") + paramName + Messages.getString("CallableStatement.4"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 1161 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 1162 | } |
| 1163 | |
| 1164 | if (forOut && !namedParamInfo.isOut) { |
| 1165 | throw new SQLException( |
| 1166 | Messages.getString("CallableStatement.5") + paramName //$NON-NLS-1$ |
| 1167 | + Messages.getString("CallableStatement.6"), //$NON-NLS-1$ |
| 1168 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 1169 | } |
| 1170 | |
| 1171 | |
| 1172 | if (this.placeholderToParameterIndexMap == null) { |
| 1173 | return namedParamInfo.index + 1; // JDBC indices are 1-based |
| 1174 | } |
| 1175 | |
| 1176 | for (int i = 0; i < this.placeholderToParameterIndexMap.length; i++) { |
| 1177 | if (this.placeholderToParameterIndexMap[i] == namedParamInfo.index) { |
| 1178 | return i + 1; |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | throw new SQLException("Can't find local placeholder mapping for parameter named \"" + |
| 1183 | paramName + "\".", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 1184 | } |
| 1185 | |
| 1186 | /** |
| 1187 | * @see java.sql.CallableStatement#getObject(int) |
| 1188 | */ |
| 1189 | public synchronized Object getObject(int parameterIndex) |
| 1190 | throws SQLException { |
| 1191 | CallableStatementParam paramDescriptor = checkIsOutputParam(parameterIndex); |
| 1192 | |
| 1193 | ResultSet rs = getOutputParameters(parameterIndex); |
| 1194 | |
| 1195 | Object retVal = rs.getObjectStoredProc( |
| 1196 | mapOutputParameterIndexToRsIndex(parameterIndex), |
| 1197 | paramDescriptor.desiredJdbcType); |
| 1198 | |
| 1199 | this.outputParamWasNull = rs.wasNull(); |
| 1200 | |
| 1201 | return retVal; |
| 1202 | } |
| 1203 | |
| 1204 | /** |
| 1205 | * @see java.sql.CallableStatement#getObject(int, java.util.Map) |
| 1206 | */ |
| 1207 | public synchronized Object getObject(int parameterIndex, Map map) |
| 1208 | throws SQLException { |
| 1209 | ResultSet rs = getOutputParameters(parameterIndex); |
| 1210 | |
| 1211 | Object retVal = rs.getObject( |
| 1212 | mapOutputParameterIndexToRsIndex(parameterIndex), map); |
| 1213 | |
| 1214 | this.outputParamWasNull = rs.wasNull(); |
| 1215 | |
| 1216 | return retVal; |
| 1217 | } |
| 1218 | |
| 1219 | /** |
| 1220 | * @see java.sql.CallableStatement#getObject(java.lang.String) |
| 1221 | */ |
| 1222 | public synchronized Object getObject(String parameterName) |
| 1223 | throws SQLException { |
| 1224 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1225 | // from ?= |
| 1226 | |
| 1227 | Object retValue = rs.getObject(fixParameterName(parameterName)); |
| 1228 | |
| 1229 | this.outputParamWasNull = rs.wasNull(); |
| 1230 | |
| 1231 | return retValue; |
| 1232 | } |
| 1233 | |
| 1234 | /** |
| 1235 | * @see java.sql.CallableStatement#getObject(java.lang.String, |
| 1236 | * java.util.Map) |
| 1237 | */ |
| 1238 | public synchronized Object getObject(String parameterName, Map map) |
| 1239 | throws SQLException { |
| 1240 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1241 | // from ?= |
| 1242 | |
| 1243 | Object retValue = rs.getObject(fixParameterName(parameterName), map); |
| 1244 | |
| 1245 | this.outputParamWasNull = rs.wasNull(); |
| 1246 | |
| 1247 | return retValue; |
| 1248 | } |
| 1249 | |
| 1250 | public synchronized ParameterMetaData getParameterMetaData() |
| 1251 | throws SQLException { |
| 1252 | return (CallableStatementParamInfoJDBC3) this.paramInfo; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * Returns the ResultSet that holds the output parameters, or throws an |
| 1257 | * appropriate exception if none exist, or they weren't returned. |
| 1258 | * |
| 1259 | * @return the ResultSet that holds the output parameters |
| 1260 | * |
| 1261 | * @throws SQLException |
| 1262 | * if no output parameters were defined, or if no output |
| 1263 | * parameters were returned. |
| 1264 | */ |
| 1265 | private ResultSet getOutputParameters(int paramIndex) throws SQLException { |
| 1266 | this.outputParamWasNull = false; |
| 1267 | |
| 1268 | if (paramIndex == 1 && this.callingStoredFunction |
| 1269 | && this.returnValueParam != null) { |
| 1270 | return this.functionReturnValueResults; |
| 1271 | } |
| 1272 | |
| 1273 | if (this.outputParameterResults == null) { |
| 1274 | if (this.paramInfo.numberOfParameters() == 0) { |
| 1275 | throw new SQLException(Messages |
| 1276 | .getString("CallableStatement.7"), //$NON-NLS-1$ |
| 1277 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 1278 | } |
| 1279 | throw new SQLException(Messages.getString("CallableStatement.8"), //$NON-NLS-1$ |
| 1280 | SQLError.SQL_STATE_GENERAL_ERROR); |
| 1281 | } |
| 1282 | |
| 1283 | return this.outputParameterResults; |
| 1284 | |
| 1285 | } |
| 1286 | |
| 1287 | /** |
| 1288 | * @see java.sql.CallableStatement#getRef(int) |
| 1289 | */ |
| 1290 | public synchronized Ref getRef(int parameterIndex) throws SQLException { |
| 1291 | ResultSet rs = getOutputParameters(parameterIndex); |
| 1292 | |
| 1293 | Ref retValue = rs |
| 1294 | .getRef(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 1295 | |
| 1296 | this.outputParamWasNull = rs.wasNull(); |
| 1297 | |
| 1298 | return retValue; |
| 1299 | } |
| 1300 | |
| 1301 | /** |
| 1302 | * @see java.sql.CallableStatement#getRef(java.lang.String) |
| 1303 | */ |
| 1304 | public synchronized Ref getRef(String parameterName) throws SQLException { |
| 1305 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1306 | // from ?= |
| 1307 | |
| 1308 | Ref retValue = rs.getRef(fixParameterName(parameterName)); |
| 1309 | |
| 1310 | this.outputParamWasNull = rs.wasNull(); |
| 1311 | |
| 1312 | return retValue; |
| 1313 | } |
| 1314 | |
| 1315 | /** |
| 1316 | * @see java.sql.CallableStatement#getShort(int) |
| 1317 | */ |
| 1318 | public synchronized short getShort(int parameterIndex) throws SQLException { |
| 1319 | ResultSet rs = getOutputParameters(parameterIndex); |
| 1320 | |
| 1321 | short retValue = rs |
| 1322 | .getShort(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 1323 | |
| 1324 | this.outputParamWasNull = rs.wasNull(); |
| 1325 | |
| 1326 | return retValue; |
| 1327 | } |
| 1328 | |
| 1329 | /** |
| 1330 | * @see java.sql.CallableStatement#getShort(java.lang.String) |
| 1331 | */ |
| 1332 | public synchronized short getShort(String parameterName) |
| 1333 | throws SQLException { |
| 1334 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1335 | // from ?= |
| 1336 | |
| 1337 | short retValue = rs.getShort(fixParameterName(parameterName)); |
| 1338 | |
| 1339 | this.outputParamWasNull = rs.wasNull(); |
| 1340 | |
| 1341 | return retValue; |
| 1342 | } |
| 1343 | |
| 1344 | /** |
| 1345 | * @see java.sql.CallableStatement#getString(int) |
| 1346 | */ |
| 1347 | public synchronized String getString(int parameterIndex) |
| 1348 | throws SQLException { |
| 1349 | ResultSet rs = getOutputParameters(parameterIndex); |
| 1350 | |
| 1351 | String retValue = rs |
| 1352 | .getString(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 1353 | |
| 1354 | this.outputParamWasNull = rs.wasNull(); |
| 1355 | |
| 1356 | return retValue; |
| 1357 | } |
| 1358 | |
| 1359 | /** |
| 1360 | * @see java.sql.CallableStatement#getString(java.lang.String) |
| 1361 | */ |
| 1362 | public synchronized String getString(String parameterName) |
| 1363 | throws SQLException { |
| 1364 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1365 | // from ?= |
| 1366 | |
| 1367 | String retValue = rs.getString(fixParameterName(parameterName)); |
| 1368 | |
| 1369 | this.outputParamWasNull = rs.wasNull(); |
| 1370 | |
| 1371 | return retValue; |
| 1372 | } |
| 1373 | |
| 1374 | /** |
| 1375 | * @see java.sql.CallableStatement#getTime(int) |
| 1376 | */ |
| 1377 | public synchronized Time getTime(int parameterIndex) throws SQLException { |
| 1378 | ResultSet rs = getOutputParameters(parameterIndex); |
| 1379 | |
| 1380 | Time retValue = rs |
| 1381 | .getTime(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 1382 | |
| 1383 | this.outputParamWasNull = rs.wasNull(); |
| 1384 | |
| 1385 | return retValue; |
| 1386 | } |
| 1387 | |
| 1388 | /** |
| 1389 | * @see java.sql.CallableStatement#getTime(int, java.util.Calendar) |
| 1390 | */ |
| 1391 | public synchronized Time getTime(int parameterIndex, Calendar cal) |
| 1392 | throws SQLException { |
| 1393 | ResultSet rs = getOutputParameters(parameterIndex); |
| 1394 | |
| 1395 | Time retValue = rs.getTime( |
| 1396 | mapOutputParameterIndexToRsIndex(parameterIndex), cal); |
| 1397 | |
| 1398 | this.outputParamWasNull = rs.wasNull(); |
| 1399 | |
| 1400 | return retValue; |
| 1401 | } |
| 1402 | |
| 1403 | /** |
| 1404 | * @see java.sql.CallableStatement#getTime(java.lang.String) |
| 1405 | */ |
| 1406 | public synchronized Time getTime(String parameterName) throws SQLException { |
| 1407 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1408 | // from ?= |
| 1409 | |
| 1410 | Time retValue = rs.getTime(fixParameterName(parameterName)); |
| 1411 | |
| 1412 | this.outputParamWasNull = rs.wasNull(); |
| 1413 | |
| 1414 | return retValue; |
| 1415 | } |
| 1416 | |
| 1417 | /** |
| 1418 | * @see java.sql.CallableStatement#getTime(java.lang.String, |
| 1419 | * java.util.Calendar) |
| 1420 | */ |
| 1421 | public synchronized Time getTime(String parameterName, Calendar cal) |
| 1422 | throws SQLException { |
| 1423 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1424 | // from ?= |
| 1425 | |
| 1426 | Time retValue = rs.getTime(fixParameterName(parameterName), cal); |
| 1427 | |
| 1428 | this.outputParamWasNull = rs.wasNull(); |
| 1429 | |
| 1430 | return retValue; |
| 1431 | } |
| 1432 | |
| 1433 | /** |
| 1434 | * @see java.sql.CallableStatement#getTimestamp(int) |
| 1435 | */ |
| 1436 | public synchronized Timestamp getTimestamp(int parameterIndex) |
| 1437 | throws SQLException { |
| 1438 | ResultSet rs = getOutputParameters(parameterIndex); |
| 1439 | |
| 1440 | Timestamp retValue = rs |
| 1441 | .getTimestamp(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 1442 | |
| 1443 | this.outputParamWasNull = rs.wasNull(); |
| 1444 | |
| 1445 | return retValue; |
| 1446 | } |
| 1447 | |
| 1448 | /** |
| 1449 | * @see java.sql.CallableStatement#getTimestamp(int, java.util.Calendar) |
| 1450 | */ |
| 1451 | public synchronized Timestamp getTimestamp(int parameterIndex, Calendar cal) |
| 1452 | throws SQLException { |
| 1453 | ResultSet rs = getOutputParameters(parameterIndex); |
| 1454 | |
| 1455 | Timestamp retValue = rs.getTimestamp( |
| 1456 | mapOutputParameterIndexToRsIndex(parameterIndex), cal); |
| 1457 | |
| 1458 | this.outputParamWasNull = rs.wasNull(); |
| 1459 | |
| 1460 | return retValue; |
| 1461 | } |
| 1462 | |
| 1463 | /** |
| 1464 | * @see java.sql.CallableStatement#getTimestamp(java.lang.String) |
| 1465 | */ |
| 1466 | public synchronized Timestamp getTimestamp(String parameterName) |
| 1467 | throws SQLException { |
| 1468 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1469 | // from ?= |
| 1470 | |
| 1471 | Timestamp retValue = rs.getTimestamp(fixParameterName(parameterName)); |
| 1472 | |
| 1473 | this.outputParamWasNull = rs.wasNull(); |
| 1474 | |
| 1475 | return retValue; |
| 1476 | } |
| 1477 | |
| 1478 | /** |
| 1479 | * @see java.sql.CallableStatement#getTimestamp(java.lang.String, |
| 1480 | * java.util.Calendar) |
| 1481 | */ |
| 1482 | public synchronized Timestamp getTimestamp(String parameterName, |
| 1483 | Calendar cal) throws SQLException { |
| 1484 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1485 | // from ?= |
| 1486 | |
| 1487 | Timestamp retValue = rs.getTimestamp(fixParameterName(parameterName), |
| 1488 | cal); |
| 1489 | |
| 1490 | this.outputParamWasNull = rs.wasNull(); |
| 1491 | |
| 1492 | return retValue; |
| 1493 | } |
| 1494 | |
| 1495 | /** |
| 1496 | * @see java.sql.CallableStatement#getURL(int) |
| 1497 | */ |
| 1498 | public synchronized URL getURL(int parameterIndex) throws SQLException { |
| 1499 | ResultSet rs = getOutputParameters(parameterIndex); |
| 1500 | |
| 1501 | URL retValue = rs |
| 1502 | .getURL(mapOutputParameterIndexToRsIndex(parameterIndex)); |
| 1503 | |
| 1504 | this.outputParamWasNull = rs.wasNull(); |
| 1505 | |
| 1506 | return retValue; |
| 1507 | } |
| 1508 | |
| 1509 | /** |
| 1510 | * @see java.sql.CallableStatement#getURL(java.lang.String) |
| 1511 | */ |
| 1512 | public synchronized URL getURL(String parameterName) throws SQLException { |
| 1513 | ResultSet rs = getOutputParameters(0); // definitely not going to be |
| 1514 | // from ?= |
| 1515 | |
| 1516 | URL retValue = rs.getURL(fixParameterName(parameterName)); |
| 1517 | |
| 1518 | this.outputParamWasNull = rs.wasNull(); |
| 1519 | |
| 1520 | return retValue; |
| 1521 | } |
| 1522 | |
| 1523 | private int mapOutputParameterIndexToRsIndex(int paramIndex) |
| 1524 | throws SQLException { |
| 1525 | |
| 1526 | if (this.returnValueParam != null && paramIndex == 1) { |
| 1527 | return 1; |
| 1528 | } |
| 1529 | |
| 1530 | checkParameterIndexBounds(paramIndex); |
| 1531 | |
| 1532 | int localParamIndex = paramIndex - 1; |
| 1533 | |
| 1534 | if (this.placeholderToParameterIndexMap != null) { |
| 1535 | localParamIndex = this.placeholderToParameterIndexMap[localParamIndex]; |
| 1536 | } |
| 1537 | |
| 1538 | int rsIndex = this.parameterIndexToRsIndex[localParamIndex]; |
| 1539 | |
| 1540 | if (rsIndex == NOT_OUTPUT_PARAMETER_INDICATOR) { |
| 1541 | throw new SQLException( |
| 1542 | Messages.getString("CallableStatement.21") + paramIndex //$NON-NLS-1$ |
| 1543 | + Messages.getString("CallableStatement.22"), //$NON-NLS-1$ |
| 1544 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 1545 | } |
| 1546 | |
| 1547 | return rsIndex + 1; |
| 1548 | } |
| 1549 | |
| 1550 | /** |
| 1551 | * @see java.sql.CallableStatement#registerOutParameter(int, int) |
| 1552 | */ |
| 1553 | public void registerOutParameter(int parameterIndex, int sqlType) |
| 1554 | throws SQLException { |
| 1555 | CallableStatementParam paramDescriptor = checkIsOutputParam(parameterIndex); |
| 1556 | paramDescriptor.desiredJdbcType = sqlType; |
| 1557 | } |
| 1558 | |
| 1559 | /** |
| 1560 | * @see java.sql.CallableStatement#registerOutParameter(int, int, int) |
| 1561 | */ |
| 1562 | public void registerOutParameter(int parameterIndex, int sqlType, int scale) |
| 1563 | throws SQLException { |
| 1564 | registerOutParameter(parameterIndex, sqlType); |
| 1565 | } |
| 1566 | |
| 1567 | /** |
| 1568 | * @see java.sql.CallableStatement#registerOutParameter(int, int, |
| 1569 | * java.lang.String) |
| 1570 | */ |
| 1571 | public void registerOutParameter(int parameterIndex, int sqlType, |
| 1572 | String typeName) throws SQLException { |
| 1573 | checkIsOutputParam(parameterIndex); |
| 1574 | } |
| 1575 | |
| 1576 | /** |
| 1577 | * @see java.sql.CallableStatement#registerOutParameter(java.lang.String, |
| 1578 | * int) |
| 1579 | */ |
| 1580 | public synchronized void registerOutParameter(String parameterName, |
| 1581 | int sqlType) throws SQLException { |
| 1582 | registerOutParameter(getNamedParamIndex(parameterName, true), sqlType); |
| 1583 | } |
| 1584 | |
| 1585 | /** |
| 1586 | * @see java.sql.CallableStatement#registerOutParameter(java.lang.String, |
| 1587 | * int, int) |
| 1588 | */ |
| 1589 | public void registerOutParameter(String parameterName, int sqlType, |
| 1590 | int scale) throws SQLException { |
| 1591 | registerOutParameter(getNamedParamIndex(parameterName, true), sqlType); |
| 1592 | } |
| 1593 | |
| 1594 | /** |
| 1595 | * @see java.sql.CallableStatement#registerOutParameter(java.lang.String, |
| 1596 | * int, java.lang.String) |
| 1597 | */ |
| 1598 | public void registerOutParameter(String parameterName, int sqlType, |
| 1599 | String typeName) throws SQLException { |
| 1600 | registerOutParameter(getNamedParamIndex(parameterName, true), sqlType, |
| 1601 | typeName); |
| 1602 | } |
| 1603 | |
| 1604 | /** |
| 1605 | * Issues a second query to retrieve all output parameters. |
| 1606 | * |
| 1607 | * @throws SQLException |
| 1608 | * if an error occurs. |
| 1609 | */ |
| 1610 | private void retrieveOutParams() throws SQLException { |
| 1611 | int numParameters = this.paramInfo.numberOfParameters(); |
| 1612 | |
| 1613 | this.parameterIndexToRsIndex = new int[numParameters]; |
| 1614 | |
| 1615 | for (int i = 0; i < numParameters; i++) { |
| 1616 | this.parameterIndexToRsIndex[i] = NOT_OUTPUT_PARAMETER_INDICATOR; |
| 1617 | } |
| 1618 | |
| 1619 | int localParamIndex = 0; |
| 1620 | |
| 1621 | if (numParameters > 0) { |
| 1622 | StringBuffer outParameterQuery = new StringBuffer("SELECT "); //$NON-NLS-1$ |
| 1623 | |
| 1624 | boolean firstParam = true; |
| 1625 | boolean hadOutputParams = false; |
| 1626 | |
| 1627 | for (Iterator paramIter = this.paramInfo.iterator(); paramIter |
| 1628 | .hasNext();) { |
| 1629 | CallableStatementParam retrParamInfo = (CallableStatementParam) paramIter |
| 1630 | .next(); |
| 1631 | |
| 1632 | if (retrParamInfo.isOut) { |
| 1633 | hadOutputParams = true; |
| 1634 | |
| 1635 | this.parameterIndexToRsIndex[retrParamInfo.index] = localParamIndex++; |
| 1636 | |
| 1637 | String outParameterName = mangleParameterName(retrParamInfo.paramName); |
| 1638 | |
| 1639 | if (!firstParam) { |
| 1640 | outParameterQuery.append(","); //$NON-NLS-1$ |
| 1641 | } else { |
| 1642 | firstParam = false; |
| 1643 | } |
| 1644 | |
| 1645 | if (!outParameterName.startsWith("@")) { //$NON-NLS-1$ |
| 1646 | outParameterQuery.append('@'); |
| 1647 | } |
| 1648 | |
| 1649 | outParameterQuery.append(outParameterName); |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | if (hadOutputParams) { |
| 1654 | // We can't use 'ourself' to execute this query, or any |
| 1655 | // pending result sets would be overwritten |
| 1656 | java.sql.Statement outParameterStmt = null; |
| 1657 | java.sql.ResultSet outParamRs = null; |
| 1658 | |
| 1659 | try { |
| 1660 | outParameterStmt = this.connection.createStatement(); |
| 1661 | outParamRs = outParameterStmt |
| 1662 | .executeQuery(outParameterQuery.toString()); |
| 1663 | this.outputParameterResults = ((com.mysql.jdbc.ResultSet) outParamRs) |
| 1664 | .copy(); |
| 1665 | |
| 1666 | if (!this.outputParameterResults.next()) { |
| 1667 | this.outputParameterResults.close(); |
| 1668 | this.outputParameterResults = null; |
| 1669 | } |
| 1670 | } finally { |
| 1671 | if (outParameterStmt != null) { |
| 1672 | outParameterStmt.close(); |
| 1673 | } |
| 1674 | } |
| 1675 | } else { |
| 1676 | this.outputParameterResults = null; |
| 1677 | } |
| 1678 | } else { |
| 1679 | this.outputParameterResults = null; |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | /** |
| 1684 | * @see java.sql.CallableStatement#setAsciiStream(java.lang.String, |
| 1685 | * java.io.InputStream, int) |
| 1686 | */ |
| 1687 | public void setAsciiStream(String parameterName, InputStream x, int length) |
| 1688 | throws SQLException { |
| 1689 | setAsciiStream(getNamedParamIndex(parameterName, false), x, length); |
| 1690 | } |
| 1691 | |
| 1692 | /** |
| 1693 | * @see java.sql.CallableStatement#setBigDecimal(java.lang.String, |
| 1694 | * java.math.BigDecimal) |
| 1695 | */ |
| 1696 | public void setBigDecimal(String parameterName, BigDecimal x) |
| 1697 | throws SQLException { |
| 1698 | setBigDecimal(getNamedParamIndex(parameterName, false), x); |
| 1699 | } |
| 1700 | |
| 1701 | /** |
| 1702 | * @see java.sql.CallableStatement#setBinaryStream(java.lang.String, |
| 1703 | * java.io.InputStream, int) |
| 1704 | */ |
| 1705 | public void setBinaryStream(String parameterName, InputStream x, int length) |
| 1706 | throws SQLException { |
| 1707 | setBinaryStream(getNamedParamIndex(parameterName, false), x, length); |
| 1708 | } |
| 1709 | |
| 1710 | /** |
| 1711 | * @see java.sql.CallableStatement#setBoolean(java.lang.String, boolean) |
| 1712 | */ |
| 1713 | public void setBoolean(String parameterName, boolean x) throws SQLException { |
| 1714 | setBoolean(getNamedParamIndex(parameterName, false), x); |
| 1715 | } |
| 1716 | |
| 1717 | /** |
| 1718 | * @see java.sql.CallableStatement#setByte(java.lang.String, byte) |
| 1719 | */ |
| 1720 | public void setByte(String parameterName, byte x) throws SQLException { |
| 1721 | setByte(getNamedParamIndex(parameterName, false), x); |
| 1722 | } |
| 1723 | |
| 1724 | /** |
| 1725 | * @see java.sql.CallableStatement#setBytes(java.lang.String, byte[]) |
| 1726 | */ |
| 1727 | public void setBytes(String parameterName, byte[] x) throws SQLException { |
| 1728 | setBytes(getNamedParamIndex(parameterName, false), x); |
| 1729 | } |
| 1730 | |
| 1731 | /** |
| 1732 | * @see java.sql.CallableStatement#setCharacterStream(java.lang.String, |
| 1733 | * java.io.Reader, int) |
| 1734 | */ |
| 1735 | public void setCharacterStream(String parameterName, Reader reader, |
| 1736 | int length) throws SQLException { |
| 1737 | setCharacterStream(getNamedParamIndex(parameterName, false), reader, |
| 1738 | length); |
| 1739 | } |
| 1740 | |
| 1741 | /** |
| 1742 | * @see java.sql.CallableStatement#setDate(java.lang.String, java.sql.Date) |
| 1743 | */ |
| 1744 | public void setDate(String parameterName, Date x) throws SQLException { |
| 1745 | setDate(getNamedParamIndex(parameterName, false), x); |
| 1746 | } |
| 1747 | |
| 1748 | /** |
| 1749 | * @see java.sql.CallableStatement#setDate(java.lang.String, java.sql.Date, |
| 1750 | * java.util.Calendar) |
| 1751 | */ |
| 1752 | public void setDate(String parameterName, Date x, Calendar cal) |
| 1753 | throws SQLException { |
| 1754 | setDate(getNamedParamIndex(parameterName, false), x, cal); |
| 1755 | } |
| 1756 | |
| 1757 | /** |
| 1758 | * @see java.sql.CallableStatement#setDouble(java.lang.String, double) |
| 1759 | */ |
| 1760 | public void setDouble(String parameterName, double x) throws SQLException { |
| 1761 | setDouble(getNamedParamIndex(parameterName, false), x); |
| 1762 | } |
| 1763 | |
| 1764 | /** |
| 1765 | * @see java.sql.CallableStatement#setFloat(java.lang.String, float) |
| 1766 | */ |
| 1767 | public void setFloat(String parameterName, float x) throws SQLException { |
| 1768 | setFloat(getNamedParamIndex(parameterName, false), x); |
| 1769 | } |
| 1770 | |
| 1771 | /** |
| 1772 | * |
| 1773 | */ |
| 1774 | private void setInOutParamsOnServer() throws SQLException { |
| 1775 | if (this.paramInfo.numParameters > 0) { |
| 1776 | int parameterIndex = 0; |
| 1777 | |
| 1778 | for (Iterator paramIter = this.paramInfo.iterator(); paramIter |
| 1779 | .hasNext();) { |
| 1780 | |
| 1781 | CallableStatementParam inParamInfo = (CallableStatementParam) paramIter |
| 1782 | .next(); |
| 1783 | |
| 1784 | if (inParamInfo.isOut && inParamInfo.isIn) { |
| 1785 | String inOutParameterName = mangleParameterName(inParamInfo.paramName); |
| 1786 | StringBuffer queryBuf = new StringBuffer( |
| 1787 | 4 + inOutParameterName.length() + 1 + 1); |
| 1788 | queryBuf.append("SET "); //$NON-NLS-1$ |
| 1789 | queryBuf.append(inOutParameterName); |
| 1790 | queryBuf.append("=?"); //$NON-NLS-1$ |
| 1791 | |
| 1792 | PreparedStatement setPstmt = null; |
| 1793 | |
| 1794 | try { |
| 1795 | setPstmt = this.connection |
| 1796 | .clientPrepareStatement(queryBuf.toString()); |
| 1797 | |
| 1798 | byte[] parameterAsBytes = this |
| 1799 | .getBytesRepresentation(inParamInfo.index); |
| 1800 | |
| 1801 | if (parameterAsBytes != null) { |
| 1802 | if (parameterAsBytes.length > 8 |
| 1803 | && parameterAsBytes[0] == '_' |
| 1804 | && parameterAsBytes[1] == 'b' |
| 1805 | && parameterAsBytes[2] == 'i' |
| 1806 | && parameterAsBytes[3] == 'n' |
| 1807 | && parameterAsBytes[4] == 'a' |
| 1808 | && parameterAsBytes[5] == 'r' |
| 1809 | && parameterAsBytes[6] == 'y' |
| 1810 | && parameterAsBytes[7] == '\'') { |
| 1811 | setPstmt.setBytesNoEscapeNoQuotes(1, |
| 1812 | parameterAsBytes); |
| 1813 | } else { |
| 1814 | setPstmt.setBytes(1, parameterAsBytes); |
| 1815 | } |
| 1816 | } else { |
| 1817 | setPstmt.setNull(1, Types.NULL); |
| 1818 | } |
| 1819 | |
| 1820 | setPstmt.executeUpdate(); |
| 1821 | } finally { |
| 1822 | if (setPstmt != null) { |
| 1823 | setPstmt.close(); |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | // StringBuffer fullName = new StringBuffer("@"); |
| 1828 | // fullName.append(outParameterName); |
| 1829 | /* |
| 1830 | * this.setBytesNoEscapeNoQuotes(paramInfo.index + 1, |
| 1831 | * StringUtils.getBytes(outParameterName, |
| 1832 | * this.charConverter, this.charEncoding, |
| 1833 | * this.connection.getServerCharacterEncoding(), |
| 1834 | * this.connection.parserKnowsUnicode())); |
| 1835 | */ |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | parameterIndex++; |
| 1840 | } |
| 1841 | |
| 1842 | } |
| 1843 | |
| 1844 | /** |
| 1845 | * @see java.sql.CallableStatement#setInt(java.lang.String, int) |
| 1846 | */ |
| 1847 | public void setInt(String parameterName, int x) throws SQLException { |
| 1848 | setInt(getNamedParamIndex(parameterName, false), x); |
| 1849 | } |
| 1850 | |
| 1851 | /** |
| 1852 | * @see java.sql.CallableStatement#setLong(java.lang.String, long) |
| 1853 | */ |
| 1854 | public void setLong(String parameterName, long x) throws SQLException { |
| 1855 | setLong(getNamedParamIndex(parameterName, false), x); |
| 1856 | } |
| 1857 | |
| 1858 | /** |
| 1859 | * @see java.sql.CallableStatement#setNull(java.lang.String, int) |
| 1860 | */ |
| 1861 | public void setNull(String parameterName, int sqlType) throws SQLException { |
| 1862 | setNull(getNamedParamIndex(parameterName, false), sqlType); |
| 1863 | } |
| 1864 | |
| 1865 | /** |
| 1866 | * @see java.sql.CallableStatement#setNull(java.lang.String, int, |
| 1867 | * java.lang.String) |
| 1868 | */ |
| 1869 | public void setNull(String parameterName, int sqlType, String typeName) |
| 1870 | throws SQLException { |
| 1871 | setNull(getNamedParamIndex(parameterName, false), sqlType, typeName); |
| 1872 | } |
| 1873 | |
| 1874 | /** |
| 1875 | * @see java.sql.CallableStatement#setObject(java.lang.String, |
| 1876 | * java.lang.Object) |
| 1877 | */ |
| 1878 | public void setObject(String parameterName, Object x) throws SQLException { |
| 1879 | setObject(getNamedParamIndex(parameterName, false), x); |
| 1880 | } |
| 1881 | |
| 1882 | /** |
| 1883 | * @see java.sql.CallableStatement#setObject(java.lang.String, |
| 1884 | * java.lang.Object, int) |
| 1885 | */ |
| 1886 | public void setObject(String parameterName, Object x, int targetSqlType) |
| 1887 | throws SQLException { |
| 1888 | setObject(getNamedParamIndex(parameterName, false), x, targetSqlType); |
| 1889 | } |
| 1890 | |
| 1891 | /** |
| 1892 | * @see java.sql.CallableStatement#setObject(java.lang.String, |
| 1893 | * java.lang.Object, int, int) |
| 1894 | */ |
| 1895 | public void setObject(String parameterName, Object x, int targetSqlType, |
| 1896 | int scale) throws SQLException { |
| 1897 | } |
| 1898 | |
| 1899 | private void setOutParams() throws SQLException { |
| 1900 | if (this.paramInfo.numParameters > 0) { |
| 1901 | for (Iterator paramIter = this.paramInfo.iterator(); paramIter |
| 1902 | .hasNext();) { |
| 1903 | CallableStatementParam outParamInfo = (CallableStatementParam) paramIter |
| 1904 | .next(); |
| 1905 | |
| 1906 | if (outParamInfo.isOut) { |
| 1907 | String outParameterName = mangleParameterName(outParamInfo.paramName); |
| 1908 | |
| 1909 | int outParamIndex; |
| 1910 | |
| 1911 | if (this.placeholderToParameterIndexMap == null) { |
| 1912 | outParamIndex = outParamInfo.index + 1; |
| 1913 | } else { |
| 1914 | outParamIndex = this.placeholderToParameterIndexMap[outParamInfo.index - 1 /* JDBC is 1-based */]; |
| 1915 | } |
| 1916 | |
| 1917 | this.setBytesNoEscapeNoQuotes(outParamIndex, |
| 1918 | StringUtils.getBytes(outParameterName, |
| 1919 | this.charConverter, this.charEncoding, |
| 1920 | this.connection |
| 1921 | .getServerCharacterEncoding(), |
| 1922 | this.connection.parserKnowsUnicode())); |
| 1923 | } |
| 1924 | } |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | /** |
| 1929 | * @see java.sql.CallableStatement#setShort(java.lang.String, short) |
| 1930 | */ |
| 1931 | public void setShort(String parameterName, short x) throws SQLException { |
| 1932 | setShort(getNamedParamIndex(parameterName, false), x); |
| 1933 | } |
| 1934 | |
| 1935 | /** |
| 1936 | * @see java.sql.CallableStatement#setString(java.lang.String, |
| 1937 | * java.lang.String) |
| 1938 | */ |
| 1939 | public void setString(String parameterName, String x) throws SQLException { |
| 1940 | setString(getNamedParamIndex(parameterName, false), x); |
| 1941 | } |
| 1942 | |
| 1943 | /** |
| 1944 | * @see java.sql.CallableStatement#setTime(java.lang.String, java.sql.Time) |
| 1945 | */ |
| 1946 | public void setTime(String parameterName, Time x) throws SQLException { |
| 1947 | setTime(getNamedParamIndex(parameterName, false), x); |
| 1948 | } |
| 1949 | |
| 1950 | /** |
| 1951 | * @see java.sql.CallableStatement#setTime(java.lang.String, java.sql.Time, |
| 1952 | * java.util.Calendar) |
| 1953 | */ |
| 1954 | public void setTime(String parameterName, Time x, Calendar cal) |
| 1955 | throws SQLException { |
| 1956 | setTime(getNamedParamIndex(parameterName, false), x, cal); |
| 1957 | } |
| 1958 | |
| 1959 | /** |
| 1960 | * @see java.sql.CallableStatement#setTimestamp(java.lang.String, |
| 1961 | * java.sql.Timestamp) |
| 1962 | */ |
| 1963 | public void setTimestamp(String parameterName, Timestamp x) |
| 1964 | throws SQLException { |
| 1965 | setTimestamp(getNamedParamIndex(parameterName, false), x); |
| 1966 | } |
| 1967 | |
| 1968 | /** |
| 1969 | * @see java.sql.CallableStatement#setTimestamp(java.lang.String, |
| 1970 | * java.sql.Timestamp, java.util.Calendar) |
| 1971 | */ |
| 1972 | public void setTimestamp(String parameterName, Timestamp x, Calendar cal) |
| 1973 | throws SQLException { |
| 1974 | setTimestamp(getNamedParamIndex(parameterName, false), x, cal); |
| 1975 | } |
| 1976 | |
| 1977 | /** |
| 1978 | * @see java.sql.CallableStatement#setURL(java.lang.String, java.net.URL) |
| 1979 | */ |
| 1980 | public void setURL(String parameterName, URL val) throws SQLException { |
| 1981 | setURL(getNamedParamIndex(parameterName, false), val); |
| 1982 | } |
| 1983 | |
| 1984 | /** |
| 1985 | * @see java.sql.CallableStatement#wasNull() |
| 1986 | */ |
| 1987 | public synchronized boolean wasNull() throws SQLException { |
| 1988 | return this.outputParamWasNull; |
| 1989 | } |
| 1990 | |
| 1991 | public int[] executeBatch() throws SQLException { |
| 1992 | if (this.hasOutputParams) { |
| 1993 | throw new SQLException("Can't call executeBatch() on CallableStatement with OUTPUT parameters", |
| 1994 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 1995 | } |
| 1996 | |
| 1997 | return super.executeBatch(); |
| 1998 | } |
| 1999 | } |