use of com.blazebit.persistence.parser.predicate.CompoundPredicate in project blaze-persistence by Blazebit.
the class ExpressionOptimizer method visit.
@Override
public Expression visit(CompoundPredicate predicate) {
if (predicate.getChildren().size() == 1) {
Predicate subPredicate = predicate.getChildren().get(0);
if (predicate.isNegated()) {
subPredicate.negate();
}
return subPredicate.accept(this);
} else {
boolean negate = predicate.isNegated();
if (negate) {
predicate = new CompoundPredicate(predicate.getOperator().invert(), predicate.getChildren());
}
for (int i = 0; i < predicate.getChildren().size(); i++) {
Predicate child = predicate.getChildren().get(i);
if (negate) {
child.negate();
}
predicate.getChildren().set(i, (Predicate) child.accept(this));
}
return predicate;
}
}
use of com.blazebit.persistence.parser.predicate.CompoundPredicate in project blaze-persistence by Blazebit.
the class JPQLNextExpressionVisitorImpl method visitAndPredicate.
@Override
public Expression visitAndPredicate(JPQLNextParser.AndPredicateContext ctx) {
List<JPQLNextParser.PredicateContext> predicate = ctx.predicate();
Predicate left = (Predicate) predicate.get(0).accept(this);
if (left instanceof CompoundPredicate && ((CompoundPredicate) left).getOperator() == CompoundPredicate.BooleanOperator.AND) {
((CompoundPredicate) left).getChildren().add((Predicate) predicate.get(1).accept(this));
return left;
} else {
List<Predicate> predicates = new ArrayList<>(2);
predicates.add(left);
predicates.add((Predicate) predicate.get(1).accept(this));
return new CompoundPredicate(CompoundPredicate.BooleanOperator.AND, predicates);
}
}
use of com.blazebit.persistence.parser.predicate.CompoundPredicate in project blaze-persistence by Blazebit.
the class SimpleQueryGenerator method visit.
@Override
public void visit(final 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() + " ";
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);
}
use of com.blazebit.persistence.parser.predicate.CompoundPredicate in project blaze-persistence by Blazebit.
the class JoinManager method findPredicate.
private boolean findPredicate(CompoundPredicate compoundPredicate, Predicate pred, String elementAlias) {
if (compoundPredicate != null) {
EqualityCheckingVisitor equalityCheckingVisitor = new EqualityCheckingVisitor();
List<Predicate> children = compoundPredicate.getChildren();
int size = children.size();
for (int i = 0; i < size; i++) {
if (equalityCheckingVisitor.isEqual(children.get(i), pred, elementAlias)) {
return true;
}
}
}
return false;
}
use of com.blazebit.persistence.parser.predicate.CompoundPredicate in project blaze-persistence by Blazebit.
the class JoinManager method correlate.
private JoinResult correlate(JoinResult result, String rootAlias, Expression correlatedAttributeExpr, EntityType<?> treatType, boolean finalOperation, boolean lateral, boolean implicitCorrelation) {
AttributeHolder joinResult = JpaUtils.getAttributeForJoining(metamodel, result.baseNode.getNodeType(), correlatedAttributeExpr, null);
Type<?> type = joinResult.getAttributeType();
String correlatedAttribute;
if (correlatedAttributeExpr instanceof ArrayExpression) {
correlatedAttribute = ((ArrayExpression) correlatedAttributeExpr).getBase().toString();
} else {
correlatedAttribute = correlatedAttributeExpr.toString();
}
boolean implicit = implicitCorrelation || !finalOperation;
JoinNode correlationRootNode;
// Lateral roots/joins are special i.e. we push the array index expression into the subquery/cte, so we don't have to handle this here
if ((!(correlatedAttributeExpr instanceof ArrayExpression) || lateral) && (rootNodes.isEmpty() || finalOperation)) {
if (rootAlias == null) {
StringBuilder sb = new StringBuilder(correlatedAttribute);
sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
for (int i = 0; i < sb.length(); i++) {
if ('.' == sb.charAt(i)) {
sb.setCharAt(i, '_');
}
}
String alias = sb.toString();
if (metamodel.getEntity(alias) == null && aliasManager.isAliasAvailable(alias)) {
rootAlias = alias;
} else {
rootAlias = aliasManager.generateRootAlias(alias);
}
} else if (!finalOperation) {
rootAlias = rootAlias + "_base";
}
if (metamodel.getEntity(rootAlias) != null || !aliasManager.isAliasAvailable(rootAlias)) {
rootAlias = aliasManager.generateRootAlias(rootAlias);
}
JoinAliasInfo rootAliasInfo = new JoinAliasInfo(rootAlias, rootAlias, implicit, true, aliasManager);
correlationRootNode = JoinNode.createCorrelationRootNode(result.baseNode, correlatedAttribute, joinResult.getAttribute(), type, treatType, rootAliasInfo, lateral);
rootAliasInfo.setJoinNode(correlationRootNode);
rootNodes.add(correlationRootNode);
explicitJoinNodes.add(correlationRootNode);
// register root alias in aliasManager
aliasManager.registerAliasInfo(rootAliasInfo);
} else {
// If there is a root node already, we have to correlate the base entity and join instead
String path;
if (result.baseNode.getAliasInfo() instanceof TreatedJoinAliasInfo) {
path = ((TreatedJoinAliasInfo) result.baseNode.getAliasInfo()).getTreatedJoinNode().getAliasInfo().getAbsolutePath();
} else {
path = result.baseNode.getAliasInfo().getAbsolutePath();
}
String alias = (path + "_" + correlatedAttribute).replace('.', '_') + "_base";
if (!aliasManager.isAliasAvailable(alias)) {
alias = aliasManager.generateRootAlias(alias);
}
String baseAlias = addRoot(result.baseNode.getEntityType(), alias, false);
JoinNode joinNode = ((JoinAliasInfo) aliasManager.getAliasInfo(baseAlias)).getJoinNode();
joinNode.getAliasInfo().setImplicit(true);
Predicate correlationPredicate = expressionFactory.createBooleanExpression(createCorrelationPredicate(result.baseNode.getEntityType(), result.baseNode.getAliasExpression(), baseAlias), false);
correlationPredicate.accept(joinVisitor);
joinNode.setOnPredicate(new CompoundPredicate(CompoundPredicate.BooleanOperator.AND, correlationPredicate));
if (implicit || !(correlatedAttributeExpr instanceof ArrayExpression)) {
PathExpression pathExpression = new PathExpression();
pathExpression.getExpressions().add(new PropertyExpression(baseAlias));
if (correlatedAttributeExpr instanceof PathExpression) {
pathExpression.getExpressions().addAll(((PathExpression) correlatedAttributeExpr).getExpressions());
} else {
pathExpression.getExpressions().add((PathElementExpression) correlatedAttributeExpr);
}
implicitJoin(pathExpression, true, true, true, treatType == null ? null : treatType.getName(), ClauseType.JOIN, null, false, false, true, false);
correlationRootNode = (JoinNode) pathExpression.getBaseNode();
} else {
JoinResult node = createOrUpdateNode(joinNode, Collections.singletonList(correlatedAttribute), treatType == null ? null : treatType.getName(), rootAlias, JoinType.INNER, null, false, false, true, true);
correlationRootNode = node.baseNode;
ArrayExpression arrayExpression = (ArrayExpression) correlatedAttributeExpr;
implicitJoinIndex(arrayExpression);
generateAndApplyOnPredicate(correlationRootNode, arrayExpression);
// In case we introduce a synthetic subquery for the ON clause predicate, we need to replace uses of the outer query with a subquery alias
correlationRootNode.getOnPredicate().accept(new CorrelationReplacementVisitor(result.baseNode, joinNode));
}
}
return new JoinResult(correlationRootNode);
}
Aggregations