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   241126 use strict;
  5         18  
  5         168  
3 5     5   30 use warnings;
  5         12  
  5         160  
4              
5 5     5   1440 use IPC::Cmd qw/can_run/;
  5         108732  
  5         319  
6 5     5   43 use Scalar::Util qw/reftype/;
  5         11  
  5         343  
7              
8             our $VERSION = '0.000021';
9              
10 5     5   584 use parent 'DBIx::QuickDB::Driver';
  5         328  
  5         50  
11              
12 5     5   406 use DBIx::QuickDB::Util::HashBase qw{-sqlite -started};
  5         13  
  5         33  
13              
14             my ($SQLITE, $DBDSQLITE);
15              
16             BEGIN {
17 5     5   22 local $@;
18              
19 5         23 $SQLITE = can_run('sqlite3');
20 5         1808 $DBDSQLITE = eval { require DBD::SQLite; 'DBD::SQLite' };
  5         4357  
  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   44 sub _default_paths { return (sqlite => $SQLITE) }
43              
44             sub viable {
45 7     7 1 18 my $this = shift;
46 7         19 my ($spec) = @_;
47              
48 7 50       36 my %check = (ref($this) ? %$this : (), $this->_default_paths, %$spec);
49              
50 7         17 my @bad;
51 7 50       27 push @bad => "'DBD::SQLite' module could not be loaded, needed for everything" unless $DBDSQLITE;
52              
53 7 50       22 return (1, undef) unless @bad;
54 7         42 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__