Search in sources :

Example 1 with SqmEnumLiteral

use of org.hibernate.query.sqm.tree.expression.SqmEnumLiteral in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method determineValueMapping.

private MappingModelExpressible<?> determineValueMapping(SqmExpression<?> sqmExpression, FromClauseIndex fromClauseIndex) {
    if (sqmExpression instanceof SqmParameter) {
        return determineValueMapping((SqmParameter<?>) sqmExpression);
    } else if (sqmExpression instanceof SqmPath) {
        log.debugf("Determining mapping-model type for SqmPath : %s ", sqmExpression);
        final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
        return SqmMappingModelHelper.resolveMappingModelExpressible(sqmExpression, domainModel, fromClauseIndex::findTableGroup);
    } else // The model type of an enum literal is always inferred
    if (sqmExpression instanceof SqmEnumLiteral<?>) {
        final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
        if (mappingModelExpressible != null) {
            return mappingModelExpressible;
        }
    } else if (sqmExpression instanceof SqmSubQuery<?>) {
        final SqmSubQuery<?> subQuery = (SqmSubQuery<?>) sqmExpression;
        final SqmSelectClause selectClause = subQuery.getQuerySpec().getSelectClause();
        if (selectClause.getSelections().size() == 1) {
            final SqmSelection<?> subQuerySelection = selectClause.getSelections().get(0);
            final SqmSelectableNode<?> selectableNode = subQuerySelection.getSelectableNode();
            if (selectableNode instanceof SqmExpression<?>) {
                return determineValueMapping((SqmExpression<?>) selectableNode, fromClauseIndex);
            }
            final SqmExpressible<?> selectionNodeType = subQuerySelection.getNodeType();
            if (selectionNodeType != null) {
                final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
                final MappingModelExpressible<?> expressible = domainModel.resolveMappingExpressible(selectionNodeType, this::findTableGroupByPath);
                if (expressible != null) {
                    return expressible;
                }
                try {
                    final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
                    if (mappingModelExpressible != null) {
                        return mappingModelExpressible;
                    }
                } catch (Exception ignore) {
                    return null;
                }
            }
        }
    }
    log.debugf("Determining mapping-model type for generalized SqmExpression : %s", sqmExpression);
    final SqmExpressible<?> nodeType = sqmExpression.getNodeType();
    if (nodeType == null) {
        // We can't determine the type of the expression
        return null;
    }
    final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final MappingModelExpressible<?> valueMapping = domainModel.resolveMappingExpressible(nodeType, fromClauseIndex::getTableGroup);
    if (valueMapping == null) {
        final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
        if (mappingModelExpressible != null) {
            return mappingModelExpressible;
        }
    }
    if (valueMapping == null) {
        // For literals it is totally possible that we can't figure out a mapping type
        if (sqmExpression instanceof SqmLiteral<?>) {
            return null;
        }
        throw new ConversionException("Could not determine ValueMapping for SqmExpression: " + sqmExpression);
    }
    return valueMapping;
}
Also used : SqmSubQuery(org.hibernate.query.sqm.tree.select.SqmSubQuery) SqmSelectableNode(org.hibernate.query.sqm.tree.select.SqmSelectableNode) MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) SqmSelection(org.hibernate.query.sqm.tree.select.SqmSelection) SqmSelectClause(org.hibernate.query.sqm.tree.select.SqmSelectClause) SelfInterpretingSqmPath(org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) DiscriminatorSqmPath(org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath) SqmEnumLiteral(org.hibernate.query.sqm.tree.expression.SqmEnumLiteral) MultipleBagFetchException(org.hibernate.loader.MultipleBagFetchException) SemanticException(org.hibernate.query.SemanticException) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) SqlTreeCreationException(org.hibernate.sql.ast.SqlTreeCreationException) HibernateException(org.hibernate.HibernateException) QueryException(org.hibernate.QueryException) InterpretationException(org.hibernate.query.sqm.InterpretationException) SQLException(java.sql.SQLException) SqmExpressible(org.hibernate.query.sqm.SqmExpressible) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) SqmParameter(org.hibernate.query.sqm.tree.expression.SqmParameter) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral)

Aggregations

SQLException (java.sql.SQLException)1 HibernateException (org.hibernate.HibernateException)1 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)1 QueryException (org.hibernate.QueryException)1 MultipleBagFetchException (org.hibernate.loader.MultipleBagFetchException)1 MappingMetamodel (org.hibernate.metamodel.MappingMetamodel)1 MappingModelExpressible (org.hibernate.metamodel.mapping.MappingModelExpressible)1 DiscriminatorSqmPath (org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath)1 SemanticException (org.hibernate.query.SemanticException)1 InterpretationException (org.hibernate.query.sqm.InterpretationException)1 SqmExpressible (org.hibernate.query.sqm.SqmExpressible)1 SelfInterpretingSqmPath (org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath)1 SqmPath (org.hibernate.query.sqm.tree.domain.SqmPath)1 SqmEnumLiteral (org.hibernate.query.sqm.tree.expression.SqmEnumLiteral)1 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)1 SqmLiteral (org.hibernate.query.sqm.tree.expression.SqmLiteral)1 SqmParameter (org.hibernate.query.sqm.tree.expression.SqmParameter)1 SqmSelectClause (org.hibernate.query.sqm.tree.select.SqmSelectClause)1 SqmSelectableNode (org.hibernate.query.sqm.tree.select.SqmSelectableNode)1 SqmSelection (org.hibernate.query.sqm.tree.select.SqmSelection)1