blob: 9037bdac6652c6f4162dd62bd2d6fd2182a12a72 [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**
drhcabb0812002-09-14 13:47:32 +000014** $Id: tclsqlite.c,v 1.42 2002/09/14 13:47:32 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 */
drhcabb0812002-09-14 13:47:32 +000054 SqlFunc *pFunc; /* List of SQL functions */
drhbec3f402000-08-04 13:49:02 +000055};
56
57/*
drh75897232000-05-29 14:26:00 +000058** An instance of this structure passes information thru the sqlite
59** logic from the original TCL command into the callback routine.
60*/
61typedef struct CallbackData CallbackData;
62struct CallbackData {
63 Tcl_Interp *interp; /* The TCL interpreter */
64 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000065 Tcl_Obj *pCode; /* The code to execute for each row */
drhce927062001-11-09 13:41:09 +000066 int once; /* Set for first callback only */
drh960e8c62001-04-03 16:53:21 +000067 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000068 int nColName; /* Number of entries in the azColName[] array */
69 char **azColName; /* Column names translated to UTF-8 */
drh98808ba2001-10-18 12:34:46 +000070};
drh297ecf12001-04-05 15:57:13 +000071
drh6d4abfb2001-10-22 02:58:08 +000072#ifdef UTF_TRANSLATION_NEEDED
drh297ecf12001-04-05 15:57:13 +000073/*
drh75897232000-05-29 14:26:00 +000074** Called for each row of the result.
drh6d4abfb2001-10-22 02:58:08 +000075**
76** This version is used when TCL expects UTF-8 data but the database
77** uses the ISO8859 format. A translation must occur from ISO8859 into
78** UTF-8.
drh75897232000-05-29 14:26:00 +000079*/
80static int DbEvalCallback(
81 void *clientData, /* An instance of CallbackData */
82 int nCol, /* Number of columns in the result */
83 char ** azCol, /* Data for each column */
84 char ** azN /* Name for each column */
85){
86 CallbackData *cbData = (CallbackData*)clientData;
87 int i, rc;
drh297ecf12001-04-05 15:57:13 +000088 Tcl_DString dCol;
drh6d4abfb2001-10-22 02:58:08 +000089 Tcl_DStringInit(&dCol);
drhce927062001-11-09 13:41:09 +000090 if( cbData->azColName==0 ){
91 assert( cbData->once );
92 cbData->once = 0;
93 if( cbData->zArray[0] ){
94 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
drh6d4abfb2001-10-22 02:58:08 +000095 }
drhce927062001-11-09 13:41:09 +000096 cbData->azColName = malloc( nCol*sizeof(char*) );
97 if( cbData->azColName==0 ){ return 1; }
drh6d4abfb2001-10-22 02:58:08 +000098 cbData->nColName = nCol;
99 for(i=0; i<nCol; i++){
100 Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol);
drhce927062001-11-09 13:41:09 +0000101 cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 );
102 if( cbData->azColName[i] ){
103 strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol));
104 }else{
105 return 1;
drh6d4abfb2001-10-22 02:58:08 +0000106 }
drhce927062001-11-09 13:41:09 +0000107 if( cbData->zArray[0] ){
108 Tcl_SetVar2(cbData->interp, cbData->zArray, "*",
109 Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh704027f2002-07-15 20:58:47 +0000110 if( azN[nCol]!=0 ){
drh5080aaa2002-07-11 12:18:16 +0000111 Tcl_DString dType;
112 Tcl_DStringInit(&dType);
113 Tcl_DStringAppend(&dType, "typeof:", -1);
114 Tcl_DStringAppend(&dType, Tcl_DStringValue(&dCol), -1);
115 Tcl_DStringFree(&dCol);
116 Tcl_ExternalToUtfDString(NULL, azN[i+nCol], -1, &dCol);
117 Tcl_SetVar2(cbData->interp, cbData->zArray,
118 Tcl_DStringValue(&dType), Tcl_DStringValue(&dCol),
119 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
120 Tcl_DStringFree(&dType);
121 }
drhce927062001-11-09 13:41:09 +0000122 }
drhfa173a72002-07-10 21:26:00 +0000123
drh6d4abfb2001-10-22 02:58:08 +0000124 Tcl_DStringFree(&dCol);
125 }
drh6d4abfb2001-10-22 02:58:08 +0000126 }
127 if( azCol!=0 ){
128 if( cbData->zArray[0] ){
129 for(i=0; i<nCol; i++){
130 char *z = azCol[i];
131 if( z==0 ) z = "";
132 Tcl_DStringInit(&dCol);
133 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
134 Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i],
135 Tcl_DStringValue(&dCol), 0);
136 Tcl_DStringFree(&dCol);
137 }
138 }else{
139 for(i=0; i<nCol; i++){
140 char *z = azCol[i];
141 if( z==0 ) z = "";
142 Tcl_DStringInit(&dCol);
143 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
144 Tcl_SetVar(cbData->interp, cbData->azColName[i],
145 Tcl_DStringValue(&dCol), 0);
146 Tcl_DStringFree(&dCol);
147 }
148 }
149 }
150 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
151 if( rc==TCL_CONTINUE ) rc = TCL_OK;
152 cbData->tcl_rc = rc;
153 return rc!=TCL_OK;
154}
155#endif /* UTF_TRANSLATION_NEEDED */
156
157#ifndef UTF_TRANSLATION_NEEDED
158/*
159** Called for each row of the result.
160**
161** This version is used when either of the following is true:
162**
163** (1) This version of TCL uses UTF-8 and the data in the
164** SQLite database is already in the UTF-8 format.
165**
166** (2) This version of TCL uses ISO8859 and the data in the
167** SQLite database is already in the ISO8859 format.
168*/
169static int DbEvalCallback(
170 void *clientData, /* An instance of CallbackData */
171 int nCol, /* Number of columns in the result */
172 char ** azCol, /* Data for each column */
173 char ** azN /* Name for each column */
174){
175 CallbackData *cbData = (CallbackData*)clientData;
176 int i, rc;
drh6a535342001-10-19 16:44:56 +0000177 if( azCol==0 || (cbData->once && cbData->zArray[0]) ){
178 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
179 for(i=0; i<nCol; i++){
180 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
181 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh5080aaa2002-07-11 12:18:16 +0000182 if( azN[nCol] ){
183 char *z = sqlite_mprintf("typeof:%s", azN[i]);
184 Tcl_SetVar2(cbData->interp, cbData->zArray, z, azN[i+nCol],
185 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
186 sqlite_freemem(z);
187 }
drh6a535342001-10-19 16:44:56 +0000188 }
189 cbData->once = 0;
190 }
191 if( azCol!=0 ){
192 if( cbData->zArray[0] ){
drh75897232000-05-29 14:26:00 +0000193 for(i=0; i<nCol; i++){
drh6a535342001-10-19 16:44:56 +0000194 char *z = azCol[i];
195 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000196 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
drh6a535342001-10-19 16:44:56 +0000197 }
198 }else{
199 for(i=0; i<nCol; i++){
200 char *z = azCol[i];
201 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000202 Tcl_SetVar(cbData->interp, azN[i], z, 0);
drh75897232000-05-29 14:26:00 +0000203 }
204 }
drh75897232000-05-29 14:26:00 +0000205 }
drh6d313162000-09-21 13:01:35 +0000206 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
drh960e8c62001-04-03 16:53:21 +0000207 if( rc==TCL_CONTINUE ) rc = TCL_OK;
208 cbData->tcl_rc = rc;
209 return rc!=TCL_OK;
drh75897232000-05-29 14:26:00 +0000210}
drh6d4abfb2001-10-22 02:58:08 +0000211#endif
drh75897232000-05-29 14:26:00 +0000212
213/*
drh6d313162000-09-21 13:01:35 +0000214** This is an alternative callback for database queries. Instead
215** of invoking a TCL script to handle the result, this callback just
216** appends each column of the result to a list. After the query
217** is complete, the list is returned.
218*/
219static int DbEvalCallback2(
220 void *clientData, /* An instance of CallbackData */
221 int nCol, /* Number of columns in the result */
222 char ** azCol, /* Data for each column */
223 char ** azN /* Name for each column */
224){
225 Tcl_Obj *pList = (Tcl_Obj*)clientData;
226 int i;
drh6a535342001-10-19 16:44:56 +0000227 if( azCol==0 ) return 0;
drh6d313162000-09-21 13:01:35 +0000228 for(i=0; i<nCol; i++){
229 Tcl_Obj *pElem;
230 if( azCol[i] && *azCol[i] ){
drh297ecf12001-04-05 15:57:13 +0000231#ifdef UTF_TRANSLATION_NEEDED
232 Tcl_DString dCol;
233 Tcl_DStringInit(&dCol);
234 Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol);
235 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
236 Tcl_DStringFree(&dCol);
237#else
drh6d313162000-09-21 13:01:35 +0000238 pElem = Tcl_NewStringObj(azCol[i], -1);
drh297ecf12001-04-05 15:57:13 +0000239#endif
drh6d313162000-09-21 13:01:35 +0000240 }else{
241 pElem = Tcl_NewObj();
242 }
243 Tcl_ListObjAppendElement(0, pList, pElem);
244 }
245 return 0;
246}
247
248/*
drh75897232000-05-29 14:26:00 +0000249** Called when the command is deleted.
250*/
251static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000252 SqliteDb *pDb = (SqliteDb*)db;
253 sqlite_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000254 while( pDb->pFunc ){
255 SqlFunc *pFunc = pDb->pFunc;
256 pDb->pFunc = pFunc->pNext;
257 Tcl_Free((char*)pFunc);
258 }
drhbec3f402000-08-04 13:49:02 +0000259 if( pDb->zBusy ){
260 Tcl_Free(pDb->zBusy);
261 }
262 Tcl_Free((char*)pDb);
263}
264
265/*
266** This routine is called when a database file is locked while trying
267** to execute SQL.
268*/
269static int DbBusyHandler(void *cd, const char *zTable, int nTries){
270 SqliteDb *pDb = (SqliteDb*)cd;
271 int rc;
272 char zVal[30];
273 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000274 Tcl_DString cmd;
275
276 Tcl_DStringInit(&cmd);
277 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
278 Tcl_DStringAppendElement(&cmd, zTable);
279 sprintf(zVal, " %d", nTries);
280 Tcl_DStringAppend(&cmd, zVal, -1);
281 zCmd = Tcl_DStringValue(&cmd);
282 rc = Tcl_Eval(pDb->interp, zCmd);
283 Tcl_DStringFree(&cmd);
284 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
285 return 0;
286 }
287 return 1;
drh75897232000-05-29 14:26:00 +0000288}
289
290/*
drhcabb0812002-09-14 13:47:32 +0000291** This routine is called to evaluate an SQL function implemented
292** using TCL script.
293*/
294static void tclSqlFunc(sqlite_func *context, int argc, const char **argv){
295 SqlFunc *p = sqlite_user_data(context);
296 Tcl_DString cmd;
297 int i;
298 int rc;
299
300 Tcl_DStringInit(&cmd);
301 Tcl_DStringAppend(&cmd, p->zScript, -1);
302 for(i=0; i<argc; i++){
303 Tcl_DStringAppendElement(&cmd, argv[i] ? argv[i] : "");
304 }
305 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
306 if( rc ){
307 sqlite_set_result_error(context, Tcl_GetStringResult(p->interp), -1);
308 }else{
309 sqlite_set_result_string(context, Tcl_GetStringResult(p->interp), -1);
310 }
311}
312
313/*
drh75897232000-05-29 14:26:00 +0000314** The "sqlite" command below creates a new Tcl command for each
315** connection it opens to an SQLite database. This routine is invoked
316** whenever one of those connection-specific commands is executed
317** in Tcl. For example, if you run Tcl code like this:
318**
319** sqlite db1 "my_database"
320** db1 close
321**
322** The first command opens a connection to the "my_database" database
323** and calls that connection "db1". The second command causes this
324** subroutine to be invoked.
325*/
drh6d313162000-09-21 13:01:35 +0000326static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000327 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000328 int choice;
drh0de8c112002-07-06 16:32:14 +0000329 static const char *DB_strs[] = {
drh411995d2002-06-25 19:31:18 +0000330 "busy", "changes", "close",
drhcabb0812002-09-14 13:47:32 +0000331 "complete", "eval", "function",
332 "last_insert_rowid", "open_aux_file", "timeout",
333 0
drh6d313162000-09-21 13:01:35 +0000334 };
drh411995d2002-06-25 19:31:18 +0000335 enum DB_enum {
336 DB_BUSY, DB_CHANGES, DB_CLOSE,
drhcabb0812002-09-14 13:47:32 +0000337 DB_COMPLETE, DB_EVAL, DB_FUNCTION,
338 DB_LAST_INSERT_ROWID, DB_OPEN_AUX_FILE, DB_TIMEOUT,
drh6d313162000-09-21 13:01:35 +0000339 };
340
341 if( objc<2 ){
342 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000343 return TCL_ERROR;
344 }
drh411995d2002-06-25 19:31:18 +0000345 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000346 return TCL_ERROR;
347 }
348
drh411995d2002-06-25 19:31:18 +0000349 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000350
drhbec3f402000-08-04 13:49:02 +0000351 /* $db busy ?CALLBACK?
352 **
353 ** Invoke the given callback if an SQL statement attempts to open
354 ** a locked database file.
355 */
drh6d313162000-09-21 13:01:35 +0000356 case DB_BUSY: {
357 if( objc>3 ){
358 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000359 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000360 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000361 if( pDb->zBusy ){
362 Tcl_AppendResult(interp, pDb->zBusy, 0);
363 }
364 }else{
drh6d313162000-09-21 13:01:35 +0000365 char *zBusy;
366 int len;
drhbec3f402000-08-04 13:49:02 +0000367 if( pDb->zBusy ){
368 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000369 }
drh6d313162000-09-21 13:01:35 +0000370 zBusy = Tcl_GetStringFromObj(objv[2], &len);
371 if( zBusy && len>0 ){
372 pDb->zBusy = Tcl_Alloc( len + 1 );
373 strcpy(pDb->zBusy, zBusy);
374 }else{
375 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000376 }
377 if( pDb->zBusy ){
378 pDb->interp = interp;
379 sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000380 }else{
381 sqlite_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000382 }
383 }
drh6d313162000-09-21 13:01:35 +0000384 break;
385 }
drhbec3f402000-08-04 13:49:02 +0000386
drhc8d30ac2002-04-12 10:08:59 +0000387 /*
388 ** $db changes
389 **
390 ** Return the number of rows that were modified, inserted, or deleted by
391 ** the most recent "eval".
392 */
393 case DB_CHANGES: {
394 Tcl_Obj *pResult;
395 int nChange;
396 if( objc!=2 ){
397 Tcl_WrongNumArgs(interp, 2, objv, "");
398 return TCL_ERROR;
399 }
400 nChange = sqlite_changes(pDb->db);
401 pResult = Tcl_GetObjResult(interp);
402 Tcl_SetIntObj(pResult, nChange);
403 break;
404 }
405
drh75897232000-05-29 14:26:00 +0000406 /* $db close
407 **
408 ** Shutdown the database
409 */
drh6d313162000-09-21 13:01:35 +0000410 case DB_CLOSE: {
411 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
412 break;
413 }
drh75897232000-05-29 14:26:00 +0000414
415 /* $db complete SQL
416 **
417 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
418 ** additional lines of input are needed. This is similar to the
419 ** built-in "info complete" command of Tcl.
420 */
drh6d313162000-09-21 13:01:35 +0000421 case DB_COMPLETE: {
422 Tcl_Obj *pResult;
423 int isComplete;
424 if( objc!=3 ){
425 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000426 return TCL_ERROR;
427 }
drh6d313162000-09-21 13:01:35 +0000428 isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
429 pResult = Tcl_GetObjResult(interp);
430 Tcl_SetBooleanObj(pResult, isComplete);
431 break;
432 }
drh75897232000-05-29 14:26:00 +0000433
434 /*
435 ** $db eval $sql ?array { ...code... }?
436 **
437 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000438 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000439 ** If "array" and "code" are omitted, then no callback is every invoked.
440 ** If "array" is an empty string, then the values are placed in variables
441 ** that have the same name as the fields extracted by the query.
442 */
drh6d313162000-09-21 13:01:35 +0000443 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000444 CallbackData cbData;
445 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000446 char *zSql;
drh75897232000-05-29 14:26:00 +0000447 int rc;
drh297ecf12001-04-05 15:57:13 +0000448#ifdef UTF_TRANSLATION_NEEDED
449 Tcl_DString dSql;
drh6d4abfb2001-10-22 02:58:08 +0000450 int i;
drh297ecf12001-04-05 15:57:13 +0000451#endif
drh75897232000-05-29 14:26:00 +0000452
drh6d313162000-09-21 13:01:35 +0000453 if( objc!=5 && objc!=3 ){
454 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000455 return TCL_ERROR;
456 }
drhbec3f402000-08-04 13:49:02 +0000457 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000458 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh297ecf12001-04-05 15:57:13 +0000459#ifdef UTF_TRANSLATION_NEEDED
460 Tcl_DStringInit(&dSql);
461 Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
462 zSql = Tcl_DStringValue(&dSql);
463#endif
drh6d313162000-09-21 13:01:35 +0000464 Tcl_IncrRefCount(objv[2]);
465 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000466 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000467 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000468 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
469 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000470 cbData.tcl_rc = TCL_OK;
drh6d4abfb2001-10-22 02:58:08 +0000471 cbData.nColName = 0;
472 cbData.azColName = 0;
drh75897232000-05-29 14:26:00 +0000473 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000474 Tcl_IncrRefCount(objv[3]);
475 Tcl_IncrRefCount(objv[4]);
476 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
477 Tcl_DecrRefCount(objv[4]);
478 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000479 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000480 }else{
drh6d313162000-09-21 13:01:35 +0000481 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000482 cbData.tcl_rc = TCL_OK;
drh6d313162000-09-21 13:01:35 +0000483 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
484 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000485 }
drhb798fa62002-09-03 19:43:23 +0000486 if( rc==SQLITE_ABORT ){
487 if( zErrMsg ) free(zErrMsg);
488 rc = cbData.tcl_rc;
489 }else if( zErrMsg ){
drh75897232000-05-29 14:26:00 +0000490 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
491 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000492 rc = TCL_ERROR;
drhb798fa62002-09-03 19:43:23 +0000493 }else if( rc!=SQLITE_OK ){
drh6d4abfb2001-10-22 02:58:08 +0000494 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
495 rc = TCL_ERROR;
drh960e8c62001-04-03 16:53:21 +0000496 }else{
drh75897232000-05-29 14:26:00 +0000497 }
drh6d313162000-09-21 13:01:35 +0000498 Tcl_DecrRefCount(objv[2]);
drh297ecf12001-04-05 15:57:13 +0000499#ifdef UTF_TRANSLATION_NEEDED
500 Tcl_DStringFree(&dSql);
drh6d4abfb2001-10-22 02:58:08 +0000501 if( objc==5 && cbData.azColName ){
502 for(i=0; i<cbData.nColName; i++){
503 if( cbData.azColName[i] ) free(cbData.azColName[i]);
504 }
505 free(cbData.azColName);
drhce927062001-11-09 13:41:09 +0000506 cbData.azColName = 0;
drh6d4abfb2001-10-22 02:58:08 +0000507 }
drh297ecf12001-04-05 15:57:13 +0000508#endif
drh75897232000-05-29 14:26:00 +0000509 return rc;
drh6d313162000-09-21 13:01:35 +0000510 }
drhbec3f402000-08-04 13:49:02 +0000511
512 /*
drhcabb0812002-09-14 13:47:32 +0000513 ** $db function NAME SCRIPT
514 **
515 ** Create a new SQL function called NAME. Whenever that function is
516 ** called, invoke SCRIPT to evaluate the function.
517 */
518 case DB_FUNCTION: {
519 SqlFunc *pFunc;
520 char *zName;
521 char *zScript;
522 int nScript;
523 if( objc!=4 ){
524 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
525 return TCL_ERROR;
526 }
527 zName = Tcl_GetStringFromObj(objv[2], 0);
528 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
529 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
530 if( pFunc==0 ) return TCL_ERROR;
531 pFunc->interp = interp;
532 pFunc->pNext = pDb->pFunc;
533 pFunc->zScript = (char*)&pFunc[1];
534 strcpy(pFunc->zScript, zScript);
535 sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc);
536 sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC);
537 break;
538 }
539
540 /*
drhaf9ff332002-01-16 21:00:27 +0000541 ** $db last_insert_rowid
542 **
543 ** Return an integer which is the ROWID for the most recent insert.
544 */
545 case DB_LAST_INSERT_ROWID: {
546 Tcl_Obj *pResult;
547 int rowid;
548 if( objc!=2 ){
549 Tcl_WrongNumArgs(interp, 2, objv, "");
550 return TCL_ERROR;
551 }
552 rowid = sqlite_last_insert_rowid(pDb->db);
553 pResult = Tcl_GetObjResult(interp);
554 Tcl_SetIntObj(pResult, rowid);
555 break;
556 }
557
558 /*
drh411995d2002-06-25 19:31:18 +0000559 ** $db open_aux_file FILENAME
560 **
561 ** Begin using FILENAME as the database file used to store temporary
562 ** tables.
563 */
564 case DB_OPEN_AUX_FILE: {
565 const char *zFilename;
566 char *zErrMsg = 0;
567 int rc;
568 if( objc!=3 ){
569 Tcl_WrongNumArgs(interp, 2, objv, "FILENAME");
570 return TCL_ERROR;
571 }
572 zFilename = Tcl_GetStringFromObj(objv[2], 0);
573 rc = sqlite_open_aux_file(pDb->db, zFilename, &zErrMsg);
574 if( rc!=0 ){
575 if( zErrMsg ){
576 Tcl_AppendResult(interp, zErrMsg, 0);
577 free(zErrMsg);
578 }else{
579 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
580 }
581 return TCL_ERROR;
582 }
583 break;
584 }
585
586 /*
drhbec3f402000-08-04 13:49:02 +0000587 ** $db timeout MILLESECONDS
588 **
589 ** Delay for the number of milliseconds specified when a file is locked.
590 */
drh6d313162000-09-21 13:01:35 +0000591 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000592 int ms;
drh6d313162000-09-21 13:01:35 +0000593 if( objc!=3 ){
594 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000595 return TCL_ERROR;
596 }
drh6d313162000-09-21 13:01:35 +0000597 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
drhbec3f402000-08-04 13:49:02 +0000598 sqlite_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000599 break;
drh75897232000-05-29 14:26:00 +0000600 }
drh6d313162000-09-21 13:01:35 +0000601 } /* End of the SWITCH statement */
drh75897232000-05-29 14:26:00 +0000602 return TCL_OK;
603}
604
605/*
606** sqlite DBNAME FILENAME ?MODE?
607**
608** This is the main Tcl command. When the "sqlite" Tcl command is
609** invoked, this routine runs to process that command.
610**
611** The first argument, DBNAME, is an arbitrary name for a new
612** database connection. This command creates a new command named
613** DBNAME that is used to control that connection. The database
614** connection is deleted when the DBNAME command is deleted.
615**
616** The second argument is the name of the directory that contains
617** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000618**
619** For testing purposes, we also support the following:
620**
621** sqlite -encoding
622**
623** Return the encoding used by LIKE and GLOB operators. Choices
624** are UTF-8 and iso8859.
625**
626** sqlite -tcl-uses-utf
627**
628** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
629** not. Used by tests to make sure the library was compiled
630** correctly.
drh75897232000-05-29 14:26:00 +0000631*/
632static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){
633 int mode;
drhbec3f402000-08-04 13:49:02 +0000634 SqliteDb *p;
drh75897232000-05-29 14:26:00 +0000635 char *zErrMsg;
drh06b27182002-06-26 20:06:05 +0000636 char zBuf[80];
drhfbc3eab2001-04-06 16:13:42 +0000637 if( argc==2 ){
638 if( strcmp(argv[1],"-encoding")==0 ){
639 Tcl_AppendResult(interp,sqlite_encoding,0);
640 return TCL_OK;
641 }
642 if( strcmp(argv[1],"-tcl-uses-utf")==0 ){
643#ifdef TCL_UTF_MAX
644 Tcl_AppendResult(interp,"1",0);
645#else
646 Tcl_AppendResult(interp,"0",0);
647#endif
648 return TCL_OK;
649 }
650 }
drh75897232000-05-29 14:26:00 +0000651 if( argc!=3 && argc!=4 ){
652 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
653 " HANDLE FILENAME ?MODE?\"", 0);
654 return TCL_ERROR;
655 }
656 if( argc==3 ){
drh58b95762000-06-02 01:17:37 +0000657 mode = 0666;
drh75897232000-05-29 14:26:00 +0000658 }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){
659 return TCL_ERROR;
660 }
661 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +0000662 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +0000663 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +0000664 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
665 return TCL_ERROR;
666 }
667 memset(p, 0, sizeof(*p));
668 p->db = sqlite_open(argv[2], mode, &zErrMsg);
669 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +0000670 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +0000671 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +0000672 free(zErrMsg);
673 return TCL_ERROR;
674 }
drh6d313162000-09-21 13:01:35 +0000675 Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +0000676
drh06b27182002-06-26 20:06:05 +0000677 /* The return value is the value of the sqlite* pointer
678 */
679 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +0000680 if( strncmp(zBuf,"0x",2) ){
681 sprintf(zBuf, "0x%p", p->db);
682 }
drh06b27182002-06-26 20:06:05 +0000683 Tcl_AppendResult(interp, zBuf, 0);
684
drhc22bd472002-05-10 13:14:07 +0000685 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +0000686 ** SQL function.
drhc22bd472002-05-10 13:14:07 +0000687 */
drh28b4e482002-03-11 02:06:13 +0000688#ifdef SQLITE_TEST
689 {
drhc22bd472002-05-10 13:14:07 +0000690 extern void Md5_Register(sqlite*);
691 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +0000692 }
drh28b4e482002-03-11 02:06:13 +0000693#endif
drh75897232000-05-29 14:26:00 +0000694 return TCL_OK;
695}
696
697/*
drh90ca9752001-09-28 17:47:14 +0000698** Provide a dummy Tcl_InitStubs if we are using this as a static
699** library.
700*/
701#ifndef USE_TCL_STUBS
702# undef Tcl_InitStubs
703# define Tcl_InitStubs(a,b,c)
704#endif
705
706/*
drh75897232000-05-29 14:26:00 +0000707** Initialize this module.
708**
709** This Tcl module contains only a single new Tcl command named "sqlite".
710** (Hence there is no namespace. There is no point in using a namespace
711** if the extension only supplies one new name!) The "sqlite" command is
712** used to open a new SQLite database. See the DbMain() routine above
713** for additional information.
714*/
715int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +0000716 Tcl_InitStubs(interp, "8.0", 0);
drhc2eef3b2002-08-31 18:53:06 +0000717 Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000718 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh90ca9752001-09-28 17:47:14 +0000719 return TCL_OK;
720}
721int Tclsqlite_Init(Tcl_Interp *interp){
722 Tcl_InitStubs(interp, "8.0", 0);
drhc2eef3b2002-08-31 18:53:06 +0000723 Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000724 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh75897232000-05-29 14:26:00 +0000725 return TCL_OK;
726}
727int Sqlite_SafeInit(Tcl_Interp *interp){
728 return TCL_OK;
729}
drh90ca9752001-09-28 17:47:14 +0000730int Tclsqlite_SafeInit(Tcl_Interp *interp){
731 return TCL_OK;
732}
drh75897232000-05-29 14:26:00 +0000733
drh3cebbde2000-10-19 14:59:27 +0000734#if 0
drh75897232000-05-29 14:26:00 +0000735/*
736** If compiled using mktclapp, this routine runs to initialize
737** everything.
738*/
739int Et_AppInit(Tcl_Interp *interp){
740 return Sqlite_Init(interp);
741}
drh3cebbde2000-10-19 14:59:27 +0000742#endif
drh348784e2000-05-29 20:41:49 +0000743
744/*
745** If the macro TCLSH is defined and is one, then put in code for the
746** "main" routine that will initialize Tcl.
747*/
748#if defined(TCLSH) && TCLSH==1
749static char zMainloop[] =
750 "set line {}\n"
751 "while {![eof stdin]} {\n"
752 "if {$line!=\"\"} {\n"
753 "puts -nonewline \"> \"\n"
754 "} else {\n"
755 "puts -nonewline \"% \"\n"
756 "}\n"
757 "flush stdout\n"
758 "append line [gets stdin]\n"
759 "if {[info complete $line]} {\n"
760 "if {[catch {uplevel #0 $line} result]} {\n"
761 "puts stderr \"Error: $result\"\n"
762 "} elseif {$result!=\"\"} {\n"
763 "puts $result\n"
764 "}\n"
765 "set line {}\n"
766 "} else {\n"
767 "append line \\n\n"
768 "}\n"
769 "}\n"
770;
771
772#define TCLSH_MAIN main /* Needed to fake out mktclapp */
773int TCLSH_MAIN(int argc, char **argv){
774 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +0000775 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +0000776 interp = Tcl_CreateInterp();
777 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +0000778#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +0000779 {
780 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +0000781 extern int Sqlitetest2_Init(Tcl_Interp*);
782 extern int Sqlitetest3_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +0000783 extern int Md5_Init(Tcl_Interp*);
drhd1bf3512001-04-07 15:24:33 +0000784 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +0000785 Sqlitetest2_Init(interp);
786 Sqlitetest3_Init(interp);
drhefc251d2001-07-01 22:12:01 +0000787 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +0000788 }
789#endif
drh348784e2000-05-29 20:41:49 +0000790 if( argc>=2 ){
791 int i;
792 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
793 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
794 for(i=2; i<argc; i++){
795 Tcl_SetVar(interp, "argv", argv[i],
796 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
797 }
798 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +0000799 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +0000800 if( zInfo==0 ) zInfo = interp->result;
801 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +0000802 return 1;
803 }
804 }else{
805 Tcl_GlobalEval(interp, zMainloop);
806 }
807 return 0;
808}
809#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +0000810
811#endif /* !defined(NO_TCL) */