Add a method so that DatabaseLink can tell you what database it's

operating on.
Add tests for the method.
This commit is contained in:
Colin Kuskie 2007-07-14 19:33:14 +00:00
parent 15e3894b86
commit 6b2ed836fb
2 changed files with 77 additions and 0 deletions

View file

@ -100,6 +100,33 @@ sub delete {
#-------------------------------------------------------------------
=head3 databaseName ( )
Based on the DSN, figures out what the database name is.
=cut
sub databaseName {
my $self = shift;
return $self->{_databaseName} if $self->{_databaseName};
my @dsnEntries = split(/[:;]/, $self->get->{DSN});
my $databaseName;
if ($dsnEntries[2] !~ /=/) {
$databaseName = $dsnEntries[2];
} else {
foreach (@dsnEntries) {
if ($_ =~ m/^(database|db|dbname)=(.+)$/) {
$databaseName = $2;
last;
}
}
}
return $databaseName;
}
#-------------------------------------------------------------------
=head2 disconnect ( )
Disconnect cleanly from the current databaseLink. You should always use this method rather than the disconnect method of the actual WebGUI::SQL database handle otherwise you may accidentally close the database handle to the WebGUI database prematurely.

50
t/DatabaseLink.t Normal file
View file

@ -0,0 +1,50 @@
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2007 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use FindBin;
use strict;
use lib "$FindBin::Bin/lib";
use WebGUI::Test;
use WebGUI::Session;
use WebGUI::DatabaseLink;
use Test::More; # increment this value for each test you create
my $session = WebGUI::Test->session;
my $DSNs = [
{
dsn => 'DBI:mysql:colonSeparated:myHost:8008',
dbName => 'colonSeparated',
},
{
dsn => 'DBI:mysql:database=myDatabase',
dbName => 'myDatabase',
},
{
dsn => 'DBI:mysql:dbName=myDbName',
dbName => 'myDbName',
},
];
plan tests => 2;
my $dbLink = WebGUI::DatabaseLink->new($session, 0);
is($dbLink->get->{DSN}, $session->config->get('dsn'), 'DSN set correctly for default database link');
my ($databaseName) = $session->db->quickArray('SELECT DATABASE()');
is ($dbLink->databaseName, $databaseName, 'databaseName parsed default DSN from config file');
END {
foreach my $link ($dbLink, ) {
$link->delete if (defined $link and ref $link eq 'WebGUI::DatabaseLink');
}
}