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