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}) { |
|
DBIx::Custom::Result::kv met...
|
259 |
_deprecate('0.28', "DBIx::Custom::Result::kv method's " |
260 |
. 'multi option is DEPRECATED. use kvs method instead'); |
|
added EXPERIMETAL DBIx::Cust...
|
261 |
$kv->{$key_value} ||= []; |
262 |
push @{$kv->{$key_value}}, $row; |
|
263 |
} |
|
264 |
else { $kv->{$key_value} = $row } |
|
265 |
} |
|
266 |
|
|
267 |
return $kv; |
|
268 |
} |
|
269 | ||
DBIx::Custom::Result::kv met...
|
270 |
sub kvs { |
271 |
my ($self, %opt) = @_; |
|
272 | ||
273 |
my $key_name = $self->{sth}{NAME}[0]; |
|
274 |
my $kv = {}; |
|
275 |
while (my $row = $self->fetch_hash) { |
|
276 |
my $key_value = delete $row->{$key_name}; |
|
277 |
next unless defined $key_value; |
|
278 |
$kv->{$key_value} ||= []; |
|
279 |
push @{$kv->{$key_value}}, $row; |
|
280 |
} |
|
281 |
|
|
282 |
return $kv; |
|
283 |
} |
|
284 | ||
added EXPERIMENTAL DBIx::Cus...
|
285 |
sub header { shift->sth->{NAME} } |
286 | ||
- renamed DBIx::Custom::Resu...
|
287 |
*one = \&fetch_hash_one; |
- added DBIx::Custom::Result...
|
288 | |
added EXPERIMENTAL DBIx::Cus...
|
289 |
sub type_rule { |
cleanup
|
290 |
my $self = shift; |
291 |
|
|
292 |
if (@_) { |
|
293 |
my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
294 | ||
295 |
# From |
|
296 |
for my $i (1 .. 2) { |
|
297 |
$type_rule->{"from$i"} = _array_to_hash($type_rule->{"from$i"}); |
|
298 |
for my $data_type (keys %{$type_rule->{"from$i"} || {}}) { |
|
299 |
croak qq{data type of from$i section must be lower case or number} |
|
300 |
if $data_type =~ /[A-Z]/; |
|
301 |
my $fname = $type_rule->{"from$i"}{$data_type}; |
|
302 |
if (defined $fname && ref $fname ne 'CODE') { |
|
303 |
croak qq{Filter "$fname" is not registered" } . _subname |
|
304 |
unless exists $self->dbi->filters->{$fname}; |
|
305 |
|
|
306 |
$type_rule->{"from$i"}{$data_type} = $self->dbi->filters->{$fname}; |
|
added EXPERIMENTAL DBIx::Cus...
|
307 |
} |
cleanup
|
308 |
} |
added EXPERIMENTAL DBIx::Cus...
|
309 |
} |
cleanup
|
310 |
$self->{type_rule} = $type_rule; |
added EXPERIMENTAL DBIx::Cus...
|
311 |
|
cleanup
|
312 |
return $self; |
313 |
} |
|
314 |
|
|
315 |
return $self->{type_rule} || {}; |
|
added EXPERIMENTAL DBIx::Cus...
|
316 |
} |
317 | ||
- changed EXPERIMENTAL DBIx:...
|
318 |
sub type_rule_off { |
cleanup
|
319 |
my $self = shift; |
320 |
$self->{type_rule_off} = 1; |
|
321 |
return $self; |
|
- changed EXPERIMENTAL DBIx:...
|
322 |
} |
323 | ||
324 |
sub type_rule_on { |
|
cleanup
|
325 |
my $self = shift; |
326 |
$self->{type_rule_off} = 0; |
|
327 |
return $self; |
|
- changed EXPERIMENTAL DBIx:...
|
328 |
} |
329 | ||
330 |
sub type_rule1_off { |
|
cleanup
|
331 |
my $self = shift; |
332 |
$self->{type_rule1_off} = 1; |
|
333 |
return $self; |
|
- changed EXPERIMENTAL DBIx:...
|
334 |
} |
335 | ||
336 |
sub type_rule1_on { |
|
cleanup
|
337 |
my $self = shift; |
338 |
$self->{type_rule1_off} = 0; |
|
339 |
return $self; |
|
- changed EXPERIMENTAL DBIx:...
|
340 |
} |
341 | ||
342 |
sub type_rule2_off { |
|
cleanup
|
343 |
my $self = shift; |
344 |
$self->{type_rule2_off} = 1; |
|
345 |
return $self; |
|
- changed EXPERIMENTAL DBIx:...
|
346 |
} |
347 | ||
348 |
sub type_rule2_on { |
|
cleanup
|
349 |
my $self = shift; |
350 |
$self->{type_rule2_off} = 0; |
|
351 |
return $self; |
|
- changed EXPERIMENTAL DBIx:...
|
352 |
} |
353 | ||
- added EXPERIMENTAL DBIx::C...
|
354 |
sub value { |
cleanup
|
355 |
my $self = shift; |
356 |
my $row = $self->fetch_one; |
|
357 |
my $value = $row ? $row->[0] : undef; |
|
358 |
return $value; |
|
- added EXPERIMENTAL DBIx::C...
|
359 |
} |
360 | ||
- added DBIX_CUSTOM_SUPPRESS...
|
361 |
sub values { |
362 |
my $self = shift; |
|
363 |
|
|
364 |
my $values = []; |
|
365 |
my $rows = $self->fetch_all; |
|
366 |
push @$values, $_->[0] for @$rows; |
|
367 |
return $values; |
|
368 |
} |
|
369 | ||
- fixed bug DBIx::Custom::Re...
|
370 |
sub _cache { |
cleanup
|
371 |
my $self = shift; |
372 |
$self->{_type_map} = {}; |
|
373 |
$self->{_pos} = {}; |
|
374 |
$self->{_columns} = {}; |
|
- removed EXPERIMENTAL statu...
|
375 |
for (my $i = 0; $i < @{$self->{sth}->{NAME} || []}; $i++) { |
cleanup
|
376 |
my $type = lc $self->{sth}{TYPE}[$i]; |
377 |
my $name = $self->{sth}{NAME}[$i]; |
|
378 |
$self->{_type_map}{$type} ||= []; |
|
379 |
push @{$self->{_type_map}{$type}}, $name; |
|
380 |
$self->{_pos}{$name} ||= []; |
|
381 |
push @{$self->{_pos}{$name}}, $i; |
|
382 |
$self->{_columns}{$name} = 1; |
|
383 |
} |
|
384 |
$self->{_cache} = 1; |
|
- fixed bug DBIx::Custom::Re...
|
385 |
} |
386 | ||
- added DBIX_CUSTOM_SUPPRESS...
|
387 |
# DEPRECATED! |
388 |
sub column { |
|
389 |
my $self = shift; |
|
390 |
|
|
391 |
_deprecate('0.25', "DBIx::Custom::Result::column method is DEPRECATED. " |
|
392 |
. "use values method instead"); |
|
393 |
|
|
394 |
return $self->values(@_); |
|
395 |
} |
|
396 | ||
- renamed DBIx::Custom::Resu...
|
397 |
# DEPRECATED! |
398 |
sub fetch_hash_first { |
|
cleanup
|
399 |
my $self = shift; |
- added DBIX_CUSTOM_SUPPRESS...
|
400 |
_deprecate('0.24', "DBIx::Custom::Result::fetch_hash_first is DEPRECATED! " |
401 |
. "use fetch_hash_one instead"); |
|
cleanup
|
402 |
return $self->fetch_hash_one(@_); |
- renamed DBIx::Custom::Resu...
|
403 |
} |
404 | ||
405 |
# DEPRECATED! |
|
406 |
sub fetch_first { |
|
cleanup
|
407 |
my $self = shift; |
- added DBIX_CUSTOM_SUPPRESS...
|
408 |
_deprecate('0.24', "DBIx::Custom::Result::fetch_first is DEPRECATED! " |
409 |
. " use fetch_one instead"); |
|
cleanup
|
410 |
return $self->fetch_one(@_); |
- renamed DBIx::Custom::Resu...
|
411 |
} |
412 | ||
- DBIx::Custom::Result filte...
|
413 |
# DEPRECATED! |
414 |
sub filter_off { |
|
- added DBIX_CUSTOM_SUPPRESS...
|
415 |
_deprecate('0.24', "filter_off method is DEPRECATED!"); |
cleanup
|
416 |
my $self = shift; |
417 |
$self->{filter_off} = 1; |
|
418 |
return $self; |
|
- DBIx::Custom::Result filte...
|
419 |
} |
420 | ||
421 |
# DEPRECATED! |
|
422 |
sub filter_on { |
|
- added DBIX_CUSTOM_SUPPRESS...
|
423 |
_deprecated('0.24', "filter_on method is DEPRECATED!"); |
cleanup
|
424 |
my $self = shift; |
425 |
$self->{filter_off} = 0; |
|
426 |
return $self; |
|
- DBIx::Custom::Result filte...
|
427 |
} |
428 | ||
cleanup
|
429 |
# DEPRECATED! |
430 |
sub end_filter { |
|
- added DBIX_CUSTOM_SUPPRESS...
|
431 |
_deprecate('0.24', "end_filter method is DEPRECATED!"); |
cleanup
|
432 |
my $self = shift; |
433 |
if (@_) { |
|
434 |
my $end_filter = {}; |
|
435 |
if (ref $_[0] eq 'HASH') { $end_filter = $_[0] } |
|
436 |
else { |
|
437 |
$end_filter = _array_to_hash( |
|
438 |
@_ > 1 ? [@_] : $_[0] |
|
439 |
); |
|
440 |
} |
|
441 |
for my $column (keys %$end_filter) { |
|
442 |
my $fname = $end_filter->{$column}; |
|
443 |
if (exists $end_filter->{$column} |
|
444 |
&& defined $fname |
|
445 |
&& ref $fname ne 'CODE') |
|
446 |
{ |
|
447 |
croak qq{Filter "$fname" is not registered" } . _subname |
|
448 |
unless exists $self->dbi->filters->{$fname}; |
|
449 |
$end_filter->{$column} = $self->dbi->filters->{$fname}; |
|
450 |
} |
|
cleanup
|
451 |
} |
cleanup
|
452 |
$self->{end_filter} = {%{$self->end_filter}, %$end_filter}; |
453 |
return $self; |
|
454 |
} |
|
455 |
return $self->{end_filter} ||= {}; |
|
cleanup
|
456 |
} |
cleanup
|
457 |
# DEPRECATED! |
added experimental DBIx::Cus...
|
458 |
sub remove_end_filter { |
- added DBIX_CUSTOM_SUPPRESS...
|
459 |
_deprecate('0.24', "remove_end_filter is DEPRECATED!"); |
cleanup
|
460 |
my $self = shift; |
461 |
$self->{end_filter} = {}; |
|
462 |
return $self; |
|
added experimental DBIx::Cus...
|
463 |
} |
cleanup
|
464 |
# DEPRECATED! |
added experimental DBIx::Cus...
|
465 |
sub remove_filter { |
- added DBIX_CUSTOM_SUPPRESS...
|
466 |
_deprecate('0.24', "remove_filter is DEPRECATED!"); |
cleanup
|
467 |
my $self = shift; |
468 |
$self->{filter} = {}; |
|
469 |
return $self; |
|
added experimental DBIx::Cus...
|
470 |
} |
cleanup
|
471 |
# DEPRECATED! |
cleanup
|
472 |
sub default_filter { |
- added DBIX_CUSTOM_SUPPRESS...
|
473 |
_deprecate('0.24', "default_filter is DEPRECATED!"); |
cleanup
|
474 |
my $self = shift; |
475 |
if (@_) { |
|
476 |
my $fname = $_[0]; |
|
477 |
if (@_ && !$fname) { |
|
478 |
$self->{default_filter} = undef; |
|
cleanup
|
479 |
} |
cleanup
|
480 |
else { |
481 |
croak qq{Filter "$fname" is not registered} |
|
482 |
unless exists $self->dbi->filters->{$fname}; |
|
483 |
$self->{default_filter} = $self->dbi->filters->{$fname}; |
|
484 |
} |
|
485 |
return $self; |
|
486 |
} |
|
487 |
return $self->{default_filter}; |
|
cleanup
|
488 |
} |
cleanup
|
489 |
# DEPRECATED! |
cleanup
|
490 |
has 'filter_check'; |
cleanup
|
491 | |
update document
|
492 |
1; |
493 | ||
packaging one directory
|
494 |
=head1 NAME |
495 | ||
cleanup
|
496 |
DBIx::Custom::Result - Result of select statement |
packaging one directory
|
497 | |
update document
|
498 |
=head1 SYNOPSIS |
cleanup
|
499 | |
cleanup
|
500 |
# Result |
501 |
my $result = $dbi->select(table => 'book'); |
|
502 | ||
503 |
# Fetch a row and put it into array reference |
|
504 |
while (my $row = $result->fetch) { |
|
505 |
my $author = $row->[0]; |
|
506 |
my $title = $row->[1]; |
|
507 |
} |
|
508 |
|
|
509 |
# Fetch only a first row and put it into array reference |
|
510 |
my $row = $result->fetch_one; |
|
511 |
|
|
512 |
# Fetch all rows and put them into array of array reference |
|
513 |
my $rows = $result->fetch_all; |
|
514 | ||
515 |
# Fetch a row and put it into hash reference |
|
516 |
while (my $row = $result->fetch_hash) { |
|
517 |
my $title = $row->{title}; |
|
518 |
my $author = $row->{author}; |
|
519 |
} |
|
520 |
|
|
521 |
# Fetch only a first row and put it into hash reference |
|
522 |
my $row = $result->fetch_hash_one; |
|
523 |
my $row = $result->one; # Alias for "fetch_hash_one" |
|
524 |
|
|
525 |
# Fetch all rows and put them into array of hash reference |
|
526 |
my $rows = $result->fetch_hash_all; |
|
527 |
my $rows = $result->all; # Alias for "fetch_hash_all" |
|
packaging one directory
|
528 | |
update document
|
529 |
=head1 ATTRIBUTES |
packaging one directory
|
530 | |
cleanup and added deprecated...
|
531 |
=head2 dbi |
cleanup
|
532 | |
cleanup
|
533 |
my $dbi = $result->dbi; |
534 |
$result = $result->dbi($dbi); |
|
cleanup
|
535 | |
sub module use DBIx::Custom ...
|
536 |
L<DBIx::Custom> object. |
cleanup
|
537 | |
cleanup and added deprecated...
|
538 |
=head2 sth |
cleanup
|
539 | |
cleanup
|
540 |
my $sth = $reuslt->sth |
541 |
$result = $result->sth($sth); |
|
cleanup
|
542 | |
543 |
Statement handle of L<DBI>. |
|
544 | ||
update document
|
545 |
=head1 METHODS |
546 | ||
renamed build_query to creat...
|
547 |
L<DBIx::Custom::Result> inherits all methods from L<Object::Simple> |
cleanup
|
548 |
and implements the following new ones. |
packaging one directory
|
549 | |
cleanup and added deprecated...
|
550 |
=head2 all |
updated pod
|
551 | |
cleanup
|
552 |
my $rows = $result->all; |
updated pod
|
553 | |
cleanup and added deprecated...
|
554 |
Same as fetch_hash_all. |
updated pod
|
555 | |
cleanup and added deprecated...
|
556 |
=head2 fetch |
packaging one directory
|
557 | |
cleanup
|
558 |
my $row = $result->fetch; |
version 0.0901
|
559 | |
cleanup
|
560 |
Fetch a row and put it into array reference. |
packaging one directory
|
561 | |
cleanup and added deprecated...
|
562 |
=head2 fetch_all |
packaging one directory
|
563 | |
cleanup
|
564 |
my $rows = $result->fetch_all; |
version 0.0901
|
565 | |
cleanup
|
566 |
Fetch all rows and put them into array of array reference. |
packaging one directory
|
567 | |
cleanup and added deprecated...
|
568 |
=head2 fetch_one |
cleanup
|
569 | |
cleanup
|
570 |
my $row = $result->fetch_one; |
cleanup
|
571 | |
cleanup
|
572 |
Fetch only a first row and put it into array reference, |
573 |
and finish statment handle. |
|
cleanup
|
574 | |
cleanup and added deprecated...
|
575 |
=head2 fetch_hash |
packaging one directory
|
576 | |
cleanup
|
577 |
my $row = $result->fetch_hash; |
packaging one directory
|
578 | |
cleanup
|
579 |
Fetch a row and put it into hash reference. |
update document
|
580 | |
cleanup and added deprecated...
|
581 |
=head2 fetch_hash_all |
cleanup
|
582 | |
cleanup
|
583 |
my $rows = $result->fetch_hash_all; |
cleanup
|
584 | |
cleanup
|
585 |
Fetch all rows and put them into array of hash reference. |
cleanup
|
586 | |
cleanup and added deprecated...
|
587 |
=head2 fetch_hash_one |
cleanup
|
588 |
|
589 |
my $row = $result->fetch_hash_one; |
|
packaging one directory
|
590 | |
cleanup
|
591 |
Fetch only a first row and put it into hash reference, |
592 |
and finish statment handle. |
|
packaging one directory
|
593 | |
cleanup and added deprecated...
|
594 |
=head2 fetch_hash_multi |
update document
|
595 | |
cleanup
|
596 |
my $rows = $result->fetch_hash_multi(5); |
597 |
|
|
cleanup
|
598 |
Fetch multiple rows and put them into array of hash reference. |
update document
|
599 | |
cleanup and added deprecated...
|
600 |
=head2 fetch_multi |
packaging one directory
|
601 | |
cleanup
|
602 |
my $rows = $result->fetch_multi(5); |
603 |
|
|
cleanup
|
604 |
Fetch multiple rows and put them into array of array reference. |
removed DESTROY method(not b...
|
605 | |
cleanup and added deprecated...
|
606 |
=head2 filter |
cleanup
|
607 | |
cleanup
|
608 |
$result->filter(title => sub { uc $_[0] }, author => 'to_upper'); |
609 |
$result->filter([qw/title author/] => 'to_upper'); |
|
added experimental DBIx::Cus...
|
610 | |
cleanup
|
611 |
Set filter for column. |
612 |
You can use subroutine or filter name as filter. |
|
- DBIx::Custom Model filter ...
|
613 |
This filter is executed after C<type_rule> filter. |
cleanup
|
614 | |
cleanup and added deprecated...
|
615 |
=head2 flat |
added EXPERIMENTAL DBIx::Cus...
|
616 | |
updated pod
|
617 |
my @list = $result->flat; |
added EXPERIMENTAL DBIx::Cus...
|
618 | |
updated pod
|
619 |
All values is added to flatten list. |
added EXPERIMENTAL DBIx::Cus...
|
620 |
|
updated pod
|
621 |
my @list = $dbi->select(['id', 'title'])->flat; |
added EXPERIMENTAL DBIx::Cus...
|
622 | |
623 |
C<flat> method return the following data. |
|
624 | ||
625 |
(1, 'Perl', 2, 'Ruby') |
|
626 | ||
627 |
You can create key-value pair easily. |
|
628 | ||
629 |
my %titles = $dbi->select(['id', 'title'])->flat; |
|
630 | ||
cleanup and added deprecated...
|
631 |
=head2 kv |
added EXPERIMETAL DBIx::Cust...
|
632 | |
633 |
my $key_value = $result->kv; |
|
634 | ||
635 |
Get key-value pairs. |
|
636 | ||
637 |
my $books = $dbi->select(['id', 'title', 'author'])->kv; |
|
638 | ||
639 |
If C<all> method return the following data: |
|
640 | ||
641 |
[ |
|
642 |
{id => 1, title => 'Perl', author => 'Ken'}, |
|
643 |
{id => 2, title => 'Ruby', author => 'Taro'} |
|
644 |
] |
|
645 | ||
646 |
C<kv> method return the following data. |
|
647 | ||
648 |
{ |
|
649 |
1 => {title => 'Perl', author => 'Ken'}, |
|
650 |
2 => {title => 'Ruby', author => 'Taro'} |
|
651 |
} |
|
652 | ||
653 |
First column value become key. |
|
654 | ||
cleanup and added deprecated...
|
655 |
=head2 kvs |
added EXPERIMETAL DBIx::Cust...
|
656 | |
DBIx::Custom::Result::kv met...
|
657 |
my $key_values = $result->kvs; |
658 | ||
659 |
Get key-values pairs. |
|
660 | ||
661 |
my $books = $dbi->select(['author', 'title', 'price'])->kvs; |
|
added EXPERIMETAL DBIx::Cust...
|
662 | |
663 |
If C<all> method return the following data: |
|
664 | ||
665 |
[ |
|
666 |
{author => 'Ken', title => 'Perl', price => 1000}, |
|
667 |
{author => 'Ken', title => 'Good', price => 2000}, |
|
668 |
{author => 'Taro', title => 'Ruby', price => 3000} |
|
669 |
{author => 'Taro', title => 'Sky', price => 4000} |
|
670 |
] |
|
671 | ||
DBIx::Custom::Result::kv met...
|
672 |
C<kvs> method return the following data. |
added EXPERIMETAL DBIx::Cust...
|
673 | |
674 |
{ |
|
675 |
Ken => [ |
|
676 |
{title => 'Perl', price => 1000}, |
|
677 |
{title => 'Good', price => 2000} |
|
678 |
], |
|
679 |
Taro => [ |
|
680 |
{title => 'Ruby', price => 3000}, |
|
681 |
{title => 'Sky', price => 4000} |
|
682 |
] |
|
683 |
} |
|
684 | ||
cleanup and added deprecated...
|
685 |
=head2 header |
added EXPERIMENTAL DBIx::Cus...
|
686 | |
cleanup
|
687 |
my $header = $result->header; |
added EXPERIMENTAL DBIx::Cus...
|
688 | |
689 |
Get header column names. |
|
690 | ||
cleanup and added deprecated...
|
691 |
=head2 one |
updated pod
|
692 | |
cleanup
|
693 |
my $row = $result->one; |
updated pod
|
694 | |
- renamed DBIx::Custom::Resu...
|
695 |
Alias for C<fetch_hash_one>. |
added experimental DBIx::Cus...
|
696 | |
cleanup and added deprecated...
|
697 |
=head2 stash |
added experimental DBIx::Cus...
|
698 | |
cleanup
|
699 |
my $stash = $result->stash; |
700 |
my $foo = $result->stash->{foo}; |
|
701 |
$result->stash->{foo} = $foo; |
|
added experimental DBIx::Cus...
|
702 | |
- added EXPERIMENTAL DBIx::C...
|
703 |
Stash is hash reference to save some data. |
added experimental DBIx::Cus...
|
704 | |
cleanup and added deprecated...
|
705 |
=head2 type_rule |
cleanup
|
706 |
|
707 |
# Merge type rule |
|
708 |
$result->type_rule( |
|
709 |
# DATE |
|
710 |
9 => sub { ... }, |
|
711 |
# DATETIME or TIMESTAMP |
|
712 |
11 => sub { ... } |
|
713 |
); |
|
714 | ||
715 |
# Replace type rule(by reference) |
|
716 |
$result->type_rule([ |
|
717 |
# DATE |
|
718 |
9 => sub { ... }, |
|
719 |
# DATETIME or TIMESTAMP |
|
720 |
11 => sub { ... } |
|
721 |
]); |
|
EXPERIMENTAL type_rule_off i...
|
722 | |
cleanup
|
723 |
This is same as L<DBIx::Custom>'s C<type_rule>'s <from>. |
EXPERIMENTAL type_rule_off i...
|
724 | |
cleanup and added deprecated...
|
725 |
=head2 type_rule_off |
- changed EXPERIMENTAL DBIx:...
|
726 | |
cleanup
|
727 |
$result = $result->type_rule_off; |
- changed EXPERIMENTAL DBIx:...
|
728 | |
729 |
Turn C<from1> and C<from2> type rule off. |
|
730 |
By default, type rule is on. |
|
731 | ||
cleanup and added deprecated...
|
732 |
=head2 type_rule_on |
- changed EXPERIMENTAL DBIx:...
|
733 | |
cleanup
|
734 |
$result = $result->type_rule_on; |
- changed EXPERIMENTAL DBIx:...
|
735 | |
736 |
Turn C<from1> and C<from2> type rule on. |
|
737 |
By default, type rule is on. |
|
738 | ||
cleanup and added deprecated...
|
739 |
=head2 type_rule1_off |
- changed EXPERIMENTAL DBIx:...
|
740 | |
cleanup
|
741 |
$result = $result->type_rule1_off; |
- changed EXPERIMENTAL DBIx:...
|
742 | |
743 |
Turn C<from1> type rule off. |
|
744 |
By default, type rule is on. |
|
745 | ||
cleanup and added deprecated...
|
746 |
=head2 type_rule1_on |
- changed EXPERIMENTAL DBIx:...
|
747 | |
cleanup
|
748 |
$result = $result->type_rule1_on; |
- changed EXPERIMENTAL DBIx:...
|
749 | |
750 |
Turn C<from1> type rule on. |
|
751 |
By default, type rule is on. |
|
752 | ||
cleanup and added deprecated...
|
753 |
=head2 type_rule2_off |
- changed EXPERIMENTAL DBIx:...
|
754 | |
cleanup
|
755 |
$result = $result->type_rule2_off; |
- changed EXPERIMENTAL DBIx:...
|
756 | |
757 |
Turn C<from2> type rule off. |
|
758 |
By default, type rule is on. |
|
759 | ||
cleanup and added deprecated...
|
760 |
=head2 type_rule2_on |
- changed EXPERIMENTAL DBIx:...
|
761 | |
cleanup
|
762 |
$result = $result->type_rule2_on; |
- changed EXPERIMENTAL DBIx:...
|
763 | |
764 |
Turn C<from2> type rule on. |
|
765 |
By default, type rule is on. |
|
766 | ||
cleanup and added deprecated...
|
767 |
=head2 value |
- added EXPERIMENTAL DBIx::C...
|
768 | |
cleanup
|
769 |
my $value = $result->value; |
- added EXPERIMENTAL DBIx::C...
|
770 | |
771 |
Get first column's first value. |
|
772 | ||
updated pod
|
773 |
my $count = $dbi->select('count(*)', table => 'book')->value; |
774 | ||
775 |
This is almost same as the following one. |
|
776 | ||
777 |
my $count = $dbi->select('count(*)')->fetch_one->[0]; |
|
- added EXPERIMENTAL DBIx::C...
|
778 | |
cleanup and added deprecated...
|
779 |
=head2 values |
- added DBIX_CUSTOM_SUPPRESS...
|
780 | |
781 |
my $values = $result->values; |
|
782 | ||
783 |
Get first column's values. |
|
784 | ||
updated pod
|
785 |
my $tables = $dbi->select('show tables')->values; |
786 | ||
787 |
This is same as the following one. |
|
788 | ||
789 |
my $rows = $dbi->select('show tables')->fetch_all; |
|
790 |
my $tables = [map { $_->[0] } @$rows]; |
|
- added DBIX_CUSTOM_SUPPRESS...
|
791 | |
packaging one directory
|
792 |
=cut |