File | /project/perl/lib/Class/DBI/AbstractSearch.pm |
Statements Executed | 14 |
Statement Execution Time | 11.3ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
0 | 0 | 0 | 0s | 0s | BEGIN | Class::DBI::AbstractSearch::
0 | 0 | 0 | 0s | 0s | search_where | Class::DBI::AbstractSearch::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Class::DBI::AbstractSearch; | ||||
2 | |||||
3 | 3 | 99µs | 1 | 28µs | use strict; # spent 28µs making 1 call to strict::import |
4 | 3 | 157µs | 1 | 225µs | use vars qw($VERSION @EXPORT); # spent 225µs making 1 call to vars::import |
5 | 1 | 6µs | $VERSION = 0.07; | ||
6 | |||||
7 | 1 | 8µs | require Exporter; | ||
8 | 1 | 10µs | *import = \&Exporter::import; | ||
9 | 1 | 6µs | @EXPORT = qw(search_where); | ||
10 | |||||
11 | 3 | 11.0ms | use SQL::Abstract::Limit; | ||
12 | |||||
13 | sub 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 | |||||
36 | 1 | 14µs | 1; | ||
37 | __END__ | ||||
38 | |||||
39 | =head1 NAME | ||||
40 | |||||
41 | Class::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 | |||||
64 | Class::DBI::AbstractSearch is a Class::DBI plugin to glue | ||||
65 | SQL::Abstract::Limit into Class::DBI. | ||||
66 | |||||
67 | =head1 METHODS | ||||
68 | |||||
69 | Using 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 | |||||
77 | Takes a hash to specify WHERE clause. See L<SQL::Abstract> for hash | ||||
78 | options. | ||||
79 | |||||
80 | $class->search_where(\%where,\%attrs); | ||||
81 | |||||
82 | Takes hash reference to specify WHERE clause. See L<SQL::Abstract> | ||||
83 | for hash options. Takes a hash reference to specify additional query | ||||
84 | attributes. Class::DBI::AbstractSearch uses these attributes: | ||||
85 | |||||
86 | =over 4 | ||||
87 | |||||
88 | =item * | ||||
89 | |||||
90 | B<order_by> | ||||
91 | |||||
92 | Array reference of fields that will be used to order the results of | ||||
93 | your query. | ||||
94 | |||||
95 | =item * | ||||
96 | |||||
97 | B<limit_dialect> | ||||
98 | |||||
99 | Scalar, DBI handle, object class, etc. that describes the syntax model | ||||
100 | for a LIMIT/OFFSET SQL clause. Please see SQL::Abstract::Limit | ||||
101 | for more information. | ||||
102 | |||||
103 | =item * | ||||
104 | |||||
105 | B<limit> | ||||
106 | |||||
107 | Scalar value that will be used for LIMIT argument in a query. | ||||
108 | |||||
109 | =item * | ||||
110 | |||||
111 | B<offset> | ||||
112 | |||||
113 | Scalar value that will be used for OFFSET argument in a query. | ||||
114 | |||||
115 | =back | ||||
116 | |||||
117 | Any other attributes are passed to the SQL::Abstract::Limit constructor, | ||||
118 | and can be used to control how queries are created. For example, | ||||
119 | to use 'AND' instead of 'OR' by default, use: | ||||
120 | |||||
121 | $class->search_where(\%where, { logic => 'AND' }); | ||||
122 | |||||
123 | =head1 AUTHOR | ||||
124 | |||||
125 | Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt> with some help from | ||||
126 | cdbi-talk mailing list, especially: | ||||
127 | |||||
128 | Tim Bunce | ||||
129 | Simon Wilcox | ||||
130 | Tony Bowden | ||||
131 | |||||
132 | This library is free software; you can redistribute it and/or modify | ||||
133 | it under the same terms as Perl itself. | ||||
134 | |||||
135 | =head1 SEE ALSO | ||||
136 | |||||
137 | L<Class::DBI>, L<SQL::Abstract>, L<SQL::Abstract::Limit> | ||||
138 | |||||
139 | =cut |