-
Notifications
You must be signed in to change notification settings - Fork 1.1k
DATAMONGO-1245 - Add support for Query By Example. #341
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
69effc9
DATAMONGO-1245 - Add support for QBE.
christophstrobl 12f5ad8
DATAMONGO-1245 - Add support for Query By Example.
christophstrobl 0997610
DATAMONGO-1245 - QBE enhancements for specific property handling.
christophstrobl 4c04ddf
DATAMONGO-1245 - QBE now uses common MongoRegexCreator.
christophstrobl 69e81a3
DATAMONGO-1245 - Switch to types used in DATACMNS-810
christophstrobl bc0a58b
DATAMONGO-1245 - Align MongoRepsoitory QBE with DATAJPA-218.
christophstrobl ff60457
DATAMONGO-1245 - Initial documentation for Query by Example.
mp911de b77e21b
DATAMONGO-1245 - Update documentation for Query by Example.
mp911de 9e18a54
DATAMONGO-1245 - Remove escaping for native regex string matching.
mp911de 6270e86
DATAMONGO-1245 - Adopt QBE API refactoring.
mp911de 1692268
DATAMONGO-1245 - Documentation for Query-by-Example.
mp911de 4223a0c
DATAMONGO-1245 - Extend Query by Example API to typed and untyped specs.
mp911de 6be07ad
DATAMONGO-1245 - Update documentation.
mp911de c9b6344
DATAMONGO-1245 - Add changes from review.
mp911de File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
247 changes: 247 additions & 0 deletions
247
...ngodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoExampleMapper.java
This file contains hidden or 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,247 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.mongodb.core.convert; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Set; | ||
import java.util.Stack; | ||
import java.util.regex.Pattern; | ||
|
||
import org.bson.BasicBSONObject; | ||
import org.springframework.data.domain.Example; | ||
import org.springframework.data.domain.ExampleSpec; | ||
import org.springframework.data.domain.ExampleSpec.NullHandler; | ||
import org.springframework.data.domain.ExampleSpec.PropertyValueTransformer; | ||
import org.springframework.data.domain.ExampleSpec.StringMatcher; | ||
import org.springframework.data.mapping.PropertyHandler; | ||
import org.springframework.data.mapping.context.MappingContext; | ||
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; | ||
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; | ||
import org.springframework.data.mongodb.core.query.MongoRegexCreator; | ||
import org.springframework.data.mongodb.core.query.SerializationUtils; | ||
import org.springframework.data.repository.core.support.ExampleSpecAccessor; | ||
import org.springframework.data.repository.query.parser.Part.Type; | ||
import org.springframework.util.ObjectUtils; | ||
import org.springframework.util.StringUtils; | ||
|
||
import com.mongodb.BasicDBObject; | ||
import com.mongodb.DBObject; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
* @author Mark Paluch | ||
* @since 1.8 | ||
*/ | ||
public class MongoExampleMapper { | ||
|
||
private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext; | ||
private final MongoConverter converter; | ||
private final Map<StringMatcher, Type> stringMatcherPartMapping = new HashMap<StringMatcher, Type>(); | ||
|
||
public MongoExampleMapper(MongoConverter converter) { | ||
|
||
this.converter = converter; | ||
this.mappingContext = converter.getMappingContext(); | ||
|
||
stringMatcherPartMapping.put(StringMatcher.EXACT, Type.SIMPLE_PROPERTY); | ||
stringMatcherPartMapping.put(StringMatcher.CONTAINING, Type.CONTAINING); | ||
stringMatcherPartMapping.put(StringMatcher.STARTING, Type.STARTING_WITH); | ||
stringMatcherPartMapping.put(StringMatcher.ENDING, Type.ENDING_WITH); | ||
stringMatcherPartMapping.put(StringMatcher.REGEX, Type.REGEX); | ||
} | ||
|
||
/** | ||
* Returns the given {@link Example} as {@link DBObject} holding matching values extracted from | ||
* {@link Example#getProbe()}. | ||
* | ||
* @param example | ||
* @return | ||
* @since 1.8 | ||
*/ | ||
public DBObject getMappedExample(Example<?> example) { | ||
return getMappedExample(example, mappingContext.getPersistentEntity(example.getProbeType())); | ||
} | ||
|
||
/** | ||
* Returns the given {@link Example} as {@link DBObject} holding matching values extracted from | ||
* {@link Example#getProbe()}. | ||
* | ||
* @param example | ||
* @param entity | ||
* @return | ||
* @since 1.8 | ||
*/ | ||
public DBObject getMappedExample(Example<?> example, MongoPersistentEntity<?> entity) { | ||
|
||
DBObject reference = (DBObject) converter.convertToMongoType(example.getProbe()); | ||
|
||
if (entity.hasIdProperty() && entity.getIdentifierAccessor(example.getProbe()).getIdentifier() == null) { | ||
reference.removeField(entity.getIdProperty().getFieldName()); | ||
} | ||
|
||
ExampleSpecAccessor exampleSpecAccessor = new ExampleSpecAccessor(example.getExampleSpec()); | ||
|
||
applyPropertySpecs("", reference, example.getProbeType(), exampleSpecAccessor); | ||
|
||
if (exampleSpecAccessor.isTyped()) { | ||
this.converter.getTypeMapper().writeTypeRestrictions(reference, (Set) Collections.singleton(example.getResultType())); | ||
} | ||
|
||
return ObjectUtils.nullSafeEquals(NullHandler.INCLUDE, exampleSpecAccessor.getNullHandler()) ? reference | ||
: new BasicDBObject(SerializationUtils.flattenMap(reference)); | ||
} | ||
|
||
private String getMappedPropertyPath(String path, Class<?> probeType) { | ||
|
||
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(probeType); | ||
|
||
Iterator<String> parts = Arrays.asList(path.split("\\.")).iterator(); | ||
|
||
final Stack<MongoPersistentProperty> stack = new Stack<MongoPersistentProperty>(); | ||
|
||
List<String> resultParts = new ArrayList<String>(); | ||
|
||
while (parts.hasNext()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd love to see the nesting reduced here. Can we introduce eager returns/continues? |
||
|
||
final String part = parts.next(); | ||
MongoPersistentProperty prop = entity.getPersistentProperty(part); | ||
|
||
if (prop == null) { | ||
|
||
entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() { | ||
|
||
@Override | ||
public void doWithPersistentProperty(MongoPersistentProperty property) { | ||
|
||
if (property.getFieldName().equals(part)) { | ||
stack.push(property); | ||
} | ||
} | ||
}); | ||
|
||
if (stack.isEmpty()) { | ||
return ""; | ||
} | ||
prop = stack.pop(); | ||
} | ||
|
||
resultParts.add(prop.getName()); | ||
|
||
if (prop.isEntity() && mappingContext.hasPersistentEntityFor(prop.getActualType())) { | ||
entity = mappingContext.getPersistentEntity(prop.getActualType()); | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
return StringUtils.collectionToDelimitedString(resultParts, "."); | ||
|
||
} | ||
|
||
private void applyPropertySpecs(String path, DBObject source, Class<?> probeType, | ||
ExampleSpecAccessor exampleSpecAccessor) { | ||
|
||
if (!(source instanceof BasicDBObject)) { | ||
return; | ||
} | ||
|
||
Iterator<Map.Entry<String, Object>> iter = ((BasicDBObject) source).entrySet().iterator(); | ||
|
||
while (iter.hasNext()) { | ||
|
||
Map.Entry<String, Object> entry = iter.next(); | ||
String propertyPath = StringUtils.hasText(path) ? path + "." + entry.getKey() : entry.getKey(); | ||
String mappedPropertyPath = getMappedPropertyPath(propertyPath, probeType); | ||
|
||
if(isEmptyIdProperty(entry)) { | ||
iter.remove(); | ||
continue; | ||
} | ||
|
||
if (exampleSpecAccessor.isIgnoredPath(propertyPath) || exampleSpecAccessor.isIgnoredPath(mappedPropertyPath)) { | ||
iter.remove(); | ||
continue; | ||
} | ||
|
||
StringMatcher stringMatcher = exampleSpecAccessor.getDefaultStringMatcher(); | ||
Object value = entry.getValue(); | ||
boolean ignoreCase = exampleSpecAccessor.isIgnoreCaseEnabled(); | ||
|
||
if (exampleSpecAccessor.hasPropertySpecifiers()) { | ||
|
||
mappedPropertyPath = exampleSpecAccessor.hasPropertySpecifier(propertyPath) ? propertyPath | ||
: getMappedPropertyPath(propertyPath, probeType); | ||
|
||
stringMatcher = exampleSpecAccessor.getStringMatcherForPath(mappedPropertyPath); | ||
ignoreCase = exampleSpecAccessor.isIgnoreCaseForPath(mappedPropertyPath); | ||
} | ||
|
||
// TODO: should a PropertySpecifier outrule the later on string matching? | ||
if (exampleSpecAccessor.hasPropertySpecifier(mappedPropertyPath)) { | ||
|
||
PropertyValueTransformer valueTransformer = exampleSpecAccessor.getValueTransformerForPath(mappedPropertyPath); | ||
value = valueTransformer.convert(value); | ||
if (value == null) { | ||
iter.remove(); | ||
continue; | ||
} | ||
|
||
entry.setValue(value); | ||
} | ||
|
||
if (entry.getValue() instanceof String) { | ||
applyStringMatcher(entry, stringMatcher, ignoreCase); | ||
} else if (entry.getValue() instanceof BasicDBObject) { | ||
applyPropertySpecs(propertyPath, (BasicDBObject) entry.getValue(), probeType, exampleSpecAccessor); | ||
} | ||
} | ||
} | ||
|
||
private boolean isEmptyIdProperty(Entry<String, Object> entry) { | ||
return entry.getKey().equals("_id") && entry.getValue() == null; | ||
} | ||
|
||
private void applyStringMatcher(Map.Entry<String, Object> entry, StringMatcher stringMatcher, boolean ignoreCase) { | ||
|
||
BasicDBObject dbo = new BasicDBObject(); | ||
|
||
if (ObjectUtils.nullSafeEquals(StringMatcher.DEFAULT, stringMatcher)) { | ||
|
||
if (ignoreCase) { | ||
dbo.put("$regex", Pattern.quote((String) entry.getValue())); | ||
entry.setValue(dbo); | ||
} | ||
} else { | ||
|
||
Type type = stringMatcherPartMapping.get(stringMatcher); | ||
String expression = MongoRegexCreator.INSTANCE.toRegularExpression((String) entry.getValue(), type); | ||
dbo.put("$regex", expression); | ||
entry.setValue(dbo); | ||
} | ||
|
||
if (ignoreCase) { | ||
dbo.put("$options", "i"); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change seems to be unrelated to the PR. @christophstrobl — can you comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true - I'll open an issue for that one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
created DATAMONGO-1390.