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'; |
all filter can receive array...
|
9 |
use DBIx::Custom::Util; |
packaging one directory
|
10 | |
added experimental DBIx::Cus...
|
11 |
__PACKAGE__->attr( |
12 |
[qw/filters sth/], |
|
13 |
stash => sub { {} } |
|
14 |
); |
|
cleanup
|
15 | |
16 |
sub filter { |
|
17 |
my $self = shift; |
|
cleanup
|
18 |
|
19 |
if (@_) { |
|
all filter can receive array...
|
20 |
my $filter = {}; |
cleanup
|
21 |
|
all filter can receive array...
|
22 |
if (ref $_[0] eq 'HASH') { |
23 |
$filter = $_[0]; |
|
24 |
} |
|
25 |
else { |
|
cleanup
|
26 |
$filter = DBIx::Custom::Util::array_to_hash( |
all filter can receive array...
|
27 |
@_ > 1 ? [@_] : $_[0] |
28 |
); |
|
29 |
} |
|
30 |
|
|
cleanup
|
31 |
foreach my $column (keys %$filter) { |
32 |
my $fname = $filter->{$column}; |
|
fix bug : filter can't over...
|
33 | |
34 |
if (exists $filter->{$column} |
|
35 |
&& defined $fname |
|
36 |
&& ref $fname ne 'CODE') |
|
37 |
{ |
|
many changed
|
38 |
croak qq{Filter "$fname" is not registered"} |
improved error messages
|
39 |
. qq{ (DBIx::Custom::Result::filter)} |
cleanup
|
40 |
unless exists $self->filters->{$fname}; |
41 |
|
|
42 |
$filter->{$column} = $self->filters->{$fname}; |
|
43 |
} |
|
cleanup
|
44 |
} |
cleanup
|
45 |
|
added experimental DBIx::Cus...
|
46 |
$self->{filter} = {%{$self->filter}, %$filter}; |
cleanup
|
47 |
|
48 |
return $self; |
|
cleanup
|
49 |
} |
50 |
|
|
added experimental DBIx::Cus...
|
51 |
return $self->{filter} ||= {}; |
52 |
} |
|
53 | ||
54 |
sub end_filter { |
|
55 |
my $self = shift; |
|
56 |
|
|
57 |
if (@_) { |
|
all filter can receive array...
|
58 |
my $end_filter = {}; |
59 |
|
|
60 |
if (ref $_[0] eq 'HASH') { |
|
61 |
$end_filter = $_[0]; |
|
62 |
} |
|
63 |
else { |
|
cleanup
|
64 |
$end_filter = DBIx::Custom::Util::array_to_hash( |
all filter can receive array...
|
65 |
@_ > 1 ? [@_] : $_[0] |
66 |
); |
|
67 |
} |
|
added experimental DBIx::Cus...
|
68 |
|
69 |
foreach my $column (keys %$end_filter) { |
|
70 |
my $fname = $end_filter->{$column}; |
|
fix bug : filter can't over...
|
71 |
|
72 |
if (exists $end_filter->{$column} |
|
73 |
&& defined $fname |
|
74 |
&& ref $fname ne 'CODE') |
|
75 |
{ |
|
many changed
|
76 |
croak qq{Filter "$fname" is not registered"} |
improved error messages
|
77 |
. qq{ (DBIx::Custom::Result::end_filter)} |
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 |
improved error messages
|
218 |
croak 'Row count must be specified (DBIx::Custom::Result::fetch_hash_multi)' |
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 |
improved error messages
|
237 |
croak 'Row count must be specified (DBIx::Custom::Result::fetch_multi)' |
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 experimental DBIx::Cus...
|
252 |
sub remove_end_filter { |
253 |
my $self = shift; |
|
254 |
|
|
255 |
$self->{end_filter} = {}; |
|
256 |
|
|
257 |
return $self; |
|
258 |
} |
|
259 | ||
added experimental DBIx::Cus...
|
260 |
sub remove_filter { |
261 |
my $self = shift; |
|
262 |
|
|
263 |
$self->{filter} = {}; |
|
264 |
|
|
265 |
return $self; |
|
266 |
} |
|
267 | ||
cleanup
|
268 |
# Deprecated |
269 |
sub default_filter { |
|
270 |
my $self = shift; |
|
271 |
|
|
272 |
if (@_) { |
|
273 |
my $fname = $_[0]; |
|
274 |
if (@_ && !$fname) { |
|
275 |
$self->{default_filter} = undef; |
|
276 |
} |
|
277 |
else { |
|
many changed
|
278 |
croak qq{Filter "$fname" is not registered} |
cleanup
|
279 |
unless exists $self->filters->{$fname}; |
280 |
|
|
281 |
$self->{default_filter} = $self->filters->{$fname}; |
|
282 |
} |
|
283 |
|
|
284 |
return $self; |
|
285 |
} |
|
286 |
|
|
287 |
return $self->{default_filter}; |
|
288 |
} |
|
289 | ||
cleanup
|
290 |
# DEPRECATED! |
291 |
__PACKAGE__->attr('filter_check'); |
|
292 | ||
update document
|
293 |
1; |
294 | ||
packaging one directory
|
295 |
=head1 NAME |
296 | ||
cleanup
|
297 |
DBIx::Custom::Result - Result of select statement |
packaging one directory
|
298 | |
update document
|
299 |
=head1 SYNOPSIS |
cleanup
|
300 | |
301 |
Get the result of select statement. |
|
302 | ||
removed reconnect method
|
303 |
# Result |
304 |
my $result = $dbi->select(table => 'books'); |
|
cleanup
|
305 | |
306 |
Fetch row into array. |
|
removed reconnect method
|
307 |
|
308 |
# Fetch a row into array |
|
309 |
while (my $row = $result->fetch) { |
|
cleanup
|
310 |
my $author = $row->[0]; |
311 |
my $title = $row->[1]; |
|
removed reconnect method
|
312 |
|
version 0.0901
|
313 |
} |
314 |
|
|
cleanup
|
315 |
# Fetch only a first row into array |
removed reconnect method
|
316 |
my $row = $result->fetch_first; |
317 |
|
|
318 |
# Fetch multiple rows into array of array |
|
319 |
while (my $rows = $result->fetch_multi(5)) { |
|
cleanup
|
320 |
my $first_author = $rows->[0][0]; |
321 |
my $first_title = $rows->[0][1]; |
|
322 |
my $second_author = $rows->[1][0]; |
|
323 |
my $second_value = $rows->[1][1]; |
|
324 |
|
|
removed reconnect method
|
325 |
} |
326 |
|
|
327 |
# Fetch all rows into array of array |
|
328 |
my $rows = $result->fetch_all; |
|
cleanup
|
329 | |
330 |
Fetch row into hash. |
|
331 | ||
332 |
# Fetch a row into hash |
|
removed reconnect method
|
333 |
while (my $row = $result->fetch_hash) { |
cleanup
|
334 |
my $title = $row->{title}; |
335 |
my $author = $row->{author}; |
|
removed reconnect method
|
336 |
|
packaging one directory
|
337 |
} |
removed reconnect method
|
338 |
|
cleanup
|
339 |
# Fetch only a first row into hash |
removed reconnect method
|
340 |
my $row = $result->fetch_hash_first; |
341 |
|
|
342 |
# Fetch multiple rows into array of hash |
|
cleanup
|
343 |
while (my $rows = $result->fetch_hash_multi(5)) { |
344 |
my $first_title = $rows->[0]{title}; |
|
345 |
my $first_author = $rows->[0]{author}; |
|
346 |
my $second_title = $rows->[1]{title}; |
|
347 |
my $second_author = $rows->[1]{author}; |
|
removed reconnect method
|
348 |
} |
349 |
|
|
350 |
# Fetch all rows into array of hash |
|
351 |
my $rows = $result->fetch_hash_all; |
|
packaging one directory
|
352 | |
update document
|
353 |
=head1 ATTRIBUTES |
packaging one directory
|
354 | |
cleanup
|
355 |
Filters when a row is fetched. |
356 |
This overwrites C<default_filter>. |
|
removed DESTROY method(not b...
|
357 | |
cleanup
|
358 |
=head2 C<filters> |
359 | ||
360 |
my $filters = $result->filters; |
|
361 |
$result = $result->filters(\%filters); |
|
362 | ||
363 |
Resistered filters. |
|
364 | ||
365 |
=head2 C<sth> |
|
366 | ||
367 |
my $sth = $reuslt->sth |
|
368 |
$result = $result->sth($sth); |
|
369 | ||
370 |
Statement handle of L<DBI>. |
|
371 | ||
update document
|
372 |
=head1 METHODS |
373 | ||
renamed build_query to creat...
|
374 |
L<DBIx::Custom::Result> inherits all methods from L<Object::Simple> |
cleanup
|
375 |
and implements the following new ones. |
packaging one directory
|
376 | |
- removed DEPRECATED DBIx::C...
|
377 |
=head2 C<end_filter> |
added experimental DBIx::Cus...
|
378 | |
all filter can receive array...
|
379 |
$result = $result->end_filter(title => 'to_something', |
added experimental DBIx::Cus...
|
380 |
author => 'to_something'); |
added experimental DBIx::Cus...
|
381 | |
all filter can receive array...
|
382 |
$result = $result->end_filter([qw/title author/] => 'to_something'); |
383 | ||
added experimental DBIx::Cus...
|
384 |
End filters. |
385 |
These each filters is executed after the filters applied by C<apply_filter> of |
|
386 |
L<DBIx::Custom> or C<filter> method. |
|
387 | ||
removed DBIx::Custom commit ...
|
388 |
=head2 C<fetch> |
packaging one directory
|
389 | |
cleanup
|
390 |
my $row = $result->fetch; |
version 0.0901
|
391 | |
cleanup
|
392 |
Fetch a row into array. |
packaging one directory
|
393 | |
removed DBIx::Custom commit ...
|
394 |
=head2 C<fetch_all> |
packaging one directory
|
395 | |
cleanup
|
396 |
my $rows = $result->fetch_all; |
version 0.0901
|
397 | |
removed DESTROY method(not b...
|
398 |
Fetch all rows into array of array. |
packaging one directory
|
399 | |
cleanup
|
400 |
=head2 C<fetch_first> |
401 | ||
402 |
my $row = $result->fetch_first; |
|
403 | ||
404 |
Fetch only a first row into array and finish statment handle. |
|
405 | ||
removed DESTROY method(not b...
|
406 |
=head2 C<fetch_hash> |
packaging one directory
|
407 | |
cleanup
|
408 |
my $row = $result->fetch_hash; |
packaging one directory
|
409 | |
removed DESTROY method(not b...
|
410 |
Fetch a row into hash |
update document
|
411 | |
cleanup
|
412 |
=head2 C<fetch_hash_all> |
413 | ||
414 |
my $rows = $result->fetch_hash_all; |
|
415 | ||
416 |
Fetch all rows into array of hash. |
|
417 | ||
removed DBIx::Custom commit ...
|
418 |
=head2 C<fetch_hash_first> |
removed reconnect method
|
419 |
|
cleanup
|
420 |
my $row = $result->fetch_hash_first; |
packaging one directory
|
421 | |
removed DESTROY method(not b...
|
422 |
Fetch only first row into hash and finish statment handle. |
packaging one directory
|
423 | |
removed DESTROY method(not b...
|
424 |
=head2 C<fetch_hash_multi> |
update document
|
425 | |
cleanup
|
426 |
my $rows = $result->fetch_hash_multi(5); |
update document
|
427 |
|
removed DESTROY method(not b...
|
428 |
Fetch multiple rows into array of hash |
cleanup
|
429 |
Row count must be specified. |
update document
|
430 | |
cleanup
|
431 |
=head2 C<fetch_multi> |
packaging one directory
|
432 | |
cleanup
|
433 |
my $rows = $result->fetch_multi(5); |
434 |
|
|
435 |
Fetch multiple rows into array of array. |
|
436 |
Row count must be specified. |
|
removed DESTROY method(not b...
|
437 | |
cleanup
|
438 |
=head2 C<filter> |
439 | ||
added experimental DBIx::Cus...
|
440 |
$result = $result->filter(title => 'to_something', |
441 |
author => 'to_something'); |
|
added experimental DBIx::Cus...
|
442 | |
all filter can receive array...
|
443 |
$result = $result->filter([qw/title author/] => 'to_something'); |
444 | ||
added experimental DBIx::Cus...
|
445 |
Filters. |
446 |
These each filters override the filters applied by C<apply_filter> of |
|
447 |
L<DBIx::Custom>. |
|
cleanup
|
448 | |
- removed DEPRECATED DBIx::C...
|
449 |
=head2 C<remove_end_filter> |
added experimental DBIx::Cus...
|
450 | |
451 |
$result->remove_end_filter; |
|
452 | ||
453 |
Remove end filter. |
|
454 | ||
- removed DEPRECATED DBIx::C...
|
455 |
=head2 C<remove_filter> |
added experimental DBIx::Cus...
|
456 | |
457 |
$result->remove_filter; |
|
458 | ||
459 |
Remove filter. End filter is not removed. |
|
460 | ||
- removed DEPRECATED DBIx::C...
|
461 |
=head2 C<stash> |
added experimental DBIx::Cus...
|
462 | |
463 |
my $stash = $result->stash; |
|
464 |
my $foo = $result->stash->{foo}; |
|
465 |
$result->stash->{foo} = $foo; |
|
466 | ||
467 |
Stash is hash reference to save your data. |
|
468 | ||
packaging one directory
|
469 |
=cut |