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