File Coverage

blib/lib/DBIx/MyPassword.pm
Criterion Covered Total %
statement 44 54 81.4
branch 6 14 42.8
condition 9 27 33.3
subroutine 14 16 87.5
pod 7 7 100.0
total 80 118 67.8


line stmt bran cond sub pod time code
1             package DBIx::MyPassword;
2              
3 1     1   1750 use warnings;
  1         2  
  1         28  
4 1     1   5 use strict;
  1         1  
  1         26  
5 1     1   13 use Carp;
  1         2  
  1         71  
6 1     1   2268 use DBI();
  1         18567  
  1         39  
7 1     1   1048 use IO::File;
  1         9890  
  1         147  
8 1     1   7 use Text::CSV;
  1         2  
  1         12  
9 1     1   18 use File::Spec;
  1         2  
  1         630  
10              
11             @DBIx::MyPassword::ISA = qw ( DBI::db );
12             $DBIx::MyPassword::VERSION = '1.02';
13              
14             #--> Name of the password file that we are looking for
15             my $PASSFILE = '.mypassword';
16              
17             #--> Name of environment variable that can point to override file
18             my $ENV_VAR = 'MYPASSWORD' ;
19              
20             #--> Order of data within password file
21             my @FIELDS = qw(alias user password datasource options) ;
22              
23             my %virtual_users;
24             my $EMPTY = q{};
25              
26             #-------------------------------------------------------------------------------
27             #-- import([password file])
28             #-------------------------------------------------------------------------------
29             #-- Try to find a file in which to grab password information. After a file is
30             #-- found, parse out the DBI connection information and store it in a hash.
31             #-------------------------------------------------------------------------------
32             #--> Pick a file to use to get passwords from. The order that we are going to
33             #--> look for a password file is:
34             #--> 1) import argument
35             #--> 2) an environment variable
36             #--> 3) the current directory
37             #--> 4) the users home directory
38             sub import {
39 1     1   8 my $file = $EMPTY;
40 1   33     28 for (
      33        
41             ( $_[-1] || $EMPTY ), #--> 'use' override
42             $ENV{$ENV_VAR}, #--> environmental override
43             $PASSFILE, #--> current directory
44             File::Spec->catpath(
45             File::Spec->rootdir(), ( $ENV{HOME} || $EMPTY ), $PASSFILE
46             ), #--> home directory
47             )
48             {
49 1 50 33     27 if ( $_ && -e $_ ) {
50 1         2 $file = $_;
51 1         3 last;
52             }
53             }
54 1 50       3 croak("Unable to find $PASSFILE file") unless ($file);
55              
56             #--> If we are here, we have found a password file. Assume that it is a CSV
57             #--> file and start to parse
58 1         6 my $csv = Text::CSV->new();
59 1         90 my $fh = IO::File->new();
60 1 50       29 croak("Unable to open file ($file): $!") unless ( $fh->open("<$file") );
61 1         59 while (<$fh>) {
62 2         4 chomp;
63 2 50       9 unless ( $csv->parse($_) ) { #--> Parse the line, just warn if problems
64 0         0 carp 'Unable to parse: ' . $csv->error_input();
65 0         0 next;
66             }
67 2 50       774 next unless ( my @fields = $csv->fields() );
68              
69             #--> Add all password elements indexed by the first CSV field
70 2 50       20 if ( $fields[0] ) {
71             $virtual_users{ $fields[0] }{ $FIELDS[$_] } = $fields[$_] || $EMPTY
72 2   33     38 for ( 0 .. $#FIELDS );
73             }
74             }
75 1         7 $fh->close();
76 1         115 return;
77             }
78              
79             #-------------------------------------------------------------------------------
80             #-- connect(virtual user)
81             #-------------------------------------------------------------------------------
82             #-- An override of the of the DBI::connect subroutine. Lookup the virtual user
83             #-- specified and return a standard DBI connection
84             #-------------------------------------------------------------------------------
85             sub connect { ##no critic
86 0     0 1 0 my ( $class, $user ) = @_;
87 0 0       0 return unless ( $virtual_users{$user} );
88              
89 0         0 my $self = DBI->connect(
90             $virtual_users{$user}{datasource}, $virtual_users{$user}{user}
91             , $virtual_users{$user}{password}
92              
93             #, eval("{$virtual_users{$user}{options}}")
94             #, eval {{$virtual_users{$user}{options}}}
95             , { $virtual_users{$user}{options} }
96             );
97              
98 0         0 bless $self, $class;
99 0         0 return $self;
100             }
101              
102             #-------------------------------------------------------------------------------
103             #-- getVirtualUsers()
104             #-------------------------------------------------------------------------------
105             #-- Return a list of virtual users. Presort them to be nice.
106             #-------------------------------------------------------------------------------
107             sub getVirtualUsers {
108 1     1 1 332 return sort keys %virtual_users;
109             }
110              
111             #-------------------------------------------------------------------------------
112             #-- checkVirtualUser(virtual user)
113             #-------------------------------------------------------------------------------
114             #-- Returns true if the specified virtual user exists, false if not.
115             #-------------------------------------------------------------------------------
116             sub checkVirtualUser {
117 2   33 2 1 211 return defined $virtual_users{ $_[-1] || $EMPTY };
118             }
119              
120             #-------------------------------------------------------------------------------
121             #-- getDataSource(virtual user)
122             #-------------------------------------------------------------------------------
123             #-- Return data source information for the specified virtual user.
124             #-------------------------------------------------------------------------------
125             sub getDataSource {
126 1   33 1 1 9 return $virtual_users{ $_[-1] || $EMPTY }{datasource};
127             }
128              
129             #-------------------------------------------------------------------------------
130             #-- getUser(virtual user)
131             #-------------------------------------------------------------------------------
132             #-- Return database user for the specified virtual user.
133             #-------------------------------------------------------------------------------
134             sub getUser {
135 1   33 1 1 8 return $virtual_users{ $_[-1] || $EMPTY }{user};
136             }
137              
138             #-------------------------------------------------------------------------------
139             #-- getPassword(virtual user)
140             #-------------------------------------------------------------------------------
141             #-- Return password for the specified virtual user.
142             #-------------------------------------------------------------------------------
143             sub getPassword {
144 1   33 1 1 9 return $virtual_users{ $_[-1] || $EMPTY }{password};
145             }
146              
147             #-------------------------------------------------------------------------------
148             #-- getOptions(virtual_user)
149             #-------------------------------------------------------------------------------
150             #-- Return options for the specified virtual user.
151             #-------------------------------------------------------------------------------
152             sub getOptions {
153 1   33 1 1 8 return $virtual_users{ $_[-1] || $EMPTY }{options};
154             }
155              
156             #-------------------------------------------------------------------------------
157             #-- DESTROY
158             #-------------------------------------------------------------------------------
159             #-- Clean up.
160             #-------------------------------------------------------------------------------
161             sub DESTROY { ##no critic
162 0     0     my ($self) = @_;
163 0           $self->SUPER::DESTROY;
164 0           return;
165             }
166              
167             1;
168              
169             __END__