diff --git a/lib/DBD/SQLite.pm b/lib/DBD/SQLite.pm index 2351fe6..7a6a19b 100644 --- a/lib/DBD/SQLite.pm +++ b/lib/DBD/SQLite.pm @@ -112,17 +112,13 @@ sub connect { # To avoid unicode and long file name problems on Windows, # convert to the shortname if the file (or parent directory) exists. - if ( $^O =~ /MSWin32/ and $real ne ':memory:' and $real ne '' and $real !~ /^file:/) { - require Win32; + if ( $^O =~ /MSWin32/ and $real ne ':memory:' and $real ne '' and $real !~ /^file:/ and !-f $real ) { require File::Basename; my ($file, $dir, $suffix) = File::Basename::fileparse($real); - my $short = Win32::GetShortPathName($real); - if ( $short && -f $short ) { - # Existing files will work directly. - $real = $short; - } elsif ( -d $dir ) { - # We are creating a new file. - # Does the directory it's in at least exist? + # We are creating a new file. + # Does the directory it's in at least exist? + if ( -d $dir ) { + require Win32; $real = join '', grep { defined } Win32::GetShortPathName($dir), $file, $suffix; } else { # SQLite can't do mkpath anyway. diff --git a/t/33_non_latin_path.t b/t/33_non_latin_path.t index 5ec52df..40550ea 100644 --- a/t/33_non_latin_path.t +++ b/t/33_non_latin_path.t @@ -13,7 +13,7 @@ use t::lib::Test; use Test::More; BEGIN { if ( $] >= 5.008005 ) { - plan( tests => (($^O eq 'cygwin') ? 14 : 26) ); + plan( tests => 2 + 12 * (($^O eq 'cygwin') ? 2 : 4) ); } else { plan( skip_all => 'Unicode is not supported before 5.8.5' ); } @@ -45,6 +45,18 @@ foreach my $subdir ( 'longascii', 'adatb }; is( $@, '', "Could connect to database in $subdir" ); diag( $@ ) if $@; + + # Reopen the database + eval { + my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", undef, undef, { + RaiseError => 1, + PrintError => 0, + } ); + isa_ok( $dbh, 'DBI::db' ); + }; + is( $@, '', "Could connect to database in $subdir" ); + diag( $@ ) if $@; + unlink(_path($dbfile)) if -e _path($dbfile); # Repeat with the unicode flag on @@ -59,6 +71,19 @@ foreach my $subdir ( 'longascii', 'adatb }; is( $@, '', "Could connect to database in $subdir" ); diag( $@ ) if $@; + + # Reopen the database + eval { + my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", undef, undef, { + RaiseError => 1, + PrintError => 0, + sqlite_unicode => 1, + } ); + isa_ok( $dbh, 'DBI::db' ); + }; + is( $@, '', "Could connect to database in $subdir" ); + diag( $@ ) if $@; + unlink(_path($ufile)) if -e _path($ufile); # when the name of the database file has non-latin characters @@ -67,6 +92,14 @@ foreach my $subdir ( 'longascii', 'adatb DBI->connect("dbi:SQLite:dbname=$dbfilex", "", "", {RaiseError => 1, PrintError => 0}); }; ok(!$@, "Could connect to database in $dbfilex") or diag $@; + ok -f _path($dbfilex), "file exists: "._path($dbfilex)." ($dbfilex)"; + + # Reopen the database + eval { + DBI->connect("dbi:SQLite:dbname=$dbfilex", "", "", {RaiseError => 1, PrintError => 0}); + }; + ok(!$@, "Could connect to database in $dbfilex") or diag $@; + unlink(_path($dbfilex)) if -e _path($dbfilex); }