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