NAME
DBIx::Array::Connect - Database connections from a configuration file
SYNOPSIS
use DBIx::Array::Connect;
my $dbx=DBIx::Array::Connect->new->connect("mydatabase"); #isa DBIx::Array
my $dac=DBIx::Array::Connect->new(file=>"./my.ini"); #isa DBIx::Array::Connect
my $dbx=$dac->connect("mydatabase"); #isa DBIx::Array
DESCRIPTION
Provides an easy way to construct database objects and connect to databases while providing an easy way to centralize management of database connection strings.
This package reads database connection information from an INI formatted configuration file.
USAGE
Create an INI configuration file with the following format. The default location for the INI file is /etc/database-connetions-config.ini on Linux-like systems and C:\Windows\database-connetions-config.ini on Windows-like systems.
[mydatabase]
connection=DBI:mysql:database=mydb;host=myhost.mydomain.tld
user=myuser
password=mypassword
options=AutoCommit=>1, RaiseError=>1
Connect to the database.
my $dbx=DBIx::Array::Connect->new->connect("mydatabase"); #isa DBIx::Array
Use the DBIx::Array object like you normally would.
CONSTRUCTOR
new
my $dac=DBIx::Array::Connect->new; #Defaults
my $dac=DBIx::Array::Connect->new(file=>"path/my.ini"); #Override the INI location
METHODS
connect
Returns a database object for the named database.
my $dbx=$dac->connect($name); #isa DBIx::Array
my %overrides=(
connection => $connection,
user => $user,
password => $password,
options => {},
execute => [],
);
my $dbx=$dac->connect($name, \%overrides); #isa DBIx::Array
sections
Returns all of the "active" section names in the INI file with the given type.
my $list=$dac->sections("db"); #[]
my @list=$dac->sections("db"); #()
my @list=$dac->sections; #All "active" sections in INI file
Note: active=1 is assumed active=0 is inactive
Example:
my @dbx=map {$dac->connect($_)} $dac->sections("db"); #connect to all active databases of type db
class
Returns the class in to which to bless objects. The "class" is assumed to be a base DBIx::Array object. This package MAY work with other objects that have a connect method that pass directly to DBI->connect. The object must have a similar execute method to support the package's execute on connect capability.
my $class=$dac->class; #$
$dac->class("DBIx::Array::Export"); #If you want the exports features
file
Sets or returns the profile INI file name
my $file=$dac->file;
my $file=$dac->file("./my.ini");
Set on construction
my $dac=DBIx::Array::Connect->new(file=>"./my.ini");
path
Sets and returns a list of search paths for the INI file.
my $path=$dac->path; # []
my $path=$dac->path(".", ".."); # []
Default: ["/etc"] on Linux-like systems Default: ['C:\Windows'] on Windows like systems
Overloading path name is a good way to migrate from one location to another over time.
package My::Connect;
use base qw{DBIx::Array::Connect};
use Path::Class qw{};
sub path {[".", "..", "/etc", "/home"]};
Put INI file in the same folder as tnsnames.ora file.
package My::Connect::Oracle;
use base qw{DBIx::Array::Connect};
use Path::Class qw{};
sub path {[Path::Class::dir($ENV{"ORACLE_HOME"}, "network", "admin")]};
basename
Returns the INI basename.
You may want to overload the basename property if you inherit this package.
package My::Connect;
use base qw{DBIx::Array::Connect};
sub basename {"whatever.ini"};
Default: database-connections-config.ini
cfg
Returns the Config::IniFiles object so that you can read additional information from the INI file.
my $cfg=$dac->cfg; #isa Config::IniFiles
Example
my $connection_string=$dac->cfg->val($database, "connection");
INI File Format
Section
The INI section name is value that needs to be passed in the connect method.
[section]
my $dbx=DBIx::Array::Connect->new->connect("section");
connection
The string passed to DBI to connect to the database.
Examples:
connection=DBI:CSV:f_dir=.
connection=DBI:mysql:database=mydb;host=myhost.mydomain.tld
connection=DBI:Sybase:server=mssqlserver.mydomain.tld;datasbase=mydb
connection=DBI:Oracle:DBTNSNAME
user
The string passed to DBI as the user. Default is "" for user-less drivers.
password
The string passed to DBI as the password. Default is "" for password-less drivers.
options
Parsed and passed as a hash reference to DBI->connect.
options=AutoCommit=>1, RaiseError=>1, ReadOnly=>1
execute
Connection settings that you want to execute every time you connect
execute=ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY HH24:MI:SS'
execute=INSERT INTO mylog (mycol) VALUES ('Me')
type
Allows grouping database connections in groups.
type=group
active
This option is used by the sections method to filter out databases that may be temporarily down.
active=1
active=0
Default: 1
LIMITATIONS
Once the file method has cached a filename, basename and path are ignored. Once the Config::IniFiles is constructed the file method is ignored. If you want to use two different INI files, you should construct two different objects.
The file, path and basename methods are common exports from other packages. Be wary!
BUGS
Send email to author and log on RT.
SUPPORT
DavisNetworks.com supports all Perl applications including this package.
AUTHOR
Michael R. Davis
CPAN ID: MRDVT
Satellite Tracking of People, LLC
mdavis@stopllc.com
http://www.stopllc.com/
COPYRIGHT
This program is free software licensed under the...
The General Public License (GPL)
Version 2, June 1991
The full text of the license can be found in the LICENSE file included with this module.