Skip to content

Commit 62feffa

Browse files
committed
Add support for ignoreCase in queries derived from method names.
Closes #984, #1481.
1 parent 1d6c033 commit 62feffa

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
@@ -507,6 +507,39 @@ protected void writeInternal(final Object source, final CouchbaseDocument target
507507

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

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

581588
/**

0 commit comments

Comments
 (0)