On Mar 5, 6:43 am, Yusuke ENDOH <[email protected]> wrote:
> Hi,
>
> 2009/3/5 Jeremy Evans <[email protected]>:
>
> >> I think that it's convenience to handle it as `no entry is rejected.'
> (snip)
> > I'm open to this idea.  Implementation might be difficult without
> > setting up a special case, though, which I try to avoid.  If you want
> > to look at the code for Dataset#filter and Dataset#filter_expr, and
> > see if you can think of an elegant way to do what you want, that would
> > be nice.  Otherwise, a simple patch that just returns an unmodified
> > clone in case an empty hash or array is used should be fine.
>
> Thank you for your consideration.  How about this?
>
> irb(main):001:0> DB[:foo].where({}).sql
> => "SELECT * FROM `foo` WHERE 1"
> irb(main):002:0> DB[:foo].where(~{}).sql
> => "SELECT * FROM `foo` WHERE 0"

I don't like this implementation.  Allowing the boolean AND and OR
operators to take no arguments makes no sense.  Your implementation
doesn't work on databases that expect a boolean value in the WHERE
clause and don't cast integers to booleans (such as PostgreSQL), but
that would be easily fixable if I thought this was a good idea in
general.

My original philosophy was that {} specifies no conditions, not a
condition that is always true, so it's not a valid expression, and you
can't invert {} via ~ because there is nothing to invert.  However, I
can see where that is limiting depending on what you want to do.  One
solution is to have BooleanExpression.from_value_pairs simply return
true for empty conditions, have BooleanExpression.invert return false
for true and vice versa, and add TrueClass#~ that returns false and
FalseClass#~ that returns true.

I should point out that you can rewrite the original code to:

  table = table.clone
  table.filter!(:column1=>:foo) if column1_must_be_foo
  table.filter!(:column2=>:bar) if column2_must_be_bar

That's how I generally deal with such situations now.  The original
code's way is obviously cleaner if you have lots of possible
conditions.

Jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to