| 1 | package DBIx::Connector::Driver; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 13 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 13 | our $VERSION = '0.55'; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | DRIVERS: { | ||||||||||||||||||||
| 8 | 13 | my %DRIVERS; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | sub new { | ||||||||||||||||||||
| 11 | 32 | ▶ | my ($class, $driver) = @_; | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 12 | 32 | ▶ | return $DRIVERS{$driver} ||= do { | ||||||||||||||||||
|
Condition66%2/3
$DRIVERS{$driver} ||= do {
MC/DC0%0/2
$DRIVERS{$driver} ||= do {
my $subclass = 'DBIx::Connector::Driver' . "::$driver";
eval "require $subclass";
$class = $subclass unless $@;
bless {"driver", $driver}, $class
}
$DRIVERS{$driver}
do {
my $subclass = 'DBIx::Connector::Driver' . "::$driver";
eval "require $subclass";
$class = $subclass unless $@;
bless {"driver", $driver}, $class
}
Truth table66%2/3
A ||= B
A= $DRIVERS{$driver}B= do {
my $subclass = 'DBIx::Connector::Driver' . "::$driver";
eval "require $subclass";
$class = $subclass unless $@;
bless {"driver", $driver}, $class
}
| |||||||||||||||||||||
| 13 | 12 | my $subclass = __PACKAGE__ . "::$driver"; | |||||||||||||||||||
| 14 | 12 | eval "require $subclass"; | |||||||||||||||||||
| 15 | 12 | ▶ | $class = $subclass unless $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 16 | 12 | bless { driver => $driver } => $class; | |||||||||||||||||||
| 17 | }; | ||||||||||||||||||||
| 18 | } | ||||||||||||||||||||
| 19 | } | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 13 | sub _connect { | |||||||||||||||||||
| 22 | 60 | ▶ | my ($self, $dbh, $dsn, $username, $password, $attrs) = @_; | ||||||||||||||||||
|
Subroutine
_connect: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 60 | $dbh; | |||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | sub ping { | ||||||||||||||||||||
| 27 | 60 | ▶ | my ($self, $dbh) = @_; | ||||||||||||||||||
|
Subroutine
ping: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 28 | 60 | $dbh->ping; | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | sub begin_work { | ||||||||||||||||||||
| 32 | 62 | ▶ | my ($self, $dbh) = @_; | ||||||||||||||||||
|
Subroutine
begin_work: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 33 | 62 | $dbh->begin_work; | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | sub commit { | ||||||||||||||||||||
| 37 | 45 | ▶ | my ($self, $dbh) = @_; | ||||||||||||||||||
|
Subroutine
commit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 38 | 45 | $dbh->commit; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | sub rollback { | ||||||||||||||||||||
| 42 | 3 | ▶ | my ($self, $dbh) = @_; | ||||||||||||||||||
|
Subroutine
rollback: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 43 | 3 | $dbh->rollback; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | sub _rollback { | ||||||||||||||||||||
| 47 | 11 | ▶ | my ($self, $dbh, $err) = @_; | ||||||||||||||||||
|
Subroutine
_rollback: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 48 | 11 | local $@; | |||||||||||||||||||
| 49 | 11 | eval { $dbh->rollback }; | |||||||||||||||||||
| 50 | 11 | ▶ | return $@ ? DBIx::Connector::TxnRollbackError->new( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | error => $err, | ||||||||||||||||||||
| 52 | rollback_error => $@, | ||||||||||||||||||||
| 53 | ) : $err; | ||||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | sub _rollback_and_release { | ||||||||||||||||||||
| 57 | 4 | ▶ | my ($self, $dbh, $name, $err) = @_; | ||||||||||||||||||
|
Subroutine
_rollback_and_release: called CC=2 SCAR=7.2
| |||||||||||||||||||||
| 58 | 4 | local $@; | |||||||||||||||||||
| 59 | 4 | eval { | |||||||||||||||||||
| 60 | 4 | $self->rollback_to($dbh, $name); | |||||||||||||||||||
| 61 | 0 | $self->release($dbh, $name); | |||||||||||||||||||
| 62 | }; | ||||||||||||||||||||
| 63 | 4 | ▶ | return $@ ? DBIx::Connector::SvpRollbackError->new( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | error => $err, | ||||||||||||||||||||
| 65 | rollback_error => $@, | ||||||||||||||||||||
| 66 | ) : $err; | ||||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | sub savepoint { | ||||||||||||||||||||
| 70 | 0 | ▶ | my ($self, $dbh, $name) = @_; | ||||||||||||||||||
|
Subroutine
savepoint: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | sub release { | ||||||||||||||||||||
| 74 | 0 | ▶ | my ($self, $dbh, $name) = @_; | ||||||||||||||||||
|
Subroutine
release: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | sub rollback_to { | ||||||||||||||||||||
| 78 | 0 | ▶ | my ($self, $dbh, $name) = @_; | ||||||||||||||||||
|
Subroutine
rollback_to: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | ROLLBACKERR: { | ||||||||||||||||||||
| 82 | package DBIx::Connector::RollbackError; | ||||||||||||||||||||
| 83 | 13 | our $VERSION = '0.55'; | |||||||||||||||||||
| 84 | # an exception is always true | ||||||||||||||||||||
| 85 | 13 | ▶ | use overload bool => sub {1}, '""' => 'as_string', fallback => 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 11 | ▶ | sub new { my $c = shift; bless {@_} => $c; } | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 88 | 27 | ▶ | sub error { shift->{error} } | ||||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 89 | 24 | ▶ | sub rollback_error { shift->{rollback_error} } | ||||||||||||||||||
|
Subroutine
rollback_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 90 | sub as_string { | ||||||||||||||||||||
| 91 | 15 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 92 | 15 | my $label = $self->_label; | |||||||||||||||||||
| 93 | 15 | return "$label aborted: " . $self->error | |||||||||||||||||||
| 94 | . "$label rollback failed: " . $self->rollback_error; | ||||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | package DBIx::Connector::TxnRollbackError; | ||||||||||||||||||||
| 98 | 13 | our $VERSION = '0.55'; | |||||||||||||||||||
| 99 | 13 | our @ISA = ('DBIx::Connector::RollbackError'); | |||||||||||||||||||
| 100 | 9 | ▶ | sub _label { 'Transaction' } | ||||||||||||||||||
|
Subroutine
_label: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | package DBIx::Connector::SvpRollbackError; | ||||||||||||||||||||
| 103 | 13 | our $VERSION = '0.55'; | |||||||||||||||||||
| 104 | 13 | our @ISA = ('DBIx::Connector::RollbackError'); | |||||||||||||||||||
| 105 | 6 | ▶ | sub _label { 'Savepoint' } | ||||||||||||||||||
|
Subroutine
_label: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 13 | 1; | |||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =head1 Name | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | DBIx::Connector::Driver - Database-specific connection interface | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =head1 Description | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | Some of the things that DBIx::Connector does are implemented differently by | ||||||||||||||||||||
| 118 | different drivers, or the official interface provided by the DBI may not be | ||||||||||||||||||||
| 119 | implemented for a particular driver. The driver-specific code therefore is | ||||||||||||||||||||
| 120 | encapsulated in this separate driver class. | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | Most of the DBI drivers work uniformly, so in most cases the implementation | ||||||||||||||||||||
| 123 | provided here in DBIx::Connector::Driver will work just fine. It's only when | ||||||||||||||||||||
| 124 | something is different that a driver subclass needs to be added. In such a | ||||||||||||||||||||
| 125 | case, the subclass's name is the same as the DBI driver. For example the | ||||||||||||||||||||
| 126 | driver for DBD::Pg is | ||||||||||||||||||||
| 127 | L<DBIx::Connector::Driver::Pg|DBIx::Connector::Driver::Pg> and the driver | ||||||||||||||||||||
| 128 | for DBD::mysql is | ||||||||||||||||||||
| 129 | L<DBIx::Connector::Driver::mysql|DBIx::Connector::Driver::mysql>. | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | If you're just a user of DBIx::Connector, you can ignore the driver classes. | ||||||||||||||||||||
| 132 | DBIx::Connector uses them internally to do its magic, so you needn't worry | ||||||||||||||||||||
| 133 | about them. | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =head1 Interface | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | In case you need to implement a driver, here's the interface you can modify. | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | =head2 Constructor | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =head3 C<new> | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | my $driver = DBIx::Connector::Driver->new( $driver ); | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | Constructs and returns a driver object. Each driver class is implemented as a | ||||||||||||||||||||
| 146 | singleton, so the same driver object is always returned for the same driver. | ||||||||||||||||||||
| 147 | The C<driver> parameter should be a Perl DBI driver name, such as C<Pg> for | ||||||||||||||||||||
| 148 | L<DBD::Pg|DBD::Pg> or C<SQLite> for L<DBD::SQLite|DBD::SQLite>. If a subclass | ||||||||||||||||||||
| 149 | has been defined for C<$driver>, then the object will be of that class. | ||||||||||||||||||||
| 150 | Otherwise it will be an instance of the driver base class. | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =head2 Instance Methods | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =head3 C<ping> | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | $driver->ping($dbh); | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | Calls C<< $dbh->ping >>. Override if for some reason the DBI driver doesn't do | ||||||||||||||||||||
| 159 | it right. | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =head3 C<begin_work> | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | $driver->begin_work($dbh); | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | Calls C<< $dbh->begin_work >>. Override if for some reason the DBI driver | ||||||||||||||||||||
| 166 | doesn't do it right. | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | =head3 C<commit> | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | $driver->commit($dbh); | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | Calls C<< $dbh->commit >>. Override if for some reason the DBI driver doesn't | ||||||||||||||||||||
| 173 | do it right. | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | =head3 C<rollback> | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | $driver->rollback($dbh); | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | Calls C<< $dbh->rollback >>. Override if for some reason the DBI driver | ||||||||||||||||||||
| 180 | doesn't do it right. | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =head3 C<savepoint> | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | $driver->savepoint($dbh, $name); | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | A no-op. Override if your database does in fact support savepoints. The driver | ||||||||||||||||||||
| 187 | subclass should create a savepoint with the given C<$name>. See the | ||||||||||||||||||||
| 188 | implementations in L<DBIx::Connector::Driver::Pg|DBIx::Connector::Driver::Pg> | ||||||||||||||||||||
| 189 | and L<DBIx::Connector::Driver::Oracle|DBIx::Connector::Driver::Oracle> for | ||||||||||||||||||||
| 190 | examples. | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =head3 C<release> | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | $driver->release($dbh, $name); | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | A no-op. Override if your database does in fact support savepoints. The driver | ||||||||||||||||||||
| 197 | subclass should release the savepoint with the given C<$name>. See the | ||||||||||||||||||||
| 198 | implementations in L<DBIx::Connector::Driver::Pg|DBIx::Connector::Driver::Pg> | ||||||||||||||||||||
| 199 | and L<DBIx::Connector::Driver::Oracle|DBIx::Connector::Driver::Oracle> for | ||||||||||||||||||||
| 200 | examples. | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | =head3 C<rollback_to> | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | $driver->rollback_to($dbh, $name); | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | A no-op. Override if your database does in fact support savepoints. The driver | ||||||||||||||||||||
| 207 | subclass should rollback to the savepoint with the given C<$name>. See the | ||||||||||||||||||||
| 208 | implementations in L<DBIx::Connector::Driver::Pg|DBIx::Connector::Driver::Pg> | ||||||||||||||||||||
| 209 | and L<DBIx::Connector::Driver::Oracle|DBIx::Connector::Driver::Oracle> for | ||||||||||||||||||||
| 210 | examples. | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | =head1 Authors | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | This module was written and is maintained by: | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | =over | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | =item David E. Wheeler <david@kineticode.com> | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | =back | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | It is based on code written by: | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | =over | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | =item Matt S. Trout <mst@shadowcatsystems.co.uk> | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | =item Peter Rabbitson <rabbit+dbic@rabbit.us> | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | =back | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | =head1 Copyright and License | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | Copyright (c) 2009-2013 David E. Wheeler. Some Rights Reserved. | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | This module is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 237 | the same terms as Perl itself. | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | =cut | ||||||||||||||||||||