JESP/Driver/mysql.pm

filter: Statementstmt 38.8% 26 / 67 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 63.6% 7 / 11 Podpod 100% 1 / 1 totaltotal 29.3% 34 / 116 CC 15 SCAR 45.7
CC
15
Cov
27%
apply_sql
182.0
_build_mysql
12.0
__ANON__
2.0
CRAP 96.5
↑ Summary JESP/Driver.pm »
1 package App::JESP::Driver::mysql;
2 1 $App::JESP::Driver::mysql::VERSION = '0.016';
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends qw/App::JESP::Driver/;
5  
6 1 ▶use File::Which qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IPC::Run qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Log::Any qw/$log/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use String::ShellQuote;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 App::JESP::Driver::mysql - mysql driver. Subclasses App::JESP::Driver
17  
18 =cut
19  
20 1 has 'mysql' => ( is => 'ro', isa => 'Str', lazy_build => 1 );
21  
22 0 ▶sub _build_mysql{
Subroutine _build_mysql: not called CC=3 SCAR=24.8
23 0 my ($self) = @_;
24 0 my $mysql = File::Which::which('mysql');
25 0 ▶ unless( $mysql ){ die "Cannot find 'mysql' in path ".$ENV{PATH}.". Set this in the plan."; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($mysql) 0 0 0
26 0 ▶ unless( -x $mysql ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-x $mysql) 0 0 0
27 0 die "Found '$mysql' but it is not executable\n";
28 }
29 0 $log->info("Found mysql client at '$mysql'");
30 0 return $mysql;
31 }
32  
33 =head2 apply_sql
34  
35 Specificaly apply sql to mysql by using the command line client.
36  
37 See Superclass.
38  
39 =cut
40  
41 0 ▶sub apply_sql{
Subroutine apply_sql: not called CC=13 SCAR=52.0
Pod documented
42 0 my ($self, $sql) = @_;
43 0 my $mysql = $self->mysql();
44  
45 0 my @cmd = ( $mysql );
46  
47 # Time to build the command according to the dsn properties
48 0 my $properties = {};
49 {
50 0 ▶ eval "require DBD::mysql" or die "Please install DBD::mysql for this to work\n";
Branch: 1 branch
BranchTrueFalseTotal
eval "require DBD::mysql" or die "Please install DBD::mysql for this to work\n" 0 0 0
51  
52 0 my ($scheme, $driver, $attr_string, $attr_hash, $driver_dsn) = DBI->parse_dsn( $self->jesp()->dsn() );
53 0 DBD::mysql->_OdbcParse( $driver_dsn , $properties , [] );
54 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
55 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
56 0 $log->trace('mysql properties: '.Dumper( $properties ));
57 }
58  
59 0 push @cmd , '-B'; # This is a batch command. We dont want interactive at all.
60  
61 0 ▶ if( my $user = $properties->{user} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $user = $properties->{"user"}) 0 0 0
62 0 push @cmd , ( '-u' , String::ShellQuote::shell_quote( $user ));
63 }
64 0 ▶ if( my $database = $properties->{database} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $database = $properties->{"database"}) 0 0 0
65 0 push @cmd , ( '-D' , String::ShellQuote::shell_quote( $database ));
66 }
67 0 ▶ if( my $host = $properties->{host} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = $properties->{"host"}) 0 0 0
68 0 push @cmd , ( '-h' , String::ShellQuote::shell_quote( $host ));
69 }
70 0 ▶ if( my $port = $properties->{port} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $port = $properties->{"port"}) 0 0 0
71 0 push @cmd , ( '-P' , String::ShellQuote::shell_quote( $port ));
72 }
73 0 ▶ if( my $mysql_socket = $properties->{mysql_socket} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $mysql_socket = $properties->{"mysql_socket"}) 0 0 0
74 0 push @cmd , ( '-S' , String::ShellQuote::shell_quote( $mysql_socket ));
75 }
76 0 ▶ if( my $password = $properties->{password} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $password = $properties->{"password"}) 0 0 0
77 0 push @cmd , ( '-p'.String::ShellQuote::shell_quote( $password ));
78 }
79  
80  
81 0 ▶ my $on_stdout = sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 $log->info( @_ );
83 0 };
84 0 my @stderr;
85 0 ▶ my $on_stderr = sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 0 $log->warn( @_ );
87 0 push @stderr , @_;
88 0 };
89  
90 # Outside testing, be verbose.
91 0 ▶ local $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
92 0 ▶ IPC::Run::run( \@cmd, \$sql , $on_stdout , $on_stderr ) or die join(' ', @cmd).": $? : ".join("\n", @stderr )."\n";
Branch: 1 branch
BranchTrueFalseTotal
IPC::Run::run(\@cmd, \$sql, $on_stdout, $on_stderr) or die join(" ", @cmd) . ": $? : " . join("\n", @stderr) . "\n" 0 0 0
93 0 $log->info("Done");
94 }
95  
96 1 __PACKAGE__->meta->make_immutable();
97 1 1;
↑ Summary JESP/Driver.pm »