Skip to content

Commit

Permalink
Merge d54fed5 into 661b3d3
Browse files Browse the repository at this point in the history
  • Loading branch information
ribasushi committed Oct 31, 2019
2 parents 661b3d3 + d54fed5 commit ae73e15
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
20 changes: 19 additions & 1 deletion lib/DBIx/Class/Helper/ResultSet/Shortcut.pm
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,28 @@ calling C<< $rs->count >>.
my $results_exist = $schema->resultset('Bar')->search({...})->results_exist;
# there is no easily expressable equivalent, so this is not exactly a
# shortcut. Nevertheless kept in this class for historical reasons
Uses C<EXISTS> SQL function to check if the query would return anything.
Possibly lighter weight than the much more common C<< foo() if $rs->count >>
Usually much less resource intensive the more common C<< foo() if $rs->count >>
idiom.
=method results_exist_as_query
...->search(
{},
{ '+columns' => {
subquery_has_members => $some_correlated_rs->results_exist_as_query
}},
);
# there is no easily expressable equivalent, so this is not exactly a
# shortcut. Nevertheless kept in this class for historical reasons
The query generator behind L</results_exist>. Can be used standalone in
complex queries returning a boolean result within a larger query context.
=method null(@columns || \@columns)
$rs->null('status');
Expand Down
34 changes: 28 additions & 6 deletions lib/DBIx/Class/Helper/ResultSet/Shortcut/ResultsExist.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,36 @@ use warnings;

use parent 'DBIx::Class::ResultSet';

sub results_exist_as_query {
my $self = shift;


my $reified = $self->search_rs( {}, {
columns => { _results_existence_check => \ '42' }
} )->as_query;


$$reified->[0] = "( SELECT EXISTS $$reified->[0] )";


$reified;
}


sub results_exist {
my $self = shift;
my $self = shift;

my( undef, $sth ) = $self->result_source
->schema
->storage
->_select(
$self->results_exist_as_query,
\'*',
{},
{},
);

$self
->result_source
->resultset
->search({ -exists => $self->as_query })
->first
$sth->fetchall_arrayref->[0][0] ? 1 : 0;
}

1;
28 changes: 28 additions & 0 deletions t/ResultSet/Shortcut/ResultsExist.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,32 @@ my $rs2 = $schema->resultset( 'Foo' )->search({ id => { '<' => 0 } });
ok( $rs->results_exist, 'check rs has some results' );
ok(!$rs2->results_exist, 'and check that rs has no results' );

is_deeply(
[
$rs->search({}, { order_by => 'id', columns => {

id => "id",

has_lesser => $rs->search(
{ 'correlation.id' => { '<' => { -ident => "me.id" } } },
{ alias => 'correlation' }
)->results_exist_as_query,

has_greater => $rs->search(
{ 'correlation.id' => { '>' => { -ident => "me.id" } } },
{ alias => 'correlation' }
)->results_exist_as_query,

}})->hri->all
],
[
{ id => 1, has_lesser => 0, has_greater => 1 },
{ id => 2, has_lesser => 1, has_greater => 1 },
{ id => 3, has_lesser => 1, has_greater => 1 },
{ id => 4, has_lesser => 1, has_greater => 1 },
{ id => 5, has_lesser => 1, has_greater => 0 },
],
"Correlated-existence works",
);

done_testing;

0 comments on commit ae73e15

Please sign in to comment.