From 96d81f99d4f9dd30235426c8c237647f2f670e2b Mon Sep 17 00:00:00 2001 From: danielk1977 Date: Sat, 19 Jun 2004 03:33:57 +0000 Subject: [PATCH] Update sqlite3_close() API to match documentation. (CVS 1630) FossilOrigin-Name: 61819740fe8916892afe502196bb8b75a51f81e8 --- manifest | 28 ++++++++++++++-------------- manifest.uuid | 2 +- src/main.c | 24 ++++++++++++++++-------- src/sqlite.h.in | 9 +++++++-- src/sqliteInt.h | 3 +-- src/test1.c | 6 ++++-- src/util.c | 8 +++----- src/vdbeaux.c | 3 --- test/capi3.test | 33 +++++++++++++++++++++++++++------ test/tableapi.test | 4 ++-- 10 files changed, 75 insertions(+), 45 deletions(-) diff --git a/manifest b/manifest index f1c7cf490d..2496fb8179 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Update\ssqlite.def\sfor\sversion\s3.0.\s(CVS\s1629) -D 2004-06-19T03:26:12 +C Update\ssqlite3_close()\sAPI\sto\smatch\sdocumentation.\s(CVS\s1630) +D 2004-06-19T03:33:57 F Makefile.in d69d53c543518c1572ee0a8e8723d7e00bdb2266 F Makefile.linux-gcc a9e5a0d309fa7c38e7c14d3ecf7690879d3a5457 F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd @@ -38,7 +38,7 @@ F src/hash.c 440c2f8cb373ee1b4e13a0988489c7cd95d55b6f F src/hash.h 762d95f1e567664d1eafc1687de755626be962fb F src/insert.c 1428887f4a7515a7d34e82aaeb76297c79ba378b F src/legacy.c ad23746f15f67e34577621b1875f639c94839e1f -F src/main.c da53b961729954bd4c4408ee339f2e2b690feb98 +F src/main.c 54ce5c19d64dd41381f92fa5e4610d9408c9b9a3 F src/md5.c d77a389955759c8329bb357e3d71bac3d6eb710b F src/os.h 1cb5f0293a30288451fe3c0c73815cf208212ed1 F src/os_common.h ba1b7306e16e2091718f2c48db0fe6c1d7a31bb8 @@ -56,11 +56,11 @@ F src/printf.c 823b6a5cbedb6971a9e62f5d83204fe9b0be7c1b F src/random.c eff68e3f257e05e81eae6c4d50a51eb88beb4ff3 F src/select.c 91570af7f13d1c5584f3b711676e1226ee12194e F src/shell.c ca519519dcbbc582f6d88f7d0e7583b857fd3469 -F src/sqlite.h.in eacd23af3280603cd8b48979c6f9297b3ac2935c -F src/sqliteInt.h 570731b6aed20f0e5dd95b03442a215f17580c19 +F src/sqlite.h.in 70091409ee7013dc06f8750ca142389b61227e30 +F src/sqliteInt.h 9cd659c9c23a6b9fa9a4dc1731994d90d8ccdaf7 F src/table.c af14284fa36c8d41f6829e3f2819dce07d3e2de2 F src/tclsqlite.c 16a56baa1294cbea3cbc3131d71e7881cdee1d67 -F src/test1.c 963f4417d1770f5238672ff5b817a9103842eeb7 +F src/test1.c 19858f61aa20a054611131c28bce21bed88f972e F src/test2.c db0a0c30b59f1639cb8c2572be06439402833cc8 F src/test3.c 7247090d15a5a43823079b6fd8dad1ed3cccdedf F src/test4.c a921a69821fd30209589228e64f94e9f715b6fe2 @@ -69,13 +69,13 @@ F src/tokenize.c 900374b6b37f04748bcd48c2d29a41c251542935 F src/trigger.c b05699cf40e99a330649eabfb1cece05ff73e469 F src/update.c f9a03233577e0c3d57234d1957963875fc941da2 F src/utf.c 3a2596013e4b9582d075ca742de7f067ff7dee95 -F src/util.c e59ee2afc6650bd62466b8d26560794c844ff66d +F src/util.c 1b3743413e11cae51a899dc03fa9b829a3a1f160 F src/vacuum.c f9561c8095407a970af4e6a304b77c4083433d3e F src/vdbe.c 2a0e908e46ea6822f215cb0f35ec31f5c2832629 F src/vdbe.h 46f74444a213129bc4b5ce40124dd8ed613b0cde F src/vdbeInt.h 54af2e00301cde7573b86ea8a9b29a2152e762f7 F src/vdbeapi.c 1601fb2d015fc88ce3dd6a6a50cf44b6419fdbbf -F src/vdbeaux.c dc0e7d3bdf3b6f322448b4bee29fe5bec656b4d4 +F src/vdbeaux.c b3c13f05f62ed9269d4919d1d05a1d6ba44448e3 F src/vdbemem.c 9359c53386e070fea9f5403cab0c6f0cfe36496b F src/where.c 6507074d8ce3f78e7a4cd33f667f11e62020553e F test/all.test 569a92a8ee88f5300c057cc4a8f50fbbc69a3242 @@ -94,7 +94,7 @@ F test/btree5.test 8e5ff32c02e685d36516c6499add9375fe1377f2 F test/btree6.test a5ede6bfbbb2ec8b27e62813612c0f28e8f3e027 F test/btree7.test 429b96cfef5b51a7d512cfb4b5b3e453384af293 F test/capi2.test 7a0d71b27cfc5337ec3525c02b9de269b5b9c175 -F test/capi3.test 6ed6bcbc8448dc7ee05c15596875f26fbaa1a037 +F test/capi3.test 6528034f21c4e8e404032124cb58b14ce934598c F test/collate1.test 2ee4fa3a47a652ccf56c5ddf65dcc44d9bad82ef F test/collate2.test c1a3b41f761b28853c5696037f92de928f93233b F test/collate3.test e60b428e07ec945492ba90ff1c895902ee3a8a50 @@ -152,7 +152,7 @@ F test/select6.test a9e31906e700e7c7592c4d0acfc022808f718baf F test/sort.test dbd94673b05054e44ca3f08a80faa1e890ef06d8 F test/subselect.test f0fea8cf9f386d416d64d152e3c65f9116d0f50f F test/table.test 06c077c82d1a0df45426518064e674d97def6485 -F test/tableapi.test 8651d6d1b043217aa851632b515ab68f185fb430 +F test/tableapi.test b21ab097e87a5484bb61029e69e1a4e5c5e65ede F test/tclsqlite.test a69e99b995c913dde3e554da8b3f8f483715fdd3 F test/temptable.test 63a16e3ad19adf073cfbcdf7624c92ac5236522c F test/tester.tcl f36cc22d0532725073ca78272d7834d56dceb6d9 @@ -225,7 +225,7 @@ F www/tclsqlite.tcl 19191cf2a1010eaeff74c51d83fd5f5a4d899075 F www/vdbe.tcl 59288db1ac5c0616296b26dce071c36cb611dfe9 F www/version3.tcl af528563442e3039928f9018327a18157e53a44f F www/whentouse.tcl a8335bce47cc2fddb07f19052cb0cb4d9129a8e4 -P 1f43219a7402af7255743466731dba2afb31d12b -R 04cb6cb57e48220a19336f286fc84528 -U drh -Z 91bdc31755ee1b84ef2ddbce38c07d07 +P 327780ade9ac99470018320218f39fbbc8f21837 +R 078ac56f136c6acfadf7a29732ae01f0 +U danielk1977 +Z 6a0040126724f15aa7fc1543066e7d58 diff --git a/manifest.uuid b/manifest.uuid index 725dea9cbe..d3e599a428 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -327780ade9ac99470018320218f39fbbc8f21837 \ No newline at end of file +61819740fe8916892afe502196bb8b75a51f81e8 \ No newline at end of file diff --git a/src/main.c b/src/main.c index 3d83bc1b28..62d5c0a882 100644 --- a/src/main.c +++ b/src/main.c @@ -14,7 +14,7 @@ ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** -** $Id: main.c,v 1.227 2004/06/19 02:22:11 danielk1977 Exp $ +** $Id: main.c,v 1.228 2004/06/19 03:33:57 danielk1977 Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -462,27 +462,32 @@ int sqlite3_last_statement_changes(sqlite *db){ /* ** Close an existing SQLite database */ -void sqlite3_close(sqlite *db){ +int sqlite3_close(sqlite *db){ HashElem *i; int j; if( !db ){ - return; + return SQLITE_OK; } - db->want_to_close = 1; + + /* If there are any outstanding VMs, return SQLITE_BUSY. */ + if( db->pVdbe ){ + sqlite3Error(db, SQLITE_BUSY, + "Unable to close due to unfinalised statements"); + return SQLITE_BUSY; + } + assert( !sqlite3SafetyCheck(db) ); /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database ** cannot be opened for some reason. So this routine needs to run in ** that case. But maybe there should be an extra magic value for the ** "failed to open" state. */ - if( db->magic!=SQLITE_MAGIC_CLOSED && - (sqlite3SafetyCheck(db) || sqlite3SafetyOn(db)) ){ + if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){ /* printf("DID NOT CLOSE\n"); fflush(stdout); */ - return; + return SQLITE_ERROR; } - db->magic = SQLITE_MAGIC_CLOSED; for(j=0; jnDb; j++){ struct Db *pDb = &db->aDb[j]; if( pDb->pBt ){ @@ -515,7 +520,10 @@ void sqlite3_close(sqlite *db){ if( db->pErr ){ sqlite3ValueFree(db->pErr); } + + db->magic = SQLITE_MAGIC_ERROR; sqliteFree(db); + return SQLITE_OK; } /* diff --git a/src/sqlite.h.in b/src/sqlite.h.in index 5ddda29a76..4bbb2ed9ea 100644 --- a/src/sqlite.h.in +++ b/src/sqlite.h.in @@ -12,7 +12,7 @@ ** This header file defines the interface that the SQLite library ** presents to client programs. ** -** @(#) $Id: sqlite.h.in,v 1.102 2004/06/17 19:04:17 drh Exp $ +** @(#) $Id: sqlite.h.in,v 1.103 2004/06/19 03:33:57 danielk1977 Exp $ */ #ifndef _SQLITE_H_ #define _SQLITE_H_ @@ -50,8 +50,13 @@ typedef struct sqlite sqlite3; ** ** Call this function with a pointer to a structure that was previously ** returned from sqlite3_open() and the corresponding database will by closed. +** +** All SQL statements prepared using sqlite3_prepare() or +** sqlite3_prepare16() must be deallocated using sqlite3_finalize() before +** this routine is called. Otherwise, SQLITE_BUSY is returned and the +** database connection remains open. */ -void sqlite3_close(sqlite *); +int sqlite3_close(sqlite *); /* ** The type for a callback function. diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 4334306d32..e714dd6902 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -11,7 +11,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.289 2004/06/18 17:45:12 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.290 2004/06/19 03:33:57 danielk1977 Exp $ */ #include "config.h" #include "sqlite3.h" @@ -390,7 +390,6 @@ struct sqlite { int flags; /* Miscellanous flags. See below */ u8 file_format; /* What file format version is this database? */ u8 safety_level; /* How aggressive at synching data to disk */ - u8 want_to_close; /* Close after all VDBEs are deallocated */ u8 temp_store; /* 1=file, 2=memory, 0=compile-time default */ int next_cookie; /* Next value of aDb[0].schema_cookie */ int cache_size; /* Number of pages to use in the cache */ diff --git a/src/test1.c b/src/test1.c index e5cc0e2180..6e37512181 100644 --- a/src/test1.c +++ b/src/test1.c @@ -13,7 +13,7 @@ ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** -** $Id: test1.c,v 1.78 2004/06/18 04:24:55 danielk1977 Exp $ +** $Id: test1.c,v 1.79 2004/06/19 03:33:57 danielk1977 Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -323,13 +323,15 @@ static int sqlite_test_close( char **argv /* Text of each argument */ ){ sqlite *db; + int rc; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " FILENAME\"", 0); return TCL_ERROR; } if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; - sqlite3_close(db); + rc = sqlite3_close(db); + Tcl_SetResult(interp, errorName(rc), TCL_STATIC); return TCL_OK; } diff --git a/src/util.c b/src/util.c index 55b82357ee..703f82c2dc 100644 --- a/src/util.c +++ b/src/util.c @@ -14,7 +14,7 @@ ** This file contains functions for allocating memory, comparing ** strings, and stuff like that. ** -** $Id: util.c,v 1.104 2004/06/18 17:10:17 drh Exp $ +** $Id: util.c,v 1.105 2004/06/19 03:33:57 danielk1977 Exp $ */ #include "sqliteInt.h" #include @@ -1098,8 +1098,7 @@ int sqlite3SafetyOn(sqlite *db){ if( db->magic==SQLITE_MAGIC_OPEN ){ db->magic = SQLITE_MAGIC_BUSY; return 0; - }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR - || db->want_to_close ){ + }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR ){ db->magic = SQLITE_MAGIC_ERROR; db->flags |= SQLITE_Interrupt; } @@ -1115,8 +1114,7 @@ int sqlite3SafetyOff(sqlite *db){ if( db->magic==SQLITE_MAGIC_BUSY ){ db->magic = SQLITE_MAGIC_OPEN; return 0; - }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR - || db->want_to_close ){ + }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR ){ db->magic = SQLITE_MAGIC_ERROR; db->flags |= SQLITE_Interrupt; } diff --git a/src/vdbeaux.c b/src/vdbeaux.c index b9d23b36ce..e4f6434a42 100644 --- a/src/vdbeaux.c +++ b/src/vdbeaux.c @@ -1323,9 +1323,6 @@ int sqlite3VdbeFinalize(Vdbe *p, char **pzErrMsg){ db = p->db; rc = sqlite3VdbeReset(p, pzErrMsg); sqlite3VdbeDelete(p); - if( db->want_to_close && db->pVdbe==0 ){ - sqlite3_close(db); - } if( rc==SQLITE_SCHEMA ){ sqlite3ResetInternalSchema(db, 0); } diff --git a/test/capi3.test b/test/capi3.test index 166f9371c2..8b73ee2938 100644 --- a/test/capi3.test +++ b/test/capi3.test @@ -11,7 +11,7 @@ # This file implements regression tests for SQLite library. The # focus of this script testing the callback-free C/C++ API. # -# $Id: capi3.test,v 1.11 2004/06/19 00:16:31 drh Exp $ +# $Id: capi3.test,v 1.12 2004/06/19 03:33:57 danielk1977 Exp $ # set testdir [file dirname $argv0] @@ -48,6 +48,7 @@ proc utf8 {str} { # capi3-3.*: Test sqlite3_open # capi3-4.*: Test sqlite3_open16 # capi3-5.*: Test the various sqlite3_result_* APIs +# capi3-6.*: Test that sqlite3_close fails if there are outstanding VMs. # db close @@ -118,7 +119,7 @@ do_test capi3-3.1 { # FIX ME: Should test the db handle works. do_test capi3-3.2 { sqlite3_close $db2 -} {} +} {SQLITE_OK} do_test capi3-3.3 { catch { set db2 [sqlite3_open /bogus/path/test.db {}] @@ -128,9 +129,9 @@ do_test capi3-3.3 { do_test capi3-3.4 { sqlite3_errmsg $db2 } {unable to open database file} -do_test capi3-3.4 { +do_test capi3-3.5 { sqlite3_close $db2 -} {} +} {SQLITE_OK} # rename sqlite3_open "" # rename sqlite3_open_old sqlite3_open @@ -142,7 +143,7 @@ do_test capi3-4.1 { # FIX ME: Should test the db handle works. do_test capi3-4.2 { sqlite3_close $db2 -} {} +} {SQLITE_OK} do_test capi3-4.3 { catch { set db2 [sqlite3_open16 [utf16 /bogus/path/test.db] {}] @@ -154,7 +155,7 @@ do_test capi3-4.4 { } {unable to open database file} do_test capi3-4.5 { sqlite3_close $db2 -} {} +} {SQLITE_OK} # This proc is used to test the following API calls: # @@ -375,6 +376,26 @@ do_test capi3-5.12 { db close +do_test capi3-6.0 { + set DB [sqlite3_open test.db] + set sql {SELECT a FROM t1 order by rowid} + set STMT [sqlite3_prepare $DB $sql -1 TAIL] + expr 0 +} {0} +do_test capi3-6.1 { + sqlite3_close $DB +} {SQLITE_BUSY} +do_test capi3-6.2 { + sqlite3_step $STMT +} {SQLITE_ROW} +check_data $STMT capi3-6.3 {INTEGER} {1} {1.0} {1} +do_test capi3-6.3 { + sqlite3_finalize $STMT +} {SQLITE_OK} +do_test capi3-6.4 { + sqlite3_close $DB +} {SQLITE_OK} + finish_test diff --git a/test/tableapi.test b/test/tableapi.test index 1dc38b3324..02f09476b2 100644 --- a/test/tableapi.test +++ b/test/tableapi.test @@ -12,7 +12,7 @@ # focus of this file is testing the sqlite_exec_printf() and # sqlite_get_table_printf() APIs. # -# $Id: tableapi.test,v 1.8 2004/06/14 23:46:48 danielk1977 Exp $ +# $Id: tableapi.test,v 1.9 2004/06/19 03:33:57 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -196,6 +196,6 @@ do_test tableapi-5.2 { do_test tableapi-99.0 { sqlite3_close $::dbx -} {} +} {SQLITE_OK} finish_test