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