use of com.blazebit.persistence.parser.predicate.CompoundPredicate in project blaze-persistence by Blazebit.
the class JoinNode method getTreatedJoinNode.
public JoinNode getTreatedJoinNode(EntityType<?> type) {
// Return this when the treat is an upcast
if (type.getJavaType().isAssignableFrom(getJavaType())) {
return this;
}
String typeName = type.getJavaType().getName();
JoinNode treatedNode = treatedJoinNodes.get(typeName);
if (treatedNode != null) {
return treatedNode;
}
String alias = aliasInfo.getAliasOwner().generateRootAlias(aliasInfo.getAlias());
TreatedJoinAliasInfo treatedJoinAliasInfo = new TreatedJoinAliasInfo(this, type, alias);
aliasInfo.getAliasOwner().registerAliasInfo(treatedJoinAliasInfo);
treatedNode = new JoinNode(treatedJoinAliasInfo);
List<Predicate> predicates = new ArrayList<>(1);
predicates.add(new EqPredicate(createExpression(null), treatedNode.createExpression(null)));
treatedNode.onPredicate = new CompoundPredicate(CompoundPredicate.BooleanOperator.AND, predicates);
treatedJoinAliasInfo.setJoinNode(treatedNode);
treatedJoinNodes.put(typeName, treatedNode);
return treatedNode;
}
use of com.blazebit.persistence.parser.predicate.CompoundPredicate in project blaze-persistence by Blazebit.
the class JoinVisitor method visit.
@Override
public void visit(JoinNode node) {
if (node.getOnPredicate() != null) {
currentJoinNode = node;
try {
node.getOnPredicate().accept(this);
if (!correlationPathReplacementVisitor.getPathsToCorrelate().isEmpty()) {
// We rewrite the predicate to use correlated subqueries for implicit joins
SubqueryInitiatorImpl<Object> subqueryInitiator = (SubqueryInitiatorImpl<Object>) joinManager.getSubqueryInitFactory().createSubqueryInitiator(null, this, true, fromClause);
SubqueryBuilderImpl<?> subqueryBuilder = null;
List<Predicate> additionalPredicates = new ArrayList<>(correlationPathReplacementVisitor.getRootsToCorrelate().size());
for (ImplicitJoinCorrelationPathReplacementVisitor.RootCorrelationEntry entry : correlationPathReplacementVisitor.getRootsToCorrelate()) {
if (subqueryBuilder == null) {
subqueryBuilder = (SubqueryBuilderImpl<?>) subqueryInitiator.from(entry.getEntityClass(), entry.getAlias());
} else {
subqueryBuilder.from(entry.getEntityClass(), entry.getAlias());
}
additionalPredicates.add(entry.getAdditionalPredicate());
}
for (ImplicitJoinCorrelationPathReplacementVisitor.CorrelationTransformEntry correlationTransformEntry : correlationPathReplacementVisitor.getPathsToCorrelate()) {
String alias = correlationTransformEntry.getAlias();
String correlationExpression = correlationTransformEntry.getCorrelationExpression();
if (correlationTransformEntry.isInConjunction()) {
if (subqueryBuilder == null) {
subqueryBuilder = (SubqueryBuilderImpl<?>) subqueryInitiator.from(correlationExpression, alias);
} else {
subqueryBuilder.from(correlationExpression, alias);
}
} else {
subqueryBuilder.leftJoinDefault(correlationExpression, alias);
}
}
subqueryBuilder.whereManager.restrictSetExpression(correlationPathReplacementVisitor.rewritePredicate(node.getOnPredicate()));
for (Predicate additionalPredicate : additionalPredicates) {
subqueryBuilder.whereManager.restrictExpression(additionalPredicate);
}
node.setOnPredicate(new CompoundPredicate(CompoundPredicate.BooleanOperator.AND, new ExistsPredicate(new SubqueryExpression(subqueryBuilder), false)));
node.getOnPredicate().accept(this);
}
} finally {
currentJoinNode = null;
}
}
node.registerDependencies();
if (node.isFetch()) {
fetchableNodes.add(node);
}
}
use of com.blazebit.persistence.parser.predicate.CompoundPredicate in project blaze-persistence by Blazebit.
the class JoinManager method findNode.
private JoinNode findNode(JoinNode baseNode, String joinRelationName, final ArrayExpression arrayExpression) {
if (arrayExpression.getBase() instanceof PropertyExpression) {
if (baseNode == null) {
for (JoinNode node : rootNodes) {
Predicate pred = getArrayExpressionPredicate(node, arrayExpression);
CompoundPredicate compoundPredicate = node.getOnPredicate();
if (findPredicate(compoundPredicate, pred, node.getAlias())) {
return node;
}
}
} else {
JoinTreeNode treeNode = baseNode.getNodes().get(joinRelationName);
if (treeNode == null) {
return null;
}
for (JoinNode node : treeNode.getJoinNodes().values()) {
Predicate pred = getArrayExpressionPredicate(node, arrayExpression);
CompoundPredicate compoundPredicate = node.getOnPredicate();
if (findPredicate(compoundPredicate, pred, node.getAlias())) {
return node;
}
}
}
} else {
final Class<?> entityType = ((EntityLiteral) arrayExpression.getBase()).getValue();
JoinNode node = null;
AbortableResultJoinNodeVisitor<Object> visitor = new AbortableResultJoinNodeVisitor<Object>() {
@Override
public Object getStopValue() {
return this;
}
@Override
public Object visit(JoinNode node) {
if (node.isEntityJoinNode() && entityType == node.getNodeType().getJavaType()) {
Predicate pred = getArrayExpressionPredicate(node, arrayExpression);
CompoundPredicate compoundPredicate = node.getOnPredicate();
if (findPredicate(compoundPredicate, pred, node.getAlias())) {
return node;
}
}
return null;
}
@Override
public int hashCode() {
return 0;
}
// This is a fake implementation for the stopValue
@Override
public boolean equals(Object obj) {
return obj != null;
}
};
for (int i = 0; i < rootNodes.size(); i++) {
node = (JoinNode) rootNodes.get(i).accept(visitor);
if (node != null) {
break;
}
}
return node;
}
return null;
}
use of com.blazebit.persistence.parser.predicate.CompoundPredicate in project blaze-persistence by Blazebit.
the class JoinManager method generateAndApplyOnPredicate.
private void generateAndApplyOnPredicate(JoinNode joinNode, ArrayExpression arrayExpr) {
PathExpression joinAliasPathExpression = new PathExpression(new PropertyExpression(joinNode.getAlias()));
arrayExpr.getIndex().accept(new AliasReplacementVisitor(joinAliasPathExpression, ArrayExpression.ELEMENT_NAME));
Predicate filterPredicate = getArrayExpressionPredicate(joinNode, arrayExpr);
// Array expression predicates get a different root, normal expressions not
ClauseType fromClause = joinVisitor.getFromClause();
JoinNode currentJoinNode = joinVisitor.getCurrentJoinNode();
CompoundPredicate currentPred = joinNode.getOnPredicate();
boolean newPredicate = false;
if (currentPred != null) {
// Only add the predicate if it isn't contained yet
if (!findPredicate(currentPred, filterPredicate, joinNode.getAlias())) {
currentPred.getChildren().add(filterPredicate);
newPredicate = true;
}
} else {
CompoundPredicate onAndPredicate = new CompoundPredicate(CompoundPredicate.BooleanOperator.AND);
onAndPredicate.getChildren().add(filterPredicate);
joinNode.setOnPredicate(onAndPredicate);
newPredicate = true;
}
try {
joinVisitor.setFromClause(ClauseType.JOIN);
joinVisitor.setCurrentJoinNode(joinNode);
if (arrayExpr.getIndex() instanceof Predicate) {
JoinNode oldRootNode = rootNode;
try {
rootNode = joinNode;
joinNode.accept(joinVisitor);
} finally {
rootNode = oldRootNode;
}
} else {
arrayExpr.getIndex().accept(joinVisitor);
}
} finally {
joinVisitor.setFromClause(fromClause);
joinVisitor.setCurrentJoinNode(currentJoinNode);
}
if (newPredicate) {
joinNode.registerDependencies();
joinNode.updateClauseDependencies(ClauseType.JOIN, new LinkedHashSet<JoinNode>());
}
}
use of com.blazebit.persistence.parser.predicate.CompoundPredicate in project blaze-persistence by Blazebit.
the class ResolvingQueryGenerator method visit.
@Override
public void visit(CompoundPredicate predicate) {
BooleanLiteralRenderingContext oldConditionalContext = setBooleanLiteralRenderingContext(BooleanLiteralRenderingContext.PREDICATE);
ParameterRenderingMode oldParameterRenderingMode = setParameterRenderingMode(ParameterRenderingMode.PLACEHOLDER);
boolean parenthesisRequired = predicate.getChildren().size() > 1;
if (predicate.isNegated()) {
sb.append("NOT ");
if (parenthesisRequired) {
sb.append('(');
}
}
if (predicate.getChildren().size() == 1) {
predicate.getChildren().get(0).accept(this);
return;
}
final int startLen = sb.length();
final String operator = " " + predicate.getOperator().toString() + " ";
final List<Predicate> children = predicate.getChildren();
int size = children.size();
for (int i = 0; i < size; i++) {
Predicate child = children.get(i);
if (child instanceof CompoundPredicate && ((CompoundPredicate) child).getOperator() != predicate.getOperator() && !child.isNegated()) {
sb.append("(");
int len = sb.length();
child.accept(this);
// If the child was empty, we remove the opening parenthesis again
if (len == sb.length()) {
sb.deleteCharAt(len - 1);
} else {
sb.append(")");
sb.append(operator);
}
} else {
child.accept(this);
sb.append(operator);
}
}
// Delete the last operator only if the children actually generated something
if (startLen < sb.length()) {
sb.delete(sb.length() - operator.length(), sb.length());
}
if (predicate.isNegated() && parenthesisRequired) {
sb.append(')');
}
setBooleanLiteralRenderingContext(oldConditionalContext);
setParameterRenderingMode(oldParameterRenderingMode);
}
Aggregations