line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::CSVDumper; |
2
|
2
|
|
|
2
|
|
161361
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
77
|
|
3
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
54
|
|
4
|
2
|
|
|
2
|
|
1132
|
use utf8; |
|
2
|
|
|
|
|
17
|
|
|
2
|
|
|
|
|
12
|
|
5
|
2
|
|
|
2
|
|
2735
|
use Encode; |
|
2
|
|
|
|
|
25573
|
|
|
2
|
|
|
|
|
273
|
|
6
|
2
|
|
|
2
|
|
1918
|
use Text::CSV; |
|
2
|
|
|
|
|
39911
|
|
|
2
|
|
|
|
|
16
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our %DEFAULT_CSV_ARGS = ( |
11
|
|
|
|
|
|
|
binary => 1, |
12
|
|
|
|
|
|
|
always_quote => 1, |
13
|
|
|
|
|
|
|
eol => "\r\n", |
14
|
|
|
|
|
|
|
); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub new { |
17
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
18
|
0
|
0
|
|
|
|
|
my %args = @_ == 1 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
|
|
19
|
0
|
|
|
|
|
|
bless {%args}, $class; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub csv_obj { |
23
|
0
|
|
|
0
|
1
|
|
my ($self, $obj) = @_; |
24
|
0
|
0
|
|
|
|
|
$self->{_csv_obj} = $obj if $obj; |
25
|
0
|
0
|
|
|
|
|
$self->{_csv_obj} ||= Text::CSV->new({ |
26
|
|
|
|
|
|
|
%DEFAULT_CSV_ARGS, |
27
|
0
|
|
0
|
|
|
|
%{ $self->{csv_args} || {} }, |
28
|
|
|
|
|
|
|
}); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub encoding { |
32
|
0
|
|
|
0
|
1
|
|
my ($self, $enc) = @_; |
33
|
0
|
0
|
|
|
|
|
$self->{_encoding} = Encode::find_encoding($enc) if $enc; |
34
|
0
|
|
0
|
|
|
|
$self->{_encoding} ||= Encode::find_encoding($self->{encoding} || 'utf-8'); |
|
|
|
0
|
|
|
|
|
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub dump { |
38
|
0
|
|
|
0
|
1
|
|
my ($self, %args) = @_; |
39
|
0
|
|
|
|
|
|
my $sth = $args{sth}; |
40
|
0
|
|
|
|
|
|
my $file = $args{file}; |
41
|
0
|
|
|
|
|
|
my $fh = $args{fh}; |
42
|
0
|
|
0
|
|
|
|
my $encoding = $args{encoding} || $self->encoding; |
43
|
|
|
|
|
|
|
|
44
|
0
|
0
|
0
|
|
|
|
$sth->execute if $DBI::VERSION >= 1.41 && !$sth->{Executed}; |
45
|
|
|
|
|
|
|
|
46
|
0
|
0
|
|
|
|
|
unless ($fh) { |
47
|
0
|
0
|
|
|
|
|
open $fh, '>', $file or die $!; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
my $csv = $self->csv_obj; |
51
|
0
|
|
|
|
|
|
my $cols = $sth->{NAME}; |
52
|
0
|
|
|
|
|
|
$csv->print($fh, $cols); |
53
|
0
|
|
|
|
|
|
while (my @data = $sth->fetchrow_array) { |
54
|
0
|
|
|
|
|
|
@data = map {encode($encoding => $_)} @data; |
|
0
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
$csv->print($fh, [@data]); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
1; |
60
|
|
|
|
|
|
|
__END__ |