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 | ||
many many changes
|
10 |
__PACKAGE__->attr([qw/sth filters default_filter filter/]); |
cleanup
|
11 | |
packaging one directory
|
12 |
sub fetch { |
update document
|
13 | |
14 |
$_[0]->{filters} ||= {}; |
|
15 |
$_[0]->{filter} ||= {}; |
|
packaging one directory
|
16 |
|
17 |
# Fetch |
|
update document
|
18 |
my @row = $_[0]->{sth}->fetchrow_array; |
packaging one directory
|
19 |
|
20 |
# Cannot fetch |
|
update document
|
21 |
return unless @row; |
many many changes
|
22 | |
packaging one directory
|
23 |
# Filter |
update document
|
24 |
for (my $i = 0; $i < @{$_[0]->{sth}->{NAME_lc}}; $i++) { |
25 |
my $fname = $_[0]->{filter}->{$_[0]->{sth}->{NAME_lc}->[$i]} |
|
26 |
|| $_[0]->{default_filter}; |
|
27 |
|
|
28 |
croak "Filter \"$fname\" is not registered." |
|
29 |
if $fname && ! exists $_[0]->{filters}->{$fname}; |
|
some changed
|
30 |
|
update document
|
31 |
next unless $fname; |
32 |
|
|
33 |
$row[$i] = ref $fname |
|
34 |
? $fname->($row[$i]) |
|
35 |
: $_[0]->{filters}->{$fname}->($row[$i]); |
|
packaging one directory
|
36 |
} |
many many changes
|
37 | |
removed reconnect method
|
38 |
return \@row; |
39 |
} |
|
40 | ||
41 |
sub fetch_first { |
|
42 |
my $self = shift; |
|
43 |
|
|
44 |
# Fetch |
|
45 |
my $row = $self->fetch; |
|
46 |
|
|
47 |
# Not exist |
|
48 |
return unless $row; |
|
49 |
|
|
50 |
# Finish statement handle |
|
51 |
$self->sth->finish; |
|
52 |
|
|
53 |
return $row; |
|
54 |
} |
|
55 | ||
56 |
sub fetch_multi { |
|
57 |
my ($self, $count) = @_; |
|
58 |
|
|
59 |
# Not specified Row count |
|
60 |
croak("Row count must be specified") |
|
61 |
unless $count; |
|
62 |
|
|
63 |
# Fetch multi rows |
|
64 |
my $rows = []; |
|
65 |
for (my $i = 0; $i < $count; $i++) { |
|
66 |
my $row = $self->fetch; |
|
67 |
|
|
68 |
last unless $row; |
|
69 |
|
|
70 |
push @$rows, $row; |
|
71 |
} |
|
72 |
|
|
73 |
return unless @$rows; |
|
74 |
return $rows; |
|
75 |
} |
|
76 | ||
77 |
sub fetch_all { |
|
78 |
my $self = shift; |
|
79 |
|
|
80 |
# Fetch all rows |
|
81 |
my $rows = []; |
|
82 |
while(my $row = $self->fetch) { |
|
83 |
push @$rows, $row; |
|
84 |
} |
|
85 |
return $rows; |
|
packaging one directory
|
86 |
} |
87 | ||
88 |
sub fetch_hash { |
|
many many changes
|
89 | |
update document
|
90 |
$_[0]->{filters} ||= {}; |
91 |
$_[0]->{filter} ||= {}; |
|
packaging one directory
|
92 |
|
93 |
# Fetch |
|
update document
|
94 |
my $row = $_[0]->{sth}->fetchrow_arrayref; |
packaging one directory
|
95 |
|
96 |
# Cannot fetch |
|
97 |
return unless $row; |
|
98 |
|
|
99 |
# Filter |
|
100 |
my $row_hash = {}; |
|
update document
|
101 |
for (my $i = 0; $i < @{$_[0]->{sth}->{NAME_lc}}; $i++) { |
102 |
|
|
103 |
my $fname = $_[0]->{filter}->{$_[0]->{sth}->{NAME_lc}->[$i]} |
|
104 |
|| $_[0]->{default_filter}; |
|
add query filter error check
|
105 |
|
update document
|
106 |
croak "Filter \"$fname\" is not registered." |
107 |
if $fname && ! exists $_[0]->{filters}->{$fname}; |
|
some changed
|
108 |
|
update document
|
109 |
$row_hash->{$_[0]->{sth}->{NAME_lc}->[$i]} |
110 |
= !$fname ? $row->[$i] : |
|
111 |
ref $fname ? $fname->($row->[$i]) : |
|
112 |
$_[0]->{filters}->{$fname}->($row->[$i]); |
|
packaging one directory
|
113 |
} |
114 |
|
|
removed reconnect method
|
115 |
return $row_hash; |
packaging one directory
|
116 |
} |
117 | ||
removed reconnect method
|
118 |
sub fetch_hash_first { |
packaging one directory
|
119 |
my $self = shift; |
120 |
|
|
121 |
# Fetch hash |
|
122 |
my $row = $self->fetch_hash; |
|
123 |
|
|
124 |
# Not exist |
|
125 |
return unless $row; |
|
126 |
|
|
127 |
# Finish statement handle |
|
some changed
|
128 |
$self->sth->finish; |
packaging one directory
|
129 |
|
removed reconnect method
|
130 |
return $row; |
packaging one directory
|
131 |
} |
132 | ||
renamed fetch_rows to fetch_...
|
133 |
sub fetch_hash_multi { |
packaging one directory
|
134 |
my ($self, $count) = @_; |
135 |
|
|
136 |
# Not specified Row count |
|
137 |
croak("Row count must be specified") |
|
138 |
unless $count; |
|
139 |
|
|
140 |
# Fetch multi rows |
|
141 |
my $rows = []; |
|
142 |
for (my $i = 0; $i < $count; $i++) { |
|
removed reconnect method
|
143 |
my $row = $self->fetch_hash; |
packaging one directory
|
144 |
|
removed reconnect method
|
145 |
last unless $row; |
packaging one directory
|
146 |
|
removed reconnect method
|
147 |
push @$rows, $row; |
packaging one directory
|
148 |
} |
149 |
|
|
150 |
return unless @$rows; |
|
removed reconnect method
|
151 |
return $rows; |
packaging one directory
|
152 |
} |
153 | ||
154 |
sub fetch_hash_all { |
|
155 |
my $self = shift; |
|
156 |
|
|
update document
|
157 |
# Fetch all rows as hash |
packaging one directory
|
158 |
my $rows = []; |
removed reconnect method
|
159 |
while(my $row = $self->fetch_hash) { |
160 |
push @$rows, $row; |
|
packaging one directory
|
161 |
} |
removed reconnect method
|
162 |
return $rows; |
packaging one directory
|
163 |
} |
164 | ||
update document
|
165 |
1; |
166 | ||
packaging one directory
|
167 |
=head1 NAME |
168 | ||
removed DESTROY method(not b...
|
169 |
DBIx::Custom::Result - Manipulate the result of select statement |
packaging one directory
|
170 | |
update document
|
171 |
=head1 SYNOPSIS |
packaging one directory
|
172 |
|
removed reconnect method
|
173 |
# Result |
174 |
my $result = $dbi->select(table => 'books'); |
|
175 |
|
|
176 |
# Fetch a row into array |
|
177 |
while (my $row = $result->fetch) { |
|
178 |
my $value1 = $row->[0]; |
|
179 |
my $valuu2 = $row->[1]; |
|
180 |
|
|
181 |
# do something |
|
version 0.0901
|
182 |
} |
183 |
|
|
removed reconnect method
|
184 |
# Fetch only first row into array |
185 |
my $row = $result->fetch_first; |
|
186 |
|
|
187 |
# Fetch multiple rows into array of array |
|
188 |
while (my $rows = $result->fetch_multi(5)) { |
|
189 |
# do something |
|
190 |
} |
|
191 |
|
|
192 |
# Fetch all rows into array of array |
|
193 |
my $rows = $result->fetch_all; |
|
194 |
|
|
195 |
# Fetch hash into hash |
|
196 |
while (my $row = $result->fetch_hash) { |
|
197 |
my $value1 = $row->{title}; |
|
198 |
my $value2 = $row->{author}; |
|
199 |
|
|
200 |
# do something |
|
packaging one directory
|
201 |
} |
removed reconnect method
|
202 |
|
203 |
# Fetch only first row into hash |
|
204 |
my $row = $result->fetch_hash_first; |
|
205 |
|
|
206 |
# Fetch multiple rows into array of hash |
|
207 |
while (my $rows = $result->fetch_hash_multi) { |
|
208 |
# do something |
|
209 |
} |
|
210 |
|
|
211 |
# Fetch all rows into array of hash |
|
212 |
my $rows = $result->fetch_hash_all; |
|
packaging one directory
|
213 | |
update document
|
214 |
=head1 ATTRIBUTES |
packaging one directory
|
215 | |
removed DBIx::Custom commit ...
|
216 |
=head2 C<sth> |
packaging one directory
|
217 | |
version 0.0901
|
218 |
$result = $result->sth($sth); |
219 |
$sth = $reuslt->sth |
|
many many changes
|
220 | |
removed DESTROY method(not b...
|
221 |
Statement handle. |
222 | ||
223 |
=head2 C<default_filter> |
|
many many changes
|
224 | |
removed reconnect method
|
225 |
$result = $result->default_filter('decode_utf8'); |
many many changes
|
226 |
$default_filter = $result->default_filter; |
227 | ||
removed DESTROY method(not b...
|
228 |
Default filter for fetching. |
packaging one directory
|
229 | |
removed DESTROY method(not b...
|
230 |
=head2 C<filter> |
update document
|
231 | |
removed reconnect method
|
232 |
$result = $result->filter({title => 'decode_utf8'}); |
233 |
$filter = $result->filter; |
|
packaging one directory
|
234 | |
removed DESTROY method(not b...
|
235 |
Filters for fetching. |
236 | ||
update document
|
237 |
=head1 METHODS |
238 | ||
239 |
This class is L<Object::Simple> subclass. |
|
240 |
You can use all methods of L<Object::Simple> |
|
packaging one directory
|
241 | |
removed DBIx::Custom commit ...
|
242 |
=head2 C<fetch> |
packaging one directory
|
243 | |
removed reconnect method
|
244 |
$row = $result->fetch; |
version 0.0901
|
245 | |
removed DESTROY method(not b...
|
246 |
Fetch a row into array |
packaging one directory
|
247 | |
248 |
while (my $row = $result->fetch) { |
|
249 |
# do something |
|
removed reconnect method
|
250 |
my $value1 = $row->[0]; |
251 |
my $value2 = $row->[1]; |
|
packaging one directory
|
252 |
} |
253 | ||
removed DBIx::Custom commit ...
|
254 |
=head2 C<fetch_first> |
update document
|
255 | |
removed reconnect method
|
256 |
$row = $result->fetch_first; |
packaging one directory
|
257 | |
removed DESTROY method(not b...
|
258 |
Fetch only first row into array and finish statment handle. |
packaging one directory
|
259 | |
removed DESTROY method(not b...
|
260 |
=head2 C<fetch_multi> |
update document
|
261 | |
removed reconnect method
|
262 |
$rows = $result->fetch_multi($count); |
packaging one directory
|
263 |
|
removed DESTROY method(not b...
|
264 |
Fetch multiple rows into array of array. |
version 0.0901
|
265 | |
renamed fetch_rows to fetch_...
|
266 |
while(my $rows = $result->fetch_multi(10)) { |
update document
|
267 |
# do someting |
268 |
} |
|
packaging one directory
|
269 | |
removed DBIx::Custom commit ...
|
270 |
=head2 C<fetch_all> |
packaging one directory
|
271 | |
removed reconnect method
|
272 |
$rows = $result->fetch_all; |
version 0.0901
|
273 | |
removed DESTROY method(not b...
|
274 |
Fetch all rows into array of array. |
packaging one directory
|
275 | |
removed DESTROY method(not b...
|
276 |
=head2 C<fetch_hash> |
packaging one directory
|
277 | |
removed reconnect method
|
278 |
$row = $result->fetch_hash; |
packaging one directory
|
279 | |
removed DESTROY method(not b...
|
280 |
Fetch a row into hash |
update document
|
281 | |
removed reconnect method
|
282 |
while (my $row = $result->fetch_hash) { |
283 |
my $val1 = $row->{title}; |
|
284 |
my $val2 = $row->{author}; |
|
285 |
|
|
286 |
# do something |
|
287 |
} |
|
version 0.0901
|
288 | |
removed DBIx::Custom commit ...
|
289 |
=head2 C<fetch_hash_first> |
removed reconnect method
|
290 |
|
291 |
$row = $result->fetch_hash_first; |
|
packaging one directory
|
292 | |
removed DESTROY method(not b...
|
293 |
Fetch only first row into hash and finish statment handle. |
packaging one directory
|
294 | |
removed DESTROY method(not b...
|
295 |
=head2 C<fetch_hash_multi> |
update document
|
296 | |
removed reconnect method
|
297 |
$rows = $result->fetch_hash_multi($count); |
update document
|
298 |
|
removed DESTROY method(not b...
|
299 |
Fetch multiple rows into array of hash |
packaging one directory
|
300 | |
removed reconnect method
|
301 |
while(my $rows = $result->fetch_hash_multi(10)) { |
302 |
# do someting |
|
303 |
} |
|
update document
|
304 | |
removed DBIx::Custom commit ...
|
305 |
=head2 C<fetch_hash_all> |
packaging one directory
|
306 | |
removed reconnect method
|
307 |
$rows = $result->fetch_hash_all; |
packaging one directory
|
308 | |
removed DESTROY method(not b...
|
309 |
Fetch all rows into array of hash. |
310 | ||
packaging one directory
|
311 |
=cut |