blob: f0aad5f639156dc92e6ec841814138bbef45a3c7 [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**
danielk19770202b292004-06-09 09:55:16 +000014** $Id: tclsqlite.c,v 1.81 2004/06/09 09:55:19 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() */
danielk197730ccda12004-05-27 12:11:31 +000072 int nChange; /* Database changes for the most recent eval */
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*/
151static int DbBusyHandler(void *cd, const char *zTable, int nTries){
152 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);
160 Tcl_DStringAppendElement(&cmd, zTable);
161 sprintf(zVal, " %d", nTries);
162 Tcl_DStringAppend(&cmd, zVal, -1);
163 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
220/*
danielk19770202b292004-06-09 09:55:16 +0000221** This routine is called to evaluate an SQL collation function implemented
222** using TCL script.
223*/
224static int tclSqlCollate(
225 void *pCtx,
226 int nA,
227 const void *zA,
228 int nB,
229 const void *zB
230){
231 SqlCollate *p = (SqlCollate *)pCtx;
232 Tcl_Obj *pCmd;
233
234 pCmd = Tcl_NewStringObj(p->zScript, -1);
235 Tcl_IncrRefCount(pCmd);
236 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA));
237 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB));
238 Tcl_EvalObjEx(p->interp, pCmd, 0);
239 Tcl_DecrRefCount(pCmd);
240 return (atoi(Tcl_GetStringResult(p->interp)));
241}
242
243/*
drhcabb0812002-09-14 13:47:32 +0000244** This routine is called to evaluate an SQL function implemented
245** using TCL script.
246*/
danielk19770ae8b832004-05-25 12:05:56 +0000247static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk19776f8a5032004-05-10 10:34:51 +0000248 SqlFunc *p = sqlite3_user_data(context);
drhcabb0812002-09-14 13:47:32 +0000249 Tcl_DString cmd;
250 int i;
251 int rc;
252
253 Tcl_DStringInit(&cmd);
254 Tcl_DStringAppend(&cmd, p->zScript, -1);
255 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000256 if( SQLITE_NULL==sqlite3_value_type(argv[i]) ){
danielk197751ad0ec2004-05-24 12:39:02 +0000257 Tcl_DStringAppendElement(&cmd, "");
258 }else{
drh4f26d6c2004-05-26 23:25:30 +0000259 Tcl_DStringAppendElement(&cmd, sqlite3_value_text(argv[i]));
danielk197751ad0ec2004-05-24 12:39:02 +0000260 }
drhcabb0812002-09-14 13:47:32 +0000261 }
262 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
263 if( rc ){
danielk19777e18c252004-05-25 11:47:24 +0000264 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhcabb0812002-09-14 13:47:32 +0000265 }else{
danielk19777e18c252004-05-25 11:47:24 +0000266 sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1, 1);
drhcabb0812002-09-14 13:47:32 +0000267 }
268}
drhe22a3342003-04-22 20:30:37 +0000269#ifndef SQLITE_OMIT_AUTHORIZATION
270/*
271** This is the authentication function. It appends the authentication
272** type code and the two arguments to zCmd[] then invokes the result
273** on the interpreter. The reply is examined to determine if the
274** authentication fails or succeeds.
275*/
276static int auth_callback(
277 void *pArg,
278 int code,
279 const char *zArg1,
280 const char *zArg2,
281 const char *zArg3,
282 const char *zArg4
283){
284 char *zCode;
285 Tcl_DString str;
286 int rc;
287 const char *zReply;
288 SqliteDb *pDb = (SqliteDb*)pArg;
289
290 switch( code ){
291 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
292 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
293 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
294 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
295 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
296 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
297 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
298 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
299 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
300 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
301 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
302 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
303 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
304 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
305 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
306 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
307 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
308 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
309 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
310 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
311 case SQLITE_READ : zCode="SQLITE_READ"; break;
312 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
313 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
314 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000315 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
316 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
drhe22a3342003-04-22 20:30:37 +0000317 default : zCode="????"; break;
318 }
319 Tcl_DStringInit(&str);
320 Tcl_DStringAppend(&str, pDb->zAuth, -1);
321 Tcl_DStringAppendElement(&str, zCode);
322 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
323 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
324 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
325 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
326 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
327 Tcl_DStringFree(&str);
328 zReply = Tcl_GetStringResult(pDb->interp);
329 if( strcmp(zReply,"SQLITE_OK")==0 ){
330 rc = SQLITE_OK;
331 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
332 rc = SQLITE_DENY;
333 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
334 rc = SQLITE_IGNORE;
335 }else{
336 rc = 999;
337 }
338 return rc;
339}
340#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000341
342/*
danielk1977ef2cb632004-05-29 02:37:19 +0000343** zText is a pointer to text obtained via an sqlite3_result_text()
344** or similar interface. This routine returns a Tcl string object,
345** reference count set to 0, containing the text. If a translation
346** between iso8859 and UTF-8 is required, it is preformed.
347*/
348static Tcl_Obj *dbTextToObj(char const *zText){
349 Tcl_Obj *pVal;
350#ifdef UTF_TRANSLATION_NEEDED
351 Tcl_DString dCol;
352 Tcl_DStringInit(&dCol);
353 Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol);
354 pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
355 Tcl_DStringFree(&dCol);
356#else
357 pVal = Tcl_NewStringObj(zText, -1);
358#endif
359 return pVal;
360}
361
362/*
drh75897232000-05-29 14:26:00 +0000363** The "sqlite" command below creates a new Tcl command for each
364** connection it opens to an SQLite database. This routine is invoked
365** whenever one of those connection-specific commands is executed
366** in Tcl. For example, if you run Tcl code like this:
367**
368** sqlite db1 "my_database"
369** db1 close
370**
371** The first command opens a connection to the "my_database" database
372** and calls that connection "db1". The second command causes this
373** subroutine to be invoked.
374*/
drh6d313162000-09-21 13:01:35 +0000375static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000376 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000377 int choice;
drh22fbcb82004-02-01 01:22:50 +0000378 int rc = TCL_OK;
drh0de8c112002-07-06 16:32:14 +0000379 static const char *DB_strs[] = {
rdcf146a772004-02-25 22:51:06 +0000380 "authorizer", "busy", "changes",
381 "close", "commit_hook", "complete",
382 "errorcode", "eval", "function",
383 "last_insert_rowid", "last_statement_changes", "onecolumn",
384 "progress", "rekey", "timeout",
danielk19770202b292004-06-09 09:55:16 +0000385 "trace", "collate",
drh22fbcb82004-02-01 01:22:50 +0000386 0
drh6d313162000-09-21 13:01:35 +0000387 };
drh411995d2002-06-25 19:31:18 +0000388 enum DB_enum {
rdcf146a772004-02-25 22:51:06 +0000389 DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
390 DB_CLOSE, DB_COMMIT_HOOK, DB_COMPLETE,
391 DB_ERRORCODE, DB_EVAL, DB_FUNCTION,
392 DB_LAST_INSERT_ROWID, DB_LAST_STATEMENT_CHANGES, DB_ONECOLUMN,
393 DB_PROGRESS, DB_REKEY, DB_TIMEOUT,
danielk19770202b292004-06-09 09:55:16 +0000394 DB_TRACE, DB_COLLATE
drh6d313162000-09-21 13:01:35 +0000395 };
396
397 if( objc<2 ){
398 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000399 return TCL_ERROR;
400 }
drh411995d2002-06-25 19:31:18 +0000401 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000402 return TCL_ERROR;
403 }
404
drh411995d2002-06-25 19:31:18 +0000405 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000406
drhe22a3342003-04-22 20:30:37 +0000407 /* $db authorizer ?CALLBACK?
408 **
409 ** Invoke the given callback to authorize each SQL operation as it is
410 ** compiled. 5 arguments are appended to the callback before it is
411 ** invoked:
412 **
413 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
414 ** (2) First descriptive name (depends on authorization type)
415 ** (3) Second descriptive name
416 ** (4) Name of the database (ex: "main", "temp")
417 ** (5) Name of trigger that is doing the access
418 **
419 ** The callback should return on of the following strings: SQLITE_OK,
420 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
421 **
422 ** If this method is invoked with no arguments, the current authorization
423 ** callback string is returned.
424 */
425 case DB_AUTHORIZER: {
426 if( objc>3 ){
427 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
428 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000429 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000430 Tcl_AppendResult(interp, pDb->zAuth, 0);
431 }
432 }else{
433 char *zAuth;
434 int len;
435 if( pDb->zAuth ){
436 Tcl_Free(pDb->zAuth);
437 }
438 zAuth = Tcl_GetStringFromObj(objv[2], &len);
439 if( zAuth && len>0 ){
440 pDb->zAuth = Tcl_Alloc( len + 1 );
441 strcpy(pDb->zAuth, zAuth);
442 }else{
443 pDb->zAuth = 0;
444 }
445#ifndef SQLITE_OMIT_AUTHORIZATION
446 if( pDb->zAuth ){
447 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000448 sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
drhe22a3342003-04-22 20:30:37 +0000449 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000450 sqlite3_set_authorizer(pDb->db, 0, 0);
drhe22a3342003-04-22 20:30:37 +0000451 }
452#endif
453 }
454 break;
455 }
456
drhbec3f402000-08-04 13:49:02 +0000457 /* $db busy ?CALLBACK?
458 **
459 ** Invoke the given callback if an SQL statement attempts to open
460 ** a locked database file.
461 */
drh6d313162000-09-21 13:01:35 +0000462 case DB_BUSY: {
463 if( objc>3 ){
464 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000465 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000466 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000467 if( pDb->zBusy ){
468 Tcl_AppendResult(interp, pDb->zBusy, 0);
469 }
470 }else{
drh6d313162000-09-21 13:01:35 +0000471 char *zBusy;
472 int len;
drhbec3f402000-08-04 13:49:02 +0000473 if( pDb->zBusy ){
474 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000475 }
drh6d313162000-09-21 13:01:35 +0000476 zBusy = Tcl_GetStringFromObj(objv[2], &len);
477 if( zBusy && len>0 ){
478 pDb->zBusy = Tcl_Alloc( len + 1 );
479 strcpy(pDb->zBusy, zBusy);
480 }else{
481 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000482 }
483 if( pDb->zBusy ){
484 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000485 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000486 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000487 sqlite3_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000488 }
489 }
drh6d313162000-09-21 13:01:35 +0000490 break;
491 }
drhbec3f402000-08-04 13:49:02 +0000492
danielk1977348bb5d2003-10-18 09:37:26 +0000493 /* $db progress ?N CALLBACK?
494 **
495 ** Invoke the given callback every N virtual machine opcodes while executing
496 ** queries.
497 */
498 case DB_PROGRESS: {
499 if( objc==2 ){
500 if( pDb->zProgress ){
501 Tcl_AppendResult(interp, pDb->zProgress, 0);
502 }
503 }else if( objc==4 ){
504 char *zProgress;
505 int len;
506 int N;
507 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
508 return TCL_ERROR;
509 };
510 if( pDb->zProgress ){
511 Tcl_Free(pDb->zProgress);
512 }
513 zProgress = Tcl_GetStringFromObj(objv[3], &len);
514 if( zProgress && len>0 ){
515 pDb->zProgress = Tcl_Alloc( len + 1 );
516 strcpy(pDb->zProgress, zProgress);
517 }else{
518 pDb->zProgress = 0;
519 }
520#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
521 if( pDb->zProgress ){
522 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000523 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
danielk1977348bb5d2003-10-18 09:37:26 +0000524 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000525 sqlite3_progress_handler(pDb->db, 0, 0, 0);
danielk1977348bb5d2003-10-18 09:37:26 +0000526 }
527#endif
528 }else{
529 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
530 return TCL_ERROR;
531 }
532 break;
533 }
534
drhc8d30ac2002-04-12 10:08:59 +0000535 /*
536 ** $db changes
537 **
538 ** Return the number of rows that were modified, inserted, or deleted by
539 ** the most recent "eval".
540 */
541 case DB_CHANGES: {
542 Tcl_Obj *pResult;
543 int nChange;
544 if( objc!=2 ){
545 Tcl_WrongNumArgs(interp, 2, objv, "");
546 return TCL_ERROR;
547 }
danielk197730ccda12004-05-27 12:11:31 +0000548 /* nChange = sqlite3_changes(pDb->db); */
549 nChange = pDb->nChange;
drhc8d30ac2002-04-12 10:08:59 +0000550 pResult = Tcl_GetObjResult(interp);
551 Tcl_SetIntObj(pResult, nChange);
552 break;
553 }
554
rdcf146a772004-02-25 22:51:06 +0000555 /*
556 ** $db last_statement_changes
557 **
558 ** Return the number of rows that were modified, inserted, or deleted by
559 ** the last statment to complete execution (excluding changes due to
560 ** triggers)
561 */
562 case DB_LAST_STATEMENT_CHANGES: {
563 Tcl_Obj *pResult;
564 int lsChange;
565 if( objc!=2 ){
566 Tcl_WrongNumArgs(interp, 2, objv, "");
567 return TCL_ERROR;
568 }
danielk19776f8a5032004-05-10 10:34:51 +0000569 lsChange = sqlite3_last_statement_changes(pDb->db);
rdcf146a772004-02-25 22:51:06 +0000570 pResult = Tcl_GetObjResult(interp);
571 Tcl_SetIntObj(pResult, lsChange);
572 break;
573 }
574
drh75897232000-05-29 14:26:00 +0000575 /* $db close
576 **
577 ** Shutdown the database
578 */
drh6d313162000-09-21 13:01:35 +0000579 case DB_CLOSE: {
580 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
581 break;
582 }
drh75897232000-05-29 14:26:00 +0000583
drhaa940ea2004-01-15 02:44:03 +0000584 /* $db commit_hook ?CALLBACK?
585 **
586 ** Invoke the given callback just before committing every SQL transaction.
587 ** If the callback throws an exception or returns non-zero, then the
588 ** transaction is aborted. If CALLBACK is an empty string, the callback
589 ** is disabled.
590 */
591 case DB_COMMIT_HOOK: {
592 if( objc>3 ){
593 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
594 }else if( objc==2 ){
595 if( pDb->zCommit ){
596 Tcl_AppendResult(interp, pDb->zCommit, 0);
597 }
598 }else{
599 char *zCommit;
600 int len;
601 if( pDb->zCommit ){
602 Tcl_Free(pDb->zCommit);
603 }
604 zCommit = Tcl_GetStringFromObj(objv[2], &len);
605 if( zCommit && len>0 ){
606 pDb->zCommit = Tcl_Alloc( len + 1 );
607 strcpy(pDb->zCommit, zCommit);
608 }else{
609 pDb->zCommit = 0;
610 }
611 if( pDb->zCommit ){
612 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000613 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
drhaa940ea2004-01-15 02:44:03 +0000614 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000615 sqlite3_commit_hook(pDb->db, 0, 0);
drhaa940ea2004-01-15 02:44:03 +0000616 }
617 }
618 break;
619 }
620
drh75897232000-05-29 14:26:00 +0000621 /* $db complete SQL
622 **
623 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
624 ** additional lines of input are needed. This is similar to the
625 ** built-in "info complete" command of Tcl.
626 */
drh6d313162000-09-21 13:01:35 +0000627 case DB_COMPLETE: {
628 Tcl_Obj *pResult;
629 int isComplete;
630 if( objc!=3 ){
631 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000632 return TCL_ERROR;
633 }
danielk19776f8a5032004-05-10 10:34:51 +0000634 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
drh6d313162000-09-21 13:01:35 +0000635 pResult = Tcl_GetObjResult(interp);
636 Tcl_SetBooleanObj(pResult, isComplete);
637 break;
638 }
drhdcd997e2003-01-31 17:21:49 +0000639
640 /*
641 ** $db errorcode
642 **
643 ** Return the numeric error code that was returned by the most recent
danielk19776f8a5032004-05-10 10:34:51 +0000644 ** call to sqlite3_exec().
drhdcd997e2003-01-31 17:21:49 +0000645 */
646 case DB_ERRORCODE: {
647 Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc));
648 break;
649 }
drh75897232000-05-29 14:26:00 +0000650
651 /*
652 ** $db eval $sql ?array { ...code... }?
653 **
654 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000655 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000656 ** If "array" and "code" are omitted, then no callback is every invoked.
657 ** If "array" is an empty string, then the values are placed in variables
658 ** that have the same name as the fields extracted by the query.
659 */
drh6d313162000-09-21 13:01:35 +0000660 case DB_EVAL: {
danielk197730ccda12004-05-27 12:11:31 +0000661 char const *zSql;
662 char const *zLeft;
663 sqlite3_stmt *pStmt;
danielk1977ef2cb632004-05-29 02:37:19 +0000664
665 Tcl_Obj *pRet = Tcl_NewObj();
666 Tcl_IncrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000667
668 if( objc!=5 && objc!=3 ){
669 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
670 return TCL_ERROR;
671 }
672
673 pDb->nChange = 0;
674 zSql = Tcl_GetStringFromObj(objv[2], 0);
675 while( zSql[0] ){
676 int i;
677
678 if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000679 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000680 rc = TCL_ERROR;
681 break;
682 }
683
684 if( pStmt && objc==5 ){
685 Tcl_Obj *pColList = Tcl_NewObj();
686 Tcl_IncrRefCount(pColList);
687
688 for(i=0; i<sqlite3_column_count(pStmt); i++){
689 Tcl_ListObjAppendElement(interp, pColList,
danielk1977ef2cb632004-05-29 02:37:19 +0000690 dbTextToObj(sqlite3_column_name(pStmt, i))
danielk197730ccda12004-05-27 12:11:31 +0000691 );
692 }
693 Tcl_ObjSetVar2(interp,objv[3],Tcl_NewStringObj("*",-1),pColList,0);
694 }
695
696 while( pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
697 for(i=0; i<sqlite3_column_count(pStmt); i++){
698 Tcl_Obj *pVal;
699
700 /* Set pVal to contain the i'th column of this row. */
drh9c054832004-05-31 18:51:57 +0000701 if( SQLITE_BLOB!=sqlite3_column_type(pStmt, i) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000702 pVal = dbTextToObj(sqlite3_column_text(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000703 }else{
danielk19773fd0a732004-05-27 13:35:19 +0000704 int bytes = sqlite3_column_bytes(pStmt, i);
705 pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes);
danielk197730ccda12004-05-27 12:11:31 +0000706 }
707
708 if( objc==5 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000709 Tcl_Obj *pName = dbTextToObj(sqlite3_column_name(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000710 Tcl_IncrRefCount(pName);
711 if( !strcmp("", Tcl_GetString(objv[3])) ){
712 Tcl_ObjSetVar2(interp, pName, 0, pVal, 0);
713 }else{
714 Tcl_ObjSetVar2(interp, objv[3], pName, pVal, 0);
715 }
716 Tcl_DecrRefCount(pName);
717 }else{
danielk197730ccda12004-05-27 12:11:31 +0000718 Tcl_ListObjAppendElement(interp, pRet, pVal);
719 }
720 }
721
722 if( objc==5 ){
723 rc = Tcl_EvalObjEx(interp, objv[4], 0);
724 if( rc!=TCL_ERROR ) rc = TCL_OK;
725 }
726 }
727
728 if( pStmt && SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){
729 continue;
730 }
731
732 if( pStmt && SQLITE_OK!=sqlite3_errcode(pDb->db) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000733 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000734 rc = TCL_ERROR;
735 break;
736 }
737
738 pDb->nChange += sqlite3_changes(pDb->db);
739 zSql = zLeft;
740 }
741
danielk1977ef2cb632004-05-29 02:37:19 +0000742 if( rc==TCL_OK ){
danielk197730ccda12004-05-27 12:11:31 +0000743 Tcl_SetObjResult(interp, pRet);
danielk197730ccda12004-05-27 12:11:31 +0000744 }
danielk1977ef2cb632004-05-29 02:37:19 +0000745 Tcl_DecrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000746
747 break;
748 }
drhbec3f402000-08-04 13:49:02 +0000749
750 /*
drhcabb0812002-09-14 13:47:32 +0000751 ** $db function NAME SCRIPT
752 **
753 ** Create a new SQL function called NAME. Whenever that function is
754 ** called, invoke SCRIPT to evaluate the function.
755 */
756 case DB_FUNCTION: {
757 SqlFunc *pFunc;
758 char *zName;
759 char *zScript;
760 int nScript;
761 if( objc!=4 ){
762 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
763 return TCL_ERROR;
764 }
765 zName = Tcl_GetStringFromObj(objv[2], 0);
766 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
767 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
768 if( pFunc==0 ) return TCL_ERROR;
769 pFunc->interp = interp;
770 pFunc->pNext = pDb->pFunc;
771 pFunc->zScript = (char*)&pFunc[1];
772 strcpy(pFunc->zScript, zScript);
danielk197765904932004-05-26 06:18:37 +0000773 sqlite3_create_function(pDb->db, zName, -1, 0, 0, pFunc, tclSqlFunc, 0, 0);
drhcabb0812002-09-14 13:47:32 +0000774 break;
775 }
776
777 /*
drhaf9ff332002-01-16 21:00:27 +0000778 ** $db last_insert_rowid
779 **
780 ** Return an integer which is the ROWID for the most recent insert.
781 */
782 case DB_LAST_INSERT_ROWID: {
783 Tcl_Obj *pResult;
784 int rowid;
785 if( objc!=2 ){
786 Tcl_WrongNumArgs(interp, 2, objv, "");
787 return TCL_ERROR;
788 }
danielk19776f8a5032004-05-10 10:34:51 +0000789 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +0000790 pResult = Tcl_GetObjResult(interp);
791 Tcl_SetIntObj(pResult, rowid);
792 break;
793 }
794
795 /*
drh5d9d7572003-08-19 14:31:01 +0000796 ** $db onecolumn SQL
797 **
798 ** Return a single column from a single row of the given SQL query.
799 */
800 case DB_ONECOLUMN: {
drh5d9d7572003-08-19 14:31:01 +0000801 char *zSql;
802 char *zErrMsg = 0;
803 if( objc!=3 ){
804 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
805 return TCL_ERROR;
806 }
807 zSql = Tcl_GetStringFromObj(objv[2], 0);
danielk19776f8a5032004-05-10 10:34:51 +0000808 rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
drh5d9d7572003-08-19 14:31:01 +0000809 if( rc==SQLITE_ABORT ){
drh22fbcb82004-02-01 01:22:50 +0000810 rc = SQLITE_OK;
drh5d9d7572003-08-19 14:31:01 +0000811 }else if( zErrMsg ){
812 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
813 free(zErrMsg);
814 rc = TCL_ERROR;
815 }else if( rc!=SQLITE_OK ){
danielk1977f20b21c2004-05-31 23:56:42 +0000816 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh5d9d7572003-08-19 14:31:01 +0000817 rc = TCL_ERROR;
818 }
819 break;
820 }
821
822 /*
drh22fbcb82004-02-01 01:22:50 +0000823 ** $db rekey KEY
824 **
825 ** Change the encryption key on the currently open database.
826 */
827 case DB_REKEY: {
828 int nKey;
829 void *pKey;
830 if( objc!=3 ){
831 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
832 return TCL_ERROR;
833 }
834 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +0000835#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000836 rc = sqlite_rekey(pDb->db, pKey, nKey);
837 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000838 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh22fbcb82004-02-01 01:22:50 +0000839 rc = TCL_ERROR;
840 }
841#endif
842 break;
843 }
844
845 /*
drhbec3f402000-08-04 13:49:02 +0000846 ** $db timeout MILLESECONDS
847 **
848 ** Delay for the number of milliseconds specified when a file is locked.
849 */
drh6d313162000-09-21 13:01:35 +0000850 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000851 int ms;
drh6d313162000-09-21 13:01:35 +0000852 if( objc!=3 ){
853 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000854 return TCL_ERROR;
855 }
drh6d313162000-09-21 13:01:35 +0000856 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +0000857 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000858 break;
drh75897232000-05-29 14:26:00 +0000859 }
drhb5a20d32003-04-23 12:25:23 +0000860
861 /* $db trace ?CALLBACK?
862 **
863 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
864 ** that is executed. The text of the SQL is appended to CALLBACK before
865 ** it is executed.
866 */
867 case DB_TRACE: {
868 if( objc>3 ){
869 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
870 }else if( objc==2 ){
871 if( pDb->zTrace ){
872 Tcl_AppendResult(interp, pDb->zTrace, 0);
873 }
874 }else{
875 char *zTrace;
876 int len;
877 if( pDb->zTrace ){
878 Tcl_Free(pDb->zTrace);
879 }
880 zTrace = Tcl_GetStringFromObj(objv[2], &len);
881 if( zTrace && len>0 ){
882 pDb->zTrace = Tcl_Alloc( len + 1 );
883 strcpy(pDb->zTrace, zTrace);
884 }else{
885 pDb->zTrace = 0;
886 }
887 if( pDb->zTrace ){
888 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000889 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +0000890 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000891 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +0000892 }
893 }
894 break;
895 }
896
danielk19770202b292004-06-09 09:55:16 +0000897 /*
898 ** $db collate NAME SCRIPT
899 **
900 ** Create a new SQL collation function called NAME. Whenever
901 ** that function is called, invoke SCRIPT to evaluate the function.
902 */
903 case DB_COLLATE: {
904 SqlCollate *pCollate;
905 char *zName;
906 char *zScript;
907 int nScript;
908 if( objc!=4 ){
909 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
910 return TCL_ERROR;
911 }
912 zName = Tcl_GetStringFromObj(objv[2], 0);
913 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
914 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
915 if( pCollate==0 ) return TCL_ERROR;
916 pCollate->interp = interp;
917 pCollate->pNext = pDb->pCollate;
918 pCollate->zScript = (char*)&pCollate[1];
919 strcpy(pCollate->zScript, zScript);
920 if( sqlite3_create_collation(pDb->db, zName, 0, pCollate, tclSqlCollate) ){
921 return TCL_ERROR;
922 }
923 break;
924 }
925
drh6d313162000-09-21 13:01:35 +0000926 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +0000927 return rc;
drh75897232000-05-29 14:26:00 +0000928}
929
930/*
drh22fbcb82004-02-01 01:22:50 +0000931** sqlite DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +0000932**
933** This is the main Tcl command. When the "sqlite" Tcl command is
934** invoked, this routine runs to process that command.
935**
936** The first argument, DBNAME, is an arbitrary name for a new
937** database connection. This command creates a new command named
938** DBNAME that is used to control that connection. The database
939** connection is deleted when the DBNAME command is deleted.
940**
941** The second argument is the name of the directory that contains
942** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000943**
944** For testing purposes, we also support the following:
945**
946** sqlite -encoding
947**
948** Return the encoding used by LIKE and GLOB operators. Choices
949** are UTF-8 and iso8859.
950**
drh647cb0e2002-11-04 19:32:25 +0000951** sqlite -version
952**
953** Return the version number of the SQLite library.
954**
drhfbc3eab2001-04-06 16:13:42 +0000955** sqlite -tcl-uses-utf
956**
957** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
958** not. Used by tests to make sure the library was compiled
959** correctly.
drh75897232000-05-29 14:26:00 +0000960*/
drh22fbcb82004-02-01 01:22:50 +0000961static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000962 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +0000963 void *pKey = 0;
964 int nKey = 0;
965 const char *zArg;
drh75897232000-05-29 14:26:00 +0000966 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +0000967 const char *zFile;
drh06b27182002-06-26 20:06:05 +0000968 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +0000969 if( objc==2 ){
970 zArg = Tcl_GetStringFromObj(objv[1], 0);
drh22fbcb82004-02-01 01:22:50 +0000971 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000972 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +0000973 return TCL_OK;
974 }
drh9eb9e262004-02-11 02:18:05 +0000975 if( strcmp(zArg,"-has-codec")==0 ){
976#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000977 Tcl_AppendResult(interp,"1",0);
978#else
979 Tcl_AppendResult(interp,"0",0);
980#endif
981 return TCL_OK;
982 }
983 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +0000984#ifdef TCL_UTF_MAX
985 Tcl_AppendResult(interp,"1",0);
986#else
987 Tcl_AppendResult(interp,"0",0);
988#endif
989 return TCL_OK;
990 }
991 }
drh22fbcb82004-02-01 01:22:50 +0000992 if( objc==5 || objc==6 ){
993 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
994 if( strcmp(zArg,"-key")==0 ){
995 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
996 objc -= 2;
997 }
998 }
999 if( objc!=3 && objc!=4 ){
1000 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +00001001#ifdef SQLITE_HAS_CODEC
1002 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +00001003#else
1004 "HANDLE FILENAME ?MODE?"
1005#endif
1006 );
drh75897232000-05-29 14:26:00 +00001007 return TCL_ERROR;
1008 }
drh75897232000-05-29 14:26:00 +00001009 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001010 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001011 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001012 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1013 return TCL_ERROR;
1014 }
1015 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00001016 zFile = Tcl_GetStringFromObj(objv[2], 0);
drh9eb9e262004-02-11 02:18:05 +00001017#ifdef SQLITE_HAS_CODEC
danielk19776f8a5032004-05-10 10:34:51 +00001018 p->db = sqlite3_open_encrypted(zFile, pKey, nKey, 0, &zErrMsg);
drheb8ed702004-02-11 10:37:23 +00001019#else
danielk19774f057f92004-06-08 00:02:33 +00001020 sqlite3_open(zFile, &p->db);
danielk197780290862004-05-22 09:21:21 +00001021 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
1022 zErrMsg = strdup(sqlite3_errmsg(p->db));
1023 sqlite3_close(p->db);
1024 p->db = 0;
1025 }
drheb8ed702004-02-11 10:37:23 +00001026#endif
drhbec3f402000-08-04 13:49:02 +00001027 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001028 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001029 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001030 free(zErrMsg);
1031 return TCL_ERROR;
1032 }
drh22fbcb82004-02-01 01:22:50 +00001033 zArg = Tcl_GetStringFromObj(objv[1], 0);
1034 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001035
drh06b27182002-06-26 20:06:05 +00001036 /* The return value is the value of the sqlite* pointer
1037 */
1038 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001039 if( strncmp(zBuf,"0x",2) ){
1040 sprintf(zBuf, "0x%p", p->db);
1041 }
drh06b27182002-06-26 20:06:05 +00001042 Tcl_AppendResult(interp, zBuf, 0);
1043
drhc22bd472002-05-10 13:14:07 +00001044 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001045 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001046 */
drh28b4e482002-03-11 02:06:13 +00001047#ifdef SQLITE_TEST
1048 {
drhc22bd472002-05-10 13:14:07 +00001049 extern void Md5_Register(sqlite*);
1050 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +00001051 }
drh28b4e482002-03-11 02:06:13 +00001052#endif
drh75897232000-05-29 14:26:00 +00001053 return TCL_OK;
1054}
1055
1056/*
drh90ca9752001-09-28 17:47:14 +00001057** Provide a dummy Tcl_InitStubs if we are using this as a static
1058** library.
1059*/
1060#ifndef USE_TCL_STUBS
1061# undef Tcl_InitStubs
1062# define Tcl_InitStubs(a,b,c)
1063#endif
1064
1065/*
drh75897232000-05-29 14:26:00 +00001066** Initialize this module.
1067**
1068** This Tcl module contains only a single new Tcl command named "sqlite".
1069** (Hence there is no namespace. There is no point in using a namespace
1070** if the extension only supplies one new name!) The "sqlite" command is
1071** used to open a new SQLite database. See the DbMain() routine above
1072** for additional information.
1073*/
1074int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001075 Tcl_InitStubs(interp, "8.0", 0);
drh22fbcb82004-02-01 01:22:50 +00001076 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001077 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh90ca9752001-09-28 17:47:14 +00001078 return TCL_OK;
1079}
1080int Tclsqlite_Init(Tcl_Interp *interp){
1081 Tcl_InitStubs(interp, "8.0", 0);
drh22fbcb82004-02-01 01:22:50 +00001082 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001083 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh75897232000-05-29 14:26:00 +00001084 return TCL_OK;
1085}
1086int Sqlite_SafeInit(Tcl_Interp *interp){
1087 return TCL_OK;
1088}
drh90ca9752001-09-28 17:47:14 +00001089int Tclsqlite_SafeInit(Tcl_Interp *interp){
1090 return TCL_OK;
1091}
drh75897232000-05-29 14:26:00 +00001092
drh3cebbde2000-10-19 14:59:27 +00001093#if 0
drh75897232000-05-29 14:26:00 +00001094/*
1095** If compiled using mktclapp, this routine runs to initialize
1096** everything.
1097*/
1098int Et_AppInit(Tcl_Interp *interp){
1099 return Sqlite_Init(interp);
1100}
drh3cebbde2000-10-19 14:59:27 +00001101#endif
drh348784e2000-05-29 20:41:49 +00001102
1103/*
1104** If the macro TCLSH is defined and is one, then put in code for the
1105** "main" routine that will initialize Tcl.
1106*/
1107#if defined(TCLSH) && TCLSH==1
1108static char zMainloop[] =
1109 "set line {}\n"
1110 "while {![eof stdin]} {\n"
1111 "if {$line!=\"\"} {\n"
1112 "puts -nonewline \"> \"\n"
1113 "} else {\n"
1114 "puts -nonewline \"% \"\n"
1115 "}\n"
1116 "flush stdout\n"
1117 "append line [gets stdin]\n"
1118 "if {[info complete $line]} {\n"
1119 "if {[catch {uplevel #0 $line} result]} {\n"
1120 "puts stderr \"Error: $result\"\n"
1121 "} elseif {$result!=\"\"} {\n"
1122 "puts $result\n"
1123 "}\n"
1124 "set line {}\n"
1125 "} else {\n"
1126 "append line \\n\n"
1127 "}\n"
1128 "}\n"
1129;
1130
1131#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1132int TCLSH_MAIN(int argc, char **argv){
1133 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001134 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001135 interp = Tcl_CreateInterp();
danielk19774adee202004-05-08 08:23:19 +00001136 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001137#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001138 {
1139 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001140 extern int Sqlitetest2_Init(Tcl_Interp*);
1141 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001142 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001143 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001144 extern int Md5_Init(Tcl_Interp*);
danielk19776490beb2004-05-11 06:17:21 +00001145 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001146 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001147 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00001148 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00001149 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001150 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001151 }
1152#endif
drh348784e2000-05-29 20:41:49 +00001153 if( argc>=2 ){
1154 int i;
1155 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1156 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1157 for(i=2; i<argc; i++){
1158 Tcl_SetVar(interp, "argv", argv[i],
1159 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1160 }
1161 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001162 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001163 if( zInfo==0 ) zInfo = interp->result;
1164 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001165 return 1;
1166 }
1167 }else{
1168 Tcl_GlobalEval(interp, zMainloop);
1169 }
1170 return 0;
1171}
1172#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001173
1174#endif /* !defined(NO_TCL) */