Acrux-DBI
view release on metacpan or search on metacpan
lib/Acrux/DBI.pm view on Meta::CPAN
my $dbi = Acrux::DBI->new( $db_url );
my $dbi = Acrux::DBI->new( $db_url, { ... options ... });
my $dbi = Acrux::DBI->new( $db_url, ... options ...);
Build new Acrux::DBI object
B<Options:>
=over 8
=item autoclean
This options turns on auto disconnecting on DESTROY phase
=back
See also list of default options in L</options>
=head1 METHODS
This class implements the following methods
lib/Acrux/DBI.pm view on Meta::CPAN
sub new {
my $class = shift;
my $url = shift || DEFAULT_DBI_URL;
croak 'Invalid DBI URL' unless $url;
my $opts = scalar(@_) ? scalar(@_) > 1 ? {@_} : {%{$_[0]}} : {};
my $uri = Mojo::URL->new($url);
# Default attributes
my %_opts = (%{(DEFAULT_DBI_OPTS)}, %$opts);
my $autoclean = delete $_opts{autoclean};
my $self = bless {
url => $url,
uri => $uri,
dsn => '',
cachekey=> '',
driver => '',
dbh => undef,
error => "", # Ok
autoclean => $autoclean ? 1 : 0,
opts => {%_opts},
cache => Mojo::Cache->new,
}, $class;
return $self;
}
# Attributes
sub url {
my $self = shift;
if (scalar(@_) >= 1) {
lib/Acrux/DBI.pm view on Meta::CPAN
sub cleanup {
my $self = shift;
undef $self->{dbh};
return $self;
}
sub DESTROY {
my $self = shift;
printf STDERR "DESTROY on phase %s\n", ${^GLOBAL_PHASE} if DEBUG;
return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
return unless $self->{autoclean};
$self->disconnect;
printf STDERR "Auto cleanup on DESTROY completed\n" if DEBUG;
}
1;
__END__
t/03-connect.t view on Meta::CPAN
use Test::More;
use Acrux::DBI;
plan skip_all => "Currently a developer-only test" unless -d ".git";
my $url = $ENV{DB_CONNECT_URL} or plan skip_all => "DB_CONNECT_URL required";
ok($url, 'DB_CONNECT_URL is correct') and note $url;
# Connect
my $dbi;
subtest 'Connecting' => sub {
$dbi = Acrux::DBI->new($url, autoclean => 1);
is($dbi->{autoclean}, 1, 'autoclean = 1');
$dbi->connect;
ok(!$dbi->error, 'Connect to ' . $dbi->dsn) or diag $dbi->error;
ok $dbi->ping, 'Connected';
#is($dbi->driver, 'postgres', 'Driver (scheme) is postgres');
#is($dbi->host, 'localhost', 'Host is localhost');
#is($dbi->port, '', 'Port is null');
#is($dbi->userinfo, 'foo:pass', 'Userinfo is foo:pass');
#is($dbi->password, 'pass', 'Password is pass');
#is($dbi->database, 'mydb', 'Password is mydb');
t/04-transaction.t view on Meta::CPAN
use Test::More;
use Acrux::DBI;
plan skip_all => "Currently a developer-only test" unless -d ".git";
my $url = $ENV{DB_CONNECT_URL} or plan skip_all => "DB_CONNECT_URL required";
ok($url, 'DB_CONNECT_URL is correct') and note $url;
# Connect
my $dbi;
subtest 'Connecting' => sub {
$dbi = Acrux::DBI->new($url, autoclean => 1)->connect;
ok(!$dbi->error, 'Connect to ' . $dbi->dsn) or diag $dbi->error;
ok $dbi->ping, 'Connected' or return;
};
subtest 'Create table' => sub {
my $res = $dbi->query('CREATE TABLE IF NOT EXISTS `names` (`id` INTEGER AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(255))');
ok($res, 'Create table') or do { diag $dbi->error; return }
};
subtest 'Transactions1' => sub {
t/05-dump.t view on Meta::CPAN
use Acrux::Util qw/ touch /;
plan skip_all => "Currently a developer-only test" unless -d ".git";
my $url = $ENV{DB_CONNECT_URL} or plan skip_all => "DB_CONNECT_URL required";
ok($url, 'DB_CONNECT_URL is correct') and note $url;
# Connect
my $dbi;
my $is_new = 0;
subtest 'Connecting' => sub {
$dbi = Acrux::DBI->new($url, autoclean => 1)->connect;
if (defined($dbi->dbh) && $dbi->driver eq 'sqlite') {
my $file = $dbi->dbh->sqlite_db_filename();
unless ($file && (-e $file) && !(-z $file)) {
touch($file);
$is_new = 1;
}
}
ok(!$dbi->error, 'Connect to ' . $dbi->dsn) or diag $dbi->error;
ok $dbi->ping, 'Connected' or return;
};
t/05-dump.t view on Meta::CPAN
$dump->poke('test');
ok(!$dbi->error, 'Poked test dump') or diag $dbi->error;
} if $is_new;
subtest 'Transaction' => sub {
$dump->poke('tx');
ok(!$dbi->error, 'Poked tx dump') or diag $dbi->error;
};
#$dbi->disconnect; # Disabled this: see autoclean option
done_testing;
1;
__END__
DB_CONNECT_URL='sqlite://./test.db?sqlite_unicode=1' prove -lv t/05-dump.t
( run in 0.485 second using v1.01-cache-2.11-cpan-a5abf4f5562 )