blob: a8dc7c09ba24a90674adbd7f22393ff5fa4d247d [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**
danielk197730ccda12004-05-27 12:11:31 +000014** $Id: tclsqlite.c,v 1.74 2004/05/27 12:11:32 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000015*/
drh6d313162000-09-21 13:01:35 +000016#ifndef NO_TCL /* Omit this whole file if TCL is unavailable */
17
drh06b27182002-06-26 20:06:05 +000018#include "sqliteInt.h"
drh17a68932001-01-31 13:28:08 +000019#include "tcl.h"
drh75897232000-05-29 14:26:00 +000020#include <stdlib.h>
21#include <string.h>
drhce927062001-11-09 13:41:09 +000022#include <assert.h>
drh75897232000-05-29 14:26:00 +000023
24/*
drh98808ba2001-10-18 12:34:46 +000025** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
26** have to do a translation when going between the two. Set the
27** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
28** this translation.
29*/
30#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
31# define UTF_TRANSLATION_NEEDED 1
32#endif
33
34/*
drhcabb0812002-09-14 13:47:32 +000035** New SQL functions can be created as TCL scripts. Each such function
36** is described by an instance of the following structure.
37*/
38typedef struct SqlFunc SqlFunc;
39struct SqlFunc {
40 Tcl_Interp *interp; /* The TCL interpret to execute the function */
41 char *zScript; /* The script to be run */
42 SqlFunc *pNext; /* Next function on the list of them all */
43};
44
45/*
drhbec3f402000-08-04 13:49:02 +000046** There is one instance of this structure for each SQLite database
47** that has been opened by the SQLite TCL interface.
48*/
49typedef struct SqliteDb SqliteDb;
50struct SqliteDb {
51 sqlite *db; /* The "real" database structure */
52 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000053 char *zBusy; /* The busy callback routine */
drhaa940ea2004-01-15 02:44:03 +000054 char *zCommit; /* The commit hook callback routine */
drhb5a20d32003-04-23 12:25:23 +000055 char *zTrace; /* The trace callback routine */
danielk1977348bb5d2003-10-18 09:37:26 +000056 char *zProgress; /* The progress callback routine */
drhe22a3342003-04-22 20:30:37 +000057 char *zAuth; /* The authorization callback routine */
drhcabb0812002-09-14 13:47:32 +000058 SqlFunc *pFunc; /* List of SQL functions */
danielk19776f8a5032004-05-10 10:34:51 +000059 int rc; /* Return code of most recent sqlite3_exec() */
danielk197730ccda12004-05-27 12:11:31 +000060 int nChange; /* Database changes for the most recent eval */
drhbec3f402000-08-04 13:49:02 +000061};
62
63/*
drh75897232000-05-29 14:26:00 +000064** An instance of this structure passes information thru the sqlite
65** logic from the original TCL command into the callback routine.
66*/
67typedef struct CallbackData CallbackData;
68struct CallbackData {
69 Tcl_Interp *interp; /* The TCL interpreter */
70 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000071 Tcl_Obj *pCode; /* The code to execute for each row */
drhce927062001-11-09 13:41:09 +000072 int once; /* Set for first callback only */
drh960e8c62001-04-03 16:53:21 +000073 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000074 int nColName; /* Number of entries in the azColName[] array */
75 char **azColName; /* Column names translated to UTF-8 */
drh98808ba2001-10-18 12:34:46 +000076};
drh297ecf12001-04-05 15:57:13 +000077
drh6d4abfb2001-10-22 02:58:08 +000078#ifdef UTF_TRANSLATION_NEEDED
drh297ecf12001-04-05 15:57:13 +000079/*
drh75897232000-05-29 14:26:00 +000080** Called for each row of the result.
drh6d4abfb2001-10-22 02:58:08 +000081**
82** This version is used when TCL expects UTF-8 data but the database
83** uses the ISO8859 format. A translation must occur from ISO8859 into
84** UTF-8.
drh75897232000-05-29 14:26:00 +000085*/
86static int DbEvalCallback(
87 void *clientData, /* An instance of CallbackData */
88 int nCol, /* Number of columns in the result */
89 char ** azCol, /* Data for each column */
90 char ** azN /* Name for each column */
91){
92 CallbackData *cbData = (CallbackData*)clientData;
93 int i, rc;
drh297ecf12001-04-05 15:57:13 +000094 Tcl_DString dCol;
drh6d4abfb2001-10-22 02:58:08 +000095 Tcl_DStringInit(&dCol);
drhce927062001-11-09 13:41:09 +000096 if( cbData->azColName==0 ){
97 assert( cbData->once );
98 cbData->once = 0;
99 if( cbData->zArray[0] ){
100 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
drh6d4abfb2001-10-22 02:58:08 +0000101 }
drhce927062001-11-09 13:41:09 +0000102 cbData->azColName = malloc( nCol*sizeof(char*) );
103 if( cbData->azColName==0 ){ return 1; }
drh6d4abfb2001-10-22 02:58:08 +0000104 cbData->nColName = nCol;
105 for(i=0; i<nCol; i++){
106 Tcl_ExternalToUtfDString(NULL, azN[i], -1, &dCol);
drhce927062001-11-09 13:41:09 +0000107 cbData->azColName[i] = malloc( Tcl_DStringLength(&dCol) + 1 );
108 if( cbData->azColName[i] ){
109 strcpy(cbData->azColName[i], Tcl_DStringValue(&dCol));
110 }else{
111 return 1;
drh6d4abfb2001-10-22 02:58:08 +0000112 }
drhce927062001-11-09 13:41:09 +0000113 if( cbData->zArray[0] ){
114 Tcl_SetVar2(cbData->interp, cbData->zArray, "*",
115 Tcl_DStringValue(&dCol), TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh704027f2002-07-15 20:58:47 +0000116 if( azN[nCol]!=0 ){
drh5080aaa2002-07-11 12:18:16 +0000117 Tcl_DString dType;
118 Tcl_DStringInit(&dType);
119 Tcl_DStringAppend(&dType, "typeof:", -1);
120 Tcl_DStringAppend(&dType, Tcl_DStringValue(&dCol), -1);
121 Tcl_DStringFree(&dCol);
122 Tcl_ExternalToUtfDString(NULL, azN[i+nCol], -1, &dCol);
123 Tcl_SetVar2(cbData->interp, cbData->zArray,
124 Tcl_DStringValue(&dType), Tcl_DStringValue(&dCol),
125 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
126 Tcl_DStringFree(&dType);
127 }
drhce927062001-11-09 13:41:09 +0000128 }
drhfa173a72002-07-10 21:26:00 +0000129
drh6d4abfb2001-10-22 02:58:08 +0000130 Tcl_DStringFree(&dCol);
131 }
drh6d4abfb2001-10-22 02:58:08 +0000132 }
133 if( azCol!=0 ){
134 if( cbData->zArray[0] ){
135 for(i=0; i<nCol; i++){
136 char *z = azCol[i];
137 if( z==0 ) z = "";
138 Tcl_DStringInit(&dCol);
139 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
140 Tcl_SetVar2(cbData->interp, cbData->zArray, cbData->azColName[i],
141 Tcl_DStringValue(&dCol), 0);
142 Tcl_DStringFree(&dCol);
143 }
144 }else{
145 for(i=0; i<nCol; i++){
146 char *z = azCol[i];
147 if( z==0 ) z = "";
148 Tcl_DStringInit(&dCol);
149 Tcl_ExternalToUtfDString(NULL, z, -1, &dCol);
150 Tcl_SetVar(cbData->interp, cbData->azColName[i],
151 Tcl_DStringValue(&dCol), 0);
152 Tcl_DStringFree(&dCol);
153 }
154 }
155 }
156 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
157 if( rc==TCL_CONTINUE ) rc = TCL_OK;
158 cbData->tcl_rc = rc;
159 return rc!=TCL_OK;
160}
161#endif /* UTF_TRANSLATION_NEEDED */
162
163#ifndef UTF_TRANSLATION_NEEDED
164/*
165** Called for each row of the result.
166**
167** This version is used when either of the following is true:
168**
169** (1) This version of TCL uses UTF-8 and the data in the
170** SQLite database is already in the UTF-8 format.
171**
172** (2) This version of TCL uses ISO8859 and the data in the
173** SQLite database is already in the ISO8859 format.
174*/
175static int DbEvalCallback(
176 void *clientData, /* An instance of CallbackData */
177 int nCol, /* Number of columns in the result */
178 char ** azCol, /* Data for each column */
179 char ** azN /* Name for each column */
180){
181 CallbackData *cbData = (CallbackData*)clientData;
182 int i, rc;
drh6a535342001-10-19 16:44:56 +0000183 if( azCol==0 || (cbData->once && cbData->zArray[0]) ){
184 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", "", 0);
185 for(i=0; i<nCol; i++){
186 Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
187 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
drh5080aaa2002-07-11 12:18:16 +0000188 if( azN[nCol] ){
danielk19776f8a5032004-05-10 10:34:51 +0000189 char *z = sqlite3_mprintf("typeof:%s", azN[i]);
drh5080aaa2002-07-11 12:18:16 +0000190 Tcl_SetVar2(cbData->interp, cbData->zArray, z, azN[i+nCol],
191 TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
danielk19776f8a5032004-05-10 10:34:51 +0000192 sqlite3_freemem(z);
drh5080aaa2002-07-11 12:18:16 +0000193 }
drh6a535342001-10-19 16:44:56 +0000194 }
195 cbData->once = 0;
196 }
197 if( azCol!=0 ){
198 if( cbData->zArray[0] ){
drh75897232000-05-29 14:26:00 +0000199 for(i=0; i<nCol; i++){
drh6a535342001-10-19 16:44:56 +0000200 char *z = azCol[i];
201 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000202 Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
drh6a535342001-10-19 16:44:56 +0000203 }
204 }else{
205 for(i=0; i<nCol; i++){
206 char *z = azCol[i];
207 if( z==0 ) z = "";
drh6a535342001-10-19 16:44:56 +0000208 Tcl_SetVar(cbData->interp, azN[i], z, 0);
drh75897232000-05-29 14:26:00 +0000209 }
210 }
drh75897232000-05-29 14:26:00 +0000211 }
drh6d313162000-09-21 13:01:35 +0000212 rc = Tcl_EvalObj(cbData->interp, cbData->pCode);
drh960e8c62001-04-03 16:53:21 +0000213 if( rc==TCL_CONTINUE ) rc = TCL_OK;
214 cbData->tcl_rc = rc;
215 return rc!=TCL_OK;
drh75897232000-05-29 14:26:00 +0000216}
drh6d4abfb2001-10-22 02:58:08 +0000217#endif
drh75897232000-05-29 14:26:00 +0000218
219/*
drh6d313162000-09-21 13:01:35 +0000220** This is an alternative callback for database queries. Instead
221** of invoking a TCL script to handle the result, this callback just
222** appends each column of the result to a list. After the query
223** is complete, the list is returned.
224*/
225static int DbEvalCallback2(
226 void *clientData, /* An instance of CallbackData */
227 int nCol, /* Number of columns in the result */
228 char ** azCol, /* Data for each column */
229 char ** azN /* Name for each column */
230){
231 Tcl_Obj *pList = (Tcl_Obj*)clientData;
232 int i;
drh6a535342001-10-19 16:44:56 +0000233 if( azCol==0 ) return 0;
drh6d313162000-09-21 13:01:35 +0000234 for(i=0; i<nCol; i++){
235 Tcl_Obj *pElem;
236 if( azCol[i] && *azCol[i] ){
drh297ecf12001-04-05 15:57:13 +0000237#ifdef UTF_TRANSLATION_NEEDED
238 Tcl_DString dCol;
239 Tcl_DStringInit(&dCol);
240 Tcl_ExternalToUtfDString(NULL, azCol[i], -1, &dCol);
241 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
242 Tcl_DStringFree(&dCol);
243#else
drh6d313162000-09-21 13:01:35 +0000244 pElem = Tcl_NewStringObj(azCol[i], -1);
drh297ecf12001-04-05 15:57:13 +0000245#endif
drh6d313162000-09-21 13:01:35 +0000246 }else{
247 pElem = Tcl_NewObj();
248 }
249 Tcl_ListObjAppendElement(0, pList, pElem);
250 }
251 return 0;
252}
253
254/*
drh5d9d7572003-08-19 14:31:01 +0000255** This is a second alternative callback for database queries. A the
256** first column of the first row of the result is made the TCL result.
257*/
258static int DbEvalCallback3(
259 void *clientData, /* An instance of CallbackData */
260 int nCol, /* Number of columns in the result */
261 char ** azCol, /* Data for each column */
262 char ** azN /* Name for each column */
263){
264 Tcl_Interp *interp = (Tcl_Interp*)clientData;
265 Tcl_Obj *pElem;
266 if( azCol==0 ) return 1;
267 if( nCol==0 ) return 1;
268#ifdef UTF_TRANSLATION_NEEDED
269 {
270 Tcl_DString dCol;
271 Tcl_DStringInit(&dCol);
272 Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol);
273 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
274 Tcl_DStringFree(&dCol);
275 }
276#else
277 pElem = Tcl_NewStringObj(azCol[0], -1);
278#endif
279 Tcl_SetObjResult(interp, pElem);
280 return 1;
281}
282
283/*
drh75897232000-05-29 14:26:00 +0000284** Called when the command is deleted.
285*/
286static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000287 SqliteDb *pDb = (SqliteDb*)db;
danielk19776f8a5032004-05-10 10:34:51 +0000288 sqlite3_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000289 while( pDb->pFunc ){
290 SqlFunc *pFunc = pDb->pFunc;
291 pDb->pFunc = pFunc->pNext;
292 Tcl_Free((char*)pFunc);
293 }
drhbec3f402000-08-04 13:49:02 +0000294 if( pDb->zBusy ){
295 Tcl_Free(pDb->zBusy);
296 }
drhb5a20d32003-04-23 12:25:23 +0000297 if( pDb->zTrace ){
298 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000299 }
drhe22a3342003-04-22 20:30:37 +0000300 if( pDb->zAuth ){
301 Tcl_Free(pDb->zAuth);
302 }
drhbec3f402000-08-04 13:49:02 +0000303 Tcl_Free((char*)pDb);
304}
305
306/*
307** This routine is called when a database file is locked while trying
308** to execute SQL.
309*/
310static int DbBusyHandler(void *cd, const char *zTable, int nTries){
311 SqliteDb *pDb = (SqliteDb*)cd;
312 int rc;
313 char zVal[30];
314 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000315 Tcl_DString cmd;
316
317 Tcl_DStringInit(&cmd);
318 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
319 Tcl_DStringAppendElement(&cmd, zTable);
320 sprintf(zVal, " %d", nTries);
321 Tcl_DStringAppend(&cmd, zVal, -1);
322 zCmd = Tcl_DStringValue(&cmd);
323 rc = Tcl_Eval(pDb->interp, zCmd);
324 Tcl_DStringFree(&cmd);
325 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
326 return 0;
327 }
328 return 1;
drh75897232000-05-29 14:26:00 +0000329}
330
331/*
danielk1977348bb5d2003-10-18 09:37:26 +0000332** This routine is invoked as the 'progress callback' for the database.
333*/
334static int DbProgressHandler(void *cd){
335 SqliteDb *pDb = (SqliteDb*)cd;
336 int rc;
337
338 assert( pDb->zProgress );
339 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
340 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
341 return 1;
342 }
343 return 0;
344}
345
346/*
drhb5a20d32003-04-23 12:25:23 +0000347** This routine is called by the SQLite trace handler whenever a new
348** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000349*/
drhb5a20d32003-04-23 12:25:23 +0000350static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000351 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000352 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000353
drhb5a20d32003-04-23 12:25:23 +0000354 Tcl_DStringInit(&str);
355 Tcl_DStringAppend(&str, pDb->zTrace, -1);
356 Tcl_DStringAppendElement(&str, zSql);
357 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
358 Tcl_DStringFree(&str);
359 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000360}
361
362/*
drhaa940ea2004-01-15 02:44:03 +0000363** This routine is called when a transaction is committed. The
364** TCL script in pDb->zCommit is executed. If it returns non-zero or
365** if it throws an exception, the transaction is rolled back instead
366** of being committed.
367*/
368static int DbCommitHandler(void *cd){
369 SqliteDb *pDb = (SqliteDb*)cd;
370 int rc;
371
372 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
373 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
374 return 1;
375 }
376 return 0;
377}
378
379/*
drhcabb0812002-09-14 13:47:32 +0000380** This routine is called to evaluate an SQL function implemented
381** using TCL script.
382*/
danielk19770ae8b832004-05-25 12:05:56 +0000383static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk19776f8a5032004-05-10 10:34:51 +0000384 SqlFunc *p = sqlite3_user_data(context);
drhcabb0812002-09-14 13:47:32 +0000385 Tcl_DString cmd;
386 int i;
387 int rc;
388
389 Tcl_DStringInit(&cmd);
390 Tcl_DStringAppend(&cmd, p->zScript, -1);
391 for(i=0; i<argc; i++){
danielk197751ad0ec2004-05-24 12:39:02 +0000392 if( SQLITE3_NULL==sqlite3_value_type(argv[i]) ){
393 Tcl_DStringAppendElement(&cmd, "");
394 }else{
drh4f26d6c2004-05-26 23:25:30 +0000395 Tcl_DStringAppendElement(&cmd, sqlite3_value_text(argv[i]));
danielk197751ad0ec2004-05-24 12:39:02 +0000396 }
drhcabb0812002-09-14 13:47:32 +0000397 }
398 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
399 if( rc ){
danielk19777e18c252004-05-25 11:47:24 +0000400 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhcabb0812002-09-14 13:47:32 +0000401 }else{
danielk19777e18c252004-05-25 11:47:24 +0000402 sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1, 1);
drhcabb0812002-09-14 13:47:32 +0000403 }
404}
drhe22a3342003-04-22 20:30:37 +0000405#ifndef SQLITE_OMIT_AUTHORIZATION
406/*
407** This is the authentication function. It appends the authentication
408** type code and the two arguments to zCmd[] then invokes the result
409** on the interpreter. The reply is examined to determine if the
410** authentication fails or succeeds.
411*/
412static int auth_callback(
413 void *pArg,
414 int code,
415 const char *zArg1,
416 const char *zArg2,
417 const char *zArg3,
418 const char *zArg4
419){
420 char *zCode;
421 Tcl_DString str;
422 int rc;
423 const char *zReply;
424 SqliteDb *pDb = (SqliteDb*)pArg;
425
426 switch( code ){
427 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
428 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
429 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
430 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
431 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
432 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
433 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
434 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
435 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
436 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
437 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
438 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
439 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
440 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
441 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
442 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
443 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
444 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
445 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
446 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
447 case SQLITE_READ : zCode="SQLITE_READ"; break;
448 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
449 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
450 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000451 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
452 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
drhe22a3342003-04-22 20:30:37 +0000453 default : zCode="????"; break;
454 }
455 Tcl_DStringInit(&str);
456 Tcl_DStringAppend(&str, pDb->zAuth, -1);
457 Tcl_DStringAppendElement(&str, zCode);
458 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
459 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
460 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
461 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
462 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
463 Tcl_DStringFree(&str);
464 zReply = Tcl_GetStringResult(pDb->interp);
465 if( strcmp(zReply,"SQLITE_OK")==0 ){
466 rc = SQLITE_OK;
467 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
468 rc = SQLITE_DENY;
469 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
470 rc = SQLITE_IGNORE;
471 }else{
472 rc = 999;
473 }
474 return rc;
475}
476#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000477
478/*
drh75897232000-05-29 14:26:00 +0000479** The "sqlite" command below creates a new Tcl command for each
480** connection it opens to an SQLite database. This routine is invoked
481** whenever one of those connection-specific commands is executed
482** in Tcl. For example, if you run Tcl code like this:
483**
484** sqlite db1 "my_database"
485** db1 close
486**
487** The first command opens a connection to the "my_database" database
488** and calls that connection "db1". The second command causes this
489** subroutine to be invoked.
490*/
drh6d313162000-09-21 13:01:35 +0000491static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000492 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000493 int choice;
drh22fbcb82004-02-01 01:22:50 +0000494 int rc = TCL_OK;
drh0de8c112002-07-06 16:32:14 +0000495 static const char *DB_strs[] = {
rdcf146a772004-02-25 22:51:06 +0000496 "authorizer", "busy", "changes",
497 "close", "commit_hook", "complete",
498 "errorcode", "eval", "function",
499 "last_insert_rowid", "last_statement_changes", "onecolumn",
500 "progress", "rekey", "timeout",
501 "trace",
drh22fbcb82004-02-01 01:22:50 +0000502 0
drh6d313162000-09-21 13:01:35 +0000503 };
drh411995d2002-06-25 19:31:18 +0000504 enum DB_enum {
rdcf146a772004-02-25 22:51:06 +0000505 DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
506 DB_CLOSE, DB_COMMIT_HOOK, DB_COMPLETE,
507 DB_ERRORCODE, DB_EVAL, DB_FUNCTION,
508 DB_LAST_INSERT_ROWID, DB_LAST_STATEMENT_CHANGES, DB_ONECOLUMN,
509 DB_PROGRESS, DB_REKEY, DB_TIMEOUT,
510 DB_TRACE
drh6d313162000-09-21 13:01:35 +0000511 };
512
513 if( objc<2 ){
514 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000515 return TCL_ERROR;
516 }
drh411995d2002-06-25 19:31:18 +0000517 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000518 return TCL_ERROR;
519 }
520
drh411995d2002-06-25 19:31:18 +0000521 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000522
drhe22a3342003-04-22 20:30:37 +0000523 /* $db authorizer ?CALLBACK?
524 **
525 ** Invoke the given callback to authorize each SQL operation as it is
526 ** compiled. 5 arguments are appended to the callback before it is
527 ** invoked:
528 **
529 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
530 ** (2) First descriptive name (depends on authorization type)
531 ** (3) Second descriptive name
532 ** (4) Name of the database (ex: "main", "temp")
533 ** (5) Name of trigger that is doing the access
534 **
535 ** The callback should return on of the following strings: SQLITE_OK,
536 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
537 **
538 ** If this method is invoked with no arguments, the current authorization
539 ** callback string is returned.
540 */
541 case DB_AUTHORIZER: {
542 if( objc>3 ){
543 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
544 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000545 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000546 Tcl_AppendResult(interp, pDb->zAuth, 0);
547 }
548 }else{
549 char *zAuth;
550 int len;
551 if( pDb->zAuth ){
552 Tcl_Free(pDb->zAuth);
553 }
554 zAuth = Tcl_GetStringFromObj(objv[2], &len);
555 if( zAuth && len>0 ){
556 pDb->zAuth = Tcl_Alloc( len + 1 );
557 strcpy(pDb->zAuth, zAuth);
558 }else{
559 pDb->zAuth = 0;
560 }
561#ifndef SQLITE_OMIT_AUTHORIZATION
562 if( pDb->zAuth ){
563 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000564 sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
drhe22a3342003-04-22 20:30:37 +0000565 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000566 sqlite3_set_authorizer(pDb->db, 0, 0);
drhe22a3342003-04-22 20:30:37 +0000567 }
568#endif
569 }
570 break;
571 }
572
drhbec3f402000-08-04 13:49:02 +0000573 /* $db busy ?CALLBACK?
574 **
575 ** Invoke the given callback if an SQL statement attempts to open
576 ** a locked database file.
577 */
drh6d313162000-09-21 13:01:35 +0000578 case DB_BUSY: {
579 if( objc>3 ){
580 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000581 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000582 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000583 if( pDb->zBusy ){
584 Tcl_AppendResult(interp, pDb->zBusy, 0);
585 }
586 }else{
drh6d313162000-09-21 13:01:35 +0000587 char *zBusy;
588 int len;
drhbec3f402000-08-04 13:49:02 +0000589 if( pDb->zBusy ){
590 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000591 }
drh6d313162000-09-21 13:01:35 +0000592 zBusy = Tcl_GetStringFromObj(objv[2], &len);
593 if( zBusy && len>0 ){
594 pDb->zBusy = Tcl_Alloc( len + 1 );
595 strcpy(pDb->zBusy, zBusy);
596 }else{
597 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000598 }
599 if( pDb->zBusy ){
600 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000601 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000602 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000603 sqlite3_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000604 }
605 }
drh6d313162000-09-21 13:01:35 +0000606 break;
607 }
drhbec3f402000-08-04 13:49:02 +0000608
danielk1977348bb5d2003-10-18 09:37:26 +0000609 /* $db progress ?N CALLBACK?
610 **
611 ** Invoke the given callback every N virtual machine opcodes while executing
612 ** queries.
613 */
614 case DB_PROGRESS: {
615 if( objc==2 ){
616 if( pDb->zProgress ){
617 Tcl_AppendResult(interp, pDb->zProgress, 0);
618 }
619 }else if( objc==4 ){
620 char *zProgress;
621 int len;
622 int N;
623 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
624 return TCL_ERROR;
625 };
626 if( pDb->zProgress ){
627 Tcl_Free(pDb->zProgress);
628 }
629 zProgress = Tcl_GetStringFromObj(objv[3], &len);
630 if( zProgress && len>0 ){
631 pDb->zProgress = Tcl_Alloc( len + 1 );
632 strcpy(pDb->zProgress, zProgress);
633 }else{
634 pDb->zProgress = 0;
635 }
636#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
637 if( pDb->zProgress ){
638 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000639 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
danielk1977348bb5d2003-10-18 09:37:26 +0000640 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000641 sqlite3_progress_handler(pDb->db, 0, 0, 0);
danielk1977348bb5d2003-10-18 09:37:26 +0000642 }
643#endif
644 }else{
645 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
646 return TCL_ERROR;
647 }
648 break;
649 }
650
drhc8d30ac2002-04-12 10:08:59 +0000651 /*
652 ** $db changes
653 **
654 ** Return the number of rows that were modified, inserted, or deleted by
655 ** the most recent "eval".
656 */
657 case DB_CHANGES: {
658 Tcl_Obj *pResult;
659 int nChange;
660 if( objc!=2 ){
661 Tcl_WrongNumArgs(interp, 2, objv, "");
662 return TCL_ERROR;
663 }
danielk197730ccda12004-05-27 12:11:31 +0000664 /* nChange = sqlite3_changes(pDb->db); */
665 nChange = pDb->nChange;
drhc8d30ac2002-04-12 10:08:59 +0000666 pResult = Tcl_GetObjResult(interp);
667 Tcl_SetIntObj(pResult, nChange);
668 break;
669 }
670
rdcf146a772004-02-25 22:51:06 +0000671 /*
672 ** $db last_statement_changes
673 **
674 ** Return the number of rows that were modified, inserted, or deleted by
675 ** the last statment to complete execution (excluding changes due to
676 ** triggers)
677 */
678 case DB_LAST_STATEMENT_CHANGES: {
679 Tcl_Obj *pResult;
680 int lsChange;
681 if( objc!=2 ){
682 Tcl_WrongNumArgs(interp, 2, objv, "");
683 return TCL_ERROR;
684 }
danielk19776f8a5032004-05-10 10:34:51 +0000685 lsChange = sqlite3_last_statement_changes(pDb->db);
rdcf146a772004-02-25 22:51:06 +0000686 pResult = Tcl_GetObjResult(interp);
687 Tcl_SetIntObj(pResult, lsChange);
688 break;
689 }
690
drh75897232000-05-29 14:26:00 +0000691 /* $db close
692 **
693 ** Shutdown the database
694 */
drh6d313162000-09-21 13:01:35 +0000695 case DB_CLOSE: {
696 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
697 break;
698 }
drh75897232000-05-29 14:26:00 +0000699
drhaa940ea2004-01-15 02:44:03 +0000700 /* $db commit_hook ?CALLBACK?
701 **
702 ** Invoke the given callback just before committing every SQL transaction.
703 ** If the callback throws an exception or returns non-zero, then the
704 ** transaction is aborted. If CALLBACK is an empty string, the callback
705 ** is disabled.
706 */
707 case DB_COMMIT_HOOK: {
708 if( objc>3 ){
709 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
710 }else if( objc==2 ){
711 if( pDb->zCommit ){
712 Tcl_AppendResult(interp, pDb->zCommit, 0);
713 }
714 }else{
715 char *zCommit;
716 int len;
717 if( pDb->zCommit ){
718 Tcl_Free(pDb->zCommit);
719 }
720 zCommit = Tcl_GetStringFromObj(objv[2], &len);
721 if( zCommit && len>0 ){
722 pDb->zCommit = Tcl_Alloc( len + 1 );
723 strcpy(pDb->zCommit, zCommit);
724 }else{
725 pDb->zCommit = 0;
726 }
727 if( pDb->zCommit ){
728 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000729 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
drhaa940ea2004-01-15 02:44:03 +0000730 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000731 sqlite3_commit_hook(pDb->db, 0, 0);
drhaa940ea2004-01-15 02:44:03 +0000732 }
733 }
734 break;
735 }
736
drh75897232000-05-29 14:26:00 +0000737 /* $db complete SQL
738 **
739 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
740 ** additional lines of input are needed. This is similar to the
741 ** built-in "info complete" command of Tcl.
742 */
drh6d313162000-09-21 13:01:35 +0000743 case DB_COMPLETE: {
744 Tcl_Obj *pResult;
745 int isComplete;
746 if( objc!=3 ){
747 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000748 return TCL_ERROR;
749 }
danielk19776f8a5032004-05-10 10:34:51 +0000750 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
drh6d313162000-09-21 13:01:35 +0000751 pResult = Tcl_GetObjResult(interp);
752 Tcl_SetBooleanObj(pResult, isComplete);
753 break;
754 }
drhdcd997e2003-01-31 17:21:49 +0000755
756 /*
757 ** $db errorcode
758 **
759 ** Return the numeric error code that was returned by the most recent
danielk19776f8a5032004-05-10 10:34:51 +0000760 ** call to sqlite3_exec().
drhdcd997e2003-01-31 17:21:49 +0000761 */
762 case DB_ERRORCODE: {
763 Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc));
764 break;
765 }
drh75897232000-05-29 14:26:00 +0000766
767 /*
768 ** $db eval $sql ?array { ...code... }?
769 **
770 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000771 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000772 ** If "array" and "code" are omitted, then no callback is every invoked.
773 ** If "array" is an empty string, then the values are placed in variables
774 ** that have the same name as the fields extracted by the query.
775 */
drh6d313162000-09-21 13:01:35 +0000776 case DB_EVAL: {
danielk197730ccda12004-05-27 12:11:31 +0000777 char const *zSql;
778 char const *zLeft;
779 sqlite3_stmt *pStmt;
780 Tcl_Obj *pRet = 0;
781
782 if( objc!=5 && objc!=3 ){
783 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
784 return TCL_ERROR;
785 }
786
787 pDb->nChange = 0;
788 zSql = Tcl_GetStringFromObj(objv[2], 0);
789 while( zSql[0] ){
790 int i;
791
792 if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
793 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_STATIC);
794 rc = TCL_ERROR;
795 break;
796 }
797
798 if( pStmt && objc==5 ){
799 Tcl_Obj *pColList = Tcl_NewObj();
800 Tcl_IncrRefCount(pColList);
801
802 for(i=0; i<sqlite3_column_count(pStmt); i++){
803 Tcl_ListObjAppendElement(interp, pColList,
804 Tcl_NewStringObj(sqlite3_column_name(pStmt, i), -1)
805 );
806 }
807 Tcl_ObjSetVar2(interp,objv[3],Tcl_NewStringObj("*",-1),pColList,0);
808 }
809
810 while( pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
811 for(i=0; i<sqlite3_column_count(pStmt); i++){
812 Tcl_Obj *pVal;
813
814 /* Set pVal to contain the i'th column of this row. */
815 if( SQLITE3_BLOB!=sqlite3_column_type(pStmt, i) ){
816 pVal = Tcl_NewStringObj(sqlite3_column_text(pStmt, i), -1);
817 }else{
818 pVal = Tcl_NewByteArrayObj(
819 sqlite3_column_blob(pStmt, i),
820 sqlite3_column_bytes(pStmt, i)
821 );
822 }
823
824 if( objc==5 ){
825 Tcl_Obj *pName = Tcl_NewStringObj(sqlite3_column_name(pStmt, i), -1);
826 Tcl_IncrRefCount(pName);
827 if( !strcmp("", Tcl_GetString(objv[3])) ){
828 Tcl_ObjSetVar2(interp, pName, 0, pVal, 0);
829 }else{
830 Tcl_ObjSetVar2(interp, objv[3], pName, pVal, 0);
831 }
832 Tcl_DecrRefCount(pName);
833 }else{
834 if( !pRet ){
835 pRet = Tcl_NewObj();
836 Tcl_IncrRefCount(pRet);
837 }
838 Tcl_ListObjAppendElement(interp, pRet, pVal);
839 }
840 }
841
842 if( objc==5 ){
843 rc = Tcl_EvalObjEx(interp, objv[4], 0);
844 if( rc!=TCL_ERROR ) rc = TCL_OK;
845 }
846 }
847
848 if( pStmt && SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){
849 continue;
850 }
851
852 if( pStmt && SQLITE_OK!=sqlite3_errcode(pDb->db) ){
853 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_STATIC);
854 rc = TCL_ERROR;
855 break;
856 }
857
858 pDb->nChange += sqlite3_changes(pDb->db);
859 zSql = zLeft;
860 }
861
862 if( rc==TCL_OK && pRet ){
863 Tcl_SetObjResult(interp, pRet);
864 Tcl_DecrRefCount(pRet);
865 }
866
867 break;
868 }
869#if 0
870 case DB_EVAL: {
drh75897232000-05-29 14:26:00 +0000871 CallbackData cbData;
872 char *zErrMsg;
drh6d313162000-09-21 13:01:35 +0000873 char *zSql;
drh297ecf12001-04-05 15:57:13 +0000874#ifdef UTF_TRANSLATION_NEEDED
875 Tcl_DString dSql;
drh6d4abfb2001-10-22 02:58:08 +0000876 int i;
drh297ecf12001-04-05 15:57:13 +0000877#endif
drh75897232000-05-29 14:26:00 +0000878
drh6d313162000-09-21 13:01:35 +0000879 if( objc!=5 && objc!=3 ){
880 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
drh75897232000-05-29 14:26:00 +0000881 return TCL_ERROR;
882 }
drhbec3f402000-08-04 13:49:02 +0000883 pDb->interp = interp;
drh6d313162000-09-21 13:01:35 +0000884 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh297ecf12001-04-05 15:57:13 +0000885#ifdef UTF_TRANSLATION_NEEDED
886 Tcl_DStringInit(&dSql);
887 Tcl_UtfToExternalDString(NULL, zSql, -1, &dSql);
888 zSql = Tcl_DStringValue(&dSql);
889#endif
drh6d313162000-09-21 13:01:35 +0000890 Tcl_IncrRefCount(objv[2]);
891 if( objc==5 ){
drh75897232000-05-29 14:26:00 +0000892 cbData.interp = interp;
drhdcc581c2000-05-30 13:44:19 +0000893 cbData.once = 1;
drh6d313162000-09-21 13:01:35 +0000894 cbData.zArray = Tcl_GetStringFromObj(objv[3], 0);
895 cbData.pCode = objv[4];
drh960e8c62001-04-03 16:53:21 +0000896 cbData.tcl_rc = TCL_OK;
drh6d4abfb2001-10-22 02:58:08 +0000897 cbData.nColName = 0;
898 cbData.azColName = 0;
drh75897232000-05-29 14:26:00 +0000899 zErrMsg = 0;
drh6d313162000-09-21 13:01:35 +0000900 Tcl_IncrRefCount(objv[3]);
901 Tcl_IncrRefCount(objv[4]);
danielk19776f8a5032004-05-10 10:34:51 +0000902 rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback, &cbData, &zErrMsg);
drh6d313162000-09-21 13:01:35 +0000903 Tcl_DecrRefCount(objv[4]);
904 Tcl_DecrRefCount(objv[3]);
drh960e8c62001-04-03 16:53:21 +0000905 if( cbData.tcl_rc==TCL_BREAK ){ cbData.tcl_rc = TCL_OK; }
drh75897232000-05-29 14:26:00 +0000906 }else{
drh6d313162000-09-21 13:01:35 +0000907 Tcl_Obj *pList = Tcl_NewObj();
drh960e8c62001-04-03 16:53:21 +0000908 cbData.tcl_rc = TCL_OK;
danielk19776f8a5032004-05-10 10:34:51 +0000909 rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback2, pList, &zErrMsg);
drh6d313162000-09-21 13:01:35 +0000910 Tcl_SetObjResult(interp, pList);
drh75897232000-05-29 14:26:00 +0000911 }
drhdcd997e2003-01-31 17:21:49 +0000912 pDb->rc = rc;
drhb798fa62002-09-03 19:43:23 +0000913 if( rc==SQLITE_ABORT ){
914 if( zErrMsg ) free(zErrMsg);
915 rc = cbData.tcl_rc;
916 }else if( zErrMsg ){
drh75897232000-05-29 14:26:00 +0000917 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
918 free(zErrMsg);
drh960e8c62001-04-03 16:53:21 +0000919 rc = TCL_ERROR;
drhb798fa62002-09-03 19:43:23 +0000920 }else if( rc!=SQLITE_OK ){
danielk19776f8a5032004-05-10 10:34:51 +0000921 Tcl_AppendResult(interp, sqlite3_error_string(rc), 0);
drh6d4abfb2001-10-22 02:58:08 +0000922 rc = TCL_ERROR;
drh960e8c62001-04-03 16:53:21 +0000923 }else{
drh75897232000-05-29 14:26:00 +0000924 }
drh6d313162000-09-21 13:01:35 +0000925 Tcl_DecrRefCount(objv[2]);
drh297ecf12001-04-05 15:57:13 +0000926#ifdef UTF_TRANSLATION_NEEDED
927 Tcl_DStringFree(&dSql);
drh6d4abfb2001-10-22 02:58:08 +0000928 if( objc==5 && cbData.azColName ){
929 for(i=0; i<cbData.nColName; i++){
930 if( cbData.azColName[i] ) free(cbData.azColName[i]);
931 }
932 free(cbData.azColName);
drhce927062001-11-09 13:41:09 +0000933 cbData.azColName = 0;
drh6d4abfb2001-10-22 02:58:08 +0000934 }
drh297ecf12001-04-05 15:57:13 +0000935#endif
drh75897232000-05-29 14:26:00 +0000936 return rc;
drh6d313162000-09-21 13:01:35 +0000937 }
danielk197730ccda12004-05-27 12:11:31 +0000938#endif
drhbec3f402000-08-04 13:49:02 +0000939
940 /*
drhcabb0812002-09-14 13:47:32 +0000941 ** $db function NAME SCRIPT
942 **
943 ** Create a new SQL function called NAME. Whenever that function is
944 ** called, invoke SCRIPT to evaluate the function.
945 */
946 case DB_FUNCTION: {
947 SqlFunc *pFunc;
948 char *zName;
949 char *zScript;
950 int nScript;
951 if( objc!=4 ){
952 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
953 return TCL_ERROR;
954 }
955 zName = Tcl_GetStringFromObj(objv[2], 0);
956 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
957 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
958 if( pFunc==0 ) return TCL_ERROR;
959 pFunc->interp = interp;
960 pFunc->pNext = pDb->pFunc;
961 pFunc->zScript = (char*)&pFunc[1];
962 strcpy(pFunc->zScript, zScript);
danielk197765904932004-05-26 06:18:37 +0000963 sqlite3_create_function(pDb->db, zName, -1, 0, 0, pFunc, tclSqlFunc, 0, 0);
drhcabb0812002-09-14 13:47:32 +0000964 break;
965 }
966
967 /*
drhaf9ff332002-01-16 21:00:27 +0000968 ** $db last_insert_rowid
969 **
970 ** Return an integer which is the ROWID for the most recent insert.
971 */
972 case DB_LAST_INSERT_ROWID: {
973 Tcl_Obj *pResult;
974 int rowid;
975 if( objc!=2 ){
976 Tcl_WrongNumArgs(interp, 2, objv, "");
977 return TCL_ERROR;
978 }
danielk19776f8a5032004-05-10 10:34:51 +0000979 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +0000980 pResult = Tcl_GetObjResult(interp);
981 Tcl_SetIntObj(pResult, rowid);
982 break;
983 }
984
985 /*
drh5d9d7572003-08-19 14:31:01 +0000986 ** $db onecolumn SQL
987 **
988 ** Return a single column from a single row of the given SQL query.
989 */
990 case DB_ONECOLUMN: {
drh5d9d7572003-08-19 14:31:01 +0000991 char *zSql;
992 char *zErrMsg = 0;
993 if( objc!=3 ){
994 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
995 return TCL_ERROR;
996 }
997 zSql = Tcl_GetStringFromObj(objv[2], 0);
danielk19776f8a5032004-05-10 10:34:51 +0000998 rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
drh5d9d7572003-08-19 14:31:01 +0000999 if( rc==SQLITE_ABORT ){
drh22fbcb82004-02-01 01:22:50 +00001000 rc = SQLITE_OK;
drh5d9d7572003-08-19 14:31:01 +00001001 }else if( zErrMsg ){
1002 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
1003 free(zErrMsg);
1004 rc = TCL_ERROR;
1005 }else if( rc!=SQLITE_OK ){
danielk19776f8a5032004-05-10 10:34:51 +00001006 Tcl_AppendResult(interp, sqlite3_error_string(rc), 0);
drh5d9d7572003-08-19 14:31:01 +00001007 rc = TCL_ERROR;
1008 }
1009 break;
1010 }
1011
1012 /*
drh22fbcb82004-02-01 01:22:50 +00001013 ** $db rekey KEY
1014 **
1015 ** Change the encryption key on the currently open database.
1016 */
1017 case DB_REKEY: {
1018 int nKey;
1019 void *pKey;
1020 if( objc!=3 ){
1021 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
1022 return TCL_ERROR;
1023 }
1024 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +00001025#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001026 rc = sqlite_rekey(pDb->db, pKey, nKey);
1027 if( rc ){
danielk19776f8a5032004-05-10 10:34:51 +00001028 Tcl_AppendResult(interp, sqlite3_error_string(rc), 0);
drh22fbcb82004-02-01 01:22:50 +00001029 rc = TCL_ERROR;
1030 }
1031#endif
1032 break;
1033 }
1034
1035 /*
drhbec3f402000-08-04 13:49:02 +00001036 ** $db timeout MILLESECONDS
1037 **
1038 ** Delay for the number of milliseconds specified when a file is locked.
1039 */
drh6d313162000-09-21 13:01:35 +00001040 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +00001041 int ms;
drh6d313162000-09-21 13:01:35 +00001042 if( objc!=3 ){
1043 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +00001044 return TCL_ERROR;
1045 }
drh6d313162000-09-21 13:01:35 +00001046 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +00001047 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +00001048 break;
drh75897232000-05-29 14:26:00 +00001049 }
drhb5a20d32003-04-23 12:25:23 +00001050
1051 /* $db trace ?CALLBACK?
1052 **
1053 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
1054 ** that is executed. The text of the SQL is appended to CALLBACK before
1055 ** it is executed.
1056 */
1057 case DB_TRACE: {
1058 if( objc>3 ){
1059 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
1060 }else if( objc==2 ){
1061 if( pDb->zTrace ){
1062 Tcl_AppendResult(interp, pDb->zTrace, 0);
1063 }
1064 }else{
1065 char *zTrace;
1066 int len;
1067 if( pDb->zTrace ){
1068 Tcl_Free(pDb->zTrace);
1069 }
1070 zTrace = Tcl_GetStringFromObj(objv[2], &len);
1071 if( zTrace && len>0 ){
1072 pDb->zTrace = Tcl_Alloc( len + 1 );
1073 strcpy(pDb->zTrace, zTrace);
1074 }else{
1075 pDb->zTrace = 0;
1076 }
1077 if( pDb->zTrace ){
1078 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +00001079 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +00001080 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001081 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +00001082 }
1083 }
1084 break;
1085 }
1086
drh6d313162000-09-21 13:01:35 +00001087 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +00001088 return rc;
drh75897232000-05-29 14:26:00 +00001089}
1090
1091/*
drh22fbcb82004-02-01 01:22:50 +00001092** sqlite DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +00001093**
1094** This is the main Tcl command. When the "sqlite" Tcl command is
1095** invoked, this routine runs to process that command.
1096**
1097** The first argument, DBNAME, is an arbitrary name for a new
1098** database connection. This command creates a new command named
1099** DBNAME that is used to control that connection. The database
1100** connection is deleted when the DBNAME command is deleted.
1101**
1102** The second argument is the name of the directory that contains
1103** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +00001104**
1105** For testing purposes, we also support the following:
1106**
1107** sqlite -encoding
1108**
1109** Return the encoding used by LIKE and GLOB operators. Choices
1110** are UTF-8 and iso8859.
1111**
drh647cb0e2002-11-04 19:32:25 +00001112** sqlite -version
1113**
1114** Return the version number of the SQLite library.
1115**
drhfbc3eab2001-04-06 16:13:42 +00001116** sqlite -tcl-uses-utf
1117**
1118** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
1119** not. Used by tests to make sure the library was compiled
1120** correctly.
drh75897232000-05-29 14:26:00 +00001121*/
drh22fbcb82004-02-01 01:22:50 +00001122static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +00001123 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +00001124 void *pKey = 0;
1125 int nKey = 0;
1126 const char *zArg;
drh75897232000-05-29 14:26:00 +00001127 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +00001128 const char *zFile;
danielk197780290862004-05-22 09:21:21 +00001129 const char *zOpts[2] = {0, 0};
drh06b27182002-06-26 20:06:05 +00001130 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +00001131 if( objc==2 ){
1132 zArg = Tcl_GetStringFromObj(objv[1], 0);
1133 if( strcmp(zArg,"-encoding")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001134 Tcl_AppendResult(interp,sqlite3_encoding,0);
drhfbc3eab2001-04-06 16:13:42 +00001135 return TCL_OK;
1136 }
drh22fbcb82004-02-01 01:22:50 +00001137 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001138 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +00001139 return TCL_OK;
1140 }
drh9eb9e262004-02-11 02:18:05 +00001141 if( strcmp(zArg,"-has-codec")==0 ){
1142#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001143 Tcl_AppendResult(interp,"1",0);
1144#else
1145 Tcl_AppendResult(interp,"0",0);
1146#endif
1147 return TCL_OK;
1148 }
1149 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +00001150#ifdef TCL_UTF_MAX
1151 Tcl_AppendResult(interp,"1",0);
1152#else
1153 Tcl_AppendResult(interp,"0",0);
1154#endif
1155 return TCL_OK;
1156 }
1157 }
drh22fbcb82004-02-01 01:22:50 +00001158 if( objc==5 || objc==6 ){
1159 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
1160 if( strcmp(zArg,"-key")==0 ){
1161 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
1162 objc -= 2;
1163 }
1164 }
1165 if( objc!=3 && objc!=4 ){
1166 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +00001167#ifdef SQLITE_HAS_CODEC
1168 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +00001169#else
1170 "HANDLE FILENAME ?MODE?"
1171#endif
1172 );
drh75897232000-05-29 14:26:00 +00001173 return TCL_ERROR;
1174 }
drh75897232000-05-29 14:26:00 +00001175 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001176 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001177 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001178 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1179 return TCL_ERROR;
1180 }
1181 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00001182 zFile = Tcl_GetStringFromObj(objv[2], 0);
drh9eb9e262004-02-11 02:18:05 +00001183#ifdef SQLITE_HAS_CODEC
danielk19776f8a5032004-05-10 10:34:51 +00001184 p->db = sqlite3_open_encrypted(zFile, pKey, nKey, 0, &zErrMsg);
drheb8ed702004-02-11 10:37:23 +00001185#else
danielk197780290862004-05-22 09:21:21 +00001186 if( objc>3 ){
1187 zOpts[0] = Tcl_GetString(objv[3]);
1188 }
1189 sqlite3_open(zFile, &p->db, zOpts);
1190 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
1191 zErrMsg = strdup(sqlite3_errmsg(p->db));
1192 sqlite3_close(p->db);
1193 p->db = 0;
1194 }
drheb8ed702004-02-11 10:37:23 +00001195#endif
drhbec3f402000-08-04 13:49:02 +00001196 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001197 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001198 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001199 free(zErrMsg);
1200 return TCL_ERROR;
1201 }
drh22fbcb82004-02-01 01:22:50 +00001202 zArg = Tcl_GetStringFromObj(objv[1], 0);
1203 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001204
drh06b27182002-06-26 20:06:05 +00001205 /* The return value is the value of the sqlite* pointer
1206 */
1207 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001208 if( strncmp(zBuf,"0x",2) ){
1209 sprintf(zBuf, "0x%p", p->db);
1210 }
drh06b27182002-06-26 20:06:05 +00001211 Tcl_AppendResult(interp, zBuf, 0);
1212
drhc22bd472002-05-10 13:14:07 +00001213 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001214 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001215 */
drh28b4e482002-03-11 02:06:13 +00001216#ifdef SQLITE_TEST
1217 {
drhc22bd472002-05-10 13:14:07 +00001218 extern void Md5_Register(sqlite*);
1219 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +00001220 }
drh28b4e482002-03-11 02:06:13 +00001221#endif
drh75897232000-05-29 14:26:00 +00001222 return TCL_OK;
1223}
1224
1225/*
drh90ca9752001-09-28 17:47:14 +00001226** Provide a dummy Tcl_InitStubs if we are using this as a static
1227** library.
1228*/
1229#ifndef USE_TCL_STUBS
1230# undef Tcl_InitStubs
1231# define Tcl_InitStubs(a,b,c)
1232#endif
1233
1234/*
drh75897232000-05-29 14:26:00 +00001235** Initialize this module.
1236**
1237** This Tcl module contains only a single new Tcl command named "sqlite".
1238** (Hence there is no namespace. There is no point in using a namespace
1239** if the extension only supplies one new name!) The "sqlite" command is
1240** used to open a new SQLite database. See the DbMain() routine above
1241** for additional information.
1242*/
1243int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001244 Tcl_InitStubs(interp, "8.0", 0);
drh22fbcb82004-02-01 01:22:50 +00001245 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001246 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh90ca9752001-09-28 17:47:14 +00001247 return TCL_OK;
1248}
1249int Tclsqlite_Init(Tcl_Interp *interp){
1250 Tcl_InitStubs(interp, "8.0", 0);
drh22fbcb82004-02-01 01:22:50 +00001251 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001252 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh75897232000-05-29 14:26:00 +00001253 return TCL_OK;
1254}
1255int Sqlite_SafeInit(Tcl_Interp *interp){
1256 return TCL_OK;
1257}
drh90ca9752001-09-28 17:47:14 +00001258int Tclsqlite_SafeInit(Tcl_Interp *interp){
1259 return TCL_OK;
1260}
drh75897232000-05-29 14:26:00 +00001261
drh3cebbde2000-10-19 14:59:27 +00001262#if 0
drh75897232000-05-29 14:26:00 +00001263/*
1264** If compiled using mktclapp, this routine runs to initialize
1265** everything.
1266*/
1267int Et_AppInit(Tcl_Interp *interp){
1268 return Sqlite_Init(interp);
1269}
drh3cebbde2000-10-19 14:59:27 +00001270#endif
drh348784e2000-05-29 20:41:49 +00001271
1272/*
1273** If the macro TCLSH is defined and is one, then put in code for the
1274** "main" routine that will initialize Tcl.
1275*/
1276#if defined(TCLSH) && TCLSH==1
1277static char zMainloop[] =
1278 "set line {}\n"
1279 "while {![eof stdin]} {\n"
1280 "if {$line!=\"\"} {\n"
1281 "puts -nonewline \"> \"\n"
1282 "} else {\n"
1283 "puts -nonewline \"% \"\n"
1284 "}\n"
1285 "flush stdout\n"
1286 "append line [gets stdin]\n"
1287 "if {[info complete $line]} {\n"
1288 "if {[catch {uplevel #0 $line} result]} {\n"
1289 "puts stderr \"Error: $result\"\n"
1290 "} elseif {$result!=\"\"} {\n"
1291 "puts $result\n"
1292 "}\n"
1293 "set line {}\n"
1294 "} else {\n"
1295 "append line \\n\n"
1296 "}\n"
1297 "}\n"
1298;
1299
1300#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1301int TCLSH_MAIN(int argc, char **argv){
1302 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001303 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001304 interp = Tcl_CreateInterp();
danielk19774adee202004-05-08 08:23:19 +00001305 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001306#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001307 {
1308 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001309 extern int Sqlitetest2_Init(Tcl_Interp*);
1310 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001311 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001312 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001313 extern int Md5_Init(Tcl_Interp*);
danielk19776490beb2004-05-11 06:17:21 +00001314 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001315 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001316 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00001317 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00001318 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001319 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001320 }
1321#endif
drh348784e2000-05-29 20:41:49 +00001322 if( argc>=2 ){
1323 int i;
1324 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1325 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1326 for(i=2; i<argc; i++){
1327 Tcl_SetVar(interp, "argv", argv[i],
1328 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1329 }
1330 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001331 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001332 if( zInfo==0 ) zInfo = interp->result;
1333 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001334 return 1;
1335 }
1336 }else{
1337 Tcl_GlobalEval(interp, zMainloop);
1338 }
1339 return 0;
1340}
1341#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001342
1343#endif /* !defined(NO_TCL) */