public Set<String> getEntityNames() { return queryTree.visit(new EntitiesFinder()) .resolveEntityNames(queryTree.getModel(), queryTree.getQueryVariableContext()); }
/** * @return returns list of join variable nodes (JOIN entityName entityAlias ON clause) from FROM statement */ public Stream<JoinVariableNode> getAstJoinVariableNodes() { return generateChildrenByClass(getAstFromNode(), SelectionSourceNode.class) .flatMap(selectionSource -> generateChildrenByClass(selectionSource, JoinVariableNode.class)); }
public boolean isQueryWithJoins() { long identificationVariableCount = queryTree.getAstIdentificationVariableNodes().count(); if (identificationVariableCount > 1) { return true; } long joinCount = queryTree.getAstJoinVariableNodes().count(); return joinCount > 0; }
public Stream<PathNode> getAstSelectedPathNodes() { SelectedItemsNode selectedItems = getAstSelectedItemsNode(); if (selectedItems != null) { return generateChildrenByClass(selectedItems, SelectedItemNode.class) .flatMap(selectedItemNode -> generateChildrenByClass(selectedItemNode, PathNode.class)); } return Stream.empty(); }
private HintResponse hintFieldName(String lastWord, String input, int caretPosition, Set<InferredType> expectedTypes) throws RecognitionException { QueryTree queryTree = null; try { queryTree = new QueryTree(model, input, false); } catch (JPA2RecognitionException e) { List<String> errorMessages = new ArrayList<>(); return new HintResponse("Query error", errorMessages); List<ErrorRec> errorRecs = queryTree.getInvalidIdVarNodes(); QueryVariableContext root = queryTree.getQueryVariableContext(); if (root == null) { List<String> errorMessages = prepareErrorMessages(errorRecs);
protected QueryTree getTree() { if (queryTree == null) { try { queryTree = new QueryTree(model, query); } catch (JPA2RecognitionException e) { throw new JpqlSyntaxException(format("Errors found for input JPQL:[%s]\n%s", StringUtils.strip(query), e.getMessage())); } List<ErrorRec> errors = new ArrayList<>(queryTree.getInvalidIdVarNodes()); if (!errors.isEmpty()) { throw new JpqlSyntaxException(format("Errors found for input JPQL:[%s]", StringUtils.strip(query)), errors); } } return queryTree; }
public String getMainSelectedEntityName(PathNode pathNode) { if (pathNode == null) { throw new RuntimeException(format("Unable to find selected entity name [%s]", StringUtils.strip(queryTree.getQueryString()))); } QueryVariableContext variableContext = queryTree.getQueryVariableContext(); JpqlEntityModel entity = variableContext.getEntityByVariableName(pathNode.getEntityVariableName()); if (entity == null) { throw new RuntimeException(format("Unable to find selected entity name [%s]", StringUtils.strip(queryTree.getQueryString()))); } return entity.getName(); }
@Override public List<QueryPath> getQueryPaths() { List<QueryPath> queryPaths = new ArrayList<>(); QueryVariableContext variableContext = getTree().getQueryVariableContext(); PathNodeFinder finder = getTree().visit(new PathNodeFinder()); for (PathNode node : finder.getSelectedPathNodes()) { JpqlEntityModel model = variableContext.getEntityByVariableNameHierarchically(node.getEntityVariableName()); QueryPath queryPath = new QueryPath(model.getName(), node.getEntityVariableName(), node.asPathString(), true); queryPaths.add(queryPath); } for (PathNode node : finder.getOtherPathNodes()) { JpqlEntityModel model = variableContext.getEntityByVariableNameHierarchically(node.getEntityVariableName()); QueryPath queryPath = new QueryPath(model.getName(), node.getEntityVariableName(), node.asPathString(), false); queryPaths.add(queryPath); } return queryPaths; }
public void mixinWhereConditionsIntoTree(CommonTree targetWhere) { CommonTree sourceWhere = queryTree.getAstWhereNode(); if (sourceWhere == null) { CommonTree tree = queryTree.getAstTree(); CommonTree fromNode = queryTree.getAstFromNode();
public String getVariableNameByEntity(String entityType) { return getQueryVariableContext().getVariableNameByEntity(entityType); }
@Nullable public IdentificationVariableNode getMainIdentificationVariableNode() { List<IdentificationVariableNode> identificationVariables = queryTree.getAstIdentificationVariableNodes() .collect(Collectors.toList()); Optional<String> variableName = queryTree.getAstSelectedPathNodes().findFirst().map(PathNode::getEntityVariableName); if (variableName.isPresent()) { for (IdentificationVariableNode node : identificationVariables) { if (variableName.get().equalsIgnoreCase(node.getVariableName())) { return node; } } } return identificationVariables.stream().findFirst().orElse(null); }
public void replaceWithCount(Tree node) { Tree selectedItems = queryTree.getAstSelectedItemsNode(); boolean isDistinct = "distinct".equalsIgnoreCase(selectedItems.getChild(0).getText()); if (!(isDistinct && selectedItems.getChildCount() == 2 || selectedItems.getChildCount() == 1)) throw new IllegalStateException("Cannot replace with count if multiple fields selected"); SelectedItemNode selectedItemNode; if (isDistinct) selectedItems.deleteChild(0); selectedItemNode = (SelectedItemNode) selectedItems.getChild(0); AggregateExpressionNode countNode = createCountNode(node, isDistinct); selectedItemNode.deleteChild(0); selectedItemNode.addChild(countNode); Tree orderBy = queryTree.getAstTree().getFirstChildWithType(JPA2Lexer.T_ORDER_BY); if (orderBy != null) { queryTree.getAstTree().deleteChild(orderBy.getChildIndex()); } queryTree.getAstTree().freshenParentAndChildIndexes(); }
public void replaceOrderBy(boolean desc, PathEntityReference... orderingFieldRefs) { removeOrderBy(); CommonTree orderBy = new OrderByNode(JPA2Lexer.T_ORDER_BY); queryTree.getAstTree().addChild(orderBy); orderBy.addChild(new CommonTree(new CommonToken(JPA2Lexer.ORDER, "order"))); orderBy.addChild(new CommonTree(new CommonToken(JPA2Lexer.BY, "by"))); joinNode.addChild(joinPathNode); CommonTree from = queryTree.getAstFromNode(); from.getChild(0).addChild(joinNode); // assumption addedJoinVariables.add(joinPathNodeVariableName);
public void addSelectionSource(CommonTree selectionSource) { CommonTree from = queryTree.getAstFromNode(); from.addChild(selectionSource); from.freshenParentAndChildIndexes(); }
public boolean removeDistinct() { Tree selectedItems = queryTree.getAstSelectedItemsNode(); boolean isDistinct = "distinct".equalsIgnoreCase(selectedItems.getChild(0).getText()); if (isDistinct) { selectedItems.deleteChild(0); selectedItems.freshenParentAndChildIndexes(); } else { return false; } return true; }
@Nullable public PathNode getMainSelectedPathNode() { List<PathNode> pathNodes = queryTree.getAstSelectedPathNodes() .limit(2) .collect(Collectors.toList()); if (pathNodes.size() == 0 || pathNodes.size() > 1) { return null; } return pathNodes.get(0); }
public void addEntityInGroupBy(String entityAlias) { Tree groupBy = queryTree.getAstGroupByNode(); if (groupBy != null) { groupBy.addChild(new PathNode(JPA2Lexer.T_SELECTED_ENTITY, entityAlias)); groupBy.freshenParentAndChildIndexes(); } }
protected QueryTree getTree() { if (queryTree == null) { try { queryTree = new QueryTree(model, query); } catch (JPA2RecognitionException e) { throw new JpqlSyntaxException(format("Errors found for input JPQL:[%s]\n%s", StringUtils.strip(query), e.getMessage())); } List<ErrorRec> errors = new ArrayList<>(queryTree.getInvalidIdVarNodes()); if (!errors.isEmpty()) { throw new JpqlSyntaxException(format("Errors found for input JPQL:[%s]", StringUtils.strip(query)), errors); } } return queryTree; }
QueryVariableContext variableContext = getTree().getQueryVariableContext(); if (queryAnalyzer.isVariablePathNode(pathNode)) { JpqlEntityModel entity = variableContext.getEntityByVariableName(pathNode.getEntityVariableName());
public void addFirstSelectionSource(CommonTree selectionSource) { CommonTree from = queryTree.getAstFromNode(); from.insertChild(0, selectionSource); from.freshenParentAndChildIndexes(); }