File Coverage

blib/lib/Database/Temp/Driver/SQLite.pm
Criterion Covered Total %
statement 47 63 74.6
branch 3 6 50.0
condition 3 5 60.0
subroutine 11 13 84.6
pod 2 2 100.0
total 66 89 74.1


line stmt bran cond sub pod time code
1             package Database::Temp::Driver::SQLite;
2              
3 2     2   2000 use strict;
  2         5  
  2         2039  
4 2     2   15 use warnings;
  2         4  
  2         185  
5              
6             # ABSTRACT: Create an ad-hoc database, driver for SQLite
7              
8             our $VERSION = '0.003'; # VERSION: generated by DZP::OurPkgVersion
9              
10 2     2   1248 use Module::Load::Conditional qw( can_load );
  2         41715  
  2         184  
11 2     2   23 use File::Spec;
  2         4  
  2         52  
12 2     2   11 use Carp qw( shortmess );
  2         3  
  2         96  
13              
14 2     2   41 use Log::Any;
  2         5  
  2         23  
15 2     2   97 use Try::Tiny;
  2         4  
  2         1676  
16              
17             sub is_available {
18 8     8 1 55 my %needed = ( 'DBD::SQLite' => 1.41_01, );
19 8 50       52 if ( can_load( modules => \%needed ) ) {
20 8         99018 return 1;
21             }
22             else {
23 0         0 Log::Any->get_logger( category => 'Database::Temp' )->infof( 'Cannot load module %s, %s', %needed );
24 0         0 return 0;
25             }
26              
27             }
28              
29             sub new {
30 6     6 1 42 my ( $class, %params ) = @_;
31 6         98 my $_log = Log::Any->get_logger( category => 'Database::Temp' );
32              
33 6   66     4578 my $dir = $params{'args'}->{'dir'} // File::Spec->tmpdir();
34 6         19 my $filename = $params{'name'};
35 6         79 my $filepath = File::Spec->catfile( $dir, $filename );
36 6         22 my $dsn = "dbi:SQLite:uri=file:$filepath?mode=rwc";
37 6         34 $_log->debugf( 'Created temp filepath \'%s\'', $filepath );
38              
39 6         83 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         20 my %info = ( 'filepath' => $filepath, );
52              
53             # Construct start method
54             my $_start = sub {
55 6     6   22 my ( $dbh, $name ) = @_;
56 6         40 Log::Any->get_logger( category => 'Database::Temp' )->debugf( 'Created temp db \'%s\'', $name );
57 6         36 };
58              
59             # Construct init method
60 6         15 my $init;
61 6 50       45 if ( ref $params{'init'} eq 'CODE' ) {
62 6         15 $init = $params{'init'};
63             }
64             else { # SCALAR
65             $init = sub {
66 0     0   0 my ($dbh) = @_;
67 0         0 $dbh->begin_work();
68 0         0 foreach my $row ( split qr/;\s*/msx, $params{'init'} ) {
69 0         0 $dbh->do($row);
70             }
71 0         0 $dbh->commit;
72 0         0 return;
73             }
74 0         0 }
75              
76             # Construct deinit method
77 6         12 my $deinit;
78 6 50       21 if ( ref $params{'deinit'} eq 'CODE' ) {
79 6         14 $deinit = $params{'deinit'};
80             }
81             else { # SCALAR
82             $deinit = sub {
83 0     0   0 my ($dbh) = @_;
84 0         0 $dbh->begin_work();
85 0         0 foreach my $row ( split qr/;\s*/msx, $params{'deinit'} ) {
86 0         0 $dbh->do($row);
87             }
88 0         0 $dbh->commit;
89 0         0 return;
90             }
91 0         0 }
92              
93             # Construct _cleanup method
94             my $_cleanup = sub {
95 4     4   16 my ( $dbh, $name, $info ) = @_;
96 4         33 $_log->infof( 'Deleting file %s, db %s', $info->{'filepath'}, $name );
97 4         363 unlink $info->{'filepath'};
98 6         28 };
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   50     333 cleanup => $params{'cleanup'} // 0,
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             1;
118              
119             __END__
120              
121             =pod
122              
123             =encoding UTF-8
124              
125             =head1 NAME
126              
127             Database::Temp::Driver::SQLite - Create an ad-hoc database, driver for SQLite
128              
129             =head1 VERSION
130              
131             version 0.003
132              
133             =head2 is_available
134              
135             Can this driver provide a database?
136              
137             Return boolean.
138              
139             =head2 new
140              
141             Create a temp database.
142              
143             User should never call this subroutine directly, only via L<Database::Temp>.
144              
145             =head1 AUTHOR
146              
147             Mikko Koivunalho <mikkoi@cpan.org>
148              
149             =head1 COPYRIGHT AND LICENSE
150              
151             This software is copyright (c) 2023 by Mikko Johannes Koivunalho.
152              
153             This is free software; you can redistribute it and/or modify it under
154             the same terms as the Perl 5 programming language system itself.
155              
156             =cut