提交 e7340f90 编写于 作者: J Juan Pan(Trista) 提交者: GitHub

support using limit pagination (#4222)

* add more tests for order-by and or sql

* support using limit pagination
上级 0d5bdd22
......@@ -31,6 +31,9 @@ import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.FromCla
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.InsertContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.InsertValuesClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.JoinedTableContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.LimitClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.LimitOffsetContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.LimitRowCountContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.MultipleTableNamesContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.MultipleTablesClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.OnDuplicateKeyClauseContext;
......@@ -65,6 +68,10 @@ import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ProjectionSegme
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ProjectionsSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ShorthandProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.order.OrderBySegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.PaginationValueSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.limit.ParameterMarkerLimitValueSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.OrPredicateSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.PredicateSegment;
......@@ -74,9 +81,11 @@ import org.apache.shardingsphere.sql.parser.sql.statement.dml.DeleteStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.UpdateStatement;
import org.apache.shardingsphere.sql.parser.sql.value.literal.impl.BooleanLiteralValue;
import org.apache.shardingsphere.sql.parser.sql.value.collection.CollectionValue;
import org.apache.shardingsphere.sql.parser.sql.value.literal.impl.BooleanLiteralValue;
import org.apache.shardingsphere.sql.parser.sql.value.literal.impl.NumberLiteralValue;
import org.apache.shardingsphere.sql.parser.sql.value.literal.impl.StringLiteralValue;
import org.apache.shardingsphere.sql.parser.sql.value.parametermarker.ParameterMarkerValue;
import java.util.Collection;
import java.util.LinkedList;
......@@ -285,6 +294,9 @@ public final class MySQLDMLVisitor extends MySQLVisitor {
result.setOrderBy(orderBy);
result.getAllSQLSegments().add(orderBy);
}
if (null != ctx.limitClause()) {
result.getAllSQLSegments().add((LimitSegment) visit(ctx.limitClause()));
}
return result;
}
......@@ -436,4 +448,37 @@ public final class MySQLDMLVisitor extends MySQLVisitor {
result.setParametersCount(getCurrentParameterIndex());
return result;
}
@Override
public ASTNode visitLimitClause(final LimitClauseContext ctx) {
if (null == ctx.limitOffset()) {
return new LimitSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), null, (PaginationValueSegment) visit(ctx.limitRowCount()));
}
PaginationValueSegment rowCount;
PaginationValueSegment limitOffset;
if (null != ctx.OFFSET()) {
rowCount = (PaginationValueSegment) visit(ctx.limitRowCount());
limitOffset = (PaginationValueSegment) visit(ctx.limitOffset());
} else {
limitOffset = (PaginationValueSegment) visit(ctx.limitOffset());
rowCount = (PaginationValueSegment) visit(ctx.limitRowCount());
}
return new LimitSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), limitOffset, rowCount);
}
@Override
public ASTNode visitLimitRowCount(final LimitRowCountContext ctx) {
if (null != ctx.numberLiterals()) {
return new NumberLiteralLimitValueSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), ((NumberLiteralValue) visit(ctx.numberLiterals())).getValue().longValue());
}
return new ParameterMarkerLimitValueSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), ((ParameterMarkerValue) visit(ctx.parameterMarker())).getValue());
}
@Override
public ASTNode visitLimitOffset(final LimitOffsetContext ctx) {
if (null != ctx.numberLiterals()) {
return new NumberLiteralLimitValueSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), ((NumberLiteralValue) visit(ctx.numberLiterals())).getValue().longValue());
}
return new ParameterMarkerLimitValueSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), ((ParameterMarkerValue) visit(ctx.parameterMarker())).getValue());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<sql-parser-test-cases>
<select sql-case-id="select_order_by_asc_and_index_desc">
<table name="t_order" alias="o" start-index="14" stop-index="20" />
<projections start-index="7" stop-index="7">
<shorthand-projection start-index="7" stop-index="7" />
</projections>
<order-by>
<column-item name="order_id">
<owner name="o" start-index="33" stop-index="33" />
</column-item>
<index-item index="2" order-direction="DESC" />
</order-by>
</select>
<select sql-case-id="select_order_by_desc_and_index_asc">
<table name="t_order" alias="o" start-index="16" stop-index="22" />
<table name="t_order_item" alias="i" start-index="27" stop-index="38" />
<projections start-index="7" stop-index="9">
<shorthand-projection start-index="7" stop-index="9">
<owner name="i" start-index="7" stop-index="7" />
</shorthand-projection>
</projections>
<where parameters-count="0" start-index="42" stop-index="92">
<and-predicate>
<predicate start-index="48" stop-index="70">
<column-left-value name="order_id" start-index="48" stop-index="57">
<owner name="o" start-index="48" stop-index="48" />
</column-left-value>
<operator type="=" />
<column-right-value name="order_id" start-index="61" stop-index="70">
<owner name="i" start-index="61" stop-index="61" />
</column-right-value>
</predicate>
<predicate start-index="76" stop-index="92">
<column-left-value name="status" start-index="76" stop-index="83">
<owner name="o" start-index="76" stop-index="76" />
</column-left-value>
<operator type="=" />
<compare-right-value>
<parameter-marker-expression value="0" />
<literal-expression value="init" />
</compare-right-value>
</predicate>
</and-predicate>
</where>
<order-by>
<column-item name="order_id" order-direction="DESC">
<owner name="o" start-index="103" stop-index="103" />
</column-item>
<index-item index="1" />
</order-by>
</select>
<select sql-case-id="select_order_by_with_ordered_column">
<table name="t_order" alias="o" start-index="40" stop-index="46" />
<projections start-index="7" stop-index="33">
<column-projection name="order_id" alias="gen_order_id_" start-index="7" stop-index="16">
<owner name="o" start-index="7" stop-index="7" />
</column-projection>
</projections>
<order-by>
<column-item name="order_id">
<owner name="o" start-index="59" stop-index="59" />
</column-item>
</order-by>
</select>
<select sql-case-id="select_order_by_with_date">
<table name="t_order" alias="o" start-index="16" stop-index="22" />
<table name="t_order_item" alias="i" start-index="27" stop-index="38" />
<projections start-index="7" stop-index="9">
<shorthand-projection start-index="7" stop-index="9">
<owner name="i" start-index="7" stop-index="7" />
</shorthand-projection>
</projections>
<where parameters-count="0" start-index="42" stop-index="92">
<and-predicate>
<predicate start-index="48" stop-index="70">
<column-left-value name="order_id" start-index="48" stop-index="57">
<owner name="o" start-index="48" stop-index="48" />
</column-left-value>
<operator type="=" />
<column-right-value name="order_id" start-index="61" stop-index="70">
<owner name="i" start-index="61" stop-index="61" />
</column-right-value>
</predicate>
<predicate start-index="76" stop-index="92">
<column-left-value name="status" start-index="76" stop-index="83">
<owner name="o" start-index="76" stop-index="76" />
</column-left-value>
<operator type="=" />
<compare-right-value>
<literal-expression value="init" />
</compare-right-value>
</predicate>
</and-predicate>
</where>
<order-by>
<column-item name="creation_date" order-direction="DESC">
<owner name="i" start-index="103" stop-index="103" />
</column-item>
<column-item name="order_id" order-direction="DESC">
<owner name="o" start-index="125" stop-index="125" />
</column-item>
<column-item name="item_id">
<owner name="i" start-index="142" stop-index="142" />
</column-item>
</order-by>
</select>
<!-- //TODO add order-by-null-type -->
<select sql-case-id="select_order_by_for_nulls_first">
<table name="t_order" alias="o" start-index="40" stop-index="46" />
<projections start-index="7" stop-index="33">
<column-projection name="order_id" alias="gen_order_id_" start-index="7" stop-index="16">
<owner name="o" start-index="7" stop-index="7" />
</column-projection>
</projections>
<order-by>
<column-item name="order_id">
<owner name="o" start-index="59" stop-index="59" />
</column-item>
</order-by>
</select>
<!-- //TODO add order-by-null-type -->
<select sql-case-id="select_order_by_for_nulls_last">
<table name="t_order" alias="o" start-index="40" stop-index="46" />
<projections start-index="7" stop-index="33">
<column-projection name="order_id" alias="gen_order_id_" start-index="7" stop-index="16">
<owner name="o" start-index="7" stop-index="7" />
</column-projection>
</projections>
<order-by>
<column-item name="order_id">
<owner name="o" start-index="59" stop-index="59" />
</column-item>
</order-by>
</select>
<select sql-case-id="select_order_by_with_multiple_stars">
<table name="t_order" alias="o" start-index="29" stop-index="35" />
<projections start-index="7" stop-index="22">
<shorthand-projection start-index="7" stop-index="7" />
<shorthand-projection start-index="20" stop-index="22">
<owner name="o" start-index="20" stop-index="20" />
</shorthand-projection>
<column-projection name="order_id" start-index="10" stop-index="17" />
</projections>
<order-by>
<column-item name="order_id">
<owner name="o" start-index="48" stop-index="48" />
</column-item>
</order-by>
</select>
<select sql-case-id="select_order_by_with_alias_star_alias_name">
<table name="t_order" alias="o" start-index="16" stop-index="22" />
<projections start-index="7" stop-index="9">
<shorthand-projection start-index="7" stop-index="9">
<owner name="o" start-index="7" stop-index="7" />
</shorthand-projection>
</projections>
<order-by>
<column-item name="order_id">
<owner name="o" start-index="35" stop-index="35" />
</column-item>
</order-by>
</select>
<select sql-case-id="select_order_by_with_star_table_alias">
<table name="t_order" alias="o" start-index="14" stop-index="20" />
<projections start-index="7" stop-index="7">
<shorthand-projection start-index="7" stop-index="7" />
</projections>
<order-by>
<column-item name="order_id" />
</order-by>
</select>
<!--TODO need to add later-->
<!--<select sql-case-id="select_order_by_with_parameter" parameters="order_id">-->
<!--<table name="t_order" alias="o" start-index="14" stop-index="20" />-->
<!--</select>-->
<select sql-case-id="select_order_by_with_table_star_table_name">
<table name="t_order" start-index="22" stop-index="28" />
<projections start-index="7" stop-index="15">
<shorthand-projection start-index="7" stop-index="15">
<owner name="t_order" start-index="7" stop-index="13" />
</shorthand-projection>
</projections>
<order-by>
<column-item name="order_id">
<owner name="t_order" start-index="39" stop-index="45" />
</column-item>
</order-by>
</select>
<select sql-case-id="select_order_by_with_star_no_table_alias">
<table name="t_order" start-index="14" stop-index="20" />
<projections start-index="7" stop-index="7">
<shorthand-projection start-index="7" stop-index="7" />
</projections>
<order-by>
<column-item name="order_id" />
</order-by>
</select>
<select sql-case-id="select_order_by_with_table_star_without_table_name">
<table name="t_order" alias="o" start-index="21" stop-index="27" />
<table name="t_order_item" alias="i" start-index="36" stop-index="47" />
<projections start-index="7" stop-index="14">
<shorthand-projection start-index="7" stop-index="9">
<owner name="i" start-index="7" stop-index="7" />
</shorthand-projection>
<shorthand-projection start-index="12" stop-index="14">
<owner name="o" start-index="12" stop-index="12" />
</shorthand-projection>
</projections>
<order-by>
<column-item name="item_id" />
</order-by>
</select>
</sql-parser-test-cases>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册