blob: c828f19a0888a18a2df74df4ce59d62b7330aafe [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**
drhde647132004-05-07 17:57:49 +000014** $Id: tclsqlite.c,v 1.62 2004/05/07 17:57:50 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
drh3aac2dd2004-04-26 14:10:20 +000024#if 0
drh75897232000-05-29 14:26:00 +000025/*
drh98808ba2001-10-18 12:34:46 +000026** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
27** have to do a translation when going between the two. Set the
28** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
29** this translation.
30*/
31#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
32# define UTF_TRANSLATION_NEEDED 1
33#endif
34
35/*
drhcabb0812002-09-14 13:47:32 +000036** New SQL functions can be created as TCL scripts. Each such function
37** is described by an instance of the following structure.
38*/
39typedef struct SqlFunc SqlFunc;
40struct SqlFunc {
41 Tcl_Interp *interp; /* The TCL interpret to execute the function */
42 char *zScript; /* The script to be run */
43 SqlFunc *pNext; /* Next function on the list of them all */
44};
45
46/*
drhbec3f402000-08-04 13:49:02 +000047** There is one instance of this structure for each SQLite database
48** that has been opened by the SQLite TCL interface.
49*/
50typedef struct SqliteDb SqliteDb;
51struct SqliteDb {
52 sqlite *db; /* The "real" database structure */
53 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000054 char *zBusy; /* The busy callback routine */
drhaa940ea2004-01-15 02:44:03 +000055 char *zCommit; /* The commit hook callback routine */
drhb5a20d32003-04-23 12:25:23 +000056 char *zTrace; /* The trace callback routine */
danielk1977348bb5d2003-10-18 09:37:26 +000057 char *zProgress; /* The progress callback routine */
drhe22a3342003-04-22 20:30:37 +000058 char *zAuth; /* The authorization callback routine */
drhcabb0812002-09-14 13:47:32 +000059 SqlFunc *pFunc; /* List of SQL functions */
drhdcd997e2003-01-31 17:21:49 +000060 int rc; /* Return code of most recent sqlite_exec() */
drhbec3f402000-08-04 13:49:02 +000061};
62
63/*
drh75897232000-05-29 14:26:00 +000064** An instance of this structure passes information thru the sqlite
65** logic from the original TCL command into the callback routine.
66*/
67typedef struct CallbackData CallbackData;
68struct CallbackData {
69 Tcl_Interp *interp; /* The TCL interpreter */
70 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000071 Tcl_Obj *pCode; /* The code to execute for each row */
drhce927062001-11-09 13:41:09 +000072 int once; /* Set for first callback only */
drh960e8c62001-04-03 16:53:21 +000073 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000074 int nColName; /* Number of entries in the azColName[] array */
75 char **azColName; /* Column names translated to UTF-8 */
drh98808ba2001-10-18 12:34:46 +000076};
drh297ecf12001-04-05 15:57:13 +000077
drh6d4abfb2001-10-22 02:58:08 +000078#ifdef UTF_TRANSLATION_NEEDED
drh297ecf12001-04-05 15:57:13 +000079/*
drh75897232000-05-29 14:26:00 +000080** Called for each row of the result.
drh6d4abfb2001-10-22 02:58:08 +000081**
82** This version is used when TCL expects UTF-8 data but the database
83** uses the ISO8859 format. A translation must occur from ISO8859 into
84** UTF-8.
drh75897232000-05-29 14:26:00 +000085*/
86static int DbEvalCallback(
87 void *clientData, /* An instance of CallbackData */
88 int nCol, /* Number of columns in the result */
89 char ** azCol, /* Data for each column */
90 char ** azN /* Name for each column */
91){
92 CallbackData *cbData = (CallbackData*)clientData;
93 int i, rc;
drh297ecf12001-04-05 15:57:13 +000094 Tcl_DString dCol;
drh6d4abfb2001-10-22 02:58:08 +000095 Tcl_DStringInit(&dCol);
drhce927062001-11-09 13:41:09 +000096 if( cbData->azColName==0 ){
97 assert( cbData->once );
98 cbData->once = 0;
99 if( cbData->zArray[0] ){
100 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
drh6d4abfb2001-10-22 02:58:08 +0000101 }
drhce927062001-11-09 13:41:09 +0000102 cbData->azColName = malloc( nCol*sizeof(char*) );
103 if( cbData->azColName==0 ){ return 1; }
drh6d4abfb2001-10-22 02:58:08 +0000104 cbData->nColName = nCol;
105 for(i=0; i<nCol; i++){
106 Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol);
drhce927062001-11-09 13:41:09 +0000107 cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 );
108 if( cbData->azColName[i] ){
109 strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol));
110 }else{
111 return 1;
drh6d4abfb2001-10-22 02:58:08 +0000112 }
drhce927062001-11-09 13:41:09 +0000113 if( cbData->zArray[0] ){
114 Tcl_SetVar2(cbData->interp, cbData->zArray, "*",
115 Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh704027f2002-07-15 20:58:47 +0000116 if( azN[nCol]!=0 ){
drh5080aaa2002-07-11 12:18:16 +0000117 Tcl_DString dType;
118 Tcl_DStringInit(&dType);
119 Tcl_DStringAppend(&dType, "typeof:", -1);
120 Tcl_DStringAppend(&dType, Tcl_DStringValue(&dCol), -1);
121 Tcl_DStringFree(&dCol);
122 Tcl_ExternalToUtfDString(NULL, azN[i+nCol], -1, &dCol);
123 Tcl_SetVar2(cbData->interp, cbData->zArray,
124 Tcl_DStringValue(&dType), Tcl_DStringValue(&dCol),
125 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
126 Tcl_DStringFree(&dType);
127 }
drhce927062001-11-09 13:41:09 +0000128 }
drhfa173a72002-07-10 21:26:00 +0000129
drh6d4abfb2001-10-22 02:58:08 +0000130 Tcl_DStringFree(&dCol);
131 }
drh6d4abfb2001-10-22 02:58:08 +0000132 }
133 if( azCol!=0 ){
134 if( cbData->zArray[0] ){
135 for(i=0; i<nCol; i++){
136 char *z = azCol[i];
137 if( z==0 ) z = "";
138 Tcl_DStringInit(&dCol);
139 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
140 Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i],
141 Tcl_DStringValue(&dCol), 0);
142 Tcl_DStringFree(&dCol);
143 }
144 }else{
145 for(i=0; i<nCol; i++){
146 char *z = azCol[i];
147 if( z==0 ) z = "";
148 Tcl_DStringInit(&dCol);
149 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
150 Tcl_SetVar(cbData->interp, cbData->azColName[i],
151 Tcl_DStringValue(&dCol), 0);
152 Tcl_DStringFree(&dCol);
153 }
154 }
155 }
156 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
157 if( rc==TCL_CONTINUE ) rc = TCL_OK;
158 cbData->tcl_rc = rc;
159 return rc!=TCL_OK;
160}
161#endif /* UTF_TRANSLATION_NEEDED */
162
163#ifndef UTF_TRANSLATION_NEEDED
164/*
165** Called for each row of the result.
166**
167** This version is used when either of the following is true:
168**
169** (1) This version of TCL uses UTF-8 and the data in the
170** SQLite database is already in the UTF-8 format.
171**
172** (2) This version of TCL uses ISO8859 and the data in the
173** SQLite database is already in the ISO8859 format.
174*/
175static int DbEvalCallback(
176 void *clientData, /* An instance of CallbackData */
177 int nCol, /* Number of columns in the result */
178 char ** azCol, /* Data for each column */
179 char ** azN /* Name for each column */
180){
181 CallbackData *cbData = (CallbackData*)clientData;
182 int i, rc;
drh6a535342001-10-19 16:44:56 +0000183 if( azCol==0 || (cbData->once && cbData->zArray[0]) ){
184 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
185 for(i=0; i<nCol; i++){
186 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
187 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh5080aaa2002-07-11 12:18:16 +0000188 if( azN[nCol] ){
189 char *z = sqlite_mprintf("typeof:%s", azN[i]);
190 Tcl_SetVar2(cbData->interp, cbData->zArray, z, azN[i+nCol],
191 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
192 sqlite_freemem(z);
193 }
drh6a535342001-10-19 16:44:56 +0000194 }
195 cbData->once = 0;
196 }
197 if( azCol!=0 ){
198 if( cbData->zArray[0] ){
drh75897232000-05-29 14:26:00 +0000199 for(i=0; i<nCol; i++){
drh6a535342001-10-19 16:44:56 +0000200 char *z = azCol[i];
201 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000202 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
drh6a535342001-10-19 16:44:56 +0000203 }
204 }else{
205 for(i=0; i<nCol; i++){
206 char *z = azCol[i];
207 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000208 Tcl_SetVar(cbData->interp, azN[i], z, 0);
drh75897232000-05-29 14:26:00 +0000209 }
210 }
drh75897232000-05-29 14:26:00 +0000211 }
drh6d313162000-09-21 13:01:35 +0000212 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
drh960e8c62001-04-03 16:53:21 +0000213 if( rc==TCL_CONTINUE ) rc = TCL_OK;
214 cbData->tcl_rc = rc;
215 return rc!=TCL_OK;
drh75897232000-05-29 14:26:00 +0000216}
drh6d4abfb2001-10-22 02:58:08 +0000217#endif
drh75897232000-05-29 14:26:00 +0000218
219/*
drh6d313162000-09-21 13:01:35 +0000220** This is an alternative callback for database queries. Instead
221** of invoking a TCL script to handle the result, this callback just
222** appends each column of the result to a list. After the query
223** is complete, the list is returned.
224*/
225static int DbEvalCallback2(
226 void *clientData, /* An instance of CallbackData */
227 int nCol, /* Number of columns in the result */
228 char ** azCol, /* Data for each column */
229 char ** azN /* Name for each column */
230){
231 Tcl_Obj *pList = (Tcl_Obj*)clientData;
232 int i;
drh6a535342001-10-19 16:44:56 +0000233 if( azCol==0 ) return 0;
drh6d313162000-09-21 13:01:35 +0000234 for(i=0; i<nCol; i++){
235 Tcl_Obj *pElem;
236 if( azCol[i] && *azCol[i] ){
drh297ecf12001-04-05 15:57:13 +0000237#ifdef UTF_TRANSLATION_NEEDED
238 Tcl_DString dCol;
239 Tcl_DStringInit(&dCol);
240 Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol);
241 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
242 Tcl_DStringFree(&dCol);
243#else
drh6d313162000-09-21 13:01:35 +0000244 pElem = Tcl_NewStringObj(azCol[i], -1);
drh297ecf12001-04-05 15:57:13 +0000245#endif
drh6d313162000-09-21 13:01:35 +0000246 }else{
247 pElem = Tcl_NewObj();
248 }
249 Tcl_ListObjAppendElement(0, pList, pElem);
250 }
251 return 0;
252}
253
254/*
drh5d9d7572003-08-19 14:31:01 +0000255** This is a second alternative callback for database queries. A the
256** first column of the first row of the result is made the TCL result.
257*/
258static int DbEvalCallback3(
259 void *clientData, /* An instance of CallbackData */
260 int nCol, /* Number of columns in the result */
261 char ** azCol, /* Data for each column */
262 char ** azN /* Name for each column */
263){
264 Tcl_Interp *interp = (Tcl_Interp*)clientData;
265 Tcl_Obj *pElem;
266 if( azCol==0 ) return 1;
267 if( nCol==0 ) return 1;
268#ifdef UTF_TRANSLATION_NEEDED
269 {
270 Tcl_DString dCol;
271 Tcl_DStringInit(&dCol);
272 Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol);
273 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
274 Tcl_DStringFree(&dCol);
275 }
276#else
277 pElem = Tcl_NewStringObj(azCol[0], -1);
278#endif
279 Tcl_SetObjResult(interp, pElem);
280 return 1;
281}
282
283/*
drh75897232000-05-29 14:26:00 +0000284** Called when the command is deleted.
285*/
286static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000287 SqliteDb *pDb = (SqliteDb*)db;
288 sqlite_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000289 while( pDb->pFunc ){
290 SqlFunc *pFunc = pDb->pFunc;
291 pDb->pFunc = pFunc->pNext;
292 Tcl_Free((char*)pFunc);
293 }
drhbec3f402000-08-04 13:49:02 +0000294 if( pDb->zBusy ){
295 Tcl_Free(pDb->zBusy);
296 }
drhb5a20d32003-04-23 12:25:23 +0000297 if( pDb->zTrace ){
298 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000299 }
drhe22a3342003-04-22 20:30:37 +0000300 if( pDb->zAuth ){
301 Tcl_Free(pDb->zAuth);
302 }
drhbec3f402000-08-04 13:49:02 +0000303 Tcl_Free((char*)pDb);
304}
305
306/*
307** This routine is called when a database file is locked while trying
308** to execute SQL.
309*/
310static int DbBusyHandler(void *cd, const char *zTable, int nTries){
311 SqliteDb *pDb = (SqliteDb*)cd;
312 int rc;
313 char zVal[30];
314 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000315 Tcl_DString cmd;
316
317 Tcl_DStringInit(&cmd);
318 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
319 Tcl_DStringAppendElement(&cmd, zTable);
320 sprintf(zVal, " %d", nTries);
321 Tcl_DStringAppend(&cmd, zVal, -1);
322 zCmd = Tcl_DStringValue(&cmd);
323 rc = Tcl_Eval(pDb->interp, zCmd);
324 Tcl_DStringFree(&cmd);
325 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
326 return 0;
327 }
328 return 1;
drh75897232000-05-29 14:26:00 +0000329}
330
331/*
danielk1977348bb5d2003-10-18 09:37:26 +0000332** This routine is invoked as the 'progress callback' for the database.
333*/
334static int DbProgressHandler(void *cd){
335 SqliteDb *pDb = (SqliteDb*)cd;
336 int rc;
337
338 assert( pDb->zProgress );
339 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
340 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
341 return 1;
342 }
343 return 0;
344}
345
346/*
drhb5a20d32003-04-23 12:25:23 +0000347** This routine is called by the SQLite trace handler whenever a new
348** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000349*/
drhb5a20d32003-04-23 12:25:23 +0000350static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000351 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000352 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000353
drhb5a20d32003-04-23 12:25:23 +0000354 Tcl_DStringInit(&str);
355 Tcl_DStringAppend(&str, pDb->zTrace, -1);
356 Tcl_DStringAppendElement(&str, zSql);
357 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
358 Tcl_DStringFree(&str);
359 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000360}
361
362/*
drhaa940ea2004-01-15 02:44:03 +0000363** This routine is called when a transaction is committed. The
364** TCL script in pDb->zCommit is executed. If it returns non-zero or
365** if it throws an exception, the transaction is rolled back instead
366** of being committed.
367*/
368static int DbCommitHandler(void *cd){
369 SqliteDb *pDb = (SqliteDb*)cd;
370 int rc;
371
372 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
373 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
374 return 1;
375 }
376 return 0;
377}
378
379/*
drhcabb0812002-09-14 13:47:32 +0000380** This routine is called to evaluate an SQL function implemented
381** using TCL script.
382*/
383static void tclSqlFunc(sqlite_func *context, int argc, const char **argv){
384 SqlFunc *p = sqlite_user_data(context);
385 Tcl_DString cmd;
386 int i;
387 int rc;
388
389 Tcl_DStringInit(&cmd);
390 Tcl_DStringAppend(&cmd, p->zScript, -1);
391 for(i=0; i<argc; i++){
392 Tcl_DStringAppendElement(&cmd, argv[i] ? argv[i] : "");
393 }
394 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
395 if( rc ){
396 sqlite_set_result_error(context, Tcl_GetStringResult(p->interp), -1);
397 }else{
398 sqlite_set_result_string(context, Tcl_GetStringResult(p->interp), -1);
399 }
400}
drhe22a3342003-04-22 20:30:37 +0000401#ifndef SQLITE_OMIT_AUTHORIZATION
402/*
403** This is the authentication function. It appends the authentication
404** type code and the two arguments to zCmd[] then invokes the result
405** on the interpreter. The reply is examined to determine if the
406** authentication fails or succeeds.
407*/
408static int auth_callback(
409 void *pArg,
410 int code,
411 const char *zArg1,
412 const char *zArg2,
413 const char *zArg3,
414 const char *zArg4
415){
416 char *zCode;
417 Tcl_DString str;
418 int rc;
419 const char *zReply;
420 SqliteDb *pDb = (SqliteDb*)pArg;
421
422 switch( code ){
423 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
424 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
425 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
426 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
427 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
428 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
429 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
430 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
431 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
432 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
433 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
434 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
435 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
436 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
437 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
438 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
439 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
440 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
441 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
442 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
443 case SQLITE_READ : zCode="SQLITE_READ"; break;
444 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
445 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
446 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000447 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
448 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
drhe22a3342003-04-22 20:30:37 +0000449 default : zCode="????"; break;
450 }
451 Tcl_DStringInit(&str);
452 Tcl_DStringAppend(&str, pDb->zAuth, -1);
453 Tcl_DStringAppendElement(&str, zCode);
454 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
455 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
456 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
457 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
458 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
459 Tcl_DStringFree(&str);
460 zReply = Tcl_GetStringResult(pDb->interp);
461 if( strcmp(zReply,"SQLITE_OK")==0 ){
462 rc = SQLITE_OK;
463 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
464 rc = SQLITE_DENY;
465 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
466 rc = SQLITE_IGNORE;
467 }else{
468 rc = 999;
469 }
470 return rc;
471}
472#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000473
474/*
drh75897232000-05-29 14:26:00 +0000475** The "sqlite" command below creates a new Tcl command for each
476** connection it opens to an SQLite database. This routine is invoked
477** whenever one of those connection-specific commands is executed
478** in Tcl. For example, if you run Tcl code like this:
479**
480** sqlite db1 "my_database"
481** db1 close
482**
483** The first command opens a connection to the "my_database" database
484** and calls that connection "db1". The second command causes this
485** subroutine to be invoked.
486*/
drh6d313162000-09-21 13:01:35 +0000487static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000488 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000489 int choice;
drh22fbcb82004-02-01 01:22:50 +0000490 int rc = TCL_OK;
drh0de8c112002-07-06 16:32:14 +0000491 static const char *DB_strs[] = {
rdcf146a772004-02-25 22:51:06 +0000492 "authorizer", "busy", "changes",
493 "close", "commit_hook", "complete",
494 "errorcode", "eval", "function",
495 "last_insert_rowid", "last_statement_changes", "onecolumn",
496 "progress", "rekey", "timeout",
497 "trace",
drh22fbcb82004-02-01 01:22:50 +0000498 0
drh6d313162000-09-21 13:01:35 +0000499 };
drh411995d2002-06-25 19:31:18 +0000500 enum DB_enum {
rdcf146a772004-02-25 22:51:06 +0000501 DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
502 DB_CLOSE, DB_COMMIT_HOOK, DB_COMPLETE,
503 DB_ERRORCODE, DB_EVAL, DB_FUNCTION,
504 DB_LAST_INSERT_ROWID, DB_LAST_STATEMENT_CHANGES, DB_ONECOLUMN,
505 DB_PROGRESS, DB_REKEY, DB_TIMEOUT,
506 DB_TRACE
drh6d313162000-09-21 13:01:35 +0000507 };
508
509 if( objc<2 ){
510 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000511 return TCL_ERROR;
512 }
drh411995d2002-06-25 19:31:18 +0000513 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000514 return TCL_ERROR;
515 }
516
drh411995d2002-06-25 19:31:18 +0000517 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000518
drhe22a3342003-04-22 20:30:37 +0000519 /* $db authorizer ?CALLBACK?
520 **
521 ** Invoke the given callback to authorize each SQL operation as it is
522 ** compiled. 5 arguments are appended to the callback before it is
523 ** invoked:
524 **
525 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
526 ** (2) First descriptive name (depends on authorization type)
527 ** (3) Second descriptive name
528 ** (4) Name of the database (ex: "main", "temp")
529 ** (5) Name of trigger that is doing the access
530 **
531 ** The callback should return on of the following strings: SQLITE_OK,
532 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
533 **
534 ** If this method is invoked with no arguments, the current authorization
535 ** callback string is returned.
536 */
537 case DB_AUTHORIZER: {
538 if( objc>3 ){
539 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
540 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000541 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000542 Tcl_AppendResult(interp, pDb->zAuth, 0);
543 }
544 }else{
545 char *zAuth;
546 int len;
547 if( pDb->zAuth ){
548 Tcl_Free(pDb->zAuth);
549 }
550 zAuth = Tcl_GetStringFromObj(objv[2], &len);
551 if( zAuth && len>0 ){
552 pDb->zAuth = Tcl_Alloc( len + 1 );
553 strcpy(pDb->zAuth, zAuth);
554 }else{
555 pDb->zAuth = 0;
556 }
557#ifndef SQLITE_OMIT_AUTHORIZATION
558 if( pDb->zAuth ){
559 pDb->interp = interp;
560 sqlite_set_authorizer(pDb->db, auth_callback, pDb);
561 }else{
562 sqlite_set_authorizer(pDb->db, 0, 0);
563 }
564#endif
565 }
566 break;
567 }
568
drhbec3f402000-08-04 13:49:02 +0000569 /* $db busy ?CALLBACK?
570 **
571 ** Invoke the given callback if an SQL statement attempts to open
572 ** a locked database file.
573 */
drh6d313162000-09-21 13:01:35 +0000574 case DB_BUSY: {
575 if( objc>3 ){
576 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000577 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000578 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000579 if( pDb->zBusy ){
580 Tcl_AppendResult(interp, pDb->zBusy, 0);
581 }
582 }else{
drh6d313162000-09-21 13:01:35 +0000583 char *zBusy;
584 int len;
drhbec3f402000-08-04 13:49:02 +0000585 if( pDb->zBusy ){
586 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000587 }
drh6d313162000-09-21 13:01:35 +0000588 zBusy = Tcl_GetStringFromObj(objv[2], &len);
589 if( zBusy && len>0 ){
590 pDb->zBusy = Tcl_Alloc( len + 1 );
591 strcpy(pDb->zBusy, zBusy);
592 }else{
593 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000594 }
595 if( pDb->zBusy ){
596 pDb->interp = interp;
597 sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000598 }else{
599 sqlite_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000600 }
601 }
drh6d313162000-09-21 13:01:35 +0000602 break;
603 }
drhbec3f402000-08-04 13:49:02 +0000604
danielk1977348bb5d2003-10-18 09:37:26 +0000605 /* $db progress ?N CALLBACK?
606 **
607 ** Invoke the given callback every N virtual machine opcodes while executing
608 ** queries.
609 */
610 case DB_PROGRESS: {
611 if( objc==2 ){
612 if( pDb->zProgress ){
613 Tcl_AppendResult(interp, pDb->zProgress, 0);
614 }
615 }else if( objc==4 ){
616 char *zProgress;
617 int len;
618 int N;
619 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
620 return TCL_ERROR;
621 };
622 if( pDb->zProgress ){
623 Tcl_Free(pDb->zProgress);
624 }
625 zProgress = Tcl_GetStringFromObj(objv[3], &len);
626 if( zProgress && len>0 ){
627 pDb->zProgress = Tcl_Alloc( len + 1 );
628 strcpy(pDb->zProgress, zProgress);
629 }else{
630 pDb->zProgress = 0;
631 }
632#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
633 if( pDb->zProgress ){
634 pDb->interp = interp;
635 sqlite_progress_handler(pDb->db, N, DbProgressHandler, pDb);
636 }else{
637 sqlite_progress_handler(pDb->db, 0, 0, 0);
638 }
639#endif
640 }else{
641 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
642 return TCL_ERROR;
643 }
644 break;
645 }
646
drhc8d30ac2002-04-12 10:08:59 +0000647 /*
648 ** $db changes
649 **
650 ** Return the number of rows that were modified, inserted, or deleted by
651 ** the most recent "eval".
652 */
653 case DB_CHANGES: {
654 Tcl_Obj *pResult;
655 int nChange;
656 if( objc!=2 ){
657 Tcl_WrongNumArgs(interp, 2, objv, "");
658 return TCL_ERROR;
659 }
660 nChange = sqlite_changes(pDb->db);
661 pResult = Tcl_GetObjResult(interp);
662 Tcl_SetIntObj(pResult, nChange);
663 break;
664 }
665
rdcf146a772004-02-25 22:51:06 +0000666 /*
667 ** $db last_statement_changes
668 **
669 ** Return the number of rows that were modified, inserted, or deleted by
670 ** the last statment to complete execution (excluding changes due to
671 ** triggers)
672 */
673 case DB_LAST_STATEMENT_CHANGES: {
674 Tcl_Obj *pResult;
675 int lsChange;
676 if( objc!=2 ){
677 Tcl_WrongNumArgs(interp, 2, objv, "");
678 return TCL_ERROR;
679 }
680 lsChange = sqlite_last_statement_changes(pDb->db);
681 pResult = Tcl_GetObjResult(interp);
682 Tcl_SetIntObj(pResult, lsChange);
683 break;
684 }
685
drh75897232000-05-29 14:26:00 +0000686 /* $db close
687 **
688 ** Shutdown the database
689 */
drh6d313162000-09-21 13:01:35 +0000690 case DB_CLOSE: {
691 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
692 break;
693 }
drh75897232000-05-29 14:26:00 +0000694
drhaa940ea2004-01-15 02:44:03 +0000695 /* $db commit_hook ?CALLBACK?
696 **
697 ** Invoke the given callback just before committing every SQL transaction.
698 ** If the callback throws an exception or returns non-zero, then the
699 ** transaction is aborted. If CALLBACK is an empty string, the callback
700 ** is disabled.
701 */
702 case DB_COMMIT_HOOK: {
703 if( objc>3 ){
704 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
705 }else if( objc==2 ){
706 if( pDb->zCommit ){
707 Tcl_AppendResult(interp, pDb->zCommit, 0);
708 }
709 }else{
710 char *zCommit;
711 int len;
712 if( pDb->zCommit ){
713 Tcl_Free(pDb->zCommit);
714 }
715 zCommit = Tcl_GetStringFromObj(objv[2], &len);
716 if( zCommit && len>0 ){
717 pDb->zCommit = Tcl_Alloc( len + 1 );
718 strcpy(pDb->zCommit, zCommit);
719 }else{
720 pDb->zCommit = 0;
721 }
722 if( pDb->zCommit ){
723 pDb->interp = interp;
724 sqlite_commit_hook(pDb->db, DbCommitHandler, pDb);
725 }else{
726 sqlite_commit_hook(pDb->db, 0, 0);
727 }
728 }
729 break;
730 }
731
drh75897232000-05-29 14:26:00 +0000732 /* $db complete SQL
733 **
734 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
735 ** additional lines of input are needed. This is similar to the
736 ** built-in "info complete" command of Tcl.
737 */
drh6d313162000-09-21 13:01:35 +0000738 case DB_COMPLETE: {
739 Tcl_Obj *pResult;
740 int isComplete;
741 if( objc!=3 ){
742 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000743 return TCL_ERROR;
744 }
drh6d313162000-09-21 13:01:35 +0000745 isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
746 pResult = Tcl_GetObjResult(interp);
747 Tcl_SetBooleanObj(pResult, isComplete);
748 break;
749 }
drhdcd997e2003-01-31 17:21:49 +0000750
751 /*
752 ** $db errorcode
753 **
754 ** Return the numeric error code that was returned by the most recent
755 ** call to sqlite_exec().
756 */
757 case DB_ERRORCODE: {
758 Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc));
759 break;
760 }
drh75897232000-05-29 14:26:00 +0000761
762 /*
763 ** $db eval $sql ?array { ...code... }?
764 **
765 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000766 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000767 ** If "array" and "code" are omitted, then no callback is every invoked.
768 ** If "array" is an empty string, then the values are placed in variables
769 ** that have the same name as the fields extracted by the query.
770 */
drh6d313162000-09-21 13:01:35 +0000771 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000772 CallbackData cbData;
773 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000774 char *zSql;
drh297ecf12001-04-05 15:57:13 +0000775#ifdef UTF_TRANSLATION_NEEDED
776 Tcl_DString dSql;
drh6d4abfb2001-10-22 02:58:08 +0000777 int i;
drh297ecf12001-04-05 15:57:13 +0000778#endif
drh75897232000-05-29 14:26:00 +0000779
drh6d313162000-09-21 13:01:35 +0000780 if( objc!=5 && objc!=3 ){
781 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000782 return TCL_ERROR;
783 }
drhbec3f402000-08-04 13:49:02 +0000784 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000785 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh297ecf12001-04-05 15:57:13 +0000786#ifdef UTF_TRANSLATION_NEEDED
787 Tcl_DStringInit(&dSql);
788 Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
789 zSql = Tcl_DStringValue(&dSql);
790#endif
drh6d313162000-09-21 13:01:35 +0000791 Tcl_IncrRefCount(objv[2]);
792 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000793 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000794 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000795 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
796 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000797 cbData.tcl_rc = TCL_OK;
drh6d4abfb2001-10-22 02:58:08 +0000798 cbData.nColName = 0;
799 cbData.azColName = 0;
drh75897232000-05-29 14:26:00 +0000800 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000801 Tcl_IncrRefCount(objv[3]);
802 Tcl_IncrRefCount(objv[4]);
803 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
804 Tcl_DecrRefCount(objv[4]);
805 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000806 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000807 }else{
drh6d313162000-09-21 13:01:35 +0000808 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000809 cbData.tcl_rc = TCL_OK;
drh6d313162000-09-21 13:01:35 +0000810 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
811 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000812 }
drhdcd997e2003-01-31 17:21:49 +0000813 pDb->rc = rc;
drhb798fa62002-09-03 19:43:23 +0000814 if( rc==SQLITE_ABORT ){
815 if( zErrMsg ) free(zErrMsg);
816 rc = cbData.tcl_rc;
817 }else if( zErrMsg ){
drh75897232000-05-29 14:26:00 +0000818 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
819 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000820 rc = TCL_ERROR;
drhb798fa62002-09-03 19:43:23 +0000821 }else if( rc!=SQLITE_OK ){
drh6d4abfb2001-10-22 02:58:08 +0000822 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
823 rc = TCL_ERROR;
drh960e8c62001-04-03 16:53:21 +0000824 }else{
drh75897232000-05-29 14:26:00 +0000825 }
drh6d313162000-09-21 13:01:35 +0000826 Tcl_DecrRefCount(objv[2]);
drh297ecf12001-04-05 15:57:13 +0000827#ifdef UTF_TRANSLATION_NEEDED
828 Tcl_DStringFree(&dSql);
drh6d4abfb2001-10-22 02:58:08 +0000829 if( objc==5 && cbData.azColName ){
830 for(i=0; i<cbData.nColName; i++){
831 if( cbData.azColName[i] ) free(cbData.azColName[i]);
832 }
833 free(cbData.azColName);
drhce927062001-11-09 13:41:09 +0000834 cbData.azColName = 0;
drh6d4abfb2001-10-22 02:58:08 +0000835 }
drh297ecf12001-04-05 15:57:13 +0000836#endif
drh75897232000-05-29 14:26:00 +0000837 return rc;
drh6d313162000-09-21 13:01:35 +0000838 }
drhbec3f402000-08-04 13:49:02 +0000839
840 /*
drhcabb0812002-09-14 13:47:32 +0000841 ** $db function NAME SCRIPT
842 **
843 ** Create a new SQL function called NAME. Whenever that function is
844 ** called, invoke SCRIPT to evaluate the function.
845 */
846 case DB_FUNCTION: {
847 SqlFunc *pFunc;
848 char *zName;
849 char *zScript;
850 int nScript;
851 if( objc!=4 ){
852 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
853 return TCL_ERROR;
854 }
855 zName = Tcl_GetStringFromObj(objv[2], 0);
856 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
857 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
858 if( pFunc==0 ) return TCL_ERROR;
859 pFunc->interp = interp;
860 pFunc->pNext = pDb->pFunc;
861 pFunc->zScript = (char*)&pFunc[1];
862 strcpy(pFunc->zScript, zScript);
863 sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc);
864 sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC);
865 break;
866 }
867
868 /*
drhaf9ff332002-01-16 21:00:27 +0000869 ** $db last_insert_rowid
870 **
871 ** Return an integer which is the ROWID for the most recent insert.
872 */
873 case DB_LAST_INSERT_ROWID: {
874 Tcl_Obj *pResult;
875 int rowid;
876 if( objc!=2 ){
877 Tcl_WrongNumArgs(interp, 2, objv, "");
878 return TCL_ERROR;
879 }
880 rowid = sqlite_last_insert_rowid(pDb->db);
881 pResult = Tcl_GetObjResult(interp);
882 Tcl_SetIntObj(pResult, rowid);
883 break;
884 }
885
886 /*
drh5d9d7572003-08-19 14:31:01 +0000887 ** $db onecolumn SQL
888 **
889 ** Return a single column from a single row of the given SQL query.
890 */
891 case DB_ONECOLUMN: {
drh5d9d7572003-08-19 14:31:01 +0000892 char *zSql;
893 char *zErrMsg = 0;
894 if( objc!=3 ){
895 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
896 return TCL_ERROR;
897 }
898 zSql = Tcl_GetStringFromObj(objv[2], 0);
899 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
900 if( rc==SQLITE_ABORT ){
drh22fbcb82004-02-01 01:22:50 +0000901 rc = SQLITE_OK;
drh5d9d7572003-08-19 14:31:01 +0000902 }else if( zErrMsg ){
903 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
904 free(zErrMsg);
905 rc = TCL_ERROR;
906 }else if( rc!=SQLITE_OK ){
907 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
908 rc = TCL_ERROR;
909 }
910 break;
911 }
912
913 /*
drh22fbcb82004-02-01 01:22:50 +0000914 ** $db rekey KEY
915 **
916 ** Change the encryption key on the currently open database.
917 */
918 case DB_REKEY: {
919 int nKey;
920 void *pKey;
921 if( objc!=3 ){
922 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
923 return TCL_ERROR;
924 }
925 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +0000926#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000927 rc = sqlite_rekey(pDb->db, pKey, nKey);
928 if( rc ){
929 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
930 rc = TCL_ERROR;
931 }
932#endif
933 break;
934 }
935
936 /*
drhbec3f402000-08-04 13:49:02 +0000937 ** $db timeout MILLESECONDS
938 **
939 ** Delay for the number of milliseconds specified when a file is locked.
940 */
drh6d313162000-09-21 13:01:35 +0000941 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000942 int ms;
drh6d313162000-09-21 13:01:35 +0000943 if( objc!=3 ){
944 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000945 return TCL_ERROR;
946 }
drh6d313162000-09-21 13:01:35 +0000947 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
drhbec3f402000-08-04 13:49:02 +0000948 sqlite_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000949 break;
drh75897232000-05-29 14:26:00 +0000950 }
drhb5a20d32003-04-23 12:25:23 +0000951
952 /* $db trace ?CALLBACK?
953 **
954 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
955 ** that is executed. The text of the SQL is appended to CALLBACK before
956 ** it is executed.
957 */
958 case DB_TRACE: {
959 if( objc>3 ){
960 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
961 }else if( objc==2 ){
962 if( pDb->zTrace ){
963 Tcl_AppendResult(interp, pDb->zTrace, 0);
964 }
965 }else{
966 char *zTrace;
967 int len;
968 if( pDb->zTrace ){
969 Tcl_Free(pDb->zTrace);
970 }
971 zTrace = Tcl_GetStringFromObj(objv[2], &len);
972 if( zTrace && len>0 ){
973 pDb->zTrace = Tcl_Alloc( len + 1 );
974 strcpy(pDb->zTrace, zTrace);
975 }else{
976 pDb->zTrace = 0;
977 }
978 if( pDb->zTrace ){
979 pDb->interp = interp;
980 sqlite_trace(pDb->db, DbTraceHandler, pDb);
981 }else{
982 sqlite_trace(pDb->db, 0, 0);
983 }
984 }
985 break;
986 }
987
drh6d313162000-09-21 13:01:35 +0000988 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +0000989 return rc;
drh75897232000-05-29 14:26:00 +0000990}
991
992/*
drh22fbcb82004-02-01 01:22:50 +0000993** sqlite DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +0000994**
995** This is the main Tcl command. When the "sqlite" Tcl command is
996** invoked, this routine runs to process that command.
997**
998** The first argument, DBNAME, is an arbitrary name for a new
999** database connection. This command creates a new command named
1000** DBNAME that is used to control that connection. The database
1001** connection is deleted when the DBNAME command is deleted.
1002**
1003** The second argument is the name of the directory that contains
1004** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +00001005**
1006** For testing purposes, we also support the following:
1007**
1008** sqlite -encoding
1009**
1010** Return the encoding used by LIKE and GLOB operators. Choices
1011** are UTF-8 and iso8859.
1012**
drh647cb0e2002-11-04 19:32:25 +00001013** sqlite -version
1014**
1015** Return the version number of the SQLite library.
1016**
drhfbc3eab2001-04-06 16:13:42 +00001017** sqlite -tcl-uses-utf
1018**
1019** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
1020** not. Used by tests to make sure the library was compiled
1021** correctly.
drh75897232000-05-29 14:26:00 +00001022*/
drh22fbcb82004-02-01 01:22:50 +00001023static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drh75897232000-05-29 14:26:00 +00001024 int mode;
drhbec3f402000-08-04 13:49:02 +00001025 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +00001026 void *pKey = 0;
1027 int nKey = 0;
1028 const char *zArg;
drh75897232000-05-29 14:26:00 +00001029 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +00001030 const char *zFile;
drh06b27182002-06-26 20:06:05 +00001031 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +00001032 if( objc==2 ){
1033 zArg = Tcl_GetStringFromObj(objv[1], 0);
1034 if( strcmp(zArg,"-encoding")==0 ){
drhfbc3eab2001-04-06 16:13:42 +00001035 Tcl_AppendResult(interp,sqlite_encoding,0);
1036 return TCL_OK;
1037 }
drh22fbcb82004-02-01 01:22:50 +00001038 if( strcmp(zArg,"-version")==0 ){
drh647cb0e2002-11-04 19:32:25 +00001039 Tcl_AppendResult(interp,sqlite_version,0);
1040 return TCL_OK;
1041 }
drh9eb9e262004-02-11 02:18:05 +00001042 if( strcmp(zArg,"-has-codec")==0 ){
1043#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001044 Tcl_AppendResult(interp,"1",0);
1045#else
1046 Tcl_AppendResult(interp,"0",0);
1047#endif
1048 return TCL_OK;
1049 }
1050 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +00001051#ifdef TCL_UTF_MAX
1052 Tcl_AppendResult(interp,"1",0);
1053#else
1054 Tcl_AppendResult(interp,"0",0);
1055#endif
1056 return TCL_OK;
1057 }
1058 }
drh22fbcb82004-02-01 01:22:50 +00001059 if( objc==5 || objc==6 ){
1060 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
1061 if( strcmp(zArg,"-key")==0 ){
1062 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
1063 objc -= 2;
1064 }
1065 }
1066 if( objc!=3 && objc!=4 ){
1067 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +00001068#ifdef SQLITE_HAS_CODEC
1069 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +00001070#else
1071 "HANDLE FILENAME ?MODE?"
1072#endif
1073 );
drh75897232000-05-29 14:26:00 +00001074 return TCL_ERROR;
1075 }
drh22fbcb82004-02-01 01:22:50 +00001076 if( objc==3 ){
drh58b95762000-06-02 01:17:37 +00001077 mode = 0666;
drh22fbcb82004-02-01 01:22:50 +00001078 }else if( Tcl_GetIntFromObj(interp, objv[3], &mode)!=TCL_OK ){
drh75897232000-05-29 14:26:00 +00001079 return TCL_ERROR;
1080 }
1081 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001082 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001083 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001084 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1085 return TCL_ERROR;
1086 }
1087 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00001088 zFile = Tcl_GetStringFromObj(objv[2], 0);
drh9eb9e262004-02-11 02:18:05 +00001089#ifdef SQLITE_HAS_CODEC
drhe384a4e2004-02-12 20:49:36 +00001090 p->db = sqlite_open_encrypted(zFile, pKey, nKey, 0, &zErrMsg);
drheb8ed702004-02-11 10:37:23 +00001091#else
drh22fbcb82004-02-01 01:22:50 +00001092 p->db = sqlite_open(zFile, mode, &zErrMsg);
drheb8ed702004-02-11 10:37:23 +00001093#endif
drhbec3f402000-08-04 13:49:02 +00001094 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001095 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001096 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001097 free(zErrMsg);
1098 return TCL_ERROR;
1099 }
drh22fbcb82004-02-01 01:22:50 +00001100 zArg = Tcl_GetStringFromObj(objv[1], 0);
1101 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001102
drh06b27182002-06-26 20:06:05 +00001103 /* The return value is the value of the sqlite* pointer
1104 */
1105 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001106 if( strncmp(zBuf,"0x",2) ){
1107 sprintf(zBuf, "0x%p", p->db);
1108 }
drh06b27182002-06-26 20:06:05 +00001109 Tcl_AppendResult(interp, zBuf, 0);
1110
drhc22bd472002-05-10 13:14:07 +00001111 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001112 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001113 */
drh28b4e482002-03-11 02:06:13 +00001114#ifdef SQLITE_TEST
1115 {
drhc22bd472002-05-10 13:14:07 +00001116 extern void Md5_Register(sqlite*);
1117 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +00001118 }
drh28b4e482002-03-11 02:06:13 +00001119#endif
drh75897232000-05-29 14:26:00 +00001120 return TCL_OK;
1121}
1122
1123/*
drh90ca9752001-09-28 17:47:14 +00001124** Provide a dummy Tcl_InitStubs if we are using this as a static
1125** library.
1126*/
1127#ifndef USE_TCL_STUBS
1128# undef Tcl_InitStubs
1129# define Tcl_InitStubs(a,b,c)
1130#endif
1131
1132/*
drh75897232000-05-29 14:26:00 +00001133** Initialize this module.
1134**
1135** This Tcl module contains only a single new Tcl command named "sqlite".
1136** (Hence there is no namespace. There is no point in using a namespace
1137** if the extension only supplies one new name!) The "sqlite" command is
1138** used to open a new SQLite database. See the DbMain() routine above
1139** for additional information.
1140*/
1141int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001142 Tcl_InitStubs(interp, "8.0", 0);
drh22fbcb82004-02-01 01:22:50 +00001143 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001144 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh90ca9752001-09-28 17:47:14 +00001145 return TCL_OK;
1146}
1147int Tclsqlite_Init(Tcl_Interp *interp){
1148 Tcl_InitStubs(interp, "8.0", 0);
drh22fbcb82004-02-01 01:22:50 +00001149 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001150 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh75897232000-05-29 14:26:00 +00001151 return TCL_OK;
1152}
1153int Sqlite_SafeInit(Tcl_Interp *interp){
1154 return TCL_OK;
1155}
drh90ca9752001-09-28 17:47:14 +00001156int Tclsqlite_SafeInit(Tcl_Interp *interp){
1157 return TCL_OK;
1158}
drh3aac2dd2004-04-26 14:10:20 +00001159#endif
drh75897232000-05-29 14:26:00 +00001160
drh3cebbde2000-10-19 14:59:27 +00001161#if 0
drh75897232000-05-29 14:26:00 +00001162/*
1163** If compiled using mktclapp, this routine runs to initialize
1164** everything.
1165*/
1166int Et_AppInit(Tcl_Interp *interp){
1167 return Sqlite_Init(interp);
1168}
drh3cebbde2000-10-19 14:59:27 +00001169#endif
drh348784e2000-05-29 20:41:49 +00001170
1171/*
1172** If the macro TCLSH is defined and is one, then put in code for the
1173** "main" routine that will initialize Tcl.
1174*/
1175#if defined(TCLSH) && TCLSH==1
1176static char zMainloop[] =
1177 "set line {}\n"
1178 "while {![eof stdin]} {\n"
1179 "if {$line!=\"\"} {\n"
1180 "puts -nonewline \"> \"\n"
1181 "} else {\n"
1182 "puts -nonewline \"% \"\n"
1183 "}\n"
1184 "flush stdout\n"
1185 "append line [gets stdin]\n"
1186 "if {[info complete $line]} {\n"
1187 "if {[catch {uplevel #0 $line} result]} {\n"
1188 "puts stderr \"Error: $result\"\n"
1189 "} elseif {$result!=\"\"} {\n"
1190 "puts $result\n"
1191 "}\n"
1192 "set line {}\n"
1193 "} else {\n"
1194 "append line \\n\n"
1195 "}\n"
1196 "}\n"
1197;
1198
1199#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1200int TCLSH_MAIN(int argc, char **argv){
1201 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001202 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001203 interp = Tcl_CreateInterp();
drh3aac2dd2004-04-26 14:10:20 +00001204 /* Sqlite_Init(interp); */
drhd9b02572001-04-15 00:37:09 +00001205#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001206 {
1207 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001208 extern int Sqlitetest2_Init(Tcl_Interp*);
1209 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001210 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001211 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001212 extern int Md5_Init(Tcl_Interp*);
drh3aac2dd2004-04-26 14:10:20 +00001213 /* Sqlitetest1_Init(interp); */
drh5c4d9702001-08-20 00:33:58 +00001214 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001215 Sqlitetest3_Init(interp);
drh3aac2dd2004-04-26 14:10:20 +00001216 /* Sqlitetest4_Init(interp); */
danielk1977998b56c2004-05-06 23:37:52 +00001217 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001218 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001219 }
1220#endif
drh348784e2000-05-29 20:41:49 +00001221 if( argc>=2 ){
1222 int i;
1223 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1224 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1225 for(i=2; i<argc; i++){
1226 Tcl_SetVar(interp, "argv", argv[i],
1227 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1228 }
1229 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001230 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001231 if( zInfo==0 ) zInfo = interp->result;
1232 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001233 return 1;
1234 }
1235 }else{
1236 Tcl_GlobalEval(interp, zMainloop);
1237 }
1238 return 0;
1239}
1240#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001241
1242#endif /* !defined(NO_TCL) */