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