Search in sources :

Example 1 with SemanticException

use of org.hibernate.query.SemanticException in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitQueryOrder.

protected void visitQueryOrder(SqmQueryPart<?> sqmQueryPart, HqlParser.QueryOrderContext ctx) {
    if (ctx == null) {
        return;
    }
    final SqmOrderByClause orderByClause;
    final HqlParser.OrderByClauseContext orderByClauseContext = (HqlParser.OrderByClauseContext) ctx.getChild(0);
    if (orderByClauseContext != null) {
        if (creationOptions.useStrictJpaCompliance() && processingStateStack.depth() > 1) {
            throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.SUBQUERY_ORDER_BY);
        }
        orderByClause = visitOrderByClause(orderByClauseContext);
        sqmQueryPart.setOrderByClause(orderByClause);
    } else {
        orderByClause = null;
    }
    int currentIndex = 1;
    final HqlParser.LimitClauseContext limitClauseContext;
    if (currentIndex < ctx.getChildCount() && ctx.getChild(currentIndex) instanceof HqlParser.LimitClauseContext) {
        limitClauseContext = (HqlParser.LimitClauseContext) ctx.getChild(currentIndex++);
    } else {
        limitClauseContext = null;
    }
    final HqlParser.OffsetClauseContext offsetClauseContext;
    if (currentIndex < ctx.getChildCount() && ctx.getChild(currentIndex) instanceof HqlParser.OffsetClauseContext) {
        offsetClauseContext = (HqlParser.OffsetClauseContext) ctx.getChild(currentIndex++);
    } else {
        offsetClauseContext = null;
    }
    final HqlParser.FetchClauseContext fetchClauseContext;
    if (currentIndex < ctx.getChildCount() && ctx.getChild(currentIndex) instanceof HqlParser.FetchClauseContext) {
        fetchClauseContext = (HqlParser.FetchClauseContext) ctx.getChild(currentIndex++);
    } else {
        fetchClauseContext = null;
    }
    if (currentIndex != 1) {
        if (getCreationOptions().useStrictJpaCompliance()) {
            throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.LIMIT_OFFSET_CLAUSE);
        }
        if (processingStateStack.depth() > 1 && orderByClause == null) {
            throw new SemanticException("limit, offset and fetch clause require an order-by clause when used in sub-query");
        }
        sqmQueryPart.setOffsetExpression(visitOffsetClause(offsetClauseContext));
        if (limitClauseContext == null) {
            sqmQueryPart.setFetchExpression(visitFetchClause(fetchClauseContext), visitFetchClauseType(fetchClauseContext));
        } else if (fetchClauseContext == null) {
            sqmQueryPart.setFetchExpression(visitLimitClause(limitClauseContext));
        } else {
            throw new SemanticException("Can't use both limit and fetch clause");
        }
    }
}
Also used : HqlParser(org.hibernate.grammars.hql.HqlParser) StrictJpaComplianceViolation(org.hibernate.query.sqm.StrictJpaComplianceViolation) SqmOrderByClause(org.hibernate.query.sqm.tree.select.SqmOrderByClause) SemanticException(org.hibernate.query.SemanticException)

Example 2 with SemanticException

use of org.hibernate.query.SemanticException in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitUpdateStatement.

@Override
public SqmUpdateStatement<R> visitUpdateStatement(HqlParser.UpdateStatementContext ctx) {
    final boolean versioned = !(ctx.getChild(1) instanceof HqlParser.TargetEntityContext);
    final int dmlTargetIndex = versioned ? 2 : 1;
    final HqlParser.TargetEntityContext dmlTargetContext = (HqlParser.TargetEntityContext) ctx.getChild(dmlTargetIndex);
    final SqmRoot<R> root = visitTargetEntity(dmlTargetContext);
    if (root.getReferencedPathSource() instanceof SqmPolymorphicRootDescriptor<?>) {
        throw new SemanticException(String.format("Target type '%s' in update statement is not an entity", root.getReferencedPathSource().getHibernateEntityName()));
    }
    final SqmUpdateStatement<R> updateStatement = new SqmUpdateStatement<>(root, creationContext.getNodeBuilder());
    parameterCollector = updateStatement;
    final SqmDmlCreationProcessingState processingState = new SqmDmlCreationProcessingState(updateStatement, this);
    processingStateStack.push(processingState);
    processingState.getPathRegistry().register(root);
    try {
        updateStatement.versioned(versioned);
        final HqlParser.SetClauseContext setClauseCtx = (HqlParser.SetClauseContext) ctx.getChild(dmlTargetIndex + 1);
        for (ParseTree subCtx : setClauseCtx.children) {
            if (subCtx instanceof HqlParser.AssignmentContext) {
                final HqlParser.AssignmentContext assignmentContext = (HqlParser.AssignmentContext) subCtx;
                updateStatement.applyAssignment(consumeDomainPath((HqlParser.SimplePathContext) assignmentContext.getChild(0)), (SqmExpression<?>) assignmentContext.getChild(2).accept(this));
            }
        }
        if (dmlTargetIndex + 2 <= ctx.getChildCount()) {
            updateStatement.applyPredicate(visitWhereClause((HqlParser.WhereClauseContext) ctx.getChild(dmlTargetIndex + 2)));
        }
        return updateStatement;
    } finally {
        processingStateStack.pop();
    }
}
Also used : SqmUpdateStatement(org.hibernate.query.sqm.tree.update.SqmUpdateStatement) TIMEZONE_HOUR(org.hibernate.query.sqm.TemporalUnit.TIMEZONE_HOUR) WEEK_OF_YEAR(org.hibernate.query.sqm.TemporalUnit.WEEK_OF_YEAR) DAY_OF_YEAR(org.hibernate.query.sqm.TemporalUnit.DAY_OF_YEAR) IDENTIFIER(org.hibernate.grammars.hql.HqlParser.IDENTIFIER) HqlParser(org.hibernate.grammars.hql.HqlParser) SqmPolymorphicRootDescriptor(org.hibernate.query.sqm.tree.domain.SqmPolymorphicRootDescriptor) SqmDmlCreationProcessingState(org.hibernate.query.sqm.internal.SqmDmlCreationProcessingState) ParseTree(org.antlr.v4.runtime.tree.ParseTree) SemanticException(org.hibernate.query.SemanticException)

Example 3 with SemanticException

use of org.hibernate.query.SemanticException in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitEntityNaturalIdReference.

@Override
public SqmPath<?> visitEntityNaturalIdReference(HqlParser.EntityNaturalIdReferenceContext ctx) {
    final SqmPath<Object> sqmPath = consumeDomainPath((HqlParser.PathContext) ctx.getChild(2));
    final DomainType<?> sqmPathType = sqmPath.getReferencedPathSource().getSqmPathType();
    if (sqmPathType instanceof IdentifiableDomainType<?>) {
        @SuppressWarnings("unchecked") final IdentifiableDomainType<Object> identifiableType = (IdentifiableDomainType<? super Object>) sqmPathType;
        final List<? extends PersistentAttribute<Object, ?>> attributes = identifiableType.findNaturalIdAttributes();
        if (attributes == null) {
            throw new SemanticException(String.format("Path '%s' resolved to entity type '%s' which does not define a natural id", sqmPath.getNavigablePath().getFullPath(), identifiableType.getTypeName()));
        } else if (attributes.size() > 1) {
            throw new SemanticException(String.format("Path '%s' resolved to entity type '%s' which defines multiple natural ids", sqmPath.getNavigablePath().getFullPath(), identifiableType.getTypeName()));
        }
        @SuppressWarnings("unchecked") SingularAttribute<Object, ?> naturalIdAttribute = (SingularAttribute<Object, ?>) attributes.get(0);
        return sqmPath.get(naturalIdAttribute);
    }
    throw new SemanticException("Path does not resolve to an entity type '" + sqmPath.getNavigablePath().getFullPath() + "'");
}
Also used : SingularAttribute(jakarta.persistence.metamodel.SingularAttribute) IdentifiableDomainType(org.hibernate.metamodel.model.domain.IdentifiableDomainType) HqlParser(org.hibernate.grammars.hql.HqlParser) SemanticException(org.hibernate.query.SemanticException)

Example 4 with SemanticException

use of org.hibernate.query.SemanticException in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitInstantiation.

@Override
public SqmDynamicInstantiation<?> visitInstantiation(HqlParser.InstantiationContext ctx) {
    final SqmDynamicInstantiation<?> dynamicInstantiation;
    final ParseTree instantiationTarget = ctx.instantiationTarget().getChild(0);
    if (instantiationTarget instanceof HqlParser.SimplePathContext) {
        final String className = instantiationTarget.getText();
        try {
            final JavaType<?> jtd = resolveInstantiationTargetJtd(className);
            dynamicInstantiation = SqmDynamicInstantiation.forClassInstantiation(jtd, creationContext.getNodeBuilder());
        } catch (ClassLoadingException e) {
            throw new SemanticException("Could not resolve class '" + className + "' named for instantiation");
        }
    } else {
        final TerminalNode terminalNode = (TerminalNode) instantiationTarget;
        switch(terminalNode.getSymbol().getType()) {
            case HqlParser.MAP:
                dynamicInstantiation = SqmDynamicInstantiation.forMapInstantiation(mapJavaType, creationContext.getNodeBuilder());
                break;
            case HqlParser.LIST:
                dynamicInstantiation = SqmDynamicInstantiation.forListInstantiation(listJavaType, creationContext.getNodeBuilder());
                break;
            default:
                throw new UnsupportedOperationException("Unsupported instantiation target: " + terminalNode);
        }
    }
    for (HqlParser.InstantiationArgumentContext arg : ctx.instantiationArguments().instantiationArgument()) {
        dynamicInstantiation.addArgument(visitInstantiationArgument(arg));
    }
    return dynamicInstantiation;
}
Also used : ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) HqlParser(org.hibernate.grammars.hql.HqlParser) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree) SemanticException(org.hibernate.query.SemanticException)

Example 5 with SemanticException

use of org.hibernate.query.SemanticException in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitInsertStatement.

@Override
public SqmInsertStatement<R> visitInsertStatement(HqlParser.InsertStatementContext ctx) {
    final int dmlTargetIndex;
    if (ctx.getChild(1) instanceof HqlParser.TargetEntityContext) {
        dmlTargetIndex = 1;
    } else {
        dmlTargetIndex = 2;
    }
    final HqlParser.TargetEntityContext dmlTargetContext = (HqlParser.TargetEntityContext) ctx.getChild(dmlTargetIndex);
    final HqlParser.TargetFieldsContext targetFieldsSpecContext = (HqlParser.TargetFieldsContext) ctx.getChild(dmlTargetIndex + 1);
    final SqmRoot<R> root = visitTargetEntity(dmlTargetContext);
    if (root.getReferencedPathSource() instanceof SqmPolymorphicRootDescriptor<?>) {
        throw new SemanticException(String.format("Target type '%s' in insert statement is not an entity", root.getReferencedPathSource().getHibernateEntityName()));
    }
    final HqlParser.QueryExpressionContext queryExpressionContext = ctx.queryExpression();
    if (queryExpressionContext != null) {
        final SqmInsertSelectStatement<R> insertStatement = new SqmInsertSelectStatement<>(root, creationContext.getNodeBuilder());
        parameterCollector = insertStatement;
        final SqmDmlCreationProcessingState processingState = new SqmDmlCreationProcessingState(insertStatement, this);
        processingStateStack.push(processingState);
        try {
            queryExpressionContext.accept(this);
            final SqmCreationProcessingState stateFieldsProcessingState = new SqmCreationProcessingStateImpl(insertStatement, this);
            stateFieldsProcessingState.getPathRegistry().register(root);
            processingStateStack.push(stateFieldsProcessingState);
            try {
                for (HqlParser.SimplePathContext stateFieldCtx : targetFieldsSpecContext.simplePath()) {
                    final SqmPath<?> stateField = (SqmPath<?>) visitSimplePath(stateFieldCtx);
                    // todo : validate each resolved stateField...
                    insertStatement.addInsertTargetStateField(stateField);
                }
            } finally {
                processingStateStack.pop();
            }
            return insertStatement;
        } finally {
            processingStateStack.pop();
        }
    } else {
        final SqmInsertValuesStatement<R> insertStatement = new SqmInsertValuesStatement<>(root, creationContext.getNodeBuilder());
        parameterCollector = insertStatement;
        final SqmDmlCreationProcessingState processingState = new SqmDmlCreationProcessingState(insertStatement, this);
        processingStateStack.push(processingState);
        processingState.getPathRegistry().register(root);
        try {
            final HqlParser.ValuesListContext valuesListContext = ctx.valuesList();
            for (int i = 1; i < valuesListContext.getChildCount(); i += 2) {
                final ParseTree values = valuesListContext.getChild(i);
                final SqmValues sqmValues = new SqmValues();
                for (int j = 1; j < values.getChildCount(); j += 2) {
                    sqmValues.getExpressions().add((SqmExpression<?>) values.getChild(j).accept(this));
                }
                insertStatement.getValuesList().add(sqmValues);
            }
            for (HqlParser.SimplePathContext stateFieldCtx : targetFieldsSpecContext.simplePath()) {
                final SqmPath<?> stateField = (SqmPath<?>) visitSimplePath(stateFieldCtx);
                // todo : validate each resolved stateField...
                insertStatement.addInsertTargetStateField(stateField);
            }
            return insertStatement;
        } finally {
            processingStateStack.pop();
        }
    }
}
Also used : SqmInsertValuesStatement(org.hibernate.query.sqm.tree.insert.SqmInsertValuesStatement) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) TIMEZONE_HOUR(org.hibernate.query.sqm.TemporalUnit.TIMEZONE_HOUR) WEEK_OF_YEAR(org.hibernate.query.sqm.TemporalUnit.WEEK_OF_YEAR) DAY_OF_YEAR(org.hibernate.query.sqm.TemporalUnit.DAY_OF_YEAR) IDENTIFIER(org.hibernate.grammars.hql.HqlParser.IDENTIFIER) SqmCreationProcessingState(org.hibernate.query.hql.spi.SqmCreationProcessingState) HqlParser(org.hibernate.grammars.hql.HqlParser) SqmValues(org.hibernate.query.sqm.tree.insert.SqmValues) SqmPolymorphicRootDescriptor(org.hibernate.query.sqm.tree.domain.SqmPolymorphicRootDescriptor) SqmInsertSelectStatement(org.hibernate.query.sqm.tree.insert.SqmInsertSelectStatement) SqmDmlCreationProcessingState(org.hibernate.query.sqm.internal.SqmDmlCreationProcessingState) SqmCreationProcessingStateImpl(org.hibernate.query.sqm.internal.SqmCreationProcessingStateImpl) ParseTree(org.antlr.v4.runtime.tree.ParseTree) SemanticException(org.hibernate.query.SemanticException)

Aggregations

SemanticException (org.hibernate.query.SemanticException)30 HqlParser (org.hibernate.grammars.hql.HqlParser)10 Expression (org.hibernate.sql.ast.tree.expression.Expression)8 ParseTree (org.antlr.v4.runtime.tree.ParseTree)7 StrictJpaComplianceViolation (org.hibernate.query.sqm.StrictJpaComplianceViolation)7 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)6 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)5 ParsingException (org.hibernate.query.sqm.ParsingException)4 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)4 TemporalType (jakarta.persistence.TemporalType)3 HibernateException (org.hibernate.HibernateException)3 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)3 BasicValuedMapping (org.hibernate.metamodel.mapping.BasicValuedMapping)3 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)3 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)3 ModelPart (org.hibernate.metamodel.mapping.ModelPart)3 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)3 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)3 SqmCreationProcessingState (org.hibernate.query.hql.spi.SqmCreationProcessingState)3