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 */ 28bd08af48Sdrh #include "tcl.h" 29b4e9af9fSdanielk1977 #include <errno.h> 306d31316cSdrh 31bd08af48Sdrh /* 32bd08af48Sdrh ** Some additional include files are needed if this file is not 33bd08af48Sdrh ** appended to the amalgamation. 34bd08af48Sdrh */ 35bd08af48Sdrh #ifndef SQLITE_AMALGAMATION 3665e8c82eSdrh # include "sqlite3.h" 3775897234Sdrh # include <stdlib.h> 3875897234Sdrh # include <string.h> 39ce927065Sdrh # include <assert.h> 4065e8c82eSdrh typedef unsigned char u8; 41bd08af48Sdrh #endif 42eb206381Sdrh #include <ctype.h> 4375897234Sdrh 441f28e070Smistachkin /* Used to get the current process ID */ 451f28e070Smistachkin #if !defined(_WIN32) 461f28e070Smistachkin # include <unistd.h> 471f28e070Smistachkin # define GETPID getpid 481f28e070Smistachkin #elif !defined(_WIN32_WCE) 491f28e070Smistachkin # ifndef SQLITE_AMALGAMATION 501f28e070Smistachkin # define WIN32_LEAN_AND_MEAN 511f28e070Smistachkin # include <windows.h> 521f28e070Smistachkin # endif 531f28e070Smistachkin # define GETPID (int)GetCurrentProcessId 541f28e070Smistachkin #endif 551f28e070Smistachkin 56ad6e1370Sdrh /* 57ad6e1370Sdrh * Windows needs to know which symbols to export. Unix does not. 58ad6e1370Sdrh * BUILD_sqlite should be undefined for Unix. 59ad6e1370Sdrh */ 60ad6e1370Sdrh #ifdef BUILD_sqlite 61ad6e1370Sdrh #undef TCL_STORAGE_CLASS 62ad6e1370Sdrh #define TCL_STORAGE_CLASS DLLEXPORT 63ad6e1370Sdrh #endif /* BUILD_sqlite */ 6429bc4615Sdrh 65a21c6b6fSdanielk1977 #define NUM_PREPARED_STMTS 10 66fb7e7651Sdrh #define MAX_PREPARED_STMTS 100 67fb7e7651Sdrh 68c45e6716Sdrh /* Forward declaration */ 69c45e6716Sdrh typedef struct SqliteDb SqliteDb; 7098808babSdrh 7198808babSdrh /* 72cabb0819Sdrh ** New SQL functions can be created as TCL scripts. Each such function 73cabb0819Sdrh ** is described by an instance of the following structure. 74cabb0819Sdrh */ 75cabb0819Sdrh typedef struct SqlFunc SqlFunc; 76cabb0819Sdrh struct SqlFunc { 77cabb0819Sdrh Tcl_Interp *interp; /* The TCL interpret to execute the function */ 78d1e4733dSdrh Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */ 79c45e6716Sdrh SqliteDb *pDb; /* Database connection that owns this function */ 80d1e4733dSdrh int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */ 81d1e4733dSdrh char *zName; /* Name of this function */ 82cabb0819Sdrh SqlFunc *pNext; /* Next function on the list of them all */ 83cabb0819Sdrh }; 84cabb0819Sdrh 85cabb0819Sdrh /* 860202b29eSdanielk1977 ** New collation sequences function can be created as TCL scripts. Each such 870202b29eSdanielk1977 ** function is described by an instance of the following structure. 880202b29eSdanielk1977 */ 890202b29eSdanielk1977 typedef struct SqlCollate SqlCollate; 900202b29eSdanielk1977 struct SqlCollate { 910202b29eSdanielk1977 Tcl_Interp *interp; /* The TCL interpret to execute the function */ 920202b29eSdanielk1977 char *zScript; /* The script to be run */ 930202b29eSdanielk1977 SqlCollate *pNext; /* Next function on the list of them all */ 940202b29eSdanielk1977 }; 950202b29eSdanielk1977 960202b29eSdanielk1977 /* 97fb7e7651Sdrh ** Prepared statements are cached for faster execution. Each prepared 98fb7e7651Sdrh ** statement is described by an instance of the following structure. 99fb7e7651Sdrh */ 100fb7e7651Sdrh typedef struct SqlPreparedStmt SqlPreparedStmt; 101fb7e7651Sdrh struct SqlPreparedStmt { 102fb7e7651Sdrh SqlPreparedStmt *pNext; /* Next in linked list */ 103fb7e7651Sdrh SqlPreparedStmt *pPrev; /* Previous on the list */ 104fb7e7651Sdrh sqlite3_stmt *pStmt; /* The prepared statement */ 105fb7e7651Sdrh int nSql; /* chars in zSql[] */ 106d0e2a854Sdanielk1977 const char *zSql; /* Text of the SQL statement */ 1074a4c11aaSdan int nParm; /* Size of apParm array */ 1084a4c11aaSdan Tcl_Obj **apParm; /* Array of referenced object pointers */ 109fb7e7651Sdrh }; 110fb7e7651Sdrh 111d0441796Sdanielk1977 typedef struct IncrblobChannel IncrblobChannel; 112d0441796Sdanielk1977 113fb7e7651Sdrh /* 114bec3f402Sdrh ** There is one instance of this structure for each SQLite database 115bec3f402Sdrh ** that has been opened by the SQLite TCL interface. 116c431fd55Sdan ** 117c431fd55Sdan ** If this module is built with SQLITE_TEST defined (to create the SQLite 118c431fd55Sdan ** testfixture executable), then it may be configured to use either 119c431fd55Sdan ** sqlite3_prepare_v2() or sqlite3_prepare() to prepare SQL statements. 120c431fd55Sdan ** If SqliteDb.bLegacyPrepare is true, sqlite3_prepare() is used. 121bec3f402Sdrh */ 122bec3f402Sdrh struct SqliteDb { 123dddca286Sdrh sqlite3 *db; /* The "real" database structure. MUST BE FIRST */ 124bec3f402Sdrh Tcl_Interp *interp; /* The interpreter used for this database */ 1256d31316cSdrh char *zBusy; /* The busy callback routine */ 126aa940eacSdrh char *zCommit; /* The commit hook callback routine */ 127b5a20d3cSdrh char *zTrace; /* The trace callback routine */ 12819e2d37fSdrh char *zProfile; /* The profile callback routine */ 129348bb5d6Sdanielk1977 char *zProgress; /* The progress callback routine */ 130e22a334bSdrh char *zAuth; /* The authorization callback routine */ 1311f1549f8Sdrh int disableAuth; /* Disable the authorizer if it exists */ 13255c45f2eSdanielk1977 char *zNull; /* Text to substitute for an SQL NULL value */ 133cabb0819Sdrh SqlFunc *pFunc; /* List of SQL functions */ 13494eb6a14Sdanielk1977 Tcl_Obj *pUpdateHook; /* Update hook script (if any) */ 13571fd80bfSdanielk1977 Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */ 1365def0843Sdrh Tcl_Obj *pWalHook; /* WAL hook script (if any) */ 137404ca075Sdanielk1977 Tcl_Obj *pUnlockNotify; /* Unlock notify script (if any) */ 1380202b29eSdanielk1977 SqlCollate *pCollate; /* List of SQL collation functions */ 1396f8a503dSdanielk1977 int rc; /* Return code of most recent sqlite3_exec() */ 1407cedc8d4Sdanielk1977 Tcl_Obj *pCollateNeeded; /* Collation needed script */ 141fb7e7651Sdrh SqlPreparedStmt *stmtList; /* List of prepared statements*/ 142fb7e7651Sdrh SqlPreparedStmt *stmtLast; /* Last statement in the list */ 143fb7e7651Sdrh int maxStmt; /* The next maximum number of stmtList */ 144fb7e7651Sdrh int nStmt; /* Number of statements in stmtList */ 145d0441796Sdanielk1977 IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */ 1463c379b01Sdrh int nStep, nSort, nIndex; /* Statistics for most recent operation */ 147cd38d520Sdanielk1977 int nTransaction; /* Number of nested [transaction] methods */ 148c431fd55Sdan #ifdef SQLITE_TEST 149c431fd55Sdan int bLegacyPrepare; /* True to use sqlite3_prepare() */ 150c431fd55Sdan #endif 15198808babSdrh }; 152297ecf14Sdrh 153b4e9af9fSdanielk1977 struct IncrblobChannel { 154d0441796Sdanielk1977 sqlite3_blob *pBlob; /* sqlite3 blob handle */ 155dcbb5d3fSdanielk1977 SqliteDb *pDb; /* Associated database connection */ 156b4e9af9fSdanielk1977 int iSeek; /* Current seek offset */ 157d0441796Sdanielk1977 Tcl_Channel channel; /* Channel identifier */ 158d0441796Sdanielk1977 IncrblobChannel *pNext; /* Linked list of all open incrblob channels */ 159d0441796Sdanielk1977 IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */ 160b4e9af9fSdanielk1977 }; 161b4e9af9fSdanielk1977 162ea678832Sdrh /* 163ea678832Sdrh ** Compute a string length that is limited to what can be stored in 164ea678832Sdrh ** lower 30 bits of a 32-bit signed integer. 165ea678832Sdrh */ 1664f21c4afSdrh static int strlen30(const char *z){ 167ea678832Sdrh const char *z2 = z; 168ea678832Sdrh while( *z2 ){ z2++; } 169ea678832Sdrh return 0x3fffffff & (int)(z2 - z); 170ea678832Sdrh } 171ea678832Sdrh 172ea678832Sdrh 17332a0d8bbSdanielk1977 #ifndef SQLITE_OMIT_INCRBLOB 174b4e9af9fSdanielk1977 /* 175d0441796Sdanielk1977 ** Close all incrblob channels opened using database connection pDb. 176d0441796Sdanielk1977 ** This is called when shutting down the database connection. 177d0441796Sdanielk1977 */ 178d0441796Sdanielk1977 static void closeIncrblobChannels(SqliteDb *pDb){ 179d0441796Sdanielk1977 IncrblobChannel *p; 180d0441796Sdanielk1977 IncrblobChannel *pNext; 181d0441796Sdanielk1977 182d0441796Sdanielk1977 for(p=pDb->pIncrblob; p; p=pNext){ 183d0441796Sdanielk1977 pNext = p->pNext; 184d0441796Sdanielk1977 185d0441796Sdanielk1977 /* Note: Calling unregister here call Tcl_Close on the incrblob channel, 186d0441796Sdanielk1977 ** which deletes the IncrblobChannel structure at *p. So do not 187d0441796Sdanielk1977 ** call Tcl_Free() here. 188d0441796Sdanielk1977 */ 189d0441796Sdanielk1977 Tcl_UnregisterChannel(pDb->interp, p->channel); 190d0441796Sdanielk1977 } 191d0441796Sdanielk1977 } 192d0441796Sdanielk1977 193d0441796Sdanielk1977 /* 194b4e9af9fSdanielk1977 ** Close an incremental blob channel. 195b4e9af9fSdanielk1977 */ 196b4e9af9fSdanielk1977 static int incrblobClose(ClientData instanceData, Tcl_Interp *interp){ 197b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 19892d4d7a9Sdanielk1977 int rc = sqlite3_blob_close(p->pBlob); 19992d4d7a9Sdanielk1977 sqlite3 *db = p->pDb->db; 200d0441796Sdanielk1977 201d0441796Sdanielk1977 /* Remove the channel from the SqliteDb.pIncrblob list. */ 202d0441796Sdanielk1977 if( p->pNext ){ 203d0441796Sdanielk1977 p->pNext->pPrev = p->pPrev; 204d0441796Sdanielk1977 } 205d0441796Sdanielk1977 if( p->pPrev ){ 206d0441796Sdanielk1977 p->pPrev->pNext = p->pNext; 207d0441796Sdanielk1977 } 208d0441796Sdanielk1977 if( p->pDb->pIncrblob==p ){ 209d0441796Sdanielk1977 p->pDb->pIncrblob = p->pNext; 210d0441796Sdanielk1977 } 211d0441796Sdanielk1977 21292d4d7a9Sdanielk1977 /* Free the IncrblobChannel structure */ 213b4e9af9fSdanielk1977 Tcl_Free((char *)p); 21492d4d7a9Sdanielk1977 21592d4d7a9Sdanielk1977 if( rc!=SQLITE_OK ){ 21692d4d7a9Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); 21792d4d7a9Sdanielk1977 return TCL_ERROR; 21892d4d7a9Sdanielk1977 } 219b4e9af9fSdanielk1977 return TCL_OK; 220b4e9af9fSdanielk1977 } 221b4e9af9fSdanielk1977 222b4e9af9fSdanielk1977 /* 223b4e9af9fSdanielk1977 ** Read data from an incremental blob channel. 224b4e9af9fSdanielk1977 */ 225b4e9af9fSdanielk1977 static int incrblobInput( 226b4e9af9fSdanielk1977 ClientData instanceData, 227b4e9af9fSdanielk1977 char *buf, 228b4e9af9fSdanielk1977 int bufSize, 229b4e9af9fSdanielk1977 int *errorCodePtr 230b4e9af9fSdanielk1977 ){ 231b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 232b4e9af9fSdanielk1977 int nRead = bufSize; /* Number of bytes to read */ 233b4e9af9fSdanielk1977 int nBlob; /* Total size of the blob */ 234b4e9af9fSdanielk1977 int rc; /* sqlite error code */ 235b4e9af9fSdanielk1977 236b4e9af9fSdanielk1977 nBlob = sqlite3_blob_bytes(p->pBlob); 237b4e9af9fSdanielk1977 if( (p->iSeek+nRead)>nBlob ){ 238b4e9af9fSdanielk1977 nRead = nBlob-p->iSeek; 239b4e9af9fSdanielk1977 } 240b4e9af9fSdanielk1977 if( nRead<=0 ){ 241b4e9af9fSdanielk1977 return 0; 242b4e9af9fSdanielk1977 } 243b4e9af9fSdanielk1977 244b4e9af9fSdanielk1977 rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek); 245b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 246b4e9af9fSdanielk1977 *errorCodePtr = rc; 247b4e9af9fSdanielk1977 return -1; 248b4e9af9fSdanielk1977 } 249b4e9af9fSdanielk1977 250b4e9af9fSdanielk1977 p->iSeek += nRead; 251b4e9af9fSdanielk1977 return nRead; 252b4e9af9fSdanielk1977 } 253b4e9af9fSdanielk1977 254d0441796Sdanielk1977 /* 255d0441796Sdanielk1977 ** Write data to an incremental blob channel. 256d0441796Sdanielk1977 */ 257b4e9af9fSdanielk1977 static int incrblobOutput( 258b4e9af9fSdanielk1977 ClientData instanceData, 259b4e9af9fSdanielk1977 CONST char *buf, 260b4e9af9fSdanielk1977 int toWrite, 261b4e9af9fSdanielk1977 int *errorCodePtr 262b4e9af9fSdanielk1977 ){ 263b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 264b4e9af9fSdanielk1977 int nWrite = toWrite; /* Number of bytes to write */ 265b4e9af9fSdanielk1977 int nBlob; /* Total size of the blob */ 266b4e9af9fSdanielk1977 int rc; /* sqlite error code */ 267b4e9af9fSdanielk1977 268b4e9af9fSdanielk1977 nBlob = sqlite3_blob_bytes(p->pBlob); 269b4e9af9fSdanielk1977 if( (p->iSeek+nWrite)>nBlob ){ 270b4e9af9fSdanielk1977 *errorCodePtr = EINVAL; 271b4e9af9fSdanielk1977 return -1; 272b4e9af9fSdanielk1977 } 273b4e9af9fSdanielk1977 if( nWrite<=0 ){ 274b4e9af9fSdanielk1977 return 0; 275b4e9af9fSdanielk1977 } 276b4e9af9fSdanielk1977 277b4e9af9fSdanielk1977 rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek); 278b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 279b4e9af9fSdanielk1977 *errorCodePtr = EIO; 280b4e9af9fSdanielk1977 return -1; 281b4e9af9fSdanielk1977 } 282b4e9af9fSdanielk1977 283b4e9af9fSdanielk1977 p->iSeek += nWrite; 284b4e9af9fSdanielk1977 return nWrite; 285b4e9af9fSdanielk1977 } 286b4e9af9fSdanielk1977 287b4e9af9fSdanielk1977 /* 288b4e9af9fSdanielk1977 ** Seek an incremental blob channel. 289b4e9af9fSdanielk1977 */ 290b4e9af9fSdanielk1977 static int incrblobSeek( 291b4e9af9fSdanielk1977 ClientData instanceData, 292b4e9af9fSdanielk1977 long offset, 293b4e9af9fSdanielk1977 int seekMode, 294b4e9af9fSdanielk1977 int *errorCodePtr 295b4e9af9fSdanielk1977 ){ 296b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 297b4e9af9fSdanielk1977 298b4e9af9fSdanielk1977 switch( seekMode ){ 299b4e9af9fSdanielk1977 case SEEK_SET: 300b4e9af9fSdanielk1977 p->iSeek = offset; 301b4e9af9fSdanielk1977 break; 302b4e9af9fSdanielk1977 case SEEK_CUR: 303b4e9af9fSdanielk1977 p->iSeek += offset; 304b4e9af9fSdanielk1977 break; 305b4e9af9fSdanielk1977 case SEEK_END: 306b4e9af9fSdanielk1977 p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset; 307b4e9af9fSdanielk1977 break; 308b4e9af9fSdanielk1977 309b4e9af9fSdanielk1977 default: assert(!"Bad seekMode"); 310b4e9af9fSdanielk1977 } 311b4e9af9fSdanielk1977 312b4e9af9fSdanielk1977 return p->iSeek; 313b4e9af9fSdanielk1977 } 314b4e9af9fSdanielk1977 315b4e9af9fSdanielk1977 316b4e9af9fSdanielk1977 static void incrblobWatch(ClientData instanceData, int mode){ 317b4e9af9fSdanielk1977 /* NO-OP */ 318b4e9af9fSdanielk1977 } 319b4e9af9fSdanielk1977 static int incrblobHandle(ClientData instanceData, int dir, ClientData *hPtr){ 320b4e9af9fSdanielk1977 return TCL_ERROR; 321b4e9af9fSdanielk1977 } 322b4e9af9fSdanielk1977 323b4e9af9fSdanielk1977 static Tcl_ChannelType IncrblobChannelType = { 324b4e9af9fSdanielk1977 "incrblob", /* typeName */ 325b4e9af9fSdanielk1977 TCL_CHANNEL_VERSION_2, /* version */ 326b4e9af9fSdanielk1977 incrblobClose, /* closeProc */ 327b4e9af9fSdanielk1977 incrblobInput, /* inputProc */ 328b4e9af9fSdanielk1977 incrblobOutput, /* outputProc */ 329b4e9af9fSdanielk1977 incrblobSeek, /* seekProc */ 330b4e9af9fSdanielk1977 0, /* setOptionProc */ 331b4e9af9fSdanielk1977 0, /* getOptionProc */ 332b4e9af9fSdanielk1977 incrblobWatch, /* watchProc (this is a no-op) */ 333b4e9af9fSdanielk1977 incrblobHandle, /* getHandleProc (always returns error) */ 334b4e9af9fSdanielk1977 0, /* close2Proc */ 335b4e9af9fSdanielk1977 0, /* blockModeProc */ 336b4e9af9fSdanielk1977 0, /* flushProc */ 337b4e9af9fSdanielk1977 0, /* handlerProc */ 338b4e9af9fSdanielk1977 0, /* wideSeekProc */ 339b4e9af9fSdanielk1977 }; 340b4e9af9fSdanielk1977 341b4e9af9fSdanielk1977 /* 342b4e9af9fSdanielk1977 ** Create a new incrblob channel. 343b4e9af9fSdanielk1977 */ 344b4e9af9fSdanielk1977 static int createIncrblobChannel( 345b4e9af9fSdanielk1977 Tcl_Interp *interp, 346b4e9af9fSdanielk1977 SqliteDb *pDb, 347b4e9af9fSdanielk1977 const char *zDb, 348b4e9af9fSdanielk1977 const char *zTable, 349b4e9af9fSdanielk1977 const char *zColumn, 3508cbadb02Sdanielk1977 sqlite_int64 iRow, 3518cbadb02Sdanielk1977 int isReadonly 352b4e9af9fSdanielk1977 ){ 353b4e9af9fSdanielk1977 IncrblobChannel *p; 3548cbadb02Sdanielk1977 sqlite3 *db = pDb->db; 355b4e9af9fSdanielk1977 sqlite3_blob *pBlob; 356b4e9af9fSdanielk1977 int rc; 3578cbadb02Sdanielk1977 int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE); 358b4e9af9fSdanielk1977 359b4e9af9fSdanielk1977 /* This variable is used to name the channels: "incrblob_[incr count]" */ 360b4e9af9fSdanielk1977 static int count = 0; 361b4e9af9fSdanielk1977 char zChannel[64]; 362b4e9af9fSdanielk1977 3638cbadb02Sdanielk1977 rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob); 364b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 365b4e9af9fSdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 366b4e9af9fSdanielk1977 return TCL_ERROR; 367b4e9af9fSdanielk1977 } 368b4e9af9fSdanielk1977 369b4e9af9fSdanielk1977 p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel)); 370b4e9af9fSdanielk1977 p->iSeek = 0; 371b4e9af9fSdanielk1977 p->pBlob = pBlob; 372b4e9af9fSdanielk1977 3735bb3eb9bSdrh sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count); 374d0441796Sdanielk1977 p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags); 375d0441796Sdanielk1977 Tcl_RegisterChannel(interp, p->channel); 376b4e9af9fSdanielk1977 377d0441796Sdanielk1977 /* Link the new channel into the SqliteDb.pIncrblob list. */ 378d0441796Sdanielk1977 p->pNext = pDb->pIncrblob; 379d0441796Sdanielk1977 p->pPrev = 0; 380d0441796Sdanielk1977 if( p->pNext ){ 381d0441796Sdanielk1977 p->pNext->pPrev = p; 382d0441796Sdanielk1977 } 383d0441796Sdanielk1977 pDb->pIncrblob = p; 384d0441796Sdanielk1977 p->pDb = pDb; 385d0441796Sdanielk1977 386d0441796Sdanielk1977 Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE); 387b4e9af9fSdanielk1977 return TCL_OK; 388b4e9af9fSdanielk1977 } 38932a0d8bbSdanielk1977 #else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */ 39032a0d8bbSdanielk1977 #define closeIncrblobChannels(pDb) 39132a0d8bbSdanielk1977 #endif 392b4e9af9fSdanielk1977 3936d31316cSdrh /* 394d1e4733dSdrh ** Look at the script prefix in pCmd. We will be executing this script 395d1e4733dSdrh ** after first appending one or more arguments. This routine analyzes 396d1e4733dSdrh ** the script to see if it is safe to use Tcl_EvalObjv() on the script 397d1e4733dSdrh ** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much 398d1e4733dSdrh ** faster. 399d1e4733dSdrh ** 400d1e4733dSdrh ** Scripts that are safe to use with Tcl_EvalObjv() consists of a 401d1e4733dSdrh ** command name followed by zero or more arguments with no [...] or $ 402d1e4733dSdrh ** or {...} or ; to be seen anywhere. Most callback scripts consist 403d1e4733dSdrh ** of just a single procedure name and they meet this requirement. 404d1e4733dSdrh */ 405d1e4733dSdrh static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){ 406d1e4733dSdrh /* We could try to do something with Tcl_Parse(). But we will instead 407d1e4733dSdrh ** just do a search for forbidden characters. If any of the forbidden 408d1e4733dSdrh ** characters appear in pCmd, we will report the string as unsafe. 409d1e4733dSdrh */ 410d1e4733dSdrh const char *z; 411d1e4733dSdrh int n; 412d1e4733dSdrh z = Tcl_GetStringFromObj(pCmd, &n); 413d1e4733dSdrh while( n-- > 0 ){ 414d1e4733dSdrh int c = *(z++); 415d1e4733dSdrh if( c=='$' || c=='[' || c==';' ) return 0; 416d1e4733dSdrh } 417d1e4733dSdrh return 1; 418d1e4733dSdrh } 419d1e4733dSdrh 420d1e4733dSdrh /* 421d1e4733dSdrh ** Find an SqlFunc structure with the given name. Or create a new 422d1e4733dSdrh ** one if an existing one cannot be found. Return a pointer to the 423d1e4733dSdrh ** structure. 424d1e4733dSdrh */ 425d1e4733dSdrh static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){ 426d1e4733dSdrh SqlFunc *p, *pNew; 4270425f189Sdrh int nName = strlen30(zName); 4280425f189Sdrh pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + nName + 1 ); 429d1e4733dSdrh pNew->zName = (char*)&pNew[1]; 4300425f189Sdrh memcpy(pNew->zName, zName, nName+1); 431d1e4733dSdrh for(p=pDb->pFunc; p; p=p->pNext){ 4320425f189Sdrh if( sqlite3_stricmp(p->zName, pNew->zName)==0 ){ 433d1e4733dSdrh Tcl_Free((char*)pNew); 434d1e4733dSdrh return p; 435d1e4733dSdrh } 436d1e4733dSdrh } 437d1e4733dSdrh pNew->interp = pDb->interp; 438c45e6716Sdrh pNew->pDb = pDb; 439d1e4733dSdrh pNew->pScript = 0; 440d1e4733dSdrh pNew->pNext = pDb->pFunc; 441d1e4733dSdrh pDb->pFunc = pNew; 442d1e4733dSdrh return pNew; 443d1e4733dSdrh } 444d1e4733dSdrh 445d1e4733dSdrh /* 446c431fd55Sdan ** Free a single SqlPreparedStmt object. 447c431fd55Sdan */ 448c431fd55Sdan static void dbFreeStmt(SqlPreparedStmt *pStmt){ 449c431fd55Sdan #ifdef SQLITE_TEST 450c431fd55Sdan if( sqlite3_sql(pStmt->pStmt)==0 ){ 451c431fd55Sdan Tcl_Free((char *)pStmt->zSql); 452c431fd55Sdan } 453c431fd55Sdan #endif 454c431fd55Sdan sqlite3_finalize(pStmt->pStmt); 455c431fd55Sdan Tcl_Free((char *)pStmt); 456c431fd55Sdan } 457c431fd55Sdan 458c431fd55Sdan /* 459fb7e7651Sdrh ** Finalize and free a list of prepared statements 460fb7e7651Sdrh */ 461fb7e7651Sdrh static void flushStmtCache(SqliteDb *pDb){ 462fb7e7651Sdrh SqlPreparedStmt *pPreStmt; 463c431fd55Sdan SqlPreparedStmt *pNext; 464fb7e7651Sdrh 465c431fd55Sdan for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pNext){ 466c431fd55Sdan pNext = pPreStmt->pNext; 467c431fd55Sdan dbFreeStmt(pPreStmt); 468fb7e7651Sdrh } 469fb7e7651Sdrh pDb->nStmt = 0; 470fb7e7651Sdrh pDb->stmtLast = 0; 471c431fd55Sdan pDb->stmtList = 0; 472fb7e7651Sdrh } 473fb7e7651Sdrh 474fb7e7651Sdrh /* 475895d7472Sdrh ** TCL calls this procedure when an sqlite3 database command is 476895d7472Sdrh ** deleted. 47775897234Sdrh */ 47875897234Sdrh static void DbDeleteCmd(void *db){ 479bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)db; 480fb7e7651Sdrh flushStmtCache(pDb); 481d0441796Sdanielk1977 closeIncrblobChannels(pDb); 4826f8a503dSdanielk1977 sqlite3_close(pDb->db); 483cabb0819Sdrh while( pDb->pFunc ){ 484cabb0819Sdrh SqlFunc *pFunc = pDb->pFunc; 485cabb0819Sdrh pDb->pFunc = pFunc->pNext; 486c45e6716Sdrh assert( pFunc->pDb==pDb ); 487d1e4733dSdrh Tcl_DecrRefCount(pFunc->pScript); 488cabb0819Sdrh Tcl_Free((char*)pFunc); 489cabb0819Sdrh } 4900202b29eSdanielk1977 while( pDb->pCollate ){ 4910202b29eSdanielk1977 SqlCollate *pCollate = pDb->pCollate; 4920202b29eSdanielk1977 pDb->pCollate = pCollate->pNext; 4930202b29eSdanielk1977 Tcl_Free((char*)pCollate); 4940202b29eSdanielk1977 } 495bec3f402Sdrh if( pDb->zBusy ){ 496bec3f402Sdrh Tcl_Free(pDb->zBusy); 497bec3f402Sdrh } 498b5a20d3cSdrh if( pDb->zTrace ){ 499b5a20d3cSdrh Tcl_Free(pDb->zTrace); 5000d1a643aSdrh } 50119e2d37fSdrh if( pDb->zProfile ){ 50219e2d37fSdrh Tcl_Free(pDb->zProfile); 50319e2d37fSdrh } 504e22a334bSdrh if( pDb->zAuth ){ 505e22a334bSdrh Tcl_Free(pDb->zAuth); 506e22a334bSdrh } 50755c45f2eSdanielk1977 if( pDb->zNull ){ 50855c45f2eSdanielk1977 Tcl_Free(pDb->zNull); 50955c45f2eSdanielk1977 } 51094eb6a14Sdanielk1977 if( pDb->pUpdateHook ){ 51194eb6a14Sdanielk1977 Tcl_DecrRefCount(pDb->pUpdateHook); 51294eb6a14Sdanielk1977 } 51371fd80bfSdanielk1977 if( pDb->pRollbackHook ){ 51471fd80bfSdanielk1977 Tcl_DecrRefCount(pDb->pRollbackHook); 51571fd80bfSdanielk1977 } 5165def0843Sdrh if( pDb->pWalHook ){ 5175def0843Sdrh Tcl_DecrRefCount(pDb->pWalHook); 5188d22a174Sdan } 51994eb6a14Sdanielk1977 if( pDb->pCollateNeeded ){ 52094eb6a14Sdanielk1977 Tcl_DecrRefCount(pDb->pCollateNeeded); 52194eb6a14Sdanielk1977 } 522bec3f402Sdrh Tcl_Free((char*)pDb); 523bec3f402Sdrh } 524bec3f402Sdrh 525bec3f402Sdrh /* 526bec3f402Sdrh ** This routine is called when a database file is locked while trying 527bec3f402Sdrh ** to execute SQL. 528bec3f402Sdrh */ 5292a764eb0Sdanielk1977 static int DbBusyHandler(void *cd, int nTries){ 530bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 531bec3f402Sdrh int rc; 532bec3f402Sdrh char zVal[30]; 533bec3f402Sdrh 5345bb3eb9bSdrh sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries); 535d1e4733dSdrh rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0); 536bec3f402Sdrh if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 537bec3f402Sdrh return 0; 538bec3f402Sdrh } 539bec3f402Sdrh return 1; 54075897234Sdrh } 54175897234Sdrh 54226e4a8b1Sdrh #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 54375897234Sdrh /* 544348bb5d6Sdanielk1977 ** This routine is invoked as the 'progress callback' for the database. 545348bb5d6Sdanielk1977 */ 546348bb5d6Sdanielk1977 static int DbProgressHandler(void *cd){ 547348bb5d6Sdanielk1977 SqliteDb *pDb = (SqliteDb*)cd; 548348bb5d6Sdanielk1977 int rc; 549348bb5d6Sdanielk1977 550348bb5d6Sdanielk1977 assert( pDb->zProgress ); 551348bb5d6Sdanielk1977 rc = Tcl_Eval(pDb->interp, pDb->zProgress); 552348bb5d6Sdanielk1977 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 553348bb5d6Sdanielk1977 return 1; 554348bb5d6Sdanielk1977 } 555348bb5d6Sdanielk1977 return 0; 556348bb5d6Sdanielk1977 } 55726e4a8b1Sdrh #endif 558348bb5d6Sdanielk1977 559d1167393Sdrh #ifndef SQLITE_OMIT_TRACE 560348bb5d6Sdanielk1977 /* 561b5a20d3cSdrh ** This routine is called by the SQLite trace handler whenever a new 562b5a20d3cSdrh ** block of SQL is executed. The TCL script in pDb->zTrace is executed. 5630d1a643aSdrh */ 564b5a20d3cSdrh static void DbTraceHandler(void *cd, const char *zSql){ 5650d1a643aSdrh SqliteDb *pDb = (SqliteDb*)cd; 566b5a20d3cSdrh Tcl_DString str; 5670d1a643aSdrh 568b5a20d3cSdrh Tcl_DStringInit(&str); 569b5a20d3cSdrh Tcl_DStringAppend(&str, pDb->zTrace, -1); 570b5a20d3cSdrh Tcl_DStringAppendElement(&str, zSql); 571b5a20d3cSdrh Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 572b5a20d3cSdrh Tcl_DStringFree(&str); 573b5a20d3cSdrh Tcl_ResetResult(pDb->interp); 5740d1a643aSdrh } 575d1167393Sdrh #endif 5760d1a643aSdrh 577d1167393Sdrh #ifndef SQLITE_OMIT_TRACE 5780d1a643aSdrh /* 57919e2d37fSdrh ** This routine is called by the SQLite profile handler after a statement 58019e2d37fSdrh ** SQL has executed. The TCL script in pDb->zProfile is evaluated. 58119e2d37fSdrh */ 58219e2d37fSdrh static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){ 58319e2d37fSdrh SqliteDb *pDb = (SqliteDb*)cd; 58419e2d37fSdrh Tcl_DString str; 58519e2d37fSdrh char zTm[100]; 58619e2d37fSdrh 58719e2d37fSdrh sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm); 58819e2d37fSdrh Tcl_DStringInit(&str); 58919e2d37fSdrh Tcl_DStringAppend(&str, pDb->zProfile, -1); 59019e2d37fSdrh Tcl_DStringAppendElement(&str, zSql); 59119e2d37fSdrh Tcl_DStringAppendElement(&str, zTm); 59219e2d37fSdrh Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 59319e2d37fSdrh Tcl_DStringFree(&str); 59419e2d37fSdrh Tcl_ResetResult(pDb->interp); 59519e2d37fSdrh } 596d1167393Sdrh #endif 59719e2d37fSdrh 59819e2d37fSdrh /* 599aa940eacSdrh ** This routine is called when a transaction is committed. The 600aa940eacSdrh ** TCL script in pDb->zCommit is executed. If it returns non-zero or 601aa940eacSdrh ** if it throws an exception, the transaction is rolled back instead 602aa940eacSdrh ** of being committed. 603aa940eacSdrh */ 604aa940eacSdrh static int DbCommitHandler(void *cd){ 605aa940eacSdrh SqliteDb *pDb = (SqliteDb*)cd; 606aa940eacSdrh int rc; 607aa940eacSdrh 608aa940eacSdrh rc = Tcl_Eval(pDb->interp, pDb->zCommit); 609aa940eacSdrh if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 610aa940eacSdrh return 1; 611aa940eacSdrh } 612aa940eacSdrh return 0; 613aa940eacSdrh } 614aa940eacSdrh 61571fd80bfSdanielk1977 static void DbRollbackHandler(void *clientData){ 61671fd80bfSdanielk1977 SqliteDb *pDb = (SqliteDb*)clientData; 61771fd80bfSdanielk1977 assert(pDb->pRollbackHook); 61871fd80bfSdanielk1977 if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){ 61971fd80bfSdanielk1977 Tcl_BackgroundError(pDb->interp); 62071fd80bfSdanielk1977 } 62171fd80bfSdanielk1977 } 62271fd80bfSdanielk1977 6235def0843Sdrh /* 6245def0843Sdrh ** This procedure handles wal_hook callbacks. 6255def0843Sdrh */ 6265def0843Sdrh static int DbWalHandler( 6278d22a174Sdan void *clientData, 6288d22a174Sdan sqlite3 *db, 6298d22a174Sdan const char *zDb, 6308d22a174Sdan int nEntry 6318d22a174Sdan ){ 6325def0843Sdrh int ret = SQLITE_OK; 6338d22a174Sdan Tcl_Obj *p; 6348d22a174Sdan SqliteDb *pDb = (SqliteDb*)clientData; 6358d22a174Sdan Tcl_Interp *interp = pDb->interp; 6365def0843Sdrh assert(pDb->pWalHook); 6378d22a174Sdan 6385def0843Sdrh p = Tcl_DuplicateObj(pDb->pWalHook); 6398d22a174Sdan Tcl_IncrRefCount(p); 6408d22a174Sdan Tcl_ListObjAppendElement(interp, p, Tcl_NewStringObj(zDb, -1)); 6418d22a174Sdan Tcl_ListObjAppendElement(interp, p, Tcl_NewIntObj(nEntry)); 6428d22a174Sdan if( TCL_OK!=Tcl_EvalObjEx(interp, p, 0) 6438d22a174Sdan || TCL_OK!=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &ret) 6448d22a174Sdan ){ 6458d22a174Sdan Tcl_BackgroundError(interp); 6468d22a174Sdan } 6478d22a174Sdan Tcl_DecrRefCount(p); 6488d22a174Sdan 6498d22a174Sdan return ret; 6508d22a174Sdan } 6518d22a174Sdan 652bcf4f484Sdrh #if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_UNLOCK_NOTIFY) 653404ca075Sdanielk1977 static void setTestUnlockNotifyVars(Tcl_Interp *interp, int iArg, int nArg){ 654404ca075Sdanielk1977 char zBuf[64]; 655404ca075Sdanielk1977 sprintf(zBuf, "%d", iArg); 656404ca075Sdanielk1977 Tcl_SetVar(interp, "sqlite_unlock_notify_arg", zBuf, TCL_GLOBAL_ONLY); 657404ca075Sdanielk1977 sprintf(zBuf, "%d", nArg); 658404ca075Sdanielk1977 Tcl_SetVar(interp, "sqlite_unlock_notify_argcount", zBuf, TCL_GLOBAL_ONLY); 659404ca075Sdanielk1977 } 660404ca075Sdanielk1977 #else 661404ca075Sdanielk1977 # define setTestUnlockNotifyVars(x,y,z) 662404ca075Sdanielk1977 #endif 663404ca075Sdanielk1977 66469910da9Sdrh #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 665404ca075Sdanielk1977 static void DbUnlockNotify(void **apArg, int nArg){ 666404ca075Sdanielk1977 int i; 667404ca075Sdanielk1977 for(i=0; i<nArg; i++){ 668404ca075Sdanielk1977 const int flags = (TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); 669404ca075Sdanielk1977 SqliteDb *pDb = (SqliteDb *)apArg[i]; 670404ca075Sdanielk1977 setTestUnlockNotifyVars(pDb->interp, i, nArg); 671404ca075Sdanielk1977 assert( pDb->pUnlockNotify); 672404ca075Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pDb->pUnlockNotify, flags); 673404ca075Sdanielk1977 Tcl_DecrRefCount(pDb->pUnlockNotify); 674404ca075Sdanielk1977 pDb->pUnlockNotify = 0; 675404ca075Sdanielk1977 } 676404ca075Sdanielk1977 } 67769910da9Sdrh #endif 678404ca075Sdanielk1977 67994eb6a14Sdanielk1977 static void DbUpdateHandler( 68094eb6a14Sdanielk1977 void *p, 68194eb6a14Sdanielk1977 int op, 68294eb6a14Sdanielk1977 const char *zDb, 68394eb6a14Sdanielk1977 const char *zTbl, 68494eb6a14Sdanielk1977 sqlite_int64 rowid 68594eb6a14Sdanielk1977 ){ 68694eb6a14Sdanielk1977 SqliteDb *pDb = (SqliteDb *)p; 68794eb6a14Sdanielk1977 Tcl_Obj *pCmd; 68894eb6a14Sdanielk1977 68994eb6a14Sdanielk1977 assert( pDb->pUpdateHook ); 69094eb6a14Sdanielk1977 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); 69194eb6a14Sdanielk1977 69294eb6a14Sdanielk1977 pCmd = Tcl_DuplicateObj(pDb->pUpdateHook); 69394eb6a14Sdanielk1977 Tcl_IncrRefCount(pCmd); 69494eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj( 69594eb6a14Sdanielk1977 ( (op==SQLITE_INSERT)?"INSERT":(op==SQLITE_UPDATE)?"UPDATE":"DELETE"), -1)); 69694eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); 69794eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); 69894eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid)); 69994eb6a14Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 700efdde169Sdrh Tcl_DecrRefCount(pCmd); 70194eb6a14Sdanielk1977 } 70294eb6a14Sdanielk1977 7037cedc8d4Sdanielk1977 static void tclCollateNeeded( 7047cedc8d4Sdanielk1977 void *pCtx, 7059bb575fdSdrh sqlite3 *db, 7067cedc8d4Sdanielk1977 int enc, 7077cedc8d4Sdanielk1977 const char *zName 7087cedc8d4Sdanielk1977 ){ 7097cedc8d4Sdanielk1977 SqliteDb *pDb = (SqliteDb *)pCtx; 7107cedc8d4Sdanielk1977 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded); 7117cedc8d4Sdanielk1977 Tcl_IncrRefCount(pScript); 7127cedc8d4Sdanielk1977 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1)); 7137cedc8d4Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pScript, 0); 7147cedc8d4Sdanielk1977 Tcl_DecrRefCount(pScript); 7157cedc8d4Sdanielk1977 } 7167cedc8d4Sdanielk1977 717aa940eacSdrh /* 7180202b29eSdanielk1977 ** This routine is called to evaluate an SQL collation function implemented 7190202b29eSdanielk1977 ** using TCL script. 7200202b29eSdanielk1977 */ 7210202b29eSdanielk1977 static int tclSqlCollate( 7220202b29eSdanielk1977 void *pCtx, 7230202b29eSdanielk1977 int nA, 7240202b29eSdanielk1977 const void *zA, 7250202b29eSdanielk1977 int nB, 7260202b29eSdanielk1977 const void *zB 7270202b29eSdanielk1977 ){ 7280202b29eSdanielk1977 SqlCollate *p = (SqlCollate *)pCtx; 7290202b29eSdanielk1977 Tcl_Obj *pCmd; 7300202b29eSdanielk1977 7310202b29eSdanielk1977 pCmd = Tcl_NewStringObj(p->zScript, -1); 7320202b29eSdanielk1977 Tcl_IncrRefCount(pCmd); 7330202b29eSdanielk1977 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA)); 7340202b29eSdanielk1977 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB)); 735d1e4733dSdrh Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 7360202b29eSdanielk1977 Tcl_DecrRefCount(pCmd); 7370202b29eSdanielk1977 return (atoi(Tcl_GetStringResult(p->interp))); 7380202b29eSdanielk1977 } 7390202b29eSdanielk1977 7400202b29eSdanielk1977 /* 741cabb0819Sdrh ** This routine is called to evaluate an SQL function implemented 742cabb0819Sdrh ** using TCL script. 743cabb0819Sdrh */ 7440ae8b831Sdanielk1977 static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ 7456f8a503dSdanielk1977 SqlFunc *p = sqlite3_user_data(context); 746d1e4733dSdrh Tcl_Obj *pCmd; 747cabb0819Sdrh int i; 748cabb0819Sdrh int rc; 749cabb0819Sdrh 750d1e4733dSdrh if( argc==0 ){ 751d1e4733dSdrh /* If there are no arguments to the function, call Tcl_EvalObjEx on the 752d1e4733dSdrh ** script object directly. This allows the TCL compiler to generate 753d1e4733dSdrh ** bytecode for the command on the first invocation and thus make 754d1e4733dSdrh ** subsequent invocations much faster. */ 755d1e4733dSdrh pCmd = p->pScript; 756d1e4733dSdrh Tcl_IncrRefCount(pCmd); 757d1e4733dSdrh rc = Tcl_EvalObjEx(p->interp, pCmd, 0); 758d1e4733dSdrh Tcl_DecrRefCount(pCmd); 75951ad0ecdSdanielk1977 }else{ 760d1e4733dSdrh /* If there are arguments to the function, make a shallow copy of the 761d1e4733dSdrh ** script object, lappend the arguments, then evaluate the copy. 762d1e4733dSdrh ** 76360ec914cSpeter.d.reid ** By "shallow" copy, we mean only the outer list Tcl_Obj is duplicated. 764d1e4733dSdrh ** The new Tcl_Obj contains pointers to the original list elements. 765d1e4733dSdrh ** That way, when Tcl_EvalObjv() is run and shimmers the first element 766d1e4733dSdrh ** of the list to tclCmdNameType, that alternate representation will 767d1e4733dSdrh ** be preserved and reused on the next invocation. 768d1e4733dSdrh */ 769d1e4733dSdrh Tcl_Obj **aArg; 770d1e4733dSdrh int nArg; 771d1e4733dSdrh if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){ 772d1e4733dSdrh sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 773d1e4733dSdrh return; 774d1e4733dSdrh } 775d1e4733dSdrh pCmd = Tcl_NewListObj(nArg, aArg); 776d1e4733dSdrh Tcl_IncrRefCount(pCmd); 777d1e4733dSdrh for(i=0; i<argc; i++){ 778d1e4733dSdrh sqlite3_value *pIn = argv[i]; 779d1e4733dSdrh Tcl_Obj *pVal; 780d1e4733dSdrh 781d1e4733dSdrh /* Set pVal to contain the i'th column of this row. */ 782d1e4733dSdrh switch( sqlite3_value_type(pIn) ){ 783d1e4733dSdrh case SQLITE_BLOB: { 784d1e4733dSdrh int bytes = sqlite3_value_bytes(pIn); 785d1e4733dSdrh pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes); 786d1e4733dSdrh break; 787d1e4733dSdrh } 788d1e4733dSdrh case SQLITE_INTEGER: { 789d1e4733dSdrh sqlite_int64 v = sqlite3_value_int64(pIn); 790d1e4733dSdrh if( v>=-2147483647 && v<=2147483647 ){ 7917fd33929Sdrh pVal = Tcl_NewIntObj((int)v); 792d1e4733dSdrh }else{ 793d1e4733dSdrh pVal = Tcl_NewWideIntObj(v); 794d1e4733dSdrh } 795d1e4733dSdrh break; 796d1e4733dSdrh } 797d1e4733dSdrh case SQLITE_FLOAT: { 798d1e4733dSdrh double r = sqlite3_value_double(pIn); 799d1e4733dSdrh pVal = Tcl_NewDoubleObj(r); 800d1e4733dSdrh break; 801d1e4733dSdrh } 802d1e4733dSdrh case SQLITE_NULL: { 803c45e6716Sdrh pVal = Tcl_NewStringObj(p->pDb->zNull, -1); 804d1e4733dSdrh break; 805d1e4733dSdrh } 806d1e4733dSdrh default: { 807d1e4733dSdrh int bytes = sqlite3_value_bytes(pIn); 80800fd957bSdanielk1977 pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes); 809d1e4733dSdrh break; 81051ad0ecdSdanielk1977 } 811cabb0819Sdrh } 812d1e4733dSdrh rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal); 813d1e4733dSdrh if( rc ){ 814d1e4733dSdrh Tcl_DecrRefCount(pCmd); 815d1e4733dSdrh sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 816d1e4733dSdrh return; 817d1e4733dSdrh } 818d1e4733dSdrh } 819d1e4733dSdrh if( !p->useEvalObjv ){ 820d1e4733dSdrh /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd 821d1e4733dSdrh ** is a list without a string representation. To prevent this from 822d1e4733dSdrh ** happening, make sure pCmd has a valid string representation */ 823d1e4733dSdrh Tcl_GetString(pCmd); 824d1e4733dSdrh } 825d1e4733dSdrh rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 826d1e4733dSdrh Tcl_DecrRefCount(pCmd); 827d1e4733dSdrh } 828562e8d3cSdanielk1977 829c7f269d5Sdrh if( rc && rc!=TCL_RETURN ){ 8307e18c259Sdanielk1977 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 831cabb0819Sdrh }else{ 832c7f269d5Sdrh Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); 833c7f269d5Sdrh int n; 834c7f269d5Sdrh u8 *data; 8354a4c11aaSdan const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 836c7f269d5Sdrh char c = zType[0]; 837df0bddaeSdrh if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ 838d1e4733dSdrh /* Only return a BLOB type if the Tcl variable is a bytearray and 839df0bddaeSdrh ** has no string representation. */ 840c7f269d5Sdrh data = Tcl_GetByteArrayFromObj(pVar, &n); 841c7f269d5Sdrh sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT); 842985e0c63Sdrh }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 843c7f269d5Sdrh Tcl_GetIntFromObj(0, pVar, &n); 844c7f269d5Sdrh sqlite3_result_int(context, n); 845c7f269d5Sdrh }else if( c=='d' && strcmp(zType,"double")==0 ){ 846c7f269d5Sdrh double r; 847c7f269d5Sdrh Tcl_GetDoubleFromObj(0, pVar, &r); 848c7f269d5Sdrh sqlite3_result_double(context, r); 849985e0c63Sdrh }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 850985e0c63Sdrh (c=='i' && strcmp(zType,"int")==0) ){ 851df0bddaeSdrh Tcl_WideInt v; 852df0bddaeSdrh Tcl_GetWideIntFromObj(0, pVar, &v); 853df0bddaeSdrh sqlite3_result_int64(context, v); 854c7f269d5Sdrh }else{ 85500fd957bSdanielk1977 data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 85600fd957bSdanielk1977 sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT); 857c7f269d5Sdrh } 858cabb0819Sdrh } 859cabb0819Sdrh } 860895d7472Sdrh 861e22a334bSdrh #ifndef SQLITE_OMIT_AUTHORIZATION 862e22a334bSdrh /* 863e22a334bSdrh ** This is the authentication function. It appends the authentication 864e22a334bSdrh ** type code and the two arguments to zCmd[] then invokes the result 865e22a334bSdrh ** on the interpreter. The reply is examined to determine if the 866e22a334bSdrh ** authentication fails or succeeds. 867e22a334bSdrh */ 868e22a334bSdrh static int auth_callback( 869e22a334bSdrh void *pArg, 870e22a334bSdrh int code, 871e22a334bSdrh const char *zArg1, 872e22a334bSdrh const char *zArg2, 873e22a334bSdrh const char *zArg3, 874e22a334bSdrh const char *zArg4 87532c6a48bSdrh #ifdef SQLITE_USER_AUTHENTICATION 87632c6a48bSdrh ,const char *zArg5 87732c6a48bSdrh #endif 878e22a334bSdrh ){ 8796ef5e12eSmistachkin const char *zCode; 880e22a334bSdrh Tcl_DString str; 881e22a334bSdrh int rc; 882e22a334bSdrh const char *zReply; 883e22a334bSdrh SqliteDb *pDb = (SqliteDb*)pArg; 8841f1549f8Sdrh if( pDb->disableAuth ) return SQLITE_OK; 885e22a334bSdrh 886e22a334bSdrh switch( code ){ 887e22a334bSdrh case SQLITE_COPY : zCode="SQLITE_COPY"; break; 888e22a334bSdrh case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; 889e22a334bSdrh case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; 890e22a334bSdrh case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; 891e22a334bSdrh case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; 892e22a334bSdrh case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; 893e22a334bSdrh case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; 894e22a334bSdrh case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; 895e22a334bSdrh case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; 896e22a334bSdrh case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; 897e22a334bSdrh case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; 898e22a334bSdrh case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; 899e22a334bSdrh case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; 900e22a334bSdrh case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; 901e22a334bSdrh case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; 902e22a334bSdrh case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; 903e22a334bSdrh case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; 904e22a334bSdrh case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; 905e22a334bSdrh case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; 906e22a334bSdrh case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; 907e22a334bSdrh case SQLITE_READ : zCode="SQLITE_READ"; break; 908e22a334bSdrh case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; 909e22a334bSdrh case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; 910e22a334bSdrh case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; 91181e293b4Sdrh case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; 91281e293b4Sdrh case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; 9131c8c23ccSdanielk1977 case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break; 9141d54df88Sdanielk1977 case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break; 915e6e04969Sdrh case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break; 916f1a381e7Sdanielk1977 case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break; 917f1a381e7Sdanielk1977 case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break; 9185169bbc6Sdrh case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break; 919ab9b703fSdanielk1977 case SQLITE_SAVEPOINT : zCode="SQLITE_SAVEPOINT"; break; 92065a2aaa6Sdrh case SQLITE_RECURSIVE : zCode="SQLITE_RECURSIVE"; break; 921e22a334bSdrh default : zCode="????"; break; 922e22a334bSdrh } 923e22a334bSdrh Tcl_DStringInit(&str); 924e22a334bSdrh Tcl_DStringAppend(&str, pDb->zAuth, -1); 925e22a334bSdrh Tcl_DStringAppendElement(&str, zCode); 926e22a334bSdrh Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); 927e22a334bSdrh Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); 928e22a334bSdrh Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); 929e22a334bSdrh Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); 93032c6a48bSdrh #ifdef SQLITE_USER_AUTHENTICATION 93132c6a48bSdrh Tcl_DStringAppendElement(&str, zArg5 ? zArg5 : ""); 93232c6a48bSdrh #endif 933e22a334bSdrh rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); 934e22a334bSdrh Tcl_DStringFree(&str); 935b07028f7Sdrh zReply = rc==TCL_OK ? Tcl_GetStringResult(pDb->interp) : "SQLITE_DENY"; 936e22a334bSdrh if( strcmp(zReply,"SQLITE_OK")==0 ){ 937e22a334bSdrh rc = SQLITE_OK; 938e22a334bSdrh }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ 939e22a334bSdrh rc = SQLITE_DENY; 940e22a334bSdrh }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ 941e22a334bSdrh rc = SQLITE_IGNORE; 942e22a334bSdrh }else{ 943e22a334bSdrh rc = 999; 944e22a334bSdrh } 945e22a334bSdrh return rc; 946e22a334bSdrh } 947e22a334bSdrh #endif /* SQLITE_OMIT_AUTHORIZATION */ 948cabb0819Sdrh 949cabb0819Sdrh /* 9501067fe11Stpoindex ** This routine reads a line of text from FILE in, stores 9511067fe11Stpoindex ** the text in memory obtained from malloc() and returns a pointer 9521067fe11Stpoindex ** to the text. NULL is returned at end of file, or if malloc() 9531067fe11Stpoindex ** fails. 9541067fe11Stpoindex ** 9551067fe11Stpoindex ** The interface is like "readline" but no command-line editing 9561067fe11Stpoindex ** is done. 9571067fe11Stpoindex ** 9581067fe11Stpoindex ** copied from shell.c from '.import' command 9591067fe11Stpoindex */ 9601067fe11Stpoindex static char *local_getline(char *zPrompt, FILE *in){ 9611067fe11Stpoindex char *zLine; 9621067fe11Stpoindex int nLine; 9631067fe11Stpoindex int n; 9641067fe11Stpoindex 9651067fe11Stpoindex nLine = 100; 9661067fe11Stpoindex zLine = malloc( nLine ); 9671067fe11Stpoindex if( zLine==0 ) return 0; 9681067fe11Stpoindex n = 0; 969b07028f7Sdrh while( 1 ){ 9701067fe11Stpoindex if( n+100>nLine ){ 9711067fe11Stpoindex nLine = nLine*2 + 100; 9721067fe11Stpoindex zLine = realloc(zLine, nLine); 9731067fe11Stpoindex if( zLine==0 ) return 0; 9741067fe11Stpoindex } 9751067fe11Stpoindex if( fgets(&zLine[n], nLine - n, in)==0 ){ 9761067fe11Stpoindex if( n==0 ){ 9771067fe11Stpoindex free(zLine); 9781067fe11Stpoindex return 0; 9791067fe11Stpoindex } 9801067fe11Stpoindex zLine[n] = 0; 9811067fe11Stpoindex break; 9821067fe11Stpoindex } 9831067fe11Stpoindex while( zLine[n] ){ n++; } 9841067fe11Stpoindex if( n>0 && zLine[n-1]=='\n' ){ 9851067fe11Stpoindex n--; 9861067fe11Stpoindex zLine[n] = 0; 987b07028f7Sdrh break; 9881067fe11Stpoindex } 9891067fe11Stpoindex } 9901067fe11Stpoindex zLine = realloc( zLine, n+1 ); 9911067fe11Stpoindex return zLine; 9921067fe11Stpoindex } 9931067fe11Stpoindex 9948e556520Sdanielk1977 9958e556520Sdanielk1977 /* 9964a4c11aaSdan ** This function is part of the implementation of the command: 9978e556520Sdanielk1977 ** 9984a4c11aaSdan ** $db transaction [-deferred|-immediate|-exclusive] SCRIPT 9998e556520Sdanielk1977 ** 10004a4c11aaSdan ** It is invoked after evaluating the script SCRIPT to commit or rollback 10014a4c11aaSdan ** the transaction or savepoint opened by the [transaction] command. 10024a4c11aaSdan */ 10034a4c11aaSdan static int DbTransPostCmd( 10044a4c11aaSdan ClientData data[], /* data[0] is the Sqlite3Db* for $db */ 10054a4c11aaSdan Tcl_Interp *interp, /* Tcl interpreter */ 10064a4c11aaSdan int result /* Result of evaluating SCRIPT */ 10074a4c11aaSdan ){ 10086ef5e12eSmistachkin static const char *const azEnd[] = { 10094a4c11aaSdan "RELEASE _tcl_transaction", /* rc==TCL_ERROR, nTransaction!=0 */ 10104a4c11aaSdan "COMMIT", /* rc!=TCL_ERROR, nTransaction==0 */ 10114a4c11aaSdan "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction", 10124a4c11aaSdan "ROLLBACK" /* rc==TCL_ERROR, nTransaction==0 */ 10134a4c11aaSdan }; 10144a4c11aaSdan SqliteDb *pDb = (SqliteDb*)data[0]; 10154a4c11aaSdan int rc = result; 10164a4c11aaSdan const char *zEnd; 10174a4c11aaSdan 10184a4c11aaSdan pDb->nTransaction--; 10194a4c11aaSdan zEnd = azEnd[(rc==TCL_ERROR)*2 + (pDb->nTransaction==0)]; 10204a4c11aaSdan 10214a4c11aaSdan pDb->disableAuth++; 10224a4c11aaSdan if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){ 10234a4c11aaSdan /* This is a tricky scenario to handle. The most likely cause of an 10244a4c11aaSdan ** error is that the exec() above was an attempt to commit the 10254a4c11aaSdan ** top-level transaction that returned SQLITE_BUSY. Or, less likely, 102648864df9Smistachkin ** that an IO-error has occurred. In either case, throw a Tcl exception 10274a4c11aaSdan ** and try to rollback the transaction. 10284a4c11aaSdan ** 10294a4c11aaSdan ** But it could also be that the user executed one or more BEGIN, 10304a4c11aaSdan ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing 10314a4c11aaSdan ** this method's logic. Not clear how this would be best handled. 10324a4c11aaSdan */ 10334a4c11aaSdan if( rc!=TCL_ERROR ){ 1034a198f2b5Sdrh Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 10354a4c11aaSdan rc = TCL_ERROR; 10364a4c11aaSdan } 10374a4c11aaSdan sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0); 10384a4c11aaSdan } 10394a4c11aaSdan pDb->disableAuth--; 10404a4c11aaSdan 10414a4c11aaSdan return rc; 10424a4c11aaSdan } 10434a4c11aaSdan 10444a4c11aaSdan /* 1045c431fd55Sdan ** Unless SQLITE_TEST is defined, this function is a simple wrapper around 1046c431fd55Sdan ** sqlite3_prepare_v2(). If SQLITE_TEST is defined, then it uses either 1047c431fd55Sdan ** sqlite3_prepare_v2() or legacy interface sqlite3_prepare(), depending 1048c431fd55Sdan ** on whether or not the [db_use_legacy_prepare] command has been used to 1049c431fd55Sdan ** configure the connection. 1050c431fd55Sdan */ 1051c431fd55Sdan static int dbPrepare( 1052c431fd55Sdan SqliteDb *pDb, /* Database object */ 1053c431fd55Sdan const char *zSql, /* SQL to compile */ 1054c431fd55Sdan sqlite3_stmt **ppStmt, /* OUT: Prepared statement */ 1055c431fd55Sdan const char **pzOut /* OUT: Pointer to next SQL statement */ 1056c431fd55Sdan ){ 1057c431fd55Sdan #ifdef SQLITE_TEST 1058c431fd55Sdan if( pDb->bLegacyPrepare ){ 1059c431fd55Sdan return sqlite3_prepare(pDb->db, zSql, -1, ppStmt, pzOut); 1060c431fd55Sdan } 1061c431fd55Sdan #endif 1062c431fd55Sdan return sqlite3_prepare_v2(pDb->db, zSql, -1, ppStmt, pzOut); 1063c431fd55Sdan } 1064c431fd55Sdan 1065c431fd55Sdan /* 10664a4c11aaSdan ** Search the cache for a prepared-statement object that implements the 10674a4c11aaSdan ** first SQL statement in the buffer pointed to by parameter zIn. If 10684a4c11aaSdan ** no such prepared-statement can be found, allocate and prepare a new 10694a4c11aaSdan ** one. In either case, bind the current values of the relevant Tcl 10704a4c11aaSdan ** variables to any $var, :var or @var variables in the statement. Before 10714a4c11aaSdan ** returning, set *ppPreStmt to point to the prepared-statement object. 10724a4c11aaSdan ** 10734a4c11aaSdan ** Output parameter *pzOut is set to point to the next SQL statement in 10744a4c11aaSdan ** buffer zIn, or to the '\0' byte at the end of zIn if there is no 10754a4c11aaSdan ** next statement. 10764a4c11aaSdan ** 10774a4c11aaSdan ** If successful, TCL_OK is returned. Otherwise, TCL_ERROR is returned 10784a4c11aaSdan ** and an error message loaded into interpreter pDb->interp. 10794a4c11aaSdan */ 10804a4c11aaSdan static int dbPrepareAndBind( 10814a4c11aaSdan SqliteDb *pDb, /* Database object */ 10824a4c11aaSdan char const *zIn, /* SQL to compile */ 10834a4c11aaSdan char const **pzOut, /* OUT: Pointer to next SQL statement */ 10844a4c11aaSdan SqlPreparedStmt **ppPreStmt /* OUT: Object used to cache statement */ 10854a4c11aaSdan ){ 10864a4c11aaSdan const char *zSql = zIn; /* Pointer to first SQL statement in zIn */ 10874a4c11aaSdan sqlite3_stmt *pStmt; /* Prepared statement object */ 10884a4c11aaSdan SqlPreparedStmt *pPreStmt; /* Pointer to cached statement */ 10894a4c11aaSdan int nSql; /* Length of zSql in bytes */ 10904a4c11aaSdan int nVar; /* Number of variables in statement */ 10914a4c11aaSdan int iParm = 0; /* Next free entry in apParm */ 10920425f189Sdrh char c; 10934a4c11aaSdan int i; 10944a4c11aaSdan Tcl_Interp *interp = pDb->interp; 10954a4c11aaSdan 10964a4c11aaSdan *ppPreStmt = 0; 10974a4c11aaSdan 10984a4c11aaSdan /* Trim spaces from the start of zSql and calculate the remaining length. */ 10990425f189Sdrh while( (c = zSql[0])==' ' || c=='\t' || c=='\r' || c=='\n' ){ zSql++; } 11004a4c11aaSdan nSql = strlen30(zSql); 11014a4c11aaSdan 11024a4c11aaSdan for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){ 11034a4c11aaSdan int n = pPreStmt->nSql; 11044a4c11aaSdan if( nSql>=n 11054a4c11aaSdan && memcmp(pPreStmt->zSql, zSql, n)==0 11064a4c11aaSdan && (zSql[n]==0 || zSql[n-1]==';') 11074a4c11aaSdan ){ 11084a4c11aaSdan pStmt = pPreStmt->pStmt; 11094a4c11aaSdan *pzOut = &zSql[pPreStmt->nSql]; 11104a4c11aaSdan 11114a4c11aaSdan /* When a prepared statement is found, unlink it from the 11124a4c11aaSdan ** cache list. It will later be added back to the beginning 11134a4c11aaSdan ** of the cache list in order to implement LRU replacement. 11144a4c11aaSdan */ 11154a4c11aaSdan if( pPreStmt->pPrev ){ 11164a4c11aaSdan pPreStmt->pPrev->pNext = pPreStmt->pNext; 11174a4c11aaSdan }else{ 11184a4c11aaSdan pDb->stmtList = pPreStmt->pNext; 11194a4c11aaSdan } 11204a4c11aaSdan if( pPreStmt->pNext ){ 11214a4c11aaSdan pPreStmt->pNext->pPrev = pPreStmt->pPrev; 11224a4c11aaSdan }else{ 11234a4c11aaSdan pDb->stmtLast = pPreStmt->pPrev; 11244a4c11aaSdan } 11254a4c11aaSdan pDb->nStmt--; 11264a4c11aaSdan nVar = sqlite3_bind_parameter_count(pStmt); 11274a4c11aaSdan break; 11284a4c11aaSdan } 11294a4c11aaSdan } 11304a4c11aaSdan 11314a4c11aaSdan /* If no prepared statement was found. Compile the SQL text. Also allocate 11324a4c11aaSdan ** a new SqlPreparedStmt structure. */ 11334a4c11aaSdan if( pPreStmt==0 ){ 11344a4c11aaSdan int nByte; 11354a4c11aaSdan 1136c431fd55Sdan if( SQLITE_OK!=dbPrepare(pDb, zSql, &pStmt, pzOut) ){ 1137c45e6716Sdrh Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 11384a4c11aaSdan return TCL_ERROR; 11394a4c11aaSdan } 11404a4c11aaSdan if( pStmt==0 ){ 11414a4c11aaSdan if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ 11424a4c11aaSdan /* A compile-time error in the statement. */ 1143c45e6716Sdrh Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 11444a4c11aaSdan return TCL_ERROR; 11454a4c11aaSdan }else{ 11464a4c11aaSdan /* The statement was a no-op. Continue to the next statement 11474a4c11aaSdan ** in the SQL string. 11484a4c11aaSdan */ 11494a4c11aaSdan return TCL_OK; 11504a4c11aaSdan } 11514a4c11aaSdan } 11524a4c11aaSdan 11534a4c11aaSdan assert( pPreStmt==0 ); 11544a4c11aaSdan nVar = sqlite3_bind_parameter_count(pStmt); 11554a4c11aaSdan nByte = sizeof(SqlPreparedStmt) + nVar*sizeof(Tcl_Obj *); 11564a4c11aaSdan pPreStmt = (SqlPreparedStmt*)Tcl_Alloc(nByte); 11574a4c11aaSdan memset(pPreStmt, 0, nByte); 11584a4c11aaSdan 11594a4c11aaSdan pPreStmt->pStmt = pStmt; 11607ed243b7Sdrh pPreStmt->nSql = (int)(*pzOut - zSql); 11614a4c11aaSdan pPreStmt->zSql = sqlite3_sql(pStmt); 11624a4c11aaSdan pPreStmt->apParm = (Tcl_Obj **)&pPreStmt[1]; 1163c431fd55Sdan #ifdef SQLITE_TEST 1164c431fd55Sdan if( pPreStmt->zSql==0 ){ 1165c431fd55Sdan char *zCopy = Tcl_Alloc(pPreStmt->nSql + 1); 1166c431fd55Sdan memcpy(zCopy, zSql, pPreStmt->nSql); 1167c431fd55Sdan zCopy[pPreStmt->nSql] = '\0'; 1168c431fd55Sdan pPreStmt->zSql = zCopy; 1169c431fd55Sdan } 1170c431fd55Sdan #endif 11714a4c11aaSdan } 11724a4c11aaSdan assert( pPreStmt ); 11734a4c11aaSdan assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql ); 11744a4c11aaSdan assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) ); 11754a4c11aaSdan 11764a4c11aaSdan /* Bind values to parameters that begin with $ or : */ 11774a4c11aaSdan for(i=1; i<=nVar; i++){ 11784a4c11aaSdan const char *zVar = sqlite3_bind_parameter_name(pStmt, i); 11794a4c11aaSdan if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){ 11804a4c11aaSdan Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); 11814a4c11aaSdan if( pVar ){ 11824a4c11aaSdan int n; 11834a4c11aaSdan u8 *data; 11844a4c11aaSdan const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 11854a4c11aaSdan char c = zType[0]; 11864a4c11aaSdan if( zVar[0]=='@' || 11874a4c11aaSdan (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){ 11884a4c11aaSdan /* Load a BLOB type if the Tcl variable is a bytearray and 11894a4c11aaSdan ** it has no string representation or the host 11904a4c11aaSdan ** parameter name begins with "@". */ 11914a4c11aaSdan data = Tcl_GetByteArrayFromObj(pVar, &n); 11924a4c11aaSdan sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); 11934a4c11aaSdan Tcl_IncrRefCount(pVar); 11944a4c11aaSdan pPreStmt->apParm[iParm++] = pVar; 11954a4c11aaSdan }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 11964a4c11aaSdan Tcl_GetIntFromObj(interp, pVar, &n); 11974a4c11aaSdan sqlite3_bind_int(pStmt, i, n); 11984a4c11aaSdan }else if( c=='d' && strcmp(zType,"double")==0 ){ 11994a4c11aaSdan double r; 12004a4c11aaSdan Tcl_GetDoubleFromObj(interp, pVar, &r); 12014a4c11aaSdan sqlite3_bind_double(pStmt, i, r); 12024a4c11aaSdan }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 12034a4c11aaSdan (c=='i' && strcmp(zType,"int")==0) ){ 12044a4c11aaSdan Tcl_WideInt v; 12054a4c11aaSdan Tcl_GetWideIntFromObj(interp, pVar, &v); 12064a4c11aaSdan sqlite3_bind_int64(pStmt, i, v); 12074a4c11aaSdan }else{ 12084a4c11aaSdan data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 12094a4c11aaSdan sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); 12104a4c11aaSdan Tcl_IncrRefCount(pVar); 12114a4c11aaSdan pPreStmt->apParm[iParm++] = pVar; 12124a4c11aaSdan } 12134a4c11aaSdan }else{ 12144a4c11aaSdan sqlite3_bind_null(pStmt, i); 12154a4c11aaSdan } 12164a4c11aaSdan } 12174a4c11aaSdan } 12184a4c11aaSdan pPreStmt->nParm = iParm; 12194a4c11aaSdan *ppPreStmt = pPreStmt; 1220937d0deaSdan 12214a4c11aaSdan return TCL_OK; 12224a4c11aaSdan } 12234a4c11aaSdan 12244a4c11aaSdan /* 12254a4c11aaSdan ** Release a statement reference obtained by calling dbPrepareAndBind(). 12264a4c11aaSdan ** There should be exactly one call to this function for each call to 12274a4c11aaSdan ** dbPrepareAndBind(). 12284a4c11aaSdan ** 12294a4c11aaSdan ** If the discard parameter is non-zero, then the statement is deleted 12304a4c11aaSdan ** immediately. Otherwise it is added to the LRU list and may be returned 12314a4c11aaSdan ** by a subsequent call to dbPrepareAndBind(). 12324a4c11aaSdan */ 12334a4c11aaSdan static void dbReleaseStmt( 12344a4c11aaSdan SqliteDb *pDb, /* Database handle */ 12354a4c11aaSdan SqlPreparedStmt *pPreStmt, /* Prepared statement handle to release */ 12364a4c11aaSdan int discard /* True to delete (not cache) the pPreStmt */ 12374a4c11aaSdan ){ 12384a4c11aaSdan int i; 12394a4c11aaSdan 12404a4c11aaSdan /* Free the bound string and blob parameters */ 12414a4c11aaSdan for(i=0; i<pPreStmt->nParm; i++){ 12424a4c11aaSdan Tcl_DecrRefCount(pPreStmt->apParm[i]); 12434a4c11aaSdan } 12444a4c11aaSdan pPreStmt->nParm = 0; 12454a4c11aaSdan 12464a4c11aaSdan if( pDb->maxStmt<=0 || discard ){ 12474a4c11aaSdan /* If the cache is turned off, deallocated the statement */ 1248c431fd55Sdan dbFreeStmt(pPreStmt); 12494a4c11aaSdan }else{ 12504a4c11aaSdan /* Add the prepared statement to the beginning of the cache list. */ 12514a4c11aaSdan pPreStmt->pNext = pDb->stmtList; 12524a4c11aaSdan pPreStmt->pPrev = 0; 12534a4c11aaSdan if( pDb->stmtList ){ 12544a4c11aaSdan pDb->stmtList->pPrev = pPreStmt; 12554a4c11aaSdan } 12564a4c11aaSdan pDb->stmtList = pPreStmt; 12574a4c11aaSdan if( pDb->stmtLast==0 ){ 12584a4c11aaSdan assert( pDb->nStmt==0 ); 12594a4c11aaSdan pDb->stmtLast = pPreStmt; 12604a4c11aaSdan }else{ 12614a4c11aaSdan assert( pDb->nStmt>0 ); 12624a4c11aaSdan } 12634a4c11aaSdan pDb->nStmt++; 12644a4c11aaSdan 12654a4c11aaSdan /* If we have too many statement in cache, remove the surplus from 12664a4c11aaSdan ** the end of the cache list. */ 12674a4c11aaSdan while( pDb->nStmt>pDb->maxStmt ){ 1268c431fd55Sdan SqlPreparedStmt *pLast = pDb->stmtLast; 1269c431fd55Sdan pDb->stmtLast = pLast->pPrev; 12704a4c11aaSdan pDb->stmtLast->pNext = 0; 12714a4c11aaSdan pDb->nStmt--; 1272c431fd55Sdan dbFreeStmt(pLast); 12734a4c11aaSdan } 12744a4c11aaSdan } 12754a4c11aaSdan } 12764a4c11aaSdan 12774a4c11aaSdan /* 12784a4c11aaSdan ** Structure used with dbEvalXXX() functions: 12794a4c11aaSdan ** 12804a4c11aaSdan ** dbEvalInit() 12814a4c11aaSdan ** dbEvalStep() 12824a4c11aaSdan ** dbEvalFinalize() 12834a4c11aaSdan ** dbEvalRowInfo() 12844a4c11aaSdan ** dbEvalColumnValue() 12854a4c11aaSdan */ 12864a4c11aaSdan typedef struct DbEvalContext DbEvalContext; 12874a4c11aaSdan struct DbEvalContext { 12884a4c11aaSdan SqliteDb *pDb; /* Database handle */ 12894a4c11aaSdan Tcl_Obj *pSql; /* Object holding string zSql */ 12904a4c11aaSdan const char *zSql; /* Remaining SQL to execute */ 12914a4c11aaSdan SqlPreparedStmt *pPreStmt; /* Current statement */ 12924a4c11aaSdan int nCol; /* Number of columns returned by pStmt */ 12934a4c11aaSdan Tcl_Obj *pArray; /* Name of array variable */ 12944a4c11aaSdan Tcl_Obj **apColName; /* Array of column names */ 12954a4c11aaSdan }; 12964a4c11aaSdan 12974a4c11aaSdan /* 12984a4c11aaSdan ** Release any cache of column names currently held as part of 12994a4c11aaSdan ** the DbEvalContext structure passed as the first argument. 13004a4c11aaSdan */ 13014a4c11aaSdan static void dbReleaseColumnNames(DbEvalContext *p){ 13024a4c11aaSdan if( p->apColName ){ 13034a4c11aaSdan int i; 13044a4c11aaSdan for(i=0; i<p->nCol; i++){ 13054a4c11aaSdan Tcl_DecrRefCount(p->apColName[i]); 13064a4c11aaSdan } 13074a4c11aaSdan Tcl_Free((char *)p->apColName); 13084a4c11aaSdan p->apColName = 0; 13094a4c11aaSdan } 13104a4c11aaSdan p->nCol = 0; 13114a4c11aaSdan } 13124a4c11aaSdan 13134a4c11aaSdan /* 13144a4c11aaSdan ** Initialize a DbEvalContext structure. 13158e556520Sdanielk1977 ** 13168e556520Sdanielk1977 ** If pArray is not NULL, then it contains the name of a Tcl array 13178e556520Sdanielk1977 ** variable. The "*" member of this array is set to a list containing 13184a4c11aaSdan ** the names of the columns returned by the statement as part of each 13194a4c11aaSdan ** call to dbEvalStep(), in order from left to right. e.g. if the names 13204a4c11aaSdan ** of the returned columns are a, b and c, it does the equivalent of the 13214a4c11aaSdan ** tcl command: 13228e556520Sdanielk1977 ** 13238e556520Sdanielk1977 ** set ${pArray}(*) {a b c} 13248e556520Sdanielk1977 */ 13254a4c11aaSdan static void dbEvalInit( 13264a4c11aaSdan DbEvalContext *p, /* Pointer to structure to initialize */ 13274a4c11aaSdan SqliteDb *pDb, /* Database handle */ 13284a4c11aaSdan Tcl_Obj *pSql, /* Object containing SQL script */ 13294a4c11aaSdan Tcl_Obj *pArray /* Name of Tcl array to set (*) element of */ 13308e556520Sdanielk1977 ){ 13314a4c11aaSdan memset(p, 0, sizeof(DbEvalContext)); 13324a4c11aaSdan p->pDb = pDb; 13334a4c11aaSdan p->zSql = Tcl_GetString(pSql); 13344a4c11aaSdan p->pSql = pSql; 13354a4c11aaSdan Tcl_IncrRefCount(pSql); 13364a4c11aaSdan if( pArray ){ 13374a4c11aaSdan p->pArray = pArray; 13384a4c11aaSdan Tcl_IncrRefCount(pArray); 13394a4c11aaSdan } 13404a4c11aaSdan } 13418e556520Sdanielk1977 13424a4c11aaSdan /* 13434a4c11aaSdan ** Obtain information about the row that the DbEvalContext passed as the 13444a4c11aaSdan ** first argument currently points to. 13454a4c11aaSdan */ 13464a4c11aaSdan static void dbEvalRowInfo( 13474a4c11aaSdan DbEvalContext *p, /* Evaluation context */ 13484a4c11aaSdan int *pnCol, /* OUT: Number of column names */ 13494a4c11aaSdan Tcl_Obj ***papColName /* OUT: Array of column names */ 13504a4c11aaSdan ){ 13518e556520Sdanielk1977 /* Compute column names */ 13524a4c11aaSdan if( 0==p->apColName ){ 13534a4c11aaSdan sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 13544a4c11aaSdan int i; /* Iterator variable */ 13554a4c11aaSdan int nCol; /* Number of columns returned by pStmt */ 13564a4c11aaSdan Tcl_Obj **apColName = 0; /* Array of column names */ 13574a4c11aaSdan 13584a4c11aaSdan p->nCol = nCol = sqlite3_column_count(pStmt); 13594a4c11aaSdan if( nCol>0 && (papColName || p->pArray) ){ 13604a4c11aaSdan apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); 13618e556520Sdanielk1977 for(i=0; i<nCol; i++){ 1362c45e6716Sdrh apColName[i] = Tcl_NewStringObj(sqlite3_column_name(pStmt,i), -1); 13638e556520Sdanielk1977 Tcl_IncrRefCount(apColName[i]); 13648e556520Sdanielk1977 } 13654a4c11aaSdan p->apColName = apColName; 13664a4c11aaSdan } 13678e556520Sdanielk1977 13688e556520Sdanielk1977 /* If results are being stored in an array variable, then create 13698e556520Sdanielk1977 ** the array(*) entry for that array 13708e556520Sdanielk1977 */ 13714a4c11aaSdan if( p->pArray ){ 13724a4c11aaSdan Tcl_Interp *interp = p->pDb->interp; 13738e556520Sdanielk1977 Tcl_Obj *pColList = Tcl_NewObj(); 13748e556520Sdanielk1977 Tcl_Obj *pStar = Tcl_NewStringObj("*", -1); 13754a4c11aaSdan 13768e556520Sdanielk1977 for(i=0; i<nCol; i++){ 13778e556520Sdanielk1977 Tcl_ListObjAppendElement(interp, pColList, apColName[i]); 13788e556520Sdanielk1977 } 13798e556520Sdanielk1977 Tcl_IncrRefCount(pStar); 13804a4c11aaSdan Tcl_ObjSetVar2(interp, p->pArray, pStar, pColList, 0); 13818e556520Sdanielk1977 Tcl_DecrRefCount(pStar); 13828e556520Sdanielk1977 } 13838e556520Sdanielk1977 } 13848e556520Sdanielk1977 13854a4c11aaSdan if( papColName ){ 13864a4c11aaSdan *papColName = p->apColName; 13874a4c11aaSdan } 13884a4c11aaSdan if( pnCol ){ 13894a4c11aaSdan *pnCol = p->nCol; 13904a4c11aaSdan } 13914a4c11aaSdan } 13924a4c11aaSdan 13934a4c11aaSdan /* 13944a4c11aaSdan ** Return one of TCL_OK, TCL_BREAK or TCL_ERROR. If TCL_ERROR is 13954a4c11aaSdan ** returned, then an error message is stored in the interpreter before 13964a4c11aaSdan ** returning. 13974a4c11aaSdan ** 13984a4c11aaSdan ** A return value of TCL_OK means there is a row of data available. The 13994a4c11aaSdan ** data may be accessed using dbEvalRowInfo() and dbEvalColumnValue(). This 14004a4c11aaSdan ** is analogous to a return of SQLITE_ROW from sqlite3_step(). If TCL_BREAK 14014a4c11aaSdan ** is returned, then the SQL script has finished executing and there are 14024a4c11aaSdan ** no further rows available. This is similar to SQLITE_DONE. 14034a4c11aaSdan */ 14044a4c11aaSdan static int dbEvalStep(DbEvalContext *p){ 1405c431fd55Sdan const char *zPrevSql = 0; /* Previous value of p->zSql */ 1406c431fd55Sdan 14074a4c11aaSdan while( p->zSql[0] || p->pPreStmt ){ 14084a4c11aaSdan int rc; 14094a4c11aaSdan if( p->pPreStmt==0 ){ 1410c431fd55Sdan zPrevSql = (p->zSql==zPrevSql ? 0 : p->zSql); 14114a4c11aaSdan rc = dbPrepareAndBind(p->pDb, p->zSql, &p->zSql, &p->pPreStmt); 14124a4c11aaSdan if( rc!=TCL_OK ) return rc; 14134a4c11aaSdan }else{ 14144a4c11aaSdan int rcs; 14154a4c11aaSdan SqliteDb *pDb = p->pDb; 14164a4c11aaSdan SqlPreparedStmt *pPreStmt = p->pPreStmt; 14174a4c11aaSdan sqlite3_stmt *pStmt = pPreStmt->pStmt; 14184a4c11aaSdan 14194a4c11aaSdan rcs = sqlite3_step(pStmt); 14204a4c11aaSdan if( rcs==SQLITE_ROW ){ 14214a4c11aaSdan return TCL_OK; 14224a4c11aaSdan } 14234a4c11aaSdan if( p->pArray ){ 14244a4c11aaSdan dbEvalRowInfo(p, 0, 0); 14254a4c11aaSdan } 14264a4c11aaSdan rcs = sqlite3_reset(pStmt); 14274a4c11aaSdan 14284a4c11aaSdan pDb->nStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_FULLSCAN_STEP,1); 14294a4c11aaSdan pDb->nSort = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_SORT,1); 14303c379b01Sdrh pDb->nIndex = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_AUTOINDEX,1); 14314a4c11aaSdan dbReleaseColumnNames(p); 14324a4c11aaSdan p->pPreStmt = 0; 14334a4c11aaSdan 14344a4c11aaSdan if( rcs!=SQLITE_OK ){ 14354a4c11aaSdan /* If a run-time error occurs, report the error and stop reading 14364a4c11aaSdan ** the SQL. */ 14374a4c11aaSdan dbReleaseStmt(pDb, pPreStmt, 1); 1438c431fd55Sdan #if SQLITE_TEST 1439c431fd55Sdan if( p->pDb->bLegacyPrepare && rcs==SQLITE_SCHEMA && zPrevSql ){ 1440c431fd55Sdan /* If the runtime error was an SQLITE_SCHEMA, and the database 1441c431fd55Sdan ** handle is configured to use the legacy sqlite3_prepare() 1442c431fd55Sdan ** interface, retry prepare()/step() on the same SQL statement. 1443c431fd55Sdan ** This only happens once. If there is a second SQLITE_SCHEMA 1444c431fd55Sdan ** error, the error will be returned to the caller. */ 1445c431fd55Sdan p->zSql = zPrevSql; 1446c431fd55Sdan continue; 1447c431fd55Sdan } 1448c431fd55Sdan #endif 1449c45e6716Sdrh Tcl_SetObjResult(pDb->interp, 1450c45e6716Sdrh Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 14514a4c11aaSdan return TCL_ERROR; 14524a4c11aaSdan }else{ 14534a4c11aaSdan dbReleaseStmt(pDb, pPreStmt, 0); 14544a4c11aaSdan } 14554a4c11aaSdan } 14564a4c11aaSdan } 14574a4c11aaSdan 14584a4c11aaSdan /* Finished */ 14594a4c11aaSdan return TCL_BREAK; 14604a4c11aaSdan } 14614a4c11aaSdan 14624a4c11aaSdan /* 14634a4c11aaSdan ** Free all resources currently held by the DbEvalContext structure passed 14644a4c11aaSdan ** as the first argument. There should be exactly one call to this function 14654a4c11aaSdan ** for each call to dbEvalInit(). 14664a4c11aaSdan */ 14674a4c11aaSdan static void dbEvalFinalize(DbEvalContext *p){ 14684a4c11aaSdan if( p->pPreStmt ){ 14694a4c11aaSdan sqlite3_reset(p->pPreStmt->pStmt); 14704a4c11aaSdan dbReleaseStmt(p->pDb, p->pPreStmt, 0); 14714a4c11aaSdan p->pPreStmt = 0; 14724a4c11aaSdan } 14734a4c11aaSdan if( p->pArray ){ 14744a4c11aaSdan Tcl_DecrRefCount(p->pArray); 14754a4c11aaSdan p->pArray = 0; 14764a4c11aaSdan } 14774a4c11aaSdan Tcl_DecrRefCount(p->pSql); 14784a4c11aaSdan dbReleaseColumnNames(p); 14794a4c11aaSdan } 14804a4c11aaSdan 14814a4c11aaSdan /* 14824a4c11aaSdan ** Return a pointer to a Tcl_Obj structure with ref-count 0 that contains 14834a4c11aaSdan ** the value for the iCol'th column of the row currently pointed to by 14844a4c11aaSdan ** the DbEvalContext structure passed as the first argument. 14854a4c11aaSdan */ 14864a4c11aaSdan static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){ 14874a4c11aaSdan sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 14884a4c11aaSdan switch( sqlite3_column_type(pStmt, iCol) ){ 14894a4c11aaSdan case SQLITE_BLOB: { 14904a4c11aaSdan int bytes = sqlite3_column_bytes(pStmt, iCol); 14914a4c11aaSdan const char *zBlob = sqlite3_column_blob(pStmt, iCol); 14924a4c11aaSdan if( !zBlob ) bytes = 0; 14934a4c11aaSdan return Tcl_NewByteArrayObj((u8*)zBlob, bytes); 14944a4c11aaSdan } 14954a4c11aaSdan case SQLITE_INTEGER: { 14964a4c11aaSdan sqlite_int64 v = sqlite3_column_int64(pStmt, iCol); 14974a4c11aaSdan if( v>=-2147483647 && v<=2147483647 ){ 14987fd33929Sdrh return Tcl_NewIntObj((int)v); 14994a4c11aaSdan }else{ 15004a4c11aaSdan return Tcl_NewWideIntObj(v); 15014a4c11aaSdan } 15024a4c11aaSdan } 15034a4c11aaSdan case SQLITE_FLOAT: { 15044a4c11aaSdan return Tcl_NewDoubleObj(sqlite3_column_double(pStmt, iCol)); 15054a4c11aaSdan } 15064a4c11aaSdan case SQLITE_NULL: { 1507c45e6716Sdrh return Tcl_NewStringObj(p->pDb->zNull, -1); 15084a4c11aaSdan } 15094a4c11aaSdan } 15104a4c11aaSdan 1511325eff58Sdrh return Tcl_NewStringObj((char*)sqlite3_column_text(pStmt, iCol), -1); 15124a4c11aaSdan } 15134a4c11aaSdan 15144a4c11aaSdan /* 15154a4c11aaSdan ** If using Tcl version 8.6 or greater, use the NR functions to avoid 15164a4c11aaSdan ** recursive evalution of scripts by the [db eval] and [db trans] 15174a4c11aaSdan ** commands. Even if the headers used while compiling the extension 15184a4c11aaSdan ** are 8.6 or newer, the code still tests the Tcl version at runtime. 15194a4c11aaSdan ** This allows stubs-enabled builds to be used with older Tcl libraries. 15204a4c11aaSdan */ 15214a4c11aaSdan #if TCL_MAJOR_VERSION>8 || (TCL_MAJOR_VERSION==8 && TCL_MINOR_VERSION>=6) 1522a2c8a95bSdrh # define SQLITE_TCL_NRE 1 15234a4c11aaSdan static int DbUseNre(void){ 15244a4c11aaSdan int major, minor; 15254a4c11aaSdan Tcl_GetVersion(&major, &minor, 0, 0); 15264a4c11aaSdan return( (major==8 && minor>=6) || major>8 ); 15274a4c11aaSdan } 15284a4c11aaSdan #else 15294a4c11aaSdan /* 15304a4c11aaSdan ** Compiling using headers earlier than 8.6. In this case NR cannot be 15314a4c11aaSdan ** used, so DbUseNre() to always return zero. Add #defines for the other 15324a4c11aaSdan ** Tcl_NRxxx() functions to prevent them from causing compilation errors, 15334a4c11aaSdan ** even though the only invocations of them are within conditional blocks 15344a4c11aaSdan ** of the form: 15354a4c11aaSdan ** 15364a4c11aaSdan ** if( DbUseNre() ) { ... } 15374a4c11aaSdan */ 1538a2c8a95bSdrh # define SQLITE_TCL_NRE 0 15394a4c11aaSdan # define DbUseNre() 0 1540a47941feSdrh # define Tcl_NRAddCallback(a,b,c,d,e,f) (void)0 15414a4c11aaSdan # define Tcl_NREvalObj(a,b,c) 0 1542a47941feSdrh # define Tcl_NRCreateCommand(a,b,c,d,e,f) (void)0 15434a4c11aaSdan #endif 15444a4c11aaSdan 15454a4c11aaSdan /* 15464a4c11aaSdan ** This function is part of the implementation of the command: 15474a4c11aaSdan ** 15484a4c11aaSdan ** $db eval SQL ?ARRAYNAME? SCRIPT 15494a4c11aaSdan */ 15504a4c11aaSdan static int DbEvalNextCmd( 15514a4c11aaSdan ClientData data[], /* data[0] is the (DbEvalContext*) */ 15524a4c11aaSdan Tcl_Interp *interp, /* Tcl interpreter */ 15534a4c11aaSdan int result /* Result so far */ 15544a4c11aaSdan ){ 15554a4c11aaSdan int rc = result; /* Return code */ 15564a4c11aaSdan 15574a4c11aaSdan /* The first element of the data[] array is a pointer to a DbEvalContext 15584a4c11aaSdan ** structure allocated using Tcl_Alloc(). The second element of data[] 15594a4c11aaSdan ** is a pointer to a Tcl_Obj containing the script to run for each row 15604a4c11aaSdan ** returned by the queries encapsulated in data[0]. */ 15614a4c11aaSdan DbEvalContext *p = (DbEvalContext *)data[0]; 15624a4c11aaSdan Tcl_Obj *pScript = (Tcl_Obj *)data[1]; 15634a4c11aaSdan Tcl_Obj *pArray = p->pArray; 15644a4c11aaSdan 15654a4c11aaSdan while( (rc==TCL_OK || rc==TCL_CONTINUE) && TCL_OK==(rc = dbEvalStep(p)) ){ 15664a4c11aaSdan int i; 15674a4c11aaSdan int nCol; 15684a4c11aaSdan Tcl_Obj **apColName; 15694a4c11aaSdan dbEvalRowInfo(p, &nCol, &apColName); 15704a4c11aaSdan for(i=0; i<nCol; i++){ 15714a4c11aaSdan Tcl_Obj *pVal = dbEvalColumnValue(p, i); 15724a4c11aaSdan if( pArray==0 ){ 15734a4c11aaSdan Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0); 15744a4c11aaSdan }else{ 15754a4c11aaSdan Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); 15764a4c11aaSdan } 15774a4c11aaSdan } 15784a4c11aaSdan 15794a4c11aaSdan /* The required interpreter variables are now populated with the data 15804a4c11aaSdan ** from the current row. If using NRE, schedule callbacks to evaluate 15814a4c11aaSdan ** script pScript, then to invoke this function again to fetch the next 15824a4c11aaSdan ** row (or clean up if there is no next row or the script throws an 15834a4c11aaSdan ** exception). After scheduling the callbacks, return control to the 15844a4c11aaSdan ** caller. 15854a4c11aaSdan ** 15864a4c11aaSdan ** If not using NRE, evaluate pScript directly and continue with the 15874a4c11aaSdan ** next iteration of this while(...) loop. */ 15884a4c11aaSdan if( DbUseNre() ){ 15894a4c11aaSdan Tcl_NRAddCallback(interp, DbEvalNextCmd, (void*)p, (void*)pScript, 0, 0); 15904a4c11aaSdan return Tcl_NREvalObj(interp, pScript, 0); 15914a4c11aaSdan }else{ 15924a4c11aaSdan rc = Tcl_EvalObjEx(interp, pScript, 0); 15934a4c11aaSdan } 15944a4c11aaSdan } 15954a4c11aaSdan 15964a4c11aaSdan Tcl_DecrRefCount(pScript); 15974a4c11aaSdan dbEvalFinalize(p); 15984a4c11aaSdan Tcl_Free((char *)p); 15994a4c11aaSdan 16004a4c11aaSdan if( rc==TCL_OK || rc==TCL_BREAK ){ 16014a4c11aaSdan Tcl_ResetResult(interp); 16024a4c11aaSdan rc = TCL_OK; 16034a4c11aaSdan } 16044a4c11aaSdan return rc; 16058e556520Sdanielk1977 } 16068e556520Sdanielk1977 16071067fe11Stpoindex /* 160875897234Sdrh ** The "sqlite" command below creates a new Tcl command for each 160975897234Sdrh ** connection it opens to an SQLite database. This routine is invoked 161075897234Sdrh ** whenever one of those connection-specific commands is executed 161175897234Sdrh ** in Tcl. For example, if you run Tcl code like this: 161275897234Sdrh ** 16139bb575fdSdrh ** sqlite3 db1 "my_database" 161475897234Sdrh ** db1 close 161575897234Sdrh ** 161675897234Sdrh ** The first command opens a connection to the "my_database" database 161775897234Sdrh ** and calls that connection "db1". The second command causes this 161875897234Sdrh ** subroutine to be invoked. 161975897234Sdrh */ 16206d31316cSdrh static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ 1621bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 16226d31316cSdrh int choice; 162322fbcb8dSdrh int rc = TCL_OK; 16240de8c112Sdrh static const char *DB_strs[] = { 1625dc2c4915Sdrh "authorizer", "backup", "busy", 1626dc2c4915Sdrh "cache", "changes", "close", 1627dc2c4915Sdrh "collate", "collation_needed", "commit_hook", 1628dc2c4915Sdrh "complete", "copy", "enable_load_extension", 1629dc2c4915Sdrh "errorcode", "eval", "exists", 1630dc2c4915Sdrh "function", "incrblob", "interrupt", 1631833bf968Sdrh "last_insert_rowid", "nullvalue", "onecolumn", 1632833bf968Sdrh "profile", "progress", "rekey", 1633833bf968Sdrh "restore", "rollback_hook", "status", 1634833bf968Sdrh "timeout", "total_changes", "trace", 1635833bf968Sdrh "transaction", "unlock_notify", "update_hook", 1636833bf968Sdrh "version", "wal_hook", 0 16376d31316cSdrh }; 1638411995dcSdrh enum DB_enum { 1639dc2c4915Sdrh DB_AUTHORIZER, DB_BACKUP, DB_BUSY, 1640dc2c4915Sdrh DB_CACHE, DB_CHANGES, DB_CLOSE, 1641dc2c4915Sdrh DB_COLLATE, DB_COLLATION_NEEDED, DB_COMMIT_HOOK, 1642dc2c4915Sdrh DB_COMPLETE, DB_COPY, DB_ENABLE_LOAD_EXTENSION, 1643dc2c4915Sdrh DB_ERRORCODE, DB_EVAL, DB_EXISTS, 1644dc2c4915Sdrh DB_FUNCTION, DB_INCRBLOB, DB_INTERRUPT, 1645833bf968Sdrh DB_LAST_INSERT_ROWID, DB_NULLVALUE, DB_ONECOLUMN, 1646833bf968Sdrh DB_PROFILE, DB_PROGRESS, DB_REKEY, 1647833bf968Sdrh DB_RESTORE, DB_ROLLBACK_HOOK, DB_STATUS, 1648833bf968Sdrh DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE, 1649833bf968Sdrh DB_TRANSACTION, DB_UNLOCK_NOTIFY, DB_UPDATE_HOOK, 1650833bf968Sdrh DB_VERSION, DB_WAL_HOOK 16516d31316cSdrh }; 16521067fe11Stpoindex /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */ 16536d31316cSdrh 16546d31316cSdrh if( objc<2 ){ 16556d31316cSdrh Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); 165675897234Sdrh return TCL_ERROR; 165775897234Sdrh } 1658411995dcSdrh if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ 16596d31316cSdrh return TCL_ERROR; 16606d31316cSdrh } 16616d31316cSdrh 1662411995dcSdrh switch( (enum DB_enum)choice ){ 166375897234Sdrh 1664e22a334bSdrh /* $db authorizer ?CALLBACK? 1665e22a334bSdrh ** 1666e22a334bSdrh ** Invoke the given callback to authorize each SQL operation as it is 1667e22a334bSdrh ** compiled. 5 arguments are appended to the callback before it is 1668e22a334bSdrh ** invoked: 1669e22a334bSdrh ** 1670e22a334bSdrh ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) 1671e22a334bSdrh ** (2) First descriptive name (depends on authorization type) 1672e22a334bSdrh ** (3) Second descriptive name 1673e22a334bSdrh ** (4) Name of the database (ex: "main", "temp") 1674e22a334bSdrh ** (5) Name of trigger that is doing the access 1675e22a334bSdrh ** 1676e22a334bSdrh ** The callback should return on of the following strings: SQLITE_OK, 1677e22a334bSdrh ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. 1678e22a334bSdrh ** 1679e22a334bSdrh ** If this method is invoked with no arguments, the current authorization 1680e22a334bSdrh ** callback string is returned. 1681e22a334bSdrh */ 1682e22a334bSdrh case DB_AUTHORIZER: { 16831211de37Sdrh #ifdef SQLITE_OMIT_AUTHORIZATION 1684a198f2b5Sdrh Tcl_AppendResult(interp, "authorization not available in this build", 1685a198f2b5Sdrh (char*)0); 16861211de37Sdrh return TCL_ERROR; 16871211de37Sdrh #else 1688e22a334bSdrh if( objc>3 ){ 1689e22a334bSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 16900f14e2ebSdrh return TCL_ERROR; 1691e22a334bSdrh }else if( objc==2 ){ 1692b5a20d3cSdrh if( pDb->zAuth ){ 1693a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zAuth, (char*)0); 1694e22a334bSdrh } 1695e22a334bSdrh }else{ 1696e22a334bSdrh char *zAuth; 1697e22a334bSdrh int len; 1698e22a334bSdrh if( pDb->zAuth ){ 1699e22a334bSdrh Tcl_Free(pDb->zAuth); 1700e22a334bSdrh } 1701e22a334bSdrh zAuth = Tcl_GetStringFromObj(objv[2], &len); 1702e22a334bSdrh if( zAuth && len>0 ){ 1703e22a334bSdrh pDb->zAuth = Tcl_Alloc( len + 1 ); 17045bb3eb9bSdrh memcpy(pDb->zAuth, zAuth, len+1); 1705e22a334bSdrh }else{ 1706e22a334bSdrh pDb->zAuth = 0; 1707e22a334bSdrh } 1708e22a334bSdrh if( pDb->zAuth ){ 170932c6a48bSdrh typedef int (*sqlite3_auth_cb)( 171032c6a48bSdrh void*,int,const char*,const char*, 171132c6a48bSdrh const char*,const char*); 1712e22a334bSdrh pDb->interp = interp; 171332c6a48bSdrh sqlite3_set_authorizer(pDb->db,(sqlite3_auth_cb)auth_callback,pDb); 1714e22a334bSdrh }else{ 17156f8a503dSdanielk1977 sqlite3_set_authorizer(pDb->db, 0, 0); 1716e22a334bSdrh } 1717e22a334bSdrh } 17181211de37Sdrh #endif 1719e22a334bSdrh break; 1720e22a334bSdrh } 1721e22a334bSdrh 1722dc2c4915Sdrh /* $db backup ?DATABASE? FILENAME 1723dc2c4915Sdrh ** 1724dc2c4915Sdrh ** Open or create a database file named FILENAME. Transfer the 1725dc2c4915Sdrh ** content of local database DATABASE (default: "main") into the 1726dc2c4915Sdrh ** FILENAME database. 1727dc2c4915Sdrh */ 1728dc2c4915Sdrh case DB_BACKUP: { 1729dc2c4915Sdrh const char *zDestFile; 1730dc2c4915Sdrh const char *zSrcDb; 1731dc2c4915Sdrh sqlite3 *pDest; 1732dc2c4915Sdrh sqlite3_backup *pBackup; 1733dc2c4915Sdrh 1734dc2c4915Sdrh if( objc==3 ){ 1735dc2c4915Sdrh zSrcDb = "main"; 1736dc2c4915Sdrh zDestFile = Tcl_GetString(objv[2]); 1737dc2c4915Sdrh }else if( objc==4 ){ 1738dc2c4915Sdrh zSrcDb = Tcl_GetString(objv[2]); 1739dc2c4915Sdrh zDestFile = Tcl_GetString(objv[3]); 1740dc2c4915Sdrh }else{ 1741dc2c4915Sdrh Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 1742dc2c4915Sdrh return TCL_ERROR; 1743dc2c4915Sdrh } 1744dc2c4915Sdrh rc = sqlite3_open(zDestFile, &pDest); 1745dc2c4915Sdrh if( rc!=SQLITE_OK ){ 1746dc2c4915Sdrh Tcl_AppendResult(interp, "cannot open target database: ", 1747dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1748dc2c4915Sdrh sqlite3_close(pDest); 1749dc2c4915Sdrh return TCL_ERROR; 1750dc2c4915Sdrh } 1751dc2c4915Sdrh pBackup = sqlite3_backup_init(pDest, "main", pDb->db, zSrcDb); 1752dc2c4915Sdrh if( pBackup==0 ){ 1753dc2c4915Sdrh Tcl_AppendResult(interp, "backup failed: ", 1754dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1755dc2c4915Sdrh sqlite3_close(pDest); 1756dc2c4915Sdrh return TCL_ERROR; 1757dc2c4915Sdrh } 1758dc2c4915Sdrh while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} 1759dc2c4915Sdrh sqlite3_backup_finish(pBackup); 1760dc2c4915Sdrh if( rc==SQLITE_DONE ){ 1761dc2c4915Sdrh rc = TCL_OK; 1762dc2c4915Sdrh }else{ 1763dc2c4915Sdrh Tcl_AppendResult(interp, "backup failed: ", 1764dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1765dc2c4915Sdrh rc = TCL_ERROR; 1766dc2c4915Sdrh } 1767dc2c4915Sdrh sqlite3_close(pDest); 1768dc2c4915Sdrh break; 1769dc2c4915Sdrh } 1770dc2c4915Sdrh 1771bec3f402Sdrh /* $db busy ?CALLBACK? 1772bec3f402Sdrh ** 1773bec3f402Sdrh ** Invoke the given callback if an SQL statement attempts to open 1774bec3f402Sdrh ** a locked database file. 1775bec3f402Sdrh */ 17766d31316cSdrh case DB_BUSY: { 17776d31316cSdrh if( objc>3 ){ 17786d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); 1779bec3f402Sdrh return TCL_ERROR; 17806d31316cSdrh }else if( objc==2 ){ 1781bec3f402Sdrh if( pDb->zBusy ){ 1782a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zBusy, (char*)0); 1783bec3f402Sdrh } 1784bec3f402Sdrh }else{ 17856d31316cSdrh char *zBusy; 17866d31316cSdrh int len; 1787bec3f402Sdrh if( pDb->zBusy ){ 1788bec3f402Sdrh Tcl_Free(pDb->zBusy); 17896d31316cSdrh } 17906d31316cSdrh zBusy = Tcl_GetStringFromObj(objv[2], &len); 17916d31316cSdrh if( zBusy && len>0 ){ 17926d31316cSdrh pDb->zBusy = Tcl_Alloc( len + 1 ); 17935bb3eb9bSdrh memcpy(pDb->zBusy, zBusy, len+1); 17946d31316cSdrh }else{ 1795bec3f402Sdrh pDb->zBusy = 0; 1796bec3f402Sdrh } 1797bec3f402Sdrh if( pDb->zBusy ){ 1798bec3f402Sdrh pDb->interp = interp; 17996f8a503dSdanielk1977 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb); 18006d31316cSdrh }else{ 18016f8a503dSdanielk1977 sqlite3_busy_handler(pDb->db, 0, 0); 1802bec3f402Sdrh } 1803bec3f402Sdrh } 18046d31316cSdrh break; 18056d31316cSdrh } 1806bec3f402Sdrh 1807fb7e7651Sdrh /* $db cache flush 1808fb7e7651Sdrh ** $db cache size n 1809fb7e7651Sdrh ** 1810fb7e7651Sdrh ** Flush the prepared statement cache, or set the maximum number of 1811fb7e7651Sdrh ** cached statements. 1812fb7e7651Sdrh */ 1813fb7e7651Sdrh case DB_CACHE: { 1814fb7e7651Sdrh char *subCmd; 1815fb7e7651Sdrh int n; 1816fb7e7651Sdrh 1817fb7e7651Sdrh if( objc<=2 ){ 1818fb7e7651Sdrh Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?"); 1819fb7e7651Sdrh return TCL_ERROR; 1820fb7e7651Sdrh } 1821fb7e7651Sdrh subCmd = Tcl_GetStringFromObj( objv[2], 0 ); 1822fb7e7651Sdrh if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){ 1823fb7e7651Sdrh if( objc!=3 ){ 1824fb7e7651Sdrh Tcl_WrongNumArgs(interp, 2, objv, "flush"); 1825fb7e7651Sdrh return TCL_ERROR; 1826fb7e7651Sdrh }else{ 1827fb7e7651Sdrh flushStmtCache( pDb ); 1828fb7e7651Sdrh } 1829fb7e7651Sdrh }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){ 1830fb7e7651Sdrh if( objc!=4 ){ 1831fb7e7651Sdrh Tcl_WrongNumArgs(interp, 2, objv, "size n"); 1832fb7e7651Sdrh return TCL_ERROR; 1833fb7e7651Sdrh }else{ 1834fb7e7651Sdrh if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){ 1835fb7e7651Sdrh Tcl_AppendResult( interp, "cannot convert \"", 1836a198f2b5Sdrh Tcl_GetStringFromObj(objv[3],0), "\" to integer", (char*)0); 1837fb7e7651Sdrh return TCL_ERROR; 1838fb7e7651Sdrh }else{ 1839fb7e7651Sdrh if( n<0 ){ 1840fb7e7651Sdrh flushStmtCache( pDb ); 1841fb7e7651Sdrh n = 0; 1842fb7e7651Sdrh }else if( n>MAX_PREPARED_STMTS ){ 1843fb7e7651Sdrh n = MAX_PREPARED_STMTS; 1844fb7e7651Sdrh } 1845fb7e7651Sdrh pDb->maxStmt = n; 1846fb7e7651Sdrh } 1847fb7e7651Sdrh } 1848fb7e7651Sdrh }else{ 1849fb7e7651Sdrh Tcl_AppendResult( interp, "bad option \"", 1850a198f2b5Sdrh Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size", 1851a198f2b5Sdrh (char*)0); 1852fb7e7651Sdrh return TCL_ERROR; 1853fb7e7651Sdrh } 1854fb7e7651Sdrh break; 1855fb7e7651Sdrh } 1856fb7e7651Sdrh 1857b28af71aSdanielk1977 /* $db changes 1858c8d30ac1Sdrh ** 1859c8d30ac1Sdrh ** Return the number of rows that were modified, inserted, or deleted by 1860b28af71aSdanielk1977 ** the most recent INSERT, UPDATE or DELETE statement, not including 1861b28af71aSdanielk1977 ** any changes made by trigger programs. 1862c8d30ac1Sdrh */ 1863c8d30ac1Sdrh case DB_CHANGES: { 1864c8d30ac1Sdrh Tcl_Obj *pResult; 1865c8d30ac1Sdrh if( objc!=2 ){ 1866c8d30ac1Sdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 1867c8d30ac1Sdrh return TCL_ERROR; 1868c8d30ac1Sdrh } 1869c8d30ac1Sdrh pResult = Tcl_GetObjResult(interp); 1870b28af71aSdanielk1977 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db)); 1871f146a776Srdc break; 1872f146a776Srdc } 1873f146a776Srdc 187475897234Sdrh /* $db close 187575897234Sdrh ** 187675897234Sdrh ** Shutdown the database 187775897234Sdrh */ 18786d31316cSdrh case DB_CLOSE: { 18796d31316cSdrh Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); 18806d31316cSdrh break; 18816d31316cSdrh } 188275897234Sdrh 18830f14e2ebSdrh /* 18840f14e2ebSdrh ** $db collate NAME SCRIPT 18850f14e2ebSdrh ** 18860f14e2ebSdrh ** Create a new SQL collation function called NAME. Whenever 18870f14e2ebSdrh ** that function is called, invoke SCRIPT to evaluate the function. 18880f14e2ebSdrh */ 18890f14e2ebSdrh case DB_COLLATE: { 18900f14e2ebSdrh SqlCollate *pCollate; 18910f14e2ebSdrh char *zName; 18920f14e2ebSdrh char *zScript; 18930f14e2ebSdrh int nScript; 18940f14e2ebSdrh if( objc!=4 ){ 18950f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); 18960f14e2ebSdrh return TCL_ERROR; 18970f14e2ebSdrh } 18980f14e2ebSdrh zName = Tcl_GetStringFromObj(objv[2], 0); 18990f14e2ebSdrh zScript = Tcl_GetStringFromObj(objv[3], &nScript); 19000f14e2ebSdrh pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 ); 19010f14e2ebSdrh if( pCollate==0 ) return TCL_ERROR; 19020f14e2ebSdrh pCollate->interp = interp; 19030f14e2ebSdrh pCollate->pNext = pDb->pCollate; 19040f14e2ebSdrh pCollate->zScript = (char*)&pCollate[1]; 19050f14e2ebSdrh pDb->pCollate = pCollate; 19065bb3eb9bSdrh memcpy(pCollate->zScript, zScript, nScript+1); 19070f14e2ebSdrh if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, 19080f14e2ebSdrh pCollate, tclSqlCollate) ){ 19099636c4e1Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 19100f14e2ebSdrh return TCL_ERROR; 19110f14e2ebSdrh } 19120f14e2ebSdrh break; 19130f14e2ebSdrh } 19140f14e2ebSdrh 19150f14e2ebSdrh /* 19160f14e2ebSdrh ** $db collation_needed SCRIPT 19170f14e2ebSdrh ** 19180f14e2ebSdrh ** Create a new SQL collation function called NAME. Whenever 19190f14e2ebSdrh ** that function is called, invoke SCRIPT to evaluate the function. 19200f14e2ebSdrh */ 19210f14e2ebSdrh case DB_COLLATION_NEEDED: { 19220f14e2ebSdrh if( objc!=3 ){ 19230f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT"); 19240f14e2ebSdrh return TCL_ERROR; 19250f14e2ebSdrh } 19260f14e2ebSdrh if( pDb->pCollateNeeded ){ 19270f14e2ebSdrh Tcl_DecrRefCount(pDb->pCollateNeeded); 19280f14e2ebSdrh } 19290f14e2ebSdrh pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]); 19300f14e2ebSdrh Tcl_IncrRefCount(pDb->pCollateNeeded); 19310f14e2ebSdrh sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded); 19320f14e2ebSdrh break; 19330f14e2ebSdrh } 19340f14e2ebSdrh 193519e2d37fSdrh /* $db commit_hook ?CALLBACK? 193619e2d37fSdrh ** 193719e2d37fSdrh ** Invoke the given callback just before committing every SQL transaction. 193819e2d37fSdrh ** If the callback throws an exception or returns non-zero, then the 193919e2d37fSdrh ** transaction is aborted. If CALLBACK is an empty string, the callback 194019e2d37fSdrh ** is disabled. 194119e2d37fSdrh */ 194219e2d37fSdrh case DB_COMMIT_HOOK: { 194319e2d37fSdrh if( objc>3 ){ 194419e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 194519e2d37fSdrh return TCL_ERROR; 194619e2d37fSdrh }else if( objc==2 ){ 194719e2d37fSdrh if( pDb->zCommit ){ 1948a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zCommit, (char*)0); 194919e2d37fSdrh } 195019e2d37fSdrh }else{ 19516ef5e12eSmistachkin const char *zCommit; 195219e2d37fSdrh int len; 195319e2d37fSdrh if( pDb->zCommit ){ 195419e2d37fSdrh Tcl_Free(pDb->zCommit); 195519e2d37fSdrh } 195619e2d37fSdrh zCommit = Tcl_GetStringFromObj(objv[2], &len); 195719e2d37fSdrh if( zCommit && len>0 ){ 195819e2d37fSdrh pDb->zCommit = Tcl_Alloc( len + 1 ); 19595bb3eb9bSdrh memcpy(pDb->zCommit, zCommit, len+1); 196019e2d37fSdrh }else{ 196119e2d37fSdrh pDb->zCommit = 0; 196219e2d37fSdrh } 196319e2d37fSdrh if( pDb->zCommit ){ 196419e2d37fSdrh pDb->interp = interp; 196519e2d37fSdrh sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb); 196619e2d37fSdrh }else{ 196719e2d37fSdrh sqlite3_commit_hook(pDb->db, 0, 0); 196819e2d37fSdrh } 196919e2d37fSdrh } 197019e2d37fSdrh break; 197119e2d37fSdrh } 197219e2d37fSdrh 197375897234Sdrh /* $db complete SQL 197475897234Sdrh ** 197575897234Sdrh ** Return TRUE if SQL is a complete SQL statement. Return FALSE if 197675897234Sdrh ** additional lines of input are needed. This is similar to the 197775897234Sdrh ** built-in "info complete" command of Tcl. 197875897234Sdrh */ 19796d31316cSdrh case DB_COMPLETE: { 1980ccae6026Sdrh #ifndef SQLITE_OMIT_COMPLETE 19816d31316cSdrh Tcl_Obj *pResult; 19826d31316cSdrh int isComplete; 19836d31316cSdrh if( objc!=3 ){ 19846d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 198575897234Sdrh return TCL_ERROR; 198675897234Sdrh } 19876f8a503dSdanielk1977 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) ); 19886d31316cSdrh pResult = Tcl_GetObjResult(interp); 19896d31316cSdrh Tcl_SetBooleanObj(pResult, isComplete); 1990ccae6026Sdrh #endif 19916d31316cSdrh break; 19926d31316cSdrh } 199375897234Sdrh 199419e2d37fSdrh /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR? 199519e2d37fSdrh ** 199619e2d37fSdrh ** Copy data into table from filename, optionally using SEPARATOR 199719e2d37fSdrh ** as column separators. If a column contains a null string, or the 199819e2d37fSdrh ** value of NULLINDICATOR, a NULL is inserted for the column. 199919e2d37fSdrh ** conflict-algorithm is one of the sqlite conflict algorithms: 200019e2d37fSdrh ** rollback, abort, fail, ignore, replace 200119e2d37fSdrh ** On success, return the number of lines processed, not necessarily same 200219e2d37fSdrh ** as 'db changes' due to conflict-algorithm selected. 200319e2d37fSdrh ** 200419e2d37fSdrh ** This code is basically an implementation/enhancement of 200519e2d37fSdrh ** the sqlite3 shell.c ".import" command. 200619e2d37fSdrh ** 200719e2d37fSdrh ** This command usage is equivalent to the sqlite2.x COPY statement, 200819e2d37fSdrh ** which imports file data into a table using the PostgreSQL COPY file format: 200919e2d37fSdrh ** $db copy $conflit_algo $table_name $filename \t \\N 201019e2d37fSdrh */ 201119e2d37fSdrh case DB_COPY: { 201219e2d37fSdrh char *zTable; /* Insert data into this table */ 201319e2d37fSdrh char *zFile; /* The file from which to extract data */ 201419e2d37fSdrh char *zConflict; /* The conflict algorithm to use */ 201519e2d37fSdrh sqlite3_stmt *pStmt; /* A statement */ 201619e2d37fSdrh int nCol; /* Number of columns in the table */ 201719e2d37fSdrh int nByte; /* Number of bytes in an SQL string */ 201819e2d37fSdrh int i, j; /* Loop counters */ 201919e2d37fSdrh int nSep; /* Number of bytes in zSep[] */ 202019e2d37fSdrh int nNull; /* Number of bytes in zNull[] */ 202119e2d37fSdrh char *zSql; /* An SQL statement */ 202219e2d37fSdrh char *zLine; /* A single line of input from the file */ 202319e2d37fSdrh char **azCol; /* zLine[] broken up into columns */ 20246ef5e12eSmistachkin const char *zCommit; /* How to commit changes */ 202519e2d37fSdrh FILE *in; /* The input file */ 202619e2d37fSdrh int lineno = 0; /* Line number of input file */ 202719e2d37fSdrh char zLineNum[80]; /* Line number print buffer */ 202819e2d37fSdrh Tcl_Obj *pResult; /* interp result */ 202919e2d37fSdrh 20306ef5e12eSmistachkin const char *zSep; 20316ef5e12eSmistachkin const char *zNull; 203219e2d37fSdrh if( objc<5 || objc>7 ){ 203319e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, 203419e2d37fSdrh "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"); 203519e2d37fSdrh return TCL_ERROR; 203619e2d37fSdrh } 203719e2d37fSdrh if( objc>=6 ){ 203819e2d37fSdrh zSep = Tcl_GetStringFromObj(objv[5], 0); 203919e2d37fSdrh }else{ 204019e2d37fSdrh zSep = "\t"; 204119e2d37fSdrh } 204219e2d37fSdrh if( objc>=7 ){ 204319e2d37fSdrh zNull = Tcl_GetStringFromObj(objv[6], 0); 204419e2d37fSdrh }else{ 204519e2d37fSdrh zNull = ""; 204619e2d37fSdrh } 204719e2d37fSdrh zConflict = Tcl_GetStringFromObj(objv[2], 0); 204819e2d37fSdrh zTable = Tcl_GetStringFromObj(objv[3], 0); 204919e2d37fSdrh zFile = Tcl_GetStringFromObj(objv[4], 0); 20504f21c4afSdrh nSep = strlen30(zSep); 20514f21c4afSdrh nNull = strlen30(zNull); 205219e2d37fSdrh if( nSep==0 ){ 2053a198f2b5Sdrh Tcl_AppendResult(interp,"Error: non-null separator required for copy", 2054a198f2b5Sdrh (char*)0); 205519e2d37fSdrh return TCL_ERROR; 205619e2d37fSdrh } 20573e59c012Sdrh if(strcmp(zConflict, "rollback") != 0 && 20583e59c012Sdrh strcmp(zConflict, "abort" ) != 0 && 20593e59c012Sdrh strcmp(zConflict, "fail" ) != 0 && 20603e59c012Sdrh strcmp(zConflict, "ignore" ) != 0 && 20613e59c012Sdrh strcmp(zConflict, "replace" ) != 0 ) { 206219e2d37fSdrh Tcl_AppendResult(interp, "Error: \"", zConflict, 206319e2d37fSdrh "\", conflict-algorithm must be one of: rollback, " 2064a198f2b5Sdrh "abort, fail, ignore, or replace", (char*)0); 206519e2d37fSdrh return TCL_ERROR; 206619e2d37fSdrh } 206719e2d37fSdrh zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); 206819e2d37fSdrh if( zSql==0 ){ 2069a198f2b5Sdrh Tcl_AppendResult(interp, "Error: no such table: ", zTable, (char*)0); 207019e2d37fSdrh return TCL_ERROR; 207119e2d37fSdrh } 20724f21c4afSdrh nByte = strlen30(zSql); 20733e701a18Sdrh rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 207419e2d37fSdrh sqlite3_free(zSql); 207519e2d37fSdrh if( rc ){ 2076a198f2b5Sdrh Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0); 207719e2d37fSdrh nCol = 0; 207819e2d37fSdrh }else{ 207919e2d37fSdrh nCol = sqlite3_column_count(pStmt); 208019e2d37fSdrh } 208119e2d37fSdrh sqlite3_finalize(pStmt); 208219e2d37fSdrh if( nCol==0 ) { 208319e2d37fSdrh return TCL_ERROR; 208419e2d37fSdrh } 208519e2d37fSdrh zSql = malloc( nByte + 50 + nCol*2 ); 208619e2d37fSdrh if( zSql==0 ) { 2087a198f2b5Sdrh Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0); 208819e2d37fSdrh return TCL_ERROR; 208919e2d37fSdrh } 209019e2d37fSdrh sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?", 209119e2d37fSdrh zConflict, zTable); 20924f21c4afSdrh j = strlen30(zSql); 209319e2d37fSdrh for(i=1; i<nCol; i++){ 209419e2d37fSdrh zSql[j++] = ','; 209519e2d37fSdrh zSql[j++] = '?'; 209619e2d37fSdrh } 209719e2d37fSdrh zSql[j++] = ')'; 209819e2d37fSdrh zSql[j] = 0; 20993e701a18Sdrh rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 210019e2d37fSdrh free(zSql); 210119e2d37fSdrh if( rc ){ 2102a198f2b5Sdrh Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0); 210319e2d37fSdrh sqlite3_finalize(pStmt); 210419e2d37fSdrh return TCL_ERROR; 210519e2d37fSdrh } 210619e2d37fSdrh in = fopen(zFile, "rb"); 210719e2d37fSdrh if( in==0 ){ 210819e2d37fSdrh Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL); 210919e2d37fSdrh sqlite3_finalize(pStmt); 211019e2d37fSdrh return TCL_ERROR; 211119e2d37fSdrh } 211219e2d37fSdrh azCol = malloc( sizeof(azCol[0])*(nCol+1) ); 211319e2d37fSdrh if( azCol==0 ) { 2114a198f2b5Sdrh Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0); 211543617e9aSdrh fclose(in); 211619e2d37fSdrh return TCL_ERROR; 211719e2d37fSdrh } 21183752785fSdrh (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0); 211919e2d37fSdrh zCommit = "COMMIT"; 212019e2d37fSdrh while( (zLine = local_getline(0, in))!=0 ){ 212119e2d37fSdrh char *z; 212219e2d37fSdrh lineno++; 212319e2d37fSdrh azCol[0] = zLine; 212419e2d37fSdrh for(i=0, z=zLine; *z; z++){ 212519e2d37fSdrh if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){ 212619e2d37fSdrh *z = 0; 212719e2d37fSdrh i++; 212819e2d37fSdrh if( i<nCol ){ 212919e2d37fSdrh azCol[i] = &z[nSep]; 213019e2d37fSdrh z += nSep-1; 213119e2d37fSdrh } 213219e2d37fSdrh } 213319e2d37fSdrh } 213419e2d37fSdrh if( i+1!=nCol ){ 213519e2d37fSdrh char *zErr; 21364f21c4afSdrh int nErr = strlen30(zFile) + 200; 21375bb3eb9bSdrh zErr = malloc(nErr); 2138c1f4494eSdrh if( zErr ){ 21395bb3eb9bSdrh sqlite3_snprintf(nErr, zErr, 2140955de52cSdanielk1977 "Error: %s line %d: expected %d columns of data but found %d", 214119e2d37fSdrh zFile, lineno, nCol, i+1); 2142a198f2b5Sdrh Tcl_AppendResult(interp, zErr, (char*)0); 214319e2d37fSdrh free(zErr); 2144c1f4494eSdrh } 214519e2d37fSdrh zCommit = "ROLLBACK"; 214619e2d37fSdrh break; 214719e2d37fSdrh } 214819e2d37fSdrh for(i=0; i<nCol; i++){ 214919e2d37fSdrh /* check for null data, if so, bind as null */ 2150ea678832Sdrh if( (nNull>0 && strcmp(azCol[i], zNull)==0) 21514f21c4afSdrh || strlen30(azCol[i])==0 2152ea678832Sdrh ){ 215319e2d37fSdrh sqlite3_bind_null(pStmt, i+1); 215419e2d37fSdrh }else{ 215519e2d37fSdrh sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); 215619e2d37fSdrh } 215719e2d37fSdrh } 215819e2d37fSdrh sqlite3_step(pStmt); 215919e2d37fSdrh rc = sqlite3_reset(pStmt); 216019e2d37fSdrh free(zLine); 216119e2d37fSdrh if( rc!=SQLITE_OK ){ 2162a198f2b5Sdrh Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), (char*)0); 216319e2d37fSdrh zCommit = "ROLLBACK"; 216419e2d37fSdrh break; 216519e2d37fSdrh } 216619e2d37fSdrh } 216719e2d37fSdrh free(azCol); 216819e2d37fSdrh fclose(in); 216919e2d37fSdrh sqlite3_finalize(pStmt); 21703752785fSdrh (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0); 217119e2d37fSdrh 217219e2d37fSdrh if( zCommit[0] == 'C' ){ 217319e2d37fSdrh /* success, set result as number of lines processed */ 217419e2d37fSdrh pResult = Tcl_GetObjResult(interp); 217519e2d37fSdrh Tcl_SetIntObj(pResult, lineno); 217619e2d37fSdrh rc = TCL_OK; 217719e2d37fSdrh }else{ 217819e2d37fSdrh /* failure, append lineno where failed */ 21795bb3eb9bSdrh sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno); 2180a198f2b5Sdrh Tcl_AppendResult(interp,", failed while processing line: ",zLineNum, 2181a198f2b5Sdrh (char*)0); 218219e2d37fSdrh rc = TCL_ERROR; 218319e2d37fSdrh } 218419e2d37fSdrh break; 218519e2d37fSdrh } 218619e2d37fSdrh 218775897234Sdrh /* 21884144905bSdrh ** $db enable_load_extension BOOLEAN 21894144905bSdrh ** 21904144905bSdrh ** Turn the extension loading feature on or off. It if off by 21914144905bSdrh ** default. 21924144905bSdrh */ 21934144905bSdrh case DB_ENABLE_LOAD_EXTENSION: { 2194f533acc0Sdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 21954144905bSdrh int onoff; 21964144905bSdrh if( objc!=3 ){ 21974144905bSdrh Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN"); 21984144905bSdrh return TCL_ERROR; 21994144905bSdrh } 22004144905bSdrh if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ 22014144905bSdrh return TCL_ERROR; 22024144905bSdrh } 22034144905bSdrh sqlite3_enable_load_extension(pDb->db, onoff); 22044144905bSdrh break; 2205f533acc0Sdrh #else 2206f533acc0Sdrh Tcl_AppendResult(interp, "extension loading is turned off at compile-time", 2207a198f2b5Sdrh (char*)0); 2208f533acc0Sdrh return TCL_ERROR; 2209f533acc0Sdrh #endif 22104144905bSdrh } 22114144905bSdrh 22124144905bSdrh /* 2213dcd997eaSdrh ** $db errorcode 2214dcd997eaSdrh ** 2215dcd997eaSdrh ** Return the numeric error code that was returned by the most recent 22166f8a503dSdanielk1977 ** call to sqlite3_exec(). 2217dcd997eaSdrh */ 2218dcd997eaSdrh case DB_ERRORCODE: { 2219f3ce83f5Sdanielk1977 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); 2220dcd997eaSdrh break; 2221dcd997eaSdrh } 2222dcd997eaSdrh 2223dcd997eaSdrh /* 22244a4c11aaSdan ** $db exists $sql 22251807ce37Sdrh ** $db onecolumn $sql 222675897234Sdrh ** 22274a4c11aaSdan ** The onecolumn method is the equivalent of: 22284a4c11aaSdan ** lindex [$db eval $sql] 0 22294a4c11aaSdan */ 22304a4c11aaSdan case DB_EXISTS: 22314a4c11aaSdan case DB_ONECOLUMN: { 22324a4c11aaSdan DbEvalContext sEval; 22334a4c11aaSdan if( objc!=3 ){ 22344a4c11aaSdan Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 22354a4c11aaSdan return TCL_ERROR; 22364a4c11aaSdan } 22374a4c11aaSdan 22384a4c11aaSdan dbEvalInit(&sEval, pDb, objv[2], 0); 22394a4c11aaSdan rc = dbEvalStep(&sEval); 22404a4c11aaSdan if( choice==DB_ONECOLUMN ){ 22414a4c11aaSdan if( rc==TCL_OK ){ 22424a4c11aaSdan Tcl_SetObjResult(interp, dbEvalColumnValue(&sEval, 0)); 2243d5f12cd5Sdan }else if( rc==TCL_BREAK ){ 2244d5f12cd5Sdan Tcl_ResetResult(interp); 22454a4c11aaSdan } 22464a4c11aaSdan }else if( rc==TCL_BREAK || rc==TCL_OK ){ 22474a4c11aaSdan Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc==TCL_OK)); 22484a4c11aaSdan } 22494a4c11aaSdan dbEvalFinalize(&sEval); 22504a4c11aaSdan 22514a4c11aaSdan if( rc==TCL_BREAK ){ 22524a4c11aaSdan rc = TCL_OK; 22534a4c11aaSdan } 22544a4c11aaSdan break; 22554a4c11aaSdan } 22564a4c11aaSdan 22574a4c11aaSdan /* 22584a4c11aaSdan ** $db eval $sql ?array? ?{ ...code... }? 22594a4c11aaSdan ** 226075897234Sdrh ** The SQL statement in $sql is evaluated. For each row, the values are 2261bec3f402Sdrh ** placed in elements of the array named "array" and ...code... is executed. 226275897234Sdrh ** If "array" and "code" are omitted, then no callback is every invoked. 226375897234Sdrh ** If "array" is an empty string, then the values are placed in variables 226475897234Sdrh ** that have the same name as the fields extracted by the query. 226575897234Sdrh */ 22664a4c11aaSdan case DB_EVAL: { 226792febd92Sdrh if( objc<3 || objc>5 ){ 2268895d7472Sdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?"); 226930ccda10Sdanielk1977 return TCL_ERROR; 227030ccda10Sdanielk1977 } 22714a4c11aaSdan 227292febd92Sdrh if( objc==3 ){ 22734a4c11aaSdan DbEvalContext sEval; 22744a4c11aaSdan Tcl_Obj *pRet = Tcl_NewObj(); 22754a4c11aaSdan Tcl_IncrRefCount(pRet); 22764a4c11aaSdan dbEvalInit(&sEval, pDb, objv[2], 0); 22774a4c11aaSdan while( TCL_OK==(rc = dbEvalStep(&sEval)) ){ 22784a4c11aaSdan int i; 22794a4c11aaSdan int nCol; 22804a4c11aaSdan dbEvalRowInfo(&sEval, &nCol, 0); 228192febd92Sdrh for(i=0; i<nCol; i++){ 22824a4c11aaSdan Tcl_ListObjAppendElement(interp, pRet, dbEvalColumnValue(&sEval, i)); 228392febd92Sdrh } 228430ccda10Sdanielk1977 } 22854a4c11aaSdan dbEvalFinalize(&sEval); 228690b6bb19Sdrh if( rc==TCL_BREAK ){ 22874a4c11aaSdan Tcl_SetObjResult(interp, pRet); 228890b6bb19Sdrh rc = TCL_OK; 228990b6bb19Sdrh } 2290ef2cb63eSdanielk1977 Tcl_DecrRefCount(pRet); 22914a4c11aaSdan }else{ 22924a4c11aaSdan ClientData cd[2]; 22934a4c11aaSdan DbEvalContext *p; 22944a4c11aaSdan Tcl_Obj *pArray = 0; 22954a4c11aaSdan Tcl_Obj *pScript; 22964a4c11aaSdan 22974a4c11aaSdan if( objc==5 && *(char *)Tcl_GetString(objv[3]) ){ 22984a4c11aaSdan pArray = objv[3]; 22994a4c11aaSdan } 23004a4c11aaSdan pScript = objv[objc-1]; 23014a4c11aaSdan Tcl_IncrRefCount(pScript); 23024a4c11aaSdan 23034a4c11aaSdan p = (DbEvalContext *)Tcl_Alloc(sizeof(DbEvalContext)); 23044a4c11aaSdan dbEvalInit(p, pDb, objv[2], pArray); 23054a4c11aaSdan 23064a4c11aaSdan cd[0] = (void *)p; 23074a4c11aaSdan cd[1] = (void *)pScript; 23084a4c11aaSdan rc = DbEvalNextCmd(cd, interp, TCL_OK); 23091807ce37Sdrh } 231030ccda10Sdanielk1977 break; 231130ccda10Sdanielk1977 } 2312bec3f402Sdrh 2313bec3f402Sdrh /* 2314e3602be8Sdrh ** $db function NAME [-argcount N] SCRIPT 2315cabb0819Sdrh ** 2316cabb0819Sdrh ** Create a new SQL function called NAME. Whenever that function is 2317cabb0819Sdrh ** called, invoke SCRIPT to evaluate the function. 2318cabb0819Sdrh */ 2319cabb0819Sdrh case DB_FUNCTION: { 2320cabb0819Sdrh SqlFunc *pFunc; 2321d1e4733dSdrh Tcl_Obj *pScript; 2322cabb0819Sdrh char *zName; 2323e3602be8Sdrh int nArg = -1; 2324e3602be8Sdrh if( objc==6 ){ 2325e3602be8Sdrh const char *z = Tcl_GetString(objv[3]); 23264f21c4afSdrh int n = strlen30(z); 2327e3602be8Sdrh if( n>2 && strncmp(z, "-argcount",n)==0 ){ 2328e3602be8Sdrh if( Tcl_GetIntFromObj(interp, objv[4], &nArg) ) return TCL_ERROR; 2329e3602be8Sdrh if( nArg<0 ){ 2330e3602be8Sdrh Tcl_AppendResult(interp, "number of arguments must be non-negative", 2331e3602be8Sdrh (char*)0); 2332cabb0819Sdrh return TCL_ERROR; 2333cabb0819Sdrh } 2334e3602be8Sdrh } 2335e3602be8Sdrh pScript = objv[5]; 2336e3602be8Sdrh }else if( objc!=4 ){ 2337e3602be8Sdrh Tcl_WrongNumArgs(interp, 2, objv, "NAME [-argcount N] SCRIPT"); 2338e3602be8Sdrh return TCL_ERROR; 2339e3602be8Sdrh }else{ 2340d1e4733dSdrh pScript = objv[3]; 2341e3602be8Sdrh } 2342e3602be8Sdrh zName = Tcl_GetStringFromObj(objv[2], 0); 2343d1e4733dSdrh pFunc = findSqlFunc(pDb, zName); 2344cabb0819Sdrh if( pFunc==0 ) return TCL_ERROR; 2345d1e4733dSdrh if( pFunc->pScript ){ 2346d1e4733dSdrh Tcl_DecrRefCount(pFunc->pScript); 2347d1e4733dSdrh } 2348d1e4733dSdrh pFunc->pScript = pScript; 2349d1e4733dSdrh Tcl_IncrRefCount(pScript); 2350d1e4733dSdrh pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript); 2351e3602be8Sdrh rc = sqlite3_create_function(pDb->db, zName, nArg, SQLITE_UTF8, 2352d8123366Sdanielk1977 pFunc, tclSqlFunc, 0, 0); 2353fb7e7651Sdrh if( rc!=SQLITE_OK ){ 2354fb7e7651Sdrh rc = TCL_ERROR; 23559636c4e1Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 2356fb7e7651Sdrh } 2357cabb0819Sdrh break; 2358cabb0819Sdrh } 2359cabb0819Sdrh 2360cabb0819Sdrh /* 23618cbadb02Sdanielk1977 ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID 2362b4e9af9fSdanielk1977 */ 2363b4e9af9fSdanielk1977 case DB_INCRBLOB: { 236432a0d8bbSdanielk1977 #ifdef SQLITE_OMIT_INCRBLOB 2365a198f2b5Sdrh Tcl_AppendResult(interp, "incrblob not available in this build", (char*)0); 236632a0d8bbSdanielk1977 return TCL_ERROR; 236732a0d8bbSdanielk1977 #else 23688cbadb02Sdanielk1977 int isReadonly = 0; 2369b4e9af9fSdanielk1977 const char *zDb = "main"; 2370b4e9af9fSdanielk1977 const char *zTable; 2371b4e9af9fSdanielk1977 const char *zColumn; 2372b3f787f4Sdrh Tcl_WideInt iRow; 2373b4e9af9fSdanielk1977 23748cbadb02Sdanielk1977 /* Check for the -readonly option */ 23758cbadb02Sdanielk1977 if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){ 23768cbadb02Sdanielk1977 isReadonly = 1; 23778cbadb02Sdanielk1977 } 23788cbadb02Sdanielk1977 23798cbadb02Sdanielk1977 if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){ 23808cbadb02Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID"); 2381b4e9af9fSdanielk1977 return TCL_ERROR; 2382b4e9af9fSdanielk1977 } 2383b4e9af9fSdanielk1977 23848cbadb02Sdanielk1977 if( objc==(6+isReadonly) ){ 2385b4e9af9fSdanielk1977 zDb = Tcl_GetString(objv[2]); 2386b4e9af9fSdanielk1977 } 2387b4e9af9fSdanielk1977 zTable = Tcl_GetString(objv[objc-3]); 2388b4e9af9fSdanielk1977 zColumn = Tcl_GetString(objv[objc-2]); 2389b4e9af9fSdanielk1977 rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow); 2390b4e9af9fSdanielk1977 2391b4e9af9fSdanielk1977 if( rc==TCL_OK ){ 23928cbadb02Sdanielk1977 rc = createIncrblobChannel( 2393edf5b165Sdan interp, pDb, zDb, zTable, zColumn, (sqlite3_int64)iRow, isReadonly 23948cbadb02Sdanielk1977 ); 2395b4e9af9fSdanielk1977 } 239632a0d8bbSdanielk1977 #endif 2397b4e9af9fSdanielk1977 break; 2398b4e9af9fSdanielk1977 } 2399b4e9af9fSdanielk1977 2400b4e9af9fSdanielk1977 /* 2401f11bded5Sdrh ** $db interrupt 2402f11bded5Sdrh ** 2403f11bded5Sdrh ** Interrupt the execution of the inner-most SQL interpreter. This 2404f11bded5Sdrh ** causes the SQL statement to return an error of SQLITE_INTERRUPT. 2405f11bded5Sdrh */ 2406f11bded5Sdrh case DB_INTERRUPT: { 2407f11bded5Sdrh sqlite3_interrupt(pDb->db); 2408f11bded5Sdrh break; 2409f11bded5Sdrh } 2410f11bded5Sdrh 2411f11bded5Sdrh /* 241219e2d37fSdrh ** $db nullvalue ?STRING? 241319e2d37fSdrh ** 241419e2d37fSdrh ** Change text used when a NULL comes back from the database. If ?STRING? 241519e2d37fSdrh ** is not present, then the current string used for NULL is returned. 241619e2d37fSdrh ** If STRING is present, then STRING is returned. 241719e2d37fSdrh ** 241819e2d37fSdrh */ 241919e2d37fSdrh case DB_NULLVALUE: { 242019e2d37fSdrh if( objc!=2 && objc!=3 ){ 242119e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE"); 242219e2d37fSdrh return TCL_ERROR; 242319e2d37fSdrh } 242419e2d37fSdrh if( objc==3 ){ 242519e2d37fSdrh int len; 242619e2d37fSdrh char *zNull = Tcl_GetStringFromObj(objv[2], &len); 242719e2d37fSdrh if( pDb->zNull ){ 242819e2d37fSdrh Tcl_Free(pDb->zNull); 242919e2d37fSdrh } 243019e2d37fSdrh if( zNull && len>0 ){ 243119e2d37fSdrh pDb->zNull = Tcl_Alloc( len + 1 ); 24327fd33929Sdrh memcpy(pDb->zNull, zNull, len); 243319e2d37fSdrh pDb->zNull[len] = '\0'; 243419e2d37fSdrh }else{ 243519e2d37fSdrh pDb->zNull = 0; 243619e2d37fSdrh } 243719e2d37fSdrh } 2438c45e6716Sdrh Tcl_SetObjResult(interp, Tcl_NewStringObj(pDb->zNull, -1)); 243919e2d37fSdrh break; 244019e2d37fSdrh } 244119e2d37fSdrh 244219e2d37fSdrh /* 2443af9ff33aSdrh ** $db last_insert_rowid 2444af9ff33aSdrh ** 2445af9ff33aSdrh ** Return an integer which is the ROWID for the most recent insert. 2446af9ff33aSdrh */ 2447af9ff33aSdrh case DB_LAST_INSERT_ROWID: { 2448af9ff33aSdrh Tcl_Obj *pResult; 2449f7e678d6Sdrh Tcl_WideInt rowid; 2450af9ff33aSdrh if( objc!=2 ){ 2451af9ff33aSdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 2452af9ff33aSdrh return TCL_ERROR; 2453af9ff33aSdrh } 24546f8a503dSdanielk1977 rowid = sqlite3_last_insert_rowid(pDb->db); 2455af9ff33aSdrh pResult = Tcl_GetObjResult(interp); 2456f7e678d6Sdrh Tcl_SetWideIntObj(pResult, rowid); 2457af9ff33aSdrh break; 2458af9ff33aSdrh } 2459af9ff33aSdrh 2460af9ff33aSdrh /* 24614a4c11aaSdan ** The DB_ONECOLUMN method is implemented together with DB_EXISTS. 24625d9d7576Sdrh */ 24631807ce37Sdrh 24641807ce37Sdrh /* $db progress ?N CALLBACK? 24651807ce37Sdrh ** 24661807ce37Sdrh ** Invoke the given callback every N virtual machine opcodes while executing 24671807ce37Sdrh ** queries. 24681807ce37Sdrh */ 24691807ce37Sdrh case DB_PROGRESS: { 24701807ce37Sdrh if( objc==2 ){ 24711807ce37Sdrh if( pDb->zProgress ){ 2472a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zProgress, (char*)0); 24735d9d7576Sdrh } 24741807ce37Sdrh }else if( objc==4 ){ 24751807ce37Sdrh char *zProgress; 24761807ce37Sdrh int len; 24771807ce37Sdrh int N; 24781807ce37Sdrh if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ 24791807ce37Sdrh return TCL_ERROR; 24801807ce37Sdrh }; 24811807ce37Sdrh if( pDb->zProgress ){ 24821807ce37Sdrh Tcl_Free(pDb->zProgress); 24831807ce37Sdrh } 24841807ce37Sdrh zProgress = Tcl_GetStringFromObj(objv[3], &len); 24851807ce37Sdrh if( zProgress && len>0 ){ 24861807ce37Sdrh pDb->zProgress = Tcl_Alloc( len + 1 ); 24875bb3eb9bSdrh memcpy(pDb->zProgress, zProgress, len+1); 24881807ce37Sdrh }else{ 24891807ce37Sdrh pDb->zProgress = 0; 24901807ce37Sdrh } 24911807ce37Sdrh #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 24921807ce37Sdrh if( pDb->zProgress ){ 24931807ce37Sdrh pDb->interp = interp; 24941807ce37Sdrh sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb); 24951807ce37Sdrh }else{ 24961807ce37Sdrh sqlite3_progress_handler(pDb->db, 0, 0, 0); 24971807ce37Sdrh } 24981807ce37Sdrh #endif 24991807ce37Sdrh }else{ 25001807ce37Sdrh Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); 25011807ce37Sdrh return TCL_ERROR; 25025d9d7576Sdrh } 25035d9d7576Sdrh break; 25045d9d7576Sdrh } 25055d9d7576Sdrh 250619e2d37fSdrh /* $db profile ?CALLBACK? 250719e2d37fSdrh ** 250819e2d37fSdrh ** Make arrangements to invoke the CALLBACK routine after each SQL statement 250919e2d37fSdrh ** that has run. The text of the SQL and the amount of elapse time are 251019e2d37fSdrh ** appended to CALLBACK before the script is run. 251119e2d37fSdrh */ 251219e2d37fSdrh case DB_PROFILE: { 251319e2d37fSdrh if( objc>3 ){ 251419e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 251519e2d37fSdrh return TCL_ERROR; 251619e2d37fSdrh }else if( objc==2 ){ 251719e2d37fSdrh if( pDb->zProfile ){ 2518a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zProfile, (char*)0); 251919e2d37fSdrh } 252019e2d37fSdrh }else{ 252119e2d37fSdrh char *zProfile; 252219e2d37fSdrh int len; 252319e2d37fSdrh if( pDb->zProfile ){ 252419e2d37fSdrh Tcl_Free(pDb->zProfile); 252519e2d37fSdrh } 252619e2d37fSdrh zProfile = Tcl_GetStringFromObj(objv[2], &len); 252719e2d37fSdrh if( zProfile && len>0 ){ 252819e2d37fSdrh pDb->zProfile = Tcl_Alloc( len + 1 ); 25295bb3eb9bSdrh memcpy(pDb->zProfile, zProfile, len+1); 253019e2d37fSdrh }else{ 253119e2d37fSdrh pDb->zProfile = 0; 253219e2d37fSdrh } 2533bb201344Sshaneh #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) 253419e2d37fSdrh if( pDb->zProfile ){ 253519e2d37fSdrh pDb->interp = interp; 253619e2d37fSdrh sqlite3_profile(pDb->db, DbProfileHandler, pDb); 253719e2d37fSdrh }else{ 253819e2d37fSdrh sqlite3_profile(pDb->db, 0, 0); 253919e2d37fSdrh } 254019e2d37fSdrh #endif 254119e2d37fSdrh } 254219e2d37fSdrh break; 254319e2d37fSdrh } 254419e2d37fSdrh 25455d9d7576Sdrh /* 254622fbcb8dSdrh ** $db rekey KEY 254722fbcb8dSdrh ** 254822fbcb8dSdrh ** Change the encryption key on the currently open database. 254922fbcb8dSdrh */ 255022fbcb8dSdrh case DB_REKEY: { 2551b07028f7Sdrh #ifdef SQLITE_HAS_CODEC 255222fbcb8dSdrh int nKey; 255322fbcb8dSdrh void *pKey; 2554b07028f7Sdrh #endif 255522fbcb8dSdrh if( objc!=3 ){ 255622fbcb8dSdrh Tcl_WrongNumArgs(interp, 2, objv, "KEY"); 255722fbcb8dSdrh return TCL_ERROR; 255822fbcb8dSdrh } 25599eb9e26bSdrh #ifdef SQLITE_HAS_CODEC 2560b07028f7Sdrh pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); 25612011d5f5Sdrh rc = sqlite3_rekey(pDb->db, pKey, nKey); 256222fbcb8dSdrh if( rc ){ 2563a198f2b5Sdrh Tcl_AppendResult(interp, sqlite3_errstr(rc), (char*)0); 256422fbcb8dSdrh rc = TCL_ERROR; 256522fbcb8dSdrh } 256622fbcb8dSdrh #endif 256722fbcb8dSdrh break; 256822fbcb8dSdrh } 256922fbcb8dSdrh 2570dc2c4915Sdrh /* $db restore ?DATABASE? FILENAME 2571dc2c4915Sdrh ** 2572dc2c4915Sdrh ** Open a database file named FILENAME. Transfer the content 2573dc2c4915Sdrh ** of FILENAME into the local database DATABASE (default: "main"). 2574dc2c4915Sdrh */ 2575dc2c4915Sdrh case DB_RESTORE: { 2576dc2c4915Sdrh const char *zSrcFile; 2577dc2c4915Sdrh const char *zDestDb; 2578dc2c4915Sdrh sqlite3 *pSrc; 2579dc2c4915Sdrh sqlite3_backup *pBackup; 2580dc2c4915Sdrh int nTimeout = 0; 2581dc2c4915Sdrh 2582dc2c4915Sdrh if( objc==3 ){ 2583dc2c4915Sdrh zDestDb = "main"; 2584dc2c4915Sdrh zSrcFile = Tcl_GetString(objv[2]); 2585dc2c4915Sdrh }else if( objc==4 ){ 2586dc2c4915Sdrh zDestDb = Tcl_GetString(objv[2]); 2587dc2c4915Sdrh zSrcFile = Tcl_GetString(objv[3]); 2588dc2c4915Sdrh }else{ 2589dc2c4915Sdrh Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 2590dc2c4915Sdrh return TCL_ERROR; 2591dc2c4915Sdrh } 2592dc2c4915Sdrh rc = sqlite3_open_v2(zSrcFile, &pSrc, SQLITE_OPEN_READONLY, 0); 2593dc2c4915Sdrh if( rc!=SQLITE_OK ){ 2594dc2c4915Sdrh Tcl_AppendResult(interp, "cannot open source database: ", 2595dc2c4915Sdrh sqlite3_errmsg(pSrc), (char*)0); 2596dc2c4915Sdrh sqlite3_close(pSrc); 2597dc2c4915Sdrh return TCL_ERROR; 2598dc2c4915Sdrh } 2599dc2c4915Sdrh pBackup = sqlite3_backup_init(pDb->db, zDestDb, pSrc, "main"); 2600dc2c4915Sdrh if( pBackup==0 ){ 2601dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: ", 2602dc2c4915Sdrh sqlite3_errmsg(pDb->db), (char*)0); 2603dc2c4915Sdrh sqlite3_close(pSrc); 2604dc2c4915Sdrh return TCL_ERROR; 2605dc2c4915Sdrh } 2606dc2c4915Sdrh while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK 2607dc2c4915Sdrh || rc==SQLITE_BUSY ){ 2608dc2c4915Sdrh if( rc==SQLITE_BUSY ){ 2609dc2c4915Sdrh if( nTimeout++ >= 3 ) break; 2610dc2c4915Sdrh sqlite3_sleep(100); 2611dc2c4915Sdrh } 2612dc2c4915Sdrh } 2613dc2c4915Sdrh sqlite3_backup_finish(pBackup); 2614dc2c4915Sdrh if( rc==SQLITE_DONE ){ 2615dc2c4915Sdrh rc = TCL_OK; 2616dc2c4915Sdrh }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 2617dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: source database busy", 2618dc2c4915Sdrh (char*)0); 2619dc2c4915Sdrh rc = TCL_ERROR; 2620dc2c4915Sdrh }else{ 2621dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: ", 2622dc2c4915Sdrh sqlite3_errmsg(pDb->db), (char*)0); 2623dc2c4915Sdrh rc = TCL_ERROR; 2624dc2c4915Sdrh } 2625dc2c4915Sdrh sqlite3_close(pSrc); 2626dc2c4915Sdrh break; 2627dc2c4915Sdrh } 2628dc2c4915Sdrh 262922fbcb8dSdrh /* 26303c379b01Sdrh ** $db status (step|sort|autoindex) 2631d1d38488Sdrh ** 2632d1d38488Sdrh ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or 2633d1d38488Sdrh ** SQLITE_STMTSTATUS_SORT for the most recent eval. 2634d1d38488Sdrh */ 2635d1d38488Sdrh case DB_STATUS: { 2636d1d38488Sdrh int v; 2637d1d38488Sdrh const char *zOp; 2638d1d38488Sdrh if( objc!=3 ){ 26391c320a43Sdrh Tcl_WrongNumArgs(interp, 2, objv, "(step|sort|autoindex)"); 2640d1d38488Sdrh return TCL_ERROR; 2641d1d38488Sdrh } 2642d1d38488Sdrh zOp = Tcl_GetString(objv[2]); 2643d1d38488Sdrh if( strcmp(zOp, "step")==0 ){ 2644d1d38488Sdrh v = pDb->nStep; 2645d1d38488Sdrh }else if( strcmp(zOp, "sort")==0 ){ 2646d1d38488Sdrh v = pDb->nSort; 26473c379b01Sdrh }else if( strcmp(zOp, "autoindex")==0 ){ 26483c379b01Sdrh v = pDb->nIndex; 2649d1d38488Sdrh }else{ 26503c379b01Sdrh Tcl_AppendResult(interp, 26513c379b01Sdrh "bad argument: should be autoindex, step, or sort", 2652d1d38488Sdrh (char*)0); 2653d1d38488Sdrh return TCL_ERROR; 2654d1d38488Sdrh } 2655d1d38488Sdrh Tcl_SetObjResult(interp, Tcl_NewIntObj(v)); 2656d1d38488Sdrh break; 2657d1d38488Sdrh } 2658d1d38488Sdrh 2659d1d38488Sdrh /* 2660bec3f402Sdrh ** $db timeout MILLESECONDS 2661bec3f402Sdrh ** 2662bec3f402Sdrh ** Delay for the number of milliseconds specified when a file is locked. 2663bec3f402Sdrh */ 26646d31316cSdrh case DB_TIMEOUT: { 2665bec3f402Sdrh int ms; 26666d31316cSdrh if( objc!=3 ){ 26676d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); 2668bec3f402Sdrh return TCL_ERROR; 266975897234Sdrh } 26706d31316cSdrh if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; 26716f8a503dSdanielk1977 sqlite3_busy_timeout(pDb->db, ms); 26726d31316cSdrh break; 267375897234Sdrh } 2674b5a20d3cSdrh 26750f14e2ebSdrh /* 26760f14e2ebSdrh ** $db total_changes 26770f14e2ebSdrh ** 26780f14e2ebSdrh ** Return the number of rows that were modified, inserted, or deleted 26790f14e2ebSdrh ** since the database handle was created. 26800f14e2ebSdrh */ 26810f14e2ebSdrh case DB_TOTAL_CHANGES: { 26820f14e2ebSdrh Tcl_Obj *pResult; 26830f14e2ebSdrh if( objc!=2 ){ 26840f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 26850f14e2ebSdrh return TCL_ERROR; 26860f14e2ebSdrh } 26870f14e2ebSdrh pResult = Tcl_GetObjResult(interp); 26880f14e2ebSdrh Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db)); 26890f14e2ebSdrh break; 26900f14e2ebSdrh } 26910f14e2ebSdrh 2692b5a20d3cSdrh /* $db trace ?CALLBACK? 2693b5a20d3cSdrh ** 2694b5a20d3cSdrh ** Make arrangements to invoke the CALLBACK routine for each SQL statement 2695b5a20d3cSdrh ** that is executed. The text of the SQL is appended to CALLBACK before 2696b5a20d3cSdrh ** it is executed. 2697b5a20d3cSdrh */ 2698b5a20d3cSdrh case DB_TRACE: { 2699b5a20d3cSdrh if( objc>3 ){ 2700b5a20d3cSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 2701b97759edSdrh return TCL_ERROR; 2702b5a20d3cSdrh }else if( objc==2 ){ 2703b5a20d3cSdrh if( pDb->zTrace ){ 2704a198f2b5Sdrh Tcl_AppendResult(interp, pDb->zTrace, (char*)0); 2705b5a20d3cSdrh } 2706b5a20d3cSdrh }else{ 2707b5a20d3cSdrh char *zTrace; 2708b5a20d3cSdrh int len; 2709b5a20d3cSdrh if( pDb->zTrace ){ 2710b5a20d3cSdrh Tcl_Free(pDb->zTrace); 2711b5a20d3cSdrh } 2712b5a20d3cSdrh zTrace = Tcl_GetStringFromObj(objv[2], &len); 2713b5a20d3cSdrh if( zTrace && len>0 ){ 2714b5a20d3cSdrh pDb->zTrace = Tcl_Alloc( len + 1 ); 27155bb3eb9bSdrh memcpy(pDb->zTrace, zTrace, len+1); 2716b5a20d3cSdrh }else{ 2717b5a20d3cSdrh pDb->zTrace = 0; 2718b5a20d3cSdrh } 2719bb201344Sshaneh #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) 2720b5a20d3cSdrh if( pDb->zTrace ){ 2721b5a20d3cSdrh pDb->interp = interp; 27226f8a503dSdanielk1977 sqlite3_trace(pDb->db, DbTraceHandler, pDb); 2723b5a20d3cSdrh }else{ 27246f8a503dSdanielk1977 sqlite3_trace(pDb->db, 0, 0); 2725b5a20d3cSdrh } 272619e2d37fSdrh #endif 2727b5a20d3cSdrh } 2728b5a20d3cSdrh break; 2729b5a20d3cSdrh } 2730b5a20d3cSdrh 27313d21423cSdrh /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT 27323d21423cSdrh ** 27333d21423cSdrh ** Start a new transaction (if we are not already in the midst of a 27343d21423cSdrh ** transaction) and execute the TCL script SCRIPT. After SCRIPT 27353d21423cSdrh ** completes, either commit the transaction or roll it back if SCRIPT 27363d21423cSdrh ** throws an exception. Or if no new transation was started, do nothing. 27373d21423cSdrh ** pass the exception on up the stack. 27383d21423cSdrh ** 27393d21423cSdrh ** This command was inspired by Dave Thomas's talk on Ruby at the 27403d21423cSdrh ** 2005 O'Reilly Open Source Convention (OSCON). 27413d21423cSdrh */ 27423d21423cSdrh case DB_TRANSACTION: { 27433d21423cSdrh Tcl_Obj *pScript; 2744cd38d520Sdanielk1977 const char *zBegin = "SAVEPOINT _tcl_transaction"; 27453d21423cSdrh if( objc!=3 && objc!=4 ){ 27463d21423cSdrh Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT"); 27473d21423cSdrh return TCL_ERROR; 27483d21423cSdrh } 2749cd38d520Sdanielk1977 27504a4c11aaSdan if( pDb->nTransaction==0 && objc==4 ){ 27513d21423cSdrh static const char *TTYPE_strs[] = { 2752ce604012Sdrh "deferred", "exclusive", "immediate", 0 27533d21423cSdrh }; 27543d21423cSdrh enum TTYPE_enum { 27553d21423cSdrh TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE 27563d21423cSdrh }; 27573d21423cSdrh int ttype; 2758b5555e7eSdrh if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type", 27593d21423cSdrh 0, &ttype) ){ 27603d21423cSdrh return TCL_ERROR; 27613d21423cSdrh } 27623d21423cSdrh switch( (enum TTYPE_enum)ttype ){ 27633d21423cSdrh case TTYPE_DEFERRED: /* no-op */; break; 27643d21423cSdrh case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break; 27653d21423cSdrh case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break; 27663d21423cSdrh } 27673d21423cSdrh } 2768cd38d520Sdanielk1977 pScript = objv[objc-1]; 2769cd38d520Sdanielk1977 27704a4c11aaSdan /* Run the SQLite BEGIN command to open a transaction or savepoint. */ 27711f1549f8Sdrh pDb->disableAuth++; 2772cd38d520Sdanielk1977 rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0); 27731f1549f8Sdrh pDb->disableAuth--; 2774cd38d520Sdanielk1977 if( rc!=SQLITE_OK ){ 2775a198f2b5Sdrh Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 2776cd38d520Sdanielk1977 return TCL_ERROR; 27773d21423cSdrh } 2778cd38d520Sdanielk1977 pDb->nTransaction++; 2779cd38d520Sdanielk1977 27804a4c11aaSdan /* If using NRE, schedule a callback to invoke the script pScript, then 27814a4c11aaSdan ** a second callback to commit (or rollback) the transaction or savepoint 27824a4c11aaSdan ** opened above. If not using NRE, evaluate the script directly, then 27834a4c11aaSdan ** call function DbTransPostCmd() to commit (or rollback) the transaction 27844a4c11aaSdan ** or savepoint. */ 27854a4c11aaSdan if( DbUseNre() ){ 27864a4c11aaSdan Tcl_NRAddCallback(interp, DbTransPostCmd, cd, 0, 0, 0); 2787a47941feSdrh (void)Tcl_NREvalObj(interp, pScript, 0); 27883d21423cSdrh }else{ 27894a4c11aaSdan rc = DbTransPostCmd(&cd, interp, Tcl_EvalObjEx(interp, pScript, 0)); 27903d21423cSdrh } 27913d21423cSdrh break; 27923d21423cSdrh } 27933d21423cSdrh 279494eb6a14Sdanielk1977 /* 2795404ca075Sdanielk1977 ** $db unlock_notify ?script? 2796404ca075Sdanielk1977 */ 2797404ca075Sdanielk1977 case DB_UNLOCK_NOTIFY: { 2798404ca075Sdanielk1977 #ifndef SQLITE_ENABLE_UNLOCK_NOTIFY 2799a198f2b5Sdrh Tcl_AppendResult(interp, "unlock_notify not available in this build", 2800a198f2b5Sdrh (char*)0); 2801404ca075Sdanielk1977 rc = TCL_ERROR; 2802404ca075Sdanielk1977 #else 2803404ca075Sdanielk1977 if( objc!=2 && objc!=3 ){ 2804404ca075Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 2805404ca075Sdanielk1977 rc = TCL_ERROR; 2806404ca075Sdanielk1977 }else{ 2807404ca075Sdanielk1977 void (*xNotify)(void **, int) = 0; 2808404ca075Sdanielk1977 void *pNotifyArg = 0; 2809404ca075Sdanielk1977 2810404ca075Sdanielk1977 if( pDb->pUnlockNotify ){ 2811404ca075Sdanielk1977 Tcl_DecrRefCount(pDb->pUnlockNotify); 2812404ca075Sdanielk1977 pDb->pUnlockNotify = 0; 2813404ca075Sdanielk1977 } 2814404ca075Sdanielk1977 2815404ca075Sdanielk1977 if( objc==3 ){ 2816404ca075Sdanielk1977 xNotify = DbUnlockNotify; 2817404ca075Sdanielk1977 pNotifyArg = (void *)pDb; 2818404ca075Sdanielk1977 pDb->pUnlockNotify = objv[2]; 2819404ca075Sdanielk1977 Tcl_IncrRefCount(pDb->pUnlockNotify); 2820404ca075Sdanielk1977 } 2821404ca075Sdanielk1977 2822404ca075Sdanielk1977 if( sqlite3_unlock_notify(pDb->db, xNotify, pNotifyArg) ){ 2823a198f2b5Sdrh Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); 2824404ca075Sdanielk1977 rc = TCL_ERROR; 2825404ca075Sdanielk1977 } 2826404ca075Sdanielk1977 } 2827404ca075Sdanielk1977 #endif 2828404ca075Sdanielk1977 break; 2829404ca075Sdanielk1977 } 2830404ca075Sdanielk1977 2831404ca075Sdanielk1977 /* 2832833bf968Sdrh ** $db wal_hook ?script? 283394eb6a14Sdanielk1977 ** $db update_hook ?script? 283471fd80bfSdanielk1977 ** $db rollback_hook ?script? 283594eb6a14Sdanielk1977 */ 2836833bf968Sdrh case DB_WAL_HOOK: 283771fd80bfSdanielk1977 case DB_UPDATE_HOOK: 283871fd80bfSdanielk1977 case DB_ROLLBACK_HOOK: { 283971fd80bfSdanielk1977 284071fd80bfSdanielk1977 /* set ppHook to point at pUpdateHook or pRollbackHook, depending on 284171fd80bfSdanielk1977 ** whether [$db update_hook] or [$db rollback_hook] was invoked. 284271fd80bfSdanielk1977 */ 284371fd80bfSdanielk1977 Tcl_Obj **ppHook; 284471fd80bfSdanielk1977 if( choice==DB_UPDATE_HOOK ){ 284571fd80bfSdanielk1977 ppHook = &pDb->pUpdateHook; 2846833bf968Sdrh }else if( choice==DB_WAL_HOOK ){ 28475def0843Sdrh ppHook = &pDb->pWalHook; 284871fd80bfSdanielk1977 }else{ 284971fd80bfSdanielk1977 ppHook = &pDb->pRollbackHook; 285071fd80bfSdanielk1977 } 285171fd80bfSdanielk1977 285294eb6a14Sdanielk1977 if( objc!=2 && objc!=3 ){ 285394eb6a14Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 285494eb6a14Sdanielk1977 return TCL_ERROR; 285594eb6a14Sdanielk1977 } 285671fd80bfSdanielk1977 if( *ppHook ){ 285771fd80bfSdanielk1977 Tcl_SetObjResult(interp, *ppHook); 285894eb6a14Sdanielk1977 if( objc==3 ){ 285971fd80bfSdanielk1977 Tcl_DecrRefCount(*ppHook); 286071fd80bfSdanielk1977 *ppHook = 0; 286194eb6a14Sdanielk1977 } 286294eb6a14Sdanielk1977 } 286394eb6a14Sdanielk1977 if( objc==3 ){ 286471fd80bfSdanielk1977 assert( !(*ppHook) ); 286594eb6a14Sdanielk1977 if( Tcl_GetCharLength(objv[2])>0 ){ 286671fd80bfSdanielk1977 *ppHook = objv[2]; 286771fd80bfSdanielk1977 Tcl_IncrRefCount(*ppHook); 286894eb6a14Sdanielk1977 } 286994eb6a14Sdanielk1977 } 287071fd80bfSdanielk1977 287171fd80bfSdanielk1977 sqlite3_update_hook(pDb->db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); 287271fd80bfSdanielk1977 sqlite3_rollback_hook(pDb->db,(pDb->pRollbackHook?DbRollbackHandler:0),pDb); 28735def0843Sdrh sqlite3_wal_hook(pDb->db,(pDb->pWalHook?DbWalHandler:0),pDb); 287471fd80bfSdanielk1977 287594eb6a14Sdanielk1977 break; 287694eb6a14Sdanielk1977 } 287794eb6a14Sdanielk1977 28784397de57Sdanielk1977 /* $db version 28794397de57Sdanielk1977 ** 28804397de57Sdanielk1977 ** Return the version string for this database. 28814397de57Sdanielk1977 */ 28824397de57Sdanielk1977 case DB_VERSION: { 28834397de57Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC); 28844397de57Sdanielk1977 break; 28854397de57Sdanielk1977 } 28864397de57Sdanielk1977 28871067fe11Stpoindex 28886d31316cSdrh } /* End of the SWITCH statement */ 288922fbcb8dSdrh return rc; 289075897234Sdrh } 289175897234Sdrh 2892a2c8a95bSdrh #if SQLITE_TCL_NRE 2893a2c8a95bSdrh /* 2894a2c8a95bSdrh ** Adaptor that provides an objCmd interface to the NRE-enabled 2895a2c8a95bSdrh ** interface implementation. 2896a2c8a95bSdrh */ 2897a2c8a95bSdrh static int DbObjCmdAdaptor( 2898a2c8a95bSdrh void *cd, 2899a2c8a95bSdrh Tcl_Interp *interp, 2900a2c8a95bSdrh int objc, 2901a2c8a95bSdrh Tcl_Obj *const*objv 2902a2c8a95bSdrh ){ 2903a2c8a95bSdrh return Tcl_NRCallObjProc(interp, DbObjCmd, cd, objc, objv); 2904a2c8a95bSdrh } 2905a2c8a95bSdrh #endif /* SQLITE_TCL_NRE */ 2906a2c8a95bSdrh 290775897234Sdrh /* 29083570ad93Sdrh ** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN? 29099a6284c1Sdanielk1977 ** ?-create BOOLEAN? ?-nomutex BOOLEAN? 291075897234Sdrh ** 291175897234Sdrh ** This is the main Tcl command. When the "sqlite" Tcl command is 291275897234Sdrh ** invoked, this routine runs to process that command. 291375897234Sdrh ** 291475897234Sdrh ** The first argument, DBNAME, is an arbitrary name for a new 291575897234Sdrh ** database connection. This command creates a new command named 291675897234Sdrh ** DBNAME that is used to control that connection. The database 291775897234Sdrh ** connection is deleted when the DBNAME command is deleted. 291875897234Sdrh ** 29193570ad93Sdrh ** The second argument is the name of the database file. 2920fbc3eab8Sdrh ** 292175897234Sdrh */ 292222fbcb8dSdrh static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ 2923bec3f402Sdrh SqliteDb *p; 292422fbcb8dSdrh const char *zArg; 292575897234Sdrh char *zErrMsg; 29263570ad93Sdrh int i; 292722fbcb8dSdrh const char *zFile; 29283570ad93Sdrh const char *zVfs = 0; 2929d9da78a2Sdrh int flags; 2930882e8e4dSdrh Tcl_DString translatedFilename; 2931b07028f7Sdrh #ifdef SQLITE_HAS_CODEC 2932b07028f7Sdrh void *pKey = 0; 2933b07028f7Sdrh int nKey = 0; 2934b07028f7Sdrh #endif 2935540ebf82Smistachkin int rc; 2936d9da78a2Sdrh 2937d9da78a2Sdrh /* In normal use, each TCL interpreter runs in a single thread. So 2938d9da78a2Sdrh ** by default, we can turn of mutexing on SQLite database connections. 2939d9da78a2Sdrh ** However, for testing purposes it is useful to have mutexes turned 2940d9da78a2Sdrh ** on. So, by default, mutexes default off. But if compiled with 2941d9da78a2Sdrh ** SQLITE_TCL_DEFAULT_FULLMUTEX then mutexes default on. 2942d9da78a2Sdrh */ 2943d9da78a2Sdrh #ifdef SQLITE_TCL_DEFAULT_FULLMUTEX 2944d9da78a2Sdrh flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX; 2945d9da78a2Sdrh #else 2946d9da78a2Sdrh flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX; 2947d9da78a2Sdrh #endif 2948d9da78a2Sdrh 294922fbcb8dSdrh if( objc==2 ){ 295022fbcb8dSdrh zArg = Tcl_GetStringFromObj(objv[1], 0); 295122fbcb8dSdrh if( strcmp(zArg,"-version")==0 ){ 2952a198f2b5Sdrh Tcl_AppendResult(interp,sqlite3_libversion(), (char*)0); 2953647cb0e1Sdrh return TCL_OK; 2954647cb0e1Sdrh } 29559eb9e26bSdrh if( strcmp(zArg,"-has-codec")==0 ){ 29569eb9e26bSdrh #ifdef SQLITE_HAS_CODEC 2957a198f2b5Sdrh Tcl_AppendResult(interp,"1",(char*)0); 295822fbcb8dSdrh #else 2959a198f2b5Sdrh Tcl_AppendResult(interp,"0",(char*)0); 296022fbcb8dSdrh #endif 296122fbcb8dSdrh return TCL_OK; 296222fbcb8dSdrh } 2963fbc3eab8Sdrh } 29643570ad93Sdrh for(i=3; i+1<objc; i+=2){ 29653570ad93Sdrh zArg = Tcl_GetString(objv[i]); 296622fbcb8dSdrh if( strcmp(zArg,"-key")==0 ){ 2967b07028f7Sdrh #ifdef SQLITE_HAS_CODEC 29683570ad93Sdrh pKey = Tcl_GetByteArrayFromObj(objv[i+1], &nKey); 2969b07028f7Sdrh #endif 29703570ad93Sdrh }else if( strcmp(zArg, "-vfs")==0 ){ 29713c3dd7b9Sdan zVfs = Tcl_GetString(objv[i+1]); 29723570ad93Sdrh }else if( strcmp(zArg, "-readonly")==0 ){ 29733570ad93Sdrh int b; 29743570ad93Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 29753570ad93Sdrh if( b ){ 297633f4e02aSdrh flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); 29773570ad93Sdrh flags |= SQLITE_OPEN_READONLY; 29783570ad93Sdrh }else{ 29793570ad93Sdrh flags &= ~SQLITE_OPEN_READONLY; 29803570ad93Sdrh flags |= SQLITE_OPEN_READWRITE; 29813570ad93Sdrh } 29823570ad93Sdrh }else if( strcmp(zArg, "-create")==0 ){ 29833570ad93Sdrh int b; 29843570ad93Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 298533f4e02aSdrh if( b && (flags & SQLITE_OPEN_READONLY)==0 ){ 29863570ad93Sdrh flags |= SQLITE_OPEN_CREATE; 29873570ad93Sdrh }else{ 29883570ad93Sdrh flags &= ~SQLITE_OPEN_CREATE; 29893570ad93Sdrh } 29909a6284c1Sdanielk1977 }else if( strcmp(zArg, "-nomutex")==0 ){ 29919a6284c1Sdanielk1977 int b; 29929a6284c1Sdanielk1977 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 29939a6284c1Sdanielk1977 if( b ){ 29949a6284c1Sdanielk1977 flags |= SQLITE_OPEN_NOMUTEX; 2995039963adSdrh flags &= ~SQLITE_OPEN_FULLMUTEX; 29969a6284c1Sdanielk1977 }else{ 29979a6284c1Sdanielk1977 flags &= ~SQLITE_OPEN_NOMUTEX; 29989a6284c1Sdanielk1977 } 2999039963adSdrh }else if( strcmp(zArg, "-fullmutex")==0 ){ 3000039963adSdrh int b; 3001039963adSdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 3002039963adSdrh if( b ){ 3003039963adSdrh flags |= SQLITE_OPEN_FULLMUTEX; 3004039963adSdrh flags &= ~SQLITE_OPEN_NOMUTEX; 3005039963adSdrh }else{ 3006039963adSdrh flags &= ~SQLITE_OPEN_FULLMUTEX; 3007039963adSdrh } 3008f12b3f60Sdrh }else if( strcmp(zArg, "-uri")==0 ){ 3009f12b3f60Sdrh int b; 3010f12b3f60Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 3011f12b3f60Sdrh if( b ){ 3012f12b3f60Sdrh flags |= SQLITE_OPEN_URI; 3013f12b3f60Sdrh }else{ 3014f12b3f60Sdrh flags &= ~SQLITE_OPEN_URI; 3015f12b3f60Sdrh } 30163570ad93Sdrh }else{ 30173570ad93Sdrh Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0); 30183570ad93Sdrh return TCL_ERROR; 301922fbcb8dSdrh } 302022fbcb8dSdrh } 30213570ad93Sdrh if( objc<3 || (objc&1)!=1 ){ 302222fbcb8dSdrh Tcl_WrongNumArgs(interp, 1, objv, 30233570ad93Sdrh "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" 302468bd4aa2Sdrh " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?" 30259eb9e26bSdrh #ifdef SQLITE_HAS_CODEC 30263570ad93Sdrh " ?-key CODECKEY?" 302722fbcb8dSdrh #endif 302822fbcb8dSdrh ); 302975897234Sdrh return TCL_ERROR; 303075897234Sdrh } 303175897234Sdrh zErrMsg = 0; 30324cdc9e84Sdrh p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); 303375897234Sdrh if( p==0 ){ 30346ef5e12eSmistachkin Tcl_SetResult(interp, (char *)"malloc failed", TCL_STATIC); 3035bec3f402Sdrh return TCL_ERROR; 3036bec3f402Sdrh } 3037bec3f402Sdrh memset(p, 0, sizeof(*p)); 303822fbcb8dSdrh zFile = Tcl_GetStringFromObj(objv[2], 0); 3039882e8e4dSdrh zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); 3040540ebf82Smistachkin rc = sqlite3_open_v2(zFile, &p->db, flags, zVfs); 3041882e8e4dSdrh Tcl_DStringFree(&translatedFilename); 3042540ebf82Smistachkin if( p->db ){ 304380290863Sdanielk1977 if( SQLITE_OK!=sqlite3_errcode(p->db) ){ 30449404d50eSdrh zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); 304580290863Sdanielk1977 sqlite3_close(p->db); 304680290863Sdanielk1977 p->db = 0; 304780290863Sdanielk1977 } 3048540ebf82Smistachkin }else{ 30495dac8432Smistachkin zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc)); 3050540ebf82Smistachkin } 30512011d5f5Sdrh #ifdef SQLITE_HAS_CODEC 3052f3a65f7eSdrh if( p->db ){ 30532011d5f5Sdrh sqlite3_key(p->db, pKey, nKey); 3054f3a65f7eSdrh } 3055eb8ed70dSdrh #endif 3056bec3f402Sdrh if( p->db==0 ){ 305775897234Sdrh Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 3058bec3f402Sdrh Tcl_Free((char*)p); 30599404d50eSdrh sqlite3_free(zErrMsg); 306075897234Sdrh return TCL_ERROR; 306175897234Sdrh } 3062fb7e7651Sdrh p->maxStmt = NUM_PREPARED_STMTS; 30635169bbc6Sdrh p->interp = interp; 306422fbcb8dSdrh zArg = Tcl_GetStringFromObj(objv[1], 0); 30654a4c11aaSdan if( DbUseNre() ){ 3066a2c8a95bSdrh Tcl_NRCreateCommand(interp, zArg, DbObjCmdAdaptor, DbObjCmd, 3067a2c8a95bSdrh (char*)p, DbDeleteCmd); 30684a4c11aaSdan }else{ 306922fbcb8dSdrh Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); 30704a4c11aaSdan } 307175897234Sdrh return TCL_OK; 307275897234Sdrh } 307375897234Sdrh 307475897234Sdrh /* 307590ca9753Sdrh ** Provide a dummy Tcl_InitStubs if we are using this as a static 307690ca9753Sdrh ** library. 307790ca9753Sdrh */ 307890ca9753Sdrh #ifndef USE_TCL_STUBS 307990ca9753Sdrh # undef Tcl_InitStubs 30800e85ccfcSdrh # define Tcl_InitStubs(a,b,c) TCL_VERSION 308190ca9753Sdrh #endif 308290ca9753Sdrh 308390ca9753Sdrh /* 308429bc4615Sdrh ** Make sure we have a PACKAGE_VERSION macro defined. This will be 308529bc4615Sdrh ** defined automatically by the TEA makefile. But other makefiles 308629bc4615Sdrh ** do not define it. 308729bc4615Sdrh */ 308829bc4615Sdrh #ifndef PACKAGE_VERSION 308929bc4615Sdrh # define PACKAGE_VERSION SQLITE_VERSION 309029bc4615Sdrh #endif 309129bc4615Sdrh 309229bc4615Sdrh /* 309375897234Sdrh ** Initialize this module. 309475897234Sdrh ** 309575897234Sdrh ** This Tcl module contains only a single new Tcl command named "sqlite". 309675897234Sdrh ** (Hence there is no namespace. There is no point in using a namespace 309775897234Sdrh ** if the extension only supplies one new name!) The "sqlite" command is 309875897234Sdrh ** used to open a new SQLite database. See the DbMain() routine above 309975897234Sdrh ** for additional information. 3100b652f432Sdrh ** 3101b652f432Sdrh ** The EXTERN macros are required by TCL in order to work on windows. 310275897234Sdrh */ 3103b652f432Sdrh EXTERN int Sqlite3_Init(Tcl_Interp *interp){ 31040e85ccfcSdrh int rc = Tcl_InitStubs(interp, "8.4", 0)==0 ? TCL_ERROR : TCL_OK; 31056dc8cbe0Sdrh if( rc==TCL_OK ){ 3106ef4ac8f9Sdrh Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); 31071cca0d22Sdrh #ifndef SQLITE_3_SUFFIX_ONLY 31081cca0d22Sdrh /* The "sqlite" alias is undocumented. It is here only to support 31091cca0d22Sdrh ** legacy scripts. All new scripts should use only the "sqlite3" 31106dc8cbe0Sdrh ** command. */ 311149766d6cSdrh Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); 31124c0f1649Sdrh #endif 31136dc8cbe0Sdrh rc = Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION); 31146dc8cbe0Sdrh } 31156dc8cbe0Sdrh return rc; 311690ca9753Sdrh } 3117b652f432Sdrh EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3118b652f432Sdrh EXTERN int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3119b652f432Sdrh EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3120e2c3a659Sdrh 3121d878cab5Sdrh /* Because it accesses the file-system and uses persistent state, SQLite 3122d878cab5Sdrh ** is not considered appropriate for safe interpreters. Hence, we deliberately 3123d878cab5Sdrh ** omit the _SafeInit() interfaces. 3124d878cab5Sdrh */ 312549766d6cSdrh 312649766d6cSdrh #ifndef SQLITE_3_SUFFIX_ONLY 3127a3e63c4aSdan int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3128a3e63c4aSdan int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3129a3e63c4aSdan int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3130a3e63c4aSdan int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 313149766d6cSdrh #endif 313275897234Sdrh 31333e27c026Sdrh #ifdef TCLSH 31343e27c026Sdrh /***************************************************************************** 313557a0227fSdrh ** All of the code that follows is used to build standalone TCL interpreters 313657a0227fSdrh ** that are statically linked with SQLite. Enable these by compiling 313757a0227fSdrh ** with -DTCLSH=n where n can be 1 or 2. An n of 1 generates a standard 313857a0227fSdrh ** tclsh but with SQLite built in. An n of 2 generates the SQLite space 313957a0227fSdrh ** analysis program. 314075897234Sdrh */ 3141348784efSdrh 314257a0227fSdrh #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) 314357a0227fSdrh /* 314457a0227fSdrh * This code implements the MD5 message-digest algorithm. 314557a0227fSdrh * The algorithm is due to Ron Rivest. This code was 314657a0227fSdrh * written by Colin Plumb in 1993, no copyright is claimed. 314757a0227fSdrh * This code is in the public domain; do with it what you wish. 314857a0227fSdrh * 314957a0227fSdrh * Equivalent code is available from RSA Data Security, Inc. 315057a0227fSdrh * This code has been tested against that, and is equivalent, 315157a0227fSdrh * except that you don't need to include two pages of legalese 315257a0227fSdrh * with every copy. 315357a0227fSdrh * 315457a0227fSdrh * To compute the message digest of a chunk of bytes, declare an 315557a0227fSdrh * MD5Context structure, pass it to MD5Init, call MD5Update as 315657a0227fSdrh * needed on buffers full of bytes, and then call MD5Final, which 315757a0227fSdrh * will fill a supplied 16-byte array with the digest. 315857a0227fSdrh */ 315957a0227fSdrh 316057a0227fSdrh /* 316157a0227fSdrh * If compiled on a machine that doesn't have a 32-bit integer, 316257a0227fSdrh * you just set "uint32" to the appropriate datatype for an 316357a0227fSdrh * unsigned 32-bit integer. For example: 316457a0227fSdrh * 316557a0227fSdrh * cc -Duint32='unsigned long' md5.c 316657a0227fSdrh * 316757a0227fSdrh */ 316857a0227fSdrh #ifndef uint32 316957a0227fSdrh # define uint32 unsigned int 317057a0227fSdrh #endif 317157a0227fSdrh 317257a0227fSdrh struct MD5Context { 317357a0227fSdrh int isInit; 317457a0227fSdrh uint32 buf[4]; 317557a0227fSdrh uint32 bits[2]; 317657a0227fSdrh unsigned char in[64]; 317757a0227fSdrh }; 317857a0227fSdrh typedef struct MD5Context MD5Context; 317957a0227fSdrh 318057a0227fSdrh /* 318157a0227fSdrh * Note: this code is harmless on little-endian machines. 318257a0227fSdrh */ 318357a0227fSdrh static void byteReverse (unsigned char *buf, unsigned longs){ 318457a0227fSdrh uint32 t; 318557a0227fSdrh do { 318657a0227fSdrh t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 | 318757a0227fSdrh ((unsigned)buf[1]<<8 | buf[0]); 318857a0227fSdrh *(uint32 *)buf = t; 318957a0227fSdrh buf += 4; 319057a0227fSdrh } while (--longs); 319157a0227fSdrh } 319257a0227fSdrh /* The four core functions - F1 is optimized somewhat */ 319357a0227fSdrh 319457a0227fSdrh /* #define F1(x, y, z) (x & y | ~x & z) */ 319557a0227fSdrh #define F1(x, y, z) (z ^ (x & (y ^ z))) 319657a0227fSdrh #define F2(x, y, z) F1(z, x, y) 319757a0227fSdrh #define F3(x, y, z) (x ^ y ^ z) 319857a0227fSdrh #define F4(x, y, z) (y ^ (x | ~z)) 319957a0227fSdrh 320057a0227fSdrh /* This is the central step in the MD5 algorithm. */ 320157a0227fSdrh #define MD5STEP(f, w, x, y, z, data, s) \ 320257a0227fSdrh ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) 320357a0227fSdrh 320457a0227fSdrh /* 320557a0227fSdrh * The core of the MD5 algorithm, this alters an existing MD5 hash to 320657a0227fSdrh * reflect the addition of 16 longwords of new data. MD5Update blocks 320757a0227fSdrh * the data and converts bytes into longwords for this routine. 320857a0227fSdrh */ 320957a0227fSdrh static void MD5Transform(uint32 buf[4], const uint32 in[16]){ 321057a0227fSdrh register uint32 a, b, c, d; 321157a0227fSdrh 321257a0227fSdrh a = buf[0]; 321357a0227fSdrh b = buf[1]; 321457a0227fSdrh c = buf[2]; 321557a0227fSdrh d = buf[3]; 321657a0227fSdrh 321757a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); 321857a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); 321957a0227fSdrh MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); 322057a0227fSdrh MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); 322157a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); 322257a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); 322357a0227fSdrh MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); 322457a0227fSdrh MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); 322557a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); 322657a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); 322757a0227fSdrh MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); 322857a0227fSdrh MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); 322957a0227fSdrh MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); 323057a0227fSdrh MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); 323157a0227fSdrh MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); 323257a0227fSdrh MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); 323357a0227fSdrh 323457a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); 323557a0227fSdrh MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); 323657a0227fSdrh MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); 323757a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); 323857a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); 323957a0227fSdrh MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); 324057a0227fSdrh MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); 324157a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); 324257a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); 324357a0227fSdrh MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); 324457a0227fSdrh MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); 324557a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); 324657a0227fSdrh MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); 324757a0227fSdrh MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); 324857a0227fSdrh MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); 324957a0227fSdrh MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); 325057a0227fSdrh 325157a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); 325257a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); 325357a0227fSdrh MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); 325457a0227fSdrh MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); 325557a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); 325657a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); 325757a0227fSdrh MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); 325857a0227fSdrh MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); 325957a0227fSdrh MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); 326057a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); 326157a0227fSdrh MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); 326257a0227fSdrh MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); 326357a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); 326457a0227fSdrh MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); 326557a0227fSdrh MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); 326657a0227fSdrh MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); 326757a0227fSdrh 326857a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); 326957a0227fSdrh MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); 327057a0227fSdrh MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); 327157a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); 327257a0227fSdrh MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); 327357a0227fSdrh MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); 327457a0227fSdrh MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); 327557a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); 327657a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); 327757a0227fSdrh MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); 327857a0227fSdrh MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); 327957a0227fSdrh MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); 328057a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); 328157a0227fSdrh MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); 328257a0227fSdrh MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); 328357a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); 328457a0227fSdrh 328557a0227fSdrh buf[0] += a; 328657a0227fSdrh buf[1] += b; 328757a0227fSdrh buf[2] += c; 328857a0227fSdrh buf[3] += d; 328957a0227fSdrh } 329057a0227fSdrh 329157a0227fSdrh /* 329257a0227fSdrh * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 329357a0227fSdrh * initialization constants. 329457a0227fSdrh */ 329557a0227fSdrh static void MD5Init(MD5Context *ctx){ 329657a0227fSdrh ctx->isInit = 1; 329757a0227fSdrh ctx->buf[0] = 0x67452301; 329857a0227fSdrh ctx->buf[1] = 0xefcdab89; 329957a0227fSdrh ctx->buf[2] = 0x98badcfe; 330057a0227fSdrh ctx->buf[3] = 0x10325476; 330157a0227fSdrh ctx->bits[0] = 0; 330257a0227fSdrh ctx->bits[1] = 0; 330357a0227fSdrh } 330457a0227fSdrh 330557a0227fSdrh /* 330657a0227fSdrh * Update context to reflect the concatenation of another buffer full 330757a0227fSdrh * of bytes. 330857a0227fSdrh */ 330957a0227fSdrh static 331057a0227fSdrh void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){ 331157a0227fSdrh uint32 t; 331257a0227fSdrh 331357a0227fSdrh /* Update bitcount */ 331457a0227fSdrh 331557a0227fSdrh t = ctx->bits[0]; 331657a0227fSdrh if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) 331757a0227fSdrh ctx->bits[1]++; /* Carry from low to high */ 331857a0227fSdrh ctx->bits[1] += len >> 29; 331957a0227fSdrh 332057a0227fSdrh t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ 332157a0227fSdrh 332257a0227fSdrh /* Handle any leading odd-sized chunks */ 332357a0227fSdrh 332457a0227fSdrh if ( t ) { 332557a0227fSdrh unsigned char *p = (unsigned char *)ctx->in + t; 332657a0227fSdrh 332757a0227fSdrh t = 64-t; 332857a0227fSdrh if (len < t) { 332957a0227fSdrh memcpy(p, buf, len); 333057a0227fSdrh return; 333157a0227fSdrh } 333257a0227fSdrh memcpy(p, buf, t); 333357a0227fSdrh byteReverse(ctx->in, 16); 333457a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 333557a0227fSdrh buf += t; 333657a0227fSdrh len -= t; 333757a0227fSdrh } 333857a0227fSdrh 333957a0227fSdrh /* Process data in 64-byte chunks */ 334057a0227fSdrh 334157a0227fSdrh while (len >= 64) { 334257a0227fSdrh memcpy(ctx->in, buf, 64); 334357a0227fSdrh byteReverse(ctx->in, 16); 334457a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 334557a0227fSdrh buf += 64; 334657a0227fSdrh len -= 64; 334757a0227fSdrh } 334857a0227fSdrh 334957a0227fSdrh /* Handle any remaining bytes of data. */ 335057a0227fSdrh 335157a0227fSdrh memcpy(ctx->in, buf, len); 335257a0227fSdrh } 335357a0227fSdrh 335457a0227fSdrh /* 335557a0227fSdrh * Final wrapup - pad to 64-byte boundary with the bit pattern 335657a0227fSdrh * 1 0* (64-bit count of bits processed, MSB-first) 335757a0227fSdrh */ 335857a0227fSdrh static void MD5Final(unsigned char digest[16], MD5Context *ctx){ 335957a0227fSdrh unsigned count; 336057a0227fSdrh unsigned char *p; 336157a0227fSdrh 336257a0227fSdrh /* Compute number of bytes mod 64 */ 336357a0227fSdrh count = (ctx->bits[0] >> 3) & 0x3F; 336457a0227fSdrh 336557a0227fSdrh /* Set the first char of padding to 0x80. This is safe since there is 336657a0227fSdrh always at least one byte free */ 336757a0227fSdrh p = ctx->in + count; 336857a0227fSdrh *p++ = 0x80; 336957a0227fSdrh 337057a0227fSdrh /* Bytes of padding needed to make 64 bytes */ 337157a0227fSdrh count = 64 - 1 - count; 337257a0227fSdrh 337357a0227fSdrh /* Pad out to 56 mod 64 */ 337457a0227fSdrh if (count < 8) { 337557a0227fSdrh /* Two lots of padding: Pad the first block to 64 bytes */ 337657a0227fSdrh memset(p, 0, count); 337757a0227fSdrh byteReverse(ctx->in, 16); 337857a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 337957a0227fSdrh 338057a0227fSdrh /* Now fill the next block with 56 bytes */ 338157a0227fSdrh memset(ctx->in, 0, 56); 338257a0227fSdrh } else { 338357a0227fSdrh /* Pad block to 56 bytes */ 338457a0227fSdrh memset(p, 0, count-8); 338557a0227fSdrh } 338657a0227fSdrh byteReverse(ctx->in, 14); 338757a0227fSdrh 338857a0227fSdrh /* Append length in bits and transform */ 3389a47941feSdrh memcpy(ctx->in + 14*4, ctx->bits, 8); 339057a0227fSdrh 339157a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 339257a0227fSdrh byteReverse((unsigned char *)ctx->buf, 4); 339357a0227fSdrh memcpy(digest, ctx->buf, 16); 339457a0227fSdrh } 339557a0227fSdrh 339657a0227fSdrh /* 339757a0227fSdrh ** Convert a 128-bit MD5 digest into a 32-digit base-16 number. 339857a0227fSdrh */ 339957a0227fSdrh static void MD5DigestToBase16(unsigned char *digest, char *zBuf){ 340057a0227fSdrh static char const zEncode[] = "0123456789abcdef"; 340157a0227fSdrh int i, j; 340257a0227fSdrh 340357a0227fSdrh for(j=i=0; i<16; i++){ 340457a0227fSdrh int a = digest[i]; 340557a0227fSdrh zBuf[j++] = zEncode[(a>>4)&0xf]; 340657a0227fSdrh zBuf[j++] = zEncode[a & 0xf]; 340757a0227fSdrh } 340857a0227fSdrh zBuf[j] = 0; 340957a0227fSdrh } 341057a0227fSdrh 341157a0227fSdrh 341257a0227fSdrh /* 341357a0227fSdrh ** Convert a 128-bit MD5 digest into sequency of eight 5-digit integers 341457a0227fSdrh ** each representing 16 bits of the digest and separated from each 341557a0227fSdrh ** other by a "-" character. 341657a0227fSdrh */ 341757a0227fSdrh static void MD5DigestToBase10x8(unsigned char digest[16], char zDigest[50]){ 341857a0227fSdrh int i, j; 341957a0227fSdrh unsigned int x; 342057a0227fSdrh for(i=j=0; i<16; i+=2){ 342157a0227fSdrh x = digest[i]*256 + digest[i+1]; 342257a0227fSdrh if( i>0 ) zDigest[j++] = '-'; 342357a0227fSdrh sprintf(&zDigest[j], "%05u", x); 342457a0227fSdrh j += 5; 342557a0227fSdrh } 342657a0227fSdrh zDigest[j] = 0; 342757a0227fSdrh } 342857a0227fSdrh 342957a0227fSdrh /* 343057a0227fSdrh ** A TCL command for md5. The argument is the text to be hashed. The 343157a0227fSdrh ** Result is the hash in base64. 343257a0227fSdrh */ 343357a0227fSdrh static int md5_cmd(void*cd, Tcl_Interp *interp, int argc, const char **argv){ 343457a0227fSdrh MD5Context ctx; 343557a0227fSdrh unsigned char digest[16]; 343657a0227fSdrh char zBuf[50]; 343757a0227fSdrh void (*converter)(unsigned char*, char*); 343857a0227fSdrh 343957a0227fSdrh if( argc!=2 ){ 344057a0227fSdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 3441a198f2b5Sdrh " TEXT\"", (char*)0); 344257a0227fSdrh return TCL_ERROR; 344357a0227fSdrh } 344457a0227fSdrh MD5Init(&ctx); 344557a0227fSdrh MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1])); 344657a0227fSdrh MD5Final(digest, &ctx); 344757a0227fSdrh converter = (void(*)(unsigned char*,char*))cd; 344857a0227fSdrh converter(digest, zBuf); 344957a0227fSdrh Tcl_AppendResult(interp, zBuf, (char*)0); 345057a0227fSdrh return TCL_OK; 345157a0227fSdrh } 345257a0227fSdrh 345357a0227fSdrh /* 345457a0227fSdrh ** A TCL command to take the md5 hash of a file. The argument is the 345557a0227fSdrh ** name of the file. 345657a0227fSdrh */ 345757a0227fSdrh static int md5file_cmd(void*cd, Tcl_Interp*interp, int argc, const char **argv){ 345857a0227fSdrh FILE *in; 345957a0227fSdrh MD5Context ctx; 346057a0227fSdrh void (*converter)(unsigned char*, char*); 346157a0227fSdrh unsigned char digest[16]; 346257a0227fSdrh char zBuf[10240]; 346357a0227fSdrh 346457a0227fSdrh if( argc!=2 ){ 346557a0227fSdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 3466a198f2b5Sdrh " FILENAME\"", (char*)0); 346757a0227fSdrh return TCL_ERROR; 346857a0227fSdrh } 346957a0227fSdrh in = fopen(argv[1],"rb"); 347057a0227fSdrh if( in==0 ){ 347157a0227fSdrh Tcl_AppendResult(interp,"unable to open file \"", argv[1], 3472a198f2b5Sdrh "\" for reading", (char*)0); 347357a0227fSdrh return TCL_ERROR; 347457a0227fSdrh } 347557a0227fSdrh MD5Init(&ctx); 347657a0227fSdrh for(;;){ 347757a0227fSdrh int n; 347883cc1392Sdrh n = (int)fread(zBuf, 1, sizeof(zBuf), in); 347957a0227fSdrh if( n<=0 ) break; 348057a0227fSdrh MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n); 348157a0227fSdrh } 348257a0227fSdrh fclose(in); 348357a0227fSdrh MD5Final(digest, &ctx); 348457a0227fSdrh converter = (void(*)(unsigned char*,char*))cd; 348557a0227fSdrh converter(digest, zBuf); 348657a0227fSdrh Tcl_AppendResult(interp, zBuf, (char*)0); 348757a0227fSdrh return TCL_OK; 348857a0227fSdrh } 348957a0227fSdrh 349057a0227fSdrh /* 349157a0227fSdrh ** Register the four new TCL commands for generating MD5 checksums 349257a0227fSdrh ** with the TCL interpreter. 349357a0227fSdrh */ 349457a0227fSdrh int Md5_Init(Tcl_Interp *interp){ 349557a0227fSdrh Tcl_CreateCommand(interp, "md5", (Tcl_CmdProc*)md5_cmd, 349657a0227fSdrh MD5DigestToBase16, 0); 349757a0227fSdrh Tcl_CreateCommand(interp, "md5-10x8", (Tcl_CmdProc*)md5_cmd, 349857a0227fSdrh MD5DigestToBase10x8, 0); 349957a0227fSdrh Tcl_CreateCommand(interp, "md5file", (Tcl_CmdProc*)md5file_cmd, 350057a0227fSdrh MD5DigestToBase16, 0); 350157a0227fSdrh Tcl_CreateCommand(interp, "md5file-10x8", (Tcl_CmdProc*)md5file_cmd, 350257a0227fSdrh MD5DigestToBase10x8, 0); 350357a0227fSdrh return TCL_OK; 350457a0227fSdrh } 350557a0227fSdrh #endif /* defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) */ 350657a0227fSdrh 350757a0227fSdrh #if defined(SQLITE_TEST) 350857a0227fSdrh /* 350957a0227fSdrh ** During testing, the special md5sum() aggregate function is available. 351057a0227fSdrh ** inside SQLite. The following routines implement that function. 351157a0227fSdrh */ 351257a0227fSdrh static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){ 351357a0227fSdrh MD5Context *p; 351457a0227fSdrh int i; 351557a0227fSdrh if( argc<1 ) return; 351657a0227fSdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 351757a0227fSdrh if( p==0 ) return; 351857a0227fSdrh if( !p->isInit ){ 351957a0227fSdrh MD5Init(p); 352057a0227fSdrh } 352157a0227fSdrh for(i=0; i<argc; i++){ 352257a0227fSdrh const char *zData = (char*)sqlite3_value_text(argv[i]); 352357a0227fSdrh if( zData ){ 352483cc1392Sdrh MD5Update(p, (unsigned char*)zData, (int)strlen(zData)); 352557a0227fSdrh } 352657a0227fSdrh } 352757a0227fSdrh } 352857a0227fSdrh static void md5finalize(sqlite3_context *context){ 352957a0227fSdrh MD5Context *p; 353057a0227fSdrh unsigned char digest[16]; 353157a0227fSdrh char zBuf[33]; 353257a0227fSdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 353357a0227fSdrh MD5Final(digest,p); 353457a0227fSdrh MD5DigestToBase16(digest, zBuf); 353557a0227fSdrh sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 353657a0227fSdrh } 353757a0227fSdrh int Md5_Register(sqlite3 *db){ 353857a0227fSdrh int rc = sqlite3_create_function(db, "md5sum", -1, SQLITE_UTF8, 0, 0, 353957a0227fSdrh md5step, md5finalize); 354057a0227fSdrh sqlite3_overload_function(db, "md5sum", -1); /* To exercise this API */ 354157a0227fSdrh return rc; 354257a0227fSdrh } 354357a0227fSdrh #endif /* defined(SQLITE_TEST) */ 354457a0227fSdrh 354557a0227fSdrh 3546348784efSdrh /* 35473e27c026Sdrh ** If the macro TCLSH is one, then put in code this for the 35483e27c026Sdrh ** "main" routine that will initialize Tcl and take input from 35493570ad93Sdrh ** standard input, or if a file is named on the command line 35503570ad93Sdrh ** the TCL interpreter reads and evaluates that file. 3551348784efSdrh */ 35523e27c026Sdrh #if TCLSH==1 35530ae479dfSdan static const char *tclsh_main_loop(void){ 35540ae479dfSdan static const char zMainloop[] = 3555348784efSdrh "set line {}\n" 3556348784efSdrh "while {![eof stdin]} {\n" 3557348784efSdrh "if {$line!=\"\"} {\n" 3558348784efSdrh "puts -nonewline \"> \"\n" 3559348784efSdrh "} else {\n" 3560348784efSdrh "puts -nonewline \"% \"\n" 3561348784efSdrh "}\n" 3562348784efSdrh "flush stdout\n" 3563348784efSdrh "append line [gets stdin]\n" 3564348784efSdrh "if {[info complete $line]} {\n" 3565348784efSdrh "if {[catch {uplevel #0 $line} result]} {\n" 3566348784efSdrh "puts stderr \"Error: $result\"\n" 3567348784efSdrh "} elseif {$result!=\"\"} {\n" 3568348784efSdrh "puts $result\n" 3569348784efSdrh "}\n" 3570348784efSdrh "set line {}\n" 3571348784efSdrh "} else {\n" 3572348784efSdrh "append line \\n\n" 3573348784efSdrh "}\n" 3574348784efSdrh "}\n" 3575348784efSdrh ; 35760ae479dfSdan return zMainloop; 35770ae479dfSdan } 35783e27c026Sdrh #endif 35793a0f13ffSdrh #if TCLSH==2 35800ae479dfSdan static const char *tclsh_main_loop(void); 35813a0f13ffSdrh #endif 35823e27c026Sdrh 3583c1a60c51Sdan #ifdef SQLITE_TEST 3584c1a60c51Sdan static void init_all(Tcl_Interp *); 3585c1a60c51Sdan static int init_all_cmd( 3586c1a60c51Sdan ClientData cd, 3587c1a60c51Sdan Tcl_Interp *interp, 3588c1a60c51Sdan int objc, 3589c1a60c51Sdan Tcl_Obj *CONST objv[] 3590c1a60c51Sdan ){ 35910a549071Sdanielk1977 3592c1a60c51Sdan Tcl_Interp *slave; 3593c1a60c51Sdan if( objc!=2 ){ 3594c1a60c51Sdan Tcl_WrongNumArgs(interp, 1, objv, "SLAVE"); 3595c1a60c51Sdan return TCL_ERROR; 3596c1a60c51Sdan } 35970a549071Sdanielk1977 3598c1a60c51Sdan slave = Tcl_GetSlave(interp, Tcl_GetString(objv[1])); 3599c1a60c51Sdan if( !slave ){ 3600c1a60c51Sdan return TCL_ERROR; 3601c1a60c51Sdan } 3602c1a60c51Sdan 3603c1a60c51Sdan init_all(slave); 3604c1a60c51Sdan return TCL_OK; 3605c1a60c51Sdan } 3606c431fd55Sdan 3607c431fd55Sdan /* 3608c431fd55Sdan ** Tclcmd: db_use_legacy_prepare DB BOOLEAN 3609c431fd55Sdan ** 3610c431fd55Sdan ** The first argument to this command must be a database command created by 3611c431fd55Sdan ** [sqlite3]. If the second argument is true, then the handle is configured 3612c431fd55Sdan ** to use the sqlite3_prepare_v2() function to prepare statements. If it 3613c431fd55Sdan ** is false, sqlite3_prepare(). 3614c431fd55Sdan */ 3615c431fd55Sdan static int db_use_legacy_prepare_cmd( 3616c431fd55Sdan ClientData cd, 3617c431fd55Sdan Tcl_Interp *interp, 3618c431fd55Sdan int objc, 3619c431fd55Sdan Tcl_Obj *CONST objv[] 3620c431fd55Sdan ){ 3621c431fd55Sdan Tcl_CmdInfo cmdInfo; 3622c431fd55Sdan SqliteDb *pDb; 3623c431fd55Sdan int bPrepare; 3624c431fd55Sdan 3625c431fd55Sdan if( objc!=3 ){ 3626c431fd55Sdan Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN"); 3627c431fd55Sdan return TCL_ERROR; 3628c431fd55Sdan } 3629c431fd55Sdan 3630c431fd55Sdan if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ 3631c431fd55Sdan Tcl_AppendResult(interp, "no such db: ", Tcl_GetString(objv[1]), (char*)0); 3632c431fd55Sdan return TCL_ERROR; 3633c431fd55Sdan } 3634c431fd55Sdan pDb = (SqliteDb*)cmdInfo.objClientData; 3635c431fd55Sdan if( Tcl_GetBooleanFromObj(interp, objv[2], &bPrepare) ){ 3636c431fd55Sdan return TCL_ERROR; 3637c431fd55Sdan } 3638c431fd55Sdan 3639c431fd55Sdan pDb->bLegacyPrepare = bPrepare; 3640c431fd55Sdan 3641c431fd55Sdan Tcl_ResetResult(interp); 3642c431fd55Sdan return TCL_OK; 3643c431fd55Sdan } 3644*04489b6dSdan 3645*04489b6dSdan /* 3646*04489b6dSdan ** Tclcmd: db_last_stmt_ptr DB 3647*04489b6dSdan ** 3648*04489b6dSdan ** If the statement cache associated with database DB is not empty, 3649*04489b6dSdan ** return the text representation of the most recently used statement 3650*04489b6dSdan ** handle. 3651*04489b6dSdan */ 3652*04489b6dSdan static int db_last_stmt_ptr( 3653*04489b6dSdan ClientData cd, 3654*04489b6dSdan Tcl_Interp *interp, 3655*04489b6dSdan int objc, 3656*04489b6dSdan Tcl_Obj *CONST objv[] 3657*04489b6dSdan ){ 3658*04489b6dSdan extern int sqlite3TestMakePointerStr(Tcl_Interp*, char*, void*); 3659*04489b6dSdan Tcl_CmdInfo cmdInfo; 3660*04489b6dSdan SqliteDb *pDb; 3661*04489b6dSdan sqlite3_stmt *pStmt = 0; 3662*04489b6dSdan char zBuf[100]; 3663*04489b6dSdan 3664*04489b6dSdan if( objc!=2 ){ 3665*04489b6dSdan Tcl_WrongNumArgs(interp, 1, objv, "DB"); 3666*04489b6dSdan return TCL_ERROR; 3667*04489b6dSdan } 3668*04489b6dSdan 3669*04489b6dSdan if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ 3670*04489b6dSdan Tcl_AppendResult(interp, "no such db: ", Tcl_GetString(objv[1]), (char*)0); 3671*04489b6dSdan return TCL_ERROR; 3672*04489b6dSdan } 3673*04489b6dSdan pDb = (SqliteDb*)cmdInfo.objClientData; 3674*04489b6dSdan 3675*04489b6dSdan if( pDb->stmtList ) pStmt = pDb->stmtList->pStmt; 3676*04489b6dSdan if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ){ 3677*04489b6dSdan return TCL_ERROR; 3678*04489b6dSdan } 3679*04489b6dSdan Tcl_SetResult(interp, zBuf, TCL_VOLATILE); 3680*04489b6dSdan 3681*04489b6dSdan return TCL_OK; 3682*04489b6dSdan } 3683c1a60c51Sdan #endif 3684c1a60c51Sdan 3685c1a60c51Sdan /* 3686c1a60c51Sdan ** Configure the interpreter passed as the first argument to have access 3687c1a60c51Sdan ** to the commands and linked variables that make up: 3688c1a60c51Sdan ** 3689c1a60c51Sdan ** * the [sqlite3] extension itself, 3690c1a60c51Sdan ** 3691c1a60c51Sdan ** * If SQLITE_TCLMD5 or SQLITE_TEST is defined, the Md5 commands, and 3692c1a60c51Sdan ** 3693c1a60c51Sdan ** * If SQLITE_TEST is set, the various test interfaces used by the Tcl 3694c1a60c51Sdan ** test suite. 3695c1a60c51Sdan */ 3696c1a60c51Sdan static void init_all(Tcl_Interp *interp){ 369738f8271fSdrh Sqlite3_Init(interp); 3698c1a60c51Sdan 369957a0227fSdrh #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) 370057a0227fSdrh Md5_Init(interp); 370157a0227fSdrh #endif 3702c1a60c51Sdan 37030ae479dfSdan /* Install the [register_dbstat_vtab] command to access the implementation 37040ae479dfSdan ** of virtual table dbstat (source file test_stat.c). This command is 37050ae479dfSdan ** required for testfixture and sqlite3_analyzer, but not by the production 37060ae479dfSdan ** Tcl extension. */ 37070ae479dfSdan #if defined(SQLITE_TEST) || TCLSH==2 37080ae479dfSdan { 37090ae479dfSdan extern int SqlitetestStat_Init(Tcl_Interp*); 37100ae479dfSdan SqlitetestStat_Init(interp); 37110ae479dfSdan } 37120ae479dfSdan #endif 37130ae479dfSdan 3714d9b0257aSdrh #ifdef SQLITE_TEST 3715d1bf3512Sdrh { 37162f999a67Sdrh extern int Sqliteconfig_Init(Tcl_Interp*); 3717d1bf3512Sdrh extern int Sqlitetest1_Init(Tcl_Interp*); 37185c4d9703Sdrh extern int Sqlitetest2_Init(Tcl_Interp*); 37195c4d9703Sdrh extern int Sqlitetest3_Init(Tcl_Interp*); 3720a6064dcfSdrh extern int Sqlitetest4_Init(Tcl_Interp*); 3721998b56c3Sdanielk1977 extern int Sqlitetest5_Init(Tcl_Interp*); 37229c06c953Sdrh extern int Sqlitetest6_Init(Tcl_Interp*); 372329c636bcSdrh extern int Sqlitetest7_Init(Tcl_Interp*); 3724b9bb7c18Sdrh extern int Sqlitetest8_Init(Tcl_Interp*); 3725a713f2c3Sdanielk1977 extern int Sqlitetest9_Init(Tcl_Interp*); 37262366940dSdrh extern int Sqlitetestasync_Init(Tcl_Interp*); 37271409be69Sdrh extern int Sqlitetest_autoext_Init(Tcl_Interp*); 37280a7a9155Sdan extern int Sqlitetest_demovfs_Init(Tcl_Interp *); 3729984bfaa4Sdrh extern int Sqlitetest_func_Init(Tcl_Interp*); 373015926590Sdrh extern int Sqlitetest_hexio_Init(Tcl_Interp*); 3731e1ab2193Sdan extern int Sqlitetest_init_Init(Tcl_Interp*); 37322f999a67Sdrh extern int Sqlitetest_malloc_Init(Tcl_Interp*); 37331a9ed0b2Sdanielk1977 extern int Sqlitetest_mutex_Init(Tcl_Interp*); 37342f999a67Sdrh extern int Sqlitetestschema_Init(Tcl_Interp*); 37352f999a67Sdrh extern int Sqlitetestsse_Init(Tcl_Interp*); 37362f999a67Sdrh extern int Sqlitetesttclvar_Init(Tcl_Interp*); 37379f5ff371Sdan extern int Sqlitetestfs_Init(Tcl_Interp*); 373844918fa0Sdanielk1977 extern int SqlitetestThread_Init(Tcl_Interp*); 3739a15db353Sdanielk1977 extern int SqlitetestOnefile_Init(); 37405d1f5aa6Sdanielk1977 extern int SqlitetestOsinst_Init(Tcl_Interp*); 37410410302eSdanielk1977 extern int Sqlitetestbackup_Init(Tcl_Interp*); 3742522efc62Sdrh extern int Sqlitetestintarray_Init(Tcl_Interp*); 3743c7991bdfSdan extern int Sqlitetestvfs_Init(Tcl_Interp *); 37449508daa9Sdan extern int Sqlitetestrtree_Init(Tcl_Interp*); 37458cf35eb4Sdan extern int Sqlitequota_Init(Tcl_Interp*); 37468a922f75Sshaneh extern int Sqlitemultiplex_Init(Tcl_Interp*); 3747e336b001Sdan extern int SqliteSuperlock_Init(Tcl_Interp*); 3748213ca0a8Sdan extern int SqlitetestSyscall_Init(Tcl_Interp*); 37492e66f0b9Sdrh 37506764a700Sdan #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) 375199ebad90Sdan extern int Sqlitetestfts3_Init(Tcl_Interp *interp); 375299ebad90Sdan #endif 375399ebad90Sdan 3754b29010cdSdan #ifdef SQLITE_ENABLE_ZIPVFS 3755b29010cdSdan extern int Zipvfs_Init(Tcl_Interp*); 3756b29010cdSdan Zipvfs_Init(interp); 3757b29010cdSdan #endif 3758b29010cdSdan 37592f999a67Sdrh Sqliteconfig_Init(interp); 37606490bebdSdanielk1977 Sqlitetest1_Init(interp); 37615c4d9703Sdrh Sqlitetest2_Init(interp); 3762de647130Sdrh Sqlitetest3_Init(interp); 3763fc57d7bfSdanielk1977 Sqlitetest4_Init(interp); 3764998b56c3Sdanielk1977 Sqlitetest5_Init(interp); 37659c06c953Sdrh Sqlitetest6_Init(interp); 376629c636bcSdrh Sqlitetest7_Init(interp); 3767b9bb7c18Sdrh Sqlitetest8_Init(interp); 3768a713f2c3Sdanielk1977 Sqlitetest9_Init(interp); 37692366940dSdrh Sqlitetestasync_Init(interp); 37701409be69Sdrh Sqlitetest_autoext_Init(interp); 37710a7a9155Sdan Sqlitetest_demovfs_Init(interp); 3772984bfaa4Sdrh Sqlitetest_func_Init(interp); 377315926590Sdrh Sqlitetest_hexio_Init(interp); 3774e1ab2193Sdan Sqlitetest_init_Init(interp); 37752f999a67Sdrh Sqlitetest_malloc_Init(interp); 37761a9ed0b2Sdanielk1977 Sqlitetest_mutex_Init(interp); 37772f999a67Sdrh Sqlitetestschema_Init(interp); 37782f999a67Sdrh Sqlitetesttclvar_Init(interp); 37799f5ff371Sdan Sqlitetestfs_Init(interp); 378044918fa0Sdanielk1977 SqlitetestThread_Init(interp); 3781a15db353Sdanielk1977 SqlitetestOnefile_Init(interp); 37825d1f5aa6Sdanielk1977 SqlitetestOsinst_Init(interp); 37830410302eSdanielk1977 Sqlitetestbackup_Init(interp); 3784522efc62Sdrh Sqlitetestintarray_Init(interp); 3785c7991bdfSdan Sqlitetestvfs_Init(interp); 37869508daa9Sdan Sqlitetestrtree_Init(interp); 37878cf35eb4Sdan Sqlitequota_Init(interp); 37888a922f75Sshaneh Sqlitemultiplex_Init(interp); 3789e336b001Sdan SqliteSuperlock_Init(interp); 3790213ca0a8Sdan SqlitetestSyscall_Init(interp); 3791a15db353Sdanielk1977 37926764a700Sdan #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) 379399ebad90Sdan Sqlitetestfts3_Init(interp); 379499ebad90Sdan #endif 379599ebad90Sdan 3796c431fd55Sdan Tcl_CreateObjCommand( 3797c431fd55Sdan interp, "load_testfixture_extensions", init_all_cmd, 0, 0 3798c431fd55Sdan ); 3799c431fd55Sdan Tcl_CreateObjCommand( 3800c431fd55Sdan interp, "db_use_legacy_prepare", db_use_legacy_prepare_cmd, 0, 0 3801c431fd55Sdan ); 3802*04489b6dSdan Tcl_CreateObjCommand( 3803*04489b6dSdan interp, "db_last_stmt_ptr", db_last_stmt_ptr, 0, 0 3804*04489b6dSdan ); 3805c1a60c51Sdan 380689dec819Sdrh #ifdef SQLITE_SSE 38072e66f0b9Sdrh Sqlitetestsse_Init(interp); 38082e66f0b9Sdrh #endif 3809d1bf3512Sdrh } 3810d1bf3512Sdrh #endif 3811c1a60c51Sdan } 3812c1a60c51Sdan 3813c1a60c51Sdan #define TCLSH_MAIN main /* Needed to fake out mktclapp */ 3814c1a60c51Sdan int TCLSH_MAIN(int argc, char **argv){ 3815c1a60c51Sdan Tcl_Interp *interp; 3816c1a60c51Sdan 38171f28e070Smistachkin #if !defined(_WIN32_WCE) 38181f28e070Smistachkin if( getenv("BREAK") ){ 38191f28e070Smistachkin fprintf(stderr, 38201f28e070Smistachkin "attach debugger to process %d and press any key to continue.\n", 38211f28e070Smistachkin GETPID()); 38221f28e070Smistachkin fgetc(stdin); 38231f28e070Smistachkin } 38241f28e070Smistachkin #endif 38251f28e070Smistachkin 3826c1a60c51Sdan /* Call sqlite3_shutdown() once before doing anything else. This is to 3827c1a60c51Sdan ** test that sqlite3_shutdown() can be safely called by a process before 3828c1a60c51Sdan ** sqlite3_initialize() is. */ 3829c1a60c51Sdan sqlite3_shutdown(); 3830c1a60c51Sdan 38310ae479dfSdan Tcl_FindExecutable(argv[0]); 38322953ba9eSmistachkin Tcl_SetSystemEncoding(NULL, "utf-8"); 38330ae479dfSdan interp = Tcl_CreateInterp(); 38340ae479dfSdan 38353a0f13ffSdrh #if TCLSH==2 38363a0f13ffSdrh sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); 38373a0f13ffSdrh #endif 3838c1a60c51Sdan 3839c1a60c51Sdan init_all(interp); 3840c7285978Sdrh if( argc>=2 ){ 3841348784efSdrh int i; 3842ad42c3a3Sshess char zArgc[32]; 3843ad42c3a3Sshess sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH)); 3844ad42c3a3Sshess Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY); 3845348784efSdrh Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); 3846348784efSdrh Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); 384761212b69Sdrh for(i=3-TCLSH; i<argc; i++){ 3848348784efSdrh Tcl_SetVar(interp, "argv", argv[i], 3849348784efSdrh TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); 3850348784efSdrh } 38513a0f13ffSdrh if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ 38520de8c112Sdrh const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); 3853a81c64a2Sdrh if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp); 3854c61053b7Sdrh fprintf(stderr,"%s: %s\n", *argv, zInfo); 3855348784efSdrh return 1; 3856348784efSdrh } 38573e27c026Sdrh } 38583a0f13ffSdrh if( TCLSH==2 || argc<=1 ){ 38590ae479dfSdan Tcl_GlobalEval(interp, tclsh_main_loop()); 3860348784efSdrh } 3861348784efSdrh return 0; 3862348784efSdrh } 3863348784efSdrh #endif /* TCLSH */ 3864