File Coverage

blib/lib/DBIx/QuickDB/Driver/SQLite.pm
Criterion Covered Total %
statement 30 69 43.4
branch 3 18 16.6
condition 0 6 0.0
subroutine 9 18 50.0
pod 9 9 100.0
total 51 120 42.5


line stmt bran cond sub pod time code
1             package DBIx::QuickDB::Driver::SQLite;
2 5     5   209901 use strict;
  5         20  
  5         134  
3 5     5   22 use warnings;
  5         8  
  5         127  
4              
5 5     5   1172 use IPC::Cmd qw/can_run/;
  5         87153  
  5         253  
6 5     5   30 use Scalar::Util qw/reftype/;
  5         10  
  5         265  
7              
8             our $VERSION = '0.000023';
9              
10 5     5   572 use parent 'DBIx::QuickDB::Driver';
  5         316  
  5         26  
11              
12 5     5   318 use DBIx::QuickDB::Util::HashBase qw{-sqlite -started};
  5         10  
  5         27  
13              
14             my ($SQLITE, $DBDSQLITE);
15              
16             BEGIN {
17 5     5   16 local $@;
18              
19 5         21 $SQLITE = can_run('sqlite3');
20 5         1428 $DBDSQLITE = eval { require DBD::SQLite; 'DBD::SQLite' };
  5         3553  
  0         0  
21             }
22              
23             sub version_string {
24 0     0 1 0 my $binary;
25              
26             # Go in reverse order assuming the last param hash provided is most important
27 0         0 for my $arg (reverse @_) {
28 0 0       0 my $type = reftype($arg) or next; # skip if not a ref
29 0 0       0 next unless $type eq 'HASH'; # We have a hashref, possibly blessed
30              
31             # If we find a launcher we are done looping, we want to use this binary.
32 0 0       0 $binary = $arg->{+SQLITE} and last;
33             }
34              
35             # If no args provided one to use we fallback to the default from $PATH
36 0   0     0 $binary ||= $SQLITE;
37              
38             # Call the binary with '-V', capturing and returning the output using backticks.
39 0         0 return `$binary -version`;
40             }
41              
42 7     7   42 sub _default_paths { return (sqlite => $SQLITE) }
43              
44             sub viable {
45 7     7 1 17 my $this = shift;
46 7         16 my ($spec) = @_;
47              
48 7 50       30 my %check = (ref($this) ? %$this : (), $this->_default_paths, %$spec);
49              
50 7         27 my @bad;
51 7 50       26 push @bad => "'DBD::SQLite' module could not be loaded, needed for everything" unless $DBDSQLITE;
52              
53 7 50       19 return (1, undef) unless @bad;
54 7         34 return (0, join "\n" => @bad);
55             }
56              
57             sub init {
58 0     0 1   my $self = shift;
59 0           $self->SUPER::init();
60              
61 0           my %defaults = $self->_default_paths;
62 0   0       $self->{$_} ||= $defaults{$_} for keys %defaults;
63              
64 0           $self->{+STARTED} = 1;
65              
66 0           return;
67             }
68              
69 0     0     sub bootstrap { return }
70 0     0 1   sub start { return }
71 0     0 1   sub stop { return }
72              
73             sub clone {
74 0     0 1   my $self = shift;
75              
76 0           local $self->{+STARTED} = 0;
77              
78 0           return $self->SUPER::clone(@_);
79             }
80              
81             sub connect_string {
82 0     0 1   my $self = shift;
83 0           my ($db_name) = @_;
84 0 0         $db_name = 'quickdb' unless defined $db_name;
85              
86 0           my $dir = $self->{+DIR};
87 0           my $path = "$dir/$db_name";
88              
89 0           require DBD::SQLite;
90 0           return "dbi:SQLite:dbname=$path";
91             }
92              
93             sub load_sql {
94 0     0 1   my $self = shift;
95 0           my ($db_name, $file) = @_;
96              
97 0           my $dbh = $self->connect($db_name, sqlite_allow_multiple_statements => 1, RaiseError => 1, AutoCommit => 1);
98              
99 0 0         open(my $fh, '<', $file) or die "Could not open file '$file': $!";
100 0           my $sql = join "" => <$fh>;
101 0           close($fh);
102              
103 0 0         $dbh->do($sql) or die $dbh->errstr;
104             }
105              
106             sub shell_command {
107 0     0 1   my $self = shift;
108 0           my ($db_name) = @_;
109              
110 0           my $dir = $self->{+DIR};
111 0           my $path = "$dir/$db_name";
112              
113 0           return ($self->{+SQLITE}, $path);
114             }
115              
116             1;
117              
118             __END__