Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support aggregations by conversing expressions to specifications #32

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/com/github/mhewedy/expressions/Expressions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -122,6 +123,10 @@ public Expressions and(Expression expression) {
return this;
}

public <T> Specification<T> getSpecification() {
return new ExpressionsRepositoryImpl.ExpressionsSpecification<>(this);
}

static Expressions of(Expression expression) {
Expressions expressions = new Expressions();
expressions.and(expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public long count(Expressions expressions) {
}

@RequiredArgsConstructor
private static class ExpressionsSpecification<T> implements Specification<T> {
static class ExpressionsSpecification<T> implements Specification<T> {

private final Expressions expressions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.mhewedy.expressions.model.*;
import jakarta.persistence.EntityManager;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -17,6 +21,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.util.ResourceUtils;
Expand All @@ -43,6 +48,8 @@
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
public class ExpressionsRepositoryImplTest {

@Autowired
EntityManager entityManager;
@Autowired
private BookRepository bookRepository;
@Autowired
Expand Down Expand Up @@ -596,7 +603,7 @@ public void testHijrahDate_InJava() {
}

@Test
public void testCompositeIdUsingEmbeddable() throws Exception{
public void testCompositeIdUsingEmbeddable() throws Exception {
String json = loadResourceJsonFile("testCompositeIdUsingEmbeddable");

Expressions expressions = new ObjectMapper().readValue(json, Expressions.class);
Expand All @@ -608,7 +615,7 @@ public void testCompositeIdUsingEmbeddable() throws Exception{
}

@Test
public void testCompositeIdUsingEmbeddable_QueryByIdParts() throws Exception{
public void testCompositeIdUsingEmbeddable_QueryByIdParts() throws Exception {
String json = loadResourceJsonFile("testCompositeIdUsingEmbeddable_QueryByIdParts");

Expressions expressions = new ObjectMapper().readValue(json, Expressions.class);
Expand All @@ -620,7 +627,7 @@ public void testCompositeIdUsingEmbeddable_QueryByIdParts() throws Exception{
}

@Test
public void testCompositeIdUsingEmbeddable_QueryByIdParts2() throws Exception{
public void testCompositeIdUsingEmbeddable_QueryByIdParts2() throws Exception {
String json = loadResourceJsonFile("testCompositeIdUsingEmbeddable_QueryByIdParts2");

Expressions expressions = new ObjectMapper().readValue(json, Expressions.class);
Expand All @@ -631,6 +638,25 @@ public void testCompositeIdUsingEmbeddable_QueryByIdParts2() throws Exception{
// where b1_0.language=?
}

@Test
public void testAggregations_by_convert_to_specifications_InJava() {
Expressions expressions = Expression.of("hBirthDate", Operator.$gte, HijrahDate.of(1388, 9, 29)).build();

// get spring-data-jpa Specification from the expressions object
Specification<Employee> specification = expressions.getSpecification();

// then using old school jpa Criteria Query API
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Integer> query = cb.createQuery(Integer.class);
Root<Employee> root = query.from(Employee.class);
query.select(cb.min(root.get("age"))).where(specification.toPredicate(root, query, cb));
Integer minAge = entityManager.createQuery(query).getSingleResult();

assertThat(minAge).isEqualTo(30);

// select min(e1_0.age) from employee e1_0 where e1_0.h_birth_date>=?
}

@SneakyThrows
private String loadResourceJsonFile(String name) {
File file = ResourceUtils.getFile("classpath:" + name + ".json");
Expand Down
Loading