From: Peter Rabbitson Date: Thu, 13 Nov 2008 18:34:29 +0000 (+0000) Subject: resultset attribute from can now take a scalarref and use it verbatim X-Git-Tag: v0.08240~242 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class.git;a=commitdiff_plain;h=0eb27426ceda801e6e872dc09eba34833fc0aaf0;hp=c5bc9ba6e36d69c76fefc18a3653596a9ea13921 resultset attribute from can now take a scalarref and use it verbatim --- diff --git a/Changes b/Changes index 6b93887..8bd0118 100644 --- a/Changes +++ b/Changes @@ -1,4 +1,8 @@ Revision history for DBIx::Class + - Allow a scalarref to be supplied to the 'from' resultset attribute + - Classes submitted as result_class for a resultsource are now + automatically loaded via ensure_loaded() + - 'result_class' resultset attribute, identical to result_class() 0.08099_05 2008-10-30 21:30:00 (UTC) - Rewritte of Storage::DBI::connect_info(), extended with an diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index ea81980..b395932 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -86,7 +86,12 @@ sub _find_syntax { sub select { my ($self, $table, $fields, $where, $order, @rest) = @_; - $table = $self->_quote($table) unless ref($table); + if (ref $table eq 'SCALAR') { + $table = $$table; + } + elsif (not ref $table) { + $table = $self->_quote($table); + } local $self->{rownum_hack_count} = 1 if (defined $rest[0] && $self->{limit_dialect} eq 'RowNum'); @rest = (-1) unless defined $rest[0]; diff --git a/t/76select.t b/t/76select.t index 213ecba..ca04b26 100644 --- a/t/76select.t +++ b/t/76select.t @@ -8,7 +8,7 @@ use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 7; +plan tests => 11; my $rs = $schema->resultset('CD')->search({}, { @@ -42,3 +42,23 @@ $rs = $schema->resultset('CD')->search({}, lives_ok(sub { $rs->first->get_column('count') }, '+select/+as chained search 1st rscolumn present'); lives_ok(sub { $rs->first->get_column('addedtitle') }, '+select/+as chained search 1st rscolumn present'); lives_ok(sub { $rs->first->get_column('addedtitle2') }, '+select/+as chained search 3rd rscolumn present'); + + +# test the from search attribute (gets between the FROM and WHERE keywords, allows arbitrary subselects) +# also shows that outer select attributes are ok (i.e. order_by) +# +# from doesn't seem to be useful without using a scalarref - there were no initial tests >:( +# +$schema->storage->debug (1); +my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # make sure order is consistent +cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs'); + +my $table = $cds->result_source->name; +my $subsel = $cds->search ({}, { + columns => [qw/cdid title/], + from => \ "(SELECT cdid, title FROM $table LIMIT 2) me", +}); + +is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds'); +is ($subsel->next->title, $cds->next->title, 'First CD title match'); +is ($subsel->next->title, $cds->next->title, 'Second CD title match');