blob: dd21cad4e30328fdc317ed1008c434a0395efd86 [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**
drh0d1a6432003-04-03 15:46:04 +000014** $Id: tclsqlite.c,v 1.46 2003/04/03 15:46:04 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 */
drh0d1a6432003-04-03 15:46:04 +000054 char *zBegin; /* The begin-transaction callback routine */
55 char *zCommit; /* The commit-transaction callback routine */
drhcabb0812002-09-14 13:47:32 +000056 SqlFunc *pFunc; /* List of SQL functions */
drhdcd997e2003-01-31 17:21:49 +000057 int rc; /* Return code of most recent sqlite_exec() */
drhbec3f402000-08-04 13:49:02 +000058};
59
60/*
drh75897232000-05-29 14:26:00 +000061** An instance of this structure passes information thru the sqlite
62** logic from the original TCL command into the callback routine.
63*/
64typedef struct CallbackData CallbackData;
65struct CallbackData {
66 Tcl_Interp *interp; /* The TCL interpreter */
67 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000068 Tcl_Obj *pCode; /* The code to execute for each row */
drhce927062001-11-09 13:41:09 +000069 int once; /* Set for first callback only */
drh960e8c62001-04-03 16:53:21 +000070 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000071 int nColName; /* Number of entries in the azColName[] array */
72 char **azColName; /* Column names translated to UTF-8 */
drh98808ba2001-10-18 12:34:46 +000073};
drh297ecf12001-04-05 15:57:13 +000074
drh6d4abfb2001-10-22 02:58:08 +000075#ifdef UTF_TRANSLATION_NEEDED
drh297ecf12001-04-05 15:57:13 +000076/*
drh75897232000-05-29 14:26:00 +000077** Called for each row of the result.
drh6d4abfb2001-10-22 02:58:08 +000078**
79** This version is used when TCL expects UTF-8 data but the database
80** uses the ISO8859 format. A translation must occur from ISO8859 into
81** UTF-8.
drh75897232000-05-29 14:26:00 +000082*/
83static int DbEvalCallback(
84 void *clientData, /* An instance of CallbackData */
85 int nCol, /* Number of columns in the result */
86 char ** azCol, /* Data for each column */
87 char ** azN /* Name for each column */
88){
89 CallbackData *cbData = (CallbackData*)clientData;
90 int i, rc;
drh297ecf12001-04-05 15:57:13 +000091 Tcl_DString dCol;
drh6d4abfb2001-10-22 02:58:08 +000092 Tcl_DStringInit(&dCol);
drhce927062001-11-09 13:41:09 +000093 if( cbData->azColName==0 ){
94 assert( cbData->once );
95 cbData->once = 0;
96 if( cbData->zArray[0] ){
97 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
drh6d4abfb2001-10-22 02:58:08 +000098 }
drhce927062001-11-09 13:41:09 +000099 cbData->azColName = malloc( nCol*sizeof(char*) );
100 if( cbData->azColName==0 ){ return 1; }
drh6d4abfb2001-10-22 02:58:08 +0000101 cbData->nColName = nCol;
102 for(i=0; i<nCol; i++){
103 Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol);
drhce927062001-11-09 13:41:09 +0000104 cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 );
105 if( cbData->azColName[i] ){
106 strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol));
107 }else{
108 return 1;
drh6d4abfb2001-10-22 02:58:08 +0000109 }
drhce927062001-11-09 13:41:09 +0000110 if( cbData->zArray[0] ){
111 Tcl_SetVar2(cbData->interp, cbData->zArray, "*",
112 Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh704027f2002-07-15 20:58:47 +0000113 if( azN[nCol]!=0 ){
drh5080aaa2002-07-11 12:18:16 +0000114 Tcl_DString dType;
115 Tcl_DStringInit(&dType);
116 Tcl_DStringAppend(&dType, "typeof:", -1);
117 Tcl_DStringAppend(&dType, Tcl_DStringValue(&dCol), -1);
118 Tcl_DStringFree(&dCol);
119 Tcl_ExternalToUtfDString(NULL, azN[i+nCol], -1, &dCol);
120 Tcl_SetVar2(cbData->interp, cbData->zArray,
121 Tcl_DStringValue(&dType), Tcl_DStringValue(&dCol),
122 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
123 Tcl_DStringFree(&dType);
124 }
drhce927062001-11-09 13:41:09 +0000125 }
drhfa173a72002-07-10 21:26:00 +0000126
drh6d4abfb2001-10-22 02:58:08 +0000127 Tcl_DStringFree(&dCol);
128 }
drh6d4abfb2001-10-22 02:58:08 +0000129 }
130 if( azCol!=0 ){
131 if( cbData->zArray[0] ){
132 for(i=0; i<nCol; i++){
133 char *z = azCol[i];
134 if( z==0 ) z = "";
135 Tcl_DStringInit(&dCol);
136 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
137 Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i],
138 Tcl_DStringValue(&dCol), 0);
139 Tcl_DStringFree(&dCol);
140 }
141 }else{
142 for(i=0; i<nCol; i++){
143 char *z = azCol[i];
144 if( z==0 ) z = "";
145 Tcl_DStringInit(&dCol);
146 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
147 Tcl_SetVar(cbData->interp, cbData->azColName[i],
148 Tcl_DStringValue(&dCol), 0);
149 Tcl_DStringFree(&dCol);
150 }
151 }
152 }
153 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
154 if( rc==TCL_CONTINUE ) rc = TCL_OK;
155 cbData->tcl_rc = rc;
156 return rc!=TCL_OK;
157}
158#endif /* UTF_TRANSLATION_NEEDED */
159
160#ifndef UTF_TRANSLATION_NEEDED
161/*
162** Called for each row of the result.
163**
164** This version is used when either of the following is true:
165**
166** (1) This version of TCL uses UTF-8 and the data in the
167** SQLite database is already in the UTF-8 format.
168**
169** (2) This version of TCL uses ISO8859 and the data in the
170** SQLite database is already in the ISO8859 format.
171*/
172static int DbEvalCallback(
173 void *clientData, /* An instance of CallbackData */
174 int nCol, /* Number of columns in the result */
175 char ** azCol, /* Data for each column */
176 char ** azN /* Name for each column */
177){
178 CallbackData *cbData = (CallbackData*)clientData;
179 int i, rc;
drh6a535342001-10-19 16:44:56 +0000180 if( azCol==0 || (cbData->once && cbData->zArray[0]) ){
181 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
182 for(i=0; i<nCol; i++){
183 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
184 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh5080aaa2002-07-11 12:18:16 +0000185 if( azN[nCol] ){
186 char *z = sqlite_mprintf("typeof:%s", azN[i]);
187 Tcl_SetVar2(cbData->interp, cbData->zArray, z, azN[i+nCol],
188 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
189 sqlite_freemem(z);
190 }
drh6a535342001-10-19 16:44:56 +0000191 }
192 cbData->once = 0;
193 }
194 if( azCol!=0 ){
195 if( cbData->zArray[0] ){
drh75897232000-05-29 14:26:00 +0000196 for(i=0; i<nCol; i++){
drh6a535342001-10-19 16:44:56 +0000197 char *z = azCol[i];
198 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000199 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
drh6a535342001-10-19 16:44:56 +0000200 }
201 }else{
202 for(i=0; i<nCol; i++){
203 char *z = azCol[i];
204 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000205 Tcl_SetVar(cbData->interp, azN[i], z, 0);
drh75897232000-05-29 14:26:00 +0000206 }
207 }
drh75897232000-05-29 14:26:00 +0000208 }
drh6d313162000-09-21 13:01:35 +0000209 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
drh960e8c62001-04-03 16:53:21 +0000210 if( rc==TCL_CONTINUE ) rc = TCL_OK;
211 cbData->tcl_rc = rc;
212 return rc!=TCL_OK;
drh75897232000-05-29 14:26:00 +0000213}
drh6d4abfb2001-10-22 02:58:08 +0000214#endif
drh75897232000-05-29 14:26:00 +0000215
216/*
drh6d313162000-09-21 13:01:35 +0000217** This is an alternative callback for database queries. Instead
218** of invoking a TCL script to handle the result, this callback just
219** appends each column of the result to a list. After the query
220** is complete, the list is returned.
221*/
222static int DbEvalCallback2(
223 void *clientData, /* An instance of CallbackData */
224 int nCol, /* Number of columns in the result */
225 char ** azCol, /* Data for each column */
226 char ** azN /* Name for each column */
227){
228 Tcl_Obj *pList = (Tcl_Obj*)clientData;
229 int i;
drh6a535342001-10-19 16:44:56 +0000230 if( azCol==0 ) return 0;
drh6d313162000-09-21 13:01:35 +0000231 for(i=0; i<nCol; i++){
232 Tcl_Obj *pElem;
233 if( azCol[i] && *azCol[i] ){
drh297ecf12001-04-05 15:57:13 +0000234#ifdef UTF_TRANSLATION_NEEDED
235 Tcl_DString dCol;
236 Tcl_DStringInit(&dCol);
237 Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol);
238 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
239 Tcl_DStringFree(&dCol);
240#else
drh6d313162000-09-21 13:01:35 +0000241 pElem = Tcl_NewStringObj(azCol[i], -1);
drh297ecf12001-04-05 15:57:13 +0000242#endif
drh6d313162000-09-21 13:01:35 +0000243 }else{
244 pElem = Tcl_NewObj();
245 }
246 Tcl_ListObjAppendElement(0, pList, pElem);
247 }
248 return 0;
249}
250
251/*
drh75897232000-05-29 14:26:00 +0000252** Called when the command is deleted.
253*/
254static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000255 SqliteDb *pDb = (SqliteDb*)db;
256 sqlite_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000257 while( pDb->pFunc ){
258 SqlFunc *pFunc = pDb->pFunc;
259 pDb->pFunc = pFunc->pNext;
260 Tcl_Free((char*)pFunc);
261 }
drhbec3f402000-08-04 13:49:02 +0000262 if( pDb->zBusy ){
263 Tcl_Free(pDb->zBusy);
264 }
drh0d1a6432003-04-03 15:46:04 +0000265 if( pDb->zBegin ){
266 Tcl_Free(pDb->zBegin);
267 }
268 if( pDb->zCommit ){
269 Tcl_Free(pDb->zCommit);
270 }
drhbec3f402000-08-04 13:49:02 +0000271 Tcl_Free((char*)pDb);
272}
273
274/*
275** This routine is called when a database file is locked while trying
276** to execute SQL.
277*/
278static int DbBusyHandler(void *cd, const char *zTable, int nTries){
279 SqliteDb *pDb = (SqliteDb*)cd;
280 int rc;
281 char zVal[30];
282 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000283 Tcl_DString cmd;
284
285 Tcl_DStringInit(&cmd);
286 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
287 Tcl_DStringAppendElement(&cmd, zTable);
288 sprintf(zVal, " %d", nTries);
289 Tcl_DStringAppend(&cmd, zVal, -1);
290 zCmd = Tcl_DStringValue(&cmd);
291 rc = Tcl_Eval(pDb->interp, zCmd);
292 Tcl_DStringFree(&cmd);
293 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
294 return 0;
295 }
296 return 1;
drh75897232000-05-29 14:26:00 +0000297}
298
299/*
drh0d1a6432003-04-03 15:46:04 +0000300** This routine is called when a new transaction is started. The
301** TCL script in pDb->zBegin is executed. If it returns non-zero or
302** if it throws an exception, the transaction is aborted.
303*/
304static int DbBeginHandler(void *cd){
305 SqliteDb *pDb = (SqliteDb*)cd;
306 int rc;
307
308 rc = Tcl_Eval(pDb->interp, pDb->zBegin);
309 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
310 return 1;
311 }
312 return 0;
313}
314
315/*
316** This routine is called when a transaction is committed. The
317** TCL script in pDb->zCommit is executed. If it returns non-zero or
318** if it throws an exception, the transaction is rolled back instead
319** of being committed.
320*/
321static int DbCommitHandler(void *cd){
322 SqliteDb *pDb = (SqliteDb*)cd;
323 int rc;
324
325 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
326 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
327 return 1;
328 }
329 return 0;
330}
331
332/*
drhcabb0812002-09-14 13:47:32 +0000333** This routine is called to evaluate an SQL function implemented
334** using TCL script.
335*/
336static void tclSqlFunc(sqlite_func *context, int argc, const char **argv){
337 SqlFunc *p = sqlite_user_data(context);
338 Tcl_DString cmd;
339 int i;
340 int rc;
341
342 Tcl_DStringInit(&cmd);
343 Tcl_DStringAppend(&cmd, p->zScript, -1);
344 for(i=0; i<argc; i++){
345 Tcl_DStringAppendElement(&cmd, argv[i] ? argv[i] : "");
346 }
347 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
348 if( rc ){
349 sqlite_set_result_error(context, Tcl_GetStringResult(p->interp), -1);
350 }else{
351 sqlite_set_result_string(context, Tcl_GetStringResult(p->interp), -1);
352 }
353}
354
355/*
drh75897232000-05-29 14:26:00 +0000356** The "sqlite" command below creates a new Tcl command for each
357** connection it opens to an SQLite database. This routine is invoked
358** whenever one of those connection-specific commands is executed
359** in Tcl. For example, if you run Tcl code like this:
360**
361** sqlite db1 "my_database"
362** db1 close
363**
364** The first command opens a connection to the "my_database" database
365** and calls that connection "db1". The second command causes this
366** subroutine to be invoked.
367*/
drh6d313162000-09-21 13:01:35 +0000368static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000369 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000370 int choice;
drh0de8c112002-07-06 16:32:14 +0000371 static const char *DB_strs[] = {
drh0d1a6432003-04-03 15:46:04 +0000372 "begin_hook", "busy", "changes",
373 "close", "commit_hook", "complete",
374 "errorcode", "eval", "function",
375 "last_insert_rowid", "timeout", 0
drh6d313162000-09-21 13:01:35 +0000376 };
drh411995d2002-06-25 19:31:18 +0000377 enum DB_enum {
drh0d1a6432003-04-03 15:46:04 +0000378 DB_BEGIN_HOOK, DB_BUSY, DB_CHANGES,
379 DB_CLOSE, DB_COMMIT_HOOK, DB_COMPLETE,
380 DB_ERRORCODE, DB_EVAL, DB_FUNCTION,
381 DB_LAST_INSERT_ROWID, DB_TIMEOUT,
drh6d313162000-09-21 13:01:35 +0000382 };
383
384 if( objc<2 ){
385 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000386 return TCL_ERROR;
387 }
drh411995d2002-06-25 19:31:18 +0000388 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000389 return TCL_ERROR;
390 }
391
drh411995d2002-06-25 19:31:18 +0000392 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000393
drh0d1a6432003-04-03 15:46:04 +0000394 /* $db begin_callback ?CALLBACK?
395 **
396 ** Invoke the given callback at the beginning of every SQL transaction.
397 ** If the callback throws an exception or returns non-zero, then the
398 ** transaction is aborted. If CALLBACK is an empty string, the callback
399 ** is disabled.
400 */
401 case DB_BEGIN_HOOK: {
402 if( objc>3 ){
403 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
404 }else if( objc==2 ){
405 if( pDb->zBegin ){
406 Tcl_AppendResult(interp, pDb->zBegin, 0);
407 }
408 }else{
409 char *zBegin;
410 int len;
411 if( pDb->zBegin ){
412 Tcl_Free(pDb->zBegin);
413 }
414 zBegin = Tcl_GetStringFromObj(objv[2], &len);
415 if( zBegin && len>0 ){
416 pDb->zBegin = Tcl_Alloc( len + 1 );
417 strcpy(pDb->zBegin, zBegin);
418 }else{
419 pDb->zBegin = 0;
420 }
421 if( pDb->zBegin ){
422 pDb->interp = interp;
423 sqlite_begin_hook(pDb->db, DbBeginHandler, pDb);
424 }else{
425 sqlite_begin_hook(pDb->db, 0, 0);
426 }
427 }
428 break;
429 }
430
drhbec3f402000-08-04 13:49:02 +0000431 /* $db busy ?CALLBACK?
432 **
433 ** Invoke the given callback if an SQL statement attempts to open
434 ** a locked database file.
435 */
drh6d313162000-09-21 13:01:35 +0000436 case DB_BUSY: {
437 if( objc>3 ){
438 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000439 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000440 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000441 if( pDb->zBusy ){
442 Tcl_AppendResult(interp, pDb->zBusy, 0);
443 }
444 }else{
drh6d313162000-09-21 13:01:35 +0000445 char *zBusy;
446 int len;
drhbec3f402000-08-04 13:49:02 +0000447 if( pDb->zBusy ){
448 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000449 }
drh6d313162000-09-21 13:01:35 +0000450 zBusy = Tcl_GetStringFromObj(objv[2], &len);
451 if( zBusy && len>0 ){
452 pDb->zBusy = Tcl_Alloc( len + 1 );
453 strcpy(pDb->zBusy, zBusy);
454 }else{
455 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000456 }
457 if( pDb->zBusy ){
458 pDb->interp = interp;
459 sqlite_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000460 }else{
461 sqlite_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000462 }
463 }
drh6d313162000-09-21 13:01:35 +0000464 break;
465 }
drhbec3f402000-08-04 13:49:02 +0000466
drhc8d30ac2002-04-12 10:08:59 +0000467 /*
468 ** $db changes
469 **
470 ** Return the number of rows that were modified, inserted, or deleted by
471 ** the most recent "eval".
472 */
473 case DB_CHANGES: {
474 Tcl_Obj *pResult;
475 int nChange;
476 if( objc!=2 ){
477 Tcl_WrongNumArgs(interp, 2, objv, "");
478 return TCL_ERROR;
479 }
480 nChange = sqlite_changes(pDb->db);
481 pResult = Tcl_GetObjResult(interp);
482 Tcl_SetIntObj(pResult, nChange);
483 break;
484 }
485
drh75897232000-05-29 14:26:00 +0000486 /* $db close
487 **
488 ** Shutdown the database
489 */
drh6d313162000-09-21 13:01:35 +0000490 case DB_CLOSE: {
491 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
492 break;
493 }
drh75897232000-05-29 14:26:00 +0000494
drh0d1a6432003-04-03 15:46:04 +0000495 /* $db commit_hook ?CALLBACK?
496 **
497 ** Invoke the given callback just before committing every SQL transaction.
498 ** If the callback throws an exception or returns non-zero, then the
499 ** transaction is aborted. If CALLBACK is an empty string, the callback
500 ** is disabled.
501 */
502 case DB_COMMIT_HOOK: {
503 if( objc>3 ){
504 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
505 }else if( objc==2 ){
506 if( pDb->zCommit ){
507 Tcl_AppendResult(interp, pDb->zCommit, 0);
508 }
509 }else{
510 char *zCommit;
511 int len;
512 if( pDb->zCommit ){
513 Tcl_Free(pDb->zCommit);
514 }
515 zCommit = Tcl_GetStringFromObj(objv[2], &len);
516 if( zCommit && len>0 ){
517 pDb->zCommit = Tcl_Alloc( len + 1 );
518 strcpy(pDb->zCommit, zCommit);
519 }else{
520 pDb->zCommit = 0;
521 }
522 if( pDb->zCommit ){
523 pDb->interp = interp;
524 sqlite_commit_hook(pDb->db, DbCommitHandler, pDb);
525 }else{
526 sqlite_commit_hook(pDb->db, 0, 0);
527 }
528 }
529 break;
530 }
531
drh75897232000-05-29 14:26:00 +0000532 /* $db complete SQL
533 **
534 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
535 ** additional lines of input are needed. This is similar to the
536 ** built-in "info complete" command of Tcl.
537 */
drh6d313162000-09-21 13:01:35 +0000538 case DB_COMPLETE: {
539 Tcl_Obj *pResult;
540 int isComplete;
541 if( objc!=3 ){
542 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000543 return TCL_ERROR;
544 }
drh6d313162000-09-21 13:01:35 +0000545 isComplete = sqlite_complete( Tcl_GetStringFromObj(objv[2], 0) );
546 pResult = Tcl_GetObjResult(interp);
547 Tcl_SetBooleanObj(pResult, isComplete);
548 break;
549 }
drhdcd997e2003-01-31 17:21:49 +0000550
551 /*
552 ** $db errorcode
553 **
554 ** Return the numeric error code that was returned by the most recent
555 ** call to sqlite_exec().
556 */
557 case DB_ERRORCODE: {
558 Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc));
559 break;
560 }
drh75897232000-05-29 14:26:00 +0000561
562 /*
563 ** $db eval $sql ?array { ...code... }?
564 **
565 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000566 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000567 ** If "array" and "code" are omitted, then no callback is every invoked.
568 ** If "array" is an empty string, then the values are placed in variables
569 ** that have the same name as the fields extracted by the query.
570 */
drh6d313162000-09-21 13:01:35 +0000571 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000572 CallbackData cbData;
573 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000574 char *zSql;
drh75897232000-05-29 14:26:00 +0000575 int rc;
drh297ecf12001-04-05 15:57:13 +0000576#ifdef UTF_TRANSLATION_NEEDED
577 Tcl_DString dSql;
drh6d4abfb2001-10-22 02:58:08 +0000578 int i;
drh297ecf12001-04-05 15:57:13 +0000579#endif
drh75897232000-05-29 14:26:00 +0000580
drh6d313162000-09-21 13:01:35 +0000581 if( objc!=5 && objc!=3 ){
582 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000583 return TCL_ERROR;
584 }
drhbec3f402000-08-04 13:49:02 +0000585 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000586 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh297ecf12001-04-05 15:57:13 +0000587#ifdef UTF_TRANSLATION_NEEDED
588 Tcl_DStringInit(&dSql);
589 Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
590 zSql = Tcl_DStringValue(&dSql);
591#endif
drh6d313162000-09-21 13:01:35 +0000592 Tcl_IncrRefCount(objv[2]);
593 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000594 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000595 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000596 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
597 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000598 cbData.tcl_rc = TCL_OK;
drh6d4abfb2001-10-22 02:58:08 +0000599 cbData.nColName = 0;
600 cbData.azColName = 0;
drh75897232000-05-29 14:26:00 +0000601 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000602 Tcl_IncrRefCount(objv[3]);
603 Tcl_IncrRefCount(objv[4]);
604 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
605 Tcl_DecrRefCount(objv[4]);
606 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000607 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000608 }else{
drh6d313162000-09-21 13:01:35 +0000609 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000610 cbData.tcl_rc = TCL_OK;
drh6d313162000-09-21 13:01:35 +0000611 rc = sqlite_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
612 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000613 }
drhdcd997e2003-01-31 17:21:49 +0000614 pDb->rc = rc;
drhb798fa62002-09-03 19:43:23 +0000615 if( rc==SQLITE_ABORT ){
616 if( zErrMsg ) free(zErrMsg);
617 rc = cbData.tcl_rc;
618 }else if( zErrMsg ){
drh75897232000-05-29 14:26:00 +0000619 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
620 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000621 rc = TCL_ERROR;
drhb798fa62002-09-03 19:43:23 +0000622 }else if( rc!=SQLITE_OK ){
drh6d4abfb2001-10-22 02:58:08 +0000623 Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
624 rc = TCL_ERROR;
drh960e8c62001-04-03 16:53:21 +0000625 }else{
drh75897232000-05-29 14:26:00 +0000626 }
drh6d313162000-09-21 13:01:35 +0000627 Tcl_DecrRefCount(objv[2]);
drh297ecf12001-04-05 15:57:13 +0000628#ifdef UTF_TRANSLATION_NEEDED
629 Tcl_DStringFree(&dSql);
drh6d4abfb2001-10-22 02:58:08 +0000630 if( objc==5 && cbData.azColName ){
631 for(i=0; i<cbData.nColName; i++){
632 if( cbData.azColName[i] ) free(cbData.azColName[i]);
633 }
634 free(cbData.azColName);
drhce927062001-11-09 13:41:09 +0000635 cbData.azColName = 0;
drh6d4abfb2001-10-22 02:58:08 +0000636 }
drh297ecf12001-04-05 15:57:13 +0000637#endif
drh75897232000-05-29 14:26:00 +0000638 return rc;
drh6d313162000-09-21 13:01:35 +0000639 }
drhbec3f402000-08-04 13:49:02 +0000640
641 /*
drhcabb0812002-09-14 13:47:32 +0000642 ** $db function NAME SCRIPT
643 **
644 ** Create a new SQL function called NAME. Whenever that function is
645 ** called, invoke SCRIPT to evaluate the function.
646 */
647 case DB_FUNCTION: {
648 SqlFunc *pFunc;
649 char *zName;
650 char *zScript;
651 int nScript;
652 if( objc!=4 ){
653 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
654 return TCL_ERROR;
655 }
656 zName = Tcl_GetStringFromObj(objv[2], 0);
657 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
658 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
659 if( pFunc==0 ) return TCL_ERROR;
660 pFunc->interp = interp;
661 pFunc->pNext = pDb->pFunc;
662 pFunc->zScript = (char*)&pFunc[1];
663 strcpy(pFunc->zScript, zScript);
664 sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc);
665 sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC);
666 break;
667 }
668
669 /*
drhaf9ff332002-01-16 21:00:27 +0000670 ** $db last_insert_rowid
671 **
672 ** Return an integer which is the ROWID for the most recent insert.
673 */
674 case DB_LAST_INSERT_ROWID: {
675 Tcl_Obj *pResult;
676 int rowid;
677 if( objc!=2 ){
678 Tcl_WrongNumArgs(interp, 2, objv, "");
679 return TCL_ERROR;
680 }
681 rowid = sqlite_last_insert_rowid(pDb->db);
682 pResult = Tcl_GetObjResult(interp);
683 Tcl_SetIntObj(pResult, rowid);
684 break;
685 }
686
687 /*
drhbec3f402000-08-04 13:49:02 +0000688 ** $db timeout MILLESECONDS
689 **
690 ** Delay for the number of milliseconds specified when a file is locked.
691 */
drh6d313162000-09-21 13:01:35 +0000692 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000693 int ms;
drh6d313162000-09-21 13:01:35 +0000694 if( objc!=3 ){
695 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000696 return TCL_ERROR;
697 }
drh6d313162000-09-21 13:01:35 +0000698 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
drhbec3f402000-08-04 13:49:02 +0000699 sqlite_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000700 break;
drh75897232000-05-29 14:26:00 +0000701 }
drh6d313162000-09-21 13:01:35 +0000702 } /* End of the SWITCH statement */
drh75897232000-05-29 14:26:00 +0000703 return TCL_OK;
704}
705
706/*
707** sqlite DBNAME FILENAME ?MODE?
708**
709** This is the main Tcl command. When the "sqlite" Tcl command is
710** invoked, this routine runs to process that command.
711**
712** The first argument, DBNAME, is an arbitrary name for a new
713** database connection. This command creates a new command named
714** DBNAME that is used to control that connection. The database
715** connection is deleted when the DBNAME command is deleted.
716**
717** The second argument is the name of the directory that contains
718** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000719**
720** For testing purposes, we also support the following:
721**
722** sqlite -encoding
723**
724** Return the encoding used by LIKE and GLOB operators. Choices
725** are UTF-8 and iso8859.
726**
drh647cb0e2002-11-04 19:32:25 +0000727** sqlite -version
728**
729** Return the version number of the SQLite library.
730**
drhfbc3eab2001-04-06 16:13:42 +0000731** sqlite -tcl-uses-utf
732**
733** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
734** not. Used by tests to make sure the library was compiled
735** correctly.
drh75897232000-05-29 14:26:00 +0000736*/
737static int DbMain(void *cd, Tcl_Interp *interp, int argc, char **argv){
738 int mode;
drhbec3f402000-08-04 13:49:02 +0000739 SqliteDb *p;
drh75897232000-05-29 14:26:00 +0000740 char *zErrMsg;
drh06b27182002-06-26 20:06:05 +0000741 char zBuf[80];
drhfbc3eab2001-04-06 16:13:42 +0000742 if( argc==2 ){
743 if( strcmp(argv[1],"-encoding")==0 ){
744 Tcl_AppendResult(interp,sqlite_encoding,0);
745 return TCL_OK;
746 }
drh647cb0e2002-11-04 19:32:25 +0000747 if( strcmp(argv[1],"-version")==0 ){
748 Tcl_AppendResult(interp,sqlite_version,0);
749 return TCL_OK;
750 }
drhfbc3eab2001-04-06 16:13:42 +0000751 if( strcmp(argv[1],"-tcl-uses-utf")==0 ){
752#ifdef TCL_UTF_MAX
753 Tcl_AppendResult(interp,"1",0);
754#else
755 Tcl_AppendResult(interp,"0",0);
756#endif
757 return TCL_OK;
758 }
759 }
drh75897232000-05-29 14:26:00 +0000760 if( argc!=3 && argc!=4 ){
761 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
762 " HANDLE FILENAME ?MODE?\"", 0);
763 return TCL_ERROR;
764 }
765 if( argc==3 ){
drh58b95762000-06-02 01:17:37 +0000766 mode = 0666;
drh75897232000-05-29 14:26:00 +0000767 }else if( Tcl_GetInt(interp, argv[3], &mode)!=TCL_OK ){
768 return TCL_ERROR;
769 }
770 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +0000771 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +0000772 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +0000773 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
774 return TCL_ERROR;
775 }
776 memset(p, 0, sizeof(*p));
777 p->db = sqlite_open(argv[2], mode, &zErrMsg);
778 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +0000779 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +0000780 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +0000781 free(zErrMsg);
782 return TCL_ERROR;
783 }
drh6d313162000-09-21 13:01:35 +0000784 Tcl_CreateObjCommand(interp, argv[1], DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +0000785
drh06b27182002-06-26 20:06:05 +0000786 /* The return value is the value of the sqlite* pointer
787 */
788 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +0000789 if( strncmp(zBuf,"0x",2) ){
790 sprintf(zBuf, "0x%p", p->db);
791 }
drh06b27182002-06-26 20:06:05 +0000792 Tcl_AppendResult(interp, zBuf, 0);
793
drhc22bd472002-05-10 13:14:07 +0000794 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +0000795 ** SQL function.
drhc22bd472002-05-10 13:14:07 +0000796 */
drh28b4e482002-03-11 02:06:13 +0000797#ifdef SQLITE_TEST
798 {
drhc22bd472002-05-10 13:14:07 +0000799 extern void Md5_Register(sqlite*);
800 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +0000801 }
drh28b4e482002-03-11 02:06:13 +0000802#endif
drh75897232000-05-29 14:26:00 +0000803 return TCL_OK;
804}
805
806/*
drh90ca9752001-09-28 17:47:14 +0000807** Provide a dummy Tcl_InitStubs if we are using this as a static
808** library.
809*/
810#ifndef USE_TCL_STUBS
811# undef Tcl_InitStubs
812# define Tcl_InitStubs(a,b,c)
813#endif
814
815/*
drh75897232000-05-29 14:26:00 +0000816** Initialize this module.
817**
818** This Tcl module contains only a single new Tcl command named "sqlite".
819** (Hence there is no namespace. There is no point in using a namespace
820** if the extension only supplies one new name!) The "sqlite" command is
821** used to open a new SQLite database. See the DbMain() routine above
822** for additional information.
823*/
824int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +0000825 Tcl_InitStubs(interp, "8.0", 0);
drhc2eef3b2002-08-31 18:53:06 +0000826 Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000827 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh90ca9752001-09-28 17:47:14 +0000828 return TCL_OK;
829}
830int Tclsqlite_Init(Tcl_Interp *interp){
831 Tcl_InitStubs(interp, "8.0", 0);
drhc2eef3b2002-08-31 18:53:06 +0000832 Tcl_CreateCommand(interp, "sqlite", (Tcl_CmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000833 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh75897232000-05-29 14:26:00 +0000834 return TCL_OK;
835}
836int Sqlite_SafeInit(Tcl_Interp *interp){
837 return TCL_OK;
838}
drh90ca9752001-09-28 17:47:14 +0000839int Tclsqlite_SafeInit(Tcl_Interp *interp){
840 return TCL_OK;
841}
drh75897232000-05-29 14:26:00 +0000842
drh3cebbde2000-10-19 14:59:27 +0000843#if 0
drh75897232000-05-29 14:26:00 +0000844/*
845** If compiled using mktclapp, this routine runs to initialize
846** everything.
847*/
848int Et_AppInit(Tcl_Interp *interp){
849 return Sqlite_Init(interp);
850}
drh3cebbde2000-10-19 14:59:27 +0000851#endif
drh348784e2000-05-29 20:41:49 +0000852
853/*
854** If the macro TCLSH is defined and is one, then put in code for the
855** "main" routine that will initialize Tcl.
856*/
857#if defined(TCLSH) && TCLSH==1
858static char zMainloop[] =
859 "set line {}\n"
860 "while {![eof stdin]} {\n"
861 "if {$line!=\"\"} {\n"
862 "puts -nonewline \"> \"\n"
863 "} else {\n"
864 "puts -nonewline \"% \"\n"
865 "}\n"
866 "flush stdout\n"
867 "append line [gets stdin]\n"
868 "if {[info complete $line]} {\n"
869 "if {[catch {uplevel #0 $line} result]} {\n"
870 "puts stderr \"Error: $result\"\n"
871 "} elseif {$result!=\"\"} {\n"
872 "puts $result\n"
873 "}\n"
874 "set line {}\n"
875 "} else {\n"
876 "append line \\n\n"
877 "}\n"
878 "}\n"
879;
880
881#define TCLSH_MAIN main /* Needed to fake out mktclapp */
882int TCLSH_MAIN(int argc, char **argv){
883 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +0000884 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +0000885 interp = Tcl_CreateInterp();
886 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +0000887#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +0000888 {
889 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +0000890 extern int Sqlitetest2_Init(Tcl_Interp*);
891 extern int Sqlitetest3_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +0000892 extern int Md5_Init(Tcl_Interp*);
drhd1bf3512001-04-07 15:24:33 +0000893 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +0000894 Sqlitetest2_Init(interp);
895 Sqlitetest3_Init(interp);
drhefc251d2001-07-01 22:12:01 +0000896 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +0000897 }
898#endif
drh348784e2000-05-29 20:41:49 +0000899 if( argc>=2 ){
900 int i;
901 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
902 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
903 for(i=2; i<argc; i++){
904 Tcl_SetVar(interp, "argv", argv[i],
905 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
906 }
907 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +0000908 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +0000909 if( zInfo==0 ) zInfo = interp->result;
910 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +0000911 return 1;
912 }
913 }else{
914 Tcl_GlobalEval(interp, zMainloop);
915 }
916 return 0;
917}
918#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +0000919
920#endif /* !defined(NO_TCL) */