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