Search in sources :

Example 6 with SqmFunctionDescriptor

use of org.hibernate.query.sqm.function.SqmFunctionDescriptor in project hibernate-orm by hibernate.

the class Configuration method buildSessionFactory.

/**
 * Create a {@link SessionFactory} using the properties and mappings
 * in this configuration. The {@code SessionFactory} will be immutable,
 * so changes made to this {@code Configuration} after building the
 * factory will not affect it.
 *
 * @param serviceRegistry The registry of services to be used in creating this session factory.
 *
 * @return The newly-built {@link SessionFactory}
 *
 * @throws HibernateException usually indicates an invalid configuration or invalid mapping information
 */
public SessionFactory buildSessionFactory(ServiceRegistry serviceRegistry) throws HibernateException {
    log.debug("Building session factory using provided StandardServiceRegistry");
    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder((StandardServiceRegistry) serviceRegistry);
    if (implicitNamingStrategy != null) {
        metadataBuilder.applyImplicitNamingStrategy(implicitNamingStrategy);
    }
    if (physicalNamingStrategy != null) {
        metadataBuilder.applyPhysicalNamingStrategy(physicalNamingStrategy);
    }
    if (sharedCacheMode != null) {
        metadataBuilder.applySharedCacheMode(sharedCacheMode);
    }
    if (!typeContributorRegistrations.isEmpty()) {
        for (TypeContributor typeContributor : typeContributorRegistrations) {
            metadataBuilder.applyTypes(typeContributor);
        }
    }
    if (userTypeRegistrations != null) {
        userTypeRegistrations.forEach(registration -> registration.registerType(metadataBuilder));
    }
    if (!basicTypes.isEmpty()) {
        for (BasicType<?> basicType : basicTypes) {
            metadataBuilder.applyBasicType(basicType);
        }
    }
    if (customFunctionDescriptors != null) {
        for (Map.Entry<String, SqmFunctionDescriptor> entry : customFunctionDescriptors.entrySet()) {
            metadataBuilder.applySqlFunction(entry.getKey(), entry.getValue());
        }
    }
    if (auxiliaryDatabaseObjectList != null) {
        for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : auxiliaryDatabaseObjectList) {
            metadataBuilder.applyAuxiliaryDatabaseObject(auxiliaryDatabaseObject);
        }
    }
    if (attributeConverterDescriptorsByClass != null) {
        attributeConverterDescriptorsByClass.values().forEach(metadataBuilder::applyAttributeConverter);
    }
    final Metadata metadata = metadataBuilder.build();
    final SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();
    if (interceptor != null && interceptor != EmptyInterceptor.INSTANCE) {
        sessionFactoryBuilder.applyInterceptor(interceptor);
    }
    if (getSessionFactoryObserver() != null) {
        sessionFactoryBuilder.addSessionFactoryObservers(getSessionFactoryObserver());
    }
    if (getEntityNotFoundDelegate() != null) {
        sessionFactoryBuilder.applyEntityNotFoundDelegate(getEntityNotFoundDelegate());
    }
    if (getCurrentTenantIdentifierResolver() != null) {
        sessionFactoryBuilder.applyCurrentTenantIdentifierResolver(getCurrentTenantIdentifierResolver());
    }
    return sessionFactoryBuilder.build();
}
Also used : MetadataBuilder(org.hibernate.boot.MetadataBuilder) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) Metadata(org.hibernate.boot.Metadata) SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor) AuxiliaryDatabaseObject(org.hibernate.boot.model.relational.AuxiliaryDatabaseObject) Map(java.util.Map) HashMap(java.util.HashMap) TypeContributor(org.hibernate.boot.model.TypeContributor)

Example 7 with SqmFunctionDescriptor

use of org.hibernate.query.sqm.function.SqmFunctionDescriptor in project hibernate-orm by hibernate.

the class NvlCoalesceEmulation method generateSqmFunctionExpression.

@Override
protected <T> SelfRenderingSqmFunction<T> generateSqmFunctionExpression(List<? extends SqmTypedNode<?>> arguments, ReturnableType<T> impliedResultType, QueryEngine queryEngine, TypeConfiguration typeConfiguration) {
    SqmFunctionDescriptor nvl = queryEngine.getSqmFunctionRegistry().namedDescriptorBuilder("nvl").setExactArgumentCount(2).descriptor();
    int pos = arguments.size();
    SqmExpression<?> result = (SqmExpression<?>) arguments.get(--pos);
    ReturnableType<?> type = (ReturnableType<?>) result.getNodeType();
    while (pos > 0) {
        SqmExpression<?> next = (SqmExpression<?>) arguments.get(--pos);
        result = nvl.generateSqmExpression(asList(next, result), type, queryEngine, typeConfiguration);
    }
    // noinspection unchecked
    return (SelfRenderingSqmFunction<T>) result;
}
Also used : ReturnableType(org.hibernate.query.ReturnableType) SelfRenderingSqmFunction(org.hibernate.query.sqm.function.SelfRenderingSqmFunction) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor) AbstractSqmFunctionDescriptor(org.hibernate.query.sqm.function.AbstractSqmFunctionDescriptor)

Example 8 with SqmFunctionDescriptor

use of org.hibernate.query.sqm.function.SqmFunctionDescriptor in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitGenericFunction.

@Override
public Object visitGenericFunction(HqlParser.GenericFunctionContext ctx) {
    final String originalFunctionName = visitGenericFunctionName(ctx.genericFunctionName());
    final String functionName = originalFunctionName.toLowerCase();
    if (creationOptions.useStrictJpaCompliance() && !JPA_STANDARD_FUNCTIONS.contains(functionName)) {
        throw new StrictJpaComplianceViolation("Encountered non-compliant non-standard function call [" + originalFunctionName + "], but strict JPA " + "compliance was requested; use JPA's FUNCTION(functionName[,...]) " + "syntax name instead", StrictJpaComplianceViolation.Type.FUNCTION_CALL);
    }
    final ParseTree argumentChild = ctx.getChild(2);
    final List<SqmTypedNode<?>> functionArguments;
    if (argumentChild instanceof HqlParser.GenericFunctionArgumentsContext) {
        functionArguments = (List<SqmTypedNode<?>>) argumentChild.accept(this);
    } else if ("*".equals(argumentChild.getText())) {
        functionArguments = Collections.singletonList(new SqmStar(getCreationContext().getNodeBuilder()));
    } else {
        functionArguments = emptyList();
    }
    final Boolean fromFirst = getFromFirst(ctx);
    final Boolean respectNulls = getRespectNullsClause(ctx);
    final SqmOrderByClause withinGroup = getWithinGroup(ctx);
    final SqmPredicate filterExpression = getFilterExpression(ctx);
    final boolean hasOverClause = ctx.getChild(ctx.getChildCount() - 1) instanceof HqlParser.OverClauseContext;
    SqmFunctionDescriptor functionTemplate = getFunctionDescriptor(functionName);
    if (functionTemplate == null) {
        FunctionKind functionKind = FunctionKind.NORMAL;
        if (withinGroup != null) {
            functionKind = FunctionKind.ORDERED_SET_AGGREGATE;
        } else if (hasOverClause) {
            functionKind = FunctionKind.WINDOW;
        } else if (filterExpression != null) {
            functionKind = FunctionKind.AGGREGATE;
        }
        functionTemplate = new NamedSqmFunctionDescriptor(functionName, true, null, StandardFunctionReturnTypeResolvers.invariant(resolveExpressibleTypeBasic(Object.class)), null, functionName, functionKind, null, SqlAstNodeRenderingMode.DEFAULT);
    } else {
        if (hasOverClause && functionTemplate.getFunctionKind() == FunctionKind.NORMAL) {
            throw new SemanticException("OVER clause is illegal for normal function: " + functionName);
        } else if (!hasOverClause && functionTemplate.getFunctionKind() == FunctionKind.WINDOW) {
            throw new SemanticException("OVER clause is mandatory for window-only function: " + functionName);
        }
        if (respectNulls != null) {
            switch(functionName) {
                case "lag":
                case "lead":
                case "first_value":
                case "last_value":
                case "nth_value":
                    break;
                default:
                    throw new SemanticException("RESPECT/IGNORE NULLS is illegal for function: " + functionName);
            }
        }
        if (fromFirst != null && !"nth_value".equals(functionName)) {
            throw new SemanticException("FROM FIRST/LAST is illegal for function: " + functionName);
        }
    }
    final SqmFunction<?> function;
    switch(functionTemplate.getFunctionKind()) {
        case ORDERED_SET_AGGREGATE:
            function = functionTemplate.generateOrderedSetAggregateSqmExpression(functionArguments, filterExpression, withinGroup, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
            break;
        case AGGREGATE:
            function = functionTemplate.generateAggregateSqmExpression(functionArguments, filterExpression, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
            break;
        case WINDOW:
            function = functionTemplate.generateWindowSqmExpression(functionArguments, filterExpression, null, null, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
            break;
        default:
            if (filterExpression != null) {
                throw new ParsingException("Illegal use of a FILTER clause for non-aggregate function: " + originalFunctionName);
            }
            function = functionTemplate.generateSqmExpression(functionArguments, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
            break;
    }
    return applyOverClause(ctx, function);
}
Also used : SqmStar(org.hibernate.query.sqm.tree.expression.SqmStar) SqmPredicate(org.hibernate.query.sqm.tree.predicate.SqmPredicate) NamedSqmFunctionDescriptor(org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor) NamedSqmFunctionDescriptor(org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor) SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor) SqmTypedNode(org.hibernate.query.sqm.tree.SqmTypedNode) ParsingException(org.hibernate.query.sqm.ParsingException) StrictJpaComplianceViolation(org.hibernate.query.sqm.StrictJpaComplianceViolation) FunctionKind(org.hibernate.query.sqm.function.FunctionKind) ParseTree(org.antlr.v4.runtime.tree.ParseTree) SqmOrderByClause(org.hibernate.query.sqm.tree.select.SqmOrderByClause) SemanticException(org.hibernate.query.SemanticException)

Example 9 with SqmFunctionDescriptor

use of org.hibernate.query.sqm.function.SqmFunctionDescriptor in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitJpaNonstandardFunction.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Functions
@Override
public SqmExpression<?> visitJpaNonstandardFunction(HqlParser.JpaNonstandardFunctionContext ctx) {
    final String functionName = QuotingHelper.unquoteStringLiteral(ctx.getChild(2).getText()).toLowerCase();
    final List<SqmTypedNode<?>> functionArguments;
    if (ctx.getChildCount() > 4) {
        // noinspection unchecked
        functionArguments = (List<SqmTypedNode<?>>) ctx.getChild(4).accept(this);
    } else {
        functionArguments = emptyList();
    }
    SqmFunctionDescriptor functionTemplate = getFunctionDescriptor(functionName);
    if (functionTemplate == null) {
        functionTemplate = new NamedSqmFunctionDescriptor(functionName, true, null, StandardFunctionReturnTypeResolvers.invariant(resolveExpressibleTypeBasic(Object.class)), null);
    }
    return functionTemplate.generateSqmExpression(functionArguments, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
}
Also used : SqmTypedNode(org.hibernate.query.sqm.tree.SqmTypedNode) NamedSqmFunctionDescriptor(org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor) NamedSqmFunctionDescriptor(org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor) SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor)

Example 10 with SqmFunctionDescriptor

use of org.hibernate.query.sqm.function.SqmFunctionDescriptor in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitListaggFunction.

@Override
public Object visitListaggFunction(ListaggFunctionContext ctx) {
    if (creationOptions.useStrictJpaCompliance()) {
        throw new StrictJpaComplianceViolation("Encountered non-compliant non-standard function call [listagg], but strict JPA " + "compliance was requested; use JPA's FUNCTION(functionName[,...]) " + "syntax name instead", StrictJpaComplianceViolation.Type.FUNCTION_CALL);
    }
    final SqmFunctionDescriptor functionTemplate = getFunctionDescriptor("listagg");
    if (functionTemplate == null) {
        throw new SemanticException("The listagg function was not registered for the dialect!");
    }
    final int argumentStartIndex;
    final ParseTree thirdChild = ctx.getChild(2);
    final boolean distinct;
    if (thirdChild instanceof TerminalNode) {
        distinct = true;
        argumentStartIndex = 3;
    } else {
        distinct = false;
        argumentStartIndex = 2;
    }
    final SqmExpression<?> firstArgument = (SqmExpression<?>) ctx.getChild(argumentStartIndex).accept(this);
    final SqmExpression<?> secondArgument = (SqmExpression<?>) ctx.getChild(argumentStartIndex + 2).accept(this);
    final ParseTree overflowCtx = ctx.getChild(argumentStartIndex + 3);
    final List<SqmTypedNode<?>> functionArguments = new ArrayList<>(3);
    if (distinct) {
        functionArguments.add(new SqmDistinct<>(firstArgument, creationContext.getNodeBuilder()));
    } else {
        functionArguments.add(firstArgument);
    }
    if (overflowCtx instanceof OnOverflowClauseContext) {
        if (overflowCtx.getChildCount() > 3) {
            // ON OVERFLOW TRUNCATE
            final TerminalNode countNode = (TerminalNode) overflowCtx.getChild(overflowCtx.getChildCount() - 2);
            final boolean withCount = countNode.getSymbol().getType() == HqlParser.WITH;
            final SqmExpression<?> fillerExpression;
            if (overflowCtx.getChildCount() == 6) {
                fillerExpression = (SqmExpression<?>) overflowCtx.getChild(3).accept(this);
            } else {
                // The SQL standard says the default is three periods `...`
                fillerExpression = new SqmLiteral<>("...", secondArgument.getNodeType(), secondArgument.nodeBuilder());
            }
            // noinspection unchecked,rawtypes
            functionArguments.add(new SqmOverflow(secondArgument, fillerExpression, withCount));
        } else {
            // ON OVERFLOW ERROR
            functionArguments.add(new SqmOverflow<>(secondArgument, null, false));
        }
    } else {
        functionArguments.add(secondArgument);
    }
    final SqmOrderByClause withinGroup = getWithinGroup(ctx);
    final SqmPredicate filterExpression = getFilterExpression(ctx);
    return applyOverClause(ctx, functionTemplate.generateOrderedSetAggregateSqmExpression(functionArguments, filterExpression, withinGroup, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration()));
}
Also used : SqmPredicate(org.hibernate.query.sqm.tree.predicate.SqmPredicate) ArrayList(java.util.ArrayList) NamedSqmFunctionDescriptor(org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor) SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor) SqmTypedNode(org.hibernate.query.sqm.tree.SqmTypedNode) SqmOverflow(org.hibernate.query.sqm.tree.expression.SqmOverflow) OnOverflowClauseContext(org.hibernate.grammars.hql.HqlParser.OnOverflowClauseContext) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) StrictJpaComplianceViolation(org.hibernate.query.sqm.StrictJpaComplianceViolation) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree) SqmOrderByClause(org.hibernate.query.sqm.tree.select.SqmOrderByClause) SemanticException(org.hibernate.query.SemanticException)

Aggregations

SqmFunctionDescriptor (org.hibernate.query.sqm.function.SqmFunctionDescriptor)11 NamedSqmFunctionDescriptor (org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor)4 SqmTypedNode (org.hibernate.query.sqm.tree.SqmTypedNode)3 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)3 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 SemanticException (org.hibernate.query.SemanticException)2 StrictJpaComplianceViolation (org.hibernate.query.sqm.StrictJpaComplianceViolation)2 AbstractSqmFunctionDescriptor (org.hibernate.query.sqm.function.AbstractSqmFunctionDescriptor)2 SqmPredicate (org.hibernate.query.sqm.tree.predicate.SqmPredicate)2 SqmOrderByClause (org.hibernate.query.sqm.tree.select.SqmOrderByClause)2 SqlAppender (org.hibernate.sql.ast.spi.SqlAppender)2 TestForIssue (org.hibernate.testing.TestForIssue)2 BasicType (org.hibernate.type.BasicType)2 TypeConfiguration (org.hibernate.type.spi.TypeConfiguration)2 Test (org.junit.Test)2 Expression (jakarta.persistence.criteria.Expression)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)1