DATADOC-283 - Adding query support for $and operator.
This commit is contained in:
committed by
Oliver Gierke
parent
f71477f17d
commit
cd4b409bf2
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.mongodb.core.query;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
import org.bson.types.BasicBSONList;
|
||||
|
||||
public class AndCriteria implements CriteriaDefinition {
|
||||
|
||||
Query[] queries = null;
|
||||
|
||||
public AndCriteria(Query[] queries) {
|
||||
super();
|
||||
this.queries = queries == null ? null : queries.clone();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.datastore.document.mongodb.query.Criteria#
|
||||
* getCriteriaObject(java.lang.String)
|
||||
*/
|
||||
public DBObject getCriteriaObject() {
|
||||
DBObject dbo = new BasicDBObject();
|
||||
BasicBSONList l = new BasicBSONList();
|
||||
for (Query q : queries) {
|
||||
l.add(q.getQueryObject());
|
||||
}
|
||||
dbo.put(getOperator(), l);
|
||||
return dbo;
|
||||
}
|
||||
|
||||
protected String getOperator() {
|
||||
return "$and";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.mongodb.core.query;
|
||||
|
||||
public class AndQuery extends Query {
|
||||
|
||||
public AndQuery(Query... q) {
|
||||
super.and(q);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* 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.mongodb.core.query;
|
||||
|
||||
public class OrQuery extends Query {
|
||||
|
||||
@@ -50,6 +50,11 @@ public class Query {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Query and(Query... queries) {
|
||||
this.criteria.put("$and", new AndCriteria(queries));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Query or(Query... queries) {
|
||||
this.criteria.put("$or", new OrCriteria(queries));
|
||||
return this;
|
||||
|
||||
@@ -58,6 +58,13 @@ public class QueryTests {
|
||||
Assert.assertEquals(expected, q.getQueryObject().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAndQuery() {
|
||||
Query q = new AndQuery(new Query(where("name").is("Sven")), new Query(where("age").lt(50)));
|
||||
String expected = "{ \"$and\" : [ { \"name\" : \"Sven\"} , { \"age\" : { \"$lt\" : 50}}]}";
|
||||
Assert.assertEquals(expected, q.getQueryObject().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNorQuery() {
|
||||
Query q = new NorQuery(new Query(where("name").is("Sven")), new Query(where("age").lt(50)), new BasicQuery(
|
||||
|
||||
Reference in New Issue
Block a user