Subject: | search_where with LIMIT |
I made a change to Class/DBI.pm that I find useful and think others may appriciate too. It adds an extra attribute to the ->search_where method allowing statments such as:
my @misc = CD::Music->search_where(
{ artist => [ 'Ozzy', 'Kelly' ],
status => { '!=', 'outdated' } },
{ order => "reldate DESC",
limit => 12 });
to return a maximum of 12 rows.
diff -Naur Class-DBI-AbstractSearch-0.03.orig/lib/Class/DBI/AbstractSearch.pm Class-DBI-AbstractSearch-0.03/lib/Class/DBI/AbstractSearch.pm
--- Class-DBI-AbstractSearch-0.03.orig/lib/Class/DBI/AbstractSearch.pm 2003-07-30 04:49:49.000000000 -0700
+++ Class-DBI-AbstractSearch-0.03/lib/Class/DBI/AbstractSearch.pm 2004-01-29 12:26:10.000000000 -0800
@@ -15,6 +15,7 @@
my $where = (ref $_[0]) ? $_[0] : { @_ };
my $attr = (ref $_[0]) ? $_[1] : undef;
my $order = ($attr) ? $attr->{order} : undef;
+ my $limit = ($attr) ? exists $attr->{limit} : undef;
$class->can('retrieve_from_sql') or do {
require Carp;
@@ -23,6 +24,7 @@
my $sql = SQL::Abstract->new; # XXX how do we supply options here?
my($where_sql, @bind) = $sql->where($where,$order);
$where_sql =~ s/^\s*WHERE\s*//i;
+ $where_sql =~ s/$/ LIMIT $limit/ if $limit;
return $class->retrieve_from_sql($where_sql, @bind);
}