下面列出了怎么用java.sql.Wrapper的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
final Object result;
if (iface.isAssignableFrom(getClass())) {
// if the proxy directly implements the interface or extends it, return the proxy
result = this;
} else if (iface.isAssignableFrom(delegate.getClass())) {
// if the proxied object directly implements the interface or extends it, return
// the proxied object
result = unwrapP6SpyProxy();
} else if (Wrapper.class.isAssignableFrom(delegate.getClass())) {
// if the proxied object implements the wrapper interface, then
// return the result of it's unwrap method.
result = ((Wrapper) unwrapP6SpyProxy()).unwrap(iface);
} else {
/*
This line of code can only be reached when the underlying object does not implement the wrapper
interface. This would mean that either the JDBC driver or the wrapper of the underlying object
does not implement the JDBC 4.0 API.
*/
throw new SQLException("Can not unwrap to " + iface.getName());
}
return iface.cast(result);
}
@Override
protected ClientPreparedStatement prepareBatchedInsertSQL(JdbcConnection localConn, int numBatches) throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
try {
ClientPreparedStatement pstmt = ((Wrapper) localConn.prepareStatement(((PreparedQuery<?>) this.query).getParseInfo().getSqlForBatch(numBatches),
this.resultSetConcurrency, this.query.getResultType().getIntValue())).unwrap(ClientPreparedStatement.class);
pstmt.setRetrieveGeneratedKeys(this.retrieveGeneratedKeys);
return pstmt;
} catch (UnsupportedEncodingException e) {
SQLException sqlEx = SQLError.createSQLException(Messages.getString("ServerPreparedStatement.27"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
this.exceptionInterceptor);
sqlEx.initCause(e);
throw sqlEx;
}
}
}
@Override
protected ClientPreparedStatement prepareBatchedInsertSQL(JdbcConnection localConn, int numBatches) throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
try {
ClientPreparedStatement pstmt = ((Wrapper) localConn.prepareStatement(((PreparedQuery<?>) this.query).getParseInfo().getSqlForBatch(numBatches),
this.resultSetConcurrency, this.query.getResultType().getIntValue())).unwrap(ClientPreparedStatement.class);
pstmt.setRetrieveGeneratedKeys(this.retrieveGeneratedKeys);
return pstmt;
} catch (UnsupportedEncodingException e) {
SQLException sqlEx = SQLError.createSQLException(Messages.getString("ServerPreparedStatement.27"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
this.exceptionInterceptor);
sqlEx.initCause(e);
throw sqlEx;
}
}
}
@BuildStep
void registerDriverForReflection(BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {
reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, Driver.class.getName()));
reflectiveClass.produce(
new ReflectiveClassBuildItem(false, false, FailoverDnsSrvConnectionUrl.class.getName()));
reflectiveClass.produce(
new ReflectiveClassBuildItem(false, false, FailoverConnectionUrl.class.getName()));
reflectiveClass
.produce(new ReflectiveClassBuildItem(false, false, SingleConnectionUrl.class.getName()));
reflectiveClass.produce(
new ReflectiveClassBuildItem(false, false, LoadBalanceConnectionUrl.class.getName()));
reflectiveClass.produce(new ReflectiveClassBuildItem(false, false,
LoadBalanceDnsSrvConnectionUrl.class.getName()));
reflectiveClass.produce(new ReflectiveClassBuildItem(false, false,
ReplicationDnsSrvConnectionUrl.class.getName()));
reflectiveClass.produce(
new ReflectiveClassBuildItem(false, false, ReplicationConnectionUrl.class.getName()));
reflectiveClass.produce(
new ReflectiveClassBuildItem(false, false, XDevApiConnectionUrl.class.getName()));
reflectiveClass.produce(
new ReflectiveClassBuildItem(false, false, XDevApiDnsSrvConnectionUrl.class.getName()));
reflectiveClass.produce(new ReflectiveClassBuildItem(false, false,
com.mysql.cj.jdbc.ha.LoadBalancedAutoCommitInterceptor.class.getName()));
reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, StandardLogger.class.getName()));
reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, Wrapper.class.getName()));
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
if (iface.isAssignableFrom(getClass())) {
// if the proxy directly proxy the interface or extends it, return true
return true;
} else if (iface.isAssignableFrom(delegate.getClass())) {
// if the proxied object directly implements the interface or extends it, return true
return true;
} else if (Wrapper.class.isAssignableFrom(delegate.getClass())) {
// if the proxied object implements the wrapper interface, then
// return the result of it's isWrapperFor method.
return ((Wrapper) unwrapP6SpyProxy()).isWrapperFor(iface);
}
return false;
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
boolean isInstance = iface.isInstance(this);
if (isInstance) {
return true;
}
return (iface.getName().equals(JdbcConnection.class.getName()) || iface.getName().equals(MysqlConnection.class.getName())
|| iface.getName().equals(java.sql.Connection.class.getName()) || iface.getName().equals(Wrapper.class.getName())
|| iface.getName().equals(AutoCloseable.class.getName()));
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
boolean isInstance = iface.isInstance(this);
if (isInstance) {
return true;
}
return (iface.getName().equals(JdbcConnection.class.getName()) || iface.getName().equals(MysqlConnection.class.getName())
|| iface.getName().equals(java.sql.Connection.class.getName()) || iface.getName().equals(Wrapper.class.getName())
|| iface.getName().equals(AutoCloseable.class.getName()));
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
if (xaDataSource == null) {
throw new SQLException("The underlying XADataSource is invalid or cannot be found");
} else if (iface.isInstance(xaDataSource)) {
return iface.cast(xaDataSource);
} else if (xaDataSource instanceof Wrapper) {
return ((java.sql.Wrapper) xaDataSource).unwrap(iface);
} else {
throw new SQLException("The requested interface cannot be unwrapped");
}
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
if (xaDataSource == null) {
throw new SQLException("The underlying XADataSource is invalid or cannot be found");
} else if (iface.isInstance(xaDataSource)) {
return true;
} else if (xaDataSource instanceof Wrapper) {
return ((java.sql.Wrapper) xaDataSource).isWrapperFor(iface);
}
return false;
}
protected Object unwrap(MethodInvocation<T> methodInvocation) throws SQLException {
final Class iface = getClassArg(methodInvocation);
final Wrapper delegateWrapper = (Wrapper) delegate;
Object result;
if (isDelegateType(iface)) {
result = delegateWrapper.isWrapperFor(iface) ? delegateWrapper.unwrap(iface) : iface.cast(delegateWrapper);
} else {
result = delegateWrapper.unwrap(iface);
}
return result;
}
private void setInOutParamsOnServer() throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
if (this.paramInfo.numParameters > 0) {
for (Iterator<CallableStatementParam> paramIter = this.paramInfo.iterator(); paramIter.hasNext();) {
CallableStatementParam inParamInfo = paramIter.next();
if (inParamInfo.isOut && inParamInfo.isIn) {
if (inParamInfo.paramName == null) {
inParamInfo.paramName = "nullnp" + inParamInfo.index;
}
String inOutParameterName = mangleParameterName(inParamInfo.paramName);
StringBuilder queryBuf = new StringBuilder(4 + inOutParameterName.length() + 1 + 1);
queryBuf.append("SET ");
queryBuf.append(inOutParameterName);
queryBuf.append("=?");
ClientPreparedStatement setPstmt = null;
try {
setPstmt = ((Wrapper) this.connection.clientPrepareStatement(queryBuf.toString())).unwrap(ClientPreparedStatement.class);
if (((PreparedQuery<?>) this.query).getQueryBindings().getBindValues()[inParamInfo.index].isNull()) {
setPstmt.setBytesNoEscapeNoQuotes(1, "NULL".getBytes());
} else {
byte[] parameterAsBytes = getBytesRepresentation(inParamInfo.index);
if (parameterAsBytes != null) {
if (parameterAsBytes.length > 8 && parameterAsBytes[0] == '_' && parameterAsBytes[1] == 'b' && parameterAsBytes[2] == 'i'
&& parameterAsBytes[3] == 'n' && parameterAsBytes[4] == 'a' && parameterAsBytes[5] == 'r'
&& parameterAsBytes[6] == 'y' && parameterAsBytes[7] == '\'') {
setPstmt.setBytesNoEscapeNoQuotes(1, parameterAsBytes);
} else {
switch (inParamInfo.desiredMysqlType) {
case BIT:
case BINARY:
case GEOMETRY:
case TINYBLOB:
case BLOB:
case MEDIUMBLOB:
case LONGBLOB:
case VARBINARY:
setPstmt.setBytes(1, parameterAsBytes);
break;
default:
// the inherited PreparedStatement methods have already escaped and quoted these parameters
setPstmt.setBytesNoEscape(1, parameterAsBytes);
}
}
} else {
setPstmt.setNull(1, MysqlType.NULL);
}
}
setPstmt.executeUpdate();
} finally {
if (setPstmt != null) {
setPstmt.close();
}
}
}
}
}
}
}
private void setInOutParamsOnServer() throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
if (this.paramInfo.numParameters > 0) {
for (Iterator<CallableStatementParam> paramIter = this.paramInfo.iterator(); paramIter.hasNext();) {
CallableStatementParam inParamInfo = paramIter.next();
if (inParamInfo.isOut && inParamInfo.isIn) {
if (inParamInfo.paramName == null) {
inParamInfo.paramName = "nullnp" + inParamInfo.index;
}
String inOutParameterName = mangleParameterName(inParamInfo.paramName);
StringBuilder queryBuf = new StringBuilder(4 + inOutParameterName.length() + 1 + 1);
queryBuf.append("SET ");
queryBuf.append(inOutParameterName);
queryBuf.append("=?");
ClientPreparedStatement setPstmt = null;
try {
setPstmt = ((Wrapper) this.connection.clientPrepareStatement(queryBuf.toString())).unwrap(ClientPreparedStatement.class);
if (((PreparedQuery<?>) this.query).getQueryBindings().getBindValues()[inParamInfo.index].isNull()) {
setPstmt.setBytesNoEscapeNoQuotes(1, "NULL".getBytes());
} else {
byte[] parameterAsBytes = getBytesRepresentation(inParamInfo.index);
if (parameterAsBytes != null) {
if (parameterAsBytes.length > 8 && parameterAsBytes[0] == '_' && parameterAsBytes[1] == 'b' && parameterAsBytes[2] == 'i'
&& parameterAsBytes[3] == 'n' && parameterAsBytes[4] == 'a' && parameterAsBytes[5] == 'r'
&& parameterAsBytes[6] == 'y' && parameterAsBytes[7] == '\'') {
setPstmt.setBytesNoEscapeNoQuotes(1, parameterAsBytes);
} else {
switch (inParamInfo.desiredMysqlType) {
case BIT:
case BINARY:
case GEOMETRY:
case TINYBLOB:
case BLOB:
case MEDIUMBLOB:
case LONGBLOB:
case VARBINARY:
setPstmt.setBytes(1, parameterAsBytes);
break;
default:
// the inherited PreparedStatement methods have already escaped and quoted these parameters
setPstmt.setBytesNoEscape(1, parameterAsBytes);
}
}
} else {
setPstmt.setNull(1, MysqlType.NULL);
}
}
setPstmt.executeUpdate();
} finally {
if (setPstmt != null) {
setPstmt.close();
}
}
}
}
}
}
}
/**
* Returns an object that implements the given interface to allow access to
* non-standard methods, or standard methods not exposed by the proxy.
*
* @param <T> the type of the class modeled by this Class object.
* @param wrapper the wrapper class.
* @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.2
*/
@SuppressWarnings("unchecked")
public static <T> T unwrap(final Wrapper wrapper, final Class<T> iFace) throws SQLException {
if (wrapper.isWrapperFor(iFace)) {
return (T) wrapper;
}
throw new SQLException("Type not found.", SQLStates.TYPE_NOT_FOUND.getValue());
}
/**
* Returns true if this either implements the interface argument or is
* directly or indirectly a wrapper for an object that does. Returns false
* otherwise..
*
* @param wrapper wrapper to test for.
* @param iFace a Class defining an interface.
* @return true if this implements the interface or directly or indirectly
* wraps an object that does.
* @since 1.2
*/
public static boolean isWrapperFor(final Wrapper wrapper, final Class<?> iFace) {
return wrapper.getClass().isAssignableFrom(iFace);
}