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();
}
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;
}
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);
}
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());
}
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()));
}
Aggregations