blob: af4131a71984575764230533187d2727767a4961 [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**
danielk19777cedc8d2004-06-10 10:50:08 +000014** $Id: tclsqlite.c,v 1.83 2004/06/10 10:50:38 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000015*/
drh6d313162000-09-21 13:01:35 +000016#ifndef NO_TCL /* Omit this whole file if TCL is unavailable */
17
drh06b27182002-06-26 20:06:05 +000018#include "sqliteInt.h"
drh17a68932001-01-31 13:28:08 +000019#include "tcl.h"
drh75897232000-05-29 14:26:00 +000020#include <stdlib.h>
21#include <string.h>
drhce927062001-11-09 13:41:09 +000022#include <assert.h>
drh75897232000-05-29 14:26:00 +000023
24/*
drh98808ba2001-10-18 12:34:46 +000025** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
26** have to do a translation when going between the two. Set the
27** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
28** this translation.
29*/
30#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
31# define UTF_TRANSLATION_NEEDED 1
32#endif
33
34/*
drhcabb0812002-09-14 13:47:32 +000035** New SQL functions can be created as TCL scripts. Each such function
36** is described by an instance of the following structure.
37*/
38typedef struct SqlFunc SqlFunc;
39struct SqlFunc {
40 Tcl_Interp *interp; /* The TCL interpret to execute the function */
41 char *zScript; /* The script to be run */
42 SqlFunc *pNext; /* Next function on the list of them all */
43};
44
45/*
danielk19770202b292004-06-09 09:55:16 +000046** New collation sequences function can be created as TCL scripts. Each such
47** function is described by an instance of the following structure.
48*/
49typedef struct SqlCollate SqlCollate;
50struct SqlCollate {
51 Tcl_Interp *interp; /* The TCL interpret to execute the function */
52 char *zScript; /* The script to be run */
53 SqlCollate *pNext; /* Next function on the list of them all */
54};
55
56/*
drhbec3f402000-08-04 13:49:02 +000057** There is one instance of this structure for each SQLite database
58** that has been opened by the SQLite TCL interface.
59*/
60typedef struct SqliteDb SqliteDb;
61struct SqliteDb {
62 sqlite *db; /* The "real" database structure */
63 Tcl_Interp *interp; /* The interpreter used for this database */
drh6d313162000-09-21 13:01:35 +000064 char *zBusy; /* The busy callback routine */
drhaa940ea2004-01-15 02:44:03 +000065 char *zCommit; /* The commit hook callback routine */
drhb5a20d32003-04-23 12:25:23 +000066 char *zTrace; /* The trace callback routine */
danielk1977348bb5d2003-10-18 09:37:26 +000067 char *zProgress; /* The progress callback routine */
drhe22a3342003-04-22 20:30:37 +000068 char *zAuth; /* The authorization callback routine */
drhcabb0812002-09-14 13:47:32 +000069 SqlFunc *pFunc; /* List of SQL functions */
danielk19770202b292004-06-09 09:55:16 +000070 SqlCollate *pCollate; /* List of SQL collation functions */
danielk19776f8a5032004-05-10 10:34:51 +000071 int rc; /* Return code of most recent sqlite3_exec() */
danielk19777cedc8d2004-06-10 10:50:08 +000072 int nChange; /* Database changes for the most recent eval */
73 Tcl_Obj *pCollateNeeded; /* Collation needed script */
drhbec3f402000-08-04 13:49:02 +000074};
75
76/*
drh75897232000-05-29 14:26:00 +000077** An instance of this structure passes information thru the sqlite
78** logic from the original TCL command into the callback routine.
79*/
80typedef struct CallbackData CallbackData;
81struct CallbackData {
82 Tcl_Interp *interp; /* The TCL interpreter */
83 char *zArray; /* The array into which data is written */
drh6d313162000-09-21 13:01:35 +000084 Tcl_Obj *pCode; /* The code to execute for each row */
drhce927062001-11-09 13:41:09 +000085 int once; /* Set for first callback only */
drh960e8c62001-04-03 16:53:21 +000086 int tcl_rc; /* Return code from TCL script */
drh98808ba2001-10-18 12:34:46 +000087 int nColName; /* Number of entries in the azColName[] array */
88 char **azColName; /* Column names translated to UTF-8 */
drh98808ba2001-10-18 12:34:46 +000089};
drh297ecf12001-04-05 15:57:13 +000090
drh6d313162000-09-21 13:01:35 +000091/*
drh5d9d7572003-08-19 14:31:01 +000092** This is a second alternative callback for database queries. A the
93** first column of the first row of the result is made the TCL result.
94*/
95static int DbEvalCallback3(
96 void *clientData, /* An instance of CallbackData */
97 int nCol, /* Number of columns in the result */
98 char ** azCol, /* Data for each column */
99 char ** azN /* Name for each column */
100){
101 Tcl_Interp *interp = (Tcl_Interp*)clientData;
102 Tcl_Obj *pElem;
103 if( azCol==0 ) return 1;
104 if( nCol==0 ) return 1;
105#ifdef UTF_TRANSLATION_NEEDED
106 {
107 Tcl_DString dCol;
108 Tcl_DStringInit(&dCol);
109 Tcl_ExternalToUtfDString(NULL, azCol[0], -1, &dCol);
110 pElem = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
111 Tcl_DStringFree(&dCol);
112 }
113#else
114 pElem = Tcl_NewStringObj(azCol[0], -1);
115#endif
116 Tcl_SetObjResult(interp, pElem);
117 return 1;
118}
119
120/*
drh75897232000-05-29 14:26:00 +0000121** Called when the command is deleted.
122*/
123static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000124 SqliteDb *pDb = (SqliteDb*)db;
danielk19776f8a5032004-05-10 10:34:51 +0000125 sqlite3_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000126 while( pDb->pFunc ){
127 SqlFunc *pFunc = pDb->pFunc;
128 pDb->pFunc = pFunc->pNext;
129 Tcl_Free((char*)pFunc);
130 }
danielk19770202b292004-06-09 09:55:16 +0000131 while( pDb->pCollate ){
132 SqlCollate *pCollate = pDb->pCollate;
133 pDb->pCollate = pCollate->pNext;
134 Tcl_Free((char*)pCollate);
135 }
drhbec3f402000-08-04 13:49:02 +0000136 if( pDb->zBusy ){
137 Tcl_Free(pDb->zBusy);
138 }
drhb5a20d32003-04-23 12:25:23 +0000139 if( pDb->zTrace ){
140 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000141 }
drhe22a3342003-04-22 20:30:37 +0000142 if( pDb->zAuth ){
143 Tcl_Free(pDb->zAuth);
144 }
drhbec3f402000-08-04 13:49:02 +0000145 Tcl_Free((char*)pDb);
146}
147
148/*
149** This routine is called when a database file is locked while trying
150** to execute SQL.
151*/
152static int DbBusyHandler(void *cd, const char *zTable, int nTries){
153 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);
161 Tcl_DStringAppendElement(&cmd, zTable);
162 sprintf(zVal, " %d", nTries);
163 Tcl_DStringAppend(&cmd, zVal, -1);
164 zCmd = Tcl_DStringValue(&cmd);
165 rc = Tcl_Eval(pDb->interp, zCmd);
166 Tcl_DStringFree(&cmd);
167 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
168 return 0;
169 }
170 return 1;
drh75897232000-05-29 14:26:00 +0000171}
172
173/*
danielk1977348bb5d2003-10-18 09:37:26 +0000174** This routine is invoked as the 'progress callback' for the database.
175*/
176static int DbProgressHandler(void *cd){
177 SqliteDb *pDb = (SqliteDb*)cd;
178 int rc;
179
180 assert( pDb->zProgress );
181 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
182 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
183 return 1;
184 }
185 return 0;
186}
187
188/*
drhb5a20d32003-04-23 12:25:23 +0000189** This routine is called by the SQLite trace handler whenever a new
190** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000191*/
drhb5a20d32003-04-23 12:25:23 +0000192static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000193 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000194 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000195
drhb5a20d32003-04-23 12:25:23 +0000196 Tcl_DStringInit(&str);
197 Tcl_DStringAppend(&str, pDb->zTrace, -1);
198 Tcl_DStringAppendElement(&str, zSql);
199 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
200 Tcl_DStringFree(&str);
201 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000202}
203
204/*
drhaa940ea2004-01-15 02:44:03 +0000205** This routine is called when a transaction is committed. The
206** TCL script in pDb->zCommit is executed. If it returns non-zero or
207** if it throws an exception, the transaction is rolled back instead
208** of being committed.
209*/
210static int DbCommitHandler(void *cd){
211 SqliteDb *pDb = (SqliteDb*)cd;
212 int rc;
213
214 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
215 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
216 return 1;
217 }
218 return 0;
219}
220
danielk19777cedc8d2004-06-10 10:50:08 +0000221static void tclCollateNeeded(
222 void *pCtx,
223 sqlite *db,
224 int enc,
225 const char *zName
226){
227 SqliteDb *pDb = (SqliteDb *)pCtx;
228 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
229 Tcl_IncrRefCount(pScript);
230 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
231 Tcl_EvalObjEx(pDb->interp, pScript, 0);
232 Tcl_DecrRefCount(pScript);
233}
234
drhaa940ea2004-01-15 02:44:03 +0000235/*
danielk19770202b292004-06-09 09:55:16 +0000236** This routine is called to evaluate an SQL collation function implemented
237** using TCL script.
238*/
239static int tclSqlCollate(
240 void *pCtx,
241 int nA,
242 const void *zA,
243 int nB,
244 const void *zB
245){
246 SqlCollate *p = (SqlCollate *)pCtx;
247 Tcl_Obj *pCmd;
248
249 pCmd = Tcl_NewStringObj(p->zScript, -1);
250 Tcl_IncrRefCount(pCmd);
251 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA));
252 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB));
253 Tcl_EvalObjEx(p->interp, pCmd, 0);
254 Tcl_DecrRefCount(pCmd);
255 return (atoi(Tcl_GetStringResult(p->interp)));
256}
257
258/*
drhcabb0812002-09-14 13:47:32 +0000259** This routine is called to evaluate an SQL function implemented
260** using TCL script.
261*/
danielk19770ae8b832004-05-25 12:05:56 +0000262static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
danielk19776f8a5032004-05-10 10:34:51 +0000263 SqlFunc *p = sqlite3_user_data(context);
drhcabb0812002-09-14 13:47:32 +0000264 Tcl_DString cmd;
265 int i;
266 int rc;
267
268 Tcl_DStringInit(&cmd);
269 Tcl_DStringAppend(&cmd, p->zScript, -1);
270 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000271 if( SQLITE_NULL==sqlite3_value_type(argv[i]) ){
danielk197751ad0ec2004-05-24 12:39:02 +0000272 Tcl_DStringAppendElement(&cmd, "");
273 }else{
drh4f26d6c2004-05-26 23:25:30 +0000274 Tcl_DStringAppendElement(&cmd, sqlite3_value_text(argv[i]));
danielk197751ad0ec2004-05-24 12:39:02 +0000275 }
drhcabb0812002-09-14 13:47:32 +0000276 }
277 rc = Tcl_Eval(p->interp, Tcl_DStringValue(&cmd));
278 if( rc ){
danielk19777e18c252004-05-25 11:47:24 +0000279 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhcabb0812002-09-14 13:47:32 +0000280 }else{
danielk19777e18c252004-05-25 11:47:24 +0000281 sqlite3_result_text(context, Tcl_GetStringResult(p->interp), -1, 1);
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: {
662 Tcl_SetObjResult(interp, Tcl_NewIntObj(pDb->rc));
663 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);
danielk197765904932004-05-26 06:18:37 +0000788 sqlite3_create_function(pDb->db, zName, -1, 0, 0, pFunc, tclSqlFunc, 0, 0);
drhcabb0812002-09-14 13:47:32 +0000789 break;
790 }
791
792 /*
drhaf9ff332002-01-16 21:00:27 +0000793 ** $db last_insert_rowid
794 **
795 ** Return an integer which is the ROWID for the most recent insert.
796 */
797 case DB_LAST_INSERT_ROWID: {
798 Tcl_Obj *pResult;
799 int rowid;
800 if( objc!=2 ){
801 Tcl_WrongNumArgs(interp, 2, objv, "");
802 return TCL_ERROR;
803 }
danielk19776f8a5032004-05-10 10:34:51 +0000804 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +0000805 pResult = Tcl_GetObjResult(interp);
806 Tcl_SetIntObj(pResult, rowid);
807 break;
808 }
809
810 /*
drh5d9d7572003-08-19 14:31:01 +0000811 ** $db onecolumn SQL
812 **
813 ** Return a single column from a single row of the given SQL query.
814 */
815 case DB_ONECOLUMN: {
drh5d9d7572003-08-19 14:31:01 +0000816 char *zSql;
817 char *zErrMsg = 0;
818 if( objc!=3 ){
819 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
820 return TCL_ERROR;
821 }
822 zSql = Tcl_GetStringFromObj(objv[2], 0);
danielk19776f8a5032004-05-10 10:34:51 +0000823 rc = sqlite3_exec(pDb->db, zSql, DbEvalCallback3, interp, &zErrMsg);
drh5d9d7572003-08-19 14:31:01 +0000824 if( rc==SQLITE_ABORT ){
drh22fbcb82004-02-01 01:22:50 +0000825 rc = SQLITE_OK;
drh5d9d7572003-08-19 14:31:01 +0000826 }else if( zErrMsg ){
827 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
828 free(zErrMsg);
829 rc = TCL_ERROR;
830 }else if( rc!=SQLITE_OK ){
danielk1977f20b21c2004-05-31 23:56:42 +0000831 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh5d9d7572003-08-19 14:31:01 +0000832 rc = TCL_ERROR;
833 }
834 break;
835 }
836
837 /*
drh22fbcb82004-02-01 01:22:50 +0000838 ** $db rekey KEY
839 **
840 ** Change the encryption key on the currently open database.
841 */
842 case DB_REKEY: {
843 int nKey;
844 void *pKey;
845 if( objc!=3 ){
846 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
847 return TCL_ERROR;
848 }
849 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +0000850#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000851 rc = sqlite_rekey(pDb->db, pKey, nKey);
852 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000853 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh22fbcb82004-02-01 01:22:50 +0000854 rc = TCL_ERROR;
855 }
856#endif
857 break;
858 }
859
860 /*
drhbec3f402000-08-04 13:49:02 +0000861 ** $db timeout MILLESECONDS
862 **
863 ** Delay for the number of milliseconds specified when a file is locked.
864 */
drh6d313162000-09-21 13:01:35 +0000865 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +0000866 int ms;
drh6d313162000-09-21 13:01:35 +0000867 if( objc!=3 ){
868 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +0000869 return TCL_ERROR;
870 }
drh6d313162000-09-21 13:01:35 +0000871 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +0000872 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +0000873 break;
drh75897232000-05-29 14:26:00 +0000874 }
drhb5a20d32003-04-23 12:25:23 +0000875
876 /* $db trace ?CALLBACK?
877 **
878 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
879 ** that is executed. The text of the SQL is appended to CALLBACK before
880 ** it is executed.
881 */
882 case DB_TRACE: {
883 if( objc>3 ){
884 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
885 }else if( objc==2 ){
886 if( pDb->zTrace ){
887 Tcl_AppendResult(interp, pDb->zTrace, 0);
888 }
889 }else{
890 char *zTrace;
891 int len;
892 if( pDb->zTrace ){
893 Tcl_Free(pDb->zTrace);
894 }
895 zTrace = Tcl_GetStringFromObj(objv[2], &len);
896 if( zTrace && len>0 ){
897 pDb->zTrace = Tcl_Alloc( len + 1 );
898 strcpy(pDb->zTrace, zTrace);
899 }else{
900 pDb->zTrace = 0;
901 }
902 if( pDb->zTrace ){
903 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +0000904 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +0000905 }else{
danielk19776f8a5032004-05-10 10:34:51 +0000906 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +0000907 }
908 }
909 break;
910 }
911
danielk19770202b292004-06-09 09:55:16 +0000912 /*
913 ** $db collate NAME SCRIPT
914 **
915 ** Create a new SQL collation function called NAME. Whenever
916 ** that function is called, invoke SCRIPT to evaluate the function.
917 */
918 case DB_COLLATE: {
919 SqlCollate *pCollate;
920 char *zName;
921 char *zScript;
922 int nScript;
923 if( objc!=4 ){
924 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
925 return TCL_ERROR;
926 }
927 zName = Tcl_GetStringFromObj(objv[2], 0);
928 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
929 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
930 if( pCollate==0 ) return TCL_ERROR;
931 pCollate->interp = interp;
932 pCollate->pNext = pDb->pCollate;
933 pCollate->zScript = (char*)&pCollate[1];
934 strcpy(pCollate->zScript, zScript);
danielk1977466be562004-06-10 02:16:01 +0000935 if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
936 pCollate, tclSqlCollate) ){
danielk19770202b292004-06-09 09:55:16 +0000937 return TCL_ERROR;
938 }
939 break;
940 }
941
danielk19777cedc8d2004-06-10 10:50:08 +0000942 /*
943 ** $db collate_needed SCRIPT
944 **
945 ** Create a new SQL collation function called NAME. Whenever
946 ** that function is called, invoke SCRIPT to evaluate the function.
947 */
948 case DB_COLLATION_NEEDED: {
949 if( objc!=3 ){
950 Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT");
951 return TCL_ERROR;
952 }
953 if( pDb->pCollateNeeded ){
954 Tcl_DecrRefCount(pDb->pCollateNeeded);
955 }
956 pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]);
957 Tcl_IncrRefCount(pDb->pCollateNeeded);
958 sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded);
959 break;
960 }
961
drh6d313162000-09-21 13:01:35 +0000962 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +0000963 return rc;
drh75897232000-05-29 14:26:00 +0000964}
965
966/*
drh22fbcb82004-02-01 01:22:50 +0000967** sqlite DBNAME FILENAME ?MODE? ?-key KEY?
drh75897232000-05-29 14:26:00 +0000968**
969** This is the main Tcl command. When the "sqlite" Tcl command is
970** invoked, this routine runs to process that command.
971**
972** The first argument, DBNAME, is an arbitrary name for a new
973** database connection. This command creates a new command named
974** DBNAME that is used to control that connection. The database
975** connection is deleted when the DBNAME command is deleted.
976**
977** The second argument is the name of the directory that contains
978** the sqlite database that is to be accessed.
drhfbc3eab2001-04-06 16:13:42 +0000979**
980** For testing purposes, we also support the following:
981**
982** sqlite -encoding
983**
984** Return the encoding used by LIKE and GLOB operators. Choices
985** are UTF-8 and iso8859.
986**
drh647cb0e2002-11-04 19:32:25 +0000987** sqlite -version
988**
989** Return the version number of the SQLite library.
990**
drhfbc3eab2001-04-06 16:13:42 +0000991** sqlite -tcl-uses-utf
992**
993** Return "1" if compiled with a Tcl uses UTF-8. Return "0" if
994** not. Used by tests to make sure the library was compiled
995** correctly.
drh75897232000-05-29 14:26:00 +0000996*/
drh22fbcb82004-02-01 01:22:50 +0000997static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000998 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +0000999 void *pKey = 0;
1000 int nKey = 0;
1001 const char *zArg;
drh75897232000-05-29 14:26:00 +00001002 char *zErrMsg;
drh22fbcb82004-02-01 01:22:50 +00001003 const char *zFile;
drh06b27182002-06-26 20:06:05 +00001004 char zBuf[80];
drh22fbcb82004-02-01 01:22:50 +00001005 if( objc==2 ){
1006 zArg = Tcl_GetStringFromObj(objv[1], 0);
drh22fbcb82004-02-01 01:22:50 +00001007 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00001008 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +00001009 return TCL_OK;
1010 }
drh9eb9e262004-02-11 02:18:05 +00001011 if( strcmp(zArg,"-has-codec")==0 ){
1012#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001013 Tcl_AppendResult(interp,"1",0);
1014#else
1015 Tcl_AppendResult(interp,"0",0);
1016#endif
1017 return TCL_OK;
1018 }
1019 if( strcmp(zArg,"-tcl-uses-utf")==0 ){
drhfbc3eab2001-04-06 16:13:42 +00001020#ifdef TCL_UTF_MAX
1021 Tcl_AppendResult(interp,"1",0);
1022#else
1023 Tcl_AppendResult(interp,"0",0);
1024#endif
1025 return TCL_OK;
1026 }
1027 }
drh22fbcb82004-02-01 01:22:50 +00001028 if( objc==5 || objc==6 ){
1029 zArg = Tcl_GetStringFromObj(objv[objc-2], 0);
1030 if( strcmp(zArg,"-key")==0 ){
1031 pKey = Tcl_GetByteArrayFromObj(objv[objc-1], &nKey);
1032 objc -= 2;
1033 }
1034 }
1035 if( objc!=3 && objc!=4 ){
1036 Tcl_WrongNumArgs(interp, 1, objv,
drh9eb9e262004-02-11 02:18:05 +00001037#ifdef SQLITE_HAS_CODEC
1038 "HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +00001039#else
1040 "HANDLE FILENAME ?MODE?"
1041#endif
1042 );
drh75897232000-05-29 14:26:00 +00001043 return TCL_ERROR;
1044 }
drh75897232000-05-29 14:26:00 +00001045 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00001046 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00001047 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00001048 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
1049 return TCL_ERROR;
1050 }
1051 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00001052 zFile = Tcl_GetStringFromObj(objv[2], 0);
drh9eb9e262004-02-11 02:18:05 +00001053#ifdef SQLITE_HAS_CODEC
danielk19776f8a5032004-05-10 10:34:51 +00001054 p->db = sqlite3_open_encrypted(zFile, pKey, nKey, 0, &zErrMsg);
drheb8ed702004-02-11 10:37:23 +00001055#else
danielk19774f057f92004-06-08 00:02:33 +00001056 sqlite3_open(zFile, &p->db);
danielk197780290862004-05-22 09:21:21 +00001057 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
1058 zErrMsg = strdup(sqlite3_errmsg(p->db));
1059 sqlite3_close(p->db);
1060 p->db = 0;
1061 }
drheb8ed702004-02-11 10:37:23 +00001062#endif
drhbec3f402000-08-04 13:49:02 +00001063 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00001064 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00001065 Tcl_Free((char*)p);
drh75897232000-05-29 14:26:00 +00001066 free(zErrMsg);
1067 return TCL_ERROR;
1068 }
drh22fbcb82004-02-01 01:22:50 +00001069 zArg = Tcl_GetStringFromObj(objv[1], 0);
1070 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drhc22bd472002-05-10 13:14:07 +00001071
drh06b27182002-06-26 20:06:05 +00001072 /* The return value is the value of the sqlite* pointer
1073 */
1074 sprintf(zBuf, "%p", p->db);
drh5e5377f2002-07-07 17:12:36 +00001075 if( strncmp(zBuf,"0x",2) ){
1076 sprintf(zBuf, "0x%p", p->db);
1077 }
drh06b27182002-06-26 20:06:05 +00001078 Tcl_AppendResult(interp, zBuf, 0);
1079
drhc22bd472002-05-10 13:14:07 +00001080 /* If compiled with SQLITE_TEST turned on, then register the "md5sum"
drh06b27182002-06-26 20:06:05 +00001081 ** SQL function.
drhc22bd472002-05-10 13:14:07 +00001082 */
drh28b4e482002-03-11 02:06:13 +00001083#ifdef SQLITE_TEST
1084 {
drhc22bd472002-05-10 13:14:07 +00001085 extern void Md5_Register(sqlite*);
1086 Md5_Register(p->db);
drh06b27182002-06-26 20:06:05 +00001087 }
drh28b4e482002-03-11 02:06:13 +00001088#endif
danielk19777cedc8d2004-06-10 10:50:08 +00001089 p->interp = interp;
drh75897232000-05-29 14:26:00 +00001090 return TCL_OK;
1091}
1092
1093/*
drh90ca9752001-09-28 17:47:14 +00001094** Provide a dummy Tcl_InitStubs if we are using this as a static
1095** library.
1096*/
1097#ifndef USE_TCL_STUBS
1098# undef Tcl_InitStubs
1099# define Tcl_InitStubs(a,b,c)
1100#endif
1101
1102/*
drh75897232000-05-29 14:26:00 +00001103** Initialize this module.
1104**
1105** This Tcl module contains only a single new Tcl command named "sqlite".
1106** (Hence there is no namespace. There is no point in using a namespace
1107** if the extension only supplies one new name!) The "sqlite" command is
1108** used to open a new SQLite database. See the DbMain() routine above
1109** for additional information.
1110*/
1111int Sqlite_Init(Tcl_Interp *interp){
drh90ca9752001-09-28 17:47:14 +00001112 Tcl_InitStubs(interp, "8.0", 0);
drh22fbcb82004-02-01 01:22:50 +00001113 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001114 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh90ca9752001-09-28 17:47:14 +00001115 return TCL_OK;
1116}
1117int Tclsqlite_Init(Tcl_Interp *interp){
1118 Tcl_InitStubs(interp, "8.0", 0);
drh22fbcb82004-02-01 01:22:50 +00001119 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +00001120 Tcl_PkgProvide(interp, "sqlite", "2.0");
drh75897232000-05-29 14:26:00 +00001121 return TCL_OK;
1122}
1123int Sqlite_SafeInit(Tcl_Interp *interp){
1124 return TCL_OK;
1125}
drh90ca9752001-09-28 17:47:14 +00001126int Tclsqlite_SafeInit(Tcl_Interp *interp){
1127 return TCL_OK;
1128}
drh75897232000-05-29 14:26:00 +00001129
drh3cebbde2000-10-19 14:59:27 +00001130#if 0
drh75897232000-05-29 14:26:00 +00001131/*
1132** If compiled using mktclapp, this routine runs to initialize
1133** everything.
1134*/
1135int Et_AppInit(Tcl_Interp *interp){
1136 return Sqlite_Init(interp);
1137}
drh3cebbde2000-10-19 14:59:27 +00001138#endif
drh348784e2000-05-29 20:41:49 +00001139
1140/*
1141** If the macro TCLSH is defined and is one, then put in code for the
1142** "main" routine that will initialize Tcl.
1143*/
1144#if defined(TCLSH) && TCLSH==1
1145static char zMainloop[] =
1146 "set line {}\n"
1147 "while {![eof stdin]} {\n"
1148 "if {$line!=\"\"} {\n"
1149 "puts -nonewline \"> \"\n"
1150 "} else {\n"
1151 "puts -nonewline \"% \"\n"
1152 "}\n"
1153 "flush stdout\n"
1154 "append line [gets stdin]\n"
1155 "if {[info complete $line]} {\n"
1156 "if {[catch {uplevel #0 $line} result]} {\n"
1157 "puts stderr \"Error: $result\"\n"
1158 "} elseif {$result!=\"\"} {\n"
1159 "puts $result\n"
1160 "}\n"
1161 "set line {}\n"
1162 "} else {\n"
1163 "append line \\n\n"
1164 "}\n"
1165 "}\n"
1166;
1167
1168#define TCLSH_MAIN main /* Needed to fake out mktclapp */
1169int TCLSH_MAIN(int argc, char **argv){
1170 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00001171 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00001172 interp = Tcl_CreateInterp();
danielk19774adee202004-05-08 08:23:19 +00001173 Sqlite_Init(interp);
drhd9b02572001-04-15 00:37:09 +00001174#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00001175 {
1176 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00001177 extern int Sqlitetest2_Init(Tcl_Interp*);
1178 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00001179 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00001180 extern int Sqlitetest5_Init(Tcl_Interp*);
drhefc251d2001-07-01 22:12:01 +00001181 extern int Md5_Init(Tcl_Interp*);
danielk19776490beb2004-05-11 06:17:21 +00001182 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00001183 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00001184 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00001185 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00001186 Sqlitetest5_Init(interp);
drhefc251d2001-07-01 22:12:01 +00001187 Md5_Init(interp);
drhd1bf3512001-04-07 15:24:33 +00001188 }
1189#endif
drh348784e2000-05-29 20:41:49 +00001190 if( argc>=2 ){
1191 int i;
1192 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
1193 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
1194 for(i=2; i<argc; i++){
1195 Tcl_SetVar(interp, "argv", argv[i],
1196 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
1197 }
1198 if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00001199 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00001200 if( zInfo==0 ) zInfo = interp->result;
1201 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00001202 return 1;
1203 }
1204 }else{
1205 Tcl_GlobalEval(interp, zMainloop);
1206 }
1207 return 0;
1208}
1209#endif /* TCLSH */
drh6d313162000-09-21 13:01:35 +00001210
1211#endif /* !defined(NO_TCL) */