blob: fa82058d0163268a2a5c65ad73745094fe3fa349 [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**
danielk19771c8c23c2004-11-12 15:53:37 +000014** $Id: tclsqlite.c,v 1.107 2004/11/12 15:53:37 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000015*/
drh6d313162000-09-21 13:01:35 +000016#ifndef NO_TCL /* Omit this whole file if TCL is unavailable */
17
drh06b27182002-06-26 20:06:05 +000018#include "sqliteInt.h"
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/*
drh895d7472004-08-20 16:02:39 +000077** TCL calls this procedure when an sqlite3 database command is
78** deleted.
drh75897232000-05-29 14:26:00 +000079*/
80static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +000081 SqliteDb *pDb = (SqliteDb*)db;
danielk19776f8a5032004-05-10 10:34:51 +000082 sqlite3_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +000083 while( pDb->pFunc ){
84 SqlFunc *pFunc = pDb->pFunc;
85 pDb->pFunc = pFunc->pNext;
86 Tcl_Free((char*)pFunc);
87 }
danielk19770202b292004-06-09 09:55:16 +000088 while( pDb->pCollate ){
89 SqlCollate *pCollate = pDb->pCollate;
90 pDb->pCollate = pCollate->pNext;
91 Tcl_Free((char*)pCollate);
92 }
drhbec3f402000-08-04 13:49:02 +000093 if( pDb->zBusy ){
94 Tcl_Free(pDb->zBusy);
95 }
drhb5a20d32003-04-23 12:25:23 +000096 if( pDb->zTrace ){
97 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +000098 }
drhe22a3342003-04-22 20:30:37 +000099 if( pDb->zAuth ){
100 Tcl_Free(pDb->zAuth);
101 }
drhbec3f402000-08-04 13:49:02 +0000102 Tcl_Free((char*)pDb);
103}
104
105/*
106** This routine is called when a database file is locked while trying
107** to execute SQL.
108*/
danielk19772a764eb2004-06-12 01:43:26 +0000109static int DbBusyHandler(void *cd, int nTries){
drhbec3f402000-08-04 13:49:02 +0000110 SqliteDb *pDb = (SqliteDb*)cd;
111 int rc;
112 char zVal[30];
113 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000114 Tcl_DString cmd;
115
116 Tcl_DStringInit(&cmd);
117 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
danielk19772a764eb2004-06-12 01:43:26 +0000118 sprintf(zVal, "%d", nTries);
119 Tcl_DStringAppendElement(&cmd, zVal);
drhbec3f402000-08-04 13:49:02 +0000120 zCmd = Tcl_DStringValue(&cmd);
121 rc = Tcl_Eval(pDb->interp, zCmd);
122 Tcl_DStringFree(&cmd);
123 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
124 return 0;
125 }
126 return 1;
drh75897232000-05-29 14:26:00 +0000127}
128
129/*
danielk1977348bb5d2003-10-18 09:37:26 +0000130** This routine is invoked as the 'progress callback' for the database.
131*/
132static int DbProgressHandler(void *cd){
133 SqliteDb *pDb = (SqliteDb*)cd;
134 int rc;
135
136 assert( pDb->zProgress );
137 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
138 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
139 return 1;
140 }
141 return 0;
142}
143
144/*
drhb5a20d32003-04-23 12:25:23 +0000145** This routine is called by the SQLite trace handler whenever a new
146** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000147*/
drhb5a20d32003-04-23 12:25:23 +0000148static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000149 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000150 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000151
drhb5a20d32003-04-23 12:25:23 +0000152 Tcl_DStringInit(&str);
153 Tcl_DStringAppend(&str, pDb->zTrace, -1);
154 Tcl_DStringAppendElement(&str, zSql);
155 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
156 Tcl_DStringFree(&str);
157 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000158}
159
160/*
drhaa940ea2004-01-15 02:44:03 +0000161** This routine is called when a transaction is committed. The
162** TCL script in pDb->zCommit is executed. If it returns non-zero or
163** if it throws an exception, the transaction is rolled back instead
164** of being committed.
165*/
166static int DbCommitHandler(void *cd){
167 SqliteDb *pDb = (SqliteDb*)cd;
168 int rc;
169
170 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
171 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
172 return 1;
173 }
174 return 0;
175}
176
danielk19777cedc8d2004-06-10 10:50:08 +0000177static void tclCollateNeeded(
178 void *pCtx,
drh9bb575f2004-09-06 17:24:11 +0000179 sqlite3 *db,
danielk19777cedc8d2004-06-10 10:50:08 +0000180 int enc,
181 const char *zName
182){
183 SqliteDb *pDb = (SqliteDb *)pCtx;
184 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
185 Tcl_IncrRefCount(pScript);
186 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
187 Tcl_EvalObjEx(pDb->interp, pScript, 0);
188 Tcl_DecrRefCount(pScript);
189}
190
drhaa940ea2004-01-15 02:44:03 +0000191/*
danielk19770202b292004-06-09 09:55:16 +0000192** This routine is called to evaluate an SQL collation function implemented
193** using TCL script.
194*/
195static int tclSqlCollate(
196 void *pCtx,
197 int nA,
198 const void *zA,
199 int nB,
200 const void *zB
201){
202 SqlCollate *p = (SqlCollate *)pCtx;
203 Tcl_Obj *pCmd;
204
205 pCmd = Tcl_NewStringObj(p->zScript, -1);
206 Tcl_IncrRefCount(pCmd);
207 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA));
208 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB));
209 Tcl_EvalObjEx(p->interp, pCmd, 0);
210 Tcl_DecrRefCount(pCmd);
211 return (atoi(Tcl_GetStringResult(p->interp)));
212}
213
214/*
drhcabb0812002-09-14 13:47:32 +0000215** This routine is called to evaluate an SQL function implemented
216** using TCL script.
217*/
danielk19770ae8b832004-05-25 12:05:56 +0000218static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk19776f8a5032004-05-10 10:34:51 +0000219 SqlFunc *p = sqlite3_user_data(context);
drhcabb0812002-09-14 13:47:32 +0000220 Tcl_DString cmd;
221 int i;
222 int rc;
223
224 Tcl_DStringInit(&cmd);
225 Tcl_DStringAppend(&cmd, p->zScript, -1);
226 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000227 if( SQLITE_NULL==sqlite3_value_type(argv[i]) ){
danielk197751ad0ec2004-05-24 12:39:02 +0000228 Tcl_DStringAppendElement(&cmd, "");
229 }else{
drh4f26d6c2004-05-26 23:25:30 +0000230 Tcl_DStringAppendElement(&cmd, sqlite3_value_text(argv[i]));
danielk197751ad0ec2004-05-24 12:39:02 +0000231 }
drhcabb0812002-09-14 13:47:32 +0000232 }
233 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
234 if( rc ){
danielk19777e18c252004-05-25 11:47:24 +0000235 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhcabb0812002-09-14 13:47:32 +0000236 }else{
danielk1977d8123362004-06-12 09:25:12 +0000237 sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1,
238 SQLITE_TRANSIENT);
drhcabb0812002-09-14 13:47:32 +0000239 }
240}
drh895d7472004-08-20 16:02:39 +0000241
drhe22a3342003-04-22 20:30:37 +0000242#ifndef SQLITE_OMIT_AUTHORIZATION
243/*
244** This is the authentication function. It appends the authentication
245** type code and the two arguments to zCmd[] then invokes the result
246** on the interpreter. The reply is examined to determine if the
247** authentication fails or succeeds.
248*/
249static int auth_callback(
250 void *pArg,
251 int code,
252 const char *zArg1,
253 const char *zArg2,
254 const char *zArg3,
255 const char *zArg4
256){
257 char *zCode;
258 Tcl_DString str;
259 int rc;
260 const char *zReply;
261 SqliteDb *pDb = (SqliteDb*)pArg;
262
263 switch( code ){
264 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
265 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
266 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
267 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
268 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
269 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
270 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
271 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
272 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
273 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
274 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
275 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
276 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
277 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
278 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
279 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
280 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
281 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
282 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
283 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
284 case SQLITE_READ : zCode="SQLITE_READ"; break;
285 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
286 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
287 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000288 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
289 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
danielk19771c8c23c2004-11-12 15:53:37 +0000290 case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break;
drhe22a3342003-04-22 20:30:37 +0000291 default : zCode="????"; break;
292 }
293 Tcl_DStringInit(&str);
294 Tcl_DStringAppend(&str, pDb->zAuth, -1);
295 Tcl_DStringAppendElement(&str, zCode);
296 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
297 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
298 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
299 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
300 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
301 Tcl_DStringFree(&str);
302 zReply = Tcl_GetStringResult(pDb->interp);
303 if( strcmp(zReply,"SQLITE_OK")==0 ){
304 rc = SQLITE_OK;
305 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
306 rc = SQLITE_DENY;
307 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
308 rc = SQLITE_IGNORE;
309 }else{
310 rc = 999;
311 }
312 return rc;
313}
314#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000315
316/*
danielk1977ef2cb632004-05-29 02:37:19 +0000317** zText is a pointer to text obtained via an sqlite3_result_text()
318** or similar interface. This routine returns a Tcl string object,
319** reference count set to 0, containing the text. If a translation
320** between iso8859 and UTF-8 is required, it is preformed.
321*/
322static Tcl_Obj *dbTextToObj(char const *zText){
323 Tcl_Obj *pVal;
324#ifdef UTF_TRANSLATION_NEEDED
325 Tcl_DString dCol;
326 Tcl_DStringInit(&dCol);
327 Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol);
328 pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
329 Tcl_DStringFree(&dCol);
330#else
331 pVal = Tcl_NewStringObj(zText, -1);
332#endif
333 return pVal;
334}
335
336/*
drh75897232000-05-29 14:26:00 +0000337** The "sqlite" command below creates a new Tcl command for each
338** connection it opens to an SQLite database. This routine is invoked
339** whenever one of those connection-specific commands is executed
340** in Tcl. For example, if you run Tcl code like this:
341**
drh9bb575f2004-09-06 17:24:11 +0000342** sqlite3 db1 "my_database"
drh75897232000-05-29 14:26:00 +0000343** db1 close
344**
345** The first command opens a connection to the "my_database" database
346** and calls that connection "db1". The second command causes this
347** subroutine to be invoked.
348*/
drh6d313162000-09-21 13:01:35 +0000349static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000350 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000351 int choice;
drh22fbcb82004-02-01 01:22:50 +0000352 int rc = TCL_OK;
drh0de8c112002-07-06 16:32:14 +0000353 static const char *DB_strs[] = {
drh0f14e2e2004-06-29 12:39:08 +0000354 "authorizer", "busy", "changes",
355 "close", "collate", "collation_needed",
356 "commit_hook", "complete", "errorcode",
357 "eval", "function", "last_insert_rowid",
358 "onecolumn", "progress", "rekey",
359 "timeout", "total_changes", "trace",
360 0
drh6d313162000-09-21 13:01:35 +0000361 };
drh411995d2002-06-25 19:31:18 +0000362 enum DB_enum {
drh0f14e2e2004-06-29 12:39:08 +0000363 DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
364 DB_CLOSE, DB_COLLATE, DB_COLLATION_NEEDED,
365 DB_COMMIT_HOOK, DB_COMPLETE, DB_ERRORCODE,
366 DB_EVAL, DB_FUNCTION, DB_LAST_INSERT_ROWID,
367 DB_ONECOLUMN, DB_PROGRESS, DB_REKEY,
368 DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE,
drh6d313162000-09-21 13:01:35 +0000369 };
370
371 if( objc<2 ){
372 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000373 return TCL_ERROR;
374 }
drh411995d2002-06-25 19:31:18 +0000375 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000376 return TCL_ERROR;
377 }
378
drh411995d2002-06-25 19:31:18 +0000379 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000380
drhe22a3342003-04-22 20:30:37 +0000381 /* $db authorizer ?CALLBACK?
382 **
383 ** Invoke the given callback to authorize each SQL operation as it is
384 ** compiled. 5 arguments are appended to the callback before it is
385 ** invoked:
386 **
387 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
388 ** (2) First descriptive name (depends on authorization type)
389 ** (3) Second descriptive name
390 ** (4) Name of the database (ex: "main", "temp")
391 ** (5) Name of trigger that is doing the access
392 **
393 ** The callback should return on of the following strings: SQLITE_OK,
394 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
395 **
396 ** If this method is invoked with no arguments, the current authorization
397 ** callback string is returned.
398 */
399 case DB_AUTHORIZER: {
drh1211de32004-07-26 12:24:22 +0000400#ifdef SQLITE_OMIT_AUTHORIZATION
401 Tcl_AppendResult(interp, "authorization not available in this build", 0);
402 return TCL_ERROR;
403#else
drhe22a3342003-04-22 20:30:37 +0000404 if( objc>3 ){
405 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +0000406 return TCL_ERROR;
drhe22a3342003-04-22 20:30:37 +0000407 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000408 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000409 Tcl_AppendResult(interp, pDb->zAuth, 0);
410 }
411 }else{
412 char *zAuth;
413 int len;
414 if( pDb->zAuth ){
415 Tcl_Free(pDb->zAuth);
416 }
417 zAuth = Tcl_GetStringFromObj(objv[2], &len);
418 if( zAuth && len>0 ){
419 pDb->zAuth = Tcl_Alloc( len + 1 );
420 strcpy(pDb->zAuth, zAuth);
421 }else{
422 pDb->zAuth = 0;
423 }
drhe22a3342003-04-22 20:30:37 +0000424 if( pDb->zAuth ){
425 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000426 sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
drhe22a3342003-04-22 20:30:37 +0000427 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000428 sqlite3_set_authorizer(pDb->db, 0, 0);
drhe22a3342003-04-22 20:30:37 +0000429 }
drhe22a3342003-04-22 20:30:37 +0000430 }
drh1211de32004-07-26 12:24:22 +0000431#endif
drhe22a3342003-04-22 20:30:37 +0000432 break;
433 }
434
drhbec3f402000-08-04 13:49:02 +0000435 /* $db busy ?CALLBACK?
436 **
437 ** Invoke the given callback if an SQL statement attempts to open
438 ** a locked database file.
439 */
drh6d313162000-09-21 13:01:35 +0000440 case DB_BUSY: {
441 if( objc>3 ){
442 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000443 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000444 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000445 if( pDb->zBusy ){
446 Tcl_AppendResult(interp, pDb->zBusy, 0);
447 }
448 }else{
drh6d313162000-09-21 13:01:35 +0000449 char *zBusy;
450 int len;
drhbec3f402000-08-04 13:49:02 +0000451 if( pDb->zBusy ){
452 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000453 }
drh6d313162000-09-21 13:01:35 +0000454 zBusy = Tcl_GetStringFromObj(objv[2], &len);
455 if( zBusy && len>0 ){
456 pDb->zBusy = Tcl_Alloc( len + 1 );
457 strcpy(pDb->zBusy, zBusy);
458 }else{
459 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000460 }
461 if( pDb->zBusy ){
462 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000463 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000464 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000465 sqlite3_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000466 }
467 }
drh6d313162000-09-21 13:01:35 +0000468 break;
469 }
drhbec3f402000-08-04 13:49:02 +0000470
danielk1977b28af712004-06-21 06:50:26 +0000471 /* $db changes
drhc8d30ac2002-04-12 10:08:59 +0000472 **
473 ** Return the number of rows that were modified, inserted, or deleted by
danielk1977b28af712004-06-21 06:50:26 +0000474 ** the most recent INSERT, UPDATE or DELETE statement, not including
475 ** any changes made by trigger programs.
drhc8d30ac2002-04-12 10:08:59 +0000476 */
477 case DB_CHANGES: {
478 Tcl_Obj *pResult;
drhc8d30ac2002-04-12 10:08:59 +0000479 if( objc!=2 ){
480 Tcl_WrongNumArgs(interp, 2, objv, "");
481 return TCL_ERROR;
482 }
drhc8d30ac2002-04-12 10:08:59 +0000483 pResult = Tcl_GetObjResult(interp);
danielk1977b28af712004-06-21 06:50:26 +0000484 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db));
rdcf146a772004-02-25 22:51:06 +0000485 break;
486 }
487
drh75897232000-05-29 14:26:00 +0000488 /* $db close
489 **
490 ** Shutdown the database
491 */
drh6d313162000-09-21 13:01:35 +0000492 case DB_CLOSE: {
493 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
494 break;
495 }
drh75897232000-05-29 14:26:00 +0000496
drhaa940ea2004-01-15 02:44:03 +0000497 /* $db commit_hook ?CALLBACK?
498 **
499 ** Invoke the given callback just before committing every SQL transaction.
500 ** If the callback throws an exception or returns non-zero, then the
501 ** transaction is aborted. If CALLBACK is an empty string, the callback
502 ** is disabled.
503 */
504 case DB_COMMIT_HOOK: {
505 if( objc>3 ){
506 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +0000507 return TCL_ERROR;
drhaa940ea2004-01-15 02:44:03 +0000508 }else if( objc==2 ){
509 if( pDb->zCommit ){
510 Tcl_AppendResult(interp, pDb->zCommit, 0);
511 }
512 }else{
513 char *zCommit;
514 int len;
515 if( pDb->zCommit ){
516 Tcl_Free(pDb->zCommit);
517 }
518 zCommit = Tcl_GetStringFromObj(objv[2], &len);
519 if( zCommit && len>0 ){
520 pDb->zCommit = Tcl_Alloc( len + 1 );
521 strcpy(pDb->zCommit, zCommit);
522 }else{
523 pDb->zCommit = 0;
524 }
525 if( pDb->zCommit ){
526 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000527 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
drhaa940ea2004-01-15 02:44:03 +0000528 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000529 sqlite3_commit_hook(pDb->db, 0, 0);
drhaa940ea2004-01-15 02:44:03 +0000530 }
531 }
532 break;
533 }
534
drh0f14e2e2004-06-29 12:39:08 +0000535 /*
536 ** $db collate NAME SCRIPT
537 **
538 ** Create a new SQL collation function called NAME. Whenever
539 ** that function is called, invoke SCRIPT to evaluate the function.
540 */
541 case DB_COLLATE: {
542 SqlCollate *pCollate;
543 char *zName;
544 char *zScript;
545 int nScript;
546 if( objc!=4 ){
547 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
548 return TCL_ERROR;
549 }
550 zName = Tcl_GetStringFromObj(objv[2], 0);
551 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
552 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
553 if( pCollate==0 ) return TCL_ERROR;
554 pCollate->interp = interp;
555 pCollate->pNext = pDb->pCollate;
556 pCollate->zScript = (char*)&pCollate[1];
557 pDb->pCollate = pCollate;
558 strcpy(pCollate->zScript, zScript);
559 if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
560 pCollate, tclSqlCollate) ){
561 return TCL_ERROR;
562 }
563 break;
564 }
565
566 /*
567 ** $db collation_needed SCRIPT
568 **
569 ** Create a new SQL collation function called NAME. Whenever
570 ** that function is called, invoke SCRIPT to evaluate the function.
571 */
572 case DB_COLLATION_NEEDED: {
573 if( objc!=3 ){
574 Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT");
575 return TCL_ERROR;
576 }
577 if( pDb->pCollateNeeded ){
578 Tcl_DecrRefCount(pDb->pCollateNeeded);
579 }
580 pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]);
581 Tcl_IncrRefCount(pDb->pCollateNeeded);
582 sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded);
583 break;
584 }
585
drh75897232000-05-29 14:26:00 +0000586 /* $db complete SQL
587 **
588 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
589 ** additional lines of input are needed. This is similar to the
590 ** built-in "info complete" command of Tcl.
591 */
drh6d313162000-09-21 13:01:35 +0000592 case DB_COMPLETE: {
593 Tcl_Obj *pResult;
594 int isComplete;
595 if( objc!=3 ){
596 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000597 return TCL_ERROR;
598 }
danielk19776f8a5032004-05-10 10:34:51 +0000599 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
drh6d313162000-09-21 13:01:35 +0000600 pResult = Tcl_GetObjResult(interp);
601 Tcl_SetBooleanObj(pResult, isComplete);
602 break;
603 }
drhdcd997e2003-01-31 17:21:49 +0000604
605 /*
606 ** $db errorcode
607 **
608 ** Return the numeric error code that was returned by the most recent
danielk19776f8a5032004-05-10 10:34:51 +0000609 ** call to sqlite3_exec().
drhdcd997e2003-01-31 17:21:49 +0000610 */
611 case DB_ERRORCODE: {
danielk1977f3ce83f2004-06-14 11:43:46 +0000612 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
drhdcd997e2003-01-31 17:21:49 +0000613 break;
614 }
drh75897232000-05-29 14:26:00 +0000615
616 /*
drh895d7472004-08-20 16:02:39 +0000617 ** $db eval $sql ?array? ?{ ...code... }?
drh1807ce32004-09-07 13:20:35 +0000618 ** $db onecolumn $sql
drh75897232000-05-29 14:26:00 +0000619 **
620 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000621 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000622 ** If "array" and "code" are omitted, then no callback is every invoked.
623 ** If "array" is an empty string, then the values are placed in variables
624 ** that have the same name as the fields extracted by the query.
drh1807ce32004-09-07 13:20:35 +0000625 **
626 ** The onecolumn method is the equivalent of:
627 ** lindex [$db eval $sql] 0
drh75897232000-05-29 14:26:00 +0000628 */
drh1807ce32004-09-07 13:20:35 +0000629 case DB_ONECOLUMN:
drh6d313162000-09-21 13:01:35 +0000630 case DB_EVAL: {
drh9d74b4c2004-08-24 15:23:34 +0000631 char const *zSql; /* Next SQL statement to execute */
632 char const *zLeft; /* What is left after first stmt in zSql */
633 sqlite3_stmt *pStmt; /* Compiled SQL statment */
drh92febd92004-08-20 18:34:20 +0000634 Tcl_Obj *pArray; /* Name of array into which results are written */
635 Tcl_Obj *pScript; /* Script to run for each result set */
drh1d895032004-08-26 00:56:05 +0000636 Tcl_Obj **apParm; /* Parameters that need a Tcl_DecrRefCount() */
637 int nParm; /* Number of entries used in apParm[] */
638 Tcl_Obj *aParm[10]; /* Static space for apParm[] in the common case */
drh1807ce32004-09-07 13:20:35 +0000639 Tcl_Obj *pRet; /* Value to be returned */
danielk1977ef2cb632004-05-29 02:37:19 +0000640
drh1807ce32004-09-07 13:20:35 +0000641 if( choice==DB_ONECOLUMN ){
642 if( objc!=3 ){
643 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
644 return TCL_ERROR;
645 }
646 pRet = 0;
647 }else{
648 if( objc<3 || objc>5 ){
649 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?");
650 return TCL_ERROR;
651 }
652 pRet = Tcl_NewObj();
653 Tcl_IncrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000654 }
drh92febd92004-08-20 18:34:20 +0000655 if( objc==3 ){
656 pArray = pScript = 0;
657 }else if( objc==4 ){
658 pArray = 0;
659 pScript = objv[3];
660 }else{
661 pArray = objv[3];
662 if( Tcl_GetString(pArray)[0]==0 ) pArray = 0;
663 pScript = objv[4];
664 }
danielk197730ccda12004-05-27 12:11:31 +0000665
drh1d895032004-08-26 00:56:05 +0000666 Tcl_IncrRefCount(objv[2]);
danielk197730ccda12004-05-27 12:11:31 +0000667 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh90b6bb12004-09-13 13:16:31 +0000668 while( rc==TCL_OK && zSql[0] ){
drh92febd92004-08-20 18:34:20 +0000669 int i; /* Loop counter */
670 int nVar; /* Number of wildcards in the SQL */
671 int nCol; /* Number of columns in the result set */
672 Tcl_Obj **apColName = 0; /* Array of column names */
danielk197730ccda12004-05-27 12:11:31 +0000673
drh92febd92004-08-20 18:34:20 +0000674 /* Compile a single SQL statement */
danielk197730ccda12004-05-27 12:11:31 +0000675 if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000676 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000677 rc = TCL_ERROR;
678 break;
679 }
drh92febd92004-08-20 18:34:20 +0000680 if( pStmt==0 ){
681 if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
682 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
683 rc = TCL_ERROR;
684 break;
685 }else{
686 zSql = zLeft;
687 continue;
danielk197730ccda12004-05-27 12:11:31 +0000688 }
danielk197730ccda12004-05-27 12:11:31 +0000689 }
690
drh1d895032004-08-26 00:56:05 +0000691 /* Bind values to wildcards that begin with $ or : */
drh92febd92004-08-20 18:34:20 +0000692 nVar = sqlite3_bind_parameter_count(pStmt);
drh1d895032004-08-26 00:56:05 +0000693 nParm = 0;
694 if( nVar>sizeof(aParm)/sizeof(aParm[0]) ){
695 apParm = (Tcl_Obj**)Tcl_Alloc(nVar*sizeof(apParm[0]));
696 }else{
697 apParm = aParm;
698 }
drh92febd92004-08-20 18:34:20 +0000699 for(i=1; i<=nVar; i++){
700 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
drh2c6674c2004-08-25 04:07:01 +0000701 if( zVar[0]=='$' || zVar[0]==':' ){
drh92febd92004-08-20 18:34:20 +0000702 Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0);
703 if( pVar ){
704 int n;
705 u8 *data;
706 char *zType = pVar->typePtr ? pVar->typePtr->name : "";
707 char c = zType[0];
708 if( c=='b' && strcmp(zType,"bytearray")==0 ){
709 data = Tcl_GetByteArrayFromObj(pVar, &n);
710 sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC);
drh1d895032004-08-26 00:56:05 +0000711 Tcl_IncrRefCount(pVar);
712 apParm[nParm++] = pVar;
drh92febd92004-08-20 18:34:20 +0000713 }else if( (c=='b' && strcmp(zType,"boolean")==0) ||
714 (c=='i' && strcmp(zType,"int")==0) ){
715 Tcl_GetIntFromObj(interp, pVar, &n);
716 sqlite3_bind_int(pStmt, i, n);
717 }else if( c=='d' && strcmp(zType,"double")==0 ){
718 double r;
719 Tcl_GetDoubleFromObj(interp, pVar, &r);
720 sqlite3_bind_double(pStmt, i, r);
721 }else{
722 data = Tcl_GetStringFromObj(pVar, &n);
723 sqlite3_bind_text(pStmt, i, data, n, SQLITE_STATIC);
drh1d895032004-08-26 00:56:05 +0000724 Tcl_IncrRefCount(pVar);
725 apParm[nParm++] = pVar;
drh92febd92004-08-20 18:34:20 +0000726 }
727 }
728 }
729 }
drh92febd92004-08-20 18:34:20 +0000730
731 /* Compute column names */
732 nCol = sqlite3_column_count(pStmt);
733 if( pScript ){
734 apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol );
735 if( apColName==0 ) break;
736 for(i=0; i<nCol; i++){
737 apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i));
738 Tcl_IncrRefCount(apColName[i]);
739 }
740 }
741
742 /* If results are being stored in an array variable, then create
743 ** the array(*) entry for that array
744 */
745 if( pArray ){
746 Tcl_Obj *pColList = Tcl_NewObj();
747 Tcl_IncrRefCount(pColList);
748 for(i=0; i<nCol; i++){
749 Tcl_ListObjAppendElement(interp, pColList, apColName[i]);
750 }
751 Tcl_ObjSetVar2(interp, pArray, Tcl_NewStringObj("*",-1), pColList,0);
752 }
753
754 /* Execute the SQL
755 */
drh90b6bb12004-09-13 13:16:31 +0000756 while( rc==TCL_OK && pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
drh92febd92004-08-20 18:34:20 +0000757 for(i=0; i<nCol; i++){
danielk197730ccda12004-05-27 12:11:31 +0000758 Tcl_Obj *pVal;
759
760 /* Set pVal to contain the i'th column of this row. */
drh92febd92004-08-20 18:34:20 +0000761 switch( sqlite3_column_type(pStmt, i) ){
762 case SQLITE_BLOB: {
763 int bytes = sqlite3_column_bytes(pStmt, i);
764 pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes);
765 break;
766 }
767 case SQLITE_INTEGER: {
768 sqlite_int64 v = sqlite3_column_int64(pStmt, i);
769 if( v>=-2147483647 && v<=2147483647 ){
770 pVal = Tcl_NewIntObj(v);
771 }else{
772 pVal = Tcl_NewWideIntObj(v);
773 }
774 break;
775 }
776 case SQLITE_FLOAT: {
777 double r = sqlite3_column_double(pStmt, i);
778 pVal = Tcl_NewDoubleObj(r);
779 break;
780 }
781 default: {
782 pVal = dbTextToObj(sqlite3_column_text(pStmt, i));
783 break;
784 }
danielk197730ccda12004-05-27 12:11:31 +0000785 }
786
drh92febd92004-08-20 18:34:20 +0000787 if( pScript ){
788 if( pArray==0 ){
789 Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0);
danielk197730ccda12004-05-27 12:11:31 +0000790 }else{
drh92febd92004-08-20 18:34:20 +0000791 Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0);
danielk197730ccda12004-05-27 12:11:31 +0000792 }
drh1807ce32004-09-07 13:20:35 +0000793 }else if( choice==DB_ONECOLUMN ){
794 if( pRet==0 ){
795 pRet = pVal;
796 Tcl_IncrRefCount(pRet);
797 }
drh90b6bb12004-09-13 13:16:31 +0000798 rc = TCL_BREAK;
danielk197730ccda12004-05-27 12:11:31 +0000799 }else{
danielk197730ccda12004-05-27 12:11:31 +0000800 Tcl_ListObjAppendElement(interp, pRet, pVal);
801 }
802 }
803
drh92febd92004-08-20 18:34:20 +0000804 if( pScript ){
805 rc = Tcl_EvalObjEx(interp, pScript, 0);
drh90b6bb12004-09-13 13:16:31 +0000806 if( rc==TCL_CONTINUE ){
807 rc = TCL_OK;
808 }
danielk197730ccda12004-05-27 12:11:31 +0000809 }
810 }
drh90b6bb12004-09-13 13:16:31 +0000811 if( rc==TCL_BREAK ){
812 rc = TCL_OK;
813 }
drh92febd92004-08-20 18:34:20 +0000814
815 /* Free the column name objects */
816 if( pScript ){
817 for(i=0; i<nCol; i++){
818 Tcl_DecrRefCount(apColName[i]);
819 }
820 Tcl_Free((char*)apColName);
821 }
822
drh1d895032004-08-26 00:56:05 +0000823 /* Free the bound string and blob parameters */
824 for(i=0; i<nParm; i++){
825 Tcl_DecrRefCount(apParm[i]);
826 }
827 if( apParm!=aParm ){
828 Tcl_Free((char*)apParm);
829 }
830
drh92febd92004-08-20 18:34:20 +0000831 /* Finalize the statement. If the result code is SQLITE_SCHEMA, then
832 ** try again to execute the same statement
833 */
834 if( SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){
danielk197730ccda12004-05-27 12:11:31 +0000835 continue;
836 }
837
drh92febd92004-08-20 18:34:20 +0000838 if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000839 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000840 rc = TCL_ERROR;
841 break;
842 }
843
danielk197730ccda12004-05-27 12:11:31 +0000844 zSql = zLeft;
845 }
drh1d895032004-08-26 00:56:05 +0000846 Tcl_DecrRefCount(objv[2]);
danielk197730ccda12004-05-27 12:11:31 +0000847
drh1807ce32004-09-07 13:20:35 +0000848 if( pRet ){
849 if( rc==TCL_OK ){
850 Tcl_SetObjResult(interp, pRet);
851 }
852 Tcl_DecrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000853 }
danielk197730ccda12004-05-27 12:11:31 +0000854 break;
855 }
drhbec3f402000-08-04 13:49:02 +0000856
857 /*
drhcabb0812002-09-14 13:47:32 +0000858 ** $db function NAME SCRIPT
859 **
860 ** Create a new SQL function called NAME. Whenever that function is
861 ** called, invoke SCRIPT to evaluate the function.
862 */
863 case DB_FUNCTION: {
864 SqlFunc *pFunc;
865 char *zName;
866 char *zScript;
867 int nScript;
868 if( objc!=4 ){
869 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
870 return TCL_ERROR;
871 }
872 zName = Tcl_GetStringFromObj(objv[2], 0);
873 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
874 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
875 if( pFunc==0 ) return TCL_ERROR;
876 pFunc->interp = interp;
877 pFunc->pNext = pDb->pFunc;
878 pFunc->zScript = (char*)&pFunc[1];
drh0f14e2e2004-06-29 12:39:08 +0000879 pDb->pFunc = pFunc;
drhcabb0812002-09-14 13:47:32 +0000880 strcpy(pFunc->zScript, zScript);
danielk1977c8c11582004-06-29 13:41:21 +0000881 rc = sqlite3_create_function(pDb->db, zName, -1, SQLITE_UTF8,
danielk1977d8123362004-06-12 09:25:12 +0000882 pFunc, tclSqlFunc, 0, 0);
danielk1977c8c11582004-06-29 13:41:21 +0000883 if( rc!=SQLITE_OK ) rc = TCL_ERROR;
drhcabb0812002-09-14 13:47:32 +0000884 break;
885 }
886
887 /*
drhaf9ff332002-01-16 21:00:27 +0000888 ** $db last_insert_rowid
889 **
890 ** Return an integer which is the ROWID for the most recent insert.
891 */
892 case DB_LAST_INSERT_ROWID: {
893 Tcl_Obj *pResult;
894 int rowid;
895 if( objc!=2 ){
896 Tcl_WrongNumArgs(interp, 2, objv, "");
897 return TCL_ERROR;
898 }
danielk19776f8a5032004-05-10 10:34:51 +0000899 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +0000900 pResult = Tcl_GetObjResult(interp);
901 Tcl_SetIntObj(pResult, rowid);
902 break;
903 }
904
905 /*
drh1807ce32004-09-07 13:20:35 +0000906 ** The DB_ONECOLUMN method is implemented together with DB_EVAL.
drh5d9d7572003-08-19 14:31:01 +0000907 */
drh1807ce32004-09-07 13:20:35 +0000908
909 /* $db progress ?N CALLBACK?
910 **
911 ** Invoke the given callback every N virtual machine opcodes while executing
912 ** queries.
913 */
914 case DB_PROGRESS: {
915 if( objc==2 ){
916 if( pDb->zProgress ){
917 Tcl_AppendResult(interp, pDb->zProgress, 0);
918 }
919 }else if( objc==4 ){
920 char *zProgress;
921 int len;
922 int N;
923 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
924 return TCL_ERROR;
925 };
926 if( pDb->zProgress ){
927 Tcl_Free(pDb->zProgress);
928 }
929 zProgress = Tcl_GetStringFromObj(objv[3], &len);
930 if( zProgress && len>0 ){
931 pDb->zProgress = Tcl_Alloc( len + 1 );
932 strcpy(pDb->zProgress, zProgress);
933 }else{
934 pDb->zProgress = 0;
935 }
936#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
937 if( pDb->zProgress ){
938 pDb->interp = interp;
939 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
940 }else{
941 sqlite3_progress_handler(pDb->db, 0, 0, 0);
942 }
943#endif
944 }else{
945 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
drh5d9d7572003-08-19 14:31:01 +0000946 return TCL_ERROR;
947 }
drh5d9d7572003-08-19 14:31:01 +0000948 break;
949 }
950
951 /*
drh22fbcb82004-02-01 01:22:50 +0000952 ** $db rekey KEY
953 **
954 ** Change the encryption key on the currently open database.
955 */
956 case DB_REKEY: {
957 int nKey;
958 void *pKey;
959 if( objc!=3 ){
960 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
961 return TCL_ERROR;
962 }
963 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +0000964#ifdef SQLITE_HAS_CODEC
drh2011d5f2004-07-22 02:40:37 +0000965 rc = sqlite3_rekey(pDb->db, pKey, nKey);
drh22fbcb82004-02-01 01:22:50 +0000966 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000967 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh22fbcb82004-02-01 01:22:50 +0000968 rc = TCL_ERROR;
969 }
970#endif
971 break;
972 }
973
974 /*
drhbec3f402000-08-04 13:49:02 +0000975 ** $db timeout MILLESECONDS
976 **
977 ** Delay for the number of milliseconds specified when a file is locked.
978 */
drh6d313162000-09-21 13:01:35 +0000979 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000980 int ms;
drh6d313162000-09-21 13:01:35 +0000981 if( objc!=3 ){
982 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000983 return TCL_ERROR;
984 }
drh6d313162000-09-21 13:01:35 +0000985 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +0000986 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000987 break;
drh75897232000-05-29 14:26:00 +0000988 }
drhb5a20d32003-04-23 12:25:23 +0000989
drh0f14e2e2004-06-29 12:39:08 +0000990 /*
991 ** $db total_changes
992 **
993 ** Return the number of rows that were modified, inserted, or deleted
994 ** since the database handle was created.
995 */
996 case DB_TOTAL_CHANGES: {
997 Tcl_Obj *pResult;
998 if( objc!=2 ){
999 Tcl_WrongNumArgs(interp, 2, objv, "");
1000 return TCL_ERROR;
1001 }
1002 pResult = Tcl_GetObjResult(interp);
1003 Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db));
1004 break;
1005 }
1006
drhb5a20d32003-04-23 12:25:23 +00001007 /* $db trace ?CALLBACK?
1008 **
1009 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
1010 ** that is executed. The text of the SQL is appended to CALLBACK before
1011 ** it is executed.
1012 */
1013 case DB_TRACE: {
1014 if( objc>3 ){
1015 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drhb97759e2004-06-29 11:26:59 +00001016 return TCL_ERROR;
drhb5a20d32003-04-23 12:25:23 +00001017 }else if( objc==2 ){
1018 if( pDb->zTrace ){
1019 Tcl_AppendResult(interp, pDb->zTrace, 0);
1020 }
1021 }else{
1022 char *zTrace;
1023 int len;
1024 if( pDb->zTrace ){
1025 Tcl_Free(pDb->zTrace);
1026 }
1027 zTrace = Tcl_GetStringFromObj(objv[2], &len);
1028 if( zTrace && len>0 ){
1029 pDb->zTrace = Tcl_Alloc( len + 1 );
1030 strcpy(pDb->zTrace, zTrace);
1031 }else{
1032 pDb->zTrace = 0;
1033 }
1034 if( pDb->zTrace ){
1035 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +00001036 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +00001037 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001038 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +00001039 }
1040 }
1041 break;
1042 }
1043
drh6d313162000-09-21 13:01:35 +00001044 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +00001045 return rc;
drh75897232000-05-29 14:26:00 +00001046}
1047
1048/*
drh9bb575f2004-09-06 17:24:11 +00001049** sqlite3 DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +00001050**
1051** This is the main Tcl command. When the "sqlite" Tcl command is
1052** invoked, this routine runs to process that command.
1053**
1054** The first argument, DBNAME, is an arbitrary name for a new
1055** database connection. This command creates a new command named
1056** DBNAME that is used to control that connection. The database
1057** connection is deleted when the DBNAME command is deleted.
1058**
1059** The second argument is the name of the directory that contains
1060** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +00001061**
1062** For testing purposes, we also support the following:
1063**
drh9bb575f2004-09-06 17:24:11 +00001064** sqlite3 -encoding
drhfbc3eab2001-04-06 16:13:42 +00001065**
1066** Return the encoding used by LIKE and GLOB operators. Choices
1067** are UTF-8 and iso8859.
1068**
drh9bb575f2004-09-06 17:24:11 +00001069** sqlite3 -version
drh647cb0e2002-11-04 19:32:25 +00001070**
1071** Return the version number of the SQLite library.
1072**
drh9bb575f2004-09-06 17:24:11 +00001073** sqlite3 -tcl-uses-utf
drhfbc3eab2001-04-06 16:13:42 +00001074**
1075** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
1076** not. Used by tests to make sure the library was compiled
1077** correctly.
drh75897232000-05-29 14:26:00 +00001078*/
drh22fbcb82004-02-01 01:22:50 +00001079static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +00001080 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +00001081 void *pKey = 0;
1082 int nKey = 0;
1083 const char *zArg;
drh75897232000-05-29 14:26:00 +00001084 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +00001085 const char *zFile;
drh06b27182002-06-26 20:06:05 +00001086 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +00001087 if( objc==2 ){
1088 zArg = Tcl_GetStringFromObj(objv[1], 0);
drh22fbcb82004-02-01 01:22:50 +00001089 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001090 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +00001091 return TCL_OK;
1092 }
drh9eb9e262004-02-11 02:18:05 +00001093 if( strcmp(zArg,"-has-codec")==0 ){
1094#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001095 Tcl_AppendResult(interp,"1",0);
1096#else
1097 Tcl_AppendResult(interp,"0",0);
1098#endif
1099 return TCL_OK;
1100 }
1101 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +00001102#ifdef TCL_UTF_MAX
1103 Tcl_AppendResult(interp,"1",0);
1104#else
1105 Tcl_AppendResult(interp,"0",0);
1106#endif
1107 return TCL_OK;
1108 }
1109 }
drh22fbcb82004-02-01 01:22:50 +00001110 if( objc==5 || objc==6 ){
1111 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
1112 if( strcmp(zArg,"-key")==0 ){
1113 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
1114 objc -= 2;
1115 }
1116 }
1117 if( objc!=3 && objc!=4 ){
1118 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +00001119#ifdef SQLITE_HAS_CODEC
1120 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +00001121#else
1122 "HANDLE FILENAME ?MODE?"
1123#endif
1124 );
drh75897232000-05-29 14:26:00 +00001125 return TCL_ERROR;
1126 }
drh75897232000-05-29 14:26:00 +00001127 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001128 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001129 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001130 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1131 return TCL_ERROR;
1132 }
1133 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00001134 zFile = Tcl_GetStringFromObj(objv[2], 0);
danielk19774f057f92004-06-08 00:02:33 +00001135 sqlite3_open(zFile, &p->db);
danielk197780290862004-05-22 09:21:21 +00001136 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
1137 zErrMsg = strdup(sqlite3_errmsg(p->db));
1138 sqlite3_close(p->db);
1139 p->db = 0;
1140 }
drh2011d5f2004-07-22 02:40:37 +00001141#ifdef SQLITE_HAS_CODEC
1142 sqlite3_key(p->db, pKey, nKey);
drheb8ed702004-02-11 10:37:23 +00001143#endif
drhbec3f402000-08-04 13:49:02 +00001144 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001145 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001146 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001147 free(zErrMsg);
1148 return TCL_ERROR;
1149 }
drh22fbcb82004-02-01 01:22:50 +00001150 zArg = Tcl_GetStringFromObj(objv[1], 0);
1151 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001152
drh06b27182002-06-26 20:06:05 +00001153 /* The return value is the value of the sqlite* pointer
1154 */
1155 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001156 if( strncmp(zBuf,"0x",2) ){
1157 sprintf(zBuf, "0x%p", p->db);
1158 }
drh06b27182002-06-26 20:06:05 +00001159 Tcl_AppendResult(interp, zBuf, 0);
1160
drhc22bd472002-05-10 13:14:07 +00001161 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001162 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001163 */
drh28b4e482002-03-11 02:06:13 +00001164#ifdef SQLITE_TEST
1165 {
drh9bb575f2004-09-06 17:24:11 +00001166 extern void Md5_Register(sqlite3*);
danielk19775b59af82004-06-30 12:42:59 +00001167#ifdef SQLITE_DEBUG
danielk197713073932004-06-30 11:54:06 +00001168 int mallocfail = sqlite3_iMallocFail;
1169 sqlite3_iMallocFail = 0;
danielk19775b59af82004-06-30 12:42:59 +00001170#endif
drhc22bd472002-05-10 13:14:07 +00001171 Md5_Register(p->db);
danielk19775b59af82004-06-30 12:42:59 +00001172#ifdef SQLITE_DEBUG
danielk197713073932004-06-30 11:54:06 +00001173 sqlite3_iMallocFail = mallocfail;
danielk19775b59af82004-06-30 12:42:59 +00001174#endif
drh06b27182002-06-26 20:06:05 +00001175 }
drh28b4e482002-03-11 02:06:13 +00001176#endif
danielk19777cedc8d2004-06-10 10:50:08 +00001177 p->interp = interp;
drh75897232000-05-29 14:26:00 +00001178 return TCL_OK;
1179}
1180
1181/*
drh90ca9752001-09-28 17:47:14 +00001182** Provide a dummy Tcl_InitStubs if we are using this as a static
1183** library.
1184*/
1185#ifndef USE_TCL_STUBS
1186# undef Tcl_InitStubs
1187# define Tcl_InitStubs(a,b,c)
1188#endif
1189
1190/*
drh75897232000-05-29 14:26:00 +00001191** Initialize this module.
1192**
1193** This Tcl module contains only a single new Tcl command named "sqlite".
1194** (Hence there is no namespace. There is no point in using a namespace
1195** if the extension only supplies one new name!) The "sqlite" command is
1196** used to open a new SQLite database. See the DbMain() routine above
1197** for additional information.
1198*/
drh38f82712004-06-18 17:10:16 +00001199int Sqlite3_Init(Tcl_Interp *interp){
drh92febd92004-08-20 18:34:20 +00001200 Tcl_InitStubs(interp, "8.4", 0);
drhef4ac8f2004-06-19 00:16:31 +00001201 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1202 Tcl_PkgProvide(interp, "sqlite3", "3.0");
drh90ca9752001-09-28 17:47:14 +00001203 return TCL_OK;
1204}
drh38f82712004-06-18 17:10:16 +00001205int Tclsqlite3_Init(Tcl_Interp *interp){
drh92febd92004-08-20 18:34:20 +00001206 Tcl_InitStubs(interp, "8.4", 0);
drhef4ac8f2004-06-19 00:16:31 +00001207 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1208 Tcl_PkgProvide(interp, "sqlite3", "3.0");
drh75897232000-05-29 14:26:00 +00001209 return TCL_OK;
1210}
drh38f82712004-06-18 17:10:16 +00001211int Sqlite3_SafeInit(Tcl_Interp *interp){
drh75897232000-05-29 14:26:00 +00001212 return TCL_OK;
1213}
drh38f82712004-06-18 17:10:16 +00001214int Tclsqlite3_SafeInit(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001215 return TCL_OK;
1216}
drh75897232000-05-29 14:26:00 +00001217
drh3e27c022004-07-23 00:01:38 +00001218#ifdef TCLSH
1219/*****************************************************************************
1220** The code that follows is used to build standalone TCL interpreters
drh75897232000-05-29 14:26:00 +00001221*/
drh348784e2000-05-29 20:41:49 +00001222
1223/*
drh3e27c022004-07-23 00:01:38 +00001224** If the macro TCLSH is one, then put in code this for the
1225** "main" routine that will initialize Tcl and take input from
1226** standard input.
drh348784e2000-05-29 20:41:49 +00001227*/
drh3e27c022004-07-23 00:01:38 +00001228#if TCLSH==1
drh348784e2000-05-29 20:41:49 +00001229static char zMainloop[] =
1230 "set line {}\n"
1231 "while {![eof stdin]} {\n"
1232 "if {$line!=\"\"} {\n"
1233 "puts -nonewline \"> \"\n"
1234 "} else {\n"
1235 "puts -nonewline \"% \"\n"
1236 "}\n"
1237 "flush stdout\n"
1238 "append line [gets stdin]\n"
1239 "if {[info complete $line]} {\n"
1240 "if {[catch {uplevel #0 $line} result]} {\n"
1241 "puts stderr \"Error: $result\"\n"
1242 "} elseif {$result!=\"\"} {\n"
1243 "puts $result\n"
1244 "}\n"
1245 "set line {}\n"
1246 "} else {\n"
1247 "append line \\n\n"
1248 "}\n"
1249 "}\n"
1250;
drh3e27c022004-07-23 00:01:38 +00001251#endif
1252
1253/*
1254** If the macro TCLSH is two, then get the main loop code out of
1255** the separate file "spaceanal_tcl.h".
1256*/
1257#if TCLSH==2
1258static char zMainloop[] =
1259#include "spaceanal_tcl.h"
1260;
1261#endif
drh348784e2000-05-29 20:41:49 +00001262
1263#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1264int TCLSH_MAIN(int argc, char **argv){
1265 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001266 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001267 interp = Tcl_CreateInterp();
drh38f82712004-06-18 17:10:16 +00001268 Sqlite3_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001269#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001270 {
1271 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001272 extern int Sqlitetest2_Init(Tcl_Interp*);
1273 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001274 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001275 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001276 extern int Md5_Init(Tcl_Interp*);
danielk19776490beb2004-05-11 06:17:21 +00001277 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001278 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001279 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00001280 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00001281 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001282 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001283 }
1284#endif
drh3e27c022004-07-23 00:01:38 +00001285 if( argc>=2 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00001286 int i;
1287 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1288 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1289 for(i=2; i<argc; i++){
1290 Tcl_SetVar(interp, "argv", argv[i],
1291 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1292 }
drh3e27c022004-07-23 00:01:38 +00001293 if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001294 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001295 if( zInfo==0 ) zInfo = interp->result;
1296 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001297 return 1;
1298 }
drh3e27c022004-07-23 00:01:38 +00001299 }
1300 if( argc<=1 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00001301 Tcl_GlobalEval(interp, zMainloop);
1302 }
1303 return 0;
1304}
1305#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001306
1307#endif /* !defined(NO_TCL) */