SQLite/DBI/Search.pm

filter: Statementstmt 91.4% 181 / 198 Branchbran 72.9% 70 / 96 Conditioncond 58.8% 10 / 17 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 100% 21 / 21 Podpod 50.0% 2 / 4 totaltotal 83.2% 289 / 347 SCAR 42.6
CC
57
Cov
84%
sql_statement
37.3
fetch_and_set
20.1
fetch
10.8
CRAP 70.5
« SQLite/Index.pm ↑ Summary SQLite/Search.pm »
1 # $Id: Search.pm 82 2020-05-30 06:14:27Z stro $
2  
3 package CPAN::SQLite::DBI::Search;
4  
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 BEGIN {
9 7 ▶ our $VERSION = '0.218';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 $CPAN::SQLite::DBI::Search::info::VERSION = $VERSION;
11 7 $CPAN::SQLite::DBI::Search::mods::VERSION = $VERSION;
12 7 $CPAN::SQLite::DBI::Search::dists::VERSION = $VERSION;
13 7 $CPAN::SQLite::DBI::Search::auths::VERSION = $VERSION;
14 }
15  
16 7 ▶use parent 'CPAN::SQLite::DBI';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use CPAN::SQLite::DBI qw($tables $dbh);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use CPAN::SQLite::Util qw($full_id);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 package CPAN::SQLite::DBI::Search::info;
21 7 ▶use parent 'CPAN::SQLite::DBI::Search';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 ▶use CPAN::SQLite::DBI qw($dbh);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 package CPAN::SQLite::DBI::Search::mods;
25 7 ▶use parent 'CPAN::SQLite::DBI::Search';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 7 ▶use CPAN::SQLite::DBI qw($dbh);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 package CPAN::SQLite::DBI::Search::dists;
29 7 ▶use parent 'CPAN::SQLite::DBI::Search';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 7 ▶use CPAN::SQLite::DBI qw($dbh);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 package CPAN::SQLite::DBI::Search::auths;
33 7 ▶use parent 'CPAN::SQLite::DBI::Search';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 7 ▶use CPAN::SQLite::DBI qw($dbh);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 package CPAN::SQLite::DBI::Search;
37 7 ▶use parent 'CPAN::SQLite::DBI';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 7 ▶use CPAN::SQLite::DBI qw($tables $dbh);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 7 ▶use CPAN::SQLite::Util qw($full_id download);
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 sub fetch {
42 871 ▶ my ($self, %args) = @_;
Subroutine fetch: called CC=10 SCAR=23.8
Pod documented
43 871 my $fields = $args{fields};
44 871 my $search = $args{search};
45 871 ▶ my @fields = ref($fields) eq 'ARRAY' ? @{$fields} : ($fields);
Branch: 1 branch
BranchTrueFalseTotal
ref $fields eq 'ARRAY' ? : 871 0 871
46 871 ▶ my $sql = $self->sql_statement(%args) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $sql = $self->sql_statement(%args) or do { $self->{"error"} = 'Error constructing sql statement: ' . $self->{"error"}; return } 0 871 871
47 0 $self->{error} = 'Error constructing sql statement: ' . $self->{error};
48 0 return;
49 };
50 871 ▶ my $sth = $dbh->prepare($sql) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $CPAN::SQLite::DBI::Search::dbh->prepare($sql) or do { $self->db_error; return } 0 871 871
51 0 $self->db_error();
52 0 return;
53 };
54 871 ▶ $sth->execute() or do {
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or do { $self->db_error($sth); return } 0 871 871
55 0 $self->db_error($sth);
56 0 return;
57 };
58  
59 871 ▶ if (not $search->{wantarray}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $search->{'wantarray'}) { } 646 225 871
60 646 my (%results, $results);
61 646 @results{@fields} = $sth->fetchrow_array;
62 646 ▶ $results = ($sth->rows == 0) ? undef : \%results;
Branch: 1 branch
BranchTrueFalseTotal
$sth->rows == 0 ? : 6 640 646
63 646 $sth->finish;
64 646 undef $sth;
65 646 ▶ $self->extra_info($results) if $results;
Branch: 1 branch
BranchTrueFalseTotal
if $results 640 6 646
66 646 return $results;
67 } else {
68 225 my (%hash, $results);
69 225 ▶ while (@hash{@fields} = $sth->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hash{@fields} = $sth->fetchrow_array) 2191 225 2416
70 2191 my %tmp = %hash;
71 2191 $self->extra_info(\%tmp);
72 2191 push @{$results}, \%tmp;
73 }
74 225 ▶ $results = undef if ($sth->rows == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $sth->rows == 0 3 222 225
75 225 $sth->finish;
76 225 undef $sth;
77 225 return $results;
78 }
79 }
80  
81 sub fetch_and_set {
82 3019 ▶ my ($self, %args) = @_;
Subroutine fetch_and_set: called CC=18 SCAR=30.0
Pod undocumented
83 3019 my $fields = $args{fields};
84 3019 my $search = $args{search};
85 3019 my $meta_obj = $args{meta_obj};
86 3019 ▶ die "Please supply a CPAN::SQLite::Meta::* object"
Branch: 1 branch
BranchTrueFalseTotal
unless $meta_obj and ref($meta_obj) =~ /^CPAN::SQLite::META/ 0 3019 3019
Condition33%1/3
$meta_obj and ref($meta_obj) =~ /^CPAN::SQLite::META/
!ll&&!rl&&r
003019
MC/DC0%0/2
$meta_obj and ref($meta_obj) =~ /^CPAN::SQLite::META/
$meta_obj ref($meta_obj) =~ /^CPAN::SQLite::META/
Truth table33%1/3
A and B
A= $meta_objB= ref($meta_obj) =~ /^CPAN::SQLite::META/
ABresult
0X0
100
111
87 unless ($meta_obj and ref($meta_obj) =~ /^CPAN::SQLite::META/);
88 3019 ▶ my @fields = ref($fields) eq 'ARRAY' ? @{$fields} : ($fields);
Branch: 1 branch
BranchTrueFalseTotal
ref $fields eq 'ARRAY' ? : 3019 0 3019
89 3019 ▶ my $sql = $self->sql_statement(%args) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $sql = $self->sql_statement(%args) or do { $self->{"error"} = 'Error constructing sql statement: ' . $self->{"error"}; return } 0 3019 3019
90 0 $self->{error} = 'Error constructing sql statement: ' . $self->{error};
91 0 return;
92 };
93 3019 ▶ my $sth = $dbh->prepare($sql) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $CPAN::SQLite::DBI::Search::dbh->prepare($sql) or do { $self->db_error; return } 0 3019 3019
94 0 $self->db_error();
95 0 return;
96 };
97 3019 ▶ $sth->execute() or do {
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or do { $self->db_error($sth); return } 0 3019 3019
98 0 $self->db_error($sth);
99 0 return;
100 };
101  
102 3019 my $want_ids = $args{want_ids};
103 3019 my $set_list = $args{set_list};
104 3019 my $download = $args{download};
105 3019 ▶ if (not $search->{wantarray}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $search->{'wantarray'}) { } 352 2667 3019
106 352 my (%results, %meta_results, $results);
107 352 @results{@fields} = $sth->fetchrow_array;
108 352 ▶ $results = ($sth->rows == 0) ? undef : \%results;
Branch: 1 branch
BranchTrueFalseTotal
$sth->rows == 0 ? : 0 352 352
109 352 $sth->finish;
110 352 undef $sth;
111 352 ▶ return unless $results;
Branch: 1 branch
BranchTrueFalseTotal
unless $results 0 352 352
112 352 $self->extra_info($results);
113 352 $meta_obj->set_data($results);
114  
115 352 ▶ if ($want_ids) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_ids) { } 6 346 352
116 6 $meta_results{dist_id} = $results{dist_id};
117 6 $meta_results{download} = download($results{cpanid}, $results{dist_file});
118 6 return \%meta_results;
119 } else {
120 346 return 1;
121 }
122 } else {
123 2667 my (%hash, $meta_results);
124 2667 ▶ while (@hash{@fields} = $sth->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hash{@fields} = $sth->fetchrow_array) 95126 2667 97793
125 95126 my %tmp = %hash;
126 95126 ▶ if ($set_list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set_list) { } 76311 18815 95126
127 76311 push @{$meta_results}, \%tmp;
128 } else {
129 18815 $self->extra_info(\%tmp);
130 18815 $meta_obj->set_data(\%tmp);
131 18815 ▶ if ($want_ids) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_ids) 13960 4855 18815
132 13960 my $download = download($tmp{cpanid}, $tmp{dist_file});
133 13960 push @{$meta_results},
134 {
135 dist_id => $tmp{dist_id},
136 13960 download => $download
137 };
138  
139 }
140 }
141 }
142 2667 ▶ $meta_results = undef if ($sth->rows == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $sth->rows == 0 0 2667 2667
143 2667 $sth->finish;
144 2667 undef $sth;
145 2667 ▶ return unless $meta_results;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta_results 2 2665 2667
146 2665 ▶ $meta_obj->set_list_data($meta_results, $download) if $set_list;
Branch: 1 branch
BranchTrueFalseTotal
if $set_list 2659 6 2665
147 2665 ▶ return $want_ids ? $meta_results : 1;
Branch: 1 branch
BranchTrueFalseTotal
$want_ids ? : 6 2659 2665
148 }
149 }
150  
151 sub extra_info {
152 21998 ▶ my ($self, $results) = @_;
Subroutine extra_info: called CC=3 SCAR=11.0
Pod undocumented
153 21998 ▶ if ($results->{cpanid} and $results->{dist_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($results->{"cpanid"} and $results->{"dist_file"}) 15313 6685 21998
Condition100%3/3
$results->{"cpanid"} and $results->{"dist_file"}
!ll&&!rl&&r
1471521415313
MC/DC100%2/2
$results->{"cpanid"} and $results->{"dist_file"}
$results->{"cpanid"} $results->{"dist_file"}
Truth table100%3/3
A and B
A= $results->{"cpanid"}B= $results->{"dist_file"}
ABresult
0X0
100
111
154 15313 $results->{download} = download($results->{cpanid}, $results->{dist_file});
155 }
156 21998 return;
157 }
158  
159 sub sql_statement {
160 3890 ▶ my ($self, %args) = @_;
Subroutine sql_statement: called CC=29 SCAR=36.2
Pod documented
161  
162 3890 my $search = $args{search};
163 3890 ▶ my $distinct = $search->{distinct} ? 'DISTINCT' : '';
Branch: 1 branch
BranchTrueFalseTotal
$search->{'distinct'} ? : 998 2892 3890
164 3890 my $table = $args{table};
165  
166 3890 my $fields = $args{fields};
167 3890 ▶ my @fields = ref($fields) eq 'ARRAY' ? @{$fields} : ($fields);
Branch: 1 branch
BranchTrueFalseTotal
ref $fields eq 'ARRAY' ? : 3890 0 3890
168 3890 for (@fields) {
169 15340 ▶ $_ = $full_id->{$_} if $full_id->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $CPAN::SQLite::DBI::Search::full_id->{$_} 2234 13106 15340
170 }
171  
172 3890 my $sql = qq{SELECT $distinct } . join(',', @fields);
173 3890 my $where = '';
174 3890 my $type = $search->{type};
175 QUERY: {
176 3890 ▶ ($type eq 'query') and do {
Branch: 1 branch
BranchTrueFalseTotal
$type eq "query" and do { my $value = $search->{"value"}; last QUERY if $value eq "^"; my $name = $search->{"name"}; my $text = $search->{"text"}; my $use_like = $value =~ /^\^?[A-Za-z0-9_\\\:\-]+$/ ? 1 : 0; my $prepend = "%"; if ($use_like and $value =~ /^\^/) { $prepend = ""; $value =~ s/^\^//; $value =~ s/\\//g; } ; $where = $use_like ? "$name LIKE '$prepend$value%'" : "$name REGEXP '(?i:$value)'"; if ($name eq "cpanid") { $where .= ($use_like ? " OR $text LIKE '$prepend$value%'" : " OR $text REGEXP '(?i:$value)'"); } ; last QUERY } 20 3870 3890
177 20 my $value = $search->{value};
178 20 ▶ last QUERY if ($value eq '^');
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "^" 0 20 20
179 20 my $name = $search->{name};
180 20 my $text = $search->{text};
181 20 ▶ my $use_like = ($value =~ /^\^?[A-Za-z0-9_\\\:\-]+$/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /^\^?[A-Za-z0-9_\\\:\-]+$/ ? : 11 9 20
182 20 my $prepend = '%';
183 20 ▶ if ($use_like and $value =~ /^\^/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_like and $value =~ /^\^/) 2 18 20
Condition100%3/3
$use_like and $value =~ /^\^/
!ll&&!rl&&r
992
MC/DC100%2/2
$use_like and $value =~ /^\^/
$use_like $value =~ /^\^/
Truth table100%3/3
A and B
A= $use_likeB= $value =~ /^\^/
ABresult
0X0
100
111
184 2 $prepend = '';
185 2 $value =~ s/^\^//;
186 2 $value =~ s{\\}{}g;
187 }
188 $where =
189 20 ▶ $use_like
Branch: 1 branch
BranchTrueFalseTotal
$use_like ? : 11 9 20
190 ? qq{$name LIKE '$prepend$value%'}
191 : qq{$name REGEXP '(?i:$value)'};
192  
193 20 ▶ if ($name eq 'cpanid') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "cpanid") 6 14 20
194 6 ▶ $where .=
Branch: 1 branch
BranchTrueFalseTotal
$use_like ? : 3 3 6
195 $use_like
196 ? qq{ OR $text LIKE '$prepend$value%'}
197 : qq{ OR $text REGEXP '(?i:$value)'};
198 }
199 20 last QUERY;
200 };
201 3870 ▶ ($type eq 'id') and do {
Branch: 1 branch
BranchTrueFalseTotal
$type eq "id" and do { $where = qq[ $search->{"id"} = $search->{"value"} ]; last QUERY } 2872 998 3870
202 2872 $where = qq{ $search->{id} = $search->{value} };
203 2872 last QUERY;
204 };
205 998 ▶ ($type eq 'name') and do {
Branch: 1 branch
BranchTrueFalseTotal
$type eq "name" and do { $where = qq[ $search->{"name"} = '$search->{"value"}' ]; last QUERY } 998 0 998
206 998 $where = qq{ $search->{name} = '$search->{value}' };
207 998 last QUERY;
208 };
209 0 warn qq{Unknown query type};
210 0 return;
211 }
212 3890 my $join;
213  
214 3890 $sql .= ' FROM ' . $table;
215 3890 ▶ my $left_join = $args{join} || $args{left_join};
Condition66%2/3
$args{'join'} || $args{'left_join'}
l!l&&r!l&&!r
66003230
MC/DC50%1/2
$args{'join'} || $args{'left_join'}
$args{'join'} $args{'left_join'}
Truth table66%2/3
A || B
A= $args{'join'}B= $args{'left_join'}
ABresult
1X1
011
000
216 3890 ▶ if ($left_join) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left_join) 660 3230 3890
217 660 ▶ if (ref($left_join) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $left_join eq "HASH") 660 0 660
218 660 foreach my $key (keys %$left_join) {
219 1216 my $id = $left_join->{$key};
220 1216 $sql .= " LEFT JOIN $key ON $table.$id=$key.$id ";
221 }
222 }
223 }
224  
225 3890 ▶ if ($where) {
Branch: 1 branch
BranchTrueFalseTotal
if ($where) { } 3890 0 3890
226 3890 $sql .= ' WHERE ( ' . $where . ' )';
227 3890 ▶ $sql .= ' AND (' . $join . ')' if $join;
Branch: 1 branch
BranchTrueFalseTotal
if $join 0 3890 3890
228 } else {
229 0 ▶ $sql .= ' WHERE (' . $join . ')' if $join;
Branch: 1 branch
BranchTrueFalseTotal
if $join 0 0 0
230 }
231  
232 3890 my $order_by = '';
233 3890 ▶ if (my $user_order_by = $args{order_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $user_order_by = $args{"order_by"}) 3890 0 3890
234 3890 ▶ $order_by = $order_by ? "$order_by,$user_order_by" : $user_order_by;
Branch: 1 branch
BranchTrueFalseTotal
$order_by ? : 0 3890 3890
235 }
236 3890 ▶ if ($order_by and $where) {
Branch: 1 branch
BranchTrueFalseTotal
if ($order_by and $where) 3890 0 3890
Condition33%1/3
$order_by and $where
!ll&&!rl&&r
003890
MC/DC0%0/2
$order_by and $where
$order_by $where
Truth table33%1/3
A and B
A= $order_byB= $where
ABresult
0X0
100
111
237 3890 $sql .= qq{ ORDER BY $order_by };
238 }
239  
240 3890 ▶ if (my $limit = $args{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $limit = $args{"limit"}) 0 3890 3890
241 my ($min, $max) =
242 ref($limit) eq 'HASH'
243 ? ($limit->{min} || 0, $limit->{max})
244 0 ▶ : (0, $limit);
Branch: 1 branch
BranchTrueFalseTotal
ref $limit eq 'HASH' ? : 0 0 0
Condition0%0/2
$limit->{"min"} or 0
l!l
00
MC/DC0%0/1
$limit->{"min"} or 0
$limit->{"min"}
Truth table0%0/2
A
A= $limit->{"min"}
Aresult
11
00
245 0 $sql .= qq{ LIMIT $min,$max };
246 }
247 3890 return $sql;
248 }
249  
250 7 1;
251  
252 =head1 NAME
253  
254 CPAN::SQLite::DBI::Search - DBI information for searching the CPAN::SQLite database
255  
256 =head1 VERSION
257  
258 version 0.218
259  
260 =head1 DESCRIPTION
261  
262 This module provides methods for L<CPAN::SQLite::Search> for searching
263 the C<CPAN::SQLite> database. There are two main methods.
264  
265 =over
266  
267 =item C<fetch>
268  
269 This takes information from C<CPAN::SQLite::Search> and sets up
270 a query on the database, returning the results found.
271  
272 =item C<sql_statement>
273  
274 This is used by the C<fetch> method to construct the appropriate
275 SQL statement.
276  
277 =back
278  
279 =head1 SEE ALSO
280  
281 L<CPAN::SQLite::Search>
282  
283 =cut
« SQLite/Index.pm ↑ Summary SQLite/Search.pm »