Skip to content

Commit 935dd1f

Browse files
committed
Add support for ignoreCase in queries derived from method names.
Closes #984, #1481.
1 parent b0e24a7 commit 935dd1f

File tree

8 files changed

+220
-103
lines changed

8 files changed

+220
-103
lines changed

src/main/java/com/querydsl/couchbase/document/CouchbaseDocumentSerializer.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.Collection;
1919
import java.util.List;
20-
import java.util.Locale;
2120
import java.util.regex.Pattern;
2221

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

51-
boolean workInProgress = true;
52-
5350
public Object handle(Expression<?> expression) {
5451
return expression.accept(this, null);
5552
}
@@ -167,25 +164,22 @@ public Object visit(Operation<?> expr, Void context) {
167164
return QueryCriteria.where(asDBKey(expr, 0)).startingWith(asDBValue(expr, 1));
168165
} else if (op == Ops.STARTS_WITH_IC) {
169166
// return asDocument(asDBKey(expr, 0), new CBRegularExpression("^" + regexValue(expr, 1), "i"));
170-
return QueryCriteria.where(asDBKey(expr, 0)).upper()
171-
.startingWith(asDBValue(expr, 1).toString().toUpperCase(Locale.ROOT));
167+
return QueryCriteria.where(asDBKey(expr, 0)).startingWith(true, asDBValue(expr, 1).toString());
172168
} else if (op == Ops.ENDS_WITH) {
173169
// return asDocument(asDBKey(expr, 0), new CBRegularExpression(regexValue(expr, 1) + "$"));
174170
return QueryCriteria.where(asDBKey(expr, 0)).endingWith(asDBValue(expr, 1));
175171
} else if (op == Ops.ENDS_WITH_IC) {
176172
// return asDocument(asDBKey(expr, 0), new CBRegularExpression(regexValue(expr, 1) + "$", "i"));
177-
return QueryCriteria.where(asDBKey(expr, 0)).upper()
178-
.endingWith(asDBValue(expr, 1).toString().toUpperCase(Locale.ROOT));
173+
return QueryCriteria.where(asDBKey(expr, 0)).endingWith(true, asDBValue(expr, 1).toString());
179174
} else if (op == Ops.EQ_IGNORE_CASE) {
180175
// return asDocument(asDBKey(expr, 0), new CBRegularExpression("^" + regexValue(expr, 1) + "$", "i"));
181-
return QueryCriteria.where(asDBKey(expr, 0)).upper().eq(asDBValue(expr, 1).toString().toUpperCase(Locale.ROOT));
176+
return QueryCriteria.where(asDBKey(expr, 0)).eq(true, asDBValue(expr, 1).toString());
182177
} else if (op == Ops.STRING_CONTAINS) {
183178
// return asDocument(asDBKey(expr, 0), new CBRegularExpression(".*" + regexValue(expr, 1) + ".*"));
184179
return QueryCriteria.where(asDBKey(expr, 0)).containing(asDBValue(expr, 1));
185180
} else if (op == Ops.STRING_CONTAINS_IC) {
186181
// return asDocument(asDBKey(expr, 0), new CBRegularExpression(".*" + regexValue(expr, 1) + ".*", "i"));
187-
return QueryCriteria.where(asDBKey(expr, 0)).upper()
188-
.containing(asDBValue(expr, 1).toString().toUpperCase(Locale.ROOT));
182+
return QueryCriteria.where(asDBKey(expr, 0)).containing(true, asDBValue(expr, 1).toString());
189183
/*
190184
} else if (op == Ops.MATCHES) {
191185
//return asDocument(asDBKey(expr, 0), new CBRegularExpression(asDBValue(expr, 1).toString()));
@@ -201,7 +195,7 @@ public Object visit(Operation<?> expr, Void context) {
201195
} else if (op == Ops.LIKE_IC) {
202196
// String regex = ExpressionUtils.likeToRegex((Expression) expr.getArg(1)).toString();
203197
// return asDocument(asDBKey(expr, 0), new CBRegularExpression(regex, "i"));
204-
return QueryCriteria.where(asDBKey(expr, 0)).upper().like(asDBValue(expr, 1).toString().toUpperCase(Locale.ROOT));
198+
return QueryCriteria.where(asDBKey(expr, 0)).like(true, asDBValue(expr, 1).toString());
205199
} else if (op == Ops.BETWEEN) {
206200
// Document value = new Document("$gte", this.asDBValue(expr, 1));
207201
// value.append("$lte", this.asDBValue(expr, 2));

src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java

+33-26
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,39 @@ protected void writeInternal(final Object source, final CouchbaseDocument target
508508

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

511+
writeToTargetDocument(target, entity, accessor, idProperty, versionProperty, prefixes, suffixes, idAttributes);
512+
513+
if (idProperty != null && target.getId() == null) {
514+
String id = accessor.getProperty(idProperty, String.class);
515+
if (idProperty.isAnnotationPresent(GeneratedValue.class) && (id == null || id.equals(""))) {
516+
generatedValueInfo = idProperty.findAnnotation(GeneratedValue.class);
517+
String generatedId = generateId(generatedValueInfo, prefixes, suffixes, idAttributes);
518+
target.setId(generatedId);
519+
// this is not effective if id is Immutable, and accessor.setProperty() returns a new object in getBean()
520+
accessor.setProperty(idProperty, generatedId);
521+
} else {
522+
target.setId(id);
523+
}
524+
}
525+
526+
entity.doWithAssociations(new AssociationHandler<CouchbasePersistentProperty>() {
527+
@Override
528+
public void doWithAssociation(final Association<CouchbasePersistentProperty> association) {
529+
CouchbasePersistentProperty inverseProp = association.getInverse();
530+
Class<?> type = inverseProp.getType();
531+
Object propertyObj = accessor.getProperty(inverseProp, type);
532+
if (null != propertyObj) {
533+
writePropertyInternal(propertyObj, target, inverseProp, false);
534+
}
535+
}
536+
});
537+
538+
}
539+
540+
private void writeToTargetDocument(final CouchbaseDocument target, final CouchbasePersistentEntity<?> entity,
541+
final ConvertingPropertyAccessor<Object> accessor, final CouchbasePersistentProperty idProperty,
542+
final CouchbasePersistentProperty versionProperty, final TreeMap<Integer, String> prefixes,
543+
final TreeMap<Integer, String> suffixes, final TreeMap<Integer, String> idAttributes) {
511544
entity.doWithProperties(new PropertyHandler<CouchbasePersistentProperty>() {
512545
@Override
513546
public void doWithPersistentProperty(final CouchbasePersistentProperty prop) {
@@ -551,32 +584,6 @@ public void doWithPersistentProperty(final CouchbasePersistentProperty prop) {
551584
}
552585
}
553586
});
554-
555-
if (idProperty != null && target.getId() == null) {
556-
String id = accessor.getProperty(idProperty, String.class);
557-
if (idProperty.isAnnotationPresent(GeneratedValue.class) && (id == null || id.equals(""))) {
558-
generatedValueInfo = idProperty.findAnnotation(GeneratedValue.class);
559-
String generatedId = generateId(generatedValueInfo, prefixes, suffixes, idAttributes);
560-
target.setId(generatedId);
561-
// this is not effective if id is Immutable, and accessor.setProperty() returns a new object in getBean()
562-
accessor.setProperty(idProperty, generatedId);
563-
} else {
564-
target.setId(id);
565-
}
566-
}
567-
568-
entity.doWithAssociations(new AssociationHandler<CouchbasePersistentProperty>() {
569-
@Override
570-
public void doWithAssociation(final Association<CouchbasePersistentProperty> association) {
571-
CouchbasePersistentProperty inverseProp = association.getInverse();
572-
Class<?> type = inverseProp.getType();
573-
Object propertyObj = accessor.getProperty(inverseProp, type);
574-
if (null != propertyObj) {
575-
writePropertyInternal(propertyObj, target, inverseProp, false);
576-
}
577-
}
578-
});
579-
580587
}
581588

582589
/**

0 commit comments

Comments
 (0)