blob: 361aa59db45f865bb057f937b9f9a6c3f228322e [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**
danielk1977b28af712004-06-21 06:50:26 +000014** $Id: tclsqlite.c,v 1.90 2004/06/21 06:50:28 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000015*/
drh6d313162000-09-21 13:01:35 +000016#ifndef NO_TCL /* Omit this whole file if TCL is unavailable */
17
drh06b27182002-06-26 20:06:05 +000018#include "sqliteInt.h"
drh17a68932001-01-31 13:28:08 +000019#include "tcl.h"
drh75897232000-05-29 14:26:00 +000020#include <stdlib.h>
21#include <string.h>
drhce927062001-11-09 13:41:09 +000022#include <assert.h>
drh75897232000-05-29 14:26:00 +000023
24/*
drh98808ba2001-10-18 12:34:46 +000025** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
26** have to do a translation when going between the two. Set the
27** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
28** this translation.
29*/
30#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
31# define UTF_TRANSLATION_NEEDED 1
32#endif
33
34/*
drhcabb0812002-09-14 13:47:32 +000035** New SQL functions can be created as TCL scripts. Each such function
36** is described by an instance of the following structure.
37*/
38typedef struct SqlFunc SqlFunc;
39struct SqlFunc {
40 Tcl_Interp *interp; /* The TCL interpret to execute the function */
41 char *zScript; /* The script to be run */
42 SqlFunc *pNext; /* Next function on the list of them all */
43};
44
45/*
danielk19770202b292004-06-09 09:55:16 +000046** New collation sequences function can be created as TCL scripts. Each such
47** function is described by an instance of the following structure.
48*/
49typedef struct SqlCollate SqlCollate;
50struct SqlCollate {
51 Tcl_Interp *interp; /* The TCL interpret to execute the function */
52 char *zScript; /* The script to be run */
53 SqlCollate *pNext; /* Next function on the list of them all */
54};
55
56/*
drhbec3f402000-08-04 13:49:02 +000057** There is one instance of this structure for each SQLite database
58** that has been opened by the SQLite TCL interface.
59*/
60typedef struct SqliteDb SqliteDb;
61struct SqliteDb {
62 sqlite *db; /* The "real" database structure */
63 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000064 char *zBusy; /* The busy callback routine */
drhaa940ea2004-01-15 02:44:03 +000065 char *zCommit; /* The commit hook callback routine */
drhb5a20d32003-04-23 12:25:23 +000066 char *zTrace; /* The trace callback routine */
danielk1977348bb5d2003-10-18 09:37:26 +000067 char *zProgress; /* The progress callback routine */
drhe22a3342003-04-22 20:30:37 +000068 char *zAuth; /* The authorization callback routine */
drhcabb0812002-09-14 13:47:32 +000069 SqlFunc *pFunc; /* List of SQL functions */
danielk19770202b292004-06-09 09:55:16 +000070 SqlCollate *pCollate; /* List of SQL collation functions */
danielk19776f8a5032004-05-10 10:34:51 +000071 int rc; /* Return code of most recent sqlite3_exec() */
danielk19777cedc8d2004-06-10 10:50:08 +000072 Tcl_Obj *pCollateNeeded; /* Collation needed script */
drhbec3f402000-08-04 13:49:02 +000073};
74
75/*
drh75897232000-05-29 14:26:00 +000076** An instance of this structure passes information thru the sqlite
77** logic from the original TCL command into the callback routine.
78*/
79typedef struct CallbackData CallbackData;
80struct CallbackData {
81 Tcl_Interp *interp; /* The TCL interpreter */
82 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000083 Tcl_Obj *pCode; /* The code to execute for each row */
drhce927062001-11-09 13:41:09 +000084 int once; /* Set for first callback only */
drh960e8c62001-04-03 16:53:21 +000085 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000086 int nColName; /* Number of entries in the azColName[] array */
87 char **azColName; /* Column names translated to UTF-8 */
drh98808ba2001-10-18 12:34:46 +000088};
drh297ecf12001-04-05 15:57:13 +000089
drh6d313162000-09-21 13:01:35 +000090/*
drh5d9d7572003-08-19 14:31:01 +000091** This is a second alternative callback for database queries. A the
92** first column of the first row of the result is made the TCL result.
93*/
94static int DbEvalCallback3(
95 void *clientData, /* An instance of CallbackData */
96 int nCol, /* Number of columns in the result */
97 char ** azCol, /* Data for each column */
98 char ** azN /* Name for each column */
99){
100 Tcl_Interp *interp = (Tcl_Interp*)clientData;
101 Tcl_Obj *pElem;
102 if( azCol==0 ) return 1;
103 if( nCol==0 ) return 1;
104#ifdef UTF_TRANSLATION_NEEDED
105 {
106 Tcl_DString dCol;
107 Tcl_DStringInit(&dCol);
108 Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol);
109 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
110 Tcl_DStringFree(&dCol);
111 }
112#else
113 pElem = Tcl_NewStringObj(azCol[0], -1);
114#endif
115 Tcl_SetObjResult(interp, pElem);
116 return 1;
117}
118
119/*
drh75897232000-05-29 14:26:00 +0000120** Called when the command is deleted.
121*/
122static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000123 SqliteDb *pDb = (SqliteDb*)db;
danielk19776f8a5032004-05-10 10:34:51 +0000124 sqlite3_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000125 while( pDb->pFunc ){
126 SqlFunc *pFunc = pDb->pFunc;
127 pDb->pFunc = pFunc->pNext;
128 Tcl_Free((char*)pFunc);
129 }
danielk19770202b292004-06-09 09:55:16 +0000130 while( pDb->pCollate ){
131 SqlCollate *pCollate = pDb->pCollate;
132 pDb->pCollate = pCollate->pNext;
133 Tcl_Free((char*)pCollate);
134 }
drhbec3f402000-08-04 13:49:02 +0000135 if( pDb->zBusy ){
136 Tcl_Free(pDb->zBusy);
137 }
drhb5a20d32003-04-23 12:25:23 +0000138 if( pDb->zTrace ){
139 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000140 }
drhe22a3342003-04-22 20:30:37 +0000141 if( pDb->zAuth ){
142 Tcl_Free(pDb->zAuth);
143 }
drhbec3f402000-08-04 13:49:02 +0000144 Tcl_Free((char*)pDb);
145}
146
147/*
148** This routine is called when a database file is locked while trying
149** to execute SQL.
150*/
danielk19772a764eb2004-06-12 01:43:26 +0000151static int DbBusyHandler(void *cd, int nTries){
drhbec3f402000-08-04 13:49:02 +0000152 SqliteDb *pDb = (SqliteDb*)cd;
153 int rc;
154 char zVal[30];
155 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000156 Tcl_DString cmd;
157
158 Tcl_DStringInit(&cmd);
159 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
danielk19772a764eb2004-06-12 01:43:26 +0000160 sprintf(zVal, "%d", nTries);
161 Tcl_DStringAppendElement(&cmd, zVal);
drhbec3f402000-08-04 13:49:02 +0000162 zCmd = Tcl_DStringValue(&cmd);
163 rc = Tcl_Eval(pDb->interp, zCmd);
164 Tcl_DStringFree(&cmd);
165 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
166 return 0;
167 }
168 return 1;
drh75897232000-05-29 14:26:00 +0000169}
170
171/*
danielk1977348bb5d2003-10-18 09:37:26 +0000172** This routine is invoked as the 'progress callback' for the database.
173*/
174static int DbProgressHandler(void *cd){
175 SqliteDb *pDb = (SqliteDb*)cd;
176 int rc;
177
178 assert( pDb->zProgress );
179 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
180 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
181 return 1;
182 }
183 return 0;
184}
185
186/*
drhb5a20d32003-04-23 12:25:23 +0000187** This routine is called by the SQLite trace handler whenever a new
188** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000189*/
drhb5a20d32003-04-23 12:25:23 +0000190static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000191 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000192 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000193
drhb5a20d32003-04-23 12:25:23 +0000194 Tcl_DStringInit(&str);
195 Tcl_DStringAppend(&str, pDb->zTrace, -1);
196 Tcl_DStringAppendElement(&str, zSql);
197 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
198 Tcl_DStringFree(&str);
199 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000200}
201
202/*
drhaa940ea2004-01-15 02:44:03 +0000203** This routine is called when a transaction is committed. The
204** TCL script in pDb->zCommit is executed. If it returns non-zero or
205** if it throws an exception, the transaction is rolled back instead
206** of being committed.
207*/
208static int DbCommitHandler(void *cd){
209 SqliteDb *pDb = (SqliteDb*)cd;
210 int rc;
211
212 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
213 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
214 return 1;
215 }
216 return 0;
217}
218
danielk19777cedc8d2004-06-10 10:50:08 +0000219static void tclCollateNeeded(
220 void *pCtx,
221 sqlite *db,
222 int enc,
223 const char *zName
224){
225 SqliteDb *pDb = (SqliteDb *)pCtx;
226 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
227 Tcl_IncrRefCount(pScript);
228 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
229 Tcl_EvalObjEx(pDb->interp, pScript, 0);
230 Tcl_DecrRefCount(pScript);
231}
232
drhaa940ea2004-01-15 02:44:03 +0000233/*
danielk19770202b292004-06-09 09:55:16 +0000234** This routine is called to evaluate an SQL collation function implemented
235** using TCL script.
236*/
237static int tclSqlCollate(
238 void *pCtx,
239 int nA,
240 const void *zA,
241 int nB,
242 const void *zB
243){
244 SqlCollate *p = (SqlCollate *)pCtx;
245 Tcl_Obj *pCmd;
246
247 pCmd = Tcl_NewStringObj(p->zScript, -1);
248 Tcl_IncrRefCount(pCmd);
249 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA));
250 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB));
251 Tcl_EvalObjEx(p->interp, pCmd, 0);
252 Tcl_DecrRefCount(pCmd);
253 return (atoi(Tcl_GetStringResult(p->interp)));
254}
255
256/*
drhcabb0812002-09-14 13:47:32 +0000257** This routine is called to evaluate an SQL function implemented
258** using TCL script.
259*/
danielk19770ae8b832004-05-25 12:05:56 +0000260static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk19776f8a5032004-05-10 10:34:51 +0000261 SqlFunc *p = sqlite3_user_data(context);
drhcabb0812002-09-14 13:47:32 +0000262 Tcl_DString cmd;
263 int i;
264 int rc;
265
266 Tcl_DStringInit(&cmd);
267 Tcl_DStringAppend(&cmd, p->zScript, -1);
268 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000269 if( SQLITE_NULL==sqlite3_value_type(argv[i]) ){
danielk197751ad0ec2004-05-24 12:39:02 +0000270 Tcl_DStringAppendElement(&cmd, "");
271 }else{
drh4f26d6c2004-05-26 23:25:30 +0000272 Tcl_DStringAppendElement(&cmd, sqlite3_value_text(argv[i]));
danielk197751ad0ec2004-05-24 12:39:02 +0000273 }
drhcabb0812002-09-14 13:47:32 +0000274 }
275 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
276 if( rc ){
danielk19777e18c252004-05-25 11:47:24 +0000277 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhcabb0812002-09-14 13:47:32 +0000278 }else{
danielk1977d8123362004-06-12 09:25:12 +0000279 sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1,
280 SQLITE_TRANSIENT);
drhcabb0812002-09-14 13:47:32 +0000281 }
282}
drhe22a3342003-04-22 20:30:37 +0000283#ifndef SQLITE_OMIT_AUTHORIZATION
284/*
285** This is the authentication function. It appends the authentication
286** type code and the two arguments to zCmd[] then invokes the result
287** on the interpreter. The reply is examined to determine if the
288** authentication fails or succeeds.
289*/
290static int auth_callback(
291 void *pArg,
292 int code,
293 const char *zArg1,
294 const char *zArg2,
295 const char *zArg3,
296 const char *zArg4
297){
298 char *zCode;
299 Tcl_DString str;
300 int rc;
301 const char *zReply;
302 SqliteDb *pDb = (SqliteDb*)pArg;
303
304 switch( code ){
305 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
306 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
307 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
308 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
309 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
310 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
311 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
312 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
313 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
314 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
315 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
316 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
317 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
318 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
319 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
320 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
321 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
322 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
323 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
324 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
325 case SQLITE_READ : zCode="SQLITE_READ"; break;
326 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
327 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
328 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000329 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
330 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
drhe22a3342003-04-22 20:30:37 +0000331 default : zCode="????"; break;
332 }
333 Tcl_DStringInit(&str);
334 Tcl_DStringAppend(&str, pDb->zAuth, -1);
335 Tcl_DStringAppendElement(&str, zCode);
336 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
337 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
338 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
339 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
340 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
341 Tcl_DStringFree(&str);
342 zReply = Tcl_GetStringResult(pDb->interp);
343 if( strcmp(zReply,"SQLITE_OK")==0 ){
344 rc = SQLITE_OK;
345 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
346 rc = SQLITE_DENY;
347 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
348 rc = SQLITE_IGNORE;
349 }else{
350 rc = 999;
351 }
352 return rc;
353}
354#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000355
356/*
danielk1977ef2cb632004-05-29 02:37:19 +0000357** zText is a pointer to text obtained via an sqlite3_result_text()
358** or similar interface. This routine returns a Tcl string object,
359** reference count set to 0, containing the text. If a translation
360** between iso8859 and UTF-8 is required, it is preformed.
361*/
362static Tcl_Obj *dbTextToObj(char const *zText){
363 Tcl_Obj *pVal;
364#ifdef UTF_TRANSLATION_NEEDED
365 Tcl_DString dCol;
366 Tcl_DStringInit(&dCol);
367 Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol);
368 pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
369 Tcl_DStringFree(&dCol);
370#else
371 pVal = Tcl_NewStringObj(zText, -1);
372#endif
373 return pVal;
374}
375
376/*
drh75897232000-05-29 14:26:00 +0000377** The "sqlite" command below creates a new Tcl command for each
378** connection it opens to an SQLite database. This routine is invoked
379** whenever one of those connection-specific commands is executed
380** in Tcl. For example, if you run Tcl code like this:
381**
382** sqlite db1 "my_database"
383** db1 close
384**
385** The first command opens a connection to the "my_database" database
386** and calls that connection "db1". The second command causes this
387** subroutine to be invoked.
388*/
drh6d313162000-09-21 13:01:35 +0000389static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000390 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000391 int choice;
drh22fbcb82004-02-01 01:22:50 +0000392 int rc = TCL_OK;
drh0de8c112002-07-06 16:32:14 +0000393 static const char *DB_strs[] = {
rdcf146a772004-02-25 22:51:06 +0000394 "authorizer", "busy", "changes",
395 "close", "commit_hook", "complete",
396 "errorcode", "eval", "function",
danielk1977b28af712004-06-21 06:50:26 +0000397 "last_insert_rowid", "onecolumn",
rdcf146a772004-02-25 22:51:06 +0000398 "progress", "rekey", "timeout",
danielk19777cedc8d2004-06-10 10:50:08 +0000399 "trace", "collate", "collation_needed",
danielk1977b28af712004-06-21 06:50:26 +0000400 "total_changes", 0
drh6d313162000-09-21 13:01:35 +0000401 };
drh411995d2002-06-25 19:31:18 +0000402 enum DB_enum {
rdcf146a772004-02-25 22:51:06 +0000403 DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
404 DB_CLOSE, DB_COMMIT_HOOK, DB_COMPLETE,
405 DB_ERRORCODE, DB_EVAL, DB_FUNCTION,
danielk1977b28af712004-06-21 06:50:26 +0000406 DB_LAST_INSERT_ROWID, DB_ONECOLUMN,
rdcf146a772004-02-25 22:51:06 +0000407 DB_PROGRESS, DB_REKEY, DB_TIMEOUT,
danielk1977b28af712004-06-21 06:50:26 +0000408 DB_TRACE, DB_COLLATE, DB_COLLATION_NEEDED,
409 DB_TOTAL_CHANGES
drh6d313162000-09-21 13:01:35 +0000410 };
411
412 if( objc<2 ){
413 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000414 return TCL_ERROR;
415 }
drh411995d2002-06-25 19:31:18 +0000416 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000417 return TCL_ERROR;
418 }
419
drh411995d2002-06-25 19:31:18 +0000420 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000421
drhe22a3342003-04-22 20:30:37 +0000422 /* $db authorizer ?CALLBACK?
423 **
424 ** Invoke the given callback to authorize each SQL operation as it is
425 ** compiled. 5 arguments are appended to the callback before it is
426 ** invoked:
427 **
428 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
429 ** (2) First descriptive name (depends on authorization type)
430 ** (3) Second descriptive name
431 ** (4) Name of the database (ex: "main", "temp")
432 ** (5) Name of trigger that is doing the access
433 **
434 ** The callback should return on of the following strings: SQLITE_OK,
435 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
436 **
437 ** If this method is invoked with no arguments, the current authorization
438 ** callback string is returned.
439 */
440 case DB_AUTHORIZER: {
441 if( objc>3 ){
442 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
443 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000444 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000445 Tcl_AppendResult(interp, pDb->zAuth, 0);
446 }
447 }else{
448 char *zAuth;
449 int len;
450 if( pDb->zAuth ){
451 Tcl_Free(pDb->zAuth);
452 }
453 zAuth = Tcl_GetStringFromObj(objv[2], &len);
454 if( zAuth && len>0 ){
455 pDb->zAuth = Tcl_Alloc( len + 1 );
456 strcpy(pDb->zAuth, zAuth);
457 }else{
458 pDb->zAuth = 0;
459 }
460#ifndef SQLITE_OMIT_AUTHORIZATION
461 if( pDb->zAuth ){
462 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000463 sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
drhe22a3342003-04-22 20:30:37 +0000464 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000465 sqlite3_set_authorizer(pDb->db, 0, 0);
drhe22a3342003-04-22 20:30:37 +0000466 }
467#endif
468 }
469 break;
470 }
471
drhbec3f402000-08-04 13:49:02 +0000472 /* $db busy ?CALLBACK?
473 **
474 ** Invoke the given callback if an SQL statement attempts to open
475 ** a locked database file.
476 */
drh6d313162000-09-21 13:01:35 +0000477 case DB_BUSY: {
478 if( objc>3 ){
479 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000480 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000481 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000482 if( pDb->zBusy ){
483 Tcl_AppendResult(interp, pDb->zBusy, 0);
484 }
485 }else{
drh6d313162000-09-21 13:01:35 +0000486 char *zBusy;
487 int len;
drhbec3f402000-08-04 13:49:02 +0000488 if( pDb->zBusy ){
489 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000490 }
drh6d313162000-09-21 13:01:35 +0000491 zBusy = Tcl_GetStringFromObj(objv[2], &len);
492 if( zBusy && len>0 ){
493 pDb->zBusy = Tcl_Alloc( len + 1 );
494 strcpy(pDb->zBusy, zBusy);
495 }else{
496 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000497 }
498 if( pDb->zBusy ){
499 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000500 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000501 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000502 sqlite3_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000503 }
504 }
drh6d313162000-09-21 13:01:35 +0000505 break;
506 }
drhbec3f402000-08-04 13:49:02 +0000507
danielk1977348bb5d2003-10-18 09:37:26 +0000508 /* $db progress ?N CALLBACK?
509 **
510 ** Invoke the given callback every N virtual machine opcodes while executing
511 ** queries.
512 */
513 case DB_PROGRESS: {
514 if( objc==2 ){
515 if( pDb->zProgress ){
516 Tcl_AppendResult(interp, pDb->zProgress, 0);
517 }
518 }else if( objc==4 ){
519 char *zProgress;
520 int len;
521 int N;
522 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
523 return TCL_ERROR;
524 };
525 if( pDb->zProgress ){
526 Tcl_Free(pDb->zProgress);
527 }
528 zProgress = Tcl_GetStringFromObj(objv[3], &len);
529 if( zProgress && len>0 ){
530 pDb->zProgress = Tcl_Alloc( len + 1 );
531 strcpy(pDb->zProgress, zProgress);
532 }else{
533 pDb->zProgress = 0;
534 }
535#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
536 if( pDb->zProgress ){
537 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000538 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
danielk1977348bb5d2003-10-18 09:37:26 +0000539 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000540 sqlite3_progress_handler(pDb->db, 0, 0, 0);
danielk1977348bb5d2003-10-18 09:37:26 +0000541 }
542#endif
543 }else{
544 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
545 return TCL_ERROR;
546 }
547 break;
548 }
549
danielk1977b28af712004-06-21 06:50:26 +0000550 /* $db changes
drhc8d30ac2002-04-12 10:08:59 +0000551 **
552 ** Return the number of rows that were modified, inserted, or deleted by
danielk1977b28af712004-06-21 06:50:26 +0000553 ** the most recent INSERT, UPDATE or DELETE statement, not including
554 ** any changes made by trigger programs.
drhc8d30ac2002-04-12 10:08:59 +0000555 */
556 case DB_CHANGES: {
557 Tcl_Obj *pResult;
drhc8d30ac2002-04-12 10:08:59 +0000558 if( objc!=2 ){
559 Tcl_WrongNumArgs(interp, 2, objv, "");
560 return TCL_ERROR;
561 }
drhc8d30ac2002-04-12 10:08:59 +0000562 pResult = Tcl_GetObjResult(interp);
danielk1977b28af712004-06-21 06:50:26 +0000563 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db));
rdcf146a772004-02-25 22:51:06 +0000564 break;
565 }
566
drh75897232000-05-29 14:26:00 +0000567 /* $db close
568 **
569 ** Shutdown the database
570 */
drh6d313162000-09-21 13:01:35 +0000571 case DB_CLOSE: {
572 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
573 break;
574 }
drh75897232000-05-29 14:26:00 +0000575
drhaa940ea2004-01-15 02:44:03 +0000576 /* $db commit_hook ?CALLBACK?
577 **
578 ** Invoke the given callback just before committing every SQL transaction.
579 ** If the callback throws an exception or returns non-zero, then the
580 ** transaction is aborted. If CALLBACK is an empty string, the callback
581 ** is disabled.
582 */
583 case DB_COMMIT_HOOK: {
584 if( objc>3 ){
585 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
586 }else if( objc==2 ){
587 if( pDb->zCommit ){
588 Tcl_AppendResult(interp, pDb->zCommit, 0);
589 }
590 }else{
591 char *zCommit;
592 int len;
593 if( pDb->zCommit ){
594 Tcl_Free(pDb->zCommit);
595 }
596 zCommit = Tcl_GetStringFromObj(objv[2], &len);
597 if( zCommit && len>0 ){
598 pDb->zCommit = Tcl_Alloc( len + 1 );
599 strcpy(pDb->zCommit, zCommit);
600 }else{
601 pDb->zCommit = 0;
602 }
603 if( pDb->zCommit ){
604 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000605 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
drhaa940ea2004-01-15 02:44:03 +0000606 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000607 sqlite3_commit_hook(pDb->db, 0, 0);
drhaa940ea2004-01-15 02:44:03 +0000608 }
609 }
610 break;
611 }
612
drh75897232000-05-29 14:26:00 +0000613 /* $db complete SQL
614 **
615 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
616 ** additional lines of input are needed. This is similar to the
617 ** built-in "info complete" command of Tcl.
618 */
drh6d313162000-09-21 13:01:35 +0000619 case DB_COMPLETE: {
620 Tcl_Obj *pResult;
621 int isComplete;
622 if( objc!=3 ){
623 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000624 return TCL_ERROR;
625 }
danielk19776f8a5032004-05-10 10:34:51 +0000626 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
drh6d313162000-09-21 13:01:35 +0000627 pResult = Tcl_GetObjResult(interp);
628 Tcl_SetBooleanObj(pResult, isComplete);
629 break;
630 }
drhdcd997e2003-01-31 17:21:49 +0000631
632 /*
633 ** $db errorcode
634 **
635 ** Return the numeric error code that was returned by the most recent
danielk19776f8a5032004-05-10 10:34:51 +0000636 ** call to sqlite3_exec().
drhdcd997e2003-01-31 17:21:49 +0000637 */
638 case DB_ERRORCODE: {
danielk1977f3ce83f2004-06-14 11:43:46 +0000639 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
drhdcd997e2003-01-31 17:21:49 +0000640 break;
641 }
drh75897232000-05-29 14:26:00 +0000642
643 /*
644 ** $db eval $sql ?array { ...code... }?
645 **
646 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000647 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000648 ** If "array" and "code" are omitted, then no callback is every invoked.
649 ** If "array" is an empty string, then the values are placed in variables
650 ** that have the same name as the fields extracted by the query.
651 */
drh6d313162000-09-21 13:01:35 +0000652 case DB_EVAL: {
danielk197730ccda12004-05-27 12:11:31 +0000653 char const *zSql;
654 char const *zLeft;
655 sqlite3_stmt *pStmt;
danielk1977ef2cb632004-05-29 02:37:19 +0000656
657 Tcl_Obj *pRet = Tcl_NewObj();
658 Tcl_IncrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000659
660 if( objc!=5 && objc!=3 ){
661 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
662 return TCL_ERROR;
663 }
664
danielk197730ccda12004-05-27 12:11:31 +0000665 zSql = Tcl_GetStringFromObj(objv[2], 0);
666 while( zSql[0] ){
667 int i;
668
669 if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000670 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000671 rc = TCL_ERROR;
672 break;
673 }
674
675 if( pStmt && objc==5 ){
676 Tcl_Obj *pColList = Tcl_NewObj();
677 Tcl_IncrRefCount(pColList);
678
679 for(i=0; i<sqlite3_column_count(pStmt); i++){
680 Tcl_ListObjAppendElement(interp, pColList,
danielk1977ef2cb632004-05-29 02:37:19 +0000681 dbTextToObj(sqlite3_column_name(pStmt, i))
danielk197730ccda12004-05-27 12:11:31 +0000682 );
683 }
684 Tcl_ObjSetVar2(interp,objv[3],Tcl_NewStringObj("*",-1),pColList,0);
685 }
686
687 while( pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
688 for(i=0; i<sqlite3_column_count(pStmt); i++){
689 Tcl_Obj *pVal;
690
691 /* Set pVal to contain the i'th column of this row. */
drh9c054832004-05-31 18:51:57 +0000692 if( SQLITE_BLOB!=sqlite3_column_type(pStmt, i) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000693 pVal = dbTextToObj(sqlite3_column_text(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000694 }else{
danielk19773fd0a732004-05-27 13:35:19 +0000695 int bytes = sqlite3_column_bytes(pStmt, i);
696 pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes);
danielk197730ccda12004-05-27 12:11:31 +0000697 }
698
699 if( objc==5 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000700 Tcl_Obj *pName = dbTextToObj(sqlite3_column_name(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000701 Tcl_IncrRefCount(pName);
702 if( !strcmp("", Tcl_GetString(objv[3])) ){
703 Tcl_ObjSetVar2(interp, pName, 0, pVal, 0);
704 }else{
705 Tcl_ObjSetVar2(interp, objv[3], pName, pVal, 0);
706 }
707 Tcl_DecrRefCount(pName);
708 }else{
danielk197730ccda12004-05-27 12:11:31 +0000709 Tcl_ListObjAppendElement(interp, pRet, pVal);
710 }
711 }
712
713 if( objc==5 ){
714 rc = Tcl_EvalObjEx(interp, objv[4], 0);
715 if( rc!=TCL_ERROR ) rc = TCL_OK;
716 }
717 }
718
719 if( pStmt && SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){
720 continue;
721 }
722
723 if( pStmt && SQLITE_OK!=sqlite3_errcode(pDb->db) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000724 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000725 rc = TCL_ERROR;
726 break;
727 }
728
danielk197730ccda12004-05-27 12:11:31 +0000729 zSql = zLeft;
730 }
731
danielk1977ef2cb632004-05-29 02:37:19 +0000732 if( rc==TCL_OK ){
danielk197730ccda12004-05-27 12:11:31 +0000733 Tcl_SetObjResult(interp, pRet);
danielk197730ccda12004-05-27 12:11:31 +0000734 }
danielk1977ef2cb632004-05-29 02:37:19 +0000735 Tcl_DecrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000736
737 break;
738 }
drhbec3f402000-08-04 13:49:02 +0000739
740 /*
drhcabb0812002-09-14 13:47:32 +0000741 ** $db function NAME SCRIPT
742 **
743 ** Create a new SQL function called NAME. Whenever that function is
744 ** called, invoke SCRIPT to evaluate the function.
745 */
746 case DB_FUNCTION: {
747 SqlFunc *pFunc;
748 char *zName;
749 char *zScript;
750 int nScript;
751 if( objc!=4 ){
752 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
753 return TCL_ERROR;
754 }
755 zName = Tcl_GetStringFromObj(objv[2], 0);
756 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
757 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
758 if( pFunc==0 ) return TCL_ERROR;
759 pFunc->interp = interp;
760 pFunc->pNext = pDb->pFunc;
761 pFunc->zScript = (char*)&pFunc[1];
762 strcpy(pFunc->zScript, zScript);
danielk1977f9d64d22004-06-19 08:18:07 +0000763 sqlite3_create_function(pDb->db, zName, -1, SQLITE_UTF8,
danielk1977d8123362004-06-12 09:25:12 +0000764 pFunc, tclSqlFunc, 0, 0);
drhcabb0812002-09-14 13:47:32 +0000765 break;
766 }
767
768 /*
drhaf9ff332002-01-16 21:00:27 +0000769 ** $db last_insert_rowid
770 **
771 ** Return an integer which is the ROWID for the most recent insert.
772 */
773 case DB_LAST_INSERT_ROWID: {
774 Tcl_Obj *pResult;
775 int rowid;
776 if( objc!=2 ){
777 Tcl_WrongNumArgs(interp, 2, objv, "");
778 return TCL_ERROR;
779 }
danielk19776f8a5032004-05-10 10:34:51 +0000780 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +0000781 pResult = Tcl_GetObjResult(interp);
782 Tcl_SetIntObj(pResult, rowid);
783 break;
784 }
785
786 /*
drh5d9d7572003-08-19 14:31:01 +0000787 ** $db onecolumn SQL
788 **
789 ** Return a single column from a single row of the given SQL query.
790 */
791 case DB_ONECOLUMN: {
drh5d9d7572003-08-19 14:31:01 +0000792 char *zSql;
793 char *zErrMsg = 0;
794 if( objc!=3 ){
795 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
796 return TCL_ERROR;
797 }
798 zSql = Tcl_GetStringFromObj(objv[2], 0);
danielk19776f8a5032004-05-10 10:34:51 +0000799 rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
drh5d9d7572003-08-19 14:31:01 +0000800 if( rc==SQLITE_ABORT ){
drh22fbcb82004-02-01 01:22:50 +0000801 rc = SQLITE_OK;
drh5d9d7572003-08-19 14:31:01 +0000802 }else if( zErrMsg ){
803 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
804 free(zErrMsg);
805 rc = TCL_ERROR;
806 }else if( rc!=SQLITE_OK ){
danielk1977f20b21c2004-05-31 23:56:42 +0000807 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh5d9d7572003-08-19 14:31:01 +0000808 rc = TCL_ERROR;
809 }
810 break;
811 }
812
813 /*
drh22fbcb82004-02-01 01:22:50 +0000814 ** $db rekey KEY
815 **
816 ** Change the encryption key on the currently open database.
817 */
818 case DB_REKEY: {
819 int nKey;
820 void *pKey;
821 if( objc!=3 ){
822 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
823 return TCL_ERROR;
824 }
825 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +0000826#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000827 rc = sqlite_rekey(pDb->db, pKey, nKey);
828 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000829 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh22fbcb82004-02-01 01:22:50 +0000830 rc = TCL_ERROR;
831 }
832#endif
833 break;
834 }
835
836 /*
drhbec3f402000-08-04 13:49:02 +0000837 ** $db timeout MILLESECONDS
838 **
839 ** Delay for the number of milliseconds specified when a file is locked.
840 */
drh6d313162000-09-21 13:01:35 +0000841 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000842 int ms;
drh6d313162000-09-21 13:01:35 +0000843 if( objc!=3 ){
844 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000845 return TCL_ERROR;
846 }
drh6d313162000-09-21 13:01:35 +0000847 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +0000848 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000849 break;
drh75897232000-05-29 14:26:00 +0000850 }
drhb5a20d32003-04-23 12:25:23 +0000851
852 /* $db trace ?CALLBACK?
853 **
854 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
855 ** that is executed. The text of the SQL is appended to CALLBACK before
856 ** it is executed.
857 */
858 case DB_TRACE: {
859 if( objc>3 ){
860 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
861 }else if( objc==2 ){
862 if( pDb->zTrace ){
863 Tcl_AppendResult(interp, pDb->zTrace, 0);
864 }
865 }else{
866 char *zTrace;
867 int len;
868 if( pDb->zTrace ){
869 Tcl_Free(pDb->zTrace);
870 }
871 zTrace = Tcl_GetStringFromObj(objv[2], &len);
872 if( zTrace && len>0 ){
873 pDb->zTrace = Tcl_Alloc( len + 1 );
874 strcpy(pDb->zTrace, zTrace);
875 }else{
876 pDb->zTrace = 0;
877 }
878 if( pDb->zTrace ){
879 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000880 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +0000881 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000882 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +0000883 }
884 }
885 break;
886 }
887
danielk19770202b292004-06-09 09:55:16 +0000888 /*
889 ** $db collate NAME SCRIPT
890 **
891 ** Create a new SQL collation function called NAME. Whenever
892 ** that function is called, invoke SCRIPT to evaluate the function.
893 */
894 case DB_COLLATE: {
895 SqlCollate *pCollate;
896 char *zName;
897 char *zScript;
898 int nScript;
899 if( objc!=4 ){
900 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
901 return TCL_ERROR;
902 }
903 zName = Tcl_GetStringFromObj(objv[2], 0);
904 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
905 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
906 if( pCollate==0 ) return TCL_ERROR;
907 pCollate->interp = interp;
908 pCollate->pNext = pDb->pCollate;
909 pCollate->zScript = (char*)&pCollate[1];
910 strcpy(pCollate->zScript, zScript);
danielk1977466be562004-06-10 02:16:01 +0000911 if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
912 pCollate, tclSqlCollate) ){
danielk19770202b292004-06-09 09:55:16 +0000913 return TCL_ERROR;
914 }
915 break;
916 }
917
danielk19777cedc8d2004-06-10 10:50:08 +0000918 /*
919 ** $db collate_needed SCRIPT
920 **
921 ** Create a new SQL collation function called NAME. Whenever
922 ** that function is called, invoke SCRIPT to evaluate the function.
923 */
924 case DB_COLLATION_NEEDED: {
925 if( objc!=3 ){
926 Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT");
927 return TCL_ERROR;
928 }
929 if( pDb->pCollateNeeded ){
930 Tcl_DecrRefCount(pDb->pCollateNeeded);
931 }
932 pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]);
933 Tcl_IncrRefCount(pDb->pCollateNeeded);
934 sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded);
935 break;
936 }
937
danielk1977b28af712004-06-21 06:50:26 +0000938 /*
939 ** $db total_changes
940 **
941 ** Return the number of rows that were modified, inserted, or deleted
942 ** since the database handle was created.
943 */
944 case DB_TOTAL_CHANGES: {
945 Tcl_Obj *pResult;
946 if( objc!=2 ){
947 Tcl_WrongNumArgs(interp, 2, objv, "");
948 return TCL_ERROR;
949 }
950 pResult = Tcl_GetObjResult(interp);
951 Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db));
952 break;
953 }
954
drh6d313162000-09-21 13:01:35 +0000955 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +0000956 return rc;
drh75897232000-05-29 14:26:00 +0000957}
958
959/*
drh22fbcb82004-02-01 01:22:50 +0000960** sqlite DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +0000961**
962** This is the main Tcl command. When the "sqlite" Tcl command is
963** invoked, this routine runs to process that command.
964**
965** The first argument, DBNAME, is an arbitrary name for a new
966** database connection. This command creates a new command named
967** DBNAME that is used to control that connection. The database
968** connection is deleted when the DBNAME command is deleted.
969**
970** The second argument is the name of the directory that contains
971** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000972**
973** For testing purposes, we also support the following:
974**
975** sqlite -encoding
976**
977** Return the encoding used by LIKE and GLOB operators. Choices
978** are UTF-8 and iso8859.
979**
drh647cb0e2002-11-04 19:32:25 +0000980** sqlite -version
981**
982** Return the version number of the SQLite library.
983**
drhfbc3eab2001-04-06 16:13:42 +0000984** sqlite -tcl-uses-utf
985**
986** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
987** not. Used by tests to make sure the library was compiled
988** correctly.
drh75897232000-05-29 14:26:00 +0000989*/
drh22fbcb82004-02-01 01:22:50 +0000990static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000991 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +0000992 void *pKey = 0;
993 int nKey = 0;
994 const char *zArg;
drh75897232000-05-29 14:26:00 +0000995 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +0000996 const char *zFile;
drh06b27182002-06-26 20:06:05 +0000997 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +0000998 if( objc==2 ){
999 zArg = Tcl_GetStringFromObj(objv[1], 0);
drh22fbcb82004-02-01 01:22:50 +00001000 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001001 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +00001002 return TCL_OK;
1003 }
drh9eb9e262004-02-11 02:18:05 +00001004 if( strcmp(zArg,"-has-codec")==0 ){
1005#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001006 Tcl_AppendResult(interp,"1",0);
1007#else
1008 Tcl_AppendResult(interp,"0",0);
1009#endif
1010 return TCL_OK;
1011 }
1012 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +00001013#ifdef TCL_UTF_MAX
1014 Tcl_AppendResult(interp,"1",0);
1015#else
1016 Tcl_AppendResult(interp,"0",0);
1017#endif
1018 return TCL_OK;
1019 }
1020 }
drh22fbcb82004-02-01 01:22:50 +00001021 if( objc==5 || objc==6 ){
1022 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
1023 if( strcmp(zArg,"-key")==0 ){
1024 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
1025 objc -= 2;
1026 }
1027 }
1028 if( objc!=3 && objc!=4 ){
1029 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +00001030#ifdef SQLITE_HAS_CODEC
1031 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +00001032#else
1033 "HANDLE FILENAME ?MODE?"
1034#endif
1035 );
drh75897232000-05-29 14:26:00 +00001036 return TCL_ERROR;
1037 }
drh75897232000-05-29 14:26:00 +00001038 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001039 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001040 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001041 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1042 return TCL_ERROR;
1043 }
1044 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00001045 zFile = Tcl_GetStringFromObj(objv[2], 0);
drh9eb9e262004-02-11 02:18:05 +00001046#ifdef SQLITE_HAS_CODEC
danielk19776f8a5032004-05-10 10:34:51 +00001047 p->db = sqlite3_open_encrypted(zFile, pKey, nKey, 0, &zErrMsg);
drheb8ed702004-02-11 10:37:23 +00001048#else
danielk19774f057f92004-06-08 00:02:33 +00001049 sqlite3_open(zFile, &p->db);
danielk197780290862004-05-22 09:21:21 +00001050 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
1051 zErrMsg = strdup(sqlite3_errmsg(p->db));
1052 sqlite3_close(p->db);
1053 p->db = 0;
1054 }
drheb8ed702004-02-11 10:37:23 +00001055#endif
drhbec3f402000-08-04 13:49:02 +00001056 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001057 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001058 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001059 free(zErrMsg);
1060 return TCL_ERROR;
1061 }
drh22fbcb82004-02-01 01:22:50 +00001062 zArg = Tcl_GetStringFromObj(objv[1], 0);
1063 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001064
drh06b27182002-06-26 20:06:05 +00001065 /* The return value is the value of the sqlite* pointer
1066 */
1067 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001068 if( strncmp(zBuf,"0x",2) ){
1069 sprintf(zBuf, "0x%p", p->db);
1070 }
drh06b27182002-06-26 20:06:05 +00001071 Tcl_AppendResult(interp, zBuf, 0);
1072
drhc22bd472002-05-10 13:14:07 +00001073 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001074 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001075 */
drh28b4e482002-03-11 02:06:13 +00001076#ifdef SQLITE_TEST
1077 {
drhc22bd472002-05-10 13:14:07 +00001078 extern void Md5_Register(sqlite*);
1079 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +00001080 }
drh28b4e482002-03-11 02:06:13 +00001081#endif
danielk19777cedc8d2004-06-10 10:50:08 +00001082 p->interp = interp;
drh75897232000-05-29 14:26:00 +00001083 return TCL_OK;
1084}
1085
1086/*
drh90ca9752001-09-28 17:47:14 +00001087** Provide a dummy Tcl_InitStubs if we are using this as a static
1088** library.
1089*/
1090#ifndef USE_TCL_STUBS
1091# undef Tcl_InitStubs
1092# define Tcl_InitStubs(a,b,c)
1093#endif
1094
1095/*
drh75897232000-05-29 14:26:00 +00001096** Initialize this module.
1097**
1098** This Tcl module contains only a single new Tcl command named "sqlite".
1099** (Hence there is no namespace. There is no point in using a namespace
1100** if the extension only supplies one new name!) The "sqlite" command is
1101** used to open a new SQLite database. See the DbMain() routine above
1102** for additional information.
1103*/
drh38f82712004-06-18 17:10:16 +00001104int Sqlite3_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001105 Tcl_InitStubs(interp, "8.0", 0);
drhef4ac8f2004-06-19 00:16:31 +00001106 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1107 Tcl_PkgProvide(interp, "sqlite3", "3.0");
drh90ca9752001-09-28 17:47:14 +00001108 return TCL_OK;
1109}
drh38f82712004-06-18 17:10:16 +00001110int Tclsqlite3_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001111 Tcl_InitStubs(interp, "8.0", 0);
drhef4ac8f2004-06-19 00:16:31 +00001112 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
1113 Tcl_PkgProvide(interp, "sqlite3", "3.0");
drh75897232000-05-29 14:26:00 +00001114 return TCL_OK;
1115}
drh38f82712004-06-18 17:10:16 +00001116int Sqlite3_SafeInit(Tcl_Interp *interp){
drh75897232000-05-29 14:26:00 +00001117 return TCL_OK;
1118}
drh38f82712004-06-18 17:10:16 +00001119int Tclsqlite3_SafeInit(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001120 return TCL_OK;
1121}
drh75897232000-05-29 14:26:00 +00001122
drh3cebbde2000-10-19 14:59:27 +00001123#if 0
drh75897232000-05-29 14:26:00 +00001124/*
1125** If compiled using mktclapp, this routine runs to initialize
1126** everything.
1127*/
1128int Et_AppInit(Tcl_Interp *interp){
drh38f82712004-06-18 17:10:16 +00001129 return Sqlite3_Init(interp);
drh75897232000-05-29 14:26:00 +00001130}
drh3cebbde2000-10-19 14:59:27 +00001131#endif
drh348784e2000-05-29 20:41:49 +00001132
1133/*
1134** If the macro TCLSH is defined and is one, then put in code for the
1135** "main" routine that will initialize Tcl.
1136*/
1137#if defined(TCLSH) && TCLSH==1
1138static char zMainloop[] =
1139 "set line {}\n"
1140 "while {![eof stdin]} {\n"
1141 "if {$line!=\"\"} {\n"
1142 "puts -nonewline \"> \"\n"
1143 "} else {\n"
1144 "puts -nonewline \"% \"\n"
1145 "}\n"
1146 "flush stdout\n"
1147 "append line [gets stdin]\n"
1148 "if {[info complete $line]} {\n"
1149 "if {[catch {uplevel #0 $line} result]} {\n"
1150 "puts stderr \"Error: $result\"\n"
1151 "} elseif {$result!=\"\"} {\n"
1152 "puts $result\n"
1153 "}\n"
1154 "set line {}\n"
1155 "} else {\n"
1156 "append line \\n\n"
1157 "}\n"
1158 "}\n"
1159;
1160
1161#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1162int TCLSH_MAIN(int argc, char **argv){
1163 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001164 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001165 interp = Tcl_CreateInterp();
drh38f82712004-06-18 17:10:16 +00001166 Sqlite3_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001167#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001168 {
1169 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001170 extern int Sqlitetest2_Init(Tcl_Interp*);
1171 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001172 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001173 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001174 extern int Md5_Init(Tcl_Interp*);
danielk19776490beb2004-05-11 06:17:21 +00001175 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001176 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001177 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00001178 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00001179 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001180 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001181 }
1182#endif
drh348784e2000-05-29 20:41:49 +00001183 if( argc>=2 ){
1184 int i;
1185 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1186 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1187 for(i=2; i<argc; i++){
1188 Tcl_SetVar(interp, "argv", argv[i],
1189 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1190 }
1191 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001192 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001193 if( zInfo==0 ) zInfo = interp->result;
1194 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001195 return 1;
1196 }
1197 }else{
1198 Tcl_GlobalEval(interp, zMainloop);
1199 }
1200 return 0;
1201}
1202#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001203
1204#endif /* !defined(NO_TCL) */