DBIx-TNDBO
view release on metacpan or search on metacpan
examples/lib/DB.pm view on Meta::CPAN
package DB;
use strict;
use warnings;
use base qw( DBIx::TNDBO );
sub credentials {
my ($database) = @_;
return {
user => 'dylan',
pass => 'nalyd',
driver => 'mysql',
dbname => 'language',
host => 'localhost',
port => 3306,
};
}
lib/DBIx/TNDBO.pm view on Meta::CPAN
$Class_Base = substr $symbol, 0, -2;
last SYMBOL;
}
}
die 'unable to determine the class using ', __PACKAGE__, ' as its base'
if !$Class_Base;
{
no strict 'refs';
*{ __PACKAGE__ . '::credentials' }
= *{ $Class_Base . '::credentials' };
}
croak sprintf 'You must define %s::credentials( $dbname ) ', $Class_Base
if !defined &credentials;
my $creds_key;
NAME:
for my $dbname (@databases) {
if ( $dbname =~ m{\A : ( \w+ ) \z}xms ) {
$creds_key = $1;
last NAME;
}
}
if ($creds_key) {
@databases = grep { $_ ne ":$creds_key" } @databases;
my $cred_hr = credentials();
if ( ref $cred_hr->{$creds_key} eq 'ARRAY' ) {
push @databases, @{ $cred_hr->{$creds_key} };
}
elsif ( ref $cred_hr->{$creds_key} eq 'HASH' ) {
push @databases, keys %{ $cred_hr->{$creds_key} };
}
elsif ( $cred_hr->{$creds_key} ) {
lib/DBIx/TNDBO.pm view on Meta::CPAN
if ( $database && exists $Dbh_For{$database} ) {
$dbh = $Dbh_For{$database};
}
if ( !$dbh || !$dbh->ping() ) {
my ( $user, $pass, $driver, $host, $port );
{
my $cred_hr = credentials($database);
my @keys = qw( user pass driver host port );
my @creds = grep {$_} @{$cred_hr}{@keys};
croak sprintf 'credentials() should include (%s)', join ',', @keys
if @creds != @keys;
( $user, $pass, $driver, $host, $port ) = @creds;
}
my $dsn
= sprintf 'DBI:%s:database=%s;host=%s;port=%s',
$driver, $database, $host, $port;
$dbh = DBI->connect( $dsn, $user, $pass );
lib/DBIx/TNDBO.pod view on Meta::CPAN
=head1 SYNOPSIS
Your Database Module
package YDB;
use strict;
use warnings;
use base qw( DBIx::TNDBO );
sub credentials {
my ($dbname) = @_;
return {
user => 'dylan',
pass => 'foobar1',
driver => 'mysql',
dbname => 'language',
host => 'localhost',
port => 3306,
};
}
lib/DBIx/TNDBO.pod view on Meta::CPAN
$word_dbo->set( $anagram );
$word_dbo->commit();
print "$word_dbo\n";
}
=head1 DESCRIPTION
This module is designed to be used as the base class for your own DBI wrapper
module. Your module will define the credentials() method which is used to
access the database and examine the schema. The schema data is then used to
create a schema aware OO interface with abbreviated syntax for generating
lists, iterators, and record objects. There is also a uniquely abbreviated
syntax for getters and setters.
=head1 USE LINE
The use line for your module is where you state the names of the databases
you intend to interact with. Otherwise, give the name of the key in the
credentials hash which has the database name, with a leading colon.
use YDB qw( :dbname );
In this case the name (or list of names) is the value keyed by dbname in
the credentials hash.
use YDB qw( language );
In this case your module will be interacting with the database called
'language'.
In these examples the names 'YDB' and 'dbname' are just sample names. You will
choose a package name (and database name) which suits you.
lib/DBIx/TNDBO.pod view on Meta::CPAN
use YDB qw( database_a database_b );
my $word_dbo = YDB();
If there is both a database_a.word table and a database_b.word table then the
results in this case are not defined.
=item *
This module was developed and tested with MySQL. It will build a DSN based on
whatever values are returned from your credentials() function. However, there
is no accounting for whatever database idiosyncracies there may be.
=back
=head1 ABOUT
This module is not the TierraNet DBO module. This is just a tribute.
The key features of this module are accomplished using:
t/DBIx-TNDBO/import.t view on Meta::CPAN
$Expect_Dsn = 'DBI:mysql:database=mock_db;host=dickclark;port=1234, joe, eoj';
}
INIT {
$::{'DBIx::TNDBO::Mockup::'} = { 'ISA' => ['DBIx::TNDBO'] };
no warnings qw( redefine once );
*DBIx::TNDBO::Mockup::credentials = sub {
return {
user => 'joe',
pass => 'eoj',
driver => 'mysql',
host => 'dickclark',
port => '1234',
};
};
*Storable::store = sub {
t/DBIx-TNDBO/import.t view on Meta::CPAN
}
}
return _mock_dbh->new( @args );
};
}
use_ok( 'DBIx::TNDBO', 'mock_db' )
|| exit;
# This covers end-to-end for
# import(), credentials(), _read_schema(), _get_dbh()
{
is( $Dsn, $Expect_Dsn, 'correct DSN and credentials' );
is_deeply( $Schema_Hr, \%Expect_Schema, 'stored correct schema' );
is( $Filename, $Expect_Filename, 'stored to correct filename' );
}
__END__
( run in 0.266 second using v1.01-cache-2.11-cpan-4d50c553e7e )