blob: ab6821e6f50006263b0740469ed9bda80610ab7e [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**
drh1211de32004-07-26 12:24:22 +000014** $Id: tclsqlite.c,v 1.98 2004/07/26 12:24:23 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/*
danielk19770202b292004-06-09 09:55:16 +000046** New collation sequences function can be created as TCL scripts. Each such
47** function is described by an instance of the following structure.
48*/
49typedef struct SqlCollate SqlCollate;
50struct SqlCollate {
51 Tcl_Interp *interp; /* The TCL interpret to execute the function */
52 char *zScript; /* The script to be run */
53 SqlCollate *pNext; /* Next function on the list of them all */
54};
55
56/*
drhbec3f402000-08-04 13:49:02 +000057** There is one instance of this structure for each SQLite database
58** that has been opened by the SQLite TCL interface.
59*/
60typedef struct SqliteDb SqliteDb;
61struct SqliteDb {
62 sqlite *db; /* The "real" database structure */
63 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000064 char *zBusy; /* The busy callback routine */
drhaa940ea2004-01-15 02:44:03 +000065 char *zCommit; /* The commit hook callback routine */
drhb5a20d32003-04-23 12:25:23 +000066 char *zTrace; /* The trace callback routine */
danielk1977348bb5d2003-10-18 09:37:26 +000067 char *zProgress; /* The progress callback routine */
drhe22a3342003-04-22 20:30:37 +000068 char *zAuth; /* The authorization callback routine */
drhcabb0812002-09-14 13:47:32 +000069 SqlFunc *pFunc; /* List of SQL functions */
danielk19770202b292004-06-09 09:55:16 +000070 SqlCollate *pCollate; /* List of SQL collation functions */
danielk19776f8a5032004-05-10 10:34:51 +000071 int rc; /* Return code of most recent sqlite3_exec() */
danielk19777cedc8d2004-06-10 10:50:08 +000072 Tcl_Obj *pCollateNeeded; /* Collation needed script */
drhbec3f402000-08-04 13:49:02 +000073};
74
75/*
drh75897232000-05-29 14:26:00 +000076** An instance of this structure passes information thru the sqlite
77** logic from the original TCL command into the callback routine.
78*/
79typedef struct CallbackData CallbackData;
80struct CallbackData {
81 Tcl_Interp *interp; /* The TCL interpreter */
82 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000083 Tcl_Obj *pCode; /* The code to execute for each row */
drhce927062001-11-09 13:41:09 +000084 int once; /* Set for first callback only */
drh960e8c62001-04-03 16:53:21 +000085 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000086 int nColName; /* Number of entries in the azColName[] array */
87 char **azColName; /* Column names translated to UTF-8 */
drh98808ba2001-10-18 12:34:46 +000088};
drh297ecf12001-04-05 15:57:13 +000089
drh6d313162000-09-21 13:01:35 +000090/*
drh5d9d7572003-08-19 14:31:01 +000091** This is a second alternative callback for database queries. A the
92** first column of the first row of the result is made the TCL result.
93*/
94static int DbEvalCallback3(
95 void *clientData, /* An instance of CallbackData */
96 int nCol, /* Number of columns in the result */
97 char ** azCol, /* Data for each column */
98 char ** azN /* Name for each column */
99){
100 Tcl_Interp *interp = (Tcl_Interp*)clientData;
101 Tcl_Obj *pElem;
102 if( azCol==0 ) return 1;
103 if( nCol==0 ) return 1;
104#ifdef UTF_TRANSLATION_NEEDED
105 {
106 Tcl_DString dCol;
107 Tcl_DStringInit(&dCol);
108 Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol);
109 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
110 Tcl_DStringFree(&dCol);
111 }
112#else
113 pElem = Tcl_NewStringObj(azCol[0], -1);
114#endif
115 Tcl_SetObjResult(interp, pElem);
116 return 1;
117}
118
119/*
drh75897232000-05-29 14:26:00 +0000120** Called when the command is deleted.
121*/
122static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000123 SqliteDb *pDb = (SqliteDb*)db;
danielk19776f8a5032004-05-10 10:34:51 +0000124 sqlite3_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000125 while( pDb->pFunc ){
126 SqlFunc *pFunc = pDb->pFunc;
127 pDb->pFunc = pFunc->pNext;
128 Tcl_Free((char*)pFunc);
129 }
danielk19770202b292004-06-09 09:55:16 +0000130 while( pDb->pCollate ){
131 SqlCollate *pCollate = pDb->pCollate;
132 pDb->pCollate = pCollate->pNext;
133 Tcl_Free((char*)pCollate);
134 }
drhbec3f402000-08-04 13:49:02 +0000135 if( pDb->zBusy ){
136 Tcl_Free(pDb->zBusy);
137 }
drhb5a20d32003-04-23 12:25:23 +0000138 if( pDb->zTrace ){
139 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000140 }
drhe22a3342003-04-22 20:30:37 +0000141 if( pDb->zAuth ){
142 Tcl_Free(pDb->zAuth);
143 }
drhbec3f402000-08-04 13:49:02 +0000144 Tcl_Free((char*)pDb);
145}
146
147/*
148** This routine is called when a database file is locked while trying
149** to execute SQL.
150*/
danielk19772a764eb2004-06-12 01:43:26 +0000151static int DbBusyHandler(void *cd, int nTries){
drhbec3f402000-08-04 13:49:02 +0000152 SqliteDb *pDb = (SqliteDb*)cd;
153 int rc;
154 char zVal[30];
155 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000156 Tcl_DString cmd;
157
158 Tcl_DStringInit(&cmd);
159 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
danielk19772a764eb2004-06-12 01:43:26 +0000160 sprintf(zVal, "%d", nTries);
161 Tcl_DStringAppendElement(&cmd, zVal);
drhbec3f402000-08-04 13:49:02 +0000162 zCmd = Tcl_DStringValue(&cmd);
163 rc = Tcl_Eval(pDb->interp, zCmd);
164 Tcl_DStringFree(&cmd);
165 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
166 return 0;
167 }
168 return 1;
drh75897232000-05-29 14:26:00 +0000169}
170
171/*
danielk1977348bb5d2003-10-18 09:37:26 +0000172** This routine is invoked as the 'progress callback' for the database.
173*/
174static int DbProgressHandler(void *cd){
175 SqliteDb *pDb = (SqliteDb*)cd;
176 int rc;
177
178 assert( pDb->zProgress );
179 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
180 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
181 return 1;
182 }
183 return 0;
184}
185
186/*
drhb5a20d32003-04-23 12:25:23 +0000187** This routine is called by the SQLite trace handler whenever a new
188** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000189*/
drhb5a20d32003-04-23 12:25:23 +0000190static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000191 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000192 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000193
drhb5a20d32003-04-23 12:25:23 +0000194 Tcl_DStringInit(&str);
195 Tcl_DStringAppend(&str, pDb->zTrace, -1);
196 Tcl_DStringAppendElement(&str, zSql);
197 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
198 Tcl_DStringFree(&str);
199 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000200}
201
202/*
drhaa940ea2004-01-15 02:44:03 +0000203** This routine is called when a transaction is committed. The
204** TCL script in pDb->zCommit is executed. If it returns non-zero or
205** if it throws an exception, the transaction is rolled back instead
206** of being committed.
207*/
208static int DbCommitHandler(void *cd){
209 SqliteDb *pDb = (SqliteDb*)cd;
210 int rc;
211
212 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
213 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
214 return 1;
215 }
216 return 0;
217}
218
danielk19777cedc8d2004-06-10 10:50:08 +0000219static void tclCollateNeeded(
220 void *pCtx,
221 sqlite *db,
222 int enc,
223 const char *zName
224){
225 SqliteDb *pDb = (SqliteDb *)pCtx;
226 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
227 Tcl_IncrRefCount(pScript);
228 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
229 Tcl_EvalObjEx(pDb->interp, pScript, 0);
230 Tcl_DecrRefCount(pScript);
231}
232
drhaa940ea2004-01-15 02:44:03 +0000233/*
danielk19770202b292004-06-09 09:55:16 +0000234** This routine is called to evaluate an SQL collation function implemented
235** using TCL script.
236*/
237static int tclSqlCollate(
238 void *pCtx,
239 int nA,
240 const void *zA,
241 int nB,
242 const void *zB
243){
244 SqlCollate *p = (SqlCollate *)pCtx;
245 Tcl_Obj *pCmd;
246
247 pCmd = Tcl_NewStringObj(p->zScript, -1);
248 Tcl_IncrRefCount(pCmd);
249 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA));
250 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB));
251 Tcl_EvalObjEx(p->interp, pCmd, 0);
252 Tcl_DecrRefCount(pCmd);
253 return (atoi(Tcl_GetStringResult(p->interp)));
254}
255
256/*
drhcabb0812002-09-14 13:47:32 +0000257** This routine is called to evaluate an SQL function implemented
258** using TCL script.
259*/
danielk19770ae8b832004-05-25 12:05:56 +0000260static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk19776f8a5032004-05-10 10:34:51 +0000261 SqlFunc *p = sqlite3_user_data(context);
drhcabb0812002-09-14 13:47:32 +0000262 Tcl_DString cmd;
263 int i;
264 int rc;
265
266 Tcl_DStringInit(&cmd);
267 Tcl_DStringAppend(&cmd, p->zScript, -1);
268 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000269 if( SQLITE_NULL==sqlite3_value_type(argv[i]) ){
danielk197751ad0ec2004-05-24 12:39:02 +0000270 Tcl_DStringAppendElement(&cmd, "");
271 }else{
drh4f26d6c2004-05-26 23:25:30 +0000272 Tcl_DStringAppendElement(&cmd, sqlite3_value_text(argv[i]));
danielk197751ad0ec2004-05-24 12:39:02 +0000273 }
drhcabb0812002-09-14 13:47:32 +0000274 }
275 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
276 if( rc ){
danielk19777e18c252004-05-25 11:47:24 +0000277 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhcabb0812002-09-14 13:47:32 +0000278 }else{
danielk1977d8123362004-06-12 09:25:12 +0000279 sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1,
280 SQLITE_TRANSIENT);
drhcabb0812002-09-14 13:47:32 +0000281 }
282}
drhe22a3342003-04-22 20:30:37 +0000283#ifndef SQLITE_OMIT_AUTHORIZATION
284/*
285** This is the authentication function. It appends the authentication
286** type code and the two arguments to zCmd[] then invokes the result
287** on the interpreter. The reply is examined to determine if the
288** authentication fails or succeeds.
289*/
290static int auth_callback(
291 void *pArg,
292 int code,
293 const char *zArg1,
294 const char *zArg2,
295 const char *zArg3,
296 const char *zArg4
297){
298 char *zCode;
299 Tcl_DString str;
300 int rc;
301 const char *zReply;
302 SqliteDb *pDb = (SqliteDb*)pArg;
303
304 switch( code ){
305 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
306 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
307 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
308 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
309 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
310 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
311 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
312 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
313 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
314 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
315 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
316 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
317 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
318 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
319 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
320 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
321 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
322 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
323 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
324 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
325 case SQLITE_READ : zCode="SQLITE_READ"; break;
326 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
327 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
328 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000329 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
330 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
drhe22a3342003-04-22 20:30:37 +0000331 default : zCode="????"; break;
332 }
333 Tcl_DStringInit(&str);
334 Tcl_DStringAppend(&str, pDb->zAuth, -1);
335 Tcl_DStringAppendElement(&str, zCode);
336 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
337 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
338 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
339 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
340 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
341 Tcl_DStringFree(&str);
342 zReply = Tcl_GetStringResult(pDb->interp);
343 if( strcmp(zReply,"SQLITE_OK")==0 ){
344 rc = SQLITE_OK;
345 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
346 rc = SQLITE_DENY;
347 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
348 rc = SQLITE_IGNORE;
349 }else{
350 rc = 999;
351 }
352 return rc;
353}
354#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000355
356/*
danielk1977ef2cb632004-05-29 02:37:19 +0000357** zText is a pointer to text obtained via an sqlite3_result_text()
358** or similar interface. This routine returns a Tcl string object,
359** reference count set to 0, containing the text. If a translation
360** between iso8859 and UTF-8 is required, it is preformed.
361*/
362static Tcl_Obj *dbTextToObj(char const *zText){
363 Tcl_Obj *pVal;
364#ifdef UTF_TRANSLATION_NEEDED
365 Tcl_DString dCol;
366 Tcl_DStringInit(&dCol);
367 Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol);
368 pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
369 Tcl_DStringFree(&dCol);
370#else
371 pVal = Tcl_NewStringObj(zText, -1);
372#endif
373 return pVal;
374}
375
376/*
drh75897232000-05-29 14:26:00 +0000377** The "sqlite" command below creates a new Tcl command for each
378** connection it opens to an SQLite database. This routine is invoked
379** whenever one of those connection-specific commands is executed
380** in Tcl. For example, if you run Tcl code like this:
381**
382** sqlite db1 "my_database"
383** db1 close
384**
385** The first command opens a connection to the "my_database" database
386** and calls that connection "db1". The second command causes this
387** subroutine to be invoked.
388*/
drh6d313162000-09-21 13:01:35 +0000389static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000390 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000391 int choice;
drh22fbcb82004-02-01 01:22:50 +0000392 int rc = TCL_OK;
drh0de8c112002-07-06 16:32:14 +0000393 static const char *DB_strs[] = {
drh0f14e2e2004-06-29 12:39:08 +0000394 "authorizer", "busy", "changes",
395 "close", "collate", "collation_needed",
396 "commit_hook", "complete", "errorcode",
397 "eval", "function", "last_insert_rowid",
398 "onecolumn", "progress", "rekey",
399 "timeout", "total_changes", "trace",
400 0
drh6d313162000-09-21 13:01:35 +0000401 };
drh411995d2002-06-25 19:31:18 +0000402 enum DB_enum {
drh0f14e2e2004-06-29 12:39:08 +0000403 DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
404 DB_CLOSE, DB_COLLATE, DB_COLLATION_NEEDED,
405 DB_COMMIT_HOOK, DB_COMPLETE, DB_ERRORCODE,
406 DB_EVAL, DB_FUNCTION, DB_LAST_INSERT_ROWID,
407 DB_ONECOLUMN, DB_PROGRESS, DB_REKEY,
408 DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE,
drh6d313162000-09-21 13:01:35 +0000409 };
410
411 if( objc<2 ){
412 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000413 return TCL_ERROR;
414 }
drh411995d2002-06-25 19:31:18 +0000415 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000416 return TCL_ERROR;
417 }
418
drh411995d2002-06-25 19:31:18 +0000419 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000420
drhe22a3342003-04-22 20:30:37 +0000421 /* $db authorizer ?CALLBACK?
422 **
423 ** Invoke the given callback to authorize each SQL operation as it is
424 ** compiled. 5 arguments are appended to the callback before it is
425 ** invoked:
426 **
427 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
428 ** (2) First descriptive name (depends on authorization type)
429 ** (3) Second descriptive name
430 ** (4) Name of the database (ex: "main", "temp")
431 ** (5) Name of trigger that is doing the access
432 **
433 ** The callback should return on of the following strings: SQLITE_OK,
434 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
435 **
436 ** If this method is invoked with no arguments, the current authorization
437 ** callback string is returned.
438 */
439 case DB_AUTHORIZER: {
drh1211de32004-07-26 12:24:22 +0000440#ifdef SQLITE_OMIT_AUTHORIZATION
441 Tcl_AppendResult(interp, "authorization not available in this build", 0);
442 return TCL_ERROR;
443#else
drhe22a3342003-04-22 20:30:37 +0000444 if( objc>3 ){
445 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +0000446 return TCL_ERROR;
drhe22a3342003-04-22 20:30:37 +0000447 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000448 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000449 Tcl_AppendResult(interp, pDb->zAuth, 0);
450 }
451 }else{
452 char *zAuth;
453 int len;
454 if( pDb->zAuth ){
455 Tcl_Free(pDb->zAuth);
456 }
457 zAuth = Tcl_GetStringFromObj(objv[2], &len);
458 if( zAuth && len>0 ){
459 pDb->zAuth = Tcl_Alloc( len + 1 );
460 strcpy(pDb->zAuth, zAuth);
461 }else{
462 pDb->zAuth = 0;
463 }
drhe22a3342003-04-22 20:30:37 +0000464 if( pDb->zAuth ){
465 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000466 sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
drhe22a3342003-04-22 20:30:37 +0000467 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000468 sqlite3_set_authorizer(pDb->db, 0, 0);
drhe22a3342003-04-22 20:30:37 +0000469 }
drhe22a3342003-04-22 20:30:37 +0000470 }
drh1211de32004-07-26 12:24:22 +0000471#endif
drhe22a3342003-04-22 20:30:37 +0000472 break;
473 }
474
drhbec3f402000-08-04 13:49:02 +0000475 /* $db busy ?CALLBACK?
476 **
477 ** Invoke the given callback if an SQL statement attempts to open
478 ** a locked database file.
479 */
drh6d313162000-09-21 13:01:35 +0000480 case DB_BUSY: {
481 if( objc>3 ){
482 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000483 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000484 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000485 if( pDb->zBusy ){
486 Tcl_AppendResult(interp, pDb->zBusy, 0);
487 }
488 }else{
drh6d313162000-09-21 13:01:35 +0000489 char *zBusy;
490 int len;
drhbec3f402000-08-04 13:49:02 +0000491 if( pDb->zBusy ){
492 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000493 }
drh6d313162000-09-21 13:01:35 +0000494 zBusy = Tcl_GetStringFromObj(objv[2], &len);
495 if( zBusy && len>0 ){
496 pDb->zBusy = Tcl_Alloc( len + 1 );
497 strcpy(pDb->zBusy, zBusy);
498 }else{
499 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000500 }
501 if( pDb->zBusy ){
502 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000503 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000504 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000505 sqlite3_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000506 }
507 }
drh6d313162000-09-21 13:01:35 +0000508 break;
509 }
drhbec3f402000-08-04 13:49:02 +0000510
danielk1977348bb5d2003-10-18 09:37:26 +0000511 /* $db progress ?N CALLBACK?
512 **
513 ** Invoke the given callback every N virtual machine opcodes while executing
514 ** queries.
515 */
516 case DB_PROGRESS: {
517 if( objc==2 ){
518 if( pDb->zProgress ){
519 Tcl_AppendResult(interp, pDb->zProgress, 0);
520 }
521 }else if( objc==4 ){
522 char *zProgress;
523 int len;
524 int N;
525 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
526 return TCL_ERROR;
527 };
528 if( pDb->zProgress ){
529 Tcl_Free(pDb->zProgress);
530 }
531 zProgress = Tcl_GetStringFromObj(objv[3], &len);
532 if( zProgress && len>0 ){
533 pDb->zProgress = Tcl_Alloc( len + 1 );
534 strcpy(pDb->zProgress, zProgress);
535 }else{
536 pDb->zProgress = 0;
537 }
538#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
539 if( pDb->zProgress ){
540 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000541 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
danielk1977348bb5d2003-10-18 09:37:26 +0000542 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000543 sqlite3_progress_handler(pDb->db, 0, 0, 0);
danielk1977348bb5d2003-10-18 09:37:26 +0000544 }
545#endif
546 }else{
547 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
548 return TCL_ERROR;
549 }
550 break;
551 }
552
danielk1977b28af712004-06-21 06:50:26 +0000553 /* $db changes
drhc8d30ac2002-04-12 10:08:59 +0000554 **
555 ** Return the number of rows that were modified, inserted, or deleted by
danielk1977b28af712004-06-21 06:50:26 +0000556 ** the most recent INSERT, UPDATE or DELETE statement, not including
557 ** any changes made by trigger programs.
drhc8d30ac2002-04-12 10:08:59 +0000558 */
559 case DB_CHANGES: {
560 Tcl_Obj *pResult;
drhc8d30ac2002-04-12 10:08:59 +0000561 if( objc!=2 ){
562 Tcl_WrongNumArgs(interp, 2, objv, "");
563 return TCL_ERROR;
564 }
drhc8d30ac2002-04-12 10:08:59 +0000565 pResult = Tcl_GetObjResult(interp);
danielk1977b28af712004-06-21 06:50:26 +0000566 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db));
rdcf146a772004-02-25 22:51:06 +0000567 break;
568 }
569
drh75897232000-05-29 14:26:00 +0000570 /* $db close
571 **
572 ** Shutdown the database
573 */
drh6d313162000-09-21 13:01:35 +0000574 case DB_CLOSE: {
575 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
576 break;
577 }
drh75897232000-05-29 14:26:00 +0000578
drhaa940ea2004-01-15 02:44:03 +0000579 /* $db commit_hook ?CALLBACK?
580 **
581 ** Invoke the given callback just before committing every SQL transaction.
582 ** If the callback throws an exception or returns non-zero, then the
583 ** transaction is aborted. If CALLBACK is an empty string, the callback
584 ** is disabled.
585 */
586 case DB_COMMIT_HOOK: {
587 if( objc>3 ){
588 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +0000589 return TCL_ERROR;
drhaa940ea2004-01-15 02:44:03 +0000590 }else if( objc==2 ){
591 if( pDb->zCommit ){
592 Tcl_AppendResult(interp, pDb->zCommit, 0);
593 }
594 }else{
595 char *zCommit;
596 int len;
597 if( pDb->zCommit ){
598 Tcl_Free(pDb->zCommit);
599 }
600 zCommit = Tcl_GetStringFromObj(objv[2], &len);
601 if( zCommit && len>0 ){
602 pDb->zCommit = Tcl_Alloc( len + 1 );
603 strcpy(pDb->zCommit, zCommit);
604 }else{
605 pDb->zCommit = 0;
606 }
607 if( pDb->zCommit ){
608 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000609 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
drhaa940ea2004-01-15 02:44:03 +0000610 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000611 sqlite3_commit_hook(pDb->db, 0, 0);
drhaa940ea2004-01-15 02:44:03 +0000612 }
613 }
614 break;
615 }
616
drh0f14e2e2004-06-29 12:39:08 +0000617 /*
618 ** $db collate NAME SCRIPT
619 **
620 ** Create a new SQL collation function called NAME. Whenever
621 ** that function is called, invoke SCRIPT to evaluate the function.
622 */
623 case DB_COLLATE: {
624 SqlCollate *pCollate;
625 char *zName;
626 char *zScript;
627 int nScript;
628 if( objc!=4 ){
629 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
630 return TCL_ERROR;
631 }
632 zName = Tcl_GetStringFromObj(objv[2], 0);
633 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
634 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
635 if( pCollate==0 ) return TCL_ERROR;
636 pCollate->interp = interp;
637 pCollate->pNext = pDb->pCollate;
638 pCollate->zScript = (char*)&pCollate[1];
639 pDb->pCollate = pCollate;
640 strcpy(pCollate->zScript, zScript);
641 if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
642 pCollate, tclSqlCollate) ){
643 return TCL_ERROR;
644 }
645 break;
646 }
647
648 /*
649 ** $db collation_needed SCRIPT
650 **
651 ** Create a new SQL collation function called NAME. Whenever
652 ** that function is called, invoke SCRIPT to evaluate the function.
653 */
654 case DB_COLLATION_NEEDED: {
655 if( objc!=3 ){
656 Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT");
657 return TCL_ERROR;
658 }
659 if( pDb->pCollateNeeded ){
660 Tcl_DecrRefCount(pDb->pCollateNeeded);
661 }
662 pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]);
663 Tcl_IncrRefCount(pDb->pCollateNeeded);
664 sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded);
665 break;
666 }
667
drh75897232000-05-29 14:26:00 +0000668 /* $db complete SQL
669 **
670 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
671 ** additional lines of input are needed. This is similar to the
672 ** built-in "info complete" command of Tcl.
673 */
drh6d313162000-09-21 13:01:35 +0000674 case DB_COMPLETE: {
675 Tcl_Obj *pResult;
676 int isComplete;
677 if( objc!=3 ){
678 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000679 return TCL_ERROR;
680 }
danielk19776f8a5032004-05-10 10:34:51 +0000681 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
drh6d313162000-09-21 13:01:35 +0000682 pResult = Tcl_GetObjResult(interp);
683 Tcl_SetBooleanObj(pResult, isComplete);
684 break;
685 }
drhdcd997e2003-01-31 17:21:49 +0000686
687 /*
688 ** $db errorcode
689 **
690 ** Return the numeric error code that was returned by the most recent
danielk19776f8a5032004-05-10 10:34:51 +0000691 ** call to sqlite3_exec().
drhdcd997e2003-01-31 17:21:49 +0000692 */
693 case DB_ERRORCODE: {
danielk1977f3ce83f2004-06-14 11:43:46 +0000694 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
drhdcd997e2003-01-31 17:21:49 +0000695 break;
696 }
drh75897232000-05-29 14:26:00 +0000697
698 /*
699 ** $db eval $sql ?array { ...code... }?
700 **
701 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000702 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000703 ** If "array" and "code" are omitted, then no callback is every invoked.
704 ** If "array" is an empty string, then the values are placed in variables
705 ** that have the same name as the fields extracted by the query.
706 */
drh6d313162000-09-21 13:01:35 +0000707 case DB_EVAL: {
danielk197730ccda12004-05-27 12:11:31 +0000708 char const *zSql;
709 char const *zLeft;
710 sqlite3_stmt *pStmt;
danielk1977ef2cb632004-05-29 02:37:19 +0000711
712 Tcl_Obj *pRet = Tcl_NewObj();
713 Tcl_IncrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000714
715 if( objc!=5 && objc!=3 ){
716 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
717 return TCL_ERROR;
718 }
719
danielk197730ccda12004-05-27 12:11:31 +0000720 zSql = Tcl_GetStringFromObj(objv[2], 0);
721 while( zSql[0] ){
722 int i;
723
724 if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000725 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000726 rc = TCL_ERROR;
727 break;
728 }
729
730 if( pStmt && objc==5 ){
731 Tcl_Obj *pColList = Tcl_NewObj();
732 Tcl_IncrRefCount(pColList);
733
734 for(i=0; i<sqlite3_column_count(pStmt); i++){
735 Tcl_ListObjAppendElement(interp, pColList,
danielk1977ef2cb632004-05-29 02:37:19 +0000736 dbTextToObj(sqlite3_column_name(pStmt, i))
danielk197730ccda12004-05-27 12:11:31 +0000737 );
738 }
739 Tcl_ObjSetVar2(interp,objv[3],Tcl_NewStringObj("*",-1),pColList,0);
740 }
741
742 while( pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
743 for(i=0; i<sqlite3_column_count(pStmt); i++){
744 Tcl_Obj *pVal;
745
746 /* Set pVal to contain the i'th column of this row. */
drh9c054832004-05-31 18:51:57 +0000747 if( SQLITE_BLOB!=sqlite3_column_type(pStmt, i) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000748 pVal = dbTextToObj(sqlite3_column_text(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000749 }else{
danielk19773fd0a732004-05-27 13:35:19 +0000750 int bytes = sqlite3_column_bytes(pStmt, i);
751 pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes);
danielk197730ccda12004-05-27 12:11:31 +0000752 }
753
754 if( objc==5 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000755 Tcl_Obj *pName = dbTextToObj(sqlite3_column_name(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000756 Tcl_IncrRefCount(pName);
757 if( !strcmp("", Tcl_GetString(objv[3])) ){
758 Tcl_ObjSetVar2(interp, pName, 0, pVal, 0);
759 }else{
760 Tcl_ObjSetVar2(interp, objv[3], pName, pVal, 0);
761 }
762 Tcl_DecrRefCount(pName);
763 }else{
danielk197730ccda12004-05-27 12:11:31 +0000764 Tcl_ListObjAppendElement(interp, pRet, pVal);
765 }
766 }
767
768 if( objc==5 ){
769 rc = Tcl_EvalObjEx(interp, objv[4], 0);
770 if( rc!=TCL_ERROR ) rc = TCL_OK;
771 }
772 }
773
774 if( pStmt && SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){
775 continue;
776 }
777
778 if( pStmt && SQLITE_OK!=sqlite3_errcode(pDb->db) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000779 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000780 rc = TCL_ERROR;
781 break;
782 }
783
danielk197730ccda12004-05-27 12:11:31 +0000784 zSql = zLeft;
785 }
786
danielk1977ef2cb632004-05-29 02:37:19 +0000787 if( rc==TCL_OK ){
danielk197730ccda12004-05-27 12:11:31 +0000788 Tcl_SetObjResult(interp, pRet);
danielk197730ccda12004-05-27 12:11:31 +0000789 }
danielk1977ef2cb632004-05-29 02:37:19 +0000790 Tcl_DecrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000791
792 break;
793 }
drhbec3f402000-08-04 13:49:02 +0000794
795 /*
drhcabb0812002-09-14 13:47:32 +0000796 ** $db function NAME SCRIPT
797 **
798 ** Create a new SQL function called NAME. Whenever that function is
799 ** called, invoke SCRIPT to evaluate the function.
800 */
801 case DB_FUNCTION: {
802 SqlFunc *pFunc;
803 char *zName;
804 char *zScript;
805 int nScript;
806 if( objc!=4 ){
807 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
808 return TCL_ERROR;
809 }
810 zName = Tcl_GetStringFromObj(objv[2], 0);
811 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
812 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
813 if( pFunc==0 ) return TCL_ERROR;
814 pFunc->interp = interp;
815 pFunc->pNext = pDb->pFunc;
816 pFunc->zScript = (char*)&pFunc[1];
drh0f14e2e2004-06-29 12:39:08 +0000817 pDb->pFunc = pFunc;
drhcabb0812002-09-14 13:47:32 +0000818 strcpy(pFunc->zScript, zScript);
danielk1977c8c11582004-06-29 13:41:21 +0000819 rc = sqlite3_create_function(pDb->db, zName, -1, SQLITE_UTF8,
danielk1977d8123362004-06-12 09:25:12 +0000820 pFunc, tclSqlFunc, 0, 0);
danielk1977c8c11582004-06-29 13:41:21 +0000821 if( rc!=SQLITE_OK ) rc = TCL_ERROR;
drhcabb0812002-09-14 13:47:32 +0000822 break;
823 }
824
825 /*
drhaf9ff332002-01-16 21:00:27 +0000826 ** $db last_insert_rowid
827 **
828 ** Return an integer which is the ROWID for the most recent insert.
829 */
830 case DB_LAST_INSERT_ROWID: {
831 Tcl_Obj *pResult;
832 int rowid;
833 if( objc!=2 ){
834 Tcl_WrongNumArgs(interp, 2, objv, "");
835 return TCL_ERROR;
836 }
danielk19776f8a5032004-05-10 10:34:51 +0000837 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +0000838 pResult = Tcl_GetObjResult(interp);
839 Tcl_SetIntObj(pResult, rowid);
840 break;
841 }
842
843 /*
drh5d9d7572003-08-19 14:31:01 +0000844 ** $db onecolumn SQL
845 **
846 ** Return a single column from a single row of the given SQL query.
847 */
848 case DB_ONECOLUMN: {
drh5d9d7572003-08-19 14:31:01 +0000849 char *zSql;
850 char *zErrMsg = 0;
851 if( objc!=3 ){
852 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
853 return TCL_ERROR;
854 }
855 zSql = Tcl_GetStringFromObj(objv[2], 0);
danielk19776f8a5032004-05-10 10:34:51 +0000856 rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
drh5d9d7572003-08-19 14:31:01 +0000857 if( rc==SQLITE_ABORT ){
drh22fbcb82004-02-01 01:22:50 +0000858 rc = SQLITE_OK;
drh5d9d7572003-08-19 14:31:01 +0000859 }else if( zErrMsg ){
860 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
861 free(zErrMsg);
862 rc = TCL_ERROR;
863 }else if( rc!=SQLITE_OK ){
danielk1977f20b21c2004-05-31 23:56:42 +0000864 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh5d9d7572003-08-19 14:31:01 +0000865 rc = TCL_ERROR;
866 }
867 break;
868 }
869
870 /*
drh22fbcb82004-02-01 01:22:50 +0000871 ** $db rekey KEY
872 **
873 ** Change the encryption key on the currently open database.
874 */
875 case DB_REKEY: {
876 int nKey;
877 void *pKey;
878 if( objc!=3 ){
879 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
880 return TCL_ERROR;
881 }
882 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +0000883#ifdef SQLITE_HAS_CODEC
drh2011d5f2004-07-22 02:40:37 +0000884 rc = sqlite3_rekey(pDb->db, pKey, nKey);
drh22fbcb82004-02-01 01:22:50 +0000885 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000886 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh22fbcb82004-02-01 01:22:50 +0000887 rc = TCL_ERROR;
888 }
889#endif
890 break;
891 }
892
893 /*
drhbec3f402000-08-04 13:49:02 +0000894 ** $db timeout MILLESECONDS
895 **
896 ** Delay for the number of milliseconds specified when a file is locked.
897 */
drh6d313162000-09-21 13:01:35 +0000898 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000899 int ms;
drh6d313162000-09-21 13:01:35 +0000900 if( objc!=3 ){
901 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000902 return TCL_ERROR;
903 }
drh6d313162000-09-21 13:01:35 +0000904 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +0000905 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000906 break;
drh75897232000-05-29 14:26:00 +0000907 }
drhb5a20d32003-04-23 12:25:23 +0000908
drh0f14e2e2004-06-29 12:39:08 +0000909 /*
910 ** $db total_changes
911 **
912 ** Return the number of rows that were modified, inserted, or deleted
913 ** since the database handle was created.
914 */
915 case DB_TOTAL_CHANGES: {
916 Tcl_Obj *pResult;
917 if( objc!=2 ){
918 Tcl_WrongNumArgs(interp, 2, objv, "");
919 return TCL_ERROR;
920 }
921 pResult = Tcl_GetObjResult(interp);
922 Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db));
923 break;
924 }
925
drhb5a20d32003-04-23 12:25:23 +0000926 /* $db trace ?CALLBACK?
927 **
928 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
929 ** that is executed. The text of the SQL is appended to CALLBACK before
930 ** it is executed.
931 */
932 case DB_TRACE: {
933 if( objc>3 ){
934 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drhb97759e2004-06-29 11:26:59 +0000935 return TCL_ERROR;
drhb5a20d32003-04-23 12:25:23 +0000936 }else if( objc==2 ){
937 if( pDb->zTrace ){
938 Tcl_AppendResult(interp, pDb->zTrace, 0);
939 }
940 }else{
941 char *zTrace;
942 int len;
943 if( pDb->zTrace ){
944 Tcl_Free(pDb->zTrace);
945 }
946 zTrace = Tcl_GetStringFromObj(objv[2], &len);
947 if( zTrace && len>0 ){
948 pDb->zTrace = Tcl_Alloc( len + 1 );
949 strcpy(pDb->zTrace, zTrace);
950 }else{
951 pDb->zTrace = 0;
952 }
953 if( pDb->zTrace ){
954 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000955 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +0000956 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000957 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +0000958 }
959 }
960 break;
961 }
962
drh6d313162000-09-21 13:01:35 +0000963 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +0000964 return rc;
drh75897232000-05-29 14:26:00 +0000965}
966
967/*
drh22fbcb82004-02-01 01:22:50 +0000968** sqlite DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +0000969**
970** This is the main Tcl command. When the "sqlite" Tcl command is
971** invoked, this routine runs to process that command.
972**
973** The first argument, DBNAME, is an arbitrary name for a new
974** database connection. This command creates a new command named
975** DBNAME that is used to control that connection. The database
976** connection is deleted when the DBNAME command is deleted.
977**
978** The second argument is the name of the directory that contains
979** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000980**
981** For testing purposes, we also support the following:
982**
983** sqlite -encoding
984**
985** Return the encoding used by LIKE and GLOB operators. Choices
986** are UTF-8 and iso8859.
987**
drh647cb0e2002-11-04 19:32:25 +0000988** sqlite -version
989**
990** Return the version number of the SQLite library.
991**
drhfbc3eab2001-04-06 16:13:42 +0000992** sqlite -tcl-uses-utf
993**
994** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
995** not. Used by tests to make sure the library was compiled
996** correctly.
drh75897232000-05-29 14:26:00 +0000997*/
drh22fbcb82004-02-01 01:22:50 +0000998static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000999 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +00001000 void *pKey = 0;
1001 int nKey = 0;
1002 const char *zArg;
drh75897232000-05-29 14:26:00 +00001003 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +00001004 const char *zFile;
drh06b27182002-06-26 20:06:05 +00001005 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +00001006 if( objc==2 ){
1007 zArg = Tcl_GetStringFromObj(objv[1], 0);
drh22fbcb82004-02-01 01:22:50 +00001008 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001009 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +00001010 return TCL_OK;
1011 }
drh9eb9e262004-02-11 02:18:05 +00001012 if( strcmp(zArg,"-has-codec")==0 ){
1013#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001014 Tcl_AppendResult(interp,"1",0);
1015#else
1016 Tcl_AppendResult(interp,"0",0);
1017#endif
1018 return TCL_OK;
1019 }
1020 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +00001021#ifdef TCL_UTF_MAX
1022 Tcl_AppendResult(interp,"1",0);
1023#else
1024 Tcl_AppendResult(interp,"0",0);
1025#endif
1026 return TCL_OK;
1027 }
1028 }
drh22fbcb82004-02-01 01:22:50 +00001029 if( objc==5 || objc==6 ){
1030 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
1031 if( strcmp(zArg,"-key")==0 ){
1032 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
1033 objc -= 2;
1034 }
1035 }
1036 if( objc!=3 && objc!=4 ){
1037 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +00001038#ifdef SQLITE_HAS_CODEC
1039 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +00001040#else
1041 "HANDLE FILENAME ?MODE?"
1042#endif
1043 );
drh75897232000-05-29 14:26:00 +00001044 return TCL_ERROR;
1045 }
drh75897232000-05-29 14:26:00 +00001046 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001047 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001048 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001049 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1050 return TCL_ERROR;
1051 }
1052 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00001053 zFile = Tcl_GetStringFromObj(objv[2], 0);
danielk19774f057f92004-06-08 00:02:33 +00001054 sqlite3_open(zFile, &p->db);
danielk197780290862004-05-22 09:21:21 +00001055 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
1056 zErrMsg = strdup(sqlite3_errmsg(p->db));
1057 sqlite3_close(p->db);
1058 p->db = 0;
1059 }
drh2011d5f2004-07-22 02:40:37 +00001060#ifdef SQLITE_HAS_CODEC
1061 sqlite3_key(p->db, pKey, nKey);
drheb8ed702004-02-11 10:37:23 +00001062#endif
drhbec3f402000-08-04 13:49:02 +00001063 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001064 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001065 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001066 free(zErrMsg);
1067 return TCL_ERROR;
1068 }
drh22fbcb82004-02-01 01:22:50 +00001069 zArg = Tcl_GetStringFromObj(objv[1], 0);
1070 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001071
drh06b27182002-06-26 20:06:05 +00001072 /* The return value is the value of the sqlite* pointer
1073 */
1074 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001075 if( strncmp(zBuf,"0x",2) ){
1076 sprintf(zBuf, "0x%p", p->db);
1077 }
drh06b27182002-06-26 20:06:05 +00001078 Tcl_AppendResult(interp, zBuf, 0);
1079
drhc22bd472002-05-10 13:14:07 +00001080 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001081 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001082 */
drh28b4e482002-03-11 02:06:13 +00001083#ifdef SQLITE_TEST
1084 {
drhc22bd472002-05-10 13:14:07 +00001085 extern void Md5_Register(sqlite*);
danielk19775b59af82004-06-30 12:42:59 +00001086#ifdef SQLITE_DEBUG
danielk197713073932004-06-30 11:54:06 +00001087 int mallocfail = sqlite3_iMallocFail;
1088 sqlite3_iMallocFail = 0;
danielk19775b59af82004-06-30 12:42:59 +00001089#endif
drhc22bd472002-05-10 13:14:07 +00001090 Md5_Register(p->db);
danielk19775b59af82004-06-30 12:42:59 +00001091#ifdef SQLITE_DEBUG
danielk197713073932004-06-30 11:54:06 +00001092 sqlite3_iMallocFail = mallocfail;
danielk19775b59af82004-06-30 12:42:59 +00001093#endif
drh06b27182002-06-26 20:06:05 +00001094 }
drh28b4e482002-03-11 02:06:13 +00001095#endif
danielk19777cedc8d2004-06-10 10:50:08 +00001096 p->interp = interp;
drh75897232000-05-29 14:26:00 +00001097 return TCL_OK;
1098}
1099
1100/*
drh90ca9752001-09-28 17:47:14 +00001101** Provide a dummy Tcl_InitStubs if we are using this as a static
1102** library.
1103*/
1104#ifndef USE_TCL_STUBS
1105# undef Tcl_InitStubs
1106# define Tcl_InitStubs(a,b,c)
1107#endif
1108
1109/*
drh75897232000-05-29 14:26:00 +00001110** Initialize this module.
1111**
1112** This Tcl module contains only a single new Tcl command named "sqlite".
1113** (Hence there is no namespace. There is no point in using a namespace
1114** if the extension only supplies one new name!) The "sqlite" command is
1115** used to open a new SQLite database. See the DbMain() routine above
1116** for additional information.
1117*/
drh38f82712004-06-18 17:10:16 +00001118int Sqlite3_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001119 Tcl_InitStubs(interp, "8.0", 0);
drhef4ac8f2004-06-19 00:16:31 +00001120 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1121 Tcl_PkgProvide(interp, "sqlite3", "3.0");
drh90ca9752001-09-28 17:47:14 +00001122 return TCL_OK;
1123}
drh38f82712004-06-18 17:10:16 +00001124int Tclsqlite3_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001125 Tcl_InitStubs(interp, "8.0", 0);
drhef4ac8f2004-06-19 00:16:31 +00001126 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1127 Tcl_PkgProvide(interp, "sqlite3", "3.0");
drh75897232000-05-29 14:26:00 +00001128 return TCL_OK;
1129}
drh38f82712004-06-18 17:10:16 +00001130int Sqlite3_SafeInit(Tcl_Interp *interp){
drh75897232000-05-29 14:26:00 +00001131 return TCL_OK;
1132}
drh38f82712004-06-18 17:10:16 +00001133int Tclsqlite3_SafeInit(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001134 return TCL_OK;
1135}
drh75897232000-05-29 14:26:00 +00001136
drh3e27c022004-07-23 00:01:38 +00001137#ifdef TCLSH
1138/*****************************************************************************
1139** The code that follows is used to build standalone TCL interpreters
drh75897232000-05-29 14:26:00 +00001140*/
drh348784e2000-05-29 20:41:49 +00001141
1142/*
drh3e27c022004-07-23 00:01:38 +00001143** If the macro TCLSH is one, then put in code this for the
1144** "main" routine that will initialize Tcl and take input from
1145** standard input.
drh348784e2000-05-29 20:41:49 +00001146*/
drh3e27c022004-07-23 00:01:38 +00001147#if TCLSH==1
drh348784e2000-05-29 20:41:49 +00001148static char zMainloop[] =
1149 "set line {}\n"
1150 "while {![eof stdin]} {\n"
1151 "if {$line!=\"\"} {\n"
1152 "puts -nonewline \"> \"\n"
1153 "} else {\n"
1154 "puts -nonewline \"% \"\n"
1155 "}\n"
1156 "flush stdout\n"
1157 "append line [gets stdin]\n"
1158 "if {[info complete $line]} {\n"
1159 "if {[catch {uplevel #0 $line} result]} {\n"
1160 "puts stderr \"Error: $result\"\n"
1161 "} elseif {$result!=\"\"} {\n"
1162 "puts $result\n"
1163 "}\n"
1164 "set line {}\n"
1165 "} else {\n"
1166 "append line \\n\n"
1167 "}\n"
1168 "}\n"
1169;
drh3e27c022004-07-23 00:01:38 +00001170#endif
1171
1172/*
1173** If the macro TCLSH is two, then get the main loop code out of
1174** the separate file "spaceanal_tcl.h".
1175*/
1176#if TCLSH==2
1177static char zMainloop[] =
1178#include "spaceanal_tcl.h"
1179;
1180#endif
drh348784e2000-05-29 20:41:49 +00001181
1182#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1183int TCLSH_MAIN(int argc, char **argv){
1184 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001185 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001186 interp = Tcl_CreateInterp();
drh38f82712004-06-18 17:10:16 +00001187 Sqlite3_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001188#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001189 {
1190 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001191 extern int Sqlitetest2_Init(Tcl_Interp*);
1192 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001193 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001194 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001195 extern int Md5_Init(Tcl_Interp*);
danielk19776490beb2004-05-11 06:17:21 +00001196 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001197 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001198 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00001199 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00001200 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001201 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001202 }
1203#endif
drh3e27c022004-07-23 00:01:38 +00001204 if( argc>=2 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00001205 int i;
1206 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1207 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1208 for(i=2; i<argc; i++){
1209 Tcl_SetVar(interp, "argv", argv[i],
1210 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1211 }
drh3e27c022004-07-23 00:01:38 +00001212 if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001213 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001214 if( zInfo==0 ) zInfo = interp->result;
1215 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001216 return 1;
1217 }
drh3e27c022004-07-23 00:01:38 +00001218 }
1219 if( argc<=1 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00001220 Tcl_GlobalEval(interp, zMainloop);
1221 }
1222 return 0;
1223}
1224#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001225
1226#endif /* !defined(NO_TCL) */