use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.
the class ExpressionReplacementWalker method visitLikePredicate.
@Override
public void visitLikePredicate(LikePredicate likePredicate) {
final Expression matchExpression = replaceExpression(likePredicate.getMatchExpression());
final Expression patternExpression = replaceExpression(likePredicate.getPattern());
final Expression escapeExpression = likePredicate.getEscapeCharacter() == null ? null : replaceExpression(likePredicate.getEscapeCharacter());
if (matchExpression != likePredicate.getMatchExpression() || patternExpression != likePredicate.getPattern() || escapeExpression != likePredicate.getEscapeCharacter()) {
returnedNode = new LikePredicate(matchExpression, patternExpression, escapeExpression, likePredicate.isNegated(), likePredicate.isCaseSensitive(), likePredicate.getExpressionType());
} else {
returnedNode = likePredicate;
}
}
use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.
the class LoaderSelectBuilder method applySubSelectRestriction.
private void applySubSelectRestriction(QuerySpec querySpec, NavigablePath rootNavigablePath, TableGroup rootTableGroup, SubselectFetch subselect, LoaderSqlAstCreationState sqlAstCreationState) {
final SqlAstCreationContext sqlAstCreationContext = sqlAstCreationState.getCreationContext();
final SessionFactoryImplementor sessionFactory = sqlAstCreationContext.getSessionFactory();
assert loadable instanceof PluralAttributeMapping;
final PluralAttributeMapping attributeMapping = (PluralAttributeMapping) loadable;
final ForeignKeyDescriptor fkDescriptor = attributeMapping.getKeyDescriptor();
final NavigablePath navigablePath = rootNavigablePath.append(attributeMapping.getAttributeName());
final Expression fkExpression;
final int jdbcTypeCount = fkDescriptor.getJdbcTypeCount();
if (jdbcTypeCount == 1) {
assert fkDescriptor instanceof SimpleForeignKeyDescriptor;
final SimpleForeignKeyDescriptor simpleFkDescriptor = (SimpleForeignKeyDescriptor) fkDescriptor;
final TableReference tableReference = rootTableGroup.resolveTableReference(navigablePath, simpleFkDescriptor.getContainingTableExpression());
fkExpression = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(createColumnReferenceKey(tableReference, simpleFkDescriptor.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, simpleFkDescriptor.getSelectionExpression(), false, null, null, simpleFkDescriptor.getJdbcMapping(), this.creationContext.getSessionFactory()));
} else {
final List<ColumnReference> columnReferences = new ArrayList<>(jdbcTypeCount);
fkDescriptor.forEachSelectable((columnIndex, selection) -> {
final TableReference tableReference = rootTableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
columnReferences.add((ColumnReference) sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, selection, this.creationContext.getSessionFactory())));
});
fkExpression = new SqlTuple(columnReferences, fkDescriptor);
}
querySpec.applyPredicate(new InSubQueryPredicate(fkExpression, generateSubSelect(attributeMapping, rootTableGroup, subselect, jdbcTypeCount, sqlAstCreationState, sessionFactory), false));
}
use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.
the class LoaderSelectBuilder method generateSubSelect.
private QueryPart generateSubSelect(PluralAttributeMapping attributeMapping, TableGroup rootTableGroup, SubselectFetch subselect, int jdbcTypeCount, LoaderSqlAstCreationState creationState, SessionFactoryImplementor sessionFactory) {
final ForeignKeyDescriptor fkDescriptor = attributeMapping.getKeyDescriptor();
final QuerySpec subQuery = new QuerySpec(false);
final QuerySpec loadingSqlAst = subselect.getLoadingSqlAst();
// todo (6.0) : we need to find the owner's TableGroup in the `loadingSqlAst`
final TableGroup ownerTableGroup = subselect.getOwnerTableGroup();
// transfer the from-clause
loadingSqlAst.getFromClause().visitRoots(subQuery.getFromClause()::addRoot);
final SqlExpressionResolver sqlExpressionResolver = creationState.getSqlExpressionResolver();
final NavigablePath navigablePath = ownerTableGroup.getNavigablePath().append(attributeMapping.getAttributeName());
fkDescriptor.visitTargetSelectables((valuesPosition, selection) -> {
// for each column, resolve a SqlSelection and add it to the sub-query select-clause
final TableReference tableReference = ownerTableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
final Expression expression = sqlExpressionResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, selection, sessionFactory));
subQuery.getSelectClause().addSqlSelection(new SqlSelectionImpl(valuesPosition + 1, valuesPosition, expression));
});
// transfer the restriction
subQuery.applyPredicate(loadingSqlAst.getWhereClauseRestrictions());
return subQuery;
}
use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.
the class NonAggregatedIdentifierMappingImpl method toSqlExpression.
@Override
public SqlTuple toSqlExpression(TableGroup tableGroup, Clause clause, SqmToSqlAstConverter walker, SqlAstCreationState sqlAstCreationState) {
if (hasContainingClass()) {
final SelectableMappings selectableMappings = getEmbeddableTypeDescriptor();
final List<ColumnReference> columnReferences = CollectionHelper.arrayList(selectableMappings.getJdbcTypeCount());
final NavigablePath navigablePath = tableGroup.getNavigablePath().append(getNavigableRole().getNavigableName());
final TableReference defaultTableReference = tableGroup.resolveTableReference(navigablePath, getContainingTableExpression());
int offset = 0;
for (AttributeMapping attributeMapping : identifierValueMapper.getAttributeMappings()) {
offset += attributeMapping.forEachSelectable(offset, (columnIndex, selection) -> {
final TableReference tableReference = defaultTableReference.resolveTableReference(selection.getContainingTableExpression()) != null ? defaultTableReference : tableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference.getIdentificationVariable(), selection, sqlAstCreationState.getCreationContext().getSessionFactory()));
columnReferences.add((ColumnReference) columnReference);
});
}
return new SqlTuple(columnReferences, this);
}
return super.toSqlExpression(tableGroup, clause, walker, sqlAstCreationState);
}
use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.
the class EmbeddedAttributeMapping method toSqlExpression.
@Override
public SqlTuple toSqlExpression(TableGroup tableGroup, Clause clause, SqmToSqlAstConverter walker, SqlAstCreationState sqlAstCreationState) {
final List<ColumnReference> columnReferences = CollectionHelper.arrayList(embeddableMappingType.getJdbcTypeCount());
final NavigablePath navigablePath = tableGroup.getNavigablePath().append(getNavigableRole().getNavigableName());
final TableReference defaultTableReference = tableGroup.resolveTableReference(navigablePath, getContainingTableExpression());
getEmbeddableTypeDescriptor().forEachSelectable((columnIndex, selection) -> {
final TableReference tableReference = defaultTableReference.resolveTableReference(selection.getContainingTableExpression()) != null ? defaultTableReference : tableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference.getIdentificationVariable(), selection, sqlAstCreationState.getCreationContext().getSessionFactory()));
columnReferences.add(columnReference.getColumnReference());
});
return new SqlTuple(columnReferences, this);
}
Aggregations