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