File Coverage

blib/lib/App/JESP/Driver/mysql.pm
Criterion Covered Total %
statement 21 62 33.8
branch 0 22 0.0
condition 0 9 0.0
subroutine 7 11 63.6
pod 1 1 100.0
total 29 105 27.6


line stmt bran cond sub pod time code
1             package App::JESP::Driver::mysql;
2             $App::JESP::Driver::mysql::VERSION = '0.014';
3 1     1   1624 use Moose;
  1         3  
  1         5  
4             extends qw/App::JESP::Driver/;
5              
6 1     1   6080 use File::Which qw//;
  1         802  
  1         19  
7 1     1   6 use IPC::Run qw//;
  1         2  
  1         16  
8 1     1   4 use Log::Any qw/$log/;
  1         2  
  1         7  
9              
10 1     1   218 use Data::Dumper;
  1         2  
  1         64  
11 1     1   11 use DBI;
  1         2  
  1         29  
12 1     1   272 use String::ShellQuote;
  1         624  
  1         512  
13              
14             =head1 NAME
15              
16             App::JESP::Driver::mysql - mysql driver. Subclasses App::JESP::Driver
17              
18             =cut
19              
20             has 'mysql' => ( is => 'ro', isa => 'Str', lazy_build => 1 );
21              
22             sub _build_mysql{
23 0     0     my ($self) = @_;
24 0           my $mysql = File::Which::which('mysql');
25 0 0         unless( $mysql ){ die "Cannot find 'mysql' in path ".$ENV{PATH}.". Set this in the plan."; }
  0            
26 0 0         unless( -x $mysql ){
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             sub apply_sql{
42 0     0 1   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 0         eval "require DBD::mysql" or die "Please install DBD::mysql for this to work\n";
  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   0       $properties->{user} ||= $self->jesp()->username();
55 0   0       $properties->{password} ||= $self->jesp()->password();
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 0         if( my $user = $properties->{user} ){
62 0           push @cmd , ( '-u' , String::ShellQuote::shell_quote( $user ));
63             }
64 0 0         if( my $database = $properties->{database} ){
65 0           push @cmd , ( '-D' , String::ShellQuote::shell_quote( $database ));
66             }
67 0 0         if( my $host = $properties->{host} ){
68 0           push @cmd , ( '-h' , String::ShellQuote::shell_quote( $host ));
69             }
70 0 0         if( my $port = $properties->{port} ){
71 0           push @cmd , ( '-P' , String::ShellQuote::shell_quote( $port ));
72             }
73 0 0         if( my $mysql_socket = $properties->{mysql_socket} ){
74 0           push @cmd , ( '-S' , String::ShellQuote::shell_quote( $mysql_socket ));
75             }
76 0 0         if( my $password = $properties->{password} ){
77 0           push @cmd , ( '-p'.String::ShellQuote::shell_quote( $password ));
78             }
79              
80              
81             my $on_stdout = sub{
82 0     0     $log->info( @_ );
83 0           };
84 0           my @stderr;
85             my $on_stderr = sub{
86 0     0     $log->warn( @_ );
87 0           push @stderr , @_;
88 0           };
89              
90             # Outside testing, be verbose.
91 0 0 0       local $ENV{IPCRUNDEBUG} = 'basic' unless( $ENV{AUTOMATED_TESTING} || $ENV{HARNESS_ACTIVE} );
92 0 0         IPC::Run::run( \@cmd, \$sql , $on_stdout , $on_stderr ) or die join(' ', @cmd).": $? : ".join("\n", @stderr )."\n";
93 0           $log->info("Done");
94             }
95              
96             __PACKAGE__->meta->make_immutable();
97             1;