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

Jesse Vincent jesse at bestpractical.com
Tue Dec 5 09:39:40 EST 2006




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');


$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');


When you need to mix and match columns yourself, you can use an explicit 'subclause' 
$collection->limit(subclause => 'myclause', column =>  'col1', value => 'x', entry_aggregator => 'OR');
$collection->limit(subclause => 'myclause', column =>  'col2', value => 'y', entry_aggregator => 'OR');

	Will generate:

	WHERE ( col1 = 'x' OR col2 = 'y');

Does that help? Are you up for adding it to the docs? Thanks!


> sub limit {
>    my $self = shift;
>    my %args = (
>        table            => $self->table,
>        column           => undef,
>        value            => undef,
>        alias            => undef,
>        quote_value      => 1,
>        entry_aggregator => 'or',
>        case_sensitive   => undef,
>        operator         => '=',
>        subclause        => undef,
>        leftjoin         => undef,
>        @_    # get the real argumentlist
>    );
>    ....
> 
> Apparently entry_aggregator defaults to "OR" according to the 
> implementation.
> 
> Then which one is incorrect? The implementation or the documentation?
> 
> Thanks,
> Agent
> _______________________________________________
> jifty-devel mailing list
> jifty-devel at lists.jifty.org
> http://lists.jifty.org/cgi-bin/mailman/listinfo/jifty-devel
> 

-- 


More information about the jifty-devel mailing list