Search in sources :

Example 6 with ResultSetMappingSqlSelection

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

the class CompleteFetchBuilderEmbeddableValuedModelPart method buildFetch.

@Override
public Fetch buildFetch(FetchParent parent, NavigablePath fetchPath, JdbcValuesMetadata jdbcResultsMetadata, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    assert fetchPath.equals(navigablePath);
    final DomainResultCreationStateImpl creationStateImpl = impl(domainResultCreationState);
    final TableGroup tableGroup = creationStateImpl.getFromClauseAccess().getTableGroup(navigablePath.getParent());
    modelPart.forEachSelectable((selectionIndex, selectableMapping) -> {
        final TableReference tableReference = tableGroup.resolveTableReference(navigablePath, selectableMapping.getContainingTableExpression());
        final String mappedColumn = selectableMapping.getSelectionExpression();
        final String columnAlias = columnAliases.get(selectionIndex);
        creationStateImpl.resolveSqlSelection(creationStateImpl.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, mappedColumn), processingState -> {
            final int jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnAlias);
            final int valuesArrayPosition = jdbcPositionToValuesArrayPosition(jdbcPosition);
            return new ResultSetMappingSqlSelection(valuesArrayPosition, selectableMapping.getJdbcMapping());
        }), modelPart.getJavaType(), creationStateImpl.getSessionFactory().getTypeConfiguration());
    });
    return parent.generateFetchableFetch(modelPart, fetchPath, FetchTiming.IMMEDIATE, true, null, domainResultCreationState);
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) FetchTiming(org.hibernate.engine.FetchTiming) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) BiFunction(java.util.function.BiFunction) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) Fetch(org.hibernate.sql.results.graph.Fetch) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ResultsHelper.impl(org.hibernate.query.results.ResultsHelper.impl) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) List(java.util.List) ResultsHelper.jdbcPositionToValuesArrayPosition(org.hibernate.query.results.ResultsHelper.jdbcPositionToValuesArrayPosition) FetchBuilder(org.hibernate.query.results.FetchBuilder) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) FetchParent(org.hibernate.sql.results.graph.FetchParent) EntityValuedFetchable(org.hibernate.sql.results.graph.entity.EntityValuedFetchable) TableReference(org.hibernate.sql.ast.tree.from.TableReference) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection)

Example 7 with ResultSetMappingSqlSelection

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

the class CompleteResultBuilderBasicValuedStandard 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 BasicValuedMapping basicType;
        if (explicitType != null) {
            basicType = explicitType;
        } else {
            basicType = jdbcResultsMetadata.resolveType(jdbcPosition, explicitJavaType, sessionFactory);
        }
        final int valuesArrayPosition = ResultsHelper.jdbcPositionToValuesArrayPosition(jdbcPosition);
        return new ResultSetMappingSqlSelection(valuesArrayPosition, basicType);
    }), explicitJavaType, sessionFactory.getTypeConfiguration());
    return new BasicResult<>(sqlSelection.getValuesArrayPosition(), columnName, sqlSelection.getExpressionType().getJdbcMappings().get(0).getMappedJavaType());
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) 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) ResultBuilder(org.hibernate.query.results.ResultBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) 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 8 with ResultSetMappingSqlSelection

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

the class CompleteResultBuilderCollectionStandard method resolveSelections.

private void resolveSelections(TableGroup tableGroup, ModelPart modelPart, String[] columnNames, JdbcValuesMetadata jdbcResultsMetadata, DomainResultCreationStateImpl creationStateImpl) {
    final SelectableConsumer consumer = (selectionIndex, selectableMapping) -> {
        final String columnName = columnNames[selectionIndex];
        creationStateImpl.resolveSqlSelection(creationStateImpl.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableGroup.resolveTableReference(selectableMapping.getContainingTableExpression()), selectableMapping.getSelectionExpression()), processingState -> {
            final int jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnName);
            final BasicValuedMapping basicType = (BasicValuedMapping) selectableMapping.getJdbcMapping();
            final int valuesArrayPosition = ResultsHelper.jdbcPositionToValuesArrayPosition(jdbcPosition);
            return new ResultSetMappingSqlSelection(valuesArrayPosition, basicType);
        }), selectableMapping.getJdbcMapping().getMappedJavaType(), creationStateImpl.getSessionFactory().getTypeConfiguration());
    };
    if (modelPart instanceof EntityValuedModelPart) {
        final EntityMappingType entityMappingType = ((EntityValuedModelPart) modelPart).getEntityMappingType();
        int index = entityMappingType.getIdentifierMapping().forEachSelectable(consumer);
        if (entityMappingType.getDiscriminatorMapping() != null) {
            index += entityMappingType.getDiscriminatorMapping().forEachSelectable(index, consumer);
        }
        entityMappingType.forEachSelectable(index, consumer);
    } else {
        modelPart.forEachSelectable(consumer);
    }
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) Arrays(java.util.Arrays) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) BiFunction(java.util.function.BiFunction) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) DomainResult(org.hibernate.sql.results.graph.DomainResult) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) FromClauseAccessImpl(org.hibernate.query.results.FromClauseAccessImpl) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) ResultsHelper.impl(org.hibernate.query.results.ResultsHelper.impl) ModelPart(org.hibernate.metamodel.mapping.ModelPart) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ResultsHelper(org.hibernate.query.results.ResultsHelper) NativeQuery(org.hibernate.query.NativeQuery) ResultBuilder(org.hibernate.query.results.ResultBuilder) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SelectableConsumer(org.hibernate.metamodel.mapping.SelectableConsumer) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) SelectableConsumer(org.hibernate.metamodel.mapping.SelectableConsumer) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType)

Example 9 with ResultSetMappingSqlSelection

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

the class CompleteFetchBuilderBasicPart method buildFetch.

@Override
public BasicFetch<?> buildFetch(FetchParent parent, NavigablePath fetchPath, JdbcValuesMetadata jdbcResultsMetadata, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    final DomainResultCreationStateImpl creationState = impl(domainResultCreationState);
    final String mappedTable = referencedModelPart.getContainingTableExpression();
    final String mappedColumn = referencedModelPart.getSelectionExpression();
    final TableGroup tableGroup = creationState.getFromClauseAccess().getTableGroup(parent.getNavigablePath());
    final TableReference tableReference = tableGroup.resolveTableReference(navigablePath, mappedTable);
    final String selectedAlias;
    final int jdbcPosition;
    if (selectionAlias != null) {
        try {
            jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(selectionAlias);
        } catch (Exception e) {
            throw new MissingSqlSelectionException("ResultSet mapping specified selected-alias `" + selectionAlias + "` which was not part of the ResultSet", e);
        }
        selectedAlias = selectionAlias;
    } else {
        if (!creationState.arePositionalSelectionsAllowed()) {
            throw new PositionalSelectionsNotAllowedException("Positional SQL selection resolution not allowed");
        }
        jdbcPosition = creationState.getNumberOfProcessedSelections();
        selectedAlias = jdbcResultsMetadata.resolveColumnName(jdbcPosition);
    }
    final int valuesArrayPosition = jdbcPositionToValuesArrayPosition(jdbcPosition);
    // we just care about the registration here.  The ModelPart will find it later
    creationState.resolveSqlExpression(createColumnReferenceKey(tableReference, mappedColumn), processingState -> new ResultSetMappingSqlSelection(valuesArrayPosition, referencedModelPart));
    return (BasicFetch<?>) parent.generateFetchableFetch(referencedModelPart, fetchPath, FetchTiming.IMMEDIATE, true, selectedAlias, domainResultCreationState);
}
Also used : BasicFetch(org.hibernate.sql.results.graph.basic.BasicFetch) TableReference(org.hibernate.sql.ast.tree.from.TableReference) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) MissingSqlSelectionException(org.hibernate.query.results.MissingSqlSelectionException) PositionalSelectionsNotAllowedException(org.hibernate.query.results.PositionalSelectionsNotAllowedException) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) PositionalSelectionsNotAllowedException(org.hibernate.query.results.PositionalSelectionsNotAllowedException) MissingSqlSelectionException(org.hibernate.query.results.MissingSqlSelectionException)

Example 10 with ResultSetMappingSqlSelection

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

the class CompleteFetchBuilderEntityValuedModelPart method buildFetch.

@Override
public Fetch buildFetch(FetchParent parent, NavigablePath fetchPath, JdbcValuesMetadata jdbcResultsMetadata, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    assert fetchPath.equals(navigablePath);
    final DomainResultCreationStateImpl creationStateImpl = impl(domainResultCreationState);
    final TableGroup tableGroup = creationStateImpl.getFromClauseAccess().getTableGroup(navigablePath.getParent());
    modelPart.forEachSelectable((selectionIndex, selectableMapping) -> {
        final TableReference tableReference = tableGroup.resolveTableReference(navigablePath, selectableMapping.getContainingTableExpression());
        final String mappedColumn = selectableMapping.getSelectionExpression();
        final String columnAlias = columnAliases.get(selectionIndex);
        creationStateImpl.resolveSqlSelection(creationStateImpl.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, mappedColumn), processingState -> {
            final int jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnAlias);
            final int valuesArrayPosition = jdbcPositionToValuesArrayPosition(jdbcPosition);
            return new ResultSetMappingSqlSelection(valuesArrayPosition, selectableMapping.getJdbcMapping());
        }), modelPart.getJavaType(), creationStateImpl.getSessionFactory().getTypeConfiguration());
    });
    return parent.generateFetchableFetch(modelPart, fetchPath, FetchTiming.DELAYED, true, null, domainResultCreationState);
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) FetchTiming(org.hibernate.engine.FetchTiming) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) BiFunction(java.util.function.BiFunction) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) Fetch(org.hibernate.sql.results.graph.Fetch) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ResultsHelper.impl(org.hibernate.query.results.ResultsHelper.impl) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) List(java.util.List) ResultsHelper.jdbcPositionToValuesArrayPosition(org.hibernate.query.results.ResultsHelper.jdbcPositionToValuesArrayPosition) FetchBuilder(org.hibernate.query.results.FetchBuilder) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) FetchParent(org.hibernate.sql.results.graph.FetchParent) EntityValuedFetchable(org.hibernate.sql.results.graph.entity.EntityValuedFetchable) TableReference(org.hibernate.sql.ast.tree.from.TableReference) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection)

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