Labyrinth/DBUtils.pm

filter: Statementstmt 15.8% 28 / 177 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 36.3% 8 / 22 Podpod 100% 7 / 7 totaltotal 11.9% 43 / 360 CC 68 SCAR 46.1
CC
68
Cov
9%
_doQuery
210.0
GetQuery
156.0
_db_connect
156.0
CRAP 100.1
« Labyrinth/Users.pm ↑ Summary Labyrinth/Mailer.pm »
1 package Labyrinth::DBUtils;
2  
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use vars qw($VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 $VERSION = '5.32';
8  
9 =head1 NAME
10  
11 Labyrinth::DBUtils - Database Manager for Labyrinth
12  
13 =head1 SYNOPSIS
14  
15 use Labyrinth::DBUtils;
16  
17 my $dbi = Labyrinth::DBUtils->new({
18 driver => 'CSV',
19 file => '/var/www/mysite/db');
20 sub errors { print STDERR "Error: $_[0], sql=$_[1]\n" }
21  
22 my @arr = $dbi->GetQuery('array','getTables');
23 my @arr = $dbi->GetQuery('array','getTable',$bid);
24 my @arr = $dbi->GetQuery('hash','getOneRow',
25 {table=>'uds_build',field=>'bid'},$bid});
26  
27 my $id = $dbi->IDQuery('insertRow',$id,$name);
28 $dbi->DoQuery('deleteRow',$id);
29 $dbi->DoQuery('updateRow',{id=>$id,name=>$name});
30  
31 my $next = Iterator('array','getTables');
32 my $row = $next->(); # returns an array ref
33  
34 my $next = Iterator('hash','getTables');
35 my $row = $next->(); # returns a hash ref
36  
37 $value = $dbi->Quote($value);
38  
39 =head1 DESCRIPTION
40  
41 The DBUtils package is a further database interface layer, providing a
42 collection of control methods to initiate the database connection, handle
43 errors and a smooth handover from the program to the database drivers.
44  
45 Reads and handles the SQL from the phrasebook, passing the statement and any
46 additional parameters through to the DBI onject.
47  
48 =cut
49  
50 # -------------------------------------
51 # Library Modules
52  
53 12 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 #use DBD::mysql;
55  
56 12 ▶use Labyrinth::Audit;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 12 ▶use Labyrinth::Phrasebook;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 12 ▶use Labyrinth::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 # -------------------------------------
61 # Variables
62  
63 12 my %autosubs = map {$_ => 1} qw( driver database file host port user password );
64  
65 # -------------------------------------
66 # The Public Interface Subs
67  
68 =head2 CONSTRUCTOR
69  
70 =over 4
71  
72 =item new DBUtils({})
73  
74 The Constructor method. Can be called with an anonymous hash,
75 listing the values to be used to connect to and handle the database.
76  
77 Values in the hash can be
78  
79 logfile
80 phrasebook (*)
81 dictionary
82 driver (*)
83 database (+)
84 dbfile (+)
85 dbhost
86 dbport
87 dbuser
88 dbpass
89 autocommit
90  
91 (*) These entries MUST exist in the hash.
92 (+) At least ONE of these must exist in the hash, and depend upon the driver.
93  
94 Note that 'file' is for use with a flat file database, such as DBD::CSV.
95  
96 =back
97  
98 =cut
99  
100 0 ▶sub new {
Subroutine new: not called CC=9 SCAR=45.0
Pod documented
101 0 my ($self, $hash) = @_;
102 0 my ($log,$pb) = (undef,undef); # in case a log is not required
103  
104 0 my $logfile = $hash->{logfile}; # mandatory
105 0 my $phrasebook = $hash->{phrasebook}; # mandatory
106 0 ▶ my $dictionary = $hash->{dictionary} || 'PROCS'; # optional
Condition0%0/2
$hash->{'dictionary'} || 'PROCS'
l!l
00
MC/DC0%0/1
$hash->{'dictionary'} || 'PROCS'
$hash->{'dictionary'}
Truth table0%0/2
A
A= $hash->{'dictionary'}
Aresult
11
00
107  
108 # check we've got our mandatory fields
109 0 ▶ Croak("$self needs a driver!") unless($hash->{driver});
Branch: 1 branch
BranchTrueFalseTotal
unless $hash->{"driver"} 0 0 0
110 Croak("$self needs a database/file!")
111 0 ▶ unless($hash->{database} || $hash->{file});
Branch: 1 branch
BranchTrueFalseTotal
unless $hash->{"database"} or $hash->{"file"} 0 0 0
Condition0%0/3
$hash->{"database"} or $hash->{"file"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$hash->{"database"} or $hash->{"file"}
$hash->{"database"} $hash->{"file"}
Truth table0%0/3
A or B
A= $hash->{"database"}B= $hash->{"file"}
ABresult
1X1
011
000
112 0 ▶ Croak("$self needs a phrasebook!") unless($phrasebook);
Branch: 1 branch
BranchTrueFalseTotal
unless $phrasebook 0 0 0
113  
114 # check files exist and we can access them correctly
115 0 ▶ Croak("$self cannot access phrasebook [$phrasebook]!")
Branch: 1 branch
BranchTrueFalseTotal
unless $phrasebook and -r $phrasebook 0 0 0
Condition0%0/3
$phrasebook and -r $phrasebook
!ll&&!rl&&r
000
MC/DC0%0/2
$phrasebook and -r $phrasebook
$phrasebook -r $phrasebook
Truth table0%0/3
A and B
A= $phrasebookB= -r $phrasebook
ABresult
0X0
100
111
116 unless($phrasebook && -r $phrasebook);
117  
118  
119 # initiate the phrasebook
120 0 $pb = Labyrinth::Phrasebook->new($phrasebook);
121 0 $pb->load($dictionary);
122  
123 # create an attributes hash
124 my $dbv = {
125 'driver' => $hash->{driver},
126 'database' => $hash->{database},
127 'file' => $hash->{dbfile},
128 'host' => $hash->{dbhost},
129 'port' => $hash->{dbport},
130 'user' => $hash->{dbuser},
131 'password' => $hash->{dbpass},
132 0 'log' => $log,
133 'pb' => $pb,
134 };
135  
136 0 ▶ $dbv->{autocommit} = $hash->{autocommit} if(defined $hash->{autocommit});
Branch: 1 branch
BranchTrueFalseTotal
if defined $hash->{"autocommit"} 0 0 0
137  
138 # create the object
139 0 bless $dbv, $self;
140 0 return $dbv;
141 }
142  
143 =head2 PUBLIC INTERFACE METHODS
144  
145 =over 4
146  
147 =item GetQuery(type,key,<list>)
148  
149 type - 'array' or 'hash'
150 key - hash key to sql in phrasebook
151 <list> - optional additional values to be inserted into SQL placeholders
152  
153 The function performs a SELECT statement, which returns either a list of lists,
154 or a list of hashes. The difference being that for each record, the field
155 values are listed in the order they are returned, or via the table column
156 name in a hash.
157  
158 The first entry in <list> can be an anonymous hash, containing the placeholder
159 values to be interpolated by Class::Phrasebook.
160  
161 Note that if the key is not found in the phrasebook, the function returns
162 with undef.
163  
164 =cut
165  
166 0 ▶sub GetQuery {
Subroutine GetQuery: not called CC=12 SCAR=50.5
Pod documented
167 0 my ($dbv,$type,$key,@args) = @_;
168 0 my ($hash,$sql);
169  
170 # retrieve the sql from the phrasebook,
171 # inserting placeholders (if required)
172 0 ▶ $hash = shift @args if(ref($args[0]) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[0] eq "HASH" 0 0 0
173 0 eval { $sql = $dbv->{pb}->get($key,$hash); };
174
175 0 ▶ LogDebug("key=[$key], sql=[$sql], args[".join(",",map{$_ || ''} @args)."], err=$@");
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
176 0 ▶ return () unless($sql);
Branch: 1 branch
BranchTrueFalseTotal
unless $sql 0 0 0
177  
178 # if the object doesnt contain a reference to a dbh object
179 # then we need to connect to the database
180 0 ▶ $dbv = &_db_connect($dbv) if not $dbv->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbv->{"dbh"} 0 0 0
181  
182 # prepare the sql statement for executing
183 0 my $sth = $dbv->{dbh}->prepare($sql);
184 0 ▶ unless($sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
185 0 ▶ LogError("err=".$dbv->{dbh}->errstr.", key=[$key], sql=[$sql], args[".join(",",map{$_ || ''} @args)."]");
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
186 0 return ();
187 }
188  
189 # execute the SQL using any values sent to the function
190 # to be placed in the sql
191 0 ▶ if(!$sth->execute(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->execute(@args)) 0 0 0
192 0 ▶ LogError("err=".$sth->errstr.", key=[$key], sql=[$sql], args[".join(",",map{$_ || ''} @args)."]");
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
193 0 return ();
194 }
195  
196 0 my @result;
197 # grab the data in the right way
198 0 ▶ if ( $type eq 'array' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'array') { } 0 0 0
199 0 ▶ while ( my $row = $sth->fetchrow_arrayref() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 0 0 0
200 0 push @result, [@{$row}];
201 }
202 } else {
203 0 ▶ while ( my $row = $sth->fetchrow_hashref() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
204 0 push @result, $row;
205 }
206 }
207  
208 # finish with our statement handle
209 0 $sth->finish;
210 # return the found datastructure
211 0 return @result;
212 }
213  
214 =item Iterator(type,key,<list>)
215  
216 type - 'array' or 'hash'
217 key - hash key to sql in phrasebook
218 <list> - optional additional values to be inserted into SQL placeholders
219  
220 The function performs a SELECT statement, which returns a subroutine reference
221 which can then be used to obtain either a list of lists, or a list of hashes.
222 The difference being that for each record, the field values are listed in the
223 order they are returned, or via the table column name in a hash.
224  
225 The first entry in <list> can be an anonymous hash, containing the placeholder
226 values to be interpolated by Class::Phrasebook.
227  
228 Note that if the key is not found in the phrasebook, the function returns
229 with undef.
230  
231 =cut
232  
233 0 ▶sub Iterator {
Subroutine Iterator: not called CC=10 SCAR=47.0
Pod documented
234 0 my ($dbv,$type,$key,@args) = @_;
235 0 my ($hash,$sql);
236  
237 # retrieve the sql from the phrasebook,
238 # inserting placeholders (if required)
239 0 ▶ $hash = shift @args if(ref($args[0]) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[0] eq "HASH" 0 0 0
240 0 eval { $sql = $dbv->{pb}->get($key,$hash); };
241  
242 0 ▶ LogDebug("key=[$key], sql=[$sql], args[".join(",",map{$_ || ''} @args)."], err=$@");
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
243 0 ▶ return unless($sql);
Branch: 1 branch
BranchTrueFalseTotal
unless $sql 0 0 0
244  
245 # if the object doesnt contain a reference to a dbh object
246 # then we need to connect to the database
247 0 ▶ $dbv = &_db_connect($dbv) if not $dbv->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbv->{"dbh"} 0 0 0
248  
249 # prepare the sql statement for executing
250 0 my $sth = $dbv->{dbh}->prepare($sql);
251 0 ▶ unless($sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
252 0 ▶ LogError("err=".$dbv->{dbh}->errstr.", sql=[$sql], args[".join(",",map{$_ || ''} @args)."]");
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
253 0 return;
254 }
255  
256 # execute the SQL using any values sent to the function
257 # to be placed in the sql
258 0 ▶ if(!$sth->execute(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->execute(@args)) 0 0 0
259 0 ▶ LogError("err=".$sth->errstr.", sql=[$sql], args[".join(",",map{$_ || ''} @args)."]");
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
260 0 return;
261 }
262  
263 # grab the data in the right way
264 0 ▶ if ( $type eq 'array' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'array') { } 0 0 0
265 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
266 0 ▶ if ( my $row = $sth->fetchrow_arrayref() ) { return $row; }
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) { } 0 0 0
267 0 else { $sth->finish; return; }
268 }
269 0 } else {
270 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
271 0 ▶ if ( my $row = $sth->fetchrow_hashref() ) { return $row; }
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) { } 0 0 0
272 0 else { $sth->finish; return; }
273 }
274 0 }
275 }
276  
277 =item DoQuery(key,<list>)
278  
279 key - hash key to sql in phrasebook
280 <list> - optional additional values to be inserted into SQL placeholders
281  
282 The function performs an SQL statement. If performing an INSERT statement that
283 returns an record id, this is returned to the calling function.
284  
285 The first entry in <list> can be an anonymous hash, containing the placeholder
286 values to be interpolated by Class::Phrasebook.
287  
288 Note that if the key is not found in the phrasebook, the function returns
289 with undef.
290  
291 =cut
292  
293 0 ▶sub DoQuery {
Subroutine DoQuery: not called CC=4 SCAR=30.0
Pod documented
294 0 my ($dbv,$key,@args) = @_;
295 0 my ($hash,$sql);
296  
297 # retrieve the sql from the phrasebook,
298 # inserting placeholders (if required)
299 0 ▶ $hash = shift @args if(ref($args[0]) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[0] eq "HASH" 0 0 0
300 0 eval { $sql = $dbv->{pb}->get($key,$hash); };
301  
302 0 ▶ LogDebug("key=[$key], sql=[$sql], args[".join(",",map{$_ || ''} @args)."], err=$@");
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
303 0 ▶ return unless($sql);
Branch: 1 branch
BranchTrueFalseTotal
unless $sql 0 0 0
304  
305 0 $dbv->_doQuery($sql,0,@args);
306 }
307  
308 =item IDQuery(key,<list>)
309  
310 key - hash key to sql in phrasebook
311 <list> - optional additional values to be inserted into SQL placeholders
312  
313 The function performs an SQL statement. If performing an INSERT statement that
314 returns an record id, this is returned to the calling function.
315  
316 The first entry in <list> can be an anonymous hash, containing the placeholder
317 values to be interpolated by Class::Phrasebook.
318  
319 Note that if the key is not found in the phrasebook, the function returns
320 with undef.
321  
322 =cut
323  
324 0 ▶sub IDQuery {
Subroutine IDQuery: not called CC=4 SCAR=30.0
Pod documented
325 0 my ($dbv,$key,@args) = @_;
326 0 my ($hash,$sql);
327  
328 # retrieve the sql from the phrasebook,
329 # inserting placeholders (if required)
330 0 ▶ $hash = shift @args if(ref($args[0]) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[0] eq "HASH" 0 0 0
331 0 eval { $sql = $dbv->{pb}->get($key,$hash); };
332  
333 0 ▶ LogDebug("key=[$key], sql=[$sql], args[".join(",",map{$_ || ''} @args)."], err=$@");
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
334 0 ▶ return unless($sql);
Branch: 1 branch
BranchTrueFalseTotal
unless $sql 0 0 0
335  
336 0 return $dbv->_doQuery($sql,1,@args);
337 }
338  
339 =item DoSQL(sql,<list>)
340  
341 sql - SQL statement
342 <list> - optional additional values to be inserted into SQL placeholders
343  
344 =cut
345  
346 0 ▶sub DoSQL {
Subroutine DoSQL: not called CC=2 SCAR=17.9
Pod documented
347 0 my ($dbv,$sql,@args) = @_;
348 0 ▶ return unless($sql);
Branch: 1 branch
BranchTrueFalseTotal
unless $sql 0 0 0
349  
350 0 $dbv->_doQuery($sql,0,@args);
351 }
352  
353 # _doQuery(key,idrequired,<list>)
354 #
355 # key - hash key to sql in phrasebook
356 # idrequired - true if an ID value is required on return
357 # <list> - optional additional values to be inserted into SQL placeholders
358 #
359 #The function performs an SQL statement. If performing an INSERT statement that
360 #returns an record id, this is returned to the calling function.
361 #
362 #The first entry in <list> can be an anonymous hash, containing the placeholder
363 #values to be interpolated by Class::Phrasebook.
364 #
365 #Note that if the key is not found in the phrasebook, the function returns
366 #with undef.
367 #
368  
369 0 ▶sub _doQuery {
Subroutine _doQuery: not called CC=14 SCAR=53.5
370 0 my ($dbv,$sql,$idrequired,@args) = @_;
371 0 my $rowid = undef;
372  
373 0 ▶ LogDebug("sql=[$sql], args[".join(",",map{$_ || ''} @args)."]");
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
374 0 ▶ return $rowid unless($sql);
Branch: 1 branch
BranchTrueFalseTotal
unless $sql 0 0 0
375  
376 # if the object doesnt contain a refrence to a dbh object
377 # then we need to connect to the database
378 0 ▶ $dbv = &_db_connect($dbv) if not $dbv->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbv->{"dbh"} 0 0 0
379  
380 0 ▶ if($idrequired) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idrequired) { } 0 0 0
381 # prepare the sql statement for executing
382 0 my $sth = $dbv->{dbh}->prepare($sql);
383 0 ▶ unless($sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
384 0 ▶ LogError("err=".$dbv->{dbh}->errstr.", sql=[$sql], args[".join(",",map{$_ || ''} @args)."]");
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
385 0 return;
386 }
387  
388 # execute the SQL using any values sent to the function
389 # to be placed in the sql
390 0 ▶ if(!$sth->execute(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->execute(@args)) 0 0 0
391 0 ▶ LogError("err=".$sth->errstr.", sql=[$sql], args[".join(",",map{$_ || ''} @args)."]");
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
392 0 return;
393 }
394  
395 0 ▶ if($dbv->{driver} =~ /mysql/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbv->{'driver'} =~ /mysql/i) { } 0 0 0
396 0 $rowid = $dbv->{dbh}->{mysql_insertid};
397 } else {
398 0 my $row;
399 0 ▶ $rowid = $row->[0] if( $row = $sth->fetchrow_arrayref() );
Branch: 1 branch
BranchTrueFalseTotal
if $row = $sth->fetchrow_arrayref 0 0 0
400 }
401  
402 } else {
403 0 eval { $rowid = $dbv->{dbh}->do($sql, undef, @args) };
404 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
405 0 ▶ LogError("err=".$dbv->{dbh}->errstr.", sql=[$sql], args[".join(",",map{$_ || ''} @args)."]");
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
406 0 return -1;
407 }
408  
409 0 ▶ $rowid ||= 1; # technically this should be the number of succesful rows
Condition0%0/2
$rowid ||= 1
l!l
00
MC/DC0%0/1
$rowid ||= 1
$rowid
Truth table0%0/2
A
A= $rowid
Aresult
11
00
410 }
411  
412  
413 ## Return the rowid we just used
414 0 return $rowid;
415 }
416  
417 =item Quote(string)
418  
419 string - string to be quoted
420  
421 The function performs a DBI quote operation, which will quote a string
422 according to the SQL rules.
423  
424 =cut
425  
426 0 ▶sub Quote {
Subroutine Quote: not called CC=4 SCAR=30.0
Pod documented
427 0 my $dbv = shift;
428 0 ▶ return unless($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 0 0
429  
430 # Cant quote with DBD::CSV
431 0 ▶ return $_[0] if($dbv->{driver} =~ /csv/i);
Branch: 1 branch
BranchTrueFalseTotal
if $dbv->{"driver"} =~ /csv/i 0 0 0
432  
433 # if the object doesnt contain a refrence to a dbh object
434 # then we need to connect to the database
435 0 ▶ $dbv = &_db_connect($dbv) if not $dbv->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbv->{"dbh"} 0 0 0
436  
437 0 $dbv->{dbh}->quote($_[0]);
438 }
439  
440 # -------------------------------------
441 # The Get & Set Methods Interface Subs
442  
443 =item Get & Set Methods
444  
445 The following accessor methods are available:
446  
447 driver
448 database
449 file
450 host
451 port
452 user
453 password
454  
455 All functions can be called to return the current value of the associated
456 object variable, or be called with a parameter to set a new value for the
457 object variable.
458  
459 (*) Setting these methods will take action immediately. All other access
460 methods require a new object to be created, before they can be used.
461  
462 Examples:
463  
464 my $database = db_database();
465 db_database('another');
466  
467 =cut
468  
469 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=13.7
470 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
471 0 my $name = $AUTOLOAD;
472 0 $name =~ s/^.*:://;
473 0 ▶ die "Unknown sub $AUTOLOAD\n" unless($autosubs{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless $autosubs{$name} 0 0 0
474  
475 0 ▶ *$name = sub { my $dbv=shift; @_ ? $dbv->{$name}=shift : $dbv->{$name} };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
476 0 goto &$name;
477 }
478  
479 # -------------------------------------
480 # The Private Subs
481 # These modules should not have to be called from outside this module
482  
483 0 ▶sub _db_connect {
Subroutine _db_connect: not called CC=12 SCAR=50.5
484 0 my $dbv = shift;
485  
486 0 my $dsn = 'dbi:' . $dbv->{driver};
487 0 ▶ my $ac = defined $dbv->{autocommit} ? $dbv->{autocommit} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $dbv->{'autocommit'} ? : 0 0 0
488  
489 0 ▶ if($dbv->{driver} =~ /ODBC/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dbv->{'driver'} =~ /ODBC/) { } 0 0 0
elsif ($dbv->{'driver'} =~ /SQLite/) { } 0 0 0
490 # all the info is in the Data Source repository
491  
492 } elsif($dbv->{driver} =~ /SQLite/) {
493 0 ▶ $dsn .= ':dbname=' . $dbv->{database} if $dbv->{database};
Branch: 1 branch
BranchTrueFalseTotal
if $dbv->{"database"} 0 0 0
494 0 ▶ $dsn .= ';host=' . $dbv->{host} if $dbv->{host};
Branch: 1 branch
BranchTrueFalseTotal
if $dbv->{"host"} 0 0 0
495 0 ▶ $dsn .= ';port=' . $dbv->{port} if $dbv->{port};
Branch: 1 branch
BranchTrueFalseTotal
if $dbv->{"port"} 0 0 0
496  
497 } else {
498 0 ▶ $dsn .= ':f_dir=' . $dbv->{file} if $dbv->{file};
Branch: 1 branch
BranchTrueFalseTotal
if $dbv->{"file"} 0 0 0
499 0 ▶ $dsn .= ':database='. $dbv->{database} if $dbv->{database};
Branch: 1 branch
BranchTrueFalseTotal
if $dbv->{"database"} 0 0 0
500 0 ▶ $dsn .= ';host=' . $dbv->{host} if $dbv->{host};
Branch: 1 branch
BranchTrueFalseTotal
if $dbv->{"host"} 0 0 0
501 0 ▶ $dsn .= ';port=' . $dbv->{port} if $dbv->{port};
Branch: 1 branch
BranchTrueFalseTotal
if $dbv->{"port"} 0 0 0
502 }
503  
504 # LogDebug("dsn=[$dsn] user[$dbv->{user}] password[$dbv->{password}]" );
505  
506 0 eval {
507 $dbv->{dbh} = DBI->connect($dsn, $dbv->{user}, $dbv->{password},
508 0 { RaiseError => 1, AutoCommit => $ac });
509 };
510  
511 0 ▶ Croak("Cannot connect to DB [$dsn]: $@") if($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
512 0 return $dbv;
513 }
514  
515 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
516 0 my $dbv = shift;
517 # $dbv->{dbh}->commit if defined $dbv->{dbh};
518 0 ▶ $dbv->{dbh}->disconnect if defined $dbv->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
if defined $dbv->{"dbh"} 0 0 0
519 }
520  
521 12 1;
522  
523 __END__
« Labyrinth/Users.pm ↑ Summary Labyrinth/Mailer.pm »