[jifty-devel] Clauses are AND'd by default in the limit method?

Jesse Vincent jesse at bestpractical.com
Tue Dec 5 17:07:34 EST 2006


On Dec 5, 2006, at 2:40 PM, Ruslan Zakirov wrote:

> On 12/5/06, Jesse Vincent <jesse at bestpractical.com> wrote:
>>
>>
>>
>> On Tue, Dec 05, 2006 at 04:01:04PM +0800, Agent Zhang wrote:
>> > Hi,
>> >
>> > The Jifty::Manual::Models document contains the following snippet:
>> >
>> >    # combining restrictions with "AND"
>> >    # note that "AND" is implicit here
>> >    $collection->limit(column=>'col1', value=>'...');
>> >    $collection->limit(column=>'col2', value=>'...');
>> >
>> > While testing my Qooqle app in Jifty, however, I found "OR" is
>> > implicit here instead. By further examining the source of
>> > Jifty::DBI::Collection, I've seen the following lines:
>>
>>
>> Here's the way it works:
>>
>>
>> * Toplevel clauses default to AND
>> * Subclauses default to OR (though at one point, they defaulted to
>>   whatever made sense. You can make decent guesses about what the  
>> user
>>   means...except that you're often wrong);
>> * By default, each column gets its own subclauses.
>>
>> So:
>>
>>
>> $collection->limit(column =>  'col1', value => 'x');
>> $collection->limit(column =>  'col1', value => 'y');
>>
>>         Will generate :
>>
>>         WHERE ( col1 = 'x' OR col2 = 'y');
>          WHERE ( col1 = 'x' AND col2 = 'y');
>
How so? My reading of sub limit says this:


sub limit {
     my $self = shift;
     my %args = (

	...
         entry_aggregator => 'or',
         ...

  	 @_    # get the real argumentlist
     );




>>
>>
>> $collection->limit(column =>  'col1', value => 'x');
>> $collection->limit(column =>  'col2', value => 'y');
>>
>>         Will generate:
>>
>>         WHERE ( col1 = 'x' ) AND  ( col2 = 'y');
>>
>> $collection->limit(column =>  'col1', value => 'x');
>> $collection->limit(column =>  'col2', value => 'x');
>> $collection->limit(column =>  'col2', value => 'y');
>>
>>         Will generate:
>>
>>         WHERE ( col1 = 'x' ) AND  ( col2 = 'x' OR col2 = 'y');
>>
>>
>>
>> You can get tricky by playing with the entry aggregator yourself:
>>
>> $collection->limit(column =>  'col1', value => 'x');
>> $collection->limit(column =>  'col2', value => 'x',  
>> entry_aggregator => 'AND');
>> $collection->limit(column =>  'col2', value => 'y',  
>> entry_aggregator => 'AND');
>>
>>         Will generate:
>>
>>         WHERE ( col1 = 'x' ) AND  ( col2 = 'x' OR col2 = 'y');
>          WHERE ( col1 = 'x' ) AND  ( col2 = 'x' AND col2 = 'y');


Thanks. Nice catch.



More information about the jifty-devel mailing list