[Dbix-class] Chained resultsets and WHERE operators

Mike Raynham catalyst at mikeraynham.co.uk
Wed Nov 10 05:34:03 GMT 2010


On 09/11/10 22:26, Peter Rabbitson wrote:
>
> -nest...? what...?
>
> I still don't understand how this is different from anything you've showed
> us in the thread so far. You said you want to do:
>
> WHERE
> ($start_date >= start_date AND $start_date <= end_date)
> OR
> ($end_date >= start_date AND $end_date <= end_date)
>
> In SQLA it is assumed that the lhs is a column and the rhs is a bind value
> (this is a *generalization*, there are all kinds of exceptions, but as a
> generalization it will suffice)
>
> So we rewrite this as:
> (start_date <= $start_date AND end_date >= $start_date)
> OR
> (start_date <= $end_date AND end_date >= $end_date)
>
> So then it boils down to:
> [
> { start_date => { '<=', $start_date }, end_date => { '>=', $start_date } },
> { start_date => { '<=', $end_date }, end_date => { '>=', $end_date } },
> ]
>
> That's it. No -nest, no braindamage.
>
> Cheers

Hi Peter,

Thank you for you input, and your solution.  I appear to have created 
some confusion, so please let me attempt to clarify things.

My original question was about chaining with an OR instead of an AND. 
You pointed out that:

 > Chaining implies AND ing (tightening the condition more
 > and more)

Ronald J Kimball provided an elegant solution which you kindly 
simplified.  I thanked you both for this, and said that I liked the 
solution and that I would use it.

I then mentioned that if I were to write the query manually, I would 
probably swap the column and bind values around.  This is simply because 
that way the meaning looks a little clearer to me.  I fully understand 
that in this case, it doesn't affect the query in any way.  I also 
pointed out that it would do exactly the same thing as the original query.

I didn't think that it was possible (at least not easily or sensibly) to 
swap the column and bind values around when generating a search with 
DBIx::Class::Resultset, and was perfectly happy to leave it at that. 
However, your response to this was:

 > I don't see why not... ? Explain what do you perceive the holdup would
 > be.

 From that, I assumed that maybe there is a way to easily swap the 
column and bind values around.  I checked the DBIx::Class::Manual::FAQ, 
and found a mention of -nest, which appeared to offer a way of doing it. 
  I didn't try it.  I was, and still am, happy to restructure my 
original query to incorporate your solution.




Regards,

Mike



More information about the DBIx-Class mailing list