line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::DBI::Loader; |
2
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
9266
|
use strict; |
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
344
|
|
4
|
7
|
|
|
7
|
|
55
|
use warnings; |
|
7
|
|
|
|
|
8
|
|
|
7
|
|
|
|
|
406
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: A tiny script to load CSV/TSV contents into a database table via DBI |
7
|
|
|
|
|
|
|
our $VERSION = 'v0.0.2'; # VERSION |
8
|
|
|
|
|
|
|
|
9
|
7
|
|
|
7
|
|
21981
|
use Getopt::Std; |
|
7
|
|
|
|
|
458
|
|
|
7
|
|
|
|
|
556
|
|
10
|
7
|
|
|
7
|
|
49
|
use Getopt::Config::FromPod; |
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
179
|
|
11
|
7
|
|
|
7
|
|
10142
|
use Pod::Usage; |
|
7
|
|
|
|
|
196874
|
|
|
7
|
|
|
|
|
1283
|
|
12
|
|
|
|
|
|
|
|
13
|
7
|
|
|
7
|
|
98
|
use DBI; |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
1254
|
|
14
|
7
|
|
|
7
|
|
8246
|
use String::Unescape; |
|
7
|
|
|
|
|
16403
|
|
|
7
|
|
|
|
|
5341
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub run |
17
|
|
|
|
|
|
|
{ |
18
|
9
|
50
|
33
|
9
|
1
|
12169121
|
shift if @_ && eval { $_[0]->isa(__PACKAGE__) }; |
|
9
|
|
|
|
|
1175
|
|
19
|
9
|
|
|
|
|
279
|
local (@ARGV) = @_; |
20
|
|
|
|
|
|
|
|
21
|
9
|
|
|
|
|
83
|
my %opts; |
22
|
9
|
|
|
|
|
356
|
getopts(Getopt::Config::FromPod->string, \%opts); |
23
|
9
|
50
|
|
|
|
304256
|
pod2usage(-verbose => 2) if exists $opts{h}; |
24
|
9
|
50
|
|
|
|
112
|
pod2usage(-msg => 'At least 2 arguments MUST be specified', -verbose => 0, -exitval => 1) if @ARGV < 2; |
25
|
9
|
100
|
|
|
|
88
|
push @ARGV, '-' if @ARGV == 2; |
26
|
|
|
|
|
|
|
|
27
|
9
|
|
100
|
|
|
107
|
$opts{t} ||= ''; |
28
|
9
|
|
100
|
|
|
349
|
my $sep = String::Unescape->unescape($opts{t}) || ','; |
29
|
|
|
|
|
|
|
|
30
|
9
|
|
|
|
|
6614
|
my $dbstr = shift @ARGV; |
31
|
9
|
|
|
|
|
36
|
my $table = shift @ARGV; |
32
|
|
|
|
|
|
|
|
33
|
9
|
50
|
50
|
|
|
1724
|
my $dbh = DBI->connect($dbstr, $opts{u} || '', $opts{p} || '') or die; |
|
|
|
50
|
|
|
|
|
34
|
9
|
|
|
|
|
557408
|
my $has_transaction = 1; |
35
|
9
|
|
|
|
|
69
|
eval { $dbh->{AutoCommit} = 0 }; |
|
9
|
|
|
|
|
67
|
|
36
|
9
|
50
|
|
|
|
21614
|
$has_transaction = 0 if $@; |
37
|
9
|
100
|
|
|
|
66
|
if($ARGV[0] =~ /\(.*\)/) { |
38
|
2
|
|
|
|
|
6
|
my $schema = shift @ARGV; |
39
|
2
|
|
|
|
|
48
|
$dbh->do("DROP TABLE IF EXISTS $table"); |
40
|
2
|
|
|
|
|
11307
|
$dbh->do("CREATE TABLE $table $schema"); |
41
|
|
|
|
|
|
|
} |
42
|
9
|
100
|
|
|
|
2758
|
if(exists $opts{c}) { |
43
|
5
|
|
|
|
|
269
|
$dbh->do("DELETE FROM $table"); |
44
|
|
|
|
|
|
|
} |
45
|
9
|
|
|
|
|
35375
|
my $sth; |
46
|
|
|
|
|
|
|
|
47
|
9
|
|
|
|
|
76
|
while(my $file = shift @ARGV) { |
48
|
13
|
|
|
|
|
27
|
my $fh; |
49
|
13
|
100
|
|
|
|
90
|
if($file eq '-') { |
50
|
6
|
|
|
|
|
37
|
$fh = \*STDIN; |
51
|
|
|
|
|
|
|
} else { |
52
|
7
|
50
|
|
|
|
493
|
open $fh, '<', $file or die; |
53
|
|
|
|
|
|
|
} |
54
|
13
|
|
|
|
|
544
|
while(<$fh>) { |
55
|
41
|
|
|
|
|
55093
|
s/[\r\n]+$//; |
56
|
41
|
50
|
|
|
|
695
|
my (@t) = $sep ? split /$sep/ : $_; |
57
|
41
|
|
66
|
|
|
342
|
$sth ||= $dbh->prepare('INSERT INTO '.$table.' VALUES ('.join(',', ('?')x @t).')'); |
58
|
41
|
|
|
|
|
17570
|
$sth->execute(@t); |
59
|
|
|
|
|
|
|
} |
60
|
13
|
|
|
|
|
18930
|
close $fh; |
61
|
|
|
|
|
|
|
} |
62
|
9
|
50
|
|
|
|
145
|
$dbh->commit if $has_transaction; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
1; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
__END__ |