src/test/perllib/TrackDb.pm 1.4

1.4 2009/03/20 18:42:23 angie
Added proper HgConf usage including db.host, since we can no longer assume localhost etc.
Index: src/test/perllib/TrackDb.pm
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/test/perllib/TrackDb.pm,v
retrieving revision 1.3
retrieving revision 1.4
diff -b -B -U 1000000 -r1.3 -r1.4
--- src/test/perllib/TrackDb.pm	5 Nov 2003 22:38:21 -0000	1.3
+++ src/test/perllib/TrackDb.pm	20 Mar 2009 18:42:23 -0000	1.4
@@ -1,88 +1,99 @@
 #
 # TrackDb: interface to the trackDb database.
 #
 # Currently, all this does is return a list of trackName values.
 # Note the hardcoded default database, username & password.
 #
 package TrackDb;
 
 use strict;
 use vars qw($VERSION @ISA @EXPORT_OK);
 use Exporter;
 
+use FindBin qw($Bin);
+use lib $Bin;
+use HgConf;
+
 use DBI;
 use Carp;
 
 @ISA = qw(Exporter);
 @EXPORT_OK = qw( new DESTROY getTrackNames );
 $VERSION = '0.01';
 
 #
 # Some parameters...
 #
 my $defaultDb = 'hg10';
-my $username  = 'hguser';
-my $password  = 'hguserstuff';
 
 #
 # new: create a TrackDb object.
 # Mandatory argument: <none>
 # Optional arguments: $dbName (name of database to use instead of default)
 #
 sub new {
     my $class = shift;
     my $dbName = shift;
     confess "Too many arguments" if (defined shift);
     $dbName = $defaultDb if (! defined $dbName);
-    my $dbh = DBI->connect("DBI:mysql:$dbName", $username, $password);
+    my $hgConf = HgConf->new($main::opt_hgConf);
+    my $host = $hgConf->lookup('db.host');
+    if ($host && $host ne 'localhost') {
+        $host = ";host=$host";
+    } else {
+        $host = "";
+    }
+    my $username = $hgConf->lookup('db.user');
+    my $password = $hgConf->lookup('db.password');
+    my $dbh = DBI->connect("DBI:mysql:database=$dbName$host", $username, $password);
     confess "Can't connect to mysql database $dbName!" if (! $dbh);
     my $this = {
 	'dbh' => $dbh,
     };
     bless $this, $class;
 } # end new
 
 
 #
 # DESTROY: perl is supposed to call this destructor when the last 
 # ref to an object goes away.
 #
 sub DESTROY {
     my $this = shift;
     confess "Too many arguments" if (defined shift);
     $this->{'dbh'}->disconnect();
 } # end DESTROY
 
 #
 # getTrackNames
 # Mandatory argument: <none>
 #
 sub getTrackNames {
     my $this = shift;
     confess "Too many arguments" if (defined shift);
     my $results = 
 	$this->{'dbh'}->selectcol_arrayref("SELECT tableName from trackDb;");
     return @{$results};
 } # end getTrackNames
 
 
 #
 # getTrackNamesTypes
 # Mandatory argument: <none>
 #
 sub getTrackNamesTypes {
     my $this = shift;
     confess "Too many arguments" if (defined shift);
     my $results = 
 	$this->{'dbh'}->selectall_arrayref("SELECT tableName,type from trackDb;");
     my %namesTypes = ();
     foreach my $row (@{$results}) {
       my ($name,$type) = @{$row};
       $namesTypes{$name} = $type;
     }
     return %namesTypes;
 } # end getTrackNames
 
 
 # perl packages need to end by returning a positive value:
 1;