Skip to content

Commit

Permalink
merge from master
Browse files Browse the repository at this point in the history
  • Loading branch information
c7ch23en committed Nov 25, 2019
2 parents 505ecd2 + acb0b24 commit 9e163b7
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 6 deletions.
3 changes: 2 additions & 1 deletion dal-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.ctrip.platform</groupId>
<artifactId>dal-client</artifactId>
<version>2.0.2</version>
<version>2.0.3</version>

<properties>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ public <T> MultipleSqlBuilder addQuery(String sql, StatementParameters parameter
mergers.add(merger);
return this;
}

public <T> 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.
Expand Down Expand Up @@ -112,8 +127,11 @@ public MultipleSqlBuilder addQuery(String sql, StatementParameters parameters, D

public List<DalResultSetExtractor<?>> getExtractors() {
List<DalResultSetExtractor<?>> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Object>());
}
}
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Object>());
}
}
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -634,15 +634,20 @@ 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)
return;

try {
List list = queryMultipleAllShards(new DalHints());

List list1 = queryMultipleAllShardsUseLocalVariable(new DalHints());
assertMultipleResult(list);
assertMultipleResult1(list1);
} catch (Exception e) {
e.printStackTrace();
fail();
Expand All @@ -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();
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion dal-cluster-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.ctrip.framework.dal</groupId>
<artifactId>dal-cluster-client</artifactId>
<version>2.0.2</version>
<version>2.0.3</version>
<!-- TODO: mvn package sources & javadoc -->
<properties>
<java.version>1.8</java.version>
Expand Down

0 comments on commit 9e163b7

Please sign in to comment.