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 3652b1dbb5Smistachkin #if defined(INCLUDE_SQLITE_TCL_H) 3752b1dbb5Smistachkin # include "sqlite_tcl.h" 3852b1dbb5Smistachkin #else 39bd08af48Sdrh # include "tcl.h" 407617e4a8Smistachkin # ifndef SQLITE_TCLAPI 417617e4a8Smistachkin # define SQLITE_TCLAPI 427617e4a8Smistachkin # endif 4352b1dbb5Smistachkin #endif 44b4e9af9fSdanielk1977 #include <errno.h> 456d31316cSdrh 46bd08af48Sdrh /* 47bd08af48Sdrh ** Some additional include files are needed if this file is not 48bd08af48Sdrh ** appended to the amalgamation. 49bd08af48Sdrh */ 50bd08af48Sdrh #ifndef SQLITE_AMALGAMATION 5165e8c82eSdrh # include "sqlite3.h" 5275897234Sdrh # include <stdlib.h> 5375897234Sdrh # include <string.h> 54ce927065Sdrh # include <assert.h> 5565e8c82eSdrh typedef unsigned char u8; 56bd08af48Sdrh #endif 57eb206381Sdrh #include <ctype.h> 5875897234Sdrh 591f28e070Smistachkin /* Used to get the current process ID */ 601f28e070Smistachkin #if !defined(_WIN32) 611f28e070Smistachkin # include <unistd.h> 621f28e070Smistachkin # define GETPID getpid 631f28e070Smistachkin #elif !defined(_WIN32_WCE) 641f28e070Smistachkin # ifndef SQLITE_AMALGAMATION 651f28e070Smistachkin # define WIN32_LEAN_AND_MEAN 661f28e070Smistachkin # include <windows.h> 671f28e070Smistachkin # endif 681f28e070Smistachkin # define GETPID (int)GetCurrentProcessId 691f28e070Smistachkin #endif 701f28e070Smistachkin 71ad6e1370Sdrh /* 72ad6e1370Sdrh * Windows needs to know which symbols to export. Unix does not. 73ad6e1370Sdrh * BUILD_sqlite should be undefined for Unix. 74ad6e1370Sdrh */ 75ad6e1370Sdrh #ifdef BUILD_sqlite 76ad6e1370Sdrh #undef TCL_STORAGE_CLASS 77ad6e1370Sdrh #define TCL_STORAGE_CLASS DLLEXPORT 78ad6e1370Sdrh #endif /* BUILD_sqlite */ 7929bc4615Sdrh 80a21c6b6fSdanielk1977 #define NUM_PREPARED_STMTS 10 81fb7e7651Sdrh #define MAX_PREPARED_STMTS 100 82fb7e7651Sdrh 83c45e6716Sdrh /* Forward declaration */ 84c45e6716Sdrh typedef struct SqliteDb SqliteDb; 8598808babSdrh 8698808babSdrh /* 87cabb0819Sdrh ** New SQL functions can be created as TCL scripts. Each such function 88cabb0819Sdrh ** is described by an instance of the following structure. 89cabb0819Sdrh */ 90cabb0819Sdrh typedef struct SqlFunc SqlFunc; 91cabb0819Sdrh struct SqlFunc { 92cabb0819Sdrh Tcl_Interp *interp; /* The TCL interpret to execute the function */ 93d1e4733dSdrh Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */ 94c45e6716Sdrh SqliteDb *pDb; /* Database connection that owns this function */ 95d1e4733dSdrh int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */ 96d1e4733dSdrh char *zName; /* Name of this function */ 97cabb0819Sdrh SqlFunc *pNext; /* Next function on the list of them all */ 98cabb0819Sdrh }; 99cabb0819Sdrh 100cabb0819Sdrh /* 1010202b29eSdanielk1977 ** New collation sequences function can be created as TCL scripts. Each such 1020202b29eSdanielk1977 ** function is described by an instance of the following structure. 1030202b29eSdanielk1977 */ 1040202b29eSdanielk1977 typedef struct SqlCollate SqlCollate; 1050202b29eSdanielk1977 struct SqlCollate { 1060202b29eSdanielk1977 Tcl_Interp *interp; /* The TCL interpret to execute the function */ 1070202b29eSdanielk1977 char *zScript; /* The script to be run */ 1080202b29eSdanielk1977 SqlCollate *pNext; /* Next function on the list of them all */ 1090202b29eSdanielk1977 }; 1100202b29eSdanielk1977 1110202b29eSdanielk1977 /* 112fb7e7651Sdrh ** Prepared statements are cached for faster execution. Each prepared 113fb7e7651Sdrh ** statement is described by an instance of the following structure. 114fb7e7651Sdrh */ 115fb7e7651Sdrh typedef struct SqlPreparedStmt SqlPreparedStmt; 116fb7e7651Sdrh struct SqlPreparedStmt { 117fb7e7651Sdrh SqlPreparedStmt *pNext; /* Next in linked list */ 118fb7e7651Sdrh SqlPreparedStmt *pPrev; /* Previous on the list */ 119fb7e7651Sdrh sqlite3_stmt *pStmt; /* The prepared statement */ 120fb7e7651Sdrh int nSql; /* chars in zSql[] */ 121d0e2a854Sdanielk1977 const char *zSql; /* Text of the SQL statement */ 1224a4c11aaSdan int nParm; /* Size of apParm array */ 1234a4c11aaSdan Tcl_Obj **apParm; /* Array of referenced object pointers */ 124fb7e7651Sdrh }; 125fb7e7651Sdrh 126d0441796Sdanielk1977 typedef struct IncrblobChannel IncrblobChannel; 127d0441796Sdanielk1977 128fb7e7651Sdrh /* 129bec3f402Sdrh ** There is one instance of this structure for each SQLite database 130bec3f402Sdrh ** that has been opened by the SQLite TCL interface. 131c431fd55Sdan ** 132c431fd55Sdan ** If this module is built with SQLITE_TEST defined (to create the SQLite 133c431fd55Sdan ** testfixture executable), then it may be configured to use either 134c431fd55Sdan ** sqlite3_prepare_v2() or sqlite3_prepare() to prepare SQL statements. 135c431fd55Sdan ** If SqliteDb.bLegacyPrepare is true, sqlite3_prepare() is used. 136bec3f402Sdrh */ 137bec3f402Sdrh struct SqliteDb { 138dddca286Sdrh sqlite3 *db; /* The "real" database structure. MUST BE FIRST */ 139bec3f402Sdrh Tcl_Interp *interp; /* The interpreter used for this database */ 1406d31316cSdrh char *zBusy; /* The busy callback routine */ 141aa940eacSdrh char *zCommit; /* The commit hook callback routine */ 142b5a20d3cSdrh char *zTrace; /* The trace callback routine */ 143b56660f5Smistachkin char *zTraceV2; /* The trace_v2 callback routine */ 14419e2d37fSdrh char *zProfile; /* The profile callback routine */ 145348bb5d6Sdanielk1977 char *zProgress; /* The progress callback routine */ 146e22a334bSdrh char *zAuth; /* The authorization callback routine */ 1471f1549f8Sdrh int disableAuth; /* Disable the authorizer if it exists */ 14855c45f2eSdanielk1977 char *zNull; /* Text to substitute for an SQL NULL value */ 149cabb0819Sdrh SqlFunc *pFunc; /* List of SQL functions */ 15094eb6a14Sdanielk1977 Tcl_Obj *pUpdateHook; /* Update hook script (if any) */ 15146c47d46Sdan Tcl_Obj *pPreUpdateHook; /* Pre-update hook script (if any) */ 15271fd80bfSdanielk1977 Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */ 1535def0843Sdrh Tcl_Obj *pWalHook; /* WAL hook script (if any) */ 154404ca075Sdanielk1977 Tcl_Obj *pUnlockNotify; /* Unlock notify script (if any) */ 1550202b29eSdanielk1977 SqlCollate *pCollate; /* List of SQL collation functions */ 1566f8a503dSdanielk1977 int rc; /* Return code of most recent sqlite3_exec() */ 1577cedc8d4Sdanielk1977 Tcl_Obj *pCollateNeeded; /* Collation needed script */ 158fb7e7651Sdrh SqlPreparedStmt *stmtList; /* List of prepared statements*/ 159fb7e7651Sdrh SqlPreparedStmt *stmtLast; /* Last statement in the list */ 160fb7e7651Sdrh int maxStmt; /* The next maximum number of stmtList */ 161fb7e7651Sdrh int nStmt; /* Number of statements in stmtList */ 162d0441796Sdanielk1977 IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */ 1633c379b01Sdrh int nStep, nSort, nIndex; /* Statistics for most recent operation */ 164cd38d520Sdanielk1977 int nTransaction; /* Number of nested [transaction] methods */ 165147ef394Sdrh int openFlags; /* Flags used to open. (SQLITE_OPEN_URI) */ 166c431fd55Sdan #ifdef SQLITE_TEST 167c431fd55Sdan int bLegacyPrepare; /* True to use sqlite3_prepare() */ 168c431fd55Sdan #endif 16998808babSdrh }; 170297ecf14Sdrh 171b4e9af9fSdanielk1977 struct IncrblobChannel { 172d0441796Sdanielk1977 sqlite3_blob *pBlob; /* sqlite3 blob handle */ 173dcbb5d3fSdanielk1977 SqliteDb *pDb; /* Associated database connection */ 174b4e9af9fSdanielk1977 int iSeek; /* Current seek offset */ 175d0441796Sdanielk1977 Tcl_Channel channel; /* Channel identifier */ 176d0441796Sdanielk1977 IncrblobChannel *pNext; /* Linked list of all open incrblob channels */ 177d0441796Sdanielk1977 IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */ 178b4e9af9fSdanielk1977 }; 179b4e9af9fSdanielk1977 180ea678832Sdrh /* 181ea678832Sdrh ** Compute a string length that is limited to what can be stored in 182ea678832Sdrh ** lower 30 bits of a 32-bit signed integer. 183ea678832Sdrh */ 1844f21c4afSdrh static int strlen30(const char *z){ 185ea678832Sdrh const char *z2 = z; 186ea678832Sdrh while( *z2 ){ z2++; } 187ea678832Sdrh return 0x3fffffff & (int)(z2 - z); 188ea678832Sdrh } 189ea678832Sdrh 190ea678832Sdrh 19132a0d8bbSdanielk1977 #ifndef SQLITE_OMIT_INCRBLOB 192b4e9af9fSdanielk1977 /* 193d0441796Sdanielk1977 ** Close all incrblob channels opened using database connection pDb. 194d0441796Sdanielk1977 ** This is called when shutting down the database connection. 195d0441796Sdanielk1977 */ 196d0441796Sdanielk1977 static void closeIncrblobChannels(SqliteDb *pDb){ 197d0441796Sdanielk1977 IncrblobChannel *p; 198d0441796Sdanielk1977 IncrblobChannel *pNext; 199d0441796Sdanielk1977 200d0441796Sdanielk1977 for(p=pDb->pIncrblob; p; p=pNext){ 201d0441796Sdanielk1977 pNext = p->pNext; 202d0441796Sdanielk1977 203d0441796Sdanielk1977 /* Note: Calling unregister here call Tcl_Close on the incrblob channel, 204d0441796Sdanielk1977 ** which deletes the IncrblobChannel structure at *p. So do not 205d0441796Sdanielk1977 ** call Tcl_Free() here. 206d0441796Sdanielk1977 */ 207d0441796Sdanielk1977 Tcl_UnregisterChannel(pDb->interp, p->channel); 208d0441796Sdanielk1977 } 209d0441796Sdanielk1977 } 210d0441796Sdanielk1977 211d0441796Sdanielk1977 /* 212b4e9af9fSdanielk1977 ** Close an incremental blob channel. 213b4e9af9fSdanielk1977 */ 2147617e4a8Smistachkin static int SQLITE_TCLAPI incrblobClose( 2157617e4a8Smistachkin ClientData instanceData, 2167617e4a8Smistachkin Tcl_Interp *interp 2177617e4a8Smistachkin ){ 218b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 21992d4d7a9Sdanielk1977 int rc = sqlite3_blob_close(p->pBlob); 22092d4d7a9Sdanielk1977 sqlite3 *db = p->pDb->db; 221d0441796Sdanielk1977 222d0441796Sdanielk1977 /* Remove the channel from the SqliteDb.pIncrblob list. */ 223d0441796Sdanielk1977 if( p->pNext ){ 224d0441796Sdanielk1977 p->pNext->pPrev = p->pPrev; 225d0441796Sdanielk1977 } 226d0441796Sdanielk1977 if( p->pPrev ){ 227d0441796Sdanielk1977 p->pPrev->pNext = p->pNext; 228d0441796Sdanielk1977 } 229d0441796Sdanielk1977 if( p->pDb->pIncrblob==p ){ 230d0441796Sdanielk1977 p->pDb->pIncrblob = p->pNext; 231d0441796Sdanielk1977 } 232d0441796Sdanielk1977 23392d4d7a9Sdanielk1977 /* Free the IncrblobChannel structure */ 234b4e9af9fSdanielk1977 Tcl_Free((char *)p); 23592d4d7a9Sdanielk1977 23692d4d7a9Sdanielk1977 if( rc!=SQLITE_OK ){ 23792d4d7a9Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); 23892d4d7a9Sdanielk1977 return TCL_ERROR; 23992d4d7a9Sdanielk1977 } 240b4e9af9fSdanielk1977 return TCL_OK; 241b4e9af9fSdanielk1977 } 242b4e9af9fSdanielk1977 243b4e9af9fSdanielk1977 /* 244b4e9af9fSdanielk1977 ** Read data from an incremental blob channel. 245b4e9af9fSdanielk1977 */ 2467617e4a8Smistachkin static int SQLITE_TCLAPI incrblobInput( 247b4e9af9fSdanielk1977 ClientData instanceData, 248b4e9af9fSdanielk1977 char *buf, 249b4e9af9fSdanielk1977 int bufSize, 250b4e9af9fSdanielk1977 int *errorCodePtr 251b4e9af9fSdanielk1977 ){ 252b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 253b4e9af9fSdanielk1977 int nRead = bufSize; /* Number of bytes to read */ 254b4e9af9fSdanielk1977 int nBlob; /* Total size of the blob */ 255b4e9af9fSdanielk1977 int rc; /* sqlite error code */ 256b4e9af9fSdanielk1977 257b4e9af9fSdanielk1977 nBlob = sqlite3_blob_bytes(p->pBlob); 258b4e9af9fSdanielk1977 if( (p->iSeek+nRead)>nBlob ){ 259b4e9af9fSdanielk1977 nRead = nBlob-p->iSeek; 260b4e9af9fSdanielk1977 } 261b4e9af9fSdanielk1977 if( nRead<=0 ){ 262b4e9af9fSdanielk1977 return 0; 263b4e9af9fSdanielk1977 } 264b4e9af9fSdanielk1977 265b4e9af9fSdanielk1977 rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek); 266b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 267b4e9af9fSdanielk1977 *errorCodePtr = rc; 268b4e9af9fSdanielk1977 return -1; 269b4e9af9fSdanielk1977 } 270b4e9af9fSdanielk1977 271b4e9af9fSdanielk1977 p->iSeek += nRead; 272b4e9af9fSdanielk1977 return nRead; 273b4e9af9fSdanielk1977 } 274b4e9af9fSdanielk1977 275d0441796Sdanielk1977 /* 276d0441796Sdanielk1977 ** Write data to an incremental blob channel. 277d0441796Sdanielk1977 */ 2787617e4a8Smistachkin static int SQLITE_TCLAPI incrblobOutput( 279b4e9af9fSdanielk1977 ClientData instanceData, 280b4e9af9fSdanielk1977 CONST char *buf, 281b4e9af9fSdanielk1977 int toWrite, 282b4e9af9fSdanielk1977 int *errorCodePtr 283b4e9af9fSdanielk1977 ){ 284b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 285b4e9af9fSdanielk1977 int nWrite = toWrite; /* Number of bytes to write */ 286b4e9af9fSdanielk1977 int nBlob; /* Total size of the blob */ 287b4e9af9fSdanielk1977 int rc; /* sqlite error code */ 288b4e9af9fSdanielk1977 289b4e9af9fSdanielk1977 nBlob = sqlite3_blob_bytes(p->pBlob); 290b4e9af9fSdanielk1977 if( (p->iSeek+nWrite)>nBlob ){ 291b4e9af9fSdanielk1977 *errorCodePtr = EINVAL; 292b4e9af9fSdanielk1977 return -1; 293b4e9af9fSdanielk1977 } 294b4e9af9fSdanielk1977 if( nWrite<=0 ){ 295b4e9af9fSdanielk1977 return 0; 296b4e9af9fSdanielk1977 } 297b4e9af9fSdanielk1977 298b4e9af9fSdanielk1977 rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek); 299b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 300b4e9af9fSdanielk1977 *errorCodePtr = EIO; 301b4e9af9fSdanielk1977 return -1; 302b4e9af9fSdanielk1977 } 303b4e9af9fSdanielk1977 304b4e9af9fSdanielk1977 p->iSeek += nWrite; 305b4e9af9fSdanielk1977 return nWrite; 306b4e9af9fSdanielk1977 } 307b4e9af9fSdanielk1977 308b4e9af9fSdanielk1977 /* 309b4e9af9fSdanielk1977 ** Seek an incremental blob channel. 310b4e9af9fSdanielk1977 */ 3117617e4a8Smistachkin static int SQLITE_TCLAPI incrblobSeek( 312b4e9af9fSdanielk1977 ClientData instanceData, 313b4e9af9fSdanielk1977 long offset, 314b4e9af9fSdanielk1977 int seekMode, 315b4e9af9fSdanielk1977 int *errorCodePtr 316b4e9af9fSdanielk1977 ){ 317b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 318b4e9af9fSdanielk1977 319b4e9af9fSdanielk1977 switch( seekMode ){ 320b4e9af9fSdanielk1977 case SEEK_SET: 321b4e9af9fSdanielk1977 p->iSeek = offset; 322b4e9af9fSdanielk1977 break; 323b4e9af9fSdanielk1977 case SEEK_CUR: 324b4e9af9fSdanielk1977 p->iSeek += offset; 325b4e9af9fSdanielk1977 break; 326b4e9af9fSdanielk1977 case SEEK_END: 327b4e9af9fSdanielk1977 p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset; 328b4e9af9fSdanielk1977 break; 329b4e9af9fSdanielk1977 330b4e9af9fSdanielk1977 default: assert(!"Bad seekMode"); 331b4e9af9fSdanielk1977 } 332b4e9af9fSdanielk1977 333b4e9af9fSdanielk1977 return p->iSeek; 334b4e9af9fSdanielk1977 } 335b4e9af9fSdanielk1977 336b4e9af9fSdanielk1977 3377617e4a8Smistachkin static void SQLITE_TCLAPI incrblobWatch( 3387617e4a8Smistachkin ClientData instanceData, 3397617e4a8Smistachkin int mode 3407617e4a8Smistachkin ){ 341b4e9af9fSdanielk1977 /* NO-OP */ 342b4e9af9fSdanielk1977 } 3437617e4a8Smistachkin static int SQLITE_TCLAPI incrblobHandle( 3447617e4a8Smistachkin ClientData instanceData, 3457617e4a8Smistachkin int dir, 3467617e4a8Smistachkin ClientData *hPtr 3477617e4a8Smistachkin ){ 348b4e9af9fSdanielk1977 return TCL_ERROR; 349b4e9af9fSdanielk1977 } 350b4e9af9fSdanielk1977 351b4e9af9fSdanielk1977 static Tcl_ChannelType IncrblobChannelType = { 352b4e9af9fSdanielk1977 "incrblob", /* typeName */ 353b4e9af9fSdanielk1977 TCL_CHANNEL_VERSION_2, /* version */ 354b4e9af9fSdanielk1977 incrblobClose, /* closeProc */ 355b4e9af9fSdanielk1977 incrblobInput, /* inputProc */ 356b4e9af9fSdanielk1977 incrblobOutput, /* outputProc */ 357b4e9af9fSdanielk1977 incrblobSeek, /* seekProc */ 358b4e9af9fSdanielk1977 0, /* setOptionProc */ 359b4e9af9fSdanielk1977 0, /* getOptionProc */ 360b4e9af9fSdanielk1977 incrblobWatch, /* watchProc (this is a no-op) */ 361b4e9af9fSdanielk1977 incrblobHandle, /* getHandleProc (always returns error) */ 362b4e9af9fSdanielk1977 0, /* close2Proc */ 363b4e9af9fSdanielk1977 0, /* blockModeProc */ 364b4e9af9fSdanielk1977 0, /* flushProc */ 365b4e9af9fSdanielk1977 0, /* handlerProc */ 366b4e9af9fSdanielk1977 0, /* wideSeekProc */ 367b4e9af9fSdanielk1977 }; 368b4e9af9fSdanielk1977 369b4e9af9fSdanielk1977 /* 370b4e9af9fSdanielk1977 ** Create a new incrblob channel. 371b4e9af9fSdanielk1977 */ 372b4e9af9fSdanielk1977 static int createIncrblobChannel( 373b4e9af9fSdanielk1977 Tcl_Interp *interp, 374b4e9af9fSdanielk1977 SqliteDb *pDb, 375b4e9af9fSdanielk1977 const char *zDb, 376b4e9af9fSdanielk1977 const char *zTable, 377b4e9af9fSdanielk1977 const char *zColumn, 3788cbadb02Sdanielk1977 sqlite_int64 iRow, 3798cbadb02Sdanielk1977 int isReadonly 380b4e9af9fSdanielk1977 ){ 381b4e9af9fSdanielk1977 IncrblobChannel *p; 3828cbadb02Sdanielk1977 sqlite3 *db = pDb->db; 383b4e9af9fSdanielk1977 sqlite3_blob *pBlob; 384b4e9af9fSdanielk1977 int rc; 3858cbadb02Sdanielk1977 int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE); 386b4e9af9fSdanielk1977 387b4e9af9fSdanielk1977 /* This variable is used to name the channels: "incrblob_[incr count]" */ 388b4e9af9fSdanielk1977 static int count = 0; 389b4e9af9fSdanielk1977 char zChannel[64]; 390b4e9af9fSdanielk1977 3918cbadb02Sdanielk1977 rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob); 392b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 393b4e9af9fSdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 394b4e9af9fSdanielk1977 return TCL_ERROR; 395b4e9af9fSdanielk1977 } 396b4e9af9fSdanielk1977 397b4e9af9fSdanielk1977 p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel)); 398b4e9af9fSdanielk1977 p->iSeek = 0; 399b4e9af9fSdanielk1977 p->pBlob = pBlob; 400b4e9af9fSdanielk1977 4015bb3eb9bSdrh sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count); 402d0441796Sdanielk1977 p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags); 403d0441796Sdanielk1977 Tcl_RegisterChannel(interp, p->channel); 404b4e9af9fSdanielk1977 405d0441796Sdanielk1977 /* Link the new channel into the SqliteDb.pIncrblob list. */ 406d0441796Sdanielk1977 p->pNext = pDb->pIncrblob; 407d0441796Sdanielk1977 p->pPrev = 0; 408d0441796Sdanielk1977 if( p->pNext ){ 409d0441796Sdanielk1977 p->pNext->pPrev = p; 410d0441796Sdanielk1977 } 411d0441796Sdanielk1977 pDb->pIncrblob = p; 412d0441796Sdanielk1977 p->pDb = pDb; 413d0441796Sdanielk1977 414d0441796Sdanielk1977 Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE); 415b4e9af9fSdanielk1977 return TCL_OK; 416b4e9af9fSdanielk1977 } 41732a0d8bbSdanielk1977 #else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */ 41832a0d8bbSdanielk1977 #define closeIncrblobChannels(pDb) 41932a0d8bbSdanielk1977 #endif 420b4e9af9fSdanielk1977 4216d31316cSdrh /* 422d1e4733dSdrh ** Look at the script prefix in pCmd. We will be executing this script 423d1e4733dSdrh ** after first appending one or more arguments. This routine analyzes 424d1e4733dSdrh ** the script to see if it is safe to use Tcl_EvalObjv() on the script 425d1e4733dSdrh ** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much 426d1e4733dSdrh ** faster. 427d1e4733dSdrh ** 428d1e4733dSdrh ** Scripts that are safe to use with Tcl_EvalObjv() consists of a 429d1e4733dSdrh ** command name followed by zero or more arguments with no [...] or $ 430d1e4733dSdrh ** or {...} or ; to be seen anywhere. Most callback scripts consist 431d1e4733dSdrh ** of just a single procedure name and they meet this requirement. 432d1e4733dSdrh */ 433d1e4733dSdrh static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){ 434d1e4733dSdrh /* We could try to do something with Tcl_Parse(). But we will instead 435d1e4733dSdrh ** just do a search for forbidden characters. If any of the forbidden 436d1e4733dSdrh ** characters appear in pCmd, we will report the string as unsafe. 437d1e4733dSdrh */ 438d1e4733dSdrh const char *z; 439d1e4733dSdrh int n; 440d1e4733dSdrh z = Tcl_GetStringFromObj(pCmd, &n); 441d1e4733dSdrh while( n-- > 0 ){ 442d1e4733dSdrh int c = *(z++); 443d1e4733dSdrh if( c=='$' || c=='[' || c==';' ) return 0; 444d1e4733dSdrh } 445d1e4733dSdrh return 1; 446d1e4733dSdrh } 447d1e4733dSdrh 448d1e4733dSdrh /* 449d1e4733dSdrh ** Find an SqlFunc structure with the given name. Or create a new 450d1e4733dSdrh ** one if an existing one cannot be found. Return a pointer to the 451d1e4733dSdrh ** structure. 452d1e4733dSdrh */ 453d1e4733dSdrh static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){ 454d1e4733dSdrh SqlFunc *p, *pNew; 4550425f189Sdrh int nName = strlen30(zName); 4560425f189Sdrh pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + nName + 1 ); 457d1e4733dSdrh pNew->zName = (char*)&pNew[1]; 4580425f189Sdrh memcpy(pNew->zName, zName, nName+1); 459d1e4733dSdrh for(p=pDb->pFunc; p; p=p->pNext){ 4600425f189Sdrh if( sqlite3_stricmp(p->zName, pNew->zName)==0 ){ 461d1e4733dSdrh Tcl_Free((char*)pNew); 462d1e4733dSdrh return p; 463d1e4733dSdrh } 464d1e4733dSdrh } 465d1e4733dSdrh pNew->interp = pDb->interp; 466c45e6716Sdrh pNew->pDb = pDb; 467d1e4733dSdrh pNew->pScript = 0; 468d1e4733dSdrh pNew->pNext = pDb->pFunc; 469d1e4733dSdrh pDb->pFunc = pNew; 470d1e4733dSdrh return pNew; 471d1e4733dSdrh } 472d1e4733dSdrh 473d1e4733dSdrh /* 474c431fd55Sdan ** Free a single SqlPreparedStmt object. 475c431fd55Sdan */ 476c431fd55Sdan static void dbFreeStmt(SqlPreparedStmt *pStmt){ 477c431fd55Sdan #ifdef SQLITE_TEST 478c431fd55Sdan if( sqlite3_sql(pStmt->pStmt)==0 ){ 479c431fd55Sdan Tcl_Free((char *)pStmt->zSql); 480c431fd55Sdan } 481c431fd55Sdan #endif 482c431fd55Sdan sqlite3_finalize(pStmt->pStmt); 483c431fd55Sdan Tcl_Free((char *)pStmt); 484c431fd55Sdan } 485c431fd55Sdan 486c431fd55Sdan /* 487fb7e7651Sdrh ** Finalize and free a list of prepared statements 488fb7e7651Sdrh */ 489fb7e7651Sdrh static void flushStmtCache(SqliteDb *pDb){ 490fb7e7651Sdrh SqlPreparedStmt *pPreStmt; 491c431fd55Sdan SqlPreparedStmt *pNext; 492fb7e7651Sdrh 493c431fd55Sdan for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pNext){ 494c431fd55Sdan pNext = pPreStmt->pNext; 495c431fd55Sdan dbFreeStmt(pPreStmt); 496fb7e7651Sdrh } 497fb7e7651Sdrh pDb->nStmt = 0; 498fb7e7651Sdrh pDb->stmtLast = 0; 499c431fd55Sdan pDb->stmtList = 0; 500fb7e7651Sdrh } 501fb7e7651Sdrh 502fb7e7651Sdrh /* 503895d7472Sdrh ** TCL calls this procedure when an sqlite3 database command is 504895d7472Sdrh ** deleted. 50575897234Sdrh */ 5067617e4a8Smistachkin static void SQLITE_TCLAPI DbDeleteCmd(void *db){ 507bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)db; 508fb7e7651Sdrh flushStmtCache(pDb); 509d0441796Sdanielk1977 closeIncrblobChannels(pDb); 5106f8a503dSdanielk1977 sqlite3_close(pDb->db); 511cabb0819Sdrh while( pDb->pFunc ){ 512cabb0819Sdrh SqlFunc *pFunc = pDb->pFunc; 513cabb0819Sdrh pDb->pFunc = pFunc->pNext; 514c45e6716Sdrh assert( pFunc->pDb==pDb ); 515d1e4733dSdrh Tcl_DecrRefCount(pFunc->pScript); 516cabb0819Sdrh Tcl_Free((char*)pFunc); 517cabb0819Sdrh } 5180202b29eSdanielk1977 while( pDb->pCollate ){ 5190202b29eSdanielk1977 SqlCollate *pCollate = pDb->pCollate; 5200202b29eSdanielk1977 pDb->pCollate = pCollate->pNext; 5210202b29eSdanielk1977 Tcl_Free((char*)pCollate); 5220202b29eSdanielk1977 } 523bec3f402Sdrh if( pDb->zBusy ){ 524bec3f402Sdrh Tcl_Free(pDb->zBusy); 525bec3f402Sdrh } 526b5a20d3cSdrh if( pDb->zTrace ){ 527b5a20d3cSdrh Tcl_Free(pDb->zTrace); 5280d1a643aSdrh } 529b56660f5Smistachkin if( pDb->zTraceV2 ){ 530b56660f5Smistachkin Tcl_Free(pDb->zTraceV2); 531b56660f5Smistachkin } 53219e2d37fSdrh if( pDb->zProfile ){ 53319e2d37fSdrh Tcl_Free(pDb->zProfile); 53419e2d37fSdrh } 535e22a334bSdrh if( pDb->zAuth ){ 536e22a334bSdrh Tcl_Free(pDb->zAuth); 537e22a334bSdrh } 53855c45f2eSdanielk1977 if( pDb->zNull ){ 53955c45f2eSdanielk1977 Tcl_Free(pDb->zNull); 54055c45f2eSdanielk1977 } 54194eb6a14Sdanielk1977 if( pDb->pUpdateHook ){ 54294eb6a14Sdanielk1977 Tcl_DecrRefCount(pDb->pUpdateHook); 54394eb6a14Sdanielk1977 } 54446c47d46Sdan if( pDb->pPreUpdateHook ){ 54546c47d46Sdan Tcl_DecrRefCount(pDb->pPreUpdateHook); 54646c47d46Sdan } 54771fd80bfSdanielk1977 if( pDb->pRollbackHook ){ 54871fd80bfSdanielk1977 Tcl_DecrRefCount(pDb->pRollbackHook); 54971fd80bfSdanielk1977 } 5505def0843Sdrh if( pDb->pWalHook ){ 5515def0843Sdrh Tcl_DecrRefCount(pDb->pWalHook); 5528d22a174Sdan } 55394eb6a14Sdanielk1977 if( pDb->pCollateNeeded ){ 55494eb6a14Sdanielk1977 Tcl_DecrRefCount(pDb->pCollateNeeded); 55594eb6a14Sdanielk1977 } 556bec3f402Sdrh Tcl_Free((char*)pDb); 557bec3f402Sdrh } 558bec3f402Sdrh 559bec3f402Sdrh /* 560bec3f402Sdrh ** This routine is called when a database file is locked while trying 561bec3f402Sdrh ** to execute SQL. 562bec3f402Sdrh */ 5632a764eb0Sdanielk1977 static int DbBusyHandler(void *cd, int nTries){ 564bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 565bec3f402Sdrh int rc; 566bec3f402Sdrh char zVal[30]; 567bec3f402Sdrh 5685bb3eb9bSdrh sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries); 569d1e4733dSdrh rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0); 570bec3f402Sdrh if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 571bec3f402Sdrh return 0; 572bec3f402Sdrh } 573bec3f402Sdrh return 1; 57475897234Sdrh } 57575897234Sdrh 57626e4a8b1Sdrh #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 57775897234Sdrh /* 578348bb5d6Sdanielk1977 ** This routine is invoked as the 'progress callback' for the database. 579348bb5d6Sdanielk1977 */ 580348bb5d6Sdanielk1977 static int DbProgressHandler(void *cd){ 581348bb5d6Sdanielk1977 SqliteDb *pDb = (SqliteDb*)cd; 582348bb5d6Sdanielk1977 int rc; 583348bb5d6Sdanielk1977 584348bb5d6Sdanielk1977 assert( pDb->zProgress ); 585348bb5d6Sdanielk1977 rc = Tcl_Eval(pDb->interp, pDb->zProgress); 586348bb5d6Sdanielk1977 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 587348bb5d6Sdanielk1977 return 1; 588348bb5d6Sdanielk1977 } 589348bb5d6Sdanielk1977 return 0; 590348bb5d6Sdanielk1977 } 59126e4a8b1Sdrh #endif 592348bb5d6Sdanielk1977 593d1167393Sdrh #ifndef SQLITE_OMIT_TRACE 594348bb5d6Sdanielk1977 /* 595b5a20d3cSdrh ** This routine is called by the SQLite trace handler whenever a new 596b5a20d3cSdrh ** block of SQL is executed. The TCL script in pDb->zTrace is executed. 5970d1a643aSdrh */ 598b5a20d3cSdrh static void DbTraceHandler(void *cd, const char *zSql){ 5990d1a643aSdrh SqliteDb *pDb = (SqliteDb*)cd; 600b5a20d3cSdrh Tcl_DString str; 6010d1a643aSdrh 602b5a20d3cSdrh Tcl_DStringInit(&str); 603b5a20d3cSdrh Tcl_DStringAppend(&str, pDb->zTrace, -1); 604b5a20d3cSdrh Tcl_DStringAppendElement(&str, zSql); 605b5a20d3cSdrh Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 606b5a20d3cSdrh Tcl_DStringFree(&str); 607b5a20d3cSdrh Tcl_ResetResult(pDb->interp); 6080d1a643aSdrh } 609d1167393Sdrh #endif 6100d1a643aSdrh 611d1167393Sdrh #ifndef SQLITE_OMIT_TRACE 6120d1a643aSdrh /* 613b56660f5Smistachkin ** This routine is called by the SQLite trace_v2 handler whenever a new 614b56660f5Smistachkin ** supported event is generated. Unsupported event types are ignored. 615b56660f5Smistachkin ** The TCL script in pDb->zTraceV2 is executed, with the arguments for 616b56660f5Smistachkin ** the event appended to it (as list elements). 617b56660f5Smistachkin */ 618b56660f5Smistachkin static int DbTraceV2Handler( 619b56660f5Smistachkin unsigned type, /* One of the SQLITE_TRACE_* event types. */ 620b56660f5Smistachkin void *cd, /* The original context data pointer. */ 621b56660f5Smistachkin void *pd, /* Primary event data, depends on event type. */ 622b56660f5Smistachkin void *xd /* Extra event data, depends on event type. */ 623b56660f5Smistachkin ){ 624b56660f5Smistachkin SqliteDb *pDb = (SqliteDb*)cd; 625b56660f5Smistachkin Tcl_Obj *pCmd; 626b56660f5Smistachkin 627b56660f5Smistachkin switch( type ){ 628b56660f5Smistachkin case SQLITE_TRACE_STMT: { 629b56660f5Smistachkin sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; 630b56660f5Smistachkin char *zSql = (char *)xd; 631b56660f5Smistachkin 632b56660f5Smistachkin pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 633b56660f5Smistachkin Tcl_IncrRefCount(pCmd); 634b56660f5Smistachkin Tcl_ListObjAppendElement(pDb->interp, pCmd, 635b56660f5Smistachkin Tcl_NewWideIntObj((Tcl_WideInt)pStmt)); 636b56660f5Smistachkin Tcl_ListObjAppendElement(pDb->interp, pCmd, 637b56660f5Smistachkin Tcl_NewStringObj(zSql, -1)); 638b56660f5Smistachkin Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 639b56660f5Smistachkin Tcl_DecrRefCount(pCmd); 640b56660f5Smistachkin Tcl_ResetResult(pDb->interp); 641b56660f5Smistachkin break; 642b56660f5Smistachkin } 643b56660f5Smistachkin case SQLITE_TRACE_PROFILE: { 644b56660f5Smistachkin sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; 645b56660f5Smistachkin sqlite3_int64 ns = (sqlite3_int64)xd; 646b56660f5Smistachkin 647b56660f5Smistachkin pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 648b56660f5Smistachkin Tcl_IncrRefCount(pCmd); 649b56660f5Smistachkin Tcl_ListObjAppendElement(pDb->interp, pCmd, 650b56660f5Smistachkin Tcl_NewWideIntObj((Tcl_WideInt)pStmt)); 651b56660f5Smistachkin Tcl_ListObjAppendElement(pDb->interp, pCmd, 652b56660f5Smistachkin Tcl_NewWideIntObj((Tcl_WideInt)ns)); 653b56660f5Smistachkin Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 654b56660f5Smistachkin Tcl_DecrRefCount(pCmd); 655b56660f5Smistachkin Tcl_ResetResult(pDb->interp); 656b56660f5Smistachkin break; 657b56660f5Smistachkin } 658b56660f5Smistachkin case SQLITE_TRACE_ROW: { 659b56660f5Smistachkin sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; 660b56660f5Smistachkin 661b56660f5Smistachkin pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 662b56660f5Smistachkin Tcl_IncrRefCount(pCmd); 663b56660f5Smistachkin Tcl_ListObjAppendElement(pDb->interp, pCmd, 664b56660f5Smistachkin Tcl_NewWideIntObj((Tcl_WideInt)pStmt)); 665b56660f5Smistachkin Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 666b56660f5Smistachkin Tcl_DecrRefCount(pCmd); 667b56660f5Smistachkin Tcl_ResetResult(pDb->interp); 668b56660f5Smistachkin break; 669b56660f5Smistachkin } 670b56660f5Smistachkin case SQLITE_TRACE_CLOSE: { 671b56660f5Smistachkin sqlite3 *db = (sqlite3 *)pd; 672b56660f5Smistachkin 673b56660f5Smistachkin pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); 674b56660f5Smistachkin Tcl_IncrRefCount(pCmd); 675b56660f5Smistachkin Tcl_ListObjAppendElement(pDb->interp, pCmd, 676b56660f5Smistachkin Tcl_NewWideIntObj((Tcl_WideInt)db)); 677b56660f5Smistachkin Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 678b56660f5Smistachkin Tcl_DecrRefCount(pCmd); 679b56660f5Smistachkin Tcl_ResetResult(pDb->interp); 680b56660f5Smistachkin break; 681b56660f5Smistachkin } 682b56660f5Smistachkin } 683b56660f5Smistachkin return SQLITE_OK; 684b56660f5Smistachkin } 685b56660f5Smistachkin #endif 686b56660f5Smistachkin 687b56660f5Smistachkin #ifndef SQLITE_OMIT_TRACE 688b56660f5Smistachkin /* 68919e2d37fSdrh ** This routine is called by the SQLite profile handler after a statement 69019e2d37fSdrh ** SQL has executed. The TCL script in pDb->zProfile is evaluated. 69119e2d37fSdrh */ 69219e2d37fSdrh static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){ 69319e2d37fSdrh SqliteDb *pDb = (SqliteDb*)cd; 69419e2d37fSdrh Tcl_DString str; 69519e2d37fSdrh char zTm[100]; 69619e2d37fSdrh 69719e2d37fSdrh sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm); 69819e2d37fSdrh Tcl_DStringInit(&str); 69919e2d37fSdrh Tcl_DStringAppend(&str, pDb->zProfile, -1); 70019e2d37fSdrh Tcl_DStringAppendElement(&str, zSql); 70119e2d37fSdrh Tcl_DStringAppendElement(&str, zTm); 70219e2d37fSdrh Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 70319e2d37fSdrh Tcl_DStringFree(&str); 70419e2d37fSdrh Tcl_ResetResult(pDb->interp); 70519e2d37fSdrh } 706d1167393Sdrh #endif 70719e2d37fSdrh 70819e2d37fSdrh /* 709aa940eacSdrh ** This routine is called when a transaction is committed. The 710aa940eacSdrh ** TCL script in pDb->zCommit is executed. If it returns non-zero or 711aa940eacSdrh ** if it throws an exception, the transaction is rolled back instead 712aa940eacSdrh ** of being committed. 713aa940eacSdrh */ 714aa940eacSdrh static int DbCommitHandler(void *cd){ 715aa940eacSdrh SqliteDb *pDb = (SqliteDb*)cd; 716aa940eacSdrh int rc; 717aa940eacSdrh 718aa940eacSdrh rc = Tcl_Eval(pDb->interp, pDb->zCommit); 719aa940eacSdrh if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 720aa940eacSdrh return 1; 721aa940eacSdrh } 722aa940eacSdrh return 0; 723aa940eacSdrh } 724aa940eacSdrh 72571fd80bfSdanielk1977 static void DbRollbackHandler(void *clientData){ 72671fd80bfSdanielk1977 SqliteDb *pDb = (SqliteDb*)clientData; 72771fd80bfSdanielk1977 assert(pDb->pRollbackHook); 72871fd80bfSdanielk1977 if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){ 72971fd80bfSdanielk1977 Tcl_BackgroundError(pDb->interp); 73071fd80bfSdanielk1977 } 73171fd80bfSdanielk1977 } 73271fd80bfSdanielk1977 7335def0843Sdrh /* 7345def0843Sdrh ** This procedure handles wal_hook callbacks. 7355def0843Sdrh */ 7365def0843Sdrh static int DbWalHandler( 7378d22a174Sdan void *clientData, 7388d22a174Sdan sqlite3 *db, 7398d22a174Sdan const char *zDb, 7408d22a174Sdan int nEntry 7418d22a174Sdan ){ 7425def0843Sdrh int ret = SQLITE_OK; 7438d22a174Sdan Tcl_Obj *p; 7448d22a174Sdan SqliteDb *pDb = (SqliteDb*)clientData; 7458d22a174Sdan Tcl_Interp *interp = pDb->interp; 7465def0843Sdrh assert(pDb->pWalHook); 7478d22a174Sdan 7486e45e0c8Sdan assert( db==pDb->db ); 7495def0843Sdrh p = Tcl_DuplicateObj(pDb->pWalHook); 7508d22a174Sdan Tcl_IncrRefCount(p); 7518d22a174Sdan Tcl_ListObjAppendElement(interp, p, Tcl_NewStringObj(zDb, -1)); 7528d22a174Sdan Tcl_ListObjAppendElement(interp, p, Tcl_NewIntObj(nEntry)); 7538d22a174Sdan if( TCL_OK!=Tcl_EvalObjEx(interp, p, 0) 7548d22a174Sdan || TCL_OK!=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &ret) 7558d22a174Sdan ){ 7568d22a174Sdan Tcl_BackgroundError(interp); 7578d22a174Sdan } 7588d22a174Sdan Tcl_DecrRefCount(p); 7598d22a174Sdan 7608d22a174Sdan return ret; 7618d22a174Sdan } 7628d22a174Sdan 763bcf4f484Sdrh #if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_UNLOCK_NOTIFY) 764404ca075Sdanielk1977 static void setTestUnlockNotifyVars(Tcl_Interp *interp, int iArg, int nArg){ 765404ca075Sdanielk1977 char zBuf[64]; 76665545b59Sdrh sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", iArg); 767404ca075Sdanielk1977 Tcl_SetVar(interp, "sqlite_unlock_notify_arg", zBuf, TCL_GLOBAL_ONLY); 76865545b59Sdrh sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nArg); 769404ca075Sdanielk1977 Tcl_SetVar(interp, "sqlite_unlock_notify_argcount", zBuf, TCL_GLOBAL_ONLY); 770404ca075Sdanielk1977 } 771404ca075Sdanielk1977 #else 772404ca075Sdanielk1977 # define setTestUnlockNotifyVars(x,y,z) 773404ca075Sdanielk1977 #endif 774404ca075Sdanielk1977 77569910da9Sdrh #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 776404ca075Sdanielk1977 static void DbUnlockNotify(void **apArg, int nArg){ 777404ca075Sdanielk1977 int i; 778404ca075Sdanielk1977 for(i=0; i<nArg; i++){ 779404ca075Sdanielk1977 const int flags = (TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); 780404ca075Sdanielk1977 SqliteDb *pDb = (SqliteDb *)apArg[i]; 781404ca075Sdanielk1977 setTestUnlockNotifyVars(pDb->interp, i, nArg); 782404ca075Sdanielk1977 assert( pDb->pUnlockNotify); 783404ca075Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pDb->pUnlockNotify, flags); 784404ca075Sdanielk1977 Tcl_DecrRefCount(pDb->pUnlockNotify); 785404ca075Sdanielk1977 pDb->pUnlockNotify = 0; 786404ca075Sdanielk1977 } 787404ca075Sdanielk1977 } 78869910da9Sdrh #endif 789404ca075Sdanielk1977 7909b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 79146c47d46Sdan /* 79246c47d46Sdan ** Pre-update hook callback. 79346c47d46Sdan */ 79446c47d46Sdan static void DbPreUpdateHandler( 79546c47d46Sdan void *p, 79646c47d46Sdan sqlite3 *db, 79746c47d46Sdan int op, 79846c47d46Sdan const char *zDb, 79946c47d46Sdan const char *zTbl, 80046c47d46Sdan sqlite_int64 iKey1, 80146c47d46Sdan sqlite_int64 iKey2 80246c47d46Sdan ){ 80346c47d46Sdan SqliteDb *pDb = (SqliteDb *)p; 80446c47d46Sdan Tcl_Obj *pCmd; 80546c47d46Sdan static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"}; 80646c47d46Sdan 80746c47d46Sdan assert( (SQLITE_DELETE-1)/9 == 0 ); 80846c47d46Sdan assert( (SQLITE_INSERT-1)/9 == 1 ); 80946c47d46Sdan assert( (SQLITE_UPDATE-1)/9 == 2 ); 81046c47d46Sdan assert( pDb->pPreUpdateHook ); 81146c47d46Sdan assert( db==pDb->db ); 81246c47d46Sdan assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); 81346c47d46Sdan 81446c47d46Sdan pCmd = Tcl_DuplicateObj(pDb->pPreUpdateHook); 81546c47d46Sdan Tcl_IncrRefCount(pCmd); 81646c47d46Sdan Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1)); 81746c47d46Sdan Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); 81846c47d46Sdan Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); 81946c47d46Sdan Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey1)); 82046c47d46Sdan Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey2)); 82146c47d46Sdan Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 82246c47d46Sdan Tcl_DecrRefCount(pCmd); 82346c47d46Sdan } 8249b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 82546c47d46Sdan 82694eb6a14Sdanielk1977 static void DbUpdateHandler( 82794eb6a14Sdanielk1977 void *p, 82894eb6a14Sdanielk1977 int op, 82994eb6a14Sdanielk1977 const char *zDb, 83094eb6a14Sdanielk1977 const char *zTbl, 83194eb6a14Sdanielk1977 sqlite_int64 rowid 83294eb6a14Sdanielk1977 ){ 83394eb6a14Sdanielk1977 SqliteDb *pDb = (SqliteDb *)p; 83494eb6a14Sdanielk1977 Tcl_Obj *pCmd; 83546c47d46Sdan static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"}; 83646c47d46Sdan 83746c47d46Sdan assert( (SQLITE_DELETE-1)/9 == 0 ); 83846c47d46Sdan assert( (SQLITE_INSERT-1)/9 == 1 ); 83946c47d46Sdan assert( (SQLITE_UPDATE-1)/9 == 2 ); 84094eb6a14Sdanielk1977 84194eb6a14Sdanielk1977 assert( pDb->pUpdateHook ); 84294eb6a14Sdanielk1977 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); 84394eb6a14Sdanielk1977 84494eb6a14Sdanielk1977 pCmd = Tcl_DuplicateObj(pDb->pUpdateHook); 84594eb6a14Sdanielk1977 Tcl_IncrRefCount(pCmd); 84646c47d46Sdan Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1)); 84794eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); 84894eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); 84994eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid)); 85094eb6a14Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 851efdde169Sdrh Tcl_DecrRefCount(pCmd); 85294eb6a14Sdanielk1977 } 85394eb6a14Sdanielk1977 8547cedc8d4Sdanielk1977 static void tclCollateNeeded( 8557cedc8d4Sdanielk1977 void *pCtx, 8569bb575fdSdrh sqlite3 *db, 8577cedc8d4Sdanielk1977 int enc, 8587cedc8d4Sdanielk1977 const char *zName 8597cedc8d4Sdanielk1977 ){ 8607cedc8d4Sdanielk1977 SqliteDb *pDb = (SqliteDb *)pCtx; 8617cedc8d4Sdanielk1977 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded); 8627cedc8d4Sdanielk1977 Tcl_IncrRefCount(pScript); 8637cedc8d4Sdanielk1977 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1)); 8647cedc8d4Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pScript, 0); 8657cedc8d4Sdanielk1977 Tcl_DecrRefCount(pScript); 8667cedc8d4Sdanielk1977 } 8677cedc8d4Sdanielk1977 868aa940eacSdrh /* 8690202b29eSdanielk1977 ** This routine is called to evaluate an SQL collation function implemented 8700202b29eSdanielk1977 ** using TCL script. 8710202b29eSdanielk1977 */ 8720202b29eSdanielk1977 static int tclSqlCollate( 8730202b29eSdanielk1977 void *pCtx, 8740202b29eSdanielk1977 int nA, 8750202b29eSdanielk1977 const void *zA, 8760202b29eSdanielk1977 int nB, 8770202b29eSdanielk1977 const void *zB 8780202b29eSdanielk1977 ){ 8790202b29eSdanielk1977 SqlCollate *p = (SqlCollate *)pCtx; 8800202b29eSdanielk1977 Tcl_Obj *pCmd; 8810202b29eSdanielk1977 8820202b29eSdanielk1977 pCmd = Tcl_NewStringObj(p->zScript, -1); 8830202b29eSdanielk1977 Tcl_IncrRefCount(pCmd); 8840202b29eSdanielk1977 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA)); 8850202b29eSdanielk1977 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB)); 886d1e4733dSdrh Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 8870202b29eSdanielk1977 Tcl_DecrRefCount(pCmd); 8880202b29eSdanielk1977 return (atoi(Tcl_GetStringResult(p->interp))); 8890202b29eSdanielk1977 } 8900202b29eSdanielk1977 8910202b29eSdanielk1977 /* 892cabb0819Sdrh ** This routine is called to evaluate an SQL function implemented 893cabb0819Sdrh ** using TCL script. 894cabb0819Sdrh */ 8950ae8b831Sdanielk1977 static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ 8966f8a503dSdanielk1977 SqlFunc *p = sqlite3_user_data(context); 897d1e4733dSdrh Tcl_Obj *pCmd; 898cabb0819Sdrh int i; 899cabb0819Sdrh int rc; 900cabb0819Sdrh 901d1e4733dSdrh if( argc==0 ){ 902d1e4733dSdrh /* If there are no arguments to the function, call Tcl_EvalObjEx on the 903d1e4733dSdrh ** script object directly. This allows the TCL compiler to generate 904d1e4733dSdrh ** bytecode for the command on the first invocation and thus make 905d1e4733dSdrh ** subsequent invocations much faster. */ 906d1e4733dSdrh pCmd = p->pScript; 907d1e4733dSdrh Tcl_IncrRefCount(pCmd); 908d1e4733dSdrh rc = Tcl_EvalObjEx(p->interp, pCmd, 0); 909d1e4733dSdrh Tcl_DecrRefCount(pCmd); 91051ad0ecdSdanielk1977 }else{ 911d1e4733dSdrh /* If there are arguments to the function, make a shallow copy of the 912d1e4733dSdrh ** script object, lappend the arguments, then evaluate the copy. 913d1e4733dSdrh ** 91460ec914cSpeter.d.reid ** By "shallow" copy, we mean only the outer list Tcl_Obj is duplicated. 915d1e4733dSdrh ** The new Tcl_Obj contains pointers to the original list elements. 916d1e4733dSdrh ** That way, when Tcl_EvalObjv() is run and shimmers the first element 917d1e4733dSdrh ** of the list to tclCmdNameType, that alternate representation will 918d1e4733dSdrh ** be preserved and reused on the next invocation. 919d1e4733dSdrh */ 920d1e4733dSdrh Tcl_Obj **aArg; 921d1e4733dSdrh int nArg; 922d1e4733dSdrh if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){ 923d1e4733dSdrh sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 924d1e4733dSdrh return; 925d1e4733dSdrh } 926d1e4733dSdrh pCmd = Tcl_NewListObj(nArg, aArg); 927d1e4733dSdrh Tcl_IncrRefCount(pCmd); 928d1e4733dSdrh for(i=0; i<argc; i++){ 929d1e4733dSdrh sqlite3_value *pIn = argv[i]; 930d1e4733dSdrh Tcl_Obj *pVal; 931d1e4733dSdrh 932d1e4733dSdrh /* Set pVal to contain the i'th column of this row. */ 933d1e4733dSdrh switch( sqlite3_value_type(pIn) ){ 934d1e4733dSdrh case SQLITE_BLOB: { 935d1e4733dSdrh int bytes = sqlite3_value_bytes(pIn); 936d1e4733dSdrh pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes); 937d1e4733dSdrh break; 938d1e4733dSdrh } 939d1e4733dSdrh case SQLITE_INTEGER: { 940d1e4733dSdrh sqlite_int64 v = sqlite3_value_int64(pIn); 941d1e4733dSdrh if( v>=-2147483647 && v<=2147483647 ){ 9427fd33929Sdrh pVal = Tcl_NewIntObj((int)v); 943d1e4733dSdrh }else{ 944d1e4733dSdrh pVal = Tcl_NewWideIntObj(v); 945d1e4733dSdrh } 946d1e4733dSdrh break; 947d1e4733dSdrh } 948d1e4733dSdrh case SQLITE_FLOAT: { 949d1e4733dSdrh double r = sqlite3_value_double(pIn); 950d1e4733dSdrh pVal = Tcl_NewDoubleObj(r); 951d1e4733dSdrh break; 952d1e4733dSdrh } 953d1e4733dSdrh case SQLITE_NULL: { 954c45e6716Sdrh pVal = Tcl_NewStringObj(p->pDb->zNull, -1); 955d1e4733dSdrh break; 956d1e4733dSdrh } 957d1e4733dSdrh default: { 958d1e4733dSdrh int bytes = sqlite3_value_bytes(pIn); 95900fd957bSdanielk1977 pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes); 960d1e4733dSdrh break; 96151ad0ecdSdanielk1977 } 962cabb0819Sdrh } 963d1e4733dSdrh rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal); 964d1e4733dSdrh if( rc ){ 965d1e4733dSdrh Tcl_DecrRefCount(pCmd); 966d1e4733dSdrh sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 967d1e4733dSdrh return; 968d1e4733dSdrh } 969d1e4733dSdrh } 970d1e4733dSdrh if( !p->useEvalObjv ){ 971d1e4733dSdrh /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd 972d1e4733dSdrh ** is a list without a string representation. To prevent this from 973d1e4733dSdrh ** happening, make sure pCmd has a valid string representation */ 974d1e4733dSdrh Tcl_GetString(pCmd); 975d1e4733dSdrh } 976d1e4733dSdrh rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 977d1e4733dSdrh Tcl_DecrRefCount(pCmd); 978d1e4733dSdrh } 979562e8d3cSdanielk1977 980c7f269d5Sdrh if( rc && rc!=TCL_RETURN ){ 9817e18c259Sdanielk1977 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 982cabb0819Sdrh }else{ 983c7f269d5Sdrh Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); 984c7f269d5Sdrh int n; 985c7f269d5Sdrh u8 *data; 9864a4c11aaSdan const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 987c7f269d5Sdrh char c = zType[0]; 988df0bddaeSdrh if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ 989d1e4733dSdrh /* Only return a BLOB type if the Tcl variable is a bytearray and 990df0bddaeSdrh ** has no string representation. */ 991c7f269d5Sdrh data = Tcl_GetByteArrayFromObj(pVar, &n); 992c7f269d5Sdrh sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT); 993985e0c63Sdrh }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 994c7f269d5Sdrh Tcl_GetIntFromObj(0, pVar, &n); 995c7f269d5Sdrh sqlite3_result_int(context, n); 996c7f269d5Sdrh }else if( c=='d' && strcmp(zType,"double")==0 ){ 997c7f269d5Sdrh double r; 998c7f269d5Sdrh Tcl_GetDoubleFromObj(0, pVar, &r); 999c7f269d5Sdrh sqlite3_result_double(context, r); 1000985e0c63Sdrh }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 1001985e0c63Sdrh (c=='i' && strcmp(zType,"int")==0) ){ 1002df0bddaeSdrh Tcl_WideInt v; 1003df0bddaeSdrh Tcl_GetWideIntFromObj(0, pVar, &v); 1004df0bddaeSdrh sqlite3_result_int64(context, v); 1005c7f269d5Sdrh }else{ 100600fd957bSdanielk1977 data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 100700fd957bSdanielk1977 sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT); 1008c7f269d5Sdrh } 1009cabb0819Sdrh } 1010cabb0819Sdrh } 1011895d7472Sdrh 1012e22a334bSdrh #ifndef SQLITE_OMIT_AUTHORIZATION 1013e22a334bSdrh /* 1014e22a334bSdrh ** This is the authentication function. It appends the authentication 1015e22a334bSdrh ** type code and the two arguments to zCmd[] then invokes the result 1016e22a334bSdrh ** on the interpreter. The reply is examined to determine if the 1017e22a334bSdrh ** authentication fails or succeeds. 1018e22a334bSdrh */ 1019e22a334bSdrh static int auth_callback( 1020e22a334bSdrh void *pArg, 1021e22a334bSdrh int code, 1022e22a334bSdrh const char *zArg1, 1023e22a334bSdrh const char *zArg2, 1024e22a334bSdrh const char *zArg3, 1025e22a334bSdrh const char *zArg4 102632c6a48bSdrh #ifdef SQLITE_USER_AUTHENTICATION 102732c6a48bSdrh ,const char *zArg5 102832c6a48bSdrh #endif 1029e22a334bSdrh ){ 10306ef5e12eSmistachkin const char *zCode; 1031e22a334bSdrh Tcl_DString str; 1032e22a334bSdrh int rc; 1033e22a334bSdrh const char *zReply; 1034e22a334bSdrh SqliteDb *pDb = (SqliteDb*)pArg; 10351f1549f8Sdrh if( pDb->disableAuth ) return SQLITE_OK; 1036e22a334bSdrh 1037e22a334bSdrh switch( code ){ 1038e22a334bSdrh case SQLITE_COPY : zCode="SQLITE_COPY"; break; 1039e22a334bSdrh case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; 1040e22a334bSdrh case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; 1041e22a334bSdrh case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; 1042e22a334bSdrh case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; 1043e22a334bSdrh case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; 1044e22a334bSdrh case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; 1045e22a334bSdrh case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; 1046e22a334bSdrh case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; 1047e22a334bSdrh case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; 1048e22a334bSdrh case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; 1049e22a334bSdrh case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; 1050e22a334bSdrh case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; 1051e22a334bSdrh case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; 1052e22a334bSdrh case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; 1053e22a334bSdrh case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; 1054e22a334bSdrh case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; 1055e22a334bSdrh case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; 1056e22a334bSdrh case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; 1057e22a334bSdrh case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; 1058e22a334bSdrh case SQLITE_READ : zCode="SQLITE_READ"; break; 1059e22a334bSdrh case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; 1060e22a334bSdrh case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; 1061e22a334bSdrh case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; 106281e293b4Sdrh case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; 106381e293b4Sdrh case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; 10641c8c23ccSdanielk1977 case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break; 10651d54df88Sdanielk1977 case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break; 1066e6e04969Sdrh case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break; 1067f1a381e7Sdanielk1977 case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break; 1068f1a381e7Sdanielk1977 case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break; 10695169bbc6Sdrh case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break; 1070ab9b703fSdanielk1977 case SQLITE_SAVEPOINT : zCode="SQLITE_SAVEPOINT"; break; 107165a2aaa6Sdrh case SQLITE_RECURSIVE : zCode="SQLITE_RECURSIVE"; break; 1072e22a334bSdrh default : zCode="????"; break; 1073e22a334bSdrh } 1074e22a334bSdrh Tcl_DStringInit(&str); 1075e22a334bSdrh Tcl_DStringAppend(&str, pDb->zAuth, -1); 1076e22a334bSdrh Tcl_DStringAppendElement(&str, zCode); 1077e22a334bSdrh Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); 1078e22a334bSdrh Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); 1079e22a334bSdrh Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); 1080e22a334bSdrh Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); 108132c6a48bSdrh #ifdef SQLITE_USER_AUTHENTICATION 108232c6a48bSdrh Tcl_DStringAppendElement(&str, zArg5 ? zArg5 : ""); 108332c6a48bSdrh #endif 1084e22a334bSdrh rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); 1085e22a334bSdrh Tcl_DStringFree(&str); 1086b07028f7Sdrh zReply = rc==TCL_OK ? Tcl_GetStringResult(pDb->interp) : "SQLITE_DENY"; 1087e22a334bSdrh if( strcmp(zReply,"SQLITE_OK")==0 ){ 1088e22a334bSdrh rc = SQLITE_OK; 1089e22a334bSdrh }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ 1090e22a334bSdrh rc = SQLITE_DENY; 1091e22a334bSdrh }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ 1092e22a334bSdrh rc = SQLITE_IGNORE; 1093e22a334bSdrh }else{ 1094e22a334bSdrh rc = 999; 1095e22a334bSdrh } 1096e22a334bSdrh return rc; 1097e22a334bSdrh } 1098e22a334bSdrh #endif /* SQLITE_OMIT_AUTHORIZATION */ 1099cabb0819Sdrh 1100cabb0819Sdrh /* 11011067fe11Stpoindex ** This routine reads a line of text from FILE in, stores 11021067fe11Stpoindex ** the text in memory obtained from malloc() and returns a pointer 11031067fe11Stpoindex ** to the text. NULL is returned at end of file, or if malloc() 11041067fe11Stpoindex ** fails. 11051067fe11Stpoindex ** 11061067fe11Stpoindex ** The interface is like "readline" but no command-line editing 11071067fe11Stpoindex ** is done. 11081067fe11Stpoindex ** 11091067fe11Stpoindex ** copied from shell.c from '.import' command 11101067fe11Stpoindex */ 11111067fe11Stpoindex static char *local_getline(char *zPrompt, FILE *in){ 11121067fe11Stpoindex char *zLine; 11131067fe11Stpoindex int nLine; 11141067fe11Stpoindex int n; 11151067fe11Stpoindex 11161067fe11Stpoindex nLine = 100; 11171067fe11Stpoindex zLine = malloc( nLine ); 11181067fe11Stpoindex if( zLine==0 ) return 0; 11191067fe11Stpoindex n = 0; 1120b07028f7Sdrh while( 1 ){ 11211067fe11Stpoindex if( n+100>nLine ){ 11221067fe11Stpoindex nLine = nLine*2 + 100; 11231067fe11Stpoindex zLine = realloc(zLine, nLine); 11241067fe11Stpoindex if( zLine==0 ) return 0; 11251067fe11Stpoindex } 11261067fe11Stpoindex if( fgets(&zLine[n], nLine - n, in)==0 ){ 11271067fe11Stpoindex if( n==0 ){ 11281067fe11Stpoindex free(zLine); 11291067fe11Stpoindex return 0; 11301067fe11Stpoindex } 11311067fe11Stpoindex zLine[n] = 0; 11321067fe11Stpoindex break; 11331067fe11Stpoindex } 11341067fe11Stpoindex while( zLine[n] ){ n++; } 11351067fe11Stpoindex if( n>0 && zLine[n-1]=='\n' ){ 11361067fe11Stpoindex n--; 11371067fe11Stpoindex zLine[n] = 0; 1138b07028f7Sdrh break; 11391067fe11Stpoindex } 11401067fe11Stpoindex } 11411067fe11Stpoindex zLine = realloc( zLine, n+1 ); 11421067fe11Stpoindex return zLine; 11431067fe11Stpoindex } 11441067fe11Stpoindex 11458e556520Sdanielk1977 11468e556520Sdanielk1977 /* 11474a4c11aaSdan ** This function is part of the implementation of the command: 11488e556520Sdanielk1977 ** 11494a4c11aaSdan ** $db transaction [-deferred|-immediate|-exclusive] SCRIPT 11508e556520Sdanielk1977 ** 11514a4c11aaSdan ** It is invoked after evaluating the script SCRIPT to commit or rollback 11524a4c11aaSdan ** the transaction or savepoint opened by the [transaction] command. 11534a4c11aaSdan */ 1154a121cc7cSmistachkin static int SQLITE_TCLAPI DbTransPostCmd( 11554a4c11aaSdan ClientData data[], /* data[0] is the Sqlite3Db* for $db */ 11564a4c11aaSdan Tcl_Interp *interp, /* Tcl interpreter */ 11574a4c11aaSdan int result /* Result of evaluating SCRIPT */ 11584a4c11aaSdan ){ 11596ef5e12eSmistachkin static const char *const azEnd[] = { 11604a4c11aaSdan "RELEASE _tcl_transaction", /* rc==TCL_ERROR, nTransaction!=0 */ 11614a4c11aaSdan "COMMIT", /* rc!=TCL_ERROR, nTransaction==0 */ 11624a4c11aaSdan "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction", 11634a4c11aaSdan "ROLLBACK" /* rc==TCL_ERROR, nTransaction==0 */ 11644a4c11aaSdan }; 11654a4c11aaSdan SqliteDb *pDb = (SqliteDb*)data[0]; 11664a4c11aaSdan int rc = result; 11674a4c11aaSdan const char *zEnd; 11684a4c11aaSdan 11694a4c11aaSdan pDb->nTransaction--; 11704a4c11aaSdan zEnd = azEnd[(rc==TCL_ERROR)*2 + (pDb->nTransaction==0)]; 11714a4c11aaSdan 11724a4c11aaSdan pDb->disableAuth++; 11734a4c11aaSdan if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){ 11744a4c11aaSdan /* This is a tricky scenario to handle. The most likely cause of an 11754a4c11aaSdan ** error is that the exec() above was an attempt to commit the 11764a4c11aaSdan ** top-level transaction that returned SQLITE_BUSY. Or, less likely, 117748864df9Smistachkin ** that an IO-error has occurred. In either case, throw a Tcl exception 11784a4c11aaSdan ** and try to rollback the transaction. 11794a4c11aaSdan ** 11804a4c11aaSdan ** But it could also be that the user executed one or more BEGIN, 11814a4c11aaSdan ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing 11824a4c11aaSdan ** this method's logic. Not clear how this would be best handled. 11834a4c11aaSdan */ 11844a4c11aaSdan if( rc!=TCL_ERROR ){ 1185a198f2b5Sdrh Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 11864a4c11aaSdan rc = TCL_ERROR; 11874a4c11aaSdan } 11884a4c11aaSdan sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0); 11894a4c11aaSdan } 11904a4c11aaSdan pDb->disableAuth--; 11914a4c11aaSdan 11924a4c11aaSdan return rc; 11934a4c11aaSdan } 11944a4c11aaSdan 11954a4c11aaSdan /* 1196c431fd55Sdan ** Unless SQLITE_TEST is defined, this function is a simple wrapper around 1197c431fd55Sdan ** sqlite3_prepare_v2(). If SQLITE_TEST is defined, then it uses either 1198c431fd55Sdan ** sqlite3_prepare_v2() or legacy interface sqlite3_prepare(), depending 1199c431fd55Sdan ** on whether or not the [db_use_legacy_prepare] command has been used to 1200c431fd55Sdan ** configure the connection. 1201c431fd55Sdan */ 1202c431fd55Sdan static int dbPrepare( 1203c431fd55Sdan SqliteDb *pDb, /* Database object */ 1204c431fd55Sdan const char *zSql, /* SQL to compile */ 1205c431fd55Sdan sqlite3_stmt **ppStmt, /* OUT: Prepared statement */ 1206c431fd55Sdan const char **pzOut /* OUT: Pointer to next SQL statement */ 1207c431fd55Sdan ){ 1208c431fd55Sdan #ifdef SQLITE_TEST 1209c431fd55Sdan if( pDb->bLegacyPrepare ){ 1210c431fd55Sdan return sqlite3_prepare(pDb->db, zSql, -1, ppStmt, pzOut); 1211c431fd55Sdan } 1212c431fd55Sdan #endif 1213c431fd55Sdan return sqlite3_prepare_v2(pDb->db, zSql, -1, ppStmt, pzOut); 1214c431fd55Sdan } 1215c431fd55Sdan 1216c431fd55Sdan /* 12174a4c11aaSdan ** Search the cache for a prepared-statement object that implements the 12184a4c11aaSdan ** first SQL statement in the buffer pointed to by parameter zIn. If 12194a4c11aaSdan ** no such prepared-statement can be found, allocate and prepare a new 12204a4c11aaSdan ** one. In either case, bind the current values of the relevant Tcl 12214a4c11aaSdan ** variables to any $var, :var or @var variables in the statement. Before 12224a4c11aaSdan ** returning, set *ppPreStmt to point to the prepared-statement object. 12234a4c11aaSdan ** 12244a4c11aaSdan ** Output parameter *pzOut is set to point to the next SQL statement in 12254a4c11aaSdan ** buffer zIn, or to the '\0' byte at the end of zIn if there is no 12264a4c11aaSdan ** next statement. 12274a4c11aaSdan ** 12284a4c11aaSdan ** If successful, TCL_OK is returned. Otherwise, TCL_ERROR is returned 12294a4c11aaSdan ** and an error message loaded into interpreter pDb->interp. 12304a4c11aaSdan */ 12314a4c11aaSdan static int dbPrepareAndBind( 12324a4c11aaSdan SqliteDb *pDb, /* Database object */ 12334a4c11aaSdan char const *zIn, /* SQL to compile */ 12344a4c11aaSdan char const **pzOut, /* OUT: Pointer to next SQL statement */ 12354a4c11aaSdan SqlPreparedStmt **ppPreStmt /* OUT: Object used to cache statement */ 12364a4c11aaSdan ){ 12374a4c11aaSdan const char *zSql = zIn; /* Pointer to first SQL statement in zIn */ 12387bb22ac7Smistachkin sqlite3_stmt *pStmt = 0; /* Prepared statement object */ 12394a4c11aaSdan SqlPreparedStmt *pPreStmt; /* Pointer to cached statement */ 12404a4c11aaSdan int nSql; /* Length of zSql in bytes */ 12417bb22ac7Smistachkin int nVar = 0; /* Number of variables in statement */ 12424a4c11aaSdan int iParm = 0; /* Next free entry in apParm */ 12430425f189Sdrh char c; 12444a4c11aaSdan int i; 12454a4c11aaSdan Tcl_Interp *interp = pDb->interp; 12464a4c11aaSdan 12474a4c11aaSdan *ppPreStmt = 0; 12484a4c11aaSdan 12494a4c11aaSdan /* Trim spaces from the start of zSql and calculate the remaining length. */ 12500425f189Sdrh while( (c = zSql[0])==' ' || c=='\t' || c=='\r' || c=='\n' ){ zSql++; } 12514a4c11aaSdan nSql = strlen30(zSql); 12524a4c11aaSdan 12534a4c11aaSdan for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){ 12544a4c11aaSdan int n = pPreStmt->nSql; 12554a4c11aaSdan if( nSql>=n 12564a4c11aaSdan && memcmp(pPreStmt->zSql, zSql, n)==0 12574a4c11aaSdan && (zSql[n]==0 || zSql[n-1]==';') 12584a4c11aaSdan ){ 12594a4c11aaSdan pStmt = pPreStmt->pStmt; 12604a4c11aaSdan *pzOut = &zSql[pPreStmt->nSql]; 12614a4c11aaSdan 12624a4c11aaSdan /* When a prepared statement is found, unlink it from the 12634a4c11aaSdan ** cache list. It will later be added back to the beginning 12644a4c11aaSdan ** of the cache list in order to implement LRU replacement. 12654a4c11aaSdan */ 12664a4c11aaSdan if( pPreStmt->pPrev ){ 12674a4c11aaSdan pPreStmt->pPrev->pNext = pPreStmt->pNext; 12684a4c11aaSdan }else{ 12694a4c11aaSdan pDb->stmtList = pPreStmt->pNext; 12704a4c11aaSdan } 12714a4c11aaSdan if( pPreStmt->pNext ){ 12724a4c11aaSdan pPreStmt->pNext->pPrev = pPreStmt->pPrev; 12734a4c11aaSdan }else{ 12744a4c11aaSdan pDb->stmtLast = pPreStmt->pPrev; 12754a4c11aaSdan } 12764a4c11aaSdan pDb->nStmt--; 12774a4c11aaSdan nVar = sqlite3_bind_parameter_count(pStmt); 12784a4c11aaSdan break; 12794a4c11aaSdan } 12804a4c11aaSdan } 12814a4c11aaSdan 12824a4c11aaSdan /* If no prepared statement was found. Compile the SQL text. Also allocate 12834a4c11aaSdan ** a new SqlPreparedStmt structure. */ 12844a4c11aaSdan if( pPreStmt==0 ){ 12854a4c11aaSdan int nByte; 12864a4c11aaSdan 1287c431fd55Sdan if( SQLITE_OK!=dbPrepare(pDb, zSql, &pStmt, pzOut) ){ 1288c45e6716Sdrh Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 12894a4c11aaSdan return TCL_ERROR; 12904a4c11aaSdan } 12914a4c11aaSdan if( pStmt==0 ){ 12924a4c11aaSdan if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ 12934a4c11aaSdan /* A compile-time error in the statement. */ 1294c45e6716Sdrh Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 12954a4c11aaSdan return TCL_ERROR; 12964a4c11aaSdan }else{ 12974a4c11aaSdan /* The statement was a no-op. Continue to the next statement 12984a4c11aaSdan ** in the SQL string. 12994a4c11aaSdan */ 13004a4c11aaSdan return TCL_OK; 13014a4c11aaSdan } 13024a4c11aaSdan } 13034a4c11aaSdan 13044a4c11aaSdan assert( pPreStmt==0 ); 13054a4c11aaSdan nVar = sqlite3_bind_parameter_count(pStmt); 13064a4c11aaSdan nByte = sizeof(SqlPreparedStmt) + nVar*sizeof(Tcl_Obj *); 13074a4c11aaSdan pPreStmt = (SqlPreparedStmt*)Tcl_Alloc(nByte); 13084a4c11aaSdan memset(pPreStmt, 0, nByte); 13094a4c11aaSdan 13104a4c11aaSdan pPreStmt->pStmt = pStmt; 13117ed243b7Sdrh pPreStmt->nSql = (int)(*pzOut - zSql); 13124a4c11aaSdan pPreStmt->zSql = sqlite3_sql(pStmt); 13134a4c11aaSdan pPreStmt->apParm = (Tcl_Obj **)&pPreStmt[1]; 1314c431fd55Sdan #ifdef SQLITE_TEST 1315c431fd55Sdan if( pPreStmt->zSql==0 ){ 1316c431fd55Sdan char *zCopy = Tcl_Alloc(pPreStmt->nSql + 1); 1317c431fd55Sdan memcpy(zCopy, zSql, pPreStmt->nSql); 1318c431fd55Sdan zCopy[pPreStmt->nSql] = '\0'; 1319c431fd55Sdan pPreStmt->zSql = zCopy; 1320c431fd55Sdan } 1321c431fd55Sdan #endif 13224a4c11aaSdan } 13234a4c11aaSdan assert( pPreStmt ); 13244a4c11aaSdan assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql ); 13254a4c11aaSdan assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) ); 13264a4c11aaSdan 13274a4c11aaSdan /* Bind values to parameters that begin with $ or : */ 13284a4c11aaSdan for(i=1; i<=nVar; i++){ 13294a4c11aaSdan const char *zVar = sqlite3_bind_parameter_name(pStmt, i); 13304a4c11aaSdan if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){ 13314a4c11aaSdan Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); 13324a4c11aaSdan if( pVar ){ 13334a4c11aaSdan int n; 13344a4c11aaSdan u8 *data; 13354a4c11aaSdan const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 13368e18922fSmistachkin c = zType[0]; 13374a4c11aaSdan if( zVar[0]=='@' || 13384a4c11aaSdan (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){ 13394a4c11aaSdan /* Load a BLOB type if the Tcl variable is a bytearray and 13404a4c11aaSdan ** it has no string representation or the host 13414a4c11aaSdan ** parameter name begins with "@". */ 13424a4c11aaSdan data = Tcl_GetByteArrayFromObj(pVar, &n); 13434a4c11aaSdan sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); 13444a4c11aaSdan Tcl_IncrRefCount(pVar); 13454a4c11aaSdan pPreStmt->apParm[iParm++] = pVar; 13464a4c11aaSdan }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 13474a4c11aaSdan Tcl_GetIntFromObj(interp, pVar, &n); 13484a4c11aaSdan sqlite3_bind_int(pStmt, i, n); 13494a4c11aaSdan }else if( c=='d' && strcmp(zType,"double")==0 ){ 13504a4c11aaSdan double r; 13514a4c11aaSdan Tcl_GetDoubleFromObj(interp, pVar, &r); 13524a4c11aaSdan sqlite3_bind_double(pStmt, i, r); 13534a4c11aaSdan }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 13544a4c11aaSdan (c=='i' && strcmp(zType,"int")==0) ){ 13554a4c11aaSdan Tcl_WideInt v; 13564a4c11aaSdan Tcl_GetWideIntFromObj(interp, pVar, &v); 13574a4c11aaSdan sqlite3_bind_int64(pStmt, i, v); 13584a4c11aaSdan }else{ 13594a4c11aaSdan data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 13604a4c11aaSdan sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); 13614a4c11aaSdan Tcl_IncrRefCount(pVar); 13624a4c11aaSdan pPreStmt->apParm[iParm++] = pVar; 13634a4c11aaSdan } 13644a4c11aaSdan }else{ 13654a4c11aaSdan sqlite3_bind_null(pStmt, i); 13664a4c11aaSdan } 13674a4c11aaSdan } 13684a4c11aaSdan } 13694a4c11aaSdan pPreStmt->nParm = iParm; 13704a4c11aaSdan *ppPreStmt = pPreStmt; 1371937d0deaSdan 13724a4c11aaSdan return TCL_OK; 13734a4c11aaSdan } 13744a4c11aaSdan 13754a4c11aaSdan /* 13764a4c11aaSdan ** Release a statement reference obtained by calling dbPrepareAndBind(). 13774a4c11aaSdan ** There should be exactly one call to this function for each call to 13784a4c11aaSdan ** dbPrepareAndBind(). 13794a4c11aaSdan ** 13804a4c11aaSdan ** If the discard parameter is non-zero, then the statement is deleted 13814a4c11aaSdan ** immediately. Otherwise it is added to the LRU list and may be returned 13824a4c11aaSdan ** by a subsequent call to dbPrepareAndBind(). 13834a4c11aaSdan */ 13844a4c11aaSdan static void dbReleaseStmt( 13854a4c11aaSdan SqliteDb *pDb, /* Database handle */ 13864a4c11aaSdan SqlPreparedStmt *pPreStmt, /* Prepared statement handle to release */ 13874a4c11aaSdan int discard /* True to delete (not cache) the pPreStmt */ 13884a4c11aaSdan ){ 13894a4c11aaSdan int i; 13904a4c11aaSdan 13914a4c11aaSdan /* Free the bound string and blob parameters */ 13924a4c11aaSdan for(i=0; i<pPreStmt->nParm; i++){ 13934a4c11aaSdan Tcl_DecrRefCount(pPreStmt->apParm[i]); 13944a4c11aaSdan } 13954a4c11aaSdan pPreStmt->nParm = 0; 13964a4c11aaSdan 13974a4c11aaSdan if( pDb->maxStmt<=0 || discard ){ 13984a4c11aaSdan /* If the cache is turned off, deallocated the statement */ 1399c431fd55Sdan dbFreeStmt(pPreStmt); 14004a4c11aaSdan }else{ 14014a4c11aaSdan /* Add the prepared statement to the beginning of the cache list. */ 14024a4c11aaSdan pPreStmt->pNext = pDb->stmtList; 14034a4c11aaSdan pPreStmt->pPrev = 0; 14044a4c11aaSdan if( pDb->stmtList ){ 14054a4c11aaSdan pDb->stmtList->pPrev = pPreStmt; 14064a4c11aaSdan } 14074a4c11aaSdan pDb->stmtList = pPreStmt; 14084a4c11aaSdan if( pDb->stmtLast==0 ){ 14094a4c11aaSdan assert( pDb->nStmt==0 ); 14104a4c11aaSdan pDb->stmtLast = pPreStmt; 14114a4c11aaSdan }else{ 14124a4c11aaSdan assert( pDb->nStmt>0 ); 14134a4c11aaSdan } 14144a4c11aaSdan pDb->nStmt++; 14154a4c11aaSdan 14164a4c11aaSdan /* If we have too many statement in cache, remove the surplus from 14174a4c11aaSdan ** the end of the cache list. */ 14184a4c11aaSdan while( pDb->nStmt>pDb->maxStmt ){ 1419c431fd55Sdan SqlPreparedStmt *pLast = pDb->stmtLast; 1420c431fd55Sdan pDb->stmtLast = pLast->pPrev; 14214a4c11aaSdan pDb->stmtLast->pNext = 0; 14224a4c11aaSdan pDb->nStmt--; 1423c431fd55Sdan dbFreeStmt(pLast); 14244a4c11aaSdan } 14254a4c11aaSdan } 14264a4c11aaSdan } 14274a4c11aaSdan 14284a4c11aaSdan /* 14294a4c11aaSdan ** Structure used with dbEvalXXX() functions: 14304a4c11aaSdan ** 14314a4c11aaSdan ** dbEvalInit() 14324a4c11aaSdan ** dbEvalStep() 14334a4c11aaSdan ** dbEvalFinalize() 14344a4c11aaSdan ** dbEvalRowInfo() 14354a4c11aaSdan ** dbEvalColumnValue() 14364a4c11aaSdan */ 14374a4c11aaSdan typedef struct DbEvalContext DbEvalContext; 14384a4c11aaSdan struct DbEvalContext { 14394a4c11aaSdan SqliteDb *pDb; /* Database handle */ 14404a4c11aaSdan Tcl_Obj *pSql; /* Object holding string zSql */ 14414a4c11aaSdan const char *zSql; /* Remaining SQL to execute */ 14424a4c11aaSdan SqlPreparedStmt *pPreStmt; /* Current statement */ 14434a4c11aaSdan int nCol; /* Number of columns returned by pStmt */ 14444a4c11aaSdan Tcl_Obj *pArray; /* Name of array variable */ 14454a4c11aaSdan Tcl_Obj **apColName; /* Array of column names */ 14464a4c11aaSdan }; 14474a4c11aaSdan 14484a4c11aaSdan /* 14494a4c11aaSdan ** Release any cache of column names currently held as part of 14504a4c11aaSdan ** the DbEvalContext structure passed as the first argument. 14514a4c11aaSdan */ 14524a4c11aaSdan static void dbReleaseColumnNames(DbEvalContext *p){ 14534a4c11aaSdan if( p->apColName ){ 14544a4c11aaSdan int i; 14554a4c11aaSdan for(i=0; i<p->nCol; i++){ 14564a4c11aaSdan Tcl_DecrRefCount(p->apColName[i]); 14574a4c11aaSdan } 14584a4c11aaSdan Tcl_Free((char *)p->apColName); 14594a4c11aaSdan p->apColName = 0; 14604a4c11aaSdan } 14614a4c11aaSdan p->nCol = 0; 14624a4c11aaSdan } 14634a4c11aaSdan 14644a4c11aaSdan /* 14654a4c11aaSdan ** Initialize a DbEvalContext structure. 14668e556520Sdanielk1977 ** 14678e556520Sdanielk1977 ** If pArray is not NULL, then it contains the name of a Tcl array 14688e556520Sdanielk1977 ** variable. The "*" member of this array is set to a list containing 14694a4c11aaSdan ** the names of the columns returned by the statement as part of each 14704a4c11aaSdan ** call to dbEvalStep(), in order from left to right. e.g. if the names 14714a4c11aaSdan ** of the returned columns are a, b and c, it does the equivalent of the 14724a4c11aaSdan ** tcl command: 14738e556520Sdanielk1977 ** 14748e556520Sdanielk1977 ** set ${pArray}(*) {a b c} 14758e556520Sdanielk1977 */ 14764a4c11aaSdan static void dbEvalInit( 14774a4c11aaSdan DbEvalContext *p, /* Pointer to structure to initialize */ 14784a4c11aaSdan SqliteDb *pDb, /* Database handle */ 14794a4c11aaSdan Tcl_Obj *pSql, /* Object containing SQL script */ 14804a4c11aaSdan Tcl_Obj *pArray /* Name of Tcl array to set (*) element of */ 14818e556520Sdanielk1977 ){ 14824a4c11aaSdan memset(p, 0, sizeof(DbEvalContext)); 14834a4c11aaSdan p->pDb = pDb; 14844a4c11aaSdan p->zSql = Tcl_GetString(pSql); 14854a4c11aaSdan p->pSql = pSql; 14864a4c11aaSdan Tcl_IncrRefCount(pSql); 14874a4c11aaSdan if( pArray ){ 14884a4c11aaSdan p->pArray = pArray; 14894a4c11aaSdan Tcl_IncrRefCount(pArray); 14904a4c11aaSdan } 14914a4c11aaSdan } 14928e556520Sdanielk1977 14934a4c11aaSdan /* 14944a4c11aaSdan ** Obtain information about the row that the DbEvalContext passed as the 14954a4c11aaSdan ** first argument currently points to. 14964a4c11aaSdan */ 14974a4c11aaSdan static void dbEvalRowInfo( 14984a4c11aaSdan DbEvalContext *p, /* Evaluation context */ 14994a4c11aaSdan int *pnCol, /* OUT: Number of column names */ 15004a4c11aaSdan Tcl_Obj ***papColName /* OUT: Array of column names */ 15014a4c11aaSdan ){ 15028e556520Sdanielk1977 /* Compute column names */ 15034a4c11aaSdan if( 0==p->apColName ){ 15044a4c11aaSdan sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 15054a4c11aaSdan int i; /* Iterator variable */ 15064a4c11aaSdan int nCol; /* Number of columns returned by pStmt */ 15074a4c11aaSdan Tcl_Obj **apColName = 0; /* Array of column names */ 15084a4c11aaSdan 15094a4c11aaSdan p->nCol = nCol = sqlite3_column_count(pStmt); 15104a4c11aaSdan if( nCol>0 && (papColName || p->pArray) ){ 15114a4c11aaSdan apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); 15128e556520Sdanielk1977 for(i=0; i<nCol; i++){ 1513c45e6716Sdrh apColName[i] = Tcl_NewStringObj(sqlite3_column_name(pStmt,i), -1); 15148e556520Sdanielk1977 Tcl_IncrRefCount(apColName[i]); 15158e556520Sdanielk1977 } 15164a4c11aaSdan p->apColName = apColName; 15174a4c11aaSdan } 15188e556520Sdanielk1977 15198e556520Sdanielk1977 /* If results are being stored in an array variable, then create 15208e556520Sdanielk1977 ** the array(*) entry for that array 15218e556520Sdanielk1977 */ 15224a4c11aaSdan if( p->pArray ){ 15234a4c11aaSdan Tcl_Interp *interp = p->pDb->interp; 15248e556520Sdanielk1977 Tcl_Obj *pColList = Tcl_NewObj(); 15258e556520Sdanielk1977 Tcl_Obj *pStar = Tcl_NewStringObj("*", -1); 15264a4c11aaSdan 15278e556520Sdanielk1977 for(i=0; i<nCol; i++){ 15288e556520Sdanielk1977 Tcl_ListObjAppendElement(interp, pColList, apColName[i]); 15298e556520Sdanielk1977 } 15308e556520Sdanielk1977 Tcl_IncrRefCount(pStar); 15314a4c11aaSdan Tcl_ObjSetVar2(interp, p->pArray, pStar, pColList, 0); 15328e556520Sdanielk1977 Tcl_DecrRefCount(pStar); 15338e556520Sdanielk1977 } 15348e556520Sdanielk1977 } 15358e556520Sdanielk1977 15364a4c11aaSdan if( papColName ){ 15374a4c11aaSdan *papColName = p->apColName; 15384a4c11aaSdan } 15394a4c11aaSdan if( pnCol ){ 15404a4c11aaSdan *pnCol = p->nCol; 15414a4c11aaSdan } 15424a4c11aaSdan } 15434a4c11aaSdan 15444a4c11aaSdan /* 15454a4c11aaSdan ** Return one of TCL_OK, TCL_BREAK or TCL_ERROR. If TCL_ERROR is 15464a4c11aaSdan ** returned, then an error message is stored in the interpreter before 15474a4c11aaSdan ** returning. 15484a4c11aaSdan ** 15494a4c11aaSdan ** A return value of TCL_OK means there is a row of data available. The 15504a4c11aaSdan ** data may be accessed using dbEvalRowInfo() and dbEvalColumnValue(). This 15514a4c11aaSdan ** is analogous to a return of SQLITE_ROW from sqlite3_step(). If TCL_BREAK 15524a4c11aaSdan ** is returned, then the SQL script has finished executing and there are 15534a4c11aaSdan ** no further rows available. This is similar to SQLITE_DONE. 15544a4c11aaSdan */ 15554a4c11aaSdan static int dbEvalStep(DbEvalContext *p){ 1556c431fd55Sdan const char *zPrevSql = 0; /* Previous value of p->zSql */ 1557c431fd55Sdan 15584a4c11aaSdan while( p->zSql[0] || p->pPreStmt ){ 15594a4c11aaSdan int rc; 15604a4c11aaSdan if( p->pPreStmt==0 ){ 1561c431fd55Sdan zPrevSql = (p->zSql==zPrevSql ? 0 : p->zSql); 15624a4c11aaSdan rc = dbPrepareAndBind(p->pDb, p->zSql, &p->zSql, &p->pPreStmt); 15634a4c11aaSdan if( rc!=TCL_OK ) return rc; 15644a4c11aaSdan }else{ 15654a4c11aaSdan int rcs; 15664a4c11aaSdan SqliteDb *pDb = p->pDb; 15674a4c11aaSdan SqlPreparedStmt *pPreStmt = p->pPreStmt; 15684a4c11aaSdan sqlite3_stmt *pStmt = pPreStmt->pStmt; 15694a4c11aaSdan 15704a4c11aaSdan rcs = sqlite3_step(pStmt); 15714a4c11aaSdan if( rcs==SQLITE_ROW ){ 15724a4c11aaSdan return TCL_OK; 15734a4c11aaSdan } 15744a4c11aaSdan if( p->pArray ){ 15754a4c11aaSdan dbEvalRowInfo(p, 0, 0); 15764a4c11aaSdan } 15774a4c11aaSdan rcs = sqlite3_reset(pStmt); 15784a4c11aaSdan 15794a4c11aaSdan pDb->nStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_FULLSCAN_STEP,1); 15804a4c11aaSdan pDb->nSort = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_SORT,1); 15813c379b01Sdrh pDb->nIndex = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_AUTOINDEX,1); 15824a4c11aaSdan dbReleaseColumnNames(p); 15834a4c11aaSdan p->pPreStmt = 0; 15844a4c11aaSdan 15854a4c11aaSdan if( rcs!=SQLITE_OK ){ 15864a4c11aaSdan /* If a run-time error occurs, report the error and stop reading 15874a4c11aaSdan ** the SQL. */ 15884a4c11aaSdan dbReleaseStmt(pDb, pPreStmt, 1); 1589c431fd55Sdan #if SQLITE_TEST 1590c431fd55Sdan if( p->pDb->bLegacyPrepare && rcs==SQLITE_SCHEMA && zPrevSql ){ 1591c431fd55Sdan /* If the runtime error was an SQLITE_SCHEMA, and the database 1592c431fd55Sdan ** handle is configured to use the legacy sqlite3_prepare() 1593c431fd55Sdan ** interface, retry prepare()/step() on the same SQL statement. 1594c431fd55Sdan ** This only happens once. If there is a second SQLITE_SCHEMA 1595c431fd55Sdan ** error, the error will be returned to the caller. */ 1596c431fd55Sdan p->zSql = zPrevSql; 1597c431fd55Sdan continue; 1598c431fd55Sdan } 1599c431fd55Sdan #endif 1600c45e6716Sdrh Tcl_SetObjResult(pDb->interp, 1601c45e6716Sdrh Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 16024a4c11aaSdan return TCL_ERROR; 16034a4c11aaSdan }else{ 16044a4c11aaSdan dbReleaseStmt(pDb, pPreStmt, 0); 16054a4c11aaSdan } 16064a4c11aaSdan } 16074a4c11aaSdan } 16084a4c11aaSdan 16094a4c11aaSdan /* Finished */ 16104a4c11aaSdan return TCL_BREAK; 16114a4c11aaSdan } 16124a4c11aaSdan 16134a4c11aaSdan /* 16144a4c11aaSdan ** Free all resources currently held by the DbEvalContext structure passed 16154a4c11aaSdan ** as the first argument. There should be exactly one call to this function 16164a4c11aaSdan ** for each call to dbEvalInit(). 16174a4c11aaSdan */ 16184a4c11aaSdan static void dbEvalFinalize(DbEvalContext *p){ 16194a4c11aaSdan if( p->pPreStmt ){ 16204a4c11aaSdan sqlite3_reset(p->pPreStmt->pStmt); 16214a4c11aaSdan dbReleaseStmt(p->pDb, p->pPreStmt, 0); 16224a4c11aaSdan p->pPreStmt = 0; 16234a4c11aaSdan } 16244a4c11aaSdan if( p->pArray ){ 16254a4c11aaSdan Tcl_DecrRefCount(p->pArray); 16264a4c11aaSdan p->pArray = 0; 16274a4c11aaSdan } 16284a4c11aaSdan Tcl_DecrRefCount(p->pSql); 16294a4c11aaSdan dbReleaseColumnNames(p); 16304a4c11aaSdan } 16314a4c11aaSdan 16324a4c11aaSdan /* 16334a4c11aaSdan ** Return a pointer to a Tcl_Obj structure with ref-count 0 that contains 16344a4c11aaSdan ** the value for the iCol'th column of the row currently pointed to by 16354a4c11aaSdan ** the DbEvalContext structure passed as the first argument. 16364a4c11aaSdan */ 16374a4c11aaSdan static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){ 16384a4c11aaSdan sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 16394a4c11aaSdan switch( sqlite3_column_type(pStmt, iCol) ){ 16404a4c11aaSdan case SQLITE_BLOB: { 16414a4c11aaSdan int bytes = sqlite3_column_bytes(pStmt, iCol); 16424a4c11aaSdan const char *zBlob = sqlite3_column_blob(pStmt, iCol); 16434a4c11aaSdan if( !zBlob ) bytes = 0; 16444a4c11aaSdan return Tcl_NewByteArrayObj((u8*)zBlob, bytes); 16454a4c11aaSdan } 16464a4c11aaSdan case SQLITE_INTEGER: { 16474a4c11aaSdan sqlite_int64 v = sqlite3_column_int64(pStmt, iCol); 16484a4c11aaSdan if( v>=-2147483647 && v<=2147483647 ){ 16497fd33929Sdrh return Tcl_NewIntObj((int)v); 16504a4c11aaSdan }else{ 16514a4c11aaSdan return Tcl_NewWideIntObj(v); 16524a4c11aaSdan } 16534a4c11aaSdan } 16544a4c11aaSdan case SQLITE_FLOAT: { 16554a4c11aaSdan return Tcl_NewDoubleObj(sqlite3_column_double(pStmt, iCol)); 16564a4c11aaSdan } 16574a4c11aaSdan case SQLITE_NULL: { 1658c45e6716Sdrh return Tcl_NewStringObj(p->pDb->zNull, -1); 16594a4c11aaSdan } 16604a4c11aaSdan } 16614a4c11aaSdan 1662325eff58Sdrh return Tcl_NewStringObj((char*)sqlite3_column_text(pStmt, iCol), -1); 16634a4c11aaSdan } 16644a4c11aaSdan 16654a4c11aaSdan /* 16664a4c11aaSdan ** If using Tcl version 8.6 or greater, use the NR functions to avoid 16674a4c11aaSdan ** recursive evalution of scripts by the [db eval] and [db trans] 16684a4c11aaSdan ** commands. Even if the headers used while compiling the extension 16694a4c11aaSdan ** are 8.6 or newer, the code still tests the Tcl version at runtime. 16704a4c11aaSdan ** This allows stubs-enabled builds to be used with older Tcl libraries. 16714a4c11aaSdan */ 16724a4c11aaSdan #if TCL_MAJOR_VERSION>8 || (TCL_MAJOR_VERSION==8 && TCL_MINOR_VERSION>=6) 1673a2c8a95bSdrh # define SQLITE_TCL_NRE 1 16744a4c11aaSdan static int DbUseNre(void){ 16754a4c11aaSdan int major, minor; 16764a4c11aaSdan Tcl_GetVersion(&major, &minor, 0, 0); 16774a4c11aaSdan return( (major==8 && minor>=6) || major>8 ); 16784a4c11aaSdan } 16794a4c11aaSdan #else 16804a4c11aaSdan /* 16814a4c11aaSdan ** Compiling using headers earlier than 8.6. In this case NR cannot be 16824a4c11aaSdan ** used, so DbUseNre() to always return zero. Add #defines for the other 16834a4c11aaSdan ** Tcl_NRxxx() functions to prevent them from causing compilation errors, 16844a4c11aaSdan ** even though the only invocations of them are within conditional blocks 16854a4c11aaSdan ** of the form: 16864a4c11aaSdan ** 16874a4c11aaSdan ** if( DbUseNre() ) { ... } 16884a4c11aaSdan */ 1689a2c8a95bSdrh # define SQLITE_TCL_NRE 0 16904a4c11aaSdan # define DbUseNre() 0 1691a47941feSdrh # define Tcl_NRAddCallback(a,b,c,d,e,f) (void)0 16924a4c11aaSdan # define Tcl_NREvalObj(a,b,c) 0 1693a47941feSdrh # define Tcl_NRCreateCommand(a,b,c,d,e,f) (void)0 16944a4c11aaSdan #endif 16954a4c11aaSdan 16964a4c11aaSdan /* 16974a4c11aaSdan ** This function is part of the implementation of the command: 16984a4c11aaSdan ** 16994a4c11aaSdan ** $db eval SQL ?ARRAYNAME? SCRIPT 17004a4c11aaSdan */ 1701a121cc7cSmistachkin static int SQLITE_TCLAPI DbEvalNextCmd( 17024a4c11aaSdan ClientData data[], /* data[0] is the (DbEvalContext*) */ 17034a4c11aaSdan Tcl_Interp *interp, /* Tcl interpreter */ 17044a4c11aaSdan int result /* Result so far */ 17054a4c11aaSdan ){ 17064a4c11aaSdan int rc = result; /* Return code */ 17074a4c11aaSdan 17084a4c11aaSdan /* The first element of the data[] array is a pointer to a DbEvalContext 17094a4c11aaSdan ** structure allocated using Tcl_Alloc(). The second element of data[] 17104a4c11aaSdan ** is a pointer to a Tcl_Obj containing the script to run for each row 17114a4c11aaSdan ** returned by the queries encapsulated in data[0]. */ 17124a4c11aaSdan DbEvalContext *p = (DbEvalContext *)data[0]; 17134a4c11aaSdan Tcl_Obj *pScript = (Tcl_Obj *)data[1]; 17144a4c11aaSdan Tcl_Obj *pArray = p->pArray; 17154a4c11aaSdan 17164a4c11aaSdan while( (rc==TCL_OK || rc==TCL_CONTINUE) && TCL_OK==(rc = dbEvalStep(p)) ){ 17174a4c11aaSdan int i; 17184a4c11aaSdan int nCol; 17194a4c11aaSdan Tcl_Obj **apColName; 17204a4c11aaSdan dbEvalRowInfo(p, &nCol, &apColName); 17214a4c11aaSdan for(i=0; i<nCol; i++){ 17224a4c11aaSdan Tcl_Obj *pVal = dbEvalColumnValue(p, i); 17234a4c11aaSdan if( pArray==0 ){ 17244a4c11aaSdan Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0); 17254a4c11aaSdan }else{ 17264a4c11aaSdan Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); 17274a4c11aaSdan } 17284a4c11aaSdan } 17294a4c11aaSdan 17304a4c11aaSdan /* The required interpreter variables are now populated with the data 17314a4c11aaSdan ** from the current row. If using NRE, schedule callbacks to evaluate 17324a4c11aaSdan ** script pScript, then to invoke this function again to fetch the next 17334a4c11aaSdan ** row (or clean up if there is no next row or the script throws an 17344a4c11aaSdan ** exception). After scheduling the callbacks, return control to the 17354a4c11aaSdan ** caller. 17364a4c11aaSdan ** 17374a4c11aaSdan ** If not using NRE, evaluate pScript directly and continue with the 17384a4c11aaSdan ** next iteration of this while(...) loop. */ 17394a4c11aaSdan if( DbUseNre() ){ 17404a4c11aaSdan Tcl_NRAddCallback(interp, DbEvalNextCmd, (void*)p, (void*)pScript, 0, 0); 17414a4c11aaSdan return Tcl_NREvalObj(interp, pScript, 0); 17424a4c11aaSdan }else{ 17434a4c11aaSdan rc = Tcl_EvalObjEx(interp, pScript, 0); 17444a4c11aaSdan } 17454a4c11aaSdan } 17464a4c11aaSdan 17474a4c11aaSdan Tcl_DecrRefCount(pScript); 17484a4c11aaSdan dbEvalFinalize(p); 17494a4c11aaSdan Tcl_Free((char *)p); 17504a4c11aaSdan 17514a4c11aaSdan if( rc==TCL_OK || rc==TCL_BREAK ){ 17524a4c11aaSdan Tcl_ResetResult(interp); 17534a4c11aaSdan rc = TCL_OK; 17544a4c11aaSdan } 17554a4c11aaSdan return rc; 17568e556520Sdanielk1977 } 17578e556520Sdanielk1977 17581067fe11Stpoindex /* 175946c47d46Sdan ** This function is used by the implementations of the following database 176046c47d46Sdan ** handle sub-commands: 176146c47d46Sdan ** 176246c47d46Sdan ** $db update_hook ?SCRIPT? 176346c47d46Sdan ** $db wal_hook ?SCRIPT? 176446c47d46Sdan ** $db commit_hook ?SCRIPT? 176546c47d46Sdan ** $db preupdate hook ?SCRIPT? 176646c47d46Sdan */ 176746c47d46Sdan static void DbHookCmd( 176846c47d46Sdan Tcl_Interp *interp, /* Tcl interpreter */ 176946c47d46Sdan SqliteDb *pDb, /* Database handle */ 177046c47d46Sdan Tcl_Obj *pArg, /* SCRIPT argument (or NULL) */ 177146c47d46Sdan Tcl_Obj **ppHook /* Pointer to member of SqliteDb */ 177246c47d46Sdan ){ 177346c47d46Sdan sqlite3 *db = pDb->db; 177446c47d46Sdan 177546c47d46Sdan if( *ppHook ){ 177646c47d46Sdan Tcl_SetObjResult(interp, *ppHook); 177746c47d46Sdan if( pArg ){ 177846c47d46Sdan Tcl_DecrRefCount(*ppHook); 177946c47d46Sdan *ppHook = 0; 178046c47d46Sdan } 178146c47d46Sdan } 178246c47d46Sdan if( pArg ){ 178346c47d46Sdan assert( !(*ppHook) ); 178446c47d46Sdan if( Tcl_GetCharLength(pArg)>0 ){ 178546c47d46Sdan *ppHook = pArg; 178646c47d46Sdan Tcl_IncrRefCount(*ppHook); 178746c47d46Sdan } 178846c47d46Sdan } 178946c47d46Sdan 17909b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 179146c47d46Sdan sqlite3_preupdate_hook(db, (pDb->pPreUpdateHook?DbPreUpdateHandler:0), pDb); 17929b1c62d4Sdrh #endif 179346c47d46Sdan sqlite3_update_hook(db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); 179446c47d46Sdan sqlite3_rollback_hook(db, (pDb->pRollbackHook?DbRollbackHandler:0), pDb); 179546c47d46Sdan sqlite3_wal_hook(db, (pDb->pWalHook?DbWalHandler:0), pDb); 179646c47d46Sdan } 179746c47d46Sdan 179846c47d46Sdan /* 179975897234Sdrh ** The "sqlite" command below creates a new Tcl command for each 180075897234Sdrh ** connection it opens to an SQLite database. This routine is invoked 180175897234Sdrh ** whenever one of those connection-specific commands is executed 180275897234Sdrh ** in Tcl. For example, if you run Tcl code like this: 180375897234Sdrh ** 18049bb575fdSdrh ** sqlite3 db1 "my_database" 180575897234Sdrh ** db1 close 180675897234Sdrh ** 180775897234Sdrh ** The first command opens a connection to the "my_database" database 180875897234Sdrh ** and calls that connection "db1". The second command causes this 180975897234Sdrh ** subroutine to be invoked. 181075897234Sdrh */ 18117617e4a8Smistachkin static int SQLITE_TCLAPI DbObjCmd( 18127617e4a8Smistachkin void *cd, 18137617e4a8Smistachkin Tcl_Interp *interp, 18147617e4a8Smistachkin int objc, 18157617e4a8Smistachkin Tcl_Obj *const*objv 18167617e4a8Smistachkin ){ 1817bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 18186d31316cSdrh int choice; 181922fbcb8dSdrh int rc = TCL_OK; 18200de8c112Sdrh static const char *DB_strs[] = { 1821dc2c4915Sdrh "authorizer", "backup", "busy", 1822dc2c4915Sdrh "cache", "changes", "close", 1823dc2c4915Sdrh "collate", "collation_needed", "commit_hook", 1824dc2c4915Sdrh "complete", "copy", "enable_load_extension", 1825dc2c4915Sdrh "errorcode", "eval", "exists", 1826dc2c4915Sdrh "function", "incrblob", "interrupt", 1827833bf968Sdrh "last_insert_rowid", "nullvalue", "onecolumn", 1828304637c0Sdrh "preupdate", "profile", "progress", 1829304637c0Sdrh "rekey", "restore", "rollback_hook", 1830304637c0Sdrh "status", "timeout", "total_changes", 1831b56660f5Smistachkin "trace", "trace_v2", "transaction", 1832b56660f5Smistachkin "unlock_notify", "update_hook", "version", 1833b56660f5Smistachkin "wal_hook", 1834304637c0Sdrh 0 18356d31316cSdrh }; 1836411995dcSdrh enum DB_enum { 1837dc2c4915Sdrh DB_AUTHORIZER, DB_BACKUP, DB_BUSY, 1838dc2c4915Sdrh DB_CACHE, DB_CHANGES, DB_CLOSE, 1839dc2c4915Sdrh DB_COLLATE, DB_COLLATION_NEEDED, DB_COMMIT_HOOK, 1840dc2c4915Sdrh DB_COMPLETE, DB_COPY, DB_ENABLE_LOAD_EXTENSION, 1841dc2c4915Sdrh DB_ERRORCODE, DB_EVAL, DB_EXISTS, 1842dc2c4915Sdrh DB_FUNCTION, DB_INCRBLOB, DB_INTERRUPT, 1843833bf968Sdrh DB_LAST_INSERT_ROWID, DB_NULLVALUE, DB_ONECOLUMN, 1844304637c0Sdrh DB_PREUPDATE, DB_PROFILE, DB_PROGRESS, 1845304637c0Sdrh DB_REKEY, DB_RESTORE, DB_ROLLBACK_HOOK, 1846304637c0Sdrh DB_STATUS, DB_TIMEOUT, DB_TOTAL_CHANGES, 1847b56660f5Smistachkin DB_TRACE, DB_TRACE_V2, DB_TRANSACTION, 1848b56660f5Smistachkin DB_UNLOCK_NOTIFY, DB_UPDATE_HOOK, DB_VERSION, 1849b56660f5Smistachkin DB_WAL_HOOK, 18506d31316cSdrh }; 18511067fe11Stpoindex /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */ 18526d31316cSdrh 18536d31316cSdrh if( objc<2 ){ 18546d31316cSdrh Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); 185575897234Sdrh return TCL_ERROR; 185675897234Sdrh } 1857411995dcSdrh if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ 18586d31316cSdrh return TCL_ERROR; 18596d31316cSdrh } 18606d31316cSdrh 1861411995dcSdrh switch( (enum DB_enum)choice ){ 186275897234Sdrh 1863e22a334bSdrh /* $db authorizer ?CALLBACK? 1864e22a334bSdrh ** 1865e22a334bSdrh ** Invoke the given callback to authorize each SQL operation as it is 1866e22a334bSdrh ** compiled. 5 arguments are appended to the callback before it is 1867e22a334bSdrh ** invoked: 1868e22a334bSdrh ** 1869e22a334bSdrh ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) 1870e22a334bSdrh ** (2) First descriptive name (depends on authorization type) 1871e22a334bSdrh ** (3) Second descriptive name 1872e22a334bSdrh ** (4) Name of the database (ex: "main", "temp") 1873e22a334bSdrh ** (5) Name of trigger that is doing the access 1874e22a334bSdrh ** 1875e22a334bSdrh ** The callback should return on of the following strings: SQLITE_OK, 1876e22a334bSdrh ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. 1877e22a334bSdrh ** 1878e22a334bSdrh ** If this method is invoked with no arguments, the current authorization 1879e22a334bSdrh ** callback string is returned. 1880e22a334bSdrh */ 1881e22a334bSdrh case DB_AUTHORIZER: { 18821211de37Sdrh #ifdef SQLITE_OMIT_AUTHORIZATION 1883a198f2b5Sdrh Tcl_AppendResult(interp, "authorization not available in this build", 1884a198f2b5Sdrh (char*)0); 18851211de37Sdrh return TCL_ERROR; 18861211de37Sdrh #else 1887e22a334bSdrh if( objc>3 ){ 1888e22a334bSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 18890f14e2ebSdrh return TCL_ERROR; 1890e22a334bSdrh }else if( objc==2 ){ 1891b5a20d3cSdrh if( pDb->zAuth ){ 1892a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zAuth, (char*)0); 1893e22a334bSdrh } 1894e22a334bSdrh }else{ 1895e22a334bSdrh char *zAuth; 1896e22a334bSdrh int len; 1897e22a334bSdrh if( pDb->zAuth ){ 1898e22a334bSdrh Tcl_Free(pDb->zAuth); 1899e22a334bSdrh } 1900e22a334bSdrh zAuth = Tcl_GetStringFromObj(objv[2], &len); 1901e22a334bSdrh if( zAuth && len>0 ){ 1902e22a334bSdrh pDb->zAuth = Tcl_Alloc( len + 1 ); 19035bb3eb9bSdrh memcpy(pDb->zAuth, zAuth, len+1); 1904e22a334bSdrh }else{ 1905e22a334bSdrh pDb->zAuth = 0; 1906e22a334bSdrh } 1907e22a334bSdrh if( pDb->zAuth ){ 190832c6a48bSdrh typedef int (*sqlite3_auth_cb)( 190932c6a48bSdrh void*,int,const char*,const char*, 191032c6a48bSdrh const char*,const char*); 1911e22a334bSdrh pDb->interp = interp; 191232c6a48bSdrh sqlite3_set_authorizer(pDb->db,(sqlite3_auth_cb)auth_callback,pDb); 1913e22a334bSdrh }else{ 19146f8a503dSdanielk1977 sqlite3_set_authorizer(pDb->db, 0, 0); 1915e22a334bSdrh } 1916e22a334bSdrh } 19171211de37Sdrh #endif 1918e22a334bSdrh break; 1919e22a334bSdrh } 1920e22a334bSdrh 1921dc2c4915Sdrh /* $db backup ?DATABASE? FILENAME 1922dc2c4915Sdrh ** 1923dc2c4915Sdrh ** Open or create a database file named FILENAME. Transfer the 1924dc2c4915Sdrh ** content of local database DATABASE (default: "main") into the 1925dc2c4915Sdrh ** FILENAME database. 1926dc2c4915Sdrh */ 1927dc2c4915Sdrh case DB_BACKUP: { 1928dc2c4915Sdrh const char *zDestFile; 1929dc2c4915Sdrh const char *zSrcDb; 1930dc2c4915Sdrh sqlite3 *pDest; 1931dc2c4915Sdrh sqlite3_backup *pBackup; 1932dc2c4915Sdrh 1933dc2c4915Sdrh if( objc==3 ){ 1934dc2c4915Sdrh zSrcDb = "main"; 1935dc2c4915Sdrh zDestFile = Tcl_GetString(objv[2]); 1936dc2c4915Sdrh }else if( objc==4 ){ 1937dc2c4915Sdrh zSrcDb = Tcl_GetString(objv[2]); 1938dc2c4915Sdrh zDestFile = Tcl_GetString(objv[3]); 1939dc2c4915Sdrh }else{ 1940dc2c4915Sdrh Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 1941dc2c4915Sdrh return TCL_ERROR; 1942dc2c4915Sdrh } 1943147ef394Sdrh rc = sqlite3_open_v2(zDestFile, &pDest, 1944147ef394Sdrh SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE| pDb->openFlags, 0); 1945dc2c4915Sdrh if( rc!=SQLITE_OK ){ 1946dc2c4915Sdrh Tcl_AppendResult(interp, "cannot open target database: ", 1947dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1948dc2c4915Sdrh sqlite3_close(pDest); 1949dc2c4915Sdrh return TCL_ERROR; 1950dc2c4915Sdrh } 1951dc2c4915Sdrh pBackup = sqlite3_backup_init(pDest, "main", pDb->db, zSrcDb); 1952dc2c4915Sdrh if( pBackup==0 ){ 1953dc2c4915Sdrh Tcl_AppendResult(interp, "backup failed: ", 1954dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1955dc2c4915Sdrh sqlite3_close(pDest); 1956dc2c4915Sdrh return TCL_ERROR; 1957dc2c4915Sdrh } 1958dc2c4915Sdrh while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} 1959dc2c4915Sdrh sqlite3_backup_finish(pBackup); 1960dc2c4915Sdrh if( rc==SQLITE_DONE ){ 1961dc2c4915Sdrh rc = TCL_OK; 1962dc2c4915Sdrh }else{ 1963dc2c4915Sdrh Tcl_AppendResult(interp, "backup failed: ", 1964dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1965dc2c4915Sdrh rc = TCL_ERROR; 1966dc2c4915Sdrh } 1967dc2c4915Sdrh sqlite3_close(pDest); 1968dc2c4915Sdrh break; 1969dc2c4915Sdrh } 1970dc2c4915Sdrh 1971bec3f402Sdrh /* $db busy ?CALLBACK? 1972bec3f402Sdrh ** 1973bec3f402Sdrh ** Invoke the given callback if an SQL statement attempts to open 1974bec3f402Sdrh ** a locked database file. 1975bec3f402Sdrh */ 19766d31316cSdrh case DB_BUSY: { 19776d31316cSdrh if( objc>3 ){ 19786d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); 1979bec3f402Sdrh return TCL_ERROR; 19806d31316cSdrh }else if( objc==2 ){ 1981bec3f402Sdrh if( pDb->zBusy ){ 1982a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zBusy, (char*)0); 1983bec3f402Sdrh } 1984bec3f402Sdrh }else{ 19856d31316cSdrh char *zBusy; 19866d31316cSdrh int len; 1987bec3f402Sdrh if( pDb->zBusy ){ 1988bec3f402Sdrh Tcl_Free(pDb->zBusy); 19896d31316cSdrh } 19906d31316cSdrh zBusy = Tcl_GetStringFromObj(objv[2], &len); 19916d31316cSdrh if( zBusy && len>0 ){ 19926d31316cSdrh pDb->zBusy = Tcl_Alloc( len + 1 ); 19935bb3eb9bSdrh memcpy(pDb->zBusy, zBusy, len+1); 19946d31316cSdrh }else{ 1995bec3f402Sdrh pDb->zBusy = 0; 1996bec3f402Sdrh } 1997bec3f402Sdrh if( pDb->zBusy ){ 1998bec3f402Sdrh pDb->interp = interp; 19996f8a503dSdanielk1977 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb); 20006d31316cSdrh }else{ 20016f8a503dSdanielk1977 sqlite3_busy_handler(pDb->db, 0, 0); 2002bec3f402Sdrh } 2003bec3f402Sdrh } 20046d31316cSdrh break; 20056d31316cSdrh } 2006bec3f402Sdrh 2007fb7e7651Sdrh /* $db cache flush 2008fb7e7651Sdrh ** $db cache size n 2009fb7e7651Sdrh ** 2010fb7e7651Sdrh ** Flush the prepared statement cache, or set the maximum number of 2011fb7e7651Sdrh ** cached statements. 2012fb7e7651Sdrh */ 2013fb7e7651Sdrh case DB_CACHE: { 2014fb7e7651Sdrh char *subCmd; 2015fb7e7651Sdrh int n; 2016fb7e7651Sdrh 2017fb7e7651Sdrh if( objc<=2 ){ 2018fb7e7651Sdrh Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?"); 2019fb7e7651Sdrh return TCL_ERROR; 2020fb7e7651Sdrh } 2021fb7e7651Sdrh subCmd = Tcl_GetStringFromObj( objv[2], 0 ); 2022fb7e7651Sdrh if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){ 2023fb7e7651Sdrh if( objc!=3 ){ 2024fb7e7651Sdrh Tcl_WrongNumArgs(interp, 2, objv, "flush"); 2025fb7e7651Sdrh return TCL_ERROR; 2026fb7e7651Sdrh }else{ 2027fb7e7651Sdrh flushStmtCache( pDb ); 2028fb7e7651Sdrh } 2029fb7e7651Sdrh }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){ 2030fb7e7651Sdrh if( objc!=4 ){ 2031fb7e7651Sdrh Tcl_WrongNumArgs(interp, 2, objv, "size n"); 2032fb7e7651Sdrh return TCL_ERROR; 2033fb7e7651Sdrh }else{ 2034fb7e7651Sdrh if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){ 2035fb7e7651Sdrh Tcl_AppendResult( interp, "cannot convert \"", 2036a198f2b5Sdrh Tcl_GetStringFromObj(objv[3],0), "\" to integer", (char*)0); 2037fb7e7651Sdrh return TCL_ERROR; 2038fb7e7651Sdrh }else{ 2039fb7e7651Sdrh if( n<0 ){ 2040fb7e7651Sdrh flushStmtCache( pDb ); 2041fb7e7651Sdrh n = 0; 2042fb7e7651Sdrh }else if( n>MAX_PREPARED_STMTS ){ 2043fb7e7651Sdrh n = MAX_PREPARED_STMTS; 2044fb7e7651Sdrh } 2045fb7e7651Sdrh pDb->maxStmt = n; 2046fb7e7651Sdrh } 2047fb7e7651Sdrh } 2048fb7e7651Sdrh }else{ 2049fb7e7651Sdrh Tcl_AppendResult( interp, "bad option \"", 2050a198f2b5Sdrh Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size", 2051a198f2b5Sdrh (char*)0); 2052fb7e7651Sdrh return TCL_ERROR; 2053fb7e7651Sdrh } 2054fb7e7651Sdrh break; 2055fb7e7651Sdrh } 2056fb7e7651Sdrh 2057b28af71aSdanielk1977 /* $db changes 2058c8d30ac1Sdrh ** 2059c8d30ac1Sdrh ** Return the number of rows that were modified, inserted, or deleted by 2060b28af71aSdanielk1977 ** the most recent INSERT, UPDATE or DELETE statement, not including 2061b28af71aSdanielk1977 ** any changes made by trigger programs. 2062c8d30ac1Sdrh */ 2063c8d30ac1Sdrh case DB_CHANGES: { 2064c8d30ac1Sdrh Tcl_Obj *pResult; 2065c8d30ac1Sdrh if( objc!=2 ){ 2066c8d30ac1Sdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 2067c8d30ac1Sdrh return TCL_ERROR; 2068c8d30ac1Sdrh } 2069c8d30ac1Sdrh pResult = Tcl_GetObjResult(interp); 2070b28af71aSdanielk1977 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db)); 2071f146a776Srdc break; 2072f146a776Srdc } 2073f146a776Srdc 207475897234Sdrh /* $db close 207575897234Sdrh ** 207675897234Sdrh ** Shutdown the database 207775897234Sdrh */ 20786d31316cSdrh case DB_CLOSE: { 20796d31316cSdrh Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); 20806d31316cSdrh break; 20816d31316cSdrh } 208275897234Sdrh 20830f14e2ebSdrh /* 20840f14e2ebSdrh ** $db collate NAME SCRIPT 20850f14e2ebSdrh ** 20860f14e2ebSdrh ** Create a new SQL collation function called NAME. Whenever 20870f14e2ebSdrh ** that function is called, invoke SCRIPT to evaluate the function. 20880f14e2ebSdrh */ 20890f14e2ebSdrh case DB_COLLATE: { 20900f14e2ebSdrh SqlCollate *pCollate; 20910f14e2ebSdrh char *zName; 20920f14e2ebSdrh char *zScript; 20930f14e2ebSdrh int nScript; 20940f14e2ebSdrh if( objc!=4 ){ 20950f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); 20960f14e2ebSdrh return TCL_ERROR; 20970f14e2ebSdrh } 20980f14e2ebSdrh zName = Tcl_GetStringFromObj(objv[2], 0); 20990f14e2ebSdrh zScript = Tcl_GetStringFromObj(objv[3], &nScript); 21000f14e2ebSdrh pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 ); 21010f14e2ebSdrh if( pCollate==0 ) return TCL_ERROR; 21020f14e2ebSdrh pCollate->interp = interp; 21030f14e2ebSdrh pCollate->pNext = pDb->pCollate; 21040f14e2ebSdrh pCollate->zScript = (char*)&pCollate[1]; 21050f14e2ebSdrh pDb->pCollate = pCollate; 21065bb3eb9bSdrh memcpy(pCollate->zScript, zScript, nScript+1); 21070f14e2ebSdrh if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, 21080f14e2ebSdrh pCollate, tclSqlCollate) ){ 21099636c4e1Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 21100f14e2ebSdrh return TCL_ERROR; 21110f14e2ebSdrh } 21120f14e2ebSdrh break; 21130f14e2ebSdrh } 21140f14e2ebSdrh 21150f14e2ebSdrh /* 21160f14e2ebSdrh ** $db collation_needed SCRIPT 21170f14e2ebSdrh ** 21180f14e2ebSdrh ** Create a new SQL collation function called NAME. Whenever 21190f14e2ebSdrh ** that function is called, invoke SCRIPT to evaluate the function. 21200f14e2ebSdrh */ 21210f14e2ebSdrh case DB_COLLATION_NEEDED: { 21220f14e2ebSdrh if( objc!=3 ){ 21230f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT"); 21240f14e2ebSdrh return TCL_ERROR; 21250f14e2ebSdrh } 21260f14e2ebSdrh if( pDb->pCollateNeeded ){ 21270f14e2ebSdrh Tcl_DecrRefCount(pDb->pCollateNeeded); 21280f14e2ebSdrh } 21290f14e2ebSdrh pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]); 21300f14e2ebSdrh Tcl_IncrRefCount(pDb->pCollateNeeded); 21310f14e2ebSdrh sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded); 21320f14e2ebSdrh break; 21330f14e2ebSdrh } 21340f14e2ebSdrh 213519e2d37fSdrh /* $db commit_hook ?CALLBACK? 213619e2d37fSdrh ** 213719e2d37fSdrh ** Invoke the given callback just before committing every SQL transaction. 213819e2d37fSdrh ** If the callback throws an exception or returns non-zero, then the 213919e2d37fSdrh ** transaction is aborted. If CALLBACK is an empty string, the callback 214019e2d37fSdrh ** is disabled. 214119e2d37fSdrh */ 214219e2d37fSdrh case DB_COMMIT_HOOK: { 214319e2d37fSdrh if( objc>3 ){ 214419e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 214519e2d37fSdrh return TCL_ERROR; 214619e2d37fSdrh }else if( objc==2 ){ 214719e2d37fSdrh if( pDb->zCommit ){ 2148a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zCommit, (char*)0); 214919e2d37fSdrh } 215019e2d37fSdrh }else{ 21516ef5e12eSmistachkin const char *zCommit; 215219e2d37fSdrh int len; 215319e2d37fSdrh if( pDb->zCommit ){ 215419e2d37fSdrh Tcl_Free(pDb->zCommit); 215519e2d37fSdrh } 215619e2d37fSdrh zCommit = Tcl_GetStringFromObj(objv[2], &len); 215719e2d37fSdrh if( zCommit && len>0 ){ 215819e2d37fSdrh pDb->zCommit = Tcl_Alloc( len + 1 ); 21595bb3eb9bSdrh memcpy(pDb->zCommit, zCommit, len+1); 216019e2d37fSdrh }else{ 216119e2d37fSdrh pDb->zCommit = 0; 216219e2d37fSdrh } 216319e2d37fSdrh if( pDb->zCommit ){ 216419e2d37fSdrh pDb->interp = interp; 216519e2d37fSdrh sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb); 216619e2d37fSdrh }else{ 216719e2d37fSdrh sqlite3_commit_hook(pDb->db, 0, 0); 216819e2d37fSdrh } 216919e2d37fSdrh } 217019e2d37fSdrh break; 217119e2d37fSdrh } 217219e2d37fSdrh 217375897234Sdrh /* $db complete SQL 217475897234Sdrh ** 217575897234Sdrh ** Return TRUE if SQL is a complete SQL statement. Return FALSE if 217675897234Sdrh ** additional lines of input are needed. This is similar to the 217775897234Sdrh ** built-in "info complete" command of Tcl. 217875897234Sdrh */ 21796d31316cSdrh case DB_COMPLETE: { 2180ccae6026Sdrh #ifndef SQLITE_OMIT_COMPLETE 21816d31316cSdrh Tcl_Obj *pResult; 21826d31316cSdrh int isComplete; 21836d31316cSdrh if( objc!=3 ){ 21846d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 218575897234Sdrh return TCL_ERROR; 218675897234Sdrh } 21876f8a503dSdanielk1977 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) ); 21886d31316cSdrh pResult = Tcl_GetObjResult(interp); 21896d31316cSdrh Tcl_SetBooleanObj(pResult, isComplete); 2190ccae6026Sdrh #endif 21916d31316cSdrh break; 21926d31316cSdrh } 219375897234Sdrh 219419e2d37fSdrh /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR? 219519e2d37fSdrh ** 219619e2d37fSdrh ** Copy data into table from filename, optionally using SEPARATOR 219719e2d37fSdrh ** as column separators. If a column contains a null string, or the 219819e2d37fSdrh ** value of NULLINDICATOR, a NULL is inserted for the column. 219919e2d37fSdrh ** conflict-algorithm is one of the sqlite conflict algorithms: 220019e2d37fSdrh ** rollback, abort, fail, ignore, replace 220119e2d37fSdrh ** On success, return the number of lines processed, not necessarily same 220219e2d37fSdrh ** as 'db changes' due to conflict-algorithm selected. 220319e2d37fSdrh ** 220419e2d37fSdrh ** This code is basically an implementation/enhancement of 220519e2d37fSdrh ** the sqlite3 shell.c ".import" command. 220619e2d37fSdrh ** 220719e2d37fSdrh ** This command usage is equivalent to the sqlite2.x COPY statement, 220819e2d37fSdrh ** which imports file data into a table using the PostgreSQL COPY file format: 220919e2d37fSdrh ** $db copy $conflit_algo $table_name $filename \t \\N 221019e2d37fSdrh */ 221119e2d37fSdrh case DB_COPY: { 221219e2d37fSdrh char *zTable; /* Insert data into this table */ 221319e2d37fSdrh char *zFile; /* The file from which to extract data */ 221419e2d37fSdrh char *zConflict; /* The conflict algorithm to use */ 221519e2d37fSdrh sqlite3_stmt *pStmt; /* A statement */ 221619e2d37fSdrh int nCol; /* Number of columns in the table */ 221719e2d37fSdrh int nByte; /* Number of bytes in an SQL string */ 221819e2d37fSdrh int i, j; /* Loop counters */ 221919e2d37fSdrh int nSep; /* Number of bytes in zSep[] */ 222019e2d37fSdrh int nNull; /* Number of bytes in zNull[] */ 222119e2d37fSdrh char *zSql; /* An SQL statement */ 222219e2d37fSdrh char *zLine; /* A single line of input from the file */ 222319e2d37fSdrh char **azCol; /* zLine[] broken up into columns */ 22246ef5e12eSmistachkin const char *zCommit; /* How to commit changes */ 222519e2d37fSdrh FILE *in; /* The input file */ 222619e2d37fSdrh int lineno = 0; /* Line number of input file */ 222719e2d37fSdrh char zLineNum[80]; /* Line number print buffer */ 222819e2d37fSdrh Tcl_Obj *pResult; /* interp result */ 222919e2d37fSdrh 22306ef5e12eSmistachkin const char *zSep; 22316ef5e12eSmistachkin const char *zNull; 223219e2d37fSdrh if( objc<5 || objc>7 ){ 223319e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, 223419e2d37fSdrh "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"); 223519e2d37fSdrh return TCL_ERROR; 223619e2d37fSdrh } 223719e2d37fSdrh if( objc>=6 ){ 223819e2d37fSdrh zSep = Tcl_GetStringFromObj(objv[5], 0); 223919e2d37fSdrh }else{ 224019e2d37fSdrh zSep = "\t"; 224119e2d37fSdrh } 224219e2d37fSdrh if( objc>=7 ){ 224319e2d37fSdrh zNull = Tcl_GetStringFromObj(objv[6], 0); 224419e2d37fSdrh }else{ 224519e2d37fSdrh zNull = ""; 224619e2d37fSdrh } 224719e2d37fSdrh zConflict = Tcl_GetStringFromObj(objv[2], 0); 224819e2d37fSdrh zTable = Tcl_GetStringFromObj(objv[3], 0); 224919e2d37fSdrh zFile = Tcl_GetStringFromObj(objv[4], 0); 22504f21c4afSdrh nSep = strlen30(zSep); 22514f21c4afSdrh nNull = strlen30(zNull); 225219e2d37fSdrh if( nSep==0 ){ 2253a198f2b5Sdrh Tcl_AppendResult(interp,"Error: non-null separator required for copy", 2254a198f2b5Sdrh (char*)0); 225519e2d37fSdrh return TCL_ERROR; 225619e2d37fSdrh } 22573e59c012Sdrh if(strcmp(zConflict, "rollback") != 0 && 22583e59c012Sdrh strcmp(zConflict, "abort" ) != 0 && 22593e59c012Sdrh strcmp(zConflict, "fail" ) != 0 && 22603e59c012Sdrh strcmp(zConflict, "ignore" ) != 0 && 22613e59c012Sdrh strcmp(zConflict, "replace" ) != 0 ) { 226219e2d37fSdrh Tcl_AppendResult(interp, "Error: \"", zConflict, 226319e2d37fSdrh "\", conflict-algorithm must be one of: rollback, " 2264a198f2b5Sdrh "abort, fail, ignore, or replace", (char*)0); 226519e2d37fSdrh return TCL_ERROR; 226619e2d37fSdrh } 226719e2d37fSdrh zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); 226819e2d37fSdrh if( zSql==0 ){ 2269a198f2b5Sdrh Tcl_AppendResult(interp, "Error: no such table: ", zTable, (char*)0); 227019e2d37fSdrh return TCL_ERROR; 227119e2d37fSdrh } 22724f21c4afSdrh nByte = strlen30(zSql); 22733e701a18Sdrh rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 227419e2d37fSdrh sqlite3_free(zSql); 227519e2d37fSdrh if( rc ){ 2276a198f2b5Sdrh Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0); 227719e2d37fSdrh nCol = 0; 227819e2d37fSdrh }else{ 227919e2d37fSdrh nCol = sqlite3_column_count(pStmt); 228019e2d37fSdrh } 228119e2d37fSdrh sqlite3_finalize(pStmt); 228219e2d37fSdrh if( nCol==0 ) { 228319e2d37fSdrh return TCL_ERROR; 228419e2d37fSdrh } 228519e2d37fSdrh zSql = malloc( nByte + 50 + nCol*2 ); 228619e2d37fSdrh if( zSql==0 ) { 2287a198f2b5Sdrh Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0); 228819e2d37fSdrh return TCL_ERROR; 228919e2d37fSdrh } 229019e2d37fSdrh sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?", 229119e2d37fSdrh zConflict, zTable); 22924f21c4afSdrh j = strlen30(zSql); 229319e2d37fSdrh for(i=1; i<nCol; i++){ 229419e2d37fSdrh zSql[j++] = ','; 229519e2d37fSdrh zSql[j++] = '?'; 229619e2d37fSdrh } 229719e2d37fSdrh zSql[j++] = ')'; 229819e2d37fSdrh zSql[j] = 0; 22993e701a18Sdrh rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 230019e2d37fSdrh free(zSql); 230119e2d37fSdrh if( rc ){ 2302a198f2b5Sdrh Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0); 230319e2d37fSdrh sqlite3_finalize(pStmt); 230419e2d37fSdrh return TCL_ERROR; 230519e2d37fSdrh } 230619e2d37fSdrh in = fopen(zFile, "rb"); 230719e2d37fSdrh if( in==0 ){ 230819e2d37fSdrh Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL); 230919e2d37fSdrh sqlite3_finalize(pStmt); 231019e2d37fSdrh return TCL_ERROR; 231119e2d37fSdrh } 231219e2d37fSdrh azCol = malloc( sizeof(azCol[0])*(nCol+1) ); 231319e2d37fSdrh if( azCol==0 ) { 2314a198f2b5Sdrh Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0); 231543617e9aSdrh fclose(in); 231619e2d37fSdrh return TCL_ERROR; 231719e2d37fSdrh } 23183752785fSdrh (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0); 231919e2d37fSdrh zCommit = "COMMIT"; 232019e2d37fSdrh while( (zLine = local_getline(0, in))!=0 ){ 232119e2d37fSdrh char *z; 232219e2d37fSdrh lineno++; 232319e2d37fSdrh azCol[0] = zLine; 232419e2d37fSdrh for(i=0, z=zLine; *z; z++){ 232519e2d37fSdrh if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){ 232619e2d37fSdrh *z = 0; 232719e2d37fSdrh i++; 232819e2d37fSdrh if( i<nCol ){ 232919e2d37fSdrh azCol[i] = &z[nSep]; 233019e2d37fSdrh z += nSep-1; 233119e2d37fSdrh } 233219e2d37fSdrh } 233319e2d37fSdrh } 233419e2d37fSdrh if( i+1!=nCol ){ 233519e2d37fSdrh char *zErr; 23364f21c4afSdrh int nErr = strlen30(zFile) + 200; 23375bb3eb9bSdrh zErr = malloc(nErr); 2338c1f4494eSdrh if( zErr ){ 23395bb3eb9bSdrh sqlite3_snprintf(nErr, zErr, 2340955de52cSdanielk1977 "Error: %s line %d: expected %d columns of data but found %d", 234119e2d37fSdrh zFile, lineno, nCol, i+1); 2342a198f2b5Sdrh Tcl_AppendResult(interp, zErr, (char*)0); 234319e2d37fSdrh free(zErr); 2344c1f4494eSdrh } 234519e2d37fSdrh zCommit = "ROLLBACK"; 234619e2d37fSdrh break; 234719e2d37fSdrh } 234819e2d37fSdrh for(i=0; i<nCol; i++){ 234919e2d37fSdrh /* check for null data, if so, bind as null */ 2350ea678832Sdrh if( (nNull>0 && strcmp(azCol[i], zNull)==0) 23514f21c4afSdrh || strlen30(azCol[i])==0 2352ea678832Sdrh ){ 235319e2d37fSdrh sqlite3_bind_null(pStmt, i+1); 235419e2d37fSdrh }else{ 235519e2d37fSdrh sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); 235619e2d37fSdrh } 235719e2d37fSdrh } 235819e2d37fSdrh sqlite3_step(pStmt); 235919e2d37fSdrh rc = sqlite3_reset(pStmt); 236019e2d37fSdrh free(zLine); 236119e2d37fSdrh if( rc!=SQLITE_OK ){ 2362a198f2b5Sdrh Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), (char*)0); 236319e2d37fSdrh zCommit = "ROLLBACK"; 236419e2d37fSdrh break; 236519e2d37fSdrh } 236619e2d37fSdrh } 236719e2d37fSdrh free(azCol); 236819e2d37fSdrh fclose(in); 236919e2d37fSdrh sqlite3_finalize(pStmt); 23703752785fSdrh (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0); 237119e2d37fSdrh 237219e2d37fSdrh if( zCommit[0] == 'C' ){ 237319e2d37fSdrh /* success, set result as number of lines processed */ 237419e2d37fSdrh pResult = Tcl_GetObjResult(interp); 237519e2d37fSdrh Tcl_SetIntObj(pResult, lineno); 237619e2d37fSdrh rc = TCL_OK; 237719e2d37fSdrh }else{ 237819e2d37fSdrh /* failure, append lineno where failed */ 23795bb3eb9bSdrh sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno); 2380a198f2b5Sdrh Tcl_AppendResult(interp,", failed while processing line: ",zLineNum, 2381a198f2b5Sdrh (char*)0); 238219e2d37fSdrh rc = TCL_ERROR; 238319e2d37fSdrh } 238419e2d37fSdrh break; 238519e2d37fSdrh } 238619e2d37fSdrh 238775897234Sdrh /* 23884144905bSdrh ** $db enable_load_extension BOOLEAN 23894144905bSdrh ** 23904144905bSdrh ** Turn the extension loading feature on or off. It if off by 23914144905bSdrh ** default. 23924144905bSdrh */ 23934144905bSdrh case DB_ENABLE_LOAD_EXTENSION: { 2394f533acc0Sdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 23954144905bSdrh int onoff; 23964144905bSdrh if( objc!=3 ){ 23974144905bSdrh Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN"); 23984144905bSdrh return TCL_ERROR; 23994144905bSdrh } 24004144905bSdrh if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ 24014144905bSdrh return TCL_ERROR; 24024144905bSdrh } 24034144905bSdrh sqlite3_enable_load_extension(pDb->db, onoff); 24044144905bSdrh break; 2405f533acc0Sdrh #else 2406f533acc0Sdrh Tcl_AppendResult(interp, "extension loading is turned off at compile-time", 2407a198f2b5Sdrh (char*)0); 2408f533acc0Sdrh return TCL_ERROR; 2409f533acc0Sdrh #endif 24104144905bSdrh } 24114144905bSdrh 24124144905bSdrh /* 2413dcd997eaSdrh ** $db errorcode 2414dcd997eaSdrh ** 2415dcd997eaSdrh ** Return the numeric error code that was returned by the most recent 24166f8a503dSdanielk1977 ** call to sqlite3_exec(). 2417dcd997eaSdrh */ 2418dcd997eaSdrh case DB_ERRORCODE: { 2419f3ce83f5Sdanielk1977 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); 2420dcd997eaSdrh break; 2421dcd997eaSdrh } 2422dcd997eaSdrh 2423dcd997eaSdrh /* 24244a4c11aaSdan ** $db exists $sql 24251807ce37Sdrh ** $db onecolumn $sql 242675897234Sdrh ** 24274a4c11aaSdan ** The onecolumn method is the equivalent of: 24284a4c11aaSdan ** lindex [$db eval $sql] 0 24294a4c11aaSdan */ 24304a4c11aaSdan case DB_EXISTS: 24314a4c11aaSdan case DB_ONECOLUMN: { 2432edc4024bSdrh Tcl_Obj *pResult = 0; 24334a4c11aaSdan DbEvalContext sEval; 24344a4c11aaSdan if( objc!=3 ){ 24354a4c11aaSdan Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 24364a4c11aaSdan return TCL_ERROR; 24374a4c11aaSdan } 24384a4c11aaSdan 24394a4c11aaSdan dbEvalInit(&sEval, pDb, objv[2], 0); 24404a4c11aaSdan rc = dbEvalStep(&sEval); 24414a4c11aaSdan if( choice==DB_ONECOLUMN ){ 24424a4c11aaSdan if( rc==TCL_OK ){ 2443edc4024bSdrh pResult = dbEvalColumnValue(&sEval, 0); 2444d5f12cd5Sdan }else if( rc==TCL_BREAK ){ 2445d5f12cd5Sdan Tcl_ResetResult(interp); 24464a4c11aaSdan } 24474a4c11aaSdan }else if( rc==TCL_BREAK || rc==TCL_OK ){ 2448edc4024bSdrh pResult = Tcl_NewBooleanObj(rc==TCL_OK); 24494a4c11aaSdan } 24504a4c11aaSdan dbEvalFinalize(&sEval); 2451edc4024bSdrh if( pResult ) Tcl_SetObjResult(interp, pResult); 24524a4c11aaSdan 24534a4c11aaSdan if( rc==TCL_BREAK ){ 24544a4c11aaSdan rc = TCL_OK; 24554a4c11aaSdan } 24564a4c11aaSdan break; 24574a4c11aaSdan } 24584a4c11aaSdan 24594a4c11aaSdan /* 24604a4c11aaSdan ** $db eval $sql ?array? ?{ ...code... }? 24614a4c11aaSdan ** 246275897234Sdrh ** The SQL statement in $sql is evaluated. For each row, the values are 2463bec3f402Sdrh ** placed in elements of the array named "array" and ...code... is executed. 246475897234Sdrh ** If "array" and "code" are omitted, then no callback is every invoked. 246575897234Sdrh ** If "array" is an empty string, then the values are placed in variables 246675897234Sdrh ** that have the same name as the fields extracted by the query. 246775897234Sdrh */ 24684a4c11aaSdan case DB_EVAL: { 246992febd92Sdrh if( objc<3 || objc>5 ){ 2470895d7472Sdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?"); 247130ccda10Sdanielk1977 return TCL_ERROR; 247230ccda10Sdanielk1977 } 24734a4c11aaSdan 247492febd92Sdrh if( objc==3 ){ 24754a4c11aaSdan DbEvalContext sEval; 24764a4c11aaSdan Tcl_Obj *pRet = Tcl_NewObj(); 24774a4c11aaSdan Tcl_IncrRefCount(pRet); 24784a4c11aaSdan dbEvalInit(&sEval, pDb, objv[2], 0); 24794a4c11aaSdan while( TCL_OK==(rc = dbEvalStep(&sEval)) ){ 24804a4c11aaSdan int i; 24814a4c11aaSdan int nCol; 24824a4c11aaSdan dbEvalRowInfo(&sEval, &nCol, 0); 248392febd92Sdrh for(i=0; i<nCol; i++){ 24844a4c11aaSdan Tcl_ListObjAppendElement(interp, pRet, dbEvalColumnValue(&sEval, i)); 248592febd92Sdrh } 248630ccda10Sdanielk1977 } 24874a4c11aaSdan dbEvalFinalize(&sEval); 248890b6bb19Sdrh if( rc==TCL_BREAK ){ 24894a4c11aaSdan Tcl_SetObjResult(interp, pRet); 249090b6bb19Sdrh rc = TCL_OK; 249190b6bb19Sdrh } 2492ef2cb63eSdanielk1977 Tcl_DecrRefCount(pRet); 24934a4c11aaSdan }else{ 24948e18922fSmistachkin ClientData cd2[2]; 24954a4c11aaSdan DbEvalContext *p; 24964a4c11aaSdan Tcl_Obj *pArray = 0; 24974a4c11aaSdan Tcl_Obj *pScript; 24984a4c11aaSdan 24994a4c11aaSdan if( objc==5 && *(char *)Tcl_GetString(objv[3]) ){ 25004a4c11aaSdan pArray = objv[3]; 25014a4c11aaSdan } 25024a4c11aaSdan pScript = objv[objc-1]; 25034a4c11aaSdan Tcl_IncrRefCount(pScript); 25044a4c11aaSdan 25054a4c11aaSdan p = (DbEvalContext *)Tcl_Alloc(sizeof(DbEvalContext)); 25064a4c11aaSdan dbEvalInit(p, pDb, objv[2], pArray); 25074a4c11aaSdan 25088e18922fSmistachkin cd2[0] = (void *)p; 25098e18922fSmistachkin cd2[1] = (void *)pScript; 25108e18922fSmistachkin rc = DbEvalNextCmd(cd2, interp, TCL_OK); 25111807ce37Sdrh } 251230ccda10Sdanielk1977 break; 251330ccda10Sdanielk1977 } 2514bec3f402Sdrh 2515bec3f402Sdrh /* 25163df30592Sdan ** $db function NAME [-argcount N] [-deterministic] SCRIPT 2517cabb0819Sdrh ** 2518cabb0819Sdrh ** Create a new SQL function called NAME. Whenever that function is 2519cabb0819Sdrh ** called, invoke SCRIPT to evaluate the function. 2520cabb0819Sdrh */ 2521cabb0819Sdrh case DB_FUNCTION: { 25223df30592Sdan int flags = SQLITE_UTF8; 2523cabb0819Sdrh SqlFunc *pFunc; 2524d1e4733dSdrh Tcl_Obj *pScript; 2525cabb0819Sdrh char *zName; 2526e3602be8Sdrh int nArg = -1; 25273df30592Sdan int i; 25283df30592Sdan if( objc<4 ){ 25293df30592Sdan Tcl_WrongNumArgs(interp, 2, objv, "NAME ?SWITCHES? SCRIPT"); 25303df30592Sdan return TCL_ERROR; 25313df30592Sdan } 25323df30592Sdan for(i=3; i<(objc-1); i++){ 25333df30592Sdan const char *z = Tcl_GetString(objv[i]); 25344f21c4afSdrh int n = strlen30(z); 2535e3602be8Sdrh if( n>2 && strncmp(z, "-argcount",n)==0 ){ 25363df30592Sdan if( i==(objc-2) ){ 25373df30592Sdan Tcl_AppendResult(interp, "option requires an argument: ", z, 0); 25383df30592Sdan return TCL_ERROR; 25393df30592Sdan } 25403df30592Sdan if( Tcl_GetIntFromObj(interp, objv[i+1], &nArg) ) return TCL_ERROR; 2541e3602be8Sdrh if( nArg<0 ){ 2542e3602be8Sdrh Tcl_AppendResult(interp, "number of arguments must be non-negative", 2543e3602be8Sdrh (char*)0); 2544cabb0819Sdrh return TCL_ERROR; 2545cabb0819Sdrh } 25463df30592Sdan i++; 25473df30592Sdan }else 25483df30592Sdan if( n>2 && strncmp(z, "-deterministic",n)==0 ){ 25493df30592Sdan flags |= SQLITE_DETERMINISTIC; 2550e3602be8Sdrh }else{ 25513df30592Sdan Tcl_AppendResult(interp, "bad option \"", z, 25523df30592Sdan "\": must be -argcount or -deterministic", 0 25533df30592Sdan ); 25543df30592Sdan return TCL_ERROR; 2555e3602be8Sdrh } 25563df30592Sdan } 25573df30592Sdan 25583df30592Sdan pScript = objv[objc-1]; 2559e3602be8Sdrh zName = Tcl_GetStringFromObj(objv[2], 0); 2560d1e4733dSdrh pFunc = findSqlFunc(pDb, zName); 2561cabb0819Sdrh if( pFunc==0 ) return TCL_ERROR; 2562d1e4733dSdrh if( pFunc->pScript ){ 2563d1e4733dSdrh Tcl_DecrRefCount(pFunc->pScript); 2564d1e4733dSdrh } 2565d1e4733dSdrh pFunc->pScript = pScript; 2566d1e4733dSdrh Tcl_IncrRefCount(pScript); 2567d1e4733dSdrh pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript); 25683df30592Sdan rc = sqlite3_create_function(pDb->db, zName, nArg, flags, 2569d8123366Sdanielk1977 pFunc, tclSqlFunc, 0, 0); 2570fb7e7651Sdrh if( rc!=SQLITE_OK ){ 2571fb7e7651Sdrh rc = TCL_ERROR; 25729636c4e1Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 2573fb7e7651Sdrh } 2574cabb0819Sdrh break; 2575cabb0819Sdrh } 2576cabb0819Sdrh 2577cabb0819Sdrh /* 25788cbadb02Sdanielk1977 ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID 2579b4e9af9fSdanielk1977 */ 2580b4e9af9fSdanielk1977 case DB_INCRBLOB: { 258132a0d8bbSdanielk1977 #ifdef SQLITE_OMIT_INCRBLOB 2582a198f2b5Sdrh Tcl_AppendResult(interp, "incrblob not available in this build", (char*)0); 258332a0d8bbSdanielk1977 return TCL_ERROR; 258432a0d8bbSdanielk1977 #else 25858cbadb02Sdanielk1977 int isReadonly = 0; 2586b4e9af9fSdanielk1977 const char *zDb = "main"; 2587b4e9af9fSdanielk1977 const char *zTable; 2588b4e9af9fSdanielk1977 const char *zColumn; 2589b3f787f4Sdrh Tcl_WideInt iRow; 2590b4e9af9fSdanielk1977 25918cbadb02Sdanielk1977 /* Check for the -readonly option */ 25928cbadb02Sdanielk1977 if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){ 25938cbadb02Sdanielk1977 isReadonly = 1; 25948cbadb02Sdanielk1977 } 25958cbadb02Sdanielk1977 25968cbadb02Sdanielk1977 if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){ 25978cbadb02Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID"); 2598b4e9af9fSdanielk1977 return TCL_ERROR; 2599b4e9af9fSdanielk1977 } 2600b4e9af9fSdanielk1977 26018cbadb02Sdanielk1977 if( objc==(6+isReadonly) ){ 2602b4e9af9fSdanielk1977 zDb = Tcl_GetString(objv[2]); 2603b4e9af9fSdanielk1977 } 2604b4e9af9fSdanielk1977 zTable = Tcl_GetString(objv[objc-3]); 2605b4e9af9fSdanielk1977 zColumn = Tcl_GetString(objv[objc-2]); 2606b4e9af9fSdanielk1977 rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow); 2607b4e9af9fSdanielk1977 2608b4e9af9fSdanielk1977 if( rc==TCL_OK ){ 26098cbadb02Sdanielk1977 rc = createIncrblobChannel( 2610edf5b165Sdan interp, pDb, zDb, zTable, zColumn, (sqlite3_int64)iRow, isReadonly 26118cbadb02Sdanielk1977 ); 2612b4e9af9fSdanielk1977 } 261332a0d8bbSdanielk1977 #endif 2614b4e9af9fSdanielk1977 break; 2615b4e9af9fSdanielk1977 } 2616b4e9af9fSdanielk1977 2617b4e9af9fSdanielk1977 /* 2618f11bded5Sdrh ** $db interrupt 2619f11bded5Sdrh ** 2620f11bded5Sdrh ** Interrupt the execution of the inner-most SQL interpreter. This 2621f11bded5Sdrh ** causes the SQL statement to return an error of SQLITE_INTERRUPT. 2622f11bded5Sdrh */ 2623f11bded5Sdrh case DB_INTERRUPT: { 2624f11bded5Sdrh sqlite3_interrupt(pDb->db); 2625f11bded5Sdrh break; 2626f11bded5Sdrh } 2627f11bded5Sdrh 2628f11bded5Sdrh /* 262919e2d37fSdrh ** $db nullvalue ?STRING? 263019e2d37fSdrh ** 263119e2d37fSdrh ** Change text used when a NULL comes back from the database. If ?STRING? 263219e2d37fSdrh ** is not present, then the current string used for NULL is returned. 263319e2d37fSdrh ** If STRING is present, then STRING is returned. 263419e2d37fSdrh ** 263519e2d37fSdrh */ 263619e2d37fSdrh case DB_NULLVALUE: { 263719e2d37fSdrh if( objc!=2 && objc!=3 ){ 263819e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE"); 263919e2d37fSdrh return TCL_ERROR; 264019e2d37fSdrh } 264119e2d37fSdrh if( objc==3 ){ 264219e2d37fSdrh int len; 264319e2d37fSdrh char *zNull = Tcl_GetStringFromObj(objv[2], &len); 264419e2d37fSdrh if( pDb->zNull ){ 264519e2d37fSdrh Tcl_Free(pDb->zNull); 264619e2d37fSdrh } 264719e2d37fSdrh if( zNull && len>0 ){ 264819e2d37fSdrh pDb->zNull = Tcl_Alloc( len + 1 ); 26497fd33929Sdrh memcpy(pDb->zNull, zNull, len); 265019e2d37fSdrh pDb->zNull[len] = '\0'; 265119e2d37fSdrh }else{ 265219e2d37fSdrh pDb->zNull = 0; 265319e2d37fSdrh } 265419e2d37fSdrh } 2655c45e6716Sdrh Tcl_SetObjResult(interp, Tcl_NewStringObj(pDb->zNull, -1)); 265619e2d37fSdrh break; 265719e2d37fSdrh } 265819e2d37fSdrh 265919e2d37fSdrh /* 2660af9ff33aSdrh ** $db last_insert_rowid 2661af9ff33aSdrh ** 2662af9ff33aSdrh ** Return an integer which is the ROWID for the most recent insert. 2663af9ff33aSdrh */ 2664af9ff33aSdrh case DB_LAST_INSERT_ROWID: { 2665af9ff33aSdrh Tcl_Obj *pResult; 2666f7e678d6Sdrh Tcl_WideInt rowid; 2667af9ff33aSdrh if( objc!=2 ){ 2668af9ff33aSdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 2669af9ff33aSdrh return TCL_ERROR; 2670af9ff33aSdrh } 26716f8a503dSdanielk1977 rowid = sqlite3_last_insert_rowid(pDb->db); 2672af9ff33aSdrh pResult = Tcl_GetObjResult(interp); 2673f7e678d6Sdrh Tcl_SetWideIntObj(pResult, rowid); 2674af9ff33aSdrh break; 2675af9ff33aSdrh } 2676af9ff33aSdrh 2677af9ff33aSdrh /* 26784a4c11aaSdan ** The DB_ONECOLUMN method is implemented together with DB_EXISTS. 26795d9d7576Sdrh */ 26801807ce37Sdrh 26811807ce37Sdrh /* $db progress ?N CALLBACK? 26821807ce37Sdrh ** 26831807ce37Sdrh ** Invoke the given callback every N virtual machine opcodes while executing 26841807ce37Sdrh ** queries. 26851807ce37Sdrh */ 26861807ce37Sdrh case DB_PROGRESS: { 26871807ce37Sdrh if( objc==2 ){ 26881807ce37Sdrh if( pDb->zProgress ){ 2689a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zProgress, (char*)0); 26905d9d7576Sdrh } 26911807ce37Sdrh }else if( objc==4 ){ 26921807ce37Sdrh char *zProgress; 26931807ce37Sdrh int len; 26941807ce37Sdrh int N; 26951807ce37Sdrh if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ 26961807ce37Sdrh return TCL_ERROR; 26971807ce37Sdrh }; 26981807ce37Sdrh if( pDb->zProgress ){ 26991807ce37Sdrh Tcl_Free(pDb->zProgress); 27001807ce37Sdrh } 27011807ce37Sdrh zProgress = Tcl_GetStringFromObj(objv[3], &len); 27021807ce37Sdrh if( zProgress && len>0 ){ 27031807ce37Sdrh pDb->zProgress = Tcl_Alloc( len + 1 ); 27045bb3eb9bSdrh memcpy(pDb->zProgress, zProgress, len+1); 27051807ce37Sdrh }else{ 27061807ce37Sdrh pDb->zProgress = 0; 27071807ce37Sdrh } 27081807ce37Sdrh #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 27091807ce37Sdrh if( pDb->zProgress ){ 27101807ce37Sdrh pDb->interp = interp; 27111807ce37Sdrh sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb); 27121807ce37Sdrh }else{ 27131807ce37Sdrh sqlite3_progress_handler(pDb->db, 0, 0, 0); 27141807ce37Sdrh } 27151807ce37Sdrh #endif 27161807ce37Sdrh }else{ 27171807ce37Sdrh Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); 27181807ce37Sdrh return TCL_ERROR; 27195d9d7576Sdrh } 27205d9d7576Sdrh break; 27215d9d7576Sdrh } 27225d9d7576Sdrh 272319e2d37fSdrh /* $db profile ?CALLBACK? 272419e2d37fSdrh ** 272519e2d37fSdrh ** Make arrangements to invoke the CALLBACK routine after each SQL statement 272619e2d37fSdrh ** that has run. The text of the SQL and the amount of elapse time are 272719e2d37fSdrh ** appended to CALLBACK before the script is run. 272819e2d37fSdrh */ 272919e2d37fSdrh case DB_PROFILE: { 273019e2d37fSdrh if( objc>3 ){ 273119e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 273219e2d37fSdrh return TCL_ERROR; 273319e2d37fSdrh }else if( objc==2 ){ 273419e2d37fSdrh if( pDb->zProfile ){ 2735a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zProfile, (char*)0); 273619e2d37fSdrh } 273719e2d37fSdrh }else{ 273819e2d37fSdrh char *zProfile; 273919e2d37fSdrh int len; 274019e2d37fSdrh if( pDb->zProfile ){ 274119e2d37fSdrh Tcl_Free(pDb->zProfile); 274219e2d37fSdrh } 274319e2d37fSdrh zProfile = Tcl_GetStringFromObj(objv[2], &len); 274419e2d37fSdrh if( zProfile && len>0 ){ 274519e2d37fSdrh pDb->zProfile = Tcl_Alloc( len + 1 ); 27465bb3eb9bSdrh memcpy(pDb->zProfile, zProfile, len+1); 274719e2d37fSdrh }else{ 274819e2d37fSdrh pDb->zProfile = 0; 274919e2d37fSdrh } 2750bb201344Sshaneh #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) 275119e2d37fSdrh if( pDb->zProfile ){ 275219e2d37fSdrh pDb->interp = interp; 275319e2d37fSdrh sqlite3_profile(pDb->db, DbProfileHandler, pDb); 275419e2d37fSdrh }else{ 275519e2d37fSdrh sqlite3_profile(pDb->db, 0, 0); 275619e2d37fSdrh } 275719e2d37fSdrh #endif 275819e2d37fSdrh } 275919e2d37fSdrh break; 276019e2d37fSdrh } 276119e2d37fSdrh 27625d9d7576Sdrh /* 276322fbcb8dSdrh ** $db rekey KEY 276422fbcb8dSdrh ** 276522fbcb8dSdrh ** Change the encryption key on the currently open database. 276622fbcb8dSdrh */ 276722fbcb8dSdrh case DB_REKEY: { 276832f57d4cSdrh #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 276922fbcb8dSdrh int nKey; 277022fbcb8dSdrh void *pKey; 2771b07028f7Sdrh #endif 277222fbcb8dSdrh if( objc!=3 ){ 277322fbcb8dSdrh Tcl_WrongNumArgs(interp, 2, objv, "KEY"); 277422fbcb8dSdrh return TCL_ERROR; 277522fbcb8dSdrh } 277632f57d4cSdrh #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 2777b07028f7Sdrh pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); 27782011d5f5Sdrh rc = sqlite3_rekey(pDb->db, pKey, nKey); 277922fbcb8dSdrh if( rc ){ 2780a198f2b5Sdrh Tcl_AppendResult(interp, sqlite3_errstr(rc), (char*)0); 278122fbcb8dSdrh rc = TCL_ERROR; 278222fbcb8dSdrh } 278322fbcb8dSdrh #endif 278422fbcb8dSdrh break; 278522fbcb8dSdrh } 278622fbcb8dSdrh 2787dc2c4915Sdrh /* $db restore ?DATABASE? FILENAME 2788dc2c4915Sdrh ** 2789dc2c4915Sdrh ** Open a database file named FILENAME. Transfer the content 2790dc2c4915Sdrh ** of FILENAME into the local database DATABASE (default: "main"). 2791dc2c4915Sdrh */ 2792dc2c4915Sdrh case DB_RESTORE: { 2793dc2c4915Sdrh const char *zSrcFile; 2794dc2c4915Sdrh const char *zDestDb; 2795dc2c4915Sdrh sqlite3 *pSrc; 2796dc2c4915Sdrh sqlite3_backup *pBackup; 2797dc2c4915Sdrh int nTimeout = 0; 2798dc2c4915Sdrh 2799dc2c4915Sdrh if( objc==3 ){ 2800dc2c4915Sdrh zDestDb = "main"; 2801dc2c4915Sdrh zSrcFile = Tcl_GetString(objv[2]); 2802dc2c4915Sdrh }else if( objc==4 ){ 2803dc2c4915Sdrh zDestDb = Tcl_GetString(objv[2]); 2804dc2c4915Sdrh zSrcFile = Tcl_GetString(objv[3]); 2805dc2c4915Sdrh }else{ 2806dc2c4915Sdrh Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 2807dc2c4915Sdrh return TCL_ERROR; 2808dc2c4915Sdrh } 2809147ef394Sdrh rc = sqlite3_open_v2(zSrcFile, &pSrc, 2810147ef394Sdrh SQLITE_OPEN_READONLY | pDb->openFlags, 0); 2811dc2c4915Sdrh if( rc!=SQLITE_OK ){ 2812dc2c4915Sdrh Tcl_AppendResult(interp, "cannot open source database: ", 2813dc2c4915Sdrh sqlite3_errmsg(pSrc), (char*)0); 2814dc2c4915Sdrh sqlite3_close(pSrc); 2815dc2c4915Sdrh return TCL_ERROR; 2816dc2c4915Sdrh } 2817dc2c4915Sdrh pBackup = sqlite3_backup_init(pDb->db, zDestDb, pSrc, "main"); 2818dc2c4915Sdrh if( pBackup==0 ){ 2819dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: ", 2820dc2c4915Sdrh sqlite3_errmsg(pDb->db), (char*)0); 2821dc2c4915Sdrh sqlite3_close(pSrc); 2822dc2c4915Sdrh return TCL_ERROR; 2823dc2c4915Sdrh } 2824dc2c4915Sdrh while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK 2825dc2c4915Sdrh || rc==SQLITE_BUSY ){ 2826dc2c4915Sdrh if( rc==SQLITE_BUSY ){ 2827dc2c4915Sdrh if( nTimeout++ >= 3 ) break; 2828dc2c4915Sdrh sqlite3_sleep(100); 2829dc2c4915Sdrh } 2830dc2c4915Sdrh } 2831dc2c4915Sdrh sqlite3_backup_finish(pBackup); 2832dc2c4915Sdrh if( rc==SQLITE_DONE ){ 2833dc2c4915Sdrh rc = TCL_OK; 2834dc2c4915Sdrh }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 2835dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: source database busy", 2836dc2c4915Sdrh (char*)0); 2837dc2c4915Sdrh rc = TCL_ERROR; 2838dc2c4915Sdrh }else{ 2839dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: ", 2840dc2c4915Sdrh sqlite3_errmsg(pDb->db), (char*)0); 2841dc2c4915Sdrh rc = TCL_ERROR; 2842dc2c4915Sdrh } 2843dc2c4915Sdrh sqlite3_close(pSrc); 2844dc2c4915Sdrh break; 2845dc2c4915Sdrh } 2846dc2c4915Sdrh 284722fbcb8dSdrh /* 28483c379b01Sdrh ** $db status (step|sort|autoindex) 2849d1d38488Sdrh ** 2850d1d38488Sdrh ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or 2851d1d38488Sdrh ** SQLITE_STMTSTATUS_SORT for the most recent eval. 2852d1d38488Sdrh */ 2853d1d38488Sdrh case DB_STATUS: { 2854d1d38488Sdrh int v; 2855d1d38488Sdrh const char *zOp; 2856d1d38488Sdrh if( objc!=3 ){ 28571c320a43Sdrh Tcl_WrongNumArgs(interp, 2, objv, "(step|sort|autoindex)"); 2858d1d38488Sdrh return TCL_ERROR; 2859d1d38488Sdrh } 2860d1d38488Sdrh zOp = Tcl_GetString(objv[2]); 2861d1d38488Sdrh if( strcmp(zOp, "step")==0 ){ 2862d1d38488Sdrh v = pDb->nStep; 2863d1d38488Sdrh }else if( strcmp(zOp, "sort")==0 ){ 2864d1d38488Sdrh v = pDb->nSort; 28653c379b01Sdrh }else if( strcmp(zOp, "autoindex")==0 ){ 28663c379b01Sdrh v = pDb->nIndex; 2867d1d38488Sdrh }else{ 28683c379b01Sdrh Tcl_AppendResult(interp, 28693c379b01Sdrh "bad argument: should be autoindex, step, or sort", 2870d1d38488Sdrh (char*)0); 2871d1d38488Sdrh return TCL_ERROR; 2872d1d38488Sdrh } 2873d1d38488Sdrh Tcl_SetObjResult(interp, Tcl_NewIntObj(v)); 2874d1d38488Sdrh break; 2875d1d38488Sdrh } 2876d1d38488Sdrh 2877d1d38488Sdrh /* 2878bec3f402Sdrh ** $db timeout MILLESECONDS 2879bec3f402Sdrh ** 2880bec3f402Sdrh ** Delay for the number of milliseconds specified when a file is locked. 2881bec3f402Sdrh */ 28826d31316cSdrh case DB_TIMEOUT: { 2883bec3f402Sdrh int ms; 28846d31316cSdrh if( objc!=3 ){ 28856d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); 2886bec3f402Sdrh return TCL_ERROR; 288775897234Sdrh } 28886d31316cSdrh if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; 28896f8a503dSdanielk1977 sqlite3_busy_timeout(pDb->db, ms); 28906d31316cSdrh break; 289175897234Sdrh } 2892b5a20d3cSdrh 28930f14e2ebSdrh /* 28940f14e2ebSdrh ** $db total_changes 28950f14e2ebSdrh ** 28960f14e2ebSdrh ** Return the number of rows that were modified, inserted, or deleted 28970f14e2ebSdrh ** since the database handle was created. 28980f14e2ebSdrh */ 28990f14e2ebSdrh case DB_TOTAL_CHANGES: { 29000f14e2ebSdrh Tcl_Obj *pResult; 29010f14e2ebSdrh if( objc!=2 ){ 29020f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 29030f14e2ebSdrh return TCL_ERROR; 29040f14e2ebSdrh } 29050f14e2ebSdrh pResult = Tcl_GetObjResult(interp); 29060f14e2ebSdrh Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db)); 29070f14e2ebSdrh break; 29080f14e2ebSdrh } 29090f14e2ebSdrh 2910b5a20d3cSdrh /* $db trace ?CALLBACK? 2911b5a20d3cSdrh ** 2912b5a20d3cSdrh ** Make arrangements to invoke the CALLBACK routine for each SQL statement 2913b5a20d3cSdrh ** that is executed. The text of the SQL is appended to CALLBACK before 2914b5a20d3cSdrh ** it is executed. 2915b5a20d3cSdrh */ 2916b5a20d3cSdrh case DB_TRACE: { 2917b5a20d3cSdrh if( objc>3 ){ 2918b5a20d3cSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 2919b97759edSdrh return TCL_ERROR; 2920b5a20d3cSdrh }else if( objc==2 ){ 2921b5a20d3cSdrh if( pDb->zTrace ){ 2922a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zTrace, (char*)0); 2923b5a20d3cSdrh } 2924b5a20d3cSdrh }else{ 2925b5a20d3cSdrh char *zTrace; 2926b5a20d3cSdrh int len; 2927b5a20d3cSdrh if( pDb->zTrace ){ 2928b5a20d3cSdrh Tcl_Free(pDb->zTrace); 2929b5a20d3cSdrh } 2930b5a20d3cSdrh zTrace = Tcl_GetStringFromObj(objv[2], &len); 2931b5a20d3cSdrh if( zTrace && len>0 ){ 2932b5a20d3cSdrh pDb->zTrace = Tcl_Alloc( len + 1 ); 29335bb3eb9bSdrh memcpy(pDb->zTrace, zTrace, len+1); 2934b5a20d3cSdrh }else{ 2935b5a20d3cSdrh pDb->zTrace = 0; 2936b5a20d3cSdrh } 2937087ec072Sdrh #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) \ 2938087ec072Sdrh && !defined(SQLITE_OMIT_DEPRECATED) 2939b5a20d3cSdrh if( pDb->zTrace ){ 2940b5a20d3cSdrh pDb->interp = interp; 29416f8a503dSdanielk1977 sqlite3_trace(pDb->db, DbTraceHandler, pDb); 2942b5a20d3cSdrh }else{ 29436f8a503dSdanielk1977 sqlite3_trace(pDb->db, 0, 0); 2944b5a20d3cSdrh } 294519e2d37fSdrh #endif 2946b5a20d3cSdrh } 2947b5a20d3cSdrh break; 2948b5a20d3cSdrh } 2949b5a20d3cSdrh 2950b56660f5Smistachkin /* $db trace_v2 ?CALLBACK? ?MASK? 2951b56660f5Smistachkin ** 2952b56660f5Smistachkin ** Make arrangements to invoke the CALLBACK routine for each trace event 2953b56660f5Smistachkin ** matching the mask that is generated. The parameters are appended to 2954b56660f5Smistachkin ** CALLBACK before it is executed. 2955b56660f5Smistachkin */ 2956b56660f5Smistachkin case DB_TRACE_V2: { 2957b56660f5Smistachkin if( objc>4 ){ 2958b56660f5Smistachkin Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK? ?MASK?"); 2959b56660f5Smistachkin return TCL_ERROR; 2960b56660f5Smistachkin }else if( objc==2 ){ 2961b56660f5Smistachkin if( pDb->zTraceV2 ){ 2962b56660f5Smistachkin Tcl_AppendResult(interp, pDb->zTraceV2, (char*)0); 2963b56660f5Smistachkin } 2964b56660f5Smistachkin }else{ 2965b56660f5Smistachkin char *zTraceV2; 2966b56660f5Smistachkin int len; 2967b52dcd89Smistachkin Tcl_WideInt wMask = 0; 2968b56660f5Smistachkin if( objc==4 ){ 2969b52dcd89Smistachkin static const char *TTYPE_strs[] = { 2970b52dcd89Smistachkin "statement", "profile", "row", "close", 0 2971b52dcd89Smistachkin }; 2972b52dcd89Smistachkin enum TTYPE_enum { 2973b52dcd89Smistachkin TTYPE_STMT, TTYPE_PROFILE, TTYPE_ROW, TTYPE_CLOSE 2974b52dcd89Smistachkin }; 2975b52dcd89Smistachkin int i; 2976b52dcd89Smistachkin if( TCL_OK!=Tcl_ListObjLength(interp, objv[3], &len) ){ 2977b52dcd89Smistachkin return TCL_ERROR; 2978b52dcd89Smistachkin } 2979b52dcd89Smistachkin for(i=0; i<len; i++){ 2980b52dcd89Smistachkin Tcl_Obj *pObj; 2981b52dcd89Smistachkin int ttype; 2982b52dcd89Smistachkin if( TCL_OK!=Tcl_ListObjIndex(interp, objv[3], i, &pObj) ){ 2983b52dcd89Smistachkin return TCL_ERROR; 2984b52dcd89Smistachkin } 2985b52dcd89Smistachkin if( Tcl_GetIndexFromObj(interp, pObj, TTYPE_strs, "trace type", 2986b52dcd89Smistachkin 0, &ttype)!=TCL_OK ){ 2987b52dcd89Smistachkin Tcl_WideInt wType; 2988b52dcd89Smistachkin Tcl_Obj *pError = Tcl_DuplicateObj(Tcl_GetObjResult(interp)); 2989b52dcd89Smistachkin Tcl_IncrRefCount(pError); 2990b52dcd89Smistachkin if( TCL_OK==Tcl_GetWideIntFromObj(interp, pObj, &wType) ){ 2991b52dcd89Smistachkin Tcl_DecrRefCount(pError); 2992b52dcd89Smistachkin wMask |= wType; 2993b52dcd89Smistachkin }else{ 2994b52dcd89Smistachkin Tcl_SetObjResult(interp, pError); 2995b52dcd89Smistachkin Tcl_DecrRefCount(pError); 2996b56660f5Smistachkin return TCL_ERROR; 2997b56660f5Smistachkin } 2998b56660f5Smistachkin }else{ 2999b52dcd89Smistachkin switch( (enum TTYPE_enum)ttype ){ 3000b52dcd89Smistachkin case TTYPE_STMT: wMask |= SQLITE_TRACE_STMT; break; 3001b52dcd89Smistachkin case TTYPE_PROFILE: wMask |= SQLITE_TRACE_PROFILE; break; 3002b52dcd89Smistachkin case TTYPE_ROW: wMask |= SQLITE_TRACE_ROW; break; 3003b52dcd89Smistachkin case TTYPE_CLOSE: wMask |= SQLITE_TRACE_CLOSE; break; 3004b52dcd89Smistachkin } 3005b52dcd89Smistachkin } 3006b52dcd89Smistachkin } 3007b52dcd89Smistachkin }else{ 3008b52dcd89Smistachkin wMask = SQLITE_TRACE_STMT; /* use the "legacy" default */ 3009b56660f5Smistachkin } 3010b56660f5Smistachkin if( pDb->zTraceV2 ){ 3011b56660f5Smistachkin Tcl_Free(pDb->zTraceV2); 3012b56660f5Smistachkin } 3013b56660f5Smistachkin zTraceV2 = Tcl_GetStringFromObj(objv[2], &len); 3014b56660f5Smistachkin if( zTraceV2 && len>0 ){ 3015b56660f5Smistachkin pDb->zTraceV2 = Tcl_Alloc( len + 1 ); 3016b56660f5Smistachkin memcpy(pDb->zTraceV2, zTraceV2, len+1); 3017b56660f5Smistachkin }else{ 3018b56660f5Smistachkin pDb->zTraceV2 = 0; 3019b56660f5Smistachkin } 3020b56660f5Smistachkin #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) 3021b56660f5Smistachkin if( pDb->zTraceV2 ){ 3022b56660f5Smistachkin pDb->interp = interp; 3023b56660f5Smistachkin sqlite3_trace_v2(pDb->db, (unsigned)wMask, DbTraceV2Handler, pDb); 3024b56660f5Smistachkin }else{ 3025b56660f5Smistachkin sqlite3_trace_v2(pDb->db, 0, 0, 0); 3026b56660f5Smistachkin } 3027b56660f5Smistachkin #endif 3028b56660f5Smistachkin } 3029b56660f5Smistachkin break; 3030b56660f5Smistachkin } 3031b56660f5Smistachkin 30323d21423cSdrh /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT 30333d21423cSdrh ** 30343d21423cSdrh ** Start a new transaction (if we are not already in the midst of a 30353d21423cSdrh ** transaction) and execute the TCL script SCRIPT. After SCRIPT 30363d21423cSdrh ** completes, either commit the transaction or roll it back if SCRIPT 30373d21423cSdrh ** throws an exception. Or if no new transation was started, do nothing. 30383d21423cSdrh ** pass the exception on up the stack. 30393d21423cSdrh ** 30403d21423cSdrh ** This command was inspired by Dave Thomas's talk on Ruby at the 30413d21423cSdrh ** 2005 O'Reilly Open Source Convention (OSCON). 30423d21423cSdrh */ 30433d21423cSdrh case DB_TRANSACTION: { 30443d21423cSdrh Tcl_Obj *pScript; 3045cd38d520Sdanielk1977 const char *zBegin = "SAVEPOINT _tcl_transaction"; 30463d21423cSdrh if( objc!=3 && objc!=4 ){ 30473d21423cSdrh Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT"); 30483d21423cSdrh return TCL_ERROR; 30493d21423cSdrh } 3050cd38d520Sdanielk1977 30514a4c11aaSdan if( pDb->nTransaction==0 && objc==4 ){ 30523d21423cSdrh static const char *TTYPE_strs[] = { 3053ce604012Sdrh "deferred", "exclusive", "immediate", 0 30543d21423cSdrh }; 30553d21423cSdrh enum TTYPE_enum { 30563d21423cSdrh TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE 30573d21423cSdrh }; 30583d21423cSdrh int ttype; 3059b5555e7eSdrh if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type", 30603d21423cSdrh 0, &ttype) ){ 30613d21423cSdrh return TCL_ERROR; 30623d21423cSdrh } 30633d21423cSdrh switch( (enum TTYPE_enum)ttype ){ 30643d21423cSdrh case TTYPE_DEFERRED: /* no-op */; break; 30653d21423cSdrh case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break; 30663d21423cSdrh case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break; 30673d21423cSdrh } 30683d21423cSdrh } 3069cd38d520Sdanielk1977 pScript = objv[objc-1]; 3070cd38d520Sdanielk1977 30714a4c11aaSdan /* Run the SQLite BEGIN command to open a transaction or savepoint. */ 30721f1549f8Sdrh pDb->disableAuth++; 3073cd38d520Sdanielk1977 rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0); 30741f1549f8Sdrh pDb->disableAuth--; 3075cd38d520Sdanielk1977 if( rc!=SQLITE_OK ){ 3076a198f2b5Sdrh Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 3077cd38d520Sdanielk1977 return TCL_ERROR; 30783d21423cSdrh } 3079cd38d520Sdanielk1977 pDb->nTransaction++; 3080cd38d520Sdanielk1977 30814a4c11aaSdan /* If using NRE, schedule a callback to invoke the script pScript, then 30824a4c11aaSdan ** a second callback to commit (or rollback) the transaction or savepoint 30834a4c11aaSdan ** opened above. If not using NRE, evaluate the script directly, then 30844a4c11aaSdan ** call function DbTransPostCmd() to commit (or rollback) the transaction 30854a4c11aaSdan ** or savepoint. */ 30864a4c11aaSdan if( DbUseNre() ){ 30874a4c11aaSdan Tcl_NRAddCallback(interp, DbTransPostCmd, cd, 0, 0, 0); 3088a47941feSdrh (void)Tcl_NREvalObj(interp, pScript, 0); 30893d21423cSdrh }else{ 30904a4c11aaSdan rc = DbTransPostCmd(&cd, interp, Tcl_EvalObjEx(interp, pScript, 0)); 30913d21423cSdrh } 30923d21423cSdrh break; 30933d21423cSdrh } 30943d21423cSdrh 309594eb6a14Sdanielk1977 /* 3096404ca075Sdanielk1977 ** $db unlock_notify ?script? 3097404ca075Sdanielk1977 */ 3098404ca075Sdanielk1977 case DB_UNLOCK_NOTIFY: { 3099404ca075Sdanielk1977 #ifndef SQLITE_ENABLE_UNLOCK_NOTIFY 3100a198f2b5Sdrh Tcl_AppendResult(interp, "unlock_notify not available in this build", 3101a198f2b5Sdrh (char*)0); 3102404ca075Sdanielk1977 rc = TCL_ERROR; 3103404ca075Sdanielk1977 #else 3104404ca075Sdanielk1977 if( objc!=2 && objc!=3 ){ 3105404ca075Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 3106404ca075Sdanielk1977 rc = TCL_ERROR; 3107404ca075Sdanielk1977 }else{ 3108404ca075Sdanielk1977 void (*xNotify)(void **, int) = 0; 3109404ca075Sdanielk1977 void *pNotifyArg = 0; 3110404ca075Sdanielk1977 3111404ca075Sdanielk1977 if( pDb->pUnlockNotify ){ 3112404ca075Sdanielk1977 Tcl_DecrRefCount(pDb->pUnlockNotify); 3113404ca075Sdanielk1977 pDb->pUnlockNotify = 0; 3114404ca075Sdanielk1977 } 3115404ca075Sdanielk1977 3116404ca075Sdanielk1977 if( objc==3 ){ 3117404ca075Sdanielk1977 xNotify = DbUnlockNotify; 3118404ca075Sdanielk1977 pNotifyArg = (void *)pDb; 3119404ca075Sdanielk1977 pDb->pUnlockNotify = objv[2]; 3120404ca075Sdanielk1977 Tcl_IncrRefCount(pDb->pUnlockNotify); 3121404ca075Sdanielk1977 } 3122404ca075Sdanielk1977 3123404ca075Sdanielk1977 if( sqlite3_unlock_notify(pDb->db, xNotify, pNotifyArg) ){ 3124a198f2b5Sdrh Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 3125404ca075Sdanielk1977 rc = TCL_ERROR; 3126404ca075Sdanielk1977 } 3127404ca075Sdanielk1977 } 3128404ca075Sdanielk1977 #endif 3129404ca075Sdanielk1977 break; 3130404ca075Sdanielk1977 } 3131404ca075Sdanielk1977 3132304637c0Sdrh /* 3133304637c0Sdrh ** $db preupdate_hook count 3134304637c0Sdrh ** $db preupdate_hook hook ?SCRIPT? 3135304637c0Sdrh ** $db preupdate_hook new INDEX 3136304637c0Sdrh ** $db preupdate_hook old INDEX 3137304637c0Sdrh */ 313846c47d46Sdan case DB_PREUPDATE: { 31399b1c62d4Sdrh #ifndef SQLITE_ENABLE_PREUPDATE_HOOK 31409b1c62d4Sdrh Tcl_AppendResult(interp, "preupdate_hook was omitted at compile-time"); 31419b1c62d4Sdrh rc = TCL_ERROR; 31429b1c62d4Sdrh #else 31431e7a2d43Sdan static const char *azSub[] = {"count", "depth", "hook", "new", "old", 0}; 314446c47d46Sdan enum DbPreupdateSubCmd { 31451e7a2d43Sdan PRE_COUNT, PRE_DEPTH, PRE_HOOK, PRE_NEW, PRE_OLD 314646c47d46Sdan }; 314746c47d46Sdan int iSub; 314846c47d46Sdan 314946c47d46Sdan if( objc<3 ){ 315046c47d46Sdan Tcl_WrongNumArgs(interp, 2, objv, "SUB-COMMAND ?ARGS?"); 315146c47d46Sdan } 315246c47d46Sdan if( Tcl_GetIndexFromObj(interp, objv[2], azSub, "sub-command", 0, &iSub) ){ 315346c47d46Sdan return TCL_ERROR; 315446c47d46Sdan } 315546c47d46Sdan 315646c47d46Sdan switch( (enum DbPreupdateSubCmd)iSub ){ 315746c47d46Sdan case PRE_COUNT: { 315846c47d46Sdan int nCol = sqlite3_preupdate_count(pDb->db); 315946c47d46Sdan Tcl_SetObjResult(interp, Tcl_NewIntObj(nCol)); 316046c47d46Sdan break; 316146c47d46Sdan } 316246c47d46Sdan 316346c47d46Sdan case PRE_HOOK: { 316446c47d46Sdan if( objc>4 ){ 316546c47d46Sdan Tcl_WrongNumArgs(interp, 2, objv, "hook ?SCRIPT?"); 316646c47d46Sdan return TCL_ERROR; 316746c47d46Sdan } 316846c47d46Sdan DbHookCmd(interp, pDb, (objc==4 ? objv[3] : 0), &pDb->pPreUpdateHook); 316946c47d46Sdan break; 317046c47d46Sdan } 317146c47d46Sdan 31721e7a2d43Sdan case PRE_DEPTH: { 31731e7a2d43Sdan Tcl_Obj *pRet; 31741e7a2d43Sdan if( objc!=3 ){ 31751e7a2d43Sdan Tcl_WrongNumArgs(interp, 3, objv, ""); 31761e7a2d43Sdan return TCL_ERROR; 31771e7a2d43Sdan } 31781e7a2d43Sdan pRet = Tcl_NewIntObj(sqlite3_preupdate_depth(pDb->db)); 31791e7a2d43Sdan Tcl_SetObjResult(interp, pRet); 31801e7a2d43Sdan break; 31811e7a2d43Sdan } 31821e7a2d43Sdan 318337db03bfSdan case PRE_NEW: 318446c47d46Sdan case PRE_OLD: { 318546c47d46Sdan int iIdx; 318637db03bfSdan sqlite3_value *pValue; 318746c47d46Sdan if( objc!=4 ){ 318846c47d46Sdan Tcl_WrongNumArgs(interp, 3, objv, "INDEX"); 318946c47d46Sdan return TCL_ERROR; 319046c47d46Sdan } 319146c47d46Sdan if( Tcl_GetIntFromObj(interp, objv[3], &iIdx) ){ 319246c47d46Sdan return TCL_ERROR; 319346c47d46Sdan } 319446c47d46Sdan 319537db03bfSdan if( iSub==PRE_OLD ){ 319646c47d46Sdan rc = sqlite3_preupdate_old(pDb->db, iIdx, &pValue); 319737db03bfSdan }else{ 319837db03bfSdan assert( iSub==PRE_NEW ); 319937db03bfSdan rc = sqlite3_preupdate_new(pDb->db, iIdx, &pValue); 320037db03bfSdan } 320137db03bfSdan 320246c47d46Sdan if( rc==SQLITE_OK ){ 3203304637c0Sdrh Tcl_Obj *pObj; 3204304637c0Sdrh pObj = Tcl_NewStringObj((char*)sqlite3_value_text(pValue), -1); 320546c47d46Sdan Tcl_SetObjResult(interp, pObj); 320637db03bfSdan }else{ 320746c47d46Sdan Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); 320846c47d46Sdan return TCL_ERROR; 320946c47d46Sdan } 321046c47d46Sdan } 321146c47d46Sdan } 32129b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 321346c47d46Sdan break; 321446c47d46Sdan } 321546c47d46Sdan 3216404ca075Sdanielk1977 /* 3217833bf968Sdrh ** $db wal_hook ?script? 321894eb6a14Sdanielk1977 ** $db update_hook ?script? 321971fd80bfSdanielk1977 ** $db rollback_hook ?script? 322094eb6a14Sdanielk1977 */ 3221833bf968Sdrh case DB_WAL_HOOK: 322271fd80bfSdanielk1977 case DB_UPDATE_HOOK: 32236566ebe1Sdan case DB_ROLLBACK_HOOK: { 322471fd80bfSdanielk1977 /* set ppHook to point at pUpdateHook or pRollbackHook, depending on 322571fd80bfSdanielk1977 ** whether [$db update_hook] or [$db rollback_hook] was invoked. 322671fd80bfSdanielk1977 */ 3227c5896b5cSmistachkin Tcl_Obj **ppHook = 0; 322846c47d46Sdan if( choice==DB_WAL_HOOK ) ppHook = &pDb->pWalHook; 322946c47d46Sdan if( choice==DB_UPDATE_HOOK ) ppHook = &pDb->pUpdateHook; 323046c47d46Sdan if( choice==DB_ROLLBACK_HOOK ) ppHook = &pDb->pRollbackHook; 323146c47d46Sdan if( objc>3 ){ 323294eb6a14Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 323394eb6a14Sdanielk1977 return TCL_ERROR; 323494eb6a14Sdanielk1977 } 323571fd80bfSdanielk1977 323646c47d46Sdan DbHookCmd(interp, pDb, (objc==3 ? objv[2] : 0), ppHook); 323794eb6a14Sdanielk1977 break; 323894eb6a14Sdanielk1977 } 323994eb6a14Sdanielk1977 32404397de57Sdanielk1977 /* $db version 32414397de57Sdanielk1977 ** 32424397de57Sdanielk1977 ** Return the version string for this database. 32434397de57Sdanielk1977 */ 32444397de57Sdanielk1977 case DB_VERSION: { 32454397de57Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC); 32464397de57Sdanielk1977 break; 32474397de57Sdanielk1977 } 32484397de57Sdanielk1977 32491067fe11Stpoindex 32506d31316cSdrh } /* End of the SWITCH statement */ 325122fbcb8dSdrh return rc; 325275897234Sdrh } 325375897234Sdrh 3254a2c8a95bSdrh #if SQLITE_TCL_NRE 3255a2c8a95bSdrh /* 3256a2c8a95bSdrh ** Adaptor that provides an objCmd interface to the NRE-enabled 3257a2c8a95bSdrh ** interface implementation. 3258a2c8a95bSdrh */ 3259a121cc7cSmistachkin static int SQLITE_TCLAPI DbObjCmdAdaptor( 3260a2c8a95bSdrh void *cd, 3261a2c8a95bSdrh Tcl_Interp *interp, 3262a2c8a95bSdrh int objc, 3263a2c8a95bSdrh Tcl_Obj *const*objv 3264a2c8a95bSdrh ){ 3265a2c8a95bSdrh return Tcl_NRCallObjProc(interp, DbObjCmd, cd, objc, objv); 3266a2c8a95bSdrh } 3267a2c8a95bSdrh #endif /* SQLITE_TCL_NRE */ 3268a2c8a95bSdrh 326975897234Sdrh /* 32703570ad93Sdrh ** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN? 32719a6284c1Sdanielk1977 ** ?-create BOOLEAN? ?-nomutex BOOLEAN? 327275897234Sdrh ** 327375897234Sdrh ** This is the main Tcl command. When the "sqlite" Tcl command is 327475897234Sdrh ** invoked, this routine runs to process that command. 327575897234Sdrh ** 327675897234Sdrh ** The first argument, DBNAME, is an arbitrary name for a new 327775897234Sdrh ** database connection. This command creates a new command named 327875897234Sdrh ** DBNAME that is used to control that connection. The database 327975897234Sdrh ** connection is deleted when the DBNAME command is deleted. 328075897234Sdrh ** 32813570ad93Sdrh ** The second argument is the name of the database file. 3282fbc3eab8Sdrh ** 328375897234Sdrh */ 32847617e4a8Smistachkin static int SQLITE_TCLAPI DbMain( 32857617e4a8Smistachkin void *cd, 32867617e4a8Smistachkin Tcl_Interp *interp, 32877617e4a8Smistachkin int objc, 32887617e4a8Smistachkin Tcl_Obj *const*objv 32897617e4a8Smistachkin ){ 3290bec3f402Sdrh SqliteDb *p; 329122fbcb8dSdrh const char *zArg; 329275897234Sdrh char *zErrMsg; 32933570ad93Sdrh int i; 329422fbcb8dSdrh const char *zFile; 32953570ad93Sdrh const char *zVfs = 0; 3296d9da78a2Sdrh int flags; 3297882e8e4dSdrh Tcl_DString translatedFilename; 329832f57d4cSdrh #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3299b07028f7Sdrh void *pKey = 0; 3300b07028f7Sdrh int nKey = 0; 3301b07028f7Sdrh #endif 3302540ebf82Smistachkin int rc; 3303d9da78a2Sdrh 3304d9da78a2Sdrh /* In normal use, each TCL interpreter runs in a single thread. So 3305d9da78a2Sdrh ** by default, we can turn of mutexing on SQLite database connections. 3306d9da78a2Sdrh ** However, for testing purposes it is useful to have mutexes turned 3307d9da78a2Sdrh ** on. So, by default, mutexes default off. But if compiled with 3308d9da78a2Sdrh ** SQLITE_TCL_DEFAULT_FULLMUTEX then mutexes default on. 3309d9da78a2Sdrh */ 3310d9da78a2Sdrh #ifdef SQLITE_TCL_DEFAULT_FULLMUTEX 3311d9da78a2Sdrh flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX; 3312d9da78a2Sdrh #else 3313d9da78a2Sdrh flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX; 3314d9da78a2Sdrh #endif 3315d9da78a2Sdrh 331622fbcb8dSdrh if( objc==2 ){ 331722fbcb8dSdrh zArg = Tcl_GetStringFromObj(objv[1], 0); 331822fbcb8dSdrh if( strcmp(zArg,"-version")==0 ){ 3319a198f2b5Sdrh Tcl_AppendResult(interp,sqlite3_libversion(), (char*)0); 3320647cb0e1Sdrh return TCL_OK; 3321647cb0e1Sdrh } 332272bf6a3eSdrh if( strcmp(zArg,"-sourceid")==0 ){ 332372bf6a3eSdrh Tcl_AppendResult(interp,sqlite3_sourceid(), (char*)0); 332472bf6a3eSdrh return TCL_OK; 332572bf6a3eSdrh } 33269eb9e26bSdrh if( strcmp(zArg,"-has-codec")==0 ){ 332732f57d4cSdrh #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3328a198f2b5Sdrh Tcl_AppendResult(interp,"1",(char*)0); 332922fbcb8dSdrh #else 3330a198f2b5Sdrh Tcl_AppendResult(interp,"0",(char*)0); 333122fbcb8dSdrh #endif 333222fbcb8dSdrh return TCL_OK; 333322fbcb8dSdrh } 3334fbc3eab8Sdrh } 33353570ad93Sdrh for(i=3; i+1<objc; i+=2){ 33363570ad93Sdrh zArg = Tcl_GetString(objv[i]); 333722fbcb8dSdrh if( strcmp(zArg,"-key")==0 ){ 333832f57d4cSdrh #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 33393570ad93Sdrh pKey = Tcl_GetByteArrayFromObj(objv[i+1], &nKey); 3340b07028f7Sdrh #endif 33413570ad93Sdrh }else if( strcmp(zArg, "-vfs")==0 ){ 33423c3dd7b9Sdan zVfs = Tcl_GetString(objv[i+1]); 33433570ad93Sdrh }else if( strcmp(zArg, "-readonly")==0 ){ 33443570ad93Sdrh int b; 33453570ad93Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 33463570ad93Sdrh if( b ){ 334733f4e02aSdrh flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); 33483570ad93Sdrh flags |= SQLITE_OPEN_READONLY; 33493570ad93Sdrh }else{ 33503570ad93Sdrh flags &= ~SQLITE_OPEN_READONLY; 33513570ad93Sdrh flags |= SQLITE_OPEN_READWRITE; 33523570ad93Sdrh } 33533570ad93Sdrh }else if( strcmp(zArg, "-create")==0 ){ 33543570ad93Sdrh int b; 33553570ad93Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 335633f4e02aSdrh if( b && (flags & SQLITE_OPEN_READONLY)==0 ){ 33573570ad93Sdrh flags |= SQLITE_OPEN_CREATE; 33583570ad93Sdrh }else{ 33593570ad93Sdrh flags &= ~SQLITE_OPEN_CREATE; 33603570ad93Sdrh } 33619a6284c1Sdanielk1977 }else if( strcmp(zArg, "-nomutex")==0 ){ 33629a6284c1Sdanielk1977 int b; 33639a6284c1Sdanielk1977 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 33649a6284c1Sdanielk1977 if( b ){ 33659a6284c1Sdanielk1977 flags |= SQLITE_OPEN_NOMUTEX; 3366039963adSdrh flags &= ~SQLITE_OPEN_FULLMUTEX; 33679a6284c1Sdanielk1977 }else{ 33689a6284c1Sdanielk1977 flags &= ~SQLITE_OPEN_NOMUTEX; 33699a6284c1Sdanielk1977 } 3370039963adSdrh }else if( strcmp(zArg, "-fullmutex")==0 ){ 3371039963adSdrh int b; 3372039963adSdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 3373039963adSdrh if( b ){ 3374039963adSdrh flags |= SQLITE_OPEN_FULLMUTEX; 3375039963adSdrh flags &= ~SQLITE_OPEN_NOMUTEX; 3376039963adSdrh }else{ 3377039963adSdrh flags &= ~SQLITE_OPEN_FULLMUTEX; 3378039963adSdrh } 3379f12b3f60Sdrh }else if( strcmp(zArg, "-uri")==0 ){ 3380f12b3f60Sdrh int b; 3381f12b3f60Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 3382f12b3f60Sdrh if( b ){ 3383f12b3f60Sdrh flags |= SQLITE_OPEN_URI; 3384f12b3f60Sdrh }else{ 3385f12b3f60Sdrh flags &= ~SQLITE_OPEN_URI; 3386f12b3f60Sdrh } 33873570ad93Sdrh }else{ 33883570ad93Sdrh Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0); 33893570ad93Sdrh return TCL_ERROR; 339022fbcb8dSdrh } 339122fbcb8dSdrh } 33923570ad93Sdrh if( objc<3 || (objc&1)!=1 ){ 339322fbcb8dSdrh Tcl_WrongNumArgs(interp, 1, objv, 33943570ad93Sdrh "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" 339568bd4aa2Sdrh " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?" 339632f57d4cSdrh #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 33973570ad93Sdrh " ?-key CODECKEY?" 339822fbcb8dSdrh #endif 339922fbcb8dSdrh ); 340075897234Sdrh return TCL_ERROR; 340175897234Sdrh } 340275897234Sdrh zErrMsg = 0; 34034cdc9e84Sdrh p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); 340475897234Sdrh if( p==0 ){ 34056ef5e12eSmistachkin Tcl_SetResult(interp, (char *)"malloc failed", TCL_STATIC); 3406bec3f402Sdrh return TCL_ERROR; 3407bec3f402Sdrh } 3408bec3f402Sdrh memset(p, 0, sizeof(*p)); 340922fbcb8dSdrh zFile = Tcl_GetStringFromObj(objv[2], 0); 3410882e8e4dSdrh zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); 3411540ebf82Smistachkin rc = sqlite3_open_v2(zFile, &p->db, flags, zVfs); 3412882e8e4dSdrh Tcl_DStringFree(&translatedFilename); 3413540ebf82Smistachkin if( p->db ){ 341480290863Sdanielk1977 if( SQLITE_OK!=sqlite3_errcode(p->db) ){ 34159404d50eSdrh zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); 341680290863Sdanielk1977 sqlite3_close(p->db); 341780290863Sdanielk1977 p->db = 0; 341880290863Sdanielk1977 } 3419540ebf82Smistachkin }else{ 34205dac8432Smistachkin zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc)); 3421540ebf82Smistachkin } 342232f57d4cSdrh #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) 3423f3a65f7eSdrh if( p->db ){ 34242011d5f5Sdrh sqlite3_key(p->db, pKey, nKey); 3425f3a65f7eSdrh } 3426eb8ed70dSdrh #endif 3427bec3f402Sdrh if( p->db==0 ){ 342875897234Sdrh Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 3429bec3f402Sdrh Tcl_Free((char*)p); 34309404d50eSdrh sqlite3_free(zErrMsg); 343175897234Sdrh return TCL_ERROR; 343275897234Sdrh } 3433fb7e7651Sdrh p->maxStmt = NUM_PREPARED_STMTS; 3434147ef394Sdrh p->openFlags = flags & SQLITE_OPEN_URI; 34355169bbc6Sdrh p->interp = interp; 343622fbcb8dSdrh zArg = Tcl_GetStringFromObj(objv[1], 0); 34374a4c11aaSdan if( DbUseNre() ){ 3438a2c8a95bSdrh Tcl_NRCreateCommand(interp, zArg, DbObjCmdAdaptor, DbObjCmd, 3439a2c8a95bSdrh (char*)p, DbDeleteCmd); 34404a4c11aaSdan }else{ 344122fbcb8dSdrh Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); 34424a4c11aaSdan } 344375897234Sdrh return TCL_OK; 344475897234Sdrh } 344575897234Sdrh 344675897234Sdrh /* 344790ca9753Sdrh ** Provide a dummy Tcl_InitStubs if we are using this as a static 344890ca9753Sdrh ** library. 344990ca9753Sdrh */ 345090ca9753Sdrh #ifndef USE_TCL_STUBS 345190ca9753Sdrh # undef Tcl_InitStubs 34520e85ccfcSdrh # define Tcl_InitStubs(a,b,c) TCL_VERSION 345390ca9753Sdrh #endif 345490ca9753Sdrh 345590ca9753Sdrh /* 345629bc4615Sdrh ** Make sure we have a PACKAGE_VERSION macro defined. This will be 345729bc4615Sdrh ** defined automatically by the TEA makefile. But other makefiles 345829bc4615Sdrh ** do not define it. 345929bc4615Sdrh */ 346029bc4615Sdrh #ifndef PACKAGE_VERSION 346129bc4615Sdrh # define PACKAGE_VERSION SQLITE_VERSION 346229bc4615Sdrh #endif 346329bc4615Sdrh 346429bc4615Sdrh /* 346575897234Sdrh ** Initialize this module. 346675897234Sdrh ** 346775897234Sdrh ** This Tcl module contains only a single new Tcl command named "sqlite". 346875897234Sdrh ** (Hence there is no namespace. There is no point in using a namespace 346975897234Sdrh ** if the extension only supplies one new name!) The "sqlite" command is 347075897234Sdrh ** used to open a new SQLite database. See the DbMain() routine above 347175897234Sdrh ** for additional information. 3472b652f432Sdrh ** 3473b652f432Sdrh ** The EXTERN macros are required by TCL in order to work on windows. 347475897234Sdrh */ 3475b652f432Sdrh EXTERN int Sqlite3_Init(Tcl_Interp *interp){ 347627b2f053Smistachkin int rc = Tcl_InitStubs(interp, "8.4", 0) ? TCL_OK : TCL_ERROR; 34776dc8cbe0Sdrh if( rc==TCL_OK ){ 3478ef4ac8f9Sdrh Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); 34791cca0d22Sdrh #ifndef SQLITE_3_SUFFIX_ONLY 34801cca0d22Sdrh /* The "sqlite" alias is undocumented. It is here only to support 34811cca0d22Sdrh ** legacy scripts. All new scripts should use only the "sqlite3" 34826dc8cbe0Sdrh ** command. */ 348349766d6cSdrh Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); 34844c0f1649Sdrh #endif 34856dc8cbe0Sdrh rc = Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION); 34866dc8cbe0Sdrh } 34876dc8cbe0Sdrh return rc; 348890ca9753Sdrh } 3489b652f432Sdrh EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3490b652f432Sdrh EXTERN int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3491b652f432Sdrh EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3492e2c3a659Sdrh 3493d878cab5Sdrh /* Because it accesses the file-system and uses persistent state, SQLite 3494e75a9eb9Sdrh ** is not considered appropriate for safe interpreters. Hence, we cause 3495e75a9eb9Sdrh ** the _SafeInit() interfaces return TCL_ERROR. 3496d878cab5Sdrh */ 3497e75a9eb9Sdrh EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_ERROR; } 3498e75a9eb9Sdrh EXTERN int Sqlite3_SafeUnload(Tcl_Interp *interp, int flags){return TCL_ERROR;} 3499e75a9eb9Sdrh 3500e75a9eb9Sdrh 350149766d6cSdrh 350249766d6cSdrh #ifndef SQLITE_3_SUFFIX_ONLY 3503a3e63c4aSdan int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3504a3e63c4aSdan int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3505a3e63c4aSdan int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3506a3e63c4aSdan int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 350749766d6cSdrh #endif 350875897234Sdrh 35093e27c026Sdrh #ifdef TCLSH 35103e27c026Sdrh /***************************************************************************** 351157a0227fSdrh ** All of the code that follows is used to build standalone TCL interpreters 351257a0227fSdrh ** that are statically linked with SQLite. Enable these by compiling 351357a0227fSdrh ** with -DTCLSH=n where n can be 1 or 2. An n of 1 generates a standard 351457a0227fSdrh ** tclsh but with SQLite built in. An n of 2 generates the SQLite space 351557a0227fSdrh ** analysis program. 351675897234Sdrh */ 3517348784efSdrh 351857a0227fSdrh #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) 351957a0227fSdrh /* 352057a0227fSdrh * This code implements the MD5 message-digest algorithm. 352157a0227fSdrh * The algorithm is due to Ron Rivest. This code was 352257a0227fSdrh * written by Colin Plumb in 1993, no copyright is claimed. 352357a0227fSdrh * This code is in the public domain; do with it what you wish. 352457a0227fSdrh * 352557a0227fSdrh * Equivalent code is available from RSA Data Security, Inc. 352657a0227fSdrh * This code has been tested against that, and is equivalent, 352757a0227fSdrh * except that you don't need to include two pages of legalese 352857a0227fSdrh * with every copy. 352957a0227fSdrh * 353057a0227fSdrh * To compute the message digest of a chunk of bytes, declare an 353157a0227fSdrh * MD5Context structure, pass it to MD5Init, call MD5Update as 353257a0227fSdrh * needed on buffers full of bytes, and then call MD5Final, which 353357a0227fSdrh * will fill a supplied 16-byte array with the digest. 353457a0227fSdrh */ 353557a0227fSdrh 353657a0227fSdrh /* 353757a0227fSdrh * If compiled on a machine that doesn't have a 32-bit integer, 353857a0227fSdrh * you just set "uint32" to the appropriate datatype for an 353957a0227fSdrh * unsigned 32-bit integer. For example: 354057a0227fSdrh * 354157a0227fSdrh * cc -Duint32='unsigned long' md5.c 354257a0227fSdrh * 354357a0227fSdrh */ 354457a0227fSdrh #ifndef uint32 354557a0227fSdrh # define uint32 unsigned int 354657a0227fSdrh #endif 354757a0227fSdrh 354857a0227fSdrh struct MD5Context { 354957a0227fSdrh int isInit; 355057a0227fSdrh uint32 buf[4]; 355157a0227fSdrh uint32 bits[2]; 355257a0227fSdrh unsigned char in[64]; 355357a0227fSdrh }; 355457a0227fSdrh typedef struct MD5Context MD5Context; 355557a0227fSdrh 355657a0227fSdrh /* 355757a0227fSdrh * Note: this code is harmless on little-endian machines. 355857a0227fSdrh */ 355957a0227fSdrh static void byteReverse (unsigned char *buf, unsigned longs){ 356057a0227fSdrh uint32 t; 356157a0227fSdrh do { 356257a0227fSdrh t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 | 356357a0227fSdrh ((unsigned)buf[1]<<8 | buf[0]); 356457a0227fSdrh *(uint32 *)buf = t; 356557a0227fSdrh buf += 4; 356657a0227fSdrh } while (--longs); 356757a0227fSdrh } 356857a0227fSdrh /* The four core functions - F1 is optimized somewhat */ 356957a0227fSdrh 357057a0227fSdrh /* #define F1(x, y, z) (x & y | ~x & z) */ 357157a0227fSdrh #define F1(x, y, z) (z ^ (x & (y ^ z))) 357257a0227fSdrh #define F2(x, y, z) F1(z, x, y) 357357a0227fSdrh #define F3(x, y, z) (x ^ y ^ z) 357457a0227fSdrh #define F4(x, y, z) (y ^ (x | ~z)) 357557a0227fSdrh 357657a0227fSdrh /* This is the central step in the MD5 algorithm. */ 357757a0227fSdrh #define MD5STEP(f, w, x, y, z, data, s) \ 357857a0227fSdrh ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) 357957a0227fSdrh 358057a0227fSdrh /* 358157a0227fSdrh * The core of the MD5 algorithm, this alters an existing MD5 hash to 358257a0227fSdrh * reflect the addition of 16 longwords of new data. MD5Update blocks 358357a0227fSdrh * the data and converts bytes into longwords for this routine. 358457a0227fSdrh */ 358557a0227fSdrh static void MD5Transform(uint32 buf[4], const uint32 in[16]){ 358657a0227fSdrh register uint32 a, b, c, d; 358757a0227fSdrh 358857a0227fSdrh a = buf[0]; 358957a0227fSdrh b = buf[1]; 359057a0227fSdrh c = buf[2]; 359157a0227fSdrh d = buf[3]; 359257a0227fSdrh 359357a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); 359457a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); 359557a0227fSdrh MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); 359657a0227fSdrh MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); 359757a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); 359857a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); 359957a0227fSdrh MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); 360057a0227fSdrh MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); 360157a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); 360257a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); 360357a0227fSdrh MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); 360457a0227fSdrh MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); 360557a0227fSdrh MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); 360657a0227fSdrh MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); 360757a0227fSdrh MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); 360857a0227fSdrh MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); 360957a0227fSdrh 361057a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); 361157a0227fSdrh MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); 361257a0227fSdrh MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); 361357a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); 361457a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); 361557a0227fSdrh MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); 361657a0227fSdrh MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); 361757a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); 361857a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); 361957a0227fSdrh MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); 362057a0227fSdrh MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); 362157a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); 362257a0227fSdrh MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); 362357a0227fSdrh MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); 362457a0227fSdrh MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); 362557a0227fSdrh MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); 362657a0227fSdrh 362757a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); 362857a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); 362957a0227fSdrh MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); 363057a0227fSdrh MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); 363157a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); 363257a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); 363357a0227fSdrh MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); 363457a0227fSdrh MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); 363557a0227fSdrh MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); 363657a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); 363757a0227fSdrh MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); 363857a0227fSdrh MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); 363957a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); 364057a0227fSdrh MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); 364157a0227fSdrh MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); 364257a0227fSdrh MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); 364357a0227fSdrh 364457a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); 364557a0227fSdrh MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); 364657a0227fSdrh MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); 364757a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); 364857a0227fSdrh MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); 364957a0227fSdrh MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); 365057a0227fSdrh MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); 365157a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); 365257a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); 365357a0227fSdrh MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); 365457a0227fSdrh MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); 365557a0227fSdrh MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); 365657a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); 365757a0227fSdrh MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); 365857a0227fSdrh MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); 365957a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); 366057a0227fSdrh 366157a0227fSdrh buf[0] += a; 366257a0227fSdrh buf[1] += b; 366357a0227fSdrh buf[2] += c; 366457a0227fSdrh buf[3] += d; 366557a0227fSdrh } 366657a0227fSdrh 366757a0227fSdrh /* 366857a0227fSdrh * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 366957a0227fSdrh * initialization constants. 367057a0227fSdrh */ 367157a0227fSdrh static void MD5Init(MD5Context *ctx){ 367257a0227fSdrh ctx->isInit = 1; 367357a0227fSdrh ctx->buf[0] = 0x67452301; 367457a0227fSdrh ctx->buf[1] = 0xefcdab89; 367557a0227fSdrh ctx->buf[2] = 0x98badcfe; 367657a0227fSdrh ctx->buf[3] = 0x10325476; 367757a0227fSdrh ctx->bits[0] = 0; 367857a0227fSdrh ctx->bits[1] = 0; 367957a0227fSdrh } 368057a0227fSdrh 368157a0227fSdrh /* 368257a0227fSdrh * Update context to reflect the concatenation of another buffer full 368357a0227fSdrh * of bytes. 368457a0227fSdrh */ 368557a0227fSdrh static 368657a0227fSdrh void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){ 368757a0227fSdrh uint32 t; 368857a0227fSdrh 368957a0227fSdrh /* Update bitcount */ 369057a0227fSdrh 369157a0227fSdrh t = ctx->bits[0]; 369257a0227fSdrh if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) 369357a0227fSdrh ctx->bits[1]++; /* Carry from low to high */ 369457a0227fSdrh ctx->bits[1] += len >> 29; 369557a0227fSdrh 369657a0227fSdrh t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ 369757a0227fSdrh 369857a0227fSdrh /* Handle any leading odd-sized chunks */ 369957a0227fSdrh 370057a0227fSdrh if ( t ) { 370157a0227fSdrh unsigned char *p = (unsigned char *)ctx->in + t; 370257a0227fSdrh 370357a0227fSdrh t = 64-t; 370457a0227fSdrh if (len < t) { 370557a0227fSdrh memcpy(p, buf, len); 370657a0227fSdrh return; 370757a0227fSdrh } 370857a0227fSdrh memcpy(p, buf, t); 370957a0227fSdrh byteReverse(ctx->in, 16); 371057a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 371157a0227fSdrh buf += t; 371257a0227fSdrh len -= t; 371357a0227fSdrh } 371457a0227fSdrh 371557a0227fSdrh /* Process data in 64-byte chunks */ 371657a0227fSdrh 371757a0227fSdrh while (len >= 64) { 371857a0227fSdrh memcpy(ctx->in, buf, 64); 371957a0227fSdrh byteReverse(ctx->in, 16); 372057a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 372157a0227fSdrh buf += 64; 372257a0227fSdrh len -= 64; 372357a0227fSdrh } 372457a0227fSdrh 372557a0227fSdrh /* Handle any remaining bytes of data. */ 372657a0227fSdrh 372757a0227fSdrh memcpy(ctx->in, buf, len); 372857a0227fSdrh } 372957a0227fSdrh 373057a0227fSdrh /* 373157a0227fSdrh * Final wrapup - pad to 64-byte boundary with the bit pattern 373257a0227fSdrh * 1 0* (64-bit count of bits processed, MSB-first) 373357a0227fSdrh */ 373457a0227fSdrh static void MD5Final(unsigned char digest[16], MD5Context *ctx){ 373557a0227fSdrh unsigned count; 373657a0227fSdrh unsigned char *p; 373757a0227fSdrh 373857a0227fSdrh /* Compute number of bytes mod 64 */ 373957a0227fSdrh count = (ctx->bits[0] >> 3) & 0x3F; 374057a0227fSdrh 374157a0227fSdrh /* Set the first char of padding to 0x80. This is safe since there is 374257a0227fSdrh always at least one byte free */ 374357a0227fSdrh p = ctx->in + count; 374457a0227fSdrh *p++ = 0x80; 374557a0227fSdrh 374657a0227fSdrh /* Bytes of padding needed to make 64 bytes */ 374757a0227fSdrh count = 64 - 1 - count; 374857a0227fSdrh 374957a0227fSdrh /* Pad out to 56 mod 64 */ 375057a0227fSdrh if (count < 8) { 375157a0227fSdrh /* Two lots of padding: Pad the first block to 64 bytes */ 375257a0227fSdrh memset(p, 0, count); 375357a0227fSdrh byteReverse(ctx->in, 16); 375457a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 375557a0227fSdrh 375657a0227fSdrh /* Now fill the next block with 56 bytes */ 375757a0227fSdrh memset(ctx->in, 0, 56); 375857a0227fSdrh } else { 375957a0227fSdrh /* Pad block to 56 bytes */ 376057a0227fSdrh memset(p, 0, count-8); 376157a0227fSdrh } 376257a0227fSdrh byteReverse(ctx->in, 14); 376357a0227fSdrh 376457a0227fSdrh /* Append length in bits and transform */ 3765a47941feSdrh memcpy(ctx->in + 14*4, ctx->bits, 8); 376657a0227fSdrh 376757a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 376857a0227fSdrh byteReverse((unsigned char *)ctx->buf, 4); 376957a0227fSdrh memcpy(digest, ctx->buf, 16); 377057a0227fSdrh } 377157a0227fSdrh 377257a0227fSdrh /* 377357a0227fSdrh ** Convert a 128-bit MD5 digest into a 32-digit base-16 number. 377457a0227fSdrh */ 377557a0227fSdrh static void MD5DigestToBase16(unsigned char *digest, char *zBuf){ 377657a0227fSdrh static char const zEncode[] = "0123456789abcdef"; 377757a0227fSdrh int i, j; 377857a0227fSdrh 377957a0227fSdrh for(j=i=0; i<16; i++){ 378057a0227fSdrh int a = digest[i]; 378157a0227fSdrh zBuf[j++] = zEncode[(a>>4)&0xf]; 378257a0227fSdrh zBuf[j++] = zEncode[a & 0xf]; 378357a0227fSdrh } 378457a0227fSdrh zBuf[j] = 0; 378557a0227fSdrh } 378657a0227fSdrh 378757a0227fSdrh 378857a0227fSdrh /* 378957a0227fSdrh ** Convert a 128-bit MD5 digest into sequency of eight 5-digit integers 379057a0227fSdrh ** each representing 16 bits of the digest and separated from each 379157a0227fSdrh ** other by a "-" character. 379257a0227fSdrh */ 379357a0227fSdrh static void MD5DigestToBase10x8(unsigned char digest[16], char zDigest[50]){ 379457a0227fSdrh int i, j; 379557a0227fSdrh unsigned int x; 379657a0227fSdrh for(i=j=0; i<16; i+=2){ 379757a0227fSdrh x = digest[i]*256 + digest[i+1]; 379857a0227fSdrh if( i>0 ) zDigest[j++] = '-'; 379905f6c67cSdrh sqlite3_snprintf(50-j, &zDigest[j], "%05u", x); 380057a0227fSdrh j += 5; 380157a0227fSdrh } 380257a0227fSdrh zDigest[j] = 0; 380357a0227fSdrh } 380457a0227fSdrh 380557a0227fSdrh /* 380657a0227fSdrh ** A TCL command for md5. The argument is the text to be hashed. The 380757a0227fSdrh ** Result is the hash in base64. 380857a0227fSdrh */ 380944e95d4fSmistachkin static int SQLITE_TCLAPI md5_cmd( 381044e95d4fSmistachkin void*cd, 381144e95d4fSmistachkin Tcl_Interp *interp, 381244e95d4fSmistachkin int argc, 381344e95d4fSmistachkin const char **argv 381444e95d4fSmistachkin ){ 381557a0227fSdrh MD5Context ctx; 381657a0227fSdrh unsigned char digest[16]; 381757a0227fSdrh char zBuf[50]; 381857a0227fSdrh void (*converter)(unsigned char*, char*); 381957a0227fSdrh 382057a0227fSdrh if( argc!=2 ){ 382157a0227fSdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 3822a198f2b5Sdrh " TEXT\"", (char*)0); 382357a0227fSdrh return TCL_ERROR; 382457a0227fSdrh } 382557a0227fSdrh MD5Init(&ctx); 382657a0227fSdrh MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1])); 382757a0227fSdrh MD5Final(digest, &ctx); 382857a0227fSdrh converter = (void(*)(unsigned char*,char*))cd; 382957a0227fSdrh converter(digest, zBuf); 383057a0227fSdrh Tcl_AppendResult(interp, zBuf, (char*)0); 383157a0227fSdrh return TCL_OK; 383257a0227fSdrh } 383357a0227fSdrh 383457a0227fSdrh /* 383557a0227fSdrh ** A TCL command to take the md5 hash of a file. The argument is the 383657a0227fSdrh ** name of the file. 383757a0227fSdrh */ 383844e95d4fSmistachkin static int SQLITE_TCLAPI md5file_cmd( 383944e95d4fSmistachkin void*cd, 384044e95d4fSmistachkin Tcl_Interp *interp, 384144e95d4fSmistachkin int argc, 384244e95d4fSmistachkin const char **argv 384344e95d4fSmistachkin ){ 384457a0227fSdrh FILE *in; 384557a0227fSdrh MD5Context ctx; 384657a0227fSdrh void (*converter)(unsigned char*, char*); 384757a0227fSdrh unsigned char digest[16]; 384857a0227fSdrh char zBuf[10240]; 384957a0227fSdrh 385057a0227fSdrh if( argc!=2 ){ 385157a0227fSdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 3852a198f2b5Sdrh " FILENAME\"", (char*)0); 385357a0227fSdrh return TCL_ERROR; 385457a0227fSdrh } 385557a0227fSdrh in = fopen(argv[1],"rb"); 385657a0227fSdrh if( in==0 ){ 385757a0227fSdrh Tcl_AppendResult(interp,"unable to open file \"", argv[1], 3858a198f2b5Sdrh "\" for reading", (char*)0); 385957a0227fSdrh return TCL_ERROR; 386057a0227fSdrh } 386157a0227fSdrh MD5Init(&ctx); 386257a0227fSdrh for(;;){ 386357a0227fSdrh int n; 386483cc1392Sdrh n = (int)fread(zBuf, 1, sizeof(zBuf), in); 386557a0227fSdrh if( n<=0 ) break; 386657a0227fSdrh MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n); 386757a0227fSdrh } 386857a0227fSdrh fclose(in); 386957a0227fSdrh MD5Final(digest, &ctx); 387057a0227fSdrh converter = (void(*)(unsigned char*,char*))cd; 387157a0227fSdrh converter(digest, zBuf); 387257a0227fSdrh Tcl_AppendResult(interp, zBuf, (char*)0); 387357a0227fSdrh return TCL_OK; 387457a0227fSdrh } 387557a0227fSdrh 387657a0227fSdrh /* 387757a0227fSdrh ** Register the four new TCL commands for generating MD5 checksums 387857a0227fSdrh ** with the TCL interpreter. 387957a0227fSdrh */ 388057a0227fSdrh int Md5_Init(Tcl_Interp *interp){ 388157a0227fSdrh Tcl_CreateCommand(interp, "md5", (Tcl_CmdProc*)md5_cmd, 388257a0227fSdrh MD5DigestToBase16, 0); 388357a0227fSdrh Tcl_CreateCommand(interp, "md5-10x8", (Tcl_CmdProc*)md5_cmd, 388457a0227fSdrh MD5DigestToBase10x8, 0); 388557a0227fSdrh Tcl_CreateCommand(interp, "md5file", (Tcl_CmdProc*)md5file_cmd, 388657a0227fSdrh MD5DigestToBase16, 0); 388757a0227fSdrh Tcl_CreateCommand(interp, "md5file-10x8", (Tcl_CmdProc*)md5file_cmd, 388857a0227fSdrh MD5DigestToBase10x8, 0); 388957a0227fSdrh return TCL_OK; 389057a0227fSdrh } 389157a0227fSdrh #endif /* defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) */ 389257a0227fSdrh 389357a0227fSdrh #if defined(SQLITE_TEST) 389457a0227fSdrh /* 389557a0227fSdrh ** During testing, the special md5sum() aggregate function is available. 389657a0227fSdrh ** inside SQLite. The following routines implement that function. 389757a0227fSdrh */ 389857a0227fSdrh static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){ 389957a0227fSdrh MD5Context *p; 390057a0227fSdrh int i; 390157a0227fSdrh if( argc<1 ) return; 390257a0227fSdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 390357a0227fSdrh if( p==0 ) return; 390457a0227fSdrh if( !p->isInit ){ 390557a0227fSdrh MD5Init(p); 390657a0227fSdrh } 390757a0227fSdrh for(i=0; i<argc; i++){ 390857a0227fSdrh const char *zData = (char*)sqlite3_value_text(argv[i]); 390957a0227fSdrh if( zData ){ 391083cc1392Sdrh MD5Update(p, (unsigned char*)zData, (int)strlen(zData)); 391157a0227fSdrh } 391257a0227fSdrh } 391357a0227fSdrh } 391457a0227fSdrh static void md5finalize(sqlite3_context *context){ 391557a0227fSdrh MD5Context *p; 391657a0227fSdrh unsigned char digest[16]; 391757a0227fSdrh char zBuf[33]; 391857a0227fSdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 391957a0227fSdrh MD5Final(digest,p); 392057a0227fSdrh MD5DigestToBase16(digest, zBuf); 392157a0227fSdrh sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 392257a0227fSdrh } 392344e95d4fSmistachkin int Md5_Register( 392444e95d4fSmistachkin sqlite3 *db, 392544e95d4fSmistachkin char **pzErrMsg, 3926*85bd982bSmistachkin const sqlite3_api_routines *pThunk 392744e95d4fSmistachkin ){ 392857a0227fSdrh int rc = sqlite3_create_function(db, "md5sum", -1, SQLITE_UTF8, 0, 0, 392957a0227fSdrh md5step, md5finalize); 393057a0227fSdrh sqlite3_overload_function(db, "md5sum", -1); /* To exercise this API */ 393157a0227fSdrh return rc; 393257a0227fSdrh } 393357a0227fSdrh #endif /* defined(SQLITE_TEST) */ 393457a0227fSdrh 393557a0227fSdrh 3936348784efSdrh /* 39373e27c026Sdrh ** If the macro TCLSH is one, then put in code this for the 39383e27c026Sdrh ** "main" routine that will initialize Tcl and take input from 39393570ad93Sdrh ** standard input, or if a file is named on the command line 39403570ad93Sdrh ** the TCL interpreter reads and evaluates that file. 3941348784efSdrh */ 39423e27c026Sdrh #if TCLSH==1 39430ae479dfSdan static const char *tclsh_main_loop(void){ 39440ae479dfSdan static const char zMainloop[] = 3945348784efSdrh "set line {}\n" 3946348784efSdrh "while {![eof stdin]} {\n" 3947348784efSdrh "if {$line!=\"\"} {\n" 3948348784efSdrh "puts -nonewline \"> \"\n" 3949348784efSdrh "} else {\n" 3950348784efSdrh "puts -nonewline \"% \"\n" 3951348784efSdrh "}\n" 3952348784efSdrh "flush stdout\n" 3953348784efSdrh "append line [gets stdin]\n" 3954348784efSdrh "if {[info complete $line]} {\n" 3955348784efSdrh "if {[catch {uplevel #0 $line} result]} {\n" 3956348784efSdrh "puts stderr \"Error: $result\"\n" 3957348784efSdrh "} elseif {$result!=\"\"} {\n" 3958348784efSdrh "puts $result\n" 3959348784efSdrh "}\n" 3960348784efSdrh "set line {}\n" 3961348784efSdrh "} else {\n" 3962348784efSdrh "append line \\n\n" 3963348784efSdrh "}\n" 3964348784efSdrh "}\n" 3965348784efSdrh ; 39660ae479dfSdan return zMainloop; 39670ae479dfSdan } 39683e27c026Sdrh #endif 39693a0f13ffSdrh #if TCLSH==2 39700ae479dfSdan static const char *tclsh_main_loop(void); 39713a0f13ffSdrh #endif 39723e27c026Sdrh 3973c1a60c51Sdan #ifdef SQLITE_TEST 3974c1a60c51Sdan static void init_all(Tcl_Interp *); 39757617e4a8Smistachkin static int SQLITE_TCLAPI init_all_cmd( 3976c1a60c51Sdan ClientData cd, 3977c1a60c51Sdan Tcl_Interp *interp, 3978c1a60c51Sdan int objc, 3979c1a60c51Sdan Tcl_Obj *CONST objv[] 3980c1a60c51Sdan ){ 39810a549071Sdanielk1977 3982c1a60c51Sdan Tcl_Interp *slave; 3983c1a60c51Sdan if( objc!=2 ){ 3984c1a60c51Sdan Tcl_WrongNumArgs(interp, 1, objv, "SLAVE"); 3985c1a60c51Sdan return TCL_ERROR; 3986c1a60c51Sdan } 39870a549071Sdanielk1977 3988c1a60c51Sdan slave = Tcl_GetSlave(interp, Tcl_GetString(objv[1])); 3989c1a60c51Sdan if( !slave ){ 3990c1a60c51Sdan return TCL_ERROR; 3991c1a60c51Sdan } 3992c1a60c51Sdan 3993c1a60c51Sdan init_all(slave); 3994c1a60c51Sdan return TCL_OK; 3995c1a60c51Sdan } 3996c431fd55Sdan 3997c431fd55Sdan /* 3998c431fd55Sdan ** Tclcmd: db_use_legacy_prepare DB BOOLEAN 3999c431fd55Sdan ** 4000c431fd55Sdan ** The first argument to this command must be a database command created by 4001c431fd55Sdan ** [sqlite3]. If the second argument is true, then the handle is configured 4002c431fd55Sdan ** to use the sqlite3_prepare_v2() function to prepare statements. If it 4003c431fd55Sdan ** is false, sqlite3_prepare(). 4004c431fd55Sdan */ 40057617e4a8Smistachkin static int SQLITE_TCLAPI db_use_legacy_prepare_cmd( 4006c431fd55Sdan ClientData cd, 4007c431fd55Sdan Tcl_Interp *interp, 4008c431fd55Sdan int objc, 4009c431fd55Sdan Tcl_Obj *CONST objv[] 4010c431fd55Sdan ){ 4011c431fd55Sdan Tcl_CmdInfo cmdInfo; 4012c431fd55Sdan SqliteDb *pDb; 4013c431fd55Sdan int bPrepare; 4014c431fd55Sdan 4015c431fd55Sdan if( objc!=3 ){ 4016c431fd55Sdan Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN"); 4017c431fd55Sdan return TCL_ERROR; 4018c431fd55Sdan } 4019c431fd55Sdan 4020c431fd55Sdan if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ 4021c431fd55Sdan Tcl_AppendResult(interp, "no such db: ", Tcl_GetString(objv[1]), (char*)0); 4022c431fd55Sdan return TCL_ERROR; 4023c431fd55Sdan } 4024c431fd55Sdan pDb = (SqliteDb*)cmdInfo.objClientData; 4025c431fd55Sdan if( Tcl_GetBooleanFromObj(interp, objv[2], &bPrepare) ){ 4026c431fd55Sdan return TCL_ERROR; 4027c431fd55Sdan } 4028c431fd55Sdan 4029c431fd55Sdan pDb->bLegacyPrepare = bPrepare; 4030c431fd55Sdan 4031c431fd55Sdan Tcl_ResetResult(interp); 4032c431fd55Sdan return TCL_OK; 4033c431fd55Sdan } 403404489b6dSdan 403504489b6dSdan /* 403604489b6dSdan ** Tclcmd: db_last_stmt_ptr DB 403704489b6dSdan ** 403804489b6dSdan ** If the statement cache associated with database DB is not empty, 403904489b6dSdan ** return the text representation of the most recently used statement 404004489b6dSdan ** handle. 404104489b6dSdan */ 40427617e4a8Smistachkin static int SQLITE_TCLAPI db_last_stmt_ptr( 404304489b6dSdan ClientData cd, 404404489b6dSdan Tcl_Interp *interp, 404504489b6dSdan int objc, 404604489b6dSdan Tcl_Obj *CONST objv[] 404704489b6dSdan ){ 404804489b6dSdan extern int sqlite3TestMakePointerStr(Tcl_Interp*, char*, void*); 404904489b6dSdan Tcl_CmdInfo cmdInfo; 405004489b6dSdan SqliteDb *pDb; 405104489b6dSdan sqlite3_stmt *pStmt = 0; 405204489b6dSdan char zBuf[100]; 405304489b6dSdan 405404489b6dSdan if( objc!=2 ){ 405504489b6dSdan Tcl_WrongNumArgs(interp, 1, objv, "DB"); 405604489b6dSdan return TCL_ERROR; 405704489b6dSdan } 405804489b6dSdan 405904489b6dSdan if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ 406004489b6dSdan Tcl_AppendResult(interp, "no such db: ", Tcl_GetString(objv[1]), (char*)0); 406104489b6dSdan return TCL_ERROR; 406204489b6dSdan } 406304489b6dSdan pDb = (SqliteDb*)cmdInfo.objClientData; 406404489b6dSdan 406504489b6dSdan if( pDb->stmtList ) pStmt = pDb->stmtList->pStmt; 406604489b6dSdan if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ){ 406704489b6dSdan return TCL_ERROR; 406804489b6dSdan } 406904489b6dSdan Tcl_SetResult(interp, zBuf, TCL_VOLATILE); 407004489b6dSdan 407104489b6dSdan return TCL_OK; 407204489b6dSdan } 40731a4a680aSdrh #endif /* SQLITE_TEST */ 40741a4a680aSdrh 4075c1a60c51Sdan /* 4076c1a60c51Sdan ** Configure the interpreter passed as the first argument to have access 4077c1a60c51Sdan ** to the commands and linked variables that make up: 4078c1a60c51Sdan ** 4079c1a60c51Sdan ** * the [sqlite3] extension itself, 4080c1a60c51Sdan ** 4081c1a60c51Sdan ** * If SQLITE_TCLMD5 or SQLITE_TEST is defined, the Md5 commands, and 4082c1a60c51Sdan ** 4083c1a60c51Sdan ** * If SQLITE_TEST is set, the various test interfaces used by the Tcl 4084c1a60c51Sdan ** test suite. 4085c1a60c51Sdan */ 4086c1a60c51Sdan static void init_all(Tcl_Interp *interp){ 408738f8271fSdrh Sqlite3_Init(interp); 4088c1a60c51Sdan 408957a0227fSdrh #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) 409057a0227fSdrh Md5_Init(interp); 409157a0227fSdrh #endif 4092c1a60c51Sdan 4093d9b0257aSdrh #ifdef SQLITE_TEST 4094d1bf3512Sdrh { 40952f999a67Sdrh extern int Sqliteconfig_Init(Tcl_Interp*); 4096d1bf3512Sdrh extern int Sqlitetest1_Init(Tcl_Interp*); 40975c4d9703Sdrh extern int Sqlitetest2_Init(Tcl_Interp*); 40985c4d9703Sdrh extern int Sqlitetest3_Init(Tcl_Interp*); 4099a6064dcfSdrh extern int Sqlitetest4_Init(Tcl_Interp*); 4100998b56c3Sdanielk1977 extern int Sqlitetest5_Init(Tcl_Interp*); 41019c06c953Sdrh extern int Sqlitetest6_Init(Tcl_Interp*); 410229c636bcSdrh extern int Sqlitetest7_Init(Tcl_Interp*); 4103b9bb7c18Sdrh extern int Sqlitetest8_Init(Tcl_Interp*); 4104a713f2c3Sdanielk1977 extern int Sqlitetest9_Init(Tcl_Interp*); 41052366940dSdrh extern int Sqlitetestasync_Init(Tcl_Interp*); 41061409be69Sdrh extern int Sqlitetest_autoext_Init(Tcl_Interp*); 4107b391b944Sdan extern int Sqlitetest_blob_Init(Tcl_Interp*); 41080a7a9155Sdan extern int Sqlitetest_demovfs_Init(Tcl_Interp *); 4109984bfaa4Sdrh extern int Sqlitetest_func_Init(Tcl_Interp*); 411015926590Sdrh extern int Sqlitetest_hexio_Init(Tcl_Interp*); 4111e1ab2193Sdan extern int Sqlitetest_init_Init(Tcl_Interp*); 41122f999a67Sdrh extern int Sqlitetest_malloc_Init(Tcl_Interp*); 41131a9ed0b2Sdanielk1977 extern int Sqlitetest_mutex_Init(Tcl_Interp*); 41142f999a67Sdrh extern int Sqlitetestschema_Init(Tcl_Interp*); 41152f999a67Sdrh extern int Sqlitetestsse_Init(Tcl_Interp*); 41162f999a67Sdrh extern int Sqlitetesttclvar_Init(Tcl_Interp*); 41179f5ff371Sdan extern int Sqlitetestfs_Init(Tcl_Interp*); 411844918fa0Sdanielk1977 extern int SqlitetestThread_Init(Tcl_Interp*); 4119a15db353Sdanielk1977 extern int SqlitetestOnefile_Init(); 41205d1f5aa6Sdanielk1977 extern int SqlitetestOsinst_Init(Tcl_Interp*); 41210410302eSdanielk1977 extern int Sqlitetestbackup_Init(Tcl_Interp*); 4122522efc62Sdrh extern int Sqlitetestintarray_Init(Tcl_Interp*); 4123c7991bdfSdan extern int Sqlitetestvfs_Init(Tcl_Interp *); 41249508daa9Sdan extern int Sqlitetestrtree_Init(Tcl_Interp*); 41258cf35eb4Sdan extern int Sqlitequota_Init(Tcl_Interp*); 41268a922f75Sshaneh extern int Sqlitemultiplex_Init(Tcl_Interp*); 4127e336b001Sdan extern int SqliteSuperlock_Init(Tcl_Interp*); 4128213ca0a8Sdan extern int SqlitetestSyscall_Init(Tcl_Interp*); 41299b1c62d4Sdrh #if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK) 41304fccf43aSdan extern int TestSession_Init(Tcl_Interp*); 41314fccf43aSdan #endif 413289a89560Sdan extern int Fts5tcl_Init(Tcl_Interp *); 4133cfb8f8d6Sdrh extern int SqliteRbu_Init(Tcl_Interp*); 41348e4251b6Sdan extern int Sqlitetesttcl_Init(Tcl_Interp*); 41356764a700Sdan #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) 413699ebad90Sdan extern int Sqlitetestfts3_Init(Tcl_Interp *interp); 413799ebad90Sdan #endif 413899ebad90Sdan 4139b29010cdSdan #ifdef SQLITE_ENABLE_ZIPVFS 4140b29010cdSdan extern int Zipvfs_Init(Tcl_Interp*); 4141b29010cdSdan Zipvfs_Init(interp); 4142b29010cdSdan #endif 4143b29010cdSdan 41442f999a67Sdrh Sqliteconfig_Init(interp); 41456490bebdSdanielk1977 Sqlitetest1_Init(interp); 41465c4d9703Sdrh Sqlitetest2_Init(interp); 4147de647130Sdrh Sqlitetest3_Init(interp); 4148fc57d7bfSdanielk1977 Sqlitetest4_Init(interp); 4149998b56c3Sdanielk1977 Sqlitetest5_Init(interp); 41509c06c953Sdrh Sqlitetest6_Init(interp); 415129c636bcSdrh Sqlitetest7_Init(interp); 4152b9bb7c18Sdrh Sqlitetest8_Init(interp); 4153a713f2c3Sdanielk1977 Sqlitetest9_Init(interp); 41542366940dSdrh Sqlitetestasync_Init(interp); 41551409be69Sdrh Sqlitetest_autoext_Init(interp); 4156b391b944Sdan Sqlitetest_blob_Init(interp); 41570a7a9155Sdan Sqlitetest_demovfs_Init(interp); 4158984bfaa4Sdrh Sqlitetest_func_Init(interp); 415915926590Sdrh Sqlitetest_hexio_Init(interp); 4160e1ab2193Sdan Sqlitetest_init_Init(interp); 41612f999a67Sdrh Sqlitetest_malloc_Init(interp); 41621a9ed0b2Sdanielk1977 Sqlitetest_mutex_Init(interp); 41632f999a67Sdrh Sqlitetestschema_Init(interp); 41642f999a67Sdrh Sqlitetesttclvar_Init(interp); 41659f5ff371Sdan Sqlitetestfs_Init(interp); 416644918fa0Sdanielk1977 SqlitetestThread_Init(interp); 416752b1dbb5Smistachkin SqlitetestOnefile_Init(); 41685d1f5aa6Sdanielk1977 SqlitetestOsinst_Init(interp); 41690410302eSdanielk1977 Sqlitetestbackup_Init(interp); 4170522efc62Sdrh Sqlitetestintarray_Init(interp); 4171c7991bdfSdan Sqlitetestvfs_Init(interp); 41729508daa9Sdan Sqlitetestrtree_Init(interp); 41738cf35eb4Sdan Sqlitequota_Init(interp); 41748a922f75Sshaneh Sqlitemultiplex_Init(interp); 4175e336b001Sdan SqliteSuperlock_Init(interp); 4176213ca0a8Sdan SqlitetestSyscall_Init(interp); 41779b1c62d4Sdrh #if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK) 41784fccf43aSdan TestSession_Init(interp); 41794fccf43aSdan #endif 418089a89560Sdan Fts5tcl_Init(interp); 4181cfb8f8d6Sdrh SqliteRbu_Init(interp); 41828e4251b6Sdan Sqlitetesttcl_Init(interp); 4183a15db353Sdanielk1977 41846764a700Sdan #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) 418599ebad90Sdan Sqlitetestfts3_Init(interp); 418699ebad90Sdan #endif 41872e66f0b9Sdrh 4188c431fd55Sdan Tcl_CreateObjCommand( 4189c431fd55Sdan interp, "load_testfixture_extensions", init_all_cmd, 0, 0 4190c431fd55Sdan ); 4191c431fd55Sdan Tcl_CreateObjCommand( 4192c431fd55Sdan interp, "db_use_legacy_prepare", db_use_legacy_prepare_cmd, 0, 0 4193c431fd55Sdan ); 419404489b6dSdan Tcl_CreateObjCommand( 419504489b6dSdan interp, "db_last_stmt_ptr", db_last_stmt_ptr, 0, 0 419604489b6dSdan ); 4197c1a60c51Sdan 41983e27c026Sdrh #ifdef SQLITE_SSE 4199348784efSdrh Sqlitetestsse_Init(interp); 4200348784efSdrh #endif 4201348784efSdrh } 420261212b69Sdrh #endif 4203c1a60c51Sdan } 4204c1a60c51Sdan 4205db6bafaeSdrh /* Needed for the setrlimit() system call on unix */ 4206db6bafaeSdrh #if defined(unix) 4207db6bafaeSdrh #include <sys/resource.h> 4208db6bafaeSdrh #endif 4209db6bafaeSdrh 4210c1a60c51Sdan #define TCLSH_MAIN main /* Needed to fake out mktclapp */ 421169def7ffSmistachkin int SQLITE_CDECL TCLSH_MAIN(int argc, char **argv){ 4212c1a60c51Sdan Tcl_Interp *interp; 4213c1a60c51Sdan 42141f28e070Smistachkin #if !defined(_WIN32_WCE) 42151f28e070Smistachkin if( getenv("BREAK") ){ 42161f28e070Smistachkin fprintf(stderr, 42171f28e070Smistachkin "attach debugger to process %d and press any key to continue.\n", 42181f28e070Smistachkin GETPID()); 42191f28e070Smistachkin fgetc(stdin); 42201f28e070Smistachkin } 42211f28e070Smistachkin #endif 42221f28e070Smistachkin 4223db6bafaeSdrh /* Since the primary use case for this binary is testing of SQLite, 4224db6bafaeSdrh ** be sure to generate core files if we crash */ 4225db6bafaeSdrh #if defined(SQLITE_TEST) && defined(unix) 4226db6bafaeSdrh { struct rlimit x; 4227db6bafaeSdrh getrlimit(RLIMIT_CORE, &x); 4228db6bafaeSdrh x.rlim_cur = x.rlim_max; 4229db6bafaeSdrh setrlimit(RLIMIT_CORE, &x); 4230db6bafaeSdrh } 4231db6bafaeSdrh #endif /* SQLITE_TEST && unix */ 4232db6bafaeSdrh 4233db6bafaeSdrh 4234c1a60c51Sdan /* Call sqlite3_shutdown() once before doing anything else. This is to 4235c1a60c51Sdan ** test that sqlite3_shutdown() can be safely called by a process before 4236c1a60c51Sdan ** sqlite3_initialize() is. */ 4237c1a60c51Sdan sqlite3_shutdown(); 4238c1a60c51Sdan 42390ae479dfSdan Tcl_FindExecutable(argv[0]); 42402953ba9eSmistachkin Tcl_SetSystemEncoding(NULL, "utf-8"); 42410ae479dfSdan interp = Tcl_CreateInterp(); 42420ae479dfSdan 42433a0f13ffSdrh #if TCLSH==2 42443a0f13ffSdrh sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); 42453a0f13ffSdrh #endif 4246c1a60c51Sdan 4247c1a60c51Sdan init_all(interp); 4248c7285978Sdrh if( argc>=2 ){ 4249348784efSdrh int i; 4250ad42c3a3Sshess char zArgc[32]; 4251ad42c3a3Sshess sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH)); 4252ad42c3a3Sshess Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY); 4253348784efSdrh Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); 4254348784efSdrh Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); 4255348784efSdrh for(i=3-TCLSH; i<argc; i++){ 4256348784efSdrh Tcl_SetVar(interp, "argv", argv[i], 4257348784efSdrh TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); 4258348784efSdrh } 42593a0f13ffSdrh if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ 42600de8c112Sdrh const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); 4261a81c64a2Sdrh if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp); 4262c61053b7Sdrh fprintf(stderr,"%s: %s\n", *argv, zInfo); 4263348784efSdrh return 1; 4264348784efSdrh } 42653e27c026Sdrh } 42663a0f13ffSdrh if( TCLSH==2 || argc<=1 ){ 42670ae479dfSdan Tcl_GlobalEval(interp, tclsh_main_loop()); 4268348784efSdrh } 4269348784efSdrh return 0; 4270348784efSdrh } 4271348784efSdrh #endif /* TCLSH */ 4272