Permalink
Browse files

Merge pull request #71 from ktoso/master

Resolves #69 - in which or(where, whereOpt(None)) creates a bad query
  • Loading branch information...
2 parents 1fb8635 + b979c66 commit 3b6c91ec4f139ad7568d829fabf8efa309cce2fe @jliszka jliszka committed Dec 26, 2012
View
4 rogue-core/src/main/scala/com/foursquare/rogue/MongoHelpers.scala
@@ -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])
View
1 rogue-core/src/main/scala/com/foursquare/rogue/QueryHelpers.scala
@@ -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
}
View
11 rogue-lift/src/test/scala/com/foursquare/rogue/QueryTest.scala
@@ -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)

0 comments on commit 3b6c91e

Please sign in to comment.