use of org.hibernate.query.sqm.tree.expression.SqmBinaryArithmetic 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.SqmBinaryArithmetic in project hibernate-orm by hibernate.
the class SelectClauseTests method testBinaryArithmeticExpression.
@Test
public void testBinaryArithmeticExpression() {
final String query = "select p.numberOfToes + p.numberOfToes as b from Person p";
final SqmSelectStatement<?> selectStatement = interpretSelect(query);
final SqmQuerySpec<?> querySpec = selectStatement.getQuerySpec();
final SqmSelection<?> selection = querySpec.getSelectClause().getSelections().get(0);
assertThat(querySpec.getFromClause().getRoots().size(), is(1));
final SqmRoot<?> root = querySpec.getFromClause().getRoots().get(0);
assertThat(root.getEntityName(), endsWith("Person"));
assertThat(root.getJoins().size(), is(0));
SqmBinaryArithmetic expression = (SqmBinaryArithmetic) selection.getSelectableNode();
SqmPath<?> leftHandOperand = (SqmPath<?>) expression.getLeftHandOperand();
assertThat(leftHandOperand.getLhs(), sameInstance(root));
assertThat(leftHandOperand.getReferencedPathSource().getPathName(), is("numberOfToes"));
// assertThat( leftHandOperand.getFromElement(), nullValue() );
SqmPath<?> rightHandOperand = (SqmPath<?>) expression.getRightHandOperand();
assertThat(rightHandOperand.getLhs(), sameInstance(root));
assertThat(rightHandOperand.getReferencedPathSource().getPathName(), is("numberOfToes"));
// assertThat( leftHandOperand.getFromElement(), nullValue() );
}
use of org.hibernate.query.sqm.tree.expression.SqmBinaryArithmetic in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitMultiplicationExpression.
@Override
public Object visitMultiplicationExpression(HqlParser.MultiplicationExpressionContext ctx) {
if (ctx.getChildCount() != 3) {
throw new ParsingException("Expecting 2 operands to the multiplicative operator");
}
final SqmExpression<?> left = (SqmExpression<?>) ctx.getChild(0).accept(this);
final SqmExpression<?> right = (SqmExpression<?>) ctx.getChild(2).accept(this);
final BinaryArithmeticOperator operator = (BinaryArithmeticOperator) ctx.getChild(1).accept(this);
if (operator == BinaryArithmeticOperator.MODULO) {
return getFunctionDescriptor("mod").generateSqmExpression(asList(left, right), null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
} else {
return new SqmBinaryArithmetic<>(operator, left, right, creationContext.getJpaMetamodel(), creationContext.getNodeBuilder());
}
}
use of org.hibernate.query.sqm.tree.expression.SqmBinaryArithmetic in project hibernate-orm by hibernate.
the class SelectClauseTests method testBinaryArithmeticExpressionWithMultipleFromSpaces.
@Test
public void testBinaryArithmeticExpressionWithMultipleFromSpaces() {
final String query = "select p.numberOfToes + p2.numberOfToes as b from Person p, Person p2";
final SqmSelectStatement<?> selectStatement = interpretSelect(query);
final SqmQuerySpec<?> querySpec = selectStatement.getQuerySpec();
final SqmSelection<?> selection = querySpec.getSelectClause().getSelections().get(0);
assertThat(querySpec.getFromClause().getRoots().size(), is(2));
final SqmRoot<?> entityRoot = querySpec.getFromClause().getRoots().get(0);
assertThat(entityRoot.getEntityName(), endsWith("Person"));
final SqmRoot<?> entity2Root = querySpec.getFromClause().getRoots().get(1);
assertThat(entity2Root.getEntityName(), endsWith("Person"));
SqmBinaryArithmetic addExpression = (SqmBinaryArithmetic) selection.getSelectableNode();
SqmPath<?> leftHandOperand = (SqmPath<?>) addExpression.getLeftHandOperand();
assertThat(leftHandOperand.getLhs(), sameInstance(entityRoot));
assertThat(leftHandOperand.getReferencedPathSource().getPathName(), is("numberOfToes"));
SqmPath<?> rightHandOperand = (SqmPath<?>) addExpression.getRightHandOperand();
assertThat(rightHandOperand.getLhs(), sameInstance(entity2Root));
assertThat(rightHandOperand.getReferencedPathSource().getPathName(), is("numberOfToes"));
}
Aggregations