1076b9443SCy Schubert #ifdef USE_SYSTEM_SQLITE 2076b9443SCy Schubert # include <sqlite3.h> 3076b9443SCy Schubert #else 4076b9443SCy Schubert #include "sqlite3.c" 5076b9443SCy Schubert #endif 6076b9443SCy Schubert /* 7076b9443SCy Schubert ** 2001 September 15 8076b9443SCy Schubert ** 9076b9443SCy Schubert ** The author disclaims copyright to this source code. In place of 10076b9443SCy Schubert ** a legal notice, here is a blessing: 11076b9443SCy Schubert ** 12076b9443SCy Schubert ** May you do good and not evil. 13076b9443SCy Schubert ** May you find forgiveness for yourself and forgive others. 14076b9443SCy Schubert ** May you share freely, never taking more than you give. 15076b9443SCy Schubert ** 16076b9443SCy Schubert ************************************************************************* 17076b9443SCy Schubert ** A TCL Interface to SQLite. Append this file to sqlite3.c and 18076b9443SCy Schubert ** compile the whole thing to build a TCL-enabled version of SQLite. 19076b9443SCy Schubert ** 20076b9443SCy Schubert ** Compile-time options: 21076b9443SCy Schubert ** 22076b9443SCy Schubert ** -DTCLSH Add a "main()" routine that works as a tclsh. 23076b9443SCy Schubert ** 24076b9443SCy Schubert ** -DTCLSH_INIT_PROC=name 25076b9443SCy Schubert ** 26076b9443SCy Schubert ** Invoke name(interp) to initialize the Tcl interpreter. 27076b9443SCy Schubert ** If name(interp) returns a non-NULL string, then run 28076b9443SCy Schubert ** that string as a Tcl script to launch the application. 29076b9443SCy Schubert ** If name(interp) returns NULL, then run the regular 30076b9443SCy Schubert ** tclsh-emulator code. 31076b9443SCy Schubert */ 32076b9443SCy Schubert #ifdef TCLSH_INIT_PROC 33076b9443SCy Schubert # define TCLSH 1 34076b9443SCy Schubert #endif 35076b9443SCy Schubert 36076b9443SCy Schubert /* 37076b9443SCy Schubert ** If requested, include the SQLite compiler options file for MSVC. 38076b9443SCy Schubert */ 39076b9443SCy Schubert #if defined(INCLUDE_MSVC_H) 40076b9443SCy Schubert # include "msvc.h" 41076b9443SCy Schubert #endif 42076b9443SCy Schubert 43076b9443SCy Schubert #if defined(INCLUDE_SQLITE_TCL_H) 44076b9443SCy Schubert # include "sqlite_tcl.h" 45076b9443SCy Schubert #else 46076b9443SCy Schubert # include "tcl.h" 47076b9443SCy Schubert # ifndef SQLITE_TCLAPI 48076b9443SCy Schubert # define SQLITE_TCLAPI 49076b9443SCy Schubert # endif 50076b9443SCy Schubert #endif 51076b9443SCy Schubert #include <errno.h> 52076b9443SCy Schubert 53076b9443SCy Schubert /* 54076b9443SCy Schubert ** Some additional include files are needed if this file is not 55076b9443SCy Schubert ** appended to the amalgamation. 56076b9443SCy Schubert */ 57076b9443SCy Schubert #ifndef SQLITE_AMALGAMATION 58076b9443SCy Schubert # include "sqlite3.h" 59076b9443SCy Schubert # include <stdlib.h> 60076b9443SCy Schubert # include <string.h> 61076b9443SCy Schubert # include <assert.h> 62076b9443SCy Schubert typedef unsigned char u8; 63076b9443SCy Schubert #endif 64076b9443SCy Schubert #include <ctype.h> 65076b9443SCy Schubert 66076b9443SCy Schubert /* Used to get the current process ID */ 67076b9443SCy Schubert #if !defined(_WIN32) 68076b9443SCy Schubert # include <signal.h> 69076b9443SCy Schubert # include <unistd.h> 70076b9443SCy Schubert # define GETPID getpid 71076b9443SCy Schubert #elif !defined(_WIN32_WCE) 72076b9443SCy Schubert # ifndef SQLITE_AMALGAMATION 73076b9443SCy Schubert # ifndef WIN32_LEAN_AND_MEAN 74076b9443SCy Schubert # define WIN32_LEAN_AND_MEAN 75076b9443SCy Schubert # endif 76076b9443SCy Schubert # include <windows.h> 77076b9443SCy Schubert # endif 78076b9443SCy Schubert # include <io.h> 79076b9443SCy Schubert # define isatty(h) _isatty(h) 80076b9443SCy Schubert # define GETPID (int)GetCurrentProcessId 81076b9443SCy Schubert #endif 82076b9443SCy Schubert 83076b9443SCy Schubert /* 84076b9443SCy Schubert * Windows needs to know which symbols to export. Unix does not. 85076b9443SCy Schubert * BUILD_sqlite should be undefined for Unix. 86076b9443SCy Schubert */ 87076b9443SCy Schubert #ifdef BUILD_sqlite 88076b9443SCy Schubert #undef TCL_STORAGE_CLASS 89076b9443SCy Schubert #define TCL_STORAGE_CLASS DLLEXPORT 90076b9443SCy Schubert #endif /* BUILD_sqlite */ 91076b9443SCy Schubert 92076b9443SCy Schubert #define NUM_PREPARED_STMTS 10 93076b9443SCy Schubert #define MAX_PREPARED_STMTS 100 94076b9443SCy Schubert 95076b9443SCy Schubert /* Forward declaration */ 96076b9443SCy Schubert typedef struct SqliteDb SqliteDb; 97076b9443SCy Schubert 98076b9443SCy Schubert /* 99076b9443SCy Schubert ** New SQL functions can be created as TCL scripts. Each such function 100076b9443SCy Schubert ** is described by an instance of the following structure. 101076b9443SCy Schubert */ 102076b9443SCy Schubert typedef struct SqlFunc SqlFunc; 103076b9443SCy Schubert struct SqlFunc { 104076b9443SCy Schubert Tcl_Interp *interp; /* The TCL interpret to execute the function */ 105076b9443SCy Schubert Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */ 106076b9443SCy Schubert SqliteDb *pDb; /* Database connection that owns this function */ 107076b9443SCy Schubert int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */ 108076b9443SCy Schubert char *zName; /* Name of this function */ 109076b9443SCy Schubert SqlFunc *pNext; /* Next function on the list of them all */ 110076b9443SCy Schubert }; 111076b9443SCy Schubert 112076b9443SCy Schubert /* 113076b9443SCy Schubert ** New collation sequences function can be created as TCL scripts. Each such 114076b9443SCy Schubert ** function is described by an instance of the following structure. 115076b9443SCy Schubert */ 116076b9443SCy Schubert typedef struct SqlCollate SqlCollate; 117076b9443SCy Schubert struct SqlCollate { 118076b9443SCy Schubert Tcl_Interp *interp; /* The TCL interpret to execute the function */ 119076b9443SCy Schubert char *zScript; /* The script to be run */ 120076b9443SCy Schubert SqlCollate *pNext; /* Next function on the list of them all */ 121076b9443SCy Schubert }; 122076b9443SCy Schubert 123076b9443SCy Schubert /* 124076b9443SCy Schubert ** Prepared statements are cached for faster execution. Each prepared 125076b9443SCy Schubert ** statement is described by an instance of the following structure. 126076b9443SCy Schubert */ 127076b9443SCy Schubert typedef struct SqlPreparedStmt SqlPreparedStmt; 128076b9443SCy Schubert struct SqlPreparedStmt { 129076b9443SCy Schubert SqlPreparedStmt *pNext; /* Next in linked list */ 130076b9443SCy Schubert SqlPreparedStmt *pPrev; /* Previous on the list */ 131076b9443SCy Schubert sqlite3_stmt *pStmt; /* The prepared statement */ 132076b9443SCy Schubert int nSql; /* chars in zSql[] */ 133076b9443SCy Schubert const char *zSql; /* Text of the SQL statement */ 134076b9443SCy Schubert int nParm; /* Size of apParm array */ 135076b9443SCy Schubert Tcl_Obj **apParm; /* Array of referenced object pointers */ 136076b9443SCy Schubert }; 137076b9443SCy Schubert 138076b9443SCy Schubert typedef struct IncrblobChannel IncrblobChannel; 139076b9443SCy Schubert 140076b9443SCy Schubert /* 141076b9443SCy Schubert ** There is one instance of this structure for each SQLite database 142076b9443SCy Schubert ** that has been opened by the SQLite TCL interface. 143076b9443SCy Schubert ** 144076b9443SCy Schubert ** If this module is built with SQLITE_TEST defined (to create the SQLite 145076b9443SCy Schubert ** testfixture executable), then it may be configured to use either 146076b9443SCy Schubert ** sqlite3_prepare_v2() or sqlite3_prepare() to prepare SQL statements. 147076b9443SCy Schubert ** If SqliteDb.bLegacyPrepare is true, sqlite3_prepare() is used. 148076b9443SCy Schubert */ 149076b9443SCy Schubert struct SqliteDb { 150076b9443SCy Schubert sqlite3 *db; /* The "real" database structure. MUST BE FIRST */ 151076b9443SCy Schubert Tcl_Interp *interp; /* The interpreter used for this database */ 152076b9443SCy Schubert char *zBusy; /* The busy callback routine */ 153076b9443SCy Schubert char *zCommit; /* The commit hook callback routine */ 154076b9443SCy Schubert char *zTrace; /* The trace callback routine */ 155076b9443SCy Schubert char *zTraceV2; /* The trace_v2 callback routine */ 156076b9443SCy Schubert char *zProfile; /* The profile callback routine */ 157076b9443SCy Schubert char *zProgress; /* The progress callback routine */ 158076b9443SCy Schubert char *zAuth; /* The authorization callback routine */ 159076b9443SCy Schubert int disableAuth; /* Disable the authorizer if it exists */ 160076b9443SCy Schubert char *zNull; /* Text to substitute for an SQL NULL value */ 161076b9443SCy Schubert SqlFunc *pFunc; /* List of SQL functions */ 162076b9443SCy Schubert Tcl_Obj *pUpdateHook; /* Update hook script (if any) */ 163076b9443SCy Schubert Tcl_Obj *pPreUpdateHook; /* Pre-update hook script (if any) */ 164076b9443SCy Schubert Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */ 165076b9443SCy Schubert Tcl_Obj *pWalHook; /* WAL hook script (if any) */ 166076b9443SCy Schubert Tcl_Obj *pUnlockNotify; /* Unlock notify script (if any) */ 167076b9443SCy Schubert SqlCollate *pCollate; /* List of SQL collation functions */ 168076b9443SCy Schubert int rc; /* Return code of most recent sqlite3_exec() */ 169076b9443SCy Schubert Tcl_Obj *pCollateNeeded; /* Collation needed script */ 170076b9443SCy Schubert SqlPreparedStmt *stmtList; /* List of prepared statements*/ 171076b9443SCy Schubert SqlPreparedStmt *stmtLast; /* Last statement in the list */ 172076b9443SCy Schubert int maxStmt; /* The next maximum number of stmtList */ 173076b9443SCy Schubert int nStmt; /* Number of statements in stmtList */ 174076b9443SCy Schubert IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */ 175076b9443SCy Schubert int nStep, nSort, nIndex; /* Statistics for most recent operation */ 176076b9443SCy Schubert int nVMStep; /* Another statistic for most recent operation */ 177076b9443SCy Schubert int nTransaction; /* Number of nested [transaction] methods */ 178076b9443SCy Schubert int openFlags; /* Flags used to open. (SQLITE_OPEN_URI) */ 179076b9443SCy Schubert #ifdef SQLITE_TEST 180076b9443SCy Schubert int bLegacyPrepare; /* True to use sqlite3_prepare() */ 181076b9443SCy Schubert #endif 182076b9443SCy Schubert }; 183076b9443SCy Schubert 184076b9443SCy Schubert struct IncrblobChannel { 185076b9443SCy Schubert sqlite3_blob *pBlob; /* sqlite3 blob handle */ 186076b9443SCy Schubert SqliteDb *pDb; /* Associated database connection */ 187076b9443SCy Schubert int iSeek; /* Current seek offset */ 188076b9443SCy Schubert Tcl_Channel channel; /* Channel identifier */ 189076b9443SCy Schubert IncrblobChannel *pNext; /* Linked list of all open incrblob channels */ 190076b9443SCy Schubert IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */ 191076b9443SCy Schubert }; 192076b9443SCy Schubert 193076b9443SCy Schubert /* 194076b9443SCy Schubert ** Compute a string length that is limited to what can be stored in 195076b9443SCy Schubert ** lower 30 bits of a 32-bit signed integer. 196076b9443SCy Schubert */ 197076b9443SCy Schubert static int strlen30(const char *z){ 198076b9443SCy Schubert const char *z2 = z; 199076b9443SCy Schubert while( *z2 ){ z2++; } 200076b9443SCy Schubert return 0x3fffffff & (int)(z2 - z); 201076b9443SCy Schubert } 202076b9443SCy Schubert 203076b9443SCy Schubert 204076b9443SCy Schubert #ifndef SQLITE_OMIT_INCRBLOB 205076b9443SCy Schubert /* 206076b9443SCy Schubert ** Close all incrblob channels opened using database connection pDb. 207076b9443SCy Schubert ** This is called when shutting down the database connection. 208076b9443SCy Schubert */ 209076b9443SCy Schubert static void closeIncrblobChannels(SqliteDb *pDb){ 210076b9443SCy Schubert IncrblobChannel *p; 211076b9443SCy Schubert IncrblobChannel *pNext; 212076b9443SCy Schubert 213076b9443SCy Schubert for(p=pDb->pIncrblob; p; p=pNext){ 214076b9443SCy Schubert pNext = p->pNext; 215076b9443SCy Schubert 216076b9443SCy Schubert /* Note: Calling unregister here call Tcl_Close on the incrblob channel, 217076b9443SCy Schubert ** which deletes the IncrblobChannel structure at *p. So do not 218076b9443SCy Schubert ** call Tcl_Free() here. 219076b9443SCy Schubert */ 220076b9443SCy Schubert Tcl_UnregisterChannel(pDb->interp, p->channel); 221076b9443SCy Schubert } 222076b9443SCy Schubert } 223076b9443SCy Schubert 224076b9443SCy Schubert /* 225076b9443SCy Schubert ** Close an incremental blob channel. 226076b9443SCy Schubert */ 227076b9443SCy Schubert static int SQLITE_TCLAPI incrblobClose( 228076b9443SCy Schubert ClientData instanceData, 229076b9443SCy Schubert Tcl_Interp *interp 230076b9443SCy Schubert ){ 231076b9443SCy Schubert IncrblobChannel *p = (IncrblobChannel *)instanceData; 232076b9443SCy Schubert int rc = sqlite3_blob_close(p->pBlob); 233076b9443SCy Schubert sqlite3 *db = p->pDb->db; 234076b9443SCy Schubert 235076b9443SCy Schubert /* Remove the channel from the SqliteDb.pIncrblob list. */ 236076b9443SCy Schubert if( p->pNext ){ 237076b9443SCy Schubert p->pNext->pPrev = p->pPrev; 238076b9443SCy Schubert } 239076b9443SCy Schubert if( p->pPrev ){ 240076b9443SCy Schubert p->pPrev->pNext = p->pNext; 241076b9443SCy Schubert } 242076b9443SCy Schubert if( p->pDb->pIncrblob==p ){ 243076b9443SCy Schubert p->pDb->pIncrblob = p->pNext; 244076b9443SCy Schubert } 245076b9443SCy Schubert 246076b9443SCy Schubert /* Free the IncrblobChannel structure */ 247076b9443SCy Schubert Tcl_Free((char *)p); 248076b9443SCy Schubert 249076b9443SCy Schubert if( rc!=SQLITE_OK ){ 250076b9443SCy Schubert Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); 251076b9443SCy Schubert return TCL_ERROR; 252076b9443SCy Schubert } 253076b9443SCy Schubert return TCL_OK; 254076b9443SCy Schubert } 255076b9443SCy Schubert 256076b9443SCy Schubert /* 257076b9443SCy Schubert ** Read data from an incremental blob channel. 258076b9443SCy Schubert */ 259076b9443SCy Schubert static int SQLITE_TCLAPI incrblobInput( 260076b9443SCy Schubert ClientData instanceData, 261076b9443SCy Schubert char *buf, 262076b9443SCy Schubert int bufSize, 263076b9443SCy Schubert int *errorCodePtr 264076b9443SCy Schubert ){ 265076b9443SCy Schubert IncrblobChannel *p = (IncrblobChannel *)instanceData; 266076b9443SCy Schubert int nRead = bufSize; /* Number of bytes to read */ 267076b9443SCy Schubert int nBlob; /* Total size of the blob */ 268076b9443SCy Schubert int rc; /* sqlite error code */ 269076b9443SCy Schubert 270076b9443SCy Schubert nBlob = sqlite3_blob_bytes(p->pBlob); 271076b9443SCy Schubert if( (p->iSeek+nRead)>nBlob ){ 272076b9443SCy Schubert nRead = nBlob-p->iSeek; 273076b9443SCy Schubert } 274076b9443SCy Schubert if( nRead<=0 ){ 275076b9443SCy Schubert return 0; 276076b9443SCy Schubert } 277076b9443SCy Schubert 278076b9443SCy Schubert rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek); 279076b9443SCy Schubert if( rc!=SQLITE_OK ){ 280076b9443SCy Schubert *errorCodePtr = rc; 281076b9443SCy Schubert return -1; 282076b9443SCy Schubert } 283076b9443SCy Schubert 284076b9443SCy Schubert p->iSeek += nRead; 285076b9443SCy Schubert return nRead; 286076b9443SCy Schubert } 287076b9443SCy Schubert 288076b9443SCy Schubert /* 289076b9443SCy Schubert ** Write data to an incremental blob channel. 290076b9443SCy Schubert */ 291076b9443SCy Schubert static int SQLITE_TCLAPI incrblobOutput( 292076b9443SCy Schubert ClientData instanceData, 293076b9443SCy Schubert CONST char *buf, 294076b9443SCy Schubert int toWrite, 295076b9443SCy Schubert int *errorCodePtr 296076b9443SCy Schubert ){ 297076b9443SCy Schubert IncrblobChannel *p = (IncrblobChannel *)instanceData; 298076b9443SCy Schubert int nWrite = toWrite; /* Number of bytes to write */ 299076b9443SCy Schubert int nBlob; /* Total size of the blob */ 300076b9443SCy Schubert int rc; /* sqlite error code */ 301076b9443SCy Schubert 302076b9443SCy Schubert nBlob = sqlite3_blob_bytes(p->pBlob); 303076b9443SCy Schubert if( (p->iSeek+nWrite)>nBlob ){ 304076b9443SCy Schubert *errorCodePtr = EINVAL; 305076b9443SCy Schubert return -1; 306076b9443SCy Schubert } 307076b9443SCy Schubert if( nWrite<=0 ){ 308076b9443SCy Schubert return 0; 309076b9443SCy Schubert } 310076b9443SCy Schubert 311076b9443SCy Schubert rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek); 312076b9443SCy Schubert if( rc!=SQLITE_OK ){ 313076b9443SCy Schubert *errorCodePtr = EIO; 314076b9443SCy Schubert return -1; 315076b9443SCy Schubert } 316076b9443SCy Schubert 317076b9443SCy Schubert p->iSeek += nWrite; 318076b9443SCy Schubert return nWrite; 319076b9443SCy Schubert } 320076b9443SCy Schubert 321076b9443SCy Schubert /* 322076b9443SCy Schubert ** Seek an incremental blob channel. 323076b9443SCy Schubert */ 324076b9443SCy Schubert static int SQLITE_TCLAPI incrblobSeek( 325076b9443SCy Schubert ClientData instanceData, 326076b9443SCy Schubert long offset, 327076b9443SCy Schubert int seekMode, 328076b9443SCy Schubert int *errorCodePtr 329076b9443SCy Schubert ){ 330076b9443SCy Schubert IncrblobChannel *p = (IncrblobChannel *)instanceData; 331076b9443SCy Schubert 332076b9443SCy Schubert switch( seekMode ){ 333076b9443SCy Schubert case SEEK_SET: 334076b9443SCy Schubert p->iSeek = offset; 335076b9443SCy Schubert break; 336076b9443SCy Schubert case SEEK_CUR: 337076b9443SCy Schubert p->iSeek += offset; 338076b9443SCy Schubert break; 339076b9443SCy Schubert case SEEK_END: 340076b9443SCy Schubert p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset; 341076b9443SCy Schubert break; 342076b9443SCy Schubert 343076b9443SCy Schubert default: assert(!"Bad seekMode"); 344076b9443SCy Schubert } 345076b9443SCy Schubert 346076b9443SCy Schubert return p->iSeek; 347076b9443SCy Schubert } 348076b9443SCy Schubert 349076b9443SCy Schubert 350076b9443SCy Schubert static void SQLITE_TCLAPI incrblobWatch( 351076b9443SCy Schubert ClientData instanceData, 352076b9443SCy Schubert int mode 353076b9443SCy Schubert ){ 354076b9443SCy Schubert /* NO-OP */ 355076b9443SCy Schubert } 356076b9443SCy Schubert static int SQLITE_TCLAPI incrblobHandle( 357076b9443SCy Schubert ClientData instanceData, 358076b9443SCy Schubert int dir, 359076b9443SCy Schubert ClientData *hPtr 360076b9443SCy Schubert ){ 361076b9443SCy Schubert return TCL_ERROR; 362076b9443SCy Schubert } 363076b9443SCy Schubert 364076b9443SCy Schubert static Tcl_ChannelType IncrblobChannelType = { 365076b9443SCy Schubert "incrblob", /* typeName */ 366076b9443SCy Schubert TCL_CHANNEL_VERSION_2, /* version */ 367076b9443SCy Schubert incrblobClose, /* closeProc */ 368076b9443SCy Schubert incrblobInput, /* inputProc */ 369076b9443SCy Schubert incrblobOutput, /* outputProc */ 370076b9443SCy Schubert incrblobSeek, /* seekProc */ 371076b9443SCy Schubert 0, /* setOptionProc */ 372076b9443SCy Schubert 0, /* getOptionProc */ 373076b9443SCy Schubert incrblobWatch, /* watchProc (this is a no-op) */ 374076b9443SCy Schubert incrblobHandle, /* getHandleProc (always returns error) */ 375076b9443SCy Schubert 0, /* close2Proc */ 376076b9443SCy Schubert 0, /* blockModeProc */ 377076b9443SCy Schubert 0, /* flushProc */ 378076b9443SCy Schubert 0, /* handlerProc */ 379076b9443SCy Schubert 0, /* wideSeekProc */ 380076b9443SCy Schubert }; 381076b9443SCy Schubert 382076b9443SCy Schubert /* 383076b9443SCy Schubert ** Create a new incrblob channel. 384076b9443SCy Schubert */ 385076b9443SCy Schubert static int createIncrblobChannel( 386076b9443SCy Schubert Tcl_Interp *interp, 387076b9443SCy Schubert SqliteDb *pDb, 388076b9443SCy Schubert const char *zDb, 389076b9443SCy Schubert const char *zTable, 390076b9443SCy Schubert const char *zColumn, 391076b9443SCy Schubert sqlite_int64 iRow, 392076b9443SCy Schubert int isReadonly 393076b9443SCy Schubert ){ 394076b9443SCy Schubert IncrblobChannel *p; 395076b9443SCy Schubert sqlite3 *db = pDb->db; 396076b9443SCy Schubert sqlite3_blob *pBlob; 397076b9443SCy Schubert int rc; 398076b9443SCy Schubert int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE); 399076b9443SCy Schubert 400076b9443SCy Schubert /* This variable is used to name the channels: "incrblob_[incr count]" */ 401076b9443SCy Schubert static int count = 0; 402076b9443SCy Schubert char zChannel[64]; 403076b9443SCy Schubert 404076b9443SCy Schubert rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob); 405076b9443SCy Schubert if( rc!=SQLITE_OK ){ 406076b9443SCy Schubert Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 407076b9443SCy Schubert return TCL_ERROR; 408076b9443SCy Schubert } 409076b9443SCy Schubert 410076b9443SCy Schubert p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel)); 411076b9443SCy Schubert p->iSeek = 0; 412076b9443SCy Schubert p->pBlob = pBlob; 413076b9443SCy Schubert 414076b9443SCy Schubert sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count); 415076b9443SCy Schubert p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags); 416076b9443SCy Schubert Tcl_RegisterChannel(interp, p->channel); 417076b9443SCy Schubert 418076b9443SCy Schubert /* Link the new channel into the SqliteDb.pIncrblob list. */ 419076b9443SCy Schubert p->pNext = pDb->pIncrblob; 420076b9443SCy Schubert p->pPrev = 0; 421076b9443SCy Schubert if( p->pNext ){ 422076b9443SCy Schubert p->pNext->pPrev = p; 423076b9443SCy Schubert } 424076b9443SCy Schubert pDb->pIncrblob = p; 425076b9443SCy Schubert p->pDb = pDb; 426076b9443SCy Schubert 427076b9443SCy Schubert Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE); 428076b9443SCy Schubert return TCL_OK; 429076b9443SCy Schubert } 430076b9443SCy Schubert #else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */ 431076b9443SCy Schubert #define closeIncrblobChannels(pDb) 432076b9443SCy Schubert #endif 433076b9443SCy Schubert 434076b9443SCy Schubert /* 435076b9443SCy Schubert ** Look at the script prefix in pCmd. We will be executing this script 436076b9443SCy Schubert ** after first appending one or more arguments. This routine analyzes 437076b9443SCy Schubert ** the script to see if it is safe to use Tcl_EvalObjv() on the script 438076b9443SCy Schubert ** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much 439076b9443SCy Schubert ** faster. 440076b9443SCy Schubert ** 441076b9443SCy Schubert ** Scripts that are safe to use with Tcl_EvalObjv() consists of a 442076b9443SCy Schubert ** command name followed by zero or more arguments with no [...] or $ 443076b9443SCy Schubert ** or {...} or ; to be seen anywhere. Most callback scripts consist 444076b9443SCy Schubert ** of just a single procedure name and they meet this requirement. 445076b9443SCy Schubert */ 446076b9443SCy Schubert static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){ 447076b9443SCy Schubert /* We could try to do something with Tcl_Parse(). But we will instead 448076b9443SCy Schubert ** just do a search for forbidden characters. If any of the forbidden 449076b9443SCy Schubert ** characters appear in pCmd, we will report the string as unsafe. 450076b9443SCy Schubert */ 451076b9443SCy Schubert const char *z; 452076b9443SCy Schubert int n; 453076b9443SCy Schubert z = Tcl_GetStringFromObj(pCmd, &n); 454076b9443SCy Schubert while( n-- > 0 ){ 455076b9443SCy Schubert int c = *(z++); 456076b9443SCy Schubert if( c=='$' || c=='[' || c==';' ) return 0; 457076b9443SCy Schubert } 458076b9443SCy Schubert return 1; 459076b9443SCy Schubert } 460076b9443SCy Schubert 461076b9443SCy Schubert /* 462076b9443SCy Schubert ** Find an SqlFunc structure with the given name. Or create a new 463076b9443SCy Schubert ** one if an existing one cannot be found. Return a pointer to the 464076b9443SCy Schubert ** structure. 465076b9443SCy Schubert */ 466076b9443SCy Schubert static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){ 467076b9443SCy Schubert SqlFunc *p, *pNew; 468076b9443SCy Schubert int nName = strlen30(zName); 469076b9443SCy Schubert pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + nName + 1 ); 470076b9443SCy Schubert pNew->zName = (char*)&pNew[1]; 471076b9443SCy Schubert memcpy(pNew->zName, zName, nName+1); 472076b9443SCy Schubert for(p=pDb->pFunc; p; p=p->pNext){ 473076b9443SCy Schubert if( sqlite3_stricmp(p->zName, pNew->zName)==0 ){ 474076b9443SCy Schubert Tcl_Free((char*)pNew); 475076b9443SCy Schubert return p; 476076b9443SCy Schubert } 477076b9443SCy Schubert } 478076b9443SCy Schubert pNew->interp = pDb->interp; 479076b9443SCy Schubert pNew->pDb = pDb; 480076b9443SCy Schubert pNew->pScript = 0; 481076b9443SCy Schubert pNew->pNext = pDb->pFunc; 482076b9443SCy Schubert pDb->pFunc = pNew; 483076b9443SCy Schubert return pNew; 484076b9443SCy Schubert } 485076b9443SCy Schubert 486076b9443SCy Schubert /* 487076b9443SCy Schubert ** Free a single SqlPreparedStmt object. 488076b9443SCy Schubert */ 489076b9443SCy Schubert static void dbFreeStmt(SqlPreparedStmt *pStmt){ 490076b9443SCy Schubert #ifdef SQLITE_TEST 491076b9443SCy Schubert if( sqlite3_sql(pStmt->pStmt)==0 ){ 492076b9443SCy Schubert Tcl_Free((char *)pStmt->zSql); 493076b9443SCy Schubert } 494076b9443SCy Schubert #endif 495076b9443SCy Schubert sqlite3_finalize(pStmt->pStmt); 496076b9443SCy Schubert Tcl_Free((char *)pStmt); 497076b9443SCy Schubert } 498076b9443SCy Schubert 499076b9443SCy Schubert /* 500076b9443SCy Schubert ** Finalize and free a list of prepared statements 501076b9443SCy Schubert */ 502076b9443SCy Schubert static void flushStmtCache(SqliteDb *pDb){ 503076b9443SCy Schubert SqlPreparedStmt *pPreStmt; 504076b9443SCy Schubert SqlPreparedStmt *pNext; 505076b9443SCy Schubert 506076b9443SCy Schubert for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pNext){ 507076b9443SCy Schubert pNext = pPreStmt->pNext; 508076b9443SCy Schubert dbFreeStmt(pPreStmt); 509076b9443SCy Schubert } 510076b9443SCy Schubert pDb->nStmt = 0; 511076b9443SCy Schubert pDb->stmtLast = 0; 512076b9443SCy Schubert pDb->stmtList = 0; 513076b9443SCy Schubert } 514076b9443SCy Schubert 515076b9443SCy Schubert /* 516076b9443SCy Schubert ** TCL calls this procedure when an sqlite3 database command is 517076b9443SCy Schubert ** deleted. 518076b9443SCy Schubert */ 519076b9443SCy Schubert static void SQLITE_TCLAPI DbDeleteCmd(void *db){ 520076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)db; 521076b9443SCy Schubert flushStmtCache(pDb); 522076b9443SCy Schubert closeIncrblobChannels(pDb); 523076b9443SCy Schubert sqlite3_close(pDb->db); 524076b9443SCy Schubert while( pDb->pFunc ){ 525076b9443SCy Schubert SqlFunc *pFunc = pDb->pFunc; 526076b9443SCy Schubert pDb->pFunc = pFunc->pNext; 527076b9443SCy Schubert assert( pFunc->pDb==pDb ); 528076b9443SCy Schubert Tcl_DecrRefCount(pFunc->pScript); 529076b9443SCy Schubert Tcl_Free((char*)pFunc); 530076b9443SCy Schubert } 531076b9443SCy Schubert while( pDb->pCollate ){ 532076b9443SCy Schubert SqlCollate *pCollate = pDb->pCollate; 533076b9443SCy Schubert pDb->pCollate = pCollate->pNext; 534076b9443SCy Schubert Tcl_Free((char*)pCollate); 535076b9443SCy Schubert } 536076b9443SCy Schubert if( pDb->zBusy ){ 537076b9443SCy Schubert Tcl_Free(pDb->zBusy); 538076b9443SCy Schubert } 539076b9443SCy Schubert if( pDb->zTrace ){ 540076b9443SCy Schubert Tcl_Free(pDb->zTrace); 541076b9443SCy Schubert } 542076b9443SCy Schubert if( pDb->zTraceV2 ){ 543076b9443SCy Schubert Tcl_Free(pDb->zTraceV2); 544076b9443SCy Schubert } 545076b9443SCy Schubert if( pDb->zProfile ){ 546076b9443SCy Schubert Tcl_Free(pDb->zProfile); 547076b9443SCy Schubert } 548076b9443SCy Schubert if( pDb->zAuth ){ 549076b9443SCy Schubert Tcl_Free(pDb->zAuth); 550076b9443SCy Schubert } 551076b9443SCy Schubert if( pDb->zNull ){ 552076b9443SCy Schubert Tcl_Free(pDb->zNull); 553076b9443SCy Schubert } 554076b9443SCy Schubert if( pDb->pUpdateHook ){ 555076b9443SCy Schubert Tcl_DecrRefCount(pDb->pUpdateHook); 556076b9443SCy Schubert } 557076b9443SCy Schubert if( pDb->pPreUpdateHook ){ 558076b9443SCy Schubert Tcl_DecrRefCount(pDb->pPreUpdateHook); 559076b9443SCy Schubert } 560076b9443SCy Schubert if( pDb->pRollbackHook ){ 561076b9443SCy Schubert Tcl_DecrRefCount(pDb->pRollbackHook); 562076b9443SCy Schubert } 563076b9443SCy Schubert if( pDb->pWalHook ){ 564076b9443SCy Schubert Tcl_DecrRefCount(pDb->pWalHook); 565076b9443SCy Schubert } 566076b9443SCy Schubert if( pDb->pCollateNeeded ){ 567076b9443SCy Schubert Tcl_DecrRefCount(pDb->pCollateNeeded); 568076b9443SCy Schubert } 569076b9443SCy Schubert Tcl_Free((char*)pDb); 570076b9443SCy Schubert } 571076b9443SCy Schubert 572076b9443SCy Schubert /* 573076b9443SCy Schubert ** This routine is called when a database file is locked while trying 574076b9443SCy Schubert ** to execute SQL. 575076b9443SCy Schubert */ 576076b9443SCy Schubert static int DbBusyHandler(void *cd, int nTries){ 577076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 578076b9443SCy Schubert int rc; 579076b9443SCy Schubert char zVal[30]; 580076b9443SCy Schubert 581076b9443SCy Schubert sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries); 582076b9443SCy Schubert rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0); 583076b9443SCy Schubert if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 584076b9443SCy Schubert return 0; 585076b9443SCy Schubert } 586076b9443SCy Schubert return 1; 587076b9443SCy Schubert } 588076b9443SCy Schubert 589076b9443SCy Schubert #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 590076b9443SCy Schubert /* 591076b9443SCy Schubert ** This routine is invoked as the 'progress callback' for the database. 592076b9443SCy Schubert */ 593076b9443SCy Schubert static int DbProgressHandler(void *cd){ 594076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 595076b9443SCy Schubert int rc; 596076b9443SCy Schubert 597076b9443SCy Schubert assert( pDb->zProgress ); 598076b9443SCy Schubert rc = Tcl_Eval(pDb->interp, pDb->zProgress); 599076b9443SCy Schubert if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 600076b9443SCy Schubert return 1; 601076b9443SCy Schubert } 602076b9443SCy Schubert return 0; 603076b9443SCy Schubert } 604076b9443SCy Schubert #endif 605076b9443SCy Schubert 606076b9443SCy Schubert #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ 607076b9443SCy Schubert !defined(SQLITE_OMIT_DEPRECATED) 608076b9443SCy Schubert /* 609076b9443SCy Schubert ** This routine is called by the SQLite trace handler whenever a new 610076b9443SCy Schubert ** block of SQL is executed. The TCL script in pDb->zTrace is executed. 611076b9443SCy Schubert */ 612076b9443SCy Schubert static void DbTraceHandler(void *cd, const char *zSql){ 613076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 614076b9443SCy Schubert Tcl_DString str; 615076b9443SCy Schubert 616076b9443SCy Schubert Tcl_DStringInit(&str); 617076b9443SCy Schubert Tcl_DStringAppend(&str, pDb->zTrace, -1); 618076b9443SCy Schubert Tcl_DStringAppendElement(&str, zSql); 619076b9443SCy Schubert Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 620076b9443SCy Schubert Tcl_DStringFree(&str); 621076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 622076b9443SCy Schubert } 623076b9443SCy Schubert #endif 624076b9443SCy Schubert 625076b9443SCy Schubert #ifndef SQLITE_OMIT_TRACE 626076b9443SCy Schubert /* 627076b9443SCy Schubert ** This routine is called by the SQLite trace_v2 handler whenever a new 628076b9443SCy Schubert ** supported event is generated. Unsupported event types are ignored. 629076b9443SCy Schubert ** The TCL script in pDb->zTraceV2 is executed, with the arguments for 630076b9443SCy Schubert ** the event appended to it (as list elements). 631076b9443SCy Schubert */ 632076b9443SCy Schubert static int DbTraceV2Handler( 633076b9443SCy Schubert unsigned type, /* One of the SQLITE_TRACE_* event types. */ 634076b9443SCy Schubert void *cd, /* The original context data pointer. */ 635076b9443SCy Schubert void *pd, /* Primary event data, depends on event type. */ 636076b9443SCy Schubert void *xd /* Extra event data, depends on event type. */ 637076b9443SCy Schubert ){ 638076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 639076b9443SCy Schubert Tcl_Obj *pCmd; 640076b9443SCy Schubert 641076b9443SCy Schubert switch( type ){ 642076b9443SCy Schubert case SQLITE_TRACE_STMT: { 643076b9443SCy Schubert sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; 644076b9443SCy Schubert char *zSql = (char *)xd; 645076b9443SCy Schubert 646076b9443SCy Schubert pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 647076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 648076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 649076b9443SCy Schubert Tcl_NewWideIntObj((Tcl_WideInt)pStmt)); 650076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 651076b9443SCy Schubert Tcl_NewStringObj(zSql, -1)); 652076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 653076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 654076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 655076b9443SCy Schubert break; 656076b9443SCy Schubert } 657076b9443SCy Schubert case SQLITE_TRACE_PROFILE: { 658076b9443SCy Schubert sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; 659076b9443SCy Schubert sqlite3_int64 ns = *(sqlite3_int64*)xd; 660076b9443SCy Schubert 661076b9443SCy Schubert pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 662076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 663076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 664076b9443SCy Schubert Tcl_NewWideIntObj((Tcl_WideInt)pStmt)); 665076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 666076b9443SCy Schubert Tcl_NewWideIntObj((Tcl_WideInt)ns)); 667076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 668076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 669076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 670076b9443SCy Schubert break; 671076b9443SCy Schubert } 672076b9443SCy Schubert case SQLITE_TRACE_ROW: { 673076b9443SCy Schubert sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; 674076b9443SCy Schubert 675076b9443SCy Schubert pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 676076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 677076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 678076b9443SCy Schubert Tcl_NewWideIntObj((Tcl_WideInt)pStmt)); 679076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 680076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 681076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 682076b9443SCy Schubert break; 683076b9443SCy Schubert } 684076b9443SCy Schubert case SQLITE_TRACE_CLOSE: { 685076b9443SCy Schubert sqlite3 *db = (sqlite3 *)pd; 686076b9443SCy Schubert 687076b9443SCy Schubert pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 688076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 689076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 690076b9443SCy Schubert Tcl_NewWideIntObj((Tcl_WideInt)db)); 691076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 692076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 693076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 694076b9443SCy Schubert break; 695076b9443SCy Schubert } 696076b9443SCy Schubert } 697076b9443SCy Schubert return SQLITE_OK; 698076b9443SCy Schubert } 699076b9443SCy Schubert #endif 700076b9443SCy Schubert 701076b9443SCy Schubert #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ 702076b9443SCy Schubert !defined(SQLITE_OMIT_DEPRECATED) 703076b9443SCy Schubert /* 704076b9443SCy Schubert ** This routine is called by the SQLite profile handler after a statement 705076b9443SCy Schubert ** SQL has executed. The TCL script in pDb->zProfile is evaluated. 706076b9443SCy Schubert */ 707076b9443SCy Schubert static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){ 708076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 709076b9443SCy Schubert Tcl_DString str; 710076b9443SCy Schubert char zTm[100]; 711076b9443SCy Schubert 712076b9443SCy Schubert sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm); 713076b9443SCy Schubert Tcl_DStringInit(&str); 714076b9443SCy Schubert Tcl_DStringAppend(&str, pDb->zProfile, -1); 715076b9443SCy Schubert Tcl_DStringAppendElement(&str, zSql); 716076b9443SCy Schubert Tcl_DStringAppendElement(&str, zTm); 717076b9443SCy Schubert Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 718076b9443SCy Schubert Tcl_DStringFree(&str); 719076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 720076b9443SCy Schubert } 721076b9443SCy Schubert #endif 722076b9443SCy Schubert 723076b9443SCy Schubert /* 724076b9443SCy Schubert ** This routine is called when a transaction is committed. The 725076b9443SCy Schubert ** TCL script in pDb->zCommit is executed. If it returns non-zero or 726076b9443SCy Schubert ** if it throws an exception, the transaction is rolled back instead 727076b9443SCy Schubert ** of being committed. 728076b9443SCy Schubert */ 729076b9443SCy Schubert static int DbCommitHandler(void *cd){ 730076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 731076b9443SCy Schubert int rc; 732076b9443SCy Schubert 733076b9443SCy Schubert rc = Tcl_Eval(pDb->interp, pDb->zCommit); 734076b9443SCy Schubert if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 735076b9443SCy Schubert return 1; 736076b9443SCy Schubert } 737076b9443SCy Schubert return 0; 738076b9443SCy Schubert } 739076b9443SCy Schubert 740076b9443SCy Schubert static void DbRollbackHandler(void *clientData){ 741076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)clientData; 742076b9443SCy Schubert assert(pDb->pRollbackHook); 743076b9443SCy Schubert if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){ 744076b9443SCy Schubert Tcl_BackgroundError(pDb->interp); 745076b9443SCy Schubert } 746076b9443SCy Schubert } 747076b9443SCy Schubert 748076b9443SCy Schubert /* 749076b9443SCy Schubert ** This procedure handles wal_hook callbacks. 750076b9443SCy Schubert */ 751076b9443SCy Schubert static int DbWalHandler( 752076b9443SCy Schubert void *clientData, 753076b9443SCy Schubert sqlite3 *db, 754076b9443SCy Schubert const char *zDb, 755076b9443SCy Schubert int nEntry 756076b9443SCy Schubert ){ 757076b9443SCy Schubert int ret = SQLITE_OK; 758076b9443SCy Schubert Tcl_Obj *p; 759076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)clientData; 760076b9443SCy Schubert Tcl_Interp *interp = pDb->interp; 761076b9443SCy Schubert assert(pDb->pWalHook); 762076b9443SCy Schubert 763076b9443SCy Schubert assert( db==pDb->db ); 764076b9443SCy Schubert p = Tcl_DuplicateObj(pDb->pWalHook); 765076b9443SCy Schubert Tcl_IncrRefCount(p); 766076b9443SCy Schubert Tcl_ListObjAppendElement(interp, p, Tcl_NewStringObj(zDb, -1)); 767076b9443SCy Schubert Tcl_ListObjAppendElement(interp, p, Tcl_NewIntObj(nEntry)); 768076b9443SCy Schubert if( TCL_OK!=Tcl_EvalObjEx(interp, p, 0) 769076b9443SCy Schubert || TCL_OK!=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &ret) 770076b9443SCy Schubert ){ 771076b9443SCy Schubert Tcl_BackgroundError(interp); 772076b9443SCy Schubert } 773076b9443SCy Schubert Tcl_DecrRefCount(p); 774076b9443SCy Schubert 775076b9443SCy Schubert return ret; 776076b9443SCy Schubert } 777076b9443SCy Schubert 778076b9443SCy Schubert #if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_UNLOCK_NOTIFY) 779076b9443SCy Schubert static void setTestUnlockNotifyVars(Tcl_Interp *interp, int iArg, int nArg){ 780076b9443SCy Schubert char zBuf[64]; 781076b9443SCy Schubert sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", iArg); 782076b9443SCy Schubert Tcl_SetVar(interp, "sqlite_unlock_notify_arg", zBuf, TCL_GLOBAL_ONLY); 783076b9443SCy Schubert sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nArg); 784076b9443SCy Schubert Tcl_SetVar(interp, "sqlite_unlock_notify_argcount", zBuf, TCL_GLOBAL_ONLY); 785076b9443SCy Schubert } 786076b9443SCy Schubert #else 787076b9443SCy Schubert # define setTestUnlockNotifyVars(x,y,z) 788076b9443SCy Schubert #endif 789076b9443SCy Schubert 790076b9443SCy Schubert #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 791076b9443SCy Schubert static void DbUnlockNotify(void **apArg, int nArg){ 792076b9443SCy Schubert int i; 793076b9443SCy Schubert for(i=0; i<nArg; i++){ 794076b9443SCy Schubert const int flags = (TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); 795076b9443SCy Schubert SqliteDb *pDb = (SqliteDb *)apArg[i]; 796076b9443SCy Schubert setTestUnlockNotifyVars(pDb->interp, i, nArg); 797076b9443SCy Schubert assert( pDb->pUnlockNotify); 798076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pDb->pUnlockNotify, flags); 799076b9443SCy Schubert Tcl_DecrRefCount(pDb->pUnlockNotify); 800076b9443SCy Schubert pDb->pUnlockNotify = 0; 801076b9443SCy Schubert } 802076b9443SCy Schubert } 803076b9443SCy Schubert #endif 804076b9443SCy Schubert 805076b9443SCy Schubert #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 806076b9443SCy Schubert /* 807076b9443SCy Schubert ** Pre-update hook callback. 808076b9443SCy Schubert */ 809076b9443SCy Schubert static void DbPreUpdateHandler( 810076b9443SCy Schubert void *p, 811076b9443SCy Schubert sqlite3 *db, 812076b9443SCy Schubert int op, 813076b9443SCy Schubert const char *zDb, 814076b9443SCy Schubert const char *zTbl, 815076b9443SCy Schubert sqlite_int64 iKey1, 816076b9443SCy Schubert sqlite_int64 iKey2 817076b9443SCy Schubert ){ 818076b9443SCy Schubert SqliteDb *pDb = (SqliteDb *)p; 819076b9443SCy Schubert Tcl_Obj *pCmd; 820076b9443SCy Schubert static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"}; 821076b9443SCy Schubert 822076b9443SCy Schubert assert( (SQLITE_DELETE-1)/9 == 0 ); 823076b9443SCy Schubert assert( (SQLITE_INSERT-1)/9 == 1 ); 824076b9443SCy Schubert assert( (SQLITE_UPDATE-1)/9 == 2 ); 825076b9443SCy Schubert assert( pDb->pPreUpdateHook ); 826076b9443SCy Schubert assert( db==pDb->db ); 827076b9443SCy Schubert assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); 828076b9443SCy Schubert 829076b9443SCy Schubert pCmd = Tcl_DuplicateObj(pDb->pPreUpdateHook); 830076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 831076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1)); 832076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); 833076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); 834076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey1)); 835076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey2)); 836076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 837076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 838076b9443SCy Schubert } 839076b9443SCy Schubert #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 840076b9443SCy Schubert 841076b9443SCy Schubert static void DbUpdateHandler( 842076b9443SCy Schubert void *p, 843076b9443SCy Schubert int op, 844076b9443SCy Schubert const char *zDb, 845076b9443SCy Schubert const char *zTbl, 846076b9443SCy Schubert sqlite_int64 rowid 847076b9443SCy Schubert ){ 848076b9443SCy Schubert SqliteDb *pDb = (SqliteDb *)p; 849076b9443SCy Schubert Tcl_Obj *pCmd; 850076b9443SCy Schubert static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"}; 851076b9443SCy Schubert 852076b9443SCy Schubert assert( (SQLITE_DELETE-1)/9 == 0 ); 853076b9443SCy Schubert assert( (SQLITE_INSERT-1)/9 == 1 ); 854076b9443SCy Schubert assert( (SQLITE_UPDATE-1)/9 == 2 ); 855076b9443SCy Schubert 856076b9443SCy Schubert assert( pDb->pUpdateHook ); 857076b9443SCy Schubert assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); 858076b9443SCy Schubert 859076b9443SCy Schubert pCmd = Tcl_DuplicateObj(pDb->pUpdateHook); 860076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 861076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1)); 862076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); 863076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); 864076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid)); 865076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 866076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 867076b9443SCy Schubert } 868076b9443SCy Schubert 869076b9443SCy Schubert static void tclCollateNeeded( 870076b9443SCy Schubert void *pCtx, 871076b9443SCy Schubert sqlite3 *db, 872076b9443SCy Schubert int enc, 873076b9443SCy Schubert const char *zName 874076b9443SCy Schubert ){ 875076b9443SCy Schubert SqliteDb *pDb = (SqliteDb *)pCtx; 876076b9443SCy Schubert Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded); 877076b9443SCy Schubert Tcl_IncrRefCount(pScript); 878076b9443SCy Schubert Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1)); 879076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pScript, 0); 880076b9443SCy Schubert Tcl_DecrRefCount(pScript); 881076b9443SCy Schubert } 882076b9443SCy Schubert 883076b9443SCy Schubert /* 884076b9443SCy Schubert ** This routine is called to evaluate an SQL collation function implemented 885076b9443SCy Schubert ** using TCL script. 886076b9443SCy Schubert */ 887076b9443SCy Schubert static int tclSqlCollate( 888076b9443SCy Schubert void *pCtx, 889076b9443SCy Schubert int nA, 890076b9443SCy Schubert const void *zA, 891076b9443SCy Schubert int nB, 892076b9443SCy Schubert const void *zB 893076b9443SCy Schubert ){ 894076b9443SCy Schubert SqlCollate *p = (SqlCollate *)pCtx; 895076b9443SCy Schubert Tcl_Obj *pCmd; 896076b9443SCy Schubert 897076b9443SCy Schubert pCmd = Tcl_NewStringObj(p->zScript, -1); 898076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 899076b9443SCy Schubert Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA)); 900076b9443SCy Schubert Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB)); 901076b9443SCy Schubert Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 902076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 903076b9443SCy Schubert return (atoi(Tcl_GetStringResult(p->interp))); 904076b9443SCy Schubert } 905076b9443SCy Schubert 906076b9443SCy Schubert /* 907076b9443SCy Schubert ** This routine is called to evaluate an SQL function implemented 908076b9443SCy Schubert ** using TCL script. 909076b9443SCy Schubert */ 910076b9443SCy Schubert static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ 911076b9443SCy Schubert SqlFunc *p = sqlite3_user_data(context); 912076b9443SCy Schubert Tcl_Obj *pCmd; 913076b9443SCy Schubert int i; 914076b9443SCy Schubert int rc; 915076b9443SCy Schubert 916076b9443SCy Schubert if( argc==0 ){ 917076b9443SCy Schubert /* If there are no arguments to the function, call Tcl_EvalObjEx on the 918076b9443SCy Schubert ** script object directly. This allows the TCL compiler to generate 919076b9443SCy Schubert ** bytecode for the command on the first invocation and thus make 920076b9443SCy Schubert ** subsequent invocations much faster. */ 921076b9443SCy Schubert pCmd = p->pScript; 922076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 923076b9443SCy Schubert rc = Tcl_EvalObjEx(p->interp, pCmd, 0); 924076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 925076b9443SCy Schubert }else{ 926076b9443SCy Schubert /* If there are arguments to the function, make a shallow copy of the 927076b9443SCy Schubert ** script object, lappend the arguments, then evaluate the copy. 928076b9443SCy Schubert ** 929076b9443SCy Schubert ** By "shallow" copy, we mean only the outer list Tcl_Obj is duplicated. 930076b9443SCy Schubert ** The new Tcl_Obj contains pointers to the original list elements. 931076b9443SCy Schubert ** That way, when Tcl_EvalObjv() is run and shimmers the first element 932076b9443SCy Schubert ** of the list to tclCmdNameType, that alternate representation will 933076b9443SCy Schubert ** be preserved and reused on the next invocation. 934076b9443SCy Schubert */ 935076b9443SCy Schubert Tcl_Obj **aArg; 936076b9443SCy Schubert int nArg; 937076b9443SCy Schubert if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){ 938076b9443SCy Schubert sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 939076b9443SCy Schubert return; 940076b9443SCy Schubert } 941076b9443SCy Schubert pCmd = Tcl_NewListObj(nArg, aArg); 942076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 943076b9443SCy Schubert for(i=0; i<argc; i++){ 944076b9443SCy Schubert sqlite3_value *pIn = argv[i]; 945076b9443SCy Schubert Tcl_Obj *pVal; 946076b9443SCy Schubert 947076b9443SCy Schubert /* Set pVal to contain the i'th column of this row. */ 948076b9443SCy Schubert switch( sqlite3_value_type(pIn) ){ 949076b9443SCy Schubert case SQLITE_BLOB: { 950076b9443SCy Schubert int bytes = sqlite3_value_bytes(pIn); 951076b9443SCy Schubert pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes); 952076b9443SCy Schubert break; 953076b9443SCy Schubert } 954076b9443SCy Schubert case SQLITE_INTEGER: { 955076b9443SCy Schubert sqlite_int64 v = sqlite3_value_int64(pIn); 956076b9443SCy Schubert if( v>=-2147483647 && v<=2147483647 ){ 957076b9443SCy Schubert pVal = Tcl_NewIntObj((int)v); 958076b9443SCy Schubert }else{ 959076b9443SCy Schubert pVal = Tcl_NewWideIntObj(v); 960076b9443SCy Schubert } 961076b9443SCy Schubert break; 962076b9443SCy Schubert } 963076b9443SCy Schubert case SQLITE_FLOAT: { 964076b9443SCy Schubert double r = sqlite3_value_double(pIn); 965076b9443SCy Schubert pVal = Tcl_NewDoubleObj(r); 966076b9443SCy Schubert break; 967076b9443SCy Schubert } 968076b9443SCy Schubert case SQLITE_NULL: { 969076b9443SCy Schubert pVal = Tcl_NewStringObj(p->pDb->zNull, -1); 970076b9443SCy Schubert break; 971076b9443SCy Schubert } 972076b9443SCy Schubert default: { 973076b9443SCy Schubert int bytes = sqlite3_value_bytes(pIn); 974076b9443SCy Schubert pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes); 975076b9443SCy Schubert break; 976076b9443SCy Schubert } 977076b9443SCy Schubert } 978076b9443SCy Schubert rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal); 979076b9443SCy Schubert if( rc ){ 980076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 981076b9443SCy Schubert sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 982076b9443SCy Schubert return; 983076b9443SCy Schubert } 984076b9443SCy Schubert } 985076b9443SCy Schubert if( !p->useEvalObjv ){ 986076b9443SCy Schubert /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd 987076b9443SCy Schubert ** is a list without a string representation. To prevent this from 988076b9443SCy Schubert ** happening, make sure pCmd has a valid string representation */ 989076b9443SCy Schubert Tcl_GetString(pCmd); 990076b9443SCy Schubert } 991076b9443SCy Schubert rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 992076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 993076b9443SCy Schubert } 994076b9443SCy Schubert 995076b9443SCy Schubert if( rc && rc!=TCL_RETURN ){ 996076b9443SCy Schubert sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 997076b9443SCy Schubert }else{ 998076b9443SCy Schubert Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); 999076b9443SCy Schubert int n; 1000076b9443SCy Schubert u8 *data; 1001076b9443SCy Schubert const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 1002076b9443SCy Schubert char c = zType[0]; 1003076b9443SCy Schubert if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ 1004076b9443SCy Schubert /* Only return a BLOB type if the Tcl variable is a bytearray and 1005076b9443SCy Schubert ** has no string representation. */ 1006076b9443SCy Schubert data = Tcl_GetByteArrayFromObj(pVar, &n); 1007076b9443SCy Schubert sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT); 1008076b9443SCy Schubert }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 1009076b9443SCy Schubert Tcl_GetIntFromObj(0, pVar, &n); 1010076b9443SCy Schubert sqlite3_result_int(context, n); 1011076b9443SCy Schubert }else if( c=='d' && strcmp(zType,"double")==0 ){ 1012076b9443SCy Schubert double r; 1013076b9443SCy Schubert Tcl_GetDoubleFromObj(0, pVar, &r); 1014076b9443SCy Schubert sqlite3_result_double(context, r); 1015076b9443SCy Schubert }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 1016076b9443SCy Schubert (c=='i' && strcmp(zType,"int")==0) ){ 1017076b9443SCy Schubert Tcl_WideInt v; 1018076b9443SCy Schubert Tcl_GetWideIntFromObj(0, pVar, &v); 1019076b9443SCy Schubert sqlite3_result_int64(context, v); 1020076b9443SCy Schubert }else{ 1021076b9443SCy Schubert data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 1022076b9443SCy Schubert sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT); 1023076b9443SCy Schubert } 1024076b9443SCy Schubert } 1025076b9443SCy Schubert } 1026076b9443SCy Schubert 1027076b9443SCy Schubert #ifndef SQLITE_OMIT_AUTHORIZATION 1028076b9443SCy Schubert /* 1029076b9443SCy Schubert ** This is the authentication function. It appends the authentication 1030076b9443SCy Schubert ** type code and the two arguments to zCmd[] then invokes the result 1031076b9443SCy Schubert ** on the interpreter. The reply is examined to determine if the 1032076b9443SCy Schubert ** authentication fails or succeeds. 1033076b9443SCy Schubert */ 1034076b9443SCy Schubert static int auth_callback( 1035076b9443SCy Schubert void *pArg, 1036076b9443SCy Schubert int code, 1037076b9443SCy Schubert const char *zArg1, 1038076b9443SCy Schubert const char *zArg2, 1039076b9443SCy Schubert const char *zArg3, 1040076b9443SCy Schubert const char *zArg4 1041076b9443SCy Schubert #ifdef SQLITE_USER_AUTHENTICATION 1042076b9443SCy Schubert ,const char *zArg5 1043076b9443SCy Schubert #endif 1044076b9443SCy Schubert ){ 1045076b9443SCy Schubert const char *zCode; 1046076b9443SCy Schubert Tcl_DString str; 1047076b9443SCy Schubert int rc; 1048076b9443SCy Schubert const char *zReply; 1049076b9443SCy Schubert /* EVIDENCE-OF: R-38590-62769 The first parameter to the authorizer 1050076b9443SCy Schubert ** callback is a copy of the third parameter to the 1051076b9443SCy Schubert ** sqlite3_set_authorizer() interface. 1052076b9443SCy Schubert */ 1053076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)pArg; 1054076b9443SCy Schubert if( pDb->disableAuth ) return SQLITE_OK; 1055076b9443SCy Schubert 1056076b9443SCy Schubert /* EVIDENCE-OF: R-56518-44310 The second parameter to the callback is an 1057076b9443SCy Schubert ** integer action code that specifies the particular action to be 1058076b9443SCy Schubert ** authorized. */ 1059076b9443SCy Schubert switch( code ){ 1060076b9443SCy Schubert case SQLITE_COPY : zCode="SQLITE_COPY"; break; 1061076b9443SCy Schubert case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; 1062076b9443SCy Schubert case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; 1063076b9443SCy Schubert case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; 1064076b9443SCy Schubert case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; 1065076b9443SCy Schubert case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; 1066076b9443SCy Schubert case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; 1067076b9443SCy Schubert case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; 1068076b9443SCy Schubert case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; 1069076b9443SCy Schubert case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; 1070076b9443SCy Schubert case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; 1071076b9443SCy Schubert case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; 1072076b9443SCy Schubert case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; 1073076b9443SCy Schubert case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; 1074076b9443SCy Schubert case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; 1075076b9443SCy Schubert case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; 1076076b9443SCy Schubert case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; 1077076b9443SCy Schubert case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; 1078076b9443SCy Schubert case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; 1079076b9443SCy Schubert case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; 1080076b9443SCy Schubert case SQLITE_READ : zCode="SQLITE_READ"; break; 1081076b9443SCy Schubert case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; 1082076b9443SCy Schubert case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; 1083076b9443SCy Schubert case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; 1084076b9443SCy Schubert case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; 1085076b9443SCy Schubert case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; 1086076b9443SCy Schubert case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break; 1087076b9443SCy Schubert case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break; 1088076b9443SCy Schubert case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break; 1089076b9443SCy Schubert case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break; 1090076b9443SCy Schubert case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break; 1091076b9443SCy Schubert case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break; 1092076b9443SCy Schubert case SQLITE_SAVEPOINT : zCode="SQLITE_SAVEPOINT"; break; 1093076b9443SCy Schubert case SQLITE_RECURSIVE : zCode="SQLITE_RECURSIVE"; break; 1094076b9443SCy Schubert default : zCode="????"; break; 1095076b9443SCy Schubert } 1096076b9443SCy Schubert Tcl_DStringInit(&str); 1097076b9443SCy Schubert Tcl_DStringAppend(&str, pDb->zAuth, -1); 1098076b9443SCy Schubert Tcl_DStringAppendElement(&str, zCode); 1099076b9443SCy Schubert Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); 1100076b9443SCy Schubert Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); 1101076b9443SCy Schubert Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); 1102076b9443SCy Schubert Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); 1103076b9443SCy Schubert #ifdef SQLITE_USER_AUTHENTICATION 1104076b9443SCy Schubert Tcl_DStringAppendElement(&str, zArg5 ? zArg5 : ""); 1105076b9443SCy Schubert #endif 1106076b9443SCy Schubert rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); 1107076b9443SCy Schubert Tcl_DStringFree(&str); 1108076b9443SCy Schubert zReply = rc==TCL_OK ? Tcl_GetStringResult(pDb->interp) : "SQLITE_DENY"; 1109076b9443SCy Schubert if( strcmp(zReply,"SQLITE_OK")==0 ){ 1110076b9443SCy Schubert rc = SQLITE_OK; 1111076b9443SCy Schubert }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ 1112076b9443SCy Schubert rc = SQLITE_DENY; 1113076b9443SCy Schubert }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ 1114076b9443SCy Schubert rc = SQLITE_IGNORE; 1115076b9443SCy Schubert }else{ 1116076b9443SCy Schubert rc = 999; 1117076b9443SCy Schubert } 1118076b9443SCy Schubert return rc; 1119076b9443SCy Schubert } 1120076b9443SCy Schubert #endif /* SQLITE_OMIT_AUTHORIZATION */ 1121076b9443SCy Schubert 1122076b9443SCy Schubert /* 1123076b9443SCy Schubert ** This routine reads a line of text from FILE in, stores 1124076b9443SCy Schubert ** the text in memory obtained from malloc() and returns a pointer 1125076b9443SCy Schubert ** to the text. NULL is returned at end of file, or if malloc() 1126076b9443SCy Schubert ** fails. 1127076b9443SCy Schubert ** 1128076b9443SCy Schubert ** The interface is like "readline" but no command-line editing 1129076b9443SCy Schubert ** is done. 1130076b9443SCy Schubert ** 1131076b9443SCy Schubert ** copied from shell.c from '.import' command 1132076b9443SCy Schubert */ 1133076b9443SCy Schubert static char *local_getline(char *zPrompt, FILE *in){ 1134076b9443SCy Schubert char *zLine; 1135076b9443SCy Schubert int nLine; 1136076b9443SCy Schubert int n; 1137076b9443SCy Schubert 1138076b9443SCy Schubert nLine = 100; 1139076b9443SCy Schubert zLine = malloc( nLine ); 1140076b9443SCy Schubert if( zLine==0 ) return 0; 1141076b9443SCy Schubert n = 0; 1142076b9443SCy Schubert while( 1 ){ 1143076b9443SCy Schubert if( n+100>nLine ){ 1144076b9443SCy Schubert nLine = nLine*2 + 100; 1145076b9443SCy Schubert zLine = realloc(zLine, nLine); 1146076b9443SCy Schubert if( zLine==0 ) return 0; 1147076b9443SCy Schubert } 1148076b9443SCy Schubert if( fgets(&zLine[n], nLine - n, in)==0 ){ 1149076b9443SCy Schubert if( n==0 ){ 1150076b9443SCy Schubert free(zLine); 1151076b9443SCy Schubert return 0; 1152076b9443SCy Schubert } 1153076b9443SCy Schubert zLine[n] = 0; 1154076b9443SCy Schubert break; 1155076b9443SCy Schubert } 1156076b9443SCy Schubert while( zLine[n] ){ n++; } 1157076b9443SCy Schubert if( n>0 && zLine[n-1]=='\n' ){ 1158076b9443SCy Schubert n--; 1159076b9443SCy Schubert zLine[n] = 0; 1160076b9443SCy Schubert break; 1161076b9443SCy Schubert } 1162076b9443SCy Schubert } 1163076b9443SCy Schubert zLine = realloc( zLine, n+1 ); 1164076b9443SCy Schubert return zLine; 1165076b9443SCy Schubert } 1166076b9443SCy Schubert 1167076b9443SCy Schubert 1168076b9443SCy Schubert /* 1169076b9443SCy Schubert ** This function is part of the implementation of the command: 1170076b9443SCy Schubert ** 1171076b9443SCy Schubert ** $db transaction [-deferred|-immediate|-exclusive] SCRIPT 1172076b9443SCy Schubert ** 1173076b9443SCy Schubert ** It is invoked after evaluating the script SCRIPT to commit or rollback 1174076b9443SCy Schubert ** the transaction or savepoint opened by the [transaction] command. 1175076b9443SCy Schubert */ 1176076b9443SCy Schubert static int SQLITE_TCLAPI DbTransPostCmd( 1177076b9443SCy Schubert ClientData data[], /* data[0] is the Sqlite3Db* for $db */ 1178076b9443SCy Schubert Tcl_Interp *interp, /* Tcl interpreter */ 1179076b9443SCy Schubert int result /* Result of evaluating SCRIPT */ 1180076b9443SCy Schubert ){ 1181076b9443SCy Schubert static const char *const azEnd[] = { 1182076b9443SCy Schubert "RELEASE _tcl_transaction", /* rc==TCL_ERROR, nTransaction!=0 */ 1183076b9443SCy Schubert "COMMIT", /* rc!=TCL_ERROR, nTransaction==0 */ 1184076b9443SCy Schubert "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction", 1185076b9443SCy Schubert "ROLLBACK" /* rc==TCL_ERROR, nTransaction==0 */ 1186076b9443SCy Schubert }; 1187076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)data[0]; 1188076b9443SCy Schubert int rc = result; 1189076b9443SCy Schubert const char *zEnd; 1190076b9443SCy Schubert 1191076b9443SCy Schubert pDb->nTransaction--; 1192076b9443SCy Schubert zEnd = azEnd[(rc==TCL_ERROR)*2 + (pDb->nTransaction==0)]; 1193076b9443SCy Schubert 1194076b9443SCy Schubert pDb->disableAuth++; 1195076b9443SCy Schubert if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){ 1196076b9443SCy Schubert /* This is a tricky scenario to handle. The most likely cause of an 1197076b9443SCy Schubert ** error is that the exec() above was an attempt to commit the 1198076b9443SCy Schubert ** top-level transaction that returned SQLITE_BUSY. Or, less likely, 1199076b9443SCy Schubert ** that an IO-error has occurred. In either case, throw a Tcl exception 1200076b9443SCy Schubert ** and try to rollback the transaction. 1201076b9443SCy Schubert ** 1202076b9443SCy Schubert ** But it could also be that the user executed one or more BEGIN, 1203076b9443SCy Schubert ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing 1204076b9443SCy Schubert ** this method's logic. Not clear how this would be best handled. 1205076b9443SCy Schubert */ 1206076b9443SCy Schubert if( rc!=TCL_ERROR ){ 1207076b9443SCy Schubert Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 1208076b9443SCy Schubert rc = TCL_ERROR; 1209076b9443SCy Schubert } 1210076b9443SCy Schubert sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0); 1211076b9443SCy Schubert } 1212076b9443SCy Schubert pDb->disableAuth--; 1213076b9443SCy Schubert 1214076b9443SCy Schubert return rc; 1215076b9443SCy Schubert } 1216076b9443SCy Schubert 1217076b9443SCy Schubert /* 1218076b9443SCy Schubert ** Unless SQLITE_TEST is defined, this function is a simple wrapper around 1219076b9443SCy Schubert ** sqlite3_prepare_v2(). If SQLITE_TEST is defined, then it uses either 1220076b9443SCy Schubert ** sqlite3_prepare_v2() or legacy interface sqlite3_prepare(), depending 1221076b9443SCy Schubert ** on whether or not the [db_use_legacy_prepare] command has been used to 1222076b9443SCy Schubert ** configure the connection. 1223076b9443SCy Schubert */ 1224076b9443SCy Schubert static int dbPrepare( 1225076b9443SCy Schubert SqliteDb *pDb, /* Database object */ 1226076b9443SCy Schubert const char *zSql, /* SQL to compile */ 1227076b9443SCy Schubert sqlite3_stmt **ppStmt, /* OUT: Prepared statement */ 1228076b9443SCy Schubert const char **pzOut /* OUT: Pointer to next SQL statement */ 1229076b9443SCy Schubert ){ 1230076b9443SCy Schubert unsigned int prepFlags = 0; 1231076b9443SCy Schubert #ifdef SQLITE_TEST 1232076b9443SCy Schubert if( pDb->bLegacyPrepare ){ 1233076b9443SCy Schubert return sqlite3_prepare(pDb->db, zSql, -1, ppStmt, pzOut); 1234076b9443SCy Schubert } 1235076b9443SCy Schubert #endif 1236076b9443SCy Schubert /* If the statement cache is large, use the SQLITE_PREPARE_PERSISTENT 1237076b9443SCy Schubert ** flags, which uses less lookaside memory. But if the cache is small, 1238076b9443SCy Schubert ** omit that flag to make full use of lookaside */ 1239076b9443SCy Schubert if( pDb->maxStmt>5 ) prepFlags = SQLITE_PREPARE_PERSISTENT; 1240076b9443SCy Schubert 1241076b9443SCy Schubert return sqlite3_prepare_v3(pDb->db, zSql, -1, prepFlags, ppStmt, pzOut); 1242076b9443SCy Schubert } 1243076b9443SCy Schubert 1244076b9443SCy Schubert /* 1245076b9443SCy Schubert ** Search the cache for a prepared-statement object that implements the 1246076b9443SCy Schubert ** first SQL statement in the buffer pointed to by parameter zIn. If 1247076b9443SCy Schubert ** no such prepared-statement can be found, allocate and prepare a new 1248076b9443SCy Schubert ** one. In either case, bind the current values of the relevant Tcl 1249076b9443SCy Schubert ** variables to any $var, :var or @var variables in the statement. Before 1250076b9443SCy Schubert ** returning, set *ppPreStmt to point to the prepared-statement object. 1251076b9443SCy Schubert ** 1252076b9443SCy Schubert ** Output parameter *pzOut is set to point to the next SQL statement in 1253076b9443SCy Schubert ** buffer zIn, or to the '\0' byte at the end of zIn if there is no 1254076b9443SCy Schubert ** next statement. 1255076b9443SCy Schubert ** 1256076b9443SCy Schubert ** If successful, TCL_OK is returned. Otherwise, TCL_ERROR is returned 1257076b9443SCy Schubert ** and an error message loaded into interpreter pDb->interp. 1258076b9443SCy Schubert */ 1259076b9443SCy Schubert static int dbPrepareAndBind( 1260076b9443SCy Schubert SqliteDb *pDb, /* Database object */ 1261076b9443SCy Schubert char const *zIn, /* SQL to compile */ 1262076b9443SCy Schubert char const **pzOut, /* OUT: Pointer to next SQL statement */ 1263076b9443SCy Schubert SqlPreparedStmt **ppPreStmt /* OUT: Object used to cache statement */ 1264076b9443SCy Schubert ){ 1265076b9443SCy Schubert const char *zSql = zIn; /* Pointer to first SQL statement in zIn */ 1266076b9443SCy Schubert sqlite3_stmt *pStmt = 0; /* Prepared statement object */ 1267076b9443SCy Schubert SqlPreparedStmt *pPreStmt; /* Pointer to cached statement */ 1268076b9443SCy Schubert int nSql; /* Length of zSql in bytes */ 1269076b9443SCy Schubert int nVar = 0; /* Number of variables in statement */ 1270076b9443SCy Schubert int iParm = 0; /* Next free entry in apParm */ 1271076b9443SCy Schubert char c; 1272076b9443SCy Schubert int i; 1273076b9443SCy Schubert Tcl_Interp *interp = pDb->interp; 1274076b9443SCy Schubert 1275076b9443SCy Schubert *ppPreStmt = 0; 1276076b9443SCy Schubert 1277076b9443SCy Schubert /* Trim spaces from the start of zSql and calculate the remaining length. */ 1278076b9443SCy Schubert while( (c = zSql[0])==' ' || c=='\t' || c=='\r' || c=='\n' ){ zSql++; } 1279076b9443SCy Schubert nSql = strlen30(zSql); 1280076b9443SCy Schubert 1281076b9443SCy Schubert for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){ 1282076b9443SCy Schubert int n = pPreStmt->nSql; 1283076b9443SCy Schubert if( nSql>=n 1284076b9443SCy Schubert && memcmp(pPreStmt->zSql, zSql, n)==0 1285076b9443SCy Schubert && (zSql[n]==0 || zSql[n-1]==';') 1286076b9443SCy Schubert ){ 1287076b9443SCy Schubert pStmt = pPreStmt->pStmt; 1288076b9443SCy Schubert *pzOut = &zSql[pPreStmt->nSql]; 1289076b9443SCy Schubert 1290076b9443SCy Schubert /* When a prepared statement is found, unlink it from the 1291076b9443SCy Schubert ** cache list. It will later be added back to the beginning 1292076b9443SCy Schubert ** of the cache list in order to implement LRU replacement. 1293076b9443SCy Schubert */ 1294076b9443SCy Schubert if( pPreStmt->pPrev ){ 1295076b9443SCy Schubert pPreStmt->pPrev->pNext = pPreStmt->pNext; 1296076b9443SCy Schubert }else{ 1297076b9443SCy Schubert pDb->stmtList = pPreStmt->pNext; 1298076b9443SCy Schubert } 1299076b9443SCy Schubert if( pPreStmt->pNext ){ 1300076b9443SCy Schubert pPreStmt->pNext->pPrev = pPreStmt->pPrev; 1301076b9443SCy Schubert }else{ 1302076b9443SCy Schubert pDb->stmtLast = pPreStmt->pPrev; 1303076b9443SCy Schubert } 1304076b9443SCy Schubert pDb->nStmt--; 1305076b9443SCy Schubert nVar = sqlite3_bind_parameter_count(pStmt); 1306076b9443SCy Schubert break; 1307076b9443SCy Schubert } 1308076b9443SCy Schubert } 1309076b9443SCy Schubert 1310076b9443SCy Schubert /* If no prepared statement was found. Compile the SQL text. Also allocate 1311076b9443SCy Schubert ** a new SqlPreparedStmt structure. */ 1312076b9443SCy Schubert if( pPreStmt==0 ){ 1313076b9443SCy Schubert int nByte; 1314076b9443SCy Schubert 1315076b9443SCy Schubert if( SQLITE_OK!=dbPrepare(pDb, zSql, &pStmt, pzOut) ){ 1316076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 1317076b9443SCy Schubert return TCL_ERROR; 1318076b9443SCy Schubert } 1319076b9443SCy Schubert if( pStmt==0 ){ 1320076b9443SCy Schubert if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ 1321076b9443SCy Schubert /* A compile-time error in the statement. */ 1322076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 1323076b9443SCy Schubert return TCL_ERROR; 1324076b9443SCy Schubert }else{ 1325076b9443SCy Schubert /* The statement was a no-op. Continue to the next statement 1326076b9443SCy Schubert ** in the SQL string. 1327076b9443SCy Schubert */ 1328076b9443SCy Schubert return TCL_OK; 1329076b9443SCy Schubert } 1330076b9443SCy Schubert } 1331076b9443SCy Schubert 1332076b9443SCy Schubert assert( pPreStmt==0 ); 1333076b9443SCy Schubert nVar = sqlite3_bind_parameter_count(pStmt); 1334076b9443SCy Schubert nByte = sizeof(SqlPreparedStmt) + nVar*sizeof(Tcl_Obj *); 1335076b9443SCy Schubert pPreStmt = (SqlPreparedStmt*)Tcl_Alloc(nByte); 1336076b9443SCy Schubert memset(pPreStmt, 0, nByte); 1337076b9443SCy Schubert 1338076b9443SCy Schubert pPreStmt->pStmt = pStmt; 1339076b9443SCy Schubert pPreStmt->nSql = (int)(*pzOut - zSql); 1340076b9443SCy Schubert pPreStmt->zSql = sqlite3_sql(pStmt); 1341076b9443SCy Schubert pPreStmt->apParm = (Tcl_Obj **)&pPreStmt[1]; 1342076b9443SCy Schubert #ifdef SQLITE_TEST 1343076b9443SCy Schubert if( pPreStmt->zSql==0 ){ 1344076b9443SCy Schubert char *zCopy = Tcl_Alloc(pPreStmt->nSql + 1); 1345076b9443SCy Schubert memcpy(zCopy, zSql, pPreStmt->nSql); 1346076b9443SCy Schubert zCopy[pPreStmt->nSql] = '\0'; 1347076b9443SCy Schubert pPreStmt->zSql = zCopy; 1348076b9443SCy Schubert } 1349076b9443SCy Schubert #endif 1350076b9443SCy Schubert } 1351076b9443SCy Schubert assert( pPreStmt ); 1352076b9443SCy Schubert assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql ); 1353076b9443SCy Schubert assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) ); 1354076b9443SCy Schubert 1355076b9443SCy Schubert /* Bind values to parameters that begin with $ or : */ 1356076b9443SCy Schubert for(i=1; i<=nVar; i++){ 1357076b9443SCy Schubert const char *zVar = sqlite3_bind_parameter_name(pStmt, i); 1358076b9443SCy Schubert if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){ 1359076b9443SCy Schubert Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); 1360076b9443SCy Schubert if( pVar ){ 1361076b9443SCy Schubert int n; 1362076b9443SCy Schubert u8 *data; 1363076b9443SCy Schubert const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 1364076b9443SCy Schubert c = zType[0]; 1365076b9443SCy Schubert if( zVar[0]=='@' || 1366076b9443SCy Schubert (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){ 1367076b9443SCy Schubert /* Load a BLOB type if the Tcl variable is a bytearray and 1368076b9443SCy Schubert ** it has no string representation or the host 1369076b9443SCy Schubert ** parameter name begins with "@". */ 1370076b9443SCy Schubert data = Tcl_GetByteArrayFromObj(pVar, &n); 1371076b9443SCy Schubert sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); 1372076b9443SCy Schubert Tcl_IncrRefCount(pVar); 1373076b9443SCy Schubert pPreStmt->apParm[iParm++] = pVar; 1374076b9443SCy Schubert }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 1375076b9443SCy Schubert Tcl_GetIntFromObj(interp, pVar, &n); 1376076b9443SCy Schubert sqlite3_bind_int(pStmt, i, n); 1377076b9443SCy Schubert }else if( c=='d' && strcmp(zType,"double")==0 ){ 1378076b9443SCy Schubert double r; 1379076b9443SCy Schubert Tcl_GetDoubleFromObj(interp, pVar, &r); 1380076b9443SCy Schubert sqlite3_bind_double(pStmt, i, r); 1381076b9443SCy Schubert }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 1382076b9443SCy Schubert (c=='i' && strcmp(zType,"int")==0) ){ 1383076b9443SCy Schubert Tcl_WideInt v; 1384076b9443SCy Schubert Tcl_GetWideIntFromObj(interp, pVar, &v); 1385076b9443SCy Schubert sqlite3_bind_int64(pStmt, i, v); 1386076b9443SCy Schubert }else{ 1387076b9443SCy Schubert data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 1388076b9443SCy Schubert sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); 1389076b9443SCy Schubert Tcl_IncrRefCount(pVar); 1390076b9443SCy Schubert pPreStmt->apParm[iParm++] = pVar; 1391076b9443SCy Schubert } 1392076b9443SCy Schubert }else{ 1393076b9443SCy Schubert sqlite3_bind_null(pStmt, i); 1394076b9443SCy Schubert } 1395076b9443SCy Schubert } 1396076b9443SCy Schubert } 1397076b9443SCy Schubert pPreStmt->nParm = iParm; 1398076b9443SCy Schubert *ppPreStmt = pPreStmt; 1399076b9443SCy Schubert 1400076b9443SCy Schubert return TCL_OK; 1401076b9443SCy Schubert } 1402076b9443SCy Schubert 1403076b9443SCy Schubert /* 1404076b9443SCy Schubert ** Release a statement reference obtained by calling dbPrepareAndBind(). 1405076b9443SCy Schubert ** There should be exactly one call to this function for each call to 1406076b9443SCy Schubert ** dbPrepareAndBind(). 1407076b9443SCy Schubert ** 1408076b9443SCy Schubert ** If the discard parameter is non-zero, then the statement is deleted 1409076b9443SCy Schubert ** immediately. Otherwise it is added to the LRU list and may be returned 1410076b9443SCy Schubert ** by a subsequent call to dbPrepareAndBind(). 1411076b9443SCy Schubert */ 1412076b9443SCy Schubert static void dbReleaseStmt( 1413076b9443SCy Schubert SqliteDb *pDb, /* Database handle */ 1414076b9443SCy Schubert SqlPreparedStmt *pPreStmt, /* Prepared statement handle to release */ 1415076b9443SCy Schubert int discard /* True to delete (not cache) the pPreStmt */ 1416076b9443SCy Schubert ){ 1417076b9443SCy Schubert int i; 1418076b9443SCy Schubert 1419076b9443SCy Schubert /* Free the bound string and blob parameters */ 1420076b9443SCy Schubert for(i=0; i<pPreStmt->nParm; i++){ 1421076b9443SCy Schubert Tcl_DecrRefCount(pPreStmt->apParm[i]); 1422076b9443SCy Schubert } 1423076b9443SCy Schubert pPreStmt->nParm = 0; 1424076b9443SCy Schubert 1425076b9443SCy Schubert if( pDb->maxStmt<=0 || discard ){ 1426076b9443SCy Schubert /* If the cache is turned off, deallocated the statement */ 1427076b9443SCy Schubert dbFreeStmt(pPreStmt); 1428076b9443SCy Schubert }else{ 1429076b9443SCy Schubert /* Add the prepared statement to the beginning of the cache list. */ 1430076b9443SCy Schubert pPreStmt->pNext = pDb->stmtList; 1431076b9443SCy Schubert pPreStmt->pPrev = 0; 1432076b9443SCy Schubert if( pDb->stmtList ){ 1433076b9443SCy Schubert pDb->stmtList->pPrev = pPreStmt; 1434076b9443SCy Schubert } 1435076b9443SCy Schubert pDb->stmtList = pPreStmt; 1436076b9443SCy Schubert if( pDb->stmtLast==0 ){ 1437076b9443SCy Schubert assert( pDb->nStmt==0 ); 1438076b9443SCy Schubert pDb->stmtLast = pPreStmt; 1439076b9443SCy Schubert }else{ 1440076b9443SCy Schubert assert( pDb->nStmt>0 ); 1441076b9443SCy Schubert } 1442076b9443SCy Schubert pDb->nStmt++; 1443076b9443SCy Schubert 1444076b9443SCy Schubert /* If we have too many statement in cache, remove the surplus from 1445076b9443SCy Schubert ** the end of the cache list. */ 1446076b9443SCy Schubert while( pDb->nStmt>pDb->maxStmt ){ 1447076b9443SCy Schubert SqlPreparedStmt *pLast = pDb->stmtLast; 1448076b9443SCy Schubert pDb->stmtLast = pLast->pPrev; 1449076b9443SCy Schubert pDb->stmtLast->pNext = 0; 1450076b9443SCy Schubert pDb->nStmt--; 1451076b9443SCy Schubert dbFreeStmt(pLast); 1452076b9443SCy Schubert } 1453076b9443SCy Schubert } 1454076b9443SCy Schubert } 1455076b9443SCy Schubert 1456076b9443SCy Schubert /* 1457076b9443SCy Schubert ** Structure used with dbEvalXXX() functions: 1458076b9443SCy Schubert ** 1459076b9443SCy Schubert ** dbEvalInit() 1460076b9443SCy Schubert ** dbEvalStep() 1461076b9443SCy Schubert ** dbEvalFinalize() 1462076b9443SCy Schubert ** dbEvalRowInfo() 1463076b9443SCy Schubert ** dbEvalColumnValue() 1464076b9443SCy Schubert */ 1465076b9443SCy Schubert typedef struct DbEvalContext DbEvalContext; 1466076b9443SCy Schubert struct DbEvalContext { 1467076b9443SCy Schubert SqliteDb *pDb; /* Database handle */ 1468076b9443SCy Schubert Tcl_Obj *pSql; /* Object holding string zSql */ 1469076b9443SCy Schubert const char *zSql; /* Remaining SQL to execute */ 1470076b9443SCy Schubert SqlPreparedStmt *pPreStmt; /* Current statement */ 1471076b9443SCy Schubert int nCol; /* Number of columns returned by pStmt */ 1472076b9443SCy Schubert int evalFlags; /* Flags used */ 1473076b9443SCy Schubert Tcl_Obj *pArray; /* Name of array variable */ 1474076b9443SCy Schubert Tcl_Obj **apColName; /* Array of column names */ 1475076b9443SCy Schubert }; 1476076b9443SCy Schubert 1477076b9443SCy Schubert #define SQLITE_EVAL_WITHOUTNULLS 0x00001 /* Unset array(*) for NULL */ 1478076b9443SCy Schubert 1479076b9443SCy Schubert /* 1480076b9443SCy Schubert ** Release any cache of column names currently held as part of 1481076b9443SCy Schubert ** the DbEvalContext structure passed as the first argument. 1482076b9443SCy Schubert */ 1483076b9443SCy Schubert static void dbReleaseColumnNames(DbEvalContext *p){ 1484076b9443SCy Schubert if( p->apColName ){ 1485076b9443SCy Schubert int i; 1486076b9443SCy Schubert for(i=0; i<p->nCol; i++){ 1487076b9443SCy Schubert Tcl_DecrRefCount(p->apColName[i]); 1488076b9443SCy Schubert } 1489076b9443SCy Schubert Tcl_Free((char *)p->apColName); 1490076b9443SCy Schubert p->apColName = 0; 1491076b9443SCy Schubert } 1492076b9443SCy Schubert p->nCol = 0; 1493076b9443SCy Schubert } 1494076b9443SCy Schubert 1495076b9443SCy Schubert /* 1496076b9443SCy Schubert ** Initialize a DbEvalContext structure. 1497076b9443SCy Schubert ** 1498076b9443SCy Schubert ** If pArray is not NULL, then it contains the name of a Tcl array 1499076b9443SCy Schubert ** variable. The "*" member of this array is set to a list containing 1500076b9443SCy Schubert ** the names of the columns returned by the statement as part of each 1501076b9443SCy Schubert ** call to dbEvalStep(), in order from left to right. e.g. if the names 1502076b9443SCy Schubert ** of the returned columns are a, b and c, it does the equivalent of the 1503076b9443SCy Schubert ** tcl command: 1504076b9443SCy Schubert ** 1505076b9443SCy Schubert ** set ${pArray}(*) {a b c} 1506076b9443SCy Schubert */ 1507076b9443SCy Schubert static void dbEvalInit( 1508076b9443SCy Schubert DbEvalContext *p, /* Pointer to structure to initialize */ 1509076b9443SCy Schubert SqliteDb *pDb, /* Database handle */ 1510076b9443SCy Schubert Tcl_Obj *pSql, /* Object containing SQL script */ 1511076b9443SCy Schubert Tcl_Obj *pArray, /* Name of Tcl array to set (*) element of */ 1512076b9443SCy Schubert int evalFlags /* Flags controlling evaluation */ 1513076b9443SCy Schubert ){ 1514076b9443SCy Schubert memset(p, 0, sizeof(DbEvalContext)); 1515076b9443SCy Schubert p->pDb = pDb; 1516076b9443SCy Schubert p->zSql = Tcl_GetString(pSql); 1517076b9443SCy Schubert p->pSql = pSql; 1518076b9443SCy Schubert Tcl_IncrRefCount(pSql); 1519076b9443SCy Schubert if( pArray ){ 1520076b9443SCy Schubert p->pArray = pArray; 1521076b9443SCy Schubert Tcl_IncrRefCount(pArray); 1522076b9443SCy Schubert } 1523076b9443SCy Schubert p->evalFlags = evalFlags; 1524076b9443SCy Schubert } 1525076b9443SCy Schubert 1526076b9443SCy Schubert /* 1527076b9443SCy Schubert ** Obtain information about the row that the DbEvalContext passed as the 1528076b9443SCy Schubert ** first argument currently points to. 1529076b9443SCy Schubert */ 1530076b9443SCy Schubert static void dbEvalRowInfo( 1531076b9443SCy Schubert DbEvalContext *p, /* Evaluation context */ 1532076b9443SCy Schubert int *pnCol, /* OUT: Number of column names */ 1533076b9443SCy Schubert Tcl_Obj ***papColName /* OUT: Array of column names */ 1534076b9443SCy Schubert ){ 1535076b9443SCy Schubert /* Compute column names */ 1536076b9443SCy Schubert if( 0==p->apColName ){ 1537076b9443SCy Schubert sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 1538076b9443SCy Schubert int i; /* Iterator variable */ 1539076b9443SCy Schubert int nCol; /* Number of columns returned by pStmt */ 1540076b9443SCy Schubert Tcl_Obj **apColName = 0; /* Array of column names */ 1541076b9443SCy Schubert 1542076b9443SCy Schubert p->nCol = nCol = sqlite3_column_count(pStmt); 1543076b9443SCy Schubert if( nCol>0 && (papColName || p->pArray) ){ 1544076b9443SCy Schubert apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); 1545076b9443SCy Schubert for(i=0; i<nCol; i++){ 1546076b9443SCy Schubert apColName[i] = Tcl_NewStringObj(sqlite3_column_name(pStmt,i), -1); 1547076b9443SCy Schubert Tcl_IncrRefCount(apColName[i]); 1548076b9443SCy Schubert } 1549076b9443SCy Schubert p->apColName = apColName; 1550076b9443SCy Schubert } 1551076b9443SCy Schubert 1552076b9443SCy Schubert /* If results are being stored in an array variable, then create 1553076b9443SCy Schubert ** the array(*) entry for that array 1554076b9443SCy Schubert */ 1555076b9443SCy Schubert if( p->pArray ){ 1556076b9443SCy Schubert Tcl_Interp *interp = p->pDb->interp; 1557076b9443SCy Schubert Tcl_Obj *pColList = Tcl_NewObj(); 1558076b9443SCy Schubert Tcl_Obj *pStar = Tcl_NewStringObj("*", -1); 1559076b9443SCy Schubert 1560076b9443SCy Schubert for(i=0; i<nCol; i++){ 1561076b9443SCy Schubert Tcl_ListObjAppendElement(interp, pColList, apColName[i]); 1562076b9443SCy Schubert } 1563076b9443SCy Schubert Tcl_IncrRefCount(pStar); 1564076b9443SCy Schubert Tcl_ObjSetVar2(interp, p->pArray, pStar, pColList, 0); 1565076b9443SCy Schubert Tcl_DecrRefCount(pStar); 1566076b9443SCy Schubert } 1567076b9443SCy Schubert } 1568076b9443SCy Schubert 1569076b9443SCy Schubert if( papColName ){ 1570076b9443SCy Schubert *papColName = p->apColName; 1571076b9443SCy Schubert } 1572076b9443SCy Schubert if( pnCol ){ 1573076b9443SCy Schubert *pnCol = p->nCol; 1574076b9443SCy Schubert } 1575076b9443SCy Schubert } 1576076b9443SCy Schubert 1577076b9443SCy Schubert /* 1578076b9443SCy Schubert ** Return one of TCL_OK, TCL_BREAK or TCL_ERROR. If TCL_ERROR is 1579076b9443SCy Schubert ** returned, then an error message is stored in the interpreter before 1580076b9443SCy Schubert ** returning. 1581076b9443SCy Schubert ** 1582076b9443SCy Schubert ** A return value of TCL_OK means there is a row of data available. The 1583076b9443SCy Schubert ** data may be accessed using dbEvalRowInfo() and dbEvalColumnValue(). This 1584076b9443SCy Schubert ** is analogous to a return of SQLITE_ROW from sqlite3_step(). If TCL_BREAK 1585076b9443SCy Schubert ** is returned, then the SQL script has finished executing and there are 1586076b9443SCy Schubert ** no further rows available. This is similar to SQLITE_DONE. 1587076b9443SCy Schubert */ 1588076b9443SCy Schubert static int dbEvalStep(DbEvalContext *p){ 1589076b9443SCy Schubert const char *zPrevSql = 0; /* Previous value of p->zSql */ 1590076b9443SCy Schubert 1591076b9443SCy Schubert while( p->zSql[0] || p->pPreStmt ){ 1592076b9443SCy Schubert int rc; 1593076b9443SCy Schubert if( p->pPreStmt==0 ){ 1594076b9443SCy Schubert zPrevSql = (p->zSql==zPrevSql ? 0 : p->zSql); 1595076b9443SCy Schubert rc = dbPrepareAndBind(p->pDb, p->zSql, &p->zSql, &p->pPreStmt); 1596076b9443SCy Schubert if( rc!=TCL_OK ) return rc; 1597076b9443SCy Schubert }else{ 1598076b9443SCy Schubert int rcs; 1599076b9443SCy Schubert SqliteDb *pDb = p->pDb; 1600076b9443SCy Schubert SqlPreparedStmt *pPreStmt = p->pPreStmt; 1601076b9443SCy Schubert sqlite3_stmt *pStmt = pPreStmt->pStmt; 1602076b9443SCy Schubert 1603076b9443SCy Schubert rcs = sqlite3_step(pStmt); 1604076b9443SCy Schubert if( rcs==SQLITE_ROW ){ 1605076b9443SCy Schubert return TCL_OK; 1606076b9443SCy Schubert } 1607076b9443SCy Schubert if( p->pArray ){ 1608076b9443SCy Schubert dbEvalRowInfo(p, 0, 0); 1609076b9443SCy Schubert } 1610076b9443SCy Schubert rcs = sqlite3_reset(pStmt); 1611076b9443SCy Schubert 1612076b9443SCy Schubert pDb->nStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_FULLSCAN_STEP,1); 1613076b9443SCy Schubert pDb->nSort = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_SORT,1); 1614076b9443SCy Schubert pDb->nIndex = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_AUTOINDEX,1); 1615076b9443SCy Schubert pDb->nVMStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_VM_STEP,1); 1616076b9443SCy Schubert dbReleaseColumnNames(p); 1617076b9443SCy Schubert p->pPreStmt = 0; 1618076b9443SCy Schubert 1619076b9443SCy Schubert if( rcs!=SQLITE_OK ){ 1620076b9443SCy Schubert /* If a run-time error occurs, report the error and stop reading 1621076b9443SCy Schubert ** the SQL. */ 1622076b9443SCy Schubert dbReleaseStmt(pDb, pPreStmt, 1); 1623076b9443SCy Schubert #if SQLITE_TEST 1624076b9443SCy Schubert if( p->pDb->bLegacyPrepare && rcs==SQLITE_SCHEMA && zPrevSql ){ 1625076b9443SCy Schubert /* If the runtime error was an SQLITE_SCHEMA, and the database 1626076b9443SCy Schubert ** handle is configured to use the legacy sqlite3_prepare() 1627076b9443SCy Schubert ** interface, retry prepare()/step() on the same SQL statement. 1628076b9443SCy Schubert ** This only happens once. If there is a second SQLITE_SCHEMA 1629076b9443SCy Schubert ** error, the error will be returned to the caller. */ 1630076b9443SCy Schubert p->zSql = zPrevSql; 1631076b9443SCy Schubert continue; 1632076b9443SCy Schubert } 1633076b9443SCy Schubert #endif 1634076b9443SCy Schubert Tcl_SetObjResult(pDb->interp, 1635076b9443SCy Schubert Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 1636076b9443SCy Schubert return TCL_ERROR; 1637076b9443SCy Schubert }else{ 1638076b9443SCy Schubert dbReleaseStmt(pDb, pPreStmt, 0); 1639076b9443SCy Schubert } 1640076b9443SCy Schubert } 1641076b9443SCy Schubert } 1642076b9443SCy Schubert 1643076b9443SCy Schubert /* Finished */ 1644076b9443SCy Schubert return TCL_BREAK; 1645076b9443SCy Schubert } 1646076b9443SCy Schubert 1647076b9443SCy Schubert /* 1648076b9443SCy Schubert ** Free all resources currently held by the DbEvalContext structure passed 1649076b9443SCy Schubert ** as the first argument. There should be exactly one call to this function 1650076b9443SCy Schubert ** for each call to dbEvalInit(). 1651076b9443SCy Schubert */ 1652076b9443SCy Schubert static void dbEvalFinalize(DbEvalContext *p){ 1653076b9443SCy Schubert if( p->pPreStmt ){ 1654076b9443SCy Schubert sqlite3_reset(p->pPreStmt->pStmt); 1655076b9443SCy Schubert dbReleaseStmt(p->pDb, p->pPreStmt, 0); 1656076b9443SCy Schubert p->pPreStmt = 0; 1657076b9443SCy Schubert } 1658076b9443SCy Schubert if( p->pArray ){ 1659076b9443SCy Schubert Tcl_DecrRefCount(p->pArray); 1660076b9443SCy Schubert p->pArray = 0; 1661076b9443SCy Schubert } 1662076b9443SCy Schubert Tcl_DecrRefCount(p->pSql); 1663076b9443SCy Schubert dbReleaseColumnNames(p); 1664076b9443SCy Schubert } 1665076b9443SCy Schubert 1666076b9443SCy Schubert /* 1667076b9443SCy Schubert ** Return a pointer to a Tcl_Obj structure with ref-count 0 that contains 1668076b9443SCy Schubert ** the value for the iCol'th column of the row currently pointed to by 1669076b9443SCy Schubert ** the DbEvalContext structure passed as the first argument. 1670076b9443SCy Schubert */ 1671076b9443SCy Schubert static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){ 1672076b9443SCy Schubert sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 1673076b9443SCy Schubert switch( sqlite3_column_type(pStmt, iCol) ){ 1674076b9443SCy Schubert case SQLITE_BLOB: { 1675076b9443SCy Schubert int bytes = sqlite3_column_bytes(pStmt, iCol); 1676076b9443SCy Schubert const char *zBlob = sqlite3_column_blob(pStmt, iCol); 1677076b9443SCy Schubert if( !zBlob ) bytes = 0; 1678076b9443SCy Schubert return Tcl_NewByteArrayObj((u8*)zBlob, bytes); 1679076b9443SCy Schubert } 1680076b9443SCy Schubert case SQLITE_INTEGER: { 1681076b9443SCy Schubert sqlite_int64 v = sqlite3_column_int64(pStmt, iCol); 1682076b9443SCy Schubert if( v>=-2147483647 && v<=2147483647 ){ 1683076b9443SCy Schubert return Tcl_NewIntObj((int)v); 1684076b9443SCy Schubert }else{ 1685076b9443SCy Schubert return Tcl_NewWideIntObj(v); 1686076b9443SCy Schubert } 1687076b9443SCy Schubert } 1688076b9443SCy Schubert case SQLITE_FLOAT: { 1689076b9443SCy Schubert return Tcl_NewDoubleObj(sqlite3_column_double(pStmt, iCol)); 1690076b9443SCy Schubert } 1691076b9443SCy Schubert case SQLITE_NULL: { 1692076b9443SCy Schubert return Tcl_NewStringObj(p->pDb->zNull, -1); 1693076b9443SCy Schubert } 1694076b9443SCy Schubert } 1695076b9443SCy Schubert 1696076b9443SCy Schubert return Tcl_NewStringObj((char*)sqlite3_column_text(pStmt, iCol), -1); 1697076b9443SCy Schubert } 1698076b9443SCy Schubert 1699076b9443SCy Schubert /* 1700076b9443SCy Schubert ** If using Tcl version 8.6 or greater, use the NR functions to avoid 1701076b9443SCy Schubert ** recursive evalution of scripts by the [db eval] and [db trans] 1702076b9443SCy Schubert ** commands. Even if the headers used while compiling the extension 1703076b9443SCy Schubert ** are 8.6 or newer, the code still tests the Tcl version at runtime. 1704076b9443SCy Schubert ** This allows stubs-enabled builds to be used with older Tcl libraries. 1705076b9443SCy Schubert */ 1706076b9443SCy Schubert #if TCL_MAJOR_VERSION>8 || (TCL_MAJOR_VERSION==8 && TCL_MINOR_VERSION>=6) 1707076b9443SCy Schubert # define SQLITE_TCL_NRE 1 1708076b9443SCy Schubert static int DbUseNre(void){ 1709076b9443SCy Schubert int major, minor; 1710076b9443SCy Schubert Tcl_GetVersion(&major, &minor, 0, 0); 1711076b9443SCy Schubert return( (major==8 && minor>=6) || major>8 ); 1712076b9443SCy Schubert } 1713076b9443SCy Schubert #else 1714076b9443SCy Schubert /* 1715076b9443SCy Schubert ** Compiling using headers earlier than 8.6. In this case NR cannot be 1716076b9443SCy Schubert ** used, so DbUseNre() to always return zero. Add #defines for the other 1717076b9443SCy Schubert ** Tcl_NRxxx() functions to prevent them from causing compilation errors, 1718076b9443SCy Schubert ** even though the only invocations of them are within conditional blocks 1719076b9443SCy Schubert ** of the form: 1720076b9443SCy Schubert ** 1721076b9443SCy Schubert ** if( DbUseNre() ) { ... } 1722076b9443SCy Schubert */ 1723076b9443SCy Schubert # define SQLITE_TCL_NRE 0 1724076b9443SCy Schubert # define DbUseNre() 0 1725076b9443SCy Schubert # define Tcl_NRAddCallback(a,b,c,d,e,f) (void)0 1726076b9443SCy Schubert # define Tcl_NREvalObj(a,b,c) 0 1727076b9443SCy Schubert # define Tcl_NRCreateCommand(a,b,c,d,e,f) (void)0 1728076b9443SCy Schubert #endif 1729076b9443SCy Schubert 1730076b9443SCy Schubert /* 1731076b9443SCy Schubert ** This function is part of the implementation of the command: 1732076b9443SCy Schubert ** 1733076b9443SCy Schubert ** $db eval SQL ?ARRAYNAME? SCRIPT 1734076b9443SCy Schubert */ 1735076b9443SCy Schubert static int SQLITE_TCLAPI DbEvalNextCmd( 1736076b9443SCy Schubert ClientData data[], /* data[0] is the (DbEvalContext*) */ 1737076b9443SCy Schubert Tcl_Interp *interp, /* Tcl interpreter */ 1738076b9443SCy Schubert int result /* Result so far */ 1739076b9443SCy Schubert ){ 1740076b9443SCy Schubert int rc = result; /* Return code */ 1741076b9443SCy Schubert 1742076b9443SCy Schubert /* The first element of the data[] array is a pointer to a DbEvalContext 1743076b9443SCy Schubert ** structure allocated using Tcl_Alloc(). The second element of data[] 1744076b9443SCy Schubert ** is a pointer to a Tcl_Obj containing the script to run for each row 1745076b9443SCy Schubert ** returned by the queries encapsulated in data[0]. */ 1746076b9443SCy Schubert DbEvalContext *p = (DbEvalContext *)data[0]; 1747076b9443SCy Schubert Tcl_Obj *pScript = (Tcl_Obj *)data[1]; 1748076b9443SCy Schubert Tcl_Obj *pArray = p->pArray; 1749076b9443SCy Schubert 1750076b9443SCy Schubert while( (rc==TCL_OK || rc==TCL_CONTINUE) && TCL_OK==(rc = dbEvalStep(p)) ){ 1751076b9443SCy Schubert int i; 1752076b9443SCy Schubert int nCol; 1753076b9443SCy Schubert Tcl_Obj **apColName; 1754076b9443SCy Schubert dbEvalRowInfo(p, &nCol, &apColName); 1755076b9443SCy Schubert for(i=0; i<nCol; i++){ 1756076b9443SCy Schubert if( pArray==0 ){ 1757076b9443SCy Schubert Tcl_ObjSetVar2(interp, apColName[i], 0, dbEvalColumnValue(p,i), 0); 1758076b9443SCy Schubert }else if( (p->evalFlags & SQLITE_EVAL_WITHOUTNULLS)!=0 1759076b9443SCy Schubert && sqlite3_column_type(p->pPreStmt->pStmt, i)==SQLITE_NULL 1760076b9443SCy Schubert ){ 1761076b9443SCy Schubert Tcl_UnsetVar2(interp, Tcl_GetString(pArray), 1762076b9443SCy Schubert Tcl_GetString(apColName[i]), 0); 1763076b9443SCy Schubert }else{ 1764076b9443SCy Schubert Tcl_ObjSetVar2(interp, pArray, apColName[i], dbEvalColumnValue(p,i), 0); 1765076b9443SCy Schubert } 1766076b9443SCy Schubert } 1767076b9443SCy Schubert 1768076b9443SCy Schubert /* The required interpreter variables are now populated with the data 1769076b9443SCy Schubert ** from the current row. If using NRE, schedule callbacks to evaluate 1770076b9443SCy Schubert ** script pScript, then to invoke this function again to fetch the next 1771076b9443SCy Schubert ** row (or clean up if there is no next row or the script throws an 1772076b9443SCy Schubert ** exception). After scheduling the callbacks, return control to the 1773076b9443SCy Schubert ** caller. 1774076b9443SCy Schubert ** 1775076b9443SCy Schubert ** If not using NRE, evaluate pScript directly and continue with the 1776076b9443SCy Schubert ** next iteration of this while(...) loop. */ 1777076b9443SCy Schubert if( DbUseNre() ){ 1778076b9443SCy Schubert Tcl_NRAddCallback(interp, DbEvalNextCmd, (void*)p, (void*)pScript, 0, 0); 1779076b9443SCy Schubert return Tcl_NREvalObj(interp, pScript, 0); 1780076b9443SCy Schubert }else{ 1781076b9443SCy Schubert rc = Tcl_EvalObjEx(interp, pScript, 0); 1782076b9443SCy Schubert } 1783076b9443SCy Schubert } 1784076b9443SCy Schubert 1785076b9443SCy Schubert Tcl_DecrRefCount(pScript); 1786076b9443SCy Schubert dbEvalFinalize(p); 1787076b9443SCy Schubert Tcl_Free((char *)p); 1788076b9443SCy Schubert 1789076b9443SCy Schubert if( rc==TCL_OK || rc==TCL_BREAK ){ 1790076b9443SCy Schubert Tcl_ResetResult(interp); 1791076b9443SCy Schubert rc = TCL_OK; 1792076b9443SCy Schubert } 1793076b9443SCy Schubert return rc; 1794076b9443SCy Schubert } 1795076b9443SCy Schubert 1796076b9443SCy Schubert /* 1797076b9443SCy Schubert ** This function is used by the implementations of the following database 1798076b9443SCy Schubert ** handle sub-commands: 1799076b9443SCy Schubert ** 1800076b9443SCy Schubert ** $db update_hook ?SCRIPT? 1801076b9443SCy Schubert ** $db wal_hook ?SCRIPT? 1802076b9443SCy Schubert ** $db commit_hook ?SCRIPT? 1803076b9443SCy Schubert ** $db preupdate hook ?SCRIPT? 1804076b9443SCy Schubert */ 1805076b9443SCy Schubert static void DbHookCmd( 1806076b9443SCy Schubert Tcl_Interp *interp, /* Tcl interpreter */ 1807076b9443SCy Schubert SqliteDb *pDb, /* Database handle */ 1808076b9443SCy Schubert Tcl_Obj *pArg, /* SCRIPT argument (or NULL) */ 1809076b9443SCy Schubert Tcl_Obj **ppHook /* Pointer to member of SqliteDb */ 1810076b9443SCy Schubert ){ 1811076b9443SCy Schubert sqlite3 *db = pDb->db; 1812076b9443SCy Schubert 1813076b9443SCy Schubert if( *ppHook ){ 1814076b9443SCy Schubert Tcl_SetObjResult(interp, *ppHook); 1815076b9443SCy Schubert if( pArg ){ 1816076b9443SCy Schubert Tcl_DecrRefCount(*ppHook); 1817076b9443SCy Schubert *ppHook = 0; 1818076b9443SCy Schubert } 1819076b9443SCy Schubert } 1820076b9443SCy Schubert if( pArg ){ 1821076b9443SCy Schubert assert( !(*ppHook) ); 1822076b9443SCy Schubert if( Tcl_GetCharLength(pArg)>0 ){ 1823076b9443SCy Schubert *ppHook = pArg; 1824076b9443SCy Schubert Tcl_IncrRefCount(*ppHook); 1825076b9443SCy Schubert } 1826076b9443SCy Schubert } 1827076b9443SCy Schubert 1828076b9443SCy Schubert #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 1829076b9443SCy Schubert sqlite3_preupdate_hook(db, (pDb->pPreUpdateHook?DbPreUpdateHandler:0), pDb); 1830076b9443SCy Schubert #endif 1831076b9443SCy Schubert sqlite3_update_hook(db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); 1832076b9443SCy Schubert sqlite3_rollback_hook(db, (pDb->pRollbackHook?DbRollbackHandler:0), pDb); 1833076b9443SCy Schubert sqlite3_wal_hook(db, (pDb->pWalHook?DbWalHandler:0), pDb); 1834076b9443SCy Schubert } 1835076b9443SCy Schubert 1836076b9443SCy Schubert /* 1837076b9443SCy Schubert ** The "sqlite" command below creates a new Tcl command for each 1838076b9443SCy Schubert ** connection it opens to an SQLite database. This routine is invoked 1839076b9443SCy Schubert ** whenever one of those connection-specific commands is executed 1840076b9443SCy Schubert ** in Tcl. For example, if you run Tcl code like this: 1841076b9443SCy Schubert ** 1842076b9443SCy Schubert ** sqlite3 db1 "my_database" 1843076b9443SCy Schubert ** db1 close 1844076b9443SCy Schubert ** 1845076b9443SCy Schubert ** The first command opens a connection to the "my_database" database 1846076b9443SCy Schubert ** and calls that connection "db1". The second command causes this 1847076b9443SCy Schubert ** subroutine to be invoked. 1848076b9443SCy Schubert */ 1849076b9443SCy Schubert static int SQLITE_TCLAPI DbObjCmd( 1850076b9443SCy Schubert void *cd, 1851076b9443SCy Schubert Tcl_Interp *interp, 1852076b9443SCy Schubert int objc, 1853076b9443SCy Schubert Tcl_Obj *const*objv 1854076b9443SCy Schubert ){ 1855076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 1856076b9443SCy Schubert int choice; 1857076b9443SCy Schubert int rc = TCL_OK; 1858076b9443SCy Schubert static const char *DB_strs[] = { 1859076b9443SCy Schubert "authorizer", "backup", "busy", 1860076b9443SCy Schubert "cache", "changes", "close", 1861076b9443SCy Schubert "collate", "collation_needed", "commit_hook", 1862076b9443SCy Schubert "complete", "copy", "deserialize", 1863076b9443SCy Schubert "enable_load_extension", "errorcode", "eval", 1864076b9443SCy Schubert "exists", "function", "incrblob", 1865076b9443SCy Schubert "interrupt", "last_insert_rowid", "nullvalue", 1866076b9443SCy Schubert "onecolumn", "preupdate", "profile", 1867076b9443SCy Schubert "progress", "rekey", "restore", 1868076b9443SCy Schubert "rollback_hook", "serialize", "status", 1869076b9443SCy Schubert "timeout", "total_changes", "trace", 1870076b9443SCy Schubert "trace_v2", "transaction", "unlock_notify", 1871076b9443SCy Schubert "update_hook", "version", "wal_hook", 1872076b9443SCy Schubert 0 1873076b9443SCy Schubert }; 1874076b9443SCy Schubert enum DB_enum { 1875076b9443SCy Schubert DB_AUTHORIZER, DB_BACKUP, DB_BUSY, 1876076b9443SCy Schubert DB_CACHE, DB_CHANGES, DB_CLOSE, 1877076b9443SCy Schubert DB_COLLATE, DB_COLLATION_NEEDED, DB_COMMIT_HOOK, 1878076b9443SCy Schubert DB_COMPLETE, DB_COPY, DB_DESERIALIZE, 1879076b9443SCy Schubert DB_ENABLE_LOAD_EXTENSION, DB_ERRORCODE, DB_EVAL, 1880076b9443SCy Schubert DB_EXISTS, DB_FUNCTION, DB_INCRBLOB, 1881076b9443SCy Schubert DB_INTERRUPT, DB_LAST_INSERT_ROWID, DB_NULLVALUE, 1882076b9443SCy Schubert DB_ONECOLUMN, DB_PREUPDATE, DB_PROFILE, 1883076b9443SCy Schubert DB_PROGRESS, DB_REKEY, DB_RESTORE, 1884076b9443SCy Schubert DB_ROLLBACK_HOOK, DB_SERIALIZE, DB_STATUS, 1885076b9443SCy Schubert DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE, 1886076b9443SCy Schubert DB_TRACE_V2, DB_TRANSACTION, DB_UNLOCK_NOTIFY, 1887076b9443SCy Schubert DB_UPDATE_HOOK, DB_VERSION, DB_WAL_HOOK 1888076b9443SCy Schubert }; 1889076b9443SCy Schubert /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */ 1890076b9443SCy Schubert 1891076b9443SCy Schubert if( objc<2 ){ 1892076b9443SCy Schubert Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); 1893076b9443SCy Schubert return TCL_ERROR; 1894076b9443SCy Schubert } 1895076b9443SCy Schubert if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ 1896076b9443SCy Schubert return TCL_ERROR; 1897076b9443SCy Schubert } 1898076b9443SCy Schubert 1899076b9443SCy Schubert switch( (enum DB_enum)choice ){ 1900076b9443SCy Schubert 1901076b9443SCy Schubert /* $db authorizer ?CALLBACK? 1902076b9443SCy Schubert ** 1903076b9443SCy Schubert ** Invoke the given callback to authorize each SQL operation as it is 1904076b9443SCy Schubert ** compiled. 5 arguments are appended to the callback before it is 1905076b9443SCy Schubert ** invoked: 1906076b9443SCy Schubert ** 1907076b9443SCy Schubert ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) 1908076b9443SCy Schubert ** (2) First descriptive name (depends on authorization type) 1909076b9443SCy Schubert ** (3) Second descriptive name 1910076b9443SCy Schubert ** (4) Name of the database (ex: "main", "temp") 1911076b9443SCy Schubert ** (5) Name of trigger that is doing the access 1912076b9443SCy Schubert ** 1913076b9443SCy Schubert ** The callback should return on of the following strings: SQLITE_OK, 1914076b9443SCy Schubert ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. 1915076b9443SCy Schubert ** 1916076b9443SCy Schubert ** If this method is invoked with no arguments, the current authorization 1917076b9443SCy Schubert ** callback string is returned. 1918076b9443SCy Schubert */ 1919076b9443SCy Schubert case DB_AUTHORIZER: { 1920076b9443SCy Schubert #ifdef SQLITE_OMIT_AUTHORIZATION 1921076b9443SCy Schubert Tcl_AppendResult(interp, "authorization not available in this build", 1922076b9443SCy Schubert (char*)0); 1923076b9443SCy Schubert return TCL_ERROR; 1924076b9443SCy Schubert #else 1925076b9443SCy Schubert if( objc>3 ){ 1926076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 1927076b9443SCy Schubert return TCL_ERROR; 1928076b9443SCy Schubert }else if( objc==2 ){ 1929076b9443SCy Schubert if( pDb->zAuth ){ 1930076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zAuth, (char*)0); 1931076b9443SCy Schubert } 1932076b9443SCy Schubert }else{ 1933076b9443SCy Schubert char *zAuth; 1934076b9443SCy Schubert int len; 1935076b9443SCy Schubert if( pDb->zAuth ){ 1936076b9443SCy Schubert Tcl_Free(pDb->zAuth); 1937076b9443SCy Schubert } 1938076b9443SCy Schubert zAuth = Tcl_GetStringFromObj(objv[2], &len); 1939076b9443SCy Schubert if( zAuth && len>0 ){ 1940076b9443SCy Schubert pDb->zAuth = Tcl_Alloc( len + 1 ); 1941076b9443SCy Schubert memcpy(pDb->zAuth, zAuth, len+1); 1942076b9443SCy Schubert }else{ 1943076b9443SCy Schubert pDb->zAuth = 0; 1944076b9443SCy Schubert } 1945076b9443SCy Schubert if( pDb->zAuth ){ 1946076b9443SCy Schubert typedef int (*sqlite3_auth_cb)( 1947076b9443SCy Schubert void*,int,const char*,const char*, 1948076b9443SCy Schubert const char*,const char*); 1949076b9443SCy Schubert pDb->interp = interp; 1950076b9443SCy Schubert sqlite3_set_authorizer(pDb->db,(sqlite3_auth_cb)auth_callback,pDb); 1951076b9443SCy Schubert }else{ 1952076b9443SCy Schubert sqlite3_set_authorizer(pDb->db, 0, 0); 1953076b9443SCy Schubert } 1954076b9443SCy Schubert } 1955076b9443SCy Schubert #endif 1956076b9443SCy Schubert break; 1957076b9443SCy Schubert } 1958076b9443SCy Schubert 1959076b9443SCy Schubert /* $db backup ?DATABASE? FILENAME 1960076b9443SCy Schubert ** 1961076b9443SCy Schubert ** Open or create a database file named FILENAME. Transfer the 1962076b9443SCy Schubert ** content of local database DATABASE (default: "main") into the 1963076b9443SCy Schubert ** FILENAME database. 1964076b9443SCy Schubert */ 1965076b9443SCy Schubert case DB_BACKUP: { 1966076b9443SCy Schubert const char *zDestFile; 1967076b9443SCy Schubert const char *zSrcDb; 1968076b9443SCy Schubert sqlite3 *pDest; 1969076b9443SCy Schubert sqlite3_backup *pBackup; 1970076b9443SCy Schubert 1971076b9443SCy Schubert if( objc==3 ){ 1972076b9443SCy Schubert zSrcDb = "main"; 1973076b9443SCy Schubert zDestFile = Tcl_GetString(objv[2]); 1974076b9443SCy Schubert }else if( objc==4 ){ 1975076b9443SCy Schubert zSrcDb = Tcl_GetString(objv[2]); 1976076b9443SCy Schubert zDestFile = Tcl_GetString(objv[3]); 1977076b9443SCy Schubert }else{ 1978076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 1979076b9443SCy Schubert return TCL_ERROR; 1980076b9443SCy Schubert } 1981076b9443SCy Schubert rc = sqlite3_open_v2(zDestFile, &pDest, 1982076b9443SCy Schubert SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE| pDb->openFlags, 0); 1983076b9443SCy Schubert if( rc!=SQLITE_OK ){ 1984076b9443SCy Schubert Tcl_AppendResult(interp, "cannot open target database: ", 1985076b9443SCy Schubert sqlite3_errmsg(pDest), (char*)0); 1986076b9443SCy Schubert sqlite3_close(pDest); 1987076b9443SCy Schubert return TCL_ERROR; 1988076b9443SCy Schubert } 1989076b9443SCy Schubert pBackup = sqlite3_backup_init(pDest, "main", pDb->db, zSrcDb); 1990076b9443SCy Schubert if( pBackup==0 ){ 1991076b9443SCy Schubert Tcl_AppendResult(interp, "backup failed: ", 1992076b9443SCy Schubert sqlite3_errmsg(pDest), (char*)0); 1993076b9443SCy Schubert sqlite3_close(pDest); 1994076b9443SCy Schubert return TCL_ERROR; 1995076b9443SCy Schubert } 1996076b9443SCy Schubert while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} 1997076b9443SCy Schubert sqlite3_backup_finish(pBackup); 1998076b9443SCy Schubert if( rc==SQLITE_DONE ){ 1999076b9443SCy Schubert rc = TCL_OK; 2000076b9443SCy Schubert }else{ 2001076b9443SCy Schubert Tcl_AppendResult(interp, "backup failed: ", 2002076b9443SCy Schubert sqlite3_errmsg(pDest), (char*)0); 2003076b9443SCy Schubert rc = TCL_ERROR; 2004076b9443SCy Schubert } 2005076b9443SCy Schubert sqlite3_close(pDest); 2006076b9443SCy Schubert break; 2007076b9443SCy Schubert } 2008076b9443SCy Schubert 2009076b9443SCy Schubert /* $db busy ?CALLBACK? 2010076b9443SCy Schubert ** 2011076b9443SCy Schubert ** Invoke the given callback if an SQL statement attempts to open 2012076b9443SCy Schubert ** a locked database file. 2013076b9443SCy Schubert */ 2014076b9443SCy Schubert case DB_BUSY: { 2015076b9443SCy Schubert if( objc>3 ){ 2016076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); 2017076b9443SCy Schubert return TCL_ERROR; 2018076b9443SCy Schubert }else if( objc==2 ){ 2019076b9443SCy Schubert if( pDb->zBusy ){ 2020076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zBusy, (char*)0); 2021076b9443SCy Schubert } 2022076b9443SCy Schubert }else{ 2023076b9443SCy Schubert char *zBusy; 2024076b9443SCy Schubert int len; 2025076b9443SCy Schubert if( pDb->zBusy ){ 2026076b9443SCy Schubert Tcl_Free(pDb->zBusy); 2027076b9443SCy Schubert } 2028076b9443SCy Schubert zBusy = Tcl_GetStringFromObj(objv[2], &len); 2029076b9443SCy Schubert if( zBusy && len>0 ){ 2030076b9443SCy Schubert pDb->zBusy = Tcl_Alloc( len + 1 ); 2031076b9443SCy Schubert memcpy(pDb->zBusy, zBusy, len+1); 2032076b9443SCy Schubert }else{ 2033076b9443SCy Schubert pDb->zBusy = 0; 2034076b9443SCy Schubert } 2035076b9443SCy Schubert if( pDb->zBusy ){ 2036076b9443SCy Schubert pDb->interp = interp; 2037076b9443SCy Schubert sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb); 2038076b9443SCy Schubert }else{ 2039076b9443SCy Schubert sqlite3_busy_handler(pDb->db, 0, 0); 2040076b9443SCy Schubert } 2041076b9443SCy Schubert } 2042076b9443SCy Schubert break; 2043076b9443SCy Schubert } 2044076b9443SCy Schubert 2045076b9443SCy Schubert /* $db cache flush 2046076b9443SCy Schubert ** $db cache size n 2047076b9443SCy Schubert ** 2048076b9443SCy Schubert ** Flush the prepared statement cache, or set the maximum number of 2049076b9443SCy Schubert ** cached statements. 2050076b9443SCy Schubert */ 2051076b9443SCy Schubert case DB_CACHE: { 2052076b9443SCy Schubert char *subCmd; 2053076b9443SCy Schubert int n; 2054076b9443SCy Schubert 2055076b9443SCy Schubert if( objc<=2 ){ 2056076b9443SCy Schubert Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?"); 2057076b9443SCy Schubert return TCL_ERROR; 2058076b9443SCy Schubert } 2059076b9443SCy Schubert subCmd = Tcl_GetStringFromObj( objv[2], 0 ); 2060076b9443SCy Schubert if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){ 2061076b9443SCy Schubert if( objc!=3 ){ 2062076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "flush"); 2063076b9443SCy Schubert return TCL_ERROR; 2064076b9443SCy Schubert }else{ 2065076b9443SCy Schubert flushStmtCache( pDb ); 2066076b9443SCy Schubert } 2067076b9443SCy Schubert }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){ 2068076b9443SCy Schubert if( objc!=4 ){ 2069076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "size n"); 2070076b9443SCy Schubert return TCL_ERROR; 2071076b9443SCy Schubert }else{ 2072076b9443SCy Schubert if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){ 2073076b9443SCy Schubert Tcl_AppendResult( interp, "cannot convert \"", 2074076b9443SCy Schubert Tcl_GetStringFromObj(objv[3],0), "\" to integer", (char*)0); 2075076b9443SCy Schubert return TCL_ERROR; 2076076b9443SCy Schubert }else{ 2077076b9443SCy Schubert if( n<0 ){ 2078076b9443SCy Schubert flushStmtCache( pDb ); 2079076b9443SCy Schubert n = 0; 2080076b9443SCy Schubert }else if( n>MAX_PREPARED_STMTS ){ 2081076b9443SCy Schubert n = MAX_PREPARED_STMTS; 2082076b9443SCy Schubert } 2083076b9443SCy Schubert pDb->maxStmt = n; 2084076b9443SCy Schubert } 2085076b9443SCy Schubert } 2086076b9443SCy Schubert }else{ 2087076b9443SCy Schubert Tcl_AppendResult( interp, "bad option \"", 2088076b9443SCy Schubert Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size", 2089076b9443SCy Schubert (char*)0); 2090076b9443SCy Schubert return TCL_ERROR; 2091076b9443SCy Schubert } 2092076b9443SCy Schubert break; 2093076b9443SCy Schubert } 2094076b9443SCy Schubert 2095076b9443SCy Schubert /* $db changes 2096076b9443SCy Schubert ** 2097076b9443SCy Schubert ** Return the number of rows that were modified, inserted, or deleted by 2098076b9443SCy Schubert ** the most recent INSERT, UPDATE or DELETE statement, not including 2099076b9443SCy Schubert ** any changes made by trigger programs. 2100076b9443SCy Schubert */ 2101076b9443SCy Schubert case DB_CHANGES: { 2102076b9443SCy Schubert Tcl_Obj *pResult; 2103076b9443SCy Schubert if( objc!=2 ){ 2104076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, ""); 2105076b9443SCy Schubert return TCL_ERROR; 2106076b9443SCy Schubert } 2107076b9443SCy Schubert pResult = Tcl_GetObjResult(interp); 2108076b9443SCy Schubert Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db)); 2109076b9443SCy Schubert break; 2110076b9443SCy Schubert } 2111076b9443SCy Schubert 2112076b9443SCy Schubert /* $db close 2113076b9443SCy Schubert ** 2114076b9443SCy Schubert ** Shutdown the database 2115076b9443SCy Schubert */ 2116076b9443SCy Schubert case DB_CLOSE: { 2117076b9443SCy Schubert Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); 2118076b9443SCy Schubert break; 2119076b9443SCy Schubert } 2120076b9443SCy Schubert 2121076b9443SCy Schubert /* 2122076b9443SCy Schubert ** $db collate NAME SCRIPT 2123076b9443SCy Schubert ** 2124076b9443SCy Schubert ** Create a new SQL collation function called NAME. Whenever 2125076b9443SCy Schubert ** that function is called, invoke SCRIPT to evaluate the function. 2126076b9443SCy Schubert */ 2127076b9443SCy Schubert case DB_COLLATE: { 2128076b9443SCy Schubert SqlCollate *pCollate; 2129076b9443SCy Schubert char *zName; 2130076b9443SCy Schubert char *zScript; 2131076b9443SCy Schubert int nScript; 2132076b9443SCy Schubert if( objc!=4 ){ 2133076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); 2134076b9443SCy Schubert return TCL_ERROR; 2135076b9443SCy Schubert } 2136076b9443SCy Schubert zName = Tcl_GetStringFromObj(objv[2], 0); 2137076b9443SCy Schubert zScript = Tcl_GetStringFromObj(objv[3], &nScript); 2138076b9443SCy Schubert pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 ); 2139076b9443SCy Schubert if( pCollate==0 ) return TCL_ERROR; 2140076b9443SCy Schubert pCollate->interp = interp; 2141076b9443SCy Schubert pCollate->pNext = pDb->pCollate; 2142076b9443SCy Schubert pCollate->zScript = (char*)&pCollate[1]; 2143076b9443SCy Schubert pDb->pCollate = pCollate; 2144076b9443SCy Schubert memcpy(pCollate->zScript, zScript, nScript+1); 2145076b9443SCy Schubert if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, 2146076b9443SCy Schubert pCollate, tclSqlCollate) ){ 2147076b9443SCy Schubert Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 2148076b9443SCy Schubert return TCL_ERROR; 2149076b9443SCy Schubert } 2150076b9443SCy Schubert break; 2151076b9443SCy Schubert } 2152076b9443SCy Schubert 2153076b9443SCy Schubert /* 2154076b9443SCy Schubert ** $db collation_needed SCRIPT 2155076b9443SCy Schubert ** 2156076b9443SCy Schubert ** Create a new SQL collation function called NAME. Whenever 2157076b9443SCy Schubert ** that function is called, invoke SCRIPT to evaluate the function. 2158076b9443SCy Schubert */ 2159076b9443SCy Schubert case DB_COLLATION_NEEDED: { 2160076b9443SCy Schubert if( objc!=3 ){ 2161076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT"); 2162076b9443SCy Schubert return TCL_ERROR; 2163076b9443SCy Schubert } 2164076b9443SCy Schubert if( pDb->pCollateNeeded ){ 2165076b9443SCy Schubert Tcl_DecrRefCount(pDb->pCollateNeeded); 2166076b9443SCy Schubert } 2167076b9443SCy Schubert pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]); 2168076b9443SCy Schubert Tcl_IncrRefCount(pDb->pCollateNeeded); 2169076b9443SCy Schubert sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded); 2170076b9443SCy Schubert break; 2171076b9443SCy Schubert } 2172076b9443SCy Schubert 2173076b9443SCy Schubert /* $db commit_hook ?CALLBACK? 2174076b9443SCy Schubert ** 2175076b9443SCy Schubert ** Invoke the given callback just before committing every SQL transaction. 2176076b9443SCy Schubert ** If the callback throws an exception or returns non-zero, then the 2177076b9443SCy Schubert ** transaction is aborted. If CALLBACK is an empty string, the callback 2178076b9443SCy Schubert ** is disabled. 2179076b9443SCy Schubert */ 2180076b9443SCy Schubert case DB_COMMIT_HOOK: { 2181076b9443SCy Schubert if( objc>3 ){ 2182076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 2183076b9443SCy Schubert return TCL_ERROR; 2184076b9443SCy Schubert }else if( objc==2 ){ 2185076b9443SCy Schubert if( pDb->zCommit ){ 2186076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zCommit, (char*)0); 2187076b9443SCy Schubert } 2188076b9443SCy Schubert }else{ 2189076b9443SCy Schubert const char *zCommit; 2190076b9443SCy Schubert int len; 2191076b9443SCy Schubert if( pDb->zCommit ){ 2192076b9443SCy Schubert Tcl_Free(pDb->zCommit); 2193076b9443SCy Schubert } 2194076b9443SCy Schubert zCommit = Tcl_GetStringFromObj(objv[2], &len); 2195076b9443SCy Schubert if( zCommit && len>0 ){ 2196076b9443SCy Schubert pDb->zCommit = Tcl_Alloc( len + 1 ); 2197076b9443SCy Schubert memcpy(pDb->zCommit, zCommit, len+1); 2198076b9443SCy Schubert }else{ 2199076b9443SCy Schubert pDb->zCommit = 0; 2200076b9443SCy Schubert } 2201076b9443SCy Schubert if( pDb->zCommit ){ 2202076b9443SCy Schubert pDb->interp = interp; 2203076b9443SCy Schubert sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb); 2204076b9443SCy Schubert }else{ 2205076b9443SCy Schubert sqlite3_commit_hook(pDb->db, 0, 0); 2206076b9443SCy Schubert } 2207076b9443SCy Schubert } 2208076b9443SCy Schubert break; 2209076b9443SCy Schubert } 2210076b9443SCy Schubert 2211076b9443SCy Schubert /* $db complete SQL 2212076b9443SCy Schubert ** 2213076b9443SCy Schubert ** Return TRUE if SQL is a complete SQL statement. Return FALSE if 2214076b9443SCy Schubert ** additional lines of input are needed. This is similar to the 2215076b9443SCy Schubert ** built-in "info complete" command of Tcl. 2216076b9443SCy Schubert */ 2217076b9443SCy Schubert case DB_COMPLETE: { 2218076b9443SCy Schubert #ifndef SQLITE_OMIT_COMPLETE 2219076b9443SCy Schubert Tcl_Obj *pResult; 2220076b9443SCy Schubert int isComplete; 2221076b9443SCy Schubert if( objc!=3 ){ 2222076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 2223076b9443SCy Schubert return TCL_ERROR; 2224076b9443SCy Schubert } 2225076b9443SCy Schubert isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) ); 2226076b9443SCy Schubert pResult = Tcl_GetObjResult(interp); 2227076b9443SCy Schubert Tcl_SetBooleanObj(pResult, isComplete); 2228076b9443SCy Schubert #endif 2229076b9443SCy Schubert break; 2230076b9443SCy Schubert } 2231076b9443SCy Schubert 2232076b9443SCy Schubert /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR? 2233076b9443SCy Schubert ** 2234076b9443SCy Schubert ** Copy data into table from filename, optionally using SEPARATOR 2235076b9443SCy Schubert ** as column separators. If a column contains a null string, or the 2236076b9443SCy Schubert ** value of NULLINDICATOR, a NULL is inserted for the column. 2237076b9443SCy Schubert ** conflict-algorithm is one of the sqlite conflict algorithms: 2238076b9443SCy Schubert ** rollback, abort, fail, ignore, replace 2239076b9443SCy Schubert ** On success, return the number of lines processed, not necessarily same 2240076b9443SCy Schubert ** as 'db changes' due to conflict-algorithm selected. 2241076b9443SCy Schubert ** 2242076b9443SCy Schubert ** This code is basically an implementation/enhancement of 2243076b9443SCy Schubert ** the sqlite3 shell.c ".import" command. 2244076b9443SCy Schubert ** 2245076b9443SCy Schubert ** This command usage is equivalent to the sqlite2.x COPY statement, 2246076b9443SCy Schubert ** which imports file data into a table using the PostgreSQL COPY file format: 2247076b9443SCy Schubert ** $db copy $conflit_algo $table_name $filename \t \\N 2248076b9443SCy Schubert */ 2249076b9443SCy Schubert case DB_COPY: { 2250076b9443SCy Schubert char *zTable; /* Insert data into this table */ 2251076b9443SCy Schubert char *zFile; /* The file from which to extract data */ 2252076b9443SCy Schubert char *zConflict; /* The conflict algorithm to use */ 2253076b9443SCy Schubert sqlite3_stmt *pStmt; /* A statement */ 2254076b9443SCy Schubert int nCol; /* Number of columns in the table */ 2255076b9443SCy Schubert int nByte; /* Number of bytes in an SQL string */ 2256076b9443SCy Schubert int i, j; /* Loop counters */ 2257076b9443SCy Schubert int nSep; /* Number of bytes in zSep[] */ 2258076b9443SCy Schubert int nNull; /* Number of bytes in zNull[] */ 2259076b9443SCy Schubert char *zSql; /* An SQL statement */ 2260076b9443SCy Schubert char *zLine; /* A single line of input from the file */ 2261076b9443SCy Schubert char **azCol; /* zLine[] broken up into columns */ 2262076b9443SCy Schubert const char *zCommit; /* How to commit changes */ 2263076b9443SCy Schubert FILE *in; /* The input file */ 2264076b9443SCy Schubert int lineno = 0; /* Line number of input file */ 2265076b9443SCy Schubert char zLineNum[80]; /* Line number print buffer */ 2266076b9443SCy Schubert Tcl_Obj *pResult; /* interp result */ 2267076b9443SCy Schubert 2268076b9443SCy Schubert const char *zSep; 2269076b9443SCy Schubert const char *zNull; 2270076b9443SCy Schubert if( objc<5 || objc>7 ){ 2271076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, 2272076b9443SCy Schubert "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"); 2273076b9443SCy Schubert return TCL_ERROR; 2274076b9443SCy Schubert } 2275076b9443SCy Schubert if( objc>=6 ){ 2276076b9443SCy Schubert zSep = Tcl_GetStringFromObj(objv[5], 0); 2277076b9443SCy Schubert }else{ 2278076b9443SCy Schubert zSep = "\t"; 2279076b9443SCy Schubert } 2280076b9443SCy Schubert if( objc>=7 ){ 2281076b9443SCy Schubert zNull = Tcl_GetStringFromObj(objv[6], 0); 2282076b9443SCy Schubert }else{ 2283076b9443SCy Schubert zNull = ""; 2284076b9443SCy Schubert } 2285076b9443SCy Schubert zConflict = Tcl_GetStringFromObj(objv[2], 0); 2286076b9443SCy Schubert zTable = Tcl_GetStringFromObj(objv[3], 0); 2287076b9443SCy Schubert zFile = Tcl_GetStringFromObj(objv[4], 0); 2288076b9443SCy Schubert nSep = strlen30(zSep); 2289076b9443SCy Schubert nNull = strlen30(zNull); 2290076b9443SCy Schubert if( nSep==0 ){ 2291076b9443SCy Schubert Tcl_AppendResult(interp,"Error: non-null separator required for copy", 2292076b9443SCy Schubert (char*)0); 2293076b9443SCy Schubert return TCL_ERROR; 2294076b9443SCy Schubert } 2295076b9443SCy Schubert if(strcmp(zConflict, "rollback") != 0 && 2296076b9443SCy Schubert strcmp(zConflict, "abort" ) != 0 && 2297076b9443SCy Schubert strcmp(zConflict, "fail" ) != 0 && 2298076b9443SCy Schubert strcmp(zConflict, "ignore" ) != 0 && 2299076b9443SCy Schubert strcmp(zConflict, "replace" ) != 0 ) { 2300076b9443SCy Schubert Tcl_AppendResult(interp, "Error: \"", zConflict, 2301076b9443SCy Schubert "\", conflict-algorithm must be one of: rollback, " 2302076b9443SCy Schubert "abort, fail, ignore, or replace", (char*)0); 2303076b9443SCy Schubert return TCL_ERROR; 2304076b9443SCy Schubert } 2305076b9443SCy Schubert zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); 2306076b9443SCy Schubert if( zSql==0 ){ 2307076b9443SCy Schubert Tcl_AppendResult(interp, "Error: no such table: ", zTable, (char*)0); 2308076b9443SCy Schubert return TCL_ERROR; 2309076b9443SCy Schubert } 2310076b9443SCy Schubert nByte = strlen30(zSql); 2311076b9443SCy Schubert rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 2312076b9443SCy Schubert sqlite3_free(zSql); 2313076b9443SCy Schubert if( rc ){ 2314076b9443SCy Schubert Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0); 2315076b9443SCy Schubert nCol = 0; 2316076b9443SCy Schubert }else{ 2317076b9443SCy Schubert nCol = sqlite3_column_count(pStmt); 2318076b9443SCy Schubert } 2319076b9443SCy Schubert sqlite3_finalize(pStmt); 2320076b9443SCy Schubert if( nCol==0 ) { 2321076b9443SCy Schubert return TCL_ERROR; 2322076b9443SCy Schubert } 2323076b9443SCy Schubert zSql = malloc( nByte + 50 + nCol*2 ); 2324076b9443SCy Schubert if( zSql==0 ) { 2325076b9443SCy Schubert Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0); 2326076b9443SCy Schubert return TCL_ERROR; 2327076b9443SCy Schubert } 2328076b9443SCy Schubert sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?", 2329076b9443SCy Schubert zConflict, zTable); 2330076b9443SCy Schubert j = strlen30(zSql); 2331076b9443SCy Schubert for(i=1; i<nCol; i++){ 2332076b9443SCy Schubert zSql[j++] = ','; 2333076b9443SCy Schubert zSql[j++] = '?'; 2334076b9443SCy Schubert } 2335076b9443SCy Schubert zSql[j++] = ')'; 2336076b9443SCy Schubert zSql[j] = 0; 2337076b9443SCy Schubert rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 2338076b9443SCy Schubert free(zSql); 2339076b9443SCy Schubert if( rc ){ 2340076b9443SCy Schubert Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0); 2341076b9443SCy Schubert sqlite3_finalize(pStmt); 2342076b9443SCy Schubert return TCL_ERROR; 2343076b9443SCy Schubert } 2344076b9443SCy Schubert in = fopen(zFile, "rb"); 2345076b9443SCy Schubert if( in==0 ){ 2346076b9443SCy Schubert Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, (char*)0); 2347076b9443SCy Schubert sqlite3_finalize(pStmt); 2348076b9443SCy Schubert return TCL_ERROR; 2349076b9443SCy Schubert } 2350076b9443SCy Schubert azCol = malloc( sizeof(azCol[0])*(nCol+1) ); 2351076b9443SCy Schubert if( azCol==0 ) { 2352076b9443SCy Schubert Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0); 2353076b9443SCy Schubert fclose(in); 2354076b9443SCy Schubert return TCL_ERROR; 2355076b9443SCy Schubert } 2356076b9443SCy Schubert (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0); 2357076b9443SCy Schubert zCommit = "COMMIT"; 2358076b9443SCy Schubert while( (zLine = local_getline(0, in))!=0 ){ 2359076b9443SCy Schubert char *z; 2360076b9443SCy Schubert lineno++; 2361076b9443SCy Schubert azCol[0] = zLine; 2362076b9443SCy Schubert for(i=0, z=zLine; *z; z++){ 2363076b9443SCy Schubert if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){ 2364076b9443SCy Schubert *z = 0; 2365076b9443SCy Schubert i++; 2366076b9443SCy Schubert if( i<nCol ){ 2367076b9443SCy Schubert azCol[i] = &z[nSep]; 2368076b9443SCy Schubert z += nSep-1; 2369076b9443SCy Schubert } 2370076b9443SCy Schubert } 2371076b9443SCy Schubert } 2372076b9443SCy Schubert if( i+1!=nCol ){ 2373076b9443SCy Schubert char *zErr; 2374076b9443SCy Schubert int nErr = strlen30(zFile) + 200; 2375076b9443SCy Schubert zErr = malloc(nErr); 2376076b9443SCy Schubert if( zErr ){ 2377076b9443SCy Schubert sqlite3_snprintf(nErr, zErr, 2378076b9443SCy Schubert "Error: %s line %d: expected %d columns of data but found %d", 2379076b9443SCy Schubert zFile, lineno, nCol, i+1); 2380076b9443SCy Schubert Tcl_AppendResult(interp, zErr, (char*)0); 2381076b9443SCy Schubert free(zErr); 2382076b9443SCy Schubert } 2383076b9443SCy Schubert zCommit = "ROLLBACK"; 2384076b9443SCy Schubert break; 2385076b9443SCy Schubert } 2386076b9443SCy Schubert for(i=0; i<nCol; i++){ 2387076b9443SCy Schubert /* check for null data, if so, bind as null */ 2388076b9443SCy Schubert if( (nNull>0 && strcmp(azCol[i], zNull)==0) 2389076b9443SCy Schubert || strlen30(azCol[i])==0 2390076b9443SCy Schubert ){ 2391076b9443SCy Schubert sqlite3_bind_null(pStmt, i+1); 2392076b9443SCy Schubert }else{ 2393076b9443SCy Schubert sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); 2394076b9443SCy Schubert } 2395076b9443SCy Schubert } 2396076b9443SCy Schubert sqlite3_step(pStmt); 2397076b9443SCy Schubert rc = sqlite3_reset(pStmt); 2398076b9443SCy Schubert free(zLine); 2399076b9443SCy Schubert if( rc!=SQLITE_OK ){ 2400076b9443SCy Schubert Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), (char*)0); 2401076b9443SCy Schubert zCommit = "ROLLBACK"; 2402076b9443SCy Schubert break; 2403076b9443SCy Schubert } 2404076b9443SCy Schubert } 2405076b9443SCy Schubert free(azCol); 2406076b9443SCy Schubert fclose(in); 2407076b9443SCy Schubert sqlite3_finalize(pStmt); 2408076b9443SCy Schubert (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0); 2409076b9443SCy Schubert 2410076b9443SCy Schubert if( zCommit[0] == 'C' ){ 2411076b9443SCy Schubert /* success, set result as number of lines processed */ 2412076b9443SCy Schubert pResult = Tcl_GetObjResult(interp); 2413076b9443SCy Schubert Tcl_SetIntObj(pResult, lineno); 2414076b9443SCy Schubert rc = TCL_OK; 2415076b9443SCy Schubert }else{ 2416076b9443SCy Schubert /* failure, append lineno where failed */ 2417076b9443SCy Schubert sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno); 2418076b9443SCy Schubert Tcl_AppendResult(interp,", failed while processing line: ",zLineNum, 2419076b9443SCy Schubert (char*)0); 2420076b9443SCy Schubert rc = TCL_ERROR; 2421076b9443SCy Schubert } 2422076b9443SCy Schubert break; 2423076b9443SCy Schubert } 2424076b9443SCy Schubert 2425076b9443SCy Schubert /* 2426*bca4681bSCy Schubert ** $db deserialize ?-maxsize N? ?-readonly BOOL? ?DATABASE? VALUE 2427076b9443SCy Schubert ** 2428076b9443SCy Schubert ** Reopen DATABASE (default "main") using the content in $VALUE 2429076b9443SCy Schubert */ 2430076b9443SCy Schubert case DB_DESERIALIZE: { 2431076b9443SCy Schubert #ifndef SQLITE_ENABLE_DESERIALIZE 2432076b9443SCy Schubert Tcl_AppendResult(interp, "MEMDB not available in this build", 2433076b9443SCy Schubert (char*)0); 2434076b9443SCy Schubert rc = TCL_ERROR; 2435076b9443SCy Schubert #else 2436*bca4681bSCy Schubert const char *zSchema = 0; 2437*bca4681bSCy Schubert Tcl_Obj *pValue = 0; 2438076b9443SCy Schubert unsigned char *pBA; 2439076b9443SCy Schubert unsigned char *pData; 2440076b9443SCy Schubert int len, xrc; 2441*bca4681bSCy Schubert sqlite3_int64 mxSize = 0; 2442*bca4681bSCy Schubert int i; 2443*bca4681bSCy Schubert int isReadonly = 0; 2444076b9443SCy Schubert 2445*bca4681bSCy Schubert 2446*bca4681bSCy Schubert if( objc<3 ){ 2447076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? VALUE"); 2448076b9443SCy Schubert rc = TCL_ERROR; 2449076b9443SCy Schubert break; 2450076b9443SCy Schubert } 2451*bca4681bSCy Schubert for(i=2; i<objc-1; i++){ 2452*bca4681bSCy Schubert const char *z = Tcl_GetString(objv[i]); 2453*bca4681bSCy Schubert if( strcmp(z,"-maxsize")==0 && i<objc-2 ){ 2454*bca4681bSCy Schubert rc = Tcl_GetWideIntFromObj(interp, objv[++i], &mxSize); 2455*bca4681bSCy Schubert if( rc ) goto deserialize_error; 2456*bca4681bSCy Schubert continue; 2457*bca4681bSCy Schubert } 2458*bca4681bSCy Schubert if( strcmp(z,"-readonly")==0 && i<objc-2 ){ 2459*bca4681bSCy Schubert rc = Tcl_GetBooleanFromObj(interp, objv[++i], &isReadonly); 2460*bca4681bSCy Schubert if( rc ) goto deserialize_error; 2461*bca4681bSCy Schubert continue; 2462*bca4681bSCy Schubert } 2463*bca4681bSCy Schubert if( zSchema==0 && i==objc-2 && z[0]!='-' ){ 2464*bca4681bSCy Schubert zSchema = z; 2465*bca4681bSCy Schubert continue; 2466*bca4681bSCy Schubert } 2467*bca4681bSCy Schubert Tcl_AppendResult(interp, "unknown option: ", z, (char*)0); 2468*bca4681bSCy Schubert rc = TCL_ERROR; 2469*bca4681bSCy Schubert goto deserialize_error; 2470*bca4681bSCy Schubert } 2471*bca4681bSCy Schubert pValue = objv[objc-1]; 2472076b9443SCy Schubert pBA = Tcl_GetByteArrayFromObj(pValue, &len); 2473076b9443SCy Schubert pData = sqlite3_malloc64( len ); 2474076b9443SCy Schubert if( pData==0 && len>0 ){ 2475076b9443SCy Schubert Tcl_AppendResult(interp, "out of memory", (char*)0); 2476076b9443SCy Schubert rc = TCL_ERROR; 2477076b9443SCy Schubert }else{ 2478*bca4681bSCy Schubert int flags; 2479076b9443SCy Schubert if( len>0 ) memcpy(pData, pBA, len); 2480*bca4681bSCy Schubert if( isReadonly ){ 2481*bca4681bSCy Schubert flags = SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_READONLY; 2482*bca4681bSCy Schubert }else{ 2483*bca4681bSCy Schubert flags = SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE; 2484*bca4681bSCy Schubert } 2485*bca4681bSCy Schubert xrc = sqlite3_deserialize(pDb->db, zSchema, pData, len, len, flags); 2486076b9443SCy Schubert if( xrc ){ 2487076b9443SCy Schubert Tcl_AppendResult(interp, "unable to set MEMDB content", (char*)0); 2488076b9443SCy Schubert rc = TCL_ERROR; 2489076b9443SCy Schubert } 2490*bca4681bSCy Schubert if( mxSize>0 ){ 2491*bca4681bSCy Schubert sqlite3_file_control(pDb->db, zSchema,SQLITE_FCNTL_SIZE_LIMIT,&mxSize); 2492076b9443SCy Schubert } 2493*bca4681bSCy Schubert } 2494*bca4681bSCy Schubert deserialize_error: 2495076b9443SCy Schubert #endif 2496076b9443SCy Schubert break; 2497076b9443SCy Schubert } 2498076b9443SCy Schubert 2499076b9443SCy Schubert /* 2500076b9443SCy Schubert ** $db enable_load_extension BOOLEAN 2501076b9443SCy Schubert ** 2502076b9443SCy Schubert ** Turn the extension loading feature on or off. It if off by 2503076b9443SCy Schubert ** default. 2504076b9443SCy Schubert */ 2505076b9443SCy Schubert case DB_ENABLE_LOAD_EXTENSION: { 2506076b9443SCy Schubert #ifndef SQLITE_OMIT_LOAD_EXTENSION 2507076b9443SCy Schubert int onoff; 2508076b9443SCy Schubert if( objc!=3 ){ 2509076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN"); 2510076b9443SCy Schubert return TCL_ERROR; 2511076b9443SCy Schubert } 2512076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ 2513076b9443SCy Schubert return TCL_ERROR; 2514076b9443SCy Schubert } 2515076b9443SCy Schubert sqlite3_enable_load_extension(pDb->db, onoff); 2516076b9443SCy Schubert break; 2517076b9443SCy Schubert #else 2518076b9443SCy Schubert Tcl_AppendResult(interp, "extension loading is turned off at compile-time", 2519076b9443SCy Schubert (char*)0); 2520076b9443SCy Schubert return TCL_ERROR; 2521076b9443SCy Schubert #endif 2522076b9443SCy Schubert } 2523076b9443SCy Schubert 2524076b9443SCy Schubert /* 2525076b9443SCy Schubert ** $db errorcode 2526076b9443SCy Schubert ** 2527076b9443SCy Schubert ** Return the numeric error code that was returned by the most recent 2528076b9443SCy Schubert ** call to sqlite3_exec(). 2529076b9443SCy Schubert */ 2530076b9443SCy Schubert case DB_ERRORCODE: { 2531076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); 2532076b9443SCy Schubert break; 2533076b9443SCy Schubert } 2534076b9443SCy Schubert 2535076b9443SCy Schubert /* 2536076b9443SCy Schubert ** $db exists $sql 2537076b9443SCy Schubert ** $db onecolumn $sql 2538076b9443SCy Schubert ** 2539076b9443SCy Schubert ** The onecolumn method is the equivalent of: 2540076b9443SCy Schubert ** lindex [$db eval $sql] 0 2541076b9443SCy Schubert */ 2542076b9443SCy Schubert case DB_EXISTS: 2543076b9443SCy Schubert case DB_ONECOLUMN: { 2544076b9443SCy Schubert Tcl_Obj *pResult = 0; 2545076b9443SCy Schubert DbEvalContext sEval; 2546076b9443SCy Schubert if( objc!=3 ){ 2547076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 2548076b9443SCy Schubert return TCL_ERROR; 2549076b9443SCy Schubert } 2550076b9443SCy Schubert 2551076b9443SCy Schubert dbEvalInit(&sEval, pDb, objv[2], 0, 0); 2552076b9443SCy Schubert rc = dbEvalStep(&sEval); 2553076b9443SCy Schubert if( choice==DB_ONECOLUMN ){ 2554076b9443SCy Schubert if( rc==TCL_OK ){ 2555076b9443SCy Schubert pResult = dbEvalColumnValue(&sEval, 0); 2556076b9443SCy Schubert }else if( rc==TCL_BREAK ){ 2557076b9443SCy Schubert Tcl_ResetResult(interp); 2558076b9443SCy Schubert } 2559076b9443SCy Schubert }else if( rc==TCL_BREAK || rc==TCL_OK ){ 2560076b9443SCy Schubert pResult = Tcl_NewBooleanObj(rc==TCL_OK); 2561076b9443SCy Schubert } 2562076b9443SCy Schubert dbEvalFinalize(&sEval); 2563076b9443SCy Schubert if( pResult ) Tcl_SetObjResult(interp, pResult); 2564076b9443SCy Schubert 2565076b9443SCy Schubert if( rc==TCL_BREAK ){ 2566076b9443SCy Schubert rc = TCL_OK; 2567076b9443SCy Schubert } 2568076b9443SCy Schubert break; 2569076b9443SCy Schubert } 2570076b9443SCy Schubert 2571076b9443SCy Schubert /* 2572076b9443SCy Schubert ** $db eval ?options? $sql ?array? ?{ ...code... }? 2573076b9443SCy Schubert ** 2574076b9443SCy Schubert ** The SQL statement in $sql is evaluated. For each row, the values are 2575076b9443SCy Schubert ** placed in elements of the array named "array" and ...code... is executed. 2576076b9443SCy Schubert ** If "array" and "code" are omitted, then no callback is every invoked. 2577076b9443SCy Schubert ** If "array" is an empty string, then the values are placed in variables 2578076b9443SCy Schubert ** that have the same name as the fields extracted by the query. 2579076b9443SCy Schubert */ 2580076b9443SCy Schubert case DB_EVAL: { 2581076b9443SCy Schubert int evalFlags = 0; 2582076b9443SCy Schubert const char *zOpt; 2583076b9443SCy Schubert while( objc>3 && (zOpt = Tcl_GetString(objv[2]))!=0 && zOpt[0]=='-' ){ 2584076b9443SCy Schubert if( strcmp(zOpt, "-withoutnulls")==0 ){ 2585076b9443SCy Schubert evalFlags |= SQLITE_EVAL_WITHOUTNULLS; 2586076b9443SCy Schubert } 2587076b9443SCy Schubert else{ 2588076b9443SCy Schubert Tcl_AppendResult(interp, "unknown option: \"", zOpt, "\"", (void*)0); 2589076b9443SCy Schubert return TCL_ERROR; 2590076b9443SCy Schubert } 2591076b9443SCy Schubert objc--; 2592076b9443SCy Schubert objv++; 2593076b9443SCy Schubert } 2594076b9443SCy Schubert if( objc<3 || objc>5 ){ 2595076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, 2596076b9443SCy Schubert "?OPTIONS? SQL ?ARRAY-NAME? ?SCRIPT?"); 2597076b9443SCy Schubert return TCL_ERROR; 2598076b9443SCy Schubert } 2599076b9443SCy Schubert 2600076b9443SCy Schubert if( objc==3 ){ 2601076b9443SCy Schubert DbEvalContext sEval; 2602076b9443SCy Schubert Tcl_Obj *pRet = Tcl_NewObj(); 2603076b9443SCy Schubert Tcl_IncrRefCount(pRet); 2604076b9443SCy Schubert dbEvalInit(&sEval, pDb, objv[2], 0, 0); 2605076b9443SCy Schubert while( TCL_OK==(rc = dbEvalStep(&sEval)) ){ 2606076b9443SCy Schubert int i; 2607076b9443SCy Schubert int nCol; 2608076b9443SCy Schubert dbEvalRowInfo(&sEval, &nCol, 0); 2609076b9443SCy Schubert for(i=0; i<nCol; i++){ 2610076b9443SCy Schubert Tcl_ListObjAppendElement(interp, pRet, dbEvalColumnValue(&sEval, i)); 2611076b9443SCy Schubert } 2612076b9443SCy Schubert } 2613076b9443SCy Schubert dbEvalFinalize(&sEval); 2614076b9443SCy Schubert if( rc==TCL_BREAK ){ 2615076b9443SCy Schubert Tcl_SetObjResult(interp, pRet); 2616076b9443SCy Schubert rc = TCL_OK; 2617076b9443SCy Schubert } 2618076b9443SCy Schubert Tcl_DecrRefCount(pRet); 2619076b9443SCy Schubert }else{ 2620076b9443SCy Schubert ClientData cd2[2]; 2621076b9443SCy Schubert DbEvalContext *p; 2622076b9443SCy Schubert Tcl_Obj *pArray = 0; 2623076b9443SCy Schubert Tcl_Obj *pScript; 2624076b9443SCy Schubert 2625076b9443SCy Schubert if( objc>=5 && *(char *)Tcl_GetString(objv[3]) ){ 2626076b9443SCy Schubert pArray = objv[3]; 2627076b9443SCy Schubert } 2628076b9443SCy Schubert pScript = objv[objc-1]; 2629076b9443SCy Schubert Tcl_IncrRefCount(pScript); 2630076b9443SCy Schubert 2631076b9443SCy Schubert p = (DbEvalContext *)Tcl_Alloc(sizeof(DbEvalContext)); 2632076b9443SCy Schubert dbEvalInit(p, pDb, objv[2], pArray, evalFlags); 2633076b9443SCy Schubert 2634076b9443SCy Schubert cd2[0] = (void *)p; 2635076b9443SCy Schubert cd2[1] = (void *)pScript; 2636076b9443SCy Schubert rc = DbEvalNextCmd(cd2, interp, TCL_OK); 2637076b9443SCy Schubert } 2638076b9443SCy Schubert break; 2639076b9443SCy Schubert } 2640076b9443SCy Schubert 2641076b9443SCy Schubert /* 2642076b9443SCy Schubert ** $db function NAME [-argcount N] [-deterministic] SCRIPT 2643076b9443SCy Schubert ** 2644076b9443SCy Schubert ** Create a new SQL function called NAME. Whenever that function is 2645076b9443SCy Schubert ** called, invoke SCRIPT to evaluate the function. 2646076b9443SCy Schubert */ 2647076b9443SCy Schubert case DB_FUNCTION: { 2648076b9443SCy Schubert int flags = SQLITE_UTF8; 2649076b9443SCy Schubert SqlFunc *pFunc; 2650076b9443SCy Schubert Tcl_Obj *pScript; 2651076b9443SCy Schubert char *zName; 2652076b9443SCy Schubert int nArg = -1; 2653076b9443SCy Schubert int i; 2654076b9443SCy Schubert if( objc<4 ){ 2655076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "NAME ?SWITCHES? SCRIPT"); 2656076b9443SCy Schubert return TCL_ERROR; 2657076b9443SCy Schubert } 2658076b9443SCy Schubert for(i=3; i<(objc-1); i++){ 2659076b9443SCy Schubert const char *z = Tcl_GetString(objv[i]); 2660076b9443SCy Schubert int n = strlen30(z); 2661076b9443SCy Schubert if( n>2 && strncmp(z, "-argcount",n)==0 ){ 2662076b9443SCy Schubert if( i==(objc-2) ){ 2663076b9443SCy Schubert Tcl_AppendResult(interp, "option requires an argument: ", z,(char*)0); 2664076b9443SCy Schubert return TCL_ERROR; 2665076b9443SCy Schubert } 2666076b9443SCy Schubert if( Tcl_GetIntFromObj(interp, objv[i+1], &nArg) ) return TCL_ERROR; 2667076b9443SCy Schubert if( nArg<0 ){ 2668076b9443SCy Schubert Tcl_AppendResult(interp, "number of arguments must be non-negative", 2669076b9443SCy Schubert (char*)0); 2670076b9443SCy Schubert return TCL_ERROR; 2671076b9443SCy Schubert } 2672076b9443SCy Schubert i++; 2673076b9443SCy Schubert }else 2674076b9443SCy Schubert if( n>2 && strncmp(z, "-deterministic",n)==0 ){ 2675076b9443SCy Schubert flags |= SQLITE_DETERMINISTIC; 2676076b9443SCy Schubert }else{ 2677076b9443SCy Schubert Tcl_AppendResult(interp, "bad option \"", z, 2678076b9443SCy Schubert "\": must be -argcount or -deterministic", (char*)0 2679076b9443SCy Schubert ); 2680076b9443SCy Schubert return TCL_ERROR; 2681076b9443SCy Schubert } 2682076b9443SCy Schubert } 2683076b9443SCy Schubert 2684076b9443SCy Schubert pScript = objv[objc-1]; 2685076b9443SCy Schubert zName = Tcl_GetStringFromObj(objv[2], 0); 2686076b9443SCy Schubert pFunc = findSqlFunc(pDb, zName); 2687076b9443SCy Schubert if( pFunc==0 ) return TCL_ERROR; 2688076b9443SCy Schubert if( pFunc->pScript ){ 2689076b9443SCy Schubert Tcl_DecrRefCount(pFunc->pScript); 2690076b9443SCy Schubert } 2691076b9443SCy Schubert pFunc->pScript = pScript; 2692076b9443SCy Schubert Tcl_IncrRefCount(pScript); 2693076b9443SCy Schubert pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript); 2694076b9443SCy Schubert rc = sqlite3_create_function(pDb->db, zName, nArg, flags, 2695076b9443SCy Schubert pFunc, tclSqlFunc, 0, 0); 2696076b9443SCy Schubert if( rc!=SQLITE_OK ){ 2697076b9443SCy Schubert rc = TCL_ERROR; 2698076b9443SCy Schubert Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 2699076b9443SCy Schubert } 2700076b9443SCy Schubert break; 2701076b9443SCy Schubert } 2702076b9443SCy Schubert 2703076b9443SCy Schubert /* 2704076b9443SCy Schubert ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID 2705076b9443SCy Schubert */ 2706076b9443SCy Schubert case DB_INCRBLOB: { 2707076b9443SCy Schubert #ifdef SQLITE_OMIT_INCRBLOB 2708076b9443SCy Schubert Tcl_AppendResult(interp, "incrblob not available in this build", (char*)0); 2709076b9443SCy Schubert return TCL_ERROR; 2710076b9443SCy Schubert #else 2711076b9443SCy Schubert int isReadonly = 0; 2712076b9443SCy Schubert const char *zDb = "main"; 2713076b9443SCy Schubert const char *zTable; 2714076b9443SCy Schubert const char *zColumn; 2715076b9443SCy Schubert Tcl_WideInt iRow; 2716076b9443SCy Schubert 2717076b9443SCy Schubert /* Check for the -readonly option */ 2718076b9443SCy Schubert if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){ 2719076b9443SCy Schubert isReadonly = 1; 2720076b9443SCy Schubert } 2721076b9443SCy Schubert 2722076b9443SCy Schubert if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){ 2723076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID"); 2724076b9443SCy Schubert return TCL_ERROR; 2725076b9443SCy Schubert } 2726076b9443SCy Schubert 2727076b9443SCy Schubert if( objc==(6+isReadonly) ){ 2728076b9443SCy Schubert zDb = Tcl_GetString(objv[2]); 2729076b9443SCy Schubert } 2730076b9443SCy Schubert zTable = Tcl_GetString(objv[objc-3]); 2731076b9443SCy Schubert zColumn = Tcl_GetString(objv[objc-2]); 2732076b9443SCy Schubert rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow); 2733076b9443SCy Schubert 2734076b9443SCy Schubert if( rc==TCL_OK ){ 2735076b9443SCy Schubert rc = createIncrblobChannel( 2736076b9443SCy Schubert interp, pDb, zDb, zTable, zColumn, (sqlite3_int64)iRow, isReadonly 2737076b9443SCy Schubert ); 2738076b9443SCy Schubert } 2739076b9443SCy Schubert #endif 2740076b9443SCy Schubert break; 2741076b9443SCy Schubert } 2742076b9443SCy Schubert 2743076b9443SCy Schubert /* 2744076b9443SCy Schubert ** $db interrupt 2745076b9443SCy Schubert ** 2746076b9443SCy Schubert ** Interrupt the execution of the inner-most SQL interpreter. This 2747076b9443SCy Schubert ** causes the SQL statement to return an error of SQLITE_INTERRUPT. 2748076b9443SCy Schubert */ 2749076b9443SCy Schubert case DB_INTERRUPT: { 2750076b9443SCy Schubert sqlite3_interrupt(pDb->db); 2751076b9443SCy Schubert break; 2752076b9443SCy Schubert } 2753076b9443SCy Schubert 2754076b9443SCy Schubert /* 2755076b9443SCy Schubert ** $db nullvalue ?STRING? 2756076b9443SCy Schubert ** 2757076b9443SCy Schubert ** Change text used when a NULL comes back from the database. If ?STRING? 2758076b9443SCy Schubert ** is not present, then the current string used for NULL is returned. 2759076b9443SCy Schubert ** If STRING is present, then STRING is returned. 2760076b9443SCy Schubert ** 2761076b9443SCy Schubert */ 2762076b9443SCy Schubert case DB_NULLVALUE: { 2763076b9443SCy Schubert if( objc!=2 && objc!=3 ){ 2764076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE"); 2765076b9443SCy Schubert return TCL_ERROR; 2766076b9443SCy Schubert } 2767076b9443SCy Schubert if( objc==3 ){ 2768076b9443SCy Schubert int len; 2769076b9443SCy Schubert char *zNull = Tcl_GetStringFromObj(objv[2], &len); 2770076b9443SCy Schubert if( pDb->zNull ){ 2771076b9443SCy Schubert Tcl_Free(pDb->zNull); 2772076b9443SCy Schubert } 2773076b9443SCy Schubert if( zNull && len>0 ){ 2774076b9443SCy Schubert pDb->zNull = Tcl_Alloc( len + 1 ); 2775076b9443SCy Schubert memcpy(pDb->zNull, zNull, len); 2776076b9443SCy Schubert pDb->zNull[len] = '\0'; 2777076b9443SCy Schubert }else{ 2778076b9443SCy Schubert pDb->zNull = 0; 2779076b9443SCy Schubert } 2780076b9443SCy Schubert } 2781076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewStringObj(pDb->zNull, -1)); 2782076b9443SCy Schubert break; 2783076b9443SCy Schubert } 2784076b9443SCy Schubert 2785076b9443SCy Schubert /* 2786076b9443SCy Schubert ** $db last_insert_rowid 2787076b9443SCy Schubert ** 2788076b9443SCy Schubert ** Return an integer which is the ROWID for the most recent insert. 2789076b9443SCy Schubert */ 2790076b9443SCy Schubert case DB_LAST_INSERT_ROWID: { 2791076b9443SCy Schubert Tcl_Obj *pResult; 2792076b9443SCy Schubert Tcl_WideInt rowid; 2793076b9443SCy Schubert if( objc!=2 ){ 2794076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, ""); 2795076b9443SCy Schubert return TCL_ERROR; 2796076b9443SCy Schubert } 2797076b9443SCy Schubert rowid = sqlite3_last_insert_rowid(pDb->db); 2798076b9443SCy Schubert pResult = Tcl_GetObjResult(interp); 2799076b9443SCy Schubert Tcl_SetWideIntObj(pResult, rowid); 2800076b9443SCy Schubert break; 2801076b9443SCy Schubert } 2802076b9443SCy Schubert 2803076b9443SCy Schubert /* 2804076b9443SCy Schubert ** The DB_ONECOLUMN method is implemented together with DB_EXISTS. 2805076b9443SCy Schubert */ 2806076b9443SCy Schubert 2807076b9443SCy Schubert /* $db progress ?N CALLBACK? 2808076b9443SCy Schubert ** 2809076b9443SCy Schubert ** Invoke the given callback every N virtual machine opcodes while executing 2810076b9443SCy Schubert ** queries. 2811076b9443SCy Schubert */ 2812076b9443SCy Schubert case DB_PROGRESS: { 2813076b9443SCy Schubert if( objc==2 ){ 2814076b9443SCy Schubert if( pDb->zProgress ){ 2815076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zProgress, (char*)0); 2816076b9443SCy Schubert } 2817076b9443SCy Schubert }else if( objc==4 ){ 2818076b9443SCy Schubert char *zProgress; 2819076b9443SCy Schubert int len; 2820076b9443SCy Schubert int N; 2821076b9443SCy Schubert if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ 2822076b9443SCy Schubert return TCL_ERROR; 2823076b9443SCy Schubert }; 2824076b9443SCy Schubert if( pDb->zProgress ){ 2825076b9443SCy Schubert Tcl_Free(pDb->zProgress); 2826076b9443SCy Schubert } 2827076b9443SCy Schubert zProgress = Tcl_GetStringFromObj(objv[3], &len); 2828076b9443SCy Schubert if( zProgress && len>0 ){ 2829076b9443SCy Schubert pDb->zProgress = Tcl_Alloc( len + 1 ); 2830076b9443SCy Schubert memcpy(pDb->zProgress, zProgress, len+1); 2831076b9443SCy Schubert }else{ 2832076b9443SCy Schubert pDb->zProgress = 0; 2833076b9443SCy Schubert } 2834076b9443SCy Schubert #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 2835076b9443SCy Schubert if( pDb->zProgress ){ 2836076b9443SCy Schubert pDb->interp = interp; 2837076b9443SCy Schubert sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb); 2838076b9443SCy Schubert }else{ 2839076b9443SCy Schubert sqlite3_progress_handler(pDb->db, 0, 0, 0); 2840076b9443SCy Schubert } 2841076b9443SCy Schubert #endif 2842076b9443SCy Schubert }else{ 2843076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); 2844076b9443SCy Schubert return TCL_ERROR; 2845076b9443SCy Schubert } 2846076b9443SCy Schubert break; 2847076b9443SCy Schubert } 2848076b9443SCy Schubert 2849076b9443SCy Schubert /* $db profile ?CALLBACK? 2850076b9443SCy Schubert ** 2851076b9443SCy Schubert ** Make arrangements to invoke the CALLBACK routine after each SQL statement 2852076b9443SCy Schubert ** that has run. The text of the SQL and the amount of elapse time are 2853076b9443SCy Schubert ** appended to CALLBACK before the script is run. 2854076b9443SCy Schubert */ 2855076b9443SCy Schubert case DB_PROFILE: { 2856076b9443SCy Schubert if( objc>3 ){ 2857076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 2858076b9443SCy Schubert return TCL_ERROR; 2859076b9443SCy Schubert }else if( objc==2 ){ 2860076b9443SCy Schubert if( pDb->zProfile ){ 2861076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zProfile, (char*)0); 2862076b9443SCy Schubert } 2863076b9443SCy Schubert }else{ 2864076b9443SCy Schubert char *zProfile; 2865076b9443SCy Schubert int len; 2866076b9443SCy Schubert if( pDb->zProfile ){ 2867076b9443SCy Schubert Tcl_Free(pDb->zProfile); 2868076b9443SCy Schubert } 2869076b9443SCy Schubert zProfile = Tcl_GetStringFromObj(objv[2], &len); 2870076b9443SCy Schubert if( zProfile && len>0 ){ 2871076b9443SCy Schubert pDb->zProfile = Tcl_Alloc( len + 1 ); 2872076b9443SCy Schubert memcpy(pDb->zProfile, zProfile, len+1); 2873076b9443SCy Schubert }else{ 2874076b9443SCy Schubert pDb->zProfile = 0; 2875076b9443SCy Schubert } 2876076b9443SCy Schubert #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ 2877076b9443SCy Schubert !defined(SQLITE_OMIT_DEPRECATED) 2878076b9443SCy Schubert if( pDb->zProfile ){ 2879076b9443SCy Schubert pDb->interp = interp; 2880076b9443SCy Schubert sqlite3_profile(pDb->db, DbProfileHandler, pDb); 2881076b9443SCy Schubert }else{ 2882076b9443SCy Schubert sqlite3_profile(pDb->db, 0, 0); 2883076b9443SCy Schubert } 2884076b9443SCy Schubert #endif 2885076b9443SCy Schubert } 2886076b9443SCy Schubert break; 2887076b9443SCy Schubert } 2888076b9443SCy Schubert 2889076b9443SCy Schubert /* 2890076b9443SCy Schubert ** $db rekey KEY 2891076b9443SCy Schubert ** 2892076b9443SCy Schubert ** Change the encryption key on the currently open database. 2893076b9443SCy Schubert */ 2894076b9443SCy Schubert case DB_REKEY: { 2895076b9443SCy Schubert #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 2896076b9443SCy Schubert int nKey; 2897076b9443SCy Schubert void *pKey; 2898076b9443SCy Schubert #endif 2899076b9443SCy Schubert if( objc!=3 ){ 2900076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "KEY"); 2901076b9443SCy Schubert return TCL_ERROR; 2902076b9443SCy Schubert } 2903076b9443SCy Schubert #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 2904076b9443SCy Schubert pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); 2905076b9443SCy Schubert rc = sqlite3_rekey(pDb->db, pKey, nKey); 2906076b9443SCy Schubert if( rc ){ 2907076b9443SCy Schubert Tcl_AppendResult(interp, sqlite3_errstr(rc), (char*)0); 2908076b9443SCy Schubert rc = TCL_ERROR; 2909076b9443SCy Schubert } 2910076b9443SCy Schubert #endif 2911076b9443SCy Schubert break; 2912076b9443SCy Schubert } 2913076b9443SCy Schubert 2914076b9443SCy Schubert /* $db restore ?DATABASE? FILENAME 2915076b9443SCy Schubert ** 2916076b9443SCy Schubert ** Open a database file named FILENAME. Transfer the content 2917076b9443SCy Schubert ** of FILENAME into the local database DATABASE (default: "main"). 2918076b9443SCy Schubert */ 2919076b9443SCy Schubert case DB_RESTORE: { 2920076b9443SCy Schubert const char *zSrcFile; 2921076b9443SCy Schubert const char *zDestDb; 2922076b9443SCy Schubert sqlite3 *pSrc; 2923076b9443SCy Schubert sqlite3_backup *pBackup; 2924076b9443SCy Schubert int nTimeout = 0; 2925076b9443SCy Schubert 2926076b9443SCy Schubert if( objc==3 ){ 2927076b9443SCy Schubert zDestDb = "main"; 2928076b9443SCy Schubert zSrcFile = Tcl_GetString(objv[2]); 2929076b9443SCy Schubert }else if( objc==4 ){ 2930076b9443SCy Schubert zDestDb = Tcl_GetString(objv[2]); 2931076b9443SCy Schubert zSrcFile = Tcl_GetString(objv[3]); 2932076b9443SCy Schubert }else{ 2933076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 2934076b9443SCy Schubert return TCL_ERROR; 2935076b9443SCy Schubert } 2936076b9443SCy Schubert rc = sqlite3_open_v2(zSrcFile, &pSrc, 2937076b9443SCy Schubert SQLITE_OPEN_READONLY | pDb->openFlags, 0); 2938076b9443SCy Schubert if( rc!=SQLITE_OK ){ 2939076b9443SCy Schubert Tcl_AppendResult(interp, "cannot open source database: ", 2940076b9443SCy Schubert sqlite3_errmsg(pSrc), (char*)0); 2941076b9443SCy Schubert sqlite3_close(pSrc); 2942076b9443SCy Schubert return TCL_ERROR; 2943076b9443SCy Schubert } 2944076b9443SCy Schubert pBackup = sqlite3_backup_init(pDb->db, zDestDb, pSrc, "main"); 2945076b9443SCy Schubert if( pBackup==0 ){ 2946076b9443SCy Schubert Tcl_AppendResult(interp, "restore failed: ", 2947076b9443SCy Schubert sqlite3_errmsg(pDb->db), (char*)0); 2948076b9443SCy Schubert sqlite3_close(pSrc); 2949076b9443SCy Schubert return TCL_ERROR; 2950076b9443SCy Schubert } 2951076b9443SCy Schubert while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK 2952076b9443SCy Schubert || rc==SQLITE_BUSY ){ 2953076b9443SCy Schubert if( rc==SQLITE_BUSY ){ 2954076b9443SCy Schubert if( nTimeout++ >= 3 ) break; 2955076b9443SCy Schubert sqlite3_sleep(100); 2956076b9443SCy Schubert } 2957076b9443SCy Schubert } 2958076b9443SCy Schubert sqlite3_backup_finish(pBackup); 2959076b9443SCy Schubert if( rc==SQLITE_DONE ){ 2960076b9443SCy Schubert rc = TCL_OK; 2961076b9443SCy Schubert }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 2962076b9443SCy Schubert Tcl_AppendResult(interp, "restore failed: source database busy", 2963076b9443SCy Schubert (char*)0); 2964076b9443SCy Schubert rc = TCL_ERROR; 2965076b9443SCy Schubert }else{ 2966076b9443SCy Schubert Tcl_AppendResult(interp, "restore failed: ", 2967076b9443SCy Schubert sqlite3_errmsg(pDb->db), (char*)0); 2968076b9443SCy Schubert rc = TCL_ERROR; 2969076b9443SCy Schubert } 2970076b9443SCy Schubert sqlite3_close(pSrc); 2971076b9443SCy Schubert break; 2972076b9443SCy Schubert } 2973076b9443SCy Schubert 2974076b9443SCy Schubert /* 2975076b9443SCy Schubert ** $db serialize ?DATABASE? 2976076b9443SCy Schubert ** 2977076b9443SCy Schubert ** Return a serialization of a database. 2978076b9443SCy Schubert */ 2979076b9443SCy Schubert case DB_SERIALIZE: { 2980076b9443SCy Schubert #ifndef SQLITE_ENABLE_DESERIALIZE 2981076b9443SCy Schubert Tcl_AppendResult(interp, "MEMDB not available in this build", 2982076b9443SCy Schubert (char*)0); 2983076b9443SCy Schubert rc = TCL_ERROR; 2984076b9443SCy Schubert #else 2985076b9443SCy Schubert const char *zSchema = objc>=3 ? Tcl_GetString(objv[2]) : "main"; 2986076b9443SCy Schubert sqlite3_int64 sz = 0; 2987076b9443SCy Schubert unsigned char *pData; 2988076b9443SCy Schubert if( objc!=2 && objc!=3 ){ 2989076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE?"); 2990076b9443SCy Schubert rc = TCL_ERROR; 2991076b9443SCy Schubert }else{ 2992076b9443SCy Schubert int needFree; 2993076b9443SCy Schubert pData = sqlite3_serialize(pDb->db, zSchema, &sz, SQLITE_SERIALIZE_NOCOPY); 2994076b9443SCy Schubert if( pData ){ 2995076b9443SCy Schubert needFree = 0; 2996076b9443SCy Schubert }else{ 2997076b9443SCy Schubert pData = sqlite3_serialize(pDb->db, zSchema, &sz, 0); 2998076b9443SCy Schubert needFree = 1; 2999076b9443SCy Schubert } 3000076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pData,sz)); 3001076b9443SCy Schubert if( needFree ) sqlite3_free(pData); 3002076b9443SCy Schubert } 3003076b9443SCy Schubert #endif 3004076b9443SCy Schubert break; 3005076b9443SCy Schubert } 3006076b9443SCy Schubert 3007076b9443SCy Schubert /* 3008076b9443SCy Schubert ** $db status (step|sort|autoindex|vmstep) 3009076b9443SCy Schubert ** 3010076b9443SCy Schubert ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or 3011076b9443SCy Schubert ** SQLITE_STMTSTATUS_SORT for the most recent eval. 3012076b9443SCy Schubert */ 3013076b9443SCy Schubert case DB_STATUS: { 3014076b9443SCy Schubert int v; 3015076b9443SCy Schubert const char *zOp; 3016076b9443SCy Schubert if( objc!=3 ){ 3017076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "(step|sort|autoindex)"); 3018076b9443SCy Schubert return TCL_ERROR; 3019076b9443SCy Schubert } 3020076b9443SCy Schubert zOp = Tcl_GetString(objv[2]); 3021076b9443SCy Schubert if( strcmp(zOp, "step")==0 ){ 3022076b9443SCy Schubert v = pDb->nStep; 3023076b9443SCy Schubert }else if( strcmp(zOp, "sort")==0 ){ 3024076b9443SCy Schubert v = pDb->nSort; 3025076b9443SCy Schubert }else if( strcmp(zOp, "autoindex")==0 ){ 3026076b9443SCy Schubert v = pDb->nIndex; 3027076b9443SCy Schubert }else if( strcmp(zOp, "vmstep")==0 ){ 3028076b9443SCy Schubert v = pDb->nVMStep; 3029076b9443SCy Schubert }else{ 3030076b9443SCy Schubert Tcl_AppendResult(interp, 3031076b9443SCy Schubert "bad argument: should be autoindex, step, sort or vmstep", 3032076b9443SCy Schubert (char*)0); 3033076b9443SCy Schubert return TCL_ERROR; 3034076b9443SCy Schubert } 3035076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewIntObj(v)); 3036076b9443SCy Schubert break; 3037076b9443SCy Schubert } 3038076b9443SCy Schubert 3039076b9443SCy Schubert /* 3040076b9443SCy Schubert ** $db timeout MILLESECONDS 3041076b9443SCy Schubert ** 3042076b9443SCy Schubert ** Delay for the number of milliseconds specified when a file is locked. 3043076b9443SCy Schubert */ 3044076b9443SCy Schubert case DB_TIMEOUT: { 3045076b9443SCy Schubert int ms; 3046076b9443SCy Schubert if( objc!=3 ){ 3047076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); 3048076b9443SCy Schubert return TCL_ERROR; 3049076b9443SCy Schubert } 3050076b9443SCy Schubert if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; 3051076b9443SCy Schubert sqlite3_busy_timeout(pDb->db, ms); 3052076b9443SCy Schubert break; 3053076b9443SCy Schubert } 3054076b9443SCy Schubert 3055076b9443SCy Schubert /* 3056076b9443SCy Schubert ** $db total_changes 3057076b9443SCy Schubert ** 3058076b9443SCy Schubert ** Return the number of rows that were modified, inserted, or deleted 3059076b9443SCy Schubert ** since the database handle was created. 3060076b9443SCy Schubert */ 3061076b9443SCy Schubert case DB_TOTAL_CHANGES: { 3062076b9443SCy Schubert Tcl_Obj *pResult; 3063076b9443SCy Schubert if( objc!=2 ){ 3064076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, ""); 3065076b9443SCy Schubert return TCL_ERROR; 3066076b9443SCy Schubert } 3067076b9443SCy Schubert pResult = Tcl_GetObjResult(interp); 3068076b9443SCy Schubert Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db)); 3069076b9443SCy Schubert break; 3070076b9443SCy Schubert } 3071076b9443SCy Schubert 3072076b9443SCy Schubert /* $db trace ?CALLBACK? 3073076b9443SCy Schubert ** 3074076b9443SCy Schubert ** Make arrangements to invoke the CALLBACK routine for each SQL statement 3075076b9443SCy Schubert ** that is executed. The text of the SQL is appended to CALLBACK before 3076076b9443SCy Schubert ** it is executed. 3077076b9443SCy Schubert */ 3078076b9443SCy Schubert case DB_TRACE: { 3079076b9443SCy Schubert if( objc>3 ){ 3080076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 3081076b9443SCy Schubert return TCL_ERROR; 3082076b9443SCy Schubert }else if( objc==2 ){ 3083076b9443SCy Schubert if( pDb->zTrace ){ 3084076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zTrace, (char*)0); 3085076b9443SCy Schubert } 3086076b9443SCy Schubert }else{ 3087076b9443SCy Schubert char *zTrace; 3088076b9443SCy Schubert int len; 3089076b9443SCy Schubert if( pDb->zTrace ){ 3090076b9443SCy Schubert Tcl_Free(pDb->zTrace); 3091076b9443SCy Schubert } 3092076b9443SCy Schubert zTrace = Tcl_GetStringFromObj(objv[2], &len); 3093076b9443SCy Schubert if( zTrace && len>0 ){ 3094076b9443SCy Schubert pDb->zTrace = Tcl_Alloc( len + 1 ); 3095076b9443SCy Schubert memcpy(pDb->zTrace, zTrace, len+1); 3096076b9443SCy Schubert }else{ 3097076b9443SCy Schubert pDb->zTrace = 0; 3098076b9443SCy Schubert } 3099076b9443SCy Schubert #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ 3100076b9443SCy Schubert !defined(SQLITE_OMIT_DEPRECATED) 3101076b9443SCy Schubert if( pDb->zTrace ){ 3102076b9443SCy Schubert pDb->interp = interp; 3103076b9443SCy Schubert sqlite3_trace(pDb->db, DbTraceHandler, pDb); 3104076b9443SCy Schubert }else{ 3105076b9443SCy Schubert sqlite3_trace(pDb->db, 0, 0); 3106076b9443SCy Schubert } 3107076b9443SCy Schubert #endif 3108076b9443SCy Schubert } 3109076b9443SCy Schubert break; 3110076b9443SCy Schubert } 3111076b9443SCy Schubert 3112076b9443SCy Schubert /* $db trace_v2 ?CALLBACK? ?MASK? 3113076b9443SCy Schubert ** 3114076b9443SCy Schubert ** Make arrangements to invoke the CALLBACK routine for each trace event 3115076b9443SCy Schubert ** matching the mask that is generated. The parameters are appended to 3116076b9443SCy Schubert ** CALLBACK before it is executed. 3117076b9443SCy Schubert */ 3118076b9443SCy Schubert case DB_TRACE_V2: { 3119076b9443SCy Schubert if( objc>4 ){ 3120076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK? ?MASK?"); 3121076b9443SCy Schubert return TCL_ERROR; 3122076b9443SCy Schubert }else if( objc==2 ){ 3123076b9443SCy Schubert if( pDb->zTraceV2 ){ 3124076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zTraceV2, (char*)0); 3125076b9443SCy Schubert } 3126076b9443SCy Schubert }else{ 3127076b9443SCy Schubert char *zTraceV2; 3128076b9443SCy Schubert int len; 3129076b9443SCy Schubert Tcl_WideInt wMask = 0; 3130076b9443SCy Schubert if( objc==4 ){ 3131076b9443SCy Schubert static const char *TTYPE_strs[] = { 3132076b9443SCy Schubert "statement", "profile", "row", "close", 0 3133076b9443SCy Schubert }; 3134076b9443SCy Schubert enum TTYPE_enum { 3135076b9443SCy Schubert TTYPE_STMT, TTYPE_PROFILE, TTYPE_ROW, TTYPE_CLOSE 3136076b9443SCy Schubert }; 3137076b9443SCy Schubert int i; 3138076b9443SCy Schubert if( TCL_OK!=Tcl_ListObjLength(interp, objv[3], &len) ){ 3139076b9443SCy Schubert return TCL_ERROR; 3140076b9443SCy Schubert } 3141076b9443SCy Schubert for(i=0; i<len; i++){ 3142076b9443SCy Schubert Tcl_Obj *pObj; 3143076b9443SCy Schubert int ttype; 3144076b9443SCy Schubert if( TCL_OK!=Tcl_ListObjIndex(interp, objv[3], i, &pObj) ){ 3145076b9443SCy Schubert return TCL_ERROR; 3146076b9443SCy Schubert } 3147076b9443SCy Schubert if( Tcl_GetIndexFromObj(interp, pObj, TTYPE_strs, "trace type", 3148076b9443SCy Schubert 0, &ttype)!=TCL_OK ){ 3149076b9443SCy Schubert Tcl_WideInt wType; 3150076b9443SCy Schubert Tcl_Obj *pError = Tcl_DuplicateObj(Tcl_GetObjResult(interp)); 3151076b9443SCy Schubert Tcl_IncrRefCount(pError); 3152076b9443SCy Schubert if( TCL_OK==Tcl_GetWideIntFromObj(interp, pObj, &wType) ){ 3153076b9443SCy Schubert Tcl_DecrRefCount(pError); 3154076b9443SCy Schubert wMask |= wType; 3155076b9443SCy Schubert }else{ 3156076b9443SCy Schubert Tcl_SetObjResult(interp, pError); 3157076b9443SCy Schubert Tcl_DecrRefCount(pError); 3158076b9443SCy Schubert return TCL_ERROR; 3159076b9443SCy Schubert } 3160076b9443SCy Schubert }else{ 3161076b9443SCy Schubert switch( (enum TTYPE_enum)ttype ){ 3162076b9443SCy Schubert case TTYPE_STMT: wMask |= SQLITE_TRACE_STMT; break; 3163076b9443SCy Schubert case TTYPE_PROFILE: wMask |= SQLITE_TRACE_PROFILE; break; 3164076b9443SCy Schubert case TTYPE_ROW: wMask |= SQLITE_TRACE_ROW; break; 3165076b9443SCy Schubert case TTYPE_CLOSE: wMask |= SQLITE_TRACE_CLOSE; break; 3166076b9443SCy Schubert } 3167076b9443SCy Schubert } 3168076b9443SCy Schubert } 3169076b9443SCy Schubert }else{ 3170076b9443SCy Schubert wMask = SQLITE_TRACE_STMT; /* use the "legacy" default */ 3171076b9443SCy Schubert } 3172076b9443SCy Schubert if( pDb->zTraceV2 ){ 3173076b9443SCy Schubert Tcl_Free(pDb->zTraceV2); 3174076b9443SCy Schubert } 3175076b9443SCy Schubert zTraceV2 = Tcl_GetStringFromObj(objv[2], &len); 3176076b9443SCy Schubert if( zTraceV2 && len>0 ){ 3177076b9443SCy Schubert pDb->zTraceV2 = Tcl_Alloc( len + 1 ); 3178076b9443SCy Schubert memcpy(pDb->zTraceV2, zTraceV2, len+1); 3179076b9443SCy Schubert }else{ 3180076b9443SCy Schubert pDb->zTraceV2 = 0; 3181076b9443SCy Schubert } 3182076b9443SCy Schubert #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) 3183076b9443SCy Schubert if( pDb->zTraceV2 ){ 3184076b9443SCy Schubert pDb->interp = interp; 3185076b9443SCy Schubert sqlite3_trace_v2(pDb->db, (unsigned)wMask, DbTraceV2Handler, pDb); 3186076b9443SCy Schubert }else{ 3187076b9443SCy Schubert sqlite3_trace_v2(pDb->db, 0, 0, 0); 3188076b9443SCy Schubert } 3189076b9443SCy Schubert #endif 3190076b9443SCy Schubert } 3191076b9443SCy Schubert break; 3192076b9443SCy Schubert } 3193076b9443SCy Schubert 3194076b9443SCy Schubert /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT 3195076b9443SCy Schubert ** 3196076b9443SCy Schubert ** Start a new transaction (if we are not already in the midst of a 3197076b9443SCy Schubert ** transaction) and execute the TCL script SCRIPT. After SCRIPT 3198076b9443SCy Schubert ** completes, either commit the transaction or roll it back if SCRIPT 3199076b9443SCy Schubert ** throws an exception. Or if no new transation was started, do nothing. 3200076b9443SCy Schubert ** pass the exception on up the stack. 3201076b9443SCy Schubert ** 3202076b9443SCy Schubert ** This command was inspired by Dave Thomas's talk on Ruby at the 3203076b9443SCy Schubert ** 2005 O'Reilly Open Source Convention (OSCON). 3204076b9443SCy Schubert */ 3205076b9443SCy Schubert case DB_TRANSACTION: { 3206076b9443SCy Schubert Tcl_Obj *pScript; 3207076b9443SCy Schubert const char *zBegin = "SAVEPOINT _tcl_transaction"; 3208076b9443SCy Schubert if( objc!=3 && objc!=4 ){ 3209076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT"); 3210076b9443SCy Schubert return TCL_ERROR; 3211076b9443SCy Schubert } 3212076b9443SCy Schubert 3213076b9443SCy Schubert if( pDb->nTransaction==0 && objc==4 ){ 3214076b9443SCy Schubert static const char *TTYPE_strs[] = { 3215076b9443SCy Schubert "deferred", "exclusive", "immediate", 0 3216076b9443SCy Schubert }; 3217076b9443SCy Schubert enum TTYPE_enum { 3218076b9443SCy Schubert TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE 3219076b9443SCy Schubert }; 3220076b9443SCy Schubert int ttype; 3221076b9443SCy Schubert if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type", 3222076b9443SCy Schubert 0, &ttype) ){ 3223076b9443SCy Schubert return TCL_ERROR; 3224076b9443SCy Schubert } 3225076b9443SCy Schubert switch( (enum TTYPE_enum)ttype ){ 3226076b9443SCy Schubert case TTYPE_DEFERRED: /* no-op */; break; 3227076b9443SCy Schubert case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break; 3228076b9443SCy Schubert case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break; 3229076b9443SCy Schubert } 3230076b9443SCy Schubert } 3231076b9443SCy Schubert pScript = objv[objc-1]; 3232076b9443SCy Schubert 3233076b9443SCy Schubert /* Run the SQLite BEGIN command to open a transaction or savepoint. */ 3234076b9443SCy Schubert pDb->disableAuth++; 3235076b9443SCy Schubert rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0); 3236076b9443SCy Schubert pDb->disableAuth--; 3237076b9443SCy Schubert if( rc!=SQLITE_OK ){ 3238076b9443SCy Schubert Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 3239076b9443SCy Schubert return TCL_ERROR; 3240076b9443SCy Schubert } 3241076b9443SCy Schubert pDb->nTransaction++; 3242076b9443SCy Schubert 3243076b9443SCy Schubert /* If using NRE, schedule a callback to invoke the script pScript, then 3244076b9443SCy Schubert ** a second callback to commit (or rollback) the transaction or savepoint 3245076b9443SCy Schubert ** opened above. If not using NRE, evaluate the script directly, then 3246076b9443SCy Schubert ** call function DbTransPostCmd() to commit (or rollback) the transaction 3247076b9443SCy Schubert ** or savepoint. */ 3248076b9443SCy Schubert if( DbUseNre() ){ 3249076b9443SCy Schubert Tcl_NRAddCallback(interp, DbTransPostCmd, cd, 0, 0, 0); 3250076b9443SCy Schubert (void)Tcl_NREvalObj(interp, pScript, 0); 3251076b9443SCy Schubert }else{ 3252076b9443SCy Schubert rc = DbTransPostCmd(&cd, interp, Tcl_EvalObjEx(interp, pScript, 0)); 3253076b9443SCy Schubert } 3254076b9443SCy Schubert break; 3255076b9443SCy Schubert } 3256076b9443SCy Schubert 3257076b9443SCy Schubert /* 3258076b9443SCy Schubert ** $db unlock_notify ?script? 3259076b9443SCy Schubert */ 3260076b9443SCy Schubert case DB_UNLOCK_NOTIFY: { 3261076b9443SCy Schubert #ifndef SQLITE_ENABLE_UNLOCK_NOTIFY 3262076b9443SCy Schubert Tcl_AppendResult(interp, "unlock_notify not available in this build", 3263076b9443SCy Schubert (char*)0); 3264076b9443SCy Schubert rc = TCL_ERROR; 3265076b9443SCy Schubert #else 3266076b9443SCy Schubert if( objc!=2 && objc!=3 ){ 3267076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 3268076b9443SCy Schubert rc = TCL_ERROR; 3269076b9443SCy Schubert }else{ 3270076b9443SCy Schubert void (*xNotify)(void **, int) = 0; 3271076b9443SCy Schubert void *pNotifyArg = 0; 3272076b9443SCy Schubert 3273076b9443SCy Schubert if( pDb->pUnlockNotify ){ 3274076b9443SCy Schubert Tcl_DecrRefCount(pDb->pUnlockNotify); 3275076b9443SCy Schubert pDb->pUnlockNotify = 0; 3276076b9443SCy Schubert } 3277076b9443SCy Schubert 3278076b9443SCy Schubert if( objc==3 ){ 3279076b9443SCy Schubert xNotify = DbUnlockNotify; 3280076b9443SCy Schubert pNotifyArg = (void *)pDb; 3281076b9443SCy Schubert pDb->pUnlockNotify = objv[2]; 3282076b9443SCy Schubert Tcl_IncrRefCount(pDb->pUnlockNotify); 3283076b9443SCy Schubert } 3284076b9443SCy Schubert 3285076b9443SCy Schubert if( sqlite3_unlock_notify(pDb->db, xNotify, pNotifyArg) ){ 3286076b9443SCy Schubert Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 3287076b9443SCy Schubert rc = TCL_ERROR; 3288076b9443SCy Schubert } 3289076b9443SCy Schubert } 3290076b9443SCy Schubert #endif 3291076b9443SCy Schubert break; 3292076b9443SCy Schubert } 3293076b9443SCy Schubert 3294076b9443SCy Schubert /* 3295076b9443SCy Schubert ** $db preupdate_hook count 3296076b9443SCy Schubert ** $db preupdate_hook hook ?SCRIPT? 3297076b9443SCy Schubert ** $db preupdate_hook new INDEX 3298076b9443SCy Schubert ** $db preupdate_hook old INDEX 3299076b9443SCy Schubert */ 3300076b9443SCy Schubert case DB_PREUPDATE: { 3301076b9443SCy Schubert #ifndef SQLITE_ENABLE_PREUPDATE_HOOK 3302076b9443SCy Schubert Tcl_AppendResult(interp, "preupdate_hook was omitted at compile-time", 3303076b9443SCy Schubert (char*)0); 3304076b9443SCy Schubert rc = TCL_ERROR; 3305076b9443SCy Schubert #else 3306076b9443SCy Schubert static const char *azSub[] = {"count", "depth", "hook", "new", "old", 0}; 3307076b9443SCy Schubert enum DbPreupdateSubCmd { 3308076b9443SCy Schubert PRE_COUNT, PRE_DEPTH, PRE_HOOK, PRE_NEW, PRE_OLD 3309076b9443SCy Schubert }; 3310076b9443SCy Schubert int iSub; 3311076b9443SCy Schubert 3312076b9443SCy Schubert if( objc<3 ){ 3313076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "SUB-COMMAND ?ARGS?"); 3314076b9443SCy Schubert } 3315076b9443SCy Schubert if( Tcl_GetIndexFromObj(interp, objv[2], azSub, "sub-command", 0, &iSub) ){ 3316076b9443SCy Schubert return TCL_ERROR; 3317076b9443SCy Schubert } 3318076b9443SCy Schubert 3319076b9443SCy Schubert switch( (enum DbPreupdateSubCmd)iSub ){ 3320076b9443SCy Schubert case PRE_COUNT: { 3321076b9443SCy Schubert int nCol = sqlite3_preupdate_count(pDb->db); 3322076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewIntObj(nCol)); 3323076b9443SCy Schubert break; 3324076b9443SCy Schubert } 3325076b9443SCy Schubert 3326076b9443SCy Schubert case PRE_HOOK: { 3327076b9443SCy Schubert if( objc>4 ){ 3328076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "hook ?SCRIPT?"); 3329076b9443SCy Schubert return TCL_ERROR; 3330076b9443SCy Schubert } 3331076b9443SCy Schubert DbHookCmd(interp, pDb, (objc==4 ? objv[3] : 0), &pDb->pPreUpdateHook); 3332076b9443SCy Schubert break; 3333076b9443SCy Schubert } 3334076b9443SCy Schubert 3335076b9443SCy Schubert case PRE_DEPTH: { 3336076b9443SCy Schubert Tcl_Obj *pRet; 3337076b9443SCy Schubert if( objc!=3 ){ 3338076b9443SCy Schubert Tcl_WrongNumArgs(interp, 3, objv, ""); 3339076b9443SCy Schubert return TCL_ERROR; 3340076b9443SCy Schubert } 3341076b9443SCy Schubert pRet = Tcl_NewIntObj(sqlite3_preupdate_depth(pDb->db)); 3342076b9443SCy Schubert Tcl_SetObjResult(interp, pRet); 3343076b9443SCy Schubert break; 3344076b9443SCy Schubert } 3345076b9443SCy Schubert 3346076b9443SCy Schubert case PRE_NEW: 3347076b9443SCy Schubert case PRE_OLD: { 3348076b9443SCy Schubert int iIdx; 3349076b9443SCy Schubert sqlite3_value *pValue; 3350076b9443SCy Schubert if( objc!=4 ){ 3351076b9443SCy Schubert Tcl_WrongNumArgs(interp, 3, objv, "INDEX"); 3352076b9443SCy Schubert return TCL_ERROR; 3353076b9443SCy Schubert } 3354076b9443SCy Schubert if( Tcl_GetIntFromObj(interp, objv[3], &iIdx) ){ 3355076b9443SCy Schubert return TCL_ERROR; 3356076b9443SCy Schubert } 3357076b9443SCy Schubert 3358076b9443SCy Schubert if( iSub==PRE_OLD ){ 3359076b9443SCy Schubert rc = sqlite3_preupdate_old(pDb->db, iIdx, &pValue); 3360076b9443SCy Schubert }else{ 3361076b9443SCy Schubert assert( iSub==PRE_NEW ); 3362076b9443SCy Schubert rc = sqlite3_preupdate_new(pDb->db, iIdx, &pValue); 3363076b9443SCy Schubert } 3364076b9443SCy Schubert 3365076b9443SCy Schubert if( rc==SQLITE_OK ){ 3366076b9443SCy Schubert Tcl_Obj *pObj; 3367076b9443SCy Schubert pObj = Tcl_NewStringObj((char*)sqlite3_value_text(pValue), -1); 3368076b9443SCy Schubert Tcl_SetObjResult(interp, pObj); 3369076b9443SCy Schubert }else{ 3370076b9443SCy Schubert Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 3371076b9443SCy Schubert return TCL_ERROR; 3372076b9443SCy Schubert } 3373076b9443SCy Schubert } 3374076b9443SCy Schubert } 3375076b9443SCy Schubert #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 3376076b9443SCy Schubert break; 3377076b9443SCy Schubert } 3378076b9443SCy Schubert 3379076b9443SCy Schubert /* 3380076b9443SCy Schubert ** $db wal_hook ?script? 3381076b9443SCy Schubert ** $db update_hook ?script? 3382076b9443SCy Schubert ** $db rollback_hook ?script? 3383076b9443SCy Schubert */ 3384076b9443SCy Schubert case DB_WAL_HOOK: 3385076b9443SCy Schubert case DB_UPDATE_HOOK: 3386076b9443SCy Schubert case DB_ROLLBACK_HOOK: { 3387076b9443SCy Schubert /* set ppHook to point at pUpdateHook or pRollbackHook, depending on 3388076b9443SCy Schubert ** whether [$db update_hook] or [$db rollback_hook] was invoked. 3389076b9443SCy Schubert */ 3390076b9443SCy Schubert Tcl_Obj **ppHook = 0; 3391076b9443SCy Schubert if( choice==DB_WAL_HOOK ) ppHook = &pDb->pWalHook; 3392076b9443SCy Schubert if( choice==DB_UPDATE_HOOK ) ppHook = &pDb->pUpdateHook; 3393076b9443SCy Schubert if( choice==DB_ROLLBACK_HOOK ) ppHook = &pDb->pRollbackHook; 3394076b9443SCy Schubert if( objc>3 ){ 3395076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 3396076b9443SCy Schubert return TCL_ERROR; 3397076b9443SCy Schubert } 3398076b9443SCy Schubert 3399076b9443SCy Schubert DbHookCmd(interp, pDb, (objc==3 ? objv[2] : 0), ppHook); 3400076b9443SCy Schubert break; 3401076b9443SCy Schubert } 3402076b9443SCy Schubert 3403076b9443SCy Schubert /* $db version 3404076b9443SCy Schubert ** 3405076b9443SCy Schubert ** Return the version string for this database. 3406076b9443SCy Schubert */ 3407076b9443SCy Schubert case DB_VERSION: { 3408076b9443SCy Schubert int i; 3409076b9443SCy Schubert for(i=2; i<objc; i++){ 3410076b9443SCy Schubert const char *zArg = Tcl_GetString(objv[i]); 3411076b9443SCy Schubert /* Optional arguments to $db version are used for testing purpose */ 3412076b9443SCy Schubert #ifdef SQLITE_TEST 3413076b9443SCy Schubert /* $db version -use-legacy-prepare BOOLEAN 3414076b9443SCy Schubert ** 3415076b9443SCy Schubert ** Turn the use of legacy sqlite3_prepare() on or off. 3416076b9443SCy Schubert */ 3417076b9443SCy Schubert if( strcmp(zArg, "-use-legacy-prepare")==0 && i+1<objc ){ 3418076b9443SCy Schubert i++; 3419076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &pDb->bLegacyPrepare) ){ 3420076b9443SCy Schubert return TCL_ERROR; 3421076b9443SCy Schubert } 3422076b9443SCy Schubert }else 3423076b9443SCy Schubert 3424076b9443SCy Schubert /* $db version -last-stmt-ptr 3425076b9443SCy Schubert ** 3426076b9443SCy Schubert ** Return a string which is a hex encoding of the pointer to the 3427076b9443SCy Schubert ** most recent sqlite3_stmt in the statement cache. 3428076b9443SCy Schubert */ 3429076b9443SCy Schubert if( strcmp(zArg, "-last-stmt-ptr")==0 ){ 3430076b9443SCy Schubert char zBuf[100]; 3431076b9443SCy Schubert sqlite3_snprintf(sizeof(zBuf), zBuf, "%p", 3432076b9443SCy Schubert pDb->stmtList ? pDb->stmtList->pStmt: 0); 3433076b9443SCy Schubert Tcl_SetResult(interp, zBuf, TCL_VOLATILE); 3434076b9443SCy Schubert }else 3435076b9443SCy Schubert #endif /* SQLITE_TEST */ 3436076b9443SCy Schubert { 3437076b9443SCy Schubert Tcl_AppendResult(interp, "unknown argument: ", zArg, (char*)0); 3438076b9443SCy Schubert return TCL_ERROR; 3439076b9443SCy Schubert } 3440076b9443SCy Schubert } 3441076b9443SCy Schubert if( i==2 ){ 3442076b9443SCy Schubert Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC); 3443076b9443SCy Schubert } 3444076b9443SCy Schubert break; 3445076b9443SCy Schubert } 3446076b9443SCy Schubert 3447076b9443SCy Schubert 3448076b9443SCy Schubert } /* End of the SWITCH statement */ 3449076b9443SCy Schubert return rc; 3450076b9443SCy Schubert } 3451076b9443SCy Schubert 3452076b9443SCy Schubert #if SQLITE_TCL_NRE 3453076b9443SCy Schubert /* 3454076b9443SCy Schubert ** Adaptor that provides an objCmd interface to the NRE-enabled 3455076b9443SCy Schubert ** interface implementation. 3456076b9443SCy Schubert */ 3457076b9443SCy Schubert static int SQLITE_TCLAPI DbObjCmdAdaptor( 3458076b9443SCy Schubert void *cd, 3459076b9443SCy Schubert Tcl_Interp *interp, 3460076b9443SCy Schubert int objc, 3461076b9443SCy Schubert Tcl_Obj *const*objv 3462076b9443SCy Schubert ){ 3463076b9443SCy Schubert return Tcl_NRCallObjProc(interp, DbObjCmd, cd, objc, objv); 3464076b9443SCy Schubert } 3465076b9443SCy Schubert #endif /* SQLITE_TCL_NRE */ 3466076b9443SCy Schubert 3467076b9443SCy Schubert /* 3468076b9443SCy Schubert ** Issue the usage message when the "sqlite3" command arguments are 3469076b9443SCy Schubert ** incorrect. 3470076b9443SCy Schubert */ 3471076b9443SCy Schubert static int sqliteCmdUsage( 3472076b9443SCy Schubert Tcl_Interp *interp, 3473076b9443SCy Schubert Tcl_Obj *const*objv 3474076b9443SCy Schubert ){ 3475076b9443SCy Schubert Tcl_WrongNumArgs(interp, 1, objv, 3476076b9443SCy Schubert "HANDLE ?FILENAME? ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" 3477076b9443SCy Schubert " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?" 3478076b9443SCy Schubert #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3479076b9443SCy Schubert " ?-key CODECKEY?" 3480076b9443SCy Schubert #endif 3481076b9443SCy Schubert ); 3482076b9443SCy Schubert return TCL_ERROR; 3483076b9443SCy Schubert } 3484076b9443SCy Schubert 3485076b9443SCy Schubert /* 3486076b9443SCy Schubert ** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN? 3487076b9443SCy Schubert ** ?-create BOOLEAN? ?-nomutex BOOLEAN? 3488076b9443SCy Schubert ** 3489076b9443SCy Schubert ** This is the main Tcl command. When the "sqlite" Tcl command is 3490076b9443SCy Schubert ** invoked, this routine runs to process that command. 3491076b9443SCy Schubert ** 3492076b9443SCy Schubert ** The first argument, DBNAME, is an arbitrary name for a new 3493076b9443SCy Schubert ** database connection. This command creates a new command named 3494076b9443SCy Schubert ** DBNAME that is used to control that connection. The database 3495076b9443SCy Schubert ** connection is deleted when the DBNAME command is deleted. 3496076b9443SCy Schubert ** 3497076b9443SCy Schubert ** The second argument is the name of the database file. 3498076b9443SCy Schubert ** 3499076b9443SCy Schubert */ 3500076b9443SCy Schubert static int SQLITE_TCLAPI DbMain( 3501076b9443SCy Schubert void *cd, 3502076b9443SCy Schubert Tcl_Interp *interp, 3503076b9443SCy Schubert int objc, 3504076b9443SCy Schubert Tcl_Obj *const*objv 3505076b9443SCy Schubert ){ 3506076b9443SCy Schubert SqliteDb *p; 3507076b9443SCy Schubert const char *zArg; 3508076b9443SCy Schubert char *zErrMsg; 3509076b9443SCy Schubert int i; 3510076b9443SCy Schubert const char *zFile = 0; 3511076b9443SCy Schubert const char *zVfs = 0; 3512076b9443SCy Schubert int flags; 3513076b9443SCy Schubert Tcl_DString translatedFilename; 3514076b9443SCy Schubert #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3515076b9443SCy Schubert void *pKey = 0; 3516076b9443SCy Schubert int nKey = 0; 3517076b9443SCy Schubert #endif 3518076b9443SCy Schubert int rc; 3519076b9443SCy Schubert 3520076b9443SCy Schubert /* In normal use, each TCL interpreter runs in a single thread. So 3521076b9443SCy Schubert ** by default, we can turn off mutexing on SQLite database connections. 3522076b9443SCy Schubert ** However, for testing purposes it is useful to have mutexes turned 3523076b9443SCy Schubert ** on. So, by default, mutexes default off. But if compiled with 3524076b9443SCy Schubert ** SQLITE_TCL_DEFAULT_FULLMUTEX then mutexes default on. 3525076b9443SCy Schubert */ 3526076b9443SCy Schubert #ifdef SQLITE_TCL_DEFAULT_FULLMUTEX 3527076b9443SCy Schubert flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX; 3528076b9443SCy Schubert #else 3529076b9443SCy Schubert flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX; 3530076b9443SCy Schubert #endif 3531076b9443SCy Schubert 3532076b9443SCy Schubert if( objc==1 ) return sqliteCmdUsage(interp, objv); 3533076b9443SCy Schubert if( objc==2 ){ 3534076b9443SCy Schubert zArg = Tcl_GetStringFromObj(objv[1], 0); 3535076b9443SCy Schubert if( strcmp(zArg,"-version")==0 ){ 3536076b9443SCy Schubert Tcl_AppendResult(interp,sqlite3_libversion(), (char*)0); 3537076b9443SCy Schubert return TCL_OK; 3538076b9443SCy Schubert } 3539076b9443SCy Schubert if( strcmp(zArg,"-sourceid")==0 ){ 3540076b9443SCy Schubert Tcl_AppendResult(interp,sqlite3_sourceid(), (char*)0); 3541076b9443SCy Schubert return TCL_OK; 3542076b9443SCy Schubert } 3543076b9443SCy Schubert if( strcmp(zArg,"-has-codec")==0 ){ 3544076b9443SCy Schubert #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3545076b9443SCy Schubert Tcl_AppendResult(interp,"1",(char*)0); 3546076b9443SCy Schubert #else 3547076b9443SCy Schubert Tcl_AppendResult(interp,"0",(char*)0); 3548076b9443SCy Schubert #endif 3549076b9443SCy Schubert return TCL_OK; 3550076b9443SCy Schubert } 3551076b9443SCy Schubert if( zArg[0]=='-' ) return sqliteCmdUsage(interp, objv); 3552076b9443SCy Schubert } 3553076b9443SCy Schubert for(i=2; i<objc; i++){ 3554076b9443SCy Schubert zArg = Tcl_GetString(objv[i]); 3555076b9443SCy Schubert if( zArg[0]!='-' ){ 3556076b9443SCy Schubert if( zFile!=0 ) return sqliteCmdUsage(interp, objv); 3557076b9443SCy Schubert zFile = zArg; 3558076b9443SCy Schubert continue; 3559076b9443SCy Schubert } 3560076b9443SCy Schubert if( i==objc-1 ) return sqliteCmdUsage(interp, objv); 3561076b9443SCy Schubert i++; 3562076b9443SCy Schubert if( strcmp(zArg,"-key")==0 ){ 3563076b9443SCy Schubert #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3564076b9443SCy Schubert pKey = Tcl_GetByteArrayFromObj(objv[i], &nKey); 3565076b9443SCy Schubert #endif 3566076b9443SCy Schubert }else if( strcmp(zArg, "-vfs")==0 ){ 3567076b9443SCy Schubert zVfs = Tcl_GetString(objv[i]); 3568076b9443SCy Schubert }else if( strcmp(zArg, "-readonly")==0 ){ 3569076b9443SCy Schubert int b; 3570076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 3571076b9443SCy Schubert if( b ){ 3572076b9443SCy Schubert flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); 3573076b9443SCy Schubert flags |= SQLITE_OPEN_READONLY; 3574076b9443SCy Schubert }else{ 3575076b9443SCy Schubert flags &= ~SQLITE_OPEN_READONLY; 3576076b9443SCy Schubert flags |= SQLITE_OPEN_READWRITE; 3577076b9443SCy Schubert } 3578076b9443SCy Schubert }else if( strcmp(zArg, "-create")==0 ){ 3579076b9443SCy Schubert int b; 3580076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 3581076b9443SCy Schubert if( b && (flags & SQLITE_OPEN_READONLY)==0 ){ 3582076b9443SCy Schubert flags |= SQLITE_OPEN_CREATE; 3583076b9443SCy Schubert }else{ 3584076b9443SCy Schubert flags &= ~SQLITE_OPEN_CREATE; 3585076b9443SCy Schubert } 3586076b9443SCy Schubert }else if( strcmp(zArg, "-nomutex")==0 ){ 3587076b9443SCy Schubert int b; 3588076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 3589076b9443SCy Schubert if( b ){ 3590076b9443SCy Schubert flags |= SQLITE_OPEN_NOMUTEX; 3591076b9443SCy Schubert flags &= ~SQLITE_OPEN_FULLMUTEX; 3592076b9443SCy Schubert }else{ 3593076b9443SCy Schubert flags &= ~SQLITE_OPEN_NOMUTEX; 3594076b9443SCy Schubert } 3595076b9443SCy Schubert }else if( strcmp(zArg, "-fullmutex")==0 ){ 3596076b9443SCy Schubert int b; 3597076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 3598076b9443SCy Schubert if( b ){ 3599076b9443SCy Schubert flags |= SQLITE_OPEN_FULLMUTEX; 3600076b9443SCy Schubert flags &= ~SQLITE_OPEN_NOMUTEX; 3601076b9443SCy Schubert }else{ 3602076b9443SCy Schubert flags &= ~SQLITE_OPEN_FULLMUTEX; 3603076b9443SCy Schubert } 3604076b9443SCy Schubert }else if( strcmp(zArg, "-uri")==0 ){ 3605076b9443SCy Schubert int b; 3606076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 3607076b9443SCy Schubert if( b ){ 3608076b9443SCy Schubert flags |= SQLITE_OPEN_URI; 3609076b9443SCy Schubert }else{ 3610076b9443SCy Schubert flags &= ~SQLITE_OPEN_URI; 3611076b9443SCy Schubert } 3612076b9443SCy Schubert }else{ 3613076b9443SCy Schubert Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0); 3614076b9443SCy Schubert return TCL_ERROR; 3615076b9443SCy Schubert } 3616076b9443SCy Schubert } 3617076b9443SCy Schubert zErrMsg = 0; 3618076b9443SCy Schubert p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); 3619076b9443SCy Schubert memset(p, 0, sizeof(*p)); 3620076b9443SCy Schubert if( zFile==0 ) zFile = ""; 3621076b9443SCy Schubert zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); 3622076b9443SCy Schubert rc = sqlite3_open_v2(zFile, &p->db, flags, zVfs); 3623076b9443SCy Schubert Tcl_DStringFree(&translatedFilename); 3624076b9443SCy Schubert if( p->db ){ 3625076b9443SCy Schubert if( SQLITE_OK!=sqlite3_errcode(p->db) ){ 3626076b9443SCy Schubert zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); 3627076b9443SCy Schubert sqlite3_close(p->db); 3628076b9443SCy Schubert p->db = 0; 3629076b9443SCy Schubert } 3630076b9443SCy Schubert }else{ 3631076b9443SCy Schubert zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc)); 3632076b9443SCy Schubert } 3633076b9443SCy Schubert #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3634076b9443SCy Schubert if( p->db ){ 3635076b9443SCy Schubert sqlite3_key(p->db, pKey, nKey); 3636076b9443SCy Schubert } 3637076b9443SCy Schubert #endif 3638076b9443SCy Schubert if( p->db==0 ){ 3639076b9443SCy Schubert Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 3640076b9443SCy Schubert Tcl_Free((char*)p); 3641076b9443SCy Schubert sqlite3_free(zErrMsg); 3642076b9443SCy Schubert return TCL_ERROR; 3643076b9443SCy Schubert } 3644076b9443SCy Schubert p->maxStmt = NUM_PREPARED_STMTS; 3645076b9443SCy Schubert p->openFlags = flags & SQLITE_OPEN_URI; 3646076b9443SCy Schubert p->interp = interp; 3647076b9443SCy Schubert zArg = Tcl_GetStringFromObj(objv[1], 0); 3648076b9443SCy Schubert if( DbUseNre() ){ 3649076b9443SCy Schubert Tcl_NRCreateCommand(interp, zArg, DbObjCmdAdaptor, DbObjCmd, 3650076b9443SCy Schubert (char*)p, DbDeleteCmd); 3651076b9443SCy Schubert }else{ 3652076b9443SCy Schubert Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); 3653076b9443SCy Schubert } 3654076b9443SCy Schubert return TCL_OK; 3655076b9443SCy Schubert } 3656076b9443SCy Schubert 3657076b9443SCy Schubert /* 3658076b9443SCy Schubert ** Provide a dummy Tcl_InitStubs if we are using this as a static 3659076b9443SCy Schubert ** library. 3660076b9443SCy Schubert */ 3661076b9443SCy Schubert #ifndef USE_TCL_STUBS 3662076b9443SCy Schubert # undef Tcl_InitStubs 3663076b9443SCy Schubert # define Tcl_InitStubs(a,b,c) TCL_VERSION 3664076b9443SCy Schubert #endif 3665076b9443SCy Schubert 3666076b9443SCy Schubert /* 3667076b9443SCy Schubert ** Make sure we have a PACKAGE_VERSION macro defined. This will be 3668076b9443SCy Schubert ** defined automatically by the TEA makefile. But other makefiles 3669076b9443SCy Schubert ** do not define it. 3670076b9443SCy Schubert */ 3671076b9443SCy Schubert #ifndef PACKAGE_VERSION 3672076b9443SCy Schubert # define PACKAGE_VERSION SQLITE_VERSION 3673076b9443SCy Schubert #endif 3674076b9443SCy Schubert 3675076b9443SCy Schubert /* 3676076b9443SCy Schubert ** Initialize this module. 3677076b9443SCy Schubert ** 3678076b9443SCy Schubert ** This Tcl module contains only a single new Tcl command named "sqlite". 3679076b9443SCy Schubert ** (Hence there is no namespace. There is no point in using a namespace 3680076b9443SCy Schubert ** if the extension only supplies one new name!) The "sqlite" command is 3681076b9443SCy Schubert ** used to open a new SQLite database. See the DbMain() routine above 3682076b9443SCy Schubert ** for additional information. 3683076b9443SCy Schubert ** 3684076b9443SCy Schubert ** The EXTERN macros are required by TCL in order to work on windows. 3685076b9443SCy Schubert */ 3686076b9443SCy Schubert EXTERN int Sqlite3_Init(Tcl_Interp *interp){ 3687076b9443SCy Schubert int rc = Tcl_InitStubs(interp, "8.4", 0) ? TCL_OK : TCL_ERROR; 3688076b9443SCy Schubert if( rc==TCL_OK ){ 3689076b9443SCy Schubert Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); 3690076b9443SCy Schubert #ifndef SQLITE_3_SUFFIX_ONLY 3691076b9443SCy Schubert /* The "sqlite" alias is undocumented. It is here only to support 3692076b9443SCy Schubert ** legacy scripts. All new scripts should use only the "sqlite3" 3693076b9443SCy Schubert ** command. */ 3694076b9443SCy Schubert Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); 3695076b9443SCy Schubert #endif 3696076b9443SCy Schubert rc = Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION); 3697076b9443SCy Schubert } 3698076b9443SCy Schubert return rc; 3699076b9443SCy Schubert } 3700076b9443SCy Schubert EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3701076b9443SCy Schubert EXTERN int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3702076b9443SCy Schubert EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3703076b9443SCy Schubert 3704076b9443SCy Schubert /* Because it accesses the file-system and uses persistent state, SQLite 3705076b9443SCy Schubert ** is not considered appropriate for safe interpreters. Hence, we cause 3706076b9443SCy Schubert ** the _SafeInit() interfaces return TCL_ERROR. 3707076b9443SCy Schubert */ 3708076b9443SCy Schubert EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_ERROR; } 3709076b9443SCy Schubert EXTERN int Sqlite3_SafeUnload(Tcl_Interp *interp, int flags){return TCL_ERROR;} 3710076b9443SCy Schubert 3711076b9443SCy Schubert 3712076b9443SCy Schubert 3713076b9443SCy Schubert #ifndef SQLITE_3_SUFFIX_ONLY 3714076b9443SCy Schubert int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3715076b9443SCy Schubert int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3716076b9443SCy Schubert int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3717076b9443SCy Schubert int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3718076b9443SCy Schubert #endif 3719076b9443SCy Schubert 3720076b9443SCy Schubert /* 3721076b9443SCy Schubert ** If the TCLSH macro is defined, add code to make a stand-alone program. 3722076b9443SCy Schubert */ 3723076b9443SCy Schubert #if defined(TCLSH) 3724076b9443SCy Schubert 3725076b9443SCy Schubert /* This is the main routine for an ordinary TCL shell. If there are 3726076b9443SCy Schubert ** are arguments, run the first argument as a script. Otherwise, 3727076b9443SCy Schubert ** read TCL commands from standard input 3728076b9443SCy Schubert */ 3729076b9443SCy Schubert static const char *tclsh_main_loop(void){ 3730076b9443SCy Schubert static const char zMainloop[] = 3731076b9443SCy Schubert "if {[llength $argv]>=1} {\n" 3732076b9443SCy Schubert "set argv0 [lindex $argv 0]\n" 3733076b9443SCy Schubert "set argv [lrange $argv 1 end]\n" 3734076b9443SCy Schubert "source $argv0\n" 3735076b9443SCy Schubert "} else {\n" 3736076b9443SCy Schubert "set line {}\n" 3737076b9443SCy Schubert "while {![eof stdin]} {\n" 3738076b9443SCy Schubert "if {$line!=\"\"} {\n" 3739076b9443SCy Schubert "puts -nonewline \"> \"\n" 3740076b9443SCy Schubert "} else {\n" 3741076b9443SCy Schubert "puts -nonewline \"% \"\n" 3742076b9443SCy Schubert "}\n" 3743076b9443SCy Schubert "flush stdout\n" 3744076b9443SCy Schubert "append line [gets stdin]\n" 3745076b9443SCy Schubert "if {[info complete $line]} {\n" 3746076b9443SCy Schubert "if {[catch {uplevel #0 $line} result]} {\n" 3747076b9443SCy Schubert "puts stderr \"Error: $result\"\n" 3748076b9443SCy Schubert "} elseif {$result!=\"\"} {\n" 3749076b9443SCy Schubert "puts $result\n" 3750076b9443SCy Schubert "}\n" 3751076b9443SCy Schubert "set line {}\n" 3752076b9443SCy Schubert "} else {\n" 3753076b9443SCy Schubert "append line \\n\n" 3754076b9443SCy Schubert "}\n" 3755076b9443SCy Schubert "}\n" 3756076b9443SCy Schubert "}\n" 3757076b9443SCy Schubert ; 3758076b9443SCy Schubert return zMainloop; 3759076b9443SCy Schubert } 3760076b9443SCy Schubert 3761076b9443SCy Schubert #define TCLSH_MAIN main /* Needed to fake out mktclapp */ 3762076b9443SCy Schubert int SQLITE_CDECL TCLSH_MAIN(int argc, char **argv){ 3763076b9443SCy Schubert Tcl_Interp *interp; 3764076b9443SCy Schubert int i; 3765076b9443SCy Schubert const char *zScript = 0; 3766076b9443SCy Schubert char zArgc[32]; 3767076b9443SCy Schubert #if defined(TCLSH_INIT_PROC) 3768076b9443SCy Schubert extern const char *TCLSH_INIT_PROC(Tcl_Interp*); 3769076b9443SCy Schubert #endif 3770076b9443SCy Schubert 3771076b9443SCy Schubert #if !defined(_WIN32_WCE) 3772076b9443SCy Schubert if( getenv("SQLITE_DEBUG_BREAK") ){ 3773076b9443SCy Schubert if( isatty(0) && isatty(2) ){ 3774076b9443SCy Schubert fprintf(stderr, 3775076b9443SCy Schubert "attach debugger to process %d and press any key to continue.\n", 3776076b9443SCy Schubert GETPID()); 3777076b9443SCy Schubert fgetc(stdin); 3778076b9443SCy Schubert }else{ 3779076b9443SCy Schubert #if defined(_WIN32) || defined(WIN32) 3780076b9443SCy Schubert DebugBreak(); 3781076b9443SCy Schubert #elif defined(SIGTRAP) 3782076b9443SCy Schubert raise(SIGTRAP); 3783076b9443SCy Schubert #endif 3784076b9443SCy Schubert } 3785076b9443SCy Schubert } 3786076b9443SCy Schubert #endif 3787076b9443SCy Schubert 3788076b9443SCy Schubert /* Call sqlite3_shutdown() once before doing anything else. This is to 3789076b9443SCy Schubert ** test that sqlite3_shutdown() can be safely called by a process before 3790076b9443SCy Schubert ** sqlite3_initialize() is. */ 3791076b9443SCy Schubert sqlite3_shutdown(); 3792076b9443SCy Schubert 3793076b9443SCy Schubert Tcl_FindExecutable(argv[0]); 3794076b9443SCy Schubert Tcl_SetSystemEncoding(NULL, "utf-8"); 3795076b9443SCy Schubert interp = Tcl_CreateInterp(); 3796076b9443SCy Schubert Sqlite3_Init(interp); 3797076b9443SCy Schubert 3798076b9443SCy Schubert sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-1); 3799076b9443SCy Schubert Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY); 3800076b9443SCy Schubert Tcl_SetVar(interp,"argv0",argv[0],TCL_GLOBAL_ONLY); 3801076b9443SCy Schubert Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); 3802076b9443SCy Schubert for(i=1; i<argc; i++){ 3803076b9443SCy Schubert Tcl_SetVar(interp, "argv", argv[i], 3804076b9443SCy Schubert TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); 3805076b9443SCy Schubert } 3806076b9443SCy Schubert #if defined(TCLSH_INIT_PROC) 3807076b9443SCy Schubert zScript = TCLSH_INIT_PROC(interp); 3808076b9443SCy Schubert #endif 3809076b9443SCy Schubert if( zScript==0 ){ 3810076b9443SCy Schubert zScript = tclsh_main_loop(); 3811076b9443SCy Schubert } 3812076b9443SCy Schubert if( Tcl_GlobalEval(interp, zScript)!=TCL_OK ){ 3813076b9443SCy Schubert const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); 3814076b9443SCy Schubert if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp); 3815076b9443SCy Schubert fprintf(stderr,"%s: %s\n", *argv, zInfo); 3816076b9443SCy Schubert return 1; 3817076b9443SCy Schubert } 3818076b9443SCy Schubert return 0; 3819076b9443SCy Schubert } 3820076b9443SCy Schubert #endif /* TCLSH */ 3821