| Class-DBI-AbstractSearch documentation | Contained in the Class-DBI-AbstractSearch distribution. |
Class::DBI::AbstractSearch - Abstract Class::DBI's SQL with SQL::Abstract::Limit
package CD::Music;
use Class::DBI::AbstractSearch;
package main;
my @music = CD::Music->search_where(
artist => [ 'Ozzy', 'Kelly' ],
status => { '!=', 'outdated' },
);
my @misc = CD::Music->search_where(
{ artist => [ 'Ozzy', 'Kelly' ],
status => { '!=', 'outdated' } },
{ order_by => "reldate DESC",
limit_dialect => 'LimitOffset',
limit => 1
offset => 2 });
Class::DBI::AbstractSearch is a Class::DBI plugin to glue SQL::Abstract::Limit into Class::DBI.
Using this module adds following methods into your data class.
$class->search_where(%where);
Takes a hash to specify WHERE clause. See SQL::Abstract for hash options.
$class->search_where(\%where,\%attrs);
Takes hash reference to specify WHERE clause. See SQL::Abstract for hash options. Takes a hash reference to specify additional query attributes. Class::DBI::AbstractSearch uses these attributes:
Any other attributes are passed to the SQL::Abstract::Limit constructor, and can be used to control how queries are created. For example, to use 'AND' instead of 'OR' by default, use:
$class->search_where(\%where, { logic => 'AND' });
Tatsuhiko Miyagawa <miyagawa@bulknews.net> with some help from cdbi-talk mailing list, especially:
Tim Bunce Simon Wilcox Tony Bowden
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Class-DBI-AbstractSearch documentation | Contained in the Class-DBI-AbstractSearch distribution. |
package Class::DBI::AbstractSearch; use strict; use vars qw($VERSION @EXPORT); $VERSION = 0.07; require Exporter; *import = \&Exporter::import; @EXPORT = qw(search_where); use SQL::Abstract::Limit; sub search_where { my $class = shift; my $where = (ref $_[0]) ? $_[0] : { @_ }; my $attr = (ref $_[0]) ? $_[1] : undef; my $order = ($attr) ? delete($attr->{order_by}) : undef; my $limit = ($attr) ? delete($attr->{limit}) : undef; my $offset = ($attr) ? delete($attr->{offset}) : undef; # order is deprecated, but still backward compatible if ($attr && exists($attr->{order})) { $order = delete($attr->{order}); } $class->can('retrieve_from_sql') or do { require Carp; Carp::croak("$class should inherit from Class::DBI >= 0.90"); }; my $sql = SQL::Abstract::Limit->new(%$attr); my($phrase, @bind) = $sql->where($where, $order, $limit, $offset); $phrase =~ s/^\s*WHERE\s*//i; return $class->retrieve_from_sql($phrase, @bind); } 1; __END__