Activator
view release on metacpan or search on metacpan
lib/Activator/DB.pm view on Meta::CPAN
=head1 CONFIGURATION
This module uses L<Activator::Registry> to automatically choose default
databases, and L<Activator::Log> to log warnings and errors.
=head2 Registry Setup (from Activator::Registry)
This module expects an environment variable ACT_REG_YAML_FILE to be
set. If you are utilizing this module from apache, this directive must
be in your httpd configuration:
SetEnv ACT_REG_YAML_FILE '/path/to/config.yml'
If you are using this module from a script, you need to insure that
the environment is properly set using a BEGIN block:
BEGIN{
$ENV{ACT_REG_YAML_FILE} ||= '/path/to/config.yml'
}
=head2 Registry Configuration
Add an C<Activator::DB> section to your project YAML configuration file:
'Activator::Registry':
log4perl<.conf>: # Log4perl config file or definition
# See Logging Configuration below
'Activator::DB':
default: # default configuration for all connections
connection: <conn_alias>
## Optional default attributes and config for all connections
config:
debug: 0/1 # default: 0, affects all queries, all aliases
reconn_att: <int> # attempt reconnects this many times. default: 3
reconn_sleep: <int> # initial sleep seconds between reconnect attempts.
# doubles every attempt. default: 1
attr: # connection attributes. Only AutoCommit at this time
AutoCommit: 0/1 # default: 1
## You must define at least one connection alias
connections:
<conn_alias>:
user: <user>
pass: <password>
dsn: '<DSN>' # MySql Example: DBI:mysql:<DBNAME>:<DBHOST>
# PostgreSQL Example: DBI:Pg:dbname=<DBNAME>
# see: perldoc DBI, perldoc DBD::Pg, perldoc DBD::mysql
# for descriptions of valid DSNs
## These attributes and config are all optional, and use the default from above
attr:
AutoCommit: 0/1
config:
debug: 0/1 # only affects this connection
=head1 USAGE
This module can be used either pseudo-OO or static on multiple
databases. I say pseudo-OO, because you don't call new: this module
auto-vivicates a singleton object whenever you connect for the first
time.
=over
=item ## pseudo-OO example:
my $db = Activator::DB->connect( 'db_alias' );
$db->query_method( $sql, $bind, @args );
$db->connect( 'alt_db_alias' );
$db->query_method( $sql, $bind, @args );
$db->connect( 'db_alias' );
$db->query_method( $sql, $bind, @args );
=item ## Static formatted calls require that you dictate the connection for
every request. So, the above can also be done as:
Activator::DB->query_method( $sql, $bind, connect => 'db_alias', @args );
Activator::DB->query_method( $sql, $bind, connect => 'alt_db_alias', @args );
Activator::DB->query_method( $sql, $bind, connect => 'db_alias', @args );
=item ## However, the common use case for this module is:
my $db = Activator::DB->connect( 'db_alias' );
$db->query_method( $sql, $bind, @args );
### do some perl
$db->query_method( $sql, $bind, @args );
### do some perl
$db->query_method( $sql, $bind, @args );
### do some perl
... etc.
=back
=head2 connect() Usage
my $db = Activator::DB->connect('my_db'); # connect to my_db
my $db->connect('default'); # connect to default db
my $db->connect('def'); # shortcut to default db
my $db->connect(); # shortercut to default db
=head2 connect() Caveat
Note that C<connect()> always returns the singleton object, which in some
usage patterns could cause some confusion:
my $db1->connect('db1'); # connect to db1
$db1->query( $sql, $bind, @args ); # acts on db1
my $db2->connect('db2'); # connect to db2
$db2->query( $sql, $bind, @args ); # acts on db2
$db1->query( $sql, $bind, @args ); # still acts on db2!
For this reason, it is highly recommended that you always use the same
variable name (probably C<$db>) for the Activator::DB object.
=head2 Query Methods Usage
Every query function takes named arguments in the format of:
Activator::DB->$query_method( $sql, $bind, opt_arg => <opt_value> );
( run in 0.465 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )