blob: 7893b73b5ba1fcb0673934c8fade0b124d2f01da [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**
drh98808ba2001-10-18 12:34:46 +000014** $Id: tclsqlite.c,v 1.25 2001/10/18 12:34:47 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
drh75897232000-05-29 14:26:00 +000018#include "sqlite.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>
22
23/*
drh98808ba2001-10-18 12:34:46 +000024** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
25** have to do a translation when going between the two. Set the
26** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
27** this translation.
28*/
29#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
30# define UTF_TRANSLATION_NEEDED 1
31#endif
32
33/*
drhbec3f402000-08-04 13:49:02 +000034** There is one instance of this structure for each SQLite database
35** that has been opened by the SQLite TCL interface.
36*/
37typedef struct SqliteDb SqliteDb;
38struct SqliteDb {
39 sqlite *db; /* The "real" database structure */
40 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000041 char *zBusy; /* The busy callback routine */
drhbec3f402000-08-04 13:49:02 +000042};
43
44/*
drh75897232000-05-29 14:26:00 +000045** An instance of this structure passes information thru the sqlite
46** logic from the original TCL command into the callback routine.
47*/
48typedef struct CallbackData CallbackData;
49struct CallbackData {
50 Tcl_Interp *interp; /* The TCL interpreter */
51 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000052 Tcl_Obj *pCode; /* The code to execute for each row */
drh75897232000-05-29 14:26:00 +000053 int once; /* Set only for the first invocation of callback */
drh960e8c62001-04-03 16:53:21 +000054 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000055#ifdef UTF_TRANSLATION_NEEDED
56 int nColName; /* Number of entries in the azColName[] array */
57 char **azColName; /* Column names translated to UTF-8 */
drh297ecf12001-04-05 15:57:13 +000058#endif
drh98808ba2001-10-18 12:34:46 +000059};
drh297ecf12001-04-05 15:57:13 +000060
61/*
drh75897232000-05-29 14:26:00 +000062** Called for each row of the result.
63*/
64static int DbEvalCallback(
65 void *clientData, /* An instance of CallbackData */
66 int nCol, /* Number of columns in the result */
67 char ** azCol, /* Data for each column */
68 char ** azN /* Name for each column */
69){
70 CallbackData *cbData = (CallbackData*)clientData;
71 int i, rc;
drh297ecf12001-04-05 15:57:13 +000072#ifdef UTF_TRANSLATION_NEEDED
73 Tcl_DString dCol;
74#endif
drh75897232000-05-29 14:26:00 +000075 if( cbData->zArray[0] ){
76 if( cbData->once ){
drh9b0d0a82000-09-30 22:46:05 +000077 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
drh75897232000-05-29 14:26:00 +000078 for(i=0; i<nCol; i++){
79 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
80 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
81 }
82 }
83 for(i=0; i<nCol; i++){
drhc61053b2000-06-04 12:58:36 +000084 char *z = azCol[i];
85 if( z==0 ) z = "";
drh297ecf12001-04-05 15:57:13 +000086#ifdef UTF_TRANSLATION_NEEDED
87 Tcl_DStringInit(&dCol);
88 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
89 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i],
90 Tcl_DStringValue(&dCol), 0);
91 Tcl_DStringFree(&dCol);
92#else
drhc61053b2000-06-04 12:58:36 +000093 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
drh297ecf12001-04-05 15:57:13 +000094#endif
drh75897232000-05-29 14:26:00 +000095 }
96 }else{
97 for(i=0; i<nCol; i++){
drhc61053b2000-06-04 12:58:36 +000098 char *z = azCol[i];
99 if( z==0 ) z = "";
drh297ecf12001-04-05 15:57:13 +0000100#ifdef UTF_TRANSLATION_NEEDED
101 Tcl_DStringInit(&dCol);
102 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
103 Tcl_SetVar(cbData->interp, azN[i], Tcl_DStringValue(&dCol), 0);
104 Tcl_DStringFree(&dCol);
105#else
drhc61053b2000-06-04 12:58:36 +0000106 Tcl_SetVar(cbData->interp, azN[i], z, 0);
drh297ecf12001-04-05 15:57:13 +0000107#endif
drh75897232000-05-29 14:26:00 +0000108 }
109 }
110 cbData->once = 0;
drh6d313162000-09-21 13:01:35 +0000111 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
drh960e8c62001-04-03 16:53:21 +0000112 if( rc==TCL_CONTINUE ) rc = TCL_OK;
113 cbData->tcl_rc = rc;
114 return rc!=TCL_OK;
drh75897232000-05-29 14:26:00 +0000115}
116
117/*
drh6d313162000-09-21 13:01:35 +0000118** This is an alternative callback for database queries. Instead
119** of invoking a TCL script to handle the result, this callback just
120** appends each column of the result to a list. After the query
121** is complete, the list is returned.
122*/
123static int DbEvalCallback2(
124 void *clientData, /* An instance of CallbackData */
125 int nCol, /* Number of columns in the result */
126 char ** azCol, /* Data for each column */
127 char ** azN /* Name for each column */
128){
129 Tcl_Obj *pList = (Tcl_Obj*)clientData;
130 int i;
131 for(i=0; i<nCol; i++){
132 Tcl_Obj *pElem;
133 if( azCol[i] && *azCol[i] ){
drh297ecf12001-04-05 15:57:13 +0000134#ifdef UTF_TRANSLATION_NEEDED
135 Tcl_DString dCol;
136 Tcl_DStringInit(&dCol);
137 Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol);
138 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
139 Tcl_DStringFree(&dCol);
140#else
drh6d313162000-09-21 13:01:35 +0000141 pElem = Tcl_NewStringObj(azCol[i], -1);
drh297ecf12001-04-05 15:57:13 +0000142#endif
drh6d313162000-09-21 13:01:35 +0000143 }else{
144 pElem = Tcl_NewObj();
145 }
146 Tcl_ListObjAppendElement(0, pList, pElem);
147 }
148 return 0;
149}
150
151/*
drh75897232000-05-29 14:26:00 +0000152** Called when the command is deleted.
153*/
154static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000155 SqliteDb *pDb = (SqliteDb*)db;
156 sqlite_close(pDb->db);
157 if( pDb->zBusy ){
158 Tcl_Free(pDb->zBusy);
159 }
160 Tcl_Free((char*)pDb);
161}
162
163/*
164** This routine is called when a database file is locked while trying
165** to execute SQL.
166*/
167static int DbBusyHandler(void *cd, const char *zTable, int nTries){
168 SqliteDb *pDb = (SqliteDb*)cd;
169 int rc;
170 char zVal[30];
171 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000172 Tcl_DString cmd;
173
174 Tcl_DStringInit(&cmd);
175 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
176 Tcl_DStringAppendElement(&cmd, zTable);
177 sprintf(zVal, " %d", nTries);
178 Tcl_DStringAppend(&cmd, zVal, -1);
179 zCmd = Tcl_DStringValue(&cmd);
180 rc = Tcl_Eval(pDb->interp, zCmd);
181 Tcl_DStringFree(&cmd);
182 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
183 return 0;
184 }
185 return 1;
drh75897232000-05-29 14:26:00 +0000186}
187
188/*
189** The "sqlite" command below creates a new Tcl command for each
190** connection it opens to an SQLite database. This routine is invoked
191** whenever one of those connection-specific commands is executed
192** in Tcl. For example, if you run Tcl code like this:
193**
194** sqlite db1 "my_database"
195** db1 close
196**
197** The first command opens a connection to the "my_database" database
198** and calls that connection "db1". The second command causes this
199** subroutine to be invoked.
200*/
drh6d313162000-09-21 13:01:35 +0000201static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000202 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000203 int choice;
204 static char *DB_optStrs[] = {
drh960e8c62001-04-03 16:53:21 +0000205 "busy", "close", "complete", "eval", "timeout", 0
drh6d313162000-09-21 13:01:35 +0000206 };
207 enum DB_opts {
208 DB_BUSY, DB_CLOSE, DB_COMPLETE, DB_EVAL, DB_TIMEOUT
209 };
210
211 if( objc<2 ){
212 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000213 return TCL_ERROR;
214 }
drh6d313162000-09-21 13:01:35 +0000215 if( Tcl_GetIndexFromObj(interp, objv[1], DB_optStrs, "option", 0, &choice) ){
216 return TCL_ERROR;
217 }
218
219 switch( (enum DB_opts)choice ){
drh75897232000-05-29 14:26:00 +0000220
drhbec3f402000-08-04 13:49:02 +0000221 /* $db busy ?CALLBACK?
222 **
223 ** Invoke the given callback if an SQL statement attempts to open
224 ** a locked database file.
225 */
drh6d313162000-09-21 13:01:35 +0000226 case DB_BUSY: {
227 if( objc>3 ){
228 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000229 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000230 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000231 if( pDb->zBusy ){
232 Tcl_AppendResult(interp, pDb->zBusy, 0);
233 }
234 }else{
drh6d313162000-09-21 13:01:35 +0000235 char *zBusy;
236 int len;
drhbec3f402000-08-04 13:49:02 +0000237 if( pDb->zBusy ){
238 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000239 }
drh6d313162000-09-21 13:01:35 +0000240 zBusy = Tcl_GetStringFromObj(objv[2], &len);
241 if( zBusy && len>0 ){
242 pDb->zBusy = Tcl_Alloc( len + 1 );
243 strcpy(pDb->zBusy, zBusy);
244 }else{
245 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000246 }
247 if( pDb->zBusy ){
248 pDb->interp = interp;
249 sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000250 }else{
251 sqlite_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000252 }
253 }
drh6d313162000-09-21 13:01:35 +0000254 break;
255 }
drhbec3f402000-08-04 13:49:02 +0000256
drh75897232000-05-29 14:26:00 +0000257 /* $db close
258 **
259 ** Shutdown the database
260 */
drh6d313162000-09-21 13:01:35 +0000261 case DB_CLOSE: {
262 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
263 break;
264 }
drh75897232000-05-29 14:26:00 +0000265
266 /* $db complete SQL
267 **
268 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
269 ** additional lines of input are needed. This is similar to the
270 ** built-in "info complete" command of Tcl.
271 */
drh6d313162000-09-21 13:01:35 +0000272 case DB_COMPLETE: {
273 Tcl_Obj *pResult;
274 int isComplete;
275 if( objc!=3 ){
276 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000277 return TCL_ERROR;
278 }
drh6d313162000-09-21 13:01:35 +0000279 isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
280 pResult = Tcl_GetObjResult(interp);
281 Tcl_SetBooleanObj(pResult, isComplete);
282 break;
283 }
drh75897232000-05-29 14:26:00 +0000284
285 /*
286 ** $db eval $sql ?array { ...code... }?
287 **
288 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000289 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000290 ** If "array" and "code" are omitted, then no callback is every invoked.
291 ** If "array" is an empty string, then the values are placed in variables
292 ** that have the same name as the fields extracted by the query.
293 */
drh6d313162000-09-21 13:01:35 +0000294 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000295 CallbackData cbData;
296 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000297 char *zSql;
drh75897232000-05-29 14:26:00 +0000298 int rc;
drh297ecf12001-04-05 15:57:13 +0000299#ifdef UTF_TRANSLATION_NEEDED
300 Tcl_DString dSql;
301#endif
drh75897232000-05-29 14:26:00 +0000302
drh6d313162000-09-21 13:01:35 +0000303 if( objc!=5 && objc!=3 ){
304 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000305 return TCL_ERROR;
306 }
drhbec3f402000-08-04 13:49:02 +0000307 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000308 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh297ecf12001-04-05 15:57:13 +0000309#ifdef UTF_TRANSLATION_NEEDED
310 Tcl_DStringInit(&dSql);
311 Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
312 zSql = Tcl_DStringValue(&dSql);
313#endif
drh6d313162000-09-21 13:01:35 +0000314 Tcl_IncrRefCount(objv[2]);
315 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000316 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000317 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000318 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
319 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000320 cbData.tcl_rc = TCL_OK;
drh75897232000-05-29 14:26:00 +0000321 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000322 Tcl_IncrRefCount(objv[3]);
323 Tcl_IncrRefCount(objv[4]);
324 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
325 Tcl_DecrRefCount(objv[4]);
326 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000327 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000328 }else{
drh6d313162000-09-21 13:01:35 +0000329 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000330 cbData.tcl_rc = TCL_OK;
drh6d313162000-09-21 13:01:35 +0000331 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
332 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000333 }
334 if( zErrMsg ){
335 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
336 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000337 rc = TCL_ERROR;
338 }else{
339 rc = cbData.tcl_rc;
drh75897232000-05-29 14:26:00 +0000340 }
drh6d313162000-09-21 13:01:35 +0000341 Tcl_DecrRefCount(objv[2]);
drh297ecf12001-04-05 15:57:13 +0000342#ifdef UTF_TRANSLATION_NEEDED
343 Tcl_DStringFree(&dSql);
344#endif
drh75897232000-05-29 14:26:00 +0000345 return rc;
drh6d313162000-09-21 13:01:35 +0000346 }
drhbec3f402000-08-04 13:49:02 +0000347
348 /*
349 ** $db timeout MILLESECONDS
350 **
351 ** Delay for the number of milliseconds specified when a file is locked.
352 */
drh6d313162000-09-21 13:01:35 +0000353 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000354 int ms;
drh6d313162000-09-21 13:01:35 +0000355 if( objc!=3 ){
356 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000357 return TCL_ERROR;
358 }
drh6d313162000-09-21 13:01:35 +0000359 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
drhbec3f402000-08-04 13:49:02 +0000360 sqlite_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000361 break;
drh75897232000-05-29 14:26:00 +0000362 }
drh6d313162000-09-21 13:01:35 +0000363 } /* End of the SWITCH statement */
drh75897232000-05-29 14:26:00 +0000364 return TCL_OK;
365}
366
367/*
368** sqlite DBNAME FILENAME ?MODE?
369**
370** This is the main Tcl command. When the "sqlite" Tcl command is
371** invoked, this routine runs to process that command.
372**
373** The first argument, DBNAME, is an arbitrary name for a new
374** database connection. This command creates a new command named
375** DBNAME that is used to control that connection. The database
376** connection is deleted when the DBNAME command is deleted.
377**
378** The second argument is the name of the directory that contains
379** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000380**
381** For testing purposes, we also support the following:
382**
383** sqlite -encoding
384**
385** Return the encoding used by LIKE and GLOB operators. Choices
386** are UTF-8 and iso8859.
387**
388** sqlite -tcl-uses-utf
389**
390** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
391** not. Used by tests to make sure the library was compiled
392** correctly.
drh75897232000-05-29 14:26:00 +0000393*/
394static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){
395 int mode;
drhbec3f402000-08-04 13:49:02 +0000396 SqliteDb *p;
drh75897232000-05-29 14:26:00 +0000397 char *zErrMsg;
drhfbc3eab2001-04-06 16:13:42 +0000398 if( argc==2 ){
399 if( strcmp(argv[1],"-encoding")==0 ){
400 Tcl_AppendResult(interp,sqlite_encoding,0);
401 return TCL_OK;
402 }
403 if( strcmp(argv[1],"-tcl-uses-utf")==0 ){
404#ifdef TCL_UTF_MAX
405 Tcl_AppendResult(interp,"1",0);
406#else
407 Tcl_AppendResult(interp,"0",0);
408#endif
409 return TCL_OK;
410 }
411 }
drh75897232000-05-29 14:26:00 +0000412 if( argc!=3 && argc!=4 ){
413 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
414 " HANDLE FILENAME ?MODE?\"", 0);
415 return TCL_ERROR;
416 }
417 if( argc==3 ){
drh58b95762000-06-02 01:17:37 +0000418 mode = 0666;
drh75897232000-05-29 14:26:00 +0000419 }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){
420 return TCL_ERROR;
421 }
422 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +0000423 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +0000424 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +0000425 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
426 return TCL_ERROR;
427 }
428 memset(p, 0, sizeof(*p));
429 p->db = sqlite_open(argv[2], mode, &zErrMsg);
430 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +0000431 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +0000432 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +0000433 free(zErrMsg);
434 return TCL_ERROR;
435 }
drh6d313162000-09-21 13:01:35 +0000436 Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd);
drh75897232000-05-29 14:26:00 +0000437 return TCL_OK;
438}
439
440/*
drh90ca9752001-09-28 17:47:14 +0000441** Provide a dummy Tcl_InitStubs if we are using this as a static
442** library.
443*/
444#ifndef USE_TCL_STUBS
445# undef Tcl_InitStubs
446# define Tcl_InitStubs(a,b,c)
447#endif
448
449/*
drh75897232000-05-29 14:26:00 +0000450** Initialize this module.
451**
452** This Tcl module contains only a single new Tcl command named "sqlite".
453** (Hence there is no namespace. There is no point in using a namespace
454** if the extension only supplies one new name!) The "sqlite" command is
455** used to open a new SQLite database. See the DbMain() routine above
456** for additional information.
457*/
458int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +0000459 Tcl_InitStubs(interp, "8.0", 0);
460 Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0);
461 Tcl_PkgProvide(interp, "sqlite", "1.0");
462 return TCL_OK;
463}
464int Tclsqlite_Init(Tcl_Interp *interp){
465 Tcl_InitStubs(interp, "8.0", 0);
drh75897232000-05-29 14:26:00 +0000466 Tcl_CreateCommand(interp, "sqlite", DbMain, 0, 0);
drh167a4b12000-08-17 09:49:59 +0000467 Tcl_PkgProvide(interp, "sqlite", "1.0");
drh75897232000-05-29 14:26:00 +0000468 return TCL_OK;
469}
470int Sqlite_SafeInit(Tcl_Interp *interp){
471 return TCL_OK;
472}
drh90ca9752001-09-28 17:47:14 +0000473int Tclsqlite_SafeInit(Tcl_Interp *interp){
474 return TCL_OK;
475}
drh75897232000-05-29 14:26:00 +0000476
drh3cebbde2000-10-19 14:59:27 +0000477#if 0
drh75897232000-05-29 14:26:00 +0000478/*
479** If compiled using mktclapp, this routine runs to initialize
480** everything.
481*/
482int Et_AppInit(Tcl_Interp *interp){
483 return Sqlite_Init(interp);
484}
drh3cebbde2000-10-19 14:59:27 +0000485#endif
drh348784e2000-05-29 20:41:49 +0000486
487/*
488** If the macro TCLSH is defined and is one, then put in code for the
489** "main" routine that will initialize Tcl.
490*/
491#if defined(TCLSH) && TCLSH==1
492static char zMainloop[] =
493 "set line {}\n"
494 "while {![eof stdin]} {\n"
495 "if {$line!=\"\"} {\n"
496 "puts -nonewline \"> \"\n"
497 "} else {\n"
498 "puts -nonewline \"% \"\n"
499 "}\n"
500 "flush stdout\n"
501 "append line [gets stdin]\n"
502 "if {[info complete $line]} {\n"
503 "if {[catch {uplevel #0 $line} result]} {\n"
504 "puts stderr \"Error: $result\"\n"
505 "} elseif {$result!=\"\"} {\n"
506 "puts $result\n"
507 "}\n"
508 "set line {}\n"
509 "} else {\n"
510 "append line \\n\n"
511 "}\n"
512 "}\n"
513;
514
515#define TCLSH_MAIN main /* Needed to fake out mktclapp */
516int TCLSH_MAIN(int argc, char **argv){
517 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +0000518 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +0000519 interp = Tcl_CreateInterp();
520 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +0000521#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +0000522 {
523 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +0000524 extern int Sqlitetest2_Init(Tcl_Interp*);
525 extern int Sqlitetest3_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +0000526 extern int Md5_Init(Tcl_Interp*);
drhd1bf3512001-04-07 15:24:33 +0000527 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +0000528 Sqlitetest2_Init(interp);
529 Sqlitetest3_Init(interp);
drhefc251d2001-07-01 22:12:01 +0000530 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +0000531 }
532#endif
drh348784e2000-05-29 20:41:49 +0000533 if( argc>=2 ){
534 int i;
535 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
536 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
537 for(i=2; i<argc; i++){
538 Tcl_SetVar(interp, "argv", argv[i],
539 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
540 }
541 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drhc61053b2000-06-04 12:58:36 +0000542 char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
543 if( zInfo==0 ) zInfo = interp->result;
544 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +0000545 return 1;
546 }
547 }else{
548 Tcl_GlobalEval(interp, zMainloop);
549 }
550 return 0;
551}
552#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +0000553
554#endif /* !defined(NO_TCL) */