packaging one directory
|
1 |
package DBIx::Custom::Result; |
updatedd pod
|
2 |
use Object::Simple -base; |
cleanup
|
3 | |
packaging one directory
|
4 |
use Carp 'croak'; |
cleanup
|
5 |
use DBIx::Custom::Util qw/_array_to_hash _subname/; |
packaging one directory
|
6 | |
cleanup
|
7 |
has [qw/filters filter_off sth type_rule_off/]; |
8 |
has stash => sub { {} }; |
|
cleanup
|
9 | |
- added DBIx::Custom::Result...
|
10 |
*all = \&fetch_hash_all; |
11 | ||
cleanup
|
12 |
sub filter { |
13 |
my $self = shift; |
|
cleanup
|
14 |
|
cleanup
|
15 |
# Set |
cleanup
|
16 |
if (@_) { |
17 |
|
|
cleanup
|
18 |
# Convert filter name to subroutine |
19 |
my $filter = @_ == 1 ? $_[0] : [@_]; |
|
20 |
$filter = _array_to_hash($filter); |
|
cleanup
|
21 |
foreach my $column (keys %$filter) { |
22 |
my $fname = $filter->{$column}; |
|
fix bug : filter can't over...
|
23 |
if (exists $filter->{$column} |
24 |
&& defined $fname |
|
25 |
&& ref $fname ne 'CODE') |
|
26 |
{ |
|
cleanup
|
27 |
croak qq{Filter "$fname" is not registered" } . _subname |
cleanup
|
28 |
unless exists $self->filters->{$fname}; |
29 |
$filter->{$column} = $self->filters->{$fname}; |
|
30 |
} |
|
cleanup
|
31 |
} |
cleanup
|
32 |
|
cleanup
|
33 |
# Merge |
added experimental DBIx::Cus...
|
34 |
$self->{filter} = {%{$self->filter}, %$filter}; |
cleanup
|
35 |
|
36 |
return $self; |
|
cleanup
|
37 |
} |
38 |
|
|
added experimental DBIx::Cus...
|
39 |
return $self->{filter} ||= {}; |
40 |
} |
|
41 | ||
packaging one directory
|
42 |
sub fetch { |
changed argument of tag proc...
|
43 |
my $self = shift; |
44 |
|
|
packaging one directory
|
45 |
# Fetch |
changed argument of tag proc...
|
46 |
my @row = $self->{sth}->fetchrow_array; |
update document
|
47 |
return unless @row; |
added check_filter attribute
|
48 |
|
cleanup
|
49 |
# Filtering |
added experimental iterate_a...
|
50 |
my $columns = $self->{sth}->{NAME}; |
added type_rule method and f...
|
51 |
my $types = $self->{sth}->{TYPE}; |
52 |
my $type_rule = $self->type_rule || {}; |
|
separate DBIx::Custom::Resul...
|
53 |
my $filter = $self->filter; |
54 |
my $end_filter = $self->end_filter; |
|
cleanup
|
55 |
for (my $i = 0; $i < @$columns; $i++) { |
added type_rule method and f...
|
56 |
|
separate DBIx::Custom::Resul...
|
57 |
# Column |
cleanup
|
58 |
my $column = $columns->[$i]; |
some changed
|
59 |
|
separate DBIx::Custom::Resul...
|
60 |
# Type rule |
61 |
my $type_filter = $type_rule->{lc($types->[$i])}; |
|
62 |
$row[$i] = $type_filter->($row[$i]) |
|
63 |
if $type_filter && !$self->{type_rule_off}; |
|
64 |
|
|
65 |
# Filter |
|
66 |
my $filter = $filter->{$column} || $self->{default_filter}; |
|
67 |
$row[$i] = $filter->($row[$i]) |
|
68 |
if $filter && !$self->{filter_off}; |
|
69 |
$row[$i] = $end_filter->{$column}->($row[$i]) |
|
70 |
if $end_filter->{$column} && !$self->{filter_off}; |
|
packaging one directory
|
71 |
} |
many many changes
|
72 | |
removed reconnect method
|
73 |
return \@row; |
74 |
} |
|
75 | ||
cleanup
|
76 |
sub fetch_all { |
77 |
my $self = shift; |
|
78 |
|
|
79 |
# Fetch all rows |
|
80 |
my $rows = []; |
|
cleanup
|
81 |
while(my $row = $self->fetch) { push @$rows, $row} |
82 |
|
|
cleanup
|
83 |
return $rows; |
84 |
} |
|
85 | ||
removed reconnect method
|
86 |
sub fetch_first { |
87 |
my $self = shift; |
|
88 |
|
|
89 |
# Fetch |
|
90 |
my $row = $self->fetch; |
|
91 |
return unless $row; |
|
92 |
|
|
93 |
# Finish statement handle |
|
94 |
$self->sth->finish; |
|
95 |
|
|
96 |
return $row; |
|
97 |
} |
|
98 | ||
packaging one directory
|
99 |
sub fetch_hash { |
changed argument of tag proc...
|
100 |
my $self = shift; |
101 |
|
|
packaging one directory
|
102 |
# Fetch |
changed argument of tag proc...
|
103 |
my $row = $self->{sth}->fetchrow_arrayref; |
packaging one directory
|
104 |
return unless $row; |
added check_filter attribute
|
105 | |
packaging one directory
|
106 |
# Filter |
separate DBIx::Custom::Resul...
|
107 |
my $hash_row = {}; |
108 |
my $filter = $self->filter; |
|
109 |
my $end_filter = $self->end_filter || {}; |
|
added experimental iterate_a...
|
110 |
my $columns = $self->{sth}->{NAME}; |
added type_rule method and f...
|
111 |
my $types = $self->{sth}->{TYPE}; |
112 |
my $type_rule = $self->type_rule || {}; |
|
cleanup
|
113 |
for (my $i = 0; $i < @$columns; $i++) { |
update document
|
114 |
|
separate DBIx::Custom::Resul...
|
115 |
# Column |
cleanup
|
116 |
my $column = $columns->[$i]; |
add query filter error check
|
117 |
|
separate DBIx::Custom::Resul...
|
118 |
# Type rule |
119 |
my $type_filter = $type_rule->{lc($types->[$i])}; |
|
120 |
if (!$self->{type_rule_off} && $type_filter) { |
|
121 |
$hash_row->{$column} = $type_filter->($row->[$i]); |
|
DBIx::Custom::Result filter ...
|
122 |
} |
separate DBIx::Custom::Resul...
|
123 |
else { $hash_row->{$column} = $row->[$i] } |
124 |
|
|
125 |
# Filter |
|
126 |
my $f = $filter->{$column} || $self->{default_filter}; |
|
127 |
$hash_row->{$column} = $f->($hash_row->{$column}) |
|
128 |
if $f && !$self->{filter_off}; |
|
129 |
$hash_row->{$column} = $end_filter->{$column}->($hash_row->{$column}) |
|
130 |
if $end_filter->{$column} && !$self->{filter_off}; |
|
packaging one directory
|
131 |
} |
132 |
|
|
separate DBIx::Custom::Resul...
|
133 |
return $hash_row; |
packaging one directory
|
134 |
} |
135 | ||
cleanup
|
136 |
sub fetch_hash_all { |
137 |
my $self = shift; |
|
138 |
|
|
139 |
# Fetch all rows as hash |
|
140 |
my $rows = []; |
|
cleanup
|
141 |
while(my $row = $self->fetch_hash) { push @$rows, $row } |
cleanup
|
142 |
|
143 |
return $rows; |
|
144 |
} |
|
145 | ||
removed reconnect method
|
146 |
sub fetch_hash_first { |
packaging one directory
|
147 |
my $self = shift; |
148 |
|
|
149 |
# Fetch hash |
|
150 |
my $row = $self->fetch_hash; |
|
151 |
return unless $row; |
|
152 |
|
|
153 |
# Finish statement handle |
|
some changed
|
154 |
$self->sth->finish; |
packaging one directory
|
155 |
|
removed reconnect method
|
156 |
return $row; |
packaging one directory
|
157 |
} |
158 | ||
renamed fetch_rows to fetch_...
|
159 |
sub fetch_hash_multi { |
packaging one directory
|
160 |
my ($self, $count) = @_; |
161 |
|
|
cleanup
|
162 |
# Fetch multiple rows |
cleanup
|
163 |
croak 'Row count must be specified ' . _subname |
packaging one directory
|
164 |
unless $count; |
165 |
my $rows = []; |
|
166 |
for (my $i = 0; $i < $count; $i++) { |
|
removed reconnect method
|
167 |
my $row = $self->fetch_hash; |
168 |
last unless $row; |
|
169 |
push @$rows, $row; |
|
packaging one directory
|
170 |
} |
171 |
|
|
172 |
return unless @$rows; |
|
removed reconnect method
|
173 |
return $rows; |
packaging one directory
|
174 |
} |
175 | ||
cleanup
|
176 |
sub fetch_multi { |
177 |
my ($self, $count) = @_; |
|
packaging one directory
|
178 |
|
cleanup
|
179 |
# Row count not specifed |
cleanup
|
180 |
croak 'Row count must be specified ' . _subname |
cleanup
|
181 |
unless $count; |
182 |
|
|
183 |
# Fetch multi rows |
|
packaging one directory
|
184 |
my $rows = []; |
cleanup
|
185 |
for (my $i = 0; $i < $count; $i++) { |
186 |
my $row = $self->fetch; |
|
187 |
last unless $row; |
|
removed reconnect method
|
188 |
push @$rows, $row; |
packaging one directory
|
189 |
} |
changed argument of tag proc...
|
190 |
|
cleanup
|
191 |
return unless @$rows; |
removed reconnect method
|
192 |
return $rows; |
packaging one directory
|
193 |
} |
194 | ||
- added DBIx::Custom::Result...
|
195 |
*one = \&fetch_hash_first; |
196 | ||
added EXPERIMENTAL DBIx::Cus...
|
197 |
sub type_rule { |
198 |
my $self = shift; |
|
199 |
|
|
200 |
# Merge type rule |
|
201 |
if (@_) { |
|
202 |
my $type_rule = @_ == 1 ? $_[0] : [@_]; |
|
203 |
$type_rule = _array_to_hash($type_rule) || {}; |
|
204 |
foreach my $data_type (keys %{$type_rule || {}}) { |
|
205 |
croak qq{data type of into section must be lower case or number} |
|
206 |
if $data_type =~ /[A-Z]/; |
|
207 |
my $fname = $type_rule->{$data_type}; |
|
208 |
if (defined $fname && ref $fname ne 'CODE') { |
|
209 |
croak qq{Filter "$fname" is not registered" } . _subname |
|
210 |
unless exists $self->filters->{$fname}; |
|
211 |
|
|
212 |
$type_rule->{$data_type} = $self->filters->{$fname}; |
|
213 |
} |
|
214 |
} |
|
cleanup
|
215 |
|
216 |
# Replace |
|
217 |
if (@_ == 1) { $self->{type_rule} = $type_rule } |
|
218 |
# Merge |
|
219 |
else { $self->{type_rule} = {%{$self->type_rule}, %$type_rule} } |
|
added EXPERIMENTAL DBIx::Cus...
|
220 |
} |
221 |
|
|
222 |
return $self->{type_rule} ||= {}; |
|
223 |
} |
|
224 | ||
cleanup
|
225 |
# DEPRECATED! |
226 |
sub end_filter { |
|
227 |
my $self = shift; |
|
228 |
if (@_) { |
|
229 |
my $end_filter = {}; |
|
cleanup
|
230 |
if (ref $_[0] eq 'HASH') { $end_filter = $_[0] } |
231 |
else { |
|
cleanup
|
232 |
$end_filter = _array_to_hash( |
233 |
@_ > 1 ? [@_] : $_[0] |
|
234 |
); |
|
235 |
} |
|
236 |
foreach my $column (keys %$end_filter) { |
|
237 |
my $fname = $end_filter->{$column}; |
|
238 |
if (exists $end_filter->{$column} |
|
239 |
&& defined $fname |
|
240 |
&& ref $fname ne 'CODE') |
|
241 |
{ |
|
242 |
croak qq{Filter "$fname" is not registered" } . _subname |
|
243 |
unless exists $self->filters->{$fname}; |
|
244 |
$end_filter->{$column} = $self->filters->{$fname}; |
|
245 |
} |
|
246 |
} |
|
247 |
$self->{end_filter} = {%{$self->end_filter}, %$end_filter}; |
|
248 |
return $self; |
|
249 |
} |
|
250 |
return $self->{end_filter} ||= {}; |
|
251 |
} |
|
252 | ||
cleanup
|
253 |
# DEPRECATED! |
added experimental DBIx::Cus...
|
254 |
sub remove_end_filter { |
255 |
my $self = shift; |
|
cleanup
|
256 |
warn "remove_end_filter is DEPRECATED! use filter_off attribute instead"; |
added experimental DBIx::Cus...
|
257 |
$self->{end_filter} = {}; |
258 |
return $self; |
|
259 |
} |
|
260 | ||
cleanup
|
261 |
# DEPRECATED! |
added experimental DBIx::Cus...
|
262 |
sub remove_filter { |
263 |
my $self = shift; |
|
cleanup
|
264 |
warn "remove_filter is DEPRECATED! use filter_off attribute instead"; |
added experimental DBIx::Cus...
|
265 |
$self->{filter} = {}; |
266 |
return $self; |
|
267 |
} |
|
268 | ||
cleanup
|
269 |
# DEPRECATED! |
cleanup
|
270 |
sub default_filter { |
271 |
my $self = shift; |
|
cleanup
|
272 |
warn "default_filter is DEPRECATED!"; |
cleanup
|
273 |
if (@_) { |
274 |
my $fname = $_[0]; |
|
275 |
if (@_ && !$fname) { |
|
276 |
$self->{default_filter} = undef; |
|
277 |
} |
|
278 |
else { |
|
many changed
|
279 |
croak qq{Filter "$fname" is not registered} |
cleanup
|
280 |
unless exists $self->filters->{$fname}; |
281 |
$self->{default_filter} = $self->filters->{$fname}; |
|
282 |
} |
|
283 |
return $self; |
|
284 |
} |
|
285 |
return $self->{default_filter}; |
|
286 |
} |
|
287 | ||
cleanup
|
288 |
# DEPRECATED! |
cleanup
|
289 |
has 'filter_check'; |
cleanup
|
290 | |
update document
|
291 |
1; |
292 | ||
packaging one directory
|
293 |
=head1 NAME |
294 | ||
cleanup
|
295 |
DBIx::Custom::Result - Result of select statement |
packaging one directory
|
296 | |
update document
|
297 |
=head1 SYNOPSIS |
cleanup
|
298 | |
removed reconnect method
|
299 |
# Result |
cleanup
|
300 |
my $result = $dbi->select(table => 'book'); |
cleanup
|
301 | |
cleanup
|
302 |
# Fetch a row and put it into array reference |
removed reconnect method
|
303 |
while (my $row = $result->fetch) { |
cleanup
|
304 |
my $author = $row->[0]; |
305 |
my $title = $row->[1]; |
|
version 0.0901
|
306 |
} |
307 |
|
|
cleanup
|
308 |
# Fetch only a first row and put it into array reference |
removed reconnect method
|
309 |
my $row = $result->fetch_first; |
310 |
|
|
cleanup
|
311 |
# Fetch all rows and put them into array of array reference |
removed reconnect method
|
312 |
my $rows = $result->fetch_all; |
cleanup
|
313 | |
cleanup
|
314 |
# Fetch a row and put it into hash reference |
removed reconnect method
|
315 |
while (my $row = $result->fetch_hash) { |
cleanup
|
316 |
my $title = $row->{title}; |
317 |
my $author = $row->{author}; |
|
packaging one directory
|
318 |
} |
removed reconnect method
|
319 |
|
cleanup
|
320 |
# Fetch only a first row and put it into hash reference |
removed reconnect method
|
321 |
my $row = $result->fetch_hash_first; |
cleanup
|
322 |
my $row = $result->one; # Same as fetch_hash_first |
removed reconnect method
|
323 |
|
cleanup
|
324 |
# Fetch all rows and put them into array of hash reference |
removed reconnect method
|
325 |
my $rows = $result->fetch_hash_all; |
cleanup
|
326 |
my $rows = $result->all; # Same as fetch_hash_all |
packaging one directory
|
327 | |
update document
|
328 |
=head1 ATTRIBUTES |
packaging one directory
|
329 | |
updated_pod
|
330 |
=head2 C<filter_off> EXPERIMENTAL |
331 | ||
332 |
my $filter_off = $resutl->filter_off; |
|
333 |
$result = $result->filter_off(1); |
|
334 | ||
cleanup
|
335 |
Filtering by C<filter> method is turned off. |
updated_pod
|
336 | |
cleanup
|
337 |
=head2 C<filters> |
338 | ||
339 |
my $filters = $result->filters; |
|
cleanup
|
340 |
$result = $result->filters(\%filters); |
cleanup
|
341 | |
cleanup
|
342 |
Filters. |
cleanup
|
343 | |
344 |
=head2 C<sth> |
|
345 | ||
346 |
my $sth = $reuslt->sth |
|
347 |
$result = $result->sth($sth); |
|
348 | ||
349 |
Statement handle of L<DBI>. |
|
350 | ||
added EXPERIMENTAL execute()...
|
351 |
=head2 C<type_rule_off> EXPERIMENTAL |
352 | ||
353 |
my $type_rule_off = $result->type_rule_off; |
|
354 |
$result = $result->type_rule_off(1); |
|
355 | ||
cleanup
|
356 |
Filtering by C<type_rule> is turned off. |
added EXPERIMENTAL execute()...
|
357 | |
update document
|
358 |
=head1 METHODS |
359 | ||
renamed build_query to creat...
|
360 |
L<DBIx::Custom::Result> inherits all methods from L<Object::Simple> |
cleanup
|
361 |
and implements the following new ones. |
packaging one directory
|
362 | |
updated pod
|
363 |
=head2 C<all> |
364 | ||
365 |
my $rows = $result->all; |
|
366 | ||
cleanup
|
367 |
Same as C<fetch_hash_all>. |
updated pod
|
368 | |
removed DBIx::Custom commit ...
|
369 |
=head2 C<fetch> |
packaging one directory
|
370 | |
cleanup
|
371 |
my $row = $result->fetch; |
version 0.0901
|
372 | |
cleanup
|
373 |
Fetch a row and put it into array reference. |
packaging one directory
|
374 | |
removed DBIx::Custom commit ...
|
375 |
=head2 C<fetch_all> |
packaging one directory
|
376 | |
cleanup
|
377 |
my $rows = $result->fetch_all; |
version 0.0901
|
378 | |
cleanup
|
379 |
Fetch all rows and put them into array of array reference. |
packaging one directory
|
380 | |
cleanup
|
381 |
=head2 C<fetch_first> |
382 | ||
383 |
my $row = $result->fetch_first; |
|
384 | ||
cleanup
|
385 |
Fetch only a first row and put it into array reference, |
386 |
and finish statment handle. |
|
cleanup
|
387 | |
removed DESTROY method(not b...
|
388 |
=head2 C<fetch_hash> |
packaging one directory
|
389 | |
cleanup
|
390 |
my $row = $result->fetch_hash; |
packaging one directory
|
391 | |
cleanup
|
392 |
Fetch a row and put it into hash reference. |
update document
|
393 | |
cleanup
|
394 |
=head2 C<fetch_hash_all> |
395 | ||
396 |
my $rows = $result->fetch_hash_all; |
|
397 | ||
cleanup
|
398 |
Fetch all rows and put them into array of hash reference. |
cleanup
|
399 | |
removed DBIx::Custom commit ...
|
400 |
=head2 C<fetch_hash_first> |
removed reconnect method
|
401 |
|
cleanup
|
402 |
my $row = $result->fetch_hash_first; |
packaging one directory
|
403 | |
cleanup
|
404 |
Fetch only a first row and put it into hash reference, |
405 |
and finish statment handle. |
|
packaging one directory
|
406 | |
removed DESTROY method(not b...
|
407 |
=head2 C<fetch_hash_multi> |
update document
|
408 | |
cleanup
|
409 |
my $rows = $result->fetch_hash_multi(5); |
update document
|
410 |
|
cleanup
|
411 |
Fetch multiple rows and put them into array of hash reference. |
update document
|
412 | |
cleanup
|
413 |
=head2 C<fetch_multi> |
packaging one directory
|
414 | |
cleanup
|
415 |
my $rows = $result->fetch_multi(5); |
416 |
|
|
cleanup
|
417 |
Fetch multiple rows and put them into array of array reference. |
removed DESTROY method(not b...
|
418 | |
cleanup
|
419 |
=head2 C<filter> |
420 | ||
cleanup
|
421 |
$result->filter(title => sub { uc $_[0] }, author => 'to_upper'); |
422 |
$result->filter([qw/title author/] => 'to_upper'); |
|
added experimental DBIx::Cus...
|
423 | |
cleanup
|
424 |
Set filter for column. |
425 |
You can use subroutine or filter name as filter. |
|
cleanup
|
426 | |
updated pod
|
427 |
=head2 C<one> |
428 | ||
429 |
my $row = $result->one; |
|
430 | ||
cleanup
|
431 |
Same as C<fetch_hash_first>. |
added experimental DBIx::Cus...
|
432 | |
- removed DEPRECATED DBIx::C...
|
433 |
=head2 C<stash> |
added experimental DBIx::Cus...
|
434 | |
435 |
my $stash = $result->stash; |
|
436 |
my $foo = $result->stash->{foo}; |
|
437 |
$result->stash->{foo} = $foo; |
|
438 | ||
cleanup
|
439 |
Stash is hash reference for data. |
added experimental DBIx::Cus...
|
440 | |
added EXPERIMENTAL DBIx::Cus...
|
441 |
=head2 C<type_rule> EXPERIMENTAL |
cleanup
|
442 |
|
443 |
# Merge type rule |
|
added EXPERIMENTAL DBIx::Cus...
|
444 |
$result->type_rule( |
445 |
# DATE |
|
446 |
9 => sub { ... }, |
|
447 |
# DATETIME or TIMESTAMP |
|
448 |
11 => sub { ... } |
|
449 |
); |
|
450 | ||
cleanup
|
451 |
# Replace type rule(by reference) |
452 |
$result->type_rule([ |
|
453 |
# DATE |
|
454 |
9 => sub { ... }, |
|
455 |
# DATETIME or TIMESTAMP |
|
456 |
11 => sub { ... } |
|
457 |
]); |
|
EXPERIMENTAL type_rule_off i...
|
458 | |
cleanup
|
459 |
This is same as L<DBIx::Custom>'s C<type_rule>'s <from>. |
EXPERIMENTAL type_rule_off i...
|
460 | |
packaging one directory
|
461 |
=cut |