Search in sources :

Example 11 with ResultSetProxy

use of com.alibaba.druid.proxy.jdbc.ResultSetProxy in project druid by alibaba.

the class DruidDataSourceTest6 method setUp.

protected void setUp() throws Exception {
    returnEmptyCount.set(0);
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setTestOnBorrow(true);
    dataSource.setInitialSize(1);
    dataSource.setValidationQuery("select 1");
    dataSource.setValidationQueryTimeout(10);
    dataSource.setQueryTimeout(100);
    dataSource.setUserCallback(new NameCallback("xx") {
    });
    dataSource.setPasswordCallback(new DruidPasswordCallback() {

        @Override
        public char[] getPassword() {
            return "xx".toCharArray();
        }
    });
    dataSource.getProxyFilters().add(new FilterAdapter() {

        public ResultSetProxy statement_executeQuery(FilterChain chain, StatementProxy statement, String sql) throws SQLException {
            if (errorCount.get() > 0) {
                errorCount.decrementAndGet();
                throw new RuntimeException();
            }
            if (returnEmptyCount.get() > 0) {
                returnEmptyCount.decrementAndGet();
                return new ResultSetProxyImpl(statement, new MockResultSet(statement), 0, sql);
            }
            return chain.statement_executeQuery(statement, sql);
        }
    });
}
Also used : SQLException(java.sql.SQLException) FilterChain(com.alibaba.druid.filter.FilterChain) FilterAdapter(com.alibaba.druid.filter.FilterAdapter) MockResultSet(com.alibaba.druid.mock.MockResultSet) ResultSetProxyImpl(com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) NameCallback(javax.security.auth.callback.NameCallback) StatementProxy(com.alibaba.druid.proxy.jdbc.StatementProxy) DruidPasswordCallback(com.alibaba.druid.util.DruidPasswordCallback) ResultSetProxy(com.alibaba.druid.proxy.jdbc.ResultSetProxy)

Example 12 with ResultSetProxy

use of com.alibaba.druid.proxy.jdbc.ResultSetProxy in project druid by alibaba.

the class DruidPooledPreparedStatementTest1 method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setTestOnBorrow(false);
    dataSource.setInitialSize(1);
    dataSource.getProxyFilters().add(new FilterAdapter() {

        @Override
        public boolean preparedStatement_execute(FilterChain chain, PreparedStatementProxy statement) throws SQLException {
            throw new SQLException();
        }

        @Override
        public int preparedStatement_executeUpdate(FilterChain chain, PreparedStatementProxy statement) throws SQLException {
            throw new SQLException();
        }

        public ResultSetProxy preparedStatement_executeQuery(FilterChain chain, PreparedStatementProxy statement) throws SQLException {
            throw new SQLException();
        }

        public void preparedStatement_clearParameters(FilterChain chain, PreparedStatementProxy statement) throws SQLException {
            throw new SQLException();
        }

        @Override
        public int[] statement_executeBatch(FilterChain chain, StatementProxy statement) throws SQLException {
            throw new SQLException();
        }

        @Override
        public ParameterMetaData preparedStatement_getParameterMetaData(FilterChain chain, PreparedStatementProxy statement) throws SQLException {
            throw new SQLException();
        }
    });
}
Also used : SQLException(java.sql.SQLException) FilterChain(com.alibaba.druid.filter.FilterChain) StatementProxy(com.alibaba.druid.proxy.jdbc.StatementProxy) PreparedStatementProxy(com.alibaba.druid.proxy.jdbc.PreparedStatementProxy) FilterAdapter(com.alibaba.druid.filter.FilterAdapter) ResultSetProxy(com.alibaba.druid.proxy.jdbc.ResultSetProxy) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) PreparedStatementProxy(com.alibaba.druid.proxy.jdbc.PreparedStatementProxy) ParameterMetaData(java.sql.ParameterMetaData)

Example 13 with ResultSetProxy

use of com.alibaba.druid.proxy.jdbc.ResultSetProxy in project druid by alibaba.

the class ResultSetProxyImplTest method test_resultset.

public void test_resultset() throws Exception {
    MockDriver driver = new MockDriver();
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setUrl("");
    config.setRawUrl("jdbc:mock:");
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(driver, config);
    {
        StatFilter filter = new StatFilter();
        filter.init(dataSource);
        config.getFilters().add(filter);
    }
    {
        Log4jFilter filter = new Log4jFilter();
        filter.init(dataSource);
        config.getFilters().add(filter);
    }
    Connection conn = dataSource.connect(null);
    conn.setClientInfo("name", null);
    Statement stmt = conn.createStatement();
    ResultSetProxy rs = (ResultSetProxy) stmt.executeQuery(sql);
    rs.insertRow();
    rs.refreshRow();
    rs.moveToInsertRow();
    rs.moveToCurrentRow();
    rs.next();
    rs.updateRef(1, null);
    rs.updateArray(1, null);
    rs.updateRowId(1, null);
    rs.updateNString(1, null);
    rs.updateNClob(1, (NClob) null);
    rs.updateNClob(1, (Reader) null);
    rs.updateNClob(1, (Reader) null, 0);
    rs.updateSQLXML(1, null);
    rs.updateNCharacterStream(1, null);
    rs.updateNCharacterStream(1, null, 0);
    rs.getArray("1");
    rs.updateRef("1", null);
    rs.updateArray("1", null);
    rs.updateRowId("1", null);
    rs.updateNString("1", null);
    rs.updateNClob("1", (NClob) null);
    rs.updateNClob("1", (Reader) null);
    rs.updateNClob("1", (Reader) null, 0);
    rs.updateSQLXML("1", null);
    rs.updateNCharacterStream("1", null);
    rs.updateNCharacterStream("1", null, 0);
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) Statement(java.sql.Statement) Log4jFilter(com.alibaba.druid.filter.logging.Log4jFilter) StatFilter(com.alibaba.druid.filter.stat.StatFilter) Connection(java.sql.Connection) ResultSetProxy(com.alibaba.druid.proxy.jdbc.ResultSetProxy)

Example 14 with ResultSetProxy

use of com.alibaba.druid.proxy.jdbc.ResultSetProxy in project druid by alibaba.

the class CounterFilterTest method test_count_filter.

public void test_count_filter() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setUrl("");
    config.setRawUrl("jdbc:mock:");
    StatFilter filter = new StatFilter();
    MockDriver driver = new MockDriver();
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(driver, config);
    filter.init(dataSource);
    config.getFilters().add(filter);
    Connection conn = dataSource.connect(null);
    Statement stmt = conn.createStatement();
    ResultSetProxy rs = (ResultSetProxy) stmt.executeQuery(sql);
    rs.close();
    stmt.close();
    conn.close();
    conn.close();
    dataSource.getCompositeData();
    dataSource.getProperties();
    dataSource.getDataSourceMBeanDomain();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) Statement(java.sql.Statement) StatFilter(com.alibaba.druid.filter.stat.StatFilter) Connection(java.sql.Connection) ResultSetProxy(com.alibaba.druid.proxy.jdbc.ResultSetProxy)

Example 15 with ResultSetProxy

use of com.alibaba.druid.proxy.jdbc.ResultSetProxy in project druid by alibaba.

the class FilterEventAdapter method preparedStatement_executeQuery.

@Override
public ResultSetProxy preparedStatement_executeQuery(FilterChain chain, PreparedStatementProxy statement) throws SQLException {
    try {
        statementExecuteQueryBefore(statement, statement.getSql());
        ResultSetProxy resultSet = chain.preparedStatement_executeQuery(statement);
        if (resultSet != null) {
            statementExecuteQueryAfter(statement, statement.getSql(), resultSet);
            resultSetOpenAfter(resultSet);
        }
        return resultSet;
    } catch (SQLException error) {
        statement_executeErrorAfter(statement, statement.getSql(), error);
        throw error;
    } catch (RuntimeException error) {
        statement_executeErrorAfter(statement, statement.getSql(), error);
        throw error;
    } catch (Error error) {
        statement_executeErrorAfter(statement, statement.getSql(), error);
        throw error;
    }
}
Also used : SQLException(java.sql.SQLException) ResultSetProxy(com.alibaba.druid.proxy.jdbc.ResultSetProxy)

Aggregations

ResultSetProxy (com.alibaba.druid.proxy.jdbc.ResultSetProxy)29 SQLException (java.sql.SQLException)17 FilterChain (com.alibaba.druid.filter.FilterChain)13 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)13 FilterAdapter (com.alibaba.druid.filter.FilterAdapter)12 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)9 MockResultSet (com.alibaba.druid.mock.MockResultSet)9 ResultSet (java.sql.ResultSet)8 ResultSetProxyImpl (com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl)6 StatementProxy (com.alibaba.druid.proxy.jdbc.StatementProxy)4 DataSourceProxyConfig (com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig)3 DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)3 PreparedStatementProxy (com.alibaba.druid.proxy.jdbc.PreparedStatementProxy)3 StatFilter (com.alibaba.druid.filter.stat.StatFilter)2 MockBlob (com.alibaba.druid.mock.MockBlob)2 MockClob (com.alibaba.druid.mock.MockClob)2 MockDriver (com.alibaba.druid.mock.MockDriver)2 ClobProxyImpl (com.alibaba.druid.proxy.jdbc.ClobProxyImpl)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Connection (java.sql.Connection)2