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

In reply to DB-independent wrapper on DBI by dragonchild

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.