Skip to content

Commit

Permalink
Added the overloaded where method to Untyped.
Browse files Browse the repository at this point in the history
See the updated guide/criteria.md for more details.
  • Loading branch information
osxhacker authored and fehmicansaglam committed Jul 7, 2014
1 parent 874f18a commit f1d475e
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 0 deletions.
66 changes: 66 additions & 0 deletions bson/src/main/scala/dsl/criteria/Untyped.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,71 @@ object Untyped {
* The criteria property is a ''factory'' of '''Untyped''' instances.
*/
val criteria = new Untyped {};

def where(block: (Untyped) => Expression): Expression =
block(criteria);

def where(block: (Untyped, Untyped) => Expression): Expression =
block(criteria, criteria);

def where(block: (Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria);

def where(block: (Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped, Untyped) => Expression): Expression =
block(criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria, criteria);
}

140 changes: 140 additions & 0 deletions bson/src/test/scala/dsl/criteria/UntypedWhereSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright 2013 Steve Vickers
*
* 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.
*
* Created on: Jun 15, 2014
*/
package reactivemongo.extensions.dsl.criteria

import org.scalatest._
import org.scalatest.matchers._

import reactivemongo.bson._

/**
* The '''UntypedWhereSpec''' type verifies the behaviour expected of the
* `where` method in the [[reactivemongo.extensions.dsl.criteria.Untyped]]
* `type`.
*
* @author svickers
*
*/
class UntypedWhereSpec
extends FlatSpec
with Matchers {
/// Class Imports
import Untyped._

"An Untyped where" should "support 1 placeholder" in
{
val q = where {
_.a === 1
}

BSONDocument.pretty(q) shouldBe (
BSONDocument.pretty(
BSONDocument(
"a" -> BSONInteger(1)
)
)
);
}

it should "support 2 placeholders" in
{
val q = where {
_.a === 1 && _.b === 2
}

BSONDocument.pretty(q) shouldBe (
BSONDocument.pretty(
BSONDocument(
"$and" ->
BSONArray(
BSONDocument(
"a" -> BSONInteger(1)
),
BSONDocument(
"b" -> BSONInteger(2)
)
)
)
)
);
}

it should "support 3 placeholders" in
{
val q = where {
_.a === 1 && _.b === 2 && _.c === 3
}

BSONDocument.pretty(q) shouldBe (
BSONDocument.pretty(
BSONDocument(
"$and" ->
BSONArray(
BSONDocument(
"a" -> BSONInteger(1)
),
BSONDocument(
"b" -> BSONInteger(2)
),
BSONDocument(
"c" -> BSONInteger(3)
)
)
)
)
);
}

/// The library supports from 1 to 22 placeholders for the where method.
it should "support 22 placeholders" in
{
val q = where {
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0 &&
_.p === 0
}

BSONDocument.pretty(q) shouldBe (
BSONDocument.pretty(
BSONDocument(
"$and" ->
BSONArray(List.fill(22)(BSONDocument("p" -> BSONInteger(0))))
)
)
);
}
}
15 changes: 15 additions & 0 deletions guide/criteria.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ What the DSL *does* provide is the ablity to formulate queries thusly:
}
```

Another form which achieves the same result is to use one of the `where` methods available:

```scala
// Using one of the Untyped.where overloads
{
import Untyped._

val cursor = collection.find(
where (_.firstName === "Jack" && _.age >= 18)
).cursor[BSONDocument];
}
```

There are overloads for between 1 and 22 place holders using the `where` method. Should more than 22 be needed, then the 1 argument version should be used with a named parameter. This allows an infinite number of property constraints to be specified.

### Roadmap

This section details the functionality either currently or planned to be supported by ReactiveMongo-Criteria.
Expand Down

0 comments on commit f1d475e

Please sign in to comment.