allow DQ expr {} blocks as first arg to search
Matt S Trout [Fri, 15 Nov 2013 08:46:39 +0000 (08:46 +0000)]
lib/DBIx/Class/ResultSet.pm
t/dq/where.t [new file with mode: 0644]

index e24544c..1e73052 100644 (file)
@@ -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 (file)
index 0000000..fc33592
--- /dev/null
@@ -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;