java.sql.Wrapper Java Examples

The following examples show how to use java.sql.Wrapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractWrapper.java    From tx-lcn with Apache License 2.0 7 votes vote down vote up
@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);
}
 
Example #2
Source File: ServerPreparedStatement.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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;
        }
    }
}
 
Example #3
Source File: ServerPreparedStatement.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
@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;
        }
    }
}
 
Example #4
Source File: MySQLJDBCReflections.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@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()));
}
 
Example #5
Source File: AbstractWrapper.java    From tx-lcn with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #6
Source File: ConnectionWrapper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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()));
}
 
Example #7
Source File: ConnectionWrapper.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@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()));
}
 
Example #8
Source File: NonJtaXADataSourceWrapper.java    From kumuluzee with MIT License 5 votes vote down vote up
@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");
    }
}
 
Example #9
Source File: NonJtaXADataSourceWrapper.java    From kumuluzee with MIT License 5 votes vote down vote up
@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;
}
 
Example #10
Source File: JdbcProxyHandler.java    From metrics-sql with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #11
Source File: CallableStatement.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
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();
                        }
                    }
                }
            }
        }
    }
}
 
Example #12
Source File: CallableStatement.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
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();
                        }
                    }
                }
            }
        }
    }
}
 
Example #13
Source File: Utils.java    From paradoxdriver with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * 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());
}
 
Example #14
Source File: Utils.java    From paradoxdriver with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * 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);
}