2000-05-29 18:26:00 +04:00
|
|
|
/*
|
2001-09-16 04:13:26 +04:00
|
|
|
** 2001 September 15
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
2001-09-16 04:13:26 +04:00
|
|
|
** The author disclaims copyright to this source code. In place of
|
|
|
|
** a legal notice, here is a blessing:
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
2001-09-16 04:13:26 +04:00
|
|
|
** May you do good and not evil.
|
|
|
|
** May you find forgiveness for yourself and forgive others.
|
|
|
|
** May you share freely, never taking more than you give.
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
|
|
|
*************************************************************************
|
2007-04-06 01:58:33 +04:00
|
|
|
** A TCL Interface to SQLite. Append this file to sqlite3.c and
|
|
|
|
** compile the whole thing to build a TCL-enabled version of SQLite.
|
2009-10-23 00:52:05 +04:00
|
|
|
**
|
|
|
|
** Compile-time options:
|
|
|
|
**
|
2017-10-13 23:14:06 +03:00
|
|
|
** -DTCLSH Add a "main()" routine that works as a tclsh.
|
2009-10-23 00:52:05 +04:00
|
|
|
**
|
2017-10-13 23:14:06 +03:00
|
|
|
** -DTCLSH_INIT_PROC=name
|
2009-10-23 00:52:05 +04:00
|
|
|
**
|
2017-10-13 23:14:06 +03:00
|
|
|
** Invoke name(interp) to initialize the Tcl interpreter.
|
|
|
|
** If name(interp) returns a non-NULL string, then run
|
|
|
|
** that string as a Tcl script to launch the application.
|
|
|
|
** If name(interp) returns NULL, then run the regular
|
|
|
|
** tclsh-emulator code.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2017-10-13 23:14:06 +03:00
|
|
|
#ifdef TCLSH_INIT_PROC
|
|
|
|
# define TCLSH 1
|
|
|
|
#endif
|
2015-01-12 22:49:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** If requested, include the SQLite compiler options file for MSVC.
|
|
|
|
*/
|
|
|
|
#if defined(INCLUDE_MSVC_H)
|
2016-07-28 17:37:04 +03:00
|
|
|
# include "msvc.h"
|
2015-01-12 22:49:46 +03:00
|
|
|
#endif
|
|
|
|
|
2016-07-28 17:37:04 +03:00
|
|
|
#if defined(INCLUDE_SQLITE_TCL_H)
|
|
|
|
# include "sqlite_tcl.h"
|
|
|
|
#else
|
|
|
|
# include "tcl.h"
|
2016-07-28 20:11:20 +03:00
|
|
|
# ifndef SQLITE_TCLAPI
|
|
|
|
# define SQLITE_TCLAPI
|
|
|
|
# endif
|
2016-07-28 17:37:04 +03:00
|
|
|
#endif
|
2007-05-01 21:49:49 +04:00
|
|
|
#include <errno.h>
|
2007-04-06 01:58:33 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Some additional include files are needed if this file is not
|
|
|
|
** appended to the amalgamation.
|
|
|
|
*/
|
|
|
|
#ifndef SQLITE_AMALGAMATION
|
2009-12-01 16:57:48 +03:00
|
|
|
# include "sqlite3.h"
|
2007-04-06 01:58:33 +04:00
|
|
|
# include <stdlib.h>
|
|
|
|
# include <string.h>
|
|
|
|
# include <assert.h>
|
2009-12-01 16:57:48 +03:00
|
|
|
typedef unsigned char u8;
|
2007-04-06 01:58:33 +04:00
|
|
|
#endif
|
2009-10-24 19:51:33 +04:00
|
|
|
#include <ctype.h>
|
2000-05-29 18:26:00 +04:00
|
|
|
|
2013-08-15 12:06:15 +04:00
|
|
|
/* Used to get the current process ID */
|
|
|
|
#if !defined(_WIN32)
|
2018-07-22 09:25:35 +03:00
|
|
|
# include <signal.h>
|
2013-08-15 12:06:15 +04:00
|
|
|
# include <unistd.h>
|
|
|
|
# define GETPID getpid
|
|
|
|
#elif !defined(_WIN32_WCE)
|
|
|
|
# ifndef SQLITE_AMALGAMATION
|
2018-01-23 10:11:05 +03:00
|
|
|
# ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# endif
|
2013-08-15 12:06:15 +04:00
|
|
|
# include <windows.h>
|
|
|
|
# endif
|
2018-07-22 09:25:35 +03:00
|
|
|
# include <io.h>
|
|
|
|
# define isatty(h) _isatty(h)
|
2013-08-15 12:06:15 +04:00
|
|
|
# define GETPID (int)GetCurrentProcessId
|
|
|
|
#endif
|
|
|
|
|
2006-07-11 01:15:51 +04:00
|
|
|
/*
|
|
|
|
* Windows needs to know which symbols to export. Unix does not.
|
|
|
|
* BUILD_sqlite should be undefined for Unix.
|
|
|
|
*/
|
|
|
|
#ifdef BUILD_sqlite
|
|
|
|
#undef TCL_STORAGE_CLASS
|
|
|
|
#define TCL_STORAGE_CLASS DLLEXPORT
|
|
|
|
#endif /* BUILD_sqlite */
|
2005-10-05 14:40:15 +04:00
|
|
|
|
2005-01-24 13:25:59 +03:00
|
|
|
#define NUM_PREPARED_STMTS 10
|
2005-01-24 03:28:42 +03:00
|
|
|
#define MAX_PREPARED_STMTS 100
|
|
|
|
|
2012-10-03 15:02:33 +04:00
|
|
|
/* Forward declaration */
|
|
|
|
typedef struct SqliteDb SqliteDb;
|
2001-10-18 16:34:46 +04:00
|
|
|
|
2002-09-14 17:47:32 +04:00
|
|
|
/*
|
|
|
|
** New SQL functions can be created as TCL scripts. Each such function
|
|
|
|
** is described by an instance of the following structure.
|
2019-02-27 19:38:19 +03:00
|
|
|
**
|
|
|
|
** Variable eType may be set to SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT,
|
|
|
|
** SQLITE_BLOB or SQLITE_NULL. If it is SQLITE_NULL, then the implementation
|
|
|
|
** attempts to determine the type of the result based on the Tcl object.
|
|
|
|
** If it is SQLITE_TEXT or SQLITE_BLOB, then a text (sqlite3_result_text())
|
|
|
|
** or blob (sqlite3_result_blob()) is returned. If it is SQLITE_INTEGER
|
|
|
|
** or SQLITE_FLOAT, then an attempt is made to return an integer or float
|
|
|
|
** value, falling back to float and then text if this is not possible.
|
2002-09-14 17:47:32 +04:00
|
|
|
*/
|
|
|
|
typedef struct SqlFunc SqlFunc;
|
|
|
|
struct SqlFunc {
|
|
|
|
Tcl_Interp *interp; /* The TCL interpret to execute the function */
|
2005-06-26 21:55:33 +04:00
|
|
|
Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */
|
2012-10-03 15:02:33 +04:00
|
|
|
SqliteDb *pDb; /* Database connection that owns this function */
|
2005-06-26 21:55:33 +04:00
|
|
|
int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */
|
2019-02-27 19:38:19 +03:00
|
|
|
int eType; /* Type of value to return */
|
2005-06-26 21:55:33 +04:00
|
|
|
char *zName; /* Name of this function */
|
2002-09-14 17:47:32 +04:00
|
|
|
SqlFunc *pNext; /* Next function on the list of them all */
|
|
|
|
};
|
|
|
|
|
2004-06-09 13:55:16 +04:00
|
|
|
/*
|
|
|
|
** New collation sequences function can be created as TCL scripts. Each such
|
|
|
|
** function is described by an instance of the following structure.
|
|
|
|
*/
|
|
|
|
typedef struct SqlCollate SqlCollate;
|
|
|
|
struct SqlCollate {
|
|
|
|
Tcl_Interp *interp; /* The TCL interpret to execute the function */
|
|
|
|
char *zScript; /* The script to be run */
|
2005-06-26 21:55:33 +04:00
|
|
|
SqlCollate *pNext; /* Next function on the list of them all */
|
2004-06-09 13:55:16 +04:00
|
|
|
};
|
|
|
|
|
2005-01-24 03:28:42 +03:00
|
|
|
/*
|
|
|
|
** Prepared statements are cached for faster execution. Each prepared
|
|
|
|
** statement is described by an instance of the following structure.
|
|
|
|
*/
|
|
|
|
typedef struct SqlPreparedStmt SqlPreparedStmt;
|
|
|
|
struct SqlPreparedStmt {
|
|
|
|
SqlPreparedStmt *pNext; /* Next in linked list */
|
|
|
|
SqlPreparedStmt *pPrev; /* Previous on the list */
|
|
|
|
sqlite3_stmt *pStmt; /* The prepared statement */
|
|
|
|
int nSql; /* chars in zSql[] */
|
2007-11-14 09:48:48 +03:00
|
|
|
const char *zSql; /* Text of the SQL statement */
|
2009-10-06 18:59:02 +04:00
|
|
|
int nParm; /* Size of apParm array */
|
|
|
|
Tcl_Obj **apParm; /* Array of referenced object pointers */
|
2005-01-24 03:28:42 +03:00
|
|
|
};
|
|
|
|
|
2007-05-02 17:16:30 +04:00
|
|
|
typedef struct IncrblobChannel IncrblobChannel;
|
|
|
|
|
2000-08-04 17:49:02 +04:00
|
|
|
/*
|
|
|
|
** There is one instance of this structure for each SQLite database
|
|
|
|
** that has been opened by the SQLite TCL interface.
|
2011-06-27 20:55:50 +04:00
|
|
|
**
|
|
|
|
** If this module is built with SQLITE_TEST defined (to create the SQLite
|
|
|
|
** testfixture executable), then it may be configured to use either
|
|
|
|
** sqlite3_prepare_v2() or sqlite3_prepare() to prepare SQL statements.
|
|
|
|
** If SqliteDb.bLegacyPrepare is true, sqlite3_prepare() is used.
|
2000-08-04 17:49:02 +04:00
|
|
|
*/
|
|
|
|
struct SqliteDb {
|
2006-01-03 03:33:50 +03:00
|
|
|
sqlite3 *db; /* The "real" database structure. MUST BE FIRST */
|
2005-06-26 21:55:33 +04:00
|
|
|
Tcl_Interp *interp; /* The interpreter used for this database */
|
|
|
|
char *zBusy; /* The busy callback routine */
|
|
|
|
char *zCommit; /* The commit hook callback routine */
|
|
|
|
char *zTrace; /* The trace callback routine */
|
2016-07-15 00:26:09 +03:00
|
|
|
char *zTraceV2; /* The trace_v2 callback routine */
|
2005-08-30 03:00:03 +04:00
|
|
|
char *zProfile; /* The profile callback routine */
|
2005-06-26 21:55:33 +04:00
|
|
|
char *zProgress; /* The progress callback routine */
|
2019-02-28 20:29:19 +03:00
|
|
|
char *zBindFallback; /* Callback to invoke on a binding miss */
|
2005-06-26 21:55:33 +04:00
|
|
|
char *zAuth; /* The authorization callback routine */
|
2008-08-27 01:33:34 +04:00
|
|
|
int disableAuth; /* Disable the authorizer if it exists */
|
2005-06-26 21:55:33 +04:00
|
|
|
char *zNull; /* Text to substitute for an SQL NULL value */
|
|
|
|
SqlFunc *pFunc; /* List of SQL functions */
|
2005-12-15 18:22:08 +03:00
|
|
|
Tcl_Obj *pUpdateHook; /* Update hook script (if any) */
|
2011-03-01 21:42:07 +03:00
|
|
|
Tcl_Obj *pPreUpdateHook; /* Pre-update hook script (if any) */
|
2005-12-16 09:54:01 +03:00
|
|
|
Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */
|
2010-05-06 00:00:25 +04:00
|
|
|
Tcl_Obj *pWalHook; /* WAL hook script (if any) */
|
2009-03-16 16:19:36 +03:00
|
|
|
Tcl_Obj *pUnlockNotify; /* Unlock notify script (if any) */
|
2005-06-26 21:55:33 +04:00
|
|
|
SqlCollate *pCollate; /* List of SQL collation functions */
|
|
|
|
int rc; /* Return code of most recent sqlite3_exec() */
|
|
|
|
Tcl_Obj *pCollateNeeded; /* Collation needed script */
|
2005-01-24 03:28:42 +03:00
|
|
|
SqlPreparedStmt *stmtList; /* List of prepared statements*/
|
|
|
|
SqlPreparedStmt *stmtLast; /* Last statement in the list */
|
|
|
|
int maxStmt; /* The next maximum number of stmtList */
|
|
|
|
int nStmt; /* Number of statements in stmtList */
|
2007-05-02 17:16:30 +04:00
|
|
|
IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */
|
2010-04-07 23:31:59 +04:00
|
|
|
int nStep, nSort, nIndex; /* Statistics for most recent operation */
|
2017-06-22 19:51:16 +03:00
|
|
|
int nVMStep; /* Another statistic for most recent operation */
|
2009-01-02 20:33:46 +03:00
|
|
|
int nTransaction; /* Number of nested [transaction] methods */
|
2016-01-23 02:17:51 +03:00
|
|
|
int openFlags; /* Flags used to open. (SQLITE_OPEN_URI) */
|
2011-06-27 20:55:50 +04:00
|
|
|
#ifdef SQLITE_TEST
|
|
|
|
int bLegacyPrepare; /* True to use sqlite3_prepare() */
|
|
|
|
#endif
|
2001-10-18 16:34:46 +04:00
|
|
|
};
|
2001-04-05 19:57:13 +04:00
|
|
|
|
2007-05-01 21:49:49 +04:00
|
|
|
struct IncrblobChannel {
|
2007-05-02 17:16:30 +04:00
|
|
|
sqlite3_blob *pBlob; /* sqlite3 blob handle */
|
2007-05-04 22:36:44 +04:00
|
|
|
SqliteDb *pDb; /* Associated database connection */
|
2007-05-02 17:16:30 +04:00
|
|
|
int iSeek; /* Current seek offset */
|
|
|
|
Tcl_Channel channel; /* Channel identifier */
|
|
|
|
IncrblobChannel *pNext; /* Linked list of all open incrblob channels */
|
|
|
|
IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */
|
2007-05-01 21:49:49 +04:00
|
|
|
};
|
|
|
|
|
2008-12-10 22:26:22 +03:00
|
|
|
/*
|
|
|
|
** Compute a string length that is limited to what can be stored in
|
|
|
|
** lower 30 bits of a 32-bit signed integer.
|
|
|
|
*/
|
2008-12-11 01:15:00 +03:00
|
|
|
static int strlen30(const char *z){
|
2008-12-10 22:26:22 +03:00
|
|
|
const char *z2 = z;
|
|
|
|
while( *z2 ){ z2++; }
|
|
|
|
return 0x3fffffff & (int)(z2 - z);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-04 23:03:02 +04:00
|
|
|
#ifndef SQLITE_OMIT_INCRBLOB
|
2007-05-02 17:16:30 +04:00
|
|
|
/*
|
|
|
|
** Close all incrblob channels opened using database connection pDb.
|
|
|
|
** This is called when shutting down the database connection.
|
|
|
|
*/
|
|
|
|
static void closeIncrblobChannels(SqliteDb *pDb){
|
|
|
|
IncrblobChannel *p;
|
|
|
|
IncrblobChannel *pNext;
|
|
|
|
|
|
|
|
for(p=pDb->pIncrblob; p; p=pNext){
|
|
|
|
pNext = p->pNext;
|
|
|
|
|
2016-07-15 00:26:09 +03:00
|
|
|
/* Note: Calling unregister here call Tcl_Close on the incrblob channel,
|
2007-05-02 17:16:30 +04:00
|
|
|
** which deletes the IncrblobChannel structure at *p. So do not
|
|
|
|
** call Tcl_Free() here.
|
|
|
|
*/
|
|
|
|
Tcl_UnregisterChannel(pDb->interp, p->channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-01 21:49:49 +04:00
|
|
|
/*
|
|
|
|
** Close an incremental blob channel.
|
|
|
|
*/
|
2016-07-28 20:11:20 +03:00
|
|
|
static int SQLITE_TCLAPI incrblobClose(
|
|
|
|
ClientData instanceData,
|
|
|
|
Tcl_Interp *interp
|
|
|
|
){
|
2007-05-01 21:49:49 +04:00
|
|
|
IncrblobChannel *p = (IncrblobChannel *)instanceData;
|
2007-05-04 16:05:56 +04:00
|
|
|
int rc = sqlite3_blob_close(p->pBlob);
|
|
|
|
sqlite3 *db = p->pDb->db;
|
2007-05-02 17:16:30 +04:00
|
|
|
|
|
|
|
/* Remove the channel from the SqliteDb.pIncrblob list. */
|
|
|
|
if( p->pNext ){
|
|
|
|
p->pNext->pPrev = p->pPrev;
|
|
|
|
}
|
|
|
|
if( p->pPrev ){
|
|
|
|
p->pPrev->pNext = p->pNext;
|
|
|
|
}
|
|
|
|
if( p->pDb->pIncrblob==p ){
|
|
|
|
p->pDb->pIncrblob = p->pNext;
|
|
|
|
}
|
|
|
|
|
2007-05-04 16:05:56 +04:00
|
|
|
/* Free the IncrblobChannel structure */
|
2007-05-01 21:49:49 +04:00
|
|
|
Tcl_Free((char *)p);
|
2007-05-04 16:05:56 +04:00
|
|
|
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2007-05-01 21:49:49 +04:00
|
|
|
return TCL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Read data from an incremental blob channel.
|
|
|
|
*/
|
2016-07-28 20:11:20 +03:00
|
|
|
static int SQLITE_TCLAPI incrblobInput(
|
2016-07-15 00:26:09 +03:00
|
|
|
ClientData instanceData,
|
|
|
|
char *buf,
|
2007-05-01 21:49:49 +04:00
|
|
|
int bufSize,
|
|
|
|
int *errorCodePtr
|
|
|
|
){
|
|
|
|
IncrblobChannel *p = (IncrblobChannel *)instanceData;
|
|
|
|
int nRead = bufSize; /* Number of bytes to read */
|
|
|
|
int nBlob; /* Total size of the blob */
|
|
|
|
int rc; /* sqlite error code */
|
|
|
|
|
|
|
|
nBlob = sqlite3_blob_bytes(p->pBlob);
|
|
|
|
if( (p->iSeek+nRead)>nBlob ){
|
|
|
|
nRead = nBlob-p->iSeek;
|
|
|
|
}
|
|
|
|
if( nRead<=0 ){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek);
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
*errorCodePtr = rc;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->iSeek += nRead;
|
|
|
|
return nRead;
|
|
|
|
}
|
|
|
|
|
2007-05-02 17:16:30 +04:00
|
|
|
/*
|
|
|
|
** Write data to an incremental blob channel.
|
|
|
|
*/
|
2016-07-28 20:11:20 +03:00
|
|
|
static int SQLITE_TCLAPI incrblobOutput(
|
2016-07-15 00:26:09 +03:00
|
|
|
ClientData instanceData,
|
|
|
|
CONST char *buf,
|
2007-05-01 21:49:49 +04:00
|
|
|
int toWrite,
|
|
|
|
int *errorCodePtr
|
|
|
|
){
|
|
|
|
IncrblobChannel *p = (IncrblobChannel *)instanceData;
|
|
|
|
int nWrite = toWrite; /* Number of bytes to write */
|
|
|
|
int nBlob; /* Total size of the blob */
|
|
|
|
int rc; /* sqlite error code */
|
|
|
|
|
|
|
|
nBlob = sqlite3_blob_bytes(p->pBlob);
|
|
|
|
if( (p->iSeek+nWrite)>nBlob ){
|
|
|
|
*errorCodePtr = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if( nWrite<=0 ){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek);
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
*errorCodePtr = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->iSeek += nWrite;
|
|
|
|
return nWrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Seek an incremental blob channel.
|
|
|
|
*/
|
2016-07-28 20:11:20 +03:00
|
|
|
static int SQLITE_TCLAPI incrblobSeek(
|
2016-07-15 00:26:09 +03:00
|
|
|
ClientData instanceData,
|
2007-05-01 21:49:49 +04:00
|
|
|
long offset,
|
|
|
|
int seekMode,
|
|
|
|
int *errorCodePtr
|
|
|
|
){
|
|
|
|
IncrblobChannel *p = (IncrblobChannel *)instanceData;
|
|
|
|
|
|
|
|
switch( seekMode ){
|
|
|
|
case SEEK_SET:
|
|
|
|
p->iSeek = offset;
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
p->iSeek += offset;
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: assert(!"Bad seekMode");
|
|
|
|
}
|
|
|
|
|
|
|
|
return p->iSeek;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-28 20:11:20 +03:00
|
|
|
static void SQLITE_TCLAPI incrblobWatch(
|
|
|
|
ClientData instanceData,
|
|
|
|
int mode
|
|
|
|
){
|
2016-07-15 00:26:09 +03:00
|
|
|
/* NO-OP */
|
2007-05-01 21:49:49 +04:00
|
|
|
}
|
2016-07-28 20:11:20 +03:00
|
|
|
static int SQLITE_TCLAPI incrblobHandle(
|
|
|
|
ClientData instanceData,
|
|
|
|
int dir,
|
|
|
|
ClientData *hPtr
|
|
|
|
){
|
2007-05-01 21:49:49 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Tcl_ChannelType IncrblobChannelType = {
|
|
|
|
"incrblob", /* typeName */
|
|
|
|
TCL_CHANNEL_VERSION_2, /* version */
|
|
|
|
incrblobClose, /* closeProc */
|
|
|
|
incrblobInput, /* inputProc */
|
|
|
|
incrblobOutput, /* outputProc */
|
|
|
|
incrblobSeek, /* seekProc */
|
|
|
|
0, /* setOptionProc */
|
|
|
|
0, /* getOptionProc */
|
|
|
|
incrblobWatch, /* watchProc (this is a no-op) */
|
|
|
|
incrblobHandle, /* getHandleProc (always returns error) */
|
|
|
|
0, /* close2Proc */
|
|
|
|
0, /* blockModeProc */
|
|
|
|
0, /* flushProc */
|
|
|
|
0, /* handlerProc */
|
|
|
|
0, /* wideSeekProc */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Create a new incrblob channel.
|
|
|
|
*/
|
|
|
|
static int createIncrblobChannel(
|
2016-07-15 00:26:09 +03:00
|
|
|
Tcl_Interp *interp,
|
|
|
|
SqliteDb *pDb,
|
2007-05-01 21:49:49 +04:00
|
|
|
const char *zDb,
|
2016-07-15 00:26:09 +03:00
|
|
|
const char *zTable,
|
|
|
|
const char *zColumn,
|
2007-05-03 20:31:26 +04:00
|
|
|
sqlite_int64 iRow,
|
|
|
|
int isReadonly
|
2007-05-01 21:49:49 +04:00
|
|
|
){
|
|
|
|
IncrblobChannel *p;
|
2007-05-03 20:31:26 +04:00
|
|
|
sqlite3 *db = pDb->db;
|
2007-05-01 21:49:49 +04:00
|
|
|
sqlite3_blob *pBlob;
|
|
|
|
int rc;
|
2007-05-03 20:31:26 +04:00
|
|
|
int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE);
|
2007-05-01 21:49:49 +04:00
|
|
|
|
|
|
|
/* This variable is used to name the channels: "incrblob_[incr count]" */
|
|
|
|
static int count = 0;
|
|
|
|
char zChannel[64];
|
|
|
|
|
2007-05-03 20:31:26 +04:00
|
|
|
rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob);
|
2007-05-01 21:49:49 +04:00
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel));
|
|
|
|
p->iSeek = 0;
|
|
|
|
p->pBlob = pBlob;
|
|
|
|
|
2007-05-04 17:15:55 +04:00
|
|
|
sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count);
|
2007-05-02 17:16:30 +04:00
|
|
|
p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags);
|
|
|
|
Tcl_RegisterChannel(interp, p->channel);
|
|
|
|
|
|
|
|
/* Link the new channel into the SqliteDb.pIncrblob list. */
|
|
|
|
p->pNext = pDb->pIncrblob;
|
|
|
|
p->pPrev = 0;
|
|
|
|
if( p->pNext ){
|
|
|
|
p->pNext->pPrev = p;
|
|
|
|
}
|
|
|
|
pDb->pIncrblob = p;
|
|
|
|
p->pDb = pDb;
|
2007-05-01 21:49:49 +04:00
|
|
|
|
2007-05-02 17:16:30 +04:00
|
|
|
Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE);
|
2007-05-01 21:49:49 +04:00
|
|
|
return TCL_OK;
|
|
|
|
}
|
2007-05-04 23:03:02 +04:00
|
|
|
#else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */
|
|
|
|
#define closeIncrblobChannels(pDb)
|
|
|
|
#endif
|
2007-05-01 21:49:49 +04:00
|
|
|
|
2005-06-26 21:55:33 +04:00
|
|
|
/*
|
|
|
|
** Look at the script prefix in pCmd. We will be executing this script
|
|
|
|
** after first appending one or more arguments. This routine analyzes
|
|
|
|
** the script to see if it is safe to use Tcl_EvalObjv() on the script
|
|
|
|
** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much
|
|
|
|
** faster.
|
|
|
|
**
|
|
|
|
** Scripts that are safe to use with Tcl_EvalObjv() consists of a
|
|
|
|
** command name followed by zero or more arguments with no [...] or $
|
|
|
|
** or {...} or ; to be seen anywhere. Most callback scripts consist
|
|
|
|
** of just a single procedure name and they meet this requirement.
|
|
|
|
*/
|
|
|
|
static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){
|
|
|
|
/* We could try to do something with Tcl_Parse(). But we will instead
|
|
|
|
** just do a search for forbidden characters. If any of the forbidden
|
|
|
|
** characters appear in pCmd, we will report the string as unsafe.
|
|
|
|
*/
|
|
|
|
const char *z;
|
|
|
|
int n;
|
|
|
|
z = Tcl_GetStringFromObj(pCmd, &n);
|
|
|
|
while( n-- > 0 ){
|
|
|
|
int c = *(z++);
|
|
|
|
if( c=='$' || c=='[' || c==';' ) return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Find an SqlFunc structure with the given name. Or create a new
|
|
|
|
** one if an existing one cannot be found. Return a pointer to the
|
|
|
|
** structure.
|
|
|
|
*/
|
|
|
|
static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){
|
|
|
|
SqlFunc *p, *pNew;
|
2013-11-26 20:48:04 +04:00
|
|
|
int nName = strlen30(zName);
|
|
|
|
pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + nName + 1 );
|
2005-06-26 21:55:33 +04:00
|
|
|
pNew->zName = (char*)&pNew[1];
|
2013-11-26 20:48:04 +04:00
|
|
|
memcpy(pNew->zName, zName, nName+1);
|
2016-07-15 00:26:09 +03:00
|
|
|
for(p=pDb->pFunc; p; p=p->pNext){
|
2013-11-26 20:48:04 +04:00
|
|
|
if( sqlite3_stricmp(p->zName, pNew->zName)==0 ){
|
2005-06-26 21:55:33 +04:00
|
|
|
Tcl_Free((char*)pNew);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pNew->interp = pDb->interp;
|
2012-10-03 15:02:33 +04:00
|
|
|
pNew->pDb = pDb;
|
2005-06-26 21:55:33 +04:00
|
|
|
pNew->pScript = 0;
|
|
|
|
pNew->pNext = pDb->pFunc;
|
|
|
|
pDb->pFunc = pNew;
|
|
|
|
return pNew;
|
|
|
|
}
|
|
|
|
|
2011-06-27 20:55:50 +04:00
|
|
|
/*
|
|
|
|
** Free a single SqlPreparedStmt object.
|
|
|
|
*/
|
|
|
|
static void dbFreeStmt(SqlPreparedStmt *pStmt){
|
|
|
|
#ifdef SQLITE_TEST
|
|
|
|
if( sqlite3_sql(pStmt->pStmt)==0 ){
|
|
|
|
Tcl_Free((char *)pStmt->zSql);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
sqlite3_finalize(pStmt->pStmt);
|
|
|
|
Tcl_Free((char *)pStmt);
|
|
|
|
}
|
|
|
|
|
2005-01-24 03:28:42 +03:00
|
|
|
/*
|
|
|
|
** Finalize and free a list of prepared statements
|
|
|
|
*/
|
2011-06-27 20:55:50 +04:00
|
|
|
static void flushStmtCache(SqliteDb *pDb){
|
2005-01-24 03:28:42 +03:00
|
|
|
SqlPreparedStmt *pPreStmt;
|
2011-06-27 20:55:50 +04:00
|
|
|
SqlPreparedStmt *pNext;
|
2005-01-24 03:28:42 +03:00
|
|
|
|
2011-06-27 20:55:50 +04:00
|
|
|
for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pNext){
|
|
|
|
pNext = pPreStmt->pNext;
|
|
|
|
dbFreeStmt(pPreStmt);
|
2005-01-24 03:28:42 +03:00
|
|
|
}
|
|
|
|
pDb->nStmt = 0;
|
|
|
|
pDb->stmtLast = 0;
|
2011-06-27 20:55:50 +04:00
|
|
|
pDb->stmtList = 0;
|
2005-01-24 03:28:42 +03:00
|
|
|
}
|
|
|
|
|
2000-05-29 18:26:00 +04:00
|
|
|
/*
|
2004-08-20 20:02:39 +04:00
|
|
|
** TCL calls this procedure when an sqlite3 database command is
|
|
|
|
** deleted.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2016-07-28 20:11:20 +03:00
|
|
|
static void SQLITE_TCLAPI DbDeleteCmd(void *db){
|
2000-08-04 17:49:02 +04:00
|
|
|
SqliteDb *pDb = (SqliteDb*)db;
|
2005-01-24 03:28:42 +03:00
|
|
|
flushStmtCache(pDb);
|
2007-05-02 17:16:30 +04:00
|
|
|
closeIncrblobChannels(pDb);
|
2004-05-10 14:34:51 +04:00
|
|
|
sqlite3_close(pDb->db);
|
2002-09-14 17:47:32 +04:00
|
|
|
while( pDb->pFunc ){
|
|
|
|
SqlFunc *pFunc = pDb->pFunc;
|
|
|
|
pDb->pFunc = pFunc->pNext;
|
2012-10-03 15:02:33 +04:00
|
|
|
assert( pFunc->pDb==pDb );
|
2005-06-26 21:55:33 +04:00
|
|
|
Tcl_DecrRefCount(pFunc->pScript);
|
2002-09-14 17:47:32 +04:00
|
|
|
Tcl_Free((char*)pFunc);
|
|
|
|
}
|
2004-06-09 13:55:16 +04:00
|
|
|
while( pDb->pCollate ){
|
|
|
|
SqlCollate *pCollate = pDb->pCollate;
|
|
|
|
pDb->pCollate = pCollate->pNext;
|
|
|
|
Tcl_Free((char*)pCollate);
|
|
|
|
}
|
2000-08-04 17:49:02 +04:00
|
|
|
if( pDb->zBusy ){
|
|
|
|
Tcl_Free(pDb->zBusy);
|
|
|
|
}
|
2003-04-23 16:25:23 +04:00
|
|
|
if( pDb->zTrace ){
|
|
|
|
Tcl_Free(pDb->zTrace);
|
2003-04-03 19:46:04 +04:00
|
|
|
}
|
2016-07-15 00:26:09 +03:00
|
|
|
if( pDb->zTraceV2 ){
|
|
|
|
Tcl_Free(pDb->zTraceV2);
|
|
|
|
}
|
2005-08-30 03:00:03 +04:00
|
|
|
if( pDb->zProfile ){
|
|
|
|
Tcl_Free(pDb->zProfile);
|
|
|
|
}
|
2019-02-28 20:29:19 +03:00
|
|
|
if( pDb->zBindFallback ){
|
|
|
|
Tcl_Free(pDb->zBindFallback);
|
|
|
|
}
|
2003-04-23 00:30:37 +04:00
|
|
|
if( pDb->zAuth ){
|
|
|
|
Tcl_Free(pDb->zAuth);
|
|
|
|
}
|
2005-04-04 03:54:43 +04:00
|
|
|
if( pDb->zNull ){
|
|
|
|
Tcl_Free(pDb->zNull);
|
|
|
|
}
|
2005-12-15 18:22:08 +03:00
|
|
|
if( pDb->pUpdateHook ){
|
|
|
|
Tcl_DecrRefCount(pDb->pUpdateHook);
|
|
|
|
}
|
2011-03-01 21:42:07 +03:00
|
|
|
if( pDb->pPreUpdateHook ){
|
|
|
|
Tcl_DecrRefCount(pDb->pPreUpdateHook);
|
|
|
|
}
|
2005-12-16 09:54:01 +03:00
|
|
|
if( pDb->pRollbackHook ){
|
|
|
|
Tcl_DecrRefCount(pDb->pRollbackHook);
|
|
|
|
}
|
2010-05-06 00:00:25 +04:00
|
|
|
if( pDb->pWalHook ){
|
|
|
|
Tcl_DecrRefCount(pDb->pWalHook);
|
2010-04-19 22:03:51 +04:00
|
|
|
}
|
2005-12-15 18:22:08 +03:00
|
|
|
if( pDb->pCollateNeeded ){
|
|
|
|
Tcl_DecrRefCount(pDb->pCollateNeeded);
|
|
|
|
}
|
2000-08-04 17:49:02 +04:00
|
|
|
Tcl_Free((char*)pDb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This routine is called when a database file is locked while trying
|
|
|
|
** to execute SQL.
|
|
|
|
*/
|
2004-06-12 05:43:26 +04:00
|
|
|
static int DbBusyHandler(void *cd, int nTries){
|
2000-08-04 17:49:02 +04:00
|
|
|
SqliteDb *pDb = (SqliteDb*)cd;
|
|
|
|
int rc;
|
|
|
|
char zVal[30];
|
|
|
|
|
2007-05-04 17:15:55 +04:00
|
|
|
sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries);
|
2005-06-26 21:55:33 +04:00
|
|
|
rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0);
|
2000-08-04 17:49:02 +04:00
|
|
|
if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
|
|
|
|
2008-05-01 21:16:52 +04:00
|
|
|
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
|
2003-10-18 13:37:26 +04:00
|
|
|
/*
|
|
|
|
** This routine is invoked as the 'progress callback' for the database.
|
|
|
|
*/
|
|
|
|
static int DbProgressHandler(void *cd){
|
|
|
|
SqliteDb *pDb = (SqliteDb*)cd;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert( pDb->zProgress );
|
|
|
|
rc = Tcl_Eval(pDb->interp, pDb->zProgress);
|
|
|
|
if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2008-05-01 21:16:52 +04:00
|
|
|
#endif
|
2003-10-18 13:37:26 +04:00
|
|
|
|
2016-09-10 22:51:40 +03:00
|
|
|
#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \
|
|
|
|
!defined(SQLITE_OMIT_DEPRECATED)
|
2003-04-03 19:46:04 +04:00
|
|
|
/*
|
2003-04-23 16:25:23 +04:00
|
|
|
** This routine is called by the SQLite trace handler whenever a new
|
|
|
|
** block of SQL is executed. The TCL script in pDb->zTrace is executed.
|
2003-04-03 19:46:04 +04:00
|
|
|
*/
|
2003-04-23 16:25:23 +04:00
|
|
|
static void DbTraceHandler(void *cd, const char *zSql){
|
2003-04-03 19:46:04 +04:00
|
|
|
SqliteDb *pDb = (SqliteDb*)cd;
|
2003-04-23 16:25:23 +04:00
|
|
|
Tcl_DString str;
|
2003-04-03 19:46:04 +04:00
|
|
|
|
2003-04-23 16:25:23 +04:00
|
|
|
Tcl_DStringInit(&str);
|
|
|
|
Tcl_DStringAppend(&str, pDb->zTrace, -1);
|
|
|
|
Tcl_DStringAppendElement(&str, zSql);
|
|
|
|
Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
|
|
|
|
Tcl_DStringFree(&str);
|
|
|
|
Tcl_ResetResult(pDb->interp);
|
2003-04-03 19:46:04 +04:00
|
|
|
}
|
2006-01-23 16:00:35 +03:00
|
|
|
#endif
|
2003-04-03 19:46:04 +04:00
|
|
|
|
2016-07-15 00:26:09 +03:00
|
|
|
#ifndef SQLITE_OMIT_TRACE
|
|
|
|
/*
|
|
|
|
** This routine is called by the SQLite trace_v2 handler whenever a new
|
|
|
|
** supported event is generated. Unsupported event types are ignored.
|
|
|
|
** The TCL script in pDb->zTraceV2 is executed, with the arguments for
|
|
|
|
** the event appended to it (as list elements).
|
|
|
|
*/
|
|
|
|
static int DbTraceV2Handler(
|
|
|
|
unsigned type, /* One of the SQLITE_TRACE_* event types. */
|
|
|
|
void *cd, /* The original context data pointer. */
|
|
|
|
void *pd, /* Primary event data, depends on event type. */
|
|
|
|
void *xd /* Extra event data, depends on event type. */
|
|
|
|
){
|
|
|
|
SqliteDb *pDb = (SqliteDb*)cd;
|
|
|
|
Tcl_Obj *pCmd;
|
|
|
|
|
|
|
|
switch( type ){
|
|
|
|
case SQLITE_TRACE_STMT: {
|
|
|
|
sqlite3_stmt *pStmt = (sqlite3_stmt *)pd;
|
|
|
|
char *zSql = (char *)xd;
|
|
|
|
|
|
|
|
pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1);
|
|
|
|
Tcl_IncrRefCount(pCmd);
|
|
|
|
Tcl_ListObjAppendElement(pDb->interp, pCmd,
|
|
|
|
Tcl_NewWideIntObj((Tcl_WideInt)pStmt));
|
|
|
|
Tcl_ListObjAppendElement(pDb->interp, pCmd,
|
|
|
|
Tcl_NewStringObj(zSql, -1));
|
|
|
|
Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
|
|
|
|
Tcl_DecrRefCount(pCmd);
|
|
|
|
Tcl_ResetResult(pDb->interp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_TRACE_PROFILE: {
|
|
|
|
sqlite3_stmt *pStmt = (sqlite3_stmt *)pd;
|
2018-03-10 23:25:08 +03:00
|
|
|
sqlite3_int64 ns = *(sqlite3_int64*)xd;
|
2016-07-15 00:26:09 +03:00
|
|
|
|
|
|
|
pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1);
|
|
|
|
Tcl_IncrRefCount(pCmd);
|
|
|
|
Tcl_ListObjAppendElement(pDb->interp, pCmd,
|
|
|
|
Tcl_NewWideIntObj((Tcl_WideInt)pStmt));
|
|
|
|
Tcl_ListObjAppendElement(pDb->interp, pCmd,
|
|
|
|
Tcl_NewWideIntObj((Tcl_WideInt)ns));
|
|
|
|
Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
|
|
|
|
Tcl_DecrRefCount(pCmd);
|
|
|
|
Tcl_ResetResult(pDb->interp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_TRACE_ROW: {
|
|
|
|
sqlite3_stmt *pStmt = (sqlite3_stmt *)pd;
|
|
|
|
|
|
|
|
pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1);
|
|
|
|
Tcl_IncrRefCount(pCmd);
|
|
|
|
Tcl_ListObjAppendElement(pDb->interp, pCmd,
|
|
|
|
Tcl_NewWideIntObj((Tcl_WideInt)pStmt));
|
|
|
|
Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
|
|
|
|
Tcl_DecrRefCount(pCmd);
|
|
|
|
Tcl_ResetResult(pDb->interp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_TRACE_CLOSE: {
|
|
|
|
sqlite3 *db = (sqlite3 *)pd;
|
|
|
|
|
|
|
|
pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1);
|
|
|
|
Tcl_IncrRefCount(pCmd);
|
|
|
|
Tcl_ListObjAppendElement(pDb->interp, pCmd,
|
|
|
|
Tcl_NewWideIntObj((Tcl_WideInt)db));
|
|
|
|
Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
|
|
|
|
Tcl_DecrRefCount(pCmd);
|
|
|
|
Tcl_ResetResult(pDb->interp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-09-10 22:51:40 +03:00
|
|
|
#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \
|
|
|
|
!defined(SQLITE_OMIT_DEPRECATED)
|
2005-08-30 03:00:03 +04:00
|
|
|
/*
|
|
|
|
** This routine is called by the SQLite profile handler after a statement
|
|
|
|
** SQL has executed. The TCL script in pDb->zProfile is evaluated.
|
|
|
|
*/
|
|
|
|
static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){
|
|
|
|
SqliteDb *pDb = (SqliteDb*)cd;
|
|
|
|
Tcl_DString str;
|
|
|
|
char zTm[100];
|
|
|
|
|
|
|
|
sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm);
|
|
|
|
Tcl_DStringInit(&str);
|
|
|
|
Tcl_DStringAppend(&str, pDb->zProfile, -1);
|
|
|
|
Tcl_DStringAppendElement(&str, zSql);
|
|
|
|
Tcl_DStringAppendElement(&str, zTm);
|
|
|
|
Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
|
|
|
|
Tcl_DStringFree(&str);
|
|
|
|
Tcl_ResetResult(pDb->interp);
|
|
|
|
}
|
2006-01-23 16:00:35 +03:00
|
|
|
#endif
|
2005-08-30 03:00:03 +04:00
|
|
|
|
2004-01-15 05:44:03 +03:00
|
|
|
/*
|
|
|
|
** This routine is called when a transaction is committed. The
|
|
|
|
** TCL script in pDb->zCommit is executed. If it returns non-zero or
|
|
|
|
** if it throws an exception, the transaction is rolled back instead
|
|
|
|
** of being committed.
|
|
|
|
*/
|
|
|
|
static int DbCommitHandler(void *cd){
|
|
|
|
SqliteDb *pDb = (SqliteDb*)cd;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = Tcl_Eval(pDb->interp, pDb->zCommit);
|
|
|
|
if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-16 09:54:01 +03:00
|
|
|
static void DbRollbackHandler(void *clientData){
|
|
|
|
SqliteDb *pDb = (SqliteDb*)clientData;
|
|
|
|
assert(pDb->pRollbackHook);
|
|
|
|
if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){
|
|
|
|
Tcl_BackgroundError(pDb->interp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-06 00:00:25 +04:00
|
|
|
/*
|
|
|
|
** This procedure handles wal_hook callbacks.
|
|
|
|
*/
|
|
|
|
static int DbWalHandler(
|
2016-07-15 00:26:09 +03:00
|
|
|
void *clientData,
|
|
|
|
sqlite3 *db,
|
|
|
|
const char *zDb,
|
2010-04-19 22:03:51 +04:00
|
|
|
int nEntry
|
|
|
|
){
|
2010-05-06 00:00:25 +04:00
|
|
|
int ret = SQLITE_OK;
|
2010-04-19 22:03:51 +04:00
|
|
|
Tcl_Obj *p;
|
|
|
|
SqliteDb *pDb = (SqliteDb*)clientData;
|
|
|
|
Tcl_Interp *interp = pDb->interp;
|
2010-05-06 00:00:25 +04:00
|
|
|
assert(pDb->pWalHook);
|
2010-04-19 22:03:51 +04:00
|
|
|
|
2014-12-10 23:29:49 +03:00
|
|
|
assert( db==pDb->db );
|
2010-05-06 00:00:25 +04:00
|
|
|
p = Tcl_DuplicateObj(pDb->pWalHook);
|
2010-04-19 22:03:51 +04:00
|
|
|
Tcl_IncrRefCount(p);
|
|
|
|
Tcl_ListObjAppendElement(interp, p, Tcl_NewStringObj(zDb, -1));
|
|
|
|
Tcl_ListObjAppendElement(interp, p, Tcl_NewIntObj(nEntry));
|
2016-07-15 00:26:09 +03:00
|
|
|
if( TCL_OK!=Tcl_EvalObjEx(interp, p, 0)
|
2010-04-19 22:03:51 +04:00
|
|
|
|| TCL_OK!=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &ret)
|
|
|
|
){
|
|
|
|
Tcl_BackgroundError(interp);
|
|
|
|
}
|
|
|
|
Tcl_DecrRefCount(p);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-03-27 15:44:35 +03:00
|
|
|
#if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_UNLOCK_NOTIFY)
|
2009-03-16 16:19:36 +03:00
|
|
|
static void setTestUnlockNotifyVars(Tcl_Interp *interp, int iArg, int nArg){
|
|
|
|
char zBuf[64];
|
2015-01-19 03:35:53 +03:00
|
|
|
sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", iArg);
|
2009-03-16 16:19:36 +03:00
|
|
|
Tcl_SetVar(interp, "sqlite_unlock_notify_arg", zBuf, TCL_GLOBAL_ONLY);
|
2015-01-19 03:35:53 +03:00
|
|
|
sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nArg);
|
2009-03-16 16:19:36 +03:00
|
|
|
Tcl_SetVar(interp, "sqlite_unlock_notify_argcount", zBuf, TCL_GLOBAL_ONLY);
|
|
|
|
}
|
|
|
|
#else
|
2009-03-27 15:44:35 +03:00
|
|
|
# define setTestUnlockNotifyVars(x,y,z)
|
2009-03-16 16:19:36 +03:00
|
|
|
#endif
|
|
|
|
|
2009-03-27 15:32:54 +03:00
|
|
|
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
|
2009-03-16 16:19:36 +03:00
|
|
|
static void DbUnlockNotify(void **apArg, int nArg){
|
|
|
|
int i;
|
|
|
|
for(i=0; i<nArg; i++){
|
|
|
|
const int flags = (TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT);
|
|
|
|
SqliteDb *pDb = (SqliteDb *)apArg[i];
|
|
|
|
setTestUnlockNotifyVars(pDb->interp, i, nArg);
|
|
|
|
assert( pDb->pUnlockNotify);
|
|
|
|
Tcl_EvalObjEx(pDb->interp, pDb->pUnlockNotify, flags);
|
|
|
|
Tcl_DecrRefCount(pDb->pUnlockNotify);
|
|
|
|
pDb->pUnlockNotify = 0;
|
|
|
|
}
|
|
|
|
}
|
2009-03-27 15:32:54 +03:00
|
|
|
#endif
|
2009-03-16 16:19:36 +03:00
|
|
|
|
2011-03-31 01:04:43 +04:00
|
|
|
#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
|
2011-03-01 21:42:07 +03:00
|
|
|
/*
|
|
|
|
** Pre-update hook callback.
|
|
|
|
*/
|
|
|
|
static void DbPreUpdateHandler(
|
2016-07-15 00:26:09 +03:00
|
|
|
void *p,
|
2011-03-01 21:42:07 +03:00
|
|
|
sqlite3 *db,
|
|
|
|
int op,
|
2016-07-15 00:26:09 +03:00
|
|
|
const char *zDb,
|
|
|
|
const char *zTbl,
|
2011-03-01 21:42:07 +03:00
|
|
|
sqlite_int64 iKey1,
|
|
|
|
sqlite_int64 iKey2
|
|
|
|
){
|
|
|
|
SqliteDb *pDb = (SqliteDb *)p;
|
|
|
|
Tcl_Obj *pCmd;
|
|
|
|
static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"};
|
|
|
|
|
|
|
|
assert( (SQLITE_DELETE-1)/9 == 0 );
|
|
|
|
assert( (SQLITE_INSERT-1)/9 == 1 );
|
|
|
|
assert( (SQLITE_UPDATE-1)/9 == 2 );
|
|
|
|
assert( pDb->pPreUpdateHook );
|
|
|
|
assert( db==pDb->db );
|
|
|
|
assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
|
|
|
|
|
|
|
|
pCmd = Tcl_DuplicateObj(pDb->pPreUpdateHook);
|
|
|
|
Tcl_IncrRefCount(pCmd);
|
|
|
|
Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1));
|
|
|
|
Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1));
|
|
|
|
Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1));
|
|
|
|
Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey1));
|
|
|
|
Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey2));
|
|
|
|
Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
|
|
|
|
Tcl_DecrRefCount(pCmd);
|
|
|
|
}
|
2011-03-31 01:04:43 +04:00
|
|
|
#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
|
2011-03-01 21:42:07 +03:00
|
|
|
|
2005-12-15 18:22:08 +03:00
|
|
|
static void DbUpdateHandler(
|
2016-07-15 00:26:09 +03:00
|
|
|
void *p,
|
2005-12-15 18:22:08 +03:00
|
|
|
int op,
|
2016-07-15 00:26:09 +03:00
|
|
|
const char *zDb,
|
|
|
|
const char *zTbl,
|
2005-12-15 18:22:08 +03:00
|
|
|
sqlite_int64 rowid
|
|
|
|
){
|
|
|
|
SqliteDb *pDb = (SqliteDb *)p;
|
|
|
|
Tcl_Obj *pCmd;
|
2011-03-01 21:42:07 +03:00
|
|
|
static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"};
|
|
|
|
|
|
|
|
assert( (SQLITE_DELETE-1)/9 == 0 );
|
|
|
|
assert( (SQLITE_INSERT-1)/9 == 1 );
|
|
|
|
assert( (SQLITE_UPDATE-1)/9 == 2 );
|
2005-12-15 18:22:08 +03:00
|
|
|
|
|
|
|
assert( pDb->pUpdateHook );
|
|
|
|
assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
|
|
|
|
|
|
|
|
pCmd = Tcl_DuplicateObj(pDb->pUpdateHook);
|
|
|
|
Tcl_IncrRefCount(pCmd);
|
2011-03-01 21:42:07 +03:00
|
|
|
Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1));
|
2005-12-15 18:22:08 +03:00
|
|
|
Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1));
|
|
|
|
Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1));
|
|
|
|
Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid));
|
|
|
|
Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
|
2010-10-27 19:36:21 +04:00
|
|
|
Tcl_DecrRefCount(pCmd);
|
2005-12-15 18:22:08 +03:00
|
|
|
}
|
|
|
|
|
2004-06-10 14:50:08 +04:00
|
|
|
static void tclCollateNeeded(
|
|
|
|
void *pCtx,
|
2004-09-06 21:24:11 +04:00
|
|
|
sqlite3 *db,
|
2004-06-10 14:50:08 +04:00
|
|
|
int enc,
|
|
|
|
const char *zName
|
|
|
|
){
|
|
|
|
SqliteDb *pDb = (SqliteDb *)pCtx;
|
|
|
|
Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
|
|
|
|
Tcl_IncrRefCount(pScript);
|
|
|
|
Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
|
|
|
|
Tcl_EvalObjEx(pDb->interp, pScript, 0);
|
|
|
|
Tcl_DecrRefCount(pScript);
|
|
|
|
}
|
|
|
|
|
2004-06-09 13:55:16 +04:00
|
|
|
/*
|
|
|
|
** This routine is called to evaluate an SQL collation function implemented
|
|
|
|
** using TCL script.
|
|
|
|
*/
|
|
|
|
static int tclSqlCollate(
|
|
|
|
void *pCtx,
|
|
|
|
int nA,
|
|
|
|
const void *zA,
|
|
|
|
int nB,
|
|
|
|
const void *zB
|
|
|
|
){
|
|
|
|
SqlCollate *p = (SqlCollate *)pCtx;
|
|
|
|
Tcl_Obj *pCmd;
|
|
|
|
|
|
|
|
pCmd = Tcl_NewStringObj(p->zScript, -1);
|
|
|
|
Tcl_IncrRefCount(pCmd);
|
|
|
|
Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA));
|
|
|
|
Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB));
|
2005-06-26 21:55:33 +04:00
|
|
|
Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
|
2004-06-09 13:55:16 +04:00
|
|
|
Tcl_DecrRefCount(pCmd);
|
|
|
|
return (atoi(Tcl_GetStringResult(p->interp)));
|
|
|
|
}
|
|
|
|
|
2002-09-14 17:47:32 +04:00
|
|
|
/*
|
|
|
|
** This routine is called to evaluate an SQL function implemented
|
|
|
|
** using TCL script.
|
|
|
|
*/
|
2005-01-24 03:28:42 +03:00
|
|
|
static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){
|
2004-05-10 14:34:51 +04:00
|
|
|
SqlFunc *p = sqlite3_user_data(context);
|
2005-06-26 21:55:33 +04:00
|
|
|
Tcl_Obj *pCmd;
|
2002-09-14 17:47:32 +04:00
|
|
|
int i;
|
|
|
|
int rc;
|
|
|
|
|
2005-06-26 21:55:33 +04:00
|
|
|
if( argc==0 ){
|
|
|
|
/* If there are no arguments to the function, call Tcl_EvalObjEx on the
|
|
|
|
** script object directly. This allows the TCL compiler to generate
|
|
|
|
** bytecode for the command on the first invocation and thus make
|
|
|
|
** subsequent invocations much faster. */
|
|
|
|
pCmd = p->pScript;
|
|
|
|
Tcl_IncrRefCount(pCmd);
|
|
|
|
rc = Tcl_EvalObjEx(p->interp, pCmd, 0);
|
|
|
|
Tcl_DecrRefCount(pCmd);
|
|
|
|
}else{
|
|
|
|
/* If there are arguments to the function, make a shallow copy of the
|
|
|
|
** script object, lappend the arguments, then evaluate the copy.
|
|
|
|
**
|
2014-09-06 20:39:46 +04:00
|
|
|
** By "shallow" copy, we mean only the outer list Tcl_Obj is duplicated.
|
2016-07-15 00:26:09 +03:00
|
|
|
** The new Tcl_Obj contains pointers to the original list elements.
|
2005-06-26 21:55:33 +04:00
|
|
|
** That way, when Tcl_EvalObjv() is run and shimmers the first element
|
|
|
|
** of the list to tclCmdNameType, that alternate representation will
|
|
|
|
** be preserved and reused on the next invocation.
|
|
|
|
*/
|
|
|
|
Tcl_Obj **aArg;
|
|
|
|
int nArg;
|
|
|
|
if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){
|
2016-07-15 00:26:09 +03:00
|
|
|
sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
|
2005-06-26 21:55:33 +04:00
|
|
|
return;
|
2016-07-15 00:26:09 +03:00
|
|
|
}
|
2005-06-26 21:55:33 +04:00
|
|
|
pCmd = Tcl_NewListObj(nArg, aArg);
|
|
|
|
Tcl_IncrRefCount(pCmd);
|
|
|
|
for(i=0; i<argc; i++){
|
|
|
|
sqlite3_value *pIn = argv[i];
|
|
|
|
Tcl_Obj *pVal;
|
2016-07-15 00:26:09 +03:00
|
|
|
|
2005-06-26 21:55:33 +04:00
|
|
|
/* Set pVal to contain the i'th column of this row. */
|
|
|
|
switch( sqlite3_value_type(pIn) ){
|
|
|
|
case SQLITE_BLOB: {
|
|
|
|
int bytes = sqlite3_value_bytes(pIn);
|
|
|
|
pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_INTEGER: {
|
|
|
|
sqlite_int64 v = sqlite3_value_int64(pIn);
|
|
|
|
if( v>=-2147483647 && v<=2147483647 ){
|
2011-06-20 23:00:30 +04:00
|
|
|
pVal = Tcl_NewIntObj((int)v);
|
2005-06-26 21:55:33 +04:00
|
|
|
}else{
|
|
|
|
pVal = Tcl_NewWideIntObj(v);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_FLOAT: {
|
|
|
|
double r = sqlite3_value_double(pIn);
|
|
|
|
pVal = Tcl_NewDoubleObj(r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_NULL: {
|
2012-10-03 15:02:33 +04:00
|
|
|
pVal = Tcl_NewStringObj(p->pDb->zNull, -1);
|
2005-06-26 21:55:33 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
int bytes = sqlite3_value_bytes(pIn);
|
2005-12-07 09:27:43 +03:00
|
|
|
pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes);
|
2005-06-26 21:55:33 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal);
|
|
|
|
if( rc ){
|
|
|
|
Tcl_DecrRefCount(pCmd);
|
2016-07-15 00:26:09 +03:00
|
|
|
sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
|
2005-06-26 21:55:33 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !p->useEvalObjv ){
|
|
|
|
/* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd
|
|
|
|
** is a list without a string representation. To prevent this from
|
|
|
|
** happening, make sure pCmd has a valid string representation */
|
|
|
|
Tcl_GetString(pCmd);
|
2004-05-24 16:39:02 +04:00
|
|
|
}
|
2005-06-26 21:55:33 +04:00
|
|
|
rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
|
|
|
|
Tcl_DecrRefCount(pCmd);
|
2002-09-14 17:47:32 +04:00
|
|
|
}
|
2005-05-20 13:40:55 +04:00
|
|
|
|
2005-05-05 14:30:29 +04:00
|
|
|
if( rc && rc!=TCL_RETURN ){
|
2016-07-15 00:26:09 +03:00
|
|
|
sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
|
2002-09-14 17:47:32 +04:00
|
|
|
}else{
|
2005-05-05 14:30:29 +04:00
|
|
|
Tcl_Obj *pVar = Tcl_GetObjResult(p->interp);
|
|
|
|
int n;
|
|
|
|
u8 *data;
|
2009-10-06 18:59:02 +04:00
|
|
|
const char *zType = (pVar->typePtr ? pVar->typePtr->name : "");
|
2005-05-05 14:30:29 +04:00
|
|
|
char c = zType[0];
|
2019-02-27 19:38:19 +03:00
|
|
|
int eType = p->eType;
|
|
|
|
|
|
|
|
if( eType==SQLITE_NULL ){
|
|
|
|
if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){
|
|
|
|
/* Only return a BLOB type if the Tcl variable is a bytearray and
|
|
|
|
** has no string representation. */
|
|
|
|
eType = SQLITE_BLOB;
|
|
|
|
}else if( (c=='b' && strcmp(zType,"boolean")==0)
|
|
|
|
|| (c=='w' && strcmp(zType,"wideInt")==0)
|
|
|
|
|| (c=='i' && strcmp(zType,"int")==0)
|
|
|
|
){
|
|
|
|
eType = SQLITE_INTEGER;
|
|
|
|
}else if( c=='d' && strcmp(zType,"double")==0 ){
|
|
|
|
eType = SQLITE_FLOAT;
|
|
|
|
}else{
|
|
|
|
eType = SQLITE_TEXT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( eType ){
|
|
|
|
case SQLITE_BLOB: {
|
|
|
|
data = Tcl_GetByteArrayFromObj(pVar, &n);
|
|
|
|
sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_INTEGER: {
|
|
|
|
Tcl_WideInt v;
|
|
|
|
if( TCL_OK==Tcl_GetWideIntFromObj(0, pVar, &v) ){
|
|
|
|
sqlite3_result_int64(context, v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fall-through */
|
|
|
|
}
|
|
|
|
case SQLITE_FLOAT: {
|
|
|
|
double r;
|
|
|
|
if( TCL_OK==Tcl_GetDoubleFromObj(0, pVar, &r) ){
|
|
|
|
sqlite3_result_double(context, r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fall-through */
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n);
|
|
|
|
sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT);
|
|
|
|
break;
|
|
|
|
}
|
2005-05-05 14:30:29 +04:00
|
|
|
}
|
2019-02-27 19:38:19 +03:00
|
|
|
|
2002-09-14 17:47:32 +04:00
|
|
|
}
|
|
|
|
}
|
2004-08-20 20:02:39 +04:00
|
|
|
|
2003-04-23 00:30:37 +04:00
|
|
|
#ifndef SQLITE_OMIT_AUTHORIZATION
|
|
|
|
/*
|
|
|
|
** This is the authentication function. It appends the authentication
|
|
|
|
** type code and the two arguments to zCmd[] then invokes the result
|
|
|
|
** on the interpreter. The reply is examined to determine if the
|
|
|
|
** authentication fails or succeeds.
|
|
|
|
*/
|
|
|
|
static int auth_callback(
|
|
|
|
void *pArg,
|
|
|
|
int code,
|
|
|
|
const char *zArg1,
|
|
|
|
const char *zArg2,
|
|
|
|
const char *zArg3,
|
|
|
|
const char *zArg4
|
2014-09-11 17:44:52 +04:00
|
|
|
#ifdef SQLITE_USER_AUTHENTICATION
|
|
|
|
,const char *zArg5
|
|
|
|
#endif
|
2003-04-23 00:30:37 +04:00
|
|
|
){
|
2014-01-24 21:03:55 +04:00
|
|
|
const char *zCode;
|
2003-04-23 00:30:37 +04:00
|
|
|
Tcl_DString str;
|
|
|
|
int rc;
|
|
|
|
const char *zReply;
|
2017-05-11 16:43:57 +03:00
|
|
|
/* EVIDENCE-OF: R-38590-62769 The first parameter to the authorizer
|
|
|
|
** callback is a copy of the third parameter to the
|
|
|
|
** sqlite3_set_authorizer() interface.
|
|
|
|
*/
|
2003-04-23 00:30:37 +04:00
|
|
|
SqliteDb *pDb = (SqliteDb*)pArg;
|
2008-08-27 01:33:34 +04:00
|
|
|
if( pDb->disableAuth ) return SQLITE_OK;
|
2003-04-23 00:30:37 +04:00
|
|
|
|
2017-05-11 16:43:57 +03:00
|
|
|
/* EVIDENCE-OF: R-56518-44310 The second parameter to the callback is an
|
|
|
|
** integer action code that specifies the particular action to be
|
|
|
|
** authorized. */
|
2003-04-23 00:30:37 +04:00
|
|
|
switch( code ){
|
|
|
|
case SQLITE_COPY : zCode="SQLITE_COPY"; break;
|
|
|
|
case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
|
|
|
|
case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
|
|
|
|
case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
|
|
|
|
case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
|
|
|
|
case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
|
|
|
|
case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
|
|
|
|
case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
|
|
|
|
case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
|
|
|
|
case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
|
|
|
|
case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
|
|
|
|
case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
|
|
|
|
case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
|
|
|
|
case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
|
|
|
|
case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
|
|
|
|
case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
|
|
|
|
case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
|
|
|
|
case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
|
|
|
|
case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
|
|
|
|
case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
|
|
|
|
case SQLITE_READ : zCode="SQLITE_READ"; break;
|
|
|
|
case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
|
|
|
|
case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
|
|
|
|
case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
|
2003-06-06 23:00:42 +04:00
|
|
|
case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
|
|
|
|
case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
|
2004-11-12 18:53:37 +03:00
|
|
|
case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break;
|
2004-11-23 18:41:16 +03:00
|
|
|
case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break;
|
2005-07-23 06:17:03 +04:00
|
|
|
case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break;
|
2006-06-16 12:01:02 +04:00
|
|
|
case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break;
|
|
|
|
case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break;
|
2006-08-24 18:59:45 +04:00
|
|
|
case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break;
|
2008-12-30 09:24:58 +03:00
|
|
|
case SQLITE_SAVEPOINT : zCode="SQLITE_SAVEPOINT"; break;
|
2014-01-17 02:40:02 +04:00
|
|
|
case SQLITE_RECURSIVE : zCode="SQLITE_RECURSIVE"; break;
|
2003-04-23 00:30:37 +04:00
|
|
|
default : zCode="????"; break;
|
|
|
|
}
|
|
|
|
Tcl_DStringInit(&str);
|
|
|
|
Tcl_DStringAppend(&str, pDb->zAuth, -1);
|
|
|
|
Tcl_DStringAppendElement(&str, zCode);
|
|
|
|
Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
|
|
|
|
Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
|
|
|
|
Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
|
|
|
|
Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
|
2014-09-11 17:44:52 +04:00
|
|
|
#ifdef SQLITE_USER_AUTHENTICATION
|
|
|
|
Tcl_DStringAppendElement(&str, zArg5 ? zArg5 : "");
|
2016-07-15 00:26:09 +03:00
|
|
|
#endif
|
2003-04-23 00:30:37 +04:00
|
|
|
rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
|
|
|
|
Tcl_DStringFree(&str);
|
2011-10-15 01:49:18 +04:00
|
|
|
zReply = rc==TCL_OK ? Tcl_GetStringResult(pDb->interp) : "SQLITE_DENY";
|
2003-04-23 00:30:37 +04:00
|
|
|
if( strcmp(zReply,"SQLITE_OK")==0 ){
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
}else if( strcmp(zReply,"SQLITE_DENY")==0 ){
|
|
|
|
rc = SQLITE_DENY;
|
|
|
|
}else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
|
|
|
|
rc = SQLITE_IGNORE;
|
|
|
|
}else{
|
|
|
|
rc = 999;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif /* SQLITE_OMIT_AUTHORIZATION */
|
2002-09-14 17:47:32 +04:00
|
|
|
|
2004-12-17 18:41:11 +03:00
|
|
|
/*
|
|
|
|
** This routine reads a line of text from FILE in, stores
|
|
|
|
** the text in memory obtained from malloc() and returns a pointer
|
|
|
|
** to the text. NULL is returned at end of file, or if malloc()
|
|
|
|
** fails.
|
|
|
|
**
|
|
|
|
** The interface is like "readline" but no command-line editing
|
|
|
|
** is done.
|
|
|
|
**
|
|
|
|
** copied from shell.c from '.import' command
|
|
|
|
*/
|
|
|
|
static char *local_getline(char *zPrompt, FILE *in){
|
|
|
|
char *zLine;
|
|
|
|
int nLine;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
nLine = 100;
|
|
|
|
zLine = malloc( nLine );
|
|
|
|
if( zLine==0 ) return 0;
|
|
|
|
n = 0;
|
2011-10-15 01:49:18 +04:00
|
|
|
while( 1 ){
|
2004-12-17 18:41:11 +03:00
|
|
|
if( n+100>nLine ){
|
|
|
|
nLine = nLine*2 + 100;
|
|
|
|
zLine = realloc(zLine, nLine);
|
|
|
|
if( zLine==0 ) return 0;
|
|
|
|
}
|
|
|
|
if( fgets(&zLine[n], nLine - n, in)==0 ){
|
|
|
|
if( n==0 ){
|
|
|
|
free(zLine);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
zLine[n] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while( zLine[n] ){ n++; }
|
|
|
|
if( n>0 && zLine[n-1]=='\n' ){
|
|
|
|
n--;
|
|
|
|
zLine[n] = 0;
|
2011-10-15 01:49:18 +04:00
|
|
|
break;
|
2004-12-17 18:41:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
zLine = realloc( zLine, n+1 );
|
|
|
|
return zLine;
|
|
|
|
}
|
|
|
|
|
2007-11-13 13:30:24 +03:00
|
|
|
|
|
|
|
/*
|
2009-10-06 18:59:02 +04:00
|
|
|
** This function is part of the implementation of the command:
|
2007-11-13 13:30:24 +03:00
|
|
|
**
|
2009-10-06 18:59:02 +04:00
|
|
|
** $db transaction [-deferred|-immediate|-exclusive] SCRIPT
|
2007-11-13 13:30:24 +03:00
|
|
|
**
|
2009-10-06 18:59:02 +04:00
|
|
|
** It is invoked after evaluating the script SCRIPT to commit or rollback
|
|
|
|
** the transaction or savepoint opened by the [transaction] command.
|
|
|
|
*/
|
2016-07-28 21:06:52 +03:00
|
|
|
static int SQLITE_TCLAPI DbTransPostCmd(
|
2009-10-06 18:59:02 +04:00
|
|
|
ClientData data[], /* data[0] is the Sqlite3Db* for $db */
|
|
|
|
Tcl_Interp *interp, /* Tcl interpreter */
|
|
|
|
int result /* Result of evaluating SCRIPT */
|
|
|
|
){
|
2014-01-24 21:03:55 +04:00
|
|
|
static const char *const azEnd[] = {
|
2009-10-06 18:59:02 +04:00
|
|
|
"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
|
2016-07-15 00:26:09 +03:00
|
|
|
** error is that the exec() above was an attempt to commit the
|
2009-10-06 18:59:02 +04:00
|
|
|
** top-level transaction that returned SQLITE_BUSY. Or, less likely,
|
2013-03-22 01:20:32 +04:00
|
|
|
** that an IO-error has occurred. In either case, throw a Tcl exception
|
2009-10-06 18:59:02 +04:00
|
|
|
** and try to rollback the transaction.
|
|
|
|
**
|
2016-07-15 00:26:09 +03:00
|
|
|
** But it could also be that the user executed one or more BEGIN,
|
2009-10-06 18:59:02 +04:00
|
|
|
** 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 ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0);
|
2009-10-06 18:59:02 +04:00
|
|
|
rc = TCL_ERROR;
|
|
|
|
}
|
|
|
|
sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0);
|
|
|
|
}
|
|
|
|
pDb->disableAuth--;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2011-06-27 20:55:50 +04:00
|
|
|
/*
|
|
|
|
** Unless SQLITE_TEST is defined, this function is a simple wrapper around
|
|
|
|
** sqlite3_prepare_v2(). If SQLITE_TEST is defined, then it uses either
|
|
|
|
** sqlite3_prepare_v2() or legacy interface sqlite3_prepare(), depending
|
2016-07-15 00:26:09 +03:00
|
|
|
** on whether or not the [db_use_legacy_prepare] command has been used to
|
2011-06-27 20:55:50 +04:00
|
|
|
** configure the connection.
|
|
|
|
*/
|
|
|
|
static int dbPrepare(
|
|
|
|
SqliteDb *pDb, /* Database object */
|
|
|
|
const char *zSql, /* SQL to compile */
|
|
|
|
sqlite3_stmt **ppStmt, /* OUT: Prepared statement */
|
|
|
|
const char **pzOut /* OUT: Pointer to next SQL statement */
|
|
|
|
){
|
2017-06-01 03:54:35 +03:00
|
|
|
unsigned int prepFlags = 0;
|
2011-06-27 20:55:50 +04:00
|
|
|
#ifdef SQLITE_TEST
|
|
|
|
if( pDb->bLegacyPrepare ){
|
|
|
|
return sqlite3_prepare(pDb->db, zSql, -1, ppStmt, pzOut);
|
|
|
|
}
|
|
|
|
#endif
|
2017-06-01 03:54:35 +03:00
|
|
|
/* If the statement cache is large, use the SQLITE_PREPARE_PERSISTENT
|
|
|
|
** flags, which uses less lookaside memory. But if the cache is small,
|
|
|
|
** omit that flag to make full use of lookaside */
|
|
|
|
if( pDb->maxStmt>5 ) prepFlags = SQLITE_PREPARE_PERSISTENT;
|
|
|
|
|
|
|
|
return sqlite3_prepare_v3(pDb->db, zSql, -1, prepFlags, ppStmt, pzOut);
|
2011-06-27 20:55:50 +04:00
|
|
|
}
|
|
|
|
|
2009-10-06 18:59:02 +04:00
|
|
|
/*
|
|
|
|
** 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 */
|
2015-01-12 22:59:12 +03:00
|
|
|
sqlite3_stmt *pStmt = 0; /* Prepared statement object */
|
2009-10-06 18:59:02 +04:00
|
|
|
SqlPreparedStmt *pPreStmt; /* Pointer to cached statement */
|
|
|
|
int nSql; /* Length of zSql in bytes */
|
2015-01-12 22:59:12 +03:00
|
|
|
int nVar = 0; /* Number of variables in statement */
|
2009-10-06 18:59:02 +04:00
|
|
|
int iParm = 0; /* Next free entry in apParm */
|
2013-11-26 20:48:04 +04:00
|
|
|
char c;
|
2009-10-06 18:59:02 +04:00
|
|
|
int i;
|
2019-02-28 20:29:19 +03:00
|
|
|
int needResultReset = 0; /* Need to invoke Tcl_ResetResult() */
|
|
|
|
int rc = SQLITE_OK; /* Value to return */
|
2009-10-06 18:59:02 +04:00
|
|
|
Tcl_Interp *interp = pDb->interp;
|
|
|
|
|
|
|
|
*ppPreStmt = 0;
|
|
|
|
|
|
|
|
/* Trim spaces from the start of zSql and calculate the remaining length. */
|
2013-11-26 20:48:04 +04:00
|
|
|
while( (c = zSql[0])==' ' || c=='\t' || c=='\r' || c=='\n' ){ zSql++; }
|
2009-10-06 18:59:02 +04:00
|
|
|
nSql = strlen30(zSql);
|
|
|
|
|
|
|
|
for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){
|
|
|
|
int n = pPreStmt->nSql;
|
2016-07-15 00:26:09 +03:00
|
|
|
if( nSql>=n
|
2009-10-06 18:59:02 +04:00
|
|
|
&& 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;
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 00:26:09 +03:00
|
|
|
|
2009-10-06 18:59:02 +04:00
|
|
|
/* If no prepared statement was found. Compile the SQL text. Also allocate
|
|
|
|
** a new SqlPreparedStmt structure. */
|
|
|
|
if( pPreStmt==0 ){
|
|
|
|
int nByte;
|
|
|
|
|
2011-06-27 20:55:50 +04:00
|
|
|
if( SQLITE_OK!=dbPrepare(pDb, zSql, &pStmt, pzOut) ){
|
2012-10-03 15:02:33 +04:00
|
|
|
Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1));
|
2009-10-06 18:59:02 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
if( pStmt==0 ){
|
|
|
|
if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
|
|
|
|
/* A compile-time error in the statement. */
|
2012-10-03 15:02:33 +04:00
|
|
|
Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1));
|
2009-10-06 18:59:02 +04:00
|
|
|
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;
|
2012-04-19 21:19:51 +04:00
|
|
|
pPreStmt->nSql = (int)(*pzOut - zSql);
|
2009-10-06 18:59:02 +04:00
|
|
|
pPreStmt->zSql = sqlite3_sql(pStmt);
|
|
|
|
pPreStmt->apParm = (Tcl_Obj **)&pPreStmt[1];
|
2011-06-27 20:55:50 +04:00
|
|
|
#ifdef SQLITE_TEST
|
|
|
|
if( pPreStmt->zSql==0 ){
|
|
|
|
char *zCopy = Tcl_Alloc(pPreStmt->nSql + 1);
|
|
|
|
memcpy(zCopy, zSql, pPreStmt->nSql);
|
|
|
|
zCopy[pPreStmt->nSql] = '\0';
|
|
|
|
pPreStmt->zSql = zCopy;
|
|
|
|
}
|
|
|
|
#endif
|
2009-10-06 18:59:02 +04:00
|
|
|
}
|
|
|
|
assert( pPreStmt );
|
|
|
|
assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql );
|
|
|
|
assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) );
|
|
|
|
|
2016-07-15 00:26:09 +03:00
|
|
|
/* Bind values to parameters that begin with $ or : */
|
2009-10-06 18:59:02 +04:00
|
|
|
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);
|
2019-02-28 20:29:19 +03:00
|
|
|
if( pVar==0 && pDb->zBindFallback!=0 ){
|
|
|
|
Tcl_Obj *pCmd;
|
|
|
|
int rx;
|
|
|
|
pCmd = Tcl_NewStringObj(pDb->zBindFallback, -1);
|
|
|
|
Tcl_IncrRefCount(pCmd);
|
|
|
|
Tcl_ListObjAppendElement(interp, pCmd, Tcl_NewStringObj(zVar,-1));
|
|
|
|
if( needResultReset ) Tcl_ResetResult(interp);
|
|
|
|
needResultReset = 1;
|
|
|
|
rx = Tcl_EvalObjEx(interp, pCmd, TCL_EVAL_DIRECT);
|
|
|
|
Tcl_DecrRefCount(pCmd);
|
|
|
|
if( rx==TCL_OK ){
|
|
|
|
pVar = Tcl_GetObjResult(interp);
|
|
|
|
}else if( rx==TCL_ERROR ){
|
|
|
|
rc = TCL_ERROR;
|
|
|
|
break;
|
|
|
|
}else{
|
|
|
|
pVar = 0;
|
|
|
|
}
|
|
|
|
}
|
2009-10-06 18:59:02 +04:00
|
|
|
if( pVar ){
|
|
|
|
int n;
|
|
|
|
u8 *data;
|
|
|
|
const char *zType = (pVar->typePtr ? pVar->typePtr->name : "");
|
2015-04-20 00:43:16 +03:00
|
|
|
c = zType[0];
|
2009-10-06 18:59:02 +04:00
|
|
|
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);
|
|
|
|
}
|
2019-02-28 20:29:19 +03:00
|
|
|
if( needResultReset ) Tcl_ResetResult(pDb->interp);
|
2009-10-06 18:59:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pPreStmt->nParm = iParm;
|
|
|
|
*ppPreStmt = pPreStmt;
|
2019-02-28 20:29:19 +03:00
|
|
|
if( needResultReset && rc==TCL_OK ) Tcl_ResetResult(pDb->interp);
|
2009-10-15 22:35:38 +04:00
|
|
|
|
2019-02-28 20:29:19 +03:00
|
|
|
return rc;
|
2009-10-06 18:59:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Release a statement reference obtained by calling dbPrepareAndBind().
|
|
|
|
** There should be exactly one call to this function for each call to
|
|
|
|
** dbPrepareAndBind().
|
|
|
|
**
|
|
|
|
** 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 */
|
2011-06-27 20:55:50 +04:00
|
|
|
dbFreeStmt(pPreStmt);
|
2009-10-06 18:59:02 +04:00
|
|
|
}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++;
|
2016-07-15 00:26:09 +03:00
|
|
|
|
|
|
|
/* If we have too many statement in cache, remove the surplus from
|
2009-10-06 18:59:02 +04:00
|
|
|
** the end of the cache list. */
|
|
|
|
while( pDb->nStmt>pDb->maxStmt ){
|
2011-06-27 20:55:50 +04:00
|
|
|
SqlPreparedStmt *pLast = pDb->stmtLast;
|
|
|
|
pDb->stmtLast = pLast->pPrev;
|
2009-10-06 18:59:02 +04:00
|
|
|
pDb->stmtLast->pNext = 0;
|
|
|
|
pDb->nStmt--;
|
2011-06-27 20:55:50 +04:00
|
|
|
dbFreeStmt(pLast);
|
2009-10-06 18:59:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Structure used with dbEvalXXX() functions:
|
|
|
|
**
|
|
|
|
** 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 */
|
2017-06-27 00:08:32 +03:00
|
|
|
int evalFlags; /* Flags used */
|
2009-10-06 18:59:02 +04:00
|
|
|
Tcl_Obj *pArray; /* Name of array variable */
|
|
|
|
Tcl_Obj **apColName; /* Array of column names */
|
|
|
|
};
|
|
|
|
|
2017-06-27 00:08:32 +03:00
|
|
|
#define SQLITE_EVAL_WITHOUTNULLS 0x00001 /* Unset array(*) for NULL */
|
|
|
|
|
2009-10-06 18:59:02 +04:00
|
|
|
/*
|
|
|
|
** 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.
|
2007-11-13 13:30:24 +03:00
|
|
|
**
|
|
|
|
** 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
|
2009-10-06 18:59:02 +04:00
|
|
|
** the names of the columns returned by the statement as part of each
|
2016-07-15 00:26:09 +03:00
|
|
|
** 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
|
2009-10-06 18:59:02 +04:00
|
|
|
** tcl command:
|
2007-11-13 13:30:24 +03:00
|
|
|
**
|
|
|
|
** set ${pArray}(*) {a b c}
|
|
|
|
*/
|
2009-10-06 18:59:02 +04:00
|
|
|
static void dbEvalInit(
|
|
|
|
DbEvalContext *p, /* Pointer to structure to initialize */
|
|
|
|
SqliteDb *pDb, /* Database handle */
|
|
|
|
Tcl_Obj *pSql, /* Object containing SQL script */
|
2017-06-27 00:08:32 +03:00
|
|
|
Tcl_Obj *pArray, /* Name of Tcl array to set (*) element of */
|
|
|
|
int evalFlags /* Flags controlling evaluation */
|
2007-11-13 13:30:24 +03:00
|
|
|
){
|
2009-10-06 18:59:02 +04:00
|
|
|
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);
|
|
|
|
}
|
2017-06-27 00:08:32 +03:00
|
|
|
p->evalFlags = evalFlags;
|
2009-10-06 18:59:02 +04:00
|
|
|
}
|
2007-11-13 13:30:24 +03:00
|
|
|
|
2009-10-06 18:59:02 +04:00
|
|
|
/*
|
|
|
|
** 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 */
|
|
|
|
){
|
2007-11-13 13:30:24 +03:00
|
|
|
/* Compute column names */
|
2009-10-06 18:59:02 +04:00
|
|
|
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++){
|
2012-10-03 15:02:33 +04:00
|
|
|
apColName[i] = Tcl_NewStringObj(sqlite3_column_name(pStmt,i), -1);
|
2009-10-06 18:59:02 +04:00
|
|
|
Tcl_IncrRefCount(apColName[i]);
|
|
|
|
}
|
|
|
|
p->apColName = apColName;
|
2007-11-13 13:30:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If results are being stored in an array variable, then create
|
|
|
|
** the array(*) entry for that array
|
|
|
|
*/
|
2009-10-06 18:59:02 +04:00
|
|
|
if( p->pArray ){
|
|
|
|
Tcl_Interp *interp = p->pDb->interp;
|
2007-11-13 13:30:24 +03:00
|
|
|
Tcl_Obj *pColList = Tcl_NewObj();
|
|
|
|
Tcl_Obj *pStar = Tcl_NewStringObj("*", -1);
|
2009-10-06 18:59:02 +04:00
|
|
|
|
2007-11-13 13:30:24 +03:00
|
|
|
for(i=0; i<nCol; i++){
|
|
|
|
Tcl_ListObjAppendElement(interp, pColList, apColName[i]);
|
|
|
|
}
|
|
|
|
Tcl_IncrRefCount(pStar);
|
2009-10-06 18:59:02 +04:00
|
|
|
Tcl_ObjSetVar2(interp, p->pArray, pStar, pColList, 0);
|
2007-11-13 13:30:24 +03:00
|
|
|
Tcl_DecrRefCount(pStar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-06 18:59:02 +04:00
|
|
|
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){
|
2011-06-27 20:55:50 +04:00
|
|
|
const char *zPrevSql = 0; /* Previous value of p->zSql */
|
|
|
|
|
2009-10-06 18:59:02 +04:00
|
|
|
while( p->zSql[0] || p->pPreStmt ){
|
|
|
|
int rc;
|
|
|
|
if( p->pPreStmt==0 ){
|
2011-06-27 20:55:50 +04:00
|
|
|
zPrevSql = (p->zSql==zPrevSql ? 0 : p->zSql);
|
2009-10-06 18:59:02 +04:00
|
|
|
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);
|
2010-04-07 23:31:59 +04:00
|
|
|
pDb->nIndex = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_AUTOINDEX,1);
|
2017-06-22 19:51:16 +03:00
|
|
|
pDb->nVMStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_VM_STEP,1);
|
2009-10-06 18:59:02 +04:00
|
|
|
dbReleaseColumnNames(p);
|
|
|
|
p->pPreStmt = 0;
|
|
|
|
|
|
|
|
if( rcs!=SQLITE_OK ){
|
|
|
|
/* If a run-time error occurs, report the error and stop reading
|
|
|
|
** the SQL. */
|
|
|
|
dbReleaseStmt(pDb, pPreStmt, 1);
|
2011-06-27 20:55:50 +04:00
|
|
|
#if SQLITE_TEST
|
|
|
|
if( p->pDb->bLegacyPrepare && rcs==SQLITE_SCHEMA && zPrevSql ){
|
|
|
|
/* If the runtime error was an SQLITE_SCHEMA, and the database
|
2016-07-15 00:26:09 +03:00
|
|
|
** handle is configured to use the legacy sqlite3_prepare()
|
2011-06-27 20:55:50 +04:00
|
|
|
** interface, retry prepare()/step() on the same SQL statement.
|
|
|
|
** This only happens once. If there is a second SQLITE_SCHEMA
|
|
|
|
** error, the error will be returned to the caller. */
|
|
|
|
p->zSql = zPrevSql;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2012-10-03 15:02:33 +04:00
|
|
|
Tcl_SetObjResult(pDb->interp,
|
|
|
|
Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1));
|
2009-10-06 18:59:02 +04:00
|
|
|
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 ){
|
2011-06-20 23:00:30 +04:00
|
|
|
return Tcl_NewIntObj((int)v);
|
2009-10-06 18:59:02 +04:00
|
|
|
}else{
|
|
|
|
return Tcl_NewWideIntObj(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case SQLITE_FLOAT: {
|
|
|
|
return Tcl_NewDoubleObj(sqlite3_column_double(pStmt, iCol));
|
|
|
|
}
|
|
|
|
case SQLITE_NULL: {
|
2012-10-03 15:02:33 +04:00
|
|
|
return Tcl_NewStringObj(p->pDb->zNull, -1);
|
2009-10-06 18:59:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 16:56:18 +04:00
|
|
|
return Tcl_NewStringObj((char*)sqlite3_column_text(pStmt, iCol), -1);
|
2009-10-06 18:59:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** 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)
|
2009-10-13 22:38:34 +04:00
|
|
|
# define SQLITE_TCL_NRE 1
|
2009-10-06 18:59:02 +04:00
|
|
|
static int DbUseNre(void){
|
|
|
|
int major, minor;
|
|
|
|
Tcl_GetVersion(&major, &minor, 0, 0);
|
|
|
|
return( (major==8 && minor>=6) || major>8 );
|
|
|
|
}
|
|
|
|
#else
|
2016-07-15 00:26:09 +03:00
|
|
|
/*
|
2009-10-06 18:59:02 +04:00
|
|
|
** 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,
|
2016-07-15 00:26:09 +03:00
|
|
|
** even though the only invocations of them are within conditional blocks
|
2009-10-06 18:59:02 +04:00
|
|
|
** of the form:
|
|
|
|
**
|
|
|
|
** if( DbUseNre() ) { ... }
|
|
|
|
*/
|
2009-10-13 22:38:34 +04:00
|
|
|
# define SQLITE_TCL_NRE 0
|
2009-10-06 18:59:02 +04:00
|
|
|
# define DbUseNre() 0
|
2013-12-20 22:57:44 +04:00
|
|
|
# define Tcl_NRAddCallback(a,b,c,d,e,f) (void)0
|
2009-10-06 18:59:02 +04:00
|
|
|
# define Tcl_NREvalObj(a,b,c) 0
|
2013-12-20 22:57:44 +04:00
|
|
|
# define Tcl_NRCreateCommand(a,b,c,d,e,f) (void)0
|
2009-10-06 18:59:02 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This function is part of the implementation of the command:
|
|
|
|
**
|
|
|
|
** $db eval SQL ?ARRAYNAME? SCRIPT
|
|
|
|
*/
|
2016-07-28 21:06:52 +03:00
|
|
|
static int SQLITE_TCLAPI DbEvalNextCmd(
|
2009-10-06 18:59:02 +04:00
|
|
|
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++){
|
|
|
|
if( pArray==0 ){
|
2017-06-27 00:08:32 +03:00
|
|
|
Tcl_ObjSetVar2(interp, apColName[i], 0, dbEvalColumnValue(p,i), 0);
|
|
|
|
}else if( (p->evalFlags & SQLITE_EVAL_WITHOUTNULLS)!=0
|
|
|
|
&& sqlite3_column_type(p->pPreStmt->pStmt, i)==SQLITE_NULL
|
|
|
|
){
|
|
|
|
Tcl_UnsetVar2(interp, Tcl_GetString(pArray),
|
|
|
|
Tcl_GetString(apColName[i]), 0);
|
2009-10-06 18:59:02 +04:00
|
|
|
}else{
|
2017-06-27 00:08:32 +03:00
|
|
|
Tcl_ObjSetVar2(interp, pArray, apColName[i], dbEvalColumnValue(p,i), 0);
|
2009-10-06 18:59:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 00:26:09 +03:00
|
|
|
/* The required interpreter variables are now populated with the data
|
2009-10-06 18:59:02 +04:00
|
|
|
** 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
|
2016-07-15 00:26:09 +03:00
|
|
|
** exception). After scheduling the callbacks, return control to the
|
2009-10-06 18:59:02 +04:00
|
|
|
** 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;
|
2007-11-13 13:30:24 +03:00
|
|
|
}
|
|
|
|
|
2011-03-01 21:42:07 +03:00
|
|
|
/*
|
2016-07-15 00:26:09 +03:00
|
|
|
** This function is used by the implementations of the following database
|
2011-03-01 21:42:07 +03:00
|
|
|
** handle sub-commands:
|
|
|
|
**
|
|
|
|
** $db update_hook ?SCRIPT?
|
|
|
|
** $db wal_hook ?SCRIPT?
|
|
|
|
** $db commit_hook ?SCRIPT?
|
|
|
|
** $db preupdate hook ?SCRIPT?
|
|
|
|
*/
|
|
|
|
static void DbHookCmd(
|
|
|
|
Tcl_Interp *interp, /* Tcl interpreter */
|
|
|
|
SqliteDb *pDb, /* Database handle */
|
|
|
|
Tcl_Obj *pArg, /* SCRIPT argument (or NULL) */
|
|
|
|
Tcl_Obj **ppHook /* Pointer to member of SqliteDb */
|
|
|
|
){
|
|
|
|
sqlite3 *db = pDb->db;
|
|
|
|
|
|
|
|
if( *ppHook ){
|
|
|
|
Tcl_SetObjResult(interp, *ppHook);
|
|
|
|
if( pArg ){
|
|
|
|
Tcl_DecrRefCount(*ppHook);
|
|
|
|
*ppHook = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( pArg ){
|
|
|
|
assert( !(*ppHook) );
|
|
|
|
if( Tcl_GetCharLength(pArg)>0 ){
|
|
|
|
*ppHook = pArg;
|
|
|
|
Tcl_IncrRefCount(*ppHook);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-31 01:04:43 +04:00
|
|
|
#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
|
2011-03-01 21:42:07 +03:00
|
|
|
sqlite3_preupdate_hook(db, (pDb->pPreUpdateHook?DbPreUpdateHandler:0), pDb);
|
2011-03-31 01:04:43 +04:00
|
|
|
#endif
|
2011-03-01 21:42:07 +03:00
|
|
|
sqlite3_update_hook(db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb);
|
|
|
|
sqlite3_rollback_hook(db, (pDb->pRollbackHook?DbRollbackHandler:0), pDb);
|
|
|
|
sqlite3_wal_hook(db, (pDb->pWalHook?DbWalHandler:0), pDb);
|
|
|
|
}
|
|
|
|
|
2000-05-29 18:26:00 +04:00
|
|
|
/*
|
|
|
|
** The "sqlite" command below creates a new Tcl command for each
|
|
|
|
** connection it opens to an SQLite database. This routine is invoked
|
|
|
|
** whenever one of those connection-specific commands is executed
|
|
|
|
** in Tcl. For example, if you run Tcl code like this:
|
|
|
|
**
|
2004-09-06 21:24:11 +04:00
|
|
|
** sqlite3 db1 "my_database"
|
2000-05-29 18:26:00 +04:00
|
|
|
** db1 close
|
|
|
|
**
|
|
|
|
** The first command opens a connection to the "my_database" database
|
|
|
|
** and calls that connection "db1". The second command causes this
|
|
|
|
** subroutine to be invoked.
|
|
|
|
*/
|
2016-07-28 20:11:20 +03:00
|
|
|
static int SQLITE_TCLAPI DbObjCmd(
|
|
|
|
void *cd,
|
|
|
|
Tcl_Interp *interp,
|
|
|
|
int objc,
|
|
|
|
Tcl_Obj *const*objv
|
|
|
|
){
|
2000-08-04 17:49:02 +04:00
|
|
|
SqliteDb *pDb = (SqliteDb*)cd;
|
2000-09-21 17:01:35 +04:00
|
|
|
int choice;
|
2004-02-01 04:22:50 +03:00
|
|
|
int rc = TCL_OK;
|
2002-07-06 20:32:14 +04:00
|
|
|
static const char *DB_strs[] = {
|
2019-02-28 20:29:19 +03:00
|
|
|
"authorizer", "backup", "bind_fallback",
|
|
|
|
"busy", "cache", "changes",
|
|
|
|
"close", "collate", "collation_needed",
|
2019-08-16 00:27:20 +03:00
|
|
|
"commit_hook", "complete", "config",
|
|
|
|
"copy", "deserialize", "enable_load_extension",
|
|
|
|
"errorcode", "eval", "exists",
|
|
|
|
"function", "incrblob", "interrupt",
|
|
|
|
"last_insert_rowid", "nullvalue", "onecolumn",
|
|
|
|
"preupdate", "profile", "progress",
|
|
|
|
"rekey", "restore", "rollback_hook",
|
|
|
|
"serialize", "status", "timeout",
|
|
|
|
"total_changes", "trace", "trace_v2",
|
|
|
|
"transaction", "unlock_notify", "update_hook",
|
|
|
|
"version", "wal_hook", 0
|
2000-09-21 17:01:35 +04:00
|
|
|
};
|
2002-06-25 23:31:18 +04:00
|
|
|
enum DB_enum {
|
2019-02-28 20:29:19 +03:00
|
|
|
DB_AUTHORIZER, DB_BACKUP, DB_BIND_FALLBACK,
|
|
|
|
DB_BUSY, DB_CACHE, DB_CHANGES,
|
|
|
|
DB_CLOSE, DB_COLLATE, DB_COLLATION_NEEDED,
|
2019-08-16 00:27:20 +03:00
|
|
|
DB_COMMIT_HOOK, DB_COMPLETE, DB_CONFIG,
|
|
|
|
DB_COPY, DB_DESERIALIZE, DB_ENABLE_LOAD_EXTENSION,
|
|
|
|
DB_ERRORCODE, DB_EVAL, DB_EXISTS,
|
|
|
|
DB_FUNCTION, DB_INCRBLOB, DB_INTERRUPT,
|
|
|
|
DB_LAST_INSERT_ROWID, DB_NULLVALUE, DB_ONECOLUMN,
|
|
|
|
DB_PREUPDATE, DB_PROFILE, DB_PROGRESS,
|
|
|
|
DB_REKEY, DB_RESTORE, DB_ROLLBACK_HOOK,
|
|
|
|
DB_SERIALIZE, DB_STATUS, DB_TIMEOUT,
|
|
|
|
DB_TOTAL_CHANGES, DB_TRACE, DB_TRACE_V2,
|
|
|
|
DB_TRANSACTION, DB_UNLOCK_NOTIFY, DB_UPDATE_HOOK,
|
|
|
|
DB_VERSION, DB_WAL_HOOK
|
2000-09-21 17:01:35 +04:00
|
|
|
};
|
2004-12-17 18:41:11 +03:00
|
|
|
/* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */
|
2000-09-21 17:01:35 +04:00
|
|
|
|
|
|
|
if( objc<2 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2002-06-25 23:31:18 +04:00
|
|
|
if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
|
2000-05-29 18:26:00 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2000-09-21 17:01:35 +04:00
|
|
|
|
2002-06-25 23:31:18 +04:00
|
|
|
switch( (enum DB_enum)choice ){
|
2000-05-29 18:26:00 +04:00
|
|
|
|
2003-04-23 00:30:37 +04:00
|
|
|
/* $db authorizer ?CALLBACK?
|
|
|
|
**
|
|
|
|
** Invoke the given callback to authorize each SQL operation as it is
|
|
|
|
** compiled. 5 arguments are appended to the callback before it is
|
|
|
|
** invoked:
|
|
|
|
**
|
|
|
|
** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
|
|
|
|
** (2) First descriptive name (depends on authorization type)
|
|
|
|
** (3) Second descriptive name
|
|
|
|
** (4) Name of the database (ex: "main", "temp")
|
|
|
|
** (5) Name of trigger that is doing the access
|
|
|
|
**
|
|
|
|
** The callback should return on of the following strings: SQLITE_OK,
|
|
|
|
** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
|
|
|
|
**
|
|
|
|
** If this method is invoked with no arguments, the current authorization
|
|
|
|
** callback string is returned.
|
|
|
|
*/
|
|
|
|
case DB_AUTHORIZER: {
|
2004-07-26 16:24:22 +04:00
|
|
|
#ifdef SQLITE_OMIT_AUTHORIZATION
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, "authorization not available in this build",
|
|
|
|
(char*)0);
|
2004-07-26 16:24:22 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
#else
|
2003-04-23 00:30:37 +04:00
|
|
|
if( objc>3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
|
2004-06-29 16:39:08 +04:00
|
|
|
return TCL_ERROR;
|
2003-04-23 00:30:37 +04:00
|
|
|
}else if( objc==2 ){
|
2003-04-23 16:25:23 +04:00
|
|
|
if( pDb->zAuth ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, pDb->zAuth, (char*)0);
|
2003-04-23 00:30:37 +04:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
char *zAuth;
|
|
|
|
int len;
|
|
|
|
if( pDb->zAuth ){
|
|
|
|
Tcl_Free(pDb->zAuth);
|
|
|
|
}
|
|
|
|
zAuth = Tcl_GetStringFromObj(objv[2], &len);
|
|
|
|
if( zAuth && len>0 ){
|
|
|
|
pDb->zAuth = Tcl_Alloc( len + 1 );
|
2007-05-04 17:15:55 +04:00
|
|
|
memcpy(pDb->zAuth, zAuth, len+1);
|
2003-04-23 00:30:37 +04:00
|
|
|
}else{
|
|
|
|
pDb->zAuth = 0;
|
|
|
|
}
|
|
|
|
if( pDb->zAuth ){
|
2014-09-11 17:44:52 +04:00
|
|
|
typedef int (*sqlite3_auth_cb)(
|
|
|
|
void*,int,const char*,const char*,
|
|
|
|
const char*,const char*);
|
2003-04-23 00:30:37 +04:00
|
|
|
pDb->interp = interp;
|
2014-09-11 17:44:52 +04:00
|
|
|
sqlite3_set_authorizer(pDb->db,(sqlite3_auth_cb)auth_callback,pDb);
|
2003-04-23 00:30:37 +04:00
|
|
|
}else{
|
2004-05-10 14:34:51 +04:00
|
|
|
sqlite3_set_authorizer(pDb->db, 0, 0);
|
2003-04-23 00:30:37 +04:00
|
|
|
}
|
|
|
|
}
|
2004-07-26 16:24:22 +04:00
|
|
|
#endif
|
2003-04-23 00:30:37 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-02-05 01:46:47 +03:00
|
|
|
/* $db backup ?DATABASE? FILENAME
|
|
|
|
**
|
|
|
|
** Open or create a database file named FILENAME. Transfer the
|
|
|
|
** content of local database DATABASE (default: "main") into the
|
|
|
|
** FILENAME database.
|
|
|
|
*/
|
|
|
|
case DB_BACKUP: {
|
|
|
|
const char *zDestFile;
|
|
|
|
const char *zSrcDb;
|
|
|
|
sqlite3 *pDest;
|
|
|
|
sqlite3_backup *pBackup;
|
|
|
|
|
|
|
|
if( objc==3 ){
|
|
|
|
zSrcDb = "main";
|
|
|
|
zDestFile = Tcl_GetString(objv[2]);
|
|
|
|
}else if( objc==4 ){
|
|
|
|
zSrcDb = Tcl_GetString(objv[2]);
|
|
|
|
zDestFile = Tcl_GetString(objv[3]);
|
|
|
|
}else{
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2016-01-23 02:17:51 +03:00
|
|
|
rc = sqlite3_open_v2(zDestFile, &pDest,
|
|
|
|
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE| pDb->openFlags, 0);
|
2009-02-05 01:46:47 +03:00
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
Tcl_AppendResult(interp, "cannot open target database: ",
|
|
|
|
sqlite3_errmsg(pDest), (char*)0);
|
|
|
|
sqlite3_close(pDest);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
pBackup = sqlite3_backup_init(pDest, "main", pDb->db, zSrcDb);
|
|
|
|
if( pBackup==0 ){
|
|
|
|
Tcl_AppendResult(interp, "backup failed: ",
|
|
|
|
sqlite3_errmsg(pDest), (char*)0);
|
|
|
|
sqlite3_close(pDest);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
|
|
|
|
sqlite3_backup_finish(pBackup);
|
|
|
|
if( rc==SQLITE_DONE ){
|
|
|
|
rc = TCL_OK;
|
|
|
|
}else{
|
|
|
|
Tcl_AppendResult(interp, "backup failed: ",
|
|
|
|
sqlite3_errmsg(pDest), (char*)0);
|
|
|
|
rc = TCL_ERROR;
|
|
|
|
}
|
|
|
|
sqlite3_close(pDest);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-02-28 20:29:19 +03:00
|
|
|
/* $db bind_fallback ?CALLBACK?
|
|
|
|
**
|
|
|
|
** When resolving bind parameters in an SQL statement, if the parameter
|
|
|
|
** cannot be associated with a TCL variable then invoke CALLBACK with a
|
|
|
|
** single argument that is the name of the parameter and use the return
|
|
|
|
** value of the CALLBACK as the binding. If CALLBACK returns something
|
|
|
|
** other than TCL_OK or TCL_ERROR then bind a NULL.
|
|
|
|
**
|
|
|
|
** If CALLBACK is an empty string, then revert to the default behavior
|
|
|
|
** which is to set the binding to NULL.
|
|
|
|
**
|
|
|
|
** If CALLBACK returns an error, that causes the statement execution to
|
|
|
|
** abort. Hence, to configure a connection so that it throws an error
|
|
|
|
** on an attempt to bind an unknown variable, do something like this:
|
|
|
|
**
|
|
|
|
** proc bind_error {name} {error "no such variable: $name"}
|
|
|
|
** db bind_fallback bind_error
|
|
|
|
*/
|
|
|
|
case DB_BIND_FALLBACK: {
|
|
|
|
if( objc>3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}else if( objc==2 ){
|
|
|
|
if( pDb->zBindFallback ){
|
|
|
|
Tcl_AppendResult(interp, pDb->zBindFallback, (char*)0);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
char *zCallback;
|
|
|
|
int len;
|
|
|
|
if( pDb->zBindFallback ){
|
|
|
|
Tcl_Free(pDb->zBindFallback);
|
|
|
|
}
|
|
|
|
zCallback = Tcl_GetStringFromObj(objv[2], &len);
|
|
|
|
if( zCallback && len>0 ){
|
|
|
|
pDb->zBindFallback = Tcl_Alloc( len + 1 );
|
|
|
|
memcpy(pDb->zBindFallback, zCallback, len+1);
|
|
|
|
}else{
|
|
|
|
pDb->zBindFallback = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-08-04 17:49:02 +04:00
|
|
|
/* $db busy ?CALLBACK?
|
|
|
|
**
|
|
|
|
** Invoke the given callback if an SQL statement attempts to open
|
|
|
|
** a locked database file.
|
|
|
|
*/
|
2000-09-21 17:01:35 +04:00
|
|
|
case DB_BUSY: {
|
|
|
|
if( objc>3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
|
2000-08-04 17:49:02 +04:00
|
|
|
return TCL_ERROR;
|
2000-09-21 17:01:35 +04:00
|
|
|
}else if( objc==2 ){
|
2000-08-04 17:49:02 +04:00
|
|
|
if( pDb->zBusy ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, pDb->zBusy, (char*)0);
|
2000-08-04 17:49:02 +04:00
|
|
|
}
|
|
|
|
}else{
|
2000-09-21 17:01:35 +04:00
|
|
|
char *zBusy;
|
|
|
|
int len;
|
2000-08-04 17:49:02 +04:00
|
|
|
if( pDb->zBusy ){
|
|
|
|
Tcl_Free(pDb->zBusy);
|
|
|
|
}
|
2000-09-21 17:01:35 +04:00
|
|
|
zBusy = Tcl_GetStringFromObj(objv[2], &len);
|
|
|
|
if( zBusy && len>0 ){
|
|
|
|
pDb->zBusy = Tcl_Alloc( len + 1 );
|
2007-05-04 17:15:55 +04:00
|
|
|
memcpy(pDb->zBusy, zBusy, len+1);
|
2000-09-21 17:01:35 +04:00
|
|
|
}else{
|
|
|
|
pDb->zBusy = 0;
|
2000-08-04 17:49:02 +04:00
|
|
|
}
|
|
|
|
if( pDb->zBusy ){
|
|
|
|
pDb->interp = interp;
|
2004-05-10 14:34:51 +04:00
|
|
|
sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
|
2000-09-21 17:01:35 +04:00
|
|
|
}else{
|
2004-05-10 14:34:51 +04:00
|
|
|
sqlite3_busy_handler(pDb->db, 0, 0);
|
2000-08-04 17:49:02 +04:00
|
|
|
}
|
|
|
|
}
|
2000-09-21 17:01:35 +04:00
|
|
|
break;
|
|
|
|
}
|
2000-08-04 17:49:02 +04:00
|
|
|
|
2005-01-24 03:28:42 +03:00
|
|
|
/* $db cache flush
|
|
|
|
** $db cache size n
|
|
|
|
**
|
|
|
|
** Flush the prepared statement cache, or set the maximum number of
|
|
|
|
** cached statements.
|
|
|
|
*/
|
|
|
|
case DB_CACHE: {
|
|
|
|
char *subCmd;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if( objc<=2 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
subCmd = Tcl_GetStringFromObj( objv[2], 0 );
|
|
|
|
if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){
|
|
|
|
if( objc!=3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "flush");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}else{
|
|
|
|
flushStmtCache( pDb );
|
|
|
|
}
|
|
|
|
}else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){
|
|
|
|
if( objc!=4 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "size n");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}else{
|
|
|
|
if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){
|
2016-07-15 00:26:09 +03:00
|
|
|
Tcl_AppendResult( interp, "cannot convert \"",
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_GetStringFromObj(objv[3],0), "\" to integer", (char*)0);
|
2005-01-24 03:28:42 +03:00
|
|
|
return TCL_ERROR;
|
|
|
|
}else{
|
|
|
|
if( n<0 ){
|
|
|
|
flushStmtCache( pDb );
|
|
|
|
n = 0;
|
|
|
|
}else if( n>MAX_PREPARED_STMTS ){
|
|
|
|
n = MAX_PREPARED_STMTS;
|
|
|
|
}
|
|
|
|
pDb->maxStmt = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
2016-07-15 00:26:09 +03:00
|
|
|
Tcl_AppendResult( interp, "bad option \"",
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size",
|
|
|
|
(char*)0);
|
2005-01-24 03:28:42 +03:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
/* $db changes
|
2002-04-12 14:08:59 +04:00
|
|
|
**
|
|
|
|
** Return the number of rows that were modified, inserted, or deleted by
|
2016-07-15 00:26:09 +03:00
|
|
|
** the most recent INSERT, UPDATE or DELETE statement, not including
|
2004-06-21 10:50:26 +04:00
|
|
|
** any changes made by trigger programs.
|
2002-04-12 14:08:59 +04:00
|
|
|
*/
|
|
|
|
case DB_CHANGES: {
|
|
|
|
Tcl_Obj *pResult;
|
2004-02-26 01:51:06 +03:00
|
|
|
if( objc!=2 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
pResult = Tcl_GetObjResult(interp);
|
2021-06-22 21:32:05 +03:00
|
|
|
Tcl_SetWideIntObj(pResult, sqlite3_changes64(pDb->db));
|
2004-02-26 01:51:06 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-05-29 18:26:00 +04:00
|
|
|
/* $db close
|
|
|
|
**
|
|
|
|
** Shutdown the database
|
|
|
|
*/
|
2000-09-21 17:01:35 +04:00
|
|
|
case DB_CLOSE: {
|
|
|
|
Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
|
|
|
|
break;
|
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
|
2004-06-29 16:39:08 +04:00
|
|
|
/*
|
|
|
|
** $db collate NAME SCRIPT
|
|
|
|
**
|
|
|
|
** Create a new SQL collation function called NAME. Whenever
|
|
|
|
** that function is called, invoke SCRIPT to evaluate the function.
|
|
|
|
*/
|
|
|
|
case DB_COLLATE: {
|
|
|
|
SqlCollate *pCollate;
|
|
|
|
char *zName;
|
|
|
|
char *zScript;
|
|
|
|
int nScript;
|
|
|
|
if( objc!=4 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
zName = Tcl_GetStringFromObj(objv[2], 0);
|
|
|
|
zScript = Tcl_GetStringFromObj(objv[3], &nScript);
|
|
|
|
pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
|
|
|
|
if( pCollate==0 ) return TCL_ERROR;
|
|
|
|
pCollate->interp = interp;
|
|
|
|
pCollate->pNext = pDb->pCollate;
|
|
|
|
pCollate->zScript = (char*)&pCollate[1];
|
|
|
|
pDb->pCollate = pCollate;
|
2007-05-04 17:15:55 +04:00
|
|
|
memcpy(pCollate->zScript, zScript, nScript+1);
|
2016-07-15 00:26:09 +03:00
|
|
|
if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
|
2004-06-29 16:39:08 +04:00
|
|
|
pCollate, tclSqlCollate) ){
|
2005-01-25 07:27:54 +03:00
|
|
|
Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
|
2004-06-29 16:39:08 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** $db collation_needed SCRIPT
|
|
|
|
**
|
|
|
|
** Create a new SQL collation function called NAME. Whenever
|
|
|
|
** that function is called, invoke SCRIPT to evaluate the function.
|
|
|
|
*/
|
|
|
|
case DB_COLLATION_NEEDED: {
|
|
|
|
if( objc!=3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
if( pDb->pCollateNeeded ){
|
|
|
|
Tcl_DecrRefCount(pDb->pCollateNeeded);
|
|
|
|
}
|
|
|
|
pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]);
|
|
|
|
Tcl_IncrRefCount(pDb->pCollateNeeded);
|
|
|
|
sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-08-30 03:00:03 +04:00
|
|
|
/* $db commit_hook ?CALLBACK?
|
|
|
|
**
|
|
|
|
** Invoke the given callback just before committing every SQL transaction.
|
|
|
|
** If the callback throws an exception or returns non-zero, then the
|
|
|
|
** transaction is aborted. If CALLBACK is an empty string, the callback
|
|
|
|
** is disabled.
|
|
|
|
*/
|
|
|
|
case DB_COMMIT_HOOK: {
|
|
|
|
if( objc>3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}else if( objc==2 ){
|
|
|
|
if( pDb->zCommit ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, pDb->zCommit, (char*)0);
|
2005-08-30 03:00:03 +04:00
|
|
|
}
|
|
|
|
}else{
|
2014-01-24 21:03:55 +04:00
|
|
|
const char *zCommit;
|
2005-08-30 03:00:03 +04:00
|
|
|
int len;
|
|
|
|
if( pDb->zCommit ){
|
|
|
|
Tcl_Free(pDb->zCommit);
|
|
|
|
}
|
|
|
|
zCommit = Tcl_GetStringFromObj(objv[2], &len);
|
|
|
|
if( zCommit && len>0 ){
|
|
|
|
pDb->zCommit = Tcl_Alloc( len + 1 );
|
2007-05-04 17:15:55 +04:00
|
|
|
memcpy(pDb->zCommit, zCommit, len+1);
|
2005-08-30 03:00:03 +04:00
|
|
|
}else{
|
|
|
|
pDb->zCommit = 0;
|
|
|
|
}
|
|
|
|
if( pDb->zCommit ){
|
|
|
|
pDb->interp = interp;
|
|
|
|
sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
|
|
|
|
}else{
|
|
|
|
sqlite3_commit_hook(pDb->db, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-05-29 18:26:00 +04:00
|
|
|
/* $db complete SQL
|
|
|
|
**
|
|
|
|
** Return TRUE if SQL is a complete SQL statement. Return FALSE if
|
|
|
|
** additional lines of input are needed. This is similar to the
|
|
|
|
** built-in "info complete" command of Tcl.
|
|
|
|
*/
|
2000-09-21 17:01:35 +04:00
|
|
|
case DB_COMPLETE: {
|
2005-02-26 20:31:26 +03:00
|
|
|
#ifndef SQLITE_OMIT_COMPLETE
|
2000-09-21 17:01:35 +04:00
|
|
|
Tcl_Obj *pResult;
|
|
|
|
int isComplete;
|
|
|
|
if( objc!=3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "SQL");
|
2000-05-29 18:26:00 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2004-05-10 14:34:51 +04:00
|
|
|
isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
|
2000-09-21 17:01:35 +04:00
|
|
|
pResult = Tcl_GetObjResult(interp);
|
|
|
|
Tcl_SetBooleanObj(pResult, isComplete);
|
2005-02-26 20:31:26 +03:00
|
|
|
#endif
|
2000-09-21 17:01:35 +04:00
|
|
|
break;
|
|
|
|
}
|
2003-01-31 20:21:49 +03:00
|
|
|
|
2019-08-16 00:27:20 +03:00
|
|
|
/* $db config ?OPTION? ?BOOLEAN?
|
|
|
|
**
|
|
|
|
** Configure the database connection using the sqlite3_db_config()
|
|
|
|
** interface.
|
|
|
|
*/
|
|
|
|
case DB_CONFIG: {
|
|
|
|
static const struct DbConfigChoices {
|
|
|
|
const char *zName;
|
|
|
|
int op;
|
|
|
|
} aDbConfig[] = {
|
2020-01-19 01:20:14 +03:00
|
|
|
{ "defensive", SQLITE_DBCONFIG_DEFENSIVE },
|
|
|
|
{ "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL },
|
|
|
|
{ "dqs_dml", SQLITE_DBCONFIG_DQS_DML },
|
2019-08-16 00:27:20 +03:00
|
|
|
{ "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
|
2020-01-19 01:20:14 +03:00
|
|
|
{ "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
|
2019-08-16 00:27:20 +03:00
|
|
|
{ "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
|
|
|
|
{ "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW },
|
|
|
|
{ "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
|
2020-01-19 01:20:14 +03:00
|
|
|
{ "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE },
|
|
|
|
{ "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT },
|
2019-08-16 00:27:20 +03:00
|
|
|
{ "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
|
|
|
|
{ "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
|
|
|
|
{ "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
|
2020-01-19 01:20:14 +03:00
|
|
|
{ "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
|
|
|
|
{ "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA },
|
2019-08-16 00:27:20 +03:00
|
|
|
{ "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
|
|
|
|
};
|
|
|
|
Tcl_Obj *pResult;
|
|
|
|
int ii;
|
|
|
|
if( objc>4 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "?OPTION? ?BOOLEAN?");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
if( objc==2 ){
|
|
|
|
/* With no arguments, list all configuration options and with the
|
|
|
|
** current value */
|
|
|
|
pResult = Tcl_NewListObj(0,0);
|
|
|
|
for(ii=0; ii<sizeof(aDbConfig)/sizeof(aDbConfig[0]); ii++){
|
|
|
|
int v = 0;
|
2019-08-16 02:11:42 +03:00
|
|
|
sqlite3_db_config(pDb->db, aDbConfig[ii].op, -1, &v);
|
2019-08-16 00:27:20 +03:00
|
|
|
Tcl_ListObjAppendElement(interp, pResult,
|
|
|
|
Tcl_NewStringObj(aDbConfig[ii].zName,-1));
|
|
|
|
Tcl_ListObjAppendElement(interp, pResult,
|
|
|
|
Tcl_NewIntObj(v));
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
const char *zOpt = Tcl_GetString(objv[2]);
|
|
|
|
int onoff = -1;
|
|
|
|
int v = 0;
|
|
|
|
if( zOpt[0]=='-' ) zOpt++;
|
|
|
|
for(ii=0; ii<sizeof(aDbConfig)/sizeof(aDbConfig[0]); ii++){
|
|
|
|
if( strcmp(aDbConfig[ii].zName, zOpt)==0 ) break;
|
|
|
|
}
|
|
|
|
if( ii>=sizeof(aDbConfig)/sizeof(aDbConfig[0]) ){
|
|
|
|
Tcl_AppendResult(interp, "unknown config option: \"", zOpt,
|
|
|
|
"\"", (void*)0);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
if( objc==4 ){
|
|
|
|
if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ){
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2019-08-16 02:11:42 +03:00
|
|
|
sqlite3_db_config(pDb->db, aDbConfig[ii].op, onoff, &v);
|
2019-08-16 00:27:20 +03:00
|
|
|
pResult = Tcl_NewIntObj(v);
|
|
|
|
}
|
|
|
|
Tcl_SetObjResult(interp, pResult);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-08-30 03:00:03 +04:00
|
|
|
/* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR?
|
2003-01-31 20:21:49 +03:00
|
|
|
**
|
2005-08-30 03:00:03 +04:00
|
|
|
** Copy data into table from filename, optionally using SEPARATOR
|
|
|
|
** as column separators. If a column contains a null string, or the
|
|
|
|
** value of NULLINDICATOR, a NULL is inserted for the column.
|
|
|
|
** conflict-algorithm is one of the sqlite conflict algorithms:
|
|
|
|
** rollback, abort, fail, ignore, replace
|
|
|
|
** On success, return the number of lines processed, not necessarily same
|
|
|
|
** as 'db changes' due to conflict-algorithm selected.
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
2005-08-30 03:00:03 +04:00
|
|
|
** This code is basically an implementation/enhancement of
|
|
|
|
** the sqlite3 shell.c ".import" command.
|
2004-09-07 17:20:35 +04:00
|
|
|
**
|
2005-08-30 03:00:03 +04:00
|
|
|
** This command usage is equivalent to the sqlite2.x COPY statement,
|
|
|
|
** which imports file data into a table using the PostgreSQL COPY file format:
|
|
|
|
** $db copy $conflit_algo $table_name $filename \t \\N
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2005-08-30 03:00:03 +04:00
|
|
|
case DB_COPY: {
|
|
|
|
char *zTable; /* Insert data into this table */
|
|
|
|
char *zFile; /* The file from which to extract data */
|
|
|
|
char *zConflict; /* The conflict algorithm to use */
|
|
|
|
sqlite3_stmt *pStmt; /* A statement */
|
|
|
|
int nCol; /* Number of columns in the table */
|
|
|
|
int nByte; /* Number of bytes in an SQL string */
|
|
|
|
int i, j; /* Loop counters */
|
|
|
|
int nSep; /* Number of bytes in zSep[] */
|
|
|
|
int nNull; /* Number of bytes in zNull[] */
|
|
|
|
char *zSql; /* An SQL statement */
|
|
|
|
char *zLine; /* A single line of input from the file */
|
|
|
|
char **azCol; /* zLine[] broken up into columns */
|
2014-01-24 21:03:55 +04:00
|
|
|
const char *zCommit; /* How to commit changes */
|
2005-08-30 03:00:03 +04:00
|
|
|
FILE *in; /* The input file */
|
|
|
|
int lineno = 0; /* Line number of input file */
|
|
|
|
char zLineNum[80]; /* Line number print buffer */
|
|
|
|
Tcl_Obj *pResult; /* interp result */
|
2004-05-29 06:37:19 +04:00
|
|
|
|
2014-01-24 21:03:55 +04:00
|
|
|
const char *zSep;
|
|
|
|
const char *zNull;
|
2005-08-30 03:00:03 +04:00
|
|
|
if( objc<5 || objc>7 ){
|
2016-07-15 00:26:09 +03:00
|
|
|
Tcl_WrongNumArgs(interp, 2, objv,
|
2005-08-30 03:00:03 +04:00
|
|
|
"CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
if( objc>=6 ){
|
|
|
|
zSep = Tcl_GetStringFromObj(objv[5], 0);
|
2004-09-07 17:20:35 +04:00
|
|
|
}else{
|
2005-08-30 03:00:03 +04:00
|
|
|
zSep = "\t";
|
2004-05-27 16:11:31 +04:00
|
|
|
}
|
2005-08-30 03:00:03 +04:00
|
|
|
if( objc>=7 ){
|
|
|
|
zNull = Tcl_GetStringFromObj(objv[6], 0);
|
2004-08-20 22:34:20 +04:00
|
|
|
}else{
|
2005-08-30 03:00:03 +04:00
|
|
|
zNull = "";
|
2004-08-20 22:34:20 +04:00
|
|
|
}
|
2005-08-30 03:00:03 +04:00
|
|
|
zConflict = Tcl_GetStringFromObj(objv[2], 0);
|
|
|
|
zTable = Tcl_GetStringFromObj(objv[3], 0);
|
|
|
|
zFile = Tcl_GetStringFromObj(objv[4], 0);
|
2008-12-11 01:15:00 +03:00
|
|
|
nSep = strlen30(zSep);
|
|
|
|
nNull = strlen30(zNull);
|
2005-08-30 03:00:03 +04:00
|
|
|
if( nSep==0 ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp,"Error: non-null separator required for copy",
|
|
|
|
(char*)0);
|
2005-08-30 03:00:03 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2008-09-23 14:12:13 +04:00
|
|
|
if(strcmp(zConflict, "rollback") != 0 &&
|
|
|
|
strcmp(zConflict, "abort" ) != 0 &&
|
|
|
|
strcmp(zConflict, "fail" ) != 0 &&
|
|
|
|
strcmp(zConflict, "ignore" ) != 0 &&
|
|
|
|
strcmp(zConflict, "replace" ) != 0 ) {
|
2016-07-15 00:26:09 +03:00
|
|
|
Tcl_AppendResult(interp, "Error: \"", zConflict,
|
2005-08-30 03:00:03 +04:00
|
|
|
"\", conflict-algorithm must be one of: rollback, "
|
2014-02-07 23:26:13 +04:00
|
|
|
"abort, fail, ignore, or replace", (char*)0);
|
2005-08-30 03:00:03 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
|
|
|
|
if( zSql==0 ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, "Error: no such table: ", zTable, (char*)0);
|
2005-08-30 03:00:03 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2008-12-11 01:15:00 +03:00
|
|
|
nByte = strlen30(zSql);
|
2007-02-01 04:53:44 +03:00
|
|
|
rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0);
|
2005-08-30 03:00:03 +04:00
|
|
|
sqlite3_free(zSql);
|
|
|
|
if( rc ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0);
|
2005-08-30 03:00:03 +04:00
|
|
|
nCol = 0;
|
|
|
|
}else{
|
|
|
|
nCol = sqlite3_column_count(pStmt);
|
|
|
|
}
|
|
|
|
sqlite3_finalize(pStmt);
|
|
|
|
if( nCol==0 ) {
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
zSql = malloc( nByte + 50 + nCol*2 );
|
|
|
|
if( zSql==0 ) {
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0);
|
2005-08-30 03:00:03 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?",
|
|
|
|
zConflict, zTable);
|
2008-12-11 01:15:00 +03:00
|
|
|
j = strlen30(zSql);
|
2005-08-30 03:00:03 +04:00
|
|
|
for(i=1; i<nCol; i++){
|
|
|
|
zSql[j++] = ',';
|
|
|
|
zSql[j++] = '?';
|
|
|
|
}
|
|
|
|
zSql[j++] = ')';
|
|
|
|
zSql[j] = 0;
|
2007-02-01 04:53:44 +03:00
|
|
|
rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0);
|
2005-08-30 03:00:03 +04:00
|
|
|
free(zSql);
|
|
|
|
if( rc ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0);
|
2005-08-30 03:00:03 +04:00
|
|
|
sqlite3_finalize(pStmt);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
in = fopen(zFile, "rb");
|
|
|
|
if( in==0 ){
|
2017-01-12 14:50:08 +03:00
|
|
|
Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, (char*)0);
|
2005-08-30 03:00:03 +04:00
|
|
|
sqlite3_finalize(pStmt);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
azCol = malloc( sizeof(azCol[0])*(nCol+1) );
|
|
|
|
if( azCol==0 ) {
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0);
|
2006-03-06 23:55:46 +03:00
|
|
|
fclose(in);
|
2005-08-30 03:00:03 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2006-03-16 19:19:56 +03:00
|
|
|
(void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0);
|
2005-08-30 03:00:03 +04:00
|
|
|
zCommit = "COMMIT";
|
|
|
|
while( (zLine = local_getline(0, in))!=0 ){
|
|
|
|
char *z;
|
|
|
|
lineno++;
|
|
|
|
azCol[0] = zLine;
|
|
|
|
for(i=0, z=zLine; *z; z++){
|
|
|
|
if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){
|
|
|
|
*z = 0;
|
|
|
|
i++;
|
|
|
|
if( i<nCol ){
|
|
|
|
azCol[i] = &z[nSep];
|
|
|
|
z += nSep-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( i+1!=nCol ){
|
|
|
|
char *zErr;
|
2008-12-11 01:15:00 +03:00
|
|
|
int nErr = strlen30(zFile) + 200;
|
2007-05-04 17:15:55 +04:00
|
|
|
zErr = malloc(nErr);
|
2006-05-10 18:39:13 +04:00
|
|
|
if( zErr ){
|
2007-05-04 17:15:55 +04:00
|
|
|
sqlite3_snprintf(nErr, zErr,
|
2006-05-10 18:39:13 +04:00
|
|
|
"Error: %s line %d: expected %d columns of data but found %d",
|
|
|
|
zFile, lineno, nCol, i+1);
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, zErr, (char*)0);
|
2006-05-10 18:39:13 +04:00
|
|
|
free(zErr);
|
|
|
|
}
|
2005-08-30 03:00:03 +04:00
|
|
|
zCommit = "ROLLBACK";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for(i=0; i<nCol; i++){
|
|
|
|
/* check for null data, if so, bind as null */
|
2008-12-10 22:26:22 +03:00
|
|
|
if( (nNull>0 && strcmp(azCol[i], zNull)==0)
|
2016-07-15 00:26:09 +03:00
|
|
|
|| strlen30(azCol[i])==0
|
2008-12-10 22:26:22 +03:00
|
|
|
){
|
2005-08-30 03:00:03 +04:00
|
|
|
sqlite3_bind_null(pStmt, i+1);
|
|
|
|
}else{
|
|
|
|
sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sqlite3_step(pStmt);
|
|
|
|
rc = sqlite3_reset(pStmt);
|
|
|
|
free(zLine);
|
|
|
|
if( rc!=SQLITE_OK ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), (char*)0);
|
2005-08-30 03:00:03 +04:00
|
|
|
zCommit = "ROLLBACK";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(azCol);
|
|
|
|
fclose(in);
|
|
|
|
sqlite3_finalize(pStmt);
|
2006-03-16 19:19:56 +03:00
|
|
|
(void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0);
|
2005-08-30 03:00:03 +04:00
|
|
|
|
|
|
|
if( zCommit[0] == 'C' ){
|
|
|
|
/* success, set result as number of lines processed */
|
|
|
|
pResult = Tcl_GetObjResult(interp);
|
|
|
|
Tcl_SetIntObj(pResult, lineno);
|
|
|
|
rc = TCL_OK;
|
|
|
|
}else{
|
|
|
|
/* failure, append lineno where failed */
|
2007-05-04 17:15:55 +04:00
|
|
|
sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno);
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,
|
|
|
|
(char*)0);
|
2005-08-30 03:00:03 +04:00
|
|
|
rc = TCL_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:49:52 +03:00
|
|
|
/*
|
2019-01-22 19:06:20 +03:00
|
|
|
** $db deserialize ?-maxsize N? ?-readonly BOOL? ?DATABASE? VALUE
|
2018-01-03 19:49:52 +03:00
|
|
|
**
|
|
|
|
** Reopen DATABASE (default "main") using the content in $VALUE
|
|
|
|
*/
|
|
|
|
case DB_DESERIALIZE: {
|
2021-05-08 20:18:23 +03:00
|
|
|
#ifdef SQLITE_OMIT_DESERIALIZE
|
2018-01-03 22:03:31 +03:00
|
|
|
Tcl_AppendResult(interp, "MEMDB not available in this build",
|
|
|
|
(char*)0);
|
|
|
|
rc = TCL_ERROR;
|
|
|
|
#else
|
2019-01-22 19:06:20 +03:00
|
|
|
const char *zSchema = 0;
|
|
|
|
Tcl_Obj *pValue = 0;
|
2018-01-03 22:03:31 +03:00
|
|
|
unsigned char *pBA;
|
|
|
|
unsigned char *pData;
|
|
|
|
int len, xrc;
|
2019-01-22 19:06:20 +03:00
|
|
|
sqlite3_int64 mxSize = 0;
|
|
|
|
int i;
|
|
|
|
int isReadonly = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if( objc<3 ){
|
2018-01-03 22:03:31 +03:00
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? VALUE");
|
|
|
|
rc = TCL_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2019-01-22 19:06:20 +03:00
|
|
|
for(i=2; i<objc-1; i++){
|
|
|
|
const char *z = Tcl_GetString(objv[i]);
|
|
|
|
if( strcmp(z,"-maxsize")==0 && i<objc-2 ){
|
|
|
|
rc = Tcl_GetWideIntFromObj(interp, objv[++i], &mxSize);
|
|
|
|
if( rc ) goto deserialize_error;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if( strcmp(z,"-readonly")==0 && i<objc-2 ){
|
|
|
|
rc = Tcl_GetBooleanFromObj(interp, objv[++i], &isReadonly);
|
|
|
|
if( rc ) goto deserialize_error;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if( zSchema==0 && i==objc-2 && z[0]!='-' ){
|
|
|
|
zSchema = z;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Tcl_AppendResult(interp, "unknown option: ", z, (char*)0);
|
|
|
|
rc = TCL_ERROR;
|
|
|
|
goto deserialize_error;
|
|
|
|
}
|
|
|
|
pValue = objv[objc-1];
|
2018-01-03 22:03:31 +03:00
|
|
|
pBA = Tcl_GetByteArrayFromObj(pValue, &len);
|
|
|
|
pData = sqlite3_malloc64( len );
|
2018-01-04 02:40:02 +03:00
|
|
|
if( pData==0 && len>0 ){
|
2018-01-03 22:03:31 +03:00
|
|
|
Tcl_AppendResult(interp, "out of memory", (char*)0);
|
|
|
|
rc = TCL_ERROR;
|
|
|
|
}else{
|
2019-01-22 19:06:20 +03:00
|
|
|
int flags;
|
2018-01-04 02:40:02 +03:00
|
|
|
if( len>0 ) memcpy(pData, pBA, len);
|
2019-01-22 19:06:20 +03:00
|
|
|
if( isReadonly ){
|
|
|
|
flags = SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_READONLY;
|
|
|
|
}else{
|
|
|
|
flags = SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE;
|
|
|
|
}
|
|
|
|
xrc = sqlite3_deserialize(pDb->db, zSchema, pData, len, len, flags);
|
2018-01-03 22:03:31 +03:00
|
|
|
if( xrc ){
|
|
|
|
Tcl_AppendResult(interp, "unable to set MEMDB content", (char*)0);
|
|
|
|
rc = TCL_ERROR;
|
|
|
|
}
|
2019-01-22 19:06:20 +03:00
|
|
|
if( mxSize>0 ){
|
|
|
|
sqlite3_file_control(pDb->db, zSchema,SQLITE_FCNTL_SIZE_LIMIT,&mxSize);
|
|
|
|
}
|
2018-01-03 22:03:31 +03:00
|
|
|
}
|
2019-01-22 19:06:20 +03:00
|
|
|
deserialize_error:
|
2018-01-03 22:03:31 +03:00
|
|
|
#endif
|
2018-01-03 19:49:52 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-07-06 21:08:48 +04:00
|
|
|
/*
|
|
|
|
** $db enable_load_extension BOOLEAN
|
|
|
|
**
|
|
|
|
** Turn the extension loading feature on or off. It if off by
|
|
|
|
** default.
|
|
|
|
*/
|
|
|
|
case DB_ENABLE_LOAD_EXTENSION: {
|
2006-12-19 21:57:11 +03:00
|
|
|
#ifndef SQLITE_OMIT_LOAD_EXTENSION
|
2006-07-06 21:08:48 +04:00
|
|
|
int onoff;
|
|
|
|
if( objc!=3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
sqlite3_enable_load_extension(pDb->db, onoff);
|
|
|
|
break;
|
2006-12-19 21:57:11 +03:00
|
|
|
#else
|
|
|
|
Tcl_AppendResult(interp, "extension loading is turned off at compile-time",
|
2014-02-07 23:26:13 +04:00
|
|
|
(char*)0);
|
2006-12-19 21:57:11 +03:00
|
|
|
return TCL_ERROR;
|
|
|
|
#endif
|
2006-07-06 21:08:48 +04:00
|
|
|
}
|
|
|
|
|
2005-08-30 03:00:03 +04:00
|
|
|
/*
|
|
|
|
** $db errorcode
|
|
|
|
**
|
|
|
|
** Return the numeric error code that was returned by the most recent
|
|
|
|
** call to sqlite3_exec().
|
|
|
|
*/
|
|
|
|
case DB_ERRORCODE: {
|
|
|
|
Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
|
|
|
|
break;
|
|
|
|
}
|
2009-10-06 18:59:02 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** $db exists $sql
|
|
|
|
** $db onecolumn $sql
|
|
|
|
**
|
|
|
|
** The onecolumn method is the equivalent of:
|
|
|
|
** lindex [$db eval $sql] 0
|
|
|
|
*/
|
2016-07-15 00:26:09 +03:00
|
|
|
case DB_EXISTS:
|
2009-10-06 18:59:02 +04:00
|
|
|
case DB_ONECOLUMN: {
|
2016-06-13 15:34:38 +03:00
|
|
|
Tcl_Obj *pResult = 0;
|
2009-10-06 18:59:02 +04:00
|
|
|
DbEvalContext sEval;
|
|
|
|
if( objc!=3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "SQL");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-06-27 00:08:32 +03:00
|
|
|
dbEvalInit(&sEval, pDb, objv[2], 0, 0);
|
2009-10-06 18:59:02 +04:00
|
|
|
rc = dbEvalStep(&sEval);
|
|
|
|
if( choice==DB_ONECOLUMN ){
|
|
|
|
if( rc==TCL_OK ){
|
2016-06-13 15:34:38 +03:00
|
|
|
pResult = dbEvalColumnValue(&sEval, 0);
|
2011-08-18 21:47:57 +04:00
|
|
|
}else if( rc==TCL_BREAK ){
|
|
|
|
Tcl_ResetResult(interp);
|
2009-10-06 18:59:02 +04:00
|
|
|
}
|
|
|
|
}else if( rc==TCL_BREAK || rc==TCL_OK ){
|
2016-06-13 15:34:38 +03:00
|
|
|
pResult = Tcl_NewBooleanObj(rc==TCL_OK);
|
2009-10-06 18:59:02 +04:00
|
|
|
}
|
|
|
|
dbEvalFinalize(&sEval);
|
2016-06-13 15:34:38 +03:00
|
|
|
if( pResult ) Tcl_SetObjResult(interp, pResult);
|
2009-10-06 18:59:02 +04:00
|
|
|
|
|
|
|
if( rc==TCL_BREAK ){
|
|
|
|
rc = TCL_OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-07-15 00:26:09 +03:00
|
|
|
|
2005-08-30 03:00:03 +04:00
|
|
|
/*
|
2017-06-27 00:08:32 +03:00
|
|
|
** $db eval ?options? $sql ?array? ?{ ...code... }?
|
2005-08-30 03:00:03 +04:00
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
*/
|
2009-10-06 18:59:02 +04:00
|
|
|
case DB_EVAL: {
|
2017-06-27 00:08:32 +03:00
|
|
|
int evalFlags = 0;
|
|
|
|
const char *zOpt;
|
|
|
|
while( objc>3 && (zOpt = Tcl_GetString(objv[2]))!=0 && zOpt[0]=='-' ){
|
|
|
|
if( strcmp(zOpt, "-withoutnulls")==0 ){
|
|
|
|
evalFlags |= SQLITE_EVAL_WITHOUTNULLS;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
Tcl_AppendResult(interp, "unknown option: \"", zOpt, "\"", (void*)0);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
objc--;
|
|
|
|
objv++;
|
|
|
|
}
|
2009-10-06 18:59:02 +04:00
|
|
|
if( objc<3 || objc>5 ){
|
2017-06-27 00:08:32 +03:00
|
|
|
Tcl_WrongNumArgs(interp, 2, objv,
|
|
|
|
"?OPTIONS? SQL ?ARRAY-NAME? ?SCRIPT?");
|
2009-10-06 18:59:02 +04:00
|
|
|
return TCL_ERROR;
|
2005-08-30 03:00:03 +04:00
|
|
|
}
|
2007-11-13 13:30:24 +03:00
|
|
|
|
2009-10-06 18:59:02 +04:00
|
|
|
if( objc==3 ){
|
|
|
|
DbEvalContext sEval;
|
|
|
|
Tcl_Obj *pRet = Tcl_NewObj();
|
|
|
|
Tcl_IncrRefCount(pRet);
|
2017-06-27 00:08:32 +03:00
|
|
|
dbEvalInit(&sEval, pDb, objv[2], 0, 0);
|
2009-10-06 18:59:02 +04:00
|
|
|
while( TCL_OK==(rc = dbEvalStep(&sEval)) ){
|
|
|
|
int i;
|
|
|
|
int nCol;
|
|
|
|
dbEvalRowInfo(&sEval, &nCol, 0);
|
2004-08-20 22:34:20 +04:00
|
|
|
for(i=0; i<nCol; i++){
|
2009-10-06 18:59:02 +04:00
|
|
|
Tcl_ListObjAppendElement(interp, pRet, dbEvalColumnValue(&sEval, i));
|
2004-05-27 16:11:31 +04:00
|
|
|
}
|
|
|
|
}
|
2009-10-06 18:59:02 +04:00
|
|
|
dbEvalFinalize(&sEval);
|
2004-09-13 17:16:31 +04:00
|
|
|
if( rc==TCL_BREAK ){
|
2009-10-06 18:59:02 +04:00
|
|
|
Tcl_SetObjResult(interp, pRet);
|
2004-09-13 17:16:31 +04:00
|
|
|
rc = TCL_OK;
|
|
|
|
}
|
2009-10-06 18:59:02 +04:00
|
|
|
Tcl_DecrRefCount(pRet);
|
|
|
|
}else{
|
2015-04-20 00:43:16 +03:00
|
|
|
ClientData cd2[2];
|
2009-10-06 18:59:02 +04:00
|
|
|
DbEvalContext *p;
|
|
|
|
Tcl_Obj *pArray = 0;
|
|
|
|
Tcl_Obj *pScript;
|
2004-08-20 22:34:20 +04:00
|
|
|
|
2017-06-27 00:08:32 +03:00
|
|
|
if( objc>=5 && *(char *)Tcl_GetString(objv[3]) ){
|
2009-10-06 18:59:02 +04:00
|
|
|
pArray = objv[3];
|
2004-05-27 16:11:31 +04:00
|
|
|
}
|
2009-10-06 18:59:02 +04:00
|
|
|
pScript = objv[objc-1];
|
|
|
|
Tcl_IncrRefCount(pScript);
|
2016-07-15 00:26:09 +03:00
|
|
|
|
2009-10-06 18:59:02 +04:00
|
|
|
p = (DbEvalContext *)Tcl_Alloc(sizeof(DbEvalContext));
|
2017-06-27 00:08:32 +03:00
|
|
|
dbEvalInit(p, pDb, objv[2], pArray, evalFlags);
|
2004-05-27 16:11:31 +04:00
|
|
|
|
2015-04-20 00:43:16 +03:00
|
|
|
cd2[0] = (void *)p;
|
|
|
|
cd2[1] = (void *)pScript;
|
|
|
|
rc = DbEvalNextCmd(cd2, interp, TCL_OK);
|
2004-05-27 16:11:31 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2000-08-04 17:49:02 +04:00
|
|
|
|
2002-09-14 17:47:32 +04:00
|
|
|
/*
|
2019-08-15 23:04:09 +03:00
|
|
|
** $db function NAME [OPTIONS] SCRIPT
|
2002-09-14 17:47:32 +04:00
|
|
|
**
|
|
|
|
** Create a new SQL function called NAME. Whenever that function is
|
|
|
|
** called, invoke SCRIPT to evaluate the function.
|
2019-08-15 23:04:09 +03:00
|
|
|
**
|
|
|
|
** Options:
|
|
|
|
** --argcount N Function has exactly N arguments
|
|
|
|
** --deterministic The function is pure
|
|
|
|
** --directonly Prohibit use inside triggers and views
|
2020-01-08 18:44:10 +03:00
|
|
|
** --innocuous Has no side effects or information leaks
|
2019-08-15 23:04:09 +03:00
|
|
|
** --returntype TYPE Specify the return type of the function
|
2002-09-14 17:47:32 +04:00
|
|
|
*/
|
|
|
|
case DB_FUNCTION: {
|
2015-03-13 11:31:54 +03:00
|
|
|
int flags = SQLITE_UTF8;
|
2002-09-14 17:47:32 +04:00
|
|
|
SqlFunc *pFunc;
|
2005-06-26 21:55:33 +04:00
|
|
|
Tcl_Obj *pScript;
|
2002-09-14 17:47:32 +04:00
|
|
|
char *zName;
|
2008-09-09 16:31:33 +04:00
|
|
|
int nArg = -1;
|
2015-03-13 11:31:54 +03:00
|
|
|
int i;
|
2019-02-27 19:38:19 +03:00
|
|
|
int eType = SQLITE_NULL;
|
2015-03-13 11:31:54 +03:00
|
|
|
if( objc<4 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "NAME ?SWITCHES? SCRIPT");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
for(i=3; i<(objc-1); i++){
|
|
|
|
const char *z = Tcl_GetString(objv[i]);
|
2008-12-11 01:15:00 +03:00
|
|
|
int n = strlen30(z);
|
2019-02-27 19:38:19 +03:00
|
|
|
if( n>1 && strncmp(z, "-argcount",n)==0 ){
|
2015-03-13 11:31:54 +03:00
|
|
|
if( i==(objc-2) ){
|
2017-01-12 14:50:08 +03:00
|
|
|
Tcl_AppendResult(interp, "option requires an argument: ", z,(char*)0);
|
2015-03-13 11:31:54 +03:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
if( Tcl_GetIntFromObj(interp, objv[i+1], &nArg) ) return TCL_ERROR;
|
2008-09-09 16:31:33 +04:00
|
|
|
if( nArg<0 ){
|
|
|
|
Tcl_AppendResult(interp, "number of arguments must be non-negative",
|
|
|
|
(char*)0);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2015-03-13 11:31:54 +03:00
|
|
|
i++;
|
|
|
|
}else
|
2019-02-27 19:38:19 +03:00
|
|
|
if( n>1 && strncmp(z, "-deterministic",n)==0 ){
|
2015-03-13 11:31:54 +03:00
|
|
|
flags |= SQLITE_DETERMINISTIC;
|
2019-02-27 19:38:19 +03:00
|
|
|
}else
|
2019-08-15 23:04:09 +03:00
|
|
|
if( n>1 && strncmp(z, "-directonly",n)==0 ){
|
|
|
|
flags |= SQLITE_DIRECTONLY;
|
|
|
|
}else
|
2020-01-08 18:44:10 +03:00
|
|
|
if( n>1 && strncmp(z, "-innocuous",n)==0 ){
|
|
|
|
flags |= SQLITE_INNOCUOUS;
|
|
|
|
}else
|
2019-02-27 19:38:19 +03:00
|
|
|
if( n>1 && strncmp(z, "-returntype", n)==0 ){
|
|
|
|
const char *azType[] = {"integer", "real", "text", "blob", "any", 0};
|
|
|
|
assert( SQLITE_INTEGER==1 && SQLITE_FLOAT==2 && SQLITE_TEXT==3 );
|
|
|
|
assert( SQLITE_BLOB==4 && SQLITE_NULL==5 );
|
|
|
|
if( i==(objc-2) ){
|
|
|
|
Tcl_AppendResult(interp, "option requires an argument: ", z,(char*)0);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
if( Tcl_GetIndexFromObj(interp, objv[i], azType, "type", 0, &eType) ){
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
eType++;
|
2015-03-13 11:31:54 +03:00
|
|
|
}else{
|
2016-07-15 00:26:09 +03:00
|
|
|
Tcl_AppendResult(interp, "bad option \"", z,
|
2019-08-15 23:04:09 +03:00
|
|
|
"\": must be -argcount, -deterministic, -directonly,"
|
2020-01-08 18:44:10 +03:00
|
|
|
" -innocuous, or -returntype", (char*)0
|
2015-03-13 11:31:54 +03:00
|
|
|
);
|
|
|
|
return TCL_ERROR;
|
2008-09-09 16:31:33 +04:00
|
|
|
}
|
2002-09-14 17:47:32 +04:00
|
|
|
}
|
2015-03-13 11:31:54 +03:00
|
|
|
|
|
|
|
pScript = objv[objc-1];
|
2002-09-14 17:47:32 +04:00
|
|
|
zName = Tcl_GetStringFromObj(objv[2], 0);
|
2005-06-26 21:55:33 +04:00
|
|
|
pFunc = findSqlFunc(pDb, zName);
|
2002-09-14 17:47:32 +04:00
|
|
|
if( pFunc==0 ) return TCL_ERROR;
|
2005-06-26 21:55:33 +04:00
|
|
|
if( pFunc->pScript ){
|
|
|
|
Tcl_DecrRefCount(pFunc->pScript);
|
|
|
|
}
|
|
|
|
pFunc->pScript = pScript;
|
|
|
|
Tcl_IncrRefCount(pScript);
|
|
|
|
pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript);
|
2019-02-27 19:38:19 +03:00
|
|
|
pFunc->eType = eType;
|
2015-03-13 11:31:54 +03:00
|
|
|
rc = sqlite3_create_function(pDb->db, zName, nArg, flags,
|
2004-06-12 13:25:12 +04:00
|
|
|
pFunc, tclSqlFunc, 0, 0);
|
2005-01-24 03:28:42 +03:00
|
|
|
if( rc!=SQLITE_OK ){
|
2005-01-25 07:27:54 +03:00
|
|
|
rc = TCL_ERROR;
|
|
|
|
Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
|
2005-01-24 03:28:42 +03:00
|
|
|
}
|
2002-09-14 17:47:32 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-05-01 21:49:49 +04:00
|
|
|
/*
|
2007-05-03 20:31:26 +04:00
|
|
|
** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID
|
2007-05-01 21:49:49 +04:00
|
|
|
*/
|
|
|
|
case DB_INCRBLOB: {
|
2007-05-04 23:03:02 +04:00
|
|
|
#ifdef SQLITE_OMIT_INCRBLOB
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, "incrblob not available in this build", (char*)0);
|
2007-05-04 23:03:02 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
#else
|
2007-05-03 20:31:26 +04:00
|
|
|
int isReadonly = 0;
|
2007-05-01 21:49:49 +04:00
|
|
|
const char *zDb = "main";
|
|
|
|
const char *zTable;
|
|
|
|
const char *zColumn;
|
2012-09-29 18:45:54 +04:00
|
|
|
Tcl_WideInt iRow;
|
2007-05-01 21:49:49 +04:00
|
|
|
|
2007-05-03 20:31:26 +04:00
|
|
|
/* Check for the -readonly option */
|
|
|
|
if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){
|
|
|
|
isReadonly = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID");
|
2007-05-01 21:49:49 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-05-03 20:31:26 +04:00
|
|
|
if( objc==(6+isReadonly) ){
|
2007-05-01 21:49:49 +04:00
|
|
|
zDb = Tcl_GetString(objv[2]);
|
|
|
|
}
|
|
|
|
zTable = Tcl_GetString(objv[objc-3]);
|
|
|
|
zColumn = Tcl_GetString(objv[objc-2]);
|
|
|
|
rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow);
|
|
|
|
|
|
|
|
if( rc==TCL_OK ){
|
2007-05-03 20:31:26 +04:00
|
|
|
rc = createIncrblobChannel(
|
2014-08-19 13:15:41 +04:00
|
|
|
interp, pDb, zDb, zTable, zColumn, (sqlite3_int64)iRow, isReadonly
|
2007-05-03 20:31:26 +04:00
|
|
|
);
|
2007-05-01 21:49:49 +04:00
|
|
|
}
|
2007-05-04 23:03:02 +04:00
|
|
|
#endif
|
2007-05-01 21:49:49 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-07-17 04:02:44 +04:00
|
|
|
/*
|
|
|
|
** $db interrupt
|
|
|
|
**
|
|
|
|
** Interrupt the execution of the inner-most SQL interpreter. This
|
|
|
|
** causes the SQL statement to return an error of SQLITE_INTERRUPT.
|
|
|
|
*/
|
|
|
|
case DB_INTERRUPT: {
|
|
|
|
sqlite3_interrupt(pDb->db);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-08-30 03:00:03 +04:00
|
|
|
/*
|
|
|
|
** $db nullvalue ?STRING?
|
|
|
|
**
|
|
|
|
** Change text used when a NULL comes back from the database. If ?STRING?
|
|
|
|
** is not present, then the current string used for NULL is returned.
|
|
|
|
** If STRING is present, then STRING is returned.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
case DB_NULLVALUE: {
|
|
|
|
if( objc!=2 && objc!=3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
if( objc==3 ){
|
|
|
|
int len;
|
|
|
|
char *zNull = Tcl_GetStringFromObj(objv[2], &len);
|
|
|
|
if( pDb->zNull ){
|
|
|
|
Tcl_Free(pDb->zNull);
|
|
|
|
}
|
|
|
|
if( zNull && len>0 ){
|
|
|
|
pDb->zNull = Tcl_Alloc( len + 1 );
|
2011-06-20 23:00:30 +04:00
|
|
|
memcpy(pDb->zNull, zNull, len);
|
2005-08-30 03:00:03 +04:00
|
|
|
pDb->zNull[len] = '\0';
|
|
|
|
}else{
|
|
|
|
pDb->zNull = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-10-03 15:02:33 +04:00
|
|
|
Tcl_SetObjResult(interp, Tcl_NewStringObj(pDb->zNull, -1));
|
2005-08-30 03:00:03 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-01-17 00:00:27 +03:00
|
|
|
/*
|
2016-07-15 00:26:09 +03:00
|
|
|
** $db last_insert_rowid
|
2002-01-17 00:00:27 +03:00
|
|
|
**
|
|
|
|
** Return an integer which is the ROWID for the most recent insert.
|
|
|
|
*/
|
|
|
|
case DB_LAST_INSERT_ROWID: {
|
|
|
|
Tcl_Obj *pResult;
|
2006-06-21 23:30:34 +04:00
|
|
|
Tcl_WideInt rowid;
|
2002-01-17 00:00:27 +03:00
|
|
|
if( objc!=2 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2004-05-10 14:34:51 +04:00
|
|
|
rowid = sqlite3_last_insert_rowid(pDb->db);
|
2002-01-17 00:00:27 +03:00
|
|
|
pResult = Tcl_GetObjResult(interp);
|
2006-06-21 23:30:34 +04:00
|
|
|
Tcl_SetWideIntObj(pResult, rowid);
|
2002-01-17 00:00:27 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-08-19 18:31:01 +04:00
|
|
|
/*
|
2009-10-06 18:59:02 +04:00
|
|
|
** The DB_ONECOLUMN method is implemented together with DB_EXISTS.
|
2003-08-19 18:31:01 +04:00
|
|
|
*/
|
2004-09-07 17:20:35 +04:00
|
|
|
|
|
|
|
/* $db progress ?N CALLBACK?
|
2016-07-15 00:26:09 +03:00
|
|
|
**
|
2004-09-07 17:20:35 +04:00
|
|
|
** Invoke the given callback every N virtual machine opcodes while executing
|
|
|
|
** queries.
|
|
|
|
*/
|
|
|
|
case DB_PROGRESS: {
|
|
|
|
if( objc==2 ){
|
|
|
|
if( pDb->zProgress ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, pDb->zProgress, (char*)0);
|
2004-09-07 17:20:35 +04:00
|
|
|
}
|
|
|
|
}else if( objc==4 ){
|
|
|
|
char *zProgress;
|
|
|
|
int len;
|
|
|
|
int N;
|
|
|
|
if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
|
2007-08-07 21:13:03 +04:00
|
|
|
return TCL_ERROR;
|
2004-09-07 17:20:35 +04:00
|
|
|
};
|
|
|
|
if( pDb->zProgress ){
|
|
|
|
Tcl_Free(pDb->zProgress);
|
|
|
|
}
|
|
|
|
zProgress = Tcl_GetStringFromObj(objv[3], &len);
|
|
|
|
if( zProgress && len>0 ){
|
|
|
|
pDb->zProgress = Tcl_Alloc( len + 1 );
|
2007-05-04 17:15:55 +04:00
|
|
|
memcpy(pDb->zProgress, zProgress, len+1);
|
2004-09-07 17:20:35 +04:00
|
|
|
}else{
|
|
|
|
pDb->zProgress = 0;
|
|
|
|
}
|
|
|
|
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
|
|
|
|
if( pDb->zProgress ){
|
|
|
|
pDb->interp = interp;
|
|
|
|
sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
|
|
|
|
}else{
|
|
|
|
sqlite3_progress_handler(pDb->db, 0, 0, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}else{
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
|
2003-08-19 18:31:01 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-08-30 03:00:03 +04:00
|
|
|
/* $db profile ?CALLBACK?
|
|
|
|
**
|
|
|
|
** Make arrangements to invoke the CALLBACK routine after each SQL statement
|
|
|
|
** that has run. The text of the SQL and the amount of elapse time are
|
|
|
|
** appended to CALLBACK before the script is run.
|
|
|
|
*/
|
|
|
|
case DB_PROFILE: {
|
|
|
|
if( objc>3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}else if( objc==2 ){
|
|
|
|
if( pDb->zProfile ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, pDb->zProfile, (char*)0);
|
2005-08-30 03:00:03 +04:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
char *zProfile;
|
|
|
|
int len;
|
|
|
|
if( pDb->zProfile ){
|
|
|
|
Tcl_Free(pDb->zProfile);
|
|
|
|
}
|
|
|
|
zProfile = Tcl_GetStringFromObj(objv[2], &len);
|
|
|
|
if( zProfile && len>0 ){
|
|
|
|
pDb->zProfile = Tcl_Alloc( len + 1 );
|
2007-05-04 17:15:55 +04:00
|
|
|
memcpy(pDb->zProfile, zProfile, len+1);
|
2005-08-30 03:00:03 +04:00
|
|
|
}else{
|
|
|
|
pDb->zProfile = 0;
|
|
|
|
}
|
2016-09-10 22:51:40 +03:00
|
|
|
#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \
|
|
|
|
!defined(SQLITE_OMIT_DEPRECATED)
|
2005-08-30 03:00:03 +04:00
|
|
|
if( pDb->zProfile ){
|
|
|
|
pDb->interp = interp;
|
|
|
|
sqlite3_profile(pDb->db, DbProfileHandler, pDb);
|
|
|
|
}else{
|
|
|
|
sqlite3_profile(pDb->db, 0, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-02-01 04:22:50 +03:00
|
|
|
/*
|
|
|
|
** $db rekey KEY
|
|
|
|
**
|
|
|
|
** Change the encryption key on the currently open database.
|
|
|
|
*/
|
|
|
|
case DB_REKEY: {
|
|
|
|
if( objc!=3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "KEY");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-02-05 01:46:47 +03:00
|
|
|
/* $db restore ?DATABASE? FILENAME
|
|
|
|
**
|
2016-07-15 00:26:09 +03:00
|
|
|
** Open a database file named FILENAME. Transfer the content
|
2009-02-05 01:46:47 +03:00
|
|
|
** of FILENAME into the local database DATABASE (default: "main").
|
|
|
|
*/
|
|
|
|
case DB_RESTORE: {
|
|
|
|
const char *zSrcFile;
|
|
|
|
const char *zDestDb;
|
|
|
|
sqlite3 *pSrc;
|
|
|
|
sqlite3_backup *pBackup;
|
|
|
|
int nTimeout = 0;
|
|
|
|
|
|
|
|
if( objc==3 ){
|
|
|
|
zDestDb = "main";
|
|
|
|
zSrcFile = Tcl_GetString(objv[2]);
|
|
|
|
}else if( objc==4 ){
|
|
|
|
zDestDb = Tcl_GetString(objv[2]);
|
|
|
|
zSrcFile = Tcl_GetString(objv[3]);
|
|
|
|
}else{
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2016-01-23 02:17:51 +03:00
|
|
|
rc = sqlite3_open_v2(zSrcFile, &pSrc,
|
|
|
|
SQLITE_OPEN_READONLY | pDb->openFlags, 0);
|
2009-02-05 01:46:47 +03:00
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
Tcl_AppendResult(interp, "cannot open source database: ",
|
|
|
|
sqlite3_errmsg(pSrc), (char*)0);
|
|
|
|
sqlite3_close(pSrc);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
pBackup = sqlite3_backup_init(pDb->db, zDestDb, pSrc, "main");
|
|
|
|
if( pBackup==0 ){
|
|
|
|
Tcl_AppendResult(interp, "restore failed: ",
|
|
|
|
sqlite3_errmsg(pDb->db), (char*)0);
|
|
|
|
sqlite3_close(pSrc);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
|
|
|
|
|| rc==SQLITE_BUSY ){
|
|
|
|
if( rc==SQLITE_BUSY ){
|
|
|
|
if( nTimeout++ >= 3 ) break;
|
|
|
|
sqlite3_sleep(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sqlite3_backup_finish(pBackup);
|
|
|
|
if( rc==SQLITE_DONE ){
|
|
|
|
rc = TCL_OK;
|
|
|
|
}else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
|
|
|
|
Tcl_AppendResult(interp, "restore failed: source database busy",
|
|
|
|
(char*)0);
|
|
|
|
rc = TCL_ERROR;
|
|
|
|
}else{
|
|
|
|
Tcl_AppendResult(interp, "restore failed: ",
|
|
|
|
sqlite3_errmsg(pDb->db), (char*)0);
|
|
|
|
rc = TCL_ERROR;
|
|
|
|
}
|
|
|
|
sqlite3_close(pSrc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:49:52 +03:00
|
|
|
/*
|
|
|
|
** $db serialize ?DATABASE?
|
|
|
|
**
|
|
|
|
** Return a serialization of a database.
|
|
|
|
*/
|
|
|
|
case DB_SERIALIZE: {
|
2021-05-08 20:18:23 +03:00
|
|
|
#ifdef SQLITE_OMIT_DESERIALIZE
|
2018-01-03 19:49:52 +03:00
|
|
|
Tcl_AppendResult(interp, "MEMDB not available in this build",
|
|
|
|
(char*)0);
|
|
|
|
rc = TCL_ERROR;
|
|
|
|
#else
|
|
|
|
const char *zSchema = objc>=3 ? Tcl_GetString(objv[2]) : "main";
|
|
|
|
sqlite3_int64 sz = 0;
|
|
|
|
unsigned char *pData;
|
|
|
|
if( objc!=2 && objc!=3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE?");
|
|
|
|
rc = TCL_ERROR;
|
|
|
|
}else{
|
|
|
|
int needFree;
|
|
|
|
pData = sqlite3_serialize(pDb->db, zSchema, &sz, SQLITE_SERIALIZE_NOCOPY);
|
|
|
|
if( pData ){
|
|
|
|
needFree = 0;
|
|
|
|
}else{
|
|
|
|
pData = sqlite3_serialize(pDb->db, zSchema, &sz, 0);
|
|
|
|
needFree = 1;
|
|
|
|
}
|
|
|
|
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pData,sz));
|
|
|
|
if( needFree ) sqlite3_free(pData);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-10-08 03:46:38 +04:00
|
|
|
/*
|
2017-06-22 19:51:16 +03:00
|
|
|
** $db status (step|sort|autoindex|vmstep)
|
2008-10-08 03:46:38 +04:00
|
|
|
**
|
2016-07-15 00:26:09 +03:00
|
|
|
** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or
|
2008-10-08 03:46:38 +04:00
|
|
|
** SQLITE_STMTSTATUS_SORT for the most recent eval.
|
|
|
|
*/
|
|
|
|
case DB_STATUS: {
|
|
|
|
int v;
|
|
|
|
const char *zOp;
|
|
|
|
if( objc!=3 ){
|
2010-08-02 02:41:32 +04:00
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "(step|sort|autoindex)");
|
2008-10-08 03:46:38 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
zOp = Tcl_GetString(objv[2]);
|
|
|
|
if( strcmp(zOp, "step")==0 ){
|
|
|
|
v = pDb->nStep;
|
|
|
|
}else if( strcmp(zOp, "sort")==0 ){
|
|
|
|
v = pDb->nSort;
|
2010-04-07 23:31:59 +04:00
|
|
|
}else if( strcmp(zOp, "autoindex")==0 ){
|
|
|
|
v = pDb->nIndex;
|
2017-06-22 19:51:16 +03:00
|
|
|
}else if( strcmp(zOp, "vmstep")==0 ){
|
|
|
|
v = pDb->nVMStep;
|
2008-10-08 03:46:38 +04:00
|
|
|
}else{
|
2016-07-15 00:26:09 +03:00
|
|
|
Tcl_AppendResult(interp,
|
2017-06-22 19:51:16 +03:00
|
|
|
"bad argument: should be autoindex, step, sort or vmstep",
|
2008-10-08 03:46:38 +04:00
|
|
|
(char*)0);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
Tcl_SetObjResult(interp, Tcl_NewIntObj(v));
|
|
|
|
break;
|
|
|
|
}
|
2016-07-15 00:26:09 +03:00
|
|
|
|
2000-08-04 17:49:02 +04:00
|
|
|
/*
|
|
|
|
** $db timeout MILLESECONDS
|
|
|
|
**
|
|
|
|
** Delay for the number of milliseconds specified when a file is locked.
|
|
|
|
*/
|
2000-09-21 17:01:35 +04:00
|
|
|
case DB_TIMEOUT: {
|
2000-08-04 17:49:02 +04:00
|
|
|
int ms;
|
2000-09-21 17:01:35 +04:00
|
|
|
if( objc!=3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
|
2000-08-04 17:49:02 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2000-09-21 17:01:35 +04:00
|
|
|
if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
|
2004-05-10 14:34:51 +04:00
|
|
|
sqlite3_busy_timeout(pDb->db, ms);
|
2000-09-21 17:01:35 +04:00
|
|
|
break;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2016-07-15 00:26:09 +03:00
|
|
|
|
2004-06-29 16:39:08 +04:00
|
|
|
/*
|
|
|
|
** $db total_changes
|
|
|
|
**
|
2016-07-15 00:26:09 +03:00
|
|
|
** Return the number of rows that were modified, inserted, or deleted
|
2004-06-29 16:39:08 +04:00
|
|
|
** since the database handle was created.
|
|
|
|
*/
|
|
|
|
case DB_TOTAL_CHANGES: {
|
|
|
|
Tcl_Obj *pResult;
|
|
|
|
if( objc!=2 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
pResult = Tcl_GetObjResult(interp);
|
2021-06-22 21:32:05 +03:00
|
|
|
Tcl_SetWideIntObj(pResult, sqlite3_total_changes64(pDb->db));
|
2004-06-29 16:39:08 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-04-23 16:25:23 +04:00
|
|
|
/* $db trace ?CALLBACK?
|
|
|
|
**
|
|
|
|
** Make arrangements to invoke the CALLBACK routine for each SQL statement
|
|
|
|
** that is executed. The text of the SQL is appended to CALLBACK before
|
|
|
|
** it is executed.
|
|
|
|
*/
|
|
|
|
case DB_TRACE: {
|
|
|
|
if( objc>3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
|
2004-06-29 15:26:59 +04:00
|
|
|
return TCL_ERROR;
|
2003-04-23 16:25:23 +04:00
|
|
|
}else if( objc==2 ){
|
|
|
|
if( pDb->zTrace ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, pDb->zTrace, (char*)0);
|
2003-04-23 16:25:23 +04:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
char *zTrace;
|
|
|
|
int len;
|
|
|
|
if( pDb->zTrace ){
|
|
|
|
Tcl_Free(pDb->zTrace);
|
|
|
|
}
|
|
|
|
zTrace = Tcl_GetStringFromObj(objv[2], &len);
|
|
|
|
if( zTrace && len>0 ){
|
|
|
|
pDb->zTrace = Tcl_Alloc( len + 1 );
|
2007-05-04 17:15:55 +04:00
|
|
|
memcpy(pDb->zTrace, zTrace, len+1);
|
2003-04-23 16:25:23 +04:00
|
|
|
}else{
|
|
|
|
pDb->zTrace = 0;
|
|
|
|
}
|
2016-09-10 22:51:40 +03:00
|
|
|
#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \
|
|
|
|
!defined(SQLITE_OMIT_DEPRECATED)
|
2003-04-23 16:25:23 +04:00
|
|
|
if( pDb->zTrace ){
|
|
|
|
pDb->interp = interp;
|
2004-05-10 14:34:51 +04:00
|
|
|
sqlite3_trace(pDb->db, DbTraceHandler, pDb);
|
2003-04-23 16:25:23 +04:00
|
|
|
}else{
|
2004-05-10 14:34:51 +04:00
|
|
|
sqlite3_trace(pDb->db, 0, 0);
|
2003-04-23 16:25:23 +04:00
|
|
|
}
|
2005-08-30 03:00:03 +04:00
|
|
|
#endif
|
2003-04-23 16:25:23 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-07-15 00:26:09 +03:00
|
|
|
/* $db trace_v2 ?CALLBACK? ?MASK?
|
|
|
|
**
|
|
|
|
** Make arrangements to invoke the CALLBACK routine for each trace event
|
|
|
|
** matching the mask that is generated. The parameters are appended to
|
|
|
|
** CALLBACK before it is executed.
|
|
|
|
*/
|
|
|
|
case DB_TRACE_V2: {
|
|
|
|
if( objc>4 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK? ?MASK?");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}else if( objc==2 ){
|
|
|
|
if( pDb->zTraceV2 ){
|
|
|
|
Tcl_AppendResult(interp, pDb->zTraceV2, (char*)0);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
char *zTraceV2;
|
|
|
|
int len;
|
2016-07-15 02:17:03 +03:00
|
|
|
Tcl_WideInt wMask = 0;
|
2016-07-15 00:26:09 +03:00
|
|
|
if( objc==4 ){
|
2016-07-15 02:17:03 +03:00
|
|
|
static const char *TTYPE_strs[] = {
|
|
|
|
"statement", "profile", "row", "close", 0
|
|
|
|
};
|
|
|
|
enum TTYPE_enum {
|
|
|
|
TTYPE_STMT, TTYPE_PROFILE, TTYPE_ROW, TTYPE_CLOSE
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
if( TCL_OK!=Tcl_ListObjLength(interp, objv[3], &len) ){
|
2016-07-15 00:26:09 +03:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2016-07-15 02:17:03 +03:00
|
|
|
for(i=0; i<len; i++){
|
|
|
|
Tcl_Obj *pObj;
|
|
|
|
int ttype;
|
|
|
|
if( TCL_OK!=Tcl_ListObjIndex(interp, objv[3], i, &pObj) ){
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
if( Tcl_GetIndexFromObj(interp, pObj, TTYPE_strs, "trace type",
|
|
|
|
0, &ttype)!=TCL_OK ){
|
|
|
|
Tcl_WideInt wType;
|
|
|
|
Tcl_Obj *pError = Tcl_DuplicateObj(Tcl_GetObjResult(interp));
|
|
|
|
Tcl_IncrRefCount(pError);
|
|
|
|
if( TCL_OK==Tcl_GetWideIntFromObj(interp, pObj, &wType) ){
|
|
|
|
Tcl_DecrRefCount(pError);
|
|
|
|
wMask |= wType;
|
|
|
|
}else{
|
|
|
|
Tcl_SetObjResult(interp, pError);
|
|
|
|
Tcl_DecrRefCount(pError);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
switch( (enum TTYPE_enum)ttype ){
|
|
|
|
case TTYPE_STMT: wMask |= SQLITE_TRACE_STMT; break;
|
|
|
|
case TTYPE_PROFILE: wMask |= SQLITE_TRACE_PROFILE; break;
|
|
|
|
case TTYPE_ROW: wMask |= SQLITE_TRACE_ROW; break;
|
|
|
|
case TTYPE_CLOSE: wMask |= SQLITE_TRACE_CLOSE; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 00:26:09 +03:00
|
|
|
}else{
|
2016-07-15 02:17:03 +03:00
|
|
|
wMask = SQLITE_TRACE_STMT; /* use the "legacy" default */
|
2016-07-15 00:26:09 +03:00
|
|
|
}
|
|
|
|
if( pDb->zTraceV2 ){
|
|
|
|
Tcl_Free(pDb->zTraceV2);
|
|
|
|
}
|
|
|
|
zTraceV2 = Tcl_GetStringFromObj(objv[2], &len);
|
|
|
|
if( zTraceV2 && len>0 ){
|
|
|
|
pDb->zTraceV2 = Tcl_Alloc( len + 1 );
|
|
|
|
memcpy(pDb->zTraceV2, zTraceV2, len+1);
|
|
|
|
}else{
|
|
|
|
pDb->zTraceV2 = 0;
|
|
|
|
}
|
|
|
|
#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
|
|
|
|
if( pDb->zTraceV2 ){
|
|
|
|
pDb->interp = interp;
|
|
|
|
sqlite3_trace_v2(pDb->db, (unsigned)wMask, DbTraceV2Handler, pDb);
|
|
|
|
}else{
|
|
|
|
sqlite3_trace_v2(pDb->db, 0, 0, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-08-02 16:21:08 +04:00
|
|
|
/* $db transaction [-deferred|-immediate|-exclusive] SCRIPT
|
|
|
|
**
|
|
|
|
** Start a new transaction (if we are not already in the midst of a
|
|
|
|
** transaction) and execute the TCL script SCRIPT. After SCRIPT
|
|
|
|
** completes, either commit the transaction or roll it back if SCRIPT
|
|
|
|
** throws an exception. Or if no new transation was started, do nothing.
|
|
|
|
** pass the exception on up the stack.
|
|
|
|
**
|
|
|
|
** This command was inspired by Dave Thomas's talk on Ruby at the
|
|
|
|
** 2005 O'Reilly Open Source Convention (OSCON).
|
|
|
|
*/
|
|
|
|
case DB_TRANSACTION: {
|
|
|
|
Tcl_Obj *pScript;
|
2009-01-02 20:33:46 +03:00
|
|
|
const char *zBegin = "SAVEPOINT _tcl_transaction";
|
2005-08-02 16:21:08 +04:00
|
|
|
if( objc!=3 && objc!=4 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2009-01-02 20:33:46 +03:00
|
|
|
|
2009-10-06 18:59:02 +04:00
|
|
|
if( pDb->nTransaction==0 && objc==4 ){
|
2005-08-02 16:21:08 +04:00
|
|
|
static const char *TTYPE_strs[] = {
|
2005-08-16 15:11:34 +04:00
|
|
|
"deferred", "exclusive", "immediate", 0
|
2005-08-02 16:21:08 +04:00
|
|
|
};
|
|
|
|
enum TTYPE_enum {
|
|
|
|
TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE
|
|
|
|
};
|
|
|
|
int ttype;
|
2005-08-02 21:15:14 +04:00
|
|
|
if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type",
|
2005-08-02 16:21:08 +04:00
|
|
|
0, &ttype) ){
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
switch( (enum TTYPE_enum)ttype ){
|
|
|
|
case TTYPE_DEFERRED: /* no-op */; break;
|
|
|
|
case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break;
|
|
|
|
case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break;
|
|
|
|
}
|
|
|
|
}
|
2009-01-02 20:33:46 +03:00
|
|
|
pScript = objv[objc-1];
|
|
|
|
|
2009-10-06 18:59:02 +04:00
|
|
|
/* Run the SQLite BEGIN command to open a transaction or savepoint. */
|
2009-01-02 20:33:46 +03:00
|
|
|
pDb->disableAuth++;
|
|
|
|
rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0);
|
|
|
|
pDb->disableAuth--;
|
|
|
|
if( rc!=SQLITE_OK ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0);
|
2009-01-02 20:33:46 +03:00
|
|
|
return TCL_ERROR;
|
2005-08-02 16:21:08 +04:00
|
|
|
}
|
2009-01-02 20:33:46 +03:00
|
|
|
pDb->nTransaction++;
|
|
|
|
|
2009-10-06 18:59:02 +04:00
|
|
|
/* 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
|
2016-07-15 00:26:09 +03:00
|
|
|
** call function DbTransPostCmd() to commit (or rollback) the transaction
|
2009-10-06 18:59:02 +04:00
|
|
|
** or savepoint. */
|
|
|
|
if( DbUseNre() ){
|
|
|
|
Tcl_NRAddCallback(interp, DbTransPostCmd, cd, 0, 0, 0);
|
2013-12-20 22:57:44 +04:00
|
|
|
(void)Tcl_NREvalObj(interp, pScript, 0);
|
2009-01-02 20:33:46 +03:00
|
|
|
}else{
|
2009-10-06 18:59:02 +04:00
|
|
|
rc = DbTransPostCmd(&cd, interp, Tcl_EvalObjEx(interp, pScript, 0));
|
2009-01-02 20:33:46 +03:00
|
|
|
}
|
2005-08-02 16:21:08 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-03-16 16:19:36 +03:00
|
|
|
/*
|
|
|
|
** $db unlock_notify ?script?
|
|
|
|
*/
|
|
|
|
case DB_UNLOCK_NOTIFY: {
|
|
|
|
#ifndef SQLITE_ENABLE_UNLOCK_NOTIFY
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, "unlock_notify not available in this build",
|
|
|
|
(char*)0);
|
2009-03-16 16:19:36 +03:00
|
|
|
rc = TCL_ERROR;
|
|
|
|
#else
|
|
|
|
if( objc!=2 && objc!=3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?");
|
|
|
|
rc = TCL_ERROR;
|
|
|
|
}else{
|
|
|
|
void (*xNotify)(void **, int) = 0;
|
|
|
|
void *pNotifyArg = 0;
|
|
|
|
|
|
|
|
if( pDb->pUnlockNotify ){
|
|
|
|
Tcl_DecrRefCount(pDb->pUnlockNotify);
|
|
|
|
pDb->pUnlockNotify = 0;
|
|
|
|
}
|
2016-07-15 00:26:09 +03:00
|
|
|
|
2009-03-16 16:19:36 +03:00
|
|
|
if( objc==3 ){
|
|
|
|
xNotify = DbUnlockNotify;
|
|
|
|
pNotifyArg = (void *)pDb;
|
|
|
|
pDb->pUnlockNotify = objv[2];
|
|
|
|
Tcl_IncrRefCount(pDb->pUnlockNotify);
|
|
|
|
}
|
2016-07-15 00:26:09 +03:00
|
|
|
|
2009-03-16 16:19:36 +03:00
|
|
|
if( sqlite3_unlock_notify(pDb->db, xNotify, pNotifyArg) ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0);
|
2009-03-16 16:19:36 +03:00
|
|
|
rc = TCL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-18 19:47:27 +03:00
|
|
|
/*
|
|
|
|
** $db preupdate_hook count
|
|
|
|
** $db preupdate_hook hook ?SCRIPT?
|
|
|
|
** $db preupdate_hook new INDEX
|
|
|
|
** $db preupdate_hook old INDEX
|
|
|
|
*/
|
2011-03-01 21:42:07 +03:00
|
|
|
case DB_PREUPDATE: {
|
2011-03-31 01:04:43 +04:00
|
|
|
#ifndef SQLITE_ENABLE_PREUPDATE_HOOK
|
2016-12-06 22:33:42 +03:00
|
|
|
Tcl_AppendResult(interp, "preupdate_hook was omitted at compile-time",
|
|
|
|
(char*)0);
|
2011-03-31 01:04:43 +04:00
|
|
|
rc = TCL_ERROR;
|
|
|
|
#else
|
2011-03-22 21:45:29 +03:00
|
|
|
static const char *azSub[] = {"count", "depth", "hook", "new", "old", 0};
|
2011-03-01 21:42:07 +03:00
|
|
|
enum DbPreupdateSubCmd {
|
2011-03-22 21:45:29 +03:00
|
|
|
PRE_COUNT, PRE_DEPTH, PRE_HOOK, PRE_NEW, PRE_OLD
|
2011-03-01 21:42:07 +03:00
|
|
|
};
|
|
|
|
int iSub;
|
|
|
|
|
|
|
|
if( objc<3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "SUB-COMMAND ?ARGS?");
|
|
|
|
}
|
|
|
|
if( Tcl_GetIndexFromObj(interp, objv[2], azSub, "sub-command", 0, &iSub) ){
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( (enum DbPreupdateSubCmd)iSub ){
|
|
|
|
case PRE_COUNT: {
|
|
|
|
int nCol = sqlite3_preupdate_count(pDb->db);
|
|
|
|
Tcl_SetObjResult(interp, Tcl_NewIntObj(nCol));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PRE_HOOK: {
|
|
|
|
if( objc>4 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "hook ?SCRIPT?");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
DbHookCmd(interp, pDb, (objc==4 ? objv[3] : 0), &pDb->pPreUpdateHook);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-22 21:45:29 +03:00
|
|
|
case PRE_DEPTH: {
|
|
|
|
Tcl_Obj *pRet;
|
|
|
|
if( objc!=3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 3, objv, "");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
pRet = Tcl_NewIntObj(sqlite3_preupdate_depth(pDb->db));
|
|
|
|
Tcl_SetObjResult(interp, pRet);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-16 22:59:18 +03:00
|
|
|
case PRE_NEW:
|
2011-03-01 21:42:07 +03:00
|
|
|
case PRE_OLD: {
|
|
|
|
int iIdx;
|
2011-03-16 22:59:18 +03:00
|
|
|
sqlite3_value *pValue;
|
2011-03-01 21:42:07 +03:00
|
|
|
if( objc!=4 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 3, objv, "INDEX");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
if( Tcl_GetIntFromObj(interp, objv[3], &iIdx) ){
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-03-16 22:59:18 +03:00
|
|
|
if( iSub==PRE_OLD ){
|
2011-03-01 21:42:07 +03:00
|
|
|
rc = sqlite3_preupdate_old(pDb->db, iIdx, &pValue);
|
2011-03-16 22:59:18 +03:00
|
|
|
}else{
|
|
|
|
assert( iSub==PRE_NEW );
|
|
|
|
rc = sqlite3_preupdate_new(pDb->db, iIdx, &pValue);
|
2011-03-01 21:42:07 +03:00
|
|
|
}
|
|
|
|
|
2011-03-16 22:59:18 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
2011-03-18 19:47:27 +03:00
|
|
|
Tcl_Obj *pObj;
|
|
|
|
pObj = Tcl_NewStringObj((char*)sqlite3_value_text(pValue), -1);
|
2011-03-16 22:59:18 +03:00
|
|
|
Tcl_SetObjResult(interp, pObj);
|
|
|
|
}else{
|
2017-01-12 14:50:08 +03:00
|
|
|
Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0);
|
2011-03-01 21:42:07 +03:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-31 01:04:43 +04:00
|
|
|
#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
|
2011-03-01 21:42:07 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-12-15 18:22:08 +03:00
|
|
|
/*
|
2010-04-28 18:42:19 +04:00
|
|
|
** $db wal_hook ?script?
|
2005-12-15 18:22:08 +03:00
|
|
|
** $db update_hook ?script?
|
2005-12-16 09:54:01 +03:00
|
|
|
** $db rollback_hook ?script?
|
2005-12-15 18:22:08 +03:00
|
|
|
*/
|
2016-07-15 00:26:09 +03:00
|
|
|
case DB_WAL_HOOK:
|
|
|
|
case DB_UPDATE_HOOK:
|
2011-03-16 12:49:14 +03:00
|
|
|
case DB_ROLLBACK_HOOK: {
|
2016-07-15 00:26:09 +03:00
|
|
|
/* set ppHook to point at pUpdateHook or pRollbackHook, depending on
|
2005-12-16 09:54:01 +03:00
|
|
|
** whether [$db update_hook] or [$db rollback_hook] was invoked.
|
|
|
|
*/
|
2016-07-15 00:26:09 +03:00
|
|
|
Tcl_Obj **ppHook = 0;
|
2011-03-01 21:42:07 +03:00
|
|
|
if( choice==DB_WAL_HOOK ) ppHook = &pDb->pWalHook;
|
|
|
|
if( choice==DB_UPDATE_HOOK ) ppHook = &pDb->pUpdateHook;
|
|
|
|
if( choice==DB_ROLLBACK_HOOK ) ppHook = &pDb->pRollbackHook;
|
|
|
|
if( objc>3 ){
|
2005-12-15 18:22:08 +03:00
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2005-12-16 09:54:01 +03:00
|
|
|
|
2011-03-01 21:42:07 +03:00
|
|
|
DbHookCmd(interp, pDb, (objc==3 ? objv[2] : 0), ppHook);
|
2005-12-15 18:22:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-01-12 15:44:03 +03:00
|
|
|
/* $db version
|
|
|
|
**
|
|
|
|
** Return the version string for this database.
|
|
|
|
*/
|
|
|
|
case DB_VERSION: {
|
2017-10-13 18:56:26 +03:00
|
|
|
int i;
|
|
|
|
for(i=2; i<objc; i++){
|
|
|
|
const char *zArg = Tcl_GetString(objv[i]);
|
|
|
|
/* Optional arguments to $db version are used for testing purpose */
|
|
|
|
#ifdef SQLITE_TEST
|
|
|
|
/* $db version -use-legacy-prepare BOOLEAN
|
|
|
|
**
|
|
|
|
** Turn the use of legacy sqlite3_prepare() on or off.
|
|
|
|
*/
|
|
|
|
if( strcmp(zArg, "-use-legacy-prepare")==0 && i+1<objc ){
|
|
|
|
i++;
|
|
|
|
if( Tcl_GetBooleanFromObj(interp, objv[i], &pDb->bLegacyPrepare) ){
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
}else
|
|
|
|
|
|
|
|
/* $db version -last-stmt-ptr
|
|
|
|
**
|
|
|
|
** Return a string which is a hex encoding of the pointer to the
|
|
|
|
** most recent sqlite3_stmt in the statement cache.
|
|
|
|
*/
|
|
|
|
if( strcmp(zArg, "-last-stmt-ptr")==0 ){
|
|
|
|
char zBuf[100];
|
|
|
|
sqlite3_snprintf(sizeof(zBuf), zBuf, "%p",
|
|
|
|
pDb->stmtList ? pDb->stmtList->pStmt: 0);
|
|
|
|
Tcl_SetResult(interp, zBuf, TCL_VOLATILE);
|
|
|
|
}else
|
|
|
|
#endif /* SQLITE_TEST */
|
|
|
|
{
|
|
|
|
Tcl_AppendResult(interp, "unknown argument: ", zArg, (char*)0);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( i==2 ){
|
|
|
|
Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC);
|
|
|
|
}
|
2005-01-12 15:44:03 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-12-17 18:41:11 +03:00
|
|
|
|
2000-09-21 17:01:35 +04:00
|
|
|
} /* End of the SWITCH statement */
|
2004-02-01 04:22:50 +03:00
|
|
|
return rc;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
|
|
|
|
2009-10-13 22:38:34 +04:00
|
|
|
#if SQLITE_TCL_NRE
|
|
|
|
/*
|
|
|
|
** Adaptor that provides an objCmd interface to the NRE-enabled
|
|
|
|
** interface implementation.
|
|
|
|
*/
|
2016-07-28 21:06:52 +03:00
|
|
|
static int SQLITE_TCLAPI DbObjCmdAdaptor(
|
2009-10-13 22:38:34 +04:00
|
|
|
void *cd,
|
|
|
|
Tcl_Interp *interp,
|
|
|
|
int objc,
|
|
|
|
Tcl_Obj *const*objv
|
|
|
|
){
|
|
|
|
return Tcl_NRCallObjProc(interp, DbObjCmd, cd, objc, objv);
|
|
|
|
}
|
|
|
|
#endif /* SQLITE_TCL_NRE */
|
|
|
|
|
2018-01-03 16:20:02 +03:00
|
|
|
/*
|
|
|
|
** Issue the usage message when the "sqlite3" command arguments are
|
|
|
|
** incorrect.
|
|
|
|
*/
|
|
|
|
static int sqliteCmdUsage(
|
|
|
|
Tcl_Interp *interp,
|
|
|
|
Tcl_Obj *const*objv
|
|
|
|
){
|
|
|
|
Tcl_WrongNumArgs(interp, 1, objv,
|
|
|
|
"HANDLE ?FILENAME? ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?"
|
2019-11-18 20:46:38 +03:00
|
|
|
" ?-nofollow BOOLEAN?"
|
2018-01-03 16:20:02 +03:00
|
|
|
" ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?"
|
|
|
|
);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
|
2000-05-29 18:26:00 +04:00
|
|
|
/*
|
2007-08-31 18:31:44 +04:00
|
|
|
** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN?
|
2008-07-10 21:52:49 +04:00
|
|
|
** ?-create BOOLEAN? ?-nomutex BOOLEAN?
|
2019-11-18 20:46:38 +03:00
|
|
|
** ?-nofollow BOOLEAN?
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
|
|
|
** This is the main Tcl command. When the "sqlite" Tcl command is
|
|
|
|
** invoked, this routine runs to process that command.
|
|
|
|
**
|
|
|
|
** The first argument, DBNAME, is an arbitrary name for a new
|
|
|
|
** database connection. This command creates a new command named
|
|
|
|
** DBNAME that is used to control that connection. The database
|
|
|
|
** connection is deleted when the DBNAME command is deleted.
|
|
|
|
**
|
2007-08-31 18:31:44 +04:00
|
|
|
** The second argument is the name of the database file.
|
2001-04-06 20:13:42 +04:00
|
|
|
**
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2016-07-28 20:11:20 +03:00
|
|
|
static int SQLITE_TCLAPI DbMain(
|
|
|
|
void *cd,
|
|
|
|
Tcl_Interp *interp,
|
|
|
|
int objc,
|
|
|
|
Tcl_Obj *const*objv
|
|
|
|
){
|
2000-08-04 17:49:02 +04:00
|
|
|
SqliteDb *p;
|
2004-02-01 04:22:50 +03:00
|
|
|
const char *zArg;
|
2000-05-29 18:26:00 +04:00
|
|
|
char *zErrMsg;
|
2007-08-31 18:31:44 +04:00
|
|
|
int i;
|
2018-01-03 16:20:02 +03:00
|
|
|
const char *zFile = 0;
|
2007-08-31 18:31:44 +04:00
|
|
|
const char *zVfs = 0;
|
2009-03-24 18:08:09 +03:00
|
|
|
int flags;
|
2020-05-15 04:18:07 +03:00
|
|
|
int bTranslateFileName = 1;
|
2006-08-24 06:42:27 +04:00
|
|
|
Tcl_DString translatedFilename;
|
2012-09-10 11:29:29 +04:00
|
|
|
int rc;
|
2009-03-24 18:08:09 +03:00
|
|
|
|
|
|
|
/* In normal use, each TCL interpreter runs in a single thread. So
|
2018-01-03 16:20:02 +03:00
|
|
|
** by default, we can turn off mutexing on SQLite database connections.
|
2009-03-24 18:08:09 +03:00
|
|
|
** However, for testing purposes it is useful to have mutexes turned
|
|
|
|
** on. So, by default, mutexes default off. But if compiled with
|
|
|
|
** SQLITE_TCL_DEFAULT_FULLMUTEX then mutexes default on.
|
|
|
|
*/
|
|
|
|
#ifdef SQLITE_TCL_DEFAULT_FULLMUTEX
|
|
|
|
flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
|
|
|
|
#else
|
|
|
|
flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX;
|
|
|
|
#endif
|
|
|
|
|
2018-09-19 18:08:21 +03:00
|
|
|
if( objc==1 ) return sqliteCmdUsage(interp, objv);
|
2004-02-01 04:22:50 +03:00
|
|
|
if( objc==2 ){
|
|
|
|
zArg = Tcl_GetStringFromObj(objv[1], 0);
|
|
|
|
if( strcmp(zArg,"-version")==0 ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp,sqlite3_libversion(), (char*)0);
|
2002-11-04 22:32:25 +03:00
|
|
|
return TCL_OK;
|
|
|
|
}
|
2016-01-07 05:06:55 +03:00
|
|
|
if( strcmp(zArg,"-sourceid")==0 ){
|
|
|
|
Tcl_AppendResult(interp,sqlite3_sourceid(), (char*)0);
|
|
|
|
return TCL_OK;
|
|
|
|
}
|
2004-02-11 05:18:05 +03:00
|
|
|
if( strcmp(zArg,"-has-codec")==0 ){
|
2014-02-07 23:26:13 +04:00
|
|
|
Tcl_AppendResult(interp,"0",(char*)0);
|
2001-04-06 20:13:42 +04:00
|
|
|
return TCL_OK;
|
|
|
|
}
|
2018-01-03 16:20:02 +03:00
|
|
|
if( zArg[0]=='-' ) return sqliteCmdUsage(interp, objv);
|
2001-04-06 20:13:42 +04:00
|
|
|
}
|
2018-01-03 16:20:02 +03:00
|
|
|
for(i=2; i<objc; i++){
|
2007-08-31 18:31:44 +04:00
|
|
|
zArg = Tcl_GetString(objv[i]);
|
2018-01-03 16:20:02 +03:00
|
|
|
if( zArg[0]!='-' ){
|
|
|
|
if( zFile!=0 ) return sqliteCmdUsage(interp, objv);
|
|
|
|
zFile = zArg;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if( i==objc-1 ) return sqliteCmdUsage(interp, objv);
|
|
|
|
i++;
|
2004-02-01 04:22:50 +03:00
|
|
|
if( strcmp(zArg,"-key")==0 ){
|
2020-02-07 04:12:53 +03:00
|
|
|
/* no-op */
|
2007-08-31 18:31:44 +04:00
|
|
|
}else if( strcmp(zArg, "-vfs")==0 ){
|
2018-01-03 16:20:02 +03:00
|
|
|
zVfs = Tcl_GetString(objv[i]);
|
2007-08-31 18:31:44 +04:00
|
|
|
}else if( strcmp(zArg, "-readonly")==0 ){
|
|
|
|
int b;
|
2018-01-03 16:20:02 +03:00
|
|
|
if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR;
|
2007-08-31 18:31:44 +04:00
|
|
|
if( b ){
|
2007-09-03 19:19:34 +04:00
|
|
|
flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
|
2007-08-31 18:31:44 +04:00
|
|
|
flags |= SQLITE_OPEN_READONLY;
|
|
|
|
}else{
|
|
|
|
flags &= ~SQLITE_OPEN_READONLY;
|
|
|
|
flags |= SQLITE_OPEN_READWRITE;
|
|
|
|
}
|
|
|
|
}else if( strcmp(zArg, "-create")==0 ){
|
|
|
|
int b;
|
2018-01-03 16:20:02 +03:00
|
|
|
if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR;
|
2007-09-03 19:19:34 +04:00
|
|
|
if( b && (flags & SQLITE_OPEN_READONLY)==0 ){
|
2007-08-31 18:31:44 +04:00
|
|
|
flags |= SQLITE_OPEN_CREATE;
|
|
|
|
}else{
|
|
|
|
flags &= ~SQLITE_OPEN_CREATE;
|
|
|
|
}
|
2019-11-18 20:46:38 +03:00
|
|
|
}else if( strcmp(zArg, "-nofollow")==0 ){
|
|
|
|
int b;
|
|
|
|
if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR;
|
|
|
|
if( b ){
|
|
|
|
flags |= SQLITE_OPEN_NOFOLLOW;
|
|
|
|
}else{
|
|
|
|
flags &= ~SQLITE_OPEN_NOFOLLOW;
|
|
|
|
}
|
2008-07-10 21:52:49 +04:00
|
|
|
}else if( strcmp(zArg, "-nomutex")==0 ){
|
|
|
|
int b;
|
2018-01-03 16:20:02 +03:00
|
|
|
if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR;
|
2008-07-10 21:52:49 +04:00
|
|
|
if( b ){
|
|
|
|
flags |= SQLITE_OPEN_NOMUTEX;
|
2008-09-03 04:43:15 +04:00
|
|
|
flags &= ~SQLITE_OPEN_FULLMUTEX;
|
2008-07-10 21:52:49 +04:00
|
|
|
}else{
|
|
|
|
flags &= ~SQLITE_OPEN_NOMUTEX;
|
|
|
|
}
|
2011-06-27 20:55:50 +04:00
|
|
|
}else if( strcmp(zArg, "-fullmutex")==0 ){
|
2008-09-03 04:43:15 +04:00
|
|
|
int b;
|
2018-01-03 16:20:02 +03:00
|
|
|
if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR;
|
2008-09-03 04:43:15 +04:00
|
|
|
if( b ){
|
|
|
|
flags |= SQLITE_OPEN_FULLMUTEX;
|
|
|
|
flags &= ~SQLITE_OPEN_NOMUTEX;
|
|
|
|
}else{
|
|
|
|
flags &= ~SQLITE_OPEN_FULLMUTEX;
|
|
|
|
}
|
2011-12-21 18:42:29 +04:00
|
|
|
}else if( strcmp(zArg, "-uri")==0 ){
|
|
|
|
int b;
|
2018-01-03 16:20:02 +03:00
|
|
|
if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR;
|
2011-12-21 18:42:29 +04:00
|
|
|
if( b ){
|
|
|
|
flags |= SQLITE_OPEN_URI;
|
|
|
|
}else{
|
|
|
|
flags &= ~SQLITE_OPEN_URI;
|
|
|
|
}
|
2020-05-15 04:18:07 +03:00
|
|
|
}else if( strcmp(zArg, "-translatefilename")==0 ){
|
|
|
|
if( Tcl_GetBooleanFromObj(interp, objv[i], &bTranslateFileName) ){
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2007-08-31 18:31:44 +04:00
|
|
|
}else{
|
|
|
|
Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0);
|
|
|
|
return TCL_ERROR;
|
2004-02-01 04:22:50 +03:00
|
|
|
}
|
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
zErrMsg = 0;
|
2000-08-04 18:56:24 +04:00
|
|
|
p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
|
2000-08-04 17:49:02 +04:00
|
|
|
memset(p, 0, sizeof(*p));
|
2018-01-03 22:03:31 +03:00
|
|
|
if( zFile==0 ) zFile = "";
|
2020-05-15 04:18:07 +03:00
|
|
|
if( bTranslateFileName ){
|
|
|
|
zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename);
|
|
|
|
}
|
2018-01-03 22:03:31 +03:00
|
|
|
rc = sqlite3_open_v2(zFile, &p->db, flags, zVfs);
|
2020-05-15 04:18:07 +03:00
|
|
|
if( bTranslateFileName ){
|
|
|
|
Tcl_DStringFree(&translatedFilename);
|
|
|
|
}
|
2012-09-10 11:29:29 +04:00
|
|
|
if( p->db ){
|
|
|
|
if( SQLITE_OK!=sqlite3_errcode(p->db) ){
|
|
|
|
zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db));
|
|
|
|
sqlite3_close(p->db);
|
|
|
|
p->db = 0;
|
|
|
|
}
|
|
|
|
}else{
|
2012-09-11 06:00:25 +04:00
|
|
|
zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc));
|
2004-05-22 13:21:21 +04:00
|
|
|
}
|
2000-08-04 17:49:02 +04:00
|
|
|
if( p->db==0 ){
|
2000-05-29 18:26:00 +04:00
|
|
|
Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
|
2000-08-04 17:49:02 +04:00
|
|
|
Tcl_Free((char*)p);
|
2006-12-19 21:46:08 +03:00
|
|
|
sqlite3_free(zErrMsg);
|
2000-05-29 18:26:00 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2005-01-24 03:28:42 +03:00
|
|
|
p->maxStmt = NUM_PREPARED_STMTS;
|
2016-01-23 02:17:51 +03:00
|
|
|
p->openFlags = flags & SQLITE_OPEN_URI;
|
2006-08-24 18:59:45 +04:00
|
|
|
p->interp = interp;
|
2004-02-01 04:22:50 +03:00
|
|
|
zArg = Tcl_GetStringFromObj(objv[1], 0);
|
2009-10-06 18:59:02 +04:00
|
|
|
if( DbUseNre() ){
|
2009-10-13 22:38:34 +04:00
|
|
|
Tcl_NRCreateCommand(interp, zArg, DbObjCmdAdaptor, DbObjCmd,
|
|
|
|
(char*)p, DbDeleteCmd);
|
2009-10-06 18:59:02 +04:00
|
|
|
}else{
|
|
|
|
Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
|
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
return TCL_OK;
|
|
|
|
}
|
|
|
|
|
2001-09-28 21:47:14 +04:00
|
|
|
/*
|
|
|
|
** Provide a dummy Tcl_InitStubs if we are using this as a static
|
|
|
|
** library.
|
|
|
|
*/
|
|
|
|
#ifndef USE_TCL_STUBS
|
|
|
|
# undef Tcl_InitStubs
|
2013-06-03 16:34:46 +04:00
|
|
|
# define Tcl_InitStubs(a,b,c) TCL_VERSION
|
2001-09-28 21:47:14 +04:00
|
|
|
#endif
|
|
|
|
|
2005-10-05 14:40:15 +04:00
|
|
|
/*
|
|
|
|
** Make sure we have a PACKAGE_VERSION macro defined. This will be
|
|
|
|
** defined automatically by the TEA makefile. But other makefiles
|
|
|
|
** do not define it.
|
|
|
|
*/
|
|
|
|
#ifndef PACKAGE_VERSION
|
|
|
|
# define PACKAGE_VERSION SQLITE_VERSION
|
|
|
|
#endif
|
|
|
|
|
2000-05-29 18:26:00 +04:00
|
|
|
/*
|
|
|
|
** Initialize this module.
|
|
|
|
**
|
|
|
|
** This Tcl module contains only a single new Tcl command named "sqlite".
|
|
|
|
** (Hence there is no namespace. There is no point in using a namespace
|
|
|
|
** 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.
|
2010-08-26 20:46:57 +04:00
|
|
|
**
|
|
|
|
** The EXTERN macros are required by TCL in order to work on windows.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2010-08-26 20:46:57 +04:00
|
|
|
EXTERN int Sqlite3_Init(Tcl_Interp *interp){
|
2015-01-12 22:49:46 +03:00
|
|
|
int rc = Tcl_InitStubs(interp, "8.4", 0) ? TCL_OK : TCL_ERROR;
|
2013-05-31 19:36:07 +04:00
|
|
|
if( rc==TCL_OK ){
|
|
|
|
Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
|
2010-08-26 00:35:51 +04:00
|
|
|
#ifndef SQLITE_3_SUFFIX_ONLY
|
2013-05-31 19:36:07 +04:00
|
|
|
/* 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);
|
2010-08-25 23:39:19 +04:00
|
|
|
#endif
|
2013-05-31 19:36:07 +04:00
|
|
|
rc = Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION);
|
|
|
|
}
|
|
|
|
return rc;
|
2001-09-28 21:47:14 +04:00
|
|
|
}
|
2010-08-26 20:46:57 +04:00
|
|
|
EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
|
|
|
|
EXTERN int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
|
|
|
|
EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
|
2008-09-23 13:58:46 +04:00
|
|
|
|
2012-03-20 19:10:42 +04:00
|
|
|
/* Because it accesses the file-system and uses persistent state, SQLite
|
2016-02-13 21:54:10 +03:00
|
|
|
** is not considered appropriate for safe interpreters. Hence, we cause
|
|
|
|
** the _SafeInit() interfaces return TCL_ERROR.
|
2012-03-20 19:10:42 +04:00
|
|
|
*/
|
2016-02-13 21:54:10 +03:00
|
|
|
EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_ERROR; }
|
|
|
|
EXTERN int Sqlite3_SafeUnload(Tcl_Interp *interp, int flags){return TCL_ERROR;}
|
|
|
|
|
|
|
|
|
2005-01-08 21:42:28 +03:00
|
|
|
|
|
|
|
#ifndef SQLITE_3_SUFFIX_ONLY
|
2010-08-20 16:33:59 +04:00
|
|
|
int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
|
|
|
|
int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
|
|
|
|
int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
|
|
|
|
int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
|
2005-01-08 21:42:28 +03:00
|
|
|
#endif
|
2000-05-29 18:26:00 +04:00
|
|
|
|
2009-10-23 00:52:05 +04:00
|
|
|
/*
|
2017-10-13 23:14:06 +03:00
|
|
|
** If the TCLSH macro is defined, add code to make a stand-alone program.
|
2009-10-23 00:52:05 +04:00
|
|
|
*/
|
2017-10-13 23:14:06 +03:00
|
|
|
#if defined(TCLSH)
|
2000-05-30 00:41:49 +04:00
|
|
|
|
2017-10-13 23:14:06 +03:00
|
|
|
/* This is the main routine for an ordinary TCL shell. If there are
|
|
|
|
** are arguments, run the first argument as a script. Otherwise,
|
|
|
|
** read TCL commands from standard input
|
2000-05-30 00:41:49 +04:00
|
|
|
*/
|
2011-09-21 20:43:07 +04:00
|
|
|
static const char *tclsh_main_loop(void){
|
|
|
|
static const char zMainloop[] =
|
2017-10-13 23:14:06 +03:00
|
|
|
"if {[llength $argv]>=1} {\n"
|
|
|
|
"set argv0 [lindex $argv 0]\n"
|
|
|
|
"set argv [lrange $argv 1 end]\n"
|
|
|
|
"source $argv0\n"
|
|
|
|
"} else {\n"
|
|
|
|
"set line {}\n"
|
|
|
|
"while {![eof stdin]} {\n"
|
|
|
|
"if {$line!=\"\"} {\n"
|
|
|
|
"puts -nonewline \"> \"\n"
|
|
|
|
"} else {\n"
|
|
|
|
"puts -nonewline \"% \"\n"
|
|
|
|
"}\n"
|
|
|
|
"flush stdout\n"
|
|
|
|
"append line [gets stdin]\n"
|
|
|
|
"if {[info complete $line]} {\n"
|
|
|
|
"if {[catch {uplevel #0 $line} result]} {\n"
|
|
|
|
"puts stderr \"Error: $result\"\n"
|
|
|
|
"} elseif {$result!=\"\"} {\n"
|
|
|
|
"puts $result\n"
|
|
|
|
"}\n"
|
|
|
|
"set line {}\n"
|
|
|
|
"} else {\n"
|
|
|
|
"append line \\n\n"
|
2011-09-21 20:43:07 +04:00
|
|
|
"}\n"
|
2000-05-30 00:41:49 +04:00
|
|
|
"}\n"
|
|
|
|
"}\n"
|
2011-09-21 20:43:07 +04:00
|
|
|
;
|
|
|
|
return zMainloop;
|
|
|
|
}
|
2004-07-23 04:01:38 +04:00
|
|
|
|
2010-06-07 18:28:16 +04:00
|
|
|
#define TCLSH_MAIN main /* Needed to fake out mktclapp */
|
2016-07-28 07:14:37 +03:00
|
|
|
int SQLITE_CDECL TCLSH_MAIN(int argc, char **argv){
|
2010-06-07 18:28:16 +04:00
|
|
|
Tcl_Interp *interp;
|
2017-10-13 23:14:06 +03:00
|
|
|
int i;
|
|
|
|
const char *zScript = 0;
|
|
|
|
char zArgc[32];
|
|
|
|
#if defined(TCLSH_INIT_PROC)
|
|
|
|
extern const char *TCLSH_INIT_PROC(Tcl_Interp*);
|
|
|
|
#endif
|
2013-08-15 12:06:15 +04:00
|
|
|
|
|
|
|
#if !defined(_WIN32_WCE)
|
2018-07-22 09:25:35 +03:00
|
|
|
if( getenv("SQLITE_DEBUG_BREAK") ){
|
|
|
|
if( isatty(0) && isatty(2) ){
|
|
|
|
fprintf(stderr,
|
|
|
|
"attach debugger to process %d and press any key to continue.\n",
|
|
|
|
GETPID());
|
|
|
|
fgetc(stdin);
|
|
|
|
}else{
|
|
|
|
#if defined(_WIN32) || defined(WIN32)
|
|
|
|
DebugBreak();
|
|
|
|
#elif defined(SIGTRAP)
|
|
|
|
raise(SIGTRAP);
|
|
|
|
#endif
|
|
|
|
}
|
2013-08-15 12:06:15 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-06-07 18:28:16 +04:00
|
|
|
/* 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();
|
|
|
|
|
2011-09-21 20:43:07 +04:00
|
|
|
Tcl_FindExecutable(argv[0]);
|
2014-02-14 04:25:03 +04:00
|
|
|
Tcl_SetSystemEncoding(NULL, "utf-8");
|
2011-09-21 20:43:07 +04:00
|
|
|
interp = Tcl_CreateInterp();
|
2017-10-13 23:14:06 +03:00
|
|
|
Sqlite3_Init(interp);
|
2011-09-21 20:43:07 +04:00
|
|
|
|
2017-10-13 23:14:06 +03:00
|
|
|
sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-1);
|
|
|
|
Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY);
|
|
|
|
Tcl_SetVar(interp,"argv0",argv[0],TCL_GLOBAL_ONLY);
|
|
|
|
Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
|
|
|
|
for(i=1; i<argc; i++){
|
|
|
|
Tcl_SetVar(interp, "argv", argv[i],
|
|
|
|
TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
|
|
|
|
}
|
|
|
|
#if defined(TCLSH_INIT_PROC)
|
|
|
|
zScript = TCLSH_INIT_PROC(interp);
|
2010-07-12 20:47:48 +04:00
|
|
|
#endif
|
2017-10-13 23:14:06 +03:00
|
|
|
if( zScript==0 ){
|
|
|
|
zScript = tclsh_main_loop();
|
2017-10-13 18:06:06 +03:00
|
|
|
}
|
2017-10-13 23:14:06 +03:00
|
|
|
if( Tcl_GlobalEval(interp, zScript)!=TCL_OK ){
|
|
|
|
const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
|
|
|
|
if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp);
|
|
|
|
fprintf(stderr,"%s: %s\n", *argv, zInfo);
|
|
|
|
return 1;
|
2000-05-30 00:41:49 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* TCLSH */
|