?? callablestatement.java
字號:
/* Copyright (C) 2002-2007 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. There are special exceptions to the terms and conditions of the GPL as it is applied to this software. View the full text of the exception in file EXCEPTIONS-CONNECTOR-J in the directory of this software distribution. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package com.mysql.jdbc;import java.io.InputStream;import java.io.Reader;import java.io.UnsupportedEncodingException;import java.math.BigDecimal;import java.net.URL;import java.sql.Array;import java.sql.Blob;import java.sql.Clob;import java.sql.Date;import java.sql.ParameterMetaData;import java.sql.Ref;import java.sql.SQLException;import java.sql.Time;import java.sql.Timestamp;import java.sql.Types;import java.util.ArrayList;import java.util.Calendar;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;/** * Representation of stored procedures for JDBC * * @author Mark Matthews * @version $Id: CallableStatement.java,v 1.1.2.1 2005/05/13 18:58:38 mmatthews * Exp $ */public class CallableStatement extends PreparedStatement implements java.sql.CallableStatement { class CallableStatementParam { int desiredJdbcType; int index; int inOutModifier; boolean isIn; boolean isOut; int jdbcType; short nullability; String paramName; int precision; int scale; String typeName; CallableStatementParam(String name, int idx, boolean in, boolean out, int jdbcType, String typeName, int precision, int scale, short nullability, int inOutModifier) { this.paramName = name; this.isIn = in; this.isOut = out; this.index = idx; this.jdbcType = jdbcType; this.typeName = typeName; this.precision = precision; this.scale = scale; this.nullability = nullability; this.inOutModifier = inOutModifier; } /* * (non-Javadoc) * * @see java.lang.Object#clone() */ protected Object clone() throws CloneNotSupportedException { return super.clone(); } } class CallableStatementParamInfo { String catalogInUse; boolean isFunctionCall; String nativeSql; int numParameters; List parameterList; Map parameterMap; /** * Constructor that converts a full list of parameter metadata into one * that only represents the placeholders present in the {CALL ()}. * * @param fullParamInfo the metadata for all parameters for this stored * procedure or function. */ CallableStatementParamInfo(CallableStatementParamInfo fullParamInfo) { this.nativeSql = originalSql; this.catalogInUse = currentCatalog; isFunctionCall = fullParamInfo.isFunctionCall; int[] localParameterMap = placeholderToParameterIndexMap; int parameterMapLength = localParameterMap.length; parameterList = new ArrayList(fullParamInfo.numParameters); parameterMap = new HashMap(fullParamInfo.numParameters); if (isFunctionCall) { // Take the return value parameterList.add(fullParamInfo.parameterList.get(0)); } int offset = isFunctionCall ? 1 : 0; for (int i = 0; i < parameterMapLength; i++) { if (localParameterMap[i] != 0) { CallableStatementParam param = (CallableStatementParam)fullParamInfo.parameterList.get(localParameterMap[i] + offset); parameterList.add(param); parameterMap.put(param.paramName, param); } } this.numParameters = parameterList.size(); } CallableStatementParamInfo(java.sql.ResultSet paramTypesRs) throws SQLException { boolean hadRows = paramTypesRs.last(); this.nativeSql = originalSql; this.catalogInUse = currentCatalog; isFunctionCall = callingStoredFunction; 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; } if (isFunctionCall) { this.numParameters += 1; } } 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 (i == 0 && isFunctionCall) { isOutParameter = true; isInParameter = false; } else 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; } int jdbcType = paramTypesRs.getInt(6); String typeName = paramTypesRs.getString(7); int precision = paramTypesRs.getInt(8); int scale = paramTypesRs.getInt(10); short nullability = paramTypesRs.getShort(12); CallableStatementParam paramInfoToAdd = new CallableStatementParam( paramName, i++, isInParameter, isOutParameter, jdbcType, typeName, precision, scale, nullability, inOutModifier); this.parameterList.add(paramInfoToAdd); this.parameterMap.put(paramName, paramInfoToAdd); } } protected void checkBounds(int paramIndex) throws SQLException { int localParamIndex = paramIndex - 1; if ((paramIndex < 0) || (localParamIndex >= this.numParameters)) { throw SQLError.createSQLException( Messages.getString("CallableStatement.11") + paramIndex //$NON-NLS-1$ + Messages.getString("CallableStatement.12") + numParameters //$NON-NLS-1$ + Messages.getString("CallableStatement.13"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ } } /* * (non-Javadoc) * * @see java.lang.Object#clone() */ protected Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); } CallableStatementParam getParameter(int index) { return (CallableStatementParam) this.parameterList.get(index); } CallableStatementParam getParameter(String name) { return (CallableStatementParam) this.parameterMap.get(name); } public String getParameterClassName(int arg0) throws SQLException { String mysqlTypeName = getParameterTypeName(arg0); boolean isBinaryOrBlob = StringUtils.indexOfIgnoreCase(mysqlTypeName, "BLOB") != -1 || StringUtils.indexOfIgnoreCase(mysqlTypeName, "BINARY") != -1; boolean isUnsigned = StringUtils.indexOfIgnoreCase(mysqlTypeName, "UNSIGNED") != -1; int mysqlTypeIfKnown = 0; if (StringUtils.startsWithIgnoreCase(mysqlTypeName, "MEDIUMINT")) { mysqlTypeIfKnown = MysqlDefs.FIELD_TYPE_INT24; } return ResultSetMetaData.getClassNameForJavaType(getParameterType(arg0), isUnsigned, mysqlTypeIfKnown, isBinaryOrBlob, false); } public int getParameterCount() throws SQLException { if (this.parameterList == null) { return 0; } return this.parameterList.size(); } public int getParameterMode(int arg0) throws SQLException { checkBounds(arg0); return getParameter(arg0 - 1).inOutModifier; } public int getParameterType(int arg0) throws SQLException { checkBounds(arg0); return getParameter(arg0 - 1).jdbcType; } public String getParameterTypeName(int arg0) throws SQLException { checkBounds(arg0); return getParameter(arg0 - 1).typeName; } public int getPrecision(int arg0) throws SQLException { checkBounds(arg0); return getParameter(arg0 - 1).precision; } public int getScale(int arg0) throws SQLException { checkBounds(arg0); return getParameter(arg0 - 1).scale; } public int isNullable(int arg0) throws SQLException { checkBounds(arg0); return getParameter(arg0 - 1).nullability; } public boolean isSigned(int arg0) throws SQLException { checkBounds(arg0); return false; } Iterator iterator() { return this.parameterList.iterator(); } int numberOfParameters() { return this.numParameters; } } /** * Can't implement this directly, as then you can't use callable statements * on JDK-1.3.1, which unfortunately isn't EOL'd yet, and still present * quite a bit out there in the wild (Websphere, FreeBSD, anyone?) */ class CallableStatementParamInfoJDBC3 extends CallableStatementParamInfo implements ParameterMetaData { CallableStatementParamInfoJDBC3(java.sql.ResultSet paramTypesRs) throws SQLException { super(paramTypesRs); } public CallableStatementParamInfoJDBC3(CallableStatementParamInfo paramInfo) { super(paramInfo); } } private final static int NOT_OUTPUT_PARAMETER_INDICATOR = Integer.MIN_VALUE; 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(); } private boolean callingStoredFunction = false; private ResultSet functionReturnValueResults; private boolean hasOutputParams = false; // private List parameterList; // private Map parameterMap; private ResultSet outputParameterResults; private boolean outputParamWasNull = false; private int[] parameterIndexToRsIndex; protected CallableStatementParamInfo paramInfo; private CallableStatementParam returnValueParam; /** * Creates a new CallableStatement * * @param conn * the connection creating this statement * @param paramInfo * the SQL to prepare * * @throws SQLException * if an error occurs */ public CallableStatement(Connection conn, CallableStatementParamInfo paramInfo) throws SQLException { super(conn, paramInfo.nativeSql, paramInfo.catalogInUse); this.paramInfo = paramInfo; this.callingStoredFunction = this.paramInfo.isFunctionCall; if (this.callingStoredFunction) { this.parameterCount += 1; } } /** * Creates a new CallableStatement * * @param conn * the connection creating this statement * @param catalog * catalog the current catalog * * @throws SQLException * if an error occurs */ public CallableStatement(Connection conn, String catalog) throws SQLException { super(conn, catalog, null); determineParameterTypes(); generateParameterMap(); if (this.callingStoredFunction) { this.parameterCount += 1; } } private int[] placeholderToParameterIndexMap; private void generateParameterMap() throws SQLException { // if the user specified some parameters as literals, we need to // provide a map from the specified placeholders to the actual // parameter numbers int parameterCountFromMetaData = this.paramInfo.getParameterCount(); // Ignore the first ? if this is a stored function, it doesn't count if (this.callingStoredFunction) { parameterCountFromMetaData--; } if (this.paramInfo != null && this.parameterCount != parameterCountFromMetaData) { this.placeholderToParameterIndexMap = new int[this.parameterCount]; int startPos = this.callingStoredFunction ? StringUtils.indexOfIgnoreCase(this.originalSql, "SELECT") : StringUtils.indexOfIgnoreCase(this.originalSql, "CALL"); if (startPos != -1) { int parenOpenPos = this.originalSql.indexOf('(', startPos + 4); if (parenOpenPos != -1) { int parenClosePos = StringUtils.indexOfIgnoreCaseRespectQuotes(parenOpenPos, this.originalSql, ")", '\'', true); if (parenClosePos != -1) { List parsedParameters = StringUtils.split(this.originalSql.substring(parenOpenPos + 1, parenClosePos), ",", "'\"", "'\"", true); int numParsedParameters = parsedParameters.size(); // sanity check if (numParsedParameters != this.parameterCount) { // bail? } int placeholderCount = 0; for (int i = 0; i < numParsedParameters; i++) { if (((String)parsedParameters.get(i)).equals("?")) { this.placeholderToParameterIndexMap[placeholderCount++] = i; } } } } } } } /** * Creates a new CallableStatement * * @param conn * the connection creating this statement * @param sql * the SQL to prepare * @param catalog * the current catalog * * @throws SQLException * if an error occurs */ public CallableStatement(Connection conn, String sql, String catalog, boolean isFunctionCall) throws SQLException { super(conn, sql, catalog); this.callingStoredFunction = isFunctionCall; determineParameterTypes(); generateParameterMap(); if (this.callingStoredFunction) { this.parameterCount += 1; } } /* * (non-Javadoc) * * @see java.sql.PreparedStatement#addBatch() */ public void addBatch() throws SQLException { setOutParams(); super.addBatch(); } private CallableStatementParam checkIsOutputParam(int paramIndex) throws SQLException { if (this.callingStoredFunction) { if (paramIndex == 1) { if (this.returnValueParam == null) { this.returnValueParam = new CallableStatementParam("", 0, false, true, Types.VARCHAR, "VARCHAR", 0, 0, DatabaseMetaData.attributeNullableUnknown, DatabaseMetaData.procedureColumnReturn); } return this.returnValueParam; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -