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; 63*ec994981SCy Schubert # ifndef SQLITE_PTRSIZE 64*ec994981SCy Schubert # if defined(__SIZEOF_POINTER__) 65*ec994981SCy Schubert # define SQLITE_PTRSIZE __SIZEOF_POINTER__ 66*ec994981SCy Schubert # elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \ 67*ec994981SCy Schubert defined(_M_ARM) || defined(__arm__) || defined(__x86) || \ 68*ec994981SCy Schubert (defined(__APPLE__) && defined(__POWERPC__)) || \ 69*ec994981SCy Schubert (defined(__TOS_AIX__) && !defined(__64BIT__)) 70*ec994981SCy Schubert # define SQLITE_PTRSIZE 4 71*ec994981SCy Schubert # else 72*ec994981SCy Schubert # define SQLITE_PTRSIZE 8 73*ec994981SCy Schubert # endif 74*ec994981SCy Schubert # endif /* SQLITE_PTRSIZE */ 75*ec994981SCy Schubert # if defined(HAVE_STDINT_H) 76*ec994981SCy Schubert typedef uintptr_t uptr; 77*ec994981SCy Schubert # elif SQLITE_PTRSIZE==4 78*ec994981SCy Schubert typedef unsigned int uptr; 79*ec994981SCy Schubert # else 80*ec994981SCy Schubert typedef sqlite3_uint64 uptr; 81*ec994981SCy Schubert # endif 82076b9443SCy Schubert #endif 83076b9443SCy Schubert #include <ctype.h> 84076b9443SCy Schubert 85076b9443SCy Schubert /* Used to get the current process ID */ 86076b9443SCy Schubert #if !defined(_WIN32) 87076b9443SCy Schubert # include <signal.h> 88076b9443SCy Schubert # include <unistd.h> 89076b9443SCy Schubert # define GETPID getpid 90076b9443SCy Schubert #elif !defined(_WIN32_WCE) 91076b9443SCy Schubert # ifndef SQLITE_AMALGAMATION 92076b9443SCy Schubert # ifndef WIN32_LEAN_AND_MEAN 93076b9443SCy Schubert # define WIN32_LEAN_AND_MEAN 94076b9443SCy Schubert # endif 95076b9443SCy Schubert # include <windows.h> 96076b9443SCy Schubert # endif 97076b9443SCy Schubert # include <io.h> 98076b9443SCy Schubert # define isatty(h) _isatty(h) 99076b9443SCy Schubert # define GETPID (int)GetCurrentProcessId 100076b9443SCy Schubert #endif 101076b9443SCy Schubert 102076b9443SCy Schubert /* 103076b9443SCy Schubert * Windows needs to know which symbols to export. Unix does not. 104076b9443SCy Schubert * BUILD_sqlite should be undefined for Unix. 105076b9443SCy Schubert */ 106076b9443SCy Schubert #ifdef BUILD_sqlite 107076b9443SCy Schubert #undef TCL_STORAGE_CLASS 108076b9443SCy Schubert #define TCL_STORAGE_CLASS DLLEXPORT 109076b9443SCy Schubert #endif /* BUILD_sqlite */ 110076b9443SCy Schubert 111076b9443SCy Schubert #define NUM_PREPARED_STMTS 10 112076b9443SCy Schubert #define MAX_PREPARED_STMTS 100 113076b9443SCy Schubert 114076b9443SCy Schubert /* Forward declaration */ 115076b9443SCy Schubert typedef struct SqliteDb SqliteDb; 116076b9443SCy Schubert 117076b9443SCy Schubert /* 118076b9443SCy Schubert ** New SQL functions can be created as TCL scripts. Each such function 119076b9443SCy Schubert ** is described by an instance of the following structure. 12002273ca8SCy Schubert ** 12102273ca8SCy Schubert ** Variable eType may be set to SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, 12202273ca8SCy Schubert ** SQLITE_BLOB or SQLITE_NULL. If it is SQLITE_NULL, then the implementation 12302273ca8SCy Schubert ** attempts to determine the type of the result based on the Tcl object. 12402273ca8SCy Schubert ** If it is SQLITE_TEXT or SQLITE_BLOB, then a text (sqlite3_result_text()) 12502273ca8SCy Schubert ** or blob (sqlite3_result_blob()) is returned. If it is SQLITE_INTEGER 12602273ca8SCy Schubert ** or SQLITE_FLOAT, then an attempt is made to return an integer or float 12702273ca8SCy Schubert ** value, falling back to float and then text if this is not possible. 128076b9443SCy Schubert */ 129076b9443SCy Schubert typedef struct SqlFunc SqlFunc; 130076b9443SCy Schubert struct SqlFunc { 131076b9443SCy Schubert Tcl_Interp *interp; /* The TCL interpret to execute the function */ 132076b9443SCy Schubert Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */ 133076b9443SCy Schubert SqliteDb *pDb; /* Database connection that owns this function */ 134076b9443SCy Schubert int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */ 13502273ca8SCy Schubert int eType; /* Type of value to return */ 136076b9443SCy Schubert char *zName; /* Name of this function */ 137076b9443SCy Schubert SqlFunc *pNext; /* Next function on the list of them all */ 138076b9443SCy Schubert }; 139076b9443SCy Schubert 140076b9443SCy Schubert /* 141076b9443SCy Schubert ** New collation sequences function can be created as TCL scripts. Each such 142076b9443SCy Schubert ** function is described by an instance of the following structure. 143076b9443SCy Schubert */ 144076b9443SCy Schubert typedef struct SqlCollate SqlCollate; 145076b9443SCy Schubert struct SqlCollate { 146076b9443SCy Schubert Tcl_Interp *interp; /* The TCL interpret to execute the function */ 147076b9443SCy Schubert char *zScript; /* The script to be run */ 148076b9443SCy Schubert SqlCollate *pNext; /* Next function on the list of them all */ 149076b9443SCy Schubert }; 150076b9443SCy Schubert 151076b9443SCy Schubert /* 152076b9443SCy Schubert ** Prepared statements are cached for faster execution. Each prepared 153076b9443SCy Schubert ** statement is described by an instance of the following structure. 154076b9443SCy Schubert */ 155076b9443SCy Schubert typedef struct SqlPreparedStmt SqlPreparedStmt; 156076b9443SCy Schubert struct SqlPreparedStmt { 157076b9443SCy Schubert SqlPreparedStmt *pNext; /* Next in linked list */ 158076b9443SCy Schubert SqlPreparedStmt *pPrev; /* Previous on the list */ 159076b9443SCy Schubert sqlite3_stmt *pStmt; /* The prepared statement */ 160076b9443SCy Schubert int nSql; /* chars in zSql[] */ 161076b9443SCy Schubert const char *zSql; /* Text of the SQL statement */ 162076b9443SCy Schubert int nParm; /* Size of apParm array */ 163076b9443SCy Schubert Tcl_Obj **apParm; /* Array of referenced object pointers */ 164076b9443SCy Schubert }; 165076b9443SCy Schubert 166076b9443SCy Schubert typedef struct IncrblobChannel IncrblobChannel; 167076b9443SCy Schubert 168076b9443SCy Schubert /* 169076b9443SCy Schubert ** There is one instance of this structure for each SQLite database 170076b9443SCy Schubert ** that has been opened by the SQLite TCL interface. 171076b9443SCy Schubert ** 172076b9443SCy Schubert ** If this module is built with SQLITE_TEST defined (to create the SQLite 173076b9443SCy Schubert ** testfixture executable), then it may be configured to use either 174076b9443SCy Schubert ** sqlite3_prepare_v2() or sqlite3_prepare() to prepare SQL statements. 175076b9443SCy Schubert ** If SqliteDb.bLegacyPrepare is true, sqlite3_prepare() is used. 176076b9443SCy Schubert */ 177076b9443SCy Schubert struct SqliteDb { 178076b9443SCy Schubert sqlite3 *db; /* The "real" database structure. MUST BE FIRST */ 179076b9443SCy Schubert Tcl_Interp *interp; /* The interpreter used for this database */ 180076b9443SCy Schubert char *zBusy; /* The busy callback routine */ 181076b9443SCy Schubert char *zCommit; /* The commit hook callback routine */ 182076b9443SCy Schubert char *zTrace; /* The trace callback routine */ 183076b9443SCy Schubert char *zTraceV2; /* The trace_v2 callback routine */ 184076b9443SCy Schubert char *zProfile; /* The profile callback routine */ 185076b9443SCy Schubert char *zProgress; /* The progress callback routine */ 18602273ca8SCy Schubert char *zBindFallback; /* Callback to invoke on a binding miss */ 187076b9443SCy Schubert char *zAuth; /* The authorization callback routine */ 188076b9443SCy Schubert int disableAuth; /* Disable the authorizer if it exists */ 189076b9443SCy Schubert char *zNull; /* Text to substitute for an SQL NULL value */ 190076b9443SCy Schubert SqlFunc *pFunc; /* List of SQL functions */ 191076b9443SCy Schubert Tcl_Obj *pUpdateHook; /* Update hook script (if any) */ 192076b9443SCy Schubert Tcl_Obj *pPreUpdateHook; /* Pre-update hook script (if any) */ 193076b9443SCy Schubert Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */ 194076b9443SCy Schubert Tcl_Obj *pWalHook; /* WAL hook script (if any) */ 195076b9443SCy Schubert Tcl_Obj *pUnlockNotify; /* Unlock notify script (if any) */ 196076b9443SCy Schubert SqlCollate *pCollate; /* List of SQL collation functions */ 197076b9443SCy Schubert int rc; /* Return code of most recent sqlite3_exec() */ 198076b9443SCy Schubert Tcl_Obj *pCollateNeeded; /* Collation needed script */ 199076b9443SCy Schubert SqlPreparedStmt *stmtList; /* List of prepared statements*/ 200076b9443SCy Schubert SqlPreparedStmt *stmtLast; /* Last statement in the list */ 201076b9443SCy Schubert int maxStmt; /* The next maximum number of stmtList */ 202076b9443SCy Schubert int nStmt; /* Number of statements in stmtList */ 203076b9443SCy Schubert IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */ 204076b9443SCy Schubert int nStep, nSort, nIndex; /* Statistics for most recent operation */ 205076b9443SCy Schubert int nVMStep; /* Another statistic for most recent operation */ 206076b9443SCy Schubert int nTransaction; /* Number of nested [transaction] methods */ 207076b9443SCy Schubert int openFlags; /* Flags used to open. (SQLITE_OPEN_URI) */ 2080197ba46SCy Schubert int nRef; /* Delete object when this reaches 0 */ 209076b9443SCy Schubert #ifdef SQLITE_TEST 210076b9443SCy Schubert int bLegacyPrepare; /* True to use sqlite3_prepare() */ 211076b9443SCy Schubert #endif 212076b9443SCy Schubert }; 213076b9443SCy Schubert 214076b9443SCy Schubert struct IncrblobChannel { 215076b9443SCy Schubert sqlite3_blob *pBlob; /* sqlite3 blob handle */ 216076b9443SCy Schubert SqliteDb *pDb; /* Associated database connection */ 217076b9443SCy Schubert int iSeek; /* Current seek offset */ 218076b9443SCy Schubert Tcl_Channel channel; /* Channel identifier */ 219076b9443SCy Schubert IncrblobChannel *pNext; /* Linked list of all open incrblob channels */ 220076b9443SCy Schubert IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */ 221076b9443SCy Schubert }; 222076b9443SCy Schubert 223076b9443SCy Schubert /* 224076b9443SCy Schubert ** Compute a string length that is limited to what can be stored in 225076b9443SCy Schubert ** lower 30 bits of a 32-bit signed integer. 226076b9443SCy Schubert */ 227076b9443SCy Schubert static int strlen30(const char *z){ 228076b9443SCy Schubert const char *z2 = z; 229076b9443SCy Schubert while( *z2 ){ z2++; } 230076b9443SCy Schubert return 0x3fffffff & (int)(z2 - z); 231076b9443SCy Schubert } 232076b9443SCy Schubert 233076b9443SCy Schubert 234076b9443SCy Schubert #ifndef SQLITE_OMIT_INCRBLOB 235076b9443SCy Schubert /* 236076b9443SCy Schubert ** Close all incrblob channels opened using database connection pDb. 237076b9443SCy Schubert ** This is called when shutting down the database connection. 238076b9443SCy Schubert */ 239076b9443SCy Schubert static void closeIncrblobChannels(SqliteDb *pDb){ 240076b9443SCy Schubert IncrblobChannel *p; 241076b9443SCy Schubert IncrblobChannel *pNext; 242076b9443SCy Schubert 243076b9443SCy Schubert for(p=pDb->pIncrblob; p; p=pNext){ 244076b9443SCy Schubert pNext = p->pNext; 245076b9443SCy Schubert 246076b9443SCy Schubert /* Note: Calling unregister here call Tcl_Close on the incrblob channel, 247076b9443SCy Schubert ** which deletes the IncrblobChannel structure at *p. So do not 248076b9443SCy Schubert ** call Tcl_Free() here. 249076b9443SCy Schubert */ 250076b9443SCy Schubert Tcl_UnregisterChannel(pDb->interp, p->channel); 251076b9443SCy Schubert } 252076b9443SCy Schubert } 253076b9443SCy Schubert 254076b9443SCy Schubert /* 255076b9443SCy Schubert ** Close an incremental blob channel. 256076b9443SCy Schubert */ 257076b9443SCy Schubert static int SQLITE_TCLAPI incrblobClose( 258076b9443SCy Schubert ClientData instanceData, 259076b9443SCy Schubert Tcl_Interp *interp 260076b9443SCy Schubert ){ 261076b9443SCy Schubert IncrblobChannel *p = (IncrblobChannel *)instanceData; 262076b9443SCy Schubert int rc = sqlite3_blob_close(p->pBlob); 263076b9443SCy Schubert sqlite3 *db = p->pDb->db; 264076b9443SCy Schubert 265076b9443SCy Schubert /* Remove the channel from the SqliteDb.pIncrblob list. */ 266076b9443SCy Schubert if( p->pNext ){ 267076b9443SCy Schubert p->pNext->pPrev = p->pPrev; 268076b9443SCy Schubert } 269076b9443SCy Schubert if( p->pPrev ){ 270076b9443SCy Schubert p->pPrev->pNext = p->pNext; 271076b9443SCy Schubert } 272076b9443SCy Schubert if( p->pDb->pIncrblob==p ){ 273076b9443SCy Schubert p->pDb->pIncrblob = p->pNext; 274076b9443SCy Schubert } 275076b9443SCy Schubert 276076b9443SCy Schubert /* Free the IncrblobChannel structure */ 277076b9443SCy Schubert Tcl_Free((char *)p); 278076b9443SCy Schubert 279076b9443SCy Schubert if( rc!=SQLITE_OK ){ 280076b9443SCy Schubert Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); 281076b9443SCy Schubert return TCL_ERROR; 282076b9443SCy Schubert } 283076b9443SCy Schubert return TCL_OK; 284076b9443SCy Schubert } 285076b9443SCy Schubert 286076b9443SCy Schubert /* 287076b9443SCy Schubert ** Read data from an incremental blob channel. 288076b9443SCy Schubert */ 289076b9443SCy Schubert static int SQLITE_TCLAPI incrblobInput( 290076b9443SCy Schubert ClientData instanceData, 291076b9443SCy Schubert char *buf, 292076b9443SCy Schubert int bufSize, 293076b9443SCy Schubert int *errorCodePtr 294076b9443SCy Schubert ){ 295076b9443SCy Schubert IncrblobChannel *p = (IncrblobChannel *)instanceData; 296076b9443SCy Schubert int nRead = bufSize; /* Number of bytes to read */ 297076b9443SCy Schubert int nBlob; /* Total size of the blob */ 298076b9443SCy Schubert int rc; /* sqlite error code */ 299076b9443SCy Schubert 300076b9443SCy Schubert nBlob = sqlite3_blob_bytes(p->pBlob); 301076b9443SCy Schubert if( (p->iSeek+nRead)>nBlob ){ 302076b9443SCy Schubert nRead = nBlob-p->iSeek; 303076b9443SCy Schubert } 304076b9443SCy Schubert if( nRead<=0 ){ 305076b9443SCy Schubert return 0; 306076b9443SCy Schubert } 307076b9443SCy Schubert 308076b9443SCy Schubert rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek); 309076b9443SCy Schubert if( rc!=SQLITE_OK ){ 310076b9443SCy Schubert *errorCodePtr = rc; 311076b9443SCy Schubert return -1; 312076b9443SCy Schubert } 313076b9443SCy Schubert 314076b9443SCy Schubert p->iSeek += nRead; 315076b9443SCy Schubert return nRead; 316076b9443SCy Schubert } 317076b9443SCy Schubert 318076b9443SCy Schubert /* 319076b9443SCy Schubert ** Write data to an incremental blob channel. 320076b9443SCy Schubert */ 321076b9443SCy Schubert static int SQLITE_TCLAPI incrblobOutput( 322076b9443SCy Schubert ClientData instanceData, 323076b9443SCy Schubert CONST char *buf, 324076b9443SCy Schubert int toWrite, 325076b9443SCy Schubert int *errorCodePtr 326076b9443SCy Schubert ){ 327076b9443SCy Schubert IncrblobChannel *p = (IncrblobChannel *)instanceData; 328076b9443SCy Schubert int nWrite = toWrite; /* Number of bytes to write */ 329076b9443SCy Schubert int nBlob; /* Total size of the blob */ 330076b9443SCy Schubert int rc; /* sqlite error code */ 331076b9443SCy Schubert 332076b9443SCy Schubert nBlob = sqlite3_blob_bytes(p->pBlob); 333076b9443SCy Schubert if( (p->iSeek+nWrite)>nBlob ){ 334076b9443SCy Schubert *errorCodePtr = EINVAL; 335076b9443SCy Schubert return -1; 336076b9443SCy Schubert } 337076b9443SCy Schubert if( nWrite<=0 ){ 338076b9443SCy Schubert return 0; 339076b9443SCy Schubert } 340076b9443SCy Schubert 341076b9443SCy Schubert rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek); 342076b9443SCy Schubert if( rc!=SQLITE_OK ){ 343076b9443SCy Schubert *errorCodePtr = EIO; 344076b9443SCy Schubert return -1; 345076b9443SCy Schubert } 346076b9443SCy Schubert 347076b9443SCy Schubert p->iSeek += nWrite; 348076b9443SCy Schubert return nWrite; 349076b9443SCy Schubert } 350076b9443SCy Schubert 351076b9443SCy Schubert /* 352076b9443SCy Schubert ** Seek an incremental blob channel. 353076b9443SCy Schubert */ 354076b9443SCy Schubert static int SQLITE_TCLAPI incrblobSeek( 355076b9443SCy Schubert ClientData instanceData, 356076b9443SCy Schubert long offset, 357076b9443SCy Schubert int seekMode, 358076b9443SCy Schubert int *errorCodePtr 359076b9443SCy Schubert ){ 360076b9443SCy Schubert IncrblobChannel *p = (IncrblobChannel *)instanceData; 361076b9443SCy Schubert 362076b9443SCy Schubert switch( seekMode ){ 363076b9443SCy Schubert case SEEK_SET: 364076b9443SCy Schubert p->iSeek = offset; 365076b9443SCy Schubert break; 366076b9443SCy Schubert case SEEK_CUR: 367076b9443SCy Schubert p->iSeek += offset; 368076b9443SCy Schubert break; 369076b9443SCy Schubert case SEEK_END: 370076b9443SCy Schubert p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset; 371076b9443SCy Schubert break; 372076b9443SCy Schubert 373076b9443SCy Schubert default: assert(!"Bad seekMode"); 374076b9443SCy Schubert } 375076b9443SCy Schubert 376076b9443SCy Schubert return p->iSeek; 377076b9443SCy Schubert } 378076b9443SCy Schubert 379076b9443SCy Schubert 380076b9443SCy Schubert static void SQLITE_TCLAPI incrblobWatch( 381076b9443SCy Schubert ClientData instanceData, 382076b9443SCy Schubert int mode 383076b9443SCy Schubert ){ 384076b9443SCy Schubert /* NO-OP */ 385076b9443SCy Schubert } 386076b9443SCy Schubert static int SQLITE_TCLAPI incrblobHandle( 387076b9443SCy Schubert ClientData instanceData, 388076b9443SCy Schubert int dir, 389076b9443SCy Schubert ClientData *hPtr 390076b9443SCy Schubert ){ 391076b9443SCy Schubert return TCL_ERROR; 392076b9443SCy Schubert } 393076b9443SCy Schubert 394076b9443SCy Schubert static Tcl_ChannelType IncrblobChannelType = { 395076b9443SCy Schubert "incrblob", /* typeName */ 396076b9443SCy Schubert TCL_CHANNEL_VERSION_2, /* version */ 397076b9443SCy Schubert incrblobClose, /* closeProc */ 398076b9443SCy Schubert incrblobInput, /* inputProc */ 399076b9443SCy Schubert incrblobOutput, /* outputProc */ 400076b9443SCy Schubert incrblobSeek, /* seekProc */ 401076b9443SCy Schubert 0, /* setOptionProc */ 402076b9443SCy Schubert 0, /* getOptionProc */ 403076b9443SCy Schubert incrblobWatch, /* watchProc (this is a no-op) */ 404076b9443SCy Schubert incrblobHandle, /* getHandleProc (always returns error) */ 405076b9443SCy Schubert 0, /* close2Proc */ 406076b9443SCy Schubert 0, /* blockModeProc */ 407076b9443SCy Schubert 0, /* flushProc */ 408076b9443SCy Schubert 0, /* handlerProc */ 409076b9443SCy Schubert 0, /* wideSeekProc */ 410076b9443SCy Schubert }; 411076b9443SCy Schubert 412076b9443SCy Schubert /* 413076b9443SCy Schubert ** Create a new incrblob channel. 414076b9443SCy Schubert */ 415076b9443SCy Schubert static int createIncrblobChannel( 416076b9443SCy Schubert Tcl_Interp *interp, 417076b9443SCy Schubert SqliteDb *pDb, 418076b9443SCy Schubert const char *zDb, 419076b9443SCy Schubert const char *zTable, 420076b9443SCy Schubert const char *zColumn, 421076b9443SCy Schubert sqlite_int64 iRow, 422076b9443SCy Schubert int isReadonly 423076b9443SCy Schubert ){ 424076b9443SCy Schubert IncrblobChannel *p; 425076b9443SCy Schubert sqlite3 *db = pDb->db; 426076b9443SCy Schubert sqlite3_blob *pBlob; 427076b9443SCy Schubert int rc; 428076b9443SCy Schubert int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE); 429076b9443SCy Schubert 430076b9443SCy Schubert /* This variable is used to name the channels: "incrblob_[incr count]" */ 431076b9443SCy Schubert static int count = 0; 432076b9443SCy Schubert char zChannel[64]; 433076b9443SCy Schubert 434076b9443SCy Schubert rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob); 435076b9443SCy Schubert if( rc!=SQLITE_OK ){ 436076b9443SCy Schubert Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 437076b9443SCy Schubert return TCL_ERROR; 438076b9443SCy Schubert } 439076b9443SCy Schubert 440076b9443SCy Schubert p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel)); 441076b9443SCy Schubert p->iSeek = 0; 442076b9443SCy Schubert p->pBlob = pBlob; 443076b9443SCy Schubert 444076b9443SCy Schubert sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count); 445076b9443SCy Schubert p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags); 446076b9443SCy Schubert Tcl_RegisterChannel(interp, p->channel); 447076b9443SCy Schubert 448076b9443SCy Schubert /* Link the new channel into the SqliteDb.pIncrblob list. */ 449076b9443SCy Schubert p->pNext = pDb->pIncrblob; 450076b9443SCy Schubert p->pPrev = 0; 451076b9443SCy Schubert if( p->pNext ){ 452076b9443SCy Schubert p->pNext->pPrev = p; 453076b9443SCy Schubert } 454076b9443SCy Schubert pDb->pIncrblob = p; 455076b9443SCy Schubert p->pDb = pDb; 456076b9443SCy Schubert 457076b9443SCy Schubert Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE); 458076b9443SCy Schubert return TCL_OK; 459076b9443SCy Schubert } 460076b9443SCy Schubert #else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */ 461076b9443SCy Schubert #define closeIncrblobChannels(pDb) 462076b9443SCy Schubert #endif 463076b9443SCy Schubert 464076b9443SCy Schubert /* 465076b9443SCy Schubert ** Look at the script prefix in pCmd. We will be executing this script 466076b9443SCy Schubert ** after first appending one or more arguments. This routine analyzes 467076b9443SCy Schubert ** the script to see if it is safe to use Tcl_EvalObjv() on the script 468076b9443SCy Schubert ** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much 469076b9443SCy Schubert ** faster. 470076b9443SCy Schubert ** 471076b9443SCy Schubert ** Scripts that are safe to use with Tcl_EvalObjv() consists of a 472076b9443SCy Schubert ** command name followed by zero or more arguments with no [...] or $ 473076b9443SCy Schubert ** or {...} or ; to be seen anywhere. Most callback scripts consist 474076b9443SCy Schubert ** of just a single procedure name and they meet this requirement. 475076b9443SCy Schubert */ 476076b9443SCy Schubert static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){ 477076b9443SCy Schubert /* We could try to do something with Tcl_Parse(). But we will instead 478076b9443SCy Schubert ** just do a search for forbidden characters. If any of the forbidden 479076b9443SCy Schubert ** characters appear in pCmd, we will report the string as unsafe. 480076b9443SCy Schubert */ 481076b9443SCy Schubert const char *z; 482076b9443SCy Schubert int n; 483076b9443SCy Schubert z = Tcl_GetStringFromObj(pCmd, &n); 484076b9443SCy Schubert while( n-- > 0 ){ 485076b9443SCy Schubert int c = *(z++); 486076b9443SCy Schubert if( c=='$' || c=='[' || c==';' ) return 0; 487076b9443SCy Schubert } 488076b9443SCy Schubert return 1; 489076b9443SCy Schubert } 490076b9443SCy Schubert 491076b9443SCy Schubert /* 492076b9443SCy Schubert ** Find an SqlFunc structure with the given name. Or create a new 493076b9443SCy Schubert ** one if an existing one cannot be found. Return a pointer to the 494076b9443SCy Schubert ** structure. 495076b9443SCy Schubert */ 496076b9443SCy Schubert static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){ 497076b9443SCy Schubert SqlFunc *p, *pNew; 498076b9443SCy Schubert int nName = strlen30(zName); 499076b9443SCy Schubert pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + nName + 1 ); 500076b9443SCy Schubert pNew->zName = (char*)&pNew[1]; 501076b9443SCy Schubert memcpy(pNew->zName, zName, nName+1); 502076b9443SCy Schubert for(p=pDb->pFunc; p; p=p->pNext){ 503076b9443SCy Schubert if( sqlite3_stricmp(p->zName, pNew->zName)==0 ){ 504076b9443SCy Schubert Tcl_Free((char*)pNew); 505076b9443SCy Schubert return p; 506076b9443SCy Schubert } 507076b9443SCy Schubert } 508076b9443SCy Schubert pNew->interp = pDb->interp; 509076b9443SCy Schubert pNew->pDb = pDb; 510076b9443SCy Schubert pNew->pScript = 0; 511076b9443SCy Schubert pNew->pNext = pDb->pFunc; 512076b9443SCy Schubert pDb->pFunc = pNew; 513076b9443SCy Schubert return pNew; 514076b9443SCy Schubert } 515076b9443SCy Schubert 516076b9443SCy Schubert /* 517076b9443SCy Schubert ** Free a single SqlPreparedStmt object. 518076b9443SCy Schubert */ 519076b9443SCy Schubert static void dbFreeStmt(SqlPreparedStmt *pStmt){ 520076b9443SCy Schubert #ifdef SQLITE_TEST 521076b9443SCy Schubert if( sqlite3_sql(pStmt->pStmt)==0 ){ 522076b9443SCy Schubert Tcl_Free((char *)pStmt->zSql); 523076b9443SCy Schubert } 524076b9443SCy Schubert #endif 525076b9443SCy Schubert sqlite3_finalize(pStmt->pStmt); 526076b9443SCy Schubert Tcl_Free((char *)pStmt); 527076b9443SCy Schubert } 528076b9443SCy Schubert 529076b9443SCy Schubert /* 530076b9443SCy Schubert ** Finalize and free a list of prepared statements 531076b9443SCy Schubert */ 532076b9443SCy Schubert static void flushStmtCache(SqliteDb *pDb){ 533076b9443SCy Schubert SqlPreparedStmt *pPreStmt; 534076b9443SCy Schubert SqlPreparedStmt *pNext; 535076b9443SCy Schubert 536076b9443SCy Schubert for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pNext){ 537076b9443SCy Schubert pNext = pPreStmt->pNext; 538076b9443SCy Schubert dbFreeStmt(pPreStmt); 539076b9443SCy Schubert } 540076b9443SCy Schubert pDb->nStmt = 0; 541076b9443SCy Schubert pDb->stmtLast = 0; 542076b9443SCy Schubert pDb->stmtList = 0; 543076b9443SCy Schubert } 544076b9443SCy Schubert 545076b9443SCy Schubert /* 5460197ba46SCy Schubert ** Increment the reference counter on the SqliteDb object. The reference 5470197ba46SCy Schubert ** should be released by calling delDatabaseRef(). 548076b9443SCy Schubert */ 5490197ba46SCy Schubert static void addDatabaseRef(SqliteDb *pDb){ 5500197ba46SCy Schubert pDb->nRef++; 5510197ba46SCy Schubert } 5520197ba46SCy Schubert 5530197ba46SCy Schubert /* 5540197ba46SCy Schubert ** Decrement the reference counter associated with the SqliteDb object. 5550197ba46SCy Schubert ** If it reaches zero, delete the object. 5560197ba46SCy Schubert */ 5570197ba46SCy Schubert static void delDatabaseRef(SqliteDb *pDb){ 5580197ba46SCy Schubert assert( pDb->nRef>0 ); 5590197ba46SCy Schubert pDb->nRef--; 5600197ba46SCy Schubert if( pDb->nRef==0 ){ 561076b9443SCy Schubert flushStmtCache(pDb); 562076b9443SCy Schubert closeIncrblobChannels(pDb); 563076b9443SCy Schubert sqlite3_close(pDb->db); 564076b9443SCy Schubert while( pDb->pFunc ){ 565076b9443SCy Schubert SqlFunc *pFunc = pDb->pFunc; 566076b9443SCy Schubert pDb->pFunc = pFunc->pNext; 567076b9443SCy Schubert assert( pFunc->pDb==pDb ); 568076b9443SCy Schubert Tcl_DecrRefCount(pFunc->pScript); 569076b9443SCy Schubert Tcl_Free((char*)pFunc); 570076b9443SCy Schubert } 571076b9443SCy Schubert while( pDb->pCollate ){ 572076b9443SCy Schubert SqlCollate *pCollate = pDb->pCollate; 573076b9443SCy Schubert pDb->pCollate = pCollate->pNext; 574076b9443SCy Schubert Tcl_Free((char*)pCollate); 575076b9443SCy Schubert } 576076b9443SCy Schubert if( pDb->zBusy ){ 577076b9443SCy Schubert Tcl_Free(pDb->zBusy); 578076b9443SCy Schubert } 579076b9443SCy Schubert if( pDb->zTrace ){ 580076b9443SCy Schubert Tcl_Free(pDb->zTrace); 581076b9443SCy Schubert } 582076b9443SCy Schubert if( pDb->zTraceV2 ){ 583076b9443SCy Schubert Tcl_Free(pDb->zTraceV2); 584076b9443SCy Schubert } 585076b9443SCy Schubert if( pDb->zProfile ){ 586076b9443SCy Schubert Tcl_Free(pDb->zProfile); 587076b9443SCy Schubert } 58802273ca8SCy Schubert if( pDb->zBindFallback ){ 58902273ca8SCy Schubert Tcl_Free(pDb->zBindFallback); 59002273ca8SCy Schubert } 591076b9443SCy Schubert if( pDb->zAuth ){ 592076b9443SCy Schubert Tcl_Free(pDb->zAuth); 593076b9443SCy Schubert } 594076b9443SCy Schubert if( pDb->zNull ){ 595076b9443SCy Schubert Tcl_Free(pDb->zNull); 596076b9443SCy Schubert } 597076b9443SCy Schubert if( pDb->pUpdateHook ){ 598076b9443SCy Schubert Tcl_DecrRefCount(pDb->pUpdateHook); 599076b9443SCy Schubert } 600076b9443SCy Schubert if( pDb->pPreUpdateHook ){ 601076b9443SCy Schubert Tcl_DecrRefCount(pDb->pPreUpdateHook); 602076b9443SCy Schubert } 603076b9443SCy Schubert if( pDb->pRollbackHook ){ 604076b9443SCy Schubert Tcl_DecrRefCount(pDb->pRollbackHook); 605076b9443SCy Schubert } 606076b9443SCy Schubert if( pDb->pWalHook ){ 607076b9443SCy Schubert Tcl_DecrRefCount(pDb->pWalHook); 608076b9443SCy Schubert } 609076b9443SCy Schubert if( pDb->pCollateNeeded ){ 610076b9443SCy Schubert Tcl_DecrRefCount(pDb->pCollateNeeded); 611076b9443SCy Schubert } 612076b9443SCy Schubert Tcl_Free((char*)pDb); 613076b9443SCy Schubert } 6140197ba46SCy Schubert } 6150197ba46SCy Schubert 6160197ba46SCy Schubert /* 6170197ba46SCy Schubert ** TCL calls this procedure when an sqlite3 database command is 6180197ba46SCy Schubert ** deleted. 6190197ba46SCy Schubert */ 6200197ba46SCy Schubert static void SQLITE_TCLAPI DbDeleteCmd(void *db){ 6210197ba46SCy Schubert SqliteDb *pDb = (SqliteDb*)db; 6220197ba46SCy Schubert delDatabaseRef(pDb); 6230197ba46SCy Schubert } 624076b9443SCy Schubert 625076b9443SCy Schubert /* 626076b9443SCy Schubert ** This routine is called when a database file is locked while trying 627076b9443SCy Schubert ** to execute SQL. 628076b9443SCy Schubert */ 629076b9443SCy Schubert static int DbBusyHandler(void *cd, int nTries){ 630076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 631076b9443SCy Schubert int rc; 632076b9443SCy Schubert char zVal[30]; 633076b9443SCy Schubert 634076b9443SCy Schubert sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries); 635076b9443SCy Schubert rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0); 636076b9443SCy Schubert if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 637076b9443SCy Schubert return 0; 638076b9443SCy Schubert } 639076b9443SCy Schubert return 1; 640076b9443SCy Schubert } 641076b9443SCy Schubert 642076b9443SCy Schubert #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 643076b9443SCy Schubert /* 644076b9443SCy Schubert ** This routine is invoked as the 'progress callback' for the database. 645076b9443SCy Schubert */ 646076b9443SCy Schubert static int DbProgressHandler(void *cd){ 647076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 648076b9443SCy Schubert int rc; 649076b9443SCy Schubert 650076b9443SCy Schubert assert( pDb->zProgress ); 651076b9443SCy Schubert rc = Tcl_Eval(pDb->interp, pDb->zProgress); 652076b9443SCy Schubert if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 653076b9443SCy Schubert return 1; 654076b9443SCy Schubert } 655076b9443SCy Schubert return 0; 656076b9443SCy Schubert } 657076b9443SCy Schubert #endif 658076b9443SCy Schubert 659076b9443SCy Schubert #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ 660076b9443SCy Schubert !defined(SQLITE_OMIT_DEPRECATED) 661076b9443SCy Schubert /* 662076b9443SCy Schubert ** This routine is called by the SQLite trace handler whenever a new 663076b9443SCy Schubert ** block of SQL is executed. The TCL script in pDb->zTrace is executed. 664076b9443SCy Schubert */ 665076b9443SCy Schubert static void DbTraceHandler(void *cd, const char *zSql){ 666076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 667076b9443SCy Schubert Tcl_DString str; 668076b9443SCy Schubert 669076b9443SCy Schubert Tcl_DStringInit(&str); 670076b9443SCy Schubert Tcl_DStringAppend(&str, pDb->zTrace, -1); 671076b9443SCy Schubert Tcl_DStringAppendElement(&str, zSql); 672076b9443SCy Schubert Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 673076b9443SCy Schubert Tcl_DStringFree(&str); 674076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 675076b9443SCy Schubert } 676076b9443SCy Schubert #endif 677076b9443SCy Schubert 678076b9443SCy Schubert #ifndef SQLITE_OMIT_TRACE 679076b9443SCy Schubert /* 680076b9443SCy Schubert ** This routine is called by the SQLite trace_v2 handler whenever a new 681076b9443SCy Schubert ** supported event is generated. Unsupported event types are ignored. 682076b9443SCy Schubert ** The TCL script in pDb->zTraceV2 is executed, with the arguments for 683076b9443SCy Schubert ** the event appended to it (as list elements). 684076b9443SCy Schubert */ 685076b9443SCy Schubert static int DbTraceV2Handler( 686076b9443SCy Schubert unsigned type, /* One of the SQLITE_TRACE_* event types. */ 687076b9443SCy Schubert void *cd, /* The original context data pointer. */ 688076b9443SCy Schubert void *pd, /* Primary event data, depends on event type. */ 689076b9443SCy Schubert void *xd /* Extra event data, depends on event type. */ 690076b9443SCy Schubert ){ 691076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 692076b9443SCy Schubert Tcl_Obj *pCmd; 693076b9443SCy Schubert 694076b9443SCy Schubert switch( type ){ 695076b9443SCy Schubert case SQLITE_TRACE_STMT: { 696076b9443SCy Schubert sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; 697076b9443SCy Schubert char *zSql = (char *)xd; 698076b9443SCy Schubert 699076b9443SCy Schubert pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 700076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 701076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 702*ec994981SCy Schubert Tcl_NewWideIntObj((Tcl_WideInt)(uptr)pStmt)); 703076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 704076b9443SCy Schubert Tcl_NewStringObj(zSql, -1)); 705076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 706076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 707076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 708076b9443SCy Schubert break; 709076b9443SCy Schubert } 710076b9443SCy Schubert case SQLITE_TRACE_PROFILE: { 711076b9443SCy Schubert sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; 712076b9443SCy Schubert sqlite3_int64 ns = *(sqlite3_int64*)xd; 713076b9443SCy Schubert 714076b9443SCy Schubert pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 715076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 716076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 717*ec994981SCy Schubert Tcl_NewWideIntObj((Tcl_WideInt)(uptr)pStmt)); 718076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 719076b9443SCy Schubert Tcl_NewWideIntObj((Tcl_WideInt)ns)); 720076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 721076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 722076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 723076b9443SCy Schubert break; 724076b9443SCy Schubert } 725076b9443SCy Schubert case SQLITE_TRACE_ROW: { 726076b9443SCy Schubert sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; 727076b9443SCy Schubert 728076b9443SCy Schubert pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 729076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 730076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 731*ec994981SCy Schubert Tcl_NewWideIntObj((Tcl_WideInt)(uptr)pStmt)); 732076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 733076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 734076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 735076b9443SCy Schubert break; 736076b9443SCy Schubert } 737076b9443SCy Schubert case SQLITE_TRACE_CLOSE: { 738076b9443SCy Schubert sqlite3 *db = (sqlite3 *)pd; 739076b9443SCy Schubert 740076b9443SCy Schubert pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 741076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 742076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 743*ec994981SCy Schubert Tcl_NewWideIntObj((Tcl_WideInt)(uptr)db)); 744076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 745076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 746076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 747076b9443SCy Schubert break; 748076b9443SCy Schubert } 749076b9443SCy Schubert } 750076b9443SCy Schubert return SQLITE_OK; 751076b9443SCy Schubert } 752076b9443SCy Schubert #endif 753076b9443SCy Schubert 754076b9443SCy Schubert #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ 755076b9443SCy Schubert !defined(SQLITE_OMIT_DEPRECATED) 756076b9443SCy Schubert /* 757076b9443SCy Schubert ** This routine is called by the SQLite profile handler after a statement 758076b9443SCy Schubert ** SQL has executed. The TCL script in pDb->zProfile is evaluated. 759076b9443SCy Schubert */ 760076b9443SCy Schubert static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){ 761076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 762076b9443SCy Schubert Tcl_DString str; 763076b9443SCy Schubert char zTm[100]; 764076b9443SCy Schubert 765076b9443SCy Schubert sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm); 766076b9443SCy Schubert Tcl_DStringInit(&str); 767076b9443SCy Schubert Tcl_DStringAppend(&str, pDb->zProfile, -1); 768076b9443SCy Schubert Tcl_DStringAppendElement(&str, zSql); 769076b9443SCy Schubert Tcl_DStringAppendElement(&str, zTm); 770076b9443SCy Schubert Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 771076b9443SCy Schubert Tcl_DStringFree(&str); 772076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 773076b9443SCy Schubert } 774076b9443SCy Schubert #endif 775076b9443SCy Schubert 776076b9443SCy Schubert /* 777076b9443SCy Schubert ** This routine is called when a transaction is committed. The 778076b9443SCy Schubert ** TCL script in pDb->zCommit is executed. If it returns non-zero or 779076b9443SCy Schubert ** if it throws an exception, the transaction is rolled back instead 780076b9443SCy Schubert ** of being committed. 781076b9443SCy Schubert */ 782076b9443SCy Schubert static int DbCommitHandler(void *cd){ 783076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 784076b9443SCy Schubert int rc; 785076b9443SCy Schubert 786076b9443SCy Schubert rc = Tcl_Eval(pDb->interp, pDb->zCommit); 787076b9443SCy Schubert if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 788076b9443SCy Schubert return 1; 789076b9443SCy Schubert } 790076b9443SCy Schubert return 0; 791076b9443SCy Schubert } 792076b9443SCy Schubert 793076b9443SCy Schubert static void DbRollbackHandler(void *clientData){ 794076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)clientData; 795076b9443SCy Schubert assert(pDb->pRollbackHook); 796076b9443SCy Schubert if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){ 797076b9443SCy Schubert Tcl_BackgroundError(pDb->interp); 798076b9443SCy Schubert } 799076b9443SCy Schubert } 800076b9443SCy Schubert 801076b9443SCy Schubert /* 802076b9443SCy Schubert ** This procedure handles wal_hook callbacks. 803076b9443SCy Schubert */ 804076b9443SCy Schubert static int DbWalHandler( 805076b9443SCy Schubert void *clientData, 806076b9443SCy Schubert sqlite3 *db, 807076b9443SCy Schubert const char *zDb, 808076b9443SCy Schubert int nEntry 809076b9443SCy Schubert ){ 810076b9443SCy Schubert int ret = SQLITE_OK; 811076b9443SCy Schubert Tcl_Obj *p; 812076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)clientData; 813076b9443SCy Schubert Tcl_Interp *interp = pDb->interp; 814076b9443SCy Schubert assert(pDb->pWalHook); 815076b9443SCy Schubert 816076b9443SCy Schubert assert( db==pDb->db ); 817076b9443SCy Schubert p = Tcl_DuplicateObj(pDb->pWalHook); 818076b9443SCy Schubert Tcl_IncrRefCount(p); 819076b9443SCy Schubert Tcl_ListObjAppendElement(interp, p, Tcl_NewStringObj(zDb, -1)); 820076b9443SCy Schubert Tcl_ListObjAppendElement(interp, p, Tcl_NewIntObj(nEntry)); 821076b9443SCy Schubert if( TCL_OK!=Tcl_EvalObjEx(interp, p, 0) 822076b9443SCy Schubert || TCL_OK!=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &ret) 823076b9443SCy Schubert ){ 824076b9443SCy Schubert Tcl_BackgroundError(interp); 825076b9443SCy Schubert } 826076b9443SCy Schubert Tcl_DecrRefCount(p); 827076b9443SCy Schubert 828076b9443SCy Schubert return ret; 829076b9443SCy Schubert } 830076b9443SCy Schubert 831076b9443SCy Schubert #if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_UNLOCK_NOTIFY) 832076b9443SCy Schubert static void setTestUnlockNotifyVars(Tcl_Interp *interp, int iArg, int nArg){ 833076b9443SCy Schubert char zBuf[64]; 834076b9443SCy Schubert sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", iArg); 835076b9443SCy Schubert Tcl_SetVar(interp, "sqlite_unlock_notify_arg", zBuf, TCL_GLOBAL_ONLY); 836076b9443SCy Schubert sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nArg); 837076b9443SCy Schubert Tcl_SetVar(interp, "sqlite_unlock_notify_argcount", zBuf, TCL_GLOBAL_ONLY); 838076b9443SCy Schubert } 839076b9443SCy Schubert #else 840076b9443SCy Schubert # define setTestUnlockNotifyVars(x,y,z) 841076b9443SCy Schubert #endif 842076b9443SCy Schubert 843076b9443SCy Schubert #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 844076b9443SCy Schubert static void DbUnlockNotify(void **apArg, int nArg){ 845076b9443SCy Schubert int i; 846076b9443SCy Schubert for(i=0; i<nArg; i++){ 847076b9443SCy Schubert const int flags = (TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); 848076b9443SCy Schubert SqliteDb *pDb = (SqliteDb *)apArg[i]; 849076b9443SCy Schubert setTestUnlockNotifyVars(pDb->interp, i, nArg); 850076b9443SCy Schubert assert( pDb->pUnlockNotify); 851076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pDb->pUnlockNotify, flags); 852076b9443SCy Schubert Tcl_DecrRefCount(pDb->pUnlockNotify); 853076b9443SCy Schubert pDb->pUnlockNotify = 0; 854076b9443SCy Schubert } 855076b9443SCy Schubert } 856076b9443SCy Schubert #endif 857076b9443SCy Schubert 858076b9443SCy Schubert #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 859076b9443SCy Schubert /* 860076b9443SCy Schubert ** Pre-update hook callback. 861076b9443SCy Schubert */ 862076b9443SCy Schubert static void DbPreUpdateHandler( 863076b9443SCy Schubert void *p, 864076b9443SCy Schubert sqlite3 *db, 865076b9443SCy Schubert int op, 866076b9443SCy Schubert const char *zDb, 867076b9443SCy Schubert const char *zTbl, 868076b9443SCy Schubert sqlite_int64 iKey1, 869076b9443SCy Schubert sqlite_int64 iKey2 870076b9443SCy Schubert ){ 871076b9443SCy Schubert SqliteDb *pDb = (SqliteDb *)p; 872076b9443SCy Schubert Tcl_Obj *pCmd; 873076b9443SCy Schubert static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"}; 874076b9443SCy Schubert 875076b9443SCy Schubert assert( (SQLITE_DELETE-1)/9 == 0 ); 876076b9443SCy Schubert assert( (SQLITE_INSERT-1)/9 == 1 ); 877076b9443SCy Schubert assert( (SQLITE_UPDATE-1)/9 == 2 ); 878076b9443SCy Schubert assert( pDb->pPreUpdateHook ); 879076b9443SCy Schubert assert( db==pDb->db ); 880076b9443SCy Schubert assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); 881076b9443SCy Schubert 882076b9443SCy Schubert pCmd = Tcl_DuplicateObj(pDb->pPreUpdateHook); 883076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 884076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1)); 885076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); 886076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); 887076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey1)); 888076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey2)); 889076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 890076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 891076b9443SCy Schubert } 892076b9443SCy Schubert #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 893076b9443SCy Schubert 894076b9443SCy Schubert static void DbUpdateHandler( 895076b9443SCy Schubert void *p, 896076b9443SCy Schubert int op, 897076b9443SCy Schubert const char *zDb, 898076b9443SCy Schubert const char *zTbl, 899076b9443SCy Schubert sqlite_int64 rowid 900076b9443SCy Schubert ){ 901076b9443SCy Schubert SqliteDb *pDb = (SqliteDb *)p; 902076b9443SCy Schubert Tcl_Obj *pCmd; 903076b9443SCy Schubert static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"}; 904076b9443SCy Schubert 905076b9443SCy Schubert assert( (SQLITE_DELETE-1)/9 == 0 ); 906076b9443SCy Schubert assert( (SQLITE_INSERT-1)/9 == 1 ); 907076b9443SCy Schubert assert( (SQLITE_UPDATE-1)/9 == 2 ); 908076b9443SCy Schubert 909076b9443SCy Schubert assert( pDb->pUpdateHook ); 910076b9443SCy Schubert assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); 911076b9443SCy Schubert 912076b9443SCy Schubert pCmd = Tcl_DuplicateObj(pDb->pUpdateHook); 913076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 914076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1)); 915076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); 916076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); 917076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid)); 918076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 919076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 920076b9443SCy Schubert } 921076b9443SCy Schubert 922076b9443SCy Schubert static void tclCollateNeeded( 923076b9443SCy Schubert void *pCtx, 924076b9443SCy Schubert sqlite3 *db, 925076b9443SCy Schubert int enc, 926076b9443SCy Schubert const char *zName 927076b9443SCy Schubert ){ 928076b9443SCy Schubert SqliteDb *pDb = (SqliteDb *)pCtx; 929076b9443SCy Schubert Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded); 930076b9443SCy Schubert Tcl_IncrRefCount(pScript); 931076b9443SCy Schubert Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1)); 932076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pScript, 0); 933076b9443SCy Schubert Tcl_DecrRefCount(pScript); 934076b9443SCy Schubert } 935076b9443SCy Schubert 936076b9443SCy Schubert /* 937076b9443SCy Schubert ** This routine is called to evaluate an SQL collation function implemented 938076b9443SCy Schubert ** using TCL script. 939076b9443SCy Schubert */ 940076b9443SCy Schubert static int tclSqlCollate( 941076b9443SCy Schubert void *pCtx, 942076b9443SCy Schubert int nA, 943076b9443SCy Schubert const void *zA, 944076b9443SCy Schubert int nB, 945076b9443SCy Schubert const void *zB 946076b9443SCy Schubert ){ 947076b9443SCy Schubert SqlCollate *p = (SqlCollate *)pCtx; 948076b9443SCy Schubert Tcl_Obj *pCmd; 949076b9443SCy Schubert 950076b9443SCy Schubert pCmd = Tcl_NewStringObj(p->zScript, -1); 951076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 952076b9443SCy Schubert Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA)); 953076b9443SCy Schubert Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB)); 954076b9443SCy Schubert Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 955076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 956076b9443SCy Schubert return (atoi(Tcl_GetStringResult(p->interp))); 957076b9443SCy Schubert } 958076b9443SCy Schubert 959076b9443SCy Schubert /* 960076b9443SCy Schubert ** This routine is called to evaluate an SQL function implemented 961076b9443SCy Schubert ** using TCL script. 962076b9443SCy Schubert */ 963076b9443SCy Schubert static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ 964076b9443SCy Schubert SqlFunc *p = sqlite3_user_data(context); 965076b9443SCy Schubert Tcl_Obj *pCmd; 966076b9443SCy Schubert int i; 967076b9443SCy Schubert int rc; 968076b9443SCy Schubert 969076b9443SCy Schubert if( argc==0 ){ 970076b9443SCy Schubert /* If there are no arguments to the function, call Tcl_EvalObjEx on the 971076b9443SCy Schubert ** script object directly. This allows the TCL compiler to generate 972076b9443SCy Schubert ** bytecode for the command on the first invocation and thus make 973076b9443SCy Schubert ** subsequent invocations much faster. */ 974076b9443SCy Schubert pCmd = p->pScript; 975076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 976076b9443SCy Schubert rc = Tcl_EvalObjEx(p->interp, pCmd, 0); 977076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 978076b9443SCy Schubert }else{ 979076b9443SCy Schubert /* If there are arguments to the function, make a shallow copy of the 980076b9443SCy Schubert ** script object, lappend the arguments, then evaluate the copy. 981076b9443SCy Schubert ** 982076b9443SCy Schubert ** By "shallow" copy, we mean only the outer list Tcl_Obj is duplicated. 983076b9443SCy Schubert ** The new Tcl_Obj contains pointers to the original list elements. 984076b9443SCy Schubert ** That way, when Tcl_EvalObjv() is run and shimmers the first element 985076b9443SCy Schubert ** of the list to tclCmdNameType, that alternate representation will 986076b9443SCy Schubert ** be preserved and reused on the next invocation. 987076b9443SCy Schubert */ 988076b9443SCy Schubert Tcl_Obj **aArg; 989076b9443SCy Schubert int nArg; 990076b9443SCy Schubert if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){ 991076b9443SCy Schubert sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 992076b9443SCy Schubert return; 993076b9443SCy Schubert } 994076b9443SCy Schubert pCmd = Tcl_NewListObj(nArg, aArg); 995076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 996076b9443SCy Schubert for(i=0; i<argc; i++){ 997076b9443SCy Schubert sqlite3_value *pIn = argv[i]; 998076b9443SCy Schubert Tcl_Obj *pVal; 999076b9443SCy Schubert 1000076b9443SCy Schubert /* Set pVal to contain the i'th column of this row. */ 1001076b9443SCy Schubert switch( sqlite3_value_type(pIn) ){ 1002076b9443SCy Schubert case SQLITE_BLOB: { 1003076b9443SCy Schubert int bytes = sqlite3_value_bytes(pIn); 1004076b9443SCy Schubert pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes); 1005076b9443SCy Schubert break; 1006076b9443SCy Schubert } 1007076b9443SCy Schubert case SQLITE_INTEGER: { 1008076b9443SCy Schubert sqlite_int64 v = sqlite3_value_int64(pIn); 1009076b9443SCy Schubert if( v>=-2147483647 && v<=2147483647 ){ 1010076b9443SCy Schubert pVal = Tcl_NewIntObj((int)v); 1011076b9443SCy Schubert }else{ 1012076b9443SCy Schubert pVal = Tcl_NewWideIntObj(v); 1013076b9443SCy Schubert } 1014076b9443SCy Schubert break; 1015076b9443SCy Schubert } 1016076b9443SCy Schubert case SQLITE_FLOAT: { 1017076b9443SCy Schubert double r = sqlite3_value_double(pIn); 1018076b9443SCy Schubert pVal = Tcl_NewDoubleObj(r); 1019076b9443SCy Schubert break; 1020076b9443SCy Schubert } 1021076b9443SCy Schubert case SQLITE_NULL: { 1022076b9443SCy Schubert pVal = Tcl_NewStringObj(p->pDb->zNull, -1); 1023076b9443SCy Schubert break; 1024076b9443SCy Schubert } 1025076b9443SCy Schubert default: { 1026076b9443SCy Schubert int bytes = sqlite3_value_bytes(pIn); 1027076b9443SCy Schubert pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes); 1028076b9443SCy Schubert break; 1029076b9443SCy Schubert } 1030076b9443SCy Schubert } 1031076b9443SCy Schubert rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal); 1032076b9443SCy Schubert if( rc ){ 1033076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 1034076b9443SCy Schubert sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 1035076b9443SCy Schubert return; 1036076b9443SCy Schubert } 1037076b9443SCy Schubert } 1038076b9443SCy Schubert if( !p->useEvalObjv ){ 1039076b9443SCy Schubert /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd 1040076b9443SCy Schubert ** is a list without a string representation. To prevent this from 1041076b9443SCy Schubert ** happening, make sure pCmd has a valid string representation */ 1042076b9443SCy Schubert Tcl_GetString(pCmd); 1043076b9443SCy Schubert } 1044076b9443SCy Schubert rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 1045076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 1046076b9443SCy Schubert } 1047076b9443SCy Schubert 1048076b9443SCy Schubert if( rc && rc!=TCL_RETURN ){ 1049076b9443SCy Schubert sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 1050076b9443SCy Schubert }else{ 1051076b9443SCy Schubert Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); 1052076b9443SCy Schubert int n; 1053076b9443SCy Schubert u8 *data; 1054076b9443SCy Schubert const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 1055076b9443SCy Schubert char c = zType[0]; 105602273ca8SCy Schubert int eType = p->eType; 105702273ca8SCy Schubert 105802273ca8SCy Schubert if( eType==SQLITE_NULL ){ 1059076b9443SCy Schubert if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ 1060076b9443SCy Schubert /* Only return a BLOB type if the Tcl variable is a bytearray and 1061076b9443SCy Schubert ** has no string representation. */ 106202273ca8SCy Schubert eType = SQLITE_BLOB; 106302273ca8SCy Schubert }else if( (c=='b' && strcmp(zType,"boolean")==0) 106402273ca8SCy Schubert || (c=='w' && strcmp(zType,"wideInt")==0) 106502273ca8SCy Schubert || (c=='i' && strcmp(zType,"int")==0) 106602273ca8SCy Schubert ){ 106702273ca8SCy Schubert eType = SQLITE_INTEGER; 106802273ca8SCy Schubert }else if( c=='d' && strcmp(zType,"double")==0 ){ 106902273ca8SCy Schubert eType = SQLITE_FLOAT; 107002273ca8SCy Schubert }else{ 107102273ca8SCy Schubert eType = SQLITE_TEXT; 107202273ca8SCy Schubert } 107302273ca8SCy Schubert } 107402273ca8SCy Schubert 107502273ca8SCy Schubert switch( eType ){ 107602273ca8SCy Schubert case SQLITE_BLOB: { 1077076b9443SCy Schubert data = Tcl_GetByteArrayFromObj(pVar, &n); 1078076b9443SCy Schubert sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT); 107902273ca8SCy Schubert break; 108002273ca8SCy Schubert } 108102273ca8SCy Schubert case SQLITE_INTEGER: { 1082076b9443SCy Schubert Tcl_WideInt v; 108302273ca8SCy Schubert if( TCL_OK==Tcl_GetWideIntFromObj(0, pVar, &v) ){ 1084076b9443SCy Schubert sqlite3_result_int64(context, v); 108502273ca8SCy Schubert break; 108602273ca8SCy Schubert } 108702273ca8SCy Schubert /* fall-through */ 108802273ca8SCy Schubert } 108902273ca8SCy Schubert case SQLITE_FLOAT: { 109002273ca8SCy Schubert double r; 109102273ca8SCy Schubert if( TCL_OK==Tcl_GetDoubleFromObj(0, pVar, &r) ){ 109202273ca8SCy Schubert sqlite3_result_double(context, r); 109302273ca8SCy Schubert break; 109402273ca8SCy Schubert } 109502273ca8SCy Schubert /* fall-through */ 109602273ca8SCy Schubert } 109702273ca8SCy Schubert default: { 1098076b9443SCy Schubert data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 1099076b9443SCy Schubert sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT); 110002273ca8SCy Schubert break; 1101076b9443SCy Schubert } 1102076b9443SCy Schubert } 110302273ca8SCy Schubert 110402273ca8SCy Schubert } 1105076b9443SCy Schubert } 1106076b9443SCy Schubert 1107076b9443SCy Schubert #ifndef SQLITE_OMIT_AUTHORIZATION 1108076b9443SCy Schubert /* 1109076b9443SCy Schubert ** This is the authentication function. It appends the authentication 1110076b9443SCy Schubert ** type code and the two arguments to zCmd[] then invokes the result 1111076b9443SCy Schubert ** on the interpreter. The reply is examined to determine if the 1112076b9443SCy Schubert ** authentication fails or succeeds. 1113076b9443SCy Schubert */ 1114076b9443SCy Schubert static int auth_callback( 1115076b9443SCy Schubert void *pArg, 1116076b9443SCy Schubert int code, 1117076b9443SCy Schubert const char *zArg1, 1118076b9443SCy Schubert const char *zArg2, 1119076b9443SCy Schubert const char *zArg3, 1120076b9443SCy Schubert const char *zArg4 1121076b9443SCy Schubert #ifdef SQLITE_USER_AUTHENTICATION 1122076b9443SCy Schubert ,const char *zArg5 1123076b9443SCy Schubert #endif 1124076b9443SCy Schubert ){ 1125076b9443SCy Schubert const char *zCode; 1126076b9443SCy Schubert Tcl_DString str; 1127076b9443SCy Schubert int rc; 1128076b9443SCy Schubert const char *zReply; 1129076b9443SCy Schubert /* EVIDENCE-OF: R-38590-62769 The first parameter to the authorizer 1130076b9443SCy Schubert ** callback is a copy of the third parameter to the 1131076b9443SCy Schubert ** sqlite3_set_authorizer() interface. 1132076b9443SCy Schubert */ 1133076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)pArg; 1134076b9443SCy Schubert if( pDb->disableAuth ) return SQLITE_OK; 1135076b9443SCy Schubert 1136076b9443SCy Schubert /* EVIDENCE-OF: R-56518-44310 The second parameter to the callback is an 1137076b9443SCy Schubert ** integer action code that specifies the particular action to be 1138076b9443SCy Schubert ** authorized. */ 1139076b9443SCy Schubert switch( code ){ 1140076b9443SCy Schubert case SQLITE_COPY : zCode="SQLITE_COPY"; break; 1141076b9443SCy Schubert case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; 1142076b9443SCy Schubert case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; 1143076b9443SCy Schubert case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; 1144076b9443SCy Schubert case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; 1145076b9443SCy Schubert case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; 1146076b9443SCy Schubert case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; 1147076b9443SCy Schubert case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; 1148076b9443SCy Schubert case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; 1149076b9443SCy Schubert case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; 1150076b9443SCy Schubert case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; 1151076b9443SCy Schubert case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; 1152076b9443SCy Schubert case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; 1153076b9443SCy Schubert case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; 1154076b9443SCy Schubert case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; 1155076b9443SCy Schubert case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; 1156076b9443SCy Schubert case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; 1157076b9443SCy Schubert case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; 1158076b9443SCy Schubert case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; 1159076b9443SCy Schubert case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; 1160076b9443SCy Schubert case SQLITE_READ : zCode="SQLITE_READ"; break; 1161076b9443SCy Schubert case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; 1162076b9443SCy Schubert case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; 1163076b9443SCy Schubert case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; 1164076b9443SCy Schubert case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; 1165076b9443SCy Schubert case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; 1166076b9443SCy Schubert case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break; 1167076b9443SCy Schubert case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break; 1168076b9443SCy Schubert case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break; 1169076b9443SCy Schubert case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break; 1170076b9443SCy Schubert case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break; 1171076b9443SCy Schubert case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break; 1172076b9443SCy Schubert case SQLITE_SAVEPOINT : zCode="SQLITE_SAVEPOINT"; break; 1173076b9443SCy Schubert case SQLITE_RECURSIVE : zCode="SQLITE_RECURSIVE"; break; 1174076b9443SCy Schubert default : zCode="????"; break; 1175076b9443SCy Schubert } 1176076b9443SCy Schubert Tcl_DStringInit(&str); 1177076b9443SCy Schubert Tcl_DStringAppend(&str, pDb->zAuth, -1); 1178076b9443SCy Schubert Tcl_DStringAppendElement(&str, zCode); 1179076b9443SCy Schubert Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); 1180076b9443SCy Schubert Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); 1181076b9443SCy Schubert Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); 1182076b9443SCy Schubert Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); 1183076b9443SCy Schubert #ifdef SQLITE_USER_AUTHENTICATION 1184076b9443SCy Schubert Tcl_DStringAppendElement(&str, zArg5 ? zArg5 : ""); 1185076b9443SCy Schubert #endif 1186076b9443SCy Schubert rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); 1187076b9443SCy Schubert Tcl_DStringFree(&str); 1188076b9443SCy Schubert zReply = rc==TCL_OK ? Tcl_GetStringResult(pDb->interp) : "SQLITE_DENY"; 1189076b9443SCy Schubert if( strcmp(zReply,"SQLITE_OK")==0 ){ 1190076b9443SCy Schubert rc = SQLITE_OK; 1191076b9443SCy Schubert }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ 1192076b9443SCy Schubert rc = SQLITE_DENY; 1193076b9443SCy Schubert }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ 1194076b9443SCy Schubert rc = SQLITE_IGNORE; 1195076b9443SCy Schubert }else{ 1196076b9443SCy Schubert rc = 999; 1197076b9443SCy Schubert } 1198076b9443SCy Schubert return rc; 1199076b9443SCy Schubert } 1200076b9443SCy Schubert #endif /* SQLITE_OMIT_AUTHORIZATION */ 1201076b9443SCy Schubert 1202076b9443SCy Schubert /* 1203076b9443SCy Schubert ** This routine reads a line of text from FILE in, stores 1204076b9443SCy Schubert ** the text in memory obtained from malloc() and returns a pointer 1205076b9443SCy Schubert ** to the text. NULL is returned at end of file, or if malloc() 1206076b9443SCy Schubert ** fails. 1207076b9443SCy Schubert ** 1208076b9443SCy Schubert ** The interface is like "readline" but no command-line editing 1209076b9443SCy Schubert ** is done. 1210076b9443SCy Schubert ** 1211076b9443SCy Schubert ** copied from shell.c from '.import' command 1212076b9443SCy Schubert */ 1213076b9443SCy Schubert static char *local_getline(char *zPrompt, FILE *in){ 1214076b9443SCy Schubert char *zLine; 1215076b9443SCy Schubert int nLine; 1216076b9443SCy Schubert int n; 1217076b9443SCy Schubert 1218076b9443SCy Schubert nLine = 100; 1219076b9443SCy Schubert zLine = malloc( nLine ); 1220076b9443SCy Schubert if( zLine==0 ) return 0; 1221076b9443SCy Schubert n = 0; 1222076b9443SCy Schubert while( 1 ){ 1223076b9443SCy Schubert if( n+100>nLine ){ 1224076b9443SCy Schubert nLine = nLine*2 + 100; 1225076b9443SCy Schubert zLine = realloc(zLine, nLine); 1226076b9443SCy Schubert if( zLine==0 ) return 0; 1227076b9443SCy Schubert } 1228076b9443SCy Schubert if( fgets(&zLine[n], nLine - n, in)==0 ){ 1229076b9443SCy Schubert if( n==0 ){ 1230076b9443SCy Schubert free(zLine); 1231076b9443SCy Schubert return 0; 1232076b9443SCy Schubert } 1233076b9443SCy Schubert zLine[n] = 0; 1234076b9443SCy Schubert break; 1235076b9443SCy Schubert } 1236076b9443SCy Schubert while( zLine[n] ){ n++; } 1237076b9443SCy Schubert if( n>0 && zLine[n-1]=='\n' ){ 1238076b9443SCy Schubert n--; 1239076b9443SCy Schubert zLine[n] = 0; 1240076b9443SCy Schubert break; 1241076b9443SCy Schubert } 1242076b9443SCy Schubert } 1243076b9443SCy Schubert zLine = realloc( zLine, n+1 ); 1244076b9443SCy Schubert return zLine; 1245076b9443SCy Schubert } 1246076b9443SCy Schubert 1247076b9443SCy Schubert 1248076b9443SCy Schubert /* 1249076b9443SCy Schubert ** This function is part of the implementation of the command: 1250076b9443SCy Schubert ** 1251076b9443SCy Schubert ** $db transaction [-deferred|-immediate|-exclusive] SCRIPT 1252076b9443SCy Schubert ** 1253076b9443SCy Schubert ** It is invoked after evaluating the script SCRIPT to commit or rollback 1254076b9443SCy Schubert ** the transaction or savepoint opened by the [transaction] command. 1255076b9443SCy Schubert */ 1256076b9443SCy Schubert static int SQLITE_TCLAPI DbTransPostCmd( 1257076b9443SCy Schubert ClientData data[], /* data[0] is the Sqlite3Db* for $db */ 1258076b9443SCy Schubert Tcl_Interp *interp, /* Tcl interpreter */ 1259076b9443SCy Schubert int result /* Result of evaluating SCRIPT */ 1260076b9443SCy Schubert ){ 1261076b9443SCy Schubert static const char *const azEnd[] = { 1262076b9443SCy Schubert "RELEASE _tcl_transaction", /* rc==TCL_ERROR, nTransaction!=0 */ 1263076b9443SCy Schubert "COMMIT", /* rc!=TCL_ERROR, nTransaction==0 */ 1264076b9443SCy Schubert "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction", 1265076b9443SCy Schubert "ROLLBACK" /* rc==TCL_ERROR, nTransaction==0 */ 1266076b9443SCy Schubert }; 1267076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)data[0]; 1268076b9443SCy Schubert int rc = result; 1269076b9443SCy Schubert const char *zEnd; 1270076b9443SCy Schubert 1271076b9443SCy Schubert pDb->nTransaction--; 1272076b9443SCy Schubert zEnd = azEnd[(rc==TCL_ERROR)*2 + (pDb->nTransaction==0)]; 1273076b9443SCy Schubert 1274076b9443SCy Schubert pDb->disableAuth++; 1275076b9443SCy Schubert if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){ 1276076b9443SCy Schubert /* This is a tricky scenario to handle. The most likely cause of an 1277076b9443SCy Schubert ** error is that the exec() above was an attempt to commit the 1278076b9443SCy Schubert ** top-level transaction that returned SQLITE_BUSY. Or, less likely, 1279076b9443SCy Schubert ** that an IO-error has occurred. In either case, throw a Tcl exception 1280076b9443SCy Schubert ** and try to rollback the transaction. 1281076b9443SCy Schubert ** 1282076b9443SCy Schubert ** But it could also be that the user executed one or more BEGIN, 1283076b9443SCy Schubert ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing 1284076b9443SCy Schubert ** this method's logic. Not clear how this would be best handled. 1285076b9443SCy Schubert */ 1286076b9443SCy Schubert if( rc!=TCL_ERROR ){ 1287076b9443SCy Schubert Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 1288076b9443SCy Schubert rc = TCL_ERROR; 1289076b9443SCy Schubert } 1290076b9443SCy Schubert sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0); 1291076b9443SCy Schubert } 1292076b9443SCy Schubert pDb->disableAuth--; 1293076b9443SCy Schubert 12940197ba46SCy Schubert delDatabaseRef(pDb); 1295076b9443SCy Schubert return rc; 1296076b9443SCy Schubert } 1297076b9443SCy Schubert 1298076b9443SCy Schubert /* 1299076b9443SCy Schubert ** Unless SQLITE_TEST is defined, this function is a simple wrapper around 1300076b9443SCy Schubert ** sqlite3_prepare_v2(). If SQLITE_TEST is defined, then it uses either 1301076b9443SCy Schubert ** sqlite3_prepare_v2() or legacy interface sqlite3_prepare(), depending 1302076b9443SCy Schubert ** on whether or not the [db_use_legacy_prepare] command has been used to 1303076b9443SCy Schubert ** configure the connection. 1304076b9443SCy Schubert */ 1305076b9443SCy Schubert static int dbPrepare( 1306076b9443SCy Schubert SqliteDb *pDb, /* Database object */ 1307076b9443SCy Schubert const char *zSql, /* SQL to compile */ 1308076b9443SCy Schubert sqlite3_stmt **ppStmt, /* OUT: Prepared statement */ 1309076b9443SCy Schubert const char **pzOut /* OUT: Pointer to next SQL statement */ 1310076b9443SCy Schubert ){ 1311076b9443SCy Schubert unsigned int prepFlags = 0; 1312076b9443SCy Schubert #ifdef SQLITE_TEST 1313076b9443SCy Schubert if( pDb->bLegacyPrepare ){ 1314076b9443SCy Schubert return sqlite3_prepare(pDb->db, zSql, -1, ppStmt, pzOut); 1315076b9443SCy Schubert } 1316076b9443SCy Schubert #endif 1317076b9443SCy Schubert /* If the statement cache is large, use the SQLITE_PREPARE_PERSISTENT 1318076b9443SCy Schubert ** flags, which uses less lookaside memory. But if the cache is small, 1319076b9443SCy Schubert ** omit that flag to make full use of lookaside */ 1320076b9443SCy Schubert if( pDb->maxStmt>5 ) prepFlags = SQLITE_PREPARE_PERSISTENT; 1321076b9443SCy Schubert 1322076b9443SCy Schubert return sqlite3_prepare_v3(pDb->db, zSql, -1, prepFlags, ppStmt, pzOut); 1323076b9443SCy Schubert } 1324076b9443SCy Schubert 1325076b9443SCy Schubert /* 1326076b9443SCy Schubert ** Search the cache for a prepared-statement object that implements the 1327076b9443SCy Schubert ** first SQL statement in the buffer pointed to by parameter zIn. If 1328076b9443SCy Schubert ** no such prepared-statement can be found, allocate and prepare a new 1329076b9443SCy Schubert ** one. In either case, bind the current values of the relevant Tcl 1330076b9443SCy Schubert ** variables to any $var, :var or @var variables in the statement. Before 1331076b9443SCy Schubert ** returning, set *ppPreStmt to point to the prepared-statement object. 1332076b9443SCy Schubert ** 1333076b9443SCy Schubert ** Output parameter *pzOut is set to point to the next SQL statement in 1334076b9443SCy Schubert ** buffer zIn, or to the '\0' byte at the end of zIn if there is no 1335076b9443SCy Schubert ** next statement. 1336076b9443SCy Schubert ** 1337076b9443SCy Schubert ** If successful, TCL_OK is returned. Otherwise, TCL_ERROR is returned 1338076b9443SCy Schubert ** and an error message loaded into interpreter pDb->interp. 1339076b9443SCy Schubert */ 1340076b9443SCy Schubert static int dbPrepareAndBind( 1341076b9443SCy Schubert SqliteDb *pDb, /* Database object */ 1342076b9443SCy Schubert char const *zIn, /* SQL to compile */ 1343076b9443SCy Schubert char const **pzOut, /* OUT: Pointer to next SQL statement */ 1344076b9443SCy Schubert SqlPreparedStmt **ppPreStmt /* OUT: Object used to cache statement */ 1345076b9443SCy Schubert ){ 1346076b9443SCy Schubert const char *zSql = zIn; /* Pointer to first SQL statement in zIn */ 1347076b9443SCy Schubert sqlite3_stmt *pStmt = 0; /* Prepared statement object */ 1348076b9443SCy Schubert SqlPreparedStmt *pPreStmt; /* Pointer to cached statement */ 1349076b9443SCy Schubert int nSql; /* Length of zSql in bytes */ 1350076b9443SCy Schubert int nVar = 0; /* Number of variables in statement */ 1351076b9443SCy Schubert int iParm = 0; /* Next free entry in apParm */ 1352076b9443SCy Schubert char c; 1353076b9443SCy Schubert int i; 135402273ca8SCy Schubert int needResultReset = 0; /* Need to invoke Tcl_ResetResult() */ 135502273ca8SCy Schubert int rc = SQLITE_OK; /* Value to return */ 1356076b9443SCy Schubert Tcl_Interp *interp = pDb->interp; 1357076b9443SCy Schubert 1358076b9443SCy Schubert *ppPreStmt = 0; 1359076b9443SCy Schubert 1360076b9443SCy Schubert /* Trim spaces from the start of zSql and calculate the remaining length. */ 1361076b9443SCy Schubert while( (c = zSql[0])==' ' || c=='\t' || c=='\r' || c=='\n' ){ zSql++; } 1362076b9443SCy Schubert nSql = strlen30(zSql); 1363076b9443SCy Schubert 1364076b9443SCy Schubert for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){ 1365076b9443SCy Schubert int n = pPreStmt->nSql; 1366076b9443SCy Schubert if( nSql>=n 1367076b9443SCy Schubert && memcmp(pPreStmt->zSql, zSql, n)==0 1368076b9443SCy Schubert && (zSql[n]==0 || zSql[n-1]==';') 1369076b9443SCy Schubert ){ 1370076b9443SCy Schubert pStmt = pPreStmt->pStmt; 1371076b9443SCy Schubert *pzOut = &zSql[pPreStmt->nSql]; 1372076b9443SCy Schubert 1373076b9443SCy Schubert /* When a prepared statement is found, unlink it from the 1374076b9443SCy Schubert ** cache list. It will later be added back to the beginning 1375076b9443SCy Schubert ** of the cache list in order to implement LRU replacement. 1376076b9443SCy Schubert */ 1377076b9443SCy Schubert if( pPreStmt->pPrev ){ 1378076b9443SCy Schubert pPreStmt->pPrev->pNext = pPreStmt->pNext; 1379076b9443SCy Schubert }else{ 1380076b9443SCy Schubert pDb->stmtList = pPreStmt->pNext; 1381076b9443SCy Schubert } 1382076b9443SCy Schubert if( pPreStmt->pNext ){ 1383076b9443SCy Schubert pPreStmt->pNext->pPrev = pPreStmt->pPrev; 1384076b9443SCy Schubert }else{ 1385076b9443SCy Schubert pDb->stmtLast = pPreStmt->pPrev; 1386076b9443SCy Schubert } 1387076b9443SCy Schubert pDb->nStmt--; 1388076b9443SCy Schubert nVar = sqlite3_bind_parameter_count(pStmt); 1389076b9443SCy Schubert break; 1390076b9443SCy Schubert } 1391076b9443SCy Schubert } 1392076b9443SCy Schubert 1393076b9443SCy Schubert /* If no prepared statement was found. Compile the SQL text. Also allocate 1394076b9443SCy Schubert ** a new SqlPreparedStmt structure. */ 1395076b9443SCy Schubert if( pPreStmt==0 ){ 1396076b9443SCy Schubert int nByte; 1397076b9443SCy Schubert 1398076b9443SCy Schubert if( SQLITE_OK!=dbPrepare(pDb, zSql, &pStmt, pzOut) ){ 1399076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 1400076b9443SCy Schubert return TCL_ERROR; 1401076b9443SCy Schubert } 1402076b9443SCy Schubert if( pStmt==0 ){ 1403076b9443SCy Schubert if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ 1404076b9443SCy Schubert /* A compile-time error in the statement. */ 1405076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 1406076b9443SCy Schubert return TCL_ERROR; 1407076b9443SCy Schubert }else{ 1408076b9443SCy Schubert /* The statement was a no-op. Continue to the next statement 1409076b9443SCy Schubert ** in the SQL string. 1410076b9443SCy Schubert */ 1411076b9443SCy Schubert return TCL_OK; 1412076b9443SCy Schubert } 1413076b9443SCy Schubert } 1414076b9443SCy Schubert 1415076b9443SCy Schubert assert( pPreStmt==0 ); 1416076b9443SCy Schubert nVar = sqlite3_bind_parameter_count(pStmt); 1417076b9443SCy Schubert nByte = sizeof(SqlPreparedStmt) + nVar*sizeof(Tcl_Obj *); 1418076b9443SCy Schubert pPreStmt = (SqlPreparedStmt*)Tcl_Alloc(nByte); 1419076b9443SCy Schubert memset(pPreStmt, 0, nByte); 1420076b9443SCy Schubert 1421076b9443SCy Schubert pPreStmt->pStmt = pStmt; 1422076b9443SCy Schubert pPreStmt->nSql = (int)(*pzOut - zSql); 1423076b9443SCy Schubert pPreStmt->zSql = sqlite3_sql(pStmt); 1424076b9443SCy Schubert pPreStmt->apParm = (Tcl_Obj **)&pPreStmt[1]; 1425076b9443SCy Schubert #ifdef SQLITE_TEST 1426076b9443SCy Schubert if( pPreStmt->zSql==0 ){ 1427076b9443SCy Schubert char *zCopy = Tcl_Alloc(pPreStmt->nSql + 1); 1428076b9443SCy Schubert memcpy(zCopy, zSql, pPreStmt->nSql); 1429076b9443SCy Schubert zCopy[pPreStmt->nSql] = '\0'; 1430076b9443SCy Schubert pPreStmt->zSql = zCopy; 1431076b9443SCy Schubert } 1432076b9443SCy Schubert #endif 1433076b9443SCy Schubert } 1434076b9443SCy Schubert assert( pPreStmt ); 1435076b9443SCy Schubert assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql ); 1436076b9443SCy Schubert assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) ); 1437076b9443SCy Schubert 1438076b9443SCy Schubert /* Bind values to parameters that begin with $ or : */ 1439076b9443SCy Schubert for(i=1; i<=nVar; i++){ 1440076b9443SCy Schubert const char *zVar = sqlite3_bind_parameter_name(pStmt, i); 1441076b9443SCy Schubert if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){ 1442076b9443SCy Schubert Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); 144302273ca8SCy Schubert if( pVar==0 && pDb->zBindFallback!=0 ){ 144402273ca8SCy Schubert Tcl_Obj *pCmd; 144502273ca8SCy Schubert int rx; 144602273ca8SCy Schubert pCmd = Tcl_NewStringObj(pDb->zBindFallback, -1); 144702273ca8SCy Schubert Tcl_IncrRefCount(pCmd); 144802273ca8SCy Schubert Tcl_ListObjAppendElement(interp, pCmd, Tcl_NewStringObj(zVar,-1)); 144902273ca8SCy Schubert if( needResultReset ) Tcl_ResetResult(interp); 145002273ca8SCy Schubert needResultReset = 1; 145102273ca8SCy Schubert rx = Tcl_EvalObjEx(interp, pCmd, TCL_EVAL_DIRECT); 145202273ca8SCy Schubert Tcl_DecrRefCount(pCmd); 145302273ca8SCy Schubert if( rx==TCL_OK ){ 145402273ca8SCy Schubert pVar = Tcl_GetObjResult(interp); 145502273ca8SCy Schubert }else if( rx==TCL_ERROR ){ 145602273ca8SCy Schubert rc = TCL_ERROR; 145702273ca8SCy Schubert break; 145802273ca8SCy Schubert }else{ 145902273ca8SCy Schubert pVar = 0; 146002273ca8SCy Schubert } 146102273ca8SCy Schubert } 1462076b9443SCy Schubert if( pVar ){ 1463076b9443SCy Schubert int n; 1464076b9443SCy Schubert u8 *data; 1465076b9443SCy Schubert const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 1466076b9443SCy Schubert c = zType[0]; 1467076b9443SCy Schubert if( zVar[0]=='@' || 1468076b9443SCy Schubert (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){ 1469076b9443SCy Schubert /* Load a BLOB type if the Tcl variable is a bytearray and 1470076b9443SCy Schubert ** it has no string representation or the host 1471076b9443SCy Schubert ** parameter name begins with "@". */ 1472076b9443SCy Schubert data = Tcl_GetByteArrayFromObj(pVar, &n); 1473076b9443SCy Schubert sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); 1474076b9443SCy Schubert Tcl_IncrRefCount(pVar); 1475076b9443SCy Schubert pPreStmt->apParm[iParm++] = pVar; 1476076b9443SCy Schubert }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 1477076b9443SCy Schubert Tcl_GetIntFromObj(interp, pVar, &n); 1478076b9443SCy Schubert sqlite3_bind_int(pStmt, i, n); 1479076b9443SCy Schubert }else if( c=='d' && strcmp(zType,"double")==0 ){ 1480076b9443SCy Schubert double r; 1481076b9443SCy Schubert Tcl_GetDoubleFromObj(interp, pVar, &r); 1482076b9443SCy Schubert sqlite3_bind_double(pStmt, i, r); 1483076b9443SCy Schubert }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 1484076b9443SCy Schubert (c=='i' && strcmp(zType,"int")==0) ){ 1485076b9443SCy Schubert Tcl_WideInt v; 1486076b9443SCy Schubert Tcl_GetWideIntFromObj(interp, pVar, &v); 1487076b9443SCy Schubert sqlite3_bind_int64(pStmt, i, v); 1488076b9443SCy Schubert }else{ 1489076b9443SCy Schubert data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 1490076b9443SCy Schubert sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); 1491076b9443SCy Schubert Tcl_IncrRefCount(pVar); 1492076b9443SCy Schubert pPreStmt->apParm[iParm++] = pVar; 1493076b9443SCy Schubert } 1494076b9443SCy Schubert }else{ 1495076b9443SCy Schubert sqlite3_bind_null(pStmt, i); 1496076b9443SCy Schubert } 149702273ca8SCy Schubert if( needResultReset ) Tcl_ResetResult(pDb->interp); 1498076b9443SCy Schubert } 1499076b9443SCy Schubert } 1500076b9443SCy Schubert pPreStmt->nParm = iParm; 1501076b9443SCy Schubert *ppPreStmt = pPreStmt; 150202273ca8SCy Schubert if( needResultReset && rc==TCL_OK ) Tcl_ResetResult(pDb->interp); 1503076b9443SCy Schubert 150402273ca8SCy Schubert return rc; 1505076b9443SCy Schubert } 1506076b9443SCy Schubert 1507076b9443SCy Schubert /* 1508076b9443SCy Schubert ** Release a statement reference obtained by calling dbPrepareAndBind(). 1509076b9443SCy Schubert ** There should be exactly one call to this function for each call to 1510076b9443SCy Schubert ** dbPrepareAndBind(). 1511076b9443SCy Schubert ** 1512076b9443SCy Schubert ** If the discard parameter is non-zero, then the statement is deleted 1513076b9443SCy Schubert ** immediately. Otherwise it is added to the LRU list and may be returned 1514076b9443SCy Schubert ** by a subsequent call to dbPrepareAndBind(). 1515076b9443SCy Schubert */ 1516076b9443SCy Schubert static void dbReleaseStmt( 1517076b9443SCy Schubert SqliteDb *pDb, /* Database handle */ 1518076b9443SCy Schubert SqlPreparedStmt *pPreStmt, /* Prepared statement handle to release */ 1519076b9443SCy Schubert int discard /* True to delete (not cache) the pPreStmt */ 1520076b9443SCy Schubert ){ 1521076b9443SCy Schubert int i; 1522076b9443SCy Schubert 1523076b9443SCy Schubert /* Free the bound string and blob parameters */ 1524076b9443SCy Schubert for(i=0; i<pPreStmt->nParm; i++){ 1525076b9443SCy Schubert Tcl_DecrRefCount(pPreStmt->apParm[i]); 1526076b9443SCy Schubert } 1527076b9443SCy Schubert pPreStmt->nParm = 0; 1528076b9443SCy Schubert 1529076b9443SCy Schubert if( pDb->maxStmt<=0 || discard ){ 1530076b9443SCy Schubert /* If the cache is turned off, deallocated the statement */ 1531076b9443SCy Schubert dbFreeStmt(pPreStmt); 1532076b9443SCy Schubert }else{ 1533076b9443SCy Schubert /* Add the prepared statement to the beginning of the cache list. */ 1534076b9443SCy Schubert pPreStmt->pNext = pDb->stmtList; 1535076b9443SCy Schubert pPreStmt->pPrev = 0; 1536076b9443SCy Schubert if( pDb->stmtList ){ 1537076b9443SCy Schubert pDb->stmtList->pPrev = pPreStmt; 1538076b9443SCy Schubert } 1539076b9443SCy Schubert pDb->stmtList = pPreStmt; 1540076b9443SCy Schubert if( pDb->stmtLast==0 ){ 1541076b9443SCy Schubert assert( pDb->nStmt==0 ); 1542076b9443SCy Schubert pDb->stmtLast = pPreStmt; 1543076b9443SCy Schubert }else{ 1544076b9443SCy Schubert assert( pDb->nStmt>0 ); 1545076b9443SCy Schubert } 1546076b9443SCy Schubert pDb->nStmt++; 1547076b9443SCy Schubert 1548076b9443SCy Schubert /* If we have too many statement in cache, remove the surplus from 1549076b9443SCy Schubert ** the end of the cache list. */ 1550076b9443SCy Schubert while( pDb->nStmt>pDb->maxStmt ){ 1551076b9443SCy Schubert SqlPreparedStmt *pLast = pDb->stmtLast; 1552076b9443SCy Schubert pDb->stmtLast = pLast->pPrev; 1553076b9443SCy Schubert pDb->stmtLast->pNext = 0; 1554076b9443SCy Schubert pDb->nStmt--; 1555076b9443SCy Schubert dbFreeStmt(pLast); 1556076b9443SCy Schubert } 1557076b9443SCy Schubert } 1558076b9443SCy Schubert } 1559076b9443SCy Schubert 1560076b9443SCy Schubert /* 1561076b9443SCy Schubert ** Structure used with dbEvalXXX() functions: 1562076b9443SCy Schubert ** 1563076b9443SCy Schubert ** dbEvalInit() 1564076b9443SCy Schubert ** dbEvalStep() 1565076b9443SCy Schubert ** dbEvalFinalize() 1566076b9443SCy Schubert ** dbEvalRowInfo() 1567076b9443SCy Schubert ** dbEvalColumnValue() 1568076b9443SCy Schubert */ 1569076b9443SCy Schubert typedef struct DbEvalContext DbEvalContext; 1570076b9443SCy Schubert struct DbEvalContext { 1571076b9443SCy Schubert SqliteDb *pDb; /* Database handle */ 1572076b9443SCy Schubert Tcl_Obj *pSql; /* Object holding string zSql */ 1573076b9443SCy Schubert const char *zSql; /* Remaining SQL to execute */ 1574076b9443SCy Schubert SqlPreparedStmt *pPreStmt; /* Current statement */ 1575076b9443SCy Schubert int nCol; /* Number of columns returned by pStmt */ 1576076b9443SCy Schubert int evalFlags; /* Flags used */ 1577076b9443SCy Schubert Tcl_Obj *pArray; /* Name of array variable */ 1578076b9443SCy Schubert Tcl_Obj **apColName; /* Array of column names */ 1579076b9443SCy Schubert }; 1580076b9443SCy Schubert 1581076b9443SCy Schubert #define SQLITE_EVAL_WITHOUTNULLS 0x00001 /* Unset array(*) for NULL */ 1582076b9443SCy Schubert 1583076b9443SCy Schubert /* 1584076b9443SCy Schubert ** Release any cache of column names currently held as part of 1585076b9443SCy Schubert ** the DbEvalContext structure passed as the first argument. 1586076b9443SCy Schubert */ 1587076b9443SCy Schubert static void dbReleaseColumnNames(DbEvalContext *p){ 1588076b9443SCy Schubert if( p->apColName ){ 1589076b9443SCy Schubert int i; 1590076b9443SCy Schubert for(i=0; i<p->nCol; i++){ 1591076b9443SCy Schubert Tcl_DecrRefCount(p->apColName[i]); 1592076b9443SCy Schubert } 1593076b9443SCy Schubert Tcl_Free((char *)p->apColName); 1594076b9443SCy Schubert p->apColName = 0; 1595076b9443SCy Schubert } 1596076b9443SCy Schubert p->nCol = 0; 1597076b9443SCy Schubert } 1598076b9443SCy Schubert 1599076b9443SCy Schubert /* 1600076b9443SCy Schubert ** Initialize a DbEvalContext structure. 1601076b9443SCy Schubert ** 1602076b9443SCy Schubert ** If pArray is not NULL, then it contains the name of a Tcl array 1603076b9443SCy Schubert ** variable. The "*" member of this array is set to a list containing 1604076b9443SCy Schubert ** the names of the columns returned by the statement as part of each 1605076b9443SCy Schubert ** call to dbEvalStep(), in order from left to right. e.g. if the names 1606076b9443SCy Schubert ** of the returned columns are a, b and c, it does the equivalent of the 1607076b9443SCy Schubert ** tcl command: 1608076b9443SCy Schubert ** 1609076b9443SCy Schubert ** set ${pArray}(*) {a b c} 1610076b9443SCy Schubert */ 1611076b9443SCy Schubert static void dbEvalInit( 1612076b9443SCy Schubert DbEvalContext *p, /* Pointer to structure to initialize */ 1613076b9443SCy Schubert SqliteDb *pDb, /* Database handle */ 1614076b9443SCy Schubert Tcl_Obj *pSql, /* Object containing SQL script */ 1615076b9443SCy Schubert Tcl_Obj *pArray, /* Name of Tcl array to set (*) element of */ 1616076b9443SCy Schubert int evalFlags /* Flags controlling evaluation */ 1617076b9443SCy Schubert ){ 1618076b9443SCy Schubert memset(p, 0, sizeof(DbEvalContext)); 1619076b9443SCy Schubert p->pDb = pDb; 1620076b9443SCy Schubert p->zSql = Tcl_GetString(pSql); 1621076b9443SCy Schubert p->pSql = pSql; 1622076b9443SCy Schubert Tcl_IncrRefCount(pSql); 1623076b9443SCy Schubert if( pArray ){ 1624076b9443SCy Schubert p->pArray = pArray; 1625076b9443SCy Schubert Tcl_IncrRefCount(pArray); 1626076b9443SCy Schubert } 1627076b9443SCy Schubert p->evalFlags = evalFlags; 16280197ba46SCy Schubert addDatabaseRef(p->pDb); 1629076b9443SCy Schubert } 1630076b9443SCy Schubert 1631076b9443SCy Schubert /* 1632076b9443SCy Schubert ** Obtain information about the row that the DbEvalContext passed as the 1633076b9443SCy Schubert ** first argument currently points to. 1634076b9443SCy Schubert */ 1635076b9443SCy Schubert static void dbEvalRowInfo( 1636076b9443SCy Schubert DbEvalContext *p, /* Evaluation context */ 1637076b9443SCy Schubert int *pnCol, /* OUT: Number of column names */ 1638076b9443SCy Schubert Tcl_Obj ***papColName /* OUT: Array of column names */ 1639076b9443SCy Schubert ){ 1640076b9443SCy Schubert /* Compute column names */ 1641076b9443SCy Schubert if( 0==p->apColName ){ 1642076b9443SCy Schubert sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 1643076b9443SCy Schubert int i; /* Iterator variable */ 1644076b9443SCy Schubert int nCol; /* Number of columns returned by pStmt */ 1645076b9443SCy Schubert Tcl_Obj **apColName = 0; /* Array of column names */ 1646076b9443SCy Schubert 1647076b9443SCy Schubert p->nCol = nCol = sqlite3_column_count(pStmt); 1648076b9443SCy Schubert if( nCol>0 && (papColName || p->pArray) ){ 1649076b9443SCy Schubert apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); 1650076b9443SCy Schubert for(i=0; i<nCol; i++){ 1651076b9443SCy Schubert apColName[i] = Tcl_NewStringObj(sqlite3_column_name(pStmt,i), -1); 1652076b9443SCy Schubert Tcl_IncrRefCount(apColName[i]); 1653076b9443SCy Schubert } 1654076b9443SCy Schubert p->apColName = apColName; 1655076b9443SCy Schubert } 1656076b9443SCy Schubert 1657076b9443SCy Schubert /* If results are being stored in an array variable, then create 1658076b9443SCy Schubert ** the array(*) entry for that array 1659076b9443SCy Schubert */ 1660076b9443SCy Schubert if( p->pArray ){ 1661076b9443SCy Schubert Tcl_Interp *interp = p->pDb->interp; 1662076b9443SCy Schubert Tcl_Obj *pColList = Tcl_NewObj(); 1663076b9443SCy Schubert Tcl_Obj *pStar = Tcl_NewStringObj("*", -1); 1664076b9443SCy Schubert 1665076b9443SCy Schubert for(i=0; i<nCol; i++){ 1666076b9443SCy Schubert Tcl_ListObjAppendElement(interp, pColList, apColName[i]); 1667076b9443SCy Schubert } 1668076b9443SCy Schubert Tcl_IncrRefCount(pStar); 1669076b9443SCy Schubert Tcl_ObjSetVar2(interp, p->pArray, pStar, pColList, 0); 1670076b9443SCy Schubert Tcl_DecrRefCount(pStar); 1671076b9443SCy Schubert } 1672076b9443SCy Schubert } 1673076b9443SCy Schubert 1674076b9443SCy Schubert if( papColName ){ 1675076b9443SCy Schubert *papColName = p->apColName; 1676076b9443SCy Schubert } 1677076b9443SCy Schubert if( pnCol ){ 1678076b9443SCy Schubert *pnCol = p->nCol; 1679076b9443SCy Schubert } 1680076b9443SCy Schubert } 1681076b9443SCy Schubert 1682076b9443SCy Schubert /* 1683076b9443SCy Schubert ** Return one of TCL_OK, TCL_BREAK or TCL_ERROR. If TCL_ERROR is 1684076b9443SCy Schubert ** returned, then an error message is stored in the interpreter before 1685076b9443SCy Schubert ** returning. 1686076b9443SCy Schubert ** 1687076b9443SCy Schubert ** A return value of TCL_OK means there is a row of data available. The 1688076b9443SCy Schubert ** data may be accessed using dbEvalRowInfo() and dbEvalColumnValue(). This 1689076b9443SCy Schubert ** is analogous to a return of SQLITE_ROW from sqlite3_step(). If TCL_BREAK 1690076b9443SCy Schubert ** is returned, then the SQL script has finished executing and there are 1691076b9443SCy Schubert ** no further rows available. This is similar to SQLITE_DONE. 1692076b9443SCy Schubert */ 1693076b9443SCy Schubert static int dbEvalStep(DbEvalContext *p){ 1694076b9443SCy Schubert const char *zPrevSql = 0; /* Previous value of p->zSql */ 1695076b9443SCy Schubert 1696076b9443SCy Schubert while( p->zSql[0] || p->pPreStmt ){ 1697076b9443SCy Schubert int rc; 1698076b9443SCy Schubert if( p->pPreStmt==0 ){ 1699076b9443SCy Schubert zPrevSql = (p->zSql==zPrevSql ? 0 : p->zSql); 1700076b9443SCy Schubert rc = dbPrepareAndBind(p->pDb, p->zSql, &p->zSql, &p->pPreStmt); 1701076b9443SCy Schubert if( rc!=TCL_OK ) return rc; 1702076b9443SCy Schubert }else{ 1703076b9443SCy Schubert int rcs; 1704076b9443SCy Schubert SqliteDb *pDb = p->pDb; 1705076b9443SCy Schubert SqlPreparedStmt *pPreStmt = p->pPreStmt; 1706076b9443SCy Schubert sqlite3_stmt *pStmt = pPreStmt->pStmt; 1707076b9443SCy Schubert 1708076b9443SCy Schubert rcs = sqlite3_step(pStmt); 1709076b9443SCy Schubert if( rcs==SQLITE_ROW ){ 1710076b9443SCy Schubert return TCL_OK; 1711076b9443SCy Schubert } 1712076b9443SCy Schubert if( p->pArray ){ 1713076b9443SCy Schubert dbEvalRowInfo(p, 0, 0); 1714076b9443SCy Schubert } 1715076b9443SCy Schubert rcs = sqlite3_reset(pStmt); 1716076b9443SCy Schubert 1717076b9443SCy Schubert pDb->nStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_FULLSCAN_STEP,1); 1718076b9443SCy Schubert pDb->nSort = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_SORT,1); 1719076b9443SCy Schubert pDb->nIndex = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_AUTOINDEX,1); 1720076b9443SCy Schubert pDb->nVMStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_VM_STEP,1); 1721076b9443SCy Schubert dbReleaseColumnNames(p); 1722076b9443SCy Schubert p->pPreStmt = 0; 1723076b9443SCy Schubert 1724076b9443SCy Schubert if( rcs!=SQLITE_OK ){ 1725076b9443SCy Schubert /* If a run-time error occurs, report the error and stop reading 1726076b9443SCy Schubert ** the SQL. */ 1727076b9443SCy Schubert dbReleaseStmt(pDb, pPreStmt, 1); 1728076b9443SCy Schubert #if SQLITE_TEST 1729076b9443SCy Schubert if( p->pDb->bLegacyPrepare && rcs==SQLITE_SCHEMA && zPrevSql ){ 1730076b9443SCy Schubert /* If the runtime error was an SQLITE_SCHEMA, and the database 1731076b9443SCy Schubert ** handle is configured to use the legacy sqlite3_prepare() 1732076b9443SCy Schubert ** interface, retry prepare()/step() on the same SQL statement. 1733076b9443SCy Schubert ** This only happens once. If there is a second SQLITE_SCHEMA 1734076b9443SCy Schubert ** error, the error will be returned to the caller. */ 1735076b9443SCy Schubert p->zSql = zPrevSql; 1736076b9443SCy Schubert continue; 1737076b9443SCy Schubert } 1738076b9443SCy Schubert #endif 1739076b9443SCy Schubert Tcl_SetObjResult(pDb->interp, 1740076b9443SCy Schubert Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 1741076b9443SCy Schubert return TCL_ERROR; 1742076b9443SCy Schubert }else{ 1743076b9443SCy Schubert dbReleaseStmt(pDb, pPreStmt, 0); 1744076b9443SCy Schubert } 1745076b9443SCy Schubert } 1746076b9443SCy Schubert } 1747076b9443SCy Schubert 1748076b9443SCy Schubert /* Finished */ 1749076b9443SCy Schubert return TCL_BREAK; 1750076b9443SCy Schubert } 1751076b9443SCy Schubert 1752076b9443SCy Schubert /* 1753076b9443SCy Schubert ** Free all resources currently held by the DbEvalContext structure passed 1754076b9443SCy Schubert ** as the first argument. There should be exactly one call to this function 1755076b9443SCy Schubert ** for each call to dbEvalInit(). 1756076b9443SCy Schubert */ 1757076b9443SCy Schubert static void dbEvalFinalize(DbEvalContext *p){ 1758076b9443SCy Schubert if( p->pPreStmt ){ 1759076b9443SCy Schubert sqlite3_reset(p->pPreStmt->pStmt); 1760076b9443SCy Schubert dbReleaseStmt(p->pDb, p->pPreStmt, 0); 1761076b9443SCy Schubert p->pPreStmt = 0; 1762076b9443SCy Schubert } 1763076b9443SCy Schubert if( p->pArray ){ 1764076b9443SCy Schubert Tcl_DecrRefCount(p->pArray); 1765076b9443SCy Schubert p->pArray = 0; 1766076b9443SCy Schubert } 1767076b9443SCy Schubert Tcl_DecrRefCount(p->pSql); 1768076b9443SCy Schubert dbReleaseColumnNames(p); 17690197ba46SCy Schubert delDatabaseRef(p->pDb); 1770076b9443SCy Schubert } 1771076b9443SCy Schubert 1772076b9443SCy Schubert /* 1773076b9443SCy Schubert ** Return a pointer to a Tcl_Obj structure with ref-count 0 that contains 1774076b9443SCy Schubert ** the value for the iCol'th column of the row currently pointed to by 1775076b9443SCy Schubert ** the DbEvalContext structure passed as the first argument. 1776076b9443SCy Schubert */ 1777076b9443SCy Schubert static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){ 1778076b9443SCy Schubert sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 1779076b9443SCy Schubert switch( sqlite3_column_type(pStmt, iCol) ){ 1780076b9443SCy Schubert case SQLITE_BLOB: { 1781076b9443SCy Schubert int bytes = sqlite3_column_bytes(pStmt, iCol); 1782076b9443SCy Schubert const char *zBlob = sqlite3_column_blob(pStmt, iCol); 1783076b9443SCy Schubert if( !zBlob ) bytes = 0; 1784076b9443SCy Schubert return Tcl_NewByteArrayObj((u8*)zBlob, bytes); 1785076b9443SCy Schubert } 1786076b9443SCy Schubert case SQLITE_INTEGER: { 1787076b9443SCy Schubert sqlite_int64 v = sqlite3_column_int64(pStmt, iCol); 1788076b9443SCy Schubert if( v>=-2147483647 && v<=2147483647 ){ 1789076b9443SCy Schubert return Tcl_NewIntObj((int)v); 1790076b9443SCy Schubert }else{ 1791076b9443SCy Schubert return Tcl_NewWideIntObj(v); 1792076b9443SCy Schubert } 1793076b9443SCy Schubert } 1794076b9443SCy Schubert case SQLITE_FLOAT: { 1795076b9443SCy Schubert return Tcl_NewDoubleObj(sqlite3_column_double(pStmt, iCol)); 1796076b9443SCy Schubert } 1797076b9443SCy Schubert case SQLITE_NULL: { 1798076b9443SCy Schubert return Tcl_NewStringObj(p->pDb->zNull, -1); 1799076b9443SCy Schubert } 1800076b9443SCy Schubert } 1801076b9443SCy Schubert 1802076b9443SCy Schubert return Tcl_NewStringObj((char*)sqlite3_column_text(pStmt, iCol), -1); 1803076b9443SCy Schubert } 1804076b9443SCy Schubert 1805076b9443SCy Schubert /* 1806076b9443SCy Schubert ** If using Tcl version 8.6 or greater, use the NR functions to avoid 1807*ec994981SCy Schubert ** recursive evaluation of scripts by the [db eval] and [db trans] 1808076b9443SCy Schubert ** commands. Even if the headers used while compiling the extension 1809076b9443SCy Schubert ** are 8.6 or newer, the code still tests the Tcl version at runtime. 1810076b9443SCy Schubert ** This allows stubs-enabled builds to be used with older Tcl libraries. 1811076b9443SCy Schubert */ 1812076b9443SCy Schubert #if TCL_MAJOR_VERSION>8 || (TCL_MAJOR_VERSION==8 && TCL_MINOR_VERSION>=6) 1813076b9443SCy Schubert # define SQLITE_TCL_NRE 1 1814076b9443SCy Schubert static int DbUseNre(void){ 1815076b9443SCy Schubert int major, minor; 1816076b9443SCy Schubert Tcl_GetVersion(&major, &minor, 0, 0); 1817076b9443SCy Schubert return( (major==8 && minor>=6) || major>8 ); 1818076b9443SCy Schubert } 1819076b9443SCy Schubert #else 1820076b9443SCy Schubert /* 1821076b9443SCy Schubert ** Compiling using headers earlier than 8.6. In this case NR cannot be 1822076b9443SCy Schubert ** used, so DbUseNre() to always return zero. Add #defines for the other 1823076b9443SCy Schubert ** Tcl_NRxxx() functions to prevent them from causing compilation errors, 1824076b9443SCy Schubert ** even though the only invocations of them are within conditional blocks 1825076b9443SCy Schubert ** of the form: 1826076b9443SCy Schubert ** 1827076b9443SCy Schubert ** if( DbUseNre() ) { ... } 1828076b9443SCy Schubert */ 1829076b9443SCy Schubert # define SQLITE_TCL_NRE 0 1830076b9443SCy Schubert # define DbUseNre() 0 1831076b9443SCy Schubert # define Tcl_NRAddCallback(a,b,c,d,e,f) (void)0 1832076b9443SCy Schubert # define Tcl_NREvalObj(a,b,c) 0 1833076b9443SCy Schubert # define Tcl_NRCreateCommand(a,b,c,d,e,f) (void)0 1834076b9443SCy Schubert #endif 1835076b9443SCy Schubert 1836076b9443SCy Schubert /* 1837076b9443SCy Schubert ** This function is part of the implementation of the command: 1838076b9443SCy Schubert ** 1839076b9443SCy Schubert ** $db eval SQL ?ARRAYNAME? SCRIPT 1840076b9443SCy Schubert */ 1841076b9443SCy Schubert static int SQLITE_TCLAPI DbEvalNextCmd( 1842076b9443SCy Schubert ClientData data[], /* data[0] is the (DbEvalContext*) */ 1843076b9443SCy Schubert Tcl_Interp *interp, /* Tcl interpreter */ 1844076b9443SCy Schubert int result /* Result so far */ 1845076b9443SCy Schubert ){ 1846076b9443SCy Schubert int rc = result; /* Return code */ 1847076b9443SCy Schubert 1848076b9443SCy Schubert /* The first element of the data[] array is a pointer to a DbEvalContext 1849076b9443SCy Schubert ** structure allocated using Tcl_Alloc(). The second element of data[] 1850076b9443SCy Schubert ** is a pointer to a Tcl_Obj containing the script to run for each row 1851076b9443SCy Schubert ** returned by the queries encapsulated in data[0]. */ 1852076b9443SCy Schubert DbEvalContext *p = (DbEvalContext *)data[0]; 1853076b9443SCy Schubert Tcl_Obj *pScript = (Tcl_Obj *)data[1]; 1854076b9443SCy Schubert Tcl_Obj *pArray = p->pArray; 1855076b9443SCy Schubert 1856076b9443SCy Schubert while( (rc==TCL_OK || rc==TCL_CONTINUE) && TCL_OK==(rc = dbEvalStep(p)) ){ 1857076b9443SCy Schubert int i; 1858076b9443SCy Schubert int nCol; 1859076b9443SCy Schubert Tcl_Obj **apColName; 1860076b9443SCy Schubert dbEvalRowInfo(p, &nCol, &apColName); 1861076b9443SCy Schubert for(i=0; i<nCol; i++){ 1862076b9443SCy Schubert if( pArray==0 ){ 1863076b9443SCy Schubert Tcl_ObjSetVar2(interp, apColName[i], 0, dbEvalColumnValue(p,i), 0); 1864076b9443SCy Schubert }else if( (p->evalFlags & SQLITE_EVAL_WITHOUTNULLS)!=0 1865076b9443SCy Schubert && sqlite3_column_type(p->pPreStmt->pStmt, i)==SQLITE_NULL 1866076b9443SCy Schubert ){ 1867076b9443SCy Schubert Tcl_UnsetVar2(interp, Tcl_GetString(pArray), 1868076b9443SCy Schubert Tcl_GetString(apColName[i]), 0); 1869076b9443SCy Schubert }else{ 1870076b9443SCy Schubert Tcl_ObjSetVar2(interp, pArray, apColName[i], dbEvalColumnValue(p,i), 0); 1871076b9443SCy Schubert } 1872076b9443SCy Schubert } 1873076b9443SCy Schubert 1874076b9443SCy Schubert /* The required interpreter variables are now populated with the data 1875076b9443SCy Schubert ** from the current row. If using NRE, schedule callbacks to evaluate 1876076b9443SCy Schubert ** script pScript, then to invoke this function again to fetch the next 1877076b9443SCy Schubert ** row (or clean up if there is no next row or the script throws an 1878076b9443SCy Schubert ** exception). After scheduling the callbacks, return control to the 1879076b9443SCy Schubert ** caller. 1880076b9443SCy Schubert ** 1881076b9443SCy Schubert ** If not using NRE, evaluate pScript directly and continue with the 1882076b9443SCy Schubert ** next iteration of this while(...) loop. */ 1883076b9443SCy Schubert if( DbUseNre() ){ 1884076b9443SCy Schubert Tcl_NRAddCallback(interp, DbEvalNextCmd, (void*)p, (void*)pScript, 0, 0); 1885076b9443SCy Schubert return Tcl_NREvalObj(interp, pScript, 0); 1886076b9443SCy Schubert }else{ 1887076b9443SCy Schubert rc = Tcl_EvalObjEx(interp, pScript, 0); 1888076b9443SCy Schubert } 1889076b9443SCy Schubert } 1890076b9443SCy Schubert 1891076b9443SCy Schubert Tcl_DecrRefCount(pScript); 1892076b9443SCy Schubert dbEvalFinalize(p); 1893076b9443SCy Schubert Tcl_Free((char *)p); 1894076b9443SCy Schubert 1895076b9443SCy Schubert if( rc==TCL_OK || rc==TCL_BREAK ){ 1896076b9443SCy Schubert Tcl_ResetResult(interp); 1897076b9443SCy Schubert rc = TCL_OK; 1898076b9443SCy Schubert } 1899076b9443SCy Schubert return rc; 1900076b9443SCy Schubert } 1901076b9443SCy Schubert 1902076b9443SCy Schubert /* 1903076b9443SCy Schubert ** This function is used by the implementations of the following database 1904076b9443SCy Schubert ** handle sub-commands: 1905076b9443SCy Schubert ** 1906076b9443SCy Schubert ** $db update_hook ?SCRIPT? 1907076b9443SCy Schubert ** $db wal_hook ?SCRIPT? 1908076b9443SCy Schubert ** $db commit_hook ?SCRIPT? 1909076b9443SCy Schubert ** $db preupdate hook ?SCRIPT? 1910076b9443SCy Schubert */ 1911076b9443SCy Schubert static void DbHookCmd( 1912076b9443SCy Schubert Tcl_Interp *interp, /* Tcl interpreter */ 1913076b9443SCy Schubert SqliteDb *pDb, /* Database handle */ 1914076b9443SCy Schubert Tcl_Obj *pArg, /* SCRIPT argument (or NULL) */ 1915076b9443SCy Schubert Tcl_Obj **ppHook /* Pointer to member of SqliteDb */ 1916076b9443SCy Schubert ){ 1917076b9443SCy Schubert sqlite3 *db = pDb->db; 1918076b9443SCy Schubert 1919076b9443SCy Schubert if( *ppHook ){ 1920076b9443SCy Schubert Tcl_SetObjResult(interp, *ppHook); 1921076b9443SCy Schubert if( pArg ){ 1922076b9443SCy Schubert Tcl_DecrRefCount(*ppHook); 1923076b9443SCy Schubert *ppHook = 0; 1924076b9443SCy Schubert } 1925076b9443SCy Schubert } 1926076b9443SCy Schubert if( pArg ){ 1927076b9443SCy Schubert assert( !(*ppHook) ); 1928076b9443SCy Schubert if( Tcl_GetCharLength(pArg)>0 ){ 1929076b9443SCy Schubert *ppHook = pArg; 1930076b9443SCy Schubert Tcl_IncrRefCount(*ppHook); 1931076b9443SCy Schubert } 1932076b9443SCy Schubert } 1933076b9443SCy Schubert 1934076b9443SCy Schubert #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 1935076b9443SCy Schubert sqlite3_preupdate_hook(db, (pDb->pPreUpdateHook?DbPreUpdateHandler:0), pDb); 1936076b9443SCy Schubert #endif 1937076b9443SCy Schubert sqlite3_update_hook(db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); 1938076b9443SCy Schubert sqlite3_rollback_hook(db, (pDb->pRollbackHook?DbRollbackHandler:0), pDb); 1939076b9443SCy Schubert sqlite3_wal_hook(db, (pDb->pWalHook?DbWalHandler:0), pDb); 1940076b9443SCy Schubert } 1941076b9443SCy Schubert 1942076b9443SCy Schubert /* 1943076b9443SCy Schubert ** The "sqlite" command below creates a new Tcl command for each 1944076b9443SCy Schubert ** connection it opens to an SQLite database. This routine is invoked 1945076b9443SCy Schubert ** whenever one of those connection-specific commands is executed 1946076b9443SCy Schubert ** in Tcl. For example, if you run Tcl code like this: 1947076b9443SCy Schubert ** 1948076b9443SCy Schubert ** sqlite3 db1 "my_database" 1949076b9443SCy Schubert ** db1 close 1950076b9443SCy Schubert ** 1951076b9443SCy Schubert ** The first command opens a connection to the "my_database" database 1952076b9443SCy Schubert ** and calls that connection "db1". The second command causes this 1953076b9443SCy Schubert ** subroutine to be invoked. 1954076b9443SCy Schubert */ 1955076b9443SCy Schubert static int SQLITE_TCLAPI DbObjCmd( 1956076b9443SCy Schubert void *cd, 1957076b9443SCy Schubert Tcl_Interp *interp, 1958076b9443SCy Schubert int objc, 1959076b9443SCy Schubert Tcl_Obj *const*objv 1960076b9443SCy Schubert ){ 1961076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 1962076b9443SCy Schubert int choice; 1963076b9443SCy Schubert int rc = TCL_OK; 1964076b9443SCy Schubert static const char *DB_strs[] = { 196502273ca8SCy Schubert "authorizer", "backup", "bind_fallback", 196602273ca8SCy Schubert "busy", "cache", "changes", 196702273ca8SCy Schubert "close", "collate", "collation_needed", 1968f1b328b3SCy Schubert "commit_hook", "complete", "config", 1969f1b328b3SCy Schubert "copy", "deserialize", "enable_load_extension", 19704fe1295cSCy Schubert "errorcode", "erroroffset", "eval", 19714fe1295cSCy Schubert "exists", "function", "incrblob", 19724fe1295cSCy Schubert "interrupt", "last_insert_rowid", "nullvalue", 19734fe1295cSCy Schubert "onecolumn", "preupdate", "profile", 19744fe1295cSCy Schubert "progress", "rekey", "restore", 19754fe1295cSCy Schubert "rollback_hook", "serialize", "status", 19764fe1295cSCy Schubert "timeout", "total_changes", "trace", 19774fe1295cSCy Schubert "trace_v2", "transaction", "unlock_notify", 19784fe1295cSCy Schubert "update_hook", "version", "wal_hook", 19794fe1295cSCy Schubert 0 1980076b9443SCy Schubert }; 1981076b9443SCy Schubert enum DB_enum { 198202273ca8SCy Schubert DB_AUTHORIZER, DB_BACKUP, DB_BIND_FALLBACK, 198302273ca8SCy Schubert DB_BUSY, DB_CACHE, DB_CHANGES, 198402273ca8SCy Schubert DB_CLOSE, DB_COLLATE, DB_COLLATION_NEEDED, 1985f1b328b3SCy Schubert DB_COMMIT_HOOK, DB_COMPLETE, DB_CONFIG, 1986f1b328b3SCy Schubert DB_COPY, DB_DESERIALIZE, DB_ENABLE_LOAD_EXTENSION, 19874fe1295cSCy Schubert DB_ERRORCODE, DB_ERROROFFSET, DB_EVAL, 19884fe1295cSCy Schubert DB_EXISTS, DB_FUNCTION, DB_INCRBLOB, 19894fe1295cSCy Schubert DB_INTERRUPT, DB_LAST_INSERT_ROWID, DB_NULLVALUE, 19904fe1295cSCy Schubert DB_ONECOLUMN, DB_PREUPDATE, DB_PROFILE, 19914fe1295cSCy Schubert DB_PROGRESS, DB_REKEY, DB_RESTORE, 19924fe1295cSCy Schubert DB_ROLLBACK_HOOK, DB_SERIALIZE, DB_STATUS, 19934fe1295cSCy Schubert DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE, 19944fe1295cSCy Schubert DB_TRACE_V2, DB_TRANSACTION, DB_UNLOCK_NOTIFY, 19954fe1295cSCy Schubert DB_UPDATE_HOOK, DB_VERSION, DB_WAL_HOOK, 1996076b9443SCy Schubert }; 1997076b9443SCy Schubert /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */ 1998076b9443SCy Schubert 1999076b9443SCy Schubert if( objc<2 ){ 2000076b9443SCy Schubert Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); 2001076b9443SCy Schubert return TCL_ERROR; 2002076b9443SCy Schubert } 2003076b9443SCy Schubert if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ 2004076b9443SCy Schubert return TCL_ERROR; 2005076b9443SCy Schubert } 2006076b9443SCy Schubert 2007076b9443SCy Schubert switch( (enum DB_enum)choice ){ 2008076b9443SCy Schubert 2009076b9443SCy Schubert /* $db authorizer ?CALLBACK? 2010076b9443SCy Schubert ** 2011076b9443SCy Schubert ** Invoke the given callback to authorize each SQL operation as it is 2012076b9443SCy Schubert ** compiled. 5 arguments are appended to the callback before it is 2013076b9443SCy Schubert ** invoked: 2014076b9443SCy Schubert ** 2015076b9443SCy Schubert ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) 2016076b9443SCy Schubert ** (2) First descriptive name (depends on authorization type) 2017076b9443SCy Schubert ** (3) Second descriptive name 2018076b9443SCy Schubert ** (4) Name of the database (ex: "main", "temp") 2019076b9443SCy Schubert ** (5) Name of trigger that is doing the access 2020076b9443SCy Schubert ** 2021076b9443SCy Schubert ** The callback should return on of the following strings: SQLITE_OK, 2022076b9443SCy Schubert ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. 2023076b9443SCy Schubert ** 2024076b9443SCy Schubert ** If this method is invoked with no arguments, the current authorization 2025076b9443SCy Schubert ** callback string is returned. 2026076b9443SCy Schubert */ 2027076b9443SCy Schubert case DB_AUTHORIZER: { 2028076b9443SCy Schubert #ifdef SQLITE_OMIT_AUTHORIZATION 2029076b9443SCy Schubert Tcl_AppendResult(interp, "authorization not available in this build", 2030076b9443SCy Schubert (char*)0); 2031076b9443SCy Schubert return TCL_ERROR; 2032076b9443SCy Schubert #else 2033076b9443SCy Schubert if( objc>3 ){ 2034076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 2035076b9443SCy Schubert return TCL_ERROR; 2036076b9443SCy Schubert }else if( objc==2 ){ 2037076b9443SCy Schubert if( pDb->zAuth ){ 2038076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zAuth, (char*)0); 2039076b9443SCy Schubert } 2040076b9443SCy Schubert }else{ 2041076b9443SCy Schubert char *zAuth; 2042076b9443SCy Schubert int len; 2043076b9443SCy Schubert if( pDb->zAuth ){ 2044076b9443SCy Schubert Tcl_Free(pDb->zAuth); 2045076b9443SCy Schubert } 2046076b9443SCy Schubert zAuth = Tcl_GetStringFromObj(objv[2], &len); 2047076b9443SCy Schubert if( zAuth && len>0 ){ 2048076b9443SCy Schubert pDb->zAuth = Tcl_Alloc( len + 1 ); 2049076b9443SCy Schubert memcpy(pDb->zAuth, zAuth, len+1); 2050076b9443SCy Schubert }else{ 2051076b9443SCy Schubert pDb->zAuth = 0; 2052076b9443SCy Schubert } 2053076b9443SCy Schubert if( pDb->zAuth ){ 2054076b9443SCy Schubert typedef int (*sqlite3_auth_cb)( 2055076b9443SCy Schubert void*,int,const char*,const char*, 2056076b9443SCy Schubert const char*,const char*); 2057076b9443SCy Schubert pDb->interp = interp; 2058076b9443SCy Schubert sqlite3_set_authorizer(pDb->db,(sqlite3_auth_cb)auth_callback,pDb); 2059076b9443SCy Schubert }else{ 2060076b9443SCy Schubert sqlite3_set_authorizer(pDb->db, 0, 0); 2061076b9443SCy Schubert } 2062076b9443SCy Schubert } 2063076b9443SCy Schubert #endif 2064076b9443SCy Schubert break; 2065076b9443SCy Schubert } 2066076b9443SCy Schubert 2067076b9443SCy Schubert /* $db backup ?DATABASE? FILENAME 2068076b9443SCy Schubert ** 2069076b9443SCy Schubert ** Open or create a database file named FILENAME. Transfer the 2070076b9443SCy Schubert ** content of local database DATABASE (default: "main") into the 2071076b9443SCy Schubert ** FILENAME database. 2072076b9443SCy Schubert */ 2073076b9443SCy Schubert case DB_BACKUP: { 2074076b9443SCy Schubert const char *zDestFile; 2075076b9443SCy Schubert const char *zSrcDb; 2076076b9443SCy Schubert sqlite3 *pDest; 2077076b9443SCy Schubert sqlite3_backup *pBackup; 2078076b9443SCy Schubert 2079076b9443SCy Schubert if( objc==3 ){ 2080076b9443SCy Schubert zSrcDb = "main"; 2081076b9443SCy Schubert zDestFile = Tcl_GetString(objv[2]); 2082076b9443SCy Schubert }else if( objc==4 ){ 2083076b9443SCy Schubert zSrcDb = Tcl_GetString(objv[2]); 2084076b9443SCy Schubert zDestFile = Tcl_GetString(objv[3]); 2085076b9443SCy Schubert }else{ 2086076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 2087076b9443SCy Schubert return TCL_ERROR; 2088076b9443SCy Schubert } 2089076b9443SCy Schubert rc = sqlite3_open_v2(zDestFile, &pDest, 2090076b9443SCy Schubert SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE| pDb->openFlags, 0); 2091076b9443SCy Schubert if( rc!=SQLITE_OK ){ 2092076b9443SCy Schubert Tcl_AppendResult(interp, "cannot open target database: ", 2093076b9443SCy Schubert sqlite3_errmsg(pDest), (char*)0); 2094076b9443SCy Schubert sqlite3_close(pDest); 2095076b9443SCy Schubert return TCL_ERROR; 2096076b9443SCy Schubert } 2097076b9443SCy Schubert pBackup = sqlite3_backup_init(pDest, "main", pDb->db, zSrcDb); 2098076b9443SCy Schubert if( pBackup==0 ){ 2099076b9443SCy Schubert Tcl_AppendResult(interp, "backup failed: ", 2100076b9443SCy Schubert sqlite3_errmsg(pDest), (char*)0); 2101076b9443SCy Schubert sqlite3_close(pDest); 2102076b9443SCy Schubert return TCL_ERROR; 2103076b9443SCy Schubert } 2104076b9443SCy Schubert while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} 2105076b9443SCy Schubert sqlite3_backup_finish(pBackup); 2106076b9443SCy Schubert if( rc==SQLITE_DONE ){ 2107076b9443SCy Schubert rc = TCL_OK; 2108076b9443SCy Schubert }else{ 2109076b9443SCy Schubert Tcl_AppendResult(interp, "backup failed: ", 2110076b9443SCy Schubert sqlite3_errmsg(pDest), (char*)0); 2111076b9443SCy Schubert rc = TCL_ERROR; 2112076b9443SCy Schubert } 2113076b9443SCy Schubert sqlite3_close(pDest); 2114076b9443SCy Schubert break; 2115076b9443SCy Schubert } 2116076b9443SCy Schubert 211702273ca8SCy Schubert /* $db bind_fallback ?CALLBACK? 211802273ca8SCy Schubert ** 211902273ca8SCy Schubert ** When resolving bind parameters in an SQL statement, if the parameter 212002273ca8SCy Schubert ** cannot be associated with a TCL variable then invoke CALLBACK with a 212102273ca8SCy Schubert ** single argument that is the name of the parameter and use the return 212202273ca8SCy Schubert ** value of the CALLBACK as the binding. If CALLBACK returns something 212302273ca8SCy Schubert ** other than TCL_OK or TCL_ERROR then bind a NULL. 212402273ca8SCy Schubert ** 212502273ca8SCy Schubert ** If CALLBACK is an empty string, then revert to the default behavior 212602273ca8SCy Schubert ** which is to set the binding to NULL. 212702273ca8SCy Schubert ** 212802273ca8SCy Schubert ** If CALLBACK returns an error, that causes the statement execution to 212902273ca8SCy Schubert ** abort. Hence, to configure a connection so that it throws an error 213002273ca8SCy Schubert ** on an attempt to bind an unknown variable, do something like this: 213102273ca8SCy Schubert ** 213202273ca8SCy Schubert ** proc bind_error {name} {error "no such variable: $name"} 213302273ca8SCy Schubert ** db bind_fallback bind_error 213402273ca8SCy Schubert */ 213502273ca8SCy Schubert case DB_BIND_FALLBACK: { 213602273ca8SCy Schubert if( objc>3 ){ 213702273ca8SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 213802273ca8SCy Schubert return TCL_ERROR; 213902273ca8SCy Schubert }else if( objc==2 ){ 214002273ca8SCy Schubert if( pDb->zBindFallback ){ 214102273ca8SCy Schubert Tcl_AppendResult(interp, pDb->zBindFallback, (char*)0); 214202273ca8SCy Schubert } 214302273ca8SCy Schubert }else{ 214402273ca8SCy Schubert char *zCallback; 214502273ca8SCy Schubert int len; 214602273ca8SCy Schubert if( pDb->zBindFallback ){ 214702273ca8SCy Schubert Tcl_Free(pDb->zBindFallback); 214802273ca8SCy Schubert } 214902273ca8SCy Schubert zCallback = Tcl_GetStringFromObj(objv[2], &len); 215002273ca8SCy Schubert if( zCallback && len>0 ){ 215102273ca8SCy Schubert pDb->zBindFallback = Tcl_Alloc( len + 1 ); 215202273ca8SCy Schubert memcpy(pDb->zBindFallback, zCallback, len+1); 215302273ca8SCy Schubert }else{ 215402273ca8SCy Schubert pDb->zBindFallback = 0; 215502273ca8SCy Schubert } 215602273ca8SCy Schubert } 215702273ca8SCy Schubert break; 215802273ca8SCy Schubert } 215902273ca8SCy Schubert 2160076b9443SCy Schubert /* $db busy ?CALLBACK? 2161076b9443SCy Schubert ** 2162076b9443SCy Schubert ** Invoke the given callback if an SQL statement attempts to open 2163076b9443SCy Schubert ** a locked database file. 2164076b9443SCy Schubert */ 2165076b9443SCy Schubert case DB_BUSY: { 2166076b9443SCy Schubert if( objc>3 ){ 2167076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); 2168076b9443SCy Schubert return TCL_ERROR; 2169076b9443SCy Schubert }else if( objc==2 ){ 2170076b9443SCy Schubert if( pDb->zBusy ){ 2171076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zBusy, (char*)0); 2172076b9443SCy Schubert } 2173076b9443SCy Schubert }else{ 2174076b9443SCy Schubert char *zBusy; 2175076b9443SCy Schubert int len; 2176076b9443SCy Schubert if( pDb->zBusy ){ 2177076b9443SCy Schubert Tcl_Free(pDb->zBusy); 2178076b9443SCy Schubert } 2179076b9443SCy Schubert zBusy = Tcl_GetStringFromObj(objv[2], &len); 2180076b9443SCy Schubert if( zBusy && len>0 ){ 2181076b9443SCy Schubert pDb->zBusy = Tcl_Alloc( len + 1 ); 2182076b9443SCy Schubert memcpy(pDb->zBusy, zBusy, len+1); 2183076b9443SCy Schubert }else{ 2184076b9443SCy Schubert pDb->zBusy = 0; 2185076b9443SCy Schubert } 2186076b9443SCy Schubert if( pDb->zBusy ){ 2187076b9443SCy Schubert pDb->interp = interp; 2188076b9443SCy Schubert sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb); 2189076b9443SCy Schubert }else{ 2190076b9443SCy Schubert sqlite3_busy_handler(pDb->db, 0, 0); 2191076b9443SCy Schubert } 2192076b9443SCy Schubert } 2193076b9443SCy Schubert break; 2194076b9443SCy Schubert } 2195076b9443SCy Schubert 2196076b9443SCy Schubert /* $db cache flush 2197076b9443SCy Schubert ** $db cache size n 2198076b9443SCy Schubert ** 2199076b9443SCy Schubert ** Flush the prepared statement cache, or set the maximum number of 2200076b9443SCy Schubert ** cached statements. 2201076b9443SCy Schubert */ 2202076b9443SCy Schubert case DB_CACHE: { 2203076b9443SCy Schubert char *subCmd; 2204076b9443SCy Schubert int n; 2205076b9443SCy Schubert 2206076b9443SCy Schubert if( objc<=2 ){ 2207076b9443SCy Schubert Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?"); 2208076b9443SCy Schubert return TCL_ERROR; 2209076b9443SCy Schubert } 2210076b9443SCy Schubert subCmd = Tcl_GetStringFromObj( objv[2], 0 ); 2211076b9443SCy Schubert if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){ 2212076b9443SCy Schubert if( objc!=3 ){ 2213076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "flush"); 2214076b9443SCy Schubert return TCL_ERROR; 2215076b9443SCy Schubert }else{ 2216076b9443SCy Schubert flushStmtCache( pDb ); 2217076b9443SCy Schubert } 2218076b9443SCy Schubert }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){ 2219076b9443SCy Schubert if( objc!=4 ){ 2220076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "size n"); 2221076b9443SCy Schubert return TCL_ERROR; 2222076b9443SCy Schubert }else{ 2223076b9443SCy Schubert if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){ 2224076b9443SCy Schubert Tcl_AppendResult( interp, "cannot convert \"", 2225076b9443SCy Schubert Tcl_GetStringFromObj(objv[3],0), "\" to integer", (char*)0); 2226076b9443SCy Schubert return TCL_ERROR; 2227076b9443SCy Schubert }else{ 2228076b9443SCy Schubert if( n<0 ){ 2229076b9443SCy Schubert flushStmtCache( pDb ); 2230076b9443SCy Schubert n = 0; 2231076b9443SCy Schubert }else if( n>MAX_PREPARED_STMTS ){ 2232076b9443SCy Schubert n = MAX_PREPARED_STMTS; 2233076b9443SCy Schubert } 2234076b9443SCy Schubert pDb->maxStmt = n; 2235076b9443SCy Schubert } 2236076b9443SCy Schubert } 2237076b9443SCy Schubert }else{ 2238076b9443SCy Schubert Tcl_AppendResult( interp, "bad option \"", 2239076b9443SCy Schubert Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size", 2240076b9443SCy Schubert (char*)0); 2241076b9443SCy Schubert return TCL_ERROR; 2242076b9443SCy Schubert } 2243076b9443SCy Schubert break; 2244076b9443SCy Schubert } 2245076b9443SCy Schubert 2246076b9443SCy Schubert /* $db changes 2247076b9443SCy Schubert ** 2248076b9443SCy Schubert ** Return the number of rows that were modified, inserted, or deleted by 2249076b9443SCy Schubert ** the most recent INSERT, UPDATE or DELETE statement, not including 2250076b9443SCy Schubert ** any changes made by trigger programs. 2251076b9443SCy Schubert */ 2252076b9443SCy Schubert case DB_CHANGES: { 2253076b9443SCy Schubert Tcl_Obj *pResult; 2254076b9443SCy Schubert if( objc!=2 ){ 2255076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, ""); 2256076b9443SCy Schubert return TCL_ERROR; 2257076b9443SCy Schubert } 2258076b9443SCy Schubert pResult = Tcl_GetObjResult(interp); 22590197ba46SCy Schubert Tcl_SetWideIntObj(pResult, sqlite3_changes64(pDb->db)); 2260076b9443SCy Schubert break; 2261076b9443SCy Schubert } 2262076b9443SCy Schubert 2263076b9443SCy Schubert /* $db close 2264076b9443SCy Schubert ** 2265076b9443SCy Schubert ** Shutdown the database 2266076b9443SCy Schubert */ 2267076b9443SCy Schubert case DB_CLOSE: { 2268076b9443SCy Schubert Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); 2269076b9443SCy Schubert break; 2270076b9443SCy Schubert } 2271076b9443SCy Schubert 2272076b9443SCy Schubert /* 2273076b9443SCy Schubert ** $db collate NAME SCRIPT 2274076b9443SCy Schubert ** 2275076b9443SCy Schubert ** Create a new SQL collation function called NAME. Whenever 2276076b9443SCy Schubert ** that function is called, invoke SCRIPT to evaluate the function. 2277076b9443SCy Schubert */ 2278076b9443SCy Schubert case DB_COLLATE: { 2279076b9443SCy Schubert SqlCollate *pCollate; 2280076b9443SCy Schubert char *zName; 2281076b9443SCy Schubert char *zScript; 2282076b9443SCy Schubert int nScript; 2283076b9443SCy Schubert if( objc!=4 ){ 2284076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); 2285076b9443SCy Schubert return TCL_ERROR; 2286076b9443SCy Schubert } 2287076b9443SCy Schubert zName = Tcl_GetStringFromObj(objv[2], 0); 2288076b9443SCy Schubert zScript = Tcl_GetStringFromObj(objv[3], &nScript); 2289076b9443SCy Schubert pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 ); 2290076b9443SCy Schubert if( pCollate==0 ) return TCL_ERROR; 2291076b9443SCy Schubert pCollate->interp = interp; 2292076b9443SCy Schubert pCollate->pNext = pDb->pCollate; 2293076b9443SCy Schubert pCollate->zScript = (char*)&pCollate[1]; 2294076b9443SCy Schubert pDb->pCollate = pCollate; 2295076b9443SCy Schubert memcpy(pCollate->zScript, zScript, nScript+1); 2296076b9443SCy Schubert if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, 2297076b9443SCy Schubert pCollate, tclSqlCollate) ){ 2298076b9443SCy Schubert Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 2299076b9443SCy Schubert return TCL_ERROR; 2300076b9443SCy Schubert } 2301076b9443SCy Schubert break; 2302076b9443SCy Schubert } 2303076b9443SCy Schubert 2304076b9443SCy Schubert /* 2305076b9443SCy Schubert ** $db collation_needed SCRIPT 2306076b9443SCy Schubert ** 2307076b9443SCy Schubert ** Create a new SQL collation function called NAME. Whenever 2308076b9443SCy Schubert ** that function is called, invoke SCRIPT to evaluate the function. 2309076b9443SCy Schubert */ 2310076b9443SCy Schubert case DB_COLLATION_NEEDED: { 2311076b9443SCy Schubert if( objc!=3 ){ 2312076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT"); 2313076b9443SCy Schubert return TCL_ERROR; 2314076b9443SCy Schubert } 2315076b9443SCy Schubert if( pDb->pCollateNeeded ){ 2316076b9443SCy Schubert Tcl_DecrRefCount(pDb->pCollateNeeded); 2317076b9443SCy Schubert } 2318076b9443SCy Schubert pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]); 2319076b9443SCy Schubert Tcl_IncrRefCount(pDb->pCollateNeeded); 2320076b9443SCy Schubert sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded); 2321076b9443SCy Schubert break; 2322076b9443SCy Schubert } 2323076b9443SCy Schubert 2324076b9443SCy Schubert /* $db commit_hook ?CALLBACK? 2325076b9443SCy Schubert ** 2326076b9443SCy Schubert ** Invoke the given callback just before committing every SQL transaction. 2327076b9443SCy Schubert ** If the callback throws an exception or returns non-zero, then the 2328076b9443SCy Schubert ** transaction is aborted. If CALLBACK is an empty string, the callback 2329076b9443SCy Schubert ** is disabled. 2330076b9443SCy Schubert */ 2331076b9443SCy Schubert case DB_COMMIT_HOOK: { 2332076b9443SCy Schubert if( objc>3 ){ 2333076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 2334076b9443SCy Schubert return TCL_ERROR; 2335076b9443SCy Schubert }else if( objc==2 ){ 2336076b9443SCy Schubert if( pDb->zCommit ){ 2337076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zCommit, (char*)0); 2338076b9443SCy Schubert } 2339076b9443SCy Schubert }else{ 2340076b9443SCy Schubert const char *zCommit; 2341076b9443SCy Schubert int len; 2342076b9443SCy Schubert if( pDb->zCommit ){ 2343076b9443SCy Schubert Tcl_Free(pDb->zCommit); 2344076b9443SCy Schubert } 2345076b9443SCy Schubert zCommit = Tcl_GetStringFromObj(objv[2], &len); 2346076b9443SCy Schubert if( zCommit && len>0 ){ 2347076b9443SCy Schubert pDb->zCommit = Tcl_Alloc( len + 1 ); 2348076b9443SCy Schubert memcpy(pDb->zCommit, zCommit, len+1); 2349076b9443SCy Schubert }else{ 2350076b9443SCy Schubert pDb->zCommit = 0; 2351076b9443SCy Schubert } 2352076b9443SCy Schubert if( pDb->zCommit ){ 2353076b9443SCy Schubert pDb->interp = interp; 2354076b9443SCy Schubert sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb); 2355076b9443SCy Schubert }else{ 2356076b9443SCy Schubert sqlite3_commit_hook(pDb->db, 0, 0); 2357076b9443SCy Schubert } 2358076b9443SCy Schubert } 2359076b9443SCy Schubert break; 2360076b9443SCy Schubert } 2361076b9443SCy Schubert 2362076b9443SCy Schubert /* $db complete SQL 2363076b9443SCy Schubert ** 2364076b9443SCy Schubert ** Return TRUE if SQL is a complete SQL statement. Return FALSE if 2365076b9443SCy Schubert ** additional lines of input are needed. This is similar to the 2366076b9443SCy Schubert ** built-in "info complete" command of Tcl. 2367076b9443SCy Schubert */ 2368076b9443SCy Schubert case DB_COMPLETE: { 2369076b9443SCy Schubert #ifndef SQLITE_OMIT_COMPLETE 2370076b9443SCy Schubert Tcl_Obj *pResult; 2371076b9443SCy Schubert int isComplete; 2372076b9443SCy Schubert if( objc!=3 ){ 2373076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 2374076b9443SCy Schubert return TCL_ERROR; 2375076b9443SCy Schubert } 2376076b9443SCy Schubert isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) ); 2377076b9443SCy Schubert pResult = Tcl_GetObjResult(interp); 2378076b9443SCy Schubert Tcl_SetBooleanObj(pResult, isComplete); 2379076b9443SCy Schubert #endif 2380076b9443SCy Schubert break; 2381076b9443SCy Schubert } 2382076b9443SCy Schubert 2383f1b328b3SCy Schubert /* $db config ?OPTION? ?BOOLEAN? 2384f1b328b3SCy Schubert ** 2385f1b328b3SCy Schubert ** Configure the database connection using the sqlite3_db_config() 2386f1b328b3SCy Schubert ** interface. 2387f1b328b3SCy Schubert */ 2388f1b328b3SCy Schubert case DB_CONFIG: { 2389f1b328b3SCy Schubert static const struct DbConfigChoices { 2390f1b328b3SCy Schubert const char *zName; 2391f1b328b3SCy Schubert int op; 2392f1b328b3SCy Schubert } aDbConfig[] = { 23930e2816f5SCy Schubert { "defensive", SQLITE_DBCONFIG_DEFENSIVE }, 23940e2816f5SCy Schubert { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL }, 23950e2816f5SCy Schubert { "dqs_dml", SQLITE_DBCONFIG_DQS_DML }, 2396f1b328b3SCy Schubert { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY }, 23970e2816f5SCy Schubert { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG }, 2398f1b328b3SCy Schubert { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER }, 2399f1b328b3SCy Schubert { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW }, 2400f1b328b3SCy Schubert { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER }, 24010e2816f5SCy Schubert { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE }, 24020e2816f5SCy Schubert { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT }, 2403f1b328b3SCy Schubert { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION }, 2404f1b328b3SCy Schubert { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE }, 2405c998f2d3SCy Schubert { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE }, 24060e2816f5SCy Schubert { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP }, 24070e2816f5SCy Schubert { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA }, 2408f1b328b3SCy Schubert { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA }, 2409f1b328b3SCy Schubert }; 2410f1b328b3SCy Schubert Tcl_Obj *pResult; 2411f1b328b3SCy Schubert int ii; 2412f1b328b3SCy Schubert if( objc>4 ){ 2413f1b328b3SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?OPTION? ?BOOLEAN?"); 2414f1b328b3SCy Schubert return TCL_ERROR; 2415f1b328b3SCy Schubert } 2416f1b328b3SCy Schubert if( objc==2 ){ 2417f1b328b3SCy Schubert /* With no arguments, list all configuration options and with the 2418f1b328b3SCy Schubert ** current value */ 2419f1b328b3SCy Schubert pResult = Tcl_NewListObj(0,0); 2420f1b328b3SCy Schubert for(ii=0; ii<sizeof(aDbConfig)/sizeof(aDbConfig[0]); ii++){ 2421f1b328b3SCy Schubert int v = 0; 2422f1b328b3SCy Schubert sqlite3_db_config(pDb->db, aDbConfig[ii].op, -1, &v); 2423f1b328b3SCy Schubert Tcl_ListObjAppendElement(interp, pResult, 2424f1b328b3SCy Schubert Tcl_NewStringObj(aDbConfig[ii].zName,-1)); 2425f1b328b3SCy Schubert Tcl_ListObjAppendElement(interp, pResult, 2426f1b328b3SCy Schubert Tcl_NewIntObj(v)); 2427f1b328b3SCy Schubert } 2428f1b328b3SCy Schubert }else{ 2429f1b328b3SCy Schubert const char *zOpt = Tcl_GetString(objv[2]); 2430f1b328b3SCy Schubert int onoff = -1; 2431f1b328b3SCy Schubert int v = 0; 2432f1b328b3SCy Schubert if( zOpt[0]=='-' ) zOpt++; 2433f1b328b3SCy Schubert for(ii=0; ii<sizeof(aDbConfig)/sizeof(aDbConfig[0]); ii++){ 2434f1b328b3SCy Schubert if( strcmp(aDbConfig[ii].zName, zOpt)==0 ) break; 2435f1b328b3SCy Schubert } 2436f1b328b3SCy Schubert if( ii>=sizeof(aDbConfig)/sizeof(aDbConfig[0]) ){ 2437f1b328b3SCy Schubert Tcl_AppendResult(interp, "unknown config option: \"", zOpt, 2438f1b328b3SCy Schubert "\"", (void*)0); 2439f1b328b3SCy Schubert return TCL_ERROR; 2440f1b328b3SCy Schubert } 2441f1b328b3SCy Schubert if( objc==4 ){ 2442f1b328b3SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ){ 2443f1b328b3SCy Schubert return TCL_ERROR; 2444f1b328b3SCy Schubert } 2445f1b328b3SCy Schubert } 2446f1b328b3SCy Schubert sqlite3_db_config(pDb->db, aDbConfig[ii].op, onoff, &v); 2447f1b328b3SCy Schubert pResult = Tcl_NewIntObj(v); 2448f1b328b3SCy Schubert } 2449f1b328b3SCy Schubert Tcl_SetObjResult(interp, pResult); 2450f1b328b3SCy Schubert break; 2451f1b328b3SCy Schubert } 2452f1b328b3SCy Schubert 2453076b9443SCy Schubert /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR? 2454076b9443SCy Schubert ** 2455076b9443SCy Schubert ** Copy data into table from filename, optionally using SEPARATOR 2456076b9443SCy Schubert ** as column separators. If a column contains a null string, or the 2457076b9443SCy Schubert ** value of NULLINDICATOR, a NULL is inserted for the column. 2458076b9443SCy Schubert ** conflict-algorithm is one of the sqlite conflict algorithms: 2459076b9443SCy Schubert ** rollback, abort, fail, ignore, replace 2460076b9443SCy Schubert ** On success, return the number of lines processed, not necessarily same 2461076b9443SCy Schubert ** as 'db changes' due to conflict-algorithm selected. 2462076b9443SCy Schubert ** 2463076b9443SCy Schubert ** This code is basically an implementation/enhancement of 2464076b9443SCy Schubert ** the sqlite3 shell.c ".import" command. 2465076b9443SCy Schubert ** 2466076b9443SCy Schubert ** This command usage is equivalent to the sqlite2.x COPY statement, 2467076b9443SCy Schubert ** which imports file data into a table using the PostgreSQL COPY file format: 2468*ec994981SCy Schubert ** $db copy $conflict_algorithm $table_name $filename \t \\N 2469076b9443SCy Schubert */ 2470076b9443SCy Schubert case DB_COPY: { 2471076b9443SCy Schubert char *zTable; /* Insert data into this table */ 2472076b9443SCy Schubert char *zFile; /* The file from which to extract data */ 2473076b9443SCy Schubert char *zConflict; /* The conflict algorithm to use */ 2474076b9443SCy Schubert sqlite3_stmt *pStmt; /* A statement */ 2475076b9443SCy Schubert int nCol; /* Number of columns in the table */ 2476076b9443SCy Schubert int nByte; /* Number of bytes in an SQL string */ 2477076b9443SCy Schubert int i, j; /* Loop counters */ 2478076b9443SCy Schubert int nSep; /* Number of bytes in zSep[] */ 2479076b9443SCy Schubert int nNull; /* Number of bytes in zNull[] */ 2480076b9443SCy Schubert char *zSql; /* An SQL statement */ 2481076b9443SCy Schubert char *zLine; /* A single line of input from the file */ 2482076b9443SCy Schubert char **azCol; /* zLine[] broken up into columns */ 2483076b9443SCy Schubert const char *zCommit; /* How to commit changes */ 2484076b9443SCy Schubert FILE *in; /* The input file */ 2485076b9443SCy Schubert int lineno = 0; /* Line number of input file */ 2486076b9443SCy Schubert char zLineNum[80]; /* Line number print buffer */ 2487076b9443SCy Schubert Tcl_Obj *pResult; /* interp result */ 2488076b9443SCy Schubert 2489076b9443SCy Schubert const char *zSep; 2490076b9443SCy Schubert const char *zNull; 2491076b9443SCy Schubert if( objc<5 || objc>7 ){ 2492076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, 2493076b9443SCy Schubert "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"); 2494076b9443SCy Schubert return TCL_ERROR; 2495076b9443SCy Schubert } 2496076b9443SCy Schubert if( objc>=6 ){ 2497076b9443SCy Schubert zSep = Tcl_GetStringFromObj(objv[5], 0); 2498076b9443SCy Schubert }else{ 2499076b9443SCy Schubert zSep = "\t"; 2500076b9443SCy Schubert } 2501076b9443SCy Schubert if( objc>=7 ){ 2502076b9443SCy Schubert zNull = Tcl_GetStringFromObj(objv[6], 0); 2503076b9443SCy Schubert }else{ 2504076b9443SCy Schubert zNull = ""; 2505076b9443SCy Schubert } 2506076b9443SCy Schubert zConflict = Tcl_GetStringFromObj(objv[2], 0); 2507076b9443SCy Schubert zTable = Tcl_GetStringFromObj(objv[3], 0); 2508076b9443SCy Schubert zFile = Tcl_GetStringFromObj(objv[4], 0); 2509076b9443SCy Schubert nSep = strlen30(zSep); 2510076b9443SCy Schubert nNull = strlen30(zNull); 2511076b9443SCy Schubert if( nSep==0 ){ 2512076b9443SCy Schubert Tcl_AppendResult(interp,"Error: non-null separator required for copy", 2513076b9443SCy Schubert (char*)0); 2514076b9443SCy Schubert return TCL_ERROR; 2515076b9443SCy Schubert } 2516076b9443SCy Schubert if(strcmp(zConflict, "rollback") != 0 && 2517076b9443SCy Schubert strcmp(zConflict, "abort" ) != 0 && 2518076b9443SCy Schubert strcmp(zConflict, "fail" ) != 0 && 2519076b9443SCy Schubert strcmp(zConflict, "ignore" ) != 0 && 2520076b9443SCy Schubert strcmp(zConflict, "replace" ) != 0 ) { 2521076b9443SCy Schubert Tcl_AppendResult(interp, "Error: \"", zConflict, 2522076b9443SCy Schubert "\", conflict-algorithm must be one of: rollback, " 2523076b9443SCy Schubert "abort, fail, ignore, or replace", (char*)0); 2524076b9443SCy Schubert return TCL_ERROR; 2525076b9443SCy Schubert } 2526076b9443SCy Schubert zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); 2527076b9443SCy Schubert if( zSql==0 ){ 2528076b9443SCy Schubert Tcl_AppendResult(interp, "Error: no such table: ", zTable, (char*)0); 2529076b9443SCy Schubert return TCL_ERROR; 2530076b9443SCy Schubert } 2531076b9443SCy Schubert nByte = strlen30(zSql); 2532076b9443SCy Schubert rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 2533076b9443SCy Schubert sqlite3_free(zSql); 2534076b9443SCy Schubert if( rc ){ 2535076b9443SCy Schubert Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0); 2536076b9443SCy Schubert nCol = 0; 2537076b9443SCy Schubert }else{ 2538076b9443SCy Schubert nCol = sqlite3_column_count(pStmt); 2539076b9443SCy Schubert } 2540076b9443SCy Schubert sqlite3_finalize(pStmt); 2541076b9443SCy Schubert if( nCol==0 ) { 2542076b9443SCy Schubert return TCL_ERROR; 2543076b9443SCy Schubert } 2544076b9443SCy Schubert zSql = malloc( nByte + 50 + nCol*2 ); 2545076b9443SCy Schubert if( zSql==0 ) { 2546076b9443SCy Schubert Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0); 2547076b9443SCy Schubert return TCL_ERROR; 2548076b9443SCy Schubert } 2549076b9443SCy Schubert sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?", 2550076b9443SCy Schubert zConflict, zTable); 2551076b9443SCy Schubert j = strlen30(zSql); 2552076b9443SCy Schubert for(i=1; i<nCol; i++){ 2553076b9443SCy Schubert zSql[j++] = ','; 2554076b9443SCy Schubert zSql[j++] = '?'; 2555076b9443SCy Schubert } 2556076b9443SCy Schubert zSql[j++] = ')'; 2557076b9443SCy Schubert zSql[j] = 0; 2558076b9443SCy Schubert rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 2559076b9443SCy Schubert free(zSql); 2560076b9443SCy Schubert if( rc ){ 2561076b9443SCy Schubert Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0); 2562076b9443SCy Schubert sqlite3_finalize(pStmt); 2563076b9443SCy Schubert return TCL_ERROR; 2564076b9443SCy Schubert } 2565076b9443SCy Schubert in = fopen(zFile, "rb"); 2566076b9443SCy Schubert if( in==0 ){ 2567076b9443SCy Schubert Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, (char*)0); 2568076b9443SCy Schubert sqlite3_finalize(pStmt); 2569076b9443SCy Schubert return TCL_ERROR; 2570076b9443SCy Schubert } 2571076b9443SCy Schubert azCol = malloc( sizeof(azCol[0])*(nCol+1) ); 2572076b9443SCy Schubert if( azCol==0 ) { 2573076b9443SCy Schubert Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0); 2574076b9443SCy Schubert fclose(in); 2575076b9443SCy Schubert return TCL_ERROR; 2576076b9443SCy Schubert } 2577076b9443SCy Schubert (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0); 2578076b9443SCy Schubert zCommit = "COMMIT"; 2579076b9443SCy Schubert while( (zLine = local_getline(0, in))!=0 ){ 2580076b9443SCy Schubert char *z; 2581076b9443SCy Schubert lineno++; 2582076b9443SCy Schubert azCol[0] = zLine; 2583076b9443SCy Schubert for(i=0, z=zLine; *z; z++){ 2584076b9443SCy Schubert if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){ 2585076b9443SCy Schubert *z = 0; 2586076b9443SCy Schubert i++; 2587076b9443SCy Schubert if( i<nCol ){ 2588076b9443SCy Schubert azCol[i] = &z[nSep]; 2589076b9443SCy Schubert z += nSep-1; 2590076b9443SCy Schubert } 2591076b9443SCy Schubert } 2592076b9443SCy Schubert } 2593076b9443SCy Schubert if( i+1!=nCol ){ 2594076b9443SCy Schubert char *zErr; 2595076b9443SCy Schubert int nErr = strlen30(zFile) + 200; 2596076b9443SCy Schubert zErr = malloc(nErr); 2597076b9443SCy Schubert if( zErr ){ 2598076b9443SCy Schubert sqlite3_snprintf(nErr, zErr, 2599076b9443SCy Schubert "Error: %s line %d: expected %d columns of data but found %d", 2600076b9443SCy Schubert zFile, lineno, nCol, i+1); 2601076b9443SCy Schubert Tcl_AppendResult(interp, zErr, (char*)0); 2602076b9443SCy Schubert free(zErr); 2603076b9443SCy Schubert } 2604076b9443SCy Schubert zCommit = "ROLLBACK"; 2605076b9443SCy Schubert break; 2606076b9443SCy Schubert } 2607076b9443SCy Schubert for(i=0; i<nCol; i++){ 2608076b9443SCy Schubert /* check for null data, if so, bind as null */ 2609076b9443SCy Schubert if( (nNull>0 && strcmp(azCol[i], zNull)==0) 2610076b9443SCy Schubert || strlen30(azCol[i])==0 2611076b9443SCy Schubert ){ 2612076b9443SCy Schubert sqlite3_bind_null(pStmt, i+1); 2613076b9443SCy Schubert }else{ 2614076b9443SCy Schubert sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); 2615076b9443SCy Schubert } 2616076b9443SCy Schubert } 2617076b9443SCy Schubert sqlite3_step(pStmt); 2618076b9443SCy Schubert rc = sqlite3_reset(pStmt); 2619076b9443SCy Schubert free(zLine); 2620076b9443SCy Schubert if( rc!=SQLITE_OK ){ 2621076b9443SCy Schubert Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), (char*)0); 2622076b9443SCy Schubert zCommit = "ROLLBACK"; 2623076b9443SCy Schubert break; 2624076b9443SCy Schubert } 2625076b9443SCy Schubert } 2626076b9443SCy Schubert free(azCol); 2627076b9443SCy Schubert fclose(in); 2628076b9443SCy Schubert sqlite3_finalize(pStmt); 2629076b9443SCy Schubert (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0); 2630076b9443SCy Schubert 2631076b9443SCy Schubert if( zCommit[0] == 'C' ){ 2632076b9443SCy Schubert /* success, set result as number of lines processed */ 2633076b9443SCy Schubert pResult = Tcl_GetObjResult(interp); 2634076b9443SCy Schubert Tcl_SetIntObj(pResult, lineno); 2635076b9443SCy Schubert rc = TCL_OK; 2636076b9443SCy Schubert }else{ 2637076b9443SCy Schubert /* failure, append lineno where failed */ 2638076b9443SCy Schubert sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno); 2639076b9443SCy Schubert Tcl_AppendResult(interp,", failed while processing line: ",zLineNum, 2640076b9443SCy Schubert (char*)0); 2641076b9443SCy Schubert rc = TCL_ERROR; 2642076b9443SCy Schubert } 2643076b9443SCy Schubert break; 2644076b9443SCy Schubert } 2645076b9443SCy Schubert 2646076b9443SCy Schubert /* 2647bca4681bSCy Schubert ** $db deserialize ?-maxsize N? ?-readonly BOOL? ?DATABASE? VALUE 2648076b9443SCy Schubert ** 2649076b9443SCy Schubert ** Reopen DATABASE (default "main") using the content in $VALUE 2650076b9443SCy Schubert */ 2651076b9443SCy Schubert case DB_DESERIALIZE: { 26520197ba46SCy Schubert #ifdef SQLITE_OMIT_DESERIALIZE 2653076b9443SCy Schubert Tcl_AppendResult(interp, "MEMDB not available in this build", 2654076b9443SCy Schubert (char*)0); 2655076b9443SCy Schubert rc = TCL_ERROR; 2656076b9443SCy Schubert #else 2657bca4681bSCy Schubert const char *zSchema = 0; 2658bca4681bSCy Schubert Tcl_Obj *pValue = 0; 2659076b9443SCy Schubert unsigned char *pBA; 2660076b9443SCy Schubert unsigned char *pData; 2661076b9443SCy Schubert int len, xrc; 2662bca4681bSCy Schubert sqlite3_int64 mxSize = 0; 2663bca4681bSCy Schubert int i; 2664bca4681bSCy Schubert int isReadonly = 0; 2665076b9443SCy Schubert 2666bca4681bSCy Schubert 2667bca4681bSCy Schubert if( objc<3 ){ 2668076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? VALUE"); 2669076b9443SCy Schubert rc = TCL_ERROR; 2670076b9443SCy Schubert break; 2671076b9443SCy Schubert } 2672bca4681bSCy Schubert for(i=2; i<objc-1; i++){ 2673bca4681bSCy Schubert const char *z = Tcl_GetString(objv[i]); 2674bca4681bSCy Schubert if( strcmp(z,"-maxsize")==0 && i<objc-2 ){ 26750197ba46SCy Schubert Tcl_WideInt x; 26760197ba46SCy Schubert rc = Tcl_GetWideIntFromObj(interp, objv[++i], &x); 2677bca4681bSCy Schubert if( rc ) goto deserialize_error; 26780197ba46SCy Schubert mxSize = x; 2679bca4681bSCy Schubert continue; 2680bca4681bSCy Schubert } 2681bca4681bSCy Schubert if( strcmp(z,"-readonly")==0 && i<objc-2 ){ 2682bca4681bSCy Schubert rc = Tcl_GetBooleanFromObj(interp, objv[++i], &isReadonly); 2683bca4681bSCy Schubert if( rc ) goto deserialize_error; 2684bca4681bSCy Schubert continue; 2685bca4681bSCy Schubert } 2686bca4681bSCy Schubert if( zSchema==0 && i==objc-2 && z[0]!='-' ){ 2687bca4681bSCy Schubert zSchema = z; 2688bca4681bSCy Schubert continue; 2689bca4681bSCy Schubert } 2690bca4681bSCy Schubert Tcl_AppendResult(interp, "unknown option: ", z, (char*)0); 2691bca4681bSCy Schubert rc = TCL_ERROR; 2692bca4681bSCy Schubert goto deserialize_error; 2693bca4681bSCy Schubert } 2694bca4681bSCy Schubert pValue = objv[objc-1]; 2695076b9443SCy Schubert pBA = Tcl_GetByteArrayFromObj(pValue, &len); 2696076b9443SCy Schubert pData = sqlite3_malloc64( len ); 2697076b9443SCy Schubert if( pData==0 && len>0 ){ 2698076b9443SCy Schubert Tcl_AppendResult(interp, "out of memory", (char*)0); 2699076b9443SCy Schubert rc = TCL_ERROR; 2700076b9443SCy Schubert }else{ 2701bca4681bSCy Schubert int flags; 2702076b9443SCy Schubert if( len>0 ) memcpy(pData, pBA, len); 2703bca4681bSCy Schubert if( isReadonly ){ 2704bca4681bSCy Schubert flags = SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_READONLY; 2705bca4681bSCy Schubert }else{ 2706bca4681bSCy Schubert flags = SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE; 2707bca4681bSCy Schubert } 2708bca4681bSCy Schubert xrc = sqlite3_deserialize(pDb->db, zSchema, pData, len, len, flags); 2709076b9443SCy Schubert if( xrc ){ 2710076b9443SCy Schubert Tcl_AppendResult(interp, "unable to set MEMDB content", (char*)0); 2711076b9443SCy Schubert rc = TCL_ERROR; 2712076b9443SCy Schubert } 2713bca4681bSCy Schubert if( mxSize>0 ){ 2714bca4681bSCy Schubert sqlite3_file_control(pDb->db, zSchema,SQLITE_FCNTL_SIZE_LIMIT,&mxSize); 2715076b9443SCy Schubert } 2716bca4681bSCy Schubert } 2717bca4681bSCy Schubert deserialize_error: 2718076b9443SCy Schubert #endif 2719076b9443SCy Schubert break; 2720076b9443SCy Schubert } 2721076b9443SCy Schubert 2722076b9443SCy Schubert /* 2723076b9443SCy Schubert ** $db enable_load_extension BOOLEAN 2724076b9443SCy Schubert ** 2725076b9443SCy Schubert ** Turn the extension loading feature on or off. It if off by 2726076b9443SCy Schubert ** default. 2727076b9443SCy Schubert */ 2728076b9443SCy Schubert case DB_ENABLE_LOAD_EXTENSION: { 2729076b9443SCy Schubert #ifndef SQLITE_OMIT_LOAD_EXTENSION 2730076b9443SCy Schubert int onoff; 2731076b9443SCy Schubert if( objc!=3 ){ 2732076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN"); 2733076b9443SCy Schubert return TCL_ERROR; 2734076b9443SCy Schubert } 2735076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ 2736076b9443SCy Schubert return TCL_ERROR; 2737076b9443SCy Schubert } 2738076b9443SCy Schubert sqlite3_enable_load_extension(pDb->db, onoff); 2739076b9443SCy Schubert break; 2740076b9443SCy Schubert #else 2741076b9443SCy Schubert Tcl_AppendResult(interp, "extension loading is turned off at compile-time", 2742076b9443SCy Schubert (char*)0); 2743076b9443SCy Schubert return TCL_ERROR; 2744076b9443SCy Schubert #endif 2745076b9443SCy Schubert } 2746076b9443SCy Schubert 2747076b9443SCy Schubert /* 2748076b9443SCy Schubert ** $db errorcode 2749076b9443SCy Schubert ** 2750076b9443SCy Schubert ** Return the numeric error code that was returned by the most recent 2751076b9443SCy Schubert ** call to sqlite3_exec(). 2752076b9443SCy Schubert */ 2753076b9443SCy Schubert case DB_ERRORCODE: { 2754076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); 2755076b9443SCy Schubert break; 2756076b9443SCy Schubert } 2757076b9443SCy Schubert 2758076b9443SCy Schubert /* 27594fe1295cSCy Schubert ** $db erroroffset 27604fe1295cSCy Schubert ** 27614fe1295cSCy Schubert ** Return the numeric error code that was returned by the most recent 27624fe1295cSCy Schubert ** call to sqlite3_exec(). 27634fe1295cSCy Schubert */ 27644fe1295cSCy Schubert case DB_ERROROFFSET: { 27654fe1295cSCy Schubert Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_error_offset(pDb->db))); 27664fe1295cSCy Schubert break; 27674fe1295cSCy Schubert } 27684fe1295cSCy Schubert 27694fe1295cSCy Schubert /* 2770076b9443SCy Schubert ** $db exists $sql 2771076b9443SCy Schubert ** $db onecolumn $sql 2772076b9443SCy Schubert ** 2773076b9443SCy Schubert ** The onecolumn method is the equivalent of: 2774076b9443SCy Schubert ** lindex [$db eval $sql] 0 2775076b9443SCy Schubert */ 2776076b9443SCy Schubert case DB_EXISTS: 2777076b9443SCy Schubert case DB_ONECOLUMN: { 2778076b9443SCy Schubert Tcl_Obj *pResult = 0; 2779076b9443SCy Schubert DbEvalContext sEval; 2780076b9443SCy Schubert if( objc!=3 ){ 2781076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 2782076b9443SCy Schubert return TCL_ERROR; 2783076b9443SCy Schubert } 2784076b9443SCy Schubert 2785076b9443SCy Schubert dbEvalInit(&sEval, pDb, objv[2], 0, 0); 2786076b9443SCy Schubert rc = dbEvalStep(&sEval); 2787076b9443SCy Schubert if( choice==DB_ONECOLUMN ){ 2788076b9443SCy Schubert if( rc==TCL_OK ){ 2789076b9443SCy Schubert pResult = dbEvalColumnValue(&sEval, 0); 2790076b9443SCy Schubert }else if( rc==TCL_BREAK ){ 2791076b9443SCy Schubert Tcl_ResetResult(interp); 2792076b9443SCy Schubert } 2793076b9443SCy Schubert }else if( rc==TCL_BREAK || rc==TCL_OK ){ 2794076b9443SCy Schubert pResult = Tcl_NewBooleanObj(rc==TCL_OK); 2795076b9443SCy Schubert } 2796076b9443SCy Schubert dbEvalFinalize(&sEval); 2797076b9443SCy Schubert if( pResult ) Tcl_SetObjResult(interp, pResult); 2798076b9443SCy Schubert 2799076b9443SCy Schubert if( rc==TCL_BREAK ){ 2800076b9443SCy Schubert rc = TCL_OK; 2801076b9443SCy Schubert } 2802076b9443SCy Schubert break; 2803076b9443SCy Schubert } 2804076b9443SCy Schubert 2805076b9443SCy Schubert /* 2806076b9443SCy Schubert ** $db eval ?options? $sql ?array? ?{ ...code... }? 2807076b9443SCy Schubert ** 2808076b9443SCy Schubert ** The SQL statement in $sql is evaluated. For each row, the values are 2809076b9443SCy Schubert ** placed in elements of the array named "array" and ...code... is executed. 2810076b9443SCy Schubert ** If "array" and "code" are omitted, then no callback is every invoked. 2811076b9443SCy Schubert ** If "array" is an empty string, then the values are placed in variables 2812076b9443SCy Schubert ** that have the same name as the fields extracted by the query. 2813076b9443SCy Schubert */ 2814076b9443SCy Schubert case DB_EVAL: { 2815076b9443SCy Schubert int evalFlags = 0; 2816076b9443SCy Schubert const char *zOpt; 2817076b9443SCy Schubert while( objc>3 && (zOpt = Tcl_GetString(objv[2]))!=0 && zOpt[0]=='-' ){ 2818076b9443SCy Schubert if( strcmp(zOpt, "-withoutnulls")==0 ){ 2819076b9443SCy Schubert evalFlags |= SQLITE_EVAL_WITHOUTNULLS; 2820076b9443SCy Schubert } 2821076b9443SCy Schubert else{ 2822076b9443SCy Schubert Tcl_AppendResult(interp, "unknown option: \"", zOpt, "\"", (void*)0); 2823076b9443SCy Schubert return TCL_ERROR; 2824076b9443SCy Schubert } 2825076b9443SCy Schubert objc--; 2826076b9443SCy Schubert objv++; 2827076b9443SCy Schubert } 2828076b9443SCy Schubert if( objc<3 || objc>5 ){ 2829076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, 2830076b9443SCy Schubert "?OPTIONS? SQL ?ARRAY-NAME? ?SCRIPT?"); 2831076b9443SCy Schubert return TCL_ERROR; 2832076b9443SCy Schubert } 2833076b9443SCy Schubert 2834076b9443SCy Schubert if( objc==3 ){ 2835076b9443SCy Schubert DbEvalContext sEval; 2836076b9443SCy Schubert Tcl_Obj *pRet = Tcl_NewObj(); 2837076b9443SCy Schubert Tcl_IncrRefCount(pRet); 2838076b9443SCy Schubert dbEvalInit(&sEval, pDb, objv[2], 0, 0); 2839076b9443SCy Schubert while( TCL_OK==(rc = dbEvalStep(&sEval)) ){ 2840076b9443SCy Schubert int i; 2841076b9443SCy Schubert int nCol; 2842076b9443SCy Schubert dbEvalRowInfo(&sEval, &nCol, 0); 2843076b9443SCy Schubert for(i=0; i<nCol; i++){ 2844076b9443SCy Schubert Tcl_ListObjAppendElement(interp, pRet, dbEvalColumnValue(&sEval, i)); 2845076b9443SCy Schubert } 2846076b9443SCy Schubert } 2847076b9443SCy Schubert dbEvalFinalize(&sEval); 2848076b9443SCy Schubert if( rc==TCL_BREAK ){ 2849076b9443SCy Schubert Tcl_SetObjResult(interp, pRet); 2850076b9443SCy Schubert rc = TCL_OK; 2851076b9443SCy Schubert } 2852076b9443SCy Schubert Tcl_DecrRefCount(pRet); 2853076b9443SCy Schubert }else{ 2854076b9443SCy Schubert ClientData cd2[2]; 2855076b9443SCy Schubert DbEvalContext *p; 2856076b9443SCy Schubert Tcl_Obj *pArray = 0; 2857076b9443SCy Schubert Tcl_Obj *pScript; 2858076b9443SCy Schubert 2859076b9443SCy Schubert if( objc>=5 && *(char *)Tcl_GetString(objv[3]) ){ 2860076b9443SCy Schubert pArray = objv[3]; 2861076b9443SCy Schubert } 2862076b9443SCy Schubert pScript = objv[objc-1]; 2863076b9443SCy Schubert Tcl_IncrRefCount(pScript); 2864076b9443SCy Schubert 2865076b9443SCy Schubert p = (DbEvalContext *)Tcl_Alloc(sizeof(DbEvalContext)); 2866076b9443SCy Schubert dbEvalInit(p, pDb, objv[2], pArray, evalFlags); 2867076b9443SCy Schubert 2868076b9443SCy Schubert cd2[0] = (void *)p; 2869076b9443SCy Schubert cd2[1] = (void *)pScript; 2870076b9443SCy Schubert rc = DbEvalNextCmd(cd2, interp, TCL_OK); 2871076b9443SCy Schubert } 2872076b9443SCy Schubert break; 2873076b9443SCy Schubert } 2874076b9443SCy Schubert 2875076b9443SCy Schubert /* 2876f1b328b3SCy Schubert ** $db function NAME [OPTIONS] SCRIPT 2877076b9443SCy Schubert ** 2878076b9443SCy Schubert ** Create a new SQL function called NAME. Whenever that function is 2879076b9443SCy Schubert ** called, invoke SCRIPT to evaluate the function. 2880f1b328b3SCy Schubert ** 2881f1b328b3SCy Schubert ** Options: 2882f1b328b3SCy Schubert ** --argcount N Function has exactly N arguments 2883f1b328b3SCy Schubert ** --deterministic The function is pure 2884f1b328b3SCy Schubert ** --directonly Prohibit use inside triggers and views 28850e2816f5SCy Schubert ** --innocuous Has no side effects or information leaks 2886f1b328b3SCy Schubert ** --returntype TYPE Specify the return type of the function 2887076b9443SCy Schubert */ 2888076b9443SCy Schubert case DB_FUNCTION: { 2889076b9443SCy Schubert int flags = SQLITE_UTF8; 2890076b9443SCy Schubert SqlFunc *pFunc; 2891076b9443SCy Schubert Tcl_Obj *pScript; 2892076b9443SCy Schubert char *zName; 2893076b9443SCy Schubert int nArg = -1; 2894076b9443SCy Schubert int i; 289502273ca8SCy Schubert int eType = SQLITE_NULL; 2896076b9443SCy Schubert if( objc<4 ){ 2897076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "NAME ?SWITCHES? SCRIPT"); 2898076b9443SCy Schubert return TCL_ERROR; 2899076b9443SCy Schubert } 2900076b9443SCy Schubert for(i=3; i<(objc-1); i++){ 2901076b9443SCy Schubert const char *z = Tcl_GetString(objv[i]); 2902076b9443SCy Schubert int n = strlen30(z); 290302273ca8SCy Schubert if( n>1 && strncmp(z, "-argcount",n)==0 ){ 2904076b9443SCy Schubert if( i==(objc-2) ){ 2905076b9443SCy Schubert Tcl_AppendResult(interp, "option requires an argument: ", z,(char*)0); 2906076b9443SCy Schubert return TCL_ERROR; 2907076b9443SCy Schubert } 2908076b9443SCy Schubert if( Tcl_GetIntFromObj(interp, objv[i+1], &nArg) ) return TCL_ERROR; 2909076b9443SCy Schubert if( nArg<0 ){ 2910076b9443SCy Schubert Tcl_AppendResult(interp, "number of arguments must be non-negative", 2911076b9443SCy Schubert (char*)0); 2912076b9443SCy Schubert return TCL_ERROR; 2913076b9443SCy Schubert } 2914076b9443SCy Schubert i++; 2915076b9443SCy Schubert }else 291602273ca8SCy Schubert if( n>1 && strncmp(z, "-deterministic",n)==0 ){ 2917076b9443SCy Schubert flags |= SQLITE_DETERMINISTIC; 291802273ca8SCy Schubert }else 2919f1b328b3SCy Schubert if( n>1 && strncmp(z, "-directonly",n)==0 ){ 2920f1b328b3SCy Schubert flags |= SQLITE_DIRECTONLY; 2921f1b328b3SCy Schubert }else 29220e2816f5SCy Schubert if( n>1 && strncmp(z, "-innocuous",n)==0 ){ 29230e2816f5SCy Schubert flags |= SQLITE_INNOCUOUS; 29240e2816f5SCy Schubert }else 292502273ca8SCy Schubert if( n>1 && strncmp(z, "-returntype", n)==0 ){ 292602273ca8SCy Schubert const char *azType[] = {"integer", "real", "text", "blob", "any", 0}; 292702273ca8SCy Schubert assert( SQLITE_INTEGER==1 && SQLITE_FLOAT==2 && SQLITE_TEXT==3 ); 292802273ca8SCy Schubert assert( SQLITE_BLOB==4 && SQLITE_NULL==5 ); 292902273ca8SCy Schubert if( i==(objc-2) ){ 293002273ca8SCy Schubert Tcl_AppendResult(interp, "option requires an argument: ", z,(char*)0); 293102273ca8SCy Schubert return TCL_ERROR; 293202273ca8SCy Schubert } 293302273ca8SCy Schubert i++; 293402273ca8SCy Schubert if( Tcl_GetIndexFromObj(interp, objv[i], azType, "type", 0, &eType) ){ 293502273ca8SCy Schubert return TCL_ERROR; 293602273ca8SCy Schubert } 293702273ca8SCy Schubert eType++; 2938076b9443SCy Schubert }else{ 2939076b9443SCy Schubert Tcl_AppendResult(interp, "bad option \"", z, 2940f1b328b3SCy Schubert "\": must be -argcount, -deterministic, -directonly," 29410e2816f5SCy Schubert " -innocuous, or -returntype", (char*)0 2942076b9443SCy Schubert ); 2943076b9443SCy Schubert return TCL_ERROR; 2944076b9443SCy Schubert } 2945076b9443SCy Schubert } 2946076b9443SCy Schubert 2947076b9443SCy Schubert pScript = objv[objc-1]; 2948076b9443SCy Schubert zName = Tcl_GetStringFromObj(objv[2], 0); 2949076b9443SCy Schubert pFunc = findSqlFunc(pDb, zName); 2950076b9443SCy Schubert if( pFunc==0 ) return TCL_ERROR; 2951076b9443SCy Schubert if( pFunc->pScript ){ 2952076b9443SCy Schubert Tcl_DecrRefCount(pFunc->pScript); 2953076b9443SCy Schubert } 2954076b9443SCy Schubert pFunc->pScript = pScript; 2955076b9443SCy Schubert Tcl_IncrRefCount(pScript); 2956076b9443SCy Schubert pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript); 295702273ca8SCy Schubert pFunc->eType = eType; 2958076b9443SCy Schubert rc = sqlite3_create_function(pDb->db, zName, nArg, flags, 2959076b9443SCy Schubert pFunc, tclSqlFunc, 0, 0); 2960076b9443SCy Schubert if( rc!=SQLITE_OK ){ 2961076b9443SCy Schubert rc = TCL_ERROR; 2962076b9443SCy Schubert Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 2963076b9443SCy Schubert } 2964076b9443SCy Schubert break; 2965076b9443SCy Schubert } 2966076b9443SCy Schubert 2967076b9443SCy Schubert /* 2968076b9443SCy Schubert ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID 2969076b9443SCy Schubert */ 2970076b9443SCy Schubert case DB_INCRBLOB: { 2971076b9443SCy Schubert #ifdef SQLITE_OMIT_INCRBLOB 2972076b9443SCy Schubert Tcl_AppendResult(interp, "incrblob not available in this build", (char*)0); 2973076b9443SCy Schubert return TCL_ERROR; 2974076b9443SCy Schubert #else 2975076b9443SCy Schubert int isReadonly = 0; 2976076b9443SCy Schubert const char *zDb = "main"; 2977076b9443SCy Schubert const char *zTable; 2978076b9443SCy Schubert const char *zColumn; 2979076b9443SCy Schubert Tcl_WideInt iRow; 2980076b9443SCy Schubert 2981076b9443SCy Schubert /* Check for the -readonly option */ 2982076b9443SCy Schubert if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){ 2983076b9443SCy Schubert isReadonly = 1; 2984076b9443SCy Schubert } 2985076b9443SCy Schubert 2986076b9443SCy Schubert if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){ 2987076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID"); 2988076b9443SCy Schubert return TCL_ERROR; 2989076b9443SCy Schubert } 2990076b9443SCy Schubert 2991076b9443SCy Schubert if( objc==(6+isReadonly) ){ 299270b56f4bSCy Schubert zDb = Tcl_GetString(objv[2+isReadonly]); 2993076b9443SCy Schubert } 2994076b9443SCy Schubert zTable = Tcl_GetString(objv[objc-3]); 2995076b9443SCy Schubert zColumn = Tcl_GetString(objv[objc-2]); 2996076b9443SCy Schubert rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow); 2997076b9443SCy Schubert 2998076b9443SCy Schubert if( rc==TCL_OK ){ 2999076b9443SCy Schubert rc = createIncrblobChannel( 3000076b9443SCy Schubert interp, pDb, zDb, zTable, zColumn, (sqlite3_int64)iRow, isReadonly 3001076b9443SCy Schubert ); 3002076b9443SCy Schubert } 3003076b9443SCy Schubert #endif 3004076b9443SCy Schubert break; 3005076b9443SCy Schubert } 3006076b9443SCy Schubert 3007076b9443SCy Schubert /* 3008076b9443SCy Schubert ** $db interrupt 3009076b9443SCy Schubert ** 3010076b9443SCy Schubert ** Interrupt the execution of the inner-most SQL interpreter. This 3011076b9443SCy Schubert ** causes the SQL statement to return an error of SQLITE_INTERRUPT. 3012076b9443SCy Schubert */ 3013076b9443SCy Schubert case DB_INTERRUPT: { 3014076b9443SCy Schubert sqlite3_interrupt(pDb->db); 3015076b9443SCy Schubert break; 3016076b9443SCy Schubert } 3017076b9443SCy Schubert 3018076b9443SCy Schubert /* 3019076b9443SCy Schubert ** $db nullvalue ?STRING? 3020076b9443SCy Schubert ** 3021076b9443SCy Schubert ** Change text used when a NULL comes back from the database. If ?STRING? 3022076b9443SCy Schubert ** is not present, then the current string used for NULL is returned. 3023076b9443SCy Schubert ** If STRING is present, then STRING is returned. 3024076b9443SCy Schubert ** 3025076b9443SCy Schubert */ 3026076b9443SCy Schubert case DB_NULLVALUE: { 3027076b9443SCy Schubert if( objc!=2 && objc!=3 ){ 3028076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE"); 3029076b9443SCy Schubert return TCL_ERROR; 3030076b9443SCy Schubert } 3031076b9443SCy Schubert if( objc==3 ){ 3032076b9443SCy Schubert int len; 3033076b9443SCy Schubert char *zNull = Tcl_GetStringFromObj(objv[2], &len); 3034076b9443SCy Schubert if( pDb->zNull ){ 3035076b9443SCy Schubert Tcl_Free(pDb->zNull); 3036076b9443SCy Schubert } 3037076b9443SCy Schubert if( zNull && len>0 ){ 3038076b9443SCy Schubert pDb->zNull = Tcl_Alloc( len + 1 ); 3039076b9443SCy Schubert memcpy(pDb->zNull, zNull, len); 3040076b9443SCy Schubert pDb->zNull[len] = '\0'; 3041076b9443SCy Schubert }else{ 3042076b9443SCy Schubert pDb->zNull = 0; 3043076b9443SCy Schubert } 3044076b9443SCy Schubert } 3045076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewStringObj(pDb->zNull, -1)); 3046076b9443SCy Schubert break; 3047076b9443SCy Schubert } 3048076b9443SCy Schubert 3049076b9443SCy Schubert /* 3050076b9443SCy Schubert ** $db last_insert_rowid 3051076b9443SCy Schubert ** 3052076b9443SCy Schubert ** Return an integer which is the ROWID for the most recent insert. 3053076b9443SCy Schubert */ 3054076b9443SCy Schubert case DB_LAST_INSERT_ROWID: { 3055076b9443SCy Schubert Tcl_Obj *pResult; 3056076b9443SCy Schubert Tcl_WideInt rowid; 3057076b9443SCy Schubert if( objc!=2 ){ 3058076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, ""); 3059076b9443SCy Schubert return TCL_ERROR; 3060076b9443SCy Schubert } 3061076b9443SCy Schubert rowid = sqlite3_last_insert_rowid(pDb->db); 3062076b9443SCy Schubert pResult = Tcl_GetObjResult(interp); 3063076b9443SCy Schubert Tcl_SetWideIntObj(pResult, rowid); 3064076b9443SCy Schubert break; 3065076b9443SCy Schubert } 3066076b9443SCy Schubert 3067076b9443SCy Schubert /* 3068076b9443SCy Schubert ** The DB_ONECOLUMN method is implemented together with DB_EXISTS. 3069076b9443SCy Schubert */ 3070076b9443SCy Schubert 3071076b9443SCy Schubert /* $db progress ?N CALLBACK? 3072076b9443SCy Schubert ** 3073076b9443SCy Schubert ** Invoke the given callback every N virtual machine opcodes while executing 3074076b9443SCy Schubert ** queries. 3075076b9443SCy Schubert */ 3076076b9443SCy Schubert case DB_PROGRESS: { 3077076b9443SCy Schubert if( objc==2 ){ 3078076b9443SCy Schubert if( pDb->zProgress ){ 3079076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zProgress, (char*)0); 3080076b9443SCy Schubert } 30817bba9d94SCy Schubert #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 30827bba9d94SCy Schubert sqlite3_progress_handler(pDb->db, 0, 0, 0); 30837bba9d94SCy Schubert #endif 3084076b9443SCy Schubert }else if( objc==4 ){ 3085076b9443SCy Schubert char *zProgress; 3086076b9443SCy Schubert int len; 3087076b9443SCy Schubert int N; 3088076b9443SCy Schubert if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ 3089076b9443SCy Schubert return TCL_ERROR; 3090076b9443SCy Schubert }; 3091076b9443SCy Schubert if( pDb->zProgress ){ 3092076b9443SCy Schubert Tcl_Free(pDb->zProgress); 3093076b9443SCy Schubert } 3094076b9443SCy Schubert zProgress = Tcl_GetStringFromObj(objv[3], &len); 3095076b9443SCy Schubert if( zProgress && len>0 ){ 3096076b9443SCy Schubert pDb->zProgress = Tcl_Alloc( len + 1 ); 3097076b9443SCy Schubert memcpy(pDb->zProgress, zProgress, len+1); 3098076b9443SCy Schubert }else{ 3099076b9443SCy Schubert pDb->zProgress = 0; 3100076b9443SCy Schubert } 3101076b9443SCy Schubert #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 3102076b9443SCy Schubert if( pDb->zProgress ){ 3103076b9443SCy Schubert pDb->interp = interp; 3104076b9443SCy Schubert sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb); 3105076b9443SCy Schubert }else{ 3106076b9443SCy Schubert sqlite3_progress_handler(pDb->db, 0, 0, 0); 3107076b9443SCy Schubert } 3108076b9443SCy Schubert #endif 3109076b9443SCy Schubert }else{ 3110076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); 3111076b9443SCy Schubert return TCL_ERROR; 3112076b9443SCy Schubert } 3113076b9443SCy Schubert break; 3114076b9443SCy Schubert } 3115076b9443SCy Schubert 3116076b9443SCy Schubert /* $db profile ?CALLBACK? 3117076b9443SCy Schubert ** 3118076b9443SCy Schubert ** Make arrangements to invoke the CALLBACK routine after each SQL statement 3119076b9443SCy Schubert ** that has run. The text of the SQL and the amount of elapse time are 3120076b9443SCy Schubert ** appended to CALLBACK before the script is run. 3121076b9443SCy Schubert */ 3122076b9443SCy Schubert case DB_PROFILE: { 3123076b9443SCy Schubert if( objc>3 ){ 3124076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 3125076b9443SCy Schubert return TCL_ERROR; 3126076b9443SCy Schubert }else if( objc==2 ){ 3127076b9443SCy Schubert if( pDb->zProfile ){ 3128076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zProfile, (char*)0); 3129076b9443SCy Schubert } 3130076b9443SCy Schubert }else{ 3131076b9443SCy Schubert char *zProfile; 3132076b9443SCy Schubert int len; 3133076b9443SCy Schubert if( pDb->zProfile ){ 3134076b9443SCy Schubert Tcl_Free(pDb->zProfile); 3135076b9443SCy Schubert } 3136076b9443SCy Schubert zProfile = Tcl_GetStringFromObj(objv[2], &len); 3137076b9443SCy Schubert if( zProfile && len>0 ){ 3138076b9443SCy Schubert pDb->zProfile = Tcl_Alloc( len + 1 ); 3139076b9443SCy Schubert memcpy(pDb->zProfile, zProfile, len+1); 3140076b9443SCy Schubert }else{ 3141076b9443SCy Schubert pDb->zProfile = 0; 3142076b9443SCy Schubert } 3143076b9443SCy Schubert #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ 3144076b9443SCy Schubert !defined(SQLITE_OMIT_DEPRECATED) 3145076b9443SCy Schubert if( pDb->zProfile ){ 3146076b9443SCy Schubert pDb->interp = interp; 3147076b9443SCy Schubert sqlite3_profile(pDb->db, DbProfileHandler, pDb); 3148076b9443SCy Schubert }else{ 3149076b9443SCy Schubert sqlite3_profile(pDb->db, 0, 0); 3150076b9443SCy Schubert } 3151076b9443SCy Schubert #endif 3152076b9443SCy Schubert } 3153076b9443SCy Schubert break; 3154076b9443SCy Schubert } 3155076b9443SCy Schubert 3156076b9443SCy Schubert /* 3157076b9443SCy Schubert ** $db rekey KEY 3158076b9443SCy Schubert ** 3159076b9443SCy Schubert ** Change the encryption key on the currently open database. 3160076b9443SCy Schubert */ 3161076b9443SCy Schubert case DB_REKEY: { 3162076b9443SCy Schubert if( objc!=3 ){ 3163076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "KEY"); 3164076b9443SCy Schubert return TCL_ERROR; 3165076b9443SCy Schubert } 3166076b9443SCy Schubert break; 3167076b9443SCy Schubert } 3168076b9443SCy Schubert 3169076b9443SCy Schubert /* $db restore ?DATABASE? FILENAME 3170076b9443SCy Schubert ** 3171076b9443SCy Schubert ** Open a database file named FILENAME. Transfer the content 3172076b9443SCy Schubert ** of FILENAME into the local database DATABASE (default: "main"). 3173076b9443SCy Schubert */ 3174076b9443SCy Schubert case DB_RESTORE: { 3175076b9443SCy Schubert const char *zSrcFile; 3176076b9443SCy Schubert const char *zDestDb; 3177076b9443SCy Schubert sqlite3 *pSrc; 3178076b9443SCy Schubert sqlite3_backup *pBackup; 3179076b9443SCy Schubert int nTimeout = 0; 3180076b9443SCy Schubert 3181076b9443SCy Schubert if( objc==3 ){ 3182076b9443SCy Schubert zDestDb = "main"; 3183076b9443SCy Schubert zSrcFile = Tcl_GetString(objv[2]); 3184076b9443SCy Schubert }else if( objc==4 ){ 3185076b9443SCy Schubert zDestDb = Tcl_GetString(objv[2]); 3186076b9443SCy Schubert zSrcFile = Tcl_GetString(objv[3]); 3187076b9443SCy Schubert }else{ 3188076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 3189076b9443SCy Schubert return TCL_ERROR; 3190076b9443SCy Schubert } 3191076b9443SCy Schubert rc = sqlite3_open_v2(zSrcFile, &pSrc, 3192076b9443SCy Schubert SQLITE_OPEN_READONLY | pDb->openFlags, 0); 3193076b9443SCy Schubert if( rc!=SQLITE_OK ){ 3194076b9443SCy Schubert Tcl_AppendResult(interp, "cannot open source database: ", 3195076b9443SCy Schubert sqlite3_errmsg(pSrc), (char*)0); 3196076b9443SCy Schubert sqlite3_close(pSrc); 3197076b9443SCy Schubert return TCL_ERROR; 3198076b9443SCy Schubert } 3199076b9443SCy Schubert pBackup = sqlite3_backup_init(pDb->db, zDestDb, pSrc, "main"); 3200076b9443SCy Schubert if( pBackup==0 ){ 3201076b9443SCy Schubert Tcl_AppendResult(interp, "restore failed: ", 3202076b9443SCy Schubert sqlite3_errmsg(pDb->db), (char*)0); 3203076b9443SCy Schubert sqlite3_close(pSrc); 3204076b9443SCy Schubert return TCL_ERROR; 3205076b9443SCy Schubert } 3206076b9443SCy Schubert while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK 3207076b9443SCy Schubert || rc==SQLITE_BUSY ){ 3208076b9443SCy Schubert if( rc==SQLITE_BUSY ){ 3209076b9443SCy Schubert if( nTimeout++ >= 3 ) break; 3210076b9443SCy Schubert sqlite3_sleep(100); 3211076b9443SCy Schubert } 3212076b9443SCy Schubert } 3213076b9443SCy Schubert sqlite3_backup_finish(pBackup); 3214076b9443SCy Schubert if( rc==SQLITE_DONE ){ 3215076b9443SCy Schubert rc = TCL_OK; 3216076b9443SCy Schubert }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 3217076b9443SCy Schubert Tcl_AppendResult(interp, "restore failed: source database busy", 3218076b9443SCy Schubert (char*)0); 3219076b9443SCy Schubert rc = TCL_ERROR; 3220076b9443SCy Schubert }else{ 3221076b9443SCy Schubert Tcl_AppendResult(interp, "restore failed: ", 3222076b9443SCy Schubert sqlite3_errmsg(pDb->db), (char*)0); 3223076b9443SCy Schubert rc = TCL_ERROR; 3224076b9443SCy Schubert } 3225076b9443SCy Schubert sqlite3_close(pSrc); 3226076b9443SCy Schubert break; 3227076b9443SCy Schubert } 3228076b9443SCy Schubert 3229076b9443SCy Schubert /* 3230076b9443SCy Schubert ** $db serialize ?DATABASE? 3231076b9443SCy Schubert ** 3232076b9443SCy Schubert ** Return a serialization of a database. 3233076b9443SCy Schubert */ 3234076b9443SCy Schubert case DB_SERIALIZE: { 32350197ba46SCy Schubert #ifdef SQLITE_OMIT_DESERIALIZE 3236076b9443SCy Schubert Tcl_AppendResult(interp, "MEMDB not available in this build", 3237076b9443SCy Schubert (char*)0); 3238076b9443SCy Schubert rc = TCL_ERROR; 3239076b9443SCy Schubert #else 3240076b9443SCy Schubert const char *zSchema = objc>=3 ? Tcl_GetString(objv[2]) : "main"; 3241076b9443SCy Schubert sqlite3_int64 sz = 0; 3242076b9443SCy Schubert unsigned char *pData; 3243076b9443SCy Schubert if( objc!=2 && objc!=3 ){ 3244076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE?"); 3245076b9443SCy Schubert rc = TCL_ERROR; 3246076b9443SCy Schubert }else{ 3247076b9443SCy Schubert int needFree; 3248076b9443SCy Schubert pData = sqlite3_serialize(pDb->db, zSchema, &sz, SQLITE_SERIALIZE_NOCOPY); 3249076b9443SCy Schubert if( pData ){ 3250076b9443SCy Schubert needFree = 0; 3251076b9443SCy Schubert }else{ 3252076b9443SCy Schubert pData = sqlite3_serialize(pDb->db, zSchema, &sz, 0); 3253076b9443SCy Schubert needFree = 1; 3254076b9443SCy Schubert } 3255076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pData,sz)); 3256076b9443SCy Schubert if( needFree ) sqlite3_free(pData); 3257076b9443SCy Schubert } 3258076b9443SCy Schubert #endif 3259076b9443SCy Schubert break; 3260076b9443SCy Schubert } 3261076b9443SCy Schubert 3262076b9443SCy Schubert /* 3263076b9443SCy Schubert ** $db status (step|sort|autoindex|vmstep) 3264076b9443SCy Schubert ** 3265076b9443SCy Schubert ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or 3266076b9443SCy Schubert ** SQLITE_STMTSTATUS_SORT for the most recent eval. 3267076b9443SCy Schubert */ 3268076b9443SCy Schubert case DB_STATUS: { 3269076b9443SCy Schubert int v; 3270076b9443SCy Schubert const char *zOp; 3271076b9443SCy Schubert if( objc!=3 ){ 3272076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "(step|sort|autoindex)"); 3273076b9443SCy Schubert return TCL_ERROR; 3274076b9443SCy Schubert } 3275076b9443SCy Schubert zOp = Tcl_GetString(objv[2]); 3276076b9443SCy Schubert if( strcmp(zOp, "step")==0 ){ 3277076b9443SCy Schubert v = pDb->nStep; 3278076b9443SCy Schubert }else if( strcmp(zOp, "sort")==0 ){ 3279076b9443SCy Schubert v = pDb->nSort; 3280076b9443SCy Schubert }else if( strcmp(zOp, "autoindex")==0 ){ 3281076b9443SCy Schubert v = pDb->nIndex; 3282076b9443SCy Schubert }else if( strcmp(zOp, "vmstep")==0 ){ 3283076b9443SCy Schubert v = pDb->nVMStep; 3284076b9443SCy Schubert }else{ 3285076b9443SCy Schubert Tcl_AppendResult(interp, 3286076b9443SCy Schubert "bad argument: should be autoindex, step, sort or vmstep", 3287076b9443SCy Schubert (char*)0); 3288076b9443SCy Schubert return TCL_ERROR; 3289076b9443SCy Schubert } 3290076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewIntObj(v)); 3291076b9443SCy Schubert break; 3292076b9443SCy Schubert } 3293076b9443SCy Schubert 3294076b9443SCy Schubert /* 3295076b9443SCy Schubert ** $db timeout MILLESECONDS 3296076b9443SCy Schubert ** 3297076b9443SCy Schubert ** Delay for the number of milliseconds specified when a file is locked. 3298076b9443SCy Schubert */ 3299076b9443SCy Schubert case DB_TIMEOUT: { 3300076b9443SCy Schubert int ms; 3301076b9443SCy Schubert if( objc!=3 ){ 3302076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); 3303076b9443SCy Schubert return TCL_ERROR; 3304076b9443SCy Schubert } 3305076b9443SCy Schubert if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; 3306076b9443SCy Schubert sqlite3_busy_timeout(pDb->db, ms); 3307076b9443SCy Schubert break; 3308076b9443SCy Schubert } 3309076b9443SCy Schubert 3310076b9443SCy Schubert /* 3311076b9443SCy Schubert ** $db total_changes 3312076b9443SCy Schubert ** 3313076b9443SCy Schubert ** Return the number of rows that were modified, inserted, or deleted 3314076b9443SCy Schubert ** since the database handle was created. 3315076b9443SCy Schubert */ 3316076b9443SCy Schubert case DB_TOTAL_CHANGES: { 3317076b9443SCy Schubert Tcl_Obj *pResult; 3318076b9443SCy Schubert if( objc!=2 ){ 3319076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, ""); 3320076b9443SCy Schubert return TCL_ERROR; 3321076b9443SCy Schubert } 3322076b9443SCy Schubert pResult = Tcl_GetObjResult(interp); 33230197ba46SCy Schubert Tcl_SetWideIntObj(pResult, sqlite3_total_changes64(pDb->db)); 3324076b9443SCy Schubert break; 3325076b9443SCy Schubert } 3326076b9443SCy Schubert 3327076b9443SCy Schubert /* $db trace ?CALLBACK? 3328076b9443SCy Schubert ** 3329076b9443SCy Schubert ** Make arrangements to invoke the CALLBACK routine for each SQL statement 3330076b9443SCy Schubert ** that is executed. The text of the SQL is appended to CALLBACK before 3331076b9443SCy Schubert ** it is executed. 3332076b9443SCy Schubert */ 3333076b9443SCy Schubert case DB_TRACE: { 3334076b9443SCy Schubert if( objc>3 ){ 3335076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 3336076b9443SCy Schubert return TCL_ERROR; 3337076b9443SCy Schubert }else if( objc==2 ){ 3338076b9443SCy Schubert if( pDb->zTrace ){ 3339076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zTrace, (char*)0); 3340076b9443SCy Schubert } 3341076b9443SCy Schubert }else{ 3342076b9443SCy Schubert char *zTrace; 3343076b9443SCy Schubert int len; 3344076b9443SCy Schubert if( pDb->zTrace ){ 3345076b9443SCy Schubert Tcl_Free(pDb->zTrace); 3346076b9443SCy Schubert } 3347076b9443SCy Schubert zTrace = Tcl_GetStringFromObj(objv[2], &len); 3348076b9443SCy Schubert if( zTrace && len>0 ){ 3349076b9443SCy Schubert pDb->zTrace = Tcl_Alloc( len + 1 ); 3350076b9443SCy Schubert memcpy(pDb->zTrace, zTrace, len+1); 3351076b9443SCy Schubert }else{ 3352076b9443SCy Schubert pDb->zTrace = 0; 3353076b9443SCy Schubert } 3354076b9443SCy Schubert #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ 3355076b9443SCy Schubert !defined(SQLITE_OMIT_DEPRECATED) 3356076b9443SCy Schubert if( pDb->zTrace ){ 3357076b9443SCy Schubert pDb->interp = interp; 3358076b9443SCy Schubert sqlite3_trace(pDb->db, DbTraceHandler, pDb); 3359076b9443SCy Schubert }else{ 3360076b9443SCy Schubert sqlite3_trace(pDb->db, 0, 0); 3361076b9443SCy Schubert } 3362076b9443SCy Schubert #endif 3363076b9443SCy Schubert } 3364076b9443SCy Schubert break; 3365076b9443SCy Schubert } 3366076b9443SCy Schubert 3367076b9443SCy Schubert /* $db trace_v2 ?CALLBACK? ?MASK? 3368076b9443SCy Schubert ** 3369076b9443SCy Schubert ** Make arrangements to invoke the CALLBACK routine for each trace event 3370076b9443SCy Schubert ** matching the mask that is generated. The parameters are appended to 3371076b9443SCy Schubert ** CALLBACK before it is executed. 3372076b9443SCy Schubert */ 3373076b9443SCy Schubert case DB_TRACE_V2: { 3374076b9443SCy Schubert if( objc>4 ){ 3375076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK? ?MASK?"); 3376076b9443SCy Schubert return TCL_ERROR; 3377076b9443SCy Schubert }else if( objc==2 ){ 3378076b9443SCy Schubert if( pDb->zTraceV2 ){ 3379076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zTraceV2, (char*)0); 3380076b9443SCy Schubert } 3381076b9443SCy Schubert }else{ 3382076b9443SCy Schubert char *zTraceV2; 3383076b9443SCy Schubert int len; 3384076b9443SCy Schubert Tcl_WideInt wMask = 0; 3385076b9443SCy Schubert if( objc==4 ){ 3386076b9443SCy Schubert static const char *TTYPE_strs[] = { 3387076b9443SCy Schubert "statement", "profile", "row", "close", 0 3388076b9443SCy Schubert }; 3389076b9443SCy Schubert enum TTYPE_enum { 3390076b9443SCy Schubert TTYPE_STMT, TTYPE_PROFILE, TTYPE_ROW, TTYPE_CLOSE 3391076b9443SCy Schubert }; 3392076b9443SCy Schubert int i; 3393076b9443SCy Schubert if( TCL_OK!=Tcl_ListObjLength(interp, objv[3], &len) ){ 3394076b9443SCy Schubert return TCL_ERROR; 3395076b9443SCy Schubert } 3396076b9443SCy Schubert for(i=0; i<len; i++){ 3397076b9443SCy Schubert Tcl_Obj *pObj; 3398076b9443SCy Schubert int ttype; 3399076b9443SCy Schubert if( TCL_OK!=Tcl_ListObjIndex(interp, objv[3], i, &pObj) ){ 3400076b9443SCy Schubert return TCL_ERROR; 3401076b9443SCy Schubert } 3402076b9443SCy Schubert if( Tcl_GetIndexFromObj(interp, pObj, TTYPE_strs, "trace type", 3403076b9443SCy Schubert 0, &ttype)!=TCL_OK ){ 3404076b9443SCy Schubert Tcl_WideInt wType; 3405076b9443SCy Schubert Tcl_Obj *pError = Tcl_DuplicateObj(Tcl_GetObjResult(interp)); 3406076b9443SCy Schubert Tcl_IncrRefCount(pError); 3407076b9443SCy Schubert if( TCL_OK==Tcl_GetWideIntFromObj(interp, pObj, &wType) ){ 3408076b9443SCy Schubert Tcl_DecrRefCount(pError); 3409076b9443SCy Schubert wMask |= wType; 3410076b9443SCy Schubert }else{ 3411076b9443SCy Schubert Tcl_SetObjResult(interp, pError); 3412076b9443SCy Schubert Tcl_DecrRefCount(pError); 3413076b9443SCy Schubert return TCL_ERROR; 3414076b9443SCy Schubert } 3415076b9443SCy Schubert }else{ 3416076b9443SCy Schubert switch( (enum TTYPE_enum)ttype ){ 3417076b9443SCy Schubert case TTYPE_STMT: wMask |= SQLITE_TRACE_STMT; break; 3418076b9443SCy Schubert case TTYPE_PROFILE: wMask |= SQLITE_TRACE_PROFILE; break; 3419076b9443SCy Schubert case TTYPE_ROW: wMask |= SQLITE_TRACE_ROW; break; 3420076b9443SCy Schubert case TTYPE_CLOSE: wMask |= SQLITE_TRACE_CLOSE; break; 3421076b9443SCy Schubert } 3422076b9443SCy Schubert } 3423076b9443SCy Schubert } 3424076b9443SCy Schubert }else{ 3425076b9443SCy Schubert wMask = SQLITE_TRACE_STMT; /* use the "legacy" default */ 3426076b9443SCy Schubert } 3427076b9443SCy Schubert if( pDb->zTraceV2 ){ 3428076b9443SCy Schubert Tcl_Free(pDb->zTraceV2); 3429076b9443SCy Schubert } 3430076b9443SCy Schubert zTraceV2 = Tcl_GetStringFromObj(objv[2], &len); 3431076b9443SCy Schubert if( zTraceV2 && len>0 ){ 3432076b9443SCy Schubert pDb->zTraceV2 = Tcl_Alloc( len + 1 ); 3433076b9443SCy Schubert memcpy(pDb->zTraceV2, zTraceV2, len+1); 3434076b9443SCy Schubert }else{ 3435076b9443SCy Schubert pDb->zTraceV2 = 0; 3436076b9443SCy Schubert } 3437076b9443SCy Schubert #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) 3438076b9443SCy Schubert if( pDb->zTraceV2 ){ 3439076b9443SCy Schubert pDb->interp = interp; 3440076b9443SCy Schubert sqlite3_trace_v2(pDb->db, (unsigned)wMask, DbTraceV2Handler, pDb); 3441076b9443SCy Schubert }else{ 3442076b9443SCy Schubert sqlite3_trace_v2(pDb->db, 0, 0, 0); 3443076b9443SCy Schubert } 3444076b9443SCy Schubert #endif 3445076b9443SCy Schubert } 3446076b9443SCy Schubert break; 3447076b9443SCy Schubert } 3448076b9443SCy Schubert 3449076b9443SCy Schubert /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT 3450076b9443SCy Schubert ** 3451076b9443SCy Schubert ** Start a new transaction (if we are not already in the midst of a 3452076b9443SCy Schubert ** transaction) and execute the TCL script SCRIPT. After SCRIPT 3453076b9443SCy Schubert ** completes, either commit the transaction or roll it back if SCRIPT 3454*ec994981SCy Schubert ** throws an exception. Or if no new transaction was started, do nothing. 3455076b9443SCy Schubert ** pass the exception on up the stack. 3456076b9443SCy Schubert ** 3457076b9443SCy Schubert ** This command was inspired by Dave Thomas's talk on Ruby at the 3458076b9443SCy Schubert ** 2005 O'Reilly Open Source Convention (OSCON). 3459076b9443SCy Schubert */ 3460076b9443SCy Schubert case DB_TRANSACTION: { 3461076b9443SCy Schubert Tcl_Obj *pScript; 3462076b9443SCy Schubert const char *zBegin = "SAVEPOINT _tcl_transaction"; 3463076b9443SCy Schubert if( objc!=3 && objc!=4 ){ 3464076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT"); 3465076b9443SCy Schubert return TCL_ERROR; 3466076b9443SCy Schubert } 3467076b9443SCy Schubert 3468076b9443SCy Schubert if( pDb->nTransaction==0 && objc==4 ){ 3469076b9443SCy Schubert static const char *TTYPE_strs[] = { 3470076b9443SCy Schubert "deferred", "exclusive", "immediate", 0 3471076b9443SCy Schubert }; 3472076b9443SCy Schubert enum TTYPE_enum { 3473076b9443SCy Schubert TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE 3474076b9443SCy Schubert }; 3475076b9443SCy Schubert int ttype; 3476076b9443SCy Schubert if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type", 3477076b9443SCy Schubert 0, &ttype) ){ 3478076b9443SCy Schubert return TCL_ERROR; 3479076b9443SCy Schubert } 3480076b9443SCy Schubert switch( (enum TTYPE_enum)ttype ){ 3481076b9443SCy Schubert case TTYPE_DEFERRED: /* no-op */; break; 3482076b9443SCy Schubert case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break; 3483076b9443SCy Schubert case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break; 3484076b9443SCy Schubert } 3485076b9443SCy Schubert } 3486076b9443SCy Schubert pScript = objv[objc-1]; 3487076b9443SCy Schubert 3488076b9443SCy Schubert /* Run the SQLite BEGIN command to open a transaction or savepoint. */ 3489076b9443SCy Schubert pDb->disableAuth++; 3490076b9443SCy Schubert rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0); 3491076b9443SCy Schubert pDb->disableAuth--; 3492076b9443SCy Schubert if( rc!=SQLITE_OK ){ 3493076b9443SCy Schubert Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 3494076b9443SCy Schubert return TCL_ERROR; 3495076b9443SCy Schubert } 3496076b9443SCy Schubert pDb->nTransaction++; 3497076b9443SCy Schubert 3498076b9443SCy Schubert /* If using NRE, schedule a callback to invoke the script pScript, then 3499076b9443SCy Schubert ** a second callback to commit (or rollback) the transaction or savepoint 3500076b9443SCy Schubert ** opened above. If not using NRE, evaluate the script directly, then 3501076b9443SCy Schubert ** call function DbTransPostCmd() to commit (or rollback) the transaction 3502076b9443SCy Schubert ** or savepoint. */ 35030197ba46SCy Schubert addDatabaseRef(pDb); /* DbTransPostCmd() calls delDatabaseRef() */ 3504076b9443SCy Schubert if( DbUseNre() ){ 3505076b9443SCy Schubert Tcl_NRAddCallback(interp, DbTransPostCmd, cd, 0, 0, 0); 3506076b9443SCy Schubert (void)Tcl_NREvalObj(interp, pScript, 0); 3507076b9443SCy Schubert }else{ 3508076b9443SCy Schubert rc = DbTransPostCmd(&cd, interp, Tcl_EvalObjEx(interp, pScript, 0)); 3509076b9443SCy Schubert } 3510076b9443SCy Schubert break; 3511076b9443SCy Schubert } 3512076b9443SCy Schubert 3513076b9443SCy Schubert /* 3514076b9443SCy Schubert ** $db unlock_notify ?script? 3515076b9443SCy Schubert */ 3516076b9443SCy Schubert case DB_UNLOCK_NOTIFY: { 3517076b9443SCy Schubert #ifndef SQLITE_ENABLE_UNLOCK_NOTIFY 3518076b9443SCy Schubert Tcl_AppendResult(interp, "unlock_notify not available in this build", 3519076b9443SCy Schubert (char*)0); 3520076b9443SCy Schubert rc = TCL_ERROR; 3521076b9443SCy Schubert #else 3522076b9443SCy Schubert if( objc!=2 && objc!=3 ){ 3523076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 3524076b9443SCy Schubert rc = TCL_ERROR; 3525076b9443SCy Schubert }else{ 3526076b9443SCy Schubert void (*xNotify)(void **, int) = 0; 3527076b9443SCy Schubert void *pNotifyArg = 0; 3528076b9443SCy Schubert 3529076b9443SCy Schubert if( pDb->pUnlockNotify ){ 3530076b9443SCy Schubert Tcl_DecrRefCount(pDb->pUnlockNotify); 3531076b9443SCy Schubert pDb->pUnlockNotify = 0; 3532076b9443SCy Schubert } 3533076b9443SCy Schubert 3534076b9443SCy Schubert if( objc==3 ){ 3535076b9443SCy Schubert xNotify = DbUnlockNotify; 3536076b9443SCy Schubert pNotifyArg = (void *)pDb; 3537076b9443SCy Schubert pDb->pUnlockNotify = objv[2]; 3538076b9443SCy Schubert Tcl_IncrRefCount(pDb->pUnlockNotify); 3539076b9443SCy Schubert } 3540076b9443SCy Schubert 3541076b9443SCy Schubert if( sqlite3_unlock_notify(pDb->db, xNotify, pNotifyArg) ){ 3542076b9443SCy Schubert Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 3543076b9443SCy Schubert rc = TCL_ERROR; 3544076b9443SCy Schubert } 3545076b9443SCy Schubert } 3546076b9443SCy Schubert #endif 3547076b9443SCy Schubert break; 3548076b9443SCy Schubert } 3549076b9443SCy Schubert 3550076b9443SCy Schubert /* 3551076b9443SCy Schubert ** $db preupdate_hook count 3552076b9443SCy Schubert ** $db preupdate_hook hook ?SCRIPT? 3553076b9443SCy Schubert ** $db preupdate_hook new INDEX 3554076b9443SCy Schubert ** $db preupdate_hook old INDEX 3555076b9443SCy Schubert */ 3556076b9443SCy Schubert case DB_PREUPDATE: { 3557076b9443SCy Schubert #ifndef SQLITE_ENABLE_PREUPDATE_HOOK 3558076b9443SCy Schubert Tcl_AppendResult(interp, "preupdate_hook was omitted at compile-time", 3559076b9443SCy Schubert (char*)0); 3560076b9443SCy Schubert rc = TCL_ERROR; 3561076b9443SCy Schubert #else 3562076b9443SCy Schubert static const char *azSub[] = {"count", "depth", "hook", "new", "old", 0}; 3563076b9443SCy Schubert enum DbPreupdateSubCmd { 3564076b9443SCy Schubert PRE_COUNT, PRE_DEPTH, PRE_HOOK, PRE_NEW, PRE_OLD 3565076b9443SCy Schubert }; 3566076b9443SCy Schubert int iSub; 3567076b9443SCy Schubert 3568076b9443SCy Schubert if( objc<3 ){ 3569076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "SUB-COMMAND ?ARGS?"); 3570076b9443SCy Schubert } 3571076b9443SCy Schubert if( Tcl_GetIndexFromObj(interp, objv[2], azSub, "sub-command", 0, &iSub) ){ 3572076b9443SCy Schubert return TCL_ERROR; 3573076b9443SCy Schubert } 3574076b9443SCy Schubert 3575076b9443SCy Schubert switch( (enum DbPreupdateSubCmd)iSub ){ 3576076b9443SCy Schubert case PRE_COUNT: { 3577076b9443SCy Schubert int nCol = sqlite3_preupdate_count(pDb->db); 3578076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewIntObj(nCol)); 3579076b9443SCy Schubert break; 3580076b9443SCy Schubert } 3581076b9443SCy Schubert 3582076b9443SCy Schubert case PRE_HOOK: { 3583076b9443SCy Schubert if( objc>4 ){ 3584076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "hook ?SCRIPT?"); 3585076b9443SCy Schubert return TCL_ERROR; 3586076b9443SCy Schubert } 3587076b9443SCy Schubert DbHookCmd(interp, pDb, (objc==4 ? objv[3] : 0), &pDb->pPreUpdateHook); 3588076b9443SCy Schubert break; 3589076b9443SCy Schubert } 3590076b9443SCy Schubert 3591076b9443SCy Schubert case PRE_DEPTH: { 3592076b9443SCy Schubert Tcl_Obj *pRet; 3593076b9443SCy Schubert if( objc!=3 ){ 3594076b9443SCy Schubert Tcl_WrongNumArgs(interp, 3, objv, ""); 3595076b9443SCy Schubert return TCL_ERROR; 3596076b9443SCy Schubert } 3597076b9443SCy Schubert pRet = Tcl_NewIntObj(sqlite3_preupdate_depth(pDb->db)); 3598076b9443SCy Schubert Tcl_SetObjResult(interp, pRet); 3599076b9443SCy Schubert break; 3600076b9443SCy Schubert } 3601076b9443SCy Schubert 3602076b9443SCy Schubert case PRE_NEW: 3603076b9443SCy Schubert case PRE_OLD: { 3604076b9443SCy Schubert int iIdx; 3605076b9443SCy Schubert sqlite3_value *pValue; 3606076b9443SCy Schubert if( objc!=4 ){ 3607076b9443SCy Schubert Tcl_WrongNumArgs(interp, 3, objv, "INDEX"); 3608076b9443SCy Schubert return TCL_ERROR; 3609076b9443SCy Schubert } 3610076b9443SCy Schubert if( Tcl_GetIntFromObj(interp, objv[3], &iIdx) ){ 3611076b9443SCy Schubert return TCL_ERROR; 3612076b9443SCy Schubert } 3613076b9443SCy Schubert 3614076b9443SCy Schubert if( iSub==PRE_OLD ){ 3615076b9443SCy Schubert rc = sqlite3_preupdate_old(pDb->db, iIdx, &pValue); 3616076b9443SCy Schubert }else{ 3617076b9443SCy Schubert assert( iSub==PRE_NEW ); 3618076b9443SCy Schubert rc = sqlite3_preupdate_new(pDb->db, iIdx, &pValue); 3619076b9443SCy Schubert } 3620076b9443SCy Schubert 3621076b9443SCy Schubert if( rc==SQLITE_OK ){ 3622076b9443SCy Schubert Tcl_Obj *pObj; 3623076b9443SCy Schubert pObj = Tcl_NewStringObj((char*)sqlite3_value_text(pValue), -1); 3624076b9443SCy Schubert Tcl_SetObjResult(interp, pObj); 3625076b9443SCy Schubert }else{ 3626076b9443SCy Schubert Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 3627076b9443SCy Schubert return TCL_ERROR; 3628076b9443SCy Schubert } 3629076b9443SCy Schubert } 3630076b9443SCy Schubert } 3631076b9443SCy Schubert #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 3632076b9443SCy Schubert break; 3633076b9443SCy Schubert } 3634076b9443SCy Schubert 3635076b9443SCy Schubert /* 3636076b9443SCy Schubert ** $db wal_hook ?script? 3637076b9443SCy Schubert ** $db update_hook ?script? 3638076b9443SCy Schubert ** $db rollback_hook ?script? 3639076b9443SCy Schubert */ 3640076b9443SCy Schubert case DB_WAL_HOOK: 3641076b9443SCy Schubert case DB_UPDATE_HOOK: 3642076b9443SCy Schubert case DB_ROLLBACK_HOOK: { 3643076b9443SCy Schubert /* set ppHook to point at pUpdateHook or pRollbackHook, depending on 3644076b9443SCy Schubert ** whether [$db update_hook] or [$db rollback_hook] was invoked. 3645076b9443SCy Schubert */ 3646076b9443SCy Schubert Tcl_Obj **ppHook = 0; 3647076b9443SCy Schubert if( choice==DB_WAL_HOOK ) ppHook = &pDb->pWalHook; 3648076b9443SCy Schubert if( choice==DB_UPDATE_HOOK ) ppHook = &pDb->pUpdateHook; 3649076b9443SCy Schubert if( choice==DB_ROLLBACK_HOOK ) ppHook = &pDb->pRollbackHook; 3650076b9443SCy Schubert if( objc>3 ){ 3651076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 3652076b9443SCy Schubert return TCL_ERROR; 3653076b9443SCy Schubert } 3654076b9443SCy Schubert 3655076b9443SCy Schubert DbHookCmd(interp, pDb, (objc==3 ? objv[2] : 0), ppHook); 3656076b9443SCy Schubert break; 3657076b9443SCy Schubert } 3658076b9443SCy Schubert 3659076b9443SCy Schubert /* $db version 3660076b9443SCy Schubert ** 3661076b9443SCy Schubert ** Return the version string for this database. 3662076b9443SCy Schubert */ 3663076b9443SCy Schubert case DB_VERSION: { 3664076b9443SCy Schubert int i; 3665076b9443SCy Schubert for(i=2; i<objc; i++){ 3666076b9443SCy Schubert const char *zArg = Tcl_GetString(objv[i]); 3667076b9443SCy Schubert /* Optional arguments to $db version are used for testing purpose */ 3668076b9443SCy Schubert #ifdef SQLITE_TEST 3669076b9443SCy Schubert /* $db version -use-legacy-prepare BOOLEAN 3670076b9443SCy Schubert ** 3671076b9443SCy Schubert ** Turn the use of legacy sqlite3_prepare() on or off. 3672076b9443SCy Schubert */ 3673076b9443SCy Schubert if( strcmp(zArg, "-use-legacy-prepare")==0 && i+1<objc ){ 3674076b9443SCy Schubert i++; 3675076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &pDb->bLegacyPrepare) ){ 3676076b9443SCy Schubert return TCL_ERROR; 3677076b9443SCy Schubert } 3678076b9443SCy Schubert }else 3679076b9443SCy Schubert 3680076b9443SCy Schubert /* $db version -last-stmt-ptr 3681076b9443SCy Schubert ** 3682076b9443SCy Schubert ** Return a string which is a hex encoding of the pointer to the 3683076b9443SCy Schubert ** most recent sqlite3_stmt in the statement cache. 3684076b9443SCy Schubert */ 3685076b9443SCy Schubert if( strcmp(zArg, "-last-stmt-ptr")==0 ){ 3686076b9443SCy Schubert char zBuf[100]; 3687076b9443SCy Schubert sqlite3_snprintf(sizeof(zBuf), zBuf, "%p", 3688076b9443SCy Schubert pDb->stmtList ? pDb->stmtList->pStmt: 0); 3689076b9443SCy Schubert Tcl_SetResult(interp, zBuf, TCL_VOLATILE); 3690076b9443SCy Schubert }else 3691076b9443SCy Schubert #endif /* SQLITE_TEST */ 3692076b9443SCy Schubert { 3693076b9443SCy Schubert Tcl_AppendResult(interp, "unknown argument: ", zArg, (char*)0); 3694076b9443SCy Schubert return TCL_ERROR; 3695076b9443SCy Schubert } 3696076b9443SCy Schubert } 3697076b9443SCy Schubert if( i==2 ){ 3698076b9443SCy Schubert Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC); 3699076b9443SCy Schubert } 3700076b9443SCy Schubert break; 3701076b9443SCy Schubert } 3702076b9443SCy Schubert 3703076b9443SCy Schubert 3704076b9443SCy Schubert } /* End of the SWITCH statement */ 3705076b9443SCy Schubert return rc; 3706076b9443SCy Schubert } 3707076b9443SCy Schubert 3708076b9443SCy Schubert #if SQLITE_TCL_NRE 3709076b9443SCy Schubert /* 3710076b9443SCy Schubert ** Adaptor that provides an objCmd interface to the NRE-enabled 3711076b9443SCy Schubert ** interface implementation. 3712076b9443SCy Schubert */ 3713076b9443SCy Schubert static int SQLITE_TCLAPI DbObjCmdAdaptor( 3714076b9443SCy Schubert void *cd, 3715076b9443SCy Schubert Tcl_Interp *interp, 3716076b9443SCy Schubert int objc, 3717076b9443SCy Schubert Tcl_Obj *const*objv 3718076b9443SCy Schubert ){ 3719076b9443SCy Schubert return Tcl_NRCallObjProc(interp, DbObjCmd, cd, objc, objv); 3720076b9443SCy Schubert } 3721076b9443SCy Schubert #endif /* SQLITE_TCL_NRE */ 3722076b9443SCy Schubert 3723076b9443SCy Schubert /* 3724076b9443SCy Schubert ** Issue the usage message when the "sqlite3" command arguments are 3725076b9443SCy Schubert ** incorrect. 3726076b9443SCy Schubert */ 3727076b9443SCy Schubert static int sqliteCmdUsage( 3728076b9443SCy Schubert Tcl_Interp *interp, 3729076b9443SCy Schubert Tcl_Obj *const*objv 3730076b9443SCy Schubert ){ 3731076b9443SCy Schubert Tcl_WrongNumArgs(interp, 1, objv, 3732076b9443SCy Schubert "HANDLE ?FILENAME? ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" 37330e2816f5SCy Schubert " ?-nofollow BOOLEAN?" 3734076b9443SCy Schubert " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?" 3735076b9443SCy Schubert ); 3736076b9443SCy Schubert return TCL_ERROR; 3737076b9443SCy Schubert } 3738076b9443SCy Schubert 3739076b9443SCy Schubert /* 3740076b9443SCy Schubert ** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN? 3741076b9443SCy Schubert ** ?-create BOOLEAN? ?-nomutex BOOLEAN? 37420e2816f5SCy Schubert ** ?-nofollow BOOLEAN? 3743076b9443SCy Schubert ** 3744076b9443SCy Schubert ** This is the main Tcl command. When the "sqlite" Tcl command is 3745076b9443SCy Schubert ** invoked, this routine runs to process that command. 3746076b9443SCy Schubert ** 3747076b9443SCy Schubert ** The first argument, DBNAME, is an arbitrary name for a new 3748076b9443SCy Schubert ** database connection. This command creates a new command named 3749076b9443SCy Schubert ** DBNAME that is used to control that connection. The database 3750076b9443SCy Schubert ** connection is deleted when the DBNAME command is deleted. 3751076b9443SCy Schubert ** 3752076b9443SCy Schubert ** The second argument is the name of the database file. 3753076b9443SCy Schubert ** 3754076b9443SCy Schubert */ 3755076b9443SCy Schubert static int SQLITE_TCLAPI DbMain( 3756076b9443SCy Schubert void *cd, 3757076b9443SCy Schubert Tcl_Interp *interp, 3758076b9443SCy Schubert int objc, 3759076b9443SCy Schubert Tcl_Obj *const*objv 3760076b9443SCy Schubert ){ 3761076b9443SCy Schubert SqliteDb *p; 3762076b9443SCy Schubert const char *zArg; 3763076b9443SCy Schubert char *zErrMsg; 3764076b9443SCy Schubert int i; 3765076b9443SCy Schubert const char *zFile = 0; 3766076b9443SCy Schubert const char *zVfs = 0; 3767076b9443SCy Schubert int flags; 3768b622dc25SCy Schubert int bTranslateFileName = 1; 3769076b9443SCy Schubert Tcl_DString translatedFilename; 3770076b9443SCy Schubert int rc; 3771076b9443SCy Schubert 3772076b9443SCy Schubert /* In normal use, each TCL interpreter runs in a single thread. So 3773076b9443SCy Schubert ** by default, we can turn off mutexing on SQLite database connections. 3774076b9443SCy Schubert ** However, for testing purposes it is useful to have mutexes turned 3775076b9443SCy Schubert ** on. So, by default, mutexes default off. But if compiled with 3776076b9443SCy Schubert ** SQLITE_TCL_DEFAULT_FULLMUTEX then mutexes default on. 3777076b9443SCy Schubert */ 3778076b9443SCy Schubert #ifdef SQLITE_TCL_DEFAULT_FULLMUTEX 3779076b9443SCy Schubert flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX; 3780076b9443SCy Schubert #else 3781076b9443SCy Schubert flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX; 3782076b9443SCy Schubert #endif 3783076b9443SCy Schubert 3784076b9443SCy Schubert if( objc==1 ) return sqliteCmdUsage(interp, objv); 3785076b9443SCy Schubert if( objc==2 ){ 3786076b9443SCy Schubert zArg = Tcl_GetStringFromObj(objv[1], 0); 3787076b9443SCy Schubert if( strcmp(zArg,"-version")==0 ){ 3788076b9443SCy Schubert Tcl_AppendResult(interp,sqlite3_libversion(), (char*)0); 3789076b9443SCy Schubert return TCL_OK; 3790076b9443SCy Schubert } 3791076b9443SCy Schubert if( strcmp(zArg,"-sourceid")==0 ){ 3792076b9443SCy Schubert Tcl_AppendResult(interp,sqlite3_sourceid(), (char*)0); 3793076b9443SCy Schubert return TCL_OK; 3794076b9443SCy Schubert } 3795076b9443SCy Schubert if( strcmp(zArg,"-has-codec")==0 ){ 3796076b9443SCy Schubert Tcl_AppendResult(interp,"0",(char*)0); 3797076b9443SCy Schubert return TCL_OK; 3798076b9443SCy Schubert } 3799076b9443SCy Schubert if( zArg[0]=='-' ) return sqliteCmdUsage(interp, objv); 3800076b9443SCy Schubert } 3801076b9443SCy Schubert for(i=2; i<objc; i++){ 3802076b9443SCy Schubert zArg = Tcl_GetString(objv[i]); 3803076b9443SCy Schubert if( zArg[0]!='-' ){ 3804076b9443SCy Schubert if( zFile!=0 ) return sqliteCmdUsage(interp, objv); 3805076b9443SCy Schubert zFile = zArg; 3806076b9443SCy Schubert continue; 3807076b9443SCy Schubert } 3808076b9443SCy Schubert if( i==objc-1 ) return sqliteCmdUsage(interp, objv); 3809076b9443SCy Schubert i++; 3810076b9443SCy Schubert if( strcmp(zArg,"-key")==0 ){ 3811b622dc25SCy Schubert /* no-op */ 3812076b9443SCy Schubert }else if( strcmp(zArg, "-vfs")==0 ){ 3813076b9443SCy Schubert zVfs = Tcl_GetString(objv[i]); 3814076b9443SCy Schubert }else if( strcmp(zArg, "-readonly")==0 ){ 3815076b9443SCy Schubert int b; 3816076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 3817076b9443SCy Schubert if( b ){ 3818076b9443SCy Schubert flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); 3819076b9443SCy Schubert flags |= SQLITE_OPEN_READONLY; 3820076b9443SCy Schubert }else{ 3821076b9443SCy Schubert flags &= ~SQLITE_OPEN_READONLY; 3822076b9443SCy Schubert flags |= SQLITE_OPEN_READWRITE; 3823076b9443SCy Schubert } 3824076b9443SCy Schubert }else if( strcmp(zArg, "-create")==0 ){ 3825076b9443SCy Schubert int b; 3826076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 3827076b9443SCy Schubert if( b && (flags & SQLITE_OPEN_READONLY)==0 ){ 3828076b9443SCy Schubert flags |= SQLITE_OPEN_CREATE; 3829076b9443SCy Schubert }else{ 3830076b9443SCy Schubert flags &= ~SQLITE_OPEN_CREATE; 3831076b9443SCy Schubert } 38320e2816f5SCy Schubert }else if( strcmp(zArg, "-nofollow")==0 ){ 38330e2816f5SCy Schubert int b; 38340e2816f5SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 38350e2816f5SCy Schubert if( b ){ 38360e2816f5SCy Schubert flags |= SQLITE_OPEN_NOFOLLOW; 38370e2816f5SCy Schubert }else{ 38380e2816f5SCy Schubert flags &= ~SQLITE_OPEN_NOFOLLOW; 38390e2816f5SCy Schubert } 3840076b9443SCy Schubert }else if( strcmp(zArg, "-nomutex")==0 ){ 3841076b9443SCy Schubert int b; 3842076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 3843076b9443SCy Schubert if( b ){ 3844076b9443SCy Schubert flags |= SQLITE_OPEN_NOMUTEX; 3845076b9443SCy Schubert flags &= ~SQLITE_OPEN_FULLMUTEX; 3846076b9443SCy Schubert }else{ 3847076b9443SCy Schubert flags &= ~SQLITE_OPEN_NOMUTEX; 3848076b9443SCy Schubert } 3849076b9443SCy Schubert }else if( strcmp(zArg, "-fullmutex")==0 ){ 3850076b9443SCy Schubert int b; 3851076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 3852076b9443SCy Schubert if( b ){ 3853076b9443SCy Schubert flags |= SQLITE_OPEN_FULLMUTEX; 3854076b9443SCy Schubert flags &= ~SQLITE_OPEN_NOMUTEX; 3855076b9443SCy Schubert }else{ 3856076b9443SCy Schubert flags &= ~SQLITE_OPEN_FULLMUTEX; 3857076b9443SCy Schubert } 3858076b9443SCy Schubert }else if( strcmp(zArg, "-uri")==0 ){ 3859076b9443SCy Schubert int b; 3860076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 3861076b9443SCy Schubert if( b ){ 3862076b9443SCy Schubert flags |= SQLITE_OPEN_URI; 3863076b9443SCy Schubert }else{ 3864076b9443SCy Schubert flags &= ~SQLITE_OPEN_URI; 3865076b9443SCy Schubert } 3866b622dc25SCy Schubert }else if( strcmp(zArg, "-translatefilename")==0 ){ 3867b622dc25SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &bTranslateFileName) ){ 3868b622dc25SCy Schubert return TCL_ERROR; 3869b622dc25SCy Schubert } 3870076b9443SCy Schubert }else{ 3871076b9443SCy Schubert Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0); 3872076b9443SCy Schubert return TCL_ERROR; 3873076b9443SCy Schubert } 3874076b9443SCy Schubert } 3875076b9443SCy Schubert zErrMsg = 0; 3876076b9443SCy Schubert p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); 3877076b9443SCy Schubert memset(p, 0, sizeof(*p)); 3878076b9443SCy Schubert if( zFile==0 ) zFile = ""; 3879b622dc25SCy Schubert if( bTranslateFileName ){ 3880076b9443SCy Schubert zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); 3881b622dc25SCy Schubert } 3882076b9443SCy Schubert rc = sqlite3_open_v2(zFile, &p->db, flags, zVfs); 3883b622dc25SCy Schubert if( bTranslateFileName ){ 3884076b9443SCy Schubert Tcl_DStringFree(&translatedFilename); 3885b622dc25SCy Schubert } 3886076b9443SCy Schubert if( p->db ){ 3887076b9443SCy Schubert if( SQLITE_OK!=sqlite3_errcode(p->db) ){ 3888076b9443SCy Schubert zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); 3889076b9443SCy Schubert sqlite3_close(p->db); 3890076b9443SCy Schubert p->db = 0; 3891076b9443SCy Schubert } 3892076b9443SCy Schubert }else{ 3893076b9443SCy Schubert zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc)); 3894076b9443SCy Schubert } 3895076b9443SCy Schubert if( p->db==0 ){ 3896076b9443SCy Schubert Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 3897076b9443SCy Schubert Tcl_Free((char*)p); 3898076b9443SCy Schubert sqlite3_free(zErrMsg); 3899076b9443SCy Schubert return TCL_ERROR; 3900076b9443SCy Schubert } 3901076b9443SCy Schubert p->maxStmt = NUM_PREPARED_STMTS; 3902076b9443SCy Schubert p->openFlags = flags & SQLITE_OPEN_URI; 3903076b9443SCy Schubert p->interp = interp; 3904076b9443SCy Schubert zArg = Tcl_GetStringFromObj(objv[1], 0); 3905076b9443SCy Schubert if( DbUseNre() ){ 3906076b9443SCy Schubert Tcl_NRCreateCommand(interp, zArg, DbObjCmdAdaptor, DbObjCmd, 3907076b9443SCy Schubert (char*)p, DbDeleteCmd); 3908076b9443SCy Schubert }else{ 3909076b9443SCy Schubert Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); 3910076b9443SCy Schubert } 39110197ba46SCy Schubert p->nRef = 1; 3912076b9443SCy Schubert return TCL_OK; 3913076b9443SCy Schubert } 3914076b9443SCy Schubert 3915076b9443SCy Schubert /* 3916076b9443SCy Schubert ** Provide a dummy Tcl_InitStubs if we are using this as a static 3917076b9443SCy Schubert ** library. 3918076b9443SCy Schubert */ 3919076b9443SCy Schubert #ifndef USE_TCL_STUBS 3920076b9443SCy Schubert # undef Tcl_InitStubs 3921076b9443SCy Schubert # define Tcl_InitStubs(a,b,c) TCL_VERSION 3922076b9443SCy Schubert #endif 3923076b9443SCy Schubert 3924076b9443SCy Schubert /* 3925076b9443SCy Schubert ** Make sure we have a PACKAGE_VERSION macro defined. This will be 3926076b9443SCy Schubert ** defined automatically by the TEA makefile. But other makefiles 3927076b9443SCy Schubert ** do not define it. 3928076b9443SCy Schubert */ 3929076b9443SCy Schubert #ifndef PACKAGE_VERSION 3930076b9443SCy Schubert # define PACKAGE_VERSION SQLITE_VERSION 3931076b9443SCy Schubert #endif 3932076b9443SCy Schubert 3933076b9443SCy Schubert /* 3934076b9443SCy Schubert ** Initialize this module. 3935076b9443SCy Schubert ** 3936076b9443SCy Schubert ** This Tcl module contains only a single new Tcl command named "sqlite". 3937076b9443SCy Schubert ** (Hence there is no namespace. There is no point in using a namespace 3938076b9443SCy Schubert ** if the extension only supplies one new name!) The "sqlite" command is 3939076b9443SCy Schubert ** used to open a new SQLite database. See the DbMain() routine above 3940076b9443SCy Schubert ** for additional information. 3941076b9443SCy Schubert ** 3942076b9443SCy Schubert ** The EXTERN macros are required by TCL in order to work on windows. 3943076b9443SCy Schubert */ 3944076b9443SCy Schubert EXTERN int Sqlite3_Init(Tcl_Interp *interp){ 3945076b9443SCy Schubert int rc = Tcl_InitStubs(interp, "8.4", 0) ? TCL_OK : TCL_ERROR; 3946076b9443SCy Schubert if( rc==TCL_OK ){ 3947076b9443SCy Schubert Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); 3948076b9443SCy Schubert #ifndef SQLITE_3_SUFFIX_ONLY 3949076b9443SCy Schubert /* The "sqlite" alias is undocumented. It is here only to support 3950076b9443SCy Schubert ** legacy scripts. All new scripts should use only the "sqlite3" 3951076b9443SCy Schubert ** command. */ 3952076b9443SCy Schubert Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); 3953076b9443SCy Schubert #endif 3954076b9443SCy Schubert rc = Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION); 3955076b9443SCy Schubert } 3956076b9443SCy Schubert return rc; 3957076b9443SCy Schubert } 3958076b9443SCy Schubert EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3959076b9443SCy Schubert EXTERN int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3960076b9443SCy Schubert EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3961076b9443SCy Schubert 3962076b9443SCy Schubert /* Because it accesses the file-system and uses persistent state, SQLite 3963076b9443SCy Schubert ** is not considered appropriate for safe interpreters. Hence, we cause 3964076b9443SCy Schubert ** the _SafeInit() interfaces return TCL_ERROR. 3965076b9443SCy Schubert */ 3966076b9443SCy Schubert EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_ERROR; } 3967076b9443SCy Schubert EXTERN int Sqlite3_SafeUnload(Tcl_Interp *interp, int flags){return TCL_ERROR;} 3968076b9443SCy Schubert 3969076b9443SCy Schubert 3970076b9443SCy Schubert 3971076b9443SCy Schubert #ifndef SQLITE_3_SUFFIX_ONLY 3972076b9443SCy Schubert int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3973076b9443SCy Schubert int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3974076b9443SCy Schubert int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3975076b9443SCy Schubert int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3976076b9443SCy Schubert #endif 3977076b9443SCy Schubert 3978076b9443SCy Schubert /* 3979076b9443SCy Schubert ** If the TCLSH macro is defined, add code to make a stand-alone program. 3980076b9443SCy Schubert */ 3981076b9443SCy Schubert #if defined(TCLSH) 3982076b9443SCy Schubert 3983076b9443SCy Schubert /* This is the main routine for an ordinary TCL shell. If there are 3984076b9443SCy Schubert ** are arguments, run the first argument as a script. Otherwise, 3985076b9443SCy Schubert ** read TCL commands from standard input 3986076b9443SCy Schubert */ 3987076b9443SCy Schubert static const char *tclsh_main_loop(void){ 3988076b9443SCy Schubert static const char zMainloop[] = 3989076b9443SCy Schubert "if {[llength $argv]>=1} {\n" 3990076b9443SCy Schubert "set argv0 [lindex $argv 0]\n" 3991076b9443SCy Schubert "set argv [lrange $argv 1 end]\n" 3992076b9443SCy Schubert "source $argv0\n" 3993076b9443SCy Schubert "} else {\n" 3994076b9443SCy Schubert "set line {}\n" 3995076b9443SCy Schubert "while {![eof stdin]} {\n" 3996076b9443SCy Schubert "if {$line!=\"\"} {\n" 3997076b9443SCy Schubert "puts -nonewline \"> \"\n" 3998076b9443SCy Schubert "} else {\n" 3999076b9443SCy Schubert "puts -nonewline \"% \"\n" 4000076b9443SCy Schubert "}\n" 4001076b9443SCy Schubert "flush stdout\n" 4002076b9443SCy Schubert "append line [gets stdin]\n" 4003076b9443SCy Schubert "if {[info complete $line]} {\n" 4004076b9443SCy Schubert "if {[catch {uplevel #0 $line} result]} {\n" 4005076b9443SCy Schubert "puts stderr \"Error: $result\"\n" 4006076b9443SCy Schubert "} elseif {$result!=\"\"} {\n" 4007076b9443SCy Schubert "puts $result\n" 4008076b9443SCy Schubert "}\n" 4009076b9443SCy Schubert "set line {}\n" 4010076b9443SCy Schubert "} else {\n" 4011076b9443SCy Schubert "append line \\n\n" 4012076b9443SCy Schubert "}\n" 4013076b9443SCy Schubert "}\n" 4014076b9443SCy Schubert "}\n" 4015076b9443SCy Schubert ; 4016076b9443SCy Schubert return zMainloop; 4017076b9443SCy Schubert } 4018076b9443SCy Schubert 40190197ba46SCy Schubert #ifndef TCLSH_MAIN 40200197ba46SCy Schubert # define TCLSH_MAIN main 40210197ba46SCy Schubert #endif 4022076b9443SCy Schubert int SQLITE_CDECL TCLSH_MAIN(int argc, char **argv){ 4023076b9443SCy Schubert Tcl_Interp *interp; 4024076b9443SCy Schubert int i; 4025076b9443SCy Schubert const char *zScript = 0; 4026076b9443SCy Schubert char zArgc[32]; 4027076b9443SCy Schubert #if defined(TCLSH_INIT_PROC) 4028076b9443SCy Schubert extern const char *TCLSH_INIT_PROC(Tcl_Interp*); 4029076b9443SCy Schubert #endif 4030076b9443SCy Schubert 4031076b9443SCy Schubert #if !defined(_WIN32_WCE) 4032076b9443SCy Schubert if( getenv("SQLITE_DEBUG_BREAK") ){ 4033076b9443SCy Schubert if( isatty(0) && isatty(2) ){ 4034076b9443SCy Schubert fprintf(stderr, 4035076b9443SCy Schubert "attach debugger to process %d and press any key to continue.\n", 4036076b9443SCy Schubert GETPID()); 4037076b9443SCy Schubert fgetc(stdin); 4038076b9443SCy Schubert }else{ 4039076b9443SCy Schubert #if defined(_WIN32) || defined(WIN32) 4040076b9443SCy Schubert DebugBreak(); 4041076b9443SCy Schubert #elif defined(SIGTRAP) 4042076b9443SCy Schubert raise(SIGTRAP); 4043076b9443SCy Schubert #endif 4044076b9443SCy Schubert } 4045076b9443SCy Schubert } 4046076b9443SCy Schubert #endif 4047076b9443SCy Schubert 4048076b9443SCy Schubert /* Call sqlite3_shutdown() once before doing anything else. This is to 4049076b9443SCy Schubert ** test that sqlite3_shutdown() can be safely called by a process before 4050076b9443SCy Schubert ** sqlite3_initialize() is. */ 4051076b9443SCy Schubert sqlite3_shutdown(); 4052076b9443SCy Schubert 4053076b9443SCy Schubert Tcl_FindExecutable(argv[0]); 4054076b9443SCy Schubert Tcl_SetSystemEncoding(NULL, "utf-8"); 4055076b9443SCy Schubert interp = Tcl_CreateInterp(); 4056076b9443SCy Schubert Sqlite3_Init(interp); 4057076b9443SCy Schubert 4058076b9443SCy Schubert sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-1); 4059076b9443SCy Schubert Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY); 4060076b9443SCy Schubert Tcl_SetVar(interp,"argv0",argv[0],TCL_GLOBAL_ONLY); 4061076b9443SCy Schubert Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); 4062076b9443SCy Schubert for(i=1; i<argc; i++){ 4063076b9443SCy Schubert Tcl_SetVar(interp, "argv", argv[i], 4064076b9443SCy Schubert TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); 4065076b9443SCy Schubert } 4066076b9443SCy Schubert #if defined(TCLSH_INIT_PROC) 4067076b9443SCy Schubert zScript = TCLSH_INIT_PROC(interp); 4068076b9443SCy Schubert #endif 4069076b9443SCy Schubert if( zScript==0 ){ 4070076b9443SCy Schubert zScript = tclsh_main_loop(); 4071076b9443SCy Schubert } 4072076b9443SCy Schubert if( Tcl_GlobalEval(interp, zScript)!=TCL_OK ){ 4073076b9443SCy Schubert const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); 4074076b9443SCy Schubert if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp); 4075076b9443SCy Schubert fprintf(stderr,"%s: %s\n", *argv, zInfo); 4076076b9443SCy Schubert return 1; 4077076b9443SCy Schubert } 4078076b9443SCy Schubert return 0; 4079076b9443SCy Schubert } 4080076b9443SCy Schubert #endif /* TCLSH */ 4081