提交 3f28158c 编写于 作者: T terrymanu

for #2441, add RowNumberValueSegment

上级 47367193
......@@ -24,7 +24,7 @@ import org.apache.shardingsphere.core.route.limit.Limit;
import java.sql.SQLException;
/**
* Decorator merged result for rownum pagination.
* Decorator merged result for row number pagination.
*
* @author zhangliang
*/
......@@ -47,7 +47,7 @@ public final class RowNumberDecoratorMergedResult extends DecoratorMergedResult
if (null == limit.getOffset()) {
end = 0;
} else {
end = limit.getOffset().getLimitValueSegment().isBoundOpened() ? limit.getOffsetValue() - 1 : limit.getOffsetValue();
end = limit.getOffset().getPaginationValueSegment().isBoundOpened() ? limit.getOffsetValue() - 1 : limit.getOffsetValue();
}
for (int i = 0; i < end; i++) {
if (!getMergedResult().next()) {
......@@ -66,7 +66,7 @@ public final class RowNumberDecoratorMergedResult extends DecoratorMergedResult
if (limit.getRowCountValue() < 0) {
return getMergedResult().next();
}
if (limit.getRowCount().getLimitValueSegment().isBoundOpened()) {
if (limit.getRowCount().getPaginationValueSegment().isBoundOpened()) {
return rowNumber++ <= limit.getRowCountValue() && getMergedResult().next();
}
return rowNumber++ < limit.getRowCountValue() && getMergedResult().next();
......
......@@ -24,7 +24,7 @@ import org.apache.shardingsphere.core.route.limit.Limit;
import java.sql.SQLException;
/**
* Decorator merged result for top and rownum pagination.
* Decorator merged result for top and row number pagination.
*
* @author zhangliang
*/
......@@ -47,7 +47,7 @@ public final class TopAndRowNumberDecoratorMergedResult extends DecoratorMergedR
if (null == limit.getOffset()) {
end = 0;
} else {
end = limit.getOffset().getLimitValueSegment().isBoundOpened() ? limit.getOffsetValue() - 1 : limit.getOffsetValue();
end = limit.getOffset().getPaginationValueSegment().isBoundOpened() ? limit.getOffsetValue() - 1 : limit.getOffsetValue();
}
for (int i = 0; i < end; i++) {
if (!getMergedResult().next()) {
......
......@@ -33,8 +33,8 @@ import org.apache.shardingsphere.core.merge.dql.pagination.RowNumberDecoratorMer
import org.apache.shardingsphere.core.merge.dql.pagination.TopAndRowNumberDecoratorMergedResult;
import org.apache.shardingsphere.core.merge.fixture.TestQueryResult;
import org.apache.shardingsphere.core.parse.sql.context.selectitem.AggregationSelectItem;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.IndexOrderByItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.route.SQLRouteResult;
import org.apache.shardingsphere.core.route.limit.Limit;
......@@ -91,14 +91,14 @@ public final class DQLMergeEngineTest {
@Test
public void assertBuildIteratorStreamMergedResultWithLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, singleQueryResult);
assertThat(mergeEngine.merge(), instanceOf(IteratorStreamMergedResult.class));
}
@Test
public void assertBuildIteratorStreamMergedResultWithMySQLLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
assertThat(actual, instanceOf(LimitDecoratorMergedResult.class));
......@@ -107,7 +107,7 @@ public final class DQLMergeEngineTest {
@Test
public void assertBuildIteratorStreamMergedResultWithOracleLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
assertThat(actual, instanceOf(RowNumberDecoratorMergedResult.class));
......@@ -116,7 +116,7 @@ public final class DQLMergeEngineTest {
@Test
public void assertBuildIteratorStreamMergedResultWithSQLServerLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
assertThat(actual, instanceOf(TopAndRowNumberDecoratorMergedResult.class));
......@@ -132,7 +132,7 @@ public final class DQLMergeEngineTest {
@Test
public void assertBuildOrderByStreamMergedResultWithMySQLLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
selectStatement.getOrderByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC));
mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......@@ -142,7 +142,7 @@ public final class DQLMergeEngineTest {
@Test
public void assertBuildOrderByStreamMergedResultWithOracleLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
selectStatement.getOrderByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC));
mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......@@ -152,7 +152,7 @@ public final class DQLMergeEngineTest {
@Test
public void assertBuildOrderByStreamMergedResultWithSQLServerLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
selectStatement.getOrderByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC));
mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......@@ -170,7 +170,7 @@ public final class DQLMergeEngineTest {
@Test
public void assertBuildGroupByStreamMergedResultWithMySQLLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
selectStatement.getGroupByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC));
selectStatement.getOrderByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC));
mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults);
......@@ -181,7 +181,7 @@ public final class DQLMergeEngineTest {
@Test
public void assertBuildGroupByStreamMergedResultWithOracleLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
selectStatement.getGroupByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC));
selectStatement.getOrderByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC));
mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults);
......@@ -192,7 +192,7 @@ public final class DQLMergeEngineTest {
@Test
public void assertBuildGroupByStreamMergedResultWithSQLServerLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
selectStatement.getGroupByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC));
selectStatement.getOrderByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC));
mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults);
......@@ -210,7 +210,7 @@ public final class DQLMergeEngineTest {
@Test
public void assertBuildGroupByMemoryMergedResultWithMySQLLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
selectStatement.getGroupByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC));
mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......@@ -220,7 +220,7 @@ public final class DQLMergeEngineTest {
@Test
public void assertBuildGroupByMemoryMergedResultWithOracleLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
selectStatement.getGroupByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC));
selectStatement.getOrderByItems().add(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC));
mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults);
......@@ -231,7 +231,7 @@ public final class DQLMergeEngineTest {
@Test
public void assertBuildGroupByMemoryMergedResultWithSQLServerLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
selectStatement.getGroupByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC));
selectStatement.getGroupByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC));
mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults);
......@@ -249,7 +249,7 @@ public final class DQLMergeEngineTest {
@Test
public void assertBuildGroupByMemoryMergedResultWithAggregationOnlyWithMySQLLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
selectStatement.getItems().add(new AggregationSelectItem(AggregationType.COUNT, "(*)", Optional.<String>absent()));
mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......@@ -259,7 +259,7 @@ public final class DQLMergeEngineTest {
@Test
public void assertBuildGroupByMemoryMergedResultWithAggregationOnlyWithOracleLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
selectStatement.getItems().add(new AggregationSelectItem(AggregationType.COUNT, "(*)", Optional.<String>absent()));
mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......@@ -269,7 +269,7 @@ public final class DQLMergeEngineTest {
@Test
public void assertBuildGroupByMemoryMergedResultWithAggregationOnlyWithSQLServerLimit() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
selectStatement.getItems().add(new AggregationSelectItem(AggregationType.COUNT, "(*)", Optional.<String>absent()));
mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
......
......@@ -23,8 +23,8 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult;
import org.apache.shardingsphere.core.merge.MergedResult;
import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine;
import org.apache.shardingsphere.core.merge.fixture.TestQueryResult;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.route.SQLRouteResult;
import org.apache.shardingsphere.core.route.limit.Limit;
......@@ -66,12 +66,12 @@ public final class LimitDecoratorMergedResultTest {
}
SelectStatement selectStatement = new SelectStatement();
routeResult = new SQLRouteResult(selectStatement);
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
}
@Test
public void assertNextForSkipAll() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, Integer.MAX_VALUE, true), null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, Integer.MAX_VALUE), null), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
assertFalse(actual.next());
......@@ -79,7 +79,7 @@ public final class LimitDecoratorMergedResultTest {
@Test
public void assertNextWithoutRowCount() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2, true), null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2), null), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
for (int i = 0; i < 6; i++) {
......@@ -90,7 +90,7 @@ public final class LimitDecoratorMergedResultTest {
@Test
public void assertNextWithRowCount() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2, true), new NumberLiteralLimitValueSegment(0, 0, 2, false)), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2), new NumberLiteralLimitValueSegment(0, 0, 2)), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
assertTrue(actual.next());
......
......@@ -23,8 +23,8 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult;
import org.apache.shardingsphere.core.merge.MergedResult;
import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine;
import org.apache.shardingsphere.core.merge.fixture.TestQueryResult;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum.NumberLiteralRowNumberValueSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.route.SQLRouteResult;
import org.apache.shardingsphere.core.route.limit.Limit;
......@@ -69,7 +69,7 @@ public final class RowNumberDecoratorMergedResultTest {
@Test
public void assertNextForSkipAll() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, Integer.MAX_VALUE, true), null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(0, 0, Integer.MAX_VALUE, true), null), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
assertFalse(actual.next());
......@@ -77,7 +77,7 @@ public final class RowNumberDecoratorMergedResultTest {
@Test
public void assertNextWithoutOffsetWithoutRowCount() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
for (int i = 0; i < 8; i++) {
......@@ -88,7 +88,8 @@ public final class RowNumberDecoratorMergedResultTest {
@Test
public void assertNextForRowCountBoundOpenedFalse() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2, true), new NumberLiteralLimitValueSegment(0, 0, 4, false)), Collections.emptyList()));
routeResult.setLimit(new Limit(
new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(0, 0, 2, true), new NumberLiteralRowNumberValueSegment(0, 0, 4, false)), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
assertTrue(actual.next());
......@@ -98,7 +99,8 @@ public final class RowNumberDecoratorMergedResultTest {
@Test
public void assertNextForRowCountBoundOpenedTrue() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2, true), new NumberLiteralLimitValueSegment(0, 0, 4, true)), Collections.emptyList()));
routeResult.setLimit(new Limit(
new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(0, 0, 2, true), new NumberLiteralRowNumberValueSegment(0, 0, 4, true)), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
assertTrue(actual.next());
......
......@@ -23,8 +23,9 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult;
import org.apache.shardingsphere.core.merge.MergedResult;
import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine;
import org.apache.shardingsphere.core.merge.fixture.TestQueryResult;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum.NumberLiteralRowNumberValueSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.route.SQLRouteResult;
import org.apache.shardingsphere.core.route.limit.Limit;
......@@ -49,8 +50,6 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
private List<QueryResult> queryResults;
private SelectStatement selectStatement;
private SQLRouteResult routeResult;
@Before
......@@ -66,13 +65,12 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
for (ResultSet each : resultSets) {
queryResults.add(new TestQueryResult(each));
}
selectStatement = new SelectStatement();
routeResult = new SQLRouteResult(selectStatement);
routeResult = new SQLRouteResult(new SelectStatement());
}
@Test
public void assertNextForSkipAll() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, Integer.MAX_VALUE, true), null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(0, 0, Integer.MAX_VALUE, true), null), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
assertFalse(actual.next());
......@@ -80,7 +78,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
@Test
public void assertNextWithoutOffsetWithRowCount() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, new NumberLiteralLimitValueSegment(0, 0, 5, false)), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, new NumberLiteralLimitValueSegment(0, 0, 5)), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
for (int i = 0; i < 5; i++) {
......@@ -91,7 +89,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
@Test
public void assertNextWithOffsetWithoutRowCount() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2, true), null), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(0, 0, 2, true), null), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
for (int i = 0; i < 7; i++) {
......@@ -102,7 +100,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
@Test
public void assertNextWithOffsetBoundOpenedFalse() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2, false), new NumberLiteralLimitValueSegment(0, 0, 4, false)), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(0, 0, 2, false), new NumberLiteralLimitValueSegment(0, 0, 4)), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
assertTrue(actual.next());
......@@ -112,7 +110,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
@Test
public void assertNextWithOffsetBoundOpenedTrue() throws SQLException {
routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2, true), new NumberLiteralLimitValueSegment(0, 0, 4, false)), Collections.emptyList()));
routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(0, 0, 2, true), new NumberLiteralLimitValueSegment(0, 0, 4)), Collections.emptyList()));
mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults);
MergedResult actual = mergeEngine.merge();
assertTrue(actual.next());
......
......@@ -25,10 +25,10 @@ import org.apache.shardingsphere.core.parse.extractor.impl.common.expression.imp
import org.apache.shardingsphere.core.parse.extractor.util.ExtractorUtils;
import org.apache.shardingsphere.core.parse.extractor.util.RuleName;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.ParameterMarkerLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment;
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.util.NumberUtil;
import java.util.Map;
......@@ -45,11 +45,11 @@ public final class LimitExtractor implements OptionalSQLSegmentExtractor {
private final ParameterMarkerExpressionExtractor parameterMarkerExpressionExtractor = new ParameterMarkerExpressionExtractor();
@Override
public Optional<LimitSegment> extract(final ParserRuleContext ancestorNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
public Optional<PaginationSegment> extract(final ParserRuleContext ancestorNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
Optional<ParserRuleContext> limitNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.LIMIT_CLAUSE);
return limitNode.isPresent()
? Optional.of(new LimitSegment(limitNode.get().getStart().getStartIndex(), limitNode.get().getStop().getStopIndex(),
extractOffset(limitNode.get(), parameterMarkerIndexes).orNull(), extractRowCount(limitNode.get(), parameterMarkerIndexes).orNull())) : Optional.<LimitSegment>absent();
? Optional.of(new PaginationSegment(limitNode.get().getStart().getStartIndex(), limitNode.get().getStop().getStopIndex(),
extractOffset(limitNode.get(), parameterMarkerIndexes).orNull(), extractRowCount(limitNode.get(), parameterMarkerIndexes).orNull())) : Optional.<PaginationSegment>absent();
}
private Optional<LimitValueSegment> extractOffset(final ParserRuleContext limitNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
......@@ -66,11 +66,11 @@ public final class LimitExtractor implements OptionalSQLSegmentExtractor {
Optional<ParameterMarkerExpressionSegment> parameterMarkerExpression = parameterMarkerExpressionExtractor.extract(limitValueNode, parameterMarkerIndexes);
if (parameterMarkerExpression.isPresent()) {
return new ParameterMarkerLimitValueSegment(
limitValueNode.getStart().getStartIndex(), limitValueNode.getStop().getStopIndex(), parameterMarkerExpression.get().getParameterMarkerIndex(), false);
limitValueNode.getStart().getStartIndex(), limitValueNode.getStop().getStopIndex(), parameterMarkerExpression.get().getParameterMarkerIndex());
}
Optional<ParserRuleContext> numberLiteralsNode = ExtractorUtils.findFirstChildNode(limitValueNode, RuleName.NUMBER_LITERALS);
Preconditions.checkState(numberLiteralsNode.isPresent());
return new NumberLiteralLimitValueSegment(
limitValueNode.getStart().getStartIndex(), limitValueNode.getStop().getStopIndex(), NumberUtil.getExactlyNumber(numberLiteralsNode.get().getText(), 10).intValue(), false);
limitValueNode.getStart().getStartIndex(), limitValueNode.getStop().getStopIndex(), NumberUtil.getExactlyNumber(numberLiteralsNode.get().getText(), 10).intValue());
}
}
......@@ -27,10 +27,10 @@ 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.limit.LimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.ParameterMarkerLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.TopSegment;
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.top.TopSegment;
import java.util.Map;
......@@ -62,11 +62,11 @@ public final class TopSelectItemExtractor implements OptionalSQLSegmentExtractor
private Optional<LimitValueSegment> createLimitValueSegment(final ExpressionSegment topExpr) {
if (topExpr instanceof ParameterMarkerExpressionSegment) {
return Optional.<LimitValueSegment>of(
new ParameterMarkerLimitValueSegment(topExpr.getStartIndex(), topExpr.getStopIndex(), ((ParameterMarkerExpressionSegment) topExpr).getParameterMarkerIndex(), false));
new ParameterMarkerLimitValueSegment(topExpr.getStartIndex(), topExpr.getStopIndex(), ((ParameterMarkerExpressionSegment) topExpr).getParameterMarkerIndex()));
}
if (topExpr instanceof LiteralExpressionSegment && ((LiteralExpressionSegment) topExpr).getLiterals() instanceof Number) {
return Optional.<LimitValueSegment>of(
new NumberLiteralLimitValueSegment(topExpr.getStartIndex(), topExpr.getStopIndex(), ((Number) ((LiteralExpressionSegment) topExpr).getLiterals()).intValue(), false));
new NumberLiteralLimitValueSegment(topExpr.getStartIndex(), topExpr.getStopIndex(), ((Number) ((LiteralExpressionSegment) topExpr).getLiterals()).intValue()));
}
return Optional.absent();
}
......
......@@ -18,7 +18,7 @@
package org.apache.shardingsphere.core.parse.filler.common.dml;
import org.apache.shardingsphere.core.parse.filler.api.SQLSegmentFiller;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
......@@ -27,10 +27,10 @@ import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
*
* @author duhongjun
*/
public final class LimitFiller implements SQLSegmentFiller<LimitSegment> {
public final class LimitFiller implements SQLSegmentFiller<PaginationSegment> {
@Override
public void fill(final LimitSegment sqlSegment, final SQLStatement sqlStatement) {
((SelectStatement) sqlStatement).setLimit(sqlSegment);
public void fill(final PaginationSegment sqlSegment, final SQLStatement sqlStatement) {
((SelectStatement) sqlStatement).setPagination(sqlSegment);
}
}
......@@ -32,8 +32,8 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.item.ColumnSelectIte
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.ExpressionSelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.SelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.ShorthandSelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.TopSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.top.TopSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
......@@ -109,7 +109,7 @@ public final class SelectItemFiller implements SQLSegmentFiller<SelectItemSegmen
}
private void fillTopSegment(final TopSegment topSegment, final SelectStatement selectStatement) {
selectStatement.setLimit(new LimitSegment(topSegment.getStartIndex(), topSegment.getStopIndex(), null, topSegment.getTop()));
selectStatement.setPagination(new PaginationSegment(topSegment.getStartIndex(), topSegment.getStopIndex(), null, topSegment.getTop()));
selectStatement.getItems().add(new CommonSelectItem("rownum", Optional.of(topSegment.getRowNumberAlias())));
}
}
......@@ -23,10 +23,10 @@ import org.apache.shardingsphere.core.parse.sql.context.selectitem.SelectItem;
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.limit.LimitSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.ParameterMarkerLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment;
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.predicate.AndPredicate;
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.OrPredicateSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.PredicateSegment;
......@@ -98,34 +98,34 @@ public final class ShardingRowNumberPredicateFiller implements SQLSegmentFiller<
}
private void fillLimit(final SelectStatement selectStatement, final Collection<PredicateSegment> rowNumberPredicates) {
LimitValueSegment rowCount = null;
LimitValueSegment offset = null;
RowNumberValueSegment rowCount = null;
RowNumberValueSegment offset = null;
for (PredicateSegment each : rowNumberPredicates) {
ExpressionSegment expression = ((PredicateCompareRightValue) each.getRightValue()).getExpression();
switch (((PredicateCompareRightValue) each.getRightValue()).getOperator()) {
case ">":
offset = createLimitValueSegment(expression, false);
offset = createRowNumberValueSegment(expression, false);
break;
case ">=":
offset = createLimitValueSegment(expression, true);
offset = createRowNumberValueSegment(expression, true);
break;
case "<":
rowCount = createLimitValueSegment(expression, false);
rowCount = createRowNumberValueSegment(expression, false);
break;
case "<=":
rowCount = createLimitValueSegment(expression, true);
rowCount = createRowNumberValueSegment(expression, true);
break;
default:
break;
}
}
selectStatement.setLimit(new LimitSegment(-1, -1, offset, rowCount));
selectStatement.setPagination(new PaginationSegment(-1, -1, offset, rowCount));
}
private LimitValueSegment createLimitValueSegment(final ExpressionSegment expression, final boolean boundOpened) {
if (expression instanceof LiteralExpressionSegment) {
return new NumberLiteralLimitValueSegment(expression.getStartIndex(), expression.getStopIndex(), (Integer) ((LiteralExpressionSegment) expression).getLiterals(), boundOpened);
}
return new ParameterMarkerLimitValueSegment(expression.getStartIndex(), expression.getStopIndex(), ((ParameterMarkerExpressionSegment) expression).getParameterMarkerIndex(), boundOpened);
private RowNumberValueSegment createRowNumberValueSegment(final ExpressionSegment expression, final boolean boundOpened) {
return expression instanceof LiteralExpressionSegment
? new NumberLiteralRowNumberValueSegment(expression.getStartIndex(), expression.getStopIndex(), (int) ((LiteralExpressionSegment) expression).getLiterals(), boundOpened)
: new ParameterMarkerRowNumberValueSegment(
expression.getStartIndex(), expression.getStopIndex(), ((ParameterMarkerExpressionSegment) expression).getParameterMarkerIndex(), boundOpened);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.core.parse.sql.segment.dml.pagination;
import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment;
/**
* Pagination value segment for number literal.
*
* @author zhangliang
*/
public interface NumberLiteralPaginationValueSegment extends SQLSegment {
/**
* Get value.
*
* @return value
*/
int getValue();
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.core.parse.sql.segment.dml.pagination;
import com.google.common.base.Optional;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment;
/**
* Pagination segment.
*
* @author duhongjun
*/
@RequiredArgsConstructor
@Getter
public final class PaginationSegment implements SQLSegment {
private final int startIndex;
private final int stopIndex;
private final PaginationValueSegment offset;
private final PaginationValueSegment rowCount;
/**
* Get offset.
*
* @return offset
*/
public Optional<PaginationValueSegment> getOffset() {
return Optional.fromNullable(offset);
}
/**
* Get row count.
*
* @return row count
*/
public Optional<PaginationValueSegment> getRowCount() {
return Optional.fromNullable(rowCount);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.core.parse.sql.segment.dml.pagination;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment;
/**
* Pagination value segment.
*
* @author zhangliang
* @author panjuan
*/
@RequiredArgsConstructor
@Getter
public abstract class PaginationValueSegment implements SQLSegment {
private final int startIndex;
private final int stopIndex;
private final boolean boundOpened;
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.core.parse.sql.segment.dml.pagination;
import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment;
/**
* Pagination value segment for parameter marker.
*
* @author zhangliang
*/
public interface ParameterMarkerPaginationValueSegment extends SQLSegment {
/**
* Get parameter index.
*
* @return parameter index
*/
int getParameterIndex();
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationValueSegment;
/**
* Limit value segment.
*
* @author zhangliang
* @author panjuan
*/
public abstract class LimitValueSegment extends PaginationValueSegment {
public LimitValueSegment(final int startIndex, final int stopIndex) {
super(startIndex, stopIndex, false);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit;
import lombok.Getter;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLiteralPaginationValueSegment;
/**
* Limit value segment for number literal.
*
* @author zhangliang
*/
@Getter
public final class NumberLiteralLimitValueSegment extends LimitValueSegment implements NumberLiteralPaginationValueSegment {
private final int value;
public NumberLiteralLimitValueSegment(final int startIndex, final int stopIndex, final int value) {
super(startIndex, stopIndex);
this.value = value;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit;
import lombok.Getter;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.ParameterMarkerPaginationValueSegment;
/**
* Limit value segment for parameter marker.
*
* @author zhangliang
*/
@Getter
public final class ParameterMarkerLimitValueSegment extends LimitValueSegment implements ParameterMarkerPaginationValueSegment {
private final int parameterIndex;
public ParameterMarkerLimitValueSegment(final int startIndex, final int stopIndex, final int parameterIndex) {
super(startIndex, stopIndex);
this.parameterIndex = parameterIndex;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum;
import lombok.Getter;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLiteralPaginationValueSegment;
/**
* Row number value segment for number literal.
*
* @author zhangliang
*/
@Getter
public final class NumberLiteralRowNumberValueSegment extends RowNumberValueSegment implements NumberLiteralPaginationValueSegment {
private final int value;
public NumberLiteralRowNumberValueSegment(final int startIndex, final int stopIndex, final int value, final boolean boundOpened) {
super(startIndex, stopIndex, boundOpened);
this.value = value;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum;
import lombok.Getter;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.ParameterMarkerPaginationValueSegment;
/**
* Row number value segment for parameter marker.
*
* @author zhangliang
*/
@Getter
public final class ParameterMarkerRowNumberValueSegment extends RowNumberValueSegment implements ParameterMarkerPaginationValueSegment {
private final int parameterIndex;
public ParameterMarkerRowNumberValueSegment(final int startIndex, final int stopIndex, final int parameterIndex, final boolean boundOpened) {
super(startIndex, stopIndex, boundOpened);
this.parameterIndex = parameterIndex;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationValueSegment;
/**
* Row number value segment.
*
* @author zhangliang
*/
public abstract class RowNumberValueSegment extends PaginationValueSegment {
public RowNumberValueSegment(final int startIndex, final int stopIndex, final boolean boundOpened) {
super(startIndex, stopIndex, boundOpened);
}
}
......@@ -15,11 +15,12 @@
* limitations under the License.
*/
package org.apache.shardingsphere.core.parse.sql.segment.dml.limit;
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;
/**
* Top segment.
......
......@@ -29,12 +29,12 @@ import org.apache.shardingsphere.core.parse.sql.context.selectitem.DistinctSelec
import org.apache.shardingsphere.core.parse.sql.context.selectitem.SelectItem;
import org.apache.shardingsphere.core.parse.sql.context.selectitem.StarSelectItem;
import org.apache.shardingsphere.core.parse.sql.context.table.Table;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.ColumnOrderByItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.ExpressionOrderByItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.IndexOrderByItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.OrderByItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.TextOrderByItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment;
import org.apache.shardingsphere.core.parse.util.SQLUtil;
import java.util.Collection;
......@@ -71,7 +71,7 @@ public final class SelectStatement extends DQLStatement {
private int groupByLastIndex;
private LimitSegment limit;
private PaginationSegment pagination;
private SelectStatement parentStatement;
......
......@@ -29,7 +29,7 @@
<filler-rule sql-segment-class="org.apache.shardingsphere.core.parse.sql.segment.dml.WhereSegment" filler-class="org.apache.shardingsphere.core.parse.filler.common.dml.WhereFiller" />
<filler-rule sql-segment-class="org.apache.shardingsphere.core.parse.sql.segment.dml.order.GroupBySegment" filler-class="org.apache.shardingsphere.core.parse.filler.common.dml.GroupByFiller" />
<filler-rule sql-segment-class="org.apache.shardingsphere.core.parse.sql.segment.dml.order.OrderBySegment" filler-class="org.apache.shardingsphere.core.parse.filler.common.dml.OrderByFiller" />
<filler-rule sql-segment-class="org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment" filler-class="org.apache.shardingsphere.core.parse.filler.common.dml.LimitFiller" />
<filler-rule sql-segment-class="org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment" filler-class="org.apache.shardingsphere.core.parse.filler.common.dml.LimitFiller" />
<filler-rule sql-segment-class="org.apache.shardingsphere.core.parse.sql.segment.common.SchemaSegment" filler-class="org.apache.shardingsphere.core.parse.filler.common.dal.SchemaFiller" />
</filler-rule-definition>
......@@ -21,7 +21,7 @@ import org.apache.shardingsphere.core.parse.integrate.asserts.condition.Conditio
import org.apache.shardingsphere.core.parse.integrate.asserts.groupby.GroupByAssert;
import org.apache.shardingsphere.core.parse.integrate.asserts.index.IndexAssert;
import org.apache.shardingsphere.core.parse.integrate.asserts.item.ItemAssert;
import org.apache.shardingsphere.core.parse.integrate.asserts.limit.LimitAssert;
import org.apache.shardingsphere.core.parse.integrate.asserts.limit.PaginationAssert;
import org.apache.shardingsphere.core.parse.integrate.asserts.meta.TableMetaDataAssert;
import org.apache.shardingsphere.core.parse.integrate.asserts.orderby.OrderByAssert;
import org.apache.shardingsphere.core.parse.integrate.asserts.table.AlterTableAssert;
......@@ -61,7 +61,7 @@ public final class SQLStatementAssert {
private final OrderByAssert orderByAssert;
private final LimitAssert limitAssert;
private final PaginationAssert paginationAssert;
private final TableMetaDataAssert metaAssert;
......@@ -84,7 +84,7 @@ public final class SQLStatementAssert {
itemAssert = new ItemAssert(assertMessage);
groupByAssert = new GroupByAssert(assertMessage);
orderByAssert = new OrderByAssert(assertMessage);
limitAssert = new LimitAssert(sqlCaseType, assertMessage);
paginationAssert = new PaginationAssert(sqlCaseType, assertMessage);
metaAssert = new TableMetaDataAssert(assertMessage);
alterTableAssert = new AlterTableAssert(assertMessage);
this.databaseType = databaseType;
......@@ -118,7 +118,7 @@ public final class SQLStatementAssert {
itemAssert.assertItems(actual.getItems(), expected.getSelectItems());
groupByAssert.assertGroupByItems(actual.getGroupByItems(), expected.getGroupByColumns());
orderByAssert.assertOrderByItems(actual.getOrderByItems(), expected.getOrderByColumns());
limitAssert.assertLimit(actual.getLimit(), expected.getLimit());
paginationAssert.assertPagination(actual.getPagination(), expected.getLimit());
}
private void assertCreateTableStatement(final CreateTableStatement actual) {
......
......@@ -20,9 +20,9 @@ package org.apache.shardingsphere.core.parse.integrate.asserts.limit;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.parse.integrate.asserts.SQLStatementAssertMessage;
import org.apache.shardingsphere.core.parse.integrate.jaxb.limit.ExpectedLimit;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.ParameterMarkerLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLiteralPaginationValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.ParameterMarkerPaginationValueSegment;
import org.apache.shardingsphere.test.sql.SQLCaseType;
import static org.hamcrest.CoreMatchers.is;
......@@ -30,45 +30,45 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
/**
* Limit assert.
* Pagination assert.
*
* @author zhangliang
*/
@RequiredArgsConstructor
public final class LimitAssert {
public final class PaginationAssert {
private final SQLCaseType sqlCaseType;
private final SQLStatementAssertMessage assertMessage;
/**
* Assert limit.
* Assert pagination.
*
* @param actual actual limit
* @param expected expected limit
* @param actual actual pagination
* @param expected expected pagination
*/
public void assertLimit(final LimitSegment actual, final ExpectedLimit expected) {
public void assertPagination(final PaginationSegment actual, final ExpectedLimit expected) {
if (null == actual) {
assertNull(assertMessage.getFullAssertMessage("Limit should not exist: "), expected);
assertNull(assertMessage.getFullAssertMessage("Pagination should not exist: "), expected);
return;
}
if (SQLCaseType.Placeholder == sqlCaseType) {
if (actual.getOffset().isPresent()) {
assertThat(assertMessage.getFullAssertMessage("Limit offset index assertion error: "),
((ParameterMarkerLimitValueSegment) actual.getOffset().get()).getParameterIndex(), is(expected.getOffsetParameterIndex()));
assertThat(assertMessage.getFullAssertMessage("Pagination offset index assertion error: "),
((ParameterMarkerPaginationValueSegment) actual.getOffset().get()).getParameterIndex(), is(expected.getOffsetParameterIndex()));
}
if (actual.getRowCount().isPresent()) {
assertThat(assertMessage.getFullAssertMessage("Limit row count index assertion error: "),
((ParameterMarkerLimitValueSegment) actual.getRowCount().get()).getParameterIndex(), is(expected.getRowCountParameterIndex()));
assertThat(assertMessage.getFullAssertMessage("Pagination row count index assertion error: "),
((ParameterMarkerPaginationValueSegment) actual.getRowCount().get()).getParameterIndex(), is(expected.getRowCountParameterIndex()));
}
} else {
if (actual.getOffset().isPresent()) {
assertThat(assertMessage.getFullAssertMessage("Limit offset value assertion error: "),
((NumberLiteralLimitValueSegment) actual.getOffset().get()).getValue(), is(expected.getOffset()));
assertThat(assertMessage.getFullAssertMessage("Pagination offset value assertion error: "),
((NumberLiteralPaginationValueSegment) actual.getOffset().get()).getValue(), is(expected.getOffset()));
}
if (actual.getRowCount().isPresent()) {
assertThat(assertMessage.getFullAssertMessage("Limit row count value assertion error: "),
((NumberLiteralLimitValueSegment) actual.getRowCount().get()).getValue(), is(expected.getRowCount()));
assertThat(assertMessage.getFullAssertMessage("Pagination row count value assertion error: "),
((NumberLiteralPaginationValueSegment) actual.getRowCount().get()).getValue(), is(expected.getRowCount()));
}
}
}
......
......@@ -18,8 +18,8 @@
package org.apache.shardingsphere.core.rewrite.token.generator;
import com.google.common.base.Optional;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLiteralPaginationValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationValueSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.rewrite.token.pojo.OffsetToken;
......@@ -37,17 +37,18 @@ public final class OffsetTokenGenerator implements OptionalSQLTokenGenerator<Sha
if (!(sqlStatement instanceof SelectStatement)) {
return Optional.absent();
}
Optional<LimitValueSegment> offsetSegment = getLiteralOffsetSegment((SelectStatement) sqlStatement);
Optional<PaginationValueSegment> offsetSegment = getLiteralOffsetSegment((SelectStatement) sqlStatement);
return offsetSegment.isPresent()
? Optional.of(new OffsetToken(offsetSegment.get().getStartIndex(), offsetSegment.get().getStopIndex(), ((NumberLiteralLimitValueSegment) offsetSegment.get()).getValue()))
? Optional.of(new OffsetToken(offsetSegment.get().getStartIndex(), offsetSegment.get().getStopIndex(), ((NumberLiteralPaginationValueSegment) offsetSegment.get()).getValue()))
: Optional.<OffsetToken>absent();
}
private Optional<LimitValueSegment> getLiteralOffsetSegment(final SelectStatement selectStatement) {
return isLiteralOffset(selectStatement) ? selectStatement.getLimit().getOffset() : Optional.<LimitValueSegment>absent();
private Optional<PaginationValueSegment> getLiteralOffsetSegment(final SelectStatement selectStatement) {
return isLiteralOffset(selectStatement) ? selectStatement.getPagination().getOffset() : Optional.<PaginationValueSegment>absent();
}
private boolean isLiteralOffset(final SelectStatement selectStatement) {
return null != selectStatement.getLimit() && selectStatement.getLimit().getOffset().isPresent() && selectStatement.getLimit().getOffset().get() instanceof NumberLiteralLimitValueSegment;
return null != selectStatement.getPagination()
&& selectStatement.getPagination().getOffset().isPresent() && selectStatement.getPagination().getOffset().get() instanceof NumberLiteralPaginationValueSegment;
}
}
......@@ -18,8 +18,8 @@
package org.apache.shardingsphere.core.rewrite.token.generator;
import com.google.common.base.Optional;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLiteralPaginationValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationValueSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.rewrite.token.pojo.RowCountToken;
......@@ -37,17 +37,18 @@ public final class RowCountTokenGenerator implements OptionalSQLTokenGenerator<S
if (!(sqlStatement instanceof SelectStatement)) {
return Optional.absent();
}
Optional<LimitValueSegment> rowCountSegment = getLiteralRowCountSegment((SelectStatement) sqlStatement);
Optional<PaginationValueSegment> rowCountSegment = getLiteralRowCountSegment((SelectStatement) sqlStatement);
return rowCountSegment.isPresent()
? Optional.of(new RowCountToken(rowCountSegment.get().getStartIndex(), rowCountSegment.get().getStopIndex(), ((NumberLiteralLimitValueSegment) rowCountSegment.get()).getValue()))
? Optional.of(new RowCountToken(rowCountSegment.get().getStartIndex(), rowCountSegment.get().getStopIndex(), ((NumberLiteralPaginationValueSegment) rowCountSegment.get()).getValue()))
: Optional.<RowCountToken>absent();
}
private Optional<LimitValueSegment> getLiteralRowCountSegment(final SelectStatement selectStatement) {
return isLiteralRowCount(selectStatement) ? selectStatement.getLimit().getRowCount() : Optional.<LimitValueSegment>absent();
private Optional<PaginationValueSegment> getLiteralRowCountSegment(final SelectStatement selectStatement) {
return isLiteralRowCount(selectStatement) ? selectStatement.getPagination().getRowCount() : Optional.<PaginationValueSegment>absent();
}
private boolean isLiteralRowCount(final SelectStatement selectStatement) {
return null != selectStatement.getLimit() && selectStatement.getLimit().getRowCount().isPresent() && selectStatement.getLimit().getRowCount().get() instanceof NumberLiteralLimitValueSegment;
return null != selectStatement.getPagination() && selectStatement.getPagination().getRowCount().isPresent()
&& selectStatement.getPagination().getRowCount().get() instanceof NumberLiteralPaginationValueSegment;
}
}
......@@ -20,7 +20,7 @@ package org.apache.shardingsphere.core.rewrite.token.pojo;
import lombok.Getter;
/**
* Offset token for limit.
* Offset token for pagination.
*
* @author zhangliang
* @author panjuan
......
......@@ -20,7 +20,7 @@ package org.apache.shardingsphere.core.rewrite.token.pojo;
import lombok.Getter;
/**
* Row count token for limit.
* Row count token for pagination.
*
* @author zhangliang
* @author panjuan
......
......@@ -46,9 +46,10 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegme
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.item.AggregationDistinctSelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.ColumnOrderByItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum.NumberLiteralRowNumberValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.PredicateSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.DeleteStatement;
......@@ -346,8 +347,8 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForLimit() {
LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(33, 33, 2, true), new NumberLiteralLimitValueSegment(36, 36, 2, false));
selectStatement.setLimit(limitSegment);
PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralLimitValueSegment(33, 33, 2), new NumberLiteralLimitValueSegment(36, 36, 2));
selectStatement.setPagination(limitSegment);
selectStatement.getSqlSegments().add(new TableSegment(17, 23, "table_x"));
routeResult = new SQLRouteResult(selectStatement);
routeResult.setLimit(new Limit(limitSegment, Collections.emptyList()));
......@@ -358,9 +359,9 @@ public final class ShardingSQLRewriterTest {
}
@Test
public void assertRewriteForRowNum() {
LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(119, 119, 2, true), new NumberLiteralLimitValueSegment(98, 98, 4, false));
selectStatement.setLimit(limitSegment);
public void assertRewriteForRowNumber() {
PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(119, 119, 2, true), new NumberLiteralRowNumberValueSegment(98, 98, 4, false));
selectStatement.setPagination(limitSegment);
selectStatement.getSqlSegments().add(new TableSegment(68, 74, "table_x"));
routeResult = new SQLRouteResult(selectStatement);
routeResult.setLimit(new Limit(limitSegment, Collections.emptyList()));
......@@ -373,8 +374,8 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForTopAndRowNumber() {
LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(123, 123, 2, true), new NumberLiteralLimitValueSegment(26, 26, 4, false));
selectStatement.setLimit(limitSegment);
PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(123, 123, 2, true), new NumberLiteralLimitValueSegment(26, 26, 4));
selectStatement.setPagination(limitSegment);
selectStatement.getSqlSegments().add(new TableSegment(85, 91, "table_x"));
routeResult = new SQLRouteResult(selectStatement);
routeResult.setLimit(new Limit(limitSegment, Collections.emptyList()));
......@@ -387,8 +388,8 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForLimitForMemoryGroupBy() {
LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(33, 33, 2, true), new NumberLiteralLimitValueSegment(36, 36, 2, false));
selectStatement.setLimit(limitSegment);
PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralLimitValueSegment(33, 33, 2), new NumberLiteralLimitValueSegment(36, 36, 2));
selectStatement.setPagination(limitSegment);
ColumnSegment columnSegment = new ColumnSegment(0, 0, "id");
columnSegment.setOwner(new TableSegment(0, 0, "x"));
selectStatement.getOrderByItems().add(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.ASC, OrderDirection.ASC));
......@@ -404,8 +405,8 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForRowNumForMemoryGroupBy() {
LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(119, 119, 2, true), new NumberLiteralLimitValueSegment(98, 98, 4, false));
selectStatement.setLimit(limitSegment);
PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(119, 119, 2, true), new NumberLiteralRowNumberValueSegment(98, 98, 4, false));
selectStatement.setPagination(limitSegment);
selectStatement.getSqlSegments().add(new TableSegment(68, 74, "table_x"));
ColumnSegment columnSegment = new ColumnSegment(0, 0, "id");
columnSegment.setOwner(new TableSegment(0, 0, "x"));
......@@ -422,8 +423,8 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForTopAndRowNumberForMemoryGroupBy() {
LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(123, 123, 2, true), new NumberLiteralLimitValueSegment(26, 26, 4, false));
selectStatement.setLimit(limitSegment);
PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(123, 123, 2, false), new NumberLiteralLimitValueSegment(26, 26, 4));
selectStatement.setPagination(limitSegment);
selectStatement.getSqlSegments().add(new TableSegment(85, 91, "table_x"));
ColumnSegment columnSegment = new ColumnSegment(0, 0, "id");
columnSegment.setOwner(new TableSegment(0, 0, "x"));
......@@ -440,8 +441,8 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForLimitForNotRewriteLimit() {
LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(33, 33, 2, true), new NumberLiteralLimitValueSegment(36, 36, 2, false));
selectStatement.setLimit(limitSegment);
PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralLimitValueSegment(33, 33, 2), new NumberLiteralLimitValueSegment(36, 36, 2));
selectStatement.setPagination(limitSegment);
selectStatement.getSqlSegments().add(new TableSegment(17, 23, "table_x"));
routeResult = new SQLRouteResult(selectStatement);
routeResult.setLimit(new Limit(limitSegment, Collections.emptyList()));
......@@ -455,8 +456,8 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForRowNumForNotRewriteLimit() {
LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(119, 119, 2, true), new NumberLiteralLimitValueSegment(98, 98, 4, false));
selectStatement.setLimit(limitSegment);
PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(119, 119, 2, true), new NumberLiteralRowNumberValueSegment(98, 98, 4, false));
selectStatement.setPagination(limitSegment);
selectStatement.getSqlSegments().add(new TableSegment(68, 74, "table_x"));
routeResult = new SQLRouteResult(selectStatement);
routeResult.setLimit(new Limit(limitSegment, Collections.emptyList()));
......@@ -471,8 +472,8 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertRewriteForTopAndRowNumberForNotRewriteLimit() {
LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(123, 123, 2, true), new NumberLiteralLimitValueSegment(26, 26, 4, false));
selectStatement.setLimit(limitSegment);
PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(123, 123, 2, true), new NumberLiteralLimitValueSegment(26, 26, 4));
selectStatement.setPagination(limitSegment);
selectStatement.getSqlSegments().add(new TableSegment(85, 91, "table_x"));
routeResult = new SQLRouteResult(selectStatement);
routeResult.setLimit(new Limit(limitSegment, Collections.emptyList()));
......
......@@ -18,10 +18,11 @@
package org.apache.shardingsphere.core.route.limit;
import lombok.Getter;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.ParameterMarkerLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLiteralPaginationValueSegment;
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.ParameterMarkerLimitValueSegment;
import java.util.HashMap;
import java.util.List;
......@@ -41,15 +42,16 @@ public final class Limit {
private final LimitValue rowCount;
public Limit(final LimitSegment limitSegment, final List<Object> parameters) {
public Limit(final PaginationSegment limitSegment, final List<Object> parameters) {
offset = limitSegment.getOffset().isPresent() ? createLimitValue(limitSegment.getOffset().get(), parameters) : null;
rowCount = limitSegment.getRowCount().isPresent() ? createLimitValue(limitSegment.getRowCount().get(), parameters) : null;
}
private LimitValue createLimitValue(final LimitValueSegment limitValueSegment, final List<Object> parameters) {
int segmentValue = limitValueSegment instanceof ParameterMarkerLimitValueSegment
? (int) parameters.get(((ParameterMarkerLimitValueSegment) limitValueSegment).getParameterIndex()) : ((NumberLiteralLimitValueSegment) limitValueSegment).getValue();
return new LimitValue(limitValueSegment, segmentValue);
private LimitValue createLimitValue(final PaginationValueSegment paginationValueSegment, final List<Object> parameters) {
int segmentValue = paginationValueSegment instanceof ParameterMarkerPaginationValueSegment
? (int) parameters.get(((ParameterMarkerPaginationValueSegment) paginationValueSegment).getParameterIndex())
: ((NumberLiteralPaginationValueSegment) paginationValueSegment).getValue();
return new LimitValue(paginationValueSegment, segmentValue);
}
/**
......@@ -79,11 +81,11 @@ public final class Limit {
*/
public Map<Integer, Object> getRevisedParameters(final boolean isFetchAll, final String databaseType) {
Map<Integer, Object> result = new HashMap<>(2, 1);
if (null != offset && offset.getLimitValueSegment() instanceof ParameterMarkerLimitValueSegment) {
result.put(((ParameterMarkerLimitValueSegment) offset.getLimitValueSegment()).getParameterIndex(), 0);
if (null != offset && offset.getPaginationValueSegment() instanceof ParameterMarkerLimitValueSegment) {
result.put(((ParameterMarkerLimitValueSegment) offset.getPaginationValueSegment()).getParameterIndex(), 0);
}
if (null != rowCount && rowCount.getLimitValueSegment() instanceof ParameterMarkerLimitValueSegment) {
result.put(((ParameterMarkerLimitValueSegment) rowCount.getLimitValueSegment()).getParameterIndex(), getRewriteRowCount(isFetchAll, databaseType));
if (null != rowCount && rowCount.getPaginationValueSegment() instanceof ParameterMarkerLimitValueSegment) {
result.put(((ParameterMarkerLimitValueSegment) rowCount.getPaginationValueSegment()).getParameterIndex(), getRewriteRowCount(isFetchAll, databaseType));
}
return result;
}
......
......@@ -19,7 +19,7 @@ package org.apache.shardingsphere.core.route.limit;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationValueSegment;
/**
* Limit value.
......@@ -30,7 +30,7 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegm
@Getter
public final class LimitValue {
private final LimitValueSegment limitValueSegment;
private final PaginationValueSegment paginationValueSegment;
private final int value;
}
......@@ -103,8 +103,8 @@ public final class ParsingSQLRouter implements ShardingRouter {
mergeShardingValues(optimizeResult.getShardingConditions());
}
RoutingResult routingResult = RoutingEngineFactory.newInstance(shardingRule, shardingMetaData.getDataSource(), sqlStatement, optimizeResult).route();
if (sqlStatement instanceof SelectStatement && null != ((SelectStatement) sqlStatement).getLimit() && !routingResult.isSingleRouting()) {
result.setLimit(new Limit(((SelectStatement) sqlStatement).getLimit(), parameters));
if (sqlStatement instanceof SelectStatement && null != ((SelectStatement) sqlStatement).getPagination() && !routingResult.isSingleRouting()) {
result.setLimit(new Limit(((SelectStatement) sqlStatement).getPagination(), parameters));
}
if (needMerge) {
Preconditions.checkState(1 == routingResult.getRoutingUnits().size(), "Must have one sharding with subquery.");
......
......@@ -27,7 +27,7 @@ import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.metadata.ShardingMetaData;
import org.apache.shardingsphere.core.metadata.table.ShardingTableMetaData;
import org.apache.shardingsphere.core.parse.cache.ParsingResultCache;
import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.route.fixture.HintShardingAlgorithmFixture;
import org.apache.shardingsphere.core.rule.ShardingRule;
......@@ -89,17 +89,17 @@ public final class DatabaseTest {
String originSQL = "select user_id from tbl_pagination limit 0,5";
SQLRouteResult actual = new StatementRoutingEngine(shardingRule, mock(ShardingMetaData.class), DatabaseType.MySQL, new ParsingResultCache()).route(originSQL);
SelectStatement selectStatement = (SelectStatement) actual.getSqlStatement();
assertTrue(selectStatement.getLimit().getOffset().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getOffset().get()).getValue(), is(0));
assertTrue(selectStatement.getLimit().getRowCount().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getRowCount().get()).getValue(), is(5));
assertTrue(selectStatement.getPagination().getOffset().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getOffset().get()).getValue(), is(0));
assertTrue(selectStatement.getPagination().getRowCount().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getRowCount().get()).getValue(), is(5));
originSQL = "select user_id from tbl_pagination limit 5,5";
actual = new StatementRoutingEngine(shardingRule, mock(ShardingMetaData.class), DatabaseType.MySQL, new ParsingResultCache()).route(originSQL);
selectStatement = (SelectStatement) actual.getSqlStatement();
assertTrue(selectStatement.getLimit().getOffset().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getOffset().get()).getValue(), is(5));
assertTrue(selectStatement.getLimit().getRowCount().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getRowCount().get()).getValue(), is(5));
assertTrue(selectStatement.getPagination().getOffset().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getOffset().get()).getValue(), is(5));
assertTrue(selectStatement.getPagination().getRowCount().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getRowCount().get()).getValue(), is(5));
}
@Test
......@@ -119,18 +119,18 @@ public final class DatabaseTest {
when(shardingMetaData.getTable()).thenReturn(mock(ShardingTableMetaData.class));
SQLRouteResult actual = new PreparedStatementRoutingEngine(originSQL, rule, shardingMetaData, DatabaseType.MySQL, new ParsingResultCache()).route(Lists.<Object>newArrayList(13, 173));
SelectStatement selectStatement = (SelectStatement) actual.getSqlStatement();
assertTrue(selectStatement.getLimit().getOffset().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getOffset().get()).getValue(), is(5));
assertTrue(selectStatement.getLimit().getRowCount().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getRowCount().get()).getValue(), is(10));
assertTrue(selectStatement.getPagination().getOffset().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getOffset().get()).getValue(), is(5));
assertTrue(selectStatement.getPagination().getRowCount().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getRowCount().get()).getValue(), is(10));
assertThat(actual.getRoutingResult().getRoutingUnits().size(), is(1));
originSQL = "select city_id from t_user where city_id in (?,?) limit 5,10";
actual = new PreparedStatementRoutingEngine(originSQL, rule, shardingMetaData, DatabaseType.MySQL, new ParsingResultCache()).route(Lists.<Object>newArrayList(89, 84));
selectStatement = (SelectStatement) actual.getSqlStatement();
assertTrue(selectStatement.getLimit().getOffset().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getOffset().get()).getValue(), is(5));
assertTrue(selectStatement.getLimit().getRowCount().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getRowCount().get()).getValue(), is(10));
assertTrue(selectStatement.getPagination().getOffset().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getOffset().get()).getValue(), is(5));
assertTrue(selectStatement.getPagination().getRowCount().isPresent());
assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getRowCount().get()).getValue(), is(10));
assertThat(actual.getRoutingResult().getRoutingUnits().size(), is(2));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册