On Friday, February 8, 2019 at 1:55:10 PM UTC-8, marc halperin wrote:
>
> Currently if we do DB[:items].or(b: 10)  the sql generated would be SELECT 
> * FROM items which as the documentation says makes since because "the 
> default is WHERE true, and OR would be redundant,". 
>
> The issue with this though is if I want to chain a few or's where an or 
> will be added or not depending on options sent by the user. In this case I 
> have to find the first 
> one to be included and make it a where call then make all other calls or. 
> I think it can make sense to have DB[:items].or(b:10) return SELECT * 
> FROM items where b = 10 , 
> then I can write code like:
>
> db = DB[:items]
>
> # db gets manipulated by some function
> db = some_func_that_manipulates_db(db)
>
> # Now I can do this without worry about if db has already had a where 
> clause added
> db = db.or(b: var1) if var1
> db = db.or(c: var2) if var2
> etc.
>
> I can't think of a time that I call 'or' and wouldn't want my condition to 
> be applied even if there was no where clause initially added so I don't 
> think changing this would
> break peoples code but maybe I'm missing something.
>
> Looks like a simple change replacing sequel/query.rb:721 from 'self' to '
> where(cond, &block)'.
>

Dataset#or being a no-op if there is no existing condition is by design and 
I think the only sensible behavior. Dataset#or should never result in fewer 
rows being returned than a dataset before the #or call.

What you want is to potentially apply a filter of potentially multiple 
conditions ORed together. I think what you want is:

db = DB[:items]
db = some_func_that_manipulates_db(db)
conditions = []
conditions << {b: var1} if var1
conditions << {c: var2} if var2
db = db.where(Sequel.|(*conditions)) unless conditions.empty?

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to