blob: 70706306adc9103690e349601b28aff331e15503 [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**
drhaa940ea2004-01-15 02:44:03 +000014** $Id: tclsqlite.c,v 1.54 2004/01/15 02:44:03 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 */
drhaa940ea2004-01-15 02:44:03 +000054 char *zCommit; /* The commit hook callback routine */
drhb5a20d32003-04-23 12:25:23 +000055 char *zTrace; /* The trace callback routine */
danielk1977348bb5d2003-10-18 09:37:26 +000056 char *zProgress; /* The progress callback routine */
drhe22a3342003-04-22 20:30:37 +000057 char *zAuth; /* The authorization callback routine */
drhcabb0812002-09-14 13:47:32 +000058 SqlFunc *pFunc; /* List of SQL functions */
drhdcd997e2003-01-31 17:21:49 +000059 int rc; /* Return code of most recent sqlite_exec() */
drhbec3f402000-08-04 13:49:02 +000060};
61
62/*
drh75897232000-05-29 14:26:00 +000063** An instance of this structure passes information thru the sqlite
64** logic from the original TCL command into the callback routine.
65*/
66typedef struct CallbackData CallbackData;
67struct CallbackData {
68 Tcl_Interp *interp; /* The TCL interpreter */
69 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000070 Tcl_Obj *pCode; /* The code to execute for each row */
drhce927062001-11-09 13:41:09 +000071 int once; /* Set for first callback only */
drh960e8c62001-04-03 16:53:21 +000072 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000073 int nColName; /* Number of entries in the azColName[] array */
74 char **azColName; /* Column names translated to UTF-8 */
drh98808ba2001-10-18 12:34:46 +000075};
drh297ecf12001-04-05 15:57:13 +000076
drh6d4abfb2001-10-22 02:58:08 +000077#ifdef UTF_TRANSLATION_NEEDED
drh297ecf12001-04-05 15:57:13 +000078/*
drh75897232000-05-29 14:26:00 +000079** Called for each row of the result.
drh6d4abfb2001-10-22 02:58:08 +000080**
81** This version is used when TCL expects UTF-8 data but the database
82** uses the ISO8859 format. A translation must occur from ISO8859 into
83** UTF-8.
drh75897232000-05-29 14:26:00 +000084*/
85static int DbEvalCallback(
86 void *clientData, /* An instance of CallbackData */
87 int nCol, /* Number of columns in the result */
88 char ** azCol, /* Data for each column */
89 char ** azN /* Name for each column */
90){
91 CallbackData *cbData = (CallbackData*)clientData;
92 int i, rc;
drh297ecf12001-04-05 15:57:13 +000093 Tcl_DString dCol;
drh6d4abfb2001-10-22 02:58:08 +000094 Tcl_DStringInit(&dCol);
drhce927062001-11-09 13:41:09 +000095 if( cbData->azColName==0 ){
96 assert( cbData->once );
97 cbData->once = 0;
98 if( cbData->zArray[0] ){
99 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
drh6d4abfb2001-10-22 02:58:08 +0000100 }
drhce927062001-11-09 13:41:09 +0000101 cbData->azColName = malloc( nCol*sizeof(char*) );
102 if( cbData->azColName==0 ){ return 1; }
drh6d4abfb2001-10-22 02:58:08 +0000103 cbData->nColName = nCol;
104 for(i=0; i<nCol; i++){
105 Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol);
drhce927062001-11-09 13:41:09 +0000106 cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 );
107 if( cbData->azColName[i] ){
108 strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol));
109 }else{
110 return 1;
drh6d4abfb2001-10-22 02:58:08 +0000111 }
drhce927062001-11-09 13:41:09 +0000112 if( cbData->zArray[0] ){
113 Tcl_SetVar2(cbData->interp, cbData->zArray, "*",
114 Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh704027f2002-07-15 20:58:47 +0000115 if( azN[nCol]!=0 ){
drh5080aaa2002-07-11 12:18:16 +0000116 Tcl_DString dType;
117 Tcl_DStringInit(&dType);
118 Tcl_DStringAppend(&dType, "typeof:", -1);
119 Tcl_DStringAppend(&dType, Tcl_DStringValue(&dCol), -1);
120 Tcl_DStringFree(&dCol);
121 Tcl_ExternalToUtfDString(NULL, azN[i+nCol], -1, &dCol);
122 Tcl_SetVar2(cbData->interp, cbData->zArray,
123 Tcl_DStringValue(&dType), Tcl_DStringValue(&dCol),
124 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
125 Tcl_DStringFree(&dType);
126 }
drhce927062001-11-09 13:41:09 +0000127 }
drhfa173a72002-07-10 21:26:00 +0000128
drh6d4abfb2001-10-22 02:58:08 +0000129 Tcl_DStringFree(&dCol);
130 }
drh6d4abfb2001-10-22 02:58:08 +0000131 }
132 if( azCol!=0 ){
133 if( cbData->zArray[0] ){
134 for(i=0; i<nCol; i++){
135 char *z = azCol[i];
136 if( z==0 ) z = "";
137 Tcl_DStringInit(&dCol);
138 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
139 Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i],
140 Tcl_DStringValue(&dCol), 0);
141 Tcl_DStringFree(&dCol);
142 }
143 }else{
144 for(i=0; i<nCol; i++){
145 char *z = azCol[i];
146 if( z==0 ) z = "";
147 Tcl_DStringInit(&dCol);
148 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
149 Tcl_SetVar(cbData->interp, cbData->azColName[i],
150 Tcl_DStringValue(&dCol), 0);
151 Tcl_DStringFree(&dCol);
152 }
153 }
154 }
155 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
156 if( rc==TCL_CONTINUE ) rc = TCL_OK;
157 cbData->tcl_rc = rc;
158 return rc!=TCL_OK;
159}
160#endif /* UTF_TRANSLATION_NEEDED */
161
162#ifndef UTF_TRANSLATION_NEEDED
163/*
164** Called for each row of the result.
165**
166** This version is used when either of the following is true:
167**
168** (1) This version of TCL uses UTF-8 and the data in the
169** SQLite database is already in the UTF-8 format.
170**
171** (2) This version of TCL uses ISO8859 and the data in the
172** SQLite database is already in the ISO8859 format.
173*/
174static int DbEvalCallback(
175 void *clientData, /* An instance of CallbackData */
176 int nCol, /* Number of columns in the result */
177 char ** azCol, /* Data for each column */
178 char ** azN /* Name for each column */
179){
180 CallbackData *cbData = (CallbackData*)clientData;
181 int i, rc;
drh6a535342001-10-19 16:44:56 +0000182 if( azCol==0 || (cbData->once && cbData->zArray[0]) ){
183 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
184 for(i=0; i<nCol; i++){
185 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
186 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh5080aaa2002-07-11 12:18:16 +0000187 if( azN[nCol] ){
188 char *z = sqlite_mprintf("typeof:%s", azN[i]);
189 Tcl_SetVar2(cbData->interp, cbData->zArray, z, azN[i+nCol],
190 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
191 sqlite_freemem(z);
192 }
drh6a535342001-10-19 16:44:56 +0000193 }
194 cbData->once = 0;
195 }
196 if( azCol!=0 ){
197 if( cbData->zArray[0] ){
drh75897232000-05-29 14:26:00 +0000198 for(i=0; i<nCol; i++){
drh6a535342001-10-19 16:44:56 +0000199 char *z = azCol[i];
200 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000201 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
drh6a535342001-10-19 16:44:56 +0000202 }
203 }else{
204 for(i=0; i<nCol; i++){
205 char *z = azCol[i];
206 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000207 Tcl_SetVar(cbData->interp, azN[i], z, 0);
drh75897232000-05-29 14:26:00 +0000208 }
209 }
drh75897232000-05-29 14:26:00 +0000210 }
drh6d313162000-09-21 13:01:35 +0000211 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
drh960e8c62001-04-03 16:53:21 +0000212 if( rc==TCL_CONTINUE ) rc = TCL_OK;
213 cbData->tcl_rc = rc;
214 return rc!=TCL_OK;
drh75897232000-05-29 14:26:00 +0000215}
drh6d4abfb2001-10-22 02:58:08 +0000216#endif
drh75897232000-05-29 14:26:00 +0000217
218/*
drh6d313162000-09-21 13:01:35 +0000219** This is an alternative callback for database queries. Instead
220** of invoking a TCL script to handle the result, this callback just
221** appends each column of the result to a list. After the query
222** is complete, the list is returned.
223*/
224static int DbEvalCallback2(
225 void *clientData, /* An instance of CallbackData */
226 int nCol, /* Number of columns in the result */
227 char ** azCol, /* Data for each column */
228 char ** azN /* Name for each column */
229){
230 Tcl_Obj *pList = (Tcl_Obj*)clientData;
231 int i;
drh6a535342001-10-19 16:44:56 +0000232 if( azCol==0 ) return 0;
drh6d313162000-09-21 13:01:35 +0000233 for(i=0; i<nCol; i++){
234 Tcl_Obj *pElem;
235 if( azCol[i] && *azCol[i] ){
drh297ecf12001-04-05 15:57:13 +0000236#ifdef UTF_TRANSLATION_NEEDED
237 Tcl_DString dCol;
238 Tcl_DStringInit(&dCol);
239 Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol);
240 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
241 Tcl_DStringFree(&dCol);
242#else
drh6d313162000-09-21 13:01:35 +0000243 pElem = Tcl_NewStringObj(azCol[i], -1);
drh297ecf12001-04-05 15:57:13 +0000244#endif
drh6d313162000-09-21 13:01:35 +0000245 }else{
246 pElem = Tcl_NewObj();
247 }
248 Tcl_ListObjAppendElement(0, pList, pElem);
249 }
250 return 0;
251}
252
253/*
drh5d9d7572003-08-19 14:31:01 +0000254** This is a second alternative callback for database queries. A the
255** first column of the first row of the result is made the TCL result.
256*/
257static int DbEvalCallback3(
258 void *clientData, /* An instance of CallbackData */
259 int nCol, /* Number of columns in the result */
260 char ** azCol, /* Data for each column */
261 char ** azN /* Name for each column */
262){
263 Tcl_Interp *interp = (Tcl_Interp*)clientData;
264 Tcl_Obj *pElem;
265 if( azCol==0 ) return 1;
266 if( nCol==0 ) return 1;
267#ifdef UTF_TRANSLATION_NEEDED
268 {
269 Tcl_DString dCol;
270 Tcl_DStringInit(&dCol);
271 Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol);
272 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
273 Tcl_DStringFree(&dCol);
274 }
275#else
276 pElem = Tcl_NewStringObj(azCol[0], -1);
277#endif
278 Tcl_SetObjResult(interp, pElem);
279 return 1;
280}
281
282/*
drh75897232000-05-29 14:26:00 +0000283** Called when the command is deleted.
284*/
285static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000286 SqliteDb *pDb = (SqliteDb*)db;
287 sqlite_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000288 while( pDb->pFunc ){
289 SqlFunc *pFunc = pDb->pFunc;
290 pDb->pFunc = pFunc->pNext;
291 Tcl_Free((char*)pFunc);
292 }
drhbec3f402000-08-04 13:49:02 +0000293 if( pDb->zBusy ){
294 Tcl_Free(pDb->zBusy);
295 }
drhb5a20d32003-04-23 12:25:23 +0000296 if( pDb->zTrace ){
297 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000298 }
drhe22a3342003-04-22 20:30:37 +0000299 if( pDb->zAuth ){
300 Tcl_Free(pDb->zAuth);
301 }
drhbec3f402000-08-04 13:49:02 +0000302 Tcl_Free((char*)pDb);
303}
304
305/*
306** This routine is called when a database file is locked while trying
307** to execute SQL.
308*/
309static int DbBusyHandler(void *cd, const char *zTable, int nTries){
310 SqliteDb *pDb = (SqliteDb*)cd;
311 int rc;
312 char zVal[30];
313 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000314 Tcl_DString cmd;
315
316 Tcl_DStringInit(&cmd);
317 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
318 Tcl_DStringAppendElement(&cmd, zTable);
319 sprintf(zVal, " %d", nTries);
320 Tcl_DStringAppend(&cmd, zVal, -1);
321 zCmd = Tcl_DStringValue(&cmd);
322 rc = Tcl_Eval(pDb->interp, zCmd);
323 Tcl_DStringFree(&cmd);
324 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
325 return 0;
326 }
327 return 1;
drh75897232000-05-29 14:26:00 +0000328}
329
330/*
danielk1977348bb5d2003-10-18 09:37:26 +0000331** This routine is invoked as the 'progress callback' for the database.
332*/
333static int DbProgressHandler(void *cd){
334 SqliteDb *pDb = (SqliteDb*)cd;
335 int rc;
336
337 assert( pDb->zProgress );
338 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
339 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
340 return 1;
341 }
342 return 0;
343}
344
345/*
drhb5a20d32003-04-23 12:25:23 +0000346** This routine is called by the SQLite trace handler whenever a new
347** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000348*/
drhb5a20d32003-04-23 12:25:23 +0000349static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000350 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000351 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000352
drhb5a20d32003-04-23 12:25:23 +0000353 Tcl_DStringInit(&str);
354 Tcl_DStringAppend(&str, pDb->zTrace, -1);
355 Tcl_DStringAppendElement(&str, zSql);
356 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
357 Tcl_DStringFree(&str);
358 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000359}
360
361/*
drhaa940ea2004-01-15 02:44:03 +0000362** This routine is called when a transaction is committed. The
363** TCL script in pDb->zCommit is executed. If it returns non-zero or
364** if it throws an exception, the transaction is rolled back instead
365** of being committed.
366*/
367static int DbCommitHandler(void *cd){
368 SqliteDb *pDb = (SqliteDb*)cd;
369 int rc;
370
371 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
372 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
373 return 1;
374 }
375 return 0;
376}
377
378/*
drhcabb0812002-09-14 13:47:32 +0000379** This routine is called to evaluate an SQL function implemented
380** using TCL script.
381*/
382static void tclSqlFunc(sqlite_func *context, int argc, const char **argv){
383 SqlFunc *p = sqlite_user_data(context);
384 Tcl_DString cmd;
385 int i;
386 int rc;
387
388 Tcl_DStringInit(&cmd);
389 Tcl_DStringAppend(&cmd, p->zScript, -1);
390 for(i=0; i<argc; i++){
391 Tcl_DStringAppendElement(&cmd, argv[i] ? argv[i] : "");
392 }
393 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
394 if( rc ){
395 sqlite_set_result_error(context, Tcl_GetStringResult(p->interp), -1);
396 }else{
397 sqlite_set_result_string(context, Tcl_GetStringResult(p->interp), -1);
398 }
399}
drhe22a3342003-04-22 20:30:37 +0000400#ifndef SQLITE_OMIT_AUTHORIZATION
401/*
402** This is the authentication function. It appends the authentication
403** type code and the two arguments to zCmd[] then invokes the result
404** on the interpreter. The reply is examined to determine if the
405** authentication fails or succeeds.
406*/
407static int auth_callback(
408 void *pArg,
409 int code,
410 const char *zArg1,
411 const char *zArg2,
412 const char *zArg3,
413 const char *zArg4
414){
415 char *zCode;
416 Tcl_DString str;
417 int rc;
418 const char *zReply;
419 SqliteDb *pDb = (SqliteDb*)pArg;
420
421 switch( code ){
422 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
423 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
424 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
425 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
426 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
427 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
428 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
429 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
430 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
431 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
432 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
433 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
434 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
435 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
436 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
437 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
438 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
439 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
440 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
441 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
442 case SQLITE_READ : zCode="SQLITE_READ"; break;
443 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
444 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
445 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000446 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
447 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
drhe22a3342003-04-22 20:30:37 +0000448 default : zCode="????"; break;
449 }
450 Tcl_DStringInit(&str);
451 Tcl_DStringAppend(&str, pDb->zAuth, -1);
452 Tcl_DStringAppendElement(&str, zCode);
453 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
454 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
455 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
456 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
457 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
458 Tcl_DStringFree(&str);
459 zReply = Tcl_GetStringResult(pDb->interp);
460 if( strcmp(zReply,"SQLITE_OK")==0 ){
461 rc = SQLITE_OK;
462 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
463 rc = SQLITE_DENY;
464 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
465 rc = SQLITE_IGNORE;
466 }else{
467 rc = 999;
468 }
469 return rc;
470}
471#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000472
473/*
drh75897232000-05-29 14:26:00 +0000474** The "sqlite" command below creates a new Tcl command for each
475** connection it opens to an SQLite database. This routine is invoked
476** whenever one of those connection-specific commands is executed
477** in Tcl. For example, if you run Tcl code like this:
478**
479** sqlite db1 "my_database"
480** db1 close
481**
482** The first command opens a connection to the "my_database" database
483** and calls that connection "db1". The second command causes this
484** subroutine to be invoked.
485*/
drh6d313162000-09-21 13:01:35 +0000486static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000487 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000488 int choice;
drh0de8c112002-07-06 16:32:14 +0000489 static const char *DB_strs[] = {
drhb5a20d32003-04-23 12:25:23 +0000490 "authorizer", "busy", "changes",
drhaa940ea2004-01-15 02:44:03 +0000491 "close", "commit_hook", "complete",
492 "errorcode", "eval", "function",
493 "last_insert_rowid", "onecolumn", "progress",
494 "timeout", "trace", 0
drh6d313162000-09-21 13:01:35 +0000495 };
drh411995d2002-06-25 19:31:18 +0000496 enum DB_enum {
drhb5a20d32003-04-23 12:25:23 +0000497 DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
drhaa940ea2004-01-15 02:44:03 +0000498 DB_CLOSE, DB_COMMIT_HOOK, DB_COMPLETE,
499 DB_ERRORCODE, DB_EVAL, DB_FUNCTION,
500 DB_LAST_INSERT_ROWID, DB_ONECOLUMN, DB_PROGRESS,
501 DB_TIMEOUT, DB_TRACE,
drh6d313162000-09-21 13:01:35 +0000502 };
503
504 if( objc<2 ){
505 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000506 return TCL_ERROR;
507 }
drh411995d2002-06-25 19:31:18 +0000508 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000509 return TCL_ERROR;
510 }
511
drh411995d2002-06-25 19:31:18 +0000512 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000513
drhe22a3342003-04-22 20:30:37 +0000514 /* $db authorizer ?CALLBACK?
515 **
516 ** Invoke the given callback to authorize each SQL operation as it is
517 ** compiled. 5 arguments are appended to the callback before it is
518 ** invoked:
519 **
520 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
521 ** (2) First descriptive name (depends on authorization type)
522 ** (3) Second descriptive name
523 ** (4) Name of the database (ex: "main", "temp")
524 ** (5) Name of trigger that is doing the access
525 **
526 ** The callback should return on of the following strings: SQLITE_OK,
527 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
528 **
529 ** If this method is invoked with no arguments, the current authorization
530 ** callback string is returned.
531 */
532 case DB_AUTHORIZER: {
533 if( objc>3 ){
534 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
535 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000536 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000537 Tcl_AppendResult(interp, pDb->zAuth, 0);
538 }
539 }else{
540 char *zAuth;
541 int len;
542 if( pDb->zAuth ){
543 Tcl_Free(pDb->zAuth);
544 }
545 zAuth = Tcl_GetStringFromObj(objv[2], &len);
546 if( zAuth && len>0 ){
547 pDb->zAuth = Tcl_Alloc( len + 1 );
548 strcpy(pDb->zAuth, zAuth);
549 }else{
550 pDb->zAuth = 0;
551 }
552#ifndef SQLITE_OMIT_AUTHORIZATION
553 if( pDb->zAuth ){
554 pDb->interp = interp;
555 sqlite_set_authorizer(pDb->db, auth_callback, pDb);
556 }else{
557 sqlite_set_authorizer(pDb->db, 0, 0);
558 }
559#endif
560 }
561 break;
562 }
563
drhbec3f402000-08-04 13:49:02 +0000564 /* $db busy ?CALLBACK?
565 **
566 ** Invoke the given callback if an SQL statement attempts to open
567 ** a locked database file.
568 */
drh6d313162000-09-21 13:01:35 +0000569 case DB_BUSY: {
570 if( objc>3 ){
571 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000572 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000573 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000574 if( pDb->zBusy ){
575 Tcl_AppendResult(interp, pDb->zBusy, 0);
576 }
577 }else{
drh6d313162000-09-21 13:01:35 +0000578 char *zBusy;
579 int len;
drhbec3f402000-08-04 13:49:02 +0000580 if( pDb->zBusy ){
581 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000582 }
drh6d313162000-09-21 13:01:35 +0000583 zBusy = Tcl_GetStringFromObj(objv[2], &len);
584 if( zBusy && len>0 ){
585 pDb->zBusy = Tcl_Alloc( len + 1 );
586 strcpy(pDb->zBusy, zBusy);
587 }else{
588 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000589 }
590 if( pDb->zBusy ){
591 pDb->interp = interp;
592 sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000593 }else{
594 sqlite_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000595 }
596 }
drh6d313162000-09-21 13:01:35 +0000597 break;
598 }
drhbec3f402000-08-04 13:49:02 +0000599
danielk1977348bb5d2003-10-18 09:37:26 +0000600 /* $db progress ?N CALLBACK?
601 **
602 ** Invoke the given callback every N virtual machine opcodes while executing
603 ** queries.
604 */
605 case DB_PROGRESS: {
606 if( objc==2 ){
607 if( pDb->zProgress ){
608 Tcl_AppendResult(interp, pDb->zProgress, 0);
609 }
610 }else if( objc==4 ){
611 char *zProgress;
612 int len;
613 int N;
614 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
615 return TCL_ERROR;
616 };
617 if( pDb->zProgress ){
618 Tcl_Free(pDb->zProgress);
619 }
620 zProgress = Tcl_GetStringFromObj(objv[3], &len);
621 if( zProgress && len>0 ){
622 pDb->zProgress = Tcl_Alloc( len + 1 );
623 strcpy(pDb->zProgress, zProgress);
624 }else{
625 pDb->zProgress = 0;
626 }
627#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
628 if( pDb->zProgress ){
629 pDb->interp = interp;
630 sqlite_progress_handler(pDb->db, N, DbProgressHandler, pDb);
631 }else{
632 sqlite_progress_handler(pDb->db, 0, 0, 0);
633 }
634#endif
635 }else{
636 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
637 return TCL_ERROR;
638 }
639 break;
640 }
641
drhc8d30ac2002-04-12 10:08:59 +0000642 /*
643 ** $db changes
644 **
645 ** Return the number of rows that were modified, inserted, or deleted by
646 ** the most recent "eval".
647 */
648 case DB_CHANGES: {
649 Tcl_Obj *pResult;
650 int nChange;
651 if( objc!=2 ){
652 Tcl_WrongNumArgs(interp, 2, objv, "");
653 return TCL_ERROR;
654 }
655 nChange = sqlite_changes(pDb->db);
656 pResult = Tcl_GetObjResult(interp);
657 Tcl_SetIntObj(pResult, nChange);
658 break;
659 }
660
drh75897232000-05-29 14:26:00 +0000661 /* $db close
662 **
663 ** Shutdown the database
664 */
drh6d313162000-09-21 13:01:35 +0000665 case DB_CLOSE: {
666 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
667 break;
668 }
drh75897232000-05-29 14:26:00 +0000669
drhaa940ea2004-01-15 02:44:03 +0000670 /* $db commit_hook ?CALLBACK?
671 **
672 ** Invoke the given callback just before committing every SQL transaction.
673 ** If the callback throws an exception or returns non-zero, then the
674 ** transaction is aborted. If CALLBACK is an empty string, the callback
675 ** is disabled.
676 */
677 case DB_COMMIT_HOOK: {
678 if( objc>3 ){
679 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
680 }else if( objc==2 ){
681 if( pDb->zCommit ){
682 Tcl_AppendResult(interp, pDb->zCommit, 0);
683 }
684 }else{
685 char *zCommit;
686 int len;
687 if( pDb->zCommit ){
688 Tcl_Free(pDb->zCommit);
689 }
690 zCommit = Tcl_GetStringFromObj(objv[2], &len);
691 if( zCommit && len>0 ){
692 pDb->zCommit = Tcl_Alloc( len + 1 );
693 strcpy(pDb->zCommit, zCommit);
694 }else{
695 pDb->zCommit = 0;
696 }
697 if( pDb->zCommit ){
698 pDb->interp = interp;
699 sqlite_commit_hook(pDb->db, DbCommitHandler, pDb);
700 }else{
701 sqlite_commit_hook(pDb->db, 0, 0);
702 }
703 }
704 break;
705 }
706
drh75897232000-05-29 14:26:00 +0000707 /* $db complete SQL
708 **
709 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
710 ** additional lines of input are needed. This is similar to the
711 ** built-in "info complete" command of Tcl.
712 */
drh6d313162000-09-21 13:01:35 +0000713 case DB_COMPLETE: {
714 Tcl_Obj *pResult;
715 int isComplete;
716 if( objc!=3 ){
717 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000718 return TCL_ERROR;
719 }
drh6d313162000-09-21 13:01:35 +0000720 isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
721 pResult = Tcl_GetObjResult(interp);
722 Tcl_SetBooleanObj(pResult, isComplete);
723 break;
724 }
drhdcd997e2003-01-31 17:21:49 +0000725
726 /*
727 ** $db errorcode
728 **
729 ** Return the numeric error code that was returned by the most recent
730 ** call to sqlite_exec().
731 */
732 case DB_ERRORCODE: {
733 Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc));
734 break;
735 }
drh75897232000-05-29 14:26:00 +0000736
737 /*
738 ** $db eval $sql ?array { ...code... }?
739 **
740 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000741 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000742 ** If "array" and "code" are omitted, then no callback is every invoked.
743 ** If "array" is an empty string, then the values are placed in variables
744 ** that have the same name as the fields extracted by the query.
745 */
drh6d313162000-09-21 13:01:35 +0000746 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000747 CallbackData cbData;
748 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000749 char *zSql;
drh75897232000-05-29 14:26:00 +0000750 int rc;
drh297ecf12001-04-05 15:57:13 +0000751#ifdef UTF_TRANSLATION_NEEDED
752 Tcl_DString dSql;
drh6d4abfb2001-10-22 02:58:08 +0000753 int i;
drh297ecf12001-04-05 15:57:13 +0000754#endif
drh75897232000-05-29 14:26:00 +0000755
drh6d313162000-09-21 13:01:35 +0000756 if( objc!=5 && objc!=3 ){
757 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000758 return TCL_ERROR;
759 }
drhbec3f402000-08-04 13:49:02 +0000760 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000761 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh297ecf12001-04-05 15:57:13 +0000762#ifdef UTF_TRANSLATION_NEEDED
763 Tcl_DStringInit(&dSql);
764 Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
765 zSql = Tcl_DStringValue(&dSql);
766#endif
drh6d313162000-09-21 13:01:35 +0000767 Tcl_IncrRefCount(objv[2]);
768 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000769 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000770 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000771 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
772 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000773 cbData.tcl_rc = TCL_OK;
drh6d4abfb2001-10-22 02:58:08 +0000774 cbData.nColName = 0;
775 cbData.azColName = 0;
drh75897232000-05-29 14:26:00 +0000776 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000777 Tcl_IncrRefCount(objv[3]);
778 Tcl_IncrRefCount(objv[4]);
779 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
780 Tcl_DecrRefCount(objv[4]);
781 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000782 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000783 }else{
drh6d313162000-09-21 13:01:35 +0000784 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000785 cbData.tcl_rc = TCL_OK;
drh6d313162000-09-21 13:01:35 +0000786 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
787 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000788 }
drhdcd997e2003-01-31 17:21:49 +0000789 pDb->rc = rc;
drhb798fa62002-09-03 19:43:23 +0000790 if( rc==SQLITE_ABORT ){
791 if( zErrMsg ) free(zErrMsg);
792 rc = cbData.tcl_rc;
793 }else if( zErrMsg ){
drh75897232000-05-29 14:26:00 +0000794 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
795 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000796 rc = TCL_ERROR;
drhb798fa62002-09-03 19:43:23 +0000797 }else if( rc!=SQLITE_OK ){
drh6d4abfb2001-10-22 02:58:08 +0000798 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
799 rc = TCL_ERROR;
drh960e8c62001-04-03 16:53:21 +0000800 }else{
drh75897232000-05-29 14:26:00 +0000801 }
drh6d313162000-09-21 13:01:35 +0000802 Tcl_DecrRefCount(objv[2]);
drh297ecf12001-04-05 15:57:13 +0000803#ifdef UTF_TRANSLATION_NEEDED
804 Tcl_DStringFree(&dSql);
drh6d4abfb2001-10-22 02:58:08 +0000805 if( objc==5 && cbData.azColName ){
806 for(i=0; i<cbData.nColName; i++){
807 if( cbData.azColName[i] ) free(cbData.azColName[i]);
808 }
809 free(cbData.azColName);
drhce927062001-11-09 13:41:09 +0000810 cbData.azColName = 0;
drh6d4abfb2001-10-22 02:58:08 +0000811 }
drh297ecf12001-04-05 15:57:13 +0000812#endif
drh75897232000-05-29 14:26:00 +0000813 return rc;
drh6d313162000-09-21 13:01:35 +0000814 }
drhbec3f402000-08-04 13:49:02 +0000815
816 /*
drhcabb0812002-09-14 13:47:32 +0000817 ** $db function NAME SCRIPT
818 **
819 ** Create a new SQL function called NAME. Whenever that function is
820 ** called, invoke SCRIPT to evaluate the function.
821 */
822 case DB_FUNCTION: {
823 SqlFunc *pFunc;
824 char *zName;
825 char *zScript;
826 int nScript;
827 if( objc!=4 ){
828 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
829 return TCL_ERROR;
830 }
831 zName = Tcl_GetStringFromObj(objv[2], 0);
832 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
833 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
834 if( pFunc==0 ) return TCL_ERROR;
835 pFunc->interp = interp;
836 pFunc->pNext = pDb->pFunc;
837 pFunc->zScript = (char*)&pFunc[1];
838 strcpy(pFunc->zScript, zScript);
839 sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc);
840 sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC);
841 break;
842 }
843
844 /*
drhaf9ff332002-01-16 21:00:27 +0000845 ** $db last_insert_rowid
846 **
847 ** Return an integer which is the ROWID for the most recent insert.
848 */
849 case DB_LAST_INSERT_ROWID: {
850 Tcl_Obj *pResult;
851 int rowid;
852 if( objc!=2 ){
853 Tcl_WrongNumArgs(interp, 2, objv, "");
854 return TCL_ERROR;
855 }
856 rowid = sqlite_last_insert_rowid(pDb->db);
857 pResult = Tcl_GetObjResult(interp);
858 Tcl_SetIntObj(pResult, rowid);
859 break;
860 }
861
862 /*
drh5d9d7572003-08-19 14:31:01 +0000863 ** $db onecolumn SQL
864 **
865 ** Return a single column from a single row of the given SQL query.
866 */
867 case DB_ONECOLUMN: {
868 int rc;
869 char *zSql;
870 char *zErrMsg = 0;
871 if( objc!=3 ){
872 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
873 return TCL_ERROR;
874 }
875 zSql = Tcl_GetStringFromObj(objv[2], 0);
876 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
877 if( rc==SQLITE_ABORT ){
878 /* Do nothing. This is normal. */
879 }else if( zErrMsg ){
880 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
881 free(zErrMsg);
882 rc = TCL_ERROR;
883 }else if( rc!=SQLITE_OK ){
884 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
885 rc = TCL_ERROR;
886 }
887 break;
888 }
889
890 /*
drhbec3f402000-08-04 13:49:02 +0000891 ** $db timeout MILLESECONDS
892 **
893 ** Delay for the number of milliseconds specified when a file is locked.
894 */
drh6d313162000-09-21 13:01:35 +0000895 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000896 int ms;
drh6d313162000-09-21 13:01:35 +0000897 if( objc!=3 ){
898 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000899 return TCL_ERROR;
900 }
drh6d313162000-09-21 13:01:35 +0000901 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
drhbec3f402000-08-04 13:49:02 +0000902 sqlite_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000903 break;
drh75897232000-05-29 14:26:00 +0000904 }
drhb5a20d32003-04-23 12:25:23 +0000905
906 /* $db trace ?CALLBACK?
907 **
908 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
909 ** that is executed. The text of the SQL is appended to CALLBACK before
910 ** it is executed.
911 */
912 case DB_TRACE: {
913 if( objc>3 ){
914 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
915 }else if( objc==2 ){
916 if( pDb->zTrace ){
917 Tcl_AppendResult(interp, pDb->zTrace, 0);
918 }
919 }else{
920 char *zTrace;
921 int len;
922 if( pDb->zTrace ){
923 Tcl_Free(pDb->zTrace);
924 }
925 zTrace = Tcl_GetStringFromObj(objv[2], &len);
926 if( zTrace && len>0 ){
927 pDb->zTrace = Tcl_Alloc( len + 1 );
928 strcpy(pDb->zTrace, zTrace);
929 }else{
930 pDb->zTrace = 0;
931 }
932 if( pDb->zTrace ){
933 pDb->interp = interp;
934 sqlite_trace(pDb->db, DbTraceHandler, pDb);
935 }else{
936 sqlite_trace(pDb->db, 0, 0);
937 }
938 }
939 break;
940 }
941
drh6d313162000-09-21 13:01:35 +0000942 } /* End of the SWITCH statement */
drh75897232000-05-29 14:26:00 +0000943 return TCL_OK;
944}
945
946/*
947** sqlite DBNAME FILENAME ?MODE?
948**
949** This is the main Tcl command. When the "sqlite" Tcl command is
950** invoked, this routine runs to process that command.
951**
952** The first argument, DBNAME, is an arbitrary name for a new
953** database connection. This command creates a new command named
954** DBNAME that is used to control that connection. The database
955** connection is deleted when the DBNAME command is deleted.
956**
957** The second argument is the name of the directory that contains
958** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000959**
960** For testing purposes, we also support the following:
961**
962** sqlite -encoding
963**
964** Return the encoding used by LIKE and GLOB operators. Choices
965** are UTF-8 and iso8859.
966**
drh647cb0e2002-11-04 19:32:25 +0000967** sqlite -version
968**
969** Return the version number of the SQLite library.
970**
drhfbc3eab2001-04-06 16:13:42 +0000971** sqlite -tcl-uses-utf
972**
973** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
974** not. Used by tests to make sure the library was compiled
975** correctly.
drh75897232000-05-29 14:26:00 +0000976*/
977static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){
978 int mode;
drhbec3f402000-08-04 13:49:02 +0000979 SqliteDb *p;
drh75897232000-05-29 14:26:00 +0000980 char *zErrMsg;
drh06b27182002-06-26 20:06:05 +0000981 char zBuf[80];
drhfbc3eab2001-04-06 16:13:42 +0000982 if( argc==2 ){
983 if( strcmp(argv[1],"-encoding")==0 ){
984 Tcl_AppendResult(interp,sqlite_encoding,0);
985 return TCL_OK;
986 }
drh647cb0e2002-11-04 19:32:25 +0000987 if( strcmp(argv[1],"-version")==0 ){
988 Tcl_AppendResult(interp,sqlite_version,0);
989 return TCL_OK;
990 }
drhfbc3eab2001-04-06 16:13:42 +0000991 if( strcmp(argv[1],"-tcl-uses-utf")==0 ){
992#ifdef TCL_UTF_MAX
993 Tcl_AppendResult(interp,"1",0);
994#else
995 Tcl_AppendResult(interp,"0",0);
996#endif
997 return TCL_OK;
998 }
999 }
drh75897232000-05-29 14:26:00 +00001000 if( argc!=3 && argc!=4 ){
1001 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
1002 " HANDLE FILENAME ?MODE?\"", 0);
1003 return TCL_ERROR;
1004 }
1005 if( argc==3 ){
drh58b95762000-06-02 01:17:37 +00001006 mode = 0666;
drh75897232000-05-29 14:26:00 +00001007 }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){
1008 return TCL_ERROR;
1009 }
1010 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001011 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001012 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001013 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1014 return TCL_ERROR;
1015 }
1016 memset(p, 0, sizeof(*p));
1017 p->db = sqlite_open(argv[2], mode, &zErrMsg);
1018 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001019 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001020 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001021 free(zErrMsg);
1022 return TCL_ERROR;
1023 }
drh6d313162000-09-21 13:01:35 +00001024 Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001025
drh06b27182002-06-26 20:06:05 +00001026 /* The return value is the value of the sqlite* pointer
1027 */
1028 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001029 if( strncmp(zBuf,"0x",2) ){
1030 sprintf(zBuf, "0x%p", p->db);
1031 }
drh06b27182002-06-26 20:06:05 +00001032 Tcl_AppendResult(interp, zBuf, 0);
1033
drhc22bd472002-05-10 13:14:07 +00001034 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001035 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001036 */
drh28b4e482002-03-11 02:06:13 +00001037#ifdef SQLITE_TEST
1038 {
drhc22bd472002-05-10 13:14:07 +00001039 extern void Md5_Register(sqlite*);
1040 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +00001041 }
drh28b4e482002-03-11 02:06:13 +00001042#endif
drh75897232000-05-29 14:26:00 +00001043 return TCL_OK;
1044}
1045
1046/*
drh90ca9752001-09-28 17:47:14 +00001047** Provide a dummy Tcl_InitStubs if we are using this as a static
1048** library.
1049*/
1050#ifndef USE_TCL_STUBS
1051# undef Tcl_InitStubs
1052# define Tcl_InitStubs(a,b,c)
1053#endif
1054
1055/*
drh75897232000-05-29 14:26:00 +00001056** Initialize this module.
1057**
1058** This Tcl module contains only a single new Tcl command named "sqlite".
1059** (Hence there is no namespace. There is no point in using a namespace
1060** if the extension only supplies one new name!) The "sqlite" command is
1061** used to open a new SQLite database. See the DbMain() routine above
1062** for additional information.
1063*/
1064int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001065 Tcl_InitStubs(interp, "8.0", 0);
drhc2eef3b2002-08-31 18:53:06 +00001066 Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001067 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh90ca9752001-09-28 17:47:14 +00001068 return TCL_OK;
1069}
1070int Tclsqlite_Init(Tcl_Interp *interp){
1071 Tcl_InitStubs(interp, "8.0", 0);
drhc2eef3b2002-08-31 18:53:06 +00001072 Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001073 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh75897232000-05-29 14:26:00 +00001074 return TCL_OK;
1075}
1076int Sqlite_SafeInit(Tcl_Interp *interp){
1077 return TCL_OK;
1078}
drh90ca9752001-09-28 17:47:14 +00001079int Tclsqlite_SafeInit(Tcl_Interp *interp){
1080 return TCL_OK;
1081}
drh75897232000-05-29 14:26:00 +00001082
drh3cebbde2000-10-19 14:59:27 +00001083#if 0
drh75897232000-05-29 14:26:00 +00001084/*
1085** If compiled using mktclapp, this routine runs to initialize
1086** everything.
1087*/
1088int Et_AppInit(Tcl_Interp *interp){
1089 return Sqlite_Init(interp);
1090}
drh3cebbde2000-10-19 14:59:27 +00001091#endif
drh348784e2000-05-29 20:41:49 +00001092
1093/*
1094** If the macro TCLSH is defined and is one, then put in code for the
1095** "main" routine that will initialize Tcl.
1096*/
1097#if defined(TCLSH) && TCLSH==1
1098static char zMainloop[] =
1099 "set line {}\n"
1100 "while {![eof stdin]} {\n"
1101 "if {$line!=\"\"} {\n"
1102 "puts -nonewline \"> \"\n"
1103 "} else {\n"
1104 "puts -nonewline \"% \"\n"
1105 "}\n"
1106 "flush stdout\n"
1107 "append line [gets stdin]\n"
1108 "if {[info complete $line]} {\n"
1109 "if {[catch {uplevel #0 $line} result]} {\n"
1110 "puts stderr \"Error: $result\"\n"
1111 "} elseif {$result!=\"\"} {\n"
1112 "puts $result\n"
1113 "}\n"
1114 "set line {}\n"
1115 "} else {\n"
1116 "append line \\n\n"
1117 "}\n"
1118 "}\n"
1119;
1120
1121#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1122int TCLSH_MAIN(int argc, char **argv){
1123 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001124 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001125 interp = Tcl_CreateInterp();
1126 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001127#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001128 {
1129 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001130 extern int Sqlitetest2_Init(Tcl_Interp*);
1131 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001132 extern int Sqlitetest4_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001133 extern int Md5_Init(Tcl_Interp*);
drhd1bf3512001-04-07 15:24:33 +00001134 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001135 Sqlitetest2_Init(interp);
1136 Sqlitetest3_Init(interp);
drha6064dc2003-12-19 02:52:05 +00001137 Sqlitetest4_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001138 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001139 }
1140#endif
drh348784e2000-05-29 20:41:49 +00001141 if( argc>=2 ){
1142 int i;
1143 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1144 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1145 for(i=2; i<argc; i++){
1146 Tcl_SetVar(interp, "argv", argv[i],
1147 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1148 }
1149 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001150 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001151 if( zInfo==0 ) zInfo = interp->result;
1152 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001153 return 1;
1154 }
1155 }else{
1156 Tcl_GlobalEval(interp, zMainloop);
1157 }
1158 return 0;
1159}
1160#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001161
1162#endif /* !defined(NO_TCL) */