From: Norbert Buchmuller Date: Tue, 11 Nov 2008 08:29:09 +0000 (+0100) Subject: * Fixed test cases of ResultSetColumn vs. +select/+as (they used to unconditionally... X-Git-Tag: v0.08240~237^2~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5d1fc7dc2e7f2cd82068f3f2f5faca1851225ccf;p=dbsrgits%2FDBIx-Class.git * Fixed test cases of ResultSetColumn vs. +select/+as (they used to unconditionally pass). * Added a test case of ResultSetColumn for a non-existent column (note: a valid SQL fragment still can be passed instead of a column). * Added a test case of ResultSetColumn that tests overriding a column using select/as. * ResultSetColumn now respects select/as/+select/+as. * Added missing throw_exception() to ResultSetColumn. --- diff --git a/lib/DBIx/Class/ResultSetColumn.pm b/lib/DBIx/Class/ResultSetColumn.pm index 68cc4e0..3780853 100644 --- a/lib/DBIx/Class/ResultSetColumn.pm +++ b/lib/DBIx/Class/ResultSetColumn.pm @@ -2,6 +2,7 @@ package DBIx::Class::ResultSetColumn; use strict; use warnings; use base 'DBIx::Class'; +use List::Util qw(first); =head1 NAME @@ -36,8 +37,13 @@ sub new { my ($class, $rs, $column) = @_; $class = ref $class if ref $class; my $new_parent_rs = $rs->search_rs; # we don't want to mess up the original, so clone it - $new_parent_rs->{attrs}->{prefetch} = undef; # prefetch causes additional columns to be fetched - my $new = bless { _column => $column, _parent_resultset => $new_parent_rs }, $class; + my $attrs = $new_parent_rs->_resolved_attrs; + $new_parent_rs->{attrs}->{$_} = undef for qw(prefetch include_columns +select +as); # prefetch, include_columns, +select, +as cause additional columns to be fetched + my ($select, $as) = + map { defined $_ ? ($attrs->{select}->[$_], $attrs->{as}->[$_]) : ($column, $column) } + first { ($attrs->{as} || [])->[$_] eq $column } + 0..$#{$attrs->{as} || []}; + my $new = bless { _select => $select, _as => $as, _parent_resultset => $new_parent_rs }, $class; $new->throw_exception("column must be supplied") unless $column; return $new; } @@ -62,7 +68,7 @@ one value. sub next { my $self = shift; - $self->{_resultset} = $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]}) unless ($self->{_resultset}); + $self->{_resultset} = $self->{_parent_resultset}->search(undef, {select => [$self->{_select}], as => [$self->{_as}]}) unless ($self->{_resultset}); my ($row) = $self->{_resultset}->cursor->next; return $row; } @@ -87,7 +93,7 @@ than row objects. sub all { my $self = shift; - return map {$_->[0]} $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]})->cursor->all; + return map {$_->[0]} $self->{_parent_resultset}->search(undef, {select => [$self->{_select}], as => [$self->{_as}]})->cursor->all; } =head2 min @@ -175,7 +181,7 @@ value. Produces the following SQL: sub func { my ($self,$function) = @_; - my $cursor = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_column}}, as => [$self->{_column}]})->cursor; + my $cursor = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_select}}, as => [$self->{_as}]})->cursor; if( wantarray ) { return map { $_->[ 0 ] } $cursor->all; @@ -184,6 +190,22 @@ sub func { return ( $cursor->next )[ 0 ]; } +=head2 throw_exception + +See L for details. + +=cut + +sub throw_exception { + my $self=shift; + if (ref $self && $self->{_parent_resultset}) { + $self->{_parent_resultset}->throw_exception(@_) + } else { + croak(@_); + } +} + + 1; =head1 AUTHORS diff --git a/t/88result_set_column.t b/t/88result_set_column.t index 52221f9..67262f0 100644 --- a/t/88result_set_column.t +++ b/t/88result_set_column.t @@ -2,12 +2,13 @@ use strict; use warnings; use Test::More; +use Test::Exception; use lib qw(t/lib); use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 14; +plan tests => 16; my $cd; my $rs = $cd = $schema->resultset("CD")->search({}); @@ -28,22 +29,34 @@ is($rs_title->min, 'Caterwaulin\' Blues', "min okay for title"); cmp_ok($rs_year->sum, '==', 9996, "three artists returned"); +# test +select/+as for single column my $psrs = $schema->resultset('CD')->search({}, { '+select' => \'COUNT(*)', '+as' => 'count' } ); -ok(defined($psrs->get_column('count')), '+select/+as count'); +lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as additional column "count" present (scalar)'); +dies_ok(sub { $psrs->get_column('noSuchColumn')->next }, '+select/+as nonexistent column throws exception'); +# test +select/+as for multiple columns $psrs = $schema->resultset('CD')->search({}, { '+select' => [ \'COUNT(*)', 'title' ], '+as' => [ 'count', 'addedtitle' ] } ); -ok(defined($psrs->get_column('count')), '+select/+as arrayref count'); -ok(defined($psrs->get_column('addedtitle')), '+select/+as title'); +lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as multiple additional columns, "count" column present'); +lives_ok(sub { $psrs->get_column('addedtitle')->next }, '+select/+as multiple additional columns, "addedtitle" column present'); + +# test +select/+as for overriding a column +$psrs = $schema->resultset('CD')->search({}, + { + 'select' => \"'The Final Countdown'", + 'as' => 'title' + } +); +is($psrs->get_column('title')->next, 'The Final Countdown', '+select/+as overridden column "title"'); { my $rs = $schema->resultset("CD")->search({}, { prefetch => 'artist' });