diff --git a/src/main/java/com/alibaba/druid/filter/Filter.java b/src/main/java/com/alibaba/druid/filter/Filter.java index b7ec85617c..c6127f18b0 100644 --- a/src/main/java/com/alibaba/druid/filter/Filter.java +++ b/src/main/java/com/alibaba/druid/filter/Filter.java @@ -57,12 +57,18 @@ import com.alibaba.druid.proxy.jdbc.StatementProxy; /** * @author wenshao */ -public interface Filter { +public interface Filter extends Wrapper { void init(DataSourceProxy dataSource); - + void destory(); + void configFromProperties(Properties properties); + + boolean isWrapperFor(java.lang.Class iface); + + T unwrap(java.lang.Class iface); + ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException; StatementProxy connection_createStatement(FilterChain chain, ConnectionProxy connection) throws SQLException; @@ -1277,6 +1283,7 @@ public interface Filter { void clob_truncate(FilterChain chain, ClobProxy wrapper, long len) throws SQLException; void dataSource_releaseConnection(FilterChain chain, DruidPooledConnection connection) throws SQLException; - - DruidPooledConnection dataSource_getConnection(FilterChain chain, DruidDataSource dataSource, long maxWaitMillis) throws SQLException; + + DruidPooledConnection dataSource_getConnection(FilterChain chain, DruidDataSource dataSource, long maxWaitMillis) + throws SQLException; } diff --git a/src/main/java/com/alibaba/druid/filter/FilterAdapter.java b/src/main/java/com/alibaba/druid/filter/FilterAdapter.java index 4d280bbea6..9869aca93c 100644 --- a/src/main/java/com/alibaba/druid/filter/FilterAdapter.java +++ b/src/main/java/com/alibaba/druid/filter/FilterAdapter.java @@ -67,6 +67,23 @@ public abstract class FilterAdapter extends NotificationBroadcasterSupport imple public void destory() { } + + public void configFromProperties(Properties properties) { + + } + + @Override + public boolean isWrapperFor(Class iface) { + return iface == this.getClass(); + } + + @SuppressWarnings("unchecked") + public T unwrap(Class iface) { + if (iface == this.getClass()) { + return (T) this; + } + return null; + } @Override public Array callableStatement_getArray(FilterChain chain, CallableStatementProxy statement, int parameterIndex) diff --git a/src/main/java/com/alibaba/druid/filter/logging/LogFilter.java b/src/main/java/com/alibaba/druid/filter/logging/LogFilter.java index 043bd006e4..4a7ae894d5 100644 --- a/src/main/java/com/alibaba/druid/filter/logging/LogFilter.java +++ b/src/main/java/com/alibaba/druid/filter/logging/LogFilter.java @@ -412,7 +412,7 @@ public abstract class LogFilter extends FilterEventAdapter implements LogFilterM @Override protected void statementExecuteAfter(StatementProxy statement, String sql, boolean firstResult) { logExecutableSql(statement, sql); - + if (statementExecuteAfterLogEnable && isStatementLogEnabled()) { statement.setLastExecuteTimeNano(); double nanos = statement.getLastExecuteTimeNano(); @@ -436,9 +436,9 @@ public abstract class LogFilter extends FilterEventAdapter implements LogFilterM } else { sql = statement.getBatchSql(); } - + logExecutableSql(statement, sql); - + if (statementExecuteBatchAfterLogEnable && isStatementLogEnabled()) { statement.setLastExecuteTimeNano(); double nanos = statement.getLastExecuteTimeNano(); @@ -460,7 +460,7 @@ public abstract class LogFilter extends FilterEventAdapter implements LogFilterM @Override protected void statementExecuteQueryAfter(StatementProxy statement, String sql, ResultSetProxy resultSet) { logExecutableSql(statement, sql); - + if (statementExecuteQueryAfterLogEnable && isStatementLogEnabled()) { statement.setLastExecuteTimeNano(); double nanos = statement.getLastExecuteTimeNano(); @@ -810,4 +810,18 @@ public abstract class LogFilter extends FilterEventAdapter implements LogFilterM chain.preparedStatement_clearParameters(statement); } + + @Override + public boolean isWrapperFor(Class iface) { + return iface == this.getClass() || iface == LogFilter.class; + } + + @SuppressWarnings("unchecked") + @Override + public T unwrap(Class iface) { + if (iface == this.getClass() || iface == LogFilter.class) { + return (T) this; + } + return null; + } } diff --git a/src/main/java/com/alibaba/druid/filter/stat/MergeStatFilter.java b/src/main/java/com/alibaba/druid/filter/stat/MergeStatFilter.java index 68f100cd00..d661e84a7d 100644 --- a/src/main/java/com/alibaba/druid/filter/stat/MergeStatFilter.java +++ b/src/main/java/com/alibaba/druid/filter/stat/MergeStatFilter.java @@ -16,7 +16,21 @@ package com.alibaba.druid.filter.stat; public class MergeStatFilter extends StatFilter { - public MergeStatFilter() { - super.setMergeSql(true); - } + + public MergeStatFilter(){ + super.setMergeSql(true); + } + + @Override + public boolean isWrapperFor(Class iface) { + return iface == MergeStatFilter.class || iface == StatFilter.class; + } + + @SuppressWarnings("unchecked") + public T unwrap(Class iface) { + if (iface == MergeStatFilter.class || iface == StatFilter.class) { + return (T) this; + } + return null; + } } diff --git a/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java b/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java index 06e15e3f90..ef41aed979 100644 --- a/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java +++ b/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java @@ -156,11 +156,11 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { this.dbType = dataSource.getDbType(); } - initFromProperties(dataSource.getConnectProperties()); - initFromProperties(System.getProperties()); + configFromProperties(dataSource.getConnectProperties()); + configFromProperties(System.getProperties()); } - private void initFromProperties(Properties properties) { + public void configFromProperties(Properties properties) { if (properties == null) { return; } diff --git a/src/main/java/com/alibaba/druid/pool/DruidDataSource.java b/src/main/java/com/alibaba/druid/pool/DruidDataSource.java index 1db4b9488d..4dc28b7f06 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/DruidDataSource.java @@ -46,6 +46,7 @@ import javax.sql.ConnectionEventListener; import javax.sql.ConnectionPoolDataSource; import javax.sql.PooledConnection; +import com.alibaba.druid.Constants; import com.alibaba.druid.TransactionTimeoutException; import com.alibaba.druid.VERSION; import com.alibaba.druid.filter.AutoLoad; @@ -144,8 +145,12 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat public DruidDataSource(boolean fairLock){ super(fairLock); + configFromPropety(System.getProperties()); + } + + public void configFromPropety(Properties properties) { { - String property = System.getProperty("druid.testWhileIdle"); + String property = properties.getProperty("druid.testWhileIdle"); if ("true".equals(property)) { this.setTestWhileIdle(true); } else if ("false".equals(property)) { @@ -153,7 +158,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } } { - String property = System.getProperty("druid.testOnBorrow"); + String property = properties.getProperty("druid.testOnBorrow"); if ("true".equals(property)) { this.setTestOnBorrow(true); } else if ("false".equals(property)) { @@ -161,13 +166,13 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } } { - String property = System.getProperty("druid.validationQuery"); + String property = properties.getProperty("druid.validationQuery"); if (property != null && property.length() > 0) { this.setValidationQuery(property); } } { - String property = System.getProperty("druid.useGloalDataSourceStat"); + String property = properties.getProperty("druid.useGloalDataSourceStat"); if ("true".equals(property)) { this.setUseGloalDataSourceStat(true); } else if ("false".equals(property)) { @@ -175,7 +180,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } } { - String property = System.getProperty("druid.filters"); + String property = properties.getProperty("druid.filters"); if (property != null && property.length() > 0) { try { @@ -186,7 +191,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } } { - String property = System.getProperty("druid.timeBetweenLogStatsMillis"); + String property = properties.getProperty("druid.timeBetweenLogStatsMillis"); if (property != null && property.length() > 0) { try { long value = Long.parseLong(property); @@ -196,6 +201,19 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } } } + { + String property = properties.getProperty(Constants.DRUID_STAT_SQL_MAX_SIZE); + if (property != null && property.length() > 0) { + try { + int value = Integer.parseInt(property); + if (dataSourceStat != null) { + dataSourceStat.setMaxSqlSize(value); + } + } catch (NumberFormatException e) { + LOG.error("illegal property '" + Constants.DRUID_STAT_SQL_MAX_SIZE + "'", e); + } + } + } } public boolean isUseGloalDataSourceStat() { @@ -371,15 +389,42 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } } + @SuppressWarnings("rawtypes") public void setConnectProperties(Properties properties) { if (properties == null) { properties = new Properties(); } - if (inited) { - if (!this.connectProperties.equals(properties)) { + boolean equals; + if (properties.size() == this.connectProperties.size()) { + equals = true; + for (Map.Entry entry : properties.entrySet()) { + Object value = this.connectProperties.get(entry.getKey()); + Object entryValue = entry.getValue(); + if (value == null && entryValue != null) { + equals = false; + break; + } + + if (!value.equals(entry.getValue())) { + equals = false; + break; + } + } + } else { + equals = false; + } + + if (!equals) { + if (inited && LOG.isInfoEnabled()) { LOG.info("connectProperties changed : " + this.connectProperties + " -> " + properties); } + + configFromPropety(properties); + + for (Filter filter : this.filters) { + filter.configFromProperties(properties); + } } this.connectProperties = properties; @@ -499,7 +544,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat dataSourceStat.setDbType(this.getDbType()); } } else { - dataSourceStat = new JdbcDataSourceStat(this.name, this.jdbcUrl, this.dbType); + dataSourceStat = new JdbcDataSourceStat(this.name, this.jdbcUrl, this.dbType, this.connectProperties); } connections = new DruidConnectionHolder[maxActive]; @@ -2188,4 +2233,26 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat public Lock getLock() { return lock; } + + @Override + public boolean isWrapperFor(Class iface) { + for (Filter filter : this.filters) { + if (filter.isWrapperFor(iface)) { + return true; + } + } + + return super.isWrapperFor(iface); + } + + @SuppressWarnings("unchecked") + public T unwrap(Class iface) { + for (Filter filter : this.filters) { + if (filter.isWrapperFor(iface)) { + return (T) filter; + } + } + + return super.unwrap(iface); + } } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java index 28af2c4e5d..f6651c53a9 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java @@ -585,12 +585,16 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV } } + String varName = x.getName(); if (x.isGlobal()) { print("@@global."); } else { - String varName = x.getName(); - if ((!varName.startsWith("@")) && (!varName.equals("?")) && (!varName.startsWith("#")) - && (!varName.startsWith("$"))) { + if ((!varName.startsWith("@")) // / + && (!varName.equals("?")) // + && (!varName.startsWith("#")) // + && (!varName.startsWith("$")) // + && (!varName.startsWith(":")) + ) { print("@@"); } } @@ -3006,7 +3010,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV print(' '); x.getValues().accept(this); } - + if (x.getDataDirectory() != null) { incrementIndent(); println(); @@ -3021,7 +3025,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV x.getIndexDirectory().accept(this); decrementIndent(); } - + return false; } diff --git a/src/main/java/com/alibaba/druid/stat/JdbcDataSourceStat.java b/src/main/java/com/alibaba/druid/stat/JdbcDataSourceStat.java index b34d71c410..e4df1ffa3c 100644 --- a/src/main/java/com/alibaba/druid/stat/JdbcDataSourceStat.java +++ b/src/main/java/com/alibaba/druid/stat/JdbcDataSourceStat.java @@ -39,7 +39,6 @@ import com.alibaba.druid.proxy.jdbc.DataSourceProxy; import com.alibaba.druid.support.logging.Log; import com.alibaba.druid.support.logging.LogFactory; import com.alibaba.druid.util.Histogram; -import com.alibaba.druid.util.LRUCache; public class JdbcDataSourceStat implements JdbcDataSourceStatMBean { @@ -53,11 +52,13 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean { private final JdbcResultSetStat resultSetStat = new JdbcResultSetStat(); private final JdbcStatementStat statementStat = new JdbcStatementStat(); - private int maxSize = 1000 * 1; + private int maxSqlSize = 1000 * 1; private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); private final LinkedHashMap sqlStatMap; + private final AtomicLong skipSqlCount = new AtomicLong(); + private final Histogram connectionHoldHistogram = new Histogram(new long[] { // // 1, 10, 100, 1000, 10 * 1000, // @@ -100,6 +101,7 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean { this(name, url, dbType, null); } + @SuppressWarnings("serial") public JdbcDataSourceStat(String name, String url, String dbType, Properties connectProperties){ this.name = name; this.url = url; @@ -114,14 +116,36 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean { if (arg != null) { try { - maxSize = Integer.parseInt(arg.toString()); + maxSqlSize = Integer.parseInt(arg.toString()); } catch (NumberFormatException ex) { LOG.error("maxSize parse error", ex); } } } - sqlStatMap = new LRUCache(maxSize, 16, 0.75f, false); + sqlStatMap = new LinkedHashMap(16, 0.75f, false) { + + protected boolean removeEldestEntry(Map.Entry eldest) { + boolean remove = (size() > maxSqlSize); + + if (remove) { + JdbcSqlStat sqlStat = eldest.getValue(); + if (sqlStat.getRunningCount() > 0 || sqlStat.getExecuteCount() > 0) { + skipSqlCount.incrementAndGet(); + } + } + + return remove; + } + }; + } + + public int getMaxSqlSize() { + return this.maxSqlSize; + } + + public void setMaxSqlSize(int value) { + this.maxSqlSize = value; } public String getDbType() { @@ -131,6 +155,14 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean { public void setDbType(String dbType) { this.dbType = dbType; } + + public long getSkipSqlCount() { + return skipSqlCount.get(); + } + + public long getSkipSqlCountAndReset() { + return skipSqlCount.getAndSet(0); + } public void reset() { blobOpenCount.set(0); @@ -140,6 +172,7 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean { statementStat.reset(); resultSetStat.reset(); connectionHoldHistogram.reset(); + skipSqlCount.set(0); lock.writeLock().lock(); try { @@ -286,7 +319,7 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean { } finally { lock.writeLock().unlock(); } - + List values = new ArrayList(stats.size()); for (int i = 0; i < stats.size(); ++i) { JdbcSqlStat stat = stats.get(i); @@ -375,7 +408,7 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean { public long getClobOpenCount() { return clobOpenCount.get(); } - + public long getClobOpenCountAndReset() { return clobOpenCount.getAndSet(0); } @@ -387,7 +420,7 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean { public long getBlobOpenCount() { return blobOpenCount.get(); } - + public long getBlobOpenCountAndReset() { return blobOpenCount.getAndSet(0); } diff --git a/src/test/java/com/alibaba/druid/bvt/pool/UnwrapTest2.java b/src/test/java/com/alibaba/druid/bvt/pool/UnwrapTest2.java index 138e36efe5..6cfad6e01f 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/UnwrapTest2.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/UnwrapTest2.java @@ -32,7 +32,7 @@ public class UnwrapTest2 extends TestCase { dataSource.setValidationQueryTimeout(10); dataSource.setQueryTimeout(100); - dataSource.setFilters("log4j"); + dataSource.setFilters("mergeStat"); } protected void tearDown() throws Exception { diff --git a/src/test/java/com/alibaba/druid/bvt/pool/UnwrapTest_filter.java b/src/test/java/com/alibaba/druid/bvt/pool/UnwrapTest_filter.java new file mode 100644 index 0000000000..ed632dbb84 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/UnwrapTest_filter.java @@ -0,0 +1,52 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.Connection; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.filter.logging.Log4jFilter; +import com.alibaba.druid.filter.logging.LogFilter; +import com.alibaba.druid.filter.stat.MergeStatFilter; +import com.alibaba.druid.filter.stat.StatFilter; +import com.alibaba.druid.pool.DruidDataSource; + +public class UnwrapTest_filter extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + + 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.setFilters("mergeStat,log4j"); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void test_unwrap() throws Exception { + Connection conn = dataSource.getConnection(); + conn.close(); + + Assert.assertTrue(dataSource.isWrapperFor(StatFilter.class)); + Assert.assertNotNull(dataSource.unwrap(StatFilter.class)); + + Assert.assertTrue(dataSource.isWrapperFor(MergeStatFilter.class)); + Assert.assertNotNull(dataSource.unwrap(MergeStatFilter.class)); + + Assert.assertTrue(dataSource.isWrapperFor(LogFilter.class)); + Assert.assertNotNull(dataSource.unwrap(LogFilter.class)); + + Assert.assertTrue(dataSource.isWrapperFor(Log4jFilter.class)); + Assert.assertNotNull(dataSource.unwrap(Log4jFilter.class)); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/dynamic/ConnectPropertiesChangeTest.java b/src/test/java/com/alibaba/druid/bvt/pool/dynamic/ConnectPropertiesChangeTest.java index b56f8432ca..98be71e031 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/dynamic/ConnectPropertiesChangeTest.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/dynamic/ConnectPropertiesChangeTest.java @@ -18,9 +18,11 @@ package com.alibaba.druid.bvt.pool.dynamic; import java.lang.reflect.Field; import java.sql.Connection; -import junit.framework.Assert; import junit.framework.TestCase; +import org.junit.Assert; + +import com.alibaba.druid.filter.stat.StatFilter; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.logging.Log; @@ -41,8 +43,9 @@ public class ConnectPropertiesChangeTest extends TestCase { dataSource.setUrl("jdbc:mock:xxx"); dataSource.setTestOnBorrow(false); dataSource.setConnectionProperties("a=3;b=4"); + dataSource.setFilters("stat"); dataSource.init(); - + Assert.assertEquals(1, dataSourceLog.getInfoCount()); } @@ -59,13 +62,37 @@ public class ConnectPropertiesChangeTest extends TestCase { Connection conn = dataSource.getConnection(); conn.close(); + dataSource.setConnectionProperties("a=3;b=4"); + Assert.assertEquals(1, dataSourceLog.getInfoCount()); + dataSource.setConnectionProperties("b=5;c=6"); - Assert.assertEquals(2, dataSourceLog.getInfoCount()); Assert.assertEquals(2, dataSource.getConnectProperties().size()); Assert.assertEquals("5", dataSource.getConnectProperties().getProperty("b")); Assert.assertEquals("6", dataSource.getConnectProperties().getProperty("c")); + + StatFilter filter = dataSource.unwrap(StatFilter.class); + Assert.assertNotNull(filter); + Assert.assertFalse(filter.isMergeSql()); + + dataSource.setConnectionProperties("b=5;c=6;druid.stat.mergeSql=true"); + + Assert.assertTrue(filter.isMergeSql()); + + Assert.assertEquals(3, dataSource.getConnectProperties().size()); + + Assert.assertEquals("true", dataSource.getConnectProperties().getProperty("druid.stat.mergeSql")); + Assert.assertEquals("5", dataSource.getConnectProperties().getProperty("b")); + Assert.assertEquals("6", dataSource.getConnectProperties().getProperty("c")); + + dataSource.setConnectionProperties("b=5;c=6;druid.stat.mergeSql=false"); + + Assert.assertFalse(filter.isMergeSql()); + + Assert.assertEquals("false", dataSource.getConnectProperties().getProperty("druid.stat.mergeSql")); + Assert.assertEquals("5", dataSource.getConnectProperties().getProperty("b")); + Assert.assertEquals("6", dataSource.getConnectProperties().getProperty("c")); } } diff --git a/src/test/java/com/alibaba/druid/bvt/pool/dynamic/ConnectPropertiesChangeTest1.java b/src/test/java/com/alibaba/druid/bvt/pool/dynamic/ConnectPropertiesChangeTest1.java new file mode 100644 index 0000000000..49723864f1 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/dynamic/ConnectPropertiesChangeTest1.java @@ -0,0 +1,81 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.bvt.pool.dynamic; + +import java.lang.reflect.Field; +import java.sql.Connection; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.filter.stat.StatFilter; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.support.logging.Log; + +public class ConnectPropertiesChangeTest1 extends TestCase { + + private DruidDataSource dataSource; + + private Log dataSourceLog; + + protected void setUp() throws Exception { + Field logField = DruidDataSource.class.getDeclaredField("LOG"); + logField.setAccessible(true); + dataSourceLog = (Log) logField.get(null); + + dataSourceLog.resetStat(); + + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setTestOnBorrow(false); + dataSource.setConnectionProperties("druid.filters=stat;druid.stat.sql.MaxSize=234"); + dataSource.init(); + + Assert.assertEquals(1, dataSourceLog.getInfoCount()); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + 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")); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/mysql/MySqlSelectTest_0.java b/src/test/java/com/alibaba/druid/bvt/sql/mysql/MySqlSelectTest_0.java index ab3c9b620e..e384c75e61 100644 --- a/src/test/java/com/alibaba/druid/bvt/sql/mysql/MySqlSelectTest_0.java +++ b/src/test/java/com/alibaba/druid/bvt/sql/mysql/MySqlSelectTest_0.java @@ -17,7 +17,7 @@ package com.alibaba.druid.bvt.sql.mysql; import java.util.List; -import junit.framework.Assert; +import org.junit.Assert; import com.alibaba.druid.sql.MysqlTest; import com.alibaba.druid.sql.ast.SQLStatement; diff --git a/src/test/java/com/alibaba/druid/bvt/sql/mysql/MySqlSelectTest_11.java b/src/test/java/com/alibaba/druid/bvt/sql/mysql/MySqlSelectTest_11.java new file mode 100644 index 0000000000..083e66cedd --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/mysql/MySqlSelectTest_11.java @@ -0,0 +1,68 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.bvt.sql.mysql; + +import java.util.List; + +import org.junit.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.ast.statement.SQLSelect; +import com.alibaba.druid.sql.ast.statement.SQLSelectStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; +import com.alibaba.druid.stat.TableStat; + +public class MySqlSelectTest_11 extends MysqlTest { + + public void test_0() throws Exception { + String sql = "select * from users where uid = :uid"; + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List statementList = parser.parseStatementList(); + SQLStatement stmt = statementList.get(0); + + SQLSelectStatement selectStmt = (SQLSelectStatement) stmt; + + SQLSelect select = selectStmt.getSelect(); + Assert.assertNotNull(select.getQuery()); + MySqlSelectQueryBlock queryBlock = (MySqlSelectQueryBlock) select.getQuery(); + Assert.assertNull(queryBlock.getOrderBy()); + + print(statementList); + + Assert.assertEquals(1, statementList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + stmt.accept(visitor); + + Assert.assertEquals(1, visitor.getTables().size()); + Assert.assertEquals(2, visitor.getColumns().size()); + Assert.assertEquals(1, visitor.getConditions().size()); + Assert.assertEquals(0, visitor.getOrderByColumns().size()); + + Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("users"))); + + String output = SQLUtils.toMySqlString(stmt); + Assert.assertEquals("SELECT *" + // + "\nFROM users" + // + "\nWHERE uid = :uid", // + output); + } +}