← Index
NYTProf Performance Profile   « block view • line view • sub view »
For ddd2.pl
  Run on Tue May 25 16:52:24 2010
Reported on Tue May 25 16:57:06 2010

File /project/perl/lib/Class/DBI/AbstractSearch.pm
Statements Executed 14
Statement Execution Time 11.3ms
Subroutines — ordered by exclusive time
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
0000s0sClass::DBI::AbstractSearch::::BEGINClass::DBI::AbstractSearch::BEGIN
0000s0sClass::DBI::AbstractSearch::::search_whereClass::DBI::AbstractSearch::search_where
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Class::DBI::AbstractSearch;
2
3399µs128µsuse strict;
# spent 28µs making 1 call to strict::import
43157µs1225µsuse vars qw($VERSION @EXPORT);
# spent 225µs making 1 call to vars::import
516µs$VERSION = 0.07;
6
718µsrequire Exporter;
8110µs*import = \&Exporter::import;
916µs@EXPORT = qw(search_where);
10
11311.0msuse SQL::Abstract::Limit;
12
13sub search_where {
14 my $class = shift;
15 my $where = (ref $_[0]) ? $_[0] : { @_ };
16 my $attr = (ref $_[0]) ? $_[1] : undef;
17 my $order = ($attr) ? delete($attr->{order_by}) : undef;
18 my $limit = ($attr) ? delete($attr->{limit}) : undef;
19 my $offset = ($attr) ? delete($attr->{offset}) : undef;
20
21 # order is deprecated, but still backward compatible
22 if ($attr && exists($attr->{order})) {
23 $order = delete($attr->{order});
24 }
25
26 $class->can('retrieve_from_sql') or do {
27 require Carp;
28 Carp::croak("$class should inherit from Class::DBI >= 0.90");
29 };
30 my $sql = SQL::Abstract::Limit->new(%$attr);
31 my($phrase, @bind) = $sql->where($where, $order, $limit, $offset);
32 $phrase =~ s/^\s*WHERE\s*//i;
33 return $class->retrieve_from_sql($phrase, @bind);
34}
35
36114µs1;
37__END__
38
39=head1 NAME
40
41Class::DBI::AbstractSearch - Abstract Class::DBI's SQL with SQL::Abstract::Limit
42
43=head1 SYNOPSIS
44
45 package CD::Music;
46 use Class::DBI::AbstractSearch;
47
48 package main;
49 my @music = CD::Music->search_where(
50 artist => [ 'Ozzy', 'Kelly' ],
51 status => { '!=', 'outdated' },
52 );
53
54 my @misc = CD::Music->search_where(
55 { artist => [ 'Ozzy', 'Kelly' ],
56 status => { '!=', 'outdated' } },
57 { order_by => "reldate DESC",
58 limit_dialect => 'LimitOffset',
59 limit => 1
60 offset => 2 });
61
62=head1 DESCRIPTION
63
64Class::DBI::AbstractSearch is a Class::DBI plugin to glue
65SQL::Abstract::Limit into Class::DBI.
66
67=head1 METHODS
68
69Using this module adds following methods into your data class.
70
71=over 4
72
73=item search_where
74
75 $class->search_where(%where);
76
77Takes a hash to specify WHERE clause. See L<SQL::Abstract> for hash
78options.
79
80 $class->search_where(\%where,\%attrs);
81
82Takes hash reference to specify WHERE clause. See L<SQL::Abstract>
83for hash options. Takes a hash reference to specify additional query
84attributes. Class::DBI::AbstractSearch uses these attributes:
85
86=over 4
87
88=item *
89
90B<order_by>
91
92Array reference of fields that will be used to order the results of
93your query.
94
95=item *
96
97B<limit_dialect>
98
99Scalar, DBI handle, object class, etc. that describes the syntax model
100for a LIMIT/OFFSET SQL clause. Please see SQL::Abstract::Limit
101for more information.
102
103=item *
104
105B<limit>
106
107Scalar value that will be used for LIMIT argument in a query.
108
109=item *
110
111B<offset>
112
113Scalar value that will be used for OFFSET argument in a query.
114
115=back
116
117Any other attributes are passed to the SQL::Abstract::Limit constructor,
118and can be used to control how queries are created. For example,
119to use 'AND' instead of 'OR' by default, use:
120
121 $class->search_where(\%where, { logic => 'AND' });
122
123=head1 AUTHOR
124
125Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt> with some help from
126cdbi-talk mailing list, especially:
127
128 Tim Bunce
129 Simon Wilcox
130 Tony Bowden
131
132This library is free software; you can redistribute it and/or modify
133it under the same terms as Perl itself.
134
135=head1 SEE ALSO
136
137L<Class::DBI>, L<SQL::Abstract>, L<SQL::Abstract::Limit>
138
139=cut