Acrux-DBI
view release on metacpan or search on metacpan
lib/Acrux/DBI.pm view on Meta::CPAN
my $options = $dbi->options;
This method returns options that will be used for generating the connection DSN
Default: all passed options to constructor merged with system defaults:
RaiseError => 0,
PrintError => 0,
PrintWarn => 0,
=head2 password
my $password = $dbi->password;
This is the L<Mojo::URL/password> that will be used for generating the connection DSN
default: none
=head2 ping
$dbi->ping ? 'OK' : 'Database session is expired';
Checks the connection to database
=head2 port
lib/Acrux/DBI.pm view on Meta::CPAN
my $opts = $self->{opts}; # defaults
my $query = $self->{uri}->query;
my %params = ();
$params{$_} = $query->param($_) for @{$query->names};
return { (%$opts, %params) } ; # merge defaults and URL params
}
sub username {
my $self = shift;
return $self->{uri}->username // '';
}
sub password {
my $self = shift;
return $self->{uri}->password // '';
}
sub userinfo {
my $self = shift;
return $self->{uri}->userinfo // '';
}
sub database {
my $self = shift;
my $u = $self->{uri};
my $dr = $self->driver;
my $db = '';
lib/Acrux/DBI.pm view on Meta::CPAN
sub errstr {
my $self = shift;
return $self->dbh->errstr // $DBI::errstr if defined($self->dbh) && $self->dbh->can('errstr');
return $DBI::errstr;
}
# Database methods
sub connect {
my $self = shift;
$self->{error} = '';
my $dbh = DBI->connect($self->dsn, $self->username, $self->password, $self->options);
if ($dbh) {
$self->{dbh} = $dbh;
printf STDERR "Connected to '%s'\n", $self->dsn if DEBUG;
} else {
$self->{error} = $DBI::errstr || "DBI->connect failed";
$self->{dbh} = undef;
}
return $self;
}
sub connect_cached {
my $self = shift;
$self->{error} = '';
my %opts = %{($self->options)};
$opts{private_cachekey} = $self->cachekey;
my $dbh = DBI->connect_cached($self->dsn, $self->username, $self->password, {%opts});
if ($dbh) {
$self->{dbh} = $dbh;
printf STDERR "Connected (cached) to '%s'\n", $self->dsn if DEBUG;
} else {
$self->{error} = $DBI::errstr || "DBI->connect failed";
$self->{dbh} = undef;
}
return $self;
}
sub disconnect {
# URL: 'postgres://foo:pass@localhost/mydb?PrintError=1&foo=123'
{
my $dbi = Acrux::DBI->new(undef, bar => 'baz');
$dbi->url('postgres://foo:pass@localhost/mydb?PrintError=1&foo=123');
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->username, 'foo', 'Username is foo');
is($dbi->password, 'pass', 'Password is pass');
is($dbi->database, 'mydb', 'Password is mydb');
is($dbi->dsn, 'DBI:Pg:dbname=mydb;host=localhost', 'DSN is DBI:Pg:dbname=mydb;host=localhost');
#note explain $dbi->cachekey;
}
done_testing;
1;
__END__
t/03-connect.t view on Meta::CPAN
$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');
#is($dbi->dsn, 'DBI:Pg:dbname=mydb;host=localhost', 'DSN is DBI:Pg:dbname=mydb;host=localhost');
#note explain {(DBI->installed_drivers)};
#my $res = $dbi->query('select * from monm');
#ok($res, 'select * from monm') or diag $dbi->error;
#note explain $res;
#note explain $res->array;
#note explain $res->arrays;
#note explain $res->collection_list;
( run in 0.491 second using v1.01-cache-2.11-cpan-49f99fa48dc )