NAME

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

SYNOPSIS

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.

PROPERTIES

dbic_rs

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

bm

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

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

build_dbic_rs

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 => ... });
}

new_result

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

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

create

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

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

find

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

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

first

Equivalent to DBIC Resultset 'first' method.

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

single

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

update_or_create

Wraps around the original DBIC update_or_create method.

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

find_or_create

Wraps around the original DBIC find_or_create method.

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

find_or_new

Wraps around the original DBIC find_or_new method.

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

pager

Shortcut to underlying dbic_rs pager.

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

delete

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

get_column

Shortcut to the get_column of the decorated dbic_rs

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

search_rs

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.

wrap

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 , ... });
}

all

Similar to DBIC Resultset all.

Usage:

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

loop_through

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

WARNINGS:

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.

Usage:

$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

fast_loop_through

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.

Usage:

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

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

Options:

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

limit: Return after looping through this amount of rows.

Important

    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.

Prerequisites:

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 http://use-the-index-luke.com/sql/partial-results/fetch-next-page

next

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

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

count

Returns the number of objects in this ResultSet.

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