private void addWhere(CommonTree whereTree, EntityReference ref, boolean replaceVariableName) { TreeVisitor visitor = new TreeVisitor(); VariableManipulator variableManip = new VariableManipulator(); visitor.visit(whereTree, variableManip); if (replaceVariableName) { Set<String> variables = variableManip.getUsedVariableNames(); if (variables.size() > 1) { // we assume that adding where that use only one variable and does not add its own variables throw new IllegalStateException("Multiple variables used in condition"); } String assumedEntityVariableInWhere = variableManip.getVariableNameInUse(0); variableManip.renameVariable(assumedEntityVariableInWhere, ref); } ParameterCounter parameterCounter = new ParameterCounter(true); visitor.visit(whereTree, parameterCounter); addedParams.addAll(parameterCounter.getParameterNames()); getQueryTransformer().mixinWhereConditionsIntoTree(whereTree); }
@Override public Set<String> getParamNames() { TreeVisitor visitor = new TreeVisitor(); ParameterCounter parameterCounter = new ParameterCounter(true); visitor.visit(getQueryAnalyzer().getTree(), parameterCounter); return parameterCounter.getParameterNames(); }
@Override public Set<String> getAllEntityNames() { TreeVisitor visitor = new TreeVisitor(); EntitiesFinder finder = new EntitiesFinder(); visitor.visit(getQueryAnalyzer().getTree(), finder); return finder.resolveEntityNames(model, getQueryAnalyzer().getRootQueryVariableContext()); }
@Override public String getResult() { CommonTree tree = getQueryTransformer().getTree(); TreeVisitor visitor = new TreeVisitor(); TreeToQuery treeToQuery = new TreeToQuery(); visitor.visit(tree, treeToQuery); return treeToQuery.getQueryString().trim(); }
public void prepare(DomainModel model, String query, boolean failOnErrors) throws RecognitionException { Preconditions.checkNotNull(query, "query is null"); this.model = model; query = query.replace("\n", " "); query = query.replace("\r", " "); query = query.replace("\t", " "); tree = Parser.parse(query, failOnErrors); TreeVisitor visitor = new TreeVisitor(); idVarSelector = new IdVarSelector(model); visitor.visit(tree, idVarSelector); }
public boolean hasJoins() { CommonTree sourceNode = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SOURCES); List<SelectionSourceNode> selectionSourceNodes = getChildrenByClass(sourceNode, SelectionSourceNode.class); if (selectionSourceNodes.size() > 1) { return true; } else if (selectionSourceNodes.size() == 1) { NodesFinder<JoinVariableNode> nodesFinder = new NodesFinder<>(JoinVariableNode.class); TreeVisitor treeVisitor = new TreeVisitor(); treeVisitor.visit(tree, nodesFinder); return !nodesFinder.getFoundNodes().isEmpty(); } else { return false; } }
private static void checkTreeForExceptions(String input, CommonTree tree) { TreeVisitor visitor = new TreeVisitor(); ErrorNodesFinder errorNodesFinder = new ErrorNodesFinder(); visitor.visit(tree, errorNodesFinder); List<ErrorRec> errors = errorNodesFinder.getErrorNodes().stream() .map(node -> new ErrorRec(node, "CommonErrorNode")) .collect(Collectors.toList()); if (!errors.isEmpty()) { throw new JpqlSyntaxException(String.format("Errors found for input jpql:[%s]", StringUtils.strip(input)), errors); } }
protected boolean isValid(CommonTree tree) { TreeVisitor visitor = new TreeVisitor(); ErrorNodesFinder errorNodesFinder = new ErrorNodesFinder(); visitor.visit(tree, errorNodesFinder); List<CommonErrorNode> errorNodes = errorNodesFinder.getErrorNodes(); if (!errorNodes.isEmpty()) { System.err.println(errorNodes); } return errorNodes.isEmpty(); }
@Test public void mixinJoinOnIntoTree() throws RecognitionException { DomainModel model = prepareDomainModel(); QueryTreeTransformer qa = new QueryTreeTransformer(); qa.prepare(model, "select c from Car c"); CommonTree tree = qa.getTree(); CommonTree sources = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SOURCES); assertEquals(1, sources.getChildCount()); assertTrue(sources.getChild(0) instanceof SelectionSourceNode); CommonTree source = (CommonTree) sources.getFirstChildWithType(JPA2Lexer.T_SOURCE); assertTrue(source.getChild(0) instanceof IdentificationVariableNode); JoinVariableNode join = Parser.parseJoinClause("join Driver d on d.car.id = c.id").get(0); qa.mixinJoinIntoTree(join, new VariableEntityReference("Car", "c"), true); tree = qa.getTree(); sources = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SOURCES); assertEquals(1, sources.getChildCount()); SelectionSourceNode sourceNode = (SelectionSourceNode) sources.getChild(0); assertEquals(2, sourceNode.getChildCount()); assertTrue(sourceNode.getChild(0) instanceof IdentificationVariableNode); assertTrue(sourceNode.getChild(1) instanceof JoinVariableNode); JoinVariableNode joinNode = (JoinVariableNode) sourceNode.getChild(1); TreeVisitor visitor = new TreeVisitor(); TreeToQuery treeToQuery = new TreeToQuery(); visitor.visit(joinNode, treeToQuery); assertEquals("d", joinNode.getVariableName()); assertEquals("join Driver d on d.car.id = c.id", treeToQuery.getQueryString().trim()); }
protected String toQuery(Tree tree) { TreeVisitor visitor = new TreeVisitor(); TreeToQuery treeToQuery = new TreeToQuery(); visitor.visit(tree, treeToQuery); return treeToQuery.getQueryString().trim(); }
public List<SimpleConditionNode> findAllConditions() { NodesFinder<SimpleConditionNode> nodesFinder = new NodesFinder<>(SimpleConditionNode.class); TreeVisitor treeVisitor = new TreeVisitor(); treeVisitor.visit(tree, nodesFinder); return nodesFinder.getFoundNodes(); }