diff options
author | Russ Allbery <rra@stanford.edu> | 2007-08-29 16:50:15 +0000 |
---|---|---|
committer | Russ Allbery <rra@stanford.edu> | 2007-08-29 16:50:15 +0000 |
commit | 44f55bbbb6e62588ed45092abed74efff97ae539 (patch) | |
tree | d3c5c8176164f44aa87006391259e25b68a170ea /perl/Wallet/Server.pm | |
parent | 9d0b4df45a4be981b8d23e9cd37e70298e3dec59 (diff) |
Wallet::Server is now responsible for connecting to the database. Don't
bother working hard to disconnect the database on exit, since the DESTROY
DBI method will do the right thing.
Diffstat (limited to 'perl/Wallet/Server.pm')
-rw-r--r-- | perl/Wallet/Server.pm | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/perl/Wallet/Server.pm b/perl/Wallet/Server.pm index 6a95e06..902e86d 100644 --- a/perl/Wallet/Server.pm +++ b/perl/Wallet/Server.pm @@ -36,14 +36,32 @@ $VERSION = '0.01'; ############################################################################## # Create a new wallet server object. A new server should be created for each -# user who is making changes to the wallet. Takes the database handle that -# will be used for all of the wallet metadata and the principal and host who -# are sending wallet requests. We also instantiate the administrative ACL, -# which we'll use for various things. Throw an exception if anything goes -# wrong. +# user who is making changes to the wallet. Takes the principal and host who +# are sending wallet requests. Opens a connection to the database that will +# be used for all of the wallet metadata based on the wallet configuration +# information. We also instantiate the administrative ACL, which we'll use +# for various things. Throw an exception if anything goes wrong. sub new { - my ($class, $dbh, $user, $host) = @_; + my ($class, $user, $host) = @_; my $acl = Wallet::ACL->new ('ADMIN'); + unless ($DB_DRIVER and ($DB_INFO or $DB_NAME)) { + die "database connection information not configured\n"; + } + my $dsn = "DBI:$DB_DRIVER:"; + if ($DB_INFO) { + $dsn .= $DB_INFO; + } else { + $dsn .= "database=$DB_NAME"; + $dsn .= ";host=$DB_HOST" if $DB_HOST; + $dsn .= ";port=$DB_PORT" if $DB_PORT; + } + my $dbh = DBI->connect ($dsn, $DB_USER, $DB_PASSWORD); + if (not defined $dbh) { + die "cannot connect to database: $DBI::errstr\n"; + } + $dbh->{AutoCommit} = 0; + $dbh->{RaiseError} = 1; + $dbh->{PrintError} = 0; my $self = { dbh => $dbh, user => $user, |