| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Beam::Make::DBI::CSV; |
|
2
|
|
|
|
|
|
|
our $VERSION = '0.002'; |
|
3
|
|
|
|
|
|
|
# ABSTRACT: A Beam::Make recipe |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
#pod =head1 SYNOPSIS |
|
6
|
|
|
|
|
|
|
#pod |
|
7
|
|
|
|
|
|
|
#pod ### container.yml |
|
8
|
|
|
|
|
|
|
#pod # A Beam::Wire container to configure a database connection to use |
|
9
|
|
|
|
|
|
|
#pod sqlite: |
|
10
|
|
|
|
|
|
|
#pod $class: DBI |
|
11
|
|
|
|
|
|
|
#pod $method: connect |
|
12
|
|
|
|
|
|
|
#pod $args: |
|
13
|
|
|
|
|
|
|
#pod - dbi:SQLite:conversion.db |
|
14
|
|
|
|
|
|
|
#pod |
|
15
|
|
|
|
|
|
|
#pod ### Beamfile |
|
16
|
|
|
|
|
|
|
#pod load_data: |
|
17
|
|
|
|
|
|
|
#pod $class: Beam::Wire::DBI |
|
18
|
|
|
|
|
|
|
#pod dbh: { $ref: 'container.yml:sqlite' } |
|
19
|
|
|
|
|
|
|
#pod table: cpan_recent |
|
20
|
|
|
|
|
|
|
#pod file: cpan_recent.csv |
|
21
|
|
|
|
|
|
|
#pod |
|
22
|
|
|
|
|
|
|
#pod =head1 DESCRIPTION |
|
23
|
|
|
|
|
|
|
#pod |
|
24
|
|
|
|
|
|
|
#pod This L<Beam::Make> recipe class loads data into a database from a CSV file. |
|
25
|
|
|
|
|
|
|
#pod |
|
26
|
|
|
|
|
|
|
#pod =head1 SEE ALSO |
|
27
|
|
|
|
|
|
|
#pod |
|
28
|
|
|
|
|
|
|
#pod L<Beam::Make>, L<Beam::Wire>, L<DBI> |
|
29
|
|
|
|
|
|
|
#pod |
|
30
|
|
|
|
|
|
|
#pod =cut |
|
31
|
|
|
|
|
|
|
|
|
32
|
1
|
|
|
1
|
|
20
|
use v5.20; |
|
|
1
|
|
|
|
|
4
|
|
|
33
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
37
|
|
|
34
|
1
|
|
|
1
|
|
525
|
use autodie; |
|
|
1
|
|
|
|
|
14223
|
|
|
|
1
|
|
|
|
|
4
|
|
|
35
|
1
|
|
|
1
|
|
7051
|
use Moo; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
9
|
|
|
36
|
1
|
|
|
1
|
|
780
|
use Time::Piece; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
11
|
|
|
37
|
1
|
|
|
1
|
|
1376
|
use Text::CSV; |
|
|
1
|
|
|
|
|
14661
|
|
|
|
1
|
|
|
|
|
49
|
|
|
38
|
1
|
|
|
1
|
|
8
|
use Digest::SHA qw( sha1_base64 ); |
|
|
1
|
|
|
|
|
5
|
|
|
|
1
|
|
|
|
|
53
|
|
|
39
|
1
|
|
|
1
|
|
7
|
use experimental qw( signatures postderef ); |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
10
|
|
|
40
|
1
|
|
|
1
|
|
188
|
use Log::Any qw( $LOG ); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
11
|
|
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
extends 'Beam::Make::Recipe'; |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
#pod =attr dbh |
|
45
|
|
|
|
|
|
|
#pod |
|
46
|
|
|
|
|
|
|
#pod Required. The L<DBI> database handle to use. Can be a reference to a service |
|
47
|
|
|
|
|
|
|
#pod in a L<Beam::Wire> container using C<< { $ref: "<container>:<service>" } >>. |
|
48
|
|
|
|
|
|
|
#pod |
|
49
|
|
|
|
|
|
|
#pod =cut |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
has dbh => ( is => 'ro', required => 1 ); |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
#pod =attr table |
|
54
|
|
|
|
|
|
|
#pod |
|
55
|
|
|
|
|
|
|
#pod Required. The table to load data to. |
|
56
|
|
|
|
|
|
|
#pod |
|
57
|
|
|
|
|
|
|
#pod =cut |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
has table => ( is => 'ro', required => 1 ); |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
#pod =attr file |
|
62
|
|
|
|
|
|
|
#pod |
|
63
|
|
|
|
|
|
|
#pod Required. The path to the CSV file to load. |
|
64
|
|
|
|
|
|
|
#pod |
|
65
|
|
|
|
|
|
|
#pod =cut |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
has file => ( is => 'ro', required => 1 ); |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
#pod =attr csv |
|
70
|
|
|
|
|
|
|
#pod |
|
71
|
|
|
|
|
|
|
#pod The configured L<Text::CSV> object to use. Can be a reference to a service |
|
72
|
|
|
|
|
|
|
#pod in a L<Beam::Wire> container using C<< { $ref: "<container>:<service>" } >>. |
|
73
|
|
|
|
|
|
|
#pod Defaults to a new, blank C<Text::CSV> object. |
|
74
|
|
|
|
|
|
|
#pod |
|
75
|
|
|
|
|
|
|
#pod ### container.yml |
|
76
|
|
|
|
|
|
|
#pod # Configure a CSV parser for pipe-separated values |
|
77
|
|
|
|
|
|
|
#pod psv: |
|
78
|
|
|
|
|
|
|
#pod $class: Text::CSV |
|
79
|
|
|
|
|
|
|
#pod $args: |
|
80
|
|
|
|
|
|
|
#pod - binary: 1 |
|
81
|
|
|
|
|
|
|
#pod sep_char: '|' |
|
82
|
|
|
|
|
|
|
#pod quote_char: ~ |
|
83
|
|
|
|
|
|
|
#pod escape_char: ~ |
|
84
|
|
|
|
|
|
|
#pod |
|
85
|
|
|
|
|
|
|
#pod ### Beamfile |
|
86
|
|
|
|
|
|
|
#pod # Load a PSV into the database |
|
87
|
|
|
|
|
|
|
#pod load_psv: |
|
88
|
|
|
|
|
|
|
#pod $class: Beam::Make::DBI::CSV |
|
89
|
|
|
|
|
|
|
#pod dbh: { $ref: 'container.yml:sqlite' } |
|
90
|
|
|
|
|
|
|
#pod csv: { $ref: 'container.yml:psv' } |
|
91
|
|
|
|
|
|
|
#pod file: accounts.psv |
|
92
|
|
|
|
|
|
|
#pod table: accounts |
|
93
|
|
|
|
|
|
|
#pod |
|
94
|
|
|
|
|
|
|
#pod =cut |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
has csv => ( is => 'ro', default => sub { Text::CSV->new } ); |
|
97
|
|
|
|
|
|
|
|
|
98
|
1
|
|
|
1
|
1
|
2
|
sub make( $self, %vars ) { |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
5
|
|
|
|
1
|
|
|
|
|
2
|
|
|
99
|
1
|
|
|
|
|
5
|
my $dbh = $self->dbh; |
|
100
|
1
|
|
|
|
|
10
|
open my $fh, '<', $self->file; |
|
101
|
1
|
|
|
|
|
2235
|
my $csv = $self->csv; |
|
102
|
1
|
|
|
|
|
70
|
my @fields = $csv->getline( $fh )->@*; |
|
103
|
|
|
|
|
|
|
my $sth = $dbh->prepare( |
|
104
|
|
|
|
|
|
|
sprintf 'INSERT INTO %s ( %s ) VALUES ( %s )', |
|
105
|
|
|
|
|
|
|
$dbh->quote_identifier( $self->table ), |
|
106
|
1
|
|
|
|
|
85
|
join( ', ', map { $dbh->quote_identifier( $_ ) } @fields ), |
|
|
2
|
|
|
|
|
66
|
|
|
107
|
|
|
|
|
|
|
join( ', ', ('?')x@fields ), |
|
108
|
|
|
|
|
|
|
); |
|
109
|
1
|
|
|
|
|
138
|
while ( my $row = $csv->getline( $fh ) ) { |
|
110
|
2
|
|
|
|
|
20504
|
$sth->execute( @$row ); |
|
111
|
|
|
|
|
|
|
} |
|
112
|
1
|
|
|
|
|
61
|
$self->cache->set( $self->name, $self->_cache_hash ); |
|
113
|
1
|
|
|
|
|
34
|
return 0; |
|
114
|
|
|
|
|
|
|
} |
|
115
|
|
|
|
|
|
|
|
|
116
|
4
|
|
|
4
|
|
9
|
sub _cache_hash( $self ) { |
|
|
4
|
|
|
|
|
9
|
|
|
|
4
|
|
|
|
|
5
|
|
|
117
|
|
|
|
|
|
|
my $content = join ';', |
|
118
|
4
|
|
|
|
|
56
|
map { join ',', @$_ } |
|
|
6
|
|
|
|
|
496
|
|
|
119
|
|
|
|
|
|
|
$self->dbh->selectall_arrayref( 'SELECT * FROM ' . $self->table )->@*; |
|
120
|
4
|
|
|
|
|
225
|
return sha1_base64( $content ); |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
3
|
|
|
3
|
1
|
6
|
sub last_modified( $self ) { |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
6
|
|
|
124
|
3
|
|
|
|
|
30
|
return $self->cache->last_modified( $self->name, $self->_cache_hash ); |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
1; |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
__END__ |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=pod |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 NAME |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Beam::Make::DBI::CSV - A Beam::Make recipe |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 VERSION |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
version 0.002 |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
### container.yml |
|
144
|
|
|
|
|
|
|
# A Beam::Wire container to configure a database connection to use |
|
145
|
|
|
|
|
|
|
sqlite: |
|
146
|
|
|
|
|
|
|
$class: DBI |
|
147
|
|
|
|
|
|
|
$method: connect |
|
148
|
|
|
|
|
|
|
$args: |
|
149
|
|
|
|
|
|
|
- dbi:SQLite:conversion.db |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
### Beamfile |
|
152
|
|
|
|
|
|
|
load_data: |
|
153
|
|
|
|
|
|
|
$class: Beam::Wire::DBI |
|
154
|
|
|
|
|
|
|
dbh: { $ref: 'container.yml:sqlite' } |
|
155
|
|
|
|
|
|
|
table: cpan_recent |
|
156
|
|
|
|
|
|
|
file: cpan_recent.csv |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
This L<Beam::Make> recipe class loads data into a database from a CSV file. |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head2 dbh |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Required. The L<DBI> database handle to use. Can be a reference to a service |
|
167
|
|
|
|
|
|
|
in a L<Beam::Wire> container using C<< { $ref: "<container>:<service>" } >>. |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head2 table |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Required. The table to load data to. |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head2 file |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Required. The path to the CSV file to load. |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head2 csv |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
The configured L<Text::CSV> object to use. Can be a reference to a service |
|
180
|
|
|
|
|
|
|
in a L<Beam::Wire> container using C<< { $ref: "<container>:<service>" } >>. |
|
181
|
|
|
|
|
|
|
Defaults to a new, blank C<Text::CSV> object. |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
### container.yml |
|
184
|
|
|
|
|
|
|
# Configure a CSV parser for pipe-separated values |
|
185
|
|
|
|
|
|
|
psv: |
|
186
|
|
|
|
|
|
|
$class: Text::CSV |
|
187
|
|
|
|
|
|
|
$args: |
|
188
|
|
|
|
|
|
|
- binary: 1 |
|
189
|
|
|
|
|
|
|
sep_char: '|' |
|
190
|
|
|
|
|
|
|
quote_char: ~ |
|
191
|
|
|
|
|
|
|
escape_char: ~ |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
### Beamfile |
|
194
|
|
|
|
|
|
|
# Load a PSV into the database |
|
195
|
|
|
|
|
|
|
load_psv: |
|
196
|
|
|
|
|
|
|
$class: Beam::Make::DBI::CSV |
|
197
|
|
|
|
|
|
|
dbh: { $ref: 'container.yml:sqlite' } |
|
198
|
|
|
|
|
|
|
csv: { $ref: 'container.yml:psv' } |
|
199
|
|
|
|
|
|
|
file: accounts.psv |
|
200
|
|
|
|
|
|
|
table: accounts |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
L<Beam::Make>, L<Beam::Wire>, L<DBI> |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 AUTHOR |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Doug Bell <preaction@cpan.org> |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
This software is copyright (c) 2020 by Doug Bell. |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
215
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=cut |