packaging one directory
|
1 |
package DBIx::Custom::Result; |
update document
|
2 | |
updatedd pod
|
3 |
use Object::Simple -base; |
cleanup
|
4 | |
packaging one directory
|
5 |
use Carp 'croak'; |
cleanup
|
6 |
use DBIx::Custom::Util qw/_array_to_hash _subname/; |
packaging one directory
|
7 | |
updatedd pod
|
8 |
has [qw/filters filter_off sth type_rule type_rule_off/], |
9 |
stash => sub { {} }; |
|
cleanup
|
10 | |
- added DBIx::Custom::Result...
|
11 |
*all = \&fetch_hash_all; |
12 | ||
cleanup
|
13 |
sub filter { |
14 |
my $self = shift; |
|
cleanup
|
15 |
|
16 |
if (@_) { |
|
all filter can receive array...
|
17 |
my $filter = {}; |
cleanup
|
18 |
|
all filter can receive array...
|
19 |
if (ref $_[0] eq 'HASH') { |
20 |
$filter = $_[0]; |
|
21 |
} |
|
22 |
else { |
|
cleanup
|
23 |
$filter = _array_to_hash( |
all filter can receive array...
|
24 |
@_ > 1 ? [@_] : $_[0] |
25 |
); |
|
26 |
} |
|
27 |
|
|
cleanup
|
28 |
foreach my $column (keys %$filter) { |
29 |
my $fname = $filter->{$column}; |
|
fix bug : filter can't over...
|
30 | |
31 |
if (exists $filter->{$column} |
|
32 |
&& defined $fname |
|
33 |
&& ref $fname ne 'CODE') |
|
34 |
{ |
|
cleanup
|
35 |
croak qq{Filter "$fname" is not registered" } . _subname |
cleanup
|
36 |
unless exists $self->filters->{$fname}; |
37 |
|
|
38 |
$filter->{$column} = $self->filters->{$fname}; |
|
39 |
} |
|
cleanup
|
40 |
} |
cleanup
|
41 |
|
added experimental DBIx::Cus...
|
42 |
$self->{filter} = {%{$self->filter}, %$filter}; |
cleanup
|
43 |
|
44 |
return $self; |
|
cleanup
|
45 |
} |
46 |
|
|
added experimental DBIx::Cus...
|
47 |
return $self->{filter} ||= {}; |
48 |
} |
|
49 | ||
packaging one directory
|
50 |
sub fetch { |
changed argument of tag proc...
|
51 |
my $self = shift; |
52 |
|
|
cleanup
|
53 |
# Filter |
added experimental DBIx::Cus...
|
54 |
my $filter = $self->filter; |
55 |
|
|
56 |
# End filter |
|
cleanup
|
57 |
my $end_filter = $self->{end_filter} || {}; |
packaging one directory
|
58 |
|
59 |
# Fetch |
|
changed argument of tag proc...
|
60 |
my @row = $self->{sth}->fetchrow_array; |
packaging one directory
|
61 |
|
cleanup
|
62 |
# No row |
update document
|
63 |
return unless @row; |
added check_filter attribute
|
64 |
|
cleanup
|
65 |
# Filtering |
added experimental iterate_a...
|
66 |
my $columns = $self->{sth}->{NAME}; |
added type_rule method and f...
|
67 |
my $types = $self->{sth}->{TYPE}; |
68 |
my $type_rule = $self->type_rule || {}; |
|
69 |
|
|
cleanup
|
70 |
for (my $i = 0; $i < @$columns; $i++) { |
update document
|
71 |
|
changed type_rule arguments ...
|
72 |
if (!$self->type_rule_off && $type_rule->{from} && |
73 |
(my $rule = $type_rule->{from}->{$types->[$i]})) |
|
added type_rule method and f...
|
74 |
{ |
75 |
$row[$i] = $rule->($row[$i]); |
|
76 |
} |
|
77 |
|
|
changed argument of tag proc...
|
78 |
# Filter name |
cleanup
|
79 |
my $column = $columns->[$i]; |
cleanup
|
80 |
my $f = exists $filter->{$column} |
81 |
? $filter->{$column} |
|
cleanup
|
82 |
: $self->{default_filter}; |
added experimental DBIx::Cus...
|
83 |
my $ef = $end_filter->{$column}; |
some changed
|
84 |
|
cleanup
|
85 |
# Filtering |
type_rule can receive filter...
|
86 |
$row[$i] = $f->($row[$i]) if $f && !$self->filter_off; |
87 |
$row[$i] = $ef->($row[$i]) if $ef && !$self->filter_off; |
|
packaging one directory
|
88 |
} |
many many changes
|
89 | |
removed reconnect method
|
90 |
return \@row; |
91 |
} |
|
92 | ||
cleanup
|
93 |
sub fetch_all { |
94 |
my $self = shift; |
|
95 |
|
|
96 |
# Fetch all rows |
|
97 |
my $rows = []; |
|
98 |
while(my $row = $self->fetch) { |
|
99 |
push @$rows, $row; |
|
100 |
} |
|
101 |
return $rows; |
|
102 |
} |
|
103 | ||
removed reconnect method
|
104 |
sub fetch_first { |
105 |
my $self = shift; |
|
106 |
|
|
107 |
# Fetch |
|
108 |
my $row = $self->fetch; |
|
109 |
|
|
cleanup
|
110 |
# No row |
removed reconnect method
|
111 |
return unless $row; |
112 |
|
|
113 |
# Finish statement handle |
|
114 |
$self->sth->finish; |
|
115 |
|
|
116 |
return $row; |
|
117 |
} |
|
118 | ||
packaging one directory
|
119 |
sub fetch_hash { |
changed argument of tag proc...
|
120 |
my $self = shift; |
121 |
|
|
cleanup
|
122 |
# Filter |
added experimental DBIx::Cus...
|
123 |
my $filter = $self->filter; |
124 |
|
|
125 |
# End filter |
|
cleanup
|
126 |
my $end_filter = $self->{end_filter} || {}; |
packaging one directory
|
127 |
|
128 |
# Fetch |
|
changed argument of tag proc...
|
129 |
my $row = $self->{sth}->fetchrow_arrayref; |
packaging one directory
|
130 |
|
131 |
# Cannot fetch |
|
132 |
return unless $row; |
|
added check_filter attribute
|
133 | |
packaging one directory
|
134 |
# Filter |
135 |
my $row_hash = {}; |
|
added experimental iterate_a...
|
136 |
my $columns = $self->{sth}->{NAME}; |
added type_rule method and f...
|
137 |
my $types = $self->{sth}->{TYPE}; |
138 |
my $type_rule = $self->type_rule || {}; |
|
cleanup
|
139 |
for (my $i = 0; $i < @$columns; $i++) { |
update document
|
140 |
|
added type_rule method and f...
|
141 |
# Type rule |
changed type_rule arguments ...
|
142 |
if (!$self->type_rule_off && $type_rule->{from} && |
143 |
(my $rule = $type_rule->{from}->{$types->[$i]})) |
|
added type_rule method and f...
|
144 |
{ |
145 |
$row->[$i] = $rule->($row->[$i]); |
|
146 |
} |
|
147 |
|
|
changed argument of tag proc...
|
148 |
# Filter name |
cleanup
|
149 |
my $column = $columns->[$i]; |
cleanup
|
150 |
my $f = exists $filter->{$column} |
151 |
? $filter->{$column} |
|
cleanup
|
152 |
: $self->{default_filter}; |
added experimental DBIx::Cus...
|
153 |
my $ef = $end_filter->{$column}; |
add query filter error check
|
154 |
|
cleanup
|
155 |
# Filtering |
type_rule can receive filter...
|
156 |
$row_hash->{$column} = $f && !$self->filter_off ? $f->($row->[$i]) |
157 |
: $row->[$i]; |
|
158 |
$row_hash->{$column} = $ef->($row_hash->{$column}) |
|
159 |
if $ef && !$self->filter_off; |
|
packaging one directory
|
160 |
} |
161 |
|
|
removed reconnect method
|
162 |
return $row_hash; |
packaging one directory
|
163 |
} |
164 | ||
cleanup
|
165 |
sub fetch_hash_all { |
166 |
my $self = shift; |
|
167 |
|
|
168 |
# Fetch all rows as hash |
|
169 |
my $rows = []; |
|
170 |
while(my $row = $self->fetch_hash) { |
|
171 |
push @$rows, $row; |
|
172 |
} |
|
173 |
|
|
174 |
return $rows; |
|
175 |
} |
|
176 | ||
removed reconnect method
|
177 |
sub fetch_hash_first { |
packaging one directory
|
178 |
my $self = shift; |
179 |
|
|
180 |
# Fetch hash |
|
181 |
my $row = $self->fetch_hash; |
|
182 |
|
|
cleanup
|
183 |
# No row |
packaging one directory
|
184 |
return unless $row; |
185 |
|
|
186 |
# Finish statement handle |
|
some changed
|
187 |
$self->sth->finish; |
packaging one directory
|
188 |
|
removed reconnect method
|
189 |
return $row; |
packaging one directory
|
190 |
} |
191 | ||
renamed fetch_rows to fetch_...
|
192 |
sub fetch_hash_multi { |
packaging one directory
|
193 |
my ($self, $count) = @_; |
194 |
|
|
cleanup
|
195 |
# Row count not specified |
cleanup
|
196 |
croak 'Row count must be specified ' . _subname |
packaging one directory
|
197 |
unless $count; |
198 |
|
|
199 |
# Fetch multi rows |
|
200 |
my $rows = []; |
|
201 |
for (my $i = 0; $i < $count; $i++) { |
|
removed reconnect method
|
202 |
my $row = $self->fetch_hash; |
203 |
last unless $row; |
|
204 |
push @$rows, $row; |
|
packaging one directory
|
205 |
} |
206 |
|
|
207 |
return unless @$rows; |
|
removed reconnect method
|
208 |
return $rows; |
packaging one directory
|
209 |
} |
210 | ||
cleanup
|
211 |
sub fetch_multi { |
212 |
my ($self, $count) = @_; |
|
packaging one directory
|
213 |
|
cleanup
|
214 |
# Row count not specifed |
cleanup
|
215 |
croak 'Row count must be specified ' . _subname |
cleanup
|
216 |
unless $count; |
217 |
|
|
218 |
# Fetch multi rows |
|
packaging one directory
|
219 |
my $rows = []; |
cleanup
|
220 |
for (my $i = 0; $i < $count; $i++) { |
221 |
my $row = $self->fetch; |
|
222 |
last unless $row; |
|
removed reconnect method
|
223 |
push @$rows, $row; |
packaging one directory
|
224 |
} |
changed argument of tag proc...
|
225 |
|
cleanup
|
226 |
return unless @$rows; |
removed reconnect method
|
227 |
return $rows; |
packaging one directory
|
228 |
} |
229 | ||
- added DBIx::Custom::Result...
|
230 |
*one = \&fetch_hash_first; |
231 | ||
cleanup
|
232 |
# DEPRECATED! |
233 |
sub end_filter { |
|
234 |
my $self = shift; |
|
235 |
|
|
236 |
if (@_) { |
|
237 |
my $end_filter = {}; |
|
238 |
|
|
239 |
if (ref $_[0] eq 'HASH') { |
|
240 |
$end_filter = $_[0]; |
|
241 |
} |
|
242 |
else { |
|
243 |
$end_filter = _array_to_hash( |
|
244 |
@_ > 1 ? [@_] : $_[0] |
|
245 |
); |
|
246 |
} |
|
247 |
|
|
248 |
foreach my $column (keys %$end_filter) { |
|
249 |
my $fname = $end_filter->{$column}; |
|
250 |
|
|
251 |
if (exists $end_filter->{$column} |
|
252 |
&& defined $fname |
|
253 |
&& ref $fname ne 'CODE') |
|
254 |
{ |
|
255 |
croak qq{Filter "$fname" is not registered" } . _subname |
|
256 |
unless exists $self->filters->{$fname}; |
|
257 |
|
|
258 |
$end_filter->{$column} = $self->filters->{$fname}; |
|
259 |
} |
|
260 |
} |
|
261 |
|
|
262 |
$self->{end_filter} = {%{$self->end_filter}, %$end_filter}; |
|
263 |
|
|
264 |
return $self; |
|
265 |
} |
|
266 |
|
|
267 |
return $self->{end_filter} ||= {}; |
|
268 |
} |
|
269 | ||
cleanup
|
270 |
# DEPRECATED! |
added experimental DBIx::Cus...
|
271 |
sub remove_end_filter { |
272 |
my $self = shift; |
|
273 |
|
|
cleanup
|
274 |
warn "remove_end_filter is DEPRECATED! use filter_off attribute instead"; |
275 |
|
|
added experimental DBIx::Cus...
|
276 |
$self->{end_filter} = {}; |
277 |
|
|
278 |
return $self; |
|
279 |
} |
|
280 | ||
cleanup
|
281 |
# DEPRECATED! |
added experimental DBIx::Cus...
|
282 |
sub remove_filter { |
283 |
my $self = shift; |
|
cleanup
|
284 | |
285 |
warn "remove_filter is DEPRECATED! use filter_off attribute instead"; |
|
added experimental DBIx::Cus...
|
286 |
|
287 |
$self->{filter} = {}; |
|
288 |
|
|
289 |
return $self; |
|
290 |
} |
|
291 | ||
cleanup
|
292 |
# DEPRECATED! |
cleanup
|
293 |
sub default_filter { |
294 |
my $self = shift; |
|
cleanup
|
295 | |
cleanup
|
296 |
warn "default_filter is DEPRECATED!"; |
cleanup
|
297 |
|
298 |
if (@_) { |
|
299 |
my $fname = $_[0]; |
|
300 |
if (@_ && !$fname) { |
|
301 |
$self->{default_filter} = undef; |
|
302 |
} |
|
303 |
else { |
|
many changed
|
304 |
croak qq{Filter "$fname" is not registered} |
cleanup
|
305 |
unless exists $self->filters->{$fname}; |
306 |
|
|
307 |
$self->{default_filter} = $self->filters->{$fname}; |
|
308 |
} |
|
309 |
|
|
310 |
return $self; |
|
311 |
} |
|
312 |
|
|
313 |
return $self->{default_filter}; |
|
314 |
} |
|
315 | ||
cleanup
|
316 |
# DEPRECATED! |
cleanup
|
317 |
has 'filter_check'; |
cleanup
|
318 | |
update document
|
319 |
1; |
320 | ||
packaging one directory
|
321 |
=head1 NAME |
322 | ||
cleanup
|
323 |
DBIx::Custom::Result - Result of select statement |
packaging one directory
|
324 | |
update document
|
325 |
=head1 SYNOPSIS |
cleanup
|
326 | |
327 |
Get the result of select statement. |
|
328 | ||
removed reconnect method
|
329 |
# Result |
330 |
my $result = $dbi->select(table => 'books'); |
|
cleanup
|
331 | |
332 |
Fetch row into array. |
|
removed reconnect method
|
333 |
|
334 |
# Fetch a row into array |
|
335 |
while (my $row = $result->fetch) { |
|
cleanup
|
336 |
my $author = $row->[0]; |
337 |
my $title = $row->[1]; |
|
removed reconnect method
|
338 |
|
version 0.0901
|
339 |
} |
340 |
|
|
cleanup
|
341 |
# Fetch only a first row into array |
removed reconnect method
|
342 |
my $row = $result->fetch_first; |
343 |
|
|
344 |
# Fetch multiple rows into array of array |
|
345 |
while (my $rows = $result->fetch_multi(5)) { |
|
cleanup
|
346 |
my $first_author = $rows->[0][0]; |
347 |
my $first_title = $rows->[0][1]; |
|
348 |
my $second_author = $rows->[1][0]; |
|
349 |
my $second_value = $rows->[1][1]; |
|
350 |
|
|
removed reconnect method
|
351 |
} |
352 |
|
|
353 |
# Fetch all rows into array of array |
|
354 |
my $rows = $result->fetch_all; |
|
cleanup
|
355 | |
356 |
Fetch row into hash. |
|
357 | ||
358 |
# Fetch a row into hash |
|
removed reconnect method
|
359 |
while (my $row = $result->fetch_hash) { |
cleanup
|
360 |
my $title = $row->{title}; |
361 |
my $author = $row->{author}; |
|
removed reconnect method
|
362 |
|
packaging one directory
|
363 |
} |
removed reconnect method
|
364 |
|
cleanup
|
365 |
# Fetch only a first row into hash |
removed reconnect method
|
366 |
my $row = $result->fetch_hash_first; |
367 |
|
|
368 |
# Fetch multiple rows into array of hash |
|
cleanup
|
369 |
while (my $rows = $result->fetch_hash_multi(5)) { |
370 |
my $first_title = $rows->[0]{title}; |
|
371 |
my $first_author = $rows->[0]{author}; |
|
372 |
my $second_title = $rows->[1]{title}; |
|
373 |
my $second_author = $rows->[1]{author}; |
|
removed reconnect method
|
374 |
} |
375 |
|
|
376 |
# Fetch all rows into array of hash |
|
377 |
my $rows = $result->fetch_hash_all; |
|
packaging one directory
|
378 | |
update document
|
379 |
=head1 ATTRIBUTES |
packaging one directory
|
380 | |
cleanup
|
381 |
Filters when a row is fetched. |
382 |
This overwrites C<default_filter>. |
|
removed DESTROY method(not b...
|
383 | |
updated_pod
|
384 |
=head2 C<filter_off> EXPERIMENTAL |
385 | ||
386 |
my $filter_off = $resutl->filter_off; |
|
387 |
$result = $result->filter_off(1); |
|
388 | ||
389 |
Turn filter off. |
|
390 | ||
cleanup
|
391 |
=head2 C<filters> |
392 | ||
393 |
my $filters = $result->filters; |
|
394 |
$result = $result->filters(\%filters); |
|
395 | ||
396 |
Resistered filters. |
|
397 | ||
398 |
=head2 C<sth> |
|
399 | ||
400 |
my $sth = $reuslt->sth |
|
401 |
$result = $result->sth($sth); |
|
402 | ||
403 |
Statement handle of L<DBI>. |
|
404 | ||
added EXPERIMENTAL execute()...
|
405 |
=head2 C<type_rule_off> EXPERIMENTAL |
406 | ||
407 |
my $type_rule_off = $result->type_rule_off; |
|
408 |
$result = $result->type_rule_off(1); |
|
409 | ||
410 |
Turn type rule off. |
|
411 | ||
update document
|
412 |
=head1 METHODS |
413 | ||
renamed build_query to creat...
|
414 |
L<DBIx::Custom::Result> inherits all methods from L<Object::Simple> |
cleanup
|
415 |
and implements the following new ones. |
packaging one directory
|
416 | |
updated pod
|
417 |
=head2 C<all> |
418 | ||
419 |
my $rows = $result->all; |
|
420 | ||
421 |
This is alias for C<fetch_hash_all>. |
|
422 | ||
removed DBIx::Custom commit ...
|
423 |
=head2 C<fetch> |
packaging one directory
|
424 | |
cleanup
|
425 |
my $row = $result->fetch; |
version 0.0901
|
426 | |
cleanup
|
427 |
Fetch a row into array. |
packaging one directory
|
428 | |
removed DBIx::Custom commit ...
|
429 |
=head2 C<fetch_all> |
packaging one directory
|
430 | |
cleanup
|
431 |
my $rows = $result->fetch_all; |
version 0.0901
|
432 | |
removed DESTROY method(not b...
|
433 |
Fetch all rows into array of array. |
packaging one directory
|
434 | |
cleanup
|
435 |
=head2 C<fetch_first> |
436 | ||
437 |
my $row = $result->fetch_first; |
|
438 | ||
439 |
Fetch only a first row into array and finish statment handle. |
|
440 | ||
removed DESTROY method(not b...
|
441 |
=head2 C<fetch_hash> |
packaging one directory
|
442 | |
cleanup
|
443 |
my $row = $result->fetch_hash; |
packaging one directory
|
444 | |
removed DESTROY method(not b...
|
445 |
Fetch a row into hash |
update document
|
446 | |
cleanup
|
447 |
=head2 C<fetch_hash_all> |
448 | ||
449 |
my $rows = $result->fetch_hash_all; |
|
450 | ||
451 |
Fetch all rows into array of hash. |
|
452 | ||
removed DBIx::Custom commit ...
|
453 |
=head2 C<fetch_hash_first> |
removed reconnect method
|
454 |
|
cleanup
|
455 |
my $row = $result->fetch_hash_first; |
packaging one directory
|
456 | |
removed DESTROY method(not b...
|
457 |
Fetch only first row into hash and finish statment handle. |
packaging one directory
|
458 | |
removed DESTROY method(not b...
|
459 |
=head2 C<fetch_hash_multi> |
update document
|
460 | |
cleanup
|
461 |
my $rows = $result->fetch_hash_multi(5); |
update document
|
462 |
|
removed DESTROY method(not b...
|
463 |
Fetch multiple rows into array of hash |
cleanup
|
464 |
Row count must be specified. |
update document
|
465 | |
cleanup
|
466 |
=head2 C<fetch_multi> |
packaging one directory
|
467 | |
cleanup
|
468 |
my $rows = $result->fetch_multi(5); |
469 |
|
|
470 |
Fetch multiple rows into array of array. |
|
471 |
Row count must be specified. |
|
removed DESTROY method(not b...
|
472 | |
cleanup
|
473 |
=head2 C<filter> |
474 | ||
added experimental DBIx::Cus...
|
475 |
$result = $result->filter(title => 'to_something', |
476 |
author => 'to_something'); |
|
added experimental DBIx::Cus...
|
477 | |
all filter can receive array...
|
478 |
$result = $result->filter([qw/title author/] => 'to_something'); |
479 | ||
added experimental DBIx::Cus...
|
480 |
Filters. |
481 |
These each filters override the filters applied by C<apply_filter> of |
|
482 |
L<DBIx::Custom>. |
|
cleanup
|
483 | |
updated pod
|
484 |
=head2 C<one> |
485 | ||
486 |
my $row = $result->one; |
|
487 | ||
488 |
This is alias for C<fetch_hash_first>. |
|
489 | ||
- removed DEPRECATED DBIx::C...
|
490 |
=head2 C<remove_filter> |
added experimental DBIx::Cus...
|
491 | |
492 |
$result->remove_filter; |
|
493 | ||
494 |
Remove filter. End filter is not removed. |
|
495 | ||
- removed DEPRECATED DBIx::C...
|
496 |
=head2 C<stash> |
added experimental DBIx::Cus...
|
497 | |
498 |
my $stash = $result->stash; |
|
499 |
my $foo = $result->stash->{foo}; |
|
500 |
$result->stash->{foo} = $foo; |
|
501 | ||
502 |
Stash is hash reference to save your data. |
|
503 | ||
EXPERIMENTAL type_rule_off i...
|
504 |
=head2 C<remove_end_filter> DEPRECATED! |
505 | ||
506 |
$result->remove_end_filter; |
|
507 | ||
508 |
Remove end filter. |
|
509 | ||
510 |
=head2 C<end_filter> DEPRECATED! |
|
511 | ||
512 |
$result = $result->end_filter(title => 'to_something', |
|
513 |
author => 'to_something'); |
|
514 | ||
515 |
$result = $result->end_filter([qw/title author/] => 'to_something'); |
|
516 | ||
517 |
End filters. |
|
518 |
These each filters is executed after the filters applied by C<apply_filter> of |
|
519 |
L<DBIx::Custom> or C<filter> method. |
|
520 | ||
packaging one directory
|
521 |
=cut |