ObjectDriver/Driver/Partition.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 119 CC 8 SCAR 15.5
CC
8
Cov
0%
_exec_partitioned
12.0
lookup
6.0
lookup_multi
6.0
CRAP 4.7
1 # $Id$
2  
3 package Data::ObjectDriver::Driver::Partition;
4 use strict;
5 use warnings;
6 use Carp();
7  
8 use base qw( Data::ObjectDriver Class::Accessor::Fast );
9  
10 __PACKAGE__->mk_accessors(qw( get_driver ));
11  
12 sub init {
13 my $driver = shift;
14 $driver->SUPER::init(@_);
15 my %param = @_;
16 $driver->get_driver($param{get_driver});
17 $driver->{__working_drivers} = [];
18 $driver;
19 }
20  
21 sub lookup {
22 my $driver = shift;
23 my($class, $id) = @_;
24 return unless $id;
25 $driver->get_driver->($id)->lookup($class, $id);
26 }
27  
28 sub lookup_multi {
29 my $driver = shift;
30 my($class, $ids) = @_;
31 return [] unless @$ids;
32 $driver->get_driver->($ids->[0])->lookup_multi($class, $ids);
33 }
34  
35 sub exists { shift->_exec_partitioned('exists', @_) }
36 sub insert { shift->_exec_partitioned('insert', @_) }
37 sub replace { shift->_exec_partitioned('replace', @_) }
38 sub update { shift->_exec_partitioned('update', @_) }
39 sub remove { shift->_exec_partitioned('remove', @_) }
40 sub fetch_data { shift->_exec_partitioned('fetch_data', @_) }
41  
42 sub search {
43 my $driver = shift;
44 my($class, $terms, $args) = @_;
45 $driver->get_driver->($terms, $args)->search($class, $terms, $args);
46 }
47  
48 sub _exec_partitioned {
49 my $driver = shift;
50 my($meth, $obj, @rest) = @_;
51 ## If called as a class method, pass in the stuff in @rest.
52 my $d;
53 if (ref($obj)) {
54 my $terms = $obj->column_values;
55 # @rest should only contain $args, but just in case
56 # don't assume we have nothing else and build @rest2
57 (undef, my @rest2) = @rest;
58 $d = $driver->get_driver->($terms, @rest2);
59 } else {
60 $d = $driver->get_driver->(@rest);
61 }
62  
63 if ( $driver->txn_active ) {
64 $driver->add_working_driver($d);
65 }
66 $d->$meth($obj, @rest);
67 }
68  
69 sub add_working_driver {
70 my $driver = shift;
71 my $part_driver = shift;
72 if (! $part_driver->txn_active) {
73 $part_driver->begin_work;
74 push @{$driver->{__working_drivers}}, $part_driver;
75 }
76 }
77  
78 sub commit {
79 my $driver = shift;
80  
81 ## if the driver has its own internal txn_active flag
82 ## off, we don't bother ending. Maybe we already did
83 return unless $driver->txn_active;
84  
85 $driver->SUPER::commit(@_);
86 _end_txn($driver, 'commit', @_);
87 }
88  
89 sub rollback {
90 my $driver = shift;
91  
92 ## if the driver has its own internal txn_active flag
93 ## off, we don't bother ending. Maybe we already did
94 return unless $driver->txn_active;
95  
96 $driver->SUPER::rollback(@_);
97 _end_txn($driver, 'rollback', @_);
98 }
99  
100 sub _end_txn {
101 my ($driver, $method) = @_;
102  
103 my $wd = $driver->{__working_drivers};
104 $driver->{__working_drivers} = [];
105  
106 for my $part_driver (@{ $wd || [] }) {
107 $part_driver->$method;
108 }
109 }
110  
111  
112 1;
113  
114 __END__