Skip to content

Commit

Permalink
Fixed #262
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed Oct 10, 2016
1 parent fd62ff9 commit 4c0d13a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ void setSubqueryInitFactory(SubqueryInitiatorFactory subqueryInitFactory) {
}

Set<JoinNode> buildClause(StringBuilder sb, Set<ClauseType> clauseExclusions, String aliasPrefix, boolean collectCollectionJoinNodes, boolean externalRepresenation) {
final boolean renderFetches = !clauseExclusions.contains(ClauseType.SELECT);
collectionJoinNodes.clear();
renderedJoins.clear();
sb.append(" FROM ");
Expand Down Expand Up @@ -582,11 +583,11 @@ Set<JoinNode> buildClause(StringBuilder sb, Set<ClauseType> clauseExclusions, St

// TODO: not sure if needed since applyImplicitJoins will already invoke that
rootNode.registerDependencies();
applyJoins(sb, rootNode.getAliasInfo(), rootNode.getNodes(), clauseExclusions, aliasPrefix, collectCollectionJoinNodes);
applyJoins(sb, rootNode.getAliasInfo(), rootNode.getNodes(), clauseExclusions, aliasPrefix, collectCollectionJoinNodes, renderFetches);
if (!rootNode.getEntityJoinNodes().isEmpty()) {
// TODO: Fix this with #216
boolean isCollection = true;
applyJoins(sb, rootNode.getAliasInfo(), new ArrayList<JoinNode>(rootNode.getEntityJoinNodes()), isCollection, clauseExclusions, aliasPrefix, collectCollectionJoinNodes);
applyJoins(sb, rootNode.getAliasInfo(), new ArrayList<JoinNode>(rootNode.getEntityJoinNodes()), isCollection, clauseExclusions, aliasPrefix, collectCollectionJoinNodes, renderFetches);
}
}

Expand Down Expand Up @@ -628,7 +629,7 @@ public void applyTransformer(ExpressionTransformer transformer) {
}
}

private void renderJoinNode(StringBuilder sb, JoinAliasInfo joinBase, JoinNode node, String aliasPrefix) {
private void renderJoinNode(StringBuilder sb, JoinAliasInfo joinBase, JoinNode node, String aliasPrefix, boolean renderFetches) {
if (!renderedJoins.contains(node)) {
switch (node.getJoinType()) {
case INNER:
Expand All @@ -641,7 +642,7 @@ private void renderJoinNode(StringBuilder sb, JoinAliasInfo joinBase, JoinNode n
sb.append(" RIGHT JOIN ");
break;
}
if (node.isFetch()) {
if (node.isFetch() && renderFetches) {
sb.append("FETCH ");
}

Expand Down Expand Up @@ -704,9 +705,9 @@ private void renderParentAlias(StringBuilder sb, JoinNode parentNode, String ali
}
}

private void renderReverseDependency(StringBuilder sb, JoinNode dependency, String aliasPrefix) {
private void renderReverseDependency(StringBuilder sb, JoinNode dependency, String aliasPrefix, boolean renderFetches) {
if (dependency.getParent() != null) {
renderReverseDependency(sb, dependency.getParent(), aliasPrefix);
renderReverseDependency(sb, dependency.getParent(), aliasPrefix, renderFetches);
if (!dependency.getDependencies().isEmpty()) {
markedJoinNodes.add(dependency);
try {
Expand All @@ -716,27 +717,27 @@ private void renderReverseDependency(StringBuilder sb, JoinNode dependency, Stri
+ dep.getAliasInfo().getAbsolutePath() + "] with alias [" + dep.getAliasInfo().getAlias() + "]");
}
// render reverse dependencies
renderReverseDependency(sb, dep, aliasPrefix);
renderReverseDependency(sb, dep, aliasPrefix, renderFetches);
}
} finally {
markedJoinNodes.remove(dependency);
}
}
renderJoinNode(sb, dependency.getParent().getAliasInfo(), dependency, aliasPrefix);
renderJoinNode(sb, dependency.getParent().getAliasInfo(), dependency, aliasPrefix, renderFetches);
}
}

private void applyJoins(StringBuilder sb, JoinAliasInfo joinBase, Map<String, JoinTreeNode> nodes, Set<ClauseType> clauseExclusions, String aliasPrefix, boolean collectCollectionJoinNodes) {
private void applyJoins(StringBuilder sb, JoinAliasInfo joinBase, Map<String, JoinTreeNode> nodes, Set<ClauseType> clauseExclusions, String aliasPrefix, boolean collectCollectionJoinNodes, boolean renderFetches) {
for (Map.Entry<String, JoinTreeNode> nodeEntry : nodes.entrySet()) {
JoinTreeNode treeNode = nodeEntry.getValue();
List<JoinNode> stack = new ArrayList<JoinNode>();
stack.addAll(treeNode.getJoinNodes().descendingMap().values());

applyJoins(sb, joinBase, stack, treeNode.isCollection(), clauseExclusions, aliasPrefix, collectCollectionJoinNodes);
applyJoins(sb, joinBase, stack, treeNode.isCollection(), clauseExclusions, aliasPrefix, collectCollectionJoinNodes, renderFetches);
}
}

private void applyJoins(StringBuilder sb, JoinAliasInfo joinBase, List<JoinNode> stack, boolean isCollection, Set<ClauseType> clauseExclusions, String aliasPrefix, boolean collectCollectionJoinNodes) {
private void applyJoins(StringBuilder sb, JoinAliasInfo joinBase, List<JoinNode> stack, boolean isCollection, Set<ClauseType> clauseExclusions, String aliasPrefix, boolean collectCollectionJoinNodes, boolean renderFetches) {
while (!stack.isEmpty()) {
JoinNode node = stack.remove(stack.size() - 1);
// If the clauses in which a join node occurs are all excluded or the join node is not mandatory for the cardinality, we skip it
Expand All @@ -748,7 +749,7 @@ private void applyJoins(StringBuilder sb, JoinAliasInfo joinBase, List<JoinNode>

// We have to render any dependencies this join node has before actually rendering itself
if (!node.getDependencies().isEmpty()) {
renderReverseDependency(sb, node, aliasPrefix);
renderReverseDependency(sb, node, aliasPrefix, renderFetches);
}

// Collect the join nodes referring to collections
Expand All @@ -757,11 +758,11 @@ private void applyJoins(StringBuilder sb, JoinAliasInfo joinBase, List<JoinNode>
}

// Finally render this join node
renderJoinNode(sb, joinBase, node, aliasPrefix);
renderJoinNode(sb, joinBase, node, aliasPrefix, renderFetches);

// Render child nodes recursively
if (!node.getNodes().isEmpty()) {
applyJoins(sb, node.getAliasInfo(), node.getNodes(), clauseExclusions, aliasPrefix, collectCollectionJoinNodes);
applyJoins(sb, node.getAliasInfo(), node.getNodes(), clauseExclusions, aliasPrefix, collectCollectionJoinNodes, renderFetches);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ public void simpleTest() {
assertEquals("DOC5", result.get(0).getName());
}


@Test
public void simpleTestFetch() {
PaginatedCriteriaBuilder<Document> cb = cbf.create(em, Document.class, "d")
.where("d.name").like(true).value("doc%").noEscape()
.where("owner.name").eq("Karl1")
.orderByAsc("d.id")
.fetch("owner")
.page(0, 1);
List<Document> result = cb.getResultList();
assertEquals(1, result.size());
assertEquals("doc1", result.get(0).getName());
}

@Test
public void testSelectIndexedWithParameter() {
String expectedCountQuery = "SELECT " + countPaginated("d.id", false) + " FROM Document d JOIN d.owner owner_1 WHERE owner_1.name = :param_0";
Expand Down

0 comments on commit 4c0d13a

Please sign in to comment.