SQLite.pm

filter: Statementstmt 71.2% 52 / 73 Branchbran 27.2% 6 / 22 Conditioncond 18.1% 2 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 88.8% 8 / 9 Podpod 66.6% 2 / 3 totaltotal 56.0% 70 / 125 SCAR 36.1
CC
16
Cov
57%
new
20.2
index
20.0
query
3.3
CRAP 36.9
« SQLite/DBI/Index.pm ↑ Summary SQLite/DBI.pm »
1 # $Id: SQLite.pm 82 2020-05-30 06:14:27Z stro $
2  
3 package CPAN::SQLite;
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 our $VERSION = '0.218';
8  
9 5 ▶use English qw/-no_match_vars/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 require File::Spec;
13 5 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 require CPAN::SQLite::META;
15  
16 # an array ref of distributions to ignore indexing
17 my $ignore = [qw(SpreadSheet-WriteExcel-WebPivot)];
18 our $db_name = 'cpandb.sql';
19  
20 5 ▶use constant WIN32 => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 sub new {
23 2 ▶ my $class = shift;
Subroutine new: called CC=11 SCAR=30.0
Pod undocumented
24 2 my %args = @_;
25  
26 2 my ($CPAN, $update_indices);
27 2 my $db_dir = $args{db_dir};
28 2 my $urllist = [];
29 2 my $keep_source_where;
30  
31 # for testing under Darwin, must load CPAN::MyConfig contained
32 # in PERL5LIB, as File::HomeDir doesn't use this
33 2 ▶ if ($ENV{CPAN_SQLITE_TESTING}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"CPAN_SQLITE_TESTING"}) 2 0 2
34 2 eval { require CPAN::MyConfig; };
35 }
36 2 eval { require CPAN; CPAN::HandleConfig->load; };
37 2 ▶ if (not $@ and not defined $args{CPAN}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ || defined $args{'CPAN'}) { } 2 0 2
Condition33%1/3
$@ || defined $args{'CPAN'}
l!l&&r!l&&!r
002
MC/DC0%0/2
$@ || defined $args{'CPAN'}
$@ defined $args{'CPAN'}
Truth table33%1/3
A || B
A= $@B= defined $args{'CPAN'}
ABresult
1X1
011
000
38 2 $CPAN = $CPAN::Config->{cpan_home};
39 2 $db_dir = $CPAN;
40 2 $keep_source_where = $CPAN::Config->{keep_source_where};
41 2 $urllist = $CPAN::Config->{urllist};
42  
43 # Sometimes this directory doesn't exist (like on new installations)
44 2 ▶ unless (-d $CPAN) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $CPAN) 0 2 2
45 0 eval { File::Path::mkpath($CPAN); }; # copied from CPAN.pm
46 }
47 2 ▶ die qq{The '$CPAN' directory doesn't exist} unless -d $CPAN;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $CPAN 0 2 2
48 2 $update_indices = 0;
49 } else {
50 0 ▶ $CPAN = $args{CPAN} || '';
Condition0%0/2
$args{'CPAN'} || ''
l!l
00
MC/DC0%0/1
$args{'CPAN'} || ''
$args{'CPAN'}
Truth table0%0/2
A
A= $args{'CPAN'}
Aresult
11
00
51 0 ▶ die qq{Please specify the CPAN location} unless defined $CPAN;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $CPAN 0 0 0
52 0 ▶ die qq{The '$CPAN' directory doesn't exist} unless (-d $CPAN);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $CPAN 0 0 0
53 0 ▶ $update_indices = (-f File::Spec->catfile($CPAN, 'MIRRORING.FROM')) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
-f 'File::Spec'->catfile($CPAN, 'MIRRORING.FROM') ? : 0 0 0
54 }
55 2 push @$urllist, q{http://www.cpan.org/};
56 2 ▶ $db_dir ||= cwd;
Condition33%1/3
$db_dir ||= cwd()
l!l&&r!l&&!r
200
MC/DC0%0/2
$db_dir ||= cwd()
$db_dir cwd()
Truth table33%1/3
A ||= B
A= $db_dirB= cwd()
ABresult
1X1
011
000
57 2 my $self = {
58 %args,
59 CPAN => $CPAN,
60 update_indices => $update_indices,
61 db_name => $db_name,
62 urllist => $urllist,
63 keep_source_where => $keep_source_where,
64 db_dir => $db_dir
65 };
66 2 return bless $self, $class;
67 }
68  
69 sub index {
70 0 ▶ my ($self, %args) = @_;
Subroutine index: not called CC=4 SCAR=30.0
Pod documented
71 0 require CPAN::SQLite::Index;
72 0 my %wanted = map { $_ => $self->{$_} } qw(CPAN ignore update_indices db_name db_dir keep_source_where setup reindex urllist);
73 0 ▶ my $log_dir = $self->{CPAN} || $self->{db_dir};
Condition0%0/3
$self->{'CPAN'} || $self->{'db_dir'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'CPAN'} || $self->{'db_dir'}
$self->{'CPAN'} $self->{'db_dir'}
Truth table0%0/3
A || B
A= $self->{'CPAN'}B= $self->{'db_dir'}
ABresult
1X1
011
000
74 0 ▶ die qq{Please create the directory '$log_dir' first} unless -d $log_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $log_dir 0 0 0
75 0 my $index = CPAN::SQLite::Index->new(%wanted, %args, log_dir => $log_dir);
76 0 ▶ $index->index() or do {
Branch: 1 branch
BranchTrueFalseTotal
$index->index or do { warn "Indexing failed!"; return } 0 0 0
77 0 warn qq{Indexing failed!};
78 0 return;
79 };
80 0 return 1;
81 }
82  
83 sub query {
84 360 ▶ my ($self, %args) = @_;
Subroutine query: called CC=3 SCAR=12.0
Pod documented
85 360 require CPAN::SQLite::Search;
86 360 my %wanted = map { $_ => $self->{$_} } qw(max_results CPAN db_name db_dir meta_obj);
87 360 my $query = CPAN::SQLite::Search->new(%wanted, %args);
88 360 %wanted = map { $_ => $self->{$_} } qw(mode query id name);
89 360 ▶ $query->query(%wanted, %args) or do {
Branch: 1 branch
BranchTrueFalseTotal
$query->query(%wanted, %args) or do { warn "Query failed!"; return } 0 360 360
90 0 warn qq{Query failed!};
91 0 return;
92 };
93 360 my $results = $query->{results};
94 360 ▶ return unless defined $results;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $results 360 0 360
95 0 $self->{results} = $query->{results};
96 0 return 1;
97 }
98  
99 1;
100  
101 =head1 NAME
102  
103 CPAN::SQLite - maintain and search a minimal CPAN database
104  
105 =head1 VERSION
106  
107 version 0.218
108  
109 =head1 SYNOPSIS
110  
111 my $obj = CPAN::SQLite->new(CPAN => '/path/to/CPAN');
112 $obj->index(setup => 1);
113  
114 $obj->query(mode => 'dist', name => 'CPAN');
115 my $results = $obj->{results};
116  
117 =head1 DESCRIPTION
118  
119 This package is used for setting up, maintaining, and
120 searching a CPAN database consisting of the information
121 stored in the two main CPAN indices:
122 F<$CPAN/modules/02packages.details.txt.gz> and
123 F<$CPAN/authors/01mailrc.txt.gz>. It should be
124 considered at an alpha stage of development.
125  
126 One begins by creating the object as
127  
128 my $obj = CPAN::SQLite->new(%args);
129  
130 which accepts the following arguments:
131  
132 =over 3
133  
134 =item * C<CPAN =E<gt> '/path/to/CPAN'>
135  
136 This specifies the path to where the index files are
137 to be stored. This could be a local CPAN mirror,
138 defined here by the presence of a F<MIRRORED.BY> file beneath
139 this directory, or a local directory in which to store
140 these files from a remote CPAN mirror. In the latter case,
141 the index files are fetched from a remote CPAN mirror,
142 using the same list that C<CPAN.pm> uses, if this is
143 configured, and are updated if they are more than one
144 day old.
145  
146 If the C<CPAN> option is not given, it will default
147 to C<cpan_home> of L<CPAN>, if this is configured,
148 with the index files found under C<keep_source_where>.
149 A fatal error results if such a directory isn't found.
150 Updates to these index files are assumed here to be
151 handled by C<CPAN.pm>.
152  
153 =item * C<db_dir =E<gt> '/path/to/db/dir'>
154  
155 This specifies the path to where the database file is
156 found. If not given, it defaults to the
157 C<cpan_home> directory of C<CPAN.pm>, if present, or to
158 the directory in which the script was invoked. The name
159 of the database file is C<cpandb.sql>.
160  
161 =back
162  
163 There are two main methods available.
164  
165 =head2 C<$obj-E<gt>index(%args);>
166  
167 This is used to set up and maintain the database. The
168 following arguments are accepted:
169  
170 =over 3
171  
172 =item * setup =E<gt> 1
173  
174 This specifies that the database is to be created and
175 populated from the CPAN indices; any existing database
176 will be overwritten. Not specifying this option will
177 assume that an existing database is to be updated.
178  
179 =item * reindex =E<gt> 'dist_name'
180  
181 This specifies that the CPAN distribution C<dist_name>
182 is to be reindexed.
183  
184 =back
185  
186 =head2 C<$obj-E<gt>query(%args);>
187  
188 This is used for querying the database by distribution
189 name, module name, or CPAN author name. There are
190 two arguments needed to specify such queries.
191  
192 =over 3
193  
194 =item * C<mode =E<gt> some_value>
195  
196 This specifies what type of query to perform,
197 with C<mode> being one of C<dist>, C<module>,
198 or C<author>, for searching through, respectively,
199 CPAN distribution names, module names, or author names and
200 CPAN ids.
201  
202 =item * C<type =E<gt> query_term>
203  
204 This specifies the query term for the search, with
205 C<type> being one of C<name>, to search for an
206 exact match, or C<search>, for searching for partial
207 matches. Perl regular expressions are supported in
208 the C<query_term> for the C<search> option.
209  
210 =back
211  
212 As well, an option of C<max_results =E<gt> some_number> will
213 limit the number of results returned; if not specified,
214 the limit doesn't apply.
215  
216 =head1 CPAN.pm support
217  
218 As of CPAN.pm version 1.88_65, there is experimental support
219 within CPAN.pm for using CPAN::SQLite to obtain
220 information on packages, modules, and authors. One goal
221 of this is to reduce the memory footprint of the CPAN.pm
222 shell, as this information is no longer all preloaded into
223 memory. This can be enabled through
224  
225 perl -MCPAN -e shell
226 cpan> o conf use_sqlite 1
227  
228 Use
229  
230 cpan> o conf commit
231  
232 to save this setting for future sessions.
233  
234 Using CPAN::SQLite, what happens is that a request for information
235 through CPAN.pm, such as
236  
237 cpan> a ANDK
238  
239 will cause a query to the SQLite database to be made.
240 If successful, this will place the relevant data for this
241 request into the data structure CPAN.pm uses to store and
242 retrieve such information. Thus, at any given time, the
243 only information CPAN.pm stores in memory is that for
244 packages, modules, and authors for which previous queries
245 have been made. There are certain requests, such as
246  
247 cpan> r
248  
249 to make a list of recommended packages for which upgrades
250 on CPAN are available, which will result in loading
251 information on all available packages into memory; if such
252 a query is made, the subsequent memory footprint of CPAN.pm
253 with and without CPAN::SQLite will be essentially the same.
254  
255 The database itself, called F<cpandb.sql>, will be stored
256 in the location specified by C<$CPAN::Config-E<gt>{cpan_home}>.
257 When first started, this database will be created, and afterwards,
258 it will be updated if the database is older than one day since
259 the last update. A log file of the creation or update process, called
260 F<cpan_search_log.dddddddddd>, will be created in the same
261 directory as the database file.
262  
263 =head1 SEE ALSO
264  
265 L<CPAN::SQLite::Index>, for setting up and maintaining the database, and
266 L<CPAN::SQLite::Search> for an interface to querying the database. Some
267 details of the interaction with L<CPAN> is available from
268 L<CPAN::SQLite::META>. See also the L<cpandb> script for a command-line
269 interface to the indexing and querying of the database.
270  
271 =head1 SUPPORT
272  
273 You can find documentation for this module with the perldoc command.
274  
275 perldoc CPAN::SQLite
276  
277 You can also look for information at:
278  
279 =over 4
280  
281 =item * AnnoCPAN: Annotated CPAN documentation
282  
283 L<http://annocpan.org/dist/CPAN-SQLite>
284  
285 =item * CPAN::Forum: Discussion forum
286  
287 L<http:///www.cpanforum.com/dist/CPAN-SQLite>
288  
289 =item * CPAN Ratings
290  
291 L<http://cpanratings.perl.org/d/CPAN-SQLite>
292  
293 =item * RT: CPAN's request tracker
294  
295 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CPAN-SQLite>
296  
297 =item * Search CPAN
298  
299 L<http://search.cpan.org/dist/CPAN-SQLite>
300  
301 =back
302  
303 =head1 BUGS
304  
305 At this time, CPAN::SQLite keeps information contained only
306 in the latest version of a CPAN distribution. This means that
307 modules that are provided only in older versions of a CPAN
308 distribution will not be present in the database; for example,
309 at this time, the latest version of the I<libwww-perl> distribution
310 on CPAN is 5.805, but there are modules such as I<URI::URL::finger>
311 contained in version 5.10 of libwww-perl that are not present in 5.805.
312 This behaviour differs from that of L<CPAN> without CPAN::SQLite.
313 This may change in the future.
314  
315 Please report bugs and feature requests via
316 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CPAN-SQLite>.
317  
318 =head1 ENVIRONMENT VARIABLES
319  
320 Information messages from the indexing procedures are printed
321 out to STDOUT if the environment variable CPAN_SQLITE_DEBUG
322 is set. This is automatically set within L<CPAN::SQLite::Index>.
323 If CPAN_SQLITE_NO_LOG_FILES is set, no log files will be created
324 during the indexing procedures. Log files are deleted automatically
325 in 30 days. To override this, set CPAN_SQLITE_LOG_FILES_CLEANUP.
326 To stop automatic cleanup, set this variable to 0.
327  
328 If CPAN_SQLITE_DOWNLOAD variable are set, an already existing and
329 up-to-date cpandb.sql file will be downloaded from
330 http://cpansqlite.trouchelle.com/ where it's updated every hour. This
331 greatly increases performance and decreases CPU and memory consumption
332 during the indexing process.
333  
334 See L<CPAN::SQLite::Index> for more details, potential problems, and more
335 configuration options.
336  
337 =head1 AUTHORS
338  
339 Randy Kobes (passed away on September 18, 2010)
340  
341 Serguei Trouchelle E<lt>stro@cpan.orgE<gt>
342  
343 =head1 COPYRIGHT
344  
345 Copyright 2006,2008 by Randy Kobes E<lt>r.kobes@uwinnipeg.caE<gt>.
346  
347 Copyright 2011-2014 by Serguei Trouchelle E<lt>stro@cpan.orgE<gt>.
348  
349 Use and redistribution are under the same terms as Perl itself.
350  
351 =cut
« SQLite/DBI/Index.pm ↑ Summary SQLite/DBI.pm »