| 1 | package DBIx::Connector; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use 5.006002; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use DBI '1.605'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use DBIx::Connector::Driver; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 13 | our $VERSION = '0.55'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 12 | 30 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 30 | my @args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 14 | bless { | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 63 | ▶ | _args => sub { @args }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 30 | _svp_depth => 0, | |||||||||||||||||||||||||||||||||||||||||
| 17 | _mode => 'no_ping', | ||||||||||||||||||||||||||||||||||||||||||
| 18 | _dond => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 19 | } => $class; | ||||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 32 | ▶ | sub DESTROY { $_[0]->disconnect if $_[0]->{_dond} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | sub _connect { | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 61 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_connect: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 61 | my @args = $self->{_args}->(); | |||||||||||||||||||||||||||||||||||||||||
| 27 | 61 | ▶ | my $dbh = do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 61 | ▶ | if ($INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$INC{"Apache/DBI.pm"} and $ENV{"MOD_PERL"}
MC/DC50%1/2
$INC{"Apache/DBI.pm"} and $ENV{"MOD_PERL"}
$INC{"Apache/DBI.pm"}
$ENV{"MOD_PERL"}
Truth table66%2/3
A and B
A= $INC{"Apache/DBI.pm"}B= $ENV{"MOD_PERL"}
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | local $DBI::connect_via = 'connect'; # Disable Apache::DBI. | |||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | DBI->connect( @args ); | |||||||||||||||||||||||||||||||||||||||||
| 31 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 60 | DBI->connect( @args ); | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | } or return undef; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | # Modify default values. | ||||||||||||||||||||||||||||||||||||||||||
| 37 | $dbh->STORE(AutoInactiveDestroy => 1) if DBI->VERSION > 1.613 && ( | ||||||||||||||||||||||||||||||||||||||||||
| 38 | @args < 4 || !exists $args[3]->{AutoInactiveDestroy} | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 60 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
"DBI"->VERSION > "1.613" and @args < 4 || !exists $args[3]{"AutoInactiveDestroy"}
@args < 4 or not exists $args[3]{"AutoInactiveDestroy"}
MC/DC33%1/3
"DBI"->VERSION > "1.613" and @args < 4 || !exists $args[3]{"AutoInactiveDestroy"}
"DBI"->VERSION > "1.613"
@args < 4
not exists $args[3]{"AutoInactiveDestroy"}
Truth table50%2/4
A and B or not(C)
A= "DBI"->VERSION > "1.613"B= @args < 4C= not exists $args[3]{"AutoInactiveDestroy"}
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | $dbh->STORE(RaiseError => 1) if @args < 4 || ( | ||||||||||||||||||||||||||||||||||||||||||
| 42 | !exists $args[3]->{RaiseError} && !exists $args[3]->{HandleError} | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 60 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
@args < 4 or not exists $args[3]{"RaiseError"} || exists $args[3]{"HandleError"}
exists $args[3]{'RaiseError'} || exists $args[3]{'HandleError'}
MC/DC100%3/3
@args < 4 or not exists $args[3]{"RaiseError"} || exists $args[3]{"HandleError"}
@args < 4
exists $args[3]{'RaiseError'}
exists $args[3]{'HandleError'}
Truth table100%4/4
A or not(B || C)
A= @args < 4B= exists $args[3]{'RaiseError'}C= exists $args[3]{'HandleError'}
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # Where are we? | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 60 | $self->{_pid} = $$; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 60 | ▶ | $self->{_tid} = threads->tid if $INC{'threads.pm'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 60 | $self->{_dbh} = $dbh; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | # Set up the driver and go! | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 60 | return $self->driver->_connect($dbh, @args); | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | sub driver { | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 229 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
driver: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 229 | ▶ | return $self->{driver} if $self->{driver}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 27 | my $driver = do { | |||||||||||||||||||||||||||||||||||||||||
| 59 | 27 | ▶ | if (my $dbh = $self->{_dbh}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 25 | $dbh->{Driver}{Name}; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | (DBI->parse_dsn( ($self->{_args}->())[0]) )[1]; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 27 | $self->{driver} = DBIx::Connector::Driver->new( $driver ); | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | sub connect { | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 4 | ▶ | my $self = shift->new(@_); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
connect: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 3 | $self->{_dond} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 3 | $self->dbh; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | sub dbh { | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 99 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dbh: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 99 | ▶ | my $dbh = $self->_seems_connected or return $self->_connect; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 64 | ▶ | return $dbh if $self->{_in_run}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 39 | ▶ | return $self->connected ? $dbh : $self->_connect; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | # Just like dbh(), except it doesn't ping the server. | ||||||||||||||||||||||||||||||||||||||||||
| 82 | sub _dbh { | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 95 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dbh: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 95 | ▶ | $self->_seems_connected || $self->_connect; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | sub connected { | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 65 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
connected: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 65 | ▶ | return unless $self->_seems_connected; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 60 | ▶ | my $dbh = $self->{_dbh} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 60 | return $self->driver->ping($dbh); | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | sub mode { | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 42 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mode: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 42 | ▶ | return $self->{_mode} unless @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 18 | ▶ | require Carp && Carp::croak(qq{Invalid mode: "$_[0]"}) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$_[0] =~ /^(?:fixup|(?:no_)?ping)$/ || require Carp && Carp::croak(qq[Invalid mode: "$_[0]"])
require Carp and Carp::croak(qq[Invalid mode: "$_[0]"])
MC/DC0%0/3
$_[0] =~ /^(?:fixup|(?:no_)?ping)$/ || require Carp && Carp::croak(qq[Invalid mode: "$_[0]"])
$_[0] =~ /^(?:fixup|(?:no_)?ping)$/
require Carp
Carp::croak(qq[Invalid mode: "$_[0]"])
Truth table0%0/4
A || B and C
A= $_[0] =~ /^(?:fixup|(?:no_)?ping)$/B= require CarpC= Carp::croak(qq[Invalid mode: "$_[0]"])
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | unless $_[0] =~ /^(?:fixup|(?:no_)?ping)$/; | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 17 | $self->{_mode} = shift; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | sub disconnect_on_destroy { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
disconnect_on_destroy: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 6 | ▶ | return $self->{_dond} unless @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 3 | $self->{_dond} = !!shift; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | sub in_txn { | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 71 | ▶ | my $dbh = shift->{_dbh} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
in_txn: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 70 | return !$dbh->FETCH('AutoCommit'); | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | # returns true if there is a database handle and the PID and TID have not | ||||||||||||||||||||||||||||||||||||||||||
| 114 | # changed and the handle's Active attribute is true. | ||||||||||||||||||||||||||||||||||||||||||
| 115 | sub _seems_connected { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 261 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_seems_connected: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 261 | ▶ | my $dbh = $self->{_dbh} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 232 | ▶ | if ( defined $self->{_tid} && $self->{_tid} != threads->tid ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $self->{"_tid"} and $self->{"_tid"} != "threads"->tid
MC/DC100%2/2
defined $self->{"_tid"} and $self->{"_tid"} != "threads"->tid
defined $self->{"_tid"}
$self->{"_tid"} != "threads"->tid
Truth table100%3/3
A and B
A= defined $self->{"_tid"}B= $self->{"_tid"} != "threads"->tid
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 120 | } elsif ( $self->{_pid} != $$ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 121 | # We've forked, so prevent the parent process handle from touching the | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # DB on DESTROY. Here in the child process, that could really screw | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # things up. This is superfluous when AutoInactiveDestroy is set, but | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # harmless. It's better to be proactive anyway. | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 3 | $dbh->STORE(InactiveDestroy => 1); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | # Use FETCH() to avoid death when called from during global destruction. | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 227 | ▶ | return $dbh->FETCH('Active') ? $dbh : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | sub disconnect { | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 31 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
disconnect: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 31 | ▶ | if (my $dbh = $self->{_dbh}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | # Some databases need this to stop spewing warnings, according to | ||||||||||||||||||||||||||||||||||||||||||
| 136 | # DBIx::Class::Storage::DBI. Probably Sybase, as the code was added | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # when Sybase ASA and SQLAnywhere support were added to DBIx::Class. | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # If that ever becomes an issue for us, add a _disconnect to the | ||||||||||||||||||||||||||||||||||||||||||
| 139 | # Driver class that does it, don't do it here. | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # $dbh->STORE(CachedKids => {}); | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 22 | $dbh->disconnect; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 22 | $self->{_dbh} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 31 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | sub run { | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 44 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 44 | ▶ | my $mode = ref $_[0] eq 'CODE' ? $self->{_mode} : shift; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 44 | local $self->{_mode} = $mode; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 44 | ▶ | return $self->_fixup_run(@_) if $mode eq 'fixup'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 29 | return $self->_run(@_); | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | sub _run { | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 29 | ▶ | my ($self, $code) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 29 | ▶ | my $dbh = $self->{_mode} eq 'ping' ? $self->dbh : $self->_dbh; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 29 | local $self->{_in_run} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 159 | 29 | return _exec( $dbh, $code, wantarray ); | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | sub _fixup_run { | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 15 | ▶ | my ($self, $code) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fixup_run: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 15 | my $dbh = $self->_dbh; | |||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 15 | my $wantarray = wantarray; | |||||||||||||||||||||||||||||||||||||||||
| 167 | return _exec( $dbh, $code, $wantarray ) | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 15 | ▶ | if $self->{_in_run} || !$dbh->FETCH('AutoCommit'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"_in_run"} or not $dbh->FETCH("AutoCommit")
MC/DC50%1/2
$self->{"_in_run"} or not $dbh->FETCH("AutoCommit")
$self->{"_in_run"}
not $dbh->FETCH("AutoCommit")
Truth table66%2/3
A or not(B)
A= $self->{"_in_run"}B= not $dbh->FETCH("AutoCommit")
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 13 | local $self->{_in_run} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 13 | my ($err, @ret); | |||||||||||||||||||||||||||||||||||||||||
| 172 | TRY: { | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 13 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 13 | @ret = eval { _exec( $dbh, $code, $wantarray ) }; | |||||||||||||||||||||||||||||||||||||||||
| 175 | 13 | $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 13 | ▶ | if ($err) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 2 | ▶ | die $err if $self->connected; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | # Not connected. Try again. | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | return _exec( $self->_connect, $code, $wantarray, @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 11 | ▶ | return $wantarray ? @ret : $ret[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | sub txn { | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 79 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
txn: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 79 | ▶ | my $mode = ref $_[0] eq 'CODE' ? $self->{_mode} : shift; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 79 | local $self->{_mode} = $mode; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 79 | ▶ | return $self->_txn_fixup_run(@_) if $mode eq 'fixup'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 53 | return $self->_txn_run(@_); | |||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | sub _txn_run { | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 53 | ▶ | my ($self, $code) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_txn_run: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 53 | my $driver = $self->driver; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 53 | my $wantarray = wantarray; | |||||||||||||||||||||||||||||||||||||||||
| 199 | 53 | ▶ | my $dbh = $self->{_mode} eq 'ping' ? $self->dbh : $self->_dbh; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | 53 | ▶ | unless ($dbh->FETCH('AutoCommit')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 6 | local $self->{_in_run} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 6 | return _exec( $dbh, $code, $wantarray ); | |||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 47 | my ($err, @ret); | |||||||||||||||||||||||||||||||||||||||||
| 207 | TRY: { | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 47 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 47 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 210 | 47 | local $self->{_in_run} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 211 | 47 | $driver->begin_work($dbh); | |||||||||||||||||||||||||||||||||||||||||
| 212 | 47 | @ret = _exec( $dbh, $code, $wantarray ); | |||||||||||||||||||||||||||||||||||||||||
| 213 | 40 | $driver->commit($dbh); | |||||||||||||||||||||||||||||||||||||||||
| 214 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 47 | $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 47 | ▶ | if ($err) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 7 | $err = $driver->_rollback($dbh, $err); | |||||||||||||||||||||||||||||||||||||||||
| 220 | 7 | die $err; | |||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 40 | ▶ | return $wantarray ? @ret : $ret[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | sub _txn_fixup_run { | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 26 | ▶ | my ($self, $code) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_txn_fixup_run: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 26 | my $dbh = $self->_dbh; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 26 | my $driver = $self->driver; | |||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | 26 | my $wantarray = wantarray; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 26 | local $self->{_in_run} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 26 | ▶ | return _exec( $dbh, $code, $wantarray ) unless $dbh->FETCH('AutoCommit'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 22 | my ($err, @ret); | |||||||||||||||||||||||||||||||||||||||||
| 237 | TRY: { | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 22 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 239 | 22 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 240 | 22 | $driver->begin_work($dbh); | |||||||||||||||||||||||||||||||||||||||||
| 241 | 22 | @ret = _exec( $dbh, $code, $wantarray ); | |||||||||||||||||||||||||||||||||||||||||
| 242 | 17 | $driver->commit($dbh); | |||||||||||||||||||||||||||||||||||||||||
| 243 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 22 | $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 22 | ▶ | if ($err) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 5 | ▶ | if ($self->connected) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 1 | $err = $driver->_rollback($dbh, $err); | |||||||||||||||||||||||||||||||||||||||||
| 250 | 1 | die $err; | |||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | # Not connected. Try again. | ||||||||||||||||||||||||||||||||||||||||||
| 254 | 4 | $dbh = $self->_connect; | |||||||||||||||||||||||||||||||||||||||||
| 255 | TRY: { | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 4 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 257 | 4 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 258 | 4 | $driver->begin_work($dbh); | |||||||||||||||||||||||||||||||||||||||||
| 259 | 4 | @ret = _exec( $dbh, $code, $wantarray ); | |||||||||||||||||||||||||||||||||||||||||
| 260 | 1 | $driver->commit($dbh); | |||||||||||||||||||||||||||||||||||||||||
| 261 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 4 | $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 4 | ▶ | if ($err) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 3 | $err = $driver->_rollback($dbh, $err); | |||||||||||||||||||||||||||||||||||||||||
| 266 | 3 | die $err; | |||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | 18 | ▶ | return $wantarray ? @ret : $ret[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | sub svp { | ||||||||||||||||||||||||||||||||||||||||||
| 274 | 39 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
svp: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 39 | my $dbh = $self->{_dbh}; | |||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | # Gotta have a transaction. | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 39 | ▶ | return $self->txn( @_ ) if !$dbh || $dbh->FETCH('AutoCommit'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $dbh or $dbh->FETCH("AutoCommit")
MC/DC50%1/2
not $dbh or $dbh->FETCH("AutoCommit")
not $dbh
$dbh->FETCH("AutoCommit")
Truth table66%2/3
not(A) or B
A= not $dbhB= $dbh->FETCH("AutoCommit")
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | 24 | ▶ | my $mode = ref $_[0] eq 'CODE' ? $self->{_mode} : shift; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 24 | local $self->{_mode} = $mode; | |||||||||||||||||||||||||||||||||||||||||
| 282 | 24 | my $code = shift; | |||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 24 | my ($err, @ret); | |||||||||||||||||||||||||||||||||||||||||
| 285 | 24 | my $wantarray = wantarray; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 24 | my $driver = $self->driver; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 24 | my $name = "savepoint_$self->{_svp_depth}"; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 24 | ++$self->{_svp_depth}; | |||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | TRY: { | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 24 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 292 | 24 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 293 | 24 | $driver->savepoint($dbh, $name); | |||||||||||||||||||||||||||||||||||||||||
| 294 | 24 | @ret = _exec( $dbh, $code, $wantarray ); | |||||||||||||||||||||||||||||||||||||||||
| 295 | 20 | $driver->release($dbh, $name); | |||||||||||||||||||||||||||||||||||||||||
| 296 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 24 | $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 24 | --$self->{_svp_depth}; | |||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 24 | ▶ | if ($err) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | # If we died, there is nothing to be done. | ||||||||||||||||||||||||||||||||||||||||||
| 303 | 4 | ▶ | if ($self->connected) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | 4 | $err = $driver->_rollback_and_release($dbh, $name, $err); | |||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 4 | die $err; | |||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | 20 | ▶ | return $wantarray ? @ret : $ret[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | sub _exec { | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 152 | ▶ | my ($dbh, $code, $wantarray) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_exec: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 152 | ▶ | local $_ = $dbh or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | # Block prevents exiting via next or last, otherwise no commit/rollback. | ||||||||||||||||||||||||||||||||||||||||||
| 316 | NOEXIT: { | ||||||||||||||||||||||||||||||||||||||||||
| 317 | 152 | ▶ | return $wantarray ? $code->($dbh) : scalar $code->($dbh) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | if defined $wantarray; | ||||||||||||||||||||||||||||||||||||||||||
| 319 | 70 | return $code->($dbh); | |||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | 18 | return; | |||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | 13 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 325 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | =head1 Name | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | DBIx::Connector - Fast, safe DBI connection and transaction management | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | =head1 Synopsis | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | use DBIx::Connector; | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | # Create a connection. | ||||||||||||||||||||||||||||||||||||||||||
| 336 | my $conn = DBIx::Connector->new($dsn, $username, $password, { | ||||||||||||||||||||||||||||||||||||||||||
| 337 | RaiseError => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 338 | AutoCommit => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 339 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | # Get the database handle and do something with it. | ||||||||||||||||||||||||||||||||||||||||||
| 342 | my $dbh = $conn->dbh; | ||||||||||||||||||||||||||||||||||||||||||
| 343 | $dbh->do('INSERT INTO foo (name) VALUES (?)', undef, 'Fred' ); | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | # Do something with the handle more efficiently. | ||||||||||||||||||||||||||||||||||||||||||
| 346 | $conn->run(fixup => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 347 | $_->do('INSERT INTO foo (name) VALUES (?)', undef, 'Fred' ); | ||||||||||||||||||||||||||||||||||||||||||
| 348 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | =head1 Description | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | DBIx::Connector provides a simple interface for fast and safe DBI connection | ||||||||||||||||||||||||||||||||||||||||||
| 353 | and transaction management. Connecting to a database can be expensive; you | ||||||||||||||||||||||||||||||||||||||||||
| 354 | don't want your application to re-connect every time you need to run a query. | ||||||||||||||||||||||||||||||||||||||||||
| 355 | The efficient thing to do is to hang on to a database handle to maintain a | ||||||||||||||||||||||||||||||||||||||||||
| 356 | connection to the database in order to minimize that overhead. DBIx::Connector | ||||||||||||||||||||||||||||||||||||||||||
| 357 | lets you do that without having to worry about dropped or corrupted | ||||||||||||||||||||||||||||||||||||||||||
| 358 | connections. | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | You might be familiar with L<Apache::DBI|Apache::DBI> and with the L<DBI>'s | ||||||||||||||||||||||||||||||||||||||||||
| 361 | L<C<connect_cached()>|DBI/connect_cached> constructor. DBIx::Connector serves | ||||||||||||||||||||||||||||||||||||||||||
| 362 | a similar need, but does a much better job. How is it different? I'm glad you | ||||||||||||||||||||||||||||||||||||||||||
| 363 | asked! | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | =item * Fork Safety | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | Like Apache::DBI, but unlike C<connect_cached()>, DBIx::Connector create a new | ||||||||||||||||||||||||||||||||||||||||||
| 370 | database connection if a new process has been C<fork>ed. This happens all the | ||||||||||||||||||||||||||||||||||||||||||
| 371 | time under L<mod_perl>, in L<POE> applications, and elsewhere. Works best with | ||||||||||||||||||||||||||||||||||||||||||
| 372 | DBI 1.614 and higher. | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | =item * Thread Safety | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | Unlike Apache::DBI or C<connect_cached()>, DBIx::Connector will create a new | ||||||||||||||||||||||||||||||||||||||||||
| 377 | database connection if a new thread has been spawned. As with C<fork>ing, | ||||||||||||||||||||||||||||||||||||||||||
| 378 | spawning a new thread can break database connections. | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | =item * Works Anywhere | ||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | Unlike Apache::DBI, DBIx::Connector runs anywhere -- inside of mod_perl or | ||||||||||||||||||||||||||||||||||||||||||
| 383 | not. Why limit yourself? | ||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | =item * Explicit Interface | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | DBIx::Connector has an explicit interface. There is none of the magical | ||||||||||||||||||||||||||||||||||||||||||
| 388 | action-at-a-distance crap that Apache::DBI is guilty of, and no global | ||||||||||||||||||||||||||||||||||||||||||
| 389 | caching. I've personally diagnosed a few issues with Apache::DBI's magic, and | ||||||||||||||||||||||||||||||||||||||||||
| 390 | killed it off in two different projects in favor of C<connect_cached()>, only | ||||||||||||||||||||||||||||||||||||||||||
| 391 | to be tripped up by other gotchas. No more. | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | =item * Optimistic Execution | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | If you use C<run()> and C<txn()>, the database handle will be passed without | ||||||||||||||||||||||||||||||||||||||||||
| 396 | first pinging the server. For the 99% or more of the time when the database is | ||||||||||||||||||||||||||||||||||||||||||
| 397 | just there, you'll save a ton of overhead without the ping. | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | DBIx::Connector's other feature is transaction management. Borrowing an | ||||||||||||||||||||||||||||||||||||||||||
| 402 | interface from L<DBIx::Class>, DBIx::Connector offers an API that efficiently | ||||||||||||||||||||||||||||||||||||||||||
| 403 | handles the scoping of database transactions so that you needn't worry about | ||||||||||||||||||||||||||||||||||||||||||
| 404 | managing the transaction yourself. Even better, it offers an API for | ||||||||||||||||||||||||||||||||||||||||||
| 405 | savepoints if your database supports them. Within a transaction, you can scope | ||||||||||||||||||||||||||||||||||||||||||
| 406 | savepoints to behave like subtransactions, so that you can save some of your | ||||||||||||||||||||||||||||||||||||||||||
| 407 | work in a transaction even if part of it fails. See L<C<txn()>|/"txn"> and | ||||||||||||||||||||||||||||||||||||||||||
| 408 | L<C<svp()>|/"svp"> for the goods. | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | =head1 Usage | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | Unlike L<Apache::DBI> and L<C<connect_cached()>|DBI/connect_cached>, | ||||||||||||||||||||||||||||||||||||||||||
| 413 | DBIx::Connector doesn't cache database handles. Rather, for a given | ||||||||||||||||||||||||||||||||||||||||||
| 414 | connection, it makes sure that the connection is just there whenever you want | ||||||||||||||||||||||||||||||||||||||||||
| 415 | it, to the extent possible. The upshot is that it's safe to create a | ||||||||||||||||||||||||||||||||||||||||||
| 416 | connection and then keep it around for as long as you need it, like so: | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | my $conn = DBIx::Connector->new(@args); | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | You can store the connection somewhere in your app where you can easily access | ||||||||||||||||||||||||||||||||||||||||||
| 421 | it, and for as long as it remains in scope, it will try its hardest to | ||||||||||||||||||||||||||||||||||||||||||
| 422 | maintain a database connection. Even across C<fork>s (especially with DBI | ||||||||||||||||||||||||||||||||||||||||||
| 423 | 1.614 and higher) and new threads, and even calls to | ||||||||||||||||||||||||||||||||||||||||||
| 424 | C<< $conn->dbh->disconnect >>. When you don't need it anymore, let it go out | ||||||||||||||||||||||||||||||||||||||||||
| 425 | of scope and the database connection will be closed. | ||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | The upshot is that your code is responsible for hanging onto a connection for | ||||||||||||||||||||||||||||||||||||||||||
| 428 | as long as it needs it. There is no magical connection caching like in | ||||||||||||||||||||||||||||||||||||||||||
| 429 | L<Apache::DBI|Apache::DBI> and L<C<connect_cached()>|DBI/connect_cached>. | ||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||
| 431 | =head2 Execution Methods | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | The real utility of DBIx::Connector comes from the use of the execution | ||||||||||||||||||||||||||||||||||||||||||
| 434 | methods, L<C<run()>|/"run">, L<C<txn()>|/"txn">, or L<C<svp()>|/"svp">. | ||||||||||||||||||||||||||||||||||||||||||
| 435 | Instead of this: | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | $conn->dbh->do($query); | ||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||
| 439 | Try this: | ||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | $conn->run(sub { $_->do($query) }); # returns retval from the sub {...} | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | The difference is that the C<run()> optimistically assumes that an existing | ||||||||||||||||||||||||||||||||||||||||||
| 444 | database handle is connected and executes the code reference without pinging | ||||||||||||||||||||||||||||||||||||||||||
| 445 | the database. The vast majority of the time, the connection will of course | ||||||||||||||||||||||||||||||||||||||||||
| 446 | still be open. You therefore save the overhead of a ping query every time you | ||||||||||||||||||||||||||||||||||||||||||
| 447 | use C<run()> (or C<txn()>). | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | Of course, if a block passed to C<run()> dies because the DBI isn't actually | ||||||||||||||||||||||||||||||||||||||||||
| 450 | connected to the database you'd need to catch that failure and try again. | ||||||||||||||||||||||||||||||||||||||||||
| 451 | DBIx::Connector provides a way to overcome this issue: connection modes. | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | =head3 Connection Modes | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | When calling L<C<run()>|/"run">, L<C<txn()>|/"txn">, or L<C<svp()>|/"svp">, | ||||||||||||||||||||||||||||||||||||||||||
| 456 | each executes within the context of a "connection mode." The supported modes | ||||||||||||||||||||||||||||||||||||||||||
| 457 | are: | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | =item * C<ping> | ||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | =item * C<fixup> | ||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||
| 465 | =item * C<no_ping> | ||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||
| 467 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | Use them via an optional first argument, like so: | ||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | $conn->run(ping => sub { $_->do($query) }); | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | Or set up a default mode via the C<mode()> accessor: | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | $conn->mode('fixup'); | ||||||||||||||||||||||||||||||||||||||||||
| 476 | $conn->run(sub { $_->do($query) }); | ||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | The return value of the block will be returned from the method call in scalar | ||||||||||||||||||||||||||||||||||||||||||
| 479 | or array context as appropriate, and the block can use C<wantarray> to | ||||||||||||||||||||||||||||||||||||||||||
| 480 | determine the context. Returning the value makes them handy for things like | ||||||||||||||||||||||||||||||||||||||||||
| 481 | constructing a statement handle: | ||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | my $sth = $conn->run(fixup => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 484 | my $sth = $_->prepare('SELECT isbn, title, rating FROM books'); | ||||||||||||||||||||||||||||||||||||||||||
| 485 | $sth->execute; | ||||||||||||||||||||||||||||||||||||||||||
| 486 | $sth; | ||||||||||||||||||||||||||||||||||||||||||
| 487 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | In C<ping> mode, C<run()> will ping the database I<before> running the block. | ||||||||||||||||||||||||||||||||||||||||||
| 490 | This is similar to what L<Apache::DBI> and the L<DBI>'s | ||||||||||||||||||||||||||||||||||||||||||
| 491 | L<C<connect_cached()>|DBI/connect_cached> method do to check the database | ||||||||||||||||||||||||||||||||||||||||||
| 492 | connection, and is the safest way to do so. If the ping fails, DBIx::Connector | ||||||||||||||||||||||||||||||||||||||||||
| 493 | will attempt to reconnect to the database before executing the block. However, | ||||||||||||||||||||||||||||||||||||||||||
| 494 | C<ping> mode does impose the overhead of the C<ping> ever time you use it. | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | In C<fixup> mode, DBIx::Connector executes the block without pinging the | ||||||||||||||||||||||||||||||||||||||||||
| 497 | database. But in the event the block throws an exception, if DBIx::Connector | ||||||||||||||||||||||||||||||||||||||||||
| 498 | finds that the database handle is no longer connected, it will reconnect to | ||||||||||||||||||||||||||||||||||||||||||
| 499 | the database and re-execute the block. Therefore, the code reference should | ||||||||||||||||||||||||||||||||||||||||||
| 500 | have B<no side-effects outside of the database,> as double-execution in the | ||||||||||||||||||||||||||||||||||||||||||
| 501 | event of a stale database connection could break something: | ||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||
| 503 | my $count; | ||||||||||||||||||||||||||||||||||||||||||
| 504 | $conn->run(fixup => sub { $count++ }); | ||||||||||||||||||||||||||||||||||||||||||
| 505 | say $count; # may be 1 or 2 | ||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | C<fixup> is the most efficient connection mode. If you're confident that the | ||||||||||||||||||||||||||||||||||||||||||
| 508 | block will have no deleterious side-effects if run twice, this is the best | ||||||||||||||||||||||||||||||||||||||||||
| 509 | option to choose. If you decide that your block is likely to have too many | ||||||||||||||||||||||||||||||||||||||||||
| 510 | side-effects to execute more than once, you can simply switch to C<ping> mode. | ||||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||
| 512 | The default is C<no_ping>, but you likely won't ever use it directly, and | ||||||||||||||||||||||||||||||||||||||||||
| 513 | isn't recommended in any event. | ||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | Simple, huh? Better still, go for the transaction management in | ||||||||||||||||||||||||||||||||||||||||||
| 516 | L<C<txn()>|/"txn"> and the savepoint management in L<C<svp()>|/"svp">. You | ||||||||||||||||||||||||||||||||||||||||||
| 517 | won't be sorry, I promise. | ||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||
| 519 | =head3 Rollback Exceptions | ||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||
| 521 | In the event of a rollback in L<C<txn()>|/"txn"> or L<C<svp()>|/"svp">, if the | ||||||||||||||||||||||||||||||||||||||||||
| 522 | rollback itself fails, a DBIx::Connector::TxnRollbackError or | ||||||||||||||||||||||||||||||||||||||||||
| 523 | DBIx::Connector::SvpRollbackError exception will be thrown, as appropriate. | ||||||||||||||||||||||||||||||||||||||||||
| 524 | These classes, which inherit from DBIx::Connector::RollbackError, stringify to | ||||||||||||||||||||||||||||||||||||||||||
| 525 | display both the rollback error and the transaction or savepoint error that | ||||||||||||||||||||||||||||||||||||||||||
| 526 | led to the rollback, something like this: | ||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||
| 528 | Transaction aborted: No such table "foo" at foo.pl line 206. | ||||||||||||||||||||||||||||||||||||||||||
| 529 | Transaction rollback failed: Invalid transaction ID at foo.pl line 203. | ||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||
| 531 | For finer-grained exception handling, you can access the individual errors via | ||||||||||||||||||||||||||||||||||||||||||
| 532 | accessors: | ||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||
| 534 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||
| 536 | =item C<error> | ||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||
| 538 | The transaction or savepoint error. | ||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||
| 540 | =item C<rollback_error> | ||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||
| 542 | The rollback error. | ||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||
| 544 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||
| 546 | For example: | ||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||
| 548 | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||||
| 549 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 550 | $conn->txn(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 551 | # ... | ||||||||||||||||||||||||||||||||||||||||||
| 552 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 553 | } catch { | ||||||||||||||||||||||||||||||||||||||||||
| 554 | if (eval { $_->isa('DBIx::Connector::RollbackError') }) { | ||||||||||||||||||||||||||||||||||||||||||
| 555 | say STDERR 'Transaction aborted: ', $_->error; | ||||||||||||||||||||||||||||||||||||||||||
| 556 | say STDERR 'Rollback failed too: ', $_->rollback_error; | ||||||||||||||||||||||||||||||||||||||||||
| 557 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 558 | warn "Caught exception: $_"; | ||||||||||||||||||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||||||||||||||||
| 560 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||
| 562 | If a L<C<svp()>|/"svp"> rollback fails and its surrounding L<C<txn()>|/"txn"> | ||||||||||||||||||||||||||||||||||||||||||
| 563 | rollback I<also> fails, the thrown DBIx::Connetor::TxnRollbackError exception | ||||||||||||||||||||||||||||||||||||||||||
| 564 | object will have the savepoint rollback exception, which will be an | ||||||||||||||||||||||||||||||||||||||||||
| 565 | DBIx::Connetor::SvpRollbackError exception object in its C<error> attribute: | ||||||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||||||
| 567 | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||||
| 568 | $conn->txn(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 569 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 570 | $conn->svp(sub { # ... }); | ||||||||||||||||||||||||||||||||||||||||||
| 571 | } catch { | ||||||||||||||||||||||||||||||||||||||||||
| 572 | if (eval { $_->isa('DBIx::Connector::RollbackError') }) { | ||||||||||||||||||||||||||||||||||||||||||
| 573 | if (eval { $_->error->isa('DBIx::Connector::SvpRollbackError') }) { | ||||||||||||||||||||||||||||||||||||||||||
| 574 | say STDERR 'Savepoint aborted: ', $_->error->error; | ||||||||||||||||||||||||||||||||||||||||||
| 575 | say STDERR 'Its rollback failed too: ', $_->error->rollback_error; | ||||||||||||||||||||||||||||||||||||||||||
| 576 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 577 | say STDERR 'Transaction aborted: ', $_->error; | ||||||||||||||||||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||||||||||||||||
| 579 | say STDERR 'Transaction rollback failed too: ', $_->rollback_error; | ||||||||||||||||||||||||||||||||||||||||||
| 580 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 581 | warn "Caught exception: $_"; | ||||||||||||||||||||||||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||||||||||||||||||||||
| 583 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 584 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||||
| 586 | But most of the time, you should be fine with the stringified form of the | ||||||||||||||||||||||||||||||||||||||||||
| 587 | exception, which will look something like this: | ||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||
| 589 | Transaction aborted: Savepoint aborted: No such table "bar" at foo.pl line 190. | ||||||||||||||||||||||||||||||||||||||||||
| 590 | Savepoint rollback failed: Invalid savepoint name at foo.pl line 161. | ||||||||||||||||||||||||||||||||||||||||||
| 591 | Transaction rollback failed: Invalid transaction identifier at fool.pl line 184. | ||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | This allows you to see you original SQL error, as well as the errors for the | ||||||||||||||||||||||||||||||||||||||||||
| 594 | savepoint rollback and transaction rollback failures. | ||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||
| 596 | =head1 Interface | ||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||
| 598 | And now for the nitty-gritty. | ||||||||||||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||||||||||||
| 600 | =head2 Constructor | ||||||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||||||
| 602 | =head3 C<new> | ||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||
| 604 | my $conn = DBIx::Connector->new($dsn, $username, $password, { | ||||||||||||||||||||||||||||||||||||||||||
| 605 | RaiseError => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 606 | AutoCommit => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 607 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||||
| 609 | Constructs and returns a DBIx::Connector object. The supported arguments are | ||||||||||||||||||||||||||||||||||||||||||
| 610 | exactly the same as those supported by the L<DBI>. Default values for those | ||||||||||||||||||||||||||||||||||||||||||
| 611 | parameters vary from the DBI as follows: | ||||||||||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||||||||||
| 613 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||
| 615 | =item C<RaiseError> | ||||||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||||||
| 617 | Defaults to true if unspecified, and if C<HandleError> is unspecified. Use of | ||||||||||||||||||||||||||||||||||||||||||
| 618 | the C<RaiseError> attribute, or a C<HandleError> attribute that always throws | ||||||||||||||||||||||||||||||||||||||||||
| 619 | exceptions (such as that provided by L<Exception::Class::DBI>), is required | ||||||||||||||||||||||||||||||||||||||||||
| 620 | for the exception-handling functionality of L<C<run()>|/"run">, | ||||||||||||||||||||||||||||||||||||||||||
| 621 | L<C<txn()>|/"txn">, and L<C<svp()>|/"svp"> to work properly. Their explicit | ||||||||||||||||||||||||||||||||||||||||||
| 622 | use is therefor recommended if for proper error handling with these execution | ||||||||||||||||||||||||||||||||||||||||||
| 623 | methods. | ||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||
| 625 | =item C<AutoInactiveDestroy> | ||||||||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||||||
| 627 | Added in L<DBI> 1.613. Defaults to true if unspecified. This is important for | ||||||||||||||||||||||||||||||||||||||||||
| 628 | safe disconnects across forking processes. | ||||||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||||||
| 630 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||||||
| 632 | In addition, explicitly setting C<AutoCommit> to true is strongly recommended | ||||||||||||||||||||||||||||||||||||||||||
| 633 | if you plan to use L<C<txn()>|/"txn"> or L<C<svp()>|/"svp">, as otherwise you | ||||||||||||||||||||||||||||||||||||||||||
| 634 | won't get the transactional scoping behavior of those two methods. | ||||||||||||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||||||||||||
| 636 | If you would like to execute custom logic each time a new connection to the | ||||||||||||||||||||||||||||||||||||||||||
| 637 | database is made you can pass a sub as the C<connected> key to the | ||||||||||||||||||||||||||||||||||||||||||
| 638 | C<Callbacks> parameter. See L<DBI/Callbacks> for usage and other available | ||||||||||||||||||||||||||||||||||||||||||
| 639 | callbacks. | ||||||||||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||||||||
| 641 | Other attributes may be modified by individual drivers. See the documentation | ||||||||||||||||||||||||||||||||||||||||||
| 642 | for the drivers for details: | ||||||||||||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||||||||||||
| 644 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||||||
| 646 | =item L<DBIx::Connector::Driver::MSSQL> | ||||||||||||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||||||||||||
| 648 | =item L<DBIx::Connector::Driver::Oracle> | ||||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||||
| 650 | =item L<DBIx::Connector::Driver::Pg> | ||||||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||||||
| 652 | =item L<DBIx::Connector::Driver::SQLite> | ||||||||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||||||||
| 654 | =item L<DBIx::Connector::Driver::mysql> | ||||||||||||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||||||||||||
| 656 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||||||||||||
| 658 | =head2 Class Method | ||||||||||||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||||||||||||
| 660 | =head3 C<connect> | ||||||||||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||||||||||
| 662 | my $dbh = DBIx::Connector->connect($dsn, $username, $password, \%attr); | ||||||||||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||||||||||
| 664 | Syntactic sugar for: | ||||||||||||||||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||||||||||||||||
| 666 | my $dbh = DBIx::Connector->new(@args)->dbh; | ||||||||||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||||||||||
| 668 | Though there's probably not much point in that, as you'll generally want to | ||||||||||||||||||||||||||||||||||||||||||
| 669 | hold on to the DBIx::Connector object. Otherwise you'd just use the L<DBI>, | ||||||||||||||||||||||||||||||||||||||||||
| 670 | no? | ||||||||||||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||||||||||||
| 672 | =head2 Instance Methods | ||||||||||||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||||||||||||
| 674 | =head3 C<dbh> | ||||||||||||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||||||||||||
| 676 | my $dbh = $conn->dbh; | ||||||||||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||||||||||
| 678 | Returns the connection's database handle. It will use a an existing handle if | ||||||||||||||||||||||||||||||||||||||||||
| 679 | there is one, if the process has not been C<fork>ed or a new thread spawned, | ||||||||||||||||||||||||||||||||||||||||||
| 680 | and if the database is pingable. Otherwise, it will instantiate, cache, and | ||||||||||||||||||||||||||||||||||||||||||
| 681 | return a new handle. | ||||||||||||||||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||||||||||||||||
| 683 | When called from blocks passed to L<C<run()>|/"run">, L<C<txn()>|/"txn">, and | ||||||||||||||||||||||||||||||||||||||||||
| 684 | L<C<svp()>|/"svp">, C<dbh()> assumes that the pingability of the database is | ||||||||||||||||||||||||||||||||||||||||||
| 685 | handled by those methods and skips the C<ping()>. Otherwise, it performs all | ||||||||||||||||||||||||||||||||||||||||||
| 686 | the same validity checks. The upshot is that it's safe to call C<dbh()> inside | ||||||||||||||||||||||||||||||||||||||||||
| 687 | those blocks without the overhead of multiple C<ping>s. Indeed, it's | ||||||||||||||||||||||||||||||||||||||||||
| 688 | preferable to do so if you're doing lots of non-database processing in those | ||||||||||||||||||||||||||||||||||||||||||
| 689 | blocks. | ||||||||||||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||||||||||||
| 691 | =head3 C<run> | ||||||||||||||||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||||||||||||||||
| 693 | $conn->run(ping => sub { $_->do($query) }); | ||||||||||||||||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||||||||||||||||
| 695 | Simply executes the block, locally setting C<$_> to and passing in the | ||||||||||||||||||||||||||||||||||||||||||
| 696 | database handle. Returns the value returned by the block in scalar or array | ||||||||||||||||||||||||||||||||||||||||||
| 697 | context as appropriate (and the block can use C<wantarray> to decide what to | ||||||||||||||||||||||||||||||||||||||||||
| 698 | do). | ||||||||||||||||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||||||||||||||||
| 700 | An optional first argument sets the connection mode, overriding that set in | ||||||||||||||||||||||||||||||||||||||||||
| 701 | the C<mode()> accessor, and may be one of C<ping>, C<fixup>, or C<no_ping> | ||||||||||||||||||||||||||||||||||||||||||
| 702 | (the default). See L</"Connection Modes"> for further explication. | ||||||||||||||||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||||||||||||||||
| 704 | For convenience, you can nest calls to C<run()> (or C<txn()> or C<svp()>), | ||||||||||||||||||||||||||||||||||||||||||
| 705 | although the connection mode will be invoked to check the connection (or not) | ||||||||||||||||||||||||||||||||||||||||||
| 706 | only in the outer-most block method call. | ||||||||||||||||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||||||||||||||||
| 708 | $conn->txn(fixup => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 709 | my $dbh = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 710 | $dbh->do($_) for @queries; | ||||||||||||||||||||||||||||||||||||||||||
| 711 | $conn->run(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 712 | $_->do($expensive_query); | ||||||||||||||||||||||||||||||||||||||||||
| 713 | $conn->txn(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 714 | $_->do($another_expensive_query); | ||||||||||||||||||||||||||||||||||||||||||
| 715 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 716 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 717 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||||||||||||||||
| 719 | All code executed inside the top-level call to C<txn()> will be executed in a | ||||||||||||||||||||||||||||||||||||||||||
| 720 | single transaction. If you'd like subtransactions, nest L<C<svp()>|/svp> | ||||||||||||||||||||||||||||||||||||||||||
| 721 | calls. | ||||||||||||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||||||||||||
| 723 | It's preferable to use C<dbh()> to fetch the database handle from within the | ||||||||||||||||||||||||||||||||||||||||||
| 724 | block if your code is doing lots of non-database stuff (shame on you!): | ||||||||||||||||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||||||||||||||||
| 726 | $conn->run(ping => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 727 | parse_gigabytes_of_xml(); # Get this out of the transaction! | ||||||||||||||||||||||||||||||||||||||||||
| 728 | $conn->dbh->do($query); | ||||||||||||||||||||||||||||||||||||||||||
| 729 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||||||||||||||||
| 731 | This is because C<dbh()> will better ensure that the database handle is active | ||||||||||||||||||||||||||||||||||||||||||
| 732 | and C<fork>- and thread-safe, although it will never C<ping()> the database | ||||||||||||||||||||||||||||||||||||||||||
| 733 | when called from inside a C<run()>, C<txn()> or C<svp()> block. | ||||||||||||||||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||||||||||||||||
| 735 | =head3 C<txn> | ||||||||||||||||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||||||||||||||||
| 737 | my $sth = $conn->txn(fixup => sub { $_->do($query) }); | ||||||||||||||||||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||||||||||||||||
| 739 | Starts a transaction, executes the block, locally setting C<$_> to and passing | ||||||||||||||||||||||||||||||||||||||||||
| 740 | in the database handle, and commits the transaction. If the block throws an | ||||||||||||||||||||||||||||||||||||||||||
| 741 | exception, the transaction will be rolled back and the exception re-thrown. | ||||||||||||||||||||||||||||||||||||||||||
| 742 | Returns the value returned by the block in scalar or array context as | ||||||||||||||||||||||||||||||||||||||||||
| 743 | appropriate (and the block can use C<wantarray> to decide what to do). | ||||||||||||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||||||||||||
| 745 | An optional first argument sets the connection mode, overriding that set in | ||||||||||||||||||||||||||||||||||||||||||
| 746 | the C<mode()> accessor, and may be one of C<ping>, C<fixup>, or C<no_ping> | ||||||||||||||||||||||||||||||||||||||||||
| 747 | (the default). In the case of C<fixup> mode, this means that the transaction | ||||||||||||||||||||||||||||||||||||||||||
| 748 | block will be re-executed for a new connection if the database handle is no | ||||||||||||||||||||||||||||||||||||||||||
| 749 | longer connected. In such a case, a second exception from the code block will | ||||||||||||||||||||||||||||||||||||||||||
| 750 | cause the transaction to be rolled back and the exception re-thrown. See | ||||||||||||||||||||||||||||||||||||||||||
| 751 | L</"Connection Modes"> for further explication. | ||||||||||||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||||||||||||
| 753 | As with C<run()>, calls to C<txn()> can be nested, although the connection | ||||||||||||||||||||||||||||||||||||||||||
| 754 | mode will be invoked to check the connection (or not) only in the outer-most | ||||||||||||||||||||||||||||||||||||||||||
| 755 | block method call. It's preferable to use C<dbh()> to fetch the database | ||||||||||||||||||||||||||||||||||||||||||
| 756 | handle from within the block if your code is doing lots of non-database | ||||||||||||||||||||||||||||||||||||||||||
| 757 | processing. | ||||||||||||||||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||||||||||||||||
| 759 | =head3 C<svp> | ||||||||||||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||||||||||||
| 761 | Executes a code block within the scope of a database savepoint if your | ||||||||||||||||||||||||||||||||||||||||||
| 762 | database supports them. Returns the value returned by the block in scalar or | ||||||||||||||||||||||||||||||||||||||||||
| 763 | array context as appropriate (and the block can use C<wantarray> to decide | ||||||||||||||||||||||||||||||||||||||||||
| 764 | what to do). | ||||||||||||||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||||||||||||
| 766 | You can think of savepoints as a kind of subtransaction. What this means is | ||||||||||||||||||||||||||||||||||||||||||
| 767 | that you can nest your savepoints and recover from failures deeper in the nest | ||||||||||||||||||||||||||||||||||||||||||
| 768 | without throwing out all changes higher up in the nest. For example: | ||||||||||||||||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||||||||||||||||
| 770 | $conn->txn(fixup => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 771 | my $dbh = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 772 | $dbh->do('INSERT INTO table1 VALUES (1)'); | ||||||||||||||||||||||||||||||||||||||||||
| 773 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 774 | $conn->svp(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 775 | shift->do('INSERT INTO table1 VALUES (2)'); | ||||||||||||||||||||||||||||||||||||||||||
| 776 | die 'OMGWTF?'; | ||||||||||||||||||||||||||||||||||||||||||
| 777 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 778 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 779 | warn "Savepoint failed\n" if $@; | ||||||||||||||||||||||||||||||||||||||||||
| 780 | $dbh->do('INSERT INTO table1 VALUES (3)'); | ||||||||||||||||||||||||||||||||||||||||||
| 781 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||||||
| 783 | This transaction will insert the values 1 and 3, but not 2. | ||||||||||||||||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||||||||||||||||
| 785 | $conn->svp(fixup => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 786 | my $dbh = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 787 | $dbh->do('INSERT INTO table1 VALUES (4)'); | ||||||||||||||||||||||||||||||||||||||||||
| 788 | $conn->svp(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 789 | shift->do('INSERT INTO table1 VALUES (5)'); | ||||||||||||||||||||||||||||||||||||||||||
| 790 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 791 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 792 | |||||||||||||||||||||||||||||||||||||||||||
| 793 | This transaction will insert both 4 and 5. | ||||||||||||||||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||||||||||||||||
| 795 | Superficially, C<svp()> resembles L<C<run()>|/"run"> and L<C<txn()>|/"txn">, | ||||||||||||||||||||||||||||||||||||||||||
| 796 | including its support for the optional L<connection mode|/"Connection Modes"> | ||||||||||||||||||||||||||||||||||||||||||
| 797 | argument, but in fact savepoints can only be used within the scope of a | ||||||||||||||||||||||||||||||||||||||||||
| 798 | transaction. Thus C<svp()> will start a transaction for you if it's called | ||||||||||||||||||||||||||||||||||||||||||
| 799 | without a transaction in-progress. It simply redispatches to C<txn()> with the | ||||||||||||||||||||||||||||||||||||||||||
| 800 | appropriate connection mode. Thus, this call from outside of a transaction: | ||||||||||||||||||||||||||||||||||||||||||
| 801 | |||||||||||||||||||||||||||||||||||||||||||
| 802 | $conn->svp(ping => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 803 | $conn->svp( sub { ... } ); | ||||||||||||||||||||||||||||||||||||||||||
| 804 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||||||||||||||||
| 806 | Is equivalent to: | ||||||||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||||||||
| 808 | $conn->txn(ping => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 809 | $conn->svp( sub { ... } ); | ||||||||||||||||||||||||||||||||||||||||||
| 810 | }) | ||||||||||||||||||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||||||||||||||||
| 812 | Savepoints are supported by the following RDBMSs: | ||||||||||||||||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||||||||||||||||
| 814 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 815 | |||||||||||||||||||||||||||||||||||||||||||
| 816 | =item * PostgreSQL 8.0 | ||||||||||||||||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||||||||||||||||
| 818 | =item * SQLite 3.6.8 | ||||||||||||||||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||||||||||||||||
| 820 | =item * MySQL 5.0.3 (InnoDB) | ||||||||||||||||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||||||||||||||||
| 822 | =item * Oracle | ||||||||||||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||||||||||||
| 824 | =item * Microsoft SQL Server | ||||||||||||||||||||||||||||||||||||||||||
| 825 | |||||||||||||||||||||||||||||||||||||||||||
| 826 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 827 | |||||||||||||||||||||||||||||||||||||||||||
| 828 | For all other RDBMSs, C<svp()> works just like C<txn()>: savepoints will be | ||||||||||||||||||||||||||||||||||||||||||
| 829 | ignored and the outer-most transaction will be the only transaction. This | ||||||||||||||||||||||||||||||||||||||||||
| 830 | tends to degrade well for non-savepoint-supporting databases, doing the right | ||||||||||||||||||||||||||||||||||||||||||
| 831 | thing in most cases. | ||||||||||||||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||||||||||||
| 833 | =head3 C<mode> | ||||||||||||||||||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||||||||||||||||
| 835 | my $mode = $conn->mode; | ||||||||||||||||||||||||||||||||||||||||||
| 836 | $conn->mode('fixup'); | ||||||||||||||||||||||||||||||||||||||||||
| 837 | $conn->txn(sub { ... }); # uses fixup mode. | ||||||||||||||||||||||||||||||||||||||||||
| 838 | $conn->mode($mode); | ||||||||||||||||||||||||||||||||||||||||||
| 839 | |||||||||||||||||||||||||||||||||||||||||||
| 840 | Gets and sets the L<connection mode|/"Connection Modes"> attribute, which is | ||||||||||||||||||||||||||||||||||||||||||
| 841 | used by C<run()>, C<txn()>, and C<svp()> if no mode is passed to them. | ||||||||||||||||||||||||||||||||||||||||||
| 842 | Defaults to "no_ping". Note that inside a block passed to C<run()>, C<txn()>, | ||||||||||||||||||||||||||||||||||||||||||
| 843 | or C<svp()>, the mode attribute will be set to the optional first parameter: | ||||||||||||||||||||||||||||||||||||||||||
| 844 | |||||||||||||||||||||||||||||||||||||||||||
| 845 | $conn->mode('ping'); | ||||||||||||||||||||||||||||||||||||||||||
| 846 | $conn->txn(fixup => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 847 | say $conn->mode; # Outputs "fixup" | ||||||||||||||||||||||||||||||||||||||||||
| 848 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 849 | say $conn->mode; # Outputs "ping" | ||||||||||||||||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||||||||||||||||
| 851 | In this way, you can reliably tell in what mode the code block is executing. | ||||||||||||||||||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||||||||||||||||
| 853 | =head3 C<connected> | ||||||||||||||||||||||||||||||||||||||||||
| 854 | |||||||||||||||||||||||||||||||||||||||||||
| 855 | if ( $conn->connected ) { | ||||||||||||||||||||||||||||||||||||||||||
| 856 | $conn->dbh->do($query); | ||||||||||||||||||||||||||||||||||||||||||
| 857 | } | ||||||||||||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||||||||||||
| 859 | Returns true if currently connected to the database and false if it's not. You | ||||||||||||||||||||||||||||||||||||||||||
| 860 | probably won't need to bother with this method; DBIx::Connector uses it | ||||||||||||||||||||||||||||||||||||||||||
| 861 | internally to determine whether or not to create a new connection to the | ||||||||||||||||||||||||||||||||||||||||||
| 862 | database before returning a handle from C<dbh()>. | ||||||||||||||||||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||||||||||||||||
| 864 | =head3 C<in_txn> | ||||||||||||||||||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||||||||||||||||
| 866 | if ( $conn->in_txn ) { | ||||||||||||||||||||||||||||||||||||||||||
| 867 | say 'Transacting!'; | ||||||||||||||||||||||||||||||||||||||||||
| 868 | } | ||||||||||||||||||||||||||||||||||||||||||
| 869 | |||||||||||||||||||||||||||||||||||||||||||
| 870 | Returns true if the connection is in a transaction. For example, inside a | ||||||||||||||||||||||||||||||||||||||||||
| 871 | C<txn()> block it would return true. It will also work if you use the DBI API | ||||||||||||||||||||||||||||||||||||||||||
| 872 | to manage transactions (i.e., C<begin_work()> or C<AutoCommit>. | ||||||||||||||||||||||||||||||||||||||||||
| 873 | |||||||||||||||||||||||||||||||||||||||||||
| 874 | Essentially, this is just sugar for: | ||||||||||||||||||||||||||||||||||||||||||
| 875 | |||||||||||||||||||||||||||||||||||||||||||
| 876 | $con->run( no_ping => sub { !$_->{AutoCommit} } ); | ||||||||||||||||||||||||||||||||||||||||||
| 877 | |||||||||||||||||||||||||||||||||||||||||||
| 878 | But without the overhead of the code reference or connection checking. | ||||||||||||||||||||||||||||||||||||||||||
| 879 | |||||||||||||||||||||||||||||||||||||||||||
| 880 | =head3 C<disconnect_on_destroy> | ||||||||||||||||||||||||||||||||||||||||||
| 881 | |||||||||||||||||||||||||||||||||||||||||||
| 882 | $conn->disconnect_on_destroy(0); | ||||||||||||||||||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||||||||||||||||
| 884 | By default, DBIx::Connector calls C<< $dbh->disconnect >> when it goes out of | ||||||||||||||||||||||||||||||||||||||||||
| 885 | scope and is garbage-collected by the system (that is, in its C<DESTROY()> | ||||||||||||||||||||||||||||||||||||||||||
| 886 | method). Usually this is what you want, but in some cases it might not be. For | ||||||||||||||||||||||||||||||||||||||||||
| 887 | example, you might have a module that uses DBIx::Connector internally, but | ||||||||||||||||||||||||||||||||||||||||||
| 888 | then makes the database handle available to callers, even after the | ||||||||||||||||||||||||||||||||||||||||||
| 889 | DBIx::Connector object goes out of scope. In such a case, you don't want the | ||||||||||||||||||||||||||||||||||||||||||
| 890 | database handle to be disconnected when the DBIx::Connector goes out of scope. | ||||||||||||||||||||||||||||||||||||||||||
| 891 | So pass a false value to C<disconnect_on_destroy> to prevent the disconnect. | ||||||||||||||||||||||||||||||||||||||||||
| 892 | An example: | ||||||||||||||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||||||||||||||
| 894 | sub database_handle { | ||||||||||||||||||||||||||||||||||||||||||
| 895 | my $conn = DBIx::Connector->new(@_); | ||||||||||||||||||||||||||||||||||||||||||
| 896 | $conn->run(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 897 | # Do stuff here. | ||||||||||||||||||||||||||||||||||||||||||
| 898 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 899 | $conn->disconnect_on_destroy(0); | ||||||||||||||||||||||||||||||||||||||||||
| 900 | return $conn->dbh; | ||||||||||||||||||||||||||||||||||||||||||
| 901 | } | ||||||||||||||||||||||||||||||||||||||||||
| 902 | |||||||||||||||||||||||||||||||||||||||||||
| 903 | Of course, if you don't need to do any work with the database handle before | ||||||||||||||||||||||||||||||||||||||||||
| 904 | returning it to your caller, you can just use C<connect()>: | ||||||||||||||||||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||||||||||||||||
| 906 | sub database_handle { | ||||||||||||||||||||||||||||||||||||||||||
| 907 | DBIx::Connector->connect(@_); | ||||||||||||||||||||||||||||||||||||||||||
| 908 | } | ||||||||||||||||||||||||||||||||||||||||||
| 909 | |||||||||||||||||||||||||||||||||||||||||||
| 910 | =head3 C<disconnect> | ||||||||||||||||||||||||||||||||||||||||||
| 911 | |||||||||||||||||||||||||||||||||||||||||||
| 912 | $conn->disconnect; | ||||||||||||||||||||||||||||||||||||||||||
| 913 | |||||||||||||||||||||||||||||||||||||||||||
| 914 | Disconnects from the database. Unless C<disconnect_on_destroy()> has been | ||||||||||||||||||||||||||||||||||||||||||
| 915 | passed a false value, DBIx::Connector uses this method internally in its | ||||||||||||||||||||||||||||||||||||||||||
| 916 | C<DESTROY> method to make sure that things are kept tidy. | ||||||||||||||||||||||||||||||||||||||||||
| 917 | |||||||||||||||||||||||||||||||||||||||||||
| 918 | =head3 C<driver> | ||||||||||||||||||||||||||||||||||||||||||
| 919 | |||||||||||||||||||||||||||||||||||||||||||
| 920 | $conn->driver->begin_work( $conn->dbh ); | ||||||||||||||||||||||||||||||||||||||||||
| 921 | |||||||||||||||||||||||||||||||||||||||||||
| 922 | In order to support all database features in a database-neutral way, | ||||||||||||||||||||||||||||||||||||||||||
| 923 | DBIx::Connector provides a number of different database drivers, subclasses of | ||||||||||||||||||||||||||||||||||||||||||
| 924 | L<DBIx::Connector::Driver>, that offer methods to handle database | ||||||||||||||||||||||||||||||||||||||||||
| 925 | communications. Although the L<DBI> provides a standard interface, for better | ||||||||||||||||||||||||||||||||||||||||||
| 926 | or for worse, not all of the drivers implement them, and some have bugs. To | ||||||||||||||||||||||||||||||||||||||||||
| 927 | avoid those issues, all database communications are handled by these driver | ||||||||||||||||||||||||||||||||||||||||||
| 928 | objects. | ||||||||||||||||||||||||||||||||||||||||||
| 929 | |||||||||||||||||||||||||||||||||||||||||||
| 930 | This can be useful if you want more fine-grained control of your | ||||||||||||||||||||||||||||||||||||||||||
| 931 | transactionality. For example, to create your own savepoint within a | ||||||||||||||||||||||||||||||||||||||||||
| 932 | transaction, you might do something like this: | ||||||||||||||||||||||||||||||||||||||||||
| 933 | |||||||||||||||||||||||||||||||||||||||||||
| 934 | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||||
| 935 | my $driver = $conn->driver; | ||||||||||||||||||||||||||||||||||||||||||
| 936 | $conn->txn(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 937 | my $dbh = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 938 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 939 | $driver->savepoint($dbh, 'mysavepoint'); | ||||||||||||||||||||||||||||||||||||||||||
| 940 | # do stuff ... | ||||||||||||||||||||||||||||||||||||||||||
| 941 | $driver->release('mysavepoint'); | ||||||||||||||||||||||||||||||||||||||||||
| 942 | } catch { | ||||||||||||||||||||||||||||||||||||||||||
| 943 | $driver->rollback_to($dbh, 'mysavepoint'); | ||||||||||||||||||||||||||||||||||||||||||
| 944 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 945 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 946 | |||||||||||||||||||||||||||||||||||||||||||
| 947 | Most often you should be able to get what you need out of L<C<txn()>|/"txn"> | ||||||||||||||||||||||||||||||||||||||||||
| 948 | and L<C<svp()>|/"svp">, but sometimes you just need the finer control. In | ||||||||||||||||||||||||||||||||||||||||||
| 949 | those cases, take advantage of the driver object to keep your use of the API | ||||||||||||||||||||||||||||||||||||||||||
| 950 | universal across database back-ends. | ||||||||||||||||||||||||||||||||||||||||||
| 951 | |||||||||||||||||||||||||||||||||||||||||||
| 952 | =head1 See Also | ||||||||||||||||||||||||||||||||||||||||||
| 953 | |||||||||||||||||||||||||||||||||||||||||||
| 954 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||||||||||||||||
| 956 | =item * L<DBIx::Connector::Driver> | ||||||||||||||||||||||||||||||||||||||||||
| 957 | |||||||||||||||||||||||||||||||||||||||||||
| 958 | =item * L<DBI> | ||||||||||||||||||||||||||||||||||||||||||
| 959 | |||||||||||||||||||||||||||||||||||||||||||
| 960 | =item * L<DBIx::Class> | ||||||||||||||||||||||||||||||||||||||||||
| 961 | |||||||||||||||||||||||||||||||||||||||||||
| 962 | =item * L<Catalyst::Model::DBI> | ||||||||||||||||||||||||||||||||||||||||||
| 963 | |||||||||||||||||||||||||||||||||||||||||||
| 964 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 965 | |||||||||||||||||||||||||||||||||||||||||||
| 966 | =head1 Support | ||||||||||||||||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||||||||||||||||
| 968 | This module is managed in an open | ||||||||||||||||||||||||||||||||||||||||||
| 969 | L<GitHub repository|http://github.com/theory/dbix-connector/>. Feel free to | ||||||||||||||||||||||||||||||||||||||||||
| 970 | fork and contribute, or to clone L<git://github.com/theory/dbix-connector.git> | ||||||||||||||||||||||||||||||||||||||||||
| 971 | and send patches! | ||||||||||||||||||||||||||||||||||||||||||
| 972 | |||||||||||||||||||||||||||||||||||||||||||
| 973 | Found a bug? Please L<post|http://github.com/theory/dbix-connector/issues> or | ||||||||||||||||||||||||||||||||||||||||||
| 974 | L<email|mailto:bug-dbix-connector@rt.cpan.org> a report! | ||||||||||||||||||||||||||||||||||||||||||
| 975 | |||||||||||||||||||||||||||||||||||||||||||
| 976 | =head1 Authors | ||||||||||||||||||||||||||||||||||||||||||
| 977 | |||||||||||||||||||||||||||||||||||||||||||
| 978 | This module was written and is maintained by: | ||||||||||||||||||||||||||||||||||||||||||
| 979 | |||||||||||||||||||||||||||||||||||||||||||
| 980 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 981 | |||||||||||||||||||||||||||||||||||||||||||
| 982 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 983 | |||||||||||||||||||||||||||||||||||||||||||
| 984 | David E. Wheeler <david@kineticode.com> | ||||||||||||||||||||||||||||||||||||||||||
| 985 | |||||||||||||||||||||||||||||||||||||||||||
| 986 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 987 | |||||||||||||||||||||||||||||||||||||||||||
| 988 | It is based on documentation, ideas, kibbitzing, and code from: | ||||||||||||||||||||||||||||||||||||||||||
| 989 | |||||||||||||||||||||||||||||||||||||||||||
| 990 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 991 | |||||||||||||||||||||||||||||||||||||||||||
| 992 | =item * Tim Bunce <http://tim.bunce.name> | ||||||||||||||||||||||||||||||||||||||||||
| 993 | |||||||||||||||||||||||||||||||||||||||||||
| 994 | =item * Brandon L. Black <blblack@gmail.com> | ||||||||||||||||||||||||||||||||||||||||||
| 995 | |||||||||||||||||||||||||||||||||||||||||||
| 996 | =item * Matt S. Trout <mst@shadowcat.co.uk> | ||||||||||||||||||||||||||||||||||||||||||
| 997 | |||||||||||||||||||||||||||||||||||||||||||
| 998 | =item * Peter Rabbitson <ribasushi@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 999 | |||||||||||||||||||||||||||||||||||||||||||
| 1000 | =item * Ash Berlin <ash@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 1001 | |||||||||||||||||||||||||||||||||||||||||||
| 1002 | =item * Rob Kinyon <rkinyon@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 1003 | |||||||||||||||||||||||||||||||||||||||||||
| 1004 | =item * Cory G Watson <gphat@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 1005 | |||||||||||||||||||||||||||||||||||||||||||
| 1006 | =item * Anders Nor Berle <berle@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 1007 | |||||||||||||||||||||||||||||||||||||||||||
| 1008 | =item * John Siracusa <siracusa@gmail.com> | ||||||||||||||||||||||||||||||||||||||||||
| 1009 | |||||||||||||||||||||||||||||||||||||||||||
| 1010 | =item * Alex Pavlovic <alex.pavlovic@taskforce-1.com> | ||||||||||||||||||||||||||||||||||||||||||
| 1011 | |||||||||||||||||||||||||||||||||||||||||||
| 1012 | =item * Many other L<DBIx::Class contributors|DBIx::Class/CONTRIBUTORS> | ||||||||||||||||||||||||||||||||||||||||||
| 1013 | |||||||||||||||||||||||||||||||||||||||||||
| 1014 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1015 | |||||||||||||||||||||||||||||||||||||||||||
| 1016 | =head1 Copyright and License | ||||||||||||||||||||||||||||||||||||||||||
| 1017 | |||||||||||||||||||||||||||||||||||||||||||
| 1018 | Copyright (c) 2009-2013 David E. Wheeler. Some Rights Reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 1019 | |||||||||||||||||||||||||||||||||||||||||||
| 1020 | This module is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 1021 | the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 1022 | |||||||||||||||||||||||||||||||||||||||||||
| 1023 | =cut | ||||||||||||||||||||||||||||||||||||||||||