blob: 0fa9fe60e3ab0b1b1ced3946dc463d6f10d13eeb [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**
danielk1977f20b21c2004-05-31 23:56:42 +000014** $Id: tclsqlite.c,v 1.78 2004/05/31 23:56:43 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/*
drhbec3f402000-08-04 13:49:02 +000046** There is one instance of this structure for each SQLite database
47** that has been opened by the SQLite TCL interface.
48*/
49typedef struct SqliteDb SqliteDb;
50struct SqliteDb {
51 sqlite *db; /* The "real" database structure */
52 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000053 char *zBusy; /* The busy callback routine */
drhaa940ea2004-01-15 02:44:03 +000054 char *zCommit; /* The commit hook callback routine */
drhb5a20d32003-04-23 12:25:23 +000055 char *zTrace; /* The trace callback routine */
danielk1977348bb5d2003-10-18 09:37:26 +000056 char *zProgress; /* The progress callback routine */
drhe22a3342003-04-22 20:30:37 +000057 char *zAuth; /* The authorization callback routine */
drhcabb0812002-09-14 13:47:32 +000058 SqlFunc *pFunc; /* List of SQL functions */
danielk19776f8a5032004-05-10 10:34:51 +000059 int rc; /* Return code of most recent sqlite3_exec() */
danielk197730ccda12004-05-27 12:11:31 +000060 int nChange; /* Database changes for the most recent eval */
drhbec3f402000-08-04 13:49:02 +000061};
62
63/*
drh75897232000-05-29 14:26:00 +000064** An instance of this structure passes information thru the sqlite
65** logic from the original TCL command into the callback routine.
66*/
67typedef struct CallbackData CallbackData;
68struct CallbackData {
69 Tcl_Interp *interp; /* The TCL interpreter */
70 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000071 Tcl_Obj *pCode; /* The code to execute for each row */
drhce927062001-11-09 13:41:09 +000072 int once; /* Set for first callback only */
drh960e8c62001-04-03 16:53:21 +000073 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000074 int nColName; /* Number of entries in the azColName[] array */
75 char **azColName; /* Column names translated to UTF-8 */
drh98808ba2001-10-18 12:34:46 +000076};
drh297ecf12001-04-05 15:57:13 +000077
drh6d313162000-09-21 13:01:35 +000078/*
drh5d9d7572003-08-19 14:31:01 +000079** This is a second alternative callback for database queries. A the
80** first column of the first row of the result is made the TCL result.
81*/
82static int DbEvalCallback3(
83 void *clientData, /* An instance of CallbackData */
84 int nCol, /* Number of columns in the result */
85 char ** azCol, /* Data for each column */
86 char ** azN /* Name for each column */
87){
88 Tcl_Interp *interp = (Tcl_Interp*)clientData;
89 Tcl_Obj *pElem;
90 if( azCol==0 ) return 1;
91 if( nCol==0 ) return 1;
92#ifdef UTF_TRANSLATION_NEEDED
93 {
94 Tcl_DString dCol;
95 Tcl_DStringInit(&dCol);
96 Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol);
97 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
98 Tcl_DStringFree(&dCol);
99 }
100#else
101 pElem = Tcl_NewStringObj(azCol[0], -1);
102#endif
103 Tcl_SetObjResult(interp, pElem);
104 return 1;
105}
106
107/*
drh75897232000-05-29 14:26:00 +0000108** Called when the command is deleted.
109*/
110static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000111 SqliteDb *pDb = (SqliteDb*)db;
danielk19776f8a5032004-05-10 10:34:51 +0000112 sqlite3_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000113 while( pDb->pFunc ){
114 SqlFunc *pFunc = pDb->pFunc;
115 pDb->pFunc = pFunc->pNext;
116 Tcl_Free((char*)pFunc);
117 }
drhbec3f402000-08-04 13:49:02 +0000118 if( pDb->zBusy ){
119 Tcl_Free(pDb->zBusy);
120 }
drhb5a20d32003-04-23 12:25:23 +0000121 if( pDb->zTrace ){
122 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000123 }
drhe22a3342003-04-22 20:30:37 +0000124 if( pDb->zAuth ){
125 Tcl_Free(pDb->zAuth);
126 }
drhbec3f402000-08-04 13:49:02 +0000127 Tcl_Free((char*)pDb);
128}
129
130/*
131** This routine is called when a database file is locked while trying
132** to execute SQL.
133*/
134static int DbBusyHandler(void *cd, const char *zTable, int nTries){
135 SqliteDb *pDb = (SqliteDb*)cd;
136 int rc;
137 char zVal[30];
138 char *zCmd;
drhbec3f402000-08-04 13:49:02 +0000139 Tcl_DString cmd;
140
141 Tcl_DStringInit(&cmd);
142 Tcl_DStringAppend(&cmd, pDb->zBusy, -1);
143 Tcl_DStringAppendElement(&cmd, zTable);
144 sprintf(zVal, " %d", nTries);
145 Tcl_DStringAppend(&cmd, zVal, -1);
146 zCmd = Tcl_DStringValue(&cmd);
147 rc = Tcl_Eval(pDb->interp, zCmd);
148 Tcl_DStringFree(&cmd);
149 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
150 return 0;
151 }
152 return 1;
drh75897232000-05-29 14:26:00 +0000153}
154
155/*
danielk1977348bb5d2003-10-18 09:37:26 +0000156** This routine is invoked as the 'progress callback' for the database.
157*/
158static int DbProgressHandler(void *cd){
159 SqliteDb *pDb = (SqliteDb*)cd;
160 int rc;
161
162 assert( pDb->zProgress );
163 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
164 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
165 return 1;
166 }
167 return 0;
168}
169
170/*
drhb5a20d32003-04-23 12:25:23 +0000171** This routine is called by the SQLite trace handler whenever a new
172** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000173*/
drhb5a20d32003-04-23 12:25:23 +0000174static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000175 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000176 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000177
drhb5a20d32003-04-23 12:25:23 +0000178 Tcl_DStringInit(&str);
179 Tcl_DStringAppend(&str, pDb->zTrace, -1);
180 Tcl_DStringAppendElement(&str, zSql);
181 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
182 Tcl_DStringFree(&str);
183 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000184}
185
186/*
drhaa940ea2004-01-15 02:44:03 +0000187** This routine is called when a transaction is committed. The
188** TCL script in pDb->zCommit is executed. If it returns non-zero or
189** if it throws an exception, the transaction is rolled back instead
190** of being committed.
191*/
192static int DbCommitHandler(void *cd){
193 SqliteDb *pDb = (SqliteDb*)cd;
194 int rc;
195
196 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
197 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
198 return 1;
199 }
200 return 0;
201}
202
203/*
drhcabb0812002-09-14 13:47:32 +0000204** This routine is called to evaluate an SQL function implemented
205** using TCL script.
206*/
danielk19770ae8b832004-05-25 12:05:56 +0000207static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk19776f8a5032004-05-10 10:34:51 +0000208 SqlFunc *p = sqlite3_user_data(context);
drhcabb0812002-09-14 13:47:32 +0000209 Tcl_DString cmd;
210 int i;
211 int rc;
212
213 Tcl_DStringInit(&cmd);
214 Tcl_DStringAppend(&cmd, p->zScript, -1);
215 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000216 if( SQLITE_NULL==sqlite3_value_type(argv[i]) ){
danielk197751ad0ec2004-05-24 12:39:02 +0000217 Tcl_DStringAppendElement(&cmd, "");
218 }else{
drh4f26d6c2004-05-26 23:25:30 +0000219 Tcl_DStringAppendElement(&cmd, sqlite3_value_text(argv[i]));
danielk197751ad0ec2004-05-24 12:39:02 +0000220 }
drhcabb0812002-09-14 13:47:32 +0000221 }
222 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
223 if( rc ){
danielk19777e18c252004-05-25 11:47:24 +0000224 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhcabb0812002-09-14 13:47:32 +0000225 }else{
danielk19777e18c252004-05-25 11:47:24 +0000226 sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1, 1);
drhcabb0812002-09-14 13:47:32 +0000227 }
228}
drhe22a3342003-04-22 20:30:37 +0000229#ifndef SQLITE_OMIT_AUTHORIZATION
230/*
231** This is the authentication function. It appends the authentication
232** type code and the two arguments to zCmd[] then invokes the result
233** on the interpreter. The reply is examined to determine if the
234** authentication fails or succeeds.
235*/
236static int auth_callback(
237 void *pArg,
238 int code,
239 const char *zArg1,
240 const char *zArg2,
241 const char *zArg3,
242 const char *zArg4
243){
244 char *zCode;
245 Tcl_DString str;
246 int rc;
247 const char *zReply;
248 SqliteDb *pDb = (SqliteDb*)pArg;
249
250 switch( code ){
251 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
252 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
253 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
254 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
255 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
256 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
257 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
258 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
259 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
260 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
261 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
262 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
263 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
264 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
265 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
266 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
267 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
268 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
269 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
270 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
271 case SQLITE_READ : zCode="SQLITE_READ"; break;
272 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
273 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
274 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000275 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
276 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
drhe22a3342003-04-22 20:30:37 +0000277 default : zCode="????"; break;
278 }
279 Tcl_DStringInit(&str);
280 Tcl_DStringAppend(&str, pDb->zAuth, -1);
281 Tcl_DStringAppendElement(&str, zCode);
282 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
283 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
284 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
285 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
286 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
287 Tcl_DStringFree(&str);
288 zReply = Tcl_GetStringResult(pDb->interp);
289 if( strcmp(zReply,"SQLITE_OK")==0 ){
290 rc = SQLITE_OK;
291 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
292 rc = SQLITE_DENY;
293 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
294 rc = SQLITE_IGNORE;
295 }else{
296 rc = 999;
297 }
298 return rc;
299}
300#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000301
302/*
danielk1977ef2cb632004-05-29 02:37:19 +0000303** zText is a pointer to text obtained via an sqlite3_result_text()
304** or similar interface. This routine returns a Tcl string object,
305** reference count set to 0, containing the text. If a translation
306** between iso8859 and UTF-8 is required, it is preformed.
307*/
308static Tcl_Obj *dbTextToObj(char const *zText){
309 Tcl_Obj *pVal;
310#ifdef UTF_TRANSLATION_NEEDED
311 Tcl_DString dCol;
312 Tcl_DStringInit(&dCol);
313 Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol);
314 pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
315 Tcl_DStringFree(&dCol);
316#else
317 pVal = Tcl_NewStringObj(zText, -1);
318#endif
319 return pVal;
320}
321
322/*
drh75897232000-05-29 14:26:00 +0000323** The "sqlite" command below creates a new Tcl command for each
324** connection it opens to an SQLite database. This routine is invoked
325** whenever one of those connection-specific commands is executed
326** in Tcl. For example, if you run Tcl code like this:
327**
328** sqlite db1 "my_database"
329** db1 close
330**
331** The first command opens a connection to the "my_database" database
332** and calls that connection "db1". The second command causes this
333** subroutine to be invoked.
334*/
drh6d313162000-09-21 13:01:35 +0000335static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000336 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000337 int choice;
drh22fbcb82004-02-01 01:22:50 +0000338 int rc = TCL_OK;
drh0de8c112002-07-06 16:32:14 +0000339 static const char *DB_strs[] = {
rdcf146a772004-02-25 22:51:06 +0000340 "authorizer", "busy", "changes",
341 "close", "commit_hook", "complete",
342 "errorcode", "eval", "function",
343 "last_insert_rowid", "last_statement_changes", "onecolumn",
344 "progress", "rekey", "timeout",
345 "trace",
drh22fbcb82004-02-01 01:22:50 +0000346 0
drh6d313162000-09-21 13:01:35 +0000347 };
drh411995d2002-06-25 19:31:18 +0000348 enum DB_enum {
rdcf146a772004-02-25 22:51:06 +0000349 DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
350 DB_CLOSE, DB_COMMIT_HOOK, DB_COMPLETE,
351 DB_ERRORCODE, DB_EVAL, DB_FUNCTION,
352 DB_LAST_INSERT_ROWID, DB_LAST_STATEMENT_CHANGES, DB_ONECOLUMN,
353 DB_PROGRESS, DB_REKEY, DB_TIMEOUT,
354 DB_TRACE
drh6d313162000-09-21 13:01:35 +0000355 };
356
357 if( objc<2 ){
358 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000359 return TCL_ERROR;
360 }
drh411995d2002-06-25 19:31:18 +0000361 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000362 return TCL_ERROR;
363 }
364
drh411995d2002-06-25 19:31:18 +0000365 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000366
drhe22a3342003-04-22 20:30:37 +0000367 /* $db authorizer ?CALLBACK?
368 **
369 ** Invoke the given callback to authorize each SQL operation as it is
370 ** compiled. 5 arguments are appended to the callback before it is
371 ** invoked:
372 **
373 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
374 ** (2) First descriptive name (depends on authorization type)
375 ** (3) Second descriptive name
376 ** (4) Name of the database (ex: "main", "temp")
377 ** (5) Name of trigger that is doing the access
378 **
379 ** The callback should return on of the following strings: SQLITE_OK,
380 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
381 **
382 ** If this method is invoked with no arguments, the current authorization
383 ** callback string is returned.
384 */
385 case DB_AUTHORIZER: {
386 if( objc>3 ){
387 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
388 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000389 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000390 Tcl_AppendResult(interp, pDb->zAuth, 0);
391 }
392 }else{
393 char *zAuth;
394 int len;
395 if( pDb->zAuth ){
396 Tcl_Free(pDb->zAuth);
397 }
398 zAuth = Tcl_GetStringFromObj(objv[2], &len);
399 if( zAuth && len>0 ){
400 pDb->zAuth = Tcl_Alloc( len + 1 );
401 strcpy(pDb->zAuth, zAuth);
402 }else{
403 pDb->zAuth = 0;
404 }
405#ifndef SQLITE_OMIT_AUTHORIZATION
406 if( pDb->zAuth ){
407 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000408 sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
drhe22a3342003-04-22 20:30:37 +0000409 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000410 sqlite3_set_authorizer(pDb->db, 0, 0);
drhe22a3342003-04-22 20:30:37 +0000411 }
412#endif
413 }
414 break;
415 }
416
drhbec3f402000-08-04 13:49:02 +0000417 /* $db busy ?CALLBACK?
418 **
419 ** Invoke the given callback if an SQL statement attempts to open
420 ** a locked database file.
421 */
drh6d313162000-09-21 13:01:35 +0000422 case DB_BUSY: {
423 if( objc>3 ){
424 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000425 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000426 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000427 if( pDb->zBusy ){
428 Tcl_AppendResult(interp, pDb->zBusy, 0);
429 }
430 }else{
drh6d313162000-09-21 13:01:35 +0000431 char *zBusy;
432 int len;
drhbec3f402000-08-04 13:49:02 +0000433 if( pDb->zBusy ){
434 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000435 }
drh6d313162000-09-21 13:01:35 +0000436 zBusy = Tcl_GetStringFromObj(objv[2], &len);
437 if( zBusy && len>0 ){
438 pDb->zBusy = Tcl_Alloc( len + 1 );
439 strcpy(pDb->zBusy, zBusy);
440 }else{
441 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000442 }
443 if( pDb->zBusy ){
444 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000445 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000446 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000447 sqlite3_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000448 }
449 }
drh6d313162000-09-21 13:01:35 +0000450 break;
451 }
drhbec3f402000-08-04 13:49:02 +0000452
danielk1977348bb5d2003-10-18 09:37:26 +0000453 /* $db progress ?N CALLBACK?
454 **
455 ** Invoke the given callback every N virtual machine opcodes while executing
456 ** queries.
457 */
458 case DB_PROGRESS: {
459 if( objc==2 ){
460 if( pDb->zProgress ){
461 Tcl_AppendResult(interp, pDb->zProgress, 0);
462 }
463 }else if( objc==4 ){
464 char *zProgress;
465 int len;
466 int N;
467 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
468 return TCL_ERROR;
469 };
470 if( pDb->zProgress ){
471 Tcl_Free(pDb->zProgress);
472 }
473 zProgress = Tcl_GetStringFromObj(objv[3], &len);
474 if( zProgress && len>0 ){
475 pDb->zProgress = Tcl_Alloc( len + 1 );
476 strcpy(pDb->zProgress, zProgress);
477 }else{
478 pDb->zProgress = 0;
479 }
480#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
481 if( pDb->zProgress ){
482 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000483 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
danielk1977348bb5d2003-10-18 09:37:26 +0000484 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000485 sqlite3_progress_handler(pDb->db, 0, 0, 0);
danielk1977348bb5d2003-10-18 09:37:26 +0000486 }
487#endif
488 }else{
489 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
490 return TCL_ERROR;
491 }
492 break;
493 }
494
drhc8d30ac2002-04-12 10:08:59 +0000495 /*
496 ** $db changes
497 **
498 ** Return the number of rows that were modified, inserted, or deleted by
499 ** the most recent "eval".
500 */
501 case DB_CHANGES: {
502 Tcl_Obj *pResult;
503 int nChange;
504 if( objc!=2 ){
505 Tcl_WrongNumArgs(interp, 2, objv, "");
506 return TCL_ERROR;
507 }
danielk197730ccda12004-05-27 12:11:31 +0000508 /* nChange = sqlite3_changes(pDb->db); */
509 nChange = pDb->nChange;
drhc8d30ac2002-04-12 10:08:59 +0000510 pResult = Tcl_GetObjResult(interp);
511 Tcl_SetIntObj(pResult, nChange);
512 break;
513 }
514
rdcf146a772004-02-25 22:51:06 +0000515 /*
516 ** $db last_statement_changes
517 **
518 ** Return the number of rows that were modified, inserted, or deleted by
519 ** the last statment to complete execution (excluding changes due to
520 ** triggers)
521 */
522 case DB_LAST_STATEMENT_CHANGES: {
523 Tcl_Obj *pResult;
524 int lsChange;
525 if( objc!=2 ){
526 Tcl_WrongNumArgs(interp, 2, objv, "");
527 return TCL_ERROR;
528 }
danielk19776f8a5032004-05-10 10:34:51 +0000529 lsChange = sqlite3_last_statement_changes(pDb->db);
rdcf146a772004-02-25 22:51:06 +0000530 pResult = Tcl_GetObjResult(interp);
531 Tcl_SetIntObj(pResult, lsChange);
532 break;
533 }
534
drh75897232000-05-29 14:26:00 +0000535 /* $db close
536 **
537 ** Shutdown the database
538 */
drh6d313162000-09-21 13:01:35 +0000539 case DB_CLOSE: {
540 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
541 break;
542 }
drh75897232000-05-29 14:26:00 +0000543
drhaa940ea2004-01-15 02:44:03 +0000544 /* $db commit_hook ?CALLBACK?
545 **
546 ** Invoke the given callback just before committing every SQL transaction.
547 ** If the callback throws an exception or returns non-zero, then the
548 ** transaction is aborted. If CALLBACK is an empty string, the callback
549 ** is disabled.
550 */
551 case DB_COMMIT_HOOK: {
552 if( objc>3 ){
553 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
554 }else if( objc==2 ){
555 if( pDb->zCommit ){
556 Tcl_AppendResult(interp, pDb->zCommit, 0);
557 }
558 }else{
559 char *zCommit;
560 int len;
561 if( pDb->zCommit ){
562 Tcl_Free(pDb->zCommit);
563 }
564 zCommit = Tcl_GetStringFromObj(objv[2], &len);
565 if( zCommit && len>0 ){
566 pDb->zCommit = Tcl_Alloc( len + 1 );
567 strcpy(pDb->zCommit, zCommit);
568 }else{
569 pDb->zCommit = 0;
570 }
571 if( pDb->zCommit ){
572 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000573 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
drhaa940ea2004-01-15 02:44:03 +0000574 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000575 sqlite3_commit_hook(pDb->db, 0, 0);
drhaa940ea2004-01-15 02:44:03 +0000576 }
577 }
578 break;
579 }
580
drh75897232000-05-29 14:26:00 +0000581 /* $db complete SQL
582 **
583 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
584 ** additional lines of input are needed. This is similar to the
585 ** built-in "info complete" command of Tcl.
586 */
drh6d313162000-09-21 13:01:35 +0000587 case DB_COMPLETE: {
588 Tcl_Obj *pResult;
589 int isComplete;
590 if( objc!=3 ){
591 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000592 return TCL_ERROR;
593 }
danielk19776f8a5032004-05-10 10:34:51 +0000594 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
drh6d313162000-09-21 13:01:35 +0000595 pResult = Tcl_GetObjResult(interp);
596 Tcl_SetBooleanObj(pResult, isComplete);
597 break;
598 }
drhdcd997e2003-01-31 17:21:49 +0000599
600 /*
601 ** $db errorcode
602 **
603 ** Return the numeric error code that was returned by the most recent
danielk19776f8a5032004-05-10 10:34:51 +0000604 ** call to sqlite3_exec().
drhdcd997e2003-01-31 17:21:49 +0000605 */
606 case DB_ERRORCODE: {
607 Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc));
608 break;
609 }
drh75897232000-05-29 14:26:00 +0000610
611 /*
612 ** $db eval $sql ?array { ...code... }?
613 **
614 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000615 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000616 ** If "array" and "code" are omitted, then no callback is every invoked.
617 ** If "array" is an empty string, then the values are placed in variables
618 ** that have the same name as the fields extracted by the query.
619 */
drh6d313162000-09-21 13:01:35 +0000620 case DB_EVAL: {
danielk197730ccda12004-05-27 12:11:31 +0000621 char const *zSql;
622 char const *zLeft;
623 sqlite3_stmt *pStmt;
danielk1977ef2cb632004-05-29 02:37:19 +0000624
625 Tcl_Obj *pRet = Tcl_NewObj();
626 Tcl_IncrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000627
628 if( objc!=5 && objc!=3 ){
629 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
630 return TCL_ERROR;
631 }
632
633 pDb->nChange = 0;
634 zSql = Tcl_GetStringFromObj(objv[2], 0);
635 while( zSql[0] ){
636 int i;
637
638 if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000639 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000640 rc = TCL_ERROR;
641 break;
642 }
643
644 if( pStmt && objc==5 ){
645 Tcl_Obj *pColList = Tcl_NewObj();
646 Tcl_IncrRefCount(pColList);
647
648 for(i=0; i<sqlite3_column_count(pStmt); i++){
649 Tcl_ListObjAppendElement(interp, pColList,
danielk1977ef2cb632004-05-29 02:37:19 +0000650 dbTextToObj(sqlite3_column_name(pStmt, i))
danielk197730ccda12004-05-27 12:11:31 +0000651 );
652 }
653 Tcl_ObjSetVar2(interp,objv[3],Tcl_NewStringObj("*",-1),pColList,0);
654 }
655
656 while( pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
657 for(i=0; i<sqlite3_column_count(pStmt); i++){
658 Tcl_Obj *pVal;
659
660 /* Set pVal to contain the i'th column of this row. */
drh9c054832004-05-31 18:51:57 +0000661 if( SQLITE_BLOB!=sqlite3_column_type(pStmt, i) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000662 pVal = dbTextToObj(sqlite3_column_text(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000663 }else{
danielk19773fd0a732004-05-27 13:35:19 +0000664 int bytes = sqlite3_column_bytes(pStmt, i);
665 pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes);
danielk197730ccda12004-05-27 12:11:31 +0000666 }
667
668 if( objc==5 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000669 Tcl_Obj *pName = dbTextToObj(sqlite3_column_name(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000670 Tcl_IncrRefCount(pName);
671 if( !strcmp("", Tcl_GetString(objv[3])) ){
672 Tcl_ObjSetVar2(interp, pName, 0, pVal, 0);
673 }else{
674 Tcl_ObjSetVar2(interp, objv[3], pName, pVal, 0);
675 }
676 Tcl_DecrRefCount(pName);
677 }else{
danielk197730ccda12004-05-27 12:11:31 +0000678 Tcl_ListObjAppendElement(interp, pRet, pVal);
679 }
680 }
681
682 if( objc==5 ){
683 rc = Tcl_EvalObjEx(interp, objv[4], 0);
684 if( rc!=TCL_ERROR ) rc = TCL_OK;
685 }
686 }
687
688 if( pStmt && SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){
689 continue;
690 }
691
692 if( pStmt && SQLITE_OK!=sqlite3_errcode(pDb->db) ){
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 pDb->nChange += sqlite3_changes(pDb->db);
699 zSql = zLeft;
700 }
701
danielk1977ef2cb632004-05-29 02:37:19 +0000702 if( rc==TCL_OK ){
danielk197730ccda12004-05-27 12:11:31 +0000703 Tcl_SetObjResult(interp, pRet);
danielk197730ccda12004-05-27 12:11:31 +0000704 }
danielk1977ef2cb632004-05-29 02:37:19 +0000705 Tcl_DecrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000706
707 break;
708 }
drhbec3f402000-08-04 13:49:02 +0000709
710 /*
drhcabb0812002-09-14 13:47:32 +0000711 ** $db function NAME SCRIPT
712 **
713 ** Create a new SQL function called NAME. Whenever that function is
714 ** called, invoke SCRIPT to evaluate the function.
715 */
716 case DB_FUNCTION: {
717 SqlFunc *pFunc;
718 char *zName;
719 char *zScript;
720 int nScript;
721 if( objc!=4 ){
722 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
723 return TCL_ERROR;
724 }
725 zName = Tcl_GetStringFromObj(objv[2], 0);
726 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
727 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
728 if( pFunc==0 ) return TCL_ERROR;
729 pFunc->interp = interp;
730 pFunc->pNext = pDb->pFunc;
731 pFunc->zScript = (char*)&pFunc[1];
732 strcpy(pFunc->zScript, zScript);
danielk197765904932004-05-26 06:18:37 +0000733 sqlite3_create_function(pDb->db, zName, -1, 0, 0, pFunc, tclSqlFunc, 0, 0);
drhcabb0812002-09-14 13:47:32 +0000734 break;
735 }
736
737 /*
drhaf9ff332002-01-16 21:00:27 +0000738 ** $db last_insert_rowid
739 **
740 ** Return an integer which is the ROWID for the most recent insert.
741 */
742 case DB_LAST_INSERT_ROWID: {
743 Tcl_Obj *pResult;
744 int rowid;
745 if( objc!=2 ){
746 Tcl_WrongNumArgs(interp, 2, objv, "");
747 return TCL_ERROR;
748 }
danielk19776f8a5032004-05-10 10:34:51 +0000749 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +0000750 pResult = Tcl_GetObjResult(interp);
751 Tcl_SetIntObj(pResult, rowid);
752 break;
753 }
754
755 /*
drh5d9d7572003-08-19 14:31:01 +0000756 ** $db onecolumn SQL
757 **
758 ** Return a single column from a single row of the given SQL query.
759 */
760 case DB_ONECOLUMN: {
drh5d9d7572003-08-19 14:31:01 +0000761 char *zSql;
762 char *zErrMsg = 0;
763 if( objc!=3 ){
764 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
765 return TCL_ERROR;
766 }
767 zSql = Tcl_GetStringFromObj(objv[2], 0);
danielk19776f8a5032004-05-10 10:34:51 +0000768 rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
drh5d9d7572003-08-19 14:31:01 +0000769 if( rc==SQLITE_ABORT ){
drh22fbcb82004-02-01 01:22:50 +0000770 rc = SQLITE_OK;
drh5d9d7572003-08-19 14:31:01 +0000771 }else if( zErrMsg ){
772 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
773 free(zErrMsg);
774 rc = TCL_ERROR;
775 }else if( rc!=SQLITE_OK ){
danielk1977f20b21c2004-05-31 23:56:42 +0000776 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh5d9d7572003-08-19 14:31:01 +0000777 rc = TCL_ERROR;
778 }
779 break;
780 }
781
782 /*
drh22fbcb82004-02-01 01:22:50 +0000783 ** $db rekey KEY
784 **
785 ** Change the encryption key on the currently open database.
786 */
787 case DB_REKEY: {
788 int nKey;
789 void *pKey;
790 if( objc!=3 ){
791 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
792 return TCL_ERROR;
793 }
794 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +0000795#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000796 rc = sqlite_rekey(pDb->db, pKey, nKey);
797 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000798 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh22fbcb82004-02-01 01:22:50 +0000799 rc = TCL_ERROR;
800 }
801#endif
802 break;
803 }
804
805 /*
drhbec3f402000-08-04 13:49:02 +0000806 ** $db timeout MILLESECONDS
807 **
808 ** Delay for the number of milliseconds specified when a file is locked.
809 */
drh6d313162000-09-21 13:01:35 +0000810 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000811 int ms;
drh6d313162000-09-21 13:01:35 +0000812 if( objc!=3 ){
813 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000814 return TCL_ERROR;
815 }
drh6d313162000-09-21 13:01:35 +0000816 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +0000817 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000818 break;
drh75897232000-05-29 14:26:00 +0000819 }
drhb5a20d32003-04-23 12:25:23 +0000820
821 /* $db trace ?CALLBACK?
822 **
823 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
824 ** that is executed. The text of the SQL is appended to CALLBACK before
825 ** it is executed.
826 */
827 case DB_TRACE: {
828 if( objc>3 ){
829 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
830 }else if( objc==2 ){
831 if( pDb->zTrace ){
832 Tcl_AppendResult(interp, pDb->zTrace, 0);
833 }
834 }else{
835 char *zTrace;
836 int len;
837 if( pDb->zTrace ){
838 Tcl_Free(pDb->zTrace);
839 }
840 zTrace = Tcl_GetStringFromObj(objv[2], &len);
841 if( zTrace && len>0 ){
842 pDb->zTrace = Tcl_Alloc( len + 1 );
843 strcpy(pDb->zTrace, zTrace);
844 }else{
845 pDb->zTrace = 0;
846 }
847 if( pDb->zTrace ){
848 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000849 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +0000850 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000851 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +0000852 }
853 }
854 break;
855 }
856
drh6d313162000-09-21 13:01:35 +0000857 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +0000858 return rc;
drh75897232000-05-29 14:26:00 +0000859}
860
861/*
drh22fbcb82004-02-01 01:22:50 +0000862** sqlite DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +0000863**
864** This is the main Tcl command. When the "sqlite" Tcl command is
865** invoked, this routine runs to process that command.
866**
867** The first argument, DBNAME, is an arbitrary name for a new
868** database connection. This command creates a new command named
869** DBNAME that is used to control that connection. The database
870** connection is deleted when the DBNAME command is deleted.
871**
872** The second argument is the name of the directory that contains
873** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000874**
875** For testing purposes, we also support the following:
876**
877** sqlite -encoding
878**
879** Return the encoding used by LIKE and GLOB operators. Choices
880** are UTF-8 and iso8859.
881**
drh647cb0e2002-11-04 19:32:25 +0000882** sqlite -version
883**
884** Return the version number of the SQLite library.
885**
drhfbc3eab2001-04-06 16:13:42 +0000886** sqlite -tcl-uses-utf
887**
888** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
889** not. Used by tests to make sure the library was compiled
890** correctly.
drh75897232000-05-29 14:26:00 +0000891*/
drh22fbcb82004-02-01 01:22:50 +0000892static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000893 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +0000894 void *pKey = 0;
895 int nKey = 0;
896 const char *zArg;
drh75897232000-05-29 14:26:00 +0000897 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +0000898 const char *zFile;
danielk197780290862004-05-22 09:21:21 +0000899 const char *zOpts[2] = {0, 0};
drh06b27182002-06-26 20:06:05 +0000900 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +0000901 if( objc==2 ){
902 zArg = Tcl_GetStringFromObj(objv[1], 0);
903 if( strcmp(zArg,"-encoding")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000904 Tcl_AppendResult(interp,sqlite3_encoding,0);
drhfbc3eab2001-04-06 16:13:42 +0000905 return TCL_OK;
906 }
drh22fbcb82004-02-01 01:22:50 +0000907 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000908 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +0000909 return TCL_OK;
910 }
drh9eb9e262004-02-11 02:18:05 +0000911 if( strcmp(zArg,"-has-codec")==0 ){
912#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000913 Tcl_AppendResult(interp,"1",0);
914#else
915 Tcl_AppendResult(interp,"0",0);
916#endif
917 return TCL_OK;
918 }
919 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +0000920#ifdef TCL_UTF_MAX
921 Tcl_AppendResult(interp,"1",0);
922#else
923 Tcl_AppendResult(interp,"0",0);
924#endif
925 return TCL_OK;
926 }
927 }
drh22fbcb82004-02-01 01:22:50 +0000928 if( objc==5 || objc==6 ){
929 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
930 if( strcmp(zArg,"-key")==0 ){
931 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
932 objc -= 2;
933 }
934 }
935 if( objc!=3 && objc!=4 ){
936 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +0000937#ifdef SQLITE_HAS_CODEC
938 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +0000939#else
940 "HANDLE FILENAME ?MODE?"
941#endif
942 );
drh75897232000-05-29 14:26:00 +0000943 return TCL_ERROR;
944 }
drh75897232000-05-29 14:26:00 +0000945 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +0000946 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +0000947 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +0000948 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
949 return TCL_ERROR;
950 }
951 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +0000952 zFile = Tcl_GetStringFromObj(objv[2], 0);
drh9eb9e262004-02-11 02:18:05 +0000953#ifdef SQLITE_HAS_CODEC
danielk19776f8a5032004-05-10 10:34:51 +0000954 p->db = sqlite3_open_encrypted(zFile, pKey, nKey, 0, &zErrMsg);
drheb8ed702004-02-11 10:37:23 +0000955#else
danielk197780290862004-05-22 09:21:21 +0000956 if( objc>3 ){
957 zOpts[0] = Tcl_GetString(objv[3]);
958 }
959 sqlite3_open(zFile, &p->db, zOpts);
960 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
961 zErrMsg = strdup(sqlite3_errmsg(p->db));
962 sqlite3_close(p->db);
963 p->db = 0;
964 }
drheb8ed702004-02-11 10:37:23 +0000965#endif
drhbec3f402000-08-04 13:49:02 +0000966 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +0000967 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +0000968 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +0000969 free(zErrMsg);
970 return TCL_ERROR;
971 }
drh22fbcb82004-02-01 01:22:50 +0000972 zArg = Tcl_GetStringFromObj(objv[1], 0);
973 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +0000974
drh06b27182002-06-26 20:06:05 +0000975 /* The return value is the value of the sqlite* pointer
976 */
977 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +0000978 if( strncmp(zBuf,"0x",2) ){
979 sprintf(zBuf, "0x%p", p->db);
980 }
drh06b27182002-06-26 20:06:05 +0000981 Tcl_AppendResult(interp, zBuf, 0);
982
drhc22bd472002-05-10 13:14:07 +0000983 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +0000984 ** SQL function.
drhc22bd472002-05-10 13:14:07 +0000985 */
drh28b4e482002-03-11 02:06:13 +0000986#ifdef SQLITE_TEST
987 {
drhc22bd472002-05-10 13:14:07 +0000988 extern void Md5_Register(sqlite*);
989 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +0000990 }
drh28b4e482002-03-11 02:06:13 +0000991#endif
drh75897232000-05-29 14:26:00 +0000992 return TCL_OK;
993}
994
995/*
drh90ca9752001-09-28 17:47:14 +0000996** Provide a dummy Tcl_InitStubs if we are using this as a static
997** library.
998*/
999#ifndef USE_TCL_STUBS
1000# undef Tcl_InitStubs
1001# define Tcl_InitStubs(a,b,c)
1002#endif
1003
1004/*
drh75897232000-05-29 14:26:00 +00001005** Initialize this module.
1006**
1007** This Tcl module contains only a single new Tcl command named "sqlite".
1008** (Hence there is no namespace. There is no point in using a namespace
1009** if the extension only supplies one new name!) The "sqlite" command is
1010** used to open a new SQLite database. See the DbMain() routine above
1011** for additional information.
1012*/
1013int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001014 Tcl_InitStubs(interp, "8.0", 0);
drh22fbcb82004-02-01 01:22:50 +00001015 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001016 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh90ca9752001-09-28 17:47:14 +00001017 return TCL_OK;
1018}
1019int Tclsqlite_Init(Tcl_Interp *interp){
1020 Tcl_InitStubs(interp, "8.0", 0);
drh22fbcb82004-02-01 01:22:50 +00001021 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001022 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh75897232000-05-29 14:26:00 +00001023 return TCL_OK;
1024}
1025int Sqlite_SafeInit(Tcl_Interp *interp){
1026 return TCL_OK;
1027}
drh90ca9752001-09-28 17:47:14 +00001028int Tclsqlite_SafeInit(Tcl_Interp *interp){
1029 return TCL_OK;
1030}
drh75897232000-05-29 14:26:00 +00001031
drh3cebbde2000-10-19 14:59:27 +00001032#if 0
drh75897232000-05-29 14:26:00 +00001033/*
1034** If compiled using mktclapp, this routine runs to initialize
1035** everything.
1036*/
1037int Et_AppInit(Tcl_Interp *interp){
1038 return Sqlite_Init(interp);
1039}
drh3cebbde2000-10-19 14:59:27 +00001040#endif
drh348784e2000-05-29 20:41:49 +00001041
1042/*
1043** If the macro TCLSH is defined and is one, then put in code for the
1044** "main" routine that will initialize Tcl.
1045*/
1046#if defined(TCLSH) && TCLSH==1
1047static char zMainloop[] =
1048 "set line {}\n"
1049 "while {![eof stdin]} {\n"
1050 "if {$line!=\"\"} {\n"
1051 "puts -nonewline \"> \"\n"
1052 "} else {\n"
1053 "puts -nonewline \"% \"\n"
1054 "}\n"
1055 "flush stdout\n"
1056 "append line [gets stdin]\n"
1057 "if {[info complete $line]} {\n"
1058 "if {[catch {uplevel #0 $line} result]} {\n"
1059 "puts stderr \"Error: $result\"\n"
1060 "} elseif {$result!=\"\"} {\n"
1061 "puts $result\n"
1062 "}\n"
1063 "set line {}\n"
1064 "} else {\n"
1065 "append line \\n\n"
1066 "}\n"
1067 "}\n"
1068;
1069
1070#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1071int TCLSH_MAIN(int argc, char **argv){
1072 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001073 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001074 interp = Tcl_CreateInterp();
danielk19774adee202004-05-08 08:23:19 +00001075 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001076#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001077 {
1078 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001079 extern int Sqlitetest2_Init(Tcl_Interp*);
1080 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001081 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001082 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001083 extern int Md5_Init(Tcl_Interp*);
danielk19776490beb2004-05-11 06:17:21 +00001084 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001085 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001086 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00001087 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00001088 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001089 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001090 }
1091#endif
drh348784e2000-05-29 20:41:49 +00001092 if( argc>=2 ){
1093 int i;
1094 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1095 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1096 for(i=2; i<argc; i++){
1097 Tcl_SetVar(interp, "argv", argv[i],
1098 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1099 }
1100 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001101 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001102 if( zInfo==0 ) zInfo = interp->result;
1103 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001104 return 1;
1105 }
1106 }else{
1107 Tcl_GlobalEval(interp, zMainloop);
1108 }
1109 return 0;
1110}
1111#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001112
1113#endif /* !defined(NO_TCL) */