Commit cba00b15 authored by Leigh Stoller's avatar Leigh Stoller

Some changes for dealing with connections to the different DBs.

parent 65f255fc
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2008 University of Utah and the Flux Group.
# Copyright (c) 2008-2009 University of Utah and the Flux Group.
# All rights reserved.
#
package GeniDB;
......@@ -11,7 +11,8 @@ use Exporter;
use vars qw(@ISA @EXPORT);
@ISA = "Exporter";
@EXPORT = qw(DBQuery DBQueryFatal DBQueryWarn DBWarn DBFatal DBErr
DBQuoteSpecial);
DBQuoteSpecial DBTableExists DBSlotExists DBSetDefault
DBSlotType DBConnect GENISA_DBNAME GENICH_DBNAME GENICM_DBNAME);
# Configure variables
my $TB = "@prefix@";
......@@ -20,6 +21,11 @@ my $TBOPS = "@TBOPSEMAIL@";
# We get this back from emdbi and use it in calls.
my $dbnum;
# DBNames.
sub GENISA_DBNAME() { return "geni"; }
sub GENICM_DBNAME() { return "geni-cm"; }
sub GENICH_DBNAME() { return "geni-ch"; }
sub DBQuery($) { return emdbi::DBQueryN($dbnum, $_[0]); }
sub DBQueryFatal($) { return emdbi::DBQueryFatalN($dbnum, $_[0]); }
sub DBQueryWarn($) { return emdbi::DBQueryWarnN($dbnum, $_[0]); }
......@@ -27,15 +33,100 @@ sub DBQuoteSpecial($) { return emdbi::DBQuoteSpecialN($dbnum, $_[0]); }
sub DBWarn($;$) { emdbi::DBError($_[0], $_[1]); }
sub DBFatal($;$) { emdbi::DBFatal($_[0], $_[1]); }
sub DBErr() { return emdbi::DBErrN($dbnum); }
sub DBTableExists($) { return emdbi::DBTableExistsN($dbnum, $_[0]); }
sub DBSlotExists($$) { return emdbi::DBSlotExistsN($dbnum, $_[0], $_[1]); }
sub DBSlotType($$) { return emdbi::DBSlotTypeN($dbnum, $_[0], $_[1]); }
$emdbi::DBQUERY_DEBUG = 0;
my $GENI_DBNAME = (defined($main::GENI_DBNAME) ? $main::GENI_DBNAME : "geni");
my $GENI_DBNAME =
(defined($main::GENI_DBNAME) ? $main::GENI_DBNAME : GENISA_DBNAME());
# Default connection.
$dbnum = emdbi::NewTBDBHandle("$GENI_DBNAME")
if (!defined($dbnum));
#
# But provide a way to change who the default DB is. Return old default.
#
sub DBSetDefault($)
{
my $newdbnum = $_[0];
my $olddbnum = $dbnum;
$dbnum = $newdbnum;
return $olddbnum;
}
#
# Connect to a DB and make it the default.
#
sub DBConnect($)
{
my ($newname) = @_;
return $dbnum
if ($newname eq $GENI_DBNAME);
$dbnum = emdbi::NewTBDBHandle($newname);
$GENI_DBNAME = $newname;
return $dbnum;
}
# Cache of object connections.
my %dbcache = ();
#
# But also provide an object interface so we can talk to different DBs
# at the same time.
#
sub New($$)
{
my ($class, $dbname) = @_;
my $dbnumber;
if (exists($dbcache{$dbname})) {
return $dbcache{$dbname};
}
if ($dbname eq $GENI_DBNAME) {
$dbnumber = $dbnum;
}
else {
$dbnumber = emdbi::NewTBDBHandle($dbname);
}
my $self = {};
$self->{'dbnum'} = $dbnumber;
bless($self, $class);
# Add to cache.
$dbcache{$dbname} = $self;
return $self;
}
sub DBnum($) {
return $_[0]->{'dbnum'}; }
sub Query($$) {
return emdbi::DBQueryN($_[0]->DBnum(), $_[0]); }
sub QueryFatal($$) {
return emdbi::DBQueryFatalN($_[0]->DBnum(), $_[0]); }
sub QueryWarn($$) {
return emdbi::DBQueryWarnN($_[0]->DBnum(), $_[0]); }
sub QuoteSpecial($$) {
return emdbi::DBQuoteSpecialN($_[0]->DBnum(), $_[0]); }
sub Warn($$;$) {
emdbi::DBError($_[0], $_[1]); }
sub Fatal($$;$) {
emdbi::DBFatal($_[0], $_[1]); }
sub Err($) {
return emdbi::DBErrN($_[0]->DBnum()); }
sub TableExists($$) {
return emdbi::DBTableExistsN($_[0]->DBnum(), $_[0]); }
sub SlotExists($$$) {
return emdbi::DBSlotExistsN($_[0]->DBnum(), $_[0], $_[1]); }
sub SlotType($$$) {
return emdbi::DBSlotExistsN($_[0]->DBnum(), $_[0], $_[1]); }
# _Always_ make sure that this 1 is at the end of the file...
1;
Markdown is supported
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