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