packaging one directory
|
1 |
package DBIx::Custom::Result; |
updatedd pod
|
2 |
use Object::Simple -base; |
cleanup
|
3 | |
packaging one directory
|
4 |
use Carp 'croak'; |
- added DBIX_CUSTOM_SUPPRESS...
|
5 |
use DBIx::Custom::Util qw/_array_to_hash _subname _deprecate/; |
packaging one directory
|
6 | |
sub module use DBIx::Custom ...
|
7 |
has [qw/dbi sth/], |
cleanup
|
8 |
stash => sub { {} }; |
cleanup
|
9 | |
- added DBIx::Custom::Result...
|
10 |
*all = \&fetch_hash_all; |
11 | ||
micro optimization
|
12 |
sub fetch { |
cleanup
|
13 |
my $self = shift; |
14 |
|
|
15 |
# Info |
|
16 |
$self->_cache unless $self->{_cache}; |
|
17 |
|
|
18 |
# Fetch |
|
19 |
my @row = $self->{sth}->fetchrow_array; |
|
20 |
return unless @row; |
|
21 |
|
|
22 |
# Type rule |
|
23 |
if ($self->{type_rule}->{from1} && !$self->{type_rule_off} && !$self->{type_rule1_off}) { |
|
24 |
my $from = $self->{type_rule}->{from1}; |
|
25 |
for my $type (keys %$from) { |
|
26 |
for my $column (@{$self->{_type_map}->{$type}}) { |
|
27 |
$row[$_] = $from->{$type}->($row[$_]) |
|
28 |
for @{$self->{_pos}{$column} || []}; |
|
29 |
} |
|
micro optimization
|
30 |
} |
cleanup
|
31 |
} |
32 |
if ($self->{type_rule}->{from2} && !$self->{type_rule_off} && !$self->{type_rule2_off}) { |
|
33 |
my $from = $self->{type_rule}->{from2}; |
|
34 |
for my $type (keys %$from) { |
|
35 |
for my $column (@{$self->{_type_map}->{$type}}) { |
|
36 |
$row[$_] = $from->{$type}->($row[$_]) |
|
37 |
for @{$self->{_pos}{$column} || []}; |
|
38 |
} |
|
micro optimization
|
39 |
} |
cleanup
|
40 |
} |
41 |
|
|
42 |
# Filter |
|
43 |
if (($self->{filter} || $self->{default_filter}) && !$self->{filter_off}) { |
|
44 |
my @columns = $self->{default_filter} ? keys %{$self->{_columns}} |
|
45 |
: keys %{$self->{filter}}; |
|
46 |
|
|
47 |
for my $column (@columns) { |
|
48 |
my $filter = exists $self->{filter}->{$column} ? $self->{filter}->{$column} |
|
49 |
: $self->{default_filter}; |
|
50 |
next unless $filter; |
|
51 |
$row[$_] = $filter->($row[$_]) |
|
52 |
for @{$self->{_pos}{$column} || []}; |
|
53 |
} |
|
54 |
} |
|
55 |
if ($self->{end_filter} && !$self->{filter_off}) { |
|
56 |
for my $column (keys %{$self->{end_filter}}) { |
|
57 |
next unless $self->{end_filter}->{$column}; |
|
58 |
$row[$_] = $self->{end_filter}->{$column}->($row[$_]) |
|
59 |
for @{$self->{_pos}{$column} || []}; |
|
60 |
} |
|
61 |
} |
|
62 | ||
63 |
return \@row; |
|
micro optimization
|
64 |
} |
65 | ||
66 |
sub fetch_hash { |
|
cleanup
|
67 |
my $self = shift; |
68 |
|
|
69 |
# Info |
|
70 |
$self->_cache unless $self->{_cache}; |
|
71 |
|
|
72 |
# Fetch |
|
73 |
return unless my $row = $self->{sth}->fetchrow_hashref; |
|
74 |
|
|
75 |
# Type rule |
|
76 |
if ($self->{type_rule}->{from1} && |
|
77 |
!$self->{type_rule_off} && !$self->{type_rule1_off}) |
|
78 |
{ |
|
79 |
my $from = $self->{type_rule}->{from1}; |
|
80 |
for my $type (keys %$from) { |
|
81 |
$from->{$type} and $row->{$_} = $from->{$type}->($row->{$_}) |
|
82 |
for @{$self->{_type_map}->{$type}}; |
|
micro optimization
|
83 |
} |
cleanup
|
84 |
} |
85 |
if ($self->{type_rule}->{from2} && |
|
86 |
!$self->{type_rule_off} && !$self->{type_rule2_off}) |
|
87 |
{ |
|
88 |
my $from = $self->{type_rule}->{from2}; |
|
89 |
for my $type (keys %{$self->{type_rule}->{from2}}) { |
|
90 |
$from->{$type} and $row->{$_} = $from->{$type}->($row->{$_}) |
|
91 |
for @{$self->{_type_map}->{$type}}; |
|
micro optimization
|
92 |
} |
cleanup
|
93 |
} |
94 |
# Filter |
|
95 |
if (($self->{filter} || $self->{default_filter}) && |
|
96 |
!$self->{filter_off}) |
|
97 |
{ |
|
98 |
my @columns = $self->{default_filter} ? keys %{$self->{_columns}} |
|
99 |
: keys %{$self->{filter}}; |
|
100 |
|
|
101 |
for my $column (@columns) { |
|
102 |
next unless exists $row->{$column}; |
|
103 |
my $filter = exists $self->{filter}->{$column} ? $self->{filter}->{$column} |
|
104 |
: $self->{default_filter}; |
|
105 |
$row->{$column} = $filter->($row->{$column}) if $filter; |
|
106 |
} |
|
107 |
} |
|
108 |
if ($self->{end_filter} && !$self->{filter_off}) { |
|
109 |
exists $self->{_columns}{$_} && $self->{end_filter}->{$_} and |
|
110 |
$row->{$_} = $self->{end_filter}->{$_}->($row->{$_}) |
|
111 |
for keys %{$self->{end_filter}}; |
|
112 |
} |
|
113 |
$row; |
|
micro optimization
|
114 |
} |
115 | ||
cleanup
|
116 |
sub fetch_all { |
cleanup
|
117 |
my $self = shift; |
118 |
|
|
119 |
# Fetch all rows |
|
120 |
my $rows = []; |
|
121 |
while(my $row = $self->fetch) { push @$rows, $row} |
|
122 |
|
|
123 |
return $rows; |
|
cleanup
|
124 |
} |
125 | ||
126 |
sub fetch_hash_all { |
|
cleanup
|
127 |
my $self = shift; |
128 |
|
|
129 |
# Fetch all rows as hash |
|
130 |
my $rows = []; |
|
131 |
while(my $row = $self->fetch_hash) { push @$rows, $row } |
|
132 |
|
|
133 |
return $rows; |
|
cleanup
|
134 |
} |
135 | ||
- renamed DBIx::Custom::Resu...
|
136 |
sub fetch_hash_one { |
cleanup
|
137 |
my $self = shift; |
138 |
|
|
139 |
# Fetch hash |
|
140 |
my $row = $self->fetch_hash; |
|
141 |
return unless $row; |
|
142 |
|
|
143 |
# Finish statement handle |
|
144 |
$self->sth->finish; |
|
145 |
|
|
146 |
return $row; |
|
packaging one directory
|
147 |
} |
148 | ||
renamed fetch_rows to fetch_...
|
149 |
sub fetch_hash_multi { |
cleanup
|
150 |
my ($self, $count) = @_; |
151 |
|
|
152 |
# Fetch multiple rows |
|
153 |
croak 'Row count must be specified ' . _subname |
|
154 |
unless $count; |
|
155 |
|
|
156 |
return if $self->{_finished}; |
|
157 | ||
158 |
my $rows = []; |
|
159 |
for (my $i = 0; $i < $count; $i++) { |
|
160 |
my $row = $self->fetch_hash; |
|
161 |
unless ($row) { |
|
162 |
$self->{_finished} = 1; |
|
163 |
last; |
|
packaging one directory
|
164 |
} |
cleanup
|
165 |
push @$rows, $row; |
166 |
} |
|
167 |
|
|
168 |
return unless @$rows; |
|
169 |
return $rows; |
|
packaging one directory
|
170 |
} |
171 | ||
cleanup
|
172 |
sub fetch_multi { |
cleanup
|
173 |
my ($self, $count) = @_; |
174 |
|
|
175 |
# Row count not specifed |
|
176 |
croak 'Row count must be specified ' . _subname |
|
177 |
unless $count; |
|
178 |
|
|
179 |
return if $self->{_finished}; |
|
180 |
|
|
181 |
# Fetch multi rows |
|
182 |
my $rows = []; |
|
183 |
for (my $i = 0; $i < $count; $i++) { |
|
184 |
my $row = $self->fetch; |
|
185 |
unless ($row) { |
|
186 |
$self->{_finished} = 1; |
|
187 |
last; |
|
packaging one directory
|
188 |
} |
cleanup
|
189 |
push @$rows, $row; |
190 |
} |
|
191 |
|
|
192 |
return unless @$rows; |
|
193 |
return $rows; |
|
packaging one directory
|
194 |
} |
195 | ||
- renamed DBIx::Custom::Resu...
|
196 | |
197 |
sub fetch_one { |
|
cleanup
|
198 |
my $self = shift; |
199 |
|
|
200 |
# Fetch |
|
201 |
my $row = $self->fetch; |
|
202 |
return unless $row; |
|
203 |
|
|
204 |
# Finish statement handle |
|
205 |
$self->sth->finish; |
|
206 |
|
|
207 |
return $row; |
|
- renamed DBIx::Custom::Resu...
|
208 |
} |
209 | ||
added EXPERIMENTAL DBIx::Cus...
|
210 |
sub filter { |
211 |
my $self = shift; |
|
212 |
|
|
213 |
# Set |
|
214 |
if (@_) { |
|
215 |
|
|
216 |
# Convert filter name to subroutine |
|
217 |
my $filter = @_ == 1 ? $_[0] : [@_]; |
|
218 |
$filter = _array_to_hash($filter); |
|
219 |
for my $column (keys %$filter) { |
|
220 |
my $fname = $filter->{$column}; |
|
221 |
if (exists $filter->{$column} |
|
222 |
&& defined $fname |
|
223 |
&& ref $fname ne 'CODE') |
|
224 |
{ |
|
225 |
croak qq{Filter "$fname" is not registered" } . _subname |
|
226 |
unless exists $self->dbi->filters->{$fname}; |
|
227 |
$filter->{$column} = $self->dbi->filters->{$fname}; |
|
228 |
} |
|
229 |
} |
|
230 |
|
|
231 |
# Merge |
|
232 |
$self->{filter} = {%{$self->filter}, %$filter}; |
|
233 |
|
|
234 |
return $self; |
|
235 |
} |
|
236 |
|
|
237 |
return $self->{filter} ||= {}; |
|
238 |
} |
|
239 | ||
240 |
sub flat { |
|
241 |
my $self = shift; |
|
242 |
|
|
243 |
my @flat; |
|
244 |
while (my $row = $self->fetch) { |
|
245 |
push @flat, @$row; |
|
246 |
} |
|
247 |
return @flat; |
|
248 |
} |
|
249 | ||
added EXPERIMETAL DBIx::Cust...
|
250 |
sub kv { |
251 |
my ($self, %opt) = @_; |
|
252 | ||
253 |
my $key_name = $self->{sth}{NAME}[0]; |
|
254 |
my $kv = {}; |
|
255 |
while (my $row = $self->fetch_hash) { |
|
256 |
my $key_value = delete $row->{$key_name}; |
|
257 |
next unless defined $key_value; |
|
258 |
if ($opt{multi}) { |
|
259 |
$kv->{$key_value} ||= []; |
|
260 |
push @{$kv->{$key_value}}, $row; |
|
261 |
} |
|
262 |
else { $kv->{$key_value} = $row } |
|
263 |
} |
|
264 |
|
|
265 |
return $kv; |
|
266 |
} |
|
267 | ||
added EXPERIMENTAL DBIx::Cus...
|
268 |
sub header { shift->sth->{NAME} } |
269 | ||
- renamed DBIx::Custom::Resu...
|
270 |
*one = \&fetch_hash_one; |
- added DBIx::Custom::Result...
|
271 | |
added EXPERIMENTAL DBIx::Cus...
|
272 |
sub type_rule { |
cleanup
|
273 |
my $self = shift; |
274 |
|
|
275 |
if (@_) { |
|
276 |
my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
277 | ||
278 |
# From |
|
279 |
for my $i (1 .. 2) { |
|
280 |
$type_rule->{"from$i"} = _array_to_hash($type_rule->{"from$i"}); |
|
281 |
for my $data_type (keys %{$type_rule->{"from$i"} || {}}) { |
|
282 |
croak qq{data type of from$i section must be lower case or number} |
|
283 |
if $data_type =~ /[A-Z]/; |
|
284 |
my $fname = $type_rule->{"from$i"}{$data_type}; |
|
285 |
if (defined $fname && ref $fname ne 'CODE') { |
|
286 |
croak qq{Filter "$fname" is not registered" } . _subname |
|
287 |
unless exists $self->dbi->filters->{$fname}; |
|
288 |
|
|
289 |
$type_rule->{"from$i"}{$data_type} = $self->dbi->filters->{$fname}; |
|
added EXPERIMENTAL DBIx::Cus...
|
290 |
} |
cleanup
|
291 |
} |
added EXPERIMENTAL DBIx::Cus...
|
292 |
} |
cleanup
|
293 |
$self->{type_rule} = $type_rule; |
added EXPERIMENTAL DBIx::Cus...
|
294 |
|
cleanup
|
295 |
return $self; |
296 |
} |
|
297 |
|
|
298 |
return $self->{type_rule} || {}; |
|
added EXPERIMENTAL DBIx::Cus...
|
299 |
} |
300 | ||
- changed EXPERIMENTAL DBIx:...
|
301 |
sub type_rule_off { |
cleanup
|
302 |
my $self = shift; |
303 |
$self->{type_rule_off} = 1; |
|
304 |
return $self; |
|
- changed EXPERIMENTAL DBIx:...
|
305 |
} |
306 | ||
307 |
sub type_rule_on { |
|
cleanup
|
308 |
my $self = shift; |
309 |
$self->{type_rule_off} = 0; |
|
310 |
return $self; |
|
- changed EXPERIMENTAL DBIx:...
|
311 |
} |
312 | ||
313 |
sub type_rule1_off { |
|
cleanup
|
314 |
my $self = shift; |
315 |
$self->{type_rule1_off} = 1; |
|
316 |
return $self; |
|
- changed EXPERIMENTAL DBIx:...
|
317 |
} |
318 | ||
319 |
sub type_rule1_on { |
|
cleanup
|
320 |
my $self = shift; |
321 |
$self->{type_rule1_off} = 0; |
|
322 |
return $self; |
|
- changed EXPERIMENTAL DBIx:...
|
323 |
} |
324 | ||
325 |
sub type_rule2_off { |
|
cleanup
|
326 |
my $self = shift; |
327 |
$self->{type_rule2_off} = 1; |
|
328 |
return $self; |
|
- changed EXPERIMENTAL DBIx:...
|
329 |
} |
330 | ||
331 |
sub type_rule2_on { |
|
cleanup
|
332 |
my $self = shift; |
333 |
$self->{type_rule2_off} = 0; |
|
334 |
return $self; |
|
- changed EXPERIMENTAL DBIx:...
|
335 |
} |
336 | ||
- added EXPERIMENTAL DBIx::C...
|
337 |
sub value { |
cleanup
|
338 |
my $self = shift; |
339 |
my $row = $self->fetch_one; |
|
340 |
my $value = $row ? $row->[0] : undef; |
|
341 |
return $value; |
|
- added EXPERIMENTAL DBIx::C...
|
342 |
} |
343 | ||
- added DBIX_CUSTOM_SUPPRESS...
|
344 |
sub values { |
345 |
my $self = shift; |
|
346 |
|
|
347 |
my $values = []; |
|
348 |
my $rows = $self->fetch_all; |
|
349 |
push @$values, $_->[0] for @$rows; |
|
350 |
return $values; |
|
351 |
} |
|
352 | ||
- fixed bug DBIx::Custom::Re...
|
353 |
sub _cache { |
cleanup
|
354 |
my $self = shift; |
355 |
$self->{_type_map} = {}; |
|
356 |
$self->{_pos} = {}; |
|
357 |
$self->{_columns} = {}; |
|
- removed EXPERIMENTAL statu...
|
358 |
for (my $i = 0; $i < @{$self->{sth}->{NAME} || []}; $i++) { |
cleanup
|
359 |
my $type = lc $self->{sth}{TYPE}[$i]; |
360 |
my $name = $self->{sth}{NAME}[$i]; |
|
361 |
$self->{_type_map}{$type} ||= []; |
|
362 |
push @{$self->{_type_map}{$type}}, $name; |
|
363 |
$self->{_pos}{$name} ||= []; |
|
364 |
push @{$self->{_pos}{$name}}, $i; |
|
365 |
$self->{_columns}{$name} = 1; |
|
366 |
} |
|
367 |
$self->{_cache} = 1; |
|
- fixed bug DBIx::Custom::Re...
|
368 |
} |
369 | ||
- added DBIX_CUSTOM_SUPPRESS...
|
370 |
# DEPRECATED! |
371 |
sub column { |
|
372 |
my $self = shift; |
|
373 |
|
|
374 |
_deprecate('0.25', "DBIx::Custom::Result::column method is DEPRECATED. " |
|
375 |
. "use values method instead"); |
|
376 |
|
|
377 |
return $self->values(@_); |
|
378 |
} |
|
379 | ||
- renamed DBIx::Custom::Resu...
|
380 |
# DEPRECATED! |
381 |
sub fetch_hash_first { |
|
cleanup
|
382 |
my $self = shift; |
- added DBIX_CUSTOM_SUPPRESS...
|
383 |
_deprecate('0.24', "DBIx::Custom::Result::fetch_hash_first is DEPRECATED! " |
384 |
. "use fetch_hash_one instead"); |
|
cleanup
|
385 |
return $self->fetch_hash_one(@_); |
- renamed DBIx::Custom::Resu...
|
386 |
} |
387 | ||
388 |
# DEPRECATED! |
|
389 |
sub fetch_first { |
|
cleanup
|
390 |
my $self = shift; |
- added DBIX_CUSTOM_SUPPRESS...
|
391 |
_deprecate('0.24', "DBIx::Custom::Result::fetch_first is DEPRECATED! " |
392 |
. " use fetch_one instead"); |
|
cleanup
|
393 |
return $self->fetch_one(@_); |
- renamed DBIx::Custom::Resu...
|
394 |
} |
395 | ||
- DBIx::Custom::Result filte...
|
396 |
# DEPRECATED! |
397 |
sub filter_off { |
|
- added DBIX_CUSTOM_SUPPRESS...
|
398 |
_deprecate('0.24', "filter_off method is DEPRECATED!"); |
cleanup
|
399 |
my $self = shift; |
400 |
$self->{filter_off} = 1; |
|
401 |
return $self; |
|
- DBIx::Custom::Result filte...
|
402 |
} |
403 | ||
404 |
# DEPRECATED! |
|
405 |
sub filter_on { |
|
- added DBIX_CUSTOM_SUPPRESS...
|
406 |
_deprecated('0.24', "filter_on method is DEPRECATED!"); |
cleanup
|
407 |
my $self = shift; |
408 |
$self->{filter_off} = 0; |
|
409 |
return $self; |
|
- DBIx::Custom::Result filte...
|
410 |
} |
411 | ||
cleanup
|
412 |
# DEPRECATED! |
413 |
sub end_filter { |
|
- added DBIX_CUSTOM_SUPPRESS...
|
414 |
_deprecate('0.24', "end_filter method is DEPRECATED!"); |
cleanup
|
415 |
my $self = shift; |
416 |
if (@_) { |
|
417 |
my $end_filter = {}; |
|
418 |
if (ref $_[0] eq 'HASH') { $end_filter = $_[0] } |
|
419 |
else { |
|
420 |
$end_filter = _array_to_hash( |
|
421 |
@_ > 1 ? [@_] : $_[0] |
|
422 |
); |
|
423 |
} |
|
424 |
for my $column (keys %$end_filter) { |
|
425 |
my $fname = $end_filter->{$column}; |
|
426 |
if (exists $end_filter->{$column} |
|
427 |
&& defined $fname |
|
428 |
&& ref $fname ne 'CODE') |
|
429 |
{ |
|
430 |
croak qq{Filter "$fname" is not registered" } . _subname |
|
431 |
unless exists $self->dbi->filters->{$fname}; |
|
432 |
$end_filter->{$column} = $self->dbi->filters->{$fname}; |
|
433 |
} |
|
cleanup
|
434 |
} |
cleanup
|
435 |
$self->{end_filter} = {%{$self->end_filter}, %$end_filter}; |
436 |
return $self; |
|
437 |
} |
|
438 |
return $self->{end_filter} ||= {}; |
|
cleanup
|
439 |
} |
cleanup
|
440 |
# DEPRECATED! |
added experimental DBIx::Cus...
|
441 |
sub remove_end_filter { |
- added DBIX_CUSTOM_SUPPRESS...
|
442 |
_deprecate('0.24', "remove_end_filter is DEPRECATED!"); |
cleanup
|
443 |
my $self = shift; |
444 |
$self->{end_filter} = {}; |
|
445 |
return $self; |
|
added experimental DBIx::Cus...
|
446 |
} |
cleanup
|
447 |
# DEPRECATED! |
added experimental DBIx::Cus...
|
448 |
sub remove_filter { |
- added DBIX_CUSTOM_SUPPRESS...
|
449 |
_deprecate('0.24', "remove_filter is DEPRECATED!"); |
cleanup
|
450 |
my $self = shift; |
451 |
$self->{filter} = {}; |
|
452 |
return $self; |
|
added experimental DBIx::Cus...
|
453 |
} |
cleanup
|
454 |
# DEPRECATED! |
cleanup
|
455 |
sub default_filter { |
- added DBIX_CUSTOM_SUPPRESS...
|
456 |
_deprecate('0.24', "default_filter is DEPRECATED!"); |
cleanup
|
457 |
my $self = shift; |
458 |
if (@_) { |
|
459 |
my $fname = $_[0]; |
|
460 |
if (@_ && !$fname) { |
|
461 |
$self->{default_filter} = undef; |
|
cleanup
|
462 |
} |
cleanup
|
463 |
else { |
464 |
croak qq{Filter "$fname" is not registered} |
|
465 |
unless exists $self->dbi->filters->{$fname}; |
|
466 |
$self->{default_filter} = $self->dbi->filters->{$fname}; |
|
467 |
} |
|
468 |
return $self; |
|
469 |
} |
|
470 |
return $self->{default_filter}; |
|
cleanup
|
471 |
} |
cleanup
|
472 |
# DEPRECATED! |
cleanup
|
473 |
has 'filter_check'; |
cleanup
|
474 | |
update document
|
475 |
1; |
476 | ||
packaging one directory
|
477 |
=head1 NAME |
478 | ||
cleanup
|
479 |
DBIx::Custom::Result - Result of select statement |
packaging one directory
|
480 | |
update document
|
481 |
=head1 SYNOPSIS |
cleanup
|
482 | |
cleanup
|
483 |
# Result |
484 |
my $result = $dbi->select(table => 'book'); |
|
485 | ||
486 |
# Fetch a row and put it into array reference |
|
487 |
while (my $row = $result->fetch) { |
|
488 |
my $author = $row->[0]; |
|
489 |
my $title = $row->[1]; |
|
490 |
} |
|
491 |
|
|
492 |
# Fetch only a first row and put it into array reference |
|
493 |
my $row = $result->fetch_one; |
|
494 |
|
|
495 |
# Fetch all rows and put them into array of array reference |
|
496 |
my $rows = $result->fetch_all; |
|
497 | ||
498 |
# Fetch a row and put it into hash reference |
|
499 |
while (my $row = $result->fetch_hash) { |
|
500 |
my $title = $row->{title}; |
|
501 |
my $author = $row->{author}; |
|
502 |
} |
|
503 |
|
|
504 |
# Fetch only a first row and put it into hash reference |
|
505 |
my $row = $result->fetch_hash_one; |
|
506 |
my $row = $result->one; # Alias for "fetch_hash_one" |
|
507 |
|
|
508 |
# Fetch all rows and put them into array of hash reference |
|
509 |
my $rows = $result->fetch_hash_all; |
|
510 |
my $rows = $result->all; # Alias for "fetch_hash_all" |
|
packaging one directory
|
511 | |
update document
|
512 |
=head1 ATTRIBUTES |
packaging one directory
|
513 | |
sub module use DBIx::Custom ...
|
514 |
=head2 C<dbi> |
cleanup
|
515 | |
cleanup
|
516 |
my $dbi = $result->dbi; |
517 |
$result = $result->dbi($dbi); |
|
cleanup
|
518 | |
sub module use DBIx::Custom ...
|
519 |
L<DBIx::Custom> object. |
cleanup
|
520 | |
521 |
=head2 C<sth> |
|
522 | ||
cleanup
|
523 |
my $sth = $reuslt->sth |
524 |
$result = $result->sth($sth); |
|
cleanup
|
525 | |
526 |
Statement handle of L<DBI>. |
|
527 | ||
update document
|
528 |
=head1 METHODS |
529 | ||
renamed build_query to creat...
|
530 |
L<DBIx::Custom::Result> inherits all methods from L<Object::Simple> |
cleanup
|
531 |
and implements the following new ones. |
packaging one directory
|
532 | |
updated pod
|
533 |
=head2 C<all> |
534 | ||
cleanup
|
535 |
my $rows = $result->all; |
updated pod
|
536 | |
cleanup
|
537 |
Same as C<fetch_hash_all>. |
updated pod
|
538 | |
removed DBIx::Custom commit ...
|
539 |
=head2 C<fetch> |
packaging one directory
|
540 | |
cleanup
|
541 |
my $row = $result->fetch; |
version 0.0901
|
542 | |
cleanup
|
543 |
Fetch a row and put it into array reference. |
packaging one directory
|
544 | |
removed DBIx::Custom commit ...
|
545 |
=head2 C<fetch_all> |
packaging one directory
|
546 | |
cleanup
|
547 |
my $rows = $result->fetch_all; |
version 0.0901
|
548 | |
cleanup
|
549 |
Fetch all rows and put them into array of array reference. |
packaging one directory
|
550 | |
- renamed DBIx::Custom::Resu...
|
551 |
=head2 C<fetch_one> |
cleanup
|
552 | |
cleanup
|
553 |
my $row = $result->fetch_one; |
cleanup
|
554 | |
cleanup
|
555 |
Fetch only a first row and put it into array reference, |
556 |
and finish statment handle. |
|
cleanup
|
557 | |
removed DESTROY method(not b...
|
558 |
=head2 C<fetch_hash> |
packaging one directory
|
559 | |
cleanup
|
560 |
my $row = $result->fetch_hash; |
packaging one directory
|
561 | |
cleanup
|
562 |
Fetch a row and put it into hash reference. |
update document
|
563 | |
cleanup
|
564 |
=head2 C<fetch_hash_all> |
565 | ||
cleanup
|
566 |
my $rows = $result->fetch_hash_all; |
cleanup
|
567 | |
cleanup
|
568 |
Fetch all rows and put them into array of hash reference. |
cleanup
|
569 | |
- renamed DBIx::Custom::Resu...
|
570 |
=head2 C<fetch_hash_one> |
cleanup
|
571 |
|
572 |
my $row = $result->fetch_hash_one; |
|
packaging one directory
|
573 | |
cleanup
|
574 |
Fetch only a first row and put it into hash reference, |
575 |
and finish statment handle. |
|
packaging one directory
|
576 | |
removed DESTROY method(not b...
|
577 |
=head2 C<fetch_hash_multi> |
update document
|
578 | |
cleanup
|
579 |
my $rows = $result->fetch_hash_multi(5); |
580 |
|
|
cleanup
|
581 |
Fetch multiple rows and put them into array of hash reference. |
update document
|
582 | |
cleanup
|
583 |
=head2 C<fetch_multi> |
packaging one directory
|
584 | |
cleanup
|
585 |
my $rows = $result->fetch_multi(5); |
586 |
|
|
cleanup
|
587 |
Fetch multiple rows and put them into array of array reference. |
removed DESTROY method(not b...
|
588 | |
cleanup
|
589 |
=head2 C<filter> |
590 | ||
cleanup
|
591 |
$result->filter(title => sub { uc $_[0] }, author => 'to_upper'); |
592 |
$result->filter([qw/title author/] => 'to_upper'); |
|
added experimental DBIx::Cus...
|
593 | |
cleanup
|
594 |
Set filter for column. |
595 |
You can use subroutine or filter name as filter. |
|
- DBIx::Custom Model filter ...
|
596 |
This filter is executed after C<type_rule> filter. |
cleanup
|
597 | |
- removed EXPERIMENTAL statu...
|
598 |
=head2 C<flat> |
added EXPERIMENTAL DBIx::Cus...
|
599 | |
updated pod
|
600 |
my @list = $result->flat; |
added EXPERIMENTAL DBIx::Cus...
|
601 | |
updated pod
|
602 |
All values is added to flatten list. |
added EXPERIMENTAL DBIx::Cus...
|
603 |
|
updated pod
|
604 |
my @list = $dbi->select(['id', 'title'])->flat; |
added EXPERIMENTAL DBIx::Cus...
|
605 | |
606 |
C<flat> method return the following data. |
|
607 | ||
608 |
(1, 'Perl', 2, 'Ruby') |
|
609 | ||
610 |
You can create key-value pair easily. |
|
611 | ||
612 |
my %titles = $dbi->select(['id', 'title'])->flat; |
|
613 | ||
- removed EXPERIMENTAL statu...
|
614 |
=head2 C<kv> |
added EXPERIMETAL DBIx::Cust...
|
615 | |
616 |
my $key_value = $result->kv; |
|
617 |
my $key_values = $result->kv(multi => 1); |
|
618 | ||
619 |
Get key-value pairs. |
|
620 | ||
621 |
my $books = $dbi->select(['id', 'title', 'author'])->kv; |
|
622 | ||
623 |
If C<all> method return the following data: |
|
624 | ||
625 |
[ |
|
626 |
{id => 1, title => 'Perl', author => 'Ken'}, |
|
627 |
{id => 2, title => 'Ruby', author => 'Taro'} |
|
628 |
] |
|
629 | ||
630 |
C<kv> method return the following data. |
|
631 | ||
632 |
{ |
|
633 |
1 => {title => 'Perl', author => 'Ken'}, |
|
634 |
2 => {title => 'Ruby', author => 'Taro'} |
|
635 |
} |
|
636 | ||
637 |
First column value become key. |
|
638 | ||
639 |
If value contains multipule data, you can push it to |
|
640 |
array refernce by C<multi> option. |
|
641 | ||
642 |
my $books = $dbi->select(['author', 'title', 'price'])->kv(multi => 1); |
|
643 | ||
644 |
If C<all> method return the following data: |
|
645 | ||
646 |
[ |
|
647 |
{author => 'Ken', title => 'Perl', price => 1000}, |
|
648 |
{author => 'Ken', title => 'Good', price => 2000}, |
|
649 |
{author => 'Taro', title => 'Ruby', price => 3000} |
|
650 |
{author => 'Taro', title => 'Sky', price => 4000} |
|
651 |
] |
|
652 | ||
653 |
C<kv> method return the following data. |
|
654 | ||
655 |
{ |
|
656 |
Ken => [ |
|
657 |
{title => 'Perl', price => 1000}, |
|
658 |
{title => 'Good', price => 2000} |
|
659 |
], |
|
660 |
Taro => [ |
|
661 |
{title => 'Ruby', price => 3000}, |
|
662 |
{title => 'Sky', price => 4000} |
|
663 |
] |
|
664 |
} |
|
665 | ||
- removed EXPERIMENTAL statu...
|
666 |
=head2 C<header> |
added EXPERIMENTAL DBIx::Cus...
|
667 | |
cleanup
|
668 |
my $header = $result->header; |
added EXPERIMENTAL DBIx::Cus...
|
669 | |
670 |
Get header column names. |
|
671 | ||
updated pod
|
672 |
=head2 C<one> |
673 | ||
cleanup
|
674 |
my $row = $result->one; |
updated pod
|
675 | |
- renamed DBIx::Custom::Resu...
|
676 |
Alias for C<fetch_hash_one>. |
added experimental DBIx::Cus...
|
677 | |
- removed DEPRECATED DBIx::C...
|
678 |
=head2 C<stash> |
added experimental DBIx::Cus...
|
679 | |
cleanup
|
680 |
my $stash = $result->stash; |
681 |
my $foo = $result->stash->{foo}; |
|
682 |
$result->stash->{foo} = $foo; |
|
added experimental DBIx::Cus...
|
683 | |
- added EXPERIMENTAL DBIx::C...
|
684 |
Stash is hash reference to save some data. |
added experimental DBIx::Cus...
|
685 | |
- removed EXPERIMENTAL flag ...
|
686 |
=head2 C<type_rule> |
cleanup
|
687 |
|
688 |
# Merge type rule |
|
689 |
$result->type_rule( |
|
690 |
# DATE |
|
691 |
9 => sub { ... }, |
|
692 |
# DATETIME or TIMESTAMP |
|
693 |
11 => sub { ... } |
|
694 |
); |
|
695 | ||
696 |
# Replace type rule(by reference) |
|
697 |
$result->type_rule([ |
|
698 |
# DATE |
|
699 |
9 => sub { ... }, |
|
700 |
# DATETIME or TIMESTAMP |
|
701 |
11 => sub { ... } |
|
702 |
]); |
|
EXPERIMENTAL type_rule_off i...
|
703 | |
cleanup
|
704 |
This is same as L<DBIx::Custom>'s C<type_rule>'s <from>. |
EXPERIMENTAL type_rule_off i...
|
705 | |
- removed EXPERIMENTAL flag ...
|
706 |
=head2 C<type_rule_off> |
- changed EXPERIMENTAL DBIx:...
|
707 | |
cleanup
|
708 |
$result = $result->type_rule_off; |
- changed EXPERIMENTAL DBIx:...
|
709 | |
710 |
Turn C<from1> and C<from2> type rule off. |
|
711 |
By default, type rule is on. |
|
712 | ||
- removed EXPERIMENTAL flag ...
|
713 |
=head2 C<type_rule_on> |
- changed EXPERIMENTAL DBIx:...
|
714 | |
cleanup
|
715 |
$result = $result->type_rule_on; |
- changed EXPERIMENTAL DBIx:...
|
716 | |
717 |
Turn C<from1> and C<from2> type rule on. |
|
718 |
By default, type rule is on. |
|
719 | ||
- removed EXPERIMENTAL flag ...
|
720 |
=head2 C<type_rule1_off> |
- changed EXPERIMENTAL DBIx:...
|
721 | |
cleanup
|
722 |
$result = $result->type_rule1_off; |
- changed EXPERIMENTAL DBIx:...
|
723 | |
724 |
Turn C<from1> type rule off. |
|
725 |
By default, type rule is on. |
|
726 | ||
- removed EXPERIMENTAL flag ...
|
727 |
=head2 C<type_rule1_on> |
- changed EXPERIMENTAL DBIx:...
|
728 | |
cleanup
|
729 |
$result = $result->type_rule1_on; |
- changed EXPERIMENTAL DBIx:...
|
730 | |
731 |
Turn C<from1> type rule on. |
|
732 |
By default, type rule is on. |
|
733 | ||
- removed EXPERIMENTAL flag ...
|
734 |
=head2 C<type_rule2_off> |
- changed EXPERIMENTAL DBIx:...
|
735 | |
cleanup
|
736 |
$result = $result->type_rule2_off; |
- changed EXPERIMENTAL DBIx:...
|
737 | |
738 |
Turn C<from2> type rule off. |
|
739 |
By default, type rule is on. |
|
740 | ||
- removed EXPERIMENTAL flag ...
|
741 |
=head2 C<type_rule2_on> |
- changed EXPERIMENTAL DBIx:...
|
742 | |
cleanup
|
743 |
$result = $result->type_rule2_on; |
- changed EXPERIMENTAL DBIx:...
|
744 | |
745 |
Turn C<from2> type rule on. |
|
746 |
By default, type rule is on. |
|
747 | ||
- removed EXPERIMENTAL statu...
|
748 |
=head2 C<value> |
- added EXPERIMENTAL DBIx::C...
|
749 | |
cleanup
|
750 |
my $value = $result->value; |
- added EXPERIMENTAL DBIx::C...
|
751 | |
752 |
Get first column's first value. |
|
753 | ||
updated pod
|
754 |
my $count = $dbi->select('count(*)', table => 'book')->value; |
755 | ||
756 |
This is almost same as the following one. |
|
757 | ||
758 |
my $count = $dbi->select('count(*)')->fetch_one->[0]; |
|
- added EXPERIMENTAL DBIx::C...
|
759 | |
- added DBIX_CUSTOM_SUPPRESS...
|
760 |
=head2 C<values> |
761 | ||
762 |
my $values = $result->values; |
|
763 | ||
764 |
Get first column's values. |
|
765 | ||
updated pod
|
766 |
my $tables = $dbi->select('show tables')->values; |
767 | ||
768 |
This is same as the following one. |
|
769 | ||
770 |
my $rows = $dbi->select('show tables')->fetch_all; |
|
771 |
my $tables = [map { $_->[0] } @$rows]; |
|
- added DBIX_CUSTOM_SUPPRESS...
|
772 | |
packaging one directory
|
773 |
=cut |