📄 callablestatement.java
字号:
} /** * @see java.sql.CallableStatement#registerOutParameter(java.lang.String, * int) */ public synchronized void registerOutParameter(String parameterName, int sqlType) throws SQLException { registerOutParameter(getNamedParamIndex(parameterName, true), sqlType); } /** * @see java.sql.CallableStatement#wasNull() */ public boolean wasNull() throws SQLException { return getOutputParameters().wasNull(); } private int getNamedParamIndex(String paramName, boolean forOut) throws SQLException { if ((paramName == null) || (paramName.length() == 0)) { throw new SQLException(Messages.getString("CallableStatement.2"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } CallableStatementParam namedParamInfo = this.paramInfo.getParameter(paramName); if (this.paramInfo == null) { throw new SQLException(Messages.getString("CallableStatement.3") + paramName + Messages.getString("CallableStatement.4"), //$NON-NLS-1$ //$NON-NLS-2$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } if (forOut && !namedParamInfo.isOut) { throw new SQLException(Messages.getString("CallableStatement.5") + paramName //$NON-NLS-1$ + Messages.getString("CallableStatement.6"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } return namedParamInfo.index + 1; // JDBC indices are 1-based } private void setOutParams() throws SQLException { if (this.paramInfo.numParameters > 0) { for (Iterator paramIter = this.paramInfo.iterator(); paramIter.hasNext();) { CallableStatementParam outParamInfo = (CallableStatementParam) paramIter .next(); if (outParamInfo.isOut) { String outParameterName = mangleParameterName(outParamInfo.paramName); this.setBytesNoEscapeNoQuotes(outParamInfo.index + 1, StringUtils.getBytes(outParameterName, this.charConverter, this.charEncoding, this.connection.getServerCharacterEncoding(), this.connection.parserKnowsUnicode())); } } } } /** * Returns the ResultSet that holds the output parameters, or throws an * appropriate exception if none exist, or they weren't returned. * * @return the ResultSet that holds the output parameters * * @throws SQLException if no output parameters were defined, or if no * output parameters were returned. */ private ResultSet getOutputParameters() throws SQLException { if (this.outputParameterResults == null) { if (this.paramInfo.numberOfParameters() == 0) { throw new SQLException(Messages.getString("CallableStatement.7"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } throw new SQLException(Messages.getString("CallableStatement.8"), //$NON-NLS-1$ SQLError.SQL_STATE_GENERAL_ERROR); } return this.outputParameterResults; } private CallableStatementParam checkIsOutputParam(int paramIndex) throws SQLException { checkParameterIndexBounds(paramIndex); int localParamIndex = paramIndex - 1; CallableStatementParam paramDescriptor = this.paramInfo .getParameter(localParamIndex); if (!paramDescriptor.isOut) { throw new SQLException(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 { int localParamIndex = paramIndex - 1; if ((paramIndex < 0) || (localParamIndex >= this.paramInfo.numberOfParameters())) { throw new SQLException(Messages.getString("CallableStatement.11") + paramIndex //$NON-NLS-1$ + Messages.getString("CallableStatement.12") + this.paramInfo.numberOfParameters() //$NON-NLS-1$ + Messages.getString("CallableStatement.13"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ } } /** * 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 new SQLException(Messages.getString("CallableStatement.14"), //$NON-NLS-1$ SQLError.SQL_STATE_DRIVER_NOT_CAPABLE); } } private void determineParameterTypes() throws SQLException { java.sql.ResultSet paramTypesRs = null; try { String procName = extractProcedureName(); java.sql.DatabaseMetaData dbmd = this.connection.getMetaData(); paramTypesRs = dbmd.getProcedureColumns(null, null, procName, "%"); //$NON-NLS-1$ this.paramInfo = new CallableStatementParamInfo(paramTypesRs); } finally { SQLException sqlExRethrow = null; if (paramTypesRs != null) { try { paramTypesRs.close(); } catch (SQLException sqlEx) { sqlExRethrow = sqlEx; } paramTypesRs = null; } if (sqlExRethrow != null) { throw sqlExRethrow; } } } private String extractProcedureName() throws SQLException { // TODO: Do this with less memory allocation int endCallIndex = StringUtils.indexOfIgnoreCase(this.originalSql, "CALL"); //$NON-NLS-1$ if (endCallIndex != -1) { StringBuffer nameBuf = new StringBuffer(); String trimmedStatement = this.originalSql.substring(endCallIndex + 4).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 new SQLException(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 new SQLException(((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$ } return mangleParameterName(paramNameIn); /* if (paramNameIn.startsWith("@")) { return paramNameIn; } else { StringBuffer paramNameBuf = new StringBuffer("@"); paramNameBuf.append(paramNameIn); return paramNameBuf.toString(); }*/ } private int mapOutputParameterIndexToRsIndex(int paramIndex) throws SQLException { checkParameterIndexBounds(paramIndex); int localParamIndex = paramIndex - 1; int rsIndex = this.parameterIndexToRsIndex[localParamIndex]; if (rsIndex == NOT_OUTPUT_PARAMETER_INDICATOR) { throw new SQLException(Messages.getString("CallableStatement.21") + paramIndex //$NON-NLS-1$ + Messages.getString("CallableStatement.22"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } return rsIndex + 1; } /** * Issues a second query to retrieve all output parameters. * * @throws SQLException if an error occurs. */ private void retrieveOutParams() throws SQLException { int numParameters = this.paramInfo.numberOfParameters(); this.parameterIndexToRsIndex = new int[numParameters]; for (int i = 0; i < numParameters; i++) { this.parameterIndexToRsIndex[i] = NOT_OUTPUT_PARAMETER_INDICATOR; } int localParamIndex = 0; if (numParameters > 0) { StringBuffer outParameterQuery = new StringBuffer("SELECT "); //$NON-NLS-1$ boolean firstParam = true; boolean hadOutputParams = false; for (Iterator paramIter = this.paramInfo.iterator(); paramIter.hasNext();) { CallableStatementParam retrParamInfo = (CallableStatementParam) paramIter .next(); if (retrParamInfo.isOut) { hadOutputParams = true; this.parameterIndexToRsIndex[retrParamInfo.index] = localParamIndex++; String outParameterName = mangleParameterName(retrParamInfo.paramName); if (!firstParam) { outParameterQuery.append(","); //$NON-NLS-1$ } else { firstParam = false; } if (!outParameterName.startsWith("@")) { //$NON-NLS-1$ outParameterQuery.append('@'); } outParameterQuery.append(outParameterName); } } if (hadOutputParams) { // We can't use 'ourself' to execute this query, or any // pending result sets would be overwritten java.sql.Statement outParameterStmt = null; java.sql.ResultSet outParamRs = null; try { outParameterStmt = this.connection.createStatement(); outParamRs = outParameterStmt.executeQuery(outParameterQuery .toString()); this.outputParameterResults = ((com.mysql.jdbc.ResultSet) outParamRs) .copy(); if (!this.outputParameterResults.next()) { this.outputParameterResults.close(); this.outputParameterResults = null; } } finally { if (outParameterStmt != null) { outParameterStmt.close(); } } } else { this.outputParameterResults = null; } } else { this.outputParameterResults = null; } } private final static String PARAMETER_NAMESPACE_PREFIX = "@com_mysql_jdbc_outparam_"; //$NON-NLS-1$ private static String mangleParameterName(String origParameterName) { if (origParameterName == null) { return null; } int offset = 0; if (origParameterName.length() > 0 && origParameterName.charAt(0) == '@') { offset = 1; } StringBuffer paramNameBuf = new StringBuffer(PARAMETER_NAMESPACE_PREFIX.length() + origParameterName.length()); paramNameBuf.append(PARAMETER_NAMESPACE_PREFIX); paramNameBuf.append(origParameterName.substring(offset)); return paramNameBuf.toString(); } class CallableStatementParam { String paramName; boolean isIn; boolean isOut; int desiredJdbcType; int index; CallableStatementParam(String name, int idx, boolean in, boolean out) { this.paramName = name; this.isIn = in; this.isOut = out; this.index = idx; } /* (non-Javadoc) * @see java.lang.Object#clone() */ protected Object clone() throws CloneNotSupportedException { return super.clone(); } } class CallableStatementParamInfo { List parameterList; Map parameterMap; String catalogInUse; String nativeSql; int numParameters; CallableStatementParamInfo(java.sql.ResultSet paramTypesRs) throws SQLException { boolean hadRows = paramTypesRs.last(); this.nativeSql = originalSql; this.catalogInUse = currentCatalog; if (hadRows) { this.numParameters = paramTypesRs.getRow(); this.parameterList = new ArrayList(this.numParameters); this.parameterMap = new HashMap(this.numParameters); paramTypesRs.beforeFirst(); addParametersFromDBMD(paramTypesRs); } else { this.numParameters = 0; } } /* (non-Javadoc) * @see java.lang.Object#clone() */ protected Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); } CallableStatementParam getParameter(String name) { return (CallableStatementParam) this.parameterMap.get(name); } CallableStatementParam getParameter(int index) { return (CallableStatementParam) this.parameterList.get(index); } Iterator iterator() { return this.parameterList.iterator(); } int numberOfParameters() { return this.numParameters; } private void addParametersFromDBMD(java.sql.ResultSet paramTypesRs) throws SQLException { int i = 0; while (paramTypesRs.next()) { String paramName = paramTypesRs.getString(4); int inOutModifier = paramTypesRs.getInt(5); boolean isOutParameter = false; boolean isInParameter = false; if (inOutModifier == DatabaseMetaData.procedureColumnInOut) { isOutParameter = true; isInParameter = true; } else if (inOutModifier == DatabaseMetaData.procedureColumnIn) { isOutParameter = false; isInParameter = true; } else if (inOutModifier == DatabaseMetaData.procedureColumnOut) { isOutParameter = true; isInParameter = false; } CallableStatementParam paramInfoToAdd = new CallableStatementParam(paramName, i++, isInParameter, isOutParameter); this.parameterList.add(paramInfoToAdd); this.parameterMap.put(paramName, paramInfoToAdd); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -