... | ... |
@@ -256,6 +256,24 @@ sub flat { |
256 | 256 |
return @flat; |
257 | 257 |
} |
258 | 258 |
|
259 |
+sub kv { |
|
260 |
+ my ($self, %opt) = @_; |
|
261 |
+ |
|
262 |
+ my $key_name = $self->{sth}{NAME}[0]; |
|
263 |
+ my $kv = {}; |
|
264 |
+ while (my $row = $self->fetch_hash) { |
|
265 |
+ my $key_value = delete $row->{$key_name}; |
|
266 |
+ next unless defined $key_value; |
|
267 |
+ if ($opt{multi}) { |
|
268 |
+ $kv->{$key_value} ||= []; |
|
269 |
+ push @{$kv->{$key_value}}, $row; |
|
270 |
+ } |
|
271 |
+ else { $kv->{$key_value} = $row } |
|
272 |
+ } |
|
273 |
+ |
|
274 |
+ return $kv; |
|
275 |
+} |
|
276 |
+ |
|
259 | 277 |
sub header { shift->sth->{NAME} } |
260 | 278 |
|
261 | 279 |
*one = \&fetch_hash_one; |
... | ... |
@@ -596,6 +614,58 @@ You can create key-value pair easily. |
596 | 614 |
|
597 | 615 |
my %titles = $dbi->select(['id', 'title'])->flat; |
598 | 616 |
|
617 |
+=head2 C<kv> EXPERIMENTAL |
|
618 |
+ |
|
619 |
+ my $key_value = $result->kv; |
|
620 |
+ my $key_values = $result->kv(multi => 1); |
|
621 |
+ |
|
622 |
+Get key-value pairs. |
|
623 |
+ |
|
624 |
+ my $books = $dbi->select(['id', 'title', 'author'])->kv; |
|
625 |
+ |
|
626 |
+If C<all> method return the following data: |
|
627 |
+ |
|
628 |
+ [ |
|
629 |
+ {id => 1, title => 'Perl', author => 'Ken'}, |
|
630 |
+ {id => 2, title => 'Ruby', author => 'Taro'} |
|
631 |
+ ] |
|
632 |
+ |
|
633 |
+C<kv> method return the following data. |
|
634 |
+ |
|
635 |
+ { |
|
636 |
+ 1 => {title => 'Perl', author => 'Ken'}, |
|
637 |
+ 2 => {title => 'Ruby', author => 'Taro'} |
|
638 |
+ } |
|
639 |
+ |
|
640 |
+First column value become key. |
|
641 |
+ |
|
642 |
+If value contains multipule data, you can push it to |
|
643 |
+array refernce by C<multi> option. |
|
644 |
+ |
|
645 |
+ my $books = $dbi->select(['author', 'title', 'price'])->kv(multi => 1); |
|
646 |
+ |
|
647 |
+If C<all> method return the following data: |
|
648 |
+ |
|
649 |
+ [ |
|
650 |
+ {author => 'Ken', title => 'Perl', price => 1000}, |
|
651 |
+ {author => 'Ken', title => 'Good', price => 2000}, |
|
652 |
+ {author => 'Taro', title => 'Ruby', price => 3000} |
|
653 |
+ {author => 'Taro', title => 'Sky', price => 4000} |
|
654 |
+ ] |
|
655 |
+ |
|
656 |
+C<kv> method return the following data. |
|
657 |
+ |
|
658 |
+ { |
|
659 |
+ Ken => [ |
|
660 |
+ {title => 'Perl', price => 1000}, |
|
661 |
+ {title => 'Good', price => 2000} |
|
662 |
+ ], |
|
663 |
+ Taro => [ |
|
664 |
+ {title => 'Ruby', price => 3000}, |
|
665 |
+ {title => 'Sky', price => 4000} |
|
666 |
+ ] |
|
667 |
+ } |
|
668 |
+ |
|
599 | 669 |
=head2 C<header> |
600 | 670 |
|
601 | 671 |
my $header = $result->header; |
... | ... |
@@ -3584,6 +3584,39 @@ $result = $dbi->select(table => $table1); |
3584 | 3584 |
$rows = [$result->flat]; |
3585 | 3585 |
is_deeply($rows, [1, 2, 3, 4]); |
3586 | 3586 |
|
3587 |
+test 'kv'; |
|
3588 |
+$dbi = DBIx::Custom->connect; |
|
3589 |
+eval { $dbi->execute("drop table $table1") }; |
|
3590 |
+$dbi->execute($create_table1); |
|
3591 |
+$dbi->insert({$key1 => 0, $key2 => 2}, table => $table1); |
|
3592 |
+$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1); |
|
3593 |
+ |
|
3594 |
+$result = $dbi->select([$key1, $key2], table => $table1, append => "order by $key1"); |
|
3595 |
+$rows = $result->kv; |
|
3596 |
+is_deeply($rows, {0 => {$key2 => 2}, 3 => {$key2 => 4}}); |
|
3597 |
+ |
|
3598 |
+$dbi = DBIx::Custom->connect; |
|
3599 |
+eval { $dbi->execute("drop table $table1") }; |
|
3600 |
+$dbi->execute($create_table1); |
|
3601 |
+$dbi->insert({$key1 => 0, $key2 => 1}, table => $table1); |
|
3602 |
+$dbi->insert({$key1 => 0, $key2 => 2}, table => $table1); |
|
3603 |
+$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1); |
|
3604 |
+$dbi->insert({$key1 => 3, $key2 => 5}, table => $table1); |
|
3605 |
+ |
|
3606 |
+$result = $dbi->select([$key1, $key2], table => $table1, append => "order by $key2"); |
|
3607 |
+$rows = $result->kv(multi => 1); |
|
3608 |
+is_deeply($rows, { |
|
3609 |
+ 0 => [ |
|
3610 |
+ {$key2 => 1}, |
|
3611 |
+ {$key2 => 2} |
|
3612 |
+ ], |
|
3613 |
+ 3 => [ |
|
3614 |
+ {$key2 => 4}, |
|
3615 |
+ {$key2 => 5} |
|
3616 |
+ ] |
|
3617 |
+}); |
|
3618 |
+ |
|
3619 |
+ |
|
3587 | 3620 |
test 'DBIx::Custom::Result fetch_multi'; |
3588 | 3621 |
eval { $dbi->execute("drop table $table1") }; |
3589 | 3622 |
$dbi->execute($create_table1); |