blob: ed4eb6c47504bc4389856660bd1f377c8601e09d [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** A TCL Interface to SQLite
13**
drhb5a20d32003-04-23 12:25:23 +000014** $Id: tclsqlite.c,v 1.48 2003/04/23 12:25:24 drh Exp $
drh75897232000-05-29 14:26:00 +000015*/
drh6d313162000-09-21 13:01:35 +000016#ifndef NO_TCL /* Omit this whole file if TCL is unavailable */
17
drh06b27182002-06-26 20:06:05 +000018#include "sqliteInt.h"
drh17a68932001-01-31 13:28:08 +000019#include "tcl.h"
drh75897232000-05-29 14:26:00 +000020#include <stdlib.h>
21#include <string.h>
drhce927062001-11-09 13:41:09 +000022#include <assert.h>
drh75897232000-05-29 14:26:00 +000023
24/*
drh98808ba2001-10-18 12:34:46 +000025** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
26** have to do a translation when going between the two. Set the
27** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
28** this translation.
29*/
30#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
31# define UTF_TRANSLATION_NEEDED 1
32#endif
33
34/*
drhcabb0812002-09-14 13:47:32 +000035** New SQL functions can be created as TCL scripts. Each such function
36** is described by an instance of the following structure.
37*/
38typedef struct SqlFunc SqlFunc;
39struct SqlFunc {
40 Tcl_Interp *interp; /* The TCL interpret to execute the function */
41 char *zScript; /* The script to be run */
42 SqlFunc *pNext; /* Next function on the list of them all */
43};
44
45/*
drhbec3f402000-08-04 13:49:02 +000046** There is one instance of this structure for each SQLite database
47** that has been opened by the SQLite TCL interface.
48*/
49typedef struct SqliteDb SqliteDb;
50struct SqliteDb {
51 sqlite *db; /* The "real" database structure */
52 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000053 char *zBusy; /* The busy callback routine */
drhb5a20d32003-04-23 12:25:23 +000054 char *zTrace; /* The trace callback routine */
drhe22a3342003-04-22 20:30:37 +000055 char *zAuth; /* The authorization callback routine */
drhcabb0812002-09-14 13:47:32 +000056 SqlFunc *pFunc; /* List of SQL functions */
drhdcd997e2003-01-31 17:21:49 +000057 int rc; /* Return code of most recent sqlite_exec() */
drhbec3f402000-08-04 13:49:02 +000058};
59
60/*
drh75897232000-05-29 14:26:00 +000061** An instance of this structure passes information thru the sqlite
62** logic from the original TCL command into the callback routine.
63*/
64typedef struct CallbackData CallbackData;
65struct CallbackData {
66 Tcl_Interp *interp; /* The TCL interpreter */
67 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000068 Tcl_Obj *pCode; /* The code to execute for each row */
drhce927062001-11-09 13:41:09 +000069 int once; /* Set for first callback only */
drh960e8c62001-04-03 16:53:21 +000070 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000071 int nColName; /* Number of entries in the azColName[] array */
72 char **azColName; /* Column names translated to UTF-8 */
drh98808ba2001-10-18 12:34:46 +000073};
drh297ecf12001-04-05 15:57:13 +000074
drh6d4abfb2001-10-22 02:58:08 +000075#ifdef UTF_TRANSLATION_NEEDED
drh297ecf12001-04-05 15:57:13 +000076/*
drh75897232000-05-29 14:26:00 +000077** Called for each row of the result.
drh6d4abfb2001-10-22 02:58:08 +000078**
79** This version is used when TCL expects UTF-8 data but the database
80** uses the ISO8859 format. A translation must occur from ISO8859 into
81** UTF-8.
drh75897232000-05-29 14:26:00 +000082*/
83static int DbEvalCallback(
84 void *clientData, /* An instance of CallbackData */
85 int nCol, /* Number of columns in the result */
86 char ** azCol, /* Data for each column */
87 char ** azN /* Name for each column */
88){
89 CallbackData *cbData = (CallbackData*)clientData;
90 int i, rc;
drh297ecf12001-04-05 15:57:13 +000091 Tcl_DString dCol;
drh6d4abfb2001-10-22 02:58:08 +000092 Tcl_DStringInit(&dCol);
drhce927062001-11-09 13:41:09 +000093 if( cbData->azColName==0 ){
94 assert( cbData->once );
95 cbData->once = 0;
96 if( cbData->zArray[0] ){
97 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
drh6d4abfb2001-10-22 02:58:08 +000098 }
drhce927062001-11-09 13:41:09 +000099 cbData->azColName = malloc( nCol*sizeof(char*) );
100 if( cbData->azColName==0 ){ return 1; }
drh6d4abfb2001-10-22 02:58:08 +0000101 cbData->nColName = nCol;
102 for(i=0; i<nCol; i++){
103 Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol);
drhce927062001-11-09 13:41:09 +0000104 cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 );
105 if( cbData->azColName[i] ){
106 strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol));
107 }else{
108 return 1;
drh6d4abfb2001-10-22 02:58:08 +0000109 }
drhce927062001-11-09 13:41:09 +0000110 if( cbData->zArray[0] ){
111 Tcl_SetVar2(cbData->interp, cbData->zArray, "*",
112 Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh704027f2002-07-15 20:58:47 +0000113 if( azN[nCol]!=0 ){
drh5080aaa2002-07-11 12:18:16 +0000114 Tcl_DString dType;
115 Tcl_DStringInit(&dType);
116 Tcl_DStringAppend(&dType, "typeof:", -1);
117 Tcl_DStringAppend(&dType, Tcl_DStringValue(&dCol), -1);
118 Tcl_DStringFree(&dCol);
119 Tcl_ExternalToUtfDString(NULL, azN[i+nCol], -1, &dCol);
120 Tcl_SetVar2(cbData->interp, cbData->zArray,
121 Tcl_DStringValue(&dType), Tcl_DStringValue(&dCol),
122 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
123 Tcl_DStringFree(&dType);
124 }
drhce927062001-11-09 13:41:09 +0000125 }
drhfa173a72002-07-10 21:26:00 +0000126
drh6d4abfb2001-10-22 02:58:08 +0000127 Tcl_DStringFree(&dCol);
128 }
drh6d4abfb2001-10-22 02:58:08 +0000129 }
130 if( azCol!=0 ){
131 if( cbData->zArray[0] ){
132 for(i=0; i<nCol; i++){
133 char *z = azCol[i];
134 if( z==0 ) z = "";
135 Tcl_DStringInit(&dCol);
136 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
137 Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i],
138 Tcl_DStringValue(&dCol), 0);
139 Tcl_DStringFree(&dCol);
140 }
141 }else{
142 for(i=0; i<nCol; i++){
143 char *z = azCol[i];
144 if( z==0 ) z = "";
145 Tcl_DStringInit(&dCol);
146 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
147 Tcl_SetVar(cbData->interp, cbData->azColName[i],
148 Tcl_DStringValue(&dCol), 0);
149 Tcl_DStringFree(&dCol);
150 }
151 }
152 }
153 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
154 if( rc==TCL_CONTINUE ) rc = TCL_OK;
155 cbData->tcl_rc = rc;
156 return rc!=TCL_OK;
157}
158#endif /* UTF_TRANSLATION_NEEDED */
159
160#ifndef UTF_TRANSLATION_NEEDED
161/*
162** Called for each row of the result.
163**
164** This version is used when either of the following is true:
165**
166** (1) This version of TCL uses UTF-8 and the data in the
167** SQLite database is already in the UTF-8 format.
168**
169** (2) This version of TCL uses ISO8859 and the data in the
170** SQLite database is already in the ISO8859 format.
171*/
172static int DbEvalCallback(
173 void *clientData, /* An instance of CallbackData */
174 int nCol, /* Number of columns in the result */
175 char ** azCol, /* Data for each column */
176 char ** azN /* Name for each column */
177){
178 CallbackData *cbData = (CallbackData*)clientData;
179 int i, rc;
drh6a535342001-10-19 16:44:56 +0000180 if( azCol==0 || (cbData->once && cbData->zArray[0]) ){
181 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
182 for(i=0; i<nCol; i++){
183 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
184 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh5080aaa2002-07-11 12:18:16 +0000185 if( azN[nCol] ){
186 char *z = sqlite_mprintf("typeof:%s", azN[i]);
187 Tcl_SetVar2(cbData->interp, cbData->zArray, z, azN[i+nCol],
188 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
189 sqlite_freemem(z);
190 }
drh6a535342001-10-19 16:44:56 +0000191 }
192 cbData->once = 0;
193 }
194 if( azCol!=0 ){
195 if( cbData->zArray[0] ){
drh75897232000-05-29 14:26:00 +0000196 for(i=0; i<nCol; i++){
drh6a535342001-10-19 16:44:56 +0000197 char *z = azCol[i];
198 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000199 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
drh6a535342001-10-19 16:44:56 +0000200 }
201 }else{
202 for(i=0; i<nCol; i++){
203 char *z = azCol[i];
204 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000205 Tcl_SetVar(cbData->interp, azN[i], z, 0);
drh75897232000-05-29 14:26:00 +0000206 }
207 }
drh75897232000-05-29 14:26:00 +0000208 }
drh6d313162000-09-21 13:01:35 +0000209 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
drh960e8c62001-04-03 16:53:21 +0000210 if( rc==TCL_CONTINUE ) rc = TCL_OK;
211 cbData->tcl_rc = rc;
212 return rc!=TCL_OK;
drh75897232000-05-29 14:26:00 +0000213}
drh6d4abfb2001-10-22 02:58:08 +0000214#endif
drh75897232000-05-29 14:26:00 +0000215
216/*
drh6d313162000-09-21 13:01:35 +0000217** This is an alternative callback for database queries. Instead
218** of invoking a TCL script to handle the result, this callback just
219** appends each column of the result to a list. After the query
220** is complete, the list is returned.
221*/
222static int DbEvalCallback2(
223 void *clientData, /* An instance of CallbackData */
224 int nCol, /* Number of columns in the result */
225 char ** azCol, /* Data for each column */
226 char ** azN /* Name for each column */
227){
228 Tcl_Obj *pList = (Tcl_Obj*)clientData;
229 int i;
drh6a535342001-10-19 16:44:56 +0000230 if( azCol==0 ) return 0;
drh6d313162000-09-21 13:01:35 +0000231 for(i=0; i<nCol; i++){
232 Tcl_Obj *pElem;
233 if( azCol[i] && *azCol[i] ){
drh297ecf12001-04-05 15:57:13 +0000234#ifdef UTF_TRANSLATION_NEEDED
235 Tcl_DString dCol;
236 Tcl_DStringInit(&dCol);
237 Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol);
238 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
239 Tcl_DStringFree(&dCol);
240#else
drh6d313162000-09-21 13:01:35 +0000241 pElem = Tcl_NewStringObj(azCol[i], -1);
drh297ecf12001-04-05 15:57:13 +0000242#endif
drh6d313162000-09-21 13:01:35 +0000243 }else{
244 pElem = Tcl_NewObj();
245 }
246 Tcl_ListObjAppendElement(0, pList, pElem);
247 }
248 return 0;
249}
250
251/*
drh75897232000-05-29 14:26:00 +0000252** Called when the command is deleted.
253*/
254static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000255 SqliteDb *pDb = (SqliteDb*)db;
256 sqlite_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000257 while( pDb->pFunc ){
258 SqlFunc *pFunc = pDb->pFunc;
259 pDb->pFunc = pFunc->pNext;
260 Tcl_Free((char*)pFunc);
261 }
drhbec3f402000-08-04 13:49:02 +0000262 if( pDb->zBusy ){
263 Tcl_Free(pDb->zBusy);
264 }
drhb5a20d32003-04-23 12:25:23 +0000265 if( pDb->zTrace ){
266 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000267 }
drhe22a3342003-04-22 20:30:37 +0000268 if( pDb->zAuth ){
269 Tcl_Free(pDb->zAuth);
270 }
drhbec3f402000-08-04 13:49:02 +0000271 Tcl_Free((char*)pDb);
272}
273
274/*
275** This routine is called when a database file is locked while trying
276** to execute SQL.
277*/
278static int DbBusyHandler(void *cd, const char *zTable, int nTries){
279 SqliteDb *pDb = (SqliteDb*)cd;
280 int rc;
281 char zVal[30];
282 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000283 Tcl_DString cmd;
284
285 Tcl_DStringInit(&cmd);
286 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
287 Tcl_DStringAppendElement(&cmd, zTable);
288 sprintf(zVal, " %d", nTries);
289 Tcl_DStringAppend(&cmd, zVal, -1);
290 zCmd = Tcl_DStringValue(&cmd);
291 rc = Tcl_Eval(pDb->interp, zCmd);
292 Tcl_DStringFree(&cmd);
293 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
294 return 0;
295 }
296 return 1;
drh75897232000-05-29 14:26:00 +0000297}
298
299/*
drhb5a20d32003-04-23 12:25:23 +0000300** This routine is called by the SQLite trace handler whenever a new
301** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000302*/
drhb5a20d32003-04-23 12:25:23 +0000303static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000304 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000305 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000306
drhb5a20d32003-04-23 12:25:23 +0000307 Tcl_DStringInit(&str);
308 Tcl_DStringAppend(&str, pDb->zTrace, -1);
309 Tcl_DStringAppendElement(&str, zSql);
310 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
311 Tcl_DStringFree(&str);
312 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000313}
314
315/*
drhcabb0812002-09-14 13:47:32 +0000316** This routine is called to evaluate an SQL function implemented
317** using TCL script.
318*/
319static void tclSqlFunc(sqlite_func *context, int argc, const char **argv){
320 SqlFunc *p = sqlite_user_data(context);
321 Tcl_DString cmd;
322 int i;
323 int rc;
324
325 Tcl_DStringInit(&cmd);
326 Tcl_DStringAppend(&cmd, p->zScript, -1);
327 for(i=0; i<argc; i++){
328 Tcl_DStringAppendElement(&cmd, argv[i] ? argv[i] : "");
329 }
330 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
331 if( rc ){
332 sqlite_set_result_error(context, Tcl_GetStringResult(p->interp), -1);
333 }else{
334 sqlite_set_result_string(context, Tcl_GetStringResult(p->interp), -1);
335 }
336}
drhe22a3342003-04-22 20:30:37 +0000337#ifndef SQLITE_OMIT_AUTHORIZATION
338/*
339** This is the authentication function. It appends the authentication
340** type code and the two arguments to zCmd[] then invokes the result
341** on the interpreter. The reply is examined to determine if the
342** authentication fails or succeeds.
343*/
344static int auth_callback(
345 void *pArg,
346 int code,
347 const char *zArg1,
348 const char *zArg2,
349 const char *zArg3,
350 const char *zArg4
351){
352 char *zCode;
353 Tcl_DString str;
354 int rc;
355 const char *zReply;
356 SqliteDb *pDb = (SqliteDb*)pArg;
357
358 switch( code ){
359 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
360 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
361 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
362 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
363 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
364 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
365 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
366 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
367 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
368 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
369 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
370 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
371 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
372 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
373 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
374 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
375 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
376 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
377 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
378 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
379 case SQLITE_READ : zCode="SQLITE_READ"; break;
380 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
381 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
382 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
383 default : zCode="????"; break;
384 }
385 Tcl_DStringInit(&str);
386 Tcl_DStringAppend(&str, pDb->zAuth, -1);
387 Tcl_DStringAppendElement(&str, zCode);
388 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
389 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
390 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
391 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
392 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
393 Tcl_DStringFree(&str);
394 zReply = Tcl_GetStringResult(pDb->interp);
395 if( strcmp(zReply,"SQLITE_OK")==0 ){
396 rc = SQLITE_OK;
397 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
398 rc = SQLITE_DENY;
399 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
400 rc = SQLITE_IGNORE;
401 }else{
402 rc = 999;
403 }
404 return rc;
405}
406#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000407
408/*
drh75897232000-05-29 14:26:00 +0000409** The "sqlite" command below creates a new Tcl command for each
410** connection it opens to an SQLite database. This routine is invoked
411** whenever one of those connection-specific commands is executed
412** in Tcl. For example, if you run Tcl code like this:
413**
414** sqlite db1 "my_database"
415** db1 close
416**
417** The first command opens a connection to the "my_database" database
418** and calls that connection "db1". The second command causes this
419** subroutine to be invoked.
420*/
drh6d313162000-09-21 13:01:35 +0000421static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000422 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000423 int choice;
drh0de8c112002-07-06 16:32:14 +0000424 static const char *DB_strs[] = {
drhb5a20d32003-04-23 12:25:23 +0000425 "authorizer", "busy", "changes",
426 "close", "complete", "errorcode",
427 "eval", "function", "last_insert_rowid",
428 "timeout", "trace", 0
drh6d313162000-09-21 13:01:35 +0000429 };
drh411995d2002-06-25 19:31:18 +0000430 enum DB_enum {
drhb5a20d32003-04-23 12:25:23 +0000431 DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
432 DB_CLOSE, DB_COMPLETE, DB_ERRORCODE,
433 DB_EVAL, DB_FUNCTION, DB_LAST_INSERT_ROWID,
434 DB_TIMEOUT, DB_TRACE,
drh6d313162000-09-21 13:01:35 +0000435 };
436
437 if( objc<2 ){
438 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000439 return TCL_ERROR;
440 }
drh411995d2002-06-25 19:31:18 +0000441 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000442 return TCL_ERROR;
443 }
444
drh411995d2002-06-25 19:31:18 +0000445 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000446
drhe22a3342003-04-22 20:30:37 +0000447 /* $db authorizer ?CALLBACK?
448 **
449 ** Invoke the given callback to authorize each SQL operation as it is
450 ** compiled. 5 arguments are appended to the callback before it is
451 ** invoked:
452 **
453 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
454 ** (2) First descriptive name (depends on authorization type)
455 ** (3) Second descriptive name
456 ** (4) Name of the database (ex: "main", "temp")
457 ** (5) Name of trigger that is doing the access
458 **
459 ** The callback should return on of the following strings: SQLITE_OK,
460 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
461 **
462 ** If this method is invoked with no arguments, the current authorization
463 ** callback string is returned.
464 */
465 case DB_AUTHORIZER: {
466 if( objc>3 ){
467 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
468 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000469 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000470 Tcl_AppendResult(interp, pDb->zAuth, 0);
471 }
472 }else{
473 char *zAuth;
474 int len;
475 if( pDb->zAuth ){
476 Tcl_Free(pDb->zAuth);
477 }
478 zAuth = Tcl_GetStringFromObj(objv[2], &len);
479 if( zAuth && len>0 ){
480 pDb->zAuth = Tcl_Alloc( len + 1 );
481 strcpy(pDb->zAuth, zAuth);
482 }else{
483 pDb->zAuth = 0;
484 }
485#ifndef SQLITE_OMIT_AUTHORIZATION
486 if( pDb->zAuth ){
487 pDb->interp = interp;
488 sqlite_set_authorizer(pDb->db, auth_callback, pDb);
489 }else{
490 sqlite_set_authorizer(pDb->db, 0, 0);
491 }
492#endif
493 }
494 break;
495 }
496
drhbec3f402000-08-04 13:49:02 +0000497 /* $db busy ?CALLBACK?
498 **
499 ** Invoke the given callback if an SQL statement attempts to open
500 ** a locked database file.
501 */
drh6d313162000-09-21 13:01:35 +0000502 case DB_BUSY: {
503 if( objc>3 ){
504 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000505 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000506 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000507 if( pDb->zBusy ){
508 Tcl_AppendResult(interp, pDb->zBusy, 0);
509 }
510 }else{
drh6d313162000-09-21 13:01:35 +0000511 char *zBusy;
512 int len;
drhbec3f402000-08-04 13:49:02 +0000513 if( pDb->zBusy ){
514 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000515 }
drh6d313162000-09-21 13:01:35 +0000516 zBusy = Tcl_GetStringFromObj(objv[2], &len);
517 if( zBusy && len>0 ){
518 pDb->zBusy = Tcl_Alloc( len + 1 );
519 strcpy(pDb->zBusy, zBusy);
520 }else{
521 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000522 }
523 if( pDb->zBusy ){
524 pDb->interp = interp;
525 sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000526 }else{
527 sqlite_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000528 }
529 }
drh6d313162000-09-21 13:01:35 +0000530 break;
531 }
drhbec3f402000-08-04 13:49:02 +0000532
drhc8d30ac2002-04-12 10:08:59 +0000533 /*
534 ** $db changes
535 **
536 ** Return the number of rows that were modified, inserted, or deleted by
537 ** the most recent "eval".
538 */
539 case DB_CHANGES: {
540 Tcl_Obj *pResult;
541 int nChange;
542 if( objc!=2 ){
543 Tcl_WrongNumArgs(interp, 2, objv, "");
544 return TCL_ERROR;
545 }
546 nChange = sqlite_changes(pDb->db);
547 pResult = Tcl_GetObjResult(interp);
548 Tcl_SetIntObj(pResult, nChange);
549 break;
550 }
551
drh75897232000-05-29 14:26:00 +0000552 /* $db close
553 **
554 ** Shutdown the database
555 */
drh6d313162000-09-21 13:01:35 +0000556 case DB_CLOSE: {
557 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
558 break;
559 }
drh75897232000-05-29 14:26:00 +0000560
561 /* $db complete SQL
562 **
563 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
564 ** additional lines of input are needed. This is similar to the
565 ** built-in "info complete" command of Tcl.
566 */
drh6d313162000-09-21 13:01:35 +0000567 case DB_COMPLETE: {
568 Tcl_Obj *pResult;
569 int isComplete;
570 if( objc!=3 ){
571 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000572 return TCL_ERROR;
573 }
drh6d313162000-09-21 13:01:35 +0000574 isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
575 pResult = Tcl_GetObjResult(interp);
576 Tcl_SetBooleanObj(pResult, isComplete);
577 break;
578 }
drhdcd997e2003-01-31 17:21:49 +0000579
580 /*
581 ** $db errorcode
582 **
583 ** Return the numeric error code that was returned by the most recent
584 ** call to sqlite_exec().
585 */
586 case DB_ERRORCODE: {
587 Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc));
588 break;
589 }
drh75897232000-05-29 14:26:00 +0000590
591 /*
592 ** $db eval $sql ?array { ...code... }?
593 **
594 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000595 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000596 ** If "array" and "code" are omitted, then no callback is every invoked.
597 ** If "array" is an empty string, then the values are placed in variables
598 ** that have the same name as the fields extracted by the query.
599 */
drh6d313162000-09-21 13:01:35 +0000600 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000601 CallbackData cbData;
602 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000603 char *zSql;
drh75897232000-05-29 14:26:00 +0000604 int rc;
drh297ecf12001-04-05 15:57:13 +0000605#ifdef UTF_TRANSLATION_NEEDED
606 Tcl_DString dSql;
drh6d4abfb2001-10-22 02:58:08 +0000607 int i;
drh297ecf12001-04-05 15:57:13 +0000608#endif
drh75897232000-05-29 14:26:00 +0000609
drh6d313162000-09-21 13:01:35 +0000610 if( objc!=5 && objc!=3 ){
611 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000612 return TCL_ERROR;
613 }
drhbec3f402000-08-04 13:49:02 +0000614 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000615 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh297ecf12001-04-05 15:57:13 +0000616#ifdef UTF_TRANSLATION_NEEDED
617 Tcl_DStringInit(&dSql);
618 Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
619 zSql = Tcl_DStringValue(&dSql);
620#endif
drh6d313162000-09-21 13:01:35 +0000621 Tcl_IncrRefCount(objv[2]);
622 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000623 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000624 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000625 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
626 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000627 cbData.tcl_rc = TCL_OK;
drh6d4abfb2001-10-22 02:58:08 +0000628 cbData.nColName = 0;
629 cbData.azColName = 0;
drh75897232000-05-29 14:26:00 +0000630 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000631 Tcl_IncrRefCount(objv[3]);
632 Tcl_IncrRefCount(objv[4]);
633 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
634 Tcl_DecrRefCount(objv[4]);
635 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000636 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000637 }else{
drh6d313162000-09-21 13:01:35 +0000638 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000639 cbData.tcl_rc = TCL_OK;
drh6d313162000-09-21 13:01:35 +0000640 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
641 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000642 }
drhdcd997e2003-01-31 17:21:49 +0000643 pDb->rc = rc;
drhb798fa62002-09-03 19:43:23 +0000644 if( rc==SQLITE_ABORT ){
645 if( zErrMsg ) free(zErrMsg);
646 rc = cbData.tcl_rc;
647 }else if( zErrMsg ){
drh75897232000-05-29 14:26:00 +0000648 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
649 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000650 rc = TCL_ERROR;
drhb798fa62002-09-03 19:43:23 +0000651 }else if( rc!=SQLITE_OK ){
drh6d4abfb2001-10-22 02:58:08 +0000652 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
653 rc = TCL_ERROR;
drh960e8c62001-04-03 16:53:21 +0000654 }else{
drh75897232000-05-29 14:26:00 +0000655 }
drh6d313162000-09-21 13:01:35 +0000656 Tcl_DecrRefCount(objv[2]);
drh297ecf12001-04-05 15:57:13 +0000657#ifdef UTF_TRANSLATION_NEEDED
658 Tcl_DStringFree(&dSql);
drh6d4abfb2001-10-22 02:58:08 +0000659 if( objc==5 && cbData.azColName ){
660 for(i=0; i<cbData.nColName; i++){
661 if( cbData.azColName[i] ) free(cbData.azColName[i]);
662 }
663 free(cbData.azColName);
drhce927062001-11-09 13:41:09 +0000664 cbData.azColName = 0;
drh6d4abfb2001-10-22 02:58:08 +0000665 }
drh297ecf12001-04-05 15:57:13 +0000666#endif
drh75897232000-05-29 14:26:00 +0000667 return rc;
drh6d313162000-09-21 13:01:35 +0000668 }
drhbec3f402000-08-04 13:49:02 +0000669
670 /*
drhcabb0812002-09-14 13:47:32 +0000671 ** $db function NAME SCRIPT
672 **
673 ** Create a new SQL function called NAME. Whenever that function is
674 ** called, invoke SCRIPT to evaluate the function.
675 */
676 case DB_FUNCTION: {
677 SqlFunc *pFunc;
678 char *zName;
679 char *zScript;
680 int nScript;
681 if( objc!=4 ){
682 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
683 return TCL_ERROR;
684 }
685 zName = Tcl_GetStringFromObj(objv[2], 0);
686 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
687 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
688 if( pFunc==0 ) return TCL_ERROR;
689 pFunc->interp = interp;
690 pFunc->pNext = pDb->pFunc;
691 pFunc->zScript = (char*)&pFunc[1];
692 strcpy(pFunc->zScript, zScript);
693 sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc);
694 sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC);
695 break;
696 }
697
698 /*
drhaf9ff332002-01-16 21:00:27 +0000699 ** $db last_insert_rowid
700 **
701 ** Return an integer which is the ROWID for the most recent insert.
702 */
703 case DB_LAST_INSERT_ROWID: {
704 Tcl_Obj *pResult;
705 int rowid;
706 if( objc!=2 ){
707 Tcl_WrongNumArgs(interp, 2, objv, "");
708 return TCL_ERROR;
709 }
710 rowid = sqlite_last_insert_rowid(pDb->db);
711 pResult = Tcl_GetObjResult(interp);
712 Tcl_SetIntObj(pResult, rowid);
713 break;
714 }
715
716 /*
drhbec3f402000-08-04 13:49:02 +0000717 ** $db timeout MILLESECONDS
718 **
719 ** Delay for the number of milliseconds specified when a file is locked.
720 */
drh6d313162000-09-21 13:01:35 +0000721 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000722 int ms;
drh6d313162000-09-21 13:01:35 +0000723 if( objc!=3 ){
724 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000725 return TCL_ERROR;
726 }
drh6d313162000-09-21 13:01:35 +0000727 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
drhbec3f402000-08-04 13:49:02 +0000728 sqlite_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000729 break;
drh75897232000-05-29 14:26:00 +0000730 }
drhb5a20d32003-04-23 12:25:23 +0000731
732 /* $db trace ?CALLBACK?
733 **
734 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
735 ** that is executed. The text of the SQL is appended to CALLBACK before
736 ** it is executed.
737 */
738 case DB_TRACE: {
739 if( objc>3 ){
740 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
741 }else if( objc==2 ){
742 if( pDb->zTrace ){
743 Tcl_AppendResult(interp, pDb->zTrace, 0);
744 }
745 }else{
746 char *zTrace;
747 int len;
748 if( pDb->zTrace ){
749 Tcl_Free(pDb->zTrace);
750 }
751 zTrace = Tcl_GetStringFromObj(objv[2], &len);
752 if( zTrace && len>0 ){
753 pDb->zTrace = Tcl_Alloc( len + 1 );
754 strcpy(pDb->zTrace, zTrace);
755 }else{
756 pDb->zTrace = 0;
757 }
758 if( pDb->zTrace ){
759 pDb->interp = interp;
760 sqlite_trace(pDb->db, DbTraceHandler, pDb);
761 }else{
762 sqlite_trace(pDb->db, 0, 0);
763 }
764 }
765 break;
766 }
767
drh6d313162000-09-21 13:01:35 +0000768 } /* End of the SWITCH statement */
drh75897232000-05-29 14:26:00 +0000769 return TCL_OK;
770}
771
772/*
773** sqlite DBNAME FILENAME ?MODE?
774**
775** This is the main Tcl command. When the "sqlite" Tcl command is
776** invoked, this routine runs to process that command.
777**
778** The first argument, DBNAME, is an arbitrary name for a new
779** database connection. This command creates a new command named
780** DBNAME that is used to control that connection. The database
781** connection is deleted when the DBNAME command is deleted.
782**
783** The second argument is the name of the directory that contains
784** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000785**
786** For testing purposes, we also support the following:
787**
788** sqlite -encoding
789**
790** Return the encoding used by LIKE and GLOB operators. Choices
791** are UTF-8 and iso8859.
792**
drh647cb0e2002-11-04 19:32:25 +0000793** sqlite -version
794**
795** Return the version number of the SQLite library.
796**
drhfbc3eab2001-04-06 16:13:42 +0000797** sqlite -tcl-uses-utf
798**
799** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
800** not. Used by tests to make sure the library was compiled
801** correctly.
drh75897232000-05-29 14:26:00 +0000802*/
803static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){
804 int mode;
drhbec3f402000-08-04 13:49:02 +0000805 SqliteDb *p;
drh75897232000-05-29 14:26:00 +0000806 char *zErrMsg;
drh06b27182002-06-26 20:06:05 +0000807 char zBuf[80];
drhfbc3eab2001-04-06 16:13:42 +0000808 if( argc==2 ){
809 if( strcmp(argv[1],"-encoding")==0 ){
810 Tcl_AppendResult(interp,sqlite_encoding,0);
811 return TCL_OK;
812 }
drh647cb0e2002-11-04 19:32:25 +0000813 if( strcmp(argv[1],"-version")==0 ){
814 Tcl_AppendResult(interp,sqlite_version,0);
815 return TCL_OK;
816 }
drhfbc3eab2001-04-06 16:13:42 +0000817 if( strcmp(argv[1],"-tcl-uses-utf")==0 ){
818#ifdef TCL_UTF_MAX
819 Tcl_AppendResult(interp,"1",0);
820#else
821 Tcl_AppendResult(interp,"0",0);
822#endif
823 return TCL_OK;
824 }
825 }
drh75897232000-05-29 14:26:00 +0000826 if( argc!=3 && argc!=4 ){
827 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
828 " HANDLE FILENAME ?MODE?\"", 0);
829 return TCL_ERROR;
830 }
831 if( argc==3 ){
drh58b95762000-06-02 01:17:37 +0000832 mode = 0666;
drh75897232000-05-29 14:26:00 +0000833 }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){
834 return TCL_ERROR;
835 }
836 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +0000837 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +0000838 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +0000839 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
840 return TCL_ERROR;
841 }
842 memset(p, 0, sizeof(*p));
843 p->db = sqlite_open(argv[2], mode, &zErrMsg);
844 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +0000845 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +0000846 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +0000847 free(zErrMsg);
848 return TCL_ERROR;
849 }
drh6d313162000-09-21 13:01:35 +0000850 Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +0000851
drh06b27182002-06-26 20:06:05 +0000852 /* The return value is the value of the sqlite* pointer
853 */
854 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +0000855 if( strncmp(zBuf,"0x",2) ){
856 sprintf(zBuf, "0x%p", p->db);
857 }
drh06b27182002-06-26 20:06:05 +0000858 Tcl_AppendResult(interp, zBuf, 0);
859
drhc22bd472002-05-10 13:14:07 +0000860 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +0000861 ** SQL function.
drhc22bd472002-05-10 13:14:07 +0000862 */
drh28b4e482002-03-11 02:06:13 +0000863#ifdef SQLITE_TEST
864 {
drhc22bd472002-05-10 13:14:07 +0000865 extern void Md5_Register(sqlite*);
866 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +0000867 }
drh28b4e482002-03-11 02:06:13 +0000868#endif
drh75897232000-05-29 14:26:00 +0000869 return TCL_OK;
870}
871
872/*
drh90ca9752001-09-28 17:47:14 +0000873** Provide a dummy Tcl_InitStubs if we are using this as a static
874** library.
875*/
876#ifndef USE_TCL_STUBS
877# undef Tcl_InitStubs
878# define Tcl_InitStubs(a,b,c)
879#endif
880
881/*
drh75897232000-05-29 14:26:00 +0000882** Initialize this module.
883**
884** This Tcl module contains only a single new Tcl command named "sqlite".
885** (Hence there is no namespace. There is no point in using a namespace
886** if the extension only supplies one new name!) The "sqlite" command is
887** used to open a new SQLite database. See the DbMain() routine above
888** for additional information.
889*/
890int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +0000891 Tcl_InitStubs(interp, "8.0", 0);
drhc2eef3b2002-08-31 18:53:06 +0000892 Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000893 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh90ca9752001-09-28 17:47:14 +0000894 return TCL_OK;
895}
896int Tclsqlite_Init(Tcl_Interp *interp){
897 Tcl_InitStubs(interp, "8.0", 0);
drhc2eef3b2002-08-31 18:53:06 +0000898 Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000899 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh75897232000-05-29 14:26:00 +0000900 return TCL_OK;
901}
902int Sqlite_SafeInit(Tcl_Interp *interp){
903 return TCL_OK;
904}
drh90ca9752001-09-28 17:47:14 +0000905int Tclsqlite_SafeInit(Tcl_Interp *interp){
906 return TCL_OK;
907}
drh75897232000-05-29 14:26:00 +0000908
drh3cebbde2000-10-19 14:59:27 +0000909#if 0
drh75897232000-05-29 14:26:00 +0000910/*
911** If compiled using mktclapp, this routine runs to initialize
912** everything.
913*/
914int Et_AppInit(Tcl_Interp *interp){
915 return Sqlite_Init(interp);
916}
drh3cebbde2000-10-19 14:59:27 +0000917#endif
drh348784e2000-05-29 20:41:49 +0000918
919/*
920** If the macro TCLSH is defined and is one, then put in code for the
921** "main" routine that will initialize Tcl.
922*/
923#if defined(TCLSH) && TCLSH==1
924static char zMainloop[] =
925 "set line {}\n"
926 "while {![eof stdin]} {\n"
927 "if {$line!=\"\"} {\n"
928 "puts -nonewline \"> \"\n"
929 "} else {\n"
930 "puts -nonewline \"% \"\n"
931 "}\n"
932 "flush stdout\n"
933 "append line [gets stdin]\n"
934 "if {[info complete $line]} {\n"
935 "if {[catch {uplevel #0 $line} result]} {\n"
936 "puts stderr \"Error: $result\"\n"
937 "} elseif {$result!=\"\"} {\n"
938 "puts $result\n"
939 "}\n"
940 "set line {}\n"
941 "} else {\n"
942 "append line \\n\n"
943 "}\n"
944 "}\n"
945;
946
947#define TCLSH_MAIN main /* Needed to fake out mktclapp */
948int TCLSH_MAIN(int argc, char **argv){
949 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +0000950 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +0000951 interp = Tcl_CreateInterp();
952 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +0000953#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +0000954 {
955 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +0000956 extern int Sqlitetest2_Init(Tcl_Interp*);
957 extern int Sqlitetest3_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +0000958 extern int Md5_Init(Tcl_Interp*);
drhd1bf3512001-04-07 15:24:33 +0000959 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +0000960 Sqlitetest2_Init(interp);
961 Sqlitetest3_Init(interp);
drhefc251d2001-07-01 22:12:01 +0000962 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +0000963 }
964#endif
drh348784e2000-05-29 20:41:49 +0000965 if( argc>=2 ){
966 int i;
967 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
968 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
969 for(i=2; i<argc; i++){
970 Tcl_SetVar(interp, "argv", argv[i],
971 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
972 }
973 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +0000974 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +0000975 if( zInfo==0 ) zInfo = interp->result;
976 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +0000977 return 1;
978 }
979 }else{
980 Tcl_GlobalEval(interp, zMainloop);
981 }
982 return 0;
983}
984#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +0000985
986#endif /* !defined(NO_TCL) */