summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Woodward2007-05-16 01:17:16 +0000
committerJason Woodward2007-05-16 01:17:16 +0000
commitdd49bfd37cfbbf70e5bdd8360e8f1731dfd83e9c (patch)
treefb4b39b53fb1dfe80f239bf8e853165779385fdb
parentf6b5f157a75d6f76d4dfd37da0948dace2adf468 (diff)
downloadJaos-DBI-dd49bfd37cfbbf70e5bdd8360e8f1731dfd83e9c.tar.gz
updated search api(), combining all the various search variants into one method.0.3.4
-rw-r--r--Changes3
-rw-r--r--lib/Jaos/DBI.pm228
-rw-r--r--t/01use.t3
-rw-r--r--t/04full.t14
-rw-r--r--t/basic.t3
-rw-r--r--t/search.t20
6 files changed, 110 insertions, 161 deletions
diff --git a/Changes b/Changes
index 2867b57..b9d8835 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
Revision history for Perl extension Jaos::DBI
+0.3.4
+ - updated search api(), combining all the various search variants into one method.
+
0.3.3
- added support for passing in an array reference for the search values
- rerieve now caches
diff --git a/lib/Jaos/DBI.pm b/lib/Jaos/DBI.pm
index b31aadb..9eb229d 100644
--- a/lib/Jaos/DBI.pm
+++ b/lib/Jaos/DBI.pm
@@ -6,7 +6,7 @@ use vars qw($cache);
use base qw/Class::Data::Inheritable Class::Accessor/;
use DBI ();
-our $VERSION = 0.3.3;
+our $VERSION = 0.3.4;
our $if_active = ($DBI::VERSION >= '1.40') ? 3 : 0;
$cache = {};
@@ -56,19 +56,19 @@ Jaos::DBI - Jaos DBI object similar to Class::DBI
# using various search options
my @objs = ();
@objs = Users->search(email => 'foo@bar.com',{ order_by => 'id', limit => '2'});
- @objs = Users->search_like(email => '%@bar.com',{ order_by => 'id', limit => '2'});
- @objs = Users->search_ilike(email => '%@bar.com',{ order_by => 'id', offset => '2'});
- @objs = Users->search_or(email => 'john.doe@bar.com', name => 'john');
- @objs = Users->search_or_like(email => 'john.doe@%', name => '%john%');
- @objs = Users->search_or_ilike(email => 'john.doe@%', name => '%john%');
- @objs = Users->search_where('name in (select name from other_user_table)');
+ @objs = Users->search(email => '%@bar.com',{ order_by => 'id', limit => '2', operator => 'like'});
+ @objs = Users->search(email => '%@bar.com',{ order_by => 'id', offset => '2', operator => 'ilike'});
+ @objs = Users->search(email => 'john.doe@bar.com', name => 'john', { boolean => 'or' });
+ @objs = Users->search(email => 'john.doe@%', name => '%john%', { boolean => 'or', operator => 'like' });
+ @objs = Users->search(email => 'john.doe@%', name => '%john%', { boolean => 'or', operator => 'ilike' });
+ @objs = Users->search({ where => 'name in (select name from other_user_table)' });
my $possibilities = [ 'foo', 'bar' ];
@objs = Users->search( name => $possibilities);
- @objs = Users->search_like( name => $possibilities);
- @objs = Users->search_ilike( name => $possibilities);
- @objs = Users->search_or( name => $possibilities);
- @objs = Users->search_or_like( name => $possibilities);
- @objs = Users->search_or_ilike( name => $possibilities);
+ @objs = Users->search( name => $possibilities, { operator => 'like' });
+ @objs = Users->search( name => $possibilities, { operator => 'ilike' });
+ @objs = Users->search( name => $possibilities, { boolean=> 'or' });
+ @objs = Users->search( name => $possibilities, { boolean=> 'or', operator => 'like' });
+ @objs = Users->search( name => $possibilities, { boolean=> 'or', operator => 'ilike' });
# all
my @objs = Users->retrieve_all();
# by primary column value (primary key)
@@ -485,159 +485,111 @@ sub find_or_create
return $self->insert(@_);
}
-=head2 search
-
-Searches the table and returns a list of matching objects.
-
- my @objs = __PACKAGE__->search(column1 => $value1, column2 => $value2,{ order_by => 'col3', offset => 10, limit => 10 });
-
- Supports optional hashref passed as the last argument, within which order_by, offset, and limit may be specified.
-
- Value may be a scalar or an array reference.
-
-=cut
-
-sub search
-{
- my $self = shift;
- return $self->_do_search(
- join => 'and',
- type => '=',
- where => undef,
- opts => \@_
- );
-}
-
-=head2 search_like
-
-Searches the table and returns a list of matching objects using column like $value rather than column = $value.
-
- my @objs = __PACKAGE__->search_like(column1 => "%$value1%", column2 => "%$value2%");
-
- Value may be a scalar or an array reference.
-
-=cut
-
sub search_like
{
+ carp 'search_like is deprecated';
my $self = shift;
- return $self->_do_search(
- join => 'and',
- type => 'like',
- where => undef,
- opts => \@_
- );
-}
-
-=head2 search_ilike
-
-same as search_like but insensitive
+ my $search_options = (ref $_[-1] eq 'HASH') ? pop @_ : {};
+ my %data = @_ if @_ % 2 == 0;
-=cut
+ $search_options->{operator} = 'like';
+ return $self->search(%data, $search_options);
+}
sub search_ilike
{
+ carp 'search_ilike is deprecated';
my $self = shift;
- return $self->_do_search(
- join => 'and',
- type => 'ilike',
- where => undef,
- opts => \@_
- );
-}
+ my $search_options = (ref $_[-1] eq 'HASH') ? pop @_ : {};
+ my %data = @_ if @_ % 2 == 0;
-=head2 search_or
-
-search where given key/values use or instead of and
-
-=cut
+ $search_options->{boolean} = 'and';
+ $search_options->{operator} = 'ilike';
+ return $self->search(%data, $search_options);
+}
sub search_or
{
+ carp 'search_or is deprecated';
my $self = shift;
- return $self->_do_search(
- join => 'or',
- type => '=',
- where => undef,
- opts => \@_
- );
-}
-
-=head2 search_or_like
-
-search where given like key/values use or instead of and
+ my $search_options = (ref $_[-1] eq 'HASH') ? pop @_ : {};
+ my %data = @_ if @_ % 2 == 0;
-=cut
+ $search_options->{boolean} = 'or';
+ $search_options->{operator} = '=';
+ return $self->search(%data, $search_options);
+}
sub search_or_like
{
+ carp 'search_or_like is deprecated';
my $self = shift;
- return $self->_do_search(
- join => 'or',
- type => 'like',
- where => undef,
- opts => \@_
- );
-}
+ my $search_options = (ref $_[-1] eq 'HASH') ? pop @_ : {};
+ my %data = @_ if @_ % 2 == 0;
-=head2 search_or_ilike
+ $search_options->{boolean} = 'or';
+ $search_options->{operator} = 'like';
+ return $self->search(%data, $search_options);
+}
-search insensitive where given ilike key/values use or instead of and
+sub search_or_ilike
+{
+ carp 'search_or_ilike is deprecated';
+ my $self = shift;
+ my $search_options = (ref $_[-1] eq 'HASH') ? pop @_ : {};
+ my %data = @_ if @_ % 2 == 0;
-=cut
+ $search_options->{boolean} = 'or';
+ $search_options->{operator} = 'ilike';
+ return $self->search(%data, $search_options);
+}
-sub search_or_ilike
+sub search_where
{
+ carp 'search_where is deprecated';
my $self = shift;
- return $self->_do_search(
- join => 'or',
- type => 'ilike',
- where => undef,
- opts => \@_
- );
+ my $search_options = (ref $_[-1] eq 'HASH') ? pop @_ : {};
+ my $where = shift;
+
+ $search_options->{where} = $where;
+ return $self->search($search_options);
}
-=head2 search_where
+=head2 search
+
+Searches the table and returns a list of matching objects. Returned value may be a scalar or an array reference depending on the context. Supports optional hashref passed as the last argument, within which order_by, offset, limit, operator, boolean, and where may be specified.
+
+ my @objs = __PACKAGE__->search(column1 => $value1, column2 => $value2, { order_by => 'col3', offset => 10, limit => 10 });
+
+The operator and boolean search options can be used to specify exactly how the results should be retrieved. The default value for operator is '='. The default value for boolean is 'and'.
+
+ my @objs = __PACKAGE__->search(column1 => "%$value1%", column2 => "%$value2%", { operator => 'like'});
+ my @objs = __PACKAGE__->search(column1 => "%$value1%", column2 => "%$value2%", { operator => 'ilike'});
+ my @objs = __PACKAGE__->search(column1 => $value1, column2 => $value2, { boolean => 'or'});
+ my @objs = __PACKAGE__->search(column1 => "%$value1%", column2 => "%$value2%", { boolean => 'or', operator => 'like'});
+ my @objs = __PACKAGE__->search(column1 => "%$value1%", column2 => "%$value2%", { boolean => 'or', operator => 'ilike'});
Searches the table and returns a list of matching objects using the specified where clause
- my @objs = __PACKAGE__->search_where('id = foo');
+ my @objs = __PACKAGE__->search({ where => 'id = foo'});
=cut
-sub search_where
+sub search
{
- my $self = shift;
- my $where = shift;
+ my $self = shift;
+ my $class = ref($self) || $self;
+ my $table = $self->table;
+ my $sql = $self->base_select || "select * from $table";
+ my @values = ();
- return $self->_do_search(
- join => undef,
- type => undef,
- where => $where,
- opts => \@_,
- );
-}
+ my $search_options = (ref $_[-1] eq 'HASH') ? pop @_ : {};
+ my %data = @_ if @_ % 2 == 0;
+ my @columns = (keys %data) if %data;
-sub _do_search
-{
- my ($self, %params) = @_;
-
- my $join = $params{join};
- my $type = $params{type};
- my $where = $params{where};
- my @opts = @{$params{opts}};
-
- my $class = ref($self) || $self;
- my $table = $self->table;
- my $sql = $self->base_select || "select * from $table";
- my @values = ();
-
- my ($search_opts,%data,@columns);
- if (@opts) {
- $search_opts = @opts % 2 ? pop @opts : {};
- %data = @opts;
- @columns = (keys %data) if %data;
- }
+ my $boolean = $search_options->{boolean} || 'and';
+ my $operator = $search_options->{operator} || '=';
+ my $where = $search_options->{where};
if ($where) {
$sql .= ' where ' . $where;
@@ -646,17 +598,17 @@ sub _do_search
my @where_clause = ();
for my $col (@columns) {
if ($data{$col} && ref($data{$col}) eq 'ARRAY') {
- push @where_clause, join(' or ', ("$col $type ?") x @{$data{$col}});
+ push @where_clause, join(' or ', ("$col $operator ?") x @{$data{$col}});
} elsif ($data{$col}) {
- push @where_clause, "$col $type ? ";
+ push @where_clause, "$col $operator ? ";
}
}
- $sql .= join(" $join ", @where_clause);
+ $sql .= join(" $boolean ", @where_clause);
}
- if ($search_opts) {
- $sql .= " order by $search_opts->{order_by}" if $search_opts->{order_by};
- $sql .= " limit $search_opts->{limit}" if $search_opts->{limit};
- $sql .= " offset $search_opts->{offset}" if $search_opts->{offset};
+ if ($search_options) {
+ $sql .= " order by $search_options->{order_by}" if $search_options->{order_by};
+ $sql .= " limit $search_options->{limit}" if $search_options->{limit};
+ $sql .= " offset $search_options->{offset}" if $search_options->{offset};
}
if (@columns) {
@@ -907,7 +859,7 @@ sub retrieve
return undef;
}
- if (my ($r) = $self->search_where( $self->primary_column . " = " . $key )) {
+ if (my ($r) = $self->search({ where => $self->primary_column . " = " . $key })) {
return $self->_add_obj_to_cache($r);
}
diff --git a/t/01use.t b/t/01use.t
index 7f54961..458fce2 100644
--- a/t/01use.t
+++ b/t/01use.t
@@ -7,8 +7,7 @@ my @base_class_methods = qw/
table columns primary_column virtual_columns manual_update
sequence sequence_nextval sequence_currval
insert create find_or_create delete set get update
- search search_like search_ilike search_or search_or_like search_or_ilike
- search_where retrieve_all retrieve
+ search retrieve_all retrieve
prepare begin_work commit rollback
cache cache_servers compress_threshold cache_expires
diff --git a/t/04full.t b/t/04full.t
index 7e53d4e..5a31cf7 100644
--- a/t/04full.t
+++ b/t/04full.t
@@ -14,7 +14,7 @@ BEGIN
or plan skip_all =>
"DBD::CSV is needed for this test";
- plan tests => 28;
+ plan tests => 25;
unlink('foo_table') if ( -e 'foo_table' );
my $dbh = DBI->connect('DBI:CSV:f_dir=./') or die $DBI::errstr;
@@ -30,8 +30,7 @@ my @base_class_methods = qw/
table columns primary_column virtual_columns manual_update
sequence sequence_nextval sequence_currval
insert create find_or_create delete set get update
- search search_like search_ilike search_or search_or_like search_or_ilike
- search_where retrieve_all retrieve
+ search retrieve_all retrieve
prepare begin_work commit rollback
cache cache_servers compress_threshold cache_expires
@@ -50,12 +49,9 @@ isa_ok(Foo->find_or_create( id => 2, name => 'jason', password => 'pass', email
ok(Foo->search(name => 'jason') > 0,'search returns array');
ok(Foo->search(name => 'jason',{ order_by => 'id'}) > 0,'search with order_by returns array');
-ok(Foo->search_where("name = 'jason'") > 0,'search returns array');
-ok(Foo->search_like(name => 'j%') > 0,'search_like returns array');
-ok(Foo->search_ilike() > 0,'search_ilike returns array');
-ok(Foo->search_or_like() > 0,'search_or_like returns array');
-ok(Foo->search_or_ilike() > 0,'search_or_ilike returns array');
-ok(Foo->retrieve_all() > 0,'search_or_ilike returns array');
+ok(Foo->search({ where => "name = 'jason'" }) > 0,'search returns array');
+ok(Foo->search(name => 'j%', { operator => 'like' }) > 0,'search like returns array');
+ok(Foo->retrieve_all() > 0,'retrieve_all returns array');
ok($obj->id == 1,'testing generated method');
ok($obj->name eq 'jason','testing generated method');
diff --git a/t/basic.t b/t/basic.t
index 7324bf0..1f1e109 100644
--- a/t/basic.t
+++ b/t/basic.t
@@ -30,8 +30,7 @@ my @base_class_methods = qw/
table columns primary_column virtual_columns manual_update
sequence sequence_nextval sequence_currval
insert create find_or_create delete set get update
- search search_like search_ilike search_or search_or_like search_or_ilike
- search_where retrieve_all retrieve
+ search retrieve_all retrieve
prepare begin_work commit rollback
/;
diff --git a/t/search.t b/t/search.t
index 0e8ee9c..9c73246 100644
--- a/t/search.t
+++ b/t/search.t
@@ -31,17 +31,17 @@ isa_ok($obj,'Foo');
ok(Foo->search(name => 'jason') > 0,'search returns array');
ok(Foo->search(name => 'jason',{ order_by => 'id'}) > 0,'search with order_by returns array');
-ok(Foo->search_where("name = 'jason'") > 0,'search returns array');
-ok(Foo->search_like(name => 'j%') > 0,'search_like returns array');
-ok(Foo->search_ilike() > 0,'search_ilike returns array');
-ok(Foo->search_or_like() > 0,'search_or_like returns array');
-ok(Foo->search_or_ilike() > 0,'search_or_ilike returns array');
+ok(Foo->search({ where => "name = 'jason'" }) > 0,'search where returns array');
+ok(Foo->search(name => 'j%', { operator => 'like' }) > 0,'search like returns array');
+ok(Foo->search({ operator => 'ilike' }) > 0,'search ilike returns array');
+ok(Foo->search({ boolean => 'or', operator => 'like' }) > 0,'search or like returns array');
+ok(Foo->search({ boolean => 'or', operator => 'like' }) > 0,'search or ilike returns array');
ok(Foo->search(name => ['jason','foo','bar']) == 3, 'search() using arrayref');
-ok(Foo->search_like(name => ['%jason','%foo','%bar'], password => 'pass') == 3, 'search_like() using arrayref');
-#PG specific #ok(Foo->search_ilike(name => ['%jason','%foo','%bar'], password => 'pass') == 3, 'search_ilike() using arrayref');
-#PG specific #ok(Foo->search_or_ilike(name => ['%jason','%foo','%bar'], password => 'pass') == 3, 'search_or_ilike() using arrayref');
-ok(Foo->search_or(name => ['jason','foo','bar'], password => 'pass') == 3, 'search_or() using arrayref');
-ok(Foo->search_or_like(name => ['%jason','%foo','%bar'], password => 'pass') == 3, 'search_or_like() using arrayref');
+ok(Foo->search(name => ['%jason','%foo','%bar'], password => 'pass', { operator => 'like' }) == 3, 'search like using arrayref');
+#PG specific #ok(Foo->search(name => ['%jason','%foo','%bar'], password => 'pass', { operator => 'ilike' }) == 3, 'search ilike using arrayref');
+#PG specific #ok(Foo->search(name => ['%jason','%foo','%bar'], password => 'pass', { boolean => 'or', operator => 'ilike' }) == 3, 'search or ilike using arrayref');
+ok(Foo->search(name => ['jason','foo','bar'], password => 'pass', { boolean => 'or' }) == 3, 'search or using arrayref');
+ok(Foo->search(name => ['%jason','%foo','%bar'], password => 'pass', { boolean => 'or', operator => 'like' }) == 3, 'search or like using arrayref');
END
{