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