| 1 | package Database::Temp::Driver::CSV; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | # ABSTRACT: Create an ad-hoc database, driver for CSV | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.003'; # VERSION: generated by DZP::OurPkgVersion | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | use Module::Load::Conditional qw( can_load ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 1 | ▶ | use Carp qw( shortmess ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use File::Path qw( make_path remove_tree ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use File::Spec; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 1 | ▶ | use Log::Any; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 1 | ▶ | use Const::Fast; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 1 | const my $TEMP_PATH_CHMOD => 700; | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 1 | ▶ | sub is_available { | ||||||||||||||||||
|
Subroutine
is_available: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 23 | 1 | my $_log = Log::Any->get_logger( category => 'Database::Temp' ); | |||||||||||||||||||
| 24 | 1 | my %needed = ( 'DBD::CSV' => 0.57, ); | |||||||||||||||||||
| 25 | 1 | ▶ | if ( !can_load( modules => \%needed ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | 1 | $_log->infof( 'Cannot load module %s, %s', %needed ); | |||||||||||||||||||
| 27 | 1 | return 0; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | 0 | return 1; | |||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 33 | 0 | my ( $class, %params ) = @_; | |||||||||||||||||||
| 34 | 0 | my $_log = Log::Any->get_logger( category => 'Database::Temp' ); | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 0 | ▶ | my $dir = $params{'args'}->{'dir'} // File::Spec->tmpdir(); | ||||||||||||||||||
|
Condition0%0/3
$params{"args"}{"dir"} // "File::Spec"->tmpdir
MC/DC0%0/2
$params{"args"}{"dir"} // "File::Spec"->tmpdir
$params{"args"}{"dir"}
"File::Spec"->tmpdir
Truth table0%0/3
A // B
A= $params{"args"}{"dir"}B= "File::Spec"->tmpdir
| |||||||||||||||||||||
| 37 | 0 | my $db_dir = $params{'name'}; | |||||||||||||||||||
| 38 | 0 | my $dirpath = File::Spec->catfile( $dir, $db_dir ); | |||||||||||||||||||
| 39 | 0 | make_path( $dirpath, { verbose => 0, mode => oct($TEMP_PATH_CHMOD), } ); | |||||||||||||||||||
| 40 | 0 | my $dsn = 'dbi:CSV:'; | |||||||||||||||||||
| 41 | 0 | $_log->debugf( 'Created temp dirpath \'%s\'', $dirpath ); | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 0 | my %attrs = ( | |||||||||||||||||||
| 44 | 'AutoCommit' => 1, | ||||||||||||||||||||
| 45 | 'RaiseError' => 1, | ||||||||||||||||||||
| 46 | 'PrintError' => 1, | ||||||||||||||||||||
| 47 | 'RaiseWarn' => 1, | ||||||||||||||||||||
| 48 | 'PrintWarn' => 1, | ||||||||||||||||||||
| 49 | 'TaintIn' => 1, | ||||||||||||||||||||
| 50 | 'TaintOut' => 0, | ||||||||||||||||||||
| 51 | 'TraceLevel' => 0, | ||||||||||||||||||||
| 52 | 'f_dir' => $dirpath, | ||||||||||||||||||||
| 53 | 'f_ext' => '.csv/r', | ||||||||||||||||||||
| 54 | 'f_encoding' => 'utf8', | ||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||
| 56 | 0 | my %info = ( 'dirpath' => $dirpath, ); | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | # Construct start method | ||||||||||||||||||||
| 59 | 0 | ▶ | my $_start = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 60 | 0 | my ( $dbh, $name ) = @_; | |||||||||||||||||||
| 61 | 0 | Log::Any->get_logger( category => 'Database::Temp' )->debugf( 'Created temp db \'%s\'', $name ); | |||||||||||||||||||
| 62 | 0 | }; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | # Construct init method | ||||||||||||||||||||
| 65 | 0 | my $init; | |||||||||||||||||||
| 66 | 0 | ▶ | if ( ref $params{'init'} eq 'CODE' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 0 | $init = $params{'init'}; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | else { # SCALAR | ||||||||||||||||||||
| 70 | # Attn. CSV does not have transactions (begin_work, commit) | ||||||||||||||||||||
| 71 | 0 | ▶ | $init = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 72 | 0 | my ($dbh) = @_; | |||||||||||||||||||
| 73 | 0 | foreach my $row ( split qr/;\s*/msx, $params{'init'} ) { | |||||||||||||||||||
| 74 | 0 | $dbh->do($row); | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | 0 | return; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | 0 | } | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | # Construct deinit method | ||||||||||||||||||||
| 81 | 0 | my $deinit; | |||||||||||||||||||
| 82 | 0 | ▶ | if ( ref $params{'deinit'} eq 'CODE' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 0 | $deinit = $params{'deinit'}; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | else { # SCALAR | ||||||||||||||||||||
| 86 | 0 | ▶ | $deinit = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 87 | 0 | my ($dbh) = @_; | |||||||||||||||||||
| 88 | 0 | foreach my $row ( split qr/;\s*/msx, $params{'deinit'} ) { | |||||||||||||||||||
| 89 | 0 | $dbh->do($row); | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | 0 | return; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | 0 | } | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | # Construct _cleanup method | ||||||||||||||||||||
| 96 | 0 | ▶ | my $_cleanup = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 97 | 0 | my ( $dbh, $name, $info ) = @_; | |||||||||||||||||||
| 98 | 0 | $_log->infof( 'Deleting dir %s for temp db \'%s\'', $info->{'dirpath'}, $name ); | |||||||||||||||||||
| 99 | 0 | remove_tree( $info->{'dirpath'}, { safe => 1, } ); | |||||||||||||||||||
| 100 | 0 | }; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | # Create database representing object. | ||||||||||||||||||||
| 103 | return Database::Temp::DB->new( | ||||||||||||||||||||
| 104 | driver => ( __PACKAGE__ =~ m/^Database::Temp::Driver::(.*)$/msx )[0], | ||||||||||||||||||||
| 105 | name => $params{'name'}, | ||||||||||||||||||||
| 106 | 0 | ▶ | cleanup => $params{'cleanup'} // 0, | ||||||||||||||||||
|
Condition0%0/2
$params{"cleanup"} // 0
MC/DC0%0/1
$params{"cleanup"} // 0
$params{"cleanup"}
Truth table0%0/2
A
A= $params{"cleanup"}
| |||||||||||||||||||||
| 107 | _cleanup => $_cleanup, | ||||||||||||||||||||
| 108 | _start => $_start, | ||||||||||||||||||||
| 109 | init => $init, | ||||||||||||||||||||
| 110 | deinit => $deinit, | ||||||||||||||||||||
| 111 | dsn => $dsn, | ||||||||||||||||||||
| 112 | username => undef, | ||||||||||||||||||||
| 113 | password => undef, | ||||||||||||||||||||
| 114 | attr => \%attrs, | ||||||||||||||||||||
| 115 | info => \%info, | ||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 1 | 1; | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||