Index: third_party/sqlite/src/src/tclsqlite.c |
diff --git a/third_party/sqlite/src/src/tclsqlite.c b/third_party/sqlite/src/src/tclsqlite.c |
index 223a7dfd728d220c9d0d574f9a9cfcf2e50e4fa3..575651d7e561d11c8e54280377b72ff6bddd75ec 100644 |
--- a/third_party/sqlite/src/src/tclsqlite.c |
+++ b/third_party/sqlite/src/src/tclsqlite.c |
@@ -11,6 +11,19 @@ |
************************************************************************* |
** A TCL Interface to SQLite. Append this file to sqlite3.c and |
** compile the whole thing to build a TCL-enabled version of SQLite. |
+** |
+** Compile-time options: |
+** |
+** -DTCLSH=1 Add a "main()" routine that works as a tclsh. |
+** |
+** -DSQLITE_TCLMD5 When used in conjuction with -DTCLSH=1, add |
+** four new commands to the TCL interpreter for |
+** generating MD5 checksums: md5, md5file, |
+** md5-10x8, and md5file-10x8. |
+** |
+** -DSQLITE_TEST When used in conjuction with -DTCLSH=1, add |
+** hundreds of new commands used for testing |
+** SQLite. This option implies -DSQLITE_TCLMD5. |
*/ |
#include "tcl.h" |
#include <errno.h> |
@@ -20,12 +33,13 @@ |
** appended to the amalgamation. |
*/ |
#ifndef SQLITE_AMALGAMATION |
-# include "sqliteInt.h" |
+# include "sqlite3.h" |
# include <stdlib.h> |
# include <string.h> |
# include <assert.h> |
-# include <ctype.h> |
+ typedef unsigned char u8; |
#endif |
+#include <ctype.h> |
/* |
* Windows needs to know which symbols to export. Unix does not. |
@@ -84,6 +98,8 @@ struct SqlPreparedStmt { |
sqlite3_stmt *pStmt; /* The prepared statement */ |
int nSql; /* chars in zSql[] */ |
const char *zSql; /* Text of the SQL statement */ |
+ int nParm; /* Size of apParm array */ |
+ Tcl_Obj **apParm; /* Array of referenced object pointers */ |
}; |
typedef struct IncrblobChannel IncrblobChannel; |
@@ -107,6 +123,7 @@ struct SqliteDb { |
SqlFunc *pFunc; /* List of SQL functions */ |
Tcl_Obj *pUpdateHook; /* Update hook script (if any) */ |
Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */ |
+ Tcl_Obj *pWalHook; /* WAL hook script (if any) */ |
Tcl_Obj *pUnlockNotify; /* Unlock notify script (if any) */ |
SqlCollate *pCollate; /* List of SQL collation functions */ |
int rc; /* Return code of most recent sqlite3_exec() */ |
@@ -116,7 +133,7 @@ struct SqliteDb { |
int maxStmt; /* The next maximum number of stmtList */ |
int nStmt; /* Number of statements in stmtList */ |
IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */ |
- int nStep, nSort; /* Statistics for most recent operation */ |
+ int nStep, nSort, nIndex; /* Statistics for most recent operation */ |
int nTransaction; /* Number of nested [transaction] methods */ |
}; |
@@ -469,6 +486,9 @@ static void DbDeleteCmd(void *db){ |
if( pDb->pRollbackHook ){ |
Tcl_DecrRefCount(pDb->pRollbackHook); |
} |
+ if( pDb->pWalHook ){ |
+ Tcl_DecrRefCount(pDb->pWalHook); |
+ } |
if( pDb->pCollateNeeded ){ |
Tcl_DecrRefCount(pDb->pCollateNeeded); |
} |
@@ -573,6 +593,35 @@ static void DbRollbackHandler(void *clientData){ |
} |
} |
+/* |
+** This procedure handles wal_hook callbacks. |
+*/ |
+static int DbWalHandler( |
+ void *clientData, |
+ sqlite3 *db, |
+ const char *zDb, |
+ int nEntry |
+){ |
+ int ret = SQLITE_OK; |
+ Tcl_Obj *p; |
+ SqliteDb *pDb = (SqliteDb*)clientData; |
+ Tcl_Interp *interp = pDb->interp; |
+ assert(pDb->pWalHook); |
+ |
+ p = Tcl_DuplicateObj(pDb->pWalHook); |
+ Tcl_IncrRefCount(p); |
+ Tcl_ListObjAppendElement(interp, p, Tcl_NewStringObj(zDb, -1)); |
+ Tcl_ListObjAppendElement(interp, p, Tcl_NewIntObj(nEntry)); |
+ if( TCL_OK!=Tcl_EvalObjEx(interp, p, 0) |
+ || TCL_OK!=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &ret) |
+ ){ |
+ Tcl_BackgroundError(interp); |
+ } |
+ Tcl_DecrRefCount(p); |
+ |
+ return ret; |
+} |
+ |
#if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_UNLOCK_NOTIFY) |
static void setTestUnlockNotifyVars(Tcl_Interp *interp, int iArg, int nArg){ |
char zBuf[64]; |
@@ -621,6 +670,7 @@ static void DbUpdateHandler( |
Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); |
Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid)); |
Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); |
+ Tcl_DecrRefCount(pCmd); |
} |
static void tclCollateNeeded( |
@@ -755,7 +805,7 @@ static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ |
Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); |
int n; |
u8 *data; |
- char *zType = pVar->typePtr ? pVar->typePtr->name : ""; |
+ const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); |
char c = zType[0]; |
if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ |
/* Only return a BLOB type if the Tcl variable is a bytearray and |
@@ -932,63 +982,572 @@ static char *local_getline(char *zPrompt, FILE *in){ |
/* |
-** Figure out the column names for the data returned by the statement |
-** passed as the second argument. |
+** This function is part of the implementation of the command: |
+** |
+** $db transaction [-deferred|-immediate|-exclusive] SCRIPT |
+** |
+** It is invoked after evaluating the script SCRIPT to commit or rollback |
+** the transaction or savepoint opened by the [transaction] command. |
+*/ |
+static int DbTransPostCmd( |
+ ClientData data[], /* data[0] is the Sqlite3Db* for $db */ |
+ Tcl_Interp *interp, /* Tcl interpreter */ |
+ int result /* Result of evaluating SCRIPT */ |
+){ |
+ static const char *azEnd[] = { |
+ "RELEASE _tcl_transaction", /* rc==TCL_ERROR, nTransaction!=0 */ |
+ "COMMIT", /* rc!=TCL_ERROR, nTransaction==0 */ |
+ "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction", |
+ "ROLLBACK" /* rc==TCL_ERROR, nTransaction==0 */ |
+ }; |
+ SqliteDb *pDb = (SqliteDb*)data[0]; |
+ int rc = result; |
+ const char *zEnd; |
+ |
+ pDb->nTransaction--; |
+ zEnd = azEnd[(rc==TCL_ERROR)*2 + (pDb->nTransaction==0)]; |
+ |
+ pDb->disableAuth++; |
+ if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){ |
+ /* This is a tricky scenario to handle. The most likely cause of an |
+ ** error is that the exec() above was an attempt to commit the |
+ ** top-level transaction that returned SQLITE_BUSY. Or, less likely, |
+ ** that an IO-error has occured. In either case, throw a Tcl exception |
+ ** and try to rollback the transaction. |
+ ** |
+ ** But it could also be that the user executed one or more BEGIN, |
+ ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing |
+ ** this method's logic. Not clear how this would be best handled. |
+ */ |
+ if( rc!=TCL_ERROR ){ |
+ Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); |
+ rc = TCL_ERROR; |
+ } |
+ sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0); |
+ } |
+ pDb->disableAuth--; |
+ |
+ return rc; |
+} |
+ |
+/* |
+** Search the cache for a prepared-statement object that implements the |
+** first SQL statement in the buffer pointed to by parameter zIn. If |
+** no such prepared-statement can be found, allocate and prepare a new |
+** one. In either case, bind the current values of the relevant Tcl |
+** variables to any $var, :var or @var variables in the statement. Before |
+** returning, set *ppPreStmt to point to the prepared-statement object. |
+** |
+** Output parameter *pzOut is set to point to the next SQL statement in |
+** buffer zIn, or to the '\0' byte at the end of zIn if there is no |
+** next statement. |
+** |
+** If successful, TCL_OK is returned. Otherwise, TCL_ERROR is returned |
+** and an error message loaded into interpreter pDb->interp. |
+*/ |
+static int dbPrepareAndBind( |
+ SqliteDb *pDb, /* Database object */ |
+ char const *zIn, /* SQL to compile */ |
+ char const **pzOut, /* OUT: Pointer to next SQL statement */ |
+ SqlPreparedStmt **ppPreStmt /* OUT: Object used to cache statement */ |
+){ |
+ const char *zSql = zIn; /* Pointer to first SQL statement in zIn */ |
+ sqlite3_stmt *pStmt; /* Prepared statement object */ |
+ SqlPreparedStmt *pPreStmt; /* Pointer to cached statement */ |
+ int nSql; /* Length of zSql in bytes */ |
+ int nVar; /* Number of variables in statement */ |
+ int iParm = 0; /* Next free entry in apParm */ |
+ int i; |
+ Tcl_Interp *interp = pDb->interp; |
+ |
+ *ppPreStmt = 0; |
+ |
+ /* Trim spaces from the start of zSql and calculate the remaining length. */ |
+ while( isspace(zSql[0]) ){ zSql++; } |
+ nSql = strlen30(zSql); |
+ |
+ for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){ |
+ int n = pPreStmt->nSql; |
+ if( nSql>=n |
+ && memcmp(pPreStmt->zSql, zSql, n)==0 |
+ && (zSql[n]==0 || zSql[n-1]==';') |
+ ){ |
+ pStmt = pPreStmt->pStmt; |
+ *pzOut = &zSql[pPreStmt->nSql]; |
+ |
+ /* When a prepared statement is found, unlink it from the |
+ ** cache list. It will later be added back to the beginning |
+ ** of the cache list in order to implement LRU replacement. |
+ */ |
+ if( pPreStmt->pPrev ){ |
+ pPreStmt->pPrev->pNext = pPreStmt->pNext; |
+ }else{ |
+ pDb->stmtList = pPreStmt->pNext; |
+ } |
+ if( pPreStmt->pNext ){ |
+ pPreStmt->pNext->pPrev = pPreStmt->pPrev; |
+ }else{ |
+ pDb->stmtLast = pPreStmt->pPrev; |
+ } |
+ pDb->nStmt--; |
+ nVar = sqlite3_bind_parameter_count(pStmt); |
+ break; |
+ } |
+ } |
+ |
+ /* If no prepared statement was found. Compile the SQL text. Also allocate |
+ ** a new SqlPreparedStmt structure. */ |
+ if( pPreStmt==0 ){ |
+ int nByte; |
+ |
+ if( SQLITE_OK!=sqlite3_prepare_v2(pDb->db, zSql, -1, &pStmt, pzOut) ){ |
+ Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
+ return TCL_ERROR; |
+ } |
+ if( pStmt==0 ){ |
+ if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ |
+ /* A compile-time error in the statement. */ |
+ Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
+ return TCL_ERROR; |
+ }else{ |
+ /* The statement was a no-op. Continue to the next statement |
+ ** in the SQL string. |
+ */ |
+ return TCL_OK; |
+ } |
+ } |
+ |
+ assert( pPreStmt==0 ); |
+ nVar = sqlite3_bind_parameter_count(pStmt); |
+ nByte = sizeof(SqlPreparedStmt) + nVar*sizeof(Tcl_Obj *); |
+ pPreStmt = (SqlPreparedStmt*)Tcl_Alloc(nByte); |
+ memset(pPreStmt, 0, nByte); |
+ |
+ pPreStmt->pStmt = pStmt; |
+ pPreStmt->nSql = (*pzOut - zSql); |
+ pPreStmt->zSql = sqlite3_sql(pStmt); |
+ pPreStmt->apParm = (Tcl_Obj **)&pPreStmt[1]; |
+ } |
+ assert( pPreStmt ); |
+ assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql ); |
+ assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) ); |
+ |
+ /* Bind values to parameters that begin with $ or : */ |
+ for(i=1; i<=nVar; i++){ |
+ const char *zVar = sqlite3_bind_parameter_name(pStmt, i); |
+ if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){ |
+ Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); |
+ if( pVar ){ |
+ int n; |
+ u8 *data; |
+ const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); |
+ char c = zType[0]; |
+ if( zVar[0]=='@' || |
+ (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){ |
+ /* Load a BLOB type if the Tcl variable is a bytearray and |
+ ** it has no string representation or the host |
+ ** parameter name begins with "@". */ |
+ data = Tcl_GetByteArrayFromObj(pVar, &n); |
+ sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); |
+ Tcl_IncrRefCount(pVar); |
+ pPreStmt->apParm[iParm++] = pVar; |
+ }else if( c=='b' && strcmp(zType,"boolean")==0 ){ |
+ Tcl_GetIntFromObj(interp, pVar, &n); |
+ sqlite3_bind_int(pStmt, i, n); |
+ }else if( c=='d' && strcmp(zType,"double")==0 ){ |
+ double r; |
+ Tcl_GetDoubleFromObj(interp, pVar, &r); |
+ sqlite3_bind_double(pStmt, i, r); |
+ }else if( (c=='w' && strcmp(zType,"wideInt")==0) || |
+ (c=='i' && strcmp(zType,"int")==0) ){ |
+ Tcl_WideInt v; |
+ Tcl_GetWideIntFromObj(interp, pVar, &v); |
+ sqlite3_bind_int64(pStmt, i, v); |
+ }else{ |
+ data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); |
+ sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); |
+ Tcl_IncrRefCount(pVar); |
+ pPreStmt->apParm[iParm++] = pVar; |
+ } |
+ }else{ |
+ sqlite3_bind_null(pStmt, i); |
+ } |
+ } |
+ } |
+ pPreStmt->nParm = iParm; |
+ *ppPreStmt = pPreStmt; |
+ |
+ return TCL_OK; |
+} |
+ |
+ |
+/* |
+** Release a statement reference obtained by calling dbPrepareAndBind(). |
+** There should be exactly one call to this function for each call to |
+** dbPrepareAndBind(). |
** |
-** If parameter papColName is not NULL, then *papColName is set to point |
-** at an array allocated using Tcl_Alloc(). It is the callers responsibility |
-** to free this array using Tcl_Free(), and to decrement the reference |
-** count of each Tcl_Obj* member of the array. |
+** If the discard parameter is non-zero, then the statement is deleted |
+** immediately. Otherwise it is added to the LRU list and may be returned |
+** by a subsequent call to dbPrepareAndBind(). |
+*/ |
+static void dbReleaseStmt( |
+ SqliteDb *pDb, /* Database handle */ |
+ SqlPreparedStmt *pPreStmt, /* Prepared statement handle to release */ |
+ int discard /* True to delete (not cache) the pPreStmt */ |
+){ |
+ int i; |
+ |
+ /* Free the bound string and blob parameters */ |
+ for(i=0; i<pPreStmt->nParm; i++){ |
+ Tcl_DecrRefCount(pPreStmt->apParm[i]); |
+ } |
+ pPreStmt->nParm = 0; |
+ |
+ if( pDb->maxStmt<=0 || discard ){ |
+ /* If the cache is turned off, deallocated the statement */ |
+ sqlite3_finalize(pPreStmt->pStmt); |
+ Tcl_Free((char *)pPreStmt); |
+ }else{ |
+ /* Add the prepared statement to the beginning of the cache list. */ |
+ pPreStmt->pNext = pDb->stmtList; |
+ pPreStmt->pPrev = 0; |
+ if( pDb->stmtList ){ |
+ pDb->stmtList->pPrev = pPreStmt; |
+ } |
+ pDb->stmtList = pPreStmt; |
+ if( pDb->stmtLast==0 ){ |
+ assert( pDb->nStmt==0 ); |
+ pDb->stmtLast = pPreStmt; |
+ }else{ |
+ assert( pDb->nStmt>0 ); |
+ } |
+ pDb->nStmt++; |
+ |
+ /* If we have too many statement in cache, remove the surplus from |
+ ** the end of the cache list. */ |
+ while( pDb->nStmt>pDb->maxStmt ){ |
+ sqlite3_finalize(pDb->stmtLast->pStmt); |
+ pDb->stmtLast = pDb->stmtLast->pPrev; |
+ Tcl_Free((char*)pDb->stmtLast->pNext); |
+ pDb->stmtLast->pNext = 0; |
+ pDb->nStmt--; |
+ } |
+ } |
+} |
+ |
+/* |
+** Structure used with dbEvalXXX() functions: |
** |
-** The return value of this function is the number of columns of data |
-** returned by pStmt (and hence the size of the *papColName array). |
+** dbEvalInit() |
+** dbEvalStep() |
+** dbEvalFinalize() |
+** dbEvalRowInfo() |
+** dbEvalColumnValue() |
+*/ |
+typedef struct DbEvalContext DbEvalContext; |
+struct DbEvalContext { |
+ SqliteDb *pDb; /* Database handle */ |
+ Tcl_Obj *pSql; /* Object holding string zSql */ |
+ const char *zSql; /* Remaining SQL to execute */ |
+ SqlPreparedStmt *pPreStmt; /* Current statement */ |
+ int nCol; /* Number of columns returned by pStmt */ |
+ Tcl_Obj *pArray; /* Name of array variable */ |
+ Tcl_Obj **apColName; /* Array of column names */ |
+}; |
+ |
+/* |
+** Release any cache of column names currently held as part of |
+** the DbEvalContext structure passed as the first argument. |
+*/ |
+static void dbReleaseColumnNames(DbEvalContext *p){ |
+ if( p->apColName ){ |
+ int i; |
+ for(i=0; i<p->nCol; i++){ |
+ Tcl_DecrRefCount(p->apColName[i]); |
+ } |
+ Tcl_Free((char *)p->apColName); |
+ p->apColName = 0; |
+ } |
+ p->nCol = 0; |
+} |
+ |
+/* |
+** Initialize a DbEvalContext structure. |
** |
** If pArray is not NULL, then it contains the name of a Tcl array |
** variable. The "*" member of this array is set to a list containing |
-** the names of the columns returned by the statement, in order from |
-** left to right. e.g. if the names of the returned columns are a, b and |
-** c, it does the equivalent of the tcl command: |
+** the names of the columns returned by the statement as part of each |
+** call to dbEvalStep(), in order from left to right. e.g. if the names |
+** of the returned columns are a, b and c, it does the equivalent of the |
+** tcl command: |
** |
** set ${pArray}(*) {a b c} |
*/ |
-static int |
-computeColumnNames( |
- Tcl_Interp *interp, |
- sqlite3_stmt *pStmt, /* SQL statement */ |
- Tcl_Obj ***papColName, /* OUT: Array of column names */ |
- Tcl_Obj *pArray /* Name of array variable (may be null) */ |
+static void dbEvalInit( |
+ DbEvalContext *p, /* Pointer to structure to initialize */ |
+ SqliteDb *pDb, /* Database handle */ |
+ Tcl_Obj *pSql, /* Object containing SQL script */ |
+ Tcl_Obj *pArray /* Name of Tcl array to set (*) element of */ |
){ |
- int nCol; |
+ memset(p, 0, sizeof(DbEvalContext)); |
+ p->pDb = pDb; |
+ p->zSql = Tcl_GetString(pSql); |
+ p->pSql = pSql; |
+ Tcl_IncrRefCount(pSql); |
+ if( pArray ){ |
+ p->pArray = pArray; |
+ Tcl_IncrRefCount(pArray); |
+ } |
+} |
+/* |
+** Obtain information about the row that the DbEvalContext passed as the |
+** first argument currently points to. |
+*/ |
+static void dbEvalRowInfo( |
+ DbEvalContext *p, /* Evaluation context */ |
+ int *pnCol, /* OUT: Number of column names */ |
+ Tcl_Obj ***papColName /* OUT: Array of column names */ |
+){ |
/* Compute column names */ |
- nCol = sqlite3_column_count(pStmt); |
- if( papColName ){ |
- int i; |
- Tcl_Obj **apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); |
- for(i=0; i<nCol; i++){ |
- apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i)); |
- Tcl_IncrRefCount(apColName[i]); |
+ if( 0==p->apColName ){ |
+ sqlite3_stmt *pStmt = p->pPreStmt->pStmt; |
+ int i; /* Iterator variable */ |
+ int nCol; /* Number of columns returned by pStmt */ |
+ Tcl_Obj **apColName = 0; /* Array of column names */ |
+ |
+ p->nCol = nCol = sqlite3_column_count(pStmt); |
+ if( nCol>0 && (papColName || p->pArray) ){ |
+ apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); |
+ for(i=0; i<nCol; i++){ |
+ apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i)); |
+ Tcl_IncrRefCount(apColName[i]); |
+ } |
+ p->apColName = apColName; |
} |
/* If results are being stored in an array variable, then create |
** the array(*) entry for that array |
*/ |
- if( pArray ){ |
+ if( p->pArray ){ |
+ Tcl_Interp *interp = p->pDb->interp; |
Tcl_Obj *pColList = Tcl_NewObj(); |
Tcl_Obj *pStar = Tcl_NewStringObj("*", -1); |
- Tcl_IncrRefCount(pColList); |
+ |
for(i=0; i<nCol; i++){ |
Tcl_ListObjAppendElement(interp, pColList, apColName[i]); |
} |
Tcl_IncrRefCount(pStar); |
- Tcl_ObjSetVar2(interp, pArray, pStar, pColList,0); |
- Tcl_DecrRefCount(pColList); |
+ Tcl_ObjSetVar2(interp, p->pArray, pStar, pColList, 0); |
Tcl_DecrRefCount(pStar); |
} |
- *papColName = apColName; |
} |
- return nCol; |
+ if( papColName ){ |
+ *papColName = p->apColName; |
+ } |
+ if( pnCol ){ |
+ *pnCol = p->nCol; |
+ } |
+} |
+ |
+/* |
+** Return one of TCL_OK, TCL_BREAK or TCL_ERROR. If TCL_ERROR is |
+** returned, then an error message is stored in the interpreter before |
+** returning. |
+** |
+** A return value of TCL_OK means there is a row of data available. The |
+** data may be accessed using dbEvalRowInfo() and dbEvalColumnValue(). This |
+** is analogous to a return of SQLITE_ROW from sqlite3_step(). If TCL_BREAK |
+** is returned, then the SQL script has finished executing and there are |
+** no further rows available. This is similar to SQLITE_DONE. |
+*/ |
+static int dbEvalStep(DbEvalContext *p){ |
+ while( p->zSql[0] || p->pPreStmt ){ |
+ int rc; |
+ if( p->pPreStmt==0 ){ |
+ rc = dbPrepareAndBind(p->pDb, p->zSql, &p->zSql, &p->pPreStmt); |
+ if( rc!=TCL_OK ) return rc; |
+ }else{ |
+ int rcs; |
+ SqliteDb *pDb = p->pDb; |
+ SqlPreparedStmt *pPreStmt = p->pPreStmt; |
+ sqlite3_stmt *pStmt = pPreStmt->pStmt; |
+ |
+ rcs = sqlite3_step(pStmt); |
+ if( rcs==SQLITE_ROW ){ |
+ return TCL_OK; |
+ } |
+ if( p->pArray ){ |
+ dbEvalRowInfo(p, 0, 0); |
+ } |
+ rcs = sqlite3_reset(pStmt); |
+ |
+ pDb->nStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_FULLSCAN_STEP,1); |
+ pDb->nSort = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_SORT,1); |
+ pDb->nIndex = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_AUTOINDEX,1); |
+ dbReleaseColumnNames(p); |
+ p->pPreStmt = 0; |
+ |
+ if( rcs!=SQLITE_OK ){ |
+ /* If a run-time error occurs, report the error and stop reading |
+ ** the SQL. */ |
+ Tcl_SetObjResult(pDb->interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
+ dbReleaseStmt(pDb, pPreStmt, 1); |
+ return TCL_ERROR; |
+ }else{ |
+ dbReleaseStmt(pDb, pPreStmt, 0); |
+ } |
+ } |
+ } |
+ |
+ /* Finished */ |
+ return TCL_BREAK; |
+} |
+ |
+/* |
+** Free all resources currently held by the DbEvalContext structure passed |
+** as the first argument. There should be exactly one call to this function |
+** for each call to dbEvalInit(). |
+*/ |
+static void dbEvalFinalize(DbEvalContext *p){ |
+ if( p->pPreStmt ){ |
+ sqlite3_reset(p->pPreStmt->pStmt); |
+ dbReleaseStmt(p->pDb, p->pPreStmt, 0); |
+ p->pPreStmt = 0; |
+ } |
+ if( p->pArray ){ |
+ Tcl_DecrRefCount(p->pArray); |
+ p->pArray = 0; |
+ } |
+ Tcl_DecrRefCount(p->pSql); |
+ dbReleaseColumnNames(p); |
+} |
+ |
+/* |
+** Return a pointer to a Tcl_Obj structure with ref-count 0 that contains |
+** the value for the iCol'th column of the row currently pointed to by |
+** the DbEvalContext structure passed as the first argument. |
+*/ |
+static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){ |
+ sqlite3_stmt *pStmt = p->pPreStmt->pStmt; |
+ switch( sqlite3_column_type(pStmt, iCol) ){ |
+ case SQLITE_BLOB: { |
+ int bytes = sqlite3_column_bytes(pStmt, iCol); |
+ const char *zBlob = sqlite3_column_blob(pStmt, iCol); |
+ if( !zBlob ) bytes = 0; |
+ return Tcl_NewByteArrayObj((u8*)zBlob, bytes); |
+ } |
+ case SQLITE_INTEGER: { |
+ sqlite_int64 v = sqlite3_column_int64(pStmt, iCol); |
+ if( v>=-2147483647 && v<=2147483647 ){ |
+ return Tcl_NewIntObj(v); |
+ }else{ |
+ return Tcl_NewWideIntObj(v); |
+ } |
+ } |
+ case SQLITE_FLOAT: { |
+ return Tcl_NewDoubleObj(sqlite3_column_double(pStmt, iCol)); |
+ } |
+ case SQLITE_NULL: { |
+ return dbTextToObj(p->pDb->zNull); |
+ } |
+ } |
+ |
+ return dbTextToObj((char *)sqlite3_column_text(pStmt, iCol)); |
+} |
+ |
+/* |
+** If using Tcl version 8.6 or greater, use the NR functions to avoid |
+** recursive evalution of scripts by the [db eval] and [db trans] |
+** commands. Even if the headers used while compiling the extension |
+** are 8.6 or newer, the code still tests the Tcl version at runtime. |
+** This allows stubs-enabled builds to be used with older Tcl libraries. |
+*/ |
+#if TCL_MAJOR_VERSION>8 || (TCL_MAJOR_VERSION==8 && TCL_MINOR_VERSION>=6) |
+# define SQLITE_TCL_NRE 1 |
+static int DbUseNre(void){ |
+ int major, minor; |
+ Tcl_GetVersion(&major, &minor, 0, 0); |
+ return( (major==8 && minor>=6) || major>8 ); |
+} |
+#else |
+/* |
+** Compiling using headers earlier than 8.6. In this case NR cannot be |
+** used, so DbUseNre() to always return zero. Add #defines for the other |
+** Tcl_NRxxx() functions to prevent them from causing compilation errors, |
+** even though the only invocations of them are within conditional blocks |
+** of the form: |
+** |
+** if( DbUseNre() ) { ... } |
+*/ |
+# define SQLITE_TCL_NRE 0 |
+# define DbUseNre() 0 |
+# define Tcl_NRAddCallback(a,b,c,d,e,f) 0 |
+# define Tcl_NREvalObj(a,b,c) 0 |
+# define Tcl_NRCreateCommand(a,b,c,d,e,f) 0 |
+#endif |
+ |
+/* |
+** This function is part of the implementation of the command: |
+** |
+** $db eval SQL ?ARRAYNAME? SCRIPT |
+*/ |
+static int DbEvalNextCmd( |
+ ClientData data[], /* data[0] is the (DbEvalContext*) */ |
+ Tcl_Interp *interp, /* Tcl interpreter */ |
+ int result /* Result so far */ |
+){ |
+ int rc = result; /* Return code */ |
+ |
+ /* The first element of the data[] array is a pointer to a DbEvalContext |
+ ** structure allocated using Tcl_Alloc(). The second element of data[] |
+ ** is a pointer to a Tcl_Obj containing the script to run for each row |
+ ** returned by the queries encapsulated in data[0]. */ |
+ DbEvalContext *p = (DbEvalContext *)data[0]; |
+ Tcl_Obj *pScript = (Tcl_Obj *)data[1]; |
+ Tcl_Obj *pArray = p->pArray; |
+ |
+ while( (rc==TCL_OK || rc==TCL_CONTINUE) && TCL_OK==(rc = dbEvalStep(p)) ){ |
+ int i; |
+ int nCol; |
+ Tcl_Obj **apColName; |
+ dbEvalRowInfo(p, &nCol, &apColName); |
+ for(i=0; i<nCol; i++){ |
+ Tcl_Obj *pVal = dbEvalColumnValue(p, i); |
+ if( pArray==0 ){ |
+ Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0); |
+ }else{ |
+ Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); |
+ } |
+ } |
+ |
+ /* The required interpreter variables are now populated with the data |
+ ** from the current row. If using NRE, schedule callbacks to evaluate |
+ ** script pScript, then to invoke this function again to fetch the next |
+ ** row (or clean up if there is no next row or the script throws an |
+ ** exception). After scheduling the callbacks, return control to the |
+ ** caller. |
+ ** |
+ ** If not using NRE, evaluate pScript directly and continue with the |
+ ** next iteration of this while(...) loop. */ |
+ if( DbUseNre() ){ |
+ Tcl_NRAddCallback(interp, DbEvalNextCmd, (void*)p, (void*)pScript, 0, 0); |
+ return Tcl_NREvalObj(interp, pScript, 0); |
+ }else{ |
+ rc = Tcl_EvalObjEx(interp, pScript, 0); |
+ } |
+ } |
+ |
+ Tcl_DecrRefCount(pScript); |
+ dbEvalFinalize(p); |
+ Tcl_Free((char *)p); |
+ |
+ if( rc==TCL_OK || rc==TCL_BREAK ){ |
+ Tcl_ResetResult(interp); |
+ rc = TCL_OK; |
+ } |
+ return rc; |
} |
/* |
@@ -1020,7 +1579,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
"restore", "rollback_hook", "status", |
"timeout", "total_changes", "trace", |
"transaction", "unlock_notify", "update_hook", |
- "version", 0 |
+ "version", "wal_hook", 0 |
}; |
enum DB_enum { |
DB_AUTHORIZER, DB_BACKUP, DB_BUSY, |
@@ -1034,7 +1593,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
DB_RESTORE, DB_ROLLBACK_HOOK, DB_STATUS, |
DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE, |
DB_TRANSACTION, DB_UNLOCK_NOTIFY, DB_UPDATE_HOOK, |
- DB_VERSION, |
+ DB_VERSION, DB_WAL_HOOK |
}; |
/* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */ |
@@ -1600,376 +2159,91 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db))); |
break; |
} |
+ |
+ /* |
+ ** $db exists $sql |
+ ** $db onecolumn $sql |
+ ** |
+ ** The onecolumn method is the equivalent of: |
+ ** lindex [$db eval $sql] 0 |
+ */ |
+ case DB_EXISTS: |
+ case DB_ONECOLUMN: { |
+ DbEvalContext sEval; |
+ if( objc!=3 ){ |
+ Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
+ return TCL_ERROR; |
+ } |
+ |
+ dbEvalInit(&sEval, pDb, objv[2], 0); |
+ rc = dbEvalStep(&sEval); |
+ if( choice==DB_ONECOLUMN ){ |
+ if( rc==TCL_OK ){ |
+ Tcl_SetObjResult(interp, dbEvalColumnValue(&sEval, 0)); |
+ } |
+ }else if( rc==TCL_BREAK || rc==TCL_OK ){ |
+ Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc==TCL_OK)); |
+ } |
+ dbEvalFinalize(&sEval); |
+ |
+ if( rc==TCL_BREAK ){ |
+ rc = TCL_OK; |
+ } |
+ break; |
+ } |
/* |
** $db eval $sql ?array? ?{ ...code... }? |
- ** $db onecolumn $sql |
** |
** The SQL statement in $sql is evaluated. For each row, the values are |
** placed in elements of the array named "array" and ...code... is executed. |
** If "array" and "code" are omitted, then no callback is every invoked. |
** If "array" is an empty string, then the values are placed in variables |
** that have the same name as the fields extracted by the query. |
- ** |
- ** The onecolumn method is the equivalent of: |
- ** lindex [$db eval $sql] 0 |
*/ |
- case DB_ONECOLUMN: |
- case DB_EVAL: |
- case DB_EXISTS: { |
- char const *zSql; /* Next SQL statement to execute */ |
- char const *zLeft; /* What is left after first stmt in zSql */ |
- sqlite3_stmt *pStmt; /* Compiled SQL statment */ |
- Tcl_Obj *pArray; /* Name of array into which results are written */ |
- Tcl_Obj *pScript; /* Script to run for each result set */ |
- Tcl_Obj **apParm; /* Parameters that need a Tcl_DecrRefCount() */ |
- int nParm; /* Number of entries used in apParm[] */ |
- Tcl_Obj *aParm[10]; /* Static space for apParm[] in the common case */ |
- Tcl_Obj *pRet; /* Value to be returned */ |
- SqlPreparedStmt *pPreStmt; /* Pointer to a prepared statement */ |
- int rc2; |
- |
- if( choice==DB_EVAL ){ |
- if( objc<3 || objc>5 ){ |
- Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?"); |
- return TCL_ERROR; |
- } |
- pRet = Tcl_NewObj(); |
- Tcl_IncrRefCount(pRet); |
- }else{ |
- if( objc!=3 ){ |
- Tcl_WrongNumArgs(interp, 2, objv, "SQL"); |
- return TCL_ERROR; |
- } |
- if( choice==DB_EXISTS ){ |
- pRet = Tcl_NewBooleanObj(0); |
- Tcl_IncrRefCount(pRet); |
- }else{ |
- pRet = 0; |
- } |
+ case DB_EVAL: { |
+ if( objc<3 || objc>5 ){ |
+ Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?"); |
+ return TCL_ERROR; |
} |
- if( objc==3 ){ |
- pArray = pScript = 0; |
- }else if( objc==4 ){ |
- pArray = 0; |
- pScript = objv[3]; |
- }else{ |
- pArray = objv[3]; |
- if( Tcl_GetString(pArray)[0]==0 ) pArray = 0; |
- pScript = objv[4]; |
- } |
- |
- Tcl_IncrRefCount(objv[2]); |
- zSql = Tcl_GetStringFromObj(objv[2], 0); |
- while( rc==TCL_OK && zSql[0] ){ |
- int i; /* Loop counter */ |
- int nVar; /* Number of bind parameters in the pStmt */ |
- int nCol = -1; /* Number of columns in the result set */ |
- Tcl_Obj **apColName = 0; /* Array of column names */ |
- int len; /* String length of zSql */ |
- |
- /* Try to find a SQL statement that has already been compiled and |
- ** which matches the next sequence of SQL. |
- */ |
- pStmt = 0; |
- while( isspace(zSql[0]) ){ zSql++; } |
- len = strlen30(zSql); |
- for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){ |
- int n = pPreStmt->nSql; |
- if( len>=n |
- && memcmp(pPreStmt->zSql, zSql, n)==0 |
- && (zSql[n]==0 || zSql[n-1]==';') |
- ){ |
- pStmt = pPreStmt->pStmt; |
- zLeft = &zSql[pPreStmt->nSql]; |
- |
- /* When a prepared statement is found, unlink it from the |
- ** cache list. It will later be added back to the beginning |
- ** of the cache list in order to implement LRU replacement. |
- */ |
- if( pPreStmt->pPrev ){ |
- pPreStmt->pPrev->pNext = pPreStmt->pNext; |
- }else{ |
- pDb->stmtList = pPreStmt->pNext; |
- } |
- if( pPreStmt->pNext ){ |
- pPreStmt->pNext->pPrev = pPreStmt->pPrev; |
- }else{ |
- pDb->stmtLast = pPreStmt->pPrev; |
- } |
- pDb->nStmt--; |
- break; |
- } |
- } |
- |
- /* If no prepared statement was found. Compile the SQL text |
- */ |
- if( pStmt==0 ){ |
- if( SQLITE_OK!=sqlite3_prepare_v2(pDb->db, zSql, -1, &pStmt, &zLeft) ){ |
- Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
- rc = TCL_ERROR; |
- break; |
- } |
- if( pStmt==0 ){ |
- if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){ |
- /* A compile-time error in the statement |
- */ |
- Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
- rc = TCL_ERROR; |
- break; |
- }else{ |
- /* The statement was a no-op. Continue to the next statement |
- ** in the SQL string. |
- */ |
- zSql = zLeft; |
- continue; |
- } |
- } |
- assert( pPreStmt==0 ); |
- } |
- |
- /* Bind values to parameters that begin with $ or : |
- */ |
- nVar = sqlite3_bind_parameter_count(pStmt); |
- nParm = 0; |
- if( nVar>sizeof(aParm)/sizeof(aParm[0]) ){ |
- apParm = (Tcl_Obj**)Tcl_Alloc(nVar*sizeof(apParm[0])); |
- }else{ |
- apParm = aParm; |
- } |
- for(i=1; i<=nVar; i++){ |
- const char *zVar = sqlite3_bind_parameter_name(pStmt, i); |
- if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){ |
- Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); |
- if( pVar ){ |
- int n; |
- u8 *data; |
- char *zType = pVar->typePtr ? pVar->typePtr->name : ""; |
- char c = zType[0]; |
- if( zVar[0]=='@' || |
- (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){ |
- /* Load a BLOB type if the Tcl variable is a bytearray and |
- ** it has no string representation or the host |
- ** parameter name begins with "@". */ |
- data = Tcl_GetByteArrayFromObj(pVar, &n); |
- sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); |
- Tcl_IncrRefCount(pVar); |
- apParm[nParm++] = pVar; |
- }else if( c=='b' && strcmp(zType,"boolean")==0 ){ |
- Tcl_GetIntFromObj(interp, pVar, &n); |
- sqlite3_bind_int(pStmt, i, n); |
- }else if( c=='d' && strcmp(zType,"double")==0 ){ |
- double r; |
- Tcl_GetDoubleFromObj(interp, pVar, &r); |
- sqlite3_bind_double(pStmt, i, r); |
- }else if( (c=='w' && strcmp(zType,"wideInt")==0) || |
- (c=='i' && strcmp(zType,"int")==0) ){ |
- Tcl_WideInt v; |
- Tcl_GetWideIntFromObj(interp, pVar, &v); |
- sqlite3_bind_int64(pStmt, i, v); |
- }else{ |
- data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); |
- sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); |
- Tcl_IncrRefCount(pVar); |
- apParm[nParm++] = pVar; |
- } |
- }else{ |
- sqlite3_bind_null( pStmt, i ); |
- } |
- } |
- } |
- |
- /* Execute the SQL |
- */ |
- while( rc==TCL_OK && pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){ |
- |
- /* Compute column names. This must be done after the first successful |
- ** call to sqlite3_step(), in case the query is recompiled and the |
- ** number or names of the returned columns changes. |
- */ |
- assert(!pArray||pScript); |
- if (nCol < 0) { |
- Tcl_Obj ***ap = (pScript?&apColName:0); |
- nCol = computeColumnNames(interp, pStmt, ap, pArray); |
- } |
+ if( objc==3 ){ |
+ DbEvalContext sEval; |
+ Tcl_Obj *pRet = Tcl_NewObj(); |
+ Tcl_IncrRefCount(pRet); |
+ dbEvalInit(&sEval, pDb, objv[2], 0); |
+ while( TCL_OK==(rc = dbEvalStep(&sEval)) ){ |
+ int i; |
+ int nCol; |
+ dbEvalRowInfo(&sEval, &nCol, 0); |
for(i=0; i<nCol; i++){ |
- Tcl_Obj *pVal; |
- |
- /* Set pVal to contain the i'th column of this row. */ |
- switch( sqlite3_column_type(pStmt, i) ){ |
- case SQLITE_BLOB: { |
- int bytes = sqlite3_column_bytes(pStmt, i); |
- const char *zBlob = sqlite3_column_blob(pStmt, i); |
- if( !zBlob ) bytes = 0; |
- pVal = Tcl_NewByteArrayObj((u8*)zBlob, bytes); |
- break; |
- } |
- case SQLITE_INTEGER: { |
- sqlite_int64 v = sqlite3_column_int64(pStmt, i); |
- if( v>=-2147483647 && v<=2147483647 ){ |
- pVal = Tcl_NewIntObj(v); |
- }else{ |
- pVal = Tcl_NewWideIntObj(v); |
- } |
- break; |
- } |
- case SQLITE_FLOAT: { |
- double r = sqlite3_column_double(pStmt, i); |
- pVal = Tcl_NewDoubleObj(r); |
- break; |
- } |
- case SQLITE_NULL: { |
- pVal = dbTextToObj(pDb->zNull); |
- break; |
- } |
- default: { |
- pVal = dbTextToObj((char *)sqlite3_column_text(pStmt, i)); |
- break; |
- } |
- } |
- |
- if( pScript ){ |
- if( pArray==0 ){ |
- Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0); |
- }else{ |
- Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); |
- } |
- }else if( choice==DB_ONECOLUMN ){ |
- assert( pRet==0 ); |
- if( pRet==0 ){ |
- pRet = pVal; |
- Tcl_IncrRefCount(pRet); |
- } |
- rc = TCL_BREAK; |
- i = nCol; |
- }else if( choice==DB_EXISTS ){ |
- Tcl_DecrRefCount(pRet); |
- pRet = Tcl_NewBooleanObj(1); |
- Tcl_IncrRefCount(pRet); |
- rc = TCL_BREAK; |
- i = nCol; |
- }else{ |
- Tcl_ListObjAppendElement(interp, pRet, pVal); |
- } |
- } |
- |
- if( pScript ){ |
- pDb->nStep = sqlite3_stmt_status(pStmt, |
- SQLITE_STMTSTATUS_FULLSCAN_STEP, 0); |
- pDb->nSort = sqlite3_stmt_status(pStmt, |
- SQLITE_STMTSTATUS_SORT, 0); |
- rc = Tcl_EvalObjEx(interp, pScript, 0); |
- if( rc==TCL_CONTINUE ){ |
- rc = TCL_OK; |
- } |
+ Tcl_ListObjAppendElement(interp, pRet, dbEvalColumnValue(&sEval, i)); |
} |
} |
+ dbEvalFinalize(&sEval); |
if( rc==TCL_BREAK ){ |
+ Tcl_SetObjResult(interp, pRet); |
rc = TCL_OK; |
} |
+ Tcl_DecrRefCount(pRet); |
+ }else{ |
+ ClientData cd[2]; |
+ DbEvalContext *p; |
+ Tcl_Obj *pArray = 0; |
+ Tcl_Obj *pScript; |
- /* Free the column name objects */ |
- if( pScript ){ |
- /* If the query returned no rows, but an array variable was |
- ** specified, call computeColumnNames() now to populate the |
- ** arrayname(*) variable. |
- */ |
- if (pArray && nCol < 0) { |
- Tcl_Obj ***ap = (pScript?&apColName:0); |
- nCol = computeColumnNames(interp, pStmt, ap, pArray); |
- } |
- for(i=0; i<nCol; i++){ |
- Tcl_DecrRefCount(apColName[i]); |
- } |
- Tcl_Free((char*)apColName); |
- } |
- |
- /* Free the bound string and blob parameters */ |
- for(i=0; i<nParm; i++){ |
- Tcl_DecrRefCount(apParm[i]); |
- } |
- if( apParm!=aParm ){ |
- Tcl_Free((char*)apParm); |
- } |
- |
- /* Reset the statement. If the result code is SQLITE_SCHEMA, then |
- ** flush the statement cache and try the statement again. |
- */ |
- rc2 = sqlite3_reset(pStmt); |
- pDb->nStep = sqlite3_stmt_status(pStmt, |
- SQLITE_STMTSTATUS_FULLSCAN_STEP, 1); |
- pDb->nSort = sqlite3_stmt_status(pStmt, |
- SQLITE_STMTSTATUS_SORT, 1); |
- if( SQLITE_OK!=rc2 ){ |
- /* If a run-time error occurs, report the error and stop reading |
- ** the SQL |
- */ |
- Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db))); |
- sqlite3_finalize(pStmt); |
- rc = TCL_ERROR; |
- if( pPreStmt ) Tcl_Free((char*)pPreStmt); |
- break; |
- }else if( pDb->maxStmt<=0 ){ |
- /* If the cache is turned off, deallocated the statement */ |
- if( pPreStmt ) Tcl_Free((char*)pPreStmt); |
- sqlite3_finalize(pStmt); |
- }else{ |
- /* Everything worked and the cache is operational. |
- ** Create a new SqlPreparedStmt structure if we need one. |
- ** (If we already have one we can just reuse it.) |
- */ |
- if( pPreStmt==0 ){ |
- len = zLeft - zSql; |
- pPreStmt = (SqlPreparedStmt*)Tcl_Alloc( sizeof(*pPreStmt) ); |
- if( pPreStmt==0 ) return TCL_ERROR; |
- pPreStmt->pStmt = pStmt; |
- pPreStmt->nSql = len; |
- pPreStmt->zSql = sqlite3_sql(pStmt); |
- assert( strlen30(pPreStmt->zSql)==len ); |
- assert( 0==memcmp(pPreStmt->zSql, zSql, len) ); |
- } |
- |
- /* Add the prepared statement to the beginning of the cache list |
- */ |
- pPreStmt->pNext = pDb->stmtList; |
- pPreStmt->pPrev = 0; |
- if( pDb->stmtList ){ |
- pDb->stmtList->pPrev = pPreStmt; |
- } |
- pDb->stmtList = pPreStmt; |
- if( pDb->stmtLast==0 ){ |
- assert( pDb->nStmt==0 ); |
- pDb->stmtLast = pPreStmt; |
- }else{ |
- assert( pDb->nStmt>0 ); |
- } |
- pDb->nStmt++; |
- |
- /* If we have too many statement in cache, remove the surplus from the |
- ** end of the cache list. |
- */ |
- while( pDb->nStmt>pDb->maxStmt ){ |
- sqlite3_finalize(pDb->stmtLast->pStmt); |
- pDb->stmtLast = pDb->stmtLast->pPrev; |
- Tcl_Free((char*)pDb->stmtLast->pNext); |
- pDb->stmtLast->pNext = 0; |
- pDb->nStmt--; |
- } |
+ if( objc==5 && *(char *)Tcl_GetString(objv[3]) ){ |
+ pArray = objv[3]; |
} |
+ pScript = objv[objc-1]; |
+ Tcl_IncrRefCount(pScript); |
+ |
+ p = (DbEvalContext *)Tcl_Alloc(sizeof(DbEvalContext)); |
+ dbEvalInit(p, pDb, objv[2], pArray); |
- /* Proceed to the next statement */ |
- zSql = zLeft; |
- } |
- Tcl_DecrRefCount(objv[2]); |
- |
- if( pRet ){ |
- if( rc==TCL_OK ){ |
- Tcl_SetObjResult(interp, pRet); |
- } |
- Tcl_DecrRefCount(pRet); |
- }else if( rc==TCL_OK ){ |
- Tcl_ResetResult(interp); |
+ cd[0] = (void *)p; |
+ cd[1] = (void *)pScript; |
+ rc = DbEvalNextCmd(cd, interp, TCL_OK); |
} |
break; |
} |
@@ -2122,7 +2396,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
} |
/* |
- ** The DB_ONECOLUMN method is implemented together with DB_EVAL. |
+ ** The DB_ONECOLUMN method is implemented together with DB_EXISTS. |
*/ |
/* $db progress ?N CALLBACK? |
@@ -2194,7 +2468,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
}else{ |
pDb->zProfile = 0; |
} |
-#ifndef SQLITE_OMIT_TRACE |
+#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
if( pDb->zProfile ){ |
pDb->interp = interp; |
sqlite3_profile(pDb->db, DbProfileHandler, pDb); |
@@ -2289,7 +2563,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
} |
/* |
- ** $db status (step|sort) |
+ ** $db status (step|sort|autoindex) |
** |
** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or |
** SQLITE_STMTSTATUS_SORT for the most recent eval. |
@@ -2298,7 +2572,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
int v; |
const char *zOp; |
if( objc!=3 ){ |
- Tcl_WrongNumArgs(interp, 2, objv, "(step|sort)"); |
+ Tcl_WrongNumArgs(interp, 2, objv, "(step|sort|autoindex)"); |
return TCL_ERROR; |
} |
zOp = Tcl_GetString(objv[2]); |
@@ -2306,8 +2580,11 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
v = pDb->nStep; |
}else if( strcmp(zOp, "sort")==0 ){ |
v = pDb->nSort; |
+ }else if( strcmp(zOp, "autoindex")==0 ){ |
+ v = pDb->nIndex; |
}else{ |
- Tcl_AppendResult(interp, "bad argument: should be step or sort", |
+ Tcl_AppendResult(interp, |
+ "bad argument: should be autoindex, step, or sort", |
(char*)0); |
return TCL_ERROR; |
} |
@@ -2375,7 +2652,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
}else{ |
pDb->zTrace = 0; |
} |
-#ifndef SQLITE_OMIT_TRACE |
+#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
if( pDb->zTrace ){ |
pDb->interp = interp; |
sqlite3_trace(pDb->db, DbTraceHandler, pDb); |
@@ -2401,15 +2678,12 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
case DB_TRANSACTION: { |
Tcl_Obj *pScript; |
const char *zBegin = "SAVEPOINT _tcl_transaction"; |
- const char *zEnd; |
if( objc!=3 && objc!=4 ){ |
Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT"); |
return TCL_ERROR; |
} |
- if( pDb->nTransaction ){ |
- zBegin = "SAVEPOINT _tcl_transaction"; |
- }else if( pDb->nTransaction==0 && objc==4 ){ |
+ if( pDb->nTransaction==0 && objc==4 ){ |
static const char *TTYPE_strs[] = { |
"deferred", "exclusive", "immediate", 0 |
}; |
@@ -2429,6 +2703,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
} |
pScript = objv[objc-1]; |
+ /* Run the SQLite BEGIN command to open a transaction or savepoint. */ |
pDb->disableAuth++; |
rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0); |
pDb->disableAuth--; |
@@ -2436,45 +2711,19 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); |
return TCL_ERROR; |
} |
- |
pDb->nTransaction++; |
- rc = Tcl_EvalObjEx(interp, pScript, 0); |
- pDb->nTransaction--; |
- if( rc!=TCL_ERROR ){ |
- if( pDb->nTransaction ){ |
- zEnd = "RELEASE _tcl_transaction"; |
- }else{ |
- zEnd = "COMMIT"; |
- } |
+ /* If using NRE, schedule a callback to invoke the script pScript, then |
+ ** a second callback to commit (or rollback) the transaction or savepoint |
+ ** opened above. If not using NRE, evaluate the script directly, then |
+ ** call function DbTransPostCmd() to commit (or rollback) the transaction |
+ ** or savepoint. */ |
+ if( DbUseNre() ){ |
+ Tcl_NRAddCallback(interp, DbTransPostCmd, cd, 0, 0, 0); |
+ Tcl_NREvalObj(interp, pScript, 0); |
}else{ |
- if( pDb->nTransaction ){ |
- zEnd = "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction"; |
- }else{ |
- zEnd = "ROLLBACK"; |
- } |
- } |
- |
- pDb->disableAuth++; |
- if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){ |
- /* This is a tricky scenario to handle. The most likely cause of an |
- ** error is that the exec() above was an attempt to commit the |
- ** top-level transaction that returned SQLITE_BUSY. Or, less likely, |
- ** that an IO-error has occured. In either case, throw a Tcl exception |
- ** and try to rollback the transaction. |
- ** |
- ** But it could also be that the user executed one or more BEGIN, |
- ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing |
- ** this method's logic. Not clear how this would be best handled. |
- */ |
- if( rc!=TCL_ERROR ){ |
- Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0); |
- rc = TCL_ERROR; |
- } |
- sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0); |
+ rc = DbTransPostCmd(&cd, interp, Tcl_EvalObjEx(interp, pScript, 0)); |
} |
- pDb->disableAuth--; |
- |
break; |
} |
@@ -2515,9 +2764,11 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
} |
/* |
+ ** $db wal_hook ?script? |
** $db update_hook ?script? |
** $db rollback_hook ?script? |
*/ |
+ case DB_WAL_HOOK: |
case DB_UPDATE_HOOK: |
case DB_ROLLBACK_HOOK: { |
@@ -2527,6 +2778,8 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
Tcl_Obj **ppHook; |
if( choice==DB_UPDATE_HOOK ){ |
ppHook = &pDb->pUpdateHook; |
+ }else if( choice==DB_WAL_HOOK ){ |
+ ppHook = &pDb->pWalHook; |
}else{ |
ppHook = &pDb->pRollbackHook; |
} |
@@ -2552,6 +2805,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
sqlite3_update_hook(pDb->db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); |
sqlite3_rollback_hook(pDb->db,(pDb->pRollbackHook?DbRollbackHandler:0),pDb); |
+ sqlite3_wal_hook(pDb->db,(pDb->pWalHook?DbWalHandler:0),pDb); |
break; |
} |
@@ -2570,6 +2824,21 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
return rc; |
} |
+#if SQLITE_TCL_NRE |
+/* |
+** Adaptor that provides an objCmd interface to the NRE-enabled |
+** interface implementation. |
+*/ |
+static int DbObjCmdAdaptor( |
+ void *cd, |
+ Tcl_Interp *interp, |
+ int objc, |
+ Tcl_Obj *const*objv |
+){ |
+ return Tcl_NRCallObjProc(interp, DbObjCmd, cd, objc, objv); |
+} |
+#endif /* SQLITE_TCL_NRE */ |
+ |
/* |
** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN? |
** ?-create BOOLEAN? ?-nomutex BOOLEAN? |
@@ -2629,8 +2898,7 @@ static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
if( strcmp(zArg,"-key")==0 ){ |
pKey = Tcl_GetByteArrayFromObj(objv[i+1], &nKey); |
}else if( strcmp(zArg, "-vfs")==0 ){ |
- i++; |
- zVfs = Tcl_GetString(objv[i]); |
+ zVfs = Tcl_GetString(objv[i+1]); |
}else if( strcmp(zArg, "-readonly")==0 ){ |
int b; |
if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; |
@@ -2712,7 +2980,12 @@ static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
p->maxStmt = NUM_PREPARED_STMTS; |
p->interp = interp; |
zArg = Tcl_GetStringFromObj(objv[1], 0); |
- Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); |
+ if( DbUseNre() ){ |
+ Tcl_NRCreateCommand(interp, zArg, DbObjCmdAdaptor, DbObjCmd, |
+ (char*)p, DbDeleteCmd); |
+ }else{ |
+ Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); |
+ } |
return TCL_OK; |
} |
@@ -2742,13 +3015,22 @@ static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ |
** if the extension only supplies one new name!) The "sqlite" command is |
** used to open a new SQLite database. See the DbMain() routine above |
** for additional information. |
+** |
+** The EXTERN macros are required by TCL in order to work on windows. |
*/ |
EXTERN int Sqlite3_Init(Tcl_Interp *interp){ |
Tcl_InitStubs(interp, "8.4", 0); |
Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION); |
+ |
+#ifndef SQLITE_3_SUFFIX_ONLY |
+ /* The "sqlite" alias is undocumented. It is here only to support |
+ ** legacy scripts. All new scripts should use only the "sqlite3" |
+ ** command. |
+ */ |
Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0); |
- Tcl_PkgProvide(interp, "sqlite", PACKAGE_VERSION); |
+#endif |
+ |
return TCL_OK; |
} |
EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
@@ -2761,22 +3043,431 @@ EXTERN int Tclsqlite3_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK;} |
#ifndef SQLITE_3_SUFFIX_ONLY |
-EXTERN int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
-EXTERN int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
-EXTERN int Sqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
-EXTERN int Tclsqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
-EXTERN int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
-EXTERN int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
-EXTERN int Sqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
-EXTERN int Tclsqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK;} |
+int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
+int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } |
+int Sqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
+int Tclsqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; } |
+int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
+int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
+int Sqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK; } |
+int Tclsqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK;} |
#endif |
#ifdef TCLSH |
/***************************************************************************** |
-** The code that follows is used to build standalone TCL interpreters |
-** that are statically linked with SQLite. |
+** All of the code that follows is used to build standalone TCL interpreters |
+** that are statically linked with SQLite. Enable these by compiling |
+** with -DTCLSH=n where n can be 1 or 2. An n of 1 generates a standard |
+** tclsh but with SQLite built in. An n of 2 generates the SQLite space |
+** analysis program. |
*/ |
+#if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) |
+/* |
+ * This code implements the MD5 message-digest algorithm. |
+ * The algorithm is due to Ron Rivest. This code was |
+ * written by Colin Plumb in 1993, no copyright is claimed. |
+ * This code is in the public domain; do with it what you wish. |
+ * |
+ * Equivalent code is available from RSA Data Security, Inc. |
+ * This code has been tested against that, and is equivalent, |
+ * except that you don't need to include two pages of legalese |
+ * with every copy. |
+ * |
+ * To compute the message digest of a chunk of bytes, declare an |
+ * MD5Context structure, pass it to MD5Init, call MD5Update as |
+ * needed on buffers full of bytes, and then call MD5Final, which |
+ * will fill a supplied 16-byte array with the digest. |
+ */ |
+ |
+/* |
+ * If compiled on a machine that doesn't have a 32-bit integer, |
+ * you just set "uint32" to the appropriate datatype for an |
+ * unsigned 32-bit integer. For example: |
+ * |
+ * cc -Duint32='unsigned long' md5.c |
+ * |
+ */ |
+#ifndef uint32 |
+# define uint32 unsigned int |
+#endif |
+ |
+struct MD5Context { |
+ int isInit; |
+ uint32 buf[4]; |
+ uint32 bits[2]; |
+ unsigned char in[64]; |
+}; |
+typedef struct MD5Context MD5Context; |
+ |
+/* |
+ * Note: this code is harmless on little-endian machines. |
+ */ |
+static void byteReverse (unsigned char *buf, unsigned longs){ |
+ uint32 t; |
+ do { |
+ t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 | |
+ ((unsigned)buf[1]<<8 | buf[0]); |
+ *(uint32 *)buf = t; |
+ buf += 4; |
+ } while (--longs); |
+} |
+/* The four core functions - F1 is optimized somewhat */ |
+ |
+/* #define F1(x, y, z) (x & y | ~x & z) */ |
+#define F1(x, y, z) (z ^ (x & (y ^ z))) |
+#define F2(x, y, z) F1(z, x, y) |
+#define F3(x, y, z) (x ^ y ^ z) |
+#define F4(x, y, z) (y ^ (x | ~z)) |
+ |
+/* This is the central step in the MD5 algorithm. */ |
+#define MD5STEP(f, w, x, y, z, data, s) \ |
+ ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) |
+ |
+/* |
+ * The core of the MD5 algorithm, this alters an existing MD5 hash to |
+ * reflect the addition of 16 longwords of new data. MD5Update blocks |
+ * the data and converts bytes into longwords for this routine. |
+ */ |
+static void MD5Transform(uint32 buf[4], const uint32 in[16]){ |
+ register uint32 a, b, c, d; |
+ |
+ a = buf[0]; |
+ b = buf[1]; |
+ c = buf[2]; |
+ d = buf[3]; |
+ |
+ MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); |
+ MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); |
+ MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); |
+ MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); |
+ MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); |
+ MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); |
+ MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); |
+ MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); |
+ MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); |
+ MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); |
+ MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); |
+ MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); |
+ MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); |
+ MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); |
+ MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); |
+ MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); |
+ |
+ MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); |
+ MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); |
+ MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); |
+ MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); |
+ MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); |
+ MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); |
+ MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); |
+ MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); |
+ MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); |
+ MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); |
+ MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); |
+ MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); |
+ MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); |
+ MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); |
+ MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); |
+ MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); |
+ |
+ MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); |
+ MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); |
+ MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); |
+ MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); |
+ MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); |
+ MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); |
+ MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); |
+ MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); |
+ MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); |
+ MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); |
+ MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); |
+ MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); |
+ MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); |
+ MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); |
+ MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); |
+ MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); |
+ |
+ MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); |
+ MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); |
+ MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); |
+ MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); |
+ MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); |
+ MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); |
+ MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); |
+ MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); |
+ MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); |
+ MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); |
+ MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); |
+ MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); |
+ MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); |
+ MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); |
+ MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); |
+ MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); |
+ |
+ buf[0] += a; |
+ buf[1] += b; |
+ buf[2] += c; |
+ buf[3] += d; |
+} |
+ |
+/* |
+ * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious |
+ * initialization constants. |
+ */ |
+static void MD5Init(MD5Context *ctx){ |
+ ctx->isInit = 1; |
+ ctx->buf[0] = 0x67452301; |
+ ctx->buf[1] = 0xefcdab89; |
+ ctx->buf[2] = 0x98badcfe; |
+ ctx->buf[3] = 0x10325476; |
+ ctx->bits[0] = 0; |
+ ctx->bits[1] = 0; |
+} |
+ |
+/* |
+ * Update context to reflect the concatenation of another buffer full |
+ * of bytes. |
+ */ |
+static |
+void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){ |
+ uint32 t; |
+ |
+ /* Update bitcount */ |
+ |
+ t = ctx->bits[0]; |
+ if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) |
+ ctx->bits[1]++; /* Carry from low to high */ |
+ ctx->bits[1] += len >> 29; |
+ |
+ t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ |
+ |
+ /* Handle any leading odd-sized chunks */ |
+ |
+ if ( t ) { |
+ unsigned char *p = (unsigned char *)ctx->in + t; |
+ |
+ t = 64-t; |
+ if (len < t) { |
+ memcpy(p, buf, len); |
+ return; |
+ } |
+ memcpy(p, buf, t); |
+ byteReverse(ctx->in, 16); |
+ MD5Transform(ctx->buf, (uint32 *)ctx->in); |
+ buf += t; |
+ len -= t; |
+ } |
+ |
+ /* Process data in 64-byte chunks */ |
+ |
+ while (len >= 64) { |
+ memcpy(ctx->in, buf, 64); |
+ byteReverse(ctx->in, 16); |
+ MD5Transform(ctx->buf, (uint32 *)ctx->in); |
+ buf += 64; |
+ len -= 64; |
+ } |
+ |
+ /* Handle any remaining bytes of data. */ |
+ |
+ memcpy(ctx->in, buf, len); |
+} |
+ |
+/* |
+ * Final wrapup - pad to 64-byte boundary with the bit pattern |
+ * 1 0* (64-bit count of bits processed, MSB-first) |
+ */ |
+static void MD5Final(unsigned char digest[16], MD5Context *ctx){ |
+ unsigned count; |
+ unsigned char *p; |
+ |
+ /* Compute number of bytes mod 64 */ |
+ count = (ctx->bits[0] >> 3) & 0x3F; |
+ |
+ /* Set the first char of padding to 0x80. This is safe since there is |
+ always at least one byte free */ |
+ p = ctx->in + count; |
+ *p++ = 0x80; |
+ |
+ /* Bytes of padding needed to make 64 bytes */ |
+ count = 64 - 1 - count; |
+ |
+ /* Pad out to 56 mod 64 */ |
+ if (count < 8) { |
+ /* Two lots of padding: Pad the first block to 64 bytes */ |
+ memset(p, 0, count); |
+ byteReverse(ctx->in, 16); |
+ MD5Transform(ctx->buf, (uint32 *)ctx->in); |
+ |
+ /* Now fill the next block with 56 bytes */ |
+ memset(ctx->in, 0, 56); |
+ } else { |
+ /* Pad block to 56 bytes */ |
+ memset(p, 0, count-8); |
+ } |
+ byteReverse(ctx->in, 14); |
+ |
+ /* Append length in bits and transform */ |
+ ((uint32 *)ctx->in)[ 14 ] = ctx->bits[0]; |
+ ((uint32 *)ctx->in)[ 15 ] = ctx->bits[1]; |
+ |
+ MD5Transform(ctx->buf, (uint32 *)ctx->in); |
+ byteReverse((unsigned char *)ctx->buf, 4); |
+ memcpy(digest, ctx->buf, 16); |
+ memset(ctx, 0, sizeof(ctx)); /* In case it is sensitive */ |
+} |
+ |
+/* |
+** Convert a 128-bit MD5 digest into a 32-digit base-16 number. |
+*/ |
+static void MD5DigestToBase16(unsigned char *digest, char *zBuf){ |
+ static char const zEncode[] = "0123456789abcdef"; |
+ int i, j; |
+ |
+ for(j=i=0; i<16; i++){ |
+ int a = digest[i]; |
+ zBuf[j++] = zEncode[(a>>4)&0xf]; |
+ zBuf[j++] = zEncode[a & 0xf]; |
+ } |
+ zBuf[j] = 0; |
+} |
+ |
+ |
+/* |
+** Convert a 128-bit MD5 digest into sequency of eight 5-digit integers |
+** each representing 16 bits of the digest and separated from each |
+** other by a "-" character. |
+*/ |
+static void MD5DigestToBase10x8(unsigned char digest[16], char zDigest[50]){ |
+ int i, j; |
+ unsigned int x; |
+ for(i=j=0; i<16; i+=2){ |
+ x = digest[i]*256 + digest[i+1]; |
+ if( i>0 ) zDigest[j++] = '-'; |
+ sprintf(&zDigest[j], "%05u", x); |
+ j += 5; |
+ } |
+ zDigest[j] = 0; |
+} |
+ |
+/* |
+** A TCL command for md5. The argument is the text to be hashed. The |
+** Result is the hash in base64. |
+*/ |
+static int md5_cmd(void*cd, Tcl_Interp *interp, int argc, const char **argv){ |
+ MD5Context ctx; |
+ unsigned char digest[16]; |
+ char zBuf[50]; |
+ void (*converter)(unsigned char*, char*); |
+ |
+ if( argc!=2 ){ |
+ Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], |
+ " TEXT\"", 0); |
+ return TCL_ERROR; |
+ } |
+ MD5Init(&ctx); |
+ MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1])); |
+ MD5Final(digest, &ctx); |
+ converter = (void(*)(unsigned char*,char*))cd; |
+ converter(digest, zBuf); |
+ Tcl_AppendResult(interp, zBuf, (char*)0); |
+ return TCL_OK; |
+} |
+ |
+/* |
+** A TCL command to take the md5 hash of a file. The argument is the |
+** name of the file. |
+*/ |
+static int md5file_cmd(void*cd, Tcl_Interp*interp, int argc, const char **argv){ |
+ FILE *in; |
+ MD5Context ctx; |
+ void (*converter)(unsigned char*, char*); |
+ unsigned char digest[16]; |
+ char zBuf[10240]; |
+ |
+ if( argc!=2 ){ |
+ Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], |
+ " FILENAME\"", 0); |
+ return TCL_ERROR; |
+ } |
+ in = fopen(argv[1],"rb"); |
+ if( in==0 ){ |
+ Tcl_AppendResult(interp,"unable to open file \"", argv[1], |
+ "\" for reading", 0); |
+ return TCL_ERROR; |
+ } |
+ MD5Init(&ctx); |
+ for(;;){ |
+ int n; |
+ n = fread(zBuf, 1, sizeof(zBuf), in); |
+ if( n<=0 ) break; |
+ MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n); |
+ } |
+ fclose(in); |
+ MD5Final(digest, &ctx); |
+ converter = (void(*)(unsigned char*,char*))cd; |
+ converter(digest, zBuf); |
+ Tcl_AppendResult(interp, zBuf, (char*)0); |
+ return TCL_OK; |
+} |
+ |
+/* |
+** Register the four new TCL commands for generating MD5 checksums |
+** with the TCL interpreter. |
+*/ |
+int Md5_Init(Tcl_Interp *interp){ |
+ Tcl_CreateCommand(interp, "md5", (Tcl_CmdProc*)md5_cmd, |
+ MD5DigestToBase16, 0); |
+ Tcl_CreateCommand(interp, "md5-10x8", (Tcl_CmdProc*)md5_cmd, |
+ MD5DigestToBase10x8, 0); |
+ Tcl_CreateCommand(interp, "md5file", (Tcl_CmdProc*)md5file_cmd, |
+ MD5DigestToBase16, 0); |
+ Tcl_CreateCommand(interp, "md5file-10x8", (Tcl_CmdProc*)md5file_cmd, |
+ MD5DigestToBase10x8, 0); |
+ return TCL_OK; |
+} |
+#endif /* defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) */ |
+ |
+#if defined(SQLITE_TEST) |
+/* |
+** During testing, the special md5sum() aggregate function is available. |
+** inside SQLite. The following routines implement that function. |
+*/ |
+static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){ |
+ MD5Context *p; |
+ int i; |
+ if( argc<1 ) return; |
+ p = sqlite3_aggregate_context(context, sizeof(*p)); |
+ if( p==0 ) return; |
+ if( !p->isInit ){ |
+ MD5Init(p); |
+ } |
+ for(i=0; i<argc; i++){ |
+ const char *zData = (char*)sqlite3_value_text(argv[i]); |
+ if( zData ){ |
+ MD5Update(p, (unsigned char*)zData, strlen(zData)); |
+ } |
+ } |
+} |
+static void md5finalize(sqlite3_context *context){ |
+ MD5Context *p; |
+ unsigned char digest[16]; |
+ char zBuf[33]; |
+ p = sqlite3_aggregate_context(context, sizeof(*p)); |
+ MD5Final(digest,p); |
+ MD5DigestToBase16(digest, zBuf); |
+ sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
+} |
+int Md5_Register(sqlite3 *db){ |
+ int rc = sqlite3_create_function(db, "md5sum", -1, SQLITE_UTF8, 0, 0, |
+ md5step, md5finalize); |
+ sqlite3_overload_function(db, "md5sum", -1); /* To exercise this API */ |
+ return rc; |
+} |
+#endif /* defined(SQLITE_TEST) */ |
+ |
+ |
/* |
** If the macro TCLSH is one, then put in code this for the |
** "main" routine that will initialize Tcl and take input from |
@@ -2807,32 +3498,57 @@ static char zMainloop[] = |
"}\n" |
; |
#endif |
- |
-/* |
-** If the macro TCLSH is two, then get the main loop code out of |
-** the separate file "spaceanal_tcl.h". |
-*/ |
#if TCLSH==2 |
static char zMainloop[] = |
#include "spaceanal_tcl.h" |
; |
#endif |
-#define TCLSH_MAIN main /* Needed to fake out mktclapp */ |
-int TCLSH_MAIN(int argc, char **argv){ |
- Tcl_Interp *interp; |
- |
- /* Call sqlite3_shutdown() once before doing anything else. This is to |
- ** test that sqlite3_shutdown() can be safely called by a process before |
- ** sqlite3_initialize() is. */ |
- sqlite3_shutdown(); |
+#ifdef SQLITE_TEST |
+static void init_all(Tcl_Interp *); |
+static int init_all_cmd( |
+ ClientData cd, |
+ Tcl_Interp *interp, |
+ int objc, |
+ Tcl_Obj *CONST objv[] |
+){ |
- Tcl_FindExecutable(argv[0]); |
- interp = Tcl_CreateInterp(); |
+ Tcl_Interp *slave; |
+ if( objc!=2 ){ |
+ Tcl_WrongNumArgs(interp, 1, objv, "SLAVE"); |
+ return TCL_ERROR; |
+ } |
+ |
+ slave = Tcl_GetSlave(interp, Tcl_GetString(objv[1])); |
+ if( !slave ){ |
+ return TCL_ERROR; |
+ } |
+ |
+ init_all(slave); |
+ return TCL_OK; |
+} |
+#endif |
+ |
+/* |
+** Configure the interpreter passed as the first argument to have access |
+** to the commands and linked variables that make up: |
+** |
+** * the [sqlite3] extension itself, |
+** |
+** * If SQLITE_TCLMD5 or SQLITE_TEST is defined, the Md5 commands, and |
+** |
+** * If SQLITE_TEST is set, the various test interfaces used by the Tcl |
+** test suite. |
+*/ |
+static void init_all(Tcl_Interp *interp){ |
Sqlite3_Init(interp); |
+ |
+#if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) |
+ Md5_Init(interp); |
+#endif |
+ |
#ifdef SQLITE_TEST |
{ |
- extern int Md5_Init(Tcl_Interp*); |
extern int Sqliteconfig_Init(Tcl_Interp*); |
extern int Sqlitetest1_Init(Tcl_Interp*); |
extern int Sqlitetest2_Init(Tcl_Interp*); |
@@ -2845,6 +3561,7 @@ int TCLSH_MAIN(int argc, char **argv){ |
extern int Sqlitetest9_Init(Tcl_Interp*); |
extern int Sqlitetestasync_Init(Tcl_Interp*); |
extern int Sqlitetest_autoext_Init(Tcl_Interp*); |
+ extern int Sqlitetest_demovfs_Init(Tcl_Interp *); |
extern int Sqlitetest_func_Init(Tcl_Interp*); |
extern int Sqlitetest_hexio_Init(Tcl_Interp*); |
extern int Sqlitetest_init_Init(Tcl_Interp*); |
@@ -2857,8 +3574,22 @@ int TCLSH_MAIN(int argc, char **argv){ |
extern int SqlitetestOnefile_Init(); |
extern int SqlitetestOsinst_Init(Tcl_Interp*); |
extern int Sqlitetestbackup_Init(Tcl_Interp*); |
+ extern int Sqlitetestintarray_Init(Tcl_Interp*); |
+ extern int Sqlitetestvfs_Init(Tcl_Interp *); |
+ extern int SqlitetestStat_Init(Tcl_Interp*); |
+ extern int Sqlitetestrtree_Init(Tcl_Interp*); |
+ extern int Sqlitequota_Init(Tcl_Interp*); |
+ extern int Sqlitemultiplex_Init(Tcl_Interp*); |
+ extern int SqliteSuperlock_Init(Tcl_Interp*); |
+ extern int SqlitetestSyscall_Init(Tcl_Interp*); |
+ extern int Sqlitetestfuzzer_Init(Tcl_Interp*); |
+ extern int Sqlitetestwholenumber_Init(Tcl_Interp*); |
+ |
+#ifdef SQLITE_ENABLE_ZIPVFS |
+ extern int Zipvfs_Init(Tcl_Interp*); |
+ Zipvfs_Init(interp); |
+#endif |
- Md5_Init(interp); |
Sqliteconfig_Init(interp); |
Sqlitetest1_Init(interp); |
Sqlitetest2_Init(interp); |
@@ -2871,6 +3602,7 @@ int TCLSH_MAIN(int argc, char **argv){ |
Sqlitetest9_Init(interp); |
Sqlitetestasync_Init(interp); |
Sqlitetest_autoext_Init(interp); |
+ Sqlitetest_demovfs_Init(interp); |
Sqlitetest_func_Init(interp); |
Sqlitetest_hexio_Init(interp); |
Sqlitetest_init_Init(interp); |
@@ -2882,13 +3614,43 @@ int TCLSH_MAIN(int argc, char **argv){ |
SqlitetestOnefile_Init(interp); |
SqlitetestOsinst_Init(interp); |
Sqlitetestbackup_Init(interp); |
+ Sqlitetestintarray_Init(interp); |
+ Sqlitetestvfs_Init(interp); |
+ SqlitetestStat_Init(interp); |
+ Sqlitetestrtree_Init(interp); |
+ Sqlitequota_Init(interp); |
+ Sqlitemultiplex_Init(interp); |
+ SqliteSuperlock_Init(interp); |
+ SqlitetestSyscall_Init(interp); |
+ Sqlitetestfuzzer_Init(interp); |
+ Sqlitetestwholenumber_Init(interp); |
+ |
+ Tcl_CreateObjCommand(interp,"load_testfixture_extensions",init_all_cmd,0,0); |
#ifdef SQLITE_SSE |
Sqlitetestsse_Init(interp); |
#endif |
} |
#endif |
- if( argc>=2 || TCLSH==2 ){ |
+} |
+ |
+#define TCLSH_MAIN main /* Needed to fake out mktclapp */ |
+int TCLSH_MAIN(int argc, char **argv){ |
+ Tcl_Interp *interp; |
+ |
+ /* Call sqlite3_shutdown() once before doing anything else. This is to |
+ ** test that sqlite3_shutdown() can be safely called by a process before |
+ ** sqlite3_initialize() is. */ |
+ sqlite3_shutdown(); |
+ |
+#if TCLSH==2 |
+ sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); |
+#endif |
+ Tcl_FindExecutable(argv[0]); |
+ |
+ interp = Tcl_CreateInterp(); |
+ init_all(interp); |
+ if( argc>=2 ){ |
int i; |
char zArgc[32]; |
sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH)); |
@@ -2906,7 +3668,7 @@ int TCLSH_MAIN(int argc, char **argv){ |
return 1; |
} |
} |
- if( argc<=1 || TCLSH==2 ){ |
+ if( TCLSH==2 || argc<=1 ){ |
Tcl_GlobalEval(interp, zMainloop); |
} |
return 0; |