blob: 6f186028fd6acb1386a48a4288cb25337b1c84b7 [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**
drh5d9d7572003-08-19 14:31:01 +000014** $Id: tclsqlite.c,v 1.50 2003/08/19 14:31:02 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/*
drh5d9d7572003-08-19 14:31:01 +0000252** This is a second alternative callback for database queries. A the
253** first column of the first row of the result is made the TCL result.
254*/
255static int DbEvalCallback3(
256 void *clientData, /* An instance of CallbackData */
257 int nCol, /* Number of columns in the result */
258 char ** azCol, /* Data for each column */
259 char ** azN /* Name for each column */
260){
261 Tcl_Interp *interp = (Tcl_Interp*)clientData;
262 Tcl_Obj *pElem;
263 if( azCol==0 ) return 1;
264 if( nCol==0 ) return 1;
265#ifdef UTF_TRANSLATION_NEEDED
266 {
267 Tcl_DString dCol;
268 Tcl_DStringInit(&dCol);
269 Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol);
270 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
271 Tcl_DStringFree(&dCol);
272 }
273#else
274 pElem = Tcl_NewStringObj(azCol[0], -1);
275#endif
276 Tcl_SetObjResult(interp, pElem);
277 return 1;
278}
279
280/*
drh75897232000-05-29 14:26:00 +0000281** Called when the command is deleted.
282*/
283static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000284 SqliteDb *pDb = (SqliteDb*)db;
285 sqlite_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000286 while( pDb->pFunc ){
287 SqlFunc *pFunc = pDb->pFunc;
288 pDb->pFunc = pFunc->pNext;
289 Tcl_Free((char*)pFunc);
290 }
drhbec3f402000-08-04 13:49:02 +0000291 if( pDb->zBusy ){
292 Tcl_Free(pDb->zBusy);
293 }
drhb5a20d32003-04-23 12:25:23 +0000294 if( pDb->zTrace ){
295 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000296 }
drhe22a3342003-04-22 20:30:37 +0000297 if( pDb->zAuth ){
298 Tcl_Free(pDb->zAuth);
299 }
drhbec3f402000-08-04 13:49:02 +0000300 Tcl_Free((char*)pDb);
301}
302
303/*
304** This routine is called when a database file is locked while trying
305** to execute SQL.
306*/
307static int DbBusyHandler(void *cd, const char *zTable, int nTries){
308 SqliteDb *pDb = (SqliteDb*)cd;
309 int rc;
310 char zVal[30];
311 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000312 Tcl_DString cmd;
313
314 Tcl_DStringInit(&cmd);
315 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
316 Tcl_DStringAppendElement(&cmd, zTable);
317 sprintf(zVal, " %d", nTries);
318 Tcl_DStringAppend(&cmd, zVal, -1);
319 zCmd = Tcl_DStringValue(&cmd);
320 rc = Tcl_Eval(pDb->interp, zCmd);
321 Tcl_DStringFree(&cmd);
322 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
323 return 0;
324 }
325 return 1;
drh75897232000-05-29 14:26:00 +0000326}
327
328/*
drhb5a20d32003-04-23 12:25:23 +0000329** This routine is called by the SQLite trace handler whenever a new
330** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000331*/
drhb5a20d32003-04-23 12:25:23 +0000332static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000333 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000334 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000335
drhb5a20d32003-04-23 12:25:23 +0000336 Tcl_DStringInit(&str);
337 Tcl_DStringAppend(&str, pDb->zTrace, -1);
338 Tcl_DStringAppendElement(&str, zSql);
339 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
340 Tcl_DStringFree(&str);
341 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000342}
343
344/*
drhcabb0812002-09-14 13:47:32 +0000345** This routine is called to evaluate an SQL function implemented
346** using TCL script.
347*/
348static void tclSqlFunc(sqlite_func *context, int argc, const char **argv){
349 SqlFunc *p = sqlite_user_data(context);
350 Tcl_DString cmd;
351 int i;
352 int rc;
353
354 Tcl_DStringInit(&cmd);
355 Tcl_DStringAppend(&cmd, p->zScript, -1);
356 for(i=0; i<argc; i++){
357 Tcl_DStringAppendElement(&cmd, argv[i] ? argv[i] : "");
358 }
359 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
360 if( rc ){
361 sqlite_set_result_error(context, Tcl_GetStringResult(p->interp), -1);
362 }else{
363 sqlite_set_result_string(context, Tcl_GetStringResult(p->interp), -1);
364 }
365}
drhe22a3342003-04-22 20:30:37 +0000366#ifndef SQLITE_OMIT_AUTHORIZATION
367/*
368** This is the authentication function. It appends the authentication
369** type code and the two arguments to zCmd[] then invokes the result
370** on the interpreter. The reply is examined to determine if the
371** authentication fails or succeeds.
372*/
373static int auth_callback(
374 void *pArg,
375 int code,
376 const char *zArg1,
377 const char *zArg2,
378 const char *zArg3,
379 const char *zArg4
380){
381 char *zCode;
382 Tcl_DString str;
383 int rc;
384 const char *zReply;
385 SqliteDb *pDb = (SqliteDb*)pArg;
386
387 switch( code ){
388 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
389 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
390 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
391 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
392 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
393 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
394 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
395 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
396 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
397 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
398 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
399 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
400 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
401 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
402 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
403 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
404 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
405 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
406 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
407 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
408 case SQLITE_READ : zCode="SQLITE_READ"; break;
409 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
410 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
411 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000412 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
413 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
drhe22a3342003-04-22 20:30:37 +0000414 default : zCode="????"; break;
415 }
416 Tcl_DStringInit(&str);
417 Tcl_DStringAppend(&str, pDb->zAuth, -1);
418 Tcl_DStringAppendElement(&str, zCode);
419 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
420 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
421 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
422 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
423 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
424 Tcl_DStringFree(&str);
425 zReply = Tcl_GetStringResult(pDb->interp);
426 if( strcmp(zReply,"SQLITE_OK")==0 ){
427 rc = SQLITE_OK;
428 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
429 rc = SQLITE_DENY;
430 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
431 rc = SQLITE_IGNORE;
432 }else{
433 rc = 999;
434 }
435 return rc;
436}
437#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000438
439/*
drh75897232000-05-29 14:26:00 +0000440** The "sqlite" command below creates a new Tcl command for each
441** connection it opens to an SQLite database. This routine is invoked
442** whenever one of those connection-specific commands is executed
443** in Tcl. For example, if you run Tcl code like this:
444**
445** sqlite db1 "my_database"
446** db1 close
447**
448** The first command opens a connection to the "my_database" database
449** and calls that connection "db1". The second command causes this
450** subroutine to be invoked.
451*/
drh6d313162000-09-21 13:01:35 +0000452static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000453 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000454 int choice;
drh0de8c112002-07-06 16:32:14 +0000455 static const char *DB_strs[] = {
drhb5a20d32003-04-23 12:25:23 +0000456 "authorizer", "busy", "changes",
457 "close", "complete", "errorcode",
458 "eval", "function", "last_insert_rowid",
drh5d9d7572003-08-19 14:31:01 +0000459 "onecolumn", "timeout", "trace",
460 0
drh6d313162000-09-21 13:01:35 +0000461 };
drh411995d2002-06-25 19:31:18 +0000462 enum DB_enum {
drhb5a20d32003-04-23 12:25:23 +0000463 DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
464 DB_CLOSE, DB_COMPLETE, DB_ERRORCODE,
465 DB_EVAL, DB_FUNCTION, DB_LAST_INSERT_ROWID,
drh5d9d7572003-08-19 14:31:01 +0000466 DB_ONECOLUMN, DB_TIMEOUT, DB_TRACE,
drh6d313162000-09-21 13:01:35 +0000467 };
468
469 if( objc<2 ){
470 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000471 return TCL_ERROR;
472 }
drh411995d2002-06-25 19:31:18 +0000473 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000474 return TCL_ERROR;
475 }
476
drh411995d2002-06-25 19:31:18 +0000477 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000478
drhe22a3342003-04-22 20:30:37 +0000479 /* $db authorizer ?CALLBACK?
480 **
481 ** Invoke the given callback to authorize each SQL operation as it is
482 ** compiled. 5 arguments are appended to the callback before it is
483 ** invoked:
484 **
485 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
486 ** (2) First descriptive name (depends on authorization type)
487 ** (3) Second descriptive name
488 ** (4) Name of the database (ex: "main", "temp")
489 ** (5) Name of trigger that is doing the access
490 **
491 ** The callback should return on of the following strings: SQLITE_OK,
492 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
493 **
494 ** If this method is invoked with no arguments, the current authorization
495 ** callback string is returned.
496 */
497 case DB_AUTHORIZER: {
498 if( objc>3 ){
499 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
500 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000501 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000502 Tcl_AppendResult(interp, pDb->zAuth, 0);
503 }
504 }else{
505 char *zAuth;
506 int len;
507 if( pDb->zAuth ){
508 Tcl_Free(pDb->zAuth);
509 }
510 zAuth = Tcl_GetStringFromObj(objv[2], &len);
511 if( zAuth && len>0 ){
512 pDb->zAuth = Tcl_Alloc( len + 1 );
513 strcpy(pDb->zAuth, zAuth);
514 }else{
515 pDb->zAuth = 0;
516 }
517#ifndef SQLITE_OMIT_AUTHORIZATION
518 if( pDb->zAuth ){
519 pDb->interp = interp;
520 sqlite_set_authorizer(pDb->db, auth_callback, pDb);
521 }else{
522 sqlite_set_authorizer(pDb->db, 0, 0);
523 }
524#endif
525 }
526 break;
527 }
528
drhbec3f402000-08-04 13:49:02 +0000529 /* $db busy ?CALLBACK?
530 **
531 ** Invoke the given callback if an SQL statement attempts to open
532 ** a locked database file.
533 */
drh6d313162000-09-21 13:01:35 +0000534 case DB_BUSY: {
535 if( objc>3 ){
536 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000537 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000538 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000539 if( pDb->zBusy ){
540 Tcl_AppendResult(interp, pDb->zBusy, 0);
541 }
542 }else{
drh6d313162000-09-21 13:01:35 +0000543 char *zBusy;
544 int len;
drhbec3f402000-08-04 13:49:02 +0000545 if( pDb->zBusy ){
546 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000547 }
drh6d313162000-09-21 13:01:35 +0000548 zBusy = Tcl_GetStringFromObj(objv[2], &len);
549 if( zBusy && len>0 ){
550 pDb->zBusy = Tcl_Alloc( len + 1 );
551 strcpy(pDb->zBusy, zBusy);
552 }else{
553 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000554 }
555 if( pDb->zBusy ){
556 pDb->interp = interp;
557 sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000558 }else{
559 sqlite_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000560 }
561 }
drh6d313162000-09-21 13:01:35 +0000562 break;
563 }
drhbec3f402000-08-04 13:49:02 +0000564
drhc8d30ac2002-04-12 10:08:59 +0000565 /*
566 ** $db changes
567 **
568 ** Return the number of rows that were modified, inserted, or deleted by
569 ** the most recent "eval".
570 */
571 case DB_CHANGES: {
572 Tcl_Obj *pResult;
573 int nChange;
574 if( objc!=2 ){
575 Tcl_WrongNumArgs(interp, 2, objv, "");
576 return TCL_ERROR;
577 }
578 nChange = sqlite_changes(pDb->db);
579 pResult = Tcl_GetObjResult(interp);
580 Tcl_SetIntObj(pResult, nChange);
581 break;
582 }
583
drh75897232000-05-29 14:26:00 +0000584 /* $db close
585 **
586 ** Shutdown the database
587 */
drh6d313162000-09-21 13:01:35 +0000588 case DB_CLOSE: {
589 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
590 break;
591 }
drh75897232000-05-29 14:26:00 +0000592
593 /* $db complete SQL
594 **
595 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
596 ** additional lines of input are needed. This is similar to the
597 ** built-in "info complete" command of Tcl.
598 */
drh6d313162000-09-21 13:01:35 +0000599 case DB_COMPLETE: {
600 Tcl_Obj *pResult;
601 int isComplete;
602 if( objc!=3 ){
603 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000604 return TCL_ERROR;
605 }
drh6d313162000-09-21 13:01:35 +0000606 isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
607 pResult = Tcl_GetObjResult(interp);
608 Tcl_SetBooleanObj(pResult, isComplete);
609 break;
610 }
drhdcd997e2003-01-31 17:21:49 +0000611
612 /*
613 ** $db errorcode
614 **
615 ** Return the numeric error code that was returned by the most recent
616 ** call to sqlite_exec().
617 */
618 case DB_ERRORCODE: {
619 Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc));
620 break;
621 }
drh75897232000-05-29 14:26:00 +0000622
623 /*
624 ** $db eval $sql ?array { ...code... }?
625 **
626 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000627 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000628 ** If "array" and "code" are omitted, then no callback is every invoked.
629 ** If "array" is an empty string, then the values are placed in variables
630 ** that have the same name as the fields extracted by the query.
631 */
drh6d313162000-09-21 13:01:35 +0000632 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000633 CallbackData cbData;
634 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000635 char *zSql;
drh75897232000-05-29 14:26:00 +0000636 int rc;
drh297ecf12001-04-05 15:57:13 +0000637#ifdef UTF_TRANSLATION_NEEDED
638 Tcl_DString dSql;
drh6d4abfb2001-10-22 02:58:08 +0000639 int i;
drh297ecf12001-04-05 15:57:13 +0000640#endif
drh75897232000-05-29 14:26:00 +0000641
drh6d313162000-09-21 13:01:35 +0000642 if( objc!=5 && objc!=3 ){
643 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000644 return TCL_ERROR;
645 }
drhbec3f402000-08-04 13:49:02 +0000646 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000647 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh297ecf12001-04-05 15:57:13 +0000648#ifdef UTF_TRANSLATION_NEEDED
649 Tcl_DStringInit(&dSql);
650 Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
651 zSql = Tcl_DStringValue(&dSql);
652#endif
drh6d313162000-09-21 13:01:35 +0000653 Tcl_IncrRefCount(objv[2]);
654 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000655 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000656 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000657 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
658 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000659 cbData.tcl_rc = TCL_OK;
drh6d4abfb2001-10-22 02:58:08 +0000660 cbData.nColName = 0;
661 cbData.azColName = 0;
drh75897232000-05-29 14:26:00 +0000662 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000663 Tcl_IncrRefCount(objv[3]);
664 Tcl_IncrRefCount(objv[4]);
665 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
666 Tcl_DecrRefCount(objv[4]);
667 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000668 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000669 }else{
drh6d313162000-09-21 13:01:35 +0000670 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000671 cbData.tcl_rc = TCL_OK;
drh6d313162000-09-21 13:01:35 +0000672 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
673 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000674 }
drhdcd997e2003-01-31 17:21:49 +0000675 pDb->rc = rc;
drhb798fa62002-09-03 19:43:23 +0000676 if( rc==SQLITE_ABORT ){
677 if( zErrMsg ) free(zErrMsg);
678 rc = cbData.tcl_rc;
679 }else if( zErrMsg ){
drh75897232000-05-29 14:26:00 +0000680 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
681 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000682 rc = TCL_ERROR;
drhb798fa62002-09-03 19:43:23 +0000683 }else if( rc!=SQLITE_OK ){
drh6d4abfb2001-10-22 02:58:08 +0000684 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
685 rc = TCL_ERROR;
drh960e8c62001-04-03 16:53:21 +0000686 }else{
drh75897232000-05-29 14:26:00 +0000687 }
drh6d313162000-09-21 13:01:35 +0000688 Tcl_DecrRefCount(objv[2]);
drh297ecf12001-04-05 15:57:13 +0000689#ifdef UTF_TRANSLATION_NEEDED
690 Tcl_DStringFree(&dSql);
drh6d4abfb2001-10-22 02:58:08 +0000691 if( objc==5 && cbData.azColName ){
692 for(i=0; i<cbData.nColName; i++){
693 if( cbData.azColName[i] ) free(cbData.azColName[i]);
694 }
695 free(cbData.azColName);
drhce927062001-11-09 13:41:09 +0000696 cbData.azColName = 0;
drh6d4abfb2001-10-22 02:58:08 +0000697 }
drh297ecf12001-04-05 15:57:13 +0000698#endif
drh75897232000-05-29 14:26:00 +0000699 return rc;
drh6d313162000-09-21 13:01:35 +0000700 }
drhbec3f402000-08-04 13:49:02 +0000701
702 /*
drhcabb0812002-09-14 13:47:32 +0000703 ** $db function NAME SCRIPT
704 **
705 ** Create a new SQL function called NAME. Whenever that function is
706 ** called, invoke SCRIPT to evaluate the function.
707 */
708 case DB_FUNCTION: {
709 SqlFunc *pFunc;
710 char *zName;
711 char *zScript;
712 int nScript;
713 if( objc!=4 ){
714 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
715 return TCL_ERROR;
716 }
717 zName = Tcl_GetStringFromObj(objv[2], 0);
718 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
719 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
720 if( pFunc==0 ) return TCL_ERROR;
721 pFunc->interp = interp;
722 pFunc->pNext = pDb->pFunc;
723 pFunc->zScript = (char*)&pFunc[1];
724 strcpy(pFunc->zScript, zScript);
725 sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc);
726 sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC);
727 break;
728 }
729
730 /*
drhaf9ff332002-01-16 21:00:27 +0000731 ** $db last_insert_rowid
732 **
733 ** Return an integer which is the ROWID for the most recent insert.
734 */
735 case DB_LAST_INSERT_ROWID: {
736 Tcl_Obj *pResult;
737 int rowid;
738 if( objc!=2 ){
739 Tcl_WrongNumArgs(interp, 2, objv, "");
740 return TCL_ERROR;
741 }
742 rowid = sqlite_last_insert_rowid(pDb->db);
743 pResult = Tcl_GetObjResult(interp);
744 Tcl_SetIntObj(pResult, rowid);
745 break;
746 }
747
748 /*
drh5d9d7572003-08-19 14:31:01 +0000749 ** $db onecolumn SQL
750 **
751 ** Return a single column from a single row of the given SQL query.
752 */
753 case DB_ONECOLUMN: {
754 int rc;
755 char *zSql;
756 char *zErrMsg = 0;
757 if( objc!=3 ){
758 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
759 return TCL_ERROR;
760 }
761 zSql = Tcl_GetStringFromObj(objv[2], 0);
762 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
763 if( rc==SQLITE_ABORT ){
764 /* Do nothing. This is normal. */
765 }else if( zErrMsg ){
766 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
767 free(zErrMsg);
768 rc = TCL_ERROR;
769 }else if( rc!=SQLITE_OK ){
770 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
771 rc = TCL_ERROR;
772 }
773 break;
774 }
775
776 /*
drhbec3f402000-08-04 13:49:02 +0000777 ** $db timeout MILLESECONDS
778 **
779 ** Delay for the number of milliseconds specified when a file is locked.
780 */
drh6d313162000-09-21 13:01:35 +0000781 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000782 int ms;
drh6d313162000-09-21 13:01:35 +0000783 if( objc!=3 ){
784 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000785 return TCL_ERROR;
786 }
drh6d313162000-09-21 13:01:35 +0000787 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
drhbec3f402000-08-04 13:49:02 +0000788 sqlite_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000789 break;
drh75897232000-05-29 14:26:00 +0000790 }
drhb5a20d32003-04-23 12:25:23 +0000791
792 /* $db trace ?CALLBACK?
793 **
794 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
795 ** that is executed. The text of the SQL is appended to CALLBACK before
796 ** it is executed.
797 */
798 case DB_TRACE: {
799 if( objc>3 ){
800 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
801 }else if( objc==2 ){
802 if( pDb->zTrace ){
803 Tcl_AppendResult(interp, pDb->zTrace, 0);
804 }
805 }else{
806 char *zTrace;
807 int len;
808 if( pDb->zTrace ){
809 Tcl_Free(pDb->zTrace);
810 }
811 zTrace = Tcl_GetStringFromObj(objv[2], &len);
812 if( zTrace && len>0 ){
813 pDb->zTrace = Tcl_Alloc( len + 1 );
814 strcpy(pDb->zTrace, zTrace);
815 }else{
816 pDb->zTrace = 0;
817 }
818 if( pDb->zTrace ){
819 pDb->interp = interp;
820 sqlite_trace(pDb->db, DbTraceHandler, pDb);
821 }else{
822 sqlite_trace(pDb->db, 0, 0);
823 }
824 }
825 break;
826 }
827
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) */