JESP/Driver.pm

filter: Statementstmt 27.4% 20 / 73 Branchbran 3.5% 1 / 28 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 50.0% 5 / 10 Podpod 100% 3 / 3 totaltotal 20.8% 29 / 139 CC 22 SCAR 43.0
CC
22
Cov
18%
_OdbcParse
132.0
apply_script
90.0
apply_sql
6.0
CRAP 74.0
« JESP/Driver/mysql.pm ↑ Summary JESP/Cmd/CommandJESP.pm »
1 package App::JESP::Driver;
2 2 $App::JESP::Driver::VERSION = '0.016';
3 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 App::JESP::Driver - DB Specific stuff superclass.
8  
9 =cut
10  
11 2 ▶use Log::Any qw/$log/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use IPC::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 has 'jesp' => ( is => 'ro' , isa => 'App::JESP', required => 1, weak_ref => 1);
16  
17 =head2 apply_patch
18  
19 Applies the given L<App::JESP::Patch> to the database. Dies in case of error.
20  
21 You do NOT need to implement that in subclasses.
22  
23 Usage:
24  
25 $this->apply_patch( $patch );
26  
27 =cut
28  
29 5 ▶sub apply_patch{
Subroutine apply_patch: called CC=3 SCAR=14.2
Pod documented
30 5 my ($self, $patch) = @_;
31 5 $log->info("Applying patch ".$patch->id());
32 5 ▶ if( my $sql = $patch->sql() ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $sql = $patch->sql) 5 0 5
33 5 $log->trace("Patch is SQL='$sql'");
34 5 return $self->apply_sql( $sql );
35 }
36 0 ▶ if( my $script_file = $patch->script_file() ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $script_file = $patch->script_file) 0 0 0
37 0 $log->trace("Patch is SCRIPT='".$script_file."'");
38 0 return $self->apply_script( $script_file );
39 }
40 }
41  
42 =head2 apply_script
43  
44 Runs the given 'script' file, with the given environment:
45  
46 JESP_DSN : The full Perl DSN string
47 JESP_USER: DB User
48 JESP_PASSWORD: DB Password
49 JESP_SCHEME: 'dbi'
50 JESP_DRIVER: The name of the DBI driver in use
51 JESP_DRIVER_DSN: the part of the DSN after the driver
52  
53 Then the JESP_DRIVER_DSN is parsed and split into its components to generate environment variables.
54 The most common is:
55  
56 JESP_DATABASE: Name of the database to connect to
57 JESP_PORT: The port to connect to.
58 ...
59  
60 =cut
61  
62 0 ▶sub apply_script{
Subroutine apply_script: not called CC=9 SCAR=45.0
Pod documented
63 0 my ($self, $script) = @_;
64  
65 0 my @cmd = ( $script );
66  
67 0 my $input = '';
68  
69 0 ▶ my $on_stdout = sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 0 $log->info( @_ );
71 0 };
72 0 my @stderr;
73 0 ▶ my $on_stderr = sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 $log->warn( @_ );
75 0 push @stderr , @_;
76 0 };
77  
78 0 my $properties = {};
79 0 my ($scheme, $driver, $attr_string, $attr_hash, $driver_dsn) = DBI->parse_dsn( $self->jesp()->dsn() );
80 0 ref($self)->_OdbcParse( $driver_dsn , $properties , [] );
81 0 ▶ $properties->{user} ||= $self->jesp()->username();
Condition0%0/3
$properties->{"user"} ||= $self->jesp->username
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{"user"} ||= $self->jesp->username
$properties->{"user"} $self->jesp->username
Truth table0%0/3
A ||= B
A= $properties->{"user"}B= $self->jesp->username
ABresult
1X1
011
000
82 0 ▶ $properties->{password} ||= $self->jesp()->password();
Condition0%0/3
$properties->{"password"} ||= $self->jesp->password
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{"password"} ||= $self->jesp->password
$properties->{"password"} $self->jesp->password
Truth table0%0/3
A ||= B
A= $properties->{"password"}B= $self->jesp->password
ABresult
1X1
011
000
83 $properties = {
84 %$properties,
85 0 ▶ %{ defined( $attr_hash ) ? $attr_hash : {} },
Branch: 1 branch
BranchTrueFalseTotal
defined $attr_hash ? : 0 0 0
86 dsn => $self->jesp()->dsn(),
87 scheme => $scheme,
88 driver => $driver,
89 driver_dsn => $driver_dsn,
90 attr_string => $attr_string,
91 };
92 0 my %EXTRA_ENV = ();
93 # Outside testing, be verbose.
94 0 ▶ $EXTRA_ENV{IPCRUNDEBUG} = 'basic' unless( $ENV{AUTOMATED_TESTING} || $ENV{HARNESS_ACTIVE} );
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"AUTOMATED_TESTING"} or $ENV{"HARNESS_ACTIVE"} 0 0 0
Condition0%0/3
$ENV{"AUTOMATED_TESTING"} or $ENV{"HARNESS_ACTIVE"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"AUTOMATED_TESTING"} or $ENV{"HARNESS_ACTIVE"}
$ENV{"AUTOMATED_TESTING"} $ENV{"HARNESS_ACTIVE"}
Truth table0%0/3
A or B
A= $ENV{"AUTOMATED_TESTING"}B= $ENV{"HARNESS_ACTIVE"}
ABresult
1X1
011
000
95 # Transfer all the DB properties
96 0 foreach my $key ( keys %{$properties} ){
97 0 ▶ if( $properties->{$key} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($properties->{$key}) 0 0 0
98 0 $EXTRA_ENV{'JESP_'.uc($key)} = $properties->{$key};
99 }
100 }
101  
102 0 local %ENV = ( %ENV , %EXTRA_ENV );
103 0 ▶ IPC::Run::run( \@cmd , \$input , $on_stdout , $on_stderr ) or die join(' ', @cmd).": $? : ".join("\n", @stderr )."\n";
Branch: 1 branch
BranchTrueFalseTotal
IPC::Run::run(\@cmd, \$input, $on_stdout, $on_stderr) or die join(" ", @cmd) . ": $? : " . join("\n", @stderr) . "\n" 0 0 0
104 }
105  
106  
107 =head2 apply_sql
108  
109 Databases and their drivers vary a lot when it comes
110 to apply SQL patches. Some of them are just fine with sending
111 a blog of SQL to the driver, even when it contains multiple
112 statements and trigger or procedure, function definitions.
113  
114 Some of them require a specific implementation.
115  
116 This is the default implementation that just use the underlying DB
117 connection to send the patch SQL content.
118  
119 =cut
120  
121 0 ▶sub apply_sql{
Subroutine apply_sql: not called CC=2 SCAR=17.9
Pod documented
122 0 my ($self, $sql) = @_;
123 0 my $dbh = $self->jesp()->get_dbh()->();
124 0 my $ret = $dbh->do( $sql );
125 0 ▶ return defined($ret) ? $ret : confess( $dbh->errstr() );
Branch: 1 branch
BranchTrueFalseTotal
defined $ret ? : 0 0 0
126 }
127  
128  
129 # Shamelessly copied from DBD-mysql-4.043/lib/DBD/mysql.pm
130 0 ▶sub _OdbcParse {
Subroutine _OdbcParse: not called CC=11 SCAR=48.8
131 0 my($class, $dsn, $hash, $args) = @_;
132 0 my($var, $val);
133 0 ▶ if (!defined($dsn)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dsn) 0 0 0
134 0 return;
135 }
136 0 ▶ while (length($dsn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dsn) 0 0 0
137 0 ▶ if ($dsn =~ /([^:;]*\[.*]|[^:;]*)[:;](.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn =~ /([^:;]*\[.*]|[^:;]*)[:;](.*)/) { } 0 0 0
138 0 $val = $1;
139 0 $dsn = $2;
140 0 $val =~ s/\[|]//g; # Remove [] if present, the rest of the code prefers plain IPv6 addresses
141 } else {
142 0 $val = $dsn;
143 0 $dsn = '';
144 }
145 0 ▶ if ($val =~ /([^=]*)=(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /([^=]*)=(.*)/) { } 0 0 0
146 0 $var = $1;
147 0 $val = $2;
148 0 ▶ if ($var eq 'hostname' || $var eq 'host') {
Branch: 2 branches
BranchTrueFalseTotal
if ($var eq 'hostname' or $var eq 'host') { } 0 0 0
elsif ($var eq 'db' or $var eq 'dbname') { } 0 0 0
Condition0%0/3
$var eq "hostname" or $var eq "host"
l!l&&r!l&&!r
000
MC/DC0%0/2
$var eq "hostname" or $var eq "host"
$var eq "hostname" $var eq "host"
Truth table0%0/3
A or B
A= $var eq "hostname"B= $var eq "host"
ABresult
1X1
011
000
149 0 $hash->{'host'} = $val;
150 ▶ } elsif ($var eq 'db' || $var eq 'dbname') {
Condition0%0/3
$var eq "db" or $var eq "dbname"
l!l&&r!l&&!r
000
MC/DC0%0/2
$var eq "db" or $var eq "dbname"
$var eq "db" $var eq "dbname"
Truth table0%0/3
A or B
A= $var eq "db"B= $var eq "dbname"
ABresult
1X1
011
000
151 0 $hash->{'database'} = $val;
152 } else {
153 0 $hash->{$var} = $val;
154 }
155 } else {
156 0 foreach $var (@$args) {
157 0 ▶ if (!defined($hash->{$var})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash->{$var}) 0 0 0
158 0 $hash->{$var} = $val;
159 0 last;
160 }
161 }
162 }
163 }
164 }
165  
166 2 __PACKAGE__->meta()->make_immutable();
« JESP/Driver/mysql.pm ↑ Summary JESP/Cmd/CommandJESP.pm »