added common test executing ...
|
1 |
package DBIx::Custom::Result; |
2 |
use Object::Simple -base; |
|
3 | ||
4 |
use Carp 'croak'; |
|
5 |
use DBIx::Custom::Util qw/_array_to_hash _subname/; |
|
6 | ||
7 |
has [qw/dbi sth/], |
|
8 |
stash => sub { {} }; |
|
9 | ||
10 |
*all = \&fetch_hash_all; |
|
11 | ||
12 |
sub filter { |
|
13 |
my $self = shift; |
|
14 |
|
|
15 |
# Set |
|
16 |
if (@_) { |
|
17 |
|
|
18 |
# Convert filter name to subroutine |
|
19 |
my $filter = @_ == 1 ? $_[0] : [@_]; |
|
20 |
$filter = _array_to_hash($filter); |
|
21 |
foreach my $column (keys %$filter) { |
|
22 |
my $fname = $filter->{$column}; |
|
23 |
if (exists $filter->{$column} |
|
24 |
&& defined $fname |
|
25 |
&& ref $fname ne 'CODE') |
|
26 |
{ |
|
27 |
croak qq{Filter "$fname" is not registered" } . _subname |
|
28 |
unless exists $self->dbi->filters->{$fname}; |
|
29 |
$filter->{$column} = $self->dbi->filters->{$fname}; |
|
30 |
} |
|
31 |
} |
|
32 |
|
|
33 |
# Merge |
|
34 |
$self->{filter} = {%{$self->filter}, %$filter}; |
|
35 |
|
|
36 |
return $self; |
|
37 |
} |
|
38 |
|
|
39 |
return $self->{filter} ||= {}; |
|
40 |
} |
|
41 | ||
42 |
sub filter_off { |
|
43 |
my $self = shift; |
|
44 |
$self->{filter_off} = 1; |
|
45 |
return $self; |
|
46 |
} |
|
47 | ||
48 |
sub filter_on { |
|
49 |
my $self = shift; |
|
50 |
$self->{filter_off} = 0; |
|
51 |
return $self; |
|
52 |
} |
|
53 | ||
54 |
sub fetch { |
|
55 |
my $self = shift; |
|
56 |
|
|
57 |
# Info |
|
58 |
my $columns = $self->{sth}->{NAME}; |
|
59 |
my $types = $self->{sth}->{TYPE}; |
|
60 |
|
|
61 |
# Fetch |
|
62 |
my @row = $self->{sth}->fetchrow_array; |
|
63 |
return unless @row; |
|
64 |
|
|
65 |
# Filtering |
|
66 |
my $type_rule1 = $self->type_rule->{from1} || {}; |
|
67 |
my $type_rule2 = $self->type_rule->{from2} || {}; |
|
68 |
my $filter = $self->filter; |
|
69 |
my $end_filter = $self->{end_filter} || {}; |
|
70 |
for (my $i = 0; $i < @$columns; $i++) { |
|
71 |
|
|
72 |
# Column |
|
73 |
my $column = $columns->[$i]; |
|
74 |
|
|
75 |
# Type rule |
|
76 |
my $type_filter1 = $type_rule1->{lc($types->[$i])}; |
|
77 |
$row[$i] = $type_filter1->($row[$i]) |
|
78 |
if $type_filter1 && !$self->{type_rule_off} |
|
79 |
&& !$self->{type_rule1_off}; |
|
80 |
my $type_filter2 = $type_rule2->{lc($types->[$i])}; |
|
81 |
$row[$i] = $type_filter2->($row[$i]) |
|
82 |
if $type_filter2 && !$self->{type_rule_off} |
|
83 |
&& !$self->{type_rule2_off}; |
|
84 |
|
|
85 |
# Filter |
|
86 |
my $filter = $filter->{$column} || $self->{default_filter}; |
|
87 |
$row[$i] = $filter->($row[$i]) |
|
88 |
if $filter && !$self->{filter_off}; |
|
89 |
$row[$i] = $end_filter->{$column}->($row[$i]) |
|
90 |
if $end_filter->{$column} && !$self->{filter_off}; |
|
91 |
} |
|
92 | ||
93 |
return \@row; |
|
94 |
} |
|
95 | ||
96 |
sub fetch_all { |
|
97 |
my $self = shift; |
|
98 |
|
|
99 |
# Fetch all rows |
|
100 |
my $rows = []; |
|
101 |
while(my $row = $self->fetch) { push @$rows, $row} |
|
102 |
|
|
103 |
return $rows; |
|
104 |
} |
|
105 | ||
106 |
sub fetch_first { |
|
107 |
my $self = shift; |
|
108 |
|
|
109 |
# Fetch |
|
110 |
my $row = $self->fetch; |
|
111 |
return unless $row; |
|
112 |
|
|
113 |
# Finish statement handle |
|
114 |
$self->sth->finish; |
|
115 |
|
|
116 |
return $row; |
|
117 |
} |
|
118 | ||
119 |
sub fetch_hash { |
|
120 |
my $self = shift; |
|
121 |
|
|
122 |
# Info |
|
123 |
my $columns = $self->{sth}->{NAME}; |
|
124 |
my $types = $self->{sth}->{TYPE}; |
|
125 |
|
|
126 |
# Fetch |
|
127 |
my $row = $self->{sth}->fetchrow_arrayref; |
|
128 |
return unless $row; |
|
129 | ||
130 |
# Filter |
|
131 |
my $hash_row = {}; |
|
132 |
my $filter = $self->filter; |
|
133 |
my $end_filter = $self->{end_filter} || {}; |
|
134 |
my $type_rule1 = $self->type_rule->{from1} || {}; |
|
135 |
my $type_rule2 = $self->type_rule->{from2} || {}; |
|
136 |
for (my $i = 0; $i < @$columns; $i++) { |
|
137 |
|
|
138 |
# Column |
|
139 |
my $column = $columns->[$i]; |
|
140 |
$hash_row->{$column} = $row->[$i]; |
|
141 |
|
|
142 |
# Type rule |
|
143 |
my $type_filter1 = $type_rule1->{lc($types->[$i])}; |
|
144 |
$hash_row->{$column} = $type_filter1->($hash_row->{$column}) |
|
145 |
if !$self->{type_rule_off} && !$self->{type_rule1_off} |
|
146 |
&& $type_filter1; |
|
147 |
my $type_filter2 = $type_rule2->{lc($types->[$i])}; |
|
148 |
$hash_row->{$column} = $type_filter2->($hash_row->{$column}) |
|
149 |
if !$self->{type_rule_off} && !$self->{type_rule2_off} |
|
150 |
&& $type_filter2; |
|
151 |
|
|
152 |
# Filter |
|
153 |
my $f = $filter->{$column} || $self->{default_filter}; |
|
154 |
$hash_row->{$column} = $f->($hash_row->{$column}) |
|
155 |
if $f && !$self->{filter_off}; |
|
156 |
$hash_row->{$column} = $end_filter->{$column}->($hash_row->{$column}) |
|
157 |
if $end_filter->{$column} && !$self->{filter_off}; |
|
158 |
} |
|
159 |
|
|
160 |
return $hash_row; |
|
161 |
} |
|
162 | ||
163 |
sub fetch_hash_all { |
|
164 |
my $self = shift; |
|
165 |
|
|
166 |
# Fetch all rows as hash |
|
167 |
my $rows = []; |
|
168 |
while(my $row = $self->fetch_hash) { push @$rows, $row } |
|
169 |
|
|
170 |
return $rows; |
|
171 |
} |
|
172 | ||
173 |
sub fetch_hash_first { |
|
174 |
my $self = shift; |
|
175 |
|
|
176 |
# Fetch hash |
|
177 |
my $row = $self->fetch_hash; |
|
178 |
return unless $row; |
|
179 |
|
|
180 |
# Finish statement handle |
|
181 |
$self->sth->finish; |
|
182 |
|
|
183 |
return $row; |
|
184 |
} |
|
185 | ||
186 |
sub fetch_hash_multi { |
|
187 |
my ($self, $count) = @_; |
|
188 |
|
|
189 |
# Fetch multiple rows |
|
190 |
croak 'Row count must be specified ' . _subname |
|
191 |
unless $count; |
|
192 |
my $rows = []; |
|
193 |
for (my $i = 0; $i < $count; $i++) { |
|
194 |
my $row = $self->fetch_hash; |
|
195 |
last unless $row; |
|
196 |
push @$rows, $row; |
|
197 |
} |
|
198 |
|
|
199 |
return unless @$rows; |
|
200 |
return $rows; |
|
201 |
} |
|
202 | ||
203 |
sub fetch_multi { |
|
204 |
my ($self, $count) = @_; |
|
205 |
|
|
206 |
# Row count not specifed |
|
207 |
croak 'Row count must be specified ' . _subname |
|
208 |
unless $count; |
|
209 |
|
|
210 |
# Fetch multi rows |
|
211 |
my $rows = []; |
|
212 |
for (my $i = 0; $i < $count; $i++) { |
|
213 |
my $row = $self->fetch; |
|
214 |
last unless $row; |
|
215 |
push @$rows, $row; |
|
216 |
} |
|
217 |
|
|
218 |
return unless @$rows; |
|
219 |
return $rows; |
|
220 |
} |
|
221 | ||
222 |
sub header { shift->sth->{NAME} } |
|
223 | ||
224 |
*one = \&fetch_hash_first; |
|
225 | ||
226 |
sub type_rule { |
|
227 |
my $self = shift; |
|
228 |
|
|
229 |
if (@_) { |
|
230 |
my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
231 | ||
232 |
# From |
|
233 |
foreach my $i (1 .. 2) { |
|
234 |
$type_rule->{"from$i"} = _array_to_hash($type_rule->{"from$i"}); |
|
235 |
foreach my $data_type (keys %{$type_rule->{"from$i"} || {}}) { |
|
236 |
croak qq{data type of from$i section must be lower case or number} |
|
237 |
if $data_type =~ /[A-Z]/; |
|
238 |
my $fname = $type_rule->{"from$i"}{$data_type}; |
|
239 |
if (defined $fname && ref $fname ne 'CODE') { |
|
240 |
croak qq{Filter "$fname" is not registered" } . _subname |
|
241 |
unless exists $self->dbi->filters->{$fname}; |
|
242 |
|
|
243 |
$type_rule->{"from$i"}{$data_type} = $self->dbi->filters->{$fname}; |
|
244 |
} |
|
245 |
} |
|
246 |
} |
|
247 |
$self->{type_rule} = $type_rule; |
|
248 |
|
|
249 |
return $self; |
|
250 |
} |
|
251 |
|
|
252 |
return $self->{type_rule} || {}; |
|
253 |
} |
|
254 | ||
255 |
sub type_rule_off { |
|
256 |
my $self = shift; |
|
257 |
$self->{type_rule_off} = 1; |
|
258 |
return $self; |
|
259 |
} |
|
260 | ||
261 |
sub type_rule_on { |
|
262 |
my $self = shift; |
|
263 |
$self->{type_rule_off} = 0; |
|
264 |
return $self; |
|
265 |
} |
|
266 | ||
267 |
sub type_rule1_off { |
|
268 |
my $self = shift; |
|
269 |
$self->{type_rule1_off} = 1; |
|
270 |
return $self; |
|
271 |
} |
|
272 | ||
273 |
sub type_rule1_on { |
|
274 |
my $self = shift; |
|
275 |
$self->{type_rule1_off} = 0; |
|
276 |
return $self; |
|
277 |
} |
|
278 | ||
279 |
sub type_rule2_off { |
|
280 |
my $self = shift; |
|
281 |
$self->{type_rule2_off} = 1; |
|
282 |
return $self; |
|
283 |
} |
|
284 | ||
285 |
sub type_rule2_on { |
|
286 |
my $self = shift; |
|
287 |
$self->{type_rule2_off} = 0; |
|
288 |
return $self; |
|
289 |
} |
|
290 | ||
291 |
# DEPRECATED! |
|
292 |
sub end_filter { |
|
293 |
warn "end_filter method is DEPRECATED!"; |
|
294 |
my $self = shift; |
|
295 |
if (@_) { |
|
296 |
my $end_filter = {}; |
|
297 |
if (ref $_[0] eq 'HASH') { $end_filter = $_[0] } |
|
298 |
else { |
|
299 |
$end_filter = _array_to_hash( |
|
300 |
@_ > 1 ? [@_] : $_[0] |
|
301 |
); |
|
302 |
} |
|
303 |
foreach my $column (keys %$end_filter) { |
|
304 |
my $fname = $end_filter->{$column}; |
|
305 |
if (exists $end_filter->{$column} |
|
306 |
&& defined $fname |
|
307 |
&& ref $fname ne 'CODE') |
|
308 |
{ |
|
309 |
croak qq{Filter "$fname" is not registered" } . _subname |
|
310 |
unless exists $self->dbi->filters->{$fname}; |
|
311 |
$end_filter->{$column} = $self->dbi->filters->{$fname}; |
|
312 |
} |
|
313 |
} |
|
314 |
$self->{end_filter} = {%{$self->end_filter}, %$end_filter}; |
|
315 |
return $self; |
|
316 |
} |
|
317 |
return $self->{end_filter} ||= {}; |
|
318 |
} |
|
319 |
# DEPRECATED! |
|
320 |
sub remove_end_filter { |
|
321 |
warn "remove_end_filter is DEPRECATED!"; |
|
322 |
my $self = shift; |
|
323 |
$self->{end_filter} = {}; |
|
324 |
return $self; |
|
325 |
} |
|
326 |
# DEPRECATED! |
|
327 |
sub remove_filter { |
|
328 |
warn "remove_filter is DEPRECATED!"; |
|
329 |
my $self = shift; |
|
330 |
$self->{filter} = {}; |
|
331 |
return $self; |
|
332 |
} |
|
333 |
# DEPRECATED! |
|
334 |
sub default_filter { |
|
335 |
warn "default_filter is DEPRECATED!"; |
|
336 |
my $self = shift; |
|
337 |
if (@_) { |
|
338 |
my $fname = $_[0]; |
|
339 |
if (@_ && !$fname) { |
|
340 |
$self->{default_filter} = undef; |
|
341 |
} |
|
342 |
else { |
|
343 |
croak qq{Filter "$fname" is not registered} |
|
344 |
unless exists $self->dbi->filters->{$fname}; |
|
345 |
$self->{default_filter} = $self->dbi->filters->{$fname}; |
|
346 |
} |
|
347 |
return $self; |
|
348 |
} |
|
349 |
return $self->{default_filter}; |
|
350 |
} |
|
351 |
# DEPRECATED! |
|
352 |
has 'filter_check'; |
|
353 | ||
354 |
1; |
|
355 | ||
356 |
=head1 NAME |
|
357 | ||
358 |
DBIx::Custom::Result - Result of select statement |
|
359 | ||
360 |
=head1 SYNOPSIS |
|
361 | ||
362 |
# Result |
|
363 |
my $result = $dbi->select(table => 'book'); |
|
364 | ||
365 |
# Fetch a row and put it into array reference |
|
366 |
while (my $row = $result->fetch) { |
|
367 |
my $author = $row->[0]; |
|
368 |
my $title = $row->[1]; |
|
369 |
} |
|
370 |
|
|
371 |
# Fetch only a first row and put it into array reference |
|
372 |
my $row = $result->fetch_first; |
|
373 |
|
|
374 |
# Fetch all rows and put them into array of array reference |
|
375 |
my $rows = $result->fetch_all; |
|
376 | ||
377 |
# Fetch a row and put it into hash reference |
|
378 |
while (my $row = $result->fetch_hash) { |
|
379 |
my $title = $row->{title}; |
|
380 |
my $author = $row->{author}; |
|
381 |
} |
|
382 |
|
|
383 |
# Fetch only a first row and put it into hash reference |
|
384 |
my $row = $result->fetch_hash_first; |
|
385 |
my $row = $result->one; # Same as fetch_hash_first |
|
386 |
|
|
387 |
# Fetch all rows and put them into array of hash reference |
|
388 |
my $rows = $result->fetch_hash_all; |
|
389 |
my $rows = $result->all; # Same as fetch_hash_all |
|
390 | ||
391 |
=head1 ATTRIBUTES |
|
392 | ||
393 |
=head2 C<dbi> |
|
394 | ||
395 |
my $dbi = $result->dbi; |
|
396 |
$result = $result->dbi($dbi); |
|
397 | ||
398 |
L<DBIx::Custom> object. |
|
399 | ||
400 |
=head2 C<sth> |
|
401 | ||
402 |
my $sth = $reuslt->sth |
|
403 |
$result = $result->sth($sth); |
|
404 | ||
405 |
Statement handle of L<DBI>. |
|
406 | ||
407 |
=head1 METHODS |
|
408 | ||
409 |
L<DBIx::Custom::Result> inherits all methods from L<Object::Simple> |
|
410 |
and implements the following new ones. |
|
411 | ||
412 |
=head2 C<all> |
|
413 | ||
414 |
my $rows = $result->all; |
|
415 | ||
416 |
Same as C<fetch_hash_all>. |
|
417 | ||
418 |
=head2 C<fetch> |
|
419 | ||
420 |
my $row = $result->fetch; |
|
421 | ||
422 |
Fetch a row and put it into array reference. |
|
423 | ||
424 |
=head2 C<fetch_all> |
|
425 | ||
426 |
my $rows = $result->fetch_all; |
|
427 | ||
428 |
Fetch all rows and put them into array of array reference. |
|
429 | ||
430 |
=head2 C<fetch_first> |
|
431 | ||
432 |
my $row = $result->fetch_first; |
|
433 | ||
434 |
Fetch only a first row and put it into array reference, |
|
435 |
and finish statment handle. |
|
436 | ||
437 |
=head2 C<fetch_hash> |
|
438 | ||
439 |
my $row = $result->fetch_hash; |
|
440 | ||
441 |
Fetch a row and put it into hash reference. |
|
442 | ||
443 |
=head2 C<fetch_hash_all> |
|
444 | ||
445 |
my $rows = $result->fetch_hash_all; |
|
446 | ||
447 |
Fetch all rows and put them into array of hash reference. |
|
448 | ||
449 |
=head2 C<fetch_hash_first> |
|
450 |
|
|
451 |
my $row = $result->fetch_hash_first; |
|
452 | ||
453 |
Fetch only a first row and put it into hash reference, |
|
454 |
and finish statment handle. |
|
455 | ||
456 |
=head2 C<fetch_hash_multi> |
|
457 | ||
458 |
my $rows = $result->fetch_hash_multi(5); |
|
459 |
|
|
460 |
Fetch multiple rows and put them into array of hash reference. |
|
461 | ||
462 |
=head2 C<fetch_multi> |
|
463 | ||
464 |
my $rows = $result->fetch_multi(5); |
|
465 |
|
|
466 |
Fetch multiple rows and put them into array of array reference. |
|
467 | ||
468 |
=head2 C<filter> |
|
469 | ||
470 |
$result->filter(title => sub { uc $_[0] }, author => 'to_upper'); |
|
471 |
$result->filter([qw/title author/] => 'to_upper'); |
|
472 | ||
473 |
Set filter for column. |
|
474 |
You can use subroutine or filter name as filter. |
|
475 |
This filter is executed after C<type_rule> filter. |
|
476 | ||
477 |
=head2 C<filter_off> EXPERIMENTAL |
|
478 | ||
479 |
$result = $result->filter_off; |
|
480 | ||
481 |
Turn filtering by C<filter> method off. |
|
482 |
By default, filterin is on. |
|
483 | ||
484 |
=head2 C<filter_on> EXPERIMENTAL |
|
485 | ||
486 |
$result = $resutl->filter_on; |
|
487 | ||
488 |
Turn filtering by C<filter> method on. |
|
489 |
By default, filterin is on. |
|
490 | ||
491 |
=head2 C<header> |
|
492 | ||
493 |
my $header = $result->header; |
|
494 | ||
495 |
Get header column names. |
|
496 | ||
497 |
=head2 C<one> |
|
498 | ||
499 |
my $row = $result->one; |
|
500 | ||
501 |
Same as C<fetch_hash_first>. |
|
502 | ||
503 |
=head2 C<stash> |
|
504 | ||
505 |
my $stash = $result->stash; |
|
506 |
my $foo = $result->stash->{foo}; |
|
507 |
$result->stash->{foo} = $foo; |
|
508 | ||
509 |
Stash is hash reference for data. |
|
510 | ||
511 |
=head2 C<type_rule> EXPERIMENTAL |
|
512 |
|
|
513 |
# Merge type rule |
|
514 |
$result->type_rule( |
|
515 |
# DATE |
|
516 |
9 => sub { ... }, |
|
517 |
# DATETIME or TIMESTAMP |
|
518 |
11 => sub { ... } |
|
519 |
); |
|
520 | ||
521 |
# Replace type rule(by reference) |
|
522 |
$result->type_rule([ |
|
523 |
# DATE |
|
524 |
9 => sub { ... }, |
|
525 |
# DATETIME or TIMESTAMP |
|
526 |
11 => sub { ... } |
|
527 |
]); |
|
528 | ||
529 |
This is same as L<DBIx::Custom>'s C<type_rule>'s <from>. |
|
530 | ||
531 |
=head2 C<type_rule_off> EXPERIMENTAL |
|
532 | ||
533 |
$result = $result->type_rule_off; |
|
534 | ||
535 |
Turn C<from1> and C<from2> type rule off. |
|
536 |
By default, type rule is on. |
|
537 | ||
538 |
=head2 C<type_rule_on> EXPERIMENTAL |
|
539 | ||
540 |
$result = $result->type_rule_on; |
|
541 | ||
542 |
Turn C<from1> and C<from2> type rule on. |
|
543 |
By default, type rule is on. |
|
544 | ||
545 |
=head2 C<type_rule1_off> EXPERIMENTAL |
|
546 | ||
547 |
$result = $result->type_rule1_off; |
|
548 | ||
549 |
Turn C<from1> type rule off. |
|
550 |
By default, type rule is on. |
|
551 | ||
552 |
=head2 C<type_rule1_on> EXPERIMENTAL |
|
553 | ||
554 |
$result = $result->type_rule1_on; |
|
555 | ||
556 |
Turn C<from1> type rule on. |
|
557 |
By default, type rule is on. |
|
558 | ||
559 |
=head2 C<type_rule2_off> EXPERIMENTAL |
|
560 | ||
561 |
$result = $result->type_rule2_off; |
|
562 | ||
563 |
Turn C<from2> type rule off. |
|
564 |
By default, type rule is on. |
|
565 | ||
566 |
=head2 C<type_rule2_on> EXPERIMENTAL |
|
567 | ||
568 |
$result = $result->type_rule2_on; |
|
569 | ||
570 |
Turn C<from2> type rule on. |
|
571 |
By default, type rule is on. |
|
572 | ||
573 |
=cut |