Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix 1348 #1499

Merged
merged 4 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public SQLRouteResult route(final String logicSQL, final List<Object> parameters
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, logicSQL, databaseType, sqlStatement, shardingConditions, parameters);
boolean isSingleRouting = routingResult.isSingleRouting();
if (sqlStatement instanceof SelectStatement && null != ((SelectStatement) sqlStatement).getLimit()) {
processLimit(parameters, (SelectStatement) sqlStatement, isSingleRouting);
processLimit(parameters, (SelectStatement) sqlStatement);
}
SQLBuilder sqlBuilder = rewriteEngine.rewrite(!isSingleRouting);
for (TableUnit each : routingResult.getTableUnits().getTableUnits()) {
Expand Down Expand Up @@ -195,11 +195,7 @@ private void setGeneratedKeys(final SQLRouteResult sqlRouteResult, final Generat
sqlRouteResult.getGeneratedKey().getGeneratedKeys().addAll(generatedKeys);
}

private void processLimit(final List<Object> parameters, final SelectStatement selectStatement, final boolean isSingleRouting) {
if (isSingleRouting) {
selectStatement.setLimit(null);
return;
}
private void processLimit(final List<Object> parameters, final SelectStatement selectStatement) {
boolean isNeedFetchAll = (!selectStatement.getGroupByItems().isEmpty() || !selectStatement.getAggregationSelectItems().isEmpty()) && !selectStatement.isSameGroupByAndOrderByItems();
selectStatement.getLimit().processParameters(parameters, isNeedFetchAll);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import io.shardingsphere.api.HintManager;
import io.shardingsphere.api.config.ShardingRuleConfiguration;
import io.shardingsphere.api.config.TableRuleConfiguration;
import io.shardingsphere.api.config.strategy.HintShardingStrategyConfiguration;
import io.shardingsphere.api.config.strategy.InlineShardingStrategyConfiguration;
import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.fixture.OrderDatabaseHintShardingAlgorithm;
import io.shardingsphere.core.parsing.parser.sql.dql.select.SelectStatement;
import io.shardingsphere.core.rule.ShardingRule;
import org.junit.Before;
import org.junit.Test;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.*;

import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
Expand Down Expand Up @@ -92,6 +92,52 @@ public String apply(final RouteUnit input) {
});
assertThat(originSql, is(actualSQLs.iterator().next()));
}

@Test
public void assertDatabaseSelectSQLPagination() {
String originSql = "select user_id from tbl_pagination limit 0,5";
SQLRouteResult actual = new StatementRoutingEngine(shardingRule, null, DatabaseType.MySQL, false, null).route(originSql);
SelectStatement stmt = (SelectStatement) actual.getSqlStatement();
assertThat(stmt.getLimit().getOffsetValue() , is(0));
assertThat(stmt.getLimit().getRowCountValue() ,is(5));

originSql = "select user_id from tbl_pagination limit 5,5";
actual = new StatementRoutingEngine(shardingRule, null, DatabaseType.MySQL, false, null).route(originSql);
stmt = (SelectStatement) actual.getSqlStatement();
assertThat(stmt.getLimit().getOffsetValue() , is(5));
assertThat(stmt.getLimit().getRowCountValue() ,is(5));
}

@Test
public void assertDatabasePrepareSelectSQLPagination() {
final String SHARDING_PREFIX = "userdb";
final String SHARDING_TABLE = "user";
Map<String, DataSource> dataSourceMap = new HashMap<>();
dataSourceMap.put(SHARDING_PREFIX + "1", null);
dataSourceMap.put(SHARDING_PREFIX + "2", null);
TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
tableRuleConfig.setLogicTable(SHARDING_TABLE);
tableRuleConfig.setActualDataNodes(SHARDING_PREFIX + "${1..2}." + SHARDING_TABLE);
tableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("city_id", SHARDING_PREFIX+"${city_id % 2 + 1}"));
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
ShardingRule rule = new ShardingRule(shardingRuleConfig, dataSourceMap.keySet());

String originSql = "select city_id from user where city_id in (?,?) limit 5,10";
SQLRouteResult actual = new PreparedStatementRoutingEngine(originSql, rule, null, DatabaseType.MySQL, false, null).route(Lists.<Object>newArrayList(13,173));
SelectStatement stmt = (SelectStatement) actual.getSqlStatement();
assertThat(stmt.getLimit().getOffsetValue() , is(5));
assertThat(stmt.getLimit().getRowCountValue() ,is(10));
assertThat(actual.getRouteUnits().size(), is(1));

originSql = "select city_id from user where city_id in (?,?) limit 5,10";
actual = new PreparedStatementRoutingEngine(originSql, rule, null, DatabaseType.MySQL, false, null).route(Lists.<Object>newArrayList(89,84));
stmt = (SelectStatement) actual.getSqlStatement();

assertThat(stmt.getLimit().getOffsetValue() , is(5));
assertThat(stmt.getLimit().getRowCountValue() ,is(10));
assertThat(actual.getRouteUnits().size(), is(2));
}

private void assertTarget(final String originSql, final String targetDataSource) {
SQLRouteResult actual = new StatementRoutingEngine(shardingRule, null, DatabaseType.MySQL, false, null).route(originSql);
Expand Down