Commit 18544d71 authored by terrymanu's avatar terrymanu
Browse files

for #2084, StarSelectItemSegmentExtractor => ShorthandSelectItemExtractor

parent de982303
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ public final class SelectItemsExtractor implements OptionalSQLSegmentExtractor {
    @Override
    public Optional<SelectItemsSegment> extract(final ParserRuleContext ancestorNode) {
        ParserRuleContext selectItemsNode = ExtractorUtils.getFirstChildNode(ancestorNode, RuleName.SELECT_ITEMS);
        SelectItemsSegment result = new SelectItemsSegment(selectItemsNode.getStart().getStartIndex(), selectItemsNode.getStop().getStopIndex(), hasDistinct(ancestorNode));
        SelectItemsSegment result = new SelectItemsSegment(selectItemsNode.getStart().getStartIndex(), selectItemsNode.getStop().getStopIndex(), extractDistinct(ancestorNode));
        Optional<ParserRuleContext> unqualifiedShorthandNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.UNQUALIFIED_SHORTHAND);
        if (unqualifiedShorthandNode.isPresent()) {
            setUnqualifiedShorthandSelectItemSegment(unqualifiedShorthandNode.get(), result);
@@ -64,7 +64,7 @@ public final class SelectItemsExtractor implements OptionalSQLSegmentExtractor {
        }
    }
    
    private boolean hasDistinct(final ParserRuleContext ancestorNode) {
    private boolean extractDistinct(final ParserRuleContext ancestorNode) {
        Optional<ParserRuleContext> duplicateSpecificationNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.DUPLICATE_SPECIFICATION);
        return duplicateSpecificationNode.isPresent()
                && (duplicateSpecificationNode.get().getText().equalsIgnoreCase("DISTINCT") || duplicateSpecificationNode.get().getText().equalsIgnoreCase("DISTINCTROW"));
+2 −2
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.item.SelectIte
 */
public final class SelectItemExtractor implements OptionalSQLSegmentExtractor {
    
    private final StarSelectItemSegmentExtractor starItemExpressionExtractor = new StarSelectItemSegmentExtractor();
    private final ShorthandSelectItemExtractor shorthandSelectItemExtractor = new ShorthandSelectItemExtractor();
    
    private final ColumnSelectItemSegmentExtractor columnSelectItemSegmentExtractor = new ColumnSelectItemSegmentExtractor();
    
@@ -43,7 +43,7 @@ public final class SelectItemExtractor implements OptionalSQLSegmentExtractor {
    @Override
    public Optional<? extends SelectItemSegment> extract(final ParserRuleContext expressionNode) {
        Optional<? extends SelectItemSegment> result;
        result = starItemExpressionExtractor.extract(expressionNode);
        result = shorthandSelectItemExtractor.extract(expressionNode);
        if (result.isPresent()) {
            return result;
        }
+14 −9
Original line number Diff line number Diff line
@@ -20,24 +20,29 @@ package org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.ite
import com.google.common.base.Optional;
import org.antlr.v4.runtime.ParserRuleContext;
import org.apache.shardingsphere.core.parse.antlr.extractor.api.OptionalSQLSegmentExtractor;
import org.apache.shardingsphere.core.parse.antlr.extractor.util.ExtractorUtils;
import org.apache.shardingsphere.core.parse.antlr.extractor.util.RuleName;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.item.StarSelectItemSegment;
import org.apache.shardingsphere.core.parse.old.lexer.token.Symbol;

/**
 * Star select item segment extractor.
 * Shorthand select item extractor.
 *
 * @author zhangliang
 */
public final class StarSelectItemSegmentExtractor implements OptionalSQLSegmentExtractor {
public final class ShorthandSelectItemExtractor implements OptionalSQLSegmentExtractor {
    
    @Override
    public Optional<StarSelectItemSegment> extract(final ParserRuleContext expressionNode) {
        String text = expressionNode.getText();
        if (!text.endsWith(Symbol.STAR.getLiterals())) {
            return Optional.absent();
        Optional<ParserRuleContext> unqualifiedShorthandNode = ExtractorUtils.findFirstChildNode(expressionNode, RuleName.UNQUALIFIED_SHORTHAND);
        if (unqualifiedShorthandNode.isPresent()) {
            return Optional.of(new StarSelectItemSegment(unqualifiedShorthandNode.get().getStart().getStartIndex()));
        }
        StarSelectItemSegment result = new StarSelectItemSegment(expressionNode.getStart().getStartIndex());
        result.setOwner(text.contains(Symbol.DOT.getLiterals()) ? text.substring(0, text.indexOf(Symbol.DOT.getLiterals())) : null);
        Optional<ParserRuleContext> qualifiedShorthandNode = ExtractorUtils.findFirstChildNode(expressionNode, RuleName.QUALIFIED_SHORTHAND);
        if (qualifiedShorthandNode.isPresent()) {
            StarSelectItemSegment result = new StarSelectItemSegment(qualifiedShorthandNode.get().getStart().getStartIndex());
            result.setOwner(qualifiedShorthandNode.get().getChild(0).getText());
            return Optional.of(result);
        }
        return Optional.absent();
    }
}