Skip to content

Commit

Permalink
refactor LimitExtractor
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jan 2, 2019
1 parent a960ac5 commit cff262b
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import io.shardingsphere.core.parsing.antlr.extractor.OptionalSQLSegmentExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.impl.dql.SubqueryExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.util.ExtractorUtils;
import io.shardingsphere.core.parsing.antlr.extractor.util.RuleName;
import io.shardingsphere.core.parsing.antlr.sql.segment.column.ColumnSegment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package io.shardingsphere.core.parsing.antlr.extractor.impl.dql;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.parsing.antlr.extractor.OptionalSQLSegmentExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.util.ExtractorUtils;
Expand All @@ -27,6 +26,7 @@
import io.shardingsphere.core.parsing.antlr.sql.segment.LimitValueSegment;
import io.shardingsphere.core.parsing.lexer.token.Symbol;
import io.shardingsphere.core.util.NumberUtil;
import lombok.RequiredArgsConstructor;
import org.antlr.v4.runtime.ParserRuleContext;

import java.util.HashMap;
Expand All @@ -37,10 +37,13 @@
*
* @author duhongjun
*/
public final class LimitExtractor implements OptionalSQLSegmentExtractor {
@RequiredArgsConstructor
public abstract class LimitExtractor implements OptionalSQLSegmentExtractor {

private final DatabaseType databaseType;

@Override
public Optional<LimitSegment> extract(final ParserRuleContext ancestorNode) {
public final Optional<LimitSegment> extract(final ParserRuleContext ancestorNode) {
Optional<ParserRuleContext> limitNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.LIMIT_CLAUSE);
if (!limitNode.isPresent()) {
return Optional.absent();
Expand All @@ -51,12 +54,11 @@ public Optional<LimitSegment> extract(final ParserRuleContext ancestorNode) {
}
Map<ParserRuleContext, Integer> placeholderAndNodeIndexMap = getPlaceholderAndNodeIndexMap(ancestorNode);
LimitValueSegment firstLimitValue = createLimitValueSegment(placeholderAndNodeIndexMap, (ParserRuleContext) rangeNode.get().getChild(0));
Preconditions.checkNotNull(firstLimitValue);
if (rangeNode.get().getChildCount() >= 3) {
LimitValueSegment rowCountLimitValue = createLimitValueSegment(placeholderAndNodeIndexMap, (ParserRuleContext) rangeNode.get().getChild(2));
return Optional.of(new LimitSegment(DatabaseType.MySQL, rowCountLimitValue, Optional.of(firstLimitValue)));
return Optional.of(new LimitSegment(databaseType, rowCountLimitValue, firstLimitValue));
}
return Optional.of(new LimitSegment(DatabaseType.MySQL, firstLimitValue));
return Optional.of(new LimitSegment(databaseType, firstLimitValue));
}

private Map<ParserRuleContext, Integer> getPlaceholderAndNodeIndexMap(final ParserRuleContext ancestorNode) {
Expand All @@ -69,9 +71,9 @@ private Map<ParserRuleContext, Integer> getPlaceholderAndNodeIndexMap(final Pars
}

private LimitValueSegment createLimitValueSegment(final Map<ParserRuleContext, Integer> placeholderAndNodeIndexMap, final ParserRuleContext node) {
if (node.getText().equals(Symbol.QUESTION.getLiterals())) {
return new LimitValueSegment(-1, placeholderAndNodeIndexMap.get(node.getChild(0)), ((ParserRuleContext) node.getChild(0)).getStart().getStartIndex());
}
return new LimitValueSegment(NumberUtil.getExactlyNumber(node.getText(), 10).intValue(), -1, node.getStart().getStartIndex());
return Symbol.QUESTION.getLiterals().equals(node.getText())
// FIXME check node.getChild(0) should be node.getChild(0) or node.getChild(2) ?
? new LimitValueSegment(-1, placeholderAndNodeIndexMap.get(node.getChild(0)), ((ParserRuleContext) node.getChild(0)).getStart().getStartIndex())
: new LimitValueSegment(NumberUtil.getExactlyNumber(node.getText(), 10).intValue(), -1, node.getStart().getStartIndex());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
* </p>
*/

package io.shardingsphere.core.parsing.antlr.extractor.impl;
package io.shardingsphere.core.parsing.antlr.extractor.impl.dql;

import com.google.common.base.Optional;
import io.shardingsphere.core.parsing.antlr.extractor.OptionalSQLSegmentExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.impl.FromWhereExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.util.ExtractorUtils;
import io.shardingsphere.core.parsing.antlr.extractor.util.RuleName;
import io.shardingsphere.core.parsing.antlr.sql.segment.FromWhereSegment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
* </p>
*/

package io.shardingsphere.core.parsing.antlr.extractor.impl;
package io.shardingsphere.core.parsing.antlr.extractor.impl.dql;

import com.google.common.base.Optional;
import io.shardingsphere.core.parsing.antlr.extractor.OptionalSQLSegmentExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.impl.dql.GroupByExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.impl.dql.OrderByExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.impl.dql.SelectClauseExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.impl.FromWhereExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.util.ExtractorUtils;
import io.shardingsphere.core.parsing.antlr.extractor.util.RuleName;
import io.shardingsphere.core.parsing.antlr.sql.segment.FromWhereSegment;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed 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.
* </p>
*/

package io.shardingsphere.core.parsing.antlr.extractor.impl.dql.dialect.mysql;

import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.parsing.antlr.extractor.impl.dql.LimitExtractor;

/**
* Limit extractor for MySQL.
*
* @author zhangliang
*/
public final class MySQLLimitExtractor extends LimitExtractor {

public MySQLLimitExtractor() {
super(DatabaseType.MySQL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.shardingsphere.core.constant.AggregationType;
import io.shardingsphere.core.metadata.table.ShardingTableMetaData;
import io.shardingsphere.core.parsing.antlr.filler.SQLStatementFiller;
import io.shardingsphere.core.parsing.antlr.filler.impl.dql.SubqueryFiller;
import io.shardingsphere.core.parsing.antlr.sql.segment.SQLSegment;
import io.shardingsphere.core.parsing.antlr.sql.segment.expr.CommonExpressionSegment;
import io.shardingsphere.core.parsing.antlr.sql.segment.expr.FunctionExpressionSegment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
* </p>
*/

package io.shardingsphere.core.parsing.antlr.filler.impl;
package io.shardingsphere.core.parsing.antlr.filler.impl.dql;

import io.shardingsphere.core.metadata.table.ShardingTableMetaData;
import io.shardingsphere.core.parsing.antlr.filler.SQLStatementFiller;
import io.shardingsphere.core.parsing.antlr.filler.impl.OrConditionFiller;
import io.shardingsphere.core.parsing.antlr.sql.segment.condition.OrConditionSegment;
import io.shardingsphere.core.parsing.antlr.sql.segment.condition.SubQueryConditionSegment;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
* </p>
*/

package io.shardingsphere.core.parsing.antlr.filler.impl;
package io.shardingsphere.core.parsing.antlr.filler.impl.dql;

import io.shardingsphere.core.metadata.table.ShardingTableMetaData;
import io.shardingsphere.core.parsing.antlr.filler.SQLStatementFiller;
import io.shardingsphere.core.parsing.antlr.filler.impl.dql.GroupByFiller;
import io.shardingsphere.core.parsing.antlr.filler.impl.dql.OrderByFiller;
import io.shardingsphere.core.parsing.antlr.filler.impl.dql.SelectClauseFiller;
import io.shardingsphere.core.parsing.antlr.filler.impl.FromWhereFiller;
import io.shardingsphere.core.parsing.antlr.sql.segment.expr.SubquerySegment;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import io.shardingsphere.core.parsing.parser.sql.dql.select.SelectStatement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,33 @@
import com.google.common.base.Optional;
import io.shardingsphere.core.constant.DatabaseType;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Limit segment.
*
* @author duhongjun
*/
@RequiredArgsConstructor
@Getter
public final class LimitSegment implements SQLSegment {

private final DatabaseType databaseType;

private final LimitValueSegment rowCount;

private final Optional<LimitValueSegment> offset;
private final LimitValueSegment offset;

public LimitSegment(final DatabaseType databaseType, final LimitValueSegment rowCount, final Optional<LimitValueSegment> offset) {
this.databaseType = databaseType;
this.rowCount = rowCount;
this.offset = offset;
public LimitSegment(final DatabaseType databaseType, final LimitValueSegment rowCount) {
this(databaseType, rowCount, null);
}

public LimitSegment(final DatabaseType databaseType, final LimitValueSegment rowCount) {
this.databaseType = databaseType;
this.rowCount = rowCount;
this.offset = Optional.absent();
/**
* Get offset.
*
* @return offset
*/
public Optional<LimitValueSegment> getOffset() {
return Optional.fromNullable(offset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@
<extractor-rule id="fromWhere" extractor-class="io.shardingsphere.core.parsing.antlr.extractor.impl.FromWhereExtractor" />
<extractor-rule id="groupBy" extractor-class="io.shardingsphere.core.parsing.antlr.extractor.impl.dql.GroupByExtractor" />
<extractor-rule id="orderBy" extractor-class="io.shardingsphere.core.parsing.antlr.extractor.impl.dql.OrderByExtractor" />
<extractor-rule id="limit" extractor-class="io.shardingsphere.core.parsing.antlr.extractor.impl.dql.LimitExtractor" />
<extractor-rule id="subQueryCondition" extractor-class="io.shardingsphere.core.parsing.antlr.extractor.impl.SubQueryConditionExtractor" />
<extractor-rule id="subQueryCondition" extractor-class="io.shardingsphere.core.parsing.antlr.extractor.impl.dql.SubQueryConditionExtractor" />
</extractor-rule-definition>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<filler-rule sql-segment-class="io.shardingsphere.core.parsing.antlr.sql.segment.definition.constraint.DropPrimaryKeySegment" filler-class="io.shardingsphere.core.parsing.antlr.filler.impl.ddl.alter.DropPrimaryKeyFiller" />
<filler-rule sql-segment-class="io.shardingsphere.core.parsing.antlr.sql.segment.SelectClauseSegment" filler-class="io.shardingsphere.core.parsing.antlr.filler.impl.dql.SelectClauseFiller" />
<filler-rule sql-segment-class="io.shardingsphere.core.parsing.antlr.sql.segment.FromWhereSegment" filler-class="io.shardingsphere.core.parsing.antlr.filler.impl.FromWhereFiller" />
<filler-rule sql-segment-class="io.shardingsphere.core.parsing.antlr.sql.segment.condition.SubQueryConditionSegment" filler-class="io.shardingsphere.core.parsing.antlr.filler.impl.SubQueryConditionFiller" />
<filler-rule sql-segment-class="io.shardingsphere.core.parsing.antlr.sql.segment.condition.SubQueryConditionSegment" filler-class="io.shardingsphere.core.parsing.antlr.filler.impl.dql.SubQueryConditionFiller" />
<filler-rule sql-segment-class="io.shardingsphere.core.parsing.antlr.sql.segment.order.GroupBySegment" filler-class="io.shardingsphere.core.parsing.antlr.filler.impl.dql.GroupByFiller" />
<filler-rule sql-segment-class="io.shardingsphere.core.parsing.antlr.sql.segment.order.OrderBySegment" filler-class="io.shardingsphere.core.parsing.antlr.filler.impl.dql.OrderByFiller" />
<filler-rule sql-segment-class="io.shardingsphere.core.parsing.antlr.sql.segment.LimitSegment" filler-class="io.shardingsphere.core.parsing.antlr.filler.impl.dql.LimitFiller" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<extractor-rule id="modifyColumnDefinition" extractor-class="io.shardingsphere.core.parsing.antlr.extractor.impl.ddl.column.dialect.mysql.MySQLModifyColumnDefinitionExtractor" />
<extractor-rule id="changeColumnDefinition" extractor-class="io.shardingsphere.core.parsing.antlr.extractor.impl.ddl.column.dialect.mysql.MySQLChangeColumnDefinitionExtractor" />
<extractor-rule id="dropPrimaryKey" extractor-class="io.shardingsphere.core.parsing.antlr.extractor.impl.ddl.constraint.dialect.mysql.MySQLDropPrimaryKeyExtractor" />
<extractor-rule id="limit" extractor-class="io.shardingsphere.core.parsing.antlr.extractor.impl.dql.dialect.mysql.MySQLLimitExtractor" />
<extractor-rule id="setAutoCommit" extractor-class="io.shardingsphere.core.parsing.antlr.extractor.impl.SetAutoCommitExtractor" />
</extractor-rule-definition>

0 comments on commit cff262b

Please sign in to comment.