File Coverage

blib/lib/Gantry/Utils/DBConnHelper.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition 1 2 50.0
subroutine 5 6 83.3
pod 2 2 100.0
total 21 25 84.0


line stmt bran cond sub pod time code
1             package Gantry::Utils::DBConnHelper;
2 2     2   10 use strict; use warnings;
  2     2   4  
  2         51  
  2         8  
  2         3  
  2         39  
3              
4 2     2   8 use Carp;
  2         15  
  2         344  
5              
6             my $subclass;
7              
8             sub import {
9 2     2   4 my $class = shift;
10 2   50     42 my $conn_info = shift || return;
11              
12 0         0 $class->set_conn_info( $conn_info );
13             }
14              
15             sub set_subclass {
16 2     2 1 3 my $class = shift;
17 2         6 $subclass = shift;
18             }
19              
20             sub get_subclass {
21 0     0 1   return $subclass;
22             }
23              
24             1;
25              
26             =head1 NAME
27              
28             Gantry::Utils::DBConnHelper - connection info and dbh cache manager base module
29              
30             =head1 SYNOPSIS
31              
32             package Gantry::Utils::DBConnHelper::YourHelper;
33              
34             use base 'Gantry::Utils::DBConnHelper';
35              
36             Gantry::Utils::DBConnHelper->set_subclass(
37             'Gantry::Utils::DBConnHelper::YourHelper'
38             );
39              
40             sub get_dbh {...}
41             sub set_dbh {...}
42             sub get_conn_info {...}
43             sub set_conn_info {...} # only for some helpers
44             sub get_auth_dbh {...}
45             sub set_auth_dbh {...}
46             sub get_auth_conn_info {...}
47             sub set_auth_conn_info {...} # only for some helpers
48              
49             =head1 DESCRIPTION
50              
51             This is mostly a documentation module. You should probably use one of the
52             available implementing modules like Gantry::Utils::DBConnHelper::Script,
53             Gantry::Utils::DBConnHelper::MP13, etc. If none of those fit your needs
54             you need to subclass this modules and define all of the methods listed below
55             (see the synopsis for an example). If you choose to subclass, you will
56             inherit the import method from this module. It allows your callers to pass
57             a hash reference of database connection info in their use statement, instead
58             of calling set_conn_info. This only works for the Script helper.
59              
60             =head1 METHODS of this class
61              
62             =over 4
63              
64             =item set_subclass
65              
66             Your subclass MUST call this method at compile time passing in the
67             fully qualified name of your subclass.
68              
69             =item get_subclass
70              
71             Returns the name of the subclass providing the actual connection information.
72             Used by any one that wants to ask the subclass for connection info. The
73             prime example is Gantry::Utils::ModelHelper.
74              
75             =back
76              
77             =head1 Required METHODS
78              
79             Your module needs to implement the methods below. Failure to implement them
80             will likely result in a fatal error at run time (or difficult to track
81             bugs).
82              
83             Get methods don't receive any parameters other than the invocant.
84              
85             =over 4
86              
87             =item get_dbh
88              
89             (required by Gantry::Utils::CDBI and Gantry::Utils::Model)
90              
91             Return a valid dbh if you have one or undef if not.
92              
93             =item set_dbh
94              
95             (required by Gantry::Utils::CDBI and Gantry::Utils::Model)
96              
97             Receives a dbh which it should store in its cache.
98              
99             =item get_conn_info
100              
101             (required by Gantry::Utils::CDBI and Gantry::Utils::Model)
102              
103             Returns a hash reference of connection info with these keys:
104              
105             =over 4
106              
107             =item dbconn
108              
109             a full dsn suitable for passing directly to DBI's connect method
110              
111             =item dbuser
112              
113             the name of the database user
114              
115             =item dbpass
116              
117             the password for dbuser
118              
119             =back
120              
121             Other keys in the hash are ignored.
122              
123             =back
124              
125             =head1 Optional METHODS (Required for Gantry authentication)
126              
127             In addition to connecting to an application database, Gantry can provide
128             authentication. In that case it uses a separate connection to the app's
129             auth database. This enables it to share authentication databases across
130             apps.
131              
132             Note that there is nothing that prevents you from storing the auth info
133             in the same database as the app data. We just use two connections to
134             add the flexibility to split these. In any case, if you are using
135             Gantry auth, you must use the methods below.
136              
137             (Note the symmetry between these methods and the ones above. These
138             simply have auth_ inserted into their names.)
139              
140             =over 4
141              
142             =item get_auth_dbh
143              
144             (required by Gantry::Utils::AuthCDBI and Gantry::Utils::AuthModel)
145              
146             Returns the database handle for the authentication database.
147              
148             =item set_auth_dbh
149              
150             (required by Gantry::Utils::AuthCDBI and Gantry::Utils::AuthModel)
151              
152             Receives a database handle for the authentication database which it
153             should cache for later retrieval by get_auth_dbh.
154              
155             =item get_auth_conn_info
156              
157             (required by Gantry::Utils::AuthCDBI and Gantry::Utils::AuthModel)
158              
159             Returns a hash reference of connection info with these keys:
160              
161             =over 4
162              
163             =item auth_dbconn
164              
165             a full dsn suitable for passing directly to DBI's connect method
166              
167             =item auth_dbuser
168              
169             the name of the database user
170              
171             =item auth_dbpass
172              
173             the password for dbuser
174              
175             =back
176              
177             Other keys in the hash are ignored.
178              
179             It is perfectly reasonable to use the same database -- or even database handle
180             -- for both the auth and regular connections. But, you need to provide
181             the methods above so that Gantry can find them.
182              
183             =back
184              
185             =head1 A METHOD for SUBCLASSES
186              
187             This module does supply a useful import method which you can inherit.
188             It allows users to supply the connection information hash as a parameter
189             in their use statement like this:
190              
191             use Gantry::Utils::DBConnHelper::YourSubclass {
192             dbconn = 'dbi:Pg:dbname=mydb;host=127.0.0.1',
193             dbuser = 'someuser',
194             dbpass = 'not_saying',
195             };
196              
197             The caller has the option of doing this in two steps (in case they need
198             to calculate the connection information at run time):
199              
200             use Gantry::Utils::DBConnHelper::YourSubclass;
201              
202             # ... figure out what information to provide
203              
204             Gantry::Utils::DBConnHelper::YourSubclass->set_conn_info(
205             {
206             dbconn = $dsn,
207             dbuser = $user,
208             dbpass = $pass,
209             }
210             );
211              
212             The import method does not help with authentication connection info.
213              
214             =head1 OTHER METHODS
215              
216             Gantry::Util::DBConnHelper::Script has two other methods for use by
217             scripts, constructors, init methods or the like.
218              
219             =over 4
220              
221             =item set_conn_info
222              
223             Not implemented by mod_perl helpers.
224              
225             Receives a hash of connection information suitable for use as the return
226             value of get_conn_info.
227              
228             =item set_auth_conn_info
229              
230             Not implemented by mod_perl helpers.
231              
232             Receives a hash reference of connection info which it should store for
233             later retrieval via get_conn_info.
234              
235             =back
236              
237             =head1 AUTHOR
238              
239             Phil Crow
240              
241             =head1 COPYRIGHT and LICENSE
242              
243             Copyright (c) 2005-6, Phil Crow.
244              
245             This library is free software; you can redistribute it and/or modify
246             it under the same terms as Perl itself, either Perl version 5.8.6 or,
247             at your option, any later version of Perl 5 you may have available.
248              
249             =cut