filter query
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -35,7 +35,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>de.infinimotion</groupId>
|
<groupId>de.infinimotion</groupId>
|
||||||
<artifactId>model-persistence</artifactId>
|
<artifactId>model-persistence</artifactId>
|
||||||
<version>0.0.76</version>
|
<version>0.0.83</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Quarkus -->
|
<!-- Quarkus -->
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package de.infinimotion.persistence.processor;
|
||||||
|
|
||||||
|
import de.infinimotion.model.persistence.GenericFilterQuery;
|
||||||
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.criteria.*;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalTime;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
public class FilterQuery implements GenericFilterQuery {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
EntityManager em;
|
||||||
|
|
||||||
|
// eq;hall.id;int;1
|
||||||
|
@Override
|
||||||
|
public <E> List<E> query(Class<E> clazz, List<String> filters) {
|
||||||
|
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<E> cr = cb.createQuery(clazz);
|
||||||
|
Root<E> root = cr.from(clazz);
|
||||||
|
|
||||||
|
List<Predicate> predicates = new LinkedList<>();
|
||||||
|
if (filters != null) {
|
||||||
|
for (String filter : filters) {
|
||||||
|
String[] split = filter.split(";", 4);
|
||||||
|
String[] pathSplit = split[1].split("\\.");
|
||||||
|
Predicate predicate = switch (split[2]) {
|
||||||
|
case "int" -> filterGeneric(cb, root, pathSplit, split[0], Integer.parseInt(split[3]));
|
||||||
|
case "date" -> filterGeneric(cb, root, pathSplit, split[0], Date.from(Instant.parse(split[3])));
|
||||||
|
case "localdate" -> filterGeneric(cb, root, pathSplit, split[0], LocalDate.parse(split[3]));
|
||||||
|
case "localtime" -> filterGeneric(cb, root, pathSplit, split[0], LocalTime.parse(split[3]));
|
||||||
|
default -> filterGeneric(cb, root, pathSplit, split[0], split[3]);
|
||||||
|
};
|
||||||
|
predicates.add(predicate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cr.select(root).where(predicates);
|
||||||
|
return em.createQuery(cr).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
<E, Y extends Comparable<? super Y>> Predicate filterGeneric(CriteriaBuilder cb, Root<E> root, String[] pathSplit, String operator, Y value) {
|
||||||
|
Path<Y> path = root.get(pathSplit[0]);
|
||||||
|
for (int i = 1; i < pathSplit.length; i++) {
|
||||||
|
path = path.get(pathSplit[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return switch (operator) {
|
||||||
|
case "eq" -> cb.equal(path, value);
|
||||||
|
case "ne" -> cb.notEqual(path, value);
|
||||||
|
case "gt" -> cb.greaterThan(path, value);
|
||||||
|
case "ge" -> cb.greaterThanOrEqualTo(path, value);
|
||||||
|
case "lt" -> cb.lessThan(path, value);
|
||||||
|
case "le" -> cb.lessThanOrEqualTo(path, value);
|
||||||
|
case "null" -> cb.isNull(path);
|
||||||
|
case "nn" -> cb.isNotNull(path);
|
||||||
|
default -> throw new IllegalArgumentException("unknown filter: " + operator);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user