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