From cd4b409bf2bcfc0f63acfcb3e3f05f3e219201f1 Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Wed, 12 Oct 2011 04:10:52 -0400 Subject: [PATCH] DATADOC-283 - Adding query support for $and operator. --- .../data/mongodb/core/query/AndCriteria.java | 51 +++++++++++++++++++ .../data/mongodb/core/query/AndQuery.java | 24 +++++++++ .../data/mongodb/core/query/OrQuery.java | 15 ++++++ .../data/mongodb/core/query/Query.java | 5 ++ .../data/mongodb/core/query/QueryTests.java | 7 +++ 5 files changed, 102 insertions(+) create mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/AndCriteria.java create mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/AndQuery.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/AndCriteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/AndCriteria.java new file mode 100644 index 000000000..b5653909f --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/AndCriteria.java @@ -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"; + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/AndQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/AndQuery.java new file mode 100644 index 000000000..039594159 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/AndQuery.java @@ -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); + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/OrQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/OrQuery.java index 3ea9adc66..dccf6ac73 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/OrQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/OrQuery.java @@ -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 { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java index de7f390bb..196b9160d 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java @@ -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; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java index 0a3a18a34..01331fcdf 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java @@ -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(