Skip to content

Commit

Permalink
Fix dbname for URI with // but no authority.
Browse files Browse the repository at this point in the history
In "db:sqlite:///foo.db", the `dbname` should be "foo.db", not "/foo.db". So
try harder to detect that situation. Instead of looking for no slash afte the
two slashes, look instead for a lack of *two* slashes. Yes this is weird. I
find myself again wishing that URI objects were deparsed, instead of stored as
a string to be parsed in every method. Resolves #8.
  • Loading branch information
theory committed Sep 21, 2015
1 parent f6b4431 commit cd65521
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
5 changes: 5 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Revision history for Perl extension URI::db.
DBD::Sybase, since the latter would require Sybase to build.
- URI::sqlserver now inherits from URI::mssql rather than the other way
around.
- Fixed a bug where a URI with three slashes but no authority part after
the first two would incorrectly think the databse name should be an
absolute path. That is, in "db:sqlite:///foo.db", the `dbname` value
is "foo.db", not "/foo.db". Thanks to Dan Book for the report
(issue #8).

0.15 2014-09-04T00:29:46Z
- Added the `canonical_engine` accessor, which returns the canonical
Expand Down
2 changes: 1 addition & 1 deletion lib/URI/_db.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ sub has_recognized_engine {

sub dbname {
my $self = shift;
my $is_full = $self->opaque =~ m{^//(?://|(?!/))};
my $is_full = $self->opaque =~ m{^//(?://|/?(?!/))};
return $self->path($is_full && defined $_[0] ? "/$_[0]" : shift) if @_;
my @segs = $self->path_segments or return;
shift @segs if $is_full;
Expand Down
19 changes: 18 additions & 1 deletion t/db.t
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,16 @@ isa_ok $uri->nested_uri, 'URI::_db';

# Pass a path.
$uri->dbname('/tmp/foo');
pass 'Assign a database name path';
pass 'Assign a database name full path';
is $uri->dbname, '/tmp/foo', 'DB name should be "/tmp/foo"';
is $uri->path, '//tmp/foo', 'Path should be "//tmp/foo"';

# Try a relative path.
$uri->dbname('foo.db');
pass 'Assign a database name relative path';
is $uri->dbname, 'foo.db', 'DB name should be "foo.db"';
is $uri->path, '/foo.db', 'Path should be "/foo.db"';

# Try a Windows path.
$uri->dbname('C:/temp/foo');
pass 'Assign a database Windows path';
Expand Down Expand Up @@ -209,4 +215,15 @@ ok $uri->eq(URI->new( $uri->as_string )), 'URI should equal equiv URI';
ok $uri->eq($uri->clone), 'URI should equal itself cloned';
ok !$uri->eq('pg:'), 'URI should not equal non-DB URI';

# Test dbname in hostless URI.
for my $opaque ('', '//', '//foo@/') {
ok my $uri = URI->new("db:sqlite:$opaque"),
qq{Create URI with opaque "$opaque"};
ok !$uri->dbname, 'dbname should be empty';
$uri->dbname('foo.db');
is $uri->dbname, 'foo.db', 'dbname should be "foo.db"';
my $prefix = $opaque eq '' ? '' : '/';
is $uri->path, "${prefix}foo.db", 'path should be "${prefix}foo.db"';
}

done_testing;
4 changes: 2 additions & 2 deletions t/dbi.t
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ for my $spec (
},
{
uri => 'db:sqlite:///path/foo.db',
dsn => 'dbi:SQLite:dbname=/path/foo.db',
dbi => [ [dbname => '/path/foo.db'] ],
dsn => 'dbi:SQLite:dbname=path/foo.db',
dbi => [ [dbname => 'path/foo.db'] ],
qry => [],
},
{
Expand Down
4 changes: 2 additions & 2 deletions t/engines.t
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ for my $spec (
is $uri->scheme, 'db', 'Scheme should be "db"';
is $uri->engine, $engine, qq{No host, full path URI engine should be "$label"};
is $uri->canonical_engine, $canon, qq{No host, full path URI canonical engine should be "$clabel"};
is $uri->dbname, '/path/to/foo.db',
'No host, full path URI db name should be "/path/to/foo.db"';
is $uri->dbname, 'path/to/foo.db',
'No host, full path URI db name should be "path/to/foo.db"';
is $uri->host, '', 'No host, full path URI host should be empty';
is $uri->port, $port, 'No host, full path URI port should be undef';
is $uri->user, undef, 'No host, full path URI user should be undef';
Expand Down

0 comments on commit cd65521

Please sign in to comment.