use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.
the class AbstractDomainPath method addSortSpecification.
private void addSortSpecification(SelectableMapping selection, QuerySpec ast, TableGroup tableGroup, String collation, SortOrder sortOrder, NullPrecedence nullPrecedence, SqlAstCreationState creationState) {
final TableReference tableReference = tableGroup.resolveTableReference(getNavigablePath(), selection.getContainingTableExpression());
final Expression expression = creationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, selection, creationState.getCreationContext().getSessionFactory()));
// SQL Server even reports a query error in this case
if (ast.hasSortSpecifications()) {
for (SortSpecification sortSpecification : ast.getSortSpecifications()) {
if (sortSpecification.getSortExpression() == expression) {
return;
}
}
}
final Expression sortExpression = OrderingExpression.applyCollation(expression, collation, creationState);
ast.addSortSpecification(new SortSpecification(sortExpression, sortOrder, nullPrecedence));
}
use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.
the class AbstractDomainPath method resolve.
public Expression resolve(ModelPart referenceModelPart, QuerySpec ast, TableGroup tableGroup, String modelPartName, SqlAstCreationState creationState) {
if (referenceModelPart instanceof BasicValuedModelPart) {
final BasicValuedModelPart selection = (BasicValuedModelPart) referenceModelPart;
final TableReference tableReference = tableGroup.resolveTableReference(getNavigablePath(), selection.getContainingTableExpression());
return creationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, selection, creationState.getCreationContext().getSessionFactory()));
} else if (referenceModelPart instanceof EntityValuedModelPart) {
final ModelPart subPart;
if (ELEMENT_TOKEN.equals(modelPartName)) {
subPart = ((EntityValuedModelPart) referenceModelPart).getEntityMappingType().getIdentifierMapping();
} else {
subPart = ((EntityValuedModelPart) referenceModelPart).findSubPart(modelPartName);
}
return resolve(subPart, ast, tableGroup, modelPartName, creationState);
} else if (referenceModelPart instanceof EmbeddableValuedModelPart) {
final EmbeddableValuedModelPart embeddableValuedModelPart = (EmbeddableValuedModelPart) referenceModelPart;
if (embeddableValuedModelPart.getFetchableName().equals(modelPartName) || ELEMENT_TOKEN.equals(modelPartName)) {
final List<Expression> expressions = new ArrayList<>(embeddableValuedModelPart.getNumberOfFetchables());
embeddableValuedModelPart.visitFetchables(fetchable -> {
expressions.add(resolve(fetchable, ast, tableGroup, modelPartName, creationState));
}, null);
return new SqlTuple(expressions, embeddableValuedModelPart);
} else {
ModelPart subPart = embeddableValuedModelPart.findSubPart(modelPartName, null);
assert subPart instanceof BasicValuedModelPart;
return resolve(subPart, ast, tableGroup, modelPartName, creationState);
}
} else {
// sure it can happen
throw new NotYetImplementedFor6Exception("Ordering for " + referenceModelPart + " not supported");
}
}
use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.
the class AnyKeyPart method generateFetch.
@Override
public Fetch generateFetch(FetchParent fetchParent, NavigablePath fetchablePath, FetchTiming fetchTiming, boolean selected, String resultVariable, DomainResultCreationState creationState) {
final FromClauseAccess fromClauseAccess = creationState.getSqlAstCreationState().getFromClauseAccess();
final SqlExpressionResolver sqlExpressionResolver = creationState.getSqlAstCreationState().getSqlExpressionResolver();
final SessionFactoryImplementor sessionFactory = creationState.getSqlAstCreationState().getCreationContext().getSessionFactory();
final TableGroup tableGroup = fromClauseAccess.getTableGroup(fetchParent.getNavigablePath().getParent());
final TableReference tableReference = tableGroup.resolveTableReference(fetchablePath, table);
final Expression columnReference = sqlExpressionResolver.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, column), processingState -> new ColumnReference(tableReference, column, false, null, null, jdbcMapping, sessionFactory));
final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(columnReference, getJavaType(), sessionFactory.getTypeConfiguration());
return new BasicFetch<>(sqlSelection.getValuesArrayPosition(), fetchParent, fetchablePath, this, null, fetchTiming, creationState);
}
use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.
the class BasicEntityIdentifierMappingImpl method resolveSqlSelection.
private SqlSelection resolveSqlSelection(NavigablePath navigablePath, TableGroup tableGroup, boolean allowFkOptimization, DomainResultCreationState creationState) {
final SqlExpressionResolver expressionResolver = creationState.getSqlAstCreationState().getSqlExpressionResolver();
final TableReference rootTableReference;
try {
rootTableReference = tableGroup.resolveTableReference(navigablePath, rootTable, allowFkOptimization);
} catch (Exception e) {
throw new IllegalStateException(String.format(Locale.ROOT, "Could not resolve table reference `%s` relative to TableGroup `%s` related with NavigablePath `%s`", rootTable, tableGroup, navigablePath), e);
}
final Expression expression = expressionResolver.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(rootTableReference, pkColumnName), sqlAstProcessingState -> new ColumnReference(rootTableReference, pkColumnName, false, null, null, ((BasicValuedMapping) entityPersister.getIdentifierType()).getJdbcMapping(), sessionFactory));
return expressionResolver.resolveSqlSelection(expression, idType.getExpressibleJavaType(), sessionFactory.getTypeConfiguration());
}
use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.
the class ImplicitFetchBuilderBasic method buildFetch.
@Override
public BasicFetch<?> buildFetch(FetchParent parent, NavigablePath fetchPath, JdbcValuesMetadata jdbcResultsMetadata, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
final DomainResultCreationStateImpl creationStateImpl = ResultsHelper.impl(domainResultCreationState);
final TableGroup parentTableGroup = creationStateImpl.getFromClauseAccess().getTableGroup(parent.getNavigablePath());
final String table = fetchable.getContainingTableExpression();
final String column;
// In case of a formula we look for a result set position with the fetchable name
if (fetchable.isFormula()) {
column = fetchable.getFetchableName();
} else {
column = fetchable.getSelectionExpression();
}
final Expression expression = creationStateImpl.resolveSqlExpression(createColumnReferenceKey(parentTableGroup.resolveTableReference(fetchPath, table), fetchable.getSelectionExpression()), processingState -> {
final int jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(column);
final int valuesArrayPosition = jdbcPositionToValuesArrayPosition(jdbcPosition);
return new ResultSetMappingSqlSelection(valuesArrayPosition, fetchable);
});
final SqlSelection sqlSelection = creationStateImpl.resolveSqlSelection(expression, fetchable.getJavaType(), domainResultCreationState.getSqlAstCreationState().getCreationContext().getSessionFactory().getTypeConfiguration());
final BasicValueConverter<?, ?> valueConverter;
if (fetchable instanceof ConvertibleModelPart) {
valueConverter = ((ConvertibleModelPart) fetchable).getValueConverter();
} else {
valueConverter = null;
}
return new BasicFetch<>(sqlSelection.getValuesArrayPosition(), parent, fetchPath, fetchable, valueConverter, FetchTiming.IMMEDIATE, domainResultCreationState);
}
Aggregations