use strict;
use warnings;
use base 'DBIx::Class';
+use List::Util qw(first);
=head1 NAME
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;
}
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;
}
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
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;
return ( $cursor->next )[ 0 ];
}
+=head2 throw_exception
+
+See L<DBIx::Class::Schema/throw_exception> 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
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({});
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' });