blob: b1b446f9872e3e8a033445bcda2f031a5109763a [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**
drhe22a3342003-04-22 20:30:37 +000014** $Id: tclsqlite.c,v 1.47 2003/04/22 20:30:39 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 */
drh0d1a6432003-04-03 15:46:04 +000054 char *zBegin; /* The begin-transaction callback routine */
55 char *zCommit; /* The commit-transaction callback routine */
drhe22a3342003-04-22 20:30:37 +000056 char *zAuth; /* The authorization callback routine */
drhcabb0812002-09-14 13:47:32 +000057 SqlFunc *pFunc; /* List of SQL functions */
drhdcd997e2003-01-31 17:21:49 +000058 int rc; /* Return code of most recent sqlite_exec() */
drhbec3f402000-08-04 13:49:02 +000059};
60
61/*
drh75897232000-05-29 14:26:00 +000062** An instance of this structure passes information thru the sqlite
63** logic from the original TCL command into the callback routine.
64*/
65typedef struct CallbackData CallbackData;
66struct CallbackData {
67 Tcl_Interp *interp; /* The TCL interpreter */
68 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000069 Tcl_Obj *pCode; /* The code to execute for each row */
drhce927062001-11-09 13:41:09 +000070 int once; /* Set for first callback only */
drh960e8c62001-04-03 16:53:21 +000071 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000072 int nColName; /* Number of entries in the azColName[] array */
73 char **azColName; /* Column names translated to UTF-8 */
drh98808ba2001-10-18 12:34:46 +000074};
drh297ecf12001-04-05 15:57:13 +000075
drh6d4abfb2001-10-22 02:58:08 +000076#ifdef UTF_TRANSLATION_NEEDED
drh297ecf12001-04-05 15:57:13 +000077/*
drh75897232000-05-29 14:26:00 +000078** Called for each row of the result.
drh6d4abfb2001-10-22 02:58:08 +000079**
80** This version is used when TCL expects UTF-8 data but the database
81** uses the ISO8859 format. A translation must occur from ISO8859 into
82** UTF-8.
drh75897232000-05-29 14:26:00 +000083*/
84static int DbEvalCallback(
85 void *clientData, /* An instance of CallbackData */
86 int nCol, /* Number of columns in the result */
87 char ** azCol, /* Data for each column */
88 char ** azN /* Name for each column */
89){
90 CallbackData *cbData = (CallbackData*)clientData;
91 int i, rc;
drh297ecf12001-04-05 15:57:13 +000092 Tcl_DString dCol;
drh6d4abfb2001-10-22 02:58:08 +000093 Tcl_DStringInit(&dCol);
drhce927062001-11-09 13:41:09 +000094 if( cbData->azColName==0 ){
95 assert( cbData->once );
96 cbData->once = 0;
97 if( cbData->zArray[0] ){
98 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
drh6d4abfb2001-10-22 02:58:08 +000099 }
drhce927062001-11-09 13:41:09 +0000100 cbData->azColName = malloc( nCol*sizeof(char*) );
101 if( cbData->azColName==0 ){ return 1; }
drh6d4abfb2001-10-22 02:58:08 +0000102 cbData->nColName = nCol;
103 for(i=0; i<nCol; i++){
104 Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol);
drhce927062001-11-09 13:41:09 +0000105 cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 );
106 if( cbData->azColName[i] ){
107 strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol));
108 }else{
109 return 1;
drh6d4abfb2001-10-22 02:58:08 +0000110 }
drhce927062001-11-09 13:41:09 +0000111 if( cbData->zArray[0] ){
112 Tcl_SetVar2(cbData->interp, cbData->zArray, "*",
113 Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh704027f2002-07-15 20:58:47 +0000114 if( azN[nCol]!=0 ){
drh5080aaa2002-07-11 12:18:16 +0000115 Tcl_DString dType;
116 Tcl_DStringInit(&dType);
117 Tcl_DStringAppend(&dType, "typeof:", -1);
118 Tcl_DStringAppend(&dType, Tcl_DStringValue(&dCol), -1);
119 Tcl_DStringFree(&dCol);
120 Tcl_ExternalToUtfDString(NULL, azN[i+nCol], -1, &dCol);
121 Tcl_SetVar2(cbData->interp, cbData->zArray,
122 Tcl_DStringValue(&dType), Tcl_DStringValue(&dCol),
123 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
124 Tcl_DStringFree(&dType);
125 }
drhce927062001-11-09 13:41:09 +0000126 }
drhfa173a72002-07-10 21:26:00 +0000127
drh6d4abfb2001-10-22 02:58:08 +0000128 Tcl_DStringFree(&dCol);
129 }
drh6d4abfb2001-10-22 02:58:08 +0000130 }
131 if( azCol!=0 ){
132 if( cbData->zArray[0] ){
133 for(i=0; i<nCol; i++){
134 char *z = azCol[i];
135 if( z==0 ) z = "";
136 Tcl_DStringInit(&dCol);
137 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
138 Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i],
139 Tcl_DStringValue(&dCol), 0);
140 Tcl_DStringFree(&dCol);
141 }
142 }else{
143 for(i=0; i<nCol; i++){
144 char *z = azCol[i];
145 if( z==0 ) z = "";
146 Tcl_DStringInit(&dCol);
147 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
148 Tcl_SetVar(cbData->interp, cbData->azColName[i],
149 Tcl_DStringValue(&dCol), 0);
150 Tcl_DStringFree(&dCol);
151 }
152 }
153 }
154 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
155 if( rc==TCL_CONTINUE ) rc = TCL_OK;
156 cbData->tcl_rc = rc;
157 return rc!=TCL_OK;
158}
159#endif /* UTF_TRANSLATION_NEEDED */
160
161#ifndef UTF_TRANSLATION_NEEDED
162/*
163** Called for each row of the result.
164**
165** This version is used when either of the following is true:
166**
167** (1) This version of TCL uses UTF-8 and the data in the
168** SQLite database is already in the UTF-8 format.
169**
170** (2) This version of TCL uses ISO8859 and the data in the
171** SQLite database is already in the ISO8859 format.
172*/
173static int DbEvalCallback(
174 void *clientData, /* An instance of CallbackData */
175 int nCol, /* Number of columns in the result */
176 char ** azCol, /* Data for each column */
177 char ** azN /* Name for each column */
178){
179 CallbackData *cbData = (CallbackData*)clientData;
180 int i, rc;
drh6a535342001-10-19 16:44:56 +0000181 if( azCol==0 || (cbData->once && cbData->zArray[0]) ){
182 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
183 for(i=0; i<nCol; i++){
184 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
185 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh5080aaa2002-07-11 12:18:16 +0000186 if( azN[nCol] ){
187 char *z = sqlite_mprintf("typeof:%s", azN[i]);
188 Tcl_SetVar2(cbData->interp, cbData->zArray, z, azN[i+nCol],
189 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
190 sqlite_freemem(z);
191 }
drh6a535342001-10-19 16:44:56 +0000192 }
193 cbData->once = 0;
194 }
195 if( azCol!=0 ){
196 if( cbData->zArray[0] ){
drh75897232000-05-29 14:26:00 +0000197 for(i=0; i<nCol; i++){
drh6a535342001-10-19 16:44:56 +0000198 char *z = azCol[i];
199 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000200 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
drh6a535342001-10-19 16:44:56 +0000201 }
202 }else{
203 for(i=0; i<nCol; i++){
204 char *z = azCol[i];
205 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000206 Tcl_SetVar(cbData->interp, azN[i], z, 0);
drh75897232000-05-29 14:26:00 +0000207 }
208 }
drh75897232000-05-29 14:26:00 +0000209 }
drh6d313162000-09-21 13:01:35 +0000210 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
drh960e8c62001-04-03 16:53:21 +0000211 if( rc==TCL_CONTINUE ) rc = TCL_OK;
212 cbData->tcl_rc = rc;
213 return rc!=TCL_OK;
drh75897232000-05-29 14:26:00 +0000214}
drh6d4abfb2001-10-22 02:58:08 +0000215#endif
drh75897232000-05-29 14:26:00 +0000216
217/*
drh6d313162000-09-21 13:01:35 +0000218** This is an alternative callback for database queries. Instead
219** of invoking a TCL script to handle the result, this callback just
220** appends each column of the result to a list. After the query
221** is complete, the list is returned.
222*/
223static int DbEvalCallback2(
224 void *clientData, /* An instance of CallbackData */
225 int nCol, /* Number of columns in the result */
226 char ** azCol, /* Data for each column */
227 char ** azN /* Name for each column */
228){
229 Tcl_Obj *pList = (Tcl_Obj*)clientData;
230 int i;
drh6a535342001-10-19 16:44:56 +0000231 if( azCol==0 ) return 0;
drh6d313162000-09-21 13:01:35 +0000232 for(i=0; i<nCol; i++){
233 Tcl_Obj *pElem;
234 if( azCol[i] && *azCol[i] ){
drh297ecf12001-04-05 15:57:13 +0000235#ifdef UTF_TRANSLATION_NEEDED
236 Tcl_DString dCol;
237 Tcl_DStringInit(&dCol);
238 Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol);
239 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
240 Tcl_DStringFree(&dCol);
241#else
drh6d313162000-09-21 13:01:35 +0000242 pElem = Tcl_NewStringObj(azCol[i], -1);
drh297ecf12001-04-05 15:57:13 +0000243#endif
drh6d313162000-09-21 13:01:35 +0000244 }else{
245 pElem = Tcl_NewObj();
246 }
247 Tcl_ListObjAppendElement(0, pList, pElem);
248 }
249 return 0;
250}
251
252/*
drh75897232000-05-29 14:26:00 +0000253** Called when the command is deleted.
254*/
255static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000256 SqliteDb *pDb = (SqliteDb*)db;
257 sqlite_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000258 while( pDb->pFunc ){
259 SqlFunc *pFunc = pDb->pFunc;
260 pDb->pFunc = pFunc->pNext;
261 Tcl_Free((char*)pFunc);
262 }
drhbec3f402000-08-04 13:49:02 +0000263 if( pDb->zBusy ){
264 Tcl_Free(pDb->zBusy);
265 }
drh0d1a6432003-04-03 15:46:04 +0000266 if( pDb->zBegin ){
267 Tcl_Free(pDb->zBegin);
268 }
269 if( pDb->zCommit ){
270 Tcl_Free(pDb->zCommit);
271 }
drhe22a3342003-04-22 20:30:37 +0000272 if( pDb->zAuth ){
273 Tcl_Free(pDb->zAuth);
274 }
drhbec3f402000-08-04 13:49:02 +0000275 Tcl_Free((char*)pDb);
276}
277
278/*
279** This routine is called when a database file is locked while trying
280** to execute SQL.
281*/
282static int DbBusyHandler(void *cd, const char *zTable, int nTries){
283 SqliteDb *pDb = (SqliteDb*)cd;
284 int rc;
285 char zVal[30];
286 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000287 Tcl_DString cmd;
288
289 Tcl_DStringInit(&cmd);
290 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
291 Tcl_DStringAppendElement(&cmd, zTable);
292 sprintf(zVal, " %d", nTries);
293 Tcl_DStringAppend(&cmd, zVal, -1);
294 zCmd = Tcl_DStringValue(&cmd);
295 rc = Tcl_Eval(pDb->interp, zCmd);
296 Tcl_DStringFree(&cmd);
297 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
298 return 0;
299 }
300 return 1;
drh75897232000-05-29 14:26:00 +0000301}
302
303/*
drh0d1a6432003-04-03 15:46:04 +0000304** This routine is called when a new transaction is started. The
305** TCL script in pDb->zBegin is executed. If it returns non-zero or
306** if it throws an exception, the transaction is aborted.
307*/
308static int DbBeginHandler(void *cd){
309 SqliteDb *pDb = (SqliteDb*)cd;
310 int rc;
311
312 rc = Tcl_Eval(pDb->interp, pDb->zBegin);
313 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
314 return 1;
315 }
316 return 0;
317}
318
319/*
320** This routine is called when a transaction is committed. The
321** TCL script in pDb->zCommit is executed. If it returns non-zero or
322** if it throws an exception, the transaction is rolled back instead
323** of being committed.
324*/
325static int DbCommitHandler(void *cd){
326 SqliteDb *pDb = (SqliteDb*)cd;
327 int rc;
328
329 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
330 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
331 return 1;
332 }
333 return 0;
334}
335
336/*
drhcabb0812002-09-14 13:47:32 +0000337** This routine is called to evaluate an SQL function implemented
338** using TCL script.
339*/
340static void tclSqlFunc(sqlite_func *context, int argc, const char **argv){
341 SqlFunc *p = sqlite_user_data(context);
342 Tcl_DString cmd;
343 int i;
344 int rc;
345
346 Tcl_DStringInit(&cmd);
347 Tcl_DStringAppend(&cmd, p->zScript, -1);
348 for(i=0; i<argc; i++){
349 Tcl_DStringAppendElement(&cmd, argv[i] ? argv[i] : "");
350 }
351 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
352 if( rc ){
353 sqlite_set_result_error(context, Tcl_GetStringResult(p->interp), -1);
354 }else{
355 sqlite_set_result_string(context, Tcl_GetStringResult(p->interp), -1);
356 }
357}
drhe22a3342003-04-22 20:30:37 +0000358#ifndef SQLITE_OMIT_AUTHORIZATION
359/*
360** This is the authentication function. It appends the authentication
361** type code and the two arguments to zCmd[] then invokes the result
362** on the interpreter. The reply is examined to determine if the
363** authentication fails or succeeds.
364*/
365static int auth_callback(
366 void *pArg,
367 int code,
368 const char *zArg1,
369 const char *zArg2,
370 const char *zArg3,
371 const char *zArg4
372){
373 char *zCode;
374 Tcl_DString str;
375 int rc;
376 const char *zReply;
377 SqliteDb *pDb = (SqliteDb*)pArg;
378
379 switch( code ){
380 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
381 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
382 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
383 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
384 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
385 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
386 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
387 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
388 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
389 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
390 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
391 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
392 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
393 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
394 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
395 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
396 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
397 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
398 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
399 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
400 case SQLITE_READ : zCode="SQLITE_READ"; break;
401 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
402 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
403 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
404 default : zCode="????"; break;
405 }
406 Tcl_DStringInit(&str);
407 Tcl_DStringAppend(&str, pDb->zAuth, -1);
408 Tcl_DStringAppendElement(&str, zCode);
409 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
410 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
411 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
412 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
413 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
414 Tcl_DStringFree(&str);
415 zReply = Tcl_GetStringResult(pDb->interp);
416 if( strcmp(zReply,"SQLITE_OK")==0 ){
417 rc = SQLITE_OK;
418 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
419 rc = SQLITE_DENY;
420 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
421 rc = SQLITE_IGNORE;
422 }else{
423 rc = 999;
424 }
425 return rc;
426}
427#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000428
429/*
drh75897232000-05-29 14:26:00 +0000430** The "sqlite" command below creates a new Tcl command for each
431** connection it opens to an SQLite database. This routine is invoked
432** whenever one of those connection-specific commands is executed
433** in Tcl. For example, if you run Tcl code like this:
434**
435** sqlite db1 "my_database"
436** db1 close
437**
438** The first command opens a connection to the "my_database" database
439** and calls that connection "db1". The second command causes this
440** subroutine to be invoked.
441*/
drh6d313162000-09-21 13:01:35 +0000442static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000443 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000444 int choice;
drh0de8c112002-07-06 16:32:14 +0000445 static const char *DB_strs[] = {
drhe22a3342003-04-22 20:30:37 +0000446 "authorizer", "begin_hook", "busy",
447 "changes", "close", "commit_hook",
448 "complete", "errorcode", "eval",
449 "function", "last_insert_rowid", "timeout",
450 0
drh6d313162000-09-21 13:01:35 +0000451 };
drh411995d2002-06-25 19:31:18 +0000452 enum DB_enum {
drhe22a3342003-04-22 20:30:37 +0000453 DB_AUTHORIZER, DB_BEGIN_HOOK, DB_BUSY,
454 DB_CHANGES, DB_CLOSE, DB_COMMIT_HOOK,
455 DB_COMPLETE, DB_ERRORCODE, DB_EVAL,
456 DB_FUNCTION, DB_LAST_INSERT_ROWID,DB_TIMEOUT,
drh6d313162000-09-21 13:01:35 +0000457 };
458
459 if( objc<2 ){
460 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000461 return TCL_ERROR;
462 }
drh411995d2002-06-25 19:31:18 +0000463 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000464 return TCL_ERROR;
465 }
466
drh411995d2002-06-25 19:31:18 +0000467 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000468
drhe22a3342003-04-22 20:30:37 +0000469 /* $db authorizer ?CALLBACK?
470 **
471 ** Invoke the given callback to authorize each SQL operation as it is
472 ** compiled. 5 arguments are appended to the callback before it is
473 ** invoked:
474 **
475 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
476 ** (2) First descriptive name (depends on authorization type)
477 ** (3) Second descriptive name
478 ** (4) Name of the database (ex: "main", "temp")
479 ** (5) Name of trigger that is doing the access
480 **
481 ** The callback should return on of the following strings: SQLITE_OK,
482 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
483 **
484 ** If this method is invoked with no arguments, the current authorization
485 ** callback string is returned.
486 */
487 case DB_AUTHORIZER: {
488 if( objc>3 ){
489 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
490 }else if( objc==2 ){
491 if( pDb->zBegin ){
492 Tcl_AppendResult(interp, pDb->zAuth, 0);
493 }
494 }else{
495 char *zAuth;
496 int len;
497 if( pDb->zAuth ){
498 Tcl_Free(pDb->zAuth);
499 }
500 zAuth = Tcl_GetStringFromObj(objv[2], &len);
501 if( zAuth && len>0 ){
502 pDb->zAuth = Tcl_Alloc( len + 1 );
503 strcpy(pDb->zAuth, zAuth);
504 }else{
505 pDb->zAuth = 0;
506 }
507#ifndef SQLITE_OMIT_AUTHORIZATION
508 if( pDb->zAuth ){
509 pDb->interp = interp;
510 sqlite_set_authorizer(pDb->db, auth_callback, pDb);
511 }else{
512 sqlite_set_authorizer(pDb->db, 0, 0);
513 }
514#endif
515 }
516 break;
517 }
518
519
drh0d1a6432003-04-03 15:46:04 +0000520 /* $db begin_callback ?CALLBACK?
521 **
522 ** Invoke the given callback at the beginning of every SQL transaction.
523 ** If the callback throws an exception or returns non-zero, then the
524 ** transaction is aborted. If CALLBACK is an empty string, the callback
525 ** is disabled.
526 */
527 case DB_BEGIN_HOOK: {
528 if( objc>3 ){
529 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
530 }else if( objc==2 ){
531 if( pDb->zBegin ){
532 Tcl_AppendResult(interp, pDb->zBegin, 0);
533 }
534 }else{
535 char *zBegin;
536 int len;
537 if( pDb->zBegin ){
538 Tcl_Free(pDb->zBegin);
539 }
540 zBegin = Tcl_GetStringFromObj(objv[2], &len);
541 if( zBegin && len>0 ){
542 pDb->zBegin = Tcl_Alloc( len + 1 );
543 strcpy(pDb->zBegin, zBegin);
544 }else{
545 pDb->zBegin = 0;
546 }
547 if( pDb->zBegin ){
548 pDb->interp = interp;
549 sqlite_begin_hook(pDb->db, DbBeginHandler, pDb);
550 }else{
551 sqlite_begin_hook(pDb->db, 0, 0);
552 }
553 }
554 break;
555 }
556
drhbec3f402000-08-04 13:49:02 +0000557 /* $db busy ?CALLBACK?
558 **
559 ** Invoke the given callback if an SQL statement attempts to open
560 ** a locked database file.
561 */
drh6d313162000-09-21 13:01:35 +0000562 case DB_BUSY: {
563 if( objc>3 ){
564 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000565 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000566 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000567 if( pDb->zBusy ){
568 Tcl_AppendResult(interp, pDb->zBusy, 0);
569 }
570 }else{
drh6d313162000-09-21 13:01:35 +0000571 char *zBusy;
572 int len;
drhbec3f402000-08-04 13:49:02 +0000573 if( pDb->zBusy ){
574 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000575 }
drh6d313162000-09-21 13:01:35 +0000576 zBusy = Tcl_GetStringFromObj(objv[2], &len);
577 if( zBusy && len>0 ){
578 pDb->zBusy = Tcl_Alloc( len + 1 );
579 strcpy(pDb->zBusy, zBusy);
580 }else{
581 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000582 }
583 if( pDb->zBusy ){
584 pDb->interp = interp;
585 sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000586 }else{
587 sqlite_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000588 }
589 }
drh6d313162000-09-21 13:01:35 +0000590 break;
591 }
drhbec3f402000-08-04 13:49:02 +0000592
drhc8d30ac2002-04-12 10:08:59 +0000593 /*
594 ** $db changes
595 **
596 ** Return the number of rows that were modified, inserted, or deleted by
597 ** the most recent "eval".
598 */
599 case DB_CHANGES: {
600 Tcl_Obj *pResult;
601 int nChange;
602 if( objc!=2 ){
603 Tcl_WrongNumArgs(interp, 2, objv, "");
604 return TCL_ERROR;
605 }
606 nChange = sqlite_changes(pDb->db);
607 pResult = Tcl_GetObjResult(interp);
608 Tcl_SetIntObj(pResult, nChange);
609 break;
610 }
611
drh75897232000-05-29 14:26:00 +0000612 /* $db close
613 **
614 ** Shutdown the database
615 */
drh6d313162000-09-21 13:01:35 +0000616 case DB_CLOSE: {
617 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
618 break;
619 }
drh75897232000-05-29 14:26:00 +0000620
drh0d1a6432003-04-03 15:46:04 +0000621 /* $db commit_hook ?CALLBACK?
622 **
623 ** Invoke the given callback just before committing every SQL transaction.
624 ** If the callback throws an exception or returns non-zero, then the
625 ** transaction is aborted. If CALLBACK is an empty string, the callback
626 ** is disabled.
627 */
628 case DB_COMMIT_HOOK: {
629 if( objc>3 ){
630 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
631 }else if( objc==2 ){
632 if( pDb->zCommit ){
633 Tcl_AppendResult(interp, pDb->zCommit, 0);
634 }
635 }else{
636 char *zCommit;
637 int len;
638 if( pDb->zCommit ){
639 Tcl_Free(pDb->zCommit);
640 }
641 zCommit = Tcl_GetStringFromObj(objv[2], &len);
642 if( zCommit && len>0 ){
643 pDb->zCommit = Tcl_Alloc( len + 1 );
644 strcpy(pDb->zCommit, zCommit);
645 }else{
646 pDb->zCommit = 0;
647 }
648 if( pDb->zCommit ){
649 pDb->interp = interp;
650 sqlite_commit_hook(pDb->db, DbCommitHandler, pDb);
651 }else{
652 sqlite_commit_hook(pDb->db, 0, 0);
653 }
654 }
655 break;
656 }
657
drh75897232000-05-29 14:26:00 +0000658 /* $db complete SQL
659 **
660 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
661 ** additional lines of input are needed. This is similar to the
662 ** built-in "info complete" command of Tcl.
663 */
drh6d313162000-09-21 13:01:35 +0000664 case DB_COMPLETE: {
665 Tcl_Obj *pResult;
666 int isComplete;
667 if( objc!=3 ){
668 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000669 return TCL_ERROR;
670 }
drh6d313162000-09-21 13:01:35 +0000671 isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
672 pResult = Tcl_GetObjResult(interp);
673 Tcl_SetBooleanObj(pResult, isComplete);
674 break;
675 }
drhdcd997e2003-01-31 17:21:49 +0000676
677 /*
678 ** $db errorcode
679 **
680 ** Return the numeric error code that was returned by the most recent
681 ** call to sqlite_exec().
682 */
683 case DB_ERRORCODE: {
684 Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc));
685 break;
686 }
drh75897232000-05-29 14:26:00 +0000687
688 /*
689 ** $db eval $sql ?array { ...code... }?
690 **
691 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000692 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000693 ** If "array" and "code" are omitted, then no callback is every invoked.
694 ** If "array" is an empty string, then the values are placed in variables
695 ** that have the same name as the fields extracted by the query.
696 */
drh6d313162000-09-21 13:01:35 +0000697 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000698 CallbackData cbData;
699 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000700 char *zSql;
drh75897232000-05-29 14:26:00 +0000701 int rc;
drh297ecf12001-04-05 15:57:13 +0000702#ifdef UTF_TRANSLATION_NEEDED
703 Tcl_DString dSql;
drh6d4abfb2001-10-22 02:58:08 +0000704 int i;
drh297ecf12001-04-05 15:57:13 +0000705#endif
drh75897232000-05-29 14:26:00 +0000706
drh6d313162000-09-21 13:01:35 +0000707 if( objc!=5 && objc!=3 ){
708 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000709 return TCL_ERROR;
710 }
drhbec3f402000-08-04 13:49:02 +0000711 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000712 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh297ecf12001-04-05 15:57:13 +0000713#ifdef UTF_TRANSLATION_NEEDED
714 Tcl_DStringInit(&dSql);
715 Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
716 zSql = Tcl_DStringValue(&dSql);
717#endif
drh6d313162000-09-21 13:01:35 +0000718 Tcl_IncrRefCount(objv[2]);
719 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000720 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000721 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000722 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
723 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000724 cbData.tcl_rc = TCL_OK;
drh6d4abfb2001-10-22 02:58:08 +0000725 cbData.nColName = 0;
726 cbData.azColName = 0;
drh75897232000-05-29 14:26:00 +0000727 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000728 Tcl_IncrRefCount(objv[3]);
729 Tcl_IncrRefCount(objv[4]);
730 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
731 Tcl_DecrRefCount(objv[4]);
732 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000733 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000734 }else{
drh6d313162000-09-21 13:01:35 +0000735 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000736 cbData.tcl_rc = TCL_OK;
drh6d313162000-09-21 13:01:35 +0000737 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
738 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000739 }
drhdcd997e2003-01-31 17:21:49 +0000740 pDb->rc = rc;
drhb798fa62002-09-03 19:43:23 +0000741 if( rc==SQLITE_ABORT ){
742 if( zErrMsg ) free(zErrMsg);
743 rc = cbData.tcl_rc;
744 }else if( zErrMsg ){
drh75897232000-05-29 14:26:00 +0000745 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
746 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000747 rc = TCL_ERROR;
drhb798fa62002-09-03 19:43:23 +0000748 }else if( rc!=SQLITE_OK ){
drh6d4abfb2001-10-22 02:58:08 +0000749 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
750 rc = TCL_ERROR;
drh960e8c62001-04-03 16:53:21 +0000751 }else{
drh75897232000-05-29 14:26:00 +0000752 }
drh6d313162000-09-21 13:01:35 +0000753 Tcl_DecrRefCount(objv[2]);
drh297ecf12001-04-05 15:57:13 +0000754#ifdef UTF_TRANSLATION_NEEDED
755 Tcl_DStringFree(&dSql);
drh6d4abfb2001-10-22 02:58:08 +0000756 if( objc==5 && cbData.azColName ){
757 for(i=0; i<cbData.nColName; i++){
758 if( cbData.azColName[i] ) free(cbData.azColName[i]);
759 }
760 free(cbData.azColName);
drhce927062001-11-09 13:41:09 +0000761 cbData.azColName = 0;
drh6d4abfb2001-10-22 02:58:08 +0000762 }
drh297ecf12001-04-05 15:57:13 +0000763#endif
drh75897232000-05-29 14:26:00 +0000764 return rc;
drh6d313162000-09-21 13:01:35 +0000765 }
drhbec3f402000-08-04 13:49:02 +0000766
767 /*
drhcabb0812002-09-14 13:47:32 +0000768 ** $db function NAME SCRIPT
769 **
770 ** Create a new SQL function called NAME. Whenever that function is
771 ** called, invoke SCRIPT to evaluate the function.
772 */
773 case DB_FUNCTION: {
774 SqlFunc *pFunc;
775 char *zName;
776 char *zScript;
777 int nScript;
778 if( objc!=4 ){
779 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
780 return TCL_ERROR;
781 }
782 zName = Tcl_GetStringFromObj(objv[2], 0);
783 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
784 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
785 if( pFunc==0 ) return TCL_ERROR;
786 pFunc->interp = interp;
787 pFunc->pNext = pDb->pFunc;
788 pFunc->zScript = (char*)&pFunc[1];
789 strcpy(pFunc->zScript, zScript);
790 sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc);
791 sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC);
792 break;
793 }
794
795 /*
drhaf9ff332002-01-16 21:00:27 +0000796 ** $db last_insert_rowid
797 **
798 ** Return an integer which is the ROWID for the most recent insert.
799 */
800 case DB_LAST_INSERT_ROWID: {
801 Tcl_Obj *pResult;
802 int rowid;
803 if( objc!=2 ){
804 Tcl_WrongNumArgs(interp, 2, objv, "");
805 return TCL_ERROR;
806 }
807 rowid = sqlite_last_insert_rowid(pDb->db);
808 pResult = Tcl_GetObjResult(interp);
809 Tcl_SetIntObj(pResult, rowid);
810 break;
811 }
812
813 /*
drhbec3f402000-08-04 13:49:02 +0000814 ** $db timeout MILLESECONDS
815 **
816 ** Delay for the number of milliseconds specified when a file is locked.
817 */
drh6d313162000-09-21 13:01:35 +0000818 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000819 int ms;
drh6d313162000-09-21 13:01:35 +0000820 if( objc!=3 ){
821 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000822 return TCL_ERROR;
823 }
drh6d313162000-09-21 13:01:35 +0000824 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
drhbec3f402000-08-04 13:49:02 +0000825 sqlite_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000826 break;
drh75897232000-05-29 14:26:00 +0000827 }
drh6d313162000-09-21 13:01:35 +0000828 } /* End of the SWITCH statement */
drh75897232000-05-29 14:26:00 +0000829 return TCL_OK;
830}
831
832/*
833** sqlite DBNAME FILENAME ?MODE?
834**
835** This is the main Tcl command. When the "sqlite" Tcl command is
836** invoked, this routine runs to process that command.
837**
838** The first argument, DBNAME, is an arbitrary name for a new
839** database connection. This command creates a new command named
840** DBNAME that is used to control that connection. The database
841** connection is deleted when the DBNAME command is deleted.
842**
843** The second argument is the name of the directory that contains
844** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000845**
846** For testing purposes, we also support the following:
847**
848** sqlite -encoding
849**
850** Return the encoding used by LIKE and GLOB operators. Choices
851** are UTF-8 and iso8859.
852**
drh647cb0e2002-11-04 19:32:25 +0000853** sqlite -version
854**
855** Return the version number of the SQLite library.
856**
drhfbc3eab2001-04-06 16:13:42 +0000857** sqlite -tcl-uses-utf
858**
859** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
860** not. Used by tests to make sure the library was compiled
861** correctly.
drh75897232000-05-29 14:26:00 +0000862*/
863static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){
864 int mode;
drhbec3f402000-08-04 13:49:02 +0000865 SqliteDb *p;
drh75897232000-05-29 14:26:00 +0000866 char *zErrMsg;
drh06b27182002-06-26 20:06:05 +0000867 char zBuf[80];
drhfbc3eab2001-04-06 16:13:42 +0000868 if( argc==2 ){
869 if( strcmp(argv[1],"-encoding")==0 ){
870 Tcl_AppendResult(interp,sqlite_encoding,0);
871 return TCL_OK;
872 }
drh647cb0e2002-11-04 19:32:25 +0000873 if( strcmp(argv[1],"-version")==0 ){
874 Tcl_AppendResult(interp,sqlite_version,0);
875 return TCL_OK;
876 }
drhfbc3eab2001-04-06 16:13:42 +0000877 if( strcmp(argv[1],"-tcl-uses-utf")==0 ){
878#ifdef TCL_UTF_MAX
879 Tcl_AppendResult(interp,"1",0);
880#else
881 Tcl_AppendResult(interp,"0",0);
882#endif
883 return TCL_OK;
884 }
885 }
drh75897232000-05-29 14:26:00 +0000886 if( argc!=3 && argc!=4 ){
887 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
888 " HANDLE FILENAME ?MODE?\"", 0);
889 return TCL_ERROR;
890 }
891 if( argc==3 ){
drh58b95762000-06-02 01:17:37 +0000892 mode = 0666;
drh75897232000-05-29 14:26:00 +0000893 }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){
894 return TCL_ERROR;
895 }
896 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +0000897 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +0000898 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +0000899 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
900 return TCL_ERROR;
901 }
902 memset(p, 0, sizeof(*p));
903 p->db = sqlite_open(argv[2], mode, &zErrMsg);
904 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +0000905 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +0000906 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +0000907 free(zErrMsg);
908 return TCL_ERROR;
909 }
drh6d313162000-09-21 13:01:35 +0000910 Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +0000911
drh06b27182002-06-26 20:06:05 +0000912 /* The return value is the value of the sqlite* pointer
913 */
914 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +0000915 if( strncmp(zBuf,"0x",2) ){
916 sprintf(zBuf, "0x%p", p->db);
917 }
drh06b27182002-06-26 20:06:05 +0000918 Tcl_AppendResult(interp, zBuf, 0);
919
drhc22bd472002-05-10 13:14:07 +0000920 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +0000921 ** SQL function.
drhc22bd472002-05-10 13:14:07 +0000922 */
drh28b4e482002-03-11 02:06:13 +0000923#ifdef SQLITE_TEST
924 {
drhc22bd472002-05-10 13:14:07 +0000925 extern void Md5_Register(sqlite*);
926 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +0000927 }
drh28b4e482002-03-11 02:06:13 +0000928#endif
drh75897232000-05-29 14:26:00 +0000929 return TCL_OK;
930}
931
932/*
drh90ca9752001-09-28 17:47:14 +0000933** Provide a dummy Tcl_InitStubs if we are using this as a static
934** library.
935*/
936#ifndef USE_TCL_STUBS
937# undef Tcl_InitStubs
938# define Tcl_InitStubs(a,b,c)
939#endif
940
941/*
drh75897232000-05-29 14:26:00 +0000942** Initialize this module.
943**
944** This Tcl module contains only a single new Tcl command named "sqlite".
945** (Hence there is no namespace. There is no point in using a namespace
946** if the extension only supplies one new name!) The "sqlite" command is
947** used to open a new SQLite database. See the DbMain() routine above
948** for additional information.
949*/
950int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +0000951 Tcl_InitStubs(interp, "8.0", 0);
drhc2eef3b2002-08-31 18:53:06 +0000952 Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000953 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh90ca9752001-09-28 17:47:14 +0000954 return TCL_OK;
955}
956int Tclsqlite_Init(Tcl_Interp *interp){
957 Tcl_InitStubs(interp, "8.0", 0);
drhc2eef3b2002-08-31 18:53:06 +0000958 Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000959 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh75897232000-05-29 14:26:00 +0000960 return TCL_OK;
961}
962int Sqlite_SafeInit(Tcl_Interp *interp){
963 return TCL_OK;
964}
drh90ca9752001-09-28 17:47:14 +0000965int Tclsqlite_SafeInit(Tcl_Interp *interp){
966 return TCL_OK;
967}
drh75897232000-05-29 14:26:00 +0000968
drh3cebbde2000-10-19 14:59:27 +0000969#if 0
drh75897232000-05-29 14:26:00 +0000970/*
971** If compiled using mktclapp, this routine runs to initialize
972** everything.
973*/
974int Et_AppInit(Tcl_Interp *interp){
975 return Sqlite_Init(interp);
976}
drh3cebbde2000-10-19 14:59:27 +0000977#endif
drh348784e2000-05-29 20:41:49 +0000978
979/*
980** If the macro TCLSH is defined and is one, then put in code for the
981** "main" routine that will initialize Tcl.
982*/
983#if defined(TCLSH) && TCLSH==1
984static char zMainloop[] =
985 "set line {}\n"
986 "while {![eof stdin]} {\n"
987 "if {$line!=\"\"} {\n"
988 "puts -nonewline \"> \"\n"
989 "} else {\n"
990 "puts -nonewline \"% \"\n"
991 "}\n"
992 "flush stdout\n"
993 "append line [gets stdin]\n"
994 "if {[info complete $line]} {\n"
995 "if {[catch {uplevel #0 $line} result]} {\n"
996 "puts stderr \"Error: $result\"\n"
997 "} elseif {$result!=\"\"} {\n"
998 "puts $result\n"
999 "}\n"
1000 "set line {}\n"
1001 "} else {\n"
1002 "append line \\n\n"
1003 "}\n"
1004 "}\n"
1005;
1006
1007#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1008int TCLSH_MAIN(int argc, char **argv){
1009 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001010 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001011 interp = Tcl_CreateInterp();
1012 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001013#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001014 {
1015 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001016 extern int Sqlitetest2_Init(Tcl_Interp*);
1017 extern int Sqlitetest3_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001018 extern int Md5_Init(Tcl_Interp*);
drhd1bf3512001-04-07 15:24:33 +00001019 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001020 Sqlitetest2_Init(interp);
1021 Sqlitetest3_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001022 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001023 }
1024#endif
drh348784e2000-05-29 20:41:49 +00001025 if( argc>=2 ){
1026 int i;
1027 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1028 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1029 for(i=2; i<argc; i++){
1030 Tcl_SetVar(interp, "argv", argv[i],
1031 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1032 }
1033 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001034 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001035 if( zInfo==0 ) zInfo = interp->result;
1036 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001037 return 1;
1038 }
1039 }else{
1040 Tcl_GlobalEval(interp, zMainloop);
1041 }
1042 return 0;
1043}
1044#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001045
1046#endif /* !defined(NO_TCL) */