use of jakarta.persistence.criteria.Expression 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 jakarta.persistence.criteria.Expression in project eclipselink by eclipse-ee4j.
the class AdvancedCriteriaQueryTestSuite method testGetRestrictionReturningCorrectPredicate.
/**
* Bug 464833 - Criteria API: calling getRestriction() on a query returns Predicate with incorrect expression
* An incorrect expression is observed when calling getRestriction() on an existing query to obtain an existing Predicate.
* Tests: Validate that an existing Predicate (obtained with criteriaQuery.getRestriction()) has correct child expressions.
*/
public void testGetRestrictionReturningCorrectPredicate() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Employee> criteriaQuery = builder.createQuery(Employee.class);
Root<Employee> root = criteriaQuery.from(Employee.class);
// simple case - construct a predicate
criteriaQuery.where(builder.equal(root.get("firstName"), "Bob"));
TypedQuery<Employee> query1 = em.createQuery(criteriaQuery);
List<Employee> results1 = query1.getResultList();
long count1 = (Long) em.createQuery("select count(e) from Employee e where e.firstName = 'Bob'").getSingleResult();
// validate the expressions on the Predicate returned from CriteriaQuery getRestriction()
Predicate predicate = criteriaQuery.getRestriction();
// for the current example, the Predicate returned is expected to be a CompoundExpressionImpl
assertNotNull("Predicate should be non-null", predicate);
assertTrue("Invalid predicate type returned: " + predicate.getClass().getName(), predicate instanceof CompoundExpressionImpl);
CompoundExpressionImpl compoundExpression = (CompoundExpressionImpl) predicate;
// The where has two child expressions representing:
// 1) a path (query key) for "firstName" and 2) an expression (constant) for "Bob".
List<Expression<?>> expressions = compoundExpression.getChildExpressions();
assertSame("Predicate should have two child expressions", 2, expressions.size());
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of jakarta.persistence.criteria.Expression in project eclipselink by eclipse-ee4j.
the class CriteriaQueryTestSuite method testEqualsClauseSingleExpressionEmptyExpressionsList.
public void testEqualsClauseSingleExpressionEmptyExpressionsList() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Employee> query = criteriaBuilder.createQuery(Employee.class);
Root<Employee> employee = query.from(Employee.class);
EntityType<Employee> employeeModel = employee.getModel();
Predicate predicate = criteriaBuilder.equal(employee.get(employeeModel.getSingularAttribute("firstName", String.class)), "Bob");
List<Expression<Boolean>> expressions = predicate.getExpressions();
assertTrue("An empty list should be returned", expressions.isEmpty());
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of jakarta.persistence.criteria.Expression in project eclipselink by eclipse-ee4j.
the class CriteriaQueryTestSuite method testInClauseSingleExpressionEmptyExpressionsList.
public void testInClauseSingleExpressionEmptyExpressionsList() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaDelete<Employee> criteriaDelete = criteriaBuilder.createCriteriaDelete(Employee.class);
Root<Employee> employee = criteriaDelete.from(Employee.class);
CriteriaDelete<Employee> where = criteriaDelete.where(employee.get("firstName").in(Arrays.asList("Bob", "Bobby")));
Predicate restriction = where.getRestriction();
List<Expression<Boolean>> expressions = restriction.getExpressions();
assertTrue("An empty list should be returned", expressions.isEmpty());
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of jakarta.persistence.criteria.Expression in project eclipselink by eclipse-ee4j.
the class QueryTestSuite method testCriteriaGetGroupList.
public void testCriteriaGetGroupList() {
EntityManager em = createEntityManager();
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Employee> query = qb.createQuery(Employee.class);
List<Expression<?>> groupList = query.getGroupList();
assertNotNull("getGroupList returned null.", groupList);
}
Aggregations