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