ProcedureCall.pm

filter: Statementstmt 55.2% 84 / 152 Branchbran 23.7% 19 / 80 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 64.7% 11 / 17 Podpod 0.0% 0 / 1 totaltotal 45.6% 114 / 250 CC 44 SCAR 37.1
CC
44
Cov
44%
__bind_params
110.0
__fetch
72.0
__run_function
56.0
CRAP 40.7
« ProcedureCall/PostgreSQL.pm ↑ Summary ProcedureCall/CLI.pm »
1 package DBIx::ProcedureCall;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 3 our $VERSION = '0.11';
10  
11 3 our %__loaded_drivers;
12  
13 3 our %__known_attributes = qw~
14 procedure 1
15 function 1
16 cached 1
17 package 1
18 packaged 1
19 cursor 1
20 fetch() 1
21 fetch[] 1
22 fetch{} 1
23 fetch[[]] 1
24 fetch[{}] 1
25 table 1
26 boolean 1
27 ~;
28
29 1 ▶sub __run_procedure{
Subroutine __run_procedure: called CC=6 SCAR=27.7
30 1 my $dbh =$_[0];
31 1 ▶ croak "expected a database handle as first parameter, but got nothing" unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 1 1
32
33 # determine database type
34 1 my $dbtype = eval { $dbh->get_info(17); }; # 17 : SQL_DBMS_NAME
35 1 ▶ croak "could not determine the database type from $dbh: $@. Is that really a DBI database handle? " unless $dbtype;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbtype 1 0 1
36
37 0 my $name = $_[1];
38 0 ▶ croak "expected a procedure name to run against the database, but got nothing" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
39
40 # delegate to the driver
41 0 ▶ unless ($__loaded_drivers{$dbtype}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($DBIx::ProcedureCall::__loaded_drivers{$dbtype}) 0 0 0
42 0 ▶ eval "require DBIx::ProcedureCall::$dbtype; \$__loaded_drivers{$dbtype} = 1;"
Branch: 1 branch
BranchTrueFalseTotal
eval "require DBIx::ProcedureCall::$dbtype; \$__loaded_drivers{$dbtype} = 1;" or croak("failed to load driver for $dbtype database: $@") 0 0 0
43 or croak "failed to load driver for $dbtype database: $@";
44 }
45
46 0 "DBIx::ProcedureCall::$dbtype"->__run_procedure(@_);
47 }
48  
49 0 ▶sub __run_function{
Subroutine __run_function: not called CC=7 SCAR=40.3
50 0 my $dbh = $_[0];
51 0 ▶ croak "expected a database handle as first parameter, but got nothing" unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 0 0
52
53 # determine database type
54 0 my $dbtype = eval { $dbh->get_info(17); }; # 17 : SQL_DBMS_NAME
55 0 ▶ croak "could not determine the database type from $dbh: $@. Is that really a DBI database handle? " unless $dbtype;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbtype 0 0 0
56
57 0 my $name = $_[1];
58 0 ▶ croak "expected a function name to run against the database, but got nothing" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
59
60 0 my $attr = $_[2];
61
62 # delegate to the driver
63 0 ▶ unless ($__loaded_drivers{$dbtype}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($DBIx::ProcedureCall::__loaded_drivers{$dbtype}) 0 0 0
64 0 ▶ eval "require DBIx::ProcedureCall::$dbtype; \$__loaded_drivers{$dbtype} = 1;"
Branch: 1 branch
BranchTrueFalseTotal
eval "require DBIx::ProcedureCall::$dbtype; \$__loaded_drivers{$dbtype} = 1;" or croak("failed to load driver for $dbtype database: $@") 0 0 0
65 or croak "failed to load driver for $dbtype database: $@";
66 }
67 0 my $r = "DBIx::ProcedureCall::$dbtype"->__run_function(@_);
68 0 ▶ return $r unless $attr->{fetch};
Branch: 1 branch
BranchTrueFalseTotal
unless $attr->{"fetch"} 0 0 0
69
70 #fetch cursor
71 0 return __fetch($r, $attr, $dbtype);
72
73 }
74  
75 0 ▶sub __fetch{
Subroutine __fetch: not called CC=8 SCAR=42.8
76 0 my ($sth, $attr, $dbtype) = @_;
77 0 my $data;
78 0 ▶ if ($attr->{'fetch[[]]'} ) { $data = $sth->fetchall_arrayref; }
Branch: 5 branches
BranchTrueFalseTotal
if ($attr->{'fetch[[]]'}) { } 0 0 0
elsif ($attr->{'fetch()'}) { } 0 0 0
elsif ($attr->{'fetch[{}]'}) { } 0 0 0
elsif ($attr->{'fetch{}'}) { } 0 0 0
elsif ($attr->{'fetch[]'}) { } 0 0 0
79 elsif ($attr->{'fetch()'} ) {
80 0 my @data = $sth->fetchrow_array;
81 0 ▶ "DBIx::ProcedureCall::$dbtype"->__close($sth) if $attr->{cursor};
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"cursor"} 0 0 0
82 0 return @data;
83 }
84 0 elsif ($attr->{'fetch[{}]'} ) { $data = $sth->fetchall_arrayref({ }); }
85 0 elsif ($attr->{'fetch{}'} ) { $data = $sth->fetchrow_hashref; }
86 0 elsif ($attr->{'fetch[]'} ) { $data = $sth->fetchrow_arrayref; }
87
88 "DBIx::ProcedureCall::$dbtype"->__close($sth)
89 0 ▶ if $attr->{cursor};
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"cursor"} 0 0 0
90
91 0 return $data;
92 }
93  
94 0 ▶sub __bind_params{
Subroutine __bind_params: not called CC=10 SCAR=47.0
95 0 my ($sql, $start_index, $params) = @_;
96 0 my @binder;
97 0 ▶ if (ref $params eq 'ARRAY'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params eq 'ARRAY') { } 0 0 0
98 0 my $i = $start_index;
99 0 foreach (@$params){
100 # special bind options
101 0 ▶ if (ref $_ eq 'ARRAY'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 0 0 0
102 0 @binder = @$_;
103 }
104 else
105 {
106 0 @binder = ( $_ );
107 }
108 # INOUT parameters
109 0 ▶ if (ref $binder[0]){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $binder[0]) { } 0 0 0
110 # default MAXLEN 100
111 0 ▶ $binder[1] = 100 unless exists $binder[1];
Branch: 1 branch
BranchTrueFalseTotal
unless exists $binder[1] 0 0 0
112 0 $sql->bind_param_inout($i++, @binder);
113 }
114 else{
115 0 $sql->bind_param($i++, @binder);
116 }
117 }
118 }
119 else{
120 0 foreach (keys %$params){
121 # special bind options
122 0 my $p = $params->{$_};
123 0 ▶ if (ref $p eq 'ARRAY'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq 'ARRAY') { } 0 0 0
124 0 @binder = @$p;
125 }
126 else
127 {
128 0 @binder = ( $p );
129 }
130 # INOUT parameters
131 0 ▶ if (ref $binder[0]){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $binder[0]) { } 0 0 0
132 # default MAXLEN 100
133 0 ▶ $binder[1] = 100 unless exists $binder[1];
Branch: 1 branch
BranchTrueFalseTotal
unless exists $binder[1] 0 0 0
134 0 $sql->bind_param_inout(":$_", @binder);
135 }
136 else{
137 0 $sql->bind_param(":$_", @binder);
138 }
139 }
140 }
141 }
142  
143 1 ▶sub __run{
Subroutine __run: called CC=4 SCAR=14.8
144 1 my $w = shift;
145 1 my $name = shift;
146 1 my $attr = shift;
147 1 my $dbh = shift;
148 # check function/procedure attribute
149 1 ▶ $w = 0 if $attr->{function};
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"function"} 0 1 1
150 1 ▶ $w = undef if $attr->{procedure};
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"procedure"} 0 1 1
151 # in void context run a procedure
152 1 ▶ return __run_procedure($dbh, $name, $attr, @_) unless defined $w;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w 1 0 1
153 # in non-void context run a function
154 0 return __run_function($dbh, $name, $attr, @_);
155 }
156  
157 0 ▶sub run{
Subroutine run: not called CC=5 SCAR=34.0
Pod undocumented
158 0 my $dbh = shift;
159 0 my $n = shift;
160 0 my ($name, @attr) = split ':', $n;
161 0 my @err = grep { not exists $__known_attributes{lc $_} } @attr;
162 0 ▶ croak "tried to set unknown attributes (@err) for stored procedure '$name' " if @err;
Branch: 1 branch
BranchTrueFalseTotal
if @err 0 0 0
163
164 0 my %attr = map { (lc($_) => 1) } @attr;
165
166 # any fetch implies function
167 0 ▶ if ( grep /^fetch/, keys %attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^fetch/, keys %attr) 0 0 0
168 0 $attr{'function'} = 1;
169 0 $attr{'fetch'} = 1;
170 }
171
172 # cursor implies function
173 0 ▶ $attr{'function'} = 1 if $attr{'cursor'};
Branch: 1 branch
BranchTrueFalseTotal
if $attr{"cursor"} 0 0 0
174
175 # table implies function
176 0 ▶ $attr{'function'} = 1 if $attr{'table'};
Branch: 1 branch
BranchTrueFalseTotal
if $attr{"table"} 0 0 0
177
178
179 0 return __run(wantarray, $name, \%attr, $dbh, @_);
180 }
181  
182  
183 19 ▶sub import {
Subroutine import: called CC=10 SCAR=23.1
184 19 my $class = shift;
185 19 my $caller = (caller)[0];
186 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
187 19 foreach (@_) {
188 38 my ($name, @attr) = split ':';
189
190 38 my @err = grep { not exists $__known_attributes{lc $_} } @attr;
191 38 ▶ croak "tried to set unknown attributes (@err) for stored procedure '$name' " if @err;
Branch: 1 branch
BranchTrueFalseTotal
if @err 0 38 38
192
193 38 my %attr = map { (lc($_) => 1) } @attr;
194
195
196 # any fetch implies function
197 38 ▶ if ( grep /^fetch/, keys %attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^fetch/, keys %attr) 7 31 38
198 7 $attr{'function'} = 1;
199 7 $attr{'fetch'} = 1;
200 }
201
202 # cursor implies function
203 38 ▶ $attr{'function'} = 1 if $attr{'cursor'};
Branch: 1 branch
BranchTrueFalseTotal
if $attr{"cursor"} 3 35 38
204
205 # table implies function
206 38 ▶ $attr{'function'} = 1 if $attr{'table'};
Branch: 1 branch
BranchTrueFalseTotal
if $attr{"table"} 4 34 38
207
208 # boolean implies function
209 38 ▶ $attr{'function'} = 1 if $attr{'boolean'};
Branch: 1 branch
BranchTrueFalseTotal
if $attr{"boolean"} 2 36 38
210
211 38 ▶ if ($attr{'package'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($attr{"package"}) 3 35 38
212 3 delete $attr{'package'};
213 3 my $pkgname = $name;
214 3 $pkgname =~ s/\./::/g;
215 3 $pkgname =~ s/[^:\w]/_/g;
216 3 ▶ *{"${pkgname}::AUTOLOAD"} = sub {__pkg_autoload($name, \%attr, @_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
217 3 next;
218 }
219 35 ▶ if ($attr{'packaged'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($attr{"packaged"}) 6 29 35
220 6 delete $attr{'packaged'};
221 6 my @p = split '\.', $name;
222 6 ▶ die "cannot create a package for unpackaged procedure $name (name contains no dots)"
Branch: 1 branch
BranchTrueFalseTotal
unless @p > 1 0 6 6
223 unless @p>1;
224 6 my $subname = pop @p;
225 6 my $pkgname = join '::', @p;
226 6 $pkgname =~ s/[^:\w]/_/g;
227 6 $subname =~ s/[^:\w]/_/g;
228 0 ▶ *{"${pkgname}::$subname"} = sub {__run(wantarray,$name,\%attr, @_) };
Subroutine __ANON__: not called CC=1 SCAR=0.4
229 6 next;
230 }
231
232 29 my $subname = $name;
233 29 $subname =~ s/\W/_/g;
234 29 ▶ *{"${caller}::$subname"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
235 0 __run(wantarray,$name,\%attr, @_)
236 29 };
237 }
238 }
239  
240 1 ▶sub __pkg_autoload{
Subroutine __pkg_autoload: called CC=1 SCAR=0.0
241 1 my $name = shift;
242 1 my $attr = shift;
243 1 my $pkgname = $name;
244 1 $pkgname =~ s/\./::/g;
245 1 $pkgname =~ s/[^:\w]/_/g;
246 1 our $AUTOLOAD;
247 1 my @p = split '::', $AUTOLOAD;
248 1 my $subname = $p[-1];
249 1 $name = "$name.$subname";
250 1 ▶ my $sub = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
251 1 __run(wantarray,$name,$attr, @_);
252 1 };
253 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
254 1 *{"${pkgname}::$subname"} = $sub;
255 1 $sub->(@_);
256 }
257  
258  
259 3 1;
260 __END__
« ProcedureCall/PostgreSQL.pm ↑ Summary ProcedureCall/CLI.pm »