Search in sources :

Example 1 with Junction

use of org.hibernate.sql.ast.tree.predicate.Junction in project hibernate-orm by hibernate.

the class EmbeddedForeignKeyDescriptor method isSimpleJoinPredicate.

@Override
public boolean isSimpleJoinPredicate(Predicate predicate) {
    if (!(predicate instanceof Junction)) {
        return false;
    }
    final Junction junction = (Junction) predicate;
    if (junction.getNature() != Junction.Nature.CONJUNCTION) {
        return false;
    }
    final List<Predicate> predicates = junction.getPredicates();
    if (predicates.size() != keySelectableMappings.getJdbcTypeCount()) {
        return false;
    }
    Boolean lhsIsKey = null;
    for (int i = 0; i < predicates.size(); i++) {
        final Predicate p = predicates.get(i);
        if (!(p instanceof ComparisonPredicate)) {
            return false;
        }
        final ComparisonPredicate comparisonPredicate = (ComparisonPredicate) p;
        if (comparisonPredicate.getOperator() != ComparisonOperator.EQUAL) {
            return false;
        }
        final Expression lhsExpr = comparisonPredicate.getLeftHandExpression();
        final Expression rhsExpr = comparisonPredicate.getRightHandExpression();
        if (!(lhsExpr instanceof ColumnReference) || !(rhsExpr instanceof ColumnReference)) {
            return false;
        }
        final ColumnReference lhs = (ColumnReference) lhsExpr;
        final ColumnReference rhs = (ColumnReference) rhsExpr;
        if (lhsIsKey == null) {
            final String keyExpression = keySelectableMappings.getSelectable(i).getSelectionExpression();
            final String targetExpression = targetSelectableMappings.getSelectable(i).getSelectionExpression();
            if (keyExpression.equals(targetExpression)) {
                if (!lhs.getColumnExpression().equals(keyExpression) || !rhs.getColumnExpression().equals(keyExpression)) {
                    return false;
                }
            } else {
                if (keyExpression.equals(lhs.getColumnExpression())) {
                    if (!targetExpression.equals(rhs.getColumnExpression())) {
                        return false;
                    }
                    lhsIsKey = true;
                } else if (keyExpression.equals(rhs.getColumnExpression())) {
                    if (!targetExpression.equals(lhs.getColumnExpression())) {
                        return false;
                    }
                    lhsIsKey = false;
                } else {
                    return false;
                }
            }
        } else {
            final String lhsSelectionExpression;
            final String rhsSelectionExpression;
            if (lhsIsKey) {
                lhsSelectionExpression = keySelectableMappings.getSelectable(i).getSelectionExpression();
                rhsSelectionExpression = targetSelectableMappings.getSelectable(i).getSelectionExpression();
            } else {
                lhsSelectionExpression = targetSelectableMappings.getSelectable(i).getSelectionExpression();
                rhsSelectionExpression = keySelectableMappings.getSelectable(i).getSelectionExpression();
            }
            if (!lhs.getColumnExpression().equals(lhsSelectionExpression) || !rhs.getColumnExpression().equals(rhsSelectionExpression)) {
                return false;
            }
        }
    }
    return true;
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) Junction(org.hibernate.sql.ast.tree.predicate.Junction) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 2 with Junction

use of org.hibernate.sql.ast.tree.predicate.Junction in project hibernate-orm by hibernate.

the class AbstractSqlAstTranslator method visitJunctionPredicate.

private void visitJunctionPredicate(Junction.Nature nature, Predicate p) {
    if (p instanceof Junction) {
        final Junction junction = (Junction) p;
        // then we don't need parenthesis, because the AND operator binds stronger
        if (nature == junction.getNature() || nature == Junction.Nature.DISJUNCTION) {
            p.accept(this);
        } else {
            appendSql(OPEN_PARENTHESIS);
            p.accept(this);
            appendSql(CLOSE_PARENTHESIS);
        }
    } else {
        p.accept(this);
    }
}
Also used : Junction(org.hibernate.sql.ast.tree.predicate.Junction)

Example 3 with Junction

use of org.hibernate.sql.ast.tree.predicate.Junction in project hibernate-orm by hibernate.

the class AbstractCteMutationHandler method createIdSubQueryPredicate.

protected Predicate createIdSubQueryPredicate(List<? extends Expression> lhsExpressions, CteStatement idSelectCte, ModelPart fkModelPart, SessionFactoryImplementor factory) {
    final NamedTableReference idSelectTableReference = new NamedTableReference(idSelectCte.getCteTable().getTableExpression(), CTE_TABLE_IDENTIFIER, false, factory);
    final Junction predicate = new Junction(Junction.Nature.CONJUNCTION);
    final List<CteColumn> cteColumns = idSelectCte.getCteTable().getCteColumns();
    final int size = lhsExpressions.size();
    final QuerySpec subQuery = new QuerySpec(false, 1);
    subQuery.getFromClause().addRoot(new CteTableGroup(idSelectTableReference));
    final SelectClause subQuerySelectClause = subQuery.getSelectClause();
    if (fkModelPart == null) {
        for (int i = 0; i < size; i++) {
            final CteColumn cteColumn = cteColumns.get(i);
            subQuerySelectClause.addSqlSelection(new SqlSelectionImpl(i + 1, i, new ColumnReference(idSelectTableReference, cteColumn.getColumnExpression(), cteColumn.getJdbcMapping(), factory)));
        }
    } else {
        fkModelPart.forEachSelectable((selectionIndex, selectableMapping) -> {
            subQuerySelectClause.addSqlSelection(new SqlSelectionImpl(selectionIndex + 1, selectionIndex, new ColumnReference(idSelectTableReference, selectableMapping.getSelectionExpression(), selectableMapping.getJdbcMapping(), factory)));
        });
    }
    final Expression lhs;
    if (lhsExpressions.size() == 1) {
        lhs = lhsExpressions.get(0);
    } else {
        lhs = new SqlTuple(lhsExpressions, null);
    }
    predicate.add(new InSubQueryPredicate(lhs, subQuery, false));
    return predicate;
}
Also used : CteColumn(org.hibernate.sql.ast.tree.cte.CteColumn) SelectClause(org.hibernate.sql.ast.tree.select.SelectClause) NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) CteTableGroup(org.hibernate.sql.ast.tree.cte.CteTableGroup) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) Junction(org.hibernate.sql.ast.tree.predicate.Junction) SqlSelectionImpl(org.hibernate.sql.results.internal.SqlSelectionImpl) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 4 with Junction

use of org.hibernate.sql.ast.tree.predicate.Junction in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitAndPredicate.

@Override
public Junction visitAndPredicate(SqmAndPredicate predicate) {
    final Junction conjunction = new Junction(Junction.Nature.CONJUNCTION, getBooleanType());
    conjunction.add((Predicate) predicate.getLeftHandPredicate().accept(this));
    conjunction.add((Predicate) predicate.getRightHandPredicate().accept(this));
    return conjunction;
}
Also used : Junction(org.hibernate.sql.ast.tree.predicate.Junction)

Example 5 with Junction

use of org.hibernate.sql.ast.tree.predicate.Junction in project hibernate-orm by hibernate.

the class ExpressionReplacementWalker method visitJunction.

@Override
public void visitJunction(Junction junction) {
    final List<Predicate> predicates = junction.getPredicates();
    List<Predicate> newPredicates = null;
    for (int i = 0; i < predicates.size(); i++) {
        predicates.get(i).accept(this);
        if (returnedNode != predicates.get(i)) {
            if (newPredicates == null) {
                newPredicates = new ArrayList<>(predicates);
            }
            newPredicates.set(i, (Predicate) returnedNode);
        }
    }
    if (newPredicates != null) {
        returnedNode = new Junction(junction.getNature(), newPredicates, junction.getExpressionType());
    } else {
        returnedNode = junction;
    }
}
Also used : Junction(org.hibernate.sql.ast.tree.predicate.Junction) SqlFragmentPredicate(org.hibernate.persister.internal.SqlFragmentPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) BetweenPredicate(org.hibernate.sql.ast.tree.predicate.BetweenPredicate) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) GroupedPredicate(org.hibernate.sql.ast.tree.predicate.GroupedPredicate) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate) ExistsPredicate(org.hibernate.sql.ast.tree.predicate.ExistsPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) SelfRenderingPredicate(org.hibernate.sql.ast.tree.predicate.SelfRenderingPredicate) FilterPredicate(org.hibernate.sql.ast.tree.predicate.FilterPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) LikePredicate(org.hibernate.sql.ast.tree.predicate.LikePredicate)

Aggregations

Junction (org.hibernate.sql.ast.tree.predicate.Junction)11 ComparisonPredicate (org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)6 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)5 Expression (org.hibernate.sql.ast.tree.expression.Expression)4 Predicate (org.hibernate.sql.ast.tree.predicate.Predicate)4 InListPredicate (org.hibernate.sql.ast.tree.predicate.InListPredicate)3 InSubQueryPredicate (org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate)3 NegatedPredicate (org.hibernate.sql.ast.tree.predicate.NegatedPredicate)3 NullnessPredicate (org.hibernate.sql.ast.tree.predicate.NullnessPredicate)3 BetweenPredicate (org.hibernate.sql.ast.tree.predicate.BetweenPredicate)2 BooleanExpressionPredicate (org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate)2 ExistsPredicate (org.hibernate.sql.ast.tree.predicate.ExistsPredicate)2 GroupedPredicate (org.hibernate.sql.ast.tree.predicate.GroupedPredicate)2 LikePredicate (org.hibernate.sql.ast.tree.predicate.LikePredicate)2 SelfRenderingPredicate (org.hibernate.sql.ast.tree.predicate.SelfRenderingPredicate)2 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 IdentityHashMap (java.util.IdentityHashMap)1