com.googlecode.mjorm.query.DaoQuery Maven / Gradle / Ivy
package com.googlecode.mjorm.query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.googlecode.mjorm.MongoDao;
import com.googlecode.mjorm.ObjectIterator;
import com.googlecode.mjorm.ObjectMapper;
import com.googlecode.mjorm.query.criteria.AbstractQueryCriterion;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.ReadPreference;
public class DaoQuery
extends AbstractQueryCriterion {
private DB db;
private ObjectMapper objectMapper;
private Map sort;
private Map specials;
private Integer firstDocument;
private Integer maxDocuments;
private Integer batchSize;
private DBObject hint;
private Boolean snapShot;
private String comment;
private String collection;
private ReadPreference readPreference;
private CursorVisitor cursorVisitor;
private DaoModifier modifier;
/**
* Allows for the visiting of the {@link DBCursor}
* before it is returned as an {@link ObjectIterator}
* form the various query methods of this class.
*/
public static interface CursorVisitor {
void visit(DBCursor cursor);
}
/**
* Creates the {@link DaoQuery}.
*/
public DaoQuery() {
this.clear();
}
/**
* Clears the query.
*/
public void clear() {
super.clear();
if (modifier!=null) {
modifier.clear();
}
sort = new HashMap();
specials = new HashMap();
firstDocument = null;
maxDocuments = null;
batchSize = null;
hint = null;
snapShot = null;
comment = null;
collection = null;
cursorVisitor = null;
modifier = null;
}
/**
* Asserts that the {@link DaoQuery} is valid.
* Throws an exception if not.
*/
public void assertValid() {
if (collection==null) {
throw new IllegalStateException("collection must be specified");
} else if (db==null) {
throw new IllegalStateException("DB must be specified");
}
}
/**
* Creates a {@link DaoModifier} for the
* current query.
* @return the {@link DaoModifier}
*/
public DaoModifier modify() {
if (modifier==null) {
modifier = new DaoModifier(this);
}
return modifier;
}
/**
* Executes the query and returns objects of the given type.
* @param clazz the type of objects to return
* @return the iterator.
*/
public ObjectIterator findObjects(Class clazz) {
assertValid();
DBCursor cursor = db.getCollection(collection).find(toQueryObject(objectMapper));
setupCursor(cursor);
return new ObjectIterator(cursor, objectMapper, clazz);
}
/**
* Executes the query and returns objects of the given type.
* @param fields the fields to return
* @return the iterator.
*/
public DBCursor findObjects(DBObject fields) {
assertValid();
DBCursor cursor = db.getCollection(collection).find(toQueryObject(objectMapper), fields);
setupCursor(cursor);
return cursor;
}
/**
* Executes the query and returns objects of the given type.
* @param clazz the type of objects to return
* @return the iterator.
*/
public DBCursor findObjects() {
assertValid();
DBCursor cursor = db.getCollection(collection).find(toQueryObject(objectMapper));
setupCursor(cursor);
return cursor;
}
/**
* Executes the query and returns objects of the given type.
* @param fields the type of objects to return
* @return the iterator.
*/
public DBCursor findObjects(String... fields) {
DBObject dbObject = new BasicDBObject();
for (String field : fields) {
dbObject.put(field, 1);
}
return findObjects(dbObject);
}
/**
* Executes the query and returns an object of the given type.
* @param clazz the type of object to return
* @return the object.
*/
public T findObject(Class clazz) {
assertValid();
ObjectIterator itr = findObjects(clazz);
return itr.hasNext() ? itr.next() : null;
}
/**
* Executes the query and returns objects of the given type.
* @param clazz the type of objects to return
* @return the iterator.
*/
public DBObject findObject(DBObject fields) {
assertValid();
DBCursor itr = findObjects(fields);
return itr.hasNext() ? itr.next() : null;
}
/**
* Executes the query and returns objects of the given type.
* @param clazz the type of objects to return
* @return the iterator.
*/
public DBObject findObject() {
assertValid();
DBCursor itr = findObjects();
return itr.hasNext() ? itr.next() : null;
}
/**
* Executes the query and returns the number of objects
* that it would return.
* @return the count
*/
public long countObjects() {
assertValid();
return (readPreference!=null)
? db.getCollection(collection).count(toQueryObject(objectMapper), readPreference)
: db.getCollection(collection).count(toQueryObject(objectMapper));
}
/**
* Returns distinct values for the given field. This field
* passed must be the name of a field on a MongoDB document.
* @param field the field
* @return the distinct objects
*/
@SuppressWarnings("unchecked")
public List