1
2
3
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
|
# Wallet::Database -- Wallet system database connection management.
#
# 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, 2010 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.03';
##############################################################################
# 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
=for stopwords
DBI RaiseError PrintError AutoCommit Allbery
=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 overridden 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 L<Wallet::Config> 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)
This module is part of the wallet system. The current version is
available from L<http://www.eyrie.org/~eagle/software/wallet/>.
=head1 AUTHOR
Russ Allbery <rra@stanford.edu>
=cut
|