| 1 | # $Id: Search.pm 84 2020-05-31 06:29:34Z stro $ | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package CPAN::SQLite::Search; | ||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | no warnings qw(redefine); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 6 | our $VERSION = '0.219'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use English qw/-no_match_vars/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use CPAN::SQLite::Util qw($mode_info); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use CPAN::SQLite::DBI::Search; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 6 | our $max_results = 0; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 6 | my $cdbi_query; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | my %mode2obj; | ||||||||||||||||||||||||||||
| 21 | 6 | $mode2obj{$_} = __PACKAGE__ . '::' . $_ for (qw(dist author module)); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub new { | ||||||||||||||||||||||||||||
| 24 | 362 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 362 | $cdbi_query = CPAN::SQLite::DBI::Search->new(%args); | |||||||||||||||||||||||||||
| 26 | 362 | ▶ | $max_results = $args{max_results} if $args{max_results}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 362 | my $self = { results => undef, error => '', %args }; | |||||||||||||||||||||||||||
| 28 | 362 | return bless $self, $class; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub query { | ||||||||||||||||||||||||||||
| 32 | 1018 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
query: called CC=11 SCAR=28.8
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 1018 | ▶ | my $mode = $args{mode} || 'module'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'mode'} || 'module'
MC/DC0%0/1
$args{'mode'} || 'module'
$args{'mode'}
Truth table50%1/2
A
A= $args{'mode'}
| |||||||||||||||||||||||||||||
| 34 | 1018 | ▶ | unless ($mode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | $self->{error} = q{Please specify a 'mode' argument}; | |||||||||||||||||||||||||||
| 36 | 0 | return; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 1018 | my $info = $mode_info->{$mode}; | |||||||||||||||||||||||||||
| 39 | 1018 | my $table = $info->{table}; | |||||||||||||||||||||||||||
| 40 | 1018 | ▶ | unless ($table) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | $self->{error} = qq{No table exists for '$mode'}; | |||||||||||||||||||||||||||
| 42 | 0 | return; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 1018 | my $cdbi = $cdbi_query->{objs}->{$table}; | |||||||||||||||||||||||||||
| 45 | 1018 | my $class = 'CPAN::SQLite::DBI::Search::' . $table; | |||||||||||||||||||||||||||
| 46 | 1018 | ▶ | unless ($cdbi and ref($cdbi) eq $class) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$cdbi and ref $cdbi eq $class
MC/DC0%0/2
$cdbi and ref $cdbi eq $class
$cdbi
ref $cdbi eq $class
Truth table33%1/3
A and B
A= $cdbiB= ref $cdbi eq $class
| |||||||||||||||||||||||||||||
| 47 | 0 | $self->{error} = qq{No cdbi object exists for '$table'}; | |||||||||||||||||||||||||||
| 48 | 0 | return; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 1018 | my $obj; | |||||||||||||||||||||||||||
| 51 | 1018 | eval { $obj = $mode2obj{$mode}->make(table => $table, cdbi => $cdbi); }; | |||||||||||||||||||||||||||
| 52 | 1018 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | $self->{error} = qq{Mode '$mode' is not known}; | |||||||||||||||||||||||||||
| 54 | 0 | return; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 1018 | my $value; | |||||||||||||||||||||||||||
| 57 | my $search = { | ||||||||||||||||||||||||||||
| 58 | name => $info->{name}, | ||||||||||||||||||||||||||||
| 59 | text => $info->{text}, | ||||||||||||||||||||||||||||
| 60 | id => $info->{id}, | ||||||||||||||||||||||||||||
| 61 | 1018 | }; | |||||||||||||||||||||||||||
| 62 | TYPE: { | ||||||||||||||||||||||||||||
| 63 | 1018 | ▶ | ($value = $args{query}) and do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 20 | $search->{value} = $value; | |||||||||||||||||||||||||||
| 65 | 20 | $search->{type} = 'query'; | |||||||||||||||||||||||||||
| 66 | 20 | $search->{wantarray} = 1; | |||||||||||||||||||||||||||
| 67 | 20 | last TYPE; | |||||||||||||||||||||||||||
| 68 | }; | ||||||||||||||||||||||||||||
| 69 | 998 | ▶ | ($value = $args{id}) and do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | $search->{value} = $value; | |||||||||||||||||||||||||||
| 71 | 0 | $search->{type} = 'id'; | |||||||||||||||||||||||||||
| 72 | 0 | $search->{distinct} = 1; | |||||||||||||||||||||||||||
| 73 | 0 | last TYPE; | |||||||||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||||||||||
| 75 | 998 | ▶ | ($value = $args{name}) and do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 998 | $search->{value} = $value; | |||||||||||||||||||||||||||
| 77 | 998 | $search->{type} = 'name'; | |||||||||||||||||||||||||||
| 78 | 998 | $search->{distinct} = 1; | |||||||||||||||||||||||||||
| 79 | 998 | last TYPE; | |||||||||||||||||||||||||||
| 80 | }; | ||||||||||||||||||||||||||||
| 81 | 0 | $self->{error} = q{Cannot determine the type of search}; | |||||||||||||||||||||||||||
| 82 | 0 | return; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1018 | $obj->search(search => $search, meta_obj => $self->{meta_obj}); | |||||||||||||||||||||||||||
| 86 | 1018 | $self->{results} = $obj->{results}; | |||||||||||||||||||||||||||
| 87 | 1018 | ▶ | if (my $error = $obj->{error}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $self->{error} = $error; | |||||||||||||||||||||||||||
| 89 | 0 | return; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 1018 | return 1; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub make { | ||||||||||||||||||||||||||||
| 95 | 1018 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
make: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 96 | 1018 | for (qw(table cdbi)) { | |||||||||||||||||||||||||||
| 97 | 2036 | ▶ | die qq{Must supply an '$_' arg} unless defined $args{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | my $self = { | ||||||||||||||||||||||||||||
| 100 | results => undef, | ||||||||||||||||||||||||||||
| 101 | error => '', | ||||||||||||||||||||||||||||
| 102 | table => $args{table}, | ||||||||||||||||||||||||||||
| 103 | 1018 | cdbi => $args{cdbi} }; | |||||||||||||||||||||||||||
| 104 | 1018 | return bless $self, $class; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | package CPAN::SQLite::Search::author; | ||||||||||||||||||||||||||||
| 108 | 6 | ▶ | use parent 'CPAN::SQLite::Search'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | sub search { | ||||||||||||||||||||||||||||
| 111 | 358 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
search: called CC=15 SCAR=27.8
| |||||||||||||||||||||||||||||
| 112 | 358 | ▶ | return unless $args{search}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 358 | my $cdbi = $self->{cdbi}; | |||||||||||||||||||||||||||
| 114 | 358 | my $meta_obj = $args{meta_obj}; | |||||||||||||||||||||||||||
| 115 | 358 | $args{fields} = [qw(auth_id cpanid fullname email)]; | |||||||||||||||||||||||||||
| 116 | 358 | $args{table} = 'auths'; | |||||||||||||||||||||||||||
| 117 | 358 | ▶ | if ($max_results) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | $args{limit} = $max_results; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 358 | $args{order_by} = 'cpanid'; | |||||||||||||||||||||||||||
| 121 | 358 | my $results; | |||||||||||||||||||||||||||
| 122 | 358 | ▶ | return unless $results = ( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 123 | $meta_obj | ||||||||||||||||||||||||||||
| 124 | ? $cdbi->fetch_and_set(%args) | ||||||||||||||||||||||||||||
| 125 | : $cdbi->fetch(%args)); | ||||||||||||||||||||||||||||
| 126 | 353 | ▶ | unless ($meta_obj) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 7 | ▶ | $self->{results} = (ref($results) eq 'ARRAY' and scalar @$results == 1) ? $results->[0] : $results; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $results eq "ARRAY" and scalar @$results == 1
MC/DC0%0/2
ref $results eq "ARRAY" and scalar @$results == 1
ref $results eq "ARRAY"
scalar @$results == 1
Truth table66%2/3
A and B
A= ref $results eq "ARRAY"B= scalar @$results == 1
| |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 353 | ▶ | return 1 if $meta_obj; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # The following will get all the dists associated with the cpanid | ||||||||||||||||||||||||||||
| 132 | 7 | $args{join} = undef; | |||||||||||||||||||||||||||
| 133 | 7 | $args{table} = 'dists'; | |||||||||||||||||||||||||||
| 134 | 7 | $args{fields} = [qw(dist_file dist_abs)]; | |||||||||||||||||||||||||||
| 135 | 7 | $args{order_by} = 'dist_file'; | |||||||||||||||||||||||||||
| 136 | 7 | ▶ | my @items = (ref($results) eq 'ARRAY') ? @$results : ($results); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 7 | foreach my $item (@items) { | |||||||||||||||||||||||||||
| 138 | my $search = { | ||||||||||||||||||||||||||||
| 139 | id => 'auth_id', | ||||||||||||||||||||||||||||
| 140 | value => $item->{auth_id}, | ||||||||||||||||||||||||||||
| 141 | 13 | type => 'id', | |||||||||||||||||||||||||||
| 142 | wantarray => 1, | ||||||||||||||||||||||||||||
| 143 | }; | ||||||||||||||||||||||||||||
| 144 | 13 | my $dists; | |||||||||||||||||||||||||||
| 145 | 13 | ▶ | next unless ($dists = $cdbi->fetch(%args, search => $search)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 13 | ▶ | $item->{dists} = (ref($dists) eq 'ARRAY') ? $dists : [$dists]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 7 | ▶ | $self->{results} = (ref($results) eq 'ARRAY' and scalar @$results == 1) ? $results->[0] : $results; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $results eq "ARRAY" and scalar @$results == 1
MC/DC0%0/2
ref $results eq "ARRAY" and scalar @$results == 1
ref $results eq "ARRAY"
scalar @$results == 1
Truth table66%2/3
A and B
A= ref $results eq "ARRAY"B= scalar @$results == 1
| |||||||||||||||||||||||||||||
| 149 | 7 | return 1; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | package CPAN::SQLite::Search::module; | ||||||||||||||||||||||||||||
| 153 | 6 | ▶ | use parent 'CPAN::SQLite::Search'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | sub search { | ||||||||||||||||||||||||||||
| 156 | 556 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
search: called CC=13 SCAR=25.8
| |||||||||||||||||||||||||||||
| 157 | 556 | ▶ | return unless $args{search}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 556 | my $cdbi = $self->{cdbi}; | |||||||||||||||||||||||||||
| 159 | 556 | my $meta_obj = $args{meta_obj}; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | $args{fields} = [ | ||||||||||||||||||||||||||||
| 162 | 556 | qw(mod_id mod_name mod_abs mod_vers | |||||||||||||||||||||||||||
| 163 | dist_id dist_name dist_file dist_vers dist_abs | ||||||||||||||||||||||||||||
| 164 | auth_id cpanid fullname email) | ||||||||||||||||||||||||||||
| 165 | ]; | ||||||||||||||||||||||||||||
| 166 | 556 | $args{table} = 'dists'; | |||||||||||||||||||||||||||
| 167 | $args{join} = { | ||||||||||||||||||||||||||||
| 168 | 556 | mods => 'dist_id', | |||||||||||||||||||||||||||
| 169 | auths => 'auth_id', | ||||||||||||||||||||||||||||
| 170 | }; | ||||||||||||||||||||||||||||
| 171 | 556 | $args{order_by} = 'mod_name'; | |||||||||||||||||||||||||||
| 172 | 556 | ▶ | if ($max_results) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 0 | $args{limit} = $max_results; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | 556 | my $results; | |||||||||||||||||||||||||||
| 176 | 556 | ▶ | return unless $results = ( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 177 | $meta_obj | ||||||||||||||||||||||||||||
| 178 | ? $cdbi->fetch_and_set(%args, want_ids => 1) | ||||||||||||||||||||||||||||
| 179 | : $cdbi->fetch(%args)); | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # if running under CPAN.pm, need to build a list of modules | ||||||||||||||||||||||||||||
| 182 | # contained in the distribution | ||||||||||||||||||||||||||||
| 183 | 553 | ▶ | if ($meta_obj) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 6 | my %seen; | |||||||||||||||||||||||||||
| 185 | 6 | $args{join} = undef; | |||||||||||||||||||||||||||
| 186 | 6 | $args{table} = 'mods'; | |||||||||||||||||||||||||||
| 187 | 6 | ▶ | my @items = (ref($results) eq 'ARRAY') ? @$results : ($results); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 6 | foreach my $item (@items) { | |||||||||||||||||||||||||||
| 189 | 13401 | my $dist_id = $item->{dist_id}; | |||||||||||||||||||||||||||
| 190 | 13401 | ▶ | next if $seen{$dist_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 2057 | $args{fields} = [qw(mod_name mod_abs)]; | |||||||||||||||||||||||||||
| 192 | 2057 | $args{order_by} = 'mod_name'; | |||||||||||||||||||||||||||
| 193 | 2057 | $args{join} = undef; | |||||||||||||||||||||||||||
| 194 | my $search = { | ||||||||||||||||||||||||||||
| 195 | id => 'dist_id', | ||||||||||||||||||||||||||||
| 196 | value => $item->{dist_id}, | ||||||||||||||||||||||||||||
| 197 | 2057 | type => 'id', | |||||||||||||||||||||||||||
| 198 | wantarray => 1, | ||||||||||||||||||||||||||||
| 199 | }; | ||||||||||||||||||||||||||||
| 200 | 2057 | $seen{$dist_id}++; | |||||||||||||||||||||||||||
| 201 | 2057 | my $mods; | |||||||||||||||||||||||||||
| 202 | next unless $mods = $cdbi->fetch_and_set( | ||||||||||||||||||||||||||||
| 203 | %args, | ||||||||||||||||||||||||||||
| 204 | search => $search, | ||||||||||||||||||||||||||||
| 205 | set_list => 1, | ||||||||||||||||||||||||||||
| 206 | 2057 | ▶ | download => $item->{download}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | 553 | ▶ | unless ($meta_obj) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 547 | ▶ | $self->{results} = (ref($results) eq 'ARRAY' and scalar @$results == 1) ? $results->[0] : $results; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $results eq "ARRAY" and scalar @$results == 1
MC/DC0%0/2
ref $results eq "ARRAY" and scalar @$results == 1
ref $results eq "ARRAY"
scalar @$results == 1
Truth table66%2/3
A and B
A= ref $results eq "ARRAY"B= scalar @$results == 1
| |||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | 553 | return 1; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | package CPAN::SQLite::Search::dist; | ||||||||||||||||||||||||||||
| 216 | 6 | ▶ | use parent 'CPAN::SQLite::Search'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | sub search { | ||||||||||||||||||||||||||||
| 219 | 104 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
search: called CC=14 SCAR=26.7
| |||||||||||||||||||||||||||||
| 220 | 104 | ▶ | return unless $args{search}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 104 | my $cdbi = $self->{cdbi}; | |||||||||||||||||||||||||||
| 222 | 104 | my $meta_obj = $args{meta_obj}; | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | $args{fields} = [ | ||||||||||||||||||||||||||||
| 225 | 104 | qw(dist_id dist_name dist_abs dist_vers | |||||||||||||||||||||||||||
| 226 | dist_file auth_id cpanid fullname email) | ||||||||||||||||||||||||||||
| 227 | ]; | ||||||||||||||||||||||||||||
| 228 | 104 | $args{table} = 'dists'; | |||||||||||||||||||||||||||
| 229 | 104 | $args{join} = { auths => 'auth_id' }; | |||||||||||||||||||||||||||
| 230 | 104 | $args{order_by} = 'dist_name'; | |||||||||||||||||||||||||||
| 231 | 104 | ▶ | if ($max_results) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 0 | $args{limit} = $max_results; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | 104 | my $results; | |||||||||||||||||||||||||||
| 235 | 104 | ▶ | return unless $results = ( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 236 | $meta_obj | ||||||||||||||||||||||||||||
| 237 | ? $cdbi->fetch_and_set(%args, want_ids => 1) | ||||||||||||||||||||||||||||
| 238 | : $cdbi->fetch(%args)); | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 101 | $args{join} = undef; | |||||||||||||||||||||||||||
| 241 | 101 | $args{table} = 'mods'; | |||||||||||||||||||||||||||
| 242 | 101 | $args{fields} = [qw(mod_name mod_abs)]; | |||||||||||||||||||||||||||
| 243 | 101 | $args{order_by} = 'mod_name'; | |||||||||||||||||||||||||||
| 244 | 101 | ▶ | my @items = (ref($results) eq 'ARRAY') ? @$results : ($results); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 101 | foreach my $item (@items) { | |||||||||||||||||||||||||||
| 246 | my $search = { | ||||||||||||||||||||||||||||
| 247 | id => 'dist_id', | ||||||||||||||||||||||||||||
| 248 | value => $item->{dist_id}, | ||||||||||||||||||||||||||||
| 249 | 802 | type => 'id', | |||||||||||||||||||||||||||
| 250 | wantarray => 1, | ||||||||||||||||||||||||||||
| 251 | }; | ||||||||||||||||||||||||||||
| 252 | 802 | my $mods; | |||||||||||||||||||||||||||
| 253 | next | ||||||||||||||||||||||||||||
| 254 | unless $mods = ( | ||||||||||||||||||||||||||||
| 255 | $meta_obj | ||||||||||||||||||||||||||||
| 256 | ? $cdbi->fetch_and_set( | ||||||||||||||||||||||||||||
| 257 | %args, | ||||||||||||||||||||||||||||
| 258 | search => $search, | ||||||||||||||||||||||||||||
| 259 | set_list => 1, | ||||||||||||||||||||||||||||
| 260 | download => $item->{download}) | ||||||||||||||||||||||||||||
| 261 | 802 | ▶ | : $cdbi->fetch(%args, search => $search)); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 262 | 802 | ▶ | next if $meta_obj; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 200 | ▶ | $item->{mods} = (ref($mods) eq 'ARRAY') ? $mods : [$mods]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | 101 | ▶ | unless ($meta_obj) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | 95 | ▶ | $self->{results} = (ref($results) eq 'ARRAY' and scalar @$results == 1) ? $results->[0] : $results; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $results eq "ARRAY" and scalar @$results == 1
MC/DC0%0/2
ref $results eq "ARRAY" and scalar @$results == 1
ref $results eq "ARRAY"
scalar @$results == 1
Truth table66%2/3
A and B
A= ref $results eq "ARRAY"B= scalar @$results == 1
| |||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | 101 | return 1; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 6 | 1; | |||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | =head1 NAME | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | CPAN::SQLite::Search - perform queries on the database | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =head1 VERSION | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | version 0.219 | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | my $max_results = 200; | ||||||||||||||||||||||||||||
| 284 | my $query = CPAN::SQLite::Search->new(db_dir => $db_dir, | ||||||||||||||||||||||||||||
| 285 | db_name => $db_name, | ||||||||||||||||||||||||||||
| 286 | max_results => $max_results); | ||||||||||||||||||||||||||||
| 287 | $query->query(mode => 'module', name => 'Net::FTP'); | ||||||||||||||||||||||||||||
| 288 | my $results = $query->{results}; | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | =head1 CONSTRUCTING THE QUERY | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | This module queries the database via various types of queries | ||||||||||||||||||||||||||||
| 293 | and returns the results for subsequent display. The | ||||||||||||||||||||||||||||
| 294 | C<CPAN::SQLite::Search> object is created via the C<new> method as | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | my $query = CPAN::SQLite::Search->new(db_dir => $db_dir, | ||||||||||||||||||||||||||||
| 297 | db_name => $db_name, | ||||||||||||||||||||||||||||
| 298 | max_results => $max_results); | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | which takes as arguments | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | =over 3 | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | =item * db_dir =E<gt> $db_dir | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | This is the directory where the database file is stored. This is | ||||||||||||||||||||||||||||
| 307 | optional if the C<CPAN> option is given. | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | =item * CPAN =E<gt> $CPAN | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | This option specifies the C<cpan_home> directory of an | ||||||||||||||||||||||||||||
| 312 | already configured CPAN.pm, which is where the database | ||||||||||||||||||||||||||||
| 313 | file will be stored if C<db_dir> is not given. | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =item * max_results =E<gt> $max_results | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | This is the maximum value used to limit the number of results | ||||||||||||||||||||||||||||
| 318 | returned under a user query. If not specified, a value contained | ||||||||||||||||||||||||||||
| 319 | within C<CPAN::SQLite::Search> will be used. | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | =back | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | A basic query then is constructed as | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | $query->query(mode => $mode, $type => $value); | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | with the results available as | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | my $results = $query->{results} | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | There are three basic modes: | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | =over 3 | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | =item * module | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | This is for information on modules. | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | =item * dist | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | This is for information on distributions. | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | =item * author | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | This is for information on CPAN authors or cpanids. | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | =back | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =head2 C<module>, C<dist>, and C<author> modes | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | For a mode of C<module>, C<dist>, and C<author>, there are | ||||||||||||||||||||||||||||
| 352 | four basic options to be used for the C<$type =E<gt> $value> option: | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | =over 3 | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =item * query =E<gt> $query_term | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | This will search through module names, | ||||||||||||||||||||||||||||
| 359 | distribution names, or CPAN author names and ids | ||||||||||||||||||||||||||||
| 360 | (for C<module>, C<dist>, and C<author> modes | ||||||||||||||||||||||||||||
| 361 | respectively). The results are case insensitive, | ||||||||||||||||||||||||||||
| 362 | and Perl regular expressions for the C<$query_term> | ||||||||||||||||||||||||||||
| 363 | are recognized. | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =item * name =E<gt> $name | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | This will report exact matches (in a case sensitive manner) | ||||||||||||||||||||||||||||
| 368 | for the module name, distribution name, or CPAN author id, | ||||||||||||||||||||||||||||
| 369 | for C<module>, C<dist>, and C<author> modes | ||||||||||||||||||||||||||||
| 370 | respectively. | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | =item * id =E<gt> $id | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | This will look up information on the primary key according | ||||||||||||||||||||||||||||
| 375 | to the mode specified. This is more for internal use, | ||||||||||||||||||||||||||||
| 376 | to help speed up queries; using this "publically" is | ||||||||||||||||||||||||||||
| 377 | probably not a good idea, as the ids may change over the | ||||||||||||||||||||||||||||
| 378 | course of time. | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | =back | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =head1 RESULTS | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | After making the query, the results can be accessed through | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | my $results = $query->{results}; | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | No results either can mean no matches were found, or | ||||||||||||||||||||||||||||
| 389 | else an error in making the query resulted (in which case, | ||||||||||||||||||||||||||||
| 390 | a brief error message is contained in C<$query-E<gt>{error}>). | ||||||||||||||||||||||||||||
| 391 | Assuming there are results, what is returned depends on | ||||||||||||||||||||||||||||
| 392 | the mode and on the type of query. See L<CPAN::SQLite::Populate> | ||||||||||||||||||||||||||||
| 393 | for a description of the fields in the various tables | ||||||||||||||||||||||||||||
| 394 | listed below - these fields are used as the keys of the | ||||||||||||||||||||||||||||
| 395 | hash references that arise. | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | =head2 C<author> mode | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | =over 3 | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =item * C<name> or C<id> query | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | This returns the C<auth_id>, C<cpanid>, C<email>, and C<fullname> | ||||||||||||||||||||||||||||
| 404 | of the C<auths> table. As well, an array reference | ||||||||||||||||||||||||||||
| 405 | C<$results-E<gt>{dists}> is returned representing | ||||||||||||||||||||||||||||
| 406 | all distributions associated with that C<cpanid> - each | ||||||||||||||||||||||||||||
| 407 | member of the array reference is a hash reference | ||||||||||||||||||||||||||||
| 408 | describing the C<dist_id>, C<dist_name>, | ||||||||||||||||||||||||||||
| 409 | C<dist_abs>, C<dist_vers>, and C<dist_file> fields in the | ||||||||||||||||||||||||||||
| 410 | C<dists> table. An additional entry, C<download>, is | ||||||||||||||||||||||||||||
| 411 | supplied, which can be used as C<$CPAN/authors/id/$download> | ||||||||||||||||||||||||||||
| 412 | to specify the url of the distribution. | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | =item * C<query> query | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | If this results in more than one match, an array reference | ||||||||||||||||||||||||||||
| 417 | is returned, each member of which is a hash reference containing | ||||||||||||||||||||||||||||
| 418 | the C<auth_id>, C<cpanid>, and C<fullname> fields. If there | ||||||||||||||||||||||||||||
| 419 | is only one result found, a C<name> query based on the | ||||||||||||||||||||||||||||
| 420 | matched C<cpanid> is performed. | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =back | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | =head2 C<module> mode | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | =over 3 | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | =item * C<name> or C<id> query | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | This returns the C<mod_id>, C<mod_name>, C<mod_abs>, C<mod_vers>, | ||||||||||||||||||||||||||||
| 431 | C<dist_id>, C<dist_name>, C<dist_file>, | ||||||||||||||||||||||||||||
| 432 | C<auth_id>, C<cpanid>, C<fullname>, and C<email> | ||||||||||||||||||||||||||||
| 433 | of the C<auths>, C<mods>, and C<dists> tables. | ||||||||||||||||||||||||||||
| 434 | As well, the following entries may be present. | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | =over 3 | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | =item * C<download> | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | This can be used as C<$CPAN/authors/id/$download> | ||||||||||||||||||||||||||||
| 441 | to specify the url of the distribution. | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | =back | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | =item * C<query> query | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | If this results in more than one match, an array reference | ||||||||||||||||||||||||||||
| 448 | is returned, each member of which is a hash reference containing | ||||||||||||||||||||||||||||
| 449 | the C<mod_id>, C<mod_name>, C<mod_abs>, C<mod_abs>, C<dist_vers>, C<dist_abs>, | ||||||||||||||||||||||||||||
| 450 | C<auth_id>, C<cpanid>, C<dist_id>, C<dist_name>, and C<dist_file>. | ||||||||||||||||||||||||||||
| 451 | As well, a C<download> field which | ||||||||||||||||||||||||||||
| 452 | can be used as C<$CPAN/authors/id/$download> | ||||||||||||||||||||||||||||
| 453 | to specify the url of the distribution is provided. If there | ||||||||||||||||||||||||||||
| 454 | is only one result found, a C<name> query based on the | ||||||||||||||||||||||||||||
| 455 | matched C<mod_name> is performed. | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | =back | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | =head2 C<dist> mode | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | =over 3 | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | =item * C<name> or C<id> query | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | This returns the C<dist_id>, C<dist_name>, C<dist_abs>, C<dist_vers>, | ||||||||||||||||||||||||||||
| 466 | C<dist_file>, C<size>, C<birth>, C<auth_id>, C<cpanid>, and C<fullname> | ||||||||||||||||||||||||||||
| 467 | of the C<auths>, C<mods>, and C<dists> tables. | ||||||||||||||||||||||||||||
| 468 | As well, the following entries may be present. | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | =over 3 | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | =item * C<download> | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | This can be used as C<$CPAN/authors/id/$download> | ||||||||||||||||||||||||||||
| 475 | to specify the url of the distribution. | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | =item * C<mods> | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | This is an array reference containing information on the | ||||||||||||||||||||||||||||
| 480 | modules present. Each entry is a hash reference containing the | ||||||||||||||||||||||||||||
| 481 | C<mod_id>, C<mod_name>, C<mod_abs>, and C<mod_vers> | ||||||||||||||||||||||||||||
| 482 | fields for the module. | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | =back | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | =item * C<query> query | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | If this results in more than one match, an array reference | ||||||||||||||||||||||||||||
| 489 | is returned, each member of which is a hash reference containing | ||||||||||||||||||||||||||||
| 490 | the C<dist_id>, C<dist_name>, C<dist_abs>, C<dist_file>, | ||||||||||||||||||||||||||||
| 491 | and C<cpanid> fields. As well, a C<download> field which | ||||||||||||||||||||||||||||
| 492 | can be used as C<$CPAN/authors/id/$download> | ||||||||||||||||||||||||||||
| 493 | to specify the url of the distribution is provided. If there | ||||||||||||||||||||||||||||
| 494 | is only one result found, a C<name> query based on the | ||||||||||||||||||||||||||||
| 495 | matched C<dist_name> is performed. | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | =back | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | L<CPAN::SQLite::Populate>. | ||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | Randy Kobes (passed away on September 18, 2010) | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | Serguei Trouchelle E<lt>stro@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | Copyright 2006,2008 by Randy Kobes E<lt>r.kobes@uwinnipeg.caE<gt>. | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | Copyright 2011-2013 by Serguei Trouchelle E<lt>stro@cpan.orgE<gt>. | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | Use and redistribution are under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | =cut | ||||||||||||||||||||||||||||