Skip to content
Snippets Groups Projects
Commit 28a00b37 authored by Glenn Proctor's avatar Glenn Proctor
Browse files

Created; base class for parsers that connect to databases.

parent 6544e72d
No related branches found
No related tags found
No related merge requests found
package XrefParser::DatabaseParser;
use strict;
use DBI;
use XrefParser::BaseParser;
use vars qw(@ISA);
@ISA = qw(XrefParser::BaseParser);
# Base class for parsers that parse from databases rather than files
# Format of $dsn (in populate_metadata.sql)
# mysql:HOST:PORT:DATABASE:USERNAME:PASSWORD
# e.g.
# mysql:ecs4:3350:glenn_elegans_xrefs:ensro::
my $db;
# Parse the DSN.
# Return hash with keys: host, port, database, user, pass
sub parse_dsn {
my ($self,$dsn) = @_;
my %hash;
my @bits = split /:/, $dsn;
$hash{'host'} = $bits[1];
$hash{'port'} = $bits[2];
$hash{'database'} = $bits[3];
$hash{'user'} = $bits[4];
$hash{'pass'} = $bits[5];
return %hash;
}
# Connect to a database. Return DB connection.
sub connect {
my ($self, $dsn) = @_;
my %dsn = $self->parse_dsn($dsn);
$db = DBI->connect( "DBI:mysql:host=" . $dsn{'host'} . ":port=" . $dsn{'port'} . ";database=" . $dsn{'database'}, $dsn{'user'}, $dsn{'pass'},
{'RaiseError' => 1}) || die "Can't connect to database\n";
return $db;
}
sub db {
return $db;
}
sub new {
my $self = {};
bless $self, "XrefParser::DatabaseParser";
return $self;
}
1;
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment