blob: bddcbb267b401ed882e174055dc7b7e87c4347e8 [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**
drhdcd997e2003-01-31 17:21:49 +000014** $Id: tclsqlite.c,v 1.44 2003/01/31 17:21: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
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 */
drhdcd997e2003-01-31 17:21:49 +000055 int rc; /* Return code of most recent sqlite_exec() */
drhbec3f402000-08-04 13:49:02 +000056};
57
58/*
drh75897232000-05-29 14:26:00 +000059** An instance of this structure passes information thru the sqlite
60** logic from the original TCL command into the callback routine.
61*/
62typedef struct CallbackData CallbackData;
63struct CallbackData {
64 Tcl_Interp *interp; /* The TCL interpreter */
65 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000066 Tcl_Obj *pCode; /* The code to execute for each row */
drhce927062001-11-09 13:41:09 +000067 int once; /* Set for first callback only */
drh960e8c62001-04-03 16:53:21 +000068 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000069 int nColName; /* Number of entries in the azColName[] array */
70 char **azColName; /* Column names translated to UTF-8 */
drh98808ba2001-10-18 12:34:46 +000071};
drh297ecf12001-04-05 15:57:13 +000072
drh6d4abfb2001-10-22 02:58:08 +000073#ifdef UTF_TRANSLATION_NEEDED
drh297ecf12001-04-05 15:57:13 +000074/*
drh75897232000-05-29 14:26:00 +000075** Called for each row of the result.
drh6d4abfb2001-10-22 02:58:08 +000076**
77** This version is used when TCL expects UTF-8 data but the database
78** uses the ISO8859 format. A translation must occur from ISO8859 into
79** UTF-8.
drh75897232000-05-29 14:26:00 +000080*/
81static int DbEvalCallback(
82 void *clientData, /* An instance of CallbackData */
83 int nCol, /* Number of columns in the result */
84 char ** azCol, /* Data for each column */
85 char ** azN /* Name for each column */
86){
87 CallbackData *cbData = (CallbackData*)clientData;
88 int i, rc;
drh297ecf12001-04-05 15:57:13 +000089 Tcl_DString dCol;
drh6d4abfb2001-10-22 02:58:08 +000090 Tcl_DStringInit(&dCol);
drhce927062001-11-09 13:41:09 +000091 if( cbData->azColName==0 ){
92 assert( cbData->once );
93 cbData->once = 0;
94 if( cbData->zArray[0] ){
95 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
drh6d4abfb2001-10-22 02:58:08 +000096 }
drhce927062001-11-09 13:41:09 +000097 cbData->azColName = malloc( nCol*sizeof(char*) );
98 if( cbData->azColName==0 ){ return 1; }
drh6d4abfb2001-10-22 02:58:08 +000099 cbData->nColName = nCol;
100 for(i=0; i<nCol; i++){
101 Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol);
drhce927062001-11-09 13:41:09 +0000102 cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 );
103 if( cbData->azColName[i] ){
104 strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol));
105 }else{
106 return 1;
drh6d4abfb2001-10-22 02:58:08 +0000107 }
drhce927062001-11-09 13:41:09 +0000108 if( cbData->zArray[0] ){
109 Tcl_SetVar2(cbData->interp, cbData->zArray, "*",
110 Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh704027f2002-07-15 20:58:47 +0000111 if( azN[nCol]!=0 ){
drh5080aaa2002-07-11 12:18:16 +0000112 Tcl_DString dType;
113 Tcl_DStringInit(&dType);
114 Tcl_DStringAppend(&dType, "typeof:", -1);
115 Tcl_DStringAppend(&dType, Tcl_DStringValue(&dCol), -1);
116 Tcl_DStringFree(&dCol);
117 Tcl_ExternalToUtfDString(NULL, azN[i+nCol], -1, &dCol);
118 Tcl_SetVar2(cbData->interp, cbData->zArray,
119 Tcl_DStringValue(&dType), Tcl_DStringValue(&dCol),
120 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
121 Tcl_DStringFree(&dType);
122 }
drhce927062001-11-09 13:41:09 +0000123 }
drhfa173a72002-07-10 21:26:00 +0000124
drh6d4abfb2001-10-22 02:58:08 +0000125 Tcl_DStringFree(&dCol);
126 }
drh6d4abfb2001-10-22 02:58:08 +0000127 }
128 if( azCol!=0 ){
129 if( cbData->zArray[0] ){
130 for(i=0; i<nCol; i++){
131 char *z = azCol[i];
132 if( z==0 ) z = "";
133 Tcl_DStringInit(&dCol);
134 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
135 Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i],
136 Tcl_DStringValue(&dCol), 0);
137 Tcl_DStringFree(&dCol);
138 }
139 }else{
140 for(i=0; i<nCol; i++){
141 char *z = azCol[i];
142 if( z==0 ) z = "";
143 Tcl_DStringInit(&dCol);
144 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
145 Tcl_SetVar(cbData->interp, cbData->azColName[i],
146 Tcl_DStringValue(&dCol), 0);
147 Tcl_DStringFree(&dCol);
148 }
149 }
150 }
151 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
152 if( rc==TCL_CONTINUE ) rc = TCL_OK;
153 cbData->tcl_rc = rc;
154 return rc!=TCL_OK;
155}
156#endif /* UTF_TRANSLATION_NEEDED */
157
158#ifndef UTF_TRANSLATION_NEEDED
159/*
160** Called for each row of the result.
161**
162** This version is used when either of the following is true:
163**
164** (1) This version of TCL uses UTF-8 and the data in the
165** SQLite database is already in the UTF-8 format.
166**
167** (2) This version of TCL uses ISO8859 and the data in the
168** SQLite database is already in the ISO8859 format.
169*/
170static int DbEvalCallback(
171 void *clientData, /* An instance of CallbackData */
172 int nCol, /* Number of columns in the result */
173 char ** azCol, /* Data for each column */
174 char ** azN /* Name for each column */
175){
176 CallbackData *cbData = (CallbackData*)clientData;
177 int i, rc;
drh6a535342001-10-19 16:44:56 +0000178 if( azCol==0 || (cbData->once && cbData->zArray[0]) ){
179 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
180 for(i=0; i<nCol; i++){
181 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
182 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh5080aaa2002-07-11 12:18:16 +0000183 if( azN[nCol] ){
184 char *z = sqlite_mprintf("typeof:%s", azN[i]);
185 Tcl_SetVar2(cbData->interp, cbData->zArray, z, azN[i+nCol],
186 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
187 sqlite_freemem(z);
188 }
drh6a535342001-10-19 16:44:56 +0000189 }
190 cbData->once = 0;
191 }
192 if( azCol!=0 ){
193 if( cbData->zArray[0] ){
drh75897232000-05-29 14:26:00 +0000194 for(i=0; i<nCol; i++){
drh6a535342001-10-19 16:44:56 +0000195 char *z = azCol[i];
196 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000197 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
drh6a535342001-10-19 16:44:56 +0000198 }
199 }else{
200 for(i=0; i<nCol; i++){
201 char *z = azCol[i];
202 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000203 Tcl_SetVar(cbData->interp, azN[i], z, 0);
drh75897232000-05-29 14:26:00 +0000204 }
205 }
drh75897232000-05-29 14:26:00 +0000206 }
drh6d313162000-09-21 13:01:35 +0000207 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
drh960e8c62001-04-03 16:53:21 +0000208 if( rc==TCL_CONTINUE ) rc = TCL_OK;
209 cbData->tcl_rc = rc;
210 return rc!=TCL_OK;
drh75897232000-05-29 14:26:00 +0000211}
drh6d4abfb2001-10-22 02:58:08 +0000212#endif
drh75897232000-05-29 14:26:00 +0000213
214/*
drh6d313162000-09-21 13:01:35 +0000215** This is an alternative callback for database queries. Instead
216** of invoking a TCL script to handle the result, this callback just
217** appends each column of the result to a list. After the query
218** is complete, the list is returned.
219*/
220static int DbEvalCallback2(
221 void *clientData, /* An instance of CallbackData */
222 int nCol, /* Number of columns in the result */
223 char ** azCol, /* Data for each column */
224 char ** azN /* Name for each column */
225){
226 Tcl_Obj *pList = (Tcl_Obj*)clientData;
227 int i;
drh6a535342001-10-19 16:44:56 +0000228 if( azCol==0 ) return 0;
drh6d313162000-09-21 13:01:35 +0000229 for(i=0; i<nCol; i++){
230 Tcl_Obj *pElem;
231 if( azCol[i] && *azCol[i] ){
drh297ecf12001-04-05 15:57:13 +0000232#ifdef UTF_TRANSLATION_NEEDED
233 Tcl_DString dCol;
234 Tcl_DStringInit(&dCol);
235 Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol);
236 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
237 Tcl_DStringFree(&dCol);
238#else
drh6d313162000-09-21 13:01:35 +0000239 pElem = Tcl_NewStringObj(azCol[i], -1);
drh297ecf12001-04-05 15:57:13 +0000240#endif
drh6d313162000-09-21 13:01:35 +0000241 }else{
242 pElem = Tcl_NewObj();
243 }
244 Tcl_ListObjAppendElement(0, pList, pElem);
245 }
246 return 0;
247}
248
249/*
drh75897232000-05-29 14:26:00 +0000250** Called when the command is deleted.
251*/
252static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000253 SqliteDb *pDb = (SqliteDb*)db;
254 sqlite_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000255 while( pDb->pFunc ){
256 SqlFunc *pFunc = pDb->pFunc;
257 pDb->pFunc = pFunc->pNext;
258 Tcl_Free((char*)pFunc);
259 }
drhbec3f402000-08-04 13:49:02 +0000260 if( pDb->zBusy ){
261 Tcl_Free(pDb->zBusy);
262 }
263 Tcl_Free((char*)pDb);
264}
265
266/*
267** This routine is called when a database file is locked while trying
268** to execute SQL.
269*/
270static int DbBusyHandler(void *cd, const char *zTable, int nTries){
271 SqliteDb *pDb = (SqliteDb*)cd;
272 int rc;
273 char zVal[30];
274 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000275 Tcl_DString cmd;
276
277 Tcl_DStringInit(&cmd);
278 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
279 Tcl_DStringAppendElement(&cmd, zTable);
280 sprintf(zVal, " %d", nTries);
281 Tcl_DStringAppend(&cmd, zVal, -1);
282 zCmd = Tcl_DStringValue(&cmd);
283 rc = Tcl_Eval(pDb->interp, zCmd);
284 Tcl_DStringFree(&cmd);
285 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
286 return 0;
287 }
288 return 1;
drh75897232000-05-29 14:26:00 +0000289}
290
291/*
drhcabb0812002-09-14 13:47:32 +0000292** This routine is called to evaluate an SQL function implemented
293** using TCL script.
294*/
295static void tclSqlFunc(sqlite_func *context, int argc, const char **argv){
296 SqlFunc *p = sqlite_user_data(context);
297 Tcl_DString cmd;
298 int i;
299 int rc;
300
301 Tcl_DStringInit(&cmd);
302 Tcl_DStringAppend(&cmd, p->zScript, -1);
303 for(i=0; i<argc; i++){
304 Tcl_DStringAppendElement(&cmd, argv[i] ? argv[i] : "");
305 }
306 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
307 if( rc ){
308 sqlite_set_result_error(context, Tcl_GetStringResult(p->interp), -1);
309 }else{
310 sqlite_set_result_string(context, Tcl_GetStringResult(p->interp), -1);
311 }
312}
313
314/*
drh75897232000-05-29 14:26:00 +0000315** The "sqlite" command below creates a new Tcl command for each
316** connection it opens to an SQLite database. This routine is invoked
317** whenever one of those connection-specific commands is executed
318** in Tcl. For example, if you run Tcl code like this:
319**
320** sqlite db1 "my_database"
321** db1 close
322**
323** The first command opens a connection to the "my_database" database
324** and calls that connection "db1". The second command causes this
325** subroutine to be invoked.
326*/
drh6d313162000-09-21 13:01:35 +0000327static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000328 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000329 int choice;
drh0de8c112002-07-06 16:32:14 +0000330 static const char *DB_strs[] = {
drh411995d2002-06-25 19:31:18 +0000331 "busy", "changes", "close",
drhdcd997e2003-01-31 17:21:49 +0000332 "complete", "errorcode", "eval",
333 "function", "last_insert_rowid", "open_aux_file",
334 "timeout", 0
drh6d313162000-09-21 13:01:35 +0000335 };
drh411995d2002-06-25 19:31:18 +0000336 enum DB_enum {
337 DB_BUSY, DB_CHANGES, DB_CLOSE,
drhdcd997e2003-01-31 17:21:49 +0000338 DB_COMPLETE, DB_ERRORCODE, DB_EVAL,
339 DB_FUNCTION, DB_LAST_INSERT_ROWID,DB_OPEN_AUX_FILE,
340 DB_TIMEOUT,
drh6d313162000-09-21 13:01:35 +0000341 };
342
343 if( objc<2 ){
344 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000345 return TCL_ERROR;
346 }
drh411995d2002-06-25 19:31:18 +0000347 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000348 return TCL_ERROR;
349 }
350
drh411995d2002-06-25 19:31:18 +0000351 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000352
drhbec3f402000-08-04 13:49:02 +0000353 /* $db busy ?CALLBACK?
354 **
355 ** Invoke the given callback if an SQL statement attempts to open
356 ** a locked database file.
357 */
drh6d313162000-09-21 13:01:35 +0000358 case DB_BUSY: {
359 if( objc>3 ){
360 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000361 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000362 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000363 if( pDb->zBusy ){
364 Tcl_AppendResult(interp, pDb->zBusy, 0);
365 }
366 }else{
drh6d313162000-09-21 13:01:35 +0000367 char *zBusy;
368 int len;
drhbec3f402000-08-04 13:49:02 +0000369 if( pDb->zBusy ){
370 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000371 }
drh6d313162000-09-21 13:01:35 +0000372 zBusy = Tcl_GetStringFromObj(objv[2], &len);
373 if( zBusy && len>0 ){
374 pDb->zBusy = Tcl_Alloc( len + 1 );
375 strcpy(pDb->zBusy, zBusy);
376 }else{
377 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000378 }
379 if( pDb->zBusy ){
380 pDb->interp = interp;
381 sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000382 }else{
383 sqlite_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000384 }
385 }
drh6d313162000-09-21 13:01:35 +0000386 break;
387 }
drhbec3f402000-08-04 13:49:02 +0000388
drhc8d30ac2002-04-12 10:08:59 +0000389 /*
390 ** $db changes
391 **
392 ** Return the number of rows that were modified, inserted, or deleted by
393 ** the most recent "eval".
394 */
395 case DB_CHANGES: {
396 Tcl_Obj *pResult;
397 int nChange;
398 if( objc!=2 ){
399 Tcl_WrongNumArgs(interp, 2, objv, "");
400 return TCL_ERROR;
401 }
402 nChange = sqlite_changes(pDb->db);
403 pResult = Tcl_GetObjResult(interp);
404 Tcl_SetIntObj(pResult, nChange);
405 break;
406 }
407
drh75897232000-05-29 14:26:00 +0000408 /* $db close
409 **
410 ** Shutdown the database
411 */
drh6d313162000-09-21 13:01:35 +0000412 case DB_CLOSE: {
413 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
414 break;
415 }
drh75897232000-05-29 14:26:00 +0000416
417 /* $db complete SQL
418 **
419 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
420 ** additional lines of input are needed. This is similar to the
421 ** built-in "info complete" command of Tcl.
422 */
drh6d313162000-09-21 13:01:35 +0000423 case DB_COMPLETE: {
424 Tcl_Obj *pResult;
425 int isComplete;
426 if( objc!=3 ){
427 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000428 return TCL_ERROR;
429 }
drh6d313162000-09-21 13:01:35 +0000430 isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
431 pResult = Tcl_GetObjResult(interp);
432 Tcl_SetBooleanObj(pResult, isComplete);
433 break;
434 }
drhdcd997e2003-01-31 17:21:49 +0000435
436 /*
437 ** $db errorcode
438 **
439 ** Return the numeric error code that was returned by the most recent
440 ** call to sqlite_exec().
441 */
442 case DB_ERRORCODE: {
443 Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc));
444 break;
445 }
drh75897232000-05-29 14:26:00 +0000446
447 /*
448 ** $db eval $sql ?array { ...code... }?
449 **
450 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000451 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000452 ** If "array" and "code" are omitted, then no callback is every invoked.
453 ** If "array" is an empty string, then the values are placed in variables
454 ** that have the same name as the fields extracted by the query.
455 */
drh6d313162000-09-21 13:01:35 +0000456 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000457 CallbackData cbData;
458 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000459 char *zSql;
drh75897232000-05-29 14:26:00 +0000460 int rc;
drh297ecf12001-04-05 15:57:13 +0000461#ifdef UTF_TRANSLATION_NEEDED
462 Tcl_DString dSql;
drh6d4abfb2001-10-22 02:58:08 +0000463 int i;
drh297ecf12001-04-05 15:57:13 +0000464#endif
drh75897232000-05-29 14:26:00 +0000465
drh6d313162000-09-21 13:01:35 +0000466 if( objc!=5 && objc!=3 ){
467 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000468 return TCL_ERROR;
469 }
drhbec3f402000-08-04 13:49:02 +0000470 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000471 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh297ecf12001-04-05 15:57:13 +0000472#ifdef UTF_TRANSLATION_NEEDED
473 Tcl_DStringInit(&dSql);
474 Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
475 zSql = Tcl_DStringValue(&dSql);
476#endif
drh6d313162000-09-21 13:01:35 +0000477 Tcl_IncrRefCount(objv[2]);
478 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000479 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000480 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000481 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
482 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000483 cbData.tcl_rc = TCL_OK;
drh6d4abfb2001-10-22 02:58:08 +0000484 cbData.nColName = 0;
485 cbData.azColName = 0;
drh75897232000-05-29 14:26:00 +0000486 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000487 Tcl_IncrRefCount(objv[3]);
488 Tcl_IncrRefCount(objv[4]);
489 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
490 Tcl_DecrRefCount(objv[4]);
491 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000492 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000493 }else{
drh6d313162000-09-21 13:01:35 +0000494 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000495 cbData.tcl_rc = TCL_OK;
drh6d313162000-09-21 13:01:35 +0000496 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
497 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000498 }
drhdcd997e2003-01-31 17:21:49 +0000499 pDb->rc = rc;
drhb798fa62002-09-03 19:43:23 +0000500 if( rc==SQLITE_ABORT ){
501 if( zErrMsg ) free(zErrMsg);
502 rc = cbData.tcl_rc;
503 }else if( zErrMsg ){
drh75897232000-05-29 14:26:00 +0000504 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
505 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000506 rc = TCL_ERROR;
drhb798fa62002-09-03 19:43:23 +0000507 }else if( rc!=SQLITE_OK ){
drh6d4abfb2001-10-22 02:58:08 +0000508 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
509 rc = TCL_ERROR;
drh960e8c62001-04-03 16:53:21 +0000510 }else{
drh75897232000-05-29 14:26:00 +0000511 }
drh6d313162000-09-21 13:01:35 +0000512 Tcl_DecrRefCount(objv[2]);
drh297ecf12001-04-05 15:57:13 +0000513#ifdef UTF_TRANSLATION_NEEDED
514 Tcl_DStringFree(&dSql);
drh6d4abfb2001-10-22 02:58:08 +0000515 if( objc==5 && cbData.azColName ){
516 for(i=0; i<cbData.nColName; i++){
517 if( cbData.azColName[i] ) free(cbData.azColName[i]);
518 }
519 free(cbData.azColName);
drhce927062001-11-09 13:41:09 +0000520 cbData.azColName = 0;
drh6d4abfb2001-10-22 02:58:08 +0000521 }
drh297ecf12001-04-05 15:57:13 +0000522#endif
drh75897232000-05-29 14:26:00 +0000523 return rc;
drh6d313162000-09-21 13:01:35 +0000524 }
drhbec3f402000-08-04 13:49:02 +0000525
526 /*
drhcabb0812002-09-14 13:47:32 +0000527 ** $db function NAME SCRIPT
528 **
529 ** Create a new SQL function called NAME. Whenever that function is
530 ** called, invoke SCRIPT to evaluate the function.
531 */
532 case DB_FUNCTION: {
533 SqlFunc *pFunc;
534 char *zName;
535 char *zScript;
536 int nScript;
537 if( objc!=4 ){
538 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
539 return TCL_ERROR;
540 }
541 zName = Tcl_GetStringFromObj(objv[2], 0);
542 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
543 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
544 if( pFunc==0 ) return TCL_ERROR;
545 pFunc->interp = interp;
546 pFunc->pNext = pDb->pFunc;
547 pFunc->zScript = (char*)&pFunc[1];
548 strcpy(pFunc->zScript, zScript);
549 sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc);
550 sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC);
551 break;
552 }
553
554 /*
drhaf9ff332002-01-16 21:00:27 +0000555 ** $db last_insert_rowid
556 **
557 ** Return an integer which is the ROWID for the most recent insert.
558 */
559 case DB_LAST_INSERT_ROWID: {
560 Tcl_Obj *pResult;
561 int rowid;
562 if( objc!=2 ){
563 Tcl_WrongNumArgs(interp, 2, objv, "");
564 return TCL_ERROR;
565 }
566 rowid = sqlite_last_insert_rowid(pDb->db);
567 pResult = Tcl_GetObjResult(interp);
568 Tcl_SetIntObj(pResult, rowid);
569 break;
570 }
571
572 /*
drh411995d2002-06-25 19:31:18 +0000573 ** $db open_aux_file FILENAME
574 **
575 ** Begin using FILENAME as the database file used to store temporary
576 ** tables.
577 */
578 case DB_OPEN_AUX_FILE: {
579 const char *zFilename;
580 char *zErrMsg = 0;
581 int rc;
582 if( objc!=3 ){
583 Tcl_WrongNumArgs(interp, 2, objv, "FILENAME");
584 return TCL_ERROR;
585 }
586 zFilename = Tcl_GetStringFromObj(objv[2], 0);
587 rc = sqlite_open_aux_file(pDb->db, zFilename, &zErrMsg);
drhdcd997e2003-01-31 17:21:49 +0000588 pDb->rc = rc;
drh411995d2002-06-25 19:31:18 +0000589 if( rc!=0 ){
590 if( zErrMsg ){
591 Tcl_AppendResult(interp, zErrMsg, 0);
592 free(zErrMsg);
593 }else{
594 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
595 }
596 return TCL_ERROR;
597 }
598 break;
599 }
600
601 /*
drhbec3f402000-08-04 13:49:02 +0000602 ** $db timeout MILLESECONDS
603 **
604 ** Delay for the number of milliseconds specified when a file is locked.
605 */
drh6d313162000-09-21 13:01:35 +0000606 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000607 int ms;
drh6d313162000-09-21 13:01:35 +0000608 if( objc!=3 ){
609 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000610 return TCL_ERROR;
611 }
drh6d313162000-09-21 13:01:35 +0000612 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
drhbec3f402000-08-04 13:49:02 +0000613 sqlite_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000614 break;
drh75897232000-05-29 14:26:00 +0000615 }
drh6d313162000-09-21 13:01:35 +0000616 } /* End of the SWITCH statement */
drh75897232000-05-29 14:26:00 +0000617 return TCL_OK;
618}
619
620/*
621** sqlite DBNAME FILENAME ?MODE?
622**
623** This is the main Tcl command. When the "sqlite" Tcl command is
624** invoked, this routine runs to process that command.
625**
626** The first argument, DBNAME, is an arbitrary name for a new
627** database connection. This command creates a new command named
628** DBNAME that is used to control that connection. The database
629** connection is deleted when the DBNAME command is deleted.
630**
631** The second argument is the name of the directory that contains
632** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000633**
634** For testing purposes, we also support the following:
635**
636** sqlite -encoding
637**
638** Return the encoding used by LIKE and GLOB operators. Choices
639** are UTF-8 and iso8859.
640**
drh647cb0e2002-11-04 19:32:25 +0000641** sqlite -version
642**
643** Return the version number of the SQLite library.
644**
drhfbc3eab2001-04-06 16:13:42 +0000645** sqlite -tcl-uses-utf
646**
647** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
648** not. Used by tests to make sure the library was compiled
649** correctly.
drh75897232000-05-29 14:26:00 +0000650*/
651static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){
652 int mode;
drhbec3f402000-08-04 13:49:02 +0000653 SqliteDb *p;
drh75897232000-05-29 14:26:00 +0000654 char *zErrMsg;
drh06b27182002-06-26 20:06:05 +0000655 char zBuf[80];
drhfbc3eab2001-04-06 16:13:42 +0000656 if( argc==2 ){
657 if( strcmp(argv[1],"-encoding")==0 ){
658 Tcl_AppendResult(interp,sqlite_encoding,0);
659 return TCL_OK;
660 }
drh647cb0e2002-11-04 19:32:25 +0000661 if( strcmp(argv[1],"-version")==0 ){
662 Tcl_AppendResult(interp,sqlite_version,0);
663 return TCL_OK;
664 }
drhfbc3eab2001-04-06 16:13:42 +0000665 if( strcmp(argv[1],"-tcl-uses-utf")==0 ){
666#ifdef TCL_UTF_MAX
667 Tcl_AppendResult(interp,"1",0);
668#else
669 Tcl_AppendResult(interp,"0",0);
670#endif
671 return TCL_OK;
672 }
673 }
drh75897232000-05-29 14:26:00 +0000674 if( argc!=3 && argc!=4 ){
675 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
676 " HANDLE FILENAME ?MODE?\"", 0);
677 return TCL_ERROR;
678 }
679 if( argc==3 ){
drh58b95762000-06-02 01:17:37 +0000680 mode = 0666;
drh75897232000-05-29 14:26:00 +0000681 }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){
682 return TCL_ERROR;
683 }
684 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +0000685 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +0000686 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +0000687 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
688 return TCL_ERROR;
689 }
690 memset(p, 0, sizeof(*p));
691 p->db = sqlite_open(argv[2], mode, &zErrMsg);
692 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +0000693 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +0000694 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +0000695 free(zErrMsg);
696 return TCL_ERROR;
697 }
drh6d313162000-09-21 13:01:35 +0000698 Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +0000699
drh06b27182002-06-26 20:06:05 +0000700 /* The return value is the value of the sqlite* pointer
701 */
702 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +0000703 if( strncmp(zBuf,"0x",2) ){
704 sprintf(zBuf, "0x%p", p->db);
705 }
drh06b27182002-06-26 20:06:05 +0000706 Tcl_AppendResult(interp, zBuf, 0);
707
drhc22bd472002-05-10 13:14:07 +0000708 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +0000709 ** SQL function.
drhc22bd472002-05-10 13:14:07 +0000710 */
drh28b4e482002-03-11 02:06:13 +0000711#ifdef SQLITE_TEST
712 {
drhc22bd472002-05-10 13:14:07 +0000713 extern void Md5_Register(sqlite*);
714 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +0000715 }
drh28b4e482002-03-11 02:06:13 +0000716#endif
drh75897232000-05-29 14:26:00 +0000717 return TCL_OK;
718}
719
720/*
drh90ca9752001-09-28 17:47:14 +0000721** Provide a dummy Tcl_InitStubs if we are using this as a static
722** library.
723*/
724#ifndef USE_TCL_STUBS
725# undef Tcl_InitStubs
726# define Tcl_InitStubs(a,b,c)
727#endif
728
729/*
drh75897232000-05-29 14:26:00 +0000730** Initialize this module.
731**
732** This Tcl module contains only a single new Tcl command named "sqlite".
733** (Hence there is no namespace. There is no point in using a namespace
734** if the extension only supplies one new name!) The "sqlite" command is
735** used to open a new SQLite database. See the DbMain() routine above
736** for additional information.
737*/
738int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +0000739 Tcl_InitStubs(interp, "8.0", 0);
drhc2eef3b2002-08-31 18:53:06 +0000740 Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000741 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh90ca9752001-09-28 17:47:14 +0000742 return TCL_OK;
743}
744int Tclsqlite_Init(Tcl_Interp *interp){
745 Tcl_InitStubs(interp, "8.0", 0);
drhc2eef3b2002-08-31 18:53:06 +0000746 Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000747 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh75897232000-05-29 14:26:00 +0000748 return TCL_OK;
749}
750int Sqlite_SafeInit(Tcl_Interp *interp){
751 return TCL_OK;
752}
drh90ca9752001-09-28 17:47:14 +0000753int Tclsqlite_SafeInit(Tcl_Interp *interp){
754 return TCL_OK;
755}
drh75897232000-05-29 14:26:00 +0000756
drh3cebbde2000-10-19 14:59:27 +0000757#if 0
drh75897232000-05-29 14:26:00 +0000758/*
759** If compiled using mktclapp, this routine runs to initialize
760** everything.
761*/
762int Et_AppInit(Tcl_Interp *interp){
763 return Sqlite_Init(interp);
764}
drh3cebbde2000-10-19 14:59:27 +0000765#endif
drh348784e2000-05-29 20:41:49 +0000766
767/*
768** If the macro TCLSH is defined and is one, then put in code for the
769** "main" routine that will initialize Tcl.
770*/
771#if defined(TCLSH) && TCLSH==1
772static char zMainloop[] =
773 "set line {}\n"
774 "while {![eof stdin]} {\n"
775 "if {$line!=\"\"} {\n"
776 "puts -nonewline \"> \"\n"
777 "} else {\n"
778 "puts -nonewline \"% \"\n"
779 "}\n"
780 "flush stdout\n"
781 "append line [gets stdin]\n"
782 "if {[info complete $line]} {\n"
783 "if {[catch {uplevel #0 $line} result]} {\n"
784 "puts stderr \"Error: $result\"\n"
785 "} elseif {$result!=\"\"} {\n"
786 "puts $result\n"
787 "}\n"
788 "set line {}\n"
789 "} else {\n"
790 "append line \\n\n"
791 "}\n"
792 "}\n"
793;
794
795#define TCLSH_MAIN main /* Needed to fake out mktclapp */
796int TCLSH_MAIN(int argc, char **argv){
797 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +0000798 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +0000799 interp = Tcl_CreateInterp();
800 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +0000801#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +0000802 {
803 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +0000804 extern int Sqlitetest2_Init(Tcl_Interp*);
805 extern int Sqlitetest3_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +0000806 extern int Md5_Init(Tcl_Interp*);
drhd1bf3512001-04-07 15:24:33 +0000807 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +0000808 Sqlitetest2_Init(interp);
809 Sqlitetest3_Init(interp);
drhefc251d2001-07-01 22:12:01 +0000810 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +0000811 }
812#endif
drh348784e2000-05-29 20:41:49 +0000813 if( argc>=2 ){
814 int i;
815 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
816 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
817 for(i=2; i<argc; i++){
818 Tcl_SetVar(interp, "argv", argv[i],
819 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
820 }
821 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +0000822 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +0000823 if( zInfo==0 ) zInfo = interp->result;
824 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +0000825 return 1;
826 }
827 }else{
828 Tcl_GlobalEval(interp, zMainloop);
829 }
830 return 0;
831}
832#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +0000833
834#endif /* !defined(NO_TCL) */