line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::SQLite; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:GENE'; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# ABSTRACT: SQLite setup/teardown for tests |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.0409'; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
720
|
use DBI; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
44
|
|
9
|
1
|
|
|
1
|
|
520
|
use File::Copy; |
|
1
|
|
|
|
|
2412
|
|
|
1
|
|
|
|
|
67
|
|
10
|
1
|
|
|
1
|
|
734
|
use File::Temp (); |
|
1
|
|
|
|
|
17468
|
|
|
1
|
|
|
|
|
27
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
594
|
use Moo; |
|
1
|
|
|
|
|
11598
|
|
|
1
|
|
|
|
|
5
|
|
13
|
1
|
|
|
1
|
|
2057
|
use strictures 2; |
|
1
|
|
|
|
|
1983
|
|
|
1
|
|
|
|
|
38
|
|
14
|
1
|
|
|
1
|
|
744
|
use namespace::clean; |
|
1
|
|
|
|
|
11699
|
|
|
1
|
|
|
|
|
6
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has database => ( |
18
|
|
|
|
|
|
|
is => 'ro', |
19
|
|
|
|
|
|
|
isa => sub { die 'database does not exist' unless -e $_[0] }, |
20
|
|
|
|
|
|
|
predicate => 'has_database', |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has schema => ( |
25
|
|
|
|
|
|
|
is => 'ro', |
26
|
|
|
|
|
|
|
isa => sub { die 'schema does not exist' unless -e $_[0] }, |
27
|
|
|
|
|
|
|
predicate => 'has_schema', |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
has memory => ( |
32
|
|
|
|
|
|
|
is => 'ro', |
33
|
|
|
|
|
|
|
predicate => 'has_memory', |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
has db_attrs => ( |
38
|
|
|
|
|
|
|
is => 'ro', |
39
|
|
|
|
|
|
|
default => sub { return { RaiseError => 1, AutoCommit => 1 } }, |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
has dsn => ( |
44
|
|
|
|
|
|
|
is => 'lazy', |
45
|
|
|
|
|
|
|
init_arg => undef, |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub _build_dsn { |
49
|
4
|
|
|
4
|
|
32
|
my ($self) = @_; |
50
|
4
|
100
|
|
|
|
74
|
return 'dbi:SQLite:dbname=' . ( $self->has_memory ? $self->_database : $self->_database->filename ); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
has dbh => ( |
55
|
|
|
|
|
|
|
is => 'lazy', |
56
|
|
|
|
|
|
|
init_arg => undef, |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _build_dbh { |
60
|
4
|
|
|
4
|
|
767
|
my ($self) = @_; |
61
|
4
|
|
|
|
|
71
|
return DBI->connect( $self->dsn, '', '', $self->db_attrs ); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
has _database => ( |
65
|
|
|
|
|
|
|
is => 'lazy', |
66
|
|
|
|
|
|
|
init_arg => undef, |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub _build__database { |
70
|
4
|
|
|
4
|
|
86
|
my ($self) = @_; |
71
|
|
|
|
|
|
|
|
72
|
4
|
100
|
|
|
|
30
|
my $tempfile = $self->has_memory |
73
|
|
|
|
|
|
|
? ':memory:' |
74
|
|
|
|
|
|
|
: File::Temp->new( unlink => 1, suffix => '.db', EXLOCK => 0 ); |
75
|
|
|
|
|
|
|
|
76
|
4
|
100
|
|
|
|
1668
|
if ( $self->has_database ) { |
|
|
100
|
|
|
|
|
|
77
|
1
|
50
|
|
|
|
6
|
copy( $self->database, $tempfile->filename ) |
78
|
|
|
|
|
|
|
or die "Can't copy " . $self->database . ": $!"; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
elsif ( $self->has_schema ) { |
81
|
1
|
50
|
|
|
|
42
|
open my $schema, '<', $self->schema |
82
|
|
|
|
|
|
|
or die "Can't read " . $self->schema . ": $!"; |
83
|
|
|
|
|
|
|
|
84
|
1
|
50
|
|
|
|
10
|
my $dbh = DBI->connect( "dbi:SQLite:dbname=$tempfile", '', '', { RaiseError => 1, AutoCommit => 0 } ) |
85
|
|
|
|
|
|
|
or die "Can't connect to $tempfile: " . $DBI::errstr; |
86
|
|
|
|
|
|
|
|
87
|
1
|
|
|
|
|
365
|
my $sql = ''; |
88
|
1
|
|
|
|
|
22
|
while ( my $line = readline($schema) ) { |
89
|
18
|
100
|
|
|
|
45
|
next if $line =~ /^\s*--/; |
90
|
17
|
100
|
|
|
|
46
|
next if $line =~ /^\s*$/; |
91
|
|
|
|
|
|
|
|
92
|
14
|
|
|
|
|
27
|
$sql .= $line; |
93
|
|
|
|
|
|
|
|
94
|
14
|
100
|
|
|
|
40
|
if ( $line =~ /;/ ) { |
95
|
3
|
50
|
|
|
|
19
|
$dbh->do($sql) |
96
|
|
|
|
|
|
|
or die 'Error executing SQL for ' . $self->schema . ': ' . $dbh->errstr; |
97
|
|
|
|
|
|
|
|
98
|
3
|
|
|
|
|
924
|
$sql = ''; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
1
|
|
|
|
|
13706
|
$dbh->commit; |
103
|
|
|
|
|
|
|
|
104
|
1
|
|
|
|
|
108
|
$dbh->disconnect; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
4
|
|
|
|
|
484
|
return $tempfile; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub BUILD { |
112
|
7
|
|
|
7
|
0
|
63
|
my ( $self, $args ) = @_; |
113
|
7
|
100
|
100
|
|
|
146
|
die 'The schema, database and memory arguments may not be used together.' |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
114
|
|
|
|
|
|
|
if ( $self->has_database and $self->has_schema ) |
115
|
|
|
|
|
|
|
or ( $self->has_database and $self->has_memory ) |
116
|
|
|
|
|
|
|
or ( $self->has_schema and $self->has_memory ); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
__END__ |