| 1 | package DBIx::MySperql; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008005; | ||||||||||||||||||||||||||
|
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 | 1 | ▶ | use vars qw($dbh); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | our %EXPORT_TAGS = ( 'all' => [ qw(DBConnect SQLExec SQLExecute SQLParse SQLFetch GetRowCount GetColumnCount GetFieldNames IsHandle) ] ); | |||||||||||||||||||||||||||
| 14 | 1 | our @EXPORT_OK = qw(DBConnect SQLExec SQLExecute SQLParse SQLFetch GetRowCount GetColumnCount GetFieldNames IsHandle); | |||||||||||||||||||||||||||
| 15 | 1 | our @EXPORT = qw($dbh); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | our $VERSION = '1.01'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub DBConnect { | ||||||||||||||||||||||||||
|
Subroutine
DBConnect: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 0 | my %parms = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Database dsn | ||||||||||||||||||||||||||||
| 23 | 0 | my $dsn = "DBI:mysql:database=$parms{database};host=$parms{host}"; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Either connect or quit with error | ||||||||||||||||||||||||||||
| 26 | 0 | $dbh = DBI->connect($dsn, $parms{user}, $parms{pass}); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Emit error string if connect failed | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | if ($DBI::err) { return "ERROR: $DBI::err : $DBI::errstr [DBConnect failed]"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | return $dbh; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub SQLExec { | ||||||||||||||||||||||||||
|
Subroutine
SQLExec: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 0 | my ($sql, $type, %parameters) = @_; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | $type = $type ? $type : '$sth'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Execute the sql with the given params | ||||||||||||||||||||||||||||
| 40 | 0 | my $sth = &SQLExecute($sql, %parameters); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # Check for type of return | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if ($type =~ m/sth/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | # Return the handle | ||||||||||||||||||||||||||||
| 45 | 0 | return $sth; | |||||||||||||||||||||||||||
| 46 | } else { | ||||||||||||||||||||||||||||
| 47 | # Return the data per type | ||||||||||||||||||||||||||||
| 48 | 0 | return &SQLFetch($sth, $type); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub SQLExecute { | ||||||||||||||||||||||||||
|
Subroutine
SQLExecute: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | # Check parameters and prepare | ||||||||||||||||||||||||||||
| 54 | 0 | my $sth = &SQLParse(@_); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Execute | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | $sth->execute() or return "ERROR: [database.pl:SQLExecute]$DBI::err : $DBI::errstr [SQLExecute failed]"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Return statement handle | ||||||||||||||||||||||||||||
| 60 | 0 | return $sth; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub SQLFetch { | ||||||||||||||||||||||||||
|
Subroutine
SQLFetch: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 0 | my ($sth, $type) = @_; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | my %return = ( | |||||||||||||||||||||||||||
| 67 | '\@@' => \&FetchAllArrayRef, | ||||||||||||||||||||||||||||
| 68 | '\@' => \&FetchRowArrayRef, | ||||||||||||||||||||||||||||
| 69 | '@' => \&FetchRowArray, | ||||||||||||||||||||||||||||
| 70 | '\%' => \&FetchRowHashRef, | ||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ($return{$type}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | return $return{$type}->($sth); | |||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||
| 76 | 0 | return "ERROR: type($type) [SQLFetch failed]"; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub FetchAllArrayRef { | ||||||||||||||||||||||||||
|
Subroutine
FetchAllArrayRef: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 0 | my ($sth) = @_; | |||||||||||||||||||||||||||
| 82 | 0 | my ($ref) = $sth->fetchall_arrayref; | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ($DBI::err) { return "ERROR: Fetching Error($DBI::err) $DBI::errstr [FetchAllArrayRef failed]"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | return $ref; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub FetchRowArrayRef { | ||||||||||||||||||||||||||
|
Subroutine
FetchRowArrayRef: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | 0 | my ($sth) = @_; | |||||||||||||||||||||||||||
| 89 | 0 | my ($ref) = $sth->fetchrow_arrayref; | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ($DBI::err) { return "ERROR: Fetching Error($DBI::err) $DBI::errstr [FetchRowArrayRef failed]"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | return $ref; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub FetchRowArray { | ||||||||||||||||||||||||||
|
Subroutine
FetchRowArray: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 0 | my ($sth) = @_; | |||||||||||||||||||||||||||
| 96 | 0 | my (@array) = $sth->fetchrow_array; | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ($DBI::err) { return "ERROR: Fetching Error($DBI::err) $DBI::errstr [FetchRowArray failed]"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | return @array; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub FetchRowHashRef { | ||||||||||||||||||||||||||
|
Subroutine
FetchRowHashRef: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 102 | 0 | my ($sth) = @_; | |||||||||||||||||||||||||||
| 103 | 0 | my ($ref) = $sth->fetchrow_hashref; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ($DBI::err) { return "ERROR: Fetching Error($DBI::err) $DBI::errstr [FetchRowHashRef failed]"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | return $ref; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub SQLParse { | ||||||||||||||||||||||||||
|
Subroutine
SQLParse: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 0 | my ($test, @parameters) = @_; | |||||||||||||||||||||||||||
| 110 | 0 | my ($sth); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if (&IsHandle($test)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | # = handle | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | $sth = $dbh->prepare($_[0]->{Statement}) or return "ERROR: Could not prepare handle [SQLParse failed]"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } else { | ||||||||||||||||||||||||||||
| 116 | # = string | ||||||||||||||||||||||||||||
| 117 | # Get database handle | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if (!($dbh->{Active})) { $dbh = &DBConnect; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | #Prepare the statement | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | $sth = $dbh->prepare("$_[0]") or return "ERROR: Could not prepare statement [SQLParse failed]"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | return $sth; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub GetRowCount { | ||||||||||||||||||||||||||
|
Subroutine
GetRowCount: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 0 | my $ref = $_[0]; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # Check to see if it is a non array reference | ||||||||||||||||||||||||||||
| 131 | 0 | ▶ | if (($ref =~ /SCALAR/) or ($ref =~ /HASH/)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ref =~ /SCALAR/ or $ref =~ /HASH/
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/
| |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ($ref =~ /HASH/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | # It is a reference to a hash | ||||||||||||||||||||||||||||
| 134 | # Return the number of keys for hash | ||||||||||||||||||||||||||||
| 135 | 0 | return keys(%$ref); | |||||||||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||||||||||
| 137 | # It is a scalar | ||||||||||||||||||||||||||||
| 138 | # Return 1 for scalar | ||||||||||||||||||||||||||||
| 139 | 0 | return 1; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } else { | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ($ref =~ /ARRAY/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if (@{ $ref->[0] }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | return @$ref; | |||||||||||||||||||||||||||
| 145 | } else { | ||||||||||||||||||||||||||||
| 146 | 0 | return 0; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | } else { | ||||||||||||||||||||||||||||
| 149 | # It is a simple array or scalar | ||||||||||||||||||||||||||||
| 150 | 0 | return 1; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | sub GetColumnCount { | ||||||||||||||||||||||||||
|
Subroutine
GetColumnCount: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 156 | 0 | my $ref = $_[0]; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # Check to see if it is a non array reference | ||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if (($ref =~ /SCALAR/) or ($ref =~ /HASH/)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ref =~ /SCALAR/ or $ref =~ /HASH/
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/
| |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | if ($ref =~ /HASH/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | # It is a reference to a hash | ||||||||||||||||||||||||||||
| 162 | # Return the number of keys for hash | ||||||||||||||||||||||||||||
| 163 | 0 | return keys(%$ref); | |||||||||||||||||||||||||||
| 164 | } else { | ||||||||||||||||||||||||||||
| 165 | # It is a scalar | ||||||||||||||||||||||||||||
| 166 | # Return 1 for scalar | ||||||||||||||||||||||||||||
| 167 | 0 | return 1; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } else { | ||||||||||||||||||||||||||||
| 170 | # Evaluates the length of the first row | ||||||||||||||||||||||||||||
| 171 | # Only non zero for actual array of arrays | ||||||||||||||||||||||||||||
| 172 | 0 | ▶ | if (@{ $ref->[0] }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | # It is a reference to an array of arrays | ||||||||||||||||||||||||||||
| 174 | # Return the length of the first row | ||||||||||||||||||||||||||||
| 175 | 0 | return @{ $ref->[0] }; | |||||||||||||||||||||||||||
| 176 | } else { | ||||||||||||||||||||||||||||
| 177 | # Regular arrays, array references, and | ||||||||||||||||||||||||||||
| 178 | # scalars land here | ||||||||||||||||||||||||||||
| 179 | # An array would have at least two elements | ||||||||||||||||||||||||||||
| 180 | # A reference or scalar would have one | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if (@_ > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | # It is an array | ||||||||||||||||||||||||||||
| 183 | # Return the length of the input array | ||||||||||||||||||||||||||||
| 184 | 0 | return @_; | |||||||||||||||||||||||||||
| 185 | } else { | ||||||||||||||||||||||||||||
| 186 | 0 | ▶ | if (@$ref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | # It is a reference to an array | ||||||||||||||||||||||||||||
| 188 | # Return the length of the input array | ||||||||||||||||||||||||||||
| 189 | 0 | return @$ref; | |||||||||||||||||||||||||||
| 190 | } else { | ||||||||||||||||||||||||||||
| 191 | # It is a scalar | ||||||||||||||||||||||||||||
| 192 | # Return 1 for scalar | ||||||||||||||||||||||||||||
| 193 | 0 | return 1; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | ▶ | sub GetFieldNames { | ||||||||||||||||||||||||||
|
Subroutine
GetFieldNames: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 201 | 0 | my $ref = $_[0]->{NAME}; | |||||||||||||||||||||||||||
| 202 | 0 | return @$ref; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 0 | ▶ | sub IsHandle { return ($_[0] =~ m/^DBI/); } | ||||||||||||||||||||||||||
|
Subroutine
IsHandle: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 1 | return 1; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | __END__ | ||||||||||||||||||||||||||||