File Coverage

blib/lib/Fault/Dbh.pm
Criterion Covered Total %
statement 9 35 25.7
branch 0 16 0.0
condition n/a
subroutine 3 8 37.5
pod 4 4 100.0
total 16 63 25.4


line stmt bran cond sub pod time code
1             #================================== Dbh.pm ===================================
2             # Filename: Dbh.pm
3             # Description: Objectifies Database handles so we only need one
4             # Original Author: Dale M. Amon
5             # Revised by: $Author: amon $
6             # Date: $Date: 2008-08-28 23:20:19 $
7             # Version: $Revision: 1.5 $
8             # License: LGPL 2.1, Perl Artistic or BSD
9             #
10             #=============================================================================
11 1     1   601 use strict;
  1         2  
  1         29  
12 1     1   2414 use DBI;
  1         26260  
  1         100  
13              
14             package Fault::Dbh;
15 1     1   13 use vars qw{@ISA};
  1         3  
  1         440  
16             @ISA = qw ( UNIVERSAL );
17              
18             #=============================================================================
19             # CLASS METHODS
20             #=============================================================================
21             my ($DBH,$DBHCNT) = (undef,0);
22              
23 0     0 1   sub init {my $class=shift; ($DBH,$DBHCNT) = (undef,0); return $class;}
  0            
  0            
24              
25              
26             #-----------------------------------------------------------------------------
27             # Open a database server connection if one is not already open.
28              
29             sub open {
30 0     0 1   my ($class,$dbname,$user,$pass) = @_;
31 0 0         defined $dbname or return undef;
32 0 0         defined $user or return undef;
33 0 0         defined $pass or return undef;
34              
35 0 0         if (defined $DBH) {$DBHCNT++;}
  0            
  0            
36             else {$DBH = DBI->connect("DBI:mysql:$dbname",$user,$pass);
37 0 0         $DBHCNT = (defined $DBH) ? 1 : 0;
38             }
39 0           my $self = bless (\$DBH, "Fault::Dbh");
40 0           return $self;
41             }
42              
43             #=============================================================================
44             # INSTANCE METHODS
45             #=============================================================================
46             # Return the database handle. (I could have done $$self, but why bother?)
47              
48 0     0 1   sub dbh {return $DBH;}
49              
50             #-----------------------------------------------------------------------------
51             # Close the database. Once this is done this object should be considered
52             # *dead*.
53              
54             sub close {
55 0     0 1   my $self = shift;
56              
57 0 0         if ($DBHCNT>1) {$DBHCNT--;}
  0 0          
  0            
58 0           elsif ($DBHCNT == 1) {$DBHCNT=0; $DBH->disconnect; $DBH=undef;}
  0            
  0            
59             else {warn ("Attempt to close an already closed dbh. Probable cause is " .
60             "a mismatch in the number of Dbh Class opens and closes.");}
61 0           return undef;
62             }
63              
64             #-----------------------------------------------------------------------------
65             # We need our own destructor so we can insure the database handle is
66             # disconnected before garbage collection.
67              
68             sub DESTROY {
69 0     0     my $self = shift;
70 0 0         (defined $DBH) and $self->close;
71 0           printf "\n\n**** WHY DID I CLOSE??? *****\n\n";
72 0           return $self;
73             }
74            
75             #=============================================================================
76             # Pod Documentation
77             #=============================================================================
78             # You may extract and format the documentation section with the 'perldoc' cmd.
79              
80             =head1 NAME
81              
82             Fault::Dbh - Database Handle abstraction.
83              
84             =head1 SYNOPSIS
85              
86             use Fault::Dbh;
87             Fault::Dbh->init;
88             $db = Fault::Dbh->open ($db, $usr, $pass);
89             $dbh = $db->dbh;
90             $db->close;
91              
92             =head1 Description
93              
94             The Fault::Dbh Database handle abstraction centralizes the creation and
95             destruction of a database handle for a connection to the database server. I
96             do this to minimize the number of active socket connections to the database
97             server. I have observed situations in which all available processes have been
98             utilized, causing further access attempts to fail.
99              
100             This is currently only coded to function on a single local MySQL database. If
101             multiple databases are required, I will have to get fancier, perhaps a local
102             hash of database names with handles attached.
103              
104             The init method is supplied for use in forked environments. Since only a
105             single database connection is created by open, no matter how many times
106             you call it, you will get into very deep trouble if you open then fork and
107             access the database from both processes. If you fork, use the init method
108             as one of the first things you do in your child process. If you do not do
109             this, do not come crying to me about the really weird random error messages
110             and connection closures you are getting from your database.
111              
112             If I wanted to, I could subclass the DBI::db handle itself, but I did not
113             study enough of it to make sure I did not step on anything,
114              
115             Error handling is currently minimal; virtually anything that goes wrong will
116             cause the return of a pointer with a value of undef.
117              
118             =head1 Examples
119              
120             use Fault::Dbh;
121             Fault::Dbh->init;
122             $db = Fault::Dbh->open ("mydatabase","me","apassword");
123             $dbh = $db->dbh;
124             $db->close;
125              
126             =head1 Class Variables (Internal)
127              
128             DBH the database handle or undef
129             DBHCNT number of opens on this handle, zero if closed.
130              
131             =head1 Instance Variables
132              
133             None.
134              
135             =head1 Class Methods
136              
137             =over 4
138              
139             =item Binit>
140              
141             Initialize the local database handles. This discards any handle which was
142             previously opened. We need this because if we fork a process the old handle
143             gets shared among parent and child processes and if any two attempt to
144             communicate with the db at the same time...
145              
146             If you are only working with a single process, you only need to use open and
147             close. If you fork, you should init as one of the very first things you do
148             in the new process.
149              
150             =item B<$dbh = Fault::Dbh-Eopen ($db, $usr, $pass)>
151              
152             Class method to create a new object to handle a connection to the local
153             database server for $db as user $usr with password $pass. It only supports
154             one localhost database at present. A new connection is opened only if the
155             count of open connections is zero; otherwise it re-uses the currently open
156             one.
157              
158             It returns undef if it fails to make the requested connection.
159              
160             =back 4
161              
162             =head1 Instance Methods
163              
164             =over 4
165              
166             =item B<$dbh = $db-Edbh>
167              
168             Return the database handle.
169              
170             =item B<$db-Eclose>
171              
172             Close this connection to the database server. It decrements the count of open
173             connections and does the real disconnect if the count reaches zero.
174              
175             =back 4
176              
177             =head1 Private Class Methods
178              
179             None.
180              
181             =head1 Private Instance Methods
182              
183             None.
184              
185             =head1 Errors and Warnings
186              
187             None.
188              
189             =head1 KNOWN BUGS
190              
191             See TODO.
192              
193             =head1 SEE ALSO
194              
195             DBI
196              
197             =head1 AUTHOR
198              
199             Dale Amon
200              
201             =cut
202            
203             #=============================================================================
204             # CVS HISTORY
205             #=============================================================================
206             # $Log: Dbh.pm,v $
207             # Revision 1.5 2008-08-28 23:20:19 amon
208             # perldoc section regularization.
209             #
210             # Revision 1.4 2008-08-17 21:56:37 amon
211             # Make all titles fit CPAN standard.
212             #
213             # Revision 1.3 2008-05-07 17:44:17 amon
214             # Documentation changes; removed use of package DMA::
215             #
216             # Revision 1.2 2008-05-04 14:34:12 amon
217             # Tidied up code and docs.
218             #
219             # Revision 1.1.1.1 2008-04-18 12:44:03 amon
220             # Fault and Log System. Pared off of DMA base lib.
221             #
222             # Revision 1.6 2008-04-18 12:44:03 amon
223             # Added arg checking and bail out to open method.
224             #
225             # Revision 1.5 2008-04-11 22:25:23 amon
226             # Add blank line after cut.
227             #
228             # Revision 1.4 2008-04-11 18:56:35 amon
229             # Fixed quoting problem with formfeeds.
230             #
231             # Revision 1.3 2008-04-11 18:39:15 amon
232             # Implimented new standard for headers and trailers.
233             #
234             # Revision 1.2 2008-04-10 15:01:08 amon
235             # Added license to headers, removed claim that the documentation section still
236             # relates to the old doc file.
237             #
238             # Revision 1.1.1.1 2004-12-02 14:28:14 amon
239             # Dale's library of primitives in Perl
240             #
241             # 20041128 Dale Amon
242             # Added init method to handle multiprocessing problems.
243             #
244             # Revision 1.1 2001/05/23 17:05:40 amon
245             # Added Dbh
246             #
247             # 20010515 Dale Amon
248             # Created
249             1;