Index: Makefile.in ================================================================== --- Makefile.in +++ Makefile.in @@ -380,10 +380,11 @@ $(TOP)/src/test_backup.c \ $(TOP)/src/test_bestindex.c \ $(TOP)/src/test_blob.c \ $(TOP)/src/test_btree.c \ $(TOP)/src/test_config.c \ + $(TOP)/src/test_delete.c \ $(TOP)/src/test_demovfs.c \ $(TOP)/src/test_devsym.c \ $(TOP)/src/test_fs.c \ $(TOP)/src/test_func.c \ $(TOP)/src/test_hexio.c \ Index: Makefile.msc ================================================================== --- Makefile.msc +++ Makefile.msc @@ -1267,10 +1267,11 @@ $(TOP)\src\test_backup.c \ $(TOP)\src\test_bestindex.c \ $(TOP)\src\test_blob.c \ $(TOP)\src\test_btree.c \ $(TOP)\src\test_config.c \ + $(TOP)\src\test_delete.c \ $(TOP)\src\test_demovfs.c \ $(TOP)\src\test_devsym.c \ $(TOP)\src\test_fs.c \ $(TOP)\src\test_func.c \ $(TOP)\src\test_hexio.c \ Index: main.mk ================================================================== --- main.mk +++ main.mk @@ -291,10 +291,11 @@ $(TOP)/src/test_backup.c \ $(TOP)/src/test_bestindex.c \ $(TOP)/src/test_blob.c \ $(TOP)/src/test_btree.c \ $(TOP)/src/test_config.c \ + $(TOP)/src/test_delete.c \ $(TOP)/src/test_demovfs.c \ $(TOP)/src/test_devsym.c \ $(TOP)/src/test_fs.c \ $(TOP)/src/test_func.c \ $(TOP)/src/test_hexio.c \ Index: src/test1.c ================================================================== --- src/test1.c +++ src/test1.c @@ -2385,10 +2385,33 @@ res = sqlite3_snapshot_cmp(p1, p2); Tcl_SetObjResult(interp, Tcl_NewIntObj(res)); return TCL_OK; } #endif /* SQLITE_ENABLE_SNAPSHOT */ + +/* +** Usage: sqlite3_delete_database FILENAME +*/ +int sqlite3_delete_database(const char*); /* in test_delete.c */ +static int SQLITE_TCLAPI test_delete_database( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + int rc; + const char *zFile; + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "FILE"); + return TCL_ERROR; + } + zFile = (const char*)Tcl_GetString(objv[1]); + rc = sqlite3_delete_database(zFile); + + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + return TCL_OK; +} /* ** Usage: sqlite3_next_stmt DB STMT ** ** Return the next statment in sequence after STMT. @@ -7481,10 +7504,11 @@ { "sqlite3_snapshot_get", test_snapshot_get, 0 }, { "sqlite3_snapshot_open", test_snapshot_open, 0 }, { "sqlite3_snapshot_free", test_snapshot_free, 0 }, { "sqlite3_snapshot_cmp", test_snapshot_cmp, 0 }, #endif + { "sqlite3_delete_database", test_delete_database, 0 }, }; static int bitmask_size = sizeof(Bitmask)*8; static int longdouble_size = sizeof(LONGDOUBLE_TYPE); int i; extern int sqlite3_sync_count, sqlite3_fullsync_count; ADDED src/test_delete.c Index: src/test_delete.c ================================================================== --- /dev/null +++ src/test_delete.c @@ -0,0 +1,140 @@ +/* +** 2016 September 10 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains test code to delete an SQLite database and all +** of its associated files. Associated files include: +** +** * The journal file. +** * The wal file. +** * The SQLITE_ENABLE_8_3_NAMES version of the db, journal or wal files. +** * Files created by the test_multiplex.c module to extend any of the +** above. +*/ + +#if SQLITE_OS_WIN +# include +# define F_OK 0 +#else +# include +#endif +#include +#include +#include "sqlite3.h" + +/* The following #defines are copied from test_multiplex.c */ +#ifndef MX_CHUNK_NUMBER +# define MX_CHUNK_NUMBER 299 +#endif +#ifndef SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET +# define SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET 400 +#endif +#ifndef SQLITE_MULTIPLEX_WAL_8_3_OFFSET +# define SQLITE_MULTIPLEX_WAL_8_3_OFFSET 700 +#endif + +/* +** This routine is a copy of (most of) the code from SQLite function +** sqlite3FileSuffix3(). It modifies the filename in buffer z in the +** same way as SQLite does when in 8.3 filenames mode. +*/ +static void sqlite3Delete83Name(char *z){ + int i, sz; + sz = strlen(z); + for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} + if( z[i]=='.' && (sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); +} + +/* +** zFile is a filename. Assuming no error occurs, if this file exists, +** set *pbExists to true and unlink it. Or, if the file does not exist, +** set *pbExists to false before returning. +** +** If an error occurs, the value of errno is returned. Or, if no error +** occurs, zero is returned. +*/ +static int sqlite3DeleteUnlinkIfExists(const char *zFile, int *pbExists){ + int rc; + rc = access(zFile, F_OK); + if( rc ){ + if( errno==ENOENT ){ + if( pbExists ) *pbExists = 0; + return 0; + } + return errno; + } + if( pbExists ) *pbExists = 1; + rc = unlink(zFile); + if( rc ) return errno; + return 0; +} + +/* +** Delete the database file identified by the string argument passed to this +** function. The string must contain a filename, not an SQLite URI. +*/ +int sqlite3_delete_database( + const char *zFile /* File to delete */ +){ + char *zBuf; /* Buffer to sprintf() filenames to */ + int nBuf; /* Size of buffer in bytes */ + int rc = 0; /* System error code */ + int i; /* Iterate through azFmt[] and aMFile[] */ + + const char *azFmt[] = { "%s", "%s-journal", "%s-wal", "%s-shm" }; + + struct MFile { + const char *zFmt; + int iOffset; + int b83; + } aMFile[] = { + { "%s%03d", 0, 0 }, + { "%s-journal%03d", 0, 0 }, + { "%s-wal%03d", 0, 0 }, + { "%s%03d", 0, 1 }, + { "%s-journal%03d", SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET, 1 }, + { "%s-wal%03d", SQLITE_MULTIPLEX_WAL_8_3_OFFSET, 1 }, + }; + + /* Allocate a buffer large enough for any of the files that need to be + ** deleted. */ + nBuf = strlen(zFile) + 100; + zBuf = (char*)sqlite3_malloc(nBuf); + if( zBuf==0 ) return SQLITE_NOMEM; + + /* Delete both the regular and 8.3 filenames versions of the database, + ** journal, wal and shm files. */ + for(i=0; rc==0 && izFmt, zFile, iChunk+p->iOffset); + if( p->b83 ) sqlite3Delete83Name(zBuf); + rc = sqlite3DeleteUnlinkIfExists(zBuf, &bExists); + if( bExists==0 || rc!=0 ) break; + } + } + + sqlite3_free(zBuf); + return (rc ? SQLITE_ERROR : SQLITE_OK); +} + + ADDED test/delete_db.test Index: test/delete_db.test ================================================================== --- /dev/null +++ test/delete_db.test @@ -0,0 +1,214 @@ +# 2016 September 10 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the DELETE FROM statement. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix delete_db + +proc delete_all {} { + foreach f [glob -nocomplain test2*] { file delete $f } + foreach f [glob -nocomplain test3*] { file delete $f } +} + +proc copydb {} { + foreach f [glob -nocomplain test3*] { file delete $f } + foreach f [glob -nocomplain test2*] { + set p [string range $f 5 end] + file copy "test2$p" "test3$p" + } +} + +proc files {} { + lsort [glob -nocomplain test3*] +} + +db close +delete_all +sqlite3 db test2.database + +#------------------------------------------------------------------------- +# +# 1.1: Journal files. +# 1.2: Wal files. +# 1.3: Multiplexor with journal file. +# 1.4: Multiplexor with wal file. +# +# 2.* are a copy of 1.* with the multiplexor enabled. +# +# 3.* tests errors. +# + +do_test 1.1.0 { + execsql { + CREATE TABLE t1(x, y); + BEGIN; + INSERT INTO t1 VALUES(1, 2); + } + copydb + files +} {test3.database test3.database-journal} + +do_test 1.1.1 { + sqlite3_delete_database test3.database + files +} {} + +do_test 1.2.0 { + execsql { + COMMIT; + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES(3, 4); + } + copydb + files +} {test3.database test3.database-shm test3.database-wal} +do_test 1.2.1 { + sqlite3_delete_database test3.database + files +} {} + +db close +delete_all +sqlite3_multiplex_initialize "" 0 +sqlite3 db test2.database -vfs multiplex +sqlite3_multiplex_control db "main" chunk_size 32768 + +do_test 1.3.0 { + execsql { + CREATE TABLE x1(a, b); + WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1000 ) + INSERT INTO x1 SELECT randomblob(100), randomblob(100) FROM s; + BEGIN; + UPDATE x1 SET a=randomblob(101) + } + copydb + files +} [list {*}{ + test3.database test3.database-journal test3.database001 + test3.database002 test3.database003 +}] +do_test 1.3.1 { + sqlite3_delete_database test3.database + files +} {} + + +do_test 1.4.0 { + execsql { + COMMIT; + PRAGMA journal_mode = wal; + UPDATE x1 SET a=randomblob(102) + } + copydb + files +} [list {*}{ + test3.database test3.database-shm test3.database-wal test3.database001 + test3.database002 test3.database003 +}] +do_test 1.4.1 { + sqlite3_delete_database test3.database + files +} {} + + +ifcapable 8_3_names { + db close + delete_all + sqlite3 db file:test2.db?8_3_names=1 -uri 1 + + do_test 2.1.0 { + execsql { + CREATE TABLE t1(x, y); + BEGIN; + INSERT INTO t1 VALUES(1, 2); + } + copydb + files + } {test3.db test3.nal} + + do_test 2.1.1 { + sqlite3_delete_database test3.db + files + } {} + + do_test 2.2.0 { + execsql { + COMMIT; + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES(3, 4); + } + copydb + files + } {test3.db test3.shm test3.wal} + do_test 2.2.1 { + sqlite3_delete_database test3.db + files + } {} + + + db close + delete_all + sqlite3_multiplex_initialize "" 0 + sqlite3 db file:test2.db?8_3_names=1 -uri 1 -vfs multiplex + sqlite3_multiplex_control db "main" chunk_size 32768 + + do_test 2.3.0 { + execsql { + CREATE TABLE x1(a, b); + WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1000 ) + INSERT INTO x1 SELECT randomblob(100), randomblob(100) FROM s; + BEGIN; + UPDATE x1 SET a=randomblob(101) + } + copydb + files + } [list {*}{ + test3.001 test3.002 test3.003 test3.db test3.nal + }] + do_test 2.3.1 { + sqlite3_delete_database test3.db + files + } {} + + + do_test 2.4.0 { + execsql { + COMMIT; + PRAGMA journal_mode = wal; + UPDATE x1 SET a=randomblob(102) + } + copydb + files + } [list {*}{ + test3.001 test3.002 test3.003 test3.db test3.db-shm test3.wal + }] + do_test 2.4.1 { + sqlite3_delete_database test3.db + files + } {} +} + +db close +delete_all +sqlite3_multiplex_shutdown + +do_test 3.0 { + file mkdir dir2.db + sqlite3_delete_database dir2.db +} {SQLITE_ERROR} +do_test 3.1 { + sqlite3_delete_database dir2.db/test.db +} {SQLITE_OK} + +finish_test