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 44ad6e1370Sdrh /* 45ad6e1370Sdrh * Windows needs to know which symbols to export. Unix does not. 46ad6e1370Sdrh * BUILD_sqlite should be undefined for Unix. 47ad6e1370Sdrh */ 48ad6e1370Sdrh #ifdef BUILD_sqlite 49ad6e1370Sdrh #undef TCL_STORAGE_CLASS 50ad6e1370Sdrh #define TCL_STORAGE_CLASS DLLEXPORT 51ad6e1370Sdrh #endif /* BUILD_sqlite */ 5229bc4615Sdrh 53a21c6b6fSdanielk1977 #define NUM_PREPARED_STMTS 10 54fb7e7651Sdrh #define MAX_PREPARED_STMTS 100 55fb7e7651Sdrh 5675897234Sdrh /* 5798808babSdrh ** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we 5898808babSdrh ** have to do a translation when going between the two. Set the 5998808babSdrh ** UTF_TRANSLATION_NEEDED macro to indicate that we need to do 6098808babSdrh ** this translation. 6198808babSdrh */ 6298808babSdrh #if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8) 6398808babSdrh # define UTF_TRANSLATION_NEEDED 1 6498808babSdrh #endif 6598808babSdrh 6698808babSdrh /* 67cabb0819Sdrh ** New SQL functions can be created as TCL scripts. Each such function 68cabb0819Sdrh ** is described by an instance of the following structure. 69cabb0819Sdrh */ 70cabb0819Sdrh typedef struct SqlFunc SqlFunc; 71cabb0819Sdrh struct SqlFunc { 72cabb0819Sdrh Tcl_Interp *interp; /* The TCL interpret to execute the function */ 73d1e4733dSdrh Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */ 74d1e4733dSdrh int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */ 75d1e4733dSdrh char *zName; /* Name of this function */ 76cabb0819Sdrh SqlFunc *pNext; /* Next function on the list of them all */ 77cabb0819Sdrh }; 78cabb0819Sdrh 79cabb0819Sdrh /* 800202b29eSdanielk1977 ** New collation sequences function can be created as TCL scripts. Each such 810202b29eSdanielk1977 ** function is described by an instance of the following structure. 820202b29eSdanielk1977 */ 830202b29eSdanielk1977 typedef struct SqlCollate SqlCollate; 840202b29eSdanielk1977 struct SqlCollate { 850202b29eSdanielk1977 Tcl_Interp *interp; /* The TCL interpret to execute the function */ 860202b29eSdanielk1977 char *zScript; /* The script to be run */ 870202b29eSdanielk1977 SqlCollate *pNext; /* Next function on the list of them all */ 880202b29eSdanielk1977 }; 890202b29eSdanielk1977 900202b29eSdanielk1977 /* 91fb7e7651Sdrh ** Prepared statements are cached for faster execution. Each prepared 92fb7e7651Sdrh ** statement is described by an instance of the following structure. 93fb7e7651Sdrh */ 94fb7e7651Sdrh typedef struct SqlPreparedStmt SqlPreparedStmt; 95fb7e7651Sdrh struct SqlPreparedStmt { 96fb7e7651Sdrh SqlPreparedStmt *pNext; /* Next in linked list */ 97fb7e7651Sdrh SqlPreparedStmt *pPrev; /* Previous on the list */ 98fb7e7651Sdrh sqlite3_stmt *pStmt; /* The prepared statement */ 99fb7e7651Sdrh int nSql; /* chars in zSql[] */ 100d0e2a854Sdanielk1977 const char *zSql; /* Text of the SQL statement */ 1014a4c11aaSdan int nParm; /* Size of apParm array */ 1024a4c11aaSdan Tcl_Obj **apParm; /* Array of referenced object pointers */ 103fb7e7651Sdrh }; 104fb7e7651Sdrh 105d0441796Sdanielk1977 typedef struct IncrblobChannel IncrblobChannel; 106d0441796Sdanielk1977 107fb7e7651Sdrh /* 108bec3f402Sdrh ** There is one instance of this structure for each SQLite database 109bec3f402Sdrh ** that has been opened by the SQLite TCL interface. 110bec3f402Sdrh */ 111bec3f402Sdrh typedef struct SqliteDb SqliteDb; 112bec3f402Sdrh struct SqliteDb { 113dddca286Sdrh sqlite3 *db; /* The "real" database structure. MUST BE FIRST */ 114bec3f402Sdrh Tcl_Interp *interp; /* The interpreter used for this database */ 1156d31316cSdrh char *zBusy; /* The busy callback routine */ 116aa940eacSdrh char *zCommit; /* The commit hook callback routine */ 117b5a20d3cSdrh char *zTrace; /* The trace callback routine */ 11819e2d37fSdrh char *zProfile; /* The profile callback routine */ 119348bb5d6Sdanielk1977 char *zProgress; /* The progress callback routine */ 120e22a334bSdrh char *zAuth; /* The authorization callback routine */ 1211f1549f8Sdrh int disableAuth; /* Disable the authorizer if it exists */ 12255c45f2eSdanielk1977 char *zNull; /* Text to substitute for an SQL NULL value */ 123cabb0819Sdrh SqlFunc *pFunc; /* List of SQL functions */ 12494eb6a14Sdanielk1977 Tcl_Obj *pUpdateHook; /* Update hook script (if any) */ 12571fd80bfSdanielk1977 Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */ 1265def0843Sdrh Tcl_Obj *pWalHook; /* WAL hook script (if any) */ 127404ca075Sdanielk1977 Tcl_Obj *pUnlockNotify; /* Unlock notify script (if any) */ 1280202b29eSdanielk1977 SqlCollate *pCollate; /* List of SQL collation functions */ 1296f8a503dSdanielk1977 int rc; /* Return code of most recent sqlite3_exec() */ 1307cedc8d4Sdanielk1977 Tcl_Obj *pCollateNeeded; /* Collation needed script */ 131fb7e7651Sdrh SqlPreparedStmt *stmtList; /* List of prepared statements*/ 132fb7e7651Sdrh SqlPreparedStmt *stmtLast; /* Last statement in the list */ 133fb7e7651Sdrh int maxStmt; /* The next maximum number of stmtList */ 134fb7e7651Sdrh int nStmt; /* Number of statements in stmtList */ 135d0441796Sdanielk1977 IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */ 1363c379b01Sdrh int nStep, nSort, nIndex; /* Statistics for most recent operation */ 137cd38d520Sdanielk1977 int nTransaction; /* Number of nested [transaction] methods */ 13898808babSdrh }; 139297ecf14Sdrh 140b4e9af9fSdanielk1977 struct IncrblobChannel { 141d0441796Sdanielk1977 sqlite3_blob *pBlob; /* sqlite3 blob handle */ 142dcbb5d3fSdanielk1977 SqliteDb *pDb; /* Associated database connection */ 143b4e9af9fSdanielk1977 int iSeek; /* Current seek offset */ 144d0441796Sdanielk1977 Tcl_Channel channel; /* Channel identifier */ 145d0441796Sdanielk1977 IncrblobChannel *pNext; /* Linked list of all open incrblob channels */ 146d0441796Sdanielk1977 IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */ 147b4e9af9fSdanielk1977 }; 148b4e9af9fSdanielk1977 149ea678832Sdrh /* 150ea678832Sdrh ** Compute a string length that is limited to what can be stored in 151ea678832Sdrh ** lower 30 bits of a 32-bit signed integer. 152ea678832Sdrh */ 1534f21c4afSdrh static int strlen30(const char *z){ 154ea678832Sdrh const char *z2 = z; 155ea678832Sdrh while( *z2 ){ z2++; } 156ea678832Sdrh return 0x3fffffff & (int)(z2 - z); 157ea678832Sdrh } 158ea678832Sdrh 159ea678832Sdrh 16032a0d8bbSdanielk1977 #ifndef SQLITE_OMIT_INCRBLOB 161b4e9af9fSdanielk1977 /* 162d0441796Sdanielk1977 ** Close all incrblob channels opened using database connection pDb. 163d0441796Sdanielk1977 ** This is called when shutting down the database connection. 164d0441796Sdanielk1977 */ 165d0441796Sdanielk1977 static void closeIncrblobChannels(SqliteDb *pDb){ 166d0441796Sdanielk1977 IncrblobChannel *p; 167d0441796Sdanielk1977 IncrblobChannel *pNext; 168d0441796Sdanielk1977 169d0441796Sdanielk1977 for(p=pDb->pIncrblob; p; p=pNext){ 170d0441796Sdanielk1977 pNext = p->pNext; 171d0441796Sdanielk1977 172d0441796Sdanielk1977 /* Note: Calling unregister here call Tcl_Close on the incrblob channel, 173d0441796Sdanielk1977 ** which deletes the IncrblobChannel structure at *p. So do not 174d0441796Sdanielk1977 ** call Tcl_Free() here. 175d0441796Sdanielk1977 */ 176d0441796Sdanielk1977 Tcl_UnregisterChannel(pDb->interp, p->channel); 177d0441796Sdanielk1977 } 178d0441796Sdanielk1977 } 179d0441796Sdanielk1977 180d0441796Sdanielk1977 /* 181b4e9af9fSdanielk1977 ** Close an incremental blob channel. 182b4e9af9fSdanielk1977 */ 183b4e9af9fSdanielk1977 static int incrblobClose(ClientData instanceData, Tcl_Interp *interp){ 184b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 18592d4d7a9Sdanielk1977 int rc = sqlite3_blob_close(p->pBlob); 18692d4d7a9Sdanielk1977 sqlite3 *db = p->pDb->db; 187d0441796Sdanielk1977 188d0441796Sdanielk1977 /* Remove the channel from the SqliteDb.pIncrblob list. */ 189d0441796Sdanielk1977 if( p->pNext ){ 190d0441796Sdanielk1977 p->pNext->pPrev = p->pPrev; 191d0441796Sdanielk1977 } 192d0441796Sdanielk1977 if( p->pPrev ){ 193d0441796Sdanielk1977 p->pPrev->pNext = p->pNext; 194d0441796Sdanielk1977 } 195d0441796Sdanielk1977 if( p->pDb->pIncrblob==p ){ 196d0441796Sdanielk1977 p->pDb->pIncrblob = p->pNext; 197d0441796Sdanielk1977 } 198d0441796Sdanielk1977 19992d4d7a9Sdanielk1977 /* Free the IncrblobChannel structure */ 200b4e9af9fSdanielk1977 Tcl_Free((char *)p); 20192d4d7a9Sdanielk1977 20292d4d7a9Sdanielk1977 if( rc!=SQLITE_OK ){ 20392d4d7a9Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); 20492d4d7a9Sdanielk1977 return TCL_ERROR; 20592d4d7a9Sdanielk1977 } 206b4e9af9fSdanielk1977 return TCL_OK; 207b4e9af9fSdanielk1977 } 208b4e9af9fSdanielk1977 209b4e9af9fSdanielk1977 /* 210b4e9af9fSdanielk1977 ** Read data from an incremental blob channel. 211b4e9af9fSdanielk1977 */ 212b4e9af9fSdanielk1977 static int incrblobInput( 213b4e9af9fSdanielk1977 ClientData instanceData, 214b4e9af9fSdanielk1977 char *buf, 215b4e9af9fSdanielk1977 int bufSize, 216b4e9af9fSdanielk1977 int *errorCodePtr 217b4e9af9fSdanielk1977 ){ 218b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 219b4e9af9fSdanielk1977 int nRead = bufSize; /* Number of bytes to read */ 220b4e9af9fSdanielk1977 int nBlob; /* Total size of the blob */ 221b4e9af9fSdanielk1977 int rc; /* sqlite error code */ 222b4e9af9fSdanielk1977 223b4e9af9fSdanielk1977 nBlob = sqlite3_blob_bytes(p->pBlob); 224b4e9af9fSdanielk1977 if( (p->iSeek+nRead)>nBlob ){ 225b4e9af9fSdanielk1977 nRead = nBlob-p->iSeek; 226b4e9af9fSdanielk1977 } 227b4e9af9fSdanielk1977 if( nRead<=0 ){ 228b4e9af9fSdanielk1977 return 0; 229b4e9af9fSdanielk1977 } 230b4e9af9fSdanielk1977 231b4e9af9fSdanielk1977 rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek); 232b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 233b4e9af9fSdanielk1977 *errorCodePtr = rc; 234b4e9af9fSdanielk1977 return -1; 235b4e9af9fSdanielk1977 } 236b4e9af9fSdanielk1977 237b4e9af9fSdanielk1977 p->iSeek += nRead; 238b4e9af9fSdanielk1977 return nRead; 239b4e9af9fSdanielk1977 } 240b4e9af9fSdanielk1977 241d0441796Sdanielk1977 /* 242d0441796Sdanielk1977 ** Write data to an incremental blob channel. 243d0441796Sdanielk1977 */ 244b4e9af9fSdanielk1977 static int incrblobOutput( 245b4e9af9fSdanielk1977 ClientData instanceData, 246b4e9af9fSdanielk1977 CONST char *buf, 247b4e9af9fSdanielk1977 int toWrite, 248b4e9af9fSdanielk1977 int *errorCodePtr 249b4e9af9fSdanielk1977 ){ 250b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 251b4e9af9fSdanielk1977 int nWrite = toWrite; /* Number of bytes to write */ 252b4e9af9fSdanielk1977 int nBlob; /* Total size of the blob */ 253b4e9af9fSdanielk1977 int rc; /* sqlite error code */ 254b4e9af9fSdanielk1977 255b4e9af9fSdanielk1977 nBlob = sqlite3_blob_bytes(p->pBlob); 256b4e9af9fSdanielk1977 if( (p->iSeek+nWrite)>nBlob ){ 257b4e9af9fSdanielk1977 *errorCodePtr = EINVAL; 258b4e9af9fSdanielk1977 return -1; 259b4e9af9fSdanielk1977 } 260b4e9af9fSdanielk1977 if( nWrite<=0 ){ 261b4e9af9fSdanielk1977 return 0; 262b4e9af9fSdanielk1977 } 263b4e9af9fSdanielk1977 264b4e9af9fSdanielk1977 rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek); 265b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 266b4e9af9fSdanielk1977 *errorCodePtr = EIO; 267b4e9af9fSdanielk1977 return -1; 268b4e9af9fSdanielk1977 } 269b4e9af9fSdanielk1977 270b4e9af9fSdanielk1977 p->iSeek += nWrite; 271b4e9af9fSdanielk1977 return nWrite; 272b4e9af9fSdanielk1977 } 273b4e9af9fSdanielk1977 274b4e9af9fSdanielk1977 /* 275b4e9af9fSdanielk1977 ** Seek an incremental blob channel. 276b4e9af9fSdanielk1977 */ 277b4e9af9fSdanielk1977 static int incrblobSeek( 278b4e9af9fSdanielk1977 ClientData instanceData, 279b4e9af9fSdanielk1977 long offset, 280b4e9af9fSdanielk1977 int seekMode, 281b4e9af9fSdanielk1977 int *errorCodePtr 282b4e9af9fSdanielk1977 ){ 283b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 284b4e9af9fSdanielk1977 285b4e9af9fSdanielk1977 switch( seekMode ){ 286b4e9af9fSdanielk1977 case SEEK_SET: 287b4e9af9fSdanielk1977 p->iSeek = offset; 288b4e9af9fSdanielk1977 break; 289b4e9af9fSdanielk1977 case SEEK_CUR: 290b4e9af9fSdanielk1977 p->iSeek += offset; 291b4e9af9fSdanielk1977 break; 292b4e9af9fSdanielk1977 case SEEK_END: 293b4e9af9fSdanielk1977 p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset; 294b4e9af9fSdanielk1977 break; 295b4e9af9fSdanielk1977 296b4e9af9fSdanielk1977 default: assert(!"Bad seekMode"); 297b4e9af9fSdanielk1977 } 298b4e9af9fSdanielk1977 299b4e9af9fSdanielk1977 return p->iSeek; 300b4e9af9fSdanielk1977 } 301b4e9af9fSdanielk1977 302b4e9af9fSdanielk1977 303b4e9af9fSdanielk1977 static void incrblobWatch(ClientData instanceData, int mode){ 304b4e9af9fSdanielk1977 /* NO-OP */ 305b4e9af9fSdanielk1977 } 306b4e9af9fSdanielk1977 static int incrblobHandle(ClientData instanceData, int dir, ClientData *hPtr){ 307b4e9af9fSdanielk1977 return TCL_ERROR; 308b4e9af9fSdanielk1977 } 309b4e9af9fSdanielk1977 310b4e9af9fSdanielk1977 static Tcl_ChannelType IncrblobChannelType = { 311b4e9af9fSdanielk1977 "incrblob", /* typeName */ 312b4e9af9fSdanielk1977 TCL_CHANNEL_VERSION_2, /* version */ 313b4e9af9fSdanielk1977 incrblobClose, /* closeProc */ 314b4e9af9fSdanielk1977 incrblobInput, /* inputProc */ 315b4e9af9fSdanielk1977 incrblobOutput, /* outputProc */ 316b4e9af9fSdanielk1977 incrblobSeek, /* seekProc */ 317b4e9af9fSdanielk1977 0, /* setOptionProc */ 318b4e9af9fSdanielk1977 0, /* getOptionProc */ 319b4e9af9fSdanielk1977 incrblobWatch, /* watchProc (this is a no-op) */ 320b4e9af9fSdanielk1977 incrblobHandle, /* getHandleProc (always returns error) */ 321b4e9af9fSdanielk1977 0, /* close2Proc */ 322b4e9af9fSdanielk1977 0, /* blockModeProc */ 323b4e9af9fSdanielk1977 0, /* flushProc */ 324b4e9af9fSdanielk1977 0, /* handlerProc */ 325b4e9af9fSdanielk1977 0, /* wideSeekProc */ 326b4e9af9fSdanielk1977 }; 327b4e9af9fSdanielk1977 328b4e9af9fSdanielk1977 /* 329b4e9af9fSdanielk1977 ** Create a new incrblob channel. 330b4e9af9fSdanielk1977 */ 331b4e9af9fSdanielk1977 static int createIncrblobChannel( 332b4e9af9fSdanielk1977 Tcl_Interp *interp, 333b4e9af9fSdanielk1977 SqliteDb *pDb, 334b4e9af9fSdanielk1977 const char *zDb, 335b4e9af9fSdanielk1977 const char *zTable, 336b4e9af9fSdanielk1977 const char *zColumn, 3378cbadb02Sdanielk1977 sqlite_int64 iRow, 3388cbadb02Sdanielk1977 int isReadonly 339b4e9af9fSdanielk1977 ){ 340b4e9af9fSdanielk1977 IncrblobChannel *p; 3418cbadb02Sdanielk1977 sqlite3 *db = pDb->db; 342b4e9af9fSdanielk1977 sqlite3_blob *pBlob; 343b4e9af9fSdanielk1977 int rc; 3448cbadb02Sdanielk1977 int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE); 345b4e9af9fSdanielk1977 346b4e9af9fSdanielk1977 /* This variable is used to name the channels: "incrblob_[incr count]" */ 347b4e9af9fSdanielk1977 static int count = 0; 348b4e9af9fSdanielk1977 char zChannel[64]; 349b4e9af9fSdanielk1977 3508cbadb02Sdanielk1977 rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob); 351b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 352b4e9af9fSdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 353b4e9af9fSdanielk1977 return TCL_ERROR; 354b4e9af9fSdanielk1977 } 355b4e9af9fSdanielk1977 356b4e9af9fSdanielk1977 p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel)); 357b4e9af9fSdanielk1977 p->iSeek = 0; 358b4e9af9fSdanielk1977 p->pBlob = pBlob; 359b4e9af9fSdanielk1977 3605bb3eb9bSdrh sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count); 361d0441796Sdanielk1977 p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags); 362d0441796Sdanielk1977 Tcl_RegisterChannel(interp, p->channel); 363b4e9af9fSdanielk1977 364d0441796Sdanielk1977 /* Link the new channel into the SqliteDb.pIncrblob list. */ 365d0441796Sdanielk1977 p->pNext = pDb->pIncrblob; 366d0441796Sdanielk1977 p->pPrev = 0; 367d0441796Sdanielk1977 if( p->pNext ){ 368d0441796Sdanielk1977 p->pNext->pPrev = p; 369d0441796Sdanielk1977 } 370d0441796Sdanielk1977 pDb->pIncrblob = p; 371d0441796Sdanielk1977 p->pDb = pDb; 372d0441796Sdanielk1977 373d0441796Sdanielk1977 Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE); 374b4e9af9fSdanielk1977 return TCL_OK; 375b4e9af9fSdanielk1977 } 37632a0d8bbSdanielk1977 #else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */ 37732a0d8bbSdanielk1977 #define closeIncrblobChannels(pDb) 37832a0d8bbSdanielk1977 #endif 379b4e9af9fSdanielk1977 3806d31316cSdrh /* 381d1e4733dSdrh ** Look at the script prefix in pCmd. We will be executing this script 382d1e4733dSdrh ** after first appending one or more arguments. This routine analyzes 383d1e4733dSdrh ** the script to see if it is safe to use Tcl_EvalObjv() on the script 384d1e4733dSdrh ** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much 385d1e4733dSdrh ** faster. 386d1e4733dSdrh ** 387d1e4733dSdrh ** Scripts that are safe to use with Tcl_EvalObjv() consists of a 388d1e4733dSdrh ** command name followed by zero or more arguments with no [...] or $ 389d1e4733dSdrh ** or {...} or ; to be seen anywhere. Most callback scripts consist 390d1e4733dSdrh ** of just a single procedure name and they meet this requirement. 391d1e4733dSdrh */ 392d1e4733dSdrh static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){ 393d1e4733dSdrh /* We could try to do something with Tcl_Parse(). But we will instead 394d1e4733dSdrh ** just do a search for forbidden characters. If any of the forbidden 395d1e4733dSdrh ** characters appear in pCmd, we will report the string as unsafe. 396d1e4733dSdrh */ 397d1e4733dSdrh const char *z; 398d1e4733dSdrh int n; 399d1e4733dSdrh z = Tcl_GetStringFromObj(pCmd, &n); 400d1e4733dSdrh while( n-- > 0 ){ 401d1e4733dSdrh int c = *(z++); 402d1e4733dSdrh if( c=='$' || c=='[' || c==';' ) return 0; 403d1e4733dSdrh } 404d1e4733dSdrh return 1; 405d1e4733dSdrh } 406d1e4733dSdrh 407d1e4733dSdrh /* 408d1e4733dSdrh ** Find an SqlFunc structure with the given name. Or create a new 409d1e4733dSdrh ** one if an existing one cannot be found. Return a pointer to the 410d1e4733dSdrh ** structure. 411d1e4733dSdrh */ 412d1e4733dSdrh static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){ 413d1e4733dSdrh SqlFunc *p, *pNew; 414d1e4733dSdrh int i; 4154f21c4afSdrh pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + strlen30(zName) + 1 ); 416d1e4733dSdrh pNew->zName = (char*)&pNew[1]; 417d1e4733dSdrh for(i=0; zName[i]; i++){ pNew->zName[i] = tolower(zName[i]); } 418d1e4733dSdrh pNew->zName[i] = 0; 419d1e4733dSdrh for(p=pDb->pFunc; p; p=p->pNext){ 420d1e4733dSdrh if( strcmp(p->zName, pNew->zName)==0 ){ 421d1e4733dSdrh Tcl_Free((char*)pNew); 422d1e4733dSdrh return p; 423d1e4733dSdrh } 424d1e4733dSdrh } 425d1e4733dSdrh pNew->interp = pDb->interp; 426d1e4733dSdrh pNew->pScript = 0; 427d1e4733dSdrh pNew->pNext = pDb->pFunc; 428d1e4733dSdrh pDb->pFunc = pNew; 429d1e4733dSdrh return pNew; 430d1e4733dSdrh } 431d1e4733dSdrh 432d1e4733dSdrh /* 433fb7e7651Sdrh ** Finalize and free a list of prepared statements 434fb7e7651Sdrh */ 435fb7e7651Sdrh static void flushStmtCache( SqliteDb *pDb ){ 436fb7e7651Sdrh SqlPreparedStmt *pPreStmt; 437fb7e7651Sdrh 438fb7e7651Sdrh while( pDb->stmtList ){ 439fb7e7651Sdrh sqlite3_finalize( pDb->stmtList->pStmt ); 440fb7e7651Sdrh pPreStmt = pDb->stmtList; 441fb7e7651Sdrh pDb->stmtList = pDb->stmtList->pNext; 442fb7e7651Sdrh Tcl_Free( (char*)pPreStmt ); 443fb7e7651Sdrh } 444fb7e7651Sdrh pDb->nStmt = 0; 445fb7e7651Sdrh pDb->stmtLast = 0; 446fb7e7651Sdrh } 447fb7e7651Sdrh 448fb7e7651Sdrh /* 449895d7472Sdrh ** TCL calls this procedure when an sqlite3 database command is 450895d7472Sdrh ** deleted. 45175897234Sdrh */ 45275897234Sdrh static void DbDeleteCmd(void *db){ 453bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)db; 454fb7e7651Sdrh flushStmtCache(pDb); 455d0441796Sdanielk1977 closeIncrblobChannels(pDb); 4566f8a503dSdanielk1977 sqlite3_close(pDb->db); 457cabb0819Sdrh while( pDb->pFunc ){ 458cabb0819Sdrh SqlFunc *pFunc = pDb->pFunc; 459cabb0819Sdrh pDb->pFunc = pFunc->pNext; 460d1e4733dSdrh Tcl_DecrRefCount(pFunc->pScript); 461cabb0819Sdrh Tcl_Free((char*)pFunc); 462cabb0819Sdrh } 4630202b29eSdanielk1977 while( pDb->pCollate ){ 4640202b29eSdanielk1977 SqlCollate *pCollate = pDb->pCollate; 4650202b29eSdanielk1977 pDb->pCollate = pCollate->pNext; 4660202b29eSdanielk1977 Tcl_Free((char*)pCollate); 4670202b29eSdanielk1977 } 468bec3f402Sdrh if( pDb->zBusy ){ 469bec3f402Sdrh Tcl_Free(pDb->zBusy); 470bec3f402Sdrh } 471b5a20d3cSdrh if( pDb->zTrace ){ 472b5a20d3cSdrh Tcl_Free(pDb->zTrace); 4730d1a643aSdrh } 47419e2d37fSdrh if( pDb->zProfile ){ 47519e2d37fSdrh Tcl_Free(pDb->zProfile); 47619e2d37fSdrh } 477e22a334bSdrh if( pDb->zAuth ){ 478e22a334bSdrh Tcl_Free(pDb->zAuth); 479e22a334bSdrh } 48055c45f2eSdanielk1977 if( pDb->zNull ){ 48155c45f2eSdanielk1977 Tcl_Free(pDb->zNull); 48255c45f2eSdanielk1977 } 48394eb6a14Sdanielk1977 if( pDb->pUpdateHook ){ 48494eb6a14Sdanielk1977 Tcl_DecrRefCount(pDb->pUpdateHook); 48594eb6a14Sdanielk1977 } 48671fd80bfSdanielk1977 if( pDb->pRollbackHook ){ 48771fd80bfSdanielk1977 Tcl_DecrRefCount(pDb->pRollbackHook); 48871fd80bfSdanielk1977 } 4895def0843Sdrh if( pDb->pWalHook ){ 4905def0843Sdrh Tcl_DecrRefCount(pDb->pWalHook); 4918d22a174Sdan } 49294eb6a14Sdanielk1977 if( pDb->pCollateNeeded ){ 49394eb6a14Sdanielk1977 Tcl_DecrRefCount(pDb->pCollateNeeded); 49494eb6a14Sdanielk1977 } 495bec3f402Sdrh Tcl_Free((char*)pDb); 496bec3f402Sdrh } 497bec3f402Sdrh 498bec3f402Sdrh /* 499bec3f402Sdrh ** This routine is called when a database file is locked while trying 500bec3f402Sdrh ** to execute SQL. 501bec3f402Sdrh */ 5022a764eb0Sdanielk1977 static int DbBusyHandler(void *cd, int nTries){ 503bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 504bec3f402Sdrh int rc; 505bec3f402Sdrh char zVal[30]; 506bec3f402Sdrh 5075bb3eb9bSdrh sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries); 508d1e4733dSdrh rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0); 509bec3f402Sdrh if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 510bec3f402Sdrh return 0; 511bec3f402Sdrh } 512bec3f402Sdrh return 1; 51375897234Sdrh } 51475897234Sdrh 51526e4a8b1Sdrh #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 51675897234Sdrh /* 517348bb5d6Sdanielk1977 ** This routine is invoked as the 'progress callback' for the database. 518348bb5d6Sdanielk1977 */ 519348bb5d6Sdanielk1977 static int DbProgressHandler(void *cd){ 520348bb5d6Sdanielk1977 SqliteDb *pDb = (SqliteDb*)cd; 521348bb5d6Sdanielk1977 int rc; 522348bb5d6Sdanielk1977 523348bb5d6Sdanielk1977 assert( pDb->zProgress ); 524348bb5d6Sdanielk1977 rc = Tcl_Eval(pDb->interp, pDb->zProgress); 525348bb5d6Sdanielk1977 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 526348bb5d6Sdanielk1977 return 1; 527348bb5d6Sdanielk1977 } 528348bb5d6Sdanielk1977 return 0; 529348bb5d6Sdanielk1977 } 53026e4a8b1Sdrh #endif 531348bb5d6Sdanielk1977 532d1167393Sdrh #ifndef SQLITE_OMIT_TRACE 533348bb5d6Sdanielk1977 /* 534b5a20d3cSdrh ** This routine is called by the SQLite trace handler whenever a new 535b5a20d3cSdrh ** block of SQL is executed. The TCL script in pDb->zTrace is executed. 5360d1a643aSdrh */ 537b5a20d3cSdrh static void DbTraceHandler(void *cd, const char *zSql){ 5380d1a643aSdrh SqliteDb *pDb = (SqliteDb*)cd; 539b5a20d3cSdrh Tcl_DString str; 5400d1a643aSdrh 541b5a20d3cSdrh Tcl_DStringInit(&str); 542b5a20d3cSdrh Tcl_DStringAppend(&str, pDb->zTrace, -1); 543b5a20d3cSdrh Tcl_DStringAppendElement(&str, zSql); 544b5a20d3cSdrh Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 545b5a20d3cSdrh Tcl_DStringFree(&str); 546b5a20d3cSdrh Tcl_ResetResult(pDb->interp); 5470d1a643aSdrh } 548d1167393Sdrh #endif 5490d1a643aSdrh 550d1167393Sdrh #ifndef SQLITE_OMIT_TRACE 5510d1a643aSdrh /* 55219e2d37fSdrh ** This routine is called by the SQLite profile handler after a statement 55319e2d37fSdrh ** SQL has executed. The TCL script in pDb->zProfile is evaluated. 55419e2d37fSdrh */ 55519e2d37fSdrh static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){ 55619e2d37fSdrh SqliteDb *pDb = (SqliteDb*)cd; 55719e2d37fSdrh Tcl_DString str; 55819e2d37fSdrh char zTm[100]; 55919e2d37fSdrh 56019e2d37fSdrh sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm); 56119e2d37fSdrh Tcl_DStringInit(&str); 56219e2d37fSdrh Tcl_DStringAppend(&str, pDb->zProfile, -1); 56319e2d37fSdrh Tcl_DStringAppendElement(&str, zSql); 56419e2d37fSdrh Tcl_DStringAppendElement(&str, zTm); 56519e2d37fSdrh Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 56619e2d37fSdrh Tcl_DStringFree(&str); 56719e2d37fSdrh Tcl_ResetResult(pDb->interp); 56819e2d37fSdrh } 569d1167393Sdrh #endif 57019e2d37fSdrh 57119e2d37fSdrh /* 572aa940eacSdrh ** This routine is called when a transaction is committed. The 573aa940eacSdrh ** TCL script in pDb->zCommit is executed. If it returns non-zero or 574aa940eacSdrh ** if it throws an exception, the transaction is rolled back instead 575aa940eacSdrh ** of being committed. 576aa940eacSdrh */ 577aa940eacSdrh static int DbCommitHandler(void *cd){ 578aa940eacSdrh SqliteDb *pDb = (SqliteDb*)cd; 579aa940eacSdrh int rc; 580aa940eacSdrh 581aa940eacSdrh rc = Tcl_Eval(pDb->interp, pDb->zCommit); 582aa940eacSdrh if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 583aa940eacSdrh return 1; 584aa940eacSdrh } 585aa940eacSdrh return 0; 586aa940eacSdrh } 587aa940eacSdrh 58871fd80bfSdanielk1977 static void DbRollbackHandler(void *clientData){ 58971fd80bfSdanielk1977 SqliteDb *pDb = (SqliteDb*)clientData; 59071fd80bfSdanielk1977 assert(pDb->pRollbackHook); 59171fd80bfSdanielk1977 if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){ 59271fd80bfSdanielk1977 Tcl_BackgroundError(pDb->interp); 59371fd80bfSdanielk1977 } 59471fd80bfSdanielk1977 } 59571fd80bfSdanielk1977 5965def0843Sdrh /* 5975def0843Sdrh ** This procedure handles wal_hook callbacks. 5985def0843Sdrh */ 5995def0843Sdrh static int DbWalHandler( 6008d22a174Sdan void *clientData, 6018d22a174Sdan sqlite3 *db, 6028d22a174Sdan const char *zDb, 6038d22a174Sdan int nEntry 6048d22a174Sdan ){ 6055def0843Sdrh int ret = SQLITE_OK; 6068d22a174Sdan Tcl_Obj *p; 6078d22a174Sdan SqliteDb *pDb = (SqliteDb*)clientData; 6088d22a174Sdan Tcl_Interp *interp = pDb->interp; 6095def0843Sdrh assert(pDb->pWalHook); 6108d22a174Sdan 6115def0843Sdrh p = Tcl_DuplicateObj(pDb->pWalHook); 6128d22a174Sdan Tcl_IncrRefCount(p); 6138d22a174Sdan Tcl_ListObjAppendElement(interp, p, Tcl_NewStringObj(zDb, -1)); 6148d22a174Sdan Tcl_ListObjAppendElement(interp, p, Tcl_NewIntObj(nEntry)); 6158d22a174Sdan if( TCL_OK!=Tcl_EvalObjEx(interp, p, 0) 6168d22a174Sdan || TCL_OK!=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &ret) 6178d22a174Sdan ){ 6188d22a174Sdan Tcl_BackgroundError(interp); 6198d22a174Sdan } 6208d22a174Sdan Tcl_DecrRefCount(p); 6218d22a174Sdan 6228d22a174Sdan return ret; 6238d22a174Sdan } 6248d22a174Sdan 625bcf4f484Sdrh #if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_UNLOCK_NOTIFY) 626404ca075Sdanielk1977 static void setTestUnlockNotifyVars(Tcl_Interp *interp, int iArg, int nArg){ 627404ca075Sdanielk1977 char zBuf[64]; 628404ca075Sdanielk1977 sprintf(zBuf, "%d", iArg); 629404ca075Sdanielk1977 Tcl_SetVar(interp, "sqlite_unlock_notify_arg", zBuf, TCL_GLOBAL_ONLY); 630404ca075Sdanielk1977 sprintf(zBuf, "%d", nArg); 631404ca075Sdanielk1977 Tcl_SetVar(interp, "sqlite_unlock_notify_argcount", zBuf, TCL_GLOBAL_ONLY); 632404ca075Sdanielk1977 } 633404ca075Sdanielk1977 #else 634404ca075Sdanielk1977 # define setTestUnlockNotifyVars(x,y,z) 635404ca075Sdanielk1977 #endif 636404ca075Sdanielk1977 63769910da9Sdrh #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 638404ca075Sdanielk1977 static void DbUnlockNotify(void **apArg, int nArg){ 639404ca075Sdanielk1977 int i; 640404ca075Sdanielk1977 for(i=0; i<nArg; i++){ 641404ca075Sdanielk1977 const int flags = (TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); 642404ca075Sdanielk1977 SqliteDb *pDb = (SqliteDb *)apArg[i]; 643404ca075Sdanielk1977 setTestUnlockNotifyVars(pDb->interp, i, nArg); 644404ca075Sdanielk1977 assert( pDb->pUnlockNotify); 645404ca075Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pDb->pUnlockNotify, flags); 646404ca075Sdanielk1977 Tcl_DecrRefCount(pDb->pUnlockNotify); 647404ca075Sdanielk1977 pDb->pUnlockNotify = 0; 648404ca075Sdanielk1977 } 649404ca075Sdanielk1977 } 65069910da9Sdrh #endif 651404ca075Sdanielk1977 65294eb6a14Sdanielk1977 static void DbUpdateHandler( 65394eb6a14Sdanielk1977 void *p, 65494eb6a14Sdanielk1977 int op, 65594eb6a14Sdanielk1977 const char *zDb, 65694eb6a14Sdanielk1977 const char *zTbl, 65794eb6a14Sdanielk1977 sqlite_int64 rowid 65894eb6a14Sdanielk1977 ){ 65994eb6a14Sdanielk1977 SqliteDb *pDb = (SqliteDb *)p; 66094eb6a14Sdanielk1977 Tcl_Obj *pCmd; 66194eb6a14Sdanielk1977 66294eb6a14Sdanielk1977 assert( pDb->pUpdateHook ); 66394eb6a14Sdanielk1977 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); 66494eb6a14Sdanielk1977 66594eb6a14Sdanielk1977 pCmd = Tcl_DuplicateObj(pDb->pUpdateHook); 66694eb6a14Sdanielk1977 Tcl_IncrRefCount(pCmd); 66794eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj( 66894eb6a14Sdanielk1977 ( (op==SQLITE_INSERT)?"INSERT":(op==SQLITE_UPDATE)?"UPDATE":"DELETE"), -1)); 66994eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); 67094eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); 67194eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid)); 67294eb6a14Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 673efdde169Sdrh Tcl_DecrRefCount(pCmd); 67494eb6a14Sdanielk1977 } 67594eb6a14Sdanielk1977 6767cedc8d4Sdanielk1977 static void tclCollateNeeded( 6777cedc8d4Sdanielk1977 void *pCtx, 6789bb575fdSdrh sqlite3 *db, 6797cedc8d4Sdanielk1977 int enc, 6807cedc8d4Sdanielk1977 const char *zName 6817cedc8d4Sdanielk1977 ){ 6827cedc8d4Sdanielk1977 SqliteDb *pDb = (SqliteDb *)pCtx; 6837cedc8d4Sdanielk1977 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded); 6847cedc8d4Sdanielk1977 Tcl_IncrRefCount(pScript); 6857cedc8d4Sdanielk1977 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1)); 6867cedc8d4Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pScript, 0); 6877cedc8d4Sdanielk1977 Tcl_DecrRefCount(pScript); 6887cedc8d4Sdanielk1977 } 6897cedc8d4Sdanielk1977 690aa940eacSdrh /* 6910202b29eSdanielk1977 ** This routine is called to evaluate an SQL collation function implemented 6920202b29eSdanielk1977 ** using TCL script. 6930202b29eSdanielk1977 */ 6940202b29eSdanielk1977 static int tclSqlCollate( 6950202b29eSdanielk1977 void *pCtx, 6960202b29eSdanielk1977 int nA, 6970202b29eSdanielk1977 const void *zA, 6980202b29eSdanielk1977 int nB, 6990202b29eSdanielk1977 const void *zB 7000202b29eSdanielk1977 ){ 7010202b29eSdanielk1977 SqlCollate *p = (SqlCollate *)pCtx; 7020202b29eSdanielk1977 Tcl_Obj *pCmd; 7030202b29eSdanielk1977 7040202b29eSdanielk1977 pCmd = Tcl_NewStringObj(p->zScript, -1); 7050202b29eSdanielk1977 Tcl_IncrRefCount(pCmd); 7060202b29eSdanielk1977 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA)); 7070202b29eSdanielk1977 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB)); 708d1e4733dSdrh Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 7090202b29eSdanielk1977 Tcl_DecrRefCount(pCmd); 7100202b29eSdanielk1977 return (atoi(Tcl_GetStringResult(p->interp))); 7110202b29eSdanielk1977 } 7120202b29eSdanielk1977 7130202b29eSdanielk1977 /* 714cabb0819Sdrh ** This routine is called to evaluate an SQL function implemented 715cabb0819Sdrh ** using TCL script. 716cabb0819Sdrh */ 7170ae8b831Sdanielk1977 static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ 7186f8a503dSdanielk1977 SqlFunc *p = sqlite3_user_data(context); 719d1e4733dSdrh Tcl_Obj *pCmd; 720cabb0819Sdrh int i; 721cabb0819Sdrh int rc; 722cabb0819Sdrh 723d1e4733dSdrh if( argc==0 ){ 724d1e4733dSdrh /* If there are no arguments to the function, call Tcl_EvalObjEx on the 725d1e4733dSdrh ** script object directly. This allows the TCL compiler to generate 726d1e4733dSdrh ** bytecode for the command on the first invocation and thus make 727d1e4733dSdrh ** subsequent invocations much faster. */ 728d1e4733dSdrh pCmd = p->pScript; 729d1e4733dSdrh Tcl_IncrRefCount(pCmd); 730d1e4733dSdrh rc = Tcl_EvalObjEx(p->interp, pCmd, 0); 731d1e4733dSdrh Tcl_DecrRefCount(pCmd); 73251ad0ecdSdanielk1977 }else{ 733d1e4733dSdrh /* If there are arguments to the function, make a shallow copy of the 734d1e4733dSdrh ** script object, lappend the arguments, then evaluate the copy. 735d1e4733dSdrh ** 736d1e4733dSdrh ** By "shallow" copy, we mean a only the outer list Tcl_Obj is duplicated. 737d1e4733dSdrh ** The new Tcl_Obj contains pointers to the original list elements. 738d1e4733dSdrh ** That way, when Tcl_EvalObjv() is run and shimmers the first element 739d1e4733dSdrh ** of the list to tclCmdNameType, that alternate representation will 740d1e4733dSdrh ** be preserved and reused on the next invocation. 741d1e4733dSdrh */ 742d1e4733dSdrh Tcl_Obj **aArg; 743d1e4733dSdrh int nArg; 744d1e4733dSdrh if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){ 745d1e4733dSdrh sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 746d1e4733dSdrh return; 747d1e4733dSdrh } 748d1e4733dSdrh pCmd = Tcl_NewListObj(nArg, aArg); 749d1e4733dSdrh Tcl_IncrRefCount(pCmd); 750d1e4733dSdrh for(i=0; i<argc; i++){ 751d1e4733dSdrh sqlite3_value *pIn = argv[i]; 752d1e4733dSdrh Tcl_Obj *pVal; 753d1e4733dSdrh 754d1e4733dSdrh /* Set pVal to contain the i'th column of this row. */ 755d1e4733dSdrh switch( sqlite3_value_type(pIn) ){ 756d1e4733dSdrh case SQLITE_BLOB: { 757d1e4733dSdrh int bytes = sqlite3_value_bytes(pIn); 758d1e4733dSdrh pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes); 759d1e4733dSdrh break; 760d1e4733dSdrh } 761d1e4733dSdrh case SQLITE_INTEGER: { 762d1e4733dSdrh sqlite_int64 v = sqlite3_value_int64(pIn); 763d1e4733dSdrh if( v>=-2147483647 && v<=2147483647 ){ 764d1e4733dSdrh pVal = Tcl_NewIntObj(v); 765d1e4733dSdrh }else{ 766d1e4733dSdrh pVal = Tcl_NewWideIntObj(v); 767d1e4733dSdrh } 768d1e4733dSdrh break; 769d1e4733dSdrh } 770d1e4733dSdrh case SQLITE_FLOAT: { 771d1e4733dSdrh double r = sqlite3_value_double(pIn); 772d1e4733dSdrh pVal = Tcl_NewDoubleObj(r); 773d1e4733dSdrh break; 774d1e4733dSdrh } 775d1e4733dSdrh case SQLITE_NULL: { 776d1e4733dSdrh pVal = Tcl_NewStringObj("", 0); 777d1e4733dSdrh break; 778d1e4733dSdrh } 779d1e4733dSdrh default: { 780d1e4733dSdrh int bytes = sqlite3_value_bytes(pIn); 78100fd957bSdanielk1977 pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes); 782d1e4733dSdrh break; 78351ad0ecdSdanielk1977 } 784cabb0819Sdrh } 785d1e4733dSdrh rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal); 786d1e4733dSdrh if( rc ){ 787d1e4733dSdrh Tcl_DecrRefCount(pCmd); 788d1e4733dSdrh sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 789d1e4733dSdrh return; 790d1e4733dSdrh } 791d1e4733dSdrh } 792d1e4733dSdrh if( !p->useEvalObjv ){ 793d1e4733dSdrh /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd 794d1e4733dSdrh ** is a list without a string representation. To prevent this from 795d1e4733dSdrh ** happening, make sure pCmd has a valid string representation */ 796d1e4733dSdrh Tcl_GetString(pCmd); 797d1e4733dSdrh } 798d1e4733dSdrh rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 799d1e4733dSdrh Tcl_DecrRefCount(pCmd); 800d1e4733dSdrh } 801562e8d3cSdanielk1977 802c7f269d5Sdrh if( rc && rc!=TCL_RETURN ){ 8037e18c259Sdanielk1977 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 804cabb0819Sdrh }else{ 805c7f269d5Sdrh Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); 806c7f269d5Sdrh int n; 807c7f269d5Sdrh u8 *data; 8084a4c11aaSdan const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 809c7f269d5Sdrh char c = zType[0]; 810df0bddaeSdrh if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ 811d1e4733dSdrh /* Only return a BLOB type if the Tcl variable is a bytearray and 812df0bddaeSdrh ** has no string representation. */ 813c7f269d5Sdrh data = Tcl_GetByteArrayFromObj(pVar, &n); 814c7f269d5Sdrh sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT); 815985e0c63Sdrh }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 816c7f269d5Sdrh Tcl_GetIntFromObj(0, pVar, &n); 817c7f269d5Sdrh sqlite3_result_int(context, n); 818c7f269d5Sdrh }else if( c=='d' && strcmp(zType,"double")==0 ){ 819c7f269d5Sdrh double r; 820c7f269d5Sdrh Tcl_GetDoubleFromObj(0, pVar, &r); 821c7f269d5Sdrh sqlite3_result_double(context, r); 822985e0c63Sdrh }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 823985e0c63Sdrh (c=='i' && strcmp(zType,"int")==0) ){ 824df0bddaeSdrh Tcl_WideInt v; 825df0bddaeSdrh Tcl_GetWideIntFromObj(0, pVar, &v); 826df0bddaeSdrh sqlite3_result_int64(context, v); 827c7f269d5Sdrh }else{ 82800fd957bSdanielk1977 data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 82900fd957bSdanielk1977 sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT); 830c7f269d5Sdrh } 831cabb0819Sdrh } 832cabb0819Sdrh } 833895d7472Sdrh 834e22a334bSdrh #ifndef SQLITE_OMIT_AUTHORIZATION 835e22a334bSdrh /* 836e22a334bSdrh ** This is the authentication function. It appends the authentication 837e22a334bSdrh ** type code and the two arguments to zCmd[] then invokes the result 838e22a334bSdrh ** on the interpreter. The reply is examined to determine if the 839e22a334bSdrh ** authentication fails or succeeds. 840e22a334bSdrh */ 841e22a334bSdrh static int auth_callback( 842e22a334bSdrh void *pArg, 843e22a334bSdrh int code, 844e22a334bSdrh const char *zArg1, 845e22a334bSdrh const char *zArg2, 846e22a334bSdrh const char *zArg3, 847e22a334bSdrh const char *zArg4 848e22a334bSdrh ){ 849e22a334bSdrh char *zCode; 850e22a334bSdrh Tcl_DString str; 851e22a334bSdrh int rc; 852e22a334bSdrh const char *zReply; 853e22a334bSdrh SqliteDb *pDb = (SqliteDb*)pArg; 8541f1549f8Sdrh if( pDb->disableAuth ) return SQLITE_OK; 855e22a334bSdrh 856e22a334bSdrh switch( code ){ 857e22a334bSdrh case SQLITE_COPY : zCode="SQLITE_COPY"; break; 858e22a334bSdrh case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; 859e22a334bSdrh case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; 860e22a334bSdrh case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; 861e22a334bSdrh case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; 862e22a334bSdrh case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; 863e22a334bSdrh case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; 864e22a334bSdrh case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; 865e22a334bSdrh case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; 866e22a334bSdrh case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; 867e22a334bSdrh case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; 868e22a334bSdrh case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; 869e22a334bSdrh case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; 870e22a334bSdrh case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; 871e22a334bSdrh case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; 872e22a334bSdrh case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; 873e22a334bSdrh case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; 874e22a334bSdrh case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; 875e22a334bSdrh case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; 876e22a334bSdrh case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; 877e22a334bSdrh case SQLITE_READ : zCode="SQLITE_READ"; break; 878e22a334bSdrh case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; 879e22a334bSdrh case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; 880e22a334bSdrh case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; 88181e293b4Sdrh case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; 88281e293b4Sdrh case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; 8831c8c23ccSdanielk1977 case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break; 8841d54df88Sdanielk1977 case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break; 885e6e04969Sdrh case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break; 886f1a381e7Sdanielk1977 case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break; 887f1a381e7Sdanielk1977 case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break; 8885169bbc6Sdrh case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break; 889ab9b703fSdanielk1977 case SQLITE_SAVEPOINT : zCode="SQLITE_SAVEPOINT"; break; 890e22a334bSdrh default : zCode="????"; break; 891e22a334bSdrh } 892e22a334bSdrh Tcl_DStringInit(&str); 893e22a334bSdrh Tcl_DStringAppend(&str, pDb->zAuth, -1); 894e22a334bSdrh Tcl_DStringAppendElement(&str, zCode); 895e22a334bSdrh Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); 896e22a334bSdrh Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); 897e22a334bSdrh Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); 898e22a334bSdrh Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); 899e22a334bSdrh rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); 900e22a334bSdrh Tcl_DStringFree(&str); 901e22a334bSdrh zReply = Tcl_GetStringResult(pDb->interp); 902e22a334bSdrh if( strcmp(zReply,"SQLITE_OK")==0 ){ 903e22a334bSdrh rc = SQLITE_OK; 904e22a334bSdrh }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ 905e22a334bSdrh rc = SQLITE_DENY; 906e22a334bSdrh }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ 907e22a334bSdrh rc = SQLITE_IGNORE; 908e22a334bSdrh }else{ 909e22a334bSdrh rc = 999; 910e22a334bSdrh } 911e22a334bSdrh return rc; 912e22a334bSdrh } 913e22a334bSdrh #endif /* SQLITE_OMIT_AUTHORIZATION */ 914cabb0819Sdrh 915cabb0819Sdrh /* 916ef2cb63eSdanielk1977 ** zText is a pointer to text obtained via an sqlite3_result_text() 917ef2cb63eSdanielk1977 ** or similar interface. This routine returns a Tcl string object, 918ef2cb63eSdanielk1977 ** reference count set to 0, containing the text. If a translation 919ef2cb63eSdanielk1977 ** between iso8859 and UTF-8 is required, it is preformed. 920ef2cb63eSdanielk1977 */ 921ef2cb63eSdanielk1977 static Tcl_Obj *dbTextToObj(char const *zText){ 922ef2cb63eSdanielk1977 Tcl_Obj *pVal; 923ef2cb63eSdanielk1977 #ifdef UTF_TRANSLATION_NEEDED 924ef2cb63eSdanielk1977 Tcl_DString dCol; 925ef2cb63eSdanielk1977 Tcl_DStringInit(&dCol); 926ef2cb63eSdanielk1977 Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol); 927ef2cb63eSdanielk1977 pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); 928ef2cb63eSdanielk1977 Tcl_DStringFree(&dCol); 929ef2cb63eSdanielk1977 #else 930ef2cb63eSdanielk1977 pVal = Tcl_NewStringObj(zText, -1); 931ef2cb63eSdanielk1977 #endif 932ef2cb63eSdanielk1977 return pVal; 933ef2cb63eSdanielk1977 } 934ef2cb63eSdanielk1977 935ef2cb63eSdanielk1977 /* 9361067fe11Stpoindex ** This routine reads a line of text from FILE in, stores 9371067fe11Stpoindex ** the text in memory obtained from malloc() and returns a pointer 9381067fe11Stpoindex ** to the text. NULL is returned at end of file, or if malloc() 9391067fe11Stpoindex ** fails. 9401067fe11Stpoindex ** 9411067fe11Stpoindex ** The interface is like "readline" but no command-line editing 9421067fe11Stpoindex ** is done. 9431067fe11Stpoindex ** 9441067fe11Stpoindex ** copied from shell.c from '.import' command 9451067fe11Stpoindex */ 9461067fe11Stpoindex static char *local_getline(char *zPrompt, FILE *in){ 9471067fe11Stpoindex char *zLine; 9481067fe11Stpoindex int nLine; 9491067fe11Stpoindex int n; 9501067fe11Stpoindex int eol; 9511067fe11Stpoindex 9521067fe11Stpoindex nLine = 100; 9531067fe11Stpoindex zLine = malloc( nLine ); 9541067fe11Stpoindex if( zLine==0 ) return 0; 9551067fe11Stpoindex n = 0; 9561067fe11Stpoindex eol = 0; 9571067fe11Stpoindex while( !eol ){ 9581067fe11Stpoindex if( n+100>nLine ){ 9591067fe11Stpoindex nLine = nLine*2 + 100; 9601067fe11Stpoindex zLine = realloc(zLine, nLine); 9611067fe11Stpoindex if( zLine==0 ) return 0; 9621067fe11Stpoindex } 9631067fe11Stpoindex if( fgets(&zLine[n], nLine - n, in)==0 ){ 9641067fe11Stpoindex if( n==0 ){ 9651067fe11Stpoindex free(zLine); 9661067fe11Stpoindex return 0; 9671067fe11Stpoindex } 9681067fe11Stpoindex zLine[n] = 0; 9691067fe11Stpoindex eol = 1; 9701067fe11Stpoindex break; 9711067fe11Stpoindex } 9721067fe11Stpoindex while( zLine[n] ){ n++; } 9731067fe11Stpoindex if( n>0 && zLine[n-1]=='\n' ){ 9741067fe11Stpoindex n--; 9751067fe11Stpoindex zLine[n] = 0; 9761067fe11Stpoindex eol = 1; 9771067fe11Stpoindex } 9781067fe11Stpoindex } 9791067fe11Stpoindex zLine = realloc( zLine, n+1 ); 9801067fe11Stpoindex return zLine; 9811067fe11Stpoindex } 9821067fe11Stpoindex 9838e556520Sdanielk1977 9848e556520Sdanielk1977 /* 9854a4c11aaSdan ** This function is part of the implementation of the command: 9868e556520Sdanielk1977 ** 9874a4c11aaSdan ** $db transaction [-deferred|-immediate|-exclusive] SCRIPT 9888e556520Sdanielk1977 ** 9894a4c11aaSdan ** It is invoked after evaluating the script SCRIPT to commit or rollback 9904a4c11aaSdan ** the transaction or savepoint opened by the [transaction] command. 9914a4c11aaSdan */ 9924a4c11aaSdan static int DbTransPostCmd( 9934a4c11aaSdan ClientData data[], /* data[0] is the Sqlite3Db* for $db */ 9944a4c11aaSdan Tcl_Interp *interp, /* Tcl interpreter */ 9954a4c11aaSdan int result /* Result of evaluating SCRIPT */ 9964a4c11aaSdan ){ 9974a4c11aaSdan static const char *azEnd[] = { 9984a4c11aaSdan "RELEASE _tcl_transaction", /* rc==TCL_ERROR, nTransaction!=0 */ 9994a4c11aaSdan "COMMIT", /* rc!=TCL_ERROR, nTransaction==0 */ 10004a4c11aaSdan "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction", 10014a4c11aaSdan "ROLLBACK" /* rc==TCL_ERROR, nTransaction==0 */ 10024a4c11aaSdan }; 10034a4c11aaSdan SqliteDb *pDb = (SqliteDb*)data[0]; 10044a4c11aaSdan int rc = result; 10054a4c11aaSdan const char *zEnd; 10064a4c11aaSdan 10074a4c11aaSdan pDb->nTransaction--; 10084a4c11aaSdan zEnd = azEnd[(rc==TCL_ERROR)*2 + (pDb->nTransaction==0)]; 10094a4c11aaSdan 10104a4c11aaSdan pDb->disableAuth++; 10114a4c11aaSdan if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){ 10124a4c11aaSdan /* This is a tricky scenario to handle. The most likely cause of an 10134a4c11aaSdan ** error is that the exec() above was an attempt to commit the 10144a4c11aaSdan ** top-level transaction that returned SQLITE_BUSY. Or, less likely, 10154a4c11aaSdan ** that an IO-error has occured. In either case, throw a Tcl exception 10164a4c11aaSdan ** and try to rollback the transaction. 10174a4c11aaSdan ** 10184a4c11aaSdan ** But it could also be that the user executed one or more BEGIN, 10194a4c11aaSdan ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing 10204a4c11aaSdan ** this method's logic. Not clear how this would be best handled. 10214a4c11aaSdan */ 10224a4c11aaSdan if( rc!=TCL_ERROR ){ 10234a4c11aaSdan Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); 10244a4c11aaSdan rc = TCL_ERROR; 10254a4c11aaSdan } 10264a4c11aaSdan sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0); 10274a4c11aaSdan } 10284a4c11aaSdan pDb->disableAuth--; 10294a4c11aaSdan 10304a4c11aaSdan return rc; 10314a4c11aaSdan } 10324a4c11aaSdan 10334a4c11aaSdan /* 10344a4c11aaSdan ** Search the cache for a prepared-statement object that implements the 10354a4c11aaSdan ** first SQL statement in the buffer pointed to by parameter zIn. If 10364a4c11aaSdan ** no such prepared-statement can be found, allocate and prepare a new 10374a4c11aaSdan ** one. In either case, bind the current values of the relevant Tcl 10384a4c11aaSdan ** variables to any $var, :var or @var variables in the statement. Before 10394a4c11aaSdan ** returning, set *ppPreStmt to point to the prepared-statement object. 10404a4c11aaSdan ** 10414a4c11aaSdan ** Output parameter *pzOut is set to point to the next SQL statement in 10424a4c11aaSdan ** buffer zIn, or to the '\0' byte at the end of zIn if there is no 10434a4c11aaSdan ** next statement. 10444a4c11aaSdan ** 10454a4c11aaSdan ** If successful, TCL_OK is returned. Otherwise, TCL_ERROR is returned 10464a4c11aaSdan ** and an error message loaded into interpreter pDb->interp. 10474a4c11aaSdan */ 10484a4c11aaSdan static int dbPrepareAndBind( 10494a4c11aaSdan SqliteDb *pDb, /* Database object */ 10504a4c11aaSdan char const *zIn, /* SQL to compile */ 10514a4c11aaSdan char const **pzOut, /* OUT: Pointer to next SQL statement */ 10524a4c11aaSdan SqlPreparedStmt **ppPreStmt /* OUT: Object used to cache statement */ 10534a4c11aaSdan ){ 10544a4c11aaSdan const char *zSql = zIn; /* Pointer to first SQL statement in zIn */ 10554a4c11aaSdan sqlite3_stmt *pStmt; /* Prepared statement object */ 10564a4c11aaSdan SqlPreparedStmt *pPreStmt; /* Pointer to cached statement */ 10574a4c11aaSdan int nSql; /* Length of zSql in bytes */ 10584a4c11aaSdan int nVar; /* Number of variables in statement */ 10594a4c11aaSdan int iParm = 0; /* Next free entry in apParm */ 10604a4c11aaSdan int i; 10614a4c11aaSdan Tcl_Interp *interp = pDb->interp; 10624a4c11aaSdan 10634a4c11aaSdan *ppPreStmt = 0; 10644a4c11aaSdan 10654a4c11aaSdan /* Trim spaces from the start of zSql and calculate the remaining length. */ 10664a4c11aaSdan while( isspace(zSql[0]) ){ zSql++; } 10674a4c11aaSdan nSql = strlen30(zSql); 10684a4c11aaSdan 10694a4c11aaSdan for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){ 10704a4c11aaSdan int n = pPreStmt->nSql; 10714a4c11aaSdan if( nSql>=n 10724a4c11aaSdan && memcmp(pPreStmt->zSql, zSql, n)==0 10734a4c11aaSdan && (zSql[n]==0 || zSql[n-1]==';') 10744a4c11aaSdan ){ 10754a4c11aaSdan pStmt = pPreStmt->pStmt; 10764a4c11aaSdan *pzOut = &zSql[pPreStmt->nSql]; 10774a4c11aaSdan 10784a4c11aaSdan /* When a prepared statement is found, unlink it from the 10794a4c11aaSdan ** cache list. It will later be added back to the beginning 10804a4c11aaSdan ** of the cache list in order to implement LRU replacement. 10814a4c11aaSdan */ 10824a4c11aaSdan if( pPreStmt->pPrev ){ 10834a4c11aaSdan pPreStmt->pPrev->pNext = pPreStmt->pNext; 10844a4c11aaSdan }else{ 10854a4c11aaSdan pDb->stmtList = pPreStmt->pNext; 10864a4c11aaSdan } 10874a4c11aaSdan if( pPreStmt->pNext ){ 10884a4c11aaSdan pPreStmt->pNext->pPrev = pPreStmt->pPrev; 10894a4c11aaSdan }else{ 10904a4c11aaSdan pDb->stmtLast = pPreStmt->pPrev; 10914a4c11aaSdan } 10924a4c11aaSdan pDb->nStmt--; 10934a4c11aaSdan nVar = sqlite3_bind_parameter_count(pStmt); 10944a4c11aaSdan break; 10954a4c11aaSdan } 10964a4c11aaSdan } 10974a4c11aaSdan 10984a4c11aaSdan /* If no prepared statement was found. Compile the SQL text. Also allocate 10994a4c11aaSdan ** a new SqlPreparedStmt structure. */ 11004a4c11aaSdan if( pPreStmt==0 ){ 11014a4c11aaSdan int nByte; 11024a4c11aaSdan 11034a4c11aaSdan if( SQLITE_OK!=sqlite3_prepare_v2(pDb->db, zSql, -1, &pStmt, pzOut) ){ 11044a4c11aaSdan Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); 11054a4c11aaSdan return TCL_ERROR; 11064a4c11aaSdan } 11074a4c11aaSdan if( pStmt==0 ){ 11084a4c11aaSdan if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ 11094a4c11aaSdan /* A compile-time error in the statement. */ 11104a4c11aaSdan Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); 11114a4c11aaSdan return TCL_ERROR; 11124a4c11aaSdan }else{ 11134a4c11aaSdan /* The statement was a no-op. Continue to the next statement 11144a4c11aaSdan ** in the SQL string. 11154a4c11aaSdan */ 11164a4c11aaSdan return TCL_OK; 11174a4c11aaSdan } 11184a4c11aaSdan } 11194a4c11aaSdan 11204a4c11aaSdan assert( pPreStmt==0 ); 11214a4c11aaSdan nVar = sqlite3_bind_parameter_count(pStmt); 11224a4c11aaSdan nByte = sizeof(SqlPreparedStmt) + nVar*sizeof(Tcl_Obj *); 11234a4c11aaSdan pPreStmt = (SqlPreparedStmt*)Tcl_Alloc(nByte); 11244a4c11aaSdan memset(pPreStmt, 0, nByte); 11254a4c11aaSdan 11264a4c11aaSdan pPreStmt->pStmt = pStmt; 11274a4c11aaSdan pPreStmt->nSql = (*pzOut - zSql); 11284a4c11aaSdan pPreStmt->zSql = sqlite3_sql(pStmt); 11294a4c11aaSdan pPreStmt->apParm = (Tcl_Obj **)&pPreStmt[1]; 11304a4c11aaSdan } 11314a4c11aaSdan assert( pPreStmt ); 11324a4c11aaSdan assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql ); 11334a4c11aaSdan assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) ); 11344a4c11aaSdan 11354a4c11aaSdan /* Bind values to parameters that begin with $ or : */ 11364a4c11aaSdan for(i=1; i<=nVar; i++){ 11374a4c11aaSdan const char *zVar = sqlite3_bind_parameter_name(pStmt, i); 11384a4c11aaSdan if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){ 11394a4c11aaSdan Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); 11404a4c11aaSdan if( pVar ){ 11414a4c11aaSdan int n; 11424a4c11aaSdan u8 *data; 11434a4c11aaSdan const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 11444a4c11aaSdan char c = zType[0]; 11454a4c11aaSdan if( zVar[0]=='@' || 11464a4c11aaSdan (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){ 11474a4c11aaSdan /* Load a BLOB type if the Tcl variable is a bytearray and 11484a4c11aaSdan ** it has no string representation or the host 11494a4c11aaSdan ** parameter name begins with "@". */ 11504a4c11aaSdan data = Tcl_GetByteArrayFromObj(pVar, &n); 11514a4c11aaSdan sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); 11524a4c11aaSdan Tcl_IncrRefCount(pVar); 11534a4c11aaSdan pPreStmt->apParm[iParm++] = pVar; 11544a4c11aaSdan }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 11554a4c11aaSdan Tcl_GetIntFromObj(interp, pVar, &n); 11564a4c11aaSdan sqlite3_bind_int(pStmt, i, n); 11574a4c11aaSdan }else if( c=='d' && strcmp(zType,"double")==0 ){ 11584a4c11aaSdan double r; 11594a4c11aaSdan Tcl_GetDoubleFromObj(interp, pVar, &r); 11604a4c11aaSdan sqlite3_bind_double(pStmt, i, r); 11614a4c11aaSdan }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 11624a4c11aaSdan (c=='i' && strcmp(zType,"int")==0) ){ 11634a4c11aaSdan Tcl_WideInt v; 11644a4c11aaSdan Tcl_GetWideIntFromObj(interp, pVar, &v); 11654a4c11aaSdan sqlite3_bind_int64(pStmt, i, v); 11664a4c11aaSdan }else{ 11674a4c11aaSdan data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 11684a4c11aaSdan sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); 11694a4c11aaSdan Tcl_IncrRefCount(pVar); 11704a4c11aaSdan pPreStmt->apParm[iParm++] = pVar; 11714a4c11aaSdan } 11724a4c11aaSdan }else{ 11734a4c11aaSdan sqlite3_bind_null(pStmt, i); 11744a4c11aaSdan } 11754a4c11aaSdan } 11764a4c11aaSdan } 11774a4c11aaSdan pPreStmt->nParm = iParm; 11784a4c11aaSdan *ppPreStmt = pPreStmt; 1179937d0deaSdan 11804a4c11aaSdan return TCL_OK; 11814a4c11aaSdan } 11824a4c11aaSdan 11834a4c11aaSdan 11844a4c11aaSdan /* 11854a4c11aaSdan ** Release a statement reference obtained by calling dbPrepareAndBind(). 11864a4c11aaSdan ** There should be exactly one call to this function for each call to 11874a4c11aaSdan ** dbPrepareAndBind(). 11884a4c11aaSdan ** 11894a4c11aaSdan ** If the discard parameter is non-zero, then the statement is deleted 11904a4c11aaSdan ** immediately. Otherwise it is added to the LRU list and may be returned 11914a4c11aaSdan ** by a subsequent call to dbPrepareAndBind(). 11924a4c11aaSdan */ 11934a4c11aaSdan static void dbReleaseStmt( 11944a4c11aaSdan SqliteDb *pDb, /* Database handle */ 11954a4c11aaSdan SqlPreparedStmt *pPreStmt, /* Prepared statement handle to release */ 11964a4c11aaSdan int discard /* True to delete (not cache) the pPreStmt */ 11974a4c11aaSdan ){ 11984a4c11aaSdan int i; 11994a4c11aaSdan 12004a4c11aaSdan /* Free the bound string and blob parameters */ 12014a4c11aaSdan for(i=0; i<pPreStmt->nParm; i++){ 12024a4c11aaSdan Tcl_DecrRefCount(pPreStmt->apParm[i]); 12034a4c11aaSdan } 12044a4c11aaSdan pPreStmt->nParm = 0; 12054a4c11aaSdan 12064a4c11aaSdan if( pDb->maxStmt<=0 || discard ){ 12074a4c11aaSdan /* If the cache is turned off, deallocated the statement */ 12084a4c11aaSdan sqlite3_finalize(pPreStmt->pStmt); 12094a4c11aaSdan Tcl_Free((char *)pPreStmt); 12104a4c11aaSdan }else{ 12114a4c11aaSdan /* Add the prepared statement to the beginning of the cache list. */ 12124a4c11aaSdan pPreStmt->pNext = pDb->stmtList; 12134a4c11aaSdan pPreStmt->pPrev = 0; 12144a4c11aaSdan if( pDb->stmtList ){ 12154a4c11aaSdan pDb->stmtList->pPrev = pPreStmt; 12164a4c11aaSdan } 12174a4c11aaSdan pDb->stmtList = pPreStmt; 12184a4c11aaSdan if( pDb->stmtLast==0 ){ 12194a4c11aaSdan assert( pDb->nStmt==0 ); 12204a4c11aaSdan pDb->stmtLast = pPreStmt; 12214a4c11aaSdan }else{ 12224a4c11aaSdan assert( pDb->nStmt>0 ); 12234a4c11aaSdan } 12244a4c11aaSdan pDb->nStmt++; 12254a4c11aaSdan 12264a4c11aaSdan /* If we have too many statement in cache, remove the surplus from 12274a4c11aaSdan ** the end of the cache list. */ 12284a4c11aaSdan while( pDb->nStmt>pDb->maxStmt ){ 12294a4c11aaSdan sqlite3_finalize(pDb->stmtLast->pStmt); 12304a4c11aaSdan pDb->stmtLast = pDb->stmtLast->pPrev; 12314a4c11aaSdan Tcl_Free((char*)pDb->stmtLast->pNext); 12324a4c11aaSdan pDb->stmtLast->pNext = 0; 12334a4c11aaSdan pDb->nStmt--; 12344a4c11aaSdan } 12354a4c11aaSdan } 12364a4c11aaSdan } 12374a4c11aaSdan 12384a4c11aaSdan /* 12394a4c11aaSdan ** Structure used with dbEvalXXX() functions: 12404a4c11aaSdan ** 12414a4c11aaSdan ** dbEvalInit() 12424a4c11aaSdan ** dbEvalStep() 12434a4c11aaSdan ** dbEvalFinalize() 12444a4c11aaSdan ** dbEvalRowInfo() 12454a4c11aaSdan ** dbEvalColumnValue() 12464a4c11aaSdan */ 12474a4c11aaSdan typedef struct DbEvalContext DbEvalContext; 12484a4c11aaSdan struct DbEvalContext { 12494a4c11aaSdan SqliteDb *pDb; /* Database handle */ 12504a4c11aaSdan Tcl_Obj *pSql; /* Object holding string zSql */ 12514a4c11aaSdan const char *zSql; /* Remaining SQL to execute */ 12524a4c11aaSdan SqlPreparedStmt *pPreStmt; /* Current statement */ 12534a4c11aaSdan int nCol; /* Number of columns returned by pStmt */ 12544a4c11aaSdan Tcl_Obj *pArray; /* Name of array variable */ 12554a4c11aaSdan Tcl_Obj **apColName; /* Array of column names */ 12564a4c11aaSdan }; 12574a4c11aaSdan 12584a4c11aaSdan /* 12594a4c11aaSdan ** Release any cache of column names currently held as part of 12604a4c11aaSdan ** the DbEvalContext structure passed as the first argument. 12614a4c11aaSdan */ 12624a4c11aaSdan static void dbReleaseColumnNames(DbEvalContext *p){ 12634a4c11aaSdan if( p->apColName ){ 12644a4c11aaSdan int i; 12654a4c11aaSdan for(i=0; i<p->nCol; i++){ 12664a4c11aaSdan Tcl_DecrRefCount(p->apColName[i]); 12674a4c11aaSdan } 12684a4c11aaSdan Tcl_Free((char *)p->apColName); 12694a4c11aaSdan p->apColName = 0; 12704a4c11aaSdan } 12714a4c11aaSdan p->nCol = 0; 12724a4c11aaSdan } 12734a4c11aaSdan 12744a4c11aaSdan /* 12754a4c11aaSdan ** Initialize a DbEvalContext structure. 12768e556520Sdanielk1977 ** 12778e556520Sdanielk1977 ** If pArray is not NULL, then it contains the name of a Tcl array 12788e556520Sdanielk1977 ** variable. The "*" member of this array is set to a list containing 12794a4c11aaSdan ** the names of the columns returned by the statement as part of each 12804a4c11aaSdan ** call to dbEvalStep(), in order from left to right. e.g. if the names 12814a4c11aaSdan ** of the returned columns are a, b and c, it does the equivalent of the 12824a4c11aaSdan ** tcl command: 12838e556520Sdanielk1977 ** 12848e556520Sdanielk1977 ** set ${pArray}(*) {a b c} 12858e556520Sdanielk1977 */ 12864a4c11aaSdan static void dbEvalInit( 12874a4c11aaSdan DbEvalContext *p, /* Pointer to structure to initialize */ 12884a4c11aaSdan SqliteDb *pDb, /* Database handle */ 12894a4c11aaSdan Tcl_Obj *pSql, /* Object containing SQL script */ 12904a4c11aaSdan Tcl_Obj *pArray /* Name of Tcl array to set (*) element of */ 12918e556520Sdanielk1977 ){ 12924a4c11aaSdan memset(p, 0, sizeof(DbEvalContext)); 12934a4c11aaSdan p->pDb = pDb; 12944a4c11aaSdan p->zSql = Tcl_GetString(pSql); 12954a4c11aaSdan p->pSql = pSql; 12964a4c11aaSdan Tcl_IncrRefCount(pSql); 12974a4c11aaSdan if( pArray ){ 12984a4c11aaSdan p->pArray = pArray; 12994a4c11aaSdan Tcl_IncrRefCount(pArray); 13004a4c11aaSdan } 13014a4c11aaSdan } 13028e556520Sdanielk1977 13034a4c11aaSdan /* 13044a4c11aaSdan ** Obtain information about the row that the DbEvalContext passed as the 13054a4c11aaSdan ** first argument currently points to. 13064a4c11aaSdan */ 13074a4c11aaSdan static void dbEvalRowInfo( 13084a4c11aaSdan DbEvalContext *p, /* Evaluation context */ 13094a4c11aaSdan int *pnCol, /* OUT: Number of column names */ 13104a4c11aaSdan Tcl_Obj ***papColName /* OUT: Array of column names */ 13114a4c11aaSdan ){ 13128e556520Sdanielk1977 /* Compute column names */ 13134a4c11aaSdan if( 0==p->apColName ){ 13144a4c11aaSdan sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 13154a4c11aaSdan int i; /* Iterator variable */ 13164a4c11aaSdan int nCol; /* Number of columns returned by pStmt */ 13174a4c11aaSdan Tcl_Obj **apColName = 0; /* Array of column names */ 13184a4c11aaSdan 13194a4c11aaSdan p->nCol = nCol = sqlite3_column_count(pStmt); 13204a4c11aaSdan if( nCol>0 && (papColName || p->pArray) ){ 13214a4c11aaSdan apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); 13228e556520Sdanielk1977 for(i=0; i<nCol; i++){ 13238e556520Sdanielk1977 apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i)); 13248e556520Sdanielk1977 Tcl_IncrRefCount(apColName[i]); 13258e556520Sdanielk1977 } 13264a4c11aaSdan p->apColName = apColName; 13274a4c11aaSdan } 13288e556520Sdanielk1977 13298e556520Sdanielk1977 /* If results are being stored in an array variable, then create 13308e556520Sdanielk1977 ** the array(*) entry for that array 13318e556520Sdanielk1977 */ 13324a4c11aaSdan if( p->pArray ){ 13334a4c11aaSdan Tcl_Interp *interp = p->pDb->interp; 13348e556520Sdanielk1977 Tcl_Obj *pColList = Tcl_NewObj(); 13358e556520Sdanielk1977 Tcl_Obj *pStar = Tcl_NewStringObj("*", -1); 13364a4c11aaSdan 13378e556520Sdanielk1977 for(i=0; i<nCol; i++){ 13388e556520Sdanielk1977 Tcl_ListObjAppendElement(interp, pColList, apColName[i]); 13398e556520Sdanielk1977 } 13408e556520Sdanielk1977 Tcl_IncrRefCount(pStar); 13414a4c11aaSdan Tcl_ObjSetVar2(interp, p->pArray, pStar, pColList, 0); 13428e556520Sdanielk1977 Tcl_DecrRefCount(pStar); 13438e556520Sdanielk1977 } 13448e556520Sdanielk1977 } 13458e556520Sdanielk1977 13464a4c11aaSdan if( papColName ){ 13474a4c11aaSdan *papColName = p->apColName; 13484a4c11aaSdan } 13494a4c11aaSdan if( pnCol ){ 13504a4c11aaSdan *pnCol = p->nCol; 13514a4c11aaSdan } 13524a4c11aaSdan } 13534a4c11aaSdan 13544a4c11aaSdan /* 13554a4c11aaSdan ** Return one of TCL_OK, TCL_BREAK or TCL_ERROR. If TCL_ERROR is 13564a4c11aaSdan ** returned, then an error message is stored in the interpreter before 13574a4c11aaSdan ** returning. 13584a4c11aaSdan ** 13594a4c11aaSdan ** A return value of TCL_OK means there is a row of data available. The 13604a4c11aaSdan ** data may be accessed using dbEvalRowInfo() and dbEvalColumnValue(). This 13614a4c11aaSdan ** is analogous to a return of SQLITE_ROW from sqlite3_step(). If TCL_BREAK 13624a4c11aaSdan ** is returned, then the SQL script has finished executing and there are 13634a4c11aaSdan ** no further rows available. This is similar to SQLITE_DONE. 13644a4c11aaSdan */ 13654a4c11aaSdan static int dbEvalStep(DbEvalContext *p){ 13664a4c11aaSdan while( p->zSql[0] || p->pPreStmt ){ 13674a4c11aaSdan int rc; 13684a4c11aaSdan if( p->pPreStmt==0 ){ 13694a4c11aaSdan rc = dbPrepareAndBind(p->pDb, p->zSql, &p->zSql, &p->pPreStmt); 13704a4c11aaSdan if( rc!=TCL_OK ) return rc; 13714a4c11aaSdan }else{ 13724a4c11aaSdan int rcs; 13734a4c11aaSdan SqliteDb *pDb = p->pDb; 13744a4c11aaSdan SqlPreparedStmt *pPreStmt = p->pPreStmt; 13754a4c11aaSdan sqlite3_stmt *pStmt = pPreStmt->pStmt; 13764a4c11aaSdan 13774a4c11aaSdan rcs = sqlite3_step(pStmt); 13784a4c11aaSdan if( rcs==SQLITE_ROW ){ 13794a4c11aaSdan return TCL_OK; 13804a4c11aaSdan } 13814a4c11aaSdan if( p->pArray ){ 13824a4c11aaSdan dbEvalRowInfo(p, 0, 0); 13834a4c11aaSdan } 13844a4c11aaSdan rcs = sqlite3_reset(pStmt); 13854a4c11aaSdan 13864a4c11aaSdan pDb->nStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_FULLSCAN_STEP,1); 13874a4c11aaSdan pDb->nSort = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_SORT,1); 13883c379b01Sdrh pDb->nIndex = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_AUTOINDEX,1); 13894a4c11aaSdan dbReleaseColumnNames(p); 13904a4c11aaSdan p->pPreStmt = 0; 13914a4c11aaSdan 13924a4c11aaSdan if( rcs!=SQLITE_OK ){ 13934a4c11aaSdan /* If a run-time error occurs, report the error and stop reading 13944a4c11aaSdan ** the SQL. */ 13954a4c11aaSdan Tcl_SetObjResult(pDb->interp, dbTextToObj(sqlite3_errmsg(pDb->db))); 13964a4c11aaSdan dbReleaseStmt(pDb, pPreStmt, 1); 13974a4c11aaSdan return TCL_ERROR; 13984a4c11aaSdan }else{ 13994a4c11aaSdan dbReleaseStmt(pDb, pPreStmt, 0); 14004a4c11aaSdan } 14014a4c11aaSdan } 14024a4c11aaSdan } 14034a4c11aaSdan 14044a4c11aaSdan /* Finished */ 14054a4c11aaSdan return TCL_BREAK; 14064a4c11aaSdan } 14074a4c11aaSdan 14084a4c11aaSdan /* 14094a4c11aaSdan ** Free all resources currently held by the DbEvalContext structure passed 14104a4c11aaSdan ** as the first argument. There should be exactly one call to this function 14114a4c11aaSdan ** for each call to dbEvalInit(). 14124a4c11aaSdan */ 14134a4c11aaSdan static void dbEvalFinalize(DbEvalContext *p){ 14144a4c11aaSdan if( p->pPreStmt ){ 14154a4c11aaSdan sqlite3_reset(p->pPreStmt->pStmt); 14164a4c11aaSdan dbReleaseStmt(p->pDb, p->pPreStmt, 0); 14174a4c11aaSdan p->pPreStmt = 0; 14184a4c11aaSdan } 14194a4c11aaSdan if( p->pArray ){ 14204a4c11aaSdan Tcl_DecrRefCount(p->pArray); 14214a4c11aaSdan p->pArray = 0; 14224a4c11aaSdan } 14234a4c11aaSdan Tcl_DecrRefCount(p->pSql); 14244a4c11aaSdan dbReleaseColumnNames(p); 14254a4c11aaSdan } 14264a4c11aaSdan 14274a4c11aaSdan /* 14284a4c11aaSdan ** Return a pointer to a Tcl_Obj structure with ref-count 0 that contains 14294a4c11aaSdan ** the value for the iCol'th column of the row currently pointed to by 14304a4c11aaSdan ** the DbEvalContext structure passed as the first argument. 14314a4c11aaSdan */ 14324a4c11aaSdan static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){ 14334a4c11aaSdan sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 14344a4c11aaSdan switch( sqlite3_column_type(pStmt, iCol) ){ 14354a4c11aaSdan case SQLITE_BLOB: { 14364a4c11aaSdan int bytes = sqlite3_column_bytes(pStmt, iCol); 14374a4c11aaSdan const char *zBlob = sqlite3_column_blob(pStmt, iCol); 14384a4c11aaSdan if( !zBlob ) bytes = 0; 14394a4c11aaSdan return Tcl_NewByteArrayObj((u8*)zBlob, bytes); 14404a4c11aaSdan } 14414a4c11aaSdan case SQLITE_INTEGER: { 14424a4c11aaSdan sqlite_int64 v = sqlite3_column_int64(pStmt, iCol); 14434a4c11aaSdan if( v>=-2147483647 && v<=2147483647 ){ 14444a4c11aaSdan return Tcl_NewIntObj(v); 14454a4c11aaSdan }else{ 14464a4c11aaSdan return Tcl_NewWideIntObj(v); 14474a4c11aaSdan } 14484a4c11aaSdan } 14494a4c11aaSdan case SQLITE_FLOAT: { 14504a4c11aaSdan return Tcl_NewDoubleObj(sqlite3_column_double(pStmt, iCol)); 14514a4c11aaSdan } 14524a4c11aaSdan case SQLITE_NULL: { 14534a4c11aaSdan return dbTextToObj(p->pDb->zNull); 14544a4c11aaSdan } 14554a4c11aaSdan } 14564a4c11aaSdan 14574a4c11aaSdan return dbTextToObj((char *)sqlite3_column_text(pStmt, iCol)); 14584a4c11aaSdan } 14594a4c11aaSdan 14604a4c11aaSdan /* 14614a4c11aaSdan ** If using Tcl version 8.6 or greater, use the NR functions to avoid 14624a4c11aaSdan ** recursive evalution of scripts by the [db eval] and [db trans] 14634a4c11aaSdan ** commands. Even if the headers used while compiling the extension 14644a4c11aaSdan ** are 8.6 or newer, the code still tests the Tcl version at runtime. 14654a4c11aaSdan ** This allows stubs-enabled builds to be used with older Tcl libraries. 14664a4c11aaSdan */ 14674a4c11aaSdan #if TCL_MAJOR_VERSION>8 || (TCL_MAJOR_VERSION==8 && TCL_MINOR_VERSION>=6) 1468a2c8a95bSdrh # define SQLITE_TCL_NRE 1 14694a4c11aaSdan static int DbUseNre(void){ 14704a4c11aaSdan int major, minor; 14714a4c11aaSdan Tcl_GetVersion(&major, &minor, 0, 0); 14724a4c11aaSdan return( (major==8 && minor>=6) || major>8 ); 14734a4c11aaSdan } 14744a4c11aaSdan #else 14754a4c11aaSdan /* 14764a4c11aaSdan ** Compiling using headers earlier than 8.6. In this case NR cannot be 14774a4c11aaSdan ** used, so DbUseNre() to always return zero. Add #defines for the other 14784a4c11aaSdan ** Tcl_NRxxx() functions to prevent them from causing compilation errors, 14794a4c11aaSdan ** even though the only invocations of them are within conditional blocks 14804a4c11aaSdan ** of the form: 14814a4c11aaSdan ** 14824a4c11aaSdan ** if( DbUseNre() ) { ... } 14834a4c11aaSdan */ 1484a2c8a95bSdrh # define SQLITE_TCL_NRE 0 14854a4c11aaSdan # define DbUseNre() 0 14864a4c11aaSdan # define Tcl_NRAddCallback(a,b,c,d,e,f) 0 14874a4c11aaSdan # define Tcl_NREvalObj(a,b,c) 0 14884a4c11aaSdan # define Tcl_NRCreateCommand(a,b,c,d,e,f) 0 14894a4c11aaSdan #endif 14904a4c11aaSdan 14914a4c11aaSdan /* 14924a4c11aaSdan ** This function is part of the implementation of the command: 14934a4c11aaSdan ** 14944a4c11aaSdan ** $db eval SQL ?ARRAYNAME? SCRIPT 14954a4c11aaSdan */ 14964a4c11aaSdan static int DbEvalNextCmd( 14974a4c11aaSdan ClientData data[], /* data[0] is the (DbEvalContext*) */ 14984a4c11aaSdan Tcl_Interp *interp, /* Tcl interpreter */ 14994a4c11aaSdan int result /* Result so far */ 15004a4c11aaSdan ){ 15014a4c11aaSdan int rc = result; /* Return code */ 15024a4c11aaSdan 15034a4c11aaSdan /* The first element of the data[] array is a pointer to a DbEvalContext 15044a4c11aaSdan ** structure allocated using Tcl_Alloc(). The second element of data[] 15054a4c11aaSdan ** is a pointer to a Tcl_Obj containing the script to run for each row 15064a4c11aaSdan ** returned by the queries encapsulated in data[0]. */ 15074a4c11aaSdan DbEvalContext *p = (DbEvalContext *)data[0]; 15084a4c11aaSdan Tcl_Obj *pScript = (Tcl_Obj *)data[1]; 15094a4c11aaSdan Tcl_Obj *pArray = p->pArray; 15104a4c11aaSdan 15114a4c11aaSdan while( (rc==TCL_OK || rc==TCL_CONTINUE) && TCL_OK==(rc = dbEvalStep(p)) ){ 15124a4c11aaSdan int i; 15134a4c11aaSdan int nCol; 15144a4c11aaSdan Tcl_Obj **apColName; 15154a4c11aaSdan dbEvalRowInfo(p, &nCol, &apColName); 15164a4c11aaSdan for(i=0; i<nCol; i++){ 15174a4c11aaSdan Tcl_Obj *pVal = dbEvalColumnValue(p, i); 15184a4c11aaSdan if( pArray==0 ){ 15194a4c11aaSdan Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0); 15204a4c11aaSdan }else{ 15214a4c11aaSdan Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); 15224a4c11aaSdan } 15234a4c11aaSdan } 15244a4c11aaSdan 15254a4c11aaSdan /* The required interpreter variables are now populated with the data 15264a4c11aaSdan ** from the current row. If using NRE, schedule callbacks to evaluate 15274a4c11aaSdan ** script pScript, then to invoke this function again to fetch the next 15284a4c11aaSdan ** row (or clean up if there is no next row or the script throws an 15294a4c11aaSdan ** exception). After scheduling the callbacks, return control to the 15304a4c11aaSdan ** caller. 15314a4c11aaSdan ** 15324a4c11aaSdan ** If not using NRE, evaluate pScript directly and continue with the 15334a4c11aaSdan ** next iteration of this while(...) loop. */ 15344a4c11aaSdan if( DbUseNre() ){ 15354a4c11aaSdan Tcl_NRAddCallback(interp, DbEvalNextCmd, (void*)p, (void*)pScript, 0, 0); 15364a4c11aaSdan return Tcl_NREvalObj(interp, pScript, 0); 15374a4c11aaSdan }else{ 15384a4c11aaSdan rc = Tcl_EvalObjEx(interp, pScript, 0); 15394a4c11aaSdan } 15404a4c11aaSdan } 15414a4c11aaSdan 15424a4c11aaSdan Tcl_DecrRefCount(pScript); 15434a4c11aaSdan dbEvalFinalize(p); 15444a4c11aaSdan Tcl_Free((char *)p); 15454a4c11aaSdan 15464a4c11aaSdan if( rc==TCL_OK || rc==TCL_BREAK ){ 15474a4c11aaSdan Tcl_ResetResult(interp); 15484a4c11aaSdan rc = TCL_OK; 15494a4c11aaSdan } 15504a4c11aaSdan return rc; 15518e556520Sdanielk1977 } 15528e556520Sdanielk1977 15531067fe11Stpoindex /* 155475897234Sdrh ** The "sqlite" command below creates a new Tcl command for each 155575897234Sdrh ** connection it opens to an SQLite database. This routine is invoked 155675897234Sdrh ** whenever one of those connection-specific commands is executed 155775897234Sdrh ** in Tcl. For example, if you run Tcl code like this: 155875897234Sdrh ** 15599bb575fdSdrh ** sqlite3 db1 "my_database" 156075897234Sdrh ** db1 close 156175897234Sdrh ** 156275897234Sdrh ** The first command opens a connection to the "my_database" database 156375897234Sdrh ** and calls that connection "db1". The second command causes this 156475897234Sdrh ** subroutine to be invoked. 156575897234Sdrh */ 15666d31316cSdrh static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ 1567bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 15686d31316cSdrh int choice; 156922fbcb8dSdrh int rc = TCL_OK; 15700de8c112Sdrh static const char *DB_strs[] = { 1571dc2c4915Sdrh "authorizer", "backup", "busy", 1572dc2c4915Sdrh "cache", "changes", "close", 1573dc2c4915Sdrh "collate", "collation_needed", "commit_hook", 1574dc2c4915Sdrh "complete", "copy", "enable_load_extension", 1575dc2c4915Sdrh "errorcode", "eval", "exists", 1576dc2c4915Sdrh "function", "incrblob", "interrupt", 1577833bf968Sdrh "last_insert_rowid", "nullvalue", "onecolumn", 1578833bf968Sdrh "profile", "progress", "rekey", 1579833bf968Sdrh "restore", "rollback_hook", "status", 1580833bf968Sdrh "timeout", "total_changes", "trace", 1581833bf968Sdrh "transaction", "unlock_notify", "update_hook", 1582833bf968Sdrh "version", "wal_hook", 0 15836d31316cSdrh }; 1584411995dcSdrh enum DB_enum { 1585dc2c4915Sdrh DB_AUTHORIZER, DB_BACKUP, DB_BUSY, 1586dc2c4915Sdrh DB_CACHE, DB_CHANGES, DB_CLOSE, 1587dc2c4915Sdrh DB_COLLATE, DB_COLLATION_NEEDED, DB_COMMIT_HOOK, 1588dc2c4915Sdrh DB_COMPLETE, DB_COPY, DB_ENABLE_LOAD_EXTENSION, 1589dc2c4915Sdrh DB_ERRORCODE, DB_EVAL, DB_EXISTS, 1590dc2c4915Sdrh DB_FUNCTION, DB_INCRBLOB, DB_INTERRUPT, 1591833bf968Sdrh DB_LAST_INSERT_ROWID, DB_NULLVALUE, DB_ONECOLUMN, 1592833bf968Sdrh DB_PROFILE, DB_PROGRESS, DB_REKEY, 1593833bf968Sdrh DB_RESTORE, DB_ROLLBACK_HOOK, DB_STATUS, 1594833bf968Sdrh DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE, 1595833bf968Sdrh DB_TRANSACTION, DB_UNLOCK_NOTIFY, DB_UPDATE_HOOK, 1596833bf968Sdrh DB_VERSION, DB_WAL_HOOK 15976d31316cSdrh }; 15981067fe11Stpoindex /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */ 15996d31316cSdrh 16006d31316cSdrh if( objc<2 ){ 16016d31316cSdrh Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); 160275897234Sdrh return TCL_ERROR; 160375897234Sdrh } 1604411995dcSdrh if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ 16056d31316cSdrh return TCL_ERROR; 16066d31316cSdrh } 16076d31316cSdrh 1608411995dcSdrh switch( (enum DB_enum)choice ){ 160975897234Sdrh 1610e22a334bSdrh /* $db authorizer ?CALLBACK? 1611e22a334bSdrh ** 1612e22a334bSdrh ** Invoke the given callback to authorize each SQL operation as it is 1613e22a334bSdrh ** compiled. 5 arguments are appended to the callback before it is 1614e22a334bSdrh ** invoked: 1615e22a334bSdrh ** 1616e22a334bSdrh ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) 1617e22a334bSdrh ** (2) First descriptive name (depends on authorization type) 1618e22a334bSdrh ** (3) Second descriptive name 1619e22a334bSdrh ** (4) Name of the database (ex: "main", "temp") 1620e22a334bSdrh ** (5) Name of trigger that is doing the access 1621e22a334bSdrh ** 1622e22a334bSdrh ** The callback should return on of the following strings: SQLITE_OK, 1623e22a334bSdrh ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. 1624e22a334bSdrh ** 1625e22a334bSdrh ** If this method is invoked with no arguments, the current authorization 1626e22a334bSdrh ** callback string is returned. 1627e22a334bSdrh */ 1628e22a334bSdrh case DB_AUTHORIZER: { 16291211de37Sdrh #ifdef SQLITE_OMIT_AUTHORIZATION 16301211de37Sdrh Tcl_AppendResult(interp, "authorization not available in this build", 0); 16311211de37Sdrh return TCL_ERROR; 16321211de37Sdrh #else 1633e22a334bSdrh if( objc>3 ){ 1634e22a334bSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 16350f14e2ebSdrh return TCL_ERROR; 1636e22a334bSdrh }else if( objc==2 ){ 1637b5a20d3cSdrh if( pDb->zAuth ){ 1638e22a334bSdrh Tcl_AppendResult(interp, pDb->zAuth, 0); 1639e22a334bSdrh } 1640e22a334bSdrh }else{ 1641e22a334bSdrh char *zAuth; 1642e22a334bSdrh int len; 1643e22a334bSdrh if( pDb->zAuth ){ 1644e22a334bSdrh Tcl_Free(pDb->zAuth); 1645e22a334bSdrh } 1646e22a334bSdrh zAuth = Tcl_GetStringFromObj(objv[2], &len); 1647e22a334bSdrh if( zAuth && len>0 ){ 1648e22a334bSdrh pDb->zAuth = Tcl_Alloc( len + 1 ); 16495bb3eb9bSdrh memcpy(pDb->zAuth, zAuth, len+1); 1650e22a334bSdrh }else{ 1651e22a334bSdrh pDb->zAuth = 0; 1652e22a334bSdrh } 1653e22a334bSdrh if( pDb->zAuth ){ 1654e22a334bSdrh pDb->interp = interp; 16556f8a503dSdanielk1977 sqlite3_set_authorizer(pDb->db, auth_callback, pDb); 1656e22a334bSdrh }else{ 16576f8a503dSdanielk1977 sqlite3_set_authorizer(pDb->db, 0, 0); 1658e22a334bSdrh } 1659e22a334bSdrh } 16601211de37Sdrh #endif 1661e22a334bSdrh break; 1662e22a334bSdrh } 1663e22a334bSdrh 1664dc2c4915Sdrh /* $db backup ?DATABASE? FILENAME 1665dc2c4915Sdrh ** 1666dc2c4915Sdrh ** Open or create a database file named FILENAME. Transfer the 1667dc2c4915Sdrh ** content of local database DATABASE (default: "main") into the 1668dc2c4915Sdrh ** FILENAME database. 1669dc2c4915Sdrh */ 1670dc2c4915Sdrh case DB_BACKUP: { 1671dc2c4915Sdrh const char *zDestFile; 1672dc2c4915Sdrh const char *zSrcDb; 1673dc2c4915Sdrh sqlite3 *pDest; 1674dc2c4915Sdrh sqlite3_backup *pBackup; 1675dc2c4915Sdrh 1676dc2c4915Sdrh if( objc==3 ){ 1677dc2c4915Sdrh zSrcDb = "main"; 1678dc2c4915Sdrh zDestFile = Tcl_GetString(objv[2]); 1679dc2c4915Sdrh }else if( objc==4 ){ 1680dc2c4915Sdrh zSrcDb = Tcl_GetString(objv[2]); 1681dc2c4915Sdrh zDestFile = Tcl_GetString(objv[3]); 1682dc2c4915Sdrh }else{ 1683dc2c4915Sdrh Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 1684dc2c4915Sdrh return TCL_ERROR; 1685dc2c4915Sdrh } 1686dc2c4915Sdrh rc = sqlite3_open(zDestFile, &pDest); 1687dc2c4915Sdrh if( rc!=SQLITE_OK ){ 1688dc2c4915Sdrh Tcl_AppendResult(interp, "cannot open target database: ", 1689dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1690dc2c4915Sdrh sqlite3_close(pDest); 1691dc2c4915Sdrh return TCL_ERROR; 1692dc2c4915Sdrh } 1693dc2c4915Sdrh pBackup = sqlite3_backup_init(pDest, "main", pDb->db, zSrcDb); 1694dc2c4915Sdrh if( pBackup==0 ){ 1695dc2c4915Sdrh Tcl_AppendResult(interp, "backup failed: ", 1696dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1697dc2c4915Sdrh sqlite3_close(pDest); 1698dc2c4915Sdrh return TCL_ERROR; 1699dc2c4915Sdrh } 1700dc2c4915Sdrh while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} 1701dc2c4915Sdrh sqlite3_backup_finish(pBackup); 1702dc2c4915Sdrh if( rc==SQLITE_DONE ){ 1703dc2c4915Sdrh rc = TCL_OK; 1704dc2c4915Sdrh }else{ 1705dc2c4915Sdrh Tcl_AppendResult(interp, "backup failed: ", 1706dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1707dc2c4915Sdrh rc = TCL_ERROR; 1708dc2c4915Sdrh } 1709dc2c4915Sdrh sqlite3_close(pDest); 1710dc2c4915Sdrh break; 1711dc2c4915Sdrh } 1712dc2c4915Sdrh 1713bec3f402Sdrh /* $db busy ?CALLBACK? 1714bec3f402Sdrh ** 1715bec3f402Sdrh ** Invoke the given callback if an SQL statement attempts to open 1716bec3f402Sdrh ** a locked database file. 1717bec3f402Sdrh */ 17186d31316cSdrh case DB_BUSY: { 17196d31316cSdrh if( objc>3 ){ 17206d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); 1721bec3f402Sdrh return TCL_ERROR; 17226d31316cSdrh }else if( objc==2 ){ 1723bec3f402Sdrh if( pDb->zBusy ){ 1724bec3f402Sdrh Tcl_AppendResult(interp, pDb->zBusy, 0); 1725bec3f402Sdrh } 1726bec3f402Sdrh }else{ 17276d31316cSdrh char *zBusy; 17286d31316cSdrh int len; 1729bec3f402Sdrh if( pDb->zBusy ){ 1730bec3f402Sdrh Tcl_Free(pDb->zBusy); 17316d31316cSdrh } 17326d31316cSdrh zBusy = Tcl_GetStringFromObj(objv[2], &len); 17336d31316cSdrh if( zBusy && len>0 ){ 17346d31316cSdrh pDb->zBusy = Tcl_Alloc( len + 1 ); 17355bb3eb9bSdrh memcpy(pDb->zBusy, zBusy, len+1); 17366d31316cSdrh }else{ 1737bec3f402Sdrh pDb->zBusy = 0; 1738bec3f402Sdrh } 1739bec3f402Sdrh if( pDb->zBusy ){ 1740bec3f402Sdrh pDb->interp = interp; 17416f8a503dSdanielk1977 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb); 17426d31316cSdrh }else{ 17436f8a503dSdanielk1977 sqlite3_busy_handler(pDb->db, 0, 0); 1744bec3f402Sdrh } 1745bec3f402Sdrh } 17466d31316cSdrh break; 17476d31316cSdrh } 1748bec3f402Sdrh 1749fb7e7651Sdrh /* $db cache flush 1750fb7e7651Sdrh ** $db cache size n 1751fb7e7651Sdrh ** 1752fb7e7651Sdrh ** Flush the prepared statement cache, or set the maximum number of 1753fb7e7651Sdrh ** cached statements. 1754fb7e7651Sdrh */ 1755fb7e7651Sdrh case DB_CACHE: { 1756fb7e7651Sdrh char *subCmd; 1757fb7e7651Sdrh int n; 1758fb7e7651Sdrh 1759fb7e7651Sdrh if( objc<=2 ){ 1760fb7e7651Sdrh Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?"); 1761fb7e7651Sdrh return TCL_ERROR; 1762fb7e7651Sdrh } 1763fb7e7651Sdrh subCmd = Tcl_GetStringFromObj( objv[2], 0 ); 1764fb7e7651Sdrh if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){ 1765fb7e7651Sdrh if( objc!=3 ){ 1766fb7e7651Sdrh Tcl_WrongNumArgs(interp, 2, objv, "flush"); 1767fb7e7651Sdrh return TCL_ERROR; 1768fb7e7651Sdrh }else{ 1769fb7e7651Sdrh flushStmtCache( pDb ); 1770fb7e7651Sdrh } 1771fb7e7651Sdrh }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){ 1772fb7e7651Sdrh if( objc!=4 ){ 1773fb7e7651Sdrh Tcl_WrongNumArgs(interp, 2, objv, "size n"); 1774fb7e7651Sdrh return TCL_ERROR; 1775fb7e7651Sdrh }else{ 1776fb7e7651Sdrh if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){ 1777fb7e7651Sdrh Tcl_AppendResult( interp, "cannot convert \"", 1778fb7e7651Sdrh Tcl_GetStringFromObj(objv[3],0), "\" to integer", 0); 1779fb7e7651Sdrh return TCL_ERROR; 1780fb7e7651Sdrh }else{ 1781fb7e7651Sdrh if( n<0 ){ 1782fb7e7651Sdrh flushStmtCache( pDb ); 1783fb7e7651Sdrh n = 0; 1784fb7e7651Sdrh }else if( n>MAX_PREPARED_STMTS ){ 1785fb7e7651Sdrh n = MAX_PREPARED_STMTS; 1786fb7e7651Sdrh } 1787fb7e7651Sdrh pDb->maxStmt = n; 1788fb7e7651Sdrh } 1789fb7e7651Sdrh } 1790fb7e7651Sdrh }else{ 1791fb7e7651Sdrh Tcl_AppendResult( interp, "bad option \"", 1792191fadcfSdanielk1977 Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size", 0); 1793fb7e7651Sdrh return TCL_ERROR; 1794fb7e7651Sdrh } 1795fb7e7651Sdrh break; 1796fb7e7651Sdrh } 1797fb7e7651Sdrh 1798b28af71aSdanielk1977 /* $db changes 1799c8d30ac1Sdrh ** 1800c8d30ac1Sdrh ** Return the number of rows that were modified, inserted, or deleted by 1801b28af71aSdanielk1977 ** the most recent INSERT, UPDATE or DELETE statement, not including 1802b28af71aSdanielk1977 ** any changes made by trigger programs. 1803c8d30ac1Sdrh */ 1804c8d30ac1Sdrh case DB_CHANGES: { 1805c8d30ac1Sdrh Tcl_Obj *pResult; 1806c8d30ac1Sdrh if( objc!=2 ){ 1807c8d30ac1Sdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 1808c8d30ac1Sdrh return TCL_ERROR; 1809c8d30ac1Sdrh } 1810c8d30ac1Sdrh pResult = Tcl_GetObjResult(interp); 1811b28af71aSdanielk1977 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db)); 1812f146a776Srdc break; 1813f146a776Srdc } 1814f146a776Srdc 181575897234Sdrh /* $db close 181675897234Sdrh ** 181775897234Sdrh ** Shutdown the database 181875897234Sdrh */ 18196d31316cSdrh case DB_CLOSE: { 18206d31316cSdrh Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); 18216d31316cSdrh break; 18226d31316cSdrh } 182375897234Sdrh 18240f14e2ebSdrh /* 18250f14e2ebSdrh ** $db collate NAME SCRIPT 18260f14e2ebSdrh ** 18270f14e2ebSdrh ** Create a new SQL collation function called NAME. Whenever 18280f14e2ebSdrh ** that function is called, invoke SCRIPT to evaluate the function. 18290f14e2ebSdrh */ 18300f14e2ebSdrh case DB_COLLATE: { 18310f14e2ebSdrh SqlCollate *pCollate; 18320f14e2ebSdrh char *zName; 18330f14e2ebSdrh char *zScript; 18340f14e2ebSdrh int nScript; 18350f14e2ebSdrh if( objc!=4 ){ 18360f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); 18370f14e2ebSdrh return TCL_ERROR; 18380f14e2ebSdrh } 18390f14e2ebSdrh zName = Tcl_GetStringFromObj(objv[2], 0); 18400f14e2ebSdrh zScript = Tcl_GetStringFromObj(objv[3], &nScript); 18410f14e2ebSdrh pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 ); 18420f14e2ebSdrh if( pCollate==0 ) return TCL_ERROR; 18430f14e2ebSdrh pCollate->interp = interp; 18440f14e2ebSdrh pCollate->pNext = pDb->pCollate; 18450f14e2ebSdrh pCollate->zScript = (char*)&pCollate[1]; 18460f14e2ebSdrh pDb->pCollate = pCollate; 18475bb3eb9bSdrh memcpy(pCollate->zScript, zScript, nScript+1); 18480f14e2ebSdrh if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, 18490f14e2ebSdrh pCollate, tclSqlCollate) ){ 18509636c4e1Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 18510f14e2ebSdrh return TCL_ERROR; 18520f14e2ebSdrh } 18530f14e2ebSdrh break; 18540f14e2ebSdrh } 18550f14e2ebSdrh 18560f14e2ebSdrh /* 18570f14e2ebSdrh ** $db collation_needed SCRIPT 18580f14e2ebSdrh ** 18590f14e2ebSdrh ** Create a new SQL collation function called NAME. Whenever 18600f14e2ebSdrh ** that function is called, invoke SCRIPT to evaluate the function. 18610f14e2ebSdrh */ 18620f14e2ebSdrh case DB_COLLATION_NEEDED: { 18630f14e2ebSdrh if( objc!=3 ){ 18640f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT"); 18650f14e2ebSdrh return TCL_ERROR; 18660f14e2ebSdrh } 18670f14e2ebSdrh if( pDb->pCollateNeeded ){ 18680f14e2ebSdrh Tcl_DecrRefCount(pDb->pCollateNeeded); 18690f14e2ebSdrh } 18700f14e2ebSdrh pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]); 18710f14e2ebSdrh Tcl_IncrRefCount(pDb->pCollateNeeded); 18720f14e2ebSdrh sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded); 18730f14e2ebSdrh break; 18740f14e2ebSdrh } 18750f14e2ebSdrh 187619e2d37fSdrh /* $db commit_hook ?CALLBACK? 187719e2d37fSdrh ** 187819e2d37fSdrh ** Invoke the given callback just before committing every SQL transaction. 187919e2d37fSdrh ** If the callback throws an exception or returns non-zero, then the 188019e2d37fSdrh ** transaction is aborted. If CALLBACK is an empty string, the callback 188119e2d37fSdrh ** is disabled. 188219e2d37fSdrh */ 188319e2d37fSdrh case DB_COMMIT_HOOK: { 188419e2d37fSdrh if( objc>3 ){ 188519e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 188619e2d37fSdrh return TCL_ERROR; 188719e2d37fSdrh }else if( objc==2 ){ 188819e2d37fSdrh if( pDb->zCommit ){ 188919e2d37fSdrh Tcl_AppendResult(interp, pDb->zCommit, 0); 189019e2d37fSdrh } 189119e2d37fSdrh }else{ 189219e2d37fSdrh char *zCommit; 189319e2d37fSdrh int len; 189419e2d37fSdrh if( pDb->zCommit ){ 189519e2d37fSdrh Tcl_Free(pDb->zCommit); 189619e2d37fSdrh } 189719e2d37fSdrh zCommit = Tcl_GetStringFromObj(objv[2], &len); 189819e2d37fSdrh if( zCommit && len>0 ){ 189919e2d37fSdrh pDb->zCommit = Tcl_Alloc( len + 1 ); 19005bb3eb9bSdrh memcpy(pDb->zCommit, zCommit, len+1); 190119e2d37fSdrh }else{ 190219e2d37fSdrh pDb->zCommit = 0; 190319e2d37fSdrh } 190419e2d37fSdrh if( pDb->zCommit ){ 190519e2d37fSdrh pDb->interp = interp; 190619e2d37fSdrh sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb); 190719e2d37fSdrh }else{ 190819e2d37fSdrh sqlite3_commit_hook(pDb->db, 0, 0); 190919e2d37fSdrh } 191019e2d37fSdrh } 191119e2d37fSdrh break; 191219e2d37fSdrh } 191319e2d37fSdrh 191475897234Sdrh /* $db complete SQL 191575897234Sdrh ** 191675897234Sdrh ** Return TRUE if SQL is a complete SQL statement. Return FALSE if 191775897234Sdrh ** additional lines of input are needed. This is similar to the 191875897234Sdrh ** built-in "info complete" command of Tcl. 191975897234Sdrh */ 19206d31316cSdrh case DB_COMPLETE: { 1921ccae6026Sdrh #ifndef SQLITE_OMIT_COMPLETE 19226d31316cSdrh Tcl_Obj *pResult; 19236d31316cSdrh int isComplete; 19246d31316cSdrh if( objc!=3 ){ 19256d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 192675897234Sdrh return TCL_ERROR; 192775897234Sdrh } 19286f8a503dSdanielk1977 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) ); 19296d31316cSdrh pResult = Tcl_GetObjResult(interp); 19306d31316cSdrh Tcl_SetBooleanObj(pResult, isComplete); 1931ccae6026Sdrh #endif 19326d31316cSdrh break; 19336d31316cSdrh } 193475897234Sdrh 193519e2d37fSdrh /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR? 193619e2d37fSdrh ** 193719e2d37fSdrh ** Copy data into table from filename, optionally using SEPARATOR 193819e2d37fSdrh ** as column separators. If a column contains a null string, or the 193919e2d37fSdrh ** value of NULLINDICATOR, a NULL is inserted for the column. 194019e2d37fSdrh ** conflict-algorithm is one of the sqlite conflict algorithms: 194119e2d37fSdrh ** rollback, abort, fail, ignore, replace 194219e2d37fSdrh ** On success, return the number of lines processed, not necessarily same 194319e2d37fSdrh ** as 'db changes' due to conflict-algorithm selected. 194419e2d37fSdrh ** 194519e2d37fSdrh ** This code is basically an implementation/enhancement of 194619e2d37fSdrh ** the sqlite3 shell.c ".import" command. 194719e2d37fSdrh ** 194819e2d37fSdrh ** This command usage is equivalent to the sqlite2.x COPY statement, 194919e2d37fSdrh ** which imports file data into a table using the PostgreSQL COPY file format: 195019e2d37fSdrh ** $db copy $conflit_algo $table_name $filename \t \\N 195119e2d37fSdrh */ 195219e2d37fSdrh case DB_COPY: { 195319e2d37fSdrh char *zTable; /* Insert data into this table */ 195419e2d37fSdrh char *zFile; /* The file from which to extract data */ 195519e2d37fSdrh char *zConflict; /* The conflict algorithm to use */ 195619e2d37fSdrh sqlite3_stmt *pStmt; /* A statement */ 195719e2d37fSdrh int nCol; /* Number of columns in the table */ 195819e2d37fSdrh int nByte; /* Number of bytes in an SQL string */ 195919e2d37fSdrh int i, j; /* Loop counters */ 196019e2d37fSdrh int nSep; /* Number of bytes in zSep[] */ 196119e2d37fSdrh int nNull; /* Number of bytes in zNull[] */ 196219e2d37fSdrh char *zSql; /* An SQL statement */ 196319e2d37fSdrh char *zLine; /* A single line of input from the file */ 196419e2d37fSdrh char **azCol; /* zLine[] broken up into columns */ 196519e2d37fSdrh char *zCommit; /* How to commit changes */ 196619e2d37fSdrh FILE *in; /* The input file */ 196719e2d37fSdrh int lineno = 0; /* Line number of input file */ 196819e2d37fSdrh char zLineNum[80]; /* Line number print buffer */ 196919e2d37fSdrh Tcl_Obj *pResult; /* interp result */ 197019e2d37fSdrh 197119e2d37fSdrh char *zSep; 197219e2d37fSdrh char *zNull; 197319e2d37fSdrh if( objc<5 || objc>7 ){ 197419e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, 197519e2d37fSdrh "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"); 197619e2d37fSdrh return TCL_ERROR; 197719e2d37fSdrh } 197819e2d37fSdrh if( objc>=6 ){ 197919e2d37fSdrh zSep = Tcl_GetStringFromObj(objv[5], 0); 198019e2d37fSdrh }else{ 198119e2d37fSdrh zSep = "\t"; 198219e2d37fSdrh } 198319e2d37fSdrh if( objc>=7 ){ 198419e2d37fSdrh zNull = Tcl_GetStringFromObj(objv[6], 0); 198519e2d37fSdrh }else{ 198619e2d37fSdrh zNull = ""; 198719e2d37fSdrh } 198819e2d37fSdrh zConflict = Tcl_GetStringFromObj(objv[2], 0); 198919e2d37fSdrh zTable = Tcl_GetStringFromObj(objv[3], 0); 199019e2d37fSdrh zFile = Tcl_GetStringFromObj(objv[4], 0); 19914f21c4afSdrh nSep = strlen30(zSep); 19924f21c4afSdrh nNull = strlen30(zNull); 199319e2d37fSdrh if( nSep==0 ){ 199419e2d37fSdrh Tcl_AppendResult(interp,"Error: non-null separator required for copy",0); 199519e2d37fSdrh return TCL_ERROR; 199619e2d37fSdrh } 19973e59c012Sdrh if(strcmp(zConflict, "rollback") != 0 && 19983e59c012Sdrh strcmp(zConflict, "abort" ) != 0 && 19993e59c012Sdrh strcmp(zConflict, "fail" ) != 0 && 20003e59c012Sdrh strcmp(zConflict, "ignore" ) != 0 && 20013e59c012Sdrh strcmp(zConflict, "replace" ) != 0 ) { 200219e2d37fSdrh Tcl_AppendResult(interp, "Error: \"", zConflict, 200319e2d37fSdrh "\", conflict-algorithm must be one of: rollback, " 200419e2d37fSdrh "abort, fail, ignore, or replace", 0); 200519e2d37fSdrh return TCL_ERROR; 200619e2d37fSdrh } 200719e2d37fSdrh zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); 200819e2d37fSdrh if( zSql==0 ){ 200919e2d37fSdrh Tcl_AppendResult(interp, "Error: no such table: ", zTable, 0); 201019e2d37fSdrh return TCL_ERROR; 201119e2d37fSdrh } 20124f21c4afSdrh nByte = strlen30(zSql); 20133e701a18Sdrh rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 201419e2d37fSdrh sqlite3_free(zSql); 201519e2d37fSdrh if( rc ){ 201619e2d37fSdrh Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0); 201719e2d37fSdrh nCol = 0; 201819e2d37fSdrh }else{ 201919e2d37fSdrh nCol = sqlite3_column_count(pStmt); 202019e2d37fSdrh } 202119e2d37fSdrh sqlite3_finalize(pStmt); 202219e2d37fSdrh if( nCol==0 ) { 202319e2d37fSdrh return TCL_ERROR; 202419e2d37fSdrh } 202519e2d37fSdrh zSql = malloc( nByte + 50 + nCol*2 ); 202619e2d37fSdrh if( zSql==0 ) { 202719e2d37fSdrh Tcl_AppendResult(interp, "Error: can't malloc()", 0); 202819e2d37fSdrh return TCL_ERROR; 202919e2d37fSdrh } 203019e2d37fSdrh sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?", 203119e2d37fSdrh zConflict, zTable); 20324f21c4afSdrh j = strlen30(zSql); 203319e2d37fSdrh for(i=1; i<nCol; i++){ 203419e2d37fSdrh zSql[j++] = ','; 203519e2d37fSdrh zSql[j++] = '?'; 203619e2d37fSdrh } 203719e2d37fSdrh zSql[j++] = ')'; 203819e2d37fSdrh zSql[j] = 0; 20393e701a18Sdrh rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 204019e2d37fSdrh free(zSql); 204119e2d37fSdrh if( rc ){ 204219e2d37fSdrh Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0); 204319e2d37fSdrh sqlite3_finalize(pStmt); 204419e2d37fSdrh return TCL_ERROR; 204519e2d37fSdrh } 204619e2d37fSdrh in = fopen(zFile, "rb"); 204719e2d37fSdrh if( in==0 ){ 204819e2d37fSdrh Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL); 204919e2d37fSdrh sqlite3_finalize(pStmt); 205019e2d37fSdrh return TCL_ERROR; 205119e2d37fSdrh } 205219e2d37fSdrh azCol = malloc( sizeof(azCol[0])*(nCol+1) ); 205319e2d37fSdrh if( azCol==0 ) { 205419e2d37fSdrh Tcl_AppendResult(interp, "Error: can't malloc()", 0); 205543617e9aSdrh fclose(in); 205619e2d37fSdrh return TCL_ERROR; 205719e2d37fSdrh } 20583752785fSdrh (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0); 205919e2d37fSdrh zCommit = "COMMIT"; 206019e2d37fSdrh while( (zLine = local_getline(0, in))!=0 ){ 206119e2d37fSdrh char *z; 206219e2d37fSdrh i = 0; 206319e2d37fSdrh lineno++; 206419e2d37fSdrh azCol[0] = zLine; 206519e2d37fSdrh for(i=0, z=zLine; *z; z++){ 206619e2d37fSdrh if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){ 206719e2d37fSdrh *z = 0; 206819e2d37fSdrh i++; 206919e2d37fSdrh if( i<nCol ){ 207019e2d37fSdrh azCol[i] = &z[nSep]; 207119e2d37fSdrh z += nSep-1; 207219e2d37fSdrh } 207319e2d37fSdrh } 207419e2d37fSdrh } 207519e2d37fSdrh if( i+1!=nCol ){ 207619e2d37fSdrh char *zErr; 20774f21c4afSdrh int nErr = strlen30(zFile) + 200; 20785bb3eb9bSdrh zErr = malloc(nErr); 2079c1f4494eSdrh if( zErr ){ 20805bb3eb9bSdrh sqlite3_snprintf(nErr, zErr, 2081955de52cSdanielk1977 "Error: %s line %d: expected %d columns of data but found %d", 208219e2d37fSdrh zFile, lineno, nCol, i+1); 208319e2d37fSdrh Tcl_AppendResult(interp, zErr, 0); 208419e2d37fSdrh free(zErr); 2085c1f4494eSdrh } 208619e2d37fSdrh zCommit = "ROLLBACK"; 208719e2d37fSdrh break; 208819e2d37fSdrh } 208919e2d37fSdrh for(i=0; i<nCol; i++){ 209019e2d37fSdrh /* check for null data, if so, bind as null */ 2091ea678832Sdrh if( (nNull>0 && strcmp(azCol[i], zNull)==0) 20924f21c4afSdrh || strlen30(azCol[i])==0 2093ea678832Sdrh ){ 209419e2d37fSdrh sqlite3_bind_null(pStmt, i+1); 209519e2d37fSdrh }else{ 209619e2d37fSdrh sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); 209719e2d37fSdrh } 209819e2d37fSdrh } 209919e2d37fSdrh sqlite3_step(pStmt); 210019e2d37fSdrh rc = sqlite3_reset(pStmt); 210119e2d37fSdrh free(zLine); 210219e2d37fSdrh if( rc!=SQLITE_OK ){ 210319e2d37fSdrh Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), 0); 210419e2d37fSdrh zCommit = "ROLLBACK"; 210519e2d37fSdrh break; 210619e2d37fSdrh } 210719e2d37fSdrh } 210819e2d37fSdrh free(azCol); 210919e2d37fSdrh fclose(in); 211019e2d37fSdrh sqlite3_finalize(pStmt); 21113752785fSdrh (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0); 211219e2d37fSdrh 211319e2d37fSdrh if( zCommit[0] == 'C' ){ 211419e2d37fSdrh /* success, set result as number of lines processed */ 211519e2d37fSdrh pResult = Tcl_GetObjResult(interp); 211619e2d37fSdrh Tcl_SetIntObj(pResult, lineno); 211719e2d37fSdrh rc = TCL_OK; 211819e2d37fSdrh }else{ 211919e2d37fSdrh /* failure, append lineno where failed */ 21205bb3eb9bSdrh sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno); 212119e2d37fSdrh Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,0); 212219e2d37fSdrh rc = TCL_ERROR; 212319e2d37fSdrh } 212419e2d37fSdrh break; 212519e2d37fSdrh } 212619e2d37fSdrh 212775897234Sdrh /* 21284144905bSdrh ** $db enable_load_extension BOOLEAN 21294144905bSdrh ** 21304144905bSdrh ** Turn the extension loading feature on or off. It if off by 21314144905bSdrh ** default. 21324144905bSdrh */ 21334144905bSdrh case DB_ENABLE_LOAD_EXTENSION: { 2134f533acc0Sdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 21354144905bSdrh int onoff; 21364144905bSdrh if( objc!=3 ){ 21374144905bSdrh Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN"); 21384144905bSdrh return TCL_ERROR; 21394144905bSdrh } 21404144905bSdrh if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ 21414144905bSdrh return TCL_ERROR; 21424144905bSdrh } 21434144905bSdrh sqlite3_enable_load_extension(pDb->db, onoff); 21444144905bSdrh break; 2145f533acc0Sdrh #else 2146f533acc0Sdrh Tcl_AppendResult(interp, "extension loading is turned off at compile-time", 2147f533acc0Sdrh 0); 2148f533acc0Sdrh return TCL_ERROR; 2149f533acc0Sdrh #endif 21504144905bSdrh } 21514144905bSdrh 21524144905bSdrh /* 2153dcd997eaSdrh ** $db errorcode 2154dcd997eaSdrh ** 2155dcd997eaSdrh ** Return the numeric error code that was returned by the most recent 21566f8a503dSdanielk1977 ** call to sqlite3_exec(). 2157dcd997eaSdrh */ 2158dcd997eaSdrh case DB_ERRORCODE: { 2159f3ce83f5Sdanielk1977 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); 2160dcd997eaSdrh break; 2161dcd997eaSdrh } 2162dcd997eaSdrh 2163dcd997eaSdrh /* 21644a4c11aaSdan ** $db exists $sql 21651807ce37Sdrh ** $db onecolumn $sql 216675897234Sdrh ** 21674a4c11aaSdan ** The onecolumn method is the equivalent of: 21684a4c11aaSdan ** lindex [$db eval $sql] 0 21694a4c11aaSdan */ 21704a4c11aaSdan case DB_EXISTS: 21714a4c11aaSdan case DB_ONECOLUMN: { 21724a4c11aaSdan DbEvalContext sEval; 21734a4c11aaSdan if( objc!=3 ){ 21744a4c11aaSdan Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 21754a4c11aaSdan return TCL_ERROR; 21764a4c11aaSdan } 21774a4c11aaSdan 21784a4c11aaSdan dbEvalInit(&sEval, pDb, objv[2], 0); 21794a4c11aaSdan rc = dbEvalStep(&sEval); 21804a4c11aaSdan if( choice==DB_ONECOLUMN ){ 21814a4c11aaSdan if( rc==TCL_OK ){ 21824a4c11aaSdan Tcl_SetObjResult(interp, dbEvalColumnValue(&sEval, 0)); 21834a4c11aaSdan } 21844a4c11aaSdan }else if( rc==TCL_BREAK || rc==TCL_OK ){ 21854a4c11aaSdan Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc==TCL_OK)); 21864a4c11aaSdan } 21874a4c11aaSdan dbEvalFinalize(&sEval); 21884a4c11aaSdan 21894a4c11aaSdan if( rc==TCL_BREAK ){ 21904a4c11aaSdan rc = TCL_OK; 21914a4c11aaSdan } 21924a4c11aaSdan break; 21934a4c11aaSdan } 21944a4c11aaSdan 21954a4c11aaSdan /* 21964a4c11aaSdan ** $db eval $sql ?array? ?{ ...code... }? 21974a4c11aaSdan ** 219875897234Sdrh ** The SQL statement in $sql is evaluated. For each row, the values are 2199bec3f402Sdrh ** placed in elements of the array named "array" and ...code... is executed. 220075897234Sdrh ** If "array" and "code" are omitted, then no callback is every invoked. 220175897234Sdrh ** If "array" is an empty string, then the values are placed in variables 220275897234Sdrh ** that have the same name as the fields extracted by the query. 220375897234Sdrh */ 22044a4c11aaSdan case DB_EVAL: { 220592febd92Sdrh if( objc<3 || objc>5 ){ 2206895d7472Sdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?"); 220730ccda10Sdanielk1977 return TCL_ERROR; 220830ccda10Sdanielk1977 } 22094a4c11aaSdan 221092febd92Sdrh if( objc==3 ){ 22114a4c11aaSdan DbEvalContext sEval; 22124a4c11aaSdan Tcl_Obj *pRet = Tcl_NewObj(); 22134a4c11aaSdan Tcl_IncrRefCount(pRet); 22144a4c11aaSdan dbEvalInit(&sEval, pDb, objv[2], 0); 22154a4c11aaSdan while( TCL_OK==(rc = dbEvalStep(&sEval)) ){ 22164a4c11aaSdan int i; 22174a4c11aaSdan int nCol; 22184a4c11aaSdan dbEvalRowInfo(&sEval, &nCol, 0); 221992febd92Sdrh for(i=0; i<nCol; i++){ 22204a4c11aaSdan Tcl_ListObjAppendElement(interp, pRet, dbEvalColumnValue(&sEval, i)); 222192febd92Sdrh } 222230ccda10Sdanielk1977 } 22234a4c11aaSdan dbEvalFinalize(&sEval); 222490b6bb19Sdrh if( rc==TCL_BREAK ){ 22254a4c11aaSdan Tcl_SetObjResult(interp, pRet); 222690b6bb19Sdrh rc = TCL_OK; 222790b6bb19Sdrh } 2228ef2cb63eSdanielk1977 Tcl_DecrRefCount(pRet); 22294a4c11aaSdan }else{ 22304a4c11aaSdan ClientData cd[2]; 22314a4c11aaSdan DbEvalContext *p; 22324a4c11aaSdan Tcl_Obj *pArray = 0; 22334a4c11aaSdan Tcl_Obj *pScript; 22344a4c11aaSdan 22354a4c11aaSdan if( objc==5 && *(char *)Tcl_GetString(objv[3]) ){ 22364a4c11aaSdan pArray = objv[3]; 22374a4c11aaSdan } 22384a4c11aaSdan pScript = objv[objc-1]; 22394a4c11aaSdan Tcl_IncrRefCount(pScript); 22404a4c11aaSdan 22414a4c11aaSdan p = (DbEvalContext *)Tcl_Alloc(sizeof(DbEvalContext)); 22424a4c11aaSdan dbEvalInit(p, pDb, objv[2], pArray); 22434a4c11aaSdan 22444a4c11aaSdan cd[0] = (void *)p; 22454a4c11aaSdan cd[1] = (void *)pScript; 22464a4c11aaSdan rc = DbEvalNextCmd(cd, interp, TCL_OK); 22471807ce37Sdrh } 224830ccda10Sdanielk1977 break; 224930ccda10Sdanielk1977 } 2250bec3f402Sdrh 2251bec3f402Sdrh /* 2252e3602be8Sdrh ** $db function NAME [-argcount N] SCRIPT 2253cabb0819Sdrh ** 2254cabb0819Sdrh ** Create a new SQL function called NAME. Whenever that function is 2255cabb0819Sdrh ** called, invoke SCRIPT to evaluate the function. 2256cabb0819Sdrh */ 2257cabb0819Sdrh case DB_FUNCTION: { 2258cabb0819Sdrh SqlFunc *pFunc; 2259d1e4733dSdrh Tcl_Obj *pScript; 2260cabb0819Sdrh char *zName; 2261e3602be8Sdrh int nArg = -1; 2262e3602be8Sdrh if( objc==6 ){ 2263e3602be8Sdrh const char *z = Tcl_GetString(objv[3]); 22644f21c4afSdrh int n = strlen30(z); 2265e3602be8Sdrh if( n>2 && strncmp(z, "-argcount",n)==0 ){ 2266e3602be8Sdrh if( Tcl_GetIntFromObj(interp, objv[4], &nArg) ) return TCL_ERROR; 2267e3602be8Sdrh if( nArg<0 ){ 2268e3602be8Sdrh Tcl_AppendResult(interp, "number of arguments must be non-negative", 2269e3602be8Sdrh (char*)0); 2270cabb0819Sdrh return TCL_ERROR; 2271cabb0819Sdrh } 2272e3602be8Sdrh } 2273e3602be8Sdrh pScript = objv[5]; 2274e3602be8Sdrh }else if( objc!=4 ){ 2275e3602be8Sdrh Tcl_WrongNumArgs(interp, 2, objv, "NAME [-argcount N] SCRIPT"); 2276e3602be8Sdrh return TCL_ERROR; 2277e3602be8Sdrh }else{ 2278d1e4733dSdrh pScript = objv[3]; 2279e3602be8Sdrh } 2280e3602be8Sdrh zName = Tcl_GetStringFromObj(objv[2], 0); 2281d1e4733dSdrh pFunc = findSqlFunc(pDb, zName); 2282cabb0819Sdrh if( pFunc==0 ) return TCL_ERROR; 2283d1e4733dSdrh if( pFunc->pScript ){ 2284d1e4733dSdrh Tcl_DecrRefCount(pFunc->pScript); 2285d1e4733dSdrh } 2286d1e4733dSdrh pFunc->pScript = pScript; 2287d1e4733dSdrh Tcl_IncrRefCount(pScript); 2288d1e4733dSdrh pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript); 2289e3602be8Sdrh rc = sqlite3_create_function(pDb->db, zName, nArg, SQLITE_UTF8, 2290d8123366Sdanielk1977 pFunc, tclSqlFunc, 0, 0); 2291fb7e7651Sdrh if( rc!=SQLITE_OK ){ 2292fb7e7651Sdrh rc = TCL_ERROR; 22939636c4e1Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 2294fb7e7651Sdrh } 2295cabb0819Sdrh break; 2296cabb0819Sdrh } 2297cabb0819Sdrh 2298cabb0819Sdrh /* 22998cbadb02Sdanielk1977 ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID 2300b4e9af9fSdanielk1977 */ 2301b4e9af9fSdanielk1977 case DB_INCRBLOB: { 230232a0d8bbSdanielk1977 #ifdef SQLITE_OMIT_INCRBLOB 230332a0d8bbSdanielk1977 Tcl_AppendResult(interp, "incrblob not available in this build", 0); 230432a0d8bbSdanielk1977 return TCL_ERROR; 230532a0d8bbSdanielk1977 #else 23068cbadb02Sdanielk1977 int isReadonly = 0; 2307b4e9af9fSdanielk1977 const char *zDb = "main"; 2308b4e9af9fSdanielk1977 const char *zTable; 2309b4e9af9fSdanielk1977 const char *zColumn; 2310b4e9af9fSdanielk1977 sqlite_int64 iRow; 2311b4e9af9fSdanielk1977 23128cbadb02Sdanielk1977 /* Check for the -readonly option */ 23138cbadb02Sdanielk1977 if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){ 23148cbadb02Sdanielk1977 isReadonly = 1; 23158cbadb02Sdanielk1977 } 23168cbadb02Sdanielk1977 23178cbadb02Sdanielk1977 if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){ 23188cbadb02Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID"); 2319b4e9af9fSdanielk1977 return TCL_ERROR; 2320b4e9af9fSdanielk1977 } 2321b4e9af9fSdanielk1977 23228cbadb02Sdanielk1977 if( objc==(6+isReadonly) ){ 2323b4e9af9fSdanielk1977 zDb = Tcl_GetString(objv[2]); 2324b4e9af9fSdanielk1977 } 2325b4e9af9fSdanielk1977 zTable = Tcl_GetString(objv[objc-3]); 2326b4e9af9fSdanielk1977 zColumn = Tcl_GetString(objv[objc-2]); 2327b4e9af9fSdanielk1977 rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow); 2328b4e9af9fSdanielk1977 2329b4e9af9fSdanielk1977 if( rc==TCL_OK ){ 23308cbadb02Sdanielk1977 rc = createIncrblobChannel( 23318cbadb02Sdanielk1977 interp, pDb, zDb, zTable, zColumn, iRow, isReadonly 23328cbadb02Sdanielk1977 ); 2333b4e9af9fSdanielk1977 } 233432a0d8bbSdanielk1977 #endif 2335b4e9af9fSdanielk1977 break; 2336b4e9af9fSdanielk1977 } 2337b4e9af9fSdanielk1977 2338b4e9af9fSdanielk1977 /* 2339f11bded5Sdrh ** $db interrupt 2340f11bded5Sdrh ** 2341f11bded5Sdrh ** Interrupt the execution of the inner-most SQL interpreter. This 2342f11bded5Sdrh ** causes the SQL statement to return an error of SQLITE_INTERRUPT. 2343f11bded5Sdrh */ 2344f11bded5Sdrh case DB_INTERRUPT: { 2345f11bded5Sdrh sqlite3_interrupt(pDb->db); 2346f11bded5Sdrh break; 2347f11bded5Sdrh } 2348f11bded5Sdrh 2349f11bded5Sdrh /* 235019e2d37fSdrh ** $db nullvalue ?STRING? 235119e2d37fSdrh ** 235219e2d37fSdrh ** Change text used when a NULL comes back from the database. If ?STRING? 235319e2d37fSdrh ** is not present, then the current string used for NULL is returned. 235419e2d37fSdrh ** If STRING is present, then STRING is returned. 235519e2d37fSdrh ** 235619e2d37fSdrh */ 235719e2d37fSdrh case DB_NULLVALUE: { 235819e2d37fSdrh if( objc!=2 && objc!=3 ){ 235919e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE"); 236019e2d37fSdrh return TCL_ERROR; 236119e2d37fSdrh } 236219e2d37fSdrh if( objc==3 ){ 236319e2d37fSdrh int len; 236419e2d37fSdrh char *zNull = Tcl_GetStringFromObj(objv[2], &len); 236519e2d37fSdrh if( pDb->zNull ){ 236619e2d37fSdrh Tcl_Free(pDb->zNull); 236719e2d37fSdrh } 236819e2d37fSdrh if( zNull && len>0 ){ 236919e2d37fSdrh pDb->zNull = Tcl_Alloc( len + 1 ); 237019e2d37fSdrh strncpy(pDb->zNull, zNull, len); 237119e2d37fSdrh pDb->zNull[len] = '\0'; 237219e2d37fSdrh }else{ 237319e2d37fSdrh pDb->zNull = 0; 237419e2d37fSdrh } 237519e2d37fSdrh } 237619e2d37fSdrh Tcl_SetObjResult(interp, dbTextToObj(pDb->zNull)); 237719e2d37fSdrh break; 237819e2d37fSdrh } 237919e2d37fSdrh 238019e2d37fSdrh /* 2381af9ff33aSdrh ** $db last_insert_rowid 2382af9ff33aSdrh ** 2383af9ff33aSdrh ** Return an integer which is the ROWID for the most recent insert. 2384af9ff33aSdrh */ 2385af9ff33aSdrh case DB_LAST_INSERT_ROWID: { 2386af9ff33aSdrh Tcl_Obj *pResult; 2387f7e678d6Sdrh Tcl_WideInt rowid; 2388af9ff33aSdrh if( objc!=2 ){ 2389af9ff33aSdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 2390af9ff33aSdrh return TCL_ERROR; 2391af9ff33aSdrh } 23926f8a503dSdanielk1977 rowid = sqlite3_last_insert_rowid(pDb->db); 2393af9ff33aSdrh pResult = Tcl_GetObjResult(interp); 2394f7e678d6Sdrh Tcl_SetWideIntObj(pResult, rowid); 2395af9ff33aSdrh break; 2396af9ff33aSdrh } 2397af9ff33aSdrh 2398af9ff33aSdrh /* 23994a4c11aaSdan ** The DB_ONECOLUMN method is implemented together with DB_EXISTS. 24005d9d7576Sdrh */ 24011807ce37Sdrh 24021807ce37Sdrh /* $db progress ?N CALLBACK? 24031807ce37Sdrh ** 24041807ce37Sdrh ** Invoke the given callback every N virtual machine opcodes while executing 24051807ce37Sdrh ** queries. 24061807ce37Sdrh */ 24071807ce37Sdrh case DB_PROGRESS: { 24081807ce37Sdrh if( objc==2 ){ 24091807ce37Sdrh if( pDb->zProgress ){ 24101807ce37Sdrh Tcl_AppendResult(interp, pDb->zProgress, 0); 24115d9d7576Sdrh } 24121807ce37Sdrh }else if( objc==4 ){ 24131807ce37Sdrh char *zProgress; 24141807ce37Sdrh int len; 24151807ce37Sdrh int N; 24161807ce37Sdrh if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ 24171807ce37Sdrh return TCL_ERROR; 24181807ce37Sdrh }; 24191807ce37Sdrh if( pDb->zProgress ){ 24201807ce37Sdrh Tcl_Free(pDb->zProgress); 24211807ce37Sdrh } 24221807ce37Sdrh zProgress = Tcl_GetStringFromObj(objv[3], &len); 24231807ce37Sdrh if( zProgress && len>0 ){ 24241807ce37Sdrh pDb->zProgress = Tcl_Alloc( len + 1 ); 24255bb3eb9bSdrh memcpy(pDb->zProgress, zProgress, len+1); 24261807ce37Sdrh }else{ 24271807ce37Sdrh pDb->zProgress = 0; 24281807ce37Sdrh } 24291807ce37Sdrh #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 24301807ce37Sdrh if( pDb->zProgress ){ 24311807ce37Sdrh pDb->interp = interp; 24321807ce37Sdrh sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb); 24331807ce37Sdrh }else{ 24341807ce37Sdrh sqlite3_progress_handler(pDb->db, 0, 0, 0); 24351807ce37Sdrh } 24361807ce37Sdrh #endif 24371807ce37Sdrh }else{ 24381807ce37Sdrh Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); 24391807ce37Sdrh return TCL_ERROR; 24405d9d7576Sdrh } 24415d9d7576Sdrh break; 24425d9d7576Sdrh } 24435d9d7576Sdrh 244419e2d37fSdrh /* $db profile ?CALLBACK? 244519e2d37fSdrh ** 244619e2d37fSdrh ** Make arrangements to invoke the CALLBACK routine after each SQL statement 244719e2d37fSdrh ** that has run. The text of the SQL and the amount of elapse time are 244819e2d37fSdrh ** appended to CALLBACK before the script is run. 244919e2d37fSdrh */ 245019e2d37fSdrh case DB_PROFILE: { 245119e2d37fSdrh if( objc>3 ){ 245219e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 245319e2d37fSdrh return TCL_ERROR; 245419e2d37fSdrh }else if( objc==2 ){ 245519e2d37fSdrh if( pDb->zProfile ){ 245619e2d37fSdrh Tcl_AppendResult(interp, pDb->zProfile, 0); 245719e2d37fSdrh } 245819e2d37fSdrh }else{ 245919e2d37fSdrh char *zProfile; 246019e2d37fSdrh int len; 246119e2d37fSdrh if( pDb->zProfile ){ 246219e2d37fSdrh Tcl_Free(pDb->zProfile); 246319e2d37fSdrh } 246419e2d37fSdrh zProfile = Tcl_GetStringFromObj(objv[2], &len); 246519e2d37fSdrh if( zProfile && len>0 ){ 246619e2d37fSdrh pDb->zProfile = Tcl_Alloc( len + 1 ); 24675bb3eb9bSdrh memcpy(pDb->zProfile, zProfile, len+1); 246819e2d37fSdrh }else{ 246919e2d37fSdrh pDb->zProfile = 0; 247019e2d37fSdrh } 247119e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 247219e2d37fSdrh if( pDb->zProfile ){ 247319e2d37fSdrh pDb->interp = interp; 247419e2d37fSdrh sqlite3_profile(pDb->db, DbProfileHandler, pDb); 247519e2d37fSdrh }else{ 247619e2d37fSdrh sqlite3_profile(pDb->db, 0, 0); 247719e2d37fSdrh } 247819e2d37fSdrh #endif 247919e2d37fSdrh } 248019e2d37fSdrh break; 248119e2d37fSdrh } 248219e2d37fSdrh 24835d9d7576Sdrh /* 248422fbcb8dSdrh ** $db rekey KEY 248522fbcb8dSdrh ** 248622fbcb8dSdrh ** Change the encryption key on the currently open database. 248722fbcb8dSdrh */ 248822fbcb8dSdrh case DB_REKEY: { 248922fbcb8dSdrh int nKey; 249022fbcb8dSdrh void *pKey; 249122fbcb8dSdrh if( objc!=3 ){ 249222fbcb8dSdrh Tcl_WrongNumArgs(interp, 2, objv, "KEY"); 249322fbcb8dSdrh return TCL_ERROR; 249422fbcb8dSdrh } 249522fbcb8dSdrh pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); 24969eb9e26bSdrh #ifdef SQLITE_HAS_CODEC 24972011d5f5Sdrh rc = sqlite3_rekey(pDb->db, pKey, nKey); 249822fbcb8dSdrh if( rc ){ 2499f20b21c8Sdanielk1977 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); 250022fbcb8dSdrh rc = TCL_ERROR; 250122fbcb8dSdrh } 250222fbcb8dSdrh #endif 250322fbcb8dSdrh break; 250422fbcb8dSdrh } 250522fbcb8dSdrh 2506dc2c4915Sdrh /* $db restore ?DATABASE? FILENAME 2507dc2c4915Sdrh ** 2508dc2c4915Sdrh ** Open a database file named FILENAME. Transfer the content 2509dc2c4915Sdrh ** of FILENAME into the local database DATABASE (default: "main"). 2510dc2c4915Sdrh */ 2511dc2c4915Sdrh case DB_RESTORE: { 2512dc2c4915Sdrh const char *zSrcFile; 2513dc2c4915Sdrh const char *zDestDb; 2514dc2c4915Sdrh sqlite3 *pSrc; 2515dc2c4915Sdrh sqlite3_backup *pBackup; 2516dc2c4915Sdrh int nTimeout = 0; 2517dc2c4915Sdrh 2518dc2c4915Sdrh if( objc==3 ){ 2519dc2c4915Sdrh zDestDb = "main"; 2520dc2c4915Sdrh zSrcFile = Tcl_GetString(objv[2]); 2521dc2c4915Sdrh }else if( objc==4 ){ 2522dc2c4915Sdrh zDestDb = Tcl_GetString(objv[2]); 2523dc2c4915Sdrh zSrcFile = Tcl_GetString(objv[3]); 2524dc2c4915Sdrh }else{ 2525dc2c4915Sdrh Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 2526dc2c4915Sdrh return TCL_ERROR; 2527dc2c4915Sdrh } 2528dc2c4915Sdrh rc = sqlite3_open_v2(zSrcFile, &pSrc, SQLITE_OPEN_READONLY, 0); 2529dc2c4915Sdrh if( rc!=SQLITE_OK ){ 2530dc2c4915Sdrh Tcl_AppendResult(interp, "cannot open source database: ", 2531dc2c4915Sdrh sqlite3_errmsg(pSrc), (char*)0); 2532dc2c4915Sdrh sqlite3_close(pSrc); 2533dc2c4915Sdrh return TCL_ERROR; 2534dc2c4915Sdrh } 2535dc2c4915Sdrh pBackup = sqlite3_backup_init(pDb->db, zDestDb, pSrc, "main"); 2536dc2c4915Sdrh if( pBackup==0 ){ 2537dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: ", 2538dc2c4915Sdrh sqlite3_errmsg(pDb->db), (char*)0); 2539dc2c4915Sdrh sqlite3_close(pSrc); 2540dc2c4915Sdrh return TCL_ERROR; 2541dc2c4915Sdrh } 2542dc2c4915Sdrh while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK 2543dc2c4915Sdrh || rc==SQLITE_BUSY ){ 2544dc2c4915Sdrh if( rc==SQLITE_BUSY ){ 2545dc2c4915Sdrh if( nTimeout++ >= 3 ) break; 2546dc2c4915Sdrh sqlite3_sleep(100); 2547dc2c4915Sdrh } 2548dc2c4915Sdrh } 2549dc2c4915Sdrh sqlite3_backup_finish(pBackup); 2550dc2c4915Sdrh if( rc==SQLITE_DONE ){ 2551dc2c4915Sdrh rc = TCL_OK; 2552dc2c4915Sdrh }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 2553dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: source database busy", 2554dc2c4915Sdrh (char*)0); 2555dc2c4915Sdrh rc = TCL_ERROR; 2556dc2c4915Sdrh }else{ 2557dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: ", 2558dc2c4915Sdrh sqlite3_errmsg(pDb->db), (char*)0); 2559dc2c4915Sdrh rc = TCL_ERROR; 2560dc2c4915Sdrh } 2561dc2c4915Sdrh sqlite3_close(pSrc); 2562dc2c4915Sdrh break; 2563dc2c4915Sdrh } 2564dc2c4915Sdrh 256522fbcb8dSdrh /* 25663c379b01Sdrh ** $db status (step|sort|autoindex) 2567d1d38488Sdrh ** 2568d1d38488Sdrh ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or 2569d1d38488Sdrh ** SQLITE_STMTSTATUS_SORT for the most recent eval. 2570d1d38488Sdrh */ 2571d1d38488Sdrh case DB_STATUS: { 2572d1d38488Sdrh int v; 2573d1d38488Sdrh const char *zOp; 2574d1d38488Sdrh if( objc!=3 ){ 25751c320a43Sdrh Tcl_WrongNumArgs(interp, 2, objv, "(step|sort|autoindex)"); 2576d1d38488Sdrh return TCL_ERROR; 2577d1d38488Sdrh } 2578d1d38488Sdrh zOp = Tcl_GetString(objv[2]); 2579d1d38488Sdrh if( strcmp(zOp, "step")==0 ){ 2580d1d38488Sdrh v = pDb->nStep; 2581d1d38488Sdrh }else if( strcmp(zOp, "sort")==0 ){ 2582d1d38488Sdrh v = pDb->nSort; 25833c379b01Sdrh }else if( strcmp(zOp, "autoindex")==0 ){ 25843c379b01Sdrh v = pDb->nIndex; 2585d1d38488Sdrh }else{ 25863c379b01Sdrh Tcl_AppendResult(interp, 25873c379b01Sdrh "bad argument: should be autoindex, step, or sort", 2588d1d38488Sdrh (char*)0); 2589d1d38488Sdrh return TCL_ERROR; 2590d1d38488Sdrh } 2591d1d38488Sdrh Tcl_SetObjResult(interp, Tcl_NewIntObj(v)); 2592d1d38488Sdrh break; 2593d1d38488Sdrh } 2594d1d38488Sdrh 2595d1d38488Sdrh /* 2596bec3f402Sdrh ** $db timeout MILLESECONDS 2597bec3f402Sdrh ** 2598bec3f402Sdrh ** Delay for the number of milliseconds specified when a file is locked. 2599bec3f402Sdrh */ 26006d31316cSdrh case DB_TIMEOUT: { 2601bec3f402Sdrh int ms; 26026d31316cSdrh if( objc!=3 ){ 26036d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); 2604bec3f402Sdrh return TCL_ERROR; 260575897234Sdrh } 26066d31316cSdrh if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; 26076f8a503dSdanielk1977 sqlite3_busy_timeout(pDb->db, ms); 26086d31316cSdrh break; 260975897234Sdrh } 2610b5a20d3cSdrh 26110f14e2ebSdrh /* 26120f14e2ebSdrh ** $db total_changes 26130f14e2ebSdrh ** 26140f14e2ebSdrh ** Return the number of rows that were modified, inserted, or deleted 26150f14e2ebSdrh ** since the database handle was created. 26160f14e2ebSdrh */ 26170f14e2ebSdrh case DB_TOTAL_CHANGES: { 26180f14e2ebSdrh Tcl_Obj *pResult; 26190f14e2ebSdrh if( objc!=2 ){ 26200f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 26210f14e2ebSdrh return TCL_ERROR; 26220f14e2ebSdrh } 26230f14e2ebSdrh pResult = Tcl_GetObjResult(interp); 26240f14e2ebSdrh Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db)); 26250f14e2ebSdrh break; 26260f14e2ebSdrh } 26270f14e2ebSdrh 2628b5a20d3cSdrh /* $db trace ?CALLBACK? 2629b5a20d3cSdrh ** 2630b5a20d3cSdrh ** Make arrangements to invoke the CALLBACK routine for each SQL statement 2631b5a20d3cSdrh ** that is executed. The text of the SQL is appended to CALLBACK before 2632b5a20d3cSdrh ** it is executed. 2633b5a20d3cSdrh */ 2634b5a20d3cSdrh case DB_TRACE: { 2635b5a20d3cSdrh if( objc>3 ){ 2636b5a20d3cSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 2637b97759edSdrh return TCL_ERROR; 2638b5a20d3cSdrh }else if( objc==2 ){ 2639b5a20d3cSdrh if( pDb->zTrace ){ 2640b5a20d3cSdrh Tcl_AppendResult(interp, pDb->zTrace, 0); 2641b5a20d3cSdrh } 2642b5a20d3cSdrh }else{ 2643b5a20d3cSdrh char *zTrace; 2644b5a20d3cSdrh int len; 2645b5a20d3cSdrh if( pDb->zTrace ){ 2646b5a20d3cSdrh Tcl_Free(pDb->zTrace); 2647b5a20d3cSdrh } 2648b5a20d3cSdrh zTrace = Tcl_GetStringFromObj(objv[2], &len); 2649b5a20d3cSdrh if( zTrace && len>0 ){ 2650b5a20d3cSdrh pDb->zTrace = Tcl_Alloc( len + 1 ); 26515bb3eb9bSdrh memcpy(pDb->zTrace, zTrace, len+1); 2652b5a20d3cSdrh }else{ 2653b5a20d3cSdrh pDb->zTrace = 0; 2654b5a20d3cSdrh } 265519e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 2656b5a20d3cSdrh if( pDb->zTrace ){ 2657b5a20d3cSdrh pDb->interp = interp; 26586f8a503dSdanielk1977 sqlite3_trace(pDb->db, DbTraceHandler, pDb); 2659b5a20d3cSdrh }else{ 26606f8a503dSdanielk1977 sqlite3_trace(pDb->db, 0, 0); 2661b5a20d3cSdrh } 266219e2d37fSdrh #endif 2663b5a20d3cSdrh } 2664b5a20d3cSdrh break; 2665b5a20d3cSdrh } 2666b5a20d3cSdrh 26673d21423cSdrh /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT 26683d21423cSdrh ** 26693d21423cSdrh ** Start a new transaction (if we are not already in the midst of a 26703d21423cSdrh ** transaction) and execute the TCL script SCRIPT. After SCRIPT 26713d21423cSdrh ** completes, either commit the transaction or roll it back if SCRIPT 26723d21423cSdrh ** throws an exception. Or if no new transation was started, do nothing. 26733d21423cSdrh ** pass the exception on up the stack. 26743d21423cSdrh ** 26753d21423cSdrh ** This command was inspired by Dave Thomas's talk on Ruby at the 26763d21423cSdrh ** 2005 O'Reilly Open Source Convention (OSCON). 26773d21423cSdrh */ 26783d21423cSdrh case DB_TRANSACTION: { 26793d21423cSdrh Tcl_Obj *pScript; 2680cd38d520Sdanielk1977 const char *zBegin = "SAVEPOINT _tcl_transaction"; 26813d21423cSdrh if( objc!=3 && objc!=4 ){ 26823d21423cSdrh Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT"); 26833d21423cSdrh return TCL_ERROR; 26843d21423cSdrh } 2685cd38d520Sdanielk1977 26864a4c11aaSdan if( pDb->nTransaction==0 && objc==4 ){ 26873d21423cSdrh static const char *TTYPE_strs[] = { 2688ce604012Sdrh "deferred", "exclusive", "immediate", 0 26893d21423cSdrh }; 26903d21423cSdrh enum TTYPE_enum { 26913d21423cSdrh TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE 26923d21423cSdrh }; 26933d21423cSdrh int ttype; 2694b5555e7eSdrh if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type", 26953d21423cSdrh 0, &ttype) ){ 26963d21423cSdrh return TCL_ERROR; 26973d21423cSdrh } 26983d21423cSdrh switch( (enum TTYPE_enum)ttype ){ 26993d21423cSdrh case TTYPE_DEFERRED: /* no-op */; break; 27003d21423cSdrh case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break; 27013d21423cSdrh case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break; 27023d21423cSdrh } 27033d21423cSdrh } 2704cd38d520Sdanielk1977 pScript = objv[objc-1]; 2705cd38d520Sdanielk1977 27064a4c11aaSdan /* Run the SQLite BEGIN command to open a transaction or savepoint. */ 27071f1549f8Sdrh pDb->disableAuth++; 2708cd38d520Sdanielk1977 rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0); 27091f1549f8Sdrh pDb->disableAuth--; 2710cd38d520Sdanielk1977 if( rc!=SQLITE_OK ){ 2711cd38d520Sdanielk1977 Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); 2712cd38d520Sdanielk1977 return TCL_ERROR; 27133d21423cSdrh } 2714cd38d520Sdanielk1977 pDb->nTransaction++; 2715cd38d520Sdanielk1977 27164a4c11aaSdan /* If using NRE, schedule a callback to invoke the script pScript, then 27174a4c11aaSdan ** a second callback to commit (or rollback) the transaction or savepoint 27184a4c11aaSdan ** opened above. If not using NRE, evaluate the script directly, then 27194a4c11aaSdan ** call function DbTransPostCmd() to commit (or rollback) the transaction 27204a4c11aaSdan ** or savepoint. */ 27214a4c11aaSdan if( DbUseNre() ){ 27224a4c11aaSdan Tcl_NRAddCallback(interp, DbTransPostCmd, cd, 0, 0, 0); 27234a4c11aaSdan Tcl_NREvalObj(interp, pScript, 0); 27243d21423cSdrh }else{ 27254a4c11aaSdan rc = DbTransPostCmd(&cd, interp, Tcl_EvalObjEx(interp, pScript, 0)); 27263d21423cSdrh } 27273d21423cSdrh break; 27283d21423cSdrh } 27293d21423cSdrh 273094eb6a14Sdanielk1977 /* 2731404ca075Sdanielk1977 ** $db unlock_notify ?script? 2732404ca075Sdanielk1977 */ 2733404ca075Sdanielk1977 case DB_UNLOCK_NOTIFY: { 2734404ca075Sdanielk1977 #ifndef SQLITE_ENABLE_UNLOCK_NOTIFY 2735404ca075Sdanielk1977 Tcl_AppendResult(interp, "unlock_notify not available in this build", 0); 2736404ca075Sdanielk1977 rc = TCL_ERROR; 2737404ca075Sdanielk1977 #else 2738404ca075Sdanielk1977 if( objc!=2 && objc!=3 ){ 2739404ca075Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 2740404ca075Sdanielk1977 rc = TCL_ERROR; 2741404ca075Sdanielk1977 }else{ 2742404ca075Sdanielk1977 void (*xNotify)(void **, int) = 0; 2743404ca075Sdanielk1977 void *pNotifyArg = 0; 2744404ca075Sdanielk1977 2745404ca075Sdanielk1977 if( pDb->pUnlockNotify ){ 2746404ca075Sdanielk1977 Tcl_DecrRefCount(pDb->pUnlockNotify); 2747404ca075Sdanielk1977 pDb->pUnlockNotify = 0; 2748404ca075Sdanielk1977 } 2749404ca075Sdanielk1977 2750404ca075Sdanielk1977 if( objc==3 ){ 2751404ca075Sdanielk1977 xNotify = DbUnlockNotify; 2752404ca075Sdanielk1977 pNotifyArg = (void *)pDb; 2753404ca075Sdanielk1977 pDb->pUnlockNotify = objv[2]; 2754404ca075Sdanielk1977 Tcl_IncrRefCount(pDb->pUnlockNotify); 2755404ca075Sdanielk1977 } 2756404ca075Sdanielk1977 2757404ca075Sdanielk1977 if( sqlite3_unlock_notify(pDb->db, xNotify, pNotifyArg) ){ 2758404ca075Sdanielk1977 Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); 2759404ca075Sdanielk1977 rc = TCL_ERROR; 2760404ca075Sdanielk1977 } 2761404ca075Sdanielk1977 } 2762404ca075Sdanielk1977 #endif 2763404ca075Sdanielk1977 break; 2764404ca075Sdanielk1977 } 2765404ca075Sdanielk1977 2766404ca075Sdanielk1977 /* 2767833bf968Sdrh ** $db wal_hook ?script? 276894eb6a14Sdanielk1977 ** $db update_hook ?script? 276971fd80bfSdanielk1977 ** $db rollback_hook ?script? 277094eb6a14Sdanielk1977 */ 2771833bf968Sdrh case DB_WAL_HOOK: 277271fd80bfSdanielk1977 case DB_UPDATE_HOOK: 277371fd80bfSdanielk1977 case DB_ROLLBACK_HOOK: { 277471fd80bfSdanielk1977 277571fd80bfSdanielk1977 /* set ppHook to point at pUpdateHook or pRollbackHook, depending on 277671fd80bfSdanielk1977 ** whether [$db update_hook] or [$db rollback_hook] was invoked. 277771fd80bfSdanielk1977 */ 277871fd80bfSdanielk1977 Tcl_Obj **ppHook; 277971fd80bfSdanielk1977 if( choice==DB_UPDATE_HOOK ){ 278071fd80bfSdanielk1977 ppHook = &pDb->pUpdateHook; 2781833bf968Sdrh }else if( choice==DB_WAL_HOOK ){ 27825def0843Sdrh ppHook = &pDb->pWalHook; 278371fd80bfSdanielk1977 }else{ 278471fd80bfSdanielk1977 ppHook = &pDb->pRollbackHook; 278571fd80bfSdanielk1977 } 278671fd80bfSdanielk1977 278794eb6a14Sdanielk1977 if( objc!=2 && objc!=3 ){ 278894eb6a14Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 278994eb6a14Sdanielk1977 return TCL_ERROR; 279094eb6a14Sdanielk1977 } 279171fd80bfSdanielk1977 if( *ppHook ){ 279271fd80bfSdanielk1977 Tcl_SetObjResult(interp, *ppHook); 279394eb6a14Sdanielk1977 if( objc==3 ){ 279471fd80bfSdanielk1977 Tcl_DecrRefCount(*ppHook); 279571fd80bfSdanielk1977 *ppHook = 0; 279694eb6a14Sdanielk1977 } 279794eb6a14Sdanielk1977 } 279894eb6a14Sdanielk1977 if( objc==3 ){ 279971fd80bfSdanielk1977 assert( !(*ppHook) ); 280094eb6a14Sdanielk1977 if( Tcl_GetCharLength(objv[2])>0 ){ 280171fd80bfSdanielk1977 *ppHook = objv[2]; 280271fd80bfSdanielk1977 Tcl_IncrRefCount(*ppHook); 280394eb6a14Sdanielk1977 } 280494eb6a14Sdanielk1977 } 280571fd80bfSdanielk1977 280671fd80bfSdanielk1977 sqlite3_update_hook(pDb->db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); 280771fd80bfSdanielk1977 sqlite3_rollback_hook(pDb->db,(pDb->pRollbackHook?DbRollbackHandler:0),pDb); 28085def0843Sdrh sqlite3_wal_hook(pDb->db,(pDb->pWalHook?DbWalHandler:0),pDb); 280971fd80bfSdanielk1977 281094eb6a14Sdanielk1977 break; 281194eb6a14Sdanielk1977 } 281294eb6a14Sdanielk1977 28134397de57Sdanielk1977 /* $db version 28144397de57Sdanielk1977 ** 28154397de57Sdanielk1977 ** Return the version string for this database. 28164397de57Sdanielk1977 */ 28174397de57Sdanielk1977 case DB_VERSION: { 28184397de57Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC); 28194397de57Sdanielk1977 break; 28204397de57Sdanielk1977 } 28214397de57Sdanielk1977 28221067fe11Stpoindex 28236d31316cSdrh } /* End of the SWITCH statement */ 282422fbcb8dSdrh return rc; 282575897234Sdrh } 282675897234Sdrh 2827a2c8a95bSdrh #if SQLITE_TCL_NRE 2828a2c8a95bSdrh /* 2829a2c8a95bSdrh ** Adaptor that provides an objCmd interface to the NRE-enabled 2830a2c8a95bSdrh ** interface implementation. 2831a2c8a95bSdrh */ 2832a2c8a95bSdrh static int DbObjCmdAdaptor( 2833a2c8a95bSdrh void *cd, 2834a2c8a95bSdrh Tcl_Interp *interp, 2835a2c8a95bSdrh int objc, 2836a2c8a95bSdrh Tcl_Obj *const*objv 2837a2c8a95bSdrh ){ 2838a2c8a95bSdrh return Tcl_NRCallObjProc(interp, DbObjCmd, cd, objc, objv); 2839a2c8a95bSdrh } 2840a2c8a95bSdrh #endif /* SQLITE_TCL_NRE */ 2841a2c8a95bSdrh 284275897234Sdrh /* 28433570ad93Sdrh ** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN? 28449a6284c1Sdanielk1977 ** ?-create BOOLEAN? ?-nomutex BOOLEAN? 284575897234Sdrh ** 284675897234Sdrh ** This is the main Tcl command. When the "sqlite" Tcl command is 284775897234Sdrh ** invoked, this routine runs to process that command. 284875897234Sdrh ** 284975897234Sdrh ** The first argument, DBNAME, is an arbitrary name for a new 285075897234Sdrh ** database connection. This command creates a new command named 285175897234Sdrh ** DBNAME that is used to control that connection. The database 285275897234Sdrh ** connection is deleted when the DBNAME command is deleted. 285375897234Sdrh ** 28543570ad93Sdrh ** The second argument is the name of the database file. 2855fbc3eab8Sdrh ** 285675897234Sdrh */ 285722fbcb8dSdrh static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ 2858bec3f402Sdrh SqliteDb *p; 285922fbcb8dSdrh void *pKey = 0; 286022fbcb8dSdrh int nKey = 0; 286122fbcb8dSdrh const char *zArg; 286275897234Sdrh char *zErrMsg; 28633570ad93Sdrh int i; 286422fbcb8dSdrh const char *zFile; 28653570ad93Sdrh const char *zVfs = 0; 2866d9da78a2Sdrh int flags; 2867882e8e4dSdrh Tcl_DString translatedFilename; 2868d9da78a2Sdrh 2869d9da78a2Sdrh /* In normal use, each TCL interpreter runs in a single thread. So 2870d9da78a2Sdrh ** by default, we can turn of mutexing on SQLite database connections. 2871d9da78a2Sdrh ** However, for testing purposes it is useful to have mutexes turned 2872d9da78a2Sdrh ** on. So, by default, mutexes default off. But if compiled with 2873d9da78a2Sdrh ** SQLITE_TCL_DEFAULT_FULLMUTEX then mutexes default on. 2874d9da78a2Sdrh */ 2875d9da78a2Sdrh #ifdef SQLITE_TCL_DEFAULT_FULLMUTEX 2876d9da78a2Sdrh flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX; 2877d9da78a2Sdrh #else 2878d9da78a2Sdrh flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX; 2879d9da78a2Sdrh #endif 2880d9da78a2Sdrh 288122fbcb8dSdrh if( objc==2 ){ 288222fbcb8dSdrh zArg = Tcl_GetStringFromObj(objv[1], 0); 288322fbcb8dSdrh if( strcmp(zArg,"-version")==0 ){ 28846f8a503dSdanielk1977 Tcl_AppendResult(interp,sqlite3_version,0); 2885647cb0e1Sdrh return TCL_OK; 2886647cb0e1Sdrh } 28879eb9e26bSdrh if( strcmp(zArg,"-has-codec")==0 ){ 28889eb9e26bSdrh #ifdef SQLITE_HAS_CODEC 288922fbcb8dSdrh Tcl_AppendResult(interp,"1",0); 289022fbcb8dSdrh #else 289122fbcb8dSdrh Tcl_AppendResult(interp,"0",0); 289222fbcb8dSdrh #endif 289322fbcb8dSdrh return TCL_OK; 289422fbcb8dSdrh } 2895fbc3eab8Sdrh } 28963570ad93Sdrh for(i=3; i+1<objc; i+=2){ 28973570ad93Sdrh zArg = Tcl_GetString(objv[i]); 289822fbcb8dSdrh if( strcmp(zArg,"-key")==0 ){ 28993570ad93Sdrh pKey = Tcl_GetByteArrayFromObj(objv[i+1], &nKey); 29003570ad93Sdrh }else if( strcmp(zArg, "-vfs")==0 ){ 29013c3dd7b9Sdan zVfs = Tcl_GetString(objv[i+1]); 29023570ad93Sdrh }else if( strcmp(zArg, "-readonly")==0 ){ 29033570ad93Sdrh int b; 29043570ad93Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 29053570ad93Sdrh if( b ){ 290633f4e02aSdrh flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); 29073570ad93Sdrh flags |= SQLITE_OPEN_READONLY; 29083570ad93Sdrh }else{ 29093570ad93Sdrh flags &= ~SQLITE_OPEN_READONLY; 29103570ad93Sdrh flags |= SQLITE_OPEN_READWRITE; 29113570ad93Sdrh } 29123570ad93Sdrh }else if( strcmp(zArg, "-create")==0 ){ 29133570ad93Sdrh int b; 29143570ad93Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 291533f4e02aSdrh if( b && (flags & SQLITE_OPEN_READONLY)==0 ){ 29163570ad93Sdrh flags |= SQLITE_OPEN_CREATE; 29173570ad93Sdrh }else{ 29183570ad93Sdrh flags &= ~SQLITE_OPEN_CREATE; 29193570ad93Sdrh } 29209a6284c1Sdanielk1977 }else if( strcmp(zArg, "-nomutex")==0 ){ 29219a6284c1Sdanielk1977 int b; 29229a6284c1Sdanielk1977 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 29239a6284c1Sdanielk1977 if( b ){ 29249a6284c1Sdanielk1977 flags |= SQLITE_OPEN_NOMUTEX; 2925039963adSdrh flags &= ~SQLITE_OPEN_FULLMUTEX; 29269a6284c1Sdanielk1977 }else{ 29279a6284c1Sdanielk1977 flags &= ~SQLITE_OPEN_NOMUTEX; 29289a6284c1Sdanielk1977 } 2929039963adSdrh }else if( strcmp(zArg, "-fullmutex")==0 ){ 2930039963adSdrh int b; 2931039963adSdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 2932039963adSdrh if( b ){ 2933039963adSdrh flags |= SQLITE_OPEN_FULLMUTEX; 2934039963adSdrh flags &= ~SQLITE_OPEN_NOMUTEX; 2935039963adSdrh }else{ 2936039963adSdrh flags &= ~SQLITE_OPEN_FULLMUTEX; 2937039963adSdrh } 29383570ad93Sdrh }else{ 29393570ad93Sdrh Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0); 29403570ad93Sdrh return TCL_ERROR; 294122fbcb8dSdrh } 294222fbcb8dSdrh } 29433570ad93Sdrh if( objc<3 || (objc&1)!=1 ){ 294422fbcb8dSdrh Tcl_WrongNumArgs(interp, 1, objv, 29453570ad93Sdrh "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" 2946039963adSdrh " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN?" 29479eb9e26bSdrh #ifdef SQLITE_HAS_CODEC 29483570ad93Sdrh " ?-key CODECKEY?" 294922fbcb8dSdrh #endif 295022fbcb8dSdrh ); 295175897234Sdrh return TCL_ERROR; 295275897234Sdrh } 295375897234Sdrh zErrMsg = 0; 29544cdc9e84Sdrh p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); 295575897234Sdrh if( p==0 ){ 2956bec3f402Sdrh Tcl_SetResult(interp, "malloc failed", TCL_STATIC); 2957bec3f402Sdrh return TCL_ERROR; 2958bec3f402Sdrh } 2959bec3f402Sdrh memset(p, 0, sizeof(*p)); 296022fbcb8dSdrh zFile = Tcl_GetStringFromObj(objv[2], 0); 2961882e8e4dSdrh zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); 29623570ad93Sdrh sqlite3_open_v2(zFile, &p->db, flags, zVfs); 2963882e8e4dSdrh Tcl_DStringFree(&translatedFilename); 296480290863Sdanielk1977 if( SQLITE_OK!=sqlite3_errcode(p->db) ){ 29659404d50eSdrh zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); 296680290863Sdanielk1977 sqlite3_close(p->db); 296780290863Sdanielk1977 p->db = 0; 296880290863Sdanielk1977 } 29692011d5f5Sdrh #ifdef SQLITE_HAS_CODEC 2970f3a65f7eSdrh if( p->db ){ 29712011d5f5Sdrh sqlite3_key(p->db, pKey, nKey); 2972f3a65f7eSdrh } 2973eb8ed70dSdrh #endif 2974bec3f402Sdrh if( p->db==0 ){ 297575897234Sdrh Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 2976bec3f402Sdrh Tcl_Free((char*)p); 29779404d50eSdrh sqlite3_free(zErrMsg); 297875897234Sdrh return TCL_ERROR; 297975897234Sdrh } 2980fb7e7651Sdrh p->maxStmt = NUM_PREPARED_STMTS; 29815169bbc6Sdrh p->interp = interp; 298222fbcb8dSdrh zArg = Tcl_GetStringFromObj(objv[1], 0); 29834a4c11aaSdan if( DbUseNre() ){ 2984a2c8a95bSdrh Tcl_NRCreateCommand(interp, zArg, DbObjCmdAdaptor, DbObjCmd, 2985a2c8a95bSdrh (char*)p, DbDeleteCmd); 29864a4c11aaSdan }else{ 298722fbcb8dSdrh Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); 29884a4c11aaSdan } 298975897234Sdrh return TCL_OK; 299075897234Sdrh } 299175897234Sdrh 299275897234Sdrh /* 299390ca9753Sdrh ** Provide a dummy Tcl_InitStubs if we are using this as a static 299490ca9753Sdrh ** library. 299590ca9753Sdrh */ 299690ca9753Sdrh #ifndef USE_TCL_STUBS 299790ca9753Sdrh # undef Tcl_InitStubs 299890ca9753Sdrh # define Tcl_InitStubs(a,b,c) 299990ca9753Sdrh #endif 300090ca9753Sdrh 300190ca9753Sdrh /* 300229bc4615Sdrh ** Make sure we have a PACKAGE_VERSION macro defined. This will be 300329bc4615Sdrh ** defined automatically by the TEA makefile. But other makefiles 300429bc4615Sdrh ** do not define it. 300529bc4615Sdrh */ 300629bc4615Sdrh #ifndef PACKAGE_VERSION 300729bc4615Sdrh # define PACKAGE_VERSION SQLITE_VERSION 300829bc4615Sdrh #endif 300929bc4615Sdrh 301029bc4615Sdrh /* 301175897234Sdrh ** Initialize this module. 301275897234Sdrh ** 301375897234Sdrh ** This Tcl module contains only a single new Tcl command named "sqlite". 301475897234Sdrh ** (Hence there is no namespace. There is no point in using a namespace 301575897234Sdrh ** if the extension only supplies one new name!) The "sqlite" command is 301675897234Sdrh ** used to open a new SQLite database. See the DbMain() routine above 301775897234Sdrh ** for additional information. 3018b652f432Sdrh ** 3019b652f432Sdrh ** The EXTERN macros are required by TCL in order to work on windows. 302075897234Sdrh */ 3021b652f432Sdrh EXTERN int Sqlite3_Init(Tcl_Interp *interp){ 302292febd92Sdrh Tcl_InitStubs(interp, "8.4", 0); 3023ef4ac8f9Sdrh Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); 302429bc4615Sdrh Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION); 30251cca0d22Sdrh 30261cca0d22Sdrh #ifndef SQLITE_3_SUFFIX_ONLY 30271cca0d22Sdrh /* The "sqlite" alias is undocumented. It is here only to support 30281cca0d22Sdrh ** legacy scripts. All new scripts should use only the "sqlite3" 30291cca0d22Sdrh ** command. 30301cca0d22Sdrh */ 303149766d6cSdrh Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); 30324c0f1649Sdrh #endif 30331cca0d22Sdrh 303490ca9753Sdrh return TCL_OK; 303590ca9753Sdrh } 3036b652f432Sdrh EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3037b652f432Sdrh EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; } 3038b652f432Sdrh EXTERN int Tclsqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; } 3039b652f432Sdrh EXTERN int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3040b652f432Sdrh EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3041b652f432Sdrh EXTERN int Sqlite3_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3042b652f432Sdrh EXTERN int Tclsqlite3_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK;} 3043e2c3a659Sdrh 304449766d6cSdrh 304549766d6cSdrh #ifndef SQLITE_3_SUFFIX_ONLY 3046a3e63c4aSdan int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3047a3e63c4aSdan int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3048a3e63c4aSdan int Sqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } 3049a3e63c4aSdan int Tclsqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } 3050a3e63c4aSdan int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3051a3e63c4aSdan int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3052a3e63c4aSdan int Sqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3053a3e63c4aSdan int Tclsqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK;} 305449766d6cSdrh #endif 305575897234Sdrh 30563e27c026Sdrh #ifdef TCLSH 30573e27c026Sdrh /***************************************************************************** 305857a0227fSdrh ** All of the code that follows is used to build standalone TCL interpreters 305957a0227fSdrh ** that are statically linked with SQLite. Enable these by compiling 306057a0227fSdrh ** with -DTCLSH=n where n can be 1 or 2. An n of 1 generates a standard 306157a0227fSdrh ** tclsh but with SQLite built in. An n of 2 generates the SQLite space 306257a0227fSdrh ** analysis program. 306375897234Sdrh */ 3064348784efSdrh 306557a0227fSdrh #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) 306657a0227fSdrh /* 306757a0227fSdrh * This code implements the MD5 message-digest algorithm. 306857a0227fSdrh * The algorithm is due to Ron Rivest. This code was 306957a0227fSdrh * written by Colin Plumb in 1993, no copyright is claimed. 307057a0227fSdrh * This code is in the public domain; do with it what you wish. 307157a0227fSdrh * 307257a0227fSdrh * Equivalent code is available from RSA Data Security, Inc. 307357a0227fSdrh * This code has been tested against that, and is equivalent, 307457a0227fSdrh * except that you don't need to include two pages of legalese 307557a0227fSdrh * with every copy. 307657a0227fSdrh * 307757a0227fSdrh * To compute the message digest of a chunk of bytes, declare an 307857a0227fSdrh * MD5Context structure, pass it to MD5Init, call MD5Update as 307957a0227fSdrh * needed on buffers full of bytes, and then call MD5Final, which 308057a0227fSdrh * will fill a supplied 16-byte array with the digest. 308157a0227fSdrh */ 308257a0227fSdrh 308357a0227fSdrh /* 308457a0227fSdrh * If compiled on a machine that doesn't have a 32-bit integer, 308557a0227fSdrh * you just set "uint32" to the appropriate datatype for an 308657a0227fSdrh * unsigned 32-bit integer. For example: 308757a0227fSdrh * 308857a0227fSdrh * cc -Duint32='unsigned long' md5.c 308957a0227fSdrh * 309057a0227fSdrh */ 309157a0227fSdrh #ifndef uint32 309257a0227fSdrh # define uint32 unsigned int 309357a0227fSdrh #endif 309457a0227fSdrh 309557a0227fSdrh struct MD5Context { 309657a0227fSdrh int isInit; 309757a0227fSdrh uint32 buf[4]; 309857a0227fSdrh uint32 bits[2]; 309957a0227fSdrh unsigned char in[64]; 310057a0227fSdrh }; 310157a0227fSdrh typedef struct MD5Context MD5Context; 310257a0227fSdrh 310357a0227fSdrh /* 310457a0227fSdrh * Note: this code is harmless on little-endian machines. 310557a0227fSdrh */ 310657a0227fSdrh static void byteReverse (unsigned char *buf, unsigned longs){ 310757a0227fSdrh uint32 t; 310857a0227fSdrh do { 310957a0227fSdrh t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 | 311057a0227fSdrh ((unsigned)buf[1]<<8 | buf[0]); 311157a0227fSdrh *(uint32 *)buf = t; 311257a0227fSdrh buf += 4; 311357a0227fSdrh } while (--longs); 311457a0227fSdrh } 311557a0227fSdrh /* The four core functions - F1 is optimized somewhat */ 311657a0227fSdrh 311757a0227fSdrh /* #define F1(x, y, z) (x & y | ~x & z) */ 311857a0227fSdrh #define F1(x, y, z) (z ^ (x & (y ^ z))) 311957a0227fSdrh #define F2(x, y, z) F1(z, x, y) 312057a0227fSdrh #define F3(x, y, z) (x ^ y ^ z) 312157a0227fSdrh #define F4(x, y, z) (y ^ (x | ~z)) 312257a0227fSdrh 312357a0227fSdrh /* This is the central step in the MD5 algorithm. */ 312457a0227fSdrh #define MD5STEP(f, w, x, y, z, data, s) \ 312557a0227fSdrh ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) 312657a0227fSdrh 312757a0227fSdrh /* 312857a0227fSdrh * The core of the MD5 algorithm, this alters an existing MD5 hash to 312957a0227fSdrh * reflect the addition of 16 longwords of new data. MD5Update blocks 313057a0227fSdrh * the data and converts bytes into longwords for this routine. 313157a0227fSdrh */ 313257a0227fSdrh static void MD5Transform(uint32 buf[4], const uint32 in[16]){ 313357a0227fSdrh register uint32 a, b, c, d; 313457a0227fSdrh 313557a0227fSdrh a = buf[0]; 313657a0227fSdrh b = buf[1]; 313757a0227fSdrh c = buf[2]; 313857a0227fSdrh d = buf[3]; 313957a0227fSdrh 314057a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); 314157a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); 314257a0227fSdrh MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); 314357a0227fSdrh MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); 314457a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); 314557a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); 314657a0227fSdrh MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); 314757a0227fSdrh MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); 314857a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); 314957a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); 315057a0227fSdrh MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); 315157a0227fSdrh MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); 315257a0227fSdrh MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); 315357a0227fSdrh MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); 315457a0227fSdrh MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); 315557a0227fSdrh MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); 315657a0227fSdrh 315757a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); 315857a0227fSdrh MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); 315957a0227fSdrh MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); 316057a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); 316157a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); 316257a0227fSdrh MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); 316357a0227fSdrh MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); 316457a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); 316557a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); 316657a0227fSdrh MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); 316757a0227fSdrh MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); 316857a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); 316957a0227fSdrh MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); 317057a0227fSdrh MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); 317157a0227fSdrh MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); 317257a0227fSdrh MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); 317357a0227fSdrh 317457a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); 317557a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); 317657a0227fSdrh MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); 317757a0227fSdrh MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); 317857a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); 317957a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); 318057a0227fSdrh MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); 318157a0227fSdrh MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); 318257a0227fSdrh MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); 318357a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); 318457a0227fSdrh MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); 318557a0227fSdrh MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); 318657a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); 318757a0227fSdrh MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); 318857a0227fSdrh MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); 318957a0227fSdrh MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); 319057a0227fSdrh 319157a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); 319257a0227fSdrh MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); 319357a0227fSdrh MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); 319457a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); 319557a0227fSdrh MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); 319657a0227fSdrh MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); 319757a0227fSdrh MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); 319857a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); 319957a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); 320057a0227fSdrh MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); 320157a0227fSdrh MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); 320257a0227fSdrh MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); 320357a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); 320457a0227fSdrh MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); 320557a0227fSdrh MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); 320657a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); 320757a0227fSdrh 320857a0227fSdrh buf[0] += a; 320957a0227fSdrh buf[1] += b; 321057a0227fSdrh buf[2] += c; 321157a0227fSdrh buf[3] += d; 321257a0227fSdrh } 321357a0227fSdrh 321457a0227fSdrh /* 321557a0227fSdrh * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 321657a0227fSdrh * initialization constants. 321757a0227fSdrh */ 321857a0227fSdrh static void MD5Init(MD5Context *ctx){ 321957a0227fSdrh ctx->isInit = 1; 322057a0227fSdrh ctx->buf[0] = 0x67452301; 322157a0227fSdrh ctx->buf[1] = 0xefcdab89; 322257a0227fSdrh ctx->buf[2] = 0x98badcfe; 322357a0227fSdrh ctx->buf[3] = 0x10325476; 322457a0227fSdrh ctx->bits[0] = 0; 322557a0227fSdrh ctx->bits[1] = 0; 322657a0227fSdrh } 322757a0227fSdrh 322857a0227fSdrh /* 322957a0227fSdrh * Update context to reflect the concatenation of another buffer full 323057a0227fSdrh * of bytes. 323157a0227fSdrh */ 323257a0227fSdrh static 323357a0227fSdrh void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){ 323457a0227fSdrh uint32 t; 323557a0227fSdrh 323657a0227fSdrh /* Update bitcount */ 323757a0227fSdrh 323857a0227fSdrh t = ctx->bits[0]; 323957a0227fSdrh if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) 324057a0227fSdrh ctx->bits[1]++; /* Carry from low to high */ 324157a0227fSdrh ctx->bits[1] += len >> 29; 324257a0227fSdrh 324357a0227fSdrh t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ 324457a0227fSdrh 324557a0227fSdrh /* Handle any leading odd-sized chunks */ 324657a0227fSdrh 324757a0227fSdrh if ( t ) { 324857a0227fSdrh unsigned char *p = (unsigned char *)ctx->in + t; 324957a0227fSdrh 325057a0227fSdrh t = 64-t; 325157a0227fSdrh if (len < t) { 325257a0227fSdrh memcpy(p, buf, len); 325357a0227fSdrh return; 325457a0227fSdrh } 325557a0227fSdrh memcpy(p, buf, t); 325657a0227fSdrh byteReverse(ctx->in, 16); 325757a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 325857a0227fSdrh buf += t; 325957a0227fSdrh len -= t; 326057a0227fSdrh } 326157a0227fSdrh 326257a0227fSdrh /* Process data in 64-byte chunks */ 326357a0227fSdrh 326457a0227fSdrh while (len >= 64) { 326557a0227fSdrh memcpy(ctx->in, buf, 64); 326657a0227fSdrh byteReverse(ctx->in, 16); 326757a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 326857a0227fSdrh buf += 64; 326957a0227fSdrh len -= 64; 327057a0227fSdrh } 327157a0227fSdrh 327257a0227fSdrh /* Handle any remaining bytes of data. */ 327357a0227fSdrh 327457a0227fSdrh memcpy(ctx->in, buf, len); 327557a0227fSdrh } 327657a0227fSdrh 327757a0227fSdrh /* 327857a0227fSdrh * Final wrapup - pad to 64-byte boundary with the bit pattern 327957a0227fSdrh * 1 0* (64-bit count of bits processed, MSB-first) 328057a0227fSdrh */ 328157a0227fSdrh static void MD5Final(unsigned char digest[16], MD5Context *ctx){ 328257a0227fSdrh unsigned count; 328357a0227fSdrh unsigned char *p; 328457a0227fSdrh 328557a0227fSdrh /* Compute number of bytes mod 64 */ 328657a0227fSdrh count = (ctx->bits[0] >> 3) & 0x3F; 328757a0227fSdrh 328857a0227fSdrh /* Set the first char of padding to 0x80. This is safe since there is 328957a0227fSdrh always at least one byte free */ 329057a0227fSdrh p = ctx->in + count; 329157a0227fSdrh *p++ = 0x80; 329257a0227fSdrh 329357a0227fSdrh /* Bytes of padding needed to make 64 bytes */ 329457a0227fSdrh count = 64 - 1 - count; 329557a0227fSdrh 329657a0227fSdrh /* Pad out to 56 mod 64 */ 329757a0227fSdrh if (count < 8) { 329857a0227fSdrh /* Two lots of padding: Pad the first block to 64 bytes */ 329957a0227fSdrh memset(p, 0, count); 330057a0227fSdrh byteReverse(ctx->in, 16); 330157a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 330257a0227fSdrh 330357a0227fSdrh /* Now fill the next block with 56 bytes */ 330457a0227fSdrh memset(ctx->in, 0, 56); 330557a0227fSdrh } else { 330657a0227fSdrh /* Pad block to 56 bytes */ 330757a0227fSdrh memset(p, 0, count-8); 330857a0227fSdrh } 330957a0227fSdrh byteReverse(ctx->in, 14); 331057a0227fSdrh 331157a0227fSdrh /* Append length in bits and transform */ 331257a0227fSdrh ((uint32 *)ctx->in)[ 14 ] = ctx->bits[0]; 331357a0227fSdrh ((uint32 *)ctx->in)[ 15 ] = ctx->bits[1]; 331457a0227fSdrh 331557a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 331657a0227fSdrh byteReverse((unsigned char *)ctx->buf, 4); 331757a0227fSdrh memcpy(digest, ctx->buf, 16); 331857a0227fSdrh memset(ctx, 0, sizeof(ctx)); /* In case it is sensitive */ 331957a0227fSdrh } 332057a0227fSdrh 332157a0227fSdrh /* 332257a0227fSdrh ** Convert a 128-bit MD5 digest into a 32-digit base-16 number. 332357a0227fSdrh */ 332457a0227fSdrh static void MD5DigestToBase16(unsigned char *digest, char *zBuf){ 332557a0227fSdrh static char const zEncode[] = "0123456789abcdef"; 332657a0227fSdrh int i, j; 332757a0227fSdrh 332857a0227fSdrh for(j=i=0; i<16; i++){ 332957a0227fSdrh int a = digest[i]; 333057a0227fSdrh zBuf[j++] = zEncode[(a>>4)&0xf]; 333157a0227fSdrh zBuf[j++] = zEncode[a & 0xf]; 333257a0227fSdrh } 333357a0227fSdrh zBuf[j] = 0; 333457a0227fSdrh } 333557a0227fSdrh 333657a0227fSdrh 333757a0227fSdrh /* 333857a0227fSdrh ** Convert a 128-bit MD5 digest into sequency of eight 5-digit integers 333957a0227fSdrh ** each representing 16 bits of the digest and separated from each 334057a0227fSdrh ** other by a "-" character. 334157a0227fSdrh */ 334257a0227fSdrh static void MD5DigestToBase10x8(unsigned char digest[16], char zDigest[50]){ 334357a0227fSdrh int i, j; 334457a0227fSdrh unsigned int x; 334557a0227fSdrh for(i=j=0; i<16; i+=2){ 334657a0227fSdrh x = digest[i]*256 + digest[i+1]; 334757a0227fSdrh if( i>0 ) zDigest[j++] = '-'; 334857a0227fSdrh sprintf(&zDigest[j], "%05u", x); 334957a0227fSdrh j += 5; 335057a0227fSdrh } 335157a0227fSdrh zDigest[j] = 0; 335257a0227fSdrh } 335357a0227fSdrh 335457a0227fSdrh /* 335557a0227fSdrh ** A TCL command for md5. The argument is the text to be hashed. The 335657a0227fSdrh ** Result is the hash in base64. 335757a0227fSdrh */ 335857a0227fSdrh static int md5_cmd(void*cd, Tcl_Interp *interp, int argc, const char **argv){ 335957a0227fSdrh MD5Context ctx; 336057a0227fSdrh unsigned char digest[16]; 336157a0227fSdrh char zBuf[50]; 336257a0227fSdrh void (*converter)(unsigned char*, char*); 336357a0227fSdrh 336457a0227fSdrh if( argc!=2 ){ 336557a0227fSdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 336657a0227fSdrh " TEXT\"", 0); 336757a0227fSdrh return TCL_ERROR; 336857a0227fSdrh } 336957a0227fSdrh MD5Init(&ctx); 337057a0227fSdrh MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1])); 337157a0227fSdrh MD5Final(digest, &ctx); 337257a0227fSdrh converter = (void(*)(unsigned char*,char*))cd; 337357a0227fSdrh converter(digest, zBuf); 337457a0227fSdrh Tcl_AppendResult(interp, zBuf, (char*)0); 337557a0227fSdrh return TCL_OK; 337657a0227fSdrh } 337757a0227fSdrh 337857a0227fSdrh /* 337957a0227fSdrh ** A TCL command to take the md5 hash of a file. The argument is the 338057a0227fSdrh ** name of the file. 338157a0227fSdrh */ 338257a0227fSdrh static int md5file_cmd(void*cd, Tcl_Interp*interp, int argc, const char **argv){ 338357a0227fSdrh FILE *in; 338457a0227fSdrh MD5Context ctx; 338557a0227fSdrh void (*converter)(unsigned char*, char*); 338657a0227fSdrh unsigned char digest[16]; 338757a0227fSdrh char zBuf[10240]; 338857a0227fSdrh 338957a0227fSdrh if( argc!=2 ){ 339057a0227fSdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 339157a0227fSdrh " FILENAME\"", 0); 339257a0227fSdrh return TCL_ERROR; 339357a0227fSdrh } 339457a0227fSdrh in = fopen(argv[1],"rb"); 339557a0227fSdrh if( in==0 ){ 339657a0227fSdrh Tcl_AppendResult(interp,"unable to open file \"", argv[1], 339757a0227fSdrh "\" for reading", 0); 339857a0227fSdrh return TCL_ERROR; 339957a0227fSdrh } 340057a0227fSdrh MD5Init(&ctx); 340157a0227fSdrh for(;;){ 340257a0227fSdrh int n; 340357a0227fSdrh n = fread(zBuf, 1, sizeof(zBuf), in); 340457a0227fSdrh if( n<=0 ) break; 340557a0227fSdrh MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n); 340657a0227fSdrh } 340757a0227fSdrh fclose(in); 340857a0227fSdrh MD5Final(digest, &ctx); 340957a0227fSdrh converter = (void(*)(unsigned char*,char*))cd; 341057a0227fSdrh converter(digest, zBuf); 341157a0227fSdrh Tcl_AppendResult(interp, zBuf, (char*)0); 341257a0227fSdrh return TCL_OK; 341357a0227fSdrh } 341457a0227fSdrh 341557a0227fSdrh /* 341657a0227fSdrh ** Register the four new TCL commands for generating MD5 checksums 341757a0227fSdrh ** with the TCL interpreter. 341857a0227fSdrh */ 341957a0227fSdrh int Md5_Init(Tcl_Interp *interp){ 342057a0227fSdrh Tcl_CreateCommand(interp, "md5", (Tcl_CmdProc*)md5_cmd, 342157a0227fSdrh MD5DigestToBase16, 0); 342257a0227fSdrh Tcl_CreateCommand(interp, "md5-10x8", (Tcl_CmdProc*)md5_cmd, 342357a0227fSdrh MD5DigestToBase10x8, 0); 342457a0227fSdrh Tcl_CreateCommand(interp, "md5file", (Tcl_CmdProc*)md5file_cmd, 342557a0227fSdrh MD5DigestToBase16, 0); 342657a0227fSdrh Tcl_CreateCommand(interp, "md5file-10x8", (Tcl_CmdProc*)md5file_cmd, 342757a0227fSdrh MD5DigestToBase10x8, 0); 342857a0227fSdrh return TCL_OK; 342957a0227fSdrh } 343057a0227fSdrh #endif /* defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) */ 343157a0227fSdrh 343257a0227fSdrh #if defined(SQLITE_TEST) 343357a0227fSdrh /* 343457a0227fSdrh ** During testing, the special md5sum() aggregate function is available. 343557a0227fSdrh ** inside SQLite. The following routines implement that function. 343657a0227fSdrh */ 343757a0227fSdrh static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){ 343857a0227fSdrh MD5Context *p; 343957a0227fSdrh int i; 344057a0227fSdrh if( argc<1 ) return; 344157a0227fSdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 344257a0227fSdrh if( p==0 ) return; 344357a0227fSdrh if( !p->isInit ){ 344457a0227fSdrh MD5Init(p); 344557a0227fSdrh } 344657a0227fSdrh for(i=0; i<argc; i++){ 344757a0227fSdrh const char *zData = (char*)sqlite3_value_text(argv[i]); 344857a0227fSdrh if( zData ){ 344957a0227fSdrh MD5Update(p, (unsigned char*)zData, strlen(zData)); 345057a0227fSdrh } 345157a0227fSdrh } 345257a0227fSdrh } 345357a0227fSdrh static void md5finalize(sqlite3_context *context){ 345457a0227fSdrh MD5Context *p; 345557a0227fSdrh unsigned char digest[16]; 345657a0227fSdrh char zBuf[33]; 345757a0227fSdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 345857a0227fSdrh MD5Final(digest,p); 345957a0227fSdrh MD5DigestToBase16(digest, zBuf); 346057a0227fSdrh sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 346157a0227fSdrh } 346257a0227fSdrh int Md5_Register(sqlite3 *db){ 346357a0227fSdrh int rc = sqlite3_create_function(db, "md5sum", -1, SQLITE_UTF8, 0, 0, 346457a0227fSdrh md5step, md5finalize); 346557a0227fSdrh sqlite3_overload_function(db, "md5sum", -1); /* To exercise this API */ 346657a0227fSdrh return rc; 346757a0227fSdrh } 346857a0227fSdrh #endif /* defined(SQLITE_TEST) */ 346957a0227fSdrh 347057a0227fSdrh 3471348784efSdrh /* 34723e27c026Sdrh ** If the macro TCLSH is one, then put in code this for the 34733e27c026Sdrh ** "main" routine that will initialize Tcl and take input from 34743570ad93Sdrh ** standard input, or if a file is named on the command line 34753570ad93Sdrh ** the TCL interpreter reads and evaluates that file. 3476348784efSdrh */ 34773e27c026Sdrh #if TCLSH==1 3478348784efSdrh static char zMainloop[] = 3479348784efSdrh "set line {}\n" 3480348784efSdrh "while {![eof stdin]} {\n" 3481348784efSdrh "if {$line!=\"\"} {\n" 3482348784efSdrh "puts -nonewline \"> \"\n" 3483348784efSdrh "} else {\n" 3484348784efSdrh "puts -nonewline \"% \"\n" 3485348784efSdrh "}\n" 3486348784efSdrh "flush stdout\n" 3487348784efSdrh "append line [gets stdin]\n" 3488348784efSdrh "if {[info complete $line]} {\n" 3489348784efSdrh "if {[catch {uplevel #0 $line} result]} {\n" 3490348784efSdrh "puts stderr \"Error: $result\"\n" 3491348784efSdrh "} elseif {$result!=\"\"} {\n" 3492348784efSdrh "puts $result\n" 3493348784efSdrh "}\n" 3494348784efSdrh "set line {}\n" 3495348784efSdrh "} else {\n" 3496348784efSdrh "append line \\n\n" 3497348784efSdrh "}\n" 3498348784efSdrh "}\n" 3499348784efSdrh ; 35003e27c026Sdrh #endif 35013a0f13ffSdrh #if TCLSH==2 35023a0f13ffSdrh static char zMainloop[] = 35033a0f13ffSdrh #include "spaceanal_tcl.h" 35043a0f13ffSdrh ; 35053a0f13ffSdrh #endif 35063e27c026Sdrh 3507c1a60c51Sdan #ifdef SQLITE_TEST 3508c1a60c51Sdan static void init_all(Tcl_Interp *); 3509c1a60c51Sdan static int init_all_cmd( 3510c1a60c51Sdan ClientData cd, 3511c1a60c51Sdan Tcl_Interp *interp, 3512c1a60c51Sdan int objc, 3513c1a60c51Sdan Tcl_Obj *CONST objv[] 3514c1a60c51Sdan ){ 35150a549071Sdanielk1977 3516c1a60c51Sdan Tcl_Interp *slave; 3517c1a60c51Sdan if( objc!=2 ){ 3518c1a60c51Sdan Tcl_WrongNumArgs(interp, 1, objv, "SLAVE"); 3519c1a60c51Sdan return TCL_ERROR; 3520c1a60c51Sdan } 35210a549071Sdanielk1977 3522c1a60c51Sdan slave = Tcl_GetSlave(interp, Tcl_GetString(objv[1])); 3523c1a60c51Sdan if( !slave ){ 3524c1a60c51Sdan return TCL_ERROR; 3525c1a60c51Sdan } 3526c1a60c51Sdan 3527c1a60c51Sdan init_all(slave); 3528c1a60c51Sdan return TCL_OK; 3529c1a60c51Sdan } 3530c1a60c51Sdan #endif 3531c1a60c51Sdan 3532c1a60c51Sdan /* 3533c1a60c51Sdan ** Configure the interpreter passed as the first argument to have access 3534c1a60c51Sdan ** to the commands and linked variables that make up: 3535c1a60c51Sdan ** 3536c1a60c51Sdan ** * the [sqlite3] extension itself, 3537c1a60c51Sdan ** 3538c1a60c51Sdan ** * If SQLITE_TCLMD5 or SQLITE_TEST is defined, the Md5 commands, and 3539c1a60c51Sdan ** 3540c1a60c51Sdan ** * If SQLITE_TEST is set, the various test interfaces used by the Tcl 3541c1a60c51Sdan ** test suite. 3542c1a60c51Sdan */ 3543c1a60c51Sdan static void init_all(Tcl_Interp *interp){ 354438f8271fSdrh Sqlite3_Init(interp); 3545c1a60c51Sdan 354657a0227fSdrh #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) 354757a0227fSdrh Md5_Init(interp); 354857a0227fSdrh #endif 3549c1a60c51Sdan 3550d9b0257aSdrh #ifdef SQLITE_TEST 3551d1bf3512Sdrh { 35522f999a67Sdrh extern int Sqliteconfig_Init(Tcl_Interp*); 3553d1bf3512Sdrh extern int Sqlitetest1_Init(Tcl_Interp*); 35545c4d9703Sdrh extern int Sqlitetest2_Init(Tcl_Interp*); 35555c4d9703Sdrh extern int Sqlitetest3_Init(Tcl_Interp*); 3556a6064dcfSdrh extern int Sqlitetest4_Init(Tcl_Interp*); 3557998b56c3Sdanielk1977 extern int Sqlitetest5_Init(Tcl_Interp*); 35589c06c953Sdrh extern int Sqlitetest6_Init(Tcl_Interp*); 355929c636bcSdrh extern int Sqlitetest7_Init(Tcl_Interp*); 3560b9bb7c18Sdrh extern int Sqlitetest8_Init(Tcl_Interp*); 3561a713f2c3Sdanielk1977 extern int Sqlitetest9_Init(Tcl_Interp*); 35622366940dSdrh extern int Sqlitetestasync_Init(Tcl_Interp*); 35631409be69Sdrh extern int Sqlitetest_autoext_Init(Tcl_Interp*); 35640a7a9155Sdan extern int Sqlitetest_demovfs_Init(Tcl_Interp *); 3565984bfaa4Sdrh extern int Sqlitetest_func_Init(Tcl_Interp*); 356615926590Sdrh extern int Sqlitetest_hexio_Init(Tcl_Interp*); 3567e1ab2193Sdan extern int Sqlitetest_init_Init(Tcl_Interp*); 35682f999a67Sdrh extern int Sqlitetest_malloc_Init(Tcl_Interp*); 35691a9ed0b2Sdanielk1977 extern int Sqlitetest_mutex_Init(Tcl_Interp*); 35702f999a67Sdrh extern int Sqlitetestschema_Init(Tcl_Interp*); 35712f999a67Sdrh extern int Sqlitetestsse_Init(Tcl_Interp*); 35722f999a67Sdrh extern int Sqlitetesttclvar_Init(Tcl_Interp*); 357344918fa0Sdanielk1977 extern int SqlitetestThread_Init(Tcl_Interp*); 3574a15db353Sdanielk1977 extern int SqlitetestOnefile_Init(); 35755d1f5aa6Sdanielk1977 extern int SqlitetestOsinst_Init(Tcl_Interp*); 35760410302eSdanielk1977 extern int Sqlitetestbackup_Init(Tcl_Interp*); 3577522efc62Sdrh extern int Sqlitetestintarray_Init(Tcl_Interp*); 3578c7991bdfSdan extern int Sqlitetestvfs_Init(Tcl_Interp *); 3579599e9d21Sdan extern int SqlitetestStat_Init(Tcl_Interp*); 35809508daa9Sdan extern int Sqlitetestrtree_Init(Tcl_Interp*); 35818cf35eb4Sdan extern int Sqlitequota_Init(Tcl_Interp*); 35828a922f75Sshaneh extern int Sqlitemultiplex_Init(Tcl_Interp*); 3583e336b001Sdan extern int SqliteSuperlock_Init(Tcl_Interp*); 35842e66f0b9Sdrh 3585*b29010cdSdan #ifdef SQLITE_ENABLE_ZIPVFS 3586*b29010cdSdan extern int Zipvfs_Init(Tcl_Interp*); 3587*b29010cdSdan Zipvfs_Init(interp); 3588*b29010cdSdan #endif 3589*b29010cdSdan 35902f999a67Sdrh Sqliteconfig_Init(interp); 35916490bebdSdanielk1977 Sqlitetest1_Init(interp); 35925c4d9703Sdrh Sqlitetest2_Init(interp); 3593de647130Sdrh Sqlitetest3_Init(interp); 3594fc57d7bfSdanielk1977 Sqlitetest4_Init(interp); 3595998b56c3Sdanielk1977 Sqlitetest5_Init(interp); 35969c06c953Sdrh Sqlitetest6_Init(interp); 359729c636bcSdrh Sqlitetest7_Init(interp); 3598b9bb7c18Sdrh Sqlitetest8_Init(interp); 3599a713f2c3Sdanielk1977 Sqlitetest9_Init(interp); 36002366940dSdrh Sqlitetestasync_Init(interp); 36011409be69Sdrh Sqlitetest_autoext_Init(interp); 36020a7a9155Sdan Sqlitetest_demovfs_Init(interp); 3603984bfaa4Sdrh Sqlitetest_func_Init(interp); 360415926590Sdrh Sqlitetest_hexio_Init(interp); 3605e1ab2193Sdan Sqlitetest_init_Init(interp); 36062f999a67Sdrh Sqlitetest_malloc_Init(interp); 36071a9ed0b2Sdanielk1977 Sqlitetest_mutex_Init(interp); 36082f999a67Sdrh Sqlitetestschema_Init(interp); 36092f999a67Sdrh Sqlitetesttclvar_Init(interp); 361044918fa0Sdanielk1977 SqlitetestThread_Init(interp); 3611a15db353Sdanielk1977 SqlitetestOnefile_Init(interp); 36125d1f5aa6Sdanielk1977 SqlitetestOsinst_Init(interp); 36130410302eSdanielk1977 Sqlitetestbackup_Init(interp); 3614522efc62Sdrh Sqlitetestintarray_Init(interp); 3615c7991bdfSdan Sqlitetestvfs_Init(interp); 3616599e9d21Sdan SqlitetestStat_Init(interp); 36179508daa9Sdan Sqlitetestrtree_Init(interp); 36188cf35eb4Sdan Sqlitequota_Init(interp); 36198a922f75Sshaneh Sqlitemultiplex_Init(interp); 3620e336b001Sdan SqliteSuperlock_Init(interp); 3621a15db353Sdanielk1977 3622c1a60c51Sdan Tcl_CreateObjCommand(interp,"load_testfixture_extensions",init_all_cmd,0,0); 3623c1a60c51Sdan 362489dec819Sdrh #ifdef SQLITE_SSE 36252e66f0b9Sdrh Sqlitetestsse_Init(interp); 36262e66f0b9Sdrh #endif 3627d1bf3512Sdrh } 3628d1bf3512Sdrh #endif 3629c1a60c51Sdan } 3630c1a60c51Sdan 3631c1a60c51Sdan #define TCLSH_MAIN main /* Needed to fake out mktclapp */ 3632c1a60c51Sdan int TCLSH_MAIN(int argc, char **argv){ 3633c1a60c51Sdan Tcl_Interp *interp; 3634c1a60c51Sdan 3635c1a60c51Sdan /* Call sqlite3_shutdown() once before doing anything else. This is to 3636c1a60c51Sdan ** test that sqlite3_shutdown() can be safely called by a process before 3637c1a60c51Sdan ** sqlite3_initialize() is. */ 3638c1a60c51Sdan sqlite3_shutdown(); 3639c1a60c51Sdan 36403a0f13ffSdrh #if TCLSH==2 36413a0f13ffSdrh sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); 36423a0f13ffSdrh #endif 3643c1a60c51Sdan Tcl_FindExecutable(argv[0]); 3644c1a60c51Sdan 3645c1a60c51Sdan interp = Tcl_CreateInterp(); 3646c1a60c51Sdan init_all(interp); 3647c7285978Sdrh if( argc>=2 ){ 3648348784efSdrh int i; 3649ad42c3a3Sshess char zArgc[32]; 3650ad42c3a3Sshess sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH)); 3651ad42c3a3Sshess Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY); 3652348784efSdrh Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); 3653348784efSdrh Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); 365461212b69Sdrh for(i=3-TCLSH; i<argc; i++){ 3655348784efSdrh Tcl_SetVar(interp, "argv", argv[i], 3656348784efSdrh TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); 3657348784efSdrh } 36583a0f13ffSdrh if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ 36590de8c112Sdrh const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); 3660a81c64a2Sdrh if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp); 3661c61053b7Sdrh fprintf(stderr,"%s: %s\n", *argv, zInfo); 3662348784efSdrh return 1; 3663348784efSdrh } 36643e27c026Sdrh } 36653a0f13ffSdrh if( TCLSH==2 || argc<=1 ){ 3666348784efSdrh Tcl_GlobalEval(interp, zMainloop); 3667348784efSdrh } 3668348784efSdrh return 0; 3669348784efSdrh } 3670348784efSdrh #endif /* TCLSH */ 3671