blob: 652f65f09937eac2fd7dcfaa6c74a214ebb10113 [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**
danielk197713073932004-06-30 11:54:06 +000014** $Id: tclsqlite.c,v 1.94 2004/06/30 11:54:07 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/*
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: {
440 if( objc>3 ){
441 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +0000442 return TCL_ERROR;
drhe22a3342003-04-22 20:30:37 +0000443 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000444 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000445 Tcl_AppendResult(interp, pDb->zAuth, 0);
446 }
447 }else{
448 char *zAuth;
449 int len;
450 if( pDb->zAuth ){
451 Tcl_Free(pDb->zAuth);
452 }
453 zAuth = Tcl_GetStringFromObj(objv[2], &len);
454 if( zAuth && len>0 ){
455 pDb->zAuth = Tcl_Alloc( len + 1 );
456 strcpy(pDb->zAuth, zAuth);
457 }else{
458 pDb->zAuth = 0;
459 }
460#ifndef SQLITE_OMIT_AUTHORIZATION
461 if( pDb->zAuth ){
462 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000463 sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
drhe22a3342003-04-22 20:30:37 +0000464 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000465 sqlite3_set_authorizer(pDb->db, 0, 0);
drhe22a3342003-04-22 20:30:37 +0000466 }
467#endif
468 }
469 break;
470 }
471
drhbec3f402000-08-04 13:49:02 +0000472 /* $db busy ?CALLBACK?
473 **
474 ** Invoke the given callback if an SQL statement attempts to open
475 ** a locked database file.
476 */
drh6d313162000-09-21 13:01:35 +0000477 case DB_BUSY: {
478 if( objc>3 ){
479 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000480 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000481 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000482 if( pDb->zBusy ){
483 Tcl_AppendResult(interp, pDb->zBusy, 0);
484 }
485 }else{
drh6d313162000-09-21 13:01:35 +0000486 char *zBusy;
487 int len;
drhbec3f402000-08-04 13:49:02 +0000488 if( pDb->zBusy ){
489 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000490 }
drh6d313162000-09-21 13:01:35 +0000491 zBusy = Tcl_GetStringFromObj(objv[2], &len);
492 if( zBusy && len>0 ){
493 pDb->zBusy = Tcl_Alloc( len + 1 );
494 strcpy(pDb->zBusy, zBusy);
495 }else{
496 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000497 }
498 if( pDb->zBusy ){
499 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000500 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000501 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000502 sqlite3_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000503 }
504 }
drh6d313162000-09-21 13:01:35 +0000505 break;
506 }
drhbec3f402000-08-04 13:49:02 +0000507
danielk1977348bb5d2003-10-18 09:37:26 +0000508 /* $db progress ?N CALLBACK?
509 **
510 ** Invoke the given callback every N virtual machine opcodes while executing
511 ** queries.
512 */
513 case DB_PROGRESS: {
514 if( objc==2 ){
515 if( pDb->zProgress ){
516 Tcl_AppendResult(interp, pDb->zProgress, 0);
517 }
518 }else if( objc==4 ){
519 char *zProgress;
520 int len;
521 int N;
522 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
523 return TCL_ERROR;
524 };
525 if( pDb->zProgress ){
526 Tcl_Free(pDb->zProgress);
527 }
528 zProgress = Tcl_GetStringFromObj(objv[3], &len);
529 if( zProgress && len>0 ){
530 pDb->zProgress = Tcl_Alloc( len + 1 );
531 strcpy(pDb->zProgress, zProgress);
532 }else{
533 pDb->zProgress = 0;
534 }
535#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
536 if( pDb->zProgress ){
537 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000538 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
danielk1977348bb5d2003-10-18 09:37:26 +0000539 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000540 sqlite3_progress_handler(pDb->db, 0, 0, 0);
danielk1977348bb5d2003-10-18 09:37:26 +0000541 }
542#endif
543 }else{
544 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
545 return TCL_ERROR;
546 }
547 break;
548 }
549
danielk1977b28af712004-06-21 06:50:26 +0000550 /* $db changes
drhc8d30ac2002-04-12 10:08:59 +0000551 **
552 ** Return the number of rows that were modified, inserted, or deleted by
danielk1977b28af712004-06-21 06:50:26 +0000553 ** the most recent INSERT, UPDATE or DELETE statement, not including
554 ** any changes made by trigger programs.
drhc8d30ac2002-04-12 10:08:59 +0000555 */
556 case DB_CHANGES: {
557 Tcl_Obj *pResult;
drhc8d30ac2002-04-12 10:08:59 +0000558 if( objc!=2 ){
559 Tcl_WrongNumArgs(interp, 2, objv, "");
560 return TCL_ERROR;
561 }
drhc8d30ac2002-04-12 10:08:59 +0000562 pResult = Tcl_GetObjResult(interp);
danielk1977b28af712004-06-21 06:50:26 +0000563 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db));
rdcf146a772004-02-25 22:51:06 +0000564 break;
565 }
566
drh75897232000-05-29 14:26:00 +0000567 /* $db close
568 **
569 ** Shutdown the database
570 */
drh6d313162000-09-21 13:01:35 +0000571 case DB_CLOSE: {
572 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
573 break;
574 }
drh75897232000-05-29 14:26:00 +0000575
drhaa940ea2004-01-15 02:44:03 +0000576 /* $db commit_hook ?CALLBACK?
577 **
578 ** Invoke the given callback just before committing every SQL transaction.
579 ** If the callback throws an exception or returns non-zero, then the
580 ** transaction is aborted. If CALLBACK is an empty string, the callback
581 ** is disabled.
582 */
583 case DB_COMMIT_HOOK: {
584 if( objc>3 ){
585 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +0000586 return TCL_ERROR;
drhaa940ea2004-01-15 02:44:03 +0000587 }else if( objc==2 ){
588 if( pDb->zCommit ){
589 Tcl_AppendResult(interp, pDb->zCommit, 0);
590 }
591 }else{
592 char *zCommit;
593 int len;
594 if( pDb->zCommit ){
595 Tcl_Free(pDb->zCommit);
596 }
597 zCommit = Tcl_GetStringFromObj(objv[2], &len);
598 if( zCommit && len>0 ){
599 pDb->zCommit = Tcl_Alloc( len + 1 );
600 strcpy(pDb->zCommit, zCommit);
601 }else{
602 pDb->zCommit = 0;
603 }
604 if( pDb->zCommit ){
605 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000606 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
drhaa940ea2004-01-15 02:44:03 +0000607 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000608 sqlite3_commit_hook(pDb->db, 0, 0);
drhaa940ea2004-01-15 02:44:03 +0000609 }
610 }
611 break;
612 }
613
drh0f14e2e2004-06-29 12:39:08 +0000614 /*
615 ** $db collate NAME SCRIPT
616 **
617 ** Create a new SQL collation function called NAME. Whenever
618 ** that function is called, invoke SCRIPT to evaluate the function.
619 */
620 case DB_COLLATE: {
621 SqlCollate *pCollate;
622 char *zName;
623 char *zScript;
624 int nScript;
625 if( objc!=4 ){
626 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
627 return TCL_ERROR;
628 }
629 zName = Tcl_GetStringFromObj(objv[2], 0);
630 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
631 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
632 if( pCollate==0 ) return TCL_ERROR;
633 pCollate->interp = interp;
634 pCollate->pNext = pDb->pCollate;
635 pCollate->zScript = (char*)&pCollate[1];
636 pDb->pCollate = pCollate;
637 strcpy(pCollate->zScript, zScript);
638 if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
639 pCollate, tclSqlCollate) ){
640 return TCL_ERROR;
641 }
642 break;
643 }
644
645 /*
646 ** $db collation_needed SCRIPT
647 **
648 ** Create a new SQL collation function called NAME. Whenever
649 ** that function is called, invoke SCRIPT to evaluate the function.
650 */
651 case DB_COLLATION_NEEDED: {
652 if( objc!=3 ){
653 Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT");
654 return TCL_ERROR;
655 }
656 if( pDb->pCollateNeeded ){
657 Tcl_DecrRefCount(pDb->pCollateNeeded);
658 }
659 pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]);
660 Tcl_IncrRefCount(pDb->pCollateNeeded);
661 sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded);
662 break;
663 }
664
drh75897232000-05-29 14:26:00 +0000665 /* $db complete SQL
666 **
667 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
668 ** additional lines of input are needed. This is similar to the
669 ** built-in "info complete" command of Tcl.
670 */
drh6d313162000-09-21 13:01:35 +0000671 case DB_COMPLETE: {
672 Tcl_Obj *pResult;
673 int isComplete;
674 if( objc!=3 ){
675 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000676 return TCL_ERROR;
677 }
danielk19776f8a5032004-05-10 10:34:51 +0000678 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
drh6d313162000-09-21 13:01:35 +0000679 pResult = Tcl_GetObjResult(interp);
680 Tcl_SetBooleanObj(pResult, isComplete);
681 break;
682 }
drhdcd997e2003-01-31 17:21:49 +0000683
684 /*
685 ** $db errorcode
686 **
687 ** Return the numeric error code that was returned by the most recent
danielk19776f8a5032004-05-10 10:34:51 +0000688 ** call to sqlite3_exec().
drhdcd997e2003-01-31 17:21:49 +0000689 */
690 case DB_ERRORCODE: {
danielk1977f3ce83f2004-06-14 11:43:46 +0000691 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
drhdcd997e2003-01-31 17:21:49 +0000692 break;
693 }
drh75897232000-05-29 14:26:00 +0000694
695 /*
696 ** $db eval $sql ?array { ...code... }?
697 **
698 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000699 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000700 ** If "array" and "code" are omitted, then no callback is every invoked.
701 ** If "array" is an empty string, then the values are placed in variables
702 ** that have the same name as the fields extracted by the query.
703 */
drh6d313162000-09-21 13:01:35 +0000704 case DB_EVAL: {
danielk197730ccda12004-05-27 12:11:31 +0000705 char const *zSql;
706 char const *zLeft;
707 sqlite3_stmt *pStmt;
danielk1977ef2cb632004-05-29 02:37:19 +0000708
709 Tcl_Obj *pRet = Tcl_NewObj();
710 Tcl_IncrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000711
712 if( objc!=5 && objc!=3 ){
713 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
714 return TCL_ERROR;
715 }
716
danielk197730ccda12004-05-27 12:11:31 +0000717 zSql = Tcl_GetStringFromObj(objv[2], 0);
718 while( zSql[0] ){
719 int i;
720
721 if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000722 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000723 rc = TCL_ERROR;
724 break;
725 }
726
727 if( pStmt && objc==5 ){
728 Tcl_Obj *pColList = Tcl_NewObj();
729 Tcl_IncrRefCount(pColList);
730
731 for(i=0; i<sqlite3_column_count(pStmt); i++){
732 Tcl_ListObjAppendElement(interp, pColList,
danielk1977ef2cb632004-05-29 02:37:19 +0000733 dbTextToObj(sqlite3_column_name(pStmt, i))
danielk197730ccda12004-05-27 12:11:31 +0000734 );
735 }
736 Tcl_ObjSetVar2(interp,objv[3],Tcl_NewStringObj("*",-1),pColList,0);
737 }
738
739 while( pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
740 for(i=0; i<sqlite3_column_count(pStmt); i++){
741 Tcl_Obj *pVal;
742
743 /* Set pVal to contain the i'th column of this row. */
drh9c054832004-05-31 18:51:57 +0000744 if( SQLITE_BLOB!=sqlite3_column_type(pStmt, i) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000745 pVal = dbTextToObj(sqlite3_column_text(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000746 }else{
danielk19773fd0a732004-05-27 13:35:19 +0000747 int bytes = sqlite3_column_bytes(pStmt, i);
748 pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes);
danielk197730ccda12004-05-27 12:11:31 +0000749 }
750
751 if( objc==5 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000752 Tcl_Obj *pName = dbTextToObj(sqlite3_column_name(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000753 Tcl_IncrRefCount(pName);
754 if( !strcmp("", Tcl_GetString(objv[3])) ){
755 Tcl_ObjSetVar2(interp, pName, 0, pVal, 0);
756 }else{
757 Tcl_ObjSetVar2(interp, objv[3], pName, pVal, 0);
758 }
759 Tcl_DecrRefCount(pName);
760 }else{
danielk197730ccda12004-05-27 12:11:31 +0000761 Tcl_ListObjAppendElement(interp, pRet, pVal);
762 }
763 }
764
765 if( objc==5 ){
766 rc = Tcl_EvalObjEx(interp, objv[4], 0);
767 if( rc!=TCL_ERROR ) rc = TCL_OK;
768 }
769 }
770
771 if( pStmt && SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){
772 continue;
773 }
774
775 if( pStmt && SQLITE_OK!=sqlite3_errcode(pDb->db) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000776 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000777 rc = TCL_ERROR;
778 break;
779 }
780
danielk197730ccda12004-05-27 12:11:31 +0000781 zSql = zLeft;
782 }
783
danielk1977ef2cb632004-05-29 02:37:19 +0000784 if( rc==TCL_OK ){
danielk197730ccda12004-05-27 12:11:31 +0000785 Tcl_SetObjResult(interp, pRet);
danielk197730ccda12004-05-27 12:11:31 +0000786 }
danielk1977ef2cb632004-05-29 02:37:19 +0000787 Tcl_DecrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000788
789 break;
790 }
drhbec3f402000-08-04 13:49:02 +0000791
792 /*
drhcabb0812002-09-14 13:47:32 +0000793 ** $db function NAME SCRIPT
794 **
795 ** Create a new SQL function called NAME. Whenever that function is
796 ** called, invoke SCRIPT to evaluate the function.
797 */
798 case DB_FUNCTION: {
799 SqlFunc *pFunc;
800 char *zName;
801 char *zScript;
802 int nScript;
803 if( objc!=4 ){
804 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
805 return TCL_ERROR;
806 }
807 zName = Tcl_GetStringFromObj(objv[2], 0);
808 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
809 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
810 if( pFunc==0 ) return TCL_ERROR;
811 pFunc->interp = interp;
812 pFunc->pNext = pDb->pFunc;
813 pFunc->zScript = (char*)&pFunc[1];
drh0f14e2e2004-06-29 12:39:08 +0000814 pDb->pFunc = pFunc;
drhcabb0812002-09-14 13:47:32 +0000815 strcpy(pFunc->zScript, zScript);
danielk1977c8c11582004-06-29 13:41:21 +0000816 rc = sqlite3_create_function(pDb->db, zName, -1, SQLITE_UTF8,
danielk1977d8123362004-06-12 09:25:12 +0000817 pFunc, tclSqlFunc, 0, 0);
danielk1977c8c11582004-06-29 13:41:21 +0000818 if( rc!=SQLITE_OK ) rc = TCL_ERROR;
drhcabb0812002-09-14 13:47:32 +0000819 break;
820 }
821
822 /*
drhaf9ff332002-01-16 21:00:27 +0000823 ** $db last_insert_rowid
824 **
825 ** Return an integer which is the ROWID for the most recent insert.
826 */
827 case DB_LAST_INSERT_ROWID: {
828 Tcl_Obj *pResult;
829 int rowid;
830 if( objc!=2 ){
831 Tcl_WrongNumArgs(interp, 2, objv, "");
832 return TCL_ERROR;
833 }
danielk19776f8a5032004-05-10 10:34:51 +0000834 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +0000835 pResult = Tcl_GetObjResult(interp);
836 Tcl_SetIntObj(pResult, rowid);
837 break;
838 }
839
840 /*
drh5d9d7572003-08-19 14:31:01 +0000841 ** $db onecolumn SQL
842 **
843 ** Return a single column from a single row of the given SQL query.
844 */
845 case DB_ONECOLUMN: {
drh5d9d7572003-08-19 14:31:01 +0000846 char *zSql;
847 char *zErrMsg = 0;
848 if( objc!=3 ){
849 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
850 return TCL_ERROR;
851 }
852 zSql = Tcl_GetStringFromObj(objv[2], 0);
danielk19776f8a5032004-05-10 10:34:51 +0000853 rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
drh5d9d7572003-08-19 14:31:01 +0000854 if( rc==SQLITE_ABORT ){
drh22fbcb82004-02-01 01:22:50 +0000855 rc = SQLITE_OK;
drh5d9d7572003-08-19 14:31:01 +0000856 }else if( zErrMsg ){
857 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
858 free(zErrMsg);
859 rc = TCL_ERROR;
860 }else if( rc!=SQLITE_OK ){
danielk1977f20b21c2004-05-31 23:56:42 +0000861 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh5d9d7572003-08-19 14:31:01 +0000862 rc = TCL_ERROR;
863 }
864 break;
865 }
866
867 /*
drh22fbcb82004-02-01 01:22:50 +0000868 ** $db rekey KEY
869 **
870 ** Change the encryption key on the currently open database.
871 */
872 case DB_REKEY: {
873 int nKey;
874 void *pKey;
875 if( objc!=3 ){
876 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
877 return TCL_ERROR;
878 }
879 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +0000880#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000881 rc = sqlite_rekey(pDb->db, pKey, nKey);
882 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000883 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh22fbcb82004-02-01 01:22:50 +0000884 rc = TCL_ERROR;
885 }
886#endif
887 break;
888 }
889
890 /*
drhbec3f402000-08-04 13:49:02 +0000891 ** $db timeout MILLESECONDS
892 **
893 ** Delay for the number of milliseconds specified when a file is locked.
894 */
drh6d313162000-09-21 13:01:35 +0000895 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000896 int ms;
drh6d313162000-09-21 13:01:35 +0000897 if( objc!=3 ){
898 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000899 return TCL_ERROR;
900 }
drh6d313162000-09-21 13:01:35 +0000901 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +0000902 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000903 break;
drh75897232000-05-29 14:26:00 +0000904 }
drhb5a20d32003-04-23 12:25:23 +0000905
drh0f14e2e2004-06-29 12:39:08 +0000906 /*
907 ** $db total_changes
908 **
909 ** Return the number of rows that were modified, inserted, or deleted
910 ** since the database handle was created.
911 */
912 case DB_TOTAL_CHANGES: {
913 Tcl_Obj *pResult;
914 if( objc!=2 ){
915 Tcl_WrongNumArgs(interp, 2, objv, "");
916 return TCL_ERROR;
917 }
918 pResult = Tcl_GetObjResult(interp);
919 Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db));
920 break;
921 }
922
drhb5a20d32003-04-23 12:25:23 +0000923 /* $db trace ?CALLBACK?
924 **
925 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
926 ** that is executed. The text of the SQL is appended to CALLBACK before
927 ** it is executed.
928 */
929 case DB_TRACE: {
930 if( objc>3 ){
931 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drhb97759e2004-06-29 11:26:59 +0000932 return TCL_ERROR;
drhb5a20d32003-04-23 12:25:23 +0000933 }else if( objc==2 ){
934 if( pDb->zTrace ){
935 Tcl_AppendResult(interp, pDb->zTrace, 0);
936 }
937 }else{
938 char *zTrace;
939 int len;
940 if( pDb->zTrace ){
941 Tcl_Free(pDb->zTrace);
942 }
943 zTrace = Tcl_GetStringFromObj(objv[2], &len);
944 if( zTrace && len>0 ){
945 pDb->zTrace = Tcl_Alloc( len + 1 );
946 strcpy(pDb->zTrace, zTrace);
947 }else{
948 pDb->zTrace = 0;
949 }
950 if( pDb->zTrace ){
951 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000952 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +0000953 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000954 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +0000955 }
956 }
957 break;
958 }
959
drh6d313162000-09-21 13:01:35 +0000960 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +0000961 return rc;
drh75897232000-05-29 14:26:00 +0000962}
963
964/*
drh22fbcb82004-02-01 01:22:50 +0000965** sqlite DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +0000966**
967** This is the main Tcl command. When the "sqlite" Tcl command is
968** invoked, this routine runs to process that command.
969**
970** The first argument, DBNAME, is an arbitrary name for a new
971** database connection. This command creates a new command named
972** DBNAME that is used to control that connection. The database
973** connection is deleted when the DBNAME command is deleted.
974**
975** The second argument is the name of the directory that contains
976** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000977**
978** For testing purposes, we also support the following:
979**
980** sqlite -encoding
981**
982** Return the encoding used by LIKE and GLOB operators. Choices
983** are UTF-8 and iso8859.
984**
drh647cb0e2002-11-04 19:32:25 +0000985** sqlite -version
986**
987** Return the version number of the SQLite library.
988**
drhfbc3eab2001-04-06 16:13:42 +0000989** sqlite -tcl-uses-utf
990**
991** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
992** not. Used by tests to make sure the library was compiled
993** correctly.
drh75897232000-05-29 14:26:00 +0000994*/
drh22fbcb82004-02-01 01:22:50 +0000995static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000996 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +0000997 void *pKey = 0;
998 int nKey = 0;
999 const char *zArg;
drh75897232000-05-29 14:26:00 +00001000 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +00001001 const char *zFile;
drh06b27182002-06-26 20:06:05 +00001002 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +00001003 if( objc==2 ){
1004 zArg = Tcl_GetStringFromObj(objv[1], 0);
drh22fbcb82004-02-01 01:22:50 +00001005 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001006 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +00001007 return TCL_OK;
1008 }
drh9eb9e262004-02-11 02:18:05 +00001009 if( strcmp(zArg,"-has-codec")==0 ){
1010#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001011 Tcl_AppendResult(interp,"1",0);
1012#else
1013 Tcl_AppendResult(interp,"0",0);
1014#endif
1015 return TCL_OK;
1016 }
1017 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +00001018#ifdef TCL_UTF_MAX
1019 Tcl_AppendResult(interp,"1",0);
1020#else
1021 Tcl_AppendResult(interp,"0",0);
1022#endif
1023 return TCL_OK;
1024 }
1025 }
drh22fbcb82004-02-01 01:22:50 +00001026 if( objc==5 || objc==6 ){
1027 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
1028 if( strcmp(zArg,"-key")==0 ){
1029 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
1030 objc -= 2;
1031 }
1032 }
1033 if( objc!=3 && objc!=4 ){
1034 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +00001035#ifdef SQLITE_HAS_CODEC
1036 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +00001037#else
1038 "HANDLE FILENAME ?MODE?"
1039#endif
1040 );
drh75897232000-05-29 14:26:00 +00001041 return TCL_ERROR;
1042 }
drh75897232000-05-29 14:26:00 +00001043 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001044 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001045 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001046 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1047 return TCL_ERROR;
1048 }
1049 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00001050 zFile = Tcl_GetStringFromObj(objv[2], 0);
drh9eb9e262004-02-11 02:18:05 +00001051#ifdef SQLITE_HAS_CODEC
danielk19776f8a5032004-05-10 10:34:51 +00001052 p->db = sqlite3_open_encrypted(zFile, pKey, nKey, 0, &zErrMsg);
drheb8ed702004-02-11 10:37:23 +00001053#else
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 }
drheb8ed702004-02-11 10:37:23 +00001060#endif
drhbec3f402000-08-04 13:49:02 +00001061 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001062 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001063 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001064 free(zErrMsg);
1065 return TCL_ERROR;
1066 }
drh22fbcb82004-02-01 01:22:50 +00001067 zArg = Tcl_GetStringFromObj(objv[1], 0);
1068 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001069
drh06b27182002-06-26 20:06:05 +00001070 /* The return value is the value of the sqlite* pointer
1071 */
1072 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001073 if( strncmp(zBuf,"0x",2) ){
1074 sprintf(zBuf, "0x%p", p->db);
1075 }
drh06b27182002-06-26 20:06:05 +00001076 Tcl_AppendResult(interp, zBuf, 0);
1077
drhc22bd472002-05-10 13:14:07 +00001078 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001079 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001080 */
drh28b4e482002-03-11 02:06:13 +00001081#ifdef SQLITE_TEST
1082 {
drhc22bd472002-05-10 13:14:07 +00001083 extern void Md5_Register(sqlite*);
danielk197713073932004-06-30 11:54:06 +00001084 int mallocfail = sqlite3_iMallocFail;
1085 sqlite3_iMallocFail = 0;
drhc22bd472002-05-10 13:14:07 +00001086 Md5_Register(p->db);
danielk197713073932004-06-30 11:54:06 +00001087 sqlite3_iMallocFail = mallocfail;
drh06b27182002-06-26 20:06:05 +00001088 }
drh28b4e482002-03-11 02:06:13 +00001089#endif
danielk19777cedc8d2004-06-10 10:50:08 +00001090 p->interp = interp;
drh75897232000-05-29 14:26:00 +00001091 return TCL_OK;
1092}
1093
1094/*
drh90ca9752001-09-28 17:47:14 +00001095** Provide a dummy Tcl_InitStubs if we are using this as a static
1096** library.
1097*/
1098#ifndef USE_TCL_STUBS
1099# undef Tcl_InitStubs
1100# define Tcl_InitStubs(a,b,c)
1101#endif
1102
1103/*
drh75897232000-05-29 14:26:00 +00001104** Initialize this module.
1105**
1106** This Tcl module contains only a single new Tcl command named "sqlite".
1107** (Hence there is no namespace. There is no point in using a namespace
1108** if the extension only supplies one new name!) The "sqlite" command is
1109** used to open a new SQLite database. See the DbMain() routine above
1110** for additional information.
1111*/
drh38f82712004-06-18 17:10:16 +00001112int Sqlite3_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001113 Tcl_InitStubs(interp, "8.0", 0);
drhef4ac8f2004-06-19 00:16:31 +00001114 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1115 Tcl_PkgProvide(interp, "sqlite3", "3.0");
drh90ca9752001-09-28 17:47:14 +00001116 return TCL_OK;
1117}
drh38f82712004-06-18 17:10:16 +00001118int Tclsqlite3_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");
drh75897232000-05-29 14:26:00 +00001122 return TCL_OK;
1123}
drh38f82712004-06-18 17:10:16 +00001124int Sqlite3_SafeInit(Tcl_Interp *interp){
drh75897232000-05-29 14:26:00 +00001125 return TCL_OK;
1126}
drh38f82712004-06-18 17:10:16 +00001127int Tclsqlite3_SafeInit(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001128 return TCL_OK;
1129}
drh75897232000-05-29 14:26:00 +00001130
drh3cebbde2000-10-19 14:59:27 +00001131#if 0
drh75897232000-05-29 14:26:00 +00001132/*
1133** If compiled using mktclapp, this routine runs to initialize
1134** everything.
1135*/
1136int Et_AppInit(Tcl_Interp *interp){
drh38f82712004-06-18 17:10:16 +00001137 return Sqlite3_Init(interp);
drh75897232000-05-29 14:26:00 +00001138}
drh3cebbde2000-10-19 14:59:27 +00001139#endif
drh348784e2000-05-29 20:41:49 +00001140
1141/*
1142** If the macro TCLSH is defined and is one, then put in code for the
1143** "main" routine that will initialize Tcl.
1144*/
1145#if defined(TCLSH) && TCLSH==1
1146static char zMainloop[] =
1147 "set line {}\n"
1148 "while {![eof stdin]} {\n"
1149 "if {$line!=\"\"} {\n"
1150 "puts -nonewline \"> \"\n"
1151 "} else {\n"
1152 "puts -nonewline \"% \"\n"
1153 "}\n"
1154 "flush stdout\n"
1155 "append line [gets stdin]\n"
1156 "if {[info complete $line]} {\n"
1157 "if {[catch {uplevel #0 $line} result]} {\n"
1158 "puts stderr \"Error: $result\"\n"
1159 "} elseif {$result!=\"\"} {\n"
1160 "puts $result\n"
1161 "}\n"
1162 "set line {}\n"
1163 "} else {\n"
1164 "append line \\n\n"
1165 "}\n"
1166 "}\n"
1167;
1168
1169#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1170int TCLSH_MAIN(int argc, char **argv){
1171 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001172 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001173 interp = Tcl_CreateInterp();
drh38f82712004-06-18 17:10:16 +00001174 Sqlite3_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001175#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001176 {
1177 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001178 extern int Sqlitetest2_Init(Tcl_Interp*);
1179 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001180 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001181 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001182 extern int Md5_Init(Tcl_Interp*);
danielk19776490beb2004-05-11 06:17:21 +00001183 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001184 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001185 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00001186 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00001187 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001188 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001189 }
1190#endif
drh348784e2000-05-29 20:41:49 +00001191 if( argc>=2 ){
1192 int i;
1193 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1194 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1195 for(i=2; i<argc; i++){
1196 Tcl_SetVar(interp, "argv", argv[i],
1197 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1198 }
1199 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001200 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001201 if( zInfo==0 ) zInfo = interp->result;
1202 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001203 return 1;
1204 }
1205 }else{
1206 Tcl_GlobalEval(interp, zMainloop);
1207 }
1208 return 0;
1209}
1210#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001211
1212#endif /* !defined(NO_TCL) */