File Coverage

blib/lib/Starch/Store/DBIx/Connector.pm
Criterion Covered Total %
statement 26 26 100.0
branch 1 2 50.0
condition n/a
subroutine 9 9 100.0
pod 1 2 50.0
total 37 39 94.8


line stmt bran cond sub pod time code
1             package Starch::Store::DBIx::Connector;
2             $Starch::Store::DBIx::Connector::VERSION = '0.02';
3             =head1 NAME
4              
5             Starch::Store::DBIx::Connector - Starch storage backend using DBIx::Connector.
6              
7             =head1 SYNOPSIS
8              
9             my $starch = Starch->new(
10             store => {
11             class => '::DBIx::Connector',
12             connector => [
13             $dsn,
14             $username,
15             $password,
16             { RaiseError=>1, AutoCommit=>1 },
17             ],
18             table => 'my_states',
19             },
20             );
21              
22             =head1 DESCRIPTION
23              
24             This L store uses L to set and get state data.
25              
26             Very little is documented in this module as it is just a subclass
27             of L modified to use L
28             instead of L.
29              
30             =cut
31              
32 1     1   16203 use DBIx::Connector;
  1         102521  
  1         74  
33 1     1   7 use Types::Standard -types;
  1         2  
  1         16  
34 1     1   5051 use Scalar::Util qw( blessed );
  1         14  
  1         93  
35              
36 1     1   7 use Moo;
  1         2  
  1         10  
37 1     1   416 use strictures 2;
  1         10  
  1         60  
38 1     1   258 use namespace::clean;
  1         2  
  1         8  
39              
40             extends 'Starch::Store::DBI';
41              
42             sub BUILD {
43 6     6 0 582859 my ($self) = @_;
44              
45             # Get this loaded as early as possible.
46 6         140 $self->connector();
47              
48 6         525 return;
49             }
50              
51             =head1 REQUIRED ARGUMENTS
52              
53             =head2 connector
54              
55             This must be set to either an array ref arguments for L
56             or a pre-built object (often retrieved using a method proxy).
57              
58             When configuring Starch from static configuration files using a
59             L
60             is a good way to link your existing L object
61             constructor in with Starch so that starch doesn't build its own.
62              
63             =cut
64              
65              
66             has '+_dbh_arg' => (
67             isa => (InstanceOf[ 'DBIx::Connector' ]) | ArrayRef,
68             init_arg => 'connector',
69             reader => '_connector_arg',
70             );
71              
72             has '+dbh' => (
73             isa => InstanceOf[ 'DBIx::Connector' ],
74             init_arg => undef,
75             reader => 'connector',
76             builder => '_build_connector',
77             );
78             sub _build_connector {
79 6     6   775 my ($self) = @_;
80              
81 6         24 my $connector = $self->_connector_arg();
82 6 50       40 return $connector if blessed $connector;
83              
84 6         51 return DBIx::Connector->new( @$connector );
85             }
86              
87             =head1 OPTIONAL ARGUMENTS
88              
89             =head2 method
90              
91             The L method to call when executing queries.
92             Must be one of C, C, or C. Defaults to C.
93              
94             =cut
95              
96             has method => (
97             is => 'ro',
98             isa => Enum['run', 'txn', 'svp'],
99             default => 'run',
100             );
101              
102             =head2 mode
103              
104             The L to use
105             when running the L. Defaults to C which lets
106             L use whichever mode it has been configured to use.
107             Must be on of C, C, C, or C.
108              
109             Typically you will not want to set this as you will have provided
110             a pre-built L object, using a method proxy, which
111             you've already called L on.
112              
113             =cut
114              
115             has mode => (
116             is => 'ro',
117             isa => (Enum['ping', 'fixup', 'no_ping']) | Undef,
118             );
119              
120             =head1 METHODS
121              
122             =head2 set
123              
124             Set L.
125              
126             =head2 get
127              
128             Set L.
129              
130             =head2 remove
131              
132             Set L.
133              
134             =cut
135              
136             # Little clean hack to make all of the DBI code "just work".
137             # local() can be awesome.
138             our $dbh;
139 57     57 1 11039 sub dbh { $dbh }
140              
141             around qw( set get remove ) => sub{
142             my ($orig, $self, @args) = @_;
143              
144             local $Carp::Interal{ (__PACKAGE__) } = 1;
145              
146             my $method = $self->method();
147             my $mode = $self->mode();
148              
149             return $self->connector->$method(
150             defined($mode) ? ($mode) : (),
151             sub{
152             local $dbh = $_;
153             return $self->$orig( @args );
154             },
155             );
156             };
157              
158             1;
159             __END__