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