Loading sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/constant/LogicalOperator.java +22 −20 Original line number Diff line number Diff line Loading @@ -17,38 +17,40 @@ package org.apache.shardingsphere.core.parse.antlr.constant; import lombok.AccessLevel; import lombok.NoArgsConstructor; import com.google.common.base.Optional; import java.util.Arrays; import java.util.Collection; import java.util.TreeSet; /** * Logical operator. * * @author zhangliang */ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class LogicalOperator { public enum LogicalOperator { /** * Judge is logical operator or not. * * @param token token * @return is logical operator or not */ public static boolean isLogicalOperator(final String token) { return isAndOperator(token) || isOrOperator(token); } AND("AND", "&&"), OR("OR", "||"); private final Collection<String> texts = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); private static boolean isAndOperator(final String token) { return "AND".equalsIgnoreCase(token) || "&&".equals(token); LogicalOperator(final String... texts) { this.texts.addAll(Arrays.asList(texts)); } /** * Judge is or operator or not. * Get logical operator value from text. * * @param token token * @return OR operator or not * @param text text * @return logical operator value */ public static boolean isOrOperator(final String token) { return "OR".equalsIgnoreCase(token) || "||".equals(token); public static Optional<LogicalOperator> valueFrom(final String text) { for (LogicalOperator each : LogicalOperator.values()) { if (each.texts.contains(text)) { return Optional.of(each); } } return Optional.absent(); } } sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/impl/dml/PredicateExtractor.java +17 −27 Original line number Diff line number Diff line Loading @@ -55,16 +55,16 @@ public final class PredicateExtractor implements OptionalSQLSegmentExtractor { @Override public Optional<OrPredicateSegment> extract(final ParserRuleContext exprNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) { return extractRecursiveWithLogicalOperator(exprNode, parameterMarkerIndexes); return extractRecursiveWithLogicalOperation(exprNode, parameterMarkerIndexes); } private Optional<OrPredicateSegment> extractRecursiveWithLogicalOperator(final ParserRuleContext exprNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) { private Optional<OrPredicateSegment> extractRecursiveWithLogicalOperation(final ParserRuleContext exprNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) { Optional<ParserRuleContext> logicalOperatorNode = ExtractorUtils.findFirstChildNodeNoneRecursive(exprNode, RuleName.LOGICAL_OPERATOR); if (!logicalOperatorNode.isPresent()) { return extractRecursiveWithParen(exprNode, parameterMarkerIndexes); } Optional<OrPredicateSegment> leftPredicate = extractRecursiveWithLogicalOperator((ParserRuleContext) exprNode.getChild(0), parameterMarkerIndexes); Optional<OrPredicateSegment> rightPredicate = extractRecursiveWithLogicalOperator((ParserRuleContext) exprNode.getChild(2), parameterMarkerIndexes); Optional<OrPredicateSegment> leftPredicate = extractRecursiveWithLogicalOperation((ParserRuleContext) exprNode.getChild(0), parameterMarkerIndexes); Optional<OrPredicateSegment> rightPredicate = extractRecursiveWithLogicalOperation((ParserRuleContext) exprNode.getChild(2), parameterMarkerIndexes); if (leftPredicate.isPresent() && rightPredicate.isPresent()) { return Optional.of(mergePredicate(leftPredicate.get(), rightPredicate.get(), logicalOperatorNode.get().getText())); } Loading @@ -72,33 +72,13 @@ public final class PredicateExtractor implements OptionalSQLSegmentExtractor { } private Optional<OrPredicateSegment> extractRecursiveWithParen(final ParserRuleContext exprNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) { Optional<Integer> index = getLeftParenIndex(exprNode); if (index.isPresent()) { ParserRuleContext expressionNode = (ParserRuleContext) exprNode.getChild(index.get() + 1); return RuleName.EXPR.getName().equals(expressionNode.getClass().getSimpleName()) ? extractRecursiveWithLogicalOperator(expressionNode, parameterMarkerIndexes) : Optional.<OrPredicateSegment>absent(); if (Paren.isLeftParen(exprNode.getChild(0).getText())) { return extractRecursiveWithLogicalOperation((ParserRuleContext) exprNode.getChild(1), parameterMarkerIndexes); } Optional<PredicateSegment> predicate = extractPredicate(exprNode, parameterMarkerIndexes); return predicate.isPresent() ? Optional.of(getOrPredicateSegment(predicate.get())) : Optional.<OrPredicateSegment>absent(); } private Optional<Integer> getLeftParenIndex(final ParserRuleContext exprNode) { for (int i = 0; i < exprNode.getChildCount(); i++) { if (Paren.isLeftParen(exprNode.getChild(i).getText())) { return Optional.of(i); } } return Optional.absent(); } private OrPredicateSegment getOrPredicateSegment(final PredicateSegment predicate) { OrPredicateSegment result = new OrPredicateSegment(); AndPredicateSegment andPredicate = new AndPredicateSegment(); andPredicate.getPredicates().add(predicate); result.getAndPredicates().add(andPredicate); return result; } private Optional<PredicateSegment> extractPredicate(final ParserRuleContext exprNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) { Optional<PredicateSegment> result = extractComparisonPredicate(exprNode, parameterMarkerIndexes); if (result.isPresent()) { Loading Loading @@ -184,8 +164,18 @@ public final class PredicateExtractor implements OptionalSQLSegmentExtractor { return result; } private OrPredicateSegment getOrPredicateSegment(final PredicateSegment predicate) { OrPredicateSegment result = new OrPredicateSegment(); AndPredicateSegment andPredicate = new AndPredicateSegment(); andPredicate.getPredicates().add(predicate); result.getAndPredicates().add(andPredicate); return result; } private OrPredicateSegment mergePredicate(final OrPredicateSegment leftPredicate, final OrPredicateSegment rightPredicate, final String operator) { if (LogicalOperator.isOrOperator(operator)) { Optional<LogicalOperator> logicalOperator = LogicalOperator.valueFrom(operator); Preconditions.checkState(logicalOperator.isPresent()); if (LogicalOperator.OR == logicalOperator.get()) { leftPredicate.getAndPredicates().addAll(rightPredicate.getAndPredicates()); return leftPredicate; } Loading Loading
sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/constant/LogicalOperator.java +22 −20 Original line number Diff line number Diff line Loading @@ -17,38 +17,40 @@ package org.apache.shardingsphere.core.parse.antlr.constant; import lombok.AccessLevel; import lombok.NoArgsConstructor; import com.google.common.base.Optional; import java.util.Arrays; import java.util.Collection; import java.util.TreeSet; /** * Logical operator. * * @author zhangliang */ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class LogicalOperator { public enum LogicalOperator { /** * Judge is logical operator or not. * * @param token token * @return is logical operator or not */ public static boolean isLogicalOperator(final String token) { return isAndOperator(token) || isOrOperator(token); } AND("AND", "&&"), OR("OR", "||"); private final Collection<String> texts = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); private static boolean isAndOperator(final String token) { return "AND".equalsIgnoreCase(token) || "&&".equals(token); LogicalOperator(final String... texts) { this.texts.addAll(Arrays.asList(texts)); } /** * Judge is or operator or not. * Get logical operator value from text. * * @param token token * @return OR operator or not * @param text text * @return logical operator value */ public static boolean isOrOperator(final String token) { return "OR".equalsIgnoreCase(token) || "||".equals(token); public static Optional<LogicalOperator> valueFrom(final String text) { for (LogicalOperator each : LogicalOperator.values()) { if (each.texts.contains(text)) { return Optional.of(each); } } return Optional.absent(); } }
sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/impl/dml/PredicateExtractor.java +17 −27 Original line number Diff line number Diff line Loading @@ -55,16 +55,16 @@ public final class PredicateExtractor implements OptionalSQLSegmentExtractor { @Override public Optional<OrPredicateSegment> extract(final ParserRuleContext exprNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) { return extractRecursiveWithLogicalOperator(exprNode, parameterMarkerIndexes); return extractRecursiveWithLogicalOperation(exprNode, parameterMarkerIndexes); } private Optional<OrPredicateSegment> extractRecursiveWithLogicalOperator(final ParserRuleContext exprNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) { private Optional<OrPredicateSegment> extractRecursiveWithLogicalOperation(final ParserRuleContext exprNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) { Optional<ParserRuleContext> logicalOperatorNode = ExtractorUtils.findFirstChildNodeNoneRecursive(exprNode, RuleName.LOGICAL_OPERATOR); if (!logicalOperatorNode.isPresent()) { return extractRecursiveWithParen(exprNode, parameterMarkerIndexes); } Optional<OrPredicateSegment> leftPredicate = extractRecursiveWithLogicalOperator((ParserRuleContext) exprNode.getChild(0), parameterMarkerIndexes); Optional<OrPredicateSegment> rightPredicate = extractRecursiveWithLogicalOperator((ParserRuleContext) exprNode.getChild(2), parameterMarkerIndexes); Optional<OrPredicateSegment> leftPredicate = extractRecursiveWithLogicalOperation((ParserRuleContext) exprNode.getChild(0), parameterMarkerIndexes); Optional<OrPredicateSegment> rightPredicate = extractRecursiveWithLogicalOperation((ParserRuleContext) exprNode.getChild(2), parameterMarkerIndexes); if (leftPredicate.isPresent() && rightPredicate.isPresent()) { return Optional.of(mergePredicate(leftPredicate.get(), rightPredicate.get(), logicalOperatorNode.get().getText())); } Loading @@ -72,33 +72,13 @@ public final class PredicateExtractor implements OptionalSQLSegmentExtractor { } private Optional<OrPredicateSegment> extractRecursiveWithParen(final ParserRuleContext exprNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) { Optional<Integer> index = getLeftParenIndex(exprNode); if (index.isPresent()) { ParserRuleContext expressionNode = (ParserRuleContext) exprNode.getChild(index.get() + 1); return RuleName.EXPR.getName().equals(expressionNode.getClass().getSimpleName()) ? extractRecursiveWithLogicalOperator(expressionNode, parameterMarkerIndexes) : Optional.<OrPredicateSegment>absent(); if (Paren.isLeftParen(exprNode.getChild(0).getText())) { return extractRecursiveWithLogicalOperation((ParserRuleContext) exprNode.getChild(1), parameterMarkerIndexes); } Optional<PredicateSegment> predicate = extractPredicate(exprNode, parameterMarkerIndexes); return predicate.isPresent() ? Optional.of(getOrPredicateSegment(predicate.get())) : Optional.<OrPredicateSegment>absent(); } private Optional<Integer> getLeftParenIndex(final ParserRuleContext exprNode) { for (int i = 0; i < exprNode.getChildCount(); i++) { if (Paren.isLeftParen(exprNode.getChild(i).getText())) { return Optional.of(i); } } return Optional.absent(); } private OrPredicateSegment getOrPredicateSegment(final PredicateSegment predicate) { OrPredicateSegment result = new OrPredicateSegment(); AndPredicateSegment andPredicate = new AndPredicateSegment(); andPredicate.getPredicates().add(predicate); result.getAndPredicates().add(andPredicate); return result; } private Optional<PredicateSegment> extractPredicate(final ParserRuleContext exprNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) { Optional<PredicateSegment> result = extractComparisonPredicate(exprNode, parameterMarkerIndexes); if (result.isPresent()) { Loading Loading @@ -184,8 +164,18 @@ public final class PredicateExtractor implements OptionalSQLSegmentExtractor { return result; } private OrPredicateSegment getOrPredicateSegment(final PredicateSegment predicate) { OrPredicateSegment result = new OrPredicateSegment(); AndPredicateSegment andPredicate = new AndPredicateSegment(); andPredicate.getPredicates().add(predicate); result.getAndPredicates().add(andPredicate); return result; } private OrPredicateSegment mergePredicate(final OrPredicateSegment leftPredicate, final OrPredicateSegment rightPredicate, final String operator) { if (LogicalOperator.isOrOperator(operator)) { Optional<LogicalOperator> logicalOperator = LogicalOperator.valueFrom(operator); Preconditions.checkState(logicalOperator.isPresent()); if (LogicalOperator.OR == logicalOperator.get()) { leftPredicate.getAndPredicates().addAll(rightPredicate.getAndPredicates()); return leftPredicate; } Loading