📄 callablestatement.java
字号:
return this.returnValueParam; } // Move to position in output result set paramIndex--; } checkParameterIndexBounds(paramIndex); int localParamIndex = paramIndex - 1; if (this.placeholderToParameterIndexMap != null) { localParamIndex = this.placeholderToParameterIndexMap[localParamIndex]; } CallableStatementParam paramDescriptor = this.paramInfo .getParameter(localParamIndex); if (!paramDescriptor.isOut) { throw SQLError.createSQLException( Messages.getString("CallableStatement.9") + paramIndex //$NON-NLS-1$ + Messages.getString("CallableStatement.10"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } this.hasOutputParams = true; return paramDescriptor; } /** * DOCUMENT ME! * * @param paramIndex * * @throws SQLException */ private void checkParameterIndexBounds(int paramIndex) throws SQLException { this.paramInfo.checkBounds(paramIndex); } /** * Checks whether or not this statement is supposed to be providing * streamable result sets...If output parameters are registered, the driver * can not stream the results. * * @throws SQLException * DOCUMENT ME! */ private void checkStreamability() throws SQLException { if (this.hasOutputParams && createStreamingResultSet()) { throw SQLError.createSQLException(Messages.getString("CallableStatement.14"), //$NON-NLS-1$ SQLError.SQL_STATE_DRIVER_NOT_CAPABLE); } } public synchronized void clearParameters() throws SQLException { super.clearParameters(); try { if (this.outputParameterResults != null) { this.outputParameterResults.close(); } } finally { this.outputParameterResults = null; } } /** * Used to fake up some metadata when we don't have access to * SHOW CREATE PROCEDURE or mysql.proc. * * @throws SQLException if we can't build the metadata. */ private void fakeParameterTypes() throws SQLException { Field[] fields = new Field[13]; fields[0] = new Field("", "PROCEDURE_CAT", Types.CHAR, 0); fields[1] = new Field("", "PROCEDURE_SCHEM", Types.CHAR, 0); fields[2] = new Field("", "PROCEDURE_NAME", Types.CHAR, 0); fields[3] = new Field("", "COLUMN_NAME", Types.CHAR, 0); fields[4] = new Field("", "COLUMN_TYPE", Types.CHAR, 0); fields[5] = new Field("", "DATA_TYPE", Types.SMALLINT, 0); fields[6] = new Field("", "TYPE_NAME", Types.CHAR, 0); fields[7] = new Field("", "PRECISION", Types.INTEGER, 0); fields[8] = new Field("", "LENGTH", Types.INTEGER, 0); fields[9] = new Field("", "SCALE", Types.SMALLINT, 0); fields[10] = new Field("", "RADIX", Types.SMALLINT, 0); fields[11] = new Field("", "NULLABLE", Types.SMALLINT, 0); fields[12] = new Field("", "REMARKS", Types.CHAR, 0); String procName = extractProcedureName(); byte[] procNameAsBytes = null; try { procNameAsBytes = procName.getBytes("UTF-8"); } catch (UnsupportedEncodingException ueEx) { procNameAsBytes = StringUtils.s2b(procName, this.connection); } ArrayList resultRows = new ArrayList(); for (int i = 0; i < this.parameterCount; i++) { byte[][] row = new byte[13][]; row[0] = null; // PROCEDURE_CAT row[1] = null; // PROCEDURE_SCHEM row[2] = procNameAsBytes; // PROCEDURE/NAME row[3] = StringUtils.s2b(String.valueOf(i), this.connection); // COLUMN_NAME row[4] = StringUtils.s2b(String .valueOf(DatabaseMetaData.procedureColumnInOut), this.connection); row[5] = StringUtils.s2b(String.valueOf(Types.VARCHAR), this.connection); // DATA_TYPE row[6] = StringUtils.s2b("VARCHAR", this.connection); // TYPE_NAME row[7] = StringUtils.s2b(Integer.toString(65535), this.connection); // PRECISION row[8] = StringUtils.s2b(Integer.toString(65535), this.connection); // LENGTH row[9] = StringUtils.s2b(Integer.toString(0), this.connection); // SCALE row[10] = StringUtils.s2b(Integer.toString(10), this.connection); // RADIX row[11] = StringUtils.s2b(Integer .toString(DatabaseMetaData.procedureNullableUnknown), this.connection); // nullable row[12] = null; resultRows.add(row); } java.sql.ResultSet paramTypesRs = DatabaseMetaData.buildResultSet( fields, resultRows, this.connection); convertGetProcedureColumnsToInternalDescriptors(paramTypesRs); } private void determineParameterTypes() throws SQLException { if (this.connection.getNoAccessToProcedureBodies()) { fakeParameterTypes(); return; } java.sql.ResultSet paramTypesRs = null; try { String procName = extractProcedureName(); java.sql.DatabaseMetaData dbmd = this.connection.getMetaData(); boolean useCatalog = false; if (procName.indexOf(".") == -1) { useCatalog = true; } paramTypesRs = dbmd.getProcedureColumns(this.connection .versionMeetsMinimum(5, 0, 2) && useCatalog ? this.currentCatalog : null, null, procName, "%"); //$NON-NLS-1$ convertGetProcedureColumnsToInternalDescriptors(paramTypesRs); } finally { SQLException sqlExRethrow = null; if (paramTypesRs != null) { try { paramTypesRs.close(); } catch (SQLException sqlEx) { sqlExRethrow = sqlEx; } paramTypesRs = null; } if (sqlExRethrow != null) { throw sqlExRethrow; } } } private void convertGetProcedureColumnsToInternalDescriptors(java.sql.ResultSet paramTypesRs) throws SQLException { if (!this.connection.isRunningOnJDK13()) { this.paramInfo = new CallableStatementParamInfoJDBC3( paramTypesRs); } else { this.paramInfo = new CallableStatementParamInfo(paramTypesRs); } } /* * (non-Javadoc) * * @see java.sql.PreparedStatement#execute() */ public boolean execute() throws SQLException { boolean returnVal = false; checkClosed(); checkStreamability(); synchronized (this.connection.getMutex()) { setInOutParamsOnServer(); setOutParams(); returnVal = super.execute(); if (this.callingStoredFunction) { this.functionReturnValueResults = this.results; this.functionReturnValueResults.next(); this.results = null; } retrieveOutParams(); } if (!this.callingStoredFunction) { return returnVal; } // Functions can't return results return false; } /* * (non-Javadoc) * * @see java.sql.PreparedStatement#executeQuery() */ public java.sql.ResultSet executeQuery() throws SQLException { checkClosed(); checkStreamability(); java.sql.ResultSet execResults = null; synchronized (this.connection.getMutex()) { setInOutParamsOnServer(); setOutParams(); execResults = super.executeQuery(); retrieveOutParams(); } return execResults; } /* * (non-Javadoc) * * @see java.sql.PreparedStatement#executeUpdate() */ public int executeUpdate() throws SQLException { int returnVal = -1; checkClosed(); checkStreamability(); if (this.callingStoredFunction) { execute(); return -1; } synchronized (this.connection.getMutex()) { setInOutParamsOnServer(); setOutParams(); returnVal = super.executeUpdate(); retrieveOutParams(); } return returnVal; } private String extractProcedureName() throws SQLException { String sanitizedSql = StringUtils.stripComments(this.originalSql, "`\"'", "`\"'", true, false, true, true); // TODO: Do this with less memory allocation int endCallIndex = StringUtils.indexOfIgnoreCase(sanitizedSql, "CALL "); //$NON-NLS-1$ int offset = 5; if (endCallIndex == -1) { endCallIndex = StringUtils.indexOfIgnoreCase(sanitizedSql, "SELECT "); offset = 7; } if (endCallIndex != -1) { StringBuffer nameBuf = new StringBuffer(); String trimmedStatement = sanitizedSql.substring( endCallIndex + offset).trim(); int statementLength = trimmedStatement.length(); for (int i = 0; i < statementLength; i++) { char c = trimmedStatement.charAt(i); if (Character.isWhitespace(c) || (c == '(') || (c == '?')) { break; } nameBuf.append(c); } return nameBuf.toString(); } throw SQLError.createSQLException(Messages.getString("CallableStatement.1"), //$NON-NLS-1$ SQLError.SQL_STATE_GENERAL_ERROR); } /** * Adds 'at' symbol to beginning of parameter names if needed. * * @param paramNameIn * the parameter name to 'fix' * * @return the parameter name with an 'a' prepended, if needed * * @throws SQLException * if the parameter name is null or empty. */ private String fixParameterName(String paramNameIn) throws SQLException { if ((paramNameIn == null) || (paramNameIn.length() == 0)) { throw SQLError.createSQLException( ((Messages.getString("CallableStatement.0") + paramNameIn) == null) //$NON-NLS-1$ ? Messages.getString("CallableStatement.15") : Messages.getString("CallableStatement.16"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ //$NON-NLS-2$ } if (this.connection.getNoAccessToProcedureBodies()) { throw SQLError.createSQLException("No access to parameters by name when connection has been configured not to access procedure bodies", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } return mangleParameterName(paramNameIn); /* * if (paramNameIn.startsWith("@")) { return paramNameIn; } else { * StringBuffer paramNameBuf = new StringBuffer("@"); * paramNameBuf.append(paramNameIn); * * return paramNameBuf.toString(); } */ } /** * @see java.sql.CallableStatement#getArray(int) */ public synchronized Array getArray(int i) throws SQLException { ResultSet rs = getOutputParameters(i); Array retValue = rs.getArray(mapOutputParameterIndexToRsIndex(i)); this.outputParamWasNull = rs.wasNull(); return retValue; } /** * @see java.sql.CallableStatement#getArray(java.lang.String) */ public synchronized Array getArray(String parameterName) throws SQLException { ResultSet rs = getOutputParameters(0); // definitely not going to be // from ?= Array retValue = rs.getArray(fixParameterName(parameterName)); this.outputParamWasNull = rs.wasNull(); return retValue; } /** * @see java.sql.CallableStatement#getBigDecimal(int) */ public synchronized BigDecimal getBigDecimal(int parameterIndex) throws SQLException { ResultSet rs = getOutputParameters(parameterIndex); BigDecimal retValue = rs .getBigDecimal(mapOutputParameterIndexToRsIndex(parameterIndex)); this.outputParamWasNull = rs.wasNull(); return retValue; } /** * DOCUMENT ME! * * @param parameterIndex * DOCUMENT ME! * @param scale * DOCUMENT ME! * * @return DOCUMENT ME! * * @throws SQLException * DOCUMENT ME! * * @see java.sql.CallableStatement#getBigDecimal(int, int) * @deprecated */ public synchronized BigDecimal getBigDecimal(int parameterIndex, int scale) throws SQLException { ResultSet rs = getOutputParameters(parameterIndex); BigDecimal retValue = rs.getBigDecimal( mapOutputParameterIndexToRsIndex(parameterIndex), scale); this.outputParamWasNull = rs.wasNull(); return retValue; } /** * @see java.sql.CallableStatement#getBigDecimal(java.lang.String) */ public synchronized BigDecimal getBigDecimal(String parameterName) throws SQLException { ResultSet rs = getOutputParameters(0); // definitely not going to be // from ?= BigDecimal retValue = rs.getBigDecimal(fixParameterName(parameterName)); this.outputParamWasNull = rs.wasNull(); return retValue; } /** * @see java.sql.CallableStatement#getBlob(int) */ public synchronized Blob getBlob(int parameterIndex) throws SQLException { ResultSet rs = getOutputParameters(parameterIndex); Blob retValue = rs .getBlob(mapOutputParameterIndexToRsIndex(parameterIndex)); this.outputParamWasNull = rs.wasNull(); return retValue; } /** * @see java.sql.CallableStatement#getBlob(java.lang.String) */ public synchronized Blob getBlob(String parameterName) throws SQLException { ResultSet rs = getOutputParameters(0); // definitely not going to be // from ?= Blob retValue = rs.getBlob(fixParameterName(parameterName)); this.outputParamWasNull = rs.wasNull(); return retValue; } /** * @see java.sql.CallableStatement#getBoolean(int) */ public synchronized boolean getBoolean(int parameterIndex) throws SQLException { ResultSet rs = getOutputParameters(parameterIndex); boolean retValue = rs .getBoolean(mapOutputParameterIndexToRsIndex(parameterIndex)); this.outputParamWasNull = rs.wasNull(); return retValue; } /** * @see java.sql.CallableStatement#getBoolean(java.lang.String) */ public synchronized boolean getBoolean(String parameterName) throws SQLException { ResultSet rs = getOutputParameters(0); // definitely not going to be // from ?= boolean retValue = rs.getBoolean(fixParameterName(parameterName)); this.outputParamWasNull = rs.wasNull(); return retValue; } /** * @see java.sql.CallableStatement#getByte(int) */ public synchronized byte getByte(int parameterIndex) throws SQLException { ResultSet rs = getOutputParameters(parameterIndex); byte retValue = rs .getByte(mapOutputParameterIndexToRsIndex(parameterIndex)); this.outputParamWasNull = rs.wasNull(); return retValue; } /** * @see java.sql.CallableStatement#getByte(java.lang.String) */ public synchronized byte getByte(String parameterName) throws SQLException { ResultSet rs = getOutputParameters(0); // definitely not going to be // from ?= byte retValue = rs.getByte(fixParameterName(parameterName)); this.outputParamWasNull = rs.wasNull(); return retValue; } /** * @see java.sql.CallableStatement#getBytes(int) */ public synchronized byte[] getBytes(int parameterIndex) throws SQLException { ResultSet rs = getOutputParameters(parameterIndex); byte[] retValue = rs .getBytes(mapOutputParameterIndexToRsIndex(parameterIndex)); this.outputParamWasNull = rs.wasNull(); return retValue; } /** * @see java.sql.CallableStatement#getBytes(java.lang.String) */ public synchronized byte[] getBytes(String parameterName)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -