| 1 | package Beam::Make::DBI::CSV; | ||||||||||
| 2 | 2 | our $VERSION = '0.003'; | |||||||||
| 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 | 2 | ▶ | use v5.20; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 2 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 2 | ▶ | use Time::Piece; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 2 | ▶ | use Text::CSV; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 2 | ▶ | use Digest::SHA qw( sha1_base64 ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 2 | ▶ | use experimental qw( signatures postderef ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 2 | ▶ | use Log::Any qw( $LOG ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | |||||||||||
| 42 | 2 | 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 | 2 | 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 | 2 | 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 | 2 | 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 | 1 | ▶ | has csv => ( is => 'ro', default => sub { Text::CSV->new } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 97 | |||||||||||
| 98 | 1 | ▶ | sub make( $self, %vars ) { | ||||||||
|
Subroutine
make: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 99 | 1 | my $dbh = $self->dbh; | |||||||||
| 100 | 1 | open my $fh, '<', $self->file; | |||||||||
| 101 | 1 | my $csv = $self->csv; | |||||||||
| 102 | 1 | 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 | join( ', ', map { $dbh->quote_identifier( $_ ) } @fields ), | |||||||||
| 107 | join( ', ', ('?')x@fields ), | ||||||||||
| 108 | ); | ||||||||||
| 109 | 1 | ▶ | while ( my $row = $csv->getline( $fh ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 2 | $sth->execute( @$row ); | |||||||||
| 111 | } | ||||||||||
| 112 | 1 | $self->cache->set( $self->name, $self->_cache_hash ); | |||||||||
| 113 | 1 | return 0; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 4 | ▶ | sub _cache_hash( $self ) { | ||||||||
|
Subroutine
_cache_hash: called CC=1 SCAR=0.0
| |||||||||||
| 117 | my $content = join ';', | ||||||||||
| 118 | 4 | map { join ',', @$_ } | |||||||||
| 119 | $self->dbh->selectall_arrayref( 'SELECT * FROM ' . $self->table )->@*; | ||||||||||
| 120 | 4 | return sha1_base64( $content ); | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 3 | ▶ | sub last_modified( $self ) { | ||||||||
|
Subroutine
last_modified: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 124 | 3 | return $self->cache->last_modified( $self->name, $self->_cache_hash ); | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | 2 | 1; | |||||||||
| 128 | |||||||||||
| 129 | __END__ | ||||||||||