Skip to content

Commit

Permalink
Moved hasToManyJoin to TableTree
Browse files Browse the repository at this point in the history
  • Loading branch information
Jugen committed Feb 12, 2024
1 parent dd73d0e commit 3c508b6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.apache.cayenne.access.translator.select;

import java.sql.Types;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* @since 4.2
Expand Down Expand Up @@ -55,7 +54,7 @@ public void perform(TranslatorContext context) {
}

// query forcing distinct or query have joins (qualifier or prefetch)
if(!context.getQuery().isDistinct() && !hasToManyJoin(context)) {
if(!context.getQuery().isDistinct() && !context.getTableTree().hasToManyJoin()) {
return;
}

Expand All @@ -69,24 +68,4 @@ public void perform(TranslatorContext context) {
}
context.getSelectBuilder().distinct();
}

private static boolean hasToManyJoin(TranslatorContext context) {
if(context.getTableTree().getNodeCount() <= 1) {
return false;
}

AtomicBoolean atomicBoolean = new AtomicBoolean(false);
context.getTableTree().visit(node -> {
if(node.getRelationship() != null && node.getRelationship().isToMany()) {
atomicBoolean.set(true);
}
});
return atomicBoolean.get();
}

static boolean isDistinct(TranslatorContext context) {
return !context.isDistinctSuppression()
&& (context.getQuery().isDistinct()
|| hasToManyJoin(context));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void perform(TranslatorContext context) {
return;
}

isDistinctOrGroupByQuery = GroupByStage.hasAggregate(context) || DistinctStage.isDistinct(context);
isDistinctOrGroupByQuery = GroupByStage.hasAggregate(context) || isDistinct(context);

QualifierTranslator qualifierTranslator = context.getQualifierTranslator();
for(Ordering ordering : context.getQuery().getOrderings()) {
Expand Down Expand Up @@ -106,4 +106,9 @@ private String getSqlString(NodeBuilder nb) {
return strBuilder.append(' ').toString();
}

private boolean isDistinct(TranslatorContext context) {
return !context.isDistinctSuppression()
&& (context.getQuery().isDistinct()
|| context.getTableTree().hasToManyJoin());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import org.apache.cayenne.CayenneRuntimeException;
Expand Down Expand Up @@ -96,6 +97,20 @@ public int getNodeCount() {
return tableNodes.size() + 1;
}

boolean hasToManyJoin() {
if(getNodeCount() <= 1) {
return false;
}

AtomicBoolean atomicBoolean = new AtomicBoolean(false);
visit(node -> {
if(node.getRelationship() != null && node.getRelationship().isToMany()) {
atomicBoolean.set(true);
}
});
return atomicBoolean.get();
}

public void visit(TableNodeVisitor visitor) {
visitor.visit(rootNode);

Expand Down

0 comments on commit 3c508b6

Please sign in to comment.