blib/lib/DBIx/Array/Connect.pm

filter: Statementstmt 91.3% 95 / 104 Branchbran 66.6% 32 / 48 Conditioncond 35.7% 5 / 14 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 100% 16 / 16 Podpod 100% 8 / 8 totaltotal 78.8% 157 / 199 CC 33 SCAR 20.5
CC
33
Cov
80%
connect
18.6
file
8.8
section_hash
5.0
CRAP 7.8
↑ Summary
1 package DBIx::Array::Connect;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use base qw{Package::New};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use File::Basename qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Config::IniFiles qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Path::Class qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 our $VERSION='0.08';
10 4 our $PACKAGE=__PACKAGE__;
11  
12 =head1 NAME
13  
14 DBIx::Array::Connect - Database Connections from an INI Configuration File
15  
16 =head1 SYNOPSIS
17  
18 use DBIx::Array::Connect;
19 my $dbx=DBIx::Array::Connect->new->connect("mydatabase"); #isa DBIx::Array
20  
21 my $dac=DBIx::Array::Connect->new(file=>"./my.ini"); #isa DBIx::Array::Connect
22 my $dbx=$dac->connect("mydatabase"); #isa DBIx::Array
23  
24 =head1 DESCRIPTION
25  
26 Provides an easy way to construct database objects and connect to databases while providing an easy way to centralize management of database connection strings.
27  
28 This package reads database connection information from an INI formatted configuration file and returns a connected database object.
29  
30 This module is used to connect to both Oracle 10g and 11g using L<DBD::Oracle> on both Linux and Win32, MySQL 4 and 5 using L<DBD::mysql> on Linux, and Microsoft SQL Server using L<DBD::Sybase> on Linux and using L<DBD::ODBC> on Win32 systems in a 24x7 production environment.
31  
32 =head1 USAGE
33  
34 Create an INI configuration file with the following format. The default location for the INI file is /etc/database-connections-config.ini on Linux-like systems and C:\Windows\database-connections-config.ini on Windows-like systems.
35  
36 [mydatabase]
37 connection=DBI:mysql:database=mydb;host=myhost.mydomain.tld
38 user=myuser
39 password=mypassword
40 options=AutoCommit=>1, RaiseError=>1
41  
42 Connect to the database.
43  
44 my $dbx=DBIx::Array::Connect->new->connect("mydatabase"); #isa DBIx::Array
45 my $dbh=$dbx->dbh; #if you don't want to use DBIx::Array...
46  
47 Use the L<DBIx::Array> object like you normally would.
48  
49 =head1 CONSTRUCTOR
50  
51 =head2 new
52  
53 my $dac=DBIx::Array::Connect->new; #Defaults
54 my $dac=DBIx::Array::Connect->new(file=>"path/my.ini"); #Override the INI location
55  
56 =head1 METHODS
57  
58 =head2 connect
59  
60 Returns a database object for the database nickname which is an INI section name.
61  
62 my $dbx=$dac->connect($nickname); #isa DBIx::Array
63  
64 my %overrides=(
65 connection => $connection,
66 user => $user,
67 password => $password,
68 options => {},
69 execute => [],
70 );
71 my $dbx=$dac->connect($nickname, \%overrides); #isa DBIx::Array
72  
73 =cut
74  
75 2 ▶sub connect {
Subroutine connect: called CC=12 SCAR=29.2
Pod documented
76 2 my $self=shift;
77 2 ▶ my $nickname=shift or die("Error: connect method requires a database nickname parameter.");
Branch: 1 branch
BranchTrueFalseTotal
my $nickname = shift() or die "Error: connect method requires a database nickname parameter." 2 0 2
78 2 my $override=shift;
79 2 ▶ $override={} unless ref($override) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $override eq "HASH" 2 0 2
80 2 ▶ my $connection= $override->{"connection"} || $self->cfg->val($nickname, "connection")
Branch: 1 branch
BranchTrueFalseTotal
my $connection = $override->{"connection"} || $self->cfg->val($nickname, "connection") or die qq[Error: connection not defined for nickname "$nickname"] 2 0 2
Condition33%1/3
$override->{'connection'} || $self->cfg->val($nickname, 'connection')
l!l&&r!l&&!r
020
MC/DC0%0/2
$override->{'connection'} || $self->cfg->val($nickname, 'connection')
$override->{'connection'} $self->cfg->val($nickname, 'connection')
Truth table33%1/3
A || B
A= $override->{'connection'}B= $self->cfg->val($nickname, 'connection')
ABresult
1X1
011
000
81 or die(qq{Error: connection not defined for nickname "$nickname"});
82 2 ▶ my $user = $override->{"user"} || $self->cfg->val($nickname, "user", "");
Condition33%1/3
$override->{'user'} || $self->cfg->val($nickname, 'user', '')
l!l&&r!l&&!r
002
MC/DC0%0/2
$override->{'user'} || $self->cfg->val($nickname, 'user', '')
$override->{'user'} $self->cfg->val($nickname, 'user', '')
Truth table33%1/3
A || B
A= $override->{'user'}B= $self->cfg->val($nickname, 'user', '')
ABresult
1X1
011
000
83 2 ▶ my $password = $override->{"password"} || $self->cfg->val($nickname, "password", "");
Condition33%1/3
$override->{'password'} || $self->cfg->val($nickname, 'password', '')
l!l&&r!l&&!r
002
MC/DC0%0/2
$override->{'password'} || $self->cfg->val($nickname, 'password', '')
$override->{'password'} $self->cfg->val($nickname, 'password', '')
Truth table33%1/3
A || B
A= $override->{'password'}B= $self->cfg->val($nickname, 'password', '')
ABresult
1X1
011
000
84 2 my %options=();
85 2 ▶ if (ref($override->{"options"}) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $override->{'options'} eq 'HASH') { } 0 2 2
86 0 %options=%{$override->{"options"}};
87 } else {
88 2 my $options=$self->cfg->val($nickname, "options", "");
89 2 %options=map {s/^\s*//;s/\s*$//;$_} split(/[,=>]+/, $options);
90 }
91 2 my $class=$self->class;
92 2 ▶ eval("use $class");
Subroutine BEGIN: called CC=1 SCAR=0.0
93 2 my $dbx=$class->new(name=>$nickname);
94 2 $dbx->connect($connection, $user, $password, \%options);
95 2 ▶ if ($dbx->can("execute")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbx->can("execute")) 2 0 2
96 2 my @execute=();
97 2 ▶ if (ref($override->{"execute"}) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $override->{'execute'} eq 'ARRAY') { } 0 2 2
98 0 @execute=@{$override->{"execute"}};
99 } else {
100 0 ▶ @execute=grep {defined && length} $self->cfg->val($nickname, "execute");
Condition0%0/3
defined $_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
101 }
102 2 $dbx->execute($_) foreach @execute;
103 }
104 2 return $dbx;
105 }
106  
107 =head2 sections
108  
109 Returns all of the "active" section names in the INI file with the given type.
110  
111 my $list=$dac->sections("db"); #[]
112 my @list=$dac->sections("db"); #()
113 my @list=$dac->sections; #All "active" sections in INI file
114  
115 Note: active=1 is assumed active=0 is inactive
116  
117 Example:
118  
119 my @dbx=map {$dac->connect($_)} $dac->sections("db"); #Connect to all active databases of type db
120  
121 =cut
122  
123 5 ▶sub sections {
Subroutine sections: called CC=4 SCAR=13.9
Pod documented
124 5 my $self=shift;
125 5 ▶ my $type=shift || "";
Condition100%2/2
shift() || ''
l!l
41
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
126 5 my @list=grep {$self->cfg->val($_, "active", "1")} $self->cfg->Sections;
127 5 ▶ @list=grep {$self->cfg->val($_, "type", "") eq $type} @list if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 4 1 5
128 5 ▶ return wantarray ? @list : \@list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 5 5
129 }
130  
131 =head2 section_hash
132  
133 Returns the contents of the INI section as a hash or hash reference.
134  
135 my $hash_ref = $dac->section_hash("db1"); #isa HASH
136 my %hash = $dac->section_hash("db1"); #isa LIST
137  
138 =cut
139  
140 2 ▶sub section_hash {
Subroutine section_hash: called CC=5 SCAR=16.1
Pod documented
141 2 my $self = shift;
142 2 ▶ my $section = shift or die("Error: $PACKAGE->section_hash requires section name parameter.");
Branch: 1 branch
BranchTrueFalseTotal
my $section = shift() or die "Error: $DBIx::Array::Connect::PACKAGE->section_hash requires section name parameter." 2 0 2
143 2 my @return = ();
144 2 foreach my $parameter ($self->cfg->Parameters($section)) {
145 10 my @value = $self->cfg->val($section, $parameter);
146 10 ▶ push @return, @value == 1 ? ($parameter, $value[0]) : ($parameter, \@value);
Branch: 1 branch
BranchTrueFalseTotal
@value == 1 ? : 8 2 10
147 }
148 2 ▶ return wantarray ? @return : {@return};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
149 }
150  
151 =head2 class
152  
153 Returns the class in to which to bless objects. The "class" is assumed to be a base DBIx::Array object. This package MAY work with other objects that have a connect method that pass directly to DBI->connect. The object must have a similar execute method to support the package's execute on connect capability.
154  
155 my $class=$dac->class; #$
156 $dac->class("DBIx::Array::Export"); #If you want the exports features of DBIx::Array
157  
158 Set on construction
159  
160 my $dac=DBIx::Array::Connect->new(class=>"DBIx::Array::Export");
161  
162 =cut
163  
164 1 ▶sub class {
Subroutine class: called CC=3 SCAR=11.4
Pod documented
165 1 my $self=shift;
166 1 ▶ $self->{"class"}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
167 1 ▶ $self->{"class"}="DBIx::Array" unless defined $self->{"class"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"class"} 1 0 1
168 1 return $self->{"class"};
169 }
170  
171 =head2 file
172  
173 Sets or returns the profile INI filename
174  
175 my $file=$dac->file;
176 my $file=$dac->file("./my.ini");
177  
178 Set on construction
179  
180 my $dac=DBIx::Array::Connect->new(file=>"./my.ini");
181  
182 =cut
183  
184 75 ▶sub file {
Subroutine file: called CC=7 SCAR=21.8
Pod documented
185 75 my $self=shift;
186 75 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 75 75
187 0 $self->{'file'}=shift;
188 0 ▶ die(sprintf(qq{Error: Cannot read file "%s".}, $self->{'file'})) unless -r $self->{'file'};
Branch: 1 branch
BranchTrueFalseTotal
unless -r $self->{"file"} 0 0 0
189 }
190 75 ▶ unless (defined $self->{'file'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"file"}) 2 73 75
191 2 ▶ die(sprintf(qq{Error: path method returned a "%s"; expecting an array reference.}, ref($self->path)))
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->path eq "ARRAY" 0 2 2
192 unless ref($self->path) eq "ARRAY";
193 2 foreach my $path (@{$self->path}) {
194 2 $self->{"file"}=Path::Class::file($path, $self->basename);
195 2 ▶ last if -r $self->{"file"};
Branch: 1 branch
BranchTrueFalseTotal
if -r $self->{"file"} 2 0 2
196 }
197 }
198 #We may not have a vaild file here? We'll let Config::IniFiles catch the error.
199 75 return $self->{'file'};
200 }
201  
202 =head2 path
203  
204 Sets and returns a list of search paths for the INI file.
205  
206 my $path=$dac->path; # []
207 my $path=$dac->path(".", ".."); # []
208  
209 Default: [".", dirname($0), "/etc"] on Linux-like systems
210 Default: [".", dirname($0), 'C:\Windows'] on Windows-like systems
211  
212 Overloading path is a good way to migrate from one location to another over time.
213  
214 package My::Connect;
215 use base qw{DBIx::Array::Connect};
216 sub path {[".", "..", "/etc", "/home"]};
217  
218 Put INI file in the same folder as tnsnames.ora file.
219  
220 package My::Connect::Oracle;
221 use base qw{DBIx::Array::Connect};
222 use Path::Class qw{};
223 sub path {[Path::Class::dir($ENV{"ORACLE_HOME"}, qw{network admin})]}; #not taint safe
224  
225 =cut
226  
227 12 ▶sub path {
Subroutine path: called CC=4 SCAR=14.0
Pod documented
228 12 my $self=shift;
229 12 ▶ $self->{"path"}=[@_] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 9 12
230 12 ▶ unless (ref($self->{"path"}) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"path"} eq "ARRAY") 1 11 12
231 1 my @path=(".", File::Basename::dirname($0));
232 1 ▶ if ($^O eq "MSWin32") {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 1 1
233 0 eval("use Win32");
234 0 push @path, eval("Win32::GetFolderPath(Win32::CSIDL_WINDOWS)");
235 } else {
236 1 ▶ eval("use Sys::Path");
Subroutine BEGIN: called CC=1 SCAR=0.0
237 1 push @path, eval("Sys::Path->sysconfdir");
238 }
239 1 $self->{"path"}=\@path;
240 }
241 12 return $self->{"path"};
242 }
243  
244 =head2 basename
245  
246 Returns the INI basename.
247  
248 You may want to overload the basename property if you inherit this package.
249  
250 package My::Connect;
251 use base qw{DBIx::Array::Connect};
252 sub basename {"whatever.ini"};
253  
254 Default: database-connections-config.ini
255  
256 =cut
257  
258 5 ▶sub basename {
Subroutine basename: called CC=3 SCAR=11.0
Pod documented
259 5 my $self=shift;
260 5 ▶ $self->{"basename"}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 2 5
261 $self->{"basename"}="database-connections-config.ini"
262 5 ▶ unless $self->{"basename"};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"basename"} 1 4 5
263 5 return $self->{"basename"};
264 }
265  
266 =head2 cfg
267  
268 Returns the L<Config::IniFiles> object so that you can read additional information from the INI file.
269  
270 my $cfg=$dac->cfg; #isa Config::IniFiles
271  
272 Example
273  
274 my $connection_string=$dac->cfg->val($database, "connection");
275  
276 =cut
277  
278 73 ▶sub cfg {
Subroutine cfg: called CC=2 SCAR=6.9
Pod documented
279 73 my $self=shift;
280 73 my $file=$self->file; #support for objects that can stringify paths.
281 $self->{'cfg'}=Config::IniFiles->new(-file=>"$file")
282 73 ▶ unless ref($self->{'cfg'}) eq "Config::IniFiles";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"cfg"} eq "Config::IniFiles" 4 69 73
283 73 return $self->{'cfg'};
284 }
285  
286 =head1 INI File Format
287  
288 =head2 Section
289  
290 The INI section is the value that needs to be passed in the connect method which is the database nickname.
291  
292 [section]
293  
294 my $dbx=DBIx::Array::Connect->new->connect("section");
295  
296 =head2 connection
297  
298 The string passed to DBI to connect to the database.
299  
300 Examples:
301  
302 connection=DBI:CSV:f_dir=.
303 connection=DBI:mysql:database=mydb;host=myhost.mydomain.tld
304 connection=DBI:Sybase:server=mssqlserver.mydomain.tld;datasbase=mydb
305 connection=DBI:Oracle:MYTNSNAME
306  
307 =head2 user
308  
309 The string passed to DBI as the user. Default is "" for user-less drivers.
310  
311 =head2 password
312  
313 The string passed to DBI as the password. Default is "" for password-less drivers.
314  
315 =head2 options
316  
317 Split and passed as a hash reference to DBI->connect.
318  
319 options=AutoCommit=>1, RaiseError=>1, ReadOnly=>1
320  
321 =head2 execute
322  
323 Connection settings that you want to execute every time you connect
324  
325 execute=ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY HH24:MI:SS'
326 execute=INSERT INTO mylog (mycol) VALUES ('Me')
327  
328 =head2 type
329  
330 Allows grouping database connections in groups.
331  
332 type=group
333  
334 =head2 active
335  
336 This option is used by the sections method to filter out databases that may be temporarily down.
337  
338 active=1
339 active=0
340  
341 Default: 1
342  
343 =head1 LIMITATIONS
344  
345 Once the file method has cached a filename, basename and path are ignored. Once the Config::IniFiles is constructed the file method is ignored. If you want to use two different INI files, you should construct two different objects.
346  
347 The file, path and basename methods are common exports from other packages. Be wary!
348  
349 =head1 AUTHOR
350  
351 Michael R. Davis
352  
353 =head1 COPYRIGHT
354  
355 This program is free software licensed under the...
356  
357 The General Public License (GPL)
358 Version 2, June 1991
359  
360 The full text of the license can be found in the LICENSE file included with this module.
361  
362 =head1 SEE ALSO
363  
364 =head2 The Building Blocks
365  
366 L<DBIx::Array>, L<Config::IniFiles>, L<Path::Class>
367  
368 =head2 The Competition
369  
370 L<DBIx::MyPassword> uses a CSV file to store data. The constructor is wrapper around DBI->connect.
371  
372 my $dbh = DBIx::MyPassword->connect("user");
373  
374 L<DBIx::PasswordIniFile> uses an INI file to store data. It uses encrypted passwords and the constructor returns array reference to feed into DBI->connect.
375  
376 my $dbh = DBI->connect(@{DBIx::PasswordIniFile->new(%arg)->getDBIConnectParams})
377  
378 L<DBIx::Password> uses and internal hash reference to store data. The constructor is wrapper around DBI->connect.
379  
380 my $dbh = DBIx::Password->connect("user");
381  
382 =head2 The Comparison
383  
384 L<DBIx::Array::Connect> uses an INI file to store data. The constructor returns a L<DBIx::Array> object which is a wrapper around DBI.
385  
386 my $dbx = DBIx::Array::Connect->new->connect("nickname");
387 my $dbh = $dbx->dbh; #if you don't want to use DBIx::Array...
388  
389 =cut
390  
391 4 1;
↑ Summary