Search in sources :

Example 1 with SqmComparisonPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate 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 SqmComparisonPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate in project hibernate-orm by hibernate.

the class AliasCollisionTest method testSubqueryUsingIdentificationVariableDefinedInRootQuery.

@Test
public void testSubqueryUsingIdentificationVariableDefinedInRootQuery() {
    final String query = "select a from SimpleEntity a where a.someString in " + "( select b.someString from SimpleEntity b where a.someLong = b.someLong )";
    final SqmSelectStatement<?> sqm = interpretSelect(query);
    final SqmQuerySpec<?> querySpec = sqm.getQuerySpec();
    final List<SqmSelection<?>> selections = querySpec.getSelectClause().getSelections();
    assertThat(selections, hasSize(1));
    assertThat(selections.get(0).getAlias(), nullValue());
    final List<SqmRoot<?>> roots = querySpec.getFromClause().getRoots();
    assertThat(roots, hasSize(1));
    assertThat(roots.get(0).getJoins(), isEmpty());
    assertThat(roots.get(0).getExplicitAlias(), is("a"));
    assertThat(querySpec.getWhereClause().getPredicate(), instanceOf(SqmInSubQueryPredicate.class));
    final SqmInSubQueryPredicate predicate = (SqmInSubQueryPredicate) querySpec.getWhereClause().getPredicate();
    final SqmQuerySpec subQuerySpec = predicate.getSubQueryExpression().getQuerySpec();
    assertThat(subQuerySpec.getFromClause().getRoots().get(0).getExplicitAlias(), is("b"));
    final SqmComparisonPredicate correlation = (SqmComparisonPredicate) subQuerySpec.getWhereClause().getPredicate();
    final SqmSimplePath leftHandExpression = (SqmSimplePath) correlation.getLeftHandExpression();
    assertThat(leftHandExpression.getLhs().getExplicitAlias(), is("a"));
    final SqmSimplePath rightHandExpression = (SqmSimplePath) correlation.getRightHandExpression();
    assertThat(rightHandExpression.getLhs().getExplicitAlias(), is("b"));
}
Also used : SqmSelection(org.hibernate.query.sqm.tree.select.SqmSelection) SqmSimplePath(org.hibernate.query.sqm.tree.domain.SqmSimplePath) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) SqmQuerySpec(org.hibernate.query.sqm.tree.select.SqmQuerySpec) SqmRoot(org.hibernate.query.sqm.tree.from.SqmRoot) SqmInSubQueryPredicate(org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate) BaseSqmUnitTest(org.hibernate.orm.test.query.sqm.BaseSqmUnitTest) Test(org.junit.jupiter.api.Test)

Example 3 with SqmComparisonPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate in project hibernate-orm by hibernate.

the class AttributePathTests method testCanonicalReferences.

@Test
public void testCanonicalReferences() {
    final SqmSelectStatement<?> sqm = interpretSelect("select s.mate from Person s where id(s) = ?1");
    assertThat(sqm.getQuerySpec().getRestriction(), notNullValue());
    final SqmComparisonPredicate restriction = (SqmComparisonPredicate) sqm.getQuerySpec().getRestriction();
    assertThat(restriction, notNullValue());
}
Also used : SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) BaseSqmUnitTest(org.hibernate.orm.test.query.sqm.BaseSqmUnitTest) Test(org.junit.jupiter.api.Test)

Example 4 with SqmComparisonPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate in project hibernate-orm by hibernate.

the class CaseExpressionsTest method testBasicSearchedCaseExpression.

@Test
public void testBasicSearchedCaseExpression() {
    SqmSelectStatement<?> select = interpretSelect("select p from Person p where p.numberOfToes = case when p.dob = ?1 then 6 else 8 end");
    final SqmComparisonPredicate predicate = TestingUtil.cast(select.getQuerySpec().getWhereClause().getPredicate(), SqmComparisonPredicate.class);
    final SqmCaseSearched caseStatement = TestingUtil.cast(predicate.getRightHandExpression(), SqmCaseSearched.class);
    assertThat(caseStatement.getOtherwise(), notNullValue());
    assertThat(caseStatement.getOtherwise(), instanceOf(SqmLiteral.class));
    assertThat(caseStatement.getWhenFragments().size(), is(1));
}
Also used : SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral) SqmCaseSearched(org.hibernate.query.sqm.tree.expression.SqmCaseSearched) BaseSqmUnitTest(org.hibernate.orm.test.query.sqm.BaseSqmUnitTest) Test(org.junit.jupiter.api.Test)

Example 5 with SqmComparisonPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate in project hibernate-orm by hibernate.

the class QueryBuilderTest method testEqualityComparisonLiteralConversion.

@Test
public void testEqualityComparisonLiteralConversion() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    Metamodel mm = em.getMetamodel();
    CriteriaQuery<Integer> cquery = cb.createQuery(Integer.class);
    Root<Product> product = cquery.from(Product.class);
    EntityType<Product> Product_ = mm.entity(Product.class);
    cquery.select(cb.toInteger(product.get(Product_.getSingularAttribute("quantity", Integer.class))));
    SqmComparisonPredicate predicate = (SqmComparisonPredicate) cb.equal(product.get(Product_.getSingularAttribute("partNumber", Long.class)), 373767373);
    assertEquals(Long.class, predicate.getLeftHandExpression().getJavaType());
    cquery.where(predicate);
    em.createQuery(cquery).getResultList();
    predicate = (SqmComparisonPredicate) cb.ge(cb.length(product.get(Product_.getSingularAttribute("name", String.class))), 4L);
    assertEquals(Integer.class, predicate.getLeftHandExpression().getJavaType());
    cquery.where(predicate);
    em.createQuery(cquery).getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) Product(org.hibernate.orm.test.jpa.metamodel.Product) Metamodel(jakarta.persistence.metamodel.Metamodel) Test(org.junit.Test)

Aggregations

SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)12 Test (org.junit.jupiter.api.Test)8 BaseSqmUnitTest (org.hibernate.orm.test.query.sqm.BaseSqmUnitTest)7 SqmLiteral (org.hibernate.query.sqm.tree.expression.SqmLiteral)5 SqmPath (org.hibernate.query.sqm.tree.domain.SqmPath)3 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)3 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)2 ComparisonOperator (org.hibernate.query.sqm.ComparisonOperator)2 SqmPredicate (org.hibernate.query.sqm.tree.predicate.SqmPredicate)2 SqmSelection (org.hibernate.query.sqm.tree.select.SqmSelection)2 EntityManager (jakarta.persistence.EntityManager)1 Expression (jakarta.persistence.criteria.Expression)1 Metamodel (jakarta.persistence.metamodel.Metamodel)1 BigInteger (java.math.BigInteger)1 Map (java.util.Map)1 HqlParser (org.hibernate.grammars.hql.HqlParser)1 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)1 JpaMetamodel (org.hibernate.metamodel.model.domain.JpaMetamodel)1 Product (org.hibernate.orm.test.jpa.metamodel.Product)1 AbstractSqmFunctionDescriptor (org.hibernate.query.sqm.function.AbstractSqmFunctionDescriptor)1