summaryrefslogtreecommitdiffstats
path: root/lib/Jaos/DBI.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Jaos/DBI.pm')
-rw-r--r--lib/Jaos/DBI.pm118
1 files changed, 95 insertions, 23 deletions
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;
}