Skip to content

Commit

Permalink
Add search shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
moltar authored and Arthur Axel 'fREW' Schmidt committed May 24, 2015
1 parent 353448c commit 67c61a3
Show file tree
Hide file tree
Showing 15 changed files with 303 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Revision history for {{$dist->name}}

{{$NEXT}}
- Add ::ResultSet::Shortcut::Search (Closes GH#44 and GH#47) (Thanks moltar!)

2.027001 2015-05-16 11:47:15-05:00 America/Chicago
- Fix missing POD in ::ResultSet::Explain
Expand Down
23 changes: 23 additions & 0 deletions lib/DBIx/Class/Helper/ResultSet/Shortcut.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use parent (qw(
DBIx::Class::Helper::ResultSet::Shortcut::ResultsExist
DBIx::Class::Helper::ResultSet::Shortcut::Rows
DBIx::Class::Helper::ResultSet::Shortcut::Page
DBIx::Class::Helper::ResultSet::Shortcut::Search
));

1;
Expand Down Expand Up @@ -162,6 +163,28 @@ 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 >>
idiom.
=method null(@columns || \@columns)
$rs->null('status');
$rs->null(['status', 'title']);
=method not_null(@columns || \@columns)
$rs->not_null('status');
$rs->not_null(['status', 'title']);
=method like($column || \@columns, $cond)
$rs->like('lyrics', '%zebra%');
$rs->like(['lyrics', 'title'], '%zebra%');
=method not_like($column || \@columns, $cond)
$rs->not_like('lyrics', '%zebra%');
$rs->not_like(['lyrics', 'title'], '%zebra%');
=cut

=head1 SEE ALSO
This component is actually a number of other components put together. It will
Expand Down
13 changes: 13 additions & 0 deletions lib/DBIx/Class/Helper/ResultSet/Shortcut/Search.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package DBIx::Class::Helper::ResultSet::Shortcut::Search;

use strict;
use warnings;

use parent (qw(
DBIx::Class::Helper::ResultSet::Shortcut::Search::Null
DBIx::Class::Helper::ResultSet::Shortcut::Search::NotNull
DBIx::Class::Helper::ResultSet::Shortcut::Search::Like
DBIx::Class::Helper::ResultSet::Shortcut::Search::NotLike
));

1;
51 changes: 51 additions & 0 deletions lib/DBIx/Class/Helper/ResultSet/Shortcut/Search/Base.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package DBIx::Class::Helper::ResultSet::Shortcut::Search::Base;

use strict;
use warnings;

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

#--------------------------------------------------------------------------#
# _helper_unwrap_columns(@columns)
#--------------------------------------------------------------------------#

sub _helper_unwrap_columns {
my ($self, @columns) = @_;

if (@columns == 1 && ref($columns[0]) && ref($columns[0]) eq 'ARRAY') {
@columns = @{ $columns[0] };
}

return @columns;
}

#--------------------------------------------------------------------------#
# _helper_meify($column)
#--------------------------------------------------------------------------#

sub _helper_meify {
my ($self, $column) = @_;

return $self->current_source_alias . $column if $column =~ m/^\./;
return $column;
}

#--------------------------------------------------------------------------#
# _helper_apply_search($cond, @columns)
#--------------------------------------------------------------------------#

sub _helper_apply_search {
my ($self, $cond, @columns) = @_;

@columns = $self->_helper_unwrap_columns(@columns);

my $rs = $self;
foreach my $column (@columns) {
$rs = $rs->search_rs({ $self->_helper_meify($column) => $cond });
}

return $rs;
}


1;
21 changes: 21 additions & 0 deletions lib/DBIx/Class/Helper/ResultSet/Shortcut/Search/Like.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package DBIx::Class::Helper::ResultSet::Shortcut::Search::Like;

use strict;
use warnings;

use parent 'DBIx::Class::Helper::ResultSet::Shortcut::Search::Base';

=head2 like($column || \@columns, $cond)
$rs->like('lyrics', '%zebra%');
$rs->like(['lyrics', 'title'], '%zebra%');
=cut

sub like {
my ($self, $columns, $cond) = @_;

return $self->_helper_apply_search({ '-like' => $cond }, $columns);
}

1;
21 changes: 21 additions & 0 deletions lib/DBIx/Class/Helper/ResultSet/Shortcut/Search/NotLike.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package DBIx::Class::Helper::ResultSet::Shortcut::Search::NotLike;

use strict;
use warnings;

use parent 'DBIx::Class::Helper::ResultSet::Shortcut::Search::Base';

=head2 not_like($column || \@columns, $cond)
$rs->not_like('lyrics', '%zebra%');
$rs->not_like(['lyrics', 'title'], '%zebra%');
=cut

sub not_like {
my ($self, $columns, $cond) = @_;

return $self->_helper_apply_search({ '-not_like' => $cond }, $columns);
}

1;
21 changes: 21 additions & 0 deletions lib/DBIx/Class/Helper/ResultSet/Shortcut/Search/NotNull.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package DBIx::Class::Helper::ResultSet::Shortcut::Search::NotNull;

use strict;
use warnings;

use parent 'DBIx::Class::Helper::ResultSet::Shortcut::Search::Base';

=head2 not_null(@columns || \@columns)
$rs->not_null('status');
$rs->not_null(['status', 'title']);
=cut

sub not_null {
my ($self, @columns) = @_;

return $self->_helper_apply_search({ '!=' => undef }, @columns);
}

1;
21 changes: 21 additions & 0 deletions lib/DBIx/Class/Helper/ResultSet/Shortcut/Search/Null.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package DBIx::Class::Helper::ResultSet::Shortcut::Search::Null;

use strict;
use warnings;

use parent 'DBIx::Class::Helper::ResultSet::Shortcut::Search::Base';

=head2 null(@columns || \@columns)
$rs->null('status');
$rs->null(['status', 'title']);
=cut

sub null {
my ($self, @columns) = @_;

return $self->_helper_apply_search({ '=' => undef }, @columns);
}

1;
22 changes: 22 additions & 0 deletions t/ResultSet/Shortcut/Search/Base.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!perl

use strict;
use warnings;

use lib 't/lib';
use Test::More;
use Test::Deep;

use TestSchema;
my $schema = TestSchema->deploy_or_connect();
$schema->prepopulate;

my $rs = $schema->resultset('Foo');

cmp_deeply ['fizz', 'bizz'], [$rs->_helper_unwrap_columns('fizz', 'bizz')], 'unwrap array';
cmp_deeply ['fizz', 'bizz'], [$rs->_helper_unwrap_columns(['fizz', 'bizz'])], 'unwrap arrayref';

is $rs->_helper_meify('id'), 'id', 'not meifying';
is $rs->_helper_meify('.id'), 'me.id', 'meifying';

done_testing;
21 changes: 21 additions & 0 deletions t/ResultSet/Shortcut/Search/Like.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!perl

use strict;
use warnings;

use lib 't/lib';
use Test::More;
use Test::Deep;

use TestSchema;
my $schema = TestSchema->deploy_or_connect();
$schema->prepopulate;

my $rs = $schema->resultset('Search');

cmp_deeply
[$rs->like('me.name', 'bar%')->all],
[$rs->search({ 'me.name' => { '-like' => 'bar%' } })->all],
'like works the same';

done_testing;
21 changes: 21 additions & 0 deletions t/ResultSet/Shortcut/Search/NotLike.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!perl

use strict;
use warnings;

use lib 't/lib';
use Test::More;
use Test::Deep;

use TestSchema;
my $schema = TestSchema->deploy_or_connect();
$schema->prepopulate;

my $rs = $schema->resultset('Search');

cmp_deeply
[$rs->not_like('me.name', 'bar%')->all],
[$rs->search({ 'me.name' => { '-not_like' => 'bar%' } })->all],
'not_like works the same';

done_testing;
21 changes: 21 additions & 0 deletions t/ResultSet/Shortcut/Search/NotNull.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!perl

use strict;
use warnings;

use lib 't/lib';
use Test::More;
use Test::Deep;

use TestSchema;
my $schema = TestSchema->deploy_or_connect();
$schema->prepopulate;

my $rs = $schema->resultset('Search');

cmp_deeply
[$rs->not_null(['me.id'])->all],
[$rs->search({ 'me.id' => { '!=' => undef } })->all],
'not_null works the same';

done_testing;
26 changes: 26 additions & 0 deletions t/ResultSet/Shortcut/Search/Null.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!perl

use strict;
use warnings;

use lib 't/lib';
use Test::More;
use Test::Deep;

use TestSchema;
my $schema = TestSchema->deploy_or_connect();
$schema->prepopulate;

my $rs = $schema->resultset('Search');

cmp_deeply
[$rs->null('me.id')->all],
[$rs->search({ 'me.id' => undef })->all],
'null works the same';

cmp_deeply
[$rs->null('.id', 'bar_id')->all],
[$rs->search({ 'me.id' => undef, 'bar_id' => undef })->all],
'null works the same for 2 params';

done_testing;
10 changes: 10 additions & 0 deletions t/lib/TestSchema/Result/Search.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package TestSchema::Result::Search;

use DBIx::Class::Candy;

table 'Search';

primary_column id => { data_type => 'int' };
column name => { data_type => 'varchar' };

1;
10 changes: 10 additions & 0 deletions t/lib/TestSchema/ResultSet/Search.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package TestSchema::ResultSet::Search;
use strict;
use warnings;

# intentionally not using TestSchema::ResultSet
use parent 'DBIx::Class::ResultSet';

__PACKAGE__->load_components(qw{ Helper::ResultSet::Shortcut::Search });

1;

0 comments on commit 67c61a3

Please sign in to comment.