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