Search in sources :

Example 96 with Expression

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));
}
Also used : TableReference(org.hibernate.sql.ast.tree.from.TableReference) Expression(org.hibernate.sql.ast.tree.expression.Expression) OrderingExpression(org.hibernate.metamodel.mapping.ordering.ast.OrderingExpression) SortSpecification(org.hibernate.sql.ast.tree.select.SortSpecification) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 97 with Expression

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");
    }
}
Also used : TableReference(org.hibernate.sql.ast.tree.from.TableReference) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) ModelPart(org.hibernate.metamodel.mapping.ModelPart) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) Expression(org.hibernate.sql.ast.tree.expression.Expression) OrderingExpression(org.hibernate.metamodel.mapping.ordering.ast.OrderingExpression) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) ArrayList(java.util.ArrayList) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 98 with Expression

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);
}
Also used : BasicFetch(org.hibernate.sql.results.graph.basic.BasicFetch) TableReference(org.hibernate.sql.ast.tree.from.TableReference) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Expression(org.hibernate.sql.ast.tree.expression.Expression) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

Example 99 with Expression

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());
}
Also used : BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) TableReference(org.hibernate.sql.ast.tree.from.TableReference) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Expression(org.hibernate.sql.ast.tree.expression.Expression) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 100 with Expression

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);
}
Also used : BasicFetch(org.hibernate.sql.results.graph.basic.BasicFetch) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) ConvertibleModelPart(org.hibernate.metamodel.mapping.ConvertibleModelPart) Expression(org.hibernate.sql.ast.tree.expression.Expression) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

Aggregations

Expression (org.hibernate.sql.ast.tree.expression.Expression)130 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)68 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)67 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)62 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)57 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)54 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)54 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)42 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)37 SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)35 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)34 SelfRenderingSqlFragmentExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression)33 SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)32 ArrayList (java.util.ArrayList)31 SqmModifiedSubQueryExpression (org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression)31 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)28 TableReference (org.hibernate.sql.ast.tree.from.TableReference)25 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)23 ComparisonPredicate (org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)23 NavigablePath (org.hibernate.query.spi.NavigablePath)21