blib/lib/App/DBI/Loader.pm

filter: Statementstmt 100% 59 / 59 Branchbran 71.4% 20 / 28 Conditioncond 64.2% 9 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 83.9% 99 / 118 CC 21 SCAR 28.6
CC
21
Cov
87%
run
23.0
BEGIN
1.0
BEGIN
1.0
CRAP 17.5
↑ Summary
1 package App::DBI::Loader;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: A tiny script to load CSV/TSV contents into a database table via DBI
7 8 our $VERSION = 'v0.0.2'; # VERSION
8  
9 8 ▶use Getopt::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Getopt::Config::FromPod;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use String::Unescape;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub run
17 9 ▶{
Subroutine run: called CC=21 SCAR=31.4
Pod documented
18 9 ▶ shift if @_ && eval { $_[0]->isa(__PACKAGE__) };
Branch: 1 branch
BranchTrueFalseTotal
if @_ and eval { do { $_[0]->isa(__PACKAGE__) } } 9 0 9
Condition33%1/3
@_ and eval { do { $_[0]->isa(__PACKAGE__) } }
!ll&&!rl&&r
009
MC/DC0%0/2
@_ and eval { do { $_[0]->isa(__PACKAGE__) } }
@_ eval { do { $_[0]->isa(__PACKAGE__) } }
Truth table33%1/3
A and B
A= @_B= eval { do { $_[0]->isa(__PACKAGE__) } }
ABresult
0X0
100
111
19 9 local (@ARGV) = @_;
20  
21 9 my %opts;
22 9 getopts(Getopt::Config::FromPod->string, \%opts);
23 9 ▶ pod2usage(-verbose => 2) if exists $opts{h};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"h"} 0 9 9
24 9 ▶ pod2usage(-msg => 'At least 2 arguments MUST be specified', -verbose => 0, -exitval => 1) if @ARGV < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV < 2 0 9 9
25 9 ▶ push @ARGV, '-' if @ARGV == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV == 2 1 8 9
26  
27 9 ▶ $opts{t} ||= '';
Condition100%2/2
$opts{"t"} ||= ""
l!l
72
MC/DC100%1/1
$opts{"t"} ||= ""
$opts{"t"}
Truth table100%2/2
A
A= $opts{"t"}
Aresult
11
00
28 9 ▶ my $sep = String::Unescape->unescape($opts{t}) || ',';
Condition100%2/2
'String::Unescape'->unescape($opts{'t'}) || ','
l!l
72
MC/DC100%1/1
'String::Unescape'->unescape($opts{'t'}) || ','
'String::Unescape'->unescape($opts{'t'})
Truth table100%2/2
A
A= 'String::Unescape'->unescape($opts{'t'})
Aresult
11
00
29  
30 9 my $dbstr = shift @ARGV;
31 9 my $table = shift @ARGV;
32  
33 9 ▶ my $dbh = DBI->connect($dbstr, $opts{u} || '', $opts{p} || '') or die;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect($dbstr, $opts{"u"} || "", $opts{"p"} || "") or die 9 0 9
Condition50%2/4
$opts{'u'} || ''
l!l
09
$opts{'p'} || ''
l!l
09
MC/DC0%0/1
$opts{'u'} || ''
$opts{'u'}
MC/DC0%0/1
$opts{'p'} || ''
$opts{'p'}
Truth table50%1/2
A
A= $opts{'u'}
Aresult
11
00
Truth table50%1/2
A
A= $opts{'p'}
Aresult
11
00
34 9 my $has_transaction = 1;
35 9 eval { $dbh->{AutoCommit} = 0 };
36 9 ▶ $has_transaction = 0 if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 9 0 9
37 9 ▶ if($ARGV[0] =~ /\(.*\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[0] =~ /\(.*\)/) 2 7 9
38 2 my $schema = shift @ARGV;
39 2 $dbh->do("DROP TABLE IF EXISTS $table");
40 2 $dbh->do("CREATE TABLE $table $schema");
41 }
42 9 ▶ if(exists $opts{c}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"c"}) 5 4 9
43 5 $dbh->do("DELETE FROM $table");
44 }
45 9 my $sth;
46  
47 9 ▶ while(my $file = shift @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = shift @ARGV) 13 9 22
48 13 my $fh;
49 13 ▶ if($file eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '-') { } 6 7 13
50 6 $fh = \*STDIN;
51 } else {
52 7 ▶ open $fh, '<', $file or die;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or die 7 0 7
53 }
54 13 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 41 13 54
55 41 s/[\r\n]+$//;
56 41 ▶ my (@t) = $sep ? split /$sep/ : $_;
Branch: 1 branch
BranchTrueFalseTotal
$sep ? : 41 0 41
57 41 ▶ $sth ||= $dbh->prepare('INSERT INTO '.$table.' VALUES ('.join(',', ('?')x @t).')');
Condition66%2/3
$sth ||= $dbh->prepare('INSERT INTO ' . $table . ' VALUES (' . join(",", ("?") x @t) . ')')
l!l&&r!l&&!r
3290
MC/DC0%0/2
$sth ||= $dbh->prepare('INSERT INTO ' . $table . ' VALUES (' . join(",", ("?") x @t) . ')')
$sth $dbh->prepare('INSERT INTO ' . $table . ' VALUES (' . join(",", ("?") x @t) . ')')
Truth table66%2/3
A ||= B
A= $sthB= $dbh->prepare('INSERT INTO ' . $table . ' VALUES (' . join(",", ("?") x @t) . ')')
ABresult
1X1
011
000
58 41 $sth->execute(@t);
59 }
60 13 close $fh;
61 }
62 9 ▶ $dbh->commit if $has_transaction;
Branch: 1 branch
BranchTrueFalseTotal
if $has_transaction 0 9 9
63 }
64  
65 8 1;
66  
67 __END__
↑ Summary