blob: c2f4cfd49d60e8dd1e352b38ad8d7999a66a7300 [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**
drhf93e4142003-12-19 12:32:45 +000014** $Id: tclsqlite.c,v 1.53 2003/12/19 12:32:46 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 */
danielk1977348bb5d2003-10-18 09:37:26 +000055 char *zProgress; /* The progress 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/*
drh5d9d7572003-08-19 14:31:01 +0000253** This is a second alternative callback for database queries. A the
254** first column of the first row of the result is made the TCL result.
255*/
256static int DbEvalCallback3(
257 void *clientData, /* An instance of CallbackData */
258 int nCol, /* Number of columns in the result */
259 char ** azCol, /* Data for each column */
260 char ** azN /* Name for each column */
261){
262 Tcl_Interp *interp = (Tcl_Interp*)clientData;
263 Tcl_Obj *pElem;
264 if( azCol==0 ) return 1;
265 if( nCol==0 ) return 1;
266#ifdef UTF_TRANSLATION_NEEDED
267 {
268 Tcl_DString dCol;
269 Tcl_DStringInit(&dCol);
270 Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol);
271 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
272 Tcl_DStringFree(&dCol);
273 }
274#else
275 pElem = Tcl_NewStringObj(azCol[0], -1);
276#endif
277 Tcl_SetObjResult(interp, pElem);
278 return 1;
279}
280
281/*
drh75897232000-05-29 14:26:00 +0000282** Called when the command is deleted.
283*/
284static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000285 SqliteDb *pDb = (SqliteDb*)db;
286 sqlite_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000287 while( pDb->pFunc ){
288 SqlFunc *pFunc = pDb->pFunc;
289 pDb->pFunc = pFunc->pNext;
290 Tcl_Free((char*)pFunc);
291 }
drhbec3f402000-08-04 13:49:02 +0000292 if( pDb->zBusy ){
293 Tcl_Free(pDb->zBusy);
294 }
drhb5a20d32003-04-23 12:25:23 +0000295 if( pDb->zTrace ){
296 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000297 }
drhe22a3342003-04-22 20:30:37 +0000298 if( pDb->zAuth ){
299 Tcl_Free(pDb->zAuth);
300 }
drhbec3f402000-08-04 13:49:02 +0000301 Tcl_Free((char*)pDb);
302}
303
304/*
305** This routine is called when a database file is locked while trying
306** to execute SQL.
307*/
308static int DbBusyHandler(void *cd, const char *zTable, int nTries){
309 SqliteDb *pDb = (SqliteDb*)cd;
310 int rc;
311 char zVal[30];
312 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000313 Tcl_DString cmd;
314
315 Tcl_DStringInit(&cmd);
316 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
317 Tcl_DStringAppendElement(&cmd, zTable);
318 sprintf(zVal, " %d", nTries);
319 Tcl_DStringAppend(&cmd, zVal, -1);
320 zCmd = Tcl_DStringValue(&cmd);
321 rc = Tcl_Eval(pDb->interp, zCmd);
322 Tcl_DStringFree(&cmd);
323 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
324 return 0;
325 }
326 return 1;
drh75897232000-05-29 14:26:00 +0000327}
328
329/*
danielk1977348bb5d2003-10-18 09:37:26 +0000330** This routine is invoked as the 'progress callback' for the database.
331*/
332static int DbProgressHandler(void *cd){
333 SqliteDb *pDb = (SqliteDb*)cd;
334 int rc;
335
336 assert( pDb->zProgress );
337 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
338 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
339 return 1;
340 }
341 return 0;
342}
343
344/*
drhb5a20d32003-04-23 12:25:23 +0000345** This routine is called by the SQLite trace handler whenever a new
346** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000347*/
drhb5a20d32003-04-23 12:25:23 +0000348static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000349 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000350 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000351
drhb5a20d32003-04-23 12:25:23 +0000352 Tcl_DStringInit(&str);
353 Tcl_DStringAppend(&str, pDb->zTrace, -1);
354 Tcl_DStringAppendElement(&str, zSql);
355 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
356 Tcl_DStringFree(&str);
357 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000358}
359
360/*
drhcabb0812002-09-14 13:47:32 +0000361** This routine is called to evaluate an SQL function implemented
362** using TCL script.
363*/
364static void tclSqlFunc(sqlite_func *context, int argc, const char **argv){
365 SqlFunc *p = sqlite_user_data(context);
366 Tcl_DString cmd;
367 int i;
368 int rc;
369
370 Tcl_DStringInit(&cmd);
371 Tcl_DStringAppend(&cmd, p->zScript, -1);
372 for(i=0; i<argc; i++){
373 Tcl_DStringAppendElement(&cmd, argv[i] ? argv[i] : "");
374 }
375 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
376 if( rc ){
377 sqlite_set_result_error(context, Tcl_GetStringResult(p->interp), -1);
378 }else{
379 sqlite_set_result_string(context, Tcl_GetStringResult(p->interp), -1);
380 }
381}
drhe22a3342003-04-22 20:30:37 +0000382#ifndef SQLITE_OMIT_AUTHORIZATION
383/*
384** This is the authentication function. It appends the authentication
385** type code and the two arguments to zCmd[] then invokes the result
386** on the interpreter. The reply is examined to determine if the
387** authentication fails or succeeds.
388*/
389static int auth_callback(
390 void *pArg,
391 int code,
392 const char *zArg1,
393 const char *zArg2,
394 const char *zArg3,
395 const char *zArg4
396){
397 char *zCode;
398 Tcl_DString str;
399 int rc;
400 const char *zReply;
401 SqliteDb *pDb = (SqliteDb*)pArg;
402
403 switch( code ){
404 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
405 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
406 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
407 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
408 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
409 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
410 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
411 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
412 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
413 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
414 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
415 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
416 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
417 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
418 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
419 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
420 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
421 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
422 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
423 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
424 case SQLITE_READ : zCode="SQLITE_READ"; break;
425 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
426 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
427 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000428 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
429 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
drhe22a3342003-04-22 20:30:37 +0000430 default : zCode="????"; break;
431 }
432 Tcl_DStringInit(&str);
433 Tcl_DStringAppend(&str, pDb->zAuth, -1);
434 Tcl_DStringAppendElement(&str, zCode);
435 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
436 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
437 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
438 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
439 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
440 Tcl_DStringFree(&str);
441 zReply = Tcl_GetStringResult(pDb->interp);
442 if( strcmp(zReply,"SQLITE_OK")==0 ){
443 rc = SQLITE_OK;
444 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
445 rc = SQLITE_DENY;
446 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
447 rc = SQLITE_IGNORE;
448 }else{
449 rc = 999;
450 }
451 return rc;
452}
453#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000454
455/*
drh75897232000-05-29 14:26:00 +0000456** The "sqlite" command below creates a new Tcl command for each
457** connection it opens to an SQLite database. This routine is invoked
458** whenever one of those connection-specific commands is executed
459** in Tcl. For example, if you run Tcl code like this:
460**
461** sqlite db1 "my_database"
462** db1 close
463**
464** The first command opens a connection to the "my_database" database
465** and calls that connection "db1". The second command causes this
466** subroutine to be invoked.
467*/
drh6d313162000-09-21 13:01:35 +0000468static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000469 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000470 int choice;
drh0de8c112002-07-06 16:32:14 +0000471 static const char *DB_strs[] = {
drhb5a20d32003-04-23 12:25:23 +0000472 "authorizer", "busy", "changes",
473 "close", "complete", "errorcode",
474 "eval", "function", "last_insert_rowid",
drh5d9d7572003-08-19 14:31:01 +0000475 "onecolumn", "timeout", "trace",
danielk1977348bb5d2003-10-18 09:37:26 +0000476 "progress", 0
drh6d313162000-09-21 13:01:35 +0000477 };
drh411995d2002-06-25 19:31:18 +0000478 enum DB_enum {
drhb5a20d32003-04-23 12:25:23 +0000479 DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
480 DB_CLOSE, DB_COMPLETE, DB_ERRORCODE,
481 DB_EVAL, DB_FUNCTION, DB_LAST_INSERT_ROWID,
drh5d9d7572003-08-19 14:31:01 +0000482 DB_ONECOLUMN, DB_TIMEOUT, DB_TRACE,
drhf93e4142003-12-19 12:32:45 +0000483 DB_PROGRESS
drh6d313162000-09-21 13:01:35 +0000484 };
485
486 if( objc<2 ){
487 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000488 return TCL_ERROR;
489 }
drh411995d2002-06-25 19:31:18 +0000490 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000491 return TCL_ERROR;
492 }
493
drh411995d2002-06-25 19:31:18 +0000494 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000495
drhe22a3342003-04-22 20:30:37 +0000496 /* $db authorizer ?CALLBACK?
497 **
498 ** Invoke the given callback to authorize each SQL operation as it is
499 ** compiled. 5 arguments are appended to the callback before it is
500 ** invoked:
501 **
502 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
503 ** (2) First descriptive name (depends on authorization type)
504 ** (3) Second descriptive name
505 ** (4) Name of the database (ex: "main", "temp")
506 ** (5) Name of trigger that is doing the access
507 **
508 ** The callback should return on of the following strings: SQLITE_OK,
509 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
510 **
511 ** If this method is invoked with no arguments, the current authorization
512 ** callback string is returned.
513 */
514 case DB_AUTHORIZER: {
515 if( objc>3 ){
516 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
517 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000518 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000519 Tcl_AppendResult(interp, pDb->zAuth, 0);
520 }
521 }else{
522 char *zAuth;
523 int len;
524 if( pDb->zAuth ){
525 Tcl_Free(pDb->zAuth);
526 }
527 zAuth = Tcl_GetStringFromObj(objv[2], &len);
528 if( zAuth && len>0 ){
529 pDb->zAuth = Tcl_Alloc( len + 1 );
530 strcpy(pDb->zAuth, zAuth);
531 }else{
532 pDb->zAuth = 0;
533 }
534#ifndef SQLITE_OMIT_AUTHORIZATION
535 if( pDb->zAuth ){
536 pDb->interp = interp;
537 sqlite_set_authorizer(pDb->db, auth_callback, pDb);
538 }else{
539 sqlite_set_authorizer(pDb->db, 0, 0);
540 }
541#endif
542 }
543 break;
544 }
545
drhbec3f402000-08-04 13:49:02 +0000546 /* $db busy ?CALLBACK?
547 **
548 ** Invoke the given callback if an SQL statement attempts to open
549 ** a locked database file.
550 */
drh6d313162000-09-21 13:01:35 +0000551 case DB_BUSY: {
552 if( objc>3 ){
553 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000554 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000555 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000556 if( pDb->zBusy ){
557 Tcl_AppendResult(interp, pDb->zBusy, 0);
558 }
559 }else{
drh6d313162000-09-21 13:01:35 +0000560 char *zBusy;
561 int len;
drhbec3f402000-08-04 13:49:02 +0000562 if( pDb->zBusy ){
563 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000564 }
drh6d313162000-09-21 13:01:35 +0000565 zBusy = Tcl_GetStringFromObj(objv[2], &len);
566 if( zBusy && len>0 ){
567 pDb->zBusy = Tcl_Alloc( len + 1 );
568 strcpy(pDb->zBusy, zBusy);
569 }else{
570 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000571 }
572 if( pDb->zBusy ){
573 pDb->interp = interp;
574 sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000575 }else{
576 sqlite_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000577 }
578 }
drh6d313162000-09-21 13:01:35 +0000579 break;
580 }
drhbec3f402000-08-04 13:49:02 +0000581
danielk1977348bb5d2003-10-18 09:37:26 +0000582 /* $db progress ?N CALLBACK?
583 **
584 ** Invoke the given callback every N virtual machine opcodes while executing
585 ** queries.
586 */
587 case DB_PROGRESS: {
588 if( objc==2 ){
589 if( pDb->zProgress ){
590 Tcl_AppendResult(interp, pDb->zProgress, 0);
591 }
592 }else if( objc==4 ){
593 char *zProgress;
594 int len;
595 int N;
596 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
597 return TCL_ERROR;
598 };
599 if( pDb->zProgress ){
600 Tcl_Free(pDb->zProgress);
601 }
602 zProgress = Tcl_GetStringFromObj(objv[3], &len);
603 if( zProgress && len>0 ){
604 pDb->zProgress = Tcl_Alloc( len + 1 );
605 strcpy(pDb->zProgress, zProgress);
606 }else{
607 pDb->zProgress = 0;
608 }
609#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
610 if( pDb->zProgress ){
611 pDb->interp = interp;
612 sqlite_progress_handler(pDb->db, N, DbProgressHandler, pDb);
613 }else{
614 sqlite_progress_handler(pDb->db, 0, 0, 0);
615 }
616#endif
617 }else{
618 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
619 return TCL_ERROR;
620 }
621 break;
622 }
623
drhc8d30ac2002-04-12 10:08:59 +0000624 /*
625 ** $db changes
626 **
627 ** Return the number of rows that were modified, inserted, or deleted by
628 ** the most recent "eval".
629 */
630 case DB_CHANGES: {
631 Tcl_Obj *pResult;
632 int nChange;
633 if( objc!=2 ){
634 Tcl_WrongNumArgs(interp, 2, objv, "");
635 return TCL_ERROR;
636 }
637 nChange = sqlite_changes(pDb->db);
638 pResult = Tcl_GetObjResult(interp);
639 Tcl_SetIntObj(pResult, nChange);
640 break;
641 }
642
drh75897232000-05-29 14:26:00 +0000643 /* $db close
644 **
645 ** Shutdown the database
646 */
drh6d313162000-09-21 13:01:35 +0000647 case DB_CLOSE: {
648 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
649 break;
650 }
drh75897232000-05-29 14:26:00 +0000651
652 /* $db complete SQL
653 **
654 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
655 ** additional lines of input are needed. This is similar to the
656 ** built-in "info complete" command of Tcl.
657 */
drh6d313162000-09-21 13:01:35 +0000658 case DB_COMPLETE: {
659 Tcl_Obj *pResult;
660 int isComplete;
661 if( objc!=3 ){
662 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000663 return TCL_ERROR;
664 }
drh6d313162000-09-21 13:01:35 +0000665 isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
666 pResult = Tcl_GetObjResult(interp);
667 Tcl_SetBooleanObj(pResult, isComplete);
668 break;
669 }
drhdcd997e2003-01-31 17:21:49 +0000670
671 /*
672 ** $db errorcode
673 **
674 ** Return the numeric error code that was returned by the most recent
675 ** call to sqlite_exec().
676 */
677 case DB_ERRORCODE: {
678 Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc));
679 break;
680 }
drh75897232000-05-29 14:26:00 +0000681
682 /*
683 ** $db eval $sql ?array { ...code... }?
684 **
685 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000686 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000687 ** If "array" and "code" are omitted, then no callback is every invoked.
688 ** If "array" is an empty string, then the values are placed in variables
689 ** that have the same name as the fields extracted by the query.
690 */
drh6d313162000-09-21 13:01:35 +0000691 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000692 CallbackData cbData;
693 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000694 char *zSql;
drh75897232000-05-29 14:26:00 +0000695 int rc;
drh297ecf12001-04-05 15:57:13 +0000696#ifdef UTF_TRANSLATION_NEEDED
697 Tcl_DString dSql;
drh6d4abfb2001-10-22 02:58:08 +0000698 int i;
drh297ecf12001-04-05 15:57:13 +0000699#endif
drh75897232000-05-29 14:26:00 +0000700
drh6d313162000-09-21 13:01:35 +0000701 if( objc!=5 && objc!=3 ){
702 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000703 return TCL_ERROR;
704 }
drhbec3f402000-08-04 13:49:02 +0000705 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000706 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh297ecf12001-04-05 15:57:13 +0000707#ifdef UTF_TRANSLATION_NEEDED
708 Tcl_DStringInit(&dSql);
709 Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
710 zSql = Tcl_DStringValue(&dSql);
711#endif
drh6d313162000-09-21 13:01:35 +0000712 Tcl_IncrRefCount(objv[2]);
713 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000714 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000715 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000716 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
717 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000718 cbData.tcl_rc = TCL_OK;
drh6d4abfb2001-10-22 02:58:08 +0000719 cbData.nColName = 0;
720 cbData.azColName = 0;
drh75897232000-05-29 14:26:00 +0000721 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000722 Tcl_IncrRefCount(objv[3]);
723 Tcl_IncrRefCount(objv[4]);
724 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
725 Tcl_DecrRefCount(objv[4]);
726 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000727 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000728 }else{
drh6d313162000-09-21 13:01:35 +0000729 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000730 cbData.tcl_rc = TCL_OK;
drh6d313162000-09-21 13:01:35 +0000731 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
732 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000733 }
drhdcd997e2003-01-31 17:21:49 +0000734 pDb->rc = rc;
drhb798fa62002-09-03 19:43:23 +0000735 if( rc==SQLITE_ABORT ){
736 if( zErrMsg ) free(zErrMsg);
737 rc = cbData.tcl_rc;
738 }else if( zErrMsg ){
drh75897232000-05-29 14:26:00 +0000739 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
740 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000741 rc = TCL_ERROR;
drhb798fa62002-09-03 19:43:23 +0000742 }else if( rc!=SQLITE_OK ){
drh6d4abfb2001-10-22 02:58:08 +0000743 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
744 rc = TCL_ERROR;
drh960e8c62001-04-03 16:53:21 +0000745 }else{
drh75897232000-05-29 14:26:00 +0000746 }
drh6d313162000-09-21 13:01:35 +0000747 Tcl_DecrRefCount(objv[2]);
drh297ecf12001-04-05 15:57:13 +0000748#ifdef UTF_TRANSLATION_NEEDED
749 Tcl_DStringFree(&dSql);
drh6d4abfb2001-10-22 02:58:08 +0000750 if( objc==5 && cbData.azColName ){
751 for(i=0; i<cbData.nColName; i++){
752 if( cbData.azColName[i] ) free(cbData.azColName[i]);
753 }
754 free(cbData.azColName);
drhce927062001-11-09 13:41:09 +0000755 cbData.azColName = 0;
drh6d4abfb2001-10-22 02:58:08 +0000756 }
drh297ecf12001-04-05 15:57:13 +0000757#endif
drh75897232000-05-29 14:26:00 +0000758 return rc;
drh6d313162000-09-21 13:01:35 +0000759 }
drhbec3f402000-08-04 13:49:02 +0000760
761 /*
drhcabb0812002-09-14 13:47:32 +0000762 ** $db function NAME SCRIPT
763 **
764 ** Create a new SQL function called NAME. Whenever that function is
765 ** called, invoke SCRIPT to evaluate the function.
766 */
767 case DB_FUNCTION: {
768 SqlFunc *pFunc;
769 char *zName;
770 char *zScript;
771 int nScript;
772 if( objc!=4 ){
773 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
774 return TCL_ERROR;
775 }
776 zName = Tcl_GetStringFromObj(objv[2], 0);
777 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
778 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
779 if( pFunc==0 ) return TCL_ERROR;
780 pFunc->interp = interp;
781 pFunc->pNext = pDb->pFunc;
782 pFunc->zScript = (char*)&pFunc[1];
783 strcpy(pFunc->zScript, zScript);
784 sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc);
785 sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC);
786 break;
787 }
788
789 /*
drhaf9ff332002-01-16 21:00:27 +0000790 ** $db last_insert_rowid
791 **
792 ** Return an integer which is the ROWID for the most recent insert.
793 */
794 case DB_LAST_INSERT_ROWID: {
795 Tcl_Obj *pResult;
796 int rowid;
797 if( objc!=2 ){
798 Tcl_WrongNumArgs(interp, 2, objv, "");
799 return TCL_ERROR;
800 }
801 rowid = sqlite_last_insert_rowid(pDb->db);
802 pResult = Tcl_GetObjResult(interp);
803 Tcl_SetIntObj(pResult, rowid);
804 break;
805 }
806
807 /*
drh5d9d7572003-08-19 14:31:01 +0000808 ** $db onecolumn SQL
809 **
810 ** Return a single column from a single row of the given SQL query.
811 */
812 case DB_ONECOLUMN: {
813 int rc;
814 char *zSql;
815 char *zErrMsg = 0;
816 if( objc!=3 ){
817 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
818 return TCL_ERROR;
819 }
820 zSql = Tcl_GetStringFromObj(objv[2], 0);
821 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
822 if( rc==SQLITE_ABORT ){
823 /* Do nothing. This is normal. */
824 }else if( zErrMsg ){
825 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
826 free(zErrMsg);
827 rc = TCL_ERROR;
828 }else if( rc!=SQLITE_OK ){
829 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
830 rc = TCL_ERROR;
831 }
832 break;
833 }
834
835 /*
drhbec3f402000-08-04 13:49:02 +0000836 ** $db timeout MILLESECONDS
837 **
838 ** Delay for the number of milliseconds specified when a file is locked.
839 */
drh6d313162000-09-21 13:01:35 +0000840 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000841 int ms;
drh6d313162000-09-21 13:01:35 +0000842 if( objc!=3 ){
843 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000844 return TCL_ERROR;
845 }
drh6d313162000-09-21 13:01:35 +0000846 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
drhbec3f402000-08-04 13:49:02 +0000847 sqlite_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000848 break;
drh75897232000-05-29 14:26:00 +0000849 }
drhb5a20d32003-04-23 12:25:23 +0000850
851 /* $db trace ?CALLBACK?
852 **
853 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
854 ** that is executed. The text of the SQL is appended to CALLBACK before
855 ** it is executed.
856 */
857 case DB_TRACE: {
858 if( objc>3 ){
859 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
860 }else if( objc==2 ){
861 if( pDb->zTrace ){
862 Tcl_AppendResult(interp, pDb->zTrace, 0);
863 }
864 }else{
865 char *zTrace;
866 int len;
867 if( pDb->zTrace ){
868 Tcl_Free(pDb->zTrace);
869 }
870 zTrace = Tcl_GetStringFromObj(objv[2], &len);
871 if( zTrace && len>0 ){
872 pDb->zTrace = Tcl_Alloc( len + 1 );
873 strcpy(pDb->zTrace, zTrace);
874 }else{
875 pDb->zTrace = 0;
876 }
877 if( pDb->zTrace ){
878 pDb->interp = interp;
879 sqlite_trace(pDb->db, DbTraceHandler, pDb);
880 }else{
881 sqlite_trace(pDb->db, 0, 0);
882 }
883 }
884 break;
885 }
886
drh6d313162000-09-21 13:01:35 +0000887 } /* End of the SWITCH statement */
drh75897232000-05-29 14:26:00 +0000888 return TCL_OK;
889}
890
891/*
892** sqlite DBNAME FILENAME ?MODE?
893**
894** This is the main Tcl command. When the "sqlite" Tcl command is
895** invoked, this routine runs to process that command.
896**
897** The first argument, DBNAME, is an arbitrary name for a new
898** database connection. This command creates a new command named
899** DBNAME that is used to control that connection. The database
900** connection is deleted when the DBNAME command is deleted.
901**
902** The second argument is the name of the directory that contains
903** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000904**
905** For testing purposes, we also support the following:
906**
907** sqlite -encoding
908**
909** Return the encoding used by LIKE and GLOB operators. Choices
910** are UTF-8 and iso8859.
911**
drh647cb0e2002-11-04 19:32:25 +0000912** sqlite -version
913**
914** Return the version number of the SQLite library.
915**
drhfbc3eab2001-04-06 16:13:42 +0000916** sqlite -tcl-uses-utf
917**
918** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
919** not. Used by tests to make sure the library was compiled
920** correctly.
drh75897232000-05-29 14:26:00 +0000921*/
922static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){
923 int mode;
drhbec3f402000-08-04 13:49:02 +0000924 SqliteDb *p;
drh75897232000-05-29 14:26:00 +0000925 char *zErrMsg;
drh06b27182002-06-26 20:06:05 +0000926 char zBuf[80];
drhfbc3eab2001-04-06 16:13:42 +0000927 if( argc==2 ){
928 if( strcmp(argv[1],"-encoding")==0 ){
929 Tcl_AppendResult(interp,sqlite_encoding,0);
930 return TCL_OK;
931 }
drh647cb0e2002-11-04 19:32:25 +0000932 if( strcmp(argv[1],"-version")==0 ){
933 Tcl_AppendResult(interp,sqlite_version,0);
934 return TCL_OK;
935 }
drhfbc3eab2001-04-06 16:13:42 +0000936 if( strcmp(argv[1],"-tcl-uses-utf")==0 ){
937#ifdef TCL_UTF_MAX
938 Tcl_AppendResult(interp,"1",0);
939#else
940 Tcl_AppendResult(interp,"0",0);
941#endif
942 return TCL_OK;
943 }
944 }
drh75897232000-05-29 14:26:00 +0000945 if( argc!=3 && argc!=4 ){
946 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
947 " HANDLE FILENAME ?MODE?\"", 0);
948 return TCL_ERROR;
949 }
950 if( argc==3 ){
drh58b95762000-06-02 01:17:37 +0000951 mode = 0666;
drh75897232000-05-29 14:26:00 +0000952 }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){
953 return TCL_ERROR;
954 }
955 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +0000956 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +0000957 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +0000958 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
959 return TCL_ERROR;
960 }
961 memset(p, 0, sizeof(*p));
962 p->db = sqlite_open(argv[2], mode, &zErrMsg);
963 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +0000964 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +0000965 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +0000966 free(zErrMsg);
967 return TCL_ERROR;
968 }
drh6d313162000-09-21 13:01:35 +0000969 Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +0000970
drh06b27182002-06-26 20:06:05 +0000971 /* The return value is the value of the sqlite* pointer
972 */
973 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +0000974 if( strncmp(zBuf,"0x",2) ){
975 sprintf(zBuf, "0x%p", p->db);
976 }
drh06b27182002-06-26 20:06:05 +0000977 Tcl_AppendResult(interp, zBuf, 0);
978
drhc22bd472002-05-10 13:14:07 +0000979 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +0000980 ** SQL function.
drhc22bd472002-05-10 13:14:07 +0000981 */
drh28b4e482002-03-11 02:06:13 +0000982#ifdef SQLITE_TEST
983 {
drhc22bd472002-05-10 13:14:07 +0000984 extern void Md5_Register(sqlite*);
985 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +0000986 }
drh28b4e482002-03-11 02:06:13 +0000987#endif
drh75897232000-05-29 14:26:00 +0000988 return TCL_OK;
989}
990
991/*
drh90ca9752001-09-28 17:47:14 +0000992** Provide a dummy Tcl_InitStubs if we are using this as a static
993** library.
994*/
995#ifndef USE_TCL_STUBS
996# undef Tcl_InitStubs
997# define Tcl_InitStubs(a,b,c)
998#endif
999
1000/*
drh75897232000-05-29 14:26:00 +00001001** Initialize this module.
1002**
1003** This Tcl module contains only a single new Tcl command named "sqlite".
1004** (Hence there is no namespace. There is no point in using a namespace
1005** if the extension only supplies one new name!) The "sqlite" command is
1006** used to open a new SQLite database. See the DbMain() routine above
1007** for additional information.
1008*/
1009int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001010 Tcl_InitStubs(interp, "8.0", 0);
drhc2eef3b2002-08-31 18:53:06 +00001011 Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001012 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh90ca9752001-09-28 17:47:14 +00001013 return TCL_OK;
1014}
1015int Tclsqlite_Init(Tcl_Interp *interp){
1016 Tcl_InitStubs(interp, "8.0", 0);
drhc2eef3b2002-08-31 18:53:06 +00001017 Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001018 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh75897232000-05-29 14:26:00 +00001019 return TCL_OK;
1020}
1021int Sqlite_SafeInit(Tcl_Interp *interp){
1022 return TCL_OK;
1023}
drh90ca9752001-09-28 17:47:14 +00001024int Tclsqlite_SafeInit(Tcl_Interp *interp){
1025 return TCL_OK;
1026}
drh75897232000-05-29 14:26:00 +00001027
drh3cebbde2000-10-19 14:59:27 +00001028#if 0
drh75897232000-05-29 14:26:00 +00001029/*
1030** If compiled using mktclapp, this routine runs to initialize
1031** everything.
1032*/
1033int Et_AppInit(Tcl_Interp *interp){
1034 return Sqlite_Init(interp);
1035}
drh3cebbde2000-10-19 14:59:27 +00001036#endif
drh348784e2000-05-29 20:41:49 +00001037
1038/*
1039** If the macro TCLSH is defined and is one, then put in code for the
1040** "main" routine that will initialize Tcl.
1041*/
1042#if defined(TCLSH) && TCLSH==1
1043static char zMainloop[] =
1044 "set line {}\n"
1045 "while {![eof stdin]} {\n"
1046 "if {$line!=\"\"} {\n"
1047 "puts -nonewline \"> \"\n"
1048 "} else {\n"
1049 "puts -nonewline \"% \"\n"
1050 "}\n"
1051 "flush stdout\n"
1052 "append line [gets stdin]\n"
1053 "if {[info complete $line]} {\n"
1054 "if {[catch {uplevel #0 $line} result]} {\n"
1055 "puts stderr \"Error: $result\"\n"
1056 "} elseif {$result!=\"\"} {\n"
1057 "puts $result\n"
1058 "}\n"
1059 "set line {}\n"
1060 "} else {\n"
1061 "append line \\n\n"
1062 "}\n"
1063 "}\n"
1064;
1065
1066#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1067int TCLSH_MAIN(int argc, char **argv){
1068 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001069 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001070 interp = Tcl_CreateInterp();
1071 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001072#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001073 {
1074 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001075 extern int Sqlitetest2_Init(Tcl_Interp*);
1076 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001077 extern int Sqlitetest4_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001078 extern int Md5_Init(Tcl_Interp*);
drhd1bf3512001-04-07 15:24:33 +00001079 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001080 Sqlitetest2_Init(interp);
1081 Sqlitetest3_Init(interp);
drha6064dc2003-12-19 02:52:05 +00001082 Sqlitetest4_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001083 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001084 }
1085#endif
drh348784e2000-05-29 20:41:49 +00001086 if( argc>=2 ){
1087 int i;
1088 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1089 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1090 for(i=2; i<argc; i++){
1091 Tcl_SetVar(interp, "argv", argv[i],
1092 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1093 }
1094 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001095 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001096 if( zInfo==0 ) zInfo = interp->result;
1097 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001098 return 1;
1099 }
1100 }else{
1101 Tcl_GlobalEval(interp, zMainloop);
1102 }
1103 return 0;
1104}
1105#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001106
1107#endif /* !defined(NO_TCL) */