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;
}
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);
}
}
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;
}
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;
}
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;
}
}
Aggregations