Search in sources :

Example 1 with SqmBinaryArithmetic

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);
    }
}
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 SqmBinaryArithmetic

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() );
}
Also used : SqmBinaryArithmetic(org.hibernate.query.sqm.tree.expression.SqmBinaryArithmetic) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) BaseSqmUnitTest(org.hibernate.orm.test.query.sqm.BaseSqmUnitTest) Test(org.junit.jupiter.api.Test)

Example 3 with SqmBinaryArithmetic

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());
    }
}
Also used : SqmBinaryArithmetic(org.hibernate.query.sqm.tree.expression.SqmBinaryArithmetic) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) ParsingException(org.hibernate.query.sqm.ParsingException) BinaryArithmeticOperator(org.hibernate.query.sqm.BinaryArithmeticOperator)

Example 4 with SqmBinaryArithmetic

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"));
}
Also used : SqmBinaryArithmetic(org.hibernate.query.sqm.tree.expression.SqmBinaryArithmetic) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) BaseSqmUnitTest(org.hibernate.orm.test.query.sqm.BaseSqmUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

SqmBinaryArithmetic (org.hibernate.query.sqm.tree.expression.SqmBinaryArithmetic)4 BaseSqmUnitTest (org.hibernate.orm.test.query.sqm.BaseSqmUnitTest)2 SqmPath (org.hibernate.query.sqm.tree.domain.SqmPath)2 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)2 Test (org.junit.jupiter.api.Test)2 Expression (jakarta.persistence.criteria.Expression)1 BinaryArithmeticOperator (org.hibernate.query.sqm.BinaryArithmeticOperator)1 ParsingException (org.hibernate.query.sqm.ParsingException)1 AbstractSqmFunctionDescriptor (org.hibernate.query.sqm.function.AbstractSqmFunctionDescriptor)1 SqmFunctionDescriptor (org.hibernate.query.sqm.function.SqmFunctionDescriptor)1 SqmLiteral (org.hibernate.query.sqm.tree.expression.SqmLiteral)1 SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)1