| 1 | package Config::DBI; | ||
| 2 | |||
| 3 | use Config::ApacheFormat; | ||
| 4 | use Data::Dumper; | ||
| 5 | use DBI; | ||
| 6 | use Term::ReadKey; | ||
| 7 | |||
| 8 | use diagnostics; | ||
| 9 | |||
| 10 | use 5.008; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | require Exporter; | ||
| 15 | |||
| 16 | our @ISA = qw(Exporter); | ||
| 17 | |||
| 18 | # Items to export into callers namespace by default. Note: do not export | ||
| 19 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 20 | # Do not simply export all your public functions/methods/constants. | ||
| 21 | |||
| 22 | # This allows declaration use Config::DBI ':all'; | ||
| 23 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||
| 24 | # will save memory. | ||
| 25 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 26 | |||
| 27 | ) ] ); | ||
| 28 | |||
| 29 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 30 | |||
| 31 | our @EXPORT = qw( | ||
| 32 | |||
| 33 | ); | ||
| 34 | |||
| 35 | our $VERSION = sprintf '%s', q$Revision: 1.8 $ =~ /Revision:\s+(\S+)\s+/ ; | ||
| 36 | |||
| 37 | |||
| 38 | my $stdin = '<STDIN>'; | ||
| 39 | |||
| 40 | our @attr = qw | ||
| 41 | ( | ||
| 42 | dbi_connect_method | ||
| 43 | Warn | ||
| 44 | |||
| 45 | _Active | ||
| 46 | _Executed | ||
| 47 | _Kids | ||
| 48 | _ActiveKids | ||
| 49 | _CachedKids | ||
| 50 | _CompatMode | ||
| 51 | |||
| 52 | InactiveDestroy | ||
| 53 | PrintWarn | ||
| 54 | PrintError | ||
| 55 | RaiseError | ||
| 56 | HandleError | ||
| 57 | HandleSetErr | ||
| 58 | |||
| 59 | _ErrCount | ||
| 60 | |||
| 61 | ShowErrorStatement | ||
| 62 | TraceLevel | ||
| 63 | FetchHashKeyName | ||
| 64 | ChopBlanks | ||
| 65 | LongReadLen | ||
| 66 | LongTruncOk | ||
| 67 | TaintIn | ||
| 68 | TaintOut | ||
| 69 | Taint | ||
| 70 | Profile | ||
| 71 | _should-add-support-for-private_your_module_name_* | ||
| 72 | |||
| 73 | |||
| 74 | AutoCommit | ||
| 75 | |||
| 76 | _Driver | ||
| 77 | _Name | ||
| 78 | _Statement | ||
| 79 | |||
| 80 | RowCacheSize | ||
| 81 | |||
| 82 | _Username | ||
| 83 | ); | ||
| 84 | |||
| 85 | our @valid_directives = ( qw(User Pass DSN), @attr ) ; | ||
| 86 | |||
| 87 | |||
| 88 | # Preloaded methods go here. | ||
| 89 | |||
| 90 | sub new { | ||
| 91 | |||
| 92 | my $envar = 'DBI_CONF'; | ||
| 93 | $ENV{$envar} or die "$envar not set"; | ||
| 94 | |||
| 95 | my $c = Config::ApacheFormat->new | ||
| 96 | ( | ||
| 97 | valid_directives => \@valid_directives | ||
| 98 | ); | ||
| 99 | $c->autoload_support(1); | ||
| 100 | $c->read($ENV{DBI_CONF}); | ||
| 101 | $c; | ||
| 102 | |||
| 103 | } | ||
| 104 | |||
| 105 | sub error_handler { | ||
| 106 | |||
| 107 | my ($errstring, $dbh, $retval) = @_; | ||
| 108 | |||
| 109 | warn "e: $errstring d: $dbh r: $retval"; | ||
| 110 | |||
| 111 | } | ||
| 112 | |||
| 113 | sub dummy_error_handler { | ||
| 114 | |||
| 115 | my ($errstring, $dbh, $retval) = @_; | ||
| 116 | |||
| 117 | warn "d_e_h -> e: $errstring d: $dbh r: $retval"; | ||
| 118 | |||
| 119 | } | ||
| 120 | |||
| 121 | sub hash { | ||
| 122 | my $self = shift; | ||
| 123 | my $label = shift; | ||
| 124 | my $c = __PACKAGE__->new; | ||
| 125 | |||
| 126 | my $block = $c->block(DBI => $label); | ||
| 127 | |||
| 128 | my %A = map { | ||
| 129 | defined($block->get($_)) ? ( $_ => $block->get($_) ) : () | ||
| 130 | } @attr; | ||
| 131 | |||
| 132 | if (my $handler = $block->HandleError) | ||
| 133 | { | ||
| 134 | my $hardref = eval "\\&$handler" ; | ||
| 135 | $A{HandleError} = $hardref; | ||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 139 | my @req = qw( DSN); | ||
| 140 | for my $req (@req) | ||
| 141 | { | ||
| 142 | unless ($block->$req()) { | ||
| 143 | die "$req must be defined" | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | my $Pass; | ||
| 149 | |||
| 150 | if ($block->Pass eq $stdin) | ||
| 151 | { | ||
| 152 | |||
| 153 | # Prevents input from being echoed to screen | ||
| 154 | ReadMode 2; | ||
| 155 | print "Enter Password for $label (will not be echoed to screen): "; | ||
| 156 | $Pass = <STDIN>; | ||
| 157 | if ($Pass) { | ||
| 158 | chomp($Pass) | ||
| 159 | }# else { | ||
| 160 | # undef $Pass | ||
| 161 | #} | ||
| 162 | |||
| 163 | print "\n"; | ||
| 164 | # Allows input to be directed to the screen again | ||
| 165 | ReadMode 0; | ||
| 166 | } | ||
| 167 | else | ||
| 168 | { | ||
| 169 | $Pass = $block->Pass | ||
| 170 | } | ||
| 171 | |||
| 172 | my %R = | ||
| 173 | ( | ||
| 174 | User => $block->User, | ||
| 175 | Pass => $Pass, | ||
| 176 | DSN => $block->DSN, | ||
| 177 | Attr => \%A | ||
| 178 | ); | ||
| 179 | |||
| 180 | } | ||
| 181 | |||
| 182 | use vars qw($AUTOLOAD); | ||
| 183 | |||
| 184 | sub AUTOLOAD { | ||
| 185 | |||
| 186 | my $self = shift; | ||
| 187 | |||
| 188 | my ($label) = ($AUTOLOAD =~ /([^:]+)$/) ; | ||
| 189 | |||
| 190 | my %c = Config::DBI->hash($label); | ||
| 191 | |||
| 192 | DBI->connect($c{DSN}, $c{User}, $c{Pass}, $c{Attr}) | ||
| 193 | or die $DBI::errstr; | ||
| 194 | |||
| 195 | } | ||
| 196 | |||
| 197 | 1; | ||
| 198 | __END__ |