| Category: | Database connectivity |
| Author/Contact Info | Rob Kinyon rkinyon@columbus.rr.com dragonchild on perlmonks |
| Description: | This is meant to be a module that is site-specific. It allows a client to specify a database by name and be able to connect to it, regardless of what the connection string is. This is most useful when a number of scripts would have to connect to a group of databases, possibly using different protocols. |
package DbConnect; use strict; use warnings; no warnings 'once'; ###################################################################### +########## BEGIN { use Exporter (); use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = 0.02; @ISA = qw (Exporter); #Give a hoot don't pollute, do not export more than needed by +default @EXPORT = qw (); @EXPORT_OK = qw ( connect ); %EXPORT_TAGS = (); } ###################################################################### +########## use DBI; ###################################################################### +########## my %Databases = ( 'AN_ORA_DB' => 'dbi:Oracle:host=127.0.0.1;sid=AN_ORA_DB', 'A_SYB_DB' => 'dbi:Sybase:database=a_syb_db', ); my %EnvToSet = ( A_SYB_DB => { TDSHOST => '127.0.0.1', TDSPORT => '1433', }, ); ###################################################################### +########## sub connect { die "Odd number of parameters passed to connect()\n" if @_ % 2 +; my %opt = @_; # Validate that a database name was passed in and it's one we +recognize. die "Must pass a database name to connect()\n" unless exists $opt{database} && defined $opt{database} +; $opt{database} = uc $opt{database}; die "'$opt{database}' is not recognized in connect()\n" unless exists $Databases{$opt{database}}; # Validate that a database username was passed in. die "Must pass a database username to connect()\n" unless exists $opt{username} && defined $opt{username} +; # Set the password to the usernamename if there is no password +. $opt{password} = $opt{username} unless exists $opt{password}; # This environment setting is required because DBD::Sybase can +not # handle the IP/port within the connect string. if (exists $EnvToSet{$opt{database}}) { while (my ($k, $v) = each %{$EnvToSet{$opt{database}}} +) { $ENV{$k} = $v; } } my $dbh = DBI->connect( $Databases{$opt{database}}, $opt{username}, $opt{password}, ) || die "Cannot connect to DB '$opt{database}'!\n"; # Clean up the environment. There's no reason to leave stuff j +ust # laying around when it's not needed anymore. if (exists $EnvToSet{$opt{database}}) { foreach my $k (keys %{$EnvToSet{$opt{database}}}) { delete $ENV{$k}; } } return $dbh; } 1; __END__ =head1 NAME DbConnect - DbConnect =head1 SYNOPSIS use DbConnect qw( connect ); my $dbh = connect( database => 'SOME_NAME', username => 'my_user', password => 'my_pass', ); =head1 DESCRIPTION This module contains the ability to connect to various databases seam +lessly. =head1 EXPORTED ITEMS =head2 connect() This wraps DBI->connect(), mapping database name to whatever is neede +d to connect. It expects two requried parameters and one optional parameter =over 4 =item database (required) This is the server you want to make a connection to. The list of acce +ptable values is in the exported %Databases hash. =item username (required) This is the username you want to connect as. This is case-sensitive. =item password (optional) This is the password for the username supplied. If this is not passed + in, then it will be set to the username. =back 4 =head1 BUGS None (that I know of). =head1 SUPPORT Rob Kinyon (dragonchild on perlmonks) =head1 AUTHOR Rob Kinyon rkinyon@columbus.rr.com dragonchild on perlmonks =head1 COPYRIGHT This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. =head1 SEE ALSO perl(1), DBI(1), DBD::Oracle(1), DBD::Sybase(1) =cut |
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DB-independent wrapper on DBI
by mpeppler (Vicar) on Oct 15, 2003 at 21:59 UTC | |
|
Re: DB-independent wrapper on DBI
by princepawn (Parson) on Oct 15, 2003 at 22:04 UTC | |
by dragonchild (Archbishop) on Oct 16, 2003 at 13:26 UTC |