Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New hstore function: PgHstoreILikeValue
- Loading branch information
Showing
5 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/java/net/kaleidos/hibernate/criterion/hstore/PgHstoreILikeValueFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<columns.length; i++) { | ||
columns[i] = "text(avals(" + columns[i]+ ")) ilike ?"; | ||
} | ||
return StringHelper.join( " and ", columns); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...ntegration/net/kaleidos/hibernate/hstore/PgHstoreILikeValueFunctionIntegrationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package net.kaleidos.hibernate.hstore | ||
|
||
import grails.plugin.spock.* | ||
import spock.lang.* | ||
|
||
import spock.lang.Specification | ||
import test.hstore.TestHstore | ||
|
||
class PgHstoreILikeValueFunctionIntegrationSpec extends Specification { | ||
def pgHstoreTestSearchService | ||
|
||
void 'Test find hstore that ilikes value'() { | ||
setup: | ||
new TestHstore(name: "test1", testAttributes: ["a": "test", "b": "1"]).save(flush: true) | ||
new TestHstore(name: "test2", testAttributes: ["b": "2"]).save(flush: true) | ||
new TestHstore(name: "test3", testAttributes: ["a": "test"]).save(flush: true) | ||
new TestHstore(name: "test4", testAttributes: ["c": "test", "b": "3"]).save(flush: true) | ||
|
||
when: | ||
def result = pgHstoreTestSearchService.search('testAttributes', 'pgHstoreILikeValue', '%test%') | ||
|
||
then: | ||
result.size() == 3 | ||
result.find { it.name == "test1" } != null | ||
result.find { it.name == "test2" } == null | ||
result.find { it.name == "test3" } != null | ||
result.find { it.name == "test4" } != null | ||
} | ||
|
||
void 'Test find hstore that no ilikes value'() { | ||
setup: | ||
new TestHstore(name: "test1", testAttributes: ["a": "test", "b": "1"]).save(flush: true) | ||
new TestHstore(name: "test2", testAttributes: ["b": "2"]).save(flush: true) | ||
new TestHstore(name: "test3", testAttributes: ["a": "test"]).save(flush: true) | ||
new TestHstore(name: "test4", testAttributes: ["c": "test", "b": "3"]).save(flush: true) | ||
|
||
when: | ||
def result = pgHstoreTestSearchService.search('testAttributes', 'pgHstoreILikeValue', '%X%') | ||
|
||
then: | ||
result.size() == 0 | ||
} | ||
} |