lib/Net/Download/Queue/DBI.pm

filter: Statementstmt 100% 41 / 41 Branchbran 62.5% 5 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 6 / 6 totaltotal 95.3% 61 / 64 CC 5 SCAR 5.7
CC
5
Cov
94%
ensureDatabase
3.0
oDownloadStatus
2.0
rebuildDatabase
2.0
CRAP 1.8
« lib/Net/Download/Queue.pm ↑ Summary lib/Net/Download/Queue/DownloadStatus.pm »
1 =head1 NAME
2
3 Net::Download::Queue::DBI - Net::Download::Queue::DBI base class
4
5 =head1 SYNOPSIS
6
7
8
9 =cut
10
11
12
13
14
15 package Net::Download::Queue::DBI;
16 8 ▶use base 'Class::DBI::SQLite';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 #use base 'Class::DBI::mysql';
18
19
20
21 8 our $VERSION = 0.01;
22
23
24
25 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 8 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
27
28
29
30
31
32 =head1 CLASS METHODS
33
34 These must be before the other ones...
35
36
37 =head2 fileDatabase()
38
39 Return file name of SQLite database.
40
41 =cut
42 26 ▶sub fileDatabase {
Subroutine fileDatabase: called CC=1 SCAR=0.0
Pod documented
43 26 return("./download-queue.db");
44 }
45
46
47
48
49
50 =head2 rebuildDatabase()
51
52 Empty and rebuild the SQLite database.
53
54 Return 1 on success, else die on errors.
55
56 =cut
57 5 ▶sub rebuildDatabase {
Subroutine rebuildDatabase: called CC=2 SCAR=7.0
Pod documented
58 5 my $pkg = shift;
59
60 5 $pkg->db_Main->disconnect; #To avoid it being locked when unlinking
61
62 5 my $fileDatabase = $pkg->fileDatabase;
63 5 ▶ unlink($fileDatabase); -f $fileDatabase and die("Could not delete existing database file ($fileDatabase): $!\n");
Branch: 1 branch
BranchTrueFalseTotal
-f $fileDatabase and die "Could not delete existing database file ($fileDatabase): $!\n" 0 5 5
64
65 5 return( $pkg->ensureDatabase() );
66 }
67
68
69
70
71
72 =head2 ensureDatabase()
73
74 Rebuild the SQLite database if it's not present.
75
76 Return 1 on success, else die on errors.
77
78 =cut
79 13 ▶sub ensureDatabase {
Subroutine ensureDatabase: called CC=3 SCAR=11.0
Pod documented
80 13 my $pkg = shift;
81 13 my $fileDatabase = $pkg->fileDatabase;
82 13 ▶ -f $fileDatabase and return(1);
Branch: 1 branch
BranchTrueFalseTotal
-f $fileDatabase and return 1 6 7 13
83
84 7 my $fileSql = dirname(__FILE__) . "/database/sqlite/create.sql";
85 7 ▶ -f $fileSql or die("Could not find SQLite create file ($fileSql)\n");
Branch: 1 branch
BranchTrueFalseTotal
-f $fileSql or die "Could not find SQLite create file ($fileSql)\n" 7 0 7
86
87 # warn "dbish\n";
88 7 `dbish "dbi:SQLite:dbname=$fileDatabase" < "$fileSql" 2>&1`;
89
90 7 return(1);
91 }
92
93
94
95
96
97 8 __PACKAGE__->ensureDatabase(); #Must be there before set_db
98
99 8 my $fileDatabase = __PACKAGE__->fileDatabase;
100 8 my ($dsn, $username, $password) = ("dbi:SQLite:dbname=$fileDatabase", undef, undef);
101 #my ($dsn, $username, $password) = ("dbi:mysql:database=app;port=3306", "app", "abc123");
102 8 __PACKAGE__->set_db('Main', $dsn, $username, $password, { AutoCommit => 1 } );
103
104 #my $dbh = DBI->connect($dsn, $username, $password) or die("Could not connect to db\n");
105
106
107
108
109
110 =head1 METHODS
111
112
113 =head1 CLASS METHODS
114
115 =head2 accessor_name
116
117 Reformat accessor names. Overridden.
118
119 =cut
120 98 ▶sub accessor_name { my $pkg = shift;
Subroutine accessor_name: called CC=1 SCAR=0.0
Pod documented
121 98 my ($column) = @_;
122
123 98 $column =~ s/ (_+) (\w) / uc($2) /gex;
124
125 98 return($column);
126 }
127
128
129
130
131
132 =head2 search_first
133
134 Like search(), but return the first row.
135
136 =cut
137 15 ▶sub search_first {
Subroutine search_first: called CC=1 SCAR=0.0
Pod documented
138 15 my $pkg = shift;
139
140 15 my $itRow = $pkg->search(@_);
141
142 15 return($itRow->next);
143 }
144
145
146
147
148
149 =head2 oDownloadStatus($name)
150
151 Return DownloadStatus object with $name, or die on errors.
152
153 =cut
154 11 ▶sub oDownloadStatus {
Subroutine oDownloadStatus: called CC=2 SCAR=7.0
Pod documented
155 11 my $pkg = shift;
156 11 my ($name) = @_;
157
158 11 ▶ my $oStatus = Net::Download::Queue::DownloadStatus->search_first({name => $name}) or die("Could not get status ($name)\n");
Branch: 1 branch
BranchTrueFalseTotal
my $oStatus = "Net::Download::Queue::DownloadStatus"->search_first({"name", $name}) or die "Could not get status ($name)\n" 11 0 11
159
160 11 return($oStatus);
161 }
162
163
164
165
166
167 8 1;
168
169
170
171
172
173 __END__
« lib/Net/Download/Queue.pm ↑ Summary lib/Net/Download/Queue/DownloadStatus.pm »