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 | ||
added experimental DBIx::Cus...
|
269 |
sub remove_end_filter { |
270 |
my $self = shift; |
|
271 |
|
|
272 |
$self->{end_filter} = {}; |
|
273 |
|
|
274 |
return $self; |
|
275 |
} |
|
276 | ||
added experimental DBIx::Cus...
|
277 |
sub remove_filter { |
278 |
my $self = shift; |
|
279 |
|
|
280 |
$self->{filter} = {}; |
|
281 |
|
|
282 |
return $self; |
|
283 |
} |
|
284 | ||
cleanup
|
285 |
# Deprecated |
286 |
sub default_filter { |
|
287 |
my $self = shift; |
|
288 |
|
|
289 |
if (@_) { |
|
290 |
my $fname = $_[0]; |
|
291 |
if (@_ && !$fname) { |
|
292 |
$self->{default_filter} = undef; |
|
293 |
} |
|
294 |
else { |
|
many changed
|
295 |
croak qq{Filter "$fname" is not registered} |
cleanup
|
296 |
unless exists $self->filters->{$fname}; |
297 |
|
|
298 |
$self->{default_filter} = $self->filters->{$fname}; |
|
299 |
} |
|
300 |
|
|
301 |
return $self; |
|
302 |
} |
|
303 |
|
|
304 |
return $self->{default_filter}; |
|
305 |
} |
|
306 | ||
cleanup
|
307 |
# DEPRECATED! |
308 |
__PACKAGE__->attr('filter_check'); |
|
309 | ||
update document
|
310 |
1; |
311 | ||
packaging one directory
|
312 |
=head1 NAME |
313 | ||
cleanup
|
314 |
DBIx::Custom::Result - Result of select statement |
packaging one directory
|
315 | |
update document
|
316 |
=head1 SYNOPSIS |
cleanup
|
317 | |
318 |
Get the result of select statement. |
|
319 | ||
removed reconnect method
|
320 |
# Result |
321 |
my $result = $dbi->select(table => 'books'); |
|
cleanup
|
322 | |
323 |
Fetch row into array. |
|
removed reconnect method
|
324 |
|
325 |
# Fetch a row into array |
|
326 |
while (my $row = $result->fetch) { |
|
cleanup
|
327 |
my $author = $row->[0]; |
328 |
my $title = $row->[1]; |
|
removed reconnect method
|
329 |
|
version 0.0901
|
330 |
} |
331 |
|
|
cleanup
|
332 |
# Fetch only a first row into array |
removed reconnect method
|
333 |
my $row = $result->fetch_first; |
334 |
|
|
335 |
# Fetch multiple rows into array of array |
|
336 |
while (my $rows = $result->fetch_multi(5)) { |
|
cleanup
|
337 |
my $first_author = $rows->[0][0]; |
338 |
my $first_title = $rows->[0][1]; |
|
339 |
my $second_author = $rows->[1][0]; |
|
340 |
my $second_value = $rows->[1][1]; |
|
341 |
|
|
removed reconnect method
|
342 |
} |
343 |
|
|
344 |
# Fetch all rows into array of array |
|
345 |
my $rows = $result->fetch_all; |
|
cleanup
|
346 | |
347 |
Fetch row into hash. |
|
348 | ||
349 |
# Fetch a row into hash |
|
removed reconnect method
|
350 |
while (my $row = $result->fetch_hash) { |
cleanup
|
351 |
my $title = $row->{title}; |
352 |
my $author = $row->{author}; |
|
removed reconnect method
|
353 |
|
packaging one directory
|
354 |
} |
removed reconnect method
|
355 |
|
cleanup
|
356 |
# Fetch only a first row into hash |
removed reconnect method
|
357 |
my $row = $result->fetch_hash_first; |
358 |
|
|
359 |
# Fetch multiple rows into array of hash |
|
cleanup
|
360 |
while (my $rows = $result->fetch_hash_multi(5)) { |
361 |
my $first_title = $rows->[0]{title}; |
|
362 |
my $first_author = $rows->[0]{author}; |
|
363 |
my $second_title = $rows->[1]{title}; |
|
364 |
my $second_author = $rows->[1]{author}; |
|
removed reconnect method
|
365 |
} |
366 |
|
|
367 |
# Fetch all rows into array of hash |
|
368 |
my $rows = $result->fetch_hash_all; |
|
packaging one directory
|
369 | |
update document
|
370 |
=head1 ATTRIBUTES |
packaging one directory
|
371 | |
cleanup
|
372 |
Filters when a row is fetched. |
373 |
This overwrites C<default_filter>. |
|
removed DESTROY method(not b...
|
374 | |
updated_pod
|
375 |
=head2 C<filter_off> EXPERIMENTAL |
376 | ||
377 |
my $filter_off = $resutl->filter_off; |
|
378 |
$result = $result->filter_off(1); |
|
379 | ||
380 |
Turn filter off. |
|
381 | ||
cleanup
|
382 |
=head2 C<filters> |
383 | ||
384 |
my $filters = $result->filters; |
|
385 |
$result = $result->filters(\%filters); |
|
386 | ||
387 |
Resistered filters. |
|
388 | ||
389 |
=head2 C<sth> |
|
390 | ||
391 |
my $sth = $reuslt->sth |
|
392 |
$result = $result->sth($sth); |
|
393 | ||
394 |
Statement handle of L<DBI>. |
|
395 | ||
added EXPERIMENTAL execute()...
|
396 |
=head2 C<type_rule_off> EXPERIMENTAL |
397 | ||
398 |
my $type_rule_off = $result->type_rule_off; |
|
399 |
$result = $result->type_rule_off(1); |
|
400 | ||
401 |
Turn type rule off. |
|
402 | ||
update document
|
403 |
=head1 METHODS |
404 | ||
renamed build_query to creat...
|
405 |
L<DBIx::Custom::Result> inherits all methods from L<Object::Simple> |
cleanup
|
406 |
and implements the following new ones. |
packaging one directory
|
407 | |
updated pod
|
408 |
=head2 C<all> |
409 | ||
410 |
my $rows = $result->all; |
|
411 | ||
412 |
This is alias for C<fetch_hash_all>. |
|
413 | ||
- removed DEPRECATED DBIx::C...
|
414 |
=head2 C<end_filter> |
added experimental DBIx::Cus...
|
415 | |
all filter can receive array...
|
416 |
$result = $result->end_filter(title => 'to_something', |
added experimental DBIx::Cus...
|
417 |
author => 'to_something'); |
added experimental DBIx::Cus...
|
418 | |
all filter can receive array...
|
419 |
$result = $result->end_filter([qw/title author/] => 'to_something'); |
420 | ||
added experimental DBIx::Cus...
|
421 |
End filters. |
422 |
These each filters is executed after the filters applied by C<apply_filter> of |
|
423 |
L<DBIx::Custom> or C<filter> method. |
|
424 | ||
removed DBIx::Custom commit ...
|
425 |
=head2 C<fetch> |
packaging one directory
|
426 | |
cleanup
|
427 |
my $row = $result->fetch; |
version 0.0901
|
428 | |
cleanup
|
429 |
Fetch a row into array. |
packaging one directory
|
430 | |
removed DBIx::Custom commit ...
|
431 |
=head2 C<fetch_all> |
packaging one directory
|
432 | |
cleanup
|
433 |
my $rows = $result->fetch_all; |
version 0.0901
|
434 | |
removed DESTROY method(not b...
|
435 |
Fetch all rows into array of array. |
packaging one directory
|
436 | |
cleanup
|
437 |
=head2 C<fetch_first> |
438 | ||
439 |
my $row = $result->fetch_first; |
|
440 | ||
441 |
Fetch only a first row into array and finish statment handle. |
|
442 | ||
removed DESTROY method(not b...
|
443 |
=head2 C<fetch_hash> |
packaging one directory
|
444 | |
cleanup
|
445 |
my $row = $result->fetch_hash; |
packaging one directory
|
446 | |
removed DESTROY method(not b...
|
447 |
Fetch a row into hash |
update document
|
448 | |
cleanup
|
449 |
=head2 C<fetch_hash_all> |
450 | ||
451 |
my $rows = $result->fetch_hash_all; |
|
452 | ||
453 |
Fetch all rows into array of hash. |
|
454 | ||
removed DBIx::Custom commit ...
|
455 |
=head2 C<fetch_hash_first> |
removed reconnect method
|
456 |
|
cleanup
|
457 |
my $row = $result->fetch_hash_first; |
packaging one directory
|
458 | |
removed DESTROY method(not b...
|
459 |
Fetch only first row into hash and finish statment handle. |
packaging one directory
|
460 | |
removed DESTROY method(not b...
|
461 |
=head2 C<fetch_hash_multi> |
update document
|
462 | |
cleanup
|
463 |
my $rows = $result->fetch_hash_multi(5); |
update document
|
464 |
|
removed DESTROY method(not b...
|
465 |
Fetch multiple rows into array of hash |
cleanup
|
466 |
Row count must be specified. |
update document
|
467 | |
cleanup
|
468 |
=head2 C<fetch_multi> |
packaging one directory
|
469 | |
cleanup
|
470 |
my $rows = $result->fetch_multi(5); |
471 |
|
|
472 |
Fetch multiple rows into array of array. |
|
473 |
Row count must be specified. |
|
removed DESTROY method(not b...
|
474 | |
cleanup
|
475 |
=head2 C<filter> |
476 | ||
added experimental DBIx::Cus...
|
477 |
$result = $result->filter(title => 'to_something', |
478 |
author => 'to_something'); |
|
added experimental DBIx::Cus...
|
479 | |
all filter can receive array...
|
480 |
$result = $result->filter([qw/title author/] => 'to_something'); |
481 | ||
added experimental DBIx::Cus...
|
482 |
Filters. |
483 |
These each filters override the filters applied by C<apply_filter> of |
|
484 |
L<DBIx::Custom>. |
|
cleanup
|
485 | |
updated pod
|
486 |
=head2 C<one> |
487 | ||
488 |
my $row = $result->one; |
|
489 | ||
490 |
This is alias for C<fetch_hash_first>. |
|
491 | ||
- removed DEPRECATED DBIx::C...
|
492 |
=head2 C<remove_end_filter> |
added experimental DBIx::Cus...
|
493 | |
494 |
$result->remove_end_filter; |
|
495 | ||
496 |
Remove end filter. |
|
497 | ||
- removed DEPRECATED DBIx::C...
|
498 |
=head2 C<remove_filter> |
added experimental DBIx::Cus...
|
499 | |
500 |
$result->remove_filter; |
|
501 | ||
502 |
Remove filter. End filter is not removed. |
|
503 | ||
- removed DEPRECATED DBIx::C...
|
504 |
=head2 C<stash> |
added experimental DBIx::Cus...
|
505 | |
506 |
my $stash = $result->stash; |
|
507 |
my $foo = $result->stash->{foo}; |
|
508 |
$result->stash->{foo} = $foo; |
|
509 | ||
510 |
Stash is hash reference to save your data. |
|
511 | ||
packaging one directory
|
512 |
=cut |