diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/impl/dml/ExpressionExtractor.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/impl/dml/ExpressionExtractor.java index d04c93488fb6c..69285b432e14c 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/impl/dml/ExpressionExtractor.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/impl/dml/ExpressionExtractor.java @@ -44,16 +44,12 @@ public Optional extract(final ParserRuleContext exp } private ExpressionSegment extractExpression(final ParserRuleContext expressionNode, final Map parameterMarkerIndexes) { - if (ExtractorUtils.findFirstChildNode(expressionNode, RuleName.FUNCTION_CALL).isPresent() - || ExtractorUtils.findFirstChildNodeNoneRecursive(expressionNode, RuleName.COLUMN_NAME).isPresent()) { - return extractCommonExpressionSegment(expressionNode); + Optional parameterMarkerNode = ExtractorUtils.findSingleNodeFromFirstDescendant(expressionNode, RuleName.PARAMETER_MARKER); + if (parameterMarkerNode.isPresent()) { + return extractLiteralExpressionSegment(parameterMarkerNode.get(), parameterMarkerIndexes); } - return extractLiteralExpressionSegment(expressionNode, parameterMarkerIndexes); - } - - // TODO extract column name and value from expression - private ExpressionSegment extractCommonExpressionSegment(final ParserRuleContext functionNode) { - return new CommonExpressionSegment(functionNode.getStart().getStartIndex(), functionNode.getStop().getStopIndex()); + Optional literalsNode = ExtractorUtils.findSingleNodeFromFirstDescendant(expressionNode, RuleName.LITERALS); + return literalsNode.isPresent() ? extractLiteralExpressionSegment(literalsNode.get(), parameterMarkerIndexes) : extractCommonExpressionSegment(expressionNode); } /** @@ -67,23 +63,23 @@ public LiteralExpressionSegment extractLiteralExpressionSegment(final ParserRule LiteralExpressionSegment result = new LiteralExpressionSegment(expressionNode.getStart().getStartIndex(), expressionNode.getStop().getStopIndex()); Optional parameterMarkerNode = ExtractorUtils.findFirstChildNode(expressionNode, RuleName.PARAMETER_MARKER); if (parameterMarkerNode.isPresent()) { - Integer index = parameterMarkerIndexes.get(parameterMarkerNode.get()); - result.setParameterMarkerIndex(index); + result.setParameterMarkerIndex(parameterMarkerIndexes.get(parameterMarkerNode.get())); return result; } - Optional bitExprNode = ExtractorUtils.findFirstChildNode(expressionNode, RuleName.BIT_EXPR); - if (bitExprNode.isPresent() && 1 != bitExprNode.get().getChildCount()) { - return result; + Optional numberLiteralsNode = ExtractorUtils.findFirstChildNode(expressionNode, RuleName.NUMBER_LITERALS); + if (numberLiteralsNode.isPresent()) { + result.setLiterals(NumberUtil.getExactlyNumber(numberLiteralsNode.get().getText(), 10)); } - Optional numberNode = ExtractorUtils.findFirstChildNode(expressionNode, RuleName.NUMBER_LITERALS); - if (numberNode.isPresent()) { - result.setLiterals(NumberUtil.getExactlyNumber(numberNode.get().getText(), 10)); - } - Optional stringNode = ExtractorUtils.findFirstChildNode(expressionNode, RuleName.STRING_LITERALS); - if (stringNode.isPresent()) { - String text = stringNode.get().getText(); + Optional stringLiteralsNode = ExtractorUtils.findFirstChildNode(expressionNode, RuleName.STRING_LITERALS); + if (stringLiteralsNode.isPresent()) { + String text = stringLiteralsNode.get().getText(); result.setLiterals(text.substring(1, text.length() - 1)); } return result; } + + // TODO extract column name and value from expression + private ExpressionSegment extractCommonExpressionSegment(final ParserRuleContext functionNode) { + return new CommonExpressionSegment(functionNode.getStart().getStartIndex(), functionNode.getStop().getStopIndex()); + } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/util/ExtractorUtils.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/util/ExtractorUtils.java index 8620ba473207c..325cea733e964 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/util/ExtractorUtils.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/util/ExtractorUtils.java @@ -94,13 +94,13 @@ public static Optional findFirstChildNodeNoneRecursive(final } /** - * Find node only from first descendant which only has one child. + * Find single node from first descendant which only has one child. * * @param node start node * @param ruleName rule name * @return matched node */ - public static Optional findNodeOnlyFromFirstDescendant(final ParserRuleContext node, final RuleName ruleName) { + public static Optional findSingleNodeFromFirstDescendant(final ParserRuleContext node, final RuleName ruleName) { ParserRuleContext nextNode = node; do { if (isMatchedNode(nextNode, ruleName)) { diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/util/RuleName.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/util/RuleName.java index f6e31ce75212c..82da5ce2962ea 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/util/RuleName.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/util/RuleName.java @@ -109,6 +109,8 @@ public enum RuleName { PARAMETER_MARKER("ParameterMarker"), + LITERALS("Literals"), + NUMBER_LITERALS("NumberLiterals"), STRING_LITERALS("StringLiterals"),