This is an automated email from the git hooks/post-receive script. New commit to branch feature/add_in_ranges_to_dao_query_builder in repository topia. See https://gitlab.nuiton.org/nuiton/topia.git commit f7b853ea4c96c45dd9d024e3330943b8438e68a6 Author: Brendan Le Ny <bleny@codelutin.com> Date: Tue Apr 10 17:41:17 2018 +0200 Add HqlAndParametersBuilder API to check a property is in Range (both commons and guava supported) --- .../topia/persistence/HqlAndParametersBuilder.java | 62 ++++++++++++++++++++++ .../persistence/HqlAndParametersBuilderTest.java | 57 ++++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/topia-persistence/src/main/java/org/nuiton/topia/persistence/HqlAndParametersBuilder.java b/topia-persistence/src/main/java/org/nuiton/topia/persistence/HqlAndParametersBuilder.java index 2edf1a78..9f51c3f1 100644 --- a/topia-persistence/src/main/java/org/nuiton/topia/persistence/HqlAndParametersBuilder.java +++ b/topia-persistence/src/main/java/org/nuiton/topia/persistence/HqlAndParametersBuilder.java @@ -29,6 +29,7 @@ import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.base.Strings; +import com.google.common.collect.BoundType; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @@ -149,6 +150,67 @@ public class HqlAndParametersBuilder<E extends TopiaEntity> { addInOrNotIn(property, values, false); } + public <T extends Comparable<T>> void addIn(String property, org.apache.commons.lang3.Range<T> range) { + addIn(property, range, true); + } + + public <T extends Comparable<T>> void addNotIn(String property, org.apache.commons.lang3.Range<T> range) { + addIn(property, range, false); + } + + public <T extends Comparable<T>> void addIn(String property, org.apache.commons.lang3.Range<T> range, boolean in) { + T minimum = range.getMinimum(); + T maximum = range.getMaximum(); + if (in) { + doAddGreaterOrEquals(property, minimum); + doAddLowerOrEquals(property, maximum); + } else { + doAddLowerThan(property, minimum); + doAddGreaterThan(property, maximum); + } + } + + public <T extends Comparable<T>> void addIn(String property, com.google.common.collect.Range<T> range) { + addIn(property, range, true); + } + + public <T extends Comparable<T>> void addNotIn(String property, com.google.common.collect.Range<T> range) { + addIn(property, range, false); + } + + public <T extends Comparable<T>> void addIn(String property, com.google.common.collect.Range<T> range, boolean in) { + if (range.hasLowerBound()) { + if (range.lowerBoundType() == BoundType.CLOSED) { + if (in) { + doAddGreaterOrEquals(property, range.lowerEndpoint()); + } else { + doAddLowerThan(property, range.lowerEndpoint()); + } + } else { + if (in) { + doAddGreaterThan(property, range.lowerEndpoint()); + } else { + doAddLowerOrEquals(property, range.lowerEndpoint()); + } + } + } + if (range.hasUpperBound()) { + if (range.upperBoundType() == BoundType.CLOSED) { + if (in) { + doAddLowerOrEquals(property, range.upperEndpoint()); + } else { + doAddGreaterThan(property, range.upperEndpoint()); + } + } else { + if (in) { + doAddLowerThan(property, range.upperEndpoint()); + } else { + doAddGreaterOrEquals(property, range.upperEndpoint()); + } + } + } + } + /** * @param property FIXME * @param values FIXME diff --git a/topia-persistence/src/test/java/org/nuiton/topia/persistence/HqlAndParametersBuilderTest.java b/topia-persistence/src/test/java/org/nuiton/topia/persistence/HqlAndParametersBuilderTest.java index 2ee81092..eabcff99 100644 --- a/topia-persistence/src/test/java/org/nuiton/topia/persistence/HqlAndParametersBuilderTest.java +++ b/topia-persistence/src/test/java/org/nuiton/topia/persistence/HqlAndParametersBuilderTest.java @@ -26,6 +26,7 @@ package org.nuiton.topia.persistence; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; +import org.apache.commons.lang3.StringUtils; import org.junit.Assert; import org.junit.Test; @@ -162,4 +163,60 @@ public class HqlAndParametersBuilderTest { // second sql (with resolved properties) Assert.assertTrue(hql.contains("myOtherProperty = :myParameter0, myOtherProperty1 = :myParameter10, myOtherProperty2 = :myParameter21, myOtherProperty20 = :myParameter20")); } + + @Test + public void testRangesAll() { + hqlAndParametersBuilder.addIn("myProperty", com.google.common.collect.Range.all()); + Assert.assertFalse(StringUtils.containsIgnoreCase(hqlAndParametersBuilder.getHql(), "where")); + } + + @Test + public void testRangesSingleton() { + hqlAndParametersBuilder.addIn("myProperty", com.google.common.collect.Range.singleton(24)); + Assert.assertTrue(StringUtils.containsIgnoreCase(hqlAndParametersBuilder.getHql(), "where (topiaEntity_.myProperty >= :myProperty0) and (topiaEntity_.myProperty <= :myProperty1)")); + } + + @Test + public void testRangesSingleBound() { + hqlAndParametersBuilder.addIn("myProperty", com.google.common.collect.Range.atLeast(18)); + Assert.assertTrue(StringUtils.containsIgnoreCase(hqlAndParametersBuilder.getHql(), "where topiaEntity_.myProperty >= :myProperty0")); + } + + @Test + public void testRanges() { + hqlAndParametersBuilder.addIn("myProperty", com.google.common.collect.Range.openClosed(18, 25)); + Assert.assertTrue(StringUtils.containsIgnoreCase(hqlAndParametersBuilder.getHql(), "where (topiaEntity_.myProperty > :myProperty0) and (topiaEntity_.myProperty <= :myProperty1)")); + Assert.assertEquals(18, hqlAndParametersBuilder.getHqlParameters().get("myProperty0")); + Assert.assertEquals(25, hqlAndParametersBuilder.getHqlParameters().get("myProperty1")); + } + + @Test + public void testRangesNotIn() { + hqlAndParametersBuilder.addNotIn("myProperty", com.google.common.collect.Range.openClosed(18, 25)); + Assert.assertTrue(StringUtils.containsIgnoreCase(hqlAndParametersBuilder.getHql(), "where (topiaEntity_.myProperty <= :myProperty0) and (topiaEntity_.myProperty > :myProperty1)")); + Assert.assertEquals(18, hqlAndParametersBuilder.getHqlParameters().get("myProperty0")); + Assert.assertEquals(25, hqlAndParametersBuilder.getHqlParameters().get("myProperty1")); + } + + @Test + public void testCommonRangesSingleton() { + hqlAndParametersBuilder.addIn("myProperty", org.apache.commons.lang3.Range.is(24)); + Assert.assertTrue(StringUtils.containsIgnoreCase(hqlAndParametersBuilder.getHql(), "where (topiaEntity_.myProperty >= :myProperty0) and (topiaEntity_.myProperty <= :myProperty1)")); + } + + @Test + public void testCommonRanges() { + hqlAndParametersBuilder.addIn("myProperty", org.apache.commons.lang3.Range.between(18, 25)); + Assert.assertTrue(StringUtils.containsIgnoreCase(hqlAndParametersBuilder.getHql(), "where (topiaEntity_.myProperty >= :myProperty0) and (topiaEntity_.myProperty <= :myProperty1)")); + Assert.assertEquals(18, hqlAndParametersBuilder.getHqlParameters().get("myProperty0")); + Assert.assertEquals(25, hqlAndParametersBuilder.getHqlParameters().get("myProperty1")); + } + + @Test + public void testCommonRangesNotIn() { + hqlAndParametersBuilder.addNotIn("myProperty", org.apache.commons.lang3.Range.between(18, 25)); + Assert.assertTrue(StringUtils.containsIgnoreCase(hqlAndParametersBuilder.getHql(), "where (topiaEntity_.myProperty < :myProperty0) and (topiaEntity_.myProperty > :myProperty1)")); + Assert.assertEquals(18, hqlAndParametersBuilder.getHqlParameters().get("myProperty0")); + Assert.assertEquals(25, hqlAndParametersBuilder.getHqlParameters().get("myProperty1")); + } } -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.