blob: 9fc5d6cef41f8cfedda517597cdb36819bb9b603 [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**
drh92febd92004-08-20 18:34:20 +000014** $Id: tclsqlite.c,v 1.100 2004/08/20 18:34:20 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;
62struct SqliteDb {
drh895d7472004-08-20 16:02:39 +000063 sqlite3 *db; /* The "real" database structure */
drhbec3f402000-08-04 13:49:02 +000064 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000065 char *zBusy; /* The busy callback routine */
drhaa940ea2004-01-15 02:44:03 +000066 char *zCommit; /* The commit hook callback routine */
drhb5a20d32003-04-23 12:25:23 +000067 char *zTrace; /* The trace callback routine */
danielk1977348bb5d2003-10-18 09:37:26 +000068 char *zProgress; /* The progress callback routine */
drhe22a3342003-04-22 20:30:37 +000069 char *zAuth; /* The authorization callback routine */
drhcabb0812002-09-14 13:47:32 +000070 SqlFunc *pFunc; /* List of SQL functions */
danielk19770202b292004-06-09 09:55:16 +000071 SqlCollate *pCollate; /* List of SQL collation functions */
danielk19776f8a5032004-05-10 10:34:51 +000072 int rc; /* Return code of most recent sqlite3_exec() */
danielk19777cedc8d2004-06-10 10:50:08 +000073 Tcl_Obj *pCollateNeeded; /* Collation needed script */
drh98808ba2001-10-18 12:34:46 +000074};
drh297ecf12001-04-05 15:57:13 +000075
drh6d313162000-09-21 13:01:35 +000076/*
drh5d9d7572003-08-19 14:31:01 +000077** This is a second alternative callback for database queries. A the
78** first column of the first row of the result is made the TCL result.
79*/
80static int DbEvalCallback3(
81 void *clientData, /* An instance of CallbackData */
82 int nCol, /* Number of columns in the result */
83 char ** azCol, /* Data for each column */
84 char ** azN /* Name for each column */
85){
86 Tcl_Interp *interp = (Tcl_Interp*)clientData;
87 Tcl_Obj *pElem;
88 if( azCol==0 ) return 1;
89 if( nCol==0 ) return 1;
90#ifdef UTF_TRANSLATION_NEEDED
91 {
92 Tcl_DString dCol;
93 Tcl_DStringInit(&dCol);
94 Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol);
95 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
96 Tcl_DStringFree(&dCol);
97 }
98#else
99 pElem = Tcl_NewStringObj(azCol[0], -1);
100#endif
101 Tcl_SetObjResult(interp, pElem);
102 return 1;
103}
104
105/*
drh895d7472004-08-20 16:02:39 +0000106** TCL calls this procedure when an sqlite3 database command is
107** deleted.
drh75897232000-05-29 14:26:00 +0000108*/
109static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000110 SqliteDb *pDb = (SqliteDb*)db;
danielk19776f8a5032004-05-10 10:34:51 +0000111 sqlite3_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000112 while( pDb->pFunc ){
113 SqlFunc *pFunc = pDb->pFunc;
114 pDb->pFunc = pFunc->pNext;
115 Tcl_Free((char*)pFunc);
116 }
danielk19770202b292004-06-09 09:55:16 +0000117 while( pDb->pCollate ){
118 SqlCollate *pCollate = pDb->pCollate;
119 pDb->pCollate = pCollate->pNext;
120 Tcl_Free((char*)pCollate);
121 }
drhbec3f402000-08-04 13:49:02 +0000122 if( pDb->zBusy ){
123 Tcl_Free(pDb->zBusy);
124 }
drhb5a20d32003-04-23 12:25:23 +0000125 if( pDb->zTrace ){
126 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000127 }
drhe22a3342003-04-22 20:30:37 +0000128 if( pDb->zAuth ){
129 Tcl_Free(pDb->zAuth);
130 }
drhbec3f402000-08-04 13:49:02 +0000131 Tcl_Free((char*)pDb);
132}
133
134/*
135** This routine is called when a database file is locked while trying
136** to execute SQL.
137*/
danielk19772a764eb2004-06-12 01:43:26 +0000138static int DbBusyHandler(void *cd, int nTries){
drhbec3f402000-08-04 13:49:02 +0000139 SqliteDb *pDb = (SqliteDb*)cd;
140 int rc;
141 char zVal[30];
142 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000143 Tcl_DString cmd;
144
145 Tcl_DStringInit(&cmd);
146 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
danielk19772a764eb2004-06-12 01:43:26 +0000147 sprintf(zVal, "%d", nTries);
148 Tcl_DStringAppendElement(&cmd, zVal);
drhbec3f402000-08-04 13:49:02 +0000149 zCmd = Tcl_DStringValue(&cmd);
150 rc = Tcl_Eval(pDb->interp, zCmd);
151 Tcl_DStringFree(&cmd);
152 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
153 return 0;
154 }
155 return 1;
drh75897232000-05-29 14:26:00 +0000156}
157
158/*
danielk1977348bb5d2003-10-18 09:37:26 +0000159** This routine is invoked as the 'progress callback' for the database.
160*/
161static int DbProgressHandler(void *cd){
162 SqliteDb *pDb = (SqliteDb*)cd;
163 int rc;
164
165 assert( pDb->zProgress );
166 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
167 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
168 return 1;
169 }
170 return 0;
171}
172
173/*
drhb5a20d32003-04-23 12:25:23 +0000174** This routine is called by the SQLite trace handler whenever a new
175** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000176*/
drhb5a20d32003-04-23 12:25:23 +0000177static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000178 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000179 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000180
drhb5a20d32003-04-23 12:25:23 +0000181 Tcl_DStringInit(&str);
182 Tcl_DStringAppend(&str, pDb->zTrace, -1);
183 Tcl_DStringAppendElement(&str, zSql);
184 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
185 Tcl_DStringFree(&str);
186 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000187}
188
189/*
drhaa940ea2004-01-15 02:44:03 +0000190** This routine is called when a transaction is committed. The
191** TCL script in pDb->zCommit is executed. If it returns non-zero or
192** if it throws an exception, the transaction is rolled back instead
193** of being committed.
194*/
195static int DbCommitHandler(void *cd){
196 SqliteDb *pDb = (SqliteDb*)cd;
197 int rc;
198
199 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
200 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
201 return 1;
202 }
203 return 0;
204}
205
danielk19777cedc8d2004-06-10 10:50:08 +0000206static void tclCollateNeeded(
207 void *pCtx,
208 sqlite *db,
209 int enc,
210 const char *zName
211){
212 SqliteDb *pDb = (SqliteDb *)pCtx;
213 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
214 Tcl_IncrRefCount(pScript);
215 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
216 Tcl_EvalObjEx(pDb->interp, pScript, 0);
217 Tcl_DecrRefCount(pScript);
218}
219
drhaa940ea2004-01-15 02:44:03 +0000220/*
danielk19770202b292004-06-09 09:55:16 +0000221** This routine is called to evaluate an SQL collation function implemented
222** using TCL script.
223*/
224static int tclSqlCollate(
225 void *pCtx,
226 int nA,
227 const void *zA,
228 int nB,
229 const void *zB
230){
231 SqlCollate *p = (SqlCollate *)pCtx;
232 Tcl_Obj *pCmd;
233
234 pCmd = Tcl_NewStringObj(p->zScript, -1);
235 Tcl_IncrRefCount(pCmd);
236 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA));
237 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB));
238 Tcl_EvalObjEx(p->interp, pCmd, 0);
239 Tcl_DecrRefCount(pCmd);
240 return (atoi(Tcl_GetStringResult(p->interp)));
241}
242
243/*
drhcabb0812002-09-14 13:47:32 +0000244** This routine is called to evaluate an SQL function implemented
245** using TCL script.
246*/
danielk19770ae8b832004-05-25 12:05:56 +0000247static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk19776f8a5032004-05-10 10:34:51 +0000248 SqlFunc *p = sqlite3_user_data(context);
drhcabb0812002-09-14 13:47:32 +0000249 Tcl_DString cmd;
250 int i;
251 int rc;
252
253 Tcl_DStringInit(&cmd);
254 Tcl_DStringAppend(&cmd, p->zScript, -1);
255 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000256 if( SQLITE_NULL==sqlite3_value_type(argv[i]) ){
danielk197751ad0ec2004-05-24 12:39:02 +0000257 Tcl_DStringAppendElement(&cmd, "");
258 }else{
drh4f26d6c2004-05-26 23:25:30 +0000259 Tcl_DStringAppendElement(&cmd, sqlite3_value_text(argv[i]));
danielk197751ad0ec2004-05-24 12:39:02 +0000260 }
drhcabb0812002-09-14 13:47:32 +0000261 }
262 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
263 if( rc ){
danielk19777e18c252004-05-25 11:47:24 +0000264 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhcabb0812002-09-14 13:47:32 +0000265 }else{
danielk1977d8123362004-06-12 09:25:12 +0000266 sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1,
267 SQLITE_TRANSIENT);
drhcabb0812002-09-14 13:47:32 +0000268 }
269}
drh895d7472004-08-20 16:02:39 +0000270
drhe22a3342003-04-22 20:30:37 +0000271#ifndef SQLITE_OMIT_AUTHORIZATION
272/*
273** This is the authentication function. It appends the authentication
274** type code and the two arguments to zCmd[] then invokes the result
275** on the interpreter. The reply is examined to determine if the
276** authentication fails or succeeds.
277*/
278static int auth_callback(
279 void *pArg,
280 int code,
281 const char *zArg1,
282 const char *zArg2,
283 const char *zArg3,
284 const char *zArg4
285){
286 char *zCode;
287 Tcl_DString str;
288 int rc;
289 const char *zReply;
290 SqliteDb *pDb = (SqliteDb*)pArg;
291
292 switch( code ){
293 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
294 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
295 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
296 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
297 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
298 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
299 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
300 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
301 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
302 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
303 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
304 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
305 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
306 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
307 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
308 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
309 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
310 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
311 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
312 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
313 case SQLITE_READ : zCode="SQLITE_READ"; break;
314 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
315 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
316 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000317 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
318 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
drhe22a3342003-04-22 20:30:37 +0000319 default : zCode="????"; break;
320 }
321 Tcl_DStringInit(&str);
322 Tcl_DStringAppend(&str, pDb->zAuth, -1);
323 Tcl_DStringAppendElement(&str, zCode);
324 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
325 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
326 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
327 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
328 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
329 Tcl_DStringFree(&str);
330 zReply = Tcl_GetStringResult(pDb->interp);
331 if( strcmp(zReply,"SQLITE_OK")==0 ){
332 rc = SQLITE_OK;
333 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
334 rc = SQLITE_DENY;
335 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
336 rc = SQLITE_IGNORE;
337 }else{
338 rc = 999;
339 }
340 return rc;
341}
342#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000343
344/*
danielk1977ef2cb632004-05-29 02:37:19 +0000345** zText is a pointer to text obtained via an sqlite3_result_text()
346** or similar interface. This routine returns a Tcl string object,
347** reference count set to 0, containing the text. If a translation
348** between iso8859 and UTF-8 is required, it is preformed.
349*/
350static Tcl_Obj *dbTextToObj(char const *zText){
351 Tcl_Obj *pVal;
352#ifdef UTF_TRANSLATION_NEEDED
353 Tcl_DString dCol;
354 Tcl_DStringInit(&dCol);
355 Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol);
356 pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
357 Tcl_DStringFree(&dCol);
358#else
359 pVal = Tcl_NewStringObj(zText, -1);
360#endif
361 return pVal;
362}
363
364/*
drh75897232000-05-29 14:26:00 +0000365** The "sqlite" command below creates a new Tcl command for each
366** connection it opens to an SQLite database. This routine is invoked
367** whenever one of those connection-specific commands is executed
368** in Tcl. For example, if you run Tcl code like this:
369**
370** sqlite db1 "my_database"
371** db1 close
372**
373** The first command opens a connection to the "my_database" database
374** and calls that connection "db1". The second command causes this
375** subroutine to be invoked.
376*/
drh6d313162000-09-21 13:01:35 +0000377static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000378 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000379 int choice;
drh22fbcb82004-02-01 01:22:50 +0000380 int rc = TCL_OK;
drh0de8c112002-07-06 16:32:14 +0000381 static const char *DB_strs[] = {
drh0f14e2e2004-06-29 12:39:08 +0000382 "authorizer", "busy", "changes",
383 "close", "collate", "collation_needed",
384 "commit_hook", "complete", "errorcode",
385 "eval", "function", "last_insert_rowid",
386 "onecolumn", "progress", "rekey",
387 "timeout", "total_changes", "trace",
388 0
drh6d313162000-09-21 13:01:35 +0000389 };
drh411995d2002-06-25 19:31:18 +0000390 enum DB_enum {
drh0f14e2e2004-06-29 12:39:08 +0000391 DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
392 DB_CLOSE, DB_COLLATE, DB_COLLATION_NEEDED,
393 DB_COMMIT_HOOK, DB_COMPLETE, DB_ERRORCODE,
394 DB_EVAL, DB_FUNCTION, DB_LAST_INSERT_ROWID,
395 DB_ONECOLUMN, DB_PROGRESS, DB_REKEY,
396 DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE,
drh6d313162000-09-21 13:01:35 +0000397 };
398
399 if( objc<2 ){
400 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000401 return TCL_ERROR;
402 }
drh411995d2002-06-25 19:31:18 +0000403 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000404 return TCL_ERROR;
405 }
406
drh411995d2002-06-25 19:31:18 +0000407 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000408
drhe22a3342003-04-22 20:30:37 +0000409 /* $db authorizer ?CALLBACK?
410 **
411 ** Invoke the given callback to authorize each SQL operation as it is
412 ** compiled. 5 arguments are appended to the callback before it is
413 ** invoked:
414 **
415 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
416 ** (2) First descriptive name (depends on authorization type)
417 ** (3) Second descriptive name
418 ** (4) Name of the database (ex: "main", "temp")
419 ** (5) Name of trigger that is doing the access
420 **
421 ** The callback should return on of the following strings: SQLITE_OK,
422 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
423 **
424 ** If this method is invoked with no arguments, the current authorization
425 ** callback string is returned.
426 */
427 case DB_AUTHORIZER: {
drh1211de32004-07-26 12:24:22 +0000428#ifdef SQLITE_OMIT_AUTHORIZATION
429 Tcl_AppendResult(interp, "authorization not available in this build", 0);
430 return TCL_ERROR;
431#else
drhe22a3342003-04-22 20:30:37 +0000432 if( objc>3 ){
433 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +0000434 return TCL_ERROR;
drhe22a3342003-04-22 20:30:37 +0000435 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000436 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000437 Tcl_AppendResult(interp, pDb->zAuth, 0);
438 }
439 }else{
440 char *zAuth;
441 int len;
442 if( pDb->zAuth ){
443 Tcl_Free(pDb->zAuth);
444 }
445 zAuth = Tcl_GetStringFromObj(objv[2], &len);
446 if( zAuth && len>0 ){
447 pDb->zAuth = Tcl_Alloc( len + 1 );
448 strcpy(pDb->zAuth, zAuth);
449 }else{
450 pDb->zAuth = 0;
451 }
drhe22a3342003-04-22 20:30:37 +0000452 if( pDb->zAuth ){
453 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000454 sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
drhe22a3342003-04-22 20:30:37 +0000455 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000456 sqlite3_set_authorizer(pDb->db, 0, 0);
drhe22a3342003-04-22 20:30:37 +0000457 }
drhe22a3342003-04-22 20:30:37 +0000458 }
drh1211de32004-07-26 12:24:22 +0000459#endif
drhe22a3342003-04-22 20:30:37 +0000460 break;
461 }
462
drhbec3f402000-08-04 13:49:02 +0000463 /* $db busy ?CALLBACK?
464 **
465 ** Invoke the given callback if an SQL statement attempts to open
466 ** a locked database file.
467 */
drh6d313162000-09-21 13:01:35 +0000468 case DB_BUSY: {
469 if( objc>3 ){
470 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000471 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000472 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000473 if( pDb->zBusy ){
474 Tcl_AppendResult(interp, pDb->zBusy, 0);
475 }
476 }else{
drh6d313162000-09-21 13:01:35 +0000477 char *zBusy;
478 int len;
drhbec3f402000-08-04 13:49:02 +0000479 if( pDb->zBusy ){
480 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000481 }
drh6d313162000-09-21 13:01:35 +0000482 zBusy = Tcl_GetStringFromObj(objv[2], &len);
483 if( zBusy && len>0 ){
484 pDb->zBusy = Tcl_Alloc( len + 1 );
485 strcpy(pDb->zBusy, zBusy);
486 }else{
487 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000488 }
489 if( pDb->zBusy ){
490 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000491 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000492 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000493 sqlite3_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000494 }
495 }
drh6d313162000-09-21 13:01:35 +0000496 break;
497 }
drhbec3f402000-08-04 13:49:02 +0000498
danielk1977348bb5d2003-10-18 09:37:26 +0000499 /* $db progress ?N CALLBACK?
500 **
501 ** Invoke the given callback every N virtual machine opcodes while executing
502 ** queries.
503 */
504 case DB_PROGRESS: {
505 if( objc==2 ){
506 if( pDb->zProgress ){
507 Tcl_AppendResult(interp, pDb->zProgress, 0);
508 }
509 }else if( objc==4 ){
510 char *zProgress;
511 int len;
512 int N;
513 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
514 return TCL_ERROR;
515 };
516 if( pDb->zProgress ){
517 Tcl_Free(pDb->zProgress);
518 }
519 zProgress = Tcl_GetStringFromObj(objv[3], &len);
520 if( zProgress && len>0 ){
521 pDb->zProgress = Tcl_Alloc( len + 1 );
522 strcpy(pDb->zProgress, zProgress);
523 }else{
524 pDb->zProgress = 0;
525 }
526#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
527 if( pDb->zProgress ){
528 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000529 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
danielk1977348bb5d2003-10-18 09:37:26 +0000530 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000531 sqlite3_progress_handler(pDb->db, 0, 0, 0);
danielk1977348bb5d2003-10-18 09:37:26 +0000532 }
533#endif
534 }else{
535 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
536 return TCL_ERROR;
537 }
538 break;
539 }
540
danielk1977b28af712004-06-21 06:50:26 +0000541 /* $db changes
drhc8d30ac2002-04-12 10:08:59 +0000542 **
543 ** Return the number of rows that were modified, inserted, or deleted by
danielk1977b28af712004-06-21 06:50:26 +0000544 ** the most recent INSERT, UPDATE or DELETE statement, not including
545 ** any changes made by trigger programs.
drhc8d30ac2002-04-12 10:08:59 +0000546 */
547 case DB_CHANGES: {
548 Tcl_Obj *pResult;
drhc8d30ac2002-04-12 10:08:59 +0000549 if( objc!=2 ){
550 Tcl_WrongNumArgs(interp, 2, objv, "");
551 return TCL_ERROR;
552 }
drhc8d30ac2002-04-12 10:08:59 +0000553 pResult = Tcl_GetObjResult(interp);
danielk1977b28af712004-06-21 06:50:26 +0000554 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db));
rdcf146a772004-02-25 22:51:06 +0000555 break;
556 }
557
drh75897232000-05-29 14:26:00 +0000558 /* $db close
559 **
560 ** Shutdown the database
561 */
drh6d313162000-09-21 13:01:35 +0000562 case DB_CLOSE: {
563 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
564 break;
565 }
drh75897232000-05-29 14:26:00 +0000566
drhaa940ea2004-01-15 02:44:03 +0000567 /* $db commit_hook ?CALLBACK?
568 **
569 ** Invoke the given callback just before committing every SQL transaction.
570 ** If the callback throws an exception or returns non-zero, then the
571 ** transaction is aborted. If CALLBACK is an empty string, the callback
572 ** is disabled.
573 */
574 case DB_COMMIT_HOOK: {
575 if( objc>3 ){
576 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +0000577 return TCL_ERROR;
drhaa940ea2004-01-15 02:44:03 +0000578 }else if( objc==2 ){
579 if( pDb->zCommit ){
580 Tcl_AppendResult(interp, pDb->zCommit, 0);
581 }
582 }else{
583 char *zCommit;
584 int len;
585 if( pDb->zCommit ){
586 Tcl_Free(pDb->zCommit);
587 }
588 zCommit = Tcl_GetStringFromObj(objv[2], &len);
589 if( zCommit && len>0 ){
590 pDb->zCommit = Tcl_Alloc( len + 1 );
591 strcpy(pDb->zCommit, zCommit);
592 }else{
593 pDb->zCommit = 0;
594 }
595 if( pDb->zCommit ){
596 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000597 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
drhaa940ea2004-01-15 02:44:03 +0000598 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000599 sqlite3_commit_hook(pDb->db, 0, 0);
drhaa940ea2004-01-15 02:44:03 +0000600 }
601 }
602 break;
603 }
604
drh0f14e2e2004-06-29 12:39:08 +0000605 /*
606 ** $db collate NAME SCRIPT
607 **
608 ** Create a new SQL collation function called NAME. Whenever
609 ** that function is called, invoke SCRIPT to evaluate the function.
610 */
611 case DB_COLLATE: {
612 SqlCollate *pCollate;
613 char *zName;
614 char *zScript;
615 int nScript;
616 if( objc!=4 ){
617 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
618 return TCL_ERROR;
619 }
620 zName = Tcl_GetStringFromObj(objv[2], 0);
621 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
622 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
623 if( pCollate==0 ) return TCL_ERROR;
624 pCollate->interp = interp;
625 pCollate->pNext = pDb->pCollate;
626 pCollate->zScript = (char*)&pCollate[1];
627 pDb->pCollate = pCollate;
628 strcpy(pCollate->zScript, zScript);
629 if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
630 pCollate, tclSqlCollate) ){
631 return TCL_ERROR;
632 }
633 break;
634 }
635
636 /*
637 ** $db collation_needed SCRIPT
638 **
639 ** Create a new SQL collation function called NAME. Whenever
640 ** that function is called, invoke SCRIPT to evaluate the function.
641 */
642 case DB_COLLATION_NEEDED: {
643 if( objc!=3 ){
644 Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT");
645 return TCL_ERROR;
646 }
647 if( pDb->pCollateNeeded ){
648 Tcl_DecrRefCount(pDb->pCollateNeeded);
649 }
650 pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]);
651 Tcl_IncrRefCount(pDb->pCollateNeeded);
652 sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded);
653 break;
654 }
655
drh75897232000-05-29 14:26:00 +0000656 /* $db complete SQL
657 **
658 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
659 ** additional lines of input are needed. This is similar to the
660 ** built-in "info complete" command of Tcl.
661 */
drh6d313162000-09-21 13:01:35 +0000662 case DB_COMPLETE: {
663 Tcl_Obj *pResult;
664 int isComplete;
665 if( objc!=3 ){
666 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000667 return TCL_ERROR;
668 }
danielk19776f8a5032004-05-10 10:34:51 +0000669 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
drh6d313162000-09-21 13:01:35 +0000670 pResult = Tcl_GetObjResult(interp);
671 Tcl_SetBooleanObj(pResult, isComplete);
672 break;
673 }
drhdcd997e2003-01-31 17:21:49 +0000674
675 /*
676 ** $db errorcode
677 **
678 ** Return the numeric error code that was returned by the most recent
danielk19776f8a5032004-05-10 10:34:51 +0000679 ** call to sqlite3_exec().
drhdcd997e2003-01-31 17:21:49 +0000680 */
681 case DB_ERRORCODE: {
danielk1977f3ce83f2004-06-14 11:43:46 +0000682 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
drhdcd997e2003-01-31 17:21:49 +0000683 break;
684 }
drh75897232000-05-29 14:26:00 +0000685
686 /*
drh895d7472004-08-20 16:02:39 +0000687 ** $db eval $sql ?array? ?{ ...code... }?
drh75897232000-05-29 14:26:00 +0000688 **
689 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000690 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000691 ** If "array" and "code" are omitted, then no callback is every invoked.
692 ** If "array" is an empty string, then the values are placed in variables
693 ** that have the same name as the fields extracted by the query.
694 */
drh6d313162000-09-21 13:01:35 +0000695 case DB_EVAL: {
danielk197730ccda12004-05-27 12:11:31 +0000696 char const *zSql;
697 char const *zLeft;
698 sqlite3_stmt *pStmt;
drh92febd92004-08-20 18:34:20 +0000699 Tcl_Obj *pArray; /* Name of array into which results are written */
700 Tcl_Obj *pScript; /* Script to run for each result set */
danielk1977ef2cb632004-05-29 02:37:19 +0000701
702 Tcl_Obj *pRet = Tcl_NewObj();
703 Tcl_IncrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000704
drh92febd92004-08-20 18:34:20 +0000705 if( objc<3 || objc>5 ){
drh895d7472004-08-20 16:02:39 +0000706 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?");
danielk197730ccda12004-05-27 12:11:31 +0000707 return TCL_ERROR;
708 }
drh92febd92004-08-20 18:34:20 +0000709 if( objc==3 ){
710 pArray = pScript = 0;
711 }else if( objc==4 ){
712 pArray = 0;
713 pScript = objv[3];
714 }else{
715 pArray = objv[3];
716 if( Tcl_GetString(pArray)[0]==0 ) pArray = 0;
717 pScript = objv[4];
718 }
danielk197730ccda12004-05-27 12:11:31 +0000719
danielk197730ccda12004-05-27 12:11:31 +0000720 zSql = Tcl_GetStringFromObj(objv[2], 0);
721 while( zSql[0] ){
drh92febd92004-08-20 18:34:20 +0000722 int i; /* Loop counter */
723 int nVar; /* Number of wildcards in the SQL */
724 int nCol; /* Number of columns in the result set */
725 Tcl_Obj **apColName = 0; /* Array of column names */
danielk197730ccda12004-05-27 12:11:31 +0000726
drh92febd92004-08-20 18:34:20 +0000727 /* Compile a single SQL statement */
danielk197730ccda12004-05-27 12:11:31 +0000728 if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000729 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000730 rc = TCL_ERROR;
731 break;
732 }
drh92febd92004-08-20 18:34:20 +0000733 if( pStmt==0 ){
734 if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
735 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
736 rc = TCL_ERROR;
737 break;
738 }else{
739 zSql = zLeft;
740 continue;
danielk197730ccda12004-05-27 12:11:31 +0000741 }
danielk197730ccda12004-05-27 12:11:31 +0000742 }
743
drh92febd92004-08-20 18:34:20 +0000744 /* Bind values to wildcards that begin with $ */
745 nVar = sqlite3_bind_parameter_count(pStmt);
746 for(i=1; i<=nVar; i++){
747 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
748 if( zVar[0]=='$' ){
749 Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0);
750 if( pVar ){
751 int n;
752 u8 *data;
753 char *zType = pVar->typePtr ? pVar->typePtr->name : "";
754 char c = zType[0];
755 if( c=='b' && strcmp(zType,"bytearray")==0 ){
756 data = Tcl_GetByteArrayFromObj(pVar, &n);
757 sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC);
758 }else if( (c=='b' && strcmp(zType,"boolean")==0) ||
759 (c=='i' && strcmp(zType,"int")==0) ){
760 Tcl_GetIntFromObj(interp, pVar, &n);
761 sqlite3_bind_int(pStmt, i, n);
762 }else if( c=='d' && strcmp(zType,"double")==0 ){
763 double r;
764 Tcl_GetDoubleFromObj(interp, pVar, &r);
765 sqlite3_bind_double(pStmt, i, r);
766 }else{
767 data = Tcl_GetStringFromObj(pVar, &n);
768 sqlite3_bind_text(pStmt, i, data, n, SQLITE_STATIC);
769 }
770 }
771 }
772 }
773
774
775 /* Compute column names */
776 nCol = sqlite3_column_count(pStmt);
777 if( pScript ){
778 apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol );
779 if( apColName==0 ) break;
780 for(i=0; i<nCol; i++){
781 apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i));
782 Tcl_IncrRefCount(apColName[i]);
783 }
784 }
785
786 /* If results are being stored in an array variable, then create
787 ** the array(*) entry for that array
788 */
789 if( pArray ){
790 Tcl_Obj *pColList = Tcl_NewObj();
791 Tcl_IncrRefCount(pColList);
792 for(i=0; i<nCol; i++){
793 Tcl_ListObjAppendElement(interp, pColList, apColName[i]);
794 }
795 Tcl_ObjSetVar2(interp, pArray, Tcl_NewStringObj("*",-1), pColList,0);
796 }
797
798 /* Execute the SQL
799 */
danielk197730ccda12004-05-27 12:11:31 +0000800 while( pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
drh92febd92004-08-20 18:34:20 +0000801 for(i=0; i<nCol; i++){
danielk197730ccda12004-05-27 12:11:31 +0000802 Tcl_Obj *pVal;
803
804 /* Set pVal to contain the i'th column of this row. */
drh92febd92004-08-20 18:34:20 +0000805 switch( sqlite3_column_type(pStmt, i) ){
806 case SQLITE_BLOB: {
807 int bytes = sqlite3_column_bytes(pStmt, i);
808 pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes);
809 break;
810 }
811 case SQLITE_INTEGER: {
812 sqlite_int64 v = sqlite3_column_int64(pStmt, i);
813 if( v>=-2147483647 && v<=2147483647 ){
814 pVal = Tcl_NewIntObj(v);
815 }else{
816 pVal = Tcl_NewWideIntObj(v);
817 }
818 break;
819 }
820 case SQLITE_FLOAT: {
821 double r = sqlite3_column_double(pStmt, i);
822 pVal = Tcl_NewDoubleObj(r);
823 break;
824 }
825 default: {
826 pVal = dbTextToObj(sqlite3_column_text(pStmt, i));
827 break;
828 }
danielk197730ccda12004-05-27 12:11:31 +0000829 }
830
drh92febd92004-08-20 18:34:20 +0000831 if( pScript ){
832 if( pArray==0 ){
833 Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0);
danielk197730ccda12004-05-27 12:11:31 +0000834 }else{
drh92febd92004-08-20 18:34:20 +0000835 Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0);
danielk197730ccda12004-05-27 12:11:31 +0000836 }
danielk197730ccda12004-05-27 12:11:31 +0000837 }else{
danielk197730ccda12004-05-27 12:11:31 +0000838 Tcl_ListObjAppendElement(interp, pRet, pVal);
839 }
840 }
841
drh92febd92004-08-20 18:34:20 +0000842 if( pScript ){
843 rc = Tcl_EvalObjEx(interp, pScript, 0);
danielk197730ccda12004-05-27 12:11:31 +0000844 if( rc!=TCL_ERROR ) rc = TCL_OK;
845 }
846 }
drh92febd92004-08-20 18:34:20 +0000847
848 /* Free the column name objects */
849 if( pScript ){
850 for(i=0; i<nCol; i++){
851 Tcl_DecrRefCount(apColName[i]);
852 }
853 Tcl_Free((char*)apColName);
854 }
855
856 /* Finalize the statement. If the result code is SQLITE_SCHEMA, then
857 ** try again to execute the same statement
858 */
859 if( SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){
danielk197730ccda12004-05-27 12:11:31 +0000860 continue;
861 }
862
drh92febd92004-08-20 18:34:20 +0000863 if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000864 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000865 rc = TCL_ERROR;
866 break;
867 }
868
danielk197730ccda12004-05-27 12:11:31 +0000869 zSql = zLeft;
870 }
871
danielk1977ef2cb632004-05-29 02:37:19 +0000872 if( rc==TCL_OK ){
danielk197730ccda12004-05-27 12:11:31 +0000873 Tcl_SetObjResult(interp, pRet);
danielk197730ccda12004-05-27 12:11:31 +0000874 }
danielk1977ef2cb632004-05-29 02:37:19 +0000875 Tcl_DecrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000876
877 break;
878 }
drhbec3f402000-08-04 13:49:02 +0000879
880 /*
drhcabb0812002-09-14 13:47:32 +0000881 ** $db function NAME SCRIPT
882 **
883 ** Create a new SQL function called NAME. Whenever that function is
884 ** called, invoke SCRIPT to evaluate the function.
885 */
886 case DB_FUNCTION: {
887 SqlFunc *pFunc;
888 char *zName;
889 char *zScript;
890 int nScript;
891 if( objc!=4 ){
892 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
893 return TCL_ERROR;
894 }
895 zName = Tcl_GetStringFromObj(objv[2], 0);
896 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
897 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
898 if( pFunc==0 ) return TCL_ERROR;
899 pFunc->interp = interp;
900 pFunc->pNext = pDb->pFunc;
901 pFunc->zScript = (char*)&pFunc[1];
drh0f14e2e2004-06-29 12:39:08 +0000902 pDb->pFunc = pFunc;
drhcabb0812002-09-14 13:47:32 +0000903 strcpy(pFunc->zScript, zScript);
danielk1977c8c11582004-06-29 13:41:21 +0000904 rc = sqlite3_create_function(pDb->db, zName, -1, SQLITE_UTF8,
danielk1977d8123362004-06-12 09:25:12 +0000905 pFunc, tclSqlFunc, 0, 0);
danielk1977c8c11582004-06-29 13:41:21 +0000906 if( rc!=SQLITE_OK ) rc = TCL_ERROR;
drhcabb0812002-09-14 13:47:32 +0000907 break;
908 }
909
910 /*
drhaf9ff332002-01-16 21:00:27 +0000911 ** $db last_insert_rowid
912 **
913 ** Return an integer which is the ROWID for the most recent insert.
914 */
915 case DB_LAST_INSERT_ROWID: {
916 Tcl_Obj *pResult;
917 int rowid;
918 if( objc!=2 ){
919 Tcl_WrongNumArgs(interp, 2, objv, "");
920 return TCL_ERROR;
921 }
danielk19776f8a5032004-05-10 10:34:51 +0000922 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +0000923 pResult = Tcl_GetObjResult(interp);
924 Tcl_SetIntObj(pResult, rowid);
925 break;
926 }
927
928 /*
drh5d9d7572003-08-19 14:31:01 +0000929 ** $db onecolumn SQL
930 **
931 ** Return a single column from a single row of the given SQL query.
932 */
933 case DB_ONECOLUMN: {
drh5d9d7572003-08-19 14:31:01 +0000934 char *zSql;
935 char *zErrMsg = 0;
936 if( objc!=3 ){
937 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
938 return TCL_ERROR;
939 }
940 zSql = Tcl_GetStringFromObj(objv[2], 0);
danielk19776f8a5032004-05-10 10:34:51 +0000941 rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
drh5d9d7572003-08-19 14:31:01 +0000942 if( rc==SQLITE_ABORT ){
drh22fbcb82004-02-01 01:22:50 +0000943 rc = SQLITE_OK;
drh5d9d7572003-08-19 14:31:01 +0000944 }else if( zErrMsg ){
945 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
946 free(zErrMsg);
947 rc = TCL_ERROR;
948 }else if( rc!=SQLITE_OK ){
danielk1977f20b21c2004-05-31 23:56:42 +0000949 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh5d9d7572003-08-19 14:31:01 +0000950 rc = TCL_ERROR;
951 }
952 break;
953 }
954
955 /*
drh22fbcb82004-02-01 01:22:50 +0000956 ** $db rekey KEY
957 **
958 ** Change the encryption key on the currently open database.
959 */
960 case DB_REKEY: {
961 int nKey;
962 void *pKey;
963 if( objc!=3 ){
964 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
965 return TCL_ERROR;
966 }
967 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +0000968#ifdef SQLITE_HAS_CODEC
drh2011d5f2004-07-22 02:40:37 +0000969 rc = sqlite3_rekey(pDb->db, pKey, nKey);
drh22fbcb82004-02-01 01:22:50 +0000970 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000971 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh22fbcb82004-02-01 01:22:50 +0000972 rc = TCL_ERROR;
973 }
974#endif
975 break;
976 }
977
978 /*
drhbec3f402000-08-04 13:49:02 +0000979 ** $db timeout MILLESECONDS
980 **
981 ** Delay for the number of milliseconds specified when a file is locked.
982 */
drh6d313162000-09-21 13:01:35 +0000983 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000984 int ms;
drh6d313162000-09-21 13:01:35 +0000985 if( objc!=3 ){
986 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000987 return TCL_ERROR;
988 }
drh6d313162000-09-21 13:01:35 +0000989 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +0000990 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000991 break;
drh75897232000-05-29 14:26:00 +0000992 }
drhb5a20d32003-04-23 12:25:23 +0000993
drh0f14e2e2004-06-29 12:39:08 +0000994 /*
995 ** $db total_changes
996 **
997 ** Return the number of rows that were modified, inserted, or deleted
998 ** since the database handle was created.
999 */
1000 case DB_TOTAL_CHANGES: {
1001 Tcl_Obj *pResult;
1002 if( objc!=2 ){
1003 Tcl_WrongNumArgs(interp, 2, objv, "");
1004 return TCL_ERROR;
1005 }
1006 pResult = Tcl_GetObjResult(interp);
1007 Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db));
1008 break;
1009 }
1010
drhb5a20d32003-04-23 12:25:23 +00001011 /* $db trace ?CALLBACK?
1012 **
1013 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
1014 ** that is executed. The text of the SQL is appended to CALLBACK before
1015 ** it is executed.
1016 */
1017 case DB_TRACE: {
1018 if( objc>3 ){
1019 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drhb97759e2004-06-29 11:26:59 +00001020 return TCL_ERROR;
drhb5a20d32003-04-23 12:25:23 +00001021 }else if( objc==2 ){
1022 if( pDb->zTrace ){
1023 Tcl_AppendResult(interp, pDb->zTrace, 0);
1024 }
1025 }else{
1026 char *zTrace;
1027 int len;
1028 if( pDb->zTrace ){
1029 Tcl_Free(pDb->zTrace);
1030 }
1031 zTrace = Tcl_GetStringFromObj(objv[2], &len);
1032 if( zTrace && len>0 ){
1033 pDb->zTrace = Tcl_Alloc( len + 1 );
1034 strcpy(pDb->zTrace, zTrace);
1035 }else{
1036 pDb->zTrace = 0;
1037 }
1038 if( pDb->zTrace ){
1039 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +00001040 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +00001041 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001042 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +00001043 }
1044 }
1045 break;
1046 }
1047
drh6d313162000-09-21 13:01:35 +00001048 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +00001049 return rc;
drh75897232000-05-29 14:26:00 +00001050}
1051
1052/*
drh22fbcb82004-02-01 01:22:50 +00001053** sqlite DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +00001054**
1055** This is the main Tcl command. When the "sqlite" Tcl command is
1056** invoked, this routine runs to process that command.
1057**
1058** The first argument, DBNAME, is an arbitrary name for a new
1059** database connection. This command creates a new command named
1060** DBNAME that is used to control that connection. The database
1061** connection is deleted when the DBNAME command is deleted.
1062**
1063** The second argument is the name of the directory that contains
1064** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +00001065**
1066** For testing purposes, we also support the following:
1067**
1068** sqlite -encoding
1069**
1070** Return the encoding used by LIKE and GLOB operators. Choices
1071** are UTF-8 and iso8859.
1072**
drh647cb0e2002-11-04 19:32:25 +00001073** sqlite -version
1074**
1075** Return the version number of the SQLite library.
1076**
drhfbc3eab2001-04-06 16:13:42 +00001077** sqlite -tcl-uses-utf
1078**
1079** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
1080** not. Used by tests to make sure the library was compiled
1081** correctly.
drh75897232000-05-29 14:26:00 +00001082*/
drh22fbcb82004-02-01 01:22:50 +00001083static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +00001084 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +00001085 void *pKey = 0;
1086 int nKey = 0;
1087 const char *zArg;
drh75897232000-05-29 14:26:00 +00001088 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +00001089 const char *zFile;
drh06b27182002-06-26 20:06:05 +00001090 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +00001091 if( objc==2 ){
1092 zArg = Tcl_GetStringFromObj(objv[1], 0);
drh22fbcb82004-02-01 01:22:50 +00001093 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001094 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +00001095 return TCL_OK;
1096 }
drh9eb9e262004-02-11 02:18:05 +00001097 if( strcmp(zArg,"-has-codec")==0 ){
1098#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001099 Tcl_AppendResult(interp,"1",0);
1100#else
1101 Tcl_AppendResult(interp,"0",0);
1102#endif
1103 return TCL_OK;
1104 }
1105 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +00001106#ifdef TCL_UTF_MAX
1107 Tcl_AppendResult(interp,"1",0);
1108#else
1109 Tcl_AppendResult(interp,"0",0);
1110#endif
1111 return TCL_OK;
1112 }
1113 }
drh22fbcb82004-02-01 01:22:50 +00001114 if( objc==5 || objc==6 ){
1115 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
1116 if( strcmp(zArg,"-key")==0 ){
1117 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
1118 objc -= 2;
1119 }
1120 }
1121 if( objc!=3 && objc!=4 ){
1122 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +00001123#ifdef SQLITE_HAS_CODEC
1124 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +00001125#else
1126 "HANDLE FILENAME ?MODE?"
1127#endif
1128 );
drh75897232000-05-29 14:26:00 +00001129 return TCL_ERROR;
1130 }
drh75897232000-05-29 14:26:00 +00001131 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001132 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001133 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001134 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1135 return TCL_ERROR;
1136 }
1137 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00001138 zFile = Tcl_GetStringFromObj(objv[2], 0);
danielk19774f057f92004-06-08 00:02:33 +00001139 sqlite3_open(zFile, &p->db);
danielk197780290862004-05-22 09:21:21 +00001140 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
1141 zErrMsg = strdup(sqlite3_errmsg(p->db));
1142 sqlite3_close(p->db);
1143 p->db = 0;
1144 }
drh2011d5f2004-07-22 02:40:37 +00001145#ifdef SQLITE_HAS_CODEC
1146 sqlite3_key(p->db, pKey, nKey);
drheb8ed702004-02-11 10:37:23 +00001147#endif
drhbec3f402000-08-04 13:49:02 +00001148 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001149 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001150 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001151 free(zErrMsg);
1152 return TCL_ERROR;
1153 }
drh22fbcb82004-02-01 01:22:50 +00001154 zArg = Tcl_GetStringFromObj(objv[1], 0);
1155 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001156
drh06b27182002-06-26 20:06:05 +00001157 /* The return value is the value of the sqlite* pointer
1158 */
1159 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001160 if( strncmp(zBuf,"0x",2) ){
1161 sprintf(zBuf, "0x%p", p->db);
1162 }
drh06b27182002-06-26 20:06:05 +00001163 Tcl_AppendResult(interp, zBuf, 0);
1164
drhc22bd472002-05-10 13:14:07 +00001165 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001166 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001167 */
drh28b4e482002-03-11 02:06:13 +00001168#ifdef SQLITE_TEST
1169 {
drhc22bd472002-05-10 13:14:07 +00001170 extern void Md5_Register(sqlite*);
danielk19775b59af82004-06-30 12:42:59 +00001171#ifdef SQLITE_DEBUG
danielk197713073932004-06-30 11:54:06 +00001172 int mallocfail = sqlite3_iMallocFail;
1173 sqlite3_iMallocFail = 0;
danielk19775b59af82004-06-30 12:42:59 +00001174#endif
drhc22bd472002-05-10 13:14:07 +00001175 Md5_Register(p->db);
danielk19775b59af82004-06-30 12:42:59 +00001176#ifdef SQLITE_DEBUG
danielk197713073932004-06-30 11:54:06 +00001177 sqlite3_iMallocFail = mallocfail;
danielk19775b59af82004-06-30 12:42:59 +00001178#endif
drh06b27182002-06-26 20:06:05 +00001179 }
drh28b4e482002-03-11 02:06:13 +00001180#endif
danielk19777cedc8d2004-06-10 10:50:08 +00001181 p->interp = interp;
drh75897232000-05-29 14:26:00 +00001182 return TCL_OK;
1183}
1184
1185/*
drh90ca9752001-09-28 17:47:14 +00001186** Provide a dummy Tcl_InitStubs if we are using this as a static
1187** library.
1188*/
1189#ifndef USE_TCL_STUBS
1190# undef Tcl_InitStubs
1191# define Tcl_InitStubs(a,b,c)
1192#endif
1193
1194/*
drh75897232000-05-29 14:26:00 +00001195** Initialize this module.
1196**
1197** This Tcl module contains only a single new Tcl command named "sqlite".
1198** (Hence there is no namespace. There is no point in using a namespace
1199** if the extension only supplies one new name!) The "sqlite" command is
1200** used to open a new SQLite database. See the DbMain() routine above
1201** for additional information.
1202*/
drh38f82712004-06-18 17:10:16 +00001203int Sqlite3_Init(Tcl_Interp *interp){
drh92febd92004-08-20 18:34:20 +00001204 Tcl_InitStubs(interp, "8.4", 0);
drhef4ac8f2004-06-19 00:16:31 +00001205 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1206 Tcl_PkgProvide(interp, "sqlite3", "3.0");
drh90ca9752001-09-28 17:47:14 +00001207 return TCL_OK;
1208}
drh38f82712004-06-18 17:10:16 +00001209int Tclsqlite3_Init(Tcl_Interp *interp){
drh92febd92004-08-20 18:34:20 +00001210 Tcl_InitStubs(interp, "8.4", 0);
drhef4ac8f2004-06-19 00:16:31 +00001211 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1212 Tcl_PkgProvide(interp, "sqlite3", "3.0");
drh75897232000-05-29 14:26:00 +00001213 return TCL_OK;
1214}
drh38f82712004-06-18 17:10:16 +00001215int Sqlite3_SafeInit(Tcl_Interp *interp){
drh75897232000-05-29 14:26:00 +00001216 return TCL_OK;
1217}
drh38f82712004-06-18 17:10:16 +00001218int Tclsqlite3_SafeInit(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001219 return TCL_OK;
1220}
drh75897232000-05-29 14:26:00 +00001221
drh3e27c022004-07-23 00:01:38 +00001222#ifdef TCLSH
1223/*****************************************************************************
1224** The code that follows is used to build standalone TCL interpreters
drh75897232000-05-29 14:26:00 +00001225*/
drh348784e2000-05-29 20:41:49 +00001226
1227/*
drh3e27c022004-07-23 00:01:38 +00001228** If the macro TCLSH is one, then put in code this for the
1229** "main" routine that will initialize Tcl and take input from
1230** standard input.
drh348784e2000-05-29 20:41:49 +00001231*/
drh3e27c022004-07-23 00:01:38 +00001232#if TCLSH==1
drh348784e2000-05-29 20:41:49 +00001233static char zMainloop[] =
1234 "set line {}\n"
1235 "while {![eof stdin]} {\n"
1236 "if {$line!=\"\"} {\n"
1237 "puts -nonewline \"> \"\n"
1238 "} else {\n"
1239 "puts -nonewline \"% \"\n"
1240 "}\n"
1241 "flush stdout\n"
1242 "append line [gets stdin]\n"
1243 "if {[info complete $line]} {\n"
1244 "if {[catch {uplevel #0 $line} result]} {\n"
1245 "puts stderr \"Error: $result\"\n"
1246 "} elseif {$result!=\"\"} {\n"
1247 "puts $result\n"
1248 "}\n"
1249 "set line {}\n"
1250 "} else {\n"
1251 "append line \\n\n"
1252 "}\n"
1253 "}\n"
1254;
drh3e27c022004-07-23 00:01:38 +00001255#endif
1256
1257/*
1258** If the macro TCLSH is two, then get the main loop code out of
1259** the separate file "spaceanal_tcl.h".
1260*/
1261#if TCLSH==2
1262static char zMainloop[] =
1263#include "spaceanal_tcl.h"
1264;
1265#endif
drh348784e2000-05-29 20:41:49 +00001266
1267#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1268int TCLSH_MAIN(int argc, char **argv){
1269 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001270 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001271 interp = Tcl_CreateInterp();
drh38f82712004-06-18 17:10:16 +00001272 Sqlite3_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001273#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001274 {
1275 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001276 extern int Sqlitetest2_Init(Tcl_Interp*);
1277 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001278 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001279 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001280 extern int Md5_Init(Tcl_Interp*);
danielk19776490beb2004-05-11 06:17:21 +00001281 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001282 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001283 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00001284 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00001285 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001286 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001287 }
1288#endif
drh3e27c022004-07-23 00:01:38 +00001289 if( argc>=2 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00001290 int i;
1291 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1292 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1293 for(i=2; i<argc; i++){
1294 Tcl_SetVar(interp, "argv", argv[i],
1295 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1296 }
drh3e27c022004-07-23 00:01:38 +00001297 if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001298 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001299 if( zInfo==0 ) zInfo = interp->result;
1300 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001301 return 1;
1302 }
drh3e27c022004-07-23 00:01:38 +00001303 }
1304 if( argc<=1 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00001305 Tcl_GlobalEval(interp, zMainloop);
1306 }
1307 return 0;
1308}
1309#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001310
1311#endif /* !defined(NO_TCL) */