175897234Sdrh /* 275897234Sdrh ** Copyright (c) 1999, 2000 D. Richard Hipp 375897234Sdrh ** 475897234Sdrh ** This program is free software; you can redistribute it and/or 575897234Sdrh ** modify it under the terms of the GNU General Public 675897234Sdrh ** License as published by the Free Software Foundation; either 775897234Sdrh ** version 2 of the License, or (at your option) any later version. 875897234Sdrh ** 975897234Sdrh ** This program is distributed in the hope that it will be useful, 1075897234Sdrh ** but WITHOUT ANY WARRANTY; without even the implied warranty of 1175897234Sdrh ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1275897234Sdrh ** General Public License for more details. 1375897234Sdrh ** 1475897234Sdrh ** You should have received a copy of the GNU General Public 1575897234Sdrh ** License along with this library; if not, write to the 1675897234Sdrh ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, 1775897234Sdrh ** Boston, MA 02111-1307, USA. 1875897234Sdrh ** 1975897234Sdrh ** Author contact information: 2075897234Sdrh ** [email protected] 2175897234Sdrh ** http://www.hwaci.com/drh/ 2275897234Sdrh ** 2375897234Sdrh ************************************************************************* 2475897234Sdrh ** A TCL Interface to SQLite 2575897234Sdrh ** 26*167a4b1cSdrh ** $Id: tclsqlite.c,v 1.8 2000/08/17 09:50:00 drh Exp $ 2775897234Sdrh */ 2875897234Sdrh #include "sqlite.h" 2975897234Sdrh #include <tcl.h> 3075897234Sdrh #include <stdlib.h> 3175897234Sdrh #include <string.h> 3275897234Sdrh 3375897234Sdrh /* 34bec3f402Sdrh ** There is one instance of this structure for each SQLite database 35bec3f402Sdrh ** that has been opened by the SQLite TCL interface. 36bec3f402Sdrh */ 37bec3f402Sdrh typedef struct SqliteDb SqliteDb; 38bec3f402Sdrh struct SqliteDb { 39bec3f402Sdrh sqlite *db; /* The "real" database structure */ 40bec3f402Sdrh Tcl_Interp *interp; /* The interpreter used for this database */ 41bec3f402Sdrh char *zBusy; /* The name of the busy callback routine */ 42bec3f402Sdrh }; 43bec3f402Sdrh 44bec3f402Sdrh /* 4575897234Sdrh ** An instance of this structure passes information thru the sqlite 4675897234Sdrh ** logic from the original TCL command into the callback routine. 4775897234Sdrh */ 4875897234Sdrh typedef struct CallbackData CallbackData; 4975897234Sdrh struct CallbackData { 5075897234Sdrh Tcl_Interp *interp; /* The TCL interpreter */ 5175897234Sdrh char *zArray; /* The array into which data is written */ 5275897234Sdrh char *zCode; /* The code to execute for each row */ 5375897234Sdrh int once; /* Set only for the first invocation of callback */ 5475897234Sdrh }; 5575897234Sdrh 5675897234Sdrh /* 5775897234Sdrh ** Called for each row of the result. 5875897234Sdrh */ 5975897234Sdrh static int DbEvalCallback( 6075897234Sdrh void *clientData, /* An instance of CallbackData */ 6175897234Sdrh int nCol, /* Number of columns in the result */ 6275897234Sdrh char ** azCol, /* Data for each column */ 6375897234Sdrh char ** azN /* Name for each column */ 6475897234Sdrh ){ 6575897234Sdrh CallbackData *cbData = (CallbackData*)clientData; 6675897234Sdrh int i, rc; 6775897234Sdrh if( cbData->zArray[0] ){ 6875897234Sdrh if( cbData->once ){ 6975897234Sdrh for(i=0; i<nCol; i++){ 7075897234Sdrh Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i], 7175897234Sdrh TCL_LIST_ELEMENT|TCL_APPEND_VALUE); 7275897234Sdrh } 7375897234Sdrh } 7475897234Sdrh for(i=0; i<nCol; i++){ 75c61053b7Sdrh char *z = azCol[i]; 76c61053b7Sdrh if( z==0 ) z = ""; 77c61053b7Sdrh Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0); 7875897234Sdrh } 7975897234Sdrh }else{ 8075897234Sdrh for(i=0; i<nCol; i++){ 81c61053b7Sdrh char *z = azCol[i]; 82c61053b7Sdrh if( z==0 ) z = ""; 83c61053b7Sdrh Tcl_SetVar(cbData->interp, azN[i], z, 0); 8475897234Sdrh } 8575897234Sdrh } 8675897234Sdrh cbData->once = 0; 8775897234Sdrh rc = Tcl_Eval(cbData->interp, cbData->zCode); 8875897234Sdrh return rc; 8975897234Sdrh } 9075897234Sdrh 9175897234Sdrh /* 9275897234Sdrh ** Called when the command is deleted. 9375897234Sdrh */ 9475897234Sdrh static void DbDeleteCmd(void *db){ 95bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)db; 96bec3f402Sdrh sqlite_close(pDb->db); 97bec3f402Sdrh if( pDb->zBusy ){ 98bec3f402Sdrh Tcl_Free(pDb->zBusy); 99bec3f402Sdrh } 100bec3f402Sdrh Tcl_Free((char*)pDb); 101bec3f402Sdrh } 102bec3f402Sdrh 103bec3f402Sdrh /* 104bec3f402Sdrh ** This routine is called when a database file is locked while trying 105bec3f402Sdrh ** to execute SQL. 106bec3f402Sdrh */ 107bec3f402Sdrh static int DbBusyHandler(void *cd, const char *zTable, int nTries){ 108bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 109bec3f402Sdrh int rc; 110bec3f402Sdrh char zVal[30]; 111bec3f402Sdrh char *zCmd; 112bec3f402Sdrh char *zResult; 113bec3f402Sdrh Tcl_DString cmd; 114bec3f402Sdrh 115bec3f402Sdrh Tcl_DStringInit(&cmd); 116bec3f402Sdrh Tcl_DStringAppend(&cmd, pDb->zBusy, -1); 117bec3f402Sdrh Tcl_DStringAppendElement(&cmd, zTable); 118bec3f402Sdrh sprintf(zVal, " %d", nTries); 119bec3f402Sdrh Tcl_DStringAppend(&cmd, zVal, -1); 120bec3f402Sdrh zCmd = Tcl_DStringValue(&cmd); 121bec3f402Sdrh rc = Tcl_Eval(pDb->interp, zCmd); 122bec3f402Sdrh Tcl_DStringFree(&cmd); 123bec3f402Sdrh if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){ 124bec3f402Sdrh return 0; 125bec3f402Sdrh } 126bec3f402Sdrh return 1; 12775897234Sdrh } 12875897234Sdrh 12975897234Sdrh /* 13075897234Sdrh ** The "sqlite" command below creates a new Tcl command for each 13175897234Sdrh ** connection it opens to an SQLite database. This routine is invoked 13275897234Sdrh ** whenever one of those connection-specific commands is executed 13375897234Sdrh ** in Tcl. For example, if you run Tcl code like this: 13475897234Sdrh ** 13575897234Sdrh ** sqlite db1 "my_database" 13675897234Sdrh ** db1 close 13775897234Sdrh ** 13875897234Sdrh ** The first command opens a connection to the "my_database" database 13975897234Sdrh ** and calls that connection "db1". The second command causes this 14075897234Sdrh ** subroutine to be invoked. 14175897234Sdrh */ 14275897234Sdrh static int DbCmd(void *cd, Tcl_Interp *interp, int argc, char **argv){ 14375897234Sdrh char *z; 14475897234Sdrh int n, c; 145bec3f402Sdrh SqliteDb *pDb = (SqliteDb*)cd; 14675897234Sdrh if( argc<2 ){ 14775897234Sdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 14875897234Sdrh " SUBCOMMAND ...\"", 0); 14975897234Sdrh return TCL_ERROR; 15075897234Sdrh } 15175897234Sdrh z = argv[1]; 15275897234Sdrh n = strlen(z); 15375897234Sdrh c = z[0]; 15475897234Sdrh 155bec3f402Sdrh /* $db busy ?CALLBACK? 156bec3f402Sdrh ** 157bec3f402Sdrh ** Invoke the given callback if an SQL statement attempts to open 158bec3f402Sdrh ** a locked database file. 159bec3f402Sdrh */ 160bec3f402Sdrh if( c=='b' && strncmp(z,"busy",n)==0 ){ 161bec3f402Sdrh if( argc>3 ){ 162bec3f402Sdrh Tcl_AppendResult(interp,"wrong # args: should be \"", 163bec3f402Sdrh argv[0], " busy ?CALLBACK?", 0); 164bec3f402Sdrh return TCL_ERROR; 165bec3f402Sdrh }else if( argc==2 ){ 166bec3f402Sdrh if( pDb->zBusy ){ 167bec3f402Sdrh Tcl_AppendResult(interp, pDb->zBusy, 0); 168bec3f402Sdrh } 169bec3f402Sdrh }else{ 170bec3f402Sdrh if( pDb->zBusy ){ 171bec3f402Sdrh Tcl_Free(pDb->zBusy); 172bec3f402Sdrh pDb->zBusy = 0; 173bec3f402Sdrh } 174bec3f402Sdrh if( argv[2][0] ){ 175bec3f402Sdrh pDb->zBusy = Tcl_Alloc( strlen(argv[2]) + 1 ); 176bec3f402Sdrh if( pDb->zBusy ){ 177bec3f402Sdrh strcpy(pDb->zBusy, argv[2]); 178bec3f402Sdrh } 179bec3f402Sdrh } 180bec3f402Sdrh if( pDb->zBusy ){ 181bec3f402Sdrh pDb->interp = interp; 182bec3f402Sdrh sqlite_busy_handler(pDb->db, DbBusyHandler, pDb); 183bec3f402Sdrh } 184bec3f402Sdrh } 185bec3f402Sdrh }else 186bec3f402Sdrh 18775897234Sdrh /* $db close 18875897234Sdrh ** 18975897234Sdrh ** Shutdown the database 19075897234Sdrh */ 19175897234Sdrh if( c=='c' && n>=2 && strncmp(z,"close",n)==0 ){ 19275897234Sdrh Tcl_DeleteCommand(interp, argv[0]); 19375897234Sdrh }else 19475897234Sdrh 19575897234Sdrh /* $db complete SQL 19675897234Sdrh ** 19775897234Sdrh ** Return TRUE if SQL is a complete SQL statement. Return FALSE if 19875897234Sdrh ** additional lines of input are needed. This is similar to the 19975897234Sdrh ** built-in "info complete" command of Tcl. 20075897234Sdrh */ 20175897234Sdrh if( c=='c' && n>=2 && strncmp(z,"complete",n)==0 ){ 20275897234Sdrh char *zRes; 20375897234Sdrh if( argc!=3 ){ 20475897234Sdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 20575897234Sdrh " complete SQL\"", 0); 20675897234Sdrh return TCL_ERROR; 20775897234Sdrh } 20875897234Sdrh zRes = sqlite_complete(argv[2]) ? "1" : "0"; 20975897234Sdrh Tcl_SetResult(interp, zRes, TCL_VOLATILE); 21075897234Sdrh }else 21175897234Sdrh 21275897234Sdrh /* 21375897234Sdrh ** $db eval $sql ?array { ...code... }? 21475897234Sdrh ** 21575897234Sdrh ** The SQL statement in $sql is evaluated. For each row, the values are 216bec3f402Sdrh ** placed in elements of the array named "array" and ...code... is executed. 21775897234Sdrh ** If "array" and "code" are omitted, then no callback is every invoked. 21875897234Sdrh ** If "array" is an empty string, then the values are placed in variables 21975897234Sdrh ** that have the same name as the fields extracted by the query. 22075897234Sdrh */ 22175897234Sdrh if( c=='e' && strncmp(z,"eval",n)==0 ){ 22275897234Sdrh CallbackData cbData; 22375897234Sdrh char *zErrMsg; 22475897234Sdrh int rc; 22575897234Sdrh 22675897234Sdrh if( argc!=5 && argc!=3 ){ 22775897234Sdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 22875897234Sdrh " eval SQL ?ARRAY-NAME CODE?", 0); 22975897234Sdrh return TCL_ERROR; 23075897234Sdrh } 231bec3f402Sdrh pDb->interp = interp; 23275897234Sdrh if( argc==5 ){ 23375897234Sdrh cbData.interp = interp; 234dcc581ccSdrh cbData.once = 1; 23575897234Sdrh cbData.zArray = argv[3]; 23675897234Sdrh cbData.zCode = argv[4]; 23775897234Sdrh zErrMsg = 0; 238bec3f402Sdrh rc = sqlite_exec(pDb->db, argv[2], DbEvalCallback, &cbData, &zErrMsg); 23975897234Sdrh }else{ 240bec3f402Sdrh rc = sqlite_exec(pDb->db, argv[2], 0, 0, &zErrMsg); 24175897234Sdrh } 24275897234Sdrh if( zErrMsg ){ 24375897234Sdrh Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 24475897234Sdrh free(zErrMsg); 24575897234Sdrh } 24675897234Sdrh return rc; 247bec3f402Sdrh }else 248bec3f402Sdrh 249bec3f402Sdrh /* 250bec3f402Sdrh ** $db timeout MILLESECONDS 251bec3f402Sdrh ** 252bec3f402Sdrh ** Delay for the number of milliseconds specified when a file is locked. 253bec3f402Sdrh */ 254bec3f402Sdrh if( c=='t' && strncmp(z,"timeout",n)==0 ){ 255bec3f402Sdrh int ms; 256bec3f402Sdrh if( argc!=3 ){ 257bec3f402Sdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 258bec3f402Sdrh " timeout MILLISECONDS", 0); 259bec3f402Sdrh return TCL_ERROR; 26075897234Sdrh } 261bec3f402Sdrh if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR; 262bec3f402Sdrh sqlite_busy_timeout(pDb->db, ms); 263bec3f402Sdrh }else 26475897234Sdrh 26575897234Sdrh /* The default 26675897234Sdrh */ 267bec3f402Sdrh { 26875897234Sdrh Tcl_AppendResult(interp,"unknown subcommand \"", z, 26975897234Sdrh "\" - should be one of: close complete eval", 0); 27075897234Sdrh return TCL_ERROR; 27175897234Sdrh } 27275897234Sdrh return TCL_OK; 27375897234Sdrh } 27475897234Sdrh 27575897234Sdrh /* 27675897234Sdrh ** sqlite DBNAME FILENAME ?MODE? 27775897234Sdrh ** 27875897234Sdrh ** This is the main Tcl command. When the "sqlite" Tcl command is 27975897234Sdrh ** invoked, this routine runs to process that command. 28075897234Sdrh ** 28175897234Sdrh ** The first argument, DBNAME, is an arbitrary name for a new 28275897234Sdrh ** database connection. This command creates a new command named 28375897234Sdrh ** DBNAME that is used to control that connection. The database 28475897234Sdrh ** connection is deleted when the DBNAME command is deleted. 28575897234Sdrh ** 28675897234Sdrh ** The second argument is the name of the directory that contains 28775897234Sdrh ** the sqlite database that is to be accessed. 28875897234Sdrh */ 28975897234Sdrh static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){ 29075897234Sdrh int mode; 291bec3f402Sdrh SqliteDb *p; 29275897234Sdrh char *zErrMsg; 29375897234Sdrh if( argc!=3 && argc!=4 ){ 29475897234Sdrh Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 29575897234Sdrh " HANDLE FILENAME ?MODE?\"", 0); 29675897234Sdrh return TCL_ERROR; 29775897234Sdrh } 29875897234Sdrh if( argc==3 ){ 29958b9576bSdrh mode = 0666; 30075897234Sdrh }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){ 30175897234Sdrh return TCL_ERROR; 30275897234Sdrh } 30375897234Sdrh zErrMsg = 0; 3044cdc9e84Sdrh p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); 30575897234Sdrh if( p==0 ){ 306bec3f402Sdrh Tcl_SetResult(interp, "malloc failed", TCL_STATIC); 307bec3f402Sdrh return TCL_ERROR; 308bec3f402Sdrh } 309bec3f402Sdrh memset(p, 0, sizeof(*p)); 310bec3f402Sdrh p->db = sqlite_open(argv[2], mode, &zErrMsg); 311bec3f402Sdrh if( p->db==0 ){ 31275897234Sdrh Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); 313bec3f402Sdrh Tcl_Free((char*)p); 31475897234Sdrh free(zErrMsg); 31575897234Sdrh return TCL_ERROR; 31675897234Sdrh } 317bec3f402Sdrh Tcl_CreateCommand(interp, argv[1], DbCmd, (char*)p, DbDeleteCmd); 31875897234Sdrh return TCL_OK; 31975897234Sdrh } 32075897234Sdrh 32175897234Sdrh /* 32275897234Sdrh ** Initialize this module. 32375897234Sdrh ** 32475897234Sdrh ** This Tcl module contains only a single new Tcl command named "sqlite". 32575897234Sdrh ** (Hence there is no namespace. There is no point in using a namespace 32675897234Sdrh ** if the extension only supplies one new name!) The "sqlite" command is 32775897234Sdrh ** used to open a new SQLite database. See the DbMain() routine above 32875897234Sdrh ** for additional information. 32975897234Sdrh */ 33075897234Sdrh int Sqlite_Init(Tcl_Interp *interp){ 33175897234Sdrh Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0); 332*167a4b1cSdrh Tcl_PkgProvide(interp, "sqlite", "1.0"); 33375897234Sdrh return TCL_OK; 33475897234Sdrh } 33575897234Sdrh int Sqlite_SafeInit(Tcl_Interp *interp){ 33675897234Sdrh return TCL_OK; 33775897234Sdrh } 33875897234Sdrh 33975897234Sdrh /* 34075897234Sdrh ** If compiled using mktclapp, this routine runs to initialize 34175897234Sdrh ** everything. 34275897234Sdrh */ 34375897234Sdrh int Et_AppInit(Tcl_Interp *interp){ 34475897234Sdrh return Sqlite_Init(interp); 34575897234Sdrh } 346348784efSdrh 347348784efSdrh /* 348348784efSdrh ** If the macro TCLSH is defined and is one, then put in code for the 349348784efSdrh ** "main" routine that will initialize Tcl. 350348784efSdrh */ 351348784efSdrh #if defined(TCLSH) && TCLSH==1 352348784efSdrh static char zMainloop[] = 353348784efSdrh "set line {}\n" 354348784efSdrh "while {![eof stdin]} {\n" 355348784efSdrh "if {$line!=\"\"} {\n" 356348784efSdrh "puts -nonewline \"> \"\n" 357348784efSdrh "} else {\n" 358348784efSdrh "puts -nonewline \"% \"\n" 359348784efSdrh "}\n" 360348784efSdrh "flush stdout\n" 361348784efSdrh "append line [gets stdin]\n" 362348784efSdrh "if {[info complete $line]} {\n" 363348784efSdrh "if {[catch {uplevel #0 $line} result]} {\n" 364348784efSdrh "puts stderr \"Error: $result\"\n" 365348784efSdrh "} elseif {$result!=\"\"} {\n" 366348784efSdrh "puts $result\n" 367348784efSdrh "}\n" 368348784efSdrh "set line {}\n" 369348784efSdrh "} else {\n" 370348784efSdrh "append line \\n\n" 371348784efSdrh "}\n" 372348784efSdrh "}\n" 373348784efSdrh ; 374348784efSdrh 375348784efSdrh #define TCLSH_MAIN main /* Needed to fake out mktclapp */ 376348784efSdrh int TCLSH_MAIN(int argc, char **argv){ 377348784efSdrh Tcl_Interp *interp; 378348784efSdrh interp = Tcl_CreateInterp(); 379348784efSdrh Sqlite_Init(interp); 380348784efSdrh if( argc>=2 ){ 381348784efSdrh int i; 382348784efSdrh Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); 383348784efSdrh Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); 384348784efSdrh for(i=2; i<argc; i++){ 385348784efSdrh Tcl_SetVar(interp, "argv", argv[i], 386348784efSdrh TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); 387348784efSdrh } 388348784efSdrh if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ 389c61053b7Sdrh char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); 390c61053b7Sdrh if( zInfo==0 ) zInfo = interp->result; 391c61053b7Sdrh fprintf(stderr,"%s: %s\n", *argv, zInfo); 392348784efSdrh return 1; 393348784efSdrh } 394348784efSdrh }else{ 395348784efSdrh Tcl_GlobalEval(interp, zMainloop); 396348784efSdrh } 397348784efSdrh return 0; 398348784efSdrh } 399348784efSdrh #endif /* TCLSH */ 400