blob: c88052422074b8ab2be45f618010b07ebc08200f [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
2** Copyright (c) 1999, 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** A TCL Interface to SQLite
25**
drh960e8c62001-04-03 16:53:21 +000026** $Id: tclsqlite.c,v 1.14 2001/04/03 16:53:22 drh Exp $
drh75897232000-05-29 14:26:00 +000027*/
drh6d313162000-09-21 13:01:35 +000028#ifndef NO_TCL /* Omit this whole file if TCL is unavailable */
29
drh75897232000-05-29 14:26:00 +000030#include "sqlite.h"
drh17a68932001-01-31 13:28:08 +000031#include "tcl.h"
drh75897232000-05-29 14:26:00 +000032#include <stdlib.h>
33#include <string.h>
34
35/*
drhbec3f402000-08-04 13:49:02 +000036** There is one instance of this structure for each SQLite database
37** that has been opened by the SQLite TCL interface.
38*/
39typedef struct SqliteDb SqliteDb;
40struct SqliteDb {
41 sqlite *db; /* The "real" database structure */
42 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000043 char *zBusy; /* The busy callback routine */
drhbec3f402000-08-04 13:49:02 +000044};
45
46/*
drh75897232000-05-29 14:26:00 +000047** An instance of this structure passes information thru the sqlite
48** logic from the original TCL command into the callback routine.
49*/
50typedef struct CallbackData CallbackData;
51struct CallbackData {
52 Tcl_Interp *interp; /* The TCL interpreter */
53 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000054 Tcl_Obj *pCode; /* The code to execute for each row */
drh75897232000-05-29 14:26:00 +000055 int once; /* Set only for the first invocation of callback */
drh960e8c62001-04-03 16:53:21 +000056 int tcl_rc; /* Return code from TCL script */
drh75897232000-05-29 14:26:00 +000057};
58
59/*
60** Called for each row of the result.
61*/
62static int DbEvalCallback(
63 void *clientData, /* An instance of CallbackData */
64 int nCol, /* Number of columns in the result */
65 char ** azCol, /* Data for each column */
66 char ** azN /* Name for each column */
67){
68 CallbackData *cbData = (CallbackData*)clientData;
69 int i, rc;
70 if( cbData->zArray[0] ){
71 if( cbData->once ){
drh9b0d0a82000-09-30 22:46:05 +000072 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
drh75897232000-05-29 14:26:00 +000073 for(i=0; i<nCol; i++){
74 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
75 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
76 }
77 }
78 for(i=0; i<nCol; i++){
drhc61053b2000-06-04 12:58:36 +000079 char *z = azCol[i];
80 if( z==0 ) z = "";
81 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
drh75897232000-05-29 14:26:00 +000082 }
83 }else{
84 for(i=0; i<nCol; i++){
drhc61053b2000-06-04 12:58:36 +000085 char *z = azCol[i];
86 if( z==0 ) z = "";
87 Tcl_SetVar(cbData->interp, azN[i], z, 0);
drh75897232000-05-29 14:26:00 +000088 }
89 }
90 cbData->once = 0;
drh6d313162000-09-21 13:01:35 +000091 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
drh960e8c62001-04-03 16:53:21 +000092 if( rc==TCL_CONTINUE ) rc = TCL_OK;
93 cbData->tcl_rc = rc;
94 return rc!=TCL_OK;
drh75897232000-05-29 14:26:00 +000095}
96
97/*
drh6d313162000-09-21 13:01:35 +000098** This is an alternative callback for database queries. Instead
99** of invoking a TCL script to handle the result, this callback just
100** appends each column of the result to a list. After the query
101** is complete, the list is returned.
102*/
103static int DbEvalCallback2(
104 void *clientData, /* An instance of CallbackData */
105 int nCol, /* Number of columns in the result */
106 char ** azCol, /* Data for each column */
107 char ** azN /* Name for each column */
108){
109 Tcl_Obj *pList = (Tcl_Obj*)clientData;
110 int i;
111 for(i=0; i<nCol; i++){
112 Tcl_Obj *pElem;
113 if( azCol[i] && *azCol[i] ){
114 pElem = Tcl_NewStringObj(azCol[i], -1);
115 }else{
116 pElem = Tcl_NewObj();
117 }
118 Tcl_ListObjAppendElement(0, pList, pElem);
119 }
120 return 0;
121}
122
123/*
drh75897232000-05-29 14:26:00 +0000124** Called when the command is deleted.
125*/
126static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000127 SqliteDb *pDb = (SqliteDb*)db;
128 sqlite_close(pDb->db);
129 if( pDb->zBusy ){
130 Tcl_Free(pDb->zBusy);
131 }
132 Tcl_Free((char*)pDb);
133}
134
135/*
136** This routine is called when a database file is locked while trying
137** to execute SQL.
138*/
139static int DbBusyHandler(void *cd, const char *zTable, int nTries){
140 SqliteDb *pDb = (SqliteDb*)cd;
141 int rc;
142 char zVal[30];
143 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000144 Tcl_DString cmd;
145
146 Tcl_DStringInit(&cmd);
147 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
148 Tcl_DStringAppendElement(&cmd, zTable);
149 sprintf(zVal, " %d", nTries);
150 Tcl_DStringAppend(&cmd, zVal, -1);
151 zCmd = Tcl_DStringValue(&cmd);
152 rc = Tcl_Eval(pDb->interp, zCmd);
153 Tcl_DStringFree(&cmd);
154 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
155 return 0;
156 }
157 return 1;
drh75897232000-05-29 14:26:00 +0000158}
159
160/*
161** The "sqlite" command below creates a new Tcl command for each
162** connection it opens to an SQLite database. This routine is invoked
163** whenever one of those connection-specific commands is executed
164** in Tcl. For example, if you run Tcl code like this:
165**
166** sqlite db1 "my_database"
167** db1 close
168**
169** The first command opens a connection to the "my_database" database
170** and calls that connection "db1". The second command causes this
171** subroutine to be invoked.
172*/
drh6d313162000-09-21 13:01:35 +0000173static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000174 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000175 int choice;
176 static char *DB_optStrs[] = {
drh960e8c62001-04-03 16:53:21 +0000177 "busy", "close", "complete", "eval", "timeout", 0
drh6d313162000-09-21 13:01:35 +0000178 };
179 enum DB_opts {
180 DB_BUSY, DB_CLOSE, DB_COMPLETE, DB_EVAL, DB_TIMEOUT
181 };
182
183 if( objc<2 ){
184 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000185 return TCL_ERROR;
186 }
drh6d313162000-09-21 13:01:35 +0000187 if( Tcl_GetIndexFromObj(interp, objv[1], DB_optStrs, "option", 0, &choice) ){
188 return TCL_ERROR;
189 }
190
191 switch( (enum DB_opts)choice ){
drh75897232000-05-29 14:26:00 +0000192
drhbec3f402000-08-04 13:49:02 +0000193 /* $db busy ?CALLBACK?
194 **
195 ** Invoke the given callback if an SQL statement attempts to open
196 ** a locked database file.
197 */
drh6d313162000-09-21 13:01:35 +0000198 case DB_BUSY: {
199 if( objc>3 ){
200 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000201 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000202 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000203 if( pDb->zBusy ){
204 Tcl_AppendResult(interp, pDb->zBusy, 0);
205 }
206 }else{
drh6d313162000-09-21 13:01:35 +0000207 char *zBusy;
208 int len;
drhbec3f402000-08-04 13:49:02 +0000209 if( pDb->zBusy ){
210 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000211 }
drh6d313162000-09-21 13:01:35 +0000212 zBusy = Tcl_GetStringFromObj(objv[2], &len);
213 if( zBusy && len>0 ){
214 pDb->zBusy = Tcl_Alloc( len + 1 );
215 strcpy(pDb->zBusy, zBusy);
216 }else{
217 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000218 }
219 if( pDb->zBusy ){
220 pDb->interp = interp;
221 sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000222 }else{
223 sqlite_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000224 }
225 }
drh6d313162000-09-21 13:01:35 +0000226 break;
227 }
drhbec3f402000-08-04 13:49:02 +0000228
drh75897232000-05-29 14:26:00 +0000229 /* $db close
230 **
231 ** Shutdown the database
232 */
drh6d313162000-09-21 13:01:35 +0000233 case DB_CLOSE: {
234 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
235 break;
236 }
drh75897232000-05-29 14:26:00 +0000237
238 /* $db complete SQL
239 **
240 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
241 ** additional lines of input are needed. This is similar to the
242 ** built-in "info complete" command of Tcl.
243 */
drh6d313162000-09-21 13:01:35 +0000244 case DB_COMPLETE: {
245 Tcl_Obj *pResult;
246 int isComplete;
247 if( objc!=3 ){
248 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000249 return TCL_ERROR;
250 }
drh6d313162000-09-21 13:01:35 +0000251 isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
252 pResult = Tcl_GetObjResult(interp);
253 Tcl_SetBooleanObj(pResult, isComplete);
254 break;
255 }
drh75897232000-05-29 14:26:00 +0000256
257 /*
258 ** $db eval $sql ?array { ...code... }?
259 **
260 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000261 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000262 ** If "array" and "code" are omitted, then no callback is every invoked.
263 ** If "array" is an empty string, then the values are placed in variables
264 ** that have the same name as the fields extracted by the query.
265 */
drh6d313162000-09-21 13:01:35 +0000266 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000267 CallbackData cbData;
268 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000269 char *zSql;
drh75897232000-05-29 14:26:00 +0000270 int rc;
271
drh6d313162000-09-21 13:01:35 +0000272 if( objc!=5 && objc!=3 ){
273 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000274 return TCL_ERROR;
275 }
drhbec3f402000-08-04 13:49:02 +0000276 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000277 zSql = Tcl_GetStringFromObj(objv[2], 0);
278 Tcl_IncrRefCount(objv[2]);
279 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000280 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000281 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000282 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
283 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000284 cbData.tcl_rc = TCL_OK;
drh75897232000-05-29 14:26:00 +0000285 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000286 Tcl_IncrRefCount(objv[3]);
287 Tcl_IncrRefCount(objv[4]);
288 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
289 Tcl_DecrRefCount(objv[4]);
290 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000291 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000292 }else{
drh6d313162000-09-21 13:01:35 +0000293 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000294 cbData.tcl_rc = TCL_OK;
drh6d313162000-09-21 13:01:35 +0000295 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
296 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000297 }
298 if( zErrMsg ){
299 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
300 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000301 rc = TCL_ERROR;
302 }else{
303 rc = cbData.tcl_rc;
drh75897232000-05-29 14:26:00 +0000304 }
drh6d313162000-09-21 13:01:35 +0000305 Tcl_DecrRefCount(objv[2]);
drh75897232000-05-29 14:26:00 +0000306 return rc;
drh6d313162000-09-21 13:01:35 +0000307 }
drhbec3f402000-08-04 13:49:02 +0000308
309 /*
310 ** $db timeout MILLESECONDS
311 **
312 ** Delay for the number of milliseconds specified when a file is locked.
313 */
drh6d313162000-09-21 13:01:35 +0000314 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000315 int ms;
drh6d313162000-09-21 13:01:35 +0000316 if( objc!=3 ){
317 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000318 return TCL_ERROR;
319 }
drh6d313162000-09-21 13:01:35 +0000320 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
drhbec3f402000-08-04 13:49:02 +0000321 sqlite_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000322 break;
drh75897232000-05-29 14:26:00 +0000323 }
drh6d313162000-09-21 13:01:35 +0000324 } /* End of the SWITCH statement */
drh75897232000-05-29 14:26:00 +0000325 return TCL_OK;
326}
327
328/*
329** sqlite DBNAME FILENAME ?MODE?
330**
331** This is the main Tcl command. When the "sqlite" Tcl command is
332** invoked, this routine runs to process that command.
333**
334** The first argument, DBNAME, is an arbitrary name for a new
335** database connection. This command creates a new command named
336** DBNAME that is used to control that connection. The database
337** connection is deleted when the DBNAME command is deleted.
338**
339** The second argument is the name of the directory that contains
340** the sqlite database that is to be accessed.
341*/
342static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){
343 int mode;
drhbec3f402000-08-04 13:49:02 +0000344 SqliteDb *p;
drh75897232000-05-29 14:26:00 +0000345 char *zErrMsg;
346 if( argc!=3 && argc!=4 ){
347 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
348 " HANDLE FILENAME ?MODE?\"", 0);
349 return TCL_ERROR;
350 }
351 if( argc==3 ){
drh58b95762000-06-02 01:17:37 +0000352 mode = 0666;
drh75897232000-05-29 14:26:00 +0000353 }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){
354 return TCL_ERROR;
355 }
356 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +0000357 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +0000358 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +0000359 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
360 return TCL_ERROR;
361 }
362 memset(p, 0, sizeof(*p));
363 p->db = sqlite_open(argv[2], mode, &zErrMsg);
364 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +0000365 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +0000366 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +0000367 free(zErrMsg);
368 return TCL_ERROR;
369 }
drh6d313162000-09-21 13:01:35 +0000370 Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd);
drh75897232000-05-29 14:26:00 +0000371 return TCL_OK;
372}
373
374/*
375** Initialize this module.
376**
377** This Tcl module contains only a single new Tcl command named "sqlite".
378** (Hence there is no namespace. There is no point in using a namespace
379** if the extension only supplies one new name!) The "sqlite" command is
380** used to open a new SQLite database. See the DbMain() routine above
381** for additional information.
382*/
383int Sqlite_Init(Tcl_Interp *interp){
384 Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0);
drh167a4b12000-08-17 09:49:59 +0000385 Tcl_PkgProvide(interp, "sqlite", "1.0");
drh75897232000-05-29 14:26:00 +0000386 return TCL_OK;
387}
388int Sqlite_SafeInit(Tcl_Interp *interp){
389 return TCL_OK;
390}
391
drh3cebbde2000-10-19 14:59:27 +0000392#if 0
drh75897232000-05-29 14:26:00 +0000393/*
394** If compiled using mktclapp, this routine runs to initialize
395** everything.
396*/
397int Et_AppInit(Tcl_Interp *interp){
398 return Sqlite_Init(interp);
399}
drh3cebbde2000-10-19 14:59:27 +0000400#endif
drh348784e2000-05-29 20:41:49 +0000401
402/*
403** If the macro TCLSH is defined and is one, then put in code for the
404** "main" routine that will initialize Tcl.
405*/
406#if defined(TCLSH) && TCLSH==1
407static char zMainloop[] =
408 "set line {}\n"
409 "while {![eof stdin]} {\n"
410 "if {$line!=\"\"} {\n"
411 "puts -nonewline \"> \"\n"
412 "} else {\n"
413 "puts -nonewline \"% \"\n"
414 "}\n"
415 "flush stdout\n"
416 "append line [gets stdin]\n"
417 "if {[info complete $line]} {\n"
418 "if {[catch {uplevel #0 $line} result]} {\n"
419 "puts stderr \"Error: $result\"\n"
420 "} elseif {$result!=\"\"} {\n"
421 "puts $result\n"
422 "}\n"
423 "set line {}\n"
424 "} else {\n"
425 "append line \\n\n"
426 "}\n"
427 "}\n"
428;
429
430#define TCLSH_MAIN main /* Needed to fake out mktclapp */
431int TCLSH_MAIN(int argc, char **argv){
432 Tcl_Interp *interp;
433 interp = Tcl_CreateInterp();
434 Sqlite_Init(interp);
435 if( argc>=2 ){
436 int i;
437 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
438 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
439 for(i=2; i<argc; i++){
440 Tcl_SetVar(interp, "argv", argv[i],
441 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
442 }
443 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drhc61053b2000-06-04 12:58:36 +0000444 char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
445 if( zInfo==0 ) zInfo = interp->result;
446 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +0000447 return 1;
448 }
449 }else{
450 Tcl_GlobalEval(interp, zMainloop);
451 }
452 return 0;
453}
454#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +0000455
456#endif /* !defined(NO_TCL) */