blib/lib/DBIx/MySperqlOO.pm

filter: Statementstmt 18.4% 29 / 157 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 20.0% 6 / 30 Podpod 26.6% 4 / 15 totaltotal 14.1% 39 / 276 CC 35 SCAR 31.4
CC
35
Cov
13%
BUILD
56.0
get_col_count
56.0
get_row_count
42.0
CRAP 23.2
↑ Summary
1 package DBIx::MySperqlOO;
2 1 ▶use Class::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Class::Std::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use version; our $VERSION = qv('1.0.1');
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 {
11 1 my %dbh_of : ATTR();
12 1 my %sth_of : ATTR();
13 1 my %dbd_of : ATTR();
14 1 my %db_of : ATTR();
15 1 my %host_of : ATTR();
16 1 my %user_of : ATTR();
17 1 my %pass_of : ATTR();
18 1 my %dsn_of : ATTR();
19  
20 0 ▶ sub BUILD {
Subroutine BUILD: not called CC=7 SCAR=40.3
Pod undocumented
21 0 my ( $self, $ident, $arg_ref ) = @_;
22  
23 0 ▶ $dbd_of{$ident} = $arg_ref->{dbd} ? $arg_ref->{dbd} : 'mysql';
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'dbd'} ? : 0 0 0
24 0 ▶ $db_of{$ident} = $arg_ref->{db} ? $arg_ref->{db} : $arg_ref->{database};
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'db'} ? : 0 0 0
25 0 ▶ $host_of{$ident} = $arg_ref->{host} ? $arg_ref->{host} : 'localhost';
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'host'} ? : 0 0 0
26 0 ▶ $user_of{$ident} = $arg_ref->{user} ? $arg_ref->{user} : '';
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'user'} ? : 0 0 0
27 0 ▶ $pass_of{$ident} = $arg_ref->{pass} ? $arg_ref->{pass} : '';
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'pass'} ? : 0 0 0
28 0 ▶ $dsn_of{$ident} = $arg_ref->{dsn} ? $self->_build_dsn() : '';
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'dsn'} ? : 0 0 0
29  
30 0 $self->connect();
31  
32 0 return;
33 }
34  
35 0 ▶ sub set_dbd { my ($self, $value) = @_; $dbd_of{ident $self} = $value; $self->_build_dsn(); return $self; }
Subroutine set_dbd: not called CC=1 SCAR=6.9
Pod undocumented
36 0 ▶ sub set_db { my ($self, $value) = @_; $db_of{ident $self} = $value; $self->_build_dsn(); return $self; }
Subroutine set_db: not called CC=1 SCAR=6.9
Pod undocumented
37 0 ▶ sub set_host { my ($self, $value) = @_; $host_of{ident $self} = $value; $self->_build_dsn(); return $self; }
Subroutine set_host: not called CC=1 SCAR=6.9
Pod undocumented
38 0 ▶ sub set_user { my ($self, $value) = @_; $user_of{ident $self} = $value; $self->_build_dsn(); return $self; }
Subroutine set_user: not called CC=1 SCAR=6.9
Pod undocumented
39 0 ▶ sub set_pass { my ($self, $value) = @_; $pass_of{ident $self} = $value; $self->_build_dsn(); return $self; }
Subroutine set_pass: not called CC=1 SCAR=6.9
Pod undocumented
40 0 ▶ sub set_dsn { my ($self, $value) = @_; $dsn_of{ident $self} = $value; return $self; }
Subroutine set_dsn: not called CC=1 SCAR=6.9
Pod undocumented
41  
42 0 ▶ sub get_user { my ($self) = @_; return $user_of{ident $self}; }
Subroutine get_user: not called CC=1 SCAR=6.9
Pod undocumented
43 0 ▶ sub get_pass { my ($self) = @_; return $pass_of{ident $self}; }
Subroutine get_pass: not called CC=1 SCAR=6.9
Pod undocumented
44 0 ▶ sub get_dsn { my ($self) = @_; my ($ident) = ident $self; if (! $dsn_of{$ident} ) { $self->_build_dsn(); } return $dsn_of{$ident}; }
Subroutine get_dsn: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless ($dsn_of{$ident}) 0 0 0
45  
46 0 ▶ sub _build_dsn { my ($self) = @_; my ($ident) = ident $self; $dsn_of{$ident} = 'DBI:' . $dbd_of{$ident} . ':database=' . $db_of{$ident} . ';host=' . $host_of{$ident}; }
Subroutine _build_dsn: not called CC=1 SCAR=6.9
47  
48 0 ▶ sub connect {
Subroutine connect: not called CC=2 SCAR=17.9
Pod documented
49 0 my ($self) = @_;
50 0 my ($ident) = ident $self;
51  
52 # Either connect or quit with error
53 0 $dbh_of{$ident} = DBI->connect( $self->get_dsn(), $self->get_user(), $self->get_pass() );
54
55 # Emit error string if connect failed
56 0 ▶ if ($DBI::err) { warn "ERROR: $DBI::err : $DBI::errstr [DBConnect failed]"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($DBI::err) 0 0 0
57
58 0 return $dbh_of{$ident};
59 }
60
61 0 ▶ sub sqlexec {
Subroutine sqlexec: not called CC=3 SCAR=24.8
Pod documented
62 0 my ( $self, $sql, $type, $parameters ) = @_;
63
64 0 ▶ $type = $type ? $type : '$sth';
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 0 0 0
65
66 # Execute the sql with the given params
67 0 $sth_of{ident $self} = $self->_sqlexecute( $sql, $parameters );
68
69 # Check for type of return
70 0 ▶ if ($type =~ m/sth/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /sth/) { } 0 0 0
71 # Return the handle
72 0 return $sth_of{ident $self};
73 } else {
74 # Return the data per type
75 0 return $self->_sqlfetch( $type );
76 }
77 }
78
79 0 ▶ sub _sqlexecute {
Subroutine _sqlexecute: not called CC=2 SCAR=17.9
80 0 my ( $self, $sql, $parameters ) = @_;
81 0 my $ident = ident($self);
82  
83 # Check parameters and prepare
84 0 $sth_of{$ident} = $self->_sqlparse( $sql, $parameters );
85
86 # Execute
87 0 ▶ $sth_of{$ident}->execute() or return "ERROR: [database.pl:_sqlexecute]$DBI::err : $DBI::errstr [_sqlexecute failed]";
Branch: 1 branch
BranchTrueFalseTotal
$sth_of{$ident}->execute or return "ERROR: [database.pl:_sqlexecute]$DBI::err : $DBI::errstr [_sqlexecute failed]" 0 0 0
88
89 # Return statement handle
90 0 return $sth_of{ident $self};
91 }
92
93 0 ▶ sub _sqlparse {
Subroutine _sqlparse: not called CC=5 SCAR=34.0
94 0 my ( $self, $test, $parameters) = @_;
95 0 my $ident = ident($self);
96
97 0 ▶ if ($self->_is_handle($test)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_handle($test)) { } 0 0 0
98 # Handle
99 0 ▶ $sth_of{$ident} = $dbh_of{$ident}->prepare($test->{Statement}) or return "ERROR: Could not prepare handle [_sqlparse failed]";
Branch: 1 branch
BranchTrueFalseTotal
$sth_of{$ident} = $dbh_of{$ident}->prepare($test->{"Statement"}) or return "ERROR: Could not prepare handle [_sqlparse failed]" 0 0 0
100 } else {
101 # SQL string
102 # Get database handle
103 0 ▶ if (!($dbh_of{$ident}->{Active})) { $dbh_of{$ident} = &DBConnect; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh_of{$ident}{"Active"}) 0 0 0
104
105 # Prepare the statement
106 0 ▶ $sth_of{$ident} = $dbh_of{$ident}->prepare($test) or return "ERROR: Could not prepare statement [_sqlparse failed]";
Branch: 1 branch
BranchTrueFalseTotal
$sth_of{$ident} = $dbh_of{$ident}->prepare($test) or return "ERROR: Could not prepare statement [_sqlparse failed]" 0 0 0
107 }
108
109 0 return $sth_of{ident $self};
110 }
111
112 0 ▶ sub _sqlfetch {
Subroutine _sqlfetch: not called CC=5 SCAR=34.0
113 0 my ( $self, $type ) = @_;
114
115 0 ▶ if ($type eq '\@@') { $self->_fetch_all_array_ref(); }
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq '\\@@') { } 0 0 0
elsif ($type eq '\\@') { } 0 0 0
elsif ($type eq '@') { } 0 0 0
elsif ($type eq '\\%') { } 0 0 0
116 0 elsif ($type eq '\@') { $self->_fetch_row_array_ref(); }
117 0 elsif ($type eq '@') { $self->_fetch_row_array(); }
118 0 elsif ($type eq '\%') { $self->_fetch_row_hash_ref(); }
119 0 else { return "ERROR: type($type) [_sqlfetch failed]"; }
120 }
121
122 0 ▶ sub _fetch_all_array_ref {
Subroutine _fetch_all_array_ref: not called CC=2 SCAR=17.9
123 0 my ( $self ) = @_;
124 0 my ($ref) = $sth_of{ident $self}->fetchall_arrayref;
125 0 ▶ if ($DBI::err) { return "ERROR: Fetching Error($DBI::err) $DBI::errstr [_fetch_all_array_ref failed]"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($DBI::err) 0 0 0
126 0 return $ref;
127 }
128
129 0 ▶ sub _fetch_row_array_ref {
Subroutine _fetch_row_array_ref: not called CC=2 SCAR=17.9
130 0 my ( $self ) = @_;
131 0 my ($ref) = $sth_of{ident $self}->fetchrow_arrayref;
132 0 ▶ if ($DBI::err) { return "ERROR: Fetching Error($DBI::err) $DBI::errstr [_fetch_row_array_ref failed]"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($DBI::err) 0 0 0
133 0 return $ref;
134 }
135
136 0 ▶ sub _fetch_row_array {
Subroutine _fetch_row_array: not called CC=2 SCAR=17.9
137 0 my ( $self ) = @_;
138 0 my (@array) = $sth_of{ident $self}->fetchrow_array;
139 0 ▶ if ($DBI::err) { return "ERROR: Fetching Error($DBI::err) $DBI::errstr [_fetch_row_array failed]"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($DBI::err) 0 0 0
140 0 return @array;
141 }
142
143 0 ▶ sub _fetch_row_hash_ref {
Subroutine _fetch_row_hash_ref: not called CC=2 SCAR=17.9
144 0 my ( $self ) = @_;
145 0 my ($ref) = $sth_of{ident $self}->fetchrow_hashref;
146 0 ▶ if ($DBI::err) { return "ERROR: Fetching Error($DBI::err) $DBI::errstr [_fetch_row_hash_ref failed]"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($DBI::err) 0 0 0
147 0 return $ref;
148 }
149
150 0 ▶ sub get_row_count {
Subroutine get_row_count: not called CC=6 SCAR=37.4
Pod documented
151 0 my $self = shift @_;
152 0 my $ref = $_[0];
153
154 # Check to see if it is a non array reference
155 0 ▶ if (($ref =~ /SCALAR/) or ($ref =~ /HASH/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref =~ /SCALAR/ or $ref =~ /HASH/) { } 0 0 0
Condition0%0/3
$ref =~ /SCALAR/ or $ref =~ /HASH/
l!l&&r!l&&!r
000
MC/DC0%0/2
$ref =~ /SCALAR/ or $ref =~ /HASH/
$ref =~ /SCALAR/ $ref =~ /HASH/
Truth table0%0/3
A or B
A= $ref =~ /SCALAR/B= $ref =~ /HASH/
ABresult
1X1
011
000
156 0 ▶ if ($ref =~ /HASH/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref =~ /HASH/) { } 0 0 0
157 # It is a reference to a hash
158 # Return the number of keys for hash
159 0 return keys(%$ref);
160 } else {
161 # It is a scalar
162 # Return 1 for scalar
163 0 return 1;
164 }
165 } else {
166 0 ▶ if ($ref =~ /ARRAY/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref =~ /ARRAY/) { } 0 0 0
167 0 ▶ if (@{ $ref->[0] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$ref->[0];}) { } 0 0 0
168 0 return @$ref;
169 } else {
170 0 return 0;
171 }
172 } else {
173 # It is a simple array or scalar
174 0 return 1;
175 }
176 }
177 }
178
179 0 ▶ sub get_col_count {
Subroutine get_col_count: not called CC=7 SCAR=40.3
Pod undocumented
180 0 my $self = shift @_;
181 0 my $ref = $_[0];
182
183 # Check to see if it is a non array reference
184 0 ▶ if (($ref =~ /SCALAR/) or ($ref =~ /HASH/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref =~ /SCALAR/ or $ref =~ /HASH/) { } 0 0 0
Condition0%0/3
$ref =~ /SCALAR/ or $ref =~ /HASH/
l!l&&r!l&&!r
000
MC/DC0%0/2
$ref =~ /SCALAR/ or $ref =~ /HASH/
$ref =~ /SCALAR/ $ref =~ /HASH/
Truth table0%0/3
A or B
A= $ref =~ /SCALAR/B= $ref =~ /HASH/
ABresult
1X1
011
000
185 0 ▶ if ($ref =~ /HASH/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref =~ /HASH/) { } 0 0 0
186 # It is a reference to a hash
187 # Return the number of keys for hash
188 0 return keys(%$ref);
189 } else {
190 # It is a scalar
191 # Return 1 for scalar
192 0 return 1;
193 }
194 } else {
195 # Evaluates the length of the first row
196 # Only non zero for actual array of arrays
197 0 ▶ if (@{ $ref->[0] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$ref->[0];}) { } 0 0 0
198 # It is a reference to an array of arrays
199 # Return the length of the first row
200 0 return @{ $ref->[0] };
201 } else {
202 # Regular arrays, array references, and
203 # scalars land here
204 # An array would have at least two elements
205 # A reference or scalar would have one
206 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 0 0
207 # It is an array
208 # Return the length of the input array
209 0 return @_;
210 } else {
211 0 ▶ if (@$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$ref) { } 0 0 0
212 # It is a reference to an array
213 # Return the length of the input array
214 0 return @$ref;
215 } else {
216 # It is a scalar
217 # Return 1 for scalar
218 0 return 1;
219 }
220 }
221 }
222 }
223 }
224
225 0 ▶ sub get_field_names { my ( $self ) = @_; my $ref = $sth_of{ident $self}->{NAME}; return @$ref; }
Subroutine get_field_names: not called CC=1 SCAR=6.9
Pod documented
226
227 0 ▶ sub _is_handle { my $self = shift @_; return ($_[0] =~ m/^DBI/); }
Subroutine _is_handle: not called CC=1 SCAR=6.9
228
229 }
230  
231 1 1; # Magic true value required at end of module
232  
233 __END__
↑ Summary