packaging one directory
|
1 |
package DBIx::Custom::Result; |
2 |
use Object::Simple; |
|
update document
|
3 | |
packaging one directory
|
4 |
use strict; |
5 |
use warnings; |
|
6 |
use Carp 'croak'; |
|
7 | ||
8 |
# Attributes |
|
bind_filter argument is chan...
|
9 |
sub _dbi : Attr {} |
packaging one directory
|
10 |
sub sth : Attr {} |
11 |
sub fetch_filter : Attr {} |
|
update document
|
12 | |
13 |
sub no_fetch_filters : Attr { type => 'array', trigger => sub { |
|
packaging one directory
|
14 |
my $self = shift; |
15 |
my $no_fetch_filters = $self->no_fetch_filters || []; |
|
16 |
my %no_fetch_filters_map = map {$_ => 1} @{$no_fetch_filters}; |
|
17 |
$self->_no_fetch_filters_map(\%no_fetch_filters_map); |
|
18 |
}} |
|
update document
|
19 | |
packaging one directory
|
20 |
sub _no_fetch_filters_map : Attr {default => sub { {} }} |
21 | ||
22 |
# Fetch (array) |
|
23 |
sub fetch { |
|
24 |
my ($self, $type) = @_; |
|
25 |
my $sth = $self->sth; |
|
26 |
my $fetch_filter = $self->fetch_filter; |
|
27 |
|
|
28 |
# Fetch |
|
29 |
my $row = $sth->fetchrow_arrayref; |
|
30 |
|
|
31 |
# Cannot fetch |
|
32 |
return unless $row; |
|
33 |
|
|
34 |
# Filter |
|
35 |
if ($fetch_filter) { |
|
36 |
my $keys = $sth->{NAME_lc}; |
|
37 |
my $types = $sth->{TYPE}; |
|
38 |
for (my $i = 0; $i < @$keys; $i++) { |
|
39 |
next if $self->_no_fetch_filters_map->{$keys->[$i]}; |
|
bind_filter argument is chan...
|
40 |
$row->[$i]= $fetch_filter->($row->[$i], $keys->[$i], $self->_dbi, |
41 |
{type => $types->[$i], sth => $sth, index => $i}); |
|
packaging one directory
|
42 |
} |
43 |
} |
|
44 |
return wantarray ? @$row : $row; |
|
45 |
} |
|
46 | ||
47 |
# Fetch (hash) |
|
48 |
sub fetch_hash { |
|
49 |
my $self = shift; |
|
50 |
my $sth = $self->sth; |
|
51 |
my $fetch_filter = $self->fetch_filter; |
|
52 |
|
|
53 |
# Fetch |
|
54 |
my $row = $sth->fetchrow_arrayref; |
|
55 |
|
|
56 |
# Cannot fetch |
|
57 |
return unless $row; |
|
58 |
|
|
59 |
# Keys |
|
60 |
my $keys = $sth->{NAME_lc}; |
|
61 |
|
|
62 |
# Filter |
|
63 |
my $row_hash = {}; |
|
64 |
if ($fetch_filter) { |
|
65 |
my $types = $sth->{TYPE}; |
|
66 |
for (my $i = 0; $i < @$keys; $i++) { |
|
67 |
if ($self->_no_fetch_filters_map->{$keys->[$i]}) { |
|
68 |
$row_hash->{$keys->[$i]} = $row->[$i]; |
|
69 |
} |
|
70 |
else { |
|
71 |
$row_hash->{$keys->[$i]} |
|
bind_filter argument is chan...
|
72 |
= $fetch_filter->($row->[$i], $keys->[$i], $self->_dbi, |
73 |
{type => $types->[$i], sth => $sth, index => $i}); |
|
packaging one directory
|
74 |
} |
75 |
} |
|
76 |
} |
|
77 |
|
|
78 |
# No filter |
|
79 |
else { |
|
80 |
for (my $i = 0; $i < @$keys; $i++) { |
|
81 |
$row_hash->{$keys->[$i]} = $row->[$i]; |
|
82 |
} |
|
83 |
} |
|
84 |
return wantarray ? %$row_hash : $row_hash; |
|
85 |
} |
|
86 | ||
87 |
# Fetch only first (array) |
|
88 |
sub fetch_first { |
|
89 |
my $self = shift; |
|
90 |
|
|
91 |
# Fetch |
|
92 |
my $row = $self->fetch; |
|
93 |
|
|
94 |
# Not exist |
|
95 |
return unless $row; |
|
96 |
|
|
97 |
# Finish statement handle |
|
98 |
$self->finish; |
|
99 |
|
|
100 |
return wantarray ? @$row : $row; |
|
101 |
} |
|
102 | ||
103 |
# Fetch only first (hash) |
|
104 |
sub fetch_hash_first { |
|
105 |
my $self = shift; |
|
106 |
|
|
107 |
# Fetch hash |
|
108 |
my $row = $self->fetch_hash; |
|
109 |
|
|
110 |
# Not exist |
|
111 |
return unless $row; |
|
112 |
|
|
113 |
# Finish statement handle |
|
114 |
$self->finish; |
|
115 |
|
|
116 |
return wantarray ? %$row : $row; |
|
117 |
} |
|
118 | ||
119 |
# Fetch multi rows (array) |
|
120 |
sub fetch_rows { |
|
121 |
my ($self, $count) = @_; |
|
122 |
|
|
123 |
# Not specified Row count |
|
124 |
croak("Row count must be specified") |
|
125 |
unless $count; |
|
126 |
|
|
127 |
# Fetch multi rows |
|
128 |
my $rows = []; |
|
129 |
for (my $i = 0; $i < $count; $i++) { |
|
130 |
my @row = $self->fetch; |
|
131 |
|
|
132 |
last unless @row; |
|
133 |
|
|
134 |
push @$rows, \@row; |
|
135 |
} |
|
136 |
|
|
137 |
return unless @$rows; |
|
138 |
return wantarray ? @$rows : $rows; |
|
139 |
} |
|
140 | ||
141 |
# Fetch multi rows (hash) |
|
142 |
sub fetch_hash_rows { |
|
143 |
my ($self, $count) = @_; |
|
144 |
|
|
145 |
# Not specified Row count |
|
146 |
croak("Row count must be specified") |
|
147 |
unless $count; |
|
148 |
|
|
149 |
# Fetch multi rows |
|
150 |
my $rows = []; |
|
151 |
for (my $i = 0; $i < $count; $i++) { |
|
152 |
my %row = $self->fetch_hash; |
|
153 |
|
|
154 |
last unless %row; |
|
155 |
|
|
156 |
push @$rows, \%row; |
|
157 |
} |
|
158 |
|
|
159 |
return unless @$rows; |
|
160 |
return wantarray ? @$rows : $rows; |
|
161 |
} |
|
162 | ||
163 | ||
164 |
# Fetch all (array) |
|
165 |
sub fetch_all { |
|
166 |
my $self = shift; |
|
167 |
|
|
168 |
my $rows = []; |
|
169 |
while(my @row = $self->fetch) { |
|
170 |
push @$rows, [@row]; |
|
171 |
} |
|
172 |
return wantarray ? @$rows : $rows; |
|
173 |
} |
|
174 | ||
175 |
# Fetch all (hash) |
|
176 |
sub fetch_hash_all { |
|
177 |
my $self = shift; |
|
178 |
|
|
179 |
my $rows = []; |
|
180 |
while(my %row = $self->fetch_hash) { |
|
181 |
push @$rows, {%row}; |
|
182 |
} |
|
183 |
return wantarray ? @$rows : $rows; |
|
184 |
} |
|
185 | ||
186 |
# Finish |
|
187 |
sub finish { shift->sth->finish } |
|
188 | ||
189 |
# Error |
|
190 |
sub error { |
|
191 |
my $self = shift; |
|
192 |
my $sth = $self->sth; |
|
193 |
return wantarray ? ($sth->errstr, $sth->err, $sth->state) : $sth->errstr; |
|
194 |
} |
|
195 | ||
196 |
Object::Simple->build_class; |
|
197 | ||
198 |
=head1 NAME |
|
199 | ||
update document
|
200 |
DBIx::Custom::Result - DBIx::Custom Resultset |
packaging one directory
|
201 | |
update document
|
202 |
=head1 Synopsis |
packaging one directory
|
203 | |
204 |
# $result is DBIx::Custom::Result object |
|
205 |
my $dbi = DBIx::Custom->new; |
|
206 |
my $result = $dbi->query($sql_template, $param); |
|
207 |
|
|
208 |
while (my ($val1, $val2) = $result->fetch) { |
|
209 |
# do something |
|
210 |
} |
|
211 | ||
update document
|
212 |
=head1 Accessors |
packaging one directory
|
213 | |
214 |
=head2 sth |
|
215 | ||
update document
|
216 |
Set and Get statement handle |
217 | ||
packaging one directory
|
218 |
$self = $result->sth($sth); |
219 |
$sth = $reuslt->sth |
|
update document
|
220 |
|
221 |
# Sample |
|
222 |
$dbi->sth->errstr |
|
packaging one directory
|
223 | |
224 |
=head2 fetch_filter |
|
225 | ||
update document
|
226 |
Set and Get fetch filter |
227 | ||
packaging one directory
|
228 |
$self = $result->fetch_filter($sth); |
229 |
$fetch_filter = $result->fech_filter; |
|
230 | ||
231 |
=head2 no_fetch_filters |
|
232 | ||
update document
|
233 |
Set and Get no filter keys when fetching |
234 | ||
packaging one directory
|
235 |
$self = $result->no_fetch_filters($no_fetch_filters); |
236 |
$no_fetch_filters = $result->no_fetch_filters; |
|
237 | ||
update document
|
238 |
=head1 Methods |
packaging one directory
|
239 | |
240 |
=head2 fetch |
|
241 | ||
update document
|
242 |
Fetch a row |
243 | ||
244 |
$row = $self->fetch; # array reference |
|
245 |
@row = $self->fecth; # array |
|
packaging one directory
|
246 | |
247 |
# Sample |
|
248 |
while (my $row = $result->fetch) { |
|
249 |
# do something |
|
250 |
my $val1 = $row->[0]; |
|
251 |
my $val2 = $row->[1]; |
|
252 |
} |
|
253 | ||
254 |
=head2 fetch_hash |
|
255 | ||
update document
|
256 |
Fetch row as hash |
257 | ||
258 |
$row = $self->fetch_hash; # hash reference |
|
cleanup and update docment
|
259 |
%row = $self->fetch_hash; # hash |
packaging one directory
|
260 | |
261 |
# Sample |
|
262 |
while (my $row = $result->fetch_hash) { |
|
263 |
# do something |
|
264 |
my $val1 = $row->{key1}; |
|
265 |
my $val2 = $row->{key2}; |
|
266 |
} |
|
267 | ||
268 |
=head2 fetch_first |
|
269 | ||
update document
|
270 |
Fetch only first row(Scalar context) |
271 | ||
272 |
$row = $self->fetch_first; # array reference |
|
273 |
@row = $self->fetch_first; # array |
|
packaging one directory
|
274 |
|
update document
|
275 |
# Sample |
276 |
$row = $result->fetch_first; |
|
packaging one directory
|
277 |
|
update document
|
278 |
This method fetch only first row and finish statement handle |
packaging one directory
|
279 | |
280 |
=head2 fetch_hash_first |
|
281 |
|
|
update document
|
282 |
Fetch only first row as hash |
283 | ||
284 |
$row = $self->fetch_hash_first; # hash reference |
|
cleanup and update docment
|
285 |
%row = $self->fetch_hash_first; # hash |
packaging one directory
|
286 |
|
update document
|
287 |
# Sample |
288 |
$row = $result->fetch_hash_first; |
|
packaging one directory
|
289 |
|
update document
|
290 |
This method fetch only first row and finish statement handle |
packaging one directory
|
291 | |
292 |
=head2 fetch_rows |
|
293 | ||
update document
|
294 |
Fetch rows |
295 | ||
296 |
$rows = $self->fetch_rows($row_count); # array ref of array ref |
|
297 |
@rows = $self->fetch_rows($row_count); # array of array ref |
|
packaging one directory
|
298 |
|
update document
|
299 |
# Sample |
300 |
while(my $rows = $result->fetch_rows(10)) { |
|
301 |
# do someting |
|
302 |
} |
|
packaging one directory
|
303 | |
304 |
=head2 fetch_hash_rows |
|
305 | ||
update document
|
306 |
Fetch rows as hash |
307 | ||
308 |
$rows = $self->fetch_hash_rows($row_count); # array ref of hash ref |
|
309 |
@rows = $self->fetch_hash_rows($row_count); # array of hash ref |
|
packaging one directory
|
310 |
|
update document
|
311 |
# Sample |
312 |
while(my $rows = $result->fetch_hash_rows(10)) { |
|
313 |
# do someting |
|
314 |
} |
|
packaging one directory
|
315 | |
316 |
=head2 fetch_all |
|
317 | ||
update document
|
318 |
Fetch all rows |
319 | ||
320 |
$rows = $self->fetch_all; # array ref of array ref |
|
321 |
@rows = $self->fecth_all; # array of array ref |
|
packaging one directory
|
322 | |
323 |
# Sample |
|
324 |
my $rows = $result->fetch_all; |
|
325 | ||
326 |
=head2 fetch_hash_all |
|
327 | ||
update document
|
328 |
Fetch all row as array ref of hash ref (Scalar context) |
329 | ||
330 |
$rows = $self->fetch_hash_all; # array ref of hash ref |
|
331 |
@rows = $self->fecth_all_hash; # array of hash ref |
|
packaging one directory
|
332 | |
333 |
# Sample |
|
334 |
my $rows = $result->fetch_hash_all; |
|
335 | ||
336 |
=head2 error |
|
337 | ||
update document
|
338 |
Get error infomation |
339 | ||
340 |
$error_messege = $self->error; |
|
341 |
($error_message, $error_number, $error_state) = $self->error; |
|
342 |
|
|
343 |
# Sample |
|
344 |
$error = $result->error; |
|
packaging one directory
|
345 | |
update document
|
346 |
You can get get information. This is same as the following. |
packaging one directory
|
347 | |
348 |
$error_message : $result->sth->errstr |
|
349 |
$error_number : $result->sth->err |
|
350 |
$error_state : $result->sth->state |
|
351 | ||
352 |
=head2 finish |
|
353 | ||
update document
|
354 |
Finish statement handle |
355 | ||
356 |
$ret_val = $self->finish |
|
packaging one directory
|
357 |
|
358 |
# Sample |
|
update document
|
359 |
my $row = $reuslt->fetch; # fetch a row |
packaging one directory
|
360 |
$result->finish |
361 | ||
update document
|
362 |
This is equel to |
packaging one directory
|
363 | |
364 |
$result->sth->finish; |
|
365 | ||
update document
|
366 |
=head1 See also |
367 | ||
368 |
L<DBIx::Custom> |
|
369 | ||
370 |
=head1 Author |
|
packaging one directory
|
371 | |
372 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
373 | ||
374 |
Github L<http://github.com/yuki-kimoto> |
|
375 | ||
update document
|
376 |
=head1 Copyright & licence |
packaging one directory
|
377 | |
378 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
379 | ||
380 |
This program is free software; you can redistribute it and/or modify it |
|
381 |
under the same terms as Perl itself. |
|
382 | ||
383 |
=cut |