提交 cabbee11 编写于 作者: T terrymanu

for #2084, use 3rd parse engine to parse limit for pg

上级 333e33f5
......@@ -47,18 +47,14 @@ public final class LimitExtractor implements OptionalSQLSegmentExtractor {
@Override
public Optional<LimitSegment> extract(final ParserRuleContext ancestorNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
Optional<ParserRuleContext> limitNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.LIMIT_CLAUSE);
if (!limitNode.isPresent()) {
return Optional.absent();
}
LimitValueSegment rowCount = extractRowCount(limitNode.get(), parameterMarkerIndexes);
Optional<LimitValueSegment> offset = extractOffset(limitNode.get(), parameterMarkerIndexes);
return offset.isPresent() ? Optional.of(new LimitSegment(rowCount, offset.get())) : Optional.of(new LimitSegment(rowCount));
return limitNode.isPresent()
? Optional.of(new LimitSegment(extractRowCount(limitNode.get(), parameterMarkerIndexes).orNull(), extractOffset(limitNode.get(), parameterMarkerIndexes).orNull()))
: Optional.<LimitSegment>absent();
}
private LimitValueSegment extractRowCount(final ParserRuleContext limitNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
private Optional<LimitValueSegment> extractRowCount(final ParserRuleContext limitNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
Optional<ParserRuleContext> rowCountNode = ExtractorUtils.findFirstChildNode(limitNode, RuleName.LIMIT_ROW_COUNT);
Preconditions.checkState(rowCountNode.isPresent());
return extractLimitValue(rowCountNode.get(), parameterMarkerIndexes);
return rowCountNode.isPresent() ? Optional.of(extractLimitValue(rowCountNode.get(), parameterMarkerIndexes)) : Optional.<LimitValueSegment>absent();
}
private Optional<LimitValueSegment> extractOffset(final ParserRuleContext limitNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
......
......@@ -40,7 +40,9 @@ public final class LimitFiller implements SQLSegmentFiller<LimitSegment> {
public void fill(final LimitSegment sqlSegment, final SQLStatement sqlStatement) {
SelectStatement selectStatement = (SelectStatement) sqlStatement;
selectStatement.setLimit(new Limit());
fillRowCount(sqlSegment.getRowCount(), selectStatement);
if (sqlSegment.getRowCount().isPresent()) {
fillRowCount(sqlSegment.getRowCount().get(), selectStatement);
}
if (sqlSegment.getOffset().isPresent()) {
fillOffset(sqlSegment.getOffset().get(), selectStatement);
}
......
......@@ -18,7 +18,6 @@
package org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.limit;
import com.google.common.base.Optional;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.SQLSegment;
......@@ -28,15 +27,19 @@ import org.apache.shardingsphere.core.parse.antlr.sql.segment.SQLSegment;
* @author duhongjun
*/
@RequiredArgsConstructor
@Getter
public final class LimitSegment implements SQLSegment {
private final LimitValueSegment rowCount;
private final LimitValueSegment offset;
public LimitSegment(final LimitValueSegment rowCount) {
this(rowCount, null);
/**
* Get row count.
*
* @return row count
*/
public Optional<LimitValueSegment> getRowCount() {
return Optional.fromNullable(rowCount);
}
/**
......
......@@ -149,15 +149,23 @@ havingClause
: HAVING expr
;
// TODO limitRowCount can be null on PG
limitClause
: (LIMIT (ALL | limitRowCount)) (OFFSET limitOffset (ROW | ROWS)?)?
: limitRowCountSyntax_ limitOffsetSyntax_?
| limitOffsetSyntax_ limitRowCountSyntax_?
;
limitRowCountSyntax_
: LIMIT (ALL | limitRowCount)
;
limitRowCount
: numberLiterals | parameterMarker
;
limitOffsetSyntax_
: OFFSET limitOffset (ROW | ROWS)?
;
limitOffset
: numberLiterals | parameterMarker
;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册