From 3997c0ffaf417b59a176413e78e4826a777a1766 Mon Sep 17 00:00:00 2001 From: Laurent Dami Date: Thu, 23 Jul 2009 07:00:26 +0000 Subject: [PATCH] - Accept empty filename at connect (sqlite will open a tempfile) (DAMI) - Documented the connect() method (DAMI) --- Changes | 2 ++ lib/DBD/SQLite.pm | 32 ++++++++++++++++++++++++++++++-- t/33_non_latin_path.t | 17 ++++++++++++++++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/Changes b/Changes index 8d2ebbe..878024b 100644 --- a/Changes +++ b/Changes @@ -15,6 +15,8 @@ Changes for Perl extension DBD-SQLite - Added a default implementation for the REGEXP infix operator (DAMI) - Renamed several internal sqlite3_ functions to sqlite_ for clarity (ISHIGAKI) + - Accept empty filename at connect (sqlite will open a tempfile) (DAMI) + - Documented the connect() method (DAMI) 1.26_02 Fri 19 Jun 2009 - Updated to SQLite 3.6.15 (DUNCAND) diff --git a/lib/DBD/SQLite.pm b/lib/DBD/SQLite.pm index 695c564..a6b18bb 100644 --- a/lib/DBD/SQLite.pm +++ b/lib/DBD/SQLite.pm @@ -98,7 +98,7 @@ 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:' ) { + if ( $^O =~ /MSWin32/ and $real ne ':memory:' and $real ne '') { require Win32; require File::Basename; my ($file, $dir, $suffix) = File::Basename::fileparse($real); @@ -505,7 +505,7 @@ DBD::SQLite - Self-contained RDBMS in a DBI Driver =head1 SYNOPSIS use DBI; - my $dbh = DBI->connect("dbi:SQLite:dbname=dbfile","",""); + my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","",""); =head1 DESCRIPTION @@ -554,6 +554,29 @@ details about core features. Currently many statement attributes are not implemented or are limited by the typeless nature of the SQLite database. +=head1 CONNECTING + +The name of the database file is passed in the the DBI connection +string : + + my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","",""); + +The file is opened in read/write mode, and will be created if +it does not exist yet. + +If the filename C<$dbfile> is ":memory:", then a private, temporary +in-memory database is created for the connection. This in-memory +database will vanish when the database connection is closed. Future +versions of SQLite might make use of additional special filenames that +begin with the ":" character. It is recommended that when a database +filename actually does begin with a ":" character you should prefix +the filename with a pathname such as "./" to avoid ambiguity. + +If the filename C<$dbfile> is an empty string, then a private, +temporary on-disk database will be created. This private database will +be automatically deleted as soon as the database connection is closed. + + =head1 DRIVER PRIVATE ATTRIBUTES =head2 Database Handle Attributes @@ -1290,6 +1313,11 @@ code we work with leaks. Reading/writing into blobs using C / C. +=head2 Flags for sqlite3_open_v2 + +Support the full API of sqlite3_open_v2 (flags for opening the file). + + =head1 SUPPORT Bugs should be reported via the CPAN bug tracker at diff --git a/t/33_non_latin_path.t b/t/33_non_latin_path.t index d7af9f4..8d1aa96 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') ? 13 : 25) ); + plan( tests => (($^O eq 'cygwin') ? 15 : 27) ); } else { plan( skip_all => 'Unicode is not supported before 5.8.5' ); } @@ -75,6 +75,21 @@ foreach my $subdir ( 'longascii', 'adatb unlink(_path($dbfilex)) if -e _path($dbfilex); } + +# connect to an empty filename - sqlite will create a tempfile +eval { + my $dbh = DBI->connect("dbi:SQLite:dbname=", undef, undef, { + RaiseError => 1, + PrintError => 0, + } ); + isa_ok( $dbh, 'DBI::db' ); +}; +is( $@, '', "Could connect to temp database (empty filename)" ); +diag( $@ ) if $@; + + + + sub _path { # copied from DBD::SQLite::connect my $path = shift;