private String getInnerExpression(final ParserRuleContext functionNode) { StringBuilder result = new StringBuilder(); for (int i = 1; i < functionNode.getChildCount(); i++) { String text = functionNode.getChild(i).getText(); result.append(text); if ("DISTINCT".equals(text)) { result.append(" "); } } return result.toString(); }
private String getDistinctExpression(final ParserRuleContext functionNode) { StringBuilder result = new StringBuilder(); for (int i = 3; i < functionNode.getChildCount() - 1; i++) { result.append(functionNode.getChild(i).getText()); } return result.toString(); } }
@Override public Optional<ColumnDefinitionSegment> extract(final ParserRuleContext ancestorNode) { Optional<ParserRuleContext> columnNameNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.COLUMN_NAME); if (!columnNameNode.isPresent()) { return Optional.absent(); } Optional<ParserRuleContext> dataTypeNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.DATA_TYPE); Optional<String> dataTypeText = dataTypeNode.isPresent() ? Optional.of(dataTypeNode.get().getChild(0).getText()) : Optional.<String>absent(); boolean isPrimaryKey = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.PRIMARY_KEY).isPresent(); return Optional.of(new ColumnDefinitionSegment(columnNameNode.get().getText(), dataTypeText.orNull(), isPrimaryKey)); } }
private Collection<DropColumnDefinitionSegment> extractDropColumnSegments(final ParserRuleContext dropColumnNode) { Collection<DropColumnDefinitionSegment> result = new HashSet<>(); for (ParseTree each : ExtractorUtils.getAllDescendantNodes(dropColumnNode, RuleName.COLUMN_NAME)) { result.add(new DropColumnDefinitionSegment(SQLUtil.getExactlyValue(each.getText()))); } return result; } }
@Override public void exitPowOpExpr(ExprParser.PowOpExprContext ctx) { nodes.put( ctx, new BinPowExpr( ctx.getChild(1).getText(), (Expr) nodes.get(ctx.getChild(0)), (Expr) nodes.get(ctx.getChild(2)) ) ); }
private void handleRuleContext(AssignmentExpressionContext ctx, ParseTree child) { ParseTree t = findChildrenByType(child, ClassNameExpressionContext.class); if (t != null) { String ttype = t.getText().trim(); AutocompleteCandidate c = new AutocompleteCandidate(GroovyCompletionTypes.NAME, ctx.getChild(0).getText()); registry.addCandidate(c); if (GroovyCompletionTypes.debug) logger.info("define variable of type " + ctx.getChild(0).getText() + " " + ttype); if (ttype != null) classUtils.defineVariable(ctx.getChild(0).getText(), ttype); } }
private boolean hasDistinct(final ParserRuleContext selectClauseNode) { return selectClauseNode.getChildCount() > 2 && DefaultKeyword.DISTINCT.name().equalsIgnoreCase(selectClauseNode.getChild(1).getText()); } }
private void nameDeclaration(DeclarationRuleContext ctx) { ParseTree t = findChildrenByType(ctx.getChild(3).getChild(0), ClassNameExpressionContext.class); if (t != null) { String ttype = t.getText().trim(); AutocompleteCandidate c = new AutocompleteCandidate(GroovyCompletionTypes.NAME, ctx.getChild(1).getText()); registry.addCandidate(c); if (GroovyCompletionTypes.debug) logger.info("define variable of type " + ctx.getChild(1).getText() + " " + ttype); if (ttype != null) classUtils.defineVariable(ctx.getChild(1).getText(), ttype); } }
private void handlePathExpressionContext(AssignmentExpressionContext ctx) { String typpen = ctx.getChild(2).getChild(0).getText().trim(); AutocompleteCandidate c = new AutocompleteCandidate(GroovyCompletionTypes.NAME, ctx.getChild(0).getText()); registry.addCandidate(c); if (GroovyCompletionTypes.debug) logger.info("define variable of type " + ctx.getChild(0).getText() + " " + typpen); if (classUtils.getVariableType(typpen) != null) { classUtils.defineVariable(ctx.getChild(0).getText(), classUtils.getVariableType(typpen)); } }
private Optional<OrConditionSegment> extractConditionInternal(final Map<ParserRuleContext, Integer> placeholderIndexes, final ParserRuleContext exprNode) { int index = -1; for (int i = 0; i < exprNode.getChildCount(); i++) { if (LogicalOperator.isLogicalOperator(exprNode.getChild(i).getText())) { index = i; break; } } if (index > 0) { Optional<OrConditionSegment> leftOrCondition = extractConditionInternal(placeholderIndexes, (ParserRuleContext) exprNode.getChild(index - 1)); Optional<OrConditionSegment> rightOrCondition = extractConditionInternal(placeholderIndexes, (ParserRuleContext) exprNode.getChild(index + 1)); if (leftOrCondition.isPresent() && rightOrCondition.isPresent()) { return Optional.of(mergeCondition(placeholderIndexes, leftOrCondition.get(), rightOrCondition.get(), exprNode.getChild(index).getText())); } return leftOrCondition.isPresent() ? leftOrCondition : rightOrCondition; } return extractConditionForParen(placeholderIndexes, exprNode); }
@Override public void exitUnaryOpExpr(ExprParser.UnaryOpExprContext ctx) { int opCode = ((TerminalNode) ctx.getChild(0)).getSymbol().getType(); switch (opCode) { case ExprParser.MINUS: nodes.put(ctx, new UnaryMinusExpr((Expr) nodes.get(ctx.getChild(1)))); break; case ExprParser.NOT: nodes.put(ctx, new UnaryNotExpr((Expr) nodes.get(ctx.getChild(1)))); break; default: throw new RuntimeException("Unrecognized unary operator " + ctx.getChild(0).getText()); } }
@Override public void enterOrderByExpression(@NotNull PQL2Parser.OrderByExpressionContext ctx) { if (ctx.getChildCount() == 1) { pushNode(new OrderByExpressionAstNode(ctx.getChild(0).getText(), "asc")); } else { pushNode(new OrderByExpressionAstNode(ctx.getChild(0).getText(), ctx.getChild(1).getText())); } }
@Override public void enterTopClause(@NotNull PQL2Parser.TopClauseContext ctx) { pushNode(new TopAstNode(Integer.parseInt(ctx.getChild(1).getText()))); }
@Override public void enterLimit(@NotNull PQL2Parser.LimitContext ctx) { // Can either be LIMIT <maxRows> or LIMIT <offset>, <maxRows> (the second is a MySQL syntax extension) if (ctx.getChild(0).getChildCount() == 2) { pushNode(new LimitAstNode(Integer.parseInt(ctx.getChild(0).getChild(1).getText()))); } else { pushNode(new LimitAstNode(Integer.parseInt(ctx.getChild(0).getChild(3).getText()), Integer.parseInt(ctx.getChild(0).getChild(1).getText()))); } }
private SelectItemSegment extractFunctionSelectItemSegment(final ParserRuleContext expressionNode, final ParserRuleContext functionNode) { String functionName = functionNode.getChild(0).getText(); Optional<AggregationType> aggregationType = findAggregationType(functionName); AliasAvailable result = aggregationType.isPresent() ? extractAggregationSelectItemSegment(aggregationType.get(), functionNode) : new ExpressionSelectItemSegment(functionNode.getText(), functionNode.getStart().getStartIndex(), functionNode.getStop().getStopIndex()); Optional<ParserRuleContext> aliasNode = ExtractorUtils.findFirstChildNode(expressionNode, RuleName.ALIAS); if (aliasNode.isPresent()) { result.setAlias(aliasNode.get().getText()); } return (SelectItemSegment) result; }
@Override public void enterInPredicate(@NotNull PQL2Parser.InPredicateContext ctx) { boolean isNotInClause = false; if ("not".equalsIgnoreCase(ctx.getChild(0).getChild(1).getText())) { isNotInClause = true; } pushNode(new InPredicateAstNode(isNotInClause)); }
@UsedInGeneratedCode @Override public void exitFunctionExpr(ExprParser.FunctionExprContext ctx) { String fnName = ctx.getChild(0).getText(); final List<Expr> args = ctx.getChildCount() > 3 ? (List<Expr>) nodes.get(ctx.getChild(2)) : Collections.emptyList(); Expr expr = macroTable.get(fnName, args); if (expr == null) { // Built-in functions. final Function function = Parser.getFunction(fnName); if (function == null) { throw new RE("function '%s' is not defined.", fnName); } expr = new FunctionExpr(function, fnName, args); } nodes.put(ctx, expr); }
@Override public void enterBinaryMathOp(@NotNull PQL2Parser.BinaryMathOpContext ctx) { pushNode(new BinaryMathOpAstNode(ctx.getChild(1).getText())); }
@Override public void enterFunctionCall(@NotNull PQL2Parser.FunctionCallContext ctx) { String expression = _expression.substring(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex() + 1); pushNode(new FunctionCallAstNode(ctx.getChild(0).getText(), expression)); }
private ExpressionSegment extractFunctionExpressionSegment(final ParserRuleContext functionNode) { return new FunctionExpressionSegment(functionNode.getChild(0).getText(), functionNode.getStart().getStartIndex(), ((TerminalNode) functionNode.getChild(1)).getSymbol().getStartIndex(), functionNode.getStop().getStopIndex(), -1); }