blib/lib/Perinci/Access/Schemeless/DBI.pm

filter: Statementstmt 78.7% 85 / 108 Branchbran 50.0% 24 / 48 Conditioncond 33.3% 11 / 33 MC/DCmcdc 5.0% 1 / 20 Subroutinesub 84.6% 11 / 13 Podpod 20.0% 1 / 5 totaltotal 58.5% 133 / 227 CC 37 SCAR 26.2
CC
37
Cov
63%
action_complete_arg_elem
30.0
action_complete_arg_val
20.7
action_list
17.7
CRAP 13.7
↑ Summary
1 package Perinci::Access::Schemeless::DBI;
2  
3 2 our $DATE = '2019-08-14'; # DATE
4 2 our $VERSION = '0.090'; # VERSION
5  
6 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use experimental 'smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 my $json = JSON::MaybeXS->new->allow_nonref;
13  
14 2 ▶use parent qw(Perinci::Access::Schemeless);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶sub new {
Subroutine new: called CC=9 SCAR=24.8
Pod documented
17 1 my $class = shift;
18 1 my $self = $class->SUPER::new(@_);
19  
20 # check required attributes
21 1 my $dbh = $self->{dbh};
22 1 ▶ die "Please specify required attribute 'dbh'" unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 1 1
23  
24 # if this looks like a table created by App::UpdateRinciMetadataDb, check
25 # its version
26 {
27 1 my @tt = $dbh->tables(undef, undef);
28 1 ▶ last unless grep {$_ eq 'meta' || $_ eq '"meta"' || $_ eq '"main"."meta"'} @tt;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "meta" or $_ eq "\"meta\"" or $_ eq "\"main\".\"meta\"";} @tt 0 1 1
Condition50%3/6
$_ eq 'meta' || $_ eq '"meta"' || $_ eq '"main"."meta"'
l!l&&r!l&&!r
025
$_ eq "meta" or $_ eq "\"meta\""
l!l&&r!l&&!r
007
MC/DC33%1/3
$_ eq 'meta' || $_ eq '"meta"' || $_ eq '"main"."meta"'
$_ eq "meta" $_ eq "\"meta\"" $_ eq '"main"."meta"'
Truth table50%2/4
A or B || C
A= $_ eq "meta"B= $_ eq "\"meta\""C= $_ eq '"main"."meta"'
ABCresult
1XX1
01X1
0011
0000
29  
30 1 my ($sch_ver) = $dbh->selectrow_array(
31 "SELECT value FROM meta WHERE name='schema_version'");
32 1 ▶ $sch_ver //= 0;
Condition50%1/2
$sch_ver //= 0
l!l
10
MC/DC0%0/1
$sch_ver //= 0
$sch_ver
Truth table50%1/2
A
A= $sch_ver
Aresult
11
00
33 1 ▶ if (!$sch_ver || $sch_ver !~ /^(2|3|4|5)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sch_ver and $sch_ver =~ /^(2|3|4|5)$/) 0 1 1
Condition33%1/3
$sch_ver and $sch_ver =~ /^(2|3|4|5)$/
!ll&&!rl&&r
001
MC/DC0%0/2
$sch_ver and $sch_ver =~ /^(2|3|4|5)$/
$sch_ver $sch_ver =~ /^(2|3|4|5)$/
Truth table33%1/3
A and B
A= $sch_verB= $sch_ver =~ /^(2|3|4|5)$/
ABresult
0X0
100
111
34 0 die "Database schema version ($sch_ver) not supported, only version 2-5 is supported";
35 }
36 }
37  
38 1 ▶ $self->{fallback_on_completion} //= 0;
Condition50%1/2
$self->{"fallback_on_completion"} //= 0
l!l
01
MC/DC0%0/1
$self->{"fallback_on_completion"} //= 0
$self->{"fallback_on_completion"}
Truth table50%1/2
A
A= $self->{"fallback_on_completion"}
Aresult
11
00
39  
40 1 $self;
41 }
42  
43 8 ▶sub get_meta {
Subroutine get_meta: called CC=4 SCAR=13.9
Pod undocumented
44 8 my ($self, $req) = @_;
45  
46 8 my $leaf = $req->{-uri_leaf};
47  
48 8 ▶ if (length $leaf) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $leaf) { } 7 1 8
49 my ($meta) = $self->{dbh}->selectrow_array(
50 "SELECT metadata FROM function WHERE package=? AND name=?", {},
51 7 $req->{-perl_package}, $leaf);
52 7 ▶ if ($meta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta) { } 6 1 7
53 6 $req->{-meta} = $json->decode($meta);
54 } else {
55 1 return [404, "No metadata found in database for package ".
56 "'$req->{-perl_package}' and function '$leaf'"];
57 }
58 } else {
59 # XXP check in database, if exists return if not return {v=>1.1}
60 my ($meta) = $self->{dbh}->selectrow_array(
61 "SELECT metadata FROM package WHERE name=?", {},
62 1 $req->{-perl_package});
63 1 ▶ if ($meta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta) { } 1 0 1
64 1 $req->{-meta} = $json->decode($meta);
65 } else {
66 0 $req->{-meta} = {v=>1.1}; # empty metadata for /
67 }
68 }
69 7 return;
70 }
71  
72 4 ▶sub action_list {
Subroutine action_list: called CC=14 SCAR=28.7
Pod undocumented
73 4 my ($self, $req) = @_;
74 4 my $detail = $req->{detail};
75 4 ▶ my $f_type = $req->{type} || "";
Condition50%1/2
$req->{'type'} || ''
l!l
04
MC/DC0%0/1
$req->{'type'} || ''
$req->{'type'}
Truth table50%1/2
A
A= $req->{'type'}
Aresult
11
00
76  
77 4 my @res;
78  
79 # XXX duplicated code with parent class
80 13 ▶ my $filter_path = sub {
Subroutine __ANON__: called CC=5 SCAR=20.9
81 13 my $path = shift;
82 13 ▶ if (defined($self->{allow_paths}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"allow_paths"} and not Perinci::Access::Schemeless::__match_paths2($path, $self->{"allow_paths"})) 0 13 13
Condition33%1/3
defined $self->{"allow_paths"} and not Perinci::Access::Schemeless::__match_paths2($path, $self->{"allow_paths"})
!ll&&!rl&&r
1300
MC/DC0%0/2
defined $self->{"allow_paths"} and not Perinci::Access::Schemeless::__match_paths2($path, $self->{"allow_paths"})
defined $self->{"allow_paths"} not Perinci::Access::Schemeless::__match_paths2($path, $self->{"allow_paths"})
Truth table33%1/3
A and not(B)
A= defined $self->{"allow_paths"}B= not Perinci::Access::Schemeless::__match_paths2($path, $self->{"allow_paths"})
ABresult
0X0
100
111
83 !Perinci::Access::Schemeless::__match_paths2($path, $self->{allow_paths})) {
84 0 return 0;
85 }
86 13 ▶ if (defined($self->{deny_paths}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"deny_paths"} and Perinci::Access::Schemeless::__match_paths2($path, $self->{"deny_paths"})) 0 13 13
Condition33%1/3
defined $self->{"deny_paths"} and Perinci::Access::Schemeless::__match_paths2($path, $self->{"deny_paths"})
!ll&&!rl&&r
1300
MC/DC0%0/2
defined $self->{"deny_paths"} and Perinci::Access::Schemeless::__match_paths2($path, $self->{"deny_paths"})
defined $self->{"deny_paths"} Perinci::Access::Schemeless::__match_paths2($path, $self->{"deny_paths"})
Truth table33%1/3
A and B
A= defined $self->{"deny_paths"}B= Perinci::Access::Schemeless::__match_paths2($path, $self->{"deny_paths"})
ABresult
0X0
100
111
87 Perinci::Access::Schemeless::__match_paths2($path, $self->{deny_paths})) {
88 0 return 0;
89 }
90 13 1;
91 4 };
92  
93 4 my $sth;
94 my %mem;
95  
96 4 my $pkg = $req->{-perl_package};
97  
98 # get subpackages
99 4 ▶ unless ($f_type && $f_type ne 'package') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($f_type and $f_type ne "package") 4 0 4
Condition33%1/3
$f_type and $f_type ne "package"
!ll&&!rl&&r
400
MC/DC0%0/2
$f_type and $f_type ne "package"
$f_type $f_type ne "package"
Truth table33%1/3
A and B
A= $f_typeB= $f_type ne "package"
ABresult
0X0
100
111
100 4 ▶ if (length $pkg) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $pkg) { } 4 0 4
101 $sth = $self->{dbh}->prepare(
102 4 "SELECT name FROM package WHERE name LIKE ? ORDER BY name");
103 4 $sth->execute("$pkg\::%");
104 } else {
105 $sth = $self->{dbh}->prepare(
106 0 "SELECT name FROM package ORDER BY name");
107 0 $sth->execute;
108 }
109 4 ▶ while (my $r = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $sth->fetchrow_hashref) 3 4 7
110 # strip pkg from name
111 3 my $m = substr($r->{name}, length($pkg));
112  
113 # strip :: prefix
114 3 $m =~ s/\A:://;
115  
116 # only take the first sublevel, e.g. if user requests 'foo::bar' and
117 # db lists 'foo::bar::baz::quux', then we only want 'baz'.
118 3 ($m) = $m =~ /(\w+)/;
119 3 $m .= "/";
120  
121 3 ▶ next if $mem{$m}++;
Branch: 1 branch
BranchTrueFalseTotal
if $mem{$m}++ 0 3 3
122  
123 3 ▶ if ($detail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($detail) { } 1 2 3
124 1 push @res, {uri=>$m, type=>"package"};
125 } else {
126 2 push @res, $m;
127 }
128 }
129 }
130  
131 # get all entities from this package. XXX currently only functions
132 4 my $dir = $req->{-uri_dir};
133 $sth = $self->{dbh}->prepare(
134 4 "SELECT name FROM function WHERE package=? ORDER BY name");
135 4 $sth->execute($req->{-perl_package});
136 4 ▶ while (my $r = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $sth->fetchrow_hashref) 13 4 17
137 13 my $e = $r->{name};
138 13 my $path = "$dir/$e";
139 13 ▶ next unless $filter_path->($path);
Branch: 1 branch
BranchTrueFalseTotal
unless &$filter_path($path) 0 13 13
140 13 ▶ my $t = $e =~ /^[%\@\$]/ ? 'variable' : 'function';
Branch: 1 branch
BranchTrueFalseTotal
$e =~ /^[%\@\$]/ ? : 0 13 13
141 13 ▶ next if $f_type && $f_type ne $t;
Branch: 1 branch
BranchTrueFalseTotal
if $f_type and $f_type ne $t 0 13 13
Condition33%1/3
$f_type and $f_type ne $t
!ll&&!rl&&r
1300
MC/DC0%0/2
$f_type and $f_type ne $t
$f_type $f_type ne $t
Truth table33%1/3
A and B
A= $f_typeB= $f_type ne $t
ABresult
0X0
100
111
142 13 ▶ if ($detail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($detail) { } 4 9 13
143 4 push @res, {
144 #v=>1.1,
145 uri=>$e, type=>$t,
146 };
147 } else {
148 9 push @res, $e;
149 }
150 }
151  
152 4 [200, "OK (list action)", \@res];
153 }
154  
155 0 ▶sub action_complete_arg_val {
Subroutine action_complete_arg_val: not called CC=5 SCAR=30.3
Pod undocumented
156 0 my ($self, $req) = @_;
157  
158 0 ▶ goto FALLBACK unless $self->{fallback_on_completion};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fallback_on_completion"} 0 0 0
159  
160 0 ▶ my $arg = $req->{arg} or return err(400, "Please specify arg");
Branch: 1 branch
BranchTrueFalseTotal
my $arg = $req->{"arg"} or return err(400, "Please specify arg") 0 0 0
161  
162 0 $self->get_meta($req);
163 0 my $c = $req->{-meta}{args}{$arg}{completion};
164 0 ▶ goto FALLBACK unless defined($c) && ref($c) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c and ref $c ne "CODE" 0 0 0
Condition0%0/3
defined $c and ref $c ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $c and ref $c ne "CODE"
defined $c ref $c ne "CODE"
Truth table0%0/3
A and B
A= defined $cB= ref $c ne "CODE"
ABresult
0X0
100
111
165  
166 # get meta from parent's get_meta
167 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
168 0 local *get_meta = \&Perinci::Access::Schemeless::get_meta;
169 0 delete $req->{-meta};
170  
171 0 FALLBACK:
172 $self->SUPER::action_complete_arg_val($req);
173 }
174  
175 0 ▶sub action_complete_arg_elem {
Subroutine action_complete_arg_elem: not called CC=5 SCAR=34.0
Pod undocumented
176 0 my ($self, $req) = @_;
177  
178 0 ▶ goto FALLBACK unless $self->{fallback_on_completion};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fallback_on_completion"} 0 0 0
179  
180 0 ▶ my $arg = $req->{arg} or return err(400, "Please specify arg");
Branch: 1 branch
BranchTrueFalseTotal
my $arg = $req->{"arg"} or return err(400, "Please specify arg") 0 0 0
181  
182 0 my $c = $req->{-meta}{$arg}{element_completion};
183 0 ▶ goto FALLBACK unless defined($c) && ref($c) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c and ref $c ne "CODE" 0 0 0
Condition0%0/3
defined $c and ref $c ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $c and ref $c ne "CODE"
defined $c ref $c ne "CODE"
Truth table0%0/3
A and B
A= defined $cB= ref $c ne "CODE"
ABresult
0X0
100
111
184  
185 # get meta from parent's get_meta
186 0 local *get_meta = \&Perinci::Access::Schemeless::get_meta;
187 0 delete $req->{-meta};
188  
189 0 FALLBACK:
190 $self->SUPER::action_complete_arg_elem($req);
191 }
192  
193 2 1;
194 # ABSTRACT: Subclass of Perinci::Access::Schemeless which gets lists of entities (and metadata) from DBI database
195  
196 __END__
↑ Summary