| 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; |