Skip to content

Commit

Permalink
Merge pull request #71 from ktoso/master
Browse files Browse the repository at this point in the history
Resolves #69 - in which or(where, whereOpt(None)) creates a bad query
  • Loading branch information
jliszka committed Dec 26, 2012
2 parents 1fb8635 + b979c66 commit 3b6c91e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import com.mongodb.{BasicDBObjectBuilder, DBObject}
import scala.collection.immutable.ListMap

object MongoHelpers extends Rogue {
case class AndCondition(clauses: List[QueryClause[_]], orCondition: Option[OrCondition])
case class AndCondition(clauses: List[QueryClause[_]], orCondition: Option[OrCondition]) {
def isEmpty: Boolean = clauses.isEmpty && orCondition.isEmpty
}

case class OrCondition(conditions: List[AndCondition])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ object QueryHelpers {
def orConditionFromQueries(subqueries: List[Query[_, _, _]]) = {
MongoHelpers.OrCondition(subqueries.flatMap(subquery => {
subquery match {
case q: Query[_, _, _] if q.condition.isEmpty => None
case q: Query[_, _, _] => Some(q.condition)
case _ => None
}
Expand Down
11 changes: 11 additions & 0 deletions rogue-lift/src/test/scala/com/foursquare/rogue/QueryTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,17 @@ class QueryTest extends SpecsMatchers {
_.where(_.mayor eqs 2))
.modify(_.userid setTo 1).toString() must_== """db.venues.update({ "$or" : [ { "legid" : 1} , { "mayor" : 2}]}, { "$set" : { "userid" : 1}}, false, false)"""

// $or with optional where clause
Venue.or(
_.where(_.legacyid eqs 1),
_.whereOpt(None)(_.mayor eqs _))
.modify(_.userid setTo 1).toString() must_== """db.venues.update({ "$or" : [ { "legid" : 1}]}, { "$set" : { "userid" : 1}}, false, false)"""

Venue.or(
_.where(_.legacyid eqs 1),
_.whereOpt(Some(2))(_.mayor eqs _))
.modify(_.userid setTo 1).toString() must_== """db.venues.update({ "$or" : [ { "legid" : 1} , { "mayor" : 2}]}, { "$set" : { "userid" : 1}}, false, false)"""

// OrQuery syntax
val q1 = Venue.where(_.legacyid eqs 1)
val q2 = Venue.where(_.legacyid eqs 2)
Expand Down

0 comments on commit 3b6c91e

Please sign in to comment.