-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from xenit-eu/starts-with-search
Add Text QuerydslPredicateFactories for starts with search [ACC-1519]
- Loading branch information
Showing
10 changed files
with
306 additions
and
86 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
2 changes: 1 addition & 1 deletion
2
.../testFixtures/java/com/contentgrid/spring/test/fixture/invoicing/model/ShippingLabel.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
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
13 changes: 13 additions & 0 deletions
13
.../java/com/contentgrid/spring/querydsl/hibernate/PostgresNormalizeFunctionContributor.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,13 @@ | ||
package com.contentgrid.spring.querydsl.hibernate; | ||
|
||
import org.hibernate.boot.model.FunctionContributions; | ||
import org.hibernate.boot.model.FunctionContributor; | ||
|
||
public class PostgresNormalizeFunctionContributor implements FunctionContributor { | ||
|
||
@Override | ||
public void contributeFunctions(FunctionContributions functionContributions) { | ||
var returnType = functionContributions.getTypeConfiguration().getBasicTypeForJavaType(String.class); | ||
functionContributions.getFunctionRegistry().registerPattern("normalize", "normalize(?1, NFKC)", returnType); | ||
} | ||
} |
31 changes: 3 additions & 28 deletions
31
...ng-querydsl/src/main/java/com/contentgrid/spring/querydsl/predicate/EqualsIgnoreCase.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 |
---|---|---|
@@ -1,36 +1,11 @@ | ||
package com.contentgrid.spring.querydsl.predicate; | ||
|
||
import com.contentgrid.spring.querydsl.mapping.UnsupportedCollectionFilterPredicatePathTypeException; | ||
import com.querydsl.core.types.Path; | ||
import com.querydsl.core.types.Predicate; | ||
import com.querydsl.core.types.dsl.StringPath; | ||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Compares strings in a case-insensitive way. | ||
* | ||
* This predicate only supports strings and can not be used with other types. | ||
* @deprecated Use {@link Text.EqualsIgnoreCase} instead. | ||
*/ | ||
public class EqualsIgnoreCase extends AbstractSimpleQuerydslPredicateFactory<StringPath, String> { | ||
|
||
@Override | ||
public StringPath coercePath(Path<?> path) { | ||
if(path instanceof StringPath stringPath) { | ||
return stringPath; | ||
} | ||
throw new UnsupportedCollectionFilterPredicatePathTypeException(this, path, StringPath.class); | ||
} | ||
|
||
@Override | ||
protected Optional<Predicate> bindCoerced(StringPath path, Collection<? extends String> values) { | ||
if(values.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
if(values.size() == 1) { | ||
var value = values.iterator().next(); | ||
return Optional.of(path.equalsIgnoreCase(value)); | ||
} | ||
return Optional.of(path.lower().in(values.stream().map(String::toLowerCase).toList())); | ||
} | ||
@Deprecated(since = "v0.15.2", forRemoval = true) | ||
public class EqualsIgnoreCase extends Text.EqualsIgnoreCase { | ||
} |
171 changes: 171 additions & 0 deletions
171
...entgrid-spring-querydsl/src/main/java/com/contentgrid/spring/querydsl/predicate/Text.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,171 @@ | ||
package com.contentgrid.spring.querydsl.predicate; | ||
|
||
import com.contentgrid.spring.querydsl.mapping.UnsupportedCollectionFilterPredicatePathTypeException; | ||
import com.querydsl.core.BooleanBuilder; | ||
import com.querydsl.core.types.Path; | ||
import com.querydsl.core.types.Predicate; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.core.types.dsl.Expressions; | ||
import com.querydsl.core.types.dsl.StringExpression; | ||
import com.querydsl.core.types.dsl.StringPath; | ||
import java.text.Normalizer; | ||
import java.text.Normalizer.Form; | ||
import java.util.Collection; | ||
import java.util.Optional; | ||
import java.util.function.BiFunction; | ||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.experimental.UtilityClass; | ||
|
||
@UtilityClass | ||
public class Text { | ||
|
||
@RequiredArgsConstructor(access = AccessLevel.PROTECTED) | ||
private abstract static class AbstractStringPredicateFactory extends AbstractSimpleQuerydslPredicateFactory<StringPath, String> { | ||
private final BiFunction<StringPath, String, BooleanExpression> stringExpressionMapper; | ||
|
||
@Override | ||
protected StringPath coercePath(Path<?> path) { | ||
if(path instanceof StringPath stringPath) { | ||
return stringPath; | ||
} | ||
throw new UnsupportedCollectionFilterPredicatePathTypeException(this, path, StringPath.class); | ||
} | ||
|
||
@Override | ||
protected Optional<Predicate> bindCoerced(StringPath path, Collection<? extends String> values) { | ||
if(values.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
if(values.size() == 1) { | ||
var value = values.iterator().next(); | ||
return Optional.of(stringExpressionMapper.apply(path, value)); | ||
} | ||
|
||
// If there are multiple values, return whether any of the provided values matches | ||
BooleanBuilder builder = new BooleanBuilder(); | ||
values.forEach(value -> builder.or(stringExpressionMapper.apply(path, value))); | ||
|
||
return Optional.ofNullable(builder.getValue()); | ||
} | ||
} | ||
|
||
/** | ||
* Filters items down to only items matching the supplied value in a case-insensitive way. | ||
* <p> | ||
* This predicate only supports {@link String}s, and can not be used with other types. | ||
*/ | ||
public static class EqualsIgnoreCase extends AbstractStringPredicateFactory { | ||
|
||
public EqualsIgnoreCase() { | ||
super(StringExpression::equalsIgnoreCase); | ||
} | ||
|
||
@Override | ||
protected Optional<Predicate> bindCoerced(StringPath path, Collection<? extends String> values) { | ||
if (values.size() <= 1) { | ||
return super.bindCoerced(path, values); | ||
} | ||
return Optional.of(path.lower().in(values.stream().map(String::toLowerCase).toList())); | ||
} | ||
} | ||
|
||
/** | ||
* Filters items down to only items starting with the supplied value. | ||
* <p> | ||
* This predicate only supports {@link String}s, and can not be used with other types. | ||
*/ | ||
public static class StartsWith extends AbstractStringPredicateFactory { | ||
|
||
public StartsWith() { | ||
super(StringExpression::startsWith); | ||
} | ||
} | ||
|
||
/** | ||
* Filters items down to only items starting with the supplied value in a case-insensitive way. | ||
* <p> | ||
* This predicate only supports {@link String}s, and can not be used with other types. | ||
*/ | ||
public static class StartsWithIgnoreCase extends AbstractStringPredicateFactory { | ||
|
||
public StartsWithIgnoreCase() { | ||
super(StringExpression::startsWithIgnoreCase); | ||
} | ||
} | ||
|
||
/** | ||
* Filters items down to only items matching the supplied value in a NFKC normalized way. | ||
* <p> | ||
* This predicate only supports {@link String}s, and can not be used with other types. | ||
*/ | ||
public static class EqualsNormalized extends AbstractStringPredicateFactory { | ||
|
||
public EqualsNormalized() { | ||
super((expr, value) -> normalize(expr).eq(Normalizer.normalize(value, Form.NFKC))); | ||
} | ||
|
||
@Override | ||
protected Optional<Predicate> bindCoerced(StringPath path, Collection<? extends String> values) { | ||
if (values.size() <= 1) { | ||
return super.bindCoerced(path, values); | ||
} | ||
|
||
return Optional.of(normalize(path).in(values.stream() | ||
.map(value -> Normalizer.normalize(value, Form.NFKC)) | ||
.toList())); | ||
} | ||
} | ||
|
||
/** | ||
* Filters items down to only items matching the supplied value in a case-insensitive, NFKC normalized way. | ||
* <p> | ||
* This predicate only supports {@link String}s, and can not be used with other types. | ||
*/ | ||
public static class EqualsIgnoreCaseNormalized extends AbstractStringPredicateFactory { | ||
|
||
public EqualsIgnoreCaseNormalized() { | ||
super((expr, value) -> normalize(expr).equalsIgnoreCase(Normalizer.normalize(value, Form.NFKC))); | ||
} | ||
|
||
@Override | ||
protected Optional<Predicate> bindCoerced(StringPath path, Collection<? extends String> values) { | ||
if (values.size() <= 1) { | ||
return super.bindCoerced(path, values); | ||
} | ||
|
||
return Optional.of(normalize(path).lower().in(values.stream() | ||
.map(value -> Normalizer.normalize(value, Form.NFKC).toLowerCase()) | ||
.toList())); | ||
} | ||
} | ||
|
||
/** | ||
* Filters items down to only items starting with the supplied value in a NFKC normalized way. | ||
* <p> | ||
* This predicate only supports {@link String}s, and can not be used with other types. | ||
*/ | ||
public static class StartsWithIgnoreCaseNormalized extends AbstractStringPredicateFactory { | ||
|
||
protected StartsWithIgnoreCaseNormalized() { | ||
super((expr, value) -> normalize(expr).startsWithIgnoreCase(Normalizer.normalize(value, Form.NFKC))); | ||
} | ||
} | ||
|
||
/** | ||
* Filters items down to only items starting with the supplied value in a case-insensitive, NFKC normalized way. | ||
* <p> | ||
* This predicate only supports {@link String}s, and can not be used with other types. | ||
*/ | ||
public static class StartsWithNormalized extends AbstractStringPredicateFactory { | ||
|
||
protected StartsWithNormalized() { | ||
super((expr, value) -> normalize(expr).startsWith(Normalizer.normalize(value, Form.NFKC))); | ||
} | ||
} | ||
|
||
static StringExpression normalize(StringExpression expr) { | ||
return Expressions.stringTemplate("normalize({0s})", expr); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...uerydsl/src/main/resources/META-INF/services/org.hibernate.boot.model.FunctionContributor
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 @@ | ||
com.contentgrid.spring.querydsl.hibernate.PostgresNormalizeFunctionContributor |
55 changes: 0 additions & 55 deletions
55
...uerydsl/src/test/java/com/contentgrid/spring/querydsl/predicate/EqualsIgnoreCaseTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.