Example usage for java.sql Connection unwrap

List of usage examples for java.sql Connection unwrap

Introduction

In this page you can find the example usage for java.sql Connection unwrap.

Prototype

<T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException;

Source Link

Document

Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy.

Usage

From source file:com.netsteadfast.greenstep.util.OlapUtils.java

/**
 * ?//from  w  w w. j a va  2  s. co  m
 * 
 * @param driver
 * @param url
 * @return
 * @throws ClassNotFoundException
 * @throws SQLException
 */
public static OlapConnection getConnection(String driver, String url)
        throws ClassNotFoundException, SQLException, Exception {

    Class.forName(driver);
    Connection connection = DriverManager.getConnection(url);
    return connection.unwrap(OlapConnection.class);
}

From source file:com.alibaba.druid.pool.bonecp.TestLRU.java

public static MockConnection unwrap(Connection conn) throws Exception {
    if (conn instanceof ConnectionHandle) {
        ConnectionHandle handle = (ConnectionHandle) conn;
        return (MockConnection) handle.getInternalConnection();
    }/*www.  jav a 2 s . c  o m*/
    if (conn instanceof NewProxyConnection) {
        NewProxyConnection handle = (NewProxyConnection) conn;

        Field field = NewProxyConnection.class.getDeclaredField("inner");
        field.setAccessible(true);
        return (MockConnection) field.get(handle);
    }

    return conn.unwrap(MockConnection.class);
}

From source file:mil.army.usace.data.dataquery.rdbms.implementations.OracleConverter.java

public OracleConverter(Connection conn) {
    try {//from ww w. j  ava  2s .c o m
        this.oc = conn.unwrap(OracleConnection.class);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.teradata.benchto.driver.macro.query.QueryMacroExecutionDriver.java

private void setSessionForPresto(Connection connection, String sqlStatement) {
    PrestoConnection prestoConnection;//w  w w.j  a v  a  2 s .c o m
    try {
        prestoConnection = connection.unwrap(PrestoConnection.class);
    } catch (SQLException e) {
        LOGGER.error(e.getMessage());
        throw new UnsupportedOperationException(
                format("SET SESSION for non PrestoConnection [%s] is not supported", connection.getClass()));
    }
    String[] keyValue = extractKeyValue(sqlStatement);
    prestoConnection.setSessionProperty(keyValue[0].trim(), keyValue[1].trim());
}

From source file:net.hydromatic.optiq.test.JdbcTest.java

static OptiqConnection getConnection(String... schema) throws ClassNotFoundException, SQLException {
    Class.forName("net.hydromatic.optiq.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:optiq:");
    OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class);
    MutableSchema rootSchema = optiqConnection.getRootSchema();
    final List<String> schemaList = Arrays.asList(schema);
    if (schemaList.contains("hr")) {
        ReflectiveSchema.create(rootSchema, "hr", new HrSchema());
    }/*from w  w w .  ja va  2  s . c o m*/
    if (schemaList.contains("foodmart")) {
        ReflectiveSchema.create(rootSchema, "foodmart", new FoodmartSchema());
    }
    if (schemaList.contains("metadata")) {
        // always present
    }
    return optiqConnection;
}

From source file:net.hydromatic.optiq.test.JdbcTest.java

/**
 * Creates a connection with a given query provider. If provider is null,
 * uses the connection as its own provider. The connection contains a
 * schema called "foodmart" backed by a JDBC connection to MySQL.
 *
 * @param queryProvider Query provider/*  w w  w. j a v  a  2 s.  c om*/
 * @param withClone Whether to create a "foodmart2" schema as in-memory
 *     clone
 * @return Connection
 * @throws ClassNotFoundException
 * @throws SQLException
 */
static OptiqConnection getConnection(QueryProvider queryProvider, boolean withClone)
        throws ClassNotFoundException, SQLException {
    Class.forName("net.hydromatic.optiq.jdbc.Driver");
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:optiq:");
    OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class);
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUrl("jdbc:mysql://localhost");
    dataSource.setUsername("foodmart");
    dataSource.setPassword("foodmart");

    JdbcSchema foodmart = JdbcSchema.create(optiqConnection.getRootSchema(), dataSource, "foodmart", null,
            "foodmart");
    if (withClone) {
        CloneSchema.create(optiqConnection.getRootSchema(), "foodmart2", foodmart);
    }
    optiqConnection.setSchema("foodmart2");
    return optiqConnection;
}

From source file:io.lightlink.oracle.AbstractOracleType.java

protected OracleConnection unwrap(Connection con) throws SQLException {
    if (con instanceof OracleConnection)
        return (OracleConnection) con;
    else/*from  w  ww.  j  a va2  s  .c  om*/
        return con.unwrap(OracleConnection.class);
}

From source file:com.p6spy.engine.common.P6WrapperUnwrapDelegateTest.java

@Test
public void testCastableFromProxy() throws SQLException {
    Connection con = new TestConnectionImpl();
    Connection proxy = ProxyFactory.createProxy(con, new GenericInvocationHandler<Connection>(con));

    // if the proxy implements the interface then the proxy should be returned
    {/* www.  j  a  v a2 s.  co  m*/
        Connection unwrapped = proxy.unwrap(Connection.class);
        assertTrue(ProxyFactory.isProxy(unwrapped));
    }

    {
        TestConnection unwrapped = proxy.unwrap(TestConnection.class);
        assertTrue(ProxyFactory.isProxy(unwrapped));
    }

    {
        Wrapper unwrapped = proxy.unwrap(Wrapper.class);
        assertTrue(ProxyFactory.isProxy(unwrapped));
    }

    {
        AutoCloseable unwrapped = proxy.unwrap(AutoCloseable.class);
        assertTrue(ProxyFactory.isProxy(unwrapped));
    }

    // TestConnectionImpl is not implemented by the proxy - proxy will not be returned
    {
        TestConnectionImpl unwrapped = proxy.unwrap(TestConnectionImpl.class);
        assertFalse(ProxyFactory.isProxy(unwrapped));
    }

}

From source file:com.p6spy.engine.common.P6WrapperUnwrapDelegateTest.java

@Test
public void testCastableFromUnderlying() throws SQLException {
    Connection con = new TestConnectionImpl();
    Connection proxy = ProxyFactory.createProxy(con, new GenericInvocationHandler<Connection>(con));

    // if the underlying object extends the class (or matches the class) then the underlying object should be returned.
    {//w  ww.ja  v a2  s. c  o m
        AbstractTestConnection unwrapped = proxy.unwrap(AbstractTestConnection.class);
        assertFalse(ProxyFactory.isProxy(unwrapped));
    }

    {
        TestConnectionImpl unwrapped = proxy.unwrap(TestConnectionImpl.class);
        assertFalse(ProxyFactory.isProxy(unwrapped));
    }

}

From source file:net.sf.jasperreports.data.xmla.XmlaDataAdapterService.java

@Override
public void test() throws JRException {
    Map<String, Object> params = new HashMap<String, Object>();
    contributeParameters(params);//from   w w  w  . j a  va  2  s . c o  m

    Properties props = new Properties();
    putNonNull(props, Olap4jXmlaQueryExecuter.XMLA_SERVER,
            params.get(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_URL));
    putNonNull(props, Olap4jXmlaQueryExecuter.XMLA_CATALOG,
            params.get(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_CATALOG));
    putNonNull(props, Olap4jXmlaQueryExecuter.XMLA_DATA_SOURCE,
            params.get(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_DATASOURCE));
    putNonNull(props, Olap4jXmlaQueryExecuter.XMLA_USER,
            params.get(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_USER));
    putNonNull(props, Olap4jXmlaQueryExecuter.XMLA_PASSWORD,
            params.get(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_PASSWORD));
    putNonNull(props, Olap4jXmlaQueryExecuter.OLAP4J_DRIVER, Olap4jXmlaQueryExecuter.OLAP4J_XMLA_DRIVER_CLASS);
    putNonNull(props, Olap4jXmlaQueryExecuter.OLAP4J_URL_PREFIX,
            Olap4jXmlaQueryExecuter.OLAP4J_XMLA_URL_PREFIX);

    try {
        // load driver  and Connection
        Class.forName(Olap4jXmlaQueryExecuter.OLAP4J_XMLA_DRIVER_CLASS);
        Connection connection = DriverManager.getConnection(Olap4jXmlaQueryExecuter.OLAP4J_XMLA_URL_PREFIX,
                props);
        OlapConnection olapConnection = connection.unwrap(OlapConnection.class);

        // doing something to validate the connection
        OlapDatabaseMetaData metaData = olapConnection.getMetaData();
        ResultSet datasources = null;
        try {
            // try olap4j 1.1 first
            Method method = OlapDatabaseMetaData.class.getMethod("getDatabases");
            datasources = (ResultSet) method.invoke(metaData);
        } catch (NoSuchMethodException e) {
            // not olap4j 1.1
            if (log.isDebugEnabled()) {
                log.debug("OlapDatabaseMetaData.getDatabases method not found: " + e.getMessage());
            }
        }

        if (datasources == null) {
            try {
                // try olap4j 0.9
                Method method = OlapDatabaseMetaData.class.getMethod("getDatasources");
                datasources = (ResultSet) method.invoke(metaData);
            } catch (NoSuchMethodException e) {
                // not olap4j 0.9?  giving up
                if (log.isDebugEnabled()) {
                    log.debug("OlapDatabaseMetaData.getDatasources method not found: " + e.getMessage());
                }
            }
        }

        if (datasources != null) {
            // making sure the request is sent
            datasources.next();

            datasources.close();
        }

        connection.close();
    } catch (ClassNotFoundException e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_XMLA_CONNECTION, null, e);
    } catch (IllegalAccessException e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_XMLA_CONNECTION, null, e);
    } catch (InvocationTargetException e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_XMLA_CONNECTION, null, e);
    } catch (SQLException e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_XMLA_CONNECTION, null, e);
    }

    dispose();
}