blob: 869e8c0e35664a180f378e12f621eed62e790d5a [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**
drh38f82712004-06-18 17:10:16 +000014** $Id: tclsqlite.c,v 1.87 2004/06/18 17:10:17 drh 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{
danielk1977d8123362004-06-12 09:25:12 +0000280 sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1,
281 SQLITE_TRANSIENT);
drhcabb0812002-09-14 13:47:32 +0000282 }
283}
drhe22a3342003-04-22 20:30:37 +0000284#ifndef SQLITE_OMIT_AUTHORIZATION
285/*
286** This is the authentication function. It appends the authentication
287** type code and the two arguments to zCmd[] then invokes the result
288** on the interpreter. The reply is examined to determine if the
289** authentication fails or succeeds.
290*/
291static int auth_callback(
292 void *pArg,
293 int code,
294 const char *zArg1,
295 const char *zArg2,
296 const char *zArg3,
297 const char *zArg4
298){
299 char *zCode;
300 Tcl_DString str;
301 int rc;
302 const char *zReply;
303 SqliteDb *pDb = (SqliteDb*)pArg;
304
305 switch( code ){
306 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
307 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
308 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
309 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
310 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
311 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
312 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
313 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
314 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
315 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
316 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
317 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
318 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
319 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
320 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
321 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
322 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
323 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
324 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
325 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
326 case SQLITE_READ : zCode="SQLITE_READ"; break;
327 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
328 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
329 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000330 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
331 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
drhe22a3342003-04-22 20:30:37 +0000332 default : zCode="????"; break;
333 }
334 Tcl_DStringInit(&str);
335 Tcl_DStringAppend(&str, pDb->zAuth, -1);
336 Tcl_DStringAppendElement(&str, zCode);
337 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
338 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
339 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
340 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
341 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
342 Tcl_DStringFree(&str);
343 zReply = Tcl_GetStringResult(pDb->interp);
344 if( strcmp(zReply,"SQLITE_OK")==0 ){
345 rc = SQLITE_OK;
346 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
347 rc = SQLITE_DENY;
348 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
349 rc = SQLITE_IGNORE;
350 }else{
351 rc = 999;
352 }
353 return rc;
354}
355#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000356
357/*
danielk1977ef2cb632004-05-29 02:37:19 +0000358** zText is a pointer to text obtained via an sqlite3_result_text()
359** or similar interface. This routine returns a Tcl string object,
360** reference count set to 0, containing the text. If a translation
361** between iso8859 and UTF-8 is required, it is preformed.
362*/
363static Tcl_Obj *dbTextToObj(char const *zText){
364 Tcl_Obj *pVal;
365#ifdef UTF_TRANSLATION_NEEDED
366 Tcl_DString dCol;
367 Tcl_DStringInit(&dCol);
368 Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol);
369 pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
370 Tcl_DStringFree(&dCol);
371#else
372 pVal = Tcl_NewStringObj(zText, -1);
373#endif
374 return pVal;
375}
376
377/*
drh75897232000-05-29 14:26:00 +0000378** The "sqlite" command below creates a new Tcl command for each
379** connection it opens to an SQLite database. This routine is invoked
380** whenever one of those connection-specific commands is executed
381** in Tcl. For example, if you run Tcl code like this:
382**
383** sqlite db1 "my_database"
384** db1 close
385**
386** The first command opens a connection to the "my_database" database
387** and calls that connection "db1". The second command causes this
388** subroutine to be invoked.
389*/
drh6d313162000-09-21 13:01:35 +0000390static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000391 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000392 int choice;
drh22fbcb82004-02-01 01:22:50 +0000393 int rc = TCL_OK;
drh0de8c112002-07-06 16:32:14 +0000394 static const char *DB_strs[] = {
rdcf146a772004-02-25 22:51:06 +0000395 "authorizer", "busy", "changes",
396 "close", "commit_hook", "complete",
397 "errorcode", "eval", "function",
398 "last_insert_rowid", "last_statement_changes", "onecolumn",
399 "progress", "rekey", "timeout",
danielk19777cedc8d2004-06-10 10:50:08 +0000400 "trace", "collate", "collation_needed",
drh22fbcb82004-02-01 01:22:50 +0000401 0
drh6d313162000-09-21 13:01:35 +0000402 };
drh411995d2002-06-25 19:31:18 +0000403 enum DB_enum {
rdcf146a772004-02-25 22:51:06 +0000404 DB_AUTHORIZER, DB_BUSY, DB_CHANGES,
405 DB_CLOSE, DB_COMMIT_HOOK, DB_COMPLETE,
406 DB_ERRORCODE, DB_EVAL, DB_FUNCTION,
407 DB_LAST_INSERT_ROWID, DB_LAST_STATEMENT_CHANGES, DB_ONECOLUMN,
408 DB_PROGRESS, DB_REKEY, DB_TIMEOUT,
danielk19777cedc8d2004-06-10 10:50:08 +0000409 DB_TRACE, DB_COLLATE, DB_COLLATION_NEEDED
drh6d313162000-09-21 13:01:35 +0000410 };
411
412 if( objc<2 ){
413 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +0000414 return TCL_ERROR;
415 }
drh411995d2002-06-25 19:31:18 +0000416 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +0000417 return TCL_ERROR;
418 }
419
drh411995d2002-06-25 19:31:18 +0000420 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +0000421
drhe22a3342003-04-22 20:30:37 +0000422 /* $db authorizer ?CALLBACK?
423 **
424 ** Invoke the given callback to authorize each SQL operation as it is
425 ** compiled. 5 arguments are appended to the callback before it is
426 ** invoked:
427 **
428 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
429 ** (2) First descriptive name (depends on authorization type)
430 ** (3) Second descriptive name
431 ** (4) Name of the database (ex: "main", "temp")
432 ** (5) Name of trigger that is doing the access
433 **
434 ** The callback should return on of the following strings: SQLITE_OK,
435 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
436 **
437 ** If this method is invoked with no arguments, the current authorization
438 ** callback string is returned.
439 */
440 case DB_AUTHORIZER: {
441 if( objc>3 ){
442 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
443 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +0000444 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +0000445 Tcl_AppendResult(interp, pDb->zAuth, 0);
446 }
447 }else{
448 char *zAuth;
449 int len;
450 if( pDb->zAuth ){
451 Tcl_Free(pDb->zAuth);
452 }
453 zAuth = Tcl_GetStringFromObj(objv[2], &len);
454 if( zAuth && len>0 ){
455 pDb->zAuth = Tcl_Alloc( len + 1 );
456 strcpy(pDb->zAuth, zAuth);
457 }else{
458 pDb->zAuth = 0;
459 }
460#ifndef SQLITE_OMIT_AUTHORIZATION
461 if( pDb->zAuth ){
462 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000463 sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
drhe22a3342003-04-22 20:30:37 +0000464 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000465 sqlite3_set_authorizer(pDb->db, 0, 0);
drhe22a3342003-04-22 20:30:37 +0000466 }
467#endif
468 }
469 break;
470 }
471
drhbec3f402000-08-04 13:49:02 +0000472 /* $db busy ?CALLBACK?
473 **
474 ** Invoke the given callback if an SQL statement attempts to open
475 ** a locked database file.
476 */
drh6d313162000-09-21 13:01:35 +0000477 case DB_BUSY: {
478 if( objc>3 ){
479 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +0000480 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +0000481 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +0000482 if( pDb->zBusy ){
483 Tcl_AppendResult(interp, pDb->zBusy, 0);
484 }
485 }else{
drh6d313162000-09-21 13:01:35 +0000486 char *zBusy;
487 int len;
drhbec3f402000-08-04 13:49:02 +0000488 if( pDb->zBusy ){
489 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +0000490 }
drh6d313162000-09-21 13:01:35 +0000491 zBusy = Tcl_GetStringFromObj(objv[2], &len);
492 if( zBusy && len>0 ){
493 pDb->zBusy = Tcl_Alloc( len + 1 );
494 strcpy(pDb->zBusy, zBusy);
495 }else{
496 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +0000497 }
498 if( pDb->zBusy ){
499 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000500 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +0000501 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000502 sqlite3_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +0000503 }
504 }
drh6d313162000-09-21 13:01:35 +0000505 break;
506 }
drhbec3f402000-08-04 13:49:02 +0000507
danielk1977348bb5d2003-10-18 09:37:26 +0000508 /* $db progress ?N CALLBACK?
509 **
510 ** Invoke the given callback every N virtual machine opcodes while executing
511 ** queries.
512 */
513 case DB_PROGRESS: {
514 if( objc==2 ){
515 if( pDb->zProgress ){
516 Tcl_AppendResult(interp, pDb->zProgress, 0);
517 }
518 }else if( objc==4 ){
519 char *zProgress;
520 int len;
521 int N;
522 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
523 return TCL_ERROR;
524 };
525 if( pDb->zProgress ){
526 Tcl_Free(pDb->zProgress);
527 }
528 zProgress = Tcl_GetStringFromObj(objv[3], &len);
529 if( zProgress && len>0 ){
530 pDb->zProgress = Tcl_Alloc( len + 1 );
531 strcpy(pDb->zProgress, zProgress);
532 }else{
533 pDb->zProgress = 0;
534 }
535#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
536 if( pDb->zProgress ){
537 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000538 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
danielk1977348bb5d2003-10-18 09:37:26 +0000539 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000540 sqlite3_progress_handler(pDb->db, 0, 0, 0);
danielk1977348bb5d2003-10-18 09:37:26 +0000541 }
542#endif
543 }else{
544 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
545 return TCL_ERROR;
546 }
547 break;
548 }
549
drhc8d30ac2002-04-12 10:08:59 +0000550 /*
551 ** $db changes
552 **
553 ** Return the number of rows that were modified, inserted, or deleted by
554 ** the most recent "eval".
555 */
556 case DB_CHANGES: {
557 Tcl_Obj *pResult;
558 int nChange;
559 if( objc!=2 ){
560 Tcl_WrongNumArgs(interp, 2, objv, "");
561 return TCL_ERROR;
562 }
danielk197730ccda12004-05-27 12:11:31 +0000563 /* nChange = sqlite3_changes(pDb->db); */
564 nChange = pDb->nChange;
drhc8d30ac2002-04-12 10:08:59 +0000565 pResult = Tcl_GetObjResult(interp);
566 Tcl_SetIntObj(pResult, nChange);
567 break;
568 }
569
rdcf146a772004-02-25 22:51:06 +0000570 /*
571 ** $db last_statement_changes
572 **
573 ** Return the number of rows that were modified, inserted, or deleted by
574 ** the last statment to complete execution (excluding changes due to
575 ** triggers)
576 */
577 case DB_LAST_STATEMENT_CHANGES: {
578 Tcl_Obj *pResult;
579 int lsChange;
580 if( objc!=2 ){
581 Tcl_WrongNumArgs(interp, 2, objv, "");
582 return TCL_ERROR;
583 }
danielk19776f8a5032004-05-10 10:34:51 +0000584 lsChange = sqlite3_last_statement_changes(pDb->db);
rdcf146a772004-02-25 22:51:06 +0000585 pResult = Tcl_GetObjResult(interp);
586 Tcl_SetIntObj(pResult, lsChange);
587 break;
588 }
589
drh75897232000-05-29 14:26:00 +0000590 /* $db close
591 **
592 ** Shutdown the database
593 */
drh6d313162000-09-21 13:01:35 +0000594 case DB_CLOSE: {
595 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
596 break;
597 }
drh75897232000-05-29 14:26:00 +0000598
drhaa940ea2004-01-15 02:44:03 +0000599 /* $db commit_hook ?CALLBACK?
600 **
601 ** Invoke the given callback just before committing every SQL transaction.
602 ** If the callback throws an exception or returns non-zero, then the
603 ** transaction is aborted. If CALLBACK is an empty string, the callback
604 ** is disabled.
605 */
606 case DB_COMMIT_HOOK: {
607 if( objc>3 ){
608 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
609 }else if( objc==2 ){
610 if( pDb->zCommit ){
611 Tcl_AppendResult(interp, pDb->zCommit, 0);
612 }
613 }else{
614 char *zCommit;
615 int len;
616 if( pDb->zCommit ){
617 Tcl_Free(pDb->zCommit);
618 }
619 zCommit = Tcl_GetStringFromObj(objv[2], &len);
620 if( zCommit && len>0 ){
621 pDb->zCommit = Tcl_Alloc( len + 1 );
622 strcpy(pDb->zCommit, zCommit);
623 }else{
624 pDb->zCommit = 0;
625 }
626 if( pDb->zCommit ){
627 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000628 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
drhaa940ea2004-01-15 02:44:03 +0000629 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000630 sqlite3_commit_hook(pDb->db, 0, 0);
drhaa940ea2004-01-15 02:44:03 +0000631 }
632 }
633 break;
634 }
635
drh75897232000-05-29 14:26:00 +0000636 /* $db complete SQL
637 **
638 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
639 ** additional lines of input are needed. This is similar to the
640 ** built-in "info complete" command of Tcl.
641 */
drh6d313162000-09-21 13:01:35 +0000642 case DB_COMPLETE: {
643 Tcl_Obj *pResult;
644 int isComplete;
645 if( objc!=3 ){
646 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +0000647 return TCL_ERROR;
648 }
danielk19776f8a5032004-05-10 10:34:51 +0000649 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
drh6d313162000-09-21 13:01:35 +0000650 pResult = Tcl_GetObjResult(interp);
651 Tcl_SetBooleanObj(pResult, isComplete);
652 break;
653 }
drhdcd997e2003-01-31 17:21:49 +0000654
655 /*
656 ** $db errorcode
657 **
658 ** Return the numeric error code that was returned by the most recent
danielk19776f8a5032004-05-10 10:34:51 +0000659 ** call to sqlite3_exec().
drhdcd997e2003-01-31 17:21:49 +0000660 */
661 case DB_ERRORCODE: {
danielk1977f3ce83f2004-06-14 11:43:46 +0000662 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
drhdcd997e2003-01-31 17:21:49 +0000663 break;
664 }
drh75897232000-05-29 14:26:00 +0000665
666 /*
667 ** $db eval $sql ?array { ...code... }?
668 **
669 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +0000670 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +0000671 ** If "array" and "code" are omitted, then no callback is every invoked.
672 ** If "array" is an empty string, then the values are placed in variables
673 ** that have the same name as the fields extracted by the query.
674 */
drh6d313162000-09-21 13:01:35 +0000675 case DB_EVAL: {
danielk197730ccda12004-05-27 12:11:31 +0000676 char const *zSql;
677 char const *zLeft;
678 sqlite3_stmt *pStmt;
danielk1977ef2cb632004-05-29 02:37:19 +0000679
680 Tcl_Obj *pRet = Tcl_NewObj();
681 Tcl_IncrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000682
683 if( objc!=5 && objc!=3 ){
684 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME CODE?");
685 return TCL_ERROR;
686 }
687
688 pDb->nChange = 0;
689 zSql = Tcl_GetStringFromObj(objv[2], 0);
690 while( zSql[0] ){
691 int i;
692
693 if( SQLITE_OK!=sqlite3_prepare(pDb->db, zSql, -1, &pStmt, &zLeft) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000694 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000695 rc = TCL_ERROR;
696 break;
697 }
698
699 if( pStmt && objc==5 ){
700 Tcl_Obj *pColList = Tcl_NewObj();
701 Tcl_IncrRefCount(pColList);
702
703 for(i=0; i<sqlite3_column_count(pStmt); i++){
704 Tcl_ListObjAppendElement(interp, pColList,
danielk1977ef2cb632004-05-29 02:37:19 +0000705 dbTextToObj(sqlite3_column_name(pStmt, i))
danielk197730ccda12004-05-27 12:11:31 +0000706 );
707 }
708 Tcl_ObjSetVar2(interp,objv[3],Tcl_NewStringObj("*",-1),pColList,0);
709 }
710
711 while( pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
712 for(i=0; i<sqlite3_column_count(pStmt); i++){
713 Tcl_Obj *pVal;
714
715 /* Set pVal to contain the i'th column of this row. */
drh9c054832004-05-31 18:51:57 +0000716 if( SQLITE_BLOB!=sqlite3_column_type(pStmt, i) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000717 pVal = dbTextToObj(sqlite3_column_text(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000718 }else{
danielk19773fd0a732004-05-27 13:35:19 +0000719 int bytes = sqlite3_column_bytes(pStmt, i);
720 pVal = Tcl_NewByteArrayObj(sqlite3_column_blob(pStmt, i), bytes);
danielk197730ccda12004-05-27 12:11:31 +0000721 }
722
723 if( objc==5 ){
danielk1977ef2cb632004-05-29 02:37:19 +0000724 Tcl_Obj *pName = dbTextToObj(sqlite3_column_name(pStmt, i));
danielk197730ccda12004-05-27 12:11:31 +0000725 Tcl_IncrRefCount(pName);
726 if( !strcmp("", Tcl_GetString(objv[3])) ){
727 Tcl_ObjSetVar2(interp, pName, 0, pVal, 0);
728 }else{
729 Tcl_ObjSetVar2(interp, objv[3], pName, pVal, 0);
730 }
731 Tcl_DecrRefCount(pName);
732 }else{
danielk197730ccda12004-05-27 12:11:31 +0000733 Tcl_ListObjAppendElement(interp, pRet, pVal);
734 }
735 }
736
737 if( objc==5 ){
738 rc = Tcl_EvalObjEx(interp, objv[4], 0);
739 if( rc!=TCL_ERROR ) rc = TCL_OK;
740 }
741 }
742
743 if( pStmt && SQLITE_SCHEMA==sqlite3_finalize(pStmt) ){
744 continue;
745 }
746
747 if( pStmt && SQLITE_OK!=sqlite3_errcode(pDb->db) ){
danielk1977ef2cb632004-05-29 02:37:19 +0000748 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
danielk197730ccda12004-05-27 12:11:31 +0000749 rc = TCL_ERROR;
750 break;
751 }
752
753 pDb->nChange += sqlite3_changes(pDb->db);
754 zSql = zLeft;
755 }
756
danielk1977ef2cb632004-05-29 02:37:19 +0000757 if( rc==TCL_OK ){
danielk197730ccda12004-05-27 12:11:31 +0000758 Tcl_SetObjResult(interp, pRet);
danielk197730ccda12004-05-27 12:11:31 +0000759 }
danielk1977ef2cb632004-05-29 02:37:19 +0000760 Tcl_DecrRefCount(pRet);
danielk197730ccda12004-05-27 12:11:31 +0000761
762 break;
763 }
drhbec3f402000-08-04 13:49:02 +0000764
765 /*
drhcabb0812002-09-14 13:47:32 +0000766 ** $db function NAME SCRIPT
767 **
768 ** Create a new SQL function called NAME. Whenever that function is
769 ** called, invoke SCRIPT to evaluate the function.
770 */
771 case DB_FUNCTION: {
772 SqlFunc *pFunc;
773 char *zName;
774 char *zScript;
775 int nScript;
776 if( objc!=4 ){
777 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
778 return TCL_ERROR;
779 }
780 zName = Tcl_GetStringFromObj(objv[2], 0);
781 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
782 pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
783 if( pFunc==0 ) return TCL_ERROR;
784 pFunc->interp = interp;
785 pFunc->pNext = pDb->pFunc;
786 pFunc->zScript = (char*)&pFunc[1];
787 strcpy(pFunc->zScript, zScript);
danielk1977d8123362004-06-12 09:25:12 +0000788 sqlite3_create_function(pDb->db, zName, -1, SQLITE_UTF8, 0,
789 pFunc, tclSqlFunc, 0, 0);
drhcabb0812002-09-14 13:47:32 +0000790 break;
791 }
792
793 /*
drhaf9ff332002-01-16 21:00:27 +0000794 ** $db last_insert_rowid
795 **
796 ** Return an integer which is the ROWID for the most recent insert.
797 */
798 case DB_LAST_INSERT_ROWID: {
799 Tcl_Obj *pResult;
800 int rowid;
801 if( objc!=2 ){
802 Tcl_WrongNumArgs(interp, 2, objv, "");
803 return TCL_ERROR;
804 }
danielk19776f8a5032004-05-10 10:34:51 +0000805 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +0000806 pResult = Tcl_GetObjResult(interp);
807 Tcl_SetIntObj(pResult, rowid);
808 break;
809 }
810
811 /*
drh5d9d7572003-08-19 14:31:01 +0000812 ** $db onecolumn SQL
813 **
814 ** Return a single column from a single row of the given SQL query.
815 */
816 case DB_ONECOLUMN: {
drh5d9d7572003-08-19 14:31:01 +0000817 char *zSql;
818 char *zErrMsg = 0;
819 if( objc!=3 ){
820 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
821 return TCL_ERROR;
822 }
823 zSql = Tcl_GetStringFromObj(objv[2], 0);
danielk19776f8a5032004-05-10 10:34:51 +0000824 rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
drh5d9d7572003-08-19 14:31:01 +0000825 if( rc==SQLITE_ABORT ){
drh22fbcb82004-02-01 01:22:50 +0000826 rc = SQLITE_OK;
drh5d9d7572003-08-19 14:31:01 +0000827 }else if( zErrMsg ){
828 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
829 free(zErrMsg);
830 rc = TCL_ERROR;
831 }else if( rc!=SQLITE_OK ){
danielk1977f20b21c2004-05-31 23:56:42 +0000832 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh5d9d7572003-08-19 14:31:01 +0000833 rc = TCL_ERROR;
834 }
835 break;
836 }
837
838 /*
drh22fbcb82004-02-01 01:22:50 +0000839 ** $db rekey KEY
840 **
841 ** Change the encryption key on the currently open database.
842 */
843 case DB_REKEY: {
844 int nKey;
845 void *pKey;
846 if( objc!=3 ){
847 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
848 return TCL_ERROR;
849 }
850 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +0000851#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000852 rc = sqlite_rekey(pDb->db, pKey, nKey);
853 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000854 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh22fbcb82004-02-01 01:22:50 +0000855 rc = TCL_ERROR;
856 }
857#endif
858 break;
859 }
860
861 /*
drhbec3f402000-08-04 13:49:02 +0000862 ** $db timeout MILLESECONDS
863 **
864 ** Delay for the number of milliseconds specified when a file is locked.
865 */
drh6d313162000-09-21 13:01:35 +0000866 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000867 int ms;
drh6d313162000-09-21 13:01:35 +0000868 if( objc!=3 ){
869 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000870 return TCL_ERROR;
871 }
drh6d313162000-09-21 13:01:35 +0000872 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +0000873 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000874 break;
drh75897232000-05-29 14:26:00 +0000875 }
drhb5a20d32003-04-23 12:25:23 +0000876
877 /* $db trace ?CALLBACK?
878 **
879 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
880 ** that is executed. The text of the SQL is appended to CALLBACK before
881 ** it is executed.
882 */
883 case DB_TRACE: {
884 if( objc>3 ){
885 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
886 }else if( objc==2 ){
887 if( pDb->zTrace ){
888 Tcl_AppendResult(interp, pDb->zTrace, 0);
889 }
890 }else{
891 char *zTrace;
892 int len;
893 if( pDb->zTrace ){
894 Tcl_Free(pDb->zTrace);
895 }
896 zTrace = Tcl_GetStringFromObj(objv[2], &len);
897 if( zTrace && len>0 ){
898 pDb->zTrace = Tcl_Alloc( len + 1 );
899 strcpy(pDb->zTrace, zTrace);
900 }else{
901 pDb->zTrace = 0;
902 }
903 if( pDb->zTrace ){
904 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000905 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +0000906 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000907 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +0000908 }
909 }
910 break;
911 }
912
danielk19770202b292004-06-09 09:55:16 +0000913 /*
914 ** $db collate NAME SCRIPT
915 **
916 ** Create a new SQL collation function called NAME. Whenever
917 ** that function is called, invoke SCRIPT to evaluate the function.
918 */
919 case DB_COLLATE: {
920 SqlCollate *pCollate;
921 char *zName;
922 char *zScript;
923 int nScript;
924 if( objc!=4 ){
925 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
926 return TCL_ERROR;
927 }
928 zName = Tcl_GetStringFromObj(objv[2], 0);
929 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
930 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
931 if( pCollate==0 ) return TCL_ERROR;
932 pCollate->interp = interp;
933 pCollate->pNext = pDb->pCollate;
934 pCollate->zScript = (char*)&pCollate[1];
935 strcpy(pCollate->zScript, zScript);
danielk1977466be562004-06-10 02:16:01 +0000936 if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
937 pCollate, tclSqlCollate) ){
danielk19770202b292004-06-09 09:55:16 +0000938 return TCL_ERROR;
939 }
940 break;
941 }
942
danielk19777cedc8d2004-06-10 10:50:08 +0000943 /*
944 ** $db collate_needed SCRIPT
945 **
946 ** Create a new SQL collation function called NAME. Whenever
947 ** that function is called, invoke SCRIPT to evaluate the function.
948 */
949 case DB_COLLATION_NEEDED: {
950 if( objc!=3 ){
951 Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT");
952 return TCL_ERROR;
953 }
954 if( pDb->pCollateNeeded ){
955 Tcl_DecrRefCount(pDb->pCollateNeeded);
956 }
957 pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]);
958 Tcl_IncrRefCount(pDb->pCollateNeeded);
959 sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded);
960 break;
961 }
962
drh6d313162000-09-21 13:01:35 +0000963 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +0000964 return rc;
drh75897232000-05-29 14:26:00 +0000965}
966
967/*
drh22fbcb82004-02-01 01:22:50 +0000968** sqlite DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +0000969**
970** This is the main Tcl command. When the "sqlite" Tcl command is
971** invoked, this routine runs to process that command.
972**
973** The first argument, DBNAME, is an arbitrary name for a new
974** database connection. This command creates a new command named
975** DBNAME that is used to control that connection. The database
976** connection is deleted when the DBNAME command is deleted.
977**
978** The second argument is the name of the directory that contains
979** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000980**
981** For testing purposes, we also support the following:
982**
983** sqlite -encoding
984**
985** Return the encoding used by LIKE and GLOB operators. Choices
986** are UTF-8 and iso8859.
987**
drh647cb0e2002-11-04 19:32:25 +0000988** sqlite -version
989**
990** Return the version number of the SQLite library.
991**
drhfbc3eab2001-04-06 16:13:42 +0000992** sqlite -tcl-uses-utf
993**
994** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
995** not. Used by tests to make sure the library was compiled
996** correctly.
drh75897232000-05-29 14:26:00 +0000997*/
drh22fbcb82004-02-01 01:22:50 +0000998static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000999 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +00001000 void *pKey = 0;
1001 int nKey = 0;
1002 const char *zArg;
drh75897232000-05-29 14:26:00 +00001003 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +00001004 const char *zFile;
drh06b27182002-06-26 20:06:05 +00001005 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +00001006 if( objc==2 ){
1007 zArg = Tcl_GetStringFromObj(objv[1], 0);
drh22fbcb82004-02-01 01:22:50 +00001008 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001009 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +00001010 return TCL_OK;
1011 }
drh9eb9e262004-02-11 02:18:05 +00001012 if( strcmp(zArg,"-has-codec")==0 ){
1013#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001014 Tcl_AppendResult(interp,"1",0);
1015#else
1016 Tcl_AppendResult(interp,"0",0);
1017#endif
1018 return TCL_OK;
1019 }
1020 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +00001021#ifdef TCL_UTF_MAX
1022 Tcl_AppendResult(interp,"1",0);
1023#else
1024 Tcl_AppendResult(interp,"0",0);
1025#endif
1026 return TCL_OK;
1027 }
1028 }
drh22fbcb82004-02-01 01:22:50 +00001029 if( objc==5 || objc==6 ){
1030 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
1031 if( strcmp(zArg,"-key")==0 ){
1032 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
1033 objc -= 2;
1034 }
1035 }
1036 if( objc!=3 && objc!=4 ){
1037 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +00001038#ifdef SQLITE_HAS_CODEC
1039 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +00001040#else
1041 "HANDLE FILENAME ?MODE?"
1042#endif
1043 );
drh75897232000-05-29 14:26:00 +00001044 return TCL_ERROR;
1045 }
drh75897232000-05-29 14:26:00 +00001046 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001047 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001048 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001049 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1050 return TCL_ERROR;
1051 }
1052 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00001053 zFile = Tcl_GetStringFromObj(objv[2], 0);
drh9eb9e262004-02-11 02:18:05 +00001054#ifdef SQLITE_HAS_CODEC
danielk19776f8a5032004-05-10 10:34:51 +00001055 p->db = sqlite3_open_encrypted(zFile, pKey, nKey, 0, &zErrMsg);
drheb8ed702004-02-11 10:37:23 +00001056#else
danielk19774f057f92004-06-08 00:02:33 +00001057 sqlite3_open(zFile, &p->db);
danielk197780290862004-05-22 09:21:21 +00001058 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
1059 zErrMsg = strdup(sqlite3_errmsg(p->db));
1060 sqlite3_close(p->db);
1061 p->db = 0;
1062 }
drheb8ed702004-02-11 10:37:23 +00001063#endif
drhbec3f402000-08-04 13:49:02 +00001064 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001065 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001066 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001067 free(zErrMsg);
1068 return TCL_ERROR;
1069 }
drh22fbcb82004-02-01 01:22:50 +00001070 zArg = Tcl_GetStringFromObj(objv[1], 0);
1071 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001072
drh06b27182002-06-26 20:06:05 +00001073 /* The return value is the value of the sqlite* pointer
1074 */
1075 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001076 if( strncmp(zBuf,"0x",2) ){
1077 sprintf(zBuf, "0x%p", p->db);
1078 }
drh06b27182002-06-26 20:06:05 +00001079 Tcl_AppendResult(interp, zBuf, 0);
1080
drhc22bd472002-05-10 13:14:07 +00001081 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001082 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001083 */
drh28b4e482002-03-11 02:06:13 +00001084#ifdef SQLITE_TEST
1085 {
drhc22bd472002-05-10 13:14:07 +00001086 extern void Md5_Register(sqlite*);
1087 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +00001088 }
drh28b4e482002-03-11 02:06:13 +00001089#endif
danielk19777cedc8d2004-06-10 10:50:08 +00001090 p->interp = interp;
drh75897232000-05-29 14:26:00 +00001091 return TCL_OK;
1092}
1093
1094/*
drh90ca9752001-09-28 17:47:14 +00001095** Provide a dummy Tcl_InitStubs if we are using this as a static
1096** library.
1097*/
1098#ifndef USE_TCL_STUBS
1099# undef Tcl_InitStubs
1100# define Tcl_InitStubs(a,b,c)
1101#endif
1102
1103/*
drh75897232000-05-29 14:26:00 +00001104** Initialize this module.
1105**
1106** This Tcl module contains only a single new Tcl command named "sqlite".
1107** (Hence there is no namespace. There is no point in using a namespace
1108** if the extension only supplies one new name!) The "sqlite" command is
1109** used to open a new SQLite database. See the DbMain() routine above
1110** for additional information.
1111*/
drh38f82712004-06-18 17:10:16 +00001112int Sqlite3_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001113 Tcl_InitStubs(interp, "8.0", 0);
drh22fbcb82004-02-01 01:22:50 +00001114 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001115 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh90ca9752001-09-28 17:47:14 +00001116 return TCL_OK;
1117}
drh38f82712004-06-18 17:10:16 +00001118int Tclsqlite3_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001119 Tcl_InitStubs(interp, "8.0", 0);
drh22fbcb82004-02-01 01:22:50 +00001120 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001121 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh75897232000-05-29 14:26:00 +00001122 return TCL_OK;
1123}
drh38f82712004-06-18 17:10:16 +00001124int Sqlite3_SafeInit(Tcl_Interp *interp){
drh75897232000-05-29 14:26:00 +00001125 return TCL_OK;
1126}
drh38f82712004-06-18 17:10:16 +00001127int Tclsqlite3_SafeInit(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001128 return TCL_OK;
1129}
drh75897232000-05-29 14:26:00 +00001130
drh3cebbde2000-10-19 14:59:27 +00001131#if 0
drh75897232000-05-29 14:26:00 +00001132/*
1133** If compiled using mktclapp, this routine runs to initialize
1134** everything.
1135*/
1136int Et_AppInit(Tcl_Interp *interp){
drh38f82712004-06-18 17:10:16 +00001137 return Sqlite3_Init(interp);
drh75897232000-05-29 14:26:00 +00001138}
drh3cebbde2000-10-19 14:59:27 +00001139#endif
drh348784e2000-05-29 20:41:49 +00001140
1141/*
1142** If the macro TCLSH is defined and is one, then put in code for the
1143** "main" routine that will initialize Tcl.
1144*/
1145#if defined(TCLSH) && TCLSH==1
1146static char zMainloop[] =
1147 "set line {}\n"
1148 "while {![eof stdin]} {\n"
1149 "if {$line!=\"\"} {\n"
1150 "puts -nonewline \"> \"\n"
1151 "} else {\n"
1152 "puts -nonewline \"% \"\n"
1153 "}\n"
1154 "flush stdout\n"
1155 "append line [gets stdin]\n"
1156 "if {[info complete $line]} {\n"
1157 "if {[catch {uplevel #0 $line} result]} {\n"
1158 "puts stderr \"Error: $result\"\n"
1159 "} elseif {$result!=\"\"} {\n"
1160 "puts $result\n"
1161 "}\n"
1162 "set line {}\n"
1163 "} else {\n"
1164 "append line \\n\n"
1165 "}\n"
1166 "}\n"
1167;
1168
1169#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1170int TCLSH_MAIN(int argc, char **argv){
1171 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001172 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001173 interp = Tcl_CreateInterp();
drh38f82712004-06-18 17:10:16 +00001174 Sqlite3_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001175#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001176 {
1177 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001178 extern int Sqlitetest2_Init(Tcl_Interp*);
1179 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001180 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001181 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001182 extern int Md5_Init(Tcl_Interp*);
danielk19776490beb2004-05-11 06:17:21 +00001183 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001184 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001185 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00001186 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00001187 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001188 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001189 }
1190#endif
drh348784e2000-05-29 20:41:49 +00001191 if( argc>=2 ){
1192 int i;
1193 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1194 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1195 for(i=2; i<argc; i++){
1196 Tcl_SetVar(interp, "argv", argv[i],
1197 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1198 }
1199 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001200 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001201 if( zInfo==0 ) zInfo = interp->result;
1202 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001203 return 1;
1204 }
1205 }else{
1206 Tcl_GlobalEval(interp, zMainloop);
1207 }
1208 return 0;
1209}
1210#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001211
1212#endif /* !defined(NO_TCL) */