Acrux-DBI
view release on metacpan or search on metacpan
lib/Acrux/DBI.pm view on Meta::CPAN
$dbi->query( ... );
$tx->commit;
};
say $@ if $@;
=head2 url
my $url = $dbi->url;
$dbi = $dbi->url('sqlite:///tmp/test.db?sqlite_unicode=1');
$dbi = $dbi->url('sqlite:///./test.db?sqlite_unicode=1'); # '/./' will be removed
$dbi = $dbi->url('postgres://foo:pass@localhost/mydb?PrintError=1');
$dbi = $dbi->url('mysql://foo:pass@localhost/test?mysql_enable_utf8=1');
Database connect url
The database connection URL from which all other attributes can be derived.
C<"url"> must be specified before the first call to C<"connect"> is made,
otherwise it will have no effect on setting the defaults.
For using SQLite databases with files relative to current directory you cat use '/./' prefix:
lib/Acrux/DBI.pm view on Meta::CPAN
} elsif ($dr eq 'mysql') {
push @params, sprintf("%s=%s", "database", $db) if length $db;
push @params, sprintf("%s=%s", "host", $self->host);
push @params, sprintf("%s=%s", "port", $self->port) if $self->port;
$dsn = sprintf('DBI:mysql:%s', join(";", @params) || '');
} elsif ($dr eq 'maria' or $dr eq 'mariadb') {
push @params, sprintf("%s=%s", "database", $db) if length $db;
push @params, sprintf("%s=%s", "host", $self->host);
push @params, sprintf("%s=%s", "port", $self->port) if $self->port;
$dsn = sprintf('DBI:MariaDB:%s', join(";", @params) || '');
} elsif ($dr eq 'pg' or $dr eq 'pgsql' or $dr eq 'postgres' or $dr eq 'postgresql') {
push @params, sprintf("%s=%s", "dbname", $db) if length $db;
push @params, sprintf("%s=%s", "host", $self->host);
push @params, sprintf("%s=%s", "port", $self->port) if $self->port;
$dsn = sprintf('DBI:Pg:%s', join(";", @params) || '');
} elsif ($dr eq 'oracle') {
push @params, sprintf("%s=%s", "host", $self->host);
push @params, sprintf("%s=%s", "sid", $db) if length $db;
push @params, sprintf("%s=%s", "port", $self->port) if $self->port;
$dsn = sprintf('DBI:Oracle:%s', join(";", @params) || '');
} else {
# Copyright (C) 1998-2026 D&D Corporation
#
# This program is distributed under the terms of the Artistic License 2.0
#
#########################################################################
use Test::More;
use Acrux::DBI;
ok(Acrux::DBI->VERSION, 'Version');
# 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;
}
t/03-connect.t view on Meta::CPAN
# 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');
#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;
t/03-connect.t view on Meta::CPAN
my $res = $dbi->query('DROP TABLE IF EXISTS `names`');
ok($res, 'Drop table') or diag $dbi->error;
};
done_testing;
1;
__END__
DB_CONNECT_URL='postgres://foo:pass@localhost/mydb?PrintError=1&foo=123' prove -lv t/03-connect.t
DB_CONNECT_URL='mysql://test:test@192.168.0.1/test?mysql_auto_reconnect=1&mysql_enable_utf8=1' prove -lv t/03-connect.t
( run in 0.811 second using v1.01-cache-2.11-cpan-ceb78f64989 )