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 3606b2718aSdrh # include "sqliteInt.h" 3775897234Sdrh # include <stdlib.h> 3875897234Sdrh # include <string.h> 39ce927065Sdrh # include <assert.h> 40bd08af48Sdrh #endif 41eb206381Sdrh #include <ctype.h> 4275897234Sdrh 43ad6e1370Sdrh /* 44ad6e1370Sdrh * Windows needs to know which symbols to export. Unix does not. 45ad6e1370Sdrh * BUILD_sqlite should be undefined for Unix. 46ad6e1370Sdrh */ 47ad6e1370Sdrh #ifdef BUILD_sqlite 48ad6e1370Sdrh #undef TCL_STORAGE_CLASS 49ad6e1370Sdrh #define TCL_STORAGE_CLASS DLLEXPORT 50ad6e1370Sdrh #endif /* BUILD_sqlite */ 5129bc4615Sdrh 52a21c6b6fSdanielk1977 #define NUM_PREPARED_STMTS 10 53fb7e7651Sdrh #define MAX_PREPARED_STMTS 100 54fb7e7651Sdrh 5575897234Sdrh /* 5698808babSdrh ** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we 5798808babSdrh ** have to do a translation when going between the two. Set the 5898808babSdrh ** UTF_TRANSLATION_NEEDED macro to indicate that we need to do 5998808babSdrh ** this translation. 6098808babSdrh */ 6198808babSdrh #if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8) 6298808babSdrh # define UTF_TRANSLATION_NEEDED 1 6398808babSdrh #endif 6498808babSdrh 6598808babSdrh /* 66cabb0819Sdrh ** New SQL functions can be created as TCL scripts. Each such function 67cabb0819Sdrh ** is described by an instance of the following structure. 68cabb0819Sdrh */ 69cabb0819Sdrh typedef struct SqlFunc SqlFunc; 70cabb0819Sdrh struct SqlFunc { 71cabb0819Sdrh Tcl_Interp *interp; /* The TCL interpret to execute the function */ 72d1e4733dSdrh Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */ 73d1e4733dSdrh int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */ 74d1e4733dSdrh char *zName; /* Name of this function */ 75cabb0819Sdrh SqlFunc *pNext; /* Next function on the list of them all */ 76cabb0819Sdrh }; 77cabb0819Sdrh 78cabb0819Sdrh /* 790202b29eSdanielk1977 ** New collation sequences function can be created as TCL scripts. Each such 800202b29eSdanielk1977 ** function is described by an instance of the following structure. 810202b29eSdanielk1977 */ 820202b29eSdanielk1977 typedef struct SqlCollate SqlCollate; 830202b29eSdanielk1977 struct SqlCollate { 840202b29eSdanielk1977 Tcl_Interp *interp; /* The TCL interpret to execute the function */ 850202b29eSdanielk1977 char *zScript; /* The script to be run */ 860202b29eSdanielk1977 SqlCollate *pNext; /* Next function on the list of them all */ 870202b29eSdanielk1977 }; 880202b29eSdanielk1977 890202b29eSdanielk1977 /* 90fb7e7651Sdrh ** Prepared statements are cached for faster execution. Each prepared 91fb7e7651Sdrh ** statement is described by an instance of the following structure. 92fb7e7651Sdrh */ 93fb7e7651Sdrh typedef struct SqlPreparedStmt SqlPreparedStmt; 94fb7e7651Sdrh struct SqlPreparedStmt { 95fb7e7651Sdrh SqlPreparedStmt *pNext; /* Next in linked list */ 96fb7e7651Sdrh SqlPreparedStmt *pPrev; /* Previous on the list */ 97fb7e7651Sdrh sqlite3_stmt *pStmt; /* The prepared statement */ 98fb7e7651Sdrh int nSql; /* chars in zSql[] */ 99d0e2a854Sdanielk1977 const char *zSql; /* Text of the SQL statement */ 1004a4c11aaSdan int nParm; /* Size of apParm array */ 1014a4c11aaSdan Tcl_Obj **apParm; /* Array of referenced object pointers */ 102fb7e7651Sdrh }; 103fb7e7651Sdrh 104d0441796Sdanielk1977 typedef struct IncrblobChannel IncrblobChannel; 105d0441796Sdanielk1977 106fb7e7651Sdrh /* 107bec3f402Sdrh ** There is one instance of this structure for each SQLite database 108bec3f402Sdrh ** that has been opened by the SQLite TCL interface. 109bec3f402Sdrh */ 110bec3f402Sdrh typedef struct SqliteDb SqliteDb; 111bec3f402Sdrh struct SqliteDb { 112dddca286Sdrh sqlite3 *db; /* The "real" database structure. MUST BE FIRST */ 113bec3f402Sdrh Tcl_Interp *interp; /* The interpreter used for this database */ 1146d31316cSdrh char *zBusy; /* The busy callback routine */ 115aa940eacSdrh char *zCommit; /* The commit hook callback routine */ 116b5a20d3cSdrh char *zTrace; /* The trace callback routine */ 11719e2d37fSdrh char *zProfile; /* The profile callback routine */ 118348bb5d6Sdanielk1977 char *zProgress; /* The progress callback routine */ 119e22a334bSdrh char *zAuth; /* The authorization callback routine */ 1201f1549f8Sdrh int disableAuth; /* Disable the authorizer if it exists */ 12155c45f2eSdanielk1977 char *zNull; /* Text to substitute for an SQL NULL value */ 122cabb0819Sdrh SqlFunc *pFunc; /* List of SQL functions */ 12394eb6a14Sdanielk1977 Tcl_Obj *pUpdateHook; /* Update hook script (if any) */ 12471fd80bfSdanielk1977 Tcl_Obj *pRollbackHook; /* Rollback 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 */ 134d1d38488Sdrh int nStep, nSort; /* Statistics for most recent operation */ 135cd38d520Sdanielk1977 int nTransaction; /* Number of nested [transaction] methods */ 13698808babSdrh }; 137297ecf14Sdrh 138b4e9af9fSdanielk1977 struct IncrblobChannel { 139d0441796Sdanielk1977 sqlite3_blob *pBlob; /* sqlite3 blob handle */ 140dcbb5d3fSdanielk1977 SqliteDb *pDb; /* Associated database connection */ 141b4e9af9fSdanielk1977 int iSeek; /* Current seek offset */ 142d0441796Sdanielk1977 Tcl_Channel channel; /* Channel identifier */ 143d0441796Sdanielk1977 IncrblobChannel *pNext; /* Linked list of all open incrblob channels */ 144d0441796Sdanielk1977 IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */ 145b4e9af9fSdanielk1977 }; 146b4e9af9fSdanielk1977 147ea678832Sdrh /* 148ea678832Sdrh ** Compute a string length that is limited to what can be stored in 149ea678832Sdrh ** lower 30 bits of a 32-bit signed integer. 150ea678832Sdrh */ 1514f21c4afSdrh static int strlen30(const char *z){ 152ea678832Sdrh const char *z2 = z; 153ea678832Sdrh while( *z2 ){ z2++; } 154ea678832Sdrh return 0x3fffffff & (int)(z2 - z); 155ea678832Sdrh } 156ea678832Sdrh 157ea678832Sdrh 15832a0d8bbSdanielk1977 #ifndef SQLITE_OMIT_INCRBLOB 159b4e9af9fSdanielk1977 /* 160d0441796Sdanielk1977 ** Close all incrblob channels opened using database connection pDb. 161d0441796Sdanielk1977 ** This is called when shutting down the database connection. 162d0441796Sdanielk1977 */ 163d0441796Sdanielk1977 static void closeIncrblobChannels(SqliteDb *pDb){ 164d0441796Sdanielk1977 IncrblobChannel *p; 165d0441796Sdanielk1977 IncrblobChannel *pNext; 166d0441796Sdanielk1977 167d0441796Sdanielk1977 for(p=pDb->pIncrblob; p; p=pNext){ 168d0441796Sdanielk1977 pNext = p->pNext; 169d0441796Sdanielk1977 170d0441796Sdanielk1977 /* Note: Calling unregister here call Tcl_Close on the incrblob channel, 171d0441796Sdanielk1977 ** which deletes the IncrblobChannel structure at *p. So do not 172d0441796Sdanielk1977 ** call Tcl_Free() here. 173d0441796Sdanielk1977 */ 174d0441796Sdanielk1977 Tcl_UnregisterChannel(pDb->interp, p->channel); 175d0441796Sdanielk1977 } 176d0441796Sdanielk1977 } 177d0441796Sdanielk1977 178d0441796Sdanielk1977 /* 179b4e9af9fSdanielk1977 ** Close an incremental blob channel. 180b4e9af9fSdanielk1977 */ 181b4e9af9fSdanielk1977 static int incrblobClose(ClientData instanceData, Tcl_Interp *interp){ 182b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 18392d4d7a9Sdanielk1977 int rc = sqlite3_blob_close(p->pBlob); 18492d4d7a9Sdanielk1977 sqlite3 *db = p->pDb->db; 185d0441796Sdanielk1977 186d0441796Sdanielk1977 /* Remove the channel from the SqliteDb.pIncrblob list. */ 187d0441796Sdanielk1977 if( p->pNext ){ 188d0441796Sdanielk1977 p->pNext->pPrev = p->pPrev; 189d0441796Sdanielk1977 } 190d0441796Sdanielk1977 if( p->pPrev ){ 191d0441796Sdanielk1977 p->pPrev->pNext = p->pNext; 192d0441796Sdanielk1977 } 193d0441796Sdanielk1977 if( p->pDb->pIncrblob==p ){ 194d0441796Sdanielk1977 p->pDb->pIncrblob = p->pNext; 195d0441796Sdanielk1977 } 196d0441796Sdanielk1977 19792d4d7a9Sdanielk1977 /* Free the IncrblobChannel structure */ 198b4e9af9fSdanielk1977 Tcl_Free((char *)p); 19992d4d7a9Sdanielk1977 20092d4d7a9Sdanielk1977 if( rc!=SQLITE_OK ){ 20192d4d7a9Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); 20292d4d7a9Sdanielk1977 return TCL_ERROR; 20392d4d7a9Sdanielk1977 } 204b4e9af9fSdanielk1977 return TCL_OK; 205b4e9af9fSdanielk1977 } 206b4e9af9fSdanielk1977 207b4e9af9fSdanielk1977 /* 208b4e9af9fSdanielk1977 ** Read data from an incremental blob channel. 209b4e9af9fSdanielk1977 */ 210b4e9af9fSdanielk1977 static int incrblobInput( 211b4e9af9fSdanielk1977 ClientData instanceData, 212b4e9af9fSdanielk1977 char *buf, 213b4e9af9fSdanielk1977 int bufSize, 214b4e9af9fSdanielk1977 int *errorCodePtr 215b4e9af9fSdanielk1977 ){ 216b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 217b4e9af9fSdanielk1977 int nRead = bufSize; /* Number of bytes to read */ 218b4e9af9fSdanielk1977 int nBlob; /* Total size of the blob */ 219b4e9af9fSdanielk1977 int rc; /* sqlite error code */ 220b4e9af9fSdanielk1977 221b4e9af9fSdanielk1977 nBlob = sqlite3_blob_bytes(p->pBlob); 222b4e9af9fSdanielk1977 if( (p->iSeek+nRead)>nBlob ){ 223b4e9af9fSdanielk1977 nRead = nBlob-p->iSeek; 224b4e9af9fSdanielk1977 } 225b4e9af9fSdanielk1977 if( nRead<=0 ){ 226b4e9af9fSdanielk1977 return 0; 227b4e9af9fSdanielk1977 } 228b4e9af9fSdanielk1977 229b4e9af9fSdanielk1977 rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek); 230b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 231b4e9af9fSdanielk1977 *errorCodePtr = rc; 232b4e9af9fSdanielk1977 return -1; 233b4e9af9fSdanielk1977 } 234b4e9af9fSdanielk1977 235b4e9af9fSdanielk1977 p->iSeek += nRead; 236b4e9af9fSdanielk1977 return nRead; 237b4e9af9fSdanielk1977 } 238b4e9af9fSdanielk1977 239d0441796Sdanielk1977 /* 240d0441796Sdanielk1977 ** Write data to an incremental blob channel. 241d0441796Sdanielk1977 */ 242b4e9af9fSdanielk1977 static int incrblobOutput( 243b4e9af9fSdanielk1977 ClientData instanceData, 244b4e9af9fSdanielk1977 CONST char *buf, 245b4e9af9fSdanielk1977 int toWrite, 246b4e9af9fSdanielk1977 int *errorCodePtr 247b4e9af9fSdanielk1977 ){ 248b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 249b4e9af9fSdanielk1977 int nWrite = toWrite; /* Number of bytes to write */ 250b4e9af9fSdanielk1977 int nBlob; /* Total size of the blob */ 251b4e9af9fSdanielk1977 int rc; /* sqlite error code */ 252b4e9af9fSdanielk1977 253b4e9af9fSdanielk1977 nBlob = sqlite3_blob_bytes(p->pBlob); 254b4e9af9fSdanielk1977 if( (p->iSeek+nWrite)>nBlob ){ 255b4e9af9fSdanielk1977 *errorCodePtr = EINVAL; 256b4e9af9fSdanielk1977 return -1; 257b4e9af9fSdanielk1977 } 258b4e9af9fSdanielk1977 if( nWrite<=0 ){ 259b4e9af9fSdanielk1977 return 0; 260b4e9af9fSdanielk1977 } 261b4e9af9fSdanielk1977 262b4e9af9fSdanielk1977 rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek); 263b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 264b4e9af9fSdanielk1977 *errorCodePtr = EIO; 265b4e9af9fSdanielk1977 return -1; 266b4e9af9fSdanielk1977 } 267b4e9af9fSdanielk1977 268b4e9af9fSdanielk1977 p->iSeek += nWrite; 269b4e9af9fSdanielk1977 return nWrite; 270b4e9af9fSdanielk1977 } 271b4e9af9fSdanielk1977 272b4e9af9fSdanielk1977 /* 273b4e9af9fSdanielk1977 ** Seek an incremental blob channel. 274b4e9af9fSdanielk1977 */ 275b4e9af9fSdanielk1977 static int incrblobSeek( 276b4e9af9fSdanielk1977 ClientData instanceData, 277b4e9af9fSdanielk1977 long offset, 278b4e9af9fSdanielk1977 int seekMode, 279b4e9af9fSdanielk1977 int *errorCodePtr 280b4e9af9fSdanielk1977 ){ 281b4e9af9fSdanielk1977 IncrblobChannel *p = (IncrblobChannel *)instanceData; 282b4e9af9fSdanielk1977 283b4e9af9fSdanielk1977 switch( seekMode ){ 284b4e9af9fSdanielk1977 case SEEK_SET: 285b4e9af9fSdanielk1977 p->iSeek = offset; 286b4e9af9fSdanielk1977 break; 287b4e9af9fSdanielk1977 case SEEK_CUR: 288b4e9af9fSdanielk1977 p->iSeek += offset; 289b4e9af9fSdanielk1977 break; 290b4e9af9fSdanielk1977 case SEEK_END: 291b4e9af9fSdanielk1977 p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset; 292b4e9af9fSdanielk1977 break; 293b4e9af9fSdanielk1977 294b4e9af9fSdanielk1977 default: assert(!"Bad seekMode"); 295b4e9af9fSdanielk1977 } 296b4e9af9fSdanielk1977 297b4e9af9fSdanielk1977 return p->iSeek; 298b4e9af9fSdanielk1977 } 299b4e9af9fSdanielk1977 300b4e9af9fSdanielk1977 301b4e9af9fSdanielk1977 static void incrblobWatch(ClientData instanceData, int mode){ 302b4e9af9fSdanielk1977 /* NO-OP */ 303b4e9af9fSdanielk1977 } 304b4e9af9fSdanielk1977 static int incrblobHandle(ClientData instanceData, int dir, ClientData *hPtr){ 305b4e9af9fSdanielk1977 return TCL_ERROR; 306b4e9af9fSdanielk1977 } 307b4e9af9fSdanielk1977 308b4e9af9fSdanielk1977 static Tcl_ChannelType IncrblobChannelType = { 309b4e9af9fSdanielk1977 "incrblob", /* typeName */ 310b4e9af9fSdanielk1977 TCL_CHANNEL_VERSION_2, /* version */ 311b4e9af9fSdanielk1977 incrblobClose, /* closeProc */ 312b4e9af9fSdanielk1977 incrblobInput, /* inputProc */ 313b4e9af9fSdanielk1977 incrblobOutput, /* outputProc */ 314b4e9af9fSdanielk1977 incrblobSeek, /* seekProc */ 315b4e9af9fSdanielk1977 0, /* setOptionProc */ 316b4e9af9fSdanielk1977 0, /* getOptionProc */ 317b4e9af9fSdanielk1977 incrblobWatch, /* watchProc (this is a no-op) */ 318b4e9af9fSdanielk1977 incrblobHandle, /* getHandleProc (always returns error) */ 319b4e9af9fSdanielk1977 0, /* close2Proc */ 320b4e9af9fSdanielk1977 0, /* blockModeProc */ 321b4e9af9fSdanielk1977 0, /* flushProc */ 322b4e9af9fSdanielk1977 0, /* handlerProc */ 323b4e9af9fSdanielk1977 0, /* wideSeekProc */ 324b4e9af9fSdanielk1977 }; 325b4e9af9fSdanielk1977 326b4e9af9fSdanielk1977 /* 327b4e9af9fSdanielk1977 ** Create a new incrblob channel. 328b4e9af9fSdanielk1977 */ 329b4e9af9fSdanielk1977 static int createIncrblobChannel( 330b4e9af9fSdanielk1977 Tcl_Interp *interp, 331b4e9af9fSdanielk1977 SqliteDb *pDb, 332b4e9af9fSdanielk1977 const char *zDb, 333b4e9af9fSdanielk1977 const char *zTable, 334b4e9af9fSdanielk1977 const char *zColumn, 3358cbadb02Sdanielk1977 sqlite_int64 iRow, 3368cbadb02Sdanielk1977 int isReadonly 337b4e9af9fSdanielk1977 ){ 338b4e9af9fSdanielk1977 IncrblobChannel *p; 3398cbadb02Sdanielk1977 sqlite3 *db = pDb->db; 340b4e9af9fSdanielk1977 sqlite3_blob *pBlob; 341b4e9af9fSdanielk1977 int rc; 3428cbadb02Sdanielk1977 int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE); 343b4e9af9fSdanielk1977 344b4e9af9fSdanielk1977 /* This variable is used to name the channels: "incrblob_[incr count]" */ 345b4e9af9fSdanielk1977 static int count = 0; 346b4e9af9fSdanielk1977 char zChannel[64]; 347b4e9af9fSdanielk1977 3488cbadb02Sdanielk1977 rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob); 349b4e9af9fSdanielk1977 if( rc!=SQLITE_OK ){ 350b4e9af9fSdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 351b4e9af9fSdanielk1977 return TCL_ERROR; 352b4e9af9fSdanielk1977 } 353b4e9af9fSdanielk1977 354b4e9af9fSdanielk1977 p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel)); 355b4e9af9fSdanielk1977 p->iSeek = 0; 356b4e9af9fSdanielk1977 p->pBlob = pBlob; 357b4e9af9fSdanielk1977 3585bb3eb9bSdrh sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count); 359d0441796Sdanielk1977 p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags); 360d0441796Sdanielk1977 Tcl_RegisterChannel(interp, p->channel); 361b4e9af9fSdanielk1977 362d0441796Sdanielk1977 /* Link the new channel into the SqliteDb.pIncrblob list. */ 363d0441796Sdanielk1977 p->pNext = pDb->pIncrblob; 364d0441796Sdanielk1977 p->pPrev = 0; 365d0441796Sdanielk1977 if( p->pNext ){ 366d0441796Sdanielk1977 p->pNext->pPrev = p; 367d0441796Sdanielk1977 } 368d0441796Sdanielk1977 pDb->pIncrblob = p; 369d0441796Sdanielk1977 p->pDb = pDb; 370d0441796Sdanielk1977 371d0441796Sdanielk1977 Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE); 372b4e9af9fSdanielk1977 return TCL_OK; 373b4e9af9fSdanielk1977 } 37432a0d8bbSdanielk1977 #else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */ 37532a0d8bbSdanielk1977 #define closeIncrblobChannels(pDb) 37632a0d8bbSdanielk1977 #endif 377b4e9af9fSdanielk1977 3786d31316cSdrh /* 379d1e4733dSdrh ** Look at the script prefix in pCmd. We will be executing this script 380d1e4733dSdrh ** after first appending one or more arguments. This routine analyzes 381d1e4733dSdrh ** the script to see if it is safe to use Tcl_EvalObjv() on the script 382d1e4733dSdrh ** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much 383d1e4733dSdrh ** faster. 384d1e4733dSdrh ** 385d1e4733dSdrh ** Scripts that are safe to use with Tcl_EvalObjv() consists of a 386d1e4733dSdrh ** command name followed by zero or more arguments with no [...] or $ 387d1e4733dSdrh ** or {...} or ; to be seen anywhere. Most callback scripts consist 388d1e4733dSdrh ** of just a single procedure name and they meet this requirement. 389d1e4733dSdrh */ 390d1e4733dSdrh static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){ 391d1e4733dSdrh /* We could try to do something with Tcl_Parse(). But we will instead 392d1e4733dSdrh ** just do a search for forbidden characters. If any of the forbidden 393d1e4733dSdrh ** characters appear in pCmd, we will report the string as unsafe. 394d1e4733dSdrh */ 395d1e4733dSdrh const char *z; 396d1e4733dSdrh int n; 397d1e4733dSdrh z = Tcl_GetStringFromObj(pCmd, &n); 398d1e4733dSdrh while( n-- > 0 ){ 399d1e4733dSdrh int c = *(z++); 400d1e4733dSdrh if( c=='$' || c=='[' || c==';' ) return 0; 401d1e4733dSdrh } 402d1e4733dSdrh return 1; 403d1e4733dSdrh } 404d1e4733dSdrh 405d1e4733dSdrh /* 406d1e4733dSdrh ** Find an SqlFunc structure with the given name. Or create a new 407d1e4733dSdrh ** one if an existing one cannot be found. Return a pointer to the 408d1e4733dSdrh ** structure. 409d1e4733dSdrh */ 410d1e4733dSdrh static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){ 411d1e4733dSdrh SqlFunc *p, *pNew; 412d1e4733dSdrh int i; 4134f21c4afSdrh pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + strlen30(zName) + 1 ); 414d1e4733dSdrh pNew->zName = (char*)&pNew[1]; 415d1e4733dSdrh for(i=0; zName[i]; i++){ pNew->zName[i] = tolower(zName[i]); } 416d1e4733dSdrh pNew->zName[i] = 0; 417d1e4733dSdrh for(p=pDb->pFunc; p; p=p->pNext){ 418d1e4733dSdrh if( strcmp(p->zName, pNew->zName)==0 ){ 419d1e4733dSdrh Tcl_Free((char*)pNew); 420d1e4733dSdrh return p; 421d1e4733dSdrh } 422d1e4733dSdrh } 423d1e4733dSdrh pNew->interp = pDb->interp; 424d1e4733dSdrh pNew->pScript = 0; 425d1e4733dSdrh pNew->pNext = pDb->pFunc; 426d1e4733dSdrh pDb->pFunc = pNew; 427d1e4733dSdrh return pNew; 428d1e4733dSdrh } 429d1e4733dSdrh 430d1e4733dSdrh /* 431fb7e7651Sdrh ** Finalize and free a list of prepared statements 432fb7e7651Sdrh */ 433fb7e7651Sdrh static void flushStmtCache( SqliteDb *pDb ){ 434fb7e7651Sdrh SqlPreparedStmt *pPreStmt; 435fb7e7651Sdrh 436fb7e7651Sdrh while( pDb->stmtList ){ 437fb7e7651Sdrh sqlite3_finalize( pDb->stmtList->pStmt ); 438fb7e7651Sdrh pPreStmt = pDb->stmtList; 439fb7e7651Sdrh pDb->stmtList = pDb->stmtList->pNext; 440fb7e7651Sdrh Tcl_Free( (char*)pPreStmt ); 441fb7e7651Sdrh } 442fb7e7651Sdrh pDb->nStmt = 0; 443fb7e7651Sdrh pDb->stmtLast = 0; 444fb7e7651Sdrh } 445fb7e7651Sdrh 446fb7e7651Sdrh /* 447895d7472Sdrh ** TCL calls this procedure when an sqlite3 database command is 448895d7472Sdrh ** deleted. 44975897234Sdrh */ 45075897234Sdrh static void DbDeleteCmd(void *db){ 451bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)db; 452fb7e7651Sdrh flushStmtCache(pDb); 453d0441796Sdanielk1977 closeIncrblobChannels(pDb); 4546f8a503dSdanielk1977 sqlite3_close(pDb->db); 455cabb0819Sdrh while( pDb->pFunc ){ 456cabb0819Sdrh SqlFunc *pFunc = pDb->pFunc; 457cabb0819Sdrh pDb->pFunc = pFunc->pNext; 458d1e4733dSdrh Tcl_DecrRefCount(pFunc->pScript); 459cabb0819Sdrh Tcl_Free((char*)pFunc); 460cabb0819Sdrh } 4610202b29eSdanielk1977 while( pDb->pCollate ){ 4620202b29eSdanielk1977 SqlCollate *pCollate = pDb->pCollate; 4630202b29eSdanielk1977 pDb->pCollate = pCollate->pNext; 4640202b29eSdanielk1977 Tcl_Free((char*)pCollate); 4650202b29eSdanielk1977 } 466bec3f402Sdrh if( pDb->zBusy ){ 467bec3f402Sdrh Tcl_Free(pDb->zBusy); 468bec3f402Sdrh } 469b5a20d3cSdrh if( pDb->zTrace ){ 470b5a20d3cSdrh Tcl_Free(pDb->zTrace); 4710d1a643aSdrh } 47219e2d37fSdrh if( pDb->zProfile ){ 47319e2d37fSdrh Tcl_Free(pDb->zProfile); 47419e2d37fSdrh } 475e22a334bSdrh if( pDb->zAuth ){ 476e22a334bSdrh Tcl_Free(pDb->zAuth); 477e22a334bSdrh } 47855c45f2eSdanielk1977 if( pDb->zNull ){ 47955c45f2eSdanielk1977 Tcl_Free(pDb->zNull); 48055c45f2eSdanielk1977 } 48194eb6a14Sdanielk1977 if( pDb->pUpdateHook ){ 48294eb6a14Sdanielk1977 Tcl_DecrRefCount(pDb->pUpdateHook); 48394eb6a14Sdanielk1977 } 48471fd80bfSdanielk1977 if( pDb->pRollbackHook ){ 48571fd80bfSdanielk1977 Tcl_DecrRefCount(pDb->pRollbackHook); 48671fd80bfSdanielk1977 } 48794eb6a14Sdanielk1977 if( pDb->pCollateNeeded ){ 48894eb6a14Sdanielk1977 Tcl_DecrRefCount(pDb->pCollateNeeded); 48994eb6a14Sdanielk1977 } 490bec3f402Sdrh Tcl_Free((char*)pDb); 491bec3f402Sdrh } 492bec3f402Sdrh 493bec3f402Sdrh /* 494bec3f402Sdrh ** This routine is called when a database file is locked while trying 495bec3f402Sdrh ** to execute SQL. 496bec3f402Sdrh */ 4972a764eb0Sdanielk1977 static int DbBusyHandler(void *cd, int nTries){ 498bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 499bec3f402Sdrh int rc; 500bec3f402Sdrh char zVal[30]; 501bec3f402Sdrh 5025bb3eb9bSdrh sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries); 503d1e4733dSdrh rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0); 504bec3f402Sdrh if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 505bec3f402Sdrh return 0; 506bec3f402Sdrh } 507bec3f402Sdrh return 1; 50875897234Sdrh } 50975897234Sdrh 51026e4a8b1Sdrh #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 51175897234Sdrh /* 512348bb5d6Sdanielk1977 ** This routine is invoked as the 'progress callback' for the database. 513348bb5d6Sdanielk1977 */ 514348bb5d6Sdanielk1977 static int DbProgressHandler(void *cd){ 515348bb5d6Sdanielk1977 SqliteDb *pDb = (SqliteDb*)cd; 516348bb5d6Sdanielk1977 int rc; 517348bb5d6Sdanielk1977 518348bb5d6Sdanielk1977 assert( pDb->zProgress ); 519348bb5d6Sdanielk1977 rc = Tcl_Eval(pDb->interp, pDb->zProgress); 520348bb5d6Sdanielk1977 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 521348bb5d6Sdanielk1977 return 1; 522348bb5d6Sdanielk1977 } 523348bb5d6Sdanielk1977 return 0; 524348bb5d6Sdanielk1977 } 52526e4a8b1Sdrh #endif 526348bb5d6Sdanielk1977 527d1167393Sdrh #ifndef SQLITE_OMIT_TRACE 528348bb5d6Sdanielk1977 /* 529b5a20d3cSdrh ** This routine is called by the SQLite trace handler whenever a new 530b5a20d3cSdrh ** block of SQL is executed. The TCL script in pDb->zTrace is executed. 5310d1a643aSdrh */ 532b5a20d3cSdrh static void DbTraceHandler(void *cd, const char *zSql){ 5330d1a643aSdrh SqliteDb *pDb = (SqliteDb*)cd; 534b5a20d3cSdrh Tcl_DString str; 5350d1a643aSdrh 536b5a20d3cSdrh Tcl_DStringInit(&str); 537b5a20d3cSdrh Tcl_DStringAppend(&str, pDb->zTrace, -1); 538b5a20d3cSdrh Tcl_DStringAppendElement(&str, zSql); 539b5a20d3cSdrh Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 540b5a20d3cSdrh Tcl_DStringFree(&str); 541b5a20d3cSdrh Tcl_ResetResult(pDb->interp); 5420d1a643aSdrh } 543d1167393Sdrh #endif 5440d1a643aSdrh 545d1167393Sdrh #ifndef SQLITE_OMIT_TRACE 5460d1a643aSdrh /* 54719e2d37fSdrh ** This routine is called by the SQLite profile handler after a statement 54819e2d37fSdrh ** SQL has executed. The TCL script in pDb->zProfile is evaluated. 54919e2d37fSdrh */ 55019e2d37fSdrh static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){ 55119e2d37fSdrh SqliteDb *pDb = (SqliteDb*)cd; 55219e2d37fSdrh Tcl_DString str; 55319e2d37fSdrh char zTm[100]; 55419e2d37fSdrh 55519e2d37fSdrh sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm); 55619e2d37fSdrh Tcl_DStringInit(&str); 55719e2d37fSdrh Tcl_DStringAppend(&str, pDb->zProfile, -1); 55819e2d37fSdrh Tcl_DStringAppendElement(&str, zSql); 55919e2d37fSdrh Tcl_DStringAppendElement(&str, zTm); 56019e2d37fSdrh Tcl_Eval(pDb->interp, Tcl_DStringValue(&str)); 56119e2d37fSdrh Tcl_DStringFree(&str); 56219e2d37fSdrh Tcl_ResetResult(pDb->interp); 56319e2d37fSdrh } 564d1167393Sdrh #endif 56519e2d37fSdrh 56619e2d37fSdrh /* 567aa940eacSdrh ** This routine is called when a transaction is committed. The 568aa940eacSdrh ** TCL script in pDb->zCommit is executed. If it returns non-zero or 569aa940eacSdrh ** if it throws an exception, the transaction is rolled back instead 570aa940eacSdrh ** of being committed. 571aa940eacSdrh */ 572aa940eacSdrh static int DbCommitHandler(void *cd){ 573aa940eacSdrh SqliteDb *pDb = (SqliteDb*)cd; 574aa940eacSdrh int rc; 575aa940eacSdrh 576aa940eacSdrh rc = Tcl_Eval(pDb->interp, pDb->zCommit); 577aa940eacSdrh if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 578aa940eacSdrh return 1; 579aa940eacSdrh } 580aa940eacSdrh return 0; 581aa940eacSdrh } 582aa940eacSdrh 58371fd80bfSdanielk1977 static void DbRollbackHandler(void *clientData){ 58471fd80bfSdanielk1977 SqliteDb *pDb = (SqliteDb*)clientData; 58571fd80bfSdanielk1977 assert(pDb->pRollbackHook); 58671fd80bfSdanielk1977 if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){ 58771fd80bfSdanielk1977 Tcl_BackgroundError(pDb->interp); 58871fd80bfSdanielk1977 } 58971fd80bfSdanielk1977 } 59071fd80bfSdanielk1977 591bcf4f484Sdrh #if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_UNLOCK_NOTIFY) 592404ca075Sdanielk1977 static void setTestUnlockNotifyVars(Tcl_Interp *interp, int iArg, int nArg){ 593404ca075Sdanielk1977 char zBuf[64]; 594404ca075Sdanielk1977 sprintf(zBuf, "%d", iArg); 595404ca075Sdanielk1977 Tcl_SetVar(interp, "sqlite_unlock_notify_arg", zBuf, TCL_GLOBAL_ONLY); 596404ca075Sdanielk1977 sprintf(zBuf, "%d", nArg); 597404ca075Sdanielk1977 Tcl_SetVar(interp, "sqlite_unlock_notify_argcount", zBuf, TCL_GLOBAL_ONLY); 598404ca075Sdanielk1977 } 599404ca075Sdanielk1977 #else 600404ca075Sdanielk1977 # define setTestUnlockNotifyVars(x,y,z) 601404ca075Sdanielk1977 #endif 602404ca075Sdanielk1977 60369910da9Sdrh #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 604404ca075Sdanielk1977 static void DbUnlockNotify(void **apArg, int nArg){ 605404ca075Sdanielk1977 int i; 606404ca075Sdanielk1977 for(i=0; i<nArg; i++){ 607404ca075Sdanielk1977 const int flags = (TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); 608404ca075Sdanielk1977 SqliteDb *pDb = (SqliteDb *)apArg[i]; 609404ca075Sdanielk1977 setTestUnlockNotifyVars(pDb->interp, i, nArg); 610404ca075Sdanielk1977 assert( pDb->pUnlockNotify); 611404ca075Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pDb->pUnlockNotify, flags); 612404ca075Sdanielk1977 Tcl_DecrRefCount(pDb->pUnlockNotify); 613404ca075Sdanielk1977 pDb->pUnlockNotify = 0; 614404ca075Sdanielk1977 } 615404ca075Sdanielk1977 } 61669910da9Sdrh #endif 617404ca075Sdanielk1977 61894eb6a14Sdanielk1977 static void DbUpdateHandler( 61994eb6a14Sdanielk1977 void *p, 62094eb6a14Sdanielk1977 int op, 62194eb6a14Sdanielk1977 const char *zDb, 62294eb6a14Sdanielk1977 const char *zTbl, 62394eb6a14Sdanielk1977 sqlite_int64 rowid 62494eb6a14Sdanielk1977 ){ 62594eb6a14Sdanielk1977 SqliteDb *pDb = (SqliteDb *)p; 62694eb6a14Sdanielk1977 Tcl_Obj *pCmd; 62794eb6a14Sdanielk1977 62894eb6a14Sdanielk1977 assert( pDb->pUpdateHook ); 62994eb6a14Sdanielk1977 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); 63094eb6a14Sdanielk1977 63194eb6a14Sdanielk1977 pCmd = Tcl_DuplicateObj(pDb->pUpdateHook); 63294eb6a14Sdanielk1977 Tcl_IncrRefCount(pCmd); 63394eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj( 63494eb6a14Sdanielk1977 ( (op==SQLITE_INSERT)?"INSERT":(op==SQLITE_UPDATE)?"UPDATE":"DELETE"), -1)); 63594eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); 63694eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); 63794eb6a14Sdanielk1977 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid)); 63894eb6a14Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); 63994eb6a14Sdanielk1977 } 64094eb6a14Sdanielk1977 6417cedc8d4Sdanielk1977 static void tclCollateNeeded( 6427cedc8d4Sdanielk1977 void *pCtx, 6439bb575fdSdrh sqlite3 *db, 6447cedc8d4Sdanielk1977 int enc, 6457cedc8d4Sdanielk1977 const char *zName 6467cedc8d4Sdanielk1977 ){ 6477cedc8d4Sdanielk1977 SqliteDb *pDb = (SqliteDb *)pCtx; 6487cedc8d4Sdanielk1977 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded); 6497cedc8d4Sdanielk1977 Tcl_IncrRefCount(pScript); 6507cedc8d4Sdanielk1977 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1)); 6517cedc8d4Sdanielk1977 Tcl_EvalObjEx(pDb->interp, pScript, 0); 6527cedc8d4Sdanielk1977 Tcl_DecrRefCount(pScript); 6537cedc8d4Sdanielk1977 } 6547cedc8d4Sdanielk1977 655aa940eacSdrh /* 6560202b29eSdanielk1977 ** This routine is called to evaluate an SQL collation function implemented 6570202b29eSdanielk1977 ** using TCL script. 6580202b29eSdanielk1977 */ 6590202b29eSdanielk1977 static int tclSqlCollate( 6600202b29eSdanielk1977 void *pCtx, 6610202b29eSdanielk1977 int nA, 6620202b29eSdanielk1977 const void *zA, 6630202b29eSdanielk1977 int nB, 6640202b29eSdanielk1977 const void *zB 6650202b29eSdanielk1977 ){ 6660202b29eSdanielk1977 SqlCollate *p = (SqlCollate *)pCtx; 6670202b29eSdanielk1977 Tcl_Obj *pCmd; 6680202b29eSdanielk1977 6690202b29eSdanielk1977 pCmd = Tcl_NewStringObj(p->zScript, -1); 6700202b29eSdanielk1977 Tcl_IncrRefCount(pCmd); 6710202b29eSdanielk1977 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA)); 6720202b29eSdanielk1977 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB)); 673d1e4733dSdrh Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 6740202b29eSdanielk1977 Tcl_DecrRefCount(pCmd); 6750202b29eSdanielk1977 return (atoi(Tcl_GetStringResult(p->interp))); 6760202b29eSdanielk1977 } 6770202b29eSdanielk1977 6780202b29eSdanielk1977 /* 679cabb0819Sdrh ** This routine is called to evaluate an SQL function implemented 680cabb0819Sdrh ** using TCL script. 681cabb0819Sdrh */ 6820ae8b831Sdanielk1977 static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ 6836f8a503dSdanielk1977 SqlFunc *p = sqlite3_user_data(context); 684d1e4733dSdrh Tcl_Obj *pCmd; 685cabb0819Sdrh int i; 686cabb0819Sdrh int rc; 687cabb0819Sdrh 688d1e4733dSdrh if( argc==0 ){ 689d1e4733dSdrh /* If there are no arguments to the function, call Tcl_EvalObjEx on the 690d1e4733dSdrh ** script object directly. This allows the TCL compiler to generate 691d1e4733dSdrh ** bytecode for the command on the first invocation and thus make 692d1e4733dSdrh ** subsequent invocations much faster. */ 693d1e4733dSdrh pCmd = p->pScript; 694d1e4733dSdrh Tcl_IncrRefCount(pCmd); 695d1e4733dSdrh rc = Tcl_EvalObjEx(p->interp, pCmd, 0); 696d1e4733dSdrh Tcl_DecrRefCount(pCmd); 69751ad0ecdSdanielk1977 }else{ 698d1e4733dSdrh /* If there are arguments to the function, make a shallow copy of the 699d1e4733dSdrh ** script object, lappend the arguments, then evaluate the copy. 700d1e4733dSdrh ** 701d1e4733dSdrh ** By "shallow" copy, we mean a only the outer list Tcl_Obj is duplicated. 702d1e4733dSdrh ** The new Tcl_Obj contains pointers to the original list elements. 703d1e4733dSdrh ** That way, when Tcl_EvalObjv() is run and shimmers the first element 704d1e4733dSdrh ** of the list to tclCmdNameType, that alternate representation will 705d1e4733dSdrh ** be preserved and reused on the next invocation. 706d1e4733dSdrh */ 707d1e4733dSdrh Tcl_Obj **aArg; 708d1e4733dSdrh int nArg; 709d1e4733dSdrh if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){ 710d1e4733dSdrh sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 711d1e4733dSdrh return; 712d1e4733dSdrh } 713d1e4733dSdrh pCmd = Tcl_NewListObj(nArg, aArg); 714d1e4733dSdrh Tcl_IncrRefCount(pCmd); 715d1e4733dSdrh for(i=0; i<argc; i++){ 716d1e4733dSdrh sqlite3_value *pIn = argv[i]; 717d1e4733dSdrh Tcl_Obj *pVal; 718d1e4733dSdrh 719d1e4733dSdrh /* Set pVal to contain the i'th column of this row. */ 720d1e4733dSdrh switch( sqlite3_value_type(pIn) ){ 721d1e4733dSdrh case SQLITE_BLOB: { 722d1e4733dSdrh int bytes = sqlite3_value_bytes(pIn); 723d1e4733dSdrh pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes); 724d1e4733dSdrh break; 725d1e4733dSdrh } 726d1e4733dSdrh case SQLITE_INTEGER: { 727d1e4733dSdrh sqlite_int64 v = sqlite3_value_int64(pIn); 728d1e4733dSdrh if( v>=-2147483647 && v<=2147483647 ){ 729d1e4733dSdrh pVal = Tcl_NewIntObj(v); 730d1e4733dSdrh }else{ 731d1e4733dSdrh pVal = Tcl_NewWideIntObj(v); 732d1e4733dSdrh } 733d1e4733dSdrh break; 734d1e4733dSdrh } 735d1e4733dSdrh case SQLITE_FLOAT: { 736d1e4733dSdrh double r = sqlite3_value_double(pIn); 737d1e4733dSdrh pVal = Tcl_NewDoubleObj(r); 738d1e4733dSdrh break; 739d1e4733dSdrh } 740d1e4733dSdrh case SQLITE_NULL: { 741d1e4733dSdrh pVal = Tcl_NewStringObj("", 0); 742d1e4733dSdrh break; 743d1e4733dSdrh } 744d1e4733dSdrh default: { 745d1e4733dSdrh int bytes = sqlite3_value_bytes(pIn); 74600fd957bSdanielk1977 pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes); 747d1e4733dSdrh break; 74851ad0ecdSdanielk1977 } 749cabb0819Sdrh } 750d1e4733dSdrh rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal); 751d1e4733dSdrh if( rc ){ 752d1e4733dSdrh Tcl_DecrRefCount(pCmd); 753d1e4733dSdrh sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 754d1e4733dSdrh return; 755d1e4733dSdrh } 756d1e4733dSdrh } 757d1e4733dSdrh if( !p->useEvalObjv ){ 758d1e4733dSdrh /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd 759d1e4733dSdrh ** is a list without a string representation. To prevent this from 760d1e4733dSdrh ** happening, make sure pCmd has a valid string representation */ 761d1e4733dSdrh Tcl_GetString(pCmd); 762d1e4733dSdrh } 763d1e4733dSdrh rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT); 764d1e4733dSdrh Tcl_DecrRefCount(pCmd); 765d1e4733dSdrh } 766562e8d3cSdanielk1977 767c7f269d5Sdrh if( rc && rc!=TCL_RETURN ){ 7687e18c259Sdanielk1977 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); 769cabb0819Sdrh }else{ 770c7f269d5Sdrh Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); 771c7f269d5Sdrh int n; 772c7f269d5Sdrh u8 *data; 7734a4c11aaSdan const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 774c7f269d5Sdrh char c = zType[0]; 775df0bddaeSdrh if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ 776d1e4733dSdrh /* Only return a BLOB type if the Tcl variable is a bytearray and 777df0bddaeSdrh ** has no string representation. */ 778c7f269d5Sdrh data = Tcl_GetByteArrayFromObj(pVar, &n); 779c7f269d5Sdrh sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT); 780985e0c63Sdrh }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 781c7f269d5Sdrh Tcl_GetIntFromObj(0, pVar, &n); 782c7f269d5Sdrh sqlite3_result_int(context, n); 783c7f269d5Sdrh }else if( c=='d' && strcmp(zType,"double")==0 ){ 784c7f269d5Sdrh double r; 785c7f269d5Sdrh Tcl_GetDoubleFromObj(0, pVar, &r); 786c7f269d5Sdrh sqlite3_result_double(context, r); 787985e0c63Sdrh }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 788985e0c63Sdrh (c=='i' && strcmp(zType,"int")==0) ){ 789df0bddaeSdrh Tcl_WideInt v; 790df0bddaeSdrh Tcl_GetWideIntFromObj(0, pVar, &v); 791df0bddaeSdrh sqlite3_result_int64(context, v); 792c7f269d5Sdrh }else{ 79300fd957bSdanielk1977 data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 79400fd957bSdanielk1977 sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT); 795c7f269d5Sdrh } 796cabb0819Sdrh } 797cabb0819Sdrh } 798895d7472Sdrh 799e22a334bSdrh #ifndef SQLITE_OMIT_AUTHORIZATION 800e22a334bSdrh /* 801e22a334bSdrh ** This is the authentication function. It appends the authentication 802e22a334bSdrh ** type code and the two arguments to zCmd[] then invokes the result 803e22a334bSdrh ** on the interpreter. The reply is examined to determine if the 804e22a334bSdrh ** authentication fails or succeeds. 805e22a334bSdrh */ 806e22a334bSdrh static int auth_callback( 807e22a334bSdrh void *pArg, 808e22a334bSdrh int code, 809e22a334bSdrh const char *zArg1, 810e22a334bSdrh const char *zArg2, 811e22a334bSdrh const char *zArg3, 812e22a334bSdrh const char *zArg4 813e22a334bSdrh ){ 814e22a334bSdrh char *zCode; 815e22a334bSdrh Tcl_DString str; 816e22a334bSdrh int rc; 817e22a334bSdrh const char *zReply; 818e22a334bSdrh SqliteDb *pDb = (SqliteDb*)pArg; 8191f1549f8Sdrh if( pDb->disableAuth ) return SQLITE_OK; 820e22a334bSdrh 821e22a334bSdrh switch( code ){ 822e22a334bSdrh case SQLITE_COPY : zCode="SQLITE_COPY"; break; 823e22a334bSdrh case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; 824e22a334bSdrh case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break; 825e22a334bSdrh case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break; 826e22a334bSdrh case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break; 827e22a334bSdrh case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break; 828e22a334bSdrh case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break; 829e22a334bSdrh case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break; 830e22a334bSdrh case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break; 831e22a334bSdrh case SQLITE_DELETE : zCode="SQLITE_DELETE"; break; 832e22a334bSdrh case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break; 833e22a334bSdrh case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break; 834e22a334bSdrh case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break; 835e22a334bSdrh case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break; 836e22a334bSdrh case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break; 837e22a334bSdrh case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break; 838e22a334bSdrh case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break; 839e22a334bSdrh case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break; 840e22a334bSdrh case SQLITE_INSERT : zCode="SQLITE_INSERT"; break; 841e22a334bSdrh case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break; 842e22a334bSdrh case SQLITE_READ : zCode="SQLITE_READ"; break; 843e22a334bSdrh case SQLITE_SELECT : zCode="SQLITE_SELECT"; break; 844e22a334bSdrh case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break; 845e22a334bSdrh case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break; 84681e293b4Sdrh case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break; 84781e293b4Sdrh case SQLITE_DETACH : zCode="SQLITE_DETACH"; break; 8481c8c23ccSdanielk1977 case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break; 8491d54df88Sdanielk1977 case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break; 850e6e04969Sdrh case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break; 851f1a381e7Sdanielk1977 case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break; 852f1a381e7Sdanielk1977 case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break; 8535169bbc6Sdrh case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break; 854ab9b703fSdanielk1977 case SQLITE_SAVEPOINT : zCode="SQLITE_SAVEPOINT"; break; 855e22a334bSdrh default : zCode="????"; break; 856e22a334bSdrh } 857e22a334bSdrh Tcl_DStringInit(&str); 858e22a334bSdrh Tcl_DStringAppend(&str, pDb->zAuth, -1); 859e22a334bSdrh Tcl_DStringAppendElement(&str, zCode); 860e22a334bSdrh Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : ""); 861e22a334bSdrh Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); 862e22a334bSdrh Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); 863e22a334bSdrh Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); 864e22a334bSdrh rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); 865e22a334bSdrh Tcl_DStringFree(&str); 866e22a334bSdrh zReply = Tcl_GetStringResult(pDb->interp); 867e22a334bSdrh if( strcmp(zReply,"SQLITE_OK")==0 ){ 868e22a334bSdrh rc = SQLITE_OK; 869e22a334bSdrh }else if( strcmp(zReply,"SQLITE_DENY")==0 ){ 870e22a334bSdrh rc = SQLITE_DENY; 871e22a334bSdrh }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){ 872e22a334bSdrh rc = SQLITE_IGNORE; 873e22a334bSdrh }else{ 874e22a334bSdrh rc = 999; 875e22a334bSdrh } 876e22a334bSdrh return rc; 877e22a334bSdrh } 878e22a334bSdrh #endif /* SQLITE_OMIT_AUTHORIZATION */ 879cabb0819Sdrh 880cabb0819Sdrh /* 881ef2cb63eSdanielk1977 ** zText is a pointer to text obtained via an sqlite3_result_text() 882ef2cb63eSdanielk1977 ** or similar interface. This routine returns a Tcl string object, 883ef2cb63eSdanielk1977 ** reference count set to 0, containing the text. If a translation 884ef2cb63eSdanielk1977 ** between iso8859 and UTF-8 is required, it is preformed. 885ef2cb63eSdanielk1977 */ 886ef2cb63eSdanielk1977 static Tcl_Obj *dbTextToObj(char const *zText){ 887ef2cb63eSdanielk1977 Tcl_Obj *pVal; 888ef2cb63eSdanielk1977 #ifdef UTF_TRANSLATION_NEEDED 889ef2cb63eSdanielk1977 Tcl_DString dCol; 890ef2cb63eSdanielk1977 Tcl_DStringInit(&dCol); 891ef2cb63eSdanielk1977 Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol); 892ef2cb63eSdanielk1977 pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1); 893ef2cb63eSdanielk1977 Tcl_DStringFree(&dCol); 894ef2cb63eSdanielk1977 #else 895ef2cb63eSdanielk1977 pVal = Tcl_NewStringObj(zText, -1); 896ef2cb63eSdanielk1977 #endif 897ef2cb63eSdanielk1977 return pVal; 898ef2cb63eSdanielk1977 } 899ef2cb63eSdanielk1977 900ef2cb63eSdanielk1977 /* 9011067fe11Stpoindex ** This routine reads a line of text from FILE in, stores 9021067fe11Stpoindex ** the text in memory obtained from malloc() and returns a pointer 9031067fe11Stpoindex ** to the text. NULL is returned at end of file, or if malloc() 9041067fe11Stpoindex ** fails. 9051067fe11Stpoindex ** 9061067fe11Stpoindex ** The interface is like "readline" but no command-line editing 9071067fe11Stpoindex ** is done. 9081067fe11Stpoindex ** 9091067fe11Stpoindex ** copied from shell.c from '.import' command 9101067fe11Stpoindex */ 9111067fe11Stpoindex static char *local_getline(char *zPrompt, FILE *in){ 9121067fe11Stpoindex char *zLine; 9131067fe11Stpoindex int nLine; 9141067fe11Stpoindex int n; 9151067fe11Stpoindex int eol; 9161067fe11Stpoindex 9171067fe11Stpoindex nLine = 100; 9181067fe11Stpoindex zLine = malloc( nLine ); 9191067fe11Stpoindex if( zLine==0 ) return 0; 9201067fe11Stpoindex n = 0; 9211067fe11Stpoindex eol = 0; 9221067fe11Stpoindex while( !eol ){ 9231067fe11Stpoindex if( n+100>nLine ){ 9241067fe11Stpoindex nLine = nLine*2 + 100; 9251067fe11Stpoindex zLine = realloc(zLine, nLine); 9261067fe11Stpoindex if( zLine==0 ) return 0; 9271067fe11Stpoindex } 9281067fe11Stpoindex if( fgets(&zLine[n], nLine - n, in)==0 ){ 9291067fe11Stpoindex if( n==0 ){ 9301067fe11Stpoindex free(zLine); 9311067fe11Stpoindex return 0; 9321067fe11Stpoindex } 9331067fe11Stpoindex zLine[n] = 0; 9341067fe11Stpoindex eol = 1; 9351067fe11Stpoindex break; 9361067fe11Stpoindex } 9371067fe11Stpoindex while( zLine[n] ){ n++; } 9381067fe11Stpoindex if( n>0 && zLine[n-1]=='\n' ){ 9391067fe11Stpoindex n--; 9401067fe11Stpoindex zLine[n] = 0; 9411067fe11Stpoindex eol = 1; 9421067fe11Stpoindex } 9431067fe11Stpoindex } 9441067fe11Stpoindex zLine = realloc( zLine, n+1 ); 9451067fe11Stpoindex return zLine; 9461067fe11Stpoindex } 9471067fe11Stpoindex 9488e556520Sdanielk1977 9498e556520Sdanielk1977 /* 9504a4c11aaSdan ** This function is part of the implementation of the command: 9518e556520Sdanielk1977 ** 9524a4c11aaSdan ** $db transaction [-deferred|-immediate|-exclusive] SCRIPT 9538e556520Sdanielk1977 ** 9544a4c11aaSdan ** It is invoked after evaluating the script SCRIPT to commit or rollback 9554a4c11aaSdan ** the transaction or savepoint opened by the [transaction] command. 9564a4c11aaSdan */ 9574a4c11aaSdan static int DbTransPostCmd( 9584a4c11aaSdan ClientData data[], /* data[0] is the Sqlite3Db* for $db */ 9594a4c11aaSdan Tcl_Interp *interp, /* Tcl interpreter */ 9604a4c11aaSdan int result /* Result of evaluating SCRIPT */ 9614a4c11aaSdan ){ 9624a4c11aaSdan static const char *azEnd[] = { 9634a4c11aaSdan "RELEASE _tcl_transaction", /* rc==TCL_ERROR, nTransaction!=0 */ 9644a4c11aaSdan "COMMIT", /* rc!=TCL_ERROR, nTransaction==0 */ 9654a4c11aaSdan "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction", 9664a4c11aaSdan "ROLLBACK" /* rc==TCL_ERROR, nTransaction==0 */ 9674a4c11aaSdan }; 9684a4c11aaSdan SqliteDb *pDb = (SqliteDb*)data[0]; 9694a4c11aaSdan int rc = result; 9704a4c11aaSdan const char *zEnd; 9714a4c11aaSdan 9724a4c11aaSdan pDb->nTransaction--; 9734a4c11aaSdan zEnd = azEnd[(rc==TCL_ERROR)*2 + (pDb->nTransaction==0)]; 9744a4c11aaSdan 9754a4c11aaSdan pDb->disableAuth++; 9764a4c11aaSdan if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){ 9774a4c11aaSdan /* This is a tricky scenario to handle. The most likely cause of an 9784a4c11aaSdan ** error is that the exec() above was an attempt to commit the 9794a4c11aaSdan ** top-level transaction that returned SQLITE_BUSY. Or, less likely, 9804a4c11aaSdan ** that an IO-error has occured. In either case, throw a Tcl exception 9814a4c11aaSdan ** and try to rollback the transaction. 9824a4c11aaSdan ** 9834a4c11aaSdan ** But it could also be that the user executed one or more BEGIN, 9844a4c11aaSdan ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing 9854a4c11aaSdan ** this method's logic. Not clear how this would be best handled. 9864a4c11aaSdan */ 9874a4c11aaSdan if( rc!=TCL_ERROR ){ 9884a4c11aaSdan Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); 9894a4c11aaSdan rc = TCL_ERROR; 9904a4c11aaSdan } 9914a4c11aaSdan sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0); 9924a4c11aaSdan } 9934a4c11aaSdan pDb->disableAuth--; 9944a4c11aaSdan 9954a4c11aaSdan return rc; 9964a4c11aaSdan } 9974a4c11aaSdan 9984a4c11aaSdan /* 9994a4c11aaSdan ** Search the cache for a prepared-statement object that implements the 10004a4c11aaSdan ** first SQL statement in the buffer pointed to by parameter zIn. If 10014a4c11aaSdan ** no such prepared-statement can be found, allocate and prepare a new 10024a4c11aaSdan ** one. In either case, bind the current values of the relevant Tcl 10034a4c11aaSdan ** variables to any $var, :var or @var variables in the statement. Before 10044a4c11aaSdan ** returning, set *ppPreStmt to point to the prepared-statement object. 10054a4c11aaSdan ** 10064a4c11aaSdan ** Output parameter *pzOut is set to point to the next SQL statement in 10074a4c11aaSdan ** buffer zIn, or to the '\0' byte at the end of zIn if there is no 10084a4c11aaSdan ** next statement. 10094a4c11aaSdan ** 10104a4c11aaSdan ** If successful, TCL_OK is returned. Otherwise, TCL_ERROR is returned 10114a4c11aaSdan ** and an error message loaded into interpreter pDb->interp. 10124a4c11aaSdan */ 10134a4c11aaSdan static int dbPrepareAndBind( 10144a4c11aaSdan SqliteDb *pDb, /* Database object */ 10154a4c11aaSdan char const *zIn, /* SQL to compile */ 10164a4c11aaSdan char const **pzOut, /* OUT: Pointer to next SQL statement */ 10174a4c11aaSdan SqlPreparedStmt **ppPreStmt /* OUT: Object used to cache statement */ 10184a4c11aaSdan ){ 10194a4c11aaSdan const char *zSql = zIn; /* Pointer to first SQL statement in zIn */ 10204a4c11aaSdan sqlite3_stmt *pStmt; /* Prepared statement object */ 10214a4c11aaSdan SqlPreparedStmt *pPreStmt; /* Pointer to cached statement */ 10224a4c11aaSdan int nSql; /* Length of zSql in bytes */ 10234a4c11aaSdan int nVar; /* Number of variables in statement */ 10244a4c11aaSdan int iParm = 0; /* Next free entry in apParm */ 10254a4c11aaSdan int i; 10264a4c11aaSdan Tcl_Interp *interp = pDb->interp; 10274a4c11aaSdan 10284a4c11aaSdan *ppPreStmt = 0; 10294a4c11aaSdan 10304a4c11aaSdan /* Trim spaces from the start of zSql and calculate the remaining length. */ 10314a4c11aaSdan while( isspace(zSql[0]) ){ zSql++; } 10324a4c11aaSdan nSql = strlen30(zSql); 10334a4c11aaSdan 10344a4c11aaSdan for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){ 10354a4c11aaSdan int n = pPreStmt->nSql; 10364a4c11aaSdan if( nSql>=n 10374a4c11aaSdan && memcmp(pPreStmt->zSql, zSql, n)==0 10384a4c11aaSdan && (zSql[n]==0 || zSql[n-1]==';') 10394a4c11aaSdan ){ 10404a4c11aaSdan pStmt = pPreStmt->pStmt; 10414a4c11aaSdan *pzOut = &zSql[pPreStmt->nSql]; 10424a4c11aaSdan 10434a4c11aaSdan /* When a prepared statement is found, unlink it from the 10444a4c11aaSdan ** cache list. It will later be added back to the beginning 10454a4c11aaSdan ** of the cache list in order to implement LRU replacement. 10464a4c11aaSdan */ 10474a4c11aaSdan if( pPreStmt->pPrev ){ 10484a4c11aaSdan pPreStmt->pPrev->pNext = pPreStmt->pNext; 10494a4c11aaSdan }else{ 10504a4c11aaSdan pDb->stmtList = pPreStmt->pNext; 10514a4c11aaSdan } 10524a4c11aaSdan if( pPreStmt->pNext ){ 10534a4c11aaSdan pPreStmt->pNext->pPrev = pPreStmt->pPrev; 10544a4c11aaSdan }else{ 10554a4c11aaSdan pDb->stmtLast = pPreStmt->pPrev; 10564a4c11aaSdan } 10574a4c11aaSdan pDb->nStmt--; 10584a4c11aaSdan nVar = sqlite3_bind_parameter_count(pStmt); 10594a4c11aaSdan break; 10604a4c11aaSdan } 10614a4c11aaSdan } 10624a4c11aaSdan 10634a4c11aaSdan /* If no prepared statement was found. Compile the SQL text. Also allocate 10644a4c11aaSdan ** a new SqlPreparedStmt structure. */ 10654a4c11aaSdan if( pPreStmt==0 ){ 10664a4c11aaSdan int nByte; 10674a4c11aaSdan 10684a4c11aaSdan if( SQLITE_OK!=sqlite3_prepare_v2(pDb->db, zSql, -1, &pStmt, pzOut) ){ 10694a4c11aaSdan Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); 10704a4c11aaSdan return TCL_ERROR; 10714a4c11aaSdan } 10724a4c11aaSdan if( pStmt==0 ){ 10734a4c11aaSdan if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ 10744a4c11aaSdan /* A compile-time error in the statement. */ 10754a4c11aaSdan Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); 10764a4c11aaSdan return TCL_ERROR; 10774a4c11aaSdan }else{ 10784a4c11aaSdan /* The statement was a no-op. Continue to the next statement 10794a4c11aaSdan ** in the SQL string. 10804a4c11aaSdan */ 10814a4c11aaSdan return TCL_OK; 10824a4c11aaSdan } 10834a4c11aaSdan } 10844a4c11aaSdan 10854a4c11aaSdan assert( pPreStmt==0 ); 10864a4c11aaSdan nVar = sqlite3_bind_parameter_count(pStmt); 10874a4c11aaSdan nByte = sizeof(SqlPreparedStmt) + nVar*sizeof(Tcl_Obj *); 10884a4c11aaSdan pPreStmt = (SqlPreparedStmt*)Tcl_Alloc(nByte); 10894a4c11aaSdan memset(pPreStmt, 0, nByte); 10904a4c11aaSdan 10914a4c11aaSdan pPreStmt->pStmt = pStmt; 10924a4c11aaSdan pPreStmt->nSql = (*pzOut - zSql); 10934a4c11aaSdan pPreStmt->zSql = sqlite3_sql(pStmt); 10944a4c11aaSdan pPreStmt->apParm = (Tcl_Obj **)&pPreStmt[1]; 10954a4c11aaSdan } 10964a4c11aaSdan assert( pPreStmt ); 10974a4c11aaSdan assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql ); 10984a4c11aaSdan assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) ); 10994a4c11aaSdan 11004a4c11aaSdan /* Bind values to parameters that begin with $ or : */ 11014a4c11aaSdan for(i=1; i<=nVar; i++){ 11024a4c11aaSdan const char *zVar = sqlite3_bind_parameter_name(pStmt, i); 11034a4c11aaSdan if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){ 11044a4c11aaSdan Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); 11054a4c11aaSdan if( pVar ){ 11064a4c11aaSdan int n; 11074a4c11aaSdan u8 *data; 11084a4c11aaSdan const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); 11094a4c11aaSdan char c = zType[0]; 11104a4c11aaSdan if( zVar[0]=='@' || 11114a4c11aaSdan (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){ 11124a4c11aaSdan /* Load a BLOB type if the Tcl variable is a bytearray and 11134a4c11aaSdan ** it has no string representation or the host 11144a4c11aaSdan ** parameter name begins with "@". */ 11154a4c11aaSdan data = Tcl_GetByteArrayFromObj(pVar, &n); 11164a4c11aaSdan sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); 11174a4c11aaSdan Tcl_IncrRefCount(pVar); 11184a4c11aaSdan pPreStmt->apParm[iParm++] = pVar; 11194a4c11aaSdan }else if( c=='b' && strcmp(zType,"boolean")==0 ){ 11204a4c11aaSdan Tcl_GetIntFromObj(interp, pVar, &n); 11214a4c11aaSdan sqlite3_bind_int(pStmt, i, n); 11224a4c11aaSdan }else if( c=='d' && strcmp(zType,"double")==0 ){ 11234a4c11aaSdan double r; 11244a4c11aaSdan Tcl_GetDoubleFromObj(interp, pVar, &r); 11254a4c11aaSdan sqlite3_bind_double(pStmt, i, r); 11264a4c11aaSdan }else if( (c=='w' && strcmp(zType,"wideInt")==0) || 11274a4c11aaSdan (c=='i' && strcmp(zType,"int")==0) ){ 11284a4c11aaSdan Tcl_WideInt v; 11294a4c11aaSdan Tcl_GetWideIntFromObj(interp, pVar, &v); 11304a4c11aaSdan sqlite3_bind_int64(pStmt, i, v); 11314a4c11aaSdan }else{ 11324a4c11aaSdan data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); 11334a4c11aaSdan sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); 11344a4c11aaSdan Tcl_IncrRefCount(pVar); 11354a4c11aaSdan pPreStmt->apParm[iParm++] = pVar; 11364a4c11aaSdan } 11374a4c11aaSdan }else{ 11384a4c11aaSdan sqlite3_bind_null(pStmt, i); 11394a4c11aaSdan } 11404a4c11aaSdan } 11414a4c11aaSdan } 11424a4c11aaSdan pPreStmt->nParm = iParm; 11434a4c11aaSdan *ppPreStmt = pPreStmt; 1144937d0deaSdan 11454a4c11aaSdan return TCL_OK; 11464a4c11aaSdan } 11474a4c11aaSdan 11484a4c11aaSdan 11494a4c11aaSdan /* 11504a4c11aaSdan ** Release a statement reference obtained by calling dbPrepareAndBind(). 11514a4c11aaSdan ** There should be exactly one call to this function for each call to 11524a4c11aaSdan ** dbPrepareAndBind(). 11534a4c11aaSdan ** 11544a4c11aaSdan ** If the discard parameter is non-zero, then the statement is deleted 11554a4c11aaSdan ** immediately. Otherwise it is added to the LRU list and may be returned 11564a4c11aaSdan ** by a subsequent call to dbPrepareAndBind(). 11574a4c11aaSdan */ 11584a4c11aaSdan static void dbReleaseStmt( 11594a4c11aaSdan SqliteDb *pDb, /* Database handle */ 11604a4c11aaSdan SqlPreparedStmt *pPreStmt, /* Prepared statement handle to release */ 11614a4c11aaSdan int discard /* True to delete (not cache) the pPreStmt */ 11624a4c11aaSdan ){ 11634a4c11aaSdan int i; 11644a4c11aaSdan 11654a4c11aaSdan /* Free the bound string and blob parameters */ 11664a4c11aaSdan for(i=0; i<pPreStmt->nParm; i++){ 11674a4c11aaSdan Tcl_DecrRefCount(pPreStmt->apParm[i]); 11684a4c11aaSdan } 11694a4c11aaSdan pPreStmt->nParm = 0; 11704a4c11aaSdan 11714a4c11aaSdan if( pDb->maxStmt<=0 || discard ){ 11724a4c11aaSdan /* If the cache is turned off, deallocated the statement */ 11734a4c11aaSdan sqlite3_finalize(pPreStmt->pStmt); 11744a4c11aaSdan Tcl_Free((char *)pPreStmt); 11754a4c11aaSdan }else{ 11764a4c11aaSdan /* Add the prepared statement to the beginning of the cache list. */ 11774a4c11aaSdan pPreStmt->pNext = pDb->stmtList; 11784a4c11aaSdan pPreStmt->pPrev = 0; 11794a4c11aaSdan if( pDb->stmtList ){ 11804a4c11aaSdan pDb->stmtList->pPrev = pPreStmt; 11814a4c11aaSdan } 11824a4c11aaSdan pDb->stmtList = pPreStmt; 11834a4c11aaSdan if( pDb->stmtLast==0 ){ 11844a4c11aaSdan assert( pDb->nStmt==0 ); 11854a4c11aaSdan pDb->stmtLast = pPreStmt; 11864a4c11aaSdan }else{ 11874a4c11aaSdan assert( pDb->nStmt>0 ); 11884a4c11aaSdan } 11894a4c11aaSdan pDb->nStmt++; 11904a4c11aaSdan 11914a4c11aaSdan /* If we have too many statement in cache, remove the surplus from 11924a4c11aaSdan ** the end of the cache list. */ 11934a4c11aaSdan while( pDb->nStmt>pDb->maxStmt ){ 11944a4c11aaSdan sqlite3_finalize(pDb->stmtLast->pStmt); 11954a4c11aaSdan pDb->stmtLast = pDb->stmtLast->pPrev; 11964a4c11aaSdan Tcl_Free((char*)pDb->stmtLast->pNext); 11974a4c11aaSdan pDb->stmtLast->pNext = 0; 11984a4c11aaSdan pDb->nStmt--; 11994a4c11aaSdan } 12004a4c11aaSdan } 12014a4c11aaSdan } 12024a4c11aaSdan 12034a4c11aaSdan /* 12044a4c11aaSdan ** Structure used with dbEvalXXX() functions: 12054a4c11aaSdan ** 12064a4c11aaSdan ** dbEvalInit() 12074a4c11aaSdan ** dbEvalStep() 12084a4c11aaSdan ** dbEvalFinalize() 12094a4c11aaSdan ** dbEvalRowInfo() 12104a4c11aaSdan ** dbEvalColumnValue() 12114a4c11aaSdan */ 12124a4c11aaSdan typedef struct DbEvalContext DbEvalContext; 12134a4c11aaSdan struct DbEvalContext { 12144a4c11aaSdan SqliteDb *pDb; /* Database handle */ 12154a4c11aaSdan Tcl_Obj *pSql; /* Object holding string zSql */ 12164a4c11aaSdan const char *zSql; /* Remaining SQL to execute */ 12174a4c11aaSdan SqlPreparedStmt *pPreStmt; /* Current statement */ 12184a4c11aaSdan int nCol; /* Number of columns returned by pStmt */ 12194a4c11aaSdan Tcl_Obj *pArray; /* Name of array variable */ 12204a4c11aaSdan Tcl_Obj **apColName; /* Array of column names */ 12214a4c11aaSdan }; 12224a4c11aaSdan 12234a4c11aaSdan /* 12244a4c11aaSdan ** Release any cache of column names currently held as part of 12254a4c11aaSdan ** the DbEvalContext structure passed as the first argument. 12264a4c11aaSdan */ 12274a4c11aaSdan static void dbReleaseColumnNames(DbEvalContext *p){ 12284a4c11aaSdan if( p->apColName ){ 12294a4c11aaSdan int i; 12304a4c11aaSdan for(i=0; i<p->nCol; i++){ 12314a4c11aaSdan Tcl_DecrRefCount(p->apColName[i]); 12324a4c11aaSdan } 12334a4c11aaSdan Tcl_Free((char *)p->apColName); 12344a4c11aaSdan p->apColName = 0; 12354a4c11aaSdan } 12364a4c11aaSdan p->nCol = 0; 12374a4c11aaSdan } 12384a4c11aaSdan 12394a4c11aaSdan /* 12404a4c11aaSdan ** Initialize a DbEvalContext structure. 12418e556520Sdanielk1977 ** 12428e556520Sdanielk1977 ** If pArray is not NULL, then it contains the name of a Tcl array 12438e556520Sdanielk1977 ** variable. The "*" member of this array is set to a list containing 12444a4c11aaSdan ** the names of the columns returned by the statement as part of each 12454a4c11aaSdan ** call to dbEvalStep(), in order from left to right. e.g. if the names 12464a4c11aaSdan ** of the returned columns are a, b and c, it does the equivalent of the 12474a4c11aaSdan ** tcl command: 12488e556520Sdanielk1977 ** 12498e556520Sdanielk1977 ** set ${pArray}(*) {a b c} 12508e556520Sdanielk1977 */ 12514a4c11aaSdan static void dbEvalInit( 12524a4c11aaSdan DbEvalContext *p, /* Pointer to structure to initialize */ 12534a4c11aaSdan SqliteDb *pDb, /* Database handle */ 12544a4c11aaSdan Tcl_Obj *pSql, /* Object containing SQL script */ 12554a4c11aaSdan Tcl_Obj *pArray /* Name of Tcl array to set (*) element of */ 12568e556520Sdanielk1977 ){ 12574a4c11aaSdan memset(p, 0, sizeof(DbEvalContext)); 12584a4c11aaSdan p->pDb = pDb; 12594a4c11aaSdan p->zSql = Tcl_GetString(pSql); 12604a4c11aaSdan p->pSql = pSql; 12614a4c11aaSdan Tcl_IncrRefCount(pSql); 12624a4c11aaSdan if( pArray ){ 12634a4c11aaSdan p->pArray = pArray; 12644a4c11aaSdan Tcl_IncrRefCount(pArray); 12654a4c11aaSdan } 12664a4c11aaSdan } 12678e556520Sdanielk1977 12684a4c11aaSdan /* 12694a4c11aaSdan ** Obtain information about the row that the DbEvalContext passed as the 12704a4c11aaSdan ** first argument currently points to. 12714a4c11aaSdan */ 12724a4c11aaSdan static void dbEvalRowInfo( 12734a4c11aaSdan DbEvalContext *p, /* Evaluation context */ 12744a4c11aaSdan int *pnCol, /* OUT: Number of column names */ 12754a4c11aaSdan Tcl_Obj ***papColName /* OUT: Array of column names */ 12764a4c11aaSdan ){ 12778e556520Sdanielk1977 /* Compute column names */ 12784a4c11aaSdan if( 0==p->apColName ){ 12794a4c11aaSdan sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 12804a4c11aaSdan int i; /* Iterator variable */ 12814a4c11aaSdan int nCol; /* Number of columns returned by pStmt */ 12824a4c11aaSdan Tcl_Obj **apColName = 0; /* Array of column names */ 12834a4c11aaSdan 12844a4c11aaSdan p->nCol = nCol = sqlite3_column_count(pStmt); 12854a4c11aaSdan if( nCol>0 && (papColName || p->pArray) ){ 12864a4c11aaSdan apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); 12878e556520Sdanielk1977 for(i=0; i<nCol; i++){ 12888e556520Sdanielk1977 apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i)); 12898e556520Sdanielk1977 Tcl_IncrRefCount(apColName[i]); 12908e556520Sdanielk1977 } 12914a4c11aaSdan p->apColName = apColName; 12924a4c11aaSdan } 12938e556520Sdanielk1977 12948e556520Sdanielk1977 /* If results are being stored in an array variable, then create 12958e556520Sdanielk1977 ** the array(*) entry for that array 12968e556520Sdanielk1977 */ 12974a4c11aaSdan if( p->pArray ){ 12984a4c11aaSdan Tcl_Interp *interp = p->pDb->interp; 12998e556520Sdanielk1977 Tcl_Obj *pColList = Tcl_NewObj(); 13008e556520Sdanielk1977 Tcl_Obj *pStar = Tcl_NewStringObj("*", -1); 13014a4c11aaSdan 13028e556520Sdanielk1977 for(i=0; i<nCol; i++){ 13038e556520Sdanielk1977 Tcl_ListObjAppendElement(interp, pColList, apColName[i]); 13048e556520Sdanielk1977 } 13058e556520Sdanielk1977 Tcl_IncrRefCount(pStar); 13064a4c11aaSdan Tcl_ObjSetVar2(interp, p->pArray, pStar, pColList, 0); 13078e556520Sdanielk1977 Tcl_DecrRefCount(pStar); 13088e556520Sdanielk1977 } 13098e556520Sdanielk1977 } 13108e556520Sdanielk1977 13114a4c11aaSdan if( papColName ){ 13124a4c11aaSdan *papColName = p->apColName; 13134a4c11aaSdan } 13144a4c11aaSdan if( pnCol ){ 13154a4c11aaSdan *pnCol = p->nCol; 13164a4c11aaSdan } 13174a4c11aaSdan } 13184a4c11aaSdan 13194a4c11aaSdan /* 13204a4c11aaSdan ** Return one of TCL_OK, TCL_BREAK or TCL_ERROR. If TCL_ERROR is 13214a4c11aaSdan ** returned, then an error message is stored in the interpreter before 13224a4c11aaSdan ** returning. 13234a4c11aaSdan ** 13244a4c11aaSdan ** A return value of TCL_OK means there is a row of data available. The 13254a4c11aaSdan ** data may be accessed using dbEvalRowInfo() and dbEvalColumnValue(). This 13264a4c11aaSdan ** is analogous to a return of SQLITE_ROW from sqlite3_step(). If TCL_BREAK 13274a4c11aaSdan ** is returned, then the SQL script has finished executing and there are 13284a4c11aaSdan ** no further rows available. This is similar to SQLITE_DONE. 13294a4c11aaSdan */ 13304a4c11aaSdan static int dbEvalStep(DbEvalContext *p){ 13314a4c11aaSdan while( p->zSql[0] || p->pPreStmt ){ 13324a4c11aaSdan int rc; 13334a4c11aaSdan if( p->pPreStmt==0 ){ 13344a4c11aaSdan rc = dbPrepareAndBind(p->pDb, p->zSql, &p->zSql, &p->pPreStmt); 13354a4c11aaSdan if( rc!=TCL_OK ) return rc; 13364a4c11aaSdan }else{ 13374a4c11aaSdan int rcs; 13384a4c11aaSdan SqliteDb *pDb = p->pDb; 13394a4c11aaSdan SqlPreparedStmt *pPreStmt = p->pPreStmt; 13404a4c11aaSdan sqlite3_stmt *pStmt = pPreStmt->pStmt; 13414a4c11aaSdan 13424a4c11aaSdan rcs = sqlite3_step(pStmt); 13434a4c11aaSdan if( rcs==SQLITE_ROW ){ 13444a4c11aaSdan return TCL_OK; 13454a4c11aaSdan } 13464a4c11aaSdan if( p->pArray ){ 13474a4c11aaSdan dbEvalRowInfo(p, 0, 0); 13484a4c11aaSdan } 13494a4c11aaSdan rcs = sqlite3_reset(pStmt); 13504a4c11aaSdan 13514a4c11aaSdan pDb->nStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_FULLSCAN_STEP,1); 13524a4c11aaSdan pDb->nSort = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_SORT,1); 13534a4c11aaSdan dbReleaseColumnNames(p); 13544a4c11aaSdan p->pPreStmt = 0; 13554a4c11aaSdan 13564a4c11aaSdan if( rcs!=SQLITE_OK ){ 13574a4c11aaSdan /* If a run-time error occurs, report the error and stop reading 13584a4c11aaSdan ** the SQL. */ 13594a4c11aaSdan Tcl_SetObjResult(pDb->interp, dbTextToObj(sqlite3_errmsg(pDb->db))); 13604a4c11aaSdan dbReleaseStmt(pDb, pPreStmt, 1); 13614a4c11aaSdan return TCL_ERROR; 13624a4c11aaSdan }else{ 13634a4c11aaSdan dbReleaseStmt(pDb, pPreStmt, 0); 13644a4c11aaSdan } 13654a4c11aaSdan } 13664a4c11aaSdan } 13674a4c11aaSdan 13684a4c11aaSdan /* Finished */ 13694a4c11aaSdan return TCL_BREAK; 13704a4c11aaSdan } 13714a4c11aaSdan 13724a4c11aaSdan /* 13734a4c11aaSdan ** Free all resources currently held by the DbEvalContext structure passed 13744a4c11aaSdan ** as the first argument. There should be exactly one call to this function 13754a4c11aaSdan ** for each call to dbEvalInit(). 13764a4c11aaSdan */ 13774a4c11aaSdan static void dbEvalFinalize(DbEvalContext *p){ 13784a4c11aaSdan if( p->pPreStmt ){ 13794a4c11aaSdan sqlite3_reset(p->pPreStmt->pStmt); 13804a4c11aaSdan dbReleaseStmt(p->pDb, p->pPreStmt, 0); 13814a4c11aaSdan p->pPreStmt = 0; 13824a4c11aaSdan } 13834a4c11aaSdan if( p->pArray ){ 13844a4c11aaSdan Tcl_DecrRefCount(p->pArray); 13854a4c11aaSdan p->pArray = 0; 13864a4c11aaSdan } 13874a4c11aaSdan Tcl_DecrRefCount(p->pSql); 13884a4c11aaSdan dbReleaseColumnNames(p); 13894a4c11aaSdan } 13904a4c11aaSdan 13914a4c11aaSdan /* 13924a4c11aaSdan ** Return a pointer to a Tcl_Obj structure with ref-count 0 that contains 13934a4c11aaSdan ** the value for the iCol'th column of the row currently pointed to by 13944a4c11aaSdan ** the DbEvalContext structure passed as the first argument. 13954a4c11aaSdan */ 13964a4c11aaSdan static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){ 13974a4c11aaSdan sqlite3_stmt *pStmt = p->pPreStmt->pStmt; 13984a4c11aaSdan switch( sqlite3_column_type(pStmt, iCol) ){ 13994a4c11aaSdan case SQLITE_BLOB: { 14004a4c11aaSdan int bytes = sqlite3_column_bytes(pStmt, iCol); 14014a4c11aaSdan const char *zBlob = sqlite3_column_blob(pStmt, iCol); 14024a4c11aaSdan if( !zBlob ) bytes = 0; 14034a4c11aaSdan return Tcl_NewByteArrayObj((u8*)zBlob, bytes); 14044a4c11aaSdan } 14054a4c11aaSdan case SQLITE_INTEGER: { 14064a4c11aaSdan sqlite_int64 v = sqlite3_column_int64(pStmt, iCol); 14074a4c11aaSdan if( v>=-2147483647 && v<=2147483647 ){ 14084a4c11aaSdan return Tcl_NewIntObj(v); 14094a4c11aaSdan }else{ 14104a4c11aaSdan return Tcl_NewWideIntObj(v); 14114a4c11aaSdan } 14124a4c11aaSdan } 14134a4c11aaSdan case SQLITE_FLOAT: { 14144a4c11aaSdan return Tcl_NewDoubleObj(sqlite3_column_double(pStmt, iCol)); 14154a4c11aaSdan } 14164a4c11aaSdan case SQLITE_NULL: { 14174a4c11aaSdan return dbTextToObj(p->pDb->zNull); 14184a4c11aaSdan } 14194a4c11aaSdan } 14204a4c11aaSdan 14214a4c11aaSdan return dbTextToObj((char *)sqlite3_column_text(pStmt, iCol)); 14224a4c11aaSdan } 14234a4c11aaSdan 14244a4c11aaSdan /* 14254a4c11aaSdan ** If using Tcl version 8.6 or greater, use the NR functions to avoid 14264a4c11aaSdan ** recursive evalution of scripts by the [db eval] and [db trans] 14274a4c11aaSdan ** commands. Even if the headers used while compiling the extension 14284a4c11aaSdan ** are 8.6 or newer, the code still tests the Tcl version at runtime. 14294a4c11aaSdan ** This allows stubs-enabled builds to be used with older Tcl libraries. 14304a4c11aaSdan */ 14314a4c11aaSdan #if TCL_MAJOR_VERSION>8 || (TCL_MAJOR_VERSION==8 && TCL_MINOR_VERSION>=6) 1432a2c8a95bSdrh # define SQLITE_TCL_NRE 1 14334a4c11aaSdan static int DbUseNre(void){ 14344a4c11aaSdan int major, minor; 14354a4c11aaSdan Tcl_GetVersion(&major, &minor, 0, 0); 14364a4c11aaSdan return( (major==8 && minor>=6) || major>8 ); 14374a4c11aaSdan } 14384a4c11aaSdan #else 14394a4c11aaSdan /* 14404a4c11aaSdan ** Compiling using headers earlier than 8.6. In this case NR cannot be 14414a4c11aaSdan ** used, so DbUseNre() to always return zero. Add #defines for the other 14424a4c11aaSdan ** Tcl_NRxxx() functions to prevent them from causing compilation errors, 14434a4c11aaSdan ** even though the only invocations of them are within conditional blocks 14444a4c11aaSdan ** of the form: 14454a4c11aaSdan ** 14464a4c11aaSdan ** if( DbUseNre() ) { ... } 14474a4c11aaSdan */ 1448a2c8a95bSdrh # define SQLITE_TCL_NRE 0 14494a4c11aaSdan # define DbUseNre() 0 14504a4c11aaSdan # define Tcl_NRAddCallback(a,b,c,d,e,f) 0 14514a4c11aaSdan # define Tcl_NREvalObj(a,b,c) 0 14524a4c11aaSdan # define Tcl_NRCreateCommand(a,b,c,d,e,f) 0 14534a4c11aaSdan #endif 14544a4c11aaSdan 14554a4c11aaSdan /* 14564a4c11aaSdan ** This function is part of the implementation of the command: 14574a4c11aaSdan ** 14584a4c11aaSdan ** $db eval SQL ?ARRAYNAME? SCRIPT 14594a4c11aaSdan */ 14604a4c11aaSdan static int DbEvalNextCmd( 14614a4c11aaSdan ClientData data[], /* data[0] is the (DbEvalContext*) */ 14624a4c11aaSdan Tcl_Interp *interp, /* Tcl interpreter */ 14634a4c11aaSdan int result /* Result so far */ 14644a4c11aaSdan ){ 14654a4c11aaSdan int rc = result; /* Return code */ 14664a4c11aaSdan 14674a4c11aaSdan /* The first element of the data[] array is a pointer to a DbEvalContext 14684a4c11aaSdan ** structure allocated using Tcl_Alloc(). The second element of data[] 14694a4c11aaSdan ** is a pointer to a Tcl_Obj containing the script to run for each row 14704a4c11aaSdan ** returned by the queries encapsulated in data[0]. */ 14714a4c11aaSdan DbEvalContext *p = (DbEvalContext *)data[0]; 14724a4c11aaSdan Tcl_Obj *pScript = (Tcl_Obj *)data[1]; 14734a4c11aaSdan Tcl_Obj *pArray = p->pArray; 14744a4c11aaSdan 14754a4c11aaSdan while( (rc==TCL_OK || rc==TCL_CONTINUE) && TCL_OK==(rc = dbEvalStep(p)) ){ 14764a4c11aaSdan int i; 14774a4c11aaSdan int nCol; 14784a4c11aaSdan Tcl_Obj **apColName; 14794a4c11aaSdan dbEvalRowInfo(p, &nCol, &apColName); 14804a4c11aaSdan for(i=0; i<nCol; i++){ 14814a4c11aaSdan Tcl_Obj *pVal = dbEvalColumnValue(p, i); 14824a4c11aaSdan if( pArray==0 ){ 14834a4c11aaSdan Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0); 14844a4c11aaSdan }else{ 14854a4c11aaSdan Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); 14864a4c11aaSdan } 14874a4c11aaSdan } 14884a4c11aaSdan 14894a4c11aaSdan /* The required interpreter variables are now populated with the data 14904a4c11aaSdan ** from the current row. If using NRE, schedule callbacks to evaluate 14914a4c11aaSdan ** script pScript, then to invoke this function again to fetch the next 14924a4c11aaSdan ** row (or clean up if there is no next row or the script throws an 14934a4c11aaSdan ** exception). After scheduling the callbacks, return control to the 14944a4c11aaSdan ** caller. 14954a4c11aaSdan ** 14964a4c11aaSdan ** If not using NRE, evaluate pScript directly and continue with the 14974a4c11aaSdan ** next iteration of this while(...) loop. */ 14984a4c11aaSdan if( DbUseNre() ){ 14994a4c11aaSdan Tcl_NRAddCallback(interp, DbEvalNextCmd, (void*)p, (void*)pScript, 0, 0); 15004a4c11aaSdan return Tcl_NREvalObj(interp, pScript, 0); 15014a4c11aaSdan }else{ 15024a4c11aaSdan rc = Tcl_EvalObjEx(interp, pScript, 0); 15034a4c11aaSdan } 15044a4c11aaSdan } 15054a4c11aaSdan 15064a4c11aaSdan Tcl_DecrRefCount(pScript); 15074a4c11aaSdan dbEvalFinalize(p); 15084a4c11aaSdan Tcl_Free((char *)p); 15094a4c11aaSdan 15104a4c11aaSdan if( rc==TCL_OK || rc==TCL_BREAK ){ 15114a4c11aaSdan Tcl_ResetResult(interp); 15124a4c11aaSdan rc = TCL_OK; 15134a4c11aaSdan } 15144a4c11aaSdan return rc; 15158e556520Sdanielk1977 } 15168e556520Sdanielk1977 15171067fe11Stpoindex /* 151875897234Sdrh ** The "sqlite" command below creates a new Tcl command for each 151975897234Sdrh ** connection it opens to an SQLite database. This routine is invoked 152075897234Sdrh ** whenever one of those connection-specific commands is executed 152175897234Sdrh ** in Tcl. For example, if you run Tcl code like this: 152275897234Sdrh ** 15239bb575fdSdrh ** sqlite3 db1 "my_database" 152475897234Sdrh ** db1 close 152575897234Sdrh ** 152675897234Sdrh ** The first command opens a connection to the "my_database" database 152775897234Sdrh ** and calls that connection "db1". The second command causes this 152875897234Sdrh ** subroutine to be invoked. 152975897234Sdrh */ 15306d31316cSdrh static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ 1531bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 15326d31316cSdrh int choice; 153322fbcb8dSdrh int rc = TCL_OK; 15340de8c112Sdrh static const char *DB_strs[] = { 1535dc2c4915Sdrh "authorizer", "backup", "busy", 1536dc2c4915Sdrh "cache", "changes", "close", 1537dc2c4915Sdrh "collate", "collation_needed", "commit_hook", 1538dc2c4915Sdrh "complete", "copy", "enable_load_extension", 1539dc2c4915Sdrh "errorcode", "eval", "exists", 1540dc2c4915Sdrh "function", "incrblob", "interrupt", 1541dc2c4915Sdrh "last_insert_rowid", "nullvalue", "onecolumn", 1542dc2c4915Sdrh "profile", "progress", "rekey", 1543dc2c4915Sdrh "restore", "rollback_hook", "status", 1544dc2c4915Sdrh "timeout", "total_changes", "trace", 1545404ca075Sdanielk1977 "transaction", "unlock_notify", "update_hook", 1546404ca075Sdanielk1977 "version", 0 15476d31316cSdrh }; 1548411995dcSdrh enum DB_enum { 1549dc2c4915Sdrh DB_AUTHORIZER, DB_BACKUP, DB_BUSY, 1550dc2c4915Sdrh DB_CACHE, DB_CHANGES, DB_CLOSE, 1551dc2c4915Sdrh DB_COLLATE, DB_COLLATION_NEEDED, DB_COMMIT_HOOK, 1552dc2c4915Sdrh DB_COMPLETE, DB_COPY, DB_ENABLE_LOAD_EXTENSION, 1553dc2c4915Sdrh DB_ERRORCODE, DB_EVAL, DB_EXISTS, 1554dc2c4915Sdrh DB_FUNCTION, DB_INCRBLOB, DB_INTERRUPT, 1555dc2c4915Sdrh DB_LAST_INSERT_ROWID, DB_NULLVALUE, DB_ONECOLUMN, 1556dc2c4915Sdrh DB_PROFILE, DB_PROGRESS, DB_REKEY, 1557dc2c4915Sdrh DB_RESTORE, DB_ROLLBACK_HOOK, DB_STATUS, 1558dc2c4915Sdrh DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE, 1559404ca075Sdanielk1977 DB_TRANSACTION, DB_UNLOCK_NOTIFY, DB_UPDATE_HOOK, 1560404ca075Sdanielk1977 DB_VERSION, 15616d31316cSdrh }; 15621067fe11Stpoindex /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */ 15636d31316cSdrh 15646d31316cSdrh if( objc<2 ){ 15656d31316cSdrh Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); 156675897234Sdrh return TCL_ERROR; 156775897234Sdrh } 1568411995dcSdrh if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){ 15696d31316cSdrh return TCL_ERROR; 15706d31316cSdrh } 15716d31316cSdrh 1572411995dcSdrh switch( (enum DB_enum)choice ){ 157375897234Sdrh 1574e22a334bSdrh /* $db authorizer ?CALLBACK? 1575e22a334bSdrh ** 1576e22a334bSdrh ** Invoke the given callback to authorize each SQL operation as it is 1577e22a334bSdrh ** compiled. 5 arguments are appended to the callback before it is 1578e22a334bSdrh ** invoked: 1579e22a334bSdrh ** 1580e22a334bSdrh ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...) 1581e22a334bSdrh ** (2) First descriptive name (depends on authorization type) 1582e22a334bSdrh ** (3) Second descriptive name 1583e22a334bSdrh ** (4) Name of the database (ex: "main", "temp") 1584e22a334bSdrh ** (5) Name of trigger that is doing the access 1585e22a334bSdrh ** 1586e22a334bSdrh ** The callback should return on of the following strings: SQLITE_OK, 1587e22a334bSdrh ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. 1588e22a334bSdrh ** 1589e22a334bSdrh ** If this method is invoked with no arguments, the current authorization 1590e22a334bSdrh ** callback string is returned. 1591e22a334bSdrh */ 1592e22a334bSdrh case DB_AUTHORIZER: { 15931211de37Sdrh #ifdef SQLITE_OMIT_AUTHORIZATION 15941211de37Sdrh Tcl_AppendResult(interp, "authorization not available in this build", 0); 15951211de37Sdrh return TCL_ERROR; 15961211de37Sdrh #else 1597e22a334bSdrh if( objc>3 ){ 1598e22a334bSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 15990f14e2ebSdrh return TCL_ERROR; 1600e22a334bSdrh }else if( objc==2 ){ 1601b5a20d3cSdrh if( pDb->zAuth ){ 1602e22a334bSdrh Tcl_AppendResult(interp, pDb->zAuth, 0); 1603e22a334bSdrh } 1604e22a334bSdrh }else{ 1605e22a334bSdrh char *zAuth; 1606e22a334bSdrh int len; 1607e22a334bSdrh if( pDb->zAuth ){ 1608e22a334bSdrh Tcl_Free(pDb->zAuth); 1609e22a334bSdrh } 1610e22a334bSdrh zAuth = Tcl_GetStringFromObj(objv[2], &len); 1611e22a334bSdrh if( zAuth && len>0 ){ 1612e22a334bSdrh pDb->zAuth = Tcl_Alloc( len + 1 ); 16135bb3eb9bSdrh memcpy(pDb->zAuth, zAuth, len+1); 1614e22a334bSdrh }else{ 1615e22a334bSdrh pDb->zAuth = 0; 1616e22a334bSdrh } 1617e22a334bSdrh if( pDb->zAuth ){ 1618e22a334bSdrh pDb->interp = interp; 16196f8a503dSdanielk1977 sqlite3_set_authorizer(pDb->db, auth_callback, pDb); 1620e22a334bSdrh }else{ 16216f8a503dSdanielk1977 sqlite3_set_authorizer(pDb->db, 0, 0); 1622e22a334bSdrh } 1623e22a334bSdrh } 16241211de37Sdrh #endif 1625e22a334bSdrh break; 1626e22a334bSdrh } 1627e22a334bSdrh 1628dc2c4915Sdrh /* $db backup ?DATABASE? FILENAME 1629dc2c4915Sdrh ** 1630dc2c4915Sdrh ** Open or create a database file named FILENAME. Transfer the 1631dc2c4915Sdrh ** content of local database DATABASE (default: "main") into the 1632dc2c4915Sdrh ** FILENAME database. 1633dc2c4915Sdrh */ 1634dc2c4915Sdrh case DB_BACKUP: { 1635dc2c4915Sdrh const char *zDestFile; 1636dc2c4915Sdrh const char *zSrcDb; 1637dc2c4915Sdrh sqlite3 *pDest; 1638dc2c4915Sdrh sqlite3_backup *pBackup; 1639dc2c4915Sdrh 1640dc2c4915Sdrh if( objc==3 ){ 1641dc2c4915Sdrh zSrcDb = "main"; 1642dc2c4915Sdrh zDestFile = Tcl_GetString(objv[2]); 1643dc2c4915Sdrh }else if( objc==4 ){ 1644dc2c4915Sdrh zSrcDb = Tcl_GetString(objv[2]); 1645dc2c4915Sdrh zDestFile = Tcl_GetString(objv[3]); 1646dc2c4915Sdrh }else{ 1647dc2c4915Sdrh Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 1648dc2c4915Sdrh return TCL_ERROR; 1649dc2c4915Sdrh } 1650dc2c4915Sdrh rc = sqlite3_open(zDestFile, &pDest); 1651dc2c4915Sdrh if( rc!=SQLITE_OK ){ 1652dc2c4915Sdrh Tcl_AppendResult(interp, "cannot open target database: ", 1653dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1654dc2c4915Sdrh sqlite3_close(pDest); 1655dc2c4915Sdrh return TCL_ERROR; 1656dc2c4915Sdrh } 1657dc2c4915Sdrh pBackup = sqlite3_backup_init(pDest, "main", pDb->db, zSrcDb); 1658dc2c4915Sdrh if( pBackup==0 ){ 1659dc2c4915Sdrh Tcl_AppendResult(interp, "backup failed: ", 1660dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1661dc2c4915Sdrh sqlite3_close(pDest); 1662dc2c4915Sdrh return TCL_ERROR; 1663dc2c4915Sdrh } 1664dc2c4915Sdrh while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} 1665dc2c4915Sdrh sqlite3_backup_finish(pBackup); 1666dc2c4915Sdrh if( rc==SQLITE_DONE ){ 1667dc2c4915Sdrh rc = TCL_OK; 1668dc2c4915Sdrh }else{ 1669dc2c4915Sdrh Tcl_AppendResult(interp, "backup failed: ", 1670dc2c4915Sdrh sqlite3_errmsg(pDest), (char*)0); 1671dc2c4915Sdrh rc = TCL_ERROR; 1672dc2c4915Sdrh } 1673dc2c4915Sdrh sqlite3_close(pDest); 1674dc2c4915Sdrh break; 1675dc2c4915Sdrh } 1676dc2c4915Sdrh 1677bec3f402Sdrh /* $db busy ?CALLBACK? 1678bec3f402Sdrh ** 1679bec3f402Sdrh ** Invoke the given callback if an SQL statement attempts to open 1680bec3f402Sdrh ** a locked database file. 1681bec3f402Sdrh */ 16826d31316cSdrh case DB_BUSY: { 16836d31316cSdrh if( objc>3 ){ 16846d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK"); 1685bec3f402Sdrh return TCL_ERROR; 16866d31316cSdrh }else if( objc==2 ){ 1687bec3f402Sdrh if( pDb->zBusy ){ 1688bec3f402Sdrh Tcl_AppendResult(interp, pDb->zBusy, 0); 1689bec3f402Sdrh } 1690bec3f402Sdrh }else{ 16916d31316cSdrh char *zBusy; 16926d31316cSdrh int len; 1693bec3f402Sdrh if( pDb->zBusy ){ 1694bec3f402Sdrh Tcl_Free(pDb->zBusy); 16956d31316cSdrh } 16966d31316cSdrh zBusy = Tcl_GetStringFromObj(objv[2], &len); 16976d31316cSdrh if( zBusy && len>0 ){ 16986d31316cSdrh pDb->zBusy = Tcl_Alloc( len + 1 ); 16995bb3eb9bSdrh memcpy(pDb->zBusy, zBusy, len+1); 17006d31316cSdrh }else{ 1701bec3f402Sdrh pDb->zBusy = 0; 1702bec3f402Sdrh } 1703bec3f402Sdrh if( pDb->zBusy ){ 1704bec3f402Sdrh pDb->interp = interp; 17056f8a503dSdanielk1977 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb); 17066d31316cSdrh }else{ 17076f8a503dSdanielk1977 sqlite3_busy_handler(pDb->db, 0, 0); 1708bec3f402Sdrh } 1709bec3f402Sdrh } 17106d31316cSdrh break; 17116d31316cSdrh } 1712bec3f402Sdrh 1713fb7e7651Sdrh /* $db cache flush 1714fb7e7651Sdrh ** $db cache size n 1715fb7e7651Sdrh ** 1716fb7e7651Sdrh ** Flush the prepared statement cache, or set the maximum number of 1717fb7e7651Sdrh ** cached statements. 1718fb7e7651Sdrh */ 1719fb7e7651Sdrh case DB_CACHE: { 1720fb7e7651Sdrh char *subCmd; 1721fb7e7651Sdrh int n; 1722fb7e7651Sdrh 1723fb7e7651Sdrh if( objc<=2 ){ 1724fb7e7651Sdrh Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?"); 1725fb7e7651Sdrh return TCL_ERROR; 1726fb7e7651Sdrh } 1727fb7e7651Sdrh subCmd = Tcl_GetStringFromObj( objv[2], 0 ); 1728fb7e7651Sdrh if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){ 1729fb7e7651Sdrh if( objc!=3 ){ 1730fb7e7651Sdrh Tcl_WrongNumArgs(interp, 2, objv, "flush"); 1731fb7e7651Sdrh return TCL_ERROR; 1732fb7e7651Sdrh }else{ 1733fb7e7651Sdrh flushStmtCache( pDb ); 1734fb7e7651Sdrh } 1735fb7e7651Sdrh }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){ 1736fb7e7651Sdrh if( objc!=4 ){ 1737fb7e7651Sdrh Tcl_WrongNumArgs(interp, 2, objv, "size n"); 1738fb7e7651Sdrh return TCL_ERROR; 1739fb7e7651Sdrh }else{ 1740fb7e7651Sdrh if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){ 1741fb7e7651Sdrh Tcl_AppendResult( interp, "cannot convert \"", 1742fb7e7651Sdrh Tcl_GetStringFromObj(objv[3],0), "\" to integer", 0); 1743fb7e7651Sdrh return TCL_ERROR; 1744fb7e7651Sdrh }else{ 1745fb7e7651Sdrh if( n<0 ){ 1746fb7e7651Sdrh flushStmtCache( pDb ); 1747fb7e7651Sdrh n = 0; 1748fb7e7651Sdrh }else if( n>MAX_PREPARED_STMTS ){ 1749fb7e7651Sdrh n = MAX_PREPARED_STMTS; 1750fb7e7651Sdrh } 1751fb7e7651Sdrh pDb->maxStmt = n; 1752fb7e7651Sdrh } 1753fb7e7651Sdrh } 1754fb7e7651Sdrh }else{ 1755fb7e7651Sdrh Tcl_AppendResult( interp, "bad option \"", 1756191fadcfSdanielk1977 Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size", 0); 1757fb7e7651Sdrh return TCL_ERROR; 1758fb7e7651Sdrh } 1759fb7e7651Sdrh break; 1760fb7e7651Sdrh } 1761fb7e7651Sdrh 1762b28af71aSdanielk1977 /* $db changes 1763c8d30ac1Sdrh ** 1764c8d30ac1Sdrh ** Return the number of rows that were modified, inserted, or deleted by 1765b28af71aSdanielk1977 ** the most recent INSERT, UPDATE or DELETE statement, not including 1766b28af71aSdanielk1977 ** any changes made by trigger programs. 1767c8d30ac1Sdrh */ 1768c8d30ac1Sdrh case DB_CHANGES: { 1769c8d30ac1Sdrh Tcl_Obj *pResult; 1770c8d30ac1Sdrh if( objc!=2 ){ 1771c8d30ac1Sdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 1772c8d30ac1Sdrh return TCL_ERROR; 1773c8d30ac1Sdrh } 1774c8d30ac1Sdrh pResult = Tcl_GetObjResult(interp); 1775b28af71aSdanielk1977 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db)); 1776f146a776Srdc break; 1777f146a776Srdc } 1778f146a776Srdc 177975897234Sdrh /* $db close 178075897234Sdrh ** 178175897234Sdrh ** Shutdown the database 178275897234Sdrh */ 17836d31316cSdrh case DB_CLOSE: { 17846d31316cSdrh Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); 17856d31316cSdrh break; 17866d31316cSdrh } 178775897234Sdrh 17880f14e2ebSdrh /* 17890f14e2ebSdrh ** $db collate NAME SCRIPT 17900f14e2ebSdrh ** 17910f14e2ebSdrh ** Create a new SQL collation function called NAME. Whenever 17920f14e2ebSdrh ** that function is called, invoke SCRIPT to evaluate the function. 17930f14e2ebSdrh */ 17940f14e2ebSdrh case DB_COLLATE: { 17950f14e2ebSdrh SqlCollate *pCollate; 17960f14e2ebSdrh char *zName; 17970f14e2ebSdrh char *zScript; 17980f14e2ebSdrh int nScript; 17990f14e2ebSdrh if( objc!=4 ){ 18000f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); 18010f14e2ebSdrh return TCL_ERROR; 18020f14e2ebSdrh } 18030f14e2ebSdrh zName = Tcl_GetStringFromObj(objv[2], 0); 18040f14e2ebSdrh zScript = Tcl_GetStringFromObj(objv[3], &nScript); 18050f14e2ebSdrh pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 ); 18060f14e2ebSdrh if( pCollate==0 ) return TCL_ERROR; 18070f14e2ebSdrh pCollate->interp = interp; 18080f14e2ebSdrh pCollate->pNext = pDb->pCollate; 18090f14e2ebSdrh pCollate->zScript = (char*)&pCollate[1]; 18100f14e2ebSdrh pDb->pCollate = pCollate; 18115bb3eb9bSdrh memcpy(pCollate->zScript, zScript, nScript+1); 18120f14e2ebSdrh if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, 18130f14e2ebSdrh pCollate, tclSqlCollate) ){ 18149636c4e1Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 18150f14e2ebSdrh return TCL_ERROR; 18160f14e2ebSdrh } 18170f14e2ebSdrh break; 18180f14e2ebSdrh } 18190f14e2ebSdrh 18200f14e2ebSdrh /* 18210f14e2ebSdrh ** $db collation_needed SCRIPT 18220f14e2ebSdrh ** 18230f14e2ebSdrh ** Create a new SQL collation function called NAME. Whenever 18240f14e2ebSdrh ** that function is called, invoke SCRIPT to evaluate the function. 18250f14e2ebSdrh */ 18260f14e2ebSdrh case DB_COLLATION_NEEDED: { 18270f14e2ebSdrh if( objc!=3 ){ 18280f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT"); 18290f14e2ebSdrh return TCL_ERROR; 18300f14e2ebSdrh } 18310f14e2ebSdrh if( pDb->pCollateNeeded ){ 18320f14e2ebSdrh Tcl_DecrRefCount(pDb->pCollateNeeded); 18330f14e2ebSdrh } 18340f14e2ebSdrh pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]); 18350f14e2ebSdrh Tcl_IncrRefCount(pDb->pCollateNeeded); 18360f14e2ebSdrh sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded); 18370f14e2ebSdrh break; 18380f14e2ebSdrh } 18390f14e2ebSdrh 184019e2d37fSdrh /* $db commit_hook ?CALLBACK? 184119e2d37fSdrh ** 184219e2d37fSdrh ** Invoke the given callback just before committing every SQL transaction. 184319e2d37fSdrh ** If the callback throws an exception or returns non-zero, then the 184419e2d37fSdrh ** transaction is aborted. If CALLBACK is an empty string, the callback 184519e2d37fSdrh ** is disabled. 184619e2d37fSdrh */ 184719e2d37fSdrh case DB_COMMIT_HOOK: { 184819e2d37fSdrh if( objc>3 ){ 184919e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 185019e2d37fSdrh return TCL_ERROR; 185119e2d37fSdrh }else if( objc==2 ){ 185219e2d37fSdrh if( pDb->zCommit ){ 185319e2d37fSdrh Tcl_AppendResult(interp, pDb->zCommit, 0); 185419e2d37fSdrh } 185519e2d37fSdrh }else{ 185619e2d37fSdrh char *zCommit; 185719e2d37fSdrh int len; 185819e2d37fSdrh if( pDb->zCommit ){ 185919e2d37fSdrh Tcl_Free(pDb->zCommit); 186019e2d37fSdrh } 186119e2d37fSdrh zCommit = Tcl_GetStringFromObj(objv[2], &len); 186219e2d37fSdrh if( zCommit && len>0 ){ 186319e2d37fSdrh pDb->zCommit = Tcl_Alloc( len + 1 ); 18645bb3eb9bSdrh memcpy(pDb->zCommit, zCommit, len+1); 186519e2d37fSdrh }else{ 186619e2d37fSdrh pDb->zCommit = 0; 186719e2d37fSdrh } 186819e2d37fSdrh if( pDb->zCommit ){ 186919e2d37fSdrh pDb->interp = interp; 187019e2d37fSdrh sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb); 187119e2d37fSdrh }else{ 187219e2d37fSdrh sqlite3_commit_hook(pDb->db, 0, 0); 187319e2d37fSdrh } 187419e2d37fSdrh } 187519e2d37fSdrh break; 187619e2d37fSdrh } 187719e2d37fSdrh 187875897234Sdrh /* $db complete SQL 187975897234Sdrh ** 188075897234Sdrh ** Return TRUE if SQL is a complete SQL statement. Return FALSE if 188175897234Sdrh ** additional lines of input are needed. This is similar to the 188275897234Sdrh ** built-in "info complete" command of Tcl. 188375897234Sdrh */ 18846d31316cSdrh case DB_COMPLETE: { 1885ccae6026Sdrh #ifndef SQLITE_OMIT_COMPLETE 18866d31316cSdrh Tcl_Obj *pResult; 18876d31316cSdrh int isComplete; 18886d31316cSdrh if( objc!=3 ){ 18896d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 189075897234Sdrh return TCL_ERROR; 189175897234Sdrh } 18926f8a503dSdanielk1977 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) ); 18936d31316cSdrh pResult = Tcl_GetObjResult(interp); 18946d31316cSdrh Tcl_SetBooleanObj(pResult, isComplete); 1895ccae6026Sdrh #endif 18966d31316cSdrh break; 18976d31316cSdrh } 189875897234Sdrh 189919e2d37fSdrh /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR? 190019e2d37fSdrh ** 190119e2d37fSdrh ** Copy data into table from filename, optionally using SEPARATOR 190219e2d37fSdrh ** as column separators. If a column contains a null string, or the 190319e2d37fSdrh ** value of NULLINDICATOR, a NULL is inserted for the column. 190419e2d37fSdrh ** conflict-algorithm is one of the sqlite conflict algorithms: 190519e2d37fSdrh ** rollback, abort, fail, ignore, replace 190619e2d37fSdrh ** On success, return the number of lines processed, not necessarily same 190719e2d37fSdrh ** as 'db changes' due to conflict-algorithm selected. 190819e2d37fSdrh ** 190919e2d37fSdrh ** This code is basically an implementation/enhancement of 191019e2d37fSdrh ** the sqlite3 shell.c ".import" command. 191119e2d37fSdrh ** 191219e2d37fSdrh ** This command usage is equivalent to the sqlite2.x COPY statement, 191319e2d37fSdrh ** which imports file data into a table using the PostgreSQL COPY file format: 191419e2d37fSdrh ** $db copy $conflit_algo $table_name $filename \t \\N 191519e2d37fSdrh */ 191619e2d37fSdrh case DB_COPY: { 191719e2d37fSdrh char *zTable; /* Insert data into this table */ 191819e2d37fSdrh char *zFile; /* The file from which to extract data */ 191919e2d37fSdrh char *zConflict; /* The conflict algorithm to use */ 192019e2d37fSdrh sqlite3_stmt *pStmt; /* A statement */ 192119e2d37fSdrh int nCol; /* Number of columns in the table */ 192219e2d37fSdrh int nByte; /* Number of bytes in an SQL string */ 192319e2d37fSdrh int i, j; /* Loop counters */ 192419e2d37fSdrh int nSep; /* Number of bytes in zSep[] */ 192519e2d37fSdrh int nNull; /* Number of bytes in zNull[] */ 192619e2d37fSdrh char *zSql; /* An SQL statement */ 192719e2d37fSdrh char *zLine; /* A single line of input from the file */ 192819e2d37fSdrh char **azCol; /* zLine[] broken up into columns */ 192919e2d37fSdrh char *zCommit; /* How to commit changes */ 193019e2d37fSdrh FILE *in; /* The input file */ 193119e2d37fSdrh int lineno = 0; /* Line number of input file */ 193219e2d37fSdrh char zLineNum[80]; /* Line number print buffer */ 193319e2d37fSdrh Tcl_Obj *pResult; /* interp result */ 193419e2d37fSdrh 193519e2d37fSdrh char *zSep; 193619e2d37fSdrh char *zNull; 193719e2d37fSdrh if( objc<5 || objc>7 ){ 193819e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, 193919e2d37fSdrh "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"); 194019e2d37fSdrh return TCL_ERROR; 194119e2d37fSdrh } 194219e2d37fSdrh if( objc>=6 ){ 194319e2d37fSdrh zSep = Tcl_GetStringFromObj(objv[5], 0); 194419e2d37fSdrh }else{ 194519e2d37fSdrh zSep = "\t"; 194619e2d37fSdrh } 194719e2d37fSdrh if( objc>=7 ){ 194819e2d37fSdrh zNull = Tcl_GetStringFromObj(objv[6], 0); 194919e2d37fSdrh }else{ 195019e2d37fSdrh zNull = ""; 195119e2d37fSdrh } 195219e2d37fSdrh zConflict = Tcl_GetStringFromObj(objv[2], 0); 195319e2d37fSdrh zTable = Tcl_GetStringFromObj(objv[3], 0); 195419e2d37fSdrh zFile = Tcl_GetStringFromObj(objv[4], 0); 19554f21c4afSdrh nSep = strlen30(zSep); 19564f21c4afSdrh nNull = strlen30(zNull); 195719e2d37fSdrh if( nSep==0 ){ 195819e2d37fSdrh Tcl_AppendResult(interp,"Error: non-null separator required for copy",0); 195919e2d37fSdrh return TCL_ERROR; 196019e2d37fSdrh } 19613e59c012Sdrh if(strcmp(zConflict, "rollback") != 0 && 19623e59c012Sdrh strcmp(zConflict, "abort" ) != 0 && 19633e59c012Sdrh strcmp(zConflict, "fail" ) != 0 && 19643e59c012Sdrh strcmp(zConflict, "ignore" ) != 0 && 19653e59c012Sdrh strcmp(zConflict, "replace" ) != 0 ) { 196619e2d37fSdrh Tcl_AppendResult(interp, "Error: \"", zConflict, 196719e2d37fSdrh "\", conflict-algorithm must be one of: rollback, " 196819e2d37fSdrh "abort, fail, ignore, or replace", 0); 196919e2d37fSdrh return TCL_ERROR; 197019e2d37fSdrh } 197119e2d37fSdrh zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); 197219e2d37fSdrh if( zSql==0 ){ 197319e2d37fSdrh Tcl_AppendResult(interp, "Error: no such table: ", zTable, 0); 197419e2d37fSdrh return TCL_ERROR; 197519e2d37fSdrh } 19764f21c4afSdrh nByte = strlen30(zSql); 19773e701a18Sdrh rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 197819e2d37fSdrh sqlite3_free(zSql); 197919e2d37fSdrh if( rc ){ 198019e2d37fSdrh Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0); 198119e2d37fSdrh nCol = 0; 198219e2d37fSdrh }else{ 198319e2d37fSdrh nCol = sqlite3_column_count(pStmt); 198419e2d37fSdrh } 198519e2d37fSdrh sqlite3_finalize(pStmt); 198619e2d37fSdrh if( nCol==0 ) { 198719e2d37fSdrh return TCL_ERROR; 198819e2d37fSdrh } 198919e2d37fSdrh zSql = malloc( nByte + 50 + nCol*2 ); 199019e2d37fSdrh if( zSql==0 ) { 199119e2d37fSdrh Tcl_AppendResult(interp, "Error: can't malloc()", 0); 199219e2d37fSdrh return TCL_ERROR; 199319e2d37fSdrh } 199419e2d37fSdrh sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?", 199519e2d37fSdrh zConflict, zTable); 19964f21c4afSdrh j = strlen30(zSql); 199719e2d37fSdrh for(i=1; i<nCol; i++){ 199819e2d37fSdrh zSql[j++] = ','; 199919e2d37fSdrh zSql[j++] = '?'; 200019e2d37fSdrh } 200119e2d37fSdrh zSql[j++] = ')'; 200219e2d37fSdrh zSql[j] = 0; 20033e701a18Sdrh rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0); 200419e2d37fSdrh free(zSql); 200519e2d37fSdrh if( rc ){ 200619e2d37fSdrh Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0); 200719e2d37fSdrh sqlite3_finalize(pStmt); 200819e2d37fSdrh return TCL_ERROR; 200919e2d37fSdrh } 201019e2d37fSdrh in = fopen(zFile, "rb"); 201119e2d37fSdrh if( in==0 ){ 201219e2d37fSdrh Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL); 201319e2d37fSdrh sqlite3_finalize(pStmt); 201419e2d37fSdrh return TCL_ERROR; 201519e2d37fSdrh } 201619e2d37fSdrh azCol = malloc( sizeof(azCol[0])*(nCol+1) ); 201719e2d37fSdrh if( azCol==0 ) { 201819e2d37fSdrh Tcl_AppendResult(interp, "Error: can't malloc()", 0); 201943617e9aSdrh fclose(in); 202019e2d37fSdrh return TCL_ERROR; 202119e2d37fSdrh } 20223752785fSdrh (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0); 202319e2d37fSdrh zCommit = "COMMIT"; 202419e2d37fSdrh while( (zLine = local_getline(0, in))!=0 ){ 202519e2d37fSdrh char *z; 202619e2d37fSdrh i = 0; 202719e2d37fSdrh lineno++; 202819e2d37fSdrh azCol[0] = zLine; 202919e2d37fSdrh for(i=0, z=zLine; *z; z++){ 203019e2d37fSdrh if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){ 203119e2d37fSdrh *z = 0; 203219e2d37fSdrh i++; 203319e2d37fSdrh if( i<nCol ){ 203419e2d37fSdrh azCol[i] = &z[nSep]; 203519e2d37fSdrh z += nSep-1; 203619e2d37fSdrh } 203719e2d37fSdrh } 203819e2d37fSdrh } 203919e2d37fSdrh if( i+1!=nCol ){ 204019e2d37fSdrh char *zErr; 20414f21c4afSdrh int nErr = strlen30(zFile) + 200; 20425bb3eb9bSdrh zErr = malloc(nErr); 2043c1f4494eSdrh if( zErr ){ 20445bb3eb9bSdrh sqlite3_snprintf(nErr, zErr, 2045955de52cSdanielk1977 "Error: %s line %d: expected %d columns of data but found %d", 204619e2d37fSdrh zFile, lineno, nCol, i+1); 204719e2d37fSdrh Tcl_AppendResult(interp, zErr, 0); 204819e2d37fSdrh free(zErr); 2049c1f4494eSdrh } 205019e2d37fSdrh zCommit = "ROLLBACK"; 205119e2d37fSdrh break; 205219e2d37fSdrh } 205319e2d37fSdrh for(i=0; i<nCol; i++){ 205419e2d37fSdrh /* check for null data, if so, bind as null */ 2055ea678832Sdrh if( (nNull>0 && strcmp(azCol[i], zNull)==0) 20564f21c4afSdrh || strlen30(azCol[i])==0 2057ea678832Sdrh ){ 205819e2d37fSdrh sqlite3_bind_null(pStmt, i+1); 205919e2d37fSdrh }else{ 206019e2d37fSdrh sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); 206119e2d37fSdrh } 206219e2d37fSdrh } 206319e2d37fSdrh sqlite3_step(pStmt); 206419e2d37fSdrh rc = sqlite3_reset(pStmt); 206519e2d37fSdrh free(zLine); 206619e2d37fSdrh if( rc!=SQLITE_OK ){ 206719e2d37fSdrh Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), 0); 206819e2d37fSdrh zCommit = "ROLLBACK"; 206919e2d37fSdrh break; 207019e2d37fSdrh } 207119e2d37fSdrh } 207219e2d37fSdrh free(azCol); 207319e2d37fSdrh fclose(in); 207419e2d37fSdrh sqlite3_finalize(pStmt); 20753752785fSdrh (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0); 207619e2d37fSdrh 207719e2d37fSdrh if( zCommit[0] == 'C' ){ 207819e2d37fSdrh /* success, set result as number of lines processed */ 207919e2d37fSdrh pResult = Tcl_GetObjResult(interp); 208019e2d37fSdrh Tcl_SetIntObj(pResult, lineno); 208119e2d37fSdrh rc = TCL_OK; 208219e2d37fSdrh }else{ 208319e2d37fSdrh /* failure, append lineno where failed */ 20845bb3eb9bSdrh sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno); 208519e2d37fSdrh Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,0); 208619e2d37fSdrh rc = TCL_ERROR; 208719e2d37fSdrh } 208819e2d37fSdrh break; 208919e2d37fSdrh } 209019e2d37fSdrh 209175897234Sdrh /* 20924144905bSdrh ** $db enable_load_extension BOOLEAN 20934144905bSdrh ** 20944144905bSdrh ** Turn the extension loading feature on or off. It if off by 20954144905bSdrh ** default. 20964144905bSdrh */ 20974144905bSdrh case DB_ENABLE_LOAD_EXTENSION: { 2098f533acc0Sdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 20994144905bSdrh int onoff; 21004144905bSdrh if( objc!=3 ){ 21014144905bSdrh Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN"); 21024144905bSdrh return TCL_ERROR; 21034144905bSdrh } 21044144905bSdrh if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ 21054144905bSdrh return TCL_ERROR; 21064144905bSdrh } 21074144905bSdrh sqlite3_enable_load_extension(pDb->db, onoff); 21084144905bSdrh break; 2109f533acc0Sdrh #else 2110f533acc0Sdrh Tcl_AppendResult(interp, "extension loading is turned off at compile-time", 2111f533acc0Sdrh 0); 2112f533acc0Sdrh return TCL_ERROR; 2113f533acc0Sdrh #endif 21144144905bSdrh } 21154144905bSdrh 21164144905bSdrh /* 2117dcd997eaSdrh ** $db errorcode 2118dcd997eaSdrh ** 2119dcd997eaSdrh ** Return the numeric error code that was returned by the most recent 21206f8a503dSdanielk1977 ** call to sqlite3_exec(). 2121dcd997eaSdrh */ 2122dcd997eaSdrh case DB_ERRORCODE: { 2123f3ce83f5Sdanielk1977 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); 2124dcd997eaSdrh break; 2125dcd997eaSdrh } 2126dcd997eaSdrh 2127dcd997eaSdrh /* 21284a4c11aaSdan ** $db exists $sql 21291807ce37Sdrh ** $db onecolumn $sql 213075897234Sdrh ** 21314a4c11aaSdan ** The onecolumn method is the equivalent of: 21324a4c11aaSdan ** lindex [$db eval $sql] 0 21334a4c11aaSdan */ 21344a4c11aaSdan case DB_EXISTS: 21354a4c11aaSdan case DB_ONECOLUMN: { 21364a4c11aaSdan DbEvalContext sEval; 21374a4c11aaSdan if( objc!=3 ){ 21384a4c11aaSdan Tcl_WrongNumArgs(interp, 2, objv, "SQL"); 21394a4c11aaSdan return TCL_ERROR; 21404a4c11aaSdan } 21414a4c11aaSdan 21424a4c11aaSdan dbEvalInit(&sEval, pDb, objv[2], 0); 21434a4c11aaSdan rc = dbEvalStep(&sEval); 21444a4c11aaSdan if( choice==DB_ONECOLUMN ){ 21454a4c11aaSdan if( rc==TCL_OK ){ 21464a4c11aaSdan Tcl_SetObjResult(interp, dbEvalColumnValue(&sEval, 0)); 21474a4c11aaSdan } 21484a4c11aaSdan }else if( rc==TCL_BREAK || rc==TCL_OK ){ 21494a4c11aaSdan Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc==TCL_OK)); 21504a4c11aaSdan } 21514a4c11aaSdan dbEvalFinalize(&sEval); 21524a4c11aaSdan 21534a4c11aaSdan if( rc==TCL_BREAK ){ 21544a4c11aaSdan rc = TCL_OK; 21554a4c11aaSdan } 21564a4c11aaSdan break; 21574a4c11aaSdan } 21584a4c11aaSdan 21594a4c11aaSdan /* 21604a4c11aaSdan ** $db eval $sql ?array? ?{ ...code... }? 21614a4c11aaSdan ** 216275897234Sdrh ** The SQL statement in $sql is evaluated. For each row, the values are 2163bec3f402Sdrh ** placed in elements of the array named "array" and ...code... is executed. 216475897234Sdrh ** If "array" and "code" are omitted, then no callback is every invoked. 216575897234Sdrh ** If "array" is an empty string, then the values are placed in variables 216675897234Sdrh ** that have the same name as the fields extracted by the query. 216775897234Sdrh */ 21684a4c11aaSdan case DB_EVAL: { 216992febd92Sdrh if( objc<3 || objc>5 ){ 2170895d7472Sdrh Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?"); 217130ccda10Sdanielk1977 return TCL_ERROR; 217230ccda10Sdanielk1977 } 21734a4c11aaSdan 217492febd92Sdrh if( objc==3 ){ 21754a4c11aaSdan DbEvalContext sEval; 21764a4c11aaSdan Tcl_Obj *pRet = Tcl_NewObj(); 21774a4c11aaSdan Tcl_IncrRefCount(pRet); 21784a4c11aaSdan dbEvalInit(&sEval, pDb, objv[2], 0); 21794a4c11aaSdan while( TCL_OK==(rc = dbEvalStep(&sEval)) ){ 21804a4c11aaSdan int i; 21814a4c11aaSdan int nCol; 21824a4c11aaSdan dbEvalRowInfo(&sEval, &nCol, 0); 218392febd92Sdrh for(i=0; i<nCol; i++){ 21844a4c11aaSdan Tcl_ListObjAppendElement(interp, pRet, dbEvalColumnValue(&sEval, i)); 218592febd92Sdrh } 218630ccda10Sdanielk1977 } 21874a4c11aaSdan dbEvalFinalize(&sEval); 218890b6bb19Sdrh if( rc==TCL_BREAK ){ 21894a4c11aaSdan Tcl_SetObjResult(interp, pRet); 219090b6bb19Sdrh rc = TCL_OK; 219190b6bb19Sdrh } 2192ef2cb63eSdanielk1977 Tcl_DecrRefCount(pRet); 21934a4c11aaSdan }else{ 21944a4c11aaSdan ClientData cd[2]; 21954a4c11aaSdan DbEvalContext *p; 21964a4c11aaSdan Tcl_Obj *pArray = 0; 21974a4c11aaSdan Tcl_Obj *pScript; 21984a4c11aaSdan 21994a4c11aaSdan if( objc==5 && *(char *)Tcl_GetString(objv[3]) ){ 22004a4c11aaSdan pArray = objv[3]; 22014a4c11aaSdan } 22024a4c11aaSdan pScript = objv[objc-1]; 22034a4c11aaSdan Tcl_IncrRefCount(pScript); 22044a4c11aaSdan 22054a4c11aaSdan p = (DbEvalContext *)Tcl_Alloc(sizeof(DbEvalContext)); 22064a4c11aaSdan dbEvalInit(p, pDb, objv[2], pArray); 22074a4c11aaSdan 22084a4c11aaSdan cd[0] = (void *)p; 22094a4c11aaSdan cd[1] = (void *)pScript; 22104a4c11aaSdan rc = DbEvalNextCmd(cd, interp, TCL_OK); 22111807ce37Sdrh } 221230ccda10Sdanielk1977 break; 221330ccda10Sdanielk1977 } 2214bec3f402Sdrh 2215bec3f402Sdrh /* 2216e3602be8Sdrh ** $db function NAME [-argcount N] SCRIPT 2217cabb0819Sdrh ** 2218cabb0819Sdrh ** Create a new SQL function called NAME. Whenever that function is 2219cabb0819Sdrh ** called, invoke SCRIPT to evaluate the function. 2220cabb0819Sdrh */ 2221cabb0819Sdrh case DB_FUNCTION: { 2222cabb0819Sdrh SqlFunc *pFunc; 2223d1e4733dSdrh Tcl_Obj *pScript; 2224cabb0819Sdrh char *zName; 2225e3602be8Sdrh int nArg = -1; 2226e3602be8Sdrh if( objc==6 ){ 2227e3602be8Sdrh const char *z = Tcl_GetString(objv[3]); 22284f21c4afSdrh int n = strlen30(z); 2229e3602be8Sdrh if( n>2 && strncmp(z, "-argcount",n)==0 ){ 2230e3602be8Sdrh if( Tcl_GetIntFromObj(interp, objv[4], &nArg) ) return TCL_ERROR; 2231e3602be8Sdrh if( nArg<0 ){ 2232e3602be8Sdrh Tcl_AppendResult(interp, "number of arguments must be non-negative", 2233e3602be8Sdrh (char*)0); 2234cabb0819Sdrh return TCL_ERROR; 2235cabb0819Sdrh } 2236e3602be8Sdrh } 2237e3602be8Sdrh pScript = objv[5]; 2238e3602be8Sdrh }else if( objc!=4 ){ 2239e3602be8Sdrh Tcl_WrongNumArgs(interp, 2, objv, "NAME [-argcount N] SCRIPT"); 2240e3602be8Sdrh return TCL_ERROR; 2241e3602be8Sdrh }else{ 2242d1e4733dSdrh pScript = objv[3]; 2243e3602be8Sdrh } 2244e3602be8Sdrh zName = Tcl_GetStringFromObj(objv[2], 0); 2245d1e4733dSdrh pFunc = findSqlFunc(pDb, zName); 2246cabb0819Sdrh if( pFunc==0 ) return TCL_ERROR; 2247d1e4733dSdrh if( pFunc->pScript ){ 2248d1e4733dSdrh Tcl_DecrRefCount(pFunc->pScript); 2249d1e4733dSdrh } 2250d1e4733dSdrh pFunc->pScript = pScript; 2251d1e4733dSdrh Tcl_IncrRefCount(pScript); 2252d1e4733dSdrh pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript); 2253e3602be8Sdrh rc = sqlite3_create_function(pDb->db, zName, nArg, SQLITE_UTF8, 2254d8123366Sdanielk1977 pFunc, tclSqlFunc, 0, 0); 2255fb7e7651Sdrh if( rc!=SQLITE_OK ){ 2256fb7e7651Sdrh rc = TCL_ERROR; 22579636c4e1Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); 2258fb7e7651Sdrh } 2259cabb0819Sdrh break; 2260cabb0819Sdrh } 2261cabb0819Sdrh 2262cabb0819Sdrh /* 22638cbadb02Sdanielk1977 ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID 2264b4e9af9fSdanielk1977 */ 2265b4e9af9fSdanielk1977 case DB_INCRBLOB: { 226632a0d8bbSdanielk1977 #ifdef SQLITE_OMIT_INCRBLOB 226732a0d8bbSdanielk1977 Tcl_AppendResult(interp, "incrblob not available in this build", 0); 226832a0d8bbSdanielk1977 return TCL_ERROR; 226932a0d8bbSdanielk1977 #else 22708cbadb02Sdanielk1977 int isReadonly = 0; 2271b4e9af9fSdanielk1977 const char *zDb = "main"; 2272b4e9af9fSdanielk1977 const char *zTable; 2273b4e9af9fSdanielk1977 const char *zColumn; 2274b4e9af9fSdanielk1977 sqlite_int64 iRow; 2275b4e9af9fSdanielk1977 22768cbadb02Sdanielk1977 /* Check for the -readonly option */ 22778cbadb02Sdanielk1977 if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){ 22788cbadb02Sdanielk1977 isReadonly = 1; 22798cbadb02Sdanielk1977 } 22808cbadb02Sdanielk1977 22818cbadb02Sdanielk1977 if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){ 22828cbadb02Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID"); 2283b4e9af9fSdanielk1977 return TCL_ERROR; 2284b4e9af9fSdanielk1977 } 2285b4e9af9fSdanielk1977 22868cbadb02Sdanielk1977 if( objc==(6+isReadonly) ){ 2287b4e9af9fSdanielk1977 zDb = Tcl_GetString(objv[2]); 2288b4e9af9fSdanielk1977 } 2289b4e9af9fSdanielk1977 zTable = Tcl_GetString(objv[objc-3]); 2290b4e9af9fSdanielk1977 zColumn = Tcl_GetString(objv[objc-2]); 2291b4e9af9fSdanielk1977 rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow); 2292b4e9af9fSdanielk1977 2293b4e9af9fSdanielk1977 if( rc==TCL_OK ){ 22948cbadb02Sdanielk1977 rc = createIncrblobChannel( 22958cbadb02Sdanielk1977 interp, pDb, zDb, zTable, zColumn, iRow, isReadonly 22968cbadb02Sdanielk1977 ); 2297b4e9af9fSdanielk1977 } 229832a0d8bbSdanielk1977 #endif 2299b4e9af9fSdanielk1977 break; 2300b4e9af9fSdanielk1977 } 2301b4e9af9fSdanielk1977 2302b4e9af9fSdanielk1977 /* 2303f11bded5Sdrh ** $db interrupt 2304f11bded5Sdrh ** 2305f11bded5Sdrh ** Interrupt the execution of the inner-most SQL interpreter. This 2306f11bded5Sdrh ** causes the SQL statement to return an error of SQLITE_INTERRUPT. 2307f11bded5Sdrh */ 2308f11bded5Sdrh case DB_INTERRUPT: { 2309f11bded5Sdrh sqlite3_interrupt(pDb->db); 2310f11bded5Sdrh break; 2311f11bded5Sdrh } 2312f11bded5Sdrh 2313f11bded5Sdrh /* 231419e2d37fSdrh ** $db nullvalue ?STRING? 231519e2d37fSdrh ** 231619e2d37fSdrh ** Change text used when a NULL comes back from the database. If ?STRING? 231719e2d37fSdrh ** is not present, then the current string used for NULL is returned. 231819e2d37fSdrh ** If STRING is present, then STRING is returned. 231919e2d37fSdrh ** 232019e2d37fSdrh */ 232119e2d37fSdrh case DB_NULLVALUE: { 232219e2d37fSdrh if( objc!=2 && objc!=3 ){ 232319e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE"); 232419e2d37fSdrh return TCL_ERROR; 232519e2d37fSdrh } 232619e2d37fSdrh if( objc==3 ){ 232719e2d37fSdrh int len; 232819e2d37fSdrh char *zNull = Tcl_GetStringFromObj(objv[2], &len); 232919e2d37fSdrh if( pDb->zNull ){ 233019e2d37fSdrh Tcl_Free(pDb->zNull); 233119e2d37fSdrh } 233219e2d37fSdrh if( zNull && len>0 ){ 233319e2d37fSdrh pDb->zNull = Tcl_Alloc( len + 1 ); 233419e2d37fSdrh strncpy(pDb->zNull, zNull, len); 233519e2d37fSdrh pDb->zNull[len] = '\0'; 233619e2d37fSdrh }else{ 233719e2d37fSdrh pDb->zNull = 0; 233819e2d37fSdrh } 233919e2d37fSdrh } 234019e2d37fSdrh Tcl_SetObjResult(interp, dbTextToObj(pDb->zNull)); 234119e2d37fSdrh break; 234219e2d37fSdrh } 234319e2d37fSdrh 234419e2d37fSdrh /* 2345af9ff33aSdrh ** $db last_insert_rowid 2346af9ff33aSdrh ** 2347af9ff33aSdrh ** Return an integer which is the ROWID for the most recent insert. 2348af9ff33aSdrh */ 2349af9ff33aSdrh case DB_LAST_INSERT_ROWID: { 2350af9ff33aSdrh Tcl_Obj *pResult; 2351f7e678d6Sdrh Tcl_WideInt rowid; 2352af9ff33aSdrh if( objc!=2 ){ 2353af9ff33aSdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 2354af9ff33aSdrh return TCL_ERROR; 2355af9ff33aSdrh } 23566f8a503dSdanielk1977 rowid = sqlite3_last_insert_rowid(pDb->db); 2357af9ff33aSdrh pResult = Tcl_GetObjResult(interp); 2358f7e678d6Sdrh Tcl_SetWideIntObj(pResult, rowid); 2359af9ff33aSdrh break; 2360af9ff33aSdrh } 2361af9ff33aSdrh 2362af9ff33aSdrh /* 23634a4c11aaSdan ** The DB_ONECOLUMN method is implemented together with DB_EXISTS. 23645d9d7576Sdrh */ 23651807ce37Sdrh 23661807ce37Sdrh /* $db progress ?N CALLBACK? 23671807ce37Sdrh ** 23681807ce37Sdrh ** Invoke the given callback every N virtual machine opcodes while executing 23691807ce37Sdrh ** queries. 23701807ce37Sdrh */ 23711807ce37Sdrh case DB_PROGRESS: { 23721807ce37Sdrh if( objc==2 ){ 23731807ce37Sdrh if( pDb->zProgress ){ 23741807ce37Sdrh Tcl_AppendResult(interp, pDb->zProgress, 0); 23755d9d7576Sdrh } 23761807ce37Sdrh }else if( objc==4 ){ 23771807ce37Sdrh char *zProgress; 23781807ce37Sdrh int len; 23791807ce37Sdrh int N; 23801807ce37Sdrh if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ 23811807ce37Sdrh return TCL_ERROR; 23821807ce37Sdrh }; 23831807ce37Sdrh if( pDb->zProgress ){ 23841807ce37Sdrh Tcl_Free(pDb->zProgress); 23851807ce37Sdrh } 23861807ce37Sdrh zProgress = Tcl_GetStringFromObj(objv[3], &len); 23871807ce37Sdrh if( zProgress && len>0 ){ 23881807ce37Sdrh pDb->zProgress = Tcl_Alloc( len + 1 ); 23895bb3eb9bSdrh memcpy(pDb->zProgress, zProgress, len+1); 23901807ce37Sdrh }else{ 23911807ce37Sdrh pDb->zProgress = 0; 23921807ce37Sdrh } 23931807ce37Sdrh #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 23941807ce37Sdrh if( pDb->zProgress ){ 23951807ce37Sdrh pDb->interp = interp; 23961807ce37Sdrh sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb); 23971807ce37Sdrh }else{ 23981807ce37Sdrh sqlite3_progress_handler(pDb->db, 0, 0, 0); 23991807ce37Sdrh } 24001807ce37Sdrh #endif 24011807ce37Sdrh }else{ 24021807ce37Sdrh Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK"); 24031807ce37Sdrh return TCL_ERROR; 24045d9d7576Sdrh } 24055d9d7576Sdrh break; 24065d9d7576Sdrh } 24075d9d7576Sdrh 240819e2d37fSdrh /* $db profile ?CALLBACK? 240919e2d37fSdrh ** 241019e2d37fSdrh ** Make arrangements to invoke the CALLBACK routine after each SQL statement 241119e2d37fSdrh ** that has run. The text of the SQL and the amount of elapse time are 241219e2d37fSdrh ** appended to CALLBACK before the script is run. 241319e2d37fSdrh */ 241419e2d37fSdrh case DB_PROFILE: { 241519e2d37fSdrh if( objc>3 ){ 241619e2d37fSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 241719e2d37fSdrh return TCL_ERROR; 241819e2d37fSdrh }else if( objc==2 ){ 241919e2d37fSdrh if( pDb->zProfile ){ 242019e2d37fSdrh Tcl_AppendResult(interp, pDb->zProfile, 0); 242119e2d37fSdrh } 242219e2d37fSdrh }else{ 242319e2d37fSdrh char *zProfile; 242419e2d37fSdrh int len; 242519e2d37fSdrh if( pDb->zProfile ){ 242619e2d37fSdrh Tcl_Free(pDb->zProfile); 242719e2d37fSdrh } 242819e2d37fSdrh zProfile = Tcl_GetStringFromObj(objv[2], &len); 242919e2d37fSdrh if( zProfile && len>0 ){ 243019e2d37fSdrh pDb->zProfile = Tcl_Alloc( len + 1 ); 24315bb3eb9bSdrh memcpy(pDb->zProfile, zProfile, len+1); 243219e2d37fSdrh }else{ 243319e2d37fSdrh pDb->zProfile = 0; 243419e2d37fSdrh } 243519e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 243619e2d37fSdrh if( pDb->zProfile ){ 243719e2d37fSdrh pDb->interp = interp; 243819e2d37fSdrh sqlite3_profile(pDb->db, DbProfileHandler, pDb); 243919e2d37fSdrh }else{ 244019e2d37fSdrh sqlite3_profile(pDb->db, 0, 0); 244119e2d37fSdrh } 244219e2d37fSdrh #endif 244319e2d37fSdrh } 244419e2d37fSdrh break; 244519e2d37fSdrh } 244619e2d37fSdrh 24475d9d7576Sdrh /* 244822fbcb8dSdrh ** $db rekey KEY 244922fbcb8dSdrh ** 245022fbcb8dSdrh ** Change the encryption key on the currently open database. 245122fbcb8dSdrh */ 245222fbcb8dSdrh case DB_REKEY: { 245322fbcb8dSdrh int nKey; 245422fbcb8dSdrh void *pKey; 245522fbcb8dSdrh if( objc!=3 ){ 245622fbcb8dSdrh Tcl_WrongNumArgs(interp, 2, objv, "KEY"); 245722fbcb8dSdrh return TCL_ERROR; 245822fbcb8dSdrh } 245922fbcb8dSdrh pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); 24609eb9e26bSdrh #ifdef SQLITE_HAS_CODEC 24612011d5f5Sdrh rc = sqlite3_rekey(pDb->db, pKey, nKey); 246222fbcb8dSdrh if( rc ){ 2463f20b21c8Sdanielk1977 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); 246422fbcb8dSdrh rc = TCL_ERROR; 246522fbcb8dSdrh } 246622fbcb8dSdrh #endif 246722fbcb8dSdrh break; 246822fbcb8dSdrh } 246922fbcb8dSdrh 2470dc2c4915Sdrh /* $db restore ?DATABASE? FILENAME 2471dc2c4915Sdrh ** 2472dc2c4915Sdrh ** Open a database file named FILENAME. Transfer the content 2473dc2c4915Sdrh ** of FILENAME into the local database DATABASE (default: "main"). 2474dc2c4915Sdrh */ 2475dc2c4915Sdrh case DB_RESTORE: { 2476dc2c4915Sdrh const char *zSrcFile; 2477dc2c4915Sdrh const char *zDestDb; 2478dc2c4915Sdrh sqlite3 *pSrc; 2479dc2c4915Sdrh sqlite3_backup *pBackup; 2480dc2c4915Sdrh int nTimeout = 0; 2481dc2c4915Sdrh 2482dc2c4915Sdrh if( objc==3 ){ 2483dc2c4915Sdrh zDestDb = "main"; 2484dc2c4915Sdrh zSrcFile = Tcl_GetString(objv[2]); 2485dc2c4915Sdrh }else if( objc==4 ){ 2486dc2c4915Sdrh zDestDb = Tcl_GetString(objv[2]); 2487dc2c4915Sdrh zSrcFile = Tcl_GetString(objv[3]); 2488dc2c4915Sdrh }else{ 2489dc2c4915Sdrh Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME"); 2490dc2c4915Sdrh return TCL_ERROR; 2491dc2c4915Sdrh } 2492dc2c4915Sdrh rc = sqlite3_open_v2(zSrcFile, &pSrc, SQLITE_OPEN_READONLY, 0); 2493dc2c4915Sdrh if( rc!=SQLITE_OK ){ 2494dc2c4915Sdrh Tcl_AppendResult(interp, "cannot open source database: ", 2495dc2c4915Sdrh sqlite3_errmsg(pSrc), (char*)0); 2496dc2c4915Sdrh sqlite3_close(pSrc); 2497dc2c4915Sdrh return TCL_ERROR; 2498dc2c4915Sdrh } 2499dc2c4915Sdrh pBackup = sqlite3_backup_init(pDb->db, zDestDb, pSrc, "main"); 2500dc2c4915Sdrh if( pBackup==0 ){ 2501dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: ", 2502dc2c4915Sdrh sqlite3_errmsg(pDb->db), (char*)0); 2503dc2c4915Sdrh sqlite3_close(pSrc); 2504dc2c4915Sdrh return TCL_ERROR; 2505dc2c4915Sdrh } 2506dc2c4915Sdrh while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK 2507dc2c4915Sdrh || rc==SQLITE_BUSY ){ 2508dc2c4915Sdrh if( rc==SQLITE_BUSY ){ 2509dc2c4915Sdrh if( nTimeout++ >= 3 ) break; 2510dc2c4915Sdrh sqlite3_sleep(100); 2511dc2c4915Sdrh } 2512dc2c4915Sdrh } 2513dc2c4915Sdrh sqlite3_backup_finish(pBackup); 2514dc2c4915Sdrh if( rc==SQLITE_DONE ){ 2515dc2c4915Sdrh rc = TCL_OK; 2516dc2c4915Sdrh }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 2517dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: source database busy", 2518dc2c4915Sdrh (char*)0); 2519dc2c4915Sdrh rc = TCL_ERROR; 2520dc2c4915Sdrh }else{ 2521dc2c4915Sdrh Tcl_AppendResult(interp, "restore failed: ", 2522dc2c4915Sdrh sqlite3_errmsg(pDb->db), (char*)0); 2523dc2c4915Sdrh rc = TCL_ERROR; 2524dc2c4915Sdrh } 2525dc2c4915Sdrh sqlite3_close(pSrc); 2526dc2c4915Sdrh break; 2527dc2c4915Sdrh } 2528dc2c4915Sdrh 252922fbcb8dSdrh /* 2530d1d38488Sdrh ** $db status (step|sort) 2531d1d38488Sdrh ** 2532d1d38488Sdrh ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or 2533d1d38488Sdrh ** SQLITE_STMTSTATUS_SORT for the most recent eval. 2534d1d38488Sdrh */ 2535d1d38488Sdrh case DB_STATUS: { 2536d1d38488Sdrh int v; 2537d1d38488Sdrh const char *zOp; 2538d1d38488Sdrh if( objc!=3 ){ 2539d1d38488Sdrh Tcl_WrongNumArgs(interp, 2, objv, "(step|sort)"); 2540d1d38488Sdrh return TCL_ERROR; 2541d1d38488Sdrh } 2542d1d38488Sdrh zOp = Tcl_GetString(objv[2]); 2543d1d38488Sdrh if( strcmp(zOp, "step")==0 ){ 2544d1d38488Sdrh v = pDb->nStep; 2545d1d38488Sdrh }else if( strcmp(zOp, "sort")==0 ){ 2546d1d38488Sdrh v = pDb->nSort; 2547d1d38488Sdrh }else{ 2548d1d38488Sdrh Tcl_AppendResult(interp, "bad argument: should be step or sort", 2549d1d38488Sdrh (char*)0); 2550d1d38488Sdrh return TCL_ERROR; 2551d1d38488Sdrh } 2552d1d38488Sdrh Tcl_SetObjResult(interp, Tcl_NewIntObj(v)); 2553d1d38488Sdrh break; 2554d1d38488Sdrh } 2555d1d38488Sdrh 2556d1d38488Sdrh /* 2557bec3f402Sdrh ** $db timeout MILLESECONDS 2558bec3f402Sdrh ** 2559bec3f402Sdrh ** Delay for the number of milliseconds specified when a file is locked. 2560bec3f402Sdrh */ 25616d31316cSdrh case DB_TIMEOUT: { 2562bec3f402Sdrh int ms; 25636d31316cSdrh if( objc!=3 ){ 25646d31316cSdrh Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS"); 2565bec3f402Sdrh return TCL_ERROR; 256675897234Sdrh } 25676d31316cSdrh if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR; 25686f8a503dSdanielk1977 sqlite3_busy_timeout(pDb->db, ms); 25696d31316cSdrh break; 257075897234Sdrh } 2571b5a20d3cSdrh 25720f14e2ebSdrh /* 25730f14e2ebSdrh ** $db total_changes 25740f14e2ebSdrh ** 25750f14e2ebSdrh ** Return the number of rows that were modified, inserted, or deleted 25760f14e2ebSdrh ** since the database handle was created. 25770f14e2ebSdrh */ 25780f14e2ebSdrh case DB_TOTAL_CHANGES: { 25790f14e2ebSdrh Tcl_Obj *pResult; 25800f14e2ebSdrh if( objc!=2 ){ 25810f14e2ebSdrh Tcl_WrongNumArgs(interp, 2, objv, ""); 25820f14e2ebSdrh return TCL_ERROR; 25830f14e2ebSdrh } 25840f14e2ebSdrh pResult = Tcl_GetObjResult(interp); 25850f14e2ebSdrh Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db)); 25860f14e2ebSdrh break; 25870f14e2ebSdrh } 25880f14e2ebSdrh 2589b5a20d3cSdrh /* $db trace ?CALLBACK? 2590b5a20d3cSdrh ** 2591b5a20d3cSdrh ** Make arrangements to invoke the CALLBACK routine for each SQL statement 2592b5a20d3cSdrh ** that is executed. The text of the SQL is appended to CALLBACK before 2593b5a20d3cSdrh ** it is executed. 2594b5a20d3cSdrh */ 2595b5a20d3cSdrh case DB_TRACE: { 2596b5a20d3cSdrh if( objc>3 ){ 2597b5a20d3cSdrh Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); 2598b97759edSdrh return TCL_ERROR; 2599b5a20d3cSdrh }else if( objc==2 ){ 2600b5a20d3cSdrh if( pDb->zTrace ){ 2601b5a20d3cSdrh Tcl_AppendResult(interp, pDb->zTrace, 0); 2602b5a20d3cSdrh } 2603b5a20d3cSdrh }else{ 2604b5a20d3cSdrh char *zTrace; 2605b5a20d3cSdrh int len; 2606b5a20d3cSdrh if( pDb->zTrace ){ 2607b5a20d3cSdrh Tcl_Free(pDb->zTrace); 2608b5a20d3cSdrh } 2609b5a20d3cSdrh zTrace = Tcl_GetStringFromObj(objv[2], &len); 2610b5a20d3cSdrh if( zTrace && len>0 ){ 2611b5a20d3cSdrh pDb->zTrace = Tcl_Alloc( len + 1 ); 26125bb3eb9bSdrh memcpy(pDb->zTrace, zTrace, len+1); 2613b5a20d3cSdrh }else{ 2614b5a20d3cSdrh pDb->zTrace = 0; 2615b5a20d3cSdrh } 261619e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 2617b5a20d3cSdrh if( pDb->zTrace ){ 2618b5a20d3cSdrh pDb->interp = interp; 26196f8a503dSdanielk1977 sqlite3_trace(pDb->db, DbTraceHandler, pDb); 2620b5a20d3cSdrh }else{ 26216f8a503dSdanielk1977 sqlite3_trace(pDb->db, 0, 0); 2622b5a20d3cSdrh } 262319e2d37fSdrh #endif 2624b5a20d3cSdrh } 2625b5a20d3cSdrh break; 2626b5a20d3cSdrh } 2627b5a20d3cSdrh 26283d21423cSdrh /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT 26293d21423cSdrh ** 26303d21423cSdrh ** Start a new transaction (if we are not already in the midst of a 26313d21423cSdrh ** transaction) and execute the TCL script SCRIPT. After SCRIPT 26323d21423cSdrh ** completes, either commit the transaction or roll it back if SCRIPT 26333d21423cSdrh ** throws an exception. Or if no new transation was started, do nothing. 26343d21423cSdrh ** pass the exception on up the stack. 26353d21423cSdrh ** 26363d21423cSdrh ** This command was inspired by Dave Thomas's talk on Ruby at the 26373d21423cSdrh ** 2005 O'Reilly Open Source Convention (OSCON). 26383d21423cSdrh */ 26393d21423cSdrh case DB_TRANSACTION: { 26403d21423cSdrh Tcl_Obj *pScript; 2641cd38d520Sdanielk1977 const char *zBegin = "SAVEPOINT _tcl_transaction"; 26423d21423cSdrh if( objc!=3 && objc!=4 ){ 26433d21423cSdrh Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT"); 26443d21423cSdrh return TCL_ERROR; 26453d21423cSdrh } 2646cd38d520Sdanielk1977 26474a4c11aaSdan if( pDb->nTransaction==0 && objc==4 ){ 26483d21423cSdrh static const char *TTYPE_strs[] = { 2649ce604012Sdrh "deferred", "exclusive", "immediate", 0 26503d21423cSdrh }; 26513d21423cSdrh enum TTYPE_enum { 26523d21423cSdrh TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE 26533d21423cSdrh }; 26543d21423cSdrh int ttype; 2655b5555e7eSdrh if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type", 26563d21423cSdrh 0, &ttype) ){ 26573d21423cSdrh return TCL_ERROR; 26583d21423cSdrh } 26593d21423cSdrh switch( (enum TTYPE_enum)ttype ){ 26603d21423cSdrh case TTYPE_DEFERRED: /* no-op */; break; 26613d21423cSdrh case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break; 26623d21423cSdrh case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break; 26633d21423cSdrh } 26643d21423cSdrh } 2665cd38d520Sdanielk1977 pScript = objv[objc-1]; 2666cd38d520Sdanielk1977 26674a4c11aaSdan /* Run the SQLite BEGIN command to open a transaction or savepoint. */ 26681f1549f8Sdrh pDb->disableAuth++; 2669cd38d520Sdanielk1977 rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0); 26701f1549f8Sdrh pDb->disableAuth--; 2671cd38d520Sdanielk1977 if( rc!=SQLITE_OK ){ 2672cd38d520Sdanielk1977 Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); 2673cd38d520Sdanielk1977 return TCL_ERROR; 26743d21423cSdrh } 2675cd38d520Sdanielk1977 pDb->nTransaction++; 2676cd38d520Sdanielk1977 26774a4c11aaSdan /* If using NRE, schedule a callback to invoke the script pScript, then 26784a4c11aaSdan ** a second callback to commit (or rollback) the transaction or savepoint 26794a4c11aaSdan ** opened above. If not using NRE, evaluate the script directly, then 26804a4c11aaSdan ** call function DbTransPostCmd() to commit (or rollback) the transaction 26814a4c11aaSdan ** or savepoint. */ 26824a4c11aaSdan if( DbUseNre() ){ 26834a4c11aaSdan Tcl_NRAddCallback(interp, DbTransPostCmd, cd, 0, 0, 0); 26844a4c11aaSdan Tcl_NREvalObj(interp, pScript, 0); 26853d21423cSdrh }else{ 26864a4c11aaSdan rc = DbTransPostCmd(&cd, interp, Tcl_EvalObjEx(interp, pScript, 0)); 26873d21423cSdrh } 26883d21423cSdrh break; 26893d21423cSdrh } 26903d21423cSdrh 269194eb6a14Sdanielk1977 /* 2692404ca075Sdanielk1977 ** $db unlock_notify ?script? 2693404ca075Sdanielk1977 */ 2694404ca075Sdanielk1977 case DB_UNLOCK_NOTIFY: { 2695404ca075Sdanielk1977 #ifndef SQLITE_ENABLE_UNLOCK_NOTIFY 2696404ca075Sdanielk1977 Tcl_AppendResult(interp, "unlock_notify not available in this build", 0); 2697404ca075Sdanielk1977 rc = TCL_ERROR; 2698404ca075Sdanielk1977 #else 2699404ca075Sdanielk1977 if( objc!=2 && objc!=3 ){ 2700404ca075Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 2701404ca075Sdanielk1977 rc = TCL_ERROR; 2702404ca075Sdanielk1977 }else{ 2703404ca075Sdanielk1977 void (*xNotify)(void **, int) = 0; 2704404ca075Sdanielk1977 void *pNotifyArg = 0; 2705404ca075Sdanielk1977 2706404ca075Sdanielk1977 if( pDb->pUnlockNotify ){ 2707404ca075Sdanielk1977 Tcl_DecrRefCount(pDb->pUnlockNotify); 2708404ca075Sdanielk1977 pDb->pUnlockNotify = 0; 2709404ca075Sdanielk1977 } 2710404ca075Sdanielk1977 2711404ca075Sdanielk1977 if( objc==3 ){ 2712404ca075Sdanielk1977 xNotify = DbUnlockNotify; 2713404ca075Sdanielk1977 pNotifyArg = (void *)pDb; 2714404ca075Sdanielk1977 pDb->pUnlockNotify = objv[2]; 2715404ca075Sdanielk1977 Tcl_IncrRefCount(pDb->pUnlockNotify); 2716404ca075Sdanielk1977 } 2717404ca075Sdanielk1977 2718404ca075Sdanielk1977 if( sqlite3_unlock_notify(pDb->db, xNotify, pNotifyArg) ){ 2719404ca075Sdanielk1977 Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); 2720404ca075Sdanielk1977 rc = TCL_ERROR; 2721404ca075Sdanielk1977 } 2722404ca075Sdanielk1977 } 2723404ca075Sdanielk1977 #endif 2724404ca075Sdanielk1977 break; 2725404ca075Sdanielk1977 } 2726404ca075Sdanielk1977 2727404ca075Sdanielk1977 /* 272894eb6a14Sdanielk1977 ** $db update_hook ?script? 272971fd80bfSdanielk1977 ** $db rollback_hook ?script? 273094eb6a14Sdanielk1977 */ 273171fd80bfSdanielk1977 case DB_UPDATE_HOOK: 273271fd80bfSdanielk1977 case DB_ROLLBACK_HOOK: { 273371fd80bfSdanielk1977 273471fd80bfSdanielk1977 /* set ppHook to point at pUpdateHook or pRollbackHook, depending on 273571fd80bfSdanielk1977 ** whether [$db update_hook] or [$db rollback_hook] was invoked. 273671fd80bfSdanielk1977 */ 273771fd80bfSdanielk1977 Tcl_Obj **ppHook; 273871fd80bfSdanielk1977 if( choice==DB_UPDATE_HOOK ){ 273971fd80bfSdanielk1977 ppHook = &pDb->pUpdateHook; 274071fd80bfSdanielk1977 }else{ 274171fd80bfSdanielk1977 ppHook = &pDb->pRollbackHook; 274271fd80bfSdanielk1977 } 274371fd80bfSdanielk1977 274494eb6a14Sdanielk1977 if( objc!=2 && objc!=3 ){ 274594eb6a14Sdanielk1977 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); 274694eb6a14Sdanielk1977 return TCL_ERROR; 274794eb6a14Sdanielk1977 } 274871fd80bfSdanielk1977 if( *ppHook ){ 274971fd80bfSdanielk1977 Tcl_SetObjResult(interp, *ppHook); 275094eb6a14Sdanielk1977 if( objc==3 ){ 275171fd80bfSdanielk1977 Tcl_DecrRefCount(*ppHook); 275271fd80bfSdanielk1977 *ppHook = 0; 275394eb6a14Sdanielk1977 } 275494eb6a14Sdanielk1977 } 275594eb6a14Sdanielk1977 if( objc==3 ){ 275671fd80bfSdanielk1977 assert( !(*ppHook) ); 275794eb6a14Sdanielk1977 if( Tcl_GetCharLength(objv[2])>0 ){ 275871fd80bfSdanielk1977 *ppHook = objv[2]; 275971fd80bfSdanielk1977 Tcl_IncrRefCount(*ppHook); 276094eb6a14Sdanielk1977 } 276194eb6a14Sdanielk1977 } 276271fd80bfSdanielk1977 276371fd80bfSdanielk1977 sqlite3_update_hook(pDb->db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); 276471fd80bfSdanielk1977 sqlite3_rollback_hook(pDb->db,(pDb->pRollbackHook?DbRollbackHandler:0),pDb); 276571fd80bfSdanielk1977 276694eb6a14Sdanielk1977 break; 276794eb6a14Sdanielk1977 } 276894eb6a14Sdanielk1977 27694397de57Sdanielk1977 /* $db version 27704397de57Sdanielk1977 ** 27714397de57Sdanielk1977 ** Return the version string for this database. 27724397de57Sdanielk1977 */ 27734397de57Sdanielk1977 case DB_VERSION: { 27744397de57Sdanielk1977 Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC); 27754397de57Sdanielk1977 break; 27764397de57Sdanielk1977 } 27774397de57Sdanielk1977 27781067fe11Stpoindex 27796d31316cSdrh } /* End of the SWITCH statement */ 278022fbcb8dSdrh return rc; 278175897234Sdrh } 278275897234Sdrh 2783a2c8a95bSdrh #if SQLITE_TCL_NRE 2784a2c8a95bSdrh /* 2785a2c8a95bSdrh ** Adaptor that provides an objCmd interface to the NRE-enabled 2786a2c8a95bSdrh ** interface implementation. 2787a2c8a95bSdrh */ 2788a2c8a95bSdrh static int DbObjCmdAdaptor( 2789a2c8a95bSdrh void *cd, 2790a2c8a95bSdrh Tcl_Interp *interp, 2791a2c8a95bSdrh int objc, 2792a2c8a95bSdrh Tcl_Obj *const*objv 2793a2c8a95bSdrh ){ 2794a2c8a95bSdrh return Tcl_NRCallObjProc(interp, DbObjCmd, cd, objc, objv); 2795a2c8a95bSdrh } 2796a2c8a95bSdrh #endif /* SQLITE_TCL_NRE */ 2797a2c8a95bSdrh 279875897234Sdrh /* 27993570ad93Sdrh ** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN? 28009a6284c1Sdanielk1977 ** ?-create BOOLEAN? ?-nomutex BOOLEAN? 280175897234Sdrh ** 280275897234Sdrh ** This is the main Tcl command. When the "sqlite" Tcl command is 280375897234Sdrh ** invoked, this routine runs to process that command. 280475897234Sdrh ** 280575897234Sdrh ** The first argument, DBNAME, is an arbitrary name for a new 280675897234Sdrh ** database connection. This command creates a new command named 280775897234Sdrh ** DBNAME that is used to control that connection. The database 280875897234Sdrh ** connection is deleted when the DBNAME command is deleted. 280975897234Sdrh ** 28103570ad93Sdrh ** The second argument is the name of the database file. 2811fbc3eab8Sdrh ** 281275897234Sdrh */ 281322fbcb8dSdrh static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ 2814bec3f402Sdrh SqliteDb *p; 281522fbcb8dSdrh void *pKey = 0; 281622fbcb8dSdrh int nKey = 0; 281722fbcb8dSdrh const char *zArg; 281875897234Sdrh char *zErrMsg; 28193570ad93Sdrh int i; 282022fbcb8dSdrh const char *zFile; 28213570ad93Sdrh const char *zVfs = 0; 2822d9da78a2Sdrh int flags; 2823882e8e4dSdrh Tcl_DString translatedFilename; 2824d9da78a2Sdrh 2825d9da78a2Sdrh /* In normal use, each TCL interpreter runs in a single thread. So 2826d9da78a2Sdrh ** by default, we can turn of mutexing on SQLite database connections. 2827d9da78a2Sdrh ** However, for testing purposes it is useful to have mutexes turned 2828d9da78a2Sdrh ** on. So, by default, mutexes default off. But if compiled with 2829d9da78a2Sdrh ** SQLITE_TCL_DEFAULT_FULLMUTEX then mutexes default on. 2830d9da78a2Sdrh */ 2831d9da78a2Sdrh #ifdef SQLITE_TCL_DEFAULT_FULLMUTEX 2832d9da78a2Sdrh flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX; 2833d9da78a2Sdrh #else 2834d9da78a2Sdrh flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX; 2835d9da78a2Sdrh #endif 2836d9da78a2Sdrh 283722fbcb8dSdrh if( objc==2 ){ 283822fbcb8dSdrh zArg = Tcl_GetStringFromObj(objv[1], 0); 283922fbcb8dSdrh if( strcmp(zArg,"-version")==0 ){ 28406f8a503dSdanielk1977 Tcl_AppendResult(interp,sqlite3_version,0); 2841647cb0e1Sdrh return TCL_OK; 2842647cb0e1Sdrh } 28439eb9e26bSdrh if( strcmp(zArg,"-has-codec")==0 ){ 28449eb9e26bSdrh #ifdef SQLITE_HAS_CODEC 284522fbcb8dSdrh Tcl_AppendResult(interp,"1",0); 284622fbcb8dSdrh #else 284722fbcb8dSdrh Tcl_AppendResult(interp,"0",0); 284822fbcb8dSdrh #endif 284922fbcb8dSdrh return TCL_OK; 285022fbcb8dSdrh } 2851fbc3eab8Sdrh } 28523570ad93Sdrh for(i=3; i+1<objc; i+=2){ 28533570ad93Sdrh zArg = Tcl_GetString(objv[i]); 285422fbcb8dSdrh if( strcmp(zArg,"-key")==0 ){ 28553570ad93Sdrh pKey = Tcl_GetByteArrayFromObj(objv[i+1], &nKey); 28563570ad93Sdrh }else if( strcmp(zArg, "-vfs")==0 ){ 28573570ad93Sdrh i++; 28583570ad93Sdrh zVfs = Tcl_GetString(objv[i]); 28593570ad93Sdrh }else if( strcmp(zArg, "-readonly")==0 ){ 28603570ad93Sdrh int b; 28613570ad93Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 28623570ad93Sdrh if( b ){ 286333f4e02aSdrh flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); 28643570ad93Sdrh flags |= SQLITE_OPEN_READONLY; 28653570ad93Sdrh }else{ 28663570ad93Sdrh flags &= ~SQLITE_OPEN_READONLY; 28673570ad93Sdrh flags |= SQLITE_OPEN_READWRITE; 28683570ad93Sdrh } 28693570ad93Sdrh }else if( strcmp(zArg, "-create")==0 ){ 28703570ad93Sdrh int b; 28713570ad93Sdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 287233f4e02aSdrh if( b && (flags & SQLITE_OPEN_READONLY)==0 ){ 28733570ad93Sdrh flags |= SQLITE_OPEN_CREATE; 28743570ad93Sdrh }else{ 28753570ad93Sdrh flags &= ~SQLITE_OPEN_CREATE; 28763570ad93Sdrh } 28779a6284c1Sdanielk1977 }else if( strcmp(zArg, "-nomutex")==0 ){ 28789a6284c1Sdanielk1977 int b; 28799a6284c1Sdanielk1977 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 28809a6284c1Sdanielk1977 if( b ){ 28819a6284c1Sdanielk1977 flags |= SQLITE_OPEN_NOMUTEX; 2882039963adSdrh flags &= ~SQLITE_OPEN_FULLMUTEX; 28839a6284c1Sdanielk1977 }else{ 28849a6284c1Sdanielk1977 flags &= ~SQLITE_OPEN_NOMUTEX; 28859a6284c1Sdanielk1977 } 2886039963adSdrh }else if( strcmp(zArg, "-fullmutex")==0 ){ 2887039963adSdrh int b; 2888039963adSdrh if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; 2889039963adSdrh if( b ){ 2890039963adSdrh flags |= SQLITE_OPEN_FULLMUTEX; 2891039963adSdrh flags &= ~SQLITE_OPEN_NOMUTEX; 2892039963adSdrh }else{ 2893039963adSdrh flags &= ~SQLITE_OPEN_FULLMUTEX; 2894039963adSdrh } 28953570ad93Sdrh }else{ 28963570ad93Sdrh Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0); 28973570ad93Sdrh return TCL_ERROR; 289822fbcb8dSdrh } 289922fbcb8dSdrh } 29003570ad93Sdrh if( objc<3 || (objc&1)!=1 ){ 290122fbcb8dSdrh Tcl_WrongNumArgs(interp, 1, objv, 29023570ad93Sdrh "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" 2903039963adSdrh " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN?" 29049eb9e26bSdrh #ifdef SQLITE_HAS_CODEC 29053570ad93Sdrh " ?-key CODECKEY?" 290622fbcb8dSdrh #endif 290722fbcb8dSdrh ); 290875897234Sdrh return TCL_ERROR; 290975897234Sdrh } 291075897234Sdrh zErrMsg = 0; 29114cdc9e84Sdrh p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); 291275897234Sdrh if( p==0 ){ 2913bec3f402Sdrh Tcl_SetResult(interp, "malloc failed", TCL_STATIC); 2914bec3f402Sdrh return TCL_ERROR; 2915bec3f402Sdrh } 2916bec3f402Sdrh memset(p, 0, sizeof(*p)); 291722fbcb8dSdrh zFile = Tcl_GetStringFromObj(objv[2], 0); 2918882e8e4dSdrh zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); 29193570ad93Sdrh sqlite3_open_v2(zFile, &p->db, flags, zVfs); 2920882e8e4dSdrh Tcl_DStringFree(&translatedFilename); 292180290863Sdanielk1977 if( SQLITE_OK!=sqlite3_errcode(p->db) ){ 29229404d50eSdrh zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); 292380290863Sdanielk1977 sqlite3_close(p->db); 292480290863Sdanielk1977 p->db = 0; 292580290863Sdanielk1977 } 29262011d5f5Sdrh #ifdef SQLITE_HAS_CODEC 2927f3a65f7eSdrh if( p->db ){ 29282011d5f5Sdrh sqlite3_key(p->db, pKey, nKey); 2929f3a65f7eSdrh } 2930eb8ed70dSdrh #endif 2931bec3f402Sdrh if( p->db==0 ){ 293275897234Sdrh Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 2933bec3f402Sdrh Tcl_Free((char*)p); 29349404d50eSdrh sqlite3_free(zErrMsg); 293575897234Sdrh return TCL_ERROR; 293675897234Sdrh } 2937fb7e7651Sdrh p->maxStmt = NUM_PREPARED_STMTS; 29385169bbc6Sdrh p->interp = interp; 293922fbcb8dSdrh zArg = Tcl_GetStringFromObj(objv[1], 0); 29404a4c11aaSdan if( DbUseNre() ){ 2941a2c8a95bSdrh Tcl_NRCreateCommand(interp, zArg, DbObjCmdAdaptor, DbObjCmd, 2942a2c8a95bSdrh (char*)p, DbDeleteCmd); 29434a4c11aaSdan }else{ 294422fbcb8dSdrh Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); 29454a4c11aaSdan } 294675897234Sdrh return TCL_OK; 294775897234Sdrh } 294875897234Sdrh 294975897234Sdrh /* 295090ca9753Sdrh ** Provide a dummy Tcl_InitStubs if we are using this as a static 295190ca9753Sdrh ** library. 295290ca9753Sdrh */ 295390ca9753Sdrh #ifndef USE_TCL_STUBS 295490ca9753Sdrh # undef Tcl_InitStubs 295590ca9753Sdrh # define Tcl_InitStubs(a,b,c) 295690ca9753Sdrh #endif 295790ca9753Sdrh 295890ca9753Sdrh /* 295929bc4615Sdrh ** Make sure we have a PACKAGE_VERSION macro defined. This will be 296029bc4615Sdrh ** defined automatically by the TEA makefile. But other makefiles 296129bc4615Sdrh ** do not define it. 296229bc4615Sdrh */ 296329bc4615Sdrh #ifndef PACKAGE_VERSION 296429bc4615Sdrh # define PACKAGE_VERSION SQLITE_VERSION 296529bc4615Sdrh #endif 296629bc4615Sdrh 296729bc4615Sdrh /* 296875897234Sdrh ** Initialize this module. 296975897234Sdrh ** 297075897234Sdrh ** This Tcl module contains only a single new Tcl command named "sqlite". 297175897234Sdrh ** (Hence there is no namespace. There is no point in using a namespace 297275897234Sdrh ** if the extension only supplies one new name!) The "sqlite" command is 297375897234Sdrh ** used to open a new SQLite database. See the DbMain() routine above 297475897234Sdrh ** for additional information. 297575897234Sdrh */ 2976ad6e1370Sdrh EXTERN int Sqlite3_Init(Tcl_Interp *interp){ 297792febd92Sdrh Tcl_InitStubs(interp, "8.4", 0); 2978ef4ac8f9Sdrh Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); 297929bc4615Sdrh Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION); 298049766d6cSdrh Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); 298129bc4615Sdrh Tcl_PkgProvide(interp, "sqlite", PACKAGE_VERSION); 298290ca9753Sdrh return TCL_OK; 298390ca9753Sdrh } 2984ad6e1370Sdrh EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 2985ad6e1370Sdrh EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; } 2986ad6e1370Sdrh EXTERN int Tclsqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; } 2987e2c3a659Sdrh EXTERN int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 2988e2c3a659Sdrh EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 2989e2c3a659Sdrh EXTERN int Sqlite3_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK; } 2990e2c3a659Sdrh EXTERN int Tclsqlite3_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK;} 2991e2c3a659Sdrh 299249766d6cSdrh 299349766d6cSdrh #ifndef SQLITE_3_SUFFIX_ONLY 2994ad6e1370Sdrh EXTERN int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 2995ad6e1370Sdrh EXTERN int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } 2996ad6e1370Sdrh EXTERN int Sqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } 2997ad6e1370Sdrh EXTERN int Tclsqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } 2998e2c3a659Sdrh EXTERN int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 2999e2c3a659Sdrh EXTERN int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3000e2c3a659Sdrh EXTERN int Sqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK; } 3001e2c3a659Sdrh EXTERN int Tclsqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK;} 300249766d6cSdrh #endif 300375897234Sdrh 30043e27c026Sdrh #ifdef TCLSH 30053e27c026Sdrh /***************************************************************************** 300657a0227fSdrh ** All of the code that follows is used to build standalone TCL interpreters 300757a0227fSdrh ** that are statically linked with SQLite. Enable these by compiling 300857a0227fSdrh ** with -DTCLSH=n where n can be 1 or 2. An n of 1 generates a standard 300957a0227fSdrh ** tclsh but with SQLite built in. An n of 2 generates the SQLite space 301057a0227fSdrh ** analysis program. 301175897234Sdrh */ 3012348784efSdrh 301357a0227fSdrh #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) 301457a0227fSdrh /* 301557a0227fSdrh * This code implements the MD5 message-digest algorithm. 301657a0227fSdrh * The algorithm is due to Ron Rivest. This code was 301757a0227fSdrh * written by Colin Plumb in 1993, no copyright is claimed. 301857a0227fSdrh * This code is in the public domain; do with it what you wish. 301957a0227fSdrh * 302057a0227fSdrh * Equivalent code is available from RSA Data Security, Inc. 302157a0227fSdrh * This code has been tested against that, and is equivalent, 302257a0227fSdrh * except that you don't need to include two pages of legalese 302357a0227fSdrh * with every copy. 302457a0227fSdrh * 302557a0227fSdrh * To compute the message digest of a chunk of bytes, declare an 302657a0227fSdrh * MD5Context structure, pass it to MD5Init, call MD5Update as 302757a0227fSdrh * needed on buffers full of bytes, and then call MD5Final, which 302857a0227fSdrh * will fill a supplied 16-byte array with the digest. 302957a0227fSdrh */ 303057a0227fSdrh 303157a0227fSdrh /* 303257a0227fSdrh * If compiled on a machine that doesn't have a 32-bit integer, 303357a0227fSdrh * you just set "uint32" to the appropriate datatype for an 303457a0227fSdrh * unsigned 32-bit integer. For example: 303557a0227fSdrh * 303657a0227fSdrh * cc -Duint32='unsigned long' md5.c 303757a0227fSdrh * 303857a0227fSdrh */ 303957a0227fSdrh #ifndef uint32 304057a0227fSdrh # define uint32 unsigned int 304157a0227fSdrh #endif 304257a0227fSdrh 304357a0227fSdrh struct MD5Context { 304457a0227fSdrh int isInit; 304557a0227fSdrh uint32 buf[4]; 304657a0227fSdrh uint32 bits[2]; 304757a0227fSdrh unsigned char in[64]; 304857a0227fSdrh }; 304957a0227fSdrh typedef struct MD5Context MD5Context; 305057a0227fSdrh 305157a0227fSdrh /* 305257a0227fSdrh * Note: this code is harmless on little-endian machines. 305357a0227fSdrh */ 305457a0227fSdrh static void byteReverse (unsigned char *buf, unsigned longs){ 305557a0227fSdrh uint32 t; 305657a0227fSdrh do { 305757a0227fSdrh t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 | 305857a0227fSdrh ((unsigned)buf[1]<<8 | buf[0]); 305957a0227fSdrh *(uint32 *)buf = t; 306057a0227fSdrh buf += 4; 306157a0227fSdrh } while (--longs); 306257a0227fSdrh } 306357a0227fSdrh /* The four core functions - F1 is optimized somewhat */ 306457a0227fSdrh 306557a0227fSdrh /* #define F1(x, y, z) (x & y | ~x & z) */ 306657a0227fSdrh #define F1(x, y, z) (z ^ (x & (y ^ z))) 306757a0227fSdrh #define F2(x, y, z) F1(z, x, y) 306857a0227fSdrh #define F3(x, y, z) (x ^ y ^ z) 306957a0227fSdrh #define F4(x, y, z) (y ^ (x | ~z)) 307057a0227fSdrh 307157a0227fSdrh /* This is the central step in the MD5 algorithm. */ 307257a0227fSdrh #define MD5STEP(f, w, x, y, z, data, s) \ 307357a0227fSdrh ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) 307457a0227fSdrh 307557a0227fSdrh /* 307657a0227fSdrh * The core of the MD5 algorithm, this alters an existing MD5 hash to 307757a0227fSdrh * reflect the addition of 16 longwords of new data. MD5Update blocks 307857a0227fSdrh * the data and converts bytes into longwords for this routine. 307957a0227fSdrh */ 308057a0227fSdrh static void MD5Transform(uint32 buf[4], const uint32 in[16]){ 308157a0227fSdrh register uint32 a, b, c, d; 308257a0227fSdrh 308357a0227fSdrh a = buf[0]; 308457a0227fSdrh b = buf[1]; 308557a0227fSdrh c = buf[2]; 308657a0227fSdrh d = buf[3]; 308757a0227fSdrh 308857a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); 308957a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); 309057a0227fSdrh MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); 309157a0227fSdrh MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); 309257a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); 309357a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); 309457a0227fSdrh MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); 309557a0227fSdrh MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); 309657a0227fSdrh MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); 309757a0227fSdrh MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); 309857a0227fSdrh MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); 309957a0227fSdrh MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); 310057a0227fSdrh MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); 310157a0227fSdrh MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); 310257a0227fSdrh MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); 310357a0227fSdrh MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); 310457a0227fSdrh 310557a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); 310657a0227fSdrh MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); 310757a0227fSdrh MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); 310857a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); 310957a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); 311057a0227fSdrh MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); 311157a0227fSdrh MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); 311257a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); 311357a0227fSdrh MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); 311457a0227fSdrh MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); 311557a0227fSdrh MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); 311657a0227fSdrh MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); 311757a0227fSdrh MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); 311857a0227fSdrh MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); 311957a0227fSdrh MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); 312057a0227fSdrh MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); 312157a0227fSdrh 312257a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); 312357a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); 312457a0227fSdrh MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); 312557a0227fSdrh MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); 312657a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); 312757a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); 312857a0227fSdrh MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); 312957a0227fSdrh MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); 313057a0227fSdrh MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); 313157a0227fSdrh MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); 313257a0227fSdrh MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); 313357a0227fSdrh MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); 313457a0227fSdrh MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); 313557a0227fSdrh MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); 313657a0227fSdrh MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); 313757a0227fSdrh MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); 313857a0227fSdrh 313957a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); 314057a0227fSdrh MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); 314157a0227fSdrh MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); 314257a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); 314357a0227fSdrh MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); 314457a0227fSdrh MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); 314557a0227fSdrh MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); 314657a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); 314757a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); 314857a0227fSdrh MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); 314957a0227fSdrh MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); 315057a0227fSdrh MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); 315157a0227fSdrh MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); 315257a0227fSdrh MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); 315357a0227fSdrh MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); 315457a0227fSdrh MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); 315557a0227fSdrh 315657a0227fSdrh buf[0] += a; 315757a0227fSdrh buf[1] += b; 315857a0227fSdrh buf[2] += c; 315957a0227fSdrh buf[3] += d; 316057a0227fSdrh } 316157a0227fSdrh 316257a0227fSdrh /* 316357a0227fSdrh * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 316457a0227fSdrh * initialization constants. 316557a0227fSdrh */ 316657a0227fSdrh static void MD5Init(MD5Context *ctx){ 316757a0227fSdrh ctx->isInit = 1; 316857a0227fSdrh ctx->buf[0] = 0x67452301; 316957a0227fSdrh ctx->buf[1] = 0xefcdab89; 317057a0227fSdrh ctx->buf[2] = 0x98badcfe; 317157a0227fSdrh ctx->buf[3] = 0x10325476; 317257a0227fSdrh ctx->bits[0] = 0; 317357a0227fSdrh ctx->bits[1] = 0; 317457a0227fSdrh } 317557a0227fSdrh 317657a0227fSdrh /* 317757a0227fSdrh * Update context to reflect the concatenation of another buffer full 317857a0227fSdrh * of bytes. 317957a0227fSdrh */ 318057a0227fSdrh static 318157a0227fSdrh void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){ 318257a0227fSdrh uint32 t; 318357a0227fSdrh 318457a0227fSdrh /* Update bitcount */ 318557a0227fSdrh 318657a0227fSdrh t = ctx->bits[0]; 318757a0227fSdrh if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) 318857a0227fSdrh ctx->bits[1]++; /* Carry from low to high */ 318957a0227fSdrh ctx->bits[1] += len >> 29; 319057a0227fSdrh 319157a0227fSdrh t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ 319257a0227fSdrh 319357a0227fSdrh /* Handle any leading odd-sized chunks */ 319457a0227fSdrh 319557a0227fSdrh if ( t ) { 319657a0227fSdrh unsigned char *p = (unsigned char *)ctx->in + t; 319757a0227fSdrh 319857a0227fSdrh t = 64-t; 319957a0227fSdrh if (len < t) { 320057a0227fSdrh memcpy(p, buf, len); 320157a0227fSdrh return; 320257a0227fSdrh } 320357a0227fSdrh memcpy(p, buf, t); 320457a0227fSdrh byteReverse(ctx->in, 16); 320557a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 320657a0227fSdrh buf += t; 320757a0227fSdrh len -= t; 320857a0227fSdrh } 320957a0227fSdrh 321057a0227fSdrh /* Process data in 64-byte chunks */ 321157a0227fSdrh 321257a0227fSdrh while (len >= 64) { 321357a0227fSdrh memcpy(ctx->in, buf, 64); 321457a0227fSdrh byteReverse(ctx->in, 16); 321557a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 321657a0227fSdrh buf += 64; 321757a0227fSdrh len -= 64; 321857a0227fSdrh } 321957a0227fSdrh 322057a0227fSdrh /* Handle any remaining bytes of data. */ 322157a0227fSdrh 322257a0227fSdrh memcpy(ctx->in, buf, len); 322357a0227fSdrh } 322457a0227fSdrh 322557a0227fSdrh /* 322657a0227fSdrh * Final wrapup - pad to 64-byte boundary with the bit pattern 322757a0227fSdrh * 1 0* (64-bit count of bits processed, MSB-first) 322857a0227fSdrh */ 322957a0227fSdrh static void MD5Final(unsigned char digest[16], MD5Context *ctx){ 323057a0227fSdrh unsigned count; 323157a0227fSdrh unsigned char *p; 323257a0227fSdrh 323357a0227fSdrh /* Compute number of bytes mod 64 */ 323457a0227fSdrh count = (ctx->bits[0] >> 3) & 0x3F; 323557a0227fSdrh 323657a0227fSdrh /* Set the first char of padding to 0x80. This is safe since there is 323757a0227fSdrh always at least one byte free */ 323857a0227fSdrh p = ctx->in + count; 323957a0227fSdrh *p++ = 0x80; 324057a0227fSdrh 324157a0227fSdrh /* Bytes of padding needed to make 64 bytes */ 324257a0227fSdrh count = 64 - 1 - count; 324357a0227fSdrh 324457a0227fSdrh /* Pad out to 56 mod 64 */ 324557a0227fSdrh if (count < 8) { 324657a0227fSdrh /* Two lots of padding: Pad the first block to 64 bytes */ 324757a0227fSdrh memset(p, 0, count); 324857a0227fSdrh byteReverse(ctx->in, 16); 324957a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 325057a0227fSdrh 325157a0227fSdrh /* Now fill the next block with 56 bytes */ 325257a0227fSdrh memset(ctx->in, 0, 56); 325357a0227fSdrh } else { 325457a0227fSdrh /* Pad block to 56 bytes */ 325557a0227fSdrh memset(p, 0, count-8); 325657a0227fSdrh } 325757a0227fSdrh byteReverse(ctx->in, 14); 325857a0227fSdrh 325957a0227fSdrh /* Append length in bits and transform */ 326057a0227fSdrh ((uint32 *)ctx->in)[ 14 ] = ctx->bits[0]; 326157a0227fSdrh ((uint32 *)ctx->in)[ 15 ] = ctx->bits[1]; 326257a0227fSdrh 326357a0227fSdrh MD5Transform(ctx->buf, (uint32 *)ctx->in); 326457a0227fSdrh byteReverse((unsigned char *)ctx->buf, 4); 326557a0227fSdrh memcpy(digest, ctx->buf, 16); 326657a0227fSdrh memset(ctx, 0, sizeof(ctx)); /* In case it is sensitive */ 326757a0227fSdrh } 326857a0227fSdrh 326957a0227fSdrh /* 327057a0227fSdrh ** Convert a 128-bit MD5 digest into a 32-digit base-16 number. 327157a0227fSdrh */ 327257a0227fSdrh static void MD5DigestToBase16(unsigned char *digest, char *zBuf){ 327357a0227fSdrh static char const zEncode[] = "0123456789abcdef"; 327457a0227fSdrh int i, j; 327557a0227fSdrh 327657a0227fSdrh for(j=i=0; i<16; i++){ 327757a0227fSdrh int a = digest[i]; 327857a0227fSdrh zBuf[j++] = zEncode[(a>>4)&0xf]; 327957a0227fSdrh zBuf[j++] = zEncode[a & 0xf]; 328057a0227fSdrh } 328157a0227fSdrh zBuf[j] = 0; 328257a0227fSdrh } 328357a0227fSdrh 328457a0227fSdrh 328557a0227fSdrh /* 328657a0227fSdrh ** Convert a 128-bit MD5 digest into sequency of eight 5-digit integers 328757a0227fSdrh ** each representing 16 bits of the digest and separated from each 328857a0227fSdrh ** other by a "-" character. 328957a0227fSdrh */ 329057a0227fSdrh static void MD5DigestToBase10x8(unsigned char digest[16], char zDigest[50]){ 329157a0227fSdrh int i, j; 329257a0227fSdrh unsigned int x; 329357a0227fSdrh for(i=j=0; i<16; i+=2){ 329457a0227fSdrh x = digest[i]*256 + digest[i+1]; 329557a0227fSdrh if( i>0 ) zDigest[j++] = '-'; 329657a0227fSdrh sprintf(&zDigest[j], "%05u", x); 329757a0227fSdrh j += 5; 329857a0227fSdrh } 329957a0227fSdrh zDigest[j] = 0; 330057a0227fSdrh } 330157a0227fSdrh 330257a0227fSdrh /* 330357a0227fSdrh ** A TCL command for md5. The argument is the text to be hashed. The 330457a0227fSdrh ** Result is the hash in base64. 330557a0227fSdrh */ 330657a0227fSdrh static int md5_cmd(void*cd, Tcl_Interp *interp, int argc, const char **argv){ 330757a0227fSdrh MD5Context ctx; 330857a0227fSdrh unsigned char digest[16]; 330957a0227fSdrh char zBuf[50]; 331057a0227fSdrh void (*converter)(unsigned char*, char*); 331157a0227fSdrh 331257a0227fSdrh if( argc!=2 ){ 331357a0227fSdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 331457a0227fSdrh " TEXT\"", 0); 331557a0227fSdrh return TCL_ERROR; 331657a0227fSdrh } 331757a0227fSdrh MD5Init(&ctx); 331857a0227fSdrh MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1])); 331957a0227fSdrh MD5Final(digest, &ctx); 332057a0227fSdrh converter = (void(*)(unsigned char*,char*))cd; 332157a0227fSdrh converter(digest, zBuf); 332257a0227fSdrh Tcl_AppendResult(interp, zBuf, (char*)0); 332357a0227fSdrh return TCL_OK; 332457a0227fSdrh } 332557a0227fSdrh 332657a0227fSdrh /* 332757a0227fSdrh ** A TCL command to take the md5 hash of a file. The argument is the 332857a0227fSdrh ** name of the file. 332957a0227fSdrh */ 333057a0227fSdrh static int md5file_cmd(void*cd, Tcl_Interp*interp, int argc, const char **argv){ 333157a0227fSdrh FILE *in; 333257a0227fSdrh MD5Context ctx; 333357a0227fSdrh void (*converter)(unsigned char*, char*); 333457a0227fSdrh unsigned char digest[16]; 333557a0227fSdrh char zBuf[10240]; 333657a0227fSdrh 333757a0227fSdrh if( argc!=2 ){ 333857a0227fSdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 333957a0227fSdrh " FILENAME\"", 0); 334057a0227fSdrh return TCL_ERROR; 334157a0227fSdrh } 334257a0227fSdrh in = fopen(argv[1],"rb"); 334357a0227fSdrh if( in==0 ){ 334457a0227fSdrh Tcl_AppendResult(interp,"unable to open file \"", argv[1], 334557a0227fSdrh "\" for reading", 0); 334657a0227fSdrh return TCL_ERROR; 334757a0227fSdrh } 334857a0227fSdrh MD5Init(&ctx); 334957a0227fSdrh for(;;){ 335057a0227fSdrh int n; 335157a0227fSdrh n = fread(zBuf, 1, sizeof(zBuf), in); 335257a0227fSdrh if( n<=0 ) break; 335357a0227fSdrh MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n); 335457a0227fSdrh } 335557a0227fSdrh fclose(in); 335657a0227fSdrh MD5Final(digest, &ctx); 335757a0227fSdrh converter = (void(*)(unsigned char*,char*))cd; 335857a0227fSdrh converter(digest, zBuf); 335957a0227fSdrh Tcl_AppendResult(interp, zBuf, (char*)0); 336057a0227fSdrh return TCL_OK; 336157a0227fSdrh } 336257a0227fSdrh 336357a0227fSdrh /* 336457a0227fSdrh ** Register the four new TCL commands for generating MD5 checksums 336557a0227fSdrh ** with the TCL interpreter. 336657a0227fSdrh */ 336757a0227fSdrh int Md5_Init(Tcl_Interp *interp){ 336857a0227fSdrh Tcl_CreateCommand(interp, "md5", (Tcl_CmdProc*)md5_cmd, 336957a0227fSdrh MD5DigestToBase16, 0); 337057a0227fSdrh Tcl_CreateCommand(interp, "md5-10x8", (Tcl_CmdProc*)md5_cmd, 337157a0227fSdrh MD5DigestToBase10x8, 0); 337257a0227fSdrh Tcl_CreateCommand(interp, "md5file", (Tcl_CmdProc*)md5file_cmd, 337357a0227fSdrh MD5DigestToBase16, 0); 337457a0227fSdrh Tcl_CreateCommand(interp, "md5file-10x8", (Tcl_CmdProc*)md5file_cmd, 337557a0227fSdrh MD5DigestToBase10x8, 0); 337657a0227fSdrh return TCL_OK; 337757a0227fSdrh } 337857a0227fSdrh #endif /* defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) */ 337957a0227fSdrh 338057a0227fSdrh #if defined(SQLITE_TEST) 338157a0227fSdrh /* 338257a0227fSdrh ** During testing, the special md5sum() aggregate function is available. 338357a0227fSdrh ** inside SQLite. The following routines implement that function. 338457a0227fSdrh */ 338557a0227fSdrh static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){ 338657a0227fSdrh MD5Context *p; 338757a0227fSdrh int i; 338857a0227fSdrh if( argc<1 ) return; 338957a0227fSdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 339057a0227fSdrh if( p==0 ) return; 339157a0227fSdrh if( !p->isInit ){ 339257a0227fSdrh MD5Init(p); 339357a0227fSdrh } 339457a0227fSdrh for(i=0; i<argc; i++){ 339557a0227fSdrh const char *zData = (char*)sqlite3_value_text(argv[i]); 339657a0227fSdrh if( zData ){ 339757a0227fSdrh MD5Update(p, (unsigned char*)zData, strlen(zData)); 339857a0227fSdrh } 339957a0227fSdrh } 340057a0227fSdrh } 340157a0227fSdrh static void md5finalize(sqlite3_context *context){ 340257a0227fSdrh MD5Context *p; 340357a0227fSdrh unsigned char digest[16]; 340457a0227fSdrh char zBuf[33]; 340557a0227fSdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 340657a0227fSdrh MD5Final(digest,p); 340757a0227fSdrh MD5DigestToBase16(digest, zBuf); 340857a0227fSdrh sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 340957a0227fSdrh } 341057a0227fSdrh int Md5_Register(sqlite3 *db){ 341157a0227fSdrh int rc = sqlite3_create_function(db, "md5sum", -1, SQLITE_UTF8, 0, 0, 341257a0227fSdrh md5step, md5finalize); 341357a0227fSdrh sqlite3_overload_function(db, "md5sum", -1); /* To exercise this API */ 341457a0227fSdrh return rc; 341557a0227fSdrh } 341657a0227fSdrh #endif /* defined(SQLITE_TEST) */ 341757a0227fSdrh 341857a0227fSdrh 3419348784efSdrh /* 34203e27c026Sdrh ** If the macro TCLSH is one, then put in code this for the 34213e27c026Sdrh ** "main" routine that will initialize Tcl and take input from 34223570ad93Sdrh ** standard input, or if a file is named on the command line 34233570ad93Sdrh ** the TCL interpreter reads and evaluates that file. 3424348784efSdrh */ 34253e27c026Sdrh #if TCLSH==1 3426348784efSdrh static char zMainloop[] = 3427348784efSdrh "set line {}\n" 3428348784efSdrh "while {![eof stdin]} {\n" 3429348784efSdrh "if {$line!=\"\"} {\n" 3430348784efSdrh "puts -nonewline \"> \"\n" 3431348784efSdrh "} else {\n" 3432348784efSdrh "puts -nonewline \"% \"\n" 3433348784efSdrh "}\n" 3434348784efSdrh "flush stdout\n" 3435348784efSdrh "append line [gets stdin]\n" 3436348784efSdrh "if {[info complete $line]} {\n" 3437348784efSdrh "if {[catch {uplevel #0 $line} result]} {\n" 3438348784efSdrh "puts stderr \"Error: $result\"\n" 3439348784efSdrh "} elseif {$result!=\"\"} {\n" 3440348784efSdrh "puts $result\n" 3441348784efSdrh "}\n" 3442348784efSdrh "set line {}\n" 3443348784efSdrh "} else {\n" 3444348784efSdrh "append line \\n\n" 3445348784efSdrh "}\n" 3446348784efSdrh "}\n" 3447348784efSdrh ; 34483e27c026Sdrh #endif 34493e27c026Sdrh 3450348784efSdrh #define TCLSH_MAIN main /* Needed to fake out mktclapp */ 3451348784efSdrh int TCLSH_MAIN(int argc, char **argv){ 3452348784efSdrh Tcl_Interp *interp; 34530a549071Sdanielk1977 34540a549071Sdanielk1977 /* Call sqlite3_shutdown() once before doing anything else. This is to 34550a549071Sdanielk1977 ** test that sqlite3_shutdown() can be safely called by a process before 34560a549071Sdanielk1977 ** sqlite3_initialize() is. */ 34570a549071Sdanielk1977 sqlite3_shutdown(); 34580a549071Sdanielk1977 3459297ecf14Sdrh Tcl_FindExecutable(argv[0]); 3460348784efSdrh interp = Tcl_CreateInterp(); 346138f8271fSdrh Sqlite3_Init(interp); 346257a0227fSdrh #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) 346357a0227fSdrh Md5_Init(interp); 346457a0227fSdrh #endif 3465d9b0257aSdrh #ifdef SQLITE_TEST 3466d1bf3512Sdrh { 34672f999a67Sdrh extern int Sqliteconfig_Init(Tcl_Interp*); 3468d1bf3512Sdrh extern int Sqlitetest1_Init(Tcl_Interp*); 34695c4d9703Sdrh extern int Sqlitetest2_Init(Tcl_Interp*); 34705c4d9703Sdrh extern int Sqlitetest3_Init(Tcl_Interp*); 3471a6064dcfSdrh extern int Sqlitetest4_Init(Tcl_Interp*); 3472998b56c3Sdanielk1977 extern int Sqlitetest5_Init(Tcl_Interp*); 34739c06c953Sdrh extern int Sqlitetest6_Init(Tcl_Interp*); 347429c636bcSdrh extern int Sqlitetest7_Init(Tcl_Interp*); 3475b9bb7c18Sdrh extern int Sqlitetest8_Init(Tcl_Interp*); 3476a713f2c3Sdanielk1977 extern int Sqlitetest9_Init(Tcl_Interp*); 34772366940dSdrh extern int Sqlitetestasync_Init(Tcl_Interp*); 34781409be69Sdrh extern int Sqlitetest_autoext_Init(Tcl_Interp*); 3479984bfaa4Sdrh extern int Sqlitetest_func_Init(Tcl_Interp*); 348015926590Sdrh extern int Sqlitetest_hexio_Init(Tcl_Interp*); 3481e1ab2193Sdan extern int Sqlitetest_init_Init(Tcl_Interp*); 34822f999a67Sdrh extern int Sqlitetest_malloc_Init(Tcl_Interp*); 34831a9ed0b2Sdanielk1977 extern int Sqlitetest_mutex_Init(Tcl_Interp*); 34842f999a67Sdrh extern int Sqlitetestschema_Init(Tcl_Interp*); 34852f999a67Sdrh extern int Sqlitetestsse_Init(Tcl_Interp*); 34862f999a67Sdrh extern int Sqlitetesttclvar_Init(Tcl_Interp*); 348744918fa0Sdanielk1977 extern int SqlitetestThread_Init(Tcl_Interp*); 3488a15db353Sdanielk1977 extern int SqlitetestOnefile_Init(); 34895d1f5aa6Sdanielk1977 extern int SqlitetestOsinst_Init(Tcl_Interp*); 34900410302eSdanielk1977 extern int Sqlitetestbackup_Init(Tcl_Interp*); 3491*522efc62Sdrh extern int Sqlitetestintarray_Init(Tcl_Interp*); 34922e66f0b9Sdrh 34932f999a67Sdrh Sqliteconfig_Init(interp); 34946490bebdSdanielk1977 Sqlitetest1_Init(interp); 34955c4d9703Sdrh Sqlitetest2_Init(interp); 3496de647130Sdrh Sqlitetest3_Init(interp); 3497fc57d7bfSdanielk1977 Sqlitetest4_Init(interp); 3498998b56c3Sdanielk1977 Sqlitetest5_Init(interp); 34999c06c953Sdrh Sqlitetest6_Init(interp); 350029c636bcSdrh Sqlitetest7_Init(interp); 3501b9bb7c18Sdrh Sqlitetest8_Init(interp); 3502a713f2c3Sdanielk1977 Sqlitetest9_Init(interp); 35032366940dSdrh Sqlitetestasync_Init(interp); 35041409be69Sdrh Sqlitetest_autoext_Init(interp); 3505984bfaa4Sdrh Sqlitetest_func_Init(interp); 350615926590Sdrh Sqlitetest_hexio_Init(interp); 3507e1ab2193Sdan Sqlitetest_init_Init(interp); 35082f999a67Sdrh Sqlitetest_malloc_Init(interp); 35091a9ed0b2Sdanielk1977 Sqlitetest_mutex_Init(interp); 35102f999a67Sdrh Sqlitetestschema_Init(interp); 35112f999a67Sdrh Sqlitetesttclvar_Init(interp); 351244918fa0Sdanielk1977 SqlitetestThread_Init(interp); 3513a15db353Sdanielk1977 SqlitetestOnefile_Init(interp); 35145d1f5aa6Sdanielk1977 SqlitetestOsinst_Init(interp); 35150410302eSdanielk1977 Sqlitetestbackup_Init(interp); 3516*522efc62Sdrh Sqlitetestintarray_Init(interp); 3517a15db353Sdanielk1977 351889dec819Sdrh #ifdef SQLITE_SSE 35192e66f0b9Sdrh Sqlitetestsse_Init(interp); 35202e66f0b9Sdrh #endif 3521d1bf3512Sdrh } 3522d1bf3512Sdrh #endif 3523c7285978Sdrh if( argc>=2 ){ 3524348784efSdrh int i; 3525ad42c3a3Sshess char zArgc[32]; 3526ad42c3a3Sshess sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH)); 3527ad42c3a3Sshess Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY); 3528348784efSdrh Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); 3529348784efSdrh Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); 353061212b69Sdrh for(i=3-TCLSH; i<argc; i++){ 3531348784efSdrh Tcl_SetVar(interp, "argv", argv[i], 3532348784efSdrh TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); 3533348784efSdrh } 3534c7285978Sdrh if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ 35350de8c112Sdrh const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); 3536a81c64a2Sdrh if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp); 3537c61053b7Sdrh fprintf(stderr,"%s: %s\n", *argv, zInfo); 3538348784efSdrh return 1; 3539348784efSdrh } 35403e27c026Sdrh } 3541c7285978Sdrh if( argc<=1 ){ 3542348784efSdrh Tcl_GlobalEval(interp, zMainloop); 3543348784efSdrh } 3544348784efSdrh return 0; 3545348784efSdrh } 3546348784efSdrh #endif /* TCLSH */ 3547