GeniDB.pm.in 4.65 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
Leigh B Stoller's avatar
Leigh B Stoller committed
3
# Copyright (c) 2008-2012, 2015 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# 
# {{{GENIPUBLIC-LICENSE
# 
# GENI Public License
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
# 
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
# 
# }}}
29 30 31 32 33 34 35 36
#
package GeniDB;
use strict;
use emdbi;
use Exporter;
use vars qw(@ISA @EXPORT);
@ISA    = "Exporter";
@EXPORT = qw(DBQuery DBQueryFatal DBQueryWarn DBWarn DBFatal DBErr
37
	     DBQuoteSpecial DBTableExists DBSlotExists DBSetDefault
38 39
	     DBSlotType DBKeyExists
	     DBConnect GENISA_DBNAME GENICH_DBNAME GENICM_DBNAME);
40 41 42 43 44 45 46 47

# Configure variables
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";

# We get this back from emdbi and use it in calls.
my $dbnum;

48 49 50 51 52
# DBNames.
sub GENISA_DBNAME()	{ return "geni"; }
sub GENICM_DBNAME()	{ return "geni-cm"; }
sub GENICH_DBNAME()	{ return "geni-ch"; }

53 54 55 56 57 58 59
sub DBQuery($)		{ return emdbi::DBQueryN($dbnum, $_[0]); }
sub DBQueryFatal($)	{ return emdbi::DBQueryFatalN($dbnum, $_[0]); }
sub DBQueryWarn($)	{ return emdbi::DBQueryWarnN($dbnum, $_[0]); }
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); }
60 61
sub DBTableExists($)	{ return emdbi::DBTableExistsN($dbnum, $_[0]); }
sub DBSlotExists($$)	{ return emdbi::DBSlotExistsN($dbnum, $_[0], $_[1]); }
62
sub DBKeyExists($$)	{ return emdbi::DBKeyExistsN($dbnum, $_[0], $_[1]); }
63
sub DBSlotType($$)	{ return emdbi::DBSlotTypeN($dbnum, $_[0], $_[1]); }
64

Leigh B. Stoller's avatar
Leigh B. Stoller committed
65
$emdbi::DBQUERY_DEBUG = 0;
66

67 68
my $GENI_DBNAME =
    (defined($main::GENI_DBNAME) ? $main::GENI_DBNAME : GENISA_DBNAME());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
69

70 71 72 73 74 75
# Default connection. For boot strapping, check to see of DB actually
# exists before trying to connect.
if (emdbi::DBExists("$GENI_DBNAME")) {
    $dbnum = emdbi::NewTBDBHandle("$GENI_DBNAME")
	if (!defined($dbnum));
}
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
#
# 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($$) {
Leigh B Stoller's avatar
Leigh B Stoller committed
138
    return emdbi::DBQueryN($_[0]->DBnum(), $_[1]); }
139
sub QueryFatal($$) {
Leigh B Stoller's avatar
Leigh B Stoller committed
140
    return emdbi::DBQueryFatalN($_[0]->DBnum(), $_[1]); }
141
sub QueryWarn($$) {
Leigh B Stoller's avatar
Leigh B Stoller committed
142
    return emdbi::DBQueryWarnN($_[0]->DBnum(), $_[1]); }
143
sub QuoteSpecial($$) {
Leigh B Stoller's avatar
Leigh B Stoller committed
144
    return emdbi::DBQuoteSpecialN($_[0]->DBnum(), $_[1]); }
145 146 147 148 149 150 151
sub Warn($$;$) {
    emdbi::DBError($_[0], $_[1]); }
sub Fatal($$;$) {
    emdbi::DBFatal($_[0], $_[1]); }
sub Err($) {
    return emdbi::DBErrN($_[0]->DBnum()); }
sub TableExists($$) {
Leigh B Stoller's avatar
Leigh B Stoller committed
152
    return emdbi::DBTableExistsN($_[0]->DBnum(), $_[1]); }
153
sub SlotExists($$$) {
Leigh B Stoller's avatar
Leigh B Stoller committed
154
    return emdbi::DBSlotExistsN($_[0]->DBnum(), $_[1], $_[2]); }
155
sub KeyExists($$$) {
Leigh B Stoller's avatar
Leigh B Stoller committed
156
    return emdbi::DBKeyExistsN($_[0]->DBnum(), $_[1], $_[2]); }
157
sub SlotType($$$) {
Leigh B Stoller's avatar
Leigh B Stoller committed
158
    return emdbi::DBSlotExistsN($_[0]->DBnum(), $_[1], $_[2]); }
159

160 161 162
# _Always_ make sure that this 1 is at the end of the file...
1;