From: Matt S Trout Date: Fri, 15 Nov 2013 08:46:39 +0000 (+0000) Subject: allow DQ expr {} blocks as first arg to search X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8a3a5bc5a44ae69c66b2aa5e0388ddb9aea98379;p=dbsrgits%2FDBIx-Class.git allow DQ expr {} blocks as first arg to search --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index e24544c..1e73052 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -398,6 +398,10 @@ sub search_rs { $call_cond = { @_ }; } + if (blessed($call_cond) and $call_cond->isa('Data::Query::ExprBuilder')) { + $call_cond = \$call_cond->{expr}; + } + # see if we can keep the cache (no $rs changes) my $cache; my %safe = (alias => 1, cache => 1); diff --git a/t/dq/where.t b/t/dq/where.t new file mode 100644 index 0000000..fc33592 --- /dev/null +++ b/t/dq/where.t @@ -0,0 +1,37 @@ +use strict; +use warnings; + +use Test::More; +use Test::Exception; +use Test::Warn; +use lib qw(t/lib); +use DBICTest; +use DBIC::SqlMakerTest; +use Data::Query::ExprDeclare; + +my $schema = DBICTest->init_schema(); + +my $mccrae = $schema->resultset('Artist') + ->find({ name => 'Caterwauler McCrae' }); + +my @cds = $schema->resultset('CD') + ->search(expr { $_->artist == $mccrae->artistid }); + +is(@cds, 3, 'CDs returned from expr search by artistid'); + +my @years = $schema->resultset('CD') + ->search(expr { $_->year < 2000 }) + ->get_column('year') + ->all; + +is_deeply([ sort @years ], [ 1997, 1998, 1999 ], 'Years for < search'); + +my $tag_cond = expr { $_->tag eq 'Blue' }; + +is($schema->resultset('Tag')->search($tag_cond)->count, 4, 'Simple tag cond'); + +$tag_cond &= expr { $_->cd < 4 }; + +is($schema->resultset('Tag')->search($tag_cond)->count, 3, 'Combi tag cond'); + +done_testing;