Search in sources :

Example 1 with SqmLiteral

use of org.hibernate.query.sqm.tree.expression.SqmLiteral 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 2 with SqmLiteral

use of org.hibernate.query.sqm.tree.expression.SqmLiteral in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method sqlTimestampLiteralFrom.

// private SqmLiteral<OffsetDateTime> offsetDatetimeLiteralFrom(String literalText) {
// TemporalAccessor parsed = OFFSET_DATE_TIME.parse( literalText );
// return new SqmLiteral<>(
// OffsetDateTime.from( parsed ),
// resolveExpressibleTypeBasic( OffsetDateTime.class ),
// creationContext.getNodeBuilder()
// );
// }
// 
// private SqmLiteral<?> dateTimeLiteralFrom(String literalText) {
// //TO DO: return an OffsetDateTime when appropriate?
// TemporalAccessor parsed = DATE_TIME.parse( literalText );
// try {
// return new SqmLiteral<>(
// ZonedDateTime.from( parsed ),
// resolveExpressibleTypeBasic( ZonedDateTime.class ),
// creationContext.getNodeBuilder()
// );
// }
// catch (DateTimeException dte) {
// return new SqmLiteral<>(
// LocalDateTime.from( parsed ),
// resolveExpressibleTypeBasic( LocalDateTime.class ),
// creationContext.getNodeBuilder()
// );
// }
// }
// 
// private SqmLiteral<LocalDate> localDateLiteralFrom(String literalText) {
// return new SqmLiteral<>(
// LocalDate.from( ISO_LOCAL_DATE.parse( literalText ) ),
// resolveExpressibleTypeBasic( LocalDate.class ),
// creationContext.getNodeBuilder()
// );
// }
// 
// private SqmLiteral<LocalTime> localTimeLiteralFrom(String literalText) {
// return new SqmLiteral<>(
// LocalTime.from( ISO_LOCAL_TIME.parse( literalText ) ),
// resolveExpressibleTypeBasic( LocalTime.class ),
// creationContext.getNodeBuilder()
// );
// }
private SqmLiteral<?> sqlTimestampLiteralFrom(String literalText) {
    final TemporalAccessor parsed = DATE_TIME.parse(literalText.subSequence(1, literalText.length() - 1));
    try {
        final ZonedDateTime zonedDateTime = ZonedDateTime.from(parsed);
        final Calendar literal = GregorianCalendar.from(zonedDateTime);
        return new SqmLiteral<>(literal, resolveExpressibleTypeBasic(Calendar.class), creationContext.getNodeBuilder());
    } catch (DateTimeException dte) {
        final LocalDateTime localDateTime = LocalDateTime.from(parsed);
        final Timestamp literal = Timestamp.valueOf(localDateTime);
        return new SqmLiteral<>(literal, resolveExpressibleTypeBasic(Timestamp.class), creationContext.getNodeBuilder());
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeException(java.time.DateTimeException) ZonedDateTime(java.time.ZonedDateTime) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral) Timestamp(java.sql.Timestamp)

Example 3 with SqmLiteral

use of org.hibernate.query.sqm.tree.expression.SqmLiteral in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method hexLiteral.

private SqmLiteral<? extends Number> hexLiteral(String text) {
    final String originalText = text;
    text = text.substring(2);
    try {
        final Number value;
        final BasicDomainType<? extends Number> type;
        if (text.endsWith("l") || text.endsWith("L")) {
            text = text.substring(0, text.length() - 1);
            value = Long.parseUnsignedLong(text, 16);
            type = resolveExpressibleTypeBasic(Long.class);
        } else {
            value = Integer.parseUnsignedInt(text, 16);
            type = resolveExpressibleTypeBasic(Integer.class);
        }
        // noinspection unchecked
        return new SqmLiteral<>(value, (SqmExpressible<Number>) type, creationContext.getNodeBuilder());
    } catch (NumberFormatException e) {
        throw new LiteralNumberFormatException("Unable to convert sqm literal [" + originalText + "]", e);
    }
}
Also used : BigInteger(java.math.BigInteger) LiteralNumberFormatException(org.hibernate.query.sqm.LiteralNumberFormatException) LiteralNumberFormatException(org.hibernate.query.sqm.LiteralNumberFormatException) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral)

Example 4 with SqmLiteral

use of org.hibernate.query.sqm.tree.expression.SqmLiteral in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method sqlTimeLiteralFrom.

private SqmLiteral<Time> sqlTimeLiteralFrom(String literalText) {
    final LocalTime localTime = LocalTime.from(ISO_LOCAL_TIME.parse(literalText.subSequence(1, literalText.length() - 1)));
    final Time literal = Time.valueOf(localTime);
    return new SqmLiteral<>(literal, resolveExpressibleTypeBasic(Time.class), creationContext.getNodeBuilder());
}
Also used : LocalTime(java.time.LocalTime) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) Time(java.sql.Time) LocalTime(java.time.LocalTime) OffsetDateTime(java.time.OffsetDateTime) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral)

Example 5 with SqmLiteral

use of org.hibernate.query.sqm.tree.expression.SqmLiteral in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method sqlDateLiteralFrom.

private SqmLiteral<Date> sqlDateLiteralFrom(String literalText) {
    final LocalDate localDate = LocalDate.from(ISO_LOCAL_DATE.parse(literalText.subSequence(1, literalText.length() - 1)));
    final Date literal = Date.valueOf(localDate);
    return new SqmLiteral<>(literal, resolveExpressibleTypeBasic(Date.class), creationContext.getNodeBuilder());
}
Also used : LocalDate(java.time.LocalDate) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral) LocalDate(java.time.LocalDate) Date(java.sql.Date)

Aggregations

SqmLiteral (org.hibernate.query.sqm.tree.expression.SqmLiteral)11 SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)4 BigInteger (java.math.BigInteger)3 SemanticException (org.hibernate.query.SemanticException)3 SqmPath (org.hibernate.query.sqm.tree.domain.SqmPath)3 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)3 SqmParameter (org.hibernate.query.sqm.tree.expression.SqmParameter)3 SqmPredicate (org.hibernate.query.sqm.tree.predicate.SqmPredicate)3 SqmSelectClause (org.hibernate.query.sqm.tree.select.SqmSelectClause)3 SqmSubQuery (org.hibernate.query.sqm.tree.select.SqmSubQuery)3 SQLException (java.sql.SQLException)2 LocalDateTime (java.time.LocalDateTime)2 ZonedDateTime (java.time.ZonedDateTime)2 ArrayList (java.util.ArrayList)2 HibernateException (org.hibernate.HibernateException)2 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)2 QueryException (org.hibernate.QueryException)2 MultipleBagFetchException (org.hibernate.loader.MultipleBagFetchException)2 MappingMetamodel (org.hibernate.metamodel.MappingMetamodel)2 MappingModelExpressible (org.hibernate.metamodel.mapping.MappingModelExpressible)2