DB/SQLite.pm

filter: Statementstmt 88.2% 75 / 85 Branchbran 70.8% 17 / 24 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 88.2% 15 / 17 Podpod 100% 5 / 5 totaltotal 83.0% 113 / 136 CC 17 SCAR 12.8
CC
17
Cov
83%
insert
8.3
_init
4.0
connect
3.1
CRAP 3.6
« Record.pm ↑ Summary
1 package Geo::GeoNames::DB::SQLite;
2  
3 =head1 NAME
4  
5 Geo::GeoNames::DB::SQLite - Perl module for handling GeoNames.org data stored in a SQLite database.
6  
7 =head1 SYNOPSIS
8  
9 use Geo::GeoNames::DB::SQLite;
10  
11 my $dbh = Geo::GeoNames::DB::SQLite->connect( "geoname.sqlite" );
12  
13 my @records = $dbh->query( "Beijing" );
14  
15 print join( "\n", @records ) . "\n";
16  
17 =head1 DESCRIPTION
18  
19 Geo::GeoNames::DB::SQLite is a Perl module to store GeoNames.org records,
20 which tries to balance the trade-offs between the memory cost of using a
21 Perl hash of Geo::GeoNames::Record objects and the speed of using using a
22 GeoNames.org data file.
23  
24 =head1 AUTHOR
25  
26 Xiangrui Meng <mengxr@stanford.edu>
27  
28 =head1 COPYRIGHT
29  
30 Copyright (C) 2010 by Xiangrui Meng
31  
32 This library is free software; you can redistribute it and/or modify
33 it under the same terms as Perl itself, either Perl version 5.8.8 or,
34 at your option, any later version of Perl 5 you may have available.
35  
36 =cut
37  
38 1 ▶use 5.008007;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 1 ▶use Geo::GeoNames::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 ▶use base qw(DBI::db);
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 =head1 METHODS
52  
53 =over
54  
55 =item connect()
56  
57 Constructor.
58  
59 my $dbh = Geo::GeoNames::DB::SQLite->connect( $dbname );
60  
61 =cut
62  
63 sub connect
64 2 ▶{
Subroutine connect: called CC=3 SCAR=11.4
Pod documented
65 2 my ( $class, $dbname ) = @_;
66
67 2 ▶ $class = (ref $class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
68  
69 2 ▶ my $self = DBI->connect( "dbi:SQLite:dbname=$dbname", "", "", {AutoCommit=>0} )
Branch: 1 branch
BranchTrueFalseTotal
my $self = "DBI"->connect("dbi:SQLite:dbname=$dbname", "", "", {"AutoCommit", 0}) or Carp::carp($DBI::errstr) 2 0 2
70 or Carp::carp( $DBI::errstr );
71  
72 2 $self->{sqlite_unicode} = 1;
73  
74 2 bless $self, $class;
75  
76 2 $self->_init;
77
78 2 return $self;
79 }
80  
81 =item insert()
82  
83 Insert or replace GeoNames.org records. It accepts Geo::GeoNames::Record
84 and Geo::GeoNames::File object(s) as input.
85  
86 Always remember to commit changes by
87  
88 $db->commit;
89  
90 =cut
91  
92 sub insert
93 2 ▶{
Subroutine insert: called CC=7 SCAR=21.2
Pod documented
94 2 my $self = shift;
95  
96 2 ▶ while ( my $data = shift )
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = shift()) 2 2 4
97 {
98 2 ▶ if ( ref( $data ) eq "Geo::GeoNames::Record" )
Branch: 3 branches
BranchTrueFalseTotal
if (ref $data eq 'Geo::GeoNames::Record') { } 1 1 2
elsif (ref $data eq 'Geo::GeoNames::File') { } 1 0 1
elsif (ref $data eq 'ARRAY') { } 0 0 0
99 {
100 1 $self->_insert( $data );
101 }
102 elsif ( ref( $data ) eq "Geo::GeoNames::File" )
103 {
104 1 ▶ while ( my $rec = $data->next() )
Branch: 1 branch
BranchTrueFalseTotal
if (my $rec = $data->next) 10 1 11
105 {
106 10 $self->_insert( $rec );
107 }
108 }
109 elsif ( ref( $data ) eq "ARRAY" )
110 {
111 0 foreach ( @$data )
112 {
113 0 $self->_insert( $_ );
114 }
115 }
116 else
117 {
118 0 Carp::carp( "Cannot recgonize input type!" );
119 }
120 }
121  
122 2 return $self;
123 }
124  
125 # insert or replace a single Geo::GeoNames::Record object
126  
127 sub _insert
128 11 ▶{
Subroutine _insert: called CC=3 SCAR=11.2
129 11 my ( $self, $record ) = @_;
130
131 11 ▶ if ( ref( $record ) eq "Geo::GeoNames::Record" )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $record eq 'Geo::GeoNames::Record') { } 11 0 11
132 {
133 11 $self->do( "INSERT OR REPLACE INTO geoname VALUES (" . join( ", ", map( $self->quote($_), @{$record}{@Geo::GeoNames::Record::fields} ) ) . ")" );
134
135 11 foreach ( $record->names() )
136 {
137 224 $self->do( "INSERT OR REPLACE INTO alternate_name (geonameid, alternate_name) VALUES ( $record->{geonameid}, " . $self->quote($_) . ")" );
138 }
139 }
140 else
141 {
142 0 Carp::carp( "Wrong type in insertion!" );
143 }
144
145 11 return $self;
146 }
147  
148 =item select_all_records()
149  
150 Select all records. (slow)
151  
152 =cut
153  
154 sub select_all_records
155 0 ▶{
Subroutine select_all_records: not called CC=1 SCAR=6.9
Pod documented
156 0 my $self = shift;
157  
158 0 my $records = $self->selectall_hashref( "SELECT * FROM geoname", "geonameid" );
159  
160 0 return map( bless($_, "Geo::GeoNames::Record"), values(%$records) );
161 }
162  
163 =item select_all_alternate_names()
164  
165 Select all the alternate names and corresponding geonameids.
166  
167 =cut
168  
169 sub select_all_alternate_names
170 0 ▶{
Subroutine select_all_alternate_names: not called CC=1 SCAR=6.9
Pod documented
171 0 my $self = shift;
172  
173 0 return $self->selectall_arrayref( "SELECT alternate_name, geonameid FROM alternate_name" );
174 }
175  
176 =item query()
177  
178 Query function.
179  
180 my @records = $dbh->query( $geonameid );
181 my @records = $dbh->query( $name1, $name2 );
182  
183 =cut
184  
185 sub query
186 1 ▶{
Subroutine query: called CC=3 SCAR=11.3
Pod documented
187 1 my $self = shift;
188  
189 1 my @records;
190  
191 1 foreach my $word (@_)
192 {
193 1 ▶ if ( $word =~ /^\d+$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ /^\d+$/) { } 0 1 1
194 {
195 0 push @records, $self->_query_id( $word );
196 }
197 else
198 {
199 1 push @records, $self->_query_name( $word );
200 }
201 }
202  
203 1 return @records;
204 }
205  
206 sub _query_id
207 1 ▶{
Subroutine _query_id: called CC=2 SCAR=7.0
208 1 my ( $self, $id ) = @_;
209
210 1 my $record = $self->selectrow_hashref( "SELECT * FROM geoname where geonameid = $id" );
211  
212 1 ▶ if( $record )
Branch: 1 branch
BranchTrueFalseTotal
if ($record) 1 0 1
213 {
214 1 bless $record, "Geo::GeoNames::Record";
215 }
216  
217 1 return $record;
218 }
219  
220 sub _query_name
221 1 ▶{
Subroutine _query_name: called CC=1 SCAR=0.0
222 1 my ( $self, $name ) = @_;
223  
224 1 $name = $self->quote($name);
225  
226 1 my $records = $self->selectall_hashref( "SELECT * FROM geoname WHERE geonameid IN (SELECT DISTINCT geonameid from alternate_name where alternate_name = $name)", "geonameid" );
227  
228 1 return map( bless($_, "Geo::GeoNames::Record"), values(%$records) );
229 }
230  
231  
232 # check and build database structure
233  
234 sub _init
235 2 ▶{
Subroutine _init: called CC=4 SCAR=13.9
236 2 my $self = shift;
237
238 # check tables
239
240 my @tbl_names = map( $_->[0],
241 2 @{$self->selectall_arrayref("SELECT name FROM sqlite_master WHERE type='table'")}
242 );
243
244 2 ▶ unless( grep {$_ eq "geoname";} @tbl_names )
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq "geoname";} @tbl_names) 1 1 2
245 {
246 1 $self->do( "CREATE TABLE geoname (geonameid INTEGER NOT NULL, name TEXT NOT NULL, asciiname TEXT NOT NULL, alternatenames TEXT, latitude REAL, longitude REAL, feature_class TEXT, feature_code TEXT, country_code TEXT, cc2 TEXT, admin1_code TEXT, admin2_code TEXT, admin3_code TEXT, admin4_code TEXT, population INTEGER, elevation INTEGER, gtopo30 INTEGER, timezone TEXT, modification_date TEXT, PRIMARY KEY (geonameid) )" );
247 }
248
249 2 ▶ unless( grep {$_ eq "alternate_name";} @tbl_names )
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq "alternate_name";} @tbl_names) 1 1 2
250 {
251 1 $self->do( "CREATE TABLE alternate_name (geonameid INTEGER NOT NULL, alternate_name TEXT NOT NULL, PRIMARY KEY (geonameid, alternate_name) )" );
252 }
253
254 # check index
255  
256 my @idx_names = map( $_->[0],
257 2 @{$self->selectall_arrayref("SELECT name FROM sqlite_master WHERE type='index'")}
258 );
259  
260 2 ▶ unless( grep {$_ eq "alternate_name_idx";} @idx_names )
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq "alternate_name_idx";} @idx_names) 1 1 2
261 {
262 1 $self->do( "CREATE INDEX alternate_name_idx ON alternate_name (alternate_name)" );
263 }
264  
265 2 return $self;
266 }
267  
268 =back
269  
270 =cut
271  
272 1 1;
273 __END__
« Record.pm ↑ Summary