blib/lib/DBIx/QuickDB/Driver/SQLite.pm

filter: Statementstmt 68.6% 59 / 86 Branchbran 35.7% 10 / 28 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 61.1% 11 / 18 Podpod 100% 9 / 9 totaltotal 58.3% 91 / 156 CC 20 SCAR 26.4
CC
20
Cov
58%
version_string
56.0
load_sql
8.5
connect_string
6.0
CRAP 14.0
« blib/lib/DBIx/QuickDB/Driver/MariaDB.pm ↑ Summary lib/DBIx/QuickDB/Util.pm »
1 package DBIx::QuickDB::Driver::SQLite;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use DBIx::QuickDB::Util qw/find_bin/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 our $VERSION = '0.000066';
9  
10 6 ▶use parent 'DBIx::QuickDB::Driver';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use DBIx::QuickDB::Util::HashBase qw{-sqlite -started};
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 my ($SQLITE, $DBDSQLITE);
15  
16 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.1
17 6 local $@;
18  
19 6 $SQLITE = find_bin('sqlite3');
20 0 $DBDSQLITE = eval { require DBD::SQLite; 'DBD::SQLite' };
21 }
22  
23 0 ▶sub version_string {
Subroutine version_string: not called CC=7 SCAR=40.3
Pod documented
24 0 my $binary;
25  
26 # Go in reverse order assuming the last param hash provided is most important
27 0 for my $arg (reverse @_) {
28 0 ▶ my $type = reftype($arg) or next; # skip if not a ref
Branch: 1 branch
BranchTrueFalseTotal
my $type = builtin::reftype($arg) or next 0 0 0
29 0 ▶ next unless $type eq 'HASH'; # We have a hashref, possibly blessed
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "HASH" 0 0 0
30  
31 # If we find a launcher we are done looping, we want to use this binary.
32 0 ▶ $binary = $arg->{+SQLITE} and last;
Branch: 1 branch
BranchTrueFalseTotal
$binary = $arg->{"sqlite"} and last 0 0 0
33 }
34  
35 # If no args provided one to use we fallback to the default from $PATH
36 0 ▶ $binary ||= $SQLITE;
Condition0%0/3
$binary ||= $SQLITE
l!l&&r!l&&!r
000
MC/DC0%0/2
$binary ||= $SQLITE
$binary $SQLITE
Truth table0%0/3
A ||= B
A= $binaryB= $SQLITE
ABresult
1X1
011
000
37  
38 0 ▶ return 'unknown' unless $binary;
Branch: 1 branch
BranchTrueFalseTotal
unless $binary 0 0 0
39  
40 # Call the binary with '-V', capturing and returning the output using backticks.
41 0 return `$binary -version`;
42 }
43  
44 10 ▶sub _default_paths { return (sqlite => $SQLITE) }
Subroutine _default_paths: called CC=1 SCAR=0.0
45  
46 7 ▶sub viable {
Subroutine viable: called CC=4 SCAR=14.3
Pod documented
47 7 my $this = shift;
48 7 my ($spec) = @_;
49  
50 7 ▶ my %check = (ref($this) ? %$this : (), $this->_default_paths, %$spec);
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 0 7 7
51  
52 7 my @bad;
53 7 ▶ push @bad => "'DBD::SQLite' module could not be loaded, needed for everything" unless $DBDSQLITE;
Branch: 1 branch
BranchTrueFalseTotal
unless $DBDSQLITE 7 0 7
54  
55 7 ▶ return (1, undef) unless @bad;
Branch: 1 branch
BranchTrueFalseTotal
unless @bad 0 7 7
56 7 return (0, join "\n" => @bad);
57 }
58  
59 3 ▶sub init {
Subroutine init: called CC=3 SCAR=11.3
Pod documented
60 3 my $self = shift;
61 3 $self->SUPER::init();
62  
63 3 my %defaults = $self->_default_paths;
64 3 ▶ $self->{$_} ||= $defaults{$_} for keys %defaults;
Condition33%1/3
$self->{$_} ||= $defaults{$_}
l!l&&r!l&&!r
003
MC/DC0%0/2
$self->{$_} ||= $defaults{$_}
$self->{$_} $defaults{$_}
Truth table33%1/3
A ||= B
A= $self->{$_}B= $defaults{$_}
ABresult
1X1
011
000
65  
66 3 $self->{+STARTED} = 1;
67  
68 3 return;
69 }
70  
71 0 ▶sub bootstrap { return }
Subroutine bootstrap: not called CC=1 SCAR=6.9
72 0 ▶sub start { return }
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
73 0 ▶sub stop { return }
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
74  
75 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
76 0 my $self = shift;
77  
78 0 local $self->{+STARTED} = 0;
79  
80 0 return $self->SUPER::clone(@_);
81 }
82  
83 0 ▶sub connect_string {
Subroutine connect_string: not called CC=2 SCAR=17.9
Pod documented
84 0 my $self = shift;
85 0 my ($db_name) = @_;
86 0 ▶ $db_name = 'quickdb' unless defined $db_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $db_name 0 0 0
87  
88 0 my $dir = $self->{+DIR};
89 0 my $path = "$dir/$db_name";
90  
91 0 require DBD::SQLite;
92 0 return "dbi:SQLite:dbname=$path";
93 }
94  
95 2 ▶sub load_sql {
Subroutine load_sql: called CC=8 SCAR=21.4
Pod documented
96 2 my $self = shift;
97 2 my ($db_name, $file) = @_;
98  
99 2 ▶ open(my $fh, '<', $file) or die "Could not open file '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Could not open file '${file}': $!" 2 0 2
100 2 my $sql = join "" => <$fh>;
101 2 close($fh);
102  
103 2 my $dbh = $self->connect($db_name, sqlite_allow_multiple_statements => 1, RaiseError => 1, AutoCommit => 1);
104  
105 2 my $result;
106 2 my $ok = eval {
107 2 ▶ $result = $dbh->do($sql) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$result = $dbh->do($sql) or die $dbh->errstr 1 0 1
108 1 1;
109 };
110 2 my $error = $@;
111  
112 # Do not rely on DBI handle destruction to close the native SQLite file.
113 # On Windows that close can otherwise lag long enough for Driver cleanup to
114 # exhaust its deletion retries and leave the generated database directory.
115 2 my $disconnect_ok = eval {
116 2 ▶ $dbh->disconnect or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->disconnect or die $dbh->errstr 2 0 2
117 2 1;
118 };
119 2 my $disconnect_error = $@;
120  
121 2 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 1 2
122 1 ▶ $error = "SQLite SQL execution failed without an error message\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error and length "$error" 0 1 1
Condition33%1/3
defined $error and length "$error"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $error and length "$error"
defined $error length "$error"
Truth table33%1/3
A and B
A= defined $errorB= length "$error"
ABresult
0X0
100
111
123 unless defined($error) && length("$error");
124 1 die $error;
125 }
126 1 ▶ die $disconnect_error unless $disconnect_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $disconnect_ok 0 1 1
127  
128 1 return $result;
129 }
130  
131 0 ▶sub shell_command {
Subroutine shell_command: not called CC=1 SCAR=6.9
Pod documented
132 0 my $self = shift;
133 0 my ($db_name) = @_;
134  
135 0 my $dir = $self->{+DIR};
136 0 my $path = "$dir/$db_name";
137  
138 0 return ($self->{+SQLITE}, $path);
139 }
140  
141 6 1;
142  
143 __END__
« blib/lib/DBIx/QuickDB/Driver/MariaDB.pm ↑ Summary lib/DBIx/QuickDB/Util.pm »