From 3c052f2f5bd2aee688829a9ede0a1345f9452564 Mon Sep 17 00:00:00 2001 From: "pablo.alba" Date: Thu, 21 May 2015 17:57:48 +0200 Subject: [PATCH] New hstore function: PgHstoreILikeValue --- README.md | 11 +++++ .../criteria/HstoreCriterias.groovy | 16 +++++++ .../hstore/PgHstoreILikeValueFunction.java | 25 +++++++++++ .../hstore/PgHstoreValueFunction.java | 6 +-- ...reILikeValueFunctionIntegrationSpec.groovy | 43 +++++++++++++++++++ 5 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 src/java/net/kaleidos/hibernate/criterion/hstore/PgHstoreILikeValueFunction.java create mode 100644 test/integration/net/kaleidos/hibernate/hstore/PgHstoreILikeValueFunctionIntegrationSpec.groovy diff --git a/README.md b/README.md index 5d79b60..a32d9d0 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Currently the plugin supports array, hstore and json fields as well as some quer * [Contains Key](#contains-key) * [Contains](#contains) * [Is Contained](#is-contained) + * [ILike Value](#ilike-value) * [JSON](#json) * [Criterias](#criterias) * [Has field value](#has-field-value) @@ -446,6 +447,16 @@ testAttributes = ["1" : "a", "2" : "b"] ``` This criteria can also be used to look for exact matches +##### ILike Value + +With this operation you can search for rows that contains an Hstore in which any value matches (ilike) to the parameter. It uses the ilike syntaxis, so you can do for example: + +```groovy +def wantedValue = "%my-value%" +def result = MyDomain.withCriteria { + pgHstoreILikeValue "attributes", wantedKey +} +``` ### JSON diff --git a/src/groovy/net/kaleidos/hibernate/postgresql/criteria/HstoreCriterias.groovy b/src/groovy/net/kaleidos/hibernate/postgresql/criteria/HstoreCriterias.groovy index 6f42040..90c2d65 100644 --- a/src/groovy/net/kaleidos/hibernate/postgresql/criteria/HstoreCriterias.groovy +++ b/src/groovy/net/kaleidos/hibernate/postgresql/criteria/HstoreCriterias.groovy @@ -2,6 +2,7 @@ package net.kaleidos.hibernate.postgresql.criteria import grails.orm.HibernateCriteriaBuilder import net.kaleidos.hibernate.criterion.hstore.PgHstoreValueFunction +import net.kaleidos.hibernate.criterion.hstore.PgHstoreILikeValueFunction import net.kaleidos.hibernate.criterion.hstore.PgHstoreOperatorExpression class HstoreCriterias { @@ -9,6 +10,7 @@ class HstoreCriterias { addPgHstoreContainsKey() addPgHstoreContains() addPgHstoreIsContained() + addPgHstoreILikeValueFunction() } void addPgHstoreContainsKey() { @@ -23,6 +25,20 @@ class HstoreCriterias { return addToCriteria(new PgHstoreValueFunction(propertyName, propertyValue, "exist")) } } + + void addPgHstoreILikeValueFunction() { + HibernateCriteriaBuilder.metaClass.pgHstoreILikeValue = { String propertyName, propertyValue -> + if (!validateSimpleExpression()) { + throwRuntimeException(new IllegalArgumentException("Call to [pgHstoreILikeValue] with propertyName [" + + propertyName + "] and value [" + propertyValue + "] not allowed here.")) + } + propertyName = calculatePropertyName(propertyName) + propertyValue = calculatePropertyValue(propertyValue) + + return addToCriteria(new PgHstoreILikeValueFunction(propertyName, propertyValue)) + } + } + void addPgHstoreContains() { HibernateCriteriaBuilder.metaClass.pgHstoreContains = { String propertyName, Map values -> if (!validateSimpleExpression()) { diff --git a/src/java/net/kaleidos/hibernate/criterion/hstore/PgHstoreILikeValueFunction.java b/src/java/net/kaleidos/hibernate/criterion/hstore/PgHstoreILikeValueFunction.java new file mode 100644 index 0000000..b195349 --- /dev/null +++ b/src/java/net/kaleidos/hibernate/criterion/hstore/PgHstoreILikeValueFunction.java @@ -0,0 +1,25 @@ +package net.kaleidos.hibernate.criterion.hstore; + +import org.hibernate.Criteria; +import org.hibernate.HibernateException; +import org.hibernate.annotations.common.util.StringHelper; +import org.hibernate.criterion.CriteriaQuery; + +/** + * Do an hstore content any value that ilikes the parameter? + */ +public class PgHstoreILikeValueFunction extends PgHstoreValueFunction { + + protected PgHstoreILikeValueFunction(String propertyName, Object value) { + super(propertyName, value, ""); + } + + @Override + public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException { + String[] columns = StringHelper.suffix(criteriaQuery.findColumns(propertyName, criteria), ""); + for (int i=0; i