| 1 | package DBIx::Wizard::DB; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 11 | ▶ | use DBI; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 11 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 11 | my %h_dsn; | |||||||||||||||||||||
| 8 | my %h_user; | ||||||||||||||||||||||
| 9 | 11 | my %h_password; | |||||||||||||||||||||
| 10 | 11 | my %h_options; | |||||||||||||||||||||
| 11 | 11 | my %h_dbh; | |||||||||||||||||||||
| 12 | 11 | my %h_inflate_class; | |||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 13 | ▶ | sub declare { | ||||||||||||||||||||
|
Subroutine
declare: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||
| 15 | 13 | my ($class, $db, $dsn, $user, $password, $rh_options) = @_; | |||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | # Extract inflate_class from options (not a DBI option) | ||||||||||||||||||||||
| 18 | 13 | my $inflate_class = delete $rh_options->{inflate_class}; | |||||||||||||||||||||
| 19 | 13 | ▶ | $h_inflate_class{$db} = $inflate_class if $inflate_class; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | 13 | $h_dsn{$db} = $dsn; | |||||||||||||||||||||
| 22 | 13 | $h_user{$db} = $user; | |||||||||||||||||||||
| 23 | 13 | $h_password{$db} = $password; | |||||||||||||||||||||
| 24 | 13 | ▶ | $h_options{$db} = $rh_options || {}; | ||||||||||||||||||||
|
Condition50%1/2
$rh_options || {}
MC/DC0%0/1
$rh_options || {}
$rh_options
Truth table50%1/2
A
A= $rh_options
| |||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | 2 | ▶ | sub inflate_class { | ||||||||||||||||||||
|
Subroutine
inflate_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 28 | 2 | my ($class, $db) = @_; | |||||||||||||||||||||
| 29 | 2 | return $h_inflate_class{$db}; | |||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||
| 31 | |||||||||||||||||||||||
| 32 | 2 | ▶ | sub _declare_from_env { | ||||||||||||||||||||
|
Subroutine
_declare_from_env: called CC=4 SCAR=14.0
| |||||||||||||||||||||||
| 33 | 2 | my ($class, $db) = @_; | |||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | 2 | my $env_key = 'DBIW_DECLARE_' . uc($db); | |||||||||||||||||||||
| 36 | 2 | my $env_val = $ENV{$env_key}; | |||||||||||||||||||||
| 37 | 2 | ▶ | return unless $env_val; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 38 | |||||||||||||||||||||||
| 39 | 1 | my ($dsn, $user, $password) = split /\|/, $env_val, 3; | |||||||||||||||||||||
| 40 | 1 | ▶ | $class->declare($db, $dsn, $user // '', $password // ''); | ||||||||||||||||||||
|
Condition50%2/4
$user // ""
$password // ""
MC/DC0%0/1
$user // ""
$user
MC/DC0%0/1
$password // ""
$password
Truth table50%1/2
A
A= $user
Truth table50%1/2
A
A= $password
| |||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | 101 | ▶ | sub dbh { | ||||||||||||||||||||
|
Subroutine
dbh: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||
| 44 | 101 | my ($class, $db) = @_; | |||||||||||||||||||||
| 45 | |||||||||||||||||||||||
| 46 | # Try environment-based declaration if not already declared | ||||||||||||||||||||||
| 47 | 101 | ▶ | if (!$h_dsn{$db}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 48 | 2 | $class->_declare_from_env($db); | |||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||
| 50 | |||||||||||||||||||||||
| 51 | 101 | ▶ | if (!$h_dsn{$db}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 52 | 1 | croak "DBIW: undeclared db: $db (set DBIW_DECLARE_" . uc($db) . " or call DBIx::Wizard::DB->declare)"; | |||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||
| 54 | |||||||||||||||||||||||
| 55 | 100 | ▶ | if ($h_dbh{$db}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 56 | 88 | return $h_dbh{$db}; | |||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | 12 | my $mysql_enable_utf8_after_connect = delete $h_options{$db}{mysql_enable_utf8_after_connect}; | |||||||||||||||||||||
| 60 | |||||||||||||||||||||||
| 61 | 12 | my $dbh = DBI->connect($h_dsn{$db}, $h_user{$db}, $h_password{$db}, $h_options{$db}); | |||||||||||||||||||||
| 62 | |||||||||||||||||||||||
| 63 | 12 | ▶ | if ($mysql_enable_utf8_after_connect) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 64 | 0 | $dbh->{mysql_enable_utf8} = 1; | |||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||
| 66 | |||||||||||||||||||||||
| 67 | 12 | $h_dbh{$db} = $dbh; | |||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | 12 | return $dbh; | |||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||
| 71 | |||||||||||||||||||||||
| 72 | 1 | ▶ | sub dbname { | ||||||||||||||||||||
|
Subroutine
dbname: called CC=7 SCAR=29.4
Pod
undocumented
| |||||||||||||||||||||||
| 73 | 1 | my ($class, $db) = @_; | |||||||||||||||||||||
| 74 | |||||||||||||||||||||||
| 75 | 1 | my $dbh = $class->dbh($db); | |||||||||||||||||||||
| 76 | |||||||||||||||||||||||
| 77 | 1 | ▶ | if ($dbh->{Driver}->{Name} =~ m/mysql|MariaDB/) { | ||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||
| 78 | 0 | ▶ | if ($dbh->{Name} =~ m/database=([^;]+)/) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 79 | 0 | return $1; | |||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||
| 81 | 0 | (my $dbname = $dbh->{Name}) =~ s/:.*//; | |||||||||||||||||||||
| 82 | 0 | return $dbname; | |||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||
| 84 | } elsif ($dbh->{Driver}->{Name} eq 'Pg') { | ||||||||||||||||||||||
| 85 | 0 | ▶ | if ($dbh->{Name} =~ m/dbname=([^;]+)/) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 86 | 0 | return $1; | |||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||
| 88 | } elsif ($dbh->{Driver}->{Name} eq 'SQLite') { | ||||||||||||||||||||||
| 89 | 1 | ▶ | if ($dbh->{Name} =~ m/dbname=([^;]+)/) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 90 | 1 | return $1; | |||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||
| 93 | |||||||||||||||||||||||
| 94 | 0 | croak "DBIW: unsupported database driver: " . $dbh->{Driver}->{Name}; | |||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||
| 96 | |||||||||||||||||||||||
| 97 | 0 | ▶ | sub catalog { | ||||||||||||||||||||
|
Subroutine
catalog: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 98 | 0 | return undef; | |||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | ## DB wrapper (returned by dbiw('dbname') without table) | ||||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | 8 | ▶ | sub wrapper { | ||||||||||||||||||||
|
Subroutine
wrapper: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 104 | 8 | my ($class, $db) = @_; | |||||||||||||||||||||
| 105 | 8 | return bless { db => $db }, "${class}::Wrapper"; | |||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||
| 107 | |||||||||||||||||||||||
| 108 | package DBIx::Wizard::DB::Wrapper; | ||||||||||||||||||||||
| 109 | |||||||||||||||||||||||
| 110 | 11 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 111 | 11 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 112 | |||||||||||||||||||||||
| 113 | 11 | my $savepoint_counter = 0; | |||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | 6 | ▶ | sub dbh { | ||||||||||||||||||||
|
Subroutine
dbh: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 116 | 6 | my ($self) = @_; | |||||||||||||||||||||
| 117 | 6 | return DBIx::Wizard::DB->dbh($self->{db}); | |||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||
| 119 | |||||||||||||||||||||||
| 120 | 7 | ▶ | sub transaction { | ||||||||||||||||||||
|
Subroutine
transaction: called CC=5 SCAR=16.1
| |||||||||||||||||||||||
| 121 | 7 | my ($self, $code) = @_; | |||||||||||||||||||||
| 122 | 7 | ▶ | croak "transaction requires a code reference" if (ref($code) ne 'CODE'); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 123 | |||||||||||||||||||||||
| 124 | 6 | my $dbh = $self->dbh; | |||||||||||||||||||||
| 125 | |||||||||||||||||||||||
| 126 | 6 | ▶ | if ($dbh->{AutoCommit}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 127 | # Outer transaction | ||||||||||||||||||||||
| 128 | 4 | $dbh->begin_work; | |||||||||||||||||||||
| 129 | 4 | eval { $code->(); $dbh->commit; 1 } | |||||||||||||||||||||
| 130 | 4 | ▶ | or do { my $err = $@; $dbh->rollback; die $err }; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 131 | } else { | ||||||||||||||||||||||
| 132 | # Already in a transaction — use savepoint | ||||||||||||||||||||||
| 133 | 2 | my $sp = "dbiw_sp_" . ++$savepoint_counter; | |||||||||||||||||||||
| 134 | 2 | $dbh->do("SAVEPOINT $sp"); | |||||||||||||||||||||
| 135 | 2 | eval { $code->(); $dbh->do("RELEASE SAVEPOINT $sp"); 1 } | |||||||||||||||||||||
| 136 | 2 | ▶ | or do { my $err = $@; $dbh->do("ROLLBACK TO SAVEPOINT $sp"); die $err }; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||
| 139 | |||||||||||||||||||||||
| 140 | 11 | 1; | |||||||||||||||||||||