blob: c0b751a603da1067759859b091ec4133b594ac4f [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**
danielk19772a764eb2004-06-12 01:43:26 +000014** $Id: tclsqlite.c,v 1.84 2004/06/12 01:43:27 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 int nChange; /* Database changes for the most recent eval */
73 Tcl_Obj *pCollateNeeded; /* Collation needed script */
drhbec3f402000-08-04 13:49:02 +000074};
75
76/*
drh75897232000-05-29 14:26:00 +000077** An instance of this structure passes information thru the sqlite
78** logic from the original TCL command into the callback routine.
79*/
80typedef struct CallbackData CallbackData;
81struct CallbackData {
82 Tcl_Interp *interp; /* The TCL interpreter */
83 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000084 Tcl_Obj *pCode; /* The code to execute for each row */
drhce927062001-11-09 13:41:09 +000085 int once; /* Set for first callback only */
drh960e8c62001-04-03 16:53:21 +000086 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000087 int nColName; /* Number of entries in the azColName[] array */
88 char **azColName; /* Column names translated to UTF-8 */
drh98808ba2001-10-18 12:34:46 +000089};
drh297ecf12001-04-05 15:57:13 +000090
drh6d313162000-09-21 13:01:35 +000091/*
drh5d9d7572003-08-19 14:31:01 +000092** This is a second alternative callback for database queries. A the
93** first column of the first row of the result is made the TCL result.
94*/
95static int DbEvalCallback3(
96 void *clientData, /* An instance of CallbackData */
97 int nCol, /* Number of columns in the result */
98 char ** azCol, /* Data for each column */
99 char ** azN /* Name for each column */
100){
101 Tcl_Interp *interp = (Tcl_Interp*)clientData;
102 Tcl_Obj *pElem;
103 if( azCol==0 ) return 1;
104 if( nCol==0 ) return 1;
105#ifdef UTF_TRANSLATION_NEEDED
106 {
107 Tcl_DString dCol;
108 Tcl_DStringInit(&dCol);
109 Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol);
110 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
111 Tcl_DStringFree(&dCol);
112 }
113#else
114 pElem = Tcl_NewStringObj(azCol[0], -1);
115#endif
116 Tcl_SetObjResult(interp, pElem);
117 return 1;
118}
119
120/*
drh75897232000-05-29 14:26:00 +0000121** Called when the command is deleted.
122*/
123static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000124 SqliteDb *pDb = (SqliteDb*)db;
danielk19776f8a5032004-05-10 10:34:51 +0000125 sqlite3_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000126 while( pDb->pFunc ){
127 SqlFunc *pFunc = pDb->pFunc;
128 pDb->pFunc = pFunc->pNext;
129 Tcl_Free((char*)pFunc);
130 }
danielk19770202b292004-06-09 09:55:16 +0000131 while( pDb->pCollate ){
132 SqlCollate *pCollate = pDb->pCollate;
133 pDb->pCollate = pCollate->pNext;
134 Tcl_Free((char*)pCollate);
135 }
drhbec3f402000-08-04 13:49:02 +0000136 if( pDb->zBusy ){
137 Tcl_Free(pDb->zBusy);
138 }
drhb5a20d32003-04-23 12:25:23 +0000139 if( pDb->zTrace ){
140 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000141 }
drhe22a3342003-04-22 20:30:37 +0000142 if( pDb->zAuth ){
143 Tcl_Free(pDb->zAuth);
144 }
drhbec3f402000-08-04 13:49:02 +0000145 Tcl_Free((char*)pDb);
146}
147
148/*
149** This routine is called when a database file is locked while trying
150** to execute SQL.
151*/
danielk19772a764eb2004-06-12 01:43:26 +0000152static int DbBusyHandler(void *cd, int nTries){
drhbec3f402000-08-04 13:49:02 +0000153 SqliteDb *pDb = (SqliteDb*)cd;
154 int rc;
155 char zVal[30];
156 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000157 Tcl_DString cmd;
158
159 Tcl_DStringInit(&cmd);
160 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
danielk19772a764eb2004-06-12 01:43:26 +0000161 sprintf(zVal, "%d", nTries);
162 Tcl_DStringAppendElement(&cmd, zVal);
drhbec3f402000-08-04 13:49:02 +0000163 zCmd = Tcl_DStringValue(&cmd);
164 rc = Tcl_Eval(pDb->interp, zCmd);
165 Tcl_DStringFree(&cmd);
166 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
167 return 0;
168 }
169 return 1;
drh75897232000-05-29 14:26:00 +0000170}
171
172/*
danielk1977348bb5d2003-10-18 09:37:26 +0000173** This routine is invoked as the 'progress callback' for the database.
174*/
175static int DbProgressHandler(void *cd){
176 SqliteDb *pDb = (SqliteDb*)cd;
177 int rc;
178
179 assert( pDb->zProgress );
180 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
181 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
182 return 1;
183 }
184 return 0;
185}
186
187/*
drhb5a20d32003-04-23 12:25:23 +0000188** This routine is called by the SQLite trace handler whenever a new
189** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000190*/
drhb5a20d32003-04-23 12:25:23 +0000191static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000192 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000193 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000194
drhb5a20d32003-04-23 12:25:23 +0000195 Tcl_DStringInit(&str);
196 Tcl_DStringAppend(&str, pDb->zTrace, -1);
197 Tcl_DStringAppendElement(&str, zSql);
198 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
199 Tcl_DStringFree(&str);
200 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000201}
202
203/*
drhaa940ea2004-01-15 02:44:03 +0000204** This routine is called when a transaction is committed. The
205** TCL script in pDb->zCommit is executed. If it returns non-zero or
206** if it throws an exception, the transaction is rolled back instead
207** of being committed.
208*/
209static int DbCommitHandler(void *cd){
210 SqliteDb *pDb = (SqliteDb*)cd;
211 int rc;
212
213 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
214 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
215 return 1;
216 }
217 return 0;
218}
219
danielk19777cedc8d2004-06-10 10:50:08 +0000220static void tclCollateNeeded(
221 void *pCtx,
222 sqlite *db,
223 int enc,
224 const char *zName
225){
226 SqliteDb *pDb = (SqliteDb *)pCtx;
227 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
228 Tcl_IncrRefCount(pScript);
229 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
230 Tcl_EvalObjEx(pDb->interp, pScript, 0);
231 Tcl_DecrRefCount(pScript);
232}
233
drhaa940ea2004-01-15 02:44:03 +0000234/*
danielk19770202b292004-06-09 09:55:16 +0000235** This routine is called to evaluate an SQL collation function implemented
236** using TCL script.
237*/
238static int tclSqlCollate(
239 void *pCtx,
240 int nA,
241 const void *zA,
242 int nB,
243 const void *zB
244){
245 SqlCollate *p = (SqlCollate *)pCtx;
246 Tcl_Obj *pCmd;
247
248 pCmd = Tcl_NewStringObj(p->zScript, -1);
249 Tcl_IncrRefCount(pCmd);
250 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA));
251 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB));
252 Tcl_EvalObjEx(p->interp, pCmd, 0);
253 Tcl_DecrRefCount(pCmd);
254 return (atoi(Tcl_GetStringResult(p->interp)));
255}
256
257/*
drhcabb0812002-09-14 13:47:32 +0000258** This routine is called to evaluate an SQL function implemented
259** using TCL script.
260*/
danielk19770ae8b832004-05-25 12:05:56 +0000261static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk19776f8a5032004-05-10 10:34:51 +0000262 SqlFunc *p = sqlite3_user_data(context);
drhcabb0812002-09-14 13:47:32 +0000263 Tcl_DString cmd;
264 int i;
265 int rc;
266
267 Tcl_DStringInit(&cmd);
268 Tcl_DStringAppend(&cmd, p->zScript, -1);
269 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000270 if( SQLITE_NULL==sqlite3_value_type(argv[i]) ){
danielk197751ad0ec2004-05-24 12:39:02 +0000271 Tcl_DStringAppendElement(&cmd, "");
272 }else{
drh4f26d6c2004-05-26 23:25:30 +0000273 Tcl_DStringAppendElement(&cmd, sqlite3_value_text(argv[i]));
danielk197751ad0ec2004-05-24 12:39:02 +0000274 }
drhcabb0812002-09-14 13:47:32 +0000275 }
276 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
277 if( rc ){
danielk19777e18c252004-05-25 11:47:24 +0000278 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhcabb0812002-09-14 13:47:32 +0000279 }else{
danielk19777e18c252004-05-25 11:47:24 +0000280 sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1, 1);
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",
397 "last_insert_rowid", "last_statement_changes", "onecolumn",
398 "progress", "rekey", "timeout",
danielk19777cedc8d2004-06-10 10:50:08 +0000399 "trace", "collate", "collation_needed",
drh22fbcb82004-02-01 01:22:50 +0000400 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,
406 DB_LAST_INSERT_ROWID, DB_LAST_STATEMENT_CHANGES, DB_ONECOLUMN,
407 DB_PROGRESS, DB_REKEY, DB_TIMEOUT,
danielk19777cedc8d2004-06-10 10:50:08 +0000408 DB_TRACE, DB_COLLATE, DB_COLLATION_NEEDED
drh6d313162000-09-21 13:01:35 +0000409 };
410
411 if( objc<2 ){
412 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000413 return TCL_ERROR;
414 }
drh411995d2002-06-25 19:31:18 +0000415 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000416 return TCL_ERROR;
417 }
418
drh411995d2002-06-25 19:31:18 +0000419 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000420
drhe22a3342003-04-22 20:30:37 +0000421 /* $db authorizer ?CALLBACK?
422 **
423 ** Invoke the given callback to authorize each SQL operation as it is
424 ** compiled. 5 arguments are appended to the callback before it is
425 ** invoked:
426 **
427 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
428 ** (2) First descriptive name (depends on authorization type)
429 ** (3) Second descriptive name
430 ** (4) Name of the database (ex: "main", "temp")
431 ** (5) Name of trigger that is doing the access
432 **
433 ** The callback should return on of the following strings: SQLITE_OK,
434 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
435 **
436 ** If this method is invoked with no arguments, the current authorization
437 ** callback string is returned.
438 */
439 case DB_AUTHORIZER: {
440 if( objc>3 ){
441 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
442 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000443 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000444 Tcl_AppendResult(interp, pDb->zAuth, 0);
445 }
446 }else{
447 char *zAuth;
448 int len;
449 if( pDb->zAuth ){
450 Tcl_Free(pDb->zAuth);
451 }
452 zAuth = Tcl_GetStringFromObj(objv[2], &len);
453 if( zAuth && len>0 ){
454 pDb->zAuth = Tcl_Alloc( len + 1 );
455 strcpy(pDb->zAuth, zAuth);
456 }else{
457 pDb->zAuth = 0;
458 }
459#ifndef SQLITE_OMIT_AUTHORIZATION
460 if( pDb->zAuth ){
461 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000462 sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
drhe22a3342003-04-22 20:30:37 +0000463 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000464 sqlite3_set_authorizer(pDb->db, 0, 0);
drhe22a3342003-04-22 20:30:37 +0000465 }
466#endif
467 }
468 break;
469 }
470
drhbec3f402000-08-04 13:49:02 +0000471 /* $db busy ?CALLBACK?
472 **
473 ** Invoke the given callback if an SQL statement attempts to open
474 ** a locked database file.
475 */
drh6d313162000-09-21 13:01:35 +0000476 case DB_BUSY: {
477 if( objc>3 ){
478 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000479 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000480 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000481 if( pDb->zBusy ){
482 Tcl_AppendResult(interp, pDb->zBusy, 0);
483 }
484 }else{
drh6d313162000-09-21 13:01:35 +0000485 char *zBusy;
486 int len;
drhbec3f402000-08-04 13:49:02 +0000487 if( pDb->zBusy ){
488 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000489 }
drh6d313162000-09-21 13:01:35 +0000490 zBusy = Tcl_GetStringFromObj(objv[2], &len);
491 if( zBusy && len>0 ){
492 pDb->zBusy = Tcl_Alloc( len + 1 );
493 strcpy(pDb->zBusy, zBusy);
494 }else{
495 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000496 }
497 if( pDb->zBusy ){
498 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000499 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000500 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000501 sqlite3_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000502 }
503 }
drh6d313162000-09-21 13:01:35 +0000504 break;
505 }
drhbec3f402000-08-04 13:49:02 +0000506
danielk1977348bb5d2003-10-18 09:37:26 +0000507 /* $db progress ?N CALLBACK?
508 **
509 ** Invoke the given callback every N virtual machine opcodes while executing
510 ** queries.
511 */
512 case DB_PROGRESS: {
513 if( objc==2 ){
514 if( pDb->zProgress ){
515 Tcl_AppendResult(interp, pDb->zProgress, 0);
516 }
517 }else if( objc==4 ){
518 char *zProgress;
519 int len;
520 int N;
521 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
522 return TCL_ERROR;
523 };
524 if( pDb->zProgress ){
525 Tcl_Free(pDb->zProgress);
526 }
527 zProgress = Tcl_GetStringFromObj(objv[3], &len);
528 if( zProgress && len>0 ){
529 pDb->zProgress = Tcl_Alloc( len + 1 );
530 strcpy(pDb->zProgress, zProgress);
531 }else{
532 pDb->zProgress = 0;
533 }
534#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
535 if( pDb->zProgress ){
536 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000537 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
danielk1977348bb5d2003-10-18 09:37:26 +0000538 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000539 sqlite3_progress_handler(pDb->db, 0, 0, 0);
danielk1977348bb5d2003-10-18 09:37:26 +0000540 }
541#endif
542 }else{
543 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
544 return TCL_ERROR;
545 }
546 break;
547 }
548
drhc8d30ac2002-04-12 10:08:59 +0000549 /*
550 ** $db changes
551 **
552 ** Return the number of rows that were modified, inserted, or deleted by
553 ** the most recent "eval".
554 */
555 case DB_CHANGES: {
556 Tcl_Obj *pResult;
557 int nChange;
558 if( objc!=2 ){
559 Tcl_WrongNumArgs(interp, 2, objv, "");
560 return TCL_ERROR;
561 }
danielk197730ccda12004-05-27 12:11:31 +0000562 /* nChange = sqlite3_changes(pDb->db); */
563 nChange = pDb->nChange;
drhc8d30ac2002-04-12 10:08:59 +0000564 pResult = Tcl_GetObjResult(interp);
565 Tcl_SetIntObj(pResult, nChange);
566 break;
567 }
568
rdcf146a772004-02-25 22:51:06 +0000569 /*
570 ** $db last_statement_changes
571 **
572 ** Return the number of rows that were modified, inserted, or deleted by
573 ** the last statment to complete execution (excluding changes due to
574 ** triggers)
575 */
576 case DB_LAST_STATEMENT_CHANGES: {
577 Tcl_Obj *pResult;
578 int lsChange;
579 if( objc!=2 ){
580 Tcl_WrongNumArgs(interp, 2, objv, "");
581 return TCL_ERROR;
582 }
danielk19776f8a5032004-05-10 10:34:51 +0000583 lsChange = sqlite3_last_statement_changes(pDb->db);
rdcf146a772004-02-25 22:51:06 +0000584 pResult = Tcl_GetObjResult(interp);
585 Tcl_SetIntObj(pResult, lsChange);
586 break;
587 }
588
drh75897232000-05-29 14:26:00 +0000589 /* $db close
590 **
591 ** Shutdown the database
592 */
drh6d313162000-09-21 13:01:35 +0000593 case DB_CLOSE: {
594 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
595 break;
596 }
drh75897232000-05-29 14:26:00 +0000597
drhaa940ea2004-01-15 02:44:03 +0000598 /* $db commit_hook ?CALLBACK?
599 **
600 ** Invoke the given callback just before committing every SQL transaction.
601 ** If the callback throws an exception or returns non-zero, then the
602 ** transaction is aborted. If CALLBACK is an empty string, the callback
603 ** is disabled.
604 */
605 case DB_COMMIT_HOOK: {
606 if( objc>3 ){
607 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
608 }else if( objc==2 ){
609 if( pDb->zCommit ){
610 Tcl_AppendResult(interp, pDb->zCommit, 0);
611 }
612 }else{
613 char *zCommit;
614 int len;
615 if( pDb->zCommit ){
616 Tcl_Free(pDb->zCommit);
617 }
618 zCommit = Tcl_GetStringFromObj(objv[2], &len);
619 if( zCommit && len>0 ){
620 pDb->zCommit = Tcl_Alloc( len + 1 );
621 strcpy(pDb->zCommit, zCommit);
622 }else{
623 pDb->zCommit = 0;
624 }
625 if( pDb->zCommit ){
626 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000627 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
drhaa940ea2004-01-15 02:44:03 +0000628 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000629 sqlite3_commit_hook(pDb->db, 0, 0);
drhaa940ea2004-01-15 02:44:03 +0000630 }
631 }
632 break;
633 }
634
drh75897232000-05-29 14:26:00 +0000635 /* $db complete SQL
636 **
637 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
638 ** additional lines of input are needed. This is similar to the
639 ** built-in "info complete" command of Tcl.
640 */
drh6d313162000-09-21 13:01:35 +0000641 case DB_COMPLETE: {
642 Tcl_Obj *pResult;
643 int isComplete;
644 if( objc!=3 ){
645 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000646 return TCL_ERROR;
647 }
danielk19776f8a5032004-05-10 10:34:51 +0000648 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
drh6d313162000-09-21 13:01:35 +0000649 pResult = Tcl_GetObjResult(interp);
650 Tcl_SetBooleanObj(pResult, isComplete);
651 break;
652 }
drhdcd997e2003-01-31 17:21:49 +0000653
654 /*
655 ** $db errorcode
656 **
657 ** Return the numeric error code that was returned by the most recent
danielk19776f8a5032004-05-10 10:34:51 +0000658 ** call to sqlite3_exec().
drhdcd997e2003-01-31 17:21:49 +0000659 */
660 case DB_ERRORCODE: {
661 Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc));
662 break;
663 }
drh75897232000-05-29 14:26:00 +0000664
665 /*
666 ** $db eval $sql ?array { ...code... }?
667 **
668 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000669 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000670 ** If "array" and "code" are omitted, then no callback is every invoked.
671 ** If "array" is an empty string, then the values are placed in variables
672 ** that have the same name as the fields extracted by the query.
673 */
drh6d313162000-09-21 13:01:35 +0000674 case DB_EVAL: {
danielk197730ccda12004-05-27 12:11:31 +0000675 char const *zSql;
676 char const *zLeft;
677 sqlite3_stmt *pStmt;
danielk1977ef2cb632004-05-29 02:37:19 +0000678
679 Tcl_Obj *pRet = Tcl_NewObj();
680 Tcl_IncrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000681
682 if( objc!=5 && objc!=3 ){
683 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
684 return TCL_ERROR;
685 }
686
687 pDb->nChange = 0;
688 zSql = Tcl_GetStringFromObj(objv[2], 0);
689 while( zSql[0] ){
690 int i;
691
692 if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000693 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000694 rc = TCL_ERROR;
695 break;
696 }
697
698 if( pStmt && objc==5 ){
699 Tcl_Obj *pColList = Tcl_NewObj();
700 Tcl_IncrRefCount(pColList);
701
702 for(i=0; i<sqlite3_column_count(pStmt); i++){
703 Tcl_ListObjAppendElement(interp, pColList,
danielk1977ef2cb632004-05-29 02:37:19 +0000704 dbTextToObj(sqlite3_column_name(pStmt, i))
danielk197730ccda12004-05-27 12:11:31 +0000705 );
706 }
707 Tcl_ObjSetVar2(interp,objv[3],Tcl_NewStringObj("*",-1),pColList,0);
708 }
709
710 while( pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
711 for(i=0; i<sqlite3_column_count(pStmt); i++){
712 Tcl_Obj *pVal;
713
714 /* Set pVal to contain the i'th column of this row. */
drh9c054832004-05-31 18:51:57 +0000715 if( SQLITE_BLOB!=sqlite3_column_type(pStmt, i) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000716 pVal = dbTextToObj(sqlite3_column_text(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000717 }else{
danielk19773fd0a732004-05-27 13:35:19 +0000718 int bytes = sqlite3_column_bytes(pStmt, i);
719 pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes);
danielk197730ccda12004-05-27 12:11:31 +0000720 }
721
722 if( objc==5 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000723 Tcl_Obj *pName = dbTextToObj(sqlite3_column_name(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000724 Tcl_IncrRefCount(pName);
725 if( !strcmp("", Tcl_GetString(objv[3])) ){
726 Tcl_ObjSetVar2(interp, pName, 0, pVal, 0);
727 }else{
728 Tcl_ObjSetVar2(interp, objv[3], pName, pVal, 0);
729 }
730 Tcl_DecrRefCount(pName);
731 }else{
danielk197730ccda12004-05-27 12:11:31 +0000732 Tcl_ListObjAppendElement(interp, pRet, pVal);
733 }
734 }
735
736 if( objc==5 ){
737 rc = Tcl_EvalObjEx(interp, objv[4], 0);
738 if( rc!=TCL_ERROR ) rc = TCL_OK;
739 }
740 }
741
742 if( pStmt && SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){
743 continue;
744 }
745
746 if( pStmt && SQLITE_OK!=sqlite3_errcode(pDb->db) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000747 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000748 rc = TCL_ERROR;
749 break;
750 }
751
752 pDb->nChange += sqlite3_changes(pDb->db);
753 zSql = zLeft;
754 }
755
danielk1977ef2cb632004-05-29 02:37:19 +0000756 if( rc==TCL_OK ){
danielk197730ccda12004-05-27 12:11:31 +0000757 Tcl_SetObjResult(interp, pRet);
danielk197730ccda12004-05-27 12:11:31 +0000758 }
danielk1977ef2cb632004-05-29 02:37:19 +0000759 Tcl_DecrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000760
761 break;
762 }
drhbec3f402000-08-04 13:49:02 +0000763
764 /*
drhcabb0812002-09-14 13:47:32 +0000765 ** $db function NAME SCRIPT
766 **
767 ** Create a new SQL function called NAME. Whenever that function is
768 ** called, invoke SCRIPT to evaluate the function.
769 */
770 case DB_FUNCTION: {
771 SqlFunc *pFunc;
772 char *zName;
773 char *zScript;
774 int nScript;
775 if( objc!=4 ){
776 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
777 return TCL_ERROR;
778 }
779 zName = Tcl_GetStringFromObj(objv[2], 0);
780 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
781 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
782 if( pFunc==0 ) return TCL_ERROR;
783 pFunc->interp = interp;
784 pFunc->pNext = pDb->pFunc;
785 pFunc->zScript = (char*)&pFunc[1];
786 strcpy(pFunc->zScript, zScript);
danielk197765904932004-05-26 06:18:37 +0000787 sqlite3_create_function(pDb->db, zName, -1, 0, 0, pFunc, tclSqlFunc, 0, 0);
drhcabb0812002-09-14 13:47:32 +0000788 break;
789 }
790
791 /*
drhaf9ff332002-01-16 21:00:27 +0000792 ** $db last_insert_rowid
793 **
794 ** Return an integer which is the ROWID for the most recent insert.
795 */
796 case DB_LAST_INSERT_ROWID: {
797 Tcl_Obj *pResult;
798 int rowid;
799 if( objc!=2 ){
800 Tcl_WrongNumArgs(interp, 2, objv, "");
801 return TCL_ERROR;
802 }
danielk19776f8a5032004-05-10 10:34:51 +0000803 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +0000804 pResult = Tcl_GetObjResult(interp);
805 Tcl_SetIntObj(pResult, rowid);
806 break;
807 }
808
809 /*
drh5d9d7572003-08-19 14:31:01 +0000810 ** $db onecolumn SQL
811 **
812 ** Return a single column from a single row of the given SQL query.
813 */
814 case DB_ONECOLUMN: {
drh5d9d7572003-08-19 14:31:01 +0000815 char *zSql;
816 char *zErrMsg = 0;
817 if( objc!=3 ){
818 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
819 return TCL_ERROR;
820 }
821 zSql = Tcl_GetStringFromObj(objv[2], 0);
danielk19776f8a5032004-05-10 10:34:51 +0000822 rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
drh5d9d7572003-08-19 14:31:01 +0000823 if( rc==SQLITE_ABORT ){
drh22fbcb82004-02-01 01:22:50 +0000824 rc = SQLITE_OK;
drh5d9d7572003-08-19 14:31:01 +0000825 }else if( zErrMsg ){
826 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
827 free(zErrMsg);
828 rc = TCL_ERROR;
829 }else if( rc!=SQLITE_OK ){
danielk1977f20b21c2004-05-31 23:56:42 +0000830 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh5d9d7572003-08-19 14:31:01 +0000831 rc = TCL_ERROR;
832 }
833 break;
834 }
835
836 /*
drh22fbcb82004-02-01 01:22:50 +0000837 ** $db rekey KEY
838 **
839 ** Change the encryption key on the currently open database.
840 */
841 case DB_REKEY: {
842 int nKey;
843 void *pKey;
844 if( objc!=3 ){
845 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
846 return TCL_ERROR;
847 }
848 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +0000849#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000850 rc = sqlite_rekey(pDb->db, pKey, nKey);
851 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000852 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh22fbcb82004-02-01 01:22:50 +0000853 rc = TCL_ERROR;
854 }
855#endif
856 break;
857 }
858
859 /*
drhbec3f402000-08-04 13:49:02 +0000860 ** $db timeout MILLESECONDS
861 **
862 ** Delay for the number of milliseconds specified when a file is locked.
863 */
drh6d313162000-09-21 13:01:35 +0000864 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000865 int ms;
drh6d313162000-09-21 13:01:35 +0000866 if( objc!=3 ){
867 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000868 return TCL_ERROR;
869 }
drh6d313162000-09-21 13:01:35 +0000870 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +0000871 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000872 break;
drh75897232000-05-29 14:26:00 +0000873 }
drhb5a20d32003-04-23 12:25:23 +0000874
875 /* $db trace ?CALLBACK?
876 **
877 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
878 ** that is executed. The text of the SQL is appended to CALLBACK before
879 ** it is executed.
880 */
881 case DB_TRACE: {
882 if( objc>3 ){
883 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
884 }else if( objc==2 ){
885 if( pDb->zTrace ){
886 Tcl_AppendResult(interp, pDb->zTrace, 0);
887 }
888 }else{
889 char *zTrace;
890 int len;
891 if( pDb->zTrace ){
892 Tcl_Free(pDb->zTrace);
893 }
894 zTrace = Tcl_GetStringFromObj(objv[2], &len);
895 if( zTrace && len>0 ){
896 pDb->zTrace = Tcl_Alloc( len + 1 );
897 strcpy(pDb->zTrace, zTrace);
898 }else{
899 pDb->zTrace = 0;
900 }
901 if( pDb->zTrace ){
902 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000903 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +0000904 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000905 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +0000906 }
907 }
908 break;
909 }
910
danielk19770202b292004-06-09 09:55:16 +0000911 /*
912 ** $db collate NAME SCRIPT
913 **
914 ** Create a new SQL collation function called NAME. Whenever
915 ** that function is called, invoke SCRIPT to evaluate the function.
916 */
917 case DB_COLLATE: {
918 SqlCollate *pCollate;
919 char *zName;
920 char *zScript;
921 int nScript;
922 if( objc!=4 ){
923 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
924 return TCL_ERROR;
925 }
926 zName = Tcl_GetStringFromObj(objv[2], 0);
927 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
928 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
929 if( pCollate==0 ) return TCL_ERROR;
930 pCollate->interp = interp;
931 pCollate->pNext = pDb->pCollate;
932 pCollate->zScript = (char*)&pCollate[1];
933 strcpy(pCollate->zScript, zScript);
danielk1977466be562004-06-10 02:16:01 +0000934 if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
935 pCollate, tclSqlCollate) ){
danielk19770202b292004-06-09 09:55:16 +0000936 return TCL_ERROR;
937 }
938 break;
939 }
940
danielk19777cedc8d2004-06-10 10:50:08 +0000941 /*
942 ** $db collate_needed SCRIPT
943 **
944 ** Create a new SQL collation function called NAME. Whenever
945 ** that function is called, invoke SCRIPT to evaluate the function.
946 */
947 case DB_COLLATION_NEEDED: {
948 if( objc!=3 ){
949 Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT");
950 return TCL_ERROR;
951 }
952 if( pDb->pCollateNeeded ){
953 Tcl_DecrRefCount(pDb->pCollateNeeded);
954 }
955 pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]);
956 Tcl_IncrRefCount(pDb->pCollateNeeded);
957 sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded);
958 break;
959 }
960
drh6d313162000-09-21 13:01:35 +0000961 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +0000962 return rc;
drh75897232000-05-29 14:26:00 +0000963}
964
965/*
drh22fbcb82004-02-01 01:22:50 +0000966** sqlite DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +0000967**
968** This is the main Tcl command. When the "sqlite" Tcl command is
969** invoked, this routine runs to process that command.
970**
971** The first argument, DBNAME, is an arbitrary name for a new
972** database connection. This command creates a new command named
973** DBNAME that is used to control that connection. The database
974** connection is deleted when the DBNAME command is deleted.
975**
976** The second argument is the name of the directory that contains
977** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000978**
979** For testing purposes, we also support the following:
980**
981** sqlite -encoding
982**
983** Return the encoding used by LIKE and GLOB operators. Choices
984** are UTF-8 and iso8859.
985**
drh647cb0e2002-11-04 19:32:25 +0000986** sqlite -version
987**
988** Return the version number of the SQLite library.
989**
drhfbc3eab2001-04-06 16:13:42 +0000990** sqlite -tcl-uses-utf
991**
992** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
993** not. Used by tests to make sure the library was compiled
994** correctly.
drh75897232000-05-29 14:26:00 +0000995*/
drh22fbcb82004-02-01 01:22:50 +0000996static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000997 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +0000998 void *pKey = 0;
999 int nKey = 0;
1000 const char *zArg;
drh75897232000-05-29 14:26:00 +00001001 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +00001002 const char *zFile;
drh06b27182002-06-26 20:06:05 +00001003 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +00001004 if( objc==2 ){
1005 zArg = Tcl_GetStringFromObj(objv[1], 0);
drh22fbcb82004-02-01 01:22:50 +00001006 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001007 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +00001008 return TCL_OK;
1009 }
drh9eb9e262004-02-11 02:18:05 +00001010 if( strcmp(zArg,"-has-codec")==0 ){
1011#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001012 Tcl_AppendResult(interp,"1",0);
1013#else
1014 Tcl_AppendResult(interp,"0",0);
1015#endif
1016 return TCL_OK;
1017 }
1018 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +00001019#ifdef TCL_UTF_MAX
1020 Tcl_AppendResult(interp,"1",0);
1021#else
1022 Tcl_AppendResult(interp,"0",0);
1023#endif
1024 return TCL_OK;
1025 }
1026 }
drh22fbcb82004-02-01 01:22:50 +00001027 if( objc==5 || objc==6 ){
1028 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
1029 if( strcmp(zArg,"-key")==0 ){
1030 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
1031 objc -= 2;
1032 }
1033 }
1034 if( objc!=3 && objc!=4 ){
1035 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +00001036#ifdef SQLITE_HAS_CODEC
1037 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +00001038#else
1039 "HANDLE FILENAME ?MODE?"
1040#endif
1041 );
drh75897232000-05-29 14:26:00 +00001042 return TCL_ERROR;
1043 }
drh75897232000-05-29 14:26:00 +00001044 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001045 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001046 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001047 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1048 return TCL_ERROR;
1049 }
1050 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00001051 zFile = Tcl_GetStringFromObj(objv[2], 0);
drh9eb9e262004-02-11 02:18:05 +00001052#ifdef SQLITE_HAS_CODEC
danielk19776f8a5032004-05-10 10:34:51 +00001053 p->db = sqlite3_open_encrypted(zFile, pKey, nKey, 0, &zErrMsg);
drheb8ed702004-02-11 10:37:23 +00001054#else
danielk19774f057f92004-06-08 00:02:33 +00001055 sqlite3_open(zFile, &p->db);
danielk197780290862004-05-22 09:21:21 +00001056 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
1057 zErrMsg = strdup(sqlite3_errmsg(p->db));
1058 sqlite3_close(p->db);
1059 p->db = 0;
1060 }
drheb8ed702004-02-11 10:37:23 +00001061#endif
drhbec3f402000-08-04 13:49:02 +00001062 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001063 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001064 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001065 free(zErrMsg);
1066 return TCL_ERROR;
1067 }
drh22fbcb82004-02-01 01:22:50 +00001068 zArg = Tcl_GetStringFromObj(objv[1], 0);
1069 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001070
drh06b27182002-06-26 20:06:05 +00001071 /* The return value is the value of the sqlite* pointer
1072 */
1073 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001074 if( strncmp(zBuf,"0x",2) ){
1075 sprintf(zBuf, "0x%p", p->db);
1076 }
drh06b27182002-06-26 20:06:05 +00001077 Tcl_AppendResult(interp, zBuf, 0);
1078
drhc22bd472002-05-10 13:14:07 +00001079 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001080 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001081 */
drh28b4e482002-03-11 02:06:13 +00001082#ifdef SQLITE_TEST
1083 {
drhc22bd472002-05-10 13:14:07 +00001084 extern void Md5_Register(sqlite*);
1085 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +00001086 }
drh28b4e482002-03-11 02:06:13 +00001087#endif
danielk19777cedc8d2004-06-10 10:50:08 +00001088 p->interp = interp;
drh75897232000-05-29 14:26:00 +00001089 return TCL_OK;
1090}
1091
1092/*
drh90ca9752001-09-28 17:47:14 +00001093** Provide a dummy Tcl_InitStubs if we are using this as a static
1094** library.
1095*/
1096#ifndef USE_TCL_STUBS
1097# undef Tcl_InitStubs
1098# define Tcl_InitStubs(a,b,c)
1099#endif
1100
1101/*
drh75897232000-05-29 14:26:00 +00001102** Initialize this module.
1103**
1104** This Tcl module contains only a single new Tcl command named "sqlite".
1105** (Hence there is no namespace. There is no point in using a namespace
1106** if the extension only supplies one new name!) The "sqlite" command is
1107** used to open a new SQLite database. See the DbMain() routine above
1108** for additional information.
1109*/
1110int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001111 Tcl_InitStubs(interp, "8.0", 0);
drh22fbcb82004-02-01 01:22:50 +00001112 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001113 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh90ca9752001-09-28 17:47:14 +00001114 return TCL_OK;
1115}
1116int Tclsqlite_Init(Tcl_Interp *interp){
1117 Tcl_InitStubs(interp, "8.0", 0);
drh22fbcb82004-02-01 01:22:50 +00001118 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001119 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh75897232000-05-29 14:26:00 +00001120 return TCL_OK;
1121}
1122int Sqlite_SafeInit(Tcl_Interp *interp){
1123 return TCL_OK;
1124}
drh90ca9752001-09-28 17:47:14 +00001125int Tclsqlite_SafeInit(Tcl_Interp *interp){
1126 return TCL_OK;
1127}
drh75897232000-05-29 14:26:00 +00001128
drh3cebbde2000-10-19 14:59:27 +00001129#if 0
drh75897232000-05-29 14:26:00 +00001130/*
1131** If compiled using mktclapp, this routine runs to initialize
1132** everything.
1133*/
1134int Et_AppInit(Tcl_Interp *interp){
1135 return Sqlite_Init(interp);
1136}
drh3cebbde2000-10-19 14:59:27 +00001137#endif
drh348784e2000-05-29 20:41:49 +00001138
1139/*
1140** If the macro TCLSH is defined and is one, then put in code for the
1141** "main" routine that will initialize Tcl.
1142*/
1143#if defined(TCLSH) && TCLSH==1
1144static char zMainloop[] =
1145 "set line {}\n"
1146 "while {![eof stdin]} {\n"
1147 "if {$line!=\"\"} {\n"
1148 "puts -nonewline \"> \"\n"
1149 "} else {\n"
1150 "puts -nonewline \"% \"\n"
1151 "}\n"
1152 "flush stdout\n"
1153 "append line [gets stdin]\n"
1154 "if {[info complete $line]} {\n"
1155 "if {[catch {uplevel #0 $line} result]} {\n"
1156 "puts stderr \"Error: $result\"\n"
1157 "} elseif {$result!=\"\"} {\n"
1158 "puts $result\n"
1159 "}\n"
1160 "set line {}\n"
1161 "} else {\n"
1162 "append line \\n\n"
1163 "}\n"
1164 "}\n"
1165;
1166
1167#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1168int TCLSH_MAIN(int argc, char **argv){
1169 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001170 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001171 interp = Tcl_CreateInterp();
danielk19774adee202004-05-08 08:23:19 +00001172 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001173#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001174 {
1175 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001176 extern int Sqlitetest2_Init(Tcl_Interp*);
1177 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001178 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001179 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001180 extern int Md5_Init(Tcl_Interp*);
danielk19776490beb2004-05-11 06:17:21 +00001181 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001182 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001183 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00001184 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00001185 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001186 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001187 }
1188#endif
drh348784e2000-05-29 20:41:49 +00001189 if( argc>=2 ){
1190 int i;
1191 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1192 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1193 for(i=2; i<argc; i++){
1194 Tcl_SetVar(interp, "argv", argv[i],
1195 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1196 }
1197 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001198 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001199 if( zInfo==0 ) zInfo = interp->result;
1200 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001201 return 1;
1202 }
1203 }else{
1204 Tcl_GlobalEval(interp, zMainloop);
1205 }
1206 return 0;
1207}
1208#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001209
1210#endif /* !defined(NO_TCL) */