com.blazebit.persistence.integration.hibernate.base.function.HibernateSqmFunctionDescriptorAdapter Maven / Gradle / Ivy
The newest version!
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Blazebit
*/
package com.blazebit.persistence.integration.hibernate.base.function;
import com.blazebit.persistence.spi.FunctionRenderContext;
import com.blazebit.persistence.spi.JpqlFunction;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.mapping.BasicValuedMapping;
import org.hibernate.metamodel.mapping.SqlExpressible;
import org.hibernate.query.ReturnableType;
import org.hibernate.query.spi.QueryEngine;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.SemanticQueryWalker;
import org.hibernate.query.sqm.SqmExpressible;
import org.hibernate.query.sqm.function.AbstractSqmFunctionDescriptor;
import org.hibernate.query.sqm.function.SelfRenderingSqmFunction;
import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
import org.hibernate.query.sqm.produce.function.FunctionReturnTypeResolver;
import org.hibernate.query.sqm.tree.SqmCopyContext;
import org.hibernate.query.sqm.tree.SqmTypedNode;
import org.hibernate.query.sqm.tree.SqmVisitableNode;
import org.hibernate.sql.ast.tree.expression.QueryLiteral;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.spi.TypeConfiguration;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
/**
*
* @author Christian Beikov
* @since 1.6.7
*/
public class HibernateSqmFunctionDescriptorAdapter implements JpqlFunction {
private static final Method GENERATE_SQM_EXPRESSION;
private static final Method RESOLVE_FUNCTION_RETURN_TYPE;
private static final Constructor QUERY_LITERAL_CONSTRUCTOR;
static {
Method generateSqmExpression;
try {
generateSqmExpression = SqmFunctionDescriptor.class.getMethod("generateSqmExpression", List.class, ReturnableType.class, QueryEngine.class);
} catch (NoSuchMethodException e1) {
try {
generateSqmExpression = SqmFunctionDescriptor.class.getMethod("generateSqmExpression", List.class, ReturnableType.class, QueryEngine.class, TypeConfiguration.class);
} catch (NoSuchMethodException e2) {
throw new RuntimeException("Could not find method to generate SQM expression for functions. Please report your version of hibernate so we can provide support for it!", e1);
}
}
GENERATE_SQM_EXPRESSION = generateSqmExpression;
Method resolveFunctionReturnType;
try {
resolveFunctionReturnType = FunctionReturnTypeResolver.class.getMethod("resolveFunctionReturnType", ReturnableType.class, Supplier.class, List.class, TypeConfiguration.class);
} catch (NoSuchMethodException e1) {
try {
resolveFunctionReturnType = FunctionReturnTypeResolver.class.getMethod("resolveFunctionReturnType", ReturnableType.class, List.class, TypeConfiguration.class);
} catch (NoSuchMethodException e2) {
throw new RuntimeException("Could not find method to resolve function return type. Please report your version of hibernate so we can provide support for it!", e1);
}
}
RESOLVE_FUNCTION_RETURN_TYPE = resolveFunctionReturnType;
Constructor queryLiteralConstructor;
try {
queryLiteralConstructor = QueryLiteral.class.getConstructor(Object.class, SqlExpressible.class);
} catch (NoSuchMethodException e1) {
try {
queryLiteralConstructor = QueryLiteral.class.getConstructor(Object.class, BasicValuedMapping.class);
} catch (NoSuchMethodException e2) {
throw new RuntimeException("Could not find constructor for QueryLiteral. Please report your version of hibernate so we can provide support for it!", e1);
}
}
QUERY_LITERAL_CONSTRUCTOR = queryLiteralConstructor;
}
private final SessionFactoryImplementor sfi;
private final SqmFunctionDescriptor function;
public HibernateSqmFunctionDescriptorAdapter(SessionFactoryImplementor sfi, SqmFunctionDescriptor function) {
this.sfi = sfi;
this.function = function;
}
@Override
public boolean hasArguments() {
return true;
}
@Override
public boolean hasParenthesesIfNoArguments() {
return true;
}
@Override
public Class> getReturnType(Class> firstArgumentType) {
if (firstArgumentType == null) {
return null;
}
SqmExpressible> type = sfi.getTypeConfiguration().getBasicTypeForJavaType(firstArgumentType);
if (type == null) {
final JavaType
© 2015 - 2025 Weber Informatics LLC | Privacy Policy