first part of MongoTemplate/Query refactoring

This commit is contained in:
Thomas Risberg
2011-02-03 16:02:11 -05:00
parent f9e50916e1
commit 9e5eb96150
26 changed files with 477 additions and 789 deletions

View File

@@ -18,11 +18,10 @@ package org.springframework.data.document.mongodb;
import java.util.List;
import java.util.Set;
import org.springframework.data.document.mongodb.builder.Query;
import org.springframework.data.document.mongodb.builder.QueryDefinition;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.WriteConcern;
/**
* Interface that specifies a basic set of MongoDB operations. Implemented by {@link MongoTemplate}.
@@ -397,14 +396,14 @@ public interface MongoOperations {
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link QuerySpec}.
* The query is specified as a {@link QueryDefinition} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link Query}.
*
* @param query the query class that specifies the criteria used to find a record and also an optional fields specification
* @param targetClass the parameterized type of the returned list.
* @return the List of converted objects
*/
<T> List<T> query(Query query, Class<T> targetClass);
<T> List<T> find(QueryDefinition query, Class<T> targetClass);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
@@ -413,15 +412,15 @@ public interface MongoOperations {
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link QuerySpec}.
* The query is specified as a {@link QueryDefinition} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link Query}.
*
* @param query the query class that specifies the criteria used to find a record and also an optional fields specification
* @param targetClass the parameterized type of the returned list.
* @param reader the MongoReader to convert from DBObject to an object.
* @return the List of converted objects
*/
<T> List<T> query(Query query, Class<T> targetClass,
<T> List<T> find(QueryDefinition query, Class<T> targetClass,
MongoReader<T> reader);
/**
@@ -431,15 +430,15 @@ public interface MongoOperations {
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link QuerySpec}.
* The query is specified as a {@link QueryDefinition} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link Query}.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query class that specifies the criteria used to find a record and also an optional fields specification
* @param targetClass the parameterized type of the returned list.
* @return the List of converted objects
*/
<T> List<T> query(String collectionName, Query query,
<T> List<T> find(String collectionName, QueryDefinition query,
Class<T> targetClass);
/**
@@ -449,8 +448,8 @@ public interface MongoOperations {
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link QuerySpec}.
* The query is specified as a {@link QueryDefinition} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link Query}.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query class that specifies the criteria used to find a record and also an optional fields specification
@@ -458,27 +457,9 @@ public interface MongoOperations {
* @param reader the MongoReader to convert from DBObject to an object.
* @return the List of converted objects
*/
<T> List<T> query(String collectionName, Query query,
<T> List<T> find(String collectionName, QueryDefinition query,
Class<T> targetClass, MongoReader<T> reader);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject.
*
* NOTE: A generic criteria API will be introduced in a future release. You can see the
* <a href="https://github.com/grails/inconsequential">inconsequential</a> project for an example of how that
* may look.
*
* @param query the query document that specifies the criteria used to find a record
* @param targetClass the parameterized type of the returned list.
* @return the List of converted objects
*/
<T> List<T> find(DBObject query, Class<T> targetClass);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
@@ -487,184 +468,16 @@ public interface MongoOperations {
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject.
* The query is specified as a {@link QueryDefinition} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link Query}.
*
* @param query the query document that specifies the criteria used to find a record
* @param collectionName name of the collection to retrieve the objects from
* @param query the query class that specifies the criteria used to find a record and also an optional fields specification
* @param targetClass the parameterized type of the returned list.
* @param preparer allows for customization of the DBCursor used when iterating over the result set,
* (apply limits, skips and so on).
* @return the List of converted objects.
*/
<T> List<T> find(DBObject query, Class<T> targetClass,
CursorPreparer preparer);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader
*
* The query document is specified as a standard DBObject.
*
* @param query the query document that specifies the criteria used to find a record
* @param targetClass the parameterized type of the returned list.
* @param reader the MongoReader to convert from DBObject to an object.
* @return the List of converted objects.
*/
<T> List<T> find(DBObject query, Class<T> targetClass,
MongoReader<T> reader);
<T> List<T> find(String collectionName, QueryDefinition query, Class<T> targetClass, CursorPreparer preparer);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject.
*
* NOTE: A generic criteria API will be introduced in a future release. You can see the
* <a href="https://github.com/grails/inconsequential">inconsequential</a> project for an example of how that
* may look.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param targetClass the parameterized type of the returned list.
* @return the List of converted objects
*/
<T> List<T> find(String collectionName, DBObject query, Class<T> targetClass);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param targetClass the parameterized type of the returned list.
* @param preparer allows for customization of the DBCursor used when iterating over the result set,
* (apply limits, skips and so on).
* @return the List of converted objects.
*/
<T> List<T> find(String collectionName, DBObject query, Class<T> targetClass, CursorPreparer preparer);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader
*
* The query document is specified as a standard DBObject.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param targetClass the parameterized type of the returned list.
* @param reader the MongoReader to convert from DBObject to an object.
* @return the List of converted objects.
*/
<T> List<T> find(String collectionName, DBObject query, Class<T> targetClass, MongoReader<T> reader);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject and so is the fields specification.
*
* NOTE: A generic criteria API will be introduced in a future release. You can see the
* <a href="https://github.com/grails/inconsequential">inconsequential</a> project for an example of how that
* may look.
*
* @param query the query document that specifies the criteria used to find a record
* @param targetClass the parameterized type of the returned list.
* @return the List of converted objects
*/
<T> List<T> find(DBObject query, DBObject fields, Class<T> targetClass);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject and so is the fields specification.
*
* @param query the query document that specifies the criteria used to find a record
* @param fields the document that specifies the fields to be returned
* @param targetClass the parameterized type of the returned list.
* @param preparer allows for customization of the DBCursor used when iterating over the result set,
* (apply limits, skips and so on).
* @return the List of converted objects.
*/
<T> List<T> find(DBObject query, DBObject fields, Class<T> targetClass,
CursorPreparer preparer);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader
*
* The query document is specified as a standard DBObject and so is the fields specification.
*
* @param query the query document that specifies the criteria used to find a record
* @param fields the document that specifies the fields to be returned
* @param targetClass the parameterized type of the returned list.
* @param reader the MongoReader to convert from DBObject to an object.
* @return the List of converted objects.
*/
<T> List<T> find(DBObject query, DBObject fields, Class<T> targetClass,
MongoReader<T> reader);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject and so is the fields specification.
*
* NOTE: A generic criteria API will be introduced in a future release. You can see the
* <a href="https://github.com/grails/inconsequential">inconsequential</a> project for an example of how that
* may look.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param fields the document that specifies the fields to be returned
* @param targetClass the parameterized type of the returned list.
* @return the List of converted objects
*/
<T> List<T> find(String collectionName, DBObject fields, DBObject query, Class<T> targetClass);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject and so is the fields specification.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param fields the document that specifies the fields to be returned
* @param targetClass the parameterized type of the returned list.
* @param preparer allows for customization of the DBCursor used when iterating over the result set,
* (apply limits, skips and so on).
* @return the List of converted objects.
*/
<T> List<T> find(String collectionName, DBObject fields, DBObject query, Class<T> targetClass, CursorPreparer preparer);
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader
*
* The query document is specified as a standard DBObject and so is the fields specification.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param fields the document that specifies the fields to be returned
* @param targetClass the parameterized type of the returned list.
* @param reader the MongoReader to convert from DBObject to an object.
* @return the List of converted objects.
*/
<T> List<T> find(String collectionName, DBObject fields, DBObject query, Class<T> targetClass, MongoReader<T> reader);
}

View File

@@ -28,7 +28,7 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
import org.springframework.data.document.mongodb.builder.Query;
import org.springframework.data.document.mongodb.builder.QueryDefinition;
import org.springframework.jca.cci.core.ConnectionCallback;
import org.springframework.util.Assert;
@@ -635,17 +635,11 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
getDefaultCollectionName());
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#getCollection(java.lang.String, java.lang.Class)
*/
public <T> List<T> getCollection(String collectionName, Class<T> targetClass) {
return executeEach(new FindCallback(null), null, new ReadDbObjectCallback<T>(mongoConverter, targetClass),
collectionName);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#getCollectionNames()
*/
public Set<String> getCollectionNames() {
return execute(new DbCallback<Set<String>>() {
public Set<String> doInDB(DB db) throws MongoException, DataAccessException {
@@ -654,131 +648,71 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
});
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#getCollection(java.lang.String, java.lang.Class, org.springframework.data.document.mongodb.MongoReader)
*/
public <T> List<T> getCollection(String collectionName, Class<T> targetClass, MongoReader<T> reader) {
return executeEach(new FindCallback(null), null, new ReadDbObjectCallback<T>(reader, targetClass),
collectionName);
}
// Queries that take JavaScript to express the query.
// Find methods that take a QueryDefinition to express the query.
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.Class)
*/
public <T> List<T> query(Query query, Class<T> targetClass) {
return query(getDefaultCollectionName(), query, targetClass); //
public <T> List<T> find(QueryDefinition query, Class<T> targetClass) {
return find(getDefaultCollectionName(), query, targetClass); //
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.Class, org.springframework.data.document.mongodb.MongoReader)
*/
public <T> List<T> query(Query query, Class<T> targetClass, MongoReader<T> reader) {
return query(getDefaultCollectionName(), query, targetClass, reader);
public <T> List<T> find(QueryDefinition query, Class<T> targetClass, MongoReader<T> reader) {
return find(getDefaultCollectionName(), query, targetClass, reader);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.String, java.lang.Class)
*/
public <T> List<T> query(String collectionName, Query query, Class<T> targetClass) {
return find(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass); //
public <T> List<T> find(String collectionName, QueryDefinition query, Class<T> targetClass) {
return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, (CursorPreparer) null);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.String, java.lang.Class, org.springframework.data.document.mongodb.MongoReader)
*/
public <T> List<T> query(String collectionName, Query query, Class<T> targetClass, MongoReader<T> reader) {
return find(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, reader);
public <T> List<T> find(String collectionName, QueryDefinition query, Class<T> targetClass, MongoReader<T> reader) {
return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, reader);
}
// Find methods that take DBObject to express the query
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class)
*/
public <T> List<T> find(DBObject query, Class<T> targetClass) {
return find(query, null, targetClass); //
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer)
*/
public <T> List<T> find(DBObject query, Class<T> targetClass, CursorPreparer preparer) {
return find(query, null, targetClass, preparer); //
public <T> List<T> find(String collectionName, QueryDefinition query,
Class<T> targetClass, CursorPreparer preparer) {
return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, preparer);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader)
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type.
*
* The object is converted from the MongoDB native representation using an instance of
* {@see MongoConverter}. Unless configured otherwise, an
* instance of SimpleMongoConverter will be used.
*
* The query document is specified as a standard DBObject and so is the fields specification.
*
* Can be overridden by subclasses.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param fields the document that specifies the fields to be returned
* @param targetClass the parameterized type of the returned list.
* @param preparer allows for customization of the DBCursor used when iterating over the result set,
* (apply limits, skips and so on).
* @return the List of converted objects.
*/
public <T> List<T> find(DBObject query, Class<T> targetClass, MongoReader<T> reader) {
return find(query, null, targetClass, reader);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class)
*/
public <T> List<T> find(String collectionName, DBObject query, Class<T> targetClass) {
return find(collectionName, query, null, targetClass);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer)
*/
public <T> List<T> find(String collectionName, DBObject query, Class<T> targetClass, CursorPreparer preparer) {
return find(collectionName, query, null, targetClass, preparer);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader)
*/
public <T> List<T> find(String collectionName, DBObject query, Class<T> targetClass, MongoReader<T> reader) {
return find(collectionName, query, null, targetClass, reader);
}
// Find methods that take DBObject to express the query and a DBObject to express the fields specification
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class)
*/
public <T> List<T> find(DBObject query, DBObject fields, Class<T> targetClass) {
return find(getDefaultCollectionName(), query, fields, targetClass); //
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer)
*/
public <T> List<T> find(DBObject query, DBObject fields, Class<T> targetClass, CursorPreparer preparer) {
return find(getDefaultCollectionName(), query, fields, targetClass, preparer); //
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader)
*/
public <T> List<T> find(DBObject query, DBObject fields, Class<T> targetClass, MongoReader<T> reader) {
return find(getDefaultCollectionName(), query, fields, targetClass, reader);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class)
*/
public <T> List<T> find(String collectionName, DBObject query, DBObject fields, Class<T> targetClass) {
return find(collectionName, query, fields, targetClass, (CursorPreparer) null);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer)
*/
public <T> List<T> find(String collectionName, DBObject query, DBObject fields, Class<T> targetClass, CursorPreparer preparer) {
protected <T> List<T> doFind(String collectionName, DBObject query, DBObject fields, Class<T> targetClass, CursorPreparer preparer) {
return executeEach(new FindCallback(query, fields), preparer, new ReadDbObjectCallback<T>(mongoConverter, targetClass),
collectionName);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader)
/**
* Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader
*
* The query document is specified as a standard DBObject and so is the fields specification.
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param fields the document that specifies the fields to be returned
* @param targetClass the parameterized type of the returned list.
* @param reader the MongoReader to convert from DBObject to an object.
* @return the List of converted objects.
*/
public <T> List<T> find(String collectionName, DBObject query, DBObject fields, Class<T> targetClass, MongoReader<T> reader) {
protected <T> List<T> doFind(String collectionName, DBObject query, DBObject fields, Class<T> targetClass, MongoReader<T> reader) {
return executeEach(new FindCallback(query, fields), null, new ReadDbObjectCallback<T>(reader, targetClass),
collectionName);
}
@@ -919,4 +853,5 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
return reader.read(type, object);
}
}
}

View File

@@ -18,7 +18,7 @@ package org.springframework.data.document.mongodb.builder;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
public class BasicQuery implements Query {
public class BasicQuery implements QueryDefinition {
private DBObject queryObject = null;

View File

@@ -18,7 +18,7 @@ package org.springframework.data.document.mongodb.builder;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
public class BasicUpdate implements Update {
public class BasicUpdate implements UpdateDefinition {
private DBObject updateObject = null;

View File

@@ -15,10 +15,147 @@
*/
package org.springframework.data.document.mongodb.builder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.springframework.data.document.InvalidDocumentStoreApiUsageException;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
public interface Criteria {
public class Criteria implements CriteriaDefinition {
private Query qb = null;
private LinkedHashMap<String, Object> criteria = new LinkedHashMap<String, Object>();
DBObject getCriteriaObject(String key);
private Object isValue = null;
public Criteria(Query qb) {
super();
this.qb = qb;
}
}
public Criteria and(String key) {
return qb.find(key);
}
public Criteria is(Object o) {
if (isValue != null) {
throw new InvalidDocumentStoreApiUsageException("Multiple 'is' values declared.");
}
this.isValue = o;
return this;
}
public Criteria lt(Object o) {
criteria.put("$lt", o);
return this;
}
public Criteria lte(Object o) {
criteria.put("$lte", o);
return this;
}
public Criteria gt(Object o) {
criteria.put("$gt", o);
return this;
}
public Criteria gte(Object o) {
criteria.put("$gte", o);
return this;
}
public Criteria in(Object... o) {
criteria.put("$in", o);
return this;
}
public Criteria nin(Object... o) {
criteria.put("$min", o);
return this;
}
public Criteria mod(Number value, Number remainder) {
List<Object> l = new ArrayList<Object>();
l.add(value);
l.add(remainder);
criteria.put("$mod", l);
return this;
}
public Criteria all(Object o) {
criteria.put("$is", o);
return this;
}
public Criteria size(Object o) {
criteria.put("$is", o);
return this;
}
public Criteria exists(boolean b) {
return this;
}
public Criteria type(int t) {
return this;
}
public Criteria not() {
criteria.put("$not", null);
return this;
}
public Criteria regExp(String re) {
return this;
}
public void or(List<Query> queries) {
criteria.put("$or", queries);
}
public Query build() {
return qb;
}
/* (non-Javadoc)
* @see org.springframework.datastore.document.mongodb.query.Criteria#getCriteriaObject(java.lang.String)
*/
public DBObject getCriteriaObject(String key) {
DBObject dbo = new BasicDBObject();
boolean not = false;
for (String k : criteria.keySet()) {
if (not) {
DBObject notDbo = new BasicDBObject();
notDbo.put(k, criteria.get(k));
dbo.put("$not", notDbo);
not = false;
}
else {
if ("$not".equals(k)) {
not = true;
}
else {
dbo.put(k, criteria.get(k));
}
}
}
DBObject queryCriteria = new BasicDBObject();
if (isValue != null) {
queryCriteria.put(key, isValue);
queryCriteria.putAll(dbo);
}
else {
queryCriteria.put(key, dbo);
}
return queryCriteria;
}
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2010-2011 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.document.mongodb.builder;
import com.mongodb.DBObject;
public interface CriteriaDefinition {
DBObject getCriteriaObject(String key);
}

View File

@@ -1,161 +0,0 @@
/*
* Copyright 2010-2011 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.document.mongodb.builder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.springframework.data.document.InvalidDocumentStoreApiUsageException;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
public class CriteriaSpec implements Criteria {
private QuerySpec qb = null;
private LinkedHashMap<String, Object> criteria = new LinkedHashMap<String, Object>();
private Object isValue = null;
public CriteriaSpec(QuerySpec qb) {
super();
this.qb = qb;
}
public CriteriaSpec and(String key) {
return qb.find(key);
}
public CriteriaSpec is(Object o) {
if (isValue != null) {
throw new InvalidDocumentStoreApiUsageException("Multiple 'is' values declared.");
}
this.isValue = o;
return this;
}
public CriteriaSpec lt(Object o) {
criteria.put("$lt", o);
return this;
}
public CriteriaSpec lte(Object o) {
criteria.put("$lte", o);
return this;
}
public CriteriaSpec gt(Object o) {
criteria.put("$gt", o);
return this;
}
public CriteriaSpec gte(Object o) {
criteria.put("$gte", o);
return this;
}
public CriteriaSpec in(Object... o) {
criteria.put("$in", o);
return this;
}
public CriteriaSpec nin(Object... o) {
criteria.put("$min", o);
return this;
}
public CriteriaSpec mod(Number value, Number remainder) {
List<Object> l = new ArrayList<Object>();
l.add(value);
l.add(remainder);
criteria.put("$mod", l);
return this;
}
public CriteriaSpec all(Object o) {
criteria.put("$is", o);
return this;
}
public CriteriaSpec size(Object o) {
criteria.put("$is", o);
return this;
}
public CriteriaSpec exists(boolean b) {
return this;
}
public CriteriaSpec type(int t) {
return this;
}
public CriteriaSpec not() {
criteria.put("$not", null);
return this;
}
public CriteriaSpec regExp(String re) {
return this;
}
public void or(List<Query> queries) {
criteria.put("$or", queries);
}
public Query build() {
return qb.build();
}
/* (non-Javadoc)
* @see org.springframework.datastore.document.mongodb.query.Criteria#getCriteriaObject(java.lang.String)
*/
public DBObject getCriteriaObject(String key) {
DBObject dbo = new BasicDBObject();
boolean not = false;
for (String k : criteria.keySet()) {
if (not) {
DBObject notDbo = new BasicDBObject();
notDbo.put(k, criteria.get(k));
dbo.put("$not", notDbo);
not = false;
}
else {
if ("$not".equals(k)) {
not = true;
}
else {
dbo.put(k, criteria.get(k));
}
}
}
DBObject queryCriteria = new BasicDBObject();
if (isValue != null) {
queryCriteria.put(key, isValue);
queryCriteria.putAll(dbo);
}
else {
queryCriteria.put(key, dbo);
}
return queryCriteria;
}
}

View File

@@ -22,28 +22,28 @@ import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
public class FieldSpec {
public class Field {
private Map<String, Integer> criteria = new HashMap<String, Integer>();
private Map<String, Object> slices = new HashMap<String, Object>();
public FieldSpec include(String key) {
public Field include(String key) {
criteria.put(key, Integer.valueOf(1));
return this;
}
public FieldSpec exclude(String key) {
public Field exclude(String key) {
criteria.put(key, Integer.valueOf(0));
return this;
}
public FieldSpec slice(String key, int size) {
public Field slice(String key, int size) {
slices.put(key, Integer.valueOf(size));
return this;
}
public FieldSpec slice(String key, int offset, int size) {
public Field slice(String key, int offset, int size) {
slices.put(key, new Integer[] {Integer.valueOf(offset), Integer.valueOf(size)});
return this;
}

View File

@@ -20,11 +20,11 @@ import org.bson.types.BasicBSONList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
public class OrCriteriaSpec implements Criteria {
public class OrCriteria implements CriteriaDefinition {
Query[] queries = null;
QueryDefinition[] queries = null;
public OrCriteriaSpec(Query[] queries) {
public OrCriteria(QueryDefinition[] queries) {
super();
this.queries = queries;
}
@@ -36,7 +36,7 @@ public class OrCriteriaSpec implements Criteria {
public DBObject getCriteriaObject(String key) {
DBObject dbo = new BasicDBObject();
BasicBSONList l = new BasicBSONList();
for (Query q : queries) {
for (QueryDefinition q : queries) {
l.add(q.getQueryObject());
}
dbo.put(key, l);

View File

@@ -15,14 +15,71 @@
*/
package org.springframework.data.document.mongodb.builder;
import java.util.LinkedHashMap;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
public interface Query {
public class Query implements QueryDefinition {
DBObject getQueryObject();
DBObject getFieldsObject();
private LinkedHashMap<String, CriteriaDefinition> criteria = new LinkedHashMap<String, CriteriaDefinition>();
int getLimit();
private Field fieldSpec;
private int limit;
public static Criteria newQuery(String key) {
return new Query().find(key);
}
public Criteria find(String key) {
Criteria c = new Criteria(this);
this.criteria.put(key, c);
return c;
}
public Query or(QueryDefinition... queries) {
this.criteria.put("$or", new OrCriteria(queries));
return this;
}
public Field fields() {
synchronized (this) {
if (fieldSpec == null) {
this.fieldSpec = new Field();
}
}
return this.fieldSpec;
}
public Query limit(int limit) {
this.limit = limit;
return this;
}
public QueryDefinition build() {
return this;
}
public DBObject getQueryObject() {
DBObject dbo = new BasicDBObject();
for (String k : criteria.keySet()) {
CriteriaDefinition c = criteria.get(k);
DBObject cl = c.getCriteriaObject(k);
dbo.putAll(cl);
}
return dbo;
}
public DBObject getFieldsObject() {
if (this.fieldSpec == null) {
return null;
}
return fieldSpec.getFieldsObject();
}
public int getLimit() {
return this.limit;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011 the original author or authors.
* Copyright 2010-2011 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.
@@ -16,22 +16,13 @@
package org.springframework.data.document.mongodb.builder;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
public class BasicSort implements Sort {
private DBObject sortObject;
public interface QueryDefinition {
public BasicSort(String sortString) {
this.sortObject = (DBObject) JSON.parse(sortString);
}
DBObject getQueryObject();
DBObject getFieldsObject();
public BasicSort(DBObject sortObject) {
this.sortObject = sortObject;
}
public DBObject getSortObject() {
return this.sortObject;
}
int getLimit();
}

View File

@@ -1,81 +0,0 @@
/*
* Copyright 2010-2011 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.document.mongodb.builder;
import java.util.LinkedHashMap;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
public class QuerySpec implements Query {
private LinkedHashMap<String, Criteria> criteria = new LinkedHashMap<String, Criteria>();
private FieldSpec fieldSpec;
private int limit;
public CriteriaSpec find(String key) {
CriteriaSpec c = new CriteriaSpec(this);
this.criteria.put(key, c);
return c;
}
public QuerySpec or(Query... queries) {
this.criteria.put("$or", new OrCriteriaSpec(queries));
return this;
}
public FieldSpec fields() {
synchronized (this) {
if (fieldSpec == null) {
this.fieldSpec = new FieldSpec();
}
}
return this.fieldSpec;
}
public QuerySpec limit(int limit) {
this.limit = limit;
return this;
}
public Query build() {
return this;
}
public DBObject getQueryObject() {
DBObject dbo = new BasicDBObject();
for (String k : criteria.keySet()) {
Criteria c = criteria.get(k);
DBObject cl = c.getCriteriaObject(k);
dbo.putAll(cl);
}
return dbo;
}
public DBObject getFieldsObject() {
if (this.fieldSpec == null) {
return null;
}
return fieldSpec.getFieldsObject();
}
public int getLimit() {
return this.limit;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011 the original author or authors.
* Copyright 2010-2011 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.
@@ -15,10 +15,32 @@
*/
package org.springframework.data.document.mongodb.builder;
import java.util.HashMap;
import java.util.Map;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
public interface Sort {
public class Sort {
DBObject getSortObject();
public enum Order {
ASCENDING, DESCENDING
}
private Map<String, Order> criteria = new HashMap<String, Order>();
public Sort on(String key, Order order) {
criteria.put(key, order);
return this;
}
public DBObject getSortObject() {
DBObject dbo = new BasicDBObject();
for (String k : criteria.keySet()) {
dbo.put(k, (criteria.get(k).equals(Order.ASCENDING) ? 1 : -1));
}
return dbo;
}
}

View File

@@ -15,10 +15,85 @@
*/
package org.springframework.data.document.mongodb.builder;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
public class Update implements UpdateDefinition {
public interface Update {
public enum Position {
LAST, FIRST
}
private HashMap<String, Object> criteria = new LinkedHashMap<String, Object>();
DBObject getUpdateObject();
public Update set(String key, Object value) {
criteria.put("$set", Collections.singletonMap(key, value));
return this;
}
public Update unset(String key) {
criteria.put("$unset", Collections.singletonMap(key, 1));
return this;
}
public Update inc(String key, long inc) {
criteria.put("$inc", Collections.singletonMap(key, inc));
return this;
}
public Update push(String key, Object value) {
criteria.put("$push", Collections.singletonMap(key, value));
return this;
}
public Update pushAll(String key, Object[] values) {
DBObject keyValue = new BasicDBObject();
keyValue.put(key, values);
criteria.put("$pushAll", keyValue);
return this;
}
public Update addToSet(String key, Object value) {
criteria.put("$addToSet", Collections.singletonMap(key, value));
return this;
}
public Update pop(String key, Position pos) {
criteria.put("$pop", Collections.singletonMap(key, (pos == Position.FIRST ? -1 : 1)));
return this;
}
public Update pull(String key, Object value) {
criteria.put("$pull", Collections.singletonMap(key, value));
return this;
}
public Update pullAll(String key, Object[] values) {
DBObject keyValue = new BasicDBObject();
keyValue.put(key, values);
criteria.put("$pullAll", keyValue);
return this;
}
public Update rename(String oldName, String newName) {
criteria.put("$rename", Collections.singletonMap(oldName, newName));
return this;
}
public UpdateDefinition build() {
return this;
}
public DBObject getUpdateObject() {
DBObject dbo = new BasicDBObject();
for (String k : criteria.keySet()) {
dbo.put(k, criteria.get(k));
}
return dbo;
}
}

View File

@@ -15,36 +15,10 @@
*/
package org.springframework.data.document.mongodb.builder;
import java.util.HashMap;
import java.util.Map;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
public class SortSpec implements Sort {
public interface UpdateDefinition {
public enum SortOrder {
ASCENDING, DESCENDING
}
private Map<String, SortOrder> criteria = new HashMap<String, SortOrder>();
public SortSpec on(String key, SortOrder order) {
criteria.put(key, order);
return this;
}
public Sort build() {
return this;
}
public DBObject getSortObject() {
DBObject dbo = new BasicDBObject();
for (String k : criteria.keySet()) {
dbo.put(k, (criteria.get(k).equals(SortOrder.ASCENDING) ? 1 : -1));
}
return dbo;
}
DBObject getUpdateObject();
}

View File

@@ -1,99 +0,0 @@
/*
* Copyright 2010-2011 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.document.mongodb.builder;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
public class UpdateSpec implements Update {
public enum Position {
LAST, FIRST
}
private HashMap<String, Object> criteria = new LinkedHashMap<String, Object>();
public UpdateSpec set(String key, Object value) {
criteria.put("$set", Collections.singletonMap(key, value));
return this;
}
public UpdateSpec unset(String key) {
criteria.put("$unset", Collections.singletonMap(key, 1));
return this;
}
public UpdateSpec inc(String key, long inc) {
criteria.put("$inc", Collections.singletonMap(key, inc));
return this;
}
public UpdateSpec push(String key, Object value) {
criteria.put("$push", Collections.singletonMap(key, value));
return this;
}
public UpdateSpec pushAll(String key, Object[] values) {
DBObject keyValue = new BasicDBObject();
keyValue.put(key, values);
criteria.put("$pushAll", keyValue);
return this;
}
public UpdateSpec addToSet(String key, Object value) {
criteria.put("$addToSet", Collections.singletonMap(key, value));
return this;
}
public UpdateSpec pop(String key, Position pos) {
criteria.put("$pop", Collections.singletonMap(key, (pos == Position.FIRST ? -1 : 1)));
return this;
}
public UpdateSpec pull(String key, Object value) {
criteria.put("$pull", Collections.singletonMap(key, value));
return this;
}
public UpdateSpec pullAll(String key, Object[] values) {
DBObject keyValue = new BasicDBObject();
keyValue.put(key, values);
criteria.put("$pullAll", keyValue);
return this;
}
public UpdateSpec rename(String oldName, String newName) {
criteria.put("$rename", Collections.singletonMap(oldName, newName));
return this;
}
public Update build() {
return this;
}
public DBObject getUpdateObject() {
DBObject dbo = new BasicDBObject();
for (String k : criteria.keySet()) {
dbo.put(k, criteria.get(k));
}
return dbo;
}
}

View File

@@ -21,7 +21,7 @@ import java.util.List;
import org.springframework.data.document.mongodb.CollectionCallback;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.builder.QuerySpec;
import org.springframework.data.document.mongodb.builder.Query;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.QueryMethod;
@@ -76,7 +76,7 @@ public class MongoQuery implements RepositoryQuery {
SimpleParameterAccessor accessor =
new SimpleParameterAccessor(method.getParameters(), parameters);
QuerySpec spec = new QuerySpec();
Query spec = new Query();
MongoQueryCreator creator =
new MongoQueryCreator(spec, tree, accessor,
@@ -95,12 +95,12 @@ public class MongoQuery implements RepositoryQuery {
private abstract class Execution {
abstract Object execute(QuerySpec query);
abstract Object execute(Query query);
protected List<?> readCollection(QuerySpec query) {
protected List<?> readCollection(Query query) {
return template.query(query.build(), method.getDomainClass());
return template.find(query.build(), method.getDomainClass());
}
}
@@ -119,7 +119,7 @@ public class MongoQuery implements RepositoryQuery {
* #execute(com.mongodb.DBObject)
*/
@Override
public Object execute(QuerySpec query) {
public Object execute(Query query) {
return readCollection(query);
}
@@ -159,12 +159,12 @@ public class MongoQuery implements RepositoryQuery {
*/
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
Object execute(QuerySpec query) {
Object execute(Query query) {
int count = getCollectionCursor(query.getQueryObject()).count();
List<?> result =
template.query(applyPagination(query, pageable),
template.find(applyPagination(query, pageable),
method.getDomainClass());
return new PageImpl(result, pageable, count);
@@ -198,7 +198,7 @@ public class MongoQuery implements RepositoryQuery {
* #execute(com.mongodb.DBObject)
*/
@Override
Object execute(QuerySpec query) {
Object execute(Query query) {
List<?> result = readCollection(query);
return result.isEmpty() ? null : result.get(0);

View File

@@ -21,10 +21,10 @@ import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.document.mongodb.MongoConverter;
import org.springframework.data.document.mongodb.builder.CriteriaDefinition;
import org.springframework.data.document.mongodb.builder.Criteria;
import org.springframework.data.document.mongodb.builder.CriteriaSpec;
import org.springframework.data.document.mongodb.builder.QueryDefinition;
import org.springframework.data.document.mongodb.builder.Query;
import org.springframework.data.document.mongodb.builder.QuerySpec;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.SimpleParameterAccessor;
import org.springframework.data.repository.query.SimpleParameterAccessor.BindableParameterIterator;
@@ -42,11 +42,11 @@ import com.mongodb.DBObject;
*
* @author Oliver Gierke
*/
class MongoQueryCreator extends AbstractQueryCreator<Void, CriteriaSpec> {
class MongoQueryCreator extends AbstractQueryCreator<Void, Criteria> {
private static final Log LOG = LogFactory.getLog(MongoQueryCreator.class);
private final MongoConverter converter;
private final QuerySpec querySpec;
private final Query querySpec;
/**
@@ -56,7 +56,7 @@ class MongoQueryCreator extends AbstractQueryCreator<Void, CriteriaSpec> {
* @param tree
* @param accessor
*/
public MongoQueryCreator(QuerySpec querySpec, PartTree tree,
public MongoQueryCreator(Query querySpec, PartTree tree,
SimpleParameterAccessor accessor, MongoConverter converter) {
super(tree, accessor);
@@ -75,7 +75,7 @@ class MongoQueryCreator extends AbstractQueryCreator<Void, CriteriaSpec> {
* .data.repository.query.SimpleParameterAccessor.BindableParameterIterator)
*/
@Override
protected CriteriaSpec create(Part part, BindableParameterIterator iterator) {
protected Criteria create(Part part, BindableParameterIterator iterator) {
return from(part.getType(),
querySpec.find(part.getProperty().toDotPath()), iterator);
@@ -92,7 +92,7 @@ class MongoQueryCreator extends AbstractQueryCreator<Void, CriteriaSpec> {
* .data.repository.query.SimpleParameterAccessor.BindableParameterIterator)
*/
@Override
protected CriteriaSpec and(Part part, CriteriaSpec base,
protected Criteria and(Part part, Criteria base,
BindableParameterIterator iterator) {
return from(part.getType(), base.and(part.getProperty().toDotPath()),
@@ -108,7 +108,7 @@ class MongoQueryCreator extends AbstractQueryCreator<Void, CriteriaSpec> {
* #or(java.lang.Object, java.lang.Object)
*/
@Override
protected CriteriaSpec or(CriteriaSpec base, CriteriaSpec criteria) {
protected Criteria or(Criteria base, Criteria criteria) {
base.or(Arrays.asList(criteria.build()));
return base;
@@ -123,7 +123,7 @@ class MongoQueryCreator extends AbstractQueryCreator<Void, CriteriaSpec> {
* #complete(java.lang.Object, org.springframework.data.domain.Sort)
*/
@Override
protected Void complete(CriteriaSpec criteria, Sort sort) {
protected Void complete(Criteria criteria, Sort sort) {
Query query = criteria.build();
@@ -136,14 +136,14 @@ class MongoQueryCreator extends AbstractQueryCreator<Void, CriteriaSpec> {
/**
* Populates the given {@link Criteria} depending on the {@link Type} given.
* Populates the given {@link CriteriaDefinition} depending on the {@link Type} given.
*
* @param type
* @param criteria
* @param parameters
* @return
*/
private CriteriaSpec from(Type type, CriteriaSpec criteria,
private Criteria from(Type type, Criteria criteria,
BindableParameterIterator parameters) {
switch (type) {

View File

@@ -15,7 +15,7 @@
*/
package org.springframework.data.document.mongodb.repository;
import org.springframework.data.document.mongodb.builder.QuerySpec;
import org.springframework.data.document.mongodb.builder.Query;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
@@ -36,14 +36,14 @@ abstract class QueryUtils {
/**
* Applies the given {@link Pageable} to the given {@link QuerySpec}. Will
* Applies the given {@link Pageable} to the given {@link Query}. Will
* do nothing if {@link Pageable} is {@literal null}.
*
* @param spec
* @param pageable
* @return
*/
public static QuerySpec applyPagination(QuerySpec spec, Pageable pageable) {
public static Query applyPagination(Query spec, Pageable pageable) {
if (pageable == null) {
return spec;
@@ -57,14 +57,14 @@ abstract class QueryUtils {
/**
* Applies the given {@link Sort} to the {@link QuerySpec}. Will do nothing
* Applies the given {@link Sort} to the {@link Query}. Will do nothing
* if {@link Sort} is {@literal null}.
*
* @param spec
* @param sort
* @return
*/
public static QuerySpec applySorting(QuerySpec spec, Sort sort) {
public static Query applySorting(Query spec, Sort sort) {
if (sort == null) {
return spec;

View File

@@ -22,7 +22,7 @@ import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.data.document.mongodb.MongoConverter;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.builder.QuerySpec;
import org.springframework.data.document.mongodb.builder.Query;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
@@ -109,8 +109,8 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
ObjectId objectId = converter.convertObjectId(id);
List<T> result =
template.query(
new QuerySpec().find("_id").is(objectId).build(),
template.find(
new Query().find("_id").is(objectId).build(),
getDomainClass());
return result.isEmpty() ? null : result.get(0);
}
@@ -202,10 +202,10 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
public Page<T> findAll(final Pageable pageable) {
Long count = count();
QuerySpec spec = new QuerySpec();
Query spec = new Query();
List<T> list =
template.query(QueryUtils.applyPagination(spec, pageable),
template.find(QueryUtils.applyPagination(spec, pageable),
getDomainClass());
return new PageImpl<T>(list, pageable, count);
@@ -221,8 +221,8 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/
public List<T> findAll(final Sort sort) {
QuerySpec query = QueryUtils.applySorting(new QuerySpec(), sort);
return template.query(query, getDomainClass());
Query query = QueryUtils.applySorting(new Query(), sort);
return template.find(query, getDomainClass());
}

View File

@@ -24,6 +24,7 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.document.mongodb.builder.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -54,7 +55,7 @@ public class MongoTemplateTests {
MongoConverter converter = template.getConverter();
List<Person> result = template.find(QueryBuilder.start("_id").is(converter.convertObjectId(person.getId())).get(), Person.class);
List<Person> result = template.find(Query.newQuery("_id").is(converter.convertObjectId(person.getId())).build(), Person.class);
assertThat(result.size(), is(1));
assertThat(result, hasItem(person));
}

View File

@@ -88,7 +88,7 @@ public class MvcAnalyticsTests {
for (DBObject dbo : mongoTemplate.getCollection("counters").find(query)) {
System.out.println(dbo);
}
List<ControllerCounter> counters = mongoTemplate.query("counters", new BasicQuery("{ 'name' : 'SignUpController'} "), ControllerCounter.class);
List<ControllerCounter> counters = mongoTemplate.find("counters", new BasicQuery("{ 'name' : 'SignUpController'} "), ControllerCounter.class);
for (ControllerCounter controllerCounter : counters) {
System.out.println(controllerCounter);
}

View File

@@ -22,7 +22,7 @@ public class QueryTests {
@Test
public void testSimpleQuery() {
QuerySpec q = new QuerySpec();
Query q = new Query();
q.find("name").is("Thomas");
q.find("age").lt(80);
String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$lt\" : 80}}";
@@ -31,7 +31,7 @@ public class QueryTests {
@Test
public void testQueryWithNot() {
QuerySpec q = new QuerySpec();
Query q = new Query();
q.find("name").is("Thomas");
q.find("age").not().mod(10, 0);
String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$not\" : { \"$mod\" : [ 10 , 0]}}}";
@@ -40,10 +40,10 @@ public class QueryTests {
@Test
public void testOrQuery() {
QuerySpec q = new QuerySpec();;
Query q = new Query();;
q.or(
new QuerySpec().find("name").is("Sven").and("age").lt(50).build(),
new QuerySpec().find("age").lt(50).build(),
new Query().find("name").is("Sven").and("age").lt(50).build(),
new Query().find("age").lt(50).build(),
new BasicQuery("{'name' : 'Thomas'}")
);
String expected = "{ \"$or\" : [ { \"name\" : \"Sven\" , \"age\" : { \"$lt\" : 50}} , { \"age\" : { \"$lt\" : 50}} , { \"name\" : \"Thomas\"}]}";
@@ -52,7 +52,7 @@ public class QueryTests {
@Test
public void testQueryWithLimit() {
QuerySpec q = new QuerySpec();
Query q = new Query();
q.find("name").gte("M").lte("T").and("age").not().gt(22);
q.limit(50);
String expected = "{ \"name\" : { \"$gte\" : \"M\" , \"$lte\" : \"T\"} , \"age\" : { \"$not\" : { \"$gt\" : 22}}}";
@@ -62,7 +62,7 @@ public class QueryTests {
@Test
public void testQueryWithFieldsAndSlice() {
QuerySpec q = new QuerySpec();
Query q = new Query();
q.find("name").gte("M").lte("T").and("age").not().gt(22);
q.fields().exclude("address").include("name").slice("orders", 10);

View File

@@ -17,20 +17,20 @@ package org.springframework.data.document.mongodb.builder;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.data.document.mongodb.builder.SortSpec.SortOrder;
import org.springframework.data.document.mongodb.builder.Sort.Order;
public class SortTests {
@Test
public void testWithSortAscending() {
SortSpec s = new SortSpec().on("name", SortOrder.ASCENDING);
Assert.assertEquals("{ \"name\" : 1}", s.build().getSortObject().toString());
Sort s = new Sort().on("name", Order.ASCENDING);
Assert.assertEquals("{ \"name\" : 1}", s.getSortObject().toString());
}
@Test
public void testWithSortDescending() {
SortSpec s = new SortSpec().on("name", SortOrder.DESCENDING);
Assert.assertEquals("{ \"name\" : -1}", s.build().getSortObject().toString());
Sort s = new Sort().on("name", Order.DESCENDING);
Assert.assertEquals("{ \"name\" : -1}", s.getSortObject().toString());
}
}

View File

@@ -20,28 +20,28 @@ import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.data.document.mongodb.builder.SortSpec.SortOrder;
import org.springframework.data.document.mongodb.builder.UpdateSpec;
import org.springframework.data.document.mongodb.builder.Sort.Order;
import org.springframework.data.document.mongodb.builder.Update;
public class UpdateTests {
@Test
public void testSet() {
UpdateSpec u = new UpdateSpec()
Update u = new Update()
.set("directory", "/Users/Test/Desktop");
Assert.assertEquals("{ \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", u.build().getUpdateObject().toString());
}
@Test
public void testInc() {
UpdateSpec u = new UpdateSpec()
Update u = new Update()
.inc("size", 1);
Assert.assertEquals("{ \"$inc\" : { \"size\" : 1}}", u.build().getUpdateObject().toString());
}
@Test
public void testIncAndSet() {
UpdateSpec u = new UpdateSpec()
Update u = new Update()
.inc("size", 1)
.set("directory", "/Users/Test/Desktop");
Assert.assertEquals("{ \"$inc\" : { \"size\" : 1} , \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}",
@@ -50,7 +50,7 @@ public class UpdateTests {
@Test
public void testUnset() {
UpdateSpec u = new UpdateSpec()
Update u = new Update()
.unset("directory");
Assert.assertEquals("{ \"$unset\" : { \"directory\" : 1}}", u.build().getUpdateObject().toString());
}
@@ -59,7 +59,7 @@ public class UpdateTests {
public void testPush() {
Map<String, Object> m = new HashMap<String, Object>();
m.put("name", "Sven");
UpdateSpec u = new UpdateSpec()
Update u = new Update()
.push("authors", m);
Assert.assertEquals("{ \"$push\" : { \"authors\" : { \"name\" : \"Sven\"}}}", u.build().getUpdateObject().toString());
}
@@ -70,7 +70,7 @@ public class UpdateTests {
m1.put("name", "Sven");
Map<String, Object> m2 = new HashMap<String, Object>();
m2.put("name", "Maria");
UpdateSpec u = new UpdateSpec()
Update u = new Update()
.pushAll("authors", new Object[] {m1, m2});
Assert.assertEquals("{ \"$pushAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}", u.build().getUpdateObject().toString());
}
@@ -79,18 +79,18 @@ public class UpdateTests {
public void testAddToSet() {
Map<String, Object> m = new HashMap<String, Object>();
m.put("name", "Sven");
UpdateSpec u = new UpdateSpec()
Update u = new Update()
.addToSet("authors", m);
Assert.assertEquals("{ \"$addToSet\" : { \"authors\" : { \"name\" : \"Sven\"}}}", u.build().getUpdateObject().toString());
}
@Test
public void testPop() {
UpdateSpec u = new UpdateSpec()
.pop("authors", UpdateSpec.Position.FIRST);
Update u = new Update()
.pop("authors", Update.Position.FIRST);
Assert.assertEquals("{ \"$pop\" : { \"authors\" : -1}}", u.build().getUpdateObject().toString());
u = new UpdateSpec()
.pop("authors", UpdateSpec.Position.LAST);
u = new Update()
.pop("authors", Update.Position.LAST);
Assert.assertEquals("{ \"$pop\" : { \"authors\" : 1}}", u.build().getUpdateObject().toString());
}
@@ -98,7 +98,7 @@ public class UpdateTests {
public void testPull() {
Map<String, Object> m = new HashMap<String, Object>();
m.put("name", "Sven");
UpdateSpec u = new UpdateSpec()
Update u = new Update()
.pull("authors", m);
Assert.assertEquals("{ \"$pull\" : { \"authors\" : { \"name\" : \"Sven\"}}}", u.build().getUpdateObject().toString());
}
@@ -109,14 +109,14 @@ public class UpdateTests {
m1.put("name", "Sven");
Map<String, Object> m2 = new HashMap<String, Object>();
m2.put("name", "Maria");
UpdateSpec u = new UpdateSpec()
Update u = new Update()
.pullAll("authors", new Object[] {m1, m2});
Assert.assertEquals("{ \"$pullAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}", u.build().getUpdateObject().toString());
}
@Test
public void testRename() {
UpdateSpec u = new UpdateSpec()
Update u = new Update()
.rename("directory", "folder");
Assert.assertEquals("{ \"$rename\" : { \"directory\" : \"folder\"}}", u.build().getUpdateObject().toString());
}

View File

@@ -25,7 +25,7 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.document.mongodb.MongoConverter;
import org.springframework.data.document.mongodb.Person;
import org.springframework.data.document.mongodb.builder.QuerySpec;
import org.springframework.data.document.mongodb.builder.Query;
import org.springframework.data.repository.query.Parameters;
import org.springframework.data.repository.query.SimpleParameterAccessor;
import org.springframework.data.repository.query.parser.PartTree;
@@ -64,7 +64,7 @@ public class MongoQueryCreatorUnitTests {
PartTree tree = new PartTree("findByFirstName", Person.class);
MongoQueryCreator creator =
new MongoQueryCreator(new QuerySpec(), tree,
new MongoQueryCreator(new Query(), tree,
new SimpleParameterAccessor(new Parameters(
findByFirstname), new Object[] { "Oliver" }),
converter);
@@ -72,7 +72,7 @@ public class MongoQueryCreatorUnitTests {
creator.createQuery();
creator =
new MongoQueryCreator(new QuerySpec(), new PartTree(
new MongoQueryCreator(new Query(), new PartTree(
"findByFirstNameAndFriend", Person.class),
new SimpleParameterAccessor(new Parameters(
findByFirstnameAndFriend), new Object[] {