File Coverage

blib/lib/Data/Downloader/DB.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Data::Downloader::DB
4              
5             =head1 DESCRIPTION
6              
7             Controls the location of the data downloader cache database.
8              
9             The database file is located in $HOME/.data_downloader.db
10             by default. This may be overridden by setting the
11             DATA_DOWNLOADER_DATABASE environment variable.
12              
13             =head1 METHODS
14              
15             =over
16              
17             =cut
18              
19             package Data::Downloader::DB;
20              
21 6     6   205752 use strict;
  6         16  
  6         189  
22 6     6   38 use warnings;
  6         10  
  6         155  
23              
24 6     6   18983 use DBIx::Simple;
  6         250897  
  6         479  
25 6     6   11205 use File::Spec::Functions qw(catfile tmpdir);
  6         6479  
  6         831  
26              
27 6     6   46 use base "Rose::DB";
  6         12  
  6         14754  
28              
29             __PACKAGE__->register_db(
30             domain => "test",
31             type => "main",
32             driver => "sqlite",
33             database => ( $ENV{DATA_DOWNLOADER_TESTDB} || ':memory:' ),
34             connect_options => {
35             PrintError => ($ENV{DD_PRINT_DB_ERRORS} ? 1 : 0),
36             RaiseError => 0,
37             sqlite_use_immediate_transaction =>
38             ($ENV{DATA_DOWNLOADER_IMMEDIATE_TRANSACTION} ? 1 : 0)
39             }
40             );
41              
42             __PACKAGE__->register_db(
43             domain => "live",
44             type => "main",
45             driver => "sqlite",
46             database => ( $ENV{DATA_DOWNLOADER_DATABASE} ||
47             catfile($ENV{HOME}, '.data_downloader.db') ),
48             connect_options => {
49             sqlite_use_immediate_transaction =>
50             ($ENV{DATA_DOWNLOADER_IMMEDIATE_TRANSACTION} ? 1 : 0)
51             }
52             );
53              
54             __PACKAGE__->default_domain($ENV{HARNESS_ACTIVE} ? "test" : "live");
55             __PACKAGE__->default_type("main");
56              
57             =item dbi_connect
58              
59             Override to use connect_cached and do sqlite-specific setup.
60              
61             =cut
62              
63             sub dbi_connect {
64             my $class = shift;
65             # See Rose::DB -- this is the recommended way to cache db handles.
66             my $dbh = DBI->connect_cached(@_);
67             $dbh->do("PRAGMA synchronous = OFF");
68             $dbh->do("PRAGMA foreign_keys = ON") unless $ENV{DATA_DOWNLOADER_BULK_DOWNLOAD};
69             $dbh->do("PRAGMA count_changes = OFF");
70             if (my $mode = $ENV{DATA_DOWNLOADER_JOURNAL_MODE}) {
71             $dbh->do("PRAGMA journal_mode = $mode")
72             if (grep $_ eq $mode, qw(DELETE TRUNCATE PERSIST MEMORY WAL OFF));
73             }
74             $dbh->sqlite_busy_timeout(1000*300); # wait up to 5 minutes if it is locked
75             return $dbh;
76             }
77              
78             =item simple
79              
80             Returns a L object, for when the ORM is not enough.
81              
82             =cut
83              
84             sub simple {
85             return DBIx::Simple->new(shift->dbh);
86             }
87              
88             =back
89              
90             =head1 SEE ALSO
91              
92             L
93              
94             L
95              
96             =cut
97              
98             1;
99