175897234Sdrh /* 2b19a2bc6Sdrh ** 2001 September 15 375897234Sdrh ** 4b19a2bc6Sdrh ** The author disclaims copyright to this source code. In place of 5b19a2bc6Sdrh ** a legal notice, here is a blessing: 675897234Sdrh ** 7b19a2bc6Sdrh ** May you do good and not evil. 8b19a2bc6Sdrh ** May you find forgiveness for yourself and forgive others. 9b19a2bc6Sdrh ** May you share freely, never taking more than you give. 1075897234Sdrh ** 1175897234Sdrh ************************************************************************* 12bd08af48Sdrh ** A TCL Interface to SQLite. Append this file to sqlite3.c and 13bd08af48Sdrh ** compile the whole thing to build a TCL-enabled version of SQLite. 1457a0227fSdrh ** 1557a0227fSdrh ** Compile-time options: 1657a0227fSdrh ** 1757a0227fSdrh ** -DTCLSH=1 Add a "main()" routine that works as a tclsh. 1857a0227fSdrh ** 1957a0227fSdrh ** -DSQLITE_TCLMD5 When used in conjuction with -DTCLSH=1, add 2057a0227fSdrh ** four new commands to the TCL interpreter for 2157a0227fSdrh ** generating MD5 checksums: md5, md5file, 2257a0227fSdrh ** md5-10x8, and md5file-10x8. 2357a0227fSdrh ** 2457a0227fSdrh ** -DSQLITE_TEST When used in conjuction with -DTCLSH=1, add 2557a0227fSdrh ** hundreds of new commands used for testing 2657a0227fSdrh ** SQLite. This option implies -DSQLITE_TCLMD5. 2775897234Sdrh */ 2827b2f053Smistachkin 2927b2f053Smistachkin /* 3027b2f053Smistachkin ** If requested, include the SQLite compiler options file for MSVC. 3127b2f053Smistachkin */ 3227b2f053Smistachkin #if defined(INCLUDE_MSVC_H) 3327b2f053Smistachkin #include "msvc.h" 3427b2f053Smistachkin #endif 3527b2f053Smistachkin 36bd08af48Sdrh #include "tcl.h" 37b4e9af9fSdanielk1977 #include <errno.h> 386d31316cSdrh 39bd08af48Sdrh /* 40bd08af48Sdrh ** Some additional include files are needed if this file is not 41bd08af48Sdrh ** appended to the amalgamation. 42bd08af48Sdrh */ 43bd08af48Sdrh #ifndef SQLITE_AMALGAMATION 4465e8c82eSdrh # include "sqlite3.h" 4575897234Sdrh # include <stdlib.h> 4675897234Sdrh # include <string.h> 47ce927065Sdrh # include <assert.h> 4865e8c82eSdrh typedef unsigned char u8; 49bd08af48Sdrh #endif 50eb206381Sdrh #include <ctype.h> 5175897234Sdrh 521f28e070Smistachkin /* Used to get the current process ID */ 531f28e070Smistachkin #if !defined(_WIN32) 541f28e070Smistachkin # include <unistd.h> 551f28e070Smistachkin # define GETPID getpid 561f28e070Smistachkin #elif !defined(_WIN32_WCE) 571f28e070Smistachkin # ifndef SQLITE_AMALGAMATION 581f28e070Smistachkin # define WIN32_LEAN_AND_MEAN 591f28e070Smistachkin # include <windows.h> 601f28e070Smistachkin # endif 611f28e070Smistachkin # define GETPID (int)GetCurrentProcessId 621f28e070Smistachkin #endif 631f28e070Smistachkin 64ad6e1370Sdrh /* 65ad6e1370Sdrh * Windows needs to know which symbols to export. Unix does not. 66ad6e1370Sdrh * BUILD_sqlite should be undefined for Unix. 67ad6e1370Sdrh */ 68ad6e1370Sdrh #ifdef BUILD_sqlite 69ad6e1370Sdrh #undef TCL_STORAGE_CLASS 70ad6e1370Sdrh #define TCL_STORAGE_CLASS DLLEXPORT 71ad6e1370Sdrh #endif /* BUILD_sqlite */ 7229bc4615Sdrh 73a21c6b6fSdanielk1977 #define NUM_PREPARED_STMTS 10 74fb7e7651Sdrh #define MAX_PREPARED_STMTS 100 75fb7e7651Sdrh 76c45e6716Sdrh /* Forward declaration */ 77c45e6716Sdrh typedef struct SqliteDb SqliteDb; 7898808babSdrh 7998808babSdrh /* 80cabb0819Sdrh ** New SQL functions can be created as TCL scripts. Each such function 81cabb0819Sdrh ** is described by an instance of the following structure. 82cabb0819Sdrh */ 83cabb0819Sdrh typedef struct SqlFunc SqlFunc; 84cabb0819Sdrh struct SqlFunc { 85cabb0819Sdrh Tcl_Interp *interp; /* The TCL interpret to execute the function */ 86d1e4733dSdrh Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */ 87c45e6716Sdrh SqliteDb *pDb; /* Database connection that owns this function */ 88d1e4733dSdrh int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */ 89d1e4733dSdrh char *zName; /* Name of this function */ 90cabb0819Sdrh SqlFunc *pNext; /* Next function on the list of them all */ 91cabb0819Sdrh }; 92cabb0819Sdrh 93cabb0819Sdrh /* 940202b29eSdanielk1977 ** New collation sequences function can be created as TCL scripts. Each such 950202b29eSdanielk1977 ** function is described by an instance of the following structure. 960202b29eSdanielk1977 */ 970202b29eSdanielk1977 typedef struct SqlCollate SqlCollate; 980202b29eSdanielk1977 struct SqlCollate { 990202b29eSdanielk1977 Tcl_Interp *interp; /* The TCL interpret to execute the function */ 1000202b29eSdanielk1977 char *zScript; /* The script to be run */ 1010202b29eSdanielk1977 SqlCollate *pNext; /* Next function on the list of them all */ 1020202b29eSdanielk1977 }; 1030202b29eSdanielk1977 1040202b29eSdanielk1977 /* 105fb7e7651Sdrh ** Prepared statements are cached for faster execution. Each prepared 106fb7e7651Sdrh ** statement is described by an instance of the following structure. 107fb7e7651Sdrh */ 108fb7e7651Sdrh typedef struct SqlPreparedStmt SqlPreparedStmt; 109fb7e7651Sdrh struct SqlPreparedStmt { 110fb7e7651Sdrh SqlPreparedStmt *pNext; /* Next in linked list */ 111fb7e7651Sdrh SqlPreparedStmt *pPrev; /* Previous on the list */ 112fb7e7651Sdrh sqlite3_stmt *pStmt; /* The prepared statement */ 113fb7e7651Sdrh int nSql; /* chars in zSql[] */ 114d0e2a854Sdanielk1977 const char *zSql; /* Text of the SQL statement */ 1154a4c11aaSdan int nParm; /* Size of apParm array */ 1164a4c11aaSdan Tcl_Obj **apParm; /* Array of referenced object pointers */ 117fb7e7651Sdrh }; 118fb7e7651Sdrh 119d0441796Sdanielk1977 typedef struct IncrblobChannel IncrblobChannel; 120d0441796Sdanielk1977 121fb7e7651Sdrh /* 122bec3f402Sdrh ** There is one instance of this structure for each SQLite database 123bec3f402Sdrh ** that has been opened by the SQLite TCL interface. 124c431fd55Sdan ** 125c431fd55Sdan ** If this module is built with SQLITE_TEST defined (to create the SQLite 126c431fd55Sdan ** testfixture executable), then it may be configured to use either 127c431fd55Sdan ** sqlite3_prepare_v2() or sqlite3_prepare() to prepare SQL statements. 128c431fd55Sdan ** If SqliteDb.bLegacyPrepare is true, sqlite3_prepare() is used. 129bec3f402Sdrh */ 130bec3f402Sdrh struct SqliteDb { 131dddca286Sdrh sqlite3 *db; /* The "real" database structure. MUST BE FIRST */ 132bec3f402Sdrh Tcl_Interp *interp; /* The interpreter used for this database */ 1336d31316cSdrh char *zBusy; /* The busy callback routine */ 134aa940eacSdrh char *zCommit; /* The commit hook callback routine */ 135b5a20d3cSdrh char *zTrace; /* The trace callback routine */ 136b56660f5Smistachkin char *zTraceV2; /* The trace_v2 callback routine */ 13719e2d37fSdrh char *zProfile; /* The profile callback routine */ 138348bb5d6Sdanielk1977 char *zProgress; /* The progress callback routine */ 139e22a334bSdrh char *zAuth; /* The authorization callback routine */ 1401f1549f8Sdrh int disableAuth; /* Disable the authorizer if it exists */ 14155c45f2eSdanielk1977 char *zNull; /* Text to substitute for an SQL NULL value */ 142cabb0819Sdrh SqlFunc *pFunc; /* List of SQL functions */ 14394eb6a14Sdanielk1977 Tcl_Obj *pUpdateHook; /* Update hook script (if any) */ 14446c47d46Sdan Tcl_Obj *pPreUpdateHook; /* Pre-update hook script (if any) */ 14571fd80bfSdanielk1977 Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */ 1465def0843Sdrh Tcl_Obj *pWalHook; /* WAL hook script (if any) */ 147404ca075Sdanielk1977 Tcl_Obj *pUnlockNotify; /* Unlock notify script (if any) */ 1480202b29eSdanielk1977 SqlCollate *pCollate; /* List of SQL collation functions */ 1496f8a503dSdanielk1977 int rc; /* Return code of most recent sqlite3_exec() */ 1507cedc8d4Sdanielk1977 Tcl_Obj *pCollateNeeded; /* Collation needed script */ 151fb7e7651Sdrh SqlPreparedStmt *stmtList; /* List of prepared statements*/ 152fb7e7651Sdrh SqlPreparedStmt *stmtLast; /* Last statement in the list */ 153fb7e7651Sdrh int maxStmt; /* The next maximum number of stmtList */ 154fb7e7651Sdrh int nStmt; /* Number of statements in stmtList */ 155d0441796Sdanielk1977 IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */ 1563c379b01Sdrh int nStep, nSort, nIndex; /* Statistics for most recent operation */ 157cd38d520Sdanielk1977 int nTransaction; /* Number of nested [transaction] methods */ 158147ef394Sdrh int openFlags; /* Flags used to open. (SQLITE_OPEN_URI) */ 159c431fd55Sdan #ifdef SQLITE_TEST 160c431fd55Sdan int bLegacyPrepare; /* True to use sqlite3_prepare() */ 161c431fd55Sdan #endif 16298808babSdrh }; 163297ecf14Sdrh 164b4e9af9fSdanielk1977 struct IncrblobChannel { 165d0441796Sdanielk1977 sqlite3_blob *pBlob; /* sqlite3 blob handle */ 166dcbb5d3fSdanielk1977 SqliteDb *pDb; /* Associated database connection */ 167b4e9af9fSdanielk1977 int iSeek; /* Current seek offset */ 168d0441796Sdanielk1977 Tcl_Channel channel; /* Channel identifier */ 169d0441796Sdanielk1977 IncrblobChannel *pNext; /* Linked list of all open incrblob channels */ 170d0441796Sdanielk1977 IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */ 171b4e9af9fSdanielk1977 }; 172b4e9af9fSdanielk1977 173ea678832Sdrh /* 174ea678832Sdrh ** Compute a string length that is limited to what can be stored in 175ea678832Sdrh ** lower 30 bits of a 32-bit signed integer. 176ea678832Sdrh */ 1774f21c4afSdrh static int strlen30(const char *z){ 178ea678832Sdrh const char *z2 = z; 179ea678832Sdrh while( *z2 ){ z2++; } 180ea678832Sdrh return 0x3fffffff & (int)(z2 - z); 181ea678832Sdrh } 182ea678832Sdrh 183ea678832Sdrh 18432a0d8bbSdanielk1977 #ifndef SQLITE_OMIT_INCRBLOB 185b4e9af9fSdanielk1977 /* 186d0441796Sdanielk1977 ** Close all incrblob channels opened using database connection pDb. 187d0441796Sdanielk1977 ** This is called when shutting down the database connection. 188d0441796Sdanielk1977 */ 189d0441796Sdanielk1977 static void closeIncrblobChannels(SqliteDb *pDb){ 190d0441796Sdanielk1977 IncrblobChannel *p; 191d0441796Sdanielk1977 IncrblobChannel *pNext; 192d0441796Sdanielk1977 193d0441796Sdanielk1977 for(p=pDb->pIncrblob; p; p=pNext){ 194d0441796Sdanielk1977 pNext = p->pNext; 195d0441796Sdanielk1977 196d0441796Sdanielk1977 /* Note: Calling unregister here call Tcl_Close on the incrblob channel, 197d0441796Sdanielk1977 ** which deletes the IncrblobChannel structure at *p. So do not 198d0441796Sdanielk1977 ** call Tcl_Free() here. 199d0441796Sdanielk1977 */ 200d0441796Sdanielk1977 Tcl_UnregisterChannel(pDb->interp, p->channel); 201d0441796Sdanielk1977 } 202d0441796Sdanielk1977 } 203d0441796Sdanielk1977 204d0441796Sdanielk1977 /* 205b4e9af9fSdanielk1977 ** Close an incremental blob channel. 206b4e9af9fSdanielk1977 */ 207b4e9af9fSdanielk1977 static int incrblobClose(ClientData instanceData, Tcl_Interp *interp){ 208b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 20992d4d7a9Sdanielk1977 int rc = sqlite3_blob_close(p->pBlob); 21092d4d7a9Sdanielk1977 sqlite3 *db = p->pDb->db; 211d0441796Sdanielk1977 212d0441796Sdanielk1977 /* Remove the channel from the SqliteDb.pIncrblob list. */ 213d0441796Sdanielk1977 if( p->pNext ){ 214d0441796Sdanielk1977 p->pNext->pPrev = p->pPrev; 215d0441796Sdanielk1977 } 216d0441796Sdanielk1977 if( p->pPrev ){ 217d0441796Sdanielk1977 p->pPrev->pNext = p->pNext; 218d0441796Sdanielk1977 } 219d0441796Sdanielk1977 if( p->pDb->pIncrblob==p ){ 220d0441796Sdanielk1977 p->pDb->pIncrblob = p->pNext; 221d0441796Sdanielk1977 } 222d0441796Sdanielk1977 22392d4d7a9Sdanielk1977 /* Free the IncrblobChannel structure */ 224b4e9af9fSdanielk1977 Tcl_Free((char *)p); 22592d4d7a9Sdanielk1977 22692d4d7a9Sdanielk1977 if( rc!=SQLITE_OK ){ 22792d4d7a9Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); 22892d4d7a9Sdanielk1977 return TCL_ERROR; 22992d4d7a9Sdanielk1977 } 230b4e9af9fSdanielk1977 return TCL_OK; 231b4e9af9fSdanielk1977 } 232b4e9af9fSdanielk1977 233b4e9af9fSdanielk1977 /* 234b4e9af9fSdanielk1977 ** Read data from an incremental blob channel. 235b4e9af9fSdanielk1977 */ 236b4e9af9fSdanielk1977 static int incrblobInput( 237b4e9af9fSdanielk1977 ClientData instanceData, 238b4e9af9fSdanielk1977 char *buf, 239b4e9af9fSdanielk1977 int bufSize, 240b4e9af9fSdanielk1977 int *errorCodePtr 241b4e9af9fSdanielk1977 ){ 242b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 243b4e9af9fSdanielk1977 int nRead = bufSize; /* Number of bytes to read */ 244b4e9af9fSdanielk1977 int nBlob; /* Total size of the blob */ 245b4e9af9fSdanielk1977 int rc; /* sqlite error code */ 246b4e9af9fSdanielk1977 247b4e9af9fSdanielk1977 nBlob = sqlite3_blob_bytes(p->pBlob); 248b4e9af9fSdanielk1977 if( (p->iSeek+nRead)>nBlob ){ 249b4e9af9fSdanielk1977 nRead = nBlob-p->iSeek; 250b4e9af9fSdanielk1977 } 251b4e9af9fSdanielk1977 if( nRead<=0 ){ 252b4e9af9fSdanielk1977 return 0; 253b4e9af9fSdanielk1977 } 254b4e9af9fSdanielk1977 255b4e9af9fSdanielk1977 rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek); 256b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 257b4e9af9fSdanielk1977 *errorCodePtr = rc; 258b4e9af9fSdanielk1977 return -1; 259b4e9af9fSdanielk1977 } 260b4e9af9fSdanielk1977 261b4e9af9fSdanielk1977 p->iSeek += nRead; 262b4e9af9fSdanielk1977 return nRead; 263b4e9af9fSdanielk1977 } 264b4e9af9fSdanielk1977 265d0441796Sdanielk1977 /* 266d0441796Sdanielk1977 ** Write data to an incremental blob channel. 267d0441796Sdanielk1977 */ 268b4e9af9fSdanielk1977 static int incrblobOutput( 269b4e9af9fSdanielk1977 ClientData instanceData, 270b4e9af9fSdanielk1977 CONST char *buf, 271b4e9af9fSdanielk1977 int toWrite, 272b4e9af9fSdanielk1977 int *errorCodePtr 273b4e9af9fSdanielk1977 ){ 274b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 275b4e9af9fSdanielk1977 int nWrite = toWrite; /* Number of bytes to write */ 276b4e9af9fSdanielk1977 int nBlob; /* Total size of the blob */ 277b4e9af9fSdanielk1977 int rc; /* sqlite error code */ 278b4e9af9fSdanielk1977 279b4e9af9fSdanielk1977 nBlob = sqlite3_blob_bytes(p->pBlob); 280b4e9af9fSdanielk1977 if( (p->iSeek+nWrite)>nBlob ){ 281b4e9af9fSdanielk1977 *errorCodePtr = EINVAL; 282b4e9af9fSdanielk1977 return -1; 283b4e9af9fSdanielk1977 } 284b4e9af9fSdanielk1977 if( nWrite<=0 ){ 285b4e9af9fSdanielk1977 return 0; 286b4e9af9fSdanielk1977 } 287b4e9af9fSdanielk1977 288b4e9af9fSdanielk1977 rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek); 289b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 290b4e9af9fSdanielk1977 *errorCodePtr = EIO; 291b4e9af9fSdanielk1977 return -1; 292b4e9af9fSdanielk1977 } 293b4e9af9fSdanielk1977 294b4e9af9fSdanielk1977 p->iSeek += nWrite; 295b4e9af9fSdanielk1977 return nWrite; 296b4e9af9fSdanielk1977 } 297b4e9af9fSdanielk1977 298b4e9af9fSdanielk1977 /* 299b4e9af9fSdanielk1977 ** Seek an incremental blob channel. 300b4e9af9fSdanielk1977 */ 301b4e9af9fSdanielk1977 static int incrblobSeek( 302b4e9af9fSdanielk1977 ClientData instanceData, 303b4e9af9fSdanielk1977 long offset, 304b4e9af9fSdanielk1977 int seekMode, 305b4e9af9fSdanielk1977 int *errorCodePtr 306b4e9af9fSdanielk1977 ){ 307b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 308b4e9af9fSdanielk1977 309b4e9af9fSdanielk1977 switch( seekMode ){ 310b4e9af9fSdanielk1977 case SEEK_SET: 311b4e9af9fSdanielk1977 p->iSeek = offset; 312b4e9af9fSdanielk1977 break; 313b4e9af9fSdanielk1977 case SEEK_CUR: 314b4e9af9fSdanielk1977 p->iSeek += offset; 315b4e9af9fSdanielk1977 break; 316b4e9af9fSdanielk1977 case SEEK_END: 317b4e9af9fSdanielk1977 p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset; 318b4e9af9fSdanielk1977 break; 319b4e9af9fSdanielk1977 320b4e9af9fSdanielk1977 default: assert(!"Bad seekMode"); 321b4e9af9fSdanielk1977 } 322b4e9af9fSdanielk1977 323b4e9af9fSdanielk1977 return p->iSeek; 324b4e9af9fSdanielk1977 } 325b4e9af9fSdanielk1977 326b4e9af9fSdanielk1977 327b4e9af9fSdanielk1977 static void incrblobWatch(ClientData instanceData, int mode){ 328b4e9af9fSdanielk1977 /* NO-OP */ 329b4e9af9fSdanielk1977 } 330b4e9af9fSdanielk1977 static int incrblobHandle(ClientData instanceData, int dir, ClientData *hPtr){ 331b4e9af9fSdanielk1977 return TCL_ERROR; 332b4e9af9fSdanielk1977 } 333b4e9af9fSdanielk1977 334b4e9af9fSdanielk1977 static Tcl_ChannelType IncrblobChannelType = { 335b4e9af9fSdanielk1977 "incrblob", /* typeName */ 336b4e9af9fSdanielk1977 TCL_CHANNEL_VERSION_2, /* version */ 337b4e9af9fSdanielk1977 incrblobClose, /* closeProc */ 338b4e9af9fSdanielk1977 incrblobInput, /* inputProc */ 339b4e9af9fSdanielk1977 incrblobOutput, /* outputProc */ 340b4e9af9fSdanielk1977 incrblobSeek, /* seekProc */ 341b4e9af9fSdanielk1977 0, /* setOptionProc */ 342b4e9af9fSdanielk1977 0, /* getOptionProc */ 343b4e9af9fSdanielk1977 incrblobWatch, /* watchProc (this is a no-op) */ 344b4e9af9fSdanielk1977 incrblobHandle, /* getHandleProc (always returns error) */ 345b4e9af9fSdanielk1977 0, /* close2Proc */ 346b4e9af9fSdanielk1977 0, /* blockModeProc */ 347b4e9af9fSdanielk1977 0, /* flushProc */ 348b4e9af9fSdanielk1977 0, /* handlerProc */ 349b4e9af9fSdanielk1977 0, /* wideSeekProc */ 350b4e9af9fSdanielk1977 }; 351b4e9af9fSdanielk1977 352b4e9af9fSdanielk1977 /* 353b4e9af9fSdanielk1977 ** Create a new incrblob channel. 354b4e9af9fSdanielk1977 */ 355b4e9af9fSdanielk1977 static int createIncrblobChannel( 356b4e9af9fSdanielk1977 Tcl_Interp *interp, 357b4e9af9fSdanielk1977 SqliteDb *pDb, 358b4e9af9fSdanielk1977 const char *zDb, 359b4e9af9fSdanielk1977 const char *zTable, 360b4e9af9fSdanielk1977 const char *zColumn, 3618cbadb02Sdanielk1977 sqlite_int64 iRow, 3628cbadb02Sdanielk1977 int isReadonly 363b4e9af9fSdanielk1977 ){ 364b4e9af9fSdanielk1977 IncrblobChannel *p; 3658cbadb02Sdanielk1977 sqlite3 *db = pDb->db; 366b4e9af9fSdanielk1977 sqlite3_blob *pBlob; 367b4e9af9fSdanielk1977 int rc; 3688cbadb02Sdanielk1977 int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE); 369b4e9af9fSdanielk1977 370b4e9af9fSdanielk1977 /* This variable is used to name the channels: "incrblob_[incr count]" */ 371b4e9af9fSdanielk1977 static int count = 0; 372b4e9af9fSdanielk1977 char zChannel[64]; 373b4e9af9fSdanielk1977 3748cbadb02Sdanielk1977 rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob); 375b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 376b4e9af9fSdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 377b4e9af9fSdanielk1977 return TCL_ERROR; 378b4e9af9fSdanielk1977 } 379b4e9af9fSdanielk1977 380b4e9af9fSdanielk1977 p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel)); 381b4e9af9fSdanielk1977 p->iSeek = 0; 382b4e9af9fSdanielk1977 p->pBlob = pBlob; 383b4e9af9fSdanielk1977 3845bb3eb9bSdrh sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count); 385d0441796Sdanielk1977 p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags); 386d0441796Sdanielk1977 Tcl_RegisterChannel(interp, p->channel); 387b4e9af9fSdanielk1977 388d0441796Sdanielk1977 /* Link the new channel into the SqliteDb.pIncrblob list. */ 389d0441796Sdanielk1977 p->pNext = pDb->pIncrblob; 390d0441796Sdanielk1977 p->pPrev = 0; 391d0441796Sdanielk1977 if( p->pNext ){ 392d0441796Sdanielk1977 p->pNext->pPrev = p; 393d0441796Sdanielk1977 } 394d0441796Sdanielk1977 pDb->pIncrblob = p; 395d0441796Sdanielk1977 p->pDb = pDb; 396d0441796Sdanielk1977 397d0441796Sdanielk1977 Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE); 398b4e9af9fSdanielk1977 return TCL_OK; 399b4e9af9fSdanielk1977 } 40032a0d8bbSdanielk1977 #else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */ 40132a0d8bbSdanielk1977 #define closeIncrblobChannels(pDb) 40232a0d8bbSdanielk1977 #endif 403b4e9af9fSdanielk1977 4046d31316cSdrh /* 405d1e4733dSdrh ** Look at the script prefix in pCmd. We will be executing this script 406d1e4733dSdrh ** after first appending one or more arguments. This routine analyzes 407d1e4733dSdrh ** the script to see if it is safe to use Tcl_EvalObjv() on the script 408d1e4733dSdrh ** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much 409d1e4733dSdrh ** faster. 410d1e4733dSdrh ** 411d1e4733dSdrh ** Scripts that are safe to use with Tcl_EvalObjv() consists of a 412d1e4733dSdrh ** command name followed by zero or more arguments with no [...] or $ 413d1e4733dSdrh ** or {...} or ; to be seen anywhere. Most callback scripts consist 414d1e4733dSdrh ** of just a single procedure name and they meet this requirement. 415d1e4733dSdrh */ 416d1e4733dSdrh static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){ 417d1e4733dSdrh /* We could try to do something with Tcl_Parse(). But we will instead 418d1e4733dSdrh ** just do a search for forbidden characters. If any of the forbidden 419d1e4733dSdrh ** characters appear in pCmd, we will report the string as unsafe. 420d1e4733dSdrh */ 421d1e4733dSdrh const char *z; 422d1e4733dSdrh int n; 423d1e4733dSdrh z = Tcl_GetStringFromObj(pCmd, &n); 424d1e4733dSdrh while( n-- > 0 ){ 425d1e4733dSdrh int c = *(z++); 426d1e4733dSdrh if( c=='$' || c=='[' || c==';' ) return 0; 427d1e4733dSdrh } 428d1e4733dSdrh return 1; 429d1e4733dSdrh } 430d1e4733dSdrh 431d1e4733dSdrh /* 432d1e4733dSdrh ** Find an SqlFunc structure with the given name. Or create a new 433d1e4733dSdrh ** one if an existing one cannot be found. Return a pointer to the 434d1e4733dSdrh ** structure. 435d1e4733dSdrh */ 436d1e4733dSdrh static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){ 437d1e4733dSdrh SqlFunc *p, *pNew; 4380425f189Sdrh int nName = strlen30(zName); 4390425f189Sdrh pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + nName + 1 ); 440d1e4733dSdrh pNew->zName = (char*)&pNew[1]; 4410425f189Sdrh memcpy(pNew->zName, zName, nName+1); 442d1e4733dSdrh for(p=pDb->pFunc; p; p=p->pNext){ 4430425f189Sdrh if( sqlite3_stricmp(p->zName, pNew->zName)==0 ){ 444d1e4733dSdrh Tcl_Free((char*)pNew); 445d1e4733dSdrh return p; 446d1e4733dSdrh } 447d1e4733dSdrh } 448d1e4733dSdrh pNew->interp = pDb->interp; 449c45e6716Sdrh pNew->pDb = pDb; 450d1e4733dSdrh pNew->pScript = 0; 451d1e4733dSdrh pNew->pNext = pDb->pFunc; 452d1e4733dSdrh pDb->pFunc = pNew; 453d1e4733dSdrh return pNew; 454d1e4733dSdrh } 455d1e4733dSdrh 456d1e4733dSdrh /* 457c431fd55Sdan ** Free a single SqlPreparedStmt object. 458c431fd55Sdan */ 459c431fd55Sdan static void dbFreeStmt(SqlPreparedStmt *pStmt){ 460c431fd55Sdan #ifdef SQLITE_TEST 461c431fd55Sdan if( sqlite3_sql(pStmt->pStmt)==0 ){ 462c431fd55Sdan Tcl_Free((char *)pStmt->zSql); 463c431fd55Sdan } 464c431fd55Sdan #endif 465c431fd55Sdan sqlite3_finalize(pStmt->pStmt); 466c431fd55Sdan Tcl_Free((char *)pStmt); 467c431fd55Sdan } 468c431fd55Sdan 469c431fd55Sdan /* 470fb7e7651Sdrh ** Finalize and free a list of prepared statements 471fb7e7651Sdrh */ 472fb7e7651Sdrh static void flushStmtCache(SqliteDb *pDb){ 473fb7e7651Sdrh SqlPreparedStmt *pPreStmt; 474c431fd55Sdan SqlPreparedStmt *pNext; 475fb7e7651Sdrh 476c431fd55Sdan for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pNext){ 477c431fd55Sdan pNext = pPreStmt->pNext; 478c431fd55Sdan dbFreeStmt(pPreStmt); 479fb7e7651Sdrh } 480fb7e7651Sdrh pDb->nStmt = 0; 481fb7e7651Sdrh pDb->stmtLast = 0; 482c431fd55Sdan pDb->stmtList = 0; 483fb7e7651Sdrh } 484fb7e7651Sdrh 485fb7e7651Sdrh /* 486895d7472Sdrh ** TCL calls this procedure when an sqlite3 database command is 487895d7472Sdrh ** deleted. 48875897234Sdrh */ 48975897234Sdrh static void DbDeleteCmd(void *db){ 490bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)db; 491fb7e7651Sdrh flushStmtCache(pDb); 492d0441796Sdanielk1977 closeIncrblobChannels(pDb); 4936f8a503dSdanielk1977 sqlite3_close(pDb->db); 494cabb0819Sdrh while( pDb->pFunc ){ 495cabb0819Sdrh SqlFunc *pFunc = pDb->pFunc; 496cabb0819Sdrh pDb->pFunc = pFunc->pNext; 497c45e6716Sdrh assert( pFunc->pDb==pDb ); 498d1e4733dSdrh Tcl_DecrRefCount(pFunc->pScript); 499cabb0819Sdrh Tcl_Free((char*)pFunc); 500cabb0819Sdrh } 5010202b29eSdanielk1977 while( pDb->pCollate ){ 5020202b29eSdanielk1977 SqlCollate *pCollate = pDb->pCollate; 5030202b29eSdanielk1977 pDb->pCollate = pCollate->pNext; 5040202b29eSdanielk1977 Tcl_Free((char*)pCollate); 5050202b29eSdanielk1977 } 506bec3f402Sdrh if( pDb->zBusy ){ 507bec3f402Sdrh Tcl_Free(pDb->zBusy); 508bec3f402Sdrh } 509b5a20d3cSdrh if( pDb->zTrace ){ 510b5a20d3cSdrh Tcl_Free(pDb->zTrace); 5110d1a643aSdrh } 512b56660f5Smistachkin if( pDb->zTraceV2 ){ 513b56660f5Smistachkin Tcl_Free(pDb->zTraceV2); 514b56660f5Smistachkin } 51519e2d37fSdrh if( pDb->zProfile ){ 51619e2d37fSdrh Tcl_Free(pDb->zProfile); 51719e2d37fSdrh } 518e22a334bSdrh if( pDb->zAuth ){ 519e22a334bSdrh Tcl_Free(pDb->zAuth); 520e22a334bSdrh } 52155c45f2eSdanielk1977 if( pDb->zNull ){ 52255c45f2eSdanielk1977 Tcl_Free(pDb->zNull); 52355c45f2eSdanielk1977 } 52494eb6a14Sdanielk1977 if( pDb->pUpdateHook ){ 52594eb6a14Sdanielk1977 Tcl_DecrRefCount(pDb->pUpdateHook); 52694eb6a14Sdanielk1977 } 52746c47d46Sdan if( pDb->pPreUpdateHook ){ 52846c47d46Sdan Tcl_DecrRefCount(pDb->pPreUpdateHook); 52946c47d46Sdan } 53071fd80bfSdanielk1977 if( pDb->pRollbackHook ){ 53171fd80bfSdanielk1977 Tcl_DecrRefCount(pDb->pRollbackHook); 53271fd80bfSdanielk1977 } 5335def0843Sdrh if( pDb->pWalHook ){ 5345def0843Sdrh Tcl_DecrRefCount(pDb->pWalHook); 5358d22a174Sdan } 53694eb6a14Sdanielk1977 if( pDb->pCollateNeeded ){ 53794eb6a14Sdanielk1977 Tcl_DecrRefCount(pDb->pCollateNeeded); 53894eb6a14Sdanielk1977 } 539bec3f402Sdrh Tcl_Free((char*)pDb); 540bec3f402Sdrh } 541bec3f402Sdrh 542bec3f402Sdrh /* 543bec3f402Sdrh ** This routine is called when a database file is locked while trying 544bec3f402Sdrh ** to execute SQL. 545bec3f402Sdrh */ 5462a764eb0Sdanielk1977 static int DbBusyHandler(void *cd, int nTries){ 547bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 548bec3f402Sdrh int rc; 549bec3f402Sdrh char zVal[30]; 550bec3f402Sdrh 5515bb3eb9bSdrh sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries); 552d1e4733dSdrh rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0); 553bec3f402Sdrh if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 554bec3f402Sdrh return 0; 555bec3f402Sdrh } 556bec3f402Sdrh return 1; 55775897234Sdrh } 55875897234Sdrh 55926e4a8b1Sdrh #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 56075897234Sdrh /* 561348bb5d6Sdanielk1977 ** This routine is invoked as the 'progress callback' for the database. 562348bb5d6Sdanielk1977 */ 563348bb5d6Sdanielk1977 static int DbProgressHandler(void *cd){ 564348bb5d6Sdanielk1977 SqliteDb *pDb = (SqliteDb*)cd; 565348bb5d6Sdanielk1977 int rc; 566348bb5d6Sdanielk1977 567348bb5d6Sdanielk1977 assert( pDb->zProgress ); 568348bb5d6Sdanielk1977 rc = Tcl_Eval(pDb->interp, pDb->zProgress); 569348bb5d6Sdanielk1977 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 570348bb5d6Sdanielk1977 return 1; 571348bb5d6Sdanielk1977 } 572348bb5d6Sdanielk1977 return 0; 573348bb5d6Sdanielk1977 } 57426e4a8b1Sdrh #endif 575348bb5d6Sdanielk1977 576d1167393Sdrh #ifndef SQLITE_OMIT_TRACE 577348bb5d6Sdanielk1977 /* 578b5a20d3cSdrh ** This routine is called by the SQLite trace handler whenever a new 579b5a20d3cSdrh ** block of SQL is executed. The TCL script in pDb->zTrace is executed. 5800d1a643aSdrh */ 581b5a20d3cSdrh static void DbTraceHandler(void *cd, const char *zSql){ 5820d1a643aSdrh SqliteDb *pDb = (SqliteDb*)cd; 583b5a20d3cSdrh Tcl_DString str; 5840d1a643aSdrh 585b5a20d3cSdrh Tcl_DStringInit(&str); 586b5a20d3cSdrh Tcl_DStringAppend(&str, pDb->zTrace, -1); 587b5a20d3cSdrh Tcl_DStringAppendElement(&str, zSql); 588b5a20d3cSdrh Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 589b5a20d3cSdrh Tcl_DStringFree(&str); 590b5a20d3cSdrh Tcl_ResetResult(pDb->interp); 5910d1a643aSdrh } 592d1167393Sdrh #endif 5930d1a643aSdrh 594d1167393Sdrh #ifndef SQLITE_OMIT_TRACE 5950d1a643aSdrh /* 596b56660f5Smistachkin ** This routine is called by the SQLite trace_v2 handler whenever a new 597b56660f5Smistachkin ** supported event is generated. Unsupported event types are ignored. 598b56660f5Smistachkin ** The TCL script in pDb->zTraceV2 is executed, with the arguments for 599b56660f5Smistachkin ** the event appended to it (as list elements). 600b56660f5Smistachkin */ 601b56660f5Smistachkin static int DbTraceV2Handler( 602b56660f5Smistachkin unsigned type, /* One of the SQLITE_TRACE_* event types. */ 603b56660f5Smistachkin void *cd, /* The original context data pointer. */ 604b56660f5Smistachkin void *pd, /* Primary event data, depends on event type. */ 605b56660f5Smistachkin void *xd /* Extra event data, depends on event type. */ 606b56660f5Smistachkin ){ 607b56660f5Smistachkin SqliteDb *pDb = (SqliteDb*)cd; 608b56660f5Smistachkin Tcl_Obj *pCmd; 609b56660f5Smistachkin 610b56660f5Smistachkin switch( type ){ 611b56660f5Smistachkin case SQLITE_TRACE_STMT: { 612b56660f5Smistachkin sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; 613b56660f5Smistachkin char *zSql = (char *)xd; 614b56660f5Smistachkin 615b56660f5Smistachkin pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 616b56660f5Smistachkin Tcl_IncrRefCount(pCmd); 617b56660f5Smistachkin Tcl_ListObjAppendElement(pDb->interp, pCmd, 618b56660f5Smistachkin Tcl_NewWideIntObj((Tcl_WideInt)pStmt)); 619b56660f5Smistachkin Tcl_ListObjAppendElement(pDb->interp, pCmd, 620b56660f5Smistachkin Tcl_NewStringObj(zSql, -1)); 621b56660f5Smistachkin Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 622b56660f5Smistachkin Tcl_DecrRefCount(pCmd); 623b56660f5Smistachkin Tcl_ResetResult(pDb->interp); 624b56660f5Smistachkin break; 625b56660f5Smistachkin } 626b56660f5Smistachkin case SQLITE_TRACE_PROFILE: { 627b56660f5Smistachkin sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; 628b56660f5Smistachkin sqlite3_int64 ns = (sqlite3_int64)xd; 629b56660f5Smistachkin 630b56660f5Smistachkin pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 631b56660f5Smistachkin Tcl_IncrRefCount(pCmd); 632b56660f5Smistachkin Tcl_ListObjAppendElement(pDb->interp, pCmd, 633b56660f5Smistachkin Tcl_NewWideIntObj((Tcl_WideInt)pStmt)); 634b56660f5Smistachkin Tcl_ListObjAppendElement(pDb->interp, pCmd, 635b56660f5Smistachkin Tcl_NewWideIntObj((Tcl_WideInt)ns)); 636b56660f5Smistachkin Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 637b56660f5Smistachkin Tcl_DecrRefCount(pCmd); 638b56660f5Smistachkin Tcl_ResetResult(pDb->interp); 639b56660f5Smistachkin break; 640b56660f5Smistachkin } 641b56660f5Smistachkin case SQLITE_TRACE_ROW: { 642b56660f5Smistachkin sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; 643b56660f5Smistachkin 644b56660f5Smistachkin pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 645b56660f5Smistachkin Tcl_IncrRefCount(pCmd); 646b56660f5Smistachkin Tcl_ListObjAppendElement(pDb->interp, pCmd, 647b56660f5Smistachkin Tcl_NewWideIntObj((Tcl_WideInt)pStmt)); 648b56660f5Smistachkin Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 649b56660f5Smistachkin Tcl_DecrRefCount(pCmd); 650b56660f5Smistachkin Tcl_ResetResult(pDb->interp); 651b56660f5Smistachkin break; 652b56660f5Smistachkin } 653b56660f5Smistachkin case SQLITE_TRACE_CLOSE: { 654b56660f5Smistachkin sqlite3 *db = (sqlite3 *)pd; 655b56660f5Smistachkin 656b56660f5Smistachkin pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 657b56660f5Smistachkin Tcl_IncrRefCount(pCmd); 658b56660f5Smistachkin Tcl_ListObjAppendElement(pDb->interp, pCmd, 659b56660f5Smistachkin Tcl_NewWideIntObj((Tcl_WideInt)db)); 660b56660f5Smistachkin Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 661b56660f5Smistachkin Tcl_DecrRefCount(pCmd); 662b56660f5Smistachkin Tcl_ResetResult(pDb->interp); 663b56660f5Smistachkin break; 664b56660f5Smistachkin } 665b56660f5Smistachkin } 666b56660f5Smistachkin return SQLITE_OK; 667b56660f5Smistachkin } 668b56660f5Smistachkin #endif 669b56660f5Smistachkin 670b56660f5Smistachkin #ifndef SQLITE_OMIT_TRACE 671b56660f5Smistachkin /* 67219e2d37fSdrh ** This routine is called by the SQLite profile handler after a statement 67319e2d37fSdrh ** SQL has executed. The TCL script in pDb->zProfile is evaluated. 67419e2d37fSdrh */ 67519e2d37fSdrh static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){ 67619e2d37fSdrh SqliteDb *pDb = (SqliteDb*)cd; 67719e2d37fSdrh Tcl_DString str; 67819e2d37fSdrh char zTm[100]; 67919e2d37fSdrh 68019e2d37fSdrh sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm); 68119e2d37fSdrh Tcl_DStringInit(&str); 68219e2d37fSdrh Tcl_DStringAppend(&str, pDb->zProfile, -1); 68319e2d37fSdrh Tcl_DStringAppendElement(&str, zSql); 68419e2d37fSdrh Tcl_DStringAppendElement(&str, zTm); 68519e2d37fSdrh Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 68619e2d37fSdrh Tcl_DStringFree(&str); 68719e2d37fSdrh Tcl_ResetResult(pDb->interp); 68819e2d37fSdrh } 689d1167393Sdrh #endif 69019e2d37fSdrh 69119e2d37fSdrh /* 692aa940eacSdrh ** This routine is called when a transaction is committed. The 693aa940eacSdrh ** TCL script in pDb->zCommit is executed. If it returns non-zero or 694aa940eacSdrh ** if it throws an exception, the transaction is rolled back instead 695aa940eacSdrh ** of being committed. 696aa940eacSdrh */ 697aa940eacSdrh static int DbCommitHandler(void *cd){ 698aa940eacSdrh SqliteDb *pDb = (SqliteDb*)cd; 699aa940eacSdrh int rc; 700aa940eacSdrh 701aa940eacSdrh rc = Tcl_Eval(pDb->interp, pDb->zCommit); 702aa940eacSdrh if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 703aa940eacSdrh return 1; 704aa940eacSdrh } 705aa940eacSdrh return 0; 706aa940eacSdrh } 707aa940eacSdrh 70871fd80bfSdanielk1977 static void DbRollbackHandler(void *clientData){ 70971fd80bfSdanielk1977 SqliteDb *pDb = (SqliteDb*)clientData; 71071fd80bfSdanielk1977 assert(pDb->pRollbackHook); 71171fd80bfSdanielk1977 if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){ 71271fd80bfSdanielk1977 Tcl_BackgroundError(pDb->interp); 71371fd80bfSdanielk1977 } 71471fd80bfSdanielk1977 } 71571fd80bfSdanielk1977 7165def0843Sdrh /* 7175def0843Sdrh ** This procedure handles wal_hook callbacks. 7185def0843Sdrh */ 7195def0843Sdrh static int DbWalHandler( 7208d22a174Sdan void *clientData, 7218d22a174Sdan sqlite3 *db, 7228d22a174Sdan const char *zDb, 7238d22a174Sdan int nEntry 7248d22a174Sdan ){ 7255def0843Sdrh int ret = SQLITE_OK; 7268d22a174Sdan Tcl_Obj *p; 7278d22a174Sdan SqliteDb *pDb = (SqliteDb*)clientData; 7288d22a174Sdan Tcl_Interp *interp = pDb->interp; 7295def0843Sdrh assert(pDb->pWalHook); 7308d22a174Sdan 7316e45e0c8Sdan assert( db==pDb->db ); 7325def0843Sdrh p = Tcl_DuplicateObj(pDb->pWalHook); 7338d22a174Sdan Tcl_IncrRefCount(p); 7348d22a174Sdan Tcl_ListObjAppendElement(interp, p, Tcl_NewStringObj(zDb, -1)); 7358d22a174Sdan Tcl_ListObjAppendElement(interp, p, Tcl_NewIntObj(nEntry)); 7368d22a174Sdan if( TCL_OK!=Tcl_EvalObjEx(interp, p, 0) 7378d22a174Sdan || TCL_OK!=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &ret) 7388d22a174Sdan ){ 7398d22a174Sdan Tcl_BackgroundError(interp); 7408d22a174Sdan } 7418d22a174Sdan Tcl_DecrRefCount(p); 7428d22a174Sdan 7438d22a174Sdan return ret; 7448d22a174Sdan } 7458d22a174Sdan 746bcf4f484Sdrh #if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_UNLOCK_NOTIFY) 747404ca075Sdanielk1977 static void setTestUnlockNotifyVars(Tcl_Interp *interp, int iArg, int nArg){ 748404ca075Sdanielk1977 char zBuf[64]; 74965545b59Sdrh sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", iArg); 750404ca075Sdanielk1977 Tcl_SetVar(interp, "sqlite_unlock_notify_arg", zBuf, TCL_GLOBAL_ONLY); 75165545b59Sdrh sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nArg); 752404ca075Sdanielk1977 Tcl_SetVar(interp, "sqlite_unlock_notify_argcount", zBuf, TCL_GLOBAL_ONLY); 753404ca075Sdanielk1977 } 754404ca075Sdanielk1977 #else 755404ca075Sdanielk1977 # define setTestUnlockNotifyVars(x,y,z) 756404ca075Sdanielk1977 #endif 757404ca075Sdanielk1977 75869910da9Sdrh #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 759404ca075Sdanielk1977 static void DbUnlockNotify(void **apArg, int nArg){ 760404ca075Sdanielk1977 int i; 761404ca075Sdanielk1977 for(i=0; i<nArg; i++){ 762404ca075Sdanielk1977 const int flags = (TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); 763404ca075Sdanielk1977 SqliteDb *pDb = (SqliteDb *)apArg[i]; 764404ca075Sdanielk1977 setTestUnlockNotifyVars(pDb->interp, i, nArg); 765404ca075Sdanielk1977 assert( pDb->pUnlockNotify); 766404ca075Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pDb->pUnlockNotify, flags); 767404ca075Sdanielk1977 Tcl_DecrRefCount(pDb->pUnlockNotify); 768404ca075Sdanielk1977 pDb->pUnlockNotify = 0; 769404ca075Sdanielk1977 } 770404ca075Sdanielk1977 } 77169910da9Sdrh #endif 772404ca075Sdanielk1977 7739b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 77446c47d46Sdan /* 77546c47d46Sdan ** Pre-update hook callback. 77646c47d46Sdan */ 77746c47d46Sdan static void DbPreUpdateHandler( 77846c47d46Sdan void *p, 77946c47d46Sdan sqlite3 *db, 78046c47d46Sdan int op, 78146c47d46Sdan const char *zDb, 78246c47d46Sdan const char *zTbl, 78346c47d46Sdan sqlite_int64 iKey1, 78446c47d46Sdan sqlite_int64 iKey2 78546c47d46Sdan ){ 78646c47d46Sdan SqliteDb *pDb = (SqliteDb *)p; 78746c47d46Sdan Tcl_Obj *pCmd; 78846c47d46Sdan static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"}; 78946c47d46Sdan 79046c47d46Sdan assert( (SQLITE_DELETE-1)/9 == 0 ); 79146c47d46Sdan assert( (SQLITE_INSERT-1)/9 == 1 ); 79246c47d46Sdan assert( (SQLITE_UPDATE-1)/9 == 2 ); 79346c47d46Sdan assert( pDb->pPreUpdateHook ); 79446c47d46Sdan assert( db==pDb->db ); 79546c47d46Sdan assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); 79646c47d46Sdan 79746c47d46Sdan pCmd = Tcl_DuplicateObj(pDb->pPreUpdateHook); 79846c47d46Sdan Tcl_IncrRefCount(pCmd); 79946c47d46Sdan Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1)); 80046c47d46Sdan Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); 80146c47d46Sdan Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); 80246c47d46Sdan Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey1)); 80346c47d46Sdan Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey2)); 80446c47d46Sdan Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 80546c47d46Sdan Tcl_DecrRefCount(pCmd); 80646c47d46Sdan } 8079b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 80846c47d46Sdan 80994eb6a14Sdanielk1977 static void DbUpdateHandler( 81094eb6a14Sdanielk1977 void *p, 81194eb6a14Sdanielk1977 int op, 81294eb6a14Sdanielk1977 const char *zDb, 81394eb6a14Sdanielk1977 const char *zTbl, 81494eb6a14Sdanielk1977 sqlite_int64 rowid 81594eb6a14Sdanielk1977 ){ 81694eb6a14Sdanielk1977 SqliteDb *pDb = (SqliteDb *)p; 81794eb6a14Sdanielk1977 Tcl_Obj *pCmd; 81846c47d46Sdan static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"}; 81946c47d46Sdan 82046c47d46Sdan assert( (SQLITE_DELETE-1)/9 == 0 ); 82146c47d46Sdan assert( (SQLITE_INSERT-1)/9 == 1 ); 82246c47d46Sdan assert( (SQLITE_UPDATE-1)/9 == 2 ); 82394eb6a14Sdanielk1977 82494eb6a14Sdanielk1977 assert( pDb->pUpdateHook ); 82594eb6a14Sdanielk1977 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); 82694eb6a14Sdanielk1977 82794eb6a14Sdanielk1977 pCmd = Tcl_DuplicateObj(pDb->pUpdateHook); 82894eb6a14Sdanielk1977 Tcl_IncrRefCount(pCmd); 82946c47d46Sdan Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1)); 83094eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); 83194eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); 83294eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid)); 83394eb6a14Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 834efdde169Sdrh Tcl_DecrRefCount(pCmd); 83594eb6a14Sdanielk1977 } 83694eb6a14Sdanielk1977 8377cedc8d4Sdanielk1977 static void tclCollateNeeded( 8387cedc8d4Sdanielk1977 void *pCtx, 8399bb575fdSdrh sqlite3 *db, 8407cedc8d4Sdanielk1977 int enc, 8417cedc8d4Sdanielk1977 const char *zName 8427cedc8d4Sdanielk1977 ){ 8437cedc8d4Sdanielk1977 SqliteDb *pDb = (SqliteDb *)pCtx; 8447cedc8d4Sdanielk1977 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded); 8457cedc8d4Sdanielk1977 Tcl_IncrRefCount(pScript); 8467cedc8d4Sdanielk1977 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1)); 8477cedc8d4Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pScript, 0); 8487cedc8d4Sdanielk1977 Tcl_DecrRefCount(pScript); 8497cedc8d4Sdanielk1977 } 8507cedc8d4Sdanielk1977 851aa940eacSdrh /* 8520202b29eSdanielk1977 ** This routine is called to evaluate an SQL collation function implemented 8530202b29eSdanielk1977 ** using TCL script. 8540202b29eSdanielk1977 */ 8550202b29eSdanielk1977 static int tclSqlCollate( 8560202b29eSdanielk1977 void *pCtx, 8570202b29eSdanielk1977 int nA, 8580202b29eSdanielk1977 const void *zA, 8590202b29eSdanielk1977 int nB, 8600202b29eSdanielk1977 const void *zB 8610202b29eSdanielk1977 ){ 8620202b29eSdanielk1977 SqlCollate *p = (SqlCollate *)pCtx; 8630202b29eSdanielk1977 Tcl_Obj *pCmd; 8640202b29eSdanielk1977 8650202b29eSdanielk1977 pCmd = Tcl_NewStringObj(p->zScript, -1); 8660202b29eSdanielk1977 Tcl_IncrRefCount(pCmd); 8670202b29eSdanielk1977 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA)); 8680202b29eSdanielk1977 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB)); 869d1e4733dSdrh Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 8700202b29eSdanielk1977 Tcl_DecrRefCount(pCmd); 8710202b29eSdanielk1977 return (atoi(Tcl_GetStringResult(p->interp))); 8720202b29eSdanielk1977 } 8730202b29eSdanielk1977 8740202b29eSdanielk1977 /* 875cabb0819Sdrh ** This routine is called to evaluate an SQL function implemented 876cabb0819Sdrh ** using TCL script. 877cabb0819Sdrh */ 8780ae8b831Sdanielk1977 static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ 8796f8a503dSdanielk1977 SqlFunc *p = sqlite3_user_data(context); 880d1e4733dSdrh Tcl_Obj *pCmd; 881cabb0819Sdrh int i; 882cabb0819Sdrh int rc; 883cabb0819Sdrh 884d1e4733dSdrh if( argc==0 ){ 885d1e4733dSdrh /* If there are no arguments to the function, call Tcl_EvalObjEx on the 886d1e4733dSdrh ** script object directly. This allows the TCL compiler to generate 887d1e4733dSdrh ** bytecode for the command on the first invocation and thus make 888d1e4733dSdrh ** subsequent invocations much faster. */ 889d1e4733dSdrh pCmd = p->pScript; 890d1e4733dSdrh Tcl_IncrRefCount(pCmd); 891d1e4733dSdrh rc = Tcl_EvalObjEx(p->interp, pCmd, 0); 892d1e4733dSdrh Tcl_DecrRefCount(pCmd); 89351ad0ecdSdanielk1977 }else{ 894d1e4733dSdrh /* If there are arguments to the function, make a shallow copy of the 895d1e4733dSdrh ** script object, lappend the arguments, then evaluate the copy. 896d1e4733dSdrh ** 89760ec914cSpeter.d.reid ** By "shallow" copy, we mean only the outer list Tcl_Obj is duplicated. 898d1e4733dSdrh ** The new Tcl_Obj contains pointers to the original list elements. 899d1e4733dSdrh ** That way, when Tcl_EvalObjv() is run and shimmers the first element 900d1e4733dSdrh ** of the list to tclCmdNameType, that alternate representation will 901d1e4733dSdrh ** be preserved and reused on the next invocation. 902d1e4733dSdrh */ 903d1e4733dSdrh Tcl_Obj **aArg; 904d1e4733dSdrh int nArg; 905d1e4733dSdrh if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){ 906d1e4733dSdrh sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 907d1e4733dSdrh return; 908d1e4733dSdrh } 909d1e4733dSdrh pCmd = Tcl_NewListObj(nArg, aArg); 910d1e4733dSdrh Tcl_IncrRefCount(pCmd); 911d1e4733dSdrh for(i=0; i<argc; i++){ 912d1e4733dSdrh sqlite3_value *pIn = argv[i]; 913d1e4733dSdrh Tcl_Obj *pVal; 914d1e4733dSdrh 915d1e4733dSdrh /* Set pVal to contain the i'th column of this row. */ 916d1e4733dSdrh switch( sqlite3_value_type(pIn) ){ 917d1e4733dSdrh case SQLITE_BLOB: { 918d1e4733dSdrh int bytes = sqlite3_value_bytes(pIn); 919d1e4733dSdrh pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes); 920d1e4733dSdrh break; 921d1e4733dSdrh } 922d1e4733dSdrh case SQLITE_INTEGER: { 923d1e4733dSdrh sqlite_int64 v = sqlite3_value_int64(pIn); 924d1e4733dSdrh if( v>=-2147483647 && v<=2147483647 ){ 9257fd33929Sdrh pVal = Tcl_NewIntObj((int)v); 926d1e4733dSdrh }else{ 927d1e4733dSdrh pVal = Tcl_NewWideIntObj(v); 928d1e4733dSdrh } 929d1e4733dSdrh break; 930d1e4733dSdrh } 931d1e4733dSdrh case SQLITE_FLOAT: { 932d1e4733dSdrh double r = sqlite3_value_double(pIn); 933d1e4733dSdrh pVal = Tcl_NewDoubleObj(r); 934d1e4733dSdrh break; 935d1e4733dSdrh } 936d1e4733dSdrh case SQLITE_NULL: { 937c45e6716Sdrh pVal = Tcl_NewStringObj(p->pDb->zNull, -1); 938d1e4733dSdrh break; 939d1e4733dSdrh } 940d1e4733dSdrh default: { 941d1e4733dSdrh int bytes = sqlite3_value_bytes(pIn); 94200fd957bSdanielk1977 pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes); 943d1e4733dSdrh break; 94451ad0ecdSdanielk1977 } 945cabb0819Sdrh } 946d1e4733dSdrh rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal); 947d1e4733dSdrh if( rc ){ 948d1e4733dSdrh Tcl_DecrRefCount(pCmd); 949d1e4733dSdrh sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 950d1e4733dSdrh return; 951d1e4733dSdrh } 952d1e4733dSdrh } 953d1e4733dSdrh if( !p->useEvalObjv ){ 954d1e4733dSdrh /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd 955d1e4733dSdrh ** is a list without a string representation. To prevent this from 956d1e4733dSdrh ** happening, make sure pCmd has a valid string representation */ 957d1e4733dSdrh Tcl_GetString(pCmd); 958d1e4733dSdrh } 959d1e4733dSdrh rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 960d1e4733dSdrh Tcl_DecrRefCount(pCmd); 961d1e4733dSdrh } 962562e8d3cSdanielk1977 963c7f269d5Sdrh if( rc && rc!=TCL_RETURN ){ 9647e18c259Sdanielk1977 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 965cabb0819Sdrh }else{ 966c7f269d5Sdrh Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); 967c7f269d5Sdrh int n; 968c7f269d5Sdrh u8 *data; 9694a4c11aaSdan const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 970c7f269d5Sdrh char c = zType[0]; 971df0bddaeSdrh if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ 972d1e4733dSdrh /* Only return a BLOB type if the Tcl variable is a bytearray and 973df0bddaeSdrh ** has no string representation. */ 974c7f269d5Sdrh data = Tcl_GetByteArrayFromObj(pVar, &n); 975c7f269d5Sdrh sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT); 976985e0c63Sdrh }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 977c7f269d5Sdrh Tcl_GetIntFromObj(0, pVar, &n); 978c7f269d5Sdrh sqlite3_result_int(context, n); 979c7f269d5Sdrh }else if( c=='d' && strcmp(zType,"double")==0 ){ 980c7f269d5Sdrh double r; 981c7f269d5Sdrh Tcl_GetDoubleFromObj(0, pVar, &r); 982c7f269d5Sdrh sqlite3_result_double(context, r); 983985e0c63Sdrh }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 984985e0c63Sdrh (c=='i' && strcmp(zType,"int")==0) ){ 985df0bddaeSdrh Tcl_WideInt v; 986df0bddaeSdrh Tcl_GetWideIntFromObj(0, pVar, &v); 987df0bddaeSdrh sqlite3_result_int64(context, v); 988c7f269d5Sdrh }else{ 98900fd957bSdanielk1977 data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 99000fd957bSdanielk1977 sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT); 991c7f269d5Sdrh } 992cabb0819Sdrh } 993cabb0819Sdrh } 994895d7472Sdrh 995e22a334bSdrh #ifndef SQLITE_OMIT_AUTHORIZATION 996e22a334bSdrh /* 997e22a334bSdrh ** This is the authentication function. It appends the authentication 998e22a334bSdrh ** type code and the two arguments to zCmd[] then invokes the result 999e22a334bSdrh ** on the interpreter. The reply is examined to determine if the 1000e22a334bSdrh ** authentication fails or succeeds. 1001e22a334bSdrh */ 1002e22a334bSdrh static int auth_callback( 1003e22a334bSdrh void *pArg, 1004e22a334bSdrh int code, 1005e22a334bSdrh const char *zArg1, 1006e22a334bSdrh const char *zArg2, 1007e22a334bSdrh const char *zArg3, 1008e22a334bSdrh const char *zArg4 100932c6a48bSdrh #ifdef SQLITE_USER_AUTHENTICATION 101032c6a48bSdrh ,const char *zArg5 101132c6a48bSdrh #endif 1012e22a334bSdrh ){ 10136ef5e12eSmistachkin const char *zCode; 1014e22a334bSdrh Tcl_DString str; 1015e22a334bSdrh int rc; 1016e22a334bSdrh const char *zReply; 1017e22a334bSdrh SqliteDb *pDb = (SqliteDb*)pArg; 10181f1549f8Sdrh if( pDb->disableAuth ) return SQLITE_OK; 1019e22a334bSdrh 1020e22a334bSdrh switch( code ){ 1021e22a334bSdrh case SQLITE_COPY : zCode="SQLITE_COPY"; break; 1022e22a334bSdrh case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; 1023e22a334bSdrh case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; 1024e22a334bSdrh case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; 1025e22a334bSdrh case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; 1026e22a334bSdrh case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; 1027e22a334bSdrh case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; 1028e22a334bSdrh case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; 1029e22a334bSdrh case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; 1030e22a334bSdrh case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; 1031e22a334bSdrh case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; 1032e22a334bSdrh case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; 1033e22a334bSdrh case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; 1034e22a334bSdrh case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; 1035e22a334bSdrh case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; 1036e22a334bSdrh case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; 1037e22a334bSdrh case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; 1038e22a334bSdrh case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; 1039e22a334bSdrh case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; 1040e22a334bSdrh case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; 1041e22a334bSdrh case SQLITE_READ : zCode="SQLITE_READ"; break; 1042e22a334bSdrh case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; 1043e22a334bSdrh case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; 1044e22a334bSdrh case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; 104581e293b4Sdrh case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; 104681e293b4Sdrh case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; 10471c8c23ccSdanielk1977 case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break; 10481d54df88Sdanielk1977 case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break; 1049e6e04969Sdrh case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break; 1050f1a381e7Sdanielk1977 case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break; 1051f1a381e7Sdanielk1977 case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break; 10525169bbc6Sdrh case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break; 1053ab9b703fSdanielk1977 case SQLITE_SAVEPOINT : zCode="SQLITE_SAVEPOINT"; break; 105465a2aaa6Sdrh case SQLITE_RECURSIVE : zCode="SQLITE_RECURSIVE"; break; 1055e22a334bSdrh default : zCode="????"; break; 1056e22a334bSdrh } 1057e22a334bSdrh Tcl_DStringInit(&str); 1058e22a334bSdrh Tcl_DStringAppend(&str, pDb->zAuth, -1); 1059e22a334bSdrh Tcl_DStringAppendElement(&str, zCode); 1060e22a334bSdrh Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); 1061e22a334bSdrh Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); 1062e22a334bSdrh Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); 1063e22a334bSdrh Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); 106432c6a48bSdrh #ifdef SQLITE_USER_AUTHENTICATION 106532c6a48bSdrh Tcl_DStringAppendElement(&str, zArg5 ? zArg5 : ""); 106632c6a48bSdrh #endif 1067e22a334bSdrh rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); 1068e22a334bSdrh Tcl_DStringFree(&str); 1069b07028f7Sdrh zReply = rc==TCL_OK ? Tcl_GetStringResult(pDb->interp) : "SQLITE_DENY"; 1070e22a334bSdrh if( strcmp(zReply,"SQLITE_OK")==0 ){ 1071e22a334bSdrh rc = SQLITE_OK; 1072e22a334bSdrh }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ 1073e22a334bSdrh rc = SQLITE_DENY; 1074e22a334bSdrh }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ 1075e22a334bSdrh rc = SQLITE_IGNORE; 1076e22a334bSdrh }else{ 1077e22a334bSdrh rc = 999; 1078e22a334bSdrh } 1079e22a334bSdrh return rc; 1080e22a334bSdrh } 1081e22a334bSdrh #endif /* SQLITE_OMIT_AUTHORIZATION */ 1082cabb0819Sdrh 1083cabb0819Sdrh /* 10841067fe11Stpoindex ** This routine reads a line of text from FILE in, stores 10851067fe11Stpoindex ** the text in memory obtained from malloc() and returns a pointer 10861067fe11Stpoindex ** to the text. NULL is returned at end of file, or if malloc() 10871067fe11Stpoindex ** fails. 10881067fe11Stpoindex ** 10891067fe11Stpoindex ** The interface is like "readline" but no command-line editing 10901067fe11Stpoindex ** is done. 10911067fe11Stpoindex ** 10921067fe11Stpoindex ** copied from shell.c from '.import' command 10931067fe11Stpoindex */ 10941067fe11Stpoindex static char *local_getline(char *zPrompt, FILE *in){ 10951067fe11Stpoindex char *zLine; 10961067fe11Stpoindex int nLine; 10971067fe11Stpoindex int n; 10981067fe11Stpoindex 10991067fe11Stpoindex nLine = 100; 11001067fe11Stpoindex zLine = malloc( nLine ); 11011067fe11Stpoindex if( zLine==0 ) return 0; 11021067fe11Stpoindex n = 0; 1103b07028f7Sdrh while( 1 ){ 11041067fe11Stpoindex if( n+100>nLine ){ 11051067fe11Stpoindex nLine = nLine*2 + 100; 11061067fe11Stpoindex zLine = realloc(zLine, nLine); 11071067fe11Stpoindex if( zLine==0 ) return 0; 11081067fe11Stpoindex } 11091067fe11Stpoindex if( fgets(&zLine[n], nLine - n, in)==0 ){ 11101067fe11Stpoindex if( n==0 ){ 11111067fe11Stpoindex free(zLine); 11121067fe11Stpoindex return 0; 11131067fe11Stpoindex } 11141067fe11Stpoindex zLine[n] = 0; 11151067fe11Stpoindex break; 11161067fe11Stpoindex } 11171067fe11Stpoindex while( zLine[n] ){ n++; } 11181067fe11Stpoindex if( n>0 && zLine[n-1]=='\n' ){ 11191067fe11Stpoindex n--; 11201067fe11Stpoindex zLine[n] = 0; 1121b07028f7Sdrh break; 11221067fe11Stpoindex } 11231067fe11Stpoindex } 11241067fe11Stpoindex zLine = realloc( zLine, n+1 ); 11251067fe11Stpoindex return zLine; 11261067fe11Stpoindex } 11271067fe11Stpoindex 11288e556520Sdanielk1977 11298e556520Sdanielk1977 /* 11304a4c11aaSdan ** This function is part of the implementation of the command: 11318e556520Sdanielk1977 ** 11324a4c11aaSdan ** $db transaction [-deferred|-immediate|-exclusive] SCRIPT 11338e556520Sdanielk1977 ** 11344a4c11aaSdan ** It is invoked after evaluating the script SCRIPT to commit or rollback 11354a4c11aaSdan ** the transaction or savepoint opened by the [transaction] command. 11364a4c11aaSdan */ 11374a4c11aaSdan static int DbTransPostCmd( 11384a4c11aaSdan ClientData data[], /* data[0] is the Sqlite3Db* for $db */ 11394a4c11aaSdan Tcl_Interp *interp, /* Tcl interpreter */ 11404a4c11aaSdan int result /* Result of evaluating SCRIPT */ 11414a4c11aaSdan ){ 11426ef5e12eSmistachkin static const char *const azEnd[] = { 11434a4c11aaSdan "RELEASE _tcl_transaction", /* rc==TCL_ERROR, nTransaction!=0 */ 11444a4c11aaSdan "COMMIT", /* rc!=TCL_ERROR, nTransaction==0 */ 11454a4c11aaSdan "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction", 11464a4c11aaSdan "ROLLBACK" /* rc==TCL_ERROR, nTransaction==0 */ 11474a4c11aaSdan }; 11484a4c11aaSdan SqliteDb *pDb = (SqliteDb*)data[0]; 11494a4c11aaSdan int rc = result; 11504a4c11aaSdan const char *zEnd; 11514a4c11aaSdan 11524a4c11aaSdan pDb->nTransaction--; 11534a4c11aaSdan zEnd = azEnd[(rc==TCL_ERROR)*2 + (pDb->nTransaction==0)]; 11544a4c11aaSdan 11554a4c11aaSdan pDb->disableAuth++; 11564a4c11aaSdan if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){ 11574a4c11aaSdan /* This is a tricky scenario to handle. The most likely cause of an 11584a4c11aaSdan ** error is that the exec() above was an attempt to commit the 11594a4c11aaSdan ** top-level transaction that returned SQLITE_BUSY. Or, less likely, 116048864df9Smistachkin ** that an IO-error has occurred. In either case, throw a Tcl exception 11614a4c11aaSdan ** and try to rollback the transaction. 11624a4c11aaSdan ** 11634a4c11aaSdan ** But it could also be that the user executed one or more BEGIN, 11644a4c11aaSdan ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing 11654a4c11aaSdan ** this method's logic. Not clear how this would be best handled. 11664a4c11aaSdan */ 11674a4c11aaSdan if( rc!=TCL_ERROR ){ 1168a198f2b5Sdrh Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 11694a4c11aaSdan rc = TCL_ERROR; 11704a4c11aaSdan } 11714a4c11aaSdan sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0); 11724a4c11aaSdan } 11734a4c11aaSdan pDb->disableAuth--; 11744a4c11aaSdan 11754a4c11aaSdan return rc; 11764a4c11aaSdan } 11774a4c11aaSdan 11784a4c11aaSdan /* 1179c431fd55Sdan ** Unless SQLITE_TEST is defined, this function is a simple wrapper around 1180c431fd55Sdan ** sqlite3_prepare_v2(). If SQLITE_TEST is defined, then it uses either 1181c431fd55Sdan ** sqlite3_prepare_v2() or legacy interface sqlite3_prepare(), depending 1182c431fd55Sdan ** on whether or not the [db_use_legacy_prepare] command has been used to 1183c431fd55Sdan ** configure the connection. 1184c431fd55Sdan */ 1185c431fd55Sdan static int dbPrepare( 1186c431fd55Sdan SqliteDb *pDb, /* Database object */ 1187c431fd55Sdan const char *zSql, /* SQL to compile */ 1188c431fd55Sdan sqlite3_stmt **ppStmt, /* OUT: Prepared statement */ 1189c431fd55Sdan const char **pzOut /* OUT: Pointer to next SQL statement */ 1190c431fd55Sdan ){ 1191c431fd55Sdan #ifdef SQLITE_TEST 1192c431fd55Sdan if( pDb->bLegacyPrepare ){ 1193c431fd55Sdan return sqlite3_prepare(pDb->db, zSql, -1, ppStmt, pzOut); 1194c431fd55Sdan } 1195c431fd55Sdan #endif 1196c431fd55Sdan return sqlite3_prepare_v2(pDb->db, zSql, -1, ppStmt, pzOut); 1197c431fd55Sdan } 1198c431fd55Sdan 1199c431fd55Sdan /* 12004a4c11aaSdan ** Search the cache for a prepared-statement object that implements the 12014a4c11aaSdan ** first SQL statement in the buffer pointed to by parameter zIn. If 12024a4c11aaSdan ** no such prepared-statement can be found, allocate and prepare a new 12034a4c11aaSdan ** one. In either case, bind the current values of the relevant Tcl 12044a4c11aaSdan ** variables to any $var, :var or @var variables in the statement. Before 12054a4c11aaSdan ** returning, set *ppPreStmt to point to the prepared-statement object. 12064a4c11aaSdan ** 12074a4c11aaSdan ** Output parameter *pzOut is set to point to the next SQL statement in 12084a4c11aaSdan ** buffer zIn, or to the '\0' byte at the end of zIn if there is no 12094a4c11aaSdan ** next statement. 12104a4c11aaSdan ** 12114a4c11aaSdan ** If successful, TCL_OK is returned. Otherwise, TCL_ERROR is returned 12124a4c11aaSdan ** and an error message loaded into interpreter pDb->interp. 12134a4c11aaSdan */ 12144a4c11aaSdan static int dbPrepareAndBind( 12154a4c11aaSdan SqliteDb *pDb, /* Database object */ 12164a4c11aaSdan char const *zIn, /* SQL to compile */ 12174a4c11aaSdan char const **pzOut, /* OUT: Pointer to next SQL statement */ 12184a4c11aaSdan SqlPreparedStmt **ppPreStmt /* OUT: Object used to cache statement */ 12194a4c11aaSdan ){ 12204a4c11aaSdan const char *zSql = zIn; /* Pointer to first SQL statement in zIn */ 12217bb22ac7Smistachkin sqlite3_stmt *pStmt = 0; /* Prepared statement object */ 12224a4c11aaSdan SqlPreparedStmt *pPreStmt; /* Pointer to cached statement */ 12234a4c11aaSdan int nSql; /* Length of zSql in bytes */ 12247bb22ac7Smistachkin int nVar = 0; /* Number of variables in statement */ 12254a4c11aaSdan int iParm = 0; /* Next free entry in apParm */ 12260425f189Sdrh char c; 12274a4c11aaSdan int i; 12284a4c11aaSdan Tcl_Interp *interp = pDb->interp; 12294a4c11aaSdan 12304a4c11aaSdan *ppPreStmt = 0; 12314a4c11aaSdan 12324a4c11aaSdan /* Trim spaces from the start of zSql and calculate the remaining length. */ 12330425f189Sdrh while( (c = zSql[0])==' ' || c=='\t' || c=='\r' || c=='\n' ){ zSql++; } 12344a4c11aaSdan nSql = strlen30(zSql); 12354a4c11aaSdan 12364a4c11aaSdan for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){ 12374a4c11aaSdan int n = pPreStmt->nSql; 12384a4c11aaSdan if( nSql>=n 12394a4c11aaSdan && memcmp(pPreStmt->zSql, zSql, n)==0 12404a4c11aaSdan && (zSql[n]==0 || zSql[n-1]==';') 12414a4c11aaSdan ){ 12424a4c11aaSdan pStmt = pPreStmt->pStmt; 12434a4c11aaSdan *pzOut = &zSql[pPreStmt->nSql]; 12444a4c11aaSdan 12454a4c11aaSdan /* When a prepared statement is found, unlink it from the 12464a4c11aaSdan ** cache list. It will later be added back to the beginning 12474a4c11aaSdan ** of the cache list in order to implement LRU replacement. 12484a4c11aaSdan */ 12494a4c11aaSdan if( pPreStmt->pPrev ){ 12504a4c11aaSdan pPreStmt->pPrev->pNext = pPreStmt->pNext; 12514a4c11aaSdan }else{ 12524a4c11aaSdan pDb->stmtList = pPreStmt->pNext; 12534a4c11aaSdan } 12544a4c11aaSdan if( pPreStmt->pNext ){ 12554a4c11aaSdan pPreStmt->pNext->pPrev = pPreStmt->pPrev; 12564a4c11aaSdan }else{ 12574a4c11aaSdan pDb->stmtLast = pPreStmt->pPrev; 12584a4c11aaSdan } 12594a4c11aaSdan pDb->nStmt--; 12604a4c11aaSdan nVar = sqlite3_bind_parameter_count(pStmt); 12614a4c11aaSdan break; 12624a4c11aaSdan } 12634a4c11aaSdan } 12644a4c11aaSdan 12654a4c11aaSdan /* If no prepared statement was found. Compile the SQL text. Also allocate 12664a4c11aaSdan ** a new SqlPreparedStmt structure. */ 12674a4c11aaSdan if( pPreStmt==0 ){ 12684a4c11aaSdan int nByte; 12694a4c11aaSdan 1270c431fd55Sdan if( SQLITE_OK!=dbPrepare(pDb, zSql, &pStmt, pzOut) ){ 1271c45e6716Sdrh Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 12724a4c11aaSdan return TCL_ERROR; 12734a4c11aaSdan } 12744a4c11aaSdan if( pStmt==0 ){ 12754a4c11aaSdan if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ 12764a4c11aaSdan /* A compile-time error in the statement. */ 1277c45e6716Sdrh Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 12784a4c11aaSdan return TCL_ERROR; 12794a4c11aaSdan }else{ 12804a4c11aaSdan /* The statement was a no-op. Continue to the next statement 12814a4c11aaSdan ** in the SQL string. 12824a4c11aaSdan */ 12834a4c11aaSdan return TCL_OK; 12844a4c11aaSdan } 12854a4c11aaSdan } 12864a4c11aaSdan 12874a4c11aaSdan assert( pPreStmt==0 ); 12884a4c11aaSdan nVar = sqlite3_bind_parameter_count(pStmt); 12894a4c11aaSdan nByte = sizeof(SqlPreparedStmt) + nVar*sizeof(Tcl_Obj *); 12904a4c11aaSdan pPreStmt = (SqlPreparedStmt*)Tcl_Alloc(nByte); 12914a4c11aaSdan memset(pPreStmt, 0, nByte); 12924a4c11aaSdan 12934a4c11aaSdan pPreStmt->pStmt = pStmt; 12947ed243b7Sdrh pPreStmt->nSql = (int)(*pzOut - zSql); 12954a4c11aaSdan pPreStmt->zSql = sqlite3_sql(pStmt); 12964a4c11aaSdan pPreStmt->apParm = (Tcl_Obj **)&pPreStmt[1]; 1297c431fd55Sdan #ifdef SQLITE_TEST 1298c431fd55Sdan if( pPreStmt->zSql==0 ){ 1299c431fd55Sdan char *zCopy = Tcl_Alloc(pPreStmt->nSql + 1); 1300c431fd55Sdan memcpy(zCopy, zSql, pPreStmt->nSql); 1301c431fd55Sdan zCopy[pPreStmt->nSql] = '\0'; 1302c431fd55Sdan pPreStmt->zSql = zCopy; 1303c431fd55Sdan } 1304c431fd55Sdan #endif 13054a4c11aaSdan } 13064a4c11aaSdan assert( pPreStmt ); 13074a4c11aaSdan assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql ); 13084a4c11aaSdan assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) ); 13094a4c11aaSdan 13104a4c11aaSdan /* Bind values to parameters that begin with $ or : */ 13114a4c11aaSdan for(i=1; i<=nVar; i++){ 13124a4c11aaSdan const char *zVar = sqlite3_bind_parameter_name(pStmt, i); 13134a4c11aaSdan if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){ 13144a4c11aaSdan Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); 13154a4c11aaSdan if( pVar ){ 13164a4c11aaSdan int n; 13174a4c11aaSdan u8 *data; 13184a4c11aaSdan const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 13198e18922fSmistachkin c = zType[0]; 13204a4c11aaSdan if( zVar[0]=='@' || 13214a4c11aaSdan (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){ 13224a4c11aaSdan /* Load a BLOB type if the Tcl variable is a bytearray and 13234a4c11aaSdan ** it has no string representation or the host 13244a4c11aaSdan ** parameter name begins with "@". */ 13254a4c11aaSdan data = Tcl_GetByteArrayFromObj(pVar, &n); 13264a4c11aaSdan sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); 13274a4c11aaSdan Tcl_IncrRefCount(pVar); 13284a4c11aaSdan pPreStmt->apParm[iParm++] = pVar; 13294a4c11aaSdan }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 13304a4c11aaSdan Tcl_GetIntFromObj(interp, pVar, &n); 13314a4c11aaSdan sqlite3_bind_int(pStmt, i, n); 13324a4c11aaSdan }else if( c=='d' && strcmp(zType,"double")==0 ){ 13334a4c11aaSdan double r; 13344a4c11aaSdan Tcl_GetDoubleFromObj(interp, pVar, &r); 13354a4c11aaSdan sqlite3_bind_double(pStmt, i, r); 13364a4c11aaSdan }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 13374a4c11aaSdan (c=='i' && strcmp(zType,"int")==0) ){ 13384a4c11aaSdan Tcl_WideInt v; 13394a4c11aaSdan Tcl_GetWideIntFromObj(interp, pVar, &v); 13404a4c11aaSdan sqlite3_bind_int64(pStmt, i, v); 13414a4c11aaSdan }else{ 13424a4c11aaSdan data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 13434a4c11aaSdan sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); 13444a4c11aaSdan Tcl_IncrRefCount(pVar); 13454a4c11aaSdan pPreStmt->apParm[iParm++] = pVar; 13464a4c11aaSdan } 13474a4c11aaSdan }else{ 13484a4c11aaSdan sqlite3_bind_null(pStmt, i); 13494a4c11aaSdan } 13504a4c11aaSdan } 13514a4c11aaSdan } 13524a4c11aaSdan pPreStmt->nParm = iParm; 13534a4c11aaSdan *ppPreStmt = pPreStmt; 1354937d0deaSdan 13554a4c11aaSdan return TCL_OK; 13564a4c11aaSdan } 13574a4c11aaSdan 13584a4c11aaSdan /* 13594a4c11aaSdan ** Release a statement reference obtained by calling dbPrepareAndBind(). 13604a4c11aaSdan ** There should be exactly one call to this function for each call to 13614a4c11aaSdan ** dbPrepareAndBind(). 13624a4c11aaSdan ** 13634a4c11aaSdan ** If the discard parameter is non-zero, then the statement is deleted 13644a4c11aaSdan ** immediately. Otherwise it is added to the LRU list and may be returned 13654a4c11aaSdan ** by a subsequent call to dbPrepareAndBind(). 13664a4c11aaSdan */ 13674a4c11aaSdan static void dbReleaseStmt( 13684a4c11aaSdan SqliteDb *pDb, /* Database handle */ 13694a4c11aaSdan SqlPreparedStmt *pPreStmt, /* Prepared statement handle to release */ 13704a4c11aaSdan int discard /* True to delete (not cache) the pPreStmt */ 13714a4c11aaSdan ){ 13724a4c11aaSdan int i; 13734a4c11aaSdan 13744a4c11aaSdan /* Free the bound string and blob parameters */ 13754a4c11aaSdan for(i=0; i<pPreStmt->nParm; i++){ 13764a4c11aaSdan Tcl_DecrRefCount(pPreStmt->apParm[i]); 13774a4c11aaSdan } 13784a4c11aaSdan pPreStmt->nParm = 0; 13794a4c11aaSdan 13804a4c11aaSdan if( pDb->maxStmt<=0 || discard ){ 13814a4c11aaSdan /* If the cache is turned off, deallocated the statement */ 1382c431fd55Sdan dbFreeStmt(pPreStmt); 13834a4c11aaSdan }else{ 13844a4c11aaSdan /* Add the prepared statement to the beginning of the cache list. */ 13854a4c11aaSdan pPreStmt->pNext = pDb->stmtList; 13864a4c11aaSdan pPreStmt->pPrev = 0; 13874a4c11aaSdan if( pDb->stmtList ){ 13884a4c11aaSdan pDb->stmtList->pPrev = pPreStmt; 13894a4c11aaSdan } 13904a4c11aaSdan pDb->stmtList = pPreStmt; 13914a4c11aaSdan if( pDb->stmtLast==0 ){ 13924a4c11aaSdan assert( pDb->nStmt==0 ); 13934a4c11aaSdan pDb->stmtLast = pPreStmt; 13944a4c11aaSdan }else{ 13954a4c11aaSdan assert( pDb->nStmt>0 ); 13964a4c11aaSdan } 13974a4c11aaSdan pDb->nStmt++; 13984a4c11aaSdan 13994a4c11aaSdan /* If we have too many statement in cache, remove the surplus from 14004a4c11aaSdan ** the end of the cache list. */ 14014a4c11aaSdan while( pDb->nStmt>pDb->maxStmt ){ 1402c431fd55Sdan SqlPreparedStmt *pLast = pDb->stmtLast; 1403c431fd55Sdan pDb->stmtLast = pLast->pPrev; 14044a4c11aaSdan pDb->stmtLast->pNext = 0; 14054a4c11aaSdan pDb->nStmt--; 1406c431fd55Sdan dbFreeStmt(pLast); 14074a4c11aaSdan } 14084a4c11aaSdan } 14094a4c11aaSdan } 14104a4c11aaSdan 14114a4c11aaSdan /* 14124a4c11aaSdan ** Structure used with dbEvalXXX() functions: 14134a4c11aaSdan ** 14144a4c11aaSdan ** dbEvalInit() 14154a4c11aaSdan ** dbEvalStep() 14164a4c11aaSdan ** dbEvalFinalize() 14174a4c11aaSdan ** dbEvalRowInfo() 14184a4c11aaSdan ** dbEvalColumnValue() 14194a4c11aaSdan */ 14204a4c11aaSdan typedef struct DbEvalContext DbEvalContext; 14214a4c11aaSdan struct DbEvalContext { 14224a4c11aaSdan SqliteDb *pDb; /* Database handle */ 14234a4c11aaSdan Tcl_Obj *pSql; /* Object holding string zSql */ 14244a4c11aaSdan const char *zSql; /* Remaining SQL to execute */ 14254a4c11aaSdan SqlPreparedStmt *pPreStmt; /* Current statement */ 14264a4c11aaSdan int nCol; /* Number of columns returned by pStmt */ 14274a4c11aaSdan Tcl_Obj *pArray; /* Name of array variable */ 14284a4c11aaSdan Tcl_Obj **apColName; /* Array of column names */ 14294a4c11aaSdan }; 14304a4c11aaSdan 14314a4c11aaSdan /* 14324a4c11aaSdan ** Release any cache of column names currently held as part of 14334a4c11aaSdan ** the DbEvalContext structure passed as the first argument. 14344a4c11aaSdan */ 14354a4c11aaSdan static void dbReleaseColumnNames(DbEvalContext *p){ 14364a4c11aaSdan if( p->apColName ){ 14374a4c11aaSdan int i; 14384a4c11aaSdan for(i=0; i<p->nCol; i++){ 14394a4c11aaSdan Tcl_DecrRefCount(p->apColName[i]); 14404a4c11aaSdan } 14414a4c11aaSdan Tcl_Free((char *)p->apColName); 14424a4c11aaSdan p->apColName = 0; 14434a4c11aaSdan } 14444a4c11aaSdan p->nCol = 0; 14454a4c11aaSdan } 14464a4c11aaSdan 14474a4c11aaSdan /* 14484a4c11aaSdan ** Initialize a DbEvalContext structure. 14498e556520Sdanielk1977 ** 14508e556520Sdanielk1977 ** If pArray is not NULL, then it contains the name of a Tcl array 14518e556520Sdanielk1977 ** variable. The "*" member of this array is set to a list containing 14524a4c11aaSdan ** the names of the columns returned by the statement as part of each 14534a4c11aaSdan ** call to dbEvalStep(), in order from left to right. e.g. if the names 14544a4c11aaSdan ** of the returned columns are a, b and c, it does the equivalent of the 14554a4c11aaSdan ** tcl command: 14568e556520Sdanielk1977 ** 14578e556520Sdanielk1977 ** set ${pArray}(*) {a b c} 14588e556520Sdanielk1977 */ 14594a4c11aaSdan static void dbEvalInit( 14604a4c11aaSdan DbEvalContext *p, /* Pointer to structure to initialize */ 14614a4c11aaSdan SqliteDb *pDb, /* Database handle */ 14624a4c11aaSdan Tcl_Obj *pSql, /* Object containing SQL script */ 14634a4c11aaSdan Tcl_Obj *pArray /* Name of Tcl array to set (*) element of */ 14648e556520Sdanielk1977 ){ 14654a4c11aaSdan memset(p, 0, sizeof(DbEvalContext)); 14664a4c11aaSdan p->pDb = pDb; 14674a4c11aaSdan p->zSql = Tcl_GetString(pSql); 14684a4c11aaSdan p->pSql = pSql; 14694a4c11aaSdan Tcl_IncrRefCount(pSql); 14704a4c11aaSdan if( pArray ){ 14714a4c11aaSdan p->pArray = pArray; 14724a4c11aaSdan Tcl_IncrRefCount(pArray); 14734a4c11aaSdan } 14744a4c11aaSdan } 14758e556520Sdanielk1977 14764a4c11aaSdan /* 14774a4c11aaSdan ** Obtain information about the row that the DbEvalContext passed as the 14784a4c11aaSdan ** first argument currently points to. 14794a4c11aaSdan */ 14804a4c11aaSdan static void dbEvalRowInfo( 14814a4c11aaSdan DbEvalContext *p, /* Evaluation context */ 14824a4c11aaSdan int *pnCol, /* OUT: Number of column names */ 14834a4c11aaSdan Tcl_Obj ***papColName /* OUT: Array of column names */ 14844a4c11aaSdan ){ 14858e556520Sdanielk1977 /* Compute column names */ 14864a4c11aaSdan if( 0==p->apColName ){ 14874a4c11aaSdan sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 14884a4c11aaSdan int i; /* Iterator variable */ 14894a4c11aaSdan int nCol; /* Number of columns returned by pStmt */ 14904a4c11aaSdan Tcl_Obj **apColName = 0; /* Array of column names */ 14914a4c11aaSdan 14924a4c11aaSdan p->nCol = nCol = sqlite3_column_count(pStmt); 14934a4c11aaSdan if( nCol>0 && (papColName || p->pArray) ){ 14944a4c11aaSdan apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); 14958e556520Sdanielk1977 for(i=0; i<nCol; i++){ 1496c45e6716Sdrh apColName[i] = Tcl_NewStringObj(sqlite3_column_name(pStmt,i), -1); 14978e556520Sdanielk1977 Tcl_IncrRefCount(apColName[i]); 14988e556520Sdanielk1977 } 14994a4c11aaSdan p->apColName = apColName; 15004a4c11aaSdan } 15018e556520Sdanielk1977 15028e556520Sdanielk1977 /* If results are being stored in an array variable, then create 15038e556520Sdanielk1977 ** the array(*) entry for that array 15048e556520Sdanielk1977 */ 15054a4c11aaSdan if( p->pArray ){ 15064a4c11aaSdan Tcl_Interp *interp = p->pDb->interp; 15078e556520Sdanielk1977 Tcl_Obj *pColList = Tcl_NewObj(); 15088e556520Sdanielk1977 Tcl_Obj *pStar = Tcl_NewStringObj("*", -1); 15094a4c11aaSdan 15108e556520Sdanielk1977 for(i=0; i<nCol; i++){ 15118e556520Sdanielk1977 Tcl_ListObjAppendElement(interp, pColList, apColName[i]); 15128e556520Sdanielk1977 } 15138e556520Sdanielk1977 Tcl_IncrRefCount(pStar); 15144a4c11aaSdan Tcl_ObjSetVar2(interp, p->pArray, pStar, pColList, 0); 15158e556520Sdanielk1977 Tcl_DecrRefCount(pStar); 15168e556520Sdanielk1977 } 15178e556520Sdanielk1977 } 15188e556520Sdanielk1977 15194a4c11aaSdan if( papColName ){ 15204a4c11aaSdan *papColName = p->apColName; 15214a4c11aaSdan } 15224a4c11aaSdan if( pnCol ){ 15234a4c11aaSdan *pnCol = p->nCol; 15244a4c11aaSdan } 15254a4c11aaSdan } 15264a4c11aaSdan 15274a4c11aaSdan /* 15284a4c11aaSdan ** Return one of TCL_OK, TCL_BREAK or TCL_ERROR. If TCL_ERROR is 15294a4c11aaSdan ** returned, then an error message is stored in the interpreter before 15304a4c11aaSdan ** returning. 15314a4c11aaSdan ** 15324a4c11aaSdan ** A return value of TCL_OK means there is a row of data available. The 15334a4c11aaSdan ** data may be accessed using dbEvalRowInfo() and dbEvalColumnValue(). This 15344a4c11aaSdan ** is analogous to a return of SQLITE_ROW from sqlite3_step(). If TCL_BREAK 15354a4c11aaSdan ** is returned, then the SQL script has finished executing and there are 15364a4c11aaSdan ** no further rows available. This is similar to SQLITE_DONE. 15374a4c11aaSdan */ 15384a4c11aaSdan static int dbEvalStep(DbEvalContext *p){ 1539c431fd55Sdan const char *zPrevSql = 0; /* Previous value of p->zSql */ 1540c431fd55Sdan 15414a4c11aaSdan while( p->zSql[0] || p->pPreStmt ){ 15424a4c11aaSdan int rc; 15434a4c11aaSdan if( p->pPreStmt==0 ){ 1544c431fd55Sdan zPrevSql = (p->zSql==zPrevSql ? 0 : p->zSql); 15454a4c11aaSdan rc = dbPrepareAndBind(p->pDb, p->zSql, &p->zSql, &p->pPreStmt); 15464a4c11aaSdan if( rc!=TCL_OK ) return rc; 15474a4c11aaSdan }else{ 15484a4c11aaSdan int rcs; 15494a4c11aaSdan SqliteDb *pDb = p->pDb; 15504a4c11aaSdan SqlPreparedStmt *pPreStmt = p->pPreStmt; 15514a4c11aaSdan sqlite3_stmt *pStmt = pPreStmt->pStmt; 15524a4c11aaSdan 15534a4c11aaSdan rcs = sqlite3_step(pStmt); 15544a4c11aaSdan if( rcs==SQLITE_ROW ){ 15554a4c11aaSdan return TCL_OK; 15564a4c11aaSdan } 15574a4c11aaSdan if( p->pArray ){ 15584a4c11aaSdan dbEvalRowInfo(p, 0, 0); 15594a4c11aaSdan } 15604a4c11aaSdan rcs = sqlite3_reset(pStmt); 15614a4c11aaSdan 15624a4c11aaSdan pDb->nStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_FULLSCAN_STEP,1); 15634a4c11aaSdan pDb->nSort = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_SORT,1); 15643c379b01Sdrh pDb->nIndex = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_AUTOINDEX,1); 15654a4c11aaSdan dbReleaseColumnNames(p); 15664a4c11aaSdan p->pPreStmt = 0; 15674a4c11aaSdan 15684a4c11aaSdan if( rcs!=SQLITE_OK ){ 15694a4c11aaSdan /* If a run-time error occurs, report the error and stop reading 15704a4c11aaSdan ** the SQL. */ 15714a4c11aaSdan dbReleaseStmt(pDb, pPreStmt, 1); 1572c431fd55Sdan #if SQLITE_TEST 1573c431fd55Sdan if( p->pDb->bLegacyPrepare && rcs==SQLITE_SCHEMA && zPrevSql ){ 1574c431fd55Sdan /* If the runtime error was an SQLITE_SCHEMA, and the database 1575c431fd55Sdan ** handle is configured to use the legacy sqlite3_prepare() 1576c431fd55Sdan ** interface, retry prepare()/step() on the same SQL statement. 1577c431fd55Sdan ** This only happens once. If there is a second SQLITE_SCHEMA 1578c431fd55Sdan ** error, the error will be returned to the caller. */ 1579c431fd55Sdan p->zSql = zPrevSql; 1580c431fd55Sdan continue; 1581c431fd55Sdan } 1582c431fd55Sdan #endif 1583c45e6716Sdrh Tcl_SetObjResult(pDb->interp, 1584c45e6716Sdrh Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 15854a4c11aaSdan return TCL_ERROR; 15864a4c11aaSdan }else{ 15874a4c11aaSdan dbReleaseStmt(pDb, pPreStmt, 0); 15884a4c11aaSdan } 15894a4c11aaSdan } 15904a4c11aaSdan } 15914a4c11aaSdan 15924a4c11aaSdan /* Finished */ 15934a4c11aaSdan return TCL_BREAK; 15944a4c11aaSdan } 15954a4c11aaSdan 15964a4c11aaSdan /* 15974a4c11aaSdan ** Free all resources currently held by the DbEvalContext structure passed 15984a4c11aaSdan ** as the first argument. There should be exactly one call to this function 15994a4c11aaSdan ** for each call to dbEvalInit(). 16004a4c11aaSdan */ 16014a4c11aaSdan static void dbEvalFinalize(DbEvalContext *p){ 16024a4c11aaSdan if( p->pPreStmt ){ 16034a4c11aaSdan sqlite3_reset(p->pPreStmt->pStmt); 16044a4c11aaSdan dbReleaseStmt(p->pDb, p->pPreStmt, 0); 16054a4c11aaSdan p->pPreStmt = 0; 16064a4c11aaSdan } 16074a4c11aaSdan if( p->pArray ){ 16084a4c11aaSdan Tcl_DecrRefCount(p->pArray); 16094a4c11aaSdan p->pArray = 0; 16104a4c11aaSdan } 16114a4c11aaSdan Tcl_DecrRefCount(p->pSql); 16124a4c11aaSdan dbReleaseColumnNames(p); 16134a4c11aaSdan } 16144a4c11aaSdan 16154a4c11aaSdan /* 16164a4c11aaSdan ** Return a pointer to a Tcl_Obj structure with ref-count 0 that contains 16174a4c11aaSdan ** the value for the iCol'th column of the row currently pointed to by 16184a4c11aaSdan ** the DbEvalContext structure passed as the first argument. 16194a4c11aaSdan */ 16204a4c11aaSdan static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){ 16214a4c11aaSdan sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 16224a4c11aaSdan switch( sqlite3_column_type(pStmt, iCol) ){ 16234a4c11aaSdan case SQLITE_BLOB: { 16244a4c11aaSdan int bytes = sqlite3_column_bytes(pStmt, iCol); 16254a4c11aaSdan const char *zBlob = sqlite3_column_blob(pStmt, iCol); 16264a4c11aaSdan if( !zBlob ) bytes = 0; 16274a4c11aaSdan return Tcl_NewByteArrayObj((u8*)zBlob, bytes); 16284a4c11aaSdan } 16294a4c11aaSdan case SQLITE_INTEGER: { 16304a4c11aaSdan sqlite_int64 v = sqlite3_column_int64(pStmt, iCol); 16314a4c11aaSdan if( v>=-2147483647 && v<=2147483647 ){ 16327fd33929Sdrh return Tcl_NewIntObj((int)v); 16334a4c11aaSdan }else{ 16344a4c11aaSdan return Tcl_NewWideIntObj(v); 16354a4c11aaSdan } 16364a4c11aaSdan } 16374a4c11aaSdan case SQLITE_FLOAT: { 16384a4c11aaSdan return Tcl_NewDoubleObj(sqlite3_column_double(pStmt, iCol)); 16394a4c11aaSdan } 16404a4c11aaSdan case SQLITE_NULL: { 1641c45e6716Sdrh return Tcl_NewStringObj(p->pDb->zNull, -1); 16424a4c11aaSdan } 16434a4c11aaSdan } 16444a4c11aaSdan 1645325eff58Sdrh return Tcl_NewStringObj((char*)sqlite3_column_text(pStmt, iCol), -1); 16464a4c11aaSdan } 16474a4c11aaSdan 16484a4c11aaSdan /* 16494a4c11aaSdan ** If using Tcl version 8.6 or greater, use the NR functions to avoid 16504a4c11aaSdan ** recursive evalution of scripts by the [db eval] and [db trans] 16514a4c11aaSdan ** commands. Even if the headers used while compiling the extension 16524a4c11aaSdan ** are 8.6 or newer, the code still tests the Tcl version at runtime. 16534a4c11aaSdan ** This allows stubs-enabled builds to be used with older Tcl libraries. 16544a4c11aaSdan */ 16554a4c11aaSdan #if TCL_MAJOR_VERSION>8 || (TCL_MAJOR_VERSION==8 && TCL_MINOR_VERSION>=6) 1656a2c8a95bSdrh # define SQLITE_TCL_NRE 1 16574a4c11aaSdan static int DbUseNre(void){ 16584a4c11aaSdan int major, minor; 16594a4c11aaSdan Tcl_GetVersion(&major, &minor, 0, 0); 16604a4c11aaSdan return( (major==8 && minor>=6) || major>8 ); 16614a4c11aaSdan } 16624a4c11aaSdan #else 16634a4c11aaSdan /* 16644a4c11aaSdan ** Compiling using headers earlier than 8.6. In this case NR cannot be 16654a4c11aaSdan ** used, so DbUseNre() to always return zero. Add #defines for the other 16664a4c11aaSdan ** Tcl_NRxxx() functions to prevent them from causing compilation errors, 16674a4c11aaSdan ** even though the only invocations of them are within conditional blocks 16684a4c11aaSdan ** of the form: 16694a4c11aaSdan ** 16704a4c11aaSdan ** if( DbUseNre() ) { ... } 16714a4c11aaSdan */ 1672a2c8a95bSdrh # define SQLITE_TCL_NRE 0 16734a4c11aaSdan # define DbUseNre() 0 1674a47941feSdrh # define Tcl_NRAddCallback(a,b,c,d,e,f) (void)0 16754a4c11aaSdan # define Tcl_NREvalObj(a,b,c) 0 1676a47941feSdrh # define Tcl_NRCreateCommand(a,b,c,d,e,f) (void)0 16774a4c11aaSdan #endif 16784a4c11aaSdan 16794a4c11aaSdan /* 16804a4c11aaSdan ** This function is part of the implementation of the command: 16814a4c11aaSdan ** 16824a4c11aaSdan ** $db eval SQL ?ARRAYNAME? SCRIPT 16834a4c11aaSdan */ 16844a4c11aaSdan static int DbEvalNextCmd( 16854a4c11aaSdan ClientData data[], /* data[0] is the (DbEvalContext*) */ 16864a4c11aaSdan Tcl_Interp *interp, /* Tcl interpreter */ 16874a4c11aaSdan int result /* Result so far */ 16884a4c11aaSdan ){ 16894a4c11aaSdan int rc = result; /* Return code */ 16904a4c11aaSdan 16914a4c11aaSdan /* The first element of the data[] array is a pointer to a DbEvalContext 16924a4c11aaSdan ** structure allocated using Tcl_Alloc(). The second element of data[] 16934a4c11aaSdan ** is a pointer to a Tcl_Obj containing the script to run for each row 16944a4c11aaSdan ** returned by the queries encapsulated in data[0]. */ 16954a4c11aaSdan DbEvalContext *p = (DbEvalContext *)data[0]; 16964a4c11aaSdan Tcl_Obj *pScript = (Tcl_Obj *)data[1]; 16974a4c11aaSdan Tcl_Obj *pArray = p->pArray; 16984a4c11aaSdan 16994a4c11aaSdan while( (rc==TCL_OK || rc==TCL_CONTINUE) && TCL_OK==(rc = dbEvalStep(p)) ){ 17004a4c11aaSdan int i; 17014a4c11aaSdan int nCol; 17024a4c11aaSdan Tcl_Obj **apColName; 17034a4c11aaSdan dbEvalRowInfo(p, &nCol, &apColName); 17044a4c11aaSdan for(i=0; i<nCol; i++){ 17054a4c11aaSdan Tcl_Obj *pVal = dbEvalColumnValue(p, i); 17064a4c11aaSdan if( pArray==0 ){ 17074a4c11aaSdan Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0); 17084a4c11aaSdan }else{ 17094a4c11aaSdan Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); 17104a4c11aaSdan } 17114a4c11aaSdan } 17124a4c11aaSdan 17134a4c11aaSdan /* The required interpreter variables are now populated with the data 17144a4c11aaSdan ** from the current row. If using NRE, schedule callbacks to evaluate 17154a4c11aaSdan ** script pScript, then to invoke this function again to fetch the next 17164a4c11aaSdan ** row (or clean up if there is no next row or the script throws an 17174a4c11aaSdan ** exception). After scheduling the callbacks, return control to the 17184a4c11aaSdan ** caller. 17194a4c11aaSdan ** 17204a4c11aaSdan ** If not using NRE, evaluate pScript directly and continue with the 17214a4c11aaSdan ** next iteration of this while(...) loop. */ 17224a4c11aaSdan if( DbUseNre() ){ 17234a4c11aaSdan Tcl_NRAddCallback(interp, DbEvalNextCmd, (void*)p, (void*)pScript, 0, 0); 17244a4c11aaSdan return Tcl_NREvalObj(interp, pScript, 0); 17254a4c11aaSdan }else{ 17264a4c11aaSdan rc = Tcl_EvalObjEx(interp, pScript, 0); 17274a4c11aaSdan } 17284a4c11aaSdan } 17294a4c11aaSdan 17304a4c11aaSdan Tcl_DecrRefCount(pScript); 17314a4c11aaSdan dbEvalFinalize(p); 17324a4c11aaSdan Tcl_Free((char *)p); 17334a4c11aaSdan 17344a4c11aaSdan if( rc==TCL_OK || rc==TCL_BREAK ){ 17354a4c11aaSdan Tcl_ResetResult(interp); 17364a4c11aaSdan rc = TCL_OK; 17374a4c11aaSdan } 17384a4c11aaSdan return rc; 17398e556520Sdanielk1977 } 17408e556520Sdanielk1977 17411067fe11Stpoindex /* 174246c47d46Sdan ** This function is used by the implementations of the following database 174346c47d46Sdan ** handle sub-commands: 174446c47d46Sdan ** 174546c47d46Sdan ** $db update_hook ?SCRIPT? 174646c47d46Sdan ** $db wal_hook ?SCRIPT? 174746c47d46Sdan ** $db commit_hook ?SCRIPT? 174846c47d46Sdan ** $db preupdate hook ?SCRIPT? 174946c47d46Sdan */ 175046c47d46Sdan static void DbHookCmd( 175146c47d46Sdan Tcl_Interp *interp, /* Tcl interpreter */ 175246c47d46Sdan SqliteDb *pDb, /* Database handle */ 175346c47d46Sdan Tcl_Obj *pArg, /* SCRIPT argument (or NULL) */ 175446c47d46Sdan Tcl_Obj **ppHook /* Pointer to member of SqliteDb */ 175546c47d46Sdan ){ 175646c47d46Sdan sqlite3 *db = pDb->db; 175746c47d46Sdan 175846c47d46Sdan if( *ppHook ){ 175946c47d46Sdan Tcl_SetObjResult(interp, *ppHook); 176046c47d46Sdan if( pArg ){ 176146c47d46Sdan Tcl_DecrRefCount(*ppHook); 176246c47d46Sdan *ppHook = 0; 176346c47d46Sdan } 176446c47d46Sdan } 176546c47d46Sdan if( pArg ){ 176646c47d46Sdan assert( !(*ppHook) ); 176746c47d46Sdan if( Tcl_GetCharLength(pArg)>0 ){ 176846c47d46Sdan *ppHook = pArg; 176946c47d46Sdan Tcl_IncrRefCount(*ppHook); 177046c47d46Sdan } 177146c47d46Sdan } 177246c47d46Sdan 17739b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 177446c47d46Sdan sqlite3_preupdate_hook(db, (pDb->pPreUpdateHook?DbPreUpdateHandler:0), pDb); 17759b1c62d4Sdrh #endif 177646c47d46Sdan sqlite3_update_hook(db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); 177746c47d46Sdan sqlite3_rollback_hook(db, (pDb->pRollbackHook?DbRollbackHandler:0), pDb); 177846c47d46Sdan sqlite3_wal_hook(db, (pDb->pWalHook?DbWalHandler:0), pDb); 177946c47d46Sdan } 178046c47d46Sdan 178146c47d46Sdan /* 178275897234Sdrh ** The "sqlite" command below creates a new Tcl command for each 178375897234Sdrh ** connection it opens to an SQLite database. This routine is invoked 178475897234Sdrh ** whenever one of those connection-specific commands is executed 178575897234Sdrh ** in Tcl. For example, if you run Tcl code like this: 178675897234Sdrh ** 17879bb575fdSdrh ** sqlite3 db1 "my_database" 178875897234Sdrh ** db1 close 178975897234Sdrh ** 179075897234Sdrh ** The first command opens a connection to the "my_database" database 179175897234Sdrh ** and calls that connection "db1". The second command causes this 179275897234Sdrh ** subroutine to be invoked. 179375897234Sdrh */ 17946d31316cSdrh static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ 1795bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 17966d31316cSdrh int choice; 179722fbcb8dSdrh int rc = TCL_OK; 17980de8c112Sdrh static const char *DB_strs[] = { 1799dc2c4915Sdrh "authorizer", "backup", "busy", 1800dc2c4915Sdrh "cache", "changes", "close", 1801dc2c4915Sdrh "collate", "collation_needed", "commit_hook", 1802dc2c4915Sdrh "complete", "copy", "enable_load_extension", 1803dc2c4915Sdrh "errorcode", "eval", "exists", 1804dc2c4915Sdrh "function", "incrblob", "interrupt", 1805833bf968Sdrh "last_insert_rowid", "nullvalue", "onecolumn", 1806304637c0Sdrh "preupdate", "profile", "progress", 1807304637c0Sdrh "rekey", "restore", "rollback_hook", 1808304637c0Sdrh "status", "timeout", "total_changes", 1809b56660f5Smistachkin "trace", "trace_v2", "transaction", 1810b56660f5Smistachkin "unlock_notify", "update_hook", "version", 1811b56660f5Smistachkin "wal_hook", 1812304637c0Sdrh 0 18136d31316cSdrh }; 1814411995dcSdrh enum DB_enum { 1815dc2c4915Sdrh DB_AUTHORIZER, DB_BACKUP, DB_BUSY, 1816dc2c4915Sdrh DB_CACHE, DB_CHANGES, DB_CLOSE, 1817dc2c4915Sdrh DB_COLLATE, DB_COLLATION_NEEDED, DB_COMMIT_HOOK, 1818dc2c4915Sdrh DB_COMPLETE, DB_COPY, DB_ENABLE_LOAD_EXTENSION, 1819dc2c4915Sdrh DB_ERRORCODE, DB_EVAL, DB_EXISTS, 1820dc2c4915Sdrh DB_FUNCTION, DB_INCRBLOB, DB_INTERRUPT, 1821833bf968Sdrh DB_LAST_INSERT_ROWID, DB_NULLVALUE, DB_ONECOLUMN, 1822304637c0Sdrh DB_PREUPDATE, DB_PROFILE, DB_PROGRESS, 1823304637c0Sdrh DB_REKEY, DB_RESTORE, DB_ROLLBACK_HOOK, 1824304637c0Sdrh DB_STATUS, DB_TIMEOUT, DB_TOTAL_CHANGES, 1825b56660f5Smistachkin DB_TRACE, DB_TRACE_V2, DB_TRANSACTION, 1826b56660f5Smistachkin DB_UNLOCK_NOTIFY, DB_UPDATE_HOOK, DB_VERSION, 1827b56660f5Smistachkin DB_WAL_HOOK, 18286d31316cSdrh }; 18291067fe11Stpoindex /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */ 18306d31316cSdrh 18316d31316cSdrh if( objc<2 ){ 18326d31316cSdrh Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); 183375897234Sdrh return TCL_ERROR; 183475897234Sdrh } 1835411995dcSdrh if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ 18366d31316cSdrh return TCL_ERROR; 18376d31316cSdrh } 18386d31316cSdrh 1839411995dcSdrh switch( (enum DB_enum)choice ){ 184075897234Sdrh 1841e22a334bSdrh /* $db authorizer ?CALLBACK? 1842e22a334bSdrh ** 1843e22a334bSdrh ** Invoke the given callback to authorize each SQL operation as it is 1844e22a334bSdrh ** compiled. 5 arguments are appended to the callback before it is 1845e22a334bSdrh ** invoked: 1846e22a334bSdrh ** 1847e22a334bSdrh ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) 1848e22a334bSdrh ** (2) First descriptive name (depends on authorization type) 1849e22a334bSdrh ** (3) Second descriptive name 1850e22a334bSdrh ** (4) Name of the database (ex: "main", "temp") 1851e22a334bSdrh ** (5) Name of trigger that is doing the access 1852e22a334bSdrh ** 1853e22a334bSdrh ** The callback should return on of the following strings: SQLITE_OK, 1854e22a334bSdrh ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. 1855e22a334bSdrh ** 1856e22a334bSdrh ** If this method is invoked with no arguments, the current authorization 1857e22a334bSdrh ** callback string is returned. 1858e22a334bSdrh */ 1859e22a334bSdrh case DB_AUTHORIZER: { 18601211de37Sdrh #ifdef SQLITE_OMIT_AUTHORIZATION 1861a198f2b5Sdrh Tcl_AppendResult(interp, "authorization not available in this build", 1862a198f2b5Sdrh (char*)0); 18631211de37Sdrh return TCL_ERROR; 18641211de37Sdrh #else 1865e22a334bSdrh if( objc>3 ){ 1866e22a334bSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 18670f14e2ebSdrh return TCL_ERROR; 1868e22a334bSdrh }else if( objc==2 ){ 1869b5a20d3cSdrh if( pDb->zAuth ){ 1870a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zAuth, (char*)0); 1871e22a334bSdrh } 1872e22a334bSdrh }else{ 1873e22a334bSdrh char *zAuth; 1874e22a334bSdrh int len; 1875e22a334bSdrh if( pDb->zAuth ){ 1876e22a334bSdrh Tcl_Free(pDb->zAuth); 1877e22a334bSdrh } 1878e22a334bSdrh zAuth = Tcl_GetStringFromObj(objv[2], &len); 1879e22a334bSdrh if( zAuth && len>0 ){ 1880e22a334bSdrh pDb->zAuth = Tcl_Alloc( len + 1 ); 18815bb3eb9bSdrh memcpy(pDb->zAuth, zAuth, len+1); 1882e22a334bSdrh }else{ 1883e22a334bSdrh pDb->zAuth = 0; 1884e22a334bSdrh } 1885e22a334bSdrh if( pDb->zAuth ){ 188632c6a48bSdrh typedef int (*sqlite3_auth_cb)( 188732c6a48bSdrh void*,int,const char*,const char*, 188832c6a48bSdrh const char*,const char*); 1889e22a334bSdrh pDb->interp = interp; 189032c6a48bSdrh sqlite3_set_authorizer(pDb->db,(sqlite3_auth_cb)auth_callback,pDb); 1891e22a334bSdrh }else{ 18926f8a503dSdanielk1977 sqlite3_set_authorizer(pDb->db, 0, 0); 1893e22a334bSdrh } 1894e22a334bSdrh } 18951211de37Sdrh #endif 1896e22a334bSdrh break; 1897e22a334bSdrh } 1898e22a334bSdrh 1899dc2c4915Sdrh /* $db backup ?DATABASE? FILENAME 1900dc2c4915Sdrh ** 1901dc2c4915Sdrh ** Open or create a database file named FILENAME. Transfer the 1902dc2c4915Sdrh ** content of local database DATABASE (default: "main") into the 1903dc2c4915Sdrh ** FILENAME database. 1904dc2c4915Sdrh */ 1905dc2c4915Sdrh case DB_BACKUP: { 1906dc2c4915Sdrh const char *zDestFile; 1907dc2c4915Sdrh const char *zSrcDb; 1908dc2c4915Sdrh sqlite3 *pDest; 1909dc2c4915Sdrh sqlite3_backup *pBackup; 1910dc2c4915Sdrh 1911dc2c4915Sdrh if( objc==3 ){ 1912dc2c4915Sdrh zSrcDb = "main"; 1913dc2c4915Sdrh zDestFile = Tcl_GetString(objv[2]); 1914dc2c4915Sdrh }else if( objc==4 ){ 1915dc2c4915Sdrh zSrcDb = Tcl_GetString(objv[2]); 1916dc2c4915Sdrh zDestFile = Tcl_GetString(objv[3]); 1917dc2c4915Sdrh }else{ 1918dc2c4915Sdrh Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 1919dc2c4915Sdrh return TCL_ERROR; 1920dc2c4915Sdrh } 1921147ef394Sdrh rc = sqlite3_open_v2(zDestFile, &pDest, 1922147ef394Sdrh SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE| pDb->openFlags, 0); 1923dc2c4915Sdrh if( rc!=SQLITE_OK ){ 1924dc2c4915Sdrh Tcl_AppendResult(interp, "cannot open target database: ", 1925dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1926dc2c4915Sdrh sqlite3_close(pDest); 1927dc2c4915Sdrh return TCL_ERROR; 1928dc2c4915Sdrh } 1929dc2c4915Sdrh pBackup = sqlite3_backup_init(pDest, "main", pDb->db, zSrcDb); 1930dc2c4915Sdrh if( pBackup==0 ){ 1931dc2c4915Sdrh Tcl_AppendResult(interp, "backup failed: ", 1932dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1933dc2c4915Sdrh sqlite3_close(pDest); 1934dc2c4915Sdrh return TCL_ERROR; 1935dc2c4915Sdrh } 1936dc2c4915Sdrh while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} 1937dc2c4915Sdrh sqlite3_backup_finish(pBackup); 1938dc2c4915Sdrh if( rc==SQLITE_DONE ){ 1939dc2c4915Sdrh rc = TCL_OK; 1940dc2c4915Sdrh }else{ 1941dc2c4915Sdrh Tcl_AppendResult(interp, "backup failed: ", 1942dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1943dc2c4915Sdrh rc = TCL_ERROR; 1944dc2c4915Sdrh } 1945dc2c4915Sdrh sqlite3_close(pDest); 1946dc2c4915Sdrh break; 1947dc2c4915Sdrh } 1948dc2c4915Sdrh 1949bec3f402Sdrh /* $db busy ?CALLBACK? 1950bec3f402Sdrh ** 1951bec3f402Sdrh ** Invoke the given callback if an SQL statement attempts to open 1952bec3f402Sdrh ** a locked database file. 1953bec3f402Sdrh */ 19546d31316cSdrh case DB_BUSY: { 19556d31316cSdrh if( objc>3 ){ 19566d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); 1957bec3f402Sdrh return TCL_ERROR; 19586d31316cSdrh }else if( objc==2 ){ 1959bec3f402Sdrh if( pDb->zBusy ){ 1960a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zBusy, (char*)0); 1961bec3f402Sdrh } 1962bec3f402Sdrh }else{ 19636d31316cSdrh char *zBusy; 19646d31316cSdrh int len; 1965bec3f402Sdrh if( pDb->zBusy ){ 1966bec3f402Sdrh Tcl_Free(pDb->zBusy); 19676d31316cSdrh } 19686d31316cSdrh zBusy = Tcl_GetStringFromObj(objv[2], &len); 19696d31316cSdrh if( zBusy && len>0 ){ 19706d31316cSdrh pDb->zBusy = Tcl_Alloc( len + 1 ); 19715bb3eb9bSdrh memcpy(pDb->zBusy, zBusy, len+1); 19726d31316cSdrh }else{ 1973bec3f402Sdrh pDb->zBusy = 0; 1974bec3f402Sdrh } 1975bec3f402Sdrh if( pDb->zBusy ){ 1976bec3f402Sdrh pDb->interp = interp; 19776f8a503dSdanielk1977 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb); 19786d31316cSdrh }else{ 19796f8a503dSdanielk1977 sqlite3_busy_handler(pDb->db, 0, 0); 1980bec3f402Sdrh } 1981bec3f402Sdrh } 19826d31316cSdrh break; 19836d31316cSdrh } 1984bec3f402Sdrh 1985fb7e7651Sdrh /* $db cache flush 1986fb7e7651Sdrh ** $db cache size n 1987fb7e7651Sdrh ** 1988fb7e7651Sdrh ** Flush the prepared statement cache, or set the maximum number of 1989fb7e7651Sdrh ** cached statements. 1990fb7e7651Sdrh */ 1991fb7e7651Sdrh case DB_CACHE: { 1992fb7e7651Sdrh char *subCmd; 1993fb7e7651Sdrh int n; 1994fb7e7651Sdrh 1995fb7e7651Sdrh if( objc<=2 ){ 1996fb7e7651Sdrh Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?"); 1997fb7e7651Sdrh return TCL_ERROR; 1998fb7e7651Sdrh } 1999fb7e7651Sdrh subCmd = Tcl_GetStringFromObj( objv[2], 0 ); 2000fb7e7651Sdrh if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){ 2001fb7e7651Sdrh if( objc!=3 ){ 2002fb7e7651Sdrh Tcl_WrongNumArgs(interp, 2, objv, "flush"); 2003fb7e7651Sdrh return TCL_ERROR; 2004fb7e7651Sdrh }else{ 2005fb7e7651Sdrh flushStmtCache( pDb ); 2006fb7e7651Sdrh } 2007fb7e7651Sdrh }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){ 2008fb7e7651Sdrh if( objc!=4 ){ 2009fb7e7651Sdrh Tcl_WrongNumArgs(interp, 2, objv, "size n"); 2010fb7e7651Sdrh return TCL_ERROR; 2011fb7e7651Sdrh }else{ 2012fb7e7651Sdrh if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){ 2013fb7e7651Sdrh Tcl_AppendResult( interp, "cannot convert \"", 2014a198f2b5Sdrh Tcl_GetStringFromObj(objv[3],0), "\" to integer", (char*)0); 2015fb7e7651Sdrh return TCL_ERROR; 2016fb7e7651Sdrh }else{ 2017fb7e7651Sdrh if( n<0 ){ 2018fb7e7651Sdrh flushStmtCache( pDb ); 2019fb7e7651Sdrh n = 0; 2020fb7e7651Sdrh }else if( n>MAX_PREPARED_STMTS ){ 2021fb7e7651Sdrh n = MAX_PREPARED_STMTS; 2022fb7e7651Sdrh } 2023fb7e7651Sdrh pDb->maxStmt = n; 2024fb7e7651Sdrh } 2025fb7e7651Sdrh } 2026fb7e7651Sdrh }else{ 2027fb7e7651Sdrh Tcl_AppendResult( interp, "bad option \"", 2028a198f2b5Sdrh Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size", 2029a198f2b5Sdrh (char*)0); 2030fb7e7651Sdrh return TCL_ERROR; 2031fb7e7651Sdrh } 2032fb7e7651Sdrh break; 2033fb7e7651Sdrh } 2034fb7e7651Sdrh 2035b28af71aSdanielk1977 /* $db changes 2036c8d30ac1Sdrh ** 2037c8d30ac1Sdrh ** Return the number of rows that were modified, inserted, or deleted by 2038b28af71aSdanielk1977 ** the most recent INSERT, UPDATE or DELETE statement, not including 2039b28af71aSdanielk1977 ** any changes made by trigger programs. 2040c8d30ac1Sdrh */ 2041c8d30ac1Sdrh case DB_CHANGES: { 2042c8d30ac1Sdrh Tcl_Obj *pResult; 2043c8d30ac1Sdrh if( objc!=2 ){ 2044c8d30ac1Sdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 2045c8d30ac1Sdrh return TCL_ERROR; 2046c8d30ac1Sdrh } 2047c8d30ac1Sdrh pResult = Tcl_GetObjResult(interp); 2048b28af71aSdanielk1977 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db)); 2049f146a776Srdc break; 2050f146a776Srdc } 2051f146a776Srdc 205275897234Sdrh /* $db close 205375897234Sdrh ** 205475897234Sdrh ** Shutdown the database 205575897234Sdrh */ 20566d31316cSdrh case DB_CLOSE: { 20576d31316cSdrh Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); 20586d31316cSdrh break; 20596d31316cSdrh } 206075897234Sdrh 20610f14e2ebSdrh /* 20620f14e2ebSdrh ** $db collate NAME SCRIPT 20630f14e2ebSdrh ** 20640f14e2ebSdrh ** Create a new SQL collation function called NAME. Whenever 20650f14e2ebSdrh ** that function is called, invoke SCRIPT to evaluate the function. 20660f14e2ebSdrh */ 20670f14e2ebSdrh case DB_COLLATE: { 20680f14e2ebSdrh SqlCollate *pCollate; 20690f14e2ebSdrh char *zName; 20700f14e2ebSdrh char *zScript; 20710f14e2ebSdrh int nScript; 20720f14e2ebSdrh if( objc!=4 ){ 20730f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); 20740f14e2ebSdrh return TCL_ERROR; 20750f14e2ebSdrh } 20760f14e2ebSdrh zName = Tcl_GetStringFromObj(objv[2], 0); 20770f14e2ebSdrh zScript = Tcl_GetStringFromObj(objv[3], &nScript); 20780f14e2ebSdrh pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 ); 20790f14e2ebSdrh if( pCollate==0 ) return TCL_ERROR; 20800f14e2ebSdrh pCollate->interp = interp; 20810f14e2ebSdrh pCollate->pNext = pDb->pCollate; 20820f14e2ebSdrh pCollate->zScript = (char*)&pCollate[1]; 20830f14e2ebSdrh pDb->pCollate = pCollate; 20845bb3eb9bSdrh memcpy(pCollate->zScript, zScript, nScript+1); 20850f14e2ebSdrh if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, 20860f14e2ebSdrh pCollate, tclSqlCollate) ){ 20879636c4e1Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 20880f14e2ebSdrh return TCL_ERROR; 20890f14e2ebSdrh } 20900f14e2ebSdrh break; 20910f14e2ebSdrh } 20920f14e2ebSdrh 20930f14e2ebSdrh /* 20940f14e2ebSdrh ** $db collation_needed SCRIPT 20950f14e2ebSdrh ** 20960f14e2ebSdrh ** Create a new SQL collation function called NAME. Whenever 20970f14e2ebSdrh ** that function is called, invoke SCRIPT to evaluate the function. 20980f14e2ebSdrh */ 20990f14e2ebSdrh case DB_COLLATION_NEEDED: { 21000f14e2ebSdrh if( objc!=3 ){ 21010f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT"); 21020f14e2ebSdrh return TCL_ERROR; 21030f14e2ebSdrh } 21040f14e2ebSdrh if( pDb->pCollateNeeded ){ 21050f14e2ebSdrh Tcl_DecrRefCount(pDb->pCollateNeeded); 21060f14e2ebSdrh } 21070f14e2ebSdrh pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]); 21080f14e2ebSdrh Tcl_IncrRefCount(pDb->pCollateNeeded); 21090f14e2ebSdrh sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded); 21100f14e2ebSdrh break; 21110f14e2ebSdrh } 21120f14e2ebSdrh 211319e2d37fSdrh /* $db commit_hook ?CALLBACK? 211419e2d37fSdrh ** 211519e2d37fSdrh ** Invoke the given callback just before committing every SQL transaction. 211619e2d37fSdrh ** If the callback throws an exception or returns non-zero, then the 211719e2d37fSdrh ** transaction is aborted. If CALLBACK is an empty string, the callback 211819e2d37fSdrh ** is disabled. 211919e2d37fSdrh */ 212019e2d37fSdrh case DB_COMMIT_HOOK: { 212119e2d37fSdrh if( objc>3 ){ 212219e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 212319e2d37fSdrh return TCL_ERROR; 212419e2d37fSdrh }else if( objc==2 ){ 212519e2d37fSdrh if( pDb->zCommit ){ 2126a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zCommit, (char*)0); 212719e2d37fSdrh } 212819e2d37fSdrh }else{ 21296ef5e12eSmistachkin const char *zCommit; 213019e2d37fSdrh int len; 213119e2d37fSdrh if( pDb->zCommit ){ 213219e2d37fSdrh Tcl_Free(pDb->zCommit); 213319e2d37fSdrh } 213419e2d37fSdrh zCommit = Tcl_GetStringFromObj(objv[2], &len); 213519e2d37fSdrh if( zCommit && len>0 ){ 213619e2d37fSdrh pDb->zCommit = Tcl_Alloc( len + 1 ); 21375bb3eb9bSdrh memcpy(pDb->zCommit, zCommit, len+1); 213819e2d37fSdrh }else{ 213919e2d37fSdrh pDb->zCommit = 0; 214019e2d37fSdrh } 214119e2d37fSdrh if( pDb->zCommit ){ 214219e2d37fSdrh pDb->interp = interp; 214319e2d37fSdrh sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb); 214419e2d37fSdrh }else{ 214519e2d37fSdrh sqlite3_commit_hook(pDb->db, 0, 0); 214619e2d37fSdrh } 214719e2d37fSdrh } 214819e2d37fSdrh break; 214919e2d37fSdrh } 215019e2d37fSdrh 215175897234Sdrh /* $db complete SQL 215275897234Sdrh ** 215375897234Sdrh ** Return TRUE if SQL is a complete SQL statement. Return FALSE if 215475897234Sdrh ** additional lines of input are needed. This is similar to the 215575897234Sdrh ** built-in "info complete" command of Tcl. 215675897234Sdrh */ 21576d31316cSdrh case DB_COMPLETE: { 2158ccae6026Sdrh #ifndef SQLITE_OMIT_COMPLETE 21596d31316cSdrh Tcl_Obj *pResult; 21606d31316cSdrh int isComplete; 21616d31316cSdrh if( objc!=3 ){ 21626d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 216375897234Sdrh return TCL_ERROR; 216475897234Sdrh } 21656f8a503dSdanielk1977 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) ); 21666d31316cSdrh pResult = Tcl_GetObjResult(interp); 21676d31316cSdrh Tcl_SetBooleanObj(pResult, isComplete); 2168ccae6026Sdrh #endif 21696d31316cSdrh break; 21706d31316cSdrh } 217175897234Sdrh 217219e2d37fSdrh /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR? 217319e2d37fSdrh ** 217419e2d37fSdrh ** Copy data into table from filename, optionally using SEPARATOR 217519e2d37fSdrh ** as column separators. If a column contains a null string, or the 217619e2d37fSdrh ** value of NULLINDICATOR, a NULL is inserted for the column. 217719e2d37fSdrh ** conflict-algorithm is one of the sqlite conflict algorithms: 217819e2d37fSdrh ** rollback, abort, fail, ignore, replace 217919e2d37fSdrh ** On success, return the number of lines processed, not necessarily same 218019e2d37fSdrh ** as 'db changes' due to conflict-algorithm selected. 218119e2d37fSdrh ** 218219e2d37fSdrh ** This code is basically an implementation/enhancement of 218319e2d37fSdrh ** the sqlite3 shell.c ".import" command. 218419e2d37fSdrh ** 218519e2d37fSdrh ** This command usage is equivalent to the sqlite2.x COPY statement, 218619e2d37fSdrh ** which imports file data into a table using the PostgreSQL COPY file format: 218719e2d37fSdrh ** $db copy $conflit_algo $table_name $filename \t \\N 218819e2d37fSdrh */ 218919e2d37fSdrh case DB_COPY: { 219019e2d37fSdrh char *zTable; /* Insert data into this table */ 219119e2d37fSdrh char *zFile; /* The file from which to extract data */ 219219e2d37fSdrh char *zConflict; /* The conflict algorithm to use */ 219319e2d37fSdrh sqlite3_stmt *pStmt; /* A statement */ 219419e2d37fSdrh int nCol; /* Number of columns in the table */ 219519e2d37fSdrh int nByte; /* Number of bytes in an SQL string */ 219619e2d37fSdrh int i, j; /* Loop counters */ 219719e2d37fSdrh int nSep; /* Number of bytes in zSep[] */ 219819e2d37fSdrh int nNull; /* Number of bytes in zNull[] */ 219919e2d37fSdrh char *zSql; /* An SQL statement */ 220019e2d37fSdrh char *zLine; /* A single line of input from the file */ 220119e2d37fSdrh char **azCol; /* zLine[] broken up into columns */ 22026ef5e12eSmistachkin const char *zCommit; /* How to commit changes */ 220319e2d37fSdrh FILE *in; /* The input file */ 220419e2d37fSdrh int lineno = 0; /* Line number of input file */ 220519e2d37fSdrh char zLineNum[80]; /* Line number print buffer */ 220619e2d37fSdrh Tcl_Obj *pResult; /* interp result */ 220719e2d37fSdrh 22086ef5e12eSmistachkin const char *zSep; 22096ef5e12eSmistachkin const char *zNull; 221019e2d37fSdrh if( objc<5 || objc>7 ){ 221119e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, 221219e2d37fSdrh "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"); 221319e2d37fSdrh return TCL_ERROR; 221419e2d37fSdrh } 221519e2d37fSdrh if( objc>=6 ){ 221619e2d37fSdrh zSep = Tcl_GetStringFromObj(objv[5], 0); 221719e2d37fSdrh }else{ 221819e2d37fSdrh zSep = "\t"; 221919e2d37fSdrh } 222019e2d37fSdrh if( objc>=7 ){ 222119e2d37fSdrh zNull = Tcl_GetStringFromObj(objv[6], 0); 222219e2d37fSdrh }else{ 222319e2d37fSdrh zNull = ""; 222419e2d37fSdrh } 222519e2d37fSdrh zConflict = Tcl_GetStringFromObj(objv[2], 0); 222619e2d37fSdrh zTable = Tcl_GetStringFromObj(objv[3], 0); 222719e2d37fSdrh zFile = Tcl_GetStringFromObj(objv[4], 0); 22284f21c4afSdrh nSep = strlen30(zSep); 22294f21c4afSdrh nNull = strlen30(zNull); 223019e2d37fSdrh if( nSep==0 ){ 2231a198f2b5Sdrh Tcl_AppendResult(interp,"Error: non-null separator required for copy", 2232a198f2b5Sdrh (char*)0); 223319e2d37fSdrh return TCL_ERROR; 223419e2d37fSdrh } 22353e59c012Sdrh if(strcmp(zConflict, "rollback") != 0 && 22363e59c012Sdrh strcmp(zConflict, "abort" ) != 0 && 22373e59c012Sdrh strcmp(zConflict, "fail" ) != 0 && 22383e59c012Sdrh strcmp(zConflict, "ignore" ) != 0 && 22393e59c012Sdrh strcmp(zConflict, "replace" ) != 0 ) { 224019e2d37fSdrh Tcl_AppendResult(interp, "Error: \"", zConflict, 224119e2d37fSdrh "\", conflict-algorithm must be one of: rollback, " 2242a198f2b5Sdrh "abort, fail, ignore, or replace", (char*)0); 224319e2d37fSdrh return TCL_ERROR; 224419e2d37fSdrh } 224519e2d37fSdrh zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); 224619e2d37fSdrh if( zSql==0 ){ 2247a198f2b5Sdrh Tcl_AppendResult(interp, "Error: no such table: ", zTable, (char*)0); 224819e2d37fSdrh return TCL_ERROR; 224919e2d37fSdrh } 22504f21c4afSdrh nByte = strlen30(zSql); 22513e701a18Sdrh rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 225219e2d37fSdrh sqlite3_free(zSql); 225319e2d37fSdrh if( rc ){ 2254a198f2b5Sdrh Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0); 225519e2d37fSdrh nCol = 0; 225619e2d37fSdrh }else{ 225719e2d37fSdrh nCol = sqlite3_column_count(pStmt); 225819e2d37fSdrh } 225919e2d37fSdrh sqlite3_finalize(pStmt); 226019e2d37fSdrh if( nCol==0 ) { 226119e2d37fSdrh return TCL_ERROR; 226219e2d37fSdrh } 226319e2d37fSdrh zSql = malloc( nByte + 50 + nCol*2 ); 226419e2d37fSdrh if( zSql==0 ) { 2265a198f2b5Sdrh Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0); 226619e2d37fSdrh return TCL_ERROR; 226719e2d37fSdrh } 226819e2d37fSdrh sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?", 226919e2d37fSdrh zConflict, zTable); 22704f21c4afSdrh j = strlen30(zSql); 227119e2d37fSdrh for(i=1; i<nCol; i++){ 227219e2d37fSdrh zSql[j++] = ','; 227319e2d37fSdrh zSql[j++] = '?'; 227419e2d37fSdrh } 227519e2d37fSdrh zSql[j++] = ')'; 227619e2d37fSdrh zSql[j] = 0; 22773e701a18Sdrh rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 227819e2d37fSdrh free(zSql); 227919e2d37fSdrh if( rc ){ 2280a198f2b5Sdrh Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0); 228119e2d37fSdrh sqlite3_finalize(pStmt); 228219e2d37fSdrh return TCL_ERROR; 228319e2d37fSdrh } 228419e2d37fSdrh in = fopen(zFile, "rb"); 228519e2d37fSdrh if( in==0 ){ 228619e2d37fSdrh Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL); 228719e2d37fSdrh sqlite3_finalize(pStmt); 228819e2d37fSdrh return TCL_ERROR; 228919e2d37fSdrh } 229019e2d37fSdrh azCol = malloc( sizeof(azCol[0])*(nCol+1) ); 229119e2d37fSdrh if( azCol==0 ) { 2292a198f2b5Sdrh Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0); 229343617e9aSdrh fclose(in); 229419e2d37fSdrh return TCL_ERROR; 229519e2d37fSdrh } 22963752785fSdrh (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0); 229719e2d37fSdrh zCommit = "COMMIT"; 229819e2d37fSdrh while( (zLine = local_getline(0, in))!=0 ){ 229919e2d37fSdrh char *z; 230019e2d37fSdrh lineno++; 230119e2d37fSdrh azCol[0] = zLine; 230219e2d37fSdrh for(i=0, z=zLine; *z; z++){ 230319e2d37fSdrh if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){ 230419e2d37fSdrh *z = 0; 230519e2d37fSdrh i++; 230619e2d37fSdrh if( i<nCol ){ 230719e2d37fSdrh azCol[i] = &z[nSep]; 230819e2d37fSdrh z += nSep-1; 230919e2d37fSdrh } 231019e2d37fSdrh } 231119e2d37fSdrh } 231219e2d37fSdrh if( i+1!=nCol ){ 231319e2d37fSdrh char *zErr; 23144f21c4afSdrh int nErr = strlen30(zFile) + 200; 23155bb3eb9bSdrh zErr = malloc(nErr); 2316c1f4494eSdrh if( zErr ){ 23175bb3eb9bSdrh sqlite3_snprintf(nErr, zErr, 2318955de52cSdanielk1977 "Error: %s line %d: expected %d columns of data but found %d", 231919e2d37fSdrh zFile, lineno, nCol, i+1); 2320a198f2b5Sdrh Tcl_AppendResult(interp, zErr, (char*)0); 232119e2d37fSdrh free(zErr); 2322c1f4494eSdrh } 232319e2d37fSdrh zCommit = "ROLLBACK"; 232419e2d37fSdrh break; 232519e2d37fSdrh } 232619e2d37fSdrh for(i=0; i<nCol; i++){ 232719e2d37fSdrh /* check for null data, if so, bind as null */ 2328ea678832Sdrh if( (nNull>0 && strcmp(azCol[i], zNull)==0) 23294f21c4afSdrh || strlen30(azCol[i])==0 2330ea678832Sdrh ){ 233119e2d37fSdrh sqlite3_bind_null(pStmt, i+1); 233219e2d37fSdrh }else{ 233319e2d37fSdrh sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); 233419e2d37fSdrh } 233519e2d37fSdrh } 233619e2d37fSdrh sqlite3_step(pStmt); 233719e2d37fSdrh rc = sqlite3_reset(pStmt); 233819e2d37fSdrh free(zLine); 233919e2d37fSdrh if( rc!=SQLITE_OK ){ 2340a198f2b5Sdrh Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), (char*)0); 234119e2d37fSdrh zCommit = "ROLLBACK"; 234219e2d37fSdrh break; 234319e2d37fSdrh } 234419e2d37fSdrh } 234519e2d37fSdrh free(azCol); 234619e2d37fSdrh fclose(in); 234719e2d37fSdrh sqlite3_finalize(pStmt); 23483752785fSdrh (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0); 234919e2d37fSdrh 235019e2d37fSdrh if( zCommit[0] == 'C' ){ 235119e2d37fSdrh /* success, set result as number of lines processed */ 235219e2d37fSdrh pResult = Tcl_GetObjResult(interp); 235319e2d37fSdrh Tcl_SetIntObj(pResult, lineno); 235419e2d37fSdrh rc = TCL_OK; 235519e2d37fSdrh }else{ 235619e2d37fSdrh /* failure, append lineno where failed */ 23575bb3eb9bSdrh sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno); 2358a198f2b5Sdrh Tcl_AppendResult(interp,", failed while processing line: ",zLineNum, 2359a198f2b5Sdrh (char*)0); 236019e2d37fSdrh rc = TCL_ERROR; 236119e2d37fSdrh } 236219e2d37fSdrh break; 236319e2d37fSdrh } 236419e2d37fSdrh 236575897234Sdrh /* 23664144905bSdrh ** $db enable_load_extension BOOLEAN 23674144905bSdrh ** 23684144905bSdrh ** Turn the extension loading feature on or off. It if off by 23694144905bSdrh ** default. 23704144905bSdrh */ 23714144905bSdrh case DB_ENABLE_LOAD_EXTENSION: { 2372f533acc0Sdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 23734144905bSdrh int onoff; 23744144905bSdrh if( objc!=3 ){ 23754144905bSdrh Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN"); 23764144905bSdrh return TCL_ERROR; 23774144905bSdrh } 23784144905bSdrh if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ 23794144905bSdrh return TCL_ERROR; 23804144905bSdrh } 23814144905bSdrh sqlite3_enable_load_extension(pDb->db, onoff); 23824144905bSdrh break; 2383f533acc0Sdrh #else 2384f533acc0Sdrh Tcl_AppendResult(interp, "extension loading is turned off at compile-time", 2385a198f2b5Sdrh (char*)0); 2386f533acc0Sdrh return TCL_ERROR; 2387f533acc0Sdrh #endif 23884144905bSdrh } 23894144905bSdrh 23904144905bSdrh /* 2391dcd997eaSdrh ** $db errorcode 2392dcd997eaSdrh ** 2393dcd997eaSdrh ** Return the numeric error code that was returned by the most recent 23946f8a503dSdanielk1977 ** call to sqlite3_exec(). 2395dcd997eaSdrh */ 2396dcd997eaSdrh case DB_ERRORCODE: { 2397f3ce83f5Sdanielk1977 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); 2398dcd997eaSdrh break; 2399dcd997eaSdrh } 2400dcd997eaSdrh 2401dcd997eaSdrh /* 24024a4c11aaSdan ** $db exists $sql 24031807ce37Sdrh ** $db onecolumn $sql 240475897234Sdrh ** 24054a4c11aaSdan ** The onecolumn method is the equivalent of: 24064a4c11aaSdan ** lindex [$db eval $sql] 0 24074a4c11aaSdan */ 24084a4c11aaSdan case DB_EXISTS: 24094a4c11aaSdan case DB_ONECOLUMN: { 2410edc4024bSdrh Tcl_Obj *pResult = 0; 24114a4c11aaSdan DbEvalContext sEval; 24124a4c11aaSdan if( objc!=3 ){ 24134a4c11aaSdan Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 24144a4c11aaSdan return TCL_ERROR; 24154a4c11aaSdan } 24164a4c11aaSdan 24174a4c11aaSdan dbEvalInit(&sEval, pDb, objv[2], 0); 24184a4c11aaSdan rc = dbEvalStep(&sEval); 24194a4c11aaSdan if( choice==DB_ONECOLUMN ){ 24204a4c11aaSdan if( rc==TCL_OK ){ 2421edc4024bSdrh pResult = dbEvalColumnValue(&sEval, 0); 2422d5f12cd5Sdan }else if( rc==TCL_BREAK ){ 2423d5f12cd5Sdan Tcl_ResetResult(interp); 24244a4c11aaSdan } 24254a4c11aaSdan }else if( rc==TCL_BREAK || rc==TCL_OK ){ 2426edc4024bSdrh pResult = Tcl_NewBooleanObj(rc==TCL_OK); 24274a4c11aaSdan } 24284a4c11aaSdan dbEvalFinalize(&sEval); 2429edc4024bSdrh if( pResult ) Tcl_SetObjResult(interp, pResult); 24304a4c11aaSdan 24314a4c11aaSdan if( rc==TCL_BREAK ){ 24324a4c11aaSdan rc = TCL_OK; 24334a4c11aaSdan } 24344a4c11aaSdan break; 24354a4c11aaSdan } 24364a4c11aaSdan 24374a4c11aaSdan /* 24384a4c11aaSdan ** $db eval $sql ?array? ?{ ...code... }? 24394a4c11aaSdan ** 244075897234Sdrh ** The SQL statement in $sql is evaluated. For each row, the values are 2441bec3f402Sdrh ** placed in elements of the array named "array" and ...code... is executed. 244275897234Sdrh ** If "array" and "code" are omitted, then no callback is every invoked. 244375897234Sdrh ** If "array" is an empty string, then the values are placed in variables 244475897234Sdrh ** that have the same name as the fields extracted by the query. 244575897234Sdrh */ 24464a4c11aaSdan case DB_EVAL: { 244792febd92Sdrh if( objc<3 || objc>5 ){ 2448895d7472Sdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?"); 244930ccda10Sdanielk1977 return TCL_ERROR; 245030ccda10Sdanielk1977 } 24514a4c11aaSdan 245292febd92Sdrh if( objc==3 ){ 24534a4c11aaSdan DbEvalContext sEval; 24544a4c11aaSdan Tcl_Obj *pRet = Tcl_NewObj(); 24554a4c11aaSdan Tcl_IncrRefCount(pRet); 24564a4c11aaSdan dbEvalInit(&sEval, pDb, objv[2], 0); 24574a4c11aaSdan while( TCL_OK==(rc = dbEvalStep(&sEval)) ){ 24584a4c11aaSdan int i; 24594a4c11aaSdan int nCol; 24604a4c11aaSdan dbEvalRowInfo(&sEval, &nCol, 0); 246192febd92Sdrh for(i=0; i<nCol; i++){ 24624a4c11aaSdan Tcl_ListObjAppendElement(interp, pRet, dbEvalColumnValue(&sEval, i)); 246392febd92Sdrh } 246430ccda10Sdanielk1977 } 24654a4c11aaSdan dbEvalFinalize(&sEval); 246690b6bb19Sdrh if( rc==TCL_BREAK ){ 24674a4c11aaSdan Tcl_SetObjResult(interp, pRet); 246890b6bb19Sdrh rc = TCL_OK; 246990b6bb19Sdrh } 2470ef2cb63eSdanielk1977 Tcl_DecrRefCount(pRet); 24714a4c11aaSdan }else{ 24728e18922fSmistachkin ClientData cd2[2]; 24734a4c11aaSdan DbEvalContext *p; 24744a4c11aaSdan Tcl_Obj *pArray = 0; 24754a4c11aaSdan Tcl_Obj *pScript; 24764a4c11aaSdan 24774a4c11aaSdan if( objc==5 && *(char *)Tcl_GetString(objv[3]) ){ 24784a4c11aaSdan pArray = objv[3]; 24794a4c11aaSdan } 24804a4c11aaSdan pScript = objv[objc-1]; 24814a4c11aaSdan Tcl_IncrRefCount(pScript); 24824a4c11aaSdan 24834a4c11aaSdan p = (DbEvalContext *)Tcl_Alloc(sizeof(DbEvalContext)); 24844a4c11aaSdan dbEvalInit(p, pDb, objv[2], pArray); 24854a4c11aaSdan 24868e18922fSmistachkin cd2[0] = (void *)p; 24878e18922fSmistachkin cd2[1] = (void *)pScript; 24888e18922fSmistachkin rc = DbEvalNextCmd(cd2, interp, TCL_OK); 24891807ce37Sdrh } 249030ccda10Sdanielk1977 break; 249130ccda10Sdanielk1977 } 2492bec3f402Sdrh 2493bec3f402Sdrh /* 24943df30592Sdan ** $db function NAME [-argcount N] [-deterministic] SCRIPT 2495cabb0819Sdrh ** 2496cabb0819Sdrh ** Create a new SQL function called NAME. Whenever that function is 2497cabb0819Sdrh ** called, invoke SCRIPT to evaluate the function. 2498cabb0819Sdrh */ 2499cabb0819Sdrh case DB_FUNCTION: { 25003df30592Sdan int flags = SQLITE_UTF8; 2501cabb0819Sdrh SqlFunc *pFunc; 2502d1e4733dSdrh Tcl_Obj *pScript; 2503cabb0819Sdrh char *zName; 2504e3602be8Sdrh int nArg = -1; 25053df30592Sdan int i; 25063df30592Sdan if( objc<4 ){ 25073df30592Sdan Tcl_WrongNumArgs(interp, 2, objv, "NAME ?SWITCHES? SCRIPT"); 25083df30592Sdan return TCL_ERROR; 25093df30592Sdan } 25103df30592Sdan for(i=3; i<(objc-1); i++){ 25113df30592Sdan const char *z = Tcl_GetString(objv[i]); 25124f21c4afSdrh int n = strlen30(z); 2513e3602be8Sdrh if( n>2 && strncmp(z, "-argcount",n)==0 ){ 25143df30592Sdan if( i==(objc-2) ){ 25153df30592Sdan Tcl_AppendResult(interp, "option requires an argument: ", z, 0); 25163df30592Sdan return TCL_ERROR; 25173df30592Sdan } 25183df30592Sdan if( Tcl_GetIntFromObj(interp, objv[i+1], &nArg) ) return TCL_ERROR; 2519e3602be8Sdrh if( nArg<0 ){ 2520e3602be8Sdrh Tcl_AppendResult(interp, "number of arguments must be non-negative", 2521e3602be8Sdrh (char*)0); 2522cabb0819Sdrh return TCL_ERROR; 2523cabb0819Sdrh } 25243df30592Sdan i++; 25253df30592Sdan }else 25263df30592Sdan if( n>2 && strncmp(z, "-deterministic",n)==0 ){ 25273df30592Sdan flags |= SQLITE_DETERMINISTIC; 2528e3602be8Sdrh }else{ 25293df30592Sdan Tcl_AppendResult(interp, "bad option \"", z, 25303df30592Sdan "\": must be -argcount or -deterministic", 0 25313df30592Sdan ); 25323df30592Sdan return TCL_ERROR; 2533e3602be8Sdrh } 25343df30592Sdan } 25353df30592Sdan 25363df30592Sdan pScript = objv[objc-1]; 2537e3602be8Sdrh zName = Tcl_GetStringFromObj(objv[2], 0); 2538d1e4733dSdrh pFunc = findSqlFunc(pDb, zName); 2539cabb0819Sdrh if( pFunc==0 ) return TCL_ERROR; 2540d1e4733dSdrh if( pFunc->pScript ){ 2541d1e4733dSdrh Tcl_DecrRefCount(pFunc->pScript); 2542d1e4733dSdrh } 2543d1e4733dSdrh pFunc->pScript = pScript; 2544d1e4733dSdrh Tcl_IncrRefCount(pScript); 2545d1e4733dSdrh pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript); 25463df30592Sdan rc = sqlite3_create_function(pDb->db, zName, nArg, flags, 2547d8123366Sdanielk1977 pFunc, tclSqlFunc, 0, 0); 2548fb7e7651Sdrh if( rc!=SQLITE_OK ){ 2549fb7e7651Sdrh rc = TCL_ERROR; 25509636c4e1Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 2551fb7e7651Sdrh } 2552cabb0819Sdrh break; 2553cabb0819Sdrh } 2554cabb0819Sdrh 2555cabb0819Sdrh /* 25568cbadb02Sdanielk1977 ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID 2557b4e9af9fSdanielk1977 */ 2558b4e9af9fSdanielk1977 case DB_INCRBLOB: { 255932a0d8bbSdanielk1977 #ifdef SQLITE_OMIT_INCRBLOB 2560a198f2b5Sdrh Tcl_AppendResult(interp, "incrblob not available in this build", (char*)0); 256132a0d8bbSdanielk1977 return TCL_ERROR; 256232a0d8bbSdanielk1977 #else 25638cbadb02Sdanielk1977 int isReadonly = 0; 2564b4e9af9fSdanielk1977 const char *zDb = "main"; 2565b4e9af9fSdanielk1977 const char *zTable; 2566b4e9af9fSdanielk1977 const char *zColumn; 2567b3f787f4Sdrh Tcl_WideInt iRow; 2568b4e9af9fSdanielk1977 25698cbadb02Sdanielk1977 /* Check for the -readonly option */ 25708cbadb02Sdanielk1977 if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){ 25718cbadb02Sdanielk1977 isReadonly = 1; 25728cbadb02Sdanielk1977 } 25738cbadb02Sdanielk1977 25748cbadb02Sdanielk1977 if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){ 25758cbadb02Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID"); 2576b4e9af9fSdanielk1977 return TCL_ERROR; 2577b4e9af9fSdanielk1977 } 2578b4e9af9fSdanielk1977 25798cbadb02Sdanielk1977 if( objc==(6+isReadonly) ){ 2580b4e9af9fSdanielk1977 zDb = Tcl_GetString(objv[2]); 2581b4e9af9fSdanielk1977 } 2582b4e9af9fSdanielk1977 zTable = Tcl_GetString(objv[objc-3]); 2583b4e9af9fSdanielk1977 zColumn = Tcl_GetString(objv[objc-2]); 2584b4e9af9fSdanielk1977 rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow); 2585b4e9af9fSdanielk1977 2586b4e9af9fSdanielk1977 if( rc==TCL_OK ){ 25878cbadb02Sdanielk1977 rc = createIncrblobChannel( 2588edf5b165Sdan interp, pDb, zDb, zTable, zColumn, (sqlite3_int64)iRow, isReadonly 25898cbadb02Sdanielk1977 ); 2590b4e9af9fSdanielk1977 } 259132a0d8bbSdanielk1977 #endif 2592b4e9af9fSdanielk1977 break; 2593b4e9af9fSdanielk1977 } 2594b4e9af9fSdanielk1977 2595b4e9af9fSdanielk1977 /* 2596f11bded5Sdrh ** $db interrupt 2597f11bded5Sdrh ** 2598f11bded5Sdrh ** Interrupt the execution of the inner-most SQL interpreter. This 2599f11bded5Sdrh ** causes the SQL statement to return an error of SQLITE_INTERRUPT. 2600f11bded5Sdrh */ 2601f11bded5Sdrh case DB_INTERRUPT: { 2602f11bded5Sdrh sqlite3_interrupt(pDb->db); 2603f11bded5Sdrh break; 2604f11bded5Sdrh } 2605f11bded5Sdrh 2606f11bded5Sdrh /* 260719e2d37fSdrh ** $db nullvalue ?STRING? 260819e2d37fSdrh ** 260919e2d37fSdrh ** Change text used when a NULL comes back from the database. If ?STRING? 261019e2d37fSdrh ** is not present, then the current string used for NULL is returned. 261119e2d37fSdrh ** If STRING is present, then STRING is returned. 261219e2d37fSdrh ** 261319e2d37fSdrh */ 261419e2d37fSdrh case DB_NULLVALUE: { 261519e2d37fSdrh if( objc!=2 && objc!=3 ){ 261619e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE"); 261719e2d37fSdrh return TCL_ERROR; 261819e2d37fSdrh } 261919e2d37fSdrh if( objc==3 ){ 262019e2d37fSdrh int len; 262119e2d37fSdrh char *zNull = Tcl_GetStringFromObj(objv[2], &len); 262219e2d37fSdrh if( pDb->zNull ){ 262319e2d37fSdrh Tcl_Free(pDb->zNull); 262419e2d37fSdrh } 262519e2d37fSdrh if( zNull && len>0 ){ 262619e2d37fSdrh pDb->zNull = Tcl_Alloc( len + 1 ); 26277fd33929Sdrh memcpy(pDb->zNull, zNull, len); 262819e2d37fSdrh pDb->zNull[len] = '\0'; 262919e2d37fSdrh }else{ 263019e2d37fSdrh pDb->zNull = 0; 263119e2d37fSdrh } 263219e2d37fSdrh } 2633c45e6716Sdrh Tcl_SetObjResult(interp, Tcl_NewStringObj(pDb->zNull, -1)); 263419e2d37fSdrh break; 263519e2d37fSdrh } 263619e2d37fSdrh 263719e2d37fSdrh /* 2638af9ff33aSdrh ** $db last_insert_rowid 2639af9ff33aSdrh ** 2640af9ff33aSdrh ** Return an integer which is the ROWID for the most recent insert. 2641af9ff33aSdrh */ 2642af9ff33aSdrh case DB_LAST_INSERT_ROWID: { 2643af9ff33aSdrh Tcl_Obj *pResult; 2644f7e678d6Sdrh Tcl_WideInt rowid; 2645af9ff33aSdrh if( objc!=2 ){ 2646af9ff33aSdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 2647af9ff33aSdrh return TCL_ERROR; 2648af9ff33aSdrh } 26496f8a503dSdanielk1977 rowid = sqlite3_last_insert_rowid(pDb->db); 2650af9ff33aSdrh pResult = Tcl_GetObjResult(interp); 2651f7e678d6Sdrh Tcl_SetWideIntObj(pResult, rowid); 2652af9ff33aSdrh break; 2653af9ff33aSdrh } 2654af9ff33aSdrh 2655af9ff33aSdrh /* 26564a4c11aaSdan ** The DB_ONECOLUMN method is implemented together with DB_EXISTS. 26575d9d7576Sdrh */ 26581807ce37Sdrh 26591807ce37Sdrh /* $db progress ?N CALLBACK? 26601807ce37Sdrh ** 26611807ce37Sdrh ** Invoke the given callback every N virtual machine opcodes while executing 26621807ce37Sdrh ** queries. 26631807ce37Sdrh */ 26641807ce37Sdrh case DB_PROGRESS: { 26651807ce37Sdrh if( objc==2 ){ 26661807ce37Sdrh if( pDb->zProgress ){ 2667a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zProgress, (char*)0); 26685d9d7576Sdrh } 26691807ce37Sdrh }else if( objc==4 ){ 26701807ce37Sdrh char *zProgress; 26711807ce37Sdrh int len; 26721807ce37Sdrh int N; 26731807ce37Sdrh if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ 26741807ce37Sdrh return TCL_ERROR; 26751807ce37Sdrh }; 26761807ce37Sdrh if( pDb->zProgress ){ 26771807ce37Sdrh Tcl_Free(pDb->zProgress); 26781807ce37Sdrh } 26791807ce37Sdrh zProgress = Tcl_GetStringFromObj(objv[3], &len); 26801807ce37Sdrh if( zProgress && len>0 ){ 26811807ce37Sdrh pDb->zProgress = Tcl_Alloc( len + 1 ); 26825bb3eb9bSdrh memcpy(pDb->zProgress, zProgress, len+1); 26831807ce37Sdrh }else{ 26841807ce37Sdrh pDb->zProgress = 0; 26851807ce37Sdrh } 26861807ce37Sdrh #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 26871807ce37Sdrh if( pDb->zProgress ){ 26881807ce37Sdrh pDb->interp = interp; 26891807ce37Sdrh sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb); 26901807ce37Sdrh }else{ 26911807ce37Sdrh sqlite3_progress_handler(pDb->db, 0, 0, 0); 26921807ce37Sdrh } 26931807ce37Sdrh #endif 26941807ce37Sdrh }else{ 26951807ce37Sdrh Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); 26961807ce37Sdrh return TCL_ERROR; 26975d9d7576Sdrh } 26985d9d7576Sdrh break; 26995d9d7576Sdrh } 27005d9d7576Sdrh 270119e2d37fSdrh /* $db profile ?CALLBACK? 270219e2d37fSdrh ** 270319e2d37fSdrh ** Make arrangements to invoke the CALLBACK routine after each SQL statement 270419e2d37fSdrh ** that has run. The text of the SQL and the amount of elapse time are 270519e2d37fSdrh ** appended to CALLBACK before the script is run. 270619e2d37fSdrh */ 270719e2d37fSdrh case DB_PROFILE: { 270819e2d37fSdrh if( objc>3 ){ 270919e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 271019e2d37fSdrh return TCL_ERROR; 271119e2d37fSdrh }else if( objc==2 ){ 271219e2d37fSdrh if( pDb->zProfile ){ 2713a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zProfile, (char*)0); 271419e2d37fSdrh } 271519e2d37fSdrh }else{ 271619e2d37fSdrh char *zProfile; 271719e2d37fSdrh int len; 271819e2d37fSdrh if( pDb->zProfile ){ 271919e2d37fSdrh Tcl_Free(pDb->zProfile); 272019e2d37fSdrh } 272119e2d37fSdrh zProfile = Tcl_GetStringFromObj(objv[2], &len); 272219e2d37fSdrh if( zProfile && len>0 ){ 272319e2d37fSdrh pDb->zProfile = Tcl_Alloc( len + 1 ); 27245bb3eb9bSdrh memcpy(pDb->zProfile, zProfile, len+1); 272519e2d37fSdrh }else{ 272619e2d37fSdrh pDb->zProfile = 0; 272719e2d37fSdrh } 2728bb201344Sshaneh #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) 272919e2d37fSdrh if( pDb->zProfile ){ 273019e2d37fSdrh pDb->interp = interp; 273119e2d37fSdrh sqlite3_profile(pDb->db, DbProfileHandler, pDb); 273219e2d37fSdrh }else{ 273319e2d37fSdrh sqlite3_profile(pDb->db, 0, 0); 273419e2d37fSdrh } 273519e2d37fSdrh #endif 273619e2d37fSdrh } 273719e2d37fSdrh break; 273819e2d37fSdrh } 273919e2d37fSdrh 27405d9d7576Sdrh /* 274122fbcb8dSdrh ** $db rekey KEY 274222fbcb8dSdrh ** 274322fbcb8dSdrh ** Change the encryption key on the currently open database. 274422fbcb8dSdrh */ 274522fbcb8dSdrh case DB_REKEY: { 274632f57d4cSdrh #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 274722fbcb8dSdrh int nKey; 274822fbcb8dSdrh void *pKey; 2749b07028f7Sdrh #endif 275022fbcb8dSdrh if( objc!=3 ){ 275122fbcb8dSdrh Tcl_WrongNumArgs(interp, 2, objv, "KEY"); 275222fbcb8dSdrh return TCL_ERROR; 275322fbcb8dSdrh } 275432f57d4cSdrh #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 2755b07028f7Sdrh pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); 27562011d5f5Sdrh rc = sqlite3_rekey(pDb->db, pKey, nKey); 275722fbcb8dSdrh if( rc ){ 2758a198f2b5Sdrh Tcl_AppendResult(interp, sqlite3_errstr(rc), (char*)0); 275922fbcb8dSdrh rc = TCL_ERROR; 276022fbcb8dSdrh } 276122fbcb8dSdrh #endif 276222fbcb8dSdrh break; 276322fbcb8dSdrh } 276422fbcb8dSdrh 2765dc2c4915Sdrh /* $db restore ?DATABASE? FILENAME 2766dc2c4915Sdrh ** 2767dc2c4915Sdrh ** Open a database file named FILENAME. Transfer the content 2768dc2c4915Sdrh ** of FILENAME into the local database DATABASE (default: "main"). 2769dc2c4915Sdrh */ 2770dc2c4915Sdrh case DB_RESTORE: { 2771dc2c4915Sdrh const char *zSrcFile; 2772dc2c4915Sdrh const char *zDestDb; 2773dc2c4915Sdrh sqlite3 *pSrc; 2774dc2c4915Sdrh sqlite3_backup *pBackup; 2775dc2c4915Sdrh int nTimeout = 0; 2776dc2c4915Sdrh 2777dc2c4915Sdrh if( objc==3 ){ 2778dc2c4915Sdrh zDestDb = "main"; 2779dc2c4915Sdrh zSrcFile = Tcl_GetString(objv[2]); 2780dc2c4915Sdrh }else if( objc==4 ){ 2781dc2c4915Sdrh zDestDb = Tcl_GetString(objv[2]); 2782dc2c4915Sdrh zSrcFile = Tcl_GetString(objv[3]); 2783dc2c4915Sdrh }else{ 2784dc2c4915Sdrh Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 2785dc2c4915Sdrh return TCL_ERROR; 2786dc2c4915Sdrh } 2787147ef394Sdrh rc = sqlite3_open_v2(zSrcFile, &pSrc, 2788147ef394Sdrh SQLITE_OPEN_READONLY | pDb->openFlags, 0); 2789dc2c4915Sdrh if( rc!=SQLITE_OK ){ 2790dc2c4915Sdrh Tcl_AppendResult(interp, "cannot open source database: ", 2791dc2c4915Sdrh sqlite3_errmsg(pSrc), (char*)0); 2792dc2c4915Sdrh sqlite3_close(pSrc); 2793dc2c4915Sdrh return TCL_ERROR; 2794dc2c4915Sdrh } 2795dc2c4915Sdrh pBackup = sqlite3_backup_init(pDb->db, zDestDb, pSrc, "main"); 2796dc2c4915Sdrh if( pBackup==0 ){ 2797dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: ", 2798dc2c4915Sdrh sqlite3_errmsg(pDb->db), (char*)0); 2799dc2c4915Sdrh sqlite3_close(pSrc); 2800dc2c4915Sdrh return TCL_ERROR; 2801dc2c4915Sdrh } 2802dc2c4915Sdrh while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK 2803dc2c4915Sdrh || rc==SQLITE_BUSY ){ 2804dc2c4915Sdrh if( rc==SQLITE_BUSY ){ 2805dc2c4915Sdrh if( nTimeout++ >= 3 ) break; 2806dc2c4915Sdrh sqlite3_sleep(100); 2807dc2c4915Sdrh } 2808dc2c4915Sdrh } 2809dc2c4915Sdrh sqlite3_backup_finish(pBackup); 2810dc2c4915Sdrh if( rc==SQLITE_DONE ){ 2811dc2c4915Sdrh rc = TCL_OK; 2812dc2c4915Sdrh }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 2813dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: source database busy", 2814dc2c4915Sdrh (char*)0); 2815dc2c4915Sdrh rc = TCL_ERROR; 2816dc2c4915Sdrh }else{ 2817dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: ", 2818dc2c4915Sdrh sqlite3_errmsg(pDb->db), (char*)0); 2819dc2c4915Sdrh rc = TCL_ERROR; 2820dc2c4915Sdrh } 2821dc2c4915Sdrh sqlite3_close(pSrc); 2822dc2c4915Sdrh break; 2823dc2c4915Sdrh } 2824dc2c4915Sdrh 282522fbcb8dSdrh /* 28263c379b01Sdrh ** $db status (step|sort|autoindex) 2827d1d38488Sdrh ** 2828d1d38488Sdrh ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or 2829d1d38488Sdrh ** SQLITE_STMTSTATUS_SORT for the most recent eval. 2830d1d38488Sdrh */ 2831d1d38488Sdrh case DB_STATUS: { 2832d1d38488Sdrh int v; 2833d1d38488Sdrh const char *zOp; 2834d1d38488Sdrh if( objc!=3 ){ 28351c320a43Sdrh Tcl_WrongNumArgs(interp, 2, objv, "(step|sort|autoindex)"); 2836d1d38488Sdrh return TCL_ERROR; 2837d1d38488Sdrh } 2838d1d38488Sdrh zOp = Tcl_GetString(objv[2]); 2839d1d38488Sdrh if( strcmp(zOp, "step")==0 ){ 2840d1d38488Sdrh v = pDb->nStep; 2841d1d38488Sdrh }else if( strcmp(zOp, "sort")==0 ){ 2842d1d38488Sdrh v = pDb->nSort; 28433c379b01Sdrh }else if( strcmp(zOp, "autoindex")==0 ){ 28443c379b01Sdrh v = pDb->nIndex; 2845d1d38488Sdrh }else{ 28463c379b01Sdrh Tcl_AppendResult(interp, 28473c379b01Sdrh "bad argument: should be autoindex, step, or sort", 2848d1d38488Sdrh (char*)0); 2849d1d38488Sdrh return TCL_ERROR; 2850d1d38488Sdrh } 2851d1d38488Sdrh Tcl_SetObjResult(interp, Tcl_NewIntObj(v)); 2852d1d38488Sdrh break; 2853d1d38488Sdrh } 2854d1d38488Sdrh 2855d1d38488Sdrh /* 2856bec3f402Sdrh ** $db timeout MILLESECONDS 2857bec3f402Sdrh ** 2858bec3f402Sdrh ** Delay for the number of milliseconds specified when a file is locked. 2859bec3f402Sdrh */ 28606d31316cSdrh case DB_TIMEOUT: { 2861bec3f402Sdrh int ms; 28626d31316cSdrh if( objc!=3 ){ 28636d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); 2864bec3f402Sdrh return TCL_ERROR; 286575897234Sdrh } 28666d31316cSdrh if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; 28676f8a503dSdanielk1977 sqlite3_busy_timeout(pDb->db, ms); 28686d31316cSdrh break; 286975897234Sdrh } 2870b5a20d3cSdrh 28710f14e2ebSdrh /* 28720f14e2ebSdrh ** $db total_changes 28730f14e2ebSdrh ** 28740f14e2ebSdrh ** Return the number of rows that were modified, inserted, or deleted 28750f14e2ebSdrh ** since the database handle was created. 28760f14e2ebSdrh */ 28770f14e2ebSdrh case DB_TOTAL_CHANGES: { 28780f14e2ebSdrh Tcl_Obj *pResult; 28790f14e2ebSdrh if( objc!=2 ){ 28800f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 28810f14e2ebSdrh return TCL_ERROR; 28820f14e2ebSdrh } 28830f14e2ebSdrh pResult = Tcl_GetObjResult(interp); 28840f14e2ebSdrh Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db)); 28850f14e2ebSdrh break; 28860f14e2ebSdrh } 28870f14e2ebSdrh 2888b5a20d3cSdrh /* $db trace ?CALLBACK? 2889b5a20d3cSdrh ** 2890b5a20d3cSdrh ** Make arrangements to invoke the CALLBACK routine for each SQL statement 2891b5a20d3cSdrh ** that is executed. The text of the SQL is appended to CALLBACK before 2892b5a20d3cSdrh ** it is executed. 2893b5a20d3cSdrh */ 2894b5a20d3cSdrh case DB_TRACE: { 2895b5a20d3cSdrh if( objc>3 ){ 2896b5a20d3cSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 2897b97759edSdrh return TCL_ERROR; 2898b5a20d3cSdrh }else if( objc==2 ){ 2899b5a20d3cSdrh if( pDb->zTrace ){ 2900a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zTrace, (char*)0); 2901b5a20d3cSdrh } 2902b5a20d3cSdrh }else{ 2903b5a20d3cSdrh char *zTrace; 2904b5a20d3cSdrh int len; 2905b5a20d3cSdrh if( pDb->zTrace ){ 2906b5a20d3cSdrh Tcl_Free(pDb->zTrace); 2907b5a20d3cSdrh } 2908b5a20d3cSdrh zTrace = Tcl_GetStringFromObj(objv[2], &len); 2909b5a20d3cSdrh if( zTrace && len>0 ){ 2910b5a20d3cSdrh pDb->zTrace = Tcl_Alloc( len + 1 ); 29115bb3eb9bSdrh memcpy(pDb->zTrace, zTrace, len+1); 2912b5a20d3cSdrh }else{ 2913b5a20d3cSdrh pDb->zTrace = 0; 2914b5a20d3cSdrh } 2915bb201344Sshaneh #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) 2916b5a20d3cSdrh if( pDb->zTrace ){ 2917b5a20d3cSdrh pDb->interp = interp; 29186f8a503dSdanielk1977 sqlite3_trace(pDb->db, DbTraceHandler, pDb); 2919b5a20d3cSdrh }else{ 29206f8a503dSdanielk1977 sqlite3_trace(pDb->db, 0, 0); 2921b5a20d3cSdrh } 292219e2d37fSdrh #endif 2923b5a20d3cSdrh } 2924b5a20d3cSdrh break; 2925b5a20d3cSdrh } 2926b5a20d3cSdrh 2927b56660f5Smistachkin /* $db trace_v2 ?CALLBACK? ?MASK? 2928b56660f5Smistachkin ** 2929b56660f5Smistachkin ** Make arrangements to invoke the CALLBACK routine for each trace event 2930b56660f5Smistachkin ** matching the mask that is generated. The parameters are appended to 2931b56660f5Smistachkin ** CALLBACK before it is executed. 2932b56660f5Smistachkin */ 2933b56660f5Smistachkin case DB_TRACE_V2: { 2934b56660f5Smistachkin if( objc>4 ){ 2935b56660f5Smistachkin Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK? ?MASK?"); 2936b56660f5Smistachkin return TCL_ERROR; 2937b56660f5Smistachkin }else if( objc==2 ){ 2938b56660f5Smistachkin if( pDb->zTraceV2 ){ 2939b56660f5Smistachkin Tcl_AppendResult(interp, pDb->zTraceV2, (char*)0); 2940b56660f5Smistachkin } 2941b56660f5Smistachkin }else{ 2942b56660f5Smistachkin char *zTraceV2; 2943b56660f5Smistachkin int len; 2944*b52dcd89Smistachkin Tcl_WideInt wMask = 0; 2945b56660f5Smistachkin if( objc==4 ){ 2946*b52dcd89Smistachkin static const char *TTYPE_strs[] = { 2947*b52dcd89Smistachkin "statement", "profile", "row", "close", 0 2948*b52dcd89Smistachkin }; 2949*b52dcd89Smistachkin enum TTYPE_enum { 2950*b52dcd89Smistachkin TTYPE_STMT, TTYPE_PROFILE, TTYPE_ROW, TTYPE_CLOSE 2951*b52dcd89Smistachkin }; 2952*b52dcd89Smistachkin int i; 2953*b52dcd89Smistachkin if( TCL_OK!=Tcl_ListObjLength(interp, objv[3], &len) ){ 2954*b52dcd89Smistachkin return TCL_ERROR; 2955*b52dcd89Smistachkin } 2956*b52dcd89Smistachkin for(i=0; i<len; i++){ 2957*b52dcd89Smistachkin Tcl_Obj *pObj; 2958*b52dcd89Smistachkin int ttype; 2959*b52dcd89Smistachkin if( TCL_OK!=Tcl_ListObjIndex(interp, objv[3], i, &pObj) ){ 2960*b52dcd89Smistachkin return TCL_ERROR; 2961*b52dcd89Smistachkin } 2962*b52dcd89Smistachkin if( Tcl_GetIndexFromObj(interp, pObj, TTYPE_strs, "trace type", 2963*b52dcd89Smistachkin 0, &ttype)!=TCL_OK ){ 2964*b52dcd89Smistachkin Tcl_WideInt wType; 2965*b52dcd89Smistachkin Tcl_Obj *pError = Tcl_DuplicateObj(Tcl_GetObjResult(interp)); 2966*b52dcd89Smistachkin Tcl_IncrRefCount(pError); 2967*b52dcd89Smistachkin if( TCL_OK==Tcl_GetWideIntFromObj(interp, pObj, &wType) ){ 2968*b52dcd89Smistachkin Tcl_DecrRefCount(pError); 2969*b52dcd89Smistachkin wMask |= wType; 2970*b52dcd89Smistachkin }else{ 2971*b52dcd89Smistachkin Tcl_SetObjResult(interp, pError); 2972*b52dcd89Smistachkin Tcl_DecrRefCount(pError); 2973b56660f5Smistachkin return TCL_ERROR; 2974b56660f5Smistachkin } 2975b56660f5Smistachkin }else{ 2976*b52dcd89Smistachkin switch( (enum TTYPE_enum)ttype ){ 2977*b52dcd89Smistachkin case TTYPE_STMT: wMask |= SQLITE_TRACE_STMT; break; 2978*b52dcd89Smistachkin case TTYPE_PROFILE: wMask |= SQLITE_TRACE_PROFILE; break; 2979*b52dcd89Smistachkin case TTYPE_ROW: wMask |= SQLITE_TRACE_ROW; break; 2980*b52dcd89Smistachkin case TTYPE_CLOSE: wMask |= SQLITE_TRACE_CLOSE; break; 2981*b52dcd89Smistachkin } 2982*b52dcd89Smistachkin } 2983*b52dcd89Smistachkin } 2984*b52dcd89Smistachkin }else{ 2985*b52dcd89Smistachkin wMask = SQLITE_TRACE_STMT; /* use the "legacy" default */ 2986b56660f5Smistachkin } 2987b56660f5Smistachkin if( pDb->zTraceV2 ){ 2988b56660f5Smistachkin Tcl_Free(pDb->zTraceV2); 2989b56660f5Smistachkin } 2990b56660f5Smistachkin zTraceV2 = Tcl_GetStringFromObj(objv[2], &len); 2991b56660f5Smistachkin if( zTraceV2 && len>0 ){ 2992b56660f5Smistachkin pDb->zTraceV2 = Tcl_Alloc( len + 1 ); 2993b56660f5Smistachkin memcpy(pDb->zTraceV2, zTraceV2, len+1); 2994b56660f5Smistachkin }else{ 2995b56660f5Smistachkin pDb->zTraceV2 = 0; 2996b56660f5Smistachkin } 2997b56660f5Smistachkin #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) 2998b56660f5Smistachkin if( pDb->zTraceV2 ){ 2999b56660f5Smistachkin pDb->interp = interp; 3000b56660f5Smistachkin sqlite3_trace_v2(pDb->db, (unsigned)wMask, DbTraceV2Handler, pDb); 3001b56660f5Smistachkin }else{ 3002b56660f5Smistachkin sqlite3_trace_v2(pDb->db, 0, 0, 0); 3003b56660f5Smistachkin } 3004b56660f5Smistachkin #endif 3005b56660f5Smistachkin } 3006b56660f5Smistachkin break; 3007b56660f5Smistachkin } 3008b56660f5Smistachkin 30093d21423cSdrh /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT 30103d21423cSdrh ** 30113d21423cSdrh ** Start a new transaction (if we are not already in the midst of a 30123d21423cSdrh ** transaction) and execute the TCL script SCRIPT. After SCRIPT 30133d21423cSdrh ** completes, either commit the transaction or roll it back if SCRIPT 30143d21423cSdrh ** throws an exception. Or if no new transation was started, do nothing. 30153d21423cSdrh ** pass the exception on up the stack. 30163d21423cSdrh ** 30173d21423cSdrh ** This command was inspired by Dave Thomas's talk on Ruby at the 30183d21423cSdrh ** 2005 O'Reilly Open Source Convention (OSCON). 30193d21423cSdrh */ 30203d21423cSdrh case DB_TRANSACTION: { 30213d21423cSdrh Tcl_Obj *pScript; 3022cd38d520Sdanielk1977 const char *zBegin = "SAVEPOINT _tcl_transaction"; 30233d21423cSdrh if( objc!=3 && objc!=4 ){ 30243d21423cSdrh Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT"); 30253d21423cSdrh return TCL_ERROR; 30263d21423cSdrh } 3027cd38d520Sdanielk1977 30284a4c11aaSdan if( pDb->nTransaction==0 && objc==4 ){ 30293d21423cSdrh static const char *TTYPE_strs[] = { 3030ce604012Sdrh "deferred", "exclusive", "immediate", 0 30313d21423cSdrh }; 30323d21423cSdrh enum TTYPE_enum { 30333d21423cSdrh TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE 30343d21423cSdrh }; 30353d21423cSdrh int ttype; 3036b5555e7eSdrh if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type", 30373d21423cSdrh 0, &ttype) ){ 30383d21423cSdrh return TCL_ERROR; 30393d21423cSdrh } 30403d21423cSdrh switch( (enum TTYPE_enum)ttype ){ 30413d21423cSdrh case TTYPE_DEFERRED: /* no-op */; break; 30423d21423cSdrh case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break; 30433d21423cSdrh case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break; 30443d21423cSdrh } 30453d21423cSdrh } 3046cd38d520Sdanielk1977 pScript = objv[objc-1]; 3047cd38d520Sdanielk1977 30484a4c11aaSdan /* Run the SQLite BEGIN command to open a transaction or savepoint. */ 30491f1549f8Sdrh pDb->disableAuth++; 3050cd38d520Sdanielk1977 rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0); 30511f1549f8Sdrh pDb->disableAuth--; 3052cd38d520Sdanielk1977 if( rc!=SQLITE_OK ){ 3053a198f2b5Sdrh Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 3054cd38d520Sdanielk1977 return TCL_ERROR; 30553d21423cSdrh } 3056cd38d520Sdanielk1977 pDb->nTransaction++; 3057cd38d520Sdanielk1977 30584a4c11aaSdan /* If using NRE, schedule a callback to invoke the script pScript, then 30594a4c11aaSdan ** a second callback to commit (or rollback) the transaction or savepoint 30604a4c11aaSdan ** opened above. If not using NRE, evaluate the script directly, then 30614a4c11aaSdan ** call function DbTransPostCmd() to commit (or rollback) the transaction 30624a4c11aaSdan ** or savepoint. */ 30634a4c11aaSdan if( DbUseNre() ){ 30644a4c11aaSdan Tcl_NRAddCallback(interp, DbTransPostCmd, cd, 0, 0, 0); 3065a47941feSdrh (void)Tcl_NREvalObj(interp, pScript, 0); 30663d21423cSdrh }else{ 30674a4c11aaSdan rc = DbTransPostCmd(&cd, interp, Tcl_EvalObjEx(interp, pScript, 0)); 30683d21423cSdrh } 30693d21423cSdrh break; 30703d21423cSdrh } 30713d21423cSdrh 307294eb6a14Sdanielk1977 /* 3073404ca075Sdanielk1977 ** $db unlock_notify ?script? 3074404ca075Sdanielk1977 */ 3075404ca075Sdanielk1977 case DB_UNLOCK_NOTIFY: { 3076404ca075Sdanielk1977 #ifndef SQLITE_ENABLE_UNLOCK_NOTIFY 3077a198f2b5Sdrh Tcl_AppendResult(interp, "unlock_notify not available in this build", 3078a198f2b5Sdrh (char*)0); 3079404ca075Sdanielk1977 rc = TCL_ERROR; 3080404ca075Sdanielk1977 #else 3081404ca075Sdanielk1977 if( objc!=2 && objc!=3 ){ 3082404ca075Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 3083404ca075Sdanielk1977 rc = TCL_ERROR; 3084404ca075Sdanielk1977 }else{ 3085404ca075Sdanielk1977 void (*xNotify)(void **, int) = 0; 3086404ca075Sdanielk1977 void *pNotifyArg = 0; 3087404ca075Sdanielk1977 3088404ca075Sdanielk1977 if( pDb->pUnlockNotify ){ 3089404ca075Sdanielk1977 Tcl_DecrRefCount(pDb->pUnlockNotify); 3090404ca075Sdanielk1977 pDb->pUnlockNotify = 0; 3091404ca075Sdanielk1977 } 3092404ca075Sdanielk1977 3093404ca075Sdanielk1977 if( objc==3 ){ 3094404ca075Sdanielk1977 xNotify = DbUnlockNotify; 3095404ca075Sdanielk1977 pNotifyArg = (void *)pDb; 3096404ca075Sdanielk1977 pDb->pUnlockNotify = objv[2]; 3097404ca075Sdanielk1977 Tcl_IncrRefCount(pDb->pUnlockNotify); 3098404ca075Sdanielk1977 } 3099404ca075Sdanielk1977 3100404ca075Sdanielk1977 if( sqlite3_unlock_notify(pDb->db, xNotify, pNotifyArg) ){ 3101a198f2b5Sdrh Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 3102404ca075Sdanielk1977 rc = TCL_ERROR; 3103404ca075Sdanielk1977 } 3104404ca075Sdanielk1977 } 3105404ca075Sdanielk1977 #endif 3106404ca075Sdanielk1977 break; 3107404ca075Sdanielk1977 } 3108404ca075Sdanielk1977 3109304637c0Sdrh /* 3110304637c0Sdrh ** $db preupdate_hook count 3111304637c0Sdrh ** $db preupdate_hook hook ?SCRIPT? 3112304637c0Sdrh ** $db preupdate_hook new INDEX 3113304637c0Sdrh ** $db preupdate_hook old INDEX 3114304637c0Sdrh */ 311546c47d46Sdan case DB_PREUPDATE: { 31169b1c62d4Sdrh #ifndef SQLITE_ENABLE_PREUPDATE_HOOK 31179b1c62d4Sdrh Tcl_AppendResult(interp, "preupdate_hook was omitted at compile-time"); 31189b1c62d4Sdrh rc = TCL_ERROR; 31199b1c62d4Sdrh #else 31201e7a2d43Sdan static const char *azSub[] = {"count", "depth", "hook", "new", "old", 0}; 312146c47d46Sdan enum DbPreupdateSubCmd { 31221e7a2d43Sdan PRE_COUNT, PRE_DEPTH, PRE_HOOK, PRE_NEW, PRE_OLD 312346c47d46Sdan }; 312446c47d46Sdan int iSub; 312546c47d46Sdan 312646c47d46Sdan if( objc<3 ){ 312746c47d46Sdan Tcl_WrongNumArgs(interp, 2, objv, "SUB-COMMAND ?ARGS?"); 312846c47d46Sdan } 312946c47d46Sdan if( Tcl_GetIndexFromObj(interp, objv[2], azSub, "sub-command", 0, &iSub) ){ 313046c47d46Sdan return TCL_ERROR; 313146c47d46Sdan } 313246c47d46Sdan 313346c47d46Sdan switch( (enum DbPreupdateSubCmd)iSub ){ 313446c47d46Sdan case PRE_COUNT: { 313546c47d46Sdan int nCol = sqlite3_preupdate_count(pDb->db); 313646c47d46Sdan Tcl_SetObjResult(interp, Tcl_NewIntObj(nCol)); 313746c47d46Sdan break; 313846c47d46Sdan } 313946c47d46Sdan 314046c47d46Sdan case PRE_HOOK: { 314146c47d46Sdan if( objc>4 ){ 314246c47d46Sdan Tcl_WrongNumArgs(interp, 2, objv, "hook ?SCRIPT?"); 314346c47d46Sdan return TCL_ERROR; 314446c47d46Sdan } 314546c47d46Sdan DbHookCmd(interp, pDb, (objc==4 ? objv[3] : 0), &pDb->pPreUpdateHook); 314646c47d46Sdan break; 314746c47d46Sdan } 314846c47d46Sdan 31491e7a2d43Sdan case PRE_DEPTH: { 31501e7a2d43Sdan Tcl_Obj *pRet; 31511e7a2d43Sdan if( objc!=3 ){ 31521e7a2d43Sdan Tcl_WrongNumArgs(interp, 3, objv, ""); 31531e7a2d43Sdan return TCL_ERROR; 31541e7a2d43Sdan } 31551e7a2d43Sdan pRet = Tcl_NewIntObj(sqlite3_preupdate_depth(pDb->db)); 31561e7a2d43Sdan Tcl_SetObjResult(interp, pRet); 31571e7a2d43Sdan break; 31581e7a2d43Sdan } 31591e7a2d43Sdan 316037db03bfSdan case PRE_NEW: 316146c47d46Sdan case PRE_OLD: { 316246c47d46Sdan int iIdx; 316337db03bfSdan sqlite3_value *pValue; 316446c47d46Sdan if( objc!=4 ){ 316546c47d46Sdan Tcl_WrongNumArgs(interp, 3, objv, "INDEX"); 316646c47d46Sdan return TCL_ERROR; 316746c47d46Sdan } 316846c47d46Sdan if( Tcl_GetIntFromObj(interp, objv[3], &iIdx) ){ 316946c47d46Sdan return TCL_ERROR; 317046c47d46Sdan } 317146c47d46Sdan 317237db03bfSdan if( iSub==PRE_OLD ){ 317346c47d46Sdan rc = sqlite3_preupdate_old(pDb->db, iIdx, &pValue); 317437db03bfSdan }else{ 317537db03bfSdan assert( iSub==PRE_NEW ); 317637db03bfSdan rc = sqlite3_preupdate_new(pDb->db, iIdx, &pValue); 317737db03bfSdan } 317837db03bfSdan 317946c47d46Sdan if( rc==SQLITE_OK ){ 3180304637c0Sdrh Tcl_Obj *pObj; 3181304637c0Sdrh pObj = Tcl_NewStringObj((char*)sqlite3_value_text(pValue), -1); 318246c47d46Sdan Tcl_SetObjResult(interp, pObj); 318337db03bfSdan }else{ 318446c47d46Sdan Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); 318546c47d46Sdan return TCL_ERROR; 318646c47d46Sdan } 318746c47d46Sdan } 318846c47d46Sdan } 31899b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 319046c47d46Sdan break; 319146c47d46Sdan } 319246c47d46Sdan 3193404ca075Sdanielk1977 /* 3194833bf968Sdrh ** $db wal_hook ?script? 319594eb6a14Sdanielk1977 ** $db update_hook ?script? 319671fd80bfSdanielk1977 ** $db rollback_hook ?script? 319794eb6a14Sdanielk1977 */ 3198833bf968Sdrh case DB_WAL_HOOK: 319971fd80bfSdanielk1977 case DB_UPDATE_HOOK: 32006566ebe1Sdan case DB_ROLLBACK_HOOK: { 320171fd80bfSdanielk1977 /* set ppHook to point at pUpdateHook or pRollbackHook, depending on 320271fd80bfSdanielk1977 ** whether [$db update_hook] or [$db rollback_hook] was invoked. 320371fd80bfSdanielk1977 */ 3204c5896b5cSmistachkin Tcl_Obj **ppHook = 0; 320546c47d46Sdan if( choice==DB_WAL_HOOK ) ppHook = &pDb->pWalHook; 320646c47d46Sdan if( choice==DB_UPDATE_HOOK ) ppHook = &pDb->pUpdateHook; 320746c47d46Sdan if( choice==DB_ROLLBACK_HOOK ) ppHook = &pDb->pRollbackHook; 320846c47d46Sdan if( objc>3 ){ 320994eb6a14Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 321094eb6a14Sdanielk1977 return TCL_ERROR; 321194eb6a14Sdanielk1977 } 321271fd80bfSdanielk1977 321346c47d46Sdan DbHookCmd(interp, pDb, (objc==3 ? objv[2] : 0), ppHook); 321494eb6a14Sdanielk1977 break; 321594eb6a14Sdanielk1977 } 321694eb6a14Sdanielk1977 32174397de57Sdanielk1977 /* $db version 32184397de57Sdanielk1977 ** 32194397de57Sdanielk1977 ** Return the version string for this database. 32204397de57Sdanielk1977 */ 32214397de57Sdanielk1977 case DB_VERSION: { 32224397de57Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC); 32234397de57Sdanielk1977 break; 32244397de57Sdanielk1977 } 32254397de57Sdanielk1977 32261067fe11Stpoindex 32276d31316cSdrh } /* End of the SWITCH statement */ 322822fbcb8dSdrh return rc; 322975897234Sdrh } 323075897234Sdrh 3231a2c8a95bSdrh #if SQLITE_TCL_NRE 3232a2c8a95bSdrh /* 3233a2c8a95bSdrh ** Adaptor that provides an objCmd interface to the NRE-enabled 3234a2c8a95bSdrh ** interface implementation. 3235a2c8a95bSdrh */ 3236a2c8a95bSdrh static int DbObjCmdAdaptor( 3237a2c8a95bSdrh void *cd, 3238a2c8a95bSdrh Tcl_Interp *interp, 3239a2c8a95bSdrh int objc, 3240a2c8a95bSdrh Tcl_Obj *const*objv 3241a2c8a95bSdrh ){ 3242a2c8a95bSdrh return Tcl_NRCallObjProc(interp, DbObjCmd, cd, objc, objv); 3243a2c8a95bSdrh } 3244a2c8a95bSdrh #endif /* SQLITE_TCL_NRE */ 3245a2c8a95bSdrh 324675897234Sdrh /* 32473570ad93Sdrh ** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN? 32489a6284c1Sdanielk1977 ** ?-create BOOLEAN? ?-nomutex BOOLEAN? 324975897234Sdrh ** 325075897234Sdrh ** This is the main Tcl command. When the "sqlite" Tcl command is 325175897234Sdrh ** invoked, this routine runs to process that command. 325275897234Sdrh ** 325375897234Sdrh ** The first argument, DBNAME, is an arbitrary name for a new 325475897234Sdrh ** database connection. This command creates a new command named 325575897234Sdrh ** DBNAME that is used to control that connection. The database 325675897234Sdrh ** connection is deleted when the DBNAME command is deleted. 325775897234Sdrh ** 32583570ad93Sdrh ** The second argument is the name of the database file. 3259fbc3eab8Sdrh ** 326075897234Sdrh */ 326122fbcb8dSdrh static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ 3262bec3f402Sdrh SqliteDb *p; 326322fbcb8dSdrh const char *zArg; 326475897234Sdrh char *zErrMsg; 32653570ad93Sdrh int i; 326622fbcb8dSdrh const char *zFile; 32673570ad93Sdrh const char *zVfs = 0; 3268d9da78a2Sdrh int flags; 3269882e8e4dSdrh Tcl_DString translatedFilename; 327032f57d4cSdrh #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3271b07028f7Sdrh void *pKey = 0; 3272b07028f7Sdrh int nKey = 0; 3273b07028f7Sdrh #endif 3274540ebf82Smistachkin int rc; 3275d9da78a2Sdrh 3276d9da78a2Sdrh /* In normal use, each TCL interpreter runs in a single thread. So 3277d9da78a2Sdrh ** by default, we can turn of mutexing on SQLite database connections. 3278d9da78a2Sdrh ** However, for testing purposes it is useful to have mutexes turned 3279d9da78a2Sdrh ** on. So, by default, mutexes default off. But if compiled with 3280d9da78a2Sdrh ** SQLITE_TCL_DEFAULT_FULLMUTEX then mutexes default on. 3281d9da78a2Sdrh */ 3282d9da78a2Sdrh #ifdef SQLITE_TCL_DEFAULT_FULLMUTEX 3283d9da78a2Sdrh flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX; 3284d9da78a2Sdrh #else 3285d9da78a2Sdrh flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX; 3286d9da78a2Sdrh #endif 3287d9da78a2Sdrh 328822fbcb8dSdrh if( objc==2 ){ 328922fbcb8dSdrh zArg = Tcl_GetStringFromObj(objv[1], 0); 329022fbcb8dSdrh if( strcmp(zArg,"-version")==0 ){ 3291a198f2b5Sdrh Tcl_AppendResult(interp,sqlite3_libversion(), (char*)0); 3292647cb0e1Sdrh return TCL_OK; 3293647cb0e1Sdrh } 329472bf6a3eSdrh if( strcmp(zArg,"-sourceid")==0 ){ 329572bf6a3eSdrh Tcl_AppendResult(interp,sqlite3_sourceid(), (char*)0); 329672bf6a3eSdrh return TCL_OK; 329772bf6a3eSdrh } 32989eb9e26bSdrh if( strcmp(zArg,"-has-codec")==0 ){ 329932f57d4cSdrh #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3300a198f2b5Sdrh Tcl_AppendResult(interp,"1",(char*)0); 330122fbcb8dSdrh #else 3302a198f2b5Sdrh Tcl_AppendResult(interp,"0",(char*)0); 330322fbcb8dSdrh #endif 330422fbcb8dSdrh return TCL_OK; 330522fbcb8dSdrh } 3306fbc3eab8Sdrh } 33073570ad93Sdrh for(i=3; i+1<objc; i+=2){ 33083570ad93Sdrh zArg = Tcl_GetString(objv[i]); 330922fbcb8dSdrh if( strcmp(zArg,"-key")==0 ){ 331032f57d4cSdrh #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 33113570ad93Sdrh pKey = Tcl_GetByteArrayFromObj(objv[i+1], &nKey); 3312b07028f7Sdrh #endif 33133570ad93Sdrh }else if( strcmp(zArg, "-vfs")==0 ){ 33143c3dd7b9Sdan zVfs = Tcl_GetString(objv[i+1]); 33153570ad93Sdrh }else if( strcmp(zArg, "-readonly")==0 ){ 33163570ad93Sdrh int b; 33173570ad93Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 33183570ad93Sdrh if( b ){ 331933f4e02aSdrh flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); 33203570ad93Sdrh flags |= SQLITE_OPEN_READONLY; 33213570ad93Sdrh }else{ 33223570ad93Sdrh flags &= ~SQLITE_OPEN_READONLY; 33233570ad93Sdrh flags |= SQLITE_OPEN_READWRITE; 33243570ad93Sdrh } 33253570ad93Sdrh }else if( strcmp(zArg, "-create")==0 ){ 33263570ad93Sdrh int b; 33273570ad93Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 332833f4e02aSdrh if( b && (flags & SQLITE_OPEN_READONLY)==0 ){ 33293570ad93Sdrh flags |= SQLITE_OPEN_CREATE; 33303570ad93Sdrh }else{ 33313570ad93Sdrh flags &= ~SQLITE_OPEN_CREATE; 33323570ad93Sdrh } 33339a6284c1Sdanielk1977 }else if( strcmp(zArg, "-nomutex")==0 ){ 33349a6284c1Sdanielk1977 int b; 33359a6284c1Sdanielk1977 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 33369a6284c1Sdanielk1977 if( b ){ 33379a6284c1Sdanielk1977 flags |= SQLITE_OPEN_NOMUTEX; 3338039963adSdrh flags &= ~SQLITE_OPEN_FULLMUTEX; 33399a6284c1Sdanielk1977 }else{ 33409a6284c1Sdanielk1977 flags &= ~SQLITE_OPEN_NOMUTEX; 33419a6284c1Sdanielk1977 } 3342039963adSdrh }else if( strcmp(zArg, "-fullmutex")==0 ){ 3343039963adSdrh int b; 3344039963adSdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 3345039963adSdrh if( b ){ 3346039963adSdrh flags |= SQLITE_OPEN_FULLMUTEX; 3347039963adSdrh flags &= ~SQLITE_OPEN_NOMUTEX; 3348039963adSdrh }else{ 3349039963adSdrh flags &= ~SQLITE_OPEN_FULLMUTEX; 3350039963adSdrh } 3351f12b3f60Sdrh }else if( strcmp(zArg, "-uri")==0 ){ 3352f12b3f60Sdrh int b; 3353f12b3f60Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 3354f12b3f60Sdrh if( b ){ 3355f12b3f60Sdrh flags |= SQLITE_OPEN_URI; 3356f12b3f60Sdrh }else{ 3357f12b3f60Sdrh flags &= ~SQLITE_OPEN_URI; 3358f12b3f60Sdrh } 33593570ad93Sdrh }else{ 33603570ad93Sdrh Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0); 33613570ad93Sdrh return TCL_ERROR; 336222fbcb8dSdrh } 336322fbcb8dSdrh } 33643570ad93Sdrh if( objc<3 || (objc&1)!=1 ){ 336522fbcb8dSdrh Tcl_WrongNumArgs(interp, 1, objv, 33663570ad93Sdrh "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" 336768bd4aa2Sdrh " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?" 336832f57d4cSdrh #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 33693570ad93Sdrh " ?-key CODECKEY?" 337022fbcb8dSdrh #endif 337122fbcb8dSdrh ); 337275897234Sdrh return TCL_ERROR; 337375897234Sdrh } 337475897234Sdrh zErrMsg = 0; 33754cdc9e84Sdrh p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); 337675897234Sdrh if( p==0 ){ 33776ef5e12eSmistachkin Tcl_SetResult(interp, (char *)"malloc failed", TCL_STATIC); 3378bec3f402Sdrh return TCL_ERROR; 3379bec3f402Sdrh } 3380bec3f402Sdrh memset(p, 0, sizeof(*p)); 338122fbcb8dSdrh zFile = Tcl_GetStringFromObj(objv[2], 0); 3382882e8e4dSdrh zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); 3383540ebf82Smistachkin rc = sqlite3_open_v2(zFile, &p->db, flags, zVfs); 3384882e8e4dSdrh Tcl_DStringFree(&translatedFilename); 3385540ebf82Smistachkin if( p->db ){ 338680290863Sdanielk1977 if( SQLITE_OK!=sqlite3_errcode(p->db) ){ 33879404d50eSdrh zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); 338880290863Sdanielk1977 sqlite3_close(p->db); 338980290863Sdanielk1977 p->db = 0; 339080290863Sdanielk1977 } 3391540ebf82Smistachkin }else{ 33925dac8432Smistachkin zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc)); 3393540ebf82Smistachkin } 339432f57d4cSdrh #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3395f3a65f7eSdrh if( p->db ){ 33962011d5f5Sdrh sqlite3_key(p->db, pKey, nKey); 3397f3a65f7eSdrh } 3398eb8ed70dSdrh #endif 3399bec3f402Sdrh if( p->db==0 ){ 340075897234Sdrh Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 3401bec3f402Sdrh Tcl_Free((char*)p); 34029404d50eSdrh sqlite3_free(zErrMsg); 340375897234Sdrh return TCL_ERROR; 340475897234Sdrh } 3405fb7e7651Sdrh p->maxStmt = NUM_PREPARED_STMTS; 3406147ef394Sdrh p->openFlags = flags & SQLITE_OPEN_URI; 34075169bbc6Sdrh p->interp = interp; 340822fbcb8dSdrh zArg = Tcl_GetStringFromObj(objv[1], 0); 34094a4c11aaSdan if( DbUseNre() ){ 3410a2c8a95bSdrh Tcl_NRCreateCommand(interp, zArg, DbObjCmdAdaptor, DbObjCmd, 3411a2c8a95bSdrh (char*)p, DbDeleteCmd); 34124a4c11aaSdan }else{ 341322fbcb8dSdrh Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); 34144a4c11aaSdan } 341575897234Sdrh return TCL_OK; 341675897234Sdrh } 341775897234Sdrh 341875897234Sdrh /* 341990ca9753Sdrh ** Provide a dummy Tcl_InitStubs if we are using this as a static 342090ca9753Sdrh ** library. 342190ca9753Sdrh */ 342290ca9753Sdrh #ifndef USE_TCL_STUBS 342390ca9753Sdrh # undef Tcl_InitStubs 34240e85ccfcSdrh # define Tcl_InitStubs(a,b,c) TCL_VERSION 342590ca9753Sdrh #endif 342690ca9753Sdrh 342790ca9753Sdrh /* 342829bc4615Sdrh ** Make sure we have a PACKAGE_VERSION macro defined. This will be 342929bc4615Sdrh ** defined automatically by the TEA makefile. But other makefiles 343029bc4615Sdrh ** do not define it. 343129bc4615Sdrh */ 343229bc4615Sdrh #ifndef PACKAGE_VERSION 343329bc4615Sdrh # define PACKAGE_VERSION SQLITE_VERSION 343429bc4615Sdrh #endif 343529bc4615Sdrh 343629bc4615Sdrh /* 343775897234Sdrh ** Initialize this module. 343875897234Sdrh ** 343975897234Sdrh ** This Tcl module contains only a single new Tcl command named "sqlite". 344075897234Sdrh ** (Hence there is no namespace. There is no point in using a namespace 344175897234Sdrh ** if the extension only supplies one new name!) The "sqlite" command is 344275897234Sdrh ** used to open a new SQLite database. See the DbMain() routine above 344375897234Sdrh ** for additional information. 3444b652f432Sdrh ** 3445b652f432Sdrh ** The EXTERN macros are required by TCL in order to work on windows. 344675897234Sdrh */ 3447b652f432Sdrh EXTERN int Sqlite3_Init(Tcl_Interp *interp){ 344827b2f053Smistachkin int rc = Tcl_InitStubs(interp, "8.4", 0) ? TCL_OK : TCL_ERROR; 34496dc8cbe0Sdrh if( rc==TCL_OK ){ 3450ef4ac8f9Sdrh Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); 34511cca0d22Sdrh #ifndef SQLITE_3_SUFFIX_ONLY 34521cca0d22Sdrh /* The "sqlite" alias is undocumented. It is here only to support 34531cca0d22Sdrh ** legacy scripts. All new scripts should use only the "sqlite3" 34546dc8cbe0Sdrh ** command. */ 345549766d6cSdrh Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); 34564c0f1649Sdrh #endif 34576dc8cbe0Sdrh rc = Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION); 34586dc8cbe0Sdrh } 34596dc8cbe0Sdrh return rc; 346090ca9753Sdrh } 3461b652f432Sdrh EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3462b652f432Sdrh EXTERN int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3463b652f432Sdrh EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3464e2c3a659Sdrh 3465d878cab5Sdrh /* Because it accesses the file-system and uses persistent state, SQLite 3466e75a9eb9Sdrh ** is not considered appropriate for safe interpreters. Hence, we cause 3467e75a9eb9Sdrh ** the _SafeInit() interfaces return TCL_ERROR. 3468d878cab5Sdrh */ 3469e75a9eb9Sdrh EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_ERROR; } 3470e75a9eb9Sdrh EXTERN int Sqlite3_SafeUnload(Tcl_Interp *interp, int flags){return TCL_ERROR;} 3471e75a9eb9Sdrh 3472e75a9eb9Sdrh 347349766d6cSdrh 347449766d6cSdrh #ifndef SQLITE_3_SUFFIX_ONLY 3475a3e63c4aSdan int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3476a3e63c4aSdan int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3477a3e63c4aSdan int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3478a3e63c4aSdan int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 347949766d6cSdrh #endif 348075897234Sdrh 34813e27c026Sdrh #ifdef TCLSH 34823e27c026Sdrh /***************************************************************************** 348357a0227fSdrh ** All of the code that follows is used to build standalone TCL interpreters 348457a0227fSdrh ** that are statically linked with SQLite. Enable these by compiling 348557a0227fSdrh ** with -DTCLSH=n where n can be 1 or 2. An n of 1 generates a standard 348657a0227fSdrh ** tclsh but with SQLite built in. An n of 2 generates the SQLite space 348757a0227fSdrh ** analysis program. 348875897234Sdrh */ 3489348784efSdrh 349057a0227fSdrh #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) 349157a0227fSdrh /* 349257a0227fSdrh * This code implements the MD5 message-digest algorithm. 349357a0227fSdrh * The algorithm is due to Ron Rivest. This code was 349457a0227fSdrh * written by Colin Plumb in 1993, no copyright is claimed. 349557a0227fSdrh * This code is in the public domain; do with it what you wish. 349657a0227fSdrh * 349757a0227fSdrh * Equivalent code is available from RSA Data Security, Inc. 349857a0227fSdrh * This code has been tested against that, and is equivalent, 349957a0227fSdrh * except that you don't need to include two pages of legalese 350057a0227fSdrh * with every copy. 350157a0227fSdrh * 350257a0227fSdrh * To compute the message digest of a chunk of bytes, declare an 350357a0227fSdrh * MD5Context structure, pass it to MD5Init, call MD5Update as 350457a0227fSdrh * needed on buffers full of bytes, and then call MD5Final, which 350557a0227fSdrh * will fill a supplied 16-byte array with the digest. 350657a0227fSdrh */ 350757a0227fSdrh 350857a0227fSdrh /* 350957a0227fSdrh * If compiled on a machine that doesn't have a 32-bit integer, 351057a0227fSdrh * you just set "uint32" to the appropriate datatype for an 351157a0227fSdrh * unsigned 32-bit integer. For example: 351257a0227fSdrh * 351357a0227fSdrh * cc -Duint32='unsigned long' md5.c 351457a0227fSdrh * 351557a0227fSdrh */ 351657a0227fSdrh #ifndef uint32 351757a0227fSdrh # define uint32 unsigned int 351857a0227fSdrh #endif 351957a0227fSdrh 352057a0227fSdrh struct MD5Context { 352157a0227fSdrh int isInit; 352257a0227fSdrh uint32 buf[4]; 352357a0227fSdrh uint32 bits[2]; 352457a0227fSdrh unsigned char in[64]; 352557a0227fSdrh }; 352657a0227fSdrh typedef struct MD5Context MD5Context; 352757a0227fSdrh 352857a0227fSdrh /* 352957a0227fSdrh * Note: this code is harmless on little-endian machines. 353057a0227fSdrh */ 353157a0227fSdrh static void byteReverse (unsigned char *buf, unsigned longs){ 353257a0227fSdrh uint32 t; 353357a0227fSdrh do { 353457a0227fSdrh t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 | 353557a0227fSdrh ((unsigned)buf[1]<<8 | buf[0]); 353657a0227fSdrh *(uint32 *)buf = t; 353757a0227fSdrh buf += 4; 353857a0227fSdrh } while (--longs); 353957a0227fSdrh } 354057a0227fSdrh /* The four core functions - F1 is optimized somewhat */ 354157a0227fSdrh 354257a0227fSdrh /* #define F1(x, y, z) (x & y | ~x & z) */ 354357a0227fSdrh #define F1(x, y, z) (z ^ (x & (y ^ z))) 354457a0227fSdrh #define F2(x, y, z) F1(z, x, y) 354557a0227fSdrh #define F3(x, y, z) (x ^ y ^ z) 354657a0227fSdrh #define F4(x, y, z) (y ^ (x | ~z)) 354757a0227fSdrh 354857a0227fSdrh /* This is the central step in the MD5 algorithm. */ 354957a0227fSdrh #define MD5STEP(f, w, x, y, z, data, s) \ 355057a0227fSdrh ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) 355157a0227fSdrh 355257a0227fSdrh /* 355357a0227fSdrh * The core of the MD5 algorithm, this alters an existing MD5 hash to 355457a0227fSdrh * reflect the addition of 16 longwords of new data. MD5Update blocks 355557a0227fSdrh * the data and converts bytes into longwords for this routine. 355657a0227fSdrh */ 355757a0227fSdrh static void MD5Transform(uint32 buf[4], const uint32 in[16]){ 355857a0227fSdrh register uint32 a, b, c, d; 355957a0227fSdrh 356057a0227fSdrh a = buf[0]; 356157a0227fSdrh b = buf[1]; 356257a0227fSdrh c = buf[2]; 356357a0227fSdrh d = buf[3]; 356457a0227fSdrh 356557a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); 356657a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); 356757a0227fSdrh MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); 356857a0227fSdrh MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); 356957a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); 357057a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); 357157a0227fSdrh MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); 357257a0227fSdrh MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); 357357a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); 357457a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); 357557a0227fSdrh MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); 357657a0227fSdrh MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); 357757a0227fSdrh MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); 357857a0227fSdrh MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); 357957a0227fSdrh MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); 358057a0227fSdrh MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); 358157a0227fSdrh 358257a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); 358357a0227fSdrh MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); 358457a0227fSdrh MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); 358557a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); 358657a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); 358757a0227fSdrh MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); 358857a0227fSdrh MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); 358957a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); 359057a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); 359157a0227fSdrh MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); 359257a0227fSdrh MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); 359357a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); 359457a0227fSdrh MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); 359557a0227fSdrh MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); 359657a0227fSdrh MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); 359757a0227fSdrh MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); 359857a0227fSdrh 359957a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); 360057a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); 360157a0227fSdrh MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); 360257a0227fSdrh MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); 360357a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); 360457a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); 360557a0227fSdrh MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); 360657a0227fSdrh MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); 360757a0227fSdrh MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); 360857a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); 360957a0227fSdrh MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); 361057a0227fSdrh MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); 361157a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); 361257a0227fSdrh MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); 361357a0227fSdrh MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); 361457a0227fSdrh MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); 361557a0227fSdrh 361657a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); 361757a0227fSdrh MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); 361857a0227fSdrh MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); 361957a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); 362057a0227fSdrh MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); 362157a0227fSdrh MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); 362257a0227fSdrh MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); 362357a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); 362457a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); 362557a0227fSdrh MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); 362657a0227fSdrh MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); 362757a0227fSdrh MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); 362857a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); 362957a0227fSdrh MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); 363057a0227fSdrh MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); 363157a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); 363257a0227fSdrh 363357a0227fSdrh buf[0] += a; 363457a0227fSdrh buf[1] += b; 363557a0227fSdrh buf[2] += c; 363657a0227fSdrh buf[3] += d; 363757a0227fSdrh } 363857a0227fSdrh 363957a0227fSdrh /* 364057a0227fSdrh * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 364157a0227fSdrh * initialization constants. 364257a0227fSdrh */ 364357a0227fSdrh static void MD5Init(MD5Context *ctx){ 364457a0227fSdrh ctx->isInit = 1; 364557a0227fSdrh ctx->buf[0] = 0x67452301; 364657a0227fSdrh ctx->buf[1] = 0xefcdab89; 364757a0227fSdrh ctx->buf[2] = 0x98badcfe; 364857a0227fSdrh ctx->buf[3] = 0x10325476; 364957a0227fSdrh ctx->bits[0] = 0; 365057a0227fSdrh ctx->bits[1] = 0; 365157a0227fSdrh } 365257a0227fSdrh 365357a0227fSdrh /* 365457a0227fSdrh * Update context to reflect the concatenation of another buffer full 365557a0227fSdrh * of bytes. 365657a0227fSdrh */ 365757a0227fSdrh static 365857a0227fSdrh void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){ 365957a0227fSdrh uint32 t; 366057a0227fSdrh 366157a0227fSdrh /* Update bitcount */ 366257a0227fSdrh 366357a0227fSdrh t = ctx->bits[0]; 366457a0227fSdrh if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) 366557a0227fSdrh ctx->bits[1]++; /* Carry from low to high */ 366657a0227fSdrh ctx->bits[1] += len >> 29; 366757a0227fSdrh 366857a0227fSdrh t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ 366957a0227fSdrh 367057a0227fSdrh /* Handle any leading odd-sized chunks */ 367157a0227fSdrh 367257a0227fSdrh if ( t ) { 367357a0227fSdrh unsigned char *p = (unsigned char *)ctx->in + t; 367457a0227fSdrh 367557a0227fSdrh t = 64-t; 367657a0227fSdrh if (len < t) { 367757a0227fSdrh memcpy(p, buf, len); 367857a0227fSdrh return; 367957a0227fSdrh } 368057a0227fSdrh memcpy(p, buf, t); 368157a0227fSdrh byteReverse(ctx->in, 16); 368257a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 368357a0227fSdrh buf += t; 368457a0227fSdrh len -= t; 368557a0227fSdrh } 368657a0227fSdrh 368757a0227fSdrh /* Process data in 64-byte chunks */ 368857a0227fSdrh 368957a0227fSdrh while (len >= 64) { 369057a0227fSdrh memcpy(ctx->in, buf, 64); 369157a0227fSdrh byteReverse(ctx->in, 16); 369257a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 369357a0227fSdrh buf += 64; 369457a0227fSdrh len -= 64; 369557a0227fSdrh } 369657a0227fSdrh 369757a0227fSdrh /* Handle any remaining bytes of data. */ 369857a0227fSdrh 369957a0227fSdrh memcpy(ctx->in, buf, len); 370057a0227fSdrh } 370157a0227fSdrh 370257a0227fSdrh /* 370357a0227fSdrh * Final wrapup - pad to 64-byte boundary with the bit pattern 370457a0227fSdrh * 1 0* (64-bit count of bits processed, MSB-first) 370557a0227fSdrh */ 370657a0227fSdrh static void MD5Final(unsigned char digest[16], MD5Context *ctx){ 370757a0227fSdrh unsigned count; 370857a0227fSdrh unsigned char *p; 370957a0227fSdrh 371057a0227fSdrh /* Compute number of bytes mod 64 */ 371157a0227fSdrh count = (ctx->bits[0] >> 3) & 0x3F; 371257a0227fSdrh 371357a0227fSdrh /* Set the first char of padding to 0x80. This is safe since there is 371457a0227fSdrh always at least one byte free */ 371557a0227fSdrh p = ctx->in + count; 371657a0227fSdrh *p++ = 0x80; 371757a0227fSdrh 371857a0227fSdrh /* Bytes of padding needed to make 64 bytes */ 371957a0227fSdrh count = 64 - 1 - count; 372057a0227fSdrh 372157a0227fSdrh /* Pad out to 56 mod 64 */ 372257a0227fSdrh if (count < 8) { 372357a0227fSdrh /* Two lots of padding: Pad the first block to 64 bytes */ 372457a0227fSdrh memset(p, 0, count); 372557a0227fSdrh byteReverse(ctx->in, 16); 372657a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 372757a0227fSdrh 372857a0227fSdrh /* Now fill the next block with 56 bytes */ 372957a0227fSdrh memset(ctx->in, 0, 56); 373057a0227fSdrh } else { 373157a0227fSdrh /* Pad block to 56 bytes */ 373257a0227fSdrh memset(p, 0, count-8); 373357a0227fSdrh } 373457a0227fSdrh byteReverse(ctx->in, 14); 373557a0227fSdrh 373657a0227fSdrh /* Append length in bits and transform */ 3737a47941feSdrh memcpy(ctx->in + 14*4, ctx->bits, 8); 373857a0227fSdrh 373957a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 374057a0227fSdrh byteReverse((unsigned char *)ctx->buf, 4); 374157a0227fSdrh memcpy(digest, ctx->buf, 16); 374257a0227fSdrh } 374357a0227fSdrh 374457a0227fSdrh /* 374557a0227fSdrh ** Convert a 128-bit MD5 digest into a 32-digit base-16 number. 374657a0227fSdrh */ 374757a0227fSdrh static void MD5DigestToBase16(unsigned char *digest, char *zBuf){ 374857a0227fSdrh static char const zEncode[] = "0123456789abcdef"; 374957a0227fSdrh int i, j; 375057a0227fSdrh 375157a0227fSdrh for(j=i=0; i<16; i++){ 375257a0227fSdrh int a = digest[i]; 375357a0227fSdrh zBuf[j++] = zEncode[(a>>4)&0xf]; 375457a0227fSdrh zBuf[j++] = zEncode[a & 0xf]; 375557a0227fSdrh } 375657a0227fSdrh zBuf[j] = 0; 375757a0227fSdrh } 375857a0227fSdrh 375957a0227fSdrh 376057a0227fSdrh /* 376157a0227fSdrh ** Convert a 128-bit MD5 digest into sequency of eight 5-digit integers 376257a0227fSdrh ** each representing 16 bits of the digest and separated from each 376357a0227fSdrh ** other by a "-" character. 376457a0227fSdrh */ 376557a0227fSdrh static void MD5DigestToBase10x8(unsigned char digest[16], char zDigest[50]){ 376657a0227fSdrh int i, j; 376757a0227fSdrh unsigned int x; 376857a0227fSdrh for(i=j=0; i<16; i+=2){ 376957a0227fSdrh x = digest[i]*256 + digest[i+1]; 377057a0227fSdrh if( i>0 ) zDigest[j++] = '-'; 377105f6c67cSdrh sqlite3_snprintf(50-j, &zDigest[j], "%05u", x); 377257a0227fSdrh j += 5; 377357a0227fSdrh } 377457a0227fSdrh zDigest[j] = 0; 377557a0227fSdrh } 377657a0227fSdrh 377757a0227fSdrh /* 377857a0227fSdrh ** A TCL command for md5. The argument is the text to be hashed. The 377957a0227fSdrh ** Result is the hash in base64. 378057a0227fSdrh */ 378157a0227fSdrh static int md5_cmd(void*cd, Tcl_Interp *interp, int argc, const char **argv){ 378257a0227fSdrh MD5Context ctx; 378357a0227fSdrh unsigned char digest[16]; 378457a0227fSdrh char zBuf[50]; 378557a0227fSdrh void (*converter)(unsigned char*, char*); 378657a0227fSdrh 378757a0227fSdrh if( argc!=2 ){ 378857a0227fSdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 3789a198f2b5Sdrh " TEXT\"", (char*)0); 379057a0227fSdrh return TCL_ERROR; 379157a0227fSdrh } 379257a0227fSdrh MD5Init(&ctx); 379357a0227fSdrh MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1])); 379457a0227fSdrh MD5Final(digest, &ctx); 379557a0227fSdrh converter = (void(*)(unsigned char*,char*))cd; 379657a0227fSdrh converter(digest, zBuf); 379757a0227fSdrh Tcl_AppendResult(interp, zBuf, (char*)0); 379857a0227fSdrh return TCL_OK; 379957a0227fSdrh } 380057a0227fSdrh 380157a0227fSdrh /* 380257a0227fSdrh ** A TCL command to take the md5 hash of a file. The argument is the 380357a0227fSdrh ** name of the file. 380457a0227fSdrh */ 380557a0227fSdrh static int md5file_cmd(void*cd, Tcl_Interp*interp, int argc, const char **argv){ 380657a0227fSdrh FILE *in; 380757a0227fSdrh MD5Context ctx; 380857a0227fSdrh void (*converter)(unsigned char*, char*); 380957a0227fSdrh unsigned char digest[16]; 381057a0227fSdrh char zBuf[10240]; 381157a0227fSdrh 381257a0227fSdrh if( argc!=2 ){ 381357a0227fSdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 3814a198f2b5Sdrh " FILENAME\"", (char*)0); 381557a0227fSdrh return TCL_ERROR; 381657a0227fSdrh } 381757a0227fSdrh in = fopen(argv[1],"rb"); 381857a0227fSdrh if( in==0 ){ 381957a0227fSdrh Tcl_AppendResult(interp,"unable to open file \"", argv[1], 3820a198f2b5Sdrh "\" for reading", (char*)0); 382157a0227fSdrh return TCL_ERROR; 382257a0227fSdrh } 382357a0227fSdrh MD5Init(&ctx); 382457a0227fSdrh for(;;){ 382557a0227fSdrh int n; 382683cc1392Sdrh n = (int)fread(zBuf, 1, sizeof(zBuf), in); 382757a0227fSdrh if( n<=0 ) break; 382857a0227fSdrh MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n); 382957a0227fSdrh } 383057a0227fSdrh fclose(in); 383157a0227fSdrh MD5Final(digest, &ctx); 383257a0227fSdrh converter = (void(*)(unsigned char*,char*))cd; 383357a0227fSdrh converter(digest, zBuf); 383457a0227fSdrh Tcl_AppendResult(interp, zBuf, (char*)0); 383557a0227fSdrh return TCL_OK; 383657a0227fSdrh } 383757a0227fSdrh 383857a0227fSdrh /* 383957a0227fSdrh ** Register the four new TCL commands for generating MD5 checksums 384057a0227fSdrh ** with the TCL interpreter. 384157a0227fSdrh */ 384257a0227fSdrh int Md5_Init(Tcl_Interp *interp){ 384357a0227fSdrh Tcl_CreateCommand(interp, "md5", (Tcl_CmdProc*)md5_cmd, 384457a0227fSdrh MD5DigestToBase16, 0); 384557a0227fSdrh Tcl_CreateCommand(interp, "md5-10x8", (Tcl_CmdProc*)md5_cmd, 384657a0227fSdrh MD5DigestToBase10x8, 0); 384757a0227fSdrh Tcl_CreateCommand(interp, "md5file", (Tcl_CmdProc*)md5file_cmd, 384857a0227fSdrh MD5DigestToBase16, 0); 384957a0227fSdrh Tcl_CreateCommand(interp, "md5file-10x8", (Tcl_CmdProc*)md5file_cmd, 385057a0227fSdrh MD5DigestToBase10x8, 0); 385157a0227fSdrh return TCL_OK; 385257a0227fSdrh } 385357a0227fSdrh #endif /* defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) */ 385457a0227fSdrh 385557a0227fSdrh #if defined(SQLITE_TEST) 385657a0227fSdrh /* 385757a0227fSdrh ** During testing, the special md5sum() aggregate function is available. 385857a0227fSdrh ** inside SQLite. The following routines implement that function. 385957a0227fSdrh */ 386057a0227fSdrh static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){ 386157a0227fSdrh MD5Context *p; 386257a0227fSdrh int i; 386357a0227fSdrh if( argc<1 ) return; 386457a0227fSdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 386557a0227fSdrh if( p==0 ) return; 386657a0227fSdrh if( !p->isInit ){ 386757a0227fSdrh MD5Init(p); 386857a0227fSdrh } 386957a0227fSdrh for(i=0; i<argc; i++){ 387057a0227fSdrh const char *zData = (char*)sqlite3_value_text(argv[i]); 387157a0227fSdrh if( zData ){ 387283cc1392Sdrh MD5Update(p, (unsigned char*)zData, (int)strlen(zData)); 387357a0227fSdrh } 387457a0227fSdrh } 387557a0227fSdrh } 387657a0227fSdrh static void md5finalize(sqlite3_context *context){ 387757a0227fSdrh MD5Context *p; 387857a0227fSdrh unsigned char digest[16]; 387957a0227fSdrh char zBuf[33]; 388057a0227fSdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 388157a0227fSdrh MD5Final(digest,p); 388257a0227fSdrh MD5DigestToBase16(digest, zBuf); 388357a0227fSdrh sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 388457a0227fSdrh } 388557a0227fSdrh int Md5_Register(sqlite3 *db){ 388657a0227fSdrh int rc = sqlite3_create_function(db, "md5sum", -1, SQLITE_UTF8, 0, 0, 388757a0227fSdrh md5step, md5finalize); 388857a0227fSdrh sqlite3_overload_function(db, "md5sum", -1); /* To exercise this API */ 388957a0227fSdrh return rc; 389057a0227fSdrh } 389157a0227fSdrh #endif /* defined(SQLITE_TEST) */ 389257a0227fSdrh 389357a0227fSdrh 3894348784efSdrh /* 38953e27c026Sdrh ** If the macro TCLSH is one, then put in code this for the 38963e27c026Sdrh ** "main" routine that will initialize Tcl and take input from 38973570ad93Sdrh ** standard input, or if a file is named on the command line 38983570ad93Sdrh ** the TCL interpreter reads and evaluates that file. 3899348784efSdrh */ 39003e27c026Sdrh #if TCLSH==1 39010ae479dfSdan static const char *tclsh_main_loop(void){ 39020ae479dfSdan static const char zMainloop[] = 3903348784efSdrh "set line {}\n" 3904348784efSdrh "while {![eof stdin]} {\n" 3905348784efSdrh "if {$line!=\"\"} {\n" 3906348784efSdrh "puts -nonewline \"> \"\n" 3907348784efSdrh "} else {\n" 3908348784efSdrh "puts -nonewline \"% \"\n" 3909348784efSdrh "}\n" 3910348784efSdrh "flush stdout\n" 3911348784efSdrh "append line [gets stdin]\n" 3912348784efSdrh "if {[info complete $line]} {\n" 3913348784efSdrh "if {[catch {uplevel #0 $line} result]} {\n" 3914348784efSdrh "puts stderr \"Error: $result\"\n" 3915348784efSdrh "} elseif {$result!=\"\"} {\n" 3916348784efSdrh "puts $result\n" 3917348784efSdrh "}\n" 3918348784efSdrh "set line {}\n" 3919348784efSdrh "} else {\n" 3920348784efSdrh "append line \\n\n" 3921348784efSdrh "}\n" 3922348784efSdrh "}\n" 3923348784efSdrh ; 39240ae479dfSdan return zMainloop; 39250ae479dfSdan } 39263e27c026Sdrh #endif 39273a0f13ffSdrh #if TCLSH==2 39280ae479dfSdan static const char *tclsh_main_loop(void); 39293a0f13ffSdrh #endif 39303e27c026Sdrh 3931c1a60c51Sdan #ifdef SQLITE_TEST 3932c1a60c51Sdan static void init_all(Tcl_Interp *); 3933c1a60c51Sdan static int init_all_cmd( 3934c1a60c51Sdan ClientData cd, 3935c1a60c51Sdan Tcl_Interp *interp, 3936c1a60c51Sdan int objc, 3937c1a60c51Sdan Tcl_Obj *CONST objv[] 3938c1a60c51Sdan ){ 39390a549071Sdanielk1977 3940c1a60c51Sdan Tcl_Interp *slave; 3941c1a60c51Sdan if( objc!=2 ){ 3942c1a60c51Sdan Tcl_WrongNumArgs(interp, 1, objv, "SLAVE"); 3943c1a60c51Sdan return TCL_ERROR; 3944c1a60c51Sdan } 39450a549071Sdanielk1977 3946c1a60c51Sdan slave = Tcl_GetSlave(interp, Tcl_GetString(objv[1])); 3947c1a60c51Sdan if( !slave ){ 3948c1a60c51Sdan return TCL_ERROR; 3949c1a60c51Sdan } 3950c1a60c51Sdan 3951c1a60c51Sdan init_all(slave); 3952c1a60c51Sdan return TCL_OK; 3953c1a60c51Sdan } 3954c431fd55Sdan 3955c431fd55Sdan /* 3956c431fd55Sdan ** Tclcmd: db_use_legacy_prepare DB BOOLEAN 3957c431fd55Sdan ** 3958c431fd55Sdan ** The first argument to this command must be a database command created by 3959c431fd55Sdan ** [sqlite3]. If the second argument is true, then the handle is configured 3960c431fd55Sdan ** to use the sqlite3_prepare_v2() function to prepare statements. If it 3961c431fd55Sdan ** is false, sqlite3_prepare(). 3962c431fd55Sdan */ 3963c431fd55Sdan static int db_use_legacy_prepare_cmd( 3964c431fd55Sdan ClientData cd, 3965c431fd55Sdan Tcl_Interp *interp, 3966c431fd55Sdan int objc, 3967c431fd55Sdan Tcl_Obj *CONST objv[] 3968c431fd55Sdan ){ 3969c431fd55Sdan Tcl_CmdInfo cmdInfo; 3970c431fd55Sdan SqliteDb *pDb; 3971c431fd55Sdan int bPrepare; 3972c431fd55Sdan 3973c431fd55Sdan if( objc!=3 ){ 3974c431fd55Sdan Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN"); 3975c431fd55Sdan return TCL_ERROR; 3976c431fd55Sdan } 3977c431fd55Sdan 3978c431fd55Sdan if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ 3979c431fd55Sdan Tcl_AppendResult(interp, "no such db: ", Tcl_GetString(objv[1]), (char*)0); 3980c431fd55Sdan return TCL_ERROR; 3981c431fd55Sdan } 3982c431fd55Sdan pDb = (SqliteDb*)cmdInfo.objClientData; 3983c431fd55Sdan if( Tcl_GetBooleanFromObj(interp, objv[2], &bPrepare) ){ 3984c431fd55Sdan return TCL_ERROR; 3985c431fd55Sdan } 3986c431fd55Sdan 3987c431fd55Sdan pDb->bLegacyPrepare = bPrepare; 3988c431fd55Sdan 3989c431fd55Sdan Tcl_ResetResult(interp); 3990c431fd55Sdan return TCL_OK; 3991c431fd55Sdan } 399204489b6dSdan 399304489b6dSdan /* 399404489b6dSdan ** Tclcmd: db_last_stmt_ptr DB 399504489b6dSdan ** 399604489b6dSdan ** If the statement cache associated with database DB is not empty, 399704489b6dSdan ** return the text representation of the most recently used statement 399804489b6dSdan ** handle. 399904489b6dSdan */ 400004489b6dSdan static int db_last_stmt_ptr( 400104489b6dSdan ClientData cd, 400204489b6dSdan Tcl_Interp *interp, 400304489b6dSdan int objc, 400404489b6dSdan Tcl_Obj *CONST objv[] 400504489b6dSdan ){ 400604489b6dSdan extern int sqlite3TestMakePointerStr(Tcl_Interp*, char*, void*); 400704489b6dSdan Tcl_CmdInfo cmdInfo; 400804489b6dSdan SqliteDb *pDb; 400904489b6dSdan sqlite3_stmt *pStmt = 0; 401004489b6dSdan char zBuf[100]; 401104489b6dSdan 401204489b6dSdan if( objc!=2 ){ 401304489b6dSdan Tcl_WrongNumArgs(interp, 1, objv, "DB"); 401404489b6dSdan return TCL_ERROR; 401504489b6dSdan } 401604489b6dSdan 401704489b6dSdan if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ 401804489b6dSdan Tcl_AppendResult(interp, "no such db: ", Tcl_GetString(objv[1]), (char*)0); 401904489b6dSdan return TCL_ERROR; 402004489b6dSdan } 402104489b6dSdan pDb = (SqliteDb*)cmdInfo.objClientData; 402204489b6dSdan 402304489b6dSdan if( pDb->stmtList ) pStmt = pDb->stmtList->pStmt; 402404489b6dSdan if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ){ 402504489b6dSdan return TCL_ERROR; 402604489b6dSdan } 402704489b6dSdan Tcl_SetResult(interp, zBuf, TCL_VOLATILE); 402804489b6dSdan 402904489b6dSdan return TCL_OK; 403004489b6dSdan } 40311a4a680aSdrh #endif /* SQLITE_TEST */ 40321a4a680aSdrh 4033c1a60c51Sdan /* 4034c1a60c51Sdan ** Configure the interpreter passed as the first argument to have access 4035c1a60c51Sdan ** to the commands and linked variables that make up: 4036c1a60c51Sdan ** 4037c1a60c51Sdan ** * the [sqlite3] extension itself, 4038c1a60c51Sdan ** 4039c1a60c51Sdan ** * If SQLITE_TCLMD5 or SQLITE_TEST is defined, the Md5 commands, and 4040c1a60c51Sdan ** 4041c1a60c51Sdan ** * If SQLITE_TEST is set, the various test interfaces used by the Tcl 4042c1a60c51Sdan ** test suite. 4043c1a60c51Sdan */ 4044c1a60c51Sdan static void init_all(Tcl_Interp *interp){ 404538f8271fSdrh Sqlite3_Init(interp); 4046c1a60c51Sdan 404757a0227fSdrh #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) 404857a0227fSdrh Md5_Init(interp); 404957a0227fSdrh #endif 4050c1a60c51Sdan 4051d9b0257aSdrh #ifdef SQLITE_TEST 4052d1bf3512Sdrh { 40532f999a67Sdrh extern int Sqliteconfig_Init(Tcl_Interp*); 4054d1bf3512Sdrh extern int Sqlitetest1_Init(Tcl_Interp*); 40555c4d9703Sdrh extern int Sqlitetest2_Init(Tcl_Interp*); 40565c4d9703Sdrh extern int Sqlitetest3_Init(Tcl_Interp*); 4057a6064dcfSdrh extern int Sqlitetest4_Init(Tcl_Interp*); 4058998b56c3Sdanielk1977 extern int Sqlitetest5_Init(Tcl_Interp*); 40599c06c953Sdrh extern int Sqlitetest6_Init(Tcl_Interp*); 406029c636bcSdrh extern int Sqlitetest7_Init(Tcl_Interp*); 4061b9bb7c18Sdrh extern int Sqlitetest8_Init(Tcl_Interp*); 4062a713f2c3Sdanielk1977 extern int Sqlitetest9_Init(Tcl_Interp*); 40632366940dSdrh extern int Sqlitetestasync_Init(Tcl_Interp*); 40641409be69Sdrh extern int Sqlitetest_autoext_Init(Tcl_Interp*); 4065b391b944Sdan extern int Sqlitetest_blob_Init(Tcl_Interp*); 40660a7a9155Sdan extern int Sqlitetest_demovfs_Init(Tcl_Interp *); 4067984bfaa4Sdrh extern int Sqlitetest_func_Init(Tcl_Interp*); 406815926590Sdrh extern int Sqlitetest_hexio_Init(Tcl_Interp*); 4069e1ab2193Sdan extern int Sqlitetest_init_Init(Tcl_Interp*); 40702f999a67Sdrh extern int Sqlitetest_malloc_Init(Tcl_Interp*); 40711a9ed0b2Sdanielk1977 extern int Sqlitetest_mutex_Init(Tcl_Interp*); 40722f999a67Sdrh extern int Sqlitetestschema_Init(Tcl_Interp*); 40732f999a67Sdrh extern int Sqlitetestsse_Init(Tcl_Interp*); 40742f999a67Sdrh extern int Sqlitetesttclvar_Init(Tcl_Interp*); 40759f5ff371Sdan extern int Sqlitetestfs_Init(Tcl_Interp*); 407644918fa0Sdanielk1977 extern int SqlitetestThread_Init(Tcl_Interp*); 4077a15db353Sdanielk1977 extern int SqlitetestOnefile_Init(); 40785d1f5aa6Sdanielk1977 extern int SqlitetestOsinst_Init(Tcl_Interp*); 40790410302eSdanielk1977 extern int Sqlitetestbackup_Init(Tcl_Interp*); 4080522efc62Sdrh extern int Sqlitetestintarray_Init(Tcl_Interp*); 4081c7991bdfSdan extern int Sqlitetestvfs_Init(Tcl_Interp *); 40829508daa9Sdan extern int Sqlitetestrtree_Init(Tcl_Interp*); 40838cf35eb4Sdan extern int Sqlitequota_Init(Tcl_Interp*); 40848a922f75Sshaneh extern int Sqlitemultiplex_Init(Tcl_Interp*); 4085e336b001Sdan extern int SqliteSuperlock_Init(Tcl_Interp*); 4086213ca0a8Sdan extern int SqlitetestSyscall_Init(Tcl_Interp*); 40879b1c62d4Sdrh #if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK) 40884fccf43aSdan extern int TestSession_Init(Tcl_Interp*); 40894fccf43aSdan #endif 409089a89560Sdan extern int Fts5tcl_Init(Tcl_Interp *); 4091cfb8f8d6Sdrh extern int SqliteRbu_Init(Tcl_Interp*); 40928e4251b6Sdan extern int Sqlitetesttcl_Init(Tcl_Interp*); 40936764a700Sdan #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) 409499ebad90Sdan extern int Sqlitetestfts3_Init(Tcl_Interp *interp); 409599ebad90Sdan #endif 409699ebad90Sdan 4097b29010cdSdan #ifdef SQLITE_ENABLE_ZIPVFS 4098b29010cdSdan extern int Zipvfs_Init(Tcl_Interp*); 4099b29010cdSdan Zipvfs_Init(interp); 4100b29010cdSdan #endif 4101b29010cdSdan 41022f999a67Sdrh Sqliteconfig_Init(interp); 41036490bebdSdanielk1977 Sqlitetest1_Init(interp); 41045c4d9703Sdrh Sqlitetest2_Init(interp); 4105de647130Sdrh Sqlitetest3_Init(interp); 4106fc57d7bfSdanielk1977 Sqlitetest4_Init(interp); 4107998b56c3Sdanielk1977 Sqlitetest5_Init(interp); 41089c06c953Sdrh Sqlitetest6_Init(interp); 410929c636bcSdrh Sqlitetest7_Init(interp); 4110b9bb7c18Sdrh Sqlitetest8_Init(interp); 4111a713f2c3Sdanielk1977 Sqlitetest9_Init(interp); 41122366940dSdrh Sqlitetestasync_Init(interp); 41131409be69Sdrh Sqlitetest_autoext_Init(interp); 4114b391b944Sdan Sqlitetest_blob_Init(interp); 41150a7a9155Sdan Sqlitetest_demovfs_Init(interp); 4116984bfaa4Sdrh Sqlitetest_func_Init(interp); 411715926590Sdrh Sqlitetest_hexio_Init(interp); 4118e1ab2193Sdan Sqlitetest_init_Init(interp); 41192f999a67Sdrh Sqlitetest_malloc_Init(interp); 41201a9ed0b2Sdanielk1977 Sqlitetest_mutex_Init(interp); 41212f999a67Sdrh Sqlitetestschema_Init(interp); 41222f999a67Sdrh Sqlitetesttclvar_Init(interp); 41239f5ff371Sdan Sqlitetestfs_Init(interp); 412444918fa0Sdanielk1977 SqlitetestThread_Init(interp); 4125a15db353Sdanielk1977 SqlitetestOnefile_Init(interp); 41265d1f5aa6Sdanielk1977 SqlitetestOsinst_Init(interp); 41270410302eSdanielk1977 Sqlitetestbackup_Init(interp); 4128522efc62Sdrh Sqlitetestintarray_Init(interp); 4129c7991bdfSdan Sqlitetestvfs_Init(interp); 41309508daa9Sdan Sqlitetestrtree_Init(interp); 41318cf35eb4Sdan Sqlitequota_Init(interp); 41328a922f75Sshaneh Sqlitemultiplex_Init(interp); 4133e336b001Sdan SqliteSuperlock_Init(interp); 4134213ca0a8Sdan SqlitetestSyscall_Init(interp); 41359b1c62d4Sdrh #if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK) 41364fccf43aSdan TestSession_Init(interp); 41374fccf43aSdan #endif 413889a89560Sdan Fts5tcl_Init(interp); 4139cfb8f8d6Sdrh SqliteRbu_Init(interp); 41408e4251b6Sdan Sqlitetesttcl_Init(interp); 4141a15db353Sdanielk1977 41426764a700Sdan #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) 414399ebad90Sdan Sqlitetestfts3_Init(interp); 414499ebad90Sdan #endif 41452e66f0b9Sdrh 4146c431fd55Sdan Tcl_CreateObjCommand( 4147c431fd55Sdan interp, "load_testfixture_extensions", init_all_cmd, 0, 0 4148c431fd55Sdan ); 4149c431fd55Sdan Tcl_CreateObjCommand( 4150c431fd55Sdan interp, "db_use_legacy_prepare", db_use_legacy_prepare_cmd, 0, 0 4151c431fd55Sdan ); 415204489b6dSdan Tcl_CreateObjCommand( 415304489b6dSdan interp, "db_last_stmt_ptr", db_last_stmt_ptr, 0, 0 415404489b6dSdan ); 4155c1a60c51Sdan 41563e27c026Sdrh #ifdef SQLITE_SSE 4157348784efSdrh Sqlitetestsse_Init(interp); 4158348784efSdrh #endif 4159348784efSdrh } 416061212b69Sdrh #endif 4161c1a60c51Sdan } 4162c1a60c51Sdan 4163db6bafaeSdrh /* Needed for the setrlimit() system call on unix */ 4164db6bafaeSdrh #if defined(unix) 4165db6bafaeSdrh #include <sys/resource.h> 4166db6bafaeSdrh #endif 4167db6bafaeSdrh 4168c1a60c51Sdan #define TCLSH_MAIN main /* Needed to fake out mktclapp */ 4169c1a60c51Sdan int TCLSH_MAIN(int argc, char **argv){ 4170c1a60c51Sdan Tcl_Interp *interp; 4171c1a60c51Sdan 41721f28e070Smistachkin #if !defined(_WIN32_WCE) 41731f28e070Smistachkin if( getenv("BREAK") ){ 41741f28e070Smistachkin fprintf(stderr, 41751f28e070Smistachkin "attach debugger to process %d and press any key to continue.\n", 41761f28e070Smistachkin GETPID()); 41771f28e070Smistachkin fgetc(stdin); 41781f28e070Smistachkin } 41791f28e070Smistachkin #endif 41801f28e070Smistachkin 4181db6bafaeSdrh /* Since the primary use case for this binary is testing of SQLite, 4182db6bafaeSdrh ** be sure to generate core files if we crash */ 4183db6bafaeSdrh #if defined(SQLITE_TEST) && defined(unix) 4184db6bafaeSdrh { struct rlimit x; 4185db6bafaeSdrh getrlimit(RLIMIT_CORE, &x); 4186db6bafaeSdrh x.rlim_cur = x.rlim_max; 4187db6bafaeSdrh setrlimit(RLIMIT_CORE, &x); 4188db6bafaeSdrh } 4189db6bafaeSdrh #endif /* SQLITE_TEST && unix */ 4190db6bafaeSdrh 4191db6bafaeSdrh 4192c1a60c51Sdan /* Call sqlite3_shutdown() once before doing anything else. This is to 4193c1a60c51Sdan ** test that sqlite3_shutdown() can be safely called by a process before 4194c1a60c51Sdan ** sqlite3_initialize() is. */ 4195c1a60c51Sdan sqlite3_shutdown(); 4196c1a60c51Sdan 41970ae479dfSdan Tcl_FindExecutable(argv[0]); 41982953ba9eSmistachkin Tcl_SetSystemEncoding(NULL, "utf-8"); 41990ae479dfSdan interp = Tcl_CreateInterp(); 42000ae479dfSdan 42013a0f13ffSdrh #if TCLSH==2 42023a0f13ffSdrh sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); 42033a0f13ffSdrh #endif 4204c1a60c51Sdan 4205c1a60c51Sdan init_all(interp); 4206c7285978Sdrh if( argc>=2 ){ 4207348784efSdrh int i; 4208ad42c3a3Sshess char zArgc[32]; 4209ad42c3a3Sshess sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH)); 4210ad42c3a3Sshess Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY); 4211348784efSdrh Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); 4212348784efSdrh Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); 4213348784efSdrh for(i=3-TCLSH; i<argc; i++){ 4214348784efSdrh Tcl_SetVar(interp, "argv", argv[i], 4215348784efSdrh TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); 4216348784efSdrh } 42173a0f13ffSdrh if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ 42180de8c112Sdrh const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); 4219a81c64a2Sdrh if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp); 4220c61053b7Sdrh fprintf(stderr,"%s: %s\n", *argv, zInfo); 4221348784efSdrh return 1; 4222348784efSdrh } 42233e27c026Sdrh } 42243a0f13ffSdrh if( TCLSH==2 || argc<=1 ){ 42250ae479dfSdan Tcl_GlobalEval(interp, tclsh_main_loop()); 4226348784efSdrh } 4227348784efSdrh return 0; 4228348784efSdrh } 4229348784efSdrh #endif /* TCLSH */ 4230