diff options
author | Russ Allbery <rra@stanford.edu> | 2008-01-22 22:46:34 +0000 |
---|---|---|
committer | Russ Allbery <rra@stanford.edu> | 2008-01-22 22:46:34 +0000 |
commit | 8014b90805aa85adf1d1155cbb07fa77e5c58f62 (patch) | |
tree | 12368474af83534d7a9ba0f30a222e35a55d33df /perl | |
parent | 6bc84715b9e490500f4bdab1417c27d0e9d31e5c (diff) |
Add a Wallet::Database class that now holds the database connection code
previously in Wallet::Server. Remove all the attribute setting on
database handles in the other classes since Wallet::Database handles that
initialization.
Diffstat (limited to 'perl')
-rw-r--r-- | perl/Wallet/ACL.pm | 10 | ||||
-rw-r--r-- | perl/Wallet/Database.pm | 128 | ||||
-rw-r--r-- | perl/Wallet/Object/Base.pm | 10 | ||||
-rw-r--r-- | perl/Wallet/Server.pm | 41 |
4 files changed, 138 insertions, 51 deletions
diff --git a/perl/Wallet/ACL.pm b/perl/Wallet/ACL.pm index dab5581..b9c710c 100644 --- a/perl/Wallet/ACL.pm +++ b/perl/Wallet/ACL.pm @@ -2,7 +2,7 @@ # $Id$ # # Written by Russ Allbery <rra@stanford.edu> -# Copyright 2007 Board of Trustees, Leland Stanford Jr. University +# Copyright 2007, 2008 Board of Trustees, Leland Stanford Jr. University # # See LICENSE for licensing terms. @@ -22,7 +22,7 @@ use POSIX qw(strftime); # This version should be increased on any code change to this module. Always # use two digits for the minor version with a leading zero if necessary so # that it will sort properly. -$VERSION = '0.04'; +$VERSION = '0.05'; ############################################################################## # Constructors @@ -34,9 +34,6 @@ $VERSION = '0.04'; # doesn't exist, throws an exception. sub new { my ($class, $id, $dbh) = @_; - $dbh->{RaiseError} = 1; - $dbh->{PrintError} = 0; - $dbh->{AutoCommit} = 0; my ($sql, $data, $name); if ($id =~ /^\d+\z/) { $sql = 'select ac_id, ac_name from acls where ac_id = ?'; @@ -70,9 +67,6 @@ sub create { if ($name =~ /^\d+\z/) { die "ACL name may not be all numbers\n"; } - $dbh->{RaiseError} = 1; - $dbh->{PrintError} = 0; - $dbh->{AutoCommit} = 0; $time ||= time; my $id; eval { diff --git a/perl/Wallet/Database.pm b/perl/Wallet/Database.pm new file mode 100644 index 0000000..f32b37e --- /dev/null +++ b/perl/Wallet/Database.pm @@ -0,0 +1,128 @@ +# Wallet::Database -- Wallet system database connection management. +# $Id$ +# +# This module is a thin wrapper around DBI to handle determination of the +# database driver and configuration settings automatically on connect. The +# intention is that Wallet::Database objects can be treated in all respects +# like DBI objects in the rest of the code. +# +# Written by Russ Allbery <rra@stanford.edu> +# Copyright 2008 Board of Trustees, Leland Stanford Jr. University +# +# See LICENSE for licensing terms. + +############################################################################## +# Modules and declarations +############################################################################## + +# Set up the subclasses. This is required to avoid warnings under DBI 1.40 +# and later, even though we don't actually make use of any overridden +# statement handle or database handle methods. +package Wallet::Database::st; +use vars qw(@ISA); +@ISA = qw(DBI::st); + +package Wallet::Database::db; +use vars qw(@ISA); +@ISA = qw(DBI::db); + +package Wallet::Database; +require 5.006; + +use strict; +use vars qw(@ISA $VERSION); + +use DBI; +use Wallet::Config; + +@ISA = qw(DBI); + +# This version should be increased on any code change to this module. Always +# use two digits for the minor version with a leading zero if necessary so +# that it will sort properly. +$VERSION = '0.01'; + +############################################################################## +# Core overrides +############################################################################## + +# Override DBI::connect to supply our own connect string, username, and +# password and to set some standard options. Takes no arguments other than +# the implicit class argument. +sub connect { + my ($class) = @_; + unless ($Wallet::Config::DB_DRIVER + and (defined ($Wallet::Config::DB_INFO) + or defined ($Wallet::Config::DB_NAME))) { + die "database connection information not configured\n"; + } + my $dsn = "DBI:$Wallet::Config::DB_DRIVER:"; + if (defined $Wallet::Config::DB_INFO) { + $dsn .= $Wallet::Config::DB_INFO; + } else { + $dsn .= "database=$Wallet::Config::DB_NAME"; + $dsn .= ";host=$Wallet::Config::DB_HOST" if $Wallet::Config::DB_HOST; + $dsn .= ";port=$Wallet::Config::DB_PORT" if $Wallet::Config::DB_PORT; + } + my $user = $Wallet::Config::DB_USER; + my $pass = $Wallet::Config::DB_PASSWORD; + my %attrs = (PrintError => 0, RaiseError => 1, AutoCommit => 0); + my $dbh = eval { $class->SUPER::connect ($dsn, $user, $pass, \%attrs) }; + if ($@) { + die "cannot connect to database: $@\n"; + } + return $dbh; +} + +1; +__END__ + +############################################################################## +# Documentation +############################################################################## + +=head1 NAME + +Wallet::Dabase - Wrapper module for wallet database connections + +=head1 SYNOPSIS + + use Wallet::Database; + my $dbh = Wallet::Database->connect; + +=head1 DESCRIPTION + +Wallet::Database is a thin wrapper module around DBI that takes care of +building a connect string and setting database options based on wallet +configuration. The only overriden method is connect(). All other methods +should work the same as in DBI and Wallet::Database objects should be +usable exactly as if they were DBI objects. + +connect() will obtain the database connection information from the wallet +configuration; see Wallet::Config(3) for more details. It will also +automatically set the RaiseError attribute to true and the PrintError and +AutoCommit attributes to false, matching the assumptions made by the +wallet database code. + +=head1 CLASS METHODS + +=over 4 + +=item connect() + +Opens a new database connection and returns the database object. On any +failure, throws an exception. Unlike the DBI method, connect() takes no +arguments; all database connection information is derived from the wallet +configuration. + +=back + +=head1 SEE ALSO + +DBI(3), Wallet::Config(3) + +=head1 AUTHOR + +Russ Allbery <rra@stanford.edu> + +=cut diff --git a/perl/Wallet/Object/Base.pm b/perl/Wallet/Object/Base.pm index e09c477..e712fd7 100644 --- a/perl/Wallet/Object/Base.pm +++ b/perl/Wallet/Object/Base.pm @@ -2,7 +2,7 @@ # $Id$ # # Written by Russ Allbery <rra@stanford.edu> -# Copyright 2007 Board of Trustees, Leland Stanford Jr. University +# Copyright 2007, 2008 Board of Trustees, Leland Stanford Jr. University # # See LICENSE for licensing terms. @@ -23,7 +23,7 @@ use Wallet::ACL; # This version should be increased on any code change to this module. Always # use two digits for the minor version with a leading zero if necessary so # that it will sort properly. -$VERSION = '0.03'; +$VERSION = '0.04'; ############################################################################## # Constructors @@ -36,9 +36,6 @@ $VERSION = '0.03'; # probably be usable as-is by most object types. sub new { my ($class, $type, $name, $dbh) = @_; - $dbh->{RaiseError} = 1; - $dbh->{PrintError} = 0; - $dbh->{AutoCommit} = 0; my $sql = 'select ob_name from objects where ob_type = ? and ob_name = ?'; my $data = $dbh->selectrow_array ($sql, undef, $type, $name); $dbh->commit; @@ -58,9 +55,6 @@ sub new { # in the object. Subclasses may need to override this to do additional setup. sub create { my ($class, $type, $name, $dbh, $user, $host, $time) = @_; - $dbh->{RaiseError} = 1; - $dbh->{PrintError} = 0; - $dbh->{AutoCommit} = 0; $time ||= time; die "invalid object type\n" unless $type; die "invalid object name\n" unless $name; diff --git a/perl/Wallet/Server.pm b/perl/Wallet/Server.pm index 6be7e59..68add52 100644 --- a/perl/Wallet/Server.pm +++ b/perl/Wallet/Server.pm @@ -2,7 +2,7 @@ # $Id$ # # Written by Russ Allbery <rra@stanford.edu> -# Copyright 2007 Board of Trustees, Leland Stanford Jr. University +# Copyright 2007, 2008 Board of Trustees, Leland Stanford Jr. University # # See LICENSE for licensing terms. @@ -18,47 +18,18 @@ use vars qw(%MAPPING $VERSION); use Wallet::ACL; use Wallet::Config; +use Wallet::Database; use Wallet::Schema; # This version should be increased on any code change to this module. Always # use two digits for the minor version with a leading zero if necessary so # that it will sort properly. -$VERSION = '0.05'; +$VERSION = '0.06'; ############################################################################## # Utility methods ############################################################################## -# Opens a database connection. This is an internal class method used by both -# initialize and new. Throws an exception if anything goes wrong; otherwise, -# returns the open database handle. -sub _open_db { - my ($class) = @_; - unless ($Wallet::Config::DB_DRIVER - and (defined ($Wallet::Config::DB_INFO) - or defined ($Wallet::Config::DB_NAME))) { - die "database connection information not configured\n"; - } - my $dsn = "DBI:$Wallet::Config::DB_DRIVER:"; - if (defined $Wallet::Config::DB_INFO) { - $dsn .= $Wallet::Config::DB_INFO; - } else { - $dsn .= "database=$Wallet::Config::DB_NAME"; - $dsn .= ";host=$Wallet::Config::DB_HOST" if $Wallet::Config::DB_HOST; - $dsn .= ";port=$Wallet::Config::DB_PORT" if $Wallet::Config::DB_PORT; - } - my $user = $Wallet::Config::DB_USER; - my $password = $Wallet::Config::DB_PASSWORD; - my $dbh = DBI->connect ($dsn, $user, $password, { PrintError => 0 }); - if (not defined $dbh) { - die "cannot connect to database: $DBI::errstr\n"; - } - $dbh->{RaiseError} = 1; - $dbh->{PrintError} = 0; - $dbh->{AutoCommit} = 0; - return $dbh; -} - # Initializes the database by populating it with our schema and then creates # and returns a new wallet server object. This is used only for initial # database creation. Takes the Kerberos principal who will be the default @@ -66,7 +37,7 @@ sub _open_db { # exception on failure. sub initialize { my ($class, $user) = @_; - my $dbh = $class->_open_db; + my $dbh = Wallet::Database->connect; my $schema = Wallet::Schema->new; $schema->create ($dbh); my $acl = Wallet::ACL->create ('ADMIN', $dbh, $user, 'localhost'); @@ -82,7 +53,7 @@ sub initialize { # failure. sub reinitialize { my ($class, $user) = @_; - my $dbh = $class->_open_db; + my $dbh = Wallet::Database->connect; my $schema = Wallet::Schema->new; $schema->drop ($dbh); $dbh->disconnect; @@ -97,7 +68,7 @@ sub reinitialize { # for various things. Throw an exception if anything goes wrong. sub new { my ($class, $user, $host) = @_; - my $dbh = $class->_open_db; + my $dbh = Wallet::Database->connect; my $acl = Wallet::ACL->new ('ADMIN', $dbh); my $self = { dbh => $dbh, |