File Coverage

blib/lib/WWW/Scraper/Wikipedia/ISO3166/Database.pm
Criterion Covered Total %
statement 27 67 40.3
branch 0 8 0.0
condition n/a
subroutine 9 18 50.0
pod 8 9 88.8
total 44 102 43.1


line stmt bran cond sub pod time code
1             package WWW::Scraper::Wikipedia::ISO3166::Database;
2              
3 1     1   33501 use parent 'WWW::Scraper::Wikipedia::ISO3166';
  1         1  
  1         5  
4 1     1   40 use strict;
  1         1  
  1         15  
5 1     1   3 use warnings;
  1         1  
  1         16  
6              
7 1     1   643 use DBD::SQLite;
  1         19341  
  1         26  
8              
9 1     1   5 use DBI;
  1         7  
  1         31  
10              
11 1     1   463 use DBIx::Admin::CreateTable;
  1         2461  
  1         25  
12              
13 1     1   475 use File::Slurp; # For read_dir().
  1         7224  
  1         55  
14              
15 1     1   4 use Moo;
  1         1  
  1         4  
16              
17 1     1   176 use Types::Standard qw/Any HashRef Str/;
  1         1  
  1         7  
18              
19             has attributes =>
20             (
21             default => sub{return {AutoCommit => 1, RaiseError => 1, sqlite_unicode => 1} },
22             is => 'rw',
23             isa => HashRef,
24             required => 0,
25             );
26              
27             has creator =>
28             (
29             default => sub{return ''},
30             is => 'rw',
31             isa => Any,
32             required => 0,
33             );
34              
35             has dbh =>
36             (
37             default => sub{return ''},
38             is => 'rw',
39             isa => Any,
40             required => 0,
41             );
42              
43             has dsn =>
44             (
45             default => sub{return ''},
46             is => 'rw',
47             isa => Str,
48             required => 0,
49             );
50              
51             has engine =>
52             (
53             default => sub{return ''},
54             is => 'rw',
55             isa => Str,
56             required => 0,
57             );
58              
59             has password =>
60             (
61             default => sub{return ''},
62             is => 'rw',
63             isa => Str,
64             required => 0,
65             );
66              
67             has time_option =>
68             (
69             default => sub{return ''},
70             is => 'rw',
71             isa => Str,
72             required => 0,
73             );
74              
75             has username =>
76             (
77             default => sub{return ''},
78             is => 'rw',
79             isa => Str,
80             required => 0,
81             );
82              
83             our $VERSION = '1.04';
84              
85             # -----------------------------------------------
86              
87             sub BUILD
88             {
89 0     0 0   my($self) = @_;
90              
91 0           $self -> dsn('dbi:SQLite:dbname=' . $self -> sqlite_file);
92 0 0         $self -> dbh(DBI -> connect($self -> dsn, $self -> username, $self -> password, $self -> attributes) ) || die $DBI::errstr;
93 0           $self -> dbh -> do('PRAGMA foreign_keys = ON');
94              
95 0           $self -> creator
96             (
97             DBIx::Admin::CreateTable -> new
98             (
99             dbh => $self -> dbh,
100             verbose => 0,
101             )
102             );
103              
104 0 0         $self -> engine
105             (
106             $self -> creator -> db_vendor =~ /(?:Mysql)/i ? 'engine=innodb' : ''
107             );
108              
109 0 0         $self -> time_option
110             (
111             $self -> creator -> db_vendor =~ /(?:MySQL|Postgres)/i ? '(0) without time zone' : ''
112             );
113              
114             } # End of BUILD.
115              
116             # -----------------------------------------------
117              
118             sub find_subcountry_downloads
119             {
120 0     0 1   my($self) = @_;
121 0           my(@file) = read_dir('data');
122              
123 0           return [grep{length == 2} grep{s/^.+\.([A-Z]{2,2})\..+$/$1/; $_} @file];
  0            
  0            
  0            
124              
125             } # End of find_subcountry_downloads.
126              
127             # ----------------------------------------------
128              
129             sub get_country_count
130             {
131 0     0 1   my($self) = @_;
132              
133 0           return ($self -> dbh -> selectrow_array('select count(*) from countries') )[0];
134              
135             } # End of get_country_count.
136              
137             # -----------------------------------------------
138              
139             sub get_statistics
140             {
141 0     0 1   my($self) = @_;
142             my(%count) =
143             (
144             countries_in_db => $self -> get_country_count,
145 0           has_subcounties => $#{$self -> who_has_subcountries} + 1,
146             subcountries_in_db => $self -> get_subcountry_count,
147 0           subcountry_files_downloaded => scalar @{$self -> find_subcountry_downloads},
  0            
148             );
149              
150 0           return {%count};
151              
152             } # End of get_statistics.
153              
154             # ----------------------------------------------
155              
156             sub get_subcountry_count
157             {
158 0     0 1   my($self) = @_;
159              
160 0           return ($self -> dbh -> selectrow_array('select count(*) from subcountries') )[0];
161              
162             } # End of get_subcountry_count.
163              
164             # ----------------------------------------------
165              
166             sub read_countries_table
167             {
168 0     0 1   my($self) = @_;
169 0           my($sth) = $self -> dbh -> prepare('select * from countries');
170              
171 0           $sth -> execute;
172 0           $sth -> fetchall_hashref('id');
173              
174             } # End of read_countries_table.
175              
176             # ----------------------------------------------
177              
178             sub read_subcountries_table
179             {
180 0     0 1   my($self) = @_;
181 0           my($sth) = $self -> dbh -> prepare('select * from subcountries');
182              
183 0           $sth -> execute;
184 0           $sth -> fetchall_hashref('id');
185              
186             } # End of read_subcountries_table.
187              
188             # -----------------------------------------------
189              
190             sub report_statistics
191             {
192 0     0 1   my($self) = @_;
193 0           my($count) = $self -> get_statistics;
194              
195 0           $self -> log(info => $_) for map{"$_ => $$count{$_}"} sort keys %$count;
  0            
196              
197             } # End of report_statistics.
198              
199             # ----------------------------------------------
200              
201             sub who_has_subcountries
202             {
203 0     0 1   my($self) = @_;
204 0           my($countries) = $self -> read_countries_table;
205              
206 0           my(@has);
207              
208 0           for my $id (keys %$countries)
209             {
210 0 0         push @has, $id if ($$countries{$id}{has_subcountries} eq 'Yes');
211             }
212              
213 0           return [@has];
214              
215             } # End of who_has_subcountries.
216              
217             # -----------------------------------------------
218              
219             1;
220              
221             =pod
222              
223             =head1 NAME
224              
225             WWW::Scraper::Wikipedia::ISO3166::Database - The interface to www.scraper.wikipedia.iso3166.sqlite
226              
227             =head1 Synopsis
228              
229             See L for a long synopsis.
230              
231             =head1 Description
232              
233             Documents the methods end-users need to access the SQLite database,
234             I, which ships with this distro.
235              
236             See L for a long description.
237              
238             See scripts/export.as.csv.pl, scripts/export.as.html.pl and scripts/report.statistics.pl.
239              
240             =head1 Distributions
241              
242             This module is available as a Unix-style distro (*.tgz).
243              
244             See http://savage.net.au/Perl-modules.html for details.
245              
246             See http://savage.net.au/Perl-modules/html/installing-a-module.html for
247             help on unpacking and installing.
248              
249             =head1 Constructor and initialization
250              
251             new(...) returns an object of type C.
252              
253             This is the class's contructor.
254              
255             Usage: C<< WWW::Scraper::Wikipedia::ISO3166::Database -> new() >>.
256              
257             This method takes a hash of options.
258              
259             Call C as C<< new(option_1 => value_1, option_2 => value_2, ...) >>.
260              
261             Available options:
262              
263             =over 4
264              
265             =item o attributes => $hash_ref
266              
267             This is the hashref of attributes passed to L's I method.
268              
269             Default: {AutoCommit => 1, RaiseError => 1, sqlite_unicode => 1}
270              
271             =back
272              
273             =head1 Methods
274              
275             This module is a sub-class of L and consequently inherits its methods.
276              
277             =head2 attributes($hashref)
278              
279             Get or set the hashref of attributes passes to L's I method.
280              
281             Also, I is an option to L.
282              
283             =head2 find_subcountry_downloads()
284              
285             Returns an arrayref of 2-letter codes of countries whose subcountry page has been downloaded to
286             data/*$code2.html.
287              
288             =head2 get_country_count()
289              
290             Returns the result of: 'select count(*) from countries'.
291              
292             =head2 get_statistics()
293              
294             Returns a hashref of database statistics:
295              
296             {
297             countries_in_db => 249,
298             has_subcounties => 199,
299             subcountries_in_db => 4593,
300             subcountry_files_downloaded => 249,
301             }
302              
303             Called by L.
304              
305             =head2 get_subcountry_count()
306              
307             Returns the result of: 'select count(*) from subcountries'.
308              
309             =head2 new()
310              
311             See L.
312              
313             =head2 read_countries_table()
314              
315             Returns a hashref of hashrefs for this SQL: 'select * from countries'.
316              
317             The key of the hashref is the primary key (integer) of the I table.
318              
319             This is discussed further in L.
320              
321             =head2 read_subcountries_table()
322              
323             Returns a hashref of hashrefs for this SQL: 'select * from subcountries'.
324              
325             The key of the hashref is the primary key (integer) of the I table.
326              
327             This is discussed further in L.
328              
329             =head2 report_statistics()
330              
331             Logs various database statistics at the I level.
332              
333             Calls L.
334              
335             This is the output from scripts/report.statistics.pl -v 1:
336              
337             info: countries_in_db => 249.
338             info: has_subcounties => 199.
339             info: subcountries_in_db => 4593.
340             info: subcountry_files_downloaded => 249.
341              
342             =head2 verbose($integer)
343              
344             Get or set the verbosity level.
345              
346             Also, I is an option to L.
347              
348             =head2 who_has_subcountries()
349              
350             Returns an arrayref of primary keys (integers) in the I table, of those countries who have
351             subcountry entries in the I table.
352              
353             =head1 FAQ
354              
355             For the database schema, etc, see L.
356              
357             =head1 References
358              
359             See L.
360              
361             =head1 Support
362              
363             Email the author, or log a bug on RT:
364              
365             L.
366              
367             =head1 Author
368              
369             C was written by Ron Savage Iron@savage.net.auE> in 2012.
370              
371             Home page: L.
372              
373             =head1 Copyright
374              
375             Australian copyright (c) 2012 Ron Savage.
376              
377             All Programs of mine are 'OSI Certified Open Source Software';
378             you can redistribute them and/or modify them under the terms of
379             The Artistic License, a copy of which is available at:
380             http://www.opensource.org/licenses/index.html
381              
382              
383             =cut