packaging one directory
|
1 |
package DBIx::Custom::Result; |
update document
|
2 | |
packaging one directory
|
3 |
use strict; |
4 |
use warnings; |
|
update document
|
5 | |
6 |
use base 'Object::Simple'; |
|
cleanup
|
7 | |
packaging one directory
|
8 |
use Carp 'croak'; |
9 | ||
cleanup
|
10 |
__PACKAGE__->attr([qw/filter_check filters sth/]); |
11 | ||
12 |
sub default_filter { |
|
13 |
my $self = shift; |
|
14 |
|
|
cleanup
|
15 |
if (@_) { |
16 |
my $fname = $_[0]; |
|
17 |
if (@_ && !$fname) { |
|
18 |
$self->{default_filter} = undef; |
|
19 |
} |
|
20 |
else { |
|
21 |
croak qq{"$fname" is not registered} |
|
22 |
unless exists $self->filters->{$fname}; |
|
23 |
|
|
24 |
$self->{default_filter} = $self->filters->{$fname}; |
|
25 |
} |
|
26 |
|
|
27 |
return $self; |
|
cleanup
|
28 |
} |
29 |
|
|
cleanup
|
30 |
return $self->{default_filter}; |
cleanup
|
31 |
} |
32 | ||
33 |
sub filter { |
|
34 |
my $self = shift; |
|
cleanup
|
35 |
|
36 |
if (@_) { |
|
37 |
my $filter = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
38 |
|
|
39 |
foreach my $column (keys %$filter) { |
|
40 |
my $fname = $filter->{$column}; |
|
41 |
unless (ref $fname eq 'CODE') { |
|
42 |
croak qq{"$fname" is not registered"} |
|
43 |
unless exists $self->filters->{$fname}; |
|
44 |
|
|
45 |
$filter->{$column} = $self->filters->{$fname}; |
|
46 |
} |
|
cleanup
|
47 |
} |
cleanup
|
48 |
|
49 |
$self->{filter} = $filter; |
|
50 |
|
|
51 |
return $self; |
|
cleanup
|
52 |
} |
53 |
|
|
cleanup
|
54 |
return $self->{filter}; |
cleanup
|
55 |
} |
cleanup
|
56 | |
packaging one directory
|
57 |
sub fetch { |
changed argument of tag proc...
|
58 |
my $self = shift; |
59 |
|
|
cleanup
|
60 |
# Filters |
cleanup
|
61 |
my $filters = $self->filters || {}; |
62 |
my $filter = $self->{filter} || {}; |
|
added auto_filter method
|
63 |
my $auto_filter = $self->{_auto_filter} || {}; |
64 |
$filter = {%$auto_filter, %$filter}; |
|
packaging one directory
|
65 |
|
66 |
# Fetch |
|
changed argument of tag proc...
|
67 |
my @row = $self->{sth}->fetchrow_array; |
packaging one directory
|
68 |
|
cleanup
|
69 |
# No row |
update document
|
70 |
return unless @row; |
added check_filter attribute
|
71 |
|
cleanup
|
72 |
# Filtering |
added experimental iterate_a...
|
73 |
my $columns = $self->{sth}->{NAME}; |
cleanup
|
74 |
for (my $i = 0; $i < @$columns; $i++) { |
update document
|
75 |
|
changed argument of tag proc...
|
76 |
# Filter name |
cleanup
|
77 |
my $column = $columns->[$i]; |
cleanup
|
78 |
my $f = exists $filter->{$column} |
79 |
? $filter->{$column} |
|
cleanup
|
80 |
: $self->default_filter; |
some changed
|
81 |
|
cleanup
|
82 |
# Filtering |
cleanup
|
83 |
$row[$i] = $f->($row[$i]) if $f; |
packaging one directory
|
84 |
} |
many many changes
|
85 | |
removed reconnect method
|
86 |
return \@row; |
87 |
} |
|
88 | ||
cleanup
|
89 |
sub fetch_all { |
90 |
my $self = shift; |
|
91 |
|
|
92 |
# Fetch all rows |
|
93 |
my $rows = []; |
|
94 |
while(my $row = $self->fetch) { |
|
95 |
push @$rows, $row; |
|
96 |
} |
|
97 |
return $rows; |
|
98 |
} |
|
99 | ||
removed reconnect method
|
100 |
sub fetch_first { |
101 |
my $self = shift; |
|
102 |
|
|
103 |
# Fetch |
|
104 |
my $row = $self->fetch; |
|
105 |
|
|
cleanup
|
106 |
# No row |
removed reconnect method
|
107 |
return unless $row; |
108 |
|
|
109 |
# Finish statement handle |
|
110 |
$self->sth->finish; |
|
111 |
|
|
112 |
return $row; |
|
113 |
} |
|
114 | ||
packaging one directory
|
115 |
sub fetch_hash { |
changed argument of tag proc...
|
116 |
my $self = shift; |
117 |
|
|
cleanup
|
118 |
# Filters |
cleanup
|
119 |
my $filters = $self->filters || {}; |
120 |
my $filter = $self->filter || {}; |
|
added auto_filter method
|
121 |
my $auto_filter = $self->{_auto_filter} || {}; |
122 |
$filter = {%$auto_filter, %$filter}; |
|
packaging one directory
|
123 |
|
124 |
# Fetch |
|
changed argument of tag proc...
|
125 |
my $row = $self->{sth}->fetchrow_arrayref; |
packaging one directory
|
126 |
|
127 |
# Cannot fetch |
|
128 |
return unless $row; |
|
added check_filter attribute
|
129 | |
packaging one directory
|
130 |
# Filter |
131 |
my $row_hash = {}; |
|
added experimental iterate_a...
|
132 |
my $columns = $self->{sth}->{NAME}; |
cleanup
|
133 |
for (my $i = 0; $i < @$columns; $i++) { |
update document
|
134 |
|
changed argument of tag proc...
|
135 |
# Filter name |
cleanup
|
136 |
my $column = $columns->[$i]; |
cleanup
|
137 |
my $f = exists $filter->{$column} |
138 |
? $filter->{$column} |
|
cleanup
|
139 |
: $self->default_filter; |
add query filter error check
|
140 |
|
cleanup
|
141 |
# Filtering |
cleanup
|
142 |
$row_hash->{$column} = $f ? $f->($row->[$i]) : $row->[$i]; |
packaging one directory
|
143 |
} |
144 |
|
|
removed reconnect method
|
145 |
return $row_hash; |
packaging one directory
|
146 |
} |
147 | ||
cleanup
|
148 |
sub fetch_hash_all { |
149 |
my $self = shift; |
|
150 |
|
|
151 |
# Fetch all rows as hash |
|
152 |
my $rows = []; |
|
153 |
while(my $row = $self->fetch_hash) { |
|
154 |
push @$rows, $row; |
|
155 |
} |
|
156 |
|
|
157 |
return $rows; |
|
158 |
} |
|
159 | ||
removed reconnect method
|
160 |
sub fetch_hash_first { |
packaging one directory
|
161 |
my $self = shift; |
162 |
|
|
163 |
# Fetch hash |
|
164 |
my $row = $self->fetch_hash; |
|
165 |
|
|
cleanup
|
166 |
# No row |
packaging one directory
|
167 |
return unless $row; |
168 |
|
|
169 |
# Finish statement handle |
|
some changed
|
170 |
$self->sth->finish; |
packaging one directory
|
171 |
|
removed reconnect method
|
172 |
return $row; |
packaging one directory
|
173 |
} |
174 | ||
renamed fetch_rows to fetch_...
|
175 |
sub fetch_hash_multi { |
packaging one directory
|
176 |
my ($self, $count) = @_; |
177 |
|
|
cleanup
|
178 |
# Row count not specified |
changed argument of tag proc...
|
179 |
croak 'Row count must be specified' |
packaging one directory
|
180 |
unless $count; |
181 |
|
|
182 |
# Fetch multi rows |
|
183 |
my $rows = []; |
|
184 |
for (my $i = 0; $i < $count; $i++) { |
|
removed reconnect method
|
185 |
my $row = $self->fetch_hash; |
186 |
last unless $row; |
|
187 |
push @$rows, $row; |
|
packaging one directory
|
188 |
} |
189 |
|
|
190 |
return unless @$rows; |
|
removed reconnect method
|
191 |
return $rows; |
packaging one directory
|
192 |
} |
193 | ||
cleanup
|
194 |
sub fetch_multi { |
195 |
my ($self, $count) = @_; |
|
packaging one directory
|
196 |
|
cleanup
|
197 |
# Row count not specifed |
198 |
croak 'Row count must be specified' |
|
199 |
unless $count; |
|
200 |
|
|
201 |
# Fetch multi rows |
|
packaging one directory
|
202 |
my $rows = []; |
cleanup
|
203 |
for (my $i = 0; $i < $count; $i++) { |
204 |
my $row = $self->fetch; |
|
205 |
last unless $row; |
|
removed reconnect method
|
206 |
push @$rows, $row; |
packaging one directory
|
207 |
} |
changed argument of tag proc...
|
208 |
|
cleanup
|
209 |
return unless @$rows; |
removed reconnect method
|
210 |
return $rows; |
packaging one directory
|
211 |
} |
212 | ||
update document
|
213 |
1; |
214 | ||
packaging one directory
|
215 |
=head1 NAME |
216 | ||
cleanup
|
217 |
DBIx::Custom::Result - Result of select statement |
packaging one directory
|
218 | |
update document
|
219 |
=head1 SYNOPSIS |
cleanup
|
220 | |
221 |
Get the result of select statement. |
|
222 | ||
removed reconnect method
|
223 |
# Result |
224 |
my $result = $dbi->select(table => 'books'); |
|
cleanup
|
225 | |
226 |
Fetch row into array. |
|
removed reconnect method
|
227 |
|
228 |
# Fetch a row into array |
|
229 |
while (my $row = $result->fetch) { |
|
cleanup
|
230 |
my $author = $row->[0]; |
231 |
my $title = $row->[1]; |
|
removed reconnect method
|
232 |
|
version 0.0901
|
233 |
} |
234 |
|
|
cleanup
|
235 |
# Fetch only a first row into array |
removed reconnect method
|
236 |
my $row = $result->fetch_first; |
237 |
|
|
238 |
# Fetch multiple rows into array of array |
|
239 |
while (my $rows = $result->fetch_multi(5)) { |
|
cleanup
|
240 |
my $first_author = $rows->[0][0]; |
241 |
my $first_title = $rows->[0][1]; |
|
242 |
my $second_author = $rows->[1][0]; |
|
243 |
my $second_value = $rows->[1][1]; |
|
244 |
|
|
removed reconnect method
|
245 |
} |
246 |
|
|
247 |
# Fetch all rows into array of array |
|
248 |
my $rows = $result->fetch_all; |
|
cleanup
|
249 | |
250 |
Fetch row into hash. |
|
251 | ||
252 |
# Fetch a row into hash |
|
removed reconnect method
|
253 |
while (my $row = $result->fetch_hash) { |
cleanup
|
254 |
my $title = $row->{title}; |
255 |
my $author = $row->{author}; |
|
removed reconnect method
|
256 |
|
packaging one directory
|
257 |
} |
removed reconnect method
|
258 |
|
cleanup
|
259 |
# Fetch only a first row into hash |
removed reconnect method
|
260 |
my $row = $result->fetch_hash_first; |
261 |
|
|
262 |
# Fetch multiple rows into array of hash |
|
cleanup
|
263 |
while (my $rows = $result->fetch_hash_multi(5)) { |
264 |
my $first_title = $rows->[0]{title}; |
|
265 |
my $first_author = $rows->[0]{author}; |
|
266 |
my $second_title = $rows->[1]{title}; |
|
267 |
my $second_author = $rows->[1]{author}; |
|
removed reconnect method
|
268 |
} |
269 |
|
|
270 |
# Fetch all rows into array of hash |
|
271 |
my $rows = $result->fetch_hash_all; |
|
packaging one directory
|
272 | |
update document
|
273 |
=head1 ATTRIBUTES |
packaging one directory
|
274 | |
cleanup
|
275 |
Filters when a row is fetched. |
276 |
This overwrites C<default_filter>. |
|
removed DESTROY method(not b...
|
277 | |
cleanup
|
278 |
=head2 C<filters> |
279 | ||
280 |
my $filters = $result->filters; |
|
281 |
$result = $result->filters(\%filters); |
|
282 | ||
283 |
Resistered filters. |
|
284 | ||
285 |
=head2 C<filter_check> |
|
286 | ||
287 |
my $filter_check = $result->filter_check; |
|
288 |
$result = $result->filter_check; |
|
289 | ||
290 |
Enable filter validation. |
|
291 | ||
292 |
=head2 C<sth> |
|
293 | ||
294 |
my $sth = $reuslt->sth |
|
295 |
$result = $result->sth($sth); |
|
296 | ||
297 |
Statement handle of L<DBI>. |
|
298 | ||
update document
|
299 |
=head1 METHODS |
300 | ||
renamed build_query to creat...
|
301 |
L<DBIx::Custom::Result> inherits all methods from L<Object::Simple> |
cleanup
|
302 |
and implements the following new ones. |
packaging one directory
|
303 | |
cleanup
|
304 |
=head2 C<(deprecated) default_filter> |
305 | ||
cleanup
|
306 |
my $default_filter = $result->default_filter; |
cleanup
|
307 |
$result = $result->default_filter($filter); |
308 | ||
309 |
Default filter when a row is fetched. |
|
310 | ||
removed DBIx::Custom commit ...
|
311 |
=head2 C<fetch> |
packaging one directory
|
312 | |
cleanup
|
313 |
my $row = $result->fetch; |
version 0.0901
|
314 | |
cleanup
|
315 |
Fetch a row into array. |
packaging one directory
|
316 | |
removed DBIx::Custom commit ...
|
317 |
=head2 C<fetch_all> |
packaging one directory
|
318 | |
cleanup
|
319 |
my $rows = $result->fetch_all; |
version 0.0901
|
320 | |
removed DESTROY method(not b...
|
321 |
Fetch all rows into array of array. |
packaging one directory
|
322 | |
cleanup
|
323 |
=head2 C<fetch_first> |
324 | ||
325 |
my $row = $result->fetch_first; |
|
326 | ||
327 |
Fetch only a first row into array and finish statment handle. |
|
328 | ||
removed DESTROY method(not b...
|
329 |
=head2 C<fetch_hash> |
packaging one directory
|
330 | |
cleanup
|
331 |
my $row = $result->fetch_hash; |
packaging one directory
|
332 | |
removed DESTROY method(not b...
|
333 |
Fetch a row into hash |
update document
|
334 | |
cleanup
|
335 |
=head2 C<fetch_hash_all> |
336 | ||
337 |
my $rows = $result->fetch_hash_all; |
|
338 | ||
339 |
Fetch all rows into array of hash. |
|
340 | ||
removed DBIx::Custom commit ...
|
341 |
=head2 C<fetch_hash_first> |
removed reconnect method
|
342 |
|
cleanup
|
343 |
my $row = $result->fetch_hash_first; |
packaging one directory
|
344 | |
removed DESTROY method(not b...
|
345 |
Fetch only first row into hash and finish statment handle. |
packaging one directory
|
346 | |
removed DESTROY method(not b...
|
347 |
=head2 C<fetch_hash_multi> |
update document
|
348 | |
cleanup
|
349 |
my $rows = $result->fetch_hash_multi(5); |
update document
|
350 |
|
removed DESTROY method(not b...
|
351 |
Fetch multiple rows into array of hash |
cleanup
|
352 |
Row count must be specified. |
update document
|
353 | |
cleanup
|
354 |
=head2 C<fetch_multi> |
packaging one directory
|
355 | |
cleanup
|
356 |
my $rows = $result->fetch_multi(5); |
357 |
|
|
358 |
Fetch multiple rows into array of array. |
|
359 |
Row count must be specified. |
|
removed DESTROY method(not b...
|
360 | |
cleanup
|
361 |
=head2 C<filter> |
362 | ||
363 |
$result = $result->filter(title => 'decode_utf8', |
|
364 |
author => 'decode_utf8'); |
|
365 | ||
packaging one directory
|
366 |
=cut |