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
    I'm sure you've thought of this already: read the %Database hash from a file to make it more flexible.

    And one clarification for others reading this - the TDSHOST/TDSPORT environment variables will only work with DBD::Sybase if it is built with the FreeTDS libraries - Sybase's libraries use a logical server name that is looked up in the "interfaces" file.

    Michael

Re: DB-independent wrapper on DBI
by princepawn (Parson) on Oct 15, 2003 at 22:04 UTC
    Hmm, looks like a recode of DBIx::Connect

    Also see DBI database connection support and more

    Finally, the term "wrapper" usually means something that abstracts the prepare-execute-fetch cycle, not something that abstracts the connect aspect... but I guess we could use the term more generically.

    Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality.

      Yes, it mostly is. One difference is that I handle DBD::Sybase/FreeTDS connections without needing a .freetds.conf file entry. The idea here is that there are a few select databases a number of developers might need to get to, from any system. (There are over 50k different machines where I work.) By handing them one file, they can reach any of these databases. (Of course, assuming the right pre-requisites are installed, which they mostly are.) If I were to recode this, I would use DBIx::Connect, now that I know of it.

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.