Commit 75e71edb authored by codefairy08's avatar codefairy08
Browse files

Merge branch 'dev' of https://github.com/apache/incubator-shardingsphere into dev_bug_fixs_2211

parents e0838fbc 3d70b7d2
Loading
Loading
Loading
Loading
+12 −18
Original line number Diff line number Diff line
@@ -15,14 +15,13 @@
 * limitations under the License.
 */

package org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select;
package org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml;

import com.google.common.base.Optional;
import lombok.Setter;
import org.antlr.v4.runtime.ParserRuleContext;
import org.apache.shardingsphere.core.parse.antlr.extractor.api.OptionalSQLSegmentExtractor;
import org.apache.shardingsphere.core.parse.antlr.extractor.api.PlaceholderIndexesAware;
import org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.PredicateExtractor;
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.WhereSegment;
@@ -32,31 +31,33 @@ import java.util.Collection;
import java.util.Map;

/**
 * Abstract where extractor.
 * Where extractor.
 *
 * @author duhongjun
 * @author zhangliang
 */
@Setter
public abstract class AbstractWhereExtractor implements OptionalSQLSegmentExtractor, PlaceholderIndexesAware {
public final class WhereExtractor implements OptionalSQLSegmentExtractor, PlaceholderIndexesAware {
    
    private final PredicateExtractor predicateExtractor = new PredicateExtractor();
    
    private Map<ParserRuleContext, Integer> placeholderIndexes;
    
    @Override
    public final Optional<WhereSegment> extract(final ParserRuleContext ancestorNode) {
    public Optional<WhereSegment> extract(final ParserRuleContext ancestorNode) {
        WhereSegment result = new WhereSegment();
        result.setParameterCount(placeholderIndexes.size());
        Optional<ParserRuleContext> whereNode = extractWhere(ancestorNode);
        Optional<ParserRuleContext> whereNode = ExtractorUtils.findFirstChildNodeNoneRecursive(ancestorNode, RuleName.WHERE_CLAUSE);
        if (whereNode.isPresent()) {
            setPropertiesForRevert(result, placeholderIndexes, whereNode.get());
            extractAndFillWhere(result, placeholderIndexes, whereNode.get());
            Optional<OrConditionSegment> orConditionSegment = extractOrConditionSegment(placeholderIndexes, whereNode.get());
            if (orConditionSegment.isPresent()) {
                result.getConditions().getAndConditions().addAll(orConditionSegment.get().getAndConditions());
            }
        }
        return Optional.of(result);
    }
    
    protected abstract Optional<ParserRuleContext> extractWhere(ParserRuleContext ancestorNode);
    
    private void setPropertiesForRevert(final WhereSegment whereSegment, final Map<ParserRuleContext, Integer> placeholderIndexes, final ParserRuleContext whereNode) {
        whereSegment.setWhereStartIndex(whereNode.getStart().getStartIndex());
        whereSegment.setWhereStopIndex(whereNode.getStop().getStopIndex());
@@ -72,15 +73,8 @@ public abstract class AbstractWhereExtractor implements OptionalSQLSegmentExtrac
        whereSegment.setWhereParameterEndIndex(whereParameterStartIndex + questionNodes.size() - 1);
    }
    
    private void extractAndFillWhere(final WhereSegment whereSegment, final Map<ParserRuleContext, Integer> placeholderIndexes, final ParserRuleContext whereNode) {
        Optional<OrConditionSegment> conditions = buildCondition((ParserRuleContext) whereNode.getChild(1), placeholderIndexes);
        if (conditions.isPresent()) {
            whereSegment.getConditions().getAndConditions().addAll(conditions.get().getAndConditions());
        }
    }
    
    private Optional<OrConditionSegment> buildCondition(final ParserRuleContext node, final Map<ParserRuleContext, Integer> placeholderIndexes) {
        Optional<ParserRuleContext> exprNode = ExtractorUtils.findFirstChildNode(node, RuleName.EXPR);
    private Optional<OrConditionSegment> extractOrConditionSegment(final Map<ParserRuleContext, Integer> placeholderIndexes, final ParserRuleContext whereNode) {
        Optional<ParserRuleContext> exprNode = ExtractorUtils.findFirstChildNode((ParserRuleContext) whereNode.getChild(1), RuleName.EXPR);
        return exprNode.isPresent() ? predicateExtractor.extract(placeholderIndexes, exprNode.get()) : Optional.<OrConditionSegment>absent();
    }
}
+0 −37
Original line number Diff line number Diff line
/*
 * 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.antlr.extractor.impl.dml.delete;

import com.google.common.base.Optional;
import org.antlr.v4.runtime.ParserRuleContext;
import org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.AbstractWhereExtractor;
import org.apache.shardingsphere.core.parse.antlr.extractor.util.ExtractorUtils;
import org.apache.shardingsphere.core.parse.antlr.extractor.util.RuleName;

/**
 * Where extractor for delete.
 *
 * @author duhongjun
 */
public final class DeleteWhereExtractor extends AbstractWhereExtractor {
    
    @Override
    protected Optional<ParserRuleContext> extractWhere(final ParserRuleContext ancestorNode) {
        return ExtractorUtils.findFirstChildNodeNoneRecursive(ancestorNode, RuleName.WHERE_CLAUSE);
    }
}
+12 −12
Original line number Diff line number Diff line
@@ -23,23 +23,23 @@ import org.apache.shardingsphere.core.parse.antlr.extractor.api.OptionalSQLSegme
import org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.item.SelectItemExtractor;
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.SelectClauseSegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.SelectItemsSegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.item.SelectItemSegment;

/**
 * Select clause extractor.
 * Select items extractor.
 *
 * @author duhongjun
 * @author panjuan
 */
public final class SelectClauseExtractor implements OptionalSQLSegmentExtractor {
public final class SelectItemsExtractor implements OptionalSQLSegmentExtractor {
    
    private final SelectItemExtractor selectItemExtractor = new SelectItemExtractor();
    
    @Override
    public Optional<SelectClauseSegment> extract(final ParserRuleContext ancestorNode) {
    public Optional<SelectItemsSegment> extract(final ParserRuleContext ancestorNode) {
        ParserRuleContext selectItemsNode = ExtractorUtils.getFirstChildNode(ancestorNode, RuleName.SELECT_ITEMS);
        SelectClauseSegment result = new SelectClauseSegment(selectItemsNode.getStart().getStartIndex(), selectItemsNode.getStop().getStopIndex(), hasDistinct(ancestorNode));
        SelectItemsSegment result = new SelectItemsSegment(selectItemsNode.getStart().getStartIndex(), selectItemsNode.getStop().getStopIndex(), hasDistinct(ancestorNode));
        Optional<ParserRuleContext> unqualifiedShorthandNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.UNQUALIFIED_SHORTHAND);
        if (unqualifiedShorthandNode.isPresent()) {
            setUnqualifiedShorthandSelectItemSegment(unqualifiedShorthandNode.get(), result);
@@ -48,23 +48,23 @@ public final class SelectClauseExtractor implements OptionalSQLSegmentExtractor
        return Optional.of(result);
    }
    
    private void setUnqualifiedShorthandSelectItemSegment(final ParserRuleContext unqualifiedShorthandNode, final SelectClauseSegment selectClauseSegment) {
    private void setUnqualifiedShorthandSelectItemSegment(final ParserRuleContext unqualifiedShorthandNode, final SelectItemsSegment selectItemsSegment) {
        Optional<? extends SelectItemSegment> unqualifiedShorthandSelectItemSegment = selectItemExtractor.extract(unqualifiedShorthandNode);
        if (unqualifiedShorthandSelectItemSegment.isPresent()) {
            selectClauseSegment.getSelectItems().add(unqualifiedShorthandSelectItemSegment.get());
            selectItemsSegment.getSelectItems().add(unqualifiedShorthandSelectItemSegment.get());
        }
    }
    
    private void setSelectItemSegment(final ParserRuleContext selectClauseNode, final SelectClauseSegment selectClauseSegment) {
        for (ParserRuleContext each : ExtractorUtils.getAllDescendantNodes(selectClauseNode, RuleName.SELECT_ITEM)) {
    private void setSelectItemSegment(final ParserRuleContext ancestorNode, final SelectItemsSegment selectItemsSegment) {
        for (ParserRuleContext each : ExtractorUtils.getAllDescendantNodes(ancestorNode, RuleName.SELECT_ITEM)) {
            Optional<? extends SelectItemSegment> selectItemSegment = selectItemExtractor.extract(each);
            if (selectItemSegment.isPresent()) {
                selectClauseSegment.getSelectItems().add(selectItemSegment.get());
                selectItemsSegment.getSelectItems().add(selectItemSegment.get());
            }
        }
    }
    
    private boolean hasDistinct(final ParserRuleContext selectClauseNode) {
        return ExtractorUtils.findFirstChildNode(ExtractorUtils.getFirstChildNode(selectClauseNode, RuleName.SELECT_SPECIFICATION), RuleName.DISTINCT).isPresent();
    private boolean hasDistinct(final ParserRuleContext ancestorNode) {
        return ExtractorUtils.findFirstChildNode(ExtractorUtils.getFirstChildNode(ancestorNode, RuleName.SELECT_SPECIFICATION), RuleName.DISTINCT).isPresent();
    }
}
+17 −3
Original line number Diff line number Diff line
@@ -18,19 +18,32 @@
package org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select;

import com.google.common.base.Optional;
import lombok.Setter;
import org.antlr.v4.runtime.ParserRuleContext;
import org.apache.shardingsphere.core.parse.antlr.extractor.api.OptionalSQLSegmentExtractor;
import org.apache.shardingsphere.core.parse.antlr.extractor.api.PlaceholderIndexesAware;
import org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.WhereExtractor;
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.WhereSegment;

import java.util.Map;

/**
 * Where extractor for select.
 *
 * @author duhongjun
 * @author zhangliang
 */
public final class SelectWhereExtractor extends AbstractWhereExtractor {
@Setter
public final class SelectWhereExtractor implements OptionalSQLSegmentExtractor, PlaceholderIndexesAware {
    
    private final WhereExtractor whereExtractor = new WhereExtractor();
    
    private Map<ParserRuleContext, Integer> placeholderIndexes;
    
    @Override
    protected Optional<ParserRuleContext> extractWhere(final ParserRuleContext ancestorNode) {
    public Optional<WhereSegment> extract(final ParserRuleContext ancestorNode) {
        Optional<ParserRuleContext> selectItemsNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.SELECT_ITEMS);
        if (!selectItemsNode.isPresent()) {
            return Optional.absent();
@@ -39,6 +52,7 @@ public final class SelectWhereExtractor extends AbstractWhereExtractor {
        if (!fromNode.isPresent()) {
            return Optional.absent();
        }
        return ExtractorUtils.findFirstChildNodeNoneRecursive(fromNode.get().getParent(), RuleName.WHERE_CLAUSE);
        whereExtractor.setPlaceholderIndexes(placeholderIndexes);
        return whereExtractor.extract(fromNode.get().getParent());
    }
}
+4 −4
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ 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.SelectClauseSegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.SelectItemsSegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.WhereSegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.expr.SubquerySegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.order.GroupBySegment;
@@ -56,9 +56,9 @@ public final class SubqueryExtractor implements OptionalSQLSegmentExtractor {
            parentNode = parentNode.getParent();
        }
        SubquerySegment result = new SubquerySegment(subqueryNode.getStart().getStartIndex(), subqueryNode.getStop().getStopIndex(), subqueryInFrom);
        Optional<SelectClauseSegment> selectClauseSegment = new SelectClauseExtractor().extract(subqueryNode);
        if (selectClauseSegment.isPresent()) {
            result.setSelectClauseSegment(selectClauseSegment.get());
        Optional<SelectItemsSegment> selectItemsSegment = new SelectItemsExtractor().extract(subqueryNode);
        if (selectItemsSegment.isPresent()) {
            result.setSelectItemsSegment(selectItemsSegment.get());
        }
        Optional<WhereSegment> whereSegment = selectWhereExtractor.extract(subqueryNode);
        if (whereSegment.isPresent()) {
Loading