The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.


DBIx::Class::Wrapper::Factory - A factory class that decorates a DBIx::Class::ResultSet.


A model implementing the role DBIx::Class::Wrapper will automatically instantiate subclasses of this for any underlying DBIx::Class ResultSet.

To implement your own factory containing your business code for the underlying DBIC resulsets, you need to subclass this.

See DBIx::Class::Wrapper for a simple synopsis overview.



The original DBIx::Class::ResultSet. Mandatory.


The business model consuming the role DBIx::Class::Wrapper. Mandatory.

See DBIx::Class::Wrapper for more details.


Builds the dbic ResultSet to be wrapped by this factory.

Defaults to the DBIx::Class Resultset with the same name as this factory.

You can override this in your business specific factories to build specific resultsets:

 package My::Model::Factory::SomeName;

 use Moose; extends  qw/DBIx::Class::Wrapper::Factory/ ;

 sub build_dbic_rs{
    my ($self) = @_;
    return $self->bm->dbic_schema->resultset('SomeOtherName');

    # Or with some restriction:

    return $self->bm->dbic_schema->resultset('SomeOtherName')
           ->search({ bla => ... });


Instantiate a new NOT INSERTED IN DB row and wrap it using the wrap method.

See "new_result" in DBIx::Class::ResultSet


Creates a new object in the DBIC Schema and return it wrapped using the wrapper method.

See "create" in DBIx::Class::ResultSet


Finds an object in the DBIC schema and returns it wrapped using the wrapper method.

See "find" in DBIx::Class::ResultSet


Equivalent to DBIC Resultset 'first' method.

See <DBIx::Class::ResultSet/first>


Equivalent to DBIx::Class::ResultSet::single. It's a bit more efficient than first().


Wraps around the original DBIC update_or_create method.

See "update_or_create" in DBIx::Class::ResultSet


Wraps around the original DBIC find_or_create method.

See "find_or_create" in DBIx::Class::ResultSet


Wraps around the original DBIC find_or_new method.

See "find_or_new" in DBIx::Class::ResultSet


Shortcut to underlying dbic_rs pager.

See "pager" in DBIx::Class::ResultSet.


Shortcut to "delete" in DBIx::Class::ResultSet


Shortcut to the get_column of the decorated dbic_rs

See "get_column" in DBIx::Class::ResultSet


Alias for search

Search objects in the DBIC Schema and returns a new instance of this factory.

Note that unlike DBIx::Class::ResultSet, this search method will not return an Array of all results in an array context.


Wraps an DBIx::Class::Row in a business object. By default, it returns the Row itself.

Override that in your subclasses of factories if you need to wrap some business code around the DBIx::Class::Row:

  sub wrap{
     my ($self, $o) = @_;

     return My::Model::O::SomeObject->new({ o => $o , ... });


Similar to DBIC Resultset all.


 my @objs = $this->all();


Loop through all the elements of this factory whilst paging and execute the given code with the current retrieved object.


Make sure your resultset is ordered as it wouldn't make much sense to page through an unordered resultset.

In case other things are concurrently adding to this resultset, it is possible that the code you give will be called with the same objects twice.

If it's not the problem and if the rate at which objects are added is not too fast compared to the processing you are doing in the code, it should be just fine.

In other cases, you probably want to wrap this in a transaction to have a frozen view of the resultset.


 $this->loop_through(sub{ my $o = shift ; do something with o });
 $this->loop_through(sub{...} , { limit => 1000 }); # Do only 1000 calls to sub.
 $this->loop_through(sub{...} , { rows => 20 }); # Go by pages of 20 rows


Loops through all the objects of this factory in a Seeking fashion. If the primary key of the underlying resultset is orderable and indexed, this should run in linear time of the number of rows on the resultset.


  $this->fast_loop_through(sub{my ($o) = @_; ... } );

  $this->fast_loop_through(sub{ .. } , { rows => 100 , limit => 1000 });


 rows: Fetch this amount of rows at each query. Default to 100

 limit: Return after looping through this amount of rows.


    You do not need to order the set, as this will order it by ascending primary key.

    This means aggregation functions (such as group_by) will not work.

    Incidentally, it means that if other processes are writing to this resultset, this method will play catch up on the resultset, so if the writing rate is higher than the reading rate, this might take a while to return.

    If you want to avoid this, set the option 'order' to 'desc'.

Returns the number of rows looped through.


Must have:

- The underlying DBIx::Class::ResultSource has a primary key

- Each component of the primary key supports the operators '>' and '<'

- It is possible to order all the rows by this primary key alone.

Should have:

- This primary key is indexed and offers fast comparison access.

Inspired by


Returns next Business Object from this current DBIx::Resultset.

See "next" in DBIx::Class::ResultSet


Returns the number of objects in this ResultSet.

See "count" in DBIx::Class::ResultSet