Skip to content

Commit

Permalink
for apache#2084, use findSingleNodeFromFirstDescendant to extract Lit…
Browse files Browse the repository at this point in the history
…eralExpressionSegment
  • Loading branch information
terrymanu committed Apr 24, 2019
1 parent 6044391 commit 89b30f4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,12 @@ public Optional<? extends ExpressionSegment> extract(final ParserRuleContext exp
}

private ExpressionSegment extractExpression(final ParserRuleContext expressionNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
if (ExtractorUtils.findFirstChildNode(expressionNode, RuleName.FUNCTION_CALL).isPresent()
|| ExtractorUtils.findFirstChildNodeNoneRecursive(expressionNode, RuleName.COLUMN_NAME).isPresent()) {
return extractCommonExpressionSegment(expressionNode);
Optional<ParserRuleContext> 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<ParserRuleContext> literalsNode = ExtractorUtils.findSingleNodeFromFirstDescendant(expressionNode, RuleName.LITERALS);
return literalsNode.isPresent() ? extractLiteralExpressionSegment(literalsNode.get(), parameterMarkerIndexes) : extractCommonExpressionSegment(expressionNode);
}

/**
Expand All @@ -67,23 +63,23 @@ public LiteralExpressionSegment extractLiteralExpressionSegment(final ParserRule
LiteralExpressionSegment result = new LiteralExpressionSegment(expressionNode.getStart().getStartIndex(), expressionNode.getStop().getStopIndex());
Optional<ParserRuleContext> 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<ParserRuleContext> bitExprNode = ExtractorUtils.findFirstChildNode(expressionNode, RuleName.BIT_EXPR);
if (bitExprNode.isPresent() && 1 != bitExprNode.get().getChildCount()) {
return result;
Optional<ParserRuleContext> numberLiteralsNode = ExtractorUtils.findFirstChildNode(expressionNode, RuleName.NUMBER_LITERALS);
if (numberLiteralsNode.isPresent()) {
result.setLiterals(NumberUtil.getExactlyNumber(numberLiteralsNode.get().getText(), 10));
}
Optional<ParserRuleContext> numberNode = ExtractorUtils.findFirstChildNode(expressionNode, RuleName.NUMBER_LITERALS);
if (numberNode.isPresent()) {
result.setLiterals(NumberUtil.getExactlyNumber(numberNode.get().getText(), 10));
}
Optional<ParserRuleContext> stringNode = ExtractorUtils.findFirstChildNode(expressionNode, RuleName.STRING_LITERALS);
if (stringNode.isPresent()) {
String text = stringNode.get().getText();
Optional<ParserRuleContext> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ public static Optional<ParserRuleContext> 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<ParserRuleContext> findNodeOnlyFromFirstDescendant(final ParserRuleContext node, final RuleName ruleName) {
public static Optional<ParserRuleContext> findSingleNodeFromFirstDescendant(final ParserRuleContext node, final RuleName ruleName) {
ParserRuleContext nextNode = node;
do {
if (isMatchedNode(nextNode, ruleName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public enum RuleName {

PARAMETER_MARKER("ParameterMarker"),

LITERALS("Literals"),

NUMBER_LITERALS("NumberLiterals"),

STRING_LITERALS("StringLiterals"),
Expand Down

0 comments on commit 89b30f4

Please sign in to comment.