提交 a662f1d9 编写于 作者: T terrymanu

for #1753, parse subquery for order by & group by

上级 76a2ae35
......@@ -80,9 +80,11 @@ public final class SelectItemsExtractor implements OptionalSQLSegmentExtractor {
}
ParserRuleContext result = tableReferencesNode.get();
Optional<ParserRuleContext> subqueryNode = ExtractorUtils.findSingleNodeFromFirstDescendant(tableReferencesNode.get(), RuleName.SUBQUERY);
boolean isFromRecursiveInMethod = false;
if (subqueryNode.isPresent()) {
isFromRecursiveInMethod = true;
result = findMainQueryNode(subqueryNode.get(), true);
}
return isFromRecursive ? result : ancestorNode;
return isFromRecursive || isFromRecursiveInMethod ? result : ancestorNode;
}
}
......@@ -42,8 +42,23 @@ public abstract class GroupByExtractor implements OptionalSQLSegmentExtractor {
@Override
public final Optional<GroupBySegment> extract(final ParserRuleContext ancestorNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
Optional<ParserRuleContext> groupByNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.GROUP_BY_CLAUSE);
Optional<ParserRuleContext> groupByNode = ExtractorUtils.findFirstChildNode(findMainQueryNode(ancestorNode, false), RuleName.GROUP_BY_CLAUSE);
return groupByNode.isPresent() ? Optional.of(new GroupBySegment(groupByNode.get().getStop().getStopIndex(), orderByItemExtractor.extract(groupByNode.get(), parameterMarkerIndexes)))
: Optional.<GroupBySegment>absent();
}
private ParserRuleContext findMainQueryNode(final ParserRuleContext ancestorNode, final boolean isFromRecursive) {
Optional<ParserRuleContext> tableReferencesNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.TABLE_REFERENCES);
if (!tableReferencesNode.isPresent()) {
return ancestorNode;
}
ParserRuleContext result = tableReferencesNode.get();
Optional<ParserRuleContext> subqueryNode = ExtractorUtils.findSingleNodeFromFirstDescendant(tableReferencesNode.get(), RuleName.SUBQUERY);
boolean isFromRecursiveInMethod = false;
if (subqueryNode.isPresent()) {
isFromRecursiveInMethod = true;
result = findMainQueryNode(subqueryNode.get(), true);
}
return isFromRecursive || isFromRecursiveInMethod ? result : ancestorNode;
}
}
......@@ -40,7 +40,22 @@ public abstract class OrderByExtractor implements OptionalSQLSegmentExtractor {
@Override
public final Optional<OrderBySegment> extract(final ParserRuleContext ancestorNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
Optional<ParserRuleContext> orderByNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.ORDER_BY_CLAUSE);
Optional<ParserRuleContext> orderByNode = ExtractorUtils.findFirstChildNode(findMainQueryNode(ancestorNode, false), RuleName.ORDER_BY_CLAUSE);
return orderByNode.isPresent() ? Optional.of(new OrderBySegment(orderByItemExtractor.extract(orderByNode.get(), parameterMarkerIndexes))) : Optional.<OrderBySegment>absent();
}
private ParserRuleContext findMainQueryNode(final ParserRuleContext ancestorNode, final boolean isFromRecursive) {
Optional<ParserRuleContext> tableReferencesNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.TABLE_REFERENCES);
if (!tableReferencesNode.isPresent()) {
return ancestorNode;
}
ParserRuleContext result = tableReferencesNode.get();
Optional<ParserRuleContext> subqueryNode = ExtractorUtils.findSingleNodeFromFirstDescendant(tableReferencesNode.get(), RuleName.SUBQUERY);
boolean isFromRecursiveInMethod = false;
if (subqueryNode.isPresent()) {
isFromRecursiveInMethod = true;
result = findMainQueryNode(subqueryNode.get(), true);
}
return isFromRecursive || isFromRecursiveInMethod ? result : ancestorNode;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册