From 4223e1291affb450d29b8ed13bf1fe23d320c93f Mon Sep 17 00:00:00 2001 From: taochen Date: Mon, 21 Oct 2019 11:33:46 +0800 Subject: [PATCH 1/3] modify multipleSqlBuilder --- .../dao/sqlbuilder/MultipleSqlBuilder.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/dal-client/src/main/java/com/ctrip/platform/dal/dao/sqlbuilder/MultipleSqlBuilder.java b/dal-client/src/main/java/com/ctrip/platform/dal/dao/sqlbuilder/MultipleSqlBuilder.java index c970a6741..491586bf2 100644 --- a/dal-client/src/main/java/com/ctrip/platform/dal/dao/sqlbuilder/MultipleSqlBuilder.java +++ b/dal-client/src/main/java/com/ctrip/platform/dal/dao/sqlbuilder/MultipleSqlBuilder.java @@ -29,6 +29,21 @@ public MultipleSqlBuilder addQuery(String sql, StatementParameters parameter mergers.add(merger); return this; } + + public MultipleSqlBuilder addQuery(String sqlStr, StatementParameters parameters) { + String[] sqls = sqlStr.split(";"); + if (sqls.length > 1) { + for (int i = 0; i < sqls.length; ++i) { + StatementParameters statementParameters = new StatementParameters(); + statementParameters.add(parameters.get(i)); + queryUnits.add(new QueryUnit(sqls[i], statementParameters, null)); + } + } + else { + queryUnits.add(new QueryUnit(sqlStr, parameters, null)); + } + return this; + } /** * This mapper instance maybe used in more than one thread. Make sure it is thread safe. @@ -112,8 +127,11 @@ public MultipleSqlBuilder addQuery(String sql, StatementParameters parameters, D public List> getExtractors() { List> extractors = new ArrayList<>(); - for(QueryUnit unit: queryUnits) - extractors.add(unit.extractor); + for(QueryUnit unit: queryUnits) { + if (unit.extractor != null) { + extractors.add(unit.extractor); + } + } return extractors; } From 2a47f38b72ed1ec3198b5c61e47eb9d7020b5e8c Mon Sep 17 00:00:00 2001 From: taochen Date: Wed, 20 Nov 2019 12:15:13 +0800 Subject: [PATCH 2/3] add unit test of used local variable in multipleSql --- .../dal/dao/shard/DalQueryDaoMySqlTest.java | 44 +++++++++++++++++ .../dal/dao/shard/DalQueryDaoSqlSvrTest.java | 47 +++++++++++++++++++ .../dal/dao/shard/DalQueryDaoTest.java | 20 +++++++- .../dao/shard/MyDalResultSetExtractor.java | 16 +++++++ 4 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/MyDalResultSetExtractor.java diff --git a/dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/DalQueryDaoMySqlTest.java b/dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/DalQueryDaoMySqlTest.java index d373c3fdc..1892e991e 100644 --- a/dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/DalQueryDaoMySqlTest.java +++ b/dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/DalQueryDaoMySqlTest.java @@ -5,14 +5,20 @@ import com.ctrip.platform.dal.dao.DalClientFactory; import com.ctrip.platform.dal.dao.DalHints; import com.ctrip.platform.dal.dao.StatementParameters; +import com.ctrip.platform.dal.dao.helper.DalListMerger; +import com.ctrip.platform.dal.dao.helper.DalObjectRowMapper; +import com.ctrip.platform.dal.dao.sqlbuilder.MultipleSqlBuilder; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import java.sql.SQLException; +import java.sql.Types; +import java.util.List; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; public class DalQueryDaoMySqlTest extends DalQueryDaoTest { @@ -108,4 +114,42 @@ public void insertBack() { fail(); } } + + @Override + public List queryMultipleAllShardsUseLocalVariable(DalHints hints) throws SQLException { + String[] sqls = new String[4]; + sqls[0] = "SET @id = ?,@type = ?"; + sqls[1] = "select id from " + TABLE_NAME + " where id = @id and type = @type"; + sqls[2] = "select type from " + TABLE_NAME + " where id = @id and type = @type"; + sqls[3] = "select quantity from " + TABLE_NAME + " where id = @id and type = @type"; + int i = 0; + try { + MultipleSqlBuilder multipleSqlBuilder = new MultipleSqlBuilder(); + for (String sql : sqls) { + ++i; + if (i == 1) { + StatementParameters parameters = new StatementParameters(); + parameters.set(1, Types.INTEGER,1); + parameters.set(2, Types.INTEGER,0); + multipleSqlBuilder.addQuery(sql, parameters, new MyDalResultSetExtractor(), new DalListMerger<>()); + } + else { + multipleSqlBuilder.addQuery(sql, new StatementParameters(), new DalObjectRowMapper()); + } + } + return dao.query(multipleSqlBuilder, hints.inAllShards()); + + } catch (Exception e) { + throw new SQLException(); + } + } + + @Override + public void assertMultipleResult1(List list) { + assertEquals(4, list.size()); + assertEquals(0, ((List)list.get(0)).size()); + assertEquals(1, ((List)list.get(1)).size()); + assertEquals(1, ((List)list.get(2)).size()); + assertEquals(1, ((List)list.get(3)).size()); + } } diff --git a/dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/DalQueryDaoSqlSvrTest.java b/dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/DalQueryDaoSqlSvrTest.java index 342c99f56..000f735e4 100644 --- a/dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/DalQueryDaoSqlSvrTest.java +++ b/dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/DalQueryDaoSqlSvrTest.java @@ -5,11 +5,18 @@ import com.ctrip.platform.dal.dao.DalClientFactory; import com.ctrip.platform.dal.dao.DalHints; import com.ctrip.platform.dal.dao.StatementParameters; +import com.ctrip.platform.dal.dao.helper.DalObjectRowMapper; +import com.ctrip.platform.dal.dao.sqlbuilder.MultipleSqlBuilder; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; +import java.sql.SQLException; +import java.sql.Types; +import java.util.List; + +import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; public class DalQueryDaoSqlSvrTest extends DalQueryDaoTest { @@ -119,4 +126,44 @@ public void insertBack() { } } + @Override + public List queryMultipleAllShardsUseLocalVariable(DalHints hints) throws SQLException { + String declareSql = "DECLARE @id int,@type int"; + String[] sqls = new String[4]; + //这其实是两个sql,用;分隔开,方便参数化设置 + sqls[0] = "SET @id = ?;SET @type = ?"; + sqls[1] = "select id from " + TABLE_NAME + " where id = @id and type = @type"; + sqls[2] = "select type from " + TABLE_NAME + " where id = @id and type = @type"; + sqls[3] = "select quantity from " + TABLE_NAME + " where id = @id and type = @type"; + + int i = 0; + try { + MultipleSqlBuilder multipleSqlBuilder = new MultipleSqlBuilder(); + //针对sqlserver 需要先声明变量,才能set + multipleSqlBuilder.addQuery(declareSql, new StatementParameters()); + for (String sql : sqls) { + ++i; + if (i == 1) { + StatementParameters parameters = new StatementParameters(); + parameters.set(1, Types.INTEGER,1); + parameters.set(2, Types.INTEGER,0); + multipleSqlBuilder.addQuery(sql, parameters); + } + else { + multipleSqlBuilder.addQuery(sql, new StatementParameters(), new DalObjectRowMapper()); + } + } + return dao.query(multipleSqlBuilder, hints.inAllShards()); + } catch (Exception e) { + throw new SQLException(); + } + } + + @Override + public void assertMultipleResult1(List list) { + assertEquals(3, list.size()); + assertEquals(1, ((List)list.get(0)).size()); + assertEquals(1, ((List)list.get(1)).size()); + assertEquals(1, ((List)list.get(2)).size()); + } } diff --git a/dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/DalQueryDaoTest.java b/dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/DalQueryDaoTest.java index 79a568ad8..3cb5a361c 100644 --- a/dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/DalQueryDaoTest.java +++ b/dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/DalQueryDaoTest.java @@ -30,7 +30,7 @@ public abstract class DalQueryDaoTest { private String logicDbName; private DatabaseCategory dbCategory; - private DalQueryDao dao; + protected DalQueryDao dao; private final static String TABLE_NAME = "dal_client_test"; public DalQueryDaoTest(String DATABASE_NAME, DatabaseCategory dbCategory){ @@ -634,6 +634,10 @@ private List queryMultipleAllShards(DalHints hints) throws SQLException { return dao.query(builder, hints.inAllShards()); } + public List queryMultipleAllShardsUseLocalVariable(DalHints hints) throws SQLException { + return null; + } + @Test public void testQueryMultipleAllShards() { if(dbCategory == DatabaseCategory.Oracle) @@ -641,8 +645,9 @@ public void testQueryMultipleAllShards() { try { List list = queryMultipleAllShards(new DalHints()); - + List list1 = queryMultipleAllShardsUseLocalVariable(new DalHints()); assertMultipleResult(list); + assertMultipleResult1(list1); } catch (Exception e) { e.printStackTrace(); fail(); @@ -658,9 +663,16 @@ public void testQueryMultipleAllShardsAsync() { DalHints hints = new DalHints(); List list = queryMultipleAllShards(hints.asyncExecution()); + DalHints hints1 = new DalHints(); + List list1 = queryMultipleAllShardsUseLocalVariable(hints1.asyncExecution()); + assertNull(list); + assertNull(list1); + list = (List)hints.getAsyncResult().get(); + list1 = (List)hints1.getAsyncResult().get(); assertMultipleResult(list); + assertMultipleResult1(list1); } catch (Exception e) { e.printStackTrace(); @@ -680,6 +692,10 @@ private void assertMultipleResult(List list) { assertEquals(6, ((List)list.get(7)).size()); } + public void assertMultipleResult1(List list) { + return; + } + @Test public void testQueryListAllShardsWithCallback() { try { diff --git a/dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/MyDalResultSetExtractor.java b/dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/MyDalResultSetExtractor.java new file mode 100644 index 000000000..249655fd5 --- /dev/null +++ b/dal-client/src/test/java/com/ctrip/platform/dal/dao/shard/MyDalResultSetExtractor.java @@ -0,0 +1,16 @@ +package com.ctrip.platform.dal.dao.shard; + +import com.ctrip.platform.dal.dao.DalResultSetExtractor; + +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * Created by taochen on 2019/10/15. + */ +public class MyDalResultSetExtractor implements DalResultSetExtractor { + @Override + public Object extract(ResultSet resultSet) throws SQLException { + return null; + } +} From acb0b2432d4fc3149c819a4a22653a7b5c20ed11 Mon Sep 17 00:00:00 2001 From: taochen Date: Thu, 21 Nov 2019 14:57:54 +0800 Subject: [PATCH 3/3] update dal to 1.18.5 --- dal-client/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dal-client/pom.xml b/dal-client/pom.xml index 35b43736a..4e5808485 100644 --- a/dal-client/pom.xml +++ b/dal-client/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.ctrip.platform dal-client - 1.18.4 + 1.18.5 ${project.version}