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
|
|
|
**
|
|
|
|
*************************************************************************
|
|
|
|
** A TCL Interface to SQLite
|
|
|
|
**
|
2004-11-12 18:53:37 +03:00
|
|
|
** $Id: tclsqlite.c,v 1.107 2004/11/12 15:53:37 danielk1977 Exp $
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2000-09-21 17:01:35 +04:00
|
|
|
#ifndef NO_TCL /* Omit this whole file if TCL is unavailable */
|
|
|
|
|
2002-06-27 00:06:05 +04:00
|
|
|
#include "sqliteInt.h"
|
2004-08-20 20:02:39 +04:00
|
|
|
#include "hash.h"
|
2001-01-31 16:28:08 +03:00
|
|
|
#include "tcl.h"
|
2000-05-29 18:26:00 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-11-09 16:41:09 +03:00
|
|
|
#include <assert.h>
|
2000-05-29 18:26:00 +04:00
|
|
|
|
2001-10-18 16:34:46 +04:00
|
|
|
/*
|
|
|
|
** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
|
|
|
|
** have to do a translation when going between the two. Set the
|
|
|
|
** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
|
|
|
|
** this translation.
|
|
|
|
*/
|
|
|
|
#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
|
|
|
|
# define UTF_TRANSLATION_NEEDED 1
|
|
|
|
#endif
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
typedef struct SqlFunc SqlFunc;
|
|
|
|
struct SqlFunc {
|
|
|
|
Tcl_Interp *interp; /* The TCL interpret to execute the function */
|
|
|
|
char *zScript; /* The script to be run */
|
|
|
|
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 */
|
|
|
|
SqlCollate *pNext; /* Next function on the list of them all */
|
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
typedef struct SqliteDb SqliteDb;
|
|
|
|
struct SqliteDb {
|
2004-08-20 20:02:39 +04:00
|
|
|
sqlite3 *db; /* The "real" database structure */
|
2000-08-04 17:49:02 +04:00
|
|
|
Tcl_Interp *interp; /* The interpreter used for this database */
|
2000-09-21 17:01:35 +04:00
|
|
|
char *zBusy; /* The busy callback routine */
|
2004-01-15 05:44:03 +03:00
|
|
|
char *zCommit; /* The commit hook callback routine */
|
2003-04-23 16:25:23 +04:00
|
|
|
char *zTrace; /* The trace callback routine */
|
2003-10-18 13:37:26 +04:00
|
|
|
char *zProgress; /* The progress callback routine */
|
2003-04-23 00:30:37 +04:00
|
|
|
char *zAuth; /* The authorization callback routine */
|
2002-09-14 17:47:32 +04:00
|
|
|
SqlFunc *pFunc; /* List of SQL functions */
|
2004-06-09 13:55:16 +04:00
|
|
|
SqlCollate *pCollate; /* List of SQL collation functions */
|
2004-05-10 14:34:51 +04:00
|
|
|
int rc; /* Return code of most recent sqlite3_exec() */
|
2004-06-10 14:50:08 +04:00
|
|
|
Tcl_Obj *pCollateNeeded; /* Collation needed script */
|
2001-10-18 16:34:46 +04:00
|
|
|
};
|
2001-04-05 19:57:13 +04: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
|
|
|
*/
|
|
|
|
static void DbDeleteCmd(void *db){
|
2000-08-04 17:49:02 +04:00
|
|
|
SqliteDb *pDb = (SqliteDb*)db;
|
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;
|
|
|
|
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
|
|
|
}
|
2003-04-23 00:30:37 +04:00
|
|
|
if( pDb->zAuth ){
|
|
|
|
Tcl_Free(pDb->zAuth);
|
|
|
|
}
|
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];
|
|
|
|
char *zCmd;
|
|
|
|
Tcl_DString cmd;
|
|
|
|
|
|
|
|
Tcl_DStringInit(&cmd);
|
|
|
|
Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
|
2004-06-12 05:43:26 +04:00
|
|
|
sprintf(zVal, "%d", nTries);
|
|
|
|
Tcl_DStringAppendElement(&cmd, zVal);
|
2000-08-04 17:49:02 +04:00
|
|
|
zCmd = Tcl_DStringValue(&cmd);
|
|
|
|
rc = Tcl_Eval(pDb->interp, zCmd);
|
|
|
|
Tcl_DStringFree(&cmd);
|
|
|
|
if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
Tcl_EvalObjEx(p->interp, pCmd, 0);
|
|
|
|
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.
|
|
|
|
*/
|
2004-05-25 16:05:56 +04: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);
|
2002-09-14 17:47:32 +04:00
|
|
|
Tcl_DString cmd;
|
|
|
|
int i;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
Tcl_DStringInit(&cmd);
|
|
|
|
Tcl_DStringAppend(&cmd, p->zScript, -1);
|
|
|
|
for(i=0; i<argc; i++){
|
2004-05-31 22:51:57 +04:00
|
|
|
if( SQLITE_NULL==sqlite3_value_type(argv[i]) ){
|
2004-05-24 16:39:02 +04:00
|
|
|
Tcl_DStringAppendElement(&cmd, "");
|
|
|
|
}else{
|
2004-05-27 03:25:30 +04:00
|
|
|
Tcl_DStringAppendElement(&cmd, sqlite3_value_text(argv[i]));
|
2004-05-24 16:39:02 +04:00
|
|
|
}
|
2002-09-14 17:47:32 +04:00
|
|
|
}
|
|
|
|
rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
|
|
|
|
if( rc ){
|
2004-05-25 15:47:24 +04:00
|
|
|
sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
|
2002-09-14 17:47:32 +04:00
|
|
|
}else{
|
2004-06-12 13:25:12 +04:00
|
|
|
sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1,
|
|
|
|
SQLITE_TRANSIENT);
|
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
|
|
|
|
){
|
|
|
|
char *zCode;
|
|
|
|
Tcl_DString str;
|
|
|
|
int rc;
|
|
|
|
const char *zReply;
|
|
|
|
SqliteDb *pDb = (SqliteDb*)pArg;
|
|
|
|
|
|
|
|
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;
|
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 : "");
|
|
|
|
rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
|
|
|
|
Tcl_DStringFree(&str);
|
|
|
|
zReply = Tcl_GetStringResult(pDb->interp);
|
|
|
|
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-05-29 06:37:19 +04:00
|
|
|
/*
|
|
|
|
** zText is a pointer to text obtained via an sqlite3_result_text()
|
|
|
|
** or similar interface. This routine returns a Tcl string object,
|
|
|
|
** reference count set to 0, containing the text. If a translation
|
|
|
|
** between iso8859 and UTF-8 is required, it is preformed.
|
|
|
|
*/
|
|
|
|
static Tcl_Obj *dbTextToObj(char const *zText){
|
|
|
|
Tcl_Obj *pVal;
|
|
|
|
#ifdef UTF_TRANSLATION_NEEDED
|
|
|
|
Tcl_DString dCol;
|
|
|
|
Tcl_DStringInit(&dCol);
|
|
|
|
Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol);
|
|
|
|
pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
|
|
|
|
Tcl_DStringFree(&dCol);
|
|
|
|
#else
|
|
|
|
pVal = Tcl_NewStringObj(zText, -1);
|
|
|
|
#endif
|
|
|
|
return pVal;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2000-09-21 17:01:35 +04:00
|
|
|
static int 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[] = {
|
2004-06-29 16:39:08 +04:00
|
|
|
"authorizer", "busy", "changes",
|
|
|
|
"close", "collate", "collation_needed",
|
|
|
|
"commit_hook", "complete", "errorcode",
|
|
|
|
"eval", "function", "last_insert_rowid",
|
|
|
|
"onecolumn", "progress", "rekey",
|
|
|
|
"timeout", "total_changes", "trace",
|
|
|
|
0
|
2000-09-21 17:01:35 +04:00
|
|
|
};
|
2002-06-25 23:31:18 +04:00
|
|
|
enum DB_enum {
|
2004-06-29 16:39:08 +04:00
|
|
|
DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
|
|
|
|
DB_CLOSE, DB_COLLATE, DB_COLLATION_NEEDED,
|
|
|
|
DB_COMMIT_HOOK, DB_COMPLETE, DB_ERRORCODE,
|
|
|
|
DB_EVAL, DB_FUNCTION, DB_LAST_INSERT_ROWID,
|
|
|
|
DB_ONECOLUMN, DB_PROGRESS, DB_REKEY,
|
|
|
|
DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE,
|
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
|
|
|
|
Tcl_AppendResult(interp, "authorization not available in this build", 0);
|
|
|
|
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 ){
|
2003-04-23 00:30:37 +04:00
|
|
|
Tcl_AppendResult(interp, pDb->zAuth, 0);
|
|
|
|
}
|
|
|
|
}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 );
|
|
|
|
strcpy(pDb->zAuth, zAuth);
|
|
|
|
}else{
|
|
|
|
pDb->zAuth = 0;
|
|
|
|
}
|
|
|
|
if( pDb->zAuth ){
|
|
|
|
pDb->interp = interp;
|
2004-05-10 14:34:51 +04:00
|
|
|
sqlite3_set_authorizer(pDb->db, 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;
|
|
|
|
}
|
|
|
|
|
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 ){
|
|
|
|
Tcl_AppendResult(interp, pDb->zBusy, 0);
|
|
|
|
}
|
|
|
|
}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 );
|
|
|
|
strcpy(pDb->zBusy, zBusy);
|
|
|
|
}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
|
|
|
|
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
|
2004-06-21 10:50:26 +04:00
|
|
|
** the most recent INSERT, UPDATE or DELETE statement, not including
|
|
|
|
** 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);
|
2004-06-21 10:50:26 +04:00
|
|
|
Tcl_SetIntObj(pResult, sqlite3_changes(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-01-15 05:44:03 +03: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?");
|
2004-06-29 16:39:08 +04:00
|
|
|
return TCL_ERROR;
|
2004-01-15 05:44:03 +03:00
|
|
|
}else if( objc==2 ){
|
|
|
|
if( pDb->zCommit ){
|
|
|
|
Tcl_AppendResult(interp, pDb->zCommit, 0);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
char *zCommit;
|
|
|
|
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 );
|
|
|
|
strcpy(pDb->zCommit, zCommit);
|
|
|
|
}else{
|
|
|
|
pDb->zCommit = 0;
|
|
|
|
}
|
|
|
|
if( pDb->zCommit ){
|
|
|
|
pDb->interp = interp;
|
2004-05-10 14:34:51 +04:00
|
|
|
sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
|
2004-01-15 05:44:03 +03:00
|
|
|
}else{
|
2004-05-10 14:34:51 +04:00
|
|
|
sqlite3_commit_hook(pDb->db, 0, 0);
|
2004-01-15 05:44:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
strcpy(pCollate->zScript, zScript);
|
|
|
|
if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
|
|
|
|
pCollate, tclSqlCollate) ){
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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: {
|
|
|
|
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);
|
|
|
|
break;
|
|
|
|
}
|
2003-01-31 20:21:49 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** $db errorcode
|
|
|
|
**
|
|
|
|
** Return the numeric error code that was returned by the most recent
|
2004-05-10 14:34:51 +04:00
|
|
|
** call to sqlite3_exec().
|
2003-01-31 20:21:49 +03:00
|
|
|
*/
|
|
|
|
case DB_ERRORCODE: {
|
2004-06-14 15:43:46 +04:00
|
|
|
Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
|
2003-01-31 20:21:49 +03:00
|
|
|
break;
|
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
|
|
|
|
/*
|
2004-08-20 20:02:39 +04:00
|
|
|
** $db eval $sql ?array? ?{ ...code... }?
|
2004-09-07 17:20:35 +04:00
|
|
|
** $db onecolumn $sql
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
|
|
|
** The SQL statement in $sql is evaluated. For each row, the values are
|
2000-08-04 17:49:02 +04:00
|
|
|
** placed in elements of the array named "array" and ...code... is executed.
|
2000-05-29 18:26:00 +04:00
|
|
|
** 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.
|
2004-09-07 17:20:35 +04:00
|
|
|
**
|
|
|
|
** The onecolumn method is the equivalent of:
|
|
|
|
** lindex [$db eval $sql] 0
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2004-09-07 17:20:35 +04:00
|
|
|
case DB_ONECOLUMN:
|
2004-05-27 16:11:31 +04:00
|
|
|
case DB_EVAL: {
|
2004-08-24 19:23:34 +04:00
|
|
|
char const *zSql; /* Next SQL statement to execute */
|
|
|
|
char const *zLeft; /* What is left after first stmt in zSql */
|
|
|
|
sqlite3_stmt *pStmt; /* Compiled SQL statment */
|
2004-08-20 22:34:20 +04:00
|
|
|
Tcl_Obj *pArray; /* Name of array into which results are written */
|
|
|
|
Tcl_Obj *pScript; /* Script to run for each result set */
|
2004-08-26 04:56:05 +04:00
|
|
|
Tcl_Obj **apParm; /* Parameters that need a Tcl_DecrRefCount() */
|
|
|
|
int nParm; /* Number of entries used in apParm[] */
|
|
|
|
Tcl_Obj *aParm[10]; /* Static space for apParm[] in the common case */
|
2004-09-07 17:20:35 +04:00
|
|
|
Tcl_Obj *pRet; /* Value to be returned */
|
2004-05-29 06:37:19 +04:00
|
|
|
|
2004-09-07 17:20:35 +04:00
|
|
|
if( choice==DB_ONECOLUMN ){
|
|
|
|
if( objc!=3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "SQL");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
pRet = 0;
|
|
|
|
}else{
|
|
|
|
if( objc<3 || objc>5 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
pRet = Tcl_NewObj();
|
|
|
|
Tcl_IncrRefCount(pRet);
|
2004-05-27 16:11:31 +04:00
|
|
|
}
|
2004-08-20 22:34:20 +04:00
|
|
|
if( objc==3 ){
|
|
|
|
pArray = pScript = 0;
|
|
|
|
}else if( objc==4 ){
|
|
|
|
pArray = 0;
|
|
|
|
pScript = objv[3];
|
|
|
|
}else{
|
|
|
|
pArray = objv[3];
|
|
|
|
if( Tcl_GetString(pArray)[0]==0 ) pArray = 0;
|
|
|
|
pScript = objv[4];
|
|
|
|
}
|
2004-05-27 16:11:31 +04:00
|
|
|
|
2004-08-26 04:56:05 +04:00
|
|
|
Tcl_IncrRefCount(objv[2]);
|
2004-05-27 16:11:31 +04:00
|
|
|
zSql = Tcl_GetStringFromObj(objv[2], 0);
|
2004-09-13 17:16:31 +04:00
|
|
|
while( rc==TCL_OK && zSql[0] ){
|
2004-08-20 22:34:20 +04:00
|
|
|
int i; /* Loop counter */
|
|
|
|
int nVar; /* Number of wildcards in the SQL */
|
|
|
|
int nCol; /* Number of columns in the result set */
|
|
|
|
Tcl_Obj **apColName = 0; /* Array of column names */
|
2004-05-27 16:11:31 +04:00
|
|
|
|
2004-08-20 22:34:20 +04:00
|
|
|
/* Compile a single SQL statement */
|
2004-05-27 16:11:31 +04:00
|
|
|
if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
|
2004-05-29 06:37:19 +04:00
|
|
|
Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
|
2004-05-27 16:11:31 +04:00
|
|
|
rc = TCL_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2004-08-20 22:34:20 +04:00
|
|
|
if( pStmt==0 ){
|
|
|
|
if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
|
|
|
|
Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
|
|
|
|
rc = TCL_ERROR;
|
|
|
|
break;
|
|
|
|
}else{
|
|
|
|
zSql = zLeft;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-26 04:56:05 +04:00
|
|
|
/* Bind values to wildcards that begin with $ or : */
|
2004-08-20 22:34:20 +04:00
|
|
|
nVar = sqlite3_bind_parameter_count(pStmt);
|
2004-08-26 04:56:05 +04:00
|
|
|
nParm = 0;
|
|
|
|
if( nVar>sizeof(aParm)/sizeof(aParm[0]) ){
|
|
|
|
apParm = (Tcl_Obj**)Tcl_Alloc(nVar*sizeof(apParm[0]));
|
|
|
|
}else{
|
|
|
|
apParm = aParm;
|
|
|
|
}
|
2004-08-20 22:34:20 +04:00
|
|
|
for(i=1; i<=nVar; i++){
|
|
|
|
const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
|
2004-08-25 08:07:01 +04:00
|
|
|
if( zVar[0]=='$' || zVar[0]==':' ){
|
2004-08-20 22:34:20 +04:00
|
|
|
Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0);
|
|
|
|
if( pVar ){
|
|
|
|
int n;
|
|
|
|
u8 *data;
|
|
|
|
char *zType = pVar->typePtr ? pVar->typePtr->name : "";
|
|
|
|
char c = zType[0];
|
|
|
|
if( c=='b' && strcmp(zType,"bytearray")==0 ){
|
|
|
|
data = Tcl_GetByteArrayFromObj(pVar, &n);
|
|
|
|
sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC);
|
2004-08-26 04:56:05 +04:00
|
|
|
Tcl_IncrRefCount(pVar);
|
|
|
|
apParm[nParm++] = pVar;
|
2004-08-20 22:34:20 +04:00
|
|
|
}else if( (c=='b' && strcmp(zType,"boolean")==0) ||
|
|
|
|
(c=='i' && strcmp(zType,"int")==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{
|
|
|
|
data = Tcl_GetStringFromObj(pVar, &n);
|
|
|
|
sqlite3_bind_text(pStmt, i, data, n, SQLITE_STATIC);
|
2004-08-26 04:56:05 +04:00
|
|
|
Tcl_IncrRefCount(pVar);
|
|
|
|
apParm[nParm++] = pVar;
|
2004-08-20 22:34:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute column names */
|
|
|
|
nCol = sqlite3_column_count(pStmt);
|
|
|
|
if( pScript ){
|
|
|
|
apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol );
|
|
|
|
if( apColName==0 ) break;
|
|
|
|
for(i=0; i<nCol; i++){
|
|
|
|
apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i));
|
|
|
|
Tcl_IncrRefCount(apColName[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If results are being stored in an array variable, then create
|
|
|
|
** the array(*) entry for that array
|
|
|
|
*/
|
|
|
|
if( pArray ){
|
2004-05-27 16:11:31 +04:00
|
|
|
Tcl_Obj *pColList = Tcl_NewObj();
|
|
|
|
Tcl_IncrRefCount(pColList);
|
2004-08-20 22:34:20 +04:00
|
|
|
for(i=0; i<nCol; i++){
|
|
|
|
Tcl_ListObjAppendElement(interp, pColList, apColName[i]);
|
2004-05-27 16:11:31 +04:00
|
|
|
}
|
2004-08-20 22:34:20 +04:00
|
|
|
Tcl_ObjSetVar2(interp, pArray, Tcl_NewStringObj("*",-1), pColList,0);
|
2004-05-27 16:11:31 +04:00
|
|
|
}
|
|
|
|
|
2004-08-20 22:34:20 +04:00
|
|
|
/* Execute the SQL
|
|
|
|
*/
|
2004-09-13 17:16:31 +04:00
|
|
|
while( rc==TCL_OK && pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
|
2004-08-20 22:34:20 +04:00
|
|
|
for(i=0; i<nCol; i++){
|
2004-05-27 16:11:31 +04:00
|
|
|
Tcl_Obj *pVal;
|
|
|
|
|
|
|
|
/* Set pVal to contain the i'th column of this row. */
|
2004-08-20 22:34:20 +04:00
|
|
|
switch( sqlite3_column_type(pStmt, i) ){
|
|
|
|
case SQLITE_BLOB: {
|
|
|
|
int bytes = sqlite3_column_bytes(pStmt, i);
|
|
|
|
pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_INTEGER: {
|
|
|
|
sqlite_int64 v = sqlite3_column_int64(pStmt, i);
|
|
|
|
if( v>=-2147483647 && v<=2147483647 ){
|
|
|
|
pVal = Tcl_NewIntObj(v);
|
|
|
|
}else{
|
|
|
|
pVal = Tcl_NewWideIntObj(v);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_FLOAT: {
|
|
|
|
double r = sqlite3_column_double(pStmt, i);
|
|
|
|
pVal = Tcl_NewDoubleObj(r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
pVal = dbTextToObj(sqlite3_column_text(pStmt, i));
|
|
|
|
break;
|
|
|
|
}
|
2004-05-27 16:11:31 +04:00
|
|
|
}
|
|
|
|
|
2004-08-20 22:34:20 +04:00
|
|
|
if( pScript ){
|
|
|
|
if( pArray==0 ){
|
|
|
|
Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0);
|
2004-05-27 16:11:31 +04:00
|
|
|
}else{
|
2004-08-20 22:34:20 +04:00
|
|
|
Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0);
|
2004-05-27 16:11:31 +04:00
|
|
|
}
|
2004-09-07 17:20:35 +04:00
|
|
|
}else if( choice==DB_ONECOLUMN ){
|
|
|
|
if( pRet==0 ){
|
|
|
|
pRet = pVal;
|
|
|
|
Tcl_IncrRefCount(pRet);
|
|
|
|
}
|
2004-09-13 17:16:31 +04:00
|
|
|
rc = TCL_BREAK;
|
2004-05-27 16:11:31 +04:00
|
|
|
}else{
|
|
|
|
Tcl_ListObjAppendElement(interp, pRet, pVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-20 22:34:20 +04:00
|
|
|
if( pScript ){
|
|
|
|
rc = Tcl_EvalObjEx(interp, pScript, 0);
|
2004-09-13 17:16:31 +04:00
|
|
|
if( rc==TCL_CONTINUE ){
|
|
|
|
rc = TCL_OK;
|
|
|
|
}
|
2004-05-27 16:11:31 +04:00
|
|
|
}
|
|
|
|
}
|
2004-09-13 17:16:31 +04:00
|
|
|
if( rc==TCL_BREAK ){
|
|
|
|
rc = TCL_OK;
|
|
|
|
}
|
2004-08-20 22:34:20 +04:00
|
|
|
|
|
|
|
/* Free the column name objects */
|
|
|
|
if( pScript ){
|
|
|
|
for(i=0; i<nCol; i++){
|
|
|
|
Tcl_DecrRefCount(apColName[i]);
|
|
|
|
}
|
|
|
|
Tcl_Free((char*)apColName);
|
|
|
|
}
|
|
|
|
|
2004-08-26 04:56:05 +04:00
|
|
|
/* Free the bound string and blob parameters */
|
|
|
|
for(i=0; i<nParm; i++){
|
|
|
|
Tcl_DecrRefCount(apParm[i]);
|
|
|
|
}
|
|
|
|
if( apParm!=aParm ){
|
|
|
|
Tcl_Free((char*)apParm);
|
|
|
|
}
|
|
|
|
|
2004-08-20 22:34:20 +04:00
|
|
|
/* Finalize the statement. If the result code is SQLITE_SCHEMA, then
|
|
|
|
** try again to execute the same statement
|
|
|
|
*/
|
|
|
|
if( SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){
|
2004-05-27 16:11:31 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-08-20 22:34:20 +04:00
|
|
|
if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
|
2004-05-29 06:37:19 +04:00
|
|
|
Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
|
2004-05-27 16:11:31 +04:00
|
|
|
rc = TCL_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
zSql = zLeft;
|
|
|
|
}
|
2004-08-26 04:56:05 +04:00
|
|
|
Tcl_DecrRefCount(objv[2]);
|
2004-05-27 16:11:31 +04:00
|
|
|
|
2004-09-07 17:20:35 +04:00
|
|
|
if( pRet ){
|
|
|
|
if( rc==TCL_OK ){
|
|
|
|
Tcl_SetObjResult(interp, pRet);
|
|
|
|
}
|
|
|
|
Tcl_DecrRefCount(pRet);
|
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
|
|
|
/*
|
|
|
|
** $db function NAME SCRIPT
|
|
|
|
**
|
|
|
|
** Create a new SQL function called NAME. Whenever that function is
|
|
|
|
** called, invoke SCRIPT to evaluate the function.
|
|
|
|
*/
|
|
|
|
case DB_FUNCTION: {
|
|
|
|
SqlFunc *pFunc;
|
|
|
|
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);
|
|
|
|
pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
|
|
|
|
if( pFunc==0 ) return TCL_ERROR;
|
|
|
|
pFunc->interp = interp;
|
|
|
|
pFunc->pNext = pDb->pFunc;
|
|
|
|
pFunc->zScript = (char*)&pFunc[1];
|
2004-06-29 16:39:08 +04:00
|
|
|
pDb->pFunc = pFunc;
|
2002-09-14 17:47:32 +04:00
|
|
|
strcpy(pFunc->zScript, zScript);
|
2004-06-29 17:41:21 +04:00
|
|
|
rc = sqlite3_create_function(pDb->db, zName, -1, SQLITE_UTF8,
|
2004-06-12 13:25:12 +04:00
|
|
|
pFunc, tclSqlFunc, 0, 0);
|
2004-06-29 17:41:21 +04:00
|
|
|
if( rc!=SQLITE_OK ) rc = TCL_ERROR;
|
2002-09-14 17:47:32 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-01-17 00:00:27 +03:00
|
|
|
/*
|
|
|
|
** $db last_insert_rowid
|
|
|
|
**
|
|
|
|
** Return an integer which is the ROWID for the most recent insert.
|
|
|
|
*/
|
|
|
|
case DB_LAST_INSERT_ROWID: {
|
|
|
|
Tcl_Obj *pResult;
|
|
|
|
int rowid;
|
|
|
|
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);
|
|
|
|
Tcl_SetIntObj(pResult, rowid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-08-19 18:31:01 +04:00
|
|
|
/*
|
2004-09-07 17:20:35 +04:00
|
|
|
** The DB_ONECOLUMN method is implemented together with DB_EVAL.
|
2003-08-19 18:31:01 +04:00
|
|
|
*/
|
2004-09-07 17:20:35 +04:00
|
|
|
|
|
|
|
/* $db progress ?N CALLBACK?
|
|
|
|
**
|
|
|
|
** Invoke the given callback every N virtual machine opcodes while executing
|
|
|
|
** queries.
|
|
|
|
*/
|
|
|
|
case DB_PROGRESS: {
|
|
|
|
if( objc==2 ){
|
|
|
|
if( pDb->zProgress ){
|
|
|
|
Tcl_AppendResult(interp, pDb->zProgress, 0);
|
|
|
|
}
|
|
|
|
}else if( objc==4 ){
|
|
|
|
char *zProgress;
|
|
|
|
int len;
|
|
|
|
int N;
|
|
|
|
if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
|
|
|
|
return TCL_ERROR;
|
|
|
|
};
|
|
|
|
if( pDb->zProgress ){
|
|
|
|
Tcl_Free(pDb->zProgress);
|
|
|
|
}
|
|
|
|
zProgress = Tcl_GetStringFromObj(objv[3], &len);
|
|
|
|
if( zProgress && len>0 ){
|
|
|
|
pDb->zProgress = Tcl_Alloc( len + 1 );
|
|
|
|
strcpy(pDb->zProgress, zProgress);
|
|
|
|
}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;
|
|
|
|
}
|
|
|
|
|
2004-02-01 04:22:50 +03:00
|
|
|
/*
|
|
|
|
** $db rekey KEY
|
|
|
|
**
|
|
|
|
** Change the encryption key on the currently open database.
|
|
|
|
*/
|
|
|
|
case DB_REKEY: {
|
|
|
|
int nKey;
|
|
|
|
void *pKey;
|
|
|
|
if( objc!=3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "KEY");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
|
2004-02-11 05:18:05 +03:00
|
|
|
#ifdef SQLITE_HAS_CODEC
|
2004-07-22 06:40:37 +04:00
|
|
|
rc = sqlite3_rekey(pDb->db, pKey, nKey);
|
2004-02-01 04:22:50 +03:00
|
|
|
if( rc ){
|
2004-06-01 03:56:42 +04:00
|
|
|
Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
|
2004-02-01 04:22:50 +03:00
|
|
|
rc = TCL_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2003-04-23 16:25:23 +04:00
|
|
|
|
2004-06-29 16:39:08 +04:00
|
|
|
/*
|
|
|
|
** $db total_changes
|
|
|
|
**
|
|
|
|
** Return the number of rows that were modified, inserted, or deleted
|
|
|
|
** 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);
|
|
|
|
Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db));
|
|
|
|
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 ){
|
|
|
|
Tcl_AppendResult(interp, pDb->zTrace, 0);
|
|
|
|
}
|
|
|
|
}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 );
|
|
|
|
strcpy(pDb->zTrace, zTrace);
|
|
|
|
}else{
|
|
|
|
pDb->zTrace = 0;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-09-06 21:24:11 +04:00
|
|
|
** sqlite3 DBNAME FILENAME ?MODE? ?-key KEY?
|
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.
|
|
|
|
**
|
|
|
|
** The second argument is the name of the directory that contains
|
|
|
|
** the sqlite database that is to be accessed.
|
2001-04-06 20:13:42 +04:00
|
|
|
**
|
|
|
|
** For testing purposes, we also support the following:
|
|
|
|
**
|
2004-09-06 21:24:11 +04:00
|
|
|
** sqlite3 -encoding
|
2001-04-06 20:13:42 +04:00
|
|
|
**
|
|
|
|
** Return the encoding used by LIKE and GLOB operators. Choices
|
|
|
|
** are UTF-8 and iso8859.
|
|
|
|
**
|
2004-09-06 21:24:11 +04:00
|
|
|
** sqlite3 -version
|
2002-11-04 22:32:25 +03:00
|
|
|
**
|
|
|
|
** Return the version number of the SQLite library.
|
|
|
|
**
|
2004-09-06 21:24:11 +04:00
|
|
|
** sqlite3 -tcl-uses-utf
|
2001-04-06 20:13:42 +04:00
|
|
|
**
|
|
|
|
** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
|
|
|
|
** not. Used by tests to make sure the library was compiled
|
|
|
|
** correctly.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2004-02-01 04:22:50 +03:00
|
|
|
static int 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
|
|
|
void *pKey = 0;
|
|
|
|
int nKey = 0;
|
|
|
|
const char *zArg;
|
2000-05-29 18:26:00 +04:00
|
|
|
char *zErrMsg;
|
2004-02-01 04:22:50 +03:00
|
|
|
const char *zFile;
|
2002-06-27 00:06:05 +04:00
|
|
|
char zBuf[80];
|
2004-02-01 04:22:50 +03:00
|
|
|
if( objc==2 ){
|
|
|
|
zArg = Tcl_GetStringFromObj(objv[1], 0);
|
|
|
|
if( strcmp(zArg,"-version")==0 ){
|
2004-05-10 14:34:51 +04:00
|
|
|
Tcl_AppendResult(interp,sqlite3_version,0);
|
2002-11-04 22:32:25 +03:00
|
|
|
return TCL_OK;
|
|
|
|
}
|
2004-02-11 05:18:05 +03:00
|
|
|
if( strcmp(zArg,"-has-codec")==0 ){
|
|
|
|
#ifdef SQLITE_HAS_CODEC
|
2004-02-01 04:22:50 +03:00
|
|
|
Tcl_AppendResult(interp,"1",0);
|
|
|
|
#else
|
|
|
|
Tcl_AppendResult(interp,"0",0);
|
|
|
|
#endif
|
|
|
|
return TCL_OK;
|
|
|
|
}
|
|
|
|
if( strcmp(zArg,"-tcl-uses-utf")==0 ){
|
2001-04-06 20:13:42 +04:00
|
|
|
#ifdef TCL_UTF_MAX
|
|
|
|
Tcl_AppendResult(interp,"1",0);
|
|
|
|
#else
|
|
|
|
Tcl_AppendResult(interp,"0",0);
|
|
|
|
#endif
|
|
|
|
return TCL_OK;
|
|
|
|
}
|
|
|
|
}
|
2004-02-01 04:22:50 +03:00
|
|
|
if( objc==5 || objc==6 ){
|
|
|
|
zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
|
|
|
|
if( strcmp(zArg,"-key")==0 ){
|
|
|
|
pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
|
|
|
|
objc -= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( objc!=3 && objc!=4 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 1, objv,
|
2004-02-11 05:18:05 +03:00
|
|
|
#ifdef SQLITE_HAS_CODEC
|
|
|
|
"HANDLE FILENAME ?-key CODEC-KEY?"
|
2004-02-01 04:22:50 +03:00
|
|
|
#else
|
|
|
|
"HANDLE FILENAME ?MODE?"
|
|
|
|
#endif
|
|
|
|
);
|
2000-05-29 18:26:00 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
zErrMsg = 0;
|
2000-08-04 18:56:24 +04:00
|
|
|
p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
|
2000-05-29 18:26:00 +04:00
|
|
|
if( p==0 ){
|
2000-08-04 17:49:02 +04:00
|
|
|
Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
memset(p, 0, sizeof(*p));
|
2004-02-01 04:22:50 +03:00
|
|
|
zFile = Tcl_GetStringFromObj(objv[2], 0);
|
2004-06-08 04:02:33 +04:00
|
|
|
sqlite3_open(zFile, &p->db);
|
2004-05-22 13:21:21 +04:00
|
|
|
if( SQLITE_OK!=sqlite3_errcode(p->db) ){
|
|
|
|
zErrMsg = strdup(sqlite3_errmsg(p->db));
|
|
|
|
sqlite3_close(p->db);
|
|
|
|
p->db = 0;
|
|
|
|
}
|
2004-07-22 06:40:37 +04:00
|
|
|
#ifdef SQLITE_HAS_CODEC
|
|
|
|
sqlite3_key(p->db, pKey, nKey);
|
2004-02-11 13:37:23 +03:00
|
|
|
#endif
|
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);
|
2000-05-29 18:26:00 +04:00
|
|
|
free(zErrMsg);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2004-02-01 04:22:50 +03:00
|
|
|
zArg = Tcl_GetStringFromObj(objv[1], 0);
|
|
|
|
Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
|
2002-05-10 17:14:07 +04:00
|
|
|
|
2002-06-27 00:06:05 +04:00
|
|
|
/* The return value is the value of the sqlite* pointer
|
|
|
|
*/
|
|
|
|
sprintf(zBuf, "%p", p->db);
|
2002-07-07 21:12:36 +04:00
|
|
|
if( strncmp(zBuf,"0x",2) ){
|
|
|
|
sprintf(zBuf, "0x%p", p->db);
|
|
|
|
}
|
2002-06-27 00:06:05 +04:00
|
|
|
Tcl_AppendResult(interp, zBuf, 0);
|
|
|
|
|
2002-05-10 17:14:07 +04:00
|
|
|
/* If compiled with SQLITE_TEST turned on, then register the "md5sum"
|
2002-06-27 00:06:05 +04:00
|
|
|
** SQL function.
|
2002-05-10 17:14:07 +04:00
|
|
|
*/
|
2002-03-11 05:06:13 +03:00
|
|
|
#ifdef SQLITE_TEST
|
|
|
|
{
|
2004-09-06 21:24:11 +04:00
|
|
|
extern void Md5_Register(sqlite3*);
|
2004-06-30 16:42:59 +04:00
|
|
|
#ifdef SQLITE_DEBUG
|
2004-06-30 15:54:06 +04:00
|
|
|
int mallocfail = sqlite3_iMallocFail;
|
|
|
|
sqlite3_iMallocFail = 0;
|
2004-06-30 16:42:59 +04:00
|
|
|
#endif
|
2002-05-10 17:14:07 +04:00
|
|
|
Md5_Register(p->db);
|
2004-06-30 16:42:59 +04:00
|
|
|
#ifdef SQLITE_DEBUG
|
2004-06-30 15:54:06 +04:00
|
|
|
sqlite3_iMallocFail = mallocfail;
|
2004-06-30 16:42:59 +04:00
|
|
|
#endif
|
2002-06-27 00:06:05 +04:00
|
|
|
}
|
2002-03-11 05:06:13 +03:00
|
|
|
#endif
|
2004-06-10 14:50:08 +04:00
|
|
|
p->interp = interp;
|
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
|
|
|
|
# define Tcl_InitStubs(a,b,c)
|
|
|
|
#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.
|
|
|
|
*/
|
2004-06-18 21:10:16 +04:00
|
|
|
int Sqlite3_Init(Tcl_Interp *interp){
|
2004-08-20 22:34:20 +04:00
|
|
|
Tcl_InitStubs(interp, "8.4", 0);
|
2004-06-19 04:16:31 +04:00
|
|
|
Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
|
|
|
|
Tcl_PkgProvide(interp, "sqlite3", "3.0");
|
2001-09-28 21:47:14 +04:00
|
|
|
return TCL_OK;
|
|
|
|
}
|
2004-06-18 21:10:16 +04:00
|
|
|
int Tclsqlite3_Init(Tcl_Interp *interp){
|
2004-08-20 22:34:20 +04:00
|
|
|
Tcl_InitStubs(interp, "8.4", 0);
|
2004-06-19 04:16:31 +04:00
|
|
|
Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
|
|
|
|
Tcl_PkgProvide(interp, "sqlite3", "3.0");
|
2000-05-29 18:26:00 +04:00
|
|
|
return TCL_OK;
|
|
|
|
}
|
2004-06-18 21:10:16 +04:00
|
|
|
int Sqlite3_SafeInit(Tcl_Interp *interp){
|
2001-09-28 21:47:14 +04:00
|
|
|
return TCL_OK;
|
|
|
|
}
|
2004-06-18 21:10:16 +04:00
|
|
|
int Tclsqlite3_SafeInit(Tcl_Interp *interp){
|
2000-05-29 18:26:00 +04:00
|
|
|
return TCL_OK;
|
|
|
|
}
|
|
|
|
|
2004-07-23 04:01:38 +04:00
|
|
|
#ifdef TCLSH
|
|
|
|
/*****************************************************************************
|
|
|
|
** The code that follows is used to build standalone TCL interpreters
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2000-05-30 00:41:49 +04:00
|
|
|
|
|
|
|
/*
|
2004-07-23 04:01:38 +04:00
|
|
|
** If the macro TCLSH is one, then put in code this for the
|
|
|
|
** "main" routine that will initialize Tcl and take input from
|
|
|
|
** standard input.
|
2000-05-30 00:41:49 +04:00
|
|
|
*/
|
2004-07-23 04:01:38 +04:00
|
|
|
#if TCLSH==1
|
2000-05-30 00:41:49 +04:00
|
|
|
static char zMainloop[] =
|
|
|
|
"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"
|
|
|
|
"}\n"
|
|
|
|
"}\n"
|
|
|
|
;
|
2004-07-23 04:01:38 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
** If the macro TCLSH is two, then get the main loop code out of
|
|
|
|
** the separate file "spaceanal_tcl.h".
|
|
|
|
*/
|
|
|
|
#if TCLSH==2
|
|
|
|
static char zMainloop[] =
|
|
|
|
#include "spaceanal_tcl.h"
|
|
|
|
;
|
|
|
|
#endif
|
2000-05-30 00:41:49 +04:00
|
|
|
|
|
|
|
#define TCLSH_MAIN main /* Needed to fake out mktclapp */
|
|
|
|
int TCLSH_MAIN(int argc, char **argv){
|
|
|
|
Tcl_Interp *interp;
|
2001-04-05 19:57:13 +04:00
|
|
|
Tcl_FindExecutable(argv[0]);
|
2000-05-30 00:41:49 +04:00
|
|
|
interp = Tcl_CreateInterp();
|
2004-06-18 21:10:16 +04:00
|
|
|
Sqlite3_Init(interp);
|
2001-04-15 04:37:09 +04:00
|
|
|
#ifdef SQLITE_TEST
|
2001-04-07 19:24:33 +04:00
|
|
|
{
|
|
|
|
extern int Sqlitetest1_Init(Tcl_Interp*);
|
2001-08-20 04:33:58 +04:00
|
|
|
extern int Sqlitetest2_Init(Tcl_Interp*);
|
|
|
|
extern int Sqlitetest3_Init(Tcl_Interp*);
|
2003-12-19 05:52:05 +03:00
|
|
|
extern int Sqlitetest4_Init(Tcl_Interp*);
|
2004-05-07 03:37:52 +04:00
|
|
|
extern int Sqlitetest5_Init(Tcl_Interp*);
|
2001-07-02 02:12:01 +04:00
|
|
|
extern int Md5_Init(Tcl_Interp*);
|
2004-05-11 10:17:21 +04:00
|
|
|
Sqlitetest1_Init(interp);
|
2001-08-20 04:33:58 +04:00
|
|
|
Sqlitetest2_Init(interp);
|
2004-05-07 21:57:49 +04:00
|
|
|
Sqlitetest3_Init(interp);
|
2004-05-26 06:04:57 +04:00
|
|
|
Sqlitetest4_Init(interp);
|
2004-05-07 03:37:52 +04:00
|
|
|
Sqlitetest5_Init(interp);
|
2001-07-02 02:12:01 +04:00
|
|
|
Md5_Init(interp);
|
2001-04-07 19:24:33 +04:00
|
|
|
}
|
|
|
|
#endif
|
2004-07-23 04:01:38 +04:00
|
|
|
if( argc>=2 || TCLSH==2 ){
|
2000-05-30 00:41:49 +04:00
|
|
|
int i;
|
|
|
|
Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
|
|
|
|
Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
|
|
|
|
for(i=2; i<argc; i++){
|
|
|
|
Tcl_SetVar(interp, "argv", argv[i],
|
|
|
|
TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
|
|
|
|
}
|
2004-07-23 04:01:38 +04:00
|
|
|
if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
|
2002-07-06 20:32:14 +04:00
|
|
|
const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
|
2000-06-04 16:58:36 +04:00
|
|
|
if( zInfo==0 ) zInfo = interp->result;
|
|
|
|
fprintf(stderr,"%s: %s\n", *argv, zInfo);
|
2000-05-30 00:41:49 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2004-07-23 04:01:38 +04:00
|
|
|
}
|
|
|
|
if( argc<=1 || TCLSH==2 ){
|
2000-05-30 00:41:49 +04:00
|
|
|
Tcl_GlobalEval(interp, zMainloop);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* TCLSH */
|
2000-09-21 17:01:35 +04:00
|
|
|
|
|
|
|
#endif /* !defined(NO_TCL) */
|