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