Data-ObjectDriver

 view release on metacpan or  search on metacpan

lib/Data/ObjectDriver/Driver/Partition.pm  view on Meta::CPAN

# $Id$

package Data::ObjectDriver::Driver::Partition;
use strict;
use warnings;
use Carp();

use base qw( Data::ObjectDriver Class::Accessor::Fast );

__PACKAGE__->mk_accessors(qw( get_driver ));

sub init {
    my $driver = shift;
    $driver->SUPER::init(@_);
    my %param = @_;
    $driver->get_driver($param{get_driver});
    $driver->{__working_drivers} = [];
    $driver;
}

sub lookup {
    my $driver = shift;
    my($class, $id) = @_;
    return unless $id;
    $driver->get_driver->($id)->lookup($class, $id);
}

sub lookup_multi {
    my $driver = shift;
    my($class, $ids) = @_;
    return [] unless @$ids;
    $driver->get_driver->($ids->[0])->lookup_multi($class, $ids);
}

sub exists     { shift->_exec_partitioned('exists',     @_) }
sub insert     { shift->_exec_partitioned('insert',     @_) }
sub replace    { shift->_exec_partitioned('replace',    @_) }
sub update     { shift->_exec_partitioned('update',     @_) }
sub remove     { shift->_exec_partitioned('remove',     @_) }
sub fetch_data { shift->_exec_partitioned('fetch_data', @_) }

sub search {
    my $driver = shift;
    my($class, $terms, $args) = @_;
    $driver->get_driver->($terms, $args)->search($class, $terms, $args);
}

sub _exec_partitioned {
    my $driver = shift;
    my($meth, $obj, @rest) = @_;
    ## If called as a class method, pass in the stuff in @rest.
    my $d;
    if (ref($obj)) {
        my $terms = $obj->column_values;
        # @rest should only contain $args, but just in case
        # don't assume we have nothing else and build @rest2
        (undef, my @rest2) = @rest;
        $d = $driver->get_driver->($terms, @rest2);
    } else {
        $d = $driver->get_driver->(@rest);
    }

    if ( $driver->txn_active ) {
        $driver->add_working_driver($d);
    }
    $d->$meth($obj, @rest);
}

sub add_working_driver {
    my $driver = shift;
    my $part_driver = shift;
    if (! $part_driver->txn_active) {
        $part_driver->begin_work;
        push @{$driver->{__working_drivers}}, $part_driver;
    }
}

sub commit {
    my $driver = shift;

    ## if the driver has its own internal txn_active flag
    ## off, we don't bother ending. Maybe we already did
    return unless $driver->txn_active;

    $driver->SUPER::commit(@_);
    _end_txn($driver, 'commit', @_);
}

sub rollback {
    my $driver = shift;

    ## if the driver has its own internal txn_active flag
    ## off, we don't bother ending. Maybe we already did
    return unless $driver->txn_active;

    $driver->SUPER::rollback(@_);
    _end_txn($driver, 'rollback', @_);
}



( run in 0.723 second using v1.01-cache-2.11-cpan-39bf76dae61 )