use of org.hibernate.sql.ast.tree.predicate.Predicate in project hibernate-orm by hibernate.
the class AbstractSqlAstTranslator method renderTableGroup.
protected void renderTableGroup(TableGroup tableGroup, Predicate predicate, List<TableGroupJoin> tableGroupJoinCollector) {
// Without reference joins or nested join groups, even a real table group does not need parenthesis
final boolean realTableGroup = tableGroup.isRealTableGroup() && (CollectionHelper.isNotEmpty(tableGroup.getTableReferenceJoins()) || hasNestedTableGroupsToRender(tableGroup.getNestedTableGroupJoins()));
if (realTableGroup) {
appendSql(OPEN_PARENTHESIS);
}
final LockMode effectiveLockMode = getEffectiveLockMode(tableGroup.getSourceAlias());
final boolean usesLockHint = renderPrimaryTableReference(tableGroup, effectiveLockMode);
final List<TableGroupJoin> tableGroupJoins;
if (realTableGroup) {
// For real table groups, we collect all normal table group joins within that table group
// The purpose of that is to render them in-order outside of the group/parenthesis
// This is necessary for at least Derby but is also a lot easier to read
renderTableReferenceJoins(tableGroup);
if (tableGroupJoinCollector == null) {
tableGroupJoins = new ArrayList<>();
processNestedTableGroupJoins(tableGroup, tableGroupJoins);
} else {
tableGroupJoins = null;
processNestedTableGroupJoins(tableGroup, tableGroupJoinCollector);
}
appendSql(CLOSE_PARENTHESIS);
} else {
tableGroupJoins = null;
}
if (predicate != null) {
appendSql(" on ");
predicate.accept(this);
}
if (tableGroup.isLateral() && !getDialect().supportsLateral()) {
final Predicate lateralEmulationPredicate = determineLateralEmulationPredicate(tableGroup);
if (lateralEmulationPredicate != null) {
if (predicate == null) {
appendSql(" on ");
} else {
appendSql(" and ");
}
lateralEmulationPredicate.accept(this);
}
}
if (!realTableGroup) {
renderTableReferenceJoins(tableGroup);
processNestedTableGroupJoins(tableGroup, tableGroupJoinCollector);
}
if (tableGroupJoinCollector != null) {
tableGroupJoinCollector.addAll(tableGroup.getTableGroupJoins());
} else {
if (tableGroupJoins != null) {
for (TableGroupJoin tableGroupJoin : tableGroupJoins) {
processTableGroupJoin(tableGroupJoin, null);
}
}
processTableGroupJoins(tableGroup);
}
ModelPartContainer modelPart = tableGroup.getModelPart();
if (modelPart instanceof AbstractEntityPersister) {
String[] querySpaces = (String[]) ((AbstractEntityPersister) modelPart).getQuerySpaces();
for (int i = 0; i < querySpaces.length; i++) {
registerAffectedTable(querySpaces[i]);
}
}
if (!usesLockHint && tableGroup.getSourceAlias() != null && LockMode.READ.lessThan(effectiveLockMode)) {
if (forUpdate == null) {
forUpdate = new ForUpdateClause(effectiveLockMode);
} else {
forUpdate.setLockMode(effectiveLockMode);
}
forUpdate.applyAliases(getDialect().getLockRowIdentifier(effectiveLockMode), tableGroup);
}
}
use of org.hibernate.sql.ast.tree.predicate.Predicate in project hibernate-orm by hibernate.
the class AbstractSqlAstTranslator method renderExpressionAsClauseItem.
protected void renderExpressionAsClauseItem(Expression expression) {
// Most databases do not support predicates as top level items
if (expression instanceof Predicate) {
appendSql("case when ");
expression.accept(this);
appendSql(" then ");
final Dialect dialect = getDialect();
dialect.appendBooleanValueString(this, true);
appendSql(" else ");
dialect.appendBooleanValueString(this, false);
appendSql(" end");
} else {
expression.accept(this);
}
}
use of org.hibernate.sql.ast.tree.predicate.Predicate 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;
}
}
use of org.hibernate.sql.ast.tree.predicate.Predicate in project hibernate-orm by hibernate.
the class CaseStatementDiscriminatorMappingImpl method createCaseSearchedExpression.
private Expression createCaseSearchedExpression(TableGroup entityTableGroup) {
return new SelfRenderingExpression() {
CaseSearchedExpression caseSearchedExpression;
@Override
public void renderToSql(SqlAppender sqlAppender, SqlAstTranslator<?> walker, SessionFactoryImplementor sessionFactory) {
if (caseSearchedExpression == null) {
// todo (6.0): possible optimization is to omit cases for table reference joins, that touch a super class, where a subclass is inner joined due to pruning
caseSearchedExpression = new CaseSearchedExpression(CaseStatementDiscriminatorMappingImpl.this);
tableDiscriminatorDetailsMap.forEach((tableName, tableDiscriminatorDetails) -> {
final TableReference tableReference = entityTableGroup.getTableReference(entityTableGroup.getNavigablePath(), tableName, false, false);
if (tableReference == null) {
// assume this is because it is a table that is not part of the processing entity's sub-hierarchy
return;
}
final Predicate predicate = new NullnessPredicate(new ColumnReference(tableReference, tableDiscriminatorDetails.getCheckColumnName(), false, null, null, getJdbcMapping(), getSessionFactory()), true);
caseSearchedExpression.when(predicate, new QueryLiteral<>(tableDiscriminatorDetails.getDiscriminatorValue(), getUnderlyingJdbcMappingType()));
});
}
caseSearchedExpression.accept(walker);
}
@Override
public JdbcMappingContainer getExpressionType() {
return CaseStatementDiscriminatorMappingImpl.this;
}
};
}
use of org.hibernate.sql.ast.tree.predicate.Predicate in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method withTreatRestriction.
private Expression withTreatRestriction(Expression expression, SqmPath<?> path) {
final SqmPath<?> lhs;
if (path instanceof SqmTreatedPath<?, ?>) {
lhs = path;
} else {
lhs = path.getLhs();
}
if (lhs instanceof SqmTreatedPath<?, ?>) {
final SqmTreatedPath<?, ?> treatedPath = (SqmTreatedPath<?, ?>) lhs;
final Class<?> treatTargetJavaType = treatedPath.getTreatTarget().getJavaType();
final Class<?> originalJavaType = treatedPath.getWrappedPath().getJavaType();
if (treatTargetJavaType.isAssignableFrom(originalJavaType)) {
// Treating a node to a super type can be ignored
return expression;
}
if (!(expression.getExpressionType() instanceof BasicValuedMapping)) {
// A case wrapper for non-basic paths is not possible,
// because a case expression must return a scalar value,
// so we instead add the type restriction predicate as conjunct
final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
final EntityPersister entityDescriptor = domainModel.findEntityDescriptor(treatedPath.getTreatTarget().getHibernateEntityName());
conjunctTreatUsages.computeIfAbsent(treatedPath.getWrappedPath(), p -> new HashSet<>(1)).addAll(entityDescriptor.getSubclassEntityNames());
return expression;
}
// Note: If the columns that are accessed are not shared with other entities, we could avoid this wrapping
return createCaseExpression(treatedPath.getWrappedPath(), treatedPath.getTreatTarget(), expression);
}
return expression;
}
Aggregations