summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Changes5
-rw-r--r--README26
-rw-r--r--lib/Jaos/DBI.pm118
-rw-r--r--t/cache.t8
-rw-r--r--t/search.t10
5 files changed, 135 insertions, 32 deletions
diff --git a/Changes b/Changes
index 60ea5cf..2867b57 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,10 @@
Revision history for Perl extension Jaos::DBI
+0.3.3
+ - added support for passing in an array reference for the search values
+ - rerieve now caches
+ - test updates
+
0.3.2
- documentation updates
- added has_a support
diff --git a/README b/README
index 71623ed..9a5278f 100644
--- a/README
+++ b/README
@@ -33,13 +33,21 @@ SYNOPSIS
# retrieve objects
# using various search options
- my @objs = Users->search(email => 'foo@bar.com',{ order_by => 'id', limit => '2'});
- my @objs = Users->search_like(email => '%@bar.com',{ order_by => 'id', limit => '2'});
- my @objs = Users->search_ilike(email => '%@bar.com',{ order_by => 'id', limit => '2'});
- my @objs = Users->search_or(email => 'john.doe@bar.com', name => 'john');
- my @objs = Users->search_or_like(email => 'john.doe@%', name => '%john%');
- my @objs = Users->search_or_ilike(email => 'john.doe@%', name => '%john%');
- my @objs = Users->search_where('name in (select name from other_user_table)');
+ 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', limit => '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)');
+ 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);
# all
my @objs = Users->retrieve_all();
# by primary column value (primary key)
@@ -172,12 +180,16 @@ METHODS
my @objs = __PACKAGE__->search(column1 => $value1, column2 => $value2);
+ Value may be a scalar or an array reference.
+
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.
+
search_ilike
same as search_like but insensitive
diff --git a/lib/Jaos/DBI.pm b/lib/Jaos/DBI.pm
index 6fcdad6..b31aadb 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.2;
+our $VERSION = 0.3.3;
our $if_active = ($DBI::VERSION >= '1.40') ? 3 : 0;
$cache = {};
@@ -54,13 +54,21 @@ Jaos::DBI - Jaos DBI object similar to Class::DBI
# retrieve objects
# using various search options
- my @objs = Users->search(email => 'foo@bar.com',{ order_by => 'id', limit => '2'});
- my @objs = Users->search_like(email => '%@bar.com',{ order_by => 'id', limit => '2'});
- my @objs = Users->search_ilike(email => '%@bar.com',{ order_by => 'id', offset => '2'});
- my @objs = Users->search_or(email => 'john.doe@bar.com', name => 'john');
- my @objs = Users->search_or_like(email => 'john.doe@%', name => '%john%');
- my @objs = Users->search_or_ilike(email => 'john.doe@%', name => '%john%');
- my @objs = Users->search_where('name in (select name from other_user_table)');
+ 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)');
+ 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);
# all
my @objs = Users->retrieve_all();
# by primary column value (primary key)
@@ -485,12 +493,19 @@ Searches the table and returns a list of matching objects.
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('and','=',undef,@_);
+ return $self->_do_search(
+ join => 'and',
+ type => '=',
+ where => undef,
+ opts => \@_
+ );
}
=head2 search_like
@@ -499,12 +514,19 @@ Searches the table and returns a list of matching objects using column like $val
my @objs = __PACKAGE__->search_like(column1 => "%$value1%", column2 => "%$value2%");
+ Value may be a scalar or an array reference.
+
=cut
sub search_like
{
my $self = shift;
- return $self->_do_search('and','like',undef,@_);
+ return $self->_do_search(
+ join => 'and',
+ type => 'like',
+ where => undef,
+ opts => \@_
+ );
}
=head2 search_ilike
@@ -516,7 +538,12 @@ same as search_like but insensitive
sub search_ilike
{
my $self = shift;
- return $self->_do_search('and','ilike',undef,@_);
+ return $self->_do_search(
+ join => 'and',
+ type => 'ilike',
+ where => undef,
+ opts => \@_
+ );
}
=head2 search_or
@@ -528,7 +555,12 @@ search where given key/values use or instead of and
sub search_or
{
my $self = shift;
- return $self->_do_search('or','=',undef,@_);
+ return $self->_do_search(
+ join => 'or',
+ type => '=',
+ where => undef,
+ opts => \@_
+ );
}
=head2 search_or_like
@@ -540,7 +572,12 @@ search where given like key/values use or instead of and
sub search_or_like
{
my $self = shift;
- return $self->_do_search('or','like',undef,@_);
+ return $self->_do_search(
+ join => 'or',
+ type => 'like',
+ where => undef,
+ opts => \@_
+ );
}
=head2 search_or_ilike
@@ -552,7 +589,12 @@ search insensitive where given ilike key/values use or instead of and
sub search_or_ilike
{
my $self = shift;
- return $self->_do_search('or','ilike',undef,@_);
+ return $self->_do_search(
+ join => 'or',
+ type => 'ilike',
+ where => undef,
+ opts => \@_
+ );
}
=head2 search_where
@@ -566,16 +608,29 @@ Searches the table and returns a list of matching objects using the specified wh
sub search_where
{
my $self = shift;
- return $self->_do_search(undef,undef,@_);
+ my $where = shift;
+
+ return $self->_do_search(
+ join => undef,
+ type => undef,
+ where => $where,
+ opts => \@_,
+ );
}
sub _do_search
{
- my ($self,$join,$type,$where,@opts) = @_;
- my $class = ref($self) || $self;
- my $table = $self->table;
- my $sql = $self->base_select || "select * from $table";
- my @values = ();
+ 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) {
@@ -588,7 +643,15 @@ sub _do_search
$sql .= ' where ' . $where;
} elsif (@columns) {
$sql .= ' where ';
- $sql .= join(" $join ",map { "$_ $type ?" } @columns);
+ 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}});
+ } elsif ($data{$col}) {
+ push @where_clause, "$col $type ? ";
+ }
+ }
+ $sql .= join(" $join ", @where_clause);
}
if ($search_opts) {
$sql .= " order by $search_opts->{order_by}" if $search_opts->{order_by};
@@ -597,7 +660,15 @@ sub _do_search
}
if (@columns) {
- @values = map {$data{$_} ? $data{$_} : undef} @columns;
+ for my $col (@columns) {
+ if ($data{$col} && ref($data{$col}) eq 'ARRAY') {
+ for my $value (@{$data{$col}}) {
+ push @values, $value;
+ }
+ } elsif ($data{$col}) {
+ push @values, $data{$col};
+ }
+ }
}
my $result = [];
@@ -837,8 +908,9 @@ sub retrieve
}
if (my ($r) = $self->search_where( $self->primary_column . " = " . $key )) {
- return $r;
+ return $self->_add_obj_to_cache($r);
}
+
return undef;
}
diff --git a/t/cache.t b/t/cache.t
index 6c44a15..ded5ad7 100644
--- a/t/cache.t
+++ b/t/cache.t
@@ -44,7 +44,13 @@ ok(Bar->cache_expires(60),'setting cache expiration');
ok(Bar->cache_servers(qw/127.0.0.1:11211/),'setting cache servers');
ok(Bar->compress_threshold(10_000),'setting cache servers');
-ok(4 == Bar->retrieve_all(),'retrieve all objects');
+my @objs = ();
+for my $id (1 .. 4)
+{
+ push @objs, Bar->retrieve($id);
+}
+
+ok(4 == @objs,'retrieve all objects');
ok(4 == scalar keys %{$Jaos::DBI::cache->get_multi( qw/Bar:1 Bar:2 Bar:3 Bar:4/ )},'check cache is initially populated');
# extract our baz objects into the cache
diff --git a/t/search.t b/t/search.t
index 2f6bb84..0e8ee9c 100644
--- a/t/search.t
+++ b/t/search.t
@@ -14,11 +14,13 @@ BEGIN
or plan skip_all =>
"DBD::CSV is needed for this test";
- plan tests => 9;
+ plan tests => 13;
unlink('foo_table') if ( -e 'foo_table' );
my $dbh = DBI->connect('DBI:CSV:f_dir=./') or die $DBI::errstr;
$dbh->do('create table foo_table (id INTEGER, name CHAR(255), password CHAR(255), email CHAR(255))') or $dbh->errstr;
+ $dbh->do("insert into foo_table values (2, 'foo', 'pass', 'foo\@bar.org')") or $dbh->errstr;
+ $dbh->do("insert into foo_table values (3, 'bar', 'pass', 'bar\@foo.org')") or $dbh->errstr;
$dbh->disconnect();
use_ok('Foo');
@@ -34,6 +36,12 @@ 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(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');
END
{