1*076b9443SCy Schubert #ifdef USE_SYSTEM_SQLITE 2*076b9443SCy Schubert # include <sqlite3.h> 3*076b9443SCy Schubert #else 4*076b9443SCy Schubert #include "sqlite3.c" 5*076b9443SCy Schubert #endif 6*076b9443SCy Schubert /* 7*076b9443SCy Schubert ** 2001 September 15 8*076b9443SCy Schubert ** 9*076b9443SCy Schubert ** The author disclaims copyright to this source code. In place of 10*076b9443SCy Schubert ** a legal notice, here is a blessing: 11*076b9443SCy Schubert ** 12*076b9443SCy Schubert ** May you do good and not evil. 13*076b9443SCy Schubert ** May you find forgiveness for yourself and forgive others. 14*076b9443SCy Schubert ** May you share freely, never taking more than you give. 15*076b9443SCy Schubert ** 16*076b9443SCy Schubert ************************************************************************* 17*076b9443SCy Schubert ** A TCL Interface to SQLite. Append this file to sqlite3.c and 18*076b9443SCy Schubert ** compile the whole thing to build a TCL-enabled version of SQLite. 19*076b9443SCy Schubert ** 20*076b9443SCy Schubert ** Compile-time options: 21*076b9443SCy Schubert ** 22*076b9443SCy Schubert ** -DTCLSH Add a "main()" routine that works as a tclsh. 23*076b9443SCy Schubert ** 24*076b9443SCy Schubert ** -DTCLSH_INIT_PROC=name 25*076b9443SCy Schubert ** 26*076b9443SCy Schubert ** Invoke name(interp) to initialize the Tcl interpreter. 27*076b9443SCy Schubert ** If name(interp) returns a non-NULL string, then run 28*076b9443SCy Schubert ** that string as a Tcl script to launch the application. 29*076b9443SCy Schubert ** If name(interp) returns NULL, then run the regular 30*076b9443SCy Schubert ** tclsh-emulator code. 31*076b9443SCy Schubert */ 32*076b9443SCy Schubert #ifdef TCLSH_INIT_PROC 33*076b9443SCy Schubert # define TCLSH 1 34*076b9443SCy Schubert #endif 35*076b9443SCy Schubert 36*076b9443SCy Schubert /* 37*076b9443SCy Schubert ** If requested, include the SQLite compiler options file for MSVC. 38*076b9443SCy Schubert */ 39*076b9443SCy Schubert #if defined(INCLUDE_MSVC_H) 40*076b9443SCy Schubert # include "msvc.h" 41*076b9443SCy Schubert #endif 42*076b9443SCy Schubert 43*076b9443SCy Schubert #if defined(INCLUDE_SQLITE_TCL_H) 44*076b9443SCy Schubert # include "sqlite_tcl.h" 45*076b9443SCy Schubert #else 46*076b9443SCy Schubert # include "tcl.h" 47*076b9443SCy Schubert # ifndef SQLITE_TCLAPI 48*076b9443SCy Schubert # define SQLITE_TCLAPI 49*076b9443SCy Schubert # endif 50*076b9443SCy Schubert #endif 51*076b9443SCy Schubert #include <errno.h> 52*076b9443SCy Schubert 53*076b9443SCy Schubert /* 54*076b9443SCy Schubert ** Some additional include files are needed if this file is not 55*076b9443SCy Schubert ** appended to the amalgamation. 56*076b9443SCy Schubert */ 57*076b9443SCy Schubert #ifndef SQLITE_AMALGAMATION 58*076b9443SCy Schubert # include "sqlite3.h" 59*076b9443SCy Schubert # include <stdlib.h> 60*076b9443SCy Schubert # include <string.h> 61*076b9443SCy Schubert # include <assert.h> 62*076b9443SCy Schubert typedef unsigned char u8; 63*076b9443SCy Schubert #endif 64*076b9443SCy Schubert #include <ctype.h> 65*076b9443SCy Schubert 66*076b9443SCy Schubert /* Used to get the current process ID */ 67*076b9443SCy Schubert #if !defined(_WIN32) 68*076b9443SCy Schubert # include <signal.h> 69*076b9443SCy Schubert # include <unistd.h> 70*076b9443SCy Schubert # define GETPID getpid 71*076b9443SCy Schubert #elif !defined(_WIN32_WCE) 72*076b9443SCy Schubert # ifndef SQLITE_AMALGAMATION 73*076b9443SCy Schubert # ifndef WIN32_LEAN_AND_MEAN 74*076b9443SCy Schubert # define WIN32_LEAN_AND_MEAN 75*076b9443SCy Schubert # endif 76*076b9443SCy Schubert # include <windows.h> 77*076b9443SCy Schubert # endif 78*076b9443SCy Schubert # include <io.h> 79*076b9443SCy Schubert # define isatty(h) _isatty(h) 80*076b9443SCy Schubert # define GETPID (int)GetCurrentProcessId 81*076b9443SCy Schubert #endif 82*076b9443SCy Schubert 83*076b9443SCy Schubert /* 84*076b9443SCy Schubert * Windows needs to know which symbols to export. Unix does not. 85*076b9443SCy Schubert * BUILD_sqlite should be undefined for Unix. 86*076b9443SCy Schubert */ 87*076b9443SCy Schubert #ifdef BUILD_sqlite 88*076b9443SCy Schubert #undef TCL_STORAGE_CLASS 89*076b9443SCy Schubert #define TCL_STORAGE_CLASS DLLEXPORT 90*076b9443SCy Schubert #endif /* BUILD_sqlite */ 91*076b9443SCy Schubert 92*076b9443SCy Schubert #define NUM_PREPARED_STMTS 10 93*076b9443SCy Schubert #define MAX_PREPARED_STMTS 100 94*076b9443SCy Schubert 95*076b9443SCy Schubert /* Forward declaration */ 96*076b9443SCy Schubert typedef struct SqliteDb SqliteDb; 97*076b9443SCy Schubert 98*076b9443SCy Schubert /* 99*076b9443SCy Schubert ** New SQL functions can be created as TCL scripts. Each such function 100*076b9443SCy Schubert ** is described by an instance of the following structure. 101*076b9443SCy Schubert */ 102*076b9443SCy Schubert typedef struct SqlFunc SqlFunc; 103*076b9443SCy Schubert struct SqlFunc { 104*076b9443SCy Schubert Tcl_Interp *interp; /* The TCL interpret to execute the function */ 105*076b9443SCy Schubert Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */ 106*076b9443SCy Schubert SqliteDb *pDb; /* Database connection that owns this function */ 107*076b9443SCy Schubert int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */ 108*076b9443SCy Schubert char *zName; /* Name of this function */ 109*076b9443SCy Schubert SqlFunc *pNext; /* Next function on the list of them all */ 110*076b9443SCy Schubert }; 111*076b9443SCy Schubert 112*076b9443SCy Schubert /* 113*076b9443SCy Schubert ** New collation sequences function can be created as TCL scripts. Each such 114*076b9443SCy Schubert ** function is described by an instance of the following structure. 115*076b9443SCy Schubert */ 116*076b9443SCy Schubert typedef struct SqlCollate SqlCollate; 117*076b9443SCy Schubert struct SqlCollate { 118*076b9443SCy Schubert Tcl_Interp *interp; /* The TCL interpret to execute the function */ 119*076b9443SCy Schubert char *zScript; /* The script to be run */ 120*076b9443SCy Schubert SqlCollate *pNext; /* Next function on the list of them all */ 121*076b9443SCy Schubert }; 122*076b9443SCy Schubert 123*076b9443SCy Schubert /* 124*076b9443SCy Schubert ** Prepared statements are cached for faster execution. Each prepared 125*076b9443SCy Schubert ** statement is described by an instance of the following structure. 126*076b9443SCy Schubert */ 127*076b9443SCy Schubert typedef struct SqlPreparedStmt SqlPreparedStmt; 128*076b9443SCy Schubert struct SqlPreparedStmt { 129*076b9443SCy Schubert SqlPreparedStmt *pNext; /* Next in linked list */ 130*076b9443SCy Schubert SqlPreparedStmt *pPrev; /* Previous on the list */ 131*076b9443SCy Schubert sqlite3_stmt *pStmt; /* The prepared statement */ 132*076b9443SCy Schubert int nSql; /* chars in zSql[] */ 133*076b9443SCy Schubert const char *zSql; /* Text of the SQL statement */ 134*076b9443SCy Schubert int nParm; /* Size of apParm array */ 135*076b9443SCy Schubert Tcl_Obj **apParm; /* Array of referenced object pointers */ 136*076b9443SCy Schubert }; 137*076b9443SCy Schubert 138*076b9443SCy Schubert typedef struct IncrblobChannel IncrblobChannel; 139*076b9443SCy Schubert 140*076b9443SCy Schubert /* 141*076b9443SCy Schubert ** There is one instance of this structure for each SQLite database 142*076b9443SCy Schubert ** that has been opened by the SQLite TCL interface. 143*076b9443SCy Schubert ** 144*076b9443SCy Schubert ** If this module is built with SQLITE_TEST defined (to create the SQLite 145*076b9443SCy Schubert ** testfixture executable), then it may be configured to use either 146*076b9443SCy Schubert ** sqlite3_prepare_v2() or sqlite3_prepare() to prepare SQL statements. 147*076b9443SCy Schubert ** If SqliteDb.bLegacyPrepare is true, sqlite3_prepare() is used. 148*076b9443SCy Schubert */ 149*076b9443SCy Schubert struct SqliteDb { 150*076b9443SCy Schubert sqlite3 *db; /* The "real" database structure. MUST BE FIRST */ 151*076b9443SCy Schubert Tcl_Interp *interp; /* The interpreter used for this database */ 152*076b9443SCy Schubert char *zBusy; /* The busy callback routine */ 153*076b9443SCy Schubert char *zCommit; /* The commit hook callback routine */ 154*076b9443SCy Schubert char *zTrace; /* The trace callback routine */ 155*076b9443SCy Schubert char *zTraceV2; /* The trace_v2 callback routine */ 156*076b9443SCy Schubert char *zProfile; /* The profile callback routine */ 157*076b9443SCy Schubert char *zProgress; /* The progress callback routine */ 158*076b9443SCy Schubert char *zAuth; /* The authorization callback routine */ 159*076b9443SCy Schubert int disableAuth; /* Disable the authorizer if it exists */ 160*076b9443SCy Schubert char *zNull; /* Text to substitute for an SQL NULL value */ 161*076b9443SCy Schubert SqlFunc *pFunc; /* List of SQL functions */ 162*076b9443SCy Schubert Tcl_Obj *pUpdateHook; /* Update hook script (if any) */ 163*076b9443SCy Schubert Tcl_Obj *pPreUpdateHook; /* Pre-update hook script (if any) */ 164*076b9443SCy Schubert Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */ 165*076b9443SCy Schubert Tcl_Obj *pWalHook; /* WAL hook script (if any) */ 166*076b9443SCy Schubert Tcl_Obj *pUnlockNotify; /* Unlock notify script (if any) */ 167*076b9443SCy Schubert SqlCollate *pCollate; /* List of SQL collation functions */ 168*076b9443SCy Schubert int rc; /* Return code of most recent sqlite3_exec() */ 169*076b9443SCy Schubert Tcl_Obj *pCollateNeeded; /* Collation needed script */ 170*076b9443SCy Schubert SqlPreparedStmt *stmtList; /* List of prepared statements*/ 171*076b9443SCy Schubert SqlPreparedStmt *stmtLast; /* Last statement in the list */ 172*076b9443SCy Schubert int maxStmt; /* The next maximum number of stmtList */ 173*076b9443SCy Schubert int nStmt; /* Number of statements in stmtList */ 174*076b9443SCy Schubert IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */ 175*076b9443SCy Schubert int nStep, nSort, nIndex; /* Statistics for most recent operation */ 176*076b9443SCy Schubert int nVMStep; /* Another statistic for most recent operation */ 177*076b9443SCy Schubert int nTransaction; /* Number of nested [transaction] methods */ 178*076b9443SCy Schubert int openFlags; /* Flags used to open. (SQLITE_OPEN_URI) */ 179*076b9443SCy Schubert #ifdef SQLITE_TEST 180*076b9443SCy Schubert int bLegacyPrepare; /* True to use sqlite3_prepare() */ 181*076b9443SCy Schubert #endif 182*076b9443SCy Schubert }; 183*076b9443SCy Schubert 184*076b9443SCy Schubert struct IncrblobChannel { 185*076b9443SCy Schubert sqlite3_blob *pBlob; /* sqlite3 blob handle */ 186*076b9443SCy Schubert SqliteDb *pDb; /* Associated database connection */ 187*076b9443SCy Schubert int iSeek; /* Current seek offset */ 188*076b9443SCy Schubert Tcl_Channel channel; /* Channel identifier */ 189*076b9443SCy Schubert IncrblobChannel *pNext; /* Linked list of all open incrblob channels */ 190*076b9443SCy Schubert IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */ 191*076b9443SCy Schubert }; 192*076b9443SCy Schubert 193*076b9443SCy Schubert /* 194*076b9443SCy Schubert ** Compute a string length that is limited to what can be stored in 195*076b9443SCy Schubert ** lower 30 bits of a 32-bit signed integer. 196*076b9443SCy Schubert */ 197*076b9443SCy Schubert static int strlen30(const char *z){ 198*076b9443SCy Schubert const char *z2 = z; 199*076b9443SCy Schubert while( *z2 ){ z2++; } 200*076b9443SCy Schubert return 0x3fffffff & (int)(z2 - z); 201*076b9443SCy Schubert } 202*076b9443SCy Schubert 203*076b9443SCy Schubert 204*076b9443SCy Schubert #ifndef SQLITE_OMIT_INCRBLOB 205*076b9443SCy Schubert /* 206*076b9443SCy Schubert ** Close all incrblob channels opened using database connection pDb. 207*076b9443SCy Schubert ** This is called when shutting down the database connection. 208*076b9443SCy Schubert */ 209*076b9443SCy Schubert static void closeIncrblobChannels(SqliteDb *pDb){ 210*076b9443SCy Schubert IncrblobChannel *p; 211*076b9443SCy Schubert IncrblobChannel *pNext; 212*076b9443SCy Schubert 213*076b9443SCy Schubert for(p=pDb->pIncrblob; p; p=pNext){ 214*076b9443SCy Schubert pNext = p->pNext; 215*076b9443SCy Schubert 216*076b9443SCy Schubert /* Note: Calling unregister here call Tcl_Close on the incrblob channel, 217*076b9443SCy Schubert ** which deletes the IncrblobChannel structure at *p. So do not 218*076b9443SCy Schubert ** call Tcl_Free() here. 219*076b9443SCy Schubert */ 220*076b9443SCy Schubert Tcl_UnregisterChannel(pDb->interp, p->channel); 221*076b9443SCy Schubert } 222*076b9443SCy Schubert } 223*076b9443SCy Schubert 224*076b9443SCy Schubert /* 225*076b9443SCy Schubert ** Close an incremental blob channel. 226*076b9443SCy Schubert */ 227*076b9443SCy Schubert static int SQLITE_TCLAPI incrblobClose( 228*076b9443SCy Schubert ClientData instanceData, 229*076b9443SCy Schubert Tcl_Interp *interp 230*076b9443SCy Schubert ){ 231*076b9443SCy Schubert IncrblobChannel *p = (IncrblobChannel *)instanceData; 232*076b9443SCy Schubert int rc = sqlite3_blob_close(p->pBlob); 233*076b9443SCy Schubert sqlite3 *db = p->pDb->db; 234*076b9443SCy Schubert 235*076b9443SCy Schubert /* Remove the channel from the SqliteDb.pIncrblob list. */ 236*076b9443SCy Schubert if( p->pNext ){ 237*076b9443SCy Schubert p->pNext->pPrev = p->pPrev; 238*076b9443SCy Schubert } 239*076b9443SCy Schubert if( p->pPrev ){ 240*076b9443SCy Schubert p->pPrev->pNext = p->pNext; 241*076b9443SCy Schubert } 242*076b9443SCy Schubert if( p->pDb->pIncrblob==p ){ 243*076b9443SCy Schubert p->pDb->pIncrblob = p->pNext; 244*076b9443SCy Schubert } 245*076b9443SCy Schubert 246*076b9443SCy Schubert /* Free the IncrblobChannel structure */ 247*076b9443SCy Schubert Tcl_Free((char *)p); 248*076b9443SCy Schubert 249*076b9443SCy Schubert if( rc!=SQLITE_OK ){ 250*076b9443SCy Schubert Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); 251*076b9443SCy Schubert return TCL_ERROR; 252*076b9443SCy Schubert } 253*076b9443SCy Schubert return TCL_OK; 254*076b9443SCy Schubert } 255*076b9443SCy Schubert 256*076b9443SCy Schubert /* 257*076b9443SCy Schubert ** Read data from an incremental blob channel. 258*076b9443SCy Schubert */ 259*076b9443SCy Schubert static int SQLITE_TCLAPI incrblobInput( 260*076b9443SCy Schubert ClientData instanceData, 261*076b9443SCy Schubert char *buf, 262*076b9443SCy Schubert int bufSize, 263*076b9443SCy Schubert int *errorCodePtr 264*076b9443SCy Schubert ){ 265*076b9443SCy Schubert IncrblobChannel *p = (IncrblobChannel *)instanceData; 266*076b9443SCy Schubert int nRead = bufSize; /* Number of bytes to read */ 267*076b9443SCy Schubert int nBlob; /* Total size of the blob */ 268*076b9443SCy Schubert int rc; /* sqlite error code */ 269*076b9443SCy Schubert 270*076b9443SCy Schubert nBlob = sqlite3_blob_bytes(p->pBlob); 271*076b9443SCy Schubert if( (p->iSeek+nRead)>nBlob ){ 272*076b9443SCy Schubert nRead = nBlob-p->iSeek; 273*076b9443SCy Schubert } 274*076b9443SCy Schubert if( nRead<=0 ){ 275*076b9443SCy Schubert return 0; 276*076b9443SCy Schubert } 277*076b9443SCy Schubert 278*076b9443SCy Schubert rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek); 279*076b9443SCy Schubert if( rc!=SQLITE_OK ){ 280*076b9443SCy Schubert *errorCodePtr = rc; 281*076b9443SCy Schubert return -1; 282*076b9443SCy Schubert } 283*076b9443SCy Schubert 284*076b9443SCy Schubert p->iSeek += nRead; 285*076b9443SCy Schubert return nRead; 286*076b9443SCy Schubert } 287*076b9443SCy Schubert 288*076b9443SCy Schubert /* 289*076b9443SCy Schubert ** Write data to an incremental blob channel. 290*076b9443SCy Schubert */ 291*076b9443SCy Schubert static int SQLITE_TCLAPI incrblobOutput( 292*076b9443SCy Schubert ClientData instanceData, 293*076b9443SCy Schubert CONST char *buf, 294*076b9443SCy Schubert int toWrite, 295*076b9443SCy Schubert int *errorCodePtr 296*076b9443SCy Schubert ){ 297*076b9443SCy Schubert IncrblobChannel *p = (IncrblobChannel *)instanceData; 298*076b9443SCy Schubert int nWrite = toWrite; /* Number of bytes to write */ 299*076b9443SCy Schubert int nBlob; /* Total size of the blob */ 300*076b9443SCy Schubert int rc; /* sqlite error code */ 301*076b9443SCy Schubert 302*076b9443SCy Schubert nBlob = sqlite3_blob_bytes(p->pBlob); 303*076b9443SCy Schubert if( (p->iSeek+nWrite)>nBlob ){ 304*076b9443SCy Schubert *errorCodePtr = EINVAL; 305*076b9443SCy Schubert return -1; 306*076b9443SCy Schubert } 307*076b9443SCy Schubert if( nWrite<=0 ){ 308*076b9443SCy Schubert return 0; 309*076b9443SCy Schubert } 310*076b9443SCy Schubert 311*076b9443SCy Schubert rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek); 312*076b9443SCy Schubert if( rc!=SQLITE_OK ){ 313*076b9443SCy Schubert *errorCodePtr = EIO; 314*076b9443SCy Schubert return -1; 315*076b9443SCy Schubert } 316*076b9443SCy Schubert 317*076b9443SCy Schubert p->iSeek += nWrite; 318*076b9443SCy Schubert return nWrite; 319*076b9443SCy Schubert } 320*076b9443SCy Schubert 321*076b9443SCy Schubert /* 322*076b9443SCy Schubert ** Seek an incremental blob channel. 323*076b9443SCy Schubert */ 324*076b9443SCy Schubert static int SQLITE_TCLAPI incrblobSeek( 325*076b9443SCy Schubert ClientData instanceData, 326*076b9443SCy Schubert long offset, 327*076b9443SCy Schubert int seekMode, 328*076b9443SCy Schubert int *errorCodePtr 329*076b9443SCy Schubert ){ 330*076b9443SCy Schubert IncrblobChannel *p = (IncrblobChannel *)instanceData; 331*076b9443SCy Schubert 332*076b9443SCy Schubert switch( seekMode ){ 333*076b9443SCy Schubert case SEEK_SET: 334*076b9443SCy Schubert p->iSeek = offset; 335*076b9443SCy Schubert break; 336*076b9443SCy Schubert case SEEK_CUR: 337*076b9443SCy Schubert p->iSeek += offset; 338*076b9443SCy Schubert break; 339*076b9443SCy Schubert case SEEK_END: 340*076b9443SCy Schubert p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset; 341*076b9443SCy Schubert break; 342*076b9443SCy Schubert 343*076b9443SCy Schubert default: assert(!"Bad seekMode"); 344*076b9443SCy Schubert } 345*076b9443SCy Schubert 346*076b9443SCy Schubert return p->iSeek; 347*076b9443SCy Schubert } 348*076b9443SCy Schubert 349*076b9443SCy Schubert 350*076b9443SCy Schubert static void SQLITE_TCLAPI incrblobWatch( 351*076b9443SCy Schubert ClientData instanceData, 352*076b9443SCy Schubert int mode 353*076b9443SCy Schubert ){ 354*076b9443SCy Schubert /* NO-OP */ 355*076b9443SCy Schubert } 356*076b9443SCy Schubert static int SQLITE_TCLAPI incrblobHandle( 357*076b9443SCy Schubert ClientData instanceData, 358*076b9443SCy Schubert int dir, 359*076b9443SCy Schubert ClientData *hPtr 360*076b9443SCy Schubert ){ 361*076b9443SCy Schubert return TCL_ERROR; 362*076b9443SCy Schubert } 363*076b9443SCy Schubert 364*076b9443SCy Schubert static Tcl_ChannelType IncrblobChannelType = { 365*076b9443SCy Schubert "incrblob", /* typeName */ 366*076b9443SCy Schubert TCL_CHANNEL_VERSION_2, /* version */ 367*076b9443SCy Schubert incrblobClose, /* closeProc */ 368*076b9443SCy Schubert incrblobInput, /* inputProc */ 369*076b9443SCy Schubert incrblobOutput, /* outputProc */ 370*076b9443SCy Schubert incrblobSeek, /* seekProc */ 371*076b9443SCy Schubert 0, /* setOptionProc */ 372*076b9443SCy Schubert 0, /* getOptionProc */ 373*076b9443SCy Schubert incrblobWatch, /* watchProc (this is a no-op) */ 374*076b9443SCy Schubert incrblobHandle, /* getHandleProc (always returns error) */ 375*076b9443SCy Schubert 0, /* close2Proc */ 376*076b9443SCy Schubert 0, /* blockModeProc */ 377*076b9443SCy Schubert 0, /* flushProc */ 378*076b9443SCy Schubert 0, /* handlerProc */ 379*076b9443SCy Schubert 0, /* wideSeekProc */ 380*076b9443SCy Schubert }; 381*076b9443SCy Schubert 382*076b9443SCy Schubert /* 383*076b9443SCy Schubert ** Create a new incrblob channel. 384*076b9443SCy Schubert */ 385*076b9443SCy Schubert static int createIncrblobChannel( 386*076b9443SCy Schubert Tcl_Interp *interp, 387*076b9443SCy Schubert SqliteDb *pDb, 388*076b9443SCy Schubert const char *zDb, 389*076b9443SCy Schubert const char *zTable, 390*076b9443SCy Schubert const char *zColumn, 391*076b9443SCy Schubert sqlite_int64 iRow, 392*076b9443SCy Schubert int isReadonly 393*076b9443SCy Schubert ){ 394*076b9443SCy Schubert IncrblobChannel *p; 395*076b9443SCy Schubert sqlite3 *db = pDb->db; 396*076b9443SCy Schubert sqlite3_blob *pBlob; 397*076b9443SCy Schubert int rc; 398*076b9443SCy Schubert int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE); 399*076b9443SCy Schubert 400*076b9443SCy Schubert /* This variable is used to name the channels: "incrblob_[incr count]" */ 401*076b9443SCy Schubert static int count = 0; 402*076b9443SCy Schubert char zChannel[64]; 403*076b9443SCy Schubert 404*076b9443SCy Schubert rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob); 405*076b9443SCy Schubert if( rc!=SQLITE_OK ){ 406*076b9443SCy Schubert Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 407*076b9443SCy Schubert return TCL_ERROR; 408*076b9443SCy Schubert } 409*076b9443SCy Schubert 410*076b9443SCy Schubert p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel)); 411*076b9443SCy Schubert p->iSeek = 0; 412*076b9443SCy Schubert p->pBlob = pBlob; 413*076b9443SCy Schubert 414*076b9443SCy Schubert sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count); 415*076b9443SCy Schubert p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags); 416*076b9443SCy Schubert Tcl_RegisterChannel(interp, p->channel); 417*076b9443SCy Schubert 418*076b9443SCy Schubert /* Link the new channel into the SqliteDb.pIncrblob list. */ 419*076b9443SCy Schubert p->pNext = pDb->pIncrblob; 420*076b9443SCy Schubert p->pPrev = 0; 421*076b9443SCy Schubert if( p->pNext ){ 422*076b9443SCy Schubert p->pNext->pPrev = p; 423*076b9443SCy Schubert } 424*076b9443SCy Schubert pDb->pIncrblob = p; 425*076b9443SCy Schubert p->pDb = pDb; 426*076b9443SCy Schubert 427*076b9443SCy Schubert Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE); 428*076b9443SCy Schubert return TCL_OK; 429*076b9443SCy Schubert } 430*076b9443SCy Schubert #else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */ 431*076b9443SCy Schubert #define closeIncrblobChannels(pDb) 432*076b9443SCy Schubert #endif 433*076b9443SCy Schubert 434*076b9443SCy Schubert /* 435*076b9443SCy Schubert ** Look at the script prefix in pCmd. We will be executing this script 436*076b9443SCy Schubert ** after first appending one or more arguments. This routine analyzes 437*076b9443SCy Schubert ** the script to see if it is safe to use Tcl_EvalObjv() on the script 438*076b9443SCy Schubert ** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much 439*076b9443SCy Schubert ** faster. 440*076b9443SCy Schubert ** 441*076b9443SCy Schubert ** Scripts that are safe to use with Tcl_EvalObjv() consists of a 442*076b9443SCy Schubert ** command name followed by zero or more arguments with no [...] or $ 443*076b9443SCy Schubert ** or {...} or ; to be seen anywhere. Most callback scripts consist 444*076b9443SCy Schubert ** of just a single procedure name and they meet this requirement. 445*076b9443SCy Schubert */ 446*076b9443SCy Schubert static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){ 447*076b9443SCy Schubert /* We could try to do something with Tcl_Parse(). But we will instead 448*076b9443SCy Schubert ** just do a search for forbidden characters. If any of the forbidden 449*076b9443SCy Schubert ** characters appear in pCmd, we will report the string as unsafe. 450*076b9443SCy Schubert */ 451*076b9443SCy Schubert const char *z; 452*076b9443SCy Schubert int n; 453*076b9443SCy Schubert z = Tcl_GetStringFromObj(pCmd, &n); 454*076b9443SCy Schubert while( n-- > 0 ){ 455*076b9443SCy Schubert int c = *(z++); 456*076b9443SCy Schubert if( c=='$' || c=='[' || c==';' ) return 0; 457*076b9443SCy Schubert } 458*076b9443SCy Schubert return 1; 459*076b9443SCy Schubert } 460*076b9443SCy Schubert 461*076b9443SCy Schubert /* 462*076b9443SCy Schubert ** Find an SqlFunc structure with the given name. Or create a new 463*076b9443SCy Schubert ** one if an existing one cannot be found. Return a pointer to the 464*076b9443SCy Schubert ** structure. 465*076b9443SCy Schubert */ 466*076b9443SCy Schubert static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){ 467*076b9443SCy Schubert SqlFunc *p, *pNew; 468*076b9443SCy Schubert int nName = strlen30(zName); 469*076b9443SCy Schubert pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + nName + 1 ); 470*076b9443SCy Schubert pNew->zName = (char*)&pNew[1]; 471*076b9443SCy Schubert memcpy(pNew->zName, zName, nName+1); 472*076b9443SCy Schubert for(p=pDb->pFunc; p; p=p->pNext){ 473*076b9443SCy Schubert if( sqlite3_stricmp(p->zName, pNew->zName)==0 ){ 474*076b9443SCy Schubert Tcl_Free((char*)pNew); 475*076b9443SCy Schubert return p; 476*076b9443SCy Schubert } 477*076b9443SCy Schubert } 478*076b9443SCy Schubert pNew->interp = pDb->interp; 479*076b9443SCy Schubert pNew->pDb = pDb; 480*076b9443SCy Schubert pNew->pScript = 0; 481*076b9443SCy Schubert pNew->pNext = pDb->pFunc; 482*076b9443SCy Schubert pDb->pFunc = pNew; 483*076b9443SCy Schubert return pNew; 484*076b9443SCy Schubert } 485*076b9443SCy Schubert 486*076b9443SCy Schubert /* 487*076b9443SCy Schubert ** Free a single SqlPreparedStmt object. 488*076b9443SCy Schubert */ 489*076b9443SCy Schubert static void dbFreeStmt(SqlPreparedStmt *pStmt){ 490*076b9443SCy Schubert #ifdef SQLITE_TEST 491*076b9443SCy Schubert if( sqlite3_sql(pStmt->pStmt)==0 ){ 492*076b9443SCy Schubert Tcl_Free((char *)pStmt->zSql); 493*076b9443SCy Schubert } 494*076b9443SCy Schubert #endif 495*076b9443SCy Schubert sqlite3_finalize(pStmt->pStmt); 496*076b9443SCy Schubert Tcl_Free((char *)pStmt); 497*076b9443SCy Schubert } 498*076b9443SCy Schubert 499*076b9443SCy Schubert /* 500*076b9443SCy Schubert ** Finalize and free a list of prepared statements 501*076b9443SCy Schubert */ 502*076b9443SCy Schubert static void flushStmtCache(SqliteDb *pDb){ 503*076b9443SCy Schubert SqlPreparedStmt *pPreStmt; 504*076b9443SCy Schubert SqlPreparedStmt *pNext; 505*076b9443SCy Schubert 506*076b9443SCy Schubert for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pNext){ 507*076b9443SCy Schubert pNext = pPreStmt->pNext; 508*076b9443SCy Schubert dbFreeStmt(pPreStmt); 509*076b9443SCy Schubert } 510*076b9443SCy Schubert pDb->nStmt = 0; 511*076b9443SCy Schubert pDb->stmtLast = 0; 512*076b9443SCy Schubert pDb->stmtList = 0; 513*076b9443SCy Schubert } 514*076b9443SCy Schubert 515*076b9443SCy Schubert /* 516*076b9443SCy Schubert ** TCL calls this procedure when an sqlite3 database command is 517*076b9443SCy Schubert ** deleted. 518*076b9443SCy Schubert */ 519*076b9443SCy Schubert static void SQLITE_TCLAPI DbDeleteCmd(void *db){ 520*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)db; 521*076b9443SCy Schubert flushStmtCache(pDb); 522*076b9443SCy Schubert closeIncrblobChannels(pDb); 523*076b9443SCy Schubert sqlite3_close(pDb->db); 524*076b9443SCy Schubert while( pDb->pFunc ){ 525*076b9443SCy Schubert SqlFunc *pFunc = pDb->pFunc; 526*076b9443SCy Schubert pDb->pFunc = pFunc->pNext; 527*076b9443SCy Schubert assert( pFunc->pDb==pDb ); 528*076b9443SCy Schubert Tcl_DecrRefCount(pFunc->pScript); 529*076b9443SCy Schubert Tcl_Free((char*)pFunc); 530*076b9443SCy Schubert } 531*076b9443SCy Schubert while( pDb->pCollate ){ 532*076b9443SCy Schubert SqlCollate *pCollate = pDb->pCollate; 533*076b9443SCy Schubert pDb->pCollate = pCollate->pNext; 534*076b9443SCy Schubert Tcl_Free((char*)pCollate); 535*076b9443SCy Schubert } 536*076b9443SCy Schubert if( pDb->zBusy ){ 537*076b9443SCy Schubert Tcl_Free(pDb->zBusy); 538*076b9443SCy Schubert } 539*076b9443SCy Schubert if( pDb->zTrace ){ 540*076b9443SCy Schubert Tcl_Free(pDb->zTrace); 541*076b9443SCy Schubert } 542*076b9443SCy Schubert if( pDb->zTraceV2 ){ 543*076b9443SCy Schubert Tcl_Free(pDb->zTraceV2); 544*076b9443SCy Schubert } 545*076b9443SCy Schubert if( pDb->zProfile ){ 546*076b9443SCy Schubert Tcl_Free(pDb->zProfile); 547*076b9443SCy Schubert } 548*076b9443SCy Schubert if( pDb->zAuth ){ 549*076b9443SCy Schubert Tcl_Free(pDb->zAuth); 550*076b9443SCy Schubert } 551*076b9443SCy Schubert if( pDb->zNull ){ 552*076b9443SCy Schubert Tcl_Free(pDb->zNull); 553*076b9443SCy Schubert } 554*076b9443SCy Schubert if( pDb->pUpdateHook ){ 555*076b9443SCy Schubert Tcl_DecrRefCount(pDb->pUpdateHook); 556*076b9443SCy Schubert } 557*076b9443SCy Schubert if( pDb->pPreUpdateHook ){ 558*076b9443SCy Schubert Tcl_DecrRefCount(pDb->pPreUpdateHook); 559*076b9443SCy Schubert } 560*076b9443SCy Schubert if( pDb->pRollbackHook ){ 561*076b9443SCy Schubert Tcl_DecrRefCount(pDb->pRollbackHook); 562*076b9443SCy Schubert } 563*076b9443SCy Schubert if( pDb->pWalHook ){ 564*076b9443SCy Schubert Tcl_DecrRefCount(pDb->pWalHook); 565*076b9443SCy Schubert } 566*076b9443SCy Schubert if( pDb->pCollateNeeded ){ 567*076b9443SCy Schubert Tcl_DecrRefCount(pDb->pCollateNeeded); 568*076b9443SCy Schubert } 569*076b9443SCy Schubert Tcl_Free((char*)pDb); 570*076b9443SCy Schubert } 571*076b9443SCy Schubert 572*076b9443SCy Schubert /* 573*076b9443SCy Schubert ** This routine is called when a database file is locked while trying 574*076b9443SCy Schubert ** to execute SQL. 575*076b9443SCy Schubert */ 576*076b9443SCy Schubert static int DbBusyHandler(void *cd, int nTries){ 577*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 578*076b9443SCy Schubert int rc; 579*076b9443SCy Schubert char zVal[30]; 580*076b9443SCy Schubert 581*076b9443SCy Schubert sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries); 582*076b9443SCy Schubert rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0); 583*076b9443SCy Schubert if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 584*076b9443SCy Schubert return 0; 585*076b9443SCy Schubert } 586*076b9443SCy Schubert return 1; 587*076b9443SCy Schubert } 588*076b9443SCy Schubert 589*076b9443SCy Schubert #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 590*076b9443SCy Schubert /* 591*076b9443SCy Schubert ** This routine is invoked as the 'progress callback' for the database. 592*076b9443SCy Schubert */ 593*076b9443SCy Schubert static int DbProgressHandler(void *cd){ 594*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 595*076b9443SCy Schubert int rc; 596*076b9443SCy Schubert 597*076b9443SCy Schubert assert( pDb->zProgress ); 598*076b9443SCy Schubert rc = Tcl_Eval(pDb->interp, pDb->zProgress); 599*076b9443SCy Schubert if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 600*076b9443SCy Schubert return 1; 601*076b9443SCy Schubert } 602*076b9443SCy Schubert return 0; 603*076b9443SCy Schubert } 604*076b9443SCy Schubert #endif 605*076b9443SCy Schubert 606*076b9443SCy Schubert #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ 607*076b9443SCy Schubert !defined(SQLITE_OMIT_DEPRECATED) 608*076b9443SCy Schubert /* 609*076b9443SCy Schubert ** This routine is called by the SQLite trace handler whenever a new 610*076b9443SCy Schubert ** block of SQL is executed. The TCL script in pDb->zTrace is executed. 611*076b9443SCy Schubert */ 612*076b9443SCy Schubert static void DbTraceHandler(void *cd, const char *zSql){ 613*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 614*076b9443SCy Schubert Tcl_DString str; 615*076b9443SCy Schubert 616*076b9443SCy Schubert Tcl_DStringInit(&str); 617*076b9443SCy Schubert Tcl_DStringAppend(&str, pDb->zTrace, -1); 618*076b9443SCy Schubert Tcl_DStringAppendElement(&str, zSql); 619*076b9443SCy Schubert Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 620*076b9443SCy Schubert Tcl_DStringFree(&str); 621*076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 622*076b9443SCy Schubert } 623*076b9443SCy Schubert #endif 624*076b9443SCy Schubert 625*076b9443SCy Schubert #ifndef SQLITE_OMIT_TRACE 626*076b9443SCy Schubert /* 627*076b9443SCy Schubert ** This routine is called by the SQLite trace_v2 handler whenever a new 628*076b9443SCy Schubert ** supported event is generated. Unsupported event types are ignored. 629*076b9443SCy Schubert ** The TCL script in pDb->zTraceV2 is executed, with the arguments for 630*076b9443SCy Schubert ** the event appended to it (as list elements). 631*076b9443SCy Schubert */ 632*076b9443SCy Schubert static int DbTraceV2Handler( 633*076b9443SCy Schubert unsigned type, /* One of the SQLITE_TRACE_* event types. */ 634*076b9443SCy Schubert void *cd, /* The original context data pointer. */ 635*076b9443SCy Schubert void *pd, /* Primary event data, depends on event type. */ 636*076b9443SCy Schubert void *xd /* Extra event data, depends on event type. */ 637*076b9443SCy Schubert ){ 638*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 639*076b9443SCy Schubert Tcl_Obj *pCmd; 640*076b9443SCy Schubert 641*076b9443SCy Schubert switch( type ){ 642*076b9443SCy Schubert case SQLITE_TRACE_STMT: { 643*076b9443SCy Schubert sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; 644*076b9443SCy Schubert char *zSql = (char *)xd; 645*076b9443SCy Schubert 646*076b9443SCy Schubert pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 647*076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 648*076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 649*076b9443SCy Schubert Tcl_NewWideIntObj((Tcl_WideInt)pStmt)); 650*076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 651*076b9443SCy Schubert Tcl_NewStringObj(zSql, -1)); 652*076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 653*076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 654*076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 655*076b9443SCy Schubert break; 656*076b9443SCy Schubert } 657*076b9443SCy Schubert case SQLITE_TRACE_PROFILE: { 658*076b9443SCy Schubert sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; 659*076b9443SCy Schubert sqlite3_int64 ns = *(sqlite3_int64*)xd; 660*076b9443SCy Schubert 661*076b9443SCy Schubert pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 662*076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 663*076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 664*076b9443SCy Schubert Tcl_NewWideIntObj((Tcl_WideInt)pStmt)); 665*076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 666*076b9443SCy Schubert Tcl_NewWideIntObj((Tcl_WideInt)ns)); 667*076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 668*076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 669*076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 670*076b9443SCy Schubert break; 671*076b9443SCy Schubert } 672*076b9443SCy Schubert case SQLITE_TRACE_ROW: { 673*076b9443SCy Schubert sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; 674*076b9443SCy Schubert 675*076b9443SCy Schubert pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 676*076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 677*076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 678*076b9443SCy Schubert Tcl_NewWideIntObj((Tcl_WideInt)pStmt)); 679*076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 680*076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 681*076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 682*076b9443SCy Schubert break; 683*076b9443SCy Schubert } 684*076b9443SCy Schubert case SQLITE_TRACE_CLOSE: { 685*076b9443SCy Schubert sqlite3 *db = (sqlite3 *)pd; 686*076b9443SCy Schubert 687*076b9443SCy Schubert pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 688*076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 689*076b9443SCy Schubert Tcl_ListObjAppendElement(pDb->interp, pCmd, 690*076b9443SCy Schubert Tcl_NewWideIntObj((Tcl_WideInt)db)); 691*076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 692*076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 693*076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 694*076b9443SCy Schubert break; 695*076b9443SCy Schubert } 696*076b9443SCy Schubert } 697*076b9443SCy Schubert return SQLITE_OK; 698*076b9443SCy Schubert } 699*076b9443SCy Schubert #endif 700*076b9443SCy Schubert 701*076b9443SCy Schubert #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ 702*076b9443SCy Schubert !defined(SQLITE_OMIT_DEPRECATED) 703*076b9443SCy Schubert /* 704*076b9443SCy Schubert ** This routine is called by the SQLite profile handler after a statement 705*076b9443SCy Schubert ** SQL has executed. The TCL script in pDb->zProfile is evaluated. 706*076b9443SCy Schubert */ 707*076b9443SCy Schubert static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){ 708*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 709*076b9443SCy Schubert Tcl_DString str; 710*076b9443SCy Schubert char zTm[100]; 711*076b9443SCy Schubert 712*076b9443SCy Schubert sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm); 713*076b9443SCy Schubert Tcl_DStringInit(&str); 714*076b9443SCy Schubert Tcl_DStringAppend(&str, pDb->zProfile, -1); 715*076b9443SCy Schubert Tcl_DStringAppendElement(&str, zSql); 716*076b9443SCy Schubert Tcl_DStringAppendElement(&str, zTm); 717*076b9443SCy Schubert Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 718*076b9443SCy Schubert Tcl_DStringFree(&str); 719*076b9443SCy Schubert Tcl_ResetResult(pDb->interp); 720*076b9443SCy Schubert } 721*076b9443SCy Schubert #endif 722*076b9443SCy Schubert 723*076b9443SCy Schubert /* 724*076b9443SCy Schubert ** This routine is called when a transaction is committed. The 725*076b9443SCy Schubert ** TCL script in pDb->zCommit is executed. If it returns non-zero or 726*076b9443SCy Schubert ** if it throws an exception, the transaction is rolled back instead 727*076b9443SCy Schubert ** of being committed. 728*076b9443SCy Schubert */ 729*076b9443SCy Schubert static int DbCommitHandler(void *cd){ 730*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 731*076b9443SCy Schubert int rc; 732*076b9443SCy Schubert 733*076b9443SCy Schubert rc = Tcl_Eval(pDb->interp, pDb->zCommit); 734*076b9443SCy Schubert if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 735*076b9443SCy Schubert return 1; 736*076b9443SCy Schubert } 737*076b9443SCy Schubert return 0; 738*076b9443SCy Schubert } 739*076b9443SCy Schubert 740*076b9443SCy Schubert static void DbRollbackHandler(void *clientData){ 741*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)clientData; 742*076b9443SCy Schubert assert(pDb->pRollbackHook); 743*076b9443SCy Schubert if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){ 744*076b9443SCy Schubert Tcl_BackgroundError(pDb->interp); 745*076b9443SCy Schubert } 746*076b9443SCy Schubert } 747*076b9443SCy Schubert 748*076b9443SCy Schubert /* 749*076b9443SCy Schubert ** This procedure handles wal_hook callbacks. 750*076b9443SCy Schubert */ 751*076b9443SCy Schubert static int DbWalHandler( 752*076b9443SCy Schubert void *clientData, 753*076b9443SCy Schubert sqlite3 *db, 754*076b9443SCy Schubert const char *zDb, 755*076b9443SCy Schubert int nEntry 756*076b9443SCy Schubert ){ 757*076b9443SCy Schubert int ret = SQLITE_OK; 758*076b9443SCy Schubert Tcl_Obj *p; 759*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)clientData; 760*076b9443SCy Schubert Tcl_Interp *interp = pDb->interp; 761*076b9443SCy Schubert assert(pDb->pWalHook); 762*076b9443SCy Schubert 763*076b9443SCy Schubert assert( db==pDb->db ); 764*076b9443SCy Schubert p = Tcl_DuplicateObj(pDb->pWalHook); 765*076b9443SCy Schubert Tcl_IncrRefCount(p); 766*076b9443SCy Schubert Tcl_ListObjAppendElement(interp, p, Tcl_NewStringObj(zDb, -1)); 767*076b9443SCy Schubert Tcl_ListObjAppendElement(interp, p, Tcl_NewIntObj(nEntry)); 768*076b9443SCy Schubert if( TCL_OK!=Tcl_EvalObjEx(interp, p, 0) 769*076b9443SCy Schubert || TCL_OK!=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &ret) 770*076b9443SCy Schubert ){ 771*076b9443SCy Schubert Tcl_BackgroundError(interp); 772*076b9443SCy Schubert } 773*076b9443SCy Schubert Tcl_DecrRefCount(p); 774*076b9443SCy Schubert 775*076b9443SCy Schubert return ret; 776*076b9443SCy Schubert } 777*076b9443SCy Schubert 778*076b9443SCy Schubert #if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_UNLOCK_NOTIFY) 779*076b9443SCy Schubert static void setTestUnlockNotifyVars(Tcl_Interp *interp, int iArg, int nArg){ 780*076b9443SCy Schubert char zBuf[64]; 781*076b9443SCy Schubert sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", iArg); 782*076b9443SCy Schubert Tcl_SetVar(interp, "sqlite_unlock_notify_arg", zBuf, TCL_GLOBAL_ONLY); 783*076b9443SCy Schubert sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nArg); 784*076b9443SCy Schubert Tcl_SetVar(interp, "sqlite_unlock_notify_argcount", zBuf, TCL_GLOBAL_ONLY); 785*076b9443SCy Schubert } 786*076b9443SCy Schubert #else 787*076b9443SCy Schubert # define setTestUnlockNotifyVars(x,y,z) 788*076b9443SCy Schubert #endif 789*076b9443SCy Schubert 790*076b9443SCy Schubert #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 791*076b9443SCy Schubert static void DbUnlockNotify(void **apArg, int nArg){ 792*076b9443SCy Schubert int i; 793*076b9443SCy Schubert for(i=0; i<nArg; i++){ 794*076b9443SCy Schubert const int flags = (TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); 795*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb *)apArg[i]; 796*076b9443SCy Schubert setTestUnlockNotifyVars(pDb->interp, i, nArg); 797*076b9443SCy Schubert assert( pDb->pUnlockNotify); 798*076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pDb->pUnlockNotify, flags); 799*076b9443SCy Schubert Tcl_DecrRefCount(pDb->pUnlockNotify); 800*076b9443SCy Schubert pDb->pUnlockNotify = 0; 801*076b9443SCy Schubert } 802*076b9443SCy Schubert } 803*076b9443SCy Schubert #endif 804*076b9443SCy Schubert 805*076b9443SCy Schubert #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 806*076b9443SCy Schubert /* 807*076b9443SCy Schubert ** Pre-update hook callback. 808*076b9443SCy Schubert */ 809*076b9443SCy Schubert static void DbPreUpdateHandler( 810*076b9443SCy Schubert void *p, 811*076b9443SCy Schubert sqlite3 *db, 812*076b9443SCy Schubert int op, 813*076b9443SCy Schubert const char *zDb, 814*076b9443SCy Schubert const char *zTbl, 815*076b9443SCy Schubert sqlite_int64 iKey1, 816*076b9443SCy Schubert sqlite_int64 iKey2 817*076b9443SCy Schubert ){ 818*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb *)p; 819*076b9443SCy Schubert Tcl_Obj *pCmd; 820*076b9443SCy Schubert static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"}; 821*076b9443SCy Schubert 822*076b9443SCy Schubert assert( (SQLITE_DELETE-1)/9 == 0 ); 823*076b9443SCy Schubert assert( (SQLITE_INSERT-1)/9 == 1 ); 824*076b9443SCy Schubert assert( (SQLITE_UPDATE-1)/9 == 2 ); 825*076b9443SCy Schubert assert( pDb->pPreUpdateHook ); 826*076b9443SCy Schubert assert( db==pDb->db ); 827*076b9443SCy Schubert assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); 828*076b9443SCy Schubert 829*076b9443SCy Schubert pCmd = Tcl_DuplicateObj(pDb->pPreUpdateHook); 830*076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 831*076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1)); 832*076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); 833*076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); 834*076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey1)); 835*076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey2)); 836*076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 837*076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 838*076b9443SCy Schubert } 839*076b9443SCy Schubert #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 840*076b9443SCy Schubert 841*076b9443SCy Schubert static void DbUpdateHandler( 842*076b9443SCy Schubert void *p, 843*076b9443SCy Schubert int op, 844*076b9443SCy Schubert const char *zDb, 845*076b9443SCy Schubert const char *zTbl, 846*076b9443SCy Schubert sqlite_int64 rowid 847*076b9443SCy Schubert ){ 848*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb *)p; 849*076b9443SCy Schubert Tcl_Obj *pCmd; 850*076b9443SCy Schubert static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"}; 851*076b9443SCy Schubert 852*076b9443SCy Schubert assert( (SQLITE_DELETE-1)/9 == 0 ); 853*076b9443SCy Schubert assert( (SQLITE_INSERT-1)/9 == 1 ); 854*076b9443SCy Schubert assert( (SQLITE_UPDATE-1)/9 == 2 ); 855*076b9443SCy Schubert 856*076b9443SCy Schubert assert( pDb->pUpdateHook ); 857*076b9443SCy Schubert assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); 858*076b9443SCy Schubert 859*076b9443SCy Schubert pCmd = Tcl_DuplicateObj(pDb->pUpdateHook); 860*076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 861*076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1)); 862*076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); 863*076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); 864*076b9443SCy Schubert Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid)); 865*076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 866*076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 867*076b9443SCy Schubert } 868*076b9443SCy Schubert 869*076b9443SCy Schubert static void tclCollateNeeded( 870*076b9443SCy Schubert void *pCtx, 871*076b9443SCy Schubert sqlite3 *db, 872*076b9443SCy Schubert int enc, 873*076b9443SCy Schubert const char *zName 874*076b9443SCy Schubert ){ 875*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb *)pCtx; 876*076b9443SCy Schubert Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded); 877*076b9443SCy Schubert Tcl_IncrRefCount(pScript); 878*076b9443SCy Schubert Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1)); 879*076b9443SCy Schubert Tcl_EvalObjEx(pDb->interp, pScript, 0); 880*076b9443SCy Schubert Tcl_DecrRefCount(pScript); 881*076b9443SCy Schubert } 882*076b9443SCy Schubert 883*076b9443SCy Schubert /* 884*076b9443SCy Schubert ** This routine is called to evaluate an SQL collation function implemented 885*076b9443SCy Schubert ** using TCL script. 886*076b9443SCy Schubert */ 887*076b9443SCy Schubert static int tclSqlCollate( 888*076b9443SCy Schubert void *pCtx, 889*076b9443SCy Schubert int nA, 890*076b9443SCy Schubert const void *zA, 891*076b9443SCy Schubert int nB, 892*076b9443SCy Schubert const void *zB 893*076b9443SCy Schubert ){ 894*076b9443SCy Schubert SqlCollate *p = (SqlCollate *)pCtx; 895*076b9443SCy Schubert Tcl_Obj *pCmd; 896*076b9443SCy Schubert 897*076b9443SCy Schubert pCmd = Tcl_NewStringObj(p->zScript, -1); 898*076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 899*076b9443SCy Schubert Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA)); 900*076b9443SCy Schubert Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB)); 901*076b9443SCy Schubert Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 902*076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 903*076b9443SCy Schubert return (atoi(Tcl_GetStringResult(p->interp))); 904*076b9443SCy Schubert } 905*076b9443SCy Schubert 906*076b9443SCy Schubert /* 907*076b9443SCy Schubert ** This routine is called to evaluate an SQL function implemented 908*076b9443SCy Schubert ** using TCL script. 909*076b9443SCy Schubert */ 910*076b9443SCy Schubert static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ 911*076b9443SCy Schubert SqlFunc *p = sqlite3_user_data(context); 912*076b9443SCy Schubert Tcl_Obj *pCmd; 913*076b9443SCy Schubert int i; 914*076b9443SCy Schubert int rc; 915*076b9443SCy Schubert 916*076b9443SCy Schubert if( argc==0 ){ 917*076b9443SCy Schubert /* If there are no arguments to the function, call Tcl_EvalObjEx on the 918*076b9443SCy Schubert ** script object directly. This allows the TCL compiler to generate 919*076b9443SCy Schubert ** bytecode for the command on the first invocation and thus make 920*076b9443SCy Schubert ** subsequent invocations much faster. */ 921*076b9443SCy Schubert pCmd = p->pScript; 922*076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 923*076b9443SCy Schubert rc = Tcl_EvalObjEx(p->interp, pCmd, 0); 924*076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 925*076b9443SCy Schubert }else{ 926*076b9443SCy Schubert /* If there are arguments to the function, make a shallow copy of the 927*076b9443SCy Schubert ** script object, lappend the arguments, then evaluate the copy. 928*076b9443SCy Schubert ** 929*076b9443SCy Schubert ** By "shallow" copy, we mean only the outer list Tcl_Obj is duplicated. 930*076b9443SCy Schubert ** The new Tcl_Obj contains pointers to the original list elements. 931*076b9443SCy Schubert ** That way, when Tcl_EvalObjv() is run and shimmers the first element 932*076b9443SCy Schubert ** of the list to tclCmdNameType, that alternate representation will 933*076b9443SCy Schubert ** be preserved and reused on the next invocation. 934*076b9443SCy Schubert */ 935*076b9443SCy Schubert Tcl_Obj **aArg; 936*076b9443SCy Schubert int nArg; 937*076b9443SCy Schubert if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){ 938*076b9443SCy Schubert sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 939*076b9443SCy Schubert return; 940*076b9443SCy Schubert } 941*076b9443SCy Schubert pCmd = Tcl_NewListObj(nArg, aArg); 942*076b9443SCy Schubert Tcl_IncrRefCount(pCmd); 943*076b9443SCy Schubert for(i=0; i<argc; i++){ 944*076b9443SCy Schubert sqlite3_value *pIn = argv[i]; 945*076b9443SCy Schubert Tcl_Obj *pVal; 946*076b9443SCy Schubert 947*076b9443SCy Schubert /* Set pVal to contain the i'th column of this row. */ 948*076b9443SCy Schubert switch( sqlite3_value_type(pIn) ){ 949*076b9443SCy Schubert case SQLITE_BLOB: { 950*076b9443SCy Schubert int bytes = sqlite3_value_bytes(pIn); 951*076b9443SCy Schubert pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes); 952*076b9443SCy Schubert break; 953*076b9443SCy Schubert } 954*076b9443SCy Schubert case SQLITE_INTEGER: { 955*076b9443SCy Schubert sqlite_int64 v = sqlite3_value_int64(pIn); 956*076b9443SCy Schubert if( v>=-2147483647 && v<=2147483647 ){ 957*076b9443SCy Schubert pVal = Tcl_NewIntObj((int)v); 958*076b9443SCy Schubert }else{ 959*076b9443SCy Schubert pVal = Tcl_NewWideIntObj(v); 960*076b9443SCy Schubert } 961*076b9443SCy Schubert break; 962*076b9443SCy Schubert } 963*076b9443SCy Schubert case SQLITE_FLOAT: { 964*076b9443SCy Schubert double r = sqlite3_value_double(pIn); 965*076b9443SCy Schubert pVal = Tcl_NewDoubleObj(r); 966*076b9443SCy Schubert break; 967*076b9443SCy Schubert } 968*076b9443SCy Schubert case SQLITE_NULL: { 969*076b9443SCy Schubert pVal = Tcl_NewStringObj(p->pDb->zNull, -1); 970*076b9443SCy Schubert break; 971*076b9443SCy Schubert } 972*076b9443SCy Schubert default: { 973*076b9443SCy Schubert int bytes = sqlite3_value_bytes(pIn); 974*076b9443SCy Schubert pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes); 975*076b9443SCy Schubert break; 976*076b9443SCy Schubert } 977*076b9443SCy Schubert } 978*076b9443SCy Schubert rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal); 979*076b9443SCy Schubert if( rc ){ 980*076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 981*076b9443SCy Schubert sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 982*076b9443SCy Schubert return; 983*076b9443SCy Schubert } 984*076b9443SCy Schubert } 985*076b9443SCy Schubert if( !p->useEvalObjv ){ 986*076b9443SCy Schubert /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd 987*076b9443SCy Schubert ** is a list without a string representation. To prevent this from 988*076b9443SCy Schubert ** happening, make sure pCmd has a valid string representation */ 989*076b9443SCy Schubert Tcl_GetString(pCmd); 990*076b9443SCy Schubert } 991*076b9443SCy Schubert rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 992*076b9443SCy Schubert Tcl_DecrRefCount(pCmd); 993*076b9443SCy Schubert } 994*076b9443SCy Schubert 995*076b9443SCy Schubert if( rc && rc!=TCL_RETURN ){ 996*076b9443SCy Schubert sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 997*076b9443SCy Schubert }else{ 998*076b9443SCy Schubert Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); 999*076b9443SCy Schubert int n; 1000*076b9443SCy Schubert u8 *data; 1001*076b9443SCy Schubert const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 1002*076b9443SCy Schubert char c = zType[0]; 1003*076b9443SCy Schubert if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ 1004*076b9443SCy Schubert /* Only return a BLOB type if the Tcl variable is a bytearray and 1005*076b9443SCy Schubert ** has no string representation. */ 1006*076b9443SCy Schubert data = Tcl_GetByteArrayFromObj(pVar, &n); 1007*076b9443SCy Schubert sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT); 1008*076b9443SCy Schubert }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 1009*076b9443SCy Schubert Tcl_GetIntFromObj(0, pVar, &n); 1010*076b9443SCy Schubert sqlite3_result_int(context, n); 1011*076b9443SCy Schubert }else if( c=='d' && strcmp(zType,"double")==0 ){ 1012*076b9443SCy Schubert double r; 1013*076b9443SCy Schubert Tcl_GetDoubleFromObj(0, pVar, &r); 1014*076b9443SCy Schubert sqlite3_result_double(context, r); 1015*076b9443SCy Schubert }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 1016*076b9443SCy Schubert (c=='i' && strcmp(zType,"int")==0) ){ 1017*076b9443SCy Schubert Tcl_WideInt v; 1018*076b9443SCy Schubert Tcl_GetWideIntFromObj(0, pVar, &v); 1019*076b9443SCy Schubert sqlite3_result_int64(context, v); 1020*076b9443SCy Schubert }else{ 1021*076b9443SCy Schubert data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 1022*076b9443SCy Schubert sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT); 1023*076b9443SCy Schubert } 1024*076b9443SCy Schubert } 1025*076b9443SCy Schubert } 1026*076b9443SCy Schubert 1027*076b9443SCy Schubert #ifndef SQLITE_OMIT_AUTHORIZATION 1028*076b9443SCy Schubert /* 1029*076b9443SCy Schubert ** This is the authentication function. It appends the authentication 1030*076b9443SCy Schubert ** type code and the two arguments to zCmd[] then invokes the result 1031*076b9443SCy Schubert ** on the interpreter. The reply is examined to determine if the 1032*076b9443SCy Schubert ** authentication fails or succeeds. 1033*076b9443SCy Schubert */ 1034*076b9443SCy Schubert static int auth_callback( 1035*076b9443SCy Schubert void *pArg, 1036*076b9443SCy Schubert int code, 1037*076b9443SCy Schubert const char *zArg1, 1038*076b9443SCy Schubert const char *zArg2, 1039*076b9443SCy Schubert const char *zArg3, 1040*076b9443SCy Schubert const char *zArg4 1041*076b9443SCy Schubert #ifdef SQLITE_USER_AUTHENTICATION 1042*076b9443SCy Schubert ,const char *zArg5 1043*076b9443SCy Schubert #endif 1044*076b9443SCy Schubert ){ 1045*076b9443SCy Schubert const char *zCode; 1046*076b9443SCy Schubert Tcl_DString str; 1047*076b9443SCy Schubert int rc; 1048*076b9443SCy Schubert const char *zReply; 1049*076b9443SCy Schubert /* EVIDENCE-OF: R-38590-62769 The first parameter to the authorizer 1050*076b9443SCy Schubert ** callback is a copy of the third parameter to the 1051*076b9443SCy Schubert ** sqlite3_set_authorizer() interface. 1052*076b9443SCy Schubert */ 1053*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)pArg; 1054*076b9443SCy Schubert if( pDb->disableAuth ) return SQLITE_OK; 1055*076b9443SCy Schubert 1056*076b9443SCy Schubert /* EVIDENCE-OF: R-56518-44310 The second parameter to the callback is an 1057*076b9443SCy Schubert ** integer action code that specifies the particular action to be 1058*076b9443SCy Schubert ** authorized. */ 1059*076b9443SCy Schubert switch( code ){ 1060*076b9443SCy Schubert case SQLITE_COPY : zCode="SQLITE_COPY"; break; 1061*076b9443SCy Schubert case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; 1062*076b9443SCy Schubert case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; 1063*076b9443SCy Schubert case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; 1064*076b9443SCy Schubert case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; 1065*076b9443SCy Schubert case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; 1066*076b9443SCy Schubert case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; 1067*076b9443SCy Schubert case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; 1068*076b9443SCy Schubert case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; 1069*076b9443SCy Schubert case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; 1070*076b9443SCy Schubert case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; 1071*076b9443SCy Schubert case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; 1072*076b9443SCy Schubert case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; 1073*076b9443SCy Schubert case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; 1074*076b9443SCy Schubert case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; 1075*076b9443SCy Schubert case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; 1076*076b9443SCy Schubert case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; 1077*076b9443SCy Schubert case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; 1078*076b9443SCy Schubert case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; 1079*076b9443SCy Schubert case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; 1080*076b9443SCy Schubert case SQLITE_READ : zCode="SQLITE_READ"; break; 1081*076b9443SCy Schubert case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; 1082*076b9443SCy Schubert case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; 1083*076b9443SCy Schubert case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; 1084*076b9443SCy Schubert case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; 1085*076b9443SCy Schubert case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; 1086*076b9443SCy Schubert case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break; 1087*076b9443SCy Schubert case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break; 1088*076b9443SCy Schubert case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break; 1089*076b9443SCy Schubert case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break; 1090*076b9443SCy Schubert case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break; 1091*076b9443SCy Schubert case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break; 1092*076b9443SCy Schubert case SQLITE_SAVEPOINT : zCode="SQLITE_SAVEPOINT"; break; 1093*076b9443SCy Schubert case SQLITE_RECURSIVE : zCode="SQLITE_RECURSIVE"; break; 1094*076b9443SCy Schubert default : zCode="????"; break; 1095*076b9443SCy Schubert } 1096*076b9443SCy Schubert Tcl_DStringInit(&str); 1097*076b9443SCy Schubert Tcl_DStringAppend(&str, pDb->zAuth, -1); 1098*076b9443SCy Schubert Tcl_DStringAppendElement(&str, zCode); 1099*076b9443SCy Schubert Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); 1100*076b9443SCy Schubert Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); 1101*076b9443SCy Schubert Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); 1102*076b9443SCy Schubert Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); 1103*076b9443SCy Schubert #ifdef SQLITE_USER_AUTHENTICATION 1104*076b9443SCy Schubert Tcl_DStringAppendElement(&str, zArg5 ? zArg5 : ""); 1105*076b9443SCy Schubert #endif 1106*076b9443SCy Schubert rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); 1107*076b9443SCy Schubert Tcl_DStringFree(&str); 1108*076b9443SCy Schubert zReply = rc==TCL_OK ? Tcl_GetStringResult(pDb->interp) : "SQLITE_DENY"; 1109*076b9443SCy Schubert if( strcmp(zReply,"SQLITE_OK")==0 ){ 1110*076b9443SCy Schubert rc = SQLITE_OK; 1111*076b9443SCy Schubert }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ 1112*076b9443SCy Schubert rc = SQLITE_DENY; 1113*076b9443SCy Schubert }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ 1114*076b9443SCy Schubert rc = SQLITE_IGNORE; 1115*076b9443SCy Schubert }else{ 1116*076b9443SCy Schubert rc = 999; 1117*076b9443SCy Schubert } 1118*076b9443SCy Schubert return rc; 1119*076b9443SCy Schubert } 1120*076b9443SCy Schubert #endif /* SQLITE_OMIT_AUTHORIZATION */ 1121*076b9443SCy Schubert 1122*076b9443SCy Schubert /* 1123*076b9443SCy Schubert ** This routine reads a line of text from FILE in, stores 1124*076b9443SCy Schubert ** the text in memory obtained from malloc() and returns a pointer 1125*076b9443SCy Schubert ** to the text. NULL is returned at end of file, or if malloc() 1126*076b9443SCy Schubert ** fails. 1127*076b9443SCy Schubert ** 1128*076b9443SCy Schubert ** The interface is like "readline" but no command-line editing 1129*076b9443SCy Schubert ** is done. 1130*076b9443SCy Schubert ** 1131*076b9443SCy Schubert ** copied from shell.c from '.import' command 1132*076b9443SCy Schubert */ 1133*076b9443SCy Schubert static char *local_getline(char *zPrompt, FILE *in){ 1134*076b9443SCy Schubert char *zLine; 1135*076b9443SCy Schubert int nLine; 1136*076b9443SCy Schubert int n; 1137*076b9443SCy Schubert 1138*076b9443SCy Schubert nLine = 100; 1139*076b9443SCy Schubert zLine = malloc( nLine ); 1140*076b9443SCy Schubert if( zLine==0 ) return 0; 1141*076b9443SCy Schubert n = 0; 1142*076b9443SCy Schubert while( 1 ){ 1143*076b9443SCy Schubert if( n+100>nLine ){ 1144*076b9443SCy Schubert nLine = nLine*2 + 100; 1145*076b9443SCy Schubert zLine = realloc(zLine, nLine); 1146*076b9443SCy Schubert if( zLine==0 ) return 0; 1147*076b9443SCy Schubert } 1148*076b9443SCy Schubert if( fgets(&zLine[n], nLine - n, in)==0 ){ 1149*076b9443SCy Schubert if( n==0 ){ 1150*076b9443SCy Schubert free(zLine); 1151*076b9443SCy Schubert return 0; 1152*076b9443SCy Schubert } 1153*076b9443SCy Schubert zLine[n] = 0; 1154*076b9443SCy Schubert break; 1155*076b9443SCy Schubert } 1156*076b9443SCy Schubert while( zLine[n] ){ n++; } 1157*076b9443SCy Schubert if( n>0 && zLine[n-1]=='\n' ){ 1158*076b9443SCy Schubert n--; 1159*076b9443SCy Schubert zLine[n] = 0; 1160*076b9443SCy Schubert break; 1161*076b9443SCy Schubert } 1162*076b9443SCy Schubert } 1163*076b9443SCy Schubert zLine = realloc( zLine, n+1 ); 1164*076b9443SCy Schubert return zLine; 1165*076b9443SCy Schubert } 1166*076b9443SCy Schubert 1167*076b9443SCy Schubert 1168*076b9443SCy Schubert /* 1169*076b9443SCy Schubert ** This function is part of the implementation of the command: 1170*076b9443SCy Schubert ** 1171*076b9443SCy Schubert ** $db transaction [-deferred|-immediate|-exclusive] SCRIPT 1172*076b9443SCy Schubert ** 1173*076b9443SCy Schubert ** It is invoked after evaluating the script SCRIPT to commit or rollback 1174*076b9443SCy Schubert ** the transaction or savepoint opened by the [transaction] command. 1175*076b9443SCy Schubert */ 1176*076b9443SCy Schubert static int SQLITE_TCLAPI DbTransPostCmd( 1177*076b9443SCy Schubert ClientData data[], /* data[0] is the Sqlite3Db* for $db */ 1178*076b9443SCy Schubert Tcl_Interp *interp, /* Tcl interpreter */ 1179*076b9443SCy Schubert int result /* Result of evaluating SCRIPT */ 1180*076b9443SCy Schubert ){ 1181*076b9443SCy Schubert static const char *const azEnd[] = { 1182*076b9443SCy Schubert "RELEASE _tcl_transaction", /* rc==TCL_ERROR, nTransaction!=0 */ 1183*076b9443SCy Schubert "COMMIT", /* rc!=TCL_ERROR, nTransaction==0 */ 1184*076b9443SCy Schubert "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction", 1185*076b9443SCy Schubert "ROLLBACK" /* rc==TCL_ERROR, nTransaction==0 */ 1186*076b9443SCy Schubert }; 1187*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)data[0]; 1188*076b9443SCy Schubert int rc = result; 1189*076b9443SCy Schubert const char *zEnd; 1190*076b9443SCy Schubert 1191*076b9443SCy Schubert pDb->nTransaction--; 1192*076b9443SCy Schubert zEnd = azEnd[(rc==TCL_ERROR)*2 + (pDb->nTransaction==0)]; 1193*076b9443SCy Schubert 1194*076b9443SCy Schubert pDb->disableAuth++; 1195*076b9443SCy Schubert if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){ 1196*076b9443SCy Schubert /* This is a tricky scenario to handle. The most likely cause of an 1197*076b9443SCy Schubert ** error is that the exec() above was an attempt to commit the 1198*076b9443SCy Schubert ** top-level transaction that returned SQLITE_BUSY. Or, less likely, 1199*076b9443SCy Schubert ** that an IO-error has occurred. In either case, throw a Tcl exception 1200*076b9443SCy Schubert ** and try to rollback the transaction. 1201*076b9443SCy Schubert ** 1202*076b9443SCy Schubert ** But it could also be that the user executed one or more BEGIN, 1203*076b9443SCy Schubert ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing 1204*076b9443SCy Schubert ** this method's logic. Not clear how this would be best handled. 1205*076b9443SCy Schubert */ 1206*076b9443SCy Schubert if( rc!=TCL_ERROR ){ 1207*076b9443SCy Schubert Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 1208*076b9443SCy Schubert rc = TCL_ERROR; 1209*076b9443SCy Schubert } 1210*076b9443SCy Schubert sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0); 1211*076b9443SCy Schubert } 1212*076b9443SCy Schubert pDb->disableAuth--; 1213*076b9443SCy Schubert 1214*076b9443SCy Schubert return rc; 1215*076b9443SCy Schubert } 1216*076b9443SCy Schubert 1217*076b9443SCy Schubert /* 1218*076b9443SCy Schubert ** Unless SQLITE_TEST is defined, this function is a simple wrapper around 1219*076b9443SCy Schubert ** sqlite3_prepare_v2(). If SQLITE_TEST is defined, then it uses either 1220*076b9443SCy Schubert ** sqlite3_prepare_v2() or legacy interface sqlite3_prepare(), depending 1221*076b9443SCy Schubert ** on whether or not the [db_use_legacy_prepare] command has been used to 1222*076b9443SCy Schubert ** configure the connection. 1223*076b9443SCy Schubert */ 1224*076b9443SCy Schubert static int dbPrepare( 1225*076b9443SCy Schubert SqliteDb *pDb, /* Database object */ 1226*076b9443SCy Schubert const char *zSql, /* SQL to compile */ 1227*076b9443SCy Schubert sqlite3_stmt **ppStmt, /* OUT: Prepared statement */ 1228*076b9443SCy Schubert const char **pzOut /* OUT: Pointer to next SQL statement */ 1229*076b9443SCy Schubert ){ 1230*076b9443SCy Schubert unsigned int prepFlags = 0; 1231*076b9443SCy Schubert #ifdef SQLITE_TEST 1232*076b9443SCy Schubert if( pDb->bLegacyPrepare ){ 1233*076b9443SCy Schubert return sqlite3_prepare(pDb->db, zSql, -1, ppStmt, pzOut); 1234*076b9443SCy Schubert } 1235*076b9443SCy Schubert #endif 1236*076b9443SCy Schubert /* If the statement cache is large, use the SQLITE_PREPARE_PERSISTENT 1237*076b9443SCy Schubert ** flags, which uses less lookaside memory. But if the cache is small, 1238*076b9443SCy Schubert ** omit that flag to make full use of lookaside */ 1239*076b9443SCy Schubert if( pDb->maxStmt>5 ) prepFlags = SQLITE_PREPARE_PERSISTENT; 1240*076b9443SCy Schubert 1241*076b9443SCy Schubert return sqlite3_prepare_v3(pDb->db, zSql, -1, prepFlags, ppStmt, pzOut); 1242*076b9443SCy Schubert } 1243*076b9443SCy Schubert 1244*076b9443SCy Schubert /* 1245*076b9443SCy Schubert ** Search the cache for a prepared-statement object that implements the 1246*076b9443SCy Schubert ** first SQL statement in the buffer pointed to by parameter zIn. If 1247*076b9443SCy Schubert ** no such prepared-statement can be found, allocate and prepare a new 1248*076b9443SCy Schubert ** one. In either case, bind the current values of the relevant Tcl 1249*076b9443SCy Schubert ** variables to any $var, :var or @var variables in the statement. Before 1250*076b9443SCy Schubert ** returning, set *ppPreStmt to point to the prepared-statement object. 1251*076b9443SCy Schubert ** 1252*076b9443SCy Schubert ** Output parameter *pzOut is set to point to the next SQL statement in 1253*076b9443SCy Schubert ** buffer zIn, or to the '\0' byte at the end of zIn if there is no 1254*076b9443SCy Schubert ** next statement. 1255*076b9443SCy Schubert ** 1256*076b9443SCy Schubert ** If successful, TCL_OK is returned. Otherwise, TCL_ERROR is returned 1257*076b9443SCy Schubert ** and an error message loaded into interpreter pDb->interp. 1258*076b9443SCy Schubert */ 1259*076b9443SCy Schubert static int dbPrepareAndBind( 1260*076b9443SCy Schubert SqliteDb *pDb, /* Database object */ 1261*076b9443SCy Schubert char const *zIn, /* SQL to compile */ 1262*076b9443SCy Schubert char const **pzOut, /* OUT: Pointer to next SQL statement */ 1263*076b9443SCy Schubert SqlPreparedStmt **ppPreStmt /* OUT: Object used to cache statement */ 1264*076b9443SCy Schubert ){ 1265*076b9443SCy Schubert const char *zSql = zIn; /* Pointer to first SQL statement in zIn */ 1266*076b9443SCy Schubert sqlite3_stmt *pStmt = 0; /* Prepared statement object */ 1267*076b9443SCy Schubert SqlPreparedStmt *pPreStmt; /* Pointer to cached statement */ 1268*076b9443SCy Schubert int nSql; /* Length of zSql in bytes */ 1269*076b9443SCy Schubert int nVar = 0; /* Number of variables in statement */ 1270*076b9443SCy Schubert int iParm = 0; /* Next free entry in apParm */ 1271*076b9443SCy Schubert char c; 1272*076b9443SCy Schubert int i; 1273*076b9443SCy Schubert Tcl_Interp *interp = pDb->interp; 1274*076b9443SCy Schubert 1275*076b9443SCy Schubert *ppPreStmt = 0; 1276*076b9443SCy Schubert 1277*076b9443SCy Schubert /* Trim spaces from the start of zSql and calculate the remaining length. */ 1278*076b9443SCy Schubert while( (c = zSql[0])==' ' || c=='\t' || c=='\r' || c=='\n' ){ zSql++; } 1279*076b9443SCy Schubert nSql = strlen30(zSql); 1280*076b9443SCy Schubert 1281*076b9443SCy Schubert for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){ 1282*076b9443SCy Schubert int n = pPreStmt->nSql; 1283*076b9443SCy Schubert if( nSql>=n 1284*076b9443SCy Schubert && memcmp(pPreStmt->zSql, zSql, n)==0 1285*076b9443SCy Schubert && (zSql[n]==0 || zSql[n-1]==';') 1286*076b9443SCy Schubert ){ 1287*076b9443SCy Schubert pStmt = pPreStmt->pStmt; 1288*076b9443SCy Schubert *pzOut = &zSql[pPreStmt->nSql]; 1289*076b9443SCy Schubert 1290*076b9443SCy Schubert /* When a prepared statement is found, unlink it from the 1291*076b9443SCy Schubert ** cache list. It will later be added back to the beginning 1292*076b9443SCy Schubert ** of the cache list in order to implement LRU replacement. 1293*076b9443SCy Schubert */ 1294*076b9443SCy Schubert if( pPreStmt->pPrev ){ 1295*076b9443SCy Schubert pPreStmt->pPrev->pNext = pPreStmt->pNext; 1296*076b9443SCy Schubert }else{ 1297*076b9443SCy Schubert pDb->stmtList = pPreStmt->pNext; 1298*076b9443SCy Schubert } 1299*076b9443SCy Schubert if( pPreStmt->pNext ){ 1300*076b9443SCy Schubert pPreStmt->pNext->pPrev = pPreStmt->pPrev; 1301*076b9443SCy Schubert }else{ 1302*076b9443SCy Schubert pDb->stmtLast = pPreStmt->pPrev; 1303*076b9443SCy Schubert } 1304*076b9443SCy Schubert pDb->nStmt--; 1305*076b9443SCy Schubert nVar = sqlite3_bind_parameter_count(pStmt); 1306*076b9443SCy Schubert break; 1307*076b9443SCy Schubert } 1308*076b9443SCy Schubert } 1309*076b9443SCy Schubert 1310*076b9443SCy Schubert /* If no prepared statement was found. Compile the SQL text. Also allocate 1311*076b9443SCy Schubert ** a new SqlPreparedStmt structure. */ 1312*076b9443SCy Schubert if( pPreStmt==0 ){ 1313*076b9443SCy Schubert int nByte; 1314*076b9443SCy Schubert 1315*076b9443SCy Schubert if( SQLITE_OK!=dbPrepare(pDb, zSql, &pStmt, pzOut) ){ 1316*076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 1317*076b9443SCy Schubert return TCL_ERROR; 1318*076b9443SCy Schubert } 1319*076b9443SCy Schubert if( pStmt==0 ){ 1320*076b9443SCy Schubert if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ 1321*076b9443SCy Schubert /* A compile-time error in the statement. */ 1322*076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 1323*076b9443SCy Schubert return TCL_ERROR; 1324*076b9443SCy Schubert }else{ 1325*076b9443SCy Schubert /* The statement was a no-op. Continue to the next statement 1326*076b9443SCy Schubert ** in the SQL string. 1327*076b9443SCy Schubert */ 1328*076b9443SCy Schubert return TCL_OK; 1329*076b9443SCy Schubert } 1330*076b9443SCy Schubert } 1331*076b9443SCy Schubert 1332*076b9443SCy Schubert assert( pPreStmt==0 ); 1333*076b9443SCy Schubert nVar = sqlite3_bind_parameter_count(pStmt); 1334*076b9443SCy Schubert nByte = sizeof(SqlPreparedStmt) + nVar*sizeof(Tcl_Obj *); 1335*076b9443SCy Schubert pPreStmt = (SqlPreparedStmt*)Tcl_Alloc(nByte); 1336*076b9443SCy Schubert memset(pPreStmt, 0, nByte); 1337*076b9443SCy Schubert 1338*076b9443SCy Schubert pPreStmt->pStmt = pStmt; 1339*076b9443SCy Schubert pPreStmt->nSql = (int)(*pzOut - zSql); 1340*076b9443SCy Schubert pPreStmt->zSql = sqlite3_sql(pStmt); 1341*076b9443SCy Schubert pPreStmt->apParm = (Tcl_Obj **)&pPreStmt[1]; 1342*076b9443SCy Schubert #ifdef SQLITE_TEST 1343*076b9443SCy Schubert if( pPreStmt->zSql==0 ){ 1344*076b9443SCy Schubert char *zCopy = Tcl_Alloc(pPreStmt->nSql + 1); 1345*076b9443SCy Schubert memcpy(zCopy, zSql, pPreStmt->nSql); 1346*076b9443SCy Schubert zCopy[pPreStmt->nSql] = '\0'; 1347*076b9443SCy Schubert pPreStmt->zSql = zCopy; 1348*076b9443SCy Schubert } 1349*076b9443SCy Schubert #endif 1350*076b9443SCy Schubert } 1351*076b9443SCy Schubert assert( pPreStmt ); 1352*076b9443SCy Schubert assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql ); 1353*076b9443SCy Schubert assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) ); 1354*076b9443SCy Schubert 1355*076b9443SCy Schubert /* Bind values to parameters that begin with $ or : */ 1356*076b9443SCy Schubert for(i=1; i<=nVar; i++){ 1357*076b9443SCy Schubert const char *zVar = sqlite3_bind_parameter_name(pStmt, i); 1358*076b9443SCy Schubert if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){ 1359*076b9443SCy Schubert Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); 1360*076b9443SCy Schubert if( pVar ){ 1361*076b9443SCy Schubert int n; 1362*076b9443SCy Schubert u8 *data; 1363*076b9443SCy Schubert const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 1364*076b9443SCy Schubert c = zType[0]; 1365*076b9443SCy Schubert if( zVar[0]=='@' || 1366*076b9443SCy Schubert (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){ 1367*076b9443SCy Schubert /* Load a BLOB type if the Tcl variable is a bytearray and 1368*076b9443SCy Schubert ** it has no string representation or the host 1369*076b9443SCy Schubert ** parameter name begins with "@". */ 1370*076b9443SCy Schubert data = Tcl_GetByteArrayFromObj(pVar, &n); 1371*076b9443SCy Schubert sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); 1372*076b9443SCy Schubert Tcl_IncrRefCount(pVar); 1373*076b9443SCy Schubert pPreStmt->apParm[iParm++] = pVar; 1374*076b9443SCy Schubert }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 1375*076b9443SCy Schubert Tcl_GetIntFromObj(interp, pVar, &n); 1376*076b9443SCy Schubert sqlite3_bind_int(pStmt, i, n); 1377*076b9443SCy Schubert }else if( c=='d' && strcmp(zType,"double")==0 ){ 1378*076b9443SCy Schubert double r; 1379*076b9443SCy Schubert Tcl_GetDoubleFromObj(interp, pVar, &r); 1380*076b9443SCy Schubert sqlite3_bind_double(pStmt, i, r); 1381*076b9443SCy Schubert }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 1382*076b9443SCy Schubert (c=='i' && strcmp(zType,"int")==0) ){ 1383*076b9443SCy Schubert Tcl_WideInt v; 1384*076b9443SCy Schubert Tcl_GetWideIntFromObj(interp, pVar, &v); 1385*076b9443SCy Schubert sqlite3_bind_int64(pStmt, i, v); 1386*076b9443SCy Schubert }else{ 1387*076b9443SCy Schubert data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 1388*076b9443SCy Schubert sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); 1389*076b9443SCy Schubert Tcl_IncrRefCount(pVar); 1390*076b9443SCy Schubert pPreStmt->apParm[iParm++] = pVar; 1391*076b9443SCy Schubert } 1392*076b9443SCy Schubert }else{ 1393*076b9443SCy Schubert sqlite3_bind_null(pStmt, i); 1394*076b9443SCy Schubert } 1395*076b9443SCy Schubert } 1396*076b9443SCy Schubert } 1397*076b9443SCy Schubert pPreStmt->nParm = iParm; 1398*076b9443SCy Schubert *ppPreStmt = pPreStmt; 1399*076b9443SCy Schubert 1400*076b9443SCy Schubert return TCL_OK; 1401*076b9443SCy Schubert } 1402*076b9443SCy Schubert 1403*076b9443SCy Schubert /* 1404*076b9443SCy Schubert ** Release a statement reference obtained by calling dbPrepareAndBind(). 1405*076b9443SCy Schubert ** There should be exactly one call to this function for each call to 1406*076b9443SCy Schubert ** dbPrepareAndBind(). 1407*076b9443SCy Schubert ** 1408*076b9443SCy Schubert ** If the discard parameter is non-zero, then the statement is deleted 1409*076b9443SCy Schubert ** immediately. Otherwise it is added to the LRU list and may be returned 1410*076b9443SCy Schubert ** by a subsequent call to dbPrepareAndBind(). 1411*076b9443SCy Schubert */ 1412*076b9443SCy Schubert static void dbReleaseStmt( 1413*076b9443SCy Schubert SqliteDb *pDb, /* Database handle */ 1414*076b9443SCy Schubert SqlPreparedStmt *pPreStmt, /* Prepared statement handle to release */ 1415*076b9443SCy Schubert int discard /* True to delete (not cache) the pPreStmt */ 1416*076b9443SCy Schubert ){ 1417*076b9443SCy Schubert int i; 1418*076b9443SCy Schubert 1419*076b9443SCy Schubert /* Free the bound string and blob parameters */ 1420*076b9443SCy Schubert for(i=0; i<pPreStmt->nParm; i++){ 1421*076b9443SCy Schubert Tcl_DecrRefCount(pPreStmt->apParm[i]); 1422*076b9443SCy Schubert } 1423*076b9443SCy Schubert pPreStmt->nParm = 0; 1424*076b9443SCy Schubert 1425*076b9443SCy Schubert if( pDb->maxStmt<=0 || discard ){ 1426*076b9443SCy Schubert /* If the cache is turned off, deallocated the statement */ 1427*076b9443SCy Schubert dbFreeStmt(pPreStmt); 1428*076b9443SCy Schubert }else{ 1429*076b9443SCy Schubert /* Add the prepared statement to the beginning of the cache list. */ 1430*076b9443SCy Schubert pPreStmt->pNext = pDb->stmtList; 1431*076b9443SCy Schubert pPreStmt->pPrev = 0; 1432*076b9443SCy Schubert if( pDb->stmtList ){ 1433*076b9443SCy Schubert pDb->stmtList->pPrev = pPreStmt; 1434*076b9443SCy Schubert } 1435*076b9443SCy Schubert pDb->stmtList = pPreStmt; 1436*076b9443SCy Schubert if( pDb->stmtLast==0 ){ 1437*076b9443SCy Schubert assert( pDb->nStmt==0 ); 1438*076b9443SCy Schubert pDb->stmtLast = pPreStmt; 1439*076b9443SCy Schubert }else{ 1440*076b9443SCy Schubert assert( pDb->nStmt>0 ); 1441*076b9443SCy Schubert } 1442*076b9443SCy Schubert pDb->nStmt++; 1443*076b9443SCy Schubert 1444*076b9443SCy Schubert /* If we have too many statement in cache, remove the surplus from 1445*076b9443SCy Schubert ** the end of the cache list. */ 1446*076b9443SCy Schubert while( pDb->nStmt>pDb->maxStmt ){ 1447*076b9443SCy Schubert SqlPreparedStmt *pLast = pDb->stmtLast; 1448*076b9443SCy Schubert pDb->stmtLast = pLast->pPrev; 1449*076b9443SCy Schubert pDb->stmtLast->pNext = 0; 1450*076b9443SCy Schubert pDb->nStmt--; 1451*076b9443SCy Schubert dbFreeStmt(pLast); 1452*076b9443SCy Schubert } 1453*076b9443SCy Schubert } 1454*076b9443SCy Schubert } 1455*076b9443SCy Schubert 1456*076b9443SCy Schubert /* 1457*076b9443SCy Schubert ** Structure used with dbEvalXXX() functions: 1458*076b9443SCy Schubert ** 1459*076b9443SCy Schubert ** dbEvalInit() 1460*076b9443SCy Schubert ** dbEvalStep() 1461*076b9443SCy Schubert ** dbEvalFinalize() 1462*076b9443SCy Schubert ** dbEvalRowInfo() 1463*076b9443SCy Schubert ** dbEvalColumnValue() 1464*076b9443SCy Schubert */ 1465*076b9443SCy Schubert typedef struct DbEvalContext DbEvalContext; 1466*076b9443SCy Schubert struct DbEvalContext { 1467*076b9443SCy Schubert SqliteDb *pDb; /* Database handle */ 1468*076b9443SCy Schubert Tcl_Obj *pSql; /* Object holding string zSql */ 1469*076b9443SCy Schubert const char *zSql; /* Remaining SQL to execute */ 1470*076b9443SCy Schubert SqlPreparedStmt *pPreStmt; /* Current statement */ 1471*076b9443SCy Schubert int nCol; /* Number of columns returned by pStmt */ 1472*076b9443SCy Schubert int evalFlags; /* Flags used */ 1473*076b9443SCy Schubert Tcl_Obj *pArray; /* Name of array variable */ 1474*076b9443SCy Schubert Tcl_Obj **apColName; /* Array of column names */ 1475*076b9443SCy Schubert }; 1476*076b9443SCy Schubert 1477*076b9443SCy Schubert #define SQLITE_EVAL_WITHOUTNULLS 0x00001 /* Unset array(*) for NULL */ 1478*076b9443SCy Schubert 1479*076b9443SCy Schubert /* 1480*076b9443SCy Schubert ** Release any cache of column names currently held as part of 1481*076b9443SCy Schubert ** the DbEvalContext structure passed as the first argument. 1482*076b9443SCy Schubert */ 1483*076b9443SCy Schubert static void dbReleaseColumnNames(DbEvalContext *p){ 1484*076b9443SCy Schubert if( p->apColName ){ 1485*076b9443SCy Schubert int i; 1486*076b9443SCy Schubert for(i=0; i<p->nCol; i++){ 1487*076b9443SCy Schubert Tcl_DecrRefCount(p->apColName[i]); 1488*076b9443SCy Schubert } 1489*076b9443SCy Schubert Tcl_Free((char *)p->apColName); 1490*076b9443SCy Schubert p->apColName = 0; 1491*076b9443SCy Schubert } 1492*076b9443SCy Schubert p->nCol = 0; 1493*076b9443SCy Schubert } 1494*076b9443SCy Schubert 1495*076b9443SCy Schubert /* 1496*076b9443SCy Schubert ** Initialize a DbEvalContext structure. 1497*076b9443SCy Schubert ** 1498*076b9443SCy Schubert ** If pArray is not NULL, then it contains the name of a Tcl array 1499*076b9443SCy Schubert ** variable. The "*" member of this array is set to a list containing 1500*076b9443SCy Schubert ** the names of the columns returned by the statement as part of each 1501*076b9443SCy Schubert ** call to dbEvalStep(), in order from left to right. e.g. if the names 1502*076b9443SCy Schubert ** of the returned columns are a, b and c, it does the equivalent of the 1503*076b9443SCy Schubert ** tcl command: 1504*076b9443SCy Schubert ** 1505*076b9443SCy Schubert ** set ${pArray}(*) {a b c} 1506*076b9443SCy Schubert */ 1507*076b9443SCy Schubert static void dbEvalInit( 1508*076b9443SCy Schubert DbEvalContext *p, /* Pointer to structure to initialize */ 1509*076b9443SCy Schubert SqliteDb *pDb, /* Database handle */ 1510*076b9443SCy Schubert Tcl_Obj *pSql, /* Object containing SQL script */ 1511*076b9443SCy Schubert Tcl_Obj *pArray, /* Name of Tcl array to set (*) element of */ 1512*076b9443SCy Schubert int evalFlags /* Flags controlling evaluation */ 1513*076b9443SCy Schubert ){ 1514*076b9443SCy Schubert memset(p, 0, sizeof(DbEvalContext)); 1515*076b9443SCy Schubert p->pDb = pDb; 1516*076b9443SCy Schubert p->zSql = Tcl_GetString(pSql); 1517*076b9443SCy Schubert p->pSql = pSql; 1518*076b9443SCy Schubert Tcl_IncrRefCount(pSql); 1519*076b9443SCy Schubert if( pArray ){ 1520*076b9443SCy Schubert p->pArray = pArray; 1521*076b9443SCy Schubert Tcl_IncrRefCount(pArray); 1522*076b9443SCy Schubert } 1523*076b9443SCy Schubert p->evalFlags = evalFlags; 1524*076b9443SCy Schubert } 1525*076b9443SCy Schubert 1526*076b9443SCy Schubert /* 1527*076b9443SCy Schubert ** Obtain information about the row that the DbEvalContext passed as the 1528*076b9443SCy Schubert ** first argument currently points to. 1529*076b9443SCy Schubert */ 1530*076b9443SCy Schubert static void dbEvalRowInfo( 1531*076b9443SCy Schubert DbEvalContext *p, /* Evaluation context */ 1532*076b9443SCy Schubert int *pnCol, /* OUT: Number of column names */ 1533*076b9443SCy Schubert Tcl_Obj ***papColName /* OUT: Array of column names */ 1534*076b9443SCy Schubert ){ 1535*076b9443SCy Schubert /* Compute column names */ 1536*076b9443SCy Schubert if( 0==p->apColName ){ 1537*076b9443SCy Schubert sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 1538*076b9443SCy Schubert int i; /* Iterator variable */ 1539*076b9443SCy Schubert int nCol; /* Number of columns returned by pStmt */ 1540*076b9443SCy Schubert Tcl_Obj **apColName = 0; /* Array of column names */ 1541*076b9443SCy Schubert 1542*076b9443SCy Schubert p->nCol = nCol = sqlite3_column_count(pStmt); 1543*076b9443SCy Schubert if( nCol>0 && (papColName || p->pArray) ){ 1544*076b9443SCy Schubert apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); 1545*076b9443SCy Schubert for(i=0; i<nCol; i++){ 1546*076b9443SCy Schubert apColName[i] = Tcl_NewStringObj(sqlite3_column_name(pStmt,i), -1); 1547*076b9443SCy Schubert Tcl_IncrRefCount(apColName[i]); 1548*076b9443SCy Schubert } 1549*076b9443SCy Schubert p->apColName = apColName; 1550*076b9443SCy Schubert } 1551*076b9443SCy Schubert 1552*076b9443SCy Schubert /* If results are being stored in an array variable, then create 1553*076b9443SCy Schubert ** the array(*) entry for that array 1554*076b9443SCy Schubert */ 1555*076b9443SCy Schubert if( p->pArray ){ 1556*076b9443SCy Schubert Tcl_Interp *interp = p->pDb->interp; 1557*076b9443SCy Schubert Tcl_Obj *pColList = Tcl_NewObj(); 1558*076b9443SCy Schubert Tcl_Obj *pStar = Tcl_NewStringObj("*", -1); 1559*076b9443SCy Schubert 1560*076b9443SCy Schubert for(i=0; i<nCol; i++){ 1561*076b9443SCy Schubert Tcl_ListObjAppendElement(interp, pColList, apColName[i]); 1562*076b9443SCy Schubert } 1563*076b9443SCy Schubert Tcl_IncrRefCount(pStar); 1564*076b9443SCy Schubert Tcl_ObjSetVar2(interp, p->pArray, pStar, pColList, 0); 1565*076b9443SCy Schubert Tcl_DecrRefCount(pStar); 1566*076b9443SCy Schubert } 1567*076b9443SCy Schubert } 1568*076b9443SCy Schubert 1569*076b9443SCy Schubert if( papColName ){ 1570*076b9443SCy Schubert *papColName = p->apColName; 1571*076b9443SCy Schubert } 1572*076b9443SCy Schubert if( pnCol ){ 1573*076b9443SCy Schubert *pnCol = p->nCol; 1574*076b9443SCy Schubert } 1575*076b9443SCy Schubert } 1576*076b9443SCy Schubert 1577*076b9443SCy Schubert /* 1578*076b9443SCy Schubert ** Return one of TCL_OK, TCL_BREAK or TCL_ERROR. If TCL_ERROR is 1579*076b9443SCy Schubert ** returned, then an error message is stored in the interpreter before 1580*076b9443SCy Schubert ** returning. 1581*076b9443SCy Schubert ** 1582*076b9443SCy Schubert ** A return value of TCL_OK means there is a row of data available. The 1583*076b9443SCy Schubert ** data may be accessed using dbEvalRowInfo() and dbEvalColumnValue(). This 1584*076b9443SCy Schubert ** is analogous to a return of SQLITE_ROW from sqlite3_step(). If TCL_BREAK 1585*076b9443SCy Schubert ** is returned, then the SQL script has finished executing and there are 1586*076b9443SCy Schubert ** no further rows available. This is similar to SQLITE_DONE. 1587*076b9443SCy Schubert */ 1588*076b9443SCy Schubert static int dbEvalStep(DbEvalContext *p){ 1589*076b9443SCy Schubert const char *zPrevSql = 0; /* Previous value of p->zSql */ 1590*076b9443SCy Schubert 1591*076b9443SCy Schubert while( p->zSql[0] || p->pPreStmt ){ 1592*076b9443SCy Schubert int rc; 1593*076b9443SCy Schubert if( p->pPreStmt==0 ){ 1594*076b9443SCy Schubert zPrevSql = (p->zSql==zPrevSql ? 0 : p->zSql); 1595*076b9443SCy Schubert rc = dbPrepareAndBind(p->pDb, p->zSql, &p->zSql, &p->pPreStmt); 1596*076b9443SCy Schubert if( rc!=TCL_OK ) return rc; 1597*076b9443SCy Schubert }else{ 1598*076b9443SCy Schubert int rcs; 1599*076b9443SCy Schubert SqliteDb *pDb = p->pDb; 1600*076b9443SCy Schubert SqlPreparedStmt *pPreStmt = p->pPreStmt; 1601*076b9443SCy Schubert sqlite3_stmt *pStmt = pPreStmt->pStmt; 1602*076b9443SCy Schubert 1603*076b9443SCy Schubert rcs = sqlite3_step(pStmt); 1604*076b9443SCy Schubert if( rcs==SQLITE_ROW ){ 1605*076b9443SCy Schubert return TCL_OK; 1606*076b9443SCy Schubert } 1607*076b9443SCy Schubert if( p->pArray ){ 1608*076b9443SCy Schubert dbEvalRowInfo(p, 0, 0); 1609*076b9443SCy Schubert } 1610*076b9443SCy Schubert rcs = sqlite3_reset(pStmt); 1611*076b9443SCy Schubert 1612*076b9443SCy Schubert pDb->nStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_FULLSCAN_STEP,1); 1613*076b9443SCy Schubert pDb->nSort = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_SORT,1); 1614*076b9443SCy Schubert pDb->nIndex = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_AUTOINDEX,1); 1615*076b9443SCy Schubert pDb->nVMStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_VM_STEP,1); 1616*076b9443SCy Schubert dbReleaseColumnNames(p); 1617*076b9443SCy Schubert p->pPreStmt = 0; 1618*076b9443SCy Schubert 1619*076b9443SCy Schubert if( rcs!=SQLITE_OK ){ 1620*076b9443SCy Schubert /* If a run-time error occurs, report the error and stop reading 1621*076b9443SCy Schubert ** the SQL. */ 1622*076b9443SCy Schubert dbReleaseStmt(pDb, pPreStmt, 1); 1623*076b9443SCy Schubert #if SQLITE_TEST 1624*076b9443SCy Schubert if( p->pDb->bLegacyPrepare && rcs==SQLITE_SCHEMA && zPrevSql ){ 1625*076b9443SCy Schubert /* If the runtime error was an SQLITE_SCHEMA, and the database 1626*076b9443SCy Schubert ** handle is configured to use the legacy sqlite3_prepare() 1627*076b9443SCy Schubert ** interface, retry prepare()/step() on the same SQL statement. 1628*076b9443SCy Schubert ** This only happens once. If there is a second SQLITE_SCHEMA 1629*076b9443SCy Schubert ** error, the error will be returned to the caller. */ 1630*076b9443SCy Schubert p->zSql = zPrevSql; 1631*076b9443SCy Schubert continue; 1632*076b9443SCy Schubert } 1633*076b9443SCy Schubert #endif 1634*076b9443SCy Schubert Tcl_SetObjResult(pDb->interp, 1635*076b9443SCy Schubert Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 1636*076b9443SCy Schubert return TCL_ERROR; 1637*076b9443SCy Schubert }else{ 1638*076b9443SCy Schubert dbReleaseStmt(pDb, pPreStmt, 0); 1639*076b9443SCy Schubert } 1640*076b9443SCy Schubert } 1641*076b9443SCy Schubert } 1642*076b9443SCy Schubert 1643*076b9443SCy Schubert /* Finished */ 1644*076b9443SCy Schubert return TCL_BREAK; 1645*076b9443SCy Schubert } 1646*076b9443SCy Schubert 1647*076b9443SCy Schubert /* 1648*076b9443SCy Schubert ** Free all resources currently held by the DbEvalContext structure passed 1649*076b9443SCy Schubert ** as the first argument. There should be exactly one call to this function 1650*076b9443SCy Schubert ** for each call to dbEvalInit(). 1651*076b9443SCy Schubert */ 1652*076b9443SCy Schubert static void dbEvalFinalize(DbEvalContext *p){ 1653*076b9443SCy Schubert if( p->pPreStmt ){ 1654*076b9443SCy Schubert sqlite3_reset(p->pPreStmt->pStmt); 1655*076b9443SCy Schubert dbReleaseStmt(p->pDb, p->pPreStmt, 0); 1656*076b9443SCy Schubert p->pPreStmt = 0; 1657*076b9443SCy Schubert } 1658*076b9443SCy Schubert if( p->pArray ){ 1659*076b9443SCy Schubert Tcl_DecrRefCount(p->pArray); 1660*076b9443SCy Schubert p->pArray = 0; 1661*076b9443SCy Schubert } 1662*076b9443SCy Schubert Tcl_DecrRefCount(p->pSql); 1663*076b9443SCy Schubert dbReleaseColumnNames(p); 1664*076b9443SCy Schubert } 1665*076b9443SCy Schubert 1666*076b9443SCy Schubert /* 1667*076b9443SCy Schubert ** Return a pointer to a Tcl_Obj structure with ref-count 0 that contains 1668*076b9443SCy Schubert ** the value for the iCol'th column of the row currently pointed to by 1669*076b9443SCy Schubert ** the DbEvalContext structure passed as the first argument. 1670*076b9443SCy Schubert */ 1671*076b9443SCy Schubert static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){ 1672*076b9443SCy Schubert sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 1673*076b9443SCy Schubert switch( sqlite3_column_type(pStmt, iCol) ){ 1674*076b9443SCy Schubert case SQLITE_BLOB: { 1675*076b9443SCy Schubert int bytes = sqlite3_column_bytes(pStmt, iCol); 1676*076b9443SCy Schubert const char *zBlob = sqlite3_column_blob(pStmt, iCol); 1677*076b9443SCy Schubert if( !zBlob ) bytes = 0; 1678*076b9443SCy Schubert return Tcl_NewByteArrayObj((u8*)zBlob, bytes); 1679*076b9443SCy Schubert } 1680*076b9443SCy Schubert case SQLITE_INTEGER: { 1681*076b9443SCy Schubert sqlite_int64 v = sqlite3_column_int64(pStmt, iCol); 1682*076b9443SCy Schubert if( v>=-2147483647 && v<=2147483647 ){ 1683*076b9443SCy Schubert return Tcl_NewIntObj((int)v); 1684*076b9443SCy Schubert }else{ 1685*076b9443SCy Schubert return Tcl_NewWideIntObj(v); 1686*076b9443SCy Schubert } 1687*076b9443SCy Schubert } 1688*076b9443SCy Schubert case SQLITE_FLOAT: { 1689*076b9443SCy Schubert return Tcl_NewDoubleObj(sqlite3_column_double(pStmt, iCol)); 1690*076b9443SCy Schubert } 1691*076b9443SCy Schubert case SQLITE_NULL: { 1692*076b9443SCy Schubert return Tcl_NewStringObj(p->pDb->zNull, -1); 1693*076b9443SCy Schubert } 1694*076b9443SCy Schubert } 1695*076b9443SCy Schubert 1696*076b9443SCy Schubert return Tcl_NewStringObj((char*)sqlite3_column_text(pStmt, iCol), -1); 1697*076b9443SCy Schubert } 1698*076b9443SCy Schubert 1699*076b9443SCy Schubert /* 1700*076b9443SCy Schubert ** If using Tcl version 8.6 or greater, use the NR functions to avoid 1701*076b9443SCy Schubert ** recursive evalution of scripts by the [db eval] and [db trans] 1702*076b9443SCy Schubert ** commands. Even if the headers used while compiling the extension 1703*076b9443SCy Schubert ** are 8.6 or newer, the code still tests the Tcl version at runtime. 1704*076b9443SCy Schubert ** This allows stubs-enabled builds to be used with older Tcl libraries. 1705*076b9443SCy Schubert */ 1706*076b9443SCy Schubert #if TCL_MAJOR_VERSION>8 || (TCL_MAJOR_VERSION==8 && TCL_MINOR_VERSION>=6) 1707*076b9443SCy Schubert # define SQLITE_TCL_NRE 1 1708*076b9443SCy Schubert static int DbUseNre(void){ 1709*076b9443SCy Schubert int major, minor; 1710*076b9443SCy Schubert Tcl_GetVersion(&major, &minor, 0, 0); 1711*076b9443SCy Schubert return( (major==8 && minor>=6) || major>8 ); 1712*076b9443SCy Schubert } 1713*076b9443SCy Schubert #else 1714*076b9443SCy Schubert /* 1715*076b9443SCy Schubert ** Compiling using headers earlier than 8.6. In this case NR cannot be 1716*076b9443SCy Schubert ** used, so DbUseNre() to always return zero. Add #defines for the other 1717*076b9443SCy Schubert ** Tcl_NRxxx() functions to prevent them from causing compilation errors, 1718*076b9443SCy Schubert ** even though the only invocations of them are within conditional blocks 1719*076b9443SCy Schubert ** of the form: 1720*076b9443SCy Schubert ** 1721*076b9443SCy Schubert ** if( DbUseNre() ) { ... } 1722*076b9443SCy Schubert */ 1723*076b9443SCy Schubert # define SQLITE_TCL_NRE 0 1724*076b9443SCy Schubert # define DbUseNre() 0 1725*076b9443SCy Schubert # define Tcl_NRAddCallback(a,b,c,d,e,f) (void)0 1726*076b9443SCy Schubert # define Tcl_NREvalObj(a,b,c) 0 1727*076b9443SCy Schubert # define Tcl_NRCreateCommand(a,b,c,d,e,f) (void)0 1728*076b9443SCy Schubert #endif 1729*076b9443SCy Schubert 1730*076b9443SCy Schubert /* 1731*076b9443SCy Schubert ** This function is part of the implementation of the command: 1732*076b9443SCy Schubert ** 1733*076b9443SCy Schubert ** $db eval SQL ?ARRAYNAME? SCRIPT 1734*076b9443SCy Schubert */ 1735*076b9443SCy Schubert static int SQLITE_TCLAPI DbEvalNextCmd( 1736*076b9443SCy Schubert ClientData data[], /* data[0] is the (DbEvalContext*) */ 1737*076b9443SCy Schubert Tcl_Interp *interp, /* Tcl interpreter */ 1738*076b9443SCy Schubert int result /* Result so far */ 1739*076b9443SCy Schubert ){ 1740*076b9443SCy Schubert int rc = result; /* Return code */ 1741*076b9443SCy Schubert 1742*076b9443SCy Schubert /* The first element of the data[] array is a pointer to a DbEvalContext 1743*076b9443SCy Schubert ** structure allocated using Tcl_Alloc(). The second element of data[] 1744*076b9443SCy Schubert ** is a pointer to a Tcl_Obj containing the script to run for each row 1745*076b9443SCy Schubert ** returned by the queries encapsulated in data[0]. */ 1746*076b9443SCy Schubert DbEvalContext *p = (DbEvalContext *)data[0]; 1747*076b9443SCy Schubert Tcl_Obj *pScript = (Tcl_Obj *)data[1]; 1748*076b9443SCy Schubert Tcl_Obj *pArray = p->pArray; 1749*076b9443SCy Schubert 1750*076b9443SCy Schubert while( (rc==TCL_OK || rc==TCL_CONTINUE) && TCL_OK==(rc = dbEvalStep(p)) ){ 1751*076b9443SCy Schubert int i; 1752*076b9443SCy Schubert int nCol; 1753*076b9443SCy Schubert Tcl_Obj **apColName; 1754*076b9443SCy Schubert dbEvalRowInfo(p, &nCol, &apColName); 1755*076b9443SCy Schubert for(i=0; i<nCol; i++){ 1756*076b9443SCy Schubert if( pArray==0 ){ 1757*076b9443SCy Schubert Tcl_ObjSetVar2(interp, apColName[i], 0, dbEvalColumnValue(p,i), 0); 1758*076b9443SCy Schubert }else if( (p->evalFlags & SQLITE_EVAL_WITHOUTNULLS)!=0 1759*076b9443SCy Schubert && sqlite3_column_type(p->pPreStmt->pStmt, i)==SQLITE_NULL 1760*076b9443SCy Schubert ){ 1761*076b9443SCy Schubert Tcl_UnsetVar2(interp, Tcl_GetString(pArray), 1762*076b9443SCy Schubert Tcl_GetString(apColName[i]), 0); 1763*076b9443SCy Schubert }else{ 1764*076b9443SCy Schubert Tcl_ObjSetVar2(interp, pArray, apColName[i], dbEvalColumnValue(p,i), 0); 1765*076b9443SCy Schubert } 1766*076b9443SCy Schubert } 1767*076b9443SCy Schubert 1768*076b9443SCy Schubert /* The required interpreter variables are now populated with the data 1769*076b9443SCy Schubert ** from the current row. If using NRE, schedule callbacks to evaluate 1770*076b9443SCy Schubert ** script pScript, then to invoke this function again to fetch the next 1771*076b9443SCy Schubert ** row (or clean up if there is no next row or the script throws an 1772*076b9443SCy Schubert ** exception). After scheduling the callbacks, return control to the 1773*076b9443SCy Schubert ** caller. 1774*076b9443SCy Schubert ** 1775*076b9443SCy Schubert ** If not using NRE, evaluate pScript directly and continue with the 1776*076b9443SCy Schubert ** next iteration of this while(...) loop. */ 1777*076b9443SCy Schubert if( DbUseNre() ){ 1778*076b9443SCy Schubert Tcl_NRAddCallback(interp, DbEvalNextCmd, (void*)p, (void*)pScript, 0, 0); 1779*076b9443SCy Schubert return Tcl_NREvalObj(interp, pScript, 0); 1780*076b9443SCy Schubert }else{ 1781*076b9443SCy Schubert rc = Tcl_EvalObjEx(interp, pScript, 0); 1782*076b9443SCy Schubert } 1783*076b9443SCy Schubert } 1784*076b9443SCy Schubert 1785*076b9443SCy Schubert Tcl_DecrRefCount(pScript); 1786*076b9443SCy Schubert dbEvalFinalize(p); 1787*076b9443SCy Schubert Tcl_Free((char *)p); 1788*076b9443SCy Schubert 1789*076b9443SCy Schubert if( rc==TCL_OK || rc==TCL_BREAK ){ 1790*076b9443SCy Schubert Tcl_ResetResult(interp); 1791*076b9443SCy Schubert rc = TCL_OK; 1792*076b9443SCy Schubert } 1793*076b9443SCy Schubert return rc; 1794*076b9443SCy Schubert } 1795*076b9443SCy Schubert 1796*076b9443SCy Schubert /* 1797*076b9443SCy Schubert ** This function is used by the implementations of the following database 1798*076b9443SCy Schubert ** handle sub-commands: 1799*076b9443SCy Schubert ** 1800*076b9443SCy Schubert ** $db update_hook ?SCRIPT? 1801*076b9443SCy Schubert ** $db wal_hook ?SCRIPT? 1802*076b9443SCy Schubert ** $db commit_hook ?SCRIPT? 1803*076b9443SCy Schubert ** $db preupdate hook ?SCRIPT? 1804*076b9443SCy Schubert */ 1805*076b9443SCy Schubert static void DbHookCmd( 1806*076b9443SCy Schubert Tcl_Interp *interp, /* Tcl interpreter */ 1807*076b9443SCy Schubert SqliteDb *pDb, /* Database handle */ 1808*076b9443SCy Schubert Tcl_Obj *pArg, /* SCRIPT argument (or NULL) */ 1809*076b9443SCy Schubert Tcl_Obj **ppHook /* Pointer to member of SqliteDb */ 1810*076b9443SCy Schubert ){ 1811*076b9443SCy Schubert sqlite3 *db = pDb->db; 1812*076b9443SCy Schubert 1813*076b9443SCy Schubert if( *ppHook ){ 1814*076b9443SCy Schubert Tcl_SetObjResult(interp, *ppHook); 1815*076b9443SCy Schubert if( pArg ){ 1816*076b9443SCy Schubert Tcl_DecrRefCount(*ppHook); 1817*076b9443SCy Schubert *ppHook = 0; 1818*076b9443SCy Schubert } 1819*076b9443SCy Schubert } 1820*076b9443SCy Schubert if( pArg ){ 1821*076b9443SCy Schubert assert( !(*ppHook) ); 1822*076b9443SCy Schubert if( Tcl_GetCharLength(pArg)>0 ){ 1823*076b9443SCy Schubert *ppHook = pArg; 1824*076b9443SCy Schubert Tcl_IncrRefCount(*ppHook); 1825*076b9443SCy Schubert } 1826*076b9443SCy Schubert } 1827*076b9443SCy Schubert 1828*076b9443SCy Schubert #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 1829*076b9443SCy Schubert sqlite3_preupdate_hook(db, (pDb->pPreUpdateHook?DbPreUpdateHandler:0), pDb); 1830*076b9443SCy Schubert #endif 1831*076b9443SCy Schubert sqlite3_update_hook(db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); 1832*076b9443SCy Schubert sqlite3_rollback_hook(db, (pDb->pRollbackHook?DbRollbackHandler:0), pDb); 1833*076b9443SCy Schubert sqlite3_wal_hook(db, (pDb->pWalHook?DbWalHandler:0), pDb); 1834*076b9443SCy Schubert } 1835*076b9443SCy Schubert 1836*076b9443SCy Schubert /* 1837*076b9443SCy Schubert ** The "sqlite" command below creates a new Tcl command for each 1838*076b9443SCy Schubert ** connection it opens to an SQLite database. This routine is invoked 1839*076b9443SCy Schubert ** whenever one of those connection-specific commands is executed 1840*076b9443SCy Schubert ** in Tcl. For example, if you run Tcl code like this: 1841*076b9443SCy Schubert ** 1842*076b9443SCy Schubert ** sqlite3 db1 "my_database" 1843*076b9443SCy Schubert ** db1 close 1844*076b9443SCy Schubert ** 1845*076b9443SCy Schubert ** The first command opens a connection to the "my_database" database 1846*076b9443SCy Schubert ** and calls that connection "db1". The second command causes this 1847*076b9443SCy Schubert ** subroutine to be invoked. 1848*076b9443SCy Schubert */ 1849*076b9443SCy Schubert static int SQLITE_TCLAPI DbObjCmd( 1850*076b9443SCy Schubert void *cd, 1851*076b9443SCy Schubert Tcl_Interp *interp, 1852*076b9443SCy Schubert int objc, 1853*076b9443SCy Schubert Tcl_Obj *const*objv 1854*076b9443SCy Schubert ){ 1855*076b9443SCy Schubert SqliteDb *pDb = (SqliteDb*)cd; 1856*076b9443SCy Schubert int choice; 1857*076b9443SCy Schubert int rc = TCL_OK; 1858*076b9443SCy Schubert static const char *DB_strs[] = { 1859*076b9443SCy Schubert "authorizer", "backup", "busy", 1860*076b9443SCy Schubert "cache", "changes", "close", 1861*076b9443SCy Schubert "collate", "collation_needed", "commit_hook", 1862*076b9443SCy Schubert "complete", "copy", "deserialize", 1863*076b9443SCy Schubert "enable_load_extension", "errorcode", "eval", 1864*076b9443SCy Schubert "exists", "function", "incrblob", 1865*076b9443SCy Schubert "interrupt", "last_insert_rowid", "nullvalue", 1866*076b9443SCy Schubert "onecolumn", "preupdate", "profile", 1867*076b9443SCy Schubert "progress", "rekey", "restore", 1868*076b9443SCy Schubert "rollback_hook", "serialize", "status", 1869*076b9443SCy Schubert "timeout", "total_changes", "trace", 1870*076b9443SCy Schubert "trace_v2", "transaction", "unlock_notify", 1871*076b9443SCy Schubert "update_hook", "version", "wal_hook", 1872*076b9443SCy Schubert 0 1873*076b9443SCy Schubert }; 1874*076b9443SCy Schubert enum DB_enum { 1875*076b9443SCy Schubert DB_AUTHORIZER, DB_BACKUP, DB_BUSY, 1876*076b9443SCy Schubert DB_CACHE, DB_CHANGES, DB_CLOSE, 1877*076b9443SCy Schubert DB_COLLATE, DB_COLLATION_NEEDED, DB_COMMIT_HOOK, 1878*076b9443SCy Schubert DB_COMPLETE, DB_COPY, DB_DESERIALIZE, 1879*076b9443SCy Schubert DB_ENABLE_LOAD_EXTENSION, DB_ERRORCODE, DB_EVAL, 1880*076b9443SCy Schubert DB_EXISTS, DB_FUNCTION, DB_INCRBLOB, 1881*076b9443SCy Schubert DB_INTERRUPT, DB_LAST_INSERT_ROWID, DB_NULLVALUE, 1882*076b9443SCy Schubert DB_ONECOLUMN, DB_PREUPDATE, DB_PROFILE, 1883*076b9443SCy Schubert DB_PROGRESS, DB_REKEY, DB_RESTORE, 1884*076b9443SCy Schubert DB_ROLLBACK_HOOK, DB_SERIALIZE, DB_STATUS, 1885*076b9443SCy Schubert DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE, 1886*076b9443SCy Schubert DB_TRACE_V2, DB_TRANSACTION, DB_UNLOCK_NOTIFY, 1887*076b9443SCy Schubert DB_UPDATE_HOOK, DB_VERSION, DB_WAL_HOOK 1888*076b9443SCy Schubert }; 1889*076b9443SCy Schubert /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */ 1890*076b9443SCy Schubert 1891*076b9443SCy Schubert if( objc<2 ){ 1892*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); 1893*076b9443SCy Schubert return TCL_ERROR; 1894*076b9443SCy Schubert } 1895*076b9443SCy Schubert if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ 1896*076b9443SCy Schubert return TCL_ERROR; 1897*076b9443SCy Schubert } 1898*076b9443SCy Schubert 1899*076b9443SCy Schubert switch( (enum DB_enum)choice ){ 1900*076b9443SCy Schubert 1901*076b9443SCy Schubert /* $db authorizer ?CALLBACK? 1902*076b9443SCy Schubert ** 1903*076b9443SCy Schubert ** Invoke the given callback to authorize each SQL operation as it is 1904*076b9443SCy Schubert ** compiled. 5 arguments are appended to the callback before it is 1905*076b9443SCy Schubert ** invoked: 1906*076b9443SCy Schubert ** 1907*076b9443SCy Schubert ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) 1908*076b9443SCy Schubert ** (2) First descriptive name (depends on authorization type) 1909*076b9443SCy Schubert ** (3) Second descriptive name 1910*076b9443SCy Schubert ** (4) Name of the database (ex: "main", "temp") 1911*076b9443SCy Schubert ** (5) Name of trigger that is doing the access 1912*076b9443SCy Schubert ** 1913*076b9443SCy Schubert ** The callback should return on of the following strings: SQLITE_OK, 1914*076b9443SCy Schubert ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. 1915*076b9443SCy Schubert ** 1916*076b9443SCy Schubert ** If this method is invoked with no arguments, the current authorization 1917*076b9443SCy Schubert ** callback string is returned. 1918*076b9443SCy Schubert */ 1919*076b9443SCy Schubert case DB_AUTHORIZER: { 1920*076b9443SCy Schubert #ifdef SQLITE_OMIT_AUTHORIZATION 1921*076b9443SCy Schubert Tcl_AppendResult(interp, "authorization not available in this build", 1922*076b9443SCy Schubert (char*)0); 1923*076b9443SCy Schubert return TCL_ERROR; 1924*076b9443SCy Schubert #else 1925*076b9443SCy Schubert if( objc>3 ){ 1926*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 1927*076b9443SCy Schubert return TCL_ERROR; 1928*076b9443SCy Schubert }else if( objc==2 ){ 1929*076b9443SCy Schubert if( pDb->zAuth ){ 1930*076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zAuth, (char*)0); 1931*076b9443SCy Schubert } 1932*076b9443SCy Schubert }else{ 1933*076b9443SCy Schubert char *zAuth; 1934*076b9443SCy Schubert int len; 1935*076b9443SCy Schubert if( pDb->zAuth ){ 1936*076b9443SCy Schubert Tcl_Free(pDb->zAuth); 1937*076b9443SCy Schubert } 1938*076b9443SCy Schubert zAuth = Tcl_GetStringFromObj(objv[2], &len); 1939*076b9443SCy Schubert if( zAuth && len>0 ){ 1940*076b9443SCy Schubert pDb->zAuth = Tcl_Alloc( len + 1 ); 1941*076b9443SCy Schubert memcpy(pDb->zAuth, zAuth, len+1); 1942*076b9443SCy Schubert }else{ 1943*076b9443SCy Schubert pDb->zAuth = 0; 1944*076b9443SCy Schubert } 1945*076b9443SCy Schubert if( pDb->zAuth ){ 1946*076b9443SCy Schubert typedef int (*sqlite3_auth_cb)( 1947*076b9443SCy Schubert void*,int,const char*,const char*, 1948*076b9443SCy Schubert const char*,const char*); 1949*076b9443SCy Schubert pDb->interp = interp; 1950*076b9443SCy Schubert sqlite3_set_authorizer(pDb->db,(sqlite3_auth_cb)auth_callback,pDb); 1951*076b9443SCy Schubert }else{ 1952*076b9443SCy Schubert sqlite3_set_authorizer(pDb->db, 0, 0); 1953*076b9443SCy Schubert } 1954*076b9443SCy Schubert } 1955*076b9443SCy Schubert #endif 1956*076b9443SCy Schubert break; 1957*076b9443SCy Schubert } 1958*076b9443SCy Schubert 1959*076b9443SCy Schubert /* $db backup ?DATABASE? FILENAME 1960*076b9443SCy Schubert ** 1961*076b9443SCy Schubert ** Open or create a database file named FILENAME. Transfer the 1962*076b9443SCy Schubert ** content of local database DATABASE (default: "main") into the 1963*076b9443SCy Schubert ** FILENAME database. 1964*076b9443SCy Schubert */ 1965*076b9443SCy Schubert case DB_BACKUP: { 1966*076b9443SCy Schubert const char *zDestFile; 1967*076b9443SCy Schubert const char *zSrcDb; 1968*076b9443SCy Schubert sqlite3 *pDest; 1969*076b9443SCy Schubert sqlite3_backup *pBackup; 1970*076b9443SCy Schubert 1971*076b9443SCy Schubert if( objc==3 ){ 1972*076b9443SCy Schubert zSrcDb = "main"; 1973*076b9443SCy Schubert zDestFile = Tcl_GetString(objv[2]); 1974*076b9443SCy Schubert }else if( objc==4 ){ 1975*076b9443SCy Schubert zSrcDb = Tcl_GetString(objv[2]); 1976*076b9443SCy Schubert zDestFile = Tcl_GetString(objv[3]); 1977*076b9443SCy Schubert }else{ 1978*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 1979*076b9443SCy Schubert return TCL_ERROR; 1980*076b9443SCy Schubert } 1981*076b9443SCy Schubert rc = sqlite3_open_v2(zDestFile, &pDest, 1982*076b9443SCy Schubert SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE| pDb->openFlags, 0); 1983*076b9443SCy Schubert if( rc!=SQLITE_OK ){ 1984*076b9443SCy Schubert Tcl_AppendResult(interp, "cannot open target database: ", 1985*076b9443SCy Schubert sqlite3_errmsg(pDest), (char*)0); 1986*076b9443SCy Schubert sqlite3_close(pDest); 1987*076b9443SCy Schubert return TCL_ERROR; 1988*076b9443SCy Schubert } 1989*076b9443SCy Schubert pBackup = sqlite3_backup_init(pDest, "main", pDb->db, zSrcDb); 1990*076b9443SCy Schubert if( pBackup==0 ){ 1991*076b9443SCy Schubert Tcl_AppendResult(interp, "backup failed: ", 1992*076b9443SCy Schubert sqlite3_errmsg(pDest), (char*)0); 1993*076b9443SCy Schubert sqlite3_close(pDest); 1994*076b9443SCy Schubert return TCL_ERROR; 1995*076b9443SCy Schubert } 1996*076b9443SCy Schubert while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} 1997*076b9443SCy Schubert sqlite3_backup_finish(pBackup); 1998*076b9443SCy Schubert if( rc==SQLITE_DONE ){ 1999*076b9443SCy Schubert rc = TCL_OK; 2000*076b9443SCy Schubert }else{ 2001*076b9443SCy Schubert Tcl_AppendResult(interp, "backup failed: ", 2002*076b9443SCy Schubert sqlite3_errmsg(pDest), (char*)0); 2003*076b9443SCy Schubert rc = TCL_ERROR; 2004*076b9443SCy Schubert } 2005*076b9443SCy Schubert sqlite3_close(pDest); 2006*076b9443SCy Schubert break; 2007*076b9443SCy Schubert } 2008*076b9443SCy Schubert 2009*076b9443SCy Schubert /* $db busy ?CALLBACK? 2010*076b9443SCy Schubert ** 2011*076b9443SCy Schubert ** Invoke the given callback if an SQL statement attempts to open 2012*076b9443SCy Schubert ** a locked database file. 2013*076b9443SCy Schubert */ 2014*076b9443SCy Schubert case DB_BUSY: { 2015*076b9443SCy Schubert if( objc>3 ){ 2016*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); 2017*076b9443SCy Schubert return TCL_ERROR; 2018*076b9443SCy Schubert }else if( objc==2 ){ 2019*076b9443SCy Schubert if( pDb->zBusy ){ 2020*076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zBusy, (char*)0); 2021*076b9443SCy Schubert } 2022*076b9443SCy Schubert }else{ 2023*076b9443SCy Schubert char *zBusy; 2024*076b9443SCy Schubert int len; 2025*076b9443SCy Schubert if( pDb->zBusy ){ 2026*076b9443SCy Schubert Tcl_Free(pDb->zBusy); 2027*076b9443SCy Schubert } 2028*076b9443SCy Schubert zBusy = Tcl_GetStringFromObj(objv[2], &len); 2029*076b9443SCy Schubert if( zBusy && len>0 ){ 2030*076b9443SCy Schubert pDb->zBusy = Tcl_Alloc( len + 1 ); 2031*076b9443SCy Schubert memcpy(pDb->zBusy, zBusy, len+1); 2032*076b9443SCy Schubert }else{ 2033*076b9443SCy Schubert pDb->zBusy = 0; 2034*076b9443SCy Schubert } 2035*076b9443SCy Schubert if( pDb->zBusy ){ 2036*076b9443SCy Schubert pDb->interp = interp; 2037*076b9443SCy Schubert sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb); 2038*076b9443SCy Schubert }else{ 2039*076b9443SCy Schubert sqlite3_busy_handler(pDb->db, 0, 0); 2040*076b9443SCy Schubert } 2041*076b9443SCy Schubert } 2042*076b9443SCy Schubert break; 2043*076b9443SCy Schubert } 2044*076b9443SCy Schubert 2045*076b9443SCy Schubert /* $db cache flush 2046*076b9443SCy Schubert ** $db cache size n 2047*076b9443SCy Schubert ** 2048*076b9443SCy Schubert ** Flush the prepared statement cache, or set the maximum number of 2049*076b9443SCy Schubert ** cached statements. 2050*076b9443SCy Schubert */ 2051*076b9443SCy Schubert case DB_CACHE: { 2052*076b9443SCy Schubert char *subCmd; 2053*076b9443SCy Schubert int n; 2054*076b9443SCy Schubert 2055*076b9443SCy Schubert if( objc<=2 ){ 2056*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?"); 2057*076b9443SCy Schubert return TCL_ERROR; 2058*076b9443SCy Schubert } 2059*076b9443SCy Schubert subCmd = Tcl_GetStringFromObj( objv[2], 0 ); 2060*076b9443SCy Schubert if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){ 2061*076b9443SCy Schubert if( objc!=3 ){ 2062*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "flush"); 2063*076b9443SCy Schubert return TCL_ERROR; 2064*076b9443SCy Schubert }else{ 2065*076b9443SCy Schubert flushStmtCache( pDb ); 2066*076b9443SCy Schubert } 2067*076b9443SCy Schubert }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){ 2068*076b9443SCy Schubert if( objc!=4 ){ 2069*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "size n"); 2070*076b9443SCy Schubert return TCL_ERROR; 2071*076b9443SCy Schubert }else{ 2072*076b9443SCy Schubert if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){ 2073*076b9443SCy Schubert Tcl_AppendResult( interp, "cannot convert \"", 2074*076b9443SCy Schubert Tcl_GetStringFromObj(objv[3],0), "\" to integer", (char*)0); 2075*076b9443SCy Schubert return TCL_ERROR; 2076*076b9443SCy Schubert }else{ 2077*076b9443SCy Schubert if( n<0 ){ 2078*076b9443SCy Schubert flushStmtCache( pDb ); 2079*076b9443SCy Schubert n = 0; 2080*076b9443SCy Schubert }else if( n>MAX_PREPARED_STMTS ){ 2081*076b9443SCy Schubert n = MAX_PREPARED_STMTS; 2082*076b9443SCy Schubert } 2083*076b9443SCy Schubert pDb->maxStmt = n; 2084*076b9443SCy Schubert } 2085*076b9443SCy Schubert } 2086*076b9443SCy Schubert }else{ 2087*076b9443SCy Schubert Tcl_AppendResult( interp, "bad option \"", 2088*076b9443SCy Schubert Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size", 2089*076b9443SCy Schubert (char*)0); 2090*076b9443SCy Schubert return TCL_ERROR; 2091*076b9443SCy Schubert } 2092*076b9443SCy Schubert break; 2093*076b9443SCy Schubert } 2094*076b9443SCy Schubert 2095*076b9443SCy Schubert /* $db changes 2096*076b9443SCy Schubert ** 2097*076b9443SCy Schubert ** Return the number of rows that were modified, inserted, or deleted by 2098*076b9443SCy Schubert ** the most recent INSERT, UPDATE or DELETE statement, not including 2099*076b9443SCy Schubert ** any changes made by trigger programs. 2100*076b9443SCy Schubert */ 2101*076b9443SCy Schubert case DB_CHANGES: { 2102*076b9443SCy Schubert Tcl_Obj *pResult; 2103*076b9443SCy Schubert if( objc!=2 ){ 2104*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, ""); 2105*076b9443SCy Schubert return TCL_ERROR; 2106*076b9443SCy Schubert } 2107*076b9443SCy Schubert pResult = Tcl_GetObjResult(interp); 2108*076b9443SCy Schubert Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db)); 2109*076b9443SCy Schubert break; 2110*076b9443SCy Schubert } 2111*076b9443SCy Schubert 2112*076b9443SCy Schubert /* $db close 2113*076b9443SCy Schubert ** 2114*076b9443SCy Schubert ** Shutdown the database 2115*076b9443SCy Schubert */ 2116*076b9443SCy Schubert case DB_CLOSE: { 2117*076b9443SCy Schubert Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); 2118*076b9443SCy Schubert break; 2119*076b9443SCy Schubert } 2120*076b9443SCy Schubert 2121*076b9443SCy Schubert /* 2122*076b9443SCy Schubert ** $db collate NAME SCRIPT 2123*076b9443SCy Schubert ** 2124*076b9443SCy Schubert ** Create a new SQL collation function called NAME. Whenever 2125*076b9443SCy Schubert ** that function is called, invoke SCRIPT to evaluate the function. 2126*076b9443SCy Schubert */ 2127*076b9443SCy Schubert case DB_COLLATE: { 2128*076b9443SCy Schubert SqlCollate *pCollate; 2129*076b9443SCy Schubert char *zName; 2130*076b9443SCy Schubert char *zScript; 2131*076b9443SCy Schubert int nScript; 2132*076b9443SCy Schubert if( objc!=4 ){ 2133*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); 2134*076b9443SCy Schubert return TCL_ERROR; 2135*076b9443SCy Schubert } 2136*076b9443SCy Schubert zName = Tcl_GetStringFromObj(objv[2], 0); 2137*076b9443SCy Schubert zScript = Tcl_GetStringFromObj(objv[3], &nScript); 2138*076b9443SCy Schubert pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 ); 2139*076b9443SCy Schubert if( pCollate==0 ) return TCL_ERROR; 2140*076b9443SCy Schubert pCollate->interp = interp; 2141*076b9443SCy Schubert pCollate->pNext = pDb->pCollate; 2142*076b9443SCy Schubert pCollate->zScript = (char*)&pCollate[1]; 2143*076b9443SCy Schubert pDb->pCollate = pCollate; 2144*076b9443SCy Schubert memcpy(pCollate->zScript, zScript, nScript+1); 2145*076b9443SCy Schubert if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, 2146*076b9443SCy Schubert pCollate, tclSqlCollate) ){ 2147*076b9443SCy Schubert Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 2148*076b9443SCy Schubert return TCL_ERROR; 2149*076b9443SCy Schubert } 2150*076b9443SCy Schubert break; 2151*076b9443SCy Schubert } 2152*076b9443SCy Schubert 2153*076b9443SCy Schubert /* 2154*076b9443SCy Schubert ** $db collation_needed SCRIPT 2155*076b9443SCy Schubert ** 2156*076b9443SCy Schubert ** Create a new SQL collation function called NAME. Whenever 2157*076b9443SCy Schubert ** that function is called, invoke SCRIPT to evaluate the function. 2158*076b9443SCy Schubert */ 2159*076b9443SCy Schubert case DB_COLLATION_NEEDED: { 2160*076b9443SCy Schubert if( objc!=3 ){ 2161*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT"); 2162*076b9443SCy Schubert return TCL_ERROR; 2163*076b9443SCy Schubert } 2164*076b9443SCy Schubert if( pDb->pCollateNeeded ){ 2165*076b9443SCy Schubert Tcl_DecrRefCount(pDb->pCollateNeeded); 2166*076b9443SCy Schubert } 2167*076b9443SCy Schubert pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]); 2168*076b9443SCy Schubert Tcl_IncrRefCount(pDb->pCollateNeeded); 2169*076b9443SCy Schubert sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded); 2170*076b9443SCy Schubert break; 2171*076b9443SCy Schubert } 2172*076b9443SCy Schubert 2173*076b9443SCy Schubert /* $db commit_hook ?CALLBACK? 2174*076b9443SCy Schubert ** 2175*076b9443SCy Schubert ** Invoke the given callback just before committing every SQL transaction. 2176*076b9443SCy Schubert ** If the callback throws an exception or returns non-zero, then the 2177*076b9443SCy Schubert ** transaction is aborted. If CALLBACK is an empty string, the callback 2178*076b9443SCy Schubert ** is disabled. 2179*076b9443SCy Schubert */ 2180*076b9443SCy Schubert case DB_COMMIT_HOOK: { 2181*076b9443SCy Schubert if( objc>3 ){ 2182*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 2183*076b9443SCy Schubert return TCL_ERROR; 2184*076b9443SCy Schubert }else if( objc==2 ){ 2185*076b9443SCy Schubert if( pDb->zCommit ){ 2186*076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zCommit, (char*)0); 2187*076b9443SCy Schubert } 2188*076b9443SCy Schubert }else{ 2189*076b9443SCy Schubert const char *zCommit; 2190*076b9443SCy Schubert int len; 2191*076b9443SCy Schubert if( pDb->zCommit ){ 2192*076b9443SCy Schubert Tcl_Free(pDb->zCommit); 2193*076b9443SCy Schubert } 2194*076b9443SCy Schubert zCommit = Tcl_GetStringFromObj(objv[2], &len); 2195*076b9443SCy Schubert if( zCommit && len>0 ){ 2196*076b9443SCy Schubert pDb->zCommit = Tcl_Alloc( len + 1 ); 2197*076b9443SCy Schubert memcpy(pDb->zCommit, zCommit, len+1); 2198*076b9443SCy Schubert }else{ 2199*076b9443SCy Schubert pDb->zCommit = 0; 2200*076b9443SCy Schubert } 2201*076b9443SCy Schubert if( pDb->zCommit ){ 2202*076b9443SCy Schubert pDb->interp = interp; 2203*076b9443SCy Schubert sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb); 2204*076b9443SCy Schubert }else{ 2205*076b9443SCy Schubert sqlite3_commit_hook(pDb->db, 0, 0); 2206*076b9443SCy Schubert } 2207*076b9443SCy Schubert } 2208*076b9443SCy Schubert break; 2209*076b9443SCy Schubert } 2210*076b9443SCy Schubert 2211*076b9443SCy Schubert /* $db complete SQL 2212*076b9443SCy Schubert ** 2213*076b9443SCy Schubert ** Return TRUE if SQL is a complete SQL statement. Return FALSE if 2214*076b9443SCy Schubert ** additional lines of input are needed. This is similar to the 2215*076b9443SCy Schubert ** built-in "info complete" command of Tcl. 2216*076b9443SCy Schubert */ 2217*076b9443SCy Schubert case DB_COMPLETE: { 2218*076b9443SCy Schubert #ifndef SQLITE_OMIT_COMPLETE 2219*076b9443SCy Schubert Tcl_Obj *pResult; 2220*076b9443SCy Schubert int isComplete; 2221*076b9443SCy Schubert if( objc!=3 ){ 2222*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 2223*076b9443SCy Schubert return TCL_ERROR; 2224*076b9443SCy Schubert } 2225*076b9443SCy Schubert isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) ); 2226*076b9443SCy Schubert pResult = Tcl_GetObjResult(interp); 2227*076b9443SCy Schubert Tcl_SetBooleanObj(pResult, isComplete); 2228*076b9443SCy Schubert #endif 2229*076b9443SCy Schubert break; 2230*076b9443SCy Schubert } 2231*076b9443SCy Schubert 2232*076b9443SCy Schubert /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR? 2233*076b9443SCy Schubert ** 2234*076b9443SCy Schubert ** Copy data into table from filename, optionally using SEPARATOR 2235*076b9443SCy Schubert ** as column separators. If a column contains a null string, or the 2236*076b9443SCy Schubert ** value of NULLINDICATOR, a NULL is inserted for the column. 2237*076b9443SCy Schubert ** conflict-algorithm is one of the sqlite conflict algorithms: 2238*076b9443SCy Schubert ** rollback, abort, fail, ignore, replace 2239*076b9443SCy Schubert ** On success, return the number of lines processed, not necessarily same 2240*076b9443SCy Schubert ** as 'db changes' due to conflict-algorithm selected. 2241*076b9443SCy Schubert ** 2242*076b9443SCy Schubert ** This code is basically an implementation/enhancement of 2243*076b9443SCy Schubert ** the sqlite3 shell.c ".import" command. 2244*076b9443SCy Schubert ** 2245*076b9443SCy Schubert ** This command usage is equivalent to the sqlite2.x COPY statement, 2246*076b9443SCy Schubert ** which imports file data into a table using the PostgreSQL COPY file format: 2247*076b9443SCy Schubert ** $db copy $conflit_algo $table_name $filename \t \\N 2248*076b9443SCy Schubert */ 2249*076b9443SCy Schubert case DB_COPY: { 2250*076b9443SCy Schubert char *zTable; /* Insert data into this table */ 2251*076b9443SCy Schubert char *zFile; /* The file from which to extract data */ 2252*076b9443SCy Schubert char *zConflict; /* The conflict algorithm to use */ 2253*076b9443SCy Schubert sqlite3_stmt *pStmt; /* A statement */ 2254*076b9443SCy Schubert int nCol; /* Number of columns in the table */ 2255*076b9443SCy Schubert int nByte; /* Number of bytes in an SQL string */ 2256*076b9443SCy Schubert int i, j; /* Loop counters */ 2257*076b9443SCy Schubert int nSep; /* Number of bytes in zSep[] */ 2258*076b9443SCy Schubert int nNull; /* Number of bytes in zNull[] */ 2259*076b9443SCy Schubert char *zSql; /* An SQL statement */ 2260*076b9443SCy Schubert char *zLine; /* A single line of input from the file */ 2261*076b9443SCy Schubert char **azCol; /* zLine[] broken up into columns */ 2262*076b9443SCy Schubert const char *zCommit; /* How to commit changes */ 2263*076b9443SCy Schubert FILE *in; /* The input file */ 2264*076b9443SCy Schubert int lineno = 0; /* Line number of input file */ 2265*076b9443SCy Schubert char zLineNum[80]; /* Line number print buffer */ 2266*076b9443SCy Schubert Tcl_Obj *pResult; /* interp result */ 2267*076b9443SCy Schubert 2268*076b9443SCy Schubert const char *zSep; 2269*076b9443SCy Schubert const char *zNull; 2270*076b9443SCy Schubert if( objc<5 || objc>7 ){ 2271*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, 2272*076b9443SCy Schubert "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"); 2273*076b9443SCy Schubert return TCL_ERROR; 2274*076b9443SCy Schubert } 2275*076b9443SCy Schubert if( objc>=6 ){ 2276*076b9443SCy Schubert zSep = Tcl_GetStringFromObj(objv[5], 0); 2277*076b9443SCy Schubert }else{ 2278*076b9443SCy Schubert zSep = "\t"; 2279*076b9443SCy Schubert } 2280*076b9443SCy Schubert if( objc>=7 ){ 2281*076b9443SCy Schubert zNull = Tcl_GetStringFromObj(objv[6], 0); 2282*076b9443SCy Schubert }else{ 2283*076b9443SCy Schubert zNull = ""; 2284*076b9443SCy Schubert } 2285*076b9443SCy Schubert zConflict = Tcl_GetStringFromObj(objv[2], 0); 2286*076b9443SCy Schubert zTable = Tcl_GetStringFromObj(objv[3], 0); 2287*076b9443SCy Schubert zFile = Tcl_GetStringFromObj(objv[4], 0); 2288*076b9443SCy Schubert nSep = strlen30(zSep); 2289*076b9443SCy Schubert nNull = strlen30(zNull); 2290*076b9443SCy Schubert if( nSep==0 ){ 2291*076b9443SCy Schubert Tcl_AppendResult(interp,"Error: non-null separator required for copy", 2292*076b9443SCy Schubert (char*)0); 2293*076b9443SCy Schubert return TCL_ERROR; 2294*076b9443SCy Schubert } 2295*076b9443SCy Schubert if(strcmp(zConflict, "rollback") != 0 && 2296*076b9443SCy Schubert strcmp(zConflict, "abort" ) != 0 && 2297*076b9443SCy Schubert strcmp(zConflict, "fail" ) != 0 && 2298*076b9443SCy Schubert strcmp(zConflict, "ignore" ) != 0 && 2299*076b9443SCy Schubert strcmp(zConflict, "replace" ) != 0 ) { 2300*076b9443SCy Schubert Tcl_AppendResult(interp, "Error: \"", zConflict, 2301*076b9443SCy Schubert "\", conflict-algorithm must be one of: rollback, " 2302*076b9443SCy Schubert "abort, fail, ignore, or replace", (char*)0); 2303*076b9443SCy Schubert return TCL_ERROR; 2304*076b9443SCy Schubert } 2305*076b9443SCy Schubert zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); 2306*076b9443SCy Schubert if( zSql==0 ){ 2307*076b9443SCy Schubert Tcl_AppendResult(interp, "Error: no such table: ", zTable, (char*)0); 2308*076b9443SCy Schubert return TCL_ERROR; 2309*076b9443SCy Schubert } 2310*076b9443SCy Schubert nByte = strlen30(zSql); 2311*076b9443SCy Schubert rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 2312*076b9443SCy Schubert sqlite3_free(zSql); 2313*076b9443SCy Schubert if( rc ){ 2314*076b9443SCy Schubert Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0); 2315*076b9443SCy Schubert nCol = 0; 2316*076b9443SCy Schubert }else{ 2317*076b9443SCy Schubert nCol = sqlite3_column_count(pStmt); 2318*076b9443SCy Schubert } 2319*076b9443SCy Schubert sqlite3_finalize(pStmt); 2320*076b9443SCy Schubert if( nCol==0 ) { 2321*076b9443SCy Schubert return TCL_ERROR; 2322*076b9443SCy Schubert } 2323*076b9443SCy Schubert zSql = malloc( nByte + 50 + nCol*2 ); 2324*076b9443SCy Schubert if( zSql==0 ) { 2325*076b9443SCy Schubert Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0); 2326*076b9443SCy Schubert return TCL_ERROR; 2327*076b9443SCy Schubert } 2328*076b9443SCy Schubert sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?", 2329*076b9443SCy Schubert zConflict, zTable); 2330*076b9443SCy Schubert j = strlen30(zSql); 2331*076b9443SCy Schubert for(i=1; i<nCol; i++){ 2332*076b9443SCy Schubert zSql[j++] = ','; 2333*076b9443SCy Schubert zSql[j++] = '?'; 2334*076b9443SCy Schubert } 2335*076b9443SCy Schubert zSql[j++] = ')'; 2336*076b9443SCy Schubert zSql[j] = 0; 2337*076b9443SCy Schubert rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 2338*076b9443SCy Schubert free(zSql); 2339*076b9443SCy Schubert if( rc ){ 2340*076b9443SCy Schubert Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0); 2341*076b9443SCy Schubert sqlite3_finalize(pStmt); 2342*076b9443SCy Schubert return TCL_ERROR; 2343*076b9443SCy Schubert } 2344*076b9443SCy Schubert in = fopen(zFile, "rb"); 2345*076b9443SCy Schubert if( in==0 ){ 2346*076b9443SCy Schubert Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, (char*)0); 2347*076b9443SCy Schubert sqlite3_finalize(pStmt); 2348*076b9443SCy Schubert return TCL_ERROR; 2349*076b9443SCy Schubert } 2350*076b9443SCy Schubert azCol = malloc( sizeof(azCol[0])*(nCol+1) ); 2351*076b9443SCy Schubert if( azCol==0 ) { 2352*076b9443SCy Schubert Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0); 2353*076b9443SCy Schubert fclose(in); 2354*076b9443SCy Schubert return TCL_ERROR; 2355*076b9443SCy Schubert } 2356*076b9443SCy Schubert (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0); 2357*076b9443SCy Schubert zCommit = "COMMIT"; 2358*076b9443SCy Schubert while( (zLine = local_getline(0, in))!=0 ){ 2359*076b9443SCy Schubert char *z; 2360*076b9443SCy Schubert lineno++; 2361*076b9443SCy Schubert azCol[0] = zLine; 2362*076b9443SCy Schubert for(i=0, z=zLine; *z; z++){ 2363*076b9443SCy Schubert if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){ 2364*076b9443SCy Schubert *z = 0; 2365*076b9443SCy Schubert i++; 2366*076b9443SCy Schubert if( i<nCol ){ 2367*076b9443SCy Schubert azCol[i] = &z[nSep]; 2368*076b9443SCy Schubert z += nSep-1; 2369*076b9443SCy Schubert } 2370*076b9443SCy Schubert } 2371*076b9443SCy Schubert } 2372*076b9443SCy Schubert if( i+1!=nCol ){ 2373*076b9443SCy Schubert char *zErr; 2374*076b9443SCy Schubert int nErr = strlen30(zFile) + 200; 2375*076b9443SCy Schubert zErr = malloc(nErr); 2376*076b9443SCy Schubert if( zErr ){ 2377*076b9443SCy Schubert sqlite3_snprintf(nErr, zErr, 2378*076b9443SCy Schubert "Error: %s line %d: expected %d columns of data but found %d", 2379*076b9443SCy Schubert zFile, lineno, nCol, i+1); 2380*076b9443SCy Schubert Tcl_AppendResult(interp, zErr, (char*)0); 2381*076b9443SCy Schubert free(zErr); 2382*076b9443SCy Schubert } 2383*076b9443SCy Schubert zCommit = "ROLLBACK"; 2384*076b9443SCy Schubert break; 2385*076b9443SCy Schubert } 2386*076b9443SCy Schubert for(i=0; i<nCol; i++){ 2387*076b9443SCy Schubert /* check for null data, if so, bind as null */ 2388*076b9443SCy Schubert if( (nNull>0 && strcmp(azCol[i], zNull)==0) 2389*076b9443SCy Schubert || strlen30(azCol[i])==0 2390*076b9443SCy Schubert ){ 2391*076b9443SCy Schubert sqlite3_bind_null(pStmt, i+1); 2392*076b9443SCy Schubert }else{ 2393*076b9443SCy Schubert sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); 2394*076b9443SCy Schubert } 2395*076b9443SCy Schubert } 2396*076b9443SCy Schubert sqlite3_step(pStmt); 2397*076b9443SCy Schubert rc = sqlite3_reset(pStmt); 2398*076b9443SCy Schubert free(zLine); 2399*076b9443SCy Schubert if( rc!=SQLITE_OK ){ 2400*076b9443SCy Schubert Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), (char*)0); 2401*076b9443SCy Schubert zCommit = "ROLLBACK"; 2402*076b9443SCy Schubert break; 2403*076b9443SCy Schubert } 2404*076b9443SCy Schubert } 2405*076b9443SCy Schubert free(azCol); 2406*076b9443SCy Schubert fclose(in); 2407*076b9443SCy Schubert sqlite3_finalize(pStmt); 2408*076b9443SCy Schubert (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0); 2409*076b9443SCy Schubert 2410*076b9443SCy Schubert if( zCommit[0] == 'C' ){ 2411*076b9443SCy Schubert /* success, set result as number of lines processed */ 2412*076b9443SCy Schubert pResult = Tcl_GetObjResult(interp); 2413*076b9443SCy Schubert Tcl_SetIntObj(pResult, lineno); 2414*076b9443SCy Schubert rc = TCL_OK; 2415*076b9443SCy Schubert }else{ 2416*076b9443SCy Schubert /* failure, append lineno where failed */ 2417*076b9443SCy Schubert sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno); 2418*076b9443SCy Schubert Tcl_AppendResult(interp,", failed while processing line: ",zLineNum, 2419*076b9443SCy Schubert (char*)0); 2420*076b9443SCy Schubert rc = TCL_ERROR; 2421*076b9443SCy Schubert } 2422*076b9443SCy Schubert break; 2423*076b9443SCy Schubert } 2424*076b9443SCy Schubert 2425*076b9443SCy Schubert /* 2426*076b9443SCy Schubert ** $db deserialize ?DATABASE? VALUE 2427*076b9443SCy Schubert ** 2428*076b9443SCy Schubert ** Reopen DATABASE (default "main") using the content in $VALUE 2429*076b9443SCy Schubert */ 2430*076b9443SCy Schubert case DB_DESERIALIZE: { 2431*076b9443SCy Schubert #ifndef SQLITE_ENABLE_DESERIALIZE 2432*076b9443SCy Schubert Tcl_AppendResult(interp, "MEMDB not available in this build", 2433*076b9443SCy Schubert (char*)0); 2434*076b9443SCy Schubert rc = TCL_ERROR; 2435*076b9443SCy Schubert #else 2436*076b9443SCy Schubert const char *zSchema; 2437*076b9443SCy Schubert Tcl_Obj *pValue; 2438*076b9443SCy Schubert unsigned char *pBA; 2439*076b9443SCy Schubert unsigned char *pData; 2440*076b9443SCy Schubert int len, xrc; 2441*076b9443SCy Schubert 2442*076b9443SCy Schubert if( objc==3 ){ 2443*076b9443SCy Schubert zSchema = 0; 2444*076b9443SCy Schubert pValue = objv[2]; 2445*076b9443SCy Schubert }else if( objc==4 ){ 2446*076b9443SCy Schubert zSchema = Tcl_GetString(objv[2]); 2447*076b9443SCy Schubert pValue = objv[3]; 2448*076b9443SCy Schubert }else{ 2449*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? VALUE"); 2450*076b9443SCy Schubert rc = TCL_ERROR; 2451*076b9443SCy Schubert break; 2452*076b9443SCy Schubert } 2453*076b9443SCy Schubert pBA = Tcl_GetByteArrayFromObj(pValue, &len); 2454*076b9443SCy Schubert pData = sqlite3_malloc64( len ); 2455*076b9443SCy Schubert if( pData==0 && len>0 ){ 2456*076b9443SCy Schubert Tcl_AppendResult(interp, "out of memory", (char*)0); 2457*076b9443SCy Schubert rc = TCL_ERROR; 2458*076b9443SCy Schubert }else{ 2459*076b9443SCy Schubert if( len>0 ) memcpy(pData, pBA, len); 2460*076b9443SCy Schubert xrc = sqlite3_deserialize(pDb->db, zSchema, pData, len, len, 2461*076b9443SCy Schubert SQLITE_DESERIALIZE_FREEONCLOSE | 2462*076b9443SCy Schubert SQLITE_DESERIALIZE_RESIZEABLE); 2463*076b9443SCy Schubert if( xrc ){ 2464*076b9443SCy Schubert Tcl_AppendResult(interp, "unable to set MEMDB content", (char*)0); 2465*076b9443SCy Schubert rc = TCL_ERROR; 2466*076b9443SCy Schubert } 2467*076b9443SCy Schubert } 2468*076b9443SCy Schubert #endif 2469*076b9443SCy Schubert break; 2470*076b9443SCy Schubert } 2471*076b9443SCy Schubert 2472*076b9443SCy Schubert /* 2473*076b9443SCy Schubert ** $db enable_load_extension BOOLEAN 2474*076b9443SCy Schubert ** 2475*076b9443SCy Schubert ** Turn the extension loading feature on or off. It if off by 2476*076b9443SCy Schubert ** default. 2477*076b9443SCy Schubert */ 2478*076b9443SCy Schubert case DB_ENABLE_LOAD_EXTENSION: { 2479*076b9443SCy Schubert #ifndef SQLITE_OMIT_LOAD_EXTENSION 2480*076b9443SCy Schubert int onoff; 2481*076b9443SCy Schubert if( objc!=3 ){ 2482*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN"); 2483*076b9443SCy Schubert return TCL_ERROR; 2484*076b9443SCy Schubert } 2485*076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ 2486*076b9443SCy Schubert return TCL_ERROR; 2487*076b9443SCy Schubert } 2488*076b9443SCy Schubert sqlite3_enable_load_extension(pDb->db, onoff); 2489*076b9443SCy Schubert break; 2490*076b9443SCy Schubert #else 2491*076b9443SCy Schubert Tcl_AppendResult(interp, "extension loading is turned off at compile-time", 2492*076b9443SCy Schubert (char*)0); 2493*076b9443SCy Schubert return TCL_ERROR; 2494*076b9443SCy Schubert #endif 2495*076b9443SCy Schubert } 2496*076b9443SCy Schubert 2497*076b9443SCy Schubert /* 2498*076b9443SCy Schubert ** $db errorcode 2499*076b9443SCy Schubert ** 2500*076b9443SCy Schubert ** Return the numeric error code that was returned by the most recent 2501*076b9443SCy Schubert ** call to sqlite3_exec(). 2502*076b9443SCy Schubert */ 2503*076b9443SCy Schubert case DB_ERRORCODE: { 2504*076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); 2505*076b9443SCy Schubert break; 2506*076b9443SCy Schubert } 2507*076b9443SCy Schubert 2508*076b9443SCy Schubert /* 2509*076b9443SCy Schubert ** $db exists $sql 2510*076b9443SCy Schubert ** $db onecolumn $sql 2511*076b9443SCy Schubert ** 2512*076b9443SCy Schubert ** The onecolumn method is the equivalent of: 2513*076b9443SCy Schubert ** lindex [$db eval $sql] 0 2514*076b9443SCy Schubert */ 2515*076b9443SCy Schubert case DB_EXISTS: 2516*076b9443SCy Schubert case DB_ONECOLUMN: { 2517*076b9443SCy Schubert Tcl_Obj *pResult = 0; 2518*076b9443SCy Schubert DbEvalContext sEval; 2519*076b9443SCy Schubert if( objc!=3 ){ 2520*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 2521*076b9443SCy Schubert return TCL_ERROR; 2522*076b9443SCy Schubert } 2523*076b9443SCy Schubert 2524*076b9443SCy Schubert dbEvalInit(&sEval, pDb, objv[2], 0, 0); 2525*076b9443SCy Schubert rc = dbEvalStep(&sEval); 2526*076b9443SCy Schubert if( choice==DB_ONECOLUMN ){ 2527*076b9443SCy Schubert if( rc==TCL_OK ){ 2528*076b9443SCy Schubert pResult = dbEvalColumnValue(&sEval, 0); 2529*076b9443SCy Schubert }else if( rc==TCL_BREAK ){ 2530*076b9443SCy Schubert Tcl_ResetResult(interp); 2531*076b9443SCy Schubert } 2532*076b9443SCy Schubert }else if( rc==TCL_BREAK || rc==TCL_OK ){ 2533*076b9443SCy Schubert pResult = Tcl_NewBooleanObj(rc==TCL_OK); 2534*076b9443SCy Schubert } 2535*076b9443SCy Schubert dbEvalFinalize(&sEval); 2536*076b9443SCy Schubert if( pResult ) Tcl_SetObjResult(interp, pResult); 2537*076b9443SCy Schubert 2538*076b9443SCy Schubert if( rc==TCL_BREAK ){ 2539*076b9443SCy Schubert rc = TCL_OK; 2540*076b9443SCy Schubert } 2541*076b9443SCy Schubert break; 2542*076b9443SCy Schubert } 2543*076b9443SCy Schubert 2544*076b9443SCy Schubert /* 2545*076b9443SCy Schubert ** $db eval ?options? $sql ?array? ?{ ...code... }? 2546*076b9443SCy Schubert ** 2547*076b9443SCy Schubert ** The SQL statement in $sql is evaluated. For each row, the values are 2548*076b9443SCy Schubert ** placed in elements of the array named "array" and ...code... is executed. 2549*076b9443SCy Schubert ** If "array" and "code" are omitted, then no callback is every invoked. 2550*076b9443SCy Schubert ** If "array" is an empty string, then the values are placed in variables 2551*076b9443SCy Schubert ** that have the same name as the fields extracted by the query. 2552*076b9443SCy Schubert */ 2553*076b9443SCy Schubert case DB_EVAL: { 2554*076b9443SCy Schubert int evalFlags = 0; 2555*076b9443SCy Schubert const char *zOpt; 2556*076b9443SCy Schubert while( objc>3 && (zOpt = Tcl_GetString(objv[2]))!=0 && zOpt[0]=='-' ){ 2557*076b9443SCy Schubert if( strcmp(zOpt, "-withoutnulls")==0 ){ 2558*076b9443SCy Schubert evalFlags |= SQLITE_EVAL_WITHOUTNULLS; 2559*076b9443SCy Schubert } 2560*076b9443SCy Schubert else{ 2561*076b9443SCy Schubert Tcl_AppendResult(interp, "unknown option: \"", zOpt, "\"", (void*)0); 2562*076b9443SCy Schubert return TCL_ERROR; 2563*076b9443SCy Schubert } 2564*076b9443SCy Schubert objc--; 2565*076b9443SCy Schubert objv++; 2566*076b9443SCy Schubert } 2567*076b9443SCy Schubert if( objc<3 || objc>5 ){ 2568*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, 2569*076b9443SCy Schubert "?OPTIONS? SQL ?ARRAY-NAME? ?SCRIPT?"); 2570*076b9443SCy Schubert return TCL_ERROR; 2571*076b9443SCy Schubert } 2572*076b9443SCy Schubert 2573*076b9443SCy Schubert if( objc==3 ){ 2574*076b9443SCy Schubert DbEvalContext sEval; 2575*076b9443SCy Schubert Tcl_Obj *pRet = Tcl_NewObj(); 2576*076b9443SCy Schubert Tcl_IncrRefCount(pRet); 2577*076b9443SCy Schubert dbEvalInit(&sEval, pDb, objv[2], 0, 0); 2578*076b9443SCy Schubert while( TCL_OK==(rc = dbEvalStep(&sEval)) ){ 2579*076b9443SCy Schubert int i; 2580*076b9443SCy Schubert int nCol; 2581*076b9443SCy Schubert dbEvalRowInfo(&sEval, &nCol, 0); 2582*076b9443SCy Schubert for(i=0; i<nCol; i++){ 2583*076b9443SCy Schubert Tcl_ListObjAppendElement(interp, pRet, dbEvalColumnValue(&sEval, i)); 2584*076b9443SCy Schubert } 2585*076b9443SCy Schubert } 2586*076b9443SCy Schubert dbEvalFinalize(&sEval); 2587*076b9443SCy Schubert if( rc==TCL_BREAK ){ 2588*076b9443SCy Schubert Tcl_SetObjResult(interp, pRet); 2589*076b9443SCy Schubert rc = TCL_OK; 2590*076b9443SCy Schubert } 2591*076b9443SCy Schubert Tcl_DecrRefCount(pRet); 2592*076b9443SCy Schubert }else{ 2593*076b9443SCy Schubert ClientData cd2[2]; 2594*076b9443SCy Schubert DbEvalContext *p; 2595*076b9443SCy Schubert Tcl_Obj *pArray = 0; 2596*076b9443SCy Schubert Tcl_Obj *pScript; 2597*076b9443SCy Schubert 2598*076b9443SCy Schubert if( objc>=5 && *(char *)Tcl_GetString(objv[3]) ){ 2599*076b9443SCy Schubert pArray = objv[3]; 2600*076b9443SCy Schubert } 2601*076b9443SCy Schubert pScript = objv[objc-1]; 2602*076b9443SCy Schubert Tcl_IncrRefCount(pScript); 2603*076b9443SCy Schubert 2604*076b9443SCy Schubert p = (DbEvalContext *)Tcl_Alloc(sizeof(DbEvalContext)); 2605*076b9443SCy Schubert dbEvalInit(p, pDb, objv[2], pArray, evalFlags); 2606*076b9443SCy Schubert 2607*076b9443SCy Schubert cd2[0] = (void *)p; 2608*076b9443SCy Schubert cd2[1] = (void *)pScript; 2609*076b9443SCy Schubert rc = DbEvalNextCmd(cd2, interp, TCL_OK); 2610*076b9443SCy Schubert } 2611*076b9443SCy Schubert break; 2612*076b9443SCy Schubert } 2613*076b9443SCy Schubert 2614*076b9443SCy Schubert /* 2615*076b9443SCy Schubert ** $db function NAME [-argcount N] [-deterministic] SCRIPT 2616*076b9443SCy Schubert ** 2617*076b9443SCy Schubert ** Create a new SQL function called NAME. Whenever that function is 2618*076b9443SCy Schubert ** called, invoke SCRIPT to evaluate the function. 2619*076b9443SCy Schubert */ 2620*076b9443SCy Schubert case DB_FUNCTION: { 2621*076b9443SCy Schubert int flags = SQLITE_UTF8; 2622*076b9443SCy Schubert SqlFunc *pFunc; 2623*076b9443SCy Schubert Tcl_Obj *pScript; 2624*076b9443SCy Schubert char *zName; 2625*076b9443SCy Schubert int nArg = -1; 2626*076b9443SCy Schubert int i; 2627*076b9443SCy Schubert if( objc<4 ){ 2628*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "NAME ?SWITCHES? SCRIPT"); 2629*076b9443SCy Schubert return TCL_ERROR; 2630*076b9443SCy Schubert } 2631*076b9443SCy Schubert for(i=3; i<(objc-1); i++){ 2632*076b9443SCy Schubert const char *z = Tcl_GetString(objv[i]); 2633*076b9443SCy Schubert int n = strlen30(z); 2634*076b9443SCy Schubert if( n>2 && strncmp(z, "-argcount",n)==0 ){ 2635*076b9443SCy Schubert if( i==(objc-2) ){ 2636*076b9443SCy Schubert Tcl_AppendResult(interp, "option requires an argument: ", z,(char*)0); 2637*076b9443SCy Schubert return TCL_ERROR; 2638*076b9443SCy Schubert } 2639*076b9443SCy Schubert if( Tcl_GetIntFromObj(interp, objv[i+1], &nArg) ) return TCL_ERROR; 2640*076b9443SCy Schubert if( nArg<0 ){ 2641*076b9443SCy Schubert Tcl_AppendResult(interp, "number of arguments must be non-negative", 2642*076b9443SCy Schubert (char*)0); 2643*076b9443SCy Schubert return TCL_ERROR; 2644*076b9443SCy Schubert } 2645*076b9443SCy Schubert i++; 2646*076b9443SCy Schubert }else 2647*076b9443SCy Schubert if( n>2 && strncmp(z, "-deterministic",n)==0 ){ 2648*076b9443SCy Schubert flags |= SQLITE_DETERMINISTIC; 2649*076b9443SCy Schubert }else{ 2650*076b9443SCy Schubert Tcl_AppendResult(interp, "bad option \"", z, 2651*076b9443SCy Schubert "\": must be -argcount or -deterministic", (char*)0 2652*076b9443SCy Schubert ); 2653*076b9443SCy Schubert return TCL_ERROR; 2654*076b9443SCy Schubert } 2655*076b9443SCy Schubert } 2656*076b9443SCy Schubert 2657*076b9443SCy Schubert pScript = objv[objc-1]; 2658*076b9443SCy Schubert zName = Tcl_GetStringFromObj(objv[2], 0); 2659*076b9443SCy Schubert pFunc = findSqlFunc(pDb, zName); 2660*076b9443SCy Schubert if( pFunc==0 ) return TCL_ERROR; 2661*076b9443SCy Schubert if( pFunc->pScript ){ 2662*076b9443SCy Schubert Tcl_DecrRefCount(pFunc->pScript); 2663*076b9443SCy Schubert } 2664*076b9443SCy Schubert pFunc->pScript = pScript; 2665*076b9443SCy Schubert Tcl_IncrRefCount(pScript); 2666*076b9443SCy Schubert pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript); 2667*076b9443SCy Schubert rc = sqlite3_create_function(pDb->db, zName, nArg, flags, 2668*076b9443SCy Schubert pFunc, tclSqlFunc, 0, 0); 2669*076b9443SCy Schubert if( rc!=SQLITE_OK ){ 2670*076b9443SCy Schubert rc = TCL_ERROR; 2671*076b9443SCy Schubert Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 2672*076b9443SCy Schubert } 2673*076b9443SCy Schubert break; 2674*076b9443SCy Schubert } 2675*076b9443SCy Schubert 2676*076b9443SCy Schubert /* 2677*076b9443SCy Schubert ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID 2678*076b9443SCy Schubert */ 2679*076b9443SCy Schubert case DB_INCRBLOB: { 2680*076b9443SCy Schubert #ifdef SQLITE_OMIT_INCRBLOB 2681*076b9443SCy Schubert Tcl_AppendResult(interp, "incrblob not available in this build", (char*)0); 2682*076b9443SCy Schubert return TCL_ERROR; 2683*076b9443SCy Schubert #else 2684*076b9443SCy Schubert int isReadonly = 0; 2685*076b9443SCy Schubert const char *zDb = "main"; 2686*076b9443SCy Schubert const char *zTable; 2687*076b9443SCy Schubert const char *zColumn; 2688*076b9443SCy Schubert Tcl_WideInt iRow; 2689*076b9443SCy Schubert 2690*076b9443SCy Schubert /* Check for the -readonly option */ 2691*076b9443SCy Schubert if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){ 2692*076b9443SCy Schubert isReadonly = 1; 2693*076b9443SCy Schubert } 2694*076b9443SCy Schubert 2695*076b9443SCy Schubert if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){ 2696*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID"); 2697*076b9443SCy Schubert return TCL_ERROR; 2698*076b9443SCy Schubert } 2699*076b9443SCy Schubert 2700*076b9443SCy Schubert if( objc==(6+isReadonly) ){ 2701*076b9443SCy Schubert zDb = Tcl_GetString(objv[2]); 2702*076b9443SCy Schubert } 2703*076b9443SCy Schubert zTable = Tcl_GetString(objv[objc-3]); 2704*076b9443SCy Schubert zColumn = Tcl_GetString(objv[objc-2]); 2705*076b9443SCy Schubert rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow); 2706*076b9443SCy Schubert 2707*076b9443SCy Schubert if( rc==TCL_OK ){ 2708*076b9443SCy Schubert rc = createIncrblobChannel( 2709*076b9443SCy Schubert interp, pDb, zDb, zTable, zColumn, (sqlite3_int64)iRow, isReadonly 2710*076b9443SCy Schubert ); 2711*076b9443SCy Schubert } 2712*076b9443SCy Schubert #endif 2713*076b9443SCy Schubert break; 2714*076b9443SCy Schubert } 2715*076b9443SCy Schubert 2716*076b9443SCy Schubert /* 2717*076b9443SCy Schubert ** $db interrupt 2718*076b9443SCy Schubert ** 2719*076b9443SCy Schubert ** Interrupt the execution of the inner-most SQL interpreter. This 2720*076b9443SCy Schubert ** causes the SQL statement to return an error of SQLITE_INTERRUPT. 2721*076b9443SCy Schubert */ 2722*076b9443SCy Schubert case DB_INTERRUPT: { 2723*076b9443SCy Schubert sqlite3_interrupt(pDb->db); 2724*076b9443SCy Schubert break; 2725*076b9443SCy Schubert } 2726*076b9443SCy Schubert 2727*076b9443SCy Schubert /* 2728*076b9443SCy Schubert ** $db nullvalue ?STRING? 2729*076b9443SCy Schubert ** 2730*076b9443SCy Schubert ** Change text used when a NULL comes back from the database. If ?STRING? 2731*076b9443SCy Schubert ** is not present, then the current string used for NULL is returned. 2732*076b9443SCy Schubert ** If STRING is present, then STRING is returned. 2733*076b9443SCy Schubert ** 2734*076b9443SCy Schubert */ 2735*076b9443SCy Schubert case DB_NULLVALUE: { 2736*076b9443SCy Schubert if( objc!=2 && objc!=3 ){ 2737*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE"); 2738*076b9443SCy Schubert return TCL_ERROR; 2739*076b9443SCy Schubert } 2740*076b9443SCy Schubert if( objc==3 ){ 2741*076b9443SCy Schubert int len; 2742*076b9443SCy Schubert char *zNull = Tcl_GetStringFromObj(objv[2], &len); 2743*076b9443SCy Schubert if( pDb->zNull ){ 2744*076b9443SCy Schubert Tcl_Free(pDb->zNull); 2745*076b9443SCy Schubert } 2746*076b9443SCy Schubert if( zNull && len>0 ){ 2747*076b9443SCy Schubert pDb->zNull = Tcl_Alloc( len + 1 ); 2748*076b9443SCy Schubert memcpy(pDb->zNull, zNull, len); 2749*076b9443SCy Schubert pDb->zNull[len] = '\0'; 2750*076b9443SCy Schubert }else{ 2751*076b9443SCy Schubert pDb->zNull = 0; 2752*076b9443SCy Schubert } 2753*076b9443SCy Schubert } 2754*076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewStringObj(pDb->zNull, -1)); 2755*076b9443SCy Schubert break; 2756*076b9443SCy Schubert } 2757*076b9443SCy Schubert 2758*076b9443SCy Schubert /* 2759*076b9443SCy Schubert ** $db last_insert_rowid 2760*076b9443SCy Schubert ** 2761*076b9443SCy Schubert ** Return an integer which is the ROWID for the most recent insert. 2762*076b9443SCy Schubert */ 2763*076b9443SCy Schubert case DB_LAST_INSERT_ROWID: { 2764*076b9443SCy Schubert Tcl_Obj *pResult; 2765*076b9443SCy Schubert Tcl_WideInt rowid; 2766*076b9443SCy Schubert if( objc!=2 ){ 2767*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, ""); 2768*076b9443SCy Schubert return TCL_ERROR; 2769*076b9443SCy Schubert } 2770*076b9443SCy Schubert rowid = sqlite3_last_insert_rowid(pDb->db); 2771*076b9443SCy Schubert pResult = Tcl_GetObjResult(interp); 2772*076b9443SCy Schubert Tcl_SetWideIntObj(pResult, rowid); 2773*076b9443SCy Schubert break; 2774*076b9443SCy Schubert } 2775*076b9443SCy Schubert 2776*076b9443SCy Schubert /* 2777*076b9443SCy Schubert ** The DB_ONECOLUMN method is implemented together with DB_EXISTS. 2778*076b9443SCy Schubert */ 2779*076b9443SCy Schubert 2780*076b9443SCy Schubert /* $db progress ?N CALLBACK? 2781*076b9443SCy Schubert ** 2782*076b9443SCy Schubert ** Invoke the given callback every N virtual machine opcodes while executing 2783*076b9443SCy Schubert ** queries. 2784*076b9443SCy Schubert */ 2785*076b9443SCy Schubert case DB_PROGRESS: { 2786*076b9443SCy Schubert if( objc==2 ){ 2787*076b9443SCy Schubert if( pDb->zProgress ){ 2788*076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zProgress, (char*)0); 2789*076b9443SCy Schubert } 2790*076b9443SCy Schubert }else if( objc==4 ){ 2791*076b9443SCy Schubert char *zProgress; 2792*076b9443SCy Schubert int len; 2793*076b9443SCy Schubert int N; 2794*076b9443SCy Schubert if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ 2795*076b9443SCy Schubert return TCL_ERROR; 2796*076b9443SCy Schubert }; 2797*076b9443SCy Schubert if( pDb->zProgress ){ 2798*076b9443SCy Schubert Tcl_Free(pDb->zProgress); 2799*076b9443SCy Schubert } 2800*076b9443SCy Schubert zProgress = Tcl_GetStringFromObj(objv[3], &len); 2801*076b9443SCy Schubert if( zProgress && len>0 ){ 2802*076b9443SCy Schubert pDb->zProgress = Tcl_Alloc( len + 1 ); 2803*076b9443SCy Schubert memcpy(pDb->zProgress, zProgress, len+1); 2804*076b9443SCy Schubert }else{ 2805*076b9443SCy Schubert pDb->zProgress = 0; 2806*076b9443SCy Schubert } 2807*076b9443SCy Schubert #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 2808*076b9443SCy Schubert if( pDb->zProgress ){ 2809*076b9443SCy Schubert pDb->interp = interp; 2810*076b9443SCy Schubert sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb); 2811*076b9443SCy Schubert }else{ 2812*076b9443SCy Schubert sqlite3_progress_handler(pDb->db, 0, 0, 0); 2813*076b9443SCy Schubert } 2814*076b9443SCy Schubert #endif 2815*076b9443SCy Schubert }else{ 2816*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); 2817*076b9443SCy Schubert return TCL_ERROR; 2818*076b9443SCy Schubert } 2819*076b9443SCy Schubert break; 2820*076b9443SCy Schubert } 2821*076b9443SCy Schubert 2822*076b9443SCy Schubert /* $db profile ?CALLBACK? 2823*076b9443SCy Schubert ** 2824*076b9443SCy Schubert ** Make arrangements to invoke the CALLBACK routine after each SQL statement 2825*076b9443SCy Schubert ** that has run. The text of the SQL and the amount of elapse time are 2826*076b9443SCy Schubert ** appended to CALLBACK before the script is run. 2827*076b9443SCy Schubert */ 2828*076b9443SCy Schubert case DB_PROFILE: { 2829*076b9443SCy Schubert if( objc>3 ){ 2830*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 2831*076b9443SCy Schubert return TCL_ERROR; 2832*076b9443SCy Schubert }else if( objc==2 ){ 2833*076b9443SCy Schubert if( pDb->zProfile ){ 2834*076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zProfile, (char*)0); 2835*076b9443SCy Schubert } 2836*076b9443SCy Schubert }else{ 2837*076b9443SCy Schubert char *zProfile; 2838*076b9443SCy Schubert int len; 2839*076b9443SCy Schubert if( pDb->zProfile ){ 2840*076b9443SCy Schubert Tcl_Free(pDb->zProfile); 2841*076b9443SCy Schubert } 2842*076b9443SCy Schubert zProfile = Tcl_GetStringFromObj(objv[2], &len); 2843*076b9443SCy Schubert if( zProfile && len>0 ){ 2844*076b9443SCy Schubert pDb->zProfile = Tcl_Alloc( len + 1 ); 2845*076b9443SCy Schubert memcpy(pDb->zProfile, zProfile, len+1); 2846*076b9443SCy Schubert }else{ 2847*076b9443SCy Schubert pDb->zProfile = 0; 2848*076b9443SCy Schubert } 2849*076b9443SCy Schubert #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ 2850*076b9443SCy Schubert !defined(SQLITE_OMIT_DEPRECATED) 2851*076b9443SCy Schubert if( pDb->zProfile ){ 2852*076b9443SCy Schubert pDb->interp = interp; 2853*076b9443SCy Schubert sqlite3_profile(pDb->db, DbProfileHandler, pDb); 2854*076b9443SCy Schubert }else{ 2855*076b9443SCy Schubert sqlite3_profile(pDb->db, 0, 0); 2856*076b9443SCy Schubert } 2857*076b9443SCy Schubert #endif 2858*076b9443SCy Schubert } 2859*076b9443SCy Schubert break; 2860*076b9443SCy Schubert } 2861*076b9443SCy Schubert 2862*076b9443SCy Schubert /* 2863*076b9443SCy Schubert ** $db rekey KEY 2864*076b9443SCy Schubert ** 2865*076b9443SCy Schubert ** Change the encryption key on the currently open database. 2866*076b9443SCy Schubert */ 2867*076b9443SCy Schubert case DB_REKEY: { 2868*076b9443SCy Schubert #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 2869*076b9443SCy Schubert int nKey; 2870*076b9443SCy Schubert void *pKey; 2871*076b9443SCy Schubert #endif 2872*076b9443SCy Schubert if( objc!=3 ){ 2873*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "KEY"); 2874*076b9443SCy Schubert return TCL_ERROR; 2875*076b9443SCy Schubert } 2876*076b9443SCy Schubert #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 2877*076b9443SCy Schubert pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); 2878*076b9443SCy Schubert rc = sqlite3_rekey(pDb->db, pKey, nKey); 2879*076b9443SCy Schubert if( rc ){ 2880*076b9443SCy Schubert Tcl_AppendResult(interp, sqlite3_errstr(rc), (char*)0); 2881*076b9443SCy Schubert rc = TCL_ERROR; 2882*076b9443SCy Schubert } 2883*076b9443SCy Schubert #endif 2884*076b9443SCy Schubert break; 2885*076b9443SCy Schubert } 2886*076b9443SCy Schubert 2887*076b9443SCy Schubert /* $db restore ?DATABASE? FILENAME 2888*076b9443SCy Schubert ** 2889*076b9443SCy Schubert ** Open a database file named FILENAME. Transfer the content 2890*076b9443SCy Schubert ** of FILENAME into the local database DATABASE (default: "main"). 2891*076b9443SCy Schubert */ 2892*076b9443SCy Schubert case DB_RESTORE: { 2893*076b9443SCy Schubert const char *zSrcFile; 2894*076b9443SCy Schubert const char *zDestDb; 2895*076b9443SCy Schubert sqlite3 *pSrc; 2896*076b9443SCy Schubert sqlite3_backup *pBackup; 2897*076b9443SCy Schubert int nTimeout = 0; 2898*076b9443SCy Schubert 2899*076b9443SCy Schubert if( objc==3 ){ 2900*076b9443SCy Schubert zDestDb = "main"; 2901*076b9443SCy Schubert zSrcFile = Tcl_GetString(objv[2]); 2902*076b9443SCy Schubert }else if( objc==4 ){ 2903*076b9443SCy Schubert zDestDb = Tcl_GetString(objv[2]); 2904*076b9443SCy Schubert zSrcFile = Tcl_GetString(objv[3]); 2905*076b9443SCy Schubert }else{ 2906*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 2907*076b9443SCy Schubert return TCL_ERROR; 2908*076b9443SCy Schubert } 2909*076b9443SCy Schubert rc = sqlite3_open_v2(zSrcFile, &pSrc, 2910*076b9443SCy Schubert SQLITE_OPEN_READONLY | pDb->openFlags, 0); 2911*076b9443SCy Schubert if( rc!=SQLITE_OK ){ 2912*076b9443SCy Schubert Tcl_AppendResult(interp, "cannot open source database: ", 2913*076b9443SCy Schubert sqlite3_errmsg(pSrc), (char*)0); 2914*076b9443SCy Schubert sqlite3_close(pSrc); 2915*076b9443SCy Schubert return TCL_ERROR; 2916*076b9443SCy Schubert } 2917*076b9443SCy Schubert pBackup = sqlite3_backup_init(pDb->db, zDestDb, pSrc, "main"); 2918*076b9443SCy Schubert if( pBackup==0 ){ 2919*076b9443SCy Schubert Tcl_AppendResult(interp, "restore failed: ", 2920*076b9443SCy Schubert sqlite3_errmsg(pDb->db), (char*)0); 2921*076b9443SCy Schubert sqlite3_close(pSrc); 2922*076b9443SCy Schubert return TCL_ERROR; 2923*076b9443SCy Schubert } 2924*076b9443SCy Schubert while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK 2925*076b9443SCy Schubert || rc==SQLITE_BUSY ){ 2926*076b9443SCy Schubert if( rc==SQLITE_BUSY ){ 2927*076b9443SCy Schubert if( nTimeout++ >= 3 ) break; 2928*076b9443SCy Schubert sqlite3_sleep(100); 2929*076b9443SCy Schubert } 2930*076b9443SCy Schubert } 2931*076b9443SCy Schubert sqlite3_backup_finish(pBackup); 2932*076b9443SCy Schubert if( rc==SQLITE_DONE ){ 2933*076b9443SCy Schubert rc = TCL_OK; 2934*076b9443SCy Schubert }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 2935*076b9443SCy Schubert Tcl_AppendResult(interp, "restore failed: source database busy", 2936*076b9443SCy Schubert (char*)0); 2937*076b9443SCy Schubert rc = TCL_ERROR; 2938*076b9443SCy Schubert }else{ 2939*076b9443SCy Schubert Tcl_AppendResult(interp, "restore failed: ", 2940*076b9443SCy Schubert sqlite3_errmsg(pDb->db), (char*)0); 2941*076b9443SCy Schubert rc = TCL_ERROR; 2942*076b9443SCy Schubert } 2943*076b9443SCy Schubert sqlite3_close(pSrc); 2944*076b9443SCy Schubert break; 2945*076b9443SCy Schubert } 2946*076b9443SCy Schubert 2947*076b9443SCy Schubert /* 2948*076b9443SCy Schubert ** $db serialize ?DATABASE? 2949*076b9443SCy Schubert ** 2950*076b9443SCy Schubert ** Return a serialization of a database. 2951*076b9443SCy Schubert */ 2952*076b9443SCy Schubert case DB_SERIALIZE: { 2953*076b9443SCy Schubert #ifndef SQLITE_ENABLE_DESERIALIZE 2954*076b9443SCy Schubert Tcl_AppendResult(interp, "MEMDB not available in this build", 2955*076b9443SCy Schubert (char*)0); 2956*076b9443SCy Schubert rc = TCL_ERROR; 2957*076b9443SCy Schubert #else 2958*076b9443SCy Schubert const char *zSchema = objc>=3 ? Tcl_GetString(objv[2]) : "main"; 2959*076b9443SCy Schubert sqlite3_int64 sz = 0; 2960*076b9443SCy Schubert unsigned char *pData; 2961*076b9443SCy Schubert if( objc!=2 && objc!=3 ){ 2962*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE?"); 2963*076b9443SCy Schubert rc = TCL_ERROR; 2964*076b9443SCy Schubert }else{ 2965*076b9443SCy Schubert int needFree; 2966*076b9443SCy Schubert pData = sqlite3_serialize(pDb->db, zSchema, &sz, SQLITE_SERIALIZE_NOCOPY); 2967*076b9443SCy Schubert if( pData ){ 2968*076b9443SCy Schubert needFree = 0; 2969*076b9443SCy Schubert }else{ 2970*076b9443SCy Schubert pData = sqlite3_serialize(pDb->db, zSchema, &sz, 0); 2971*076b9443SCy Schubert needFree = 1; 2972*076b9443SCy Schubert } 2973*076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pData,sz)); 2974*076b9443SCy Schubert if( needFree ) sqlite3_free(pData); 2975*076b9443SCy Schubert } 2976*076b9443SCy Schubert #endif 2977*076b9443SCy Schubert break; 2978*076b9443SCy Schubert } 2979*076b9443SCy Schubert 2980*076b9443SCy Schubert /* 2981*076b9443SCy Schubert ** $db status (step|sort|autoindex|vmstep) 2982*076b9443SCy Schubert ** 2983*076b9443SCy Schubert ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or 2984*076b9443SCy Schubert ** SQLITE_STMTSTATUS_SORT for the most recent eval. 2985*076b9443SCy Schubert */ 2986*076b9443SCy Schubert case DB_STATUS: { 2987*076b9443SCy Schubert int v; 2988*076b9443SCy Schubert const char *zOp; 2989*076b9443SCy Schubert if( objc!=3 ){ 2990*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "(step|sort|autoindex)"); 2991*076b9443SCy Schubert return TCL_ERROR; 2992*076b9443SCy Schubert } 2993*076b9443SCy Schubert zOp = Tcl_GetString(objv[2]); 2994*076b9443SCy Schubert if( strcmp(zOp, "step")==0 ){ 2995*076b9443SCy Schubert v = pDb->nStep; 2996*076b9443SCy Schubert }else if( strcmp(zOp, "sort")==0 ){ 2997*076b9443SCy Schubert v = pDb->nSort; 2998*076b9443SCy Schubert }else if( strcmp(zOp, "autoindex")==0 ){ 2999*076b9443SCy Schubert v = pDb->nIndex; 3000*076b9443SCy Schubert }else if( strcmp(zOp, "vmstep")==0 ){ 3001*076b9443SCy Schubert v = pDb->nVMStep; 3002*076b9443SCy Schubert }else{ 3003*076b9443SCy Schubert Tcl_AppendResult(interp, 3004*076b9443SCy Schubert "bad argument: should be autoindex, step, sort or vmstep", 3005*076b9443SCy Schubert (char*)0); 3006*076b9443SCy Schubert return TCL_ERROR; 3007*076b9443SCy Schubert } 3008*076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewIntObj(v)); 3009*076b9443SCy Schubert break; 3010*076b9443SCy Schubert } 3011*076b9443SCy Schubert 3012*076b9443SCy Schubert /* 3013*076b9443SCy Schubert ** $db timeout MILLESECONDS 3014*076b9443SCy Schubert ** 3015*076b9443SCy Schubert ** Delay for the number of milliseconds specified when a file is locked. 3016*076b9443SCy Schubert */ 3017*076b9443SCy Schubert case DB_TIMEOUT: { 3018*076b9443SCy Schubert int ms; 3019*076b9443SCy Schubert if( objc!=3 ){ 3020*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); 3021*076b9443SCy Schubert return TCL_ERROR; 3022*076b9443SCy Schubert } 3023*076b9443SCy Schubert if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; 3024*076b9443SCy Schubert sqlite3_busy_timeout(pDb->db, ms); 3025*076b9443SCy Schubert break; 3026*076b9443SCy Schubert } 3027*076b9443SCy Schubert 3028*076b9443SCy Schubert /* 3029*076b9443SCy Schubert ** $db total_changes 3030*076b9443SCy Schubert ** 3031*076b9443SCy Schubert ** Return the number of rows that were modified, inserted, or deleted 3032*076b9443SCy Schubert ** since the database handle was created. 3033*076b9443SCy Schubert */ 3034*076b9443SCy Schubert case DB_TOTAL_CHANGES: { 3035*076b9443SCy Schubert Tcl_Obj *pResult; 3036*076b9443SCy Schubert if( objc!=2 ){ 3037*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, ""); 3038*076b9443SCy Schubert return TCL_ERROR; 3039*076b9443SCy Schubert } 3040*076b9443SCy Schubert pResult = Tcl_GetObjResult(interp); 3041*076b9443SCy Schubert Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db)); 3042*076b9443SCy Schubert break; 3043*076b9443SCy Schubert } 3044*076b9443SCy Schubert 3045*076b9443SCy Schubert /* $db trace ?CALLBACK? 3046*076b9443SCy Schubert ** 3047*076b9443SCy Schubert ** Make arrangements to invoke the CALLBACK routine for each SQL statement 3048*076b9443SCy Schubert ** that is executed. The text of the SQL is appended to CALLBACK before 3049*076b9443SCy Schubert ** it is executed. 3050*076b9443SCy Schubert */ 3051*076b9443SCy Schubert case DB_TRACE: { 3052*076b9443SCy Schubert if( objc>3 ){ 3053*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 3054*076b9443SCy Schubert return TCL_ERROR; 3055*076b9443SCy Schubert }else if( objc==2 ){ 3056*076b9443SCy Schubert if( pDb->zTrace ){ 3057*076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zTrace, (char*)0); 3058*076b9443SCy Schubert } 3059*076b9443SCy Schubert }else{ 3060*076b9443SCy Schubert char *zTrace; 3061*076b9443SCy Schubert int len; 3062*076b9443SCy Schubert if( pDb->zTrace ){ 3063*076b9443SCy Schubert Tcl_Free(pDb->zTrace); 3064*076b9443SCy Schubert } 3065*076b9443SCy Schubert zTrace = Tcl_GetStringFromObj(objv[2], &len); 3066*076b9443SCy Schubert if( zTrace && len>0 ){ 3067*076b9443SCy Schubert pDb->zTrace = Tcl_Alloc( len + 1 ); 3068*076b9443SCy Schubert memcpy(pDb->zTrace, zTrace, len+1); 3069*076b9443SCy Schubert }else{ 3070*076b9443SCy Schubert pDb->zTrace = 0; 3071*076b9443SCy Schubert } 3072*076b9443SCy Schubert #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ 3073*076b9443SCy Schubert !defined(SQLITE_OMIT_DEPRECATED) 3074*076b9443SCy Schubert if( pDb->zTrace ){ 3075*076b9443SCy Schubert pDb->interp = interp; 3076*076b9443SCy Schubert sqlite3_trace(pDb->db, DbTraceHandler, pDb); 3077*076b9443SCy Schubert }else{ 3078*076b9443SCy Schubert sqlite3_trace(pDb->db, 0, 0); 3079*076b9443SCy Schubert } 3080*076b9443SCy Schubert #endif 3081*076b9443SCy Schubert } 3082*076b9443SCy Schubert break; 3083*076b9443SCy Schubert } 3084*076b9443SCy Schubert 3085*076b9443SCy Schubert /* $db trace_v2 ?CALLBACK? ?MASK? 3086*076b9443SCy Schubert ** 3087*076b9443SCy Schubert ** Make arrangements to invoke the CALLBACK routine for each trace event 3088*076b9443SCy Schubert ** matching the mask that is generated. The parameters are appended to 3089*076b9443SCy Schubert ** CALLBACK before it is executed. 3090*076b9443SCy Schubert */ 3091*076b9443SCy Schubert case DB_TRACE_V2: { 3092*076b9443SCy Schubert if( objc>4 ){ 3093*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK? ?MASK?"); 3094*076b9443SCy Schubert return TCL_ERROR; 3095*076b9443SCy Schubert }else if( objc==2 ){ 3096*076b9443SCy Schubert if( pDb->zTraceV2 ){ 3097*076b9443SCy Schubert Tcl_AppendResult(interp, pDb->zTraceV2, (char*)0); 3098*076b9443SCy Schubert } 3099*076b9443SCy Schubert }else{ 3100*076b9443SCy Schubert char *zTraceV2; 3101*076b9443SCy Schubert int len; 3102*076b9443SCy Schubert Tcl_WideInt wMask = 0; 3103*076b9443SCy Schubert if( objc==4 ){ 3104*076b9443SCy Schubert static const char *TTYPE_strs[] = { 3105*076b9443SCy Schubert "statement", "profile", "row", "close", 0 3106*076b9443SCy Schubert }; 3107*076b9443SCy Schubert enum TTYPE_enum { 3108*076b9443SCy Schubert TTYPE_STMT, TTYPE_PROFILE, TTYPE_ROW, TTYPE_CLOSE 3109*076b9443SCy Schubert }; 3110*076b9443SCy Schubert int i; 3111*076b9443SCy Schubert if( TCL_OK!=Tcl_ListObjLength(interp, objv[3], &len) ){ 3112*076b9443SCy Schubert return TCL_ERROR; 3113*076b9443SCy Schubert } 3114*076b9443SCy Schubert for(i=0; i<len; i++){ 3115*076b9443SCy Schubert Tcl_Obj *pObj; 3116*076b9443SCy Schubert int ttype; 3117*076b9443SCy Schubert if( TCL_OK!=Tcl_ListObjIndex(interp, objv[3], i, &pObj) ){ 3118*076b9443SCy Schubert return TCL_ERROR; 3119*076b9443SCy Schubert } 3120*076b9443SCy Schubert if( Tcl_GetIndexFromObj(interp, pObj, TTYPE_strs, "trace type", 3121*076b9443SCy Schubert 0, &ttype)!=TCL_OK ){ 3122*076b9443SCy Schubert Tcl_WideInt wType; 3123*076b9443SCy Schubert Tcl_Obj *pError = Tcl_DuplicateObj(Tcl_GetObjResult(interp)); 3124*076b9443SCy Schubert Tcl_IncrRefCount(pError); 3125*076b9443SCy Schubert if( TCL_OK==Tcl_GetWideIntFromObj(interp, pObj, &wType) ){ 3126*076b9443SCy Schubert Tcl_DecrRefCount(pError); 3127*076b9443SCy Schubert wMask |= wType; 3128*076b9443SCy Schubert }else{ 3129*076b9443SCy Schubert Tcl_SetObjResult(interp, pError); 3130*076b9443SCy Schubert Tcl_DecrRefCount(pError); 3131*076b9443SCy Schubert return TCL_ERROR; 3132*076b9443SCy Schubert } 3133*076b9443SCy Schubert }else{ 3134*076b9443SCy Schubert switch( (enum TTYPE_enum)ttype ){ 3135*076b9443SCy Schubert case TTYPE_STMT: wMask |= SQLITE_TRACE_STMT; break; 3136*076b9443SCy Schubert case TTYPE_PROFILE: wMask |= SQLITE_TRACE_PROFILE; break; 3137*076b9443SCy Schubert case TTYPE_ROW: wMask |= SQLITE_TRACE_ROW; break; 3138*076b9443SCy Schubert case TTYPE_CLOSE: wMask |= SQLITE_TRACE_CLOSE; break; 3139*076b9443SCy Schubert } 3140*076b9443SCy Schubert } 3141*076b9443SCy Schubert } 3142*076b9443SCy Schubert }else{ 3143*076b9443SCy Schubert wMask = SQLITE_TRACE_STMT; /* use the "legacy" default */ 3144*076b9443SCy Schubert } 3145*076b9443SCy Schubert if( pDb->zTraceV2 ){ 3146*076b9443SCy Schubert Tcl_Free(pDb->zTraceV2); 3147*076b9443SCy Schubert } 3148*076b9443SCy Schubert zTraceV2 = Tcl_GetStringFromObj(objv[2], &len); 3149*076b9443SCy Schubert if( zTraceV2 && len>0 ){ 3150*076b9443SCy Schubert pDb->zTraceV2 = Tcl_Alloc( len + 1 ); 3151*076b9443SCy Schubert memcpy(pDb->zTraceV2, zTraceV2, len+1); 3152*076b9443SCy Schubert }else{ 3153*076b9443SCy Schubert pDb->zTraceV2 = 0; 3154*076b9443SCy Schubert } 3155*076b9443SCy Schubert #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) 3156*076b9443SCy Schubert if( pDb->zTraceV2 ){ 3157*076b9443SCy Schubert pDb->interp = interp; 3158*076b9443SCy Schubert sqlite3_trace_v2(pDb->db, (unsigned)wMask, DbTraceV2Handler, pDb); 3159*076b9443SCy Schubert }else{ 3160*076b9443SCy Schubert sqlite3_trace_v2(pDb->db, 0, 0, 0); 3161*076b9443SCy Schubert } 3162*076b9443SCy Schubert #endif 3163*076b9443SCy Schubert } 3164*076b9443SCy Schubert break; 3165*076b9443SCy Schubert } 3166*076b9443SCy Schubert 3167*076b9443SCy Schubert /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT 3168*076b9443SCy Schubert ** 3169*076b9443SCy Schubert ** Start a new transaction (if we are not already in the midst of a 3170*076b9443SCy Schubert ** transaction) and execute the TCL script SCRIPT. After SCRIPT 3171*076b9443SCy Schubert ** completes, either commit the transaction or roll it back if SCRIPT 3172*076b9443SCy Schubert ** throws an exception. Or if no new transation was started, do nothing. 3173*076b9443SCy Schubert ** pass the exception on up the stack. 3174*076b9443SCy Schubert ** 3175*076b9443SCy Schubert ** This command was inspired by Dave Thomas's talk on Ruby at the 3176*076b9443SCy Schubert ** 2005 O'Reilly Open Source Convention (OSCON). 3177*076b9443SCy Schubert */ 3178*076b9443SCy Schubert case DB_TRANSACTION: { 3179*076b9443SCy Schubert Tcl_Obj *pScript; 3180*076b9443SCy Schubert const char *zBegin = "SAVEPOINT _tcl_transaction"; 3181*076b9443SCy Schubert if( objc!=3 && objc!=4 ){ 3182*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT"); 3183*076b9443SCy Schubert return TCL_ERROR; 3184*076b9443SCy Schubert } 3185*076b9443SCy Schubert 3186*076b9443SCy Schubert if( pDb->nTransaction==0 && objc==4 ){ 3187*076b9443SCy Schubert static const char *TTYPE_strs[] = { 3188*076b9443SCy Schubert "deferred", "exclusive", "immediate", 0 3189*076b9443SCy Schubert }; 3190*076b9443SCy Schubert enum TTYPE_enum { 3191*076b9443SCy Schubert TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE 3192*076b9443SCy Schubert }; 3193*076b9443SCy Schubert int ttype; 3194*076b9443SCy Schubert if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type", 3195*076b9443SCy Schubert 0, &ttype) ){ 3196*076b9443SCy Schubert return TCL_ERROR; 3197*076b9443SCy Schubert } 3198*076b9443SCy Schubert switch( (enum TTYPE_enum)ttype ){ 3199*076b9443SCy Schubert case TTYPE_DEFERRED: /* no-op */; break; 3200*076b9443SCy Schubert case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break; 3201*076b9443SCy Schubert case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break; 3202*076b9443SCy Schubert } 3203*076b9443SCy Schubert } 3204*076b9443SCy Schubert pScript = objv[objc-1]; 3205*076b9443SCy Schubert 3206*076b9443SCy Schubert /* Run the SQLite BEGIN command to open a transaction or savepoint. */ 3207*076b9443SCy Schubert pDb->disableAuth++; 3208*076b9443SCy Schubert rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0); 3209*076b9443SCy Schubert pDb->disableAuth--; 3210*076b9443SCy Schubert if( rc!=SQLITE_OK ){ 3211*076b9443SCy Schubert Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 3212*076b9443SCy Schubert return TCL_ERROR; 3213*076b9443SCy Schubert } 3214*076b9443SCy Schubert pDb->nTransaction++; 3215*076b9443SCy Schubert 3216*076b9443SCy Schubert /* If using NRE, schedule a callback to invoke the script pScript, then 3217*076b9443SCy Schubert ** a second callback to commit (or rollback) the transaction or savepoint 3218*076b9443SCy Schubert ** opened above. If not using NRE, evaluate the script directly, then 3219*076b9443SCy Schubert ** call function DbTransPostCmd() to commit (or rollback) the transaction 3220*076b9443SCy Schubert ** or savepoint. */ 3221*076b9443SCy Schubert if( DbUseNre() ){ 3222*076b9443SCy Schubert Tcl_NRAddCallback(interp, DbTransPostCmd, cd, 0, 0, 0); 3223*076b9443SCy Schubert (void)Tcl_NREvalObj(interp, pScript, 0); 3224*076b9443SCy Schubert }else{ 3225*076b9443SCy Schubert rc = DbTransPostCmd(&cd, interp, Tcl_EvalObjEx(interp, pScript, 0)); 3226*076b9443SCy Schubert } 3227*076b9443SCy Schubert break; 3228*076b9443SCy Schubert } 3229*076b9443SCy Schubert 3230*076b9443SCy Schubert /* 3231*076b9443SCy Schubert ** $db unlock_notify ?script? 3232*076b9443SCy Schubert */ 3233*076b9443SCy Schubert case DB_UNLOCK_NOTIFY: { 3234*076b9443SCy Schubert #ifndef SQLITE_ENABLE_UNLOCK_NOTIFY 3235*076b9443SCy Schubert Tcl_AppendResult(interp, "unlock_notify not available in this build", 3236*076b9443SCy Schubert (char*)0); 3237*076b9443SCy Schubert rc = TCL_ERROR; 3238*076b9443SCy Schubert #else 3239*076b9443SCy Schubert if( objc!=2 && objc!=3 ){ 3240*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 3241*076b9443SCy Schubert rc = TCL_ERROR; 3242*076b9443SCy Schubert }else{ 3243*076b9443SCy Schubert void (*xNotify)(void **, int) = 0; 3244*076b9443SCy Schubert void *pNotifyArg = 0; 3245*076b9443SCy Schubert 3246*076b9443SCy Schubert if( pDb->pUnlockNotify ){ 3247*076b9443SCy Schubert Tcl_DecrRefCount(pDb->pUnlockNotify); 3248*076b9443SCy Schubert pDb->pUnlockNotify = 0; 3249*076b9443SCy Schubert } 3250*076b9443SCy Schubert 3251*076b9443SCy Schubert if( objc==3 ){ 3252*076b9443SCy Schubert xNotify = DbUnlockNotify; 3253*076b9443SCy Schubert pNotifyArg = (void *)pDb; 3254*076b9443SCy Schubert pDb->pUnlockNotify = objv[2]; 3255*076b9443SCy Schubert Tcl_IncrRefCount(pDb->pUnlockNotify); 3256*076b9443SCy Schubert } 3257*076b9443SCy Schubert 3258*076b9443SCy Schubert if( sqlite3_unlock_notify(pDb->db, xNotify, pNotifyArg) ){ 3259*076b9443SCy Schubert Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 3260*076b9443SCy Schubert rc = TCL_ERROR; 3261*076b9443SCy Schubert } 3262*076b9443SCy Schubert } 3263*076b9443SCy Schubert #endif 3264*076b9443SCy Schubert break; 3265*076b9443SCy Schubert } 3266*076b9443SCy Schubert 3267*076b9443SCy Schubert /* 3268*076b9443SCy Schubert ** $db preupdate_hook count 3269*076b9443SCy Schubert ** $db preupdate_hook hook ?SCRIPT? 3270*076b9443SCy Schubert ** $db preupdate_hook new INDEX 3271*076b9443SCy Schubert ** $db preupdate_hook old INDEX 3272*076b9443SCy Schubert */ 3273*076b9443SCy Schubert case DB_PREUPDATE: { 3274*076b9443SCy Schubert #ifndef SQLITE_ENABLE_PREUPDATE_HOOK 3275*076b9443SCy Schubert Tcl_AppendResult(interp, "preupdate_hook was omitted at compile-time", 3276*076b9443SCy Schubert (char*)0); 3277*076b9443SCy Schubert rc = TCL_ERROR; 3278*076b9443SCy Schubert #else 3279*076b9443SCy Schubert static const char *azSub[] = {"count", "depth", "hook", "new", "old", 0}; 3280*076b9443SCy Schubert enum DbPreupdateSubCmd { 3281*076b9443SCy Schubert PRE_COUNT, PRE_DEPTH, PRE_HOOK, PRE_NEW, PRE_OLD 3282*076b9443SCy Schubert }; 3283*076b9443SCy Schubert int iSub; 3284*076b9443SCy Schubert 3285*076b9443SCy Schubert if( objc<3 ){ 3286*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "SUB-COMMAND ?ARGS?"); 3287*076b9443SCy Schubert } 3288*076b9443SCy Schubert if( Tcl_GetIndexFromObj(interp, objv[2], azSub, "sub-command", 0, &iSub) ){ 3289*076b9443SCy Schubert return TCL_ERROR; 3290*076b9443SCy Schubert } 3291*076b9443SCy Schubert 3292*076b9443SCy Schubert switch( (enum DbPreupdateSubCmd)iSub ){ 3293*076b9443SCy Schubert case PRE_COUNT: { 3294*076b9443SCy Schubert int nCol = sqlite3_preupdate_count(pDb->db); 3295*076b9443SCy Schubert Tcl_SetObjResult(interp, Tcl_NewIntObj(nCol)); 3296*076b9443SCy Schubert break; 3297*076b9443SCy Schubert } 3298*076b9443SCy Schubert 3299*076b9443SCy Schubert case PRE_HOOK: { 3300*076b9443SCy Schubert if( objc>4 ){ 3301*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "hook ?SCRIPT?"); 3302*076b9443SCy Schubert return TCL_ERROR; 3303*076b9443SCy Schubert } 3304*076b9443SCy Schubert DbHookCmd(interp, pDb, (objc==4 ? objv[3] : 0), &pDb->pPreUpdateHook); 3305*076b9443SCy Schubert break; 3306*076b9443SCy Schubert } 3307*076b9443SCy Schubert 3308*076b9443SCy Schubert case PRE_DEPTH: { 3309*076b9443SCy Schubert Tcl_Obj *pRet; 3310*076b9443SCy Schubert if( objc!=3 ){ 3311*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 3, objv, ""); 3312*076b9443SCy Schubert return TCL_ERROR; 3313*076b9443SCy Schubert } 3314*076b9443SCy Schubert pRet = Tcl_NewIntObj(sqlite3_preupdate_depth(pDb->db)); 3315*076b9443SCy Schubert Tcl_SetObjResult(interp, pRet); 3316*076b9443SCy Schubert break; 3317*076b9443SCy Schubert } 3318*076b9443SCy Schubert 3319*076b9443SCy Schubert case PRE_NEW: 3320*076b9443SCy Schubert case PRE_OLD: { 3321*076b9443SCy Schubert int iIdx; 3322*076b9443SCy Schubert sqlite3_value *pValue; 3323*076b9443SCy Schubert if( objc!=4 ){ 3324*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 3, objv, "INDEX"); 3325*076b9443SCy Schubert return TCL_ERROR; 3326*076b9443SCy Schubert } 3327*076b9443SCy Schubert if( Tcl_GetIntFromObj(interp, objv[3], &iIdx) ){ 3328*076b9443SCy Schubert return TCL_ERROR; 3329*076b9443SCy Schubert } 3330*076b9443SCy Schubert 3331*076b9443SCy Schubert if( iSub==PRE_OLD ){ 3332*076b9443SCy Schubert rc = sqlite3_preupdate_old(pDb->db, iIdx, &pValue); 3333*076b9443SCy Schubert }else{ 3334*076b9443SCy Schubert assert( iSub==PRE_NEW ); 3335*076b9443SCy Schubert rc = sqlite3_preupdate_new(pDb->db, iIdx, &pValue); 3336*076b9443SCy Schubert } 3337*076b9443SCy Schubert 3338*076b9443SCy Schubert if( rc==SQLITE_OK ){ 3339*076b9443SCy Schubert Tcl_Obj *pObj; 3340*076b9443SCy Schubert pObj = Tcl_NewStringObj((char*)sqlite3_value_text(pValue), -1); 3341*076b9443SCy Schubert Tcl_SetObjResult(interp, pObj); 3342*076b9443SCy Schubert }else{ 3343*076b9443SCy Schubert Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 3344*076b9443SCy Schubert return TCL_ERROR; 3345*076b9443SCy Schubert } 3346*076b9443SCy Schubert } 3347*076b9443SCy Schubert } 3348*076b9443SCy Schubert #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 3349*076b9443SCy Schubert break; 3350*076b9443SCy Schubert } 3351*076b9443SCy Schubert 3352*076b9443SCy Schubert /* 3353*076b9443SCy Schubert ** $db wal_hook ?script? 3354*076b9443SCy Schubert ** $db update_hook ?script? 3355*076b9443SCy Schubert ** $db rollback_hook ?script? 3356*076b9443SCy Schubert */ 3357*076b9443SCy Schubert case DB_WAL_HOOK: 3358*076b9443SCy Schubert case DB_UPDATE_HOOK: 3359*076b9443SCy Schubert case DB_ROLLBACK_HOOK: { 3360*076b9443SCy Schubert /* set ppHook to point at pUpdateHook or pRollbackHook, depending on 3361*076b9443SCy Schubert ** whether [$db update_hook] or [$db rollback_hook] was invoked. 3362*076b9443SCy Schubert */ 3363*076b9443SCy Schubert Tcl_Obj **ppHook = 0; 3364*076b9443SCy Schubert if( choice==DB_WAL_HOOK ) ppHook = &pDb->pWalHook; 3365*076b9443SCy Schubert if( choice==DB_UPDATE_HOOK ) ppHook = &pDb->pUpdateHook; 3366*076b9443SCy Schubert if( choice==DB_ROLLBACK_HOOK ) ppHook = &pDb->pRollbackHook; 3367*076b9443SCy Schubert if( objc>3 ){ 3368*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 3369*076b9443SCy Schubert return TCL_ERROR; 3370*076b9443SCy Schubert } 3371*076b9443SCy Schubert 3372*076b9443SCy Schubert DbHookCmd(interp, pDb, (objc==3 ? objv[2] : 0), ppHook); 3373*076b9443SCy Schubert break; 3374*076b9443SCy Schubert } 3375*076b9443SCy Schubert 3376*076b9443SCy Schubert /* $db version 3377*076b9443SCy Schubert ** 3378*076b9443SCy Schubert ** Return the version string for this database. 3379*076b9443SCy Schubert */ 3380*076b9443SCy Schubert case DB_VERSION: { 3381*076b9443SCy Schubert int i; 3382*076b9443SCy Schubert for(i=2; i<objc; i++){ 3383*076b9443SCy Schubert const char *zArg = Tcl_GetString(objv[i]); 3384*076b9443SCy Schubert /* Optional arguments to $db version are used for testing purpose */ 3385*076b9443SCy Schubert #ifdef SQLITE_TEST 3386*076b9443SCy Schubert /* $db version -use-legacy-prepare BOOLEAN 3387*076b9443SCy Schubert ** 3388*076b9443SCy Schubert ** Turn the use of legacy sqlite3_prepare() on or off. 3389*076b9443SCy Schubert */ 3390*076b9443SCy Schubert if( strcmp(zArg, "-use-legacy-prepare")==0 && i+1<objc ){ 3391*076b9443SCy Schubert i++; 3392*076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &pDb->bLegacyPrepare) ){ 3393*076b9443SCy Schubert return TCL_ERROR; 3394*076b9443SCy Schubert } 3395*076b9443SCy Schubert }else 3396*076b9443SCy Schubert 3397*076b9443SCy Schubert /* $db version -last-stmt-ptr 3398*076b9443SCy Schubert ** 3399*076b9443SCy Schubert ** Return a string which is a hex encoding of the pointer to the 3400*076b9443SCy Schubert ** most recent sqlite3_stmt in the statement cache. 3401*076b9443SCy Schubert */ 3402*076b9443SCy Schubert if( strcmp(zArg, "-last-stmt-ptr")==0 ){ 3403*076b9443SCy Schubert char zBuf[100]; 3404*076b9443SCy Schubert sqlite3_snprintf(sizeof(zBuf), zBuf, "%p", 3405*076b9443SCy Schubert pDb->stmtList ? pDb->stmtList->pStmt: 0); 3406*076b9443SCy Schubert Tcl_SetResult(interp, zBuf, TCL_VOLATILE); 3407*076b9443SCy Schubert }else 3408*076b9443SCy Schubert #endif /* SQLITE_TEST */ 3409*076b9443SCy Schubert { 3410*076b9443SCy Schubert Tcl_AppendResult(interp, "unknown argument: ", zArg, (char*)0); 3411*076b9443SCy Schubert return TCL_ERROR; 3412*076b9443SCy Schubert } 3413*076b9443SCy Schubert } 3414*076b9443SCy Schubert if( i==2 ){ 3415*076b9443SCy Schubert Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC); 3416*076b9443SCy Schubert } 3417*076b9443SCy Schubert break; 3418*076b9443SCy Schubert } 3419*076b9443SCy Schubert 3420*076b9443SCy Schubert 3421*076b9443SCy Schubert } /* End of the SWITCH statement */ 3422*076b9443SCy Schubert return rc; 3423*076b9443SCy Schubert } 3424*076b9443SCy Schubert 3425*076b9443SCy Schubert #if SQLITE_TCL_NRE 3426*076b9443SCy Schubert /* 3427*076b9443SCy Schubert ** Adaptor that provides an objCmd interface to the NRE-enabled 3428*076b9443SCy Schubert ** interface implementation. 3429*076b9443SCy Schubert */ 3430*076b9443SCy Schubert static int SQLITE_TCLAPI DbObjCmdAdaptor( 3431*076b9443SCy Schubert void *cd, 3432*076b9443SCy Schubert Tcl_Interp *interp, 3433*076b9443SCy Schubert int objc, 3434*076b9443SCy Schubert Tcl_Obj *const*objv 3435*076b9443SCy Schubert ){ 3436*076b9443SCy Schubert return Tcl_NRCallObjProc(interp, DbObjCmd, cd, objc, objv); 3437*076b9443SCy Schubert } 3438*076b9443SCy Schubert #endif /* SQLITE_TCL_NRE */ 3439*076b9443SCy Schubert 3440*076b9443SCy Schubert /* 3441*076b9443SCy Schubert ** Issue the usage message when the "sqlite3" command arguments are 3442*076b9443SCy Schubert ** incorrect. 3443*076b9443SCy Schubert */ 3444*076b9443SCy Schubert static int sqliteCmdUsage( 3445*076b9443SCy Schubert Tcl_Interp *interp, 3446*076b9443SCy Schubert Tcl_Obj *const*objv 3447*076b9443SCy Schubert ){ 3448*076b9443SCy Schubert Tcl_WrongNumArgs(interp, 1, objv, 3449*076b9443SCy Schubert "HANDLE ?FILENAME? ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" 3450*076b9443SCy Schubert " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?" 3451*076b9443SCy Schubert #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3452*076b9443SCy Schubert " ?-key CODECKEY?" 3453*076b9443SCy Schubert #endif 3454*076b9443SCy Schubert ); 3455*076b9443SCy Schubert return TCL_ERROR; 3456*076b9443SCy Schubert } 3457*076b9443SCy Schubert 3458*076b9443SCy Schubert /* 3459*076b9443SCy Schubert ** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN? 3460*076b9443SCy Schubert ** ?-create BOOLEAN? ?-nomutex BOOLEAN? 3461*076b9443SCy Schubert ** 3462*076b9443SCy Schubert ** This is the main Tcl command. When the "sqlite" Tcl command is 3463*076b9443SCy Schubert ** invoked, this routine runs to process that command. 3464*076b9443SCy Schubert ** 3465*076b9443SCy Schubert ** The first argument, DBNAME, is an arbitrary name for a new 3466*076b9443SCy Schubert ** database connection. This command creates a new command named 3467*076b9443SCy Schubert ** DBNAME that is used to control that connection. The database 3468*076b9443SCy Schubert ** connection is deleted when the DBNAME command is deleted. 3469*076b9443SCy Schubert ** 3470*076b9443SCy Schubert ** The second argument is the name of the database file. 3471*076b9443SCy Schubert ** 3472*076b9443SCy Schubert */ 3473*076b9443SCy Schubert static int SQLITE_TCLAPI DbMain( 3474*076b9443SCy Schubert void *cd, 3475*076b9443SCy Schubert Tcl_Interp *interp, 3476*076b9443SCy Schubert int objc, 3477*076b9443SCy Schubert Tcl_Obj *const*objv 3478*076b9443SCy Schubert ){ 3479*076b9443SCy Schubert SqliteDb *p; 3480*076b9443SCy Schubert const char *zArg; 3481*076b9443SCy Schubert char *zErrMsg; 3482*076b9443SCy Schubert int i; 3483*076b9443SCy Schubert const char *zFile = 0; 3484*076b9443SCy Schubert const char *zVfs = 0; 3485*076b9443SCy Schubert int flags; 3486*076b9443SCy Schubert Tcl_DString translatedFilename; 3487*076b9443SCy Schubert #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3488*076b9443SCy Schubert void *pKey = 0; 3489*076b9443SCy Schubert int nKey = 0; 3490*076b9443SCy Schubert #endif 3491*076b9443SCy Schubert int rc; 3492*076b9443SCy Schubert 3493*076b9443SCy Schubert /* In normal use, each TCL interpreter runs in a single thread. So 3494*076b9443SCy Schubert ** by default, we can turn off mutexing on SQLite database connections. 3495*076b9443SCy Schubert ** However, for testing purposes it is useful to have mutexes turned 3496*076b9443SCy Schubert ** on. So, by default, mutexes default off. But if compiled with 3497*076b9443SCy Schubert ** SQLITE_TCL_DEFAULT_FULLMUTEX then mutexes default on. 3498*076b9443SCy Schubert */ 3499*076b9443SCy Schubert #ifdef SQLITE_TCL_DEFAULT_FULLMUTEX 3500*076b9443SCy Schubert flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX; 3501*076b9443SCy Schubert #else 3502*076b9443SCy Schubert flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX; 3503*076b9443SCy Schubert #endif 3504*076b9443SCy Schubert 3505*076b9443SCy Schubert if( objc==1 ) return sqliteCmdUsage(interp, objv); 3506*076b9443SCy Schubert if( objc==2 ){ 3507*076b9443SCy Schubert zArg = Tcl_GetStringFromObj(objv[1], 0); 3508*076b9443SCy Schubert if( strcmp(zArg,"-version")==0 ){ 3509*076b9443SCy Schubert Tcl_AppendResult(interp,sqlite3_libversion(), (char*)0); 3510*076b9443SCy Schubert return TCL_OK; 3511*076b9443SCy Schubert } 3512*076b9443SCy Schubert if( strcmp(zArg,"-sourceid")==0 ){ 3513*076b9443SCy Schubert Tcl_AppendResult(interp,sqlite3_sourceid(), (char*)0); 3514*076b9443SCy Schubert return TCL_OK; 3515*076b9443SCy Schubert } 3516*076b9443SCy Schubert if( strcmp(zArg,"-has-codec")==0 ){ 3517*076b9443SCy Schubert #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3518*076b9443SCy Schubert Tcl_AppendResult(interp,"1",(char*)0); 3519*076b9443SCy Schubert #else 3520*076b9443SCy Schubert Tcl_AppendResult(interp,"0",(char*)0); 3521*076b9443SCy Schubert #endif 3522*076b9443SCy Schubert return TCL_OK; 3523*076b9443SCy Schubert } 3524*076b9443SCy Schubert if( zArg[0]=='-' ) return sqliteCmdUsage(interp, objv); 3525*076b9443SCy Schubert } 3526*076b9443SCy Schubert for(i=2; i<objc; i++){ 3527*076b9443SCy Schubert zArg = Tcl_GetString(objv[i]); 3528*076b9443SCy Schubert if( zArg[0]!='-' ){ 3529*076b9443SCy Schubert if( zFile!=0 ) return sqliteCmdUsage(interp, objv); 3530*076b9443SCy Schubert zFile = zArg; 3531*076b9443SCy Schubert continue; 3532*076b9443SCy Schubert } 3533*076b9443SCy Schubert if( i==objc-1 ) return sqliteCmdUsage(interp, objv); 3534*076b9443SCy Schubert i++; 3535*076b9443SCy Schubert if( strcmp(zArg,"-key")==0 ){ 3536*076b9443SCy Schubert #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3537*076b9443SCy Schubert pKey = Tcl_GetByteArrayFromObj(objv[i], &nKey); 3538*076b9443SCy Schubert #endif 3539*076b9443SCy Schubert }else if( strcmp(zArg, "-vfs")==0 ){ 3540*076b9443SCy Schubert zVfs = Tcl_GetString(objv[i]); 3541*076b9443SCy Schubert }else if( strcmp(zArg, "-readonly")==0 ){ 3542*076b9443SCy Schubert int b; 3543*076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 3544*076b9443SCy Schubert if( b ){ 3545*076b9443SCy Schubert flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); 3546*076b9443SCy Schubert flags |= SQLITE_OPEN_READONLY; 3547*076b9443SCy Schubert }else{ 3548*076b9443SCy Schubert flags &= ~SQLITE_OPEN_READONLY; 3549*076b9443SCy Schubert flags |= SQLITE_OPEN_READWRITE; 3550*076b9443SCy Schubert } 3551*076b9443SCy Schubert }else if( strcmp(zArg, "-create")==0 ){ 3552*076b9443SCy Schubert int b; 3553*076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 3554*076b9443SCy Schubert if( b && (flags & SQLITE_OPEN_READONLY)==0 ){ 3555*076b9443SCy Schubert flags |= SQLITE_OPEN_CREATE; 3556*076b9443SCy Schubert }else{ 3557*076b9443SCy Schubert flags &= ~SQLITE_OPEN_CREATE; 3558*076b9443SCy Schubert } 3559*076b9443SCy Schubert }else if( strcmp(zArg, "-nomutex")==0 ){ 3560*076b9443SCy Schubert int b; 3561*076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 3562*076b9443SCy Schubert if( b ){ 3563*076b9443SCy Schubert flags |= SQLITE_OPEN_NOMUTEX; 3564*076b9443SCy Schubert flags &= ~SQLITE_OPEN_FULLMUTEX; 3565*076b9443SCy Schubert }else{ 3566*076b9443SCy Schubert flags &= ~SQLITE_OPEN_NOMUTEX; 3567*076b9443SCy Schubert } 3568*076b9443SCy Schubert }else if( strcmp(zArg, "-fullmutex")==0 ){ 3569*076b9443SCy Schubert int b; 3570*076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 3571*076b9443SCy Schubert if( b ){ 3572*076b9443SCy Schubert flags |= SQLITE_OPEN_FULLMUTEX; 3573*076b9443SCy Schubert flags &= ~SQLITE_OPEN_NOMUTEX; 3574*076b9443SCy Schubert }else{ 3575*076b9443SCy Schubert flags &= ~SQLITE_OPEN_FULLMUTEX; 3576*076b9443SCy Schubert } 3577*076b9443SCy Schubert }else if( strcmp(zArg, "-uri")==0 ){ 3578*076b9443SCy Schubert int b; 3579*076b9443SCy Schubert if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; 3580*076b9443SCy Schubert if( b ){ 3581*076b9443SCy Schubert flags |= SQLITE_OPEN_URI; 3582*076b9443SCy Schubert }else{ 3583*076b9443SCy Schubert flags &= ~SQLITE_OPEN_URI; 3584*076b9443SCy Schubert } 3585*076b9443SCy Schubert }else{ 3586*076b9443SCy Schubert Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0); 3587*076b9443SCy Schubert return TCL_ERROR; 3588*076b9443SCy Schubert } 3589*076b9443SCy Schubert } 3590*076b9443SCy Schubert zErrMsg = 0; 3591*076b9443SCy Schubert p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); 3592*076b9443SCy Schubert memset(p, 0, sizeof(*p)); 3593*076b9443SCy Schubert if( zFile==0 ) zFile = ""; 3594*076b9443SCy Schubert zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); 3595*076b9443SCy Schubert rc = sqlite3_open_v2(zFile, &p->db, flags, zVfs); 3596*076b9443SCy Schubert Tcl_DStringFree(&translatedFilename); 3597*076b9443SCy Schubert if( p->db ){ 3598*076b9443SCy Schubert if( SQLITE_OK!=sqlite3_errcode(p->db) ){ 3599*076b9443SCy Schubert zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); 3600*076b9443SCy Schubert sqlite3_close(p->db); 3601*076b9443SCy Schubert p->db = 0; 3602*076b9443SCy Schubert } 3603*076b9443SCy Schubert }else{ 3604*076b9443SCy Schubert zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc)); 3605*076b9443SCy Schubert } 3606*076b9443SCy Schubert #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3607*076b9443SCy Schubert if( p->db ){ 3608*076b9443SCy Schubert sqlite3_key(p->db, pKey, nKey); 3609*076b9443SCy Schubert } 3610*076b9443SCy Schubert #endif 3611*076b9443SCy Schubert if( p->db==0 ){ 3612*076b9443SCy Schubert Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 3613*076b9443SCy Schubert Tcl_Free((char*)p); 3614*076b9443SCy Schubert sqlite3_free(zErrMsg); 3615*076b9443SCy Schubert return TCL_ERROR; 3616*076b9443SCy Schubert } 3617*076b9443SCy Schubert p->maxStmt = NUM_PREPARED_STMTS; 3618*076b9443SCy Schubert p->openFlags = flags & SQLITE_OPEN_URI; 3619*076b9443SCy Schubert p->interp = interp; 3620*076b9443SCy Schubert zArg = Tcl_GetStringFromObj(objv[1], 0); 3621*076b9443SCy Schubert if( DbUseNre() ){ 3622*076b9443SCy Schubert Tcl_NRCreateCommand(interp, zArg, DbObjCmdAdaptor, DbObjCmd, 3623*076b9443SCy Schubert (char*)p, DbDeleteCmd); 3624*076b9443SCy Schubert }else{ 3625*076b9443SCy Schubert Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); 3626*076b9443SCy Schubert } 3627*076b9443SCy Schubert return TCL_OK; 3628*076b9443SCy Schubert } 3629*076b9443SCy Schubert 3630*076b9443SCy Schubert /* 3631*076b9443SCy Schubert ** Provide a dummy Tcl_InitStubs if we are using this as a static 3632*076b9443SCy Schubert ** library. 3633*076b9443SCy Schubert */ 3634*076b9443SCy Schubert #ifndef USE_TCL_STUBS 3635*076b9443SCy Schubert # undef Tcl_InitStubs 3636*076b9443SCy Schubert # define Tcl_InitStubs(a,b,c) TCL_VERSION 3637*076b9443SCy Schubert #endif 3638*076b9443SCy Schubert 3639*076b9443SCy Schubert /* 3640*076b9443SCy Schubert ** Make sure we have a PACKAGE_VERSION macro defined. This will be 3641*076b9443SCy Schubert ** defined automatically by the TEA makefile. But other makefiles 3642*076b9443SCy Schubert ** do not define it. 3643*076b9443SCy Schubert */ 3644*076b9443SCy Schubert #ifndef PACKAGE_VERSION 3645*076b9443SCy Schubert # define PACKAGE_VERSION SQLITE_VERSION 3646*076b9443SCy Schubert #endif 3647*076b9443SCy Schubert 3648*076b9443SCy Schubert /* 3649*076b9443SCy Schubert ** Initialize this module. 3650*076b9443SCy Schubert ** 3651*076b9443SCy Schubert ** This Tcl module contains only a single new Tcl command named "sqlite". 3652*076b9443SCy Schubert ** (Hence there is no namespace. There is no point in using a namespace 3653*076b9443SCy Schubert ** if the extension only supplies one new name!) The "sqlite" command is 3654*076b9443SCy Schubert ** used to open a new SQLite database. See the DbMain() routine above 3655*076b9443SCy Schubert ** for additional information. 3656*076b9443SCy Schubert ** 3657*076b9443SCy Schubert ** The EXTERN macros are required by TCL in order to work on windows. 3658*076b9443SCy Schubert */ 3659*076b9443SCy Schubert EXTERN int Sqlite3_Init(Tcl_Interp *interp){ 3660*076b9443SCy Schubert int rc = Tcl_InitStubs(interp, "8.4", 0) ? TCL_OK : TCL_ERROR; 3661*076b9443SCy Schubert if( rc==TCL_OK ){ 3662*076b9443SCy Schubert Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); 3663*076b9443SCy Schubert #ifndef SQLITE_3_SUFFIX_ONLY 3664*076b9443SCy Schubert /* The "sqlite" alias is undocumented. It is here only to support 3665*076b9443SCy Schubert ** legacy scripts. All new scripts should use only the "sqlite3" 3666*076b9443SCy Schubert ** command. */ 3667*076b9443SCy Schubert Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); 3668*076b9443SCy Schubert #endif 3669*076b9443SCy Schubert rc = Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION); 3670*076b9443SCy Schubert } 3671*076b9443SCy Schubert return rc; 3672*076b9443SCy Schubert } 3673*076b9443SCy Schubert EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3674*076b9443SCy Schubert EXTERN int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3675*076b9443SCy Schubert EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3676*076b9443SCy Schubert 3677*076b9443SCy Schubert /* Because it accesses the file-system and uses persistent state, SQLite 3678*076b9443SCy Schubert ** is not considered appropriate for safe interpreters. Hence, we cause 3679*076b9443SCy Schubert ** the _SafeInit() interfaces return TCL_ERROR. 3680*076b9443SCy Schubert */ 3681*076b9443SCy Schubert EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_ERROR; } 3682*076b9443SCy Schubert EXTERN int Sqlite3_SafeUnload(Tcl_Interp *interp, int flags){return TCL_ERROR;} 3683*076b9443SCy Schubert 3684*076b9443SCy Schubert 3685*076b9443SCy Schubert 3686*076b9443SCy Schubert #ifndef SQLITE_3_SUFFIX_ONLY 3687*076b9443SCy Schubert int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3688*076b9443SCy Schubert int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3689*076b9443SCy Schubert int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3690*076b9443SCy Schubert int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3691*076b9443SCy Schubert #endif 3692*076b9443SCy Schubert 3693*076b9443SCy Schubert /* 3694*076b9443SCy Schubert ** If the TCLSH macro is defined, add code to make a stand-alone program. 3695*076b9443SCy Schubert */ 3696*076b9443SCy Schubert #if defined(TCLSH) 3697*076b9443SCy Schubert 3698*076b9443SCy Schubert /* This is the main routine for an ordinary TCL shell. If there are 3699*076b9443SCy Schubert ** are arguments, run the first argument as a script. Otherwise, 3700*076b9443SCy Schubert ** read TCL commands from standard input 3701*076b9443SCy Schubert */ 3702*076b9443SCy Schubert static const char *tclsh_main_loop(void){ 3703*076b9443SCy Schubert static const char zMainloop[] = 3704*076b9443SCy Schubert "if {[llength $argv]>=1} {\n" 3705*076b9443SCy Schubert "set argv0 [lindex $argv 0]\n" 3706*076b9443SCy Schubert "set argv [lrange $argv 1 end]\n" 3707*076b9443SCy Schubert "source $argv0\n" 3708*076b9443SCy Schubert "} else {\n" 3709*076b9443SCy Schubert "set line {}\n" 3710*076b9443SCy Schubert "while {![eof stdin]} {\n" 3711*076b9443SCy Schubert "if {$line!=\"\"} {\n" 3712*076b9443SCy Schubert "puts -nonewline \"> \"\n" 3713*076b9443SCy Schubert "} else {\n" 3714*076b9443SCy Schubert "puts -nonewline \"% \"\n" 3715*076b9443SCy Schubert "}\n" 3716*076b9443SCy Schubert "flush stdout\n" 3717*076b9443SCy Schubert "append line [gets stdin]\n" 3718*076b9443SCy Schubert "if {[info complete $line]} {\n" 3719*076b9443SCy Schubert "if {[catch {uplevel #0 $line} result]} {\n" 3720*076b9443SCy Schubert "puts stderr \"Error: $result\"\n" 3721*076b9443SCy Schubert "} elseif {$result!=\"\"} {\n" 3722*076b9443SCy Schubert "puts $result\n" 3723*076b9443SCy Schubert "}\n" 3724*076b9443SCy Schubert "set line {}\n" 3725*076b9443SCy Schubert "} else {\n" 3726*076b9443SCy Schubert "append line \\n\n" 3727*076b9443SCy Schubert "}\n" 3728*076b9443SCy Schubert "}\n" 3729*076b9443SCy Schubert "}\n" 3730*076b9443SCy Schubert ; 3731*076b9443SCy Schubert return zMainloop; 3732*076b9443SCy Schubert } 3733*076b9443SCy Schubert 3734*076b9443SCy Schubert #define TCLSH_MAIN main /* Needed to fake out mktclapp */ 3735*076b9443SCy Schubert int SQLITE_CDECL TCLSH_MAIN(int argc, char **argv){ 3736*076b9443SCy Schubert Tcl_Interp *interp; 3737*076b9443SCy Schubert int i; 3738*076b9443SCy Schubert const char *zScript = 0; 3739*076b9443SCy Schubert char zArgc[32]; 3740*076b9443SCy Schubert #if defined(TCLSH_INIT_PROC) 3741*076b9443SCy Schubert extern const char *TCLSH_INIT_PROC(Tcl_Interp*); 3742*076b9443SCy Schubert #endif 3743*076b9443SCy Schubert 3744*076b9443SCy Schubert #if !defined(_WIN32_WCE) 3745*076b9443SCy Schubert if( getenv("SQLITE_DEBUG_BREAK") ){ 3746*076b9443SCy Schubert if( isatty(0) && isatty(2) ){ 3747*076b9443SCy Schubert fprintf(stderr, 3748*076b9443SCy Schubert "attach debugger to process %d and press any key to continue.\n", 3749*076b9443SCy Schubert GETPID()); 3750*076b9443SCy Schubert fgetc(stdin); 3751*076b9443SCy Schubert }else{ 3752*076b9443SCy Schubert #if defined(_WIN32) || defined(WIN32) 3753*076b9443SCy Schubert DebugBreak(); 3754*076b9443SCy Schubert #elif defined(SIGTRAP) 3755*076b9443SCy Schubert raise(SIGTRAP); 3756*076b9443SCy Schubert #endif 3757*076b9443SCy Schubert } 3758*076b9443SCy Schubert } 3759*076b9443SCy Schubert #endif 3760*076b9443SCy Schubert 3761*076b9443SCy Schubert /* Call sqlite3_shutdown() once before doing anything else. This is to 3762*076b9443SCy Schubert ** test that sqlite3_shutdown() can be safely called by a process before 3763*076b9443SCy Schubert ** sqlite3_initialize() is. */ 3764*076b9443SCy Schubert sqlite3_shutdown(); 3765*076b9443SCy Schubert 3766*076b9443SCy Schubert Tcl_FindExecutable(argv[0]); 3767*076b9443SCy Schubert Tcl_SetSystemEncoding(NULL, "utf-8"); 3768*076b9443SCy Schubert interp = Tcl_CreateInterp(); 3769*076b9443SCy Schubert Sqlite3_Init(interp); 3770*076b9443SCy Schubert 3771*076b9443SCy Schubert sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-1); 3772*076b9443SCy Schubert Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY); 3773*076b9443SCy Schubert Tcl_SetVar(interp,"argv0",argv[0],TCL_GLOBAL_ONLY); 3774*076b9443SCy Schubert Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); 3775*076b9443SCy Schubert for(i=1; i<argc; i++){ 3776*076b9443SCy Schubert Tcl_SetVar(interp, "argv", argv[i], 3777*076b9443SCy Schubert TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); 3778*076b9443SCy Schubert } 3779*076b9443SCy Schubert #if defined(TCLSH_INIT_PROC) 3780*076b9443SCy Schubert zScript = TCLSH_INIT_PROC(interp); 3781*076b9443SCy Schubert #endif 3782*076b9443SCy Schubert if( zScript==0 ){ 3783*076b9443SCy Schubert zScript = tclsh_main_loop(); 3784*076b9443SCy Schubert } 3785*076b9443SCy Schubert if( Tcl_GlobalEval(interp, zScript)!=TCL_OK ){ 3786*076b9443SCy Schubert const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); 3787*076b9443SCy Schubert if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp); 3788*076b9443SCy Schubert fprintf(stderr,"%s: %s\n", *argv, zInfo); 3789*076b9443SCy Schubert return 1; 3790*076b9443SCy Schubert } 3791*076b9443SCy Schubert return 0; 3792*076b9443SCy Schubert } 3793*076b9443SCy Schubert #endif /* TCLSH */ 3794