blob: 33f741d1c9b74c713d7f496f8a00b93fc51e3c34 [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**
drh895d7472004-08-20 16:02:39 +000014** $Id: tclsqlite.c,v 1.99 2004/08/20 16:02:39 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"
drh895d7472004-08-20 16:02:39 +000019#include "hash.h"
drh17a68932001-01-31 13:28:08 +000020#include "tcl.h"
drh75897232000-05-29 14:26:00 +000021#include <stdlib.h>
22#include <string.h>
drhce927062001-11-09 13:41:09 +000023#include <assert.h>
drh75897232000-05-29 14:26:00 +000024
25/*
drh98808ba2001-10-18 12:34:46 +000026** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
27** have to do a translation when going between the two. Set the
28** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
29** this translation.
30*/
31#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
32# define UTF_TRANSLATION_NEEDED 1
33#endif
34
35/*
drhcabb0812002-09-14 13:47:32 +000036** New SQL functions can be created as TCL scripts. Each such function
37** is described by an instance of the following structure.
38*/
39typedef struct SqlFunc SqlFunc;
40struct SqlFunc {
41 Tcl_Interp *interp; /* The TCL interpret to execute the function */
42 char *zScript; /* The script to be run */
43 SqlFunc *pNext; /* Next function on the list of them all */
44};
45
46/*
danielk19770202b292004-06-09 09:55:16 +000047** New collation sequences function can be created as TCL scripts. Each such
48** function is described by an instance of the following structure.
49*/
50typedef struct SqlCollate SqlCollate;
51struct SqlCollate {
52 Tcl_Interp *interp; /* The TCL interpret to execute the function */
53 char *zScript; /* The script to be run */
54 SqlCollate *pNext; /* Next function on the list of them all */
55};
56
57/*
drhbec3f402000-08-04 13:49:02 +000058** There is one instance of this structure for each SQLite database
59** that has been opened by the SQLite TCL interface.
60*/
61typedef struct SqliteDb SqliteDb;
drh895d7472004-08-20 16:02:39 +000062typedef struct SqlStmt SqlStmt;
drhbec3f402000-08-04 13:49:02 +000063struct SqliteDb {
drh895d7472004-08-20 16:02:39 +000064 sqlite3 *db; /* The "real" database structure */
drhbec3f402000-08-04 13:49:02 +000065 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000066 char *zBusy; /* The busy callback routine */
drhaa940ea2004-01-15 02:44:03 +000067 char *zCommit; /* The commit hook callback routine */
drhb5a20d32003-04-23 12:25:23 +000068 char *zTrace; /* The trace callback routine */
danielk1977348bb5d2003-10-18 09:37:26 +000069 char *zProgress; /* The progress callback routine */
drhe22a3342003-04-22 20:30:37 +000070 char *zAuth; /* The authorization callback routine */
drhcabb0812002-09-14 13:47:32 +000071 SqlFunc *pFunc; /* List of SQL functions */
danielk19770202b292004-06-09 09:55:16 +000072 SqlCollate *pCollate; /* List of SQL collation functions */
danielk19776f8a5032004-05-10 10:34:51 +000073 int rc; /* Return code of most recent sqlite3_exec() */
danielk19777cedc8d2004-06-10 10:50:08 +000074 Tcl_Obj *pCollateNeeded; /* Collation needed script */
drh895d7472004-08-20 16:02:39 +000075 SqlStmt *pStmtList; /* List of all prepared statements */
drhbec3f402000-08-04 13:49:02 +000076};
77
78/*
drh895d7472004-08-20 16:02:39 +000079** Each prepared statement is an instance of the following structure.
drh75897232000-05-29 14:26:00 +000080*/
drh895d7472004-08-20 16:02:39 +000081struct SqlStmt {
82 SqliteDb *pDb; /* The database that this statement is part of */
83 SqlStmt *pAll; /* Next statement in list of all for pDb */
84 SqlStmt **ppPrev; /* Previous pAll pointer */
85 sqlite3_stmt *pVm; /* Compiled statement. */
86 int nBind; /* Number of bindings in this statement */
87 char *azBindVar[1]; /* Name of variables for each binding */
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/*
drh895d7472004-08-20 16:02:39 +0000120** TCL calls this procedure when an sqlite3 database command is
121** deleted.
drh75897232000-05-29 14:26:00 +0000122*/
123static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000124 SqliteDb *pDb = (SqliteDb*)db;
drh895d7472004-08-20 16:02:39 +0000125 SqlStmt *pStmt, *pNextStmt;
126 for(pStmt=pDb->pStmtList; pStmt; pStmt=pNextStmt){
127 pNextStmt = pStmt->pAll;
128 sqlite3_finalize(pStmt->pVm);
129 Tcl_Free(pStmt);
130 }
danielk19776f8a5032004-05-10 10:34:51 +0000131 sqlite3_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000132 while( pDb->pFunc ){
133 SqlFunc *pFunc = pDb->pFunc;
134 pDb->pFunc = pFunc->pNext;
135 Tcl_Free((char*)pFunc);
136 }
danielk19770202b292004-06-09 09:55:16 +0000137 while( pDb->pCollate ){
138 SqlCollate *pCollate = pDb->pCollate;
139 pDb->pCollate = pCollate->pNext;
140 Tcl_Free((char*)pCollate);
141 }
drhbec3f402000-08-04 13:49:02 +0000142 if( pDb->zBusy ){
143 Tcl_Free(pDb->zBusy);
144 }
drhb5a20d32003-04-23 12:25:23 +0000145 if( pDb->zTrace ){
146 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000147 }
drhe22a3342003-04-22 20:30:37 +0000148 if( pDb->zAuth ){
149 Tcl_Free(pDb->zAuth);
150 }
drhbec3f402000-08-04 13:49:02 +0000151 Tcl_Free((char*)pDb);
152}
153
154/*
155** This routine is called when a database file is locked while trying
156** to execute SQL.
157*/
danielk19772a764eb2004-06-12 01:43:26 +0000158static int DbBusyHandler(void *cd, int nTries){
drhbec3f402000-08-04 13:49:02 +0000159 SqliteDb *pDb = (SqliteDb*)cd;
160 int rc;
161 char zVal[30];
162 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000163 Tcl_DString cmd;
164
165 Tcl_DStringInit(&cmd);
166 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
danielk19772a764eb2004-06-12 01:43:26 +0000167 sprintf(zVal, "%d", nTries);
168 Tcl_DStringAppendElement(&cmd, zVal);
drhbec3f402000-08-04 13:49:02 +0000169 zCmd = Tcl_DStringValue(&cmd);
170 rc = Tcl_Eval(pDb->interp, zCmd);
171 Tcl_DStringFree(&cmd);
172 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
173 return 0;
174 }
175 return 1;
drh75897232000-05-29 14:26:00 +0000176}
177
178/*
danielk1977348bb5d2003-10-18 09:37:26 +0000179** This routine is invoked as the 'progress callback' for the database.
180*/
181static int DbProgressHandler(void *cd){
182 SqliteDb *pDb = (SqliteDb*)cd;
183 int rc;
184
185 assert( pDb->zProgress );
186 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
187 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
188 return 1;
189 }
190 return 0;
191}
192
193/*
drhb5a20d32003-04-23 12:25:23 +0000194** This routine is called by the SQLite trace handler whenever a new
195** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000196*/
drhb5a20d32003-04-23 12:25:23 +0000197static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000198 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000199 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000200
drhb5a20d32003-04-23 12:25:23 +0000201 Tcl_DStringInit(&str);
202 Tcl_DStringAppend(&str, pDb->zTrace, -1);
203 Tcl_DStringAppendElement(&str, zSql);
204 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
205 Tcl_DStringFree(&str);
206 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000207}
208
209/*
drhaa940ea2004-01-15 02:44:03 +0000210** This routine is called when a transaction is committed. The
211** TCL script in pDb->zCommit is executed. If it returns non-zero or
212** if it throws an exception, the transaction is rolled back instead
213** of being committed.
214*/
215static int DbCommitHandler(void *cd){
216 SqliteDb *pDb = (SqliteDb*)cd;
217 int rc;
218
219 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
220 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
221 return 1;
222 }
223 return 0;
224}
225
danielk19777cedc8d2004-06-10 10:50:08 +0000226static void tclCollateNeeded(
227 void *pCtx,
228 sqlite *db,
229 int enc,
230 const char *zName
231){
232 SqliteDb *pDb = (SqliteDb *)pCtx;
233 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
234 Tcl_IncrRefCount(pScript);
235 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
236 Tcl_EvalObjEx(pDb->interp, pScript, 0);
237 Tcl_DecrRefCount(pScript);
238}
239
drhaa940ea2004-01-15 02:44:03 +0000240/*
danielk19770202b292004-06-09 09:55:16 +0000241** This routine is called to evaluate an SQL collation function implemented
242** using TCL script.
243*/
244static int tclSqlCollate(
245 void *pCtx,
246 int nA,
247 const void *zA,
248 int nB,
249 const void *zB
250){
251 SqlCollate *p = (SqlCollate *)pCtx;
252 Tcl_Obj *pCmd;
253
254 pCmd = Tcl_NewStringObj(p->zScript, -1);
255 Tcl_IncrRefCount(pCmd);
256 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA));
257 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB));
258 Tcl_EvalObjEx(p->interp, pCmd, 0);
259 Tcl_DecrRefCount(pCmd);
260 return (atoi(Tcl_GetStringResult(p->interp)));
261}
262
263/*
drhcabb0812002-09-14 13:47:32 +0000264** This routine is called to evaluate an SQL function implemented
265** using TCL script.
266*/
danielk19770ae8b832004-05-25 12:05:56 +0000267static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk19776f8a5032004-05-10 10:34:51 +0000268 SqlFunc *p = sqlite3_user_data(context);
drhcabb0812002-09-14 13:47:32 +0000269 Tcl_DString cmd;
270 int i;
271 int rc;
272
273 Tcl_DStringInit(&cmd);
274 Tcl_DStringAppend(&cmd, p->zScript, -1);
275 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000276 if( SQLITE_NULL==sqlite3_value_type(argv[i]) ){
danielk197751ad0ec2004-05-24 12:39:02 +0000277 Tcl_DStringAppendElement(&cmd, "");
278 }else{
drh4f26d6c2004-05-26 23:25:30 +0000279 Tcl_DStringAppendElement(&cmd, sqlite3_value_text(argv[i]));
danielk197751ad0ec2004-05-24 12:39:02 +0000280 }
drhcabb0812002-09-14 13:47:32 +0000281 }
282 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
283 if( rc ){
danielk19777e18c252004-05-25 11:47:24 +0000284 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhcabb0812002-09-14 13:47:32 +0000285 }else{
danielk1977d8123362004-06-12 09:25:12 +0000286 sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1,
287 SQLITE_TRANSIENT);
drhcabb0812002-09-14 13:47:32 +0000288 }
289}
drh895d7472004-08-20 16:02:39 +0000290
drhe22a3342003-04-22 20:30:37 +0000291#ifndef SQLITE_OMIT_AUTHORIZATION
292/*
293** This is the authentication function. It appends the authentication
294** type code and the two arguments to zCmd[] then invokes the result
295** on the interpreter. The reply is examined to determine if the
296** authentication fails or succeeds.
297*/
298static int auth_callback(
299 void *pArg,
300 int code,
301 const char *zArg1,
302 const char *zArg2,
303 const char *zArg3,
304 const char *zArg4
305){
306 char *zCode;
307 Tcl_DString str;
308 int rc;
309 const char *zReply;
310 SqliteDb *pDb = (SqliteDb*)pArg;
311
312 switch( code ){
313 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
314 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
315 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
316 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
317 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
318 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
319 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
320 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
321 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
322 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
323 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
324 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
325 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
326 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
327 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
328 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
329 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
330 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
331 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
332 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
333 case SQLITE_READ : zCode="SQLITE_READ"; break;
334 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
335 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
336 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000337 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
338 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
drhe22a3342003-04-22 20:30:37 +0000339 default : zCode="????"; break;
340 }
341 Tcl_DStringInit(&str);
342 Tcl_DStringAppend(&str, pDb->zAuth, -1);
343 Tcl_DStringAppendElement(&str, zCode);
344 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
345 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
346 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
347 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
348 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
349 Tcl_DStringFree(&str);
350 zReply = Tcl_GetStringResult(pDb->interp);
351 if( strcmp(zReply,"SQLITE_OK")==0 ){
352 rc = SQLITE_OK;
353 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
354 rc = SQLITE_DENY;
355 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
356 rc = SQLITE_IGNORE;
357 }else{
358 rc = 999;
359 }
360 return rc;
361}
362#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000363
364/*
danielk1977ef2cb632004-05-29 02:37:19 +0000365** zText is a pointer to text obtained via an sqlite3_result_text()
366** or similar interface. This routine returns a Tcl string object,
367** reference count set to 0, containing the text. If a translation
368** between iso8859 and UTF-8 is required, it is preformed.
369*/
370static Tcl_Obj *dbTextToObj(char const *zText){
371 Tcl_Obj *pVal;
372#ifdef UTF_TRANSLATION_NEEDED
373 Tcl_DString dCol;
374 Tcl_DStringInit(&dCol);
375 Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol);
376 pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
377 Tcl_DStringFree(&dCol);
378#else
379 pVal = Tcl_NewStringObj(zText, -1);
380#endif
381 return pVal;
382}
383
384/*
drh75897232000-05-29 14:26:00 +0000385** The "sqlite" command below creates a new Tcl command for each
386** connection it opens to an SQLite database. This routine is invoked
387** whenever one of those connection-specific commands is executed
388** in Tcl. For example, if you run Tcl code like this:
389**
390** sqlite db1 "my_database"
391** db1 close
392**
393** The first command opens a connection to the "my_database" database
394** and calls that connection "db1". The second command causes this
395** subroutine to be invoked.
396*/
drh6d313162000-09-21 13:01:35 +0000397static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000398 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000399 int choice;
drh22fbcb82004-02-01 01:22:50 +0000400 int rc = TCL_OK;
drh0de8c112002-07-06 16:32:14 +0000401 static const char *DB_strs[] = {
drh0f14e2e2004-06-29 12:39:08 +0000402 "authorizer", "busy", "changes",
403 "close", "collate", "collation_needed",
404 "commit_hook", "complete", "errorcode",
405 "eval", "function", "last_insert_rowid",
406 "onecolumn", "progress", "rekey",
407 "timeout", "total_changes", "trace",
408 0
drh6d313162000-09-21 13:01:35 +0000409 };
drh411995d2002-06-25 19:31:18 +0000410 enum DB_enum {
drh0f14e2e2004-06-29 12:39:08 +0000411 DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
412 DB_CLOSE, DB_COLLATE, DB_COLLATION_NEEDED,
413 DB_COMMIT_HOOK, DB_COMPLETE, DB_ERRORCODE,
414 DB_EVAL, DB_FUNCTION, DB_LAST_INSERT_ROWID,
415 DB_ONECOLUMN, DB_PROGRESS, DB_REKEY,
416 DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE,
drh6d313162000-09-21 13:01:35 +0000417 };
418
419 if( objc<2 ){
420 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000421 return TCL_ERROR;
422 }
drh411995d2002-06-25 19:31:18 +0000423 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000424 return TCL_ERROR;
425 }
426
drh411995d2002-06-25 19:31:18 +0000427 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000428
drhe22a3342003-04-22 20:30:37 +0000429 /* $db authorizer ?CALLBACK?
430 **
431 ** Invoke the given callback to authorize each SQL operation as it is
432 ** compiled. 5 arguments are appended to the callback before it is
433 ** invoked:
434 **
435 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
436 ** (2) First descriptive name (depends on authorization type)
437 ** (3) Second descriptive name
438 ** (4) Name of the database (ex: "main", "temp")
439 ** (5) Name of trigger that is doing the access
440 **
441 ** The callback should return on of the following strings: SQLITE_OK,
442 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
443 **
444 ** If this method is invoked with no arguments, the current authorization
445 ** callback string is returned.
446 */
447 case DB_AUTHORIZER: {
drh1211de32004-07-26 12:24:22 +0000448#ifdef SQLITE_OMIT_AUTHORIZATION
449 Tcl_AppendResult(interp, "authorization not available in this build", 0);
450 return TCL_ERROR;
451#else
drhe22a3342003-04-22 20:30:37 +0000452 if( objc>3 ){
453 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +0000454 return TCL_ERROR;
drhe22a3342003-04-22 20:30:37 +0000455 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000456 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000457 Tcl_AppendResult(interp, pDb->zAuth, 0);
458 }
459 }else{
460 char *zAuth;
461 int len;
462 if( pDb->zAuth ){
463 Tcl_Free(pDb->zAuth);
464 }
465 zAuth = Tcl_GetStringFromObj(objv[2], &len);
466 if( zAuth && len>0 ){
467 pDb->zAuth = Tcl_Alloc( len + 1 );
468 strcpy(pDb->zAuth, zAuth);
469 }else{
470 pDb->zAuth = 0;
471 }
drhe22a3342003-04-22 20:30:37 +0000472 if( pDb->zAuth ){
473 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000474 sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
drhe22a3342003-04-22 20:30:37 +0000475 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000476 sqlite3_set_authorizer(pDb->db, 0, 0);
drhe22a3342003-04-22 20:30:37 +0000477 }
drhe22a3342003-04-22 20:30:37 +0000478 }
drh1211de32004-07-26 12:24:22 +0000479#endif
drhe22a3342003-04-22 20:30:37 +0000480 break;
481 }
482
drhbec3f402000-08-04 13:49:02 +0000483 /* $db busy ?CALLBACK?
484 **
485 ** Invoke the given callback if an SQL statement attempts to open
486 ** a locked database file.
487 */
drh6d313162000-09-21 13:01:35 +0000488 case DB_BUSY: {
489 if( objc>3 ){
490 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000491 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000492 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000493 if( pDb->zBusy ){
494 Tcl_AppendResult(interp, pDb->zBusy, 0);
495 }
496 }else{
drh6d313162000-09-21 13:01:35 +0000497 char *zBusy;
498 int len;
drhbec3f402000-08-04 13:49:02 +0000499 if( pDb->zBusy ){
500 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000501 }
drh6d313162000-09-21 13:01:35 +0000502 zBusy = Tcl_GetStringFromObj(objv[2], &len);
503 if( zBusy && len>0 ){
504 pDb->zBusy = Tcl_Alloc( len + 1 );
505 strcpy(pDb->zBusy, zBusy);
506 }else{
507 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000508 }
509 if( pDb->zBusy ){
510 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000511 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000512 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000513 sqlite3_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000514 }
515 }
drh6d313162000-09-21 13:01:35 +0000516 break;
517 }
drhbec3f402000-08-04 13:49:02 +0000518
danielk1977348bb5d2003-10-18 09:37:26 +0000519 /* $db progress ?N CALLBACK?
520 **
521 ** Invoke the given callback every N virtual machine opcodes while executing
522 ** queries.
523 */
524 case DB_PROGRESS: {
525 if( objc==2 ){
526 if( pDb->zProgress ){
527 Tcl_AppendResult(interp, pDb->zProgress, 0);
528 }
529 }else if( objc==4 ){
530 char *zProgress;
531 int len;
532 int N;
533 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
534 return TCL_ERROR;
535 };
536 if( pDb->zProgress ){
537 Tcl_Free(pDb->zProgress);
538 }
539 zProgress = Tcl_GetStringFromObj(objv[3], &len);
540 if( zProgress && len>0 ){
541 pDb->zProgress = Tcl_Alloc( len + 1 );
542 strcpy(pDb->zProgress, zProgress);
543 }else{
544 pDb->zProgress = 0;
545 }
546#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
547 if( pDb->zProgress ){
548 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000549 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
danielk1977348bb5d2003-10-18 09:37:26 +0000550 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000551 sqlite3_progress_handler(pDb->db, 0, 0, 0);
danielk1977348bb5d2003-10-18 09:37:26 +0000552 }
553#endif
554 }else{
555 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
556 return TCL_ERROR;
557 }
558 break;
559 }
560
danielk1977b28af712004-06-21 06:50:26 +0000561 /* $db changes
drhc8d30ac2002-04-12 10:08:59 +0000562 **
563 ** Return the number of rows that were modified, inserted, or deleted by
danielk1977b28af712004-06-21 06:50:26 +0000564 ** the most recent INSERT, UPDATE or DELETE statement, not including
565 ** any changes made by trigger programs.
drhc8d30ac2002-04-12 10:08:59 +0000566 */
567 case DB_CHANGES: {
568 Tcl_Obj *pResult;
drhc8d30ac2002-04-12 10:08:59 +0000569 if( objc!=2 ){
570 Tcl_WrongNumArgs(interp, 2, objv, "");
571 return TCL_ERROR;
572 }
drhc8d30ac2002-04-12 10:08:59 +0000573 pResult = Tcl_GetObjResult(interp);
danielk1977b28af712004-06-21 06:50:26 +0000574 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db));
rdcf146a772004-02-25 22:51:06 +0000575 break;
576 }
577
drh75897232000-05-29 14:26:00 +0000578 /* $db close
579 **
580 ** Shutdown the database
581 */
drh6d313162000-09-21 13:01:35 +0000582 case DB_CLOSE: {
583 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
584 break;
585 }
drh75897232000-05-29 14:26:00 +0000586
drhaa940ea2004-01-15 02:44:03 +0000587 /* $db commit_hook ?CALLBACK?
588 **
589 ** Invoke the given callback just before committing every SQL transaction.
590 ** If the callback throws an exception or returns non-zero, then the
591 ** transaction is aborted. If CALLBACK is an empty string, the callback
592 ** is disabled.
593 */
594 case DB_COMMIT_HOOK: {
595 if( objc>3 ){
596 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +0000597 return TCL_ERROR;
drhaa940ea2004-01-15 02:44:03 +0000598 }else if( objc==2 ){
599 if( pDb->zCommit ){
600 Tcl_AppendResult(interp, pDb->zCommit, 0);
601 }
602 }else{
603 char *zCommit;
604 int len;
605 if( pDb->zCommit ){
606 Tcl_Free(pDb->zCommit);
607 }
608 zCommit = Tcl_GetStringFromObj(objv[2], &len);
609 if( zCommit && len>0 ){
610 pDb->zCommit = Tcl_Alloc( len + 1 );
611 strcpy(pDb->zCommit, zCommit);
612 }else{
613 pDb->zCommit = 0;
614 }
615 if( pDb->zCommit ){
616 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000617 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
drhaa940ea2004-01-15 02:44:03 +0000618 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000619 sqlite3_commit_hook(pDb->db, 0, 0);
drhaa940ea2004-01-15 02:44:03 +0000620 }
621 }
622 break;
623 }
624
drh0f14e2e2004-06-29 12:39:08 +0000625 /*
626 ** $db collate NAME SCRIPT
627 **
628 ** Create a new SQL collation function called NAME. Whenever
629 ** that function is called, invoke SCRIPT to evaluate the function.
630 */
631 case DB_COLLATE: {
632 SqlCollate *pCollate;
633 char *zName;
634 char *zScript;
635 int nScript;
636 if( objc!=4 ){
637 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
638 return TCL_ERROR;
639 }
640 zName = Tcl_GetStringFromObj(objv[2], 0);
641 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
642 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
643 if( pCollate==0 ) return TCL_ERROR;
644 pCollate->interp = interp;
645 pCollate->pNext = pDb->pCollate;
646 pCollate->zScript = (char*)&pCollate[1];
647 pDb->pCollate = pCollate;
648 strcpy(pCollate->zScript, zScript);
649 if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
650 pCollate, tclSqlCollate) ){
651 return TCL_ERROR;
652 }
653 break;
654 }
655
656 /*
657 ** $db collation_needed SCRIPT
658 **
659 ** Create a new SQL collation function called NAME. Whenever
660 ** that function is called, invoke SCRIPT to evaluate the function.
661 */
662 case DB_COLLATION_NEEDED: {
663 if( objc!=3 ){
664 Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT");
665 return TCL_ERROR;
666 }
667 if( pDb->pCollateNeeded ){
668 Tcl_DecrRefCount(pDb->pCollateNeeded);
669 }
670 pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]);
671 Tcl_IncrRefCount(pDb->pCollateNeeded);
672 sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded);
673 break;
674 }
675
drh75897232000-05-29 14:26:00 +0000676 /* $db complete SQL
677 **
678 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
679 ** additional lines of input are needed. This is similar to the
680 ** built-in "info complete" command of Tcl.
681 */
drh6d313162000-09-21 13:01:35 +0000682 case DB_COMPLETE: {
683 Tcl_Obj *pResult;
684 int isComplete;
685 if( objc!=3 ){
686 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000687 return TCL_ERROR;
688 }
danielk19776f8a5032004-05-10 10:34:51 +0000689 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
drh6d313162000-09-21 13:01:35 +0000690 pResult = Tcl_GetObjResult(interp);
691 Tcl_SetBooleanObj(pResult, isComplete);
692 break;
693 }
drhdcd997e2003-01-31 17:21:49 +0000694
695 /*
696 ** $db errorcode
697 **
698 ** Return the numeric error code that was returned by the most recent
danielk19776f8a5032004-05-10 10:34:51 +0000699 ** call to sqlite3_exec().
drhdcd997e2003-01-31 17:21:49 +0000700 */
701 case DB_ERRORCODE: {
danielk1977f3ce83f2004-06-14 11:43:46 +0000702 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
drhdcd997e2003-01-31 17:21:49 +0000703 break;
704 }
drh75897232000-05-29 14:26:00 +0000705
706 /*
drh895d7472004-08-20 16:02:39 +0000707 ** $db eval $sql ?array? ?{ ...code... }?
drh75897232000-05-29 14:26:00 +0000708 **
709 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000710 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000711 ** If "array" and "code" are omitted, then no callback is every invoked.
712 ** If "array" is an empty string, then the values are placed in variables
713 ** that have the same name as the fields extracted by the query.
714 */
drh6d313162000-09-21 13:01:35 +0000715 case DB_EVAL: {
danielk197730ccda12004-05-27 12:11:31 +0000716 char const *zSql;
717 char const *zLeft;
718 sqlite3_stmt *pStmt;
danielk1977ef2cb632004-05-29 02:37:19 +0000719
720 Tcl_Obj *pRet = Tcl_NewObj();
721 Tcl_IncrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000722
drh895d7472004-08-20 16:02:39 +0000723 if( objc<3 || objc>5 || objc==4 ){
724 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?");
danielk197730ccda12004-05-27 12:11:31 +0000725 return TCL_ERROR;
726 }
727
danielk197730ccda12004-05-27 12:11:31 +0000728 zSql = Tcl_GetStringFromObj(objv[2], 0);
729 while( zSql[0] ){
730 int i;
731
732 if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000733 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000734 rc = TCL_ERROR;
735 break;
736 }
737
738 if( pStmt && objc==5 ){
739 Tcl_Obj *pColList = Tcl_NewObj();
740 Tcl_IncrRefCount(pColList);
741
742 for(i=0; i<sqlite3_column_count(pStmt); i++){
743 Tcl_ListObjAppendElement(interp, pColList,
danielk1977ef2cb632004-05-29 02:37:19 +0000744 dbTextToObj(sqlite3_column_name(pStmt, i))
danielk197730ccda12004-05-27 12:11:31 +0000745 );
746 }
747 Tcl_ObjSetVar2(interp,objv[3],Tcl_NewStringObj("*",-1),pColList,0);
748 }
749
750 while( pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
751 for(i=0; i<sqlite3_column_count(pStmt); i++){
752 Tcl_Obj *pVal;
753
754 /* Set pVal to contain the i'th column of this row. */
drh9c054832004-05-31 18:51:57 +0000755 if( SQLITE_BLOB!=sqlite3_column_type(pStmt, i) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000756 pVal = dbTextToObj(sqlite3_column_text(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000757 }else{
danielk19773fd0a732004-05-27 13:35:19 +0000758 int bytes = sqlite3_column_bytes(pStmt, i);
759 pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes);
danielk197730ccda12004-05-27 12:11:31 +0000760 }
761
762 if( objc==5 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000763 Tcl_Obj *pName = dbTextToObj(sqlite3_column_name(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000764 Tcl_IncrRefCount(pName);
765 if( !strcmp("", Tcl_GetString(objv[3])) ){
766 Tcl_ObjSetVar2(interp, pName, 0, pVal, 0);
767 }else{
768 Tcl_ObjSetVar2(interp, objv[3], pName, pVal, 0);
769 }
770 Tcl_DecrRefCount(pName);
771 }else{
danielk197730ccda12004-05-27 12:11:31 +0000772 Tcl_ListObjAppendElement(interp, pRet, pVal);
773 }
774 }
775
776 if( objc==5 ){
777 rc = Tcl_EvalObjEx(interp, objv[4], 0);
778 if( rc!=TCL_ERROR ) rc = TCL_OK;
779 }
780 }
781
782 if( pStmt && SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){
783 continue;
784 }
785
786 if( pStmt && SQLITE_OK!=sqlite3_errcode(pDb->db) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000787 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000788 rc = TCL_ERROR;
789 break;
790 }
791
danielk197730ccda12004-05-27 12:11:31 +0000792 zSql = zLeft;
793 }
794
danielk1977ef2cb632004-05-29 02:37:19 +0000795 if( rc==TCL_OK ){
danielk197730ccda12004-05-27 12:11:31 +0000796 Tcl_SetObjResult(interp, pRet);
danielk197730ccda12004-05-27 12:11:31 +0000797 }
danielk1977ef2cb632004-05-29 02:37:19 +0000798 Tcl_DecrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000799
800 break;
801 }
drhbec3f402000-08-04 13:49:02 +0000802
803 /*
drhcabb0812002-09-14 13:47:32 +0000804 ** $db function NAME SCRIPT
805 **
806 ** Create a new SQL function called NAME. Whenever that function is
807 ** called, invoke SCRIPT to evaluate the function.
808 */
809 case DB_FUNCTION: {
810 SqlFunc *pFunc;
811 char *zName;
812 char *zScript;
813 int nScript;
814 if( objc!=4 ){
815 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
816 return TCL_ERROR;
817 }
818 zName = Tcl_GetStringFromObj(objv[2], 0);
819 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
820 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
821 if( pFunc==0 ) return TCL_ERROR;
822 pFunc->interp = interp;
823 pFunc->pNext = pDb->pFunc;
824 pFunc->zScript = (char*)&pFunc[1];
drh0f14e2e2004-06-29 12:39:08 +0000825 pDb->pFunc = pFunc;
drhcabb0812002-09-14 13:47:32 +0000826 strcpy(pFunc->zScript, zScript);
danielk1977c8c11582004-06-29 13:41:21 +0000827 rc = sqlite3_create_function(pDb->db, zName, -1, SQLITE_UTF8,
danielk1977d8123362004-06-12 09:25:12 +0000828 pFunc, tclSqlFunc, 0, 0);
danielk1977c8c11582004-06-29 13:41:21 +0000829 if( rc!=SQLITE_OK ) rc = TCL_ERROR;
drhcabb0812002-09-14 13:47:32 +0000830 break;
831 }
832
833 /*
drhaf9ff332002-01-16 21:00:27 +0000834 ** $db last_insert_rowid
835 **
836 ** Return an integer which is the ROWID for the most recent insert.
837 */
838 case DB_LAST_INSERT_ROWID: {
839 Tcl_Obj *pResult;
840 int rowid;
841 if( objc!=2 ){
842 Tcl_WrongNumArgs(interp, 2, objv, "");
843 return TCL_ERROR;
844 }
danielk19776f8a5032004-05-10 10:34:51 +0000845 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +0000846 pResult = Tcl_GetObjResult(interp);
847 Tcl_SetIntObj(pResult, rowid);
848 break;
849 }
850
851 /*
drh5d9d7572003-08-19 14:31:01 +0000852 ** $db onecolumn SQL
853 **
854 ** Return a single column from a single row of the given SQL query.
855 */
856 case DB_ONECOLUMN: {
drh5d9d7572003-08-19 14:31:01 +0000857 char *zSql;
858 char *zErrMsg = 0;
859 if( objc!=3 ){
860 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
861 return TCL_ERROR;
862 }
863 zSql = Tcl_GetStringFromObj(objv[2], 0);
danielk19776f8a5032004-05-10 10:34:51 +0000864 rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
drh5d9d7572003-08-19 14:31:01 +0000865 if( rc==SQLITE_ABORT ){
drh22fbcb82004-02-01 01:22:50 +0000866 rc = SQLITE_OK;
drh5d9d7572003-08-19 14:31:01 +0000867 }else if( zErrMsg ){
868 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
869 free(zErrMsg);
870 rc = TCL_ERROR;
871 }else if( rc!=SQLITE_OK ){
danielk1977f20b21c2004-05-31 23:56:42 +0000872 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh5d9d7572003-08-19 14:31:01 +0000873 rc = TCL_ERROR;
874 }
875 break;
876 }
877
878 /*
drh22fbcb82004-02-01 01:22:50 +0000879 ** $db rekey KEY
880 **
881 ** Change the encryption key on the currently open database.
882 */
883 case DB_REKEY: {
884 int nKey;
885 void *pKey;
886 if( objc!=3 ){
887 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
888 return TCL_ERROR;
889 }
890 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +0000891#ifdef SQLITE_HAS_CODEC
drh2011d5f2004-07-22 02:40:37 +0000892 rc = sqlite3_rekey(pDb->db, pKey, nKey);
drh22fbcb82004-02-01 01:22:50 +0000893 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000894 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh22fbcb82004-02-01 01:22:50 +0000895 rc = TCL_ERROR;
896 }
897#endif
898 break;
899 }
900
901 /*
drhbec3f402000-08-04 13:49:02 +0000902 ** $db timeout MILLESECONDS
903 **
904 ** Delay for the number of milliseconds specified when a file is locked.
905 */
drh6d313162000-09-21 13:01:35 +0000906 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000907 int ms;
drh6d313162000-09-21 13:01:35 +0000908 if( objc!=3 ){
909 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000910 return TCL_ERROR;
911 }
drh6d313162000-09-21 13:01:35 +0000912 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +0000913 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000914 break;
drh75897232000-05-29 14:26:00 +0000915 }
drhb5a20d32003-04-23 12:25:23 +0000916
drh0f14e2e2004-06-29 12:39:08 +0000917 /*
918 ** $db total_changes
919 **
920 ** Return the number of rows that were modified, inserted, or deleted
921 ** since the database handle was created.
922 */
923 case DB_TOTAL_CHANGES: {
924 Tcl_Obj *pResult;
925 if( objc!=2 ){
926 Tcl_WrongNumArgs(interp, 2, objv, "");
927 return TCL_ERROR;
928 }
929 pResult = Tcl_GetObjResult(interp);
930 Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db));
931 break;
932 }
933
drhb5a20d32003-04-23 12:25:23 +0000934 /* $db trace ?CALLBACK?
935 **
936 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
937 ** that is executed. The text of the SQL is appended to CALLBACK before
938 ** it is executed.
939 */
940 case DB_TRACE: {
941 if( objc>3 ){
942 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drhb97759e2004-06-29 11:26:59 +0000943 return TCL_ERROR;
drhb5a20d32003-04-23 12:25:23 +0000944 }else if( objc==2 ){
945 if( pDb->zTrace ){
946 Tcl_AppendResult(interp, pDb->zTrace, 0);
947 }
948 }else{
949 char *zTrace;
950 int len;
951 if( pDb->zTrace ){
952 Tcl_Free(pDb->zTrace);
953 }
954 zTrace = Tcl_GetStringFromObj(objv[2], &len);
955 if( zTrace && len>0 ){
956 pDb->zTrace = Tcl_Alloc( len + 1 );
957 strcpy(pDb->zTrace, zTrace);
958 }else{
959 pDb->zTrace = 0;
960 }
961 if( pDb->zTrace ){
962 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000963 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +0000964 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000965 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +0000966 }
967 }
968 break;
969 }
970
drh6d313162000-09-21 13:01:35 +0000971 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +0000972 return rc;
drh75897232000-05-29 14:26:00 +0000973}
974
975/*
drh22fbcb82004-02-01 01:22:50 +0000976** sqlite DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +0000977**
978** This is the main Tcl command. When the "sqlite" Tcl command is
979** invoked, this routine runs to process that command.
980**
981** The first argument, DBNAME, is an arbitrary name for a new
982** database connection. This command creates a new command named
983** DBNAME that is used to control that connection. The database
984** connection is deleted when the DBNAME command is deleted.
985**
986** The second argument is the name of the directory that contains
987** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000988**
989** For testing purposes, we also support the following:
990**
991** sqlite -encoding
992**
993** Return the encoding used by LIKE and GLOB operators. Choices
994** are UTF-8 and iso8859.
995**
drh647cb0e2002-11-04 19:32:25 +0000996** sqlite -version
997**
998** Return the version number of the SQLite library.
999**
drhfbc3eab2001-04-06 16:13:42 +00001000** sqlite -tcl-uses-utf
1001**
1002** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
1003** not. Used by tests to make sure the library was compiled
1004** correctly.
drh75897232000-05-29 14:26:00 +00001005*/
drh22fbcb82004-02-01 01:22:50 +00001006static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +00001007 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +00001008 void *pKey = 0;
1009 int nKey = 0;
1010 const char *zArg;
drh75897232000-05-29 14:26:00 +00001011 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +00001012 const char *zFile;
drh06b27182002-06-26 20:06:05 +00001013 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +00001014 if( objc==2 ){
1015 zArg = Tcl_GetStringFromObj(objv[1], 0);
drh22fbcb82004-02-01 01:22:50 +00001016 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001017 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +00001018 return TCL_OK;
1019 }
drh9eb9e262004-02-11 02:18:05 +00001020 if( strcmp(zArg,"-has-codec")==0 ){
1021#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001022 Tcl_AppendResult(interp,"1",0);
1023#else
1024 Tcl_AppendResult(interp,"0",0);
1025#endif
1026 return TCL_OK;
1027 }
1028 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +00001029#ifdef TCL_UTF_MAX
1030 Tcl_AppendResult(interp,"1",0);
1031#else
1032 Tcl_AppendResult(interp,"0",0);
1033#endif
1034 return TCL_OK;
1035 }
1036 }
drh22fbcb82004-02-01 01:22:50 +00001037 if( objc==5 || objc==6 ){
1038 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
1039 if( strcmp(zArg,"-key")==0 ){
1040 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
1041 objc -= 2;
1042 }
1043 }
1044 if( objc!=3 && objc!=4 ){
1045 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +00001046#ifdef SQLITE_HAS_CODEC
1047 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +00001048#else
1049 "HANDLE FILENAME ?MODE?"
1050#endif
1051 );
drh75897232000-05-29 14:26:00 +00001052 return TCL_ERROR;
1053 }
drh75897232000-05-29 14:26:00 +00001054 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001055 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001056 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001057 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1058 return TCL_ERROR;
1059 }
1060 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00001061 zFile = Tcl_GetStringFromObj(objv[2], 0);
danielk19774f057f92004-06-08 00:02:33 +00001062 sqlite3_open(zFile, &p->db);
danielk197780290862004-05-22 09:21:21 +00001063 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
1064 zErrMsg = strdup(sqlite3_errmsg(p->db));
1065 sqlite3_close(p->db);
1066 p->db = 0;
1067 }
drh2011d5f2004-07-22 02:40:37 +00001068#ifdef SQLITE_HAS_CODEC
1069 sqlite3_key(p->db, pKey, nKey);
drheb8ed702004-02-11 10:37:23 +00001070#endif
drhbec3f402000-08-04 13:49:02 +00001071 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001072 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001073 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001074 free(zErrMsg);
1075 return TCL_ERROR;
1076 }
drh22fbcb82004-02-01 01:22:50 +00001077 zArg = Tcl_GetStringFromObj(objv[1], 0);
1078 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001079
drh06b27182002-06-26 20:06:05 +00001080 /* The return value is the value of the sqlite* pointer
1081 */
1082 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001083 if( strncmp(zBuf,"0x",2) ){
1084 sprintf(zBuf, "0x%p", p->db);
1085 }
drh06b27182002-06-26 20:06:05 +00001086 Tcl_AppendResult(interp, zBuf, 0);
1087
drhc22bd472002-05-10 13:14:07 +00001088 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001089 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001090 */
drh28b4e482002-03-11 02:06:13 +00001091#ifdef SQLITE_TEST
1092 {
drhc22bd472002-05-10 13:14:07 +00001093 extern void Md5_Register(sqlite*);
danielk19775b59af82004-06-30 12:42:59 +00001094#ifdef SQLITE_DEBUG
danielk197713073932004-06-30 11:54:06 +00001095 int mallocfail = sqlite3_iMallocFail;
1096 sqlite3_iMallocFail = 0;
danielk19775b59af82004-06-30 12:42:59 +00001097#endif
drhc22bd472002-05-10 13:14:07 +00001098 Md5_Register(p->db);
danielk19775b59af82004-06-30 12:42:59 +00001099#ifdef SQLITE_DEBUG
danielk197713073932004-06-30 11:54:06 +00001100 sqlite3_iMallocFail = mallocfail;
danielk19775b59af82004-06-30 12:42:59 +00001101#endif
drh06b27182002-06-26 20:06:05 +00001102 }
drh28b4e482002-03-11 02:06:13 +00001103#endif
danielk19777cedc8d2004-06-10 10:50:08 +00001104 p->interp = interp;
drh75897232000-05-29 14:26:00 +00001105 return TCL_OK;
1106}
1107
1108/*
drh90ca9752001-09-28 17:47:14 +00001109** Provide a dummy Tcl_InitStubs if we are using this as a static
1110** library.
1111*/
1112#ifndef USE_TCL_STUBS
1113# undef Tcl_InitStubs
1114# define Tcl_InitStubs(a,b,c)
1115#endif
1116
1117/*
drh75897232000-05-29 14:26:00 +00001118** Initialize this module.
1119**
1120** This Tcl module contains only a single new Tcl command named "sqlite".
1121** (Hence there is no namespace. There is no point in using a namespace
1122** if the extension only supplies one new name!) The "sqlite" command is
1123** used to open a new SQLite database. See the DbMain() routine above
1124** for additional information.
1125*/
drh38f82712004-06-18 17:10:16 +00001126int Sqlite3_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001127 Tcl_InitStubs(interp, "8.0", 0);
drhef4ac8f2004-06-19 00:16:31 +00001128 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1129 Tcl_PkgProvide(interp, "sqlite3", "3.0");
drh90ca9752001-09-28 17:47:14 +00001130 return TCL_OK;
1131}
drh38f82712004-06-18 17:10:16 +00001132int Tclsqlite3_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001133 Tcl_InitStubs(interp, "8.0", 0);
drhef4ac8f2004-06-19 00:16:31 +00001134 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1135 Tcl_PkgProvide(interp, "sqlite3", "3.0");
drh75897232000-05-29 14:26:00 +00001136 return TCL_OK;
1137}
drh38f82712004-06-18 17:10:16 +00001138int Sqlite3_SafeInit(Tcl_Interp *interp){
drh75897232000-05-29 14:26:00 +00001139 return TCL_OK;
1140}
drh38f82712004-06-18 17:10:16 +00001141int Tclsqlite3_SafeInit(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001142 return TCL_OK;
1143}
drh75897232000-05-29 14:26:00 +00001144
drh3e27c022004-07-23 00:01:38 +00001145#ifdef TCLSH
1146/*****************************************************************************
1147** The code that follows is used to build standalone TCL interpreters
drh75897232000-05-29 14:26:00 +00001148*/
drh348784e2000-05-29 20:41:49 +00001149
1150/*
drh3e27c022004-07-23 00:01:38 +00001151** If the macro TCLSH is one, then put in code this for the
1152** "main" routine that will initialize Tcl and take input from
1153** standard input.
drh348784e2000-05-29 20:41:49 +00001154*/
drh3e27c022004-07-23 00:01:38 +00001155#if TCLSH==1
drh348784e2000-05-29 20:41:49 +00001156static char zMainloop[] =
1157 "set line {}\n"
1158 "while {![eof stdin]} {\n"
1159 "if {$line!=\"\"} {\n"
1160 "puts -nonewline \"> \"\n"
1161 "} else {\n"
1162 "puts -nonewline \"% \"\n"
1163 "}\n"
1164 "flush stdout\n"
1165 "append line [gets stdin]\n"
1166 "if {[info complete $line]} {\n"
1167 "if {[catch {uplevel #0 $line} result]} {\n"
1168 "puts stderr \"Error: $result\"\n"
1169 "} elseif {$result!=\"\"} {\n"
1170 "puts $result\n"
1171 "}\n"
1172 "set line {}\n"
1173 "} else {\n"
1174 "append line \\n\n"
1175 "}\n"
1176 "}\n"
1177;
drh3e27c022004-07-23 00:01:38 +00001178#endif
1179
1180/*
1181** If the macro TCLSH is two, then get the main loop code out of
1182** the separate file "spaceanal_tcl.h".
1183*/
1184#if TCLSH==2
1185static char zMainloop[] =
1186#include "spaceanal_tcl.h"
1187;
1188#endif
drh348784e2000-05-29 20:41:49 +00001189
1190#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1191int TCLSH_MAIN(int argc, char **argv){
1192 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001193 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001194 interp = Tcl_CreateInterp();
drh38f82712004-06-18 17:10:16 +00001195 Sqlite3_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001196#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001197 {
1198 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001199 extern int Sqlitetest2_Init(Tcl_Interp*);
1200 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001201 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001202 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001203 extern int Md5_Init(Tcl_Interp*);
danielk19776490beb2004-05-11 06:17:21 +00001204 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001205 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001206 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00001207 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00001208 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001209 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001210 }
1211#endif
drh3e27c022004-07-23 00:01:38 +00001212 if( argc>=2 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00001213 int i;
1214 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1215 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1216 for(i=2; i<argc; i++){
1217 Tcl_SetVar(interp, "argv", argv[i],
1218 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1219 }
drh3e27c022004-07-23 00:01:38 +00001220 if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001221 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001222 if( zInfo==0 ) zInfo = interp->result;
1223 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001224 return 1;
1225 }
drh3e27c022004-07-23 00:01:38 +00001226 }
1227 if( argc<=1 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00001228 Tcl_GlobalEval(interp, zMainloop);
1229 }
1230 return 0;
1231}
1232#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001233
1234#endif /* !defined(NO_TCL) */