| 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
Condition33%1/3
@_ and eval {
do {
$_[0]->isa(__PACKAGE__)
}
}
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__)
}
}
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 24 | 9 | ▶ | pod2usage(-msg => 'At least 2 arguments MUST be specified', -verbose => 0, -exitval => 1) if @ARGV < 2; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 25 | 9 | ▶ | push @ARGV, '-' if @ARGV == 2; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||
| 27 | 9 | ▶ | $opts{t} ||= ''; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"t"} ||= ""
MC/DC100%1/1
$opts{"t"} ||= ""
$opts{"t"}
Truth table100%2/2
A
A= $opts{"t"}
| |||||||||||||||||||||||||||||||
| 28 | 9 | ▶ | my $sep = String::Unescape->unescape($opts{t}) || ','; | ||||||||||||||||||||||||||||
|
Condition100%2/2
'String::Unescape'->unescape($opts{'t'}) || ','
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'})
| |||||||||||||||||||||||||||||||
| 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
Condition50%2/4
$opts{'u'} || ''
$opts{'p'} || ''
MC/DC0%0/1
$opts{'u'} || ''
$opts{'u'}
MC/DC0%0/1
$opts{'p'} || ''
$opts{'p'}
Truth table50%1/2
A
A= $opts{'u'}
Truth table50%1/2
A
A= $opts{'p'}
| |||||||||||||||||||||||||||||||
| 34 | 9 | my $has_transaction = 1; | |||||||||||||||||||||||||||||
| 35 | 9 | eval { $dbh->{AutoCommit} = 0 }; | |||||||||||||||||||||||||||||
| 36 | 9 | ▶ | $has_transaction = 0 if $@; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 37 | 9 | ▶ | if($ARGV[0] =~ /\(.*\)/) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 43 | 5 | $dbh->do("DELETE FROM $table"); | |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||
| 45 | 9 | my $sth; | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | 9 | ▶ | while(my $file = shift @ARGV) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 48 | 13 | my $fh; | |||||||||||||||||||||||||||||
| 49 | 13 | ▶ | if($file eq '-') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 50 | 6 | $fh = \*STDIN; | |||||||||||||||||||||||||||||
| 51 | } else { | ||||||||||||||||||||||||||||||
| 52 | 7 | ▶ | open $fh, '<', $file or die; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||
| 54 | 13 | ▶ | while(<$fh>) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 55 | 41 | s/[\r\n]+$//; | |||||||||||||||||||||||||||||
| 56 | 41 | ▶ | my (@t) = $sep ? split /$sep/ : $_; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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) . ')')
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) . ')')
| |||||||||||||||||||||||||||||||
| 58 | 41 | $sth->execute(@t); | |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||
| 60 | 13 | close $fh; | |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||
| 62 | 9 | ▶ | $dbh->commit if $has_transaction; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||
| 65 | 8 | 1; | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||
| 67 | __END__ | ||||||||||||||||||||||||||||||