Connector/Proxy/DBI.pm

filter: Statementstmt 84.3% 108 / 128 Branchbran 60.0% 18 / 30 Conditioncond 60.0% 12 / 20 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 85.7% 12 / 14 Podpod 100% 6 / 6 totaltotal 75.8% 160 / 211 CC 24 SCAR 17.6
CC
24
Cov
78%
get
8.5
get_hash
8.1
get_list
6.8
CRAP 5.8
« Connector/Multi/Merge.pm ↑ Summary Connector/Multi/YAML.pm »
1 # Connector::Proxy::DBI
2 #
3 # Proxy class for fetching a value from using DBI
4 #
5 # Written by Oliver Welter and Martin Bartosch for the OpenXPKI project 2013
6  
7 package Connector::Proxy::DBI;
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 extends 'Connector::Proxy';
17  
18  
19 1 has dbuser => (
20 is => 'rw',
21 isa => 'Str',
22 );
23  
24 1 has password => (
25 is => 'rw',
26 isa => 'Str',
27 );
28  
29 1 has table => (
30 is => 'rw',
31 isa => 'Str',
32 );
33  
34 1 has column => (
35 is => 'rw',
36 isa => 'Str|HashRef',
37 );
38  
39 1 has condition => (
40 is => 'rw',
41 isa => 'Str',
42 );
43  
44 1 has ambiguous => (
45 is => 'rw',
46 isa => 'Str',
47 default => 'empty',
48 );
49  
50 1 has _dbi => (
51 is => 'ro',
52 isa => 'Object',
53 lazy => 1,
54 builder => '_dbi_handle'
55 );
56  
57  
58 3 ▶sub _dbi_handle {
Subroutine _dbi_handle: called CC=3 SCAR=12.3
59  
60 3 my $self = shift;
61  
62 3 my $dsn = $self->LOCATION();
63  
64 3 my $dbh;
65 3 eval {
66 3 $dbh = DBI->connect($dsn, $self->dbuser(), $self->password(),
67 { RaiseError => 1, LongReadLen => 1024 });
68 };
69  
70 3 ▶ if ($EVAL_ERROR || !$dbh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Connector::Proxy::DBI::EVAL_ERROR or not $dbh) 0 3 3
Condition33%1/3
$Connector::Proxy::DBI::EVAL_ERROR or not $dbh
l!l&&r!l&&!r
003
MC/DC0%0/2
$Connector::Proxy::DBI::EVAL_ERROR or not $dbh
$Connector::Proxy::DBI::EVAL_ERROR not $dbh
Truth table33%1/3
A or not(B)
A= $Connector::Proxy::DBI::EVAL_ERRORB= not $dbh
ABresult
1X1
011
000
71 0 $self->log()->error('DBI connect failed. DSN: '.$dsn. ' - Error: ' . $EVAL_ERROR );
72 0 die "DBI connect failed."
73 }
74 3 return $dbh;
75  
76 }
77  
78 5 ▶sub get {
Subroutine get: called CC=8 SCAR=21.4
Pod documented
79  
80 5 my $self = shift;
81 5 my @path = $self->_build_path( shift );
82  
83 5 my $column = $self->column();
84 5 ▶ if (!$column || ref $column ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $column or ref $column ne "") 0 5 5
Condition33%1/3
not $column or ref $column ne ""
l!l&&r!l&&!r
005
MC/DC0%0/2
not $column or ref $column ne ""
not $column ref $column ne ""
Truth table33%1/3
not(A) or B
A= not $columnB= ref $column ne ""
ABresult
1X1
011
000
85 0 die "column must be a singe column name when using get";
86 }
87  
88 5 my $query = sprintf "SELECT %s FROM %s WHERE %s",
89 $column, $self->table(), $self->condition();
90  
91 5 $self->log()->debug('Query is ' . $query);
92  
93 5 my $sth = $self->_dbi()->prepare($query);
94 5 $sth->execute( @path );
95  
96 5 my $row = $sth->fetchrow_arrayref();
97  
98 5 $self->log()->trace('result is ' . Dumper $row );
99  
100 5 my $result;
101 5 ▶ if (!$row) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $row) { } 0 5 5
elsif ($self->ambiguous ne 'return' and $sth->fetchrow_arrayref) { } 2 4 6
102 0 return $self->_node_not_exists( @path );
103  
104 ▶ } elsif (($self->ambiguous() ne 'return') && $sth->fetchrow_arrayref()) {
Condition100%3/3
$self->ambiguous ne "return" and $sth->fetchrow_arrayref
!ll&&!rl&&r
122
MC/DC100%2/2
$self->ambiguous ne "return" and $sth->fetchrow_arrayref
$self->ambiguous ne "return" $sth->fetchrow_arrayref
Truth table100%3/3
A and B
A= $self->ambiguous ne "return"B= $sth->fetchrow_arrayref
ABresult
0X0
100
111
105  
106 2 $self->log()->error('Ambiguous (multi-valued) result');
107 2 ▶ if ($self->ambiguous() eq 'die') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ambiguous eq "die") 1 1 2
108 1 die "Ambiguous (multi-valued) result";
109 }
110 1 return $self->_node_not_exists( @path );
111  
112 }
113  
114 3 ▶ $self->log()->debug('Valid return: ' . ($row->[0] // '<undef>'));
Condition50%1/2
$row->[0] // "<undef>"
l!l
30
MC/DC0%0/1
$row->[0] // "<undef>"
$row->[0]
Truth table50%1/2
A
A= $row->[0]
Aresult
11
00
115 3 return $row->[0];
116  
117 }
118  
119 6 ▶sub get_hash {
Subroutine get_hash: called CC=8 SCAR=20.9
Pod documented
120  
121 6 my $self = shift;
122 6 my @path = $self->_build_path( shift );
123  
124 6 my $column = $self->column();
125 6 ▶ if ($column && ref $column ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($column and ref $column ne "HASH") 0 6 6
Condition66%2/3
$column and ref $column ne "HASH"
!ll&&!rl&&r
510
MC/DC0%0/2
$column and ref $column ne "HASH"
$column ref $column ne "HASH"
Truth table66%2/3
A and B
A= $columnB= ref $column ne "HASH"
ABresult
0X0
100
111
126 0 die "column must be a hashref or empty when using get_hash";
127 }
128  
129 6 my $columns = '*';
130 6 ▶ if (ref $column eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $column eq "HASH") 1 5 6
131 1 my @cols;
132 map {
133 3 push @cols, sprintf( "%s as %s", $column->{$_}, $_ );
134 1 } keys %{$column};
135 1 $columns = join(",", @cols);
136 }
137  
138 6 my $query = sprintf "SELECT %s FROM %s WHERE %s",
139 $columns, $self->table(), $self->condition();
140  
141 6 $self->log()->debug('Query is ' . $query);
142  
143 6 my $sth = $self->_dbi()->prepare($query);
144 6 $sth->execute( @path );
145  
146 6 my $row = $sth->fetchrow_hashref();
147  
148 6 $self->log()->trace('result is ' . Dumper $row );
149  
150 6 ▶ if (!$row) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $row) { } 1 5 6
elsif ($self->ambiguous ne 'return' and $sth->fetchrow_hashref) { } 1 6 7
151 1 return $self->_node_not_exists( @path );
152  
153 ▶ } elsif (($self->ambiguous() ne 'return') && $sth->fetchrow_hashref()) {
Condition100%3/3
$self->ambiguous ne "return" and $sth->fetchrow_hashref
!ll&&!rl&&r
221
MC/DC100%2/2
$self->ambiguous ne "return" and $sth->fetchrow_hashref
$self->ambiguous ne "return" $sth->fetchrow_hashref
Truth table100%3/3
A and B
A= $self->ambiguous ne "return"B= $sth->fetchrow_hashref
ABresult
0X0
100
111
154  
155 1 $self->log()->error('Ambiguous (multi-valued) result');
156 1 ▶ if ($self->ambiguous() eq 'die') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ambiguous eq "die") 1 0 1
157 1 die "Ambiguous (multi-valued) result";
158 }
159 0 return $self->_node_not_exists( @path );
160 }
161  
162 4 $self->log()->debug('Valid return: ' . Dumper $row);
163 4 return $row;
164  
165 }
166  
167  
168 1 ▶sub get_list {
Subroutine get_list: called CC=6 SCAR=19.2
Pod documented
169  
170 1 my $self = shift;
171 1 my @path = $self->_build_path( shift );
172  
173  
174 1 my $column = $self->column();
175 1 ▶ if (!$column || ref $column ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $column or ref $column ne "") 0 1 1
Condition33%1/3
not $column or ref $column ne ""
l!l&&r!l&&!r
001
MC/DC0%0/2
not $column or ref $column ne ""
not $column ref $column ne ""
Truth table33%1/3
not(A) or B
A= not $columnB= ref $column ne ""
ABresult
1X1
011
000
176 0 die "column must be a singe column name when using get_list";
177 }
178  
179 1 my $query = sprintf "SELECT %s FROM %s WHERE %s",
180 $self->column(), $self->table(), $self->condition();
181  
182 1 $self->log()->debug('Query is ' . $query);
183  
184 1 my $sth = $self->_dbi()->prepare($query);
185 1 $sth->execute( @path );
186  
187 1 my $rows = $sth->fetchall_arrayref();
188  
189 # hmpf
190 1 ▶ unless (ref $rows eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $rows eq "ARRAY") 0 1 1
191 0 $self->log()->error('DBI did not return an arrayref');
192 0 die "DBI did not return an arrayref.";
193 }
194  
195 1 ▶ if (scalar @{$rows} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$rows;} == 0) 0 1 1
196 0 return $self->_node_not_exists( @path );
197 }
198 1 my @result;
199 1 foreach my $row (@{$rows}) {
200 1 push @result, $row->[0];
201 }
202  
203 1 $self->log()->trace('result ' . Dumper \@result);
204  
205 1 $self->log()->debug('Valid return, '. scalar @result .' lines');
206 1 return @result;
207  
208 }
209  
210 2 ▶sub get_size {
Subroutine get_size: called CC=1 SCAR=0.0
Pod documented
211  
212 2 my $self = shift;
213 2 my @path = $self->_build_path( shift );
214  
215 2 my $query = sprintf "SELECT COUNT(*) as count FROM %s WHERE %s",
216 $self->table(), $self->condition();
217  
218 2 $self->log()->debug('Query is ' . $query);
219  
220 2 my $sth = $self->_dbi()->prepare($query);
221 2 $sth->execute( @path );
222  
223 2 my $row = $sth->fetchrow_arrayref();
224  
225 2 $self->log()->trace('Result is ' . Dumper $row);
226  
227 2 return $row->[0];
228  
229 }
230  
231 0 ▶sub get_meta {
Subroutine get_meta: not called CC=2 SCAR=17.9
Pod documented
232  
233 0 my $self = shift;
234  
235 # If we have no path, we tell the caller that we are a connector
236 0 my @path = $self->_build_path( shift );
237 0 ▶ if (scalar @path == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @path == 0) 0 0 0
238 0 return { TYPE => "connector" };
239 }
240  
241 # can be used as scalar also but list will be fine in any case
242 0 return { TYPE => 'list' };
243 }
244  
245  
246 # Will not catch get with an ambigous result :(
247 0 ▶sub exists {
Subroutine exists: not called CC=2 SCAR=17.9
Pod documented
248  
249 0 my $self = shift;
250  
251 # No path = connector root which always exists
252 0 my @path = $self->_build_path( shift );
253 0 ▶ if (scalar @path == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @path == 0) 0 0 0
254 0 return 1;
255 }
256  
257 0 return $self->get_size( \@path ) > 0;
258  
259 }
260  
261 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
262 1 __PACKAGE__->meta->make_immutable;
263  
264 1 1;
265 __END__
« Connector/Multi/Merge.pm ↑ Summary Connector/Multi/YAML.pm »