Search in sources :

Example 1 with SqlAliasBaseConstant

use of org.hibernate.sql.ast.spi.SqlAliasBaseConstant in project hibernate-orm by hibernate.

the class DynamicResultBuilderEntityCalculated method buildResult.

@Override
public EntityResult buildResult(JdbcValuesMetadata jdbcResultsMetadata, int resultPosition, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    final DomainResultCreationStateImpl creationStateImpl = ResultsHelper.impl(domainResultCreationState);
    final TableGroup tableGroup = entityMapping.createRootTableGroup(true, navigablePath, tableAlias, null, new SqlAliasBaseConstant(tableAlias), creationStateImpl, creationStateImpl.getFromClauseAccess(), creationStateImpl.getCreationContext());
    creationStateImpl.getFromClauseAccess().registerTableGroup(navigablePath, tableGroup);
    if (explicitLockMode != null) {
        domainResultCreationState.getSqlAstCreationState().registerLockMode(tableAlias, explicitLockMode);
    }
    return (EntityResult) entityMapping.createDomainResult(navigablePath, tableGroup, tableAlias, domainResultCreationState);
}
Also used : DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult)

Example 2 with SqlAliasBaseConstant

use of org.hibernate.sql.ast.spi.SqlAliasBaseConstant in project hibernate-orm by hibernate.

the class AbstractCollectionPersister method selectFragment.

/**
 * Generate a list of collection index, key and element columns
 */
@Override
public String selectFragment(String alias, String columnSuffix) {
    final PluralAttributeMapping attributeMapping = getAttributeMapping();
    final QuerySpec rootQuerySpec = new QuerySpec(true);
    final LoaderSqlAstCreationState sqlAstCreationState = new LoaderSqlAstCreationState(rootQuerySpec, new SqlAliasBaseManager(), new SimpleFromClauseAccessImpl(), LockOptions.NONE, (fetchParent, querySpec, creationState) -> new ArrayList<>(), true, getFactory());
    final NavigablePath entityPath = new NavigablePath(attributeMapping.getRootPathName());
    final TableGroup rootTableGroup = attributeMapping.createRootTableGroup(true, entityPath, null, () -> p -> {
    }, new SqlAliasBaseConstant(alias), sqlAstCreationState.getSqlExpressionResolver(), sqlAstCreationState.getFromClauseAccess(), getFactory());
    rootQuerySpec.getFromClause().addRoot(rootTableGroup);
    sqlAstCreationState.getFromClauseAccess().registerTableGroup(entityPath, rootTableGroup);
    attributeMapping.createDomainResult(entityPath, rootTableGroup, null, sqlAstCreationState);
    // Wrap expressions with aliases
    final SelectClause selectClause = rootQuerySpec.getSelectClause();
    final java.util.List<SqlSelection> sqlSelections = selectClause.getSqlSelections();
    int i = 0;
    for (String keyAlias : keyColumnAliases) {
        sqlSelections.set(i, new SqlSelectionImpl(i, i + 1, new AliasedExpression(sqlSelections.get(i).getExpression(), keyAlias + columnSuffix)));
        i++;
    }
    if (hasIndex) {
        for (String indexAlias : indexColumnAliases) {
            sqlSelections.set(i, new SqlSelectionImpl(i, i + 1, new AliasedExpression(sqlSelections.get(i).getExpression(), indexAlias + columnSuffix)));
            i++;
        }
    }
    if (hasIdentifier) {
        sqlSelections.set(i, new SqlSelectionImpl(i, i + 1, new AliasedExpression(sqlSelections.get(i).getExpression(), identifierColumnAlias + columnSuffix)));
        i++;
    }
    for (int columnIndex = 0; i < sqlSelections.size(); i++, columnIndex++) {
        final SqlSelection sqlSelection = sqlSelections.get(i);
        sqlSelections.set(i, new SqlSelectionImpl(sqlSelection.getValuesArrayPosition(), sqlSelection.getJdbcResultSetIndex(), new AliasedExpression(sqlSelection.getExpression(), elementColumnAliases[columnIndex] + columnSuffix)));
    }
    final String sql = getFactory().getJdbcServices().getDialect().getSqlAstTranslatorFactory().buildSelectTranslator(getFactory(), new SelectStatement(rootQuerySpec)).translate(null, QueryOptions.NONE).getSql();
    final int fromIndex = sql.lastIndexOf(" from");
    final String expression;
    if (fromIndex != -1) {
        expression = sql.substring("select ".length(), fromIndex);
    } else {
        expression = sql.substring("select ".length());
    }
    return expression;
}
Also used : SelectClause(org.hibernate.sql.ast.tree.select.SelectClause) NavigablePath(org.hibernate.query.spi.NavigablePath) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) SimpleFromClauseAccessImpl(org.hibernate.sql.ast.spi.SimpleFromClauseAccessImpl) SqlAliasBaseManager(org.hibernate.sql.ast.spi.SqlAliasBaseManager) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) AliasedExpression(org.hibernate.sql.ast.tree.expression.AliasedExpression) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) LoaderSqlAstCreationState(org.hibernate.loader.ast.internal.LoaderSqlAstCreationState) SqlSelectionImpl(org.hibernate.sql.results.internal.SqlSelectionImpl) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec)

Example 3 with SqlAliasBaseConstant

use of org.hibernate.sql.ast.spi.SqlAliasBaseConstant in project hibernate-orm by hibernate.

the class AbstractEntityPersister method selectFragment.

@Override
public String selectFragment(String alias, String suffix) {
    final QuerySpec rootQuerySpec = new QuerySpec(true);
    final String rootTableName = getRootTableName();
    final LoaderSqlAstCreationState sqlAstCreationState = new LoaderSqlAstCreationState(rootQuerySpec, new SqlAliasBaseManager(), new SimpleFromClauseAccessImpl(), LockOptions.NONE, (fetchParent, querySpec, creationState) -> {
        final List<Fetch> fetches = new ArrayList<>();
        fetchParent.getReferencedMappingContainer().visitFetchables(fetchable -> {
            // Ignore plural attributes
            if (fetchable instanceof PluralAttributeMapping) {
                return;
            }
            FetchTiming fetchTiming = fetchable.getMappedFetchOptions().getTiming();
            final boolean selectable;
            if (fetchable instanceof StateArrayContributorMapping) {
                final int propertyNumber = ((StateArrayContributorMapping) fetchable).getStateArrayPosition();
                final int tableNumber = getSubclassPropertyTableNumber(propertyNumber);
                selectable = !isSubclassTableSequentialSelect(tableNumber) && propertySelectable[propertyNumber];
            } else {
                selectable = true;
            }
            if (fetchable instanceof BasicValuedModelPart) {
                // Ignore lazy basic columns
                if (fetchTiming == FetchTiming.DELAYED) {
                    return;
                }
            } else if (fetchable instanceof Association) {
                final Association association = (Association) fetchable;
                // Ignore the fetchable if the FK is on the other side
                if (association.getSideNature() == ForeignKeyDescriptor.Nature.TARGET) {
                    return;
                }
                // Ensure the FK comes from the root table
                if (!rootTableName.equals(association.getForeignKeyDescriptor().getKeyTable())) {
                    return;
                }
                fetchTiming = FetchTiming.DELAYED;
            }
            if (selectable) {
                final NavigablePath navigablePath = fetchParent.resolveNavigablePath(fetchable);
                final Fetch fetch = fetchParent.generateFetchableFetch(fetchable, navigablePath, fetchTiming, true, null, creationState);
                fetches.add(fetch);
            }
        }, null);
        return fetches;
    }, true, getFactory());
    final NavigablePath entityPath = new NavigablePath(getRootPathName());
    final TableGroup rootTableGroup = createRootTableGroup(true, entityPath, null, () -> p -> {
    }, new SqlAliasBaseConstant(alias), sqlAstCreationState.getSqlExpressionResolver(), sqlAstCreationState.getFromClauseAccess(), getFactory());
    rootQuerySpec.getFromClause().addRoot(rootTableGroup);
    sqlAstCreationState.getFromClauseAccess().registerTableGroup(entityPath, rootTableGroup);
    createDomainResult(entityPath, rootTableGroup, null, sqlAstCreationState);
    // Wrap expressions with aliases
    final SelectClause selectClause = rootQuerySpec.getSelectClause();
    final List<SqlSelection> sqlSelections = selectClause.getSqlSelections();
    int i = 0;
    for (String identifierAlias : identifierAliases) {
        sqlSelections.set(i, new SqlSelectionImpl(i, i + 1, new AliasedExpression(sqlSelections.get(i).getExpression(), identifierAlias + suffix)));
        i++;
    }
    if (entityMetamodel.hasSubclasses()) {
        sqlSelections.set(i, new SqlSelectionImpl(i, i + 1, new AliasedExpression(sqlSelections.get(i).getExpression(), getDiscriminatorAlias() + suffix)));
        i++;
    }
    if (hasRowId()) {
        sqlSelections.set(i, new SqlSelectionImpl(i, i + 1, new AliasedExpression(sqlSelections.get(i).getExpression(), ROWID_ALIAS + suffix)));
        i++;
    }
    final String[] columnAliases = getSubclassColumnAliasClosure();
    final String[] formulaAliases = getSubclassFormulaAliasClosure();
    int columnIndex = 0;
    int formulaIndex = 0;
    for (; i < sqlSelections.size(); i++) {
        final SqlSelection sqlSelection = sqlSelections.get(i);
        final ColumnReference columnReference = (ColumnReference) sqlSelection.getExpression();
        final String selectAlias;
        if (!columnReference.isColumnExpressionFormula()) {
            // Skip over columns that are not selectable like in the fetch generation
            while (!subclassColumnSelectableClosure[columnIndex]) {
                columnIndex++;
            }
            selectAlias = columnAliases[columnIndex++] + suffix;
        } else {
            selectAlias = formulaAliases[formulaIndex++] + suffix;
        }
        sqlSelections.set(i, new SqlSelectionImpl(sqlSelection.getValuesArrayPosition(), sqlSelection.getJdbcResultSetIndex(), new AliasedExpression(sqlSelection.getExpression(), selectAlias)));
    }
    final String sql = getFactory().getJdbcServices().getDialect().getSqlAstTranslatorFactory().buildSelectTranslator(getFactory(), new SelectStatement(rootQuerySpec)).translate(null, QueryOptions.NONE).getSql();
    final int fromIndex = sql.lastIndexOf(" from");
    final String expression;
    if (fromIndex != -1) {
        expression = sql.substring("select ".length(), fromIndex);
    } else {
        expression = sql.substring("select ".length());
    }
    return expression;
}
Also used : SelectClause(org.hibernate.sql.ast.tree.select.SelectClause) NavigablePath(org.hibernate.query.spi.NavigablePath) ArrayList(java.util.ArrayList) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) SqlAliasBaseManager(org.hibernate.sql.ast.spi.SqlAliasBaseManager) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) AliasedExpression(org.hibernate.sql.ast.tree.expression.AliasedExpression) Fetch(org.hibernate.sql.results.graph.Fetch) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) Association(org.hibernate.metamodel.mapping.Association) StandardTableGroup(org.hibernate.sql.ast.tree.from.StandardTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) SimpleFromClauseAccessImpl(org.hibernate.sql.ast.spi.SimpleFromClauseAccessImpl) StateArrayContributorMapping(org.hibernate.metamodel.mapping.StateArrayContributorMapping) LoaderSqlAstCreationState(org.hibernate.loader.ast.internal.LoaderSqlAstCreationState) FetchTiming(org.hibernate.engine.FetchTiming) SqlSelectionImpl(org.hibernate.sql.results.internal.SqlSelectionImpl) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 4 with SqlAliasBaseConstant

use of org.hibernate.sql.ast.spi.SqlAliasBaseConstant in project hibernate-orm by hibernate.

the class CompleteResultBuilderEntityStandard method buildResult.

@Override
public EntityResult buildResult(JdbcValuesMetadata jdbcResultsMetadata, int resultPosition, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    final DomainResultCreationStateImpl impl = ResultsHelper.impl(domainResultCreationState);
    impl.disallowPositionalSelections();
    impl.pushExplicitFetchMementoResolver(explicitFetchBuilderMap::get);
    try {
        // we just want it added to the registry
        impl.getFromClauseAccess().resolveTableGroup(navigablePath, np -> entityDescriptor.createRootTableGroup(// since this is only used for result set mappings, the canUseInnerJoins value is irrelevant.
        true, navigablePath, tableAlias, null, new SqlAliasBaseConstant(tableAlias), impl.getSqlExpressionResolver(), impl.getFromClauseAccess(), impl.getCreationContext()));
        return new EntityResultImpl(navigablePath, entityDescriptor, tableAlias, lockMode, (entityResult) -> {
            if (discriminatorFetchBuilder == null) {
                return null;
            }
            return discriminatorFetchBuilder.buildFetch(entityResult, navigablePath.append(EntityDiscriminatorMapping.ROLE_NAME), jdbcResultsMetadata, legacyFetchResolver, domainResultCreationState);
        }, domainResultCreationState);
    } finally {
        impl.popExplicitFetchMementoResolver();
    }
}
Also used : DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant)

Example 5 with SqlAliasBaseConstant

use of org.hibernate.sql.ast.spi.SqlAliasBaseConstant in project hibernate-orm by hibernate.

the class CompleteResultBuilderCollectionStandard method buildResult.

@Override
public DomainResult<?> buildResult(JdbcValuesMetadata jdbcResultsMetadata, int resultPosition, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    final DomainResultCreationStateImpl creationStateImpl = impl(domainResultCreationState);
    final SessionFactoryImplementor sessionFactory = creationStateImpl.getSessionFactory();
    final FromClauseAccessImpl fromClauseAccess = creationStateImpl.getFromClauseAccess();
    final TableGroup rootTableGroup = pluralAttributeDescriptor.createRootTableGroup(false, navigablePath, tableAlias, null, new SqlAliasBaseConstant(tableAlias), creationStateImpl.getSqlExpressionResolver(), creationStateImpl.getFromClauseAccess(), sessionFactory);
    fromClauseAccess.registerTableGroup(navigablePath, rootTableGroup);
    resolveSelections(rootTableGroup, pluralAttributeDescriptor.getKeyDescriptor(), keyColumnNames, jdbcResultsMetadata, creationStateImpl);
    if (pluralAttributeDescriptor.getIndexDescriptor() != null) {
        resolveSelections(rootTableGroup, pluralAttributeDescriptor.getIndexDescriptor(), indexColumnNames, jdbcResultsMetadata, creationStateImpl);
    }
    resolveSelections(rootTableGroup, pluralAttributeDescriptor.getElementDescriptor(), elementColumnNames, jdbcResultsMetadata, creationStateImpl);
    return pluralAttributeDescriptor.createDomainResult(navigablePath, rootTableGroup, null, domainResultCreationState);
}
Also used : DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant) FromClauseAccessImpl(org.hibernate.query.results.FromClauseAccessImpl)

Aggregations

SqlAliasBaseConstant (org.hibernate.sql.ast.spi.SqlAliasBaseConstant)7 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)6 DomainResultCreationStateImpl (org.hibernate.query.results.DomainResultCreationStateImpl)5 NavigablePath (org.hibernate.query.spi.NavigablePath)4 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)3 LoaderSqlAstCreationState (org.hibernate.loader.ast.internal.LoaderSqlAstCreationState)2 SimpleFromClauseAccessImpl (org.hibernate.sql.ast.spi.SimpleFromClauseAccessImpl)2 SqlAliasBaseManager (org.hibernate.sql.ast.spi.SqlAliasBaseManager)2 SqlSelection (org.hibernate.sql.ast.spi.SqlSelection)2 AliasedExpression (org.hibernate.sql.ast.tree.expression.AliasedExpression)2 QuerySpec (org.hibernate.sql.ast.tree.select.QuerySpec)2 SelectClause (org.hibernate.sql.ast.tree.select.SelectClause)2 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)2 SqlSelectionImpl (org.hibernate.sql.results.internal.SqlSelectionImpl)2 ArrayList (java.util.ArrayList)1 FetchTiming (org.hibernate.engine.FetchTiming)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 Association (org.hibernate.metamodel.mapping.Association)1 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)1 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)1