| 1 | # vim: set ts=2 sts=2 sw=2 expandtab smarttab: | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # This file is part of DBIx-TableLoader-CSV | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # This software is copyright (c) 2011 by Randy Stauner. | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 8 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | package DBIx::TableLoader::CSV; | ||||||||||||||||||||||||||||
| 14 | { | ||||||||||||||||||||||||||||
| 15 | 5 | $DBIx::TableLoader::CSV::VERSION = '1.102'; | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | # git description: v1.101-5-g35f5bf3 | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 5 | $DBIx::TableLoader::CSV::AUTHORITY = 'cpan:RWSTAUNER'; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | # ABSTRACT: Easily load a CSV into a database table | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 5 | ▶ | use DBIx::TableLoader 1.100; # rollback after error | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 5 | our @ISA = 'DBIx::TableLoader'; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 5 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 5 | ▶ | use Module::Load (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 5 | ▶ | use Text::CSV 1.21 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # 'new' inherited | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 69 | ▶ | sub defaults { | ||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 69 | my ($self) = @_; | |||||||||||||||||||||||||||
| 36 | return { | ||||||||||||||||||||||||||||
| 37 | 69 | csv => undef, | |||||||||||||||||||||||||||
| 38 | csv_class => 'Text::CSV', | ||||||||||||||||||||||||||||
| 39 | csv_defaults => { | ||||||||||||||||||||||||||||
| 40 | # Text::CSV encourages setting { binary => 1 } | ||||||||||||||||||||||||||||
| 41 | binary => 1, | ||||||||||||||||||||||||||||
| 42 | }, | ||||||||||||||||||||||||||||
| 43 | csv_opts => {}, | ||||||||||||||||||||||||||||
| 44 | file => undef, | ||||||||||||||||||||||||||||
| 45 | file_encoding => '', | ||||||||||||||||||||||||||||
| 46 | file_open_layers => '', | ||||||||||||||||||||||||||||
| 47 | ignore_csv_errors => 0, | ||||||||||||||||||||||||||||
| 48 | io => undef, | ||||||||||||||||||||||||||||
| 49 | no_header => 0, | ||||||||||||||||||||||||||||
| 50 | }; | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 109 | ▶ | sub get_raw_row { | ||||||||||||||||||||||||||
|
Subroutine
get_raw_row: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 109 | my ($self) = @_; | |||||||||||||||||||||||||||
| 56 | 109 | my $row = $self->{csv}->getline($self->{io}); | |||||||||||||||||||||||||||
| 57 | 108 | ▶ | unless( $self->{ignore_csv_errors} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 105 | ▶ | if( !$row && !$self->{csv}->eof ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$row or $self->{"csv"}->eof
MC/DC100%2/2
$row or $self->{"csv"}->eof
$row
$self->{"csv"}->eof
Truth table100%3/3
A or B
A= $rowB= $self->{"csv"}->eof
| |||||||||||||||||||||||||||||
| 59 | 1 | croak 'CSV parse error: ' . $self->{csv}->error_diag; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 107 | return $row; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 18 | ▶ | sub default_name { | ||||||||||||||||||||||||||
|
Subroutine
default_name: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 18 | my ($self) = @_; | |||||||||||||||||||||||||||
| 68 | # guess name if not provided | ||||||||||||||||||||||||||||
| 69 | return $self->{name} ||= | ||||||||||||||||||||||||||||
| 70 | $self->{file} | ||||||||||||||||||||||||||||
| 71 | 18 | ▶ | ? do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"name"} ||= $self->{"file"} ? do {
require File::Basename;
File::Basename::fileparse($self->{"file"}, qr/\.[^.]*/)
} : "csv"
MC/DC0%0/2
$self->{"name"} ||= $self->{"file"} ? do {
require File::Basename;
File::Basename::fileparse($self->{"file"}, qr/\.[^.]*/)
} : "csv"
$self->{"name"}
$self->{"file"} ? do {
require File::Basename;
File::Basename::fileparse($self->{"file"}, qr/\.[^.]*/)
} : "csv"
Truth table33%1/3
A ||= B
A= $self->{"name"}B= $self->{"file"} ? do {
require File::Basename;
File::Basename::fileparse($self->{"file"}, qr/\.[^.]*/)
} : "csv"
| |||||||||||||||||||||||||||||
| 72 | 15 | require File::Basename; # core | |||||||||||||||||||||||||||
| 73 | 15 | File::Basename::fileparse($self->{file}, qr/\.[^.]*/); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | : 'csv'; | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 69 | ▶ | sub prepare_data { | ||||||||||||||||||||||||||
|
Subroutine
prepare_data: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 69 | my ($self) = @_; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 69 | Module::Load::load($self->{csv_class}); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # if an object is not passed in via 'csv', create one from 'csv_opts' | ||||||||||||||||||||||||||||
| 85 | $self->{csv} ||= $self->{csv_class}->new({ | ||||||||||||||||||||||||||||
| 86 | 65 | %{ $self->{csv_defaults} }, | |||||||||||||||||||||||||||
| 87 | 65 | %{ $self->{csv_opts} } | |||||||||||||||||||||||||||
| 88 | }) | ||||||||||||||||||||||||||||
| 89 | 69 | ▶ | or croak "Cannot use CSV: " . $self->{csv_class}->error_diag(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"csv"} ||= $self->{"csv_class"}->new({%{$self->{"csv_defaults"};}, %{$self->{"csv_opts"};}})
MC/DC100%2/2
$self->{"csv"} ||= $self->{"csv_class"}->new({%{$self->{"csv_defaults"};}, %{$self->{"csv_opts"};}})
$self->{"csv"}
$self->{"csv_class"}->new({%{$self->{"csv_defaults"};}, %{$self->{"csv_opts"};}})
Truth table100%3/3
A ||= B
A= $self->{"csv"}B= $self->{"csv_class"}->new({%{$self->{"csv_defaults"};}, %{$self->{"csv_opts"};}})
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # if 'io' not provided set it to the handle returned from opening 'file' | ||||||||||||||||||||||||||||
| 92 | 68 | ▶ | $self->{io} ||= $self->_open_file | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"io"} ||= $self->_open_file
MC/DC100%2/2
$self->{"io"} ||= $self->_open_file
$self->{"io"}
$self->_open_file
Truth table100%3/3
A ||= B
A= $self->{"io"}B= $self->_open_file
| |||||||||||||||||||||||||||||
| 93 | or croak("Cannot proceed without a 'file' or 'io' attribute"); | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # discard first row if columns given (see POD for 'no_header' option) | ||||||||||||||||||||||||||||
| 96 | $self->{first_row} = $self->get_raw_row() | ||||||||||||||||||||||||||||
| 97 | 62 | ▶ | if $self->{columns} && !$self->{no_header}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"columns"} and not $self->{"no_header"}
MC/DC100%2/2
$self->{"columns"} and not $self->{"no_header"}
$self->{"columns"}
not $self->{"no_header"}
Truth table100%3/3
A and not(B)
A= $self->{"columns"}B= not $self->{"no_header"}
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 23 | ▶ | sub _open_file { | ||||||||||||||||||||||||||
|
Subroutine
_open_file: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 101 | 23 | my ($self) = @_; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | return | ||||||||||||||||||||||||||||
| 104 | 23 | ▶ | unless my $file = $self->{file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 20 | my $mode = '<'; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 20 | ▶ | if( my $layers = $self->{file_open_layers} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 6 | $mode .= $layers; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # convenience shortcut (layers would be sufficient but this is easier) | ||||||||||||||||||||||||||||
| 113 | 20 | ▶ | if( my $enc = $self->{file_encoding} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 3 | $mode .= ':encoding(' . $enc . ')'; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 20 | ▶ | open(my $fh, $mode, $file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | or croak("Failed to open '$file': $!"); | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 17 | return $fh; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 5 | 1; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | __END__ | ||||||||||||||||||||||||||||