提交 97d6d3bb 编写于 作者: T terrymanu

for #2441, remove database type dependency of rewrite module

上级 c48e56c4
......@@ -19,7 +19,6 @@ package org.apache.shardingsphere.core;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.api.hint.HintManager;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.constant.properties.ShardingProperties;
import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import org.apache.shardingsphere.core.metadata.ShardingMetaData;
......@@ -57,8 +56,6 @@ public abstract class BaseShardingEngine {
private final ShardingMetaData metaData;
private final DatabaseType databaseType;
private final SPIRoutingHook routingHook = new SPIRoutingHook();
/**
......@@ -107,8 +104,8 @@ public abstract class BaseShardingEngine {
private Collection<RouteUnit> rewriteAndConvert(final List<Object> parameters, final SQLRouteResult sqlRouteResult) {
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, sqlRouteResult.getSqlStatement(), parameters, sqlRouteResult.getRoutingResult().isSingleRouting());
ShardingParameterRewriter shardingParameterRewriter = new ShardingParameterRewriter(databaseType, sqlRouteResult);
ShardingSQLRewriter shardingSQLRewriter = new ShardingSQLRewriter(shardingRule, databaseType, sqlRouteResult, sqlRouteResult.getOptimizeResult());
ShardingParameterRewriter shardingParameterRewriter = new ShardingParameterRewriter(sqlRouteResult);
ShardingSQLRewriter shardingSQLRewriter = new ShardingSQLRewriter(shardingRule, sqlRouteResult, sqlRouteResult.getOptimizeResult());
EncryptSQLRewriter encryptSQLRewriter = new EncryptSQLRewriter(shardingRule.getEncryptRule().getEncryptorEngine(), sqlRouteResult.getSqlStatement(), sqlRouteResult.getOptimizeResult());
rewriteEngine.init(Collections.<ParameterRewriter>singletonList(shardingParameterRewriter), Arrays.asList(shardingSQLRewriter, encryptSQLRewriter));
Collection<RouteUnit> result = new LinkedHashSet<>();
......
......@@ -46,7 +46,7 @@ public final class PreparedQueryShardingEngine extends BaseShardingEngine {
public PreparedQueryShardingEngine(final String sql, final ShardingRule shardingRule, final ShardingProperties shardingProperties,
final ShardingMetaData metaData, final DatabaseType databaseType, final ParsingResultCache cache) {
super(shardingRule, shardingProperties, metaData, databaseType);
super(shardingRule, shardingProperties, metaData);
routingEngine = new PreparedStatementRoutingEngine(sql, shardingRule, metaData, databaseType, cache);
}
......
......@@ -46,7 +46,7 @@ public final class SimpleQueryShardingEngine extends BaseShardingEngine {
public SimpleQueryShardingEngine(final ShardingRule shardingRule,
final ShardingProperties shardingProperties, final ShardingMetaData metaData, final DatabaseType databaseType, final ParsingResultCache cache) {
super(shardingRule, shardingProperties, metaData, databaseType);
super(shardingRule, shardingProperties, metaData);
routingEngine = new StatementRoutingEngine(shardingRule, metaData, databaseType, cache);
}
......
......@@ -27,9 +27,9 @@ import org.apache.shardingsphere.core.parse.extractor.util.RuleName;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.LimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.ParameterMarkerLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum.NumberLiteralRowNumberValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum.ParameterMarkerRowNumberValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum.RowNumberValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.top.TopSegment;
import java.util.Map;
......@@ -52,21 +52,21 @@ public final class TopSelectItemExtractor implements OptionalSQLSegmentExtractor
ParserRuleContext topExprNode = ExtractorUtils.getFirstChildNode(topNode.get(), RuleName.EXPR);
Optional<? extends ExpressionSegment> topExpr = expressionExtractor.extract(topExprNode, parameterMarkerIndexes);
Preconditions.checkState(topExpr.isPresent());
Optional<LimitValueSegment> limitValueSegment = createLimitValueSegment(topExpr.get());
Preconditions.checkState(limitValueSegment.isPresent());
Optional<RowNumberValueSegment> rowNumberValueSegment = createRowNumberValueSegment(topExpr.get());
Preconditions.checkState(rowNumberValueSegment.isPresent());
ParserRuleContext rowNumberAliasNode = ExtractorUtils.getFirstChildNode(topNode.get().getParent(), RuleName.ALIAS);
return Optional.of(
new TopSegment(topNode.get().getStart().getStartIndex(), topNode.get().getStop().getStopIndex(), topNode.get().getText(), limitValueSegment.get(), rowNumberAliasNode.getText()));
new TopSegment(topNode.get().getStart().getStartIndex(), topNode.get().getStop().getStopIndex(), topNode.get().getText(), rowNumberValueSegment.get(), rowNumberAliasNode.getText()));
}
private Optional<LimitValueSegment> createLimitValueSegment(final ExpressionSegment topExpr) {
private Optional<RowNumberValueSegment> createRowNumberValueSegment(final ExpressionSegment topExpr) {
if (topExpr instanceof ParameterMarkerExpressionSegment) {
return Optional.<LimitValueSegment>of(
new ParameterMarkerLimitValueSegment(topExpr.getStartIndex(), topExpr.getStopIndex(), ((ParameterMarkerExpressionSegment) topExpr).getParameterMarkerIndex()));
return Optional.<RowNumberValueSegment>of(
new ParameterMarkerRowNumberValueSegment(topExpr.getStartIndex(), topExpr.getStopIndex(), ((ParameterMarkerExpressionSegment) topExpr).getParameterMarkerIndex(), false));
}
if (topExpr instanceof LiteralExpressionSegment && ((LiteralExpressionSegment) topExpr).getLiterals() instanceof Number) {
return Optional.<LimitValueSegment>of(
new NumberLiteralLimitValueSegment(topExpr.getStartIndex(), topExpr.getStopIndex(), ((Number) ((LiteralExpressionSegment) topExpr).getLiterals()).intValue()));
return Optional.<RowNumberValueSegment>of(
new NumberLiteralRowNumberValueSegment(topExpr.getStartIndex(), topExpr.getStopIndex(), ((Number) ((LiteralExpressionSegment) topExpr).getLiterals()).intValue(), false));
}
return Optional.absent();
}
......
......@@ -20,7 +20,7 @@ package org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.top;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.SelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.LimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum.RowNumberValueSegment;
/**
* Top segment.
......@@ -37,7 +37,7 @@ public final class TopSegment implements SelectItemSegment {
private final String text;
private final LimitValueSegment top;
private final RowNumberValueSegment top;
private final String rowNumberAlias;
}
......@@ -18,7 +18,6 @@
package org.apache.shardingsphere.core.rewrite.rewriter.parameter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.rewrite.builder.ParameterBuilder;
import org.apache.shardingsphere.core.route.SQLRouteResult;
......@@ -31,8 +30,6 @@ import org.apache.shardingsphere.core.route.SQLRouteResult;
@RequiredArgsConstructor
public final class ShardingParameterRewriter implements ParameterRewriter {
private final DatabaseType databaseType;
private final SQLRouteResult sqlRouteResult;
@Override
......@@ -43,7 +40,7 @@ public final class ShardingParameterRewriter implements ParameterRewriter {
}
private void rewriteLimit(final SelectStatement selectStatement, final ParameterBuilder parameterBuilder) {
boolean isNeedFetchAll = (!selectStatement.getGroupByItems().isEmpty() || !selectStatement.getAggregationSelectItems().isEmpty()) && !selectStatement.isSameGroupByAndOrderByItems();
parameterBuilder.getReplacedIndexAndParameters().putAll(sqlRouteResult.getPagination().getRevisedParameters(isNeedFetchAll, databaseType.name()));
boolean isMaxRowCount = (!selectStatement.getGroupByItems().isEmpty() || !selectStatement.getAggregationSelectItems().isEmpty()) && !selectStatement.isSameGroupByAndOrderByItems();
parameterBuilder.getReplacedIndexAndParameters().putAll(sqlRouteResult.getPagination().getRevisedParameters(isMaxRowCount));
}
}
......@@ -19,7 +19,6 @@ package org.apache.shardingsphere.core.rewrite.rewriter.sql;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.optimize.result.OptimizeResult;
import org.apache.shardingsphere.core.optimize.result.insert.InsertOptimizeResult;
import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.OrderByItemSegment;
......@@ -63,15 +62,12 @@ public final class ShardingSQLRewriter implements SQLRewriter {
private final ShardingRule shardingRule;
private final DatabaseType databaseType;
private final SQLRouteResult sqlRouteResult;
private final InsertOptimizeResult insertOptimizeResult;
public ShardingSQLRewriter(final ShardingRule shardingRule, final DatabaseType databaseType, final SQLRouteResult sqlRouteResult, final OptimizeResult optimizeResult) {
public ShardingSQLRewriter(final ShardingRule shardingRule, final SQLRouteResult sqlRouteResult, final OptimizeResult optimizeResult) {
this.shardingRule = shardingRule;
this.databaseType = databaseType;
this.sqlRouteResult = sqlRouteResult;
this.insertOptimizeResult = getInsertOptimizeResult(optimizeResult);
}
......@@ -147,10 +143,7 @@ public final class ShardingSQLRewriter implements SQLRewriter {
if (!isRewrite) {
return rowCountToken.getRowCount();
}
if (isMaxRowCount(selectStatement)) {
return Integer.MAX_VALUE;
}
return pagination.isNeedRewriteRowCount(databaseType.name()) ? rowCountToken.getRowCount() + pagination.getOffsetValue() : rowCountToken.getRowCount();
return isMaxRowCount(selectStatement) ? Integer.MAX_VALUE : pagination.getRevisedRowCount();
}
private boolean isMaxRowCount(final SelectStatement selectStatement) {
......
......@@ -21,7 +21,6 @@ import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import lombok.SneakyThrows;
import org.apache.shardingsphere.core.constant.AggregationType;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.constant.OrderDirection;
import org.apache.shardingsphere.core.optimize.result.OptimizeResult;
import org.apache.shardingsphere.core.optimize.result.insert.InsertOptimizeResult;
......@@ -131,7 +130,7 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(selectStatement);
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT table_y.id FROM table_y WHERE table_y.id=?");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.<Object>singletonList(1));
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.<Object>singletonList(1));
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SELECT table_y.id FROM table_y WHERE table_y.id=?"));
}
......@@ -146,7 +145,7 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(selectStatement);
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT table_x.id, x.name FROM table_x x WHERE table_x.id=? AND x.name=?");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, parameters);
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(parameters);
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SELECT table_1.id, x.name FROM table_1 x WHERE table_1.id=? AND x.name=?"));
}
......@@ -161,7 +160,7 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(selectStatement);
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT x.age FROM table_x x GROUP BY x.id ORDER BY x.name");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is(
"SELECT x.age , x.id AS GROUP_BY_DERIVED_0 , x.name AS ORDER_BY_DERIVED_0 FROM table_1 x GROUP BY x.id ORDER BY x.name"));
}
......@@ -179,7 +178,7 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(selectStatement);
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT AVG(x.age) FROM table_x x");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is(
"SELECT AVG(x.age) , COUNT(x.age) AS AVG_DERIVED_COUNT_0 , SUM(x.age) AS AVG_DERIVED_SUM_0 FROM table_1 x"));
}
......@@ -205,7 +204,7 @@ public final class ShardingSQLRewriterTest {
routeResult.setRoutingResult(new RoutingResult());
routeResult.setOptimizeResult(new OptimizeResult(insertOptimizeResult));
insertStatement.setLogicSQL("INSERT INTO table_x (name, age) VALUES (?, ?)");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Arrays.asList(parameters));
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Arrays.asList(parameters));
assertThat(getSQLBuilder(rewriteEngine).toSQL(routingUnit, tableTokens), is("INSERT INTO table_1 (name, age, id) VALUES (?, ?, ?)"));
}
......@@ -229,7 +228,7 @@ public final class ShardingSQLRewriterTest {
routeResult.setRoutingResult(new RoutingResult());
routeResult.setOptimizeResult(new OptimizeResult(insertOptimizeResult));
insertStatement.setLogicSQL("INSERT INTO `table_x` VALUES (?)");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Arrays.asList(parameters));
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Arrays.asList(parameters));
assertThat(getSQLBuilder(rewriteEngine).toSQL(routingUnit, tableTokens), is("INSERT INTO `table_1`(name, id) VALUES (?, ?)"));
}
......@@ -251,7 +250,7 @@ public final class ShardingSQLRewriterTest {
routeResult.setRoutingResult(new RoutingResult());
routeResult.setOptimizeResult(new OptimizeResult(insertOptimizeResult));
insertStatement.setLogicSQL("INSERT INTO `table_x` VALUES (10)");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(routingUnit, tableTokens), is("INSERT INTO `table_1`(name, id) VALUES (10, 1)"));
}
......@@ -273,7 +272,7 @@ public final class ShardingSQLRewriterTest {
routeResult.setRoutingResult(new RoutingResult());
routeResult.setOptimizeResult(new OptimizeResult(insertOptimizeResult));
insertStatement.setLogicSQL("INSERT INTO `table_x` VALUES (10) ON DUPLICATE KEY UPDATE name = VALUES(name)");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(routingUnit, tableTokens), is("INSERT INTO `table_1`(name, id) VALUES (10, 1) ON DUPLICATE KEY UPDATE name = VALUES(name)"));
}
......@@ -293,7 +292,7 @@ public final class ShardingSQLRewriterTest {
routeResult.setOptimizeResult(new OptimizeResult(insertOptimizeResult));
routeResult.setRoutingResult(new RoutingResult());
insertStatement.setLogicSQL("INSERT INTO `table_x` set name = 10, id = 1 ON DUPLICATE KEY UPDATE name = VALUES(name)");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(routingUnit, tableTokens), is("INSERT INTO `table_1` set name = 10, id = 1 ON DUPLICATE KEY UPDATE name = VALUES(name)"));
}
......@@ -318,7 +317,7 @@ public final class ShardingSQLRewriterTest {
routeResult.setOptimizeResult(new OptimizeResult(insertOptimizeResult));
routeResult.setRoutingResult(new RoutingResult());
insertStatement.setLogicSQL("INSERT INTO `table_x` VALUES (10, 1)");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, parameters);
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(parameters);
assertThat(getSQLBuilder(rewriteEngine).toSQL(routingUnit, tableTokens), is("INSERT INTO `table_1`(name, id) VALUES (10, 1)"));
}
......@@ -341,7 +340,7 @@ public final class ShardingSQLRewriterTest {
routeResult.setOptimizeResult(new OptimizeResult(insertOptimizeResult));
routeResult.setRoutingResult(new RoutingResult());
insertStatement.setLogicSQL("INSERT INTO `table_x` VALUES (?, ?)");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Arrays.asList(parameters));
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Arrays.asList(parameters));
assertThat(getSQLBuilder(rewriteEngine).toSQL(routingUnit, tableTokens), is("INSERT INTO `table_1`(name, id) VALUES (?, ?)"));
}
......@@ -354,7 +353,7 @@ public final class ShardingSQLRewriterTest {
routeResult.setPagination(new Pagination(paginationSegment, Collections.emptyList()));
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT x.id FROM table_x x LIMIT 2, 2");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SELECT x.id FROM table_1 x LIMIT 0, 4"));
}
......@@ -367,21 +366,21 @@ public final class ShardingSQLRewriterTest {
routeResult.setPagination(new Pagination(paginationSegment, Collections.emptyList()));
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.Oracle, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens),
is("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_1 x) row_ WHERE rownum<=4) t WHERE t.rownum_>0"));
}
@Test
public void assertRewriteForTopAndRowNumber() {
PaginationSegment paginationSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(123, 123, 2, true), new NumberLiteralLimitValueSegment(26, 26, 4));
PaginationSegment paginationSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(123, 123, 2, true), new NumberLiteralRowNumberValueSegment(26, 26, 4, false));
selectStatement.setPagination(paginationSegment);
selectStatement.getSqlSegments().add(new TableSegment(85, 91, "table_x"));
routeResult = new SQLRouteResult(selectStatement);
routeResult.setPagination(new Pagination(paginationSegment, Collections.emptyList()));
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.SQLServer, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens),
is("SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_1 x) AS row_ WHERE row_.rownum_>0"));
}
......@@ -399,7 +398,7 @@ public final class ShardingSQLRewriterTest {
routeResult.setPagination(new Pagination(paginationSegment, Collections.emptyList()));
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT x.id FROM table_x x LIMIT 2, 2");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SELECT x.id FROM table_1 x LIMIT 0, 2147483647"));
}
......@@ -416,14 +415,14 @@ public final class ShardingSQLRewriterTest {
routeResult.setPagination(new Pagination(paginationSegment, Collections.emptyList()));
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.Oracle, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens),
is("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_1 x) row_ WHERE rownum<=2147483647) t WHERE t.rownum_>0"));
}
@Test
public void assertRewriteForTopAndRowNumberForMemoryGroupBy() {
PaginationSegment paginationSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(123, 123, 2, false), new NumberLiteralLimitValueSegment(26, 26, 4));
PaginationSegment paginationSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(123, 123, 2, false), new NumberLiteralRowNumberValueSegment(26, 26, 4, false));
selectStatement.setPagination(paginationSegment);
selectStatement.getSqlSegments().add(new TableSegment(85, 91, "table_x"));
ColumnSegment columnSegment = new ColumnSegment(0, 0, "id");
......@@ -434,7 +433,7 @@ public final class ShardingSQLRewriterTest {
routeResult.setPagination(new Pagination(paginationSegment, Collections.emptyList()));
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.SQLServer, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens),
is("SELECT * FROM (SELECT TOP(2147483647) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_1 x) AS row_ WHERE row_.rownum_>0"));
}
......@@ -450,7 +449,7 @@ public final class ShardingSQLRewriterTest {
routingResult.getRoutingUnits().add(new RoutingUnit("ds"));
routeResult.setRoutingResult(routingResult);
selectStatement.setLogicSQL("SELECT x.id FROM table_x x LIMIT 2, 2");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SELECT x.id FROM table_1 x LIMIT 2, 2"));
}
......@@ -465,14 +464,14 @@ public final class ShardingSQLRewriterTest {
routingResult.getRoutingUnits().add(new RoutingUnit("ds"));
routeResult.setRoutingResult(routingResult);
selectStatement.setLogicSQL("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.Oracle, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens),
is("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_1 x) row_ WHERE rownum<=4) t WHERE t.rownum_>2"));
}
@Test
public void assertRewriteForTopAndRowNumberForNotRewritePagination() {
PaginationSegment paginationSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(123, 123, 2, true), new NumberLiteralLimitValueSegment(26, 26, 4));
PaginationSegment paginationSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(123, 123, 2, true), new NumberLiteralRowNumberValueSegment(26, 26, 4, false));
selectStatement.setPagination(paginationSegment);
selectStatement.getSqlSegments().add(new TableSegment(85, 91, "table_x"));
routeResult = new SQLRouteResult(selectStatement);
......@@ -481,7 +480,7 @@ public final class ShardingSQLRewriterTest {
routingResult.getRoutingUnits().add(new RoutingUnit("ds"));
routeResult.setRoutingResult(routingResult);
selectStatement.setLogicSQL("SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.SQLServer, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens),
is("SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_1 x) AS row_ WHERE row_.rownum_>2"));
}
......@@ -502,7 +501,7 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(selectStatement);
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT x.id, x.name FROM table_x x GROUP BY x.id, x.name DESC");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SELECT x.id, x.name FROM table_1 x GROUP BY x.id, x.name DESC ORDER BY x.id ASC,x.name DESC "));
}
......@@ -517,8 +516,8 @@ public final class ShardingSQLRewriterTest {
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT table_x.id, x.name FROM table_x x, table_y y WHERE table_x.id=? AND x.name=?");
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, routeResult.getSqlStatement(), parameters, routeResult.getRoutingResult().isSingleRouting());
rewriteEngine.init(Collections.<ParameterRewriter>singletonList(new ShardingParameterRewriter(DatabaseType.MySQL, routeResult)),
Arrays.asList(new ShardingSQLRewriter(shardingRule, DatabaseType.MySQL, routeResult, null),
rewriteEngine.init(Collections.<ParameterRewriter>singletonList(new ShardingParameterRewriter(routeResult)),
Arrays.asList(new ShardingSQLRewriter(shardingRule, routeResult, null),
new EncryptSQLRewriter(shardingRule.getEncryptRule().getEncryptorEngine(), routeResult.getSqlStatement(), routeResult.getOptimizeResult())));
RoutingUnit routingUnit = new RoutingUnit("db0");
routingUnit.getTableUnits().add(new TableUnit("table_x", "table_x"));
......@@ -533,7 +532,7 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(selectStatement);
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("CREATE INDEX index_name ON table_x ('column')");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("CREATE INDEX index_name_table_1 ON table_1 ('column')"));
}
......@@ -559,7 +558,7 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(selectStatement);
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("CREATE INDEX logic_index ON table_x ('column')");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("CREATE INDEX logic_index_table_1 ON table_1 ('column')"));
}
......@@ -571,7 +570,7 @@ public final class ShardingSQLRewriterTest {
routingResult.getRoutingUnits().add(new RoutingUnit("ds"));
routeResult.setRoutingResult(routingResult);
showTablesStatement.setLogicSQL("SHOW COLUMNS FROM table_x");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SHOW COLUMNS FROM table_1"));
}
......@@ -584,7 +583,7 @@ public final class ShardingSQLRewriterTest {
routingResult.getRoutingUnits().add(new RoutingUnit("ds"));
routeResult.setRoutingResult(routingResult);
showTablesStatement.setLogicSQL("SHOW COLUMNS FROM table_x FROM 'sharding_db'");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
Map<String, String> logicAndActualTableMap = new LinkedHashMap<>();
logicAndActualTableMap.put("table_x", "table_x");
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, logicAndActualTableMap), is("SHOW COLUMNS FROM table_x"));
......@@ -598,7 +597,7 @@ public final class ShardingSQLRewriterTest {
routingResult.getRoutingUnits().add(new RoutingUnit("ds"));
routeResult.setRoutingResult(routingResult);
showTablesStatement.setLogicSQL("SHOW COLUMNS FROM `table_x`");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SHOW COLUMNS FROM `table_1`"));
}
......@@ -610,7 +609,7 @@ public final class ShardingSQLRewriterTest {
routingResult.getRoutingUnits().add(new RoutingUnit("ds"));
routeResult.setRoutingResult(routingResult);
showTablesStatement.setLogicSQL("SHOW COLUMNS FROM `table_x` FROM 'sharding_db'");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SHOW COLUMNS FROM `table_1` FROM 'sharding_db'"));
}
......@@ -622,7 +621,7 @@ public final class ShardingSQLRewriterTest {
routingResult.getRoutingUnits().add(new RoutingUnit("ds"));
routeResult.setRoutingResult(routingResult);
showTablesStatement.setLogicSQL("SHOW COLUMNS FROM sharding_db.table_x");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SHOW COLUMNS FROM table_1"));
}
......@@ -634,7 +633,7 @@ public final class ShardingSQLRewriterTest {
routingResult.getRoutingUnits().add(new RoutingUnit("ds"));
routeResult.setRoutingResult(routingResult);
showTablesStatement.setLogicSQL("SHOW COLUMNS FROM sharding_db.table_x FROM sharding_db");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SHOW COLUMNS FROM table_1 FROM sharding_db"));
}
......@@ -646,7 +645,7 @@ public final class ShardingSQLRewriterTest {
routingResult.getRoutingUnits().add(new RoutingUnit("ds"));
routeResult.setRoutingResult(routingResult);
showTablesStatement.setLogicSQL("SHOW COLUMNS FROM sharding_db.`table_x`");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SHOW COLUMNS FROM `table_1`"));
}
......@@ -658,7 +657,7 @@ public final class ShardingSQLRewriterTest {
routingResult.getRoutingUnits().add(new RoutingUnit("ds"));
routeResult.setRoutingResult(routingResult);
showTablesStatement.setLogicSQL("SHOW COLUMNS FROM sharding_db.`table_x` FROM sharding_db");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SHOW COLUMNS FROM `table_1` FROM sharding_db"));
}
......@@ -670,7 +669,7 @@ public final class ShardingSQLRewriterTest {
routingResult.getRoutingUnits().add(new RoutingUnit("ds"));
routeResult.setRoutingResult(routingResult);
showTablesStatement.setLogicSQL("SHOW COLUMNS FROM `sharding_db`.`table_x`");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SHOW COLUMNS FROM `table_1`"));
}
......@@ -682,7 +681,7 @@ public final class ShardingSQLRewriterTest {
routingResult.getRoutingUnits().add(new RoutingUnit("ds"));
routeResult.setRoutingResult(routingResult);
showTablesStatement.setLogicSQL("SHOW COLUMNS FROM `sharding_db`.`table_x` FROM sharding_db");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SHOW COLUMNS FROM `table_1` FROM sharding_db"));
}
......@@ -697,7 +696,7 @@ public final class ShardingSQLRewriterTest {
routingResult.getRoutingUnits().add(new RoutingUnit("ds"));
routeResult.setRoutingResult(routingResult);
selectStatement.setLogicSQL("SELECT * FROM sharding_db.table_x");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SELECT * FROM table_1"));
}
......@@ -707,7 +706,7 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(insertStatement);
routeResult.setRoutingResult(new RoutingResult());
insertStatement.setLogicSQL("INSERT INTO sharding_db.table_x (order_id, user_id, status) values (1, 1, 'OK')");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(
null, tableTokens), is("INSERT INTO table_1 (order_id, user_id, status) values (1, 1, 'OK')"));
}
......@@ -718,7 +717,7 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(updateStatement);
routeResult.setRoutingResult(new RoutingResult());
updateStatement.setLogicSQL("UPDATE `sharding_db`.table_x SET user_id=1 WHERE order_id=1");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("UPDATE table_1 SET user_id=1 WHERE order_id=1"));
}
......@@ -730,7 +729,7 @@ public final class ShardingSQLRewriterTest {
routingResult.getRoutingUnits().add(new RoutingUnit("ds"));
routeResult.setRoutingResult(routingResult);
deleteStatement.setLogicSQL("DELETE FROM `sharding_db`.`table_x` WHERE user_id=1");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("DELETE FROM `table_1` WHERE user_id=1"));
}
......@@ -747,7 +746,7 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(selectStatement);
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT id FROM table_z WHERE id=? AND name=?");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, parameters);
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(parameters);
assertThat(getSQLBuilder(rewriteEngine).toSQL(), is("SELECT id FROM table_z WHERE id = ? AND name=?"));
assertThat(getParameterBuilder(rewriteEngine).getParameters().get(0), is((Object) "encryptValue"));
}
......@@ -764,7 +763,7 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(selectStatement);
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT id FROM table_z WHERE id in (3,5)");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SELECT id FROM table_z WHERE id IN ('encryptValue', 'encryptValue')"));
}
......@@ -785,7 +784,7 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(selectStatement);
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT id FROM table_z WHERE id in (?, ?) or id = 3");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, parameters);
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(parameters);
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SELECT id FROM table_z WHERE id IN (?, ?) or id = 'encryptValue'"));
assertThat(getParameterBuilder(rewriteEngine).getParameters().get(0), is((Object) "encryptValue"));
assertThat(getParameterBuilder(rewriteEngine).getParameters().get(1), is((Object) "encryptValue"));
......@@ -804,7 +803,7 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(selectStatement);
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT id FROM table_k WHERE id=? AND name=?");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, parameters);
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(parameters);
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SELECT id FROM table_k WHERE query_id = ? AND name=?"));
assertThat(getParameterBuilder(rewriteEngine).getParameters().get(0), is((Object) "assistedEncryptValue"));
}
......@@ -821,7 +820,7 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(selectStatement);
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT id FROM table_k WHERE id in (3,5)");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens),
is("SELECT id FROM table_k WHERE query_id IN ('assistedEncryptValue', 'assistedEncryptValue')"));
}
......@@ -839,7 +838,7 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(updateStatement);
routeResult.setRoutingResult(new RoutingResult());
updateStatement.setLogicSQL("UPDATE table_z SET id = 1 WHERE id = 2");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("UPDATE table_z SET id = 'encryptValue' WHERE id = 'encryptValue'"));
}
......@@ -861,7 +860,7 @@ public final class ShardingSQLRewriterTest {
routeResult.setOptimizeResult(new OptimizeResult(insertOptimizeResult));
routeResult.setRoutingResult(new RoutingResult());
insertStatement.setLogicSQL("INSERT INTO `table_w` set name = 10 ON DUPLICATE KEY UPDATE name = VALUES(name)");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(routingUnit, tableTokens),
is("INSERT INTO `table_w` set name = 'encryptValue', id = 1, query_name = 'assistedEncryptValue' ON DUPLICATE KEY UPDATE name = VALUES(name)"));
}
......@@ -879,14 +878,14 @@ public final class ShardingSQLRewriterTest {
routeResult = new SQLRouteResult(selectStatement);
routeResult.setRoutingResult(new RoutingResult());
selectStatement.setLogicSQL("SELECT COUNT(DISTINCT id), SUM(DISTINCT id) FROM table_z WHERE id in (3,5)");
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(DatabaseType.MySQL, Collections.emptyList());
SQLRewriteEngine rewriteEngine = createSQLRewriteEngine(Collections.emptyList());
assertThat(getSQLBuilder(rewriteEngine).toSQL(null, tableTokens), is("SELECT DISTINCT id, id FROM table_z WHERE id in (3,5)"));
}
private SQLRewriteEngine createSQLRewriteEngine(final DatabaseType databaseType, final List<Object> parameters) {
private SQLRewriteEngine createSQLRewriteEngine(final List<Object> parameters) {
SQLRewriteEngine result = new SQLRewriteEngine(shardingRule, routeResult.getSqlStatement(), parameters, routeResult.getRoutingResult().isSingleRouting());
result.init(Collections.<ParameterRewriter>singletonList(new ShardingParameterRewriter(DatabaseType.MySQL, routeResult)),
Arrays.asList(new ShardingSQLRewriter(shardingRule, databaseType, routeResult, routeResult.getOptimizeResult()),
result.init(Collections.<ParameterRewriter>singletonList(new ShardingParameterRewriter(routeResult)),
Arrays.asList(new ShardingSQLRewriter(shardingRule, routeResult, routeResult.getOptimizeResult()),
new EncryptSQLRewriter(shardingRule.getEncryptRule().getEncryptorEngine(), routeResult.getSqlStatement(), routeResult.getOptimizeResult())));
return result;
}
......
......@@ -22,6 +22,7 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLit
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.ParameterMarkerPaginationValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.LimitValueSegment;
import java.util.HashMap;
import java.util.List;
......@@ -74,35 +75,26 @@ public final class Pagination {
/**
* Get revise parameters.
*
* @param isFetchAll is fetch all data or not
* @param databaseType database type
* @param isMaxRowCount is max row count
* @return revised parameters and parameters' indexes
*/
public Map<Integer, Object> getRevisedParameters(final boolean isFetchAll, final String databaseType) {
public Map<Integer, Object> getRevisedParameters(final boolean isMaxRowCount) {
Map<Integer, Object> result = new HashMap<>(2, 1);
if (null != offset && offset.getSegment() instanceof ParameterMarkerPaginationValueSegment) {
result.put(((ParameterMarkerPaginationValueSegment) offset.getSegment()).getParameterIndex(), 0);
}
if (null != rowCount && rowCount.getSegment() instanceof ParameterMarkerPaginationValueSegment) {
result.put(((ParameterMarkerPaginationValueSegment) rowCount.getSegment()).getParameterIndex(), getRewriteRowCount(isFetchAll, databaseType));
result.put(((ParameterMarkerPaginationValueSegment) rowCount.getSegment()).getParameterIndex(), isMaxRowCount ? Integer.MAX_VALUE : getRevisedRowCount());
}
return result;
}
private int getRewriteRowCount(final boolean isFetchAll, final String databaseType) {
if (isFetchAll) {
return Integer.MAX_VALUE;
}
return isNeedRewriteRowCount(databaseType) ? getOffsetValue() + rowCount.getValue() : rowCount.getValue();
}
/**
* Judge is need rewrite row count or not.
* Get revised row count.
*
* @param databaseType database type
* @return is need rewrite row count or not
* @return revised row count
*/
public boolean isNeedRewriteRowCount(final String databaseType) {
return "MySQL".equals(databaseType) || "PostgreSQL".equals(databaseType) || "H2".equals(databaseType);
public int getRevisedRowCount() {
return rowCount.getSegment() instanceof LimitValueSegment ? getOffsetValue() + rowCount.getValue() : rowCount.getValue();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册