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