?? 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.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;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.Map;import java.util.Properties;import com.mysql.jdbc.exceptions.NotYetImplementedException;/** * 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 { protected final static Constructor JDBC_4_CSTMT_2_ARGS_CTOR; protected final static Constructor JDBC_4_CSTMT_4_ARGS_CTOR; static { if (Util.isJdbc4()) { try { JDBC_4_CSTMT_2_ARGS_CTOR = Class.forName( "com.mysql.jdbc.JDBC4CallableStatement") .getConstructor( new Class[] { ConnectionImpl.class, CallableStatementParamInfo.class }); JDBC_4_CSTMT_4_ARGS_CTOR = Class.forName( "com.mysql.jdbc.JDBC4CallableStatement") .getConstructor( new Class[] { ConnectionImpl.class, String.class, String.class, Boolean.TYPE }); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } else { JDBC_4_CSTMT_4_ARGS_CTOR = null; JDBC_4_CSTMT_2_ARGS_CTOR = null; } } protected 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(); } } protected 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?) */ protected class CallableStatementParamInfoJDBC3 extends CallableStatementParamInfo implements ParameterMetaData { CallableStatementParamInfoJDBC3(java.sql.ResultSet paramTypesRs) throws SQLException { super(paramTypesRs); } public CallableStatementParamInfoJDBC3(CallableStatementParamInfo paramInfo) { super(paramInfo); } /** * Returns true if this either implements the interface argument or is directly or indirectly a wrapper * for an object that does. Returns false otherwise. If this implements the interface then return true, * else if this is a wrapper then return the result of recursively calling <code>isWrapperFor</code> on the wrapped * object. If this does not implement the interface and is not a wrapper, return false. * This method should be implemented as a low-cost operation compared to <code>unwrap</code> so that * callers can use this method to avoid expensive <code>unwrap</code> calls that may fail. If this method * returns true then calling <code>unwrap</code> with the same argument should succeed. * * @param interfaces a Class defining an interface. * @return true if this implements the interface or directly or indirectly wraps an object that does. * @throws java.sql.SQLException if an error occurs while determining whether this is a wrapper * for an object with the given interface. * @since 1.6 */ public boolean isWrapperFor(Class iface) throws SQLException { checkClosed(); // This works for classes that aren't actually wrapping // anything return iface.isInstance(this); } /** * Returns an object that implements the given interface to allow access to non-standard methods, * or standard methods not exposed by the proxy. * The result may be either the object found to implement the interface or a proxy for that object. * If the receiver implements the interface then that is the object. If the receiver is a wrapper * and the wrapped object implements the interface then that is the object. Otherwise the object is * the result of calling <code>unwrap</code> recursively on the wrapped object. If the receiver is not a * wrapper and does not implement the interface, then an <code>SQLException</code> is thrown. * * @param iface A Class defining an interface that the result must implement. * @return an object that implements the interface. May be a proxy for the actual implementing object. * @throws java.sql.SQLException If no object found that implements the interface * @since 1.6 */ public Object unwrap(Class iface) throws java.sql.SQLException { try { // This works for classes that aren't actually wrapping // anything return Util.cast(iface, this); } catch (ClassCastException cce) { throw SQLError.createSQLException("Unable to unwrap to " + iface.toString(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } } } 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 ResultSetInternalMethods functionReturnValueResults;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -