The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

DBIx::Class::ResultSet - Responsible for fetching and creating resultset.

SYNOPSIS

my $rs = MyApp::DB::Class->search(registered => 1); my @rows = MyApp::DB::Class->search(foo => 'bar');

DESCRIPTION

The resultset is also known as an iterator. It is responsible for handling queries that may return an arbitrary number of rows, e.g. via search or a has_many relationship.

METHODS

new($source, \%$attrs)

The resultset constructor. Takes a source object (usually a DBIx::Class::Table) and an attribute hash (see below for more information on attributes). Does not perform any queries -- these are executed as needed by the other methods.

  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"

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

cursor

Returns a storage-driven cursor to the given resultset.

search_like

Identical to search except defaults to 'LIKE' instead of '=' in condition

slice($first, $last)

Returns a subset of elements from the resultset.

next

Returns the next element in the resultset (undef is there is none).

count

Performs an SQL COUNT with the same query as the resultset was built with to find the number of elements. If passed arguments, does a search on the resultset and counts the results of that.

count_literal

Calls search_literal with the passed arguments, then count.

all

Returns all elements in the resultset. Called implictly if the resultset is returned in list context.

reset

Resets the resultset's cursor, so you can iterate through the elements again.

first

Resets the resultset and returns the first element.

delete

Deletes all elements in the resultset.

pager

Returns a Data::Page object for the current resultset. Only makes sense for queries with page turned on.

page($page_num)

Returns a new resultset for the specified page.

Attributes

The resultset takes various attributes that modify its behavior. Here's an overview of them:

order_by

Which column(s) to order the results by. This is currently passed through directly to SQL, so you can give e.g. foo DESC for a descending order.

cols (arrayref)

Shortcut to request a particular set of columns to be retrieved - adds 'me.' onto the start of any column without a '.' in it and sets 'select' from that, then auto-populates 'as' from 'select' as normal

select (arrayref)

Indicates which columns should be selected from the storage

as (arrayref)

Indicates column names for object inflation

join

Contains a list of relationships that should be joined for this query. Can also contain a hash reference to refer to that relation's relations. So, if one column in your class belongs_to foo and another belongs_to bar, you can do join => [qw/ foo bar /] to join both (and e.g. use them for order_by). If a foo contains many margles and you want to join those too, you can do join => { foo => 'margle' }. If you want to fetch the columns from the related table as well, see prefetch below.

prefetch

Contains a list of relationships that should be fetched along with the main query (when they are accessed afterwards they will have already been "prefetched"). This is useful for when you know you will need the related object(s), because it saves a query. Currently limited to prefetching one relationship deep, so unlike join, prefetch must be an arrayref.

from

This attribute can contain a arrayref of elements. Each element can be another arrayref, to nest joins, or it can be a hash which represents the two sides of the join.

NOTE: Use this on your own risk. This allows you to shoot your foot off!

page

For a paged resultset, specifies which page to retrieve. Leave unset for an unpaged resultset.

rows

For a paged resultset, how many rows per page