Skip to content

Commit

Permalink
DBIx::Custom::Result::kv method's multi option is DEPRECATED! added D…
Browse files Browse the repository at this point in the history
…BIx::Custom::Result::kvs method instead.
  • Loading branch information
yuki-kimoto committed Mar 4, 2013
1 parent 98d77d6 commit 4b275b8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
5 changes: 4 additions & 1 deletion Changes
@@ -1,4 +1,7 @@
0.27 (2012-09-17)
0.28
- DBIx::Custom::Result::kv method's multi option is DEPRECATED!
added DBIx::Custom::Result::kvs method instead.
0.27
- fixed documentation miss about execute method's table_alias option.
0.26
- fixed bug that when id option's value is object, don't work.
Expand Down
2 changes: 1 addition & 1 deletion lib/DBIx/Custom.pm
Expand Up @@ -2,7 +2,7 @@ use 5.008007;
package DBIx::Custom;
use Object::Simple -base;

our $VERSION = '0.27';
our $VERSION = '0.28';

use Carp 'croak';
use DBI;
Expand Down
29 changes: 24 additions & 5 deletions lib/DBIx/Custom/Result.pm
Expand Up @@ -256,6 +256,8 @@ sub kv {
my $key_value = delete $row->{$key_name};
next unless defined $key_value;
if ($opt{multi}) {
_deprecate('0.28', "DBIx::Custom::Result::kv method's "
. 'multi option is DEPRECATED. use kvs method instead');
$kv->{$key_value} ||= [];
push @{$kv->{$key_value}}, $row;
}
Expand All @@ -265,6 +267,21 @@ sub kv {
return $kv;
}

sub kvs {
my ($self, %opt) = @_;

my $key_name = $self->{sth}{NAME}[0];
my $kv = {};
while (my $row = $self->fetch_hash) {
my $key_value = delete $row->{$key_name};
next unless defined $key_value;
$kv->{$key_value} ||= [];
push @{$kv->{$key_value}}, $row;
}

return $kv;
}

sub header { shift->sth->{NAME} }

*one = \&fetch_hash_one;
Expand Down Expand Up @@ -614,7 +631,6 @@ You can create key-value pair easily.
=head2 C<kv>
my $key_value = $result->kv;
my $key_values = $result->kv(multi => 1);
Get key-value pairs.
Expand All @@ -636,10 +652,13 @@ C<kv> method return the following data.
First column value become key.
If value contains multipule data, you can push it to
array refernce by C<multi> option.
=head2 C<kvs>
my $books = $dbi->select(['author', 'title', 'price'])->kv(multi => 1);
my $key_values = $result->kvs;
Get key-values pairs.
my $books = $dbi->select(['author', 'title', 'price'])->kvs;
If C<all> method return the following data:
Expand All @@ -650,7 +669,7 @@ If C<all> method return the following data:
{author => 'Taro', title => 'Sky', price => 4000}
]
C<kv> method return the following data.
C<kvs> method return the following data.
{
Ken => [
Expand Down
14 changes: 13 additions & 1 deletion t/common.t
Expand Up @@ -5,7 +5,7 @@ use Encode qw/encode_utf8/;
use FindBin;
use Scalar::Util 'isweak';

$ENV{DBIX_CUSTOM_SUPPRESS_DEPRECATION} = '0.25';
$ENV{DBIX_CUSTOM_SUPPRESS_DEPRECATION} = '0.28';

my $dbi;

Expand Down Expand Up @@ -3767,6 +3767,18 @@ is_deeply($rows, {
]
});

$result = $dbi->select([$key1, $key2], table => $table1, append => "order by $key2");
$rows = $result->kvs;
is_deeply($rows, {
0 => [
{$key2 => 1},
{$key2 => 2}
],
3 => [
{$key2 => 4},
{$key2 => 5}
]
});

test 'DBIx::Custom::Result fetch_multi';
eval { $dbi->execute("drop table $table1") };
Expand Down

0 comments on commit 4b275b8

Please sign in to comment.