diff --git a/src/main/java/com/alibaba/druid/support/ibatis/IbatisUtils.java b/src/main/java/com/alibaba/druid/support/ibatis/IbatisUtils.java index db9d23b7b5..c6cc3ed4bb 100644 --- a/src/main/java/com/alibaba/druid/support/ibatis/IbatisUtils.java +++ b/src/main/java/com/alibaba/druid/support/ibatis/IbatisUtils.java @@ -55,7 +55,7 @@ public class IbatisUtils { * * @return */ - protected static String createId(Object statement) { + protected static String getId(Object statement) { try { if (methodGetId == null) { Class clazz = statement.getClass(); @@ -80,7 +80,7 @@ public class IbatisUtils { * * @return */ - protected static String createResource(Object statement) { + protected static String getResource(Object statement) { try { if (methodGetResource == null) { methodGetResource = statement.getClass().getMethod("getResource"); diff --git a/src/main/java/com/alibaba/druid/support/ibatis/SqlMapClientImplWrapper.java b/src/main/java/com/alibaba/druid/support/ibatis/SqlMapClientImplWrapper.java index a5996ee07b..9c6c89dc2e 100644 --- a/src/main/java/com/alibaba/druid/support/ibatis/SqlMapClientImplWrapper.java +++ b/src/main/java/com/alibaba/druid/support/ibatis/SqlMapClientImplWrapper.java @@ -5,8 +5,12 @@ import java.sql.Connection; import java.sql.SQLException; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import com.alibaba.druid.stat.JdbcSqlStat; import com.ibatis.common.util.PaginatedList; +import com.ibatis.sqlmap.client.SqlMapExecutor; import com.ibatis.sqlmap.client.SqlMapSession; import com.ibatis.sqlmap.client.event.RowHandler; import com.ibatis.sqlmap.engine.execution.BatchException; @@ -16,170 +20,240 @@ import com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl; @SuppressWarnings("deprecation") public class SqlMapClientImplWrapper extends SqlMapClientImpl { - private SqlMapClientImpl raw; + private SqlMapClientImpl raw; - private static Method getLocalSqlMapSessionMethod = null; + private static Method getLocalSqlMapSessionMethod = null; - public SqlMapClientImplWrapper(SqlMapClientImpl raw){ - super(raw.getDelegate()); - this.raw = raw; - } + private ConcurrentMap statementInfoMap = new ConcurrentHashMap(); - protected SqlMapSessionWrapper getLocalSqlMapSessionWrapper() { - try { - if (getLocalSqlMapSessionMethod == null) { - getLocalSqlMapSessionMethod = raw.getClass().getDeclaredMethod("getLocalSqlMapSession"); - getLocalSqlMapSessionMethod.setAccessible(true); - } - SqlMapSessionImpl sessionImpl = (SqlMapSessionImpl) getLocalSqlMapSessionMethod.invoke(raw); - IbatisUtils.set(sessionImpl, this); - return new SqlMapSessionWrapper(raw, sessionImpl); - } catch (Exception e) { - throw new IllegalStateException(e.getMessage(), e); - } - } + public SqlMapClientImplWrapper(SqlMapClientImpl raw) { + super(raw.getDelegate()); + this.raw = raw; + } - public SqlMapSession openSession(Connection conn) { - SqlMapSession session = raw.openSession(conn); - IbatisUtils.setClientImpl(session, this); - return new SqlMapSessionWrapper(raw, session); - } + public void setLocal(String id, SqlMapExecutor executor) { + IbatisStatementInfo stmtInfo = statementInfoMap.get(id); - public SqlMapSession getSession() { - SqlMapSession session = raw.getSession(); - IbatisUtils.setClientImpl(session, this); - return new SqlMapSessionWrapper(raw, session); - } + if (stmtInfo != null) { + JdbcSqlStat.setContextSqlName(stmtInfo.getId()); + JdbcSqlStat.setContextSqlFile(stmtInfo.getResource()); + return; + } - // ///// - public Object insert(String id, Object param) throws SQLException { - return getLocalSqlMapSessionWrapper().insert(id, param); - } + Object statement = null; + if (executor instanceof SqlMapSessionImpl) { + statement = ((SqlMapSessionImpl) executor).getMappedStatement(id); + } - public Object insert(String id) throws SQLException { - return getLocalSqlMapSessionWrapper().insert(id); - } + if (executor instanceof SqlMapClientImpl) { + statement = ((SqlMapClientImpl) executor).getMappedStatement(id); + } - public int update(String id, Object param) throws SQLException { - return getLocalSqlMapSessionWrapper().update(id, param); - } + if (statement == null) { + return; + } - public int update(String id) throws SQLException { - return getLocalSqlMapSessionWrapper().update(id); - } + String stmtId = IbatisUtils.getId(statement); + String stmtResource = IbatisUtils.getResource(statement); + stmtInfo = new IbatisStatementInfo(stmtId, stmtResource); + statementInfoMap.putIfAbsent(id, stmtInfo); + + JdbcSqlStat.setContextSqlName(stmtId); + JdbcSqlStat.setContextSqlFile(stmtResource); + } - public int delete(String id, Object param) throws SQLException { - return getLocalSqlMapSessionWrapper().delete(id, param); - } + protected SqlMapSessionWrapper getLocalSqlMapSessionWrapper() { + try { + if (getLocalSqlMapSessionMethod == null) { + getLocalSqlMapSessionMethod = raw.getClass().getDeclaredMethod( + "getLocalSqlMapSession"); + getLocalSqlMapSessionMethod.setAccessible(true); + } + SqlMapSessionImpl sessionImpl = (SqlMapSessionImpl) getLocalSqlMapSessionMethod + .invoke(raw); + IbatisUtils.set(sessionImpl, this); + return new SqlMapSessionWrapper(raw, sessionImpl); + } catch (Exception e) { + throw new IllegalStateException(e.getMessage(), e); + } + } - public int delete(String id) throws SQLException { - return getLocalSqlMapSessionWrapper().delete(id); - } + public SqlMapSession openSession(Connection conn) { + SqlMapSession session = raw.openSession(conn); + IbatisUtils.setClientImpl(session, this); + return new SqlMapSessionWrapper(raw, session); + } - public Object queryForObject(String id, Object paramObject) throws SQLException { - return getLocalSqlMapSessionWrapper().queryForObject(id, paramObject); - } + public SqlMapSession getSession() { + SqlMapSession session = raw.getSession(); + IbatisUtils.setClientImpl(session, this); + return new SqlMapSessionWrapper(raw, session); + } - public Object queryForObject(String id) throws SQLException { - return getLocalSqlMapSessionWrapper().queryForObject(id); - } + // ///// + public Object insert(String id, Object param) throws SQLException { + return getLocalSqlMapSessionWrapper().insert(id, param); + } - public Object queryForObject(String id, Object paramObject, Object resultObject) throws SQLException { - return getLocalSqlMapSessionWrapper().queryForObject(id, paramObject, resultObject); - } + public Object insert(String id) throws SQLException { + return getLocalSqlMapSessionWrapper().insert(id); + } - @SuppressWarnings("rawtypes") - public List queryForList(String id, Object paramObject) throws SQLException { - return getLocalSqlMapSessionWrapper().queryForList(id, paramObject); - } + public int update(String id, Object param) throws SQLException { + return getLocalSqlMapSessionWrapper().update(id, param); + } - @SuppressWarnings("rawtypes") - public List queryForList(String id) throws SQLException { - return getLocalSqlMapSessionWrapper().queryForList(id); - } + public int update(String id) throws SQLException { + return getLocalSqlMapSessionWrapper().update(id); + } - @SuppressWarnings("rawtypes") - public List queryForList(String id, Object paramObject, int skip, int max) throws SQLException { - return getLocalSqlMapSessionWrapper().queryForList(id, paramObject, skip, max); - } + public int delete(String id, Object param) throws SQLException { + return getLocalSqlMapSessionWrapper().delete(id, param); + } - @SuppressWarnings("rawtypes") - public List queryForList(String id, int skip, int max) throws SQLException { - return getLocalSqlMapSessionWrapper().queryForList(id, skip, max); - } + public int delete(String id) throws SQLException { + return getLocalSqlMapSessionWrapper().delete(id); + } - /** - * @deprecated All paginated list features have been deprecated - */ - public PaginatedList queryForPaginatedList(String id, Object paramObject, int pageSize) throws SQLException { - return getLocalSqlMapSessionWrapper().queryForPaginatedList(id, paramObject, pageSize); - } + public Object queryForObject(String id, Object paramObject) + throws SQLException { + return getLocalSqlMapSessionWrapper().queryForObject(id, paramObject); + } - /** - * @deprecated All paginated list features have been deprecated - */ - public PaginatedList queryForPaginatedList(String id, int pageSize) throws SQLException { - return getLocalSqlMapSessionWrapper().queryForPaginatedList(id, pageSize); - } + public Object queryForObject(String id) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForObject(id); + } - @SuppressWarnings("rawtypes") - public Map queryForMap(String id, Object paramObject, String keyProp) throws SQLException { - return getLocalSqlMapSessionWrapper().queryForMap(id, paramObject, keyProp); - } + public Object queryForObject(String id, Object paramObject, + Object resultObject) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForObject(id, paramObject, + resultObject); + } - @SuppressWarnings("rawtypes") - public Map queryForMap(String id, Object paramObject, String keyProp, String valueProp) throws SQLException { - return getLocalSqlMapSessionWrapper().queryForMap(id, paramObject, keyProp, valueProp); - } + @SuppressWarnings("rawtypes") + public List queryForList(String id, Object paramObject) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForList(id, paramObject); + } - public void queryWithRowHandler(String id, Object paramObject, RowHandler rowHandler) throws SQLException { - getLocalSqlMapSessionWrapper().queryWithRowHandler(id, paramObject, rowHandler); - } + @SuppressWarnings("rawtypes") + public List queryForList(String id) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForList(id); + } - public void queryWithRowHandler(String id, RowHandler rowHandler) throws SQLException { - getLocalSqlMapSessionWrapper().queryWithRowHandler(id, rowHandler); - } + @SuppressWarnings("rawtypes") + public List queryForList(String id, Object paramObject, int skip, int max) + throws SQLException { + return getLocalSqlMapSessionWrapper().queryForList(id, paramObject, + skip, max); + } - public void startTransaction() throws SQLException { - getLocalSqlMapSessionWrapper().startTransaction(); - } + @SuppressWarnings("rawtypes") + public List queryForList(String id, int skip, int max) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForList(id, skip, max); + } - public void startTransaction(int transactionIsolation) throws SQLException { - getLocalSqlMapSessionWrapper().startTransaction(transactionIsolation); - } + /** + * @deprecated All paginated list features have been deprecated + */ + public PaginatedList queryForPaginatedList(String id, Object paramObject, + int pageSize) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForPaginatedList(id, + paramObject, pageSize); + } - public void commitTransaction() throws SQLException { - getLocalSqlMapSessionWrapper().commitTransaction(); - } + /** + * @deprecated All paginated list features have been deprecated + */ + public PaginatedList queryForPaginatedList(String id, int pageSize) + throws SQLException { + return getLocalSqlMapSessionWrapper().queryForPaginatedList(id, + pageSize); + } - public void endTransaction() throws SQLException { - try { - getLocalSqlMapSessionWrapper().endTransaction(); - } finally { - getLocalSqlMapSessionWrapper().close(); - } - } + @SuppressWarnings("rawtypes") + public Map queryForMap(String id, Object paramObject, String keyProp) + throws SQLException { + return getLocalSqlMapSessionWrapper().queryForMap(id, paramObject, + keyProp); + } - public void startBatch() throws SQLException { - getLocalSqlMapSessionWrapper().startBatch(); - } + @SuppressWarnings("rawtypes") + public Map queryForMap(String id, Object paramObject, String keyProp, + String valueProp) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForMap(id, paramObject, + keyProp, valueProp); + } - public int executeBatch() throws SQLException { - return getLocalSqlMapSessionWrapper().executeBatch(); - } + public void queryWithRowHandler(String id, Object paramObject, + RowHandler rowHandler) throws SQLException { + getLocalSqlMapSessionWrapper().queryWithRowHandler(id, paramObject, + rowHandler); + } - @SuppressWarnings("rawtypes") - public List executeBatchDetailed() throws SQLException, BatchException { - return getLocalSqlMapSessionWrapper().executeBatchDetailed(); - } + public void queryWithRowHandler(String id, RowHandler rowHandler) + throws SQLException { + getLocalSqlMapSessionWrapper().queryWithRowHandler(id, rowHandler); + } - public void setUserConnection(Connection connection) throws SQLException { - try { - getLocalSqlMapSessionWrapper().setUserConnection(connection); - } finally { - if (connection == null) { - getLocalSqlMapSessionWrapper().close(); - } - } - } + public void startTransaction() throws SQLException { + getLocalSqlMapSessionWrapper().startTransaction(); + } + + public void startTransaction(int transactionIsolation) throws SQLException { + getLocalSqlMapSessionWrapper().startTransaction(transactionIsolation); + } + + public void commitTransaction() throws SQLException { + getLocalSqlMapSessionWrapper().commitTransaction(); + } + + public void endTransaction() throws SQLException { + try { + getLocalSqlMapSessionWrapper().endTransaction(); + } finally { + getLocalSqlMapSessionWrapper().close(); + } + } + + public void startBatch() throws SQLException { + getLocalSqlMapSessionWrapper().startBatch(); + } + + public int executeBatch() throws SQLException { + return getLocalSqlMapSessionWrapper().executeBatch(); + } + + @SuppressWarnings("rawtypes") + public List executeBatchDetailed() throws SQLException, BatchException { + return getLocalSqlMapSessionWrapper().executeBatchDetailed(); + } + + public void setUserConnection(Connection connection) throws SQLException { + try { + getLocalSqlMapSessionWrapper().setUserConnection(connection); + } finally { + if (connection == null) { + getLocalSqlMapSessionWrapper().close(); + } + } + } + + public static class IbatisStatementInfo { + private final String id; + private final String resource; + + public IbatisStatementInfo(String id, String resource) { + this.id = id; + this.resource = resource; + } + + public String getId() { + return id; + } + + public String getResource() { + return resource; + } + + } } diff --git a/src/main/java/com/alibaba/druid/support/ibatis/SqlMapExecutorWrapper.java b/src/main/java/com/alibaba/druid/support/ibatis/SqlMapExecutorWrapper.java index f185c17747..f53af2534a 100644 --- a/src/main/java/com/alibaba/druid/support/ibatis/SqlMapExecutorWrapper.java +++ b/src/main/java/com/alibaba/druid/support/ibatis/SqlMapExecutorWrapper.java @@ -27,22 +27,26 @@ public class SqlMapExecutorWrapper implements SqlMapExecutor { } public Object insert(String id, Object parameterObject) throws SQLException { + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); return executor.insert(id, parameterObject); } public Object insert(String id) throws SQLException { + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); return executor.insert(id); } public int update(String id, Object parameterObject) throws SQLException { + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); int effectedRowCount = executor.update(id, parameterObject); return effectedRowCount; } public int update(String id) throws SQLException { + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); int effectedRowCount = executor.update(id); @@ -50,7 +54,7 @@ public class SqlMapExecutorWrapper implements SqlMapExecutor { } public int delete(String id, Object parameterObject) throws SQLException { - + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); int effectedRowCount = executor.delete(id, parameterObject); @@ -58,7 +62,7 @@ public class SqlMapExecutorWrapper implements SqlMapExecutor { } public int delete(String id) throws SQLException { - + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); int effectedRowCount = executor.delete(id); @@ -66,6 +70,7 @@ public class SqlMapExecutorWrapper implements SqlMapExecutor { } public Object queryForObject(String id, Object parameterObject) throws SQLException { + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); Object object = executor.queryForObject(id, parameterObject); @@ -73,7 +78,7 @@ public class SqlMapExecutorWrapper implements SqlMapExecutor { } public Object queryForObject(String id) throws SQLException { - + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); Object object = executor.queryForObject(id); @@ -81,7 +86,7 @@ public class SqlMapExecutorWrapper implements SqlMapExecutor { } public Object queryForObject(String id, Object parameterObject, Object resultObject) throws SQLException { - + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); Object object = executor.queryForObject(id, parameterObject, resultObject); @@ -90,7 +95,7 @@ public class SqlMapExecutorWrapper implements SqlMapExecutor { @SuppressWarnings("rawtypes") public List queryForList(String id, Object parameterObject) throws SQLException { - + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); List list = executor.queryForList(id, parameterObject); @@ -99,7 +104,7 @@ public class SqlMapExecutorWrapper implements SqlMapExecutor { @SuppressWarnings("rawtypes") public List queryForList(String id) throws SQLException { - + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); List list = executor.queryForList(id); @@ -108,6 +113,7 @@ public class SqlMapExecutorWrapper implements SqlMapExecutor { @SuppressWarnings("rawtypes") public List queryForList(String id, Object parameterObject, int skip, int max) throws SQLException { + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); List list = executor.queryForList(id, parameterObject, skip, max); @@ -116,7 +122,7 @@ public class SqlMapExecutorWrapper implements SqlMapExecutor { @SuppressWarnings("rawtypes") public List queryForList(String id, int skip, int max) throws SQLException { - + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); List list = executor.queryForList(id, skip, max); @@ -124,17 +130,19 @@ public class SqlMapExecutorWrapper implements SqlMapExecutor { } public void queryWithRowHandler(String id, Object parameterObject, RowHandler rowHandler) throws SQLException { + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); executor.queryWithRowHandler(id, parameterObject, rowHandler); } public void queryWithRowHandler(String id, RowHandler rowHandler) throws SQLException { + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); executor.queryWithRowHandler(id, rowHandler); } public PaginatedList queryForPaginatedList(String id, Object parameterObject, int pageSize) throws SQLException { - + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); PaginatedList list = executor.queryForPaginatedList(id, parameterObject, pageSize); @@ -142,6 +150,7 @@ public class SqlMapExecutorWrapper implements SqlMapExecutor { } public PaginatedList queryForPaginatedList(String id, int pageSize) throws SQLException { + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); PaginatedList list = executor.queryForPaginatedList(id, pageSize); @@ -150,7 +159,7 @@ public class SqlMapExecutorWrapper implements SqlMapExecutor { @SuppressWarnings("rawtypes") public Map queryForMap(String id, Object parameterObject, String keyProp) throws SQLException { - + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); Map map = executor.queryForMap(id, parameterObject, keyProp); @@ -159,7 +168,7 @@ public class SqlMapExecutorWrapper implements SqlMapExecutor { @SuppressWarnings("rawtypes") public Map queryForMap(String id, Object parameterObject, String keyProp, String valueProp) throws SQLException { - + clientImplWrapper.setLocal(id, executor); IbatisUtils.setClientImpl(executor, clientImplWrapper); Map map = executor.queryForMap(id, parameterObject, keyProp, valueProp); diff --git a/src/main/java/com/alibaba/druid/wall/spi/WallVisitorUtils.java b/src/main/java/com/alibaba/druid/wall/spi/WallVisitorUtils.java index 7236ac488f..1bc9eb4e56 100644 --- a/src/main/java/com/alibaba/druid/wall/spi/WallVisitorUtils.java +++ b/src/main/java/com/alibaba/druid/wall/spi/WallVisitorUtils.java @@ -21,7 +21,9 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.math.BigDecimal; import java.net.URL; +import java.util.ArrayList; import java.util.Enumeration; +import java.util.List; import java.util.Set; import com.alibaba.druid.sql.ast.SQLExpr; @@ -245,16 +247,45 @@ public class WallVisitorUtils { } } } + + if (x.getOperator() == SQLBinaryOperator.BooleanOr) { + List groupList = new ArrayList(); + SQLExpr left = x.getLeft(); + for (;;) { + if (left instanceof SQLBinaryOpExpr && ((SQLBinaryOpExpr) left).getOperator() == x.getOperator()) { + SQLBinaryOpExpr binaryLeft = (SQLBinaryOpExpr) left; + groupList.add(binaryLeft.getRight()); + left = binaryLeft.getLeft(); + } else { + groupList.add(left); + break; + } + } + groupList.add(x.getRight()); + + boolean allFalse = true; + for (int i = groupList.size() - 1; i >= 0; --i) { + Object result = getValue(groupList.get(i)); + if (Boolean.TRUE == result) { + return true; + } + + if (Boolean.FALSE != result) { + allFalse = false; + } + } + + if (allFalse) { + return false; + } + + return null; + } + Object leftResult = getValue(x.getLeft()); Object rightResult = getValue(x.getRight()); - if (x.getOperator() == SQLBinaryOperator.BooleanOr) { - if (Boolean.TRUE == leftResult || Boolean.TRUE == rightResult) { - return true; - } - } - if (x.getOperator() == SQLBinaryOperator.BooleanAnd) { if (Boolean.FALSE == leftResult || Boolean.FALSE == rightResult) { return false; diff --git a/src/test/java/com/alibaba/druid/bvt/filter/wall/WallVisitorUtilsLargeOrTest.java b/src/test/java/com/alibaba/druid/bvt/filter/wall/WallVisitorUtilsLargeOrTest.java new file mode 100644 index 0000000000..d8cf189324 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/wall/WallVisitorUtilsLargeOrTest.java @@ -0,0 +1,19 @@ +package com.alibaba.druid.bvt.filter.wall; + +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.wall.spi.WallVisitorUtils; + +import junit.framework.Assert; +import junit.framework.TestCase; + +public class WallVisitorUtilsLargeOrTest extends TestCase { + public void test_largeOr() throws Exception { + StringBuilder buf = new StringBuilder(); + buf.append("ID = 1"); + for (int i = 2; i <= 1000 * 10; ++i) { + buf.append(" OR ID = " + i); + } + + Assert.assertEquals(null, WallVisitorUtils.getValue(SQLUtils.toSQLExpr(buf.toString()))); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/postgresql/PGSelectTest5.java b/src/test/java/com/alibaba/druid/bvt/sql/postgresql/PGSelectTest5.java new file mode 100644 index 0000000000..7ccc21c51f --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/postgresql/PGSelectTest5.java @@ -0,0 +1,56 @@ +/* + * 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.postgresql; + +import java.util.List; + +import junit.framework.Assert; + +import com.alibaba.druid.sql.PGTest; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser; +import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor; + +public class PGSelectTest5 extends PGTest { + + public void test_0() throws Exception { + String sql = " SELECT rs.* FROM ( WITH RECURSIVE r AS( SELECT * FROM t_e_shopcatalog WHERE parentcatalogid= (SELECT catalogid FROM t_e_shopcatalog where catalogname='学习分类' and parentcatalogid='0') UNION ALL SELECT t_e_shopcatalog.* FROM t_e_shopcatalog, r WHERE t_e_shopcatalog.parentcatalogid = r.catalogid )SELECT * FROM r )rs WHERE 1=1"; + + PGSQLStatementParser parser = new PGSQLStatementParser(sql); + List statementList = parser.parseStatementList(); + SQLStatement statemen = statementList.get(0); + print(statementList); + + Assert.assertEquals(1, statementList.size()); + + PGSchemaStatVisitor visitor = new PGSchemaStatVisitor(); + statemen.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getColumns()); + System.out.println("coditions : " + visitor.getConditions()); + + Assert.assertEquals(2, visitor.getColumns().size()); + Assert.assertEquals(0, visitor.getTables().size()); + } + +} + +// select categoryId , offerIds from cnres.function_select_get_spt_p4p_offer_list (' 1031918 , 1031919 , 1037004 ') as +// a(categoryId numeric,offerIds character varying(4000)) +// select memberId , offerIds from cnres.function_select_get_seller_hot_offer_list('\'gzyyd168\'') as a(memberId +// character varying(20),offerIds character varying(4000)) + diff --git a/src/test/java/com/alibaba/druid/bvt/sql/sqlserver/SQLServerTopTest2.java b/src/test/java/com/alibaba/druid/bvt/sql/sqlserver/SQLServerTopTest2.java new file mode 100644 index 0000000000..05c9c05a14 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/sqlserver/SQLServerTopTest2.java @@ -0,0 +1,41 @@ +/* + * 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.sqlserver; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.druid.sql.ast.statement.SQLSelectStatement; +import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser; +import com.alibaba.druid.sql.test.TestUtils; + +public class SQLServerTopTest2 extends TestCase { + + public void test_isEmpty() throws Exception { + String sql = "SELECT TOP ? * FROM T"; + + String expect = "SELECT TOP ? *\nFROM T"; + + SQLServerStatementParser parser = new SQLServerStatementParser(sql); + SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatementList().get(0); + + String text = TestUtils.outputSqlServer(stmt); + + Assert.assertEquals(expect, text); + + System.out.println(text); + } +} diff --git a/src/test/resources/com/alibaba/druid/pool/ibatis/spring-config-ibatis.xml b/src/test/resources/com/alibaba/druid/pool/ibatis/spring-config-ibatis.xml index ea987687cb..cd295f98c0 100644 --- a/src/test/resources/com/alibaba/druid/pool/ibatis/spring-config-ibatis.xml +++ b/src/test/resources/com/alibaba/druid/pool/ibatis/spring-config-ibatis.xml @@ -7,6 +7,9 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schem http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" default-lazy-init="false"> + + + diff --git a/src/test/resources/com/alibaba/druid/pool/mybatis/spring-config-mybatis.xml b/src/test/resources/com/alibaba/druid/pool/mybatis/spring-config-mybatis.xml index efcab2a65d..bc802baddd 100644 --- a/src/test/resources/com/alibaba/druid/pool/mybatis/spring-config-mybatis.xml +++ b/src/test/resources/com/alibaba/druid/pool/mybatis/spring-config-mybatis.xml @@ -18,9 +18,5 @@ http://www.springframework.org/schema/aop http://www.springframework.org/schema/ - - - -