blob: bcc2f0ef6f83de640494542515bbccfd3a629c5c [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**
drh9bb575f2004-09-06 17:24:11 +000014** $Id: tclsqlite.c,v 1.104 2004/09/06 17:24:13 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,
drh9bb575f2004-09-06 17:24:11 +0000208 sqlite3 *db,
danielk19777cedc8d2004-06-10 10:50:08 +0000209 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**
drh9bb575f2004-09-06 17:24:11 +0000370** sqlite3 db1 "my_database"
drh75897232000-05-29 14:26:00 +0000371** 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: {
drh9d74b4c2004-08-24 15:23:34 +0000696 char const *zSql; /* Next SQL statement to execute */
697 char const *zLeft; /* What is left after first stmt in zSql */
698 sqlite3_stmt *pStmt; /* Compiled SQL statment */
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 */
drh1d895032004-08-26 00:56:05 +0000701 Tcl_Obj **apParm; /* Parameters that need a Tcl_DecrRefCount() */
702 int nParm; /* Number of entries used in apParm[] */
703 Tcl_Obj *aParm[10]; /* Static space for apParm[] in the common case */
danielk1977ef2cb632004-05-29 02:37:19 +0000704
705 Tcl_Obj *pRet = Tcl_NewObj();
706 Tcl_IncrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000707
drh92febd92004-08-20 18:34:20 +0000708 if( objc<3 || objc>5 ){
drh895d7472004-08-20 16:02:39 +0000709 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?");
danielk197730ccda12004-05-27 12:11:31 +0000710 return TCL_ERROR;
711 }
drh92febd92004-08-20 18:34:20 +0000712 if( objc==3 ){
713 pArray = pScript = 0;
714 }else if( objc==4 ){
715 pArray = 0;
716 pScript = objv[3];
717 }else{
718 pArray = objv[3];
719 if( Tcl_GetString(pArray)[0]==0 ) pArray = 0;
720 pScript = objv[4];
721 }
danielk197730ccda12004-05-27 12:11:31 +0000722
drh1d895032004-08-26 00:56:05 +0000723 Tcl_IncrRefCount(objv[2]);
danielk197730ccda12004-05-27 12:11:31 +0000724 zSql = Tcl_GetStringFromObj(objv[2], 0);
725 while( zSql[0] ){
drh92febd92004-08-20 18:34:20 +0000726 int i; /* Loop counter */
727 int nVar; /* Number of wildcards in the SQL */
728 int nCol; /* Number of columns in the result set */
729 Tcl_Obj **apColName = 0; /* Array of column names */
danielk197730ccda12004-05-27 12:11:31 +0000730
drh92febd92004-08-20 18:34:20 +0000731 /* Compile a single SQL statement */
danielk197730ccda12004-05-27 12:11:31 +0000732 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 }
drh92febd92004-08-20 18:34:20 +0000737 if( pStmt==0 ){
738 if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
739 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
740 rc = TCL_ERROR;
741 break;
742 }else{
743 zSql = zLeft;
744 continue;
danielk197730ccda12004-05-27 12:11:31 +0000745 }
danielk197730ccda12004-05-27 12:11:31 +0000746 }
747
drh1d895032004-08-26 00:56:05 +0000748 /* Bind values to wildcards that begin with $ or : */
drh92febd92004-08-20 18:34:20 +0000749 nVar = sqlite3_bind_parameter_count(pStmt);
drh1d895032004-08-26 00:56:05 +0000750 nParm = 0;
751 if( nVar>sizeof(aParm)/sizeof(aParm[0]) ){
752 apParm = (Tcl_Obj**)Tcl_Alloc(nVar*sizeof(apParm[0]));
753 }else{
754 apParm = aParm;
755 }
drh92febd92004-08-20 18:34:20 +0000756 for(i=1; i<=nVar; i++){
757 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
drh2c6674c2004-08-25 04:07:01 +0000758 if( zVar[0]=='$' || zVar[0]==':' ){
drh92febd92004-08-20 18:34:20 +0000759 Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0);
760 if( pVar ){
761 int n;
762 u8 *data;
763 char *zType = pVar->typePtr ? pVar->typePtr->name : "";
764 char c = zType[0];
765 if( c=='b' && strcmp(zType,"bytearray")==0 ){
766 data = Tcl_GetByteArrayFromObj(pVar, &n);
767 sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC);
drh1d895032004-08-26 00:56:05 +0000768 Tcl_IncrRefCount(pVar);
769 apParm[nParm++] = pVar;
drh92febd92004-08-20 18:34:20 +0000770 }else if( (c=='b' && strcmp(zType,"boolean")==0) ||
771 (c=='i' && strcmp(zType,"int")==0) ){
772 Tcl_GetIntFromObj(interp, pVar, &n);
773 sqlite3_bind_int(pStmt, i, n);
774 }else if( c=='d' && strcmp(zType,"double")==0 ){
775 double r;
776 Tcl_GetDoubleFromObj(interp, pVar, &r);
777 sqlite3_bind_double(pStmt, i, r);
778 }else{
779 data = Tcl_GetStringFromObj(pVar, &n);
780 sqlite3_bind_text(pStmt, i, data, n, SQLITE_STATIC);
drh1d895032004-08-26 00:56:05 +0000781 Tcl_IncrRefCount(pVar);
782 apParm[nParm++] = pVar;
drh92febd92004-08-20 18:34:20 +0000783 }
784 }
785 }
786 }
787
788
789 /* Compute column names */
790 nCol = sqlite3_column_count(pStmt);
791 if( pScript ){
792 apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol );
793 if( apColName==0 ) break;
794 for(i=0; i<nCol; i++){
795 apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i));
796 Tcl_IncrRefCount(apColName[i]);
797 }
798 }
799
800 /* If results are being stored in an array variable, then create
801 ** the array(*) entry for that array
802 */
803 if( pArray ){
804 Tcl_Obj *pColList = Tcl_NewObj();
805 Tcl_IncrRefCount(pColList);
806 for(i=0; i<nCol; i++){
807 Tcl_ListObjAppendElement(interp, pColList, apColName[i]);
808 }
809 Tcl_ObjSetVar2(interp, pArray, Tcl_NewStringObj("*",-1), pColList,0);
810 }
811
812 /* Execute the SQL
813 */
danielk197730ccda12004-05-27 12:11:31 +0000814 while( pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
drh92febd92004-08-20 18:34:20 +0000815 for(i=0; i<nCol; i++){
danielk197730ccda12004-05-27 12:11:31 +0000816 Tcl_Obj *pVal;
817
818 /* Set pVal to contain the i'th column of this row. */
drh92febd92004-08-20 18:34:20 +0000819 switch( sqlite3_column_type(pStmt, i) ){
820 case SQLITE_BLOB: {
821 int bytes = sqlite3_column_bytes(pStmt, i);
822 pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes);
823 break;
824 }
825 case SQLITE_INTEGER: {
826 sqlite_int64 v = sqlite3_column_int64(pStmt, i);
827 if( v>=-2147483647 && v<=2147483647 ){
828 pVal = Tcl_NewIntObj(v);
829 }else{
830 pVal = Tcl_NewWideIntObj(v);
831 }
832 break;
833 }
834 case SQLITE_FLOAT: {
835 double r = sqlite3_column_double(pStmt, i);
836 pVal = Tcl_NewDoubleObj(r);
837 break;
838 }
839 default: {
840 pVal = dbTextToObj(sqlite3_column_text(pStmt, i));
841 break;
842 }
danielk197730ccda12004-05-27 12:11:31 +0000843 }
844
drh92febd92004-08-20 18:34:20 +0000845 if( pScript ){
846 if( pArray==0 ){
847 Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0);
danielk197730ccda12004-05-27 12:11:31 +0000848 }else{
drh92febd92004-08-20 18:34:20 +0000849 Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0);
danielk197730ccda12004-05-27 12:11:31 +0000850 }
danielk197730ccda12004-05-27 12:11:31 +0000851 }else{
danielk197730ccda12004-05-27 12:11:31 +0000852 Tcl_ListObjAppendElement(interp, pRet, pVal);
853 }
854 }
855
drh92febd92004-08-20 18:34:20 +0000856 if( pScript ){
857 rc = Tcl_EvalObjEx(interp, pScript, 0);
danielk197730ccda12004-05-27 12:11:31 +0000858 if( rc!=TCL_ERROR ) rc = TCL_OK;
859 }
860 }
drh92febd92004-08-20 18:34:20 +0000861
862 /* Free the column name objects */
863 if( pScript ){
864 for(i=0; i<nCol; i++){
865 Tcl_DecrRefCount(apColName[i]);
866 }
867 Tcl_Free((char*)apColName);
868 }
869
drh1d895032004-08-26 00:56:05 +0000870 /* Free the bound string and blob parameters */
871 for(i=0; i<nParm; i++){
872 Tcl_DecrRefCount(apParm[i]);
873 }
874 if( apParm!=aParm ){
875 Tcl_Free((char*)apParm);
876 }
877
drh92febd92004-08-20 18:34:20 +0000878 /* Finalize the statement. If the result code is SQLITE_SCHEMA, then
879 ** try again to execute the same statement
880 */
881 if( SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){
danielk197730ccda12004-05-27 12:11:31 +0000882 continue;
883 }
884
drh92febd92004-08-20 18:34:20 +0000885 if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000886 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000887 rc = TCL_ERROR;
888 break;
889 }
890
danielk197730ccda12004-05-27 12:11:31 +0000891 zSql = zLeft;
892 }
drh1d895032004-08-26 00:56:05 +0000893 Tcl_DecrRefCount(objv[2]);
danielk197730ccda12004-05-27 12:11:31 +0000894
danielk1977ef2cb632004-05-29 02:37:19 +0000895 if( rc==TCL_OK ){
danielk197730ccda12004-05-27 12:11:31 +0000896 Tcl_SetObjResult(interp, pRet);
danielk197730ccda12004-05-27 12:11:31 +0000897 }
danielk1977ef2cb632004-05-29 02:37:19 +0000898 Tcl_DecrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000899
900 break;
901 }
drhbec3f402000-08-04 13:49:02 +0000902
903 /*
drhcabb0812002-09-14 13:47:32 +0000904 ** $db function NAME SCRIPT
905 **
906 ** Create a new SQL function called NAME. Whenever that function is
907 ** called, invoke SCRIPT to evaluate the function.
908 */
909 case DB_FUNCTION: {
910 SqlFunc *pFunc;
911 char *zName;
912 char *zScript;
913 int nScript;
914 if( objc!=4 ){
915 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
916 return TCL_ERROR;
917 }
918 zName = Tcl_GetStringFromObj(objv[2], 0);
919 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
920 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
921 if( pFunc==0 ) return TCL_ERROR;
922 pFunc->interp = interp;
923 pFunc->pNext = pDb->pFunc;
924 pFunc->zScript = (char*)&pFunc[1];
drh0f14e2e2004-06-29 12:39:08 +0000925 pDb->pFunc = pFunc;
drhcabb0812002-09-14 13:47:32 +0000926 strcpy(pFunc->zScript, zScript);
danielk1977c8c11582004-06-29 13:41:21 +0000927 rc = sqlite3_create_function(pDb->db, zName, -1, SQLITE_UTF8,
danielk1977d8123362004-06-12 09:25:12 +0000928 pFunc, tclSqlFunc, 0, 0);
danielk1977c8c11582004-06-29 13:41:21 +0000929 if( rc!=SQLITE_OK ) rc = TCL_ERROR;
drhcabb0812002-09-14 13:47:32 +0000930 break;
931 }
932
933 /*
drhaf9ff332002-01-16 21:00:27 +0000934 ** $db last_insert_rowid
935 **
936 ** Return an integer which is the ROWID for the most recent insert.
937 */
938 case DB_LAST_INSERT_ROWID: {
939 Tcl_Obj *pResult;
940 int rowid;
941 if( objc!=2 ){
942 Tcl_WrongNumArgs(interp, 2, objv, "");
943 return TCL_ERROR;
944 }
danielk19776f8a5032004-05-10 10:34:51 +0000945 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +0000946 pResult = Tcl_GetObjResult(interp);
947 Tcl_SetIntObj(pResult, rowid);
948 break;
949 }
950
951 /*
drh5d9d7572003-08-19 14:31:01 +0000952 ** $db onecolumn SQL
953 **
954 ** Return a single column from a single row of the given SQL query.
955 */
956 case DB_ONECOLUMN: {
drh5d9d7572003-08-19 14:31:01 +0000957 char *zSql;
958 char *zErrMsg = 0;
959 if( objc!=3 ){
960 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
961 return TCL_ERROR;
962 }
963 zSql = Tcl_GetStringFromObj(objv[2], 0);
danielk19776f8a5032004-05-10 10:34:51 +0000964 rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
drh5d9d7572003-08-19 14:31:01 +0000965 if( rc==SQLITE_ABORT ){
drh22fbcb82004-02-01 01:22:50 +0000966 rc = SQLITE_OK;
drh5d9d7572003-08-19 14:31:01 +0000967 }else if( zErrMsg ){
968 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
969 free(zErrMsg);
970 rc = TCL_ERROR;
971 }else if( rc!=SQLITE_OK ){
danielk1977f20b21c2004-05-31 23:56:42 +0000972 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh5d9d7572003-08-19 14:31:01 +0000973 rc = TCL_ERROR;
974 }
975 break;
976 }
977
978 /*
drh22fbcb82004-02-01 01:22:50 +0000979 ** $db rekey KEY
980 **
981 ** Change the encryption key on the currently open database.
982 */
983 case DB_REKEY: {
984 int nKey;
985 void *pKey;
986 if( objc!=3 ){
987 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
988 return TCL_ERROR;
989 }
990 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +0000991#ifdef SQLITE_HAS_CODEC
drh2011d5f2004-07-22 02:40:37 +0000992 rc = sqlite3_rekey(pDb->db, pKey, nKey);
drh22fbcb82004-02-01 01:22:50 +0000993 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000994 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh22fbcb82004-02-01 01:22:50 +0000995 rc = TCL_ERROR;
996 }
997#endif
998 break;
999 }
1000
1001 /*
drhbec3f402000-08-04 13:49:02 +00001002 ** $db timeout MILLESECONDS
1003 **
1004 ** Delay for the number of milliseconds specified when a file is locked.
1005 */
drh6d313162000-09-21 13:01:35 +00001006 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +00001007 int ms;
drh6d313162000-09-21 13:01:35 +00001008 if( objc!=3 ){
1009 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +00001010 return TCL_ERROR;
1011 }
drh6d313162000-09-21 13:01:35 +00001012 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +00001013 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +00001014 break;
drh75897232000-05-29 14:26:00 +00001015 }
drhb5a20d32003-04-23 12:25:23 +00001016
drh0f14e2e2004-06-29 12:39:08 +00001017 /*
1018 ** $db total_changes
1019 **
1020 ** Return the number of rows that were modified, inserted, or deleted
1021 ** since the database handle was created.
1022 */
1023 case DB_TOTAL_CHANGES: {
1024 Tcl_Obj *pResult;
1025 if( objc!=2 ){
1026 Tcl_WrongNumArgs(interp, 2, objv, "");
1027 return TCL_ERROR;
1028 }
1029 pResult = Tcl_GetObjResult(interp);
1030 Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db));
1031 break;
1032 }
1033
drhb5a20d32003-04-23 12:25:23 +00001034 /* $db trace ?CALLBACK?
1035 **
1036 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
1037 ** that is executed. The text of the SQL is appended to CALLBACK before
1038 ** it is executed.
1039 */
1040 case DB_TRACE: {
1041 if( objc>3 ){
1042 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drhb97759e2004-06-29 11:26:59 +00001043 return TCL_ERROR;
drhb5a20d32003-04-23 12:25:23 +00001044 }else if( objc==2 ){
1045 if( pDb->zTrace ){
1046 Tcl_AppendResult(interp, pDb->zTrace, 0);
1047 }
1048 }else{
1049 char *zTrace;
1050 int len;
1051 if( pDb->zTrace ){
1052 Tcl_Free(pDb->zTrace);
1053 }
1054 zTrace = Tcl_GetStringFromObj(objv[2], &len);
1055 if( zTrace && len>0 ){
1056 pDb->zTrace = Tcl_Alloc( len + 1 );
1057 strcpy(pDb->zTrace, zTrace);
1058 }else{
1059 pDb->zTrace = 0;
1060 }
1061 if( pDb->zTrace ){
1062 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +00001063 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +00001064 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001065 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +00001066 }
1067 }
1068 break;
1069 }
1070
drh6d313162000-09-21 13:01:35 +00001071 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +00001072 return rc;
drh75897232000-05-29 14:26:00 +00001073}
1074
1075/*
drh9bb575f2004-09-06 17:24:11 +00001076** sqlite3 DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +00001077**
1078** This is the main Tcl command. When the "sqlite" Tcl command is
1079** invoked, this routine runs to process that command.
1080**
1081** The first argument, DBNAME, is an arbitrary name for a new
1082** database connection. This command creates a new command named
1083** DBNAME that is used to control that connection. The database
1084** connection is deleted when the DBNAME command is deleted.
1085**
1086** The second argument is the name of the directory that contains
1087** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +00001088**
1089** For testing purposes, we also support the following:
1090**
drh9bb575f2004-09-06 17:24:11 +00001091** sqlite3 -encoding
drhfbc3eab2001-04-06 16:13:42 +00001092**
1093** Return the encoding used by LIKE and GLOB operators. Choices
1094** are UTF-8 and iso8859.
1095**
drh9bb575f2004-09-06 17:24:11 +00001096** sqlite3 -version
drh647cb0e2002-11-04 19:32:25 +00001097**
1098** Return the version number of the SQLite library.
1099**
drh9bb575f2004-09-06 17:24:11 +00001100** sqlite3 -tcl-uses-utf
drhfbc3eab2001-04-06 16:13:42 +00001101**
1102** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
1103** not. Used by tests to make sure the library was compiled
1104** correctly.
drh75897232000-05-29 14:26:00 +00001105*/
drh22fbcb82004-02-01 01:22:50 +00001106static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +00001107 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +00001108 void *pKey = 0;
1109 int nKey = 0;
1110 const char *zArg;
drh75897232000-05-29 14:26:00 +00001111 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +00001112 const char *zFile;
drh06b27182002-06-26 20:06:05 +00001113 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +00001114 if( objc==2 ){
1115 zArg = Tcl_GetStringFromObj(objv[1], 0);
drh22fbcb82004-02-01 01:22:50 +00001116 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001117 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +00001118 return TCL_OK;
1119 }
drh9eb9e262004-02-11 02:18:05 +00001120 if( strcmp(zArg,"-has-codec")==0 ){
1121#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001122 Tcl_AppendResult(interp,"1",0);
1123#else
1124 Tcl_AppendResult(interp,"0",0);
1125#endif
1126 return TCL_OK;
1127 }
1128 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +00001129#ifdef TCL_UTF_MAX
1130 Tcl_AppendResult(interp,"1",0);
1131#else
1132 Tcl_AppendResult(interp,"0",0);
1133#endif
1134 return TCL_OK;
1135 }
1136 }
drh22fbcb82004-02-01 01:22:50 +00001137 if( objc==5 || objc==6 ){
1138 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
1139 if( strcmp(zArg,"-key")==0 ){
1140 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
1141 objc -= 2;
1142 }
1143 }
1144 if( objc!=3 && objc!=4 ){
1145 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +00001146#ifdef SQLITE_HAS_CODEC
1147 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +00001148#else
1149 "HANDLE FILENAME ?MODE?"
1150#endif
1151 );
drh75897232000-05-29 14:26:00 +00001152 return TCL_ERROR;
1153 }
drh75897232000-05-29 14:26:00 +00001154 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001155 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001156 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001157 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1158 return TCL_ERROR;
1159 }
1160 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00001161 zFile = Tcl_GetStringFromObj(objv[2], 0);
danielk19774f057f92004-06-08 00:02:33 +00001162 sqlite3_open(zFile, &p->db);
danielk197780290862004-05-22 09:21:21 +00001163 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
1164 zErrMsg = strdup(sqlite3_errmsg(p->db));
1165 sqlite3_close(p->db);
1166 p->db = 0;
1167 }
drh2011d5f2004-07-22 02:40:37 +00001168#ifdef SQLITE_HAS_CODEC
1169 sqlite3_key(p->db, pKey, nKey);
drheb8ed702004-02-11 10:37:23 +00001170#endif
drhbec3f402000-08-04 13:49:02 +00001171 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001172 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001173 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001174 free(zErrMsg);
1175 return TCL_ERROR;
1176 }
drh22fbcb82004-02-01 01:22:50 +00001177 zArg = Tcl_GetStringFromObj(objv[1], 0);
1178 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001179
drh06b27182002-06-26 20:06:05 +00001180 /* The return value is the value of the sqlite* pointer
1181 */
1182 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001183 if( strncmp(zBuf,"0x",2) ){
1184 sprintf(zBuf, "0x%p", p->db);
1185 }
drh06b27182002-06-26 20:06:05 +00001186 Tcl_AppendResult(interp, zBuf, 0);
1187
drhc22bd472002-05-10 13:14:07 +00001188 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001189 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001190 */
drh28b4e482002-03-11 02:06:13 +00001191#ifdef SQLITE_TEST
1192 {
drh9bb575f2004-09-06 17:24:11 +00001193 extern void Md5_Register(sqlite3*);
danielk19775b59af82004-06-30 12:42:59 +00001194#ifdef SQLITE_DEBUG
danielk197713073932004-06-30 11:54:06 +00001195 int mallocfail = sqlite3_iMallocFail;
1196 sqlite3_iMallocFail = 0;
danielk19775b59af82004-06-30 12:42:59 +00001197#endif
drhc22bd472002-05-10 13:14:07 +00001198 Md5_Register(p->db);
danielk19775b59af82004-06-30 12:42:59 +00001199#ifdef SQLITE_DEBUG
danielk197713073932004-06-30 11:54:06 +00001200 sqlite3_iMallocFail = mallocfail;
danielk19775b59af82004-06-30 12:42:59 +00001201#endif
drh06b27182002-06-26 20:06:05 +00001202 }
drh28b4e482002-03-11 02:06:13 +00001203#endif
danielk19777cedc8d2004-06-10 10:50:08 +00001204 p->interp = interp;
drh75897232000-05-29 14:26:00 +00001205 return TCL_OK;
1206}
1207
1208/*
drh90ca9752001-09-28 17:47:14 +00001209** Provide a dummy Tcl_InitStubs if we are using this as a static
1210** library.
1211*/
1212#ifndef USE_TCL_STUBS
1213# undef Tcl_InitStubs
1214# define Tcl_InitStubs(a,b,c)
1215#endif
1216
1217/*
drh75897232000-05-29 14:26:00 +00001218** Initialize this module.
1219**
1220** This Tcl module contains only a single new Tcl command named "sqlite".
1221** (Hence there is no namespace. There is no point in using a namespace
1222** if the extension only supplies one new name!) The "sqlite" command is
1223** used to open a new SQLite database. See the DbMain() routine above
1224** for additional information.
1225*/
drh38f82712004-06-18 17:10:16 +00001226int Sqlite3_Init(Tcl_Interp *interp){
drh92febd92004-08-20 18:34:20 +00001227 Tcl_InitStubs(interp, "8.4", 0);
drhef4ac8f2004-06-19 00:16:31 +00001228 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1229 Tcl_PkgProvide(interp, "sqlite3", "3.0");
drh90ca9752001-09-28 17:47:14 +00001230 return TCL_OK;
1231}
drh38f82712004-06-18 17:10:16 +00001232int Tclsqlite3_Init(Tcl_Interp *interp){
drh92febd92004-08-20 18:34:20 +00001233 Tcl_InitStubs(interp, "8.4", 0);
drhef4ac8f2004-06-19 00:16:31 +00001234 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1235 Tcl_PkgProvide(interp, "sqlite3", "3.0");
drh75897232000-05-29 14:26:00 +00001236 return TCL_OK;
1237}
drh38f82712004-06-18 17:10:16 +00001238int Sqlite3_SafeInit(Tcl_Interp *interp){
drh75897232000-05-29 14:26:00 +00001239 return TCL_OK;
1240}
drh38f82712004-06-18 17:10:16 +00001241int Tclsqlite3_SafeInit(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001242 return TCL_OK;
1243}
drh75897232000-05-29 14:26:00 +00001244
drh3e27c022004-07-23 00:01:38 +00001245#ifdef TCLSH
1246/*****************************************************************************
1247** The code that follows is used to build standalone TCL interpreters
drh75897232000-05-29 14:26:00 +00001248*/
drh348784e2000-05-29 20:41:49 +00001249
1250/*
drh3e27c022004-07-23 00:01:38 +00001251** If the macro TCLSH is one, then put in code this for the
1252** "main" routine that will initialize Tcl and take input from
1253** standard input.
drh348784e2000-05-29 20:41:49 +00001254*/
drh3e27c022004-07-23 00:01:38 +00001255#if TCLSH==1
drh348784e2000-05-29 20:41:49 +00001256static char zMainloop[] =
1257 "set line {}\n"
1258 "while {![eof stdin]} {\n"
1259 "if {$line!=\"\"} {\n"
1260 "puts -nonewline \"> \"\n"
1261 "} else {\n"
1262 "puts -nonewline \"% \"\n"
1263 "}\n"
1264 "flush stdout\n"
1265 "append line [gets stdin]\n"
1266 "if {[info complete $line]} {\n"
1267 "if {[catch {uplevel #0 $line} result]} {\n"
1268 "puts stderr \"Error: $result\"\n"
1269 "} elseif {$result!=\"\"} {\n"
1270 "puts $result\n"
1271 "}\n"
1272 "set line {}\n"
1273 "} else {\n"
1274 "append line \\n\n"
1275 "}\n"
1276 "}\n"
1277;
drh3e27c022004-07-23 00:01:38 +00001278#endif
1279
1280/*
1281** If the macro TCLSH is two, then get the main loop code out of
1282** the separate file "spaceanal_tcl.h".
1283*/
1284#if TCLSH==2
1285static char zMainloop[] =
1286#include "spaceanal_tcl.h"
1287;
1288#endif
drh348784e2000-05-29 20:41:49 +00001289
1290#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1291int TCLSH_MAIN(int argc, char **argv){
1292 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001293 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001294 interp = Tcl_CreateInterp();
drh38f82712004-06-18 17:10:16 +00001295 Sqlite3_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001296#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001297 {
1298 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001299 extern int Sqlitetest2_Init(Tcl_Interp*);
1300 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001301 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001302 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001303 extern int Md5_Init(Tcl_Interp*);
danielk19776490beb2004-05-11 06:17:21 +00001304 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001305 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001306 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00001307 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00001308 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001309 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001310 }
1311#endif
drh3e27c022004-07-23 00:01:38 +00001312 if( argc>=2 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00001313 int i;
1314 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1315 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1316 for(i=2; i<argc; i++){
1317 Tcl_SetVar(interp, "argv", argv[i],
1318 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1319 }
drh3e27c022004-07-23 00:01:38 +00001320 if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001321 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001322 if( zInfo==0 ) zInfo = interp->result;
1323 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001324 return 1;
1325 }
drh3e27c022004-07-23 00:01:38 +00001326 }
1327 if( argc<=1 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00001328 Tcl_GlobalEval(interp, zMainloop);
1329 }
1330 return 0;
1331}
1332#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001333
1334#endif /* !defined(NO_TCL) */