2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A** 2001 September 15
2N/A**
2N/A** The author disclaims copyright to this source code. In place of
2N/A** a legal notice, here is a blessing:
2N/A**
2N/A** May you do good and not evil.
2N/A** May you find forgiveness for yourself and forgive others.
2N/A** May you share freely, never taking more than you give.
2N/A**
2N/A*************************************************************************
2N/A** A TCL Interface to SQLite
2N/A**
2N/A** $Id: tclsqlite.c,v 1.59.2.1 2004/06/19 11:57:40 drh Exp $
2N/A*/
2N/A#ifndef NO_TCL /* Omit this whole file if TCL is unavailable */
2N/A
2N/A#include "sqliteInt.h"
2N/A#include "tcl.h"
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <assert.h>
2N/A
2N/A/*
2N/A** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
2N/A** have to do a translation when going between the two. Set the
2N/A** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
2N/A** this translation.
2N/A*/
2N/A#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
2N/A# define UTF_TRANSLATION_NEEDED 1
2N/A#endif
2N/A
2N/A/*
2N/A** New SQL functions can be created as TCL scripts. Each such function
2N/A** is described by an instance of the following structure.
2N/A*/
2N/Atypedef struct SqlFunc SqlFunc;
2N/Astruct SqlFunc {
2N/A Tcl_Interp *interp; /* The TCL interpret to execute the function */
2N/A char *zScript; /* The script to be run */
2N/A SqlFunc *pNext; /* Next function on the list of them all */
2N/A};
2N/A
2N/A/*
2N/A** There is one instance of this structure for each SQLite database
2N/A** that has been opened by the SQLite TCL interface.
2N/A*/
2N/Atypedef struct SqliteDb SqliteDb;
2N/Astruct SqliteDb {
2N/A sqlite *db; /* The "real" database structure */
2N/A Tcl_Interp *interp; /* The interpreter used for this database */
2N/A char *zBusy; /* The busy callback routine */
2N/A char *zCommit; /* The commit hook callback routine */
2N/A char *zTrace; /* The trace callback routine */
2N/A char *zProgress; /* The progress callback routine */
2N/A char *zAuth; /* The authorization callback routine */
2N/A SqlFunc *pFunc; /* List of SQL functions */
2N/A int rc; /* Return code of most recent sqlite_exec() */
2N/A};
2N/A
2N/A/*
2N/A** An instance of this structure passes information thru the sqlite
2N/A** logic from the original TCL command into the callback routine.
2N/A*/
2N/Atypedef struct CallbackData CallbackData;
2N/Astruct CallbackData {
2N/A Tcl_Interp *interp; /* The TCL interpreter */
2N/A char *zArray; /* The array into which data is written */
2N/A Tcl_Obj *pCode; /* The code to execute for each row */
2N/A int once; /* Set for first callback only */
2N/A int tcl_rc; /* Return code from TCL script */
2N/A int nColName; /* Number of entries in the azColName[] array */
2N/A char **azColName; /* Column names translated to UTF-8 */
2N/A};
2N/A
2N/A#ifdef UTF_TRANSLATION_NEEDED
2N/A/*
2N/A** Called for each row of the result.
2N/A**
2N/A** This version is used when TCL expects UTF-8 data but the database
2N/A** uses the ISO8859 format. A translation must occur from ISO8859 into
2N/A** UTF-8.
2N/A*/
2N/Astatic int DbEvalCallback(
2N/A void *clientData, /* An instance of CallbackData */
2N/A int nCol, /* Number of columns in the result */
2N/A char ** azCol, /* Data for each column */
2N/A char ** azN /* Name for each column */
2N/A){
2N/A CallbackData *cbData = (CallbackData*)clientData;
2N/A int i, rc;
2N/A Tcl_DString dCol;
2N/A Tcl_DStringInit(&dCol);
2N/A if( cbData->azColName==0 ){
2N/A assert( cbData->once );
2N/A cbData->once = 0;
2N/A if( cbData->zArray[0] ){
2N/A Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
2N/A }
2N/A cbData->azColName = malloc( nCol*sizeof(char*) );
2N/A if( cbData->azColName==0 ){ return 1; }
2N/A cbData->nColName = nCol;
2N/A for(i=0; i<nCol; i++){
2N/A Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol);
2N/A cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 );
2N/A if( cbData->azColName[i] ){
2N/A strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol));
2N/A }else{
2N/A return 1;
2N/A }
2N/A if( cbData->zArray[0] ){
2N/A Tcl_SetVar2(cbData->interp, cbData->zArray, "*",
2N/A Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
2N/A if( azN[nCol]!=0 ){
2N/A Tcl_DString dType;
2N/A Tcl_DStringInit(&dType);
2N/A Tcl_DStringAppend(&dType, "typeof:", -1);
2N/A Tcl_DStringAppend(&dType, Tcl_DStringValue(&dCol), -1);
2N/A Tcl_DStringFree(&dCol);
2N/A Tcl_ExternalToUtfDString(NULL, azN[i+nCol], -1, &dCol);
2N/A Tcl_SetVar2(cbData->interp, cbData->zArray,
2N/A Tcl_DStringValue(&dType), Tcl_DStringValue(&dCol),
2N/A TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
2N/A Tcl_DStringFree(&dType);
2N/A }
2N/A }
2N/A
2N/A Tcl_DStringFree(&dCol);
2N/A }
2N/A }
2N/A if( azCol!=0 ){
2N/A if( cbData->zArray[0] ){
2N/A for(i=0; i<nCol; i++){
2N/A char *z = azCol[i];
2N/A if( z==0 ) z = "";
2N/A Tcl_DStringInit(&dCol);
2N/A Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
2N/A Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i],
2N/A Tcl_DStringValue(&dCol), 0);
2N/A Tcl_DStringFree(&dCol);
2N/A }
2N/A }else{
2N/A for(i=0; i<nCol; i++){
2N/A char *z = azCol[i];
2N/A if( z==0 ) z = "";
2N/A Tcl_DStringInit(&dCol);
2N/A Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
2N/A Tcl_SetVar(cbData->interp, cbData->azColName[i],
2N/A Tcl_DStringValue(&dCol), 0);
2N/A Tcl_DStringFree(&dCol);
2N/A }
2N/A }
2N/A }
2N/A rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
2N/A if( rc==TCL_CONTINUE ) rc = TCL_OK;
2N/A cbData->tcl_rc = rc;
2N/A return rc!=TCL_OK;
2N/A}
2N/A#endif /* UTF_TRANSLATION_NEEDED */
2N/A
2N/A#ifndef UTF_TRANSLATION_NEEDED
2N/A/*
2N/A** Called for each row of the result.
2N/A**
2N/A** This version is used when either of the following is true:
2N/A**
2N/A** (1) This version of TCL uses UTF-8 and the data in the
2N/A** SQLite database is already in the UTF-8 format.
2N/A**
2N/A** (2) This version of TCL uses ISO8859 and the data in the
2N/A** SQLite database is already in the ISO8859 format.
2N/A*/
2N/Astatic int DbEvalCallback(
2N/A void *clientData, /* An instance of CallbackData */
2N/A int nCol, /* Number of columns in the result */
2N/A char ** azCol, /* Data for each column */
2N/A char ** azN /* Name for each column */
2N/A){
2N/A CallbackData *cbData = (CallbackData*)clientData;
2N/A int i, rc;
2N/A if( azCol==0 || (cbData->once && cbData->zArray[0]) ){
2N/A Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
2N/A for(i=0; i<nCol; i++){
2N/A Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
2N/A TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
2N/A if( azN[nCol] ){
2N/A char *z = sqlite_mprintf("typeof:%s", azN[i]);
2N/A Tcl_SetVar2(cbData->interp, cbData->zArray, z, azN[i+nCol],
2N/A TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
2N/A sqlite_freemem(z);
2N/A }
2N/A }
2N/A cbData->once = 0;
2N/A }
2N/A if( azCol!=0 ){
2N/A if( cbData->zArray[0] ){
2N/A for(i=0; i<nCol; i++){
2N/A char *z = azCol[i];
2N/A if( z==0 ) z = "";
2N/A Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
2N/A }
2N/A }else{
2N/A for(i=0; i<nCol; i++){
2N/A char *z = azCol[i];
2N/A if( z==0 ) z = "";
2N/A Tcl_SetVar(cbData->interp, azN[i], z, 0);
2N/A }
2N/A }
2N/A }
2N/A rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
2N/A if( rc==TCL_CONTINUE ) rc = TCL_OK;
2N/A cbData->tcl_rc = rc;
2N/A return rc!=TCL_OK;
2N/A}
2N/A#endif
2N/A
2N/A/*
2N/A** This is an alternative callback for database queries. Instead
2N/A** of invoking a TCL script to handle the result, this callback just
2N/A** appends each column of the result to a list. After the query
2N/A** is complete, the list is returned.
2N/A*/
2N/Astatic int DbEvalCallback2(
2N/A void *clientData, /* An instance of CallbackData */
2N/A int nCol, /* Number of columns in the result */
2N/A char ** azCol, /* Data for each column */
2N/A char ** azN /* Name for each column */
2N/A){
2N/A Tcl_Obj *pList = (Tcl_Obj*)clientData;
2N/A int i;
2N/A if( azCol==0 ) return 0;
2N/A for(i=0; i<nCol; i++){
2N/A Tcl_Obj *pElem;
2N/A if( azCol[i] && *azCol[i] ){
2N/A#ifdef UTF_TRANSLATION_NEEDED
2N/A Tcl_DString dCol;
2N/A Tcl_DStringInit(&dCol);
2N/A Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol);
2N/A pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
2N/A Tcl_DStringFree(&dCol);
2N/A#else
2N/A pElem = Tcl_NewStringObj(azCol[i], -1);
2N/A#endif
2N/A }else{
2N/A pElem = Tcl_NewObj();
2N/A }
2N/A Tcl_ListObjAppendElement(0, pList, pElem);
2N/A }
2N/A return 0;
2N/A}
2N/A
2N/A/*
2N/A** This is a second alternative callback for database queries. A the
2N/A** first column of the first row of the result is made the TCL result.
2N/A*/
2N/Astatic int DbEvalCallback3(
2N/A void *clientData, /* An instance of CallbackData */
2N/A int nCol, /* Number of columns in the result */
2N/A char ** azCol, /* Data for each column */
2N/A char ** azN /* Name for each column */
2N/A){
2N/A Tcl_Interp *interp = (Tcl_Interp*)clientData;
2N/A Tcl_Obj *pElem;
2N/A if( azCol==0 ) return 1;
2N/A if( nCol==0 ) return 1;
2N/A#ifdef UTF_TRANSLATION_NEEDED
2N/A {
2N/A Tcl_DString dCol;
2N/A Tcl_DStringInit(&dCol);
2N/A Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol);
2N/A pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
2N/A Tcl_DStringFree(&dCol);
2N/A }
2N/A#else
2N/A pElem = Tcl_NewStringObj(azCol[0], -1);
2N/A#endif
2N/A Tcl_SetObjResult(interp, pElem);
2N/A return 1;
2N/A}
2N/A
2N/A/*
2N/A** Called when the command is deleted.
2N/A*/
2N/Astatic void DbDeleteCmd(void *db){
2N/A SqliteDb *pDb = (SqliteDb*)db;
2N/A sqlite_close(pDb->db);
2N/A while( pDb->pFunc ){
2N/A SqlFunc *pFunc = pDb->pFunc;
2N/A pDb->pFunc = pFunc->pNext;
2N/A Tcl_Free((char*)pFunc);
2N/A }
2N/A if( pDb->zBusy ){
2N/A Tcl_Free(pDb->zBusy);
2N/A }
2N/A if( pDb->zTrace ){
2N/A Tcl_Free(pDb->zTrace);
2N/A }
2N/A if( pDb->zAuth ){
2N/A Tcl_Free(pDb->zAuth);
2N/A }
2N/A Tcl_Free((char*)pDb);
2N/A}
2N/A
2N/A/*
2N/A** This routine is called when a database file is locked while trying
2N/A** to execute SQL.
2N/A*/
2N/Astatic int DbBusyHandler(void *cd, const char *zTable, int nTries){
2N/A SqliteDb *pDb = (SqliteDb*)cd;
2N/A int rc;
2N/A char zVal[30];
2N/A char *zCmd;
2N/A Tcl_DString cmd;
2N/A
2N/A Tcl_DStringInit(&cmd);
2N/A Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
2N/A Tcl_DStringAppendElement(&cmd, zTable);
2N/A sprintf(zVal, " %d", nTries);
2N/A Tcl_DStringAppend(&cmd, zVal, -1);
2N/A zCmd = Tcl_DStringValue(&cmd);
2N/A rc = Tcl_Eval(pDb->interp, zCmd);
2N/A Tcl_DStringFree(&cmd);
2N/A if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
2N/A return 0;
2N/A }
2N/A return 1;
2N/A}
2N/A
2N/A/*
2N/A** This routine is invoked as the 'progress callback' for the database.
2N/A*/
2N/Astatic int DbProgressHandler(void *cd){
2N/A SqliteDb *pDb = (SqliteDb*)cd;
2N/A int rc;
2N/A
2N/A assert( pDb->zProgress );
2N/A rc = Tcl_Eval(pDb->interp, pDb->zProgress);
2N/A if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
2N/A return 1;
2N/A }
2N/A return 0;
2N/A}
2N/A
2N/A/*
2N/A** This routine is called by the SQLite trace handler whenever a new
2N/A** block of SQL is executed. The TCL script in pDb->zTrace is executed.
2N/A*/
2N/Astatic void DbTraceHandler(void *cd, const char *zSql){
2N/A SqliteDb *pDb = (SqliteDb*)cd;
2N/A Tcl_DString str;
2N/A
2N/A Tcl_DStringInit(&str);
2N/A Tcl_DStringAppend(&str, pDb->zTrace, -1);
2N/A Tcl_DStringAppendElement(&str, zSql);
2N/A Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
2N/A Tcl_DStringFree(&str);
2N/A Tcl_ResetResult(pDb->interp);
2N/A}
2N/A
2N/A/*
2N/A** This routine is called when a transaction is committed. The
2N/A** TCL script in pDb->zCommit is executed. If it returns non-zero or
2N/A** if it throws an exception, the transaction is rolled back instead
2N/A** of being committed.
2N/A*/
2N/Astatic int DbCommitHandler(void *cd){
2N/A SqliteDb *pDb = (SqliteDb*)cd;
2N/A int rc;
2N/A
2N/A rc = Tcl_Eval(pDb->interp, pDb->zCommit);
2N/A if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
2N/A return 1;
2N/A }
2N/A return 0;
2N/A}
2N/A
2N/A/*
2N/A** This routine is called to evaluate an SQL function implemented
2N/A** using TCL script.
2N/A*/
2N/Astatic void tclSqlFunc(sqlite_func *context, int argc, const char **argv){
2N/A SqlFunc *p = sqlite_user_data(context);
2N/A Tcl_DString cmd;
2N/A int i;
2N/A int rc;
2N/A
2N/A Tcl_DStringInit(&cmd);
2N/A Tcl_DStringAppend(&cmd, p->zScript, -1);
2N/A for(i=0; i<argc; i++){
2N/A Tcl_DStringAppendElement(&cmd, argv[i] ? argv[i] : "");
2N/A }
2N/A rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
2N/A if( rc ){
2N/A sqlite_set_result_error(context, Tcl_GetStringResult(p->interp), -1);
2N/A }else{
2N/A sqlite_set_result_string(context, Tcl_GetStringResult(p->interp), -1);
2N/A }
2N/A}
2N/A#ifndef SQLITE_OMIT_AUTHORIZATION
2N/A/*
2N/A** This is the authentication function. It appends the authentication
2N/A** type code and the two arguments to zCmd[] then invokes the result
2N/A** on the interpreter. The reply is examined to determine if the
2N/A** authentication fails or succeeds.
2N/A*/
2N/Astatic int auth_callback(
2N/A void *pArg,
2N/A int code,
2N/A const char *zArg1,
2N/A const char *zArg2,
2N/A const char *zArg3,
2N/A const char *zArg4
2N/A){
2N/A char *zCode;
2N/A Tcl_DString str;
2N/A int rc;
2N/A const char *zReply;
2N/A SqliteDb *pDb = (SqliteDb*)pArg;
2N/A
2N/A switch( code ){
2N/A case SQLITE_COPY : zCode="SQLITE_COPY"; break;
2N/A case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
2N/A case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
2N/A case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
2N/A case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
2N/A case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
2N/A case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
2N/A case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
2N/A case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
2N/A case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
2N/A case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
2N/A case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
2N/A case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
2N/A case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
2N/A case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
2N/A case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
2N/A case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
2N/A case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
2N/A case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
2N/A case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
2N/A case SQLITE_READ : zCode="SQLITE_READ"; break;
2N/A case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
2N/A case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
2N/A case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
2N/A case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
2N/A case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
2N/A default : zCode="????"; break;
2N/A }
2N/A Tcl_DStringInit(&str);
2N/A Tcl_DStringAppend(&str, pDb->zAuth, -1);
2N/A Tcl_DStringAppendElement(&str, zCode);
2N/A Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
2N/A Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
2N/A Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
2N/A Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
2N/A rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
2N/A Tcl_DStringFree(&str);
2N/A zReply = Tcl_GetStringResult(pDb->interp);
2N/A if( strcmp(zReply,"SQLITE_OK")==0 ){
2N/A rc = SQLITE_OK;
2N/A }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
2N/A rc = SQLITE_DENY;
2N/A }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
2N/A rc = SQLITE_IGNORE;
2N/A }else{
2N/A rc = 999;
2N/A }
2N/A return rc;
2N/A}
2N/A#endif /* SQLITE_OMIT_AUTHORIZATION */
2N/A
2N/A/*
2N/A** The "sqlite" command below creates a new Tcl command for each
2N/A** connection it opens to an SQLite database. This routine is invoked
2N/A** whenever one of those connection-specific commands is executed
2N/A** in Tcl. For example, if you run Tcl code like this:
2N/A**
2N/A** sqlite db1 "my_database"
2N/A** db1 close
2N/A**
2N/A** The first command opens a connection to the "my_database" database
2N/A** and calls that connection "db1". The second command causes this
2N/A** subroutine to be invoked.
2N/A*/
2N/Astatic int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
2N/A SqliteDb *pDb = (SqliteDb*)cd;
2N/A int choice;
2N/A int rc = TCL_OK;
2N/A static const char *DB_strs[] = {
2N/A "authorizer", "busy", "changes",
2N/A "close", "commit_hook", "complete",
2N/A "errorcode", "eval", "function",
2N/A "last_insert_rowid", "last_statement_changes", "onecolumn",
2N/A "progress", "rekey", "timeout",
2N/A "trace",
2N/A 0
2N/A };
2N/A enum DB_enum {
2N/A DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
2N/A DB_CLOSE, DB_COMMIT_HOOK, DB_COMPLETE,
2N/A DB_ERRORCODE, DB_EVAL, DB_FUNCTION,
2N/A DB_LAST_INSERT_ROWID, DB_LAST_STATEMENT_CHANGES, DB_ONECOLUMN,
2N/A DB_PROGRESS, DB_REKEY, DB_TIMEOUT,
2N/A DB_TRACE
2N/A };
2N/A
2N/A if( objc<2 ){
2N/A Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
2N/A return TCL_ERROR;
2N/A }
2N/A if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
2N/A return TCL_ERROR;
2N/A }
2N/A
2N/A switch( (enum DB_enum)choice ){
2N/A
2N/A /* $db authorizer ?CALLBACK?
2N/A **
2N/A ** Invoke the given callback to authorize each SQL operation as it is
2N/A ** compiled. 5 arguments are appended to the callback before it is
2N/A ** invoked:
2N/A **
2N/A ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
2N/A ** (2) First descriptive name (depends on authorization type)
2N/A ** (3) Second descriptive name
2N/A ** (4) Name of the database (ex: "main", "temp")
2N/A ** (5) Name of trigger that is doing the access
2N/A **
2N/A ** The callback should return on of the following strings: SQLITE_OK,
2N/A ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
2N/A **
2N/A ** If this method is invoked with no arguments, the current authorization
2N/A ** callback string is returned.
2N/A */
2N/A case DB_AUTHORIZER: {
2N/A if( objc>3 ){
2N/A Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
2N/A }else if( objc==2 ){
2N/A if( pDb->zAuth ){
2N/A Tcl_AppendResult(interp, pDb->zAuth, 0);
2N/A }
2N/A }else{
2N/A char *zAuth;
2N/A int len;
2N/A if( pDb->zAuth ){
2N/A Tcl_Free(pDb->zAuth);
2N/A }
2N/A zAuth = Tcl_GetStringFromObj(objv[2], &len);
2N/A if( zAuth && len>0 ){
2N/A pDb->zAuth = Tcl_Alloc( len + 1 );
2N/A strcpy(pDb->zAuth, zAuth);
2N/A }else{
2N/A pDb->zAuth = 0;
2N/A }
2N/A#ifndef SQLITE_OMIT_AUTHORIZATION
2N/A if( pDb->zAuth ){
2N/A pDb->interp = interp;
2N/A sqlite_set_authorizer(pDb->db, auth_callback, pDb);
2N/A }else{
2N/A sqlite_set_authorizer(pDb->db, 0, 0);
2N/A }
2N/A#endif
2N/A }
2N/A break;
2N/A }
2N/A
2N/A /* $db busy ?CALLBACK?
2N/A **
2N/A ** Invoke the given callback if an SQL statement attempts to open
2N/A ** a locked database file.
2N/A */
2N/A case DB_BUSY: {
2N/A if( objc>3 ){
2N/A Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
2N/A return TCL_ERROR;
2N/A }else if( objc==2 ){
2N/A if( pDb->zBusy ){
2N/A Tcl_AppendResult(interp, pDb->zBusy, 0);
2N/A }
2N/A }else{
2N/A char *zBusy;
2N/A int len;
2N/A if( pDb->zBusy ){
2N/A Tcl_Free(pDb->zBusy);
2N/A }
2N/A zBusy = Tcl_GetStringFromObj(objv[2], &len);
2N/A if( zBusy && len>0 ){
2N/A pDb->zBusy = Tcl_Alloc( len + 1 );
2N/A strcpy(pDb->zBusy, zBusy);
2N/A }else{
2N/A pDb->zBusy = 0;
2N/A }
2N/A if( pDb->zBusy ){
2N/A pDb->interp = interp;
2N/A sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
2N/A }else{
2N/A sqlite_busy_handler(pDb->db, 0, 0);
2N/A }
2N/A }
2N/A break;
2N/A }
2N/A
2N/A /* $db progress ?N CALLBACK?
2N/A **
2N/A ** Invoke the given callback every N virtual machine opcodes while executing
2N/A ** queries.
2N/A */
2N/A case DB_PROGRESS: {
2N/A if( objc==2 ){
2N/A if( pDb->zProgress ){
2N/A Tcl_AppendResult(interp, pDb->zProgress, 0);
2N/A }
2N/A }else if( objc==4 ){
2N/A char *zProgress;
2N/A int len;
2N/A int N;
2N/A if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
2N/A return TCL_ERROR;
2N/A };
2N/A if( pDb->zProgress ){
2N/A Tcl_Free(pDb->zProgress);
2N/A }
2N/A zProgress = Tcl_GetStringFromObj(objv[3], &len);
2N/A if( zProgress && len>0 ){
2N/A pDb->zProgress = Tcl_Alloc( len + 1 );
2N/A strcpy(pDb->zProgress, zProgress);
2N/A }else{
2N/A pDb->zProgress = 0;
2N/A }
2N/A#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2N/A if( pDb->zProgress ){
2N/A pDb->interp = interp;
2N/A sqlite_progress_handler(pDb->db, N, DbProgressHandler, pDb);
2N/A }else{
2N/A sqlite_progress_handler(pDb->db, 0, 0, 0);
2N/A }
2N/A#endif
2N/A }else{
2N/A Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
2N/A return TCL_ERROR;
2N/A }
2N/A break;
2N/A }
2N/A
2N/A /*
2N/A ** $db changes
2N/A **
2N/A ** Return the number of rows that were modified, inserted, or deleted by
2N/A ** the most recent "eval".
2N/A */
2N/A case DB_CHANGES: {
2N/A Tcl_Obj *pResult;
2N/A int nChange;
2N/A if( objc!=2 ){
2N/A Tcl_WrongNumArgs(interp, 2, objv, "");
2N/A return TCL_ERROR;
2N/A }
2N/A nChange = sqlite_changes(pDb->db);
2N/A pResult = Tcl_GetObjResult(interp);
2N/A Tcl_SetIntObj(pResult, nChange);
2N/A break;
2N/A }
2N/A
2N/A /*
2N/A ** $db last_statement_changes
2N/A **
2N/A ** Return the number of rows that were modified, inserted, or deleted by
2N/A ** the last statment to complete execution (excluding changes due to
2N/A ** triggers)
2N/A */
2N/A case DB_LAST_STATEMENT_CHANGES: {
2N/A Tcl_Obj *pResult;
2N/A int lsChange;
2N/A if( objc!=2 ){
2N/A Tcl_WrongNumArgs(interp, 2, objv, "");
2N/A return TCL_ERROR;
2N/A }
2N/A lsChange = sqlite_last_statement_changes(pDb->db);
2N/A pResult = Tcl_GetObjResult(interp);
2N/A Tcl_SetIntObj(pResult, lsChange);
2N/A break;
2N/A }
2N/A
2N/A /* $db close
2N/A **
2N/A ** Shutdown the database
2N/A */
2N/A case DB_CLOSE: {
2N/A Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
2N/A break;
2N/A }
2N/A
2N/A /* $db commit_hook ?CALLBACK?
2N/A **
2N/A ** Invoke the given callback just before committing every SQL transaction.
2N/A ** If the callback throws an exception or returns non-zero, then the
2N/A ** transaction is aborted. If CALLBACK is an empty string, the callback
2N/A ** is disabled.
2N/A */
2N/A case DB_COMMIT_HOOK: {
2N/A if( objc>3 ){
2N/A Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
2N/A }else if( objc==2 ){
2N/A if( pDb->zCommit ){
2N/A Tcl_AppendResult(interp, pDb->zCommit, 0);
2N/A }
2N/A }else{
2N/A char *zCommit;
2N/A int len;
2N/A if( pDb->zCommit ){
2N/A Tcl_Free(pDb->zCommit);
2N/A }
2N/A zCommit = Tcl_GetStringFromObj(objv[2], &len);
2N/A if( zCommit && len>0 ){
2N/A pDb->zCommit = Tcl_Alloc( len + 1 );
2N/A strcpy(pDb->zCommit, zCommit);
2N/A }else{
2N/A pDb->zCommit = 0;
2N/A }
2N/A if( pDb->zCommit ){
2N/A pDb->interp = interp;
2N/A sqlite_commit_hook(pDb->db, DbCommitHandler, pDb);
2N/A }else{
2N/A sqlite_commit_hook(pDb->db, 0, 0);
2N/A }
2N/A }
2N/A break;
2N/A }
2N/A
2N/A /* $db complete SQL
2N/A **
2N/A ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
2N/A ** additional lines of input are needed. This is similar to the
2N/A ** built-in "info complete" command of Tcl.
2N/A */
2N/A case DB_COMPLETE: {
2N/A Tcl_Obj *pResult;
2N/A int isComplete;
2N/A if( objc!=3 ){
2N/A Tcl_WrongNumArgs(interp, 2, objv, "SQL");
2N/A return TCL_ERROR;
2N/A }
2N/A isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
2N/A pResult = Tcl_GetObjResult(interp);
2N/A Tcl_SetBooleanObj(pResult, isComplete);
2N/A break;
2N/A }
2N/A
2N/A /*
2N/A ** $db errorcode
2N/A **
2N/A ** Return the numeric error code that was returned by the most recent
2N/A ** call to sqlite_exec().
2N/A */
2N/A case DB_ERRORCODE: {
2N/A Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc));
2N/A break;
2N/A }
2N/A
2N/A /*
2N/A ** $db eval $sql ?array { ...code... }?
2N/A **
2N/A ** The SQL statement in $sql is evaluated. For each row, the values are
2N/A ** placed in elements of the array named "array" and ...code... is executed.
2N/A ** If "array" and "code" are omitted, then no callback is every invoked.
2N/A ** If "array" is an empty string, then the values are placed in variables
2N/A ** that have the same name as the fields extracted by the query.
2N/A */
2N/A case DB_EVAL: {
2N/A CallbackData cbData;
2N/A char *zErrMsg;
2N/A char *zSql;
2N/A#ifdef UTF_TRANSLATION_NEEDED
2N/A Tcl_DString dSql;
2N/A int i;
2N/A#endif
2N/A
2N/A if( objc!=5 && objc!=3 ){
2N/A Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
2N/A return TCL_ERROR;
2N/A }
2N/A pDb->interp = interp;
2N/A zSql = Tcl_GetStringFromObj(objv[2], 0);
2N/A#ifdef UTF_TRANSLATION_NEEDED
2N/A Tcl_DStringInit(&dSql);
2N/A Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
2N/A zSql = Tcl_DStringValue(&dSql);
2N/A#endif
2N/A Tcl_IncrRefCount(objv[2]);
2N/A if( objc==5 ){
2N/A cbData.interp = interp;
2N/A cbData.once = 1;
2N/A cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
2N/A cbData.pCode = objv[4];
2N/A cbData.tcl_rc = TCL_OK;
2N/A cbData.nColName = 0;
2N/A cbData.azColName = 0;
2N/A zErrMsg = 0;
2N/A Tcl_IncrRefCount(objv[3]);
2N/A Tcl_IncrRefCount(objv[4]);
2N/A rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
2N/A Tcl_DecrRefCount(objv[4]);
2N/A Tcl_DecrRefCount(objv[3]);
2N/A if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
2N/A }else{
2N/A Tcl_Obj *pList = Tcl_NewObj();
2N/A cbData.tcl_rc = TCL_OK;
2N/A rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
2N/A Tcl_SetObjResult(interp, pList);
2N/A }
2N/A pDb->rc = rc;
2N/A if( rc==SQLITE_ABORT ){
2N/A if( zErrMsg ) free(zErrMsg);
2N/A rc = cbData.tcl_rc;
2N/A }else if( zErrMsg ){
2N/A Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
2N/A free(zErrMsg);
2N/A rc = TCL_ERROR;
2N/A }else if( rc!=SQLITE_OK ){
2N/A Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
2N/A rc = TCL_ERROR;
2N/A }else{
2N/A }
2N/A Tcl_DecrRefCount(objv[2]);
2N/A#ifdef UTF_TRANSLATION_NEEDED
2N/A Tcl_DStringFree(&dSql);
2N/A if( objc==5 && cbData.azColName ){
2N/A for(i=0; i<cbData.nColName; i++){
2N/A if( cbData.azColName[i] ) free(cbData.azColName[i]);
2N/A }
2N/A free(cbData.azColName);
2N/A cbData.azColName = 0;
2N/A }
2N/A#endif
2N/A return rc;
2N/A }
2N/A
2N/A /*
2N/A ** $db function NAME SCRIPT
2N/A **
2N/A ** Create a new SQL function called NAME. Whenever that function is
2N/A ** called, invoke SCRIPT to evaluate the function.
2N/A */
2N/A case DB_FUNCTION: {
2N/A SqlFunc *pFunc;
2N/A char *zName;
2N/A char *zScript;
2N/A int nScript;
2N/A if( objc!=4 ){
2N/A Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
2N/A return TCL_ERROR;
2N/A }
2N/A zName = Tcl_GetStringFromObj(objv[2], 0);
2N/A zScript = Tcl_GetStringFromObj(objv[3], &nScript);
2N/A pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
2N/A if( pFunc==0 ) return TCL_ERROR;
2N/A pFunc->interp = interp;
2N/A pFunc->pNext = pDb->pFunc;
2N/A pFunc->zScript = (char*)&pFunc[1];
2N/A strcpy(pFunc->zScript, zScript);
2N/A sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc);
2N/A sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC);
2N/A break;
2N/A }
2N/A
2N/A /*
2N/A ** $db last_insert_rowid
2N/A **
2N/A ** Return an integer which is the ROWID for the most recent insert.
2N/A */
2N/A case DB_LAST_INSERT_ROWID: {
2N/A Tcl_Obj *pResult;
2N/A int rowid;
2N/A if( objc!=2 ){
2N/A Tcl_WrongNumArgs(interp, 2, objv, "");
2N/A return TCL_ERROR;
2N/A }
2N/A rowid = sqlite_last_insert_rowid(pDb->db);
2N/A pResult = Tcl_GetObjResult(interp);
2N/A Tcl_SetIntObj(pResult, rowid);
2N/A break;
2N/A }
2N/A
2N/A /*
2N/A ** $db onecolumn SQL
2N/A **
2N/A ** Return a single column from a single row of the given SQL query.
2N/A */
2N/A case DB_ONECOLUMN: {
2N/A char *zSql;
2N/A char *zErrMsg = 0;
2N/A if( objc!=3 ){
2N/A Tcl_WrongNumArgs(interp, 2, objv, "SQL");
2N/A return TCL_ERROR;
2N/A }
2N/A zSql = Tcl_GetStringFromObj(objv[2], 0);
2N/A rc = sqlite_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
2N/A if( rc==SQLITE_ABORT ){
2N/A rc = SQLITE_OK;
2N/A }else if( zErrMsg ){
2N/A Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
2N/A free(zErrMsg);
2N/A rc = TCL_ERROR;
2N/A }else if( rc!=SQLITE_OK ){
2N/A Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
2N/A rc = TCL_ERROR;
2N/A }
2N/A break;
2N/A }
2N/A
2N/A /*
2N/A ** $db rekey KEY
2N/A **
2N/A ** Change the encryption key on the currently open database.
2N/A */
2N/A case DB_REKEY: {
2N/A int nKey;
2N/A void *pKey;
2N/A if( objc!=3 ){
2N/A Tcl_WrongNumArgs(interp, 2, objv, "KEY");
2N/A return TCL_ERROR;
2N/A }
2N/A pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
2N/A#ifdef SQLITE_HAS_CODEC
2N/A rc = sqlite_rekey(pDb->db, pKey, nKey);
2N/A if( rc ){
2N/A Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
2N/A rc = TCL_ERROR;
2N/A }
2N/A#endif
2N/A break;
2N/A }
2N/A
2N/A /*
2N/A ** $db timeout MILLESECONDS
2N/A **
2N/A ** Delay for the number of milliseconds specified when a file is locked.
2N/A */
2N/A case DB_TIMEOUT: {
2N/A int ms;
2N/A if( objc!=3 ){
2N/A Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
2N/A return TCL_ERROR;
2N/A }
2N/A if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
2N/A sqlite_busy_timeout(pDb->db, ms);
2N/A break;
2N/A }
2N/A
2N/A /* $db trace ?CALLBACK?
2N/A **
2N/A ** Make arrangements to invoke the CALLBACK routine for each SQL statement
2N/A ** that is executed. The text of the SQL is appended to CALLBACK before
2N/A ** it is executed.
2N/A */
2N/A case DB_TRACE: {
2N/A if( objc>3 ){
2N/A Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
2N/A }else if( objc==2 ){
2N/A if( pDb->zTrace ){
2N/A Tcl_AppendResult(interp, pDb->zTrace, 0);
2N/A }
2N/A }else{
2N/A char *zTrace;
2N/A int len;
2N/A if( pDb->zTrace ){
2N/A Tcl_Free(pDb->zTrace);
2N/A }
2N/A zTrace = Tcl_GetStringFromObj(objv[2], &len);
2N/A if( zTrace && len>0 ){
2N/A pDb->zTrace = Tcl_Alloc( len + 1 );
2N/A strcpy(pDb->zTrace, zTrace);
2N/A }else{
2N/A pDb->zTrace = 0;
2N/A }
2N/A if( pDb->zTrace ){
2N/A pDb->interp = interp;
2N/A sqlite_trace(pDb->db, DbTraceHandler, pDb);
2N/A }else{
2N/A sqlite_trace(pDb->db, 0, 0);
2N/A }
2N/A }
2N/A break;
2N/A }
2N/A
2N/A } /* End of the SWITCH statement */
2N/A return rc;
2N/A}
2N/A
2N/A/*
2N/A** sqlite DBNAME FILENAME ?MODE? ?-key KEY?
2N/A**
2N/A** This is the main Tcl command. When the "sqlite" Tcl command is
2N/A** invoked, this routine runs to process that command.
2N/A**
2N/A** The first argument, DBNAME, is an arbitrary name for a new
2N/A** database connection. This command creates a new command named
2N/A** DBNAME that is used to control that connection. The database
2N/A** connection is deleted when the DBNAME command is deleted.
2N/A**
2N/A** The second argument is the name of the directory that contains
2N/A** the sqlite database that is to be accessed.
2N/A**
2N/A** For testing purposes, we also support the following:
2N/A**
2N/A** sqlite -encoding
2N/A**
2N/A** Return the encoding used by LIKE and GLOB operators. Choices
2N/A** are UTF-8 and iso8859.
2N/A**
2N/A** sqlite -version
2N/A**
2N/A** Return the version number of the SQLite library.
2N/A**
2N/A** sqlite -tcl-uses-utf
2N/A**
2N/A** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
2N/A** not. Used by tests to make sure the library was compiled
2N/A** correctly.
2N/A*/
2N/Astatic int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
2N/A int mode;
2N/A SqliteDb *p;
2N/A void *pKey = 0;
2N/A int nKey = 0;
2N/A const char *zArg;
2N/A char *zErrMsg;
2N/A const char *zFile;
2N/A char zBuf[80];
2N/A if( objc==2 ){
2N/A zArg = Tcl_GetStringFromObj(objv[1], 0);
2N/A if( strcmp(zArg,"-encoding")==0 ){
2N/A Tcl_AppendResult(interp,sqlite_encoding,0);
2N/A return TCL_OK;
2N/A }
2N/A if( strcmp(zArg,"-version")==0 ){
2N/A Tcl_AppendResult(interp,sqlite_version,0);
2N/A return TCL_OK;
2N/A }
2N/A if( strcmp(zArg,"-has-codec")==0 ){
2N/A#ifdef SQLITE_HAS_CODEC
2N/A Tcl_AppendResult(interp,"1",0);
2N/A#else
2N/A Tcl_AppendResult(interp,"0",0);
2N/A#endif
2N/A return TCL_OK;
2N/A }
2N/A if( strcmp(zArg,"-tcl-uses-utf")==0 ){
2N/A#ifdef TCL_UTF_MAX
2N/A Tcl_AppendResult(interp,"1",0);
2N/A#else
2N/A Tcl_AppendResult(interp,"0",0);
2N/A#endif
2N/A return TCL_OK;
2N/A }
2N/A }
2N/A if( objc==5 || objc==6 ){
2N/A zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
2N/A if( strcmp(zArg,"-key")==0 ){
2N/A pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
2N/A objc -= 2;
2N/A }
2N/A }
2N/A if( objc!=3 && objc!=4 ){
2N/A Tcl_WrongNumArgs(interp, 1, objv,
2N/A#ifdef SQLITE_HAS_CODEC
2N/A "HANDLE FILENAME ?-key CODEC-KEY?"
2N/A#else
2N/A "HANDLE FILENAME ?MODE?"
2N/A#endif
2N/A );
2N/A return TCL_ERROR;
2N/A }
2N/A if( objc==3 ){
2N/A mode = 0666;
2N/A }else if( Tcl_GetIntFromObj(interp, objv[3], &mode)!=TCL_OK ){
2N/A return TCL_ERROR;
2N/A }
2N/A zErrMsg = 0;
2N/A p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
2N/A if( p==0 ){
2N/A Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
2N/A return TCL_ERROR;
2N/A }
2N/A memset(p, 0, sizeof(*p));
2N/A zFile = Tcl_GetStringFromObj(objv[2], 0);
2N/A#ifdef SQLITE_HAS_CODEC
2N/A p->db = sqlite_open_encrypted(zFile, pKey, nKey, 0, &zErrMsg);
2N/A#else
2N/A p->db = sqlite_open(zFile, mode, &zErrMsg);
2N/A#endif
2N/A if( p->db==0 ){
2N/A Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
2N/A Tcl_Free((char*)p);
2N/A free(zErrMsg);
2N/A return TCL_ERROR;
2N/A }
2N/A zArg = Tcl_GetStringFromObj(objv[1], 0);
2N/A Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
2N/A
2N/A /* The return value is the value of the sqlite* pointer
2N/A */
2N/A sprintf(zBuf, "%p", p->db);
2N/A if( strncmp(zBuf,"0x",2) ){
2N/A sprintf(zBuf, "0x%p", p->db);
2N/A }
2N/A Tcl_AppendResult(interp, zBuf, 0);
2N/A
2N/A /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
2N/A ** SQL function.
2N/A */
2N/A#ifdef SQLITE_TEST
2N/A {
2N/A extern void Md5_Register(sqlite*);
2N/A Md5_Register(p->db);
2N/A }
2N/A#endif
2N/A return TCL_OK;
2N/A}
2N/A
2N/A/*
2N/A** Provide a dummy Tcl_InitStubs if we are using this as a static
2N/A** library.
2N/A*/
2N/A#ifndef USE_TCL_STUBS
2N/A# undef Tcl_InitStubs
2N/A# define Tcl_InitStubs(a,b,c)
2N/A#endif
2N/A
2N/A/*
2N/A** Initialize this module.
2N/A**
2N/A** This Tcl module contains only a single new Tcl command named "sqlite".
2N/A** (Hence there is no namespace. There is no point in using a namespace
2N/A** if the extension only supplies one new name!) The "sqlite" command is
2N/A** used to open a new SQLite database. See the DbMain() routine above
2N/A** for additional information.
2N/A*/
2N/Aint Sqlite_Init(Tcl_Interp *interp){
2N/A Tcl_InitStubs(interp, "8.0", 0);
2N/A Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
2N/A Tcl_PkgProvide(interp, "sqlite", "2.0");
2N/A return TCL_OK;
2N/A}
2N/Aint Tclsqlite_Init(Tcl_Interp *interp){
2N/A Tcl_InitStubs(interp, "8.0", 0);
2N/A Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
2N/A Tcl_PkgProvide(interp, "sqlite", "2.0");
2N/A return TCL_OK;
2N/A}
2N/Aint Sqlite_SafeInit(Tcl_Interp *interp){
2N/A return TCL_OK;
2N/A}
2N/Aint Tclsqlite_SafeInit(Tcl_Interp *interp){
2N/A return TCL_OK;
2N/A}
2N/A
2N/A#if 0
2N/A/*
2N/A** If compiled using mktclapp, this routine runs to initialize
2N/A** everything.
2N/A*/
2N/Aint Et_AppInit(Tcl_Interp *interp){
2N/A return Sqlite_Init(interp);
2N/A}
2N/A#endif
2N/A/***************************************************************************
2N/A** The remaining code is only included if the TCLSH macro is defined to
2N/A** be an integer greater than 0
2N/A*/
2N/A#if defined(TCLSH) && TCLSH>0
2N/A
2N/A/*
2N/A** If the macro TCLSH is defined and is one, then put in code for the
2N/A** "main" routine that implement a interactive shell into which the user
2N/A** can type TCL commands.
2N/A*/
2N/A#if TCLSH==1
2N/Astatic char zMainloop[] =
2N/A "set line {}\n"
2N/A "while {![eof stdin]} {\n"
2N/A "if {$line!=\"\"} {\n"
2N/A "puts -nonewline \"> \"\n"
2N/A "} else {\n"
2N/A "puts -nonewline \"% \"\n"
2N/A "}\n"
2N/A "flush stdout\n"
2N/A "append line [gets stdin]\n"
2N/A "if {[info complete $line]} {\n"
2N/A "if {[catch {uplevel #0 $line} result]} {\n"
2N/A "puts stderr \"Error: $result\"\n"
2N/A "} elseif {$result!=\"\"} {\n"
2N/A "puts $result\n"
2N/A "}\n"
2N/A "set line {}\n"
2N/A "} else {\n"
2N/A "append line \\n\n"
2N/A "}\n"
2N/A "}\n"
2N/A;
2N/A#endif /* TCLSH==1 */
2N/A
2N/Aint Libsqlite_Init( Tcl_Interp *interp) {
2N/A#ifdef TCL_THREADS
2N/A if (Thread_Init(interp) == TCL_ERROR) {
2N/A return TCL_ERROR;
2N/A }
2N/A#endif
2N/A Sqlite_Init(interp);
2N/A#ifdef SQLITE_TEST
2N/A {
2N/A extern int Sqlitetest1_Init(Tcl_Interp*);
2N/A extern int Sqlitetest2_Init(Tcl_Interp*);
2N/A extern int Sqlitetest3_Init(Tcl_Interp*);
2N/A extern int Md5_Init(Tcl_Interp*);
2N/A Sqlitetest1_Init(interp);
2N/A Sqlitetest2_Init(interp);
2N/A Sqlitetest3_Init(interp);
2N/A Md5_Init(interp);
2N/A Tcl_StaticPackage(interp, "sqlite", Libsqlite_Init, Libsqlite_Init);
2N/A }
2N/A#endif
2N/A return TCL_OK;
2N/A}
2N/A
2N/A#define TCLSH_MAIN main /* Needed to fake out mktclapp */
2N/A#if TCLSH==1
2N/Aint TCLSH_MAIN(int argc, char **argv){
2N/A#ifndef TCL_THREADS
2N/A Tcl_Interp *interp;
2N/A Tcl_FindExecutable(argv[0]);
2N/A interp = Tcl_CreateInterp();
2N/A Libsqlite_Init(interp);
2N/A if( argc>=2 ){
2N/A int i;
2N/A Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
2N/A Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
2N/A for(i=2; i<argc; i++){
2N/A Tcl_SetVar(interp, "argv", argv[i],
2N/A TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
2N/A }
2N/A if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
2N/A const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
2N/A if( zInfo==0 ) zInfo = interp->result;
2N/A fprintf(stderr,"%s: %s\n", *argv, zInfo);
2N/A return TCL_ERROR;
2N/A }
2N/A }else{
2N/A Tcl_GlobalEval(interp, zMainloop);
2N/A }
2N/A return 0;
2N/A#else
2N/A Tcl_Main(argc, argv, Libsqlite_Init);
2N/A#endif /* TCL_THREADS */
2N/A return 0;
2N/A}
2N/A#endif /* TCLSH==1 */
2N/A
2N/A
2N/A/*
2N/A** If the macro TCLSH is set to 2, then implement a space analysis tool.
2N/A*/
2N/A#if TCLSH==2
2N/Astatic char zAnalysis[] =
2N/A#include "spaceanal_tcl.h"
2N/A;
2N/A
2N/Aint main(int argc, char **argv){
2N/A Tcl_Interp *interp;
2N/A int i;
2N/A Tcl_FindExecutable(argv[0]);
2N/A interp = Tcl_CreateInterp();
2N/A Libsqlite_Init(interp);
2N/A Tcl_SetVar(interp,"argv0",argv[0],TCL_GLOBAL_ONLY);
2N/A Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
2N/A for(i=1; i<argc; i++){
2N/A Tcl_SetVar(interp, "argv", argv[i],
2N/A TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
2N/A }
2N/A if( Tcl_GlobalEval(interp, zAnalysis)!=TCL_OK ){
2N/A const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
2N/A if( zInfo==0 ) zInfo = interp->result;
2N/A fprintf(stderr,"%s: %s\n", *argv, zInfo);
2N/A return TCL_ERROR;
2N/A }
2N/A return 0;
2N/A}
2N/A#endif /* TCLSH==2 */
2N/A
2N/A#endif /* TCLSH */
2N/A
2N/A#endif /* NO_TCL */