DATADOC-72 renamed Query.and() to Query.addCriteria() to avoid confusion with Criteria.add() method that supports chained Criteria

This commit is contained in:
Thomas Risberg
2011-03-31 18:33:51 -04:00
parent f4f9eac03a
commit 2b9b082978
5 changed files with 36 additions and 18 deletions

View File

@@ -51,7 +51,7 @@ public class BasicQuery extends Query {
}
@Override
public Query and(Criteria criteria) {
public Query addCriteria(Criteria criteria) {
this.queryObject.putAll(criteria.getCriteriaObject());
return this;
}

View File

@@ -36,10 +36,10 @@ public class Query {
}
public Query(Criteria criteria) {
and(criteria);
addCriteria(criteria);
}
public Query and(Criteria criteria) {
public Query addCriteria(Criteria criteria) {
this.criteria.put(criteria.getKey(), criteria);
return this;
}

View File

@@ -82,7 +82,7 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Query> {
Criteria criteria = from(part.getType(), where(part.getProperty().toDotPath()),
iterator);
return base.and(criteria);
return base.addCriteria(criteria);
}

View File

@@ -24,14 +24,14 @@ public class QueryTests {
@Test
public void testSimpleQuery() {
Query q = new Query(where("name").is("Thomas")).and(where("age").lt(80));
Query q = new Query(where("name").is("Thomas").and("age").lt(80));
String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$lt\" : 80}}";
Assert.assertEquals(expected, q.getQueryObject().toString());
}
@Test
public void testQueryWithNot() {
Query q = new Query(where("name").is("Thomas")).and(where("age").not().mod(10, 0));
Query q = new Query(where("name").is("Thomas").and("age").not().mod(10, 0));
String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$not\" : { \"$mod\" : [ 10 , 0]}}}";
Assert.assertEquals(expected, q.getQueryObject().toString());
}
@@ -39,7 +39,7 @@ public class QueryTests {
@Test
public void testOrQuery() {
Query q = new OrQuery(
new Query(where("name").is("Sven")).and(where("age").lt(50)),
new Query(where("name").is("Sven").and("age").lt(50)),
new Query(where("age").lt(50)),
new BasicQuery("{'name' : 'Thomas'}")
);
@@ -49,7 +49,7 @@ public class QueryTests {
@Test
public void testQueryWithLimit() {
Query q = new Query(where("name").gte("M").lte("T")).and(where("age").not().gt(22));
Query q = new Query(where("name").gte("M").lte("T").and("age").not().gt(22));
q.limit(50);
String expected = "{ \"name\" : { \"$gte\" : \"M\" , \"$lte\" : \"T\"} , \"age\" : { \"$not\" : { \"$gt\" : 22}}}";
Assert.assertEquals(expected, q.getQueryObject().toString());
@@ -58,7 +58,7 @@ public class QueryTests {
@Test
public void testQueryWithFieldsAndSlice() {
Query q = new Query(where("name").gte("M").lte("T")).and(where("age").not().gt(22));
Query q = new Query(where("name").gte("M").lte("T").and("age").not().gt(22));
q.fields().exclude("address").include("name").slice("orders", 10);
String expected = "{ \"name\" : { \"$gte\" : \"M\" , \"$lte\" : \"T\"} , \"age\" : { \"$not\" : { \"$gt\" : 22}}}";
@@ -69,7 +69,7 @@ public class QueryTests {
@Test
public void testBasicQuery() {
Query q = new BasicQuery("{ \"name\" : \"Thomas\"}").and(where("age").lt(80));
Query q = new BasicQuery("{ \"name\" : \"Thomas\"}").addCriteria(where("age").lt(80));
String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$lt\" : 80}}";
Assert.assertEquals(expected, q.getQueryObject().toString());
}
@@ -83,10 +83,18 @@ public class QueryTests {
@Test
public void testComplexQueryWithMultipleChainedCriteria() {
Query q = new Query(where("name").regex("^T.*").and("age").gt(20).lt(80).and("city").in("Stockholm", "London", "New York"));
String expected = "{ \"name\" : { \"$regex\" : \"^T.*\"} , \"age\" : { \"$gt\" : 20 , \"$lt\" : 80} , " +
"\"city\" : { \"$in\" : [ \"Stockholm\" , \"London\" , \"New York\"]}}";
Assert.assertEquals(expected, q.getQueryObject().toString());
}
@Test
public void testAddCriteriaWithComplexQueryWithMultipleChainedCriteria() {
Query q1 = new Query(where("name").regex("^T.*").and("age").gt(20).lt(80).and("city").in("Stockholm", "London", "New York"));
Query q2 = new Query(where("name").regex("^T.*").and("age").gt(20).lt(80)).and(where("city").in("Stockholm", "London", "New York"));
Query q2 = new Query(where("name").regex("^T.*").and("age").gt(20).lt(80)).addCriteria(where("city").in("Stockholm", "London", "New York"));
Assert.assertEquals(q1.getQueryObject().toString(), q2.getQueryObject().toString());
Query q3 = new Query(where("name").regex("^T.*")).and(where("age").gt(20).lt(80)).and(where("city").in("Stockholm", "London", "New York"));
Query q3 = new Query(where("name").regex("^T.*")).addCriteria(where("age").gt(20).lt(80)).addCriteria(where("city").in("Stockholm", "London", "New York"));
Assert.assertEquals(q1.getQueryObject().toString(), q3.getQueryObject().toString());
}

View File

@@ -961,7 +961,7 @@
...
List&lt;Person&gt; result = mongoTemplate.find(
new Query(where("age").lt(50)).and(where("accounts.balance").gt(1000.00d)),
new Query(where("age").lt(50).and("accounts.balance").gt(1000.00d)),
Person.class);
</programlisting>
</example>
@@ -1076,10 +1076,12 @@
</listitem>
<listitem>
<para><literal>void</literal> <emphasis role="bold">or
</emphasis> <literal>(List&lt;Query&gt; queries)
</literal>Creates an or query using the <literal>$or</literal>
operator for all of the provided queries</para>
<para><literal>Criteria</literal> <emphasis role="bold">and
</emphasis> <literal>(String key) </literal>Adds a chained
<classname>Criteria</classname> with the specified
<literal>key</literal> to the current
<classname>Criteria</classname> and retuns the newly created
one</para>
<para />
</listitem>
@@ -1096,11 +1098,19 @@
<para>
<itemizedlist>
<listitem>
<para><literal>Query</literal> <emphasis role="bold">and
<para><literal>Query</literal> <emphasis role="bold">addCriteria
</emphasis> <literal>(Criteria criteria)</literal> used to add
additional criteria to the query</para>
</listitem>
<listitem>
<para><literal>void</literal> <emphasis
role="bold">or</emphasis> <literal>(List&lt;Query&gt;
queries)</literal> Creates an or query using the
<literal>$or</literal> operator for all of the provided
queries</para>
</listitem>
<listitem>
<para><literal>Field</literal> <emphasis role="bold">fields
</emphasis> <literal>()</literal> used to define fields to be