[Lada-commits] [PATCH 1 of 5] Added methods for sql 'IN' queries

Wald Commits scm-commit at wald.intevation.org
Wed Apr 22 09:17:34 CEST 2015


# HG changeset patch
# User Raimund Renkert <raimund.renkert at intevation.de>
# Date 1429542290 -7200
# Node ID da56b05604aec3bc85d3f08c3d072bc9e964194b
# Parent  6973466d97ac6d2f433c2404fa0a033c30a1df98
Added methods for sql 'IN' queries.

diff -r 6973466d97ac -r da56b05604ae src/main/java/de/intevation/lada/util/data/QueryBuilder.java
--- a/src/main/java/de/intevation/lada/util/data/QueryBuilder.java	Fri Apr 17 15:19:39 2015 +0200
+++ b/src/main/java/de/intevation/lada/util/data/QueryBuilder.java	Mon Apr 20 17:04:50 2015 +0200
@@ -12,6 +12,7 @@
 import javax.persistence.EntityManager;
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Expression;
 import javax.persistence.criteria.Path;
 import javax.persistence.criteria.Predicate;
 import javax.persistence.criteria.Root;
@@ -203,6 +204,48 @@
     }
 
     /**
+     * IN operation combined as logical OR.
+     * Test whether result of 'key' is in a list of values.
+     *
+     * @param key   The database column.
+     * @param values    The list of values.
+     *
+     * @return The current Querybuilder.
+     */
+    public QueryBuilder<T> orIn(String key, List<String> values) {
+        Expression<String> exp = this.root.get(key);
+        Predicate p = exp.in(values);
+        if (this.filter == null) {
+            this.filter = this.builder.or(p);
+        }
+        else {
+            this.filter = this.builder.or(this.filter, p);
+        }
+        return this;
+    }
+
+    /**
+     * IN operation combined as logical AND.
+     * Test whether result of 'key' is in a list of values.
+     *
+     * @param key   The database column.
+     * @param values    The list of values.
+     *
+     * @return The current Querybuilder.
+     */
+    public QueryBuilder<T> andIn(String key, List<String> values) {
+        Expression<String> exp = this.root.get(key);
+        Predicate p = exp.in(values);
+        if (this.filter == null) {
+            this.filter = this.builder.and(p);
+        }
+        else {
+            this.filter = this.builder.and(this.filter, p);
+        }
+        return this;
+    }
+
+    /**
      * Use 'distinct' in the query.
      */
     public void distinct() {


More information about the Lada-commits mailing list