Search in sources :

Example 1 with SqmFunctionDescriptor

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

the class ParseTreeVisitor method visitIdentifier.

@Override
public OrderingExpression visitIdentifier(OrderingParser.IdentifierContext ctx) {
    final String unquotedIdentifier = (String) ctx.getChild(0).accept(this);
    final SqmFunctionDescriptor descriptor = translationContext.getFactory().getQueryEngine().getSqmFunctionRegistry().findFunctionDescriptor(unquotedIdentifier);
    // If there is no function with this name, it always requires parenthesis or if this is a quoted identifiers
    // then we interpret this as a path part instead of as function
    final String identifier = ctx.getChild(0).getText();
    if (descriptor == null || descriptor.alwaysIncludesParentheses() || !unquotedIdentifier.equals(identifier)) {
        pathConsumer.consumeIdentifier(unquotedIdentifier, identifier, true, true);
        return (OrderingExpression) pathConsumer.getConsumedPart();
    }
    return new SelfRenderingOrderingExpression(unquotedIdentifier);
}
Also used : SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor)

Example 2 with SqmFunctionDescriptor

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

the class InformixDialectTestCase method testCurrentTimestampFunction.

@Test
@TestForIssue(jiraKey = "HHH-10800")
public void testCurrentTimestampFunction() {
    SqmFunctionDescriptor functionDescriptor = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("current_timestamp");
    SelfRenderingSqmFunction<Object> sqmExpression = functionDescriptor.generateSqmExpression(null, queryEngine, new TypeConfiguration());
    BasicType<?> basicType = (BasicType<?>) sqmExpression.getNodeType();
    assertEquals(JdbcTimestampJavaType.INSTANCE, basicType.getJavaTypeDescriptor());
    assertEquals(TimestampJdbcType.INSTANCE, basicType.getJdbcType());
    SqlAppender appender = new StringBuilderSqlAppender();
    sqmExpression.getRenderingSupport().render(appender, Collections.emptyList(), null);
    assertEquals("current", appender.toString());
}
Also used : SqlAppender(org.hibernate.sql.ast.spi.SqlAppender) BasicType(org.hibernate.type.BasicType) SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with SqmFunctionDescriptor

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

the class QueryEngine method from.

public static QueryEngine from(SessionFactoryImplementor sessionFactory, MetadataImplementor metadata) {
    final QueryEngineOptions queryEngineOptions = sessionFactory.getSessionFactoryOptions();
    final SqmCreationOptions sqmCreationOptions = new SqmCreationOptionsStandard(sessionFactory);
    final Dialect dialect = sessionFactory.getJdbcServices().getDialect();
    final HqlTranslator hqlTranslator = resolveHqlTranslator(queryEngineOptions, dialect, sessionFactory, sqmCreationOptions);
    final SqmTranslatorFactory sqmTranslatorFactory = resolveSqmTranslatorFactory(queryEngineOptions, dialect);
    final SqmFunctionRegistry customSqmFunctionRegistry;
    if (queryEngineOptions.getCustomSqmFunctionRegistry() == null) {
        final Map<String, SqmFunctionDescriptor> customSqlFunctionMap = queryEngineOptions.getCustomSqlFunctionMap();
        if (customSqlFunctionMap == null || customSqlFunctionMap.isEmpty()) {
            customSqmFunctionRegistry = null;
        } else {
            customSqmFunctionRegistry = new SqmFunctionRegistry();
            customSqlFunctionMap.forEach(customSqmFunctionRegistry::register);
        }
    } else {
        customSqmFunctionRegistry = queryEngineOptions.getCustomSqmFunctionRegistry();
    }
    return new QueryEngine(sessionFactory.getUuid(), sessionFactory.getName(), sessionFactory.getSessionFactoryOptions().getJpaCompliance(), () -> sessionFactory.getRuntimeMetamodels().getJpaMetamodel(), sessionFactory.getSessionFactoryOptions().getCriteriaValueHandlingMode(), sessionFactory.getSessionFactoryOptions().getPreferredSqlTypeCodeForBoolean(), metadata.buildNamedQueryRepository(sessionFactory), hqlTranslator, sqmTranslatorFactory, sessionFactory.getServiceRegistry().getService(NativeQueryInterpreter.class), buildInterpretationCache(sessionFactory::getStatistics, sessionFactory.getProperties()), metadata.getTypeConfiguration(), dialect, customSqmFunctionRegistry, sessionFactory.getServiceRegistry());
}
Also used : SqmCreationOptionsStandard(org.hibernate.query.sqm.internal.SqmCreationOptionsStandard) SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor) NativeQueryInterpreter(org.hibernate.engine.query.spi.NativeQueryInterpreter) SqmCreationOptions(org.hibernate.query.hql.spi.SqmCreationOptions) HqlTranslator(org.hibernate.query.hql.HqlTranslator) StandardHqlTranslator(org.hibernate.query.hql.internal.StandardHqlTranslator) Dialect(org.hibernate.dialect.Dialect) StandardSqmTranslatorFactory(org.hibernate.query.sqm.sql.StandardSqmTranslatorFactory) SqmTranslatorFactory(org.hibernate.query.sqm.sql.SqmTranslatorFactory) SqmFunctionRegistry(org.hibernate.query.sqm.function.SqmFunctionRegistry)

Example 4 with SqmFunctionDescriptor

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

the class InsertSubstringOverlayEmulation method generateSqmFunctionExpression.

@Override
protected <T> SelfRenderingSqmFunction<T> generateSqmFunctionExpression(List<? extends SqmTypedNode<?>> arguments, ReturnableType<T> impliedResultType, QueryEngine queryEngine, TypeConfiguration typeConfiguration) {
    final BasicType<Integer> intType = typeConfiguration.getBasicTypeForJavaType(Integer.class);
    final BasicType<String> stringType = typeConfiguration.getBasicTypeForJavaType(String.class);
    SqmTypedNode<?> string = arguments.get(0);
    SqmTypedNode<?> replacement = arguments.get(1);
    SqmTypedNode<?> start = arguments.get(2);
    SqmTypedNode<?> length = arguments.size() > 3 ? arguments.get(3) : queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("length").generateSqmExpression(replacement, intType, queryEngine, typeConfiguration);
    SqmFunctionDescriptor insert = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("insert");
    if (insert != null) {
        return insert.generateSqmExpression(asList(string, start, length, replacement), impliedResultType, queryEngine, typeConfiguration);
    } else {
        SqmFunctionDescriptor lengthFunction = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("length");
        SqmFunctionDescriptor substring = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("substring");
        SqmFunctionDescriptor concat = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("concat");
        SqmLiteral<Integer> one = new SqmLiteral<>(1, intType, queryEngine.getCriteriaBuilder());
        SqmExpression<Integer> startPlusLength = new SqmBinaryArithmetic<>(BinaryArithmeticOperator.ADD, (SqmExpression<?>) start, (SqmExpression<?>) length, intType, queryEngine.getCriteriaBuilder());
        SqmExpression<Integer> startMinusOne = new SqmBinaryArithmetic<>(BinaryArithmeticOperator.SUBTRACT, (SqmExpression<?>) start, one, intType, queryEngine.getCriteriaBuilder());
        SqmTypedNode<?> restString = substring.generateSqmExpression(asList(string, startPlusLength), impliedResultType, queryEngine, typeConfiguration);
        if (strictSubstring) {
            restString = new SqmCaseSearched<>(stringType, start.nodeBuilder()).when(new SqmComparisonPredicate(startPlusLength, ComparisonOperator.GREATER_THAN, lengthFunction.generateSqmExpression(asList(string), intType, queryEngine, typeConfiguration), string.nodeBuilder()), new SqmLiteral<>("", stringType, string.nodeBuilder())).otherwise((Expression<? extends String>) restString);
        }
        return concat.generateSqmExpression(asList(substring.generateSqmExpression(asList(string, one, startMinusOne), impliedResultType, queryEngine, typeConfiguration), replacement, restString), impliedResultType, queryEngine, typeConfiguration);
    }
}
Also used : SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor) AbstractSqmFunctionDescriptor(org.hibernate.query.sqm.function.AbstractSqmFunctionDescriptor) SqmBinaryArithmetic(org.hibernate.query.sqm.tree.expression.SqmBinaryArithmetic) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) Expression(jakarta.persistence.criteria.Expression) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral)

Example 5 with SqmFunctionDescriptor

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

the class InformixDialectTestCase method testCurrentDateFunction.

@Test
@TestForIssue(jiraKey = "HHH-10800")
public void testCurrentDateFunction() {
    SqmFunctionDescriptor functionDescriptor = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("current_date");
    SelfRenderingSqmFunction<Object> sqmExpression = functionDescriptor.generateSqmExpression(null, queryEngine, new TypeConfiguration());
    BasicType<?> basicType = (BasicType<?>) sqmExpression.getNodeType();
    assertEquals(JdbcDateJavaType.INSTANCE, basicType.getJavaTypeDescriptor());
    assertEquals(DateJdbcType.INSTANCE, basicType.getJdbcType());
    SqlAppender appender = new StringBuilderSqlAppender();
    sqmExpression.getRenderingSupport().render(appender, Collections.emptyList(), null);
    assertEquals("today", appender.toString());
}
Also used : SqlAppender(org.hibernate.sql.ast.spi.SqlAppender) BasicType(org.hibernate.type.BasicType) SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

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