Perform/DButils.pm untested

filter: Statementstmt 0.0% 0 / 25 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 38 CC 3 SCAR 19.5
CC
3
Cov
0%
open_db
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 7.0
1  
2 package DBIx::Perform::DButils;
3 use strict;
4 use base 'Exporter';
5  
6 our @EXPORT_OK = qw(&open_db);
7 our $VERSION = '0.04';
8  
9 use DBI;
10  
11 our %DB_MARKERS =
12 ( Pg => { dbname => 'dbname=',
13 host => 'host=',
14 },
15 mysql => { dbname => 'database=',
16 host => 'host=',
17 },
18 );
19  
20 sub open_db
21 {
22 my $dbname = shift; # May be a connect arg.
23  
24 my $connect_arg = $dbname;
25 if ($connect_arg !~ /^dbi:/) { # not already a DBI connect-arg...
26 my $dbtype = $ENV{DB_CLASS} || 'Pg';
27 $connect_arg = "dbi:$dbtype:";
28 my $specifics = $DB_MARKERS{$dbtype};
29 $connect_arg .= $$specifics{'dbname'} . $dbname . ";";
30 my $host = $ENV{DB_HOST};
31 $connect_arg .= $$specifics{'host'} . $host . ";"
32 if ($host);
33 }
34 my $dbuser = $ENV{DB_USER};
35 my $dbpass = $ENV{DB_PASSWORD};
36 my $dbh = DBI->connect($connect_arg, $dbuser, $dbpass,
37 {PrintError => 0})
38 or die "Unable to connect to '$connect_arg' as user '$dbuser'";
39 return $dbh;
40 }
41  
42 1;