-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
moltar
authored and
Arthur Axel 'fREW' Schmidt
committed
May 24, 2015
1 parent
353448c
commit 67c61a3
Showing
15 changed files
with
303 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
lib/DBIx/Class/Helper/ResultSet/Shortcut/Search/NotLike.pm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
lib/DBIx/Class/Helper/ResultSet/Shortcut/Search/NotNull.pm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |