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 56c45e6716Sdrh /* Forward declaration */ 57c45e6716Sdrh typedef struct SqliteDb SqliteDb; 5898808babSdrh 5998808babSdrh /* 60cabb0819Sdrh ** New SQL functions can be created as TCL scripts. Each such function 61cabb0819Sdrh ** is described by an instance of the following structure. 62cabb0819Sdrh */ 63cabb0819Sdrh typedef struct SqlFunc SqlFunc; 64cabb0819Sdrh struct SqlFunc { 65cabb0819Sdrh Tcl_Interp *interp; /* The TCL interpret to execute the function */ 66d1e4733dSdrh Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */ 67c45e6716Sdrh SqliteDb *pDb; /* Database connection that owns this function */ 68d1e4733dSdrh int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */ 69d1e4733dSdrh char *zName; /* Name of this function */ 70cabb0819Sdrh SqlFunc *pNext; /* Next function on the list of them all */ 71cabb0819Sdrh }; 72cabb0819Sdrh 73cabb0819Sdrh /* 740202b29eSdanielk1977 ** New collation sequences function can be created as TCL scripts. Each such 750202b29eSdanielk1977 ** function is described by an instance of the following structure. 760202b29eSdanielk1977 */ 770202b29eSdanielk1977 typedef struct SqlCollate SqlCollate; 780202b29eSdanielk1977 struct SqlCollate { 790202b29eSdanielk1977 Tcl_Interp *interp; /* The TCL interpret to execute the function */ 800202b29eSdanielk1977 char *zScript; /* The script to be run */ 810202b29eSdanielk1977 SqlCollate *pNext; /* Next function on the list of them all */ 820202b29eSdanielk1977 }; 830202b29eSdanielk1977 840202b29eSdanielk1977 /* 85fb7e7651Sdrh ** Prepared statements are cached for faster execution. Each prepared 86fb7e7651Sdrh ** statement is described by an instance of the following structure. 87fb7e7651Sdrh */ 88fb7e7651Sdrh typedef struct SqlPreparedStmt SqlPreparedStmt; 89fb7e7651Sdrh struct SqlPreparedStmt { 90fb7e7651Sdrh SqlPreparedStmt *pNext; /* Next in linked list */ 91fb7e7651Sdrh SqlPreparedStmt *pPrev; /* Previous on the list */ 92fb7e7651Sdrh sqlite3_stmt *pStmt; /* The prepared statement */ 93fb7e7651Sdrh int nSql; /* chars in zSql[] */ 94d0e2a854Sdanielk1977 const char *zSql; /* Text of the SQL statement */ 954a4c11aaSdan int nParm; /* Size of apParm array */ 964a4c11aaSdan Tcl_Obj **apParm; /* Array of referenced object pointers */ 97fb7e7651Sdrh }; 98fb7e7651Sdrh 99d0441796Sdanielk1977 typedef struct IncrblobChannel IncrblobChannel; 100d0441796Sdanielk1977 101fb7e7651Sdrh /* 102bec3f402Sdrh ** There is one instance of this structure for each SQLite database 103bec3f402Sdrh ** that has been opened by the SQLite TCL interface. 104c431fd55Sdan ** 105c431fd55Sdan ** If this module is built with SQLITE_TEST defined (to create the SQLite 106c431fd55Sdan ** testfixture executable), then it may be configured to use either 107c431fd55Sdan ** sqlite3_prepare_v2() or sqlite3_prepare() to prepare SQL statements. 108c431fd55Sdan ** If SqliteDb.bLegacyPrepare is true, sqlite3_prepare() is used. 109bec3f402Sdrh */ 110bec3f402Sdrh struct SqliteDb { 111dddca286Sdrh sqlite3 *db; /* The "real" database structure. MUST BE FIRST */ 112bec3f402Sdrh Tcl_Interp *interp; /* The interpreter used for this database */ 1136d31316cSdrh char *zBusy; /* The busy callback routine */ 114aa940eacSdrh char *zCommit; /* The commit hook callback routine */ 115b5a20d3cSdrh char *zTrace; /* The trace callback routine */ 11619e2d37fSdrh char *zProfile; /* The profile callback routine */ 117348bb5d6Sdanielk1977 char *zProgress; /* The progress callback routine */ 118e22a334bSdrh char *zAuth; /* The authorization callback routine */ 1191f1549f8Sdrh int disableAuth; /* Disable the authorizer if it exists */ 12055c45f2eSdanielk1977 char *zNull; /* Text to substitute for an SQL NULL value */ 121cabb0819Sdrh SqlFunc *pFunc; /* List of SQL functions */ 12294eb6a14Sdanielk1977 Tcl_Obj *pUpdateHook; /* Update hook script (if any) */ 12371fd80bfSdanielk1977 Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */ 1245def0843Sdrh Tcl_Obj *pWalHook; /* WAL hook script (if any) */ 125404ca075Sdanielk1977 Tcl_Obj *pUnlockNotify; /* Unlock notify script (if any) */ 1260202b29eSdanielk1977 SqlCollate *pCollate; /* List of SQL collation functions */ 1276f8a503dSdanielk1977 int rc; /* Return code of most recent sqlite3_exec() */ 1287cedc8d4Sdanielk1977 Tcl_Obj *pCollateNeeded; /* Collation needed script */ 129fb7e7651Sdrh SqlPreparedStmt *stmtList; /* List of prepared statements*/ 130fb7e7651Sdrh SqlPreparedStmt *stmtLast; /* Last statement in the list */ 131fb7e7651Sdrh int maxStmt; /* The next maximum number of stmtList */ 132fb7e7651Sdrh int nStmt; /* Number of statements in stmtList */ 133d0441796Sdanielk1977 IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */ 1343c379b01Sdrh int nStep, nSort, nIndex; /* Statistics for most recent operation */ 135cd38d520Sdanielk1977 int nTransaction; /* Number of nested [transaction] methods */ 136c431fd55Sdan #ifdef SQLITE_TEST 137c431fd55Sdan int bLegacyPrepare; /* True to use sqlite3_prepare() */ 138c431fd55Sdan #endif 13998808babSdrh }; 140297ecf14Sdrh 141b4e9af9fSdanielk1977 struct IncrblobChannel { 142d0441796Sdanielk1977 sqlite3_blob *pBlob; /* sqlite3 blob handle */ 143dcbb5d3fSdanielk1977 SqliteDb *pDb; /* Associated database connection */ 144b4e9af9fSdanielk1977 int iSeek; /* Current seek offset */ 145d0441796Sdanielk1977 Tcl_Channel channel; /* Channel identifier */ 146d0441796Sdanielk1977 IncrblobChannel *pNext; /* Linked list of all open incrblob channels */ 147d0441796Sdanielk1977 IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */ 148b4e9af9fSdanielk1977 }; 149b4e9af9fSdanielk1977 150ea678832Sdrh /* 151ea678832Sdrh ** Compute a string length that is limited to what can be stored in 152ea678832Sdrh ** lower 30 bits of a 32-bit signed integer. 153ea678832Sdrh */ 1544f21c4afSdrh static int strlen30(const char *z){ 155ea678832Sdrh const char *z2 = z; 156ea678832Sdrh while( *z2 ){ z2++; } 157ea678832Sdrh return 0x3fffffff & (int)(z2 - z); 158ea678832Sdrh } 159ea678832Sdrh 160ea678832Sdrh 16132a0d8bbSdanielk1977 #ifndef SQLITE_OMIT_INCRBLOB 162b4e9af9fSdanielk1977 /* 163d0441796Sdanielk1977 ** Close all incrblob channels opened using database connection pDb. 164d0441796Sdanielk1977 ** This is called when shutting down the database connection. 165d0441796Sdanielk1977 */ 166d0441796Sdanielk1977 static void closeIncrblobChannels(SqliteDb *pDb){ 167d0441796Sdanielk1977 IncrblobChannel *p; 168d0441796Sdanielk1977 IncrblobChannel *pNext; 169d0441796Sdanielk1977 170d0441796Sdanielk1977 for(p=pDb->pIncrblob; p; p=pNext){ 171d0441796Sdanielk1977 pNext = p->pNext; 172d0441796Sdanielk1977 173d0441796Sdanielk1977 /* Note: Calling unregister here call Tcl_Close on the incrblob channel, 174d0441796Sdanielk1977 ** which deletes the IncrblobChannel structure at *p. So do not 175d0441796Sdanielk1977 ** call Tcl_Free() here. 176d0441796Sdanielk1977 */ 177d0441796Sdanielk1977 Tcl_UnregisterChannel(pDb->interp, p->channel); 178d0441796Sdanielk1977 } 179d0441796Sdanielk1977 } 180d0441796Sdanielk1977 181d0441796Sdanielk1977 /* 182b4e9af9fSdanielk1977 ** Close an incremental blob channel. 183b4e9af9fSdanielk1977 */ 184b4e9af9fSdanielk1977 static int incrblobClose(ClientData instanceData, Tcl_Interp *interp){ 185b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 18692d4d7a9Sdanielk1977 int rc = sqlite3_blob_close(p->pBlob); 18792d4d7a9Sdanielk1977 sqlite3 *db = p->pDb->db; 188d0441796Sdanielk1977 189d0441796Sdanielk1977 /* Remove the channel from the SqliteDb.pIncrblob list. */ 190d0441796Sdanielk1977 if( p->pNext ){ 191d0441796Sdanielk1977 p->pNext->pPrev = p->pPrev; 192d0441796Sdanielk1977 } 193d0441796Sdanielk1977 if( p->pPrev ){ 194d0441796Sdanielk1977 p->pPrev->pNext = p->pNext; 195d0441796Sdanielk1977 } 196d0441796Sdanielk1977 if( p->pDb->pIncrblob==p ){ 197d0441796Sdanielk1977 p->pDb->pIncrblob = p->pNext; 198d0441796Sdanielk1977 } 199d0441796Sdanielk1977 20092d4d7a9Sdanielk1977 /* Free the IncrblobChannel structure */ 201b4e9af9fSdanielk1977 Tcl_Free((char *)p); 20292d4d7a9Sdanielk1977 20392d4d7a9Sdanielk1977 if( rc!=SQLITE_OK ){ 20492d4d7a9Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); 20592d4d7a9Sdanielk1977 return TCL_ERROR; 20692d4d7a9Sdanielk1977 } 207b4e9af9fSdanielk1977 return TCL_OK; 208b4e9af9fSdanielk1977 } 209b4e9af9fSdanielk1977 210b4e9af9fSdanielk1977 /* 211b4e9af9fSdanielk1977 ** Read data from an incremental blob channel. 212b4e9af9fSdanielk1977 */ 213b4e9af9fSdanielk1977 static int incrblobInput( 214b4e9af9fSdanielk1977 ClientData instanceData, 215b4e9af9fSdanielk1977 char *buf, 216b4e9af9fSdanielk1977 int bufSize, 217b4e9af9fSdanielk1977 int *errorCodePtr 218b4e9af9fSdanielk1977 ){ 219b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 220b4e9af9fSdanielk1977 int nRead = bufSize; /* Number of bytes to read */ 221b4e9af9fSdanielk1977 int nBlob; /* Total size of the blob */ 222b4e9af9fSdanielk1977 int rc; /* sqlite error code */ 223b4e9af9fSdanielk1977 224b4e9af9fSdanielk1977 nBlob = sqlite3_blob_bytes(p->pBlob); 225b4e9af9fSdanielk1977 if( (p->iSeek+nRead)>nBlob ){ 226b4e9af9fSdanielk1977 nRead = nBlob-p->iSeek; 227b4e9af9fSdanielk1977 } 228b4e9af9fSdanielk1977 if( nRead<=0 ){ 229b4e9af9fSdanielk1977 return 0; 230b4e9af9fSdanielk1977 } 231b4e9af9fSdanielk1977 232b4e9af9fSdanielk1977 rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek); 233b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 234b4e9af9fSdanielk1977 *errorCodePtr = rc; 235b4e9af9fSdanielk1977 return -1; 236b4e9af9fSdanielk1977 } 237b4e9af9fSdanielk1977 238b4e9af9fSdanielk1977 p->iSeek += nRead; 239b4e9af9fSdanielk1977 return nRead; 240b4e9af9fSdanielk1977 } 241b4e9af9fSdanielk1977 242d0441796Sdanielk1977 /* 243d0441796Sdanielk1977 ** Write data to an incremental blob channel. 244d0441796Sdanielk1977 */ 245b4e9af9fSdanielk1977 static int incrblobOutput( 246b4e9af9fSdanielk1977 ClientData instanceData, 247b4e9af9fSdanielk1977 CONST char *buf, 248b4e9af9fSdanielk1977 int toWrite, 249b4e9af9fSdanielk1977 int *errorCodePtr 250b4e9af9fSdanielk1977 ){ 251b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 252b4e9af9fSdanielk1977 int nWrite = toWrite; /* Number of bytes to write */ 253b4e9af9fSdanielk1977 int nBlob; /* Total size of the blob */ 254b4e9af9fSdanielk1977 int rc; /* sqlite error code */ 255b4e9af9fSdanielk1977 256b4e9af9fSdanielk1977 nBlob = sqlite3_blob_bytes(p->pBlob); 257b4e9af9fSdanielk1977 if( (p->iSeek+nWrite)>nBlob ){ 258b4e9af9fSdanielk1977 *errorCodePtr = EINVAL; 259b4e9af9fSdanielk1977 return -1; 260b4e9af9fSdanielk1977 } 261b4e9af9fSdanielk1977 if( nWrite<=0 ){ 262b4e9af9fSdanielk1977 return 0; 263b4e9af9fSdanielk1977 } 264b4e9af9fSdanielk1977 265b4e9af9fSdanielk1977 rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek); 266b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 267b4e9af9fSdanielk1977 *errorCodePtr = EIO; 268b4e9af9fSdanielk1977 return -1; 269b4e9af9fSdanielk1977 } 270b4e9af9fSdanielk1977 271b4e9af9fSdanielk1977 p->iSeek += nWrite; 272b4e9af9fSdanielk1977 return nWrite; 273b4e9af9fSdanielk1977 } 274b4e9af9fSdanielk1977 275b4e9af9fSdanielk1977 /* 276b4e9af9fSdanielk1977 ** Seek an incremental blob channel. 277b4e9af9fSdanielk1977 */ 278b4e9af9fSdanielk1977 static int incrblobSeek( 279b4e9af9fSdanielk1977 ClientData instanceData, 280b4e9af9fSdanielk1977 long offset, 281b4e9af9fSdanielk1977 int seekMode, 282b4e9af9fSdanielk1977 int *errorCodePtr 283b4e9af9fSdanielk1977 ){ 284b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 285b4e9af9fSdanielk1977 286b4e9af9fSdanielk1977 switch( seekMode ){ 287b4e9af9fSdanielk1977 case SEEK_SET: 288b4e9af9fSdanielk1977 p->iSeek = offset; 289b4e9af9fSdanielk1977 break; 290b4e9af9fSdanielk1977 case SEEK_CUR: 291b4e9af9fSdanielk1977 p->iSeek += offset; 292b4e9af9fSdanielk1977 break; 293b4e9af9fSdanielk1977 case SEEK_END: 294b4e9af9fSdanielk1977 p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset; 295b4e9af9fSdanielk1977 break; 296b4e9af9fSdanielk1977 297b4e9af9fSdanielk1977 default: assert(!"Bad seekMode"); 298b4e9af9fSdanielk1977 } 299b4e9af9fSdanielk1977 300b4e9af9fSdanielk1977 return p->iSeek; 301b4e9af9fSdanielk1977 } 302b4e9af9fSdanielk1977 303b4e9af9fSdanielk1977 304b4e9af9fSdanielk1977 static void incrblobWatch(ClientData instanceData, int mode){ 305b4e9af9fSdanielk1977 /* NO-OP */ 306b4e9af9fSdanielk1977 } 307b4e9af9fSdanielk1977 static int incrblobHandle(ClientData instanceData, int dir, ClientData *hPtr){ 308b4e9af9fSdanielk1977 return TCL_ERROR; 309b4e9af9fSdanielk1977 } 310b4e9af9fSdanielk1977 311b4e9af9fSdanielk1977 static Tcl_ChannelType IncrblobChannelType = { 312b4e9af9fSdanielk1977 "incrblob", /* typeName */ 313b4e9af9fSdanielk1977 TCL_CHANNEL_VERSION_2, /* version */ 314b4e9af9fSdanielk1977 incrblobClose, /* closeProc */ 315b4e9af9fSdanielk1977 incrblobInput, /* inputProc */ 316b4e9af9fSdanielk1977 incrblobOutput, /* outputProc */ 317b4e9af9fSdanielk1977 incrblobSeek, /* seekProc */ 318b4e9af9fSdanielk1977 0, /* setOptionProc */ 319b4e9af9fSdanielk1977 0, /* getOptionProc */ 320b4e9af9fSdanielk1977 incrblobWatch, /* watchProc (this is a no-op) */ 321b4e9af9fSdanielk1977 incrblobHandle, /* getHandleProc (always returns error) */ 322b4e9af9fSdanielk1977 0, /* close2Proc */ 323b4e9af9fSdanielk1977 0, /* blockModeProc */ 324b4e9af9fSdanielk1977 0, /* flushProc */ 325b4e9af9fSdanielk1977 0, /* handlerProc */ 326b4e9af9fSdanielk1977 0, /* wideSeekProc */ 327b4e9af9fSdanielk1977 }; 328b4e9af9fSdanielk1977 329b4e9af9fSdanielk1977 /* 330b4e9af9fSdanielk1977 ** Create a new incrblob channel. 331b4e9af9fSdanielk1977 */ 332b4e9af9fSdanielk1977 static int createIncrblobChannel( 333b4e9af9fSdanielk1977 Tcl_Interp *interp, 334b4e9af9fSdanielk1977 SqliteDb *pDb, 335b4e9af9fSdanielk1977 const char *zDb, 336b4e9af9fSdanielk1977 const char *zTable, 337b4e9af9fSdanielk1977 const char *zColumn, 3388cbadb02Sdanielk1977 sqlite_int64 iRow, 3398cbadb02Sdanielk1977 int isReadonly 340b4e9af9fSdanielk1977 ){ 341b4e9af9fSdanielk1977 IncrblobChannel *p; 3428cbadb02Sdanielk1977 sqlite3 *db = pDb->db; 343b4e9af9fSdanielk1977 sqlite3_blob *pBlob; 344b4e9af9fSdanielk1977 int rc; 3458cbadb02Sdanielk1977 int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE); 346b4e9af9fSdanielk1977 347b4e9af9fSdanielk1977 /* This variable is used to name the channels: "incrblob_[incr count]" */ 348b4e9af9fSdanielk1977 static int count = 0; 349b4e9af9fSdanielk1977 char zChannel[64]; 350b4e9af9fSdanielk1977 3518cbadb02Sdanielk1977 rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob); 352b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 353b4e9af9fSdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 354b4e9af9fSdanielk1977 return TCL_ERROR; 355b4e9af9fSdanielk1977 } 356b4e9af9fSdanielk1977 357b4e9af9fSdanielk1977 p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel)); 358b4e9af9fSdanielk1977 p->iSeek = 0; 359b4e9af9fSdanielk1977 p->pBlob = pBlob; 360b4e9af9fSdanielk1977 3615bb3eb9bSdrh sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count); 362d0441796Sdanielk1977 p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags); 363d0441796Sdanielk1977 Tcl_RegisterChannel(interp, p->channel); 364b4e9af9fSdanielk1977 365d0441796Sdanielk1977 /* Link the new channel into the SqliteDb.pIncrblob list. */ 366d0441796Sdanielk1977 p->pNext = pDb->pIncrblob; 367d0441796Sdanielk1977 p->pPrev = 0; 368d0441796Sdanielk1977 if( p->pNext ){ 369d0441796Sdanielk1977 p->pNext->pPrev = p; 370d0441796Sdanielk1977 } 371d0441796Sdanielk1977 pDb->pIncrblob = p; 372d0441796Sdanielk1977 p->pDb = pDb; 373d0441796Sdanielk1977 374d0441796Sdanielk1977 Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE); 375b4e9af9fSdanielk1977 return TCL_OK; 376b4e9af9fSdanielk1977 } 37732a0d8bbSdanielk1977 #else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */ 37832a0d8bbSdanielk1977 #define closeIncrblobChannels(pDb) 37932a0d8bbSdanielk1977 #endif 380b4e9af9fSdanielk1977 3816d31316cSdrh /* 382d1e4733dSdrh ** Look at the script prefix in pCmd. We will be executing this script 383d1e4733dSdrh ** after first appending one or more arguments. This routine analyzes 384d1e4733dSdrh ** the script to see if it is safe to use Tcl_EvalObjv() on the script 385d1e4733dSdrh ** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much 386d1e4733dSdrh ** faster. 387d1e4733dSdrh ** 388d1e4733dSdrh ** Scripts that are safe to use with Tcl_EvalObjv() consists of a 389d1e4733dSdrh ** command name followed by zero or more arguments with no [...] or $ 390d1e4733dSdrh ** or {...} or ; to be seen anywhere. Most callback scripts consist 391d1e4733dSdrh ** of just a single procedure name and they meet this requirement. 392d1e4733dSdrh */ 393d1e4733dSdrh static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){ 394d1e4733dSdrh /* We could try to do something with Tcl_Parse(). But we will instead 395d1e4733dSdrh ** just do a search for forbidden characters. If any of the forbidden 396d1e4733dSdrh ** characters appear in pCmd, we will report the string as unsafe. 397d1e4733dSdrh */ 398d1e4733dSdrh const char *z; 399d1e4733dSdrh int n; 400d1e4733dSdrh z = Tcl_GetStringFromObj(pCmd, &n); 401d1e4733dSdrh while( n-- > 0 ){ 402d1e4733dSdrh int c = *(z++); 403d1e4733dSdrh if( c=='$' || c=='[' || c==';' ) return 0; 404d1e4733dSdrh } 405d1e4733dSdrh return 1; 406d1e4733dSdrh } 407d1e4733dSdrh 408d1e4733dSdrh /* 409d1e4733dSdrh ** Find an SqlFunc structure with the given name. Or create a new 410d1e4733dSdrh ** one if an existing one cannot be found. Return a pointer to the 411d1e4733dSdrh ** structure. 412d1e4733dSdrh */ 413d1e4733dSdrh static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){ 414d1e4733dSdrh SqlFunc *p, *pNew; 415d1e4733dSdrh int i; 4164f21c4afSdrh pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + strlen30(zName) + 1 ); 417d1e4733dSdrh pNew->zName = (char*)&pNew[1]; 418d1e4733dSdrh for(i=0; zName[i]; i++){ pNew->zName[i] = tolower(zName[i]); } 419d1e4733dSdrh pNew->zName[i] = 0; 420d1e4733dSdrh for(p=pDb->pFunc; p; p=p->pNext){ 421d1e4733dSdrh if( strcmp(p->zName, pNew->zName)==0 ){ 422d1e4733dSdrh Tcl_Free((char*)pNew); 423d1e4733dSdrh return p; 424d1e4733dSdrh } 425d1e4733dSdrh } 426d1e4733dSdrh pNew->interp = pDb->interp; 427c45e6716Sdrh pNew->pDb = pDb; 428d1e4733dSdrh pNew->pScript = 0; 429d1e4733dSdrh pNew->pNext = pDb->pFunc; 430d1e4733dSdrh pDb->pFunc = pNew; 431d1e4733dSdrh return pNew; 432d1e4733dSdrh } 433d1e4733dSdrh 434d1e4733dSdrh /* 435c431fd55Sdan ** Free a single SqlPreparedStmt object. 436c431fd55Sdan */ 437c431fd55Sdan static void dbFreeStmt(SqlPreparedStmt *pStmt){ 438c431fd55Sdan #ifdef SQLITE_TEST 439c431fd55Sdan if( sqlite3_sql(pStmt->pStmt)==0 ){ 440c431fd55Sdan Tcl_Free((char *)pStmt->zSql); 441c431fd55Sdan } 442c431fd55Sdan #endif 443c431fd55Sdan sqlite3_finalize(pStmt->pStmt); 444c431fd55Sdan Tcl_Free((char *)pStmt); 445c431fd55Sdan } 446c431fd55Sdan 447c431fd55Sdan /* 448fb7e7651Sdrh ** Finalize and free a list of prepared statements 449fb7e7651Sdrh */ 450fb7e7651Sdrh static void flushStmtCache(SqliteDb *pDb){ 451fb7e7651Sdrh SqlPreparedStmt *pPreStmt; 452c431fd55Sdan SqlPreparedStmt *pNext; 453fb7e7651Sdrh 454c431fd55Sdan for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pNext){ 455c431fd55Sdan pNext = pPreStmt->pNext; 456c431fd55Sdan dbFreeStmt(pPreStmt); 457fb7e7651Sdrh } 458fb7e7651Sdrh pDb->nStmt = 0; 459fb7e7651Sdrh pDb->stmtLast = 0; 460c431fd55Sdan pDb->stmtList = 0; 461fb7e7651Sdrh } 462fb7e7651Sdrh 463fb7e7651Sdrh /* 464895d7472Sdrh ** TCL calls this procedure when an sqlite3 database command is 465895d7472Sdrh ** deleted. 46675897234Sdrh */ 46775897234Sdrh static void DbDeleteCmd(void *db){ 468bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)db; 469fb7e7651Sdrh flushStmtCache(pDb); 470d0441796Sdanielk1977 closeIncrblobChannels(pDb); 4716f8a503dSdanielk1977 sqlite3_close(pDb->db); 472cabb0819Sdrh while( pDb->pFunc ){ 473cabb0819Sdrh SqlFunc *pFunc = pDb->pFunc; 474cabb0819Sdrh pDb->pFunc = pFunc->pNext; 475c45e6716Sdrh assert( pFunc->pDb==pDb ); 476d1e4733dSdrh Tcl_DecrRefCount(pFunc->pScript); 477cabb0819Sdrh Tcl_Free((char*)pFunc); 478cabb0819Sdrh } 4790202b29eSdanielk1977 while( pDb->pCollate ){ 4800202b29eSdanielk1977 SqlCollate *pCollate = pDb->pCollate; 4810202b29eSdanielk1977 pDb->pCollate = pCollate->pNext; 4820202b29eSdanielk1977 Tcl_Free((char*)pCollate); 4830202b29eSdanielk1977 } 484bec3f402Sdrh if( pDb->zBusy ){ 485bec3f402Sdrh Tcl_Free(pDb->zBusy); 486bec3f402Sdrh } 487b5a20d3cSdrh if( pDb->zTrace ){ 488b5a20d3cSdrh Tcl_Free(pDb->zTrace); 4890d1a643aSdrh } 49019e2d37fSdrh if( pDb->zProfile ){ 49119e2d37fSdrh Tcl_Free(pDb->zProfile); 49219e2d37fSdrh } 493e22a334bSdrh if( pDb->zAuth ){ 494e22a334bSdrh Tcl_Free(pDb->zAuth); 495e22a334bSdrh } 49655c45f2eSdanielk1977 if( pDb->zNull ){ 49755c45f2eSdanielk1977 Tcl_Free(pDb->zNull); 49855c45f2eSdanielk1977 } 49994eb6a14Sdanielk1977 if( pDb->pUpdateHook ){ 50094eb6a14Sdanielk1977 Tcl_DecrRefCount(pDb->pUpdateHook); 50194eb6a14Sdanielk1977 } 50271fd80bfSdanielk1977 if( pDb->pRollbackHook ){ 50371fd80bfSdanielk1977 Tcl_DecrRefCount(pDb->pRollbackHook); 50471fd80bfSdanielk1977 } 5055def0843Sdrh if( pDb->pWalHook ){ 5065def0843Sdrh Tcl_DecrRefCount(pDb->pWalHook); 5078d22a174Sdan } 50894eb6a14Sdanielk1977 if( pDb->pCollateNeeded ){ 50994eb6a14Sdanielk1977 Tcl_DecrRefCount(pDb->pCollateNeeded); 51094eb6a14Sdanielk1977 } 511bec3f402Sdrh Tcl_Free((char*)pDb); 512bec3f402Sdrh } 513bec3f402Sdrh 514bec3f402Sdrh /* 515bec3f402Sdrh ** This routine is called when a database file is locked while trying 516bec3f402Sdrh ** to execute SQL. 517bec3f402Sdrh */ 5182a764eb0Sdanielk1977 static int DbBusyHandler(void *cd, int nTries){ 519bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 520bec3f402Sdrh int rc; 521bec3f402Sdrh char zVal[30]; 522bec3f402Sdrh 5235bb3eb9bSdrh sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries); 524d1e4733dSdrh rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0); 525bec3f402Sdrh if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 526bec3f402Sdrh return 0; 527bec3f402Sdrh } 528bec3f402Sdrh return 1; 52975897234Sdrh } 53075897234Sdrh 53126e4a8b1Sdrh #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 53275897234Sdrh /* 533348bb5d6Sdanielk1977 ** This routine is invoked as the 'progress callback' for the database. 534348bb5d6Sdanielk1977 */ 535348bb5d6Sdanielk1977 static int DbProgressHandler(void *cd){ 536348bb5d6Sdanielk1977 SqliteDb *pDb = (SqliteDb*)cd; 537348bb5d6Sdanielk1977 int rc; 538348bb5d6Sdanielk1977 539348bb5d6Sdanielk1977 assert( pDb->zProgress ); 540348bb5d6Sdanielk1977 rc = Tcl_Eval(pDb->interp, pDb->zProgress); 541348bb5d6Sdanielk1977 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 542348bb5d6Sdanielk1977 return 1; 543348bb5d6Sdanielk1977 } 544348bb5d6Sdanielk1977 return 0; 545348bb5d6Sdanielk1977 } 54626e4a8b1Sdrh #endif 547348bb5d6Sdanielk1977 548d1167393Sdrh #ifndef SQLITE_OMIT_TRACE 549348bb5d6Sdanielk1977 /* 550b5a20d3cSdrh ** This routine is called by the SQLite trace handler whenever a new 551b5a20d3cSdrh ** block of SQL is executed. The TCL script in pDb->zTrace is executed. 5520d1a643aSdrh */ 553b5a20d3cSdrh static void DbTraceHandler(void *cd, const char *zSql){ 5540d1a643aSdrh SqliteDb *pDb = (SqliteDb*)cd; 555b5a20d3cSdrh Tcl_DString str; 5560d1a643aSdrh 557b5a20d3cSdrh Tcl_DStringInit(&str); 558b5a20d3cSdrh Tcl_DStringAppend(&str, pDb->zTrace, -1); 559b5a20d3cSdrh Tcl_DStringAppendElement(&str, zSql); 560b5a20d3cSdrh Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 561b5a20d3cSdrh Tcl_DStringFree(&str); 562b5a20d3cSdrh Tcl_ResetResult(pDb->interp); 5630d1a643aSdrh } 564d1167393Sdrh #endif 5650d1a643aSdrh 566d1167393Sdrh #ifndef SQLITE_OMIT_TRACE 5670d1a643aSdrh /* 56819e2d37fSdrh ** This routine is called by the SQLite profile handler after a statement 56919e2d37fSdrh ** SQL has executed. The TCL script in pDb->zProfile is evaluated. 57019e2d37fSdrh */ 57119e2d37fSdrh static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){ 57219e2d37fSdrh SqliteDb *pDb = (SqliteDb*)cd; 57319e2d37fSdrh Tcl_DString str; 57419e2d37fSdrh char zTm[100]; 57519e2d37fSdrh 57619e2d37fSdrh sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm); 57719e2d37fSdrh Tcl_DStringInit(&str); 57819e2d37fSdrh Tcl_DStringAppend(&str, pDb->zProfile, -1); 57919e2d37fSdrh Tcl_DStringAppendElement(&str, zSql); 58019e2d37fSdrh Tcl_DStringAppendElement(&str, zTm); 58119e2d37fSdrh Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 58219e2d37fSdrh Tcl_DStringFree(&str); 58319e2d37fSdrh Tcl_ResetResult(pDb->interp); 58419e2d37fSdrh } 585d1167393Sdrh #endif 58619e2d37fSdrh 58719e2d37fSdrh /* 588aa940eacSdrh ** This routine is called when a transaction is committed. The 589aa940eacSdrh ** TCL script in pDb->zCommit is executed. If it returns non-zero or 590aa940eacSdrh ** if it throws an exception, the transaction is rolled back instead 591aa940eacSdrh ** of being committed. 592aa940eacSdrh */ 593aa940eacSdrh static int DbCommitHandler(void *cd){ 594aa940eacSdrh SqliteDb *pDb = (SqliteDb*)cd; 595aa940eacSdrh int rc; 596aa940eacSdrh 597aa940eacSdrh rc = Tcl_Eval(pDb->interp, pDb->zCommit); 598aa940eacSdrh if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 599aa940eacSdrh return 1; 600aa940eacSdrh } 601aa940eacSdrh return 0; 602aa940eacSdrh } 603aa940eacSdrh 60471fd80bfSdanielk1977 static void DbRollbackHandler(void *clientData){ 60571fd80bfSdanielk1977 SqliteDb *pDb = (SqliteDb*)clientData; 60671fd80bfSdanielk1977 assert(pDb->pRollbackHook); 60771fd80bfSdanielk1977 if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){ 60871fd80bfSdanielk1977 Tcl_BackgroundError(pDb->interp); 60971fd80bfSdanielk1977 } 61071fd80bfSdanielk1977 } 61171fd80bfSdanielk1977 6125def0843Sdrh /* 6135def0843Sdrh ** This procedure handles wal_hook callbacks. 6145def0843Sdrh */ 6155def0843Sdrh static int DbWalHandler( 6168d22a174Sdan void *clientData, 6178d22a174Sdan sqlite3 *db, 6188d22a174Sdan const char *zDb, 6198d22a174Sdan int nEntry 6208d22a174Sdan ){ 6215def0843Sdrh int ret = SQLITE_OK; 6228d22a174Sdan Tcl_Obj *p; 6238d22a174Sdan SqliteDb *pDb = (SqliteDb*)clientData; 6248d22a174Sdan Tcl_Interp *interp = pDb->interp; 6255def0843Sdrh assert(pDb->pWalHook); 6268d22a174Sdan 6275def0843Sdrh p = Tcl_DuplicateObj(pDb->pWalHook); 6288d22a174Sdan Tcl_IncrRefCount(p); 6298d22a174Sdan Tcl_ListObjAppendElement(interp, p, Tcl_NewStringObj(zDb, -1)); 6308d22a174Sdan Tcl_ListObjAppendElement(interp, p, Tcl_NewIntObj(nEntry)); 6318d22a174Sdan if( TCL_OK!=Tcl_EvalObjEx(interp, p, 0) 6328d22a174Sdan || TCL_OK!=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &ret) 6338d22a174Sdan ){ 6348d22a174Sdan Tcl_BackgroundError(interp); 6358d22a174Sdan } 6368d22a174Sdan Tcl_DecrRefCount(p); 6378d22a174Sdan 6388d22a174Sdan return ret; 6398d22a174Sdan } 6408d22a174Sdan 641bcf4f484Sdrh #if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_UNLOCK_NOTIFY) 642404ca075Sdanielk1977 static void setTestUnlockNotifyVars(Tcl_Interp *interp, int iArg, int nArg){ 643404ca075Sdanielk1977 char zBuf[64]; 644404ca075Sdanielk1977 sprintf(zBuf, "%d", iArg); 645404ca075Sdanielk1977 Tcl_SetVar(interp, "sqlite_unlock_notify_arg", zBuf, TCL_GLOBAL_ONLY); 646404ca075Sdanielk1977 sprintf(zBuf, "%d", nArg); 647404ca075Sdanielk1977 Tcl_SetVar(interp, "sqlite_unlock_notify_argcount", zBuf, TCL_GLOBAL_ONLY); 648404ca075Sdanielk1977 } 649404ca075Sdanielk1977 #else 650404ca075Sdanielk1977 # define setTestUnlockNotifyVars(x,y,z) 651404ca075Sdanielk1977 #endif 652404ca075Sdanielk1977 65369910da9Sdrh #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 654404ca075Sdanielk1977 static void DbUnlockNotify(void **apArg, int nArg){ 655404ca075Sdanielk1977 int i; 656404ca075Sdanielk1977 for(i=0; i<nArg; i++){ 657404ca075Sdanielk1977 const int flags = (TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); 658404ca075Sdanielk1977 SqliteDb *pDb = (SqliteDb *)apArg[i]; 659404ca075Sdanielk1977 setTestUnlockNotifyVars(pDb->interp, i, nArg); 660404ca075Sdanielk1977 assert( pDb->pUnlockNotify); 661404ca075Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pDb->pUnlockNotify, flags); 662404ca075Sdanielk1977 Tcl_DecrRefCount(pDb->pUnlockNotify); 663404ca075Sdanielk1977 pDb->pUnlockNotify = 0; 664404ca075Sdanielk1977 } 665404ca075Sdanielk1977 } 66669910da9Sdrh #endif 667404ca075Sdanielk1977 66894eb6a14Sdanielk1977 static void DbUpdateHandler( 66994eb6a14Sdanielk1977 void *p, 67094eb6a14Sdanielk1977 int op, 67194eb6a14Sdanielk1977 const char *zDb, 67294eb6a14Sdanielk1977 const char *zTbl, 67394eb6a14Sdanielk1977 sqlite_int64 rowid 67494eb6a14Sdanielk1977 ){ 67594eb6a14Sdanielk1977 SqliteDb *pDb = (SqliteDb *)p; 67694eb6a14Sdanielk1977 Tcl_Obj *pCmd; 67794eb6a14Sdanielk1977 67894eb6a14Sdanielk1977 assert( pDb->pUpdateHook ); 67994eb6a14Sdanielk1977 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); 68094eb6a14Sdanielk1977 68194eb6a14Sdanielk1977 pCmd = Tcl_DuplicateObj(pDb->pUpdateHook); 68294eb6a14Sdanielk1977 Tcl_IncrRefCount(pCmd); 68394eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj( 68494eb6a14Sdanielk1977 ( (op==SQLITE_INSERT)?"INSERT":(op==SQLITE_UPDATE)?"UPDATE":"DELETE"), -1)); 68594eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); 68694eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); 68794eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid)); 68894eb6a14Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 689efdde169Sdrh Tcl_DecrRefCount(pCmd); 69094eb6a14Sdanielk1977 } 69194eb6a14Sdanielk1977 6927cedc8d4Sdanielk1977 static void tclCollateNeeded( 6937cedc8d4Sdanielk1977 void *pCtx, 6949bb575fdSdrh sqlite3 *db, 6957cedc8d4Sdanielk1977 int enc, 6967cedc8d4Sdanielk1977 const char *zName 6977cedc8d4Sdanielk1977 ){ 6987cedc8d4Sdanielk1977 SqliteDb *pDb = (SqliteDb *)pCtx; 6997cedc8d4Sdanielk1977 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded); 7007cedc8d4Sdanielk1977 Tcl_IncrRefCount(pScript); 7017cedc8d4Sdanielk1977 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1)); 7027cedc8d4Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pScript, 0); 7037cedc8d4Sdanielk1977 Tcl_DecrRefCount(pScript); 7047cedc8d4Sdanielk1977 } 7057cedc8d4Sdanielk1977 706aa940eacSdrh /* 7070202b29eSdanielk1977 ** This routine is called to evaluate an SQL collation function implemented 7080202b29eSdanielk1977 ** using TCL script. 7090202b29eSdanielk1977 */ 7100202b29eSdanielk1977 static int tclSqlCollate( 7110202b29eSdanielk1977 void *pCtx, 7120202b29eSdanielk1977 int nA, 7130202b29eSdanielk1977 const void *zA, 7140202b29eSdanielk1977 int nB, 7150202b29eSdanielk1977 const void *zB 7160202b29eSdanielk1977 ){ 7170202b29eSdanielk1977 SqlCollate *p = (SqlCollate *)pCtx; 7180202b29eSdanielk1977 Tcl_Obj *pCmd; 7190202b29eSdanielk1977 7200202b29eSdanielk1977 pCmd = Tcl_NewStringObj(p->zScript, -1); 7210202b29eSdanielk1977 Tcl_IncrRefCount(pCmd); 7220202b29eSdanielk1977 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA)); 7230202b29eSdanielk1977 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB)); 724d1e4733dSdrh Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 7250202b29eSdanielk1977 Tcl_DecrRefCount(pCmd); 7260202b29eSdanielk1977 return (atoi(Tcl_GetStringResult(p->interp))); 7270202b29eSdanielk1977 } 7280202b29eSdanielk1977 7290202b29eSdanielk1977 /* 730cabb0819Sdrh ** This routine is called to evaluate an SQL function implemented 731cabb0819Sdrh ** using TCL script. 732cabb0819Sdrh */ 7330ae8b831Sdanielk1977 static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ 7346f8a503dSdanielk1977 SqlFunc *p = sqlite3_user_data(context); 735d1e4733dSdrh Tcl_Obj *pCmd; 736cabb0819Sdrh int i; 737cabb0819Sdrh int rc; 738cabb0819Sdrh 739d1e4733dSdrh if( argc==0 ){ 740d1e4733dSdrh /* If there are no arguments to the function, call Tcl_EvalObjEx on the 741d1e4733dSdrh ** script object directly. This allows the TCL compiler to generate 742d1e4733dSdrh ** bytecode for the command on the first invocation and thus make 743d1e4733dSdrh ** subsequent invocations much faster. */ 744d1e4733dSdrh pCmd = p->pScript; 745d1e4733dSdrh Tcl_IncrRefCount(pCmd); 746d1e4733dSdrh rc = Tcl_EvalObjEx(p->interp, pCmd, 0); 747d1e4733dSdrh Tcl_DecrRefCount(pCmd); 74851ad0ecdSdanielk1977 }else{ 749d1e4733dSdrh /* If there are arguments to the function, make a shallow copy of the 750d1e4733dSdrh ** script object, lappend the arguments, then evaluate the copy. 751d1e4733dSdrh ** 752d1e4733dSdrh ** By "shallow" copy, we mean a only the outer list Tcl_Obj is duplicated. 753d1e4733dSdrh ** The new Tcl_Obj contains pointers to the original list elements. 754d1e4733dSdrh ** That way, when Tcl_EvalObjv() is run and shimmers the first element 755d1e4733dSdrh ** of the list to tclCmdNameType, that alternate representation will 756d1e4733dSdrh ** be preserved and reused on the next invocation. 757d1e4733dSdrh */ 758d1e4733dSdrh Tcl_Obj **aArg; 759d1e4733dSdrh int nArg; 760d1e4733dSdrh if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){ 761d1e4733dSdrh sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 762d1e4733dSdrh return; 763d1e4733dSdrh } 764d1e4733dSdrh pCmd = Tcl_NewListObj(nArg, aArg); 765d1e4733dSdrh Tcl_IncrRefCount(pCmd); 766d1e4733dSdrh for(i=0; i<argc; i++){ 767d1e4733dSdrh sqlite3_value *pIn = argv[i]; 768d1e4733dSdrh Tcl_Obj *pVal; 769d1e4733dSdrh 770d1e4733dSdrh /* Set pVal to contain the i'th column of this row. */ 771d1e4733dSdrh switch( sqlite3_value_type(pIn) ){ 772d1e4733dSdrh case SQLITE_BLOB: { 773d1e4733dSdrh int bytes = sqlite3_value_bytes(pIn); 774d1e4733dSdrh pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes); 775d1e4733dSdrh break; 776d1e4733dSdrh } 777d1e4733dSdrh case SQLITE_INTEGER: { 778d1e4733dSdrh sqlite_int64 v = sqlite3_value_int64(pIn); 779d1e4733dSdrh if( v>=-2147483647 && v<=2147483647 ){ 7807fd33929Sdrh pVal = Tcl_NewIntObj((int)v); 781d1e4733dSdrh }else{ 782d1e4733dSdrh pVal = Tcl_NewWideIntObj(v); 783d1e4733dSdrh } 784d1e4733dSdrh break; 785d1e4733dSdrh } 786d1e4733dSdrh case SQLITE_FLOAT: { 787d1e4733dSdrh double r = sqlite3_value_double(pIn); 788d1e4733dSdrh pVal = Tcl_NewDoubleObj(r); 789d1e4733dSdrh break; 790d1e4733dSdrh } 791d1e4733dSdrh case SQLITE_NULL: { 792c45e6716Sdrh pVal = Tcl_NewStringObj(p->pDb->zNull, -1); 793d1e4733dSdrh break; 794d1e4733dSdrh } 795d1e4733dSdrh default: { 796d1e4733dSdrh int bytes = sqlite3_value_bytes(pIn); 79700fd957bSdanielk1977 pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes); 798d1e4733dSdrh break; 79951ad0ecdSdanielk1977 } 800cabb0819Sdrh } 801d1e4733dSdrh rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal); 802d1e4733dSdrh if( rc ){ 803d1e4733dSdrh Tcl_DecrRefCount(pCmd); 804d1e4733dSdrh sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 805d1e4733dSdrh return; 806d1e4733dSdrh } 807d1e4733dSdrh } 808d1e4733dSdrh if( !p->useEvalObjv ){ 809d1e4733dSdrh /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd 810d1e4733dSdrh ** is a list without a string representation. To prevent this from 811d1e4733dSdrh ** happening, make sure pCmd has a valid string representation */ 812d1e4733dSdrh Tcl_GetString(pCmd); 813d1e4733dSdrh } 814d1e4733dSdrh rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 815d1e4733dSdrh Tcl_DecrRefCount(pCmd); 816d1e4733dSdrh } 817562e8d3cSdanielk1977 818c7f269d5Sdrh if( rc && rc!=TCL_RETURN ){ 8197e18c259Sdanielk1977 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 820cabb0819Sdrh }else{ 821c7f269d5Sdrh Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); 822c7f269d5Sdrh int n; 823c7f269d5Sdrh u8 *data; 8244a4c11aaSdan const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 825c7f269d5Sdrh char c = zType[0]; 826df0bddaeSdrh if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ 827d1e4733dSdrh /* Only return a BLOB type if the Tcl variable is a bytearray and 828df0bddaeSdrh ** has no string representation. */ 829c7f269d5Sdrh data = Tcl_GetByteArrayFromObj(pVar, &n); 830c7f269d5Sdrh sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT); 831985e0c63Sdrh }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 832c7f269d5Sdrh Tcl_GetIntFromObj(0, pVar, &n); 833c7f269d5Sdrh sqlite3_result_int(context, n); 834c7f269d5Sdrh }else if( c=='d' && strcmp(zType,"double")==0 ){ 835c7f269d5Sdrh double r; 836c7f269d5Sdrh Tcl_GetDoubleFromObj(0, pVar, &r); 837c7f269d5Sdrh sqlite3_result_double(context, r); 838985e0c63Sdrh }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 839985e0c63Sdrh (c=='i' && strcmp(zType,"int")==0) ){ 840df0bddaeSdrh Tcl_WideInt v; 841df0bddaeSdrh Tcl_GetWideIntFromObj(0, pVar, &v); 842df0bddaeSdrh sqlite3_result_int64(context, v); 843c7f269d5Sdrh }else{ 84400fd957bSdanielk1977 data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 84500fd957bSdanielk1977 sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT); 846c7f269d5Sdrh } 847cabb0819Sdrh } 848cabb0819Sdrh } 849895d7472Sdrh 850e22a334bSdrh #ifndef SQLITE_OMIT_AUTHORIZATION 851e22a334bSdrh /* 852e22a334bSdrh ** This is the authentication function. It appends the authentication 853e22a334bSdrh ** type code and the two arguments to zCmd[] then invokes the result 854e22a334bSdrh ** on the interpreter. The reply is examined to determine if the 855e22a334bSdrh ** authentication fails or succeeds. 856e22a334bSdrh */ 857e22a334bSdrh static int auth_callback( 858e22a334bSdrh void *pArg, 859e22a334bSdrh int code, 860e22a334bSdrh const char *zArg1, 861e22a334bSdrh const char *zArg2, 862e22a334bSdrh const char *zArg3, 863e22a334bSdrh const char *zArg4 864e22a334bSdrh ){ 865e22a334bSdrh char *zCode; 866e22a334bSdrh Tcl_DString str; 867e22a334bSdrh int rc; 868e22a334bSdrh const char *zReply; 869e22a334bSdrh SqliteDb *pDb = (SqliteDb*)pArg; 8701f1549f8Sdrh if( pDb->disableAuth ) return SQLITE_OK; 871e22a334bSdrh 872e22a334bSdrh switch( code ){ 873e22a334bSdrh case SQLITE_COPY : zCode="SQLITE_COPY"; break; 874e22a334bSdrh case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; 875e22a334bSdrh case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; 876e22a334bSdrh case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; 877e22a334bSdrh case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; 878e22a334bSdrh case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; 879e22a334bSdrh case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; 880e22a334bSdrh case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; 881e22a334bSdrh case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; 882e22a334bSdrh case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; 883e22a334bSdrh case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; 884e22a334bSdrh case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; 885e22a334bSdrh case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; 886e22a334bSdrh case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; 887e22a334bSdrh case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; 888e22a334bSdrh case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; 889e22a334bSdrh case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; 890e22a334bSdrh case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; 891e22a334bSdrh case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; 892e22a334bSdrh case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; 893e22a334bSdrh case SQLITE_READ : zCode="SQLITE_READ"; break; 894e22a334bSdrh case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; 895e22a334bSdrh case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; 896e22a334bSdrh case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; 89781e293b4Sdrh case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; 89881e293b4Sdrh case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; 8991c8c23ccSdanielk1977 case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break; 9001d54df88Sdanielk1977 case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break; 901e6e04969Sdrh case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break; 902f1a381e7Sdanielk1977 case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break; 903f1a381e7Sdanielk1977 case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break; 9045169bbc6Sdrh case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break; 905ab9b703fSdanielk1977 case SQLITE_SAVEPOINT : zCode="SQLITE_SAVEPOINT"; break; 906e22a334bSdrh default : zCode="????"; break; 907e22a334bSdrh } 908e22a334bSdrh Tcl_DStringInit(&str); 909e22a334bSdrh Tcl_DStringAppend(&str, pDb->zAuth, -1); 910e22a334bSdrh Tcl_DStringAppendElement(&str, zCode); 911e22a334bSdrh Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); 912e22a334bSdrh Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); 913e22a334bSdrh Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); 914e22a334bSdrh Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); 915e22a334bSdrh rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); 916e22a334bSdrh Tcl_DStringFree(&str); 917b07028f7Sdrh zReply = rc==TCL_OK ? Tcl_GetStringResult(pDb->interp) : "SQLITE_DENY"; 918e22a334bSdrh if( strcmp(zReply,"SQLITE_OK")==0 ){ 919e22a334bSdrh rc = SQLITE_OK; 920e22a334bSdrh }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ 921e22a334bSdrh rc = SQLITE_DENY; 922e22a334bSdrh }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ 923e22a334bSdrh rc = SQLITE_IGNORE; 924e22a334bSdrh }else{ 925e22a334bSdrh rc = 999; 926e22a334bSdrh } 927e22a334bSdrh return rc; 928e22a334bSdrh } 929e22a334bSdrh #endif /* SQLITE_OMIT_AUTHORIZATION */ 930cabb0819Sdrh 931cabb0819Sdrh /* 9321067fe11Stpoindex ** This routine reads a line of text from FILE in, stores 9331067fe11Stpoindex ** the text in memory obtained from malloc() and returns a pointer 9341067fe11Stpoindex ** to the text. NULL is returned at end of file, or if malloc() 9351067fe11Stpoindex ** fails. 9361067fe11Stpoindex ** 9371067fe11Stpoindex ** The interface is like "readline" but no command-line editing 9381067fe11Stpoindex ** is done. 9391067fe11Stpoindex ** 9401067fe11Stpoindex ** copied from shell.c from '.import' command 9411067fe11Stpoindex */ 9421067fe11Stpoindex static char *local_getline(char *zPrompt, FILE *in){ 9431067fe11Stpoindex char *zLine; 9441067fe11Stpoindex int nLine; 9451067fe11Stpoindex int n; 9461067fe11Stpoindex 9471067fe11Stpoindex nLine = 100; 9481067fe11Stpoindex zLine = malloc( nLine ); 9491067fe11Stpoindex if( zLine==0 ) return 0; 9501067fe11Stpoindex n = 0; 951b07028f7Sdrh while( 1 ){ 9521067fe11Stpoindex if( n+100>nLine ){ 9531067fe11Stpoindex nLine = nLine*2 + 100; 9541067fe11Stpoindex zLine = realloc(zLine, nLine); 9551067fe11Stpoindex if( zLine==0 ) return 0; 9561067fe11Stpoindex } 9571067fe11Stpoindex if( fgets(&zLine[n], nLine - n, in)==0 ){ 9581067fe11Stpoindex if( n==0 ){ 9591067fe11Stpoindex free(zLine); 9601067fe11Stpoindex return 0; 9611067fe11Stpoindex } 9621067fe11Stpoindex zLine[n] = 0; 9631067fe11Stpoindex break; 9641067fe11Stpoindex } 9651067fe11Stpoindex while( zLine[n] ){ n++; } 9661067fe11Stpoindex if( n>0 && zLine[n-1]=='\n' ){ 9671067fe11Stpoindex n--; 9681067fe11Stpoindex zLine[n] = 0; 969b07028f7Sdrh break; 9701067fe11Stpoindex } 9711067fe11Stpoindex } 9721067fe11Stpoindex zLine = realloc( zLine, n+1 ); 9731067fe11Stpoindex return zLine; 9741067fe11Stpoindex } 9751067fe11Stpoindex 9768e556520Sdanielk1977 9778e556520Sdanielk1977 /* 9784a4c11aaSdan ** This function is part of the implementation of the command: 9798e556520Sdanielk1977 ** 9804a4c11aaSdan ** $db transaction [-deferred|-immediate|-exclusive] SCRIPT 9818e556520Sdanielk1977 ** 9824a4c11aaSdan ** It is invoked after evaluating the script SCRIPT to commit or rollback 9834a4c11aaSdan ** the transaction or savepoint opened by the [transaction] command. 9844a4c11aaSdan */ 9854a4c11aaSdan static int DbTransPostCmd( 9864a4c11aaSdan ClientData data[], /* data[0] is the Sqlite3Db* for $db */ 9874a4c11aaSdan Tcl_Interp *interp, /* Tcl interpreter */ 9884a4c11aaSdan int result /* Result of evaluating SCRIPT */ 9894a4c11aaSdan ){ 9904a4c11aaSdan static const char *azEnd[] = { 9914a4c11aaSdan "RELEASE _tcl_transaction", /* rc==TCL_ERROR, nTransaction!=0 */ 9924a4c11aaSdan "COMMIT", /* rc!=TCL_ERROR, nTransaction==0 */ 9934a4c11aaSdan "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction", 9944a4c11aaSdan "ROLLBACK" /* rc==TCL_ERROR, nTransaction==0 */ 9954a4c11aaSdan }; 9964a4c11aaSdan SqliteDb *pDb = (SqliteDb*)data[0]; 9974a4c11aaSdan int rc = result; 9984a4c11aaSdan const char *zEnd; 9994a4c11aaSdan 10004a4c11aaSdan pDb->nTransaction--; 10014a4c11aaSdan zEnd = azEnd[(rc==TCL_ERROR)*2 + (pDb->nTransaction==0)]; 10024a4c11aaSdan 10034a4c11aaSdan pDb->disableAuth++; 10044a4c11aaSdan if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){ 10054a4c11aaSdan /* This is a tricky scenario to handle. The most likely cause of an 10064a4c11aaSdan ** error is that the exec() above was an attempt to commit the 10074a4c11aaSdan ** top-level transaction that returned SQLITE_BUSY. Or, less likely, 10084a4c11aaSdan ** that an IO-error has occured. In either case, throw a Tcl exception 10094a4c11aaSdan ** and try to rollback the transaction. 10104a4c11aaSdan ** 10114a4c11aaSdan ** But it could also be that the user executed one or more BEGIN, 10124a4c11aaSdan ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing 10134a4c11aaSdan ** this method's logic. Not clear how this would be best handled. 10144a4c11aaSdan */ 10154a4c11aaSdan if( rc!=TCL_ERROR ){ 10164a4c11aaSdan Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); 10174a4c11aaSdan rc = TCL_ERROR; 10184a4c11aaSdan } 10194a4c11aaSdan sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0); 10204a4c11aaSdan } 10214a4c11aaSdan pDb->disableAuth--; 10224a4c11aaSdan 10234a4c11aaSdan return rc; 10244a4c11aaSdan } 10254a4c11aaSdan 10264a4c11aaSdan /* 1027c431fd55Sdan ** Unless SQLITE_TEST is defined, this function is a simple wrapper around 1028c431fd55Sdan ** sqlite3_prepare_v2(). If SQLITE_TEST is defined, then it uses either 1029c431fd55Sdan ** sqlite3_prepare_v2() or legacy interface sqlite3_prepare(), depending 1030c431fd55Sdan ** on whether or not the [db_use_legacy_prepare] command has been used to 1031c431fd55Sdan ** configure the connection. 1032c431fd55Sdan */ 1033c431fd55Sdan static int dbPrepare( 1034c431fd55Sdan SqliteDb *pDb, /* Database object */ 1035c431fd55Sdan const char *zSql, /* SQL to compile */ 1036c431fd55Sdan sqlite3_stmt **ppStmt, /* OUT: Prepared statement */ 1037c431fd55Sdan const char **pzOut /* OUT: Pointer to next SQL statement */ 1038c431fd55Sdan ){ 1039c431fd55Sdan #ifdef SQLITE_TEST 1040c431fd55Sdan if( pDb->bLegacyPrepare ){ 1041c431fd55Sdan return sqlite3_prepare(pDb->db, zSql, -1, ppStmt, pzOut); 1042c431fd55Sdan } 1043c431fd55Sdan #endif 1044c431fd55Sdan return sqlite3_prepare_v2(pDb->db, zSql, -1, ppStmt, pzOut); 1045c431fd55Sdan } 1046c431fd55Sdan 1047c431fd55Sdan /* 10484a4c11aaSdan ** Search the cache for a prepared-statement object that implements the 10494a4c11aaSdan ** first SQL statement in the buffer pointed to by parameter zIn. If 10504a4c11aaSdan ** no such prepared-statement can be found, allocate and prepare a new 10514a4c11aaSdan ** one. In either case, bind the current values of the relevant Tcl 10524a4c11aaSdan ** variables to any $var, :var or @var variables in the statement. Before 10534a4c11aaSdan ** returning, set *ppPreStmt to point to the prepared-statement object. 10544a4c11aaSdan ** 10554a4c11aaSdan ** Output parameter *pzOut is set to point to the next SQL statement in 10564a4c11aaSdan ** buffer zIn, or to the '\0' byte at the end of zIn if there is no 10574a4c11aaSdan ** next statement. 10584a4c11aaSdan ** 10594a4c11aaSdan ** If successful, TCL_OK is returned. Otherwise, TCL_ERROR is returned 10604a4c11aaSdan ** and an error message loaded into interpreter pDb->interp. 10614a4c11aaSdan */ 10624a4c11aaSdan static int dbPrepareAndBind( 10634a4c11aaSdan SqliteDb *pDb, /* Database object */ 10644a4c11aaSdan char const *zIn, /* SQL to compile */ 10654a4c11aaSdan char const **pzOut, /* OUT: Pointer to next SQL statement */ 10664a4c11aaSdan SqlPreparedStmt **ppPreStmt /* OUT: Object used to cache statement */ 10674a4c11aaSdan ){ 10684a4c11aaSdan const char *zSql = zIn; /* Pointer to first SQL statement in zIn */ 10694a4c11aaSdan sqlite3_stmt *pStmt; /* Prepared statement object */ 10704a4c11aaSdan SqlPreparedStmt *pPreStmt; /* Pointer to cached statement */ 10714a4c11aaSdan int nSql; /* Length of zSql in bytes */ 10724a4c11aaSdan int nVar; /* Number of variables in statement */ 10734a4c11aaSdan int iParm = 0; /* Next free entry in apParm */ 10744a4c11aaSdan int i; 10754a4c11aaSdan Tcl_Interp *interp = pDb->interp; 10764a4c11aaSdan 10774a4c11aaSdan *ppPreStmt = 0; 10784a4c11aaSdan 10794a4c11aaSdan /* Trim spaces from the start of zSql and calculate the remaining length. */ 10804a4c11aaSdan while( isspace(zSql[0]) ){ zSql++; } 10814a4c11aaSdan nSql = strlen30(zSql); 10824a4c11aaSdan 10834a4c11aaSdan for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){ 10844a4c11aaSdan int n = pPreStmt->nSql; 10854a4c11aaSdan if( nSql>=n 10864a4c11aaSdan && memcmp(pPreStmt->zSql, zSql, n)==0 10874a4c11aaSdan && (zSql[n]==0 || zSql[n-1]==';') 10884a4c11aaSdan ){ 10894a4c11aaSdan pStmt = pPreStmt->pStmt; 10904a4c11aaSdan *pzOut = &zSql[pPreStmt->nSql]; 10914a4c11aaSdan 10924a4c11aaSdan /* When a prepared statement is found, unlink it from the 10934a4c11aaSdan ** cache list. It will later be added back to the beginning 10944a4c11aaSdan ** of the cache list in order to implement LRU replacement. 10954a4c11aaSdan */ 10964a4c11aaSdan if( pPreStmt->pPrev ){ 10974a4c11aaSdan pPreStmt->pPrev->pNext = pPreStmt->pNext; 10984a4c11aaSdan }else{ 10994a4c11aaSdan pDb->stmtList = pPreStmt->pNext; 11004a4c11aaSdan } 11014a4c11aaSdan if( pPreStmt->pNext ){ 11024a4c11aaSdan pPreStmt->pNext->pPrev = pPreStmt->pPrev; 11034a4c11aaSdan }else{ 11044a4c11aaSdan pDb->stmtLast = pPreStmt->pPrev; 11054a4c11aaSdan } 11064a4c11aaSdan pDb->nStmt--; 11074a4c11aaSdan nVar = sqlite3_bind_parameter_count(pStmt); 11084a4c11aaSdan break; 11094a4c11aaSdan } 11104a4c11aaSdan } 11114a4c11aaSdan 11124a4c11aaSdan /* If no prepared statement was found. Compile the SQL text. Also allocate 11134a4c11aaSdan ** a new SqlPreparedStmt structure. */ 11144a4c11aaSdan if( pPreStmt==0 ){ 11154a4c11aaSdan int nByte; 11164a4c11aaSdan 1117c431fd55Sdan if( SQLITE_OK!=dbPrepare(pDb, zSql, &pStmt, pzOut) ){ 1118c45e6716Sdrh Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 11194a4c11aaSdan return TCL_ERROR; 11204a4c11aaSdan } 11214a4c11aaSdan if( pStmt==0 ){ 11224a4c11aaSdan if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ 11234a4c11aaSdan /* A compile-time error in the statement. */ 1124c45e6716Sdrh Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 11254a4c11aaSdan return TCL_ERROR; 11264a4c11aaSdan }else{ 11274a4c11aaSdan /* The statement was a no-op. Continue to the next statement 11284a4c11aaSdan ** in the SQL string. 11294a4c11aaSdan */ 11304a4c11aaSdan return TCL_OK; 11314a4c11aaSdan } 11324a4c11aaSdan } 11334a4c11aaSdan 11344a4c11aaSdan assert( pPreStmt==0 ); 11354a4c11aaSdan nVar = sqlite3_bind_parameter_count(pStmt); 11364a4c11aaSdan nByte = sizeof(SqlPreparedStmt) + nVar*sizeof(Tcl_Obj *); 11374a4c11aaSdan pPreStmt = (SqlPreparedStmt*)Tcl_Alloc(nByte); 11384a4c11aaSdan memset(pPreStmt, 0, nByte); 11394a4c11aaSdan 11404a4c11aaSdan pPreStmt->pStmt = pStmt; 11417ed243b7Sdrh pPreStmt->nSql = (int)(*pzOut - zSql); 11424a4c11aaSdan pPreStmt->zSql = sqlite3_sql(pStmt); 11434a4c11aaSdan pPreStmt->apParm = (Tcl_Obj **)&pPreStmt[1]; 1144c431fd55Sdan #ifdef SQLITE_TEST 1145c431fd55Sdan if( pPreStmt->zSql==0 ){ 1146c431fd55Sdan char *zCopy = Tcl_Alloc(pPreStmt->nSql + 1); 1147c431fd55Sdan memcpy(zCopy, zSql, pPreStmt->nSql); 1148c431fd55Sdan zCopy[pPreStmt->nSql] = '\0'; 1149c431fd55Sdan pPreStmt->zSql = zCopy; 1150c431fd55Sdan } 1151c431fd55Sdan #endif 11524a4c11aaSdan } 11534a4c11aaSdan assert( pPreStmt ); 11544a4c11aaSdan assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql ); 11554a4c11aaSdan assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) ); 11564a4c11aaSdan 11574a4c11aaSdan /* Bind values to parameters that begin with $ or : */ 11584a4c11aaSdan for(i=1; i<=nVar; i++){ 11594a4c11aaSdan const char *zVar = sqlite3_bind_parameter_name(pStmt, i); 11604a4c11aaSdan if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){ 11614a4c11aaSdan Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); 11624a4c11aaSdan if( pVar ){ 11634a4c11aaSdan int n; 11644a4c11aaSdan u8 *data; 11654a4c11aaSdan const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 11664a4c11aaSdan char c = zType[0]; 11674a4c11aaSdan if( zVar[0]=='@' || 11684a4c11aaSdan (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){ 11694a4c11aaSdan /* Load a BLOB type if the Tcl variable is a bytearray and 11704a4c11aaSdan ** it has no string representation or the host 11714a4c11aaSdan ** parameter name begins with "@". */ 11724a4c11aaSdan data = Tcl_GetByteArrayFromObj(pVar, &n); 11734a4c11aaSdan sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); 11744a4c11aaSdan Tcl_IncrRefCount(pVar); 11754a4c11aaSdan pPreStmt->apParm[iParm++] = pVar; 11764a4c11aaSdan }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 11774a4c11aaSdan Tcl_GetIntFromObj(interp, pVar, &n); 11784a4c11aaSdan sqlite3_bind_int(pStmt, i, n); 11794a4c11aaSdan }else if( c=='d' && strcmp(zType,"double")==0 ){ 11804a4c11aaSdan double r; 11814a4c11aaSdan Tcl_GetDoubleFromObj(interp, pVar, &r); 11824a4c11aaSdan sqlite3_bind_double(pStmt, i, r); 11834a4c11aaSdan }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 11844a4c11aaSdan (c=='i' && strcmp(zType,"int")==0) ){ 11854a4c11aaSdan Tcl_WideInt v; 11864a4c11aaSdan Tcl_GetWideIntFromObj(interp, pVar, &v); 11874a4c11aaSdan sqlite3_bind_int64(pStmt, i, v); 11884a4c11aaSdan }else{ 11894a4c11aaSdan data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 11904a4c11aaSdan sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); 11914a4c11aaSdan Tcl_IncrRefCount(pVar); 11924a4c11aaSdan pPreStmt->apParm[iParm++] = pVar; 11934a4c11aaSdan } 11944a4c11aaSdan }else{ 11954a4c11aaSdan sqlite3_bind_null(pStmt, i); 11964a4c11aaSdan } 11974a4c11aaSdan } 11984a4c11aaSdan } 11994a4c11aaSdan pPreStmt->nParm = iParm; 12004a4c11aaSdan *ppPreStmt = pPreStmt; 1201937d0deaSdan 12024a4c11aaSdan return TCL_OK; 12034a4c11aaSdan } 12044a4c11aaSdan 12054a4c11aaSdan /* 12064a4c11aaSdan ** Release a statement reference obtained by calling dbPrepareAndBind(). 12074a4c11aaSdan ** There should be exactly one call to this function for each call to 12084a4c11aaSdan ** dbPrepareAndBind(). 12094a4c11aaSdan ** 12104a4c11aaSdan ** If the discard parameter is non-zero, then the statement is deleted 12114a4c11aaSdan ** immediately. Otherwise it is added to the LRU list and may be returned 12124a4c11aaSdan ** by a subsequent call to dbPrepareAndBind(). 12134a4c11aaSdan */ 12144a4c11aaSdan static void dbReleaseStmt( 12154a4c11aaSdan SqliteDb *pDb, /* Database handle */ 12164a4c11aaSdan SqlPreparedStmt *pPreStmt, /* Prepared statement handle to release */ 12174a4c11aaSdan int discard /* True to delete (not cache) the pPreStmt */ 12184a4c11aaSdan ){ 12194a4c11aaSdan int i; 12204a4c11aaSdan 12214a4c11aaSdan /* Free the bound string and blob parameters */ 12224a4c11aaSdan for(i=0; i<pPreStmt->nParm; i++){ 12234a4c11aaSdan Tcl_DecrRefCount(pPreStmt->apParm[i]); 12244a4c11aaSdan } 12254a4c11aaSdan pPreStmt->nParm = 0; 12264a4c11aaSdan 12274a4c11aaSdan if( pDb->maxStmt<=0 || discard ){ 12284a4c11aaSdan /* If the cache is turned off, deallocated the statement */ 1229c431fd55Sdan dbFreeStmt(pPreStmt); 12304a4c11aaSdan }else{ 12314a4c11aaSdan /* Add the prepared statement to the beginning of the cache list. */ 12324a4c11aaSdan pPreStmt->pNext = pDb->stmtList; 12334a4c11aaSdan pPreStmt->pPrev = 0; 12344a4c11aaSdan if( pDb->stmtList ){ 12354a4c11aaSdan pDb->stmtList->pPrev = pPreStmt; 12364a4c11aaSdan } 12374a4c11aaSdan pDb->stmtList = pPreStmt; 12384a4c11aaSdan if( pDb->stmtLast==0 ){ 12394a4c11aaSdan assert( pDb->nStmt==0 ); 12404a4c11aaSdan pDb->stmtLast = pPreStmt; 12414a4c11aaSdan }else{ 12424a4c11aaSdan assert( pDb->nStmt>0 ); 12434a4c11aaSdan } 12444a4c11aaSdan pDb->nStmt++; 12454a4c11aaSdan 12464a4c11aaSdan /* If we have too many statement in cache, remove the surplus from 12474a4c11aaSdan ** the end of the cache list. */ 12484a4c11aaSdan while( pDb->nStmt>pDb->maxStmt ){ 1249c431fd55Sdan SqlPreparedStmt *pLast = pDb->stmtLast; 1250c431fd55Sdan pDb->stmtLast = pLast->pPrev; 12514a4c11aaSdan pDb->stmtLast->pNext = 0; 12524a4c11aaSdan pDb->nStmt--; 1253c431fd55Sdan dbFreeStmt(pLast); 12544a4c11aaSdan } 12554a4c11aaSdan } 12564a4c11aaSdan } 12574a4c11aaSdan 12584a4c11aaSdan /* 12594a4c11aaSdan ** Structure used with dbEvalXXX() functions: 12604a4c11aaSdan ** 12614a4c11aaSdan ** dbEvalInit() 12624a4c11aaSdan ** dbEvalStep() 12634a4c11aaSdan ** dbEvalFinalize() 12644a4c11aaSdan ** dbEvalRowInfo() 12654a4c11aaSdan ** dbEvalColumnValue() 12664a4c11aaSdan */ 12674a4c11aaSdan typedef struct DbEvalContext DbEvalContext; 12684a4c11aaSdan struct DbEvalContext { 12694a4c11aaSdan SqliteDb *pDb; /* Database handle */ 12704a4c11aaSdan Tcl_Obj *pSql; /* Object holding string zSql */ 12714a4c11aaSdan const char *zSql; /* Remaining SQL to execute */ 12724a4c11aaSdan SqlPreparedStmt *pPreStmt; /* Current statement */ 12734a4c11aaSdan int nCol; /* Number of columns returned by pStmt */ 12744a4c11aaSdan Tcl_Obj *pArray; /* Name of array variable */ 12754a4c11aaSdan Tcl_Obj **apColName; /* Array of column names */ 12764a4c11aaSdan }; 12774a4c11aaSdan 12784a4c11aaSdan /* 12794a4c11aaSdan ** Release any cache of column names currently held as part of 12804a4c11aaSdan ** the DbEvalContext structure passed as the first argument. 12814a4c11aaSdan */ 12824a4c11aaSdan static void dbReleaseColumnNames(DbEvalContext *p){ 12834a4c11aaSdan if( p->apColName ){ 12844a4c11aaSdan int i; 12854a4c11aaSdan for(i=0; i<p->nCol; i++){ 12864a4c11aaSdan Tcl_DecrRefCount(p->apColName[i]); 12874a4c11aaSdan } 12884a4c11aaSdan Tcl_Free((char *)p->apColName); 12894a4c11aaSdan p->apColName = 0; 12904a4c11aaSdan } 12914a4c11aaSdan p->nCol = 0; 12924a4c11aaSdan } 12934a4c11aaSdan 12944a4c11aaSdan /* 12954a4c11aaSdan ** Initialize a DbEvalContext structure. 12968e556520Sdanielk1977 ** 12978e556520Sdanielk1977 ** If pArray is not NULL, then it contains the name of a Tcl array 12988e556520Sdanielk1977 ** variable. The "*" member of this array is set to a list containing 12994a4c11aaSdan ** the names of the columns returned by the statement as part of each 13004a4c11aaSdan ** call to dbEvalStep(), in order from left to right. e.g. if the names 13014a4c11aaSdan ** of the returned columns are a, b and c, it does the equivalent of the 13024a4c11aaSdan ** tcl command: 13038e556520Sdanielk1977 ** 13048e556520Sdanielk1977 ** set ${pArray}(*) {a b c} 13058e556520Sdanielk1977 */ 13064a4c11aaSdan static void dbEvalInit( 13074a4c11aaSdan DbEvalContext *p, /* Pointer to structure to initialize */ 13084a4c11aaSdan SqliteDb *pDb, /* Database handle */ 13094a4c11aaSdan Tcl_Obj *pSql, /* Object containing SQL script */ 13104a4c11aaSdan Tcl_Obj *pArray /* Name of Tcl array to set (*) element of */ 13118e556520Sdanielk1977 ){ 13124a4c11aaSdan memset(p, 0, sizeof(DbEvalContext)); 13134a4c11aaSdan p->pDb = pDb; 13144a4c11aaSdan p->zSql = Tcl_GetString(pSql); 13154a4c11aaSdan p->pSql = pSql; 13164a4c11aaSdan Tcl_IncrRefCount(pSql); 13174a4c11aaSdan if( pArray ){ 13184a4c11aaSdan p->pArray = pArray; 13194a4c11aaSdan Tcl_IncrRefCount(pArray); 13204a4c11aaSdan } 13214a4c11aaSdan } 13228e556520Sdanielk1977 13234a4c11aaSdan /* 13244a4c11aaSdan ** Obtain information about the row that the DbEvalContext passed as the 13254a4c11aaSdan ** first argument currently points to. 13264a4c11aaSdan */ 13274a4c11aaSdan static void dbEvalRowInfo( 13284a4c11aaSdan DbEvalContext *p, /* Evaluation context */ 13294a4c11aaSdan int *pnCol, /* OUT: Number of column names */ 13304a4c11aaSdan Tcl_Obj ***papColName /* OUT: Array of column names */ 13314a4c11aaSdan ){ 13328e556520Sdanielk1977 /* Compute column names */ 13334a4c11aaSdan if( 0==p->apColName ){ 13344a4c11aaSdan sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 13354a4c11aaSdan int i; /* Iterator variable */ 13364a4c11aaSdan int nCol; /* Number of columns returned by pStmt */ 13374a4c11aaSdan Tcl_Obj **apColName = 0; /* Array of column names */ 13384a4c11aaSdan 13394a4c11aaSdan p->nCol = nCol = sqlite3_column_count(pStmt); 13404a4c11aaSdan if( nCol>0 && (papColName || p->pArray) ){ 13414a4c11aaSdan apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); 13428e556520Sdanielk1977 for(i=0; i<nCol; i++){ 1343c45e6716Sdrh apColName[i] = Tcl_NewStringObj(sqlite3_column_name(pStmt,i), -1); 13448e556520Sdanielk1977 Tcl_IncrRefCount(apColName[i]); 13458e556520Sdanielk1977 } 13464a4c11aaSdan p->apColName = apColName; 13474a4c11aaSdan } 13488e556520Sdanielk1977 13498e556520Sdanielk1977 /* If results are being stored in an array variable, then create 13508e556520Sdanielk1977 ** the array(*) entry for that array 13518e556520Sdanielk1977 */ 13524a4c11aaSdan if( p->pArray ){ 13534a4c11aaSdan Tcl_Interp *interp = p->pDb->interp; 13548e556520Sdanielk1977 Tcl_Obj *pColList = Tcl_NewObj(); 13558e556520Sdanielk1977 Tcl_Obj *pStar = Tcl_NewStringObj("*", -1); 13564a4c11aaSdan 13578e556520Sdanielk1977 for(i=0; i<nCol; i++){ 13588e556520Sdanielk1977 Tcl_ListObjAppendElement(interp, pColList, apColName[i]); 13598e556520Sdanielk1977 } 13608e556520Sdanielk1977 Tcl_IncrRefCount(pStar); 13614a4c11aaSdan Tcl_ObjSetVar2(interp, p->pArray, pStar, pColList, 0); 13628e556520Sdanielk1977 Tcl_DecrRefCount(pStar); 13638e556520Sdanielk1977 } 13648e556520Sdanielk1977 } 13658e556520Sdanielk1977 13664a4c11aaSdan if( papColName ){ 13674a4c11aaSdan *papColName = p->apColName; 13684a4c11aaSdan } 13694a4c11aaSdan if( pnCol ){ 13704a4c11aaSdan *pnCol = p->nCol; 13714a4c11aaSdan } 13724a4c11aaSdan } 13734a4c11aaSdan 13744a4c11aaSdan /* 13754a4c11aaSdan ** Return one of TCL_OK, TCL_BREAK or TCL_ERROR. If TCL_ERROR is 13764a4c11aaSdan ** returned, then an error message is stored in the interpreter before 13774a4c11aaSdan ** returning. 13784a4c11aaSdan ** 13794a4c11aaSdan ** A return value of TCL_OK means there is a row of data available. The 13804a4c11aaSdan ** data may be accessed using dbEvalRowInfo() and dbEvalColumnValue(). This 13814a4c11aaSdan ** is analogous to a return of SQLITE_ROW from sqlite3_step(). If TCL_BREAK 13824a4c11aaSdan ** is returned, then the SQL script has finished executing and there are 13834a4c11aaSdan ** no further rows available. This is similar to SQLITE_DONE. 13844a4c11aaSdan */ 13854a4c11aaSdan static int dbEvalStep(DbEvalContext *p){ 1386c431fd55Sdan const char *zPrevSql = 0; /* Previous value of p->zSql */ 1387c431fd55Sdan 13884a4c11aaSdan while( p->zSql[0] || p->pPreStmt ){ 13894a4c11aaSdan int rc; 13904a4c11aaSdan if( p->pPreStmt==0 ){ 1391c431fd55Sdan zPrevSql = (p->zSql==zPrevSql ? 0 : p->zSql); 13924a4c11aaSdan rc = dbPrepareAndBind(p->pDb, p->zSql, &p->zSql, &p->pPreStmt); 13934a4c11aaSdan if( rc!=TCL_OK ) return rc; 13944a4c11aaSdan }else{ 13954a4c11aaSdan int rcs; 13964a4c11aaSdan SqliteDb *pDb = p->pDb; 13974a4c11aaSdan SqlPreparedStmt *pPreStmt = p->pPreStmt; 13984a4c11aaSdan sqlite3_stmt *pStmt = pPreStmt->pStmt; 13994a4c11aaSdan 14004a4c11aaSdan rcs = sqlite3_step(pStmt); 14014a4c11aaSdan if( rcs==SQLITE_ROW ){ 14024a4c11aaSdan return TCL_OK; 14034a4c11aaSdan } 14044a4c11aaSdan if( p->pArray ){ 14054a4c11aaSdan dbEvalRowInfo(p, 0, 0); 14064a4c11aaSdan } 14074a4c11aaSdan rcs = sqlite3_reset(pStmt); 14084a4c11aaSdan 14094a4c11aaSdan pDb->nStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_FULLSCAN_STEP,1); 14104a4c11aaSdan pDb->nSort = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_SORT,1); 14113c379b01Sdrh pDb->nIndex = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_AUTOINDEX,1); 14124a4c11aaSdan dbReleaseColumnNames(p); 14134a4c11aaSdan p->pPreStmt = 0; 14144a4c11aaSdan 14154a4c11aaSdan if( rcs!=SQLITE_OK ){ 14164a4c11aaSdan /* If a run-time error occurs, report the error and stop reading 14174a4c11aaSdan ** the SQL. */ 14184a4c11aaSdan dbReleaseStmt(pDb, pPreStmt, 1); 1419c431fd55Sdan #if SQLITE_TEST 1420c431fd55Sdan if( p->pDb->bLegacyPrepare && rcs==SQLITE_SCHEMA && zPrevSql ){ 1421c431fd55Sdan /* If the runtime error was an SQLITE_SCHEMA, and the database 1422c431fd55Sdan ** handle is configured to use the legacy sqlite3_prepare() 1423c431fd55Sdan ** interface, retry prepare()/step() on the same SQL statement. 1424c431fd55Sdan ** This only happens once. If there is a second SQLITE_SCHEMA 1425c431fd55Sdan ** error, the error will be returned to the caller. */ 1426c431fd55Sdan p->zSql = zPrevSql; 1427c431fd55Sdan continue; 1428c431fd55Sdan } 1429c431fd55Sdan #endif 1430c45e6716Sdrh Tcl_SetObjResult(pDb->interp, 1431c45e6716Sdrh Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1)); 14324a4c11aaSdan return TCL_ERROR; 14334a4c11aaSdan }else{ 14344a4c11aaSdan dbReleaseStmt(pDb, pPreStmt, 0); 14354a4c11aaSdan } 14364a4c11aaSdan } 14374a4c11aaSdan } 14384a4c11aaSdan 14394a4c11aaSdan /* Finished */ 14404a4c11aaSdan return TCL_BREAK; 14414a4c11aaSdan } 14424a4c11aaSdan 14434a4c11aaSdan /* 14444a4c11aaSdan ** Free all resources currently held by the DbEvalContext structure passed 14454a4c11aaSdan ** as the first argument. There should be exactly one call to this function 14464a4c11aaSdan ** for each call to dbEvalInit(). 14474a4c11aaSdan */ 14484a4c11aaSdan static void dbEvalFinalize(DbEvalContext *p){ 14494a4c11aaSdan if( p->pPreStmt ){ 14504a4c11aaSdan sqlite3_reset(p->pPreStmt->pStmt); 14514a4c11aaSdan dbReleaseStmt(p->pDb, p->pPreStmt, 0); 14524a4c11aaSdan p->pPreStmt = 0; 14534a4c11aaSdan } 14544a4c11aaSdan if( p->pArray ){ 14554a4c11aaSdan Tcl_DecrRefCount(p->pArray); 14564a4c11aaSdan p->pArray = 0; 14574a4c11aaSdan } 14584a4c11aaSdan Tcl_DecrRefCount(p->pSql); 14594a4c11aaSdan dbReleaseColumnNames(p); 14604a4c11aaSdan } 14614a4c11aaSdan 14624a4c11aaSdan /* 14634a4c11aaSdan ** Return a pointer to a Tcl_Obj structure with ref-count 0 that contains 14644a4c11aaSdan ** the value for the iCol'th column of the row currently pointed to by 14654a4c11aaSdan ** the DbEvalContext structure passed as the first argument. 14664a4c11aaSdan */ 14674a4c11aaSdan static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){ 14684a4c11aaSdan sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 14694a4c11aaSdan switch( sqlite3_column_type(pStmt, iCol) ){ 14704a4c11aaSdan case SQLITE_BLOB: { 14714a4c11aaSdan int bytes = sqlite3_column_bytes(pStmt, iCol); 14724a4c11aaSdan const char *zBlob = sqlite3_column_blob(pStmt, iCol); 14734a4c11aaSdan if( !zBlob ) bytes = 0; 14744a4c11aaSdan return Tcl_NewByteArrayObj((u8*)zBlob, bytes); 14754a4c11aaSdan } 14764a4c11aaSdan case SQLITE_INTEGER: { 14774a4c11aaSdan sqlite_int64 v = sqlite3_column_int64(pStmt, iCol); 14784a4c11aaSdan if( v>=-2147483647 && v<=2147483647 ){ 14797fd33929Sdrh return Tcl_NewIntObj((int)v); 14804a4c11aaSdan }else{ 14814a4c11aaSdan return Tcl_NewWideIntObj(v); 14824a4c11aaSdan } 14834a4c11aaSdan } 14844a4c11aaSdan case SQLITE_FLOAT: { 14854a4c11aaSdan return Tcl_NewDoubleObj(sqlite3_column_double(pStmt, iCol)); 14864a4c11aaSdan } 14874a4c11aaSdan case SQLITE_NULL: { 1488c45e6716Sdrh return Tcl_NewStringObj(p->pDb->zNull, -1); 14894a4c11aaSdan } 14904a4c11aaSdan } 14914a4c11aaSdan 1492325eff58Sdrh return Tcl_NewStringObj((char*)sqlite3_column_text(pStmt, iCol), -1); 14934a4c11aaSdan } 14944a4c11aaSdan 14954a4c11aaSdan /* 14964a4c11aaSdan ** If using Tcl version 8.6 or greater, use the NR functions to avoid 14974a4c11aaSdan ** recursive evalution of scripts by the [db eval] and [db trans] 14984a4c11aaSdan ** commands. Even if the headers used while compiling the extension 14994a4c11aaSdan ** are 8.6 or newer, the code still tests the Tcl version at runtime. 15004a4c11aaSdan ** This allows stubs-enabled builds to be used with older Tcl libraries. 15014a4c11aaSdan */ 15024a4c11aaSdan #if TCL_MAJOR_VERSION>8 || (TCL_MAJOR_VERSION==8 && TCL_MINOR_VERSION>=6) 1503a2c8a95bSdrh # define SQLITE_TCL_NRE 1 15044a4c11aaSdan static int DbUseNre(void){ 15054a4c11aaSdan int major, minor; 15064a4c11aaSdan Tcl_GetVersion(&major, &minor, 0, 0); 15074a4c11aaSdan return( (major==8 && minor>=6) || major>8 ); 15084a4c11aaSdan } 15094a4c11aaSdan #else 15104a4c11aaSdan /* 15114a4c11aaSdan ** Compiling using headers earlier than 8.6. In this case NR cannot be 15124a4c11aaSdan ** used, so DbUseNre() to always return zero. Add #defines for the other 15134a4c11aaSdan ** Tcl_NRxxx() functions to prevent them from causing compilation errors, 15144a4c11aaSdan ** even though the only invocations of them are within conditional blocks 15154a4c11aaSdan ** of the form: 15164a4c11aaSdan ** 15174a4c11aaSdan ** if( DbUseNre() ) { ... } 15184a4c11aaSdan */ 1519a2c8a95bSdrh # define SQLITE_TCL_NRE 0 15204a4c11aaSdan # define DbUseNre() 0 15214a4c11aaSdan # define Tcl_NRAddCallback(a,b,c,d,e,f) 0 15224a4c11aaSdan # define Tcl_NREvalObj(a,b,c) 0 15234a4c11aaSdan # define Tcl_NRCreateCommand(a,b,c,d,e,f) 0 15244a4c11aaSdan #endif 15254a4c11aaSdan 15264a4c11aaSdan /* 15274a4c11aaSdan ** This function is part of the implementation of the command: 15284a4c11aaSdan ** 15294a4c11aaSdan ** $db eval SQL ?ARRAYNAME? SCRIPT 15304a4c11aaSdan */ 15314a4c11aaSdan static int DbEvalNextCmd( 15324a4c11aaSdan ClientData data[], /* data[0] is the (DbEvalContext*) */ 15334a4c11aaSdan Tcl_Interp *interp, /* Tcl interpreter */ 15344a4c11aaSdan int result /* Result so far */ 15354a4c11aaSdan ){ 15364a4c11aaSdan int rc = result; /* Return code */ 15374a4c11aaSdan 15384a4c11aaSdan /* The first element of the data[] array is a pointer to a DbEvalContext 15394a4c11aaSdan ** structure allocated using Tcl_Alloc(). The second element of data[] 15404a4c11aaSdan ** is a pointer to a Tcl_Obj containing the script to run for each row 15414a4c11aaSdan ** returned by the queries encapsulated in data[0]. */ 15424a4c11aaSdan DbEvalContext *p = (DbEvalContext *)data[0]; 15434a4c11aaSdan Tcl_Obj *pScript = (Tcl_Obj *)data[1]; 15444a4c11aaSdan Tcl_Obj *pArray = p->pArray; 15454a4c11aaSdan 15464a4c11aaSdan while( (rc==TCL_OK || rc==TCL_CONTINUE) && TCL_OK==(rc = dbEvalStep(p)) ){ 15474a4c11aaSdan int i; 15484a4c11aaSdan int nCol; 15494a4c11aaSdan Tcl_Obj **apColName; 15504a4c11aaSdan dbEvalRowInfo(p, &nCol, &apColName); 15514a4c11aaSdan for(i=0; i<nCol; i++){ 15524a4c11aaSdan Tcl_Obj *pVal = dbEvalColumnValue(p, i); 15534a4c11aaSdan if( pArray==0 ){ 15544a4c11aaSdan Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0); 15554a4c11aaSdan }else{ 15564a4c11aaSdan Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); 15574a4c11aaSdan } 15584a4c11aaSdan } 15594a4c11aaSdan 15604a4c11aaSdan /* The required interpreter variables are now populated with the data 15614a4c11aaSdan ** from the current row. If using NRE, schedule callbacks to evaluate 15624a4c11aaSdan ** script pScript, then to invoke this function again to fetch the next 15634a4c11aaSdan ** row (or clean up if there is no next row or the script throws an 15644a4c11aaSdan ** exception). After scheduling the callbacks, return control to the 15654a4c11aaSdan ** caller. 15664a4c11aaSdan ** 15674a4c11aaSdan ** If not using NRE, evaluate pScript directly and continue with the 15684a4c11aaSdan ** next iteration of this while(...) loop. */ 15694a4c11aaSdan if( DbUseNre() ){ 15704a4c11aaSdan Tcl_NRAddCallback(interp, DbEvalNextCmd, (void*)p, (void*)pScript, 0, 0); 15714a4c11aaSdan return Tcl_NREvalObj(interp, pScript, 0); 15724a4c11aaSdan }else{ 15734a4c11aaSdan rc = Tcl_EvalObjEx(interp, pScript, 0); 15744a4c11aaSdan } 15754a4c11aaSdan } 15764a4c11aaSdan 15774a4c11aaSdan Tcl_DecrRefCount(pScript); 15784a4c11aaSdan dbEvalFinalize(p); 15794a4c11aaSdan Tcl_Free((char *)p); 15804a4c11aaSdan 15814a4c11aaSdan if( rc==TCL_OK || rc==TCL_BREAK ){ 15824a4c11aaSdan Tcl_ResetResult(interp); 15834a4c11aaSdan rc = TCL_OK; 15844a4c11aaSdan } 15854a4c11aaSdan return rc; 15868e556520Sdanielk1977 } 15878e556520Sdanielk1977 15881067fe11Stpoindex /* 158975897234Sdrh ** The "sqlite" command below creates a new Tcl command for each 159075897234Sdrh ** connection it opens to an SQLite database. This routine is invoked 159175897234Sdrh ** whenever one of those connection-specific commands is executed 159275897234Sdrh ** in Tcl. For example, if you run Tcl code like this: 159375897234Sdrh ** 15949bb575fdSdrh ** sqlite3 db1 "my_database" 159575897234Sdrh ** db1 close 159675897234Sdrh ** 159775897234Sdrh ** The first command opens a connection to the "my_database" database 159875897234Sdrh ** and calls that connection "db1". The second command causes this 159975897234Sdrh ** subroutine to be invoked. 160075897234Sdrh */ 16016d31316cSdrh static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ 1602bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 16036d31316cSdrh int choice; 160422fbcb8dSdrh int rc = TCL_OK; 16050de8c112Sdrh static const char *DB_strs[] = { 1606dc2c4915Sdrh "authorizer", "backup", "busy", 1607dc2c4915Sdrh "cache", "changes", "close", 1608dc2c4915Sdrh "collate", "collation_needed", "commit_hook", 1609dc2c4915Sdrh "complete", "copy", "enable_load_extension", 1610dc2c4915Sdrh "errorcode", "eval", "exists", 1611dc2c4915Sdrh "function", "incrblob", "interrupt", 1612833bf968Sdrh "last_insert_rowid", "nullvalue", "onecolumn", 1613833bf968Sdrh "profile", "progress", "rekey", 1614833bf968Sdrh "restore", "rollback_hook", "status", 1615833bf968Sdrh "timeout", "total_changes", "trace", 1616833bf968Sdrh "transaction", "unlock_notify", "update_hook", 1617833bf968Sdrh "version", "wal_hook", 0 16186d31316cSdrh }; 1619411995dcSdrh enum DB_enum { 1620dc2c4915Sdrh DB_AUTHORIZER, DB_BACKUP, DB_BUSY, 1621dc2c4915Sdrh DB_CACHE, DB_CHANGES, DB_CLOSE, 1622dc2c4915Sdrh DB_COLLATE, DB_COLLATION_NEEDED, DB_COMMIT_HOOK, 1623dc2c4915Sdrh DB_COMPLETE, DB_COPY, DB_ENABLE_LOAD_EXTENSION, 1624dc2c4915Sdrh DB_ERRORCODE, DB_EVAL, DB_EXISTS, 1625dc2c4915Sdrh DB_FUNCTION, DB_INCRBLOB, DB_INTERRUPT, 1626833bf968Sdrh DB_LAST_INSERT_ROWID, DB_NULLVALUE, DB_ONECOLUMN, 1627833bf968Sdrh DB_PROFILE, DB_PROGRESS, DB_REKEY, 1628833bf968Sdrh DB_RESTORE, DB_ROLLBACK_HOOK, DB_STATUS, 1629833bf968Sdrh DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE, 1630833bf968Sdrh DB_TRANSACTION, DB_UNLOCK_NOTIFY, DB_UPDATE_HOOK, 1631833bf968Sdrh DB_VERSION, DB_WAL_HOOK 16326d31316cSdrh }; 16331067fe11Stpoindex /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */ 16346d31316cSdrh 16356d31316cSdrh if( objc<2 ){ 16366d31316cSdrh Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); 163775897234Sdrh return TCL_ERROR; 163875897234Sdrh } 1639411995dcSdrh if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ 16406d31316cSdrh return TCL_ERROR; 16416d31316cSdrh } 16426d31316cSdrh 1643411995dcSdrh switch( (enum DB_enum)choice ){ 164475897234Sdrh 1645e22a334bSdrh /* $db authorizer ?CALLBACK? 1646e22a334bSdrh ** 1647e22a334bSdrh ** Invoke the given callback to authorize each SQL operation as it is 1648e22a334bSdrh ** compiled. 5 arguments are appended to the callback before it is 1649e22a334bSdrh ** invoked: 1650e22a334bSdrh ** 1651e22a334bSdrh ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) 1652e22a334bSdrh ** (2) First descriptive name (depends on authorization type) 1653e22a334bSdrh ** (3) Second descriptive name 1654e22a334bSdrh ** (4) Name of the database (ex: "main", "temp") 1655e22a334bSdrh ** (5) Name of trigger that is doing the access 1656e22a334bSdrh ** 1657e22a334bSdrh ** The callback should return on of the following strings: SQLITE_OK, 1658e22a334bSdrh ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. 1659e22a334bSdrh ** 1660e22a334bSdrh ** If this method is invoked with no arguments, the current authorization 1661e22a334bSdrh ** callback string is returned. 1662e22a334bSdrh */ 1663e22a334bSdrh case DB_AUTHORIZER: { 16641211de37Sdrh #ifdef SQLITE_OMIT_AUTHORIZATION 16651211de37Sdrh Tcl_AppendResult(interp, "authorization not available in this build", 0); 16661211de37Sdrh return TCL_ERROR; 16671211de37Sdrh #else 1668e22a334bSdrh if( objc>3 ){ 1669e22a334bSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 16700f14e2ebSdrh return TCL_ERROR; 1671e22a334bSdrh }else if( objc==2 ){ 1672b5a20d3cSdrh if( pDb->zAuth ){ 1673e22a334bSdrh Tcl_AppendResult(interp, pDb->zAuth, 0); 1674e22a334bSdrh } 1675e22a334bSdrh }else{ 1676e22a334bSdrh char *zAuth; 1677e22a334bSdrh int len; 1678e22a334bSdrh if( pDb->zAuth ){ 1679e22a334bSdrh Tcl_Free(pDb->zAuth); 1680e22a334bSdrh } 1681e22a334bSdrh zAuth = Tcl_GetStringFromObj(objv[2], &len); 1682e22a334bSdrh if( zAuth && len>0 ){ 1683e22a334bSdrh pDb->zAuth = Tcl_Alloc( len + 1 ); 16845bb3eb9bSdrh memcpy(pDb->zAuth, zAuth, len+1); 1685e22a334bSdrh }else{ 1686e22a334bSdrh pDb->zAuth = 0; 1687e22a334bSdrh } 1688e22a334bSdrh if( pDb->zAuth ){ 1689e22a334bSdrh pDb->interp = interp; 16906f8a503dSdanielk1977 sqlite3_set_authorizer(pDb->db, auth_callback, pDb); 1691e22a334bSdrh }else{ 16926f8a503dSdanielk1977 sqlite3_set_authorizer(pDb->db, 0, 0); 1693e22a334bSdrh } 1694e22a334bSdrh } 16951211de37Sdrh #endif 1696e22a334bSdrh break; 1697e22a334bSdrh } 1698e22a334bSdrh 1699dc2c4915Sdrh /* $db backup ?DATABASE? FILENAME 1700dc2c4915Sdrh ** 1701dc2c4915Sdrh ** Open or create a database file named FILENAME. Transfer the 1702dc2c4915Sdrh ** content of local database DATABASE (default: "main") into the 1703dc2c4915Sdrh ** FILENAME database. 1704dc2c4915Sdrh */ 1705dc2c4915Sdrh case DB_BACKUP: { 1706dc2c4915Sdrh const char *zDestFile; 1707dc2c4915Sdrh const char *zSrcDb; 1708dc2c4915Sdrh sqlite3 *pDest; 1709dc2c4915Sdrh sqlite3_backup *pBackup; 1710dc2c4915Sdrh 1711dc2c4915Sdrh if( objc==3 ){ 1712dc2c4915Sdrh zSrcDb = "main"; 1713dc2c4915Sdrh zDestFile = Tcl_GetString(objv[2]); 1714dc2c4915Sdrh }else if( objc==4 ){ 1715dc2c4915Sdrh zSrcDb = Tcl_GetString(objv[2]); 1716dc2c4915Sdrh zDestFile = Tcl_GetString(objv[3]); 1717dc2c4915Sdrh }else{ 1718dc2c4915Sdrh Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 1719dc2c4915Sdrh return TCL_ERROR; 1720dc2c4915Sdrh } 1721dc2c4915Sdrh rc = sqlite3_open(zDestFile, &pDest); 1722dc2c4915Sdrh if( rc!=SQLITE_OK ){ 1723dc2c4915Sdrh Tcl_AppendResult(interp, "cannot open target database: ", 1724dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1725dc2c4915Sdrh sqlite3_close(pDest); 1726dc2c4915Sdrh return TCL_ERROR; 1727dc2c4915Sdrh } 1728dc2c4915Sdrh pBackup = sqlite3_backup_init(pDest, "main", pDb->db, zSrcDb); 1729dc2c4915Sdrh if( pBackup==0 ){ 1730dc2c4915Sdrh Tcl_AppendResult(interp, "backup failed: ", 1731dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1732dc2c4915Sdrh sqlite3_close(pDest); 1733dc2c4915Sdrh return TCL_ERROR; 1734dc2c4915Sdrh } 1735dc2c4915Sdrh while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} 1736dc2c4915Sdrh sqlite3_backup_finish(pBackup); 1737dc2c4915Sdrh if( rc==SQLITE_DONE ){ 1738dc2c4915Sdrh rc = TCL_OK; 1739dc2c4915Sdrh }else{ 1740dc2c4915Sdrh Tcl_AppendResult(interp, "backup failed: ", 1741dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1742dc2c4915Sdrh rc = TCL_ERROR; 1743dc2c4915Sdrh } 1744dc2c4915Sdrh sqlite3_close(pDest); 1745dc2c4915Sdrh break; 1746dc2c4915Sdrh } 1747dc2c4915Sdrh 1748bec3f402Sdrh /* $db busy ?CALLBACK? 1749bec3f402Sdrh ** 1750bec3f402Sdrh ** Invoke the given callback if an SQL statement attempts to open 1751bec3f402Sdrh ** a locked database file. 1752bec3f402Sdrh */ 17536d31316cSdrh case DB_BUSY: { 17546d31316cSdrh if( objc>3 ){ 17556d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); 1756bec3f402Sdrh return TCL_ERROR; 17576d31316cSdrh }else if( objc==2 ){ 1758bec3f402Sdrh if( pDb->zBusy ){ 1759bec3f402Sdrh Tcl_AppendResult(interp, pDb->zBusy, 0); 1760bec3f402Sdrh } 1761bec3f402Sdrh }else{ 17626d31316cSdrh char *zBusy; 17636d31316cSdrh int len; 1764bec3f402Sdrh if( pDb->zBusy ){ 1765bec3f402Sdrh Tcl_Free(pDb->zBusy); 17666d31316cSdrh } 17676d31316cSdrh zBusy = Tcl_GetStringFromObj(objv[2], &len); 17686d31316cSdrh if( zBusy && len>0 ){ 17696d31316cSdrh pDb->zBusy = Tcl_Alloc( len + 1 ); 17705bb3eb9bSdrh memcpy(pDb->zBusy, zBusy, len+1); 17716d31316cSdrh }else{ 1772bec3f402Sdrh pDb->zBusy = 0; 1773bec3f402Sdrh } 1774bec3f402Sdrh if( pDb->zBusy ){ 1775bec3f402Sdrh pDb->interp = interp; 17766f8a503dSdanielk1977 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb); 17776d31316cSdrh }else{ 17786f8a503dSdanielk1977 sqlite3_busy_handler(pDb->db, 0, 0); 1779bec3f402Sdrh } 1780bec3f402Sdrh } 17816d31316cSdrh break; 17826d31316cSdrh } 1783bec3f402Sdrh 1784fb7e7651Sdrh /* $db cache flush 1785fb7e7651Sdrh ** $db cache size n 1786fb7e7651Sdrh ** 1787fb7e7651Sdrh ** Flush the prepared statement cache, or set the maximum number of 1788fb7e7651Sdrh ** cached statements. 1789fb7e7651Sdrh */ 1790fb7e7651Sdrh case DB_CACHE: { 1791fb7e7651Sdrh char *subCmd; 1792fb7e7651Sdrh int n; 1793fb7e7651Sdrh 1794fb7e7651Sdrh if( objc<=2 ){ 1795fb7e7651Sdrh Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?"); 1796fb7e7651Sdrh return TCL_ERROR; 1797fb7e7651Sdrh } 1798fb7e7651Sdrh subCmd = Tcl_GetStringFromObj( objv[2], 0 ); 1799fb7e7651Sdrh if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){ 1800fb7e7651Sdrh if( objc!=3 ){ 1801fb7e7651Sdrh Tcl_WrongNumArgs(interp, 2, objv, "flush"); 1802fb7e7651Sdrh return TCL_ERROR; 1803fb7e7651Sdrh }else{ 1804fb7e7651Sdrh flushStmtCache( pDb ); 1805fb7e7651Sdrh } 1806fb7e7651Sdrh }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){ 1807fb7e7651Sdrh if( objc!=4 ){ 1808fb7e7651Sdrh Tcl_WrongNumArgs(interp, 2, objv, "size n"); 1809fb7e7651Sdrh return TCL_ERROR; 1810fb7e7651Sdrh }else{ 1811fb7e7651Sdrh if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){ 1812fb7e7651Sdrh Tcl_AppendResult( interp, "cannot convert \"", 1813fb7e7651Sdrh Tcl_GetStringFromObj(objv[3],0), "\" to integer", 0); 1814fb7e7651Sdrh return TCL_ERROR; 1815fb7e7651Sdrh }else{ 1816fb7e7651Sdrh if( n<0 ){ 1817fb7e7651Sdrh flushStmtCache( pDb ); 1818fb7e7651Sdrh n = 0; 1819fb7e7651Sdrh }else if( n>MAX_PREPARED_STMTS ){ 1820fb7e7651Sdrh n = MAX_PREPARED_STMTS; 1821fb7e7651Sdrh } 1822fb7e7651Sdrh pDb->maxStmt = n; 1823fb7e7651Sdrh } 1824fb7e7651Sdrh } 1825fb7e7651Sdrh }else{ 1826fb7e7651Sdrh Tcl_AppendResult( interp, "bad option \"", 1827191fadcfSdanielk1977 Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size", 0); 1828fb7e7651Sdrh return TCL_ERROR; 1829fb7e7651Sdrh } 1830fb7e7651Sdrh break; 1831fb7e7651Sdrh } 1832fb7e7651Sdrh 1833b28af71aSdanielk1977 /* $db changes 1834c8d30ac1Sdrh ** 1835c8d30ac1Sdrh ** Return the number of rows that were modified, inserted, or deleted by 1836b28af71aSdanielk1977 ** the most recent INSERT, UPDATE or DELETE statement, not including 1837b28af71aSdanielk1977 ** any changes made by trigger programs. 1838c8d30ac1Sdrh */ 1839c8d30ac1Sdrh case DB_CHANGES: { 1840c8d30ac1Sdrh Tcl_Obj *pResult; 1841c8d30ac1Sdrh if( objc!=2 ){ 1842c8d30ac1Sdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 1843c8d30ac1Sdrh return TCL_ERROR; 1844c8d30ac1Sdrh } 1845c8d30ac1Sdrh pResult = Tcl_GetObjResult(interp); 1846b28af71aSdanielk1977 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db)); 1847f146a776Srdc break; 1848f146a776Srdc } 1849f146a776Srdc 185075897234Sdrh /* $db close 185175897234Sdrh ** 185275897234Sdrh ** Shutdown the database 185375897234Sdrh */ 18546d31316cSdrh case DB_CLOSE: { 18556d31316cSdrh Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); 18566d31316cSdrh break; 18576d31316cSdrh } 185875897234Sdrh 18590f14e2ebSdrh /* 18600f14e2ebSdrh ** $db collate NAME SCRIPT 18610f14e2ebSdrh ** 18620f14e2ebSdrh ** Create a new SQL collation function called NAME. Whenever 18630f14e2ebSdrh ** that function is called, invoke SCRIPT to evaluate the function. 18640f14e2ebSdrh */ 18650f14e2ebSdrh case DB_COLLATE: { 18660f14e2ebSdrh SqlCollate *pCollate; 18670f14e2ebSdrh char *zName; 18680f14e2ebSdrh char *zScript; 18690f14e2ebSdrh int nScript; 18700f14e2ebSdrh if( objc!=4 ){ 18710f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); 18720f14e2ebSdrh return TCL_ERROR; 18730f14e2ebSdrh } 18740f14e2ebSdrh zName = Tcl_GetStringFromObj(objv[2], 0); 18750f14e2ebSdrh zScript = Tcl_GetStringFromObj(objv[3], &nScript); 18760f14e2ebSdrh pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 ); 18770f14e2ebSdrh if( pCollate==0 ) return TCL_ERROR; 18780f14e2ebSdrh pCollate->interp = interp; 18790f14e2ebSdrh pCollate->pNext = pDb->pCollate; 18800f14e2ebSdrh pCollate->zScript = (char*)&pCollate[1]; 18810f14e2ebSdrh pDb->pCollate = pCollate; 18825bb3eb9bSdrh memcpy(pCollate->zScript, zScript, nScript+1); 18830f14e2ebSdrh if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, 18840f14e2ebSdrh pCollate, tclSqlCollate) ){ 18859636c4e1Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 18860f14e2ebSdrh return TCL_ERROR; 18870f14e2ebSdrh } 18880f14e2ebSdrh break; 18890f14e2ebSdrh } 18900f14e2ebSdrh 18910f14e2ebSdrh /* 18920f14e2ebSdrh ** $db collation_needed SCRIPT 18930f14e2ebSdrh ** 18940f14e2ebSdrh ** Create a new SQL collation function called NAME. Whenever 18950f14e2ebSdrh ** that function is called, invoke SCRIPT to evaluate the function. 18960f14e2ebSdrh */ 18970f14e2ebSdrh case DB_COLLATION_NEEDED: { 18980f14e2ebSdrh if( objc!=3 ){ 18990f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT"); 19000f14e2ebSdrh return TCL_ERROR; 19010f14e2ebSdrh } 19020f14e2ebSdrh if( pDb->pCollateNeeded ){ 19030f14e2ebSdrh Tcl_DecrRefCount(pDb->pCollateNeeded); 19040f14e2ebSdrh } 19050f14e2ebSdrh pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]); 19060f14e2ebSdrh Tcl_IncrRefCount(pDb->pCollateNeeded); 19070f14e2ebSdrh sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded); 19080f14e2ebSdrh break; 19090f14e2ebSdrh } 19100f14e2ebSdrh 191119e2d37fSdrh /* $db commit_hook ?CALLBACK? 191219e2d37fSdrh ** 191319e2d37fSdrh ** Invoke the given callback just before committing every SQL transaction. 191419e2d37fSdrh ** If the callback throws an exception or returns non-zero, then the 191519e2d37fSdrh ** transaction is aborted. If CALLBACK is an empty string, the callback 191619e2d37fSdrh ** is disabled. 191719e2d37fSdrh */ 191819e2d37fSdrh case DB_COMMIT_HOOK: { 191919e2d37fSdrh if( objc>3 ){ 192019e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 192119e2d37fSdrh return TCL_ERROR; 192219e2d37fSdrh }else if( objc==2 ){ 192319e2d37fSdrh if( pDb->zCommit ){ 192419e2d37fSdrh Tcl_AppendResult(interp, pDb->zCommit, 0); 192519e2d37fSdrh } 192619e2d37fSdrh }else{ 192719e2d37fSdrh char *zCommit; 192819e2d37fSdrh int len; 192919e2d37fSdrh if( pDb->zCommit ){ 193019e2d37fSdrh Tcl_Free(pDb->zCommit); 193119e2d37fSdrh } 193219e2d37fSdrh zCommit = Tcl_GetStringFromObj(objv[2], &len); 193319e2d37fSdrh if( zCommit && len>0 ){ 193419e2d37fSdrh pDb->zCommit = Tcl_Alloc( len + 1 ); 19355bb3eb9bSdrh memcpy(pDb->zCommit, zCommit, len+1); 193619e2d37fSdrh }else{ 193719e2d37fSdrh pDb->zCommit = 0; 193819e2d37fSdrh } 193919e2d37fSdrh if( pDb->zCommit ){ 194019e2d37fSdrh pDb->interp = interp; 194119e2d37fSdrh sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb); 194219e2d37fSdrh }else{ 194319e2d37fSdrh sqlite3_commit_hook(pDb->db, 0, 0); 194419e2d37fSdrh } 194519e2d37fSdrh } 194619e2d37fSdrh break; 194719e2d37fSdrh } 194819e2d37fSdrh 194975897234Sdrh /* $db complete SQL 195075897234Sdrh ** 195175897234Sdrh ** Return TRUE if SQL is a complete SQL statement. Return FALSE if 195275897234Sdrh ** additional lines of input are needed. This is similar to the 195375897234Sdrh ** built-in "info complete" command of Tcl. 195475897234Sdrh */ 19556d31316cSdrh case DB_COMPLETE: { 1956ccae6026Sdrh #ifndef SQLITE_OMIT_COMPLETE 19576d31316cSdrh Tcl_Obj *pResult; 19586d31316cSdrh int isComplete; 19596d31316cSdrh if( objc!=3 ){ 19606d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 196175897234Sdrh return TCL_ERROR; 196275897234Sdrh } 19636f8a503dSdanielk1977 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) ); 19646d31316cSdrh pResult = Tcl_GetObjResult(interp); 19656d31316cSdrh Tcl_SetBooleanObj(pResult, isComplete); 1966ccae6026Sdrh #endif 19676d31316cSdrh break; 19686d31316cSdrh } 196975897234Sdrh 197019e2d37fSdrh /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR? 197119e2d37fSdrh ** 197219e2d37fSdrh ** Copy data into table from filename, optionally using SEPARATOR 197319e2d37fSdrh ** as column separators. If a column contains a null string, or the 197419e2d37fSdrh ** value of NULLINDICATOR, a NULL is inserted for the column. 197519e2d37fSdrh ** conflict-algorithm is one of the sqlite conflict algorithms: 197619e2d37fSdrh ** rollback, abort, fail, ignore, replace 197719e2d37fSdrh ** On success, return the number of lines processed, not necessarily same 197819e2d37fSdrh ** as 'db changes' due to conflict-algorithm selected. 197919e2d37fSdrh ** 198019e2d37fSdrh ** This code is basically an implementation/enhancement of 198119e2d37fSdrh ** the sqlite3 shell.c ".import" command. 198219e2d37fSdrh ** 198319e2d37fSdrh ** This command usage is equivalent to the sqlite2.x COPY statement, 198419e2d37fSdrh ** which imports file data into a table using the PostgreSQL COPY file format: 198519e2d37fSdrh ** $db copy $conflit_algo $table_name $filename \t \\N 198619e2d37fSdrh */ 198719e2d37fSdrh case DB_COPY: { 198819e2d37fSdrh char *zTable; /* Insert data into this table */ 198919e2d37fSdrh char *zFile; /* The file from which to extract data */ 199019e2d37fSdrh char *zConflict; /* The conflict algorithm to use */ 199119e2d37fSdrh sqlite3_stmt *pStmt; /* A statement */ 199219e2d37fSdrh int nCol; /* Number of columns in the table */ 199319e2d37fSdrh int nByte; /* Number of bytes in an SQL string */ 199419e2d37fSdrh int i, j; /* Loop counters */ 199519e2d37fSdrh int nSep; /* Number of bytes in zSep[] */ 199619e2d37fSdrh int nNull; /* Number of bytes in zNull[] */ 199719e2d37fSdrh char *zSql; /* An SQL statement */ 199819e2d37fSdrh char *zLine; /* A single line of input from the file */ 199919e2d37fSdrh char **azCol; /* zLine[] broken up into columns */ 200019e2d37fSdrh char *zCommit; /* How to commit changes */ 200119e2d37fSdrh FILE *in; /* The input file */ 200219e2d37fSdrh int lineno = 0; /* Line number of input file */ 200319e2d37fSdrh char zLineNum[80]; /* Line number print buffer */ 200419e2d37fSdrh Tcl_Obj *pResult; /* interp result */ 200519e2d37fSdrh 200619e2d37fSdrh char *zSep; 200719e2d37fSdrh char *zNull; 200819e2d37fSdrh if( objc<5 || objc>7 ){ 200919e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, 201019e2d37fSdrh "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"); 201119e2d37fSdrh return TCL_ERROR; 201219e2d37fSdrh } 201319e2d37fSdrh if( objc>=6 ){ 201419e2d37fSdrh zSep = Tcl_GetStringFromObj(objv[5], 0); 201519e2d37fSdrh }else{ 201619e2d37fSdrh zSep = "\t"; 201719e2d37fSdrh } 201819e2d37fSdrh if( objc>=7 ){ 201919e2d37fSdrh zNull = Tcl_GetStringFromObj(objv[6], 0); 202019e2d37fSdrh }else{ 202119e2d37fSdrh zNull = ""; 202219e2d37fSdrh } 202319e2d37fSdrh zConflict = Tcl_GetStringFromObj(objv[2], 0); 202419e2d37fSdrh zTable = Tcl_GetStringFromObj(objv[3], 0); 202519e2d37fSdrh zFile = Tcl_GetStringFromObj(objv[4], 0); 20264f21c4afSdrh nSep = strlen30(zSep); 20274f21c4afSdrh nNull = strlen30(zNull); 202819e2d37fSdrh if( nSep==0 ){ 202919e2d37fSdrh Tcl_AppendResult(interp,"Error: non-null separator required for copy",0); 203019e2d37fSdrh return TCL_ERROR; 203119e2d37fSdrh } 20323e59c012Sdrh if(strcmp(zConflict, "rollback") != 0 && 20333e59c012Sdrh strcmp(zConflict, "abort" ) != 0 && 20343e59c012Sdrh strcmp(zConflict, "fail" ) != 0 && 20353e59c012Sdrh strcmp(zConflict, "ignore" ) != 0 && 20363e59c012Sdrh strcmp(zConflict, "replace" ) != 0 ) { 203719e2d37fSdrh Tcl_AppendResult(interp, "Error: \"", zConflict, 203819e2d37fSdrh "\", conflict-algorithm must be one of: rollback, " 203919e2d37fSdrh "abort, fail, ignore, or replace", 0); 204019e2d37fSdrh return TCL_ERROR; 204119e2d37fSdrh } 204219e2d37fSdrh zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); 204319e2d37fSdrh if( zSql==0 ){ 204419e2d37fSdrh Tcl_AppendResult(interp, "Error: no such table: ", zTable, 0); 204519e2d37fSdrh return TCL_ERROR; 204619e2d37fSdrh } 20474f21c4afSdrh nByte = strlen30(zSql); 20483e701a18Sdrh rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 204919e2d37fSdrh sqlite3_free(zSql); 205019e2d37fSdrh if( rc ){ 205119e2d37fSdrh Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0); 205219e2d37fSdrh nCol = 0; 205319e2d37fSdrh }else{ 205419e2d37fSdrh nCol = sqlite3_column_count(pStmt); 205519e2d37fSdrh } 205619e2d37fSdrh sqlite3_finalize(pStmt); 205719e2d37fSdrh if( nCol==0 ) { 205819e2d37fSdrh return TCL_ERROR; 205919e2d37fSdrh } 206019e2d37fSdrh zSql = malloc( nByte + 50 + nCol*2 ); 206119e2d37fSdrh if( zSql==0 ) { 206219e2d37fSdrh Tcl_AppendResult(interp, "Error: can't malloc()", 0); 206319e2d37fSdrh return TCL_ERROR; 206419e2d37fSdrh } 206519e2d37fSdrh sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?", 206619e2d37fSdrh zConflict, zTable); 20674f21c4afSdrh j = strlen30(zSql); 206819e2d37fSdrh for(i=1; i<nCol; i++){ 206919e2d37fSdrh zSql[j++] = ','; 207019e2d37fSdrh zSql[j++] = '?'; 207119e2d37fSdrh } 207219e2d37fSdrh zSql[j++] = ')'; 207319e2d37fSdrh zSql[j] = 0; 20743e701a18Sdrh rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 207519e2d37fSdrh free(zSql); 207619e2d37fSdrh if( rc ){ 207719e2d37fSdrh Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0); 207819e2d37fSdrh sqlite3_finalize(pStmt); 207919e2d37fSdrh return TCL_ERROR; 208019e2d37fSdrh } 208119e2d37fSdrh in = fopen(zFile, "rb"); 208219e2d37fSdrh if( in==0 ){ 208319e2d37fSdrh Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL); 208419e2d37fSdrh sqlite3_finalize(pStmt); 208519e2d37fSdrh return TCL_ERROR; 208619e2d37fSdrh } 208719e2d37fSdrh azCol = malloc( sizeof(azCol[0])*(nCol+1) ); 208819e2d37fSdrh if( azCol==0 ) { 208919e2d37fSdrh Tcl_AppendResult(interp, "Error: can't malloc()", 0); 209043617e9aSdrh fclose(in); 209119e2d37fSdrh return TCL_ERROR; 209219e2d37fSdrh } 20933752785fSdrh (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0); 209419e2d37fSdrh zCommit = "COMMIT"; 209519e2d37fSdrh while( (zLine = local_getline(0, in))!=0 ){ 209619e2d37fSdrh char *z; 209719e2d37fSdrh lineno++; 209819e2d37fSdrh azCol[0] = zLine; 209919e2d37fSdrh for(i=0, z=zLine; *z; z++){ 210019e2d37fSdrh if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){ 210119e2d37fSdrh *z = 0; 210219e2d37fSdrh i++; 210319e2d37fSdrh if( i<nCol ){ 210419e2d37fSdrh azCol[i] = &z[nSep]; 210519e2d37fSdrh z += nSep-1; 210619e2d37fSdrh } 210719e2d37fSdrh } 210819e2d37fSdrh } 210919e2d37fSdrh if( i+1!=nCol ){ 211019e2d37fSdrh char *zErr; 21114f21c4afSdrh int nErr = strlen30(zFile) + 200; 21125bb3eb9bSdrh zErr = malloc(nErr); 2113c1f4494eSdrh if( zErr ){ 21145bb3eb9bSdrh sqlite3_snprintf(nErr, zErr, 2115955de52cSdanielk1977 "Error: %s line %d: expected %d columns of data but found %d", 211619e2d37fSdrh zFile, lineno, nCol, i+1); 211719e2d37fSdrh Tcl_AppendResult(interp, zErr, 0); 211819e2d37fSdrh free(zErr); 2119c1f4494eSdrh } 212019e2d37fSdrh zCommit = "ROLLBACK"; 212119e2d37fSdrh break; 212219e2d37fSdrh } 212319e2d37fSdrh for(i=0; i<nCol; i++){ 212419e2d37fSdrh /* check for null data, if so, bind as null */ 2125ea678832Sdrh if( (nNull>0 && strcmp(azCol[i], zNull)==0) 21264f21c4afSdrh || strlen30(azCol[i])==0 2127ea678832Sdrh ){ 212819e2d37fSdrh sqlite3_bind_null(pStmt, i+1); 212919e2d37fSdrh }else{ 213019e2d37fSdrh sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); 213119e2d37fSdrh } 213219e2d37fSdrh } 213319e2d37fSdrh sqlite3_step(pStmt); 213419e2d37fSdrh rc = sqlite3_reset(pStmt); 213519e2d37fSdrh free(zLine); 213619e2d37fSdrh if( rc!=SQLITE_OK ){ 213719e2d37fSdrh Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), 0); 213819e2d37fSdrh zCommit = "ROLLBACK"; 213919e2d37fSdrh break; 214019e2d37fSdrh } 214119e2d37fSdrh } 214219e2d37fSdrh free(azCol); 214319e2d37fSdrh fclose(in); 214419e2d37fSdrh sqlite3_finalize(pStmt); 21453752785fSdrh (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0); 214619e2d37fSdrh 214719e2d37fSdrh if( zCommit[0] == 'C' ){ 214819e2d37fSdrh /* success, set result as number of lines processed */ 214919e2d37fSdrh pResult = Tcl_GetObjResult(interp); 215019e2d37fSdrh Tcl_SetIntObj(pResult, lineno); 215119e2d37fSdrh rc = TCL_OK; 215219e2d37fSdrh }else{ 215319e2d37fSdrh /* failure, append lineno where failed */ 21545bb3eb9bSdrh sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno); 215519e2d37fSdrh Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,0); 215619e2d37fSdrh rc = TCL_ERROR; 215719e2d37fSdrh } 215819e2d37fSdrh break; 215919e2d37fSdrh } 216019e2d37fSdrh 216175897234Sdrh /* 21624144905bSdrh ** $db enable_load_extension BOOLEAN 21634144905bSdrh ** 21644144905bSdrh ** Turn the extension loading feature on or off. It if off by 21654144905bSdrh ** default. 21664144905bSdrh */ 21674144905bSdrh case DB_ENABLE_LOAD_EXTENSION: { 2168f533acc0Sdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 21694144905bSdrh int onoff; 21704144905bSdrh if( objc!=3 ){ 21714144905bSdrh Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN"); 21724144905bSdrh return TCL_ERROR; 21734144905bSdrh } 21744144905bSdrh if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ 21754144905bSdrh return TCL_ERROR; 21764144905bSdrh } 21774144905bSdrh sqlite3_enable_load_extension(pDb->db, onoff); 21784144905bSdrh break; 2179f533acc0Sdrh #else 2180f533acc0Sdrh Tcl_AppendResult(interp, "extension loading is turned off at compile-time", 2181f533acc0Sdrh 0); 2182f533acc0Sdrh return TCL_ERROR; 2183f533acc0Sdrh #endif 21844144905bSdrh } 21854144905bSdrh 21864144905bSdrh /* 2187dcd997eaSdrh ** $db errorcode 2188dcd997eaSdrh ** 2189dcd997eaSdrh ** Return the numeric error code that was returned by the most recent 21906f8a503dSdanielk1977 ** call to sqlite3_exec(). 2191dcd997eaSdrh */ 2192dcd997eaSdrh case DB_ERRORCODE: { 2193f3ce83f5Sdanielk1977 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); 2194dcd997eaSdrh break; 2195dcd997eaSdrh } 2196dcd997eaSdrh 2197dcd997eaSdrh /* 21984a4c11aaSdan ** $db exists $sql 21991807ce37Sdrh ** $db onecolumn $sql 220075897234Sdrh ** 22014a4c11aaSdan ** The onecolumn method is the equivalent of: 22024a4c11aaSdan ** lindex [$db eval $sql] 0 22034a4c11aaSdan */ 22044a4c11aaSdan case DB_EXISTS: 22054a4c11aaSdan case DB_ONECOLUMN: { 22064a4c11aaSdan DbEvalContext sEval; 22074a4c11aaSdan if( objc!=3 ){ 22084a4c11aaSdan Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 22094a4c11aaSdan return TCL_ERROR; 22104a4c11aaSdan } 22114a4c11aaSdan 22124a4c11aaSdan dbEvalInit(&sEval, pDb, objv[2], 0); 22134a4c11aaSdan rc = dbEvalStep(&sEval); 22144a4c11aaSdan if( choice==DB_ONECOLUMN ){ 22154a4c11aaSdan if( rc==TCL_OK ){ 22164a4c11aaSdan Tcl_SetObjResult(interp, dbEvalColumnValue(&sEval, 0)); 2217d5f12cd5Sdan }else if( rc==TCL_BREAK ){ 2218d5f12cd5Sdan Tcl_ResetResult(interp); 22194a4c11aaSdan } 22204a4c11aaSdan }else if( rc==TCL_BREAK || rc==TCL_OK ){ 22214a4c11aaSdan Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc==TCL_OK)); 22224a4c11aaSdan } 22234a4c11aaSdan dbEvalFinalize(&sEval); 22244a4c11aaSdan 22254a4c11aaSdan if( rc==TCL_BREAK ){ 22264a4c11aaSdan rc = TCL_OK; 22274a4c11aaSdan } 22284a4c11aaSdan break; 22294a4c11aaSdan } 22304a4c11aaSdan 22314a4c11aaSdan /* 22324a4c11aaSdan ** $db eval $sql ?array? ?{ ...code... }? 22334a4c11aaSdan ** 223475897234Sdrh ** The SQL statement in $sql is evaluated. For each row, the values are 2235bec3f402Sdrh ** placed in elements of the array named "array" and ...code... is executed. 223675897234Sdrh ** If "array" and "code" are omitted, then no callback is every invoked. 223775897234Sdrh ** If "array" is an empty string, then the values are placed in variables 223875897234Sdrh ** that have the same name as the fields extracted by the query. 223975897234Sdrh */ 22404a4c11aaSdan case DB_EVAL: { 224192febd92Sdrh if( objc<3 || objc>5 ){ 2242895d7472Sdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?"); 224330ccda10Sdanielk1977 return TCL_ERROR; 224430ccda10Sdanielk1977 } 22454a4c11aaSdan 224692febd92Sdrh if( objc==3 ){ 22474a4c11aaSdan DbEvalContext sEval; 22484a4c11aaSdan Tcl_Obj *pRet = Tcl_NewObj(); 22494a4c11aaSdan Tcl_IncrRefCount(pRet); 22504a4c11aaSdan dbEvalInit(&sEval, pDb, objv[2], 0); 22514a4c11aaSdan while( TCL_OK==(rc = dbEvalStep(&sEval)) ){ 22524a4c11aaSdan int i; 22534a4c11aaSdan int nCol; 22544a4c11aaSdan dbEvalRowInfo(&sEval, &nCol, 0); 225592febd92Sdrh for(i=0; i<nCol; i++){ 22564a4c11aaSdan Tcl_ListObjAppendElement(interp, pRet, dbEvalColumnValue(&sEval, i)); 225792febd92Sdrh } 225830ccda10Sdanielk1977 } 22594a4c11aaSdan dbEvalFinalize(&sEval); 226090b6bb19Sdrh if( rc==TCL_BREAK ){ 22614a4c11aaSdan Tcl_SetObjResult(interp, pRet); 226290b6bb19Sdrh rc = TCL_OK; 226390b6bb19Sdrh } 2264ef2cb63eSdanielk1977 Tcl_DecrRefCount(pRet); 22654a4c11aaSdan }else{ 22664a4c11aaSdan ClientData cd[2]; 22674a4c11aaSdan DbEvalContext *p; 22684a4c11aaSdan Tcl_Obj *pArray = 0; 22694a4c11aaSdan Tcl_Obj *pScript; 22704a4c11aaSdan 22714a4c11aaSdan if( objc==5 && *(char *)Tcl_GetString(objv[3]) ){ 22724a4c11aaSdan pArray = objv[3]; 22734a4c11aaSdan } 22744a4c11aaSdan pScript = objv[objc-1]; 22754a4c11aaSdan Tcl_IncrRefCount(pScript); 22764a4c11aaSdan 22774a4c11aaSdan p = (DbEvalContext *)Tcl_Alloc(sizeof(DbEvalContext)); 22784a4c11aaSdan dbEvalInit(p, pDb, objv[2], pArray); 22794a4c11aaSdan 22804a4c11aaSdan cd[0] = (void *)p; 22814a4c11aaSdan cd[1] = (void *)pScript; 22824a4c11aaSdan rc = DbEvalNextCmd(cd, interp, TCL_OK); 22831807ce37Sdrh } 228430ccda10Sdanielk1977 break; 228530ccda10Sdanielk1977 } 2286bec3f402Sdrh 2287bec3f402Sdrh /* 2288e3602be8Sdrh ** $db function NAME [-argcount N] SCRIPT 2289cabb0819Sdrh ** 2290cabb0819Sdrh ** Create a new SQL function called NAME. Whenever that function is 2291cabb0819Sdrh ** called, invoke SCRIPT to evaluate the function. 2292cabb0819Sdrh */ 2293cabb0819Sdrh case DB_FUNCTION: { 2294cabb0819Sdrh SqlFunc *pFunc; 2295d1e4733dSdrh Tcl_Obj *pScript; 2296cabb0819Sdrh char *zName; 2297e3602be8Sdrh int nArg = -1; 2298e3602be8Sdrh if( objc==6 ){ 2299e3602be8Sdrh const char *z = Tcl_GetString(objv[3]); 23004f21c4afSdrh int n = strlen30(z); 2301e3602be8Sdrh if( n>2 && strncmp(z, "-argcount",n)==0 ){ 2302e3602be8Sdrh if( Tcl_GetIntFromObj(interp, objv[4], &nArg) ) return TCL_ERROR; 2303e3602be8Sdrh if( nArg<0 ){ 2304e3602be8Sdrh Tcl_AppendResult(interp, "number of arguments must be non-negative", 2305e3602be8Sdrh (char*)0); 2306cabb0819Sdrh return TCL_ERROR; 2307cabb0819Sdrh } 2308e3602be8Sdrh } 2309e3602be8Sdrh pScript = objv[5]; 2310e3602be8Sdrh }else if( objc!=4 ){ 2311e3602be8Sdrh Tcl_WrongNumArgs(interp, 2, objv, "NAME [-argcount N] SCRIPT"); 2312e3602be8Sdrh return TCL_ERROR; 2313e3602be8Sdrh }else{ 2314d1e4733dSdrh pScript = objv[3]; 2315e3602be8Sdrh } 2316e3602be8Sdrh zName = Tcl_GetStringFromObj(objv[2], 0); 2317d1e4733dSdrh pFunc = findSqlFunc(pDb, zName); 2318cabb0819Sdrh if( pFunc==0 ) return TCL_ERROR; 2319d1e4733dSdrh if( pFunc->pScript ){ 2320d1e4733dSdrh Tcl_DecrRefCount(pFunc->pScript); 2321d1e4733dSdrh } 2322d1e4733dSdrh pFunc->pScript = pScript; 2323d1e4733dSdrh Tcl_IncrRefCount(pScript); 2324d1e4733dSdrh pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript); 2325e3602be8Sdrh rc = sqlite3_create_function(pDb->db, zName, nArg, SQLITE_UTF8, 2326d8123366Sdanielk1977 pFunc, tclSqlFunc, 0, 0); 2327fb7e7651Sdrh if( rc!=SQLITE_OK ){ 2328fb7e7651Sdrh rc = TCL_ERROR; 23299636c4e1Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 2330fb7e7651Sdrh } 2331cabb0819Sdrh break; 2332cabb0819Sdrh } 2333cabb0819Sdrh 2334cabb0819Sdrh /* 23358cbadb02Sdanielk1977 ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID 2336b4e9af9fSdanielk1977 */ 2337b4e9af9fSdanielk1977 case DB_INCRBLOB: { 233832a0d8bbSdanielk1977 #ifdef SQLITE_OMIT_INCRBLOB 233932a0d8bbSdanielk1977 Tcl_AppendResult(interp, "incrblob not available in this build", 0); 234032a0d8bbSdanielk1977 return TCL_ERROR; 234132a0d8bbSdanielk1977 #else 23428cbadb02Sdanielk1977 int isReadonly = 0; 2343b4e9af9fSdanielk1977 const char *zDb = "main"; 2344b4e9af9fSdanielk1977 const char *zTable; 2345b4e9af9fSdanielk1977 const char *zColumn; 2346b3f787f4Sdrh Tcl_WideInt iRow; 2347b4e9af9fSdanielk1977 23488cbadb02Sdanielk1977 /* Check for the -readonly option */ 23498cbadb02Sdanielk1977 if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){ 23508cbadb02Sdanielk1977 isReadonly = 1; 23518cbadb02Sdanielk1977 } 23528cbadb02Sdanielk1977 23538cbadb02Sdanielk1977 if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){ 23548cbadb02Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID"); 2355b4e9af9fSdanielk1977 return TCL_ERROR; 2356b4e9af9fSdanielk1977 } 2357b4e9af9fSdanielk1977 23588cbadb02Sdanielk1977 if( objc==(6+isReadonly) ){ 2359b4e9af9fSdanielk1977 zDb = Tcl_GetString(objv[2]); 2360b4e9af9fSdanielk1977 } 2361b4e9af9fSdanielk1977 zTable = Tcl_GetString(objv[objc-3]); 2362b4e9af9fSdanielk1977 zColumn = Tcl_GetString(objv[objc-2]); 2363b4e9af9fSdanielk1977 rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow); 2364b4e9af9fSdanielk1977 2365b4e9af9fSdanielk1977 if( rc==TCL_OK ){ 23668cbadb02Sdanielk1977 rc = createIncrblobChannel( 23678cbadb02Sdanielk1977 interp, pDb, zDb, zTable, zColumn, iRow, isReadonly 23688cbadb02Sdanielk1977 ); 2369b4e9af9fSdanielk1977 } 237032a0d8bbSdanielk1977 #endif 2371b4e9af9fSdanielk1977 break; 2372b4e9af9fSdanielk1977 } 2373b4e9af9fSdanielk1977 2374b4e9af9fSdanielk1977 /* 2375f11bded5Sdrh ** $db interrupt 2376f11bded5Sdrh ** 2377f11bded5Sdrh ** Interrupt the execution of the inner-most SQL interpreter. This 2378f11bded5Sdrh ** causes the SQL statement to return an error of SQLITE_INTERRUPT. 2379f11bded5Sdrh */ 2380f11bded5Sdrh case DB_INTERRUPT: { 2381f11bded5Sdrh sqlite3_interrupt(pDb->db); 2382f11bded5Sdrh break; 2383f11bded5Sdrh } 2384f11bded5Sdrh 2385f11bded5Sdrh /* 238619e2d37fSdrh ** $db nullvalue ?STRING? 238719e2d37fSdrh ** 238819e2d37fSdrh ** Change text used when a NULL comes back from the database. If ?STRING? 238919e2d37fSdrh ** is not present, then the current string used for NULL is returned. 239019e2d37fSdrh ** If STRING is present, then STRING is returned. 239119e2d37fSdrh ** 239219e2d37fSdrh */ 239319e2d37fSdrh case DB_NULLVALUE: { 239419e2d37fSdrh if( objc!=2 && objc!=3 ){ 239519e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE"); 239619e2d37fSdrh return TCL_ERROR; 239719e2d37fSdrh } 239819e2d37fSdrh if( objc==3 ){ 239919e2d37fSdrh int len; 240019e2d37fSdrh char *zNull = Tcl_GetStringFromObj(objv[2], &len); 240119e2d37fSdrh if( pDb->zNull ){ 240219e2d37fSdrh Tcl_Free(pDb->zNull); 240319e2d37fSdrh } 240419e2d37fSdrh if( zNull && len>0 ){ 240519e2d37fSdrh pDb->zNull = Tcl_Alloc( len + 1 ); 24067fd33929Sdrh memcpy(pDb->zNull, zNull, len); 240719e2d37fSdrh pDb->zNull[len] = '\0'; 240819e2d37fSdrh }else{ 240919e2d37fSdrh pDb->zNull = 0; 241019e2d37fSdrh } 241119e2d37fSdrh } 2412c45e6716Sdrh Tcl_SetObjResult(interp, Tcl_NewStringObj(pDb->zNull, -1)); 241319e2d37fSdrh break; 241419e2d37fSdrh } 241519e2d37fSdrh 241619e2d37fSdrh /* 2417af9ff33aSdrh ** $db last_insert_rowid 2418af9ff33aSdrh ** 2419af9ff33aSdrh ** Return an integer which is the ROWID for the most recent insert. 2420af9ff33aSdrh */ 2421af9ff33aSdrh case DB_LAST_INSERT_ROWID: { 2422af9ff33aSdrh Tcl_Obj *pResult; 2423f7e678d6Sdrh Tcl_WideInt rowid; 2424af9ff33aSdrh if( objc!=2 ){ 2425af9ff33aSdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 2426af9ff33aSdrh return TCL_ERROR; 2427af9ff33aSdrh } 24286f8a503dSdanielk1977 rowid = sqlite3_last_insert_rowid(pDb->db); 2429af9ff33aSdrh pResult = Tcl_GetObjResult(interp); 2430f7e678d6Sdrh Tcl_SetWideIntObj(pResult, rowid); 2431af9ff33aSdrh break; 2432af9ff33aSdrh } 2433af9ff33aSdrh 2434af9ff33aSdrh /* 24354a4c11aaSdan ** The DB_ONECOLUMN method is implemented together with DB_EXISTS. 24365d9d7576Sdrh */ 24371807ce37Sdrh 24381807ce37Sdrh /* $db progress ?N CALLBACK? 24391807ce37Sdrh ** 24401807ce37Sdrh ** Invoke the given callback every N virtual machine opcodes while executing 24411807ce37Sdrh ** queries. 24421807ce37Sdrh */ 24431807ce37Sdrh case DB_PROGRESS: { 24441807ce37Sdrh if( objc==2 ){ 24451807ce37Sdrh if( pDb->zProgress ){ 24461807ce37Sdrh Tcl_AppendResult(interp, pDb->zProgress, 0); 24475d9d7576Sdrh } 24481807ce37Sdrh }else if( objc==4 ){ 24491807ce37Sdrh char *zProgress; 24501807ce37Sdrh int len; 24511807ce37Sdrh int N; 24521807ce37Sdrh if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ 24531807ce37Sdrh return TCL_ERROR; 24541807ce37Sdrh }; 24551807ce37Sdrh if( pDb->zProgress ){ 24561807ce37Sdrh Tcl_Free(pDb->zProgress); 24571807ce37Sdrh } 24581807ce37Sdrh zProgress = Tcl_GetStringFromObj(objv[3], &len); 24591807ce37Sdrh if( zProgress && len>0 ){ 24601807ce37Sdrh pDb->zProgress = Tcl_Alloc( len + 1 ); 24615bb3eb9bSdrh memcpy(pDb->zProgress, zProgress, len+1); 24621807ce37Sdrh }else{ 24631807ce37Sdrh pDb->zProgress = 0; 24641807ce37Sdrh } 24651807ce37Sdrh #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 24661807ce37Sdrh if( pDb->zProgress ){ 24671807ce37Sdrh pDb->interp = interp; 24681807ce37Sdrh sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb); 24691807ce37Sdrh }else{ 24701807ce37Sdrh sqlite3_progress_handler(pDb->db, 0, 0, 0); 24711807ce37Sdrh } 24721807ce37Sdrh #endif 24731807ce37Sdrh }else{ 24741807ce37Sdrh Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); 24751807ce37Sdrh return TCL_ERROR; 24765d9d7576Sdrh } 24775d9d7576Sdrh break; 24785d9d7576Sdrh } 24795d9d7576Sdrh 248019e2d37fSdrh /* $db profile ?CALLBACK? 248119e2d37fSdrh ** 248219e2d37fSdrh ** Make arrangements to invoke the CALLBACK routine after each SQL statement 248319e2d37fSdrh ** that has run. The text of the SQL and the amount of elapse time are 248419e2d37fSdrh ** appended to CALLBACK before the script is run. 248519e2d37fSdrh */ 248619e2d37fSdrh case DB_PROFILE: { 248719e2d37fSdrh if( objc>3 ){ 248819e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 248919e2d37fSdrh return TCL_ERROR; 249019e2d37fSdrh }else if( objc==2 ){ 249119e2d37fSdrh if( pDb->zProfile ){ 249219e2d37fSdrh Tcl_AppendResult(interp, pDb->zProfile, 0); 249319e2d37fSdrh } 249419e2d37fSdrh }else{ 249519e2d37fSdrh char *zProfile; 249619e2d37fSdrh int len; 249719e2d37fSdrh if( pDb->zProfile ){ 249819e2d37fSdrh Tcl_Free(pDb->zProfile); 249919e2d37fSdrh } 250019e2d37fSdrh zProfile = Tcl_GetStringFromObj(objv[2], &len); 250119e2d37fSdrh if( zProfile && len>0 ){ 250219e2d37fSdrh pDb->zProfile = Tcl_Alloc( len + 1 ); 25035bb3eb9bSdrh memcpy(pDb->zProfile, zProfile, len+1); 250419e2d37fSdrh }else{ 250519e2d37fSdrh pDb->zProfile = 0; 250619e2d37fSdrh } 2507bb201344Sshaneh #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) 250819e2d37fSdrh if( pDb->zProfile ){ 250919e2d37fSdrh pDb->interp = interp; 251019e2d37fSdrh sqlite3_profile(pDb->db, DbProfileHandler, pDb); 251119e2d37fSdrh }else{ 251219e2d37fSdrh sqlite3_profile(pDb->db, 0, 0); 251319e2d37fSdrh } 251419e2d37fSdrh #endif 251519e2d37fSdrh } 251619e2d37fSdrh break; 251719e2d37fSdrh } 251819e2d37fSdrh 25195d9d7576Sdrh /* 252022fbcb8dSdrh ** $db rekey KEY 252122fbcb8dSdrh ** 252222fbcb8dSdrh ** Change the encryption key on the currently open database. 252322fbcb8dSdrh */ 252422fbcb8dSdrh case DB_REKEY: { 2525b07028f7Sdrh #ifdef SQLITE_HAS_CODEC 252622fbcb8dSdrh int nKey; 252722fbcb8dSdrh void *pKey; 2528b07028f7Sdrh #endif 252922fbcb8dSdrh if( objc!=3 ){ 253022fbcb8dSdrh Tcl_WrongNumArgs(interp, 2, objv, "KEY"); 253122fbcb8dSdrh return TCL_ERROR; 253222fbcb8dSdrh } 25339eb9e26bSdrh #ifdef SQLITE_HAS_CODEC 2534b07028f7Sdrh pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); 25352011d5f5Sdrh rc = sqlite3_rekey(pDb->db, pKey, nKey); 253622fbcb8dSdrh if( rc ){ 25375dac8432Smistachkin Tcl_AppendResult(interp, sqlite3_errstr(rc), 0); 253822fbcb8dSdrh rc = TCL_ERROR; 253922fbcb8dSdrh } 254022fbcb8dSdrh #endif 254122fbcb8dSdrh break; 254222fbcb8dSdrh } 254322fbcb8dSdrh 2544dc2c4915Sdrh /* $db restore ?DATABASE? FILENAME 2545dc2c4915Sdrh ** 2546dc2c4915Sdrh ** Open a database file named FILENAME. Transfer the content 2547dc2c4915Sdrh ** of FILENAME into the local database DATABASE (default: "main"). 2548dc2c4915Sdrh */ 2549dc2c4915Sdrh case DB_RESTORE: { 2550dc2c4915Sdrh const char *zSrcFile; 2551dc2c4915Sdrh const char *zDestDb; 2552dc2c4915Sdrh sqlite3 *pSrc; 2553dc2c4915Sdrh sqlite3_backup *pBackup; 2554dc2c4915Sdrh int nTimeout = 0; 2555dc2c4915Sdrh 2556dc2c4915Sdrh if( objc==3 ){ 2557dc2c4915Sdrh zDestDb = "main"; 2558dc2c4915Sdrh zSrcFile = Tcl_GetString(objv[2]); 2559dc2c4915Sdrh }else if( objc==4 ){ 2560dc2c4915Sdrh zDestDb = Tcl_GetString(objv[2]); 2561dc2c4915Sdrh zSrcFile = Tcl_GetString(objv[3]); 2562dc2c4915Sdrh }else{ 2563dc2c4915Sdrh Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 2564dc2c4915Sdrh return TCL_ERROR; 2565dc2c4915Sdrh } 2566dc2c4915Sdrh rc = sqlite3_open_v2(zSrcFile, &pSrc, SQLITE_OPEN_READONLY, 0); 2567dc2c4915Sdrh if( rc!=SQLITE_OK ){ 2568dc2c4915Sdrh Tcl_AppendResult(interp, "cannot open source database: ", 2569dc2c4915Sdrh sqlite3_errmsg(pSrc), (char*)0); 2570dc2c4915Sdrh sqlite3_close(pSrc); 2571dc2c4915Sdrh return TCL_ERROR; 2572dc2c4915Sdrh } 2573dc2c4915Sdrh pBackup = sqlite3_backup_init(pDb->db, zDestDb, pSrc, "main"); 2574dc2c4915Sdrh if( pBackup==0 ){ 2575dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: ", 2576dc2c4915Sdrh sqlite3_errmsg(pDb->db), (char*)0); 2577dc2c4915Sdrh sqlite3_close(pSrc); 2578dc2c4915Sdrh return TCL_ERROR; 2579dc2c4915Sdrh } 2580dc2c4915Sdrh while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK 2581dc2c4915Sdrh || rc==SQLITE_BUSY ){ 2582dc2c4915Sdrh if( rc==SQLITE_BUSY ){ 2583dc2c4915Sdrh if( nTimeout++ >= 3 ) break; 2584dc2c4915Sdrh sqlite3_sleep(100); 2585dc2c4915Sdrh } 2586dc2c4915Sdrh } 2587dc2c4915Sdrh sqlite3_backup_finish(pBackup); 2588dc2c4915Sdrh if( rc==SQLITE_DONE ){ 2589dc2c4915Sdrh rc = TCL_OK; 2590dc2c4915Sdrh }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 2591dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: source database busy", 2592dc2c4915Sdrh (char*)0); 2593dc2c4915Sdrh rc = TCL_ERROR; 2594dc2c4915Sdrh }else{ 2595dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: ", 2596dc2c4915Sdrh sqlite3_errmsg(pDb->db), (char*)0); 2597dc2c4915Sdrh rc = TCL_ERROR; 2598dc2c4915Sdrh } 2599dc2c4915Sdrh sqlite3_close(pSrc); 2600dc2c4915Sdrh break; 2601dc2c4915Sdrh } 2602dc2c4915Sdrh 260322fbcb8dSdrh /* 26043c379b01Sdrh ** $db status (step|sort|autoindex) 2605d1d38488Sdrh ** 2606d1d38488Sdrh ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or 2607d1d38488Sdrh ** SQLITE_STMTSTATUS_SORT for the most recent eval. 2608d1d38488Sdrh */ 2609d1d38488Sdrh case DB_STATUS: { 2610d1d38488Sdrh int v; 2611d1d38488Sdrh const char *zOp; 2612d1d38488Sdrh if( objc!=3 ){ 26131c320a43Sdrh Tcl_WrongNumArgs(interp, 2, objv, "(step|sort|autoindex)"); 2614d1d38488Sdrh return TCL_ERROR; 2615d1d38488Sdrh } 2616d1d38488Sdrh zOp = Tcl_GetString(objv[2]); 2617d1d38488Sdrh if( strcmp(zOp, "step")==0 ){ 2618d1d38488Sdrh v = pDb->nStep; 2619d1d38488Sdrh }else if( strcmp(zOp, "sort")==0 ){ 2620d1d38488Sdrh v = pDb->nSort; 26213c379b01Sdrh }else if( strcmp(zOp, "autoindex")==0 ){ 26223c379b01Sdrh v = pDb->nIndex; 2623d1d38488Sdrh }else{ 26243c379b01Sdrh Tcl_AppendResult(interp, 26253c379b01Sdrh "bad argument: should be autoindex, step, or sort", 2626d1d38488Sdrh (char*)0); 2627d1d38488Sdrh return TCL_ERROR; 2628d1d38488Sdrh } 2629d1d38488Sdrh Tcl_SetObjResult(interp, Tcl_NewIntObj(v)); 2630d1d38488Sdrh break; 2631d1d38488Sdrh } 2632d1d38488Sdrh 2633d1d38488Sdrh /* 2634bec3f402Sdrh ** $db timeout MILLESECONDS 2635bec3f402Sdrh ** 2636bec3f402Sdrh ** Delay for the number of milliseconds specified when a file is locked. 2637bec3f402Sdrh */ 26386d31316cSdrh case DB_TIMEOUT: { 2639bec3f402Sdrh int ms; 26406d31316cSdrh if( objc!=3 ){ 26416d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); 2642bec3f402Sdrh return TCL_ERROR; 264375897234Sdrh } 26446d31316cSdrh if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; 26456f8a503dSdanielk1977 sqlite3_busy_timeout(pDb->db, ms); 26466d31316cSdrh break; 264775897234Sdrh } 2648b5a20d3cSdrh 26490f14e2ebSdrh /* 26500f14e2ebSdrh ** $db total_changes 26510f14e2ebSdrh ** 26520f14e2ebSdrh ** Return the number of rows that were modified, inserted, or deleted 26530f14e2ebSdrh ** since the database handle was created. 26540f14e2ebSdrh */ 26550f14e2ebSdrh case DB_TOTAL_CHANGES: { 26560f14e2ebSdrh Tcl_Obj *pResult; 26570f14e2ebSdrh if( objc!=2 ){ 26580f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 26590f14e2ebSdrh return TCL_ERROR; 26600f14e2ebSdrh } 26610f14e2ebSdrh pResult = Tcl_GetObjResult(interp); 26620f14e2ebSdrh Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db)); 26630f14e2ebSdrh break; 26640f14e2ebSdrh } 26650f14e2ebSdrh 2666b5a20d3cSdrh /* $db trace ?CALLBACK? 2667b5a20d3cSdrh ** 2668b5a20d3cSdrh ** Make arrangements to invoke the CALLBACK routine for each SQL statement 2669b5a20d3cSdrh ** that is executed. The text of the SQL is appended to CALLBACK before 2670b5a20d3cSdrh ** it is executed. 2671b5a20d3cSdrh */ 2672b5a20d3cSdrh case DB_TRACE: { 2673b5a20d3cSdrh if( objc>3 ){ 2674b5a20d3cSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 2675b97759edSdrh return TCL_ERROR; 2676b5a20d3cSdrh }else if( objc==2 ){ 2677b5a20d3cSdrh if( pDb->zTrace ){ 2678b5a20d3cSdrh Tcl_AppendResult(interp, pDb->zTrace, 0); 2679b5a20d3cSdrh } 2680b5a20d3cSdrh }else{ 2681b5a20d3cSdrh char *zTrace; 2682b5a20d3cSdrh int len; 2683b5a20d3cSdrh if( pDb->zTrace ){ 2684b5a20d3cSdrh Tcl_Free(pDb->zTrace); 2685b5a20d3cSdrh } 2686b5a20d3cSdrh zTrace = Tcl_GetStringFromObj(objv[2], &len); 2687b5a20d3cSdrh if( zTrace && len>0 ){ 2688b5a20d3cSdrh pDb->zTrace = Tcl_Alloc( len + 1 ); 26895bb3eb9bSdrh memcpy(pDb->zTrace, zTrace, len+1); 2690b5a20d3cSdrh }else{ 2691b5a20d3cSdrh pDb->zTrace = 0; 2692b5a20d3cSdrh } 2693bb201344Sshaneh #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) 2694b5a20d3cSdrh if( pDb->zTrace ){ 2695b5a20d3cSdrh pDb->interp = interp; 26966f8a503dSdanielk1977 sqlite3_trace(pDb->db, DbTraceHandler, pDb); 2697b5a20d3cSdrh }else{ 26986f8a503dSdanielk1977 sqlite3_trace(pDb->db, 0, 0); 2699b5a20d3cSdrh } 270019e2d37fSdrh #endif 2701b5a20d3cSdrh } 2702b5a20d3cSdrh break; 2703b5a20d3cSdrh } 2704b5a20d3cSdrh 27053d21423cSdrh /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT 27063d21423cSdrh ** 27073d21423cSdrh ** Start a new transaction (if we are not already in the midst of a 27083d21423cSdrh ** transaction) and execute the TCL script SCRIPT. After SCRIPT 27093d21423cSdrh ** completes, either commit the transaction or roll it back if SCRIPT 27103d21423cSdrh ** throws an exception. Or if no new transation was started, do nothing. 27113d21423cSdrh ** pass the exception on up the stack. 27123d21423cSdrh ** 27133d21423cSdrh ** This command was inspired by Dave Thomas's talk on Ruby at the 27143d21423cSdrh ** 2005 O'Reilly Open Source Convention (OSCON). 27153d21423cSdrh */ 27163d21423cSdrh case DB_TRANSACTION: { 27173d21423cSdrh Tcl_Obj *pScript; 2718cd38d520Sdanielk1977 const char *zBegin = "SAVEPOINT _tcl_transaction"; 27193d21423cSdrh if( objc!=3 && objc!=4 ){ 27203d21423cSdrh Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT"); 27213d21423cSdrh return TCL_ERROR; 27223d21423cSdrh } 2723cd38d520Sdanielk1977 27244a4c11aaSdan if( pDb->nTransaction==0 && objc==4 ){ 27253d21423cSdrh static const char *TTYPE_strs[] = { 2726ce604012Sdrh "deferred", "exclusive", "immediate", 0 27273d21423cSdrh }; 27283d21423cSdrh enum TTYPE_enum { 27293d21423cSdrh TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE 27303d21423cSdrh }; 27313d21423cSdrh int ttype; 2732b5555e7eSdrh if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type", 27333d21423cSdrh 0, &ttype) ){ 27343d21423cSdrh return TCL_ERROR; 27353d21423cSdrh } 27363d21423cSdrh switch( (enum TTYPE_enum)ttype ){ 27373d21423cSdrh case TTYPE_DEFERRED: /* no-op */; break; 27383d21423cSdrh case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break; 27393d21423cSdrh case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break; 27403d21423cSdrh } 27413d21423cSdrh } 2742cd38d520Sdanielk1977 pScript = objv[objc-1]; 2743cd38d520Sdanielk1977 27444a4c11aaSdan /* Run the SQLite BEGIN command to open a transaction or savepoint. */ 27451f1549f8Sdrh pDb->disableAuth++; 2746cd38d520Sdanielk1977 rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0); 27471f1549f8Sdrh pDb->disableAuth--; 2748cd38d520Sdanielk1977 if( rc!=SQLITE_OK ){ 2749cd38d520Sdanielk1977 Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); 2750cd38d520Sdanielk1977 return TCL_ERROR; 27513d21423cSdrh } 2752cd38d520Sdanielk1977 pDb->nTransaction++; 2753cd38d520Sdanielk1977 27544a4c11aaSdan /* If using NRE, schedule a callback to invoke the script pScript, then 27554a4c11aaSdan ** a second callback to commit (or rollback) the transaction or savepoint 27564a4c11aaSdan ** opened above. If not using NRE, evaluate the script directly, then 27574a4c11aaSdan ** call function DbTransPostCmd() to commit (or rollback) the transaction 27584a4c11aaSdan ** or savepoint. */ 27594a4c11aaSdan if( DbUseNre() ){ 27604a4c11aaSdan Tcl_NRAddCallback(interp, DbTransPostCmd, cd, 0, 0, 0); 27614a4c11aaSdan Tcl_NREvalObj(interp, pScript, 0); 27623d21423cSdrh }else{ 27634a4c11aaSdan rc = DbTransPostCmd(&cd, interp, Tcl_EvalObjEx(interp, pScript, 0)); 27643d21423cSdrh } 27653d21423cSdrh break; 27663d21423cSdrh } 27673d21423cSdrh 276894eb6a14Sdanielk1977 /* 2769404ca075Sdanielk1977 ** $db unlock_notify ?script? 2770404ca075Sdanielk1977 */ 2771404ca075Sdanielk1977 case DB_UNLOCK_NOTIFY: { 2772404ca075Sdanielk1977 #ifndef SQLITE_ENABLE_UNLOCK_NOTIFY 2773404ca075Sdanielk1977 Tcl_AppendResult(interp, "unlock_notify not available in this build", 0); 2774404ca075Sdanielk1977 rc = TCL_ERROR; 2775404ca075Sdanielk1977 #else 2776404ca075Sdanielk1977 if( objc!=2 && objc!=3 ){ 2777404ca075Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 2778404ca075Sdanielk1977 rc = TCL_ERROR; 2779404ca075Sdanielk1977 }else{ 2780404ca075Sdanielk1977 void (*xNotify)(void **, int) = 0; 2781404ca075Sdanielk1977 void *pNotifyArg = 0; 2782404ca075Sdanielk1977 2783404ca075Sdanielk1977 if( pDb->pUnlockNotify ){ 2784404ca075Sdanielk1977 Tcl_DecrRefCount(pDb->pUnlockNotify); 2785404ca075Sdanielk1977 pDb->pUnlockNotify = 0; 2786404ca075Sdanielk1977 } 2787404ca075Sdanielk1977 2788404ca075Sdanielk1977 if( objc==3 ){ 2789404ca075Sdanielk1977 xNotify = DbUnlockNotify; 2790404ca075Sdanielk1977 pNotifyArg = (void *)pDb; 2791404ca075Sdanielk1977 pDb->pUnlockNotify = objv[2]; 2792404ca075Sdanielk1977 Tcl_IncrRefCount(pDb->pUnlockNotify); 2793404ca075Sdanielk1977 } 2794404ca075Sdanielk1977 2795404ca075Sdanielk1977 if( sqlite3_unlock_notify(pDb->db, xNotify, pNotifyArg) ){ 2796404ca075Sdanielk1977 Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); 2797404ca075Sdanielk1977 rc = TCL_ERROR; 2798404ca075Sdanielk1977 } 2799404ca075Sdanielk1977 } 2800404ca075Sdanielk1977 #endif 2801404ca075Sdanielk1977 break; 2802404ca075Sdanielk1977 } 2803404ca075Sdanielk1977 2804404ca075Sdanielk1977 /* 2805833bf968Sdrh ** $db wal_hook ?script? 280694eb6a14Sdanielk1977 ** $db update_hook ?script? 280771fd80bfSdanielk1977 ** $db rollback_hook ?script? 280894eb6a14Sdanielk1977 */ 2809833bf968Sdrh case DB_WAL_HOOK: 281071fd80bfSdanielk1977 case DB_UPDATE_HOOK: 281171fd80bfSdanielk1977 case DB_ROLLBACK_HOOK: { 281271fd80bfSdanielk1977 281371fd80bfSdanielk1977 /* set ppHook to point at pUpdateHook or pRollbackHook, depending on 281471fd80bfSdanielk1977 ** whether [$db update_hook] or [$db rollback_hook] was invoked. 281571fd80bfSdanielk1977 */ 281671fd80bfSdanielk1977 Tcl_Obj **ppHook; 281771fd80bfSdanielk1977 if( choice==DB_UPDATE_HOOK ){ 281871fd80bfSdanielk1977 ppHook = &pDb->pUpdateHook; 2819833bf968Sdrh }else if( choice==DB_WAL_HOOK ){ 28205def0843Sdrh ppHook = &pDb->pWalHook; 282171fd80bfSdanielk1977 }else{ 282271fd80bfSdanielk1977 ppHook = &pDb->pRollbackHook; 282371fd80bfSdanielk1977 } 282471fd80bfSdanielk1977 282594eb6a14Sdanielk1977 if( objc!=2 && objc!=3 ){ 282694eb6a14Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 282794eb6a14Sdanielk1977 return TCL_ERROR; 282894eb6a14Sdanielk1977 } 282971fd80bfSdanielk1977 if( *ppHook ){ 283071fd80bfSdanielk1977 Tcl_SetObjResult(interp, *ppHook); 283194eb6a14Sdanielk1977 if( objc==3 ){ 283271fd80bfSdanielk1977 Tcl_DecrRefCount(*ppHook); 283371fd80bfSdanielk1977 *ppHook = 0; 283494eb6a14Sdanielk1977 } 283594eb6a14Sdanielk1977 } 283694eb6a14Sdanielk1977 if( objc==3 ){ 283771fd80bfSdanielk1977 assert( !(*ppHook) ); 283894eb6a14Sdanielk1977 if( Tcl_GetCharLength(objv[2])>0 ){ 283971fd80bfSdanielk1977 *ppHook = objv[2]; 284071fd80bfSdanielk1977 Tcl_IncrRefCount(*ppHook); 284194eb6a14Sdanielk1977 } 284294eb6a14Sdanielk1977 } 284371fd80bfSdanielk1977 284471fd80bfSdanielk1977 sqlite3_update_hook(pDb->db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); 284571fd80bfSdanielk1977 sqlite3_rollback_hook(pDb->db,(pDb->pRollbackHook?DbRollbackHandler:0),pDb); 28465def0843Sdrh sqlite3_wal_hook(pDb->db,(pDb->pWalHook?DbWalHandler:0),pDb); 284771fd80bfSdanielk1977 284894eb6a14Sdanielk1977 break; 284994eb6a14Sdanielk1977 } 285094eb6a14Sdanielk1977 28514397de57Sdanielk1977 /* $db version 28524397de57Sdanielk1977 ** 28534397de57Sdanielk1977 ** Return the version string for this database. 28544397de57Sdanielk1977 */ 28554397de57Sdanielk1977 case DB_VERSION: { 28564397de57Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC); 28574397de57Sdanielk1977 break; 28584397de57Sdanielk1977 } 28594397de57Sdanielk1977 28601067fe11Stpoindex 28616d31316cSdrh } /* End of the SWITCH statement */ 286222fbcb8dSdrh return rc; 286375897234Sdrh } 286475897234Sdrh 2865a2c8a95bSdrh #if SQLITE_TCL_NRE 2866a2c8a95bSdrh /* 2867a2c8a95bSdrh ** Adaptor that provides an objCmd interface to the NRE-enabled 2868a2c8a95bSdrh ** interface implementation. 2869a2c8a95bSdrh */ 2870a2c8a95bSdrh static int DbObjCmdAdaptor( 2871a2c8a95bSdrh void *cd, 2872a2c8a95bSdrh Tcl_Interp *interp, 2873a2c8a95bSdrh int objc, 2874a2c8a95bSdrh Tcl_Obj *const*objv 2875a2c8a95bSdrh ){ 2876a2c8a95bSdrh return Tcl_NRCallObjProc(interp, DbObjCmd, cd, objc, objv); 2877a2c8a95bSdrh } 2878a2c8a95bSdrh #endif /* SQLITE_TCL_NRE */ 2879a2c8a95bSdrh 288075897234Sdrh /* 28813570ad93Sdrh ** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN? 28829a6284c1Sdanielk1977 ** ?-create BOOLEAN? ?-nomutex BOOLEAN? 288375897234Sdrh ** 288475897234Sdrh ** This is the main Tcl command. When the "sqlite" Tcl command is 288575897234Sdrh ** invoked, this routine runs to process that command. 288675897234Sdrh ** 288775897234Sdrh ** The first argument, DBNAME, is an arbitrary name for a new 288875897234Sdrh ** database connection. This command creates a new command named 288975897234Sdrh ** DBNAME that is used to control that connection. The database 289075897234Sdrh ** connection is deleted when the DBNAME command is deleted. 289175897234Sdrh ** 28923570ad93Sdrh ** The second argument is the name of the database file. 2893fbc3eab8Sdrh ** 289475897234Sdrh */ 289522fbcb8dSdrh static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ 2896bec3f402Sdrh SqliteDb *p; 289722fbcb8dSdrh const char *zArg; 289875897234Sdrh char *zErrMsg; 28993570ad93Sdrh int i; 290022fbcb8dSdrh const char *zFile; 29013570ad93Sdrh const char *zVfs = 0; 2902d9da78a2Sdrh int flags; 2903882e8e4dSdrh Tcl_DString translatedFilename; 2904b07028f7Sdrh #ifdef SQLITE_HAS_CODEC 2905b07028f7Sdrh void *pKey = 0; 2906b07028f7Sdrh int nKey = 0; 2907b07028f7Sdrh #endif 2908540ebf82Smistachkin int rc; 2909d9da78a2Sdrh 2910d9da78a2Sdrh /* In normal use, each TCL interpreter runs in a single thread. So 2911d9da78a2Sdrh ** by default, we can turn of mutexing on SQLite database connections. 2912d9da78a2Sdrh ** However, for testing purposes it is useful to have mutexes turned 2913d9da78a2Sdrh ** on. So, by default, mutexes default off. But if compiled with 2914d9da78a2Sdrh ** SQLITE_TCL_DEFAULT_FULLMUTEX then mutexes default on. 2915d9da78a2Sdrh */ 2916d9da78a2Sdrh #ifdef SQLITE_TCL_DEFAULT_FULLMUTEX 2917d9da78a2Sdrh flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX; 2918d9da78a2Sdrh #else 2919d9da78a2Sdrh flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX; 2920d9da78a2Sdrh #endif 2921d9da78a2Sdrh 292222fbcb8dSdrh if( objc==2 ){ 292322fbcb8dSdrh zArg = Tcl_GetStringFromObj(objv[1], 0); 292422fbcb8dSdrh if( strcmp(zArg,"-version")==0 ){ 29256f8a503dSdanielk1977 Tcl_AppendResult(interp,sqlite3_version,0); 2926647cb0e1Sdrh return TCL_OK; 2927647cb0e1Sdrh } 29289eb9e26bSdrh if( strcmp(zArg,"-has-codec")==0 ){ 29299eb9e26bSdrh #ifdef SQLITE_HAS_CODEC 293022fbcb8dSdrh Tcl_AppendResult(interp,"1",0); 293122fbcb8dSdrh #else 293222fbcb8dSdrh Tcl_AppendResult(interp,"0",0); 293322fbcb8dSdrh #endif 293422fbcb8dSdrh return TCL_OK; 293522fbcb8dSdrh } 2936fbc3eab8Sdrh } 29373570ad93Sdrh for(i=3; i+1<objc; i+=2){ 29383570ad93Sdrh zArg = Tcl_GetString(objv[i]); 293922fbcb8dSdrh if( strcmp(zArg,"-key")==0 ){ 2940b07028f7Sdrh #ifdef SQLITE_HAS_CODEC 29413570ad93Sdrh pKey = Tcl_GetByteArrayFromObj(objv[i+1], &nKey); 2942b07028f7Sdrh #endif 29433570ad93Sdrh }else if( strcmp(zArg, "-vfs")==0 ){ 29443c3dd7b9Sdan zVfs = Tcl_GetString(objv[i+1]); 29453570ad93Sdrh }else if( strcmp(zArg, "-readonly")==0 ){ 29463570ad93Sdrh int b; 29473570ad93Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 29483570ad93Sdrh if( b ){ 294933f4e02aSdrh flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); 29503570ad93Sdrh flags |= SQLITE_OPEN_READONLY; 29513570ad93Sdrh }else{ 29523570ad93Sdrh flags &= ~SQLITE_OPEN_READONLY; 29533570ad93Sdrh flags |= SQLITE_OPEN_READWRITE; 29543570ad93Sdrh } 29553570ad93Sdrh }else if( strcmp(zArg, "-create")==0 ){ 29563570ad93Sdrh int b; 29573570ad93Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 295833f4e02aSdrh if( b && (flags & SQLITE_OPEN_READONLY)==0 ){ 29593570ad93Sdrh flags |= SQLITE_OPEN_CREATE; 29603570ad93Sdrh }else{ 29613570ad93Sdrh flags &= ~SQLITE_OPEN_CREATE; 29623570ad93Sdrh } 29639a6284c1Sdanielk1977 }else if( strcmp(zArg, "-nomutex")==0 ){ 29649a6284c1Sdanielk1977 int b; 29659a6284c1Sdanielk1977 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 29669a6284c1Sdanielk1977 if( b ){ 29679a6284c1Sdanielk1977 flags |= SQLITE_OPEN_NOMUTEX; 2968039963adSdrh flags &= ~SQLITE_OPEN_FULLMUTEX; 29699a6284c1Sdanielk1977 }else{ 29709a6284c1Sdanielk1977 flags &= ~SQLITE_OPEN_NOMUTEX; 29719a6284c1Sdanielk1977 } 2972039963adSdrh }else if( strcmp(zArg, "-fullmutex")==0 ){ 2973039963adSdrh int b; 2974039963adSdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 2975039963adSdrh if( b ){ 2976039963adSdrh flags |= SQLITE_OPEN_FULLMUTEX; 2977039963adSdrh flags &= ~SQLITE_OPEN_NOMUTEX; 2978039963adSdrh }else{ 2979039963adSdrh flags &= ~SQLITE_OPEN_FULLMUTEX; 2980039963adSdrh } 2981f12b3f60Sdrh }else if( strcmp(zArg, "-uri")==0 ){ 2982f12b3f60Sdrh int b; 2983f12b3f60Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 2984f12b3f60Sdrh if( b ){ 2985f12b3f60Sdrh flags |= SQLITE_OPEN_URI; 2986f12b3f60Sdrh }else{ 2987f12b3f60Sdrh flags &= ~SQLITE_OPEN_URI; 2988f12b3f60Sdrh } 29893570ad93Sdrh }else{ 29903570ad93Sdrh Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0); 29913570ad93Sdrh return TCL_ERROR; 299222fbcb8dSdrh } 299322fbcb8dSdrh } 29943570ad93Sdrh if( objc<3 || (objc&1)!=1 ){ 299522fbcb8dSdrh Tcl_WrongNumArgs(interp, 1, objv, 29963570ad93Sdrh "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" 299768bd4aa2Sdrh " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?" 29989eb9e26bSdrh #ifdef SQLITE_HAS_CODEC 29993570ad93Sdrh " ?-key CODECKEY?" 300022fbcb8dSdrh #endif 300122fbcb8dSdrh ); 300275897234Sdrh return TCL_ERROR; 300375897234Sdrh } 300475897234Sdrh zErrMsg = 0; 30054cdc9e84Sdrh p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); 300675897234Sdrh if( p==0 ){ 3007bec3f402Sdrh Tcl_SetResult(interp, "malloc failed", TCL_STATIC); 3008bec3f402Sdrh return TCL_ERROR; 3009bec3f402Sdrh } 3010bec3f402Sdrh memset(p, 0, sizeof(*p)); 301122fbcb8dSdrh zFile = Tcl_GetStringFromObj(objv[2], 0); 3012882e8e4dSdrh zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); 3013540ebf82Smistachkin rc = sqlite3_open_v2(zFile, &p->db, flags, zVfs); 3014882e8e4dSdrh Tcl_DStringFree(&translatedFilename); 3015540ebf82Smistachkin if( p->db ){ 301680290863Sdanielk1977 if( SQLITE_OK!=sqlite3_errcode(p->db) ){ 30179404d50eSdrh zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); 301880290863Sdanielk1977 sqlite3_close(p->db); 301980290863Sdanielk1977 p->db = 0; 302080290863Sdanielk1977 } 3021540ebf82Smistachkin }else{ 30225dac8432Smistachkin zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc)); 3023540ebf82Smistachkin } 30242011d5f5Sdrh #ifdef SQLITE_HAS_CODEC 3025f3a65f7eSdrh if( p->db ){ 30262011d5f5Sdrh sqlite3_key(p->db, pKey, nKey); 3027f3a65f7eSdrh } 3028eb8ed70dSdrh #endif 3029bec3f402Sdrh if( p->db==0 ){ 303075897234Sdrh Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 3031bec3f402Sdrh Tcl_Free((char*)p); 30329404d50eSdrh sqlite3_free(zErrMsg); 303375897234Sdrh return TCL_ERROR; 303475897234Sdrh } 3035fb7e7651Sdrh p->maxStmt = NUM_PREPARED_STMTS; 30365169bbc6Sdrh p->interp = interp; 303722fbcb8dSdrh zArg = Tcl_GetStringFromObj(objv[1], 0); 30384a4c11aaSdan if( DbUseNre() ){ 3039a2c8a95bSdrh Tcl_NRCreateCommand(interp, zArg, DbObjCmdAdaptor, DbObjCmd, 3040a2c8a95bSdrh (char*)p, DbDeleteCmd); 30414a4c11aaSdan }else{ 304222fbcb8dSdrh Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); 30434a4c11aaSdan } 304475897234Sdrh return TCL_OK; 304575897234Sdrh } 304675897234Sdrh 304775897234Sdrh /* 304890ca9753Sdrh ** Provide a dummy Tcl_InitStubs if we are using this as a static 304990ca9753Sdrh ** library. 305090ca9753Sdrh */ 305190ca9753Sdrh #ifndef USE_TCL_STUBS 305290ca9753Sdrh # undef Tcl_InitStubs 305390ca9753Sdrh # define Tcl_InitStubs(a,b,c) 305490ca9753Sdrh #endif 305590ca9753Sdrh 305690ca9753Sdrh /* 305729bc4615Sdrh ** Make sure we have a PACKAGE_VERSION macro defined. This will be 305829bc4615Sdrh ** defined automatically by the TEA makefile. But other makefiles 305929bc4615Sdrh ** do not define it. 306029bc4615Sdrh */ 306129bc4615Sdrh #ifndef PACKAGE_VERSION 306229bc4615Sdrh # define PACKAGE_VERSION SQLITE_VERSION 306329bc4615Sdrh #endif 306429bc4615Sdrh 306529bc4615Sdrh /* 306675897234Sdrh ** Initialize this module. 306775897234Sdrh ** 306875897234Sdrh ** This Tcl module contains only a single new Tcl command named "sqlite". 306975897234Sdrh ** (Hence there is no namespace. There is no point in using a namespace 307075897234Sdrh ** if the extension only supplies one new name!) The "sqlite" command is 307175897234Sdrh ** used to open a new SQLite database. See the DbMain() routine above 307275897234Sdrh ** for additional information. 3073b652f432Sdrh ** 3074b652f432Sdrh ** The EXTERN macros are required by TCL in order to work on windows. 307575897234Sdrh */ 3076b652f432Sdrh EXTERN int Sqlite3_Init(Tcl_Interp *interp){ 307792febd92Sdrh Tcl_InitStubs(interp, "8.4", 0); 3078ef4ac8f9Sdrh Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); 307929bc4615Sdrh Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION); 30801cca0d22Sdrh 30811cca0d22Sdrh #ifndef SQLITE_3_SUFFIX_ONLY 30821cca0d22Sdrh /* The "sqlite" alias is undocumented. It is here only to support 30831cca0d22Sdrh ** legacy scripts. All new scripts should use only the "sqlite3" 30841cca0d22Sdrh ** command. 30851cca0d22Sdrh */ 308649766d6cSdrh Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); 30874c0f1649Sdrh #endif 30881cca0d22Sdrh 308990ca9753Sdrh return TCL_OK; 309090ca9753Sdrh } 3091b652f432Sdrh EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3092b652f432Sdrh EXTERN int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3093b652f432Sdrh EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3094e2c3a659Sdrh 3095d878cab5Sdrh /* Because it accesses the file-system and uses persistent state, SQLite 3096d878cab5Sdrh ** is not considered appropriate for safe interpreters. Hence, we deliberately 3097d878cab5Sdrh ** omit the _SafeInit() interfaces. 3098d878cab5Sdrh */ 309949766d6cSdrh 310049766d6cSdrh #ifndef SQLITE_3_SUFFIX_ONLY 3101a3e63c4aSdan int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3102a3e63c4aSdan int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 3103a3e63c4aSdan int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3104a3e63c4aSdan int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 310549766d6cSdrh #endif 310675897234Sdrh 31073e27c026Sdrh #ifdef TCLSH 31083e27c026Sdrh /***************************************************************************** 310957a0227fSdrh ** All of the code that follows is used to build standalone TCL interpreters 311057a0227fSdrh ** that are statically linked with SQLite. Enable these by compiling 311157a0227fSdrh ** with -DTCLSH=n where n can be 1 or 2. An n of 1 generates a standard 311257a0227fSdrh ** tclsh but with SQLite built in. An n of 2 generates the SQLite space 311357a0227fSdrh ** analysis program. 311475897234Sdrh */ 3115348784efSdrh 311657a0227fSdrh #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) 311757a0227fSdrh /* 311857a0227fSdrh * This code implements the MD5 message-digest algorithm. 311957a0227fSdrh * The algorithm is due to Ron Rivest. This code was 312057a0227fSdrh * written by Colin Plumb in 1993, no copyright is claimed. 312157a0227fSdrh * This code is in the public domain; do with it what you wish. 312257a0227fSdrh * 312357a0227fSdrh * Equivalent code is available from RSA Data Security, Inc. 312457a0227fSdrh * This code has been tested against that, and is equivalent, 312557a0227fSdrh * except that you don't need to include two pages of legalese 312657a0227fSdrh * with every copy. 312757a0227fSdrh * 312857a0227fSdrh * To compute the message digest of a chunk of bytes, declare an 312957a0227fSdrh * MD5Context structure, pass it to MD5Init, call MD5Update as 313057a0227fSdrh * needed on buffers full of bytes, and then call MD5Final, which 313157a0227fSdrh * will fill a supplied 16-byte array with the digest. 313257a0227fSdrh */ 313357a0227fSdrh 313457a0227fSdrh /* 313557a0227fSdrh * If compiled on a machine that doesn't have a 32-bit integer, 313657a0227fSdrh * you just set "uint32" to the appropriate datatype for an 313757a0227fSdrh * unsigned 32-bit integer. For example: 313857a0227fSdrh * 313957a0227fSdrh * cc -Duint32='unsigned long' md5.c 314057a0227fSdrh * 314157a0227fSdrh */ 314257a0227fSdrh #ifndef uint32 314357a0227fSdrh # define uint32 unsigned int 314457a0227fSdrh #endif 314557a0227fSdrh 314657a0227fSdrh struct MD5Context { 314757a0227fSdrh int isInit; 314857a0227fSdrh uint32 buf[4]; 314957a0227fSdrh uint32 bits[2]; 315057a0227fSdrh unsigned char in[64]; 315157a0227fSdrh }; 315257a0227fSdrh typedef struct MD5Context MD5Context; 315357a0227fSdrh 315457a0227fSdrh /* 315557a0227fSdrh * Note: this code is harmless on little-endian machines. 315657a0227fSdrh */ 315757a0227fSdrh static void byteReverse (unsigned char *buf, unsigned longs){ 315857a0227fSdrh uint32 t; 315957a0227fSdrh do { 316057a0227fSdrh t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 | 316157a0227fSdrh ((unsigned)buf[1]<<8 | buf[0]); 316257a0227fSdrh *(uint32 *)buf = t; 316357a0227fSdrh buf += 4; 316457a0227fSdrh } while (--longs); 316557a0227fSdrh } 316657a0227fSdrh /* The four core functions - F1 is optimized somewhat */ 316757a0227fSdrh 316857a0227fSdrh /* #define F1(x, y, z) (x & y | ~x & z) */ 316957a0227fSdrh #define F1(x, y, z) (z ^ (x & (y ^ z))) 317057a0227fSdrh #define F2(x, y, z) F1(z, x, y) 317157a0227fSdrh #define F3(x, y, z) (x ^ y ^ z) 317257a0227fSdrh #define F4(x, y, z) (y ^ (x | ~z)) 317357a0227fSdrh 317457a0227fSdrh /* This is the central step in the MD5 algorithm. */ 317557a0227fSdrh #define MD5STEP(f, w, x, y, z, data, s) \ 317657a0227fSdrh ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) 317757a0227fSdrh 317857a0227fSdrh /* 317957a0227fSdrh * The core of the MD5 algorithm, this alters an existing MD5 hash to 318057a0227fSdrh * reflect the addition of 16 longwords of new data. MD5Update blocks 318157a0227fSdrh * the data and converts bytes into longwords for this routine. 318257a0227fSdrh */ 318357a0227fSdrh static void MD5Transform(uint32 buf[4], const uint32 in[16]){ 318457a0227fSdrh register uint32 a, b, c, d; 318557a0227fSdrh 318657a0227fSdrh a = buf[0]; 318757a0227fSdrh b = buf[1]; 318857a0227fSdrh c = buf[2]; 318957a0227fSdrh d = buf[3]; 319057a0227fSdrh 319157a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); 319257a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); 319357a0227fSdrh MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); 319457a0227fSdrh MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); 319557a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); 319657a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); 319757a0227fSdrh MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); 319857a0227fSdrh MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); 319957a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); 320057a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); 320157a0227fSdrh MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); 320257a0227fSdrh MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); 320357a0227fSdrh MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); 320457a0227fSdrh MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); 320557a0227fSdrh MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); 320657a0227fSdrh MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); 320757a0227fSdrh 320857a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); 320957a0227fSdrh MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); 321057a0227fSdrh MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); 321157a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); 321257a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); 321357a0227fSdrh MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); 321457a0227fSdrh MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); 321557a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); 321657a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); 321757a0227fSdrh MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); 321857a0227fSdrh MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); 321957a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); 322057a0227fSdrh MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); 322157a0227fSdrh MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); 322257a0227fSdrh MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); 322357a0227fSdrh MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); 322457a0227fSdrh 322557a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); 322657a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); 322757a0227fSdrh MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); 322857a0227fSdrh MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); 322957a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); 323057a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); 323157a0227fSdrh MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); 323257a0227fSdrh MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); 323357a0227fSdrh MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); 323457a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); 323557a0227fSdrh MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); 323657a0227fSdrh MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); 323757a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); 323857a0227fSdrh MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); 323957a0227fSdrh MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); 324057a0227fSdrh MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); 324157a0227fSdrh 324257a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); 324357a0227fSdrh MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); 324457a0227fSdrh MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); 324557a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); 324657a0227fSdrh MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); 324757a0227fSdrh MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); 324857a0227fSdrh MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); 324957a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); 325057a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); 325157a0227fSdrh MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); 325257a0227fSdrh MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); 325357a0227fSdrh MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); 325457a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); 325557a0227fSdrh MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); 325657a0227fSdrh MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); 325757a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); 325857a0227fSdrh 325957a0227fSdrh buf[0] += a; 326057a0227fSdrh buf[1] += b; 326157a0227fSdrh buf[2] += c; 326257a0227fSdrh buf[3] += d; 326357a0227fSdrh } 326457a0227fSdrh 326557a0227fSdrh /* 326657a0227fSdrh * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 326757a0227fSdrh * initialization constants. 326857a0227fSdrh */ 326957a0227fSdrh static void MD5Init(MD5Context *ctx){ 327057a0227fSdrh ctx->isInit = 1; 327157a0227fSdrh ctx->buf[0] = 0x67452301; 327257a0227fSdrh ctx->buf[1] = 0xefcdab89; 327357a0227fSdrh ctx->buf[2] = 0x98badcfe; 327457a0227fSdrh ctx->buf[3] = 0x10325476; 327557a0227fSdrh ctx->bits[0] = 0; 327657a0227fSdrh ctx->bits[1] = 0; 327757a0227fSdrh } 327857a0227fSdrh 327957a0227fSdrh /* 328057a0227fSdrh * Update context to reflect the concatenation of another buffer full 328157a0227fSdrh * of bytes. 328257a0227fSdrh */ 328357a0227fSdrh static 328457a0227fSdrh void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){ 328557a0227fSdrh uint32 t; 328657a0227fSdrh 328757a0227fSdrh /* Update bitcount */ 328857a0227fSdrh 328957a0227fSdrh t = ctx->bits[0]; 329057a0227fSdrh if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) 329157a0227fSdrh ctx->bits[1]++; /* Carry from low to high */ 329257a0227fSdrh ctx->bits[1] += len >> 29; 329357a0227fSdrh 329457a0227fSdrh t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ 329557a0227fSdrh 329657a0227fSdrh /* Handle any leading odd-sized chunks */ 329757a0227fSdrh 329857a0227fSdrh if ( t ) { 329957a0227fSdrh unsigned char *p = (unsigned char *)ctx->in + t; 330057a0227fSdrh 330157a0227fSdrh t = 64-t; 330257a0227fSdrh if (len < t) { 330357a0227fSdrh memcpy(p, buf, len); 330457a0227fSdrh return; 330557a0227fSdrh } 330657a0227fSdrh memcpy(p, buf, t); 330757a0227fSdrh byteReverse(ctx->in, 16); 330857a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 330957a0227fSdrh buf += t; 331057a0227fSdrh len -= t; 331157a0227fSdrh } 331257a0227fSdrh 331357a0227fSdrh /* Process data in 64-byte chunks */ 331457a0227fSdrh 331557a0227fSdrh while (len >= 64) { 331657a0227fSdrh memcpy(ctx->in, buf, 64); 331757a0227fSdrh byteReverse(ctx->in, 16); 331857a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 331957a0227fSdrh buf += 64; 332057a0227fSdrh len -= 64; 332157a0227fSdrh } 332257a0227fSdrh 332357a0227fSdrh /* Handle any remaining bytes of data. */ 332457a0227fSdrh 332557a0227fSdrh memcpy(ctx->in, buf, len); 332657a0227fSdrh } 332757a0227fSdrh 332857a0227fSdrh /* 332957a0227fSdrh * Final wrapup - pad to 64-byte boundary with the bit pattern 333057a0227fSdrh * 1 0* (64-bit count of bits processed, MSB-first) 333157a0227fSdrh */ 333257a0227fSdrh static void MD5Final(unsigned char digest[16], MD5Context *ctx){ 333357a0227fSdrh unsigned count; 333457a0227fSdrh unsigned char *p; 333557a0227fSdrh 333657a0227fSdrh /* Compute number of bytes mod 64 */ 333757a0227fSdrh count = (ctx->bits[0] >> 3) & 0x3F; 333857a0227fSdrh 333957a0227fSdrh /* Set the first char of padding to 0x80. This is safe since there is 334057a0227fSdrh always at least one byte free */ 334157a0227fSdrh p = ctx->in + count; 334257a0227fSdrh *p++ = 0x80; 334357a0227fSdrh 334457a0227fSdrh /* Bytes of padding needed to make 64 bytes */ 334557a0227fSdrh count = 64 - 1 - count; 334657a0227fSdrh 334757a0227fSdrh /* Pad out to 56 mod 64 */ 334857a0227fSdrh if (count < 8) { 334957a0227fSdrh /* Two lots of padding: Pad the first block to 64 bytes */ 335057a0227fSdrh memset(p, 0, count); 335157a0227fSdrh byteReverse(ctx->in, 16); 335257a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 335357a0227fSdrh 335457a0227fSdrh /* Now fill the next block with 56 bytes */ 335557a0227fSdrh memset(ctx->in, 0, 56); 335657a0227fSdrh } else { 335757a0227fSdrh /* Pad block to 56 bytes */ 335857a0227fSdrh memset(p, 0, count-8); 335957a0227fSdrh } 336057a0227fSdrh byteReverse(ctx->in, 14); 336157a0227fSdrh 336257a0227fSdrh /* Append length in bits and transform */ 336357a0227fSdrh ((uint32 *)ctx->in)[ 14 ] = ctx->bits[0]; 336457a0227fSdrh ((uint32 *)ctx->in)[ 15 ] = ctx->bits[1]; 336557a0227fSdrh 336657a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 336757a0227fSdrh byteReverse((unsigned char *)ctx->buf, 4); 336857a0227fSdrh memcpy(digest, ctx->buf, 16); 336957a0227fSdrh memset(ctx, 0, sizeof(ctx)); /* In case it is sensitive */ 337057a0227fSdrh } 337157a0227fSdrh 337257a0227fSdrh /* 337357a0227fSdrh ** Convert a 128-bit MD5 digest into a 32-digit base-16 number. 337457a0227fSdrh */ 337557a0227fSdrh static void MD5DigestToBase16(unsigned char *digest, char *zBuf){ 337657a0227fSdrh static char const zEncode[] = "0123456789abcdef"; 337757a0227fSdrh int i, j; 337857a0227fSdrh 337957a0227fSdrh for(j=i=0; i<16; i++){ 338057a0227fSdrh int a = digest[i]; 338157a0227fSdrh zBuf[j++] = zEncode[(a>>4)&0xf]; 338257a0227fSdrh zBuf[j++] = zEncode[a & 0xf]; 338357a0227fSdrh } 338457a0227fSdrh zBuf[j] = 0; 338557a0227fSdrh } 338657a0227fSdrh 338757a0227fSdrh 338857a0227fSdrh /* 338957a0227fSdrh ** Convert a 128-bit MD5 digest into sequency of eight 5-digit integers 339057a0227fSdrh ** each representing 16 bits of the digest and separated from each 339157a0227fSdrh ** other by a "-" character. 339257a0227fSdrh */ 339357a0227fSdrh static void MD5DigestToBase10x8(unsigned char digest[16], char zDigest[50]){ 339457a0227fSdrh int i, j; 339557a0227fSdrh unsigned int x; 339657a0227fSdrh for(i=j=0; i<16; i+=2){ 339757a0227fSdrh x = digest[i]*256 + digest[i+1]; 339857a0227fSdrh if( i>0 ) zDigest[j++] = '-'; 339957a0227fSdrh sprintf(&zDigest[j], "%05u", x); 340057a0227fSdrh j += 5; 340157a0227fSdrh } 340257a0227fSdrh zDigest[j] = 0; 340357a0227fSdrh } 340457a0227fSdrh 340557a0227fSdrh /* 340657a0227fSdrh ** A TCL command for md5. The argument is the text to be hashed. The 340757a0227fSdrh ** Result is the hash in base64. 340857a0227fSdrh */ 340957a0227fSdrh static int md5_cmd(void*cd, Tcl_Interp *interp, int argc, const char **argv){ 341057a0227fSdrh MD5Context ctx; 341157a0227fSdrh unsigned char digest[16]; 341257a0227fSdrh char zBuf[50]; 341357a0227fSdrh void (*converter)(unsigned char*, char*); 341457a0227fSdrh 341557a0227fSdrh if( argc!=2 ){ 341657a0227fSdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 341757a0227fSdrh " TEXT\"", 0); 341857a0227fSdrh return TCL_ERROR; 341957a0227fSdrh } 342057a0227fSdrh MD5Init(&ctx); 342157a0227fSdrh MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1])); 342257a0227fSdrh MD5Final(digest, &ctx); 342357a0227fSdrh converter = (void(*)(unsigned char*,char*))cd; 342457a0227fSdrh converter(digest, zBuf); 342557a0227fSdrh Tcl_AppendResult(interp, zBuf, (char*)0); 342657a0227fSdrh return TCL_OK; 342757a0227fSdrh } 342857a0227fSdrh 342957a0227fSdrh /* 343057a0227fSdrh ** A TCL command to take the md5 hash of a file. The argument is the 343157a0227fSdrh ** name of the file. 343257a0227fSdrh */ 343357a0227fSdrh static int md5file_cmd(void*cd, Tcl_Interp*interp, int argc, const char **argv){ 343457a0227fSdrh FILE *in; 343557a0227fSdrh MD5Context ctx; 343657a0227fSdrh void (*converter)(unsigned char*, char*); 343757a0227fSdrh unsigned char digest[16]; 343857a0227fSdrh char zBuf[10240]; 343957a0227fSdrh 344057a0227fSdrh if( argc!=2 ){ 344157a0227fSdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 344257a0227fSdrh " FILENAME\"", 0); 344357a0227fSdrh return TCL_ERROR; 344457a0227fSdrh } 344557a0227fSdrh in = fopen(argv[1],"rb"); 344657a0227fSdrh if( in==0 ){ 344757a0227fSdrh Tcl_AppendResult(interp,"unable to open file \"", argv[1], 344857a0227fSdrh "\" for reading", 0); 344957a0227fSdrh return TCL_ERROR; 345057a0227fSdrh } 345157a0227fSdrh MD5Init(&ctx); 345257a0227fSdrh for(;;){ 345357a0227fSdrh int n; 345483cc1392Sdrh n = (int)fread(zBuf, 1, sizeof(zBuf), in); 345557a0227fSdrh if( n<=0 ) break; 345657a0227fSdrh MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n); 345757a0227fSdrh } 345857a0227fSdrh fclose(in); 345957a0227fSdrh MD5Final(digest, &ctx); 346057a0227fSdrh converter = (void(*)(unsigned char*,char*))cd; 346157a0227fSdrh converter(digest, zBuf); 346257a0227fSdrh Tcl_AppendResult(interp, zBuf, (char*)0); 346357a0227fSdrh return TCL_OK; 346457a0227fSdrh } 346557a0227fSdrh 346657a0227fSdrh /* 346757a0227fSdrh ** Register the four new TCL commands for generating MD5 checksums 346857a0227fSdrh ** with the TCL interpreter. 346957a0227fSdrh */ 347057a0227fSdrh int Md5_Init(Tcl_Interp *interp){ 347157a0227fSdrh Tcl_CreateCommand(interp, "md5", (Tcl_CmdProc*)md5_cmd, 347257a0227fSdrh MD5DigestToBase16, 0); 347357a0227fSdrh Tcl_CreateCommand(interp, "md5-10x8", (Tcl_CmdProc*)md5_cmd, 347457a0227fSdrh MD5DigestToBase10x8, 0); 347557a0227fSdrh Tcl_CreateCommand(interp, "md5file", (Tcl_CmdProc*)md5file_cmd, 347657a0227fSdrh MD5DigestToBase16, 0); 347757a0227fSdrh Tcl_CreateCommand(interp, "md5file-10x8", (Tcl_CmdProc*)md5file_cmd, 347857a0227fSdrh MD5DigestToBase10x8, 0); 347957a0227fSdrh return TCL_OK; 348057a0227fSdrh } 348157a0227fSdrh #endif /* defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) */ 348257a0227fSdrh 348357a0227fSdrh #if defined(SQLITE_TEST) 348457a0227fSdrh /* 348557a0227fSdrh ** During testing, the special md5sum() aggregate function is available. 348657a0227fSdrh ** inside SQLite. The following routines implement that function. 348757a0227fSdrh */ 348857a0227fSdrh static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){ 348957a0227fSdrh MD5Context *p; 349057a0227fSdrh int i; 349157a0227fSdrh if( argc<1 ) return; 349257a0227fSdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 349357a0227fSdrh if( p==0 ) return; 349457a0227fSdrh if( !p->isInit ){ 349557a0227fSdrh MD5Init(p); 349657a0227fSdrh } 349757a0227fSdrh for(i=0; i<argc; i++){ 349857a0227fSdrh const char *zData = (char*)sqlite3_value_text(argv[i]); 349957a0227fSdrh if( zData ){ 350083cc1392Sdrh MD5Update(p, (unsigned char*)zData, (int)strlen(zData)); 350157a0227fSdrh } 350257a0227fSdrh } 350357a0227fSdrh } 350457a0227fSdrh static void md5finalize(sqlite3_context *context){ 350557a0227fSdrh MD5Context *p; 350657a0227fSdrh unsigned char digest[16]; 350757a0227fSdrh char zBuf[33]; 350857a0227fSdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 350957a0227fSdrh MD5Final(digest,p); 351057a0227fSdrh MD5DigestToBase16(digest, zBuf); 351157a0227fSdrh sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 351257a0227fSdrh } 351357a0227fSdrh int Md5_Register(sqlite3 *db){ 351457a0227fSdrh int rc = sqlite3_create_function(db, "md5sum", -1, SQLITE_UTF8, 0, 0, 351557a0227fSdrh md5step, md5finalize); 351657a0227fSdrh sqlite3_overload_function(db, "md5sum", -1); /* To exercise this API */ 351757a0227fSdrh return rc; 351857a0227fSdrh } 351957a0227fSdrh #endif /* defined(SQLITE_TEST) */ 352057a0227fSdrh 352157a0227fSdrh 3522348784efSdrh /* 35233e27c026Sdrh ** If the macro TCLSH is one, then put in code this for the 35243e27c026Sdrh ** "main" routine that will initialize Tcl and take input from 35253570ad93Sdrh ** standard input, or if a file is named on the command line 35263570ad93Sdrh ** the TCL interpreter reads and evaluates that file. 3527348784efSdrh */ 35283e27c026Sdrh #if TCLSH==1 35290ae479dfSdan static const char *tclsh_main_loop(void){ 35300ae479dfSdan static const char zMainloop[] = 3531348784efSdrh "set line {}\n" 3532348784efSdrh "while {![eof stdin]} {\n" 3533348784efSdrh "if {$line!=\"\"} {\n" 3534348784efSdrh "puts -nonewline \"> \"\n" 3535348784efSdrh "} else {\n" 3536348784efSdrh "puts -nonewline \"% \"\n" 3537348784efSdrh "}\n" 3538348784efSdrh "flush stdout\n" 3539348784efSdrh "append line [gets stdin]\n" 3540348784efSdrh "if {[info complete $line]} {\n" 3541348784efSdrh "if {[catch {uplevel #0 $line} result]} {\n" 3542348784efSdrh "puts stderr \"Error: $result\"\n" 3543348784efSdrh "} elseif {$result!=\"\"} {\n" 3544348784efSdrh "puts $result\n" 3545348784efSdrh "}\n" 3546348784efSdrh "set line {}\n" 3547348784efSdrh "} else {\n" 3548348784efSdrh "append line \\n\n" 3549348784efSdrh "}\n" 3550348784efSdrh "}\n" 3551348784efSdrh ; 35520ae479dfSdan return zMainloop; 35530ae479dfSdan } 35543e27c026Sdrh #endif 35553a0f13ffSdrh #if TCLSH==2 35560ae479dfSdan static const char *tclsh_main_loop(void); 35573a0f13ffSdrh #endif 35583e27c026Sdrh 3559c1a60c51Sdan #ifdef SQLITE_TEST 3560c1a60c51Sdan static void init_all(Tcl_Interp *); 3561c1a60c51Sdan static int init_all_cmd( 3562c1a60c51Sdan ClientData cd, 3563c1a60c51Sdan Tcl_Interp *interp, 3564c1a60c51Sdan int objc, 3565c1a60c51Sdan Tcl_Obj *CONST objv[] 3566c1a60c51Sdan ){ 35670a549071Sdanielk1977 3568c1a60c51Sdan Tcl_Interp *slave; 3569c1a60c51Sdan if( objc!=2 ){ 3570c1a60c51Sdan Tcl_WrongNumArgs(interp, 1, objv, "SLAVE"); 3571c1a60c51Sdan return TCL_ERROR; 3572c1a60c51Sdan } 35730a549071Sdanielk1977 3574c1a60c51Sdan slave = Tcl_GetSlave(interp, Tcl_GetString(objv[1])); 3575c1a60c51Sdan if( !slave ){ 3576c1a60c51Sdan return TCL_ERROR; 3577c1a60c51Sdan } 3578c1a60c51Sdan 3579c1a60c51Sdan init_all(slave); 3580c1a60c51Sdan return TCL_OK; 3581c1a60c51Sdan } 3582c431fd55Sdan 3583c431fd55Sdan /* 3584c431fd55Sdan ** Tclcmd: db_use_legacy_prepare DB BOOLEAN 3585c431fd55Sdan ** 3586c431fd55Sdan ** The first argument to this command must be a database command created by 3587c431fd55Sdan ** [sqlite3]. If the second argument is true, then the handle is configured 3588c431fd55Sdan ** to use the sqlite3_prepare_v2() function to prepare statements. If it 3589c431fd55Sdan ** is false, sqlite3_prepare(). 3590c431fd55Sdan */ 3591c431fd55Sdan static int db_use_legacy_prepare_cmd( 3592c431fd55Sdan ClientData cd, 3593c431fd55Sdan Tcl_Interp *interp, 3594c431fd55Sdan int objc, 3595c431fd55Sdan Tcl_Obj *CONST objv[] 3596c431fd55Sdan ){ 3597c431fd55Sdan Tcl_CmdInfo cmdInfo; 3598c431fd55Sdan SqliteDb *pDb; 3599c431fd55Sdan int bPrepare; 3600c431fd55Sdan 3601c431fd55Sdan if( objc!=3 ){ 3602c431fd55Sdan Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN"); 3603c431fd55Sdan return TCL_ERROR; 3604c431fd55Sdan } 3605c431fd55Sdan 3606c431fd55Sdan if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ 3607c431fd55Sdan Tcl_AppendResult(interp, "no such db: ", Tcl_GetString(objv[1]), (char*)0); 3608c431fd55Sdan return TCL_ERROR; 3609c431fd55Sdan } 3610c431fd55Sdan pDb = (SqliteDb*)cmdInfo.objClientData; 3611c431fd55Sdan if( Tcl_GetBooleanFromObj(interp, objv[2], &bPrepare) ){ 3612c431fd55Sdan return TCL_ERROR; 3613c431fd55Sdan } 3614c431fd55Sdan 3615c431fd55Sdan pDb->bLegacyPrepare = bPrepare; 3616c431fd55Sdan 3617c431fd55Sdan Tcl_ResetResult(interp); 3618c431fd55Sdan return TCL_OK; 3619c431fd55Sdan } 3620c1a60c51Sdan #endif 3621c1a60c51Sdan 3622c1a60c51Sdan /* 3623c1a60c51Sdan ** Configure the interpreter passed as the first argument to have access 3624c1a60c51Sdan ** to the commands and linked variables that make up: 3625c1a60c51Sdan ** 3626c1a60c51Sdan ** * the [sqlite3] extension itself, 3627c1a60c51Sdan ** 3628c1a60c51Sdan ** * If SQLITE_TCLMD5 or SQLITE_TEST is defined, the Md5 commands, and 3629c1a60c51Sdan ** 3630c1a60c51Sdan ** * If SQLITE_TEST is set, the various test interfaces used by the Tcl 3631c1a60c51Sdan ** test suite. 3632c1a60c51Sdan */ 3633c1a60c51Sdan static void init_all(Tcl_Interp *interp){ 363438f8271fSdrh Sqlite3_Init(interp); 3635c1a60c51Sdan 363657a0227fSdrh #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) 363757a0227fSdrh Md5_Init(interp); 363857a0227fSdrh #endif 3639c1a60c51Sdan 36400ae479dfSdan /* Install the [register_dbstat_vtab] command to access the implementation 36410ae479dfSdan ** of virtual table dbstat (source file test_stat.c). This command is 36420ae479dfSdan ** required for testfixture and sqlite3_analyzer, but not by the production 36430ae479dfSdan ** Tcl extension. */ 36440ae479dfSdan #if defined(SQLITE_TEST) || TCLSH==2 36450ae479dfSdan { 36460ae479dfSdan extern int SqlitetestStat_Init(Tcl_Interp*); 36470ae479dfSdan SqlitetestStat_Init(interp); 36480ae479dfSdan } 36490ae479dfSdan #endif 36500ae479dfSdan 3651d9b0257aSdrh #ifdef SQLITE_TEST 3652d1bf3512Sdrh { 36532f999a67Sdrh extern int Sqliteconfig_Init(Tcl_Interp*); 3654d1bf3512Sdrh extern int Sqlitetest1_Init(Tcl_Interp*); 36555c4d9703Sdrh extern int Sqlitetest2_Init(Tcl_Interp*); 36565c4d9703Sdrh extern int Sqlitetest3_Init(Tcl_Interp*); 3657a6064dcfSdrh extern int Sqlitetest4_Init(Tcl_Interp*); 3658998b56c3Sdanielk1977 extern int Sqlitetest5_Init(Tcl_Interp*); 36599c06c953Sdrh extern int Sqlitetest6_Init(Tcl_Interp*); 366029c636bcSdrh extern int Sqlitetest7_Init(Tcl_Interp*); 3661b9bb7c18Sdrh extern int Sqlitetest8_Init(Tcl_Interp*); 3662a713f2c3Sdanielk1977 extern int Sqlitetest9_Init(Tcl_Interp*); 36632366940dSdrh extern int Sqlitetestasync_Init(Tcl_Interp*); 36641409be69Sdrh extern int Sqlitetest_autoext_Init(Tcl_Interp*); 36650a7a9155Sdan extern int Sqlitetest_demovfs_Init(Tcl_Interp *); 3666984bfaa4Sdrh extern int Sqlitetest_func_Init(Tcl_Interp*); 366715926590Sdrh extern int Sqlitetest_hexio_Init(Tcl_Interp*); 3668e1ab2193Sdan extern int Sqlitetest_init_Init(Tcl_Interp*); 36692f999a67Sdrh extern int Sqlitetest_malloc_Init(Tcl_Interp*); 36701a9ed0b2Sdanielk1977 extern int Sqlitetest_mutex_Init(Tcl_Interp*); 36712f999a67Sdrh extern int Sqlitetestschema_Init(Tcl_Interp*); 36722f999a67Sdrh extern int Sqlitetestsse_Init(Tcl_Interp*); 36732f999a67Sdrh extern int Sqlitetesttclvar_Init(Tcl_Interp*); 3674*9f5ff371Sdan extern int Sqlitetestfs_Init(Tcl_Interp*); 367544918fa0Sdanielk1977 extern int SqlitetestThread_Init(Tcl_Interp*); 3676a15db353Sdanielk1977 extern int SqlitetestOnefile_Init(); 36775d1f5aa6Sdanielk1977 extern int SqlitetestOsinst_Init(Tcl_Interp*); 36780410302eSdanielk1977 extern int Sqlitetestbackup_Init(Tcl_Interp*); 3679522efc62Sdrh extern int Sqlitetestintarray_Init(Tcl_Interp*); 3680c7991bdfSdan extern int Sqlitetestvfs_Init(Tcl_Interp *); 36819508daa9Sdan extern int Sqlitetestrtree_Init(Tcl_Interp*); 36828cf35eb4Sdan extern int Sqlitequota_Init(Tcl_Interp*); 36838a922f75Sshaneh extern int Sqlitemultiplex_Init(Tcl_Interp*); 3684e336b001Sdan extern int SqliteSuperlock_Init(Tcl_Interp*); 3685213ca0a8Sdan extern int SqlitetestSyscall_Init(Tcl_Interp*); 3686326a67d0Sdrh extern int Sqlitetestfuzzer_Init(Tcl_Interp*); 368770586bebSdrh extern int Sqlitetestwholenumber_Init(Tcl_Interp*); 368814172743Sdrh extern int Sqlitetestregexp_Init(Tcl_Interp*); 36892e66f0b9Sdrh 36906764a700Sdan #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) 369199ebad90Sdan extern int Sqlitetestfts3_Init(Tcl_Interp *interp); 369299ebad90Sdan #endif 369399ebad90Sdan 3694b29010cdSdan #ifdef SQLITE_ENABLE_ZIPVFS 3695b29010cdSdan extern int Zipvfs_Init(Tcl_Interp*); 3696b29010cdSdan Zipvfs_Init(interp); 3697b29010cdSdan #endif 3698b29010cdSdan 36992f999a67Sdrh Sqliteconfig_Init(interp); 37006490bebdSdanielk1977 Sqlitetest1_Init(interp); 37015c4d9703Sdrh Sqlitetest2_Init(interp); 3702de647130Sdrh Sqlitetest3_Init(interp); 3703fc57d7bfSdanielk1977 Sqlitetest4_Init(interp); 3704998b56c3Sdanielk1977 Sqlitetest5_Init(interp); 37059c06c953Sdrh Sqlitetest6_Init(interp); 370629c636bcSdrh Sqlitetest7_Init(interp); 3707b9bb7c18Sdrh Sqlitetest8_Init(interp); 3708a713f2c3Sdanielk1977 Sqlitetest9_Init(interp); 37092366940dSdrh Sqlitetestasync_Init(interp); 37101409be69Sdrh Sqlitetest_autoext_Init(interp); 37110a7a9155Sdan Sqlitetest_demovfs_Init(interp); 3712984bfaa4Sdrh Sqlitetest_func_Init(interp); 371315926590Sdrh Sqlitetest_hexio_Init(interp); 3714e1ab2193Sdan Sqlitetest_init_Init(interp); 37152f999a67Sdrh Sqlitetest_malloc_Init(interp); 37161a9ed0b2Sdanielk1977 Sqlitetest_mutex_Init(interp); 37172f999a67Sdrh Sqlitetestschema_Init(interp); 37182f999a67Sdrh Sqlitetesttclvar_Init(interp); 3719*9f5ff371Sdan Sqlitetestfs_Init(interp); 372044918fa0Sdanielk1977 SqlitetestThread_Init(interp); 3721a15db353Sdanielk1977 SqlitetestOnefile_Init(interp); 37225d1f5aa6Sdanielk1977 SqlitetestOsinst_Init(interp); 37230410302eSdanielk1977 Sqlitetestbackup_Init(interp); 3724522efc62Sdrh Sqlitetestintarray_Init(interp); 3725c7991bdfSdan Sqlitetestvfs_Init(interp); 37269508daa9Sdan Sqlitetestrtree_Init(interp); 37278cf35eb4Sdan Sqlitequota_Init(interp); 37288a922f75Sshaneh Sqlitemultiplex_Init(interp); 3729e336b001Sdan SqliteSuperlock_Init(interp); 3730213ca0a8Sdan SqlitetestSyscall_Init(interp); 3731326a67d0Sdrh Sqlitetestfuzzer_Init(interp); 373270586bebSdrh Sqlitetestwholenumber_Init(interp); 373314172743Sdrh Sqlitetestregexp_Init(interp); 3734a15db353Sdanielk1977 37356764a700Sdan #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) 373699ebad90Sdan Sqlitetestfts3_Init(interp); 373799ebad90Sdan #endif 373899ebad90Sdan 3739c431fd55Sdan Tcl_CreateObjCommand( 3740c431fd55Sdan interp, "load_testfixture_extensions", init_all_cmd, 0, 0 3741c431fd55Sdan ); 3742c431fd55Sdan Tcl_CreateObjCommand( 3743c431fd55Sdan interp, "db_use_legacy_prepare", db_use_legacy_prepare_cmd, 0, 0 3744c431fd55Sdan ); 3745c1a60c51Sdan 374689dec819Sdrh #ifdef SQLITE_SSE 37472e66f0b9Sdrh Sqlitetestsse_Init(interp); 37482e66f0b9Sdrh #endif 3749d1bf3512Sdrh } 3750d1bf3512Sdrh #endif 3751c1a60c51Sdan } 3752c1a60c51Sdan 3753c1a60c51Sdan #define TCLSH_MAIN main /* Needed to fake out mktclapp */ 3754c1a60c51Sdan int TCLSH_MAIN(int argc, char **argv){ 3755c1a60c51Sdan Tcl_Interp *interp; 3756c1a60c51Sdan 3757c1a60c51Sdan /* Call sqlite3_shutdown() once before doing anything else. This is to 3758c1a60c51Sdan ** test that sqlite3_shutdown() can be safely called by a process before 3759c1a60c51Sdan ** sqlite3_initialize() is. */ 3760c1a60c51Sdan sqlite3_shutdown(); 3761c1a60c51Sdan 37620ae479dfSdan Tcl_FindExecutable(argv[0]); 37630ae479dfSdan interp = Tcl_CreateInterp(); 37640ae479dfSdan 37653a0f13ffSdrh #if TCLSH==2 37663a0f13ffSdrh sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); 37673a0f13ffSdrh #endif 3768c1a60c51Sdan 3769c1a60c51Sdan init_all(interp); 3770c7285978Sdrh if( argc>=2 ){ 3771348784efSdrh int i; 3772ad42c3a3Sshess char zArgc[32]; 3773ad42c3a3Sshess sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH)); 3774ad42c3a3Sshess Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY); 3775348784efSdrh Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); 3776348784efSdrh Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); 377761212b69Sdrh for(i=3-TCLSH; i<argc; i++){ 3778348784efSdrh Tcl_SetVar(interp, "argv", argv[i], 3779348784efSdrh TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); 3780348784efSdrh } 37813a0f13ffSdrh if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ 37820de8c112Sdrh const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); 3783a81c64a2Sdrh if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp); 3784c61053b7Sdrh fprintf(stderr,"%s: %s\n", *argv, zInfo); 3785348784efSdrh return 1; 3786348784efSdrh } 37873e27c026Sdrh } 37883a0f13ffSdrh if( TCLSH==2 || argc<=1 ){ 37890ae479dfSdan Tcl_GlobalEval(interp, tclsh_main_loop()); 3790348784efSdrh } 3791348784efSdrh return 0; 3792348784efSdrh } 3793348784efSdrh #endif /* TCLSH */ 3794