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