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
|
|
|
|
**
|
2003-08-19 18:31:01 +04:00
|
|
|
** $Id: tclsqlite.c,v 1.50 2003/08/19 14:31:02 drh 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"
|
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 */
|
|
|
|
};
|
|
|
|
|
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 {
|
|
|
|
sqlite *db; /* The "real" database structure */
|
|
|
|
Tcl_Interp *interp; /* The interpreter used for this database */
|
2000-09-21 17:01:35 +04:00
|
|
|
char *zBusy; /* The busy callback routine */
|
2003-04-23 16:25:23 +04:00
|
|
|
char *zTrace; /* The trace 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 */
|
2003-01-31 20:21:49 +03:00
|
|
|
int rc; /* Return code of most recent sqlite_exec() */
|
2000-08-04 17:49:02 +04:00
|
|
|
};
|
|
|
|
|
2000-05-29 18:26:00 +04:00
|
|
|
/*
|
|
|
|
** An instance of this structure passes information thru the sqlite
|
|
|
|
** logic from the original TCL command into the callback routine.
|
|
|
|
*/
|
|
|
|
typedef struct CallbackData CallbackData;
|
|
|
|
struct CallbackData {
|
|
|
|
Tcl_Interp *interp; /* The TCL interpreter */
|
|
|
|
char *zArray; /* The array into which data is written */
|
2000-09-21 17:01:35 +04:00
|
|
|
Tcl_Obj *pCode; /* The code to execute for each row */
|
2001-11-09 16:41:09 +03:00
|
|
|
int once; /* Set for first callback only */
|
2001-04-03 20:53:21 +04:00
|
|
|
int tcl_rc; /* Return code from TCL script */
|
2001-10-18 16:34:46 +04:00
|
|
|
int nColName; /* Number of entries in the azColName[] array */
|
|
|
|
char **azColName; /* Column names translated to UTF-8 */
|
|
|
|
};
|
2001-04-05 19:57:13 +04:00
|
|
|
|
2001-10-22 06:58:08 +04:00
|
|
|
#ifdef UTF_TRANSLATION_NEEDED
|
2000-05-29 18:26:00 +04:00
|
|
|
/*
|
|
|
|
** Called for each row of the result.
|
2001-10-22 06:58:08 +04:00
|
|
|
**
|
|
|
|
** This version is used when TCL expects UTF-8 data but the database
|
|
|
|
** uses the ISO8859 format. A translation must occur from ISO8859 into
|
|
|
|
** UTF-8.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
|
|
|
static int DbEvalCallback(
|
|
|
|
void *clientData, /* An instance of CallbackData */
|
|
|
|
int nCol, /* Number of columns in the result */
|
|
|
|
char ** azCol, /* Data for each column */
|
|
|
|
char ** azN /* Name for each column */
|
|
|
|
){
|
|
|
|
CallbackData *cbData = (CallbackData*)clientData;
|
|
|
|
int i, rc;
|
2001-04-05 19:57:13 +04:00
|
|
|
Tcl_DString dCol;
|
2001-10-22 06:58:08 +04:00
|
|
|
Tcl_DStringInit(&dCol);
|
2001-11-09 16:41:09 +03:00
|
|
|
if( cbData->azColName==0 ){
|
|
|
|
assert( cbData->once );
|
|
|
|
cbData->once = 0;
|
|
|
|
if( cbData->zArray[0] ){
|
|
|
|
Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
|
2001-10-22 06:58:08 +04:00
|
|
|
}
|
2001-11-09 16:41:09 +03:00
|
|
|
cbData->azColName = malloc( nCol*sizeof(char*) );
|
|
|
|
if( cbData->azColName==0 ){ return 1; }
|
2001-10-22 06:58:08 +04:00
|
|
|
cbData->nColName = nCol;
|
2000-05-29 18:26:00 +04:00
|
|
|
for(i=0; i<nCol; i++){
|
2001-10-22 06:58:08 +04:00
|
|
|
Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol);
|
2001-11-09 16:41:09 +03:00
|
|
|
cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 );
|
|
|
|
if( cbData->azColName[i] ){
|
|
|
|
strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol));
|
|
|
|
}else{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if( cbData->zArray[0] ){
|
|
|
|
Tcl_SetVar2(cbData->interp, cbData->zArray, "*",
|
|
|
|
Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
|
2002-07-16 00:58:47 +04:00
|
|
|
if( azN[nCol]!=0 ){
|
2002-07-11 16:18:16 +04:00
|
|
|
Tcl_DString dType;
|
|
|
|
Tcl_DStringInit(&dType);
|
|
|
|
Tcl_DStringAppend(&dType, "typeof:", -1);
|
|
|
|
Tcl_DStringAppend(&dType, Tcl_DStringValue(&dCol), -1);
|
|
|
|
Tcl_DStringFree(&dCol);
|
|
|
|
Tcl_ExternalToUtfDString(NULL, azN[i+nCol], -1, &dCol);
|
|
|
|
Tcl_SetVar2(cbData->interp, cbData->zArray,
|
|
|
|
Tcl_DStringValue(&dType), Tcl_DStringValue(&dCol),
|
|
|
|
TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
|
|
|
|
Tcl_DStringFree(&dType);
|
|
|
|
}
|
2001-10-22 06:58:08 +04:00
|
|
|
}
|
2002-07-11 01:26:00 +04:00
|
|
|
|
2001-10-22 06:58:08 +04:00
|
|
|
Tcl_DStringFree(&dCol);
|
2001-10-19 20:44:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if( azCol!=0 ){
|
|
|
|
if( cbData->zArray[0] ){
|
|
|
|
for(i=0; i<nCol; i++){
|
|
|
|
char *z = azCol[i];
|
|
|
|
if( z==0 ) z = "";
|
|
|
|
Tcl_DStringInit(&dCol);
|
|
|
|
Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
|
2001-10-22 06:58:08 +04:00
|
|
|
Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i],
|
2001-10-19 20:44:56 +04:00
|
|
|
Tcl_DStringValue(&dCol), 0);
|
|
|
|
Tcl_DStringFree(&dCol);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
for(i=0; i<nCol; i++){
|
|
|
|
char *z = azCol[i];
|
|
|
|
if( z==0 ) z = "";
|
|
|
|
Tcl_DStringInit(&dCol);
|
|
|
|
Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
|
2001-10-22 06:58:08 +04:00
|
|
|
Tcl_SetVar(cbData->interp, cbData->azColName[i],
|
|
|
|
Tcl_DStringValue(&dCol), 0);
|
2001-10-19 20:44:56 +04:00
|
|
|
Tcl_DStringFree(&dCol);
|
2001-10-22 06:58:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
|
|
|
|
if( rc==TCL_CONTINUE ) rc = TCL_OK;
|
|
|
|
cbData->tcl_rc = rc;
|
|
|
|
return rc!=TCL_OK;
|
|
|
|
}
|
|
|
|
#endif /* UTF_TRANSLATION_NEEDED */
|
|
|
|
|
|
|
|
#ifndef UTF_TRANSLATION_NEEDED
|
|
|
|
/*
|
|
|
|
** Called for each row of the result.
|
|
|
|
**
|
|
|
|
** This version is used when either of the following is true:
|
|
|
|
**
|
|
|
|
** (1) This version of TCL uses UTF-8 and the data in the
|
|
|
|
** SQLite database is already in the UTF-8 format.
|
|
|
|
**
|
|
|
|
** (2) This version of TCL uses ISO8859 and the data in the
|
|
|
|
** SQLite database is already in the ISO8859 format.
|
|
|
|
*/
|
|
|
|
static int DbEvalCallback(
|
|
|
|
void *clientData, /* An instance of CallbackData */
|
|
|
|
int nCol, /* Number of columns in the result */
|
|
|
|
char ** azCol, /* Data for each column */
|
|
|
|
char ** azN /* Name for each column */
|
|
|
|
){
|
|
|
|
CallbackData *cbData = (CallbackData*)clientData;
|
|
|
|
int i, rc;
|
|
|
|
if( azCol==0 || (cbData->once && cbData->zArray[0]) ){
|
|
|
|
Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
|
|
|
|
for(i=0; i<nCol; i++){
|
|
|
|
Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
|
|
|
|
TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
|
2002-07-11 16:18:16 +04:00
|
|
|
if( azN[nCol] ){
|
|
|
|
char *z = sqlite_mprintf("typeof:%s", azN[i]);
|
|
|
|
Tcl_SetVar2(cbData->interp, cbData->zArray, z, azN[i+nCol],
|
|
|
|
TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
|
|
|
|
sqlite_freemem(z);
|
|
|
|
}
|
2001-10-22 06:58:08 +04:00
|
|
|
}
|
|
|
|
cbData->once = 0;
|
|
|
|
}
|
|
|
|
if( azCol!=0 ){
|
|
|
|
if( cbData->zArray[0] ){
|
|
|
|
for(i=0; i<nCol; i++){
|
|
|
|
char *z = azCol[i];
|
|
|
|
if( z==0 ) z = "";
|
|
|
|
Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
for(i=0; i<nCol; i++){
|
|
|
|
char *z = azCol[i];
|
|
|
|
if( z==0 ) z = "";
|
2001-10-19 20:44:56 +04:00
|
|
|
Tcl_SetVar(cbData->interp, azN[i], z, 0);
|
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
|
|
|
}
|
2000-09-21 17:01:35 +04:00
|
|
|
rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
|
2001-04-03 20:53:21 +04:00
|
|
|
if( rc==TCL_CONTINUE ) rc = TCL_OK;
|
|
|
|
cbData->tcl_rc = rc;
|
|
|
|
return rc!=TCL_OK;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2001-10-22 06:58:08 +04:00
|
|
|
#endif
|
2000-05-29 18:26:00 +04:00
|
|
|
|
2000-09-21 17:01:35 +04:00
|
|
|
/*
|
|
|
|
** This is an alternative callback for database queries. Instead
|
|
|
|
** of invoking a TCL script to handle the result, this callback just
|
|
|
|
** appends each column of the result to a list. After the query
|
|
|
|
** is complete, the list is returned.
|
|
|
|
*/
|
|
|
|
static int DbEvalCallback2(
|
|
|
|
void *clientData, /* An instance of CallbackData */
|
|
|
|
int nCol, /* Number of columns in the result */
|
|
|
|
char ** azCol, /* Data for each column */
|
|
|
|
char ** azN /* Name for each column */
|
|
|
|
){
|
|
|
|
Tcl_Obj *pList = (Tcl_Obj*)clientData;
|
|
|
|
int i;
|
2001-10-19 20:44:56 +04:00
|
|
|
if( azCol==0 ) return 0;
|
2000-09-21 17:01:35 +04:00
|
|
|
for(i=0; i<nCol; i++){
|
|
|
|
Tcl_Obj *pElem;
|
|
|
|
if( azCol[i] && *azCol[i] ){
|
2001-04-05 19:57:13 +04:00
|
|
|
#ifdef UTF_TRANSLATION_NEEDED
|
|
|
|
Tcl_DString dCol;
|
|
|
|
Tcl_DStringInit(&dCol);
|
|
|
|
Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol);
|
|
|
|
pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
|
|
|
|
Tcl_DStringFree(&dCol);
|
|
|
|
#else
|
2000-09-21 17:01:35 +04:00
|
|
|
pElem = Tcl_NewStringObj(azCol[i], -1);
|
2001-04-05 19:57:13 +04:00
|
|
|
#endif
|
2000-09-21 17:01:35 +04:00
|
|
|
}else{
|
|
|
|
pElem = Tcl_NewObj();
|
|
|
|
}
|
|
|
|
Tcl_ListObjAppendElement(0, pList, pElem);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-08-19 18:31:01 +04:00
|
|
|
/*
|
|
|
|
** This is a second alternative callback for database queries. A the
|
|
|
|
** first column of the first row of the result is made the TCL result.
|
|
|
|
*/
|
|
|
|
static int DbEvalCallback3(
|
|
|
|
void *clientData, /* An instance of CallbackData */
|
|
|
|
int nCol, /* Number of columns in the result */
|
|
|
|
char ** azCol, /* Data for each column */
|
|
|
|
char ** azN /* Name for each column */
|
|
|
|
){
|
|
|
|
Tcl_Interp *interp = (Tcl_Interp*)clientData;
|
|
|
|
Tcl_Obj *pElem;
|
|
|
|
if( azCol==0 ) return 1;
|
|
|
|
if( nCol==0 ) return 1;
|
|
|
|
#ifdef UTF_TRANSLATION_NEEDED
|
|
|
|
{
|
|
|
|
Tcl_DString dCol;
|
|
|
|
Tcl_DStringInit(&dCol);
|
|
|
|
Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol);
|
|
|
|
pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
|
|
|
|
Tcl_DStringFree(&dCol);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
pElem = Tcl_NewStringObj(azCol[0], -1);
|
|
|
|
#endif
|
|
|
|
Tcl_SetObjResult(interp, pElem);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-05-29 18:26:00 +04:00
|
|
|
/*
|
|
|
|
** Called when the command is deleted.
|
|
|
|
*/
|
|
|
|
static void DbDeleteCmd(void *db){
|
2000-08-04 17:49:02 +04:00
|
|
|
SqliteDb *pDb = (SqliteDb*)db;
|
|
|
|
sqlite_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);
|
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
static int DbBusyHandler(void *cd, const char *zTable, int nTries){
|
|
|
|
SqliteDb *pDb = (SqliteDb*)cd;
|
|
|
|
int rc;
|
|
|
|
char zVal[30];
|
|
|
|
char *zCmd;
|
|
|
|
Tcl_DString cmd;
|
|
|
|
|
|
|
|
Tcl_DStringInit(&cmd);
|
|
|
|
Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
|
|
|
|
Tcl_DStringAppendElement(&cmd, zTable);
|
|
|
|
sprintf(zVal, " %d", nTries);
|
|
|
|
Tcl_DStringAppend(&cmd, zVal, -1);
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
2002-09-14 17:47:32 +04:00
|
|
|
/*
|
|
|
|
** This routine is called to evaluate an SQL function implemented
|
|
|
|
** using TCL script.
|
|
|
|
*/
|
|
|
|
static void tclSqlFunc(sqlite_func *context, int argc, const char **argv){
|
|
|
|
SqlFunc *p = sqlite_user_data(context);
|
|
|
|
Tcl_DString cmd;
|
|
|
|
int i;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
Tcl_DStringInit(&cmd);
|
|
|
|
Tcl_DStringAppend(&cmd, p->zScript, -1);
|
|
|
|
for(i=0; i<argc; i++){
|
|
|
|
Tcl_DStringAppendElement(&cmd, argv[i] ? argv[i] : "");
|
|
|
|
}
|
|
|
|
rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
|
|
|
|
if( rc ){
|
|
|
|
sqlite_set_result_error(context, Tcl_GetStringResult(p->interp), -1);
|
|
|
|
}else{
|
|
|
|
sqlite_set_result_string(context, Tcl_GetStringResult(p->interp), -1);
|
|
|
|
}
|
|
|
|
}
|
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;
|
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
|
|
|
|
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:
|
|
|
|
**
|
|
|
|
** sqlite db1 "my_database"
|
|
|
|
** 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;
|
2002-07-06 20:32:14 +04:00
|
|
|
static const char *DB_strs[] = {
|
2003-04-23 16:25:23 +04:00
|
|
|
"authorizer", "busy", "changes",
|
|
|
|
"close", "complete", "errorcode",
|
|
|
|
"eval", "function", "last_insert_rowid",
|
2003-08-19 18:31:01 +04:00
|
|
|
"onecolumn", "timeout", "trace",
|
|
|
|
0
|
2000-09-21 17:01:35 +04:00
|
|
|
};
|
2002-06-25 23:31:18 +04:00
|
|
|
enum DB_enum {
|
2003-04-23 16:25:23 +04:00
|
|
|
DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
|
|
|
|
DB_CLOSE, DB_COMPLETE, DB_ERRORCODE,
|
|
|
|
DB_EVAL, DB_FUNCTION, DB_LAST_INSERT_ROWID,
|
2003-08-19 18:31:01 +04:00
|
|
|
DB_ONECOLUMN, DB_TIMEOUT, 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: {
|
|
|
|
if( objc>3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
|
|
|
|
}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;
|
|
|
|
}
|
|
|
|
#ifndef SQLITE_OMIT_AUTHORIZATION
|
|
|
|
if( pDb->zAuth ){
|
|
|
|
pDb->interp = interp;
|
|
|
|
sqlite_set_authorizer(pDb->db, auth_callback, pDb);
|
|
|
|
}else{
|
|
|
|
sqlite_set_authorizer(pDb->db, 0, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
|
2000-09-21 17:01:35 +04:00
|
|
|
}else{
|
|
|
|
sqlite_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
|
|
|
|
2002-04-12 14:08:59 +04:00
|
|
|
/*
|
|
|
|
** $db changes
|
|
|
|
**
|
|
|
|
** Return the number of rows that were modified, inserted, or deleted by
|
|
|
|
** the most recent "eval".
|
|
|
|
*/
|
|
|
|
case DB_CHANGES: {
|
|
|
|
Tcl_Obj *pResult;
|
|
|
|
int nChange;
|
|
|
|
if( objc!=2 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
nChange = sqlite_changes(pDb->db);
|
|
|
|
pResult = Tcl_GetObjResult(interp);
|
|
|
|
Tcl_SetIntObj(pResult, nChange);
|
|
|
|
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
|
|
|
|
|
|
|
/* $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;
|
|
|
|
}
|
2000-09-21 17:01:35 +04:00
|
|
|
isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
|
|
|
|
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
|
|
|
|
** call to sqlite_exec().
|
|
|
|
*/
|
|
|
|
case DB_ERRORCODE: {
|
|
|
|
Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc));
|
|
|
|
break;
|
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** $db eval $sql ?array { ...code... }?
|
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
*/
|
2000-09-21 17:01:35 +04:00
|
|
|
case DB_EVAL: {
|
2000-05-29 18:26:00 +04:00
|
|
|
CallbackData cbData;
|
|
|
|
char *zErrMsg;
|
2000-09-21 17:01:35 +04:00
|
|
|
char *zSql;
|
2000-05-29 18:26:00 +04:00
|
|
|
int rc;
|
2001-04-05 19:57:13 +04:00
|
|
|
#ifdef UTF_TRANSLATION_NEEDED
|
|
|
|
Tcl_DString dSql;
|
2001-10-22 06:58:08 +04:00
|
|
|
int i;
|
2001-04-05 19:57:13 +04:00
|
|
|
#endif
|
2000-05-29 18:26:00 +04:00
|
|
|
|
2000-09-21 17:01:35 +04:00
|
|
|
if( objc!=5 && objc!=3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
|
2000-05-29 18:26:00 +04:00
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
2000-08-04 17:49:02 +04:00
|
|
|
pDb->interp = interp;
|
2000-09-21 17:01:35 +04:00
|
|
|
zSql = Tcl_GetStringFromObj(objv[2], 0);
|
2001-04-05 19:57:13 +04:00
|
|
|
#ifdef UTF_TRANSLATION_NEEDED
|
|
|
|
Tcl_DStringInit(&dSql);
|
|
|
|
Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
|
|
|
|
zSql = Tcl_DStringValue(&dSql);
|
|
|
|
#endif
|
2000-09-21 17:01:35 +04:00
|
|
|
Tcl_IncrRefCount(objv[2]);
|
|
|
|
if( objc==5 ){
|
2000-05-29 18:26:00 +04:00
|
|
|
cbData.interp = interp;
|
2000-05-30 17:44:19 +04:00
|
|
|
cbData.once = 1;
|
2000-09-21 17:01:35 +04:00
|
|
|
cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
|
|
|
|
cbData.pCode = objv[4];
|
2001-04-03 20:53:21 +04:00
|
|
|
cbData.tcl_rc = TCL_OK;
|
2001-10-22 06:58:08 +04:00
|
|
|
cbData.nColName = 0;
|
|
|
|
cbData.azColName = 0;
|
2000-05-29 18:26:00 +04:00
|
|
|
zErrMsg = 0;
|
2000-09-21 17:01:35 +04:00
|
|
|
Tcl_IncrRefCount(objv[3]);
|
|
|
|
Tcl_IncrRefCount(objv[4]);
|
|
|
|
rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
|
|
|
|
Tcl_DecrRefCount(objv[4]);
|
|
|
|
Tcl_DecrRefCount(objv[3]);
|
2001-04-03 20:53:21 +04:00
|
|
|
if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
|
2000-05-29 18:26:00 +04:00
|
|
|
}else{
|
2000-09-21 17:01:35 +04:00
|
|
|
Tcl_Obj *pList = Tcl_NewObj();
|
2001-04-03 20:53:21 +04:00
|
|
|
cbData.tcl_rc = TCL_OK;
|
2000-09-21 17:01:35 +04:00
|
|
|
rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
|
|
|
|
Tcl_SetObjResult(interp, pList);
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2003-01-31 20:21:49 +03:00
|
|
|
pDb->rc = rc;
|
2002-09-03 23:43:23 +04:00
|
|
|
if( rc==SQLITE_ABORT ){
|
|
|
|
if( zErrMsg ) free(zErrMsg);
|
|
|
|
rc = cbData.tcl_rc;
|
|
|
|
}else if( zErrMsg ){
|
2000-05-29 18:26:00 +04:00
|
|
|
Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
|
|
|
|
free(zErrMsg);
|
2001-04-03 20:53:21 +04:00
|
|
|
rc = TCL_ERROR;
|
2002-09-03 23:43:23 +04:00
|
|
|
}else if( rc!=SQLITE_OK ){
|
2001-10-22 06:58:08 +04:00
|
|
|
Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
|
|
|
|
rc = TCL_ERROR;
|
2001-04-03 20:53:21 +04:00
|
|
|
}else{
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2000-09-21 17:01:35 +04:00
|
|
|
Tcl_DecrRefCount(objv[2]);
|
2001-04-05 19:57:13 +04:00
|
|
|
#ifdef UTF_TRANSLATION_NEEDED
|
|
|
|
Tcl_DStringFree(&dSql);
|
2001-10-22 06:58:08 +04:00
|
|
|
if( objc==5 && cbData.azColName ){
|
|
|
|
for(i=0; i<cbData.nColName; i++){
|
|
|
|
if( cbData.azColName[i] ) free(cbData.azColName[i]);
|
|
|
|
}
|
|
|
|
free(cbData.azColName);
|
2001-11-09 16:41:09 +03:00
|
|
|
cbData.azColName = 0;
|
2001-10-22 06:58:08 +04:00
|
|
|
}
|
2001-04-05 19:57:13 +04:00
|
|
|
#endif
|
2000-05-29 18:26:00 +04:00
|
|
|
return rc;
|
2000-09-21 17:01:35 +04:00
|
|
|
}
|
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];
|
|
|
|
strcpy(pFunc->zScript, zScript);
|
|
|
|
sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc);
|
|
|
|
sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
rowid = sqlite_last_insert_rowid(pDb->db);
|
|
|
|
pResult = Tcl_GetObjResult(interp);
|
|
|
|
Tcl_SetIntObj(pResult, rowid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-08-19 18:31:01 +04:00
|
|
|
/*
|
|
|
|
** $db onecolumn SQL
|
|
|
|
**
|
|
|
|
** Return a single column from a single row of the given SQL query.
|
|
|
|
*/
|
|
|
|
case DB_ONECOLUMN: {
|
|
|
|
int rc;
|
|
|
|
char *zSql;
|
|
|
|
char *zErrMsg = 0;
|
|
|
|
if( objc!=3 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 2, objv, "SQL");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
zSql = Tcl_GetStringFromObj(objv[2], 0);
|
|
|
|
rc = sqlite_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
|
|
|
|
if( rc==SQLITE_ABORT ){
|
|
|
|
/* Do nothing. This is normal. */
|
|
|
|
}else if( zErrMsg ){
|
|
|
|
Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
|
|
|
|
free(zErrMsg);
|
|
|
|
rc = TCL_ERROR;
|
|
|
|
}else if( rc!=SQLITE_OK ){
|
|
|
|
Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
|
|
|
|
rc = TCL_ERROR;
|
|
|
|
}
|
|
|
|
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;
|
2000-08-04 17:49:02 +04:00
|
|
|
sqlite_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
|
|
|
|
|
|
|
/* $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?");
|
|
|
|
}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;
|
|
|
|
sqlite_trace(pDb->db, DbTraceHandler, pDb);
|
|
|
|
}else{
|
|
|
|
sqlite_trace(pDb->db, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-09-21 17:01:35 +04:00
|
|
|
} /* End of the SWITCH statement */
|
2000-05-29 18:26:00 +04:00
|
|
|
return TCL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** sqlite DBNAME FILENAME ?MODE?
|
|
|
|
**
|
|
|
|
** 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:
|
|
|
|
**
|
|
|
|
** sqlite -encoding
|
|
|
|
**
|
|
|
|
** Return the encoding used by LIKE and GLOB operators. Choices
|
|
|
|
** are UTF-8 and iso8859.
|
|
|
|
**
|
2002-11-04 22:32:25 +03:00
|
|
|
** sqlite -version
|
|
|
|
**
|
|
|
|
** Return the version number of the SQLite library.
|
|
|
|
**
|
2001-04-06 20:13:42 +04:00
|
|
|
** sqlite -tcl-uses-utf
|
|
|
|
**
|
|
|
|
** 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
|
|
|
*/
|
|
|
|
static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){
|
|
|
|
int mode;
|
2000-08-04 17:49:02 +04:00
|
|
|
SqliteDb *p;
|
2000-05-29 18:26:00 +04:00
|
|
|
char *zErrMsg;
|
2002-06-27 00:06:05 +04:00
|
|
|
char zBuf[80];
|
2001-04-06 20:13:42 +04:00
|
|
|
if( argc==2 ){
|
|
|
|
if( strcmp(argv[1],"-encoding")==0 ){
|
|
|
|
Tcl_AppendResult(interp,sqlite_encoding,0);
|
|
|
|
return TCL_OK;
|
|
|
|
}
|
2002-11-04 22:32:25 +03:00
|
|
|
if( strcmp(argv[1],"-version")==0 ){
|
|
|
|
Tcl_AppendResult(interp,sqlite_version,0);
|
|
|
|
return TCL_OK;
|
|
|
|
}
|
2001-04-06 20:13:42 +04:00
|
|
|
if( strcmp(argv[1],"-tcl-uses-utf")==0 ){
|
|
|
|
#ifdef TCL_UTF_MAX
|
|
|
|
Tcl_AppendResult(interp,"1",0);
|
|
|
|
#else
|
|
|
|
Tcl_AppendResult(interp,"0",0);
|
|
|
|
#endif
|
|
|
|
return TCL_OK;
|
|
|
|
}
|
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
if( argc!=3 && argc!=4 ){
|
|
|
|
Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
|
|
|
|
" HANDLE FILENAME ?MODE?\"", 0);
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
if( argc==3 ){
|
2000-06-02 05:17:37 +04:00
|
|
|
mode = 0666;
|
2000-05-29 18:26:00 +04:00
|
|
|
}else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){
|
|
|
|
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));
|
|
|
|
p->db = sqlite_open(argv[2], mode, &zErrMsg);
|
|
|
|
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;
|
|
|
|
}
|
2000-09-21 17:01:35 +04:00
|
|
|
Tcl_CreateObjCommand(interp, argv[1], 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
|
|
|
|
{
|
2002-05-10 17:14:07 +04:00
|
|
|
extern void Md5_Register(sqlite*);
|
|
|
|
Md5_Register(p->db);
|
2002-06-27 00:06:05 +04:00
|
|
|
}
|
2002-03-11 05:06:13 +03:00
|
|
|
#endif
|
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.
|
|
|
|
*/
|
|
|
|
int Sqlite_Init(Tcl_Interp *interp){
|
2001-09-28 21:47:14 +04:00
|
|
|
Tcl_InitStubs(interp, "8.0", 0);
|
2002-08-31 22:53:06 +04:00
|
|
|
Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
|
2001-10-22 06:58:08 +04:00
|
|
|
Tcl_PkgProvide(interp, "sqlite", "2.0");
|
2001-09-28 21:47:14 +04:00
|
|
|
return TCL_OK;
|
|
|
|
}
|
|
|
|
int Tclsqlite_Init(Tcl_Interp *interp){
|
|
|
|
Tcl_InitStubs(interp, "8.0", 0);
|
2002-08-31 22:53:06 +04:00
|
|
|
Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
|
2001-10-22 06:58:08 +04:00
|
|
|
Tcl_PkgProvide(interp, "sqlite", "2.0");
|
2000-05-29 18:26:00 +04:00
|
|
|
return TCL_OK;
|
|
|
|
}
|
|
|
|
int Sqlite_SafeInit(Tcl_Interp *interp){
|
2001-09-28 21:47:14 +04:00
|
|
|
return TCL_OK;
|
|
|
|
}
|
|
|
|
int Tclsqlite_SafeInit(Tcl_Interp *interp){
|
2000-05-29 18:26:00 +04:00
|
|
|
return TCL_OK;
|
|
|
|
}
|
|
|
|
|
2000-10-19 18:59:27 +04:00
|
|
|
#if 0
|
2000-05-29 18:26:00 +04:00
|
|
|
/*
|
|
|
|
** If compiled using mktclapp, this routine runs to initialize
|
|
|
|
** everything.
|
|
|
|
*/
|
|
|
|
int Et_AppInit(Tcl_Interp *interp){
|
|
|
|
return Sqlite_Init(interp);
|
|
|
|
}
|
2000-10-19 18:59:27 +04:00
|
|
|
#endif
|
2000-05-30 00:41:49 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** If the macro TCLSH is defined and is one, then put in code for the
|
|
|
|
** "main" routine that will initialize Tcl.
|
|
|
|
*/
|
|
|
|
#if defined(TCLSH) && TCLSH==1
|
|
|
|
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"
|
|
|
|
;
|
|
|
|
|
|
|
|
#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();
|
|
|
|
Sqlite_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*);
|
2001-07-02 02:12:01 +04:00
|
|
|
extern int Md5_Init(Tcl_Interp*);
|
2001-04-07 19:24:33 +04:00
|
|
|
Sqlitetest1_Init(interp);
|
2001-08-20 04:33:58 +04:00
|
|
|
Sqlitetest2_Init(interp);
|
|
|
|
Sqlitetest3_Init(interp);
|
2001-07-02 02:12:01 +04:00
|
|
|
Md5_Init(interp);
|
2001-04-07 19:24:33 +04:00
|
|
|
}
|
|
|
|
#endif
|
2000-05-30 00:41:49 +04:00
|
|
|
if( argc>=2 ){
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
if( 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;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
Tcl_GlobalEval(interp, zMainloop);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* TCLSH */
|
2000-09-21 17:01:35 +04:00
|
|
|
|
|
|
|
#endif /* !defined(NO_TCL) */
|