Search in sources :

Example 16 with StatFilter

use of com.alibaba.druid.filter.stat.StatFilter in project druid by alibaba.

the class ConnectPropertiesChangeTest1 method test_connectPropertiesChange.

public void test_connectPropertiesChange() throws Exception {
    Assert.assertEquals(2, dataSource.getConnectProperties().size());
    Connection conn = dataSource.getConnection();
    conn.close();
    StatFilter filter = dataSource.unwrap(StatFilter.class);
    Assert.assertNotNull(filter);
    Assert.assertFalse(filter.isMergeSql());
    Assert.assertEquals(234, dataSource.getDataSourceStat().getMaxSqlSize());
    dataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.sql.MaxSize=456");
    Assert.assertEquals(456, dataSource.getDataSourceStat().getMaxSqlSize());
    Assert.assertTrue(filter.isMergeSql());
    Assert.assertEquals(2, dataSource.getConnectProperties().size());
    Assert.assertEquals("true", dataSource.getConnectProperties().getProperty("druid.stat.mergeSql"));
    dataSource.setConnectionProperties("druid.stat.mergeSql=false");
    Assert.assertFalse(filter.isMergeSql());
    Assert.assertEquals("false", dataSource.getConnectProperties().getProperty("druid.stat.mergeSql"));
}
Also used : Connection(java.sql.Connection) StatFilter(com.alibaba.druid.filter.stat.StatFilter)

Example 17 with StatFilter

use of com.alibaba.druid.filter.stat.StatFilter in project druid by alibaba.

the class CounterFilterTest method test_countFilter.

public void test_countFilter() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setUrl("");
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(null, config);
    JdbcDataSourceStat dataSourceStat = dataSource.getDataSourceStat();
    StatFilter filter = new StatFilter();
    filter.init(dataSource);
    dataSourceStat.reset();
    Assert.assertNull(StatFilter.getStatFilter(dataSource));
    Assert.assertNull(dataSourceStat.getSqlStat(Integer.MAX_VALUE));
    Assert.assertNull(dataSourceStat.getConnectionStat().getConnectLastTime());
    FilterChain chain = new FilterChainImpl(dataSource) {

        public ConnectionProxy connection_connect(Properties info) throws SQLException {
            throw new SQLException();
        }
    };
    Exception error = null;
    try {
        filter.connection_connect(chain, new Properties());
    } catch (SQLException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
    Assert.assertEquals(1, dataSourceStat.getConnectionStat().getConnectErrorCount());
    Assert.assertNotNull(dataSourceStat.getConnectionStat().getConnectLastTime());
}
Also used : FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) SQLException(java.sql.SQLException) FilterChain(com.alibaba.druid.filter.FilterChain) StatFilter(com.alibaba.druid.filter.stat.StatFilter) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) Properties(java.util.Properties) SQLException(java.sql.SQLException)

Example 18 with StatFilter

use of com.alibaba.druid.filter.stat.StatFilter in project druid by alibaba.

the class StatFilterTest3 method test_dbType_error.

public void test_dbType_error() throws Exception {
    StatFilter filter = new StatFilter();
    filter.setDbType("mysql");
    filter.setMergeSql(true);
    Assert.assertEquals(DbType.mysql, filter.getDbType());
    Assert.assertEquals("sdafawer asf ", filter.mergeSql("sdafawer asf "));
}
Also used : StatFilter(com.alibaba.druid.filter.stat.StatFilter)

Example 19 with StatFilter

use of com.alibaba.druid.filter.stat.StatFilter in project druid by alibaba.

the class StatFilterTest3 method test_merge.

public void test_merge() throws Exception {
    StatFilter filter = new StatFilter();
    filter.setDbType("mysql");
    filter.setMergeSql(false);
    Assert.assertEquals(DbType.mysql, filter.getDbType());
    Assert.assertEquals("select 'x' limit 1", filter.mergeSql("select 'x' limit 1"));
}
Also used : StatFilter(com.alibaba.druid.filter.stat.StatFilter)

Example 20 with StatFilter

use of com.alibaba.druid.filter.stat.StatFilter in project druid by alibaba.

the class StatFilterTest3 method test_merge_pg.

public void test_merge_pg() throws Exception {
    StatFilter filter = new StatFilter();
    filter.setDbType(JdbcConstants.POSTGRESQL);
    filter.setMergeSql(true);
    Assert.assertEquals(JdbcConstants.POSTGRESQL, filter.getDbType());
    Assert.assertEquals("DROP TABLE IF EXISTS test_site_data_select_111;\n" + "CREATE TABLE test_site_data_select_111\n" + "AS\n" + "SELECT *\n" + "FROM postman_trace_info_one\n" + "WHERE lng > ?\n" + "\tAND lat > ?\n" + "\tAND site_id = ?;", filter.mergeSql("drop table if exists test_site_data_select_111; create table test_site_data_select_111 AS select * from postman_trace_info_one  where lng>0 and lat>0  and site_id='17814' ;", JdbcConstants.POSTGRESQL));
}
Also used : StatFilter(com.alibaba.druid.filter.stat.StatFilter)

Aggregations

StatFilter (com.alibaba.druid.filter.stat.StatFilter)22 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)4 Connection (java.sql.Connection)4 DataSourceProxyConfig (com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig)3 DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)3 WallFilter (com.alibaba.druid.wall.WallFilter)3 MockDriver (com.alibaba.druid.mock.MockDriver)2 ResultSetProxy (com.alibaba.druid.proxy.jdbc.ResultSetProxy)2 WebStatFilter (com.alibaba.druid.support.http.WebStatFilter)2 ActiveRecordPlugin (com.jfinal.plugin.activerecord.ActiveRecordPlugin)2 MysqlDialect (com.jfinal.plugin.activerecord.dialect.MysqlDialect)2 DruidPlugin (com.jfinal.plugin.druid.DruidPlugin)2 Statement (java.sql.Statement)2 FilterRegistrationBean (org.springframework.boot.web.servlet.FilterRegistrationBean)2 ServletRegistrationBean (org.springframework.boot.web.servlet.ServletRegistrationBean)2 Bean (org.springframework.context.annotation.Bean)2 Filter (com.alibaba.druid.filter.Filter)1 FilterChain (com.alibaba.druid.filter.FilterChain)1 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)1 Log4jFilter (com.alibaba.druid.filter.logging.Log4jFilter)1