Skip to content

Add support for ignoreCase in queries derived from method names. #1481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;

import org.springframework.data.couchbase.core.query.QueryCriteria;
Expand Down Expand Up @@ -48,8 +47,6 @@
*/
public abstract class CouchbaseDocumentSerializer implements Visitor<Object, Void> {

boolean workInProgress = true;

public Object handle(Expression<?> expression) {
return expression.accept(this, null);
}
Expand Down Expand Up @@ -167,25 +164,22 @@ public Object visit(Operation<?> expr, Void context) {
return QueryCriteria.where(asDBKey(expr, 0)).startingWith(asDBValue(expr, 1));
} else if (op == Ops.STARTS_WITH_IC) {
// return asDocument(asDBKey(expr, 0), new CBRegularExpression("^" + regexValue(expr, 1), "i"));
return QueryCriteria.where(asDBKey(expr, 0)).upper()
.startingWith(asDBValue(expr, 1).toString().toUpperCase(Locale.ROOT));
return QueryCriteria.where(asDBKey(expr, 0)).startingWith(true, asDBValue(expr, 1).toString());
} else if (op == Ops.ENDS_WITH) {
// return asDocument(asDBKey(expr, 0), new CBRegularExpression(regexValue(expr, 1) + "$"));
return QueryCriteria.where(asDBKey(expr, 0)).endingWith(asDBValue(expr, 1));
} else if (op == Ops.ENDS_WITH_IC) {
// return asDocument(asDBKey(expr, 0), new CBRegularExpression(regexValue(expr, 1) + "$", "i"));
return QueryCriteria.where(asDBKey(expr, 0)).upper()
.endingWith(asDBValue(expr, 1).toString().toUpperCase(Locale.ROOT));
return QueryCriteria.where(asDBKey(expr, 0)).endingWith(true, asDBValue(expr, 1).toString());
} else if (op == Ops.EQ_IGNORE_CASE) {
// return asDocument(asDBKey(expr, 0), new CBRegularExpression("^" + regexValue(expr, 1) + "$", "i"));
return QueryCriteria.where(asDBKey(expr, 0)).upper().eq(asDBValue(expr, 1).toString().toUpperCase(Locale.ROOT));
return QueryCriteria.where(asDBKey(expr, 0)).eq(true, asDBValue(expr, 1).toString());
} else if (op == Ops.STRING_CONTAINS) {
// return asDocument(asDBKey(expr, 0), new CBRegularExpression(".*" + regexValue(expr, 1) + ".*"));
return QueryCriteria.where(asDBKey(expr, 0)).containing(asDBValue(expr, 1));
} else if (op == Ops.STRING_CONTAINS_IC) {
// return asDocument(asDBKey(expr, 0), new CBRegularExpression(".*" + regexValue(expr, 1) + ".*", "i"));
return QueryCriteria.where(asDBKey(expr, 0)).upper()
.containing(asDBValue(expr, 1).toString().toUpperCase(Locale.ROOT));
return QueryCriteria.where(asDBKey(expr, 0)).containing(true, asDBValue(expr, 1).toString());
/*
} else if (op == Ops.MATCHES) {
//return asDocument(asDBKey(expr, 0), new CBRegularExpression(asDBValue(expr, 1).toString()));
Expand All @@ -201,7 +195,7 @@ public Object visit(Operation<?> expr, Void context) {
} else if (op == Ops.LIKE_IC) {
// String regex = ExpressionUtils.likeToRegex((Expression) expr.getArg(1)).toString();
// return asDocument(asDBKey(expr, 0), new CBRegularExpression(regex, "i"));
return QueryCriteria.where(asDBKey(expr, 0)).upper().like(asDBValue(expr, 1).toString().toUpperCase(Locale.ROOT));
return QueryCriteria.where(asDBKey(expr, 0)).like(true, asDBValue(expr, 1).toString());
} else if (op == Ops.BETWEEN) {
// Document value = new Document("$gte", this.asDBValue(expr, 1));
// value.append("$lte", this.asDBValue(expr, 2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,39 @@ protected void writeInternal(final Object source, final CouchbaseDocument target

target.setExpiration((int) (entity.getExpiryDuration().getSeconds()));

writeToTargetDocument(target, entity, accessor, idProperty, versionProperty, prefixes, suffixes, idAttributes);

if (idProperty != null && target.getId() == null) {
String id = accessor.getProperty(idProperty, String.class);
if (idProperty.isAnnotationPresent(GeneratedValue.class) && (id == null || id.equals(""))) {
generatedValueInfo = idProperty.findAnnotation(GeneratedValue.class);
String generatedId = generateId(generatedValueInfo, prefixes, suffixes, idAttributes);
target.setId(generatedId);
// this is not effective if id is Immutable, and accessor.setProperty() returns a new object in getBean()
accessor.setProperty(idProperty, generatedId);
} else {
target.setId(id);
}
}

entity.doWithAssociations(new AssociationHandler<CouchbasePersistentProperty>() {
@Override
public void doWithAssociation(final Association<CouchbasePersistentProperty> association) {
CouchbasePersistentProperty inverseProp = association.getInverse();
Class<?> type = inverseProp.getType();
Object propertyObj = accessor.getProperty(inverseProp, type);
if (null != propertyObj) {
writePropertyInternal(propertyObj, target, inverseProp, false);
}
}
});

}

private void writeToTargetDocument(final CouchbaseDocument target, final CouchbasePersistentEntity<?> entity,
final ConvertingPropertyAccessor<Object> accessor, final CouchbasePersistentProperty idProperty,
final CouchbasePersistentProperty versionProperty, final TreeMap<Integer, String> prefixes,
final TreeMap<Integer, String> suffixes, final TreeMap<Integer, String> idAttributes) {
entity.doWithProperties(new PropertyHandler<CouchbasePersistentProperty>() {
@Override
public void doWithPersistentProperty(final CouchbasePersistentProperty prop) {
Expand Down Expand Up @@ -550,32 +583,6 @@ public void doWithPersistentProperty(final CouchbasePersistentProperty prop) {
}
}
});

if (idProperty != null && target.getId() == null) {
String id = accessor.getProperty(idProperty, String.class);
if (idProperty.isAnnotationPresent(GeneratedValue.class) && (id == null || id.equals(""))) {
generatedValueInfo = idProperty.findAnnotation(GeneratedValue.class);
String generatedId = generateId(generatedValueInfo, prefixes, suffixes, idAttributes);
target.setId(generatedId);
// this is not effective if id is Immutable, and accessor.setProperty() returns a new object in getBean()
accessor.setProperty(idProperty, generatedId);
} else {
target.setId(id);
}
}

entity.doWithAssociations(new AssociationHandler<CouchbasePersistentProperty>() {
@Override
public void doWithAssociation(final Association<CouchbasePersistentProperty> association) {
CouchbasePersistentProperty inverseProp = association.getInverse();
Class<?> type = inverseProp.getType();
Object propertyObj = accessor.getProperty(inverseProp, type);
if (null != propertyObj) {
writePropertyInternal(propertyObj, target, inverseProp, false);
}
}
});

}

/**
Expand Down
Loading