From: Matt S Trout Date: Thu, 1 Dec 2005 07:55:29 +0000 (+0000) Subject: Moved search_literal, count, count_literal onto resultset X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6009260a5d243a47c66be545dd624235ba10330f;p=dbsrgits%2FDBIx-Class-Historic.git Moved search_literal, count, count_literal onto resultset --- diff --git a/lib/DBIx/Class/CDBICompat/Retrieve.pm b/lib/DBIx/Class/CDBICompat/Retrieve.pm index 6937fb3..31dd128 100644 --- a/lib/DBIx/Class/CDBICompat/Retrieve.pm +++ b/lib/DBIx/Class/CDBICompat/Retrieve.pm @@ -5,7 +5,12 @@ use warnings FATAL => 'all'; sub retrieve { shift->find(@_) } sub retrieve_all { shift->search } -sub retrieve_from_sql { shift->search_literal(@_) } + +sub retrieve_from_sql { + my ($class, $cond, @rest) = @_; + $cond =~ s/^\s*WHERE//i; + $class->search_literal($cond, @rest); +} sub count_all { shift->count } # Contributed by Numa. No test for this though. diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 4e69104..ec07702 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -71,19 +71,27 @@ sub new { =item search -Runs a search against the current resultset. + my @obj = $rs->search({ foo => 3 }); # "... WHERE foo = 3" + my $new_rs = $rs->search({ foo => 3 }); + +If you need to pass in additional attributes but no additional condition, +call it as ->search(undef, \%attrs); + + my @all = $class->search({}, { cols => [qw/foo bar/] }); # "SELECT foo, bar FROM $class_table" =cut sub search { my $self = shift; + #use Data::Dumper;warn Dumper(@_); + my $attrs = { %{$self->{attrs}} }; if (@_ > 1 && ref $_[$#_] eq 'HASH') { $attrs = { %{ pop(@_) } }; } - my $where = (@_ == 1 || ref $_[0] eq "HASH" ? shift: {@_}); + my $where = ((@_ == 1 || ref $_[0] eq "HASH") ? shift : {@_}); if (defined $where) { $where = (defined $attrs->{where} ? { '-and' => [ $where, $attrs->{where} ] } @@ -96,6 +104,20 @@ sub search { return (wantarray ? $rs->all : $rs); } +=item search_literal + my @obj = $rs->search_literal($literal_where_cond, @bind); + my $new_rs = $rs->search_literal($literal_where_cond, @bind); + +Pass a literal chunk of SQL to be added to the conditional part of the +resultset + +=cut +sub search_literal { + my ($self, $cond, @vals) = @_; + my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {}); + $attrs->{bind} = [ @{$self->{attrs}{bind}||[]}, @vals ]; + return $self->search(\$cond, $attrs); +} =item cursor @@ -186,13 +208,14 @@ sub _construct_object { =item count Performs an SQL count with the same query as the resultset was built -with to find the number of elements. +with to find the number of elements. If passed arguments, does a search +on the resultset and counts the results of that. =cut - sub count { - my ($self) = @_; + my $self = shift; + return $self->search(@_)->count if @_ && defined $_[0]; my $attrs = { %{ $self->{attrs} } }; unless ($self->{count}) { # offset and order by are not needed to count @@ -209,6 +232,14 @@ sub count { : $self->{count}; } +=item count_literal + +Calls search_literal with the passed arguments, then count + +=cut + +sub count_literal { shift->search_literal(@_)->count; } + =item all Returns all elements in the resultset. Is called implictly if the search diff --git a/lib/DBIx/Class/ResultSetInstance.pm b/lib/DBIx/Class/ResultSetInstance.pm index 9eefaa3..4b83c42 100644 --- a/lib/DBIx/Class/ResultSetInstance.pm +++ b/lib/DBIx/Class/ResultSetInstance.pm @@ -2,6 +2,9 @@ package DBIx::Class::ResultSetInstance; use base qw/DBIx::Class/; -sub search { shift->resultset->search(@_); } +sub search { shift->resultset->search(@_); } +sub search_literal { shift->resultset->search_literal(@_); } +sub count { shift->resultset->count(@_); } +sub count_literal { shift->resultset->count_literal(@_); } 1; diff --git a/lib/DBIx/Class/Table.pm b/lib/DBIx/Class/Table.pm index 16fc291..349d163 100644 --- a/lib/DBIx/Class/Table.pm +++ b/lib/DBIx/Class/Table.pm @@ -62,60 +62,6 @@ sub add_columns { $class->_mk_column_accessors(@cols); } -=item search_literal - - my @obj = $class->search_literal($literal_where_cond, @bind); - my $cursor = $class->search_literal($literal_where_cond, @bind); - -=cut - -sub search_literal { - my ($class, $cond, @vals) = @_; - $cond =~ s/^\s*WHERE//i; - my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {}); - $attrs->{bind} = \@vals; - return $class->search(\$cond, $attrs); -} - -=item count_literal - - my $count = $class->count_literal($literal_where_cond); - -=cut - -sub count_literal { - my $class = shift; - return $class->search_literal(@_)->count; -} - -=item count - - my $count = $class->count({ foo => 3 }); - -=cut - -sub count { - my $class = shift; - return $class->search(@_)->count; -} - -=item search - - my @obj = $class->search({ foo => 3 }); # "... WHERE foo = 3" - my $cursor = $class->search({ foo => 3 }); - -To retrieve all rows, simply call C with no condition parameter, - - my @all = $class->search(); # equivalent to search({}) - -If you need to pass in additional attributes (see -L for details) an empty hash indicates -no condition, - - my @all = $class->search({}, { cols => [qw/foo bar/] }); # "SELECT foo, bar FROM $class_table" - -=cut - sub resultset { my $class = shift;