-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 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
45 changes: 45 additions & 0 deletions
45
src/java/net/kaleidos/hibernate/criterion/array/PgArrayILikeFunction.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,45 @@ | ||
package net.kaleidos.hibernate.criterion.array; | ||
|
||
import net.kaleidos.hibernate.usertype.ArrayType; | ||
import net.kaleidos.hibernate.utils.PgArrayUtils; | ||
import org.hibernate.Criteria; | ||
import org.hibernate.HibernateException; | ||
import org.hibernate.annotations.common.util.StringHelper; | ||
import org.hibernate.criterion.CriteriaQuery; | ||
import org.hibernate.criterion.Criterion; | ||
import org.hibernate.engine.spi.TypedValue; | ||
import org.hibernate.type.CustomType; | ||
import org.hibernate.type.Type; | ||
import org.hibernate.type.StringType; | ||
|
||
/** | ||
* Constrains a property in an array | ||
*/ | ||
public class PgArrayILikeFunction implements Criterion { | ||
|
||
private static final long serialVersionUID = 7475136611436979257L; | ||
|
||
private final String propertyName; | ||
private final String value; | ||
|
||
protected PgArrayILikeFunction(String propertyName, String value) { | ||
this.propertyName = propertyName; | ||
this.value = 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(" + columns[i]+ ") ilike ?"; | ||
} | ||
return StringHelper.join( " and ", columns); | ||
} | ||
|
||
@Override | ||
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException { | ||
return new TypedValue[]{ | ||
new TypedValue(new StringType(), value) | ||
}; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...integration/net/kaleidos/hibernate/array/PgILikeCriteriaTestServiceIntegrationSpec.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,35 @@ | ||
package net.kaleidos.hibernate.array | ||
|
||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
import test.criteria.array.Like | ||
|
||
class PgILikeCriteriaTestServiceIntegrationSpec extends Specification { | ||
|
||
def pgArrayTestSearchService | ||
|
||
@Unroll | ||
void "check ilike for #movie in an array of strings"() { | ||
setup: | ||
new Like(favoriteMovies: ["The Matrix", "The Lord of the Rings"]).save() | ||
new Like(favoriteMovies: ["Spiderman", "Blade Runner", "Starwars"]).save() | ||
new Like(favoriteMovies: ["Starwars"]).save() | ||
new Like(favoriteMovies: ["Romeo & Juliet", "Blade Runner", "The Lord of the Rings"]).save() | ||
new Like(favoriteMovies: []).save() | ||
|
||
when: | ||
def result = pgArrayTestSearchService.search('favoriteMovies', 'pgArrayILike', movie) | ||
|
||
then: | ||
result.size() == resultSize | ||
|
||
where: | ||
movie | resultSize | ||
"%tarwar%" | 2 | ||
"%ider%" | 1 | ||
"%Suspects%" | 0 | ||
"" | 0 | ||
"%" | 5 | ||
} | ||
|
||
} |