Search in sources :

Example 11 with ResultSetMappingSqlSelection

use of org.hibernate.query.results.ResultSetMappingSqlSelection in project hibernate-orm by hibernate.

the class CompleteResultBuilderBasicValuedConverted method buildResult.

@Override
public BasicResult<?> buildResult(JdbcValuesMetadata jdbcResultsMetadata, int resultPosition, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    final DomainResultCreationStateImpl creationStateImpl = impl(domainResultCreationState);
    final SessionFactoryImplementor sessionFactory = creationStateImpl.getSessionFactory();
    final String columnName;
    if (explicitColumnName != null) {
        columnName = explicitColumnName;
    } else {
        columnName = jdbcResultsMetadata.resolveColumnName(resultPosition + 1);
    }
    // final int jdbcPosition;
    // if ( explicitColumnName != null ) {
    // jdbcPosition = jdbcResultsMetadata.resolveColumnPosition( explicitColumnName );
    // }
    // else {
    // jdbcPosition = resultPosition + 1;
    // }
    // 
    // final BasicValuedMapping basicType;
    // if ( explicitType != null ) {
    // basicType = explicitType;
    // }
    // else {
    // basicType = jdbcResultsMetadata.resolveType( jdbcPosition, explicitJavaType );
    // }
    // 
    // final SqlSelection sqlSelection = creationStateImpl.resolveSqlSelection(
    // creationStateImpl.resolveSqlExpression(
    // columnName,
    // processingState -> {
    // final int valuesArrayPosition = ResultsHelper.jdbcPositionToValuesArrayPosition( jdbcPosition );
    // return new SqlSelectionImpl( valuesArrayPosition, basicType );
    // }
    // ),
    // basicType.getExpressibleJavaType(),
    // sessionFactory.getTypeConfiguration()
    // );
    final SqlSelection sqlSelection = creationStateImpl.resolveSqlSelection(creationStateImpl.resolveSqlExpression(columnName, processingState -> {
        final int jdbcPosition;
        if (explicitColumnName != null) {
            jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(explicitColumnName);
        } else {
            jdbcPosition = resultPosition + 1;
        }
        final int valuesArrayPosition = ResultsHelper.jdbcPositionToValuesArrayPosition(jdbcPosition);
        return new ResultSetMappingSqlSelection(valuesArrayPosition, underlyingMapping);
    }), valueConverter.getDomainJavaType(), sessionFactory.getTypeConfiguration());
    return new BasicResult<>(sqlSelection.getValuesArrayPosition(), columnName, valueConverter.getDomainJavaType(), valueConverter);
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) JpaAttributeConverterImpl(org.hibernate.metamodel.model.convert.internal.JpaAttributeConverterImpl) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) BiFunction(java.util.function.BiFunction) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) JavaType(org.hibernate.type.descriptor.java.JavaType) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) ResultsHelper.impl(org.hibernate.query.results.ResultsHelper.impl) Objects(java.util.Objects) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) ResultsHelper(org.hibernate.query.results.ResultsHelper) ManagedBean(org.hibernate.resource.beans.spi.ManagedBean) ResultBuilder(org.hibernate.query.results.ResultBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) AttributeConverter(jakarta.persistence.AttributeConverter) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

Example 12 with ResultSetMappingSqlSelection

use of org.hibernate.query.results.ResultSetMappingSqlSelection 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)

Example 13 with ResultSetMappingSqlSelection

use of org.hibernate.query.results.ResultSetMappingSqlSelection in project hibernate-orm by hibernate.

the class DynamicResultBuilderAttribute method buildResult.

@Override
public DomainResult<?> buildResult(JdbcValuesMetadata jdbcResultsMetadata, int resultPosition, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    // todo (6.0) : TableGroups + `attributeMapping#buldResult`
    final SqlExpressionResolver sqlExpressionResolver = domainResultCreationState.getSqlAstCreationState().getSqlExpressionResolver();
    final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(columnAlias, state -> {
        final int resultSetPosition = jdbcResultsMetadata.resolveColumnPosition(columnAlias);
        final int valuesArrayPosition = resultSetPosition - 1;
        return new ResultSetMappingSqlSelection(valuesArrayPosition, attributeMapping);
    }), attributeMapping.getJavaType(), domainResultCreationState.getSqlAstCreationState().getCreationContext().getSessionFactory().getTypeConfiguration());
    return new BasicResult<>(sqlSelection.getValuesArrayPosition(), columnAlias, attributeMapping.getJavaType(), attributeMapping.getValueConverter());
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) Locale(java.util.Locale) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) BiFunction(java.util.function.BiFunction) NativeQuery(org.hibernate.query.NativeQuery) DomainResult(org.hibernate.sql.results.graph.DomainResult) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

Example 14 with ResultSetMappingSqlSelection

use of org.hibernate.query.results.ResultSetMappingSqlSelection in project hibernate-orm by hibernate.

the class DynamicResultBuilderBasicStandard method buildResult.

@Override
public BasicResult<?> buildResult(JdbcValuesMetadata jdbcResultsMetadata, int resultPosition, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    final SessionFactoryImplementor sessionFactory = domainResultCreationState.getSqlAstCreationState().getCreationContext().getSessionFactory();
    final SqlExpressionResolver sqlExpressionResolver = domainResultCreationState.getSqlAstCreationState().getSqlExpressionResolver();
    final Expression expression = sqlExpressionResolver.resolveSqlExpression(columnName, state -> {
        final int jdbcPosition;
        if (columnPosition > 0) {
            jdbcPosition = columnPosition;
        } else {
            jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnName);
        }
        final int valuesArrayPosition = ResultsHelper.jdbcPositionToValuesArrayPosition(jdbcPosition);
        final BasicType<?> basicType;
        if (explicitType != null) {
            basicType = explicitType;
        } else {
            basicType = jdbcResultsMetadata.resolveType(jdbcPosition, explicitJavaType, sessionFactory);
        }
        return new ResultSetMappingSqlSelection(valuesArrayPosition, (BasicValuedMapping) basicType);
    });
    final JavaType<?> javaType;
    if (explicitJavaType != null) {
        javaType = explicitJavaType;
    } else {
        javaType = expression.getExpressionType().getJdbcMappings().get(0).getMappedJavaType();
    }
    final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(expression, javaType, sessionFactory.getTypeConfiguration());
    // StandardRowReader expects there to be a JavaType as part of the ResultAssembler.
    assert javaType != null;
    return new BasicResult<>(sqlSelection.getValuesArrayPosition(), resultAlias, javaType);
}
Also used : SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Expression(org.hibernate.sql.ast.tree.expression.Expression) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

Aggregations

ResultSetMappingSqlSelection (org.hibernate.query.results.ResultSetMappingSqlSelection)14 BiFunction (java.util.function.BiFunction)11 DomainResultCreationStateImpl (org.hibernate.query.results.DomainResultCreationStateImpl)11 DomainResultCreationState (org.hibernate.sql.results.graph.DomainResultCreationState)11 JdbcValuesMetadata (org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata)11 SqlExpressionResolver (org.hibernate.sql.ast.spi.SqlExpressionResolver)10 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)9 ResultsHelper.impl (org.hibernate.query.results.ResultsHelper.impl)7 NavigablePath (org.hibernate.query.spi.NavigablePath)7 SqlSelection (org.hibernate.sql.ast.spi.SqlSelection)7 ResultsHelper (org.hibernate.query.results.ResultsHelper)6 DynamicFetchBuilderLegacy (org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy)6 TableReference (org.hibernate.sql.ast.tree.from.TableReference)6 BasicResult (org.hibernate.sql.results.graph.basic.BasicResult)6 List (java.util.List)5 Objects (java.util.Objects)5 FetchTiming (org.hibernate.engine.FetchTiming)5 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)5 BasicValuedMapping (org.hibernate.metamodel.mapping.BasicValuedMapping)5 NativeQuery (org.hibernate.query.NativeQuery)5