| 1 | package CGI::Lazy::DB; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 1 | ▶ | use DBI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use CGI::Lazy::Globals; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | #--------------------------------------------------------------------------------------- | ||||||||||||||
| 10 | 0 | ▶ | sub config { | ||||||||||||
|
Subroutine
config: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 11 | 0 | my $self = shift; | |||||||||||||
| 12 | |||||||||||||||
| 13 | 0 | return $self->q->config; | |||||||||||||
| 14 | } | ||||||||||||||
| 15 | |||||||||||||||
| 16 | #--------------------------------------------------------------------------------------- | ||||||||||||||
| 17 | 0 | ▶ | sub dbh { | ||||||||||||
|
Subroutine
dbh: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 18 | 0 | my $self = shift; | |||||||||||||
| 19 | |||||||||||||||
| 20 | 0 | return $self->{_dbh}; | |||||||||||||
| 21 | } | ||||||||||||||
| 22 | |||||||||||||||
| 23 | #--------------------------------------------------------------------------------------- | ||||||||||||||
| 24 | 0 | ▶ | sub do { #run query with no return value | ||||||||||||
|
Subroutine
do: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 25 | 0 | my $self = shift; | |||||||||||||
| 26 | 0 | my $query = shift; | |||||||||||||
| 27 | 0 | my @bindvars = @_; | |||||||||||||
| 28 | |||||||||||||||
| 29 | 0 | my $dbh = $self->dbh; | |||||||||||||
| 30 | 0 | my $sth; | |||||||||||||
| 31 | |||||||||||||||
| 32 | 0 | eval { | |||||||||||||
| 33 | 0 | $sth = $dbh->prepare($query); | |||||||||||||
| 34 | 0 | ▶ | $sth->execute(@bindvars) or carp $!; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | }; | ||||||||||||||
| 36 | |||||||||||||||
| 37 | 0 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | 0 | $self->q->errorHandler->dbError; | |||||||||||||
| 39 | 0 | return; | |||||||||||||
| 40 | } else { | ||||||||||||||
| 41 | 0 | return 1; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | } | ||||||||||||||
| 44 | |||||||||||||||
| 45 | #--------------------------------------------------------------------------------------- | ||||||||||||||
| 46 | 0 | ▶ | sub get { #run query returning single value | ||||||||||||
|
Subroutine
get: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||
| 47 | 0 | my $self = shift; | |||||||||||||
| 48 | 0 | my $query = shift; | |||||||||||||
| 49 | 0 | my @bindvars = @_; | |||||||||||||
| 50 | |||||||||||||||
| 51 | 0 | ▶ | if (ref $bindvars[0]) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | 0 | ▶ | if (ref $bindvars[0] eq 'ARRAY') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | 0 | @bindvars = @{$bindvars[0]}; | |||||||||||||
| 54 | } else { | ||||||||||||||
| 55 | 0 | $self->q->errorHandler->getWithOtherThanArray; | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | } | ||||||||||||||
| 58 | |||||||||||||||
| 59 | 0 | my $dbh = $self->dbh; | |||||||||||||
| 60 | 0 | my $sth; | |||||||||||||
| 61 | |||||||||||||||
| 62 | 0 | eval { | |||||||||||||
| 63 | 0 | $sth = $dbh->prepare($query); | |||||||||||||
| 64 | 0 | $sth->execute(@bindvars); | |||||||||||||
| 65 | }; | ||||||||||||||
| 66 | |||||||||||||||
| 67 | 0 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | 0 | $self->q->errorHandler->dbError; | |||||||||||||
| 69 | 0 | return; | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 0 | my @results = $sth->fetchrow_array; | |||||||||||||
| 73 | |||||||||||||||
| 74 | 0 | return $results[0]; | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | #--------------------------------------------------------------------------------------- | ||||||||||||||
| 78 | 0 | ▶ | sub getarray { #run query with return value | ||||||||||||
|
Subroutine
getarray: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||
| 79 | 0 | my $self = shift; | |||||||||||||
| 80 | 0 | my $query = shift; | |||||||||||||
| 81 | 0 | my @bindvars = @_; | |||||||||||||
| 82 | |||||||||||||||
| 83 | 0 | ▶ | if (ref $bindvars[0]) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | 0 | ▶ | if (ref $bindvars[0] eq 'ARRAY') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 0 | @bindvars = @{$bindvars[0]}; | |||||||||||||
| 86 | } else { | ||||||||||||||
| 87 | 0 | $self->q->errorHandler->getWithOtherThanArray; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | } | ||||||||||||||
| 90 | |||||||||||||||
| 91 | 0 | my $dbh = $self->dbh; | |||||||||||||
| 92 | 0 | my $sth; | |||||||||||||
| 93 | |||||||||||||||
| 94 | 0 | eval { | |||||||||||||
| 95 | 0 | $sth = $dbh->prepare($query); | |||||||||||||
| 96 | 0 | $sth->execute(@bindvars); | |||||||||||||
| 97 | }; | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 0 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | 0 | $self->q->errorHandler->dbError; | |||||||||||||
| 101 | 0 | return; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | 0 | return $sth->fetchall_arrayref; | |||||||||||||
| 105 | |||||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | #--------------------------------------------------------------------------------------- | ||||||||||||||
| 109 | 0 | ▶ | sub gethash { #run query with return value | ||||||||||||
|
Subroutine
gethash: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 110 | 0 | my $self = shift; | |||||||||||||
| 111 | 0 | my $query = shift; | |||||||||||||
| 112 | 0 | my $key = shift; | |||||||||||||
| 113 | 0 | my @bindvars = @_; | |||||||||||||
| 114 | |||||||||||||||
| 115 | 0 | my $dbh = $self->dbh; | |||||||||||||
| 116 | 0 | my $sth; | |||||||||||||
| 117 | |||||||||||||||
| 118 | 0 | eval { | |||||||||||||
| 119 | 0 | $sth = $dbh->prepare($query); | |||||||||||||
| 120 | 0 | $sth->execute(@bindvars); | |||||||||||||
| 121 | }; | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 0 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 124 | 0 | $self->q->errorHandler->dbError; | |||||||||||||
| 125 | 0 | return; | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | 0 | return $sth->fetchall_hashref($key); | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | #--------------------------------------------------------------------------------------- | ||||||||||||||
| 132 | 0 | ▶ | sub gethashlist { #run query with return value | ||||||||||||
|
Subroutine
gethashlist: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 133 | 0 | my $self = shift; | |||||||||||||
| 134 | 0 | my $query = shift; | |||||||||||||
| 135 | 0 | my @bindvars = @_; | |||||||||||||
| 136 | |||||||||||||||
| 137 | 0 | ▶ | if (ref $bindvars[0]) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 138 | 0 | ▶ | if (ref $bindvars[0] eq 'ARRAY') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 139 | 0 | @bindvars = @{$bindvars[0]}; | |||||||||||||
| 140 | } else { | ||||||||||||||
| 141 | 0 | $self->q->errorHandler->getWithOtherThanArray; | |||||||||||||
| 142 | } | ||||||||||||||
| 143 | } | ||||||||||||||
| 144 | |||||||||||||||
| 145 | 0 | my $dbh = $self->dbh; | |||||||||||||
| 146 | 0 | my $sth; | |||||||||||||
| 147 | |||||||||||||||
| 148 | 0 | eval { | |||||||||||||
| 149 | 0 | $sth = $dbh->prepare($query); | |||||||||||||
| 150 | 0 | $sth->execute(@bindvars); | |||||||||||||
| 151 | }; | ||||||||||||||
| 152 | |||||||||||||||
| 153 | 0 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 154 | 0 | $self->q->errorHandler->dbError; | |||||||||||||
| 155 | 0 | return; | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | |||||||||||||||
| 158 | 0 | my $results = []; | |||||||||||||
| 159 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 160 | 0 | push @$results, $row; | |||||||||||||
| 161 | } | ||||||||||||||
| 162 | |||||||||||||||
| 163 | 0 | return $results; | |||||||||||||
| 164 | } | ||||||||||||||
| 165 | |||||||||||||||
| 166 | #--------------------------------------------------------------------------------------- | ||||||||||||||
| 167 | 1 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=7 SCAR=30.0
Pod
documented
| |||||||||||||||
| 168 | 1 | my $class = shift; | |||||||||||||
| 169 | 1 | my $q = shift; | |||||||||||||
| 170 | |||||||||||||||
| 171 | 1 | my $self = bless {_q => $q}, $class; | |||||||||||||
| 172 | |||||||||||||||
| 173 | 1 | ▶ | if ($q->plugin->dbh) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 174 | 0 | my @list = split ':', $self->config->plugins->{dbh}->{dbDatasource}; | |||||||||||||
| 175 | 0 | my $type = $list[1]; | |||||||||||||
| 176 | 0 | $self->{_type} = $type; | |||||||||||||
| 177 | |||||||||||||||
| 178 | 0 | eval { | |||||||||||||
| 179 | $self->{_dbh} = DBI->connect( | ||||||||||||||
| 180 | $self->config->plugins->{dbh}->{dbDatasource}, | ||||||||||||||
| 181 | $self->config->plugins->{dbh}->{dbUser}, | ||||||||||||||
| 182 | $self->config->plugins->{dbh}->{dbPasswd}, | ||||||||||||||
| 183 | $self->config->plugins->{dbh}->{dbArgs} | ||||||||||||||
| 184 | 0 | ▶ | ) or die $!; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 185 | }; | ||||||||||||||
| 186 | |||||||||||||||
| 187 | 0 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 188 | 0 | $q->errorHandler->dbConnectFailed; | |||||||||||||
| 189 | 0 | exit; | |||||||||||||
| 190 | } | ||||||||||||||
| 191 | } else { #using dbh from somewhere else | ||||||||||||||
| 192 | 1 | ▶ | if ($q->vars->{dbh} ) { #if a dbh is specified on cgi creation use that one | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 193 | 0 | $self->{_dbh} = $q->vars->{dbh}; | |||||||||||||
| 194 | } elsif ($q->config->dbhVar){ | ||||||||||||||
| 195 | { | ||||||||||||||
| 196 | 0 | ▶ | no strict 'vars'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||
| 197 | 1 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 198 | |||||||||||||||
| 199 | 0 | ▶ | if ($self->config->plugins->{mod_perl}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 200 | 0 | my $handler = $self->config->plugins->{mod_perl}->{PerlHandler}; | |||||||||||||
| 201 | 0 | require Apache2::RequestUtil; | |||||||||||||
| 202 | 0 | my $r = Apache2::RequestUtil->request(); | |||||||||||||
| 203 | 0 | my $mp = "$handler"->new($r); | |||||||||||||
| 204 | |||||||||||||||
| 205 | 0 | $self->{_dbh} = ${$mp->make_namespace."::".$self->config->dbhVar}; | |||||||||||||
| 206 | |||||||||||||||
| 207 | } else { | ||||||||||||||
| 208 | 0 | $self->{_dbh} = $main::{$self->config->dbhVar}; | |||||||||||||
| 209 | |||||||||||||||
| 210 | } | ||||||||||||||
| 211 | } | ||||||||||||||
| 212 | } else { | ||||||||||||||
| 213 | |||||||||||||||
| 214 | } | ||||||||||||||
| 215 | } | ||||||||||||||
| 216 | |||||||||||||||
| 217 | 1 | return $self; | |||||||||||||
| 218 | } | ||||||||||||||
| 219 | |||||||||||||||
| 220 | #--------------------------------------------------------------------------------------- | ||||||||||||||
| 221 | 0 | ▶ | sub q { | ||||||||||||
|
Subroutine
q: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 222 | 0 | my $self = shift; | |||||||||||||
| 223 | |||||||||||||||
| 224 | 0 | return $self->{_q}; | |||||||||||||
| 225 | } | ||||||||||||||
| 226 | |||||||||||||||
| 227 | #--------------------------------------------------------------------------------------- | ||||||||||||||
| 228 | 0 | ▶ | sub recordset { | ||||||||||||
|
Subroutine
recordset: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 229 | 0 | my $self = shift; | |||||||||||||
| 230 | 0 | my $args = shift; | |||||||||||||
| 231 | |||||||||||||||
| 232 | 0 | return CGI::Lazy::DB::RecordSet->new($self, $args); | |||||||||||||
| 233 | |||||||||||||||
| 234 | } | ||||||||||||||
| 235 | |||||||||||||||
| 236 | #--------------------------------------------------------------------------------------- | ||||||||||||||
| 237 | 0 | ▶ | sub type { | ||||||||||||
|
Subroutine
type: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 238 | 0 | my $self = shift; | |||||||||||||
| 239 | |||||||||||||||
| 240 | 0 | return $self->{_dbh}->{Driver}->{Name}; | |||||||||||||
| 241 | } | ||||||||||||||
| 242 | 1 | 1 | |||||||||||||
| 243 | |||||||||||||||
| 244 | __END__ | ||||||||||||||