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.
=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} ] }
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
=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
: $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
$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<search()> with no condition parameter,
-
- my @all = $class->search(); # equivalent to search({})
-
-If you need to pass in additional attributes (see
-L<DBIx::Class::ResultSet/Attributes> 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;