blob: e3fd44c1a0d73f460c60417fcdb403f29b8a13a2 [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*************************************************************************
drhbd08af42007-04-05 21:58:33 +000012** A TCL Interface to SQLite. Append this file to sqlite3.c and
13** compile the whole thing to build a TCL-enabled version of SQLite.
drh75897232000-05-29 14:26:00 +000014**
danielk1977ab9b7032008-12-30 06:24:58 +000015** $Id: tclsqlite.c,v 1.232 2008/12/30 06:24:58 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000016*/
drh17a68932001-01-31 13:28:08 +000017#include "tcl.h"
danielk1977b4e9af92007-05-01 17:49:49 +000018#include <errno.h>
drhbd08af42007-04-05 21:58:33 +000019
20/*
21** Some additional include files are needed if this file is not
22** appended to the amalgamation.
23*/
24#ifndef SQLITE_AMALGAMATION
25# include "sqliteInt.h"
drhbd08af42007-04-05 21:58:33 +000026# include <stdlib.h>
27# include <string.h>
28# include <assert.h>
29# include <ctype.h>
30#endif
drh75897232000-05-29 14:26:00 +000031
drhad6e1372006-07-10 21:15:51 +000032/*
33 * Windows needs to know which symbols to export. Unix does not.
34 * BUILD_sqlite should be undefined for Unix.
35 */
36#ifdef BUILD_sqlite
37#undef TCL_STORAGE_CLASS
38#define TCL_STORAGE_CLASS DLLEXPORT
39#endif /* BUILD_sqlite */
drh29bc4612005-10-05 10:40:15 +000040
danielk1977a21c6b62005-01-24 10:25:59 +000041#define NUM_PREPARED_STMTS 10
drhfb7e7652005-01-24 00:28:42 +000042#define MAX_PREPARED_STMTS 100
43
drh75897232000-05-29 14:26:00 +000044/*
drh98808ba2001-10-18 12:34:46 +000045** If TCL uses UTF-8 and SQLite is configured to use iso8859, then we
46** have to do a translation when going between the two. Set the
47** UTF_TRANSLATION_NEEDED macro to indicate that we need to do
48** this translation.
49*/
50#if defined(TCL_UTF_MAX) && !defined(SQLITE_UTF8)
51# define UTF_TRANSLATION_NEEDED 1
52#endif
53
54/*
drhcabb0812002-09-14 13:47:32 +000055** New SQL functions can be created as TCL scripts. Each such function
56** is described by an instance of the following structure.
57*/
58typedef struct SqlFunc SqlFunc;
59struct SqlFunc {
60 Tcl_Interp *interp; /* The TCL interpret to execute the function */
drhd1e47332005-06-26 17:55:33 +000061 Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */
62 int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */
63 char *zName; /* Name of this function */
drhcabb0812002-09-14 13:47:32 +000064 SqlFunc *pNext; /* Next function on the list of them all */
65};
66
67/*
danielk19770202b292004-06-09 09:55:16 +000068** New collation sequences function can be created as TCL scripts. Each such
69** function is described by an instance of the following structure.
70*/
71typedef struct SqlCollate SqlCollate;
72struct SqlCollate {
73 Tcl_Interp *interp; /* The TCL interpret to execute the function */
74 char *zScript; /* The script to be run */
drhd1e47332005-06-26 17:55:33 +000075 SqlCollate *pNext; /* Next function on the list of them all */
danielk19770202b292004-06-09 09:55:16 +000076};
77
78/*
drhfb7e7652005-01-24 00:28:42 +000079** Prepared statements are cached for faster execution. Each prepared
80** statement is described by an instance of the following structure.
81*/
82typedef struct SqlPreparedStmt SqlPreparedStmt;
83struct SqlPreparedStmt {
84 SqlPreparedStmt *pNext; /* Next in linked list */
85 SqlPreparedStmt *pPrev; /* Previous on the list */
86 sqlite3_stmt *pStmt; /* The prepared statement */
87 int nSql; /* chars in zSql[] */
danielk1977d0e2a852007-11-14 06:48:48 +000088 const char *zSql; /* Text of the SQL statement */
drhfb7e7652005-01-24 00:28:42 +000089};
90
danielk1977d04417962007-05-02 13:16:30 +000091typedef struct IncrblobChannel IncrblobChannel;
92
drhfb7e7652005-01-24 00:28:42 +000093/*
drhbec3f402000-08-04 13:49:02 +000094** There is one instance of this structure for each SQLite database
95** that has been opened by the SQLite TCL interface.
96*/
97typedef struct SqliteDb SqliteDb;
98struct SqliteDb {
drhdddca282006-01-03 00:33:50 +000099 sqlite3 *db; /* The "real" database structure. MUST BE FIRST */
drhd1e47332005-06-26 17:55:33 +0000100 Tcl_Interp *interp; /* The interpreter used for this database */
101 char *zBusy; /* The busy callback routine */
102 char *zCommit; /* The commit hook callback routine */
103 char *zTrace; /* The trace callback routine */
drh19e2d372005-08-29 23:00:03 +0000104 char *zProfile; /* The profile callback routine */
drhd1e47332005-06-26 17:55:33 +0000105 char *zProgress; /* The progress callback routine */
106 char *zAuth; /* The authorization callback routine */
drh1f1549f2008-08-26 21:33:34 +0000107 int disableAuth; /* Disable the authorizer if it exists */
drhd1e47332005-06-26 17:55:33 +0000108 char *zNull; /* Text to substitute for an SQL NULL value */
109 SqlFunc *pFunc; /* List of SQL functions */
danielk197794eb6a12005-12-15 15:22:08 +0000110 Tcl_Obj *pUpdateHook; /* Update hook script (if any) */
danielk197771fd80b2005-12-16 06:54:01 +0000111 Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */
drhd1e47332005-06-26 17:55:33 +0000112 SqlCollate *pCollate; /* List of SQL collation functions */
113 int rc; /* Return code of most recent sqlite3_exec() */
114 Tcl_Obj *pCollateNeeded; /* Collation needed script */
drhfb7e7652005-01-24 00:28:42 +0000115 SqlPreparedStmt *stmtList; /* List of prepared statements*/
116 SqlPreparedStmt *stmtLast; /* Last statement in the list */
117 int maxStmt; /* The next maximum number of stmtList */
118 int nStmt; /* Number of statements in stmtList */
danielk1977d04417962007-05-02 13:16:30 +0000119 IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */
drhd1d38482008-10-07 23:46:38 +0000120 int nStep, nSort; /* Statistics for most recent operation */
drh98808ba2001-10-18 12:34:46 +0000121};
drh297ecf12001-04-05 15:57:13 +0000122
danielk1977b4e9af92007-05-01 17:49:49 +0000123struct IncrblobChannel {
danielk1977d04417962007-05-02 13:16:30 +0000124 sqlite3_blob *pBlob; /* sqlite3 blob handle */
danielk1977dcbb5d32007-05-04 18:36:44 +0000125 SqliteDb *pDb; /* Associated database connection */
danielk1977d04417962007-05-02 13:16:30 +0000126 int iSeek; /* Current seek offset */
danielk1977d04417962007-05-02 13:16:30 +0000127 Tcl_Channel channel; /* Channel identifier */
128 IncrblobChannel *pNext; /* Linked list of all open incrblob channels */
129 IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */
danielk1977b4e9af92007-05-01 17:49:49 +0000130};
131
drhea678832008-12-10 19:26:22 +0000132/*
133** Compute a string length that is limited to what can be stored in
134** lower 30 bits of a 32-bit signed integer.
135*/
drh4f21c4a2008-12-10 22:15:00 +0000136static int strlen30(const char *z){
drhea678832008-12-10 19:26:22 +0000137 const char *z2 = z;
138 while( *z2 ){ z2++; }
139 return 0x3fffffff & (int)(z2 - z);
140}
drhea678832008-12-10 19:26:22 +0000141
142
danielk197732a0d8b2007-05-04 19:03:02 +0000143#ifndef SQLITE_OMIT_INCRBLOB
danielk1977b4e9af92007-05-01 17:49:49 +0000144/*
danielk1977d04417962007-05-02 13:16:30 +0000145** Close all incrblob channels opened using database connection pDb.
146** This is called when shutting down the database connection.
147*/
148static void closeIncrblobChannels(SqliteDb *pDb){
149 IncrblobChannel *p;
150 IncrblobChannel *pNext;
151
152 for(p=pDb->pIncrblob; p; p=pNext){
153 pNext = p->pNext;
154
155 /* Note: Calling unregister here call Tcl_Close on the incrblob channel,
156 ** which deletes the IncrblobChannel structure at *p. So do not
157 ** call Tcl_Free() here.
158 */
159 Tcl_UnregisterChannel(pDb->interp, p->channel);
160 }
161}
162
163/*
danielk1977b4e9af92007-05-01 17:49:49 +0000164** Close an incremental blob channel.
165*/
166static int incrblobClose(ClientData instanceData, Tcl_Interp *interp){
167 IncrblobChannel *p = (IncrblobChannel *)instanceData;
danielk197792d4d7a2007-05-04 12:05:56 +0000168 int rc = sqlite3_blob_close(p->pBlob);
169 sqlite3 *db = p->pDb->db;
danielk1977d04417962007-05-02 13:16:30 +0000170
171 /* Remove the channel from the SqliteDb.pIncrblob list. */
172 if( p->pNext ){
173 p->pNext->pPrev = p->pPrev;
174 }
175 if( p->pPrev ){
176 p->pPrev->pNext = p->pNext;
177 }
178 if( p->pDb->pIncrblob==p ){
179 p->pDb->pIncrblob = p->pNext;
180 }
181
danielk197792d4d7a2007-05-04 12:05:56 +0000182 /* Free the IncrblobChannel structure */
danielk1977b4e9af92007-05-01 17:49:49 +0000183 Tcl_Free((char *)p);
danielk197792d4d7a2007-05-04 12:05:56 +0000184
185 if( rc!=SQLITE_OK ){
186 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
187 return TCL_ERROR;
188 }
danielk1977b4e9af92007-05-01 17:49:49 +0000189 return TCL_OK;
190}
191
192/*
193** Read data from an incremental blob channel.
194*/
195static int incrblobInput(
196 ClientData instanceData,
197 char *buf,
198 int bufSize,
199 int *errorCodePtr
200){
201 IncrblobChannel *p = (IncrblobChannel *)instanceData;
202 int nRead = bufSize; /* Number of bytes to read */
203 int nBlob; /* Total size of the blob */
204 int rc; /* sqlite error code */
205
206 nBlob = sqlite3_blob_bytes(p->pBlob);
207 if( (p->iSeek+nRead)>nBlob ){
208 nRead = nBlob-p->iSeek;
209 }
210 if( nRead<=0 ){
211 return 0;
212 }
213
214 rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek);
215 if( rc!=SQLITE_OK ){
216 *errorCodePtr = rc;
217 return -1;
218 }
219
220 p->iSeek += nRead;
221 return nRead;
222}
223
danielk1977d04417962007-05-02 13:16:30 +0000224/*
225** Write data to an incremental blob channel.
226*/
danielk1977b4e9af92007-05-01 17:49:49 +0000227static int incrblobOutput(
228 ClientData instanceData,
229 CONST char *buf,
230 int toWrite,
231 int *errorCodePtr
232){
233 IncrblobChannel *p = (IncrblobChannel *)instanceData;
234 int nWrite = toWrite; /* Number of bytes to write */
235 int nBlob; /* Total size of the blob */
236 int rc; /* sqlite error code */
237
238 nBlob = sqlite3_blob_bytes(p->pBlob);
239 if( (p->iSeek+nWrite)>nBlob ){
240 *errorCodePtr = EINVAL;
241 return -1;
242 }
243 if( nWrite<=0 ){
244 return 0;
245 }
246
247 rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek);
248 if( rc!=SQLITE_OK ){
249 *errorCodePtr = EIO;
250 return -1;
251 }
252
253 p->iSeek += nWrite;
254 return nWrite;
255}
256
257/*
258** Seek an incremental blob channel.
259*/
260static int incrblobSeek(
261 ClientData instanceData,
262 long offset,
263 int seekMode,
264 int *errorCodePtr
265){
266 IncrblobChannel *p = (IncrblobChannel *)instanceData;
267
268 switch( seekMode ){
269 case SEEK_SET:
270 p->iSeek = offset;
271 break;
272 case SEEK_CUR:
273 p->iSeek += offset;
274 break;
275 case SEEK_END:
276 p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset;
277 break;
278
279 default: assert(!"Bad seekMode");
280 }
281
282 return p->iSeek;
283}
284
285
286static void incrblobWatch(ClientData instanceData, int mode){
287 /* NO-OP */
288}
289static int incrblobHandle(ClientData instanceData, int dir, ClientData *hPtr){
290 return TCL_ERROR;
291}
292
293static Tcl_ChannelType IncrblobChannelType = {
294 "incrblob", /* typeName */
295 TCL_CHANNEL_VERSION_2, /* version */
296 incrblobClose, /* closeProc */
297 incrblobInput, /* inputProc */
298 incrblobOutput, /* outputProc */
299 incrblobSeek, /* seekProc */
300 0, /* setOptionProc */
301 0, /* getOptionProc */
302 incrblobWatch, /* watchProc (this is a no-op) */
303 incrblobHandle, /* getHandleProc (always returns error) */
304 0, /* close2Proc */
305 0, /* blockModeProc */
306 0, /* flushProc */
307 0, /* handlerProc */
308 0, /* wideSeekProc */
danielk1977b4e9af92007-05-01 17:49:49 +0000309};
310
311/*
312** Create a new incrblob channel.
313*/
314static int createIncrblobChannel(
315 Tcl_Interp *interp,
316 SqliteDb *pDb,
317 const char *zDb,
318 const char *zTable,
319 const char *zColumn,
danielk19778cbadb02007-05-03 16:31:26 +0000320 sqlite_int64 iRow,
321 int isReadonly
danielk1977b4e9af92007-05-01 17:49:49 +0000322){
323 IncrblobChannel *p;
danielk19778cbadb02007-05-03 16:31:26 +0000324 sqlite3 *db = pDb->db;
danielk1977b4e9af92007-05-01 17:49:49 +0000325 sqlite3_blob *pBlob;
326 int rc;
danielk19778cbadb02007-05-03 16:31:26 +0000327 int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE);
danielk1977b4e9af92007-05-01 17:49:49 +0000328
329 /* This variable is used to name the channels: "incrblob_[incr count]" */
330 static int count = 0;
331 char zChannel[64];
332
danielk19778cbadb02007-05-03 16:31:26 +0000333 rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob);
danielk1977b4e9af92007-05-01 17:49:49 +0000334 if( rc!=SQLITE_OK ){
335 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
336 return TCL_ERROR;
337 }
338
339 p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel));
340 p->iSeek = 0;
341 p->pBlob = pBlob;
342
drh5bb3eb92007-05-04 13:15:55 +0000343 sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count);
danielk1977d04417962007-05-02 13:16:30 +0000344 p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags);
345 Tcl_RegisterChannel(interp, p->channel);
danielk1977b4e9af92007-05-01 17:49:49 +0000346
danielk1977d04417962007-05-02 13:16:30 +0000347 /* Link the new channel into the SqliteDb.pIncrblob list. */
348 p->pNext = pDb->pIncrblob;
349 p->pPrev = 0;
350 if( p->pNext ){
351 p->pNext->pPrev = p;
352 }
353 pDb->pIncrblob = p;
354 p->pDb = pDb;
355
356 Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE);
danielk1977b4e9af92007-05-01 17:49:49 +0000357 return TCL_OK;
358}
danielk197732a0d8b2007-05-04 19:03:02 +0000359#else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */
360 #define closeIncrblobChannels(pDb)
361#endif
danielk1977b4e9af92007-05-01 17:49:49 +0000362
drh6d313162000-09-21 13:01:35 +0000363/*
drhd1e47332005-06-26 17:55:33 +0000364** Look at the script prefix in pCmd. We will be executing this script
365** after first appending one or more arguments. This routine analyzes
366** the script to see if it is safe to use Tcl_EvalObjv() on the script
367** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much
368** faster.
369**
370** Scripts that are safe to use with Tcl_EvalObjv() consists of a
371** command name followed by zero or more arguments with no [...] or $
372** or {...} or ; to be seen anywhere. Most callback scripts consist
373** of just a single procedure name and they meet this requirement.
374*/
375static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){
376 /* We could try to do something with Tcl_Parse(). But we will instead
377 ** just do a search for forbidden characters. If any of the forbidden
378 ** characters appear in pCmd, we will report the string as unsafe.
379 */
380 const char *z;
381 int n;
382 z = Tcl_GetStringFromObj(pCmd, &n);
383 while( n-- > 0 ){
384 int c = *(z++);
385 if( c=='$' || c=='[' || c==';' ) return 0;
386 }
387 return 1;
388}
389
390/*
391** Find an SqlFunc structure with the given name. Or create a new
392** one if an existing one cannot be found. Return a pointer to the
393** structure.
394*/
395static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){
396 SqlFunc *p, *pNew;
397 int i;
drh4f21c4a2008-12-10 22:15:00 +0000398 pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + strlen30(zName) + 1 );
drhd1e47332005-06-26 17:55:33 +0000399 pNew->zName = (char*)&pNew[1];
400 for(i=0; zName[i]; i++){ pNew->zName[i] = tolower(zName[i]); }
401 pNew->zName[i] = 0;
402 for(p=pDb->pFunc; p; p=p->pNext){
403 if( strcmp(p->zName, pNew->zName)==0 ){
404 Tcl_Free((char*)pNew);
405 return p;
406 }
407 }
408 pNew->interp = pDb->interp;
409 pNew->pScript = 0;
410 pNew->pNext = pDb->pFunc;
411 pDb->pFunc = pNew;
412 return pNew;
413}
414
415/*
drhfb7e7652005-01-24 00:28:42 +0000416** Finalize and free a list of prepared statements
417*/
418static void flushStmtCache( SqliteDb *pDb ){
419 SqlPreparedStmt *pPreStmt;
420
421 while( pDb->stmtList ){
422 sqlite3_finalize( pDb->stmtList->pStmt );
423 pPreStmt = pDb->stmtList;
424 pDb->stmtList = pDb->stmtList->pNext;
425 Tcl_Free( (char*)pPreStmt );
426 }
427 pDb->nStmt = 0;
428 pDb->stmtLast = 0;
429}
430
431/*
drh895d7472004-08-20 16:02:39 +0000432** TCL calls this procedure when an sqlite3 database command is
433** deleted.
drh75897232000-05-29 14:26:00 +0000434*/
435static void DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000436 SqliteDb *pDb = (SqliteDb*)db;
drhfb7e7652005-01-24 00:28:42 +0000437 flushStmtCache(pDb);
danielk1977d04417962007-05-02 13:16:30 +0000438 closeIncrblobChannels(pDb);
danielk19776f8a5032004-05-10 10:34:51 +0000439 sqlite3_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000440 while( pDb->pFunc ){
441 SqlFunc *pFunc = pDb->pFunc;
442 pDb->pFunc = pFunc->pNext;
drhd1e47332005-06-26 17:55:33 +0000443 Tcl_DecrRefCount(pFunc->pScript);
drhcabb0812002-09-14 13:47:32 +0000444 Tcl_Free((char*)pFunc);
445 }
danielk19770202b292004-06-09 09:55:16 +0000446 while( pDb->pCollate ){
447 SqlCollate *pCollate = pDb->pCollate;
448 pDb->pCollate = pCollate->pNext;
449 Tcl_Free((char*)pCollate);
450 }
drhbec3f402000-08-04 13:49:02 +0000451 if( pDb->zBusy ){
452 Tcl_Free(pDb->zBusy);
453 }
drhb5a20d32003-04-23 12:25:23 +0000454 if( pDb->zTrace ){
455 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000456 }
drh19e2d372005-08-29 23:00:03 +0000457 if( pDb->zProfile ){
458 Tcl_Free(pDb->zProfile);
459 }
drhe22a3342003-04-22 20:30:37 +0000460 if( pDb->zAuth ){
461 Tcl_Free(pDb->zAuth);
462 }
danielk197755c45f22005-04-03 23:54:43 +0000463 if( pDb->zNull ){
464 Tcl_Free(pDb->zNull);
465 }
danielk197794eb6a12005-12-15 15:22:08 +0000466 if( pDb->pUpdateHook ){
467 Tcl_DecrRefCount(pDb->pUpdateHook);
468 }
danielk197771fd80b2005-12-16 06:54:01 +0000469 if( pDb->pRollbackHook ){
470 Tcl_DecrRefCount(pDb->pRollbackHook);
471 }
danielk197794eb6a12005-12-15 15:22:08 +0000472 if( pDb->pCollateNeeded ){
473 Tcl_DecrRefCount(pDb->pCollateNeeded);
474 }
drhbec3f402000-08-04 13:49:02 +0000475 Tcl_Free((char*)pDb);
476}
477
478/*
479** This routine is called when a database file is locked while trying
480** to execute SQL.
481*/
danielk19772a764eb2004-06-12 01:43:26 +0000482static int DbBusyHandler(void *cd, int nTries){
drhbec3f402000-08-04 13:49:02 +0000483 SqliteDb *pDb = (SqliteDb*)cd;
484 int rc;
485 char zVal[30];
drhbec3f402000-08-04 13:49:02 +0000486
drh5bb3eb92007-05-04 13:15:55 +0000487 sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries);
drhd1e47332005-06-26 17:55:33 +0000488 rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0);
drhbec3f402000-08-04 13:49:02 +0000489 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
490 return 0;
491 }
492 return 1;
drh75897232000-05-29 14:26:00 +0000493}
494
drh26e4a8b2008-05-01 17:16:52 +0000495#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh75897232000-05-29 14:26:00 +0000496/*
danielk1977348bb5d2003-10-18 09:37:26 +0000497** This routine is invoked as the 'progress callback' for the database.
498*/
499static int DbProgressHandler(void *cd){
500 SqliteDb *pDb = (SqliteDb*)cd;
501 int rc;
502
503 assert( pDb->zProgress );
504 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
505 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
506 return 1;
507 }
508 return 0;
509}
drh26e4a8b2008-05-01 17:16:52 +0000510#endif
danielk1977348bb5d2003-10-18 09:37:26 +0000511
drhd1167392006-01-23 13:00:35 +0000512#ifndef SQLITE_OMIT_TRACE
danielk1977348bb5d2003-10-18 09:37:26 +0000513/*
drhb5a20d32003-04-23 12:25:23 +0000514** This routine is called by the SQLite trace handler whenever a new
515** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000516*/
drhb5a20d32003-04-23 12:25:23 +0000517static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000518 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000519 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000520
drhb5a20d32003-04-23 12:25:23 +0000521 Tcl_DStringInit(&str);
522 Tcl_DStringAppend(&str, pDb->zTrace, -1);
523 Tcl_DStringAppendElement(&str, zSql);
524 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
525 Tcl_DStringFree(&str);
526 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000527}
drhd1167392006-01-23 13:00:35 +0000528#endif
drh0d1a6432003-04-03 15:46:04 +0000529
drhd1167392006-01-23 13:00:35 +0000530#ifndef SQLITE_OMIT_TRACE
drh0d1a6432003-04-03 15:46:04 +0000531/*
drh19e2d372005-08-29 23:00:03 +0000532** This routine is called by the SQLite profile handler after a statement
533** SQL has executed. The TCL script in pDb->zProfile is evaluated.
534*/
535static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){
536 SqliteDb *pDb = (SqliteDb*)cd;
537 Tcl_DString str;
538 char zTm[100];
539
540 sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm);
541 Tcl_DStringInit(&str);
542 Tcl_DStringAppend(&str, pDb->zProfile, -1);
543 Tcl_DStringAppendElement(&str, zSql);
544 Tcl_DStringAppendElement(&str, zTm);
545 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
546 Tcl_DStringFree(&str);
547 Tcl_ResetResult(pDb->interp);
548}
drhd1167392006-01-23 13:00:35 +0000549#endif
drh19e2d372005-08-29 23:00:03 +0000550
551/*
drhaa940ea2004-01-15 02:44:03 +0000552** This routine is called when a transaction is committed. The
553** TCL script in pDb->zCommit is executed. If it returns non-zero or
554** if it throws an exception, the transaction is rolled back instead
555** of being committed.
556*/
557static int DbCommitHandler(void *cd){
558 SqliteDb *pDb = (SqliteDb*)cd;
559 int rc;
560
561 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
562 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
563 return 1;
564 }
565 return 0;
566}
567
danielk197771fd80b2005-12-16 06:54:01 +0000568static void DbRollbackHandler(void *clientData){
569 SqliteDb *pDb = (SqliteDb*)clientData;
570 assert(pDb->pRollbackHook);
571 if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){
572 Tcl_BackgroundError(pDb->interp);
573 }
574}
575
danielk197794eb6a12005-12-15 15:22:08 +0000576static void DbUpdateHandler(
577 void *p,
578 int op,
579 const char *zDb,
580 const char *zTbl,
581 sqlite_int64 rowid
582){
583 SqliteDb *pDb = (SqliteDb *)p;
584 Tcl_Obj *pCmd;
585
586 assert( pDb->pUpdateHook );
587 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
588
589 pCmd = Tcl_DuplicateObj(pDb->pUpdateHook);
590 Tcl_IncrRefCount(pCmd);
591 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(
592 ( (op==SQLITE_INSERT)?"INSERT":(op==SQLITE_UPDATE)?"UPDATE":"DELETE"), -1));
593 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1));
594 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1));
595 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid));
596 Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
597}
598
danielk19777cedc8d2004-06-10 10:50:08 +0000599static void tclCollateNeeded(
600 void *pCtx,
drh9bb575f2004-09-06 17:24:11 +0000601 sqlite3 *db,
danielk19777cedc8d2004-06-10 10:50:08 +0000602 int enc,
603 const char *zName
604){
605 SqliteDb *pDb = (SqliteDb *)pCtx;
606 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
607 Tcl_IncrRefCount(pScript);
608 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
609 Tcl_EvalObjEx(pDb->interp, pScript, 0);
610 Tcl_DecrRefCount(pScript);
611}
612
drhaa940ea2004-01-15 02:44:03 +0000613/*
danielk19770202b292004-06-09 09:55:16 +0000614** This routine is called to evaluate an SQL collation function implemented
615** using TCL script.
616*/
617static int tclSqlCollate(
618 void *pCtx,
619 int nA,
620 const void *zA,
621 int nB,
622 const void *zB
623){
624 SqlCollate *p = (SqlCollate *)pCtx;
625 Tcl_Obj *pCmd;
626
627 pCmd = Tcl_NewStringObj(p->zScript, -1);
628 Tcl_IncrRefCount(pCmd);
629 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA));
630 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB));
drhd1e47332005-06-26 17:55:33 +0000631 Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
danielk19770202b292004-06-09 09:55:16 +0000632 Tcl_DecrRefCount(pCmd);
633 return (atoi(Tcl_GetStringResult(p->interp)));
634}
635
636/*
drhcabb0812002-09-14 13:47:32 +0000637** This routine is called to evaluate an SQL function implemented
638** using TCL script.
639*/
drhfb7e7652005-01-24 00:28:42 +0000640static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){
danielk19776f8a5032004-05-10 10:34:51 +0000641 SqlFunc *p = sqlite3_user_data(context);
drhd1e47332005-06-26 17:55:33 +0000642 Tcl_Obj *pCmd;
drhcabb0812002-09-14 13:47:32 +0000643 int i;
644 int rc;
645
drhd1e47332005-06-26 17:55:33 +0000646 if( argc==0 ){
647 /* If there are no arguments to the function, call Tcl_EvalObjEx on the
648 ** script object directly. This allows the TCL compiler to generate
649 ** bytecode for the command on the first invocation and thus make
650 ** subsequent invocations much faster. */
651 pCmd = p->pScript;
652 Tcl_IncrRefCount(pCmd);
653 rc = Tcl_EvalObjEx(p->interp, pCmd, 0);
654 Tcl_DecrRefCount(pCmd);
655 }else{
656 /* If there are arguments to the function, make a shallow copy of the
657 ** script object, lappend the arguments, then evaluate the copy.
658 **
659 ** By "shallow" copy, we mean a only the outer list Tcl_Obj is duplicated.
660 ** The new Tcl_Obj contains pointers to the original list elements.
661 ** That way, when Tcl_EvalObjv() is run and shimmers the first element
662 ** of the list to tclCmdNameType, that alternate representation will
663 ** be preserved and reused on the next invocation.
664 */
665 Tcl_Obj **aArg;
666 int nArg;
667 if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){
668 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
669 return;
670 }
671 pCmd = Tcl_NewListObj(nArg, aArg);
672 Tcl_IncrRefCount(pCmd);
673 for(i=0; i<argc; i++){
674 sqlite3_value *pIn = argv[i];
675 Tcl_Obj *pVal;
676
677 /* Set pVal to contain the i'th column of this row. */
678 switch( sqlite3_value_type(pIn) ){
679 case SQLITE_BLOB: {
680 int bytes = sqlite3_value_bytes(pIn);
681 pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes);
682 break;
683 }
684 case SQLITE_INTEGER: {
685 sqlite_int64 v = sqlite3_value_int64(pIn);
686 if( v>=-2147483647 && v<=2147483647 ){
687 pVal = Tcl_NewIntObj(v);
688 }else{
689 pVal = Tcl_NewWideIntObj(v);
690 }
691 break;
692 }
693 case SQLITE_FLOAT: {
694 double r = sqlite3_value_double(pIn);
695 pVal = Tcl_NewDoubleObj(r);
696 break;
697 }
698 case SQLITE_NULL: {
699 pVal = Tcl_NewStringObj("", 0);
700 break;
701 }
702 default: {
703 int bytes = sqlite3_value_bytes(pIn);
danielk197700fd9572005-12-07 06:27:43 +0000704 pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes);
drhd1e47332005-06-26 17:55:33 +0000705 break;
706 }
707 }
708 rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal);
709 if( rc ){
710 Tcl_DecrRefCount(pCmd);
711 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
712 return;
713 }
danielk197751ad0ec2004-05-24 12:39:02 +0000714 }
drhd1e47332005-06-26 17:55:33 +0000715 if( !p->useEvalObjv ){
716 /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd
717 ** is a list without a string representation. To prevent this from
718 ** happening, make sure pCmd has a valid string representation */
719 Tcl_GetString(pCmd);
720 }
721 rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
722 Tcl_DecrRefCount(pCmd);
drhcabb0812002-09-14 13:47:32 +0000723 }
danielk1977562e8d32005-05-20 09:40:55 +0000724
drhc7f269d2005-05-05 10:30:29 +0000725 if( rc && rc!=TCL_RETURN ){
danielk19777e18c252004-05-25 11:47:24 +0000726 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhcabb0812002-09-14 13:47:32 +0000727 }else{
drhc7f269d2005-05-05 10:30:29 +0000728 Tcl_Obj *pVar = Tcl_GetObjResult(p->interp);
729 int n;
730 u8 *data;
731 char *zType = pVar->typePtr ? pVar->typePtr->name : "";
732 char c = zType[0];
drhdf0bdda2005-06-25 19:31:48 +0000733 if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){
drhd1e47332005-06-26 17:55:33 +0000734 /* Only return a BLOB type if the Tcl variable is a bytearray and
drhdf0bdda2005-06-25 19:31:48 +0000735 ** has no string representation. */
drhc7f269d2005-05-05 10:30:29 +0000736 data = Tcl_GetByteArrayFromObj(pVar, &n);
737 sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT);
drh985e0c62007-06-26 22:55:37 +0000738 }else if( c=='b' && strcmp(zType,"boolean")==0 ){
drhc7f269d2005-05-05 10:30:29 +0000739 Tcl_GetIntFromObj(0, pVar, &n);
740 sqlite3_result_int(context, n);
741 }else if( c=='d' && strcmp(zType,"double")==0 ){
742 double r;
743 Tcl_GetDoubleFromObj(0, pVar, &r);
744 sqlite3_result_double(context, r);
drh985e0c62007-06-26 22:55:37 +0000745 }else if( (c=='w' && strcmp(zType,"wideInt")==0) ||
746 (c=='i' && strcmp(zType,"int")==0) ){
drhdf0bdda2005-06-25 19:31:48 +0000747 Tcl_WideInt v;
748 Tcl_GetWideIntFromObj(0, pVar, &v);
749 sqlite3_result_int64(context, v);
drhc7f269d2005-05-05 10:30:29 +0000750 }else{
danielk197700fd9572005-12-07 06:27:43 +0000751 data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n);
752 sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT);
drhc7f269d2005-05-05 10:30:29 +0000753 }
drhcabb0812002-09-14 13:47:32 +0000754 }
755}
drh895d7472004-08-20 16:02:39 +0000756
drhe22a3342003-04-22 20:30:37 +0000757#ifndef SQLITE_OMIT_AUTHORIZATION
758/*
759** This is the authentication function. It appends the authentication
760** type code and the two arguments to zCmd[] then invokes the result
761** on the interpreter. The reply is examined to determine if the
762** authentication fails or succeeds.
763*/
764static int auth_callback(
765 void *pArg,
766 int code,
767 const char *zArg1,
768 const char *zArg2,
769 const char *zArg3,
770 const char *zArg4
771){
772 char *zCode;
773 Tcl_DString str;
774 int rc;
775 const char *zReply;
776 SqliteDb *pDb = (SqliteDb*)pArg;
drh1f1549f2008-08-26 21:33:34 +0000777 if( pDb->disableAuth ) return SQLITE_OK;
drhe22a3342003-04-22 20:30:37 +0000778
779 switch( code ){
780 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
781 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
782 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
783 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
784 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
785 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
786 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
787 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
788 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
789 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
790 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
791 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
792 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
793 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
794 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
795 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
796 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
797 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
798 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
799 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
800 case SQLITE_READ : zCode="SQLITE_READ"; break;
801 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
802 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
803 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +0000804 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
805 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
danielk19771c8c23c2004-11-12 15:53:37 +0000806 case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break;
danielk19771d54df82004-11-23 15:41:16 +0000807 case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break;
drhe6e04962005-07-23 02:17:03 +0000808 case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break;
danielk1977f1a381e2006-06-16 08:01:02 +0000809 case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break;
810 case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break;
drh5169bbc2006-08-24 14:59:45 +0000811 case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break;
danielk1977ab9b7032008-12-30 06:24:58 +0000812 case SQLITE_SAVEPOINT : zCode="SQLITE_SAVEPOINT"; break;
drhe22a3342003-04-22 20:30:37 +0000813 default : zCode="????"; break;
814 }
815 Tcl_DStringInit(&str);
816 Tcl_DStringAppend(&str, pDb->zAuth, -1);
817 Tcl_DStringAppendElement(&str, zCode);
818 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
819 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
820 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
821 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
822 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
823 Tcl_DStringFree(&str);
824 zReply = Tcl_GetStringResult(pDb->interp);
825 if( strcmp(zReply,"SQLITE_OK")==0 ){
826 rc = SQLITE_OK;
827 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
828 rc = SQLITE_DENY;
829 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
830 rc = SQLITE_IGNORE;
831 }else{
832 rc = 999;
833 }
834 return rc;
835}
836#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +0000837
838/*
danielk1977ef2cb632004-05-29 02:37:19 +0000839** zText is a pointer to text obtained via an sqlite3_result_text()
840** or similar interface. This routine returns a Tcl string object,
841** reference count set to 0, containing the text. If a translation
842** between iso8859 and UTF-8 is required, it is preformed.
843*/
844static Tcl_Obj *dbTextToObj(char const *zText){
845 Tcl_Obj *pVal;
846#ifdef UTF_TRANSLATION_NEEDED
847 Tcl_DString dCol;
848 Tcl_DStringInit(&dCol);
849 Tcl_ExternalToUtfDString(NULL, zText, -1, &dCol);
850 pVal = Tcl_NewStringObj(Tcl_DStringValue(&dCol), -1);
851 Tcl_DStringFree(&dCol);
852#else
853 pVal = Tcl_NewStringObj(zText, -1);
854#endif
855 return pVal;
856}
857
858/*
tpoindex1067fe12004-12-17 15:41:11 +0000859** This routine reads a line of text from FILE in, stores
860** the text in memory obtained from malloc() and returns a pointer
861** to the text. NULL is returned at end of file, or if malloc()
862** fails.
863**
864** The interface is like "readline" but no command-line editing
865** is done.
866**
867** copied from shell.c from '.import' command
868*/
869static char *local_getline(char *zPrompt, FILE *in){
870 char *zLine;
871 int nLine;
872 int n;
873 int eol;
874
875 nLine = 100;
876 zLine = malloc( nLine );
877 if( zLine==0 ) return 0;
878 n = 0;
879 eol = 0;
880 while( !eol ){
881 if( n+100>nLine ){
882 nLine = nLine*2 + 100;
883 zLine = realloc(zLine, nLine);
884 if( zLine==0 ) return 0;
885 }
886 if( fgets(&zLine[n], nLine - n, in)==0 ){
887 if( n==0 ){
888 free(zLine);
889 return 0;
890 }
891 zLine[n] = 0;
892 eol = 1;
893 break;
894 }
895 while( zLine[n] ){ n++; }
896 if( n>0 && zLine[n-1]=='\n' ){
897 n--;
898 zLine[n] = 0;
899 eol = 1;
900 }
901 }
902 zLine = realloc( zLine, n+1 );
903 return zLine;
904}
905
danielk19778e556522007-11-13 10:30:24 +0000906
907/*
908** Figure out the column names for the data returned by the statement
909** passed as the second argument.
910**
911** If parameter papColName is not NULL, then *papColName is set to point
912** at an array allocated using Tcl_Alloc(). It is the callers responsibility
913** to free this array using Tcl_Free(), and to decrement the reference
914** count of each Tcl_Obj* member of the array.
915**
916** The return value of this function is the number of columns of data
917** returned by pStmt (and hence the size of the *papColName array).
918**
919** If pArray is not NULL, then it contains the name of a Tcl array
920** variable. The "*" member of this array is set to a list containing
921** the names of the columns returned by the statement, in order from
922** left to right. e.g. if the names of the returned columns are a, b and
923** c, it does the equivalent of the tcl command:
924**
925** set ${pArray}(*) {a b c}
926*/
927static int
928computeColumnNames(
929 Tcl_Interp *interp,
930 sqlite3_stmt *pStmt, /* SQL statement */
931 Tcl_Obj ***papColName, /* OUT: Array of column names */
932 Tcl_Obj *pArray /* Name of array variable (may be null) */
933){
934 int nCol;
935
936 /* Compute column names */
937 nCol = sqlite3_column_count(pStmt);
938 if( papColName ){
939 int i;
940 Tcl_Obj **apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol );
941 for(i=0; i<nCol; i++){
942 apColName[i] = dbTextToObj(sqlite3_column_name(pStmt,i));
943 Tcl_IncrRefCount(apColName[i]);
944 }
945
946 /* If results are being stored in an array variable, then create
947 ** the array(*) entry for that array
948 */
949 if( pArray ){
950 Tcl_Obj *pColList = Tcl_NewObj();
951 Tcl_Obj *pStar = Tcl_NewStringObj("*", -1);
952 Tcl_IncrRefCount(pColList);
953 for(i=0; i<nCol; i++){
954 Tcl_ListObjAppendElement(interp, pColList, apColName[i]);
955 }
956 Tcl_IncrRefCount(pStar);
957 Tcl_ObjSetVar2(interp, pArray, pStar, pColList,0);
958 Tcl_DecrRefCount(pColList);
959 Tcl_DecrRefCount(pStar);
960 }
961 *papColName = apColName;
962 }
963
964 return nCol;
965}
966
tpoindex1067fe12004-12-17 15:41:11 +0000967/*
drh75897232000-05-29 14:26:00 +0000968** The "sqlite" command below creates a new Tcl command for each
969** connection it opens to an SQLite database. This routine is invoked
970** whenever one of those connection-specific commands is executed
971** in Tcl. For example, if you run Tcl code like this:
972**
drh9bb575f2004-09-06 17:24:11 +0000973** sqlite3 db1 "my_database"
drh75897232000-05-29 14:26:00 +0000974** db1 close
975**
976** The first command opens a connection to the "my_database" database
977** and calls that connection "db1". The second command causes this
978** subroutine to be invoked.
979*/
drh6d313162000-09-21 13:01:35 +0000980static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +0000981 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +0000982 int choice;
drh22fbcb82004-02-01 01:22:50 +0000983 int rc = TCL_OK;
drh0de8c112002-07-06 16:32:14 +0000984 static const char *DB_strs[] = {
drhfb7e7652005-01-24 00:28:42 +0000985 "authorizer", "busy", "cache",
986 "changes", "close", "collate",
987 "collation_needed", "commit_hook", "complete",
drh41449052006-07-06 17:08:48 +0000988 "copy", "enable_load_extension","errorcode",
989 "eval", "exists", "function",
drh59fffd02007-06-19 17:48:57 +0000990 "incrblob", "interrupt", "last_insert_rowid",
991 "nullvalue", "onecolumn", "profile",
992 "progress", "rekey", "rollback_hook",
drhd1d38482008-10-07 23:46:38 +0000993 "status", "timeout", "total_changes",
994 "trace", "transaction", "update_hook",
995 "version", 0
drh6d313162000-09-21 13:01:35 +0000996 };
drh411995d2002-06-25 19:31:18 +0000997 enum DB_enum {
drhfb7e7652005-01-24 00:28:42 +0000998 DB_AUTHORIZER, DB_BUSY, DB_CACHE,
999 DB_CHANGES, DB_CLOSE, DB_COLLATE,
1000 DB_COLLATION_NEEDED, DB_COMMIT_HOOK, DB_COMPLETE,
drh41449052006-07-06 17:08:48 +00001001 DB_COPY, DB_ENABLE_LOAD_EXTENSION,DB_ERRORCODE,
1002 DB_EVAL, DB_EXISTS, DB_FUNCTION,
drh59fffd02007-06-19 17:48:57 +00001003 DB_INCRBLOB, DB_INTERRUPT, DB_LAST_INSERT_ROWID,
1004 DB_NULLVALUE, DB_ONECOLUMN, DB_PROFILE,
1005 DB_PROGRESS, DB_REKEY, DB_ROLLBACK_HOOK,
drhd1d38482008-10-07 23:46:38 +00001006 DB_STATUS, DB_TIMEOUT, DB_TOTAL_CHANGES,
1007 DB_TRACE, DB_TRANSACTION, DB_UPDATE_HOOK,
1008 DB_VERSION
drh6d313162000-09-21 13:01:35 +00001009 };
tpoindex1067fe12004-12-17 15:41:11 +00001010 /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */
drh6d313162000-09-21 13:01:35 +00001011
1012 if( objc<2 ){
1013 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +00001014 return TCL_ERROR;
1015 }
drh411995d2002-06-25 19:31:18 +00001016 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +00001017 return TCL_ERROR;
1018 }
1019
drh411995d2002-06-25 19:31:18 +00001020 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +00001021
drhe22a3342003-04-22 20:30:37 +00001022 /* $db authorizer ?CALLBACK?
1023 **
1024 ** Invoke the given callback to authorize each SQL operation as it is
1025 ** compiled. 5 arguments are appended to the callback before it is
1026 ** invoked:
1027 **
1028 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
1029 ** (2) First descriptive name (depends on authorization type)
1030 ** (3) Second descriptive name
1031 ** (4) Name of the database (ex: "main", "temp")
1032 ** (5) Name of trigger that is doing the access
1033 **
1034 ** The callback should return on of the following strings: SQLITE_OK,
1035 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
1036 **
1037 ** If this method is invoked with no arguments, the current authorization
1038 ** callback string is returned.
1039 */
1040 case DB_AUTHORIZER: {
drh1211de32004-07-26 12:24:22 +00001041#ifdef SQLITE_OMIT_AUTHORIZATION
1042 Tcl_AppendResult(interp, "authorization not available in this build", 0);
1043 return TCL_ERROR;
1044#else
drhe22a3342003-04-22 20:30:37 +00001045 if( objc>3 ){
1046 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +00001047 return TCL_ERROR;
drhe22a3342003-04-22 20:30:37 +00001048 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +00001049 if( pDb->zAuth ){
drhe22a3342003-04-22 20:30:37 +00001050 Tcl_AppendResult(interp, pDb->zAuth, 0);
1051 }
1052 }else{
1053 char *zAuth;
1054 int len;
1055 if( pDb->zAuth ){
1056 Tcl_Free(pDb->zAuth);
1057 }
1058 zAuth = Tcl_GetStringFromObj(objv[2], &len);
1059 if( zAuth && len>0 ){
1060 pDb->zAuth = Tcl_Alloc( len + 1 );
drh5bb3eb92007-05-04 13:15:55 +00001061 memcpy(pDb->zAuth, zAuth, len+1);
drhe22a3342003-04-22 20:30:37 +00001062 }else{
1063 pDb->zAuth = 0;
1064 }
drhe22a3342003-04-22 20:30:37 +00001065 if( pDb->zAuth ){
1066 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +00001067 sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
drhe22a3342003-04-22 20:30:37 +00001068 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001069 sqlite3_set_authorizer(pDb->db, 0, 0);
drhe22a3342003-04-22 20:30:37 +00001070 }
drhe22a3342003-04-22 20:30:37 +00001071 }
drh1211de32004-07-26 12:24:22 +00001072#endif
drhe22a3342003-04-22 20:30:37 +00001073 break;
1074 }
1075
drhbec3f402000-08-04 13:49:02 +00001076 /* $db busy ?CALLBACK?
1077 **
1078 ** Invoke the given callback if an SQL statement attempts to open
1079 ** a locked database file.
1080 */
drh6d313162000-09-21 13:01:35 +00001081 case DB_BUSY: {
1082 if( objc>3 ){
1083 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +00001084 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +00001085 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +00001086 if( pDb->zBusy ){
1087 Tcl_AppendResult(interp, pDb->zBusy, 0);
1088 }
1089 }else{
drh6d313162000-09-21 13:01:35 +00001090 char *zBusy;
1091 int len;
drhbec3f402000-08-04 13:49:02 +00001092 if( pDb->zBusy ){
1093 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +00001094 }
drh6d313162000-09-21 13:01:35 +00001095 zBusy = Tcl_GetStringFromObj(objv[2], &len);
1096 if( zBusy && len>0 ){
1097 pDb->zBusy = Tcl_Alloc( len + 1 );
drh5bb3eb92007-05-04 13:15:55 +00001098 memcpy(pDb->zBusy, zBusy, len+1);
drh6d313162000-09-21 13:01:35 +00001099 }else{
1100 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +00001101 }
1102 if( pDb->zBusy ){
1103 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +00001104 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +00001105 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001106 sqlite3_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +00001107 }
1108 }
drh6d313162000-09-21 13:01:35 +00001109 break;
1110 }
drhbec3f402000-08-04 13:49:02 +00001111
drhfb7e7652005-01-24 00:28:42 +00001112 /* $db cache flush
1113 ** $db cache size n
1114 **
1115 ** Flush the prepared statement cache, or set the maximum number of
1116 ** cached statements.
1117 */
1118 case DB_CACHE: {
1119 char *subCmd;
1120 int n;
1121
1122 if( objc<=2 ){
1123 Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?");
1124 return TCL_ERROR;
1125 }
1126 subCmd = Tcl_GetStringFromObj( objv[2], 0 );
1127 if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){
1128 if( objc!=3 ){
1129 Tcl_WrongNumArgs(interp, 2, objv, "flush");
1130 return TCL_ERROR;
1131 }else{
1132 flushStmtCache( pDb );
1133 }
1134 }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){
1135 if( objc!=4 ){
1136 Tcl_WrongNumArgs(interp, 2, objv, "size n");
1137 return TCL_ERROR;
1138 }else{
1139 if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){
1140 Tcl_AppendResult( interp, "cannot convert \"",
1141 Tcl_GetStringFromObj(objv[3],0), "\" to integer", 0);
1142 return TCL_ERROR;
1143 }else{
1144 if( n<0 ){
1145 flushStmtCache( pDb );
1146 n = 0;
1147 }else if( n>MAX_PREPARED_STMTS ){
1148 n = MAX_PREPARED_STMTS;
1149 }
1150 pDb->maxStmt = n;
1151 }
1152 }
1153 }else{
1154 Tcl_AppendResult( interp, "bad option \"",
danielk1977191fadc2007-10-23 08:17:48 +00001155 Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size", 0);
drhfb7e7652005-01-24 00:28:42 +00001156 return TCL_ERROR;
1157 }
1158 break;
1159 }
1160
danielk1977b28af712004-06-21 06:50:26 +00001161 /* $db changes
drhc8d30ac2002-04-12 10:08:59 +00001162 **
1163 ** Return the number of rows that were modified, inserted, or deleted by
danielk1977b28af712004-06-21 06:50:26 +00001164 ** the most recent INSERT, UPDATE or DELETE statement, not including
1165 ** any changes made by trigger programs.
drhc8d30ac2002-04-12 10:08:59 +00001166 */
1167 case DB_CHANGES: {
1168 Tcl_Obj *pResult;
drhc8d30ac2002-04-12 10:08:59 +00001169 if( objc!=2 ){
1170 Tcl_WrongNumArgs(interp, 2, objv, "");
1171 return TCL_ERROR;
1172 }
drhc8d30ac2002-04-12 10:08:59 +00001173 pResult = Tcl_GetObjResult(interp);
danielk1977b28af712004-06-21 06:50:26 +00001174 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db));
rdcf146a772004-02-25 22:51:06 +00001175 break;
1176 }
1177
drh75897232000-05-29 14:26:00 +00001178 /* $db close
1179 **
1180 ** Shutdown the database
1181 */
drh6d313162000-09-21 13:01:35 +00001182 case DB_CLOSE: {
1183 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
1184 break;
1185 }
drh75897232000-05-29 14:26:00 +00001186
drh0f14e2e2004-06-29 12:39:08 +00001187 /*
1188 ** $db collate NAME SCRIPT
1189 **
1190 ** Create a new SQL collation function called NAME. Whenever
1191 ** that function is called, invoke SCRIPT to evaluate the function.
1192 */
1193 case DB_COLLATE: {
1194 SqlCollate *pCollate;
1195 char *zName;
1196 char *zScript;
1197 int nScript;
1198 if( objc!=4 ){
1199 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
1200 return TCL_ERROR;
1201 }
1202 zName = Tcl_GetStringFromObj(objv[2], 0);
1203 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
1204 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
1205 if( pCollate==0 ) return TCL_ERROR;
1206 pCollate->interp = interp;
1207 pCollate->pNext = pDb->pCollate;
1208 pCollate->zScript = (char*)&pCollate[1];
1209 pDb->pCollate = pCollate;
drh5bb3eb92007-05-04 13:15:55 +00001210 memcpy(pCollate->zScript, zScript, nScript+1);
drh0f14e2e2004-06-29 12:39:08 +00001211 if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
1212 pCollate, tclSqlCollate) ){
danielk19779636c4e2005-01-25 04:27:54 +00001213 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
drh0f14e2e2004-06-29 12:39:08 +00001214 return TCL_ERROR;
1215 }
1216 break;
1217 }
1218
1219 /*
1220 ** $db collation_needed SCRIPT
1221 **
1222 ** Create a new SQL collation function called NAME. Whenever
1223 ** that function is called, invoke SCRIPT to evaluate the function.
1224 */
1225 case DB_COLLATION_NEEDED: {
1226 if( objc!=3 ){
1227 Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT");
1228 return TCL_ERROR;
1229 }
1230 if( pDb->pCollateNeeded ){
1231 Tcl_DecrRefCount(pDb->pCollateNeeded);
1232 }
1233 pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]);
1234 Tcl_IncrRefCount(pDb->pCollateNeeded);
1235 sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded);
1236 break;
1237 }
1238
drh19e2d372005-08-29 23:00:03 +00001239 /* $db commit_hook ?CALLBACK?
1240 **
1241 ** Invoke the given callback just before committing every SQL transaction.
1242 ** If the callback throws an exception or returns non-zero, then the
1243 ** transaction is aborted. If CALLBACK is an empty string, the callback
1244 ** is disabled.
1245 */
1246 case DB_COMMIT_HOOK: {
1247 if( objc>3 ){
1248 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
1249 return TCL_ERROR;
1250 }else if( objc==2 ){
1251 if( pDb->zCommit ){
1252 Tcl_AppendResult(interp, pDb->zCommit, 0);
1253 }
1254 }else{
1255 char *zCommit;
1256 int len;
1257 if( pDb->zCommit ){
1258 Tcl_Free(pDb->zCommit);
1259 }
1260 zCommit = Tcl_GetStringFromObj(objv[2], &len);
1261 if( zCommit && len>0 ){
1262 pDb->zCommit = Tcl_Alloc( len + 1 );
drh5bb3eb92007-05-04 13:15:55 +00001263 memcpy(pDb->zCommit, zCommit, len+1);
drh19e2d372005-08-29 23:00:03 +00001264 }else{
1265 pDb->zCommit = 0;
1266 }
1267 if( pDb->zCommit ){
1268 pDb->interp = interp;
1269 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
1270 }else{
1271 sqlite3_commit_hook(pDb->db, 0, 0);
1272 }
1273 }
1274 break;
1275 }
1276
drh75897232000-05-29 14:26:00 +00001277 /* $db complete SQL
1278 **
1279 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
1280 ** additional lines of input are needed. This is similar to the
1281 ** built-in "info complete" command of Tcl.
1282 */
drh6d313162000-09-21 13:01:35 +00001283 case DB_COMPLETE: {
drhccae6022005-02-26 17:31:26 +00001284#ifndef SQLITE_OMIT_COMPLETE
drh6d313162000-09-21 13:01:35 +00001285 Tcl_Obj *pResult;
1286 int isComplete;
1287 if( objc!=3 ){
1288 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +00001289 return TCL_ERROR;
1290 }
danielk19776f8a5032004-05-10 10:34:51 +00001291 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
drh6d313162000-09-21 13:01:35 +00001292 pResult = Tcl_GetObjResult(interp);
1293 Tcl_SetBooleanObj(pResult, isComplete);
drhccae6022005-02-26 17:31:26 +00001294#endif
drh6d313162000-09-21 13:01:35 +00001295 break;
1296 }
drhdcd997e2003-01-31 17:21:49 +00001297
drh19e2d372005-08-29 23:00:03 +00001298 /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR?
1299 **
1300 ** Copy data into table from filename, optionally using SEPARATOR
1301 ** as column separators. If a column contains a null string, or the
1302 ** value of NULLINDICATOR, a NULL is inserted for the column.
1303 ** conflict-algorithm is one of the sqlite conflict algorithms:
1304 ** rollback, abort, fail, ignore, replace
1305 ** On success, return the number of lines processed, not necessarily same
1306 ** as 'db changes' due to conflict-algorithm selected.
1307 **
1308 ** This code is basically an implementation/enhancement of
1309 ** the sqlite3 shell.c ".import" command.
1310 **
1311 ** This command usage is equivalent to the sqlite2.x COPY statement,
1312 ** which imports file data into a table using the PostgreSQL COPY file format:
1313 ** $db copy $conflit_algo $table_name $filename \t \\N
1314 */
1315 case DB_COPY: {
1316 char *zTable; /* Insert data into this table */
1317 char *zFile; /* The file from which to extract data */
1318 char *zConflict; /* The conflict algorithm to use */
1319 sqlite3_stmt *pStmt; /* A statement */
drh19e2d372005-08-29 23:00:03 +00001320 int nCol; /* Number of columns in the table */
1321 int nByte; /* Number of bytes in an SQL string */
1322 int i, j; /* Loop counters */
1323 int nSep; /* Number of bytes in zSep[] */
1324 int nNull; /* Number of bytes in zNull[] */
1325 char *zSql; /* An SQL statement */
1326 char *zLine; /* A single line of input from the file */
1327 char **azCol; /* zLine[] broken up into columns */
1328 char *zCommit; /* How to commit changes */
1329 FILE *in; /* The input file */
1330 int lineno = 0; /* Line number of input file */
1331 char zLineNum[80]; /* Line number print buffer */
1332 Tcl_Obj *pResult; /* interp result */
1333
1334 char *zSep;
1335 char *zNull;
1336 if( objc<5 || objc>7 ){
1337 Tcl_WrongNumArgs(interp, 2, objv,
1338 "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?");
1339 return TCL_ERROR;
1340 }
1341 if( objc>=6 ){
1342 zSep = Tcl_GetStringFromObj(objv[5], 0);
1343 }else{
1344 zSep = "\t";
1345 }
1346 if( objc>=7 ){
1347 zNull = Tcl_GetStringFromObj(objv[6], 0);
1348 }else{
1349 zNull = "";
1350 }
1351 zConflict = Tcl_GetStringFromObj(objv[2], 0);
1352 zTable = Tcl_GetStringFromObj(objv[3], 0);
1353 zFile = Tcl_GetStringFromObj(objv[4], 0);
drh4f21c4a2008-12-10 22:15:00 +00001354 nSep = strlen30(zSep);
1355 nNull = strlen30(zNull);
drh19e2d372005-08-29 23:00:03 +00001356 if( nSep==0 ){
drh1409be62006-08-23 20:07:20 +00001357 Tcl_AppendResult(interp,"Error: non-null separator required for copy",0);
drh19e2d372005-08-29 23:00:03 +00001358 return TCL_ERROR;
1359 }
drh3e59c012008-09-23 10:12:13 +00001360 if(strcmp(zConflict, "rollback") != 0 &&
1361 strcmp(zConflict, "abort" ) != 0 &&
1362 strcmp(zConflict, "fail" ) != 0 &&
1363 strcmp(zConflict, "ignore" ) != 0 &&
1364 strcmp(zConflict, "replace" ) != 0 ) {
drh19e2d372005-08-29 23:00:03 +00001365 Tcl_AppendResult(interp, "Error: \"", zConflict,
1366 "\", conflict-algorithm must be one of: rollback, "
1367 "abort, fail, ignore, or replace", 0);
1368 return TCL_ERROR;
1369 }
1370 zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
1371 if( zSql==0 ){
1372 Tcl_AppendResult(interp, "Error: no such table: ", zTable, 0);
1373 return TCL_ERROR;
1374 }
drh4f21c4a2008-12-10 22:15:00 +00001375 nByte = strlen30(zSql);
drh3e701a12007-02-01 01:53:44 +00001376 rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0);
drh19e2d372005-08-29 23:00:03 +00001377 sqlite3_free(zSql);
1378 if( rc ){
1379 Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0);
1380 nCol = 0;
1381 }else{
1382 nCol = sqlite3_column_count(pStmt);
1383 }
1384 sqlite3_finalize(pStmt);
1385 if( nCol==0 ) {
1386 return TCL_ERROR;
1387 }
1388 zSql = malloc( nByte + 50 + nCol*2 );
1389 if( zSql==0 ) {
1390 Tcl_AppendResult(interp, "Error: can't malloc()", 0);
1391 return TCL_ERROR;
1392 }
1393 sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?",
1394 zConflict, zTable);
drh4f21c4a2008-12-10 22:15:00 +00001395 j = strlen30(zSql);
drh19e2d372005-08-29 23:00:03 +00001396 for(i=1; i<nCol; i++){
1397 zSql[j++] = ',';
1398 zSql[j++] = '?';
1399 }
1400 zSql[j++] = ')';
1401 zSql[j] = 0;
drh3e701a12007-02-01 01:53:44 +00001402 rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0);
drh19e2d372005-08-29 23:00:03 +00001403 free(zSql);
1404 if( rc ){
1405 Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0);
1406 sqlite3_finalize(pStmt);
1407 return TCL_ERROR;
1408 }
1409 in = fopen(zFile, "rb");
1410 if( in==0 ){
1411 Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL);
1412 sqlite3_finalize(pStmt);
1413 return TCL_ERROR;
1414 }
1415 azCol = malloc( sizeof(azCol[0])*(nCol+1) );
1416 if( azCol==0 ) {
1417 Tcl_AppendResult(interp, "Error: can't malloc()", 0);
drh43617e92006-03-06 20:55:46 +00001418 fclose(in);
drh19e2d372005-08-29 23:00:03 +00001419 return TCL_ERROR;
1420 }
drh37527852006-03-16 16:19:56 +00001421 (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0);
drh19e2d372005-08-29 23:00:03 +00001422 zCommit = "COMMIT";
1423 while( (zLine = local_getline(0, in))!=0 ){
1424 char *z;
1425 i = 0;
1426 lineno++;
1427 azCol[0] = zLine;
1428 for(i=0, z=zLine; *z; z++){
1429 if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){
1430 *z = 0;
1431 i++;
1432 if( i<nCol ){
1433 azCol[i] = &z[nSep];
1434 z += nSep-1;
1435 }
1436 }
1437 }
1438 if( i+1!=nCol ){
1439 char *zErr;
drh4f21c4a2008-12-10 22:15:00 +00001440 int nErr = strlen30(zFile) + 200;
drh5bb3eb92007-05-04 13:15:55 +00001441 zErr = malloc(nErr);
drhc1f44942006-05-10 14:39:13 +00001442 if( zErr ){
drh5bb3eb92007-05-04 13:15:55 +00001443 sqlite3_snprintf(nErr, zErr,
drhc1f44942006-05-10 14:39:13 +00001444 "Error: %s line %d: expected %d columns of data but found %d",
1445 zFile, lineno, nCol, i+1);
1446 Tcl_AppendResult(interp, zErr, 0);
1447 free(zErr);
1448 }
drh19e2d372005-08-29 23:00:03 +00001449 zCommit = "ROLLBACK";
1450 break;
1451 }
1452 for(i=0; i<nCol; i++){
1453 /* check for null data, if so, bind as null */
drhea678832008-12-10 19:26:22 +00001454 if( (nNull>0 && strcmp(azCol[i], zNull)==0)
drh4f21c4a2008-12-10 22:15:00 +00001455 || strlen30(azCol[i])==0
drhea678832008-12-10 19:26:22 +00001456 ){
drh19e2d372005-08-29 23:00:03 +00001457 sqlite3_bind_null(pStmt, i+1);
1458 }else{
1459 sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
1460 }
1461 }
1462 sqlite3_step(pStmt);
1463 rc = sqlite3_reset(pStmt);
1464 free(zLine);
1465 if( rc!=SQLITE_OK ){
1466 Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), 0);
1467 zCommit = "ROLLBACK";
1468 break;
1469 }
1470 }
1471 free(azCol);
1472 fclose(in);
1473 sqlite3_finalize(pStmt);
drh37527852006-03-16 16:19:56 +00001474 (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0);
drh19e2d372005-08-29 23:00:03 +00001475
1476 if( zCommit[0] == 'C' ){
1477 /* success, set result as number of lines processed */
1478 pResult = Tcl_GetObjResult(interp);
1479 Tcl_SetIntObj(pResult, lineno);
1480 rc = TCL_OK;
1481 }else{
1482 /* failure, append lineno where failed */
drh5bb3eb92007-05-04 13:15:55 +00001483 sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno);
drh19e2d372005-08-29 23:00:03 +00001484 Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,0);
1485 rc = TCL_ERROR;
1486 }
1487 break;
1488 }
1489
drhdcd997e2003-01-31 17:21:49 +00001490 /*
drh41449052006-07-06 17:08:48 +00001491 ** $db enable_load_extension BOOLEAN
1492 **
1493 ** Turn the extension loading feature on or off. It if off by
1494 ** default.
1495 */
1496 case DB_ENABLE_LOAD_EXTENSION: {
drhf533acc2006-12-19 18:57:11 +00001497#ifndef SQLITE_OMIT_LOAD_EXTENSION
drh41449052006-07-06 17:08:48 +00001498 int onoff;
1499 if( objc!=3 ){
1500 Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN");
1501 return TCL_ERROR;
1502 }
1503 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
1504 return TCL_ERROR;
1505 }
1506 sqlite3_enable_load_extension(pDb->db, onoff);
1507 break;
drhf533acc2006-12-19 18:57:11 +00001508#else
1509 Tcl_AppendResult(interp, "extension loading is turned off at compile-time",
1510 0);
1511 return TCL_ERROR;
1512#endif
drh41449052006-07-06 17:08:48 +00001513 }
1514
1515 /*
drhdcd997e2003-01-31 17:21:49 +00001516 ** $db errorcode
1517 **
1518 ** Return the numeric error code that was returned by the most recent
danielk19776f8a5032004-05-10 10:34:51 +00001519 ** call to sqlite3_exec().
drhdcd997e2003-01-31 17:21:49 +00001520 */
1521 case DB_ERRORCODE: {
danielk1977f3ce83f2004-06-14 11:43:46 +00001522 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
drhdcd997e2003-01-31 17:21:49 +00001523 break;
1524 }
drh75897232000-05-29 14:26:00 +00001525
1526 /*
drh895d7472004-08-20 16:02:39 +00001527 ** $db eval $sql ?array? ?{ ...code... }?
drh1807ce32004-09-07 13:20:35 +00001528 ** $db onecolumn $sql
drh75897232000-05-29 14:26:00 +00001529 **
1530 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +00001531 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +00001532 ** If "array" and "code" are omitted, then no callback is every invoked.
1533 ** If "array" is an empty string, then the values are placed in variables
1534 ** that have the same name as the fields extracted by the query.
drh1807ce32004-09-07 13:20:35 +00001535 **
1536 ** The onecolumn method is the equivalent of:
1537 ** lindex [$db eval $sql] 0
drh75897232000-05-29 14:26:00 +00001538 */
drh1807ce32004-09-07 13:20:35 +00001539 case DB_ONECOLUMN:
drh97f2ebc2005-12-10 21:19:04 +00001540 case DB_EVAL:
1541 case DB_EXISTS: {
drh9d74b4c2004-08-24 15:23:34 +00001542 char const *zSql; /* Next SQL statement to execute */
1543 char const *zLeft; /* What is left after first stmt in zSql */
1544 sqlite3_stmt *pStmt; /* Compiled SQL statment */
drh92febd92004-08-20 18:34:20 +00001545 Tcl_Obj *pArray; /* Name of array into which results are written */
1546 Tcl_Obj *pScript; /* Script to run for each result set */
drh1d895032004-08-26 00:56:05 +00001547 Tcl_Obj **apParm; /* Parameters that need a Tcl_DecrRefCount() */
1548 int nParm; /* Number of entries used in apParm[] */
1549 Tcl_Obj *aParm[10]; /* Static space for apParm[] in the common case */
drh1807ce32004-09-07 13:20:35 +00001550 Tcl_Obj *pRet; /* Value to be returned */
drhfb7e7652005-01-24 00:28:42 +00001551 SqlPreparedStmt *pPreStmt; /* Pointer to a prepared statement */
1552 int rc2;
danielk1977ef2cb632004-05-29 02:37:19 +00001553
drh97f2ebc2005-12-10 21:19:04 +00001554 if( choice==DB_EVAL ){
drh1807ce32004-09-07 13:20:35 +00001555 if( objc<3 || objc>5 ){
1556 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?");
1557 return TCL_ERROR;
1558 }
1559 pRet = Tcl_NewObj();
1560 Tcl_IncrRefCount(pRet);
drh97f2ebc2005-12-10 21:19:04 +00001561 }else{
1562 if( objc!=3 ){
1563 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
1564 return TCL_ERROR;
1565 }
drh97f2ebc2005-12-10 21:19:04 +00001566 if( choice==DB_EXISTS ){
drh446a9b82006-01-04 18:13:26 +00001567 pRet = Tcl_NewBooleanObj(0);
1568 Tcl_IncrRefCount(pRet);
1569 }else{
1570 pRet = 0;
drh97f2ebc2005-12-10 21:19:04 +00001571 }
danielk197730ccda12004-05-27 12:11:31 +00001572 }
drh92febd92004-08-20 18:34:20 +00001573 if( objc==3 ){
1574 pArray = pScript = 0;
1575 }else if( objc==4 ){
1576 pArray = 0;
1577 pScript = objv[3];
1578 }else{
1579 pArray = objv[3];
1580 if( Tcl_GetString(pArray)[0]==0 ) pArray = 0;
1581 pScript = objv[4];
1582 }
danielk197730ccda12004-05-27 12:11:31 +00001583
drh1d895032004-08-26 00:56:05 +00001584 Tcl_IncrRefCount(objv[2]);
danielk197730ccda12004-05-27 12:11:31 +00001585 zSql = Tcl_GetStringFromObj(objv[2], 0);
drh90b6bb12004-09-13 13:16:31 +00001586 while( rc==TCL_OK && zSql[0] ){
drhfb7e7652005-01-24 00:28:42 +00001587 int i; /* Loop counter */
1588 int nVar; /* Number of bind parameters in the pStmt */
danielk19778e556522007-11-13 10:30:24 +00001589 int nCol = -1; /* Number of columns in the result set */
drh92febd92004-08-20 18:34:20 +00001590 Tcl_Obj **apColName = 0; /* Array of column names */
drhfb7e7652005-01-24 00:28:42 +00001591 int len; /* String length of zSql */
danielk197730ccda12004-05-27 12:11:31 +00001592
drhfb7e7652005-01-24 00:28:42 +00001593 /* Try to find a SQL statement that has already been compiled and
1594 ** which matches the next sequence of SQL.
1595 */
1596 pStmt = 0;
drh4f21c4a2008-12-10 22:15:00 +00001597 len = strlen30(zSql);
danielk19778e556522007-11-13 10:30:24 +00001598 for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){
drhfb7e7652005-01-24 00:28:42 +00001599 int n = pPreStmt->nSql;
1600 if( len>=n
1601 && memcmp(pPreStmt->zSql, zSql, n)==0
1602 && (zSql[n]==0 || zSql[n-1]==';')
1603 ){
1604 pStmt = pPreStmt->pStmt;
1605 zLeft = &zSql[pPreStmt->nSql];
1606
1607 /* When a prepared statement is found, unlink it from the
1608 ** cache list. It will later be added back to the beginning
1609 ** of the cache list in order to implement LRU replacement.
1610 */
1611 if( pPreStmt->pPrev ){
1612 pPreStmt->pPrev->pNext = pPreStmt->pNext;
1613 }else{
1614 pDb->stmtList = pPreStmt->pNext;
1615 }
1616 if( pPreStmt->pNext ){
1617 pPreStmt->pNext->pPrev = pPreStmt->pPrev;
1618 }else{
1619 pDb->stmtLast = pPreStmt->pPrev;
1620 }
1621 pDb->nStmt--;
1622 break;
1623 }
1624 }
1625
1626 /* If no prepared statement was found. Compile the SQL text
1627 */
drh92febd92004-08-20 18:34:20 +00001628 if( pStmt==0 ){
danielk19778e556522007-11-13 10:30:24 +00001629 if( SQLITE_OK!=sqlite3_prepare_v2(pDb->db, zSql, -1, &pStmt, &zLeft) ){
drh92febd92004-08-20 18:34:20 +00001630 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
1631 rc = TCL_ERROR;
1632 break;
danielk197730ccda12004-05-27 12:11:31 +00001633 }
drhfb7e7652005-01-24 00:28:42 +00001634 if( pStmt==0 ){
1635 if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
1636 /* A compile-time error in the statement
1637 */
1638 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
1639 rc = TCL_ERROR;
1640 break;
1641 }else{
1642 /* The statement was a no-op. Continue to the next statement
1643 ** in the SQL string.
1644 */
1645 zSql = zLeft;
1646 continue;
1647 }
1648 }
1649 assert( pPreStmt==0 );
danielk197730ccda12004-05-27 12:11:31 +00001650 }
1651
drhfb7e7652005-01-24 00:28:42 +00001652 /* Bind values to parameters that begin with $ or :
1653 */
drh92febd92004-08-20 18:34:20 +00001654 nVar = sqlite3_bind_parameter_count(pStmt);
drh1d895032004-08-26 00:56:05 +00001655 nParm = 0;
1656 if( nVar>sizeof(aParm)/sizeof(aParm[0]) ){
1657 apParm = (Tcl_Obj**)Tcl_Alloc(nVar*sizeof(apParm[0]));
1658 }else{
1659 apParm = aParm;
1660 }
drh92febd92004-08-20 18:34:20 +00001661 for(i=1; i<=nVar; i++){
1662 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
drh4f5e80f2007-06-19 17:15:46 +00001663 if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){
drh92febd92004-08-20 18:34:20 +00001664 Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0);
1665 if( pVar ){
1666 int n;
1667 u8 *data;
1668 char *zType = pVar->typePtr ? pVar->typePtr->name : "";
1669 char c = zType[0];
drh1c747812007-06-19 23:01:41 +00001670 if( zVar[0]=='@' ||
1671 (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){
1672 /* Load a BLOB type if the Tcl variable is a bytearray and
1673 ** it has no string representation or the host
drh4f5e80f2007-06-19 17:15:46 +00001674 ** parameter name begins with "@". */
drh92febd92004-08-20 18:34:20 +00001675 data = Tcl_GetByteArrayFromObj(pVar, &n);
1676 sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC);
drh1d895032004-08-26 00:56:05 +00001677 Tcl_IncrRefCount(pVar);
1678 apParm[nParm++] = pVar;
drh985e0c62007-06-26 22:55:37 +00001679 }else if( c=='b' && strcmp(zType,"boolean")==0 ){
drh92febd92004-08-20 18:34:20 +00001680 Tcl_GetIntFromObj(interp, pVar, &n);
1681 sqlite3_bind_int(pStmt, i, n);
1682 }else if( c=='d' && strcmp(zType,"double")==0 ){
1683 double r;
1684 Tcl_GetDoubleFromObj(interp, pVar, &r);
1685 sqlite3_bind_double(pStmt, i, r);
drh985e0c62007-06-26 22:55:37 +00001686 }else if( (c=='w' && strcmp(zType,"wideInt")==0) ||
1687 (c=='i' && strcmp(zType,"int")==0) ){
drhdf0bdda2005-06-25 19:31:48 +00001688 Tcl_WideInt v;
1689 Tcl_GetWideIntFromObj(interp, pVar, &v);
1690 sqlite3_bind_int64(pStmt, i, v);
drh92febd92004-08-20 18:34:20 +00001691 }else{
danielk197700fd9572005-12-07 06:27:43 +00001692 data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n);
drhb08c2a72008-04-16 00:28:13 +00001693 sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC);
drh1d895032004-08-26 00:56:05 +00001694 Tcl_IncrRefCount(pVar);
1695 apParm[nParm++] = pVar;
drh92febd92004-08-20 18:34:20 +00001696 }
drhfb7e7652005-01-24 00:28:42 +00001697 }else{
1698 sqlite3_bind_null( pStmt, i );
drh92febd92004-08-20 18:34:20 +00001699 }
1700 }
1701 }
drh92febd92004-08-20 18:34:20 +00001702
drh92febd92004-08-20 18:34:20 +00001703 /* Execute the SQL
1704 */
drh90b6bb12004-09-13 13:16:31 +00001705 while( rc==TCL_OK && pStmt && SQLITE_ROW==sqlite3_step(pStmt) ){
danielk19778e556522007-11-13 10:30:24 +00001706
1707 /* Compute column names. This must be done after the first successful
1708 ** call to sqlite3_step(), in case the query is recompiled and the
1709 ** number or names of the returned columns changes.
1710 */
1711 assert(!pArray||pScript);
1712 if (nCol < 0) {
1713 Tcl_Obj ***ap = (pScript?&apColName:0);
1714 nCol = computeColumnNames(interp, pStmt, ap, pArray);
1715 }
1716
drh92febd92004-08-20 18:34:20 +00001717 for(i=0; i<nCol; i++){
danielk197730ccda12004-05-27 12:11:31 +00001718 Tcl_Obj *pVal;
1719
1720 /* Set pVal to contain the i'th column of this row. */
drh92febd92004-08-20 18:34:20 +00001721 switch( sqlite3_column_type(pStmt, i) ){
1722 case SQLITE_BLOB: {
1723 int bytes = sqlite3_column_bytes(pStmt, i);
drheee4c8c2008-02-18 22:24:57 +00001724 const char *zBlob = sqlite3_column_blob(pStmt, i);
danielk1977a7a8e142008-02-13 18:25:27 +00001725 if( !zBlob ) bytes = 0;
drheee4c8c2008-02-18 22:24:57 +00001726 pVal = Tcl_NewByteArrayObj((u8*)zBlob, bytes);
drh92febd92004-08-20 18:34:20 +00001727 break;
1728 }
1729 case SQLITE_INTEGER: {
1730 sqlite_int64 v = sqlite3_column_int64(pStmt, i);
1731 if( v>=-2147483647 && v<=2147483647 ){
1732 pVal = Tcl_NewIntObj(v);
1733 }else{
1734 pVal = Tcl_NewWideIntObj(v);
1735 }
1736 break;
1737 }
1738 case SQLITE_FLOAT: {
1739 double r = sqlite3_column_double(pStmt, i);
1740 pVal = Tcl_NewDoubleObj(r);
1741 break;
1742 }
danielk197755c45f22005-04-03 23:54:43 +00001743 case SQLITE_NULL: {
1744 pVal = dbTextToObj(pDb->zNull);
1745 break;
1746 }
drh92febd92004-08-20 18:34:20 +00001747 default: {
danielk197700fd9572005-12-07 06:27:43 +00001748 pVal = dbTextToObj((char *)sqlite3_column_text(pStmt, i));
drh92febd92004-08-20 18:34:20 +00001749 break;
1750 }
danielk197730ccda12004-05-27 12:11:31 +00001751 }
1752
drh92febd92004-08-20 18:34:20 +00001753 if( pScript ){
1754 if( pArray==0 ){
1755 Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0);
danielk197730ccda12004-05-27 12:11:31 +00001756 }else{
drh92febd92004-08-20 18:34:20 +00001757 Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0);
danielk197730ccda12004-05-27 12:11:31 +00001758 }
drh1807ce32004-09-07 13:20:35 +00001759 }else if( choice==DB_ONECOLUMN ){
drh97f2ebc2005-12-10 21:19:04 +00001760 assert( pRet==0 );
drh1807ce32004-09-07 13:20:35 +00001761 if( pRet==0 ){
1762 pRet = pVal;
1763 Tcl_IncrRefCount(pRet);
1764 }
drh90b6bb12004-09-13 13:16:31 +00001765 rc = TCL_BREAK;
drh97f2ebc2005-12-10 21:19:04 +00001766 i = nCol;
1767 }else if( choice==DB_EXISTS ){
drh446a9b82006-01-04 18:13:26 +00001768 Tcl_DecrRefCount(pRet);
1769 pRet = Tcl_NewBooleanObj(1);
1770 Tcl_IncrRefCount(pRet);
drh97f2ebc2005-12-10 21:19:04 +00001771 rc = TCL_BREAK;
1772 i = nCol;
danielk197730ccda12004-05-27 12:11:31 +00001773 }else{
danielk197730ccda12004-05-27 12:11:31 +00001774 Tcl_ListObjAppendElement(interp, pRet, pVal);
1775 }
1776 }
1777
drh92febd92004-08-20 18:34:20 +00001778 if( pScript ){
drhd1d38482008-10-07 23:46:38 +00001779 pDb->nStep = sqlite3_stmt_status(pStmt,
1780 SQLITE_STMTSTATUS_FULLSCAN_STEP, 0);
1781 pDb->nSort = sqlite3_stmt_status(pStmt,
1782 SQLITE_STMTSTATUS_SORT, 0);
drh92febd92004-08-20 18:34:20 +00001783 rc = Tcl_EvalObjEx(interp, pScript, 0);
drh90b6bb12004-09-13 13:16:31 +00001784 if( rc==TCL_CONTINUE ){
1785 rc = TCL_OK;
1786 }
danielk197730ccda12004-05-27 12:11:31 +00001787 }
1788 }
drh90b6bb12004-09-13 13:16:31 +00001789 if( rc==TCL_BREAK ){
1790 rc = TCL_OK;
1791 }
drh92febd92004-08-20 18:34:20 +00001792
1793 /* Free the column name objects */
1794 if( pScript ){
danielk19778e556522007-11-13 10:30:24 +00001795 /* If the query returned no rows, but an array variable was
1796 ** specified, call computeColumnNames() now to populate the
1797 ** arrayname(*) variable.
1798 */
1799 if (pArray && nCol < 0) {
1800 Tcl_Obj ***ap = (pScript?&apColName:0);
1801 nCol = computeColumnNames(interp, pStmt, ap, pArray);
1802 }
drh92febd92004-08-20 18:34:20 +00001803 for(i=0; i<nCol; i++){
1804 Tcl_DecrRefCount(apColName[i]);
1805 }
1806 Tcl_Free((char*)apColName);
1807 }
1808
drh1d895032004-08-26 00:56:05 +00001809 /* Free the bound string and blob parameters */
1810 for(i=0; i<nParm; i++){
1811 Tcl_DecrRefCount(apParm[i]);
1812 }
1813 if( apParm!=aParm ){
1814 Tcl_Free((char*)apParm);
1815 }
1816
drhfb7e7652005-01-24 00:28:42 +00001817 /* Reset the statement. If the result code is SQLITE_SCHEMA, then
1818 ** flush the statement cache and try the statement again.
drh92febd92004-08-20 18:34:20 +00001819 */
drhfb7e7652005-01-24 00:28:42 +00001820 rc2 = sqlite3_reset(pStmt);
drhd1d38482008-10-07 23:46:38 +00001821 pDb->nStep = sqlite3_stmt_status(pStmt,
1822 SQLITE_STMTSTATUS_FULLSCAN_STEP, 1);
1823 pDb->nSort = sqlite3_stmt_status(pStmt,
1824 SQLITE_STMTSTATUS_SORT, 1);
danielk19778e556522007-11-13 10:30:24 +00001825 if( SQLITE_OK!=rc2 ){
drhfb7e7652005-01-24 00:28:42 +00001826 /* If a run-time error occurs, report the error and stop reading
1827 ** the SQL
1828 */
danielk1977ef2cb632004-05-29 02:37:19 +00001829 Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
drhfb7e7652005-01-24 00:28:42 +00001830 sqlite3_finalize(pStmt);
danielk197730ccda12004-05-27 12:11:31 +00001831 rc = TCL_ERROR;
drhfb7e7652005-01-24 00:28:42 +00001832 if( pPreStmt ) Tcl_Free((char*)pPreStmt);
danielk197730ccda12004-05-27 12:11:31 +00001833 break;
drhfb7e7652005-01-24 00:28:42 +00001834 }else if( pDb->maxStmt<=0 ){
1835 /* If the cache is turned off, deallocated the statement */
1836 if( pPreStmt ) Tcl_Free((char*)pPreStmt);
1837 sqlite3_finalize(pStmt);
1838 }else{
1839 /* Everything worked and the cache is operational.
1840 ** Create a new SqlPreparedStmt structure if we need one.
1841 ** (If we already have one we can just reuse it.)
1842 */
1843 if( pPreStmt==0 ){
1844 len = zLeft - zSql;
danielk1977d0e2a852007-11-14 06:48:48 +00001845 pPreStmt = (SqlPreparedStmt*)Tcl_Alloc( sizeof(*pPreStmt) );
drhfb7e7652005-01-24 00:28:42 +00001846 if( pPreStmt==0 ) return TCL_ERROR;
1847 pPreStmt->pStmt = pStmt;
1848 pPreStmt->nSql = len;
danielk1977d0e2a852007-11-14 06:48:48 +00001849 pPreStmt->zSql = sqlite3_sql(pStmt);
drh4f21c4a2008-12-10 22:15:00 +00001850 assert( strlen30(pPreStmt->zSql)==len );
danielk1977d0e2a852007-11-14 06:48:48 +00001851 assert( 0==memcmp(pPreStmt->zSql, zSql, len) );
drhfb7e7652005-01-24 00:28:42 +00001852 }
1853
1854 /* Add the prepared statement to the beginning of the cache list
1855 */
1856 pPreStmt->pNext = pDb->stmtList;
1857 pPreStmt->pPrev = 0;
1858 if( pDb->stmtList ){
1859 pDb->stmtList->pPrev = pPreStmt;
1860 }
1861 pDb->stmtList = pPreStmt;
1862 if( pDb->stmtLast==0 ){
1863 assert( pDb->nStmt==0 );
1864 pDb->stmtLast = pPreStmt;
1865 }else{
1866 assert( pDb->nStmt>0 );
1867 }
1868 pDb->nStmt++;
1869
1870 /* If we have too many statement in cache, remove the surplus from the
1871 ** end of the cache list.
1872 */
1873 while( pDb->nStmt>pDb->maxStmt ){
1874 sqlite3_finalize(pDb->stmtLast->pStmt);
1875 pDb->stmtLast = pDb->stmtLast->pPrev;
1876 Tcl_Free((char*)pDb->stmtLast->pNext);
1877 pDb->stmtLast->pNext = 0;
1878 pDb->nStmt--;
1879 }
danielk197730ccda12004-05-27 12:11:31 +00001880 }
1881
drhfb7e7652005-01-24 00:28:42 +00001882 /* Proceed to the next statement */
danielk197730ccda12004-05-27 12:11:31 +00001883 zSql = zLeft;
1884 }
drh1d895032004-08-26 00:56:05 +00001885 Tcl_DecrRefCount(objv[2]);
danielk197730ccda12004-05-27 12:11:31 +00001886
drh1807ce32004-09-07 13:20:35 +00001887 if( pRet ){
1888 if( rc==TCL_OK ){
1889 Tcl_SetObjResult(interp, pRet);
1890 }
1891 Tcl_DecrRefCount(pRet);
drh050be322006-07-12 00:18:40 +00001892 }else if( rc==TCL_OK ){
1893 Tcl_ResetResult(interp);
danielk197730ccda12004-05-27 12:11:31 +00001894 }
danielk197730ccda12004-05-27 12:11:31 +00001895 break;
1896 }
drhbec3f402000-08-04 13:49:02 +00001897
1898 /*
drhe3602be2008-09-09 12:31:33 +00001899 ** $db function NAME [-argcount N] SCRIPT
drhcabb0812002-09-14 13:47:32 +00001900 **
1901 ** Create a new SQL function called NAME. Whenever that function is
1902 ** called, invoke SCRIPT to evaluate the function.
1903 */
1904 case DB_FUNCTION: {
1905 SqlFunc *pFunc;
drhd1e47332005-06-26 17:55:33 +00001906 Tcl_Obj *pScript;
drhcabb0812002-09-14 13:47:32 +00001907 char *zName;
drhe3602be2008-09-09 12:31:33 +00001908 int nArg = -1;
1909 if( objc==6 ){
1910 const char *z = Tcl_GetString(objv[3]);
drh4f21c4a2008-12-10 22:15:00 +00001911 int n = strlen30(z);
drhe3602be2008-09-09 12:31:33 +00001912 if( n>2 && strncmp(z, "-argcount",n)==0 ){
1913 if( Tcl_GetIntFromObj(interp, objv[4], &nArg) ) return TCL_ERROR;
1914 if( nArg<0 ){
1915 Tcl_AppendResult(interp, "number of arguments must be non-negative",
1916 (char*)0);
1917 return TCL_ERROR;
1918 }
1919 }
1920 pScript = objv[5];
1921 }else if( objc!=4 ){
1922 Tcl_WrongNumArgs(interp, 2, objv, "NAME [-argcount N] SCRIPT");
drhcabb0812002-09-14 13:47:32 +00001923 return TCL_ERROR;
drhe3602be2008-09-09 12:31:33 +00001924 }else{
1925 pScript = objv[3];
drhcabb0812002-09-14 13:47:32 +00001926 }
1927 zName = Tcl_GetStringFromObj(objv[2], 0);
drhd1e47332005-06-26 17:55:33 +00001928 pFunc = findSqlFunc(pDb, zName);
drhcabb0812002-09-14 13:47:32 +00001929 if( pFunc==0 ) return TCL_ERROR;
drhd1e47332005-06-26 17:55:33 +00001930 if( pFunc->pScript ){
1931 Tcl_DecrRefCount(pFunc->pScript);
1932 }
1933 pFunc->pScript = pScript;
1934 Tcl_IncrRefCount(pScript);
1935 pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript);
drhe3602be2008-09-09 12:31:33 +00001936 rc = sqlite3_create_function(pDb->db, zName, nArg, SQLITE_UTF8,
danielk1977d8123362004-06-12 09:25:12 +00001937 pFunc, tclSqlFunc, 0, 0);
drhfb7e7652005-01-24 00:28:42 +00001938 if( rc!=SQLITE_OK ){
danielk19779636c4e2005-01-25 04:27:54 +00001939 rc = TCL_ERROR;
1940 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
drhfb7e7652005-01-24 00:28:42 +00001941 }
drhcabb0812002-09-14 13:47:32 +00001942 break;
1943 }
1944
1945 /*
danielk19778cbadb02007-05-03 16:31:26 +00001946 ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID
danielk1977b4e9af92007-05-01 17:49:49 +00001947 */
1948 case DB_INCRBLOB: {
danielk197732a0d8b2007-05-04 19:03:02 +00001949#ifdef SQLITE_OMIT_INCRBLOB
1950 Tcl_AppendResult(interp, "incrblob not available in this build", 0);
1951 return TCL_ERROR;
1952#else
danielk19778cbadb02007-05-03 16:31:26 +00001953 int isReadonly = 0;
danielk1977b4e9af92007-05-01 17:49:49 +00001954 const char *zDb = "main";
1955 const char *zTable;
1956 const char *zColumn;
1957 sqlite_int64 iRow;
1958
danielk19778cbadb02007-05-03 16:31:26 +00001959 /* Check for the -readonly option */
1960 if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){
1961 isReadonly = 1;
1962 }
1963
1964 if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){
1965 Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID");
danielk1977b4e9af92007-05-01 17:49:49 +00001966 return TCL_ERROR;
1967 }
1968
danielk19778cbadb02007-05-03 16:31:26 +00001969 if( objc==(6+isReadonly) ){
danielk1977b4e9af92007-05-01 17:49:49 +00001970 zDb = Tcl_GetString(objv[2]);
1971 }
1972 zTable = Tcl_GetString(objv[objc-3]);
1973 zColumn = Tcl_GetString(objv[objc-2]);
1974 rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow);
1975
1976 if( rc==TCL_OK ){
danielk19778cbadb02007-05-03 16:31:26 +00001977 rc = createIncrblobChannel(
1978 interp, pDb, zDb, zTable, zColumn, iRow, isReadonly
1979 );
danielk1977b4e9af92007-05-01 17:49:49 +00001980 }
danielk197732a0d8b2007-05-04 19:03:02 +00001981#endif
danielk1977b4e9af92007-05-01 17:49:49 +00001982 break;
1983 }
1984
1985 /*
drhf11bded2006-07-17 00:02:44 +00001986 ** $db interrupt
1987 **
1988 ** Interrupt the execution of the inner-most SQL interpreter. This
1989 ** causes the SQL statement to return an error of SQLITE_INTERRUPT.
1990 */
1991 case DB_INTERRUPT: {
1992 sqlite3_interrupt(pDb->db);
1993 break;
1994 }
1995
1996 /*
drh19e2d372005-08-29 23:00:03 +00001997 ** $db nullvalue ?STRING?
1998 **
1999 ** Change text used when a NULL comes back from the database. If ?STRING?
2000 ** is not present, then the current string used for NULL is returned.
2001 ** If STRING is present, then STRING is returned.
2002 **
2003 */
2004 case DB_NULLVALUE: {
2005 if( objc!=2 && objc!=3 ){
2006 Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE");
2007 return TCL_ERROR;
2008 }
2009 if( objc==3 ){
2010 int len;
2011 char *zNull = Tcl_GetStringFromObj(objv[2], &len);
2012 if( pDb->zNull ){
2013 Tcl_Free(pDb->zNull);
2014 }
2015 if( zNull && len>0 ){
2016 pDb->zNull = Tcl_Alloc( len + 1 );
2017 strncpy(pDb->zNull, zNull, len);
2018 pDb->zNull[len] = '\0';
2019 }else{
2020 pDb->zNull = 0;
2021 }
2022 }
2023 Tcl_SetObjResult(interp, dbTextToObj(pDb->zNull));
2024 break;
2025 }
2026
2027 /*
drhaf9ff332002-01-16 21:00:27 +00002028 ** $db last_insert_rowid
2029 **
2030 ** Return an integer which is the ROWID for the most recent insert.
2031 */
2032 case DB_LAST_INSERT_ROWID: {
2033 Tcl_Obj *pResult;
drhf7e678d2006-06-21 19:30:34 +00002034 Tcl_WideInt rowid;
drhaf9ff332002-01-16 21:00:27 +00002035 if( objc!=2 ){
2036 Tcl_WrongNumArgs(interp, 2, objv, "");
2037 return TCL_ERROR;
2038 }
danielk19776f8a5032004-05-10 10:34:51 +00002039 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +00002040 pResult = Tcl_GetObjResult(interp);
drhf7e678d2006-06-21 19:30:34 +00002041 Tcl_SetWideIntObj(pResult, rowid);
drhaf9ff332002-01-16 21:00:27 +00002042 break;
2043 }
2044
2045 /*
drh1807ce32004-09-07 13:20:35 +00002046 ** The DB_ONECOLUMN method is implemented together with DB_EVAL.
drh5d9d7572003-08-19 14:31:01 +00002047 */
drh1807ce32004-09-07 13:20:35 +00002048
2049 /* $db progress ?N CALLBACK?
2050 **
2051 ** Invoke the given callback every N virtual machine opcodes while executing
2052 ** queries.
2053 */
2054 case DB_PROGRESS: {
2055 if( objc==2 ){
2056 if( pDb->zProgress ){
2057 Tcl_AppendResult(interp, pDb->zProgress, 0);
2058 }
2059 }else if( objc==4 ){
2060 char *zProgress;
2061 int len;
2062 int N;
2063 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
drhfd131da2007-08-07 17:13:03 +00002064 return TCL_ERROR;
drh1807ce32004-09-07 13:20:35 +00002065 };
2066 if( pDb->zProgress ){
2067 Tcl_Free(pDb->zProgress);
2068 }
2069 zProgress = Tcl_GetStringFromObj(objv[3], &len);
2070 if( zProgress && len>0 ){
2071 pDb->zProgress = Tcl_Alloc( len + 1 );
drh5bb3eb92007-05-04 13:15:55 +00002072 memcpy(pDb->zProgress, zProgress, len+1);
drh1807ce32004-09-07 13:20:35 +00002073 }else{
2074 pDb->zProgress = 0;
2075 }
2076#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2077 if( pDb->zProgress ){
2078 pDb->interp = interp;
2079 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
2080 }else{
2081 sqlite3_progress_handler(pDb->db, 0, 0, 0);
2082 }
2083#endif
2084 }else{
2085 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
drh5d9d7572003-08-19 14:31:01 +00002086 return TCL_ERROR;
2087 }
drh5d9d7572003-08-19 14:31:01 +00002088 break;
2089 }
2090
drh19e2d372005-08-29 23:00:03 +00002091 /* $db profile ?CALLBACK?
2092 **
2093 ** Make arrangements to invoke the CALLBACK routine after each SQL statement
2094 ** that has run. The text of the SQL and the amount of elapse time are
2095 ** appended to CALLBACK before the script is run.
2096 */
2097 case DB_PROFILE: {
2098 if( objc>3 ){
2099 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
2100 return TCL_ERROR;
2101 }else if( objc==2 ){
2102 if( pDb->zProfile ){
2103 Tcl_AppendResult(interp, pDb->zProfile, 0);
2104 }
2105 }else{
2106 char *zProfile;
2107 int len;
2108 if( pDb->zProfile ){
2109 Tcl_Free(pDb->zProfile);
2110 }
2111 zProfile = Tcl_GetStringFromObj(objv[2], &len);
2112 if( zProfile && len>0 ){
2113 pDb->zProfile = Tcl_Alloc( len + 1 );
drh5bb3eb92007-05-04 13:15:55 +00002114 memcpy(pDb->zProfile, zProfile, len+1);
drh19e2d372005-08-29 23:00:03 +00002115 }else{
2116 pDb->zProfile = 0;
2117 }
2118#ifndef SQLITE_OMIT_TRACE
2119 if( pDb->zProfile ){
2120 pDb->interp = interp;
2121 sqlite3_profile(pDb->db, DbProfileHandler, pDb);
2122 }else{
2123 sqlite3_profile(pDb->db, 0, 0);
2124 }
2125#endif
2126 }
2127 break;
2128 }
2129
drh5d9d7572003-08-19 14:31:01 +00002130 /*
drh22fbcb82004-02-01 01:22:50 +00002131 ** $db rekey KEY
2132 **
2133 ** Change the encryption key on the currently open database.
2134 */
2135 case DB_REKEY: {
2136 int nKey;
2137 void *pKey;
2138 if( objc!=3 ){
2139 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
2140 return TCL_ERROR;
2141 }
2142 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh9eb9e262004-02-11 02:18:05 +00002143#ifdef SQLITE_HAS_CODEC
drh2011d5f2004-07-22 02:40:37 +00002144 rc = sqlite3_rekey(pDb->db, pKey, nKey);
drh22fbcb82004-02-01 01:22:50 +00002145 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +00002146 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh22fbcb82004-02-01 01:22:50 +00002147 rc = TCL_ERROR;
2148 }
2149#endif
2150 break;
2151 }
2152
2153 /*
drhd1d38482008-10-07 23:46:38 +00002154 ** $db status (step|sort)
2155 **
2156 ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or
2157 ** SQLITE_STMTSTATUS_SORT for the most recent eval.
2158 */
2159 case DB_STATUS: {
drhd1d38482008-10-07 23:46:38 +00002160 int v;
2161 const char *zOp;
2162 if( objc!=3 ){
2163 Tcl_WrongNumArgs(interp, 2, objv, "(step|sort)");
2164 return TCL_ERROR;
2165 }
2166 zOp = Tcl_GetString(objv[2]);
2167 if( strcmp(zOp, "step")==0 ){
2168 v = pDb->nStep;
2169 }else if( strcmp(zOp, "sort")==0 ){
2170 v = pDb->nSort;
2171 }else{
2172 Tcl_AppendResult(interp, "bad argument: should be step or sort",
2173 (char*)0);
2174 return TCL_ERROR;
2175 }
2176 Tcl_SetObjResult(interp, Tcl_NewIntObj(v));
2177 break;
2178 }
2179
2180 /*
drhbec3f402000-08-04 13:49:02 +00002181 ** $db timeout MILLESECONDS
2182 **
2183 ** Delay for the number of milliseconds specified when a file is locked.
2184 */
drh6d313162000-09-21 13:01:35 +00002185 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +00002186 int ms;
drh6d313162000-09-21 13:01:35 +00002187 if( objc!=3 ){
2188 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +00002189 return TCL_ERROR;
2190 }
drh6d313162000-09-21 13:01:35 +00002191 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +00002192 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +00002193 break;
drh75897232000-05-29 14:26:00 +00002194 }
danielk197755c45f22005-04-03 23:54:43 +00002195
2196 /*
drh0f14e2e2004-06-29 12:39:08 +00002197 ** $db total_changes
2198 **
2199 ** Return the number of rows that were modified, inserted, or deleted
2200 ** since the database handle was created.
2201 */
2202 case DB_TOTAL_CHANGES: {
2203 Tcl_Obj *pResult;
2204 if( objc!=2 ){
2205 Tcl_WrongNumArgs(interp, 2, objv, "");
2206 return TCL_ERROR;
2207 }
2208 pResult = Tcl_GetObjResult(interp);
2209 Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db));
2210 break;
2211 }
2212
drhb5a20d32003-04-23 12:25:23 +00002213 /* $db trace ?CALLBACK?
2214 **
2215 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
2216 ** that is executed. The text of the SQL is appended to CALLBACK before
2217 ** it is executed.
2218 */
2219 case DB_TRACE: {
2220 if( objc>3 ){
2221 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drhb97759e2004-06-29 11:26:59 +00002222 return TCL_ERROR;
drhb5a20d32003-04-23 12:25:23 +00002223 }else if( objc==2 ){
2224 if( pDb->zTrace ){
2225 Tcl_AppendResult(interp, pDb->zTrace, 0);
2226 }
2227 }else{
2228 char *zTrace;
2229 int len;
2230 if( pDb->zTrace ){
2231 Tcl_Free(pDb->zTrace);
2232 }
2233 zTrace = Tcl_GetStringFromObj(objv[2], &len);
2234 if( zTrace && len>0 ){
2235 pDb->zTrace = Tcl_Alloc( len + 1 );
drh5bb3eb92007-05-04 13:15:55 +00002236 memcpy(pDb->zTrace, zTrace, len+1);
drhb5a20d32003-04-23 12:25:23 +00002237 }else{
2238 pDb->zTrace = 0;
2239 }
drh19e2d372005-08-29 23:00:03 +00002240#ifndef SQLITE_OMIT_TRACE
drhb5a20d32003-04-23 12:25:23 +00002241 if( pDb->zTrace ){
2242 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +00002243 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +00002244 }else{
danielk19776f8a5032004-05-10 10:34:51 +00002245 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +00002246 }
drh19e2d372005-08-29 23:00:03 +00002247#endif
drhb5a20d32003-04-23 12:25:23 +00002248 }
2249 break;
2250 }
2251
drh3d214232005-08-02 12:21:08 +00002252 /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT
2253 **
2254 ** Start a new transaction (if we are not already in the midst of a
2255 ** transaction) and execute the TCL script SCRIPT. After SCRIPT
2256 ** completes, either commit the transaction or roll it back if SCRIPT
2257 ** throws an exception. Or if no new transation was started, do nothing.
2258 ** pass the exception on up the stack.
2259 **
2260 ** This command was inspired by Dave Thomas's talk on Ruby at the
2261 ** 2005 O'Reilly Open Source Convention (OSCON).
2262 */
2263 case DB_TRANSACTION: {
2264 int inTrans;
2265 Tcl_Obj *pScript;
2266 const char *zBegin = "BEGIN";
2267 if( objc!=3 && objc!=4 ){
2268 Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT");
2269 return TCL_ERROR;
2270 }
2271 if( objc==3 ){
2272 pScript = objv[2];
2273 } else {
2274 static const char *TTYPE_strs[] = {
drhce604012005-08-16 11:11:34 +00002275 "deferred", "exclusive", "immediate", 0
drh3d214232005-08-02 12:21:08 +00002276 };
2277 enum TTYPE_enum {
2278 TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE
2279 };
2280 int ttype;
drhb5555e72005-08-02 17:15:14 +00002281 if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type",
drh3d214232005-08-02 12:21:08 +00002282 0, &ttype) ){
2283 return TCL_ERROR;
2284 }
2285 switch( (enum TTYPE_enum)ttype ){
2286 case TTYPE_DEFERRED: /* no-op */; break;
2287 case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break;
2288 case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break;
2289 }
2290 pScript = objv[3];
2291 }
2292 inTrans = !sqlite3_get_autocommit(pDb->db);
2293 if( !inTrans ){
drh1f1549f2008-08-26 21:33:34 +00002294 pDb->disableAuth++;
drh37527852006-03-16 16:19:56 +00002295 (void)sqlite3_exec(pDb->db, zBegin, 0, 0, 0);
drh1f1549f2008-08-26 21:33:34 +00002296 pDb->disableAuth--;
drh3d214232005-08-02 12:21:08 +00002297 }
2298 rc = Tcl_EvalObjEx(interp, pScript, 0);
2299 if( !inTrans ){
2300 const char *zEnd;
2301 if( rc==TCL_ERROR ){
2302 zEnd = "ROLLBACK";
2303 } else {
2304 zEnd = "COMMIT";
2305 }
drh1f1549f2008-08-26 21:33:34 +00002306 pDb->disableAuth++;
drh109b4352007-06-12 18:50:13 +00002307 if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){
2308 sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0);
2309 }
drh1f1549f2008-08-26 21:33:34 +00002310 pDb->disableAuth--;
drh3d214232005-08-02 12:21:08 +00002311 }
2312 break;
2313 }
2314
danielk197794eb6a12005-12-15 15:22:08 +00002315 /*
2316 ** $db update_hook ?script?
danielk197771fd80b2005-12-16 06:54:01 +00002317 ** $db rollback_hook ?script?
danielk197794eb6a12005-12-15 15:22:08 +00002318 */
danielk197771fd80b2005-12-16 06:54:01 +00002319 case DB_UPDATE_HOOK:
2320 case DB_ROLLBACK_HOOK: {
2321
2322 /* set ppHook to point at pUpdateHook or pRollbackHook, depending on
2323 ** whether [$db update_hook] or [$db rollback_hook] was invoked.
2324 */
2325 Tcl_Obj **ppHook;
2326 if( choice==DB_UPDATE_HOOK ){
2327 ppHook = &pDb->pUpdateHook;
2328 }else{
2329 ppHook = &pDb->pRollbackHook;
2330 }
2331
danielk197794eb6a12005-12-15 15:22:08 +00002332 if( objc!=2 && objc!=3 ){
2333 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?");
2334 return TCL_ERROR;
2335 }
danielk197771fd80b2005-12-16 06:54:01 +00002336 if( *ppHook ){
2337 Tcl_SetObjResult(interp, *ppHook);
danielk197794eb6a12005-12-15 15:22:08 +00002338 if( objc==3 ){
danielk197771fd80b2005-12-16 06:54:01 +00002339 Tcl_DecrRefCount(*ppHook);
2340 *ppHook = 0;
danielk197794eb6a12005-12-15 15:22:08 +00002341 }
2342 }
2343 if( objc==3 ){
danielk197771fd80b2005-12-16 06:54:01 +00002344 assert( !(*ppHook) );
danielk197794eb6a12005-12-15 15:22:08 +00002345 if( Tcl_GetCharLength(objv[2])>0 ){
danielk197771fd80b2005-12-16 06:54:01 +00002346 *ppHook = objv[2];
2347 Tcl_IncrRefCount(*ppHook);
danielk197794eb6a12005-12-15 15:22:08 +00002348 }
2349 }
danielk197771fd80b2005-12-16 06:54:01 +00002350
2351 sqlite3_update_hook(pDb->db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb);
2352 sqlite3_rollback_hook(pDb->db,(pDb->pRollbackHook?DbRollbackHandler:0),pDb);
2353
danielk197794eb6a12005-12-15 15:22:08 +00002354 break;
2355 }
2356
danielk19774397de52005-01-12 12:44:03 +00002357 /* $db version
2358 **
2359 ** Return the version string for this database.
2360 */
2361 case DB_VERSION: {
2362 Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC);
2363 break;
2364 }
2365
tpoindex1067fe12004-12-17 15:41:11 +00002366
drh6d313162000-09-21 13:01:35 +00002367 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +00002368 return rc;
drh75897232000-05-29 14:26:00 +00002369}
2370
2371/*
drh3570ad92007-08-31 14:31:44 +00002372** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN?
danielk19779a6284c2008-07-10 17:52:49 +00002373** ?-create BOOLEAN? ?-nomutex BOOLEAN?
drh75897232000-05-29 14:26:00 +00002374**
2375** This is the main Tcl command. When the "sqlite" Tcl command is
2376** invoked, this routine runs to process that command.
2377**
2378** The first argument, DBNAME, is an arbitrary name for a new
2379** database connection. This command creates a new command named
2380** DBNAME that is used to control that connection. The database
2381** connection is deleted when the DBNAME command is deleted.
2382**
drh3570ad92007-08-31 14:31:44 +00002383** The second argument is the name of the database file.
drhfbc3eab2001-04-06 16:13:42 +00002384**
drh75897232000-05-29 14:26:00 +00002385*/
drh22fbcb82004-02-01 01:22:50 +00002386static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
drhbec3f402000-08-04 13:49:02 +00002387 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +00002388 void *pKey = 0;
2389 int nKey = 0;
2390 const char *zArg;
drh75897232000-05-29 14:26:00 +00002391 char *zErrMsg;
drh3570ad92007-08-31 14:31:44 +00002392 int i;
drh22fbcb82004-02-01 01:22:50 +00002393 const char *zFile;
drh3570ad92007-08-31 14:31:44 +00002394 const char *zVfs = 0;
drhcc91e7f2008-09-03 01:08:00 +00002395 int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX;
drh882e8e42006-08-24 02:42:27 +00002396 Tcl_DString translatedFilename;
drh22fbcb82004-02-01 01:22:50 +00002397 if( objc==2 ){
2398 zArg = Tcl_GetStringFromObj(objv[1], 0);
drh22fbcb82004-02-01 01:22:50 +00002399 if( strcmp(zArg,"-version")==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00002400 Tcl_AppendResult(interp,sqlite3_version,0);
drh647cb0e2002-11-04 19:32:25 +00002401 return TCL_OK;
2402 }
drh9eb9e262004-02-11 02:18:05 +00002403 if( strcmp(zArg,"-has-codec")==0 ){
2404#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00002405 Tcl_AppendResult(interp,"1",0);
2406#else
2407 Tcl_AppendResult(interp,"0",0);
2408#endif
2409 return TCL_OK;
2410 }
drhfbc3eab2001-04-06 16:13:42 +00002411 }
drh3570ad92007-08-31 14:31:44 +00002412 for(i=3; i+1<objc; i+=2){
2413 zArg = Tcl_GetString(objv[i]);
drh22fbcb82004-02-01 01:22:50 +00002414 if( strcmp(zArg,"-key")==0 ){
drh3570ad92007-08-31 14:31:44 +00002415 pKey = Tcl_GetByteArrayFromObj(objv[i+1], &nKey);
2416 }else if( strcmp(zArg, "-vfs")==0 ){
2417 i++;
2418 zVfs = Tcl_GetString(objv[i]);
2419 }else if( strcmp(zArg, "-readonly")==0 ){
2420 int b;
2421 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR;
2422 if( b ){
drh33f4e022007-09-03 15:19:34 +00002423 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
drh3570ad92007-08-31 14:31:44 +00002424 flags |= SQLITE_OPEN_READONLY;
2425 }else{
2426 flags &= ~SQLITE_OPEN_READONLY;
2427 flags |= SQLITE_OPEN_READWRITE;
2428 }
2429 }else if( strcmp(zArg, "-create")==0 ){
2430 int b;
2431 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR;
drh33f4e022007-09-03 15:19:34 +00002432 if( b && (flags & SQLITE_OPEN_READONLY)==0 ){
drh3570ad92007-08-31 14:31:44 +00002433 flags |= SQLITE_OPEN_CREATE;
2434 }else{
2435 flags &= ~SQLITE_OPEN_CREATE;
2436 }
danielk19779a6284c2008-07-10 17:52:49 +00002437 }else if( strcmp(zArg, "-nomutex")==0 ){
2438 int b;
2439 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR;
2440 if( b ){
2441 flags |= SQLITE_OPEN_NOMUTEX;
drh039963a2008-09-03 00:43:15 +00002442 flags &= ~SQLITE_OPEN_FULLMUTEX;
danielk19779a6284c2008-07-10 17:52:49 +00002443 }else{
2444 flags &= ~SQLITE_OPEN_NOMUTEX;
2445 }
drh039963a2008-09-03 00:43:15 +00002446 }else if( strcmp(zArg, "-fullmutex")==0 ){
2447 int b;
2448 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR;
2449 if( b ){
2450 flags |= SQLITE_OPEN_FULLMUTEX;
2451 flags &= ~SQLITE_OPEN_NOMUTEX;
2452 }else{
2453 flags &= ~SQLITE_OPEN_FULLMUTEX;
2454 }
drh3570ad92007-08-31 14:31:44 +00002455 }else{
2456 Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0);
2457 return TCL_ERROR;
drh22fbcb82004-02-01 01:22:50 +00002458 }
2459 }
drh3570ad92007-08-31 14:31:44 +00002460 if( objc<3 || (objc&1)!=1 ){
drh22fbcb82004-02-01 01:22:50 +00002461 Tcl_WrongNumArgs(interp, 1, objv,
drh3570ad92007-08-31 14:31:44 +00002462 "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?"
drh039963a2008-09-03 00:43:15 +00002463 " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN?"
drh9eb9e262004-02-11 02:18:05 +00002464#ifdef SQLITE_HAS_CODEC
drh3570ad92007-08-31 14:31:44 +00002465 " ?-key CODECKEY?"
drh22fbcb82004-02-01 01:22:50 +00002466#endif
2467 );
drh75897232000-05-29 14:26:00 +00002468 return TCL_ERROR;
2469 }
drh75897232000-05-29 14:26:00 +00002470 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00002471 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drh75897232000-05-29 14:26:00 +00002472 if( p==0 ){
drhbec3f402000-08-04 13:49:02 +00002473 Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
2474 return TCL_ERROR;
2475 }
2476 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00002477 zFile = Tcl_GetStringFromObj(objv[2], 0);
drh882e8e42006-08-24 02:42:27 +00002478 zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename);
drh3570ad92007-08-31 14:31:44 +00002479 sqlite3_open_v2(zFile, &p->db, flags, zVfs);
drh882e8e42006-08-24 02:42:27 +00002480 Tcl_DStringFree(&translatedFilename);
danielk197780290862004-05-22 09:21:21 +00002481 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
drh9404d502006-12-19 18:46:08 +00002482 zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db));
danielk197780290862004-05-22 09:21:21 +00002483 sqlite3_close(p->db);
2484 p->db = 0;
2485 }
drh2011d5f2004-07-22 02:40:37 +00002486#ifdef SQLITE_HAS_CODEC
drhf3a65f72007-08-22 20:18:21 +00002487 if( p->db ){
2488 sqlite3_key(p->db, pKey, nKey);
2489 }
drheb8ed702004-02-11 10:37:23 +00002490#endif
drhbec3f402000-08-04 13:49:02 +00002491 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00002492 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00002493 Tcl_Free((char*)p);
drh9404d502006-12-19 18:46:08 +00002494 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00002495 return TCL_ERROR;
2496 }
drhfb7e7652005-01-24 00:28:42 +00002497 p->maxStmt = NUM_PREPARED_STMTS;
drh5169bbc2006-08-24 14:59:45 +00002498 p->interp = interp;
drh22fbcb82004-02-01 01:22:50 +00002499 zArg = Tcl_GetStringFromObj(objv[1], 0);
2500 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
drh75897232000-05-29 14:26:00 +00002501 return TCL_OK;
2502}
2503
2504/*
drh90ca9752001-09-28 17:47:14 +00002505** Provide a dummy Tcl_InitStubs if we are using this as a static
2506** library.
2507*/
2508#ifndef USE_TCL_STUBS
2509# undef Tcl_InitStubs
2510# define Tcl_InitStubs(a,b,c)
2511#endif
2512
2513/*
drh29bc4612005-10-05 10:40:15 +00002514** Make sure we have a PACKAGE_VERSION macro defined. This will be
2515** defined automatically by the TEA makefile. But other makefiles
2516** do not define it.
2517*/
2518#ifndef PACKAGE_VERSION
2519# define PACKAGE_VERSION SQLITE_VERSION
2520#endif
2521
2522/*
drh75897232000-05-29 14:26:00 +00002523** Initialize this module.
2524**
2525** This Tcl module contains only a single new Tcl command named "sqlite".
2526** (Hence there is no namespace. There is no point in using a namespace
2527** if the extension only supplies one new name!) The "sqlite" command is
2528** used to open a new SQLite database. See the DbMain() routine above
2529** for additional information.
2530*/
drhad6e1372006-07-10 21:15:51 +00002531EXTERN int Sqlite3_Init(Tcl_Interp *interp){
drh92febd92004-08-20 18:34:20 +00002532 Tcl_InitStubs(interp, "8.4", 0);
drhef4ac8f2004-06-19 00:16:31 +00002533 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh29bc4612005-10-05 10:40:15 +00002534 Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION);
drh49766d62005-01-08 18:42:28 +00002535 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh29bc4612005-10-05 10:40:15 +00002536 Tcl_PkgProvide(interp, "sqlite", PACKAGE_VERSION);
drh90ca9752001-09-28 17:47:14 +00002537 return TCL_OK;
2538}
drhad6e1372006-07-10 21:15:51 +00002539EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
2540EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
2541EXTERN int Tclsqlite3_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
drhe2c3a652008-09-23 09:58:46 +00002542EXTERN int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
2543EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
2544EXTERN int Sqlite3_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK; }
2545EXTERN int Tclsqlite3_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK;}
2546
drh49766d62005-01-08 18:42:28 +00002547
2548#ifndef SQLITE_3_SUFFIX_ONLY
drhad6e1372006-07-10 21:15:51 +00002549EXTERN int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
2550EXTERN int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
2551EXTERN int Sqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
2552EXTERN int Tclsqlite_SafeInit(Tcl_Interp *interp){ return TCL_OK; }
drhe2c3a652008-09-23 09:58:46 +00002553EXTERN int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
2554EXTERN int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
2555EXTERN int Sqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK; }
2556EXTERN int Tclsqlite_SafeUnload(Tcl_Interp *interp, int flags){ return TCL_OK;}
drh49766d62005-01-08 18:42:28 +00002557#endif
drh75897232000-05-29 14:26:00 +00002558
drh3e27c022004-07-23 00:01:38 +00002559#ifdef TCLSH
2560/*****************************************************************************
2561** The code that follows is used to build standalone TCL interpreters
drh3570ad92007-08-31 14:31:44 +00002562** that are statically linked with SQLite.
drh75897232000-05-29 14:26:00 +00002563*/
drh348784e2000-05-29 20:41:49 +00002564
2565/*
drh3e27c022004-07-23 00:01:38 +00002566** If the macro TCLSH is one, then put in code this for the
2567** "main" routine that will initialize Tcl and take input from
drh3570ad92007-08-31 14:31:44 +00002568** standard input, or if a file is named on the command line
2569** the TCL interpreter reads and evaluates that file.
drh348784e2000-05-29 20:41:49 +00002570*/
drh3e27c022004-07-23 00:01:38 +00002571#if TCLSH==1
drh348784e2000-05-29 20:41:49 +00002572static char zMainloop[] =
2573 "set line {}\n"
2574 "while {![eof stdin]} {\n"
2575 "if {$line!=\"\"} {\n"
2576 "puts -nonewline \"> \"\n"
2577 "} else {\n"
2578 "puts -nonewline \"% \"\n"
2579 "}\n"
2580 "flush stdout\n"
2581 "append line [gets stdin]\n"
2582 "if {[info complete $line]} {\n"
2583 "if {[catch {uplevel #0 $line} result]} {\n"
2584 "puts stderr \"Error: $result\"\n"
2585 "} elseif {$result!=\"\"} {\n"
2586 "puts $result\n"
2587 "}\n"
2588 "set line {}\n"
2589 "} else {\n"
2590 "append line \\n\n"
2591 "}\n"
2592 "}\n"
2593;
drh3e27c022004-07-23 00:01:38 +00002594#endif
2595
2596/*
2597** If the macro TCLSH is two, then get the main loop code out of
2598** the separate file "spaceanal_tcl.h".
2599*/
2600#if TCLSH==2
2601static char zMainloop[] =
2602#include "spaceanal_tcl.h"
2603;
2604#endif
drh348784e2000-05-29 20:41:49 +00002605
2606#define TCLSH_MAIN main /* Needed to fake out mktclapp */
2607int TCLSH_MAIN(int argc, char **argv){
2608 Tcl_Interp *interp;
drh297ecf12001-04-05 15:57:13 +00002609 Tcl_FindExecutable(argv[0]);
drh348784e2000-05-29 20:41:49 +00002610 interp = Tcl_CreateInterp();
drh38f82712004-06-18 17:10:16 +00002611 Sqlite3_Init(interp);
drhd9b02572001-04-15 00:37:09 +00002612#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00002613 {
drh2f999a62007-08-15 19:16:43 +00002614 extern int Md5_Init(Tcl_Interp*);
2615 extern int Sqliteconfig_Init(Tcl_Interp*);
drhd1bf3512001-04-07 15:24:33 +00002616 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00002617 extern int Sqlitetest2_Init(Tcl_Interp*);
2618 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00002619 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00002620 extern int Sqlitetest5_Init(Tcl_Interp*);
drh9c06c952005-11-26 00:25:00 +00002621 extern int Sqlitetest6_Init(Tcl_Interp*);
drh29c636b2006-01-09 23:40:25 +00002622 extern int Sqlitetest7_Init(Tcl_Interp*);
drhb9bb7c12006-06-11 23:41:55 +00002623 extern int Sqlitetest8_Init(Tcl_Interp*);
danielk1977a713f2c2007-03-29 12:19:11 +00002624 extern int Sqlitetest9_Init(Tcl_Interp*);
drh23669402006-01-09 17:29:52 +00002625 extern int Sqlitetestasync_Init(Tcl_Interp*);
drh1409be62006-08-23 20:07:20 +00002626 extern int Sqlitetest_autoext_Init(Tcl_Interp*);
drh984bfaa2008-03-19 16:08:53 +00002627 extern int Sqlitetest_func_Init(Tcl_Interp*);
drh15926592007-04-06 15:02:13 +00002628 extern int Sqlitetest_hexio_Init(Tcl_Interp*);
drh2f999a62007-08-15 19:16:43 +00002629 extern int Sqlitetest_malloc_Init(Tcl_Interp*);
danielk19771a9ed0b2008-06-18 09:45:56 +00002630 extern int Sqlitetest_mutex_Init(Tcl_Interp*);
drh2f999a62007-08-15 19:16:43 +00002631 extern int Sqlitetestschema_Init(Tcl_Interp*);
2632 extern int Sqlitetestsse_Init(Tcl_Interp*);
2633 extern int Sqlitetesttclvar_Init(Tcl_Interp*);
danielk197744918fa2007-09-07 11:29:25 +00002634 extern int SqlitetestThread_Init(Tcl_Interp*);
danielk1977a15db352007-09-14 16:20:00 +00002635 extern int SqlitetestOnefile_Init();
danielk19775d1f5aa2008-04-10 14:51:00 +00002636 extern int SqlitetestOsinst_Init(Tcl_Interp*);
drh2e66f0b2005-04-28 17:18:48 +00002637
drh2f999a62007-08-15 19:16:43 +00002638 Md5_Init(interp);
2639 Sqliteconfig_Init(interp);
danielk19776490beb2004-05-11 06:17:21 +00002640 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00002641 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00002642 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00002643 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00002644 Sqlitetest5_Init(interp);
drh9c06c952005-11-26 00:25:00 +00002645 Sqlitetest6_Init(interp);
drh29c636b2006-01-09 23:40:25 +00002646 Sqlitetest7_Init(interp);
drhb9bb7c12006-06-11 23:41:55 +00002647 Sqlitetest8_Init(interp);
danielk1977a713f2c2007-03-29 12:19:11 +00002648 Sqlitetest9_Init(interp);
drh23669402006-01-09 17:29:52 +00002649 Sqlitetestasync_Init(interp);
drh1409be62006-08-23 20:07:20 +00002650 Sqlitetest_autoext_Init(interp);
drh984bfaa2008-03-19 16:08:53 +00002651 Sqlitetest_func_Init(interp);
drh15926592007-04-06 15:02:13 +00002652 Sqlitetest_hexio_Init(interp);
drh2f999a62007-08-15 19:16:43 +00002653 Sqlitetest_malloc_Init(interp);
danielk19771a9ed0b2008-06-18 09:45:56 +00002654 Sqlitetest_mutex_Init(interp);
drh2f999a62007-08-15 19:16:43 +00002655 Sqlitetestschema_Init(interp);
2656 Sqlitetesttclvar_Init(interp);
danielk197744918fa2007-09-07 11:29:25 +00002657 SqlitetestThread_Init(interp);
danielk1977a15db352007-09-14 16:20:00 +00002658 SqlitetestOnefile_Init(interp);
danielk19775d1f5aa2008-04-10 14:51:00 +00002659 SqlitetestOsinst_Init(interp);
danielk1977a15db352007-09-14 16:20:00 +00002660
drh89dec812005-04-28 19:03:37 +00002661#ifdef SQLITE_SSE
drh2e66f0b2005-04-28 17:18:48 +00002662 Sqlitetestsse_Init(interp);
2663#endif
drhd1bf3512001-04-07 15:24:33 +00002664 }
2665#endif
drh3e27c022004-07-23 00:01:38 +00002666 if( argc>=2 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00002667 int i;
shessad42c3a2006-08-22 23:53:46 +00002668 char zArgc[32];
2669 sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH));
2670 Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY);
drh348784e2000-05-29 20:41:49 +00002671 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
2672 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
drh61212b62004-12-02 20:17:00 +00002673 for(i=3-TCLSH; i<argc; i++){
drh348784e2000-05-29 20:41:49 +00002674 Tcl_SetVar(interp, "argv", argv[i],
2675 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
2676 }
drh3e27c022004-07-23 00:01:38 +00002677 if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00002678 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drhc61053b2000-06-04 12:58:36 +00002679 if( zInfo==0 ) zInfo = interp->result;
2680 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00002681 return 1;
2682 }
drh3e27c022004-07-23 00:01:38 +00002683 }
2684 if( argc<=1 || TCLSH==2 ){
drh348784e2000-05-29 20:41:49 +00002685 Tcl_GlobalEval(interp, zMainloop);
2686 }
2687 return 0;
2688}
2689#endif /* TCLSH */