blob: bb7c9e5b82b5ab101b19859b25b73e737fecdb62 [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.
drh57a02272009-10-22 20:52:05 +000014**
15** Compile-time options:
16**
17** -DTCLSH=1 Add a "main()" routine that works as a tclsh.
18**
19** -DSQLITE_TCLMD5 When used in conjuction with -DTCLSH=1, add
20** four new commands to the TCL interpreter for
21** generating MD5 checksums: md5, md5file,
22** md5-10x8, and md5file-10x8.
23**
24** -DSQLITE_TEST When used in conjuction with -DTCLSH=1, add
25** hundreds of new commands used for testing
26** SQLite. This option implies -DSQLITE_TCLMD5.
drh75897232000-05-29 14:26:00 +000027*/
mistachkin27b2f052015-01-12 19:49:46 +000028
29/*
30** If requested, include the SQLite compiler options file for MSVC.
31*/
32#if defined(INCLUDE_MSVC_H)
mistachkin52b1dbb2016-07-28 14:37:04 +000033# include "msvc.h"
mistachkin27b2f052015-01-12 19:49:46 +000034#endif
35
mistachkin52b1dbb2016-07-28 14:37:04 +000036#if defined(INCLUDE_SQLITE_TCL_H)
37# include "sqlite_tcl.h"
38#else
39# include "tcl.h"
mistachkin7617e4a2016-07-28 17:11:20 +000040# ifndef SQLITE_TCLAPI
41# define SQLITE_TCLAPI
42# endif
mistachkin52b1dbb2016-07-28 14:37:04 +000043#endif
danielk1977b4e9af92007-05-01 17:49:49 +000044#include <errno.h>
drhbd08af42007-04-05 21:58:33 +000045
46/*
47** Some additional include files are needed if this file is not
48** appended to the amalgamation.
49*/
50#ifndef SQLITE_AMALGAMATION
drh65e8c822009-12-01 13:57:48 +000051# include "sqlite3.h"
drhbd08af42007-04-05 21:58:33 +000052# include <stdlib.h>
53# include <string.h>
54# include <assert.h>
drh65e8c822009-12-01 13:57:48 +000055 typedef unsigned char u8;
drhbd08af42007-04-05 21:58:33 +000056#endif
drheb206382009-10-24 15:51:33 +000057#include <ctype.h>
drh75897232000-05-29 14:26:00 +000058
mistachkin1f28e072013-08-15 08:06:15 +000059/* Used to get the current process ID */
60#if !defined(_WIN32)
61# include <unistd.h>
62# define GETPID getpid
63#elif !defined(_WIN32_WCE)
64# ifndef SQLITE_AMALGAMATION
65# define WIN32_LEAN_AND_MEAN
66# include <windows.h>
67# endif
68# define GETPID (int)GetCurrentProcessId
69#endif
70
drhad6e1372006-07-10 21:15:51 +000071/*
72 * Windows needs to know which symbols to export. Unix does not.
73 * BUILD_sqlite should be undefined for Unix.
74 */
75#ifdef BUILD_sqlite
76#undef TCL_STORAGE_CLASS
77#define TCL_STORAGE_CLASS DLLEXPORT
78#endif /* BUILD_sqlite */
drh29bc4612005-10-05 10:40:15 +000079
danielk1977a21c6b62005-01-24 10:25:59 +000080#define NUM_PREPARED_STMTS 10
drhfb7e7652005-01-24 00:28:42 +000081#define MAX_PREPARED_STMTS 100
82
drhc45e6712012-10-03 11:02:33 +000083/* Forward declaration */
84typedef struct SqliteDb SqliteDb;
drh98808ba2001-10-18 12:34:46 +000085
86/*
drhcabb0812002-09-14 13:47:32 +000087** New SQL functions can be created as TCL scripts. Each such function
88** is described by an instance of the following structure.
89*/
90typedef struct SqlFunc SqlFunc;
91struct SqlFunc {
92 Tcl_Interp *interp; /* The TCL interpret to execute the function */
drhd1e47332005-06-26 17:55:33 +000093 Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */
drhc45e6712012-10-03 11:02:33 +000094 SqliteDb *pDb; /* Database connection that owns this function */
drhd1e47332005-06-26 17:55:33 +000095 int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */
96 char *zName; /* Name of this function */
drhcabb0812002-09-14 13:47:32 +000097 SqlFunc *pNext; /* Next function on the list of them all */
98};
99
100/*
danielk19770202b292004-06-09 09:55:16 +0000101** New collation sequences function can be created as TCL scripts. Each such
102** function is described by an instance of the following structure.
103*/
104typedef struct SqlCollate SqlCollate;
105struct SqlCollate {
106 Tcl_Interp *interp; /* The TCL interpret to execute the function */
107 char *zScript; /* The script to be run */
drhd1e47332005-06-26 17:55:33 +0000108 SqlCollate *pNext; /* Next function on the list of them all */
danielk19770202b292004-06-09 09:55:16 +0000109};
110
111/*
drhfb7e7652005-01-24 00:28:42 +0000112** Prepared statements are cached for faster execution. Each prepared
113** statement is described by an instance of the following structure.
114*/
115typedef struct SqlPreparedStmt SqlPreparedStmt;
116struct SqlPreparedStmt {
117 SqlPreparedStmt *pNext; /* Next in linked list */
118 SqlPreparedStmt *pPrev; /* Previous on the list */
119 sqlite3_stmt *pStmt; /* The prepared statement */
120 int nSql; /* chars in zSql[] */
danielk1977d0e2a852007-11-14 06:48:48 +0000121 const char *zSql; /* Text of the SQL statement */
dan4a4c11a2009-10-06 14:59:02 +0000122 int nParm; /* Size of apParm array */
123 Tcl_Obj **apParm; /* Array of referenced object pointers */
drhfb7e7652005-01-24 00:28:42 +0000124};
125
danielk1977d04417962007-05-02 13:16:30 +0000126typedef struct IncrblobChannel IncrblobChannel;
127
drhfb7e7652005-01-24 00:28:42 +0000128/*
drhbec3f402000-08-04 13:49:02 +0000129** There is one instance of this structure for each SQLite database
130** that has been opened by the SQLite TCL interface.
danc431fd52011-06-27 16:55:50 +0000131**
132** If this module is built with SQLITE_TEST defined (to create the SQLite
133** testfixture executable), then it may be configured to use either
134** sqlite3_prepare_v2() or sqlite3_prepare() to prepare SQL statements.
135** If SqliteDb.bLegacyPrepare is true, sqlite3_prepare() is used.
drhbec3f402000-08-04 13:49:02 +0000136*/
drhbec3f402000-08-04 13:49:02 +0000137struct SqliteDb {
drhdddca282006-01-03 00:33:50 +0000138 sqlite3 *db; /* The "real" database structure. MUST BE FIRST */
drhd1e47332005-06-26 17:55:33 +0000139 Tcl_Interp *interp; /* The interpreter used for this database */
140 char *zBusy; /* The busy callback routine */
141 char *zCommit; /* The commit hook callback routine */
142 char *zTrace; /* The trace callback routine */
mistachkinb56660f2016-07-14 21:26:09 +0000143 char *zTraceV2; /* The trace_v2 callback routine */
drh19e2d372005-08-29 23:00:03 +0000144 char *zProfile; /* The profile callback routine */
drhd1e47332005-06-26 17:55:33 +0000145 char *zProgress; /* The progress callback routine */
146 char *zAuth; /* The authorization callback routine */
drh1f1549f2008-08-26 21:33:34 +0000147 int disableAuth; /* Disable the authorizer if it exists */
drhd1e47332005-06-26 17:55:33 +0000148 char *zNull; /* Text to substitute for an SQL NULL value */
149 SqlFunc *pFunc; /* List of SQL functions */
danielk197794eb6a12005-12-15 15:22:08 +0000150 Tcl_Obj *pUpdateHook; /* Update hook script (if any) */
dan46c47d42011-03-01 18:42:07 +0000151 Tcl_Obj *pPreUpdateHook; /* Pre-update hook script (if any) */
danielk197771fd80b2005-12-16 06:54:01 +0000152 Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */
drh5def0842010-05-05 20:00:25 +0000153 Tcl_Obj *pWalHook; /* WAL hook script (if any) */
danielk1977404ca072009-03-16 13:19:36 +0000154 Tcl_Obj *pUnlockNotify; /* Unlock notify script (if any) */
drhd1e47332005-06-26 17:55:33 +0000155 SqlCollate *pCollate; /* List of SQL collation functions */
156 int rc; /* Return code of most recent sqlite3_exec() */
157 Tcl_Obj *pCollateNeeded; /* Collation needed script */
drhfb7e7652005-01-24 00:28:42 +0000158 SqlPreparedStmt *stmtList; /* List of prepared statements*/
159 SqlPreparedStmt *stmtLast; /* Last statement in the list */
160 int maxStmt; /* The next maximum number of stmtList */
161 int nStmt; /* Number of statements in stmtList */
danielk1977d04417962007-05-02 13:16:30 +0000162 IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */
drh3c379b02010-04-07 19:31:59 +0000163 int nStep, nSort, nIndex; /* Statistics for most recent operation */
danc456a762017-06-22 16:51:16 +0000164 int nVMStep; /* Another statistic for most recent operation */
danielk1977cd38d522009-01-02 17:33:46 +0000165 int nTransaction; /* Number of nested [transaction] methods */
drh147ef392016-01-22 23:17:51 +0000166 int openFlags; /* Flags used to open. (SQLITE_OPEN_URI) */
danc431fd52011-06-27 16:55:50 +0000167#ifdef SQLITE_TEST
168 int bLegacyPrepare; /* True to use sqlite3_prepare() */
169#endif
drh98808ba2001-10-18 12:34:46 +0000170};
drh297ecf12001-04-05 15:57:13 +0000171
danielk1977b4e9af92007-05-01 17:49:49 +0000172struct IncrblobChannel {
danielk1977d04417962007-05-02 13:16:30 +0000173 sqlite3_blob *pBlob; /* sqlite3 blob handle */
danielk1977dcbb5d32007-05-04 18:36:44 +0000174 SqliteDb *pDb; /* Associated database connection */
danielk1977d04417962007-05-02 13:16:30 +0000175 int iSeek; /* Current seek offset */
danielk1977d04417962007-05-02 13:16:30 +0000176 Tcl_Channel channel; /* Channel identifier */
177 IncrblobChannel *pNext; /* Linked list of all open incrblob channels */
178 IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */
danielk1977b4e9af92007-05-01 17:49:49 +0000179};
180
drhea678832008-12-10 19:26:22 +0000181/*
182** Compute a string length that is limited to what can be stored in
183** lower 30 bits of a 32-bit signed integer.
184*/
drh4f21c4a2008-12-10 22:15:00 +0000185static int strlen30(const char *z){
drhea678832008-12-10 19:26:22 +0000186 const char *z2 = z;
187 while( *z2 ){ z2++; }
188 return 0x3fffffff & (int)(z2 - z);
189}
drhea678832008-12-10 19:26:22 +0000190
191
danielk197732a0d8b2007-05-04 19:03:02 +0000192#ifndef SQLITE_OMIT_INCRBLOB
danielk1977b4e9af92007-05-01 17:49:49 +0000193/*
danielk1977d04417962007-05-02 13:16:30 +0000194** Close all incrblob channels opened using database connection pDb.
195** This is called when shutting down the database connection.
196*/
197static void closeIncrblobChannels(SqliteDb *pDb){
198 IncrblobChannel *p;
199 IncrblobChannel *pNext;
200
201 for(p=pDb->pIncrblob; p; p=pNext){
202 pNext = p->pNext;
203
mistachkinb56660f2016-07-14 21:26:09 +0000204 /* Note: Calling unregister here call Tcl_Close on the incrblob channel,
danielk1977d04417962007-05-02 13:16:30 +0000205 ** which deletes the IncrblobChannel structure at *p. So do not
206 ** call Tcl_Free() here.
207 */
208 Tcl_UnregisterChannel(pDb->interp, p->channel);
209 }
210}
211
212/*
danielk1977b4e9af92007-05-01 17:49:49 +0000213** Close an incremental blob channel.
214*/
mistachkin7617e4a2016-07-28 17:11:20 +0000215static int SQLITE_TCLAPI incrblobClose(
216 ClientData instanceData,
217 Tcl_Interp *interp
218){
danielk1977b4e9af92007-05-01 17:49:49 +0000219 IncrblobChannel *p = (IncrblobChannel *)instanceData;
danielk197792d4d7a2007-05-04 12:05:56 +0000220 int rc = sqlite3_blob_close(p->pBlob);
221 sqlite3 *db = p->pDb->db;
danielk1977d04417962007-05-02 13:16:30 +0000222
223 /* Remove the channel from the SqliteDb.pIncrblob list. */
224 if( p->pNext ){
225 p->pNext->pPrev = p->pPrev;
226 }
227 if( p->pPrev ){
228 p->pPrev->pNext = p->pNext;
229 }
230 if( p->pDb->pIncrblob==p ){
231 p->pDb->pIncrblob = p->pNext;
232 }
233
danielk197792d4d7a2007-05-04 12:05:56 +0000234 /* Free the IncrblobChannel structure */
danielk1977b4e9af92007-05-01 17:49:49 +0000235 Tcl_Free((char *)p);
danielk197792d4d7a2007-05-04 12:05:56 +0000236
237 if( rc!=SQLITE_OK ){
238 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
239 return TCL_ERROR;
240 }
danielk1977b4e9af92007-05-01 17:49:49 +0000241 return TCL_OK;
242}
243
244/*
245** Read data from an incremental blob channel.
246*/
mistachkin7617e4a2016-07-28 17:11:20 +0000247static int SQLITE_TCLAPI incrblobInput(
mistachkinb56660f2016-07-14 21:26:09 +0000248 ClientData instanceData,
249 char *buf,
danielk1977b4e9af92007-05-01 17:49:49 +0000250 int bufSize,
251 int *errorCodePtr
252){
253 IncrblobChannel *p = (IncrblobChannel *)instanceData;
254 int nRead = bufSize; /* Number of bytes to read */
255 int nBlob; /* Total size of the blob */
256 int rc; /* sqlite error code */
257
258 nBlob = sqlite3_blob_bytes(p->pBlob);
259 if( (p->iSeek+nRead)>nBlob ){
260 nRead = nBlob-p->iSeek;
261 }
262 if( nRead<=0 ){
263 return 0;
264 }
265
266 rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek);
267 if( rc!=SQLITE_OK ){
268 *errorCodePtr = rc;
269 return -1;
270 }
271
272 p->iSeek += nRead;
273 return nRead;
274}
275
danielk1977d04417962007-05-02 13:16:30 +0000276/*
277** Write data to an incremental blob channel.
278*/
mistachkin7617e4a2016-07-28 17:11:20 +0000279static int SQLITE_TCLAPI incrblobOutput(
mistachkinb56660f2016-07-14 21:26:09 +0000280 ClientData instanceData,
281 CONST char *buf,
danielk1977b4e9af92007-05-01 17:49:49 +0000282 int toWrite,
283 int *errorCodePtr
284){
285 IncrblobChannel *p = (IncrblobChannel *)instanceData;
286 int nWrite = toWrite; /* Number of bytes to write */
287 int nBlob; /* Total size of the blob */
288 int rc; /* sqlite error code */
289
290 nBlob = sqlite3_blob_bytes(p->pBlob);
291 if( (p->iSeek+nWrite)>nBlob ){
292 *errorCodePtr = EINVAL;
293 return -1;
294 }
295 if( nWrite<=0 ){
296 return 0;
297 }
298
299 rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek);
300 if( rc!=SQLITE_OK ){
301 *errorCodePtr = EIO;
302 return -1;
303 }
304
305 p->iSeek += nWrite;
306 return nWrite;
307}
308
309/*
310** Seek an incremental blob channel.
311*/
mistachkin7617e4a2016-07-28 17:11:20 +0000312static int SQLITE_TCLAPI incrblobSeek(
mistachkinb56660f2016-07-14 21:26:09 +0000313 ClientData instanceData,
danielk1977b4e9af92007-05-01 17:49:49 +0000314 long offset,
315 int seekMode,
316 int *errorCodePtr
317){
318 IncrblobChannel *p = (IncrblobChannel *)instanceData;
319
320 switch( seekMode ){
321 case SEEK_SET:
322 p->iSeek = offset;
323 break;
324 case SEEK_CUR:
325 p->iSeek += offset;
326 break;
327 case SEEK_END:
328 p->iSeek = sqlite3_blob_bytes(p->pBlob) + offset;
329 break;
330
331 default: assert(!"Bad seekMode");
332 }
333
334 return p->iSeek;
335}
336
337
mistachkin7617e4a2016-07-28 17:11:20 +0000338static void SQLITE_TCLAPI incrblobWatch(
339 ClientData instanceData,
340 int mode
341){
mistachkinb56660f2016-07-14 21:26:09 +0000342 /* NO-OP */
danielk1977b4e9af92007-05-01 17:49:49 +0000343}
mistachkin7617e4a2016-07-28 17:11:20 +0000344static int SQLITE_TCLAPI incrblobHandle(
345 ClientData instanceData,
346 int dir,
347 ClientData *hPtr
348){
danielk1977b4e9af92007-05-01 17:49:49 +0000349 return TCL_ERROR;
350}
351
352static Tcl_ChannelType IncrblobChannelType = {
353 "incrblob", /* typeName */
354 TCL_CHANNEL_VERSION_2, /* version */
355 incrblobClose, /* closeProc */
356 incrblobInput, /* inputProc */
357 incrblobOutput, /* outputProc */
358 incrblobSeek, /* seekProc */
359 0, /* setOptionProc */
360 0, /* getOptionProc */
361 incrblobWatch, /* watchProc (this is a no-op) */
362 incrblobHandle, /* getHandleProc (always returns error) */
363 0, /* close2Proc */
364 0, /* blockModeProc */
365 0, /* flushProc */
366 0, /* handlerProc */
367 0, /* wideSeekProc */
danielk1977b4e9af92007-05-01 17:49:49 +0000368};
369
370/*
371** Create a new incrblob channel.
372*/
373static int createIncrblobChannel(
mistachkinb56660f2016-07-14 21:26:09 +0000374 Tcl_Interp *interp,
375 SqliteDb *pDb,
danielk1977b4e9af92007-05-01 17:49:49 +0000376 const char *zDb,
mistachkinb56660f2016-07-14 21:26:09 +0000377 const char *zTable,
378 const char *zColumn,
danielk19778cbadb02007-05-03 16:31:26 +0000379 sqlite_int64 iRow,
380 int isReadonly
danielk1977b4e9af92007-05-01 17:49:49 +0000381){
382 IncrblobChannel *p;
danielk19778cbadb02007-05-03 16:31:26 +0000383 sqlite3 *db = pDb->db;
danielk1977b4e9af92007-05-01 17:49:49 +0000384 sqlite3_blob *pBlob;
385 int rc;
danielk19778cbadb02007-05-03 16:31:26 +0000386 int flags = TCL_READABLE|(isReadonly ? 0 : TCL_WRITABLE);
danielk1977b4e9af92007-05-01 17:49:49 +0000387
388 /* This variable is used to name the channels: "incrblob_[incr count]" */
389 static int count = 0;
390 char zChannel[64];
391
danielk19778cbadb02007-05-03 16:31:26 +0000392 rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob);
danielk1977b4e9af92007-05-01 17:49:49 +0000393 if( rc!=SQLITE_OK ){
394 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
395 return TCL_ERROR;
396 }
397
398 p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel));
399 p->iSeek = 0;
400 p->pBlob = pBlob;
401
drh5bb3eb92007-05-04 13:15:55 +0000402 sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count);
danielk1977d04417962007-05-02 13:16:30 +0000403 p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags);
404 Tcl_RegisterChannel(interp, p->channel);
danielk1977b4e9af92007-05-01 17:49:49 +0000405
danielk1977d04417962007-05-02 13:16:30 +0000406 /* Link the new channel into the SqliteDb.pIncrblob list. */
407 p->pNext = pDb->pIncrblob;
408 p->pPrev = 0;
409 if( p->pNext ){
410 p->pNext->pPrev = p;
411 }
412 pDb->pIncrblob = p;
413 p->pDb = pDb;
414
415 Tcl_SetResult(interp, (char *)Tcl_GetChannelName(p->channel), TCL_VOLATILE);
danielk1977b4e9af92007-05-01 17:49:49 +0000416 return TCL_OK;
417}
danielk197732a0d8b2007-05-04 19:03:02 +0000418#else /* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */
419 #define closeIncrblobChannels(pDb)
420#endif
danielk1977b4e9af92007-05-01 17:49:49 +0000421
drh6d313162000-09-21 13:01:35 +0000422/*
drhd1e47332005-06-26 17:55:33 +0000423** Look at the script prefix in pCmd. We will be executing this script
424** after first appending one or more arguments. This routine analyzes
425** the script to see if it is safe to use Tcl_EvalObjv() on the script
426** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much
427** faster.
428**
429** Scripts that are safe to use with Tcl_EvalObjv() consists of a
430** command name followed by zero or more arguments with no [...] or $
431** or {...} or ; to be seen anywhere. Most callback scripts consist
432** of just a single procedure name and they meet this requirement.
433*/
434static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){
435 /* We could try to do something with Tcl_Parse(). But we will instead
436 ** just do a search for forbidden characters. If any of the forbidden
437 ** characters appear in pCmd, we will report the string as unsafe.
438 */
439 const char *z;
440 int n;
441 z = Tcl_GetStringFromObj(pCmd, &n);
442 while( n-- > 0 ){
443 int c = *(z++);
444 if( c=='$' || c=='[' || c==';' ) return 0;
445 }
446 return 1;
447}
448
449/*
450** Find an SqlFunc structure with the given name. Or create a new
451** one if an existing one cannot be found. Return a pointer to the
452** structure.
453*/
454static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){
455 SqlFunc *p, *pNew;
drh0425f182013-11-26 16:48:04 +0000456 int nName = strlen30(zName);
457 pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + nName + 1 );
drhd1e47332005-06-26 17:55:33 +0000458 pNew->zName = (char*)&pNew[1];
drh0425f182013-11-26 16:48:04 +0000459 memcpy(pNew->zName, zName, nName+1);
mistachkinb56660f2016-07-14 21:26:09 +0000460 for(p=pDb->pFunc; p; p=p->pNext){
drh0425f182013-11-26 16:48:04 +0000461 if( sqlite3_stricmp(p->zName, pNew->zName)==0 ){
drhd1e47332005-06-26 17:55:33 +0000462 Tcl_Free((char*)pNew);
463 return p;
464 }
465 }
466 pNew->interp = pDb->interp;
drhc45e6712012-10-03 11:02:33 +0000467 pNew->pDb = pDb;
drhd1e47332005-06-26 17:55:33 +0000468 pNew->pScript = 0;
469 pNew->pNext = pDb->pFunc;
470 pDb->pFunc = pNew;
471 return pNew;
472}
473
474/*
danc431fd52011-06-27 16:55:50 +0000475** Free a single SqlPreparedStmt object.
476*/
477static void dbFreeStmt(SqlPreparedStmt *pStmt){
478#ifdef SQLITE_TEST
479 if( sqlite3_sql(pStmt->pStmt)==0 ){
480 Tcl_Free((char *)pStmt->zSql);
481 }
482#endif
483 sqlite3_finalize(pStmt->pStmt);
484 Tcl_Free((char *)pStmt);
485}
486
487/*
drhfb7e7652005-01-24 00:28:42 +0000488** Finalize and free a list of prepared statements
489*/
danc431fd52011-06-27 16:55:50 +0000490static void flushStmtCache(SqliteDb *pDb){
drhfb7e7652005-01-24 00:28:42 +0000491 SqlPreparedStmt *pPreStmt;
danc431fd52011-06-27 16:55:50 +0000492 SqlPreparedStmt *pNext;
drhfb7e7652005-01-24 00:28:42 +0000493
danc431fd52011-06-27 16:55:50 +0000494 for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pNext){
495 pNext = pPreStmt->pNext;
496 dbFreeStmt(pPreStmt);
drhfb7e7652005-01-24 00:28:42 +0000497 }
498 pDb->nStmt = 0;
499 pDb->stmtLast = 0;
danc431fd52011-06-27 16:55:50 +0000500 pDb->stmtList = 0;
drhfb7e7652005-01-24 00:28:42 +0000501}
502
503/*
drh895d7472004-08-20 16:02:39 +0000504** TCL calls this procedure when an sqlite3 database command is
505** deleted.
drh75897232000-05-29 14:26:00 +0000506*/
mistachkin7617e4a2016-07-28 17:11:20 +0000507static void SQLITE_TCLAPI DbDeleteCmd(void *db){
drhbec3f402000-08-04 13:49:02 +0000508 SqliteDb *pDb = (SqliteDb*)db;
drhfb7e7652005-01-24 00:28:42 +0000509 flushStmtCache(pDb);
danielk1977d04417962007-05-02 13:16:30 +0000510 closeIncrblobChannels(pDb);
danielk19776f8a5032004-05-10 10:34:51 +0000511 sqlite3_close(pDb->db);
drhcabb0812002-09-14 13:47:32 +0000512 while( pDb->pFunc ){
513 SqlFunc *pFunc = pDb->pFunc;
514 pDb->pFunc = pFunc->pNext;
drhc45e6712012-10-03 11:02:33 +0000515 assert( pFunc->pDb==pDb );
drhd1e47332005-06-26 17:55:33 +0000516 Tcl_DecrRefCount(pFunc->pScript);
drhcabb0812002-09-14 13:47:32 +0000517 Tcl_Free((char*)pFunc);
518 }
danielk19770202b292004-06-09 09:55:16 +0000519 while( pDb->pCollate ){
520 SqlCollate *pCollate = pDb->pCollate;
521 pDb->pCollate = pCollate->pNext;
522 Tcl_Free((char*)pCollate);
523 }
drhbec3f402000-08-04 13:49:02 +0000524 if( pDb->zBusy ){
525 Tcl_Free(pDb->zBusy);
526 }
drhb5a20d32003-04-23 12:25:23 +0000527 if( pDb->zTrace ){
528 Tcl_Free(pDb->zTrace);
drh0d1a6432003-04-03 15:46:04 +0000529 }
mistachkinb56660f2016-07-14 21:26:09 +0000530 if( pDb->zTraceV2 ){
531 Tcl_Free(pDb->zTraceV2);
532 }
drh19e2d372005-08-29 23:00:03 +0000533 if( pDb->zProfile ){
534 Tcl_Free(pDb->zProfile);
535 }
drhe22a3342003-04-22 20:30:37 +0000536 if( pDb->zAuth ){
537 Tcl_Free(pDb->zAuth);
538 }
danielk197755c45f22005-04-03 23:54:43 +0000539 if( pDb->zNull ){
540 Tcl_Free(pDb->zNull);
541 }
danielk197794eb6a12005-12-15 15:22:08 +0000542 if( pDb->pUpdateHook ){
543 Tcl_DecrRefCount(pDb->pUpdateHook);
544 }
dan46c47d42011-03-01 18:42:07 +0000545 if( pDb->pPreUpdateHook ){
546 Tcl_DecrRefCount(pDb->pPreUpdateHook);
547 }
danielk197771fd80b2005-12-16 06:54:01 +0000548 if( pDb->pRollbackHook ){
549 Tcl_DecrRefCount(pDb->pRollbackHook);
550 }
drh5def0842010-05-05 20:00:25 +0000551 if( pDb->pWalHook ){
552 Tcl_DecrRefCount(pDb->pWalHook);
dan8d22a172010-04-19 18:03:51 +0000553 }
danielk197794eb6a12005-12-15 15:22:08 +0000554 if( pDb->pCollateNeeded ){
555 Tcl_DecrRefCount(pDb->pCollateNeeded);
556 }
drhbec3f402000-08-04 13:49:02 +0000557 Tcl_Free((char*)pDb);
558}
559
560/*
561** This routine is called when a database file is locked while trying
562** to execute SQL.
563*/
danielk19772a764eb2004-06-12 01:43:26 +0000564static int DbBusyHandler(void *cd, int nTries){
drhbec3f402000-08-04 13:49:02 +0000565 SqliteDb *pDb = (SqliteDb*)cd;
566 int rc;
567 char zVal[30];
drhbec3f402000-08-04 13:49:02 +0000568
drh5bb3eb92007-05-04 13:15:55 +0000569 sqlite3_snprintf(sizeof(zVal), zVal, "%d", nTries);
drhd1e47332005-06-26 17:55:33 +0000570 rc = Tcl_VarEval(pDb->interp, pDb->zBusy, " ", zVal, (char*)0);
drhbec3f402000-08-04 13:49:02 +0000571 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
572 return 0;
573 }
574 return 1;
drh75897232000-05-29 14:26:00 +0000575}
576
drh26e4a8b2008-05-01 17:16:52 +0000577#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh75897232000-05-29 14:26:00 +0000578/*
danielk1977348bb5d2003-10-18 09:37:26 +0000579** This routine is invoked as the 'progress callback' for the database.
580*/
581static int DbProgressHandler(void *cd){
582 SqliteDb *pDb = (SqliteDb*)cd;
583 int rc;
584
585 assert( pDb->zProgress );
586 rc = Tcl_Eval(pDb->interp, pDb->zProgress);
587 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
588 return 1;
589 }
590 return 0;
591}
drh26e4a8b2008-05-01 17:16:52 +0000592#endif
danielk1977348bb5d2003-10-18 09:37:26 +0000593
drh2eb22af2016-09-10 19:51:40 +0000594#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \
595 !defined(SQLITE_OMIT_DEPRECATED)
danielk1977348bb5d2003-10-18 09:37:26 +0000596/*
drhb5a20d32003-04-23 12:25:23 +0000597** This routine is called by the SQLite trace handler whenever a new
598** block of SQL is executed. The TCL script in pDb->zTrace is executed.
drh0d1a6432003-04-03 15:46:04 +0000599*/
drhb5a20d32003-04-23 12:25:23 +0000600static void DbTraceHandler(void *cd, const char *zSql){
drh0d1a6432003-04-03 15:46:04 +0000601 SqliteDb *pDb = (SqliteDb*)cd;
drhb5a20d32003-04-23 12:25:23 +0000602 Tcl_DString str;
drh0d1a6432003-04-03 15:46:04 +0000603
drhb5a20d32003-04-23 12:25:23 +0000604 Tcl_DStringInit(&str);
605 Tcl_DStringAppend(&str, pDb->zTrace, -1);
606 Tcl_DStringAppendElement(&str, zSql);
607 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
608 Tcl_DStringFree(&str);
609 Tcl_ResetResult(pDb->interp);
drh0d1a6432003-04-03 15:46:04 +0000610}
drhd1167392006-01-23 13:00:35 +0000611#endif
drh0d1a6432003-04-03 15:46:04 +0000612
drhd1167392006-01-23 13:00:35 +0000613#ifndef SQLITE_OMIT_TRACE
drh0d1a6432003-04-03 15:46:04 +0000614/*
mistachkinb56660f2016-07-14 21:26:09 +0000615** This routine is called by the SQLite trace_v2 handler whenever a new
616** supported event is generated. Unsupported event types are ignored.
617** The TCL script in pDb->zTraceV2 is executed, with the arguments for
618** the event appended to it (as list elements).
619*/
620static int DbTraceV2Handler(
621 unsigned type, /* One of the SQLITE_TRACE_* event types. */
622 void *cd, /* The original context data pointer. */
623 void *pd, /* Primary event data, depends on event type. */
624 void *xd /* Extra event data, depends on event type. */
625){
626 SqliteDb *pDb = (SqliteDb*)cd;
627 Tcl_Obj *pCmd;
628
629 switch( type ){
630 case SQLITE_TRACE_STMT: {
631 sqlite3_stmt *pStmt = (sqlite3_stmt *)pd;
632 char *zSql = (char *)xd;
633
634 pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1);
635 Tcl_IncrRefCount(pCmd);
636 Tcl_ListObjAppendElement(pDb->interp, pCmd,
637 Tcl_NewWideIntObj((Tcl_WideInt)pStmt));
638 Tcl_ListObjAppendElement(pDb->interp, pCmd,
639 Tcl_NewStringObj(zSql, -1));
640 Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
641 Tcl_DecrRefCount(pCmd);
642 Tcl_ResetResult(pDb->interp);
643 break;
644 }
645 case SQLITE_TRACE_PROFILE: {
646 sqlite3_stmt *pStmt = (sqlite3_stmt *)pd;
647 sqlite3_int64 ns = (sqlite3_int64)xd;
648
649 pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1);
650 Tcl_IncrRefCount(pCmd);
651 Tcl_ListObjAppendElement(pDb->interp, pCmd,
652 Tcl_NewWideIntObj((Tcl_WideInt)pStmt));
653 Tcl_ListObjAppendElement(pDb->interp, pCmd,
654 Tcl_NewWideIntObj((Tcl_WideInt)ns));
655 Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
656 Tcl_DecrRefCount(pCmd);
657 Tcl_ResetResult(pDb->interp);
658 break;
659 }
660 case SQLITE_TRACE_ROW: {
661 sqlite3_stmt *pStmt = (sqlite3_stmt *)pd;
662
663 pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1);
664 Tcl_IncrRefCount(pCmd);
665 Tcl_ListObjAppendElement(pDb->interp, pCmd,
666 Tcl_NewWideIntObj((Tcl_WideInt)pStmt));
667 Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
668 Tcl_DecrRefCount(pCmd);
669 Tcl_ResetResult(pDb->interp);
670 break;
671 }
672 case SQLITE_TRACE_CLOSE: {
673 sqlite3 *db = (sqlite3 *)pd;
674
675 pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1);
676 Tcl_IncrRefCount(pCmd);
677 Tcl_ListObjAppendElement(pDb->interp, pCmd,
678 Tcl_NewWideIntObj((Tcl_WideInt)db));
679 Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
680 Tcl_DecrRefCount(pCmd);
681 Tcl_ResetResult(pDb->interp);
682 break;
683 }
684 }
685 return SQLITE_OK;
686}
687#endif
688
drh2eb22af2016-09-10 19:51:40 +0000689#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \
690 !defined(SQLITE_OMIT_DEPRECATED)
mistachkinb56660f2016-07-14 21:26:09 +0000691/*
drh19e2d372005-08-29 23:00:03 +0000692** This routine is called by the SQLite profile handler after a statement
693** SQL has executed. The TCL script in pDb->zProfile is evaluated.
694*/
695static void DbProfileHandler(void *cd, const char *zSql, sqlite_uint64 tm){
696 SqliteDb *pDb = (SqliteDb*)cd;
697 Tcl_DString str;
698 char zTm[100];
699
700 sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", tm);
701 Tcl_DStringInit(&str);
702 Tcl_DStringAppend(&str, pDb->zProfile, -1);
703 Tcl_DStringAppendElement(&str, zSql);
704 Tcl_DStringAppendElement(&str, zTm);
705 Tcl_Eval(pDb->interp, Tcl_DStringValue(&str));
706 Tcl_DStringFree(&str);
707 Tcl_ResetResult(pDb->interp);
708}
drhd1167392006-01-23 13:00:35 +0000709#endif
drh19e2d372005-08-29 23:00:03 +0000710
711/*
drhaa940ea2004-01-15 02:44:03 +0000712** This routine is called when a transaction is committed. The
713** TCL script in pDb->zCommit is executed. If it returns non-zero or
714** if it throws an exception, the transaction is rolled back instead
715** of being committed.
716*/
717static int DbCommitHandler(void *cd){
718 SqliteDb *pDb = (SqliteDb*)cd;
719 int rc;
720
721 rc = Tcl_Eval(pDb->interp, pDb->zCommit);
722 if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
723 return 1;
724 }
725 return 0;
726}
727
danielk197771fd80b2005-12-16 06:54:01 +0000728static void DbRollbackHandler(void *clientData){
729 SqliteDb *pDb = (SqliteDb*)clientData;
730 assert(pDb->pRollbackHook);
731 if( TCL_OK!=Tcl_EvalObjEx(pDb->interp, pDb->pRollbackHook, 0) ){
732 Tcl_BackgroundError(pDb->interp);
733 }
734}
735
drh5def0842010-05-05 20:00:25 +0000736/*
737** This procedure handles wal_hook callbacks.
738*/
739static int DbWalHandler(
mistachkinb56660f2016-07-14 21:26:09 +0000740 void *clientData,
741 sqlite3 *db,
742 const char *zDb,
dan8d22a172010-04-19 18:03:51 +0000743 int nEntry
744){
drh5def0842010-05-05 20:00:25 +0000745 int ret = SQLITE_OK;
dan8d22a172010-04-19 18:03:51 +0000746 Tcl_Obj *p;
747 SqliteDb *pDb = (SqliteDb*)clientData;
748 Tcl_Interp *interp = pDb->interp;
drh5def0842010-05-05 20:00:25 +0000749 assert(pDb->pWalHook);
dan8d22a172010-04-19 18:03:51 +0000750
dan6e45e0c2014-12-10 20:29:49 +0000751 assert( db==pDb->db );
drh5def0842010-05-05 20:00:25 +0000752 p = Tcl_DuplicateObj(pDb->pWalHook);
dan8d22a172010-04-19 18:03:51 +0000753 Tcl_IncrRefCount(p);
754 Tcl_ListObjAppendElement(interp, p, Tcl_NewStringObj(zDb, -1));
755 Tcl_ListObjAppendElement(interp, p, Tcl_NewIntObj(nEntry));
mistachkinb56660f2016-07-14 21:26:09 +0000756 if( TCL_OK!=Tcl_EvalObjEx(interp, p, 0)
dan8d22a172010-04-19 18:03:51 +0000757 || TCL_OK!=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &ret)
758 ){
759 Tcl_BackgroundError(interp);
760 }
761 Tcl_DecrRefCount(p);
762
763 return ret;
764}
765
drhbcf4f482009-03-27 12:44:35 +0000766#if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_UNLOCK_NOTIFY)
danielk1977404ca072009-03-16 13:19:36 +0000767static void setTestUnlockNotifyVars(Tcl_Interp *interp, int iArg, int nArg){
768 char zBuf[64];
drh65545b52015-01-19 00:35:53 +0000769 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", iArg);
danielk1977404ca072009-03-16 13:19:36 +0000770 Tcl_SetVar(interp, "sqlite_unlock_notify_arg", zBuf, TCL_GLOBAL_ONLY);
drh65545b52015-01-19 00:35:53 +0000771 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nArg);
danielk1977404ca072009-03-16 13:19:36 +0000772 Tcl_SetVar(interp, "sqlite_unlock_notify_argcount", zBuf, TCL_GLOBAL_ONLY);
773}
774#else
drhbcf4f482009-03-27 12:44:35 +0000775# define setTestUnlockNotifyVars(x,y,z)
danielk1977404ca072009-03-16 13:19:36 +0000776#endif
777
drh69910da2009-03-27 12:32:54 +0000778#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
danielk1977404ca072009-03-16 13:19:36 +0000779static void DbUnlockNotify(void **apArg, int nArg){
780 int i;
781 for(i=0; i<nArg; i++){
782 const int flags = (TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT);
783 SqliteDb *pDb = (SqliteDb *)apArg[i];
784 setTestUnlockNotifyVars(pDb->interp, i, nArg);
785 assert( pDb->pUnlockNotify);
786 Tcl_EvalObjEx(pDb->interp, pDb->pUnlockNotify, flags);
787 Tcl_DecrRefCount(pDb->pUnlockNotify);
788 pDb->pUnlockNotify = 0;
789 }
790}
drh69910da2009-03-27 12:32:54 +0000791#endif
danielk1977404ca072009-03-16 13:19:36 +0000792
drh9b1c62d2011-03-30 21:04:43 +0000793#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +0000794/*
795** Pre-update hook callback.
796*/
797static void DbPreUpdateHandler(
mistachkinb56660f2016-07-14 21:26:09 +0000798 void *p,
dan46c47d42011-03-01 18:42:07 +0000799 sqlite3 *db,
800 int op,
mistachkinb56660f2016-07-14 21:26:09 +0000801 const char *zDb,
802 const char *zTbl,
dan46c47d42011-03-01 18:42:07 +0000803 sqlite_int64 iKey1,
804 sqlite_int64 iKey2
805){
806 SqliteDb *pDb = (SqliteDb *)p;
807 Tcl_Obj *pCmd;
808 static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"};
809
810 assert( (SQLITE_DELETE-1)/9 == 0 );
811 assert( (SQLITE_INSERT-1)/9 == 1 );
812 assert( (SQLITE_UPDATE-1)/9 == 2 );
813 assert( pDb->pPreUpdateHook );
814 assert( db==pDb->db );
815 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
816
817 pCmd = Tcl_DuplicateObj(pDb->pPreUpdateHook);
818 Tcl_IncrRefCount(pCmd);
819 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1));
820 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1));
821 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1));
822 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey1));
823 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey2));
824 Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
825 Tcl_DecrRefCount(pCmd);
826}
drh9b1c62d2011-03-30 21:04:43 +0000827#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan46c47d42011-03-01 18:42:07 +0000828
danielk197794eb6a12005-12-15 15:22:08 +0000829static void DbUpdateHandler(
mistachkinb56660f2016-07-14 21:26:09 +0000830 void *p,
danielk197794eb6a12005-12-15 15:22:08 +0000831 int op,
mistachkinb56660f2016-07-14 21:26:09 +0000832 const char *zDb,
833 const char *zTbl,
danielk197794eb6a12005-12-15 15:22:08 +0000834 sqlite_int64 rowid
835){
836 SqliteDb *pDb = (SqliteDb *)p;
837 Tcl_Obj *pCmd;
dan46c47d42011-03-01 18:42:07 +0000838 static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"};
839
840 assert( (SQLITE_DELETE-1)/9 == 0 );
841 assert( (SQLITE_INSERT-1)/9 == 1 );
842 assert( (SQLITE_UPDATE-1)/9 == 2 );
danielk197794eb6a12005-12-15 15:22:08 +0000843
844 assert( pDb->pUpdateHook );
845 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
846
847 pCmd = Tcl_DuplicateObj(pDb->pUpdateHook);
848 Tcl_IncrRefCount(pCmd);
dan46c47d42011-03-01 18:42:07 +0000849 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1));
danielk197794eb6a12005-12-15 15:22:08 +0000850 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1));
851 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1));
852 Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid));
853 Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
drhefdde162010-10-27 15:36:21 +0000854 Tcl_DecrRefCount(pCmd);
danielk197794eb6a12005-12-15 15:22:08 +0000855}
856
danielk19777cedc8d2004-06-10 10:50:08 +0000857static void tclCollateNeeded(
858 void *pCtx,
drh9bb575f2004-09-06 17:24:11 +0000859 sqlite3 *db,
danielk19777cedc8d2004-06-10 10:50:08 +0000860 int enc,
861 const char *zName
862){
863 SqliteDb *pDb = (SqliteDb *)pCtx;
864 Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
865 Tcl_IncrRefCount(pScript);
866 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
867 Tcl_EvalObjEx(pDb->interp, pScript, 0);
868 Tcl_DecrRefCount(pScript);
869}
870
drhaa940ea2004-01-15 02:44:03 +0000871/*
danielk19770202b292004-06-09 09:55:16 +0000872** This routine is called to evaluate an SQL collation function implemented
873** using TCL script.
874*/
875static int tclSqlCollate(
876 void *pCtx,
877 int nA,
878 const void *zA,
879 int nB,
880 const void *zB
881){
882 SqlCollate *p = (SqlCollate *)pCtx;
883 Tcl_Obj *pCmd;
884
885 pCmd = Tcl_NewStringObj(p->zScript, -1);
886 Tcl_IncrRefCount(pCmd);
887 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zA, nA));
888 Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj(zB, nB));
drhd1e47332005-06-26 17:55:33 +0000889 Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
danielk19770202b292004-06-09 09:55:16 +0000890 Tcl_DecrRefCount(pCmd);
891 return (atoi(Tcl_GetStringResult(p->interp)));
892}
893
894/*
drhcabb0812002-09-14 13:47:32 +0000895** This routine is called to evaluate an SQL function implemented
896** using TCL script.
897*/
drhfb7e7652005-01-24 00:28:42 +0000898static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){
danielk19776f8a5032004-05-10 10:34:51 +0000899 SqlFunc *p = sqlite3_user_data(context);
drhd1e47332005-06-26 17:55:33 +0000900 Tcl_Obj *pCmd;
drhcabb0812002-09-14 13:47:32 +0000901 int i;
902 int rc;
903
drhd1e47332005-06-26 17:55:33 +0000904 if( argc==0 ){
905 /* If there are no arguments to the function, call Tcl_EvalObjEx on the
906 ** script object directly. This allows the TCL compiler to generate
907 ** bytecode for the command on the first invocation and thus make
908 ** subsequent invocations much faster. */
909 pCmd = p->pScript;
910 Tcl_IncrRefCount(pCmd);
911 rc = Tcl_EvalObjEx(p->interp, pCmd, 0);
912 Tcl_DecrRefCount(pCmd);
913 }else{
914 /* If there are arguments to the function, make a shallow copy of the
915 ** script object, lappend the arguments, then evaluate the copy.
916 **
peter.d.reid60ec9142014-09-06 16:39:46 +0000917 ** By "shallow" copy, we mean only the outer list Tcl_Obj is duplicated.
mistachkinb56660f2016-07-14 21:26:09 +0000918 ** The new Tcl_Obj contains pointers to the original list elements.
drhd1e47332005-06-26 17:55:33 +0000919 ** That way, when Tcl_EvalObjv() is run and shimmers the first element
920 ** of the list to tclCmdNameType, that alternate representation will
921 ** be preserved and reused on the next invocation.
922 */
923 Tcl_Obj **aArg;
924 int nArg;
925 if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){
mistachkinb56660f2016-07-14 21:26:09 +0000926 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhd1e47332005-06-26 17:55:33 +0000927 return;
mistachkinb56660f2016-07-14 21:26:09 +0000928 }
drhd1e47332005-06-26 17:55:33 +0000929 pCmd = Tcl_NewListObj(nArg, aArg);
930 Tcl_IncrRefCount(pCmd);
931 for(i=0; i<argc; i++){
932 sqlite3_value *pIn = argv[i];
933 Tcl_Obj *pVal;
mistachkinb56660f2016-07-14 21:26:09 +0000934
drhd1e47332005-06-26 17:55:33 +0000935 /* Set pVal to contain the i'th column of this row. */
936 switch( sqlite3_value_type(pIn) ){
937 case SQLITE_BLOB: {
938 int bytes = sqlite3_value_bytes(pIn);
939 pVal = Tcl_NewByteArrayObj(sqlite3_value_blob(pIn), bytes);
940 break;
941 }
942 case SQLITE_INTEGER: {
943 sqlite_int64 v = sqlite3_value_int64(pIn);
944 if( v>=-2147483647 && v<=2147483647 ){
drh7fd33922011-06-20 19:00:30 +0000945 pVal = Tcl_NewIntObj((int)v);
drhd1e47332005-06-26 17:55:33 +0000946 }else{
947 pVal = Tcl_NewWideIntObj(v);
948 }
949 break;
950 }
951 case SQLITE_FLOAT: {
952 double r = sqlite3_value_double(pIn);
953 pVal = Tcl_NewDoubleObj(r);
954 break;
955 }
956 case SQLITE_NULL: {
drhc45e6712012-10-03 11:02:33 +0000957 pVal = Tcl_NewStringObj(p->pDb->zNull, -1);
drhd1e47332005-06-26 17:55:33 +0000958 break;
959 }
960 default: {
961 int bytes = sqlite3_value_bytes(pIn);
danielk197700fd9572005-12-07 06:27:43 +0000962 pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes);
drhd1e47332005-06-26 17:55:33 +0000963 break;
964 }
965 }
966 rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal);
967 if( rc ){
968 Tcl_DecrRefCount(pCmd);
mistachkinb56660f2016-07-14 21:26:09 +0000969 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhd1e47332005-06-26 17:55:33 +0000970 return;
971 }
danielk197751ad0ec2004-05-24 12:39:02 +0000972 }
drhd1e47332005-06-26 17:55:33 +0000973 if( !p->useEvalObjv ){
974 /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd
975 ** is a list without a string representation. To prevent this from
976 ** happening, make sure pCmd has a valid string representation */
977 Tcl_GetString(pCmd);
978 }
979 rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
980 Tcl_DecrRefCount(pCmd);
drhcabb0812002-09-14 13:47:32 +0000981 }
danielk1977562e8d32005-05-20 09:40:55 +0000982
drhc7f269d2005-05-05 10:30:29 +0000983 if( rc && rc!=TCL_RETURN ){
mistachkinb56660f2016-07-14 21:26:09 +0000984 sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
drhcabb0812002-09-14 13:47:32 +0000985 }else{
drhc7f269d2005-05-05 10:30:29 +0000986 Tcl_Obj *pVar = Tcl_GetObjResult(p->interp);
987 int n;
988 u8 *data;
dan4a4c11a2009-10-06 14:59:02 +0000989 const char *zType = (pVar->typePtr ? pVar->typePtr->name : "");
drhc7f269d2005-05-05 10:30:29 +0000990 char c = zType[0];
drhdf0bdda2005-06-25 19:31:48 +0000991 if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){
drhd1e47332005-06-26 17:55:33 +0000992 /* Only return a BLOB type if the Tcl variable is a bytearray and
drhdf0bdda2005-06-25 19:31:48 +0000993 ** has no string representation. */
drhc7f269d2005-05-05 10:30:29 +0000994 data = Tcl_GetByteArrayFromObj(pVar, &n);
995 sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT);
drh985e0c62007-06-26 22:55:37 +0000996 }else if( c=='b' && strcmp(zType,"boolean")==0 ){
drhc7f269d2005-05-05 10:30:29 +0000997 Tcl_GetIntFromObj(0, pVar, &n);
998 sqlite3_result_int(context, n);
999 }else if( c=='d' && strcmp(zType,"double")==0 ){
1000 double r;
1001 Tcl_GetDoubleFromObj(0, pVar, &r);
1002 sqlite3_result_double(context, r);
drh985e0c62007-06-26 22:55:37 +00001003 }else if( (c=='w' && strcmp(zType,"wideInt")==0) ||
1004 (c=='i' && strcmp(zType,"int")==0) ){
drhdf0bdda2005-06-25 19:31:48 +00001005 Tcl_WideInt v;
1006 Tcl_GetWideIntFromObj(0, pVar, &v);
1007 sqlite3_result_int64(context, v);
drhc7f269d2005-05-05 10:30:29 +00001008 }else{
danielk197700fd9572005-12-07 06:27:43 +00001009 data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n);
1010 sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT);
drhc7f269d2005-05-05 10:30:29 +00001011 }
drhcabb0812002-09-14 13:47:32 +00001012 }
1013}
drh895d7472004-08-20 16:02:39 +00001014
drhe22a3342003-04-22 20:30:37 +00001015#ifndef SQLITE_OMIT_AUTHORIZATION
1016/*
1017** This is the authentication function. It appends the authentication
1018** type code and the two arguments to zCmd[] then invokes the result
1019** on the interpreter. The reply is examined to determine if the
1020** authentication fails or succeeds.
1021*/
1022static int auth_callback(
1023 void *pArg,
1024 int code,
1025 const char *zArg1,
1026 const char *zArg2,
1027 const char *zArg3,
1028 const char *zArg4
drh32c6a482014-09-11 13:44:52 +00001029#ifdef SQLITE_USER_AUTHENTICATION
1030 ,const char *zArg5
1031#endif
drhe22a3342003-04-22 20:30:37 +00001032){
mistachkin6ef5e122014-01-24 17:03:55 +00001033 const char *zCode;
drhe22a3342003-04-22 20:30:37 +00001034 Tcl_DString str;
1035 int rc;
1036 const char *zReply;
drh94189212017-05-11 13:43:57 +00001037 /* EVIDENCE-OF: R-38590-62769 The first parameter to the authorizer
1038 ** callback is a copy of the third parameter to the
1039 ** sqlite3_set_authorizer() interface.
1040 */
drhe22a3342003-04-22 20:30:37 +00001041 SqliteDb *pDb = (SqliteDb*)pArg;
drh1f1549f2008-08-26 21:33:34 +00001042 if( pDb->disableAuth ) return SQLITE_OK;
drhe22a3342003-04-22 20:30:37 +00001043
drh94189212017-05-11 13:43:57 +00001044 /* EVIDENCE-OF: R-56518-44310 The second parameter to the callback is an
1045 ** integer action code that specifies the particular action to be
1046 ** authorized. */
drhe22a3342003-04-22 20:30:37 +00001047 switch( code ){
1048 case SQLITE_COPY : zCode="SQLITE_COPY"; break;
1049 case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break;
1050 case SQLITE_CREATE_TABLE : zCode="SQLITE_CREATE_TABLE"; break;
1051 case SQLITE_CREATE_TEMP_INDEX : zCode="SQLITE_CREATE_TEMP_INDEX"; break;
1052 case SQLITE_CREATE_TEMP_TABLE : zCode="SQLITE_CREATE_TEMP_TABLE"; break;
1053 case SQLITE_CREATE_TEMP_TRIGGER: zCode="SQLITE_CREATE_TEMP_TRIGGER"; break;
1054 case SQLITE_CREATE_TEMP_VIEW : zCode="SQLITE_CREATE_TEMP_VIEW"; break;
1055 case SQLITE_CREATE_TRIGGER : zCode="SQLITE_CREATE_TRIGGER"; break;
1056 case SQLITE_CREATE_VIEW : zCode="SQLITE_CREATE_VIEW"; break;
1057 case SQLITE_DELETE : zCode="SQLITE_DELETE"; break;
1058 case SQLITE_DROP_INDEX : zCode="SQLITE_DROP_INDEX"; break;
1059 case SQLITE_DROP_TABLE : zCode="SQLITE_DROP_TABLE"; break;
1060 case SQLITE_DROP_TEMP_INDEX : zCode="SQLITE_DROP_TEMP_INDEX"; break;
1061 case SQLITE_DROP_TEMP_TABLE : zCode="SQLITE_DROP_TEMP_TABLE"; break;
1062 case SQLITE_DROP_TEMP_TRIGGER : zCode="SQLITE_DROP_TEMP_TRIGGER"; break;
1063 case SQLITE_DROP_TEMP_VIEW : zCode="SQLITE_DROP_TEMP_VIEW"; break;
1064 case SQLITE_DROP_TRIGGER : zCode="SQLITE_DROP_TRIGGER"; break;
1065 case SQLITE_DROP_VIEW : zCode="SQLITE_DROP_VIEW"; break;
1066 case SQLITE_INSERT : zCode="SQLITE_INSERT"; break;
1067 case SQLITE_PRAGMA : zCode="SQLITE_PRAGMA"; break;
1068 case SQLITE_READ : zCode="SQLITE_READ"; break;
1069 case SQLITE_SELECT : zCode="SQLITE_SELECT"; break;
1070 case SQLITE_TRANSACTION : zCode="SQLITE_TRANSACTION"; break;
1071 case SQLITE_UPDATE : zCode="SQLITE_UPDATE"; break;
drh81e293b2003-06-06 19:00:42 +00001072 case SQLITE_ATTACH : zCode="SQLITE_ATTACH"; break;
1073 case SQLITE_DETACH : zCode="SQLITE_DETACH"; break;
danielk19771c8c23c2004-11-12 15:53:37 +00001074 case SQLITE_ALTER_TABLE : zCode="SQLITE_ALTER_TABLE"; break;
danielk19771d54df82004-11-23 15:41:16 +00001075 case SQLITE_REINDEX : zCode="SQLITE_REINDEX"; break;
drhe6e04962005-07-23 02:17:03 +00001076 case SQLITE_ANALYZE : zCode="SQLITE_ANALYZE"; break;
danielk1977f1a381e2006-06-16 08:01:02 +00001077 case SQLITE_CREATE_VTABLE : zCode="SQLITE_CREATE_VTABLE"; break;
1078 case SQLITE_DROP_VTABLE : zCode="SQLITE_DROP_VTABLE"; break;
drh5169bbc2006-08-24 14:59:45 +00001079 case SQLITE_FUNCTION : zCode="SQLITE_FUNCTION"; break;
danielk1977ab9b7032008-12-30 06:24:58 +00001080 case SQLITE_SAVEPOINT : zCode="SQLITE_SAVEPOINT"; break;
drh65a2aaa2014-01-16 22:40:02 +00001081 case SQLITE_RECURSIVE : zCode="SQLITE_RECURSIVE"; break;
drhe22a3342003-04-22 20:30:37 +00001082 default : zCode="????"; break;
1083 }
1084 Tcl_DStringInit(&str);
1085 Tcl_DStringAppend(&str, pDb->zAuth, -1);
1086 Tcl_DStringAppendElement(&str, zCode);
1087 Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
1088 Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
1089 Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
1090 Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
drh32c6a482014-09-11 13:44:52 +00001091#ifdef SQLITE_USER_AUTHENTICATION
1092 Tcl_DStringAppendElement(&str, zArg5 ? zArg5 : "");
mistachkinb56660f2016-07-14 21:26:09 +00001093#endif
drhe22a3342003-04-22 20:30:37 +00001094 rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
1095 Tcl_DStringFree(&str);
drhb07028f2011-10-14 21:49:18 +00001096 zReply = rc==TCL_OK ? Tcl_GetStringResult(pDb->interp) : "SQLITE_DENY";
drhe22a3342003-04-22 20:30:37 +00001097 if( strcmp(zReply,"SQLITE_OK")==0 ){
1098 rc = SQLITE_OK;
1099 }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
1100 rc = SQLITE_DENY;
1101 }else if( strcmp(zReply,"SQLITE_IGNORE")==0 ){
1102 rc = SQLITE_IGNORE;
1103 }else{
1104 rc = 999;
1105 }
1106 return rc;
1107}
1108#endif /* SQLITE_OMIT_AUTHORIZATION */
drhcabb0812002-09-14 13:47:32 +00001109
1110/*
tpoindex1067fe12004-12-17 15:41:11 +00001111** This routine reads a line of text from FILE in, stores
1112** the text in memory obtained from malloc() and returns a pointer
1113** to the text. NULL is returned at end of file, or if malloc()
1114** fails.
1115**
1116** The interface is like "readline" but no command-line editing
1117** is done.
1118**
1119** copied from shell.c from '.import' command
1120*/
1121static char *local_getline(char *zPrompt, FILE *in){
1122 char *zLine;
1123 int nLine;
1124 int n;
tpoindex1067fe12004-12-17 15:41:11 +00001125
1126 nLine = 100;
1127 zLine = malloc( nLine );
1128 if( zLine==0 ) return 0;
1129 n = 0;
drhb07028f2011-10-14 21:49:18 +00001130 while( 1 ){
tpoindex1067fe12004-12-17 15:41:11 +00001131 if( n+100>nLine ){
1132 nLine = nLine*2 + 100;
1133 zLine = realloc(zLine, nLine);
1134 if( zLine==0 ) return 0;
1135 }
1136 if( fgets(&zLine[n], nLine - n, in)==0 ){
1137 if( n==0 ){
1138 free(zLine);
1139 return 0;
1140 }
1141 zLine[n] = 0;
tpoindex1067fe12004-12-17 15:41:11 +00001142 break;
1143 }
1144 while( zLine[n] ){ n++; }
1145 if( n>0 && zLine[n-1]=='\n' ){
1146 n--;
1147 zLine[n] = 0;
drhb07028f2011-10-14 21:49:18 +00001148 break;
tpoindex1067fe12004-12-17 15:41:11 +00001149 }
1150 }
1151 zLine = realloc( zLine, n+1 );
1152 return zLine;
1153}
1154
danielk19778e556522007-11-13 10:30:24 +00001155
1156/*
dan4a4c11a2009-10-06 14:59:02 +00001157** This function is part of the implementation of the command:
danielk19778e556522007-11-13 10:30:24 +00001158**
dan4a4c11a2009-10-06 14:59:02 +00001159** $db transaction [-deferred|-immediate|-exclusive] SCRIPT
danielk19778e556522007-11-13 10:30:24 +00001160**
dan4a4c11a2009-10-06 14:59:02 +00001161** It is invoked after evaluating the script SCRIPT to commit or rollback
1162** the transaction or savepoint opened by the [transaction] command.
1163*/
mistachkina121cc72016-07-28 18:06:52 +00001164static int SQLITE_TCLAPI DbTransPostCmd(
dan4a4c11a2009-10-06 14:59:02 +00001165 ClientData data[], /* data[0] is the Sqlite3Db* for $db */
1166 Tcl_Interp *interp, /* Tcl interpreter */
1167 int result /* Result of evaluating SCRIPT */
1168){
mistachkin6ef5e122014-01-24 17:03:55 +00001169 static const char *const azEnd[] = {
dan4a4c11a2009-10-06 14:59:02 +00001170 "RELEASE _tcl_transaction", /* rc==TCL_ERROR, nTransaction!=0 */
1171 "COMMIT", /* rc!=TCL_ERROR, nTransaction==0 */
1172 "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction",
1173 "ROLLBACK" /* rc==TCL_ERROR, nTransaction==0 */
1174 };
1175 SqliteDb *pDb = (SqliteDb*)data[0];
1176 int rc = result;
1177 const char *zEnd;
1178
1179 pDb->nTransaction--;
1180 zEnd = azEnd[(rc==TCL_ERROR)*2 + (pDb->nTransaction==0)];
1181
1182 pDb->disableAuth++;
1183 if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){
1184 /* This is a tricky scenario to handle. The most likely cause of an
mistachkinb56660f2016-07-14 21:26:09 +00001185 ** error is that the exec() above was an attempt to commit the
dan4a4c11a2009-10-06 14:59:02 +00001186 ** top-level transaction that returned SQLITE_BUSY. Or, less likely,
mistachkin48864df2013-03-21 21:20:32 +00001187 ** that an IO-error has occurred. In either case, throw a Tcl exception
dan4a4c11a2009-10-06 14:59:02 +00001188 ** and try to rollback the transaction.
1189 **
mistachkinb56660f2016-07-14 21:26:09 +00001190 ** But it could also be that the user executed one or more BEGIN,
dan4a4c11a2009-10-06 14:59:02 +00001191 ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing
1192 ** this method's logic. Not clear how this would be best handled.
1193 */
1194 if( rc!=TCL_ERROR ){
drha198f2b2014-02-07 19:26:13 +00001195 Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0);
dan4a4c11a2009-10-06 14:59:02 +00001196 rc = TCL_ERROR;
1197 }
1198 sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0);
1199 }
1200 pDb->disableAuth--;
1201
1202 return rc;
1203}
1204
1205/*
danc431fd52011-06-27 16:55:50 +00001206** Unless SQLITE_TEST is defined, this function is a simple wrapper around
1207** sqlite3_prepare_v2(). If SQLITE_TEST is defined, then it uses either
1208** sqlite3_prepare_v2() or legacy interface sqlite3_prepare(), depending
mistachkinb56660f2016-07-14 21:26:09 +00001209** on whether or not the [db_use_legacy_prepare] command has been used to
danc431fd52011-06-27 16:55:50 +00001210** configure the connection.
1211*/
1212static int dbPrepare(
1213 SqliteDb *pDb, /* Database object */
1214 const char *zSql, /* SQL to compile */
1215 sqlite3_stmt **ppStmt, /* OUT: Prepared statement */
1216 const char **pzOut /* OUT: Pointer to next SQL statement */
1217){
1218#ifdef SQLITE_TEST
1219 if( pDb->bLegacyPrepare ){
1220 return sqlite3_prepare(pDb->db, zSql, -1, ppStmt, pzOut);
1221 }
1222#endif
1223 return sqlite3_prepare_v2(pDb->db, zSql, -1, ppStmt, pzOut);
1224}
1225
1226/*
dan4a4c11a2009-10-06 14:59:02 +00001227** Search the cache for a prepared-statement object that implements the
1228** first SQL statement in the buffer pointed to by parameter zIn. If
1229** no such prepared-statement can be found, allocate and prepare a new
1230** one. In either case, bind the current values of the relevant Tcl
1231** variables to any $var, :var or @var variables in the statement. Before
1232** returning, set *ppPreStmt to point to the prepared-statement object.
1233**
1234** Output parameter *pzOut is set to point to the next SQL statement in
1235** buffer zIn, or to the '\0' byte at the end of zIn if there is no
1236** next statement.
1237**
1238** If successful, TCL_OK is returned. Otherwise, TCL_ERROR is returned
1239** and an error message loaded into interpreter pDb->interp.
1240*/
1241static int dbPrepareAndBind(
1242 SqliteDb *pDb, /* Database object */
1243 char const *zIn, /* SQL to compile */
1244 char const **pzOut, /* OUT: Pointer to next SQL statement */
1245 SqlPreparedStmt **ppPreStmt /* OUT: Object used to cache statement */
1246){
1247 const char *zSql = zIn; /* Pointer to first SQL statement in zIn */
mistachkin7bb22ac2015-01-12 19:59:12 +00001248 sqlite3_stmt *pStmt = 0; /* Prepared statement object */
dan4a4c11a2009-10-06 14:59:02 +00001249 SqlPreparedStmt *pPreStmt; /* Pointer to cached statement */
1250 int nSql; /* Length of zSql in bytes */
mistachkin7bb22ac2015-01-12 19:59:12 +00001251 int nVar = 0; /* Number of variables in statement */
dan4a4c11a2009-10-06 14:59:02 +00001252 int iParm = 0; /* Next free entry in apParm */
drh0425f182013-11-26 16:48:04 +00001253 char c;
dan4a4c11a2009-10-06 14:59:02 +00001254 int i;
1255 Tcl_Interp *interp = pDb->interp;
1256
1257 *ppPreStmt = 0;
1258
1259 /* Trim spaces from the start of zSql and calculate the remaining length. */
drh0425f182013-11-26 16:48:04 +00001260 while( (c = zSql[0])==' ' || c=='\t' || c=='\r' || c=='\n' ){ zSql++; }
dan4a4c11a2009-10-06 14:59:02 +00001261 nSql = strlen30(zSql);
1262
1263 for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){
1264 int n = pPreStmt->nSql;
mistachkinb56660f2016-07-14 21:26:09 +00001265 if( nSql>=n
dan4a4c11a2009-10-06 14:59:02 +00001266 && memcmp(pPreStmt->zSql, zSql, n)==0
1267 && (zSql[n]==0 || zSql[n-1]==';')
1268 ){
1269 pStmt = pPreStmt->pStmt;
1270 *pzOut = &zSql[pPreStmt->nSql];
1271
1272 /* When a prepared statement is found, unlink it from the
1273 ** cache list. It will later be added back to the beginning
1274 ** of the cache list in order to implement LRU replacement.
1275 */
1276 if( pPreStmt->pPrev ){
1277 pPreStmt->pPrev->pNext = pPreStmt->pNext;
1278 }else{
1279 pDb->stmtList = pPreStmt->pNext;
1280 }
1281 if( pPreStmt->pNext ){
1282 pPreStmt->pNext->pPrev = pPreStmt->pPrev;
1283 }else{
1284 pDb->stmtLast = pPreStmt->pPrev;
1285 }
1286 pDb->nStmt--;
1287 nVar = sqlite3_bind_parameter_count(pStmt);
1288 break;
1289 }
1290 }
mistachkinb56660f2016-07-14 21:26:09 +00001291
dan4a4c11a2009-10-06 14:59:02 +00001292 /* If no prepared statement was found. Compile the SQL text. Also allocate
1293 ** a new SqlPreparedStmt structure. */
1294 if( pPreStmt==0 ){
1295 int nByte;
1296
danc431fd52011-06-27 16:55:50 +00001297 if( SQLITE_OK!=dbPrepare(pDb, zSql, &pStmt, pzOut) ){
drhc45e6712012-10-03 11:02:33 +00001298 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1));
dan4a4c11a2009-10-06 14:59:02 +00001299 return TCL_ERROR;
1300 }
1301 if( pStmt==0 ){
1302 if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
1303 /* A compile-time error in the statement. */
drhc45e6712012-10-03 11:02:33 +00001304 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1));
dan4a4c11a2009-10-06 14:59:02 +00001305 return TCL_ERROR;
1306 }else{
1307 /* The statement was a no-op. Continue to the next statement
1308 ** in the SQL string.
1309 */
1310 return TCL_OK;
1311 }
1312 }
1313
1314 assert( pPreStmt==0 );
1315 nVar = sqlite3_bind_parameter_count(pStmt);
1316 nByte = sizeof(SqlPreparedStmt) + nVar*sizeof(Tcl_Obj *);
1317 pPreStmt = (SqlPreparedStmt*)Tcl_Alloc(nByte);
1318 memset(pPreStmt, 0, nByte);
1319
1320 pPreStmt->pStmt = pStmt;
drh7ed243b2012-04-19 17:19:51 +00001321 pPreStmt->nSql = (int)(*pzOut - zSql);
dan4a4c11a2009-10-06 14:59:02 +00001322 pPreStmt->zSql = sqlite3_sql(pStmt);
1323 pPreStmt->apParm = (Tcl_Obj **)&pPreStmt[1];
danc431fd52011-06-27 16:55:50 +00001324#ifdef SQLITE_TEST
1325 if( pPreStmt->zSql==0 ){
1326 char *zCopy = Tcl_Alloc(pPreStmt->nSql + 1);
1327 memcpy(zCopy, zSql, pPreStmt->nSql);
1328 zCopy[pPreStmt->nSql] = '\0';
1329 pPreStmt->zSql = zCopy;
1330 }
1331#endif
dan4a4c11a2009-10-06 14:59:02 +00001332 }
1333 assert( pPreStmt );
1334 assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql );
1335 assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) );
1336
mistachkinb56660f2016-07-14 21:26:09 +00001337 /* Bind values to parameters that begin with $ or : */
dan4a4c11a2009-10-06 14:59:02 +00001338 for(i=1; i<=nVar; i++){
1339 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
1340 if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){
1341 Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0);
1342 if( pVar ){
1343 int n;
1344 u8 *data;
1345 const char *zType = (pVar->typePtr ? pVar->typePtr->name : "");
mistachkin8e189222015-04-19 21:43:16 +00001346 c = zType[0];
dan4a4c11a2009-10-06 14:59:02 +00001347 if( zVar[0]=='@' ||
1348 (c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0) ){
1349 /* Load a BLOB type if the Tcl variable is a bytearray and
1350 ** it has no string representation or the host
1351 ** parameter name begins with "@". */
1352 data = Tcl_GetByteArrayFromObj(pVar, &n);
1353 sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC);
1354 Tcl_IncrRefCount(pVar);
1355 pPreStmt->apParm[iParm++] = pVar;
1356 }else if( c=='b' && strcmp(zType,"boolean")==0 ){
1357 Tcl_GetIntFromObj(interp, pVar, &n);
1358 sqlite3_bind_int(pStmt, i, n);
1359 }else if( c=='d' && strcmp(zType,"double")==0 ){
1360 double r;
1361 Tcl_GetDoubleFromObj(interp, pVar, &r);
1362 sqlite3_bind_double(pStmt, i, r);
1363 }else if( (c=='w' && strcmp(zType,"wideInt")==0) ||
1364 (c=='i' && strcmp(zType,"int")==0) ){
1365 Tcl_WideInt v;
1366 Tcl_GetWideIntFromObj(interp, pVar, &v);
1367 sqlite3_bind_int64(pStmt, i, v);
1368 }else{
1369 data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n);
1370 sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC);
1371 Tcl_IncrRefCount(pVar);
1372 pPreStmt->apParm[iParm++] = pVar;
1373 }
1374 }else{
1375 sqlite3_bind_null(pStmt, i);
1376 }
1377 }
1378 }
1379 pPreStmt->nParm = iParm;
1380 *ppPreStmt = pPreStmt;
dan937d0de2009-10-15 18:35:38 +00001381
dan4a4c11a2009-10-06 14:59:02 +00001382 return TCL_OK;
1383}
1384
dan4a4c11a2009-10-06 14:59:02 +00001385/*
1386** Release a statement reference obtained by calling dbPrepareAndBind().
1387** There should be exactly one call to this function for each call to
1388** dbPrepareAndBind().
1389**
1390** If the discard parameter is non-zero, then the statement is deleted
1391** immediately. Otherwise it is added to the LRU list and may be returned
1392** by a subsequent call to dbPrepareAndBind().
1393*/
1394static void dbReleaseStmt(
1395 SqliteDb *pDb, /* Database handle */
1396 SqlPreparedStmt *pPreStmt, /* Prepared statement handle to release */
1397 int discard /* True to delete (not cache) the pPreStmt */
1398){
1399 int i;
1400
1401 /* Free the bound string and blob parameters */
1402 for(i=0; i<pPreStmt->nParm; i++){
1403 Tcl_DecrRefCount(pPreStmt->apParm[i]);
1404 }
1405 pPreStmt->nParm = 0;
1406
1407 if( pDb->maxStmt<=0 || discard ){
1408 /* If the cache is turned off, deallocated the statement */
danc431fd52011-06-27 16:55:50 +00001409 dbFreeStmt(pPreStmt);
dan4a4c11a2009-10-06 14:59:02 +00001410 }else{
1411 /* Add the prepared statement to the beginning of the cache list. */
1412 pPreStmt->pNext = pDb->stmtList;
1413 pPreStmt->pPrev = 0;
1414 if( pDb->stmtList ){
1415 pDb->stmtList->pPrev = pPreStmt;
1416 }
1417 pDb->stmtList = pPreStmt;
1418 if( pDb->stmtLast==0 ){
1419 assert( pDb->nStmt==0 );
1420 pDb->stmtLast = pPreStmt;
1421 }else{
1422 assert( pDb->nStmt>0 );
1423 }
1424 pDb->nStmt++;
mistachkinb56660f2016-07-14 21:26:09 +00001425
1426 /* If we have too many statement in cache, remove the surplus from
dan4a4c11a2009-10-06 14:59:02 +00001427 ** the end of the cache list. */
1428 while( pDb->nStmt>pDb->maxStmt ){
danc431fd52011-06-27 16:55:50 +00001429 SqlPreparedStmt *pLast = pDb->stmtLast;
1430 pDb->stmtLast = pLast->pPrev;
dan4a4c11a2009-10-06 14:59:02 +00001431 pDb->stmtLast->pNext = 0;
1432 pDb->nStmt--;
danc431fd52011-06-27 16:55:50 +00001433 dbFreeStmt(pLast);
dan4a4c11a2009-10-06 14:59:02 +00001434 }
1435 }
1436}
1437
1438/*
1439** Structure used with dbEvalXXX() functions:
1440**
1441** dbEvalInit()
1442** dbEvalStep()
1443** dbEvalFinalize()
1444** dbEvalRowInfo()
1445** dbEvalColumnValue()
1446*/
1447typedef struct DbEvalContext DbEvalContext;
1448struct DbEvalContext {
1449 SqliteDb *pDb; /* Database handle */
1450 Tcl_Obj *pSql; /* Object holding string zSql */
1451 const char *zSql; /* Remaining SQL to execute */
1452 SqlPreparedStmt *pPreStmt; /* Current statement */
1453 int nCol; /* Number of columns returned by pStmt */
1454 Tcl_Obj *pArray; /* Name of array variable */
1455 Tcl_Obj **apColName; /* Array of column names */
1456};
1457
1458/*
1459** Release any cache of column names currently held as part of
1460** the DbEvalContext structure passed as the first argument.
1461*/
1462static void dbReleaseColumnNames(DbEvalContext *p){
1463 if( p->apColName ){
1464 int i;
1465 for(i=0; i<p->nCol; i++){
1466 Tcl_DecrRefCount(p->apColName[i]);
1467 }
1468 Tcl_Free((char *)p->apColName);
1469 p->apColName = 0;
1470 }
1471 p->nCol = 0;
1472}
1473
1474/*
1475** Initialize a DbEvalContext structure.
danielk19778e556522007-11-13 10:30:24 +00001476**
1477** If pArray is not NULL, then it contains the name of a Tcl array
1478** variable. The "*" member of this array is set to a list containing
dan4a4c11a2009-10-06 14:59:02 +00001479** the names of the columns returned by the statement as part of each
mistachkinb56660f2016-07-14 21:26:09 +00001480** call to dbEvalStep(), in order from left to right. e.g. if the names
1481** of the returned columns are a, b and c, it does the equivalent of the
dan4a4c11a2009-10-06 14:59:02 +00001482** tcl command:
danielk19778e556522007-11-13 10:30:24 +00001483**
1484** set ${pArray}(*) {a b c}
1485*/
dan4a4c11a2009-10-06 14:59:02 +00001486static void dbEvalInit(
1487 DbEvalContext *p, /* Pointer to structure to initialize */
1488 SqliteDb *pDb, /* Database handle */
1489 Tcl_Obj *pSql, /* Object containing SQL script */
1490 Tcl_Obj *pArray /* Name of Tcl array to set (*) element of */
danielk19778e556522007-11-13 10:30:24 +00001491){
dan4a4c11a2009-10-06 14:59:02 +00001492 memset(p, 0, sizeof(DbEvalContext));
1493 p->pDb = pDb;
1494 p->zSql = Tcl_GetString(pSql);
1495 p->pSql = pSql;
1496 Tcl_IncrRefCount(pSql);
1497 if( pArray ){
1498 p->pArray = pArray;
1499 Tcl_IncrRefCount(pArray);
1500 }
1501}
danielk19778e556522007-11-13 10:30:24 +00001502
dan4a4c11a2009-10-06 14:59:02 +00001503/*
1504** Obtain information about the row that the DbEvalContext passed as the
1505** first argument currently points to.
1506*/
1507static void dbEvalRowInfo(
1508 DbEvalContext *p, /* Evaluation context */
1509 int *pnCol, /* OUT: Number of column names */
1510 Tcl_Obj ***papColName /* OUT: Array of column names */
1511){
danielk19778e556522007-11-13 10:30:24 +00001512 /* Compute column names */
dan4a4c11a2009-10-06 14:59:02 +00001513 if( 0==p->apColName ){
1514 sqlite3_stmt *pStmt = p->pPreStmt->pStmt;
1515 int i; /* Iterator variable */
1516 int nCol; /* Number of columns returned by pStmt */
1517 Tcl_Obj **apColName = 0; /* Array of column names */
1518
1519 p->nCol = nCol = sqlite3_column_count(pStmt);
1520 if( nCol>0 && (papColName || p->pArray) ){
1521 apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol );
1522 for(i=0; i<nCol; i++){
drhc45e6712012-10-03 11:02:33 +00001523 apColName[i] = Tcl_NewStringObj(sqlite3_column_name(pStmt,i), -1);
dan4a4c11a2009-10-06 14:59:02 +00001524 Tcl_IncrRefCount(apColName[i]);
1525 }
1526 p->apColName = apColName;
danielk19778e556522007-11-13 10:30:24 +00001527 }
1528
1529 /* If results are being stored in an array variable, then create
1530 ** the array(*) entry for that array
1531 */
dan4a4c11a2009-10-06 14:59:02 +00001532 if( p->pArray ){
1533 Tcl_Interp *interp = p->pDb->interp;
danielk19778e556522007-11-13 10:30:24 +00001534 Tcl_Obj *pColList = Tcl_NewObj();
1535 Tcl_Obj *pStar = Tcl_NewStringObj("*", -1);
dan4a4c11a2009-10-06 14:59:02 +00001536
danielk19778e556522007-11-13 10:30:24 +00001537 for(i=0; i<nCol; i++){
1538 Tcl_ListObjAppendElement(interp, pColList, apColName[i]);
1539 }
1540 Tcl_IncrRefCount(pStar);
dan4a4c11a2009-10-06 14:59:02 +00001541 Tcl_ObjSetVar2(interp, p->pArray, pStar, pColList, 0);
danielk19778e556522007-11-13 10:30:24 +00001542 Tcl_DecrRefCount(pStar);
1543 }
danielk19778e556522007-11-13 10:30:24 +00001544 }
1545
dan4a4c11a2009-10-06 14:59:02 +00001546 if( papColName ){
1547 *papColName = p->apColName;
1548 }
1549 if( pnCol ){
1550 *pnCol = p->nCol;
1551 }
1552}
1553
1554/*
1555** Return one of TCL_OK, TCL_BREAK or TCL_ERROR. If TCL_ERROR is
1556** returned, then an error message is stored in the interpreter before
1557** returning.
1558**
1559** A return value of TCL_OK means there is a row of data available. The
1560** data may be accessed using dbEvalRowInfo() and dbEvalColumnValue(). This
1561** is analogous to a return of SQLITE_ROW from sqlite3_step(). If TCL_BREAK
1562** is returned, then the SQL script has finished executing and there are
1563** no further rows available. This is similar to SQLITE_DONE.
1564*/
1565static int dbEvalStep(DbEvalContext *p){
danc431fd52011-06-27 16:55:50 +00001566 const char *zPrevSql = 0; /* Previous value of p->zSql */
1567
dan4a4c11a2009-10-06 14:59:02 +00001568 while( p->zSql[0] || p->pPreStmt ){
1569 int rc;
1570 if( p->pPreStmt==0 ){
danc431fd52011-06-27 16:55:50 +00001571 zPrevSql = (p->zSql==zPrevSql ? 0 : p->zSql);
dan4a4c11a2009-10-06 14:59:02 +00001572 rc = dbPrepareAndBind(p->pDb, p->zSql, &p->zSql, &p->pPreStmt);
1573 if( rc!=TCL_OK ) return rc;
1574 }else{
1575 int rcs;
1576 SqliteDb *pDb = p->pDb;
1577 SqlPreparedStmt *pPreStmt = p->pPreStmt;
1578 sqlite3_stmt *pStmt = pPreStmt->pStmt;
1579
1580 rcs = sqlite3_step(pStmt);
1581 if( rcs==SQLITE_ROW ){
1582 return TCL_OK;
1583 }
1584 if( p->pArray ){
1585 dbEvalRowInfo(p, 0, 0);
1586 }
1587 rcs = sqlite3_reset(pStmt);
1588
1589 pDb->nStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_FULLSCAN_STEP,1);
1590 pDb->nSort = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_SORT,1);
drh3c379b02010-04-07 19:31:59 +00001591 pDb->nIndex = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_AUTOINDEX,1);
danc456a762017-06-22 16:51:16 +00001592 pDb->nVMStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_VM_STEP,1);
dan4a4c11a2009-10-06 14:59:02 +00001593 dbReleaseColumnNames(p);
1594 p->pPreStmt = 0;
1595
1596 if( rcs!=SQLITE_OK ){
1597 /* If a run-time error occurs, report the error and stop reading
1598 ** the SQL. */
dan4a4c11a2009-10-06 14:59:02 +00001599 dbReleaseStmt(pDb, pPreStmt, 1);
danc431fd52011-06-27 16:55:50 +00001600#if SQLITE_TEST
1601 if( p->pDb->bLegacyPrepare && rcs==SQLITE_SCHEMA && zPrevSql ){
1602 /* If the runtime error was an SQLITE_SCHEMA, and the database
mistachkinb56660f2016-07-14 21:26:09 +00001603 ** handle is configured to use the legacy sqlite3_prepare()
danc431fd52011-06-27 16:55:50 +00001604 ** interface, retry prepare()/step() on the same SQL statement.
1605 ** This only happens once. If there is a second SQLITE_SCHEMA
1606 ** error, the error will be returned to the caller. */
1607 p->zSql = zPrevSql;
1608 continue;
1609 }
1610#endif
drhc45e6712012-10-03 11:02:33 +00001611 Tcl_SetObjResult(pDb->interp,
1612 Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1));
dan4a4c11a2009-10-06 14:59:02 +00001613 return TCL_ERROR;
1614 }else{
1615 dbReleaseStmt(pDb, pPreStmt, 0);
1616 }
1617 }
1618 }
1619
1620 /* Finished */
1621 return TCL_BREAK;
1622}
1623
1624/*
1625** Free all resources currently held by the DbEvalContext structure passed
1626** as the first argument. There should be exactly one call to this function
1627** for each call to dbEvalInit().
1628*/
1629static void dbEvalFinalize(DbEvalContext *p){
1630 if( p->pPreStmt ){
1631 sqlite3_reset(p->pPreStmt->pStmt);
1632 dbReleaseStmt(p->pDb, p->pPreStmt, 0);
1633 p->pPreStmt = 0;
1634 }
1635 if( p->pArray ){
1636 Tcl_DecrRefCount(p->pArray);
1637 p->pArray = 0;
1638 }
1639 Tcl_DecrRefCount(p->pSql);
1640 dbReleaseColumnNames(p);
1641}
1642
1643/*
1644** Return a pointer to a Tcl_Obj structure with ref-count 0 that contains
1645** the value for the iCol'th column of the row currently pointed to by
1646** the DbEvalContext structure passed as the first argument.
1647*/
1648static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){
1649 sqlite3_stmt *pStmt = p->pPreStmt->pStmt;
1650 switch( sqlite3_column_type(pStmt, iCol) ){
1651 case SQLITE_BLOB: {
1652 int bytes = sqlite3_column_bytes(pStmt, iCol);
1653 const char *zBlob = sqlite3_column_blob(pStmt, iCol);
1654 if( !zBlob ) bytes = 0;
1655 return Tcl_NewByteArrayObj((u8*)zBlob, bytes);
1656 }
1657 case SQLITE_INTEGER: {
1658 sqlite_int64 v = sqlite3_column_int64(pStmt, iCol);
1659 if( v>=-2147483647 && v<=2147483647 ){
drh7fd33922011-06-20 19:00:30 +00001660 return Tcl_NewIntObj((int)v);
dan4a4c11a2009-10-06 14:59:02 +00001661 }else{
1662 return Tcl_NewWideIntObj(v);
1663 }
1664 }
1665 case SQLITE_FLOAT: {
1666 return Tcl_NewDoubleObj(sqlite3_column_double(pStmt, iCol));
1667 }
1668 case SQLITE_NULL: {
drhc45e6712012-10-03 11:02:33 +00001669 return Tcl_NewStringObj(p->pDb->zNull, -1);
dan4a4c11a2009-10-06 14:59:02 +00001670 }
1671 }
1672
drh325eff52012-10-03 12:56:18 +00001673 return Tcl_NewStringObj((char*)sqlite3_column_text(pStmt, iCol), -1);
dan4a4c11a2009-10-06 14:59:02 +00001674}
1675
1676/*
1677** If using Tcl version 8.6 or greater, use the NR functions to avoid
1678** recursive evalution of scripts by the [db eval] and [db trans]
1679** commands. Even if the headers used while compiling the extension
1680** are 8.6 or newer, the code still tests the Tcl version at runtime.
1681** This allows stubs-enabled builds to be used with older Tcl libraries.
1682*/
1683#if TCL_MAJOR_VERSION>8 || (TCL_MAJOR_VERSION==8 && TCL_MINOR_VERSION>=6)
drha2c8a952009-10-13 18:38:34 +00001684# define SQLITE_TCL_NRE 1
dan4a4c11a2009-10-06 14:59:02 +00001685static int DbUseNre(void){
1686 int major, minor;
1687 Tcl_GetVersion(&major, &minor, 0, 0);
1688 return( (major==8 && minor>=6) || major>8 );
1689}
1690#else
mistachkinb56660f2016-07-14 21:26:09 +00001691/*
dan4a4c11a2009-10-06 14:59:02 +00001692** Compiling using headers earlier than 8.6. In this case NR cannot be
1693** used, so DbUseNre() to always return zero. Add #defines for the other
1694** Tcl_NRxxx() functions to prevent them from causing compilation errors,
mistachkinb56660f2016-07-14 21:26:09 +00001695** even though the only invocations of them are within conditional blocks
dan4a4c11a2009-10-06 14:59:02 +00001696** of the form:
1697**
1698** if( DbUseNre() ) { ... }
1699*/
drha2c8a952009-10-13 18:38:34 +00001700# define SQLITE_TCL_NRE 0
dan4a4c11a2009-10-06 14:59:02 +00001701# define DbUseNre() 0
drha47941f2013-12-20 18:57:44 +00001702# define Tcl_NRAddCallback(a,b,c,d,e,f) (void)0
dan4a4c11a2009-10-06 14:59:02 +00001703# define Tcl_NREvalObj(a,b,c) 0
drha47941f2013-12-20 18:57:44 +00001704# define Tcl_NRCreateCommand(a,b,c,d,e,f) (void)0
dan4a4c11a2009-10-06 14:59:02 +00001705#endif
1706
1707/*
1708** This function is part of the implementation of the command:
1709**
1710** $db eval SQL ?ARRAYNAME? SCRIPT
1711*/
mistachkina121cc72016-07-28 18:06:52 +00001712static int SQLITE_TCLAPI DbEvalNextCmd(
dan4a4c11a2009-10-06 14:59:02 +00001713 ClientData data[], /* data[0] is the (DbEvalContext*) */
1714 Tcl_Interp *interp, /* Tcl interpreter */
1715 int result /* Result so far */
1716){
1717 int rc = result; /* Return code */
1718
1719 /* The first element of the data[] array is a pointer to a DbEvalContext
1720 ** structure allocated using Tcl_Alloc(). The second element of data[]
1721 ** is a pointer to a Tcl_Obj containing the script to run for each row
1722 ** returned by the queries encapsulated in data[0]. */
1723 DbEvalContext *p = (DbEvalContext *)data[0];
1724 Tcl_Obj *pScript = (Tcl_Obj *)data[1];
1725 Tcl_Obj *pArray = p->pArray;
1726
1727 while( (rc==TCL_OK || rc==TCL_CONTINUE) && TCL_OK==(rc = dbEvalStep(p)) ){
1728 int i;
1729 int nCol;
1730 Tcl_Obj **apColName;
1731 dbEvalRowInfo(p, &nCol, &apColName);
1732 for(i=0; i<nCol; i++){
1733 Tcl_Obj *pVal = dbEvalColumnValue(p, i);
1734 if( pArray==0 ){
1735 Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0);
1736 }else{
1737 Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0);
1738 }
1739 }
1740
mistachkinb56660f2016-07-14 21:26:09 +00001741 /* The required interpreter variables are now populated with the data
dan4a4c11a2009-10-06 14:59:02 +00001742 ** from the current row. If using NRE, schedule callbacks to evaluate
1743 ** script pScript, then to invoke this function again to fetch the next
1744 ** row (or clean up if there is no next row or the script throws an
mistachkinb56660f2016-07-14 21:26:09 +00001745 ** exception). After scheduling the callbacks, return control to the
dan4a4c11a2009-10-06 14:59:02 +00001746 ** caller.
1747 **
1748 ** If not using NRE, evaluate pScript directly and continue with the
1749 ** next iteration of this while(...) loop. */
1750 if( DbUseNre() ){
1751 Tcl_NRAddCallback(interp, DbEvalNextCmd, (void*)p, (void*)pScript, 0, 0);
1752 return Tcl_NREvalObj(interp, pScript, 0);
1753 }else{
1754 rc = Tcl_EvalObjEx(interp, pScript, 0);
1755 }
1756 }
1757
1758 Tcl_DecrRefCount(pScript);
1759 dbEvalFinalize(p);
1760 Tcl_Free((char *)p);
1761
1762 if( rc==TCL_OK || rc==TCL_BREAK ){
1763 Tcl_ResetResult(interp);
1764 rc = TCL_OK;
1765 }
1766 return rc;
danielk19778e556522007-11-13 10:30:24 +00001767}
1768
tpoindex1067fe12004-12-17 15:41:11 +00001769/*
mistachkinb56660f2016-07-14 21:26:09 +00001770** This function is used by the implementations of the following database
dan46c47d42011-03-01 18:42:07 +00001771** handle sub-commands:
1772**
1773** $db update_hook ?SCRIPT?
1774** $db wal_hook ?SCRIPT?
1775** $db commit_hook ?SCRIPT?
1776** $db preupdate hook ?SCRIPT?
1777*/
1778static void DbHookCmd(
1779 Tcl_Interp *interp, /* Tcl interpreter */
1780 SqliteDb *pDb, /* Database handle */
1781 Tcl_Obj *pArg, /* SCRIPT argument (or NULL) */
1782 Tcl_Obj **ppHook /* Pointer to member of SqliteDb */
1783){
1784 sqlite3 *db = pDb->db;
1785
1786 if( *ppHook ){
1787 Tcl_SetObjResult(interp, *ppHook);
1788 if( pArg ){
1789 Tcl_DecrRefCount(*ppHook);
1790 *ppHook = 0;
1791 }
1792 }
1793 if( pArg ){
1794 assert( !(*ppHook) );
1795 if( Tcl_GetCharLength(pArg)>0 ){
1796 *ppHook = pArg;
1797 Tcl_IncrRefCount(*ppHook);
1798 }
1799 }
1800
drh9b1c62d2011-03-30 21:04:43 +00001801#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00001802 sqlite3_preupdate_hook(db, (pDb->pPreUpdateHook?DbPreUpdateHandler:0), pDb);
drh9b1c62d2011-03-30 21:04:43 +00001803#endif
dan46c47d42011-03-01 18:42:07 +00001804 sqlite3_update_hook(db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb);
1805 sqlite3_rollback_hook(db, (pDb->pRollbackHook?DbRollbackHandler:0), pDb);
1806 sqlite3_wal_hook(db, (pDb->pWalHook?DbWalHandler:0), pDb);
1807}
1808
1809/*
drh75897232000-05-29 14:26:00 +00001810** The "sqlite" command below creates a new Tcl command for each
1811** connection it opens to an SQLite database. This routine is invoked
1812** whenever one of those connection-specific commands is executed
1813** in Tcl. For example, if you run Tcl code like this:
1814**
drh9bb575f2004-09-06 17:24:11 +00001815** sqlite3 db1 "my_database"
drh75897232000-05-29 14:26:00 +00001816** db1 close
1817**
1818** The first command opens a connection to the "my_database" database
1819** and calls that connection "db1". The second command causes this
1820** subroutine to be invoked.
1821*/
mistachkin7617e4a2016-07-28 17:11:20 +00001822static int SQLITE_TCLAPI DbObjCmd(
1823 void *cd,
1824 Tcl_Interp *interp,
1825 int objc,
1826 Tcl_Obj *const*objv
1827){
drhbec3f402000-08-04 13:49:02 +00001828 SqliteDb *pDb = (SqliteDb*)cd;
drh6d313162000-09-21 13:01:35 +00001829 int choice;
drh22fbcb82004-02-01 01:22:50 +00001830 int rc = TCL_OK;
drh0de8c112002-07-06 16:32:14 +00001831 static const char *DB_strs[] = {
drhdc2c4912009-02-04 22:46:47 +00001832 "authorizer", "backup", "busy",
1833 "cache", "changes", "close",
1834 "collate", "collation_needed", "commit_hook",
1835 "complete", "copy", "enable_load_extension",
1836 "errorcode", "eval", "exists",
1837 "function", "incrblob", "interrupt",
drh833bf962010-04-28 14:42:19 +00001838 "last_insert_rowid", "nullvalue", "onecolumn",
drh304637c2011-03-18 16:47:27 +00001839 "preupdate", "profile", "progress",
1840 "rekey", "restore", "rollback_hook",
1841 "status", "timeout", "total_changes",
mistachkinb56660f2016-07-14 21:26:09 +00001842 "trace", "trace_v2", "transaction",
1843 "unlock_notify", "update_hook", "version",
1844 "wal_hook",
1845 0
drh6d313162000-09-21 13:01:35 +00001846 };
drh411995d2002-06-25 19:31:18 +00001847 enum DB_enum {
drhdc2c4912009-02-04 22:46:47 +00001848 DB_AUTHORIZER, DB_BACKUP, DB_BUSY,
1849 DB_CACHE, DB_CHANGES, DB_CLOSE,
1850 DB_COLLATE, DB_COLLATION_NEEDED, DB_COMMIT_HOOK,
1851 DB_COMPLETE, DB_COPY, DB_ENABLE_LOAD_EXTENSION,
1852 DB_ERRORCODE, DB_EVAL, DB_EXISTS,
1853 DB_FUNCTION, DB_INCRBLOB, DB_INTERRUPT,
drh833bf962010-04-28 14:42:19 +00001854 DB_LAST_INSERT_ROWID, DB_NULLVALUE, DB_ONECOLUMN,
drh304637c2011-03-18 16:47:27 +00001855 DB_PREUPDATE, DB_PROFILE, DB_PROGRESS,
1856 DB_REKEY, DB_RESTORE, DB_ROLLBACK_HOOK,
1857 DB_STATUS, DB_TIMEOUT, DB_TOTAL_CHANGES,
mistachkinb56660f2016-07-14 21:26:09 +00001858 DB_TRACE, DB_TRACE_V2, DB_TRANSACTION,
1859 DB_UNLOCK_NOTIFY, DB_UPDATE_HOOK, DB_VERSION,
1860 DB_WAL_HOOK,
drh6d313162000-09-21 13:01:35 +00001861 };
tpoindex1067fe12004-12-17 15:41:11 +00001862 /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */
drh6d313162000-09-21 13:01:35 +00001863
1864 if( objc<2 ){
1865 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
drh75897232000-05-29 14:26:00 +00001866 return TCL_ERROR;
1867 }
drh411995d2002-06-25 19:31:18 +00001868 if( Tcl_GetIndexFromObj(interp, objv[1], DB_strs, "option", 0, &choice) ){
drh6d313162000-09-21 13:01:35 +00001869 return TCL_ERROR;
1870 }
1871
drh411995d2002-06-25 19:31:18 +00001872 switch( (enum DB_enum)choice ){
drh75897232000-05-29 14:26:00 +00001873
drhe22a3342003-04-22 20:30:37 +00001874 /* $db authorizer ?CALLBACK?
1875 **
1876 ** Invoke the given callback to authorize each SQL operation as it is
1877 ** compiled. 5 arguments are appended to the callback before it is
1878 ** invoked:
1879 **
1880 ** (1) The authorization type (ex: SQLITE_CREATE_TABLE, SQLITE_INSERT, ...)
1881 ** (2) First descriptive name (depends on authorization type)
1882 ** (3) Second descriptive name
1883 ** (4) Name of the database (ex: "main", "temp")
1884 ** (5) Name of trigger that is doing the access
1885 **
1886 ** The callback should return on of the following strings: SQLITE_OK,
1887 ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error.
1888 **
1889 ** If this method is invoked with no arguments, the current authorization
1890 ** callback string is returned.
1891 */
1892 case DB_AUTHORIZER: {
drh1211de32004-07-26 12:24:22 +00001893#ifdef SQLITE_OMIT_AUTHORIZATION
drha198f2b2014-02-07 19:26:13 +00001894 Tcl_AppendResult(interp, "authorization not available in this build",
1895 (char*)0);
drh1211de32004-07-26 12:24:22 +00001896 return TCL_ERROR;
1897#else
drhe22a3342003-04-22 20:30:37 +00001898 if( objc>3 ){
1899 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drh0f14e2e2004-06-29 12:39:08 +00001900 return TCL_ERROR;
drhe22a3342003-04-22 20:30:37 +00001901 }else if( objc==2 ){
drhb5a20d32003-04-23 12:25:23 +00001902 if( pDb->zAuth ){
drha198f2b2014-02-07 19:26:13 +00001903 Tcl_AppendResult(interp, pDb->zAuth, (char*)0);
drhe22a3342003-04-22 20:30:37 +00001904 }
1905 }else{
1906 char *zAuth;
1907 int len;
1908 if( pDb->zAuth ){
1909 Tcl_Free(pDb->zAuth);
1910 }
1911 zAuth = Tcl_GetStringFromObj(objv[2], &len);
1912 if( zAuth && len>0 ){
1913 pDb->zAuth = Tcl_Alloc( len + 1 );
drh5bb3eb92007-05-04 13:15:55 +00001914 memcpy(pDb->zAuth, zAuth, len+1);
drhe22a3342003-04-22 20:30:37 +00001915 }else{
1916 pDb->zAuth = 0;
1917 }
drhe22a3342003-04-22 20:30:37 +00001918 if( pDb->zAuth ){
drh32c6a482014-09-11 13:44:52 +00001919 typedef int (*sqlite3_auth_cb)(
1920 void*,int,const char*,const char*,
1921 const char*,const char*);
drhe22a3342003-04-22 20:30:37 +00001922 pDb->interp = interp;
drh32c6a482014-09-11 13:44:52 +00001923 sqlite3_set_authorizer(pDb->db,(sqlite3_auth_cb)auth_callback,pDb);
drhe22a3342003-04-22 20:30:37 +00001924 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001925 sqlite3_set_authorizer(pDb->db, 0, 0);
drhe22a3342003-04-22 20:30:37 +00001926 }
drhe22a3342003-04-22 20:30:37 +00001927 }
drh1211de32004-07-26 12:24:22 +00001928#endif
drhe22a3342003-04-22 20:30:37 +00001929 break;
1930 }
1931
drhdc2c4912009-02-04 22:46:47 +00001932 /* $db backup ?DATABASE? FILENAME
1933 **
1934 ** Open or create a database file named FILENAME. Transfer the
1935 ** content of local database DATABASE (default: "main") into the
1936 ** FILENAME database.
1937 */
1938 case DB_BACKUP: {
1939 const char *zDestFile;
1940 const char *zSrcDb;
1941 sqlite3 *pDest;
1942 sqlite3_backup *pBackup;
1943
1944 if( objc==3 ){
1945 zSrcDb = "main";
1946 zDestFile = Tcl_GetString(objv[2]);
1947 }else if( objc==4 ){
1948 zSrcDb = Tcl_GetString(objv[2]);
1949 zDestFile = Tcl_GetString(objv[3]);
1950 }else{
1951 Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME");
1952 return TCL_ERROR;
1953 }
drh147ef392016-01-22 23:17:51 +00001954 rc = sqlite3_open_v2(zDestFile, &pDest,
1955 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE| pDb->openFlags, 0);
drhdc2c4912009-02-04 22:46:47 +00001956 if( rc!=SQLITE_OK ){
1957 Tcl_AppendResult(interp, "cannot open target database: ",
1958 sqlite3_errmsg(pDest), (char*)0);
1959 sqlite3_close(pDest);
1960 return TCL_ERROR;
1961 }
1962 pBackup = sqlite3_backup_init(pDest, "main", pDb->db, zSrcDb);
1963 if( pBackup==0 ){
1964 Tcl_AppendResult(interp, "backup failed: ",
1965 sqlite3_errmsg(pDest), (char*)0);
1966 sqlite3_close(pDest);
1967 return TCL_ERROR;
1968 }
1969 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
1970 sqlite3_backup_finish(pBackup);
1971 if( rc==SQLITE_DONE ){
1972 rc = TCL_OK;
1973 }else{
1974 Tcl_AppendResult(interp, "backup failed: ",
1975 sqlite3_errmsg(pDest), (char*)0);
1976 rc = TCL_ERROR;
1977 }
1978 sqlite3_close(pDest);
1979 break;
1980 }
1981
drhbec3f402000-08-04 13:49:02 +00001982 /* $db busy ?CALLBACK?
1983 **
1984 ** Invoke the given callback if an SQL statement attempts to open
1985 ** a locked database file.
1986 */
drh6d313162000-09-21 13:01:35 +00001987 case DB_BUSY: {
1988 if( objc>3 ){
1989 Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
drhbec3f402000-08-04 13:49:02 +00001990 return TCL_ERROR;
drh6d313162000-09-21 13:01:35 +00001991 }else if( objc==2 ){
drhbec3f402000-08-04 13:49:02 +00001992 if( pDb->zBusy ){
drha198f2b2014-02-07 19:26:13 +00001993 Tcl_AppendResult(interp, pDb->zBusy, (char*)0);
drhbec3f402000-08-04 13:49:02 +00001994 }
1995 }else{
drh6d313162000-09-21 13:01:35 +00001996 char *zBusy;
1997 int len;
drhbec3f402000-08-04 13:49:02 +00001998 if( pDb->zBusy ){
1999 Tcl_Free(pDb->zBusy);
drhbec3f402000-08-04 13:49:02 +00002000 }
drh6d313162000-09-21 13:01:35 +00002001 zBusy = Tcl_GetStringFromObj(objv[2], &len);
2002 if( zBusy && len>0 ){
2003 pDb->zBusy = Tcl_Alloc( len + 1 );
drh5bb3eb92007-05-04 13:15:55 +00002004 memcpy(pDb->zBusy, zBusy, len+1);
drh6d313162000-09-21 13:01:35 +00002005 }else{
2006 pDb->zBusy = 0;
drhbec3f402000-08-04 13:49:02 +00002007 }
2008 if( pDb->zBusy ){
2009 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +00002010 sqlite3_busy_handler(pDb->db, DbBusyHandler, pDb);
drh6d313162000-09-21 13:01:35 +00002011 }else{
danielk19776f8a5032004-05-10 10:34:51 +00002012 sqlite3_busy_handler(pDb->db, 0, 0);
drhbec3f402000-08-04 13:49:02 +00002013 }
2014 }
drh6d313162000-09-21 13:01:35 +00002015 break;
2016 }
drhbec3f402000-08-04 13:49:02 +00002017
drhfb7e7652005-01-24 00:28:42 +00002018 /* $db cache flush
2019 ** $db cache size n
2020 **
2021 ** Flush the prepared statement cache, or set the maximum number of
2022 ** cached statements.
2023 */
2024 case DB_CACHE: {
2025 char *subCmd;
2026 int n;
2027
2028 if( objc<=2 ){
2029 Tcl_WrongNumArgs(interp, 1, objv, "cache option ?arg?");
2030 return TCL_ERROR;
2031 }
2032 subCmd = Tcl_GetStringFromObj( objv[2], 0 );
2033 if( *subCmd=='f' && strcmp(subCmd,"flush")==0 ){
2034 if( objc!=3 ){
2035 Tcl_WrongNumArgs(interp, 2, objv, "flush");
2036 return TCL_ERROR;
2037 }else{
2038 flushStmtCache( pDb );
2039 }
2040 }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){
2041 if( objc!=4 ){
2042 Tcl_WrongNumArgs(interp, 2, objv, "size n");
2043 return TCL_ERROR;
2044 }else{
2045 if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){
mistachkinb56660f2016-07-14 21:26:09 +00002046 Tcl_AppendResult( interp, "cannot convert \"",
drha198f2b2014-02-07 19:26:13 +00002047 Tcl_GetStringFromObj(objv[3],0), "\" to integer", (char*)0);
drhfb7e7652005-01-24 00:28:42 +00002048 return TCL_ERROR;
2049 }else{
2050 if( n<0 ){
2051 flushStmtCache( pDb );
2052 n = 0;
2053 }else if( n>MAX_PREPARED_STMTS ){
2054 n = MAX_PREPARED_STMTS;
2055 }
2056 pDb->maxStmt = n;
2057 }
2058 }
2059 }else{
mistachkinb56660f2016-07-14 21:26:09 +00002060 Tcl_AppendResult( interp, "bad option \"",
drha198f2b2014-02-07 19:26:13 +00002061 Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size",
2062 (char*)0);
drhfb7e7652005-01-24 00:28:42 +00002063 return TCL_ERROR;
2064 }
2065 break;
2066 }
2067
danielk1977b28af712004-06-21 06:50:26 +00002068 /* $db changes
drhc8d30ac2002-04-12 10:08:59 +00002069 **
2070 ** Return the number of rows that were modified, inserted, or deleted by
mistachkinb56660f2016-07-14 21:26:09 +00002071 ** the most recent INSERT, UPDATE or DELETE statement, not including
danielk1977b28af712004-06-21 06:50:26 +00002072 ** any changes made by trigger programs.
drhc8d30ac2002-04-12 10:08:59 +00002073 */
2074 case DB_CHANGES: {
2075 Tcl_Obj *pResult;
drhc8d30ac2002-04-12 10:08:59 +00002076 if( objc!=2 ){
2077 Tcl_WrongNumArgs(interp, 2, objv, "");
2078 return TCL_ERROR;
2079 }
drhc8d30ac2002-04-12 10:08:59 +00002080 pResult = Tcl_GetObjResult(interp);
danielk1977b28af712004-06-21 06:50:26 +00002081 Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db));
rdcf146a772004-02-25 22:51:06 +00002082 break;
2083 }
2084
drh75897232000-05-29 14:26:00 +00002085 /* $db close
2086 **
2087 ** Shutdown the database
2088 */
drh6d313162000-09-21 13:01:35 +00002089 case DB_CLOSE: {
2090 Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0));
2091 break;
2092 }
drh75897232000-05-29 14:26:00 +00002093
drh0f14e2e2004-06-29 12:39:08 +00002094 /*
2095 ** $db collate NAME SCRIPT
2096 **
2097 ** Create a new SQL collation function called NAME. Whenever
2098 ** that function is called, invoke SCRIPT to evaluate the function.
2099 */
2100 case DB_COLLATE: {
2101 SqlCollate *pCollate;
2102 char *zName;
2103 char *zScript;
2104 int nScript;
2105 if( objc!=4 ){
2106 Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
2107 return TCL_ERROR;
2108 }
2109 zName = Tcl_GetStringFromObj(objv[2], 0);
2110 zScript = Tcl_GetStringFromObj(objv[3], &nScript);
2111 pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
2112 if( pCollate==0 ) return TCL_ERROR;
2113 pCollate->interp = interp;
2114 pCollate->pNext = pDb->pCollate;
2115 pCollate->zScript = (char*)&pCollate[1];
2116 pDb->pCollate = pCollate;
drh5bb3eb92007-05-04 13:15:55 +00002117 memcpy(pCollate->zScript, zScript, nScript+1);
mistachkinb56660f2016-07-14 21:26:09 +00002118 if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8,
drh0f14e2e2004-06-29 12:39:08 +00002119 pCollate, tclSqlCollate) ){
danielk19779636c4e2005-01-25 04:27:54 +00002120 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
drh0f14e2e2004-06-29 12:39:08 +00002121 return TCL_ERROR;
2122 }
2123 break;
2124 }
2125
2126 /*
2127 ** $db collation_needed SCRIPT
2128 **
2129 ** Create a new SQL collation function called NAME. Whenever
2130 ** that function is called, invoke SCRIPT to evaluate the function.
2131 */
2132 case DB_COLLATION_NEEDED: {
2133 if( objc!=3 ){
2134 Tcl_WrongNumArgs(interp, 2, objv, "SCRIPT");
2135 return TCL_ERROR;
2136 }
2137 if( pDb->pCollateNeeded ){
2138 Tcl_DecrRefCount(pDb->pCollateNeeded);
2139 }
2140 pDb->pCollateNeeded = Tcl_DuplicateObj(objv[2]);
2141 Tcl_IncrRefCount(pDb->pCollateNeeded);
2142 sqlite3_collation_needed(pDb->db, pDb, tclCollateNeeded);
2143 break;
2144 }
2145
drh19e2d372005-08-29 23:00:03 +00002146 /* $db commit_hook ?CALLBACK?
2147 **
2148 ** Invoke the given callback just before committing every SQL transaction.
2149 ** If the callback throws an exception or returns non-zero, then the
2150 ** transaction is aborted. If CALLBACK is an empty string, the callback
2151 ** is disabled.
2152 */
2153 case DB_COMMIT_HOOK: {
2154 if( objc>3 ){
2155 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
2156 return TCL_ERROR;
2157 }else if( objc==2 ){
2158 if( pDb->zCommit ){
drha198f2b2014-02-07 19:26:13 +00002159 Tcl_AppendResult(interp, pDb->zCommit, (char*)0);
drh19e2d372005-08-29 23:00:03 +00002160 }
2161 }else{
mistachkin6ef5e122014-01-24 17:03:55 +00002162 const char *zCommit;
drh19e2d372005-08-29 23:00:03 +00002163 int len;
2164 if( pDb->zCommit ){
2165 Tcl_Free(pDb->zCommit);
2166 }
2167 zCommit = Tcl_GetStringFromObj(objv[2], &len);
2168 if( zCommit && len>0 ){
2169 pDb->zCommit = Tcl_Alloc( len + 1 );
drh5bb3eb92007-05-04 13:15:55 +00002170 memcpy(pDb->zCommit, zCommit, len+1);
drh19e2d372005-08-29 23:00:03 +00002171 }else{
2172 pDb->zCommit = 0;
2173 }
2174 if( pDb->zCommit ){
2175 pDb->interp = interp;
2176 sqlite3_commit_hook(pDb->db, DbCommitHandler, pDb);
2177 }else{
2178 sqlite3_commit_hook(pDb->db, 0, 0);
2179 }
2180 }
2181 break;
2182 }
2183
drh75897232000-05-29 14:26:00 +00002184 /* $db complete SQL
2185 **
2186 ** Return TRUE if SQL is a complete SQL statement. Return FALSE if
2187 ** additional lines of input are needed. This is similar to the
2188 ** built-in "info complete" command of Tcl.
2189 */
drh6d313162000-09-21 13:01:35 +00002190 case DB_COMPLETE: {
drhccae6022005-02-26 17:31:26 +00002191#ifndef SQLITE_OMIT_COMPLETE
drh6d313162000-09-21 13:01:35 +00002192 Tcl_Obj *pResult;
2193 int isComplete;
2194 if( objc!=3 ){
2195 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
drh75897232000-05-29 14:26:00 +00002196 return TCL_ERROR;
2197 }
danielk19776f8a5032004-05-10 10:34:51 +00002198 isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
drh6d313162000-09-21 13:01:35 +00002199 pResult = Tcl_GetObjResult(interp);
2200 Tcl_SetBooleanObj(pResult, isComplete);
drhccae6022005-02-26 17:31:26 +00002201#endif
drh6d313162000-09-21 13:01:35 +00002202 break;
2203 }
drhdcd997e2003-01-31 17:21:49 +00002204
drh19e2d372005-08-29 23:00:03 +00002205 /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR?
2206 **
2207 ** Copy data into table from filename, optionally using SEPARATOR
2208 ** as column separators. If a column contains a null string, or the
2209 ** value of NULLINDICATOR, a NULL is inserted for the column.
2210 ** conflict-algorithm is one of the sqlite conflict algorithms:
2211 ** rollback, abort, fail, ignore, replace
2212 ** On success, return the number of lines processed, not necessarily same
2213 ** as 'db changes' due to conflict-algorithm selected.
2214 **
2215 ** This code is basically an implementation/enhancement of
2216 ** the sqlite3 shell.c ".import" command.
2217 **
2218 ** This command usage is equivalent to the sqlite2.x COPY statement,
2219 ** which imports file data into a table using the PostgreSQL COPY file format:
2220 ** $db copy $conflit_algo $table_name $filename \t \\N
2221 */
2222 case DB_COPY: {
2223 char *zTable; /* Insert data into this table */
2224 char *zFile; /* The file from which to extract data */
2225 char *zConflict; /* The conflict algorithm to use */
2226 sqlite3_stmt *pStmt; /* A statement */
drh19e2d372005-08-29 23:00:03 +00002227 int nCol; /* Number of columns in the table */
2228 int nByte; /* Number of bytes in an SQL string */
2229 int i, j; /* Loop counters */
2230 int nSep; /* Number of bytes in zSep[] */
2231 int nNull; /* Number of bytes in zNull[] */
2232 char *zSql; /* An SQL statement */
2233 char *zLine; /* A single line of input from the file */
2234 char **azCol; /* zLine[] broken up into columns */
mistachkin6ef5e122014-01-24 17:03:55 +00002235 const char *zCommit; /* How to commit changes */
drh19e2d372005-08-29 23:00:03 +00002236 FILE *in; /* The input file */
2237 int lineno = 0; /* Line number of input file */
2238 char zLineNum[80]; /* Line number print buffer */
2239 Tcl_Obj *pResult; /* interp result */
2240
mistachkin6ef5e122014-01-24 17:03:55 +00002241 const char *zSep;
2242 const char *zNull;
drh19e2d372005-08-29 23:00:03 +00002243 if( objc<5 || objc>7 ){
mistachkinb56660f2016-07-14 21:26:09 +00002244 Tcl_WrongNumArgs(interp, 2, objv,
drh19e2d372005-08-29 23:00:03 +00002245 "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?");
2246 return TCL_ERROR;
2247 }
2248 if( objc>=6 ){
2249 zSep = Tcl_GetStringFromObj(objv[5], 0);
2250 }else{
2251 zSep = "\t";
2252 }
2253 if( objc>=7 ){
2254 zNull = Tcl_GetStringFromObj(objv[6], 0);
2255 }else{
2256 zNull = "";
2257 }
2258 zConflict = Tcl_GetStringFromObj(objv[2], 0);
2259 zTable = Tcl_GetStringFromObj(objv[3], 0);
2260 zFile = Tcl_GetStringFromObj(objv[4], 0);
drh4f21c4a2008-12-10 22:15:00 +00002261 nSep = strlen30(zSep);
2262 nNull = strlen30(zNull);
drh19e2d372005-08-29 23:00:03 +00002263 if( nSep==0 ){
drha198f2b2014-02-07 19:26:13 +00002264 Tcl_AppendResult(interp,"Error: non-null separator required for copy",
2265 (char*)0);
drh19e2d372005-08-29 23:00:03 +00002266 return TCL_ERROR;
2267 }
drh3e59c012008-09-23 10:12:13 +00002268 if(strcmp(zConflict, "rollback") != 0 &&
2269 strcmp(zConflict, "abort" ) != 0 &&
2270 strcmp(zConflict, "fail" ) != 0 &&
2271 strcmp(zConflict, "ignore" ) != 0 &&
2272 strcmp(zConflict, "replace" ) != 0 ) {
mistachkinb56660f2016-07-14 21:26:09 +00002273 Tcl_AppendResult(interp, "Error: \"", zConflict,
drh19e2d372005-08-29 23:00:03 +00002274 "\", conflict-algorithm must be one of: rollback, "
drha198f2b2014-02-07 19:26:13 +00002275 "abort, fail, ignore, or replace", (char*)0);
drh19e2d372005-08-29 23:00:03 +00002276 return TCL_ERROR;
2277 }
2278 zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
2279 if( zSql==0 ){
drha198f2b2014-02-07 19:26:13 +00002280 Tcl_AppendResult(interp, "Error: no such table: ", zTable, (char*)0);
drh19e2d372005-08-29 23:00:03 +00002281 return TCL_ERROR;
2282 }
drh4f21c4a2008-12-10 22:15:00 +00002283 nByte = strlen30(zSql);
drh3e701a12007-02-01 01:53:44 +00002284 rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0);
drh19e2d372005-08-29 23:00:03 +00002285 sqlite3_free(zSql);
2286 if( rc ){
drha198f2b2014-02-07 19:26:13 +00002287 Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0);
drh19e2d372005-08-29 23:00:03 +00002288 nCol = 0;
2289 }else{
2290 nCol = sqlite3_column_count(pStmt);
2291 }
2292 sqlite3_finalize(pStmt);
2293 if( nCol==0 ) {
2294 return TCL_ERROR;
2295 }
2296 zSql = malloc( nByte + 50 + nCol*2 );
2297 if( zSql==0 ) {
drha198f2b2014-02-07 19:26:13 +00002298 Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0);
drh19e2d372005-08-29 23:00:03 +00002299 return TCL_ERROR;
2300 }
2301 sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?",
2302 zConflict, zTable);
drh4f21c4a2008-12-10 22:15:00 +00002303 j = strlen30(zSql);
drh19e2d372005-08-29 23:00:03 +00002304 for(i=1; i<nCol; i++){
2305 zSql[j++] = ',';
2306 zSql[j++] = '?';
2307 }
2308 zSql[j++] = ')';
2309 zSql[j] = 0;
drh3e701a12007-02-01 01:53:44 +00002310 rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0);
drh19e2d372005-08-29 23:00:03 +00002311 free(zSql);
2312 if( rc ){
drha198f2b2014-02-07 19:26:13 +00002313 Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0);
drh19e2d372005-08-29 23:00:03 +00002314 sqlite3_finalize(pStmt);
2315 return TCL_ERROR;
2316 }
2317 in = fopen(zFile, "rb");
2318 if( in==0 ){
drhea8f0a12017-01-12 11:50:08 +00002319 Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, (char*)0);
drh19e2d372005-08-29 23:00:03 +00002320 sqlite3_finalize(pStmt);
2321 return TCL_ERROR;
2322 }
2323 azCol = malloc( sizeof(azCol[0])*(nCol+1) );
2324 if( azCol==0 ) {
drha198f2b2014-02-07 19:26:13 +00002325 Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0);
drh43617e92006-03-06 20:55:46 +00002326 fclose(in);
drh19e2d372005-08-29 23:00:03 +00002327 return TCL_ERROR;
2328 }
drh37527852006-03-16 16:19:56 +00002329 (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0);
drh19e2d372005-08-29 23:00:03 +00002330 zCommit = "COMMIT";
2331 while( (zLine = local_getline(0, in))!=0 ){
2332 char *z;
drh19e2d372005-08-29 23:00:03 +00002333 lineno++;
2334 azCol[0] = zLine;
2335 for(i=0, z=zLine; *z; z++){
2336 if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){
2337 *z = 0;
2338 i++;
2339 if( i<nCol ){
2340 azCol[i] = &z[nSep];
2341 z += nSep-1;
2342 }
2343 }
2344 }
2345 if( i+1!=nCol ){
2346 char *zErr;
drh4f21c4a2008-12-10 22:15:00 +00002347 int nErr = strlen30(zFile) + 200;
drh5bb3eb92007-05-04 13:15:55 +00002348 zErr = malloc(nErr);
drhc1f44942006-05-10 14:39:13 +00002349 if( zErr ){
drh5bb3eb92007-05-04 13:15:55 +00002350 sqlite3_snprintf(nErr, zErr,
drhc1f44942006-05-10 14:39:13 +00002351 "Error: %s line %d: expected %d columns of data but found %d",
2352 zFile, lineno, nCol, i+1);
drha198f2b2014-02-07 19:26:13 +00002353 Tcl_AppendResult(interp, zErr, (char*)0);
drhc1f44942006-05-10 14:39:13 +00002354 free(zErr);
2355 }
drh19e2d372005-08-29 23:00:03 +00002356 zCommit = "ROLLBACK";
2357 break;
2358 }
2359 for(i=0; i<nCol; i++){
2360 /* check for null data, if so, bind as null */
drhea678832008-12-10 19:26:22 +00002361 if( (nNull>0 && strcmp(azCol[i], zNull)==0)
mistachkinb56660f2016-07-14 21:26:09 +00002362 || strlen30(azCol[i])==0
drhea678832008-12-10 19:26:22 +00002363 ){
drh19e2d372005-08-29 23:00:03 +00002364 sqlite3_bind_null(pStmt, i+1);
2365 }else{
2366 sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
2367 }
2368 }
2369 sqlite3_step(pStmt);
2370 rc = sqlite3_reset(pStmt);
2371 free(zLine);
2372 if( rc!=SQLITE_OK ){
drha198f2b2014-02-07 19:26:13 +00002373 Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), (char*)0);
drh19e2d372005-08-29 23:00:03 +00002374 zCommit = "ROLLBACK";
2375 break;
2376 }
2377 }
2378 free(azCol);
2379 fclose(in);
2380 sqlite3_finalize(pStmt);
drh37527852006-03-16 16:19:56 +00002381 (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0);
drh19e2d372005-08-29 23:00:03 +00002382
2383 if( zCommit[0] == 'C' ){
2384 /* success, set result as number of lines processed */
2385 pResult = Tcl_GetObjResult(interp);
2386 Tcl_SetIntObj(pResult, lineno);
2387 rc = TCL_OK;
2388 }else{
2389 /* failure, append lineno where failed */
drh5bb3eb92007-05-04 13:15:55 +00002390 sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno);
drha198f2b2014-02-07 19:26:13 +00002391 Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,
2392 (char*)0);
drh19e2d372005-08-29 23:00:03 +00002393 rc = TCL_ERROR;
2394 }
2395 break;
2396 }
2397
drhdcd997e2003-01-31 17:21:49 +00002398 /*
drh41449052006-07-06 17:08:48 +00002399 ** $db enable_load_extension BOOLEAN
2400 **
2401 ** Turn the extension loading feature on or off. It if off by
2402 ** default.
2403 */
2404 case DB_ENABLE_LOAD_EXTENSION: {
drhf533acc2006-12-19 18:57:11 +00002405#ifndef SQLITE_OMIT_LOAD_EXTENSION
drh41449052006-07-06 17:08:48 +00002406 int onoff;
2407 if( objc!=3 ){
2408 Tcl_WrongNumArgs(interp, 2, objv, "BOOLEAN");
2409 return TCL_ERROR;
2410 }
2411 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
2412 return TCL_ERROR;
2413 }
2414 sqlite3_enable_load_extension(pDb->db, onoff);
2415 break;
drhf533acc2006-12-19 18:57:11 +00002416#else
2417 Tcl_AppendResult(interp, "extension loading is turned off at compile-time",
drha198f2b2014-02-07 19:26:13 +00002418 (char*)0);
drhf533acc2006-12-19 18:57:11 +00002419 return TCL_ERROR;
2420#endif
drh41449052006-07-06 17:08:48 +00002421 }
2422
2423 /*
drhdcd997e2003-01-31 17:21:49 +00002424 ** $db errorcode
2425 **
2426 ** Return the numeric error code that was returned by the most recent
danielk19776f8a5032004-05-10 10:34:51 +00002427 ** call to sqlite3_exec().
drhdcd997e2003-01-31 17:21:49 +00002428 */
2429 case DB_ERRORCODE: {
danielk1977f3ce83f2004-06-14 11:43:46 +00002430 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_errcode(pDb->db)));
drhdcd997e2003-01-31 17:21:49 +00002431 break;
2432 }
dan4a4c11a2009-10-06 14:59:02 +00002433
2434 /*
2435 ** $db exists $sql
2436 ** $db onecolumn $sql
2437 **
2438 ** The onecolumn method is the equivalent of:
2439 ** lindex [$db eval $sql] 0
2440 */
mistachkinb56660f2016-07-14 21:26:09 +00002441 case DB_EXISTS:
dan4a4c11a2009-10-06 14:59:02 +00002442 case DB_ONECOLUMN: {
drhedc40242016-06-13 12:34:38 +00002443 Tcl_Obj *pResult = 0;
dan4a4c11a2009-10-06 14:59:02 +00002444 DbEvalContext sEval;
2445 if( objc!=3 ){
2446 Tcl_WrongNumArgs(interp, 2, objv, "SQL");
2447 return TCL_ERROR;
2448 }
2449
2450 dbEvalInit(&sEval, pDb, objv[2], 0);
2451 rc = dbEvalStep(&sEval);
2452 if( choice==DB_ONECOLUMN ){
2453 if( rc==TCL_OK ){
drhedc40242016-06-13 12:34:38 +00002454 pResult = dbEvalColumnValue(&sEval, 0);
dand5f12cd2011-08-18 17:47:57 +00002455 }else if( rc==TCL_BREAK ){
2456 Tcl_ResetResult(interp);
dan4a4c11a2009-10-06 14:59:02 +00002457 }
2458 }else if( rc==TCL_BREAK || rc==TCL_OK ){
drhedc40242016-06-13 12:34:38 +00002459 pResult = Tcl_NewBooleanObj(rc==TCL_OK);
dan4a4c11a2009-10-06 14:59:02 +00002460 }
2461 dbEvalFinalize(&sEval);
drhedc40242016-06-13 12:34:38 +00002462 if( pResult ) Tcl_SetObjResult(interp, pResult);
dan4a4c11a2009-10-06 14:59:02 +00002463
2464 if( rc==TCL_BREAK ){
2465 rc = TCL_OK;
2466 }
2467 break;
2468 }
mistachkinb56660f2016-07-14 21:26:09 +00002469
drh75897232000-05-29 14:26:00 +00002470 /*
drh895d7472004-08-20 16:02:39 +00002471 ** $db eval $sql ?array? ?{ ...code... }?
drh75897232000-05-29 14:26:00 +00002472 **
2473 ** The SQL statement in $sql is evaluated. For each row, the values are
drhbec3f402000-08-04 13:49:02 +00002474 ** placed in elements of the array named "array" and ...code... is executed.
drh75897232000-05-29 14:26:00 +00002475 ** If "array" and "code" are omitted, then no callback is every invoked.
2476 ** If "array" is an empty string, then the values are placed in variables
2477 ** that have the same name as the fields extracted by the query.
2478 */
dan4a4c11a2009-10-06 14:59:02 +00002479 case DB_EVAL: {
2480 if( objc<3 || objc>5 ){
2481 Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?");
2482 return TCL_ERROR;
danielk197730ccda12004-05-27 12:11:31 +00002483 }
dan4a4c11a2009-10-06 14:59:02 +00002484
drh92febd92004-08-20 18:34:20 +00002485 if( objc==3 ){
dan4a4c11a2009-10-06 14:59:02 +00002486 DbEvalContext sEval;
2487 Tcl_Obj *pRet = Tcl_NewObj();
2488 Tcl_IncrRefCount(pRet);
2489 dbEvalInit(&sEval, pDb, objv[2], 0);
2490 while( TCL_OK==(rc = dbEvalStep(&sEval)) ){
2491 int i;
2492 int nCol;
2493 dbEvalRowInfo(&sEval, &nCol, 0);
drh92febd92004-08-20 18:34:20 +00002494 for(i=0; i<nCol; i++){
dan4a4c11a2009-10-06 14:59:02 +00002495 Tcl_ListObjAppendElement(interp, pRet, dbEvalColumnValue(&sEval, i));
danielk197730ccda12004-05-27 12:11:31 +00002496 }
2497 }
dan4a4c11a2009-10-06 14:59:02 +00002498 dbEvalFinalize(&sEval);
drh90b6bb12004-09-13 13:16:31 +00002499 if( rc==TCL_BREAK ){
dan4a4c11a2009-10-06 14:59:02 +00002500 Tcl_SetObjResult(interp, pRet);
drh90b6bb12004-09-13 13:16:31 +00002501 rc = TCL_OK;
2502 }
drh1807ce32004-09-07 13:20:35 +00002503 Tcl_DecrRefCount(pRet);
dan4a4c11a2009-10-06 14:59:02 +00002504 }else{
mistachkin8e189222015-04-19 21:43:16 +00002505 ClientData cd2[2];
dan4a4c11a2009-10-06 14:59:02 +00002506 DbEvalContext *p;
2507 Tcl_Obj *pArray = 0;
2508 Tcl_Obj *pScript;
2509
2510 if( objc==5 && *(char *)Tcl_GetString(objv[3]) ){
2511 pArray = objv[3];
2512 }
2513 pScript = objv[objc-1];
2514 Tcl_IncrRefCount(pScript);
mistachkinb56660f2016-07-14 21:26:09 +00002515
dan4a4c11a2009-10-06 14:59:02 +00002516 p = (DbEvalContext *)Tcl_Alloc(sizeof(DbEvalContext));
2517 dbEvalInit(p, pDb, objv[2], pArray);
2518
mistachkin8e189222015-04-19 21:43:16 +00002519 cd2[0] = (void *)p;
2520 cd2[1] = (void *)pScript;
2521 rc = DbEvalNextCmd(cd2, interp, TCL_OK);
danielk197730ccda12004-05-27 12:11:31 +00002522 }
danielk197730ccda12004-05-27 12:11:31 +00002523 break;
2524 }
drhbec3f402000-08-04 13:49:02 +00002525
2526 /*
dan3df30592015-03-13 08:31:54 +00002527 ** $db function NAME [-argcount N] [-deterministic] SCRIPT
drhcabb0812002-09-14 13:47:32 +00002528 **
2529 ** Create a new SQL function called NAME. Whenever that function is
2530 ** called, invoke SCRIPT to evaluate the function.
2531 */
2532 case DB_FUNCTION: {
dan3df30592015-03-13 08:31:54 +00002533 int flags = SQLITE_UTF8;
drhcabb0812002-09-14 13:47:32 +00002534 SqlFunc *pFunc;
drhd1e47332005-06-26 17:55:33 +00002535 Tcl_Obj *pScript;
drhcabb0812002-09-14 13:47:32 +00002536 char *zName;
drhe3602be2008-09-09 12:31:33 +00002537 int nArg = -1;
dan3df30592015-03-13 08:31:54 +00002538 int i;
2539 if( objc<4 ){
2540 Tcl_WrongNumArgs(interp, 2, objv, "NAME ?SWITCHES? SCRIPT");
2541 return TCL_ERROR;
2542 }
2543 for(i=3; i<(objc-1); i++){
2544 const char *z = Tcl_GetString(objv[i]);
drh4f21c4a2008-12-10 22:15:00 +00002545 int n = strlen30(z);
drhe3602be2008-09-09 12:31:33 +00002546 if( n>2 && strncmp(z, "-argcount",n)==0 ){
dan3df30592015-03-13 08:31:54 +00002547 if( i==(objc-2) ){
drhea8f0a12017-01-12 11:50:08 +00002548 Tcl_AppendResult(interp, "option requires an argument: ", z,(char*)0);
dan3df30592015-03-13 08:31:54 +00002549 return TCL_ERROR;
2550 }
2551 if( Tcl_GetIntFromObj(interp, objv[i+1], &nArg) ) return TCL_ERROR;
drhe3602be2008-09-09 12:31:33 +00002552 if( nArg<0 ){
2553 Tcl_AppendResult(interp, "number of arguments must be non-negative",
2554 (char*)0);
2555 return TCL_ERROR;
2556 }
dan3df30592015-03-13 08:31:54 +00002557 i++;
2558 }else
2559 if( n>2 && strncmp(z, "-deterministic",n)==0 ){
2560 flags |= SQLITE_DETERMINISTIC;
2561 }else{
mistachkinb56660f2016-07-14 21:26:09 +00002562 Tcl_AppendResult(interp, "bad option \"", z,
drhea8f0a12017-01-12 11:50:08 +00002563 "\": must be -argcount or -deterministic", (char*)0
dan3df30592015-03-13 08:31:54 +00002564 );
2565 return TCL_ERROR;
drhe3602be2008-09-09 12:31:33 +00002566 }
drhcabb0812002-09-14 13:47:32 +00002567 }
dan3df30592015-03-13 08:31:54 +00002568
2569 pScript = objv[objc-1];
drhcabb0812002-09-14 13:47:32 +00002570 zName = Tcl_GetStringFromObj(objv[2], 0);
drhd1e47332005-06-26 17:55:33 +00002571 pFunc = findSqlFunc(pDb, zName);
drhcabb0812002-09-14 13:47:32 +00002572 if( pFunc==0 ) return TCL_ERROR;
drhd1e47332005-06-26 17:55:33 +00002573 if( pFunc->pScript ){
2574 Tcl_DecrRefCount(pFunc->pScript);
2575 }
2576 pFunc->pScript = pScript;
2577 Tcl_IncrRefCount(pScript);
2578 pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript);
dan3df30592015-03-13 08:31:54 +00002579 rc = sqlite3_create_function(pDb->db, zName, nArg, flags,
danielk1977d8123362004-06-12 09:25:12 +00002580 pFunc, tclSqlFunc, 0, 0);
drhfb7e7652005-01-24 00:28:42 +00002581 if( rc!=SQLITE_OK ){
danielk19779636c4e2005-01-25 04:27:54 +00002582 rc = TCL_ERROR;
2583 Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
drhfb7e7652005-01-24 00:28:42 +00002584 }
drhcabb0812002-09-14 13:47:32 +00002585 break;
2586 }
2587
2588 /*
danielk19778cbadb02007-05-03 16:31:26 +00002589 ** $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID
danielk1977b4e9af92007-05-01 17:49:49 +00002590 */
2591 case DB_INCRBLOB: {
danielk197732a0d8b2007-05-04 19:03:02 +00002592#ifdef SQLITE_OMIT_INCRBLOB
drha198f2b2014-02-07 19:26:13 +00002593 Tcl_AppendResult(interp, "incrblob not available in this build", (char*)0);
danielk197732a0d8b2007-05-04 19:03:02 +00002594 return TCL_ERROR;
2595#else
danielk19778cbadb02007-05-03 16:31:26 +00002596 int isReadonly = 0;
danielk1977b4e9af92007-05-01 17:49:49 +00002597 const char *zDb = "main";
2598 const char *zTable;
2599 const char *zColumn;
drhb3f787f2012-09-29 14:45:54 +00002600 Tcl_WideInt iRow;
danielk1977b4e9af92007-05-01 17:49:49 +00002601
danielk19778cbadb02007-05-03 16:31:26 +00002602 /* Check for the -readonly option */
2603 if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){
2604 isReadonly = 1;
2605 }
2606
2607 if( objc!=(5+isReadonly) && objc!=(6+isReadonly) ){
2608 Tcl_WrongNumArgs(interp, 2, objv, "?-readonly? ?DB? TABLE COLUMN ROWID");
danielk1977b4e9af92007-05-01 17:49:49 +00002609 return TCL_ERROR;
2610 }
2611
danielk19778cbadb02007-05-03 16:31:26 +00002612 if( objc==(6+isReadonly) ){
danielk1977b4e9af92007-05-01 17:49:49 +00002613 zDb = Tcl_GetString(objv[2]);
2614 }
2615 zTable = Tcl_GetString(objv[objc-3]);
2616 zColumn = Tcl_GetString(objv[objc-2]);
2617 rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow);
2618
2619 if( rc==TCL_OK ){
danielk19778cbadb02007-05-03 16:31:26 +00002620 rc = createIncrblobChannel(
danedf5b162014-08-19 09:15:41 +00002621 interp, pDb, zDb, zTable, zColumn, (sqlite3_int64)iRow, isReadonly
danielk19778cbadb02007-05-03 16:31:26 +00002622 );
danielk1977b4e9af92007-05-01 17:49:49 +00002623 }
danielk197732a0d8b2007-05-04 19:03:02 +00002624#endif
danielk1977b4e9af92007-05-01 17:49:49 +00002625 break;
2626 }
2627
2628 /*
drhf11bded2006-07-17 00:02:44 +00002629 ** $db interrupt
2630 **
2631 ** Interrupt the execution of the inner-most SQL interpreter. This
2632 ** causes the SQL statement to return an error of SQLITE_INTERRUPT.
2633 */
2634 case DB_INTERRUPT: {
2635 sqlite3_interrupt(pDb->db);
2636 break;
2637 }
2638
2639 /*
drh19e2d372005-08-29 23:00:03 +00002640 ** $db nullvalue ?STRING?
2641 **
2642 ** Change text used when a NULL comes back from the database. If ?STRING?
2643 ** is not present, then the current string used for NULL is returned.
2644 ** If STRING is present, then STRING is returned.
2645 **
2646 */
2647 case DB_NULLVALUE: {
2648 if( objc!=2 && objc!=3 ){
2649 Tcl_WrongNumArgs(interp, 2, objv, "NULLVALUE");
2650 return TCL_ERROR;
2651 }
2652 if( objc==3 ){
2653 int len;
2654 char *zNull = Tcl_GetStringFromObj(objv[2], &len);
2655 if( pDb->zNull ){
2656 Tcl_Free(pDb->zNull);
2657 }
2658 if( zNull && len>0 ){
2659 pDb->zNull = Tcl_Alloc( len + 1 );
drh7fd33922011-06-20 19:00:30 +00002660 memcpy(pDb->zNull, zNull, len);
drh19e2d372005-08-29 23:00:03 +00002661 pDb->zNull[len] = '\0';
2662 }else{
2663 pDb->zNull = 0;
2664 }
2665 }
drhc45e6712012-10-03 11:02:33 +00002666 Tcl_SetObjResult(interp, Tcl_NewStringObj(pDb->zNull, -1));
drh19e2d372005-08-29 23:00:03 +00002667 break;
2668 }
2669
2670 /*
mistachkinb56660f2016-07-14 21:26:09 +00002671 ** $db last_insert_rowid
drhaf9ff332002-01-16 21:00:27 +00002672 **
2673 ** Return an integer which is the ROWID for the most recent insert.
2674 */
2675 case DB_LAST_INSERT_ROWID: {
2676 Tcl_Obj *pResult;
drhf7e678d2006-06-21 19:30:34 +00002677 Tcl_WideInt rowid;
drhaf9ff332002-01-16 21:00:27 +00002678 if( objc!=2 ){
2679 Tcl_WrongNumArgs(interp, 2, objv, "");
2680 return TCL_ERROR;
2681 }
danielk19776f8a5032004-05-10 10:34:51 +00002682 rowid = sqlite3_last_insert_rowid(pDb->db);
drhaf9ff332002-01-16 21:00:27 +00002683 pResult = Tcl_GetObjResult(interp);
drhf7e678d2006-06-21 19:30:34 +00002684 Tcl_SetWideIntObj(pResult, rowid);
drhaf9ff332002-01-16 21:00:27 +00002685 break;
2686 }
2687
2688 /*
dan4a4c11a2009-10-06 14:59:02 +00002689 ** The DB_ONECOLUMN method is implemented together with DB_EXISTS.
drh5d9d7572003-08-19 14:31:01 +00002690 */
drh1807ce32004-09-07 13:20:35 +00002691
2692 /* $db progress ?N CALLBACK?
mistachkinb56660f2016-07-14 21:26:09 +00002693 **
drh1807ce32004-09-07 13:20:35 +00002694 ** Invoke the given callback every N virtual machine opcodes while executing
2695 ** queries.
2696 */
2697 case DB_PROGRESS: {
2698 if( objc==2 ){
2699 if( pDb->zProgress ){
drha198f2b2014-02-07 19:26:13 +00002700 Tcl_AppendResult(interp, pDb->zProgress, (char*)0);
drh1807ce32004-09-07 13:20:35 +00002701 }
2702 }else if( objc==4 ){
2703 char *zProgress;
2704 int len;
2705 int N;
2706 if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
drhfd131da2007-08-07 17:13:03 +00002707 return TCL_ERROR;
drh1807ce32004-09-07 13:20:35 +00002708 };
2709 if( pDb->zProgress ){
2710 Tcl_Free(pDb->zProgress);
2711 }
2712 zProgress = Tcl_GetStringFromObj(objv[3], &len);
2713 if( zProgress && len>0 ){
2714 pDb->zProgress = Tcl_Alloc( len + 1 );
drh5bb3eb92007-05-04 13:15:55 +00002715 memcpy(pDb->zProgress, zProgress, len+1);
drh1807ce32004-09-07 13:20:35 +00002716 }else{
2717 pDb->zProgress = 0;
2718 }
2719#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2720 if( pDb->zProgress ){
2721 pDb->interp = interp;
2722 sqlite3_progress_handler(pDb->db, N, DbProgressHandler, pDb);
2723 }else{
2724 sqlite3_progress_handler(pDb->db, 0, 0, 0);
2725 }
2726#endif
2727 }else{
2728 Tcl_WrongNumArgs(interp, 2, objv, "N CALLBACK");
drh5d9d7572003-08-19 14:31:01 +00002729 return TCL_ERROR;
2730 }
drh5d9d7572003-08-19 14:31:01 +00002731 break;
2732 }
2733
drh19e2d372005-08-29 23:00:03 +00002734 /* $db profile ?CALLBACK?
2735 **
2736 ** Make arrangements to invoke the CALLBACK routine after each SQL statement
2737 ** that has run. The text of the SQL and the amount of elapse time are
2738 ** appended to CALLBACK before the script is run.
2739 */
2740 case DB_PROFILE: {
2741 if( objc>3 ){
2742 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
2743 return TCL_ERROR;
2744 }else if( objc==2 ){
2745 if( pDb->zProfile ){
drha198f2b2014-02-07 19:26:13 +00002746 Tcl_AppendResult(interp, pDb->zProfile, (char*)0);
drh19e2d372005-08-29 23:00:03 +00002747 }
2748 }else{
2749 char *zProfile;
2750 int len;
2751 if( pDb->zProfile ){
2752 Tcl_Free(pDb->zProfile);
2753 }
2754 zProfile = Tcl_GetStringFromObj(objv[2], &len);
2755 if( zProfile && len>0 ){
2756 pDb->zProfile = Tcl_Alloc( len + 1 );
drh5bb3eb92007-05-04 13:15:55 +00002757 memcpy(pDb->zProfile, zProfile, len+1);
drh19e2d372005-08-29 23:00:03 +00002758 }else{
2759 pDb->zProfile = 0;
2760 }
drh2eb22af2016-09-10 19:51:40 +00002761#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \
2762 !defined(SQLITE_OMIT_DEPRECATED)
drh19e2d372005-08-29 23:00:03 +00002763 if( pDb->zProfile ){
2764 pDb->interp = interp;
2765 sqlite3_profile(pDb->db, DbProfileHandler, pDb);
2766 }else{
2767 sqlite3_profile(pDb->db, 0, 0);
2768 }
2769#endif
2770 }
2771 break;
2772 }
2773
drh5d9d7572003-08-19 14:31:01 +00002774 /*
drh22fbcb82004-02-01 01:22:50 +00002775 ** $db rekey KEY
2776 **
2777 ** Change the encryption key on the currently open database.
2778 */
2779 case DB_REKEY: {
drh32f57d42016-03-16 01:03:10 +00002780#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL)
drh22fbcb82004-02-01 01:22:50 +00002781 int nKey;
2782 void *pKey;
drhb07028f2011-10-14 21:49:18 +00002783#endif
drh22fbcb82004-02-01 01:22:50 +00002784 if( objc!=3 ){
2785 Tcl_WrongNumArgs(interp, 2, objv, "KEY");
2786 return TCL_ERROR;
2787 }
drh32f57d42016-03-16 01:03:10 +00002788#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL)
drhb07028f2011-10-14 21:49:18 +00002789 pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
drh2011d5f2004-07-22 02:40:37 +00002790 rc = sqlite3_rekey(pDb->db, pKey, nKey);
drh22fbcb82004-02-01 01:22:50 +00002791 if( rc ){
drha198f2b2014-02-07 19:26:13 +00002792 Tcl_AppendResult(interp, sqlite3_errstr(rc), (char*)0);
drh22fbcb82004-02-01 01:22:50 +00002793 rc = TCL_ERROR;
2794 }
2795#endif
2796 break;
2797 }
2798
drhdc2c4912009-02-04 22:46:47 +00002799 /* $db restore ?DATABASE? FILENAME
2800 **
mistachkinb56660f2016-07-14 21:26:09 +00002801 ** Open a database file named FILENAME. Transfer the content
drhdc2c4912009-02-04 22:46:47 +00002802 ** of FILENAME into the local database DATABASE (default: "main").
2803 */
2804 case DB_RESTORE: {
2805 const char *zSrcFile;
2806 const char *zDestDb;
2807 sqlite3 *pSrc;
2808 sqlite3_backup *pBackup;
2809 int nTimeout = 0;
2810
2811 if( objc==3 ){
2812 zDestDb = "main";
2813 zSrcFile = Tcl_GetString(objv[2]);
2814 }else if( objc==4 ){
2815 zDestDb = Tcl_GetString(objv[2]);
2816 zSrcFile = Tcl_GetString(objv[3]);
2817 }else{
2818 Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME");
2819 return TCL_ERROR;
2820 }
drh147ef392016-01-22 23:17:51 +00002821 rc = sqlite3_open_v2(zSrcFile, &pSrc,
2822 SQLITE_OPEN_READONLY | pDb->openFlags, 0);
drhdc2c4912009-02-04 22:46:47 +00002823 if( rc!=SQLITE_OK ){
2824 Tcl_AppendResult(interp, "cannot open source database: ",
2825 sqlite3_errmsg(pSrc), (char*)0);
2826 sqlite3_close(pSrc);
2827 return TCL_ERROR;
2828 }
2829 pBackup = sqlite3_backup_init(pDb->db, zDestDb, pSrc, "main");
2830 if( pBackup==0 ){
2831 Tcl_AppendResult(interp, "restore failed: ",
2832 sqlite3_errmsg(pDb->db), (char*)0);
2833 sqlite3_close(pSrc);
2834 return TCL_ERROR;
2835 }
2836 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
2837 || rc==SQLITE_BUSY ){
2838 if( rc==SQLITE_BUSY ){
2839 if( nTimeout++ >= 3 ) break;
2840 sqlite3_sleep(100);
2841 }
2842 }
2843 sqlite3_backup_finish(pBackup);
2844 if( rc==SQLITE_DONE ){
2845 rc = TCL_OK;
2846 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
2847 Tcl_AppendResult(interp, "restore failed: source database busy",
2848 (char*)0);
2849 rc = TCL_ERROR;
2850 }else{
2851 Tcl_AppendResult(interp, "restore failed: ",
2852 sqlite3_errmsg(pDb->db), (char*)0);
2853 rc = TCL_ERROR;
2854 }
2855 sqlite3_close(pSrc);
2856 break;
2857 }
2858
drh22fbcb82004-02-01 01:22:50 +00002859 /*
danc456a762017-06-22 16:51:16 +00002860 ** $db status (step|sort|autoindex|vmstep)
drhd1d38482008-10-07 23:46:38 +00002861 **
mistachkinb56660f2016-07-14 21:26:09 +00002862 ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or
drhd1d38482008-10-07 23:46:38 +00002863 ** SQLITE_STMTSTATUS_SORT for the most recent eval.
2864 */
2865 case DB_STATUS: {
drhd1d38482008-10-07 23:46:38 +00002866 int v;
2867 const char *zOp;
2868 if( objc!=3 ){
drh1c320a42010-08-01 22:41:32 +00002869 Tcl_WrongNumArgs(interp, 2, objv, "(step|sort|autoindex)");
drhd1d38482008-10-07 23:46:38 +00002870 return TCL_ERROR;
2871 }
2872 zOp = Tcl_GetString(objv[2]);
2873 if( strcmp(zOp, "step")==0 ){
2874 v = pDb->nStep;
2875 }else if( strcmp(zOp, "sort")==0 ){
2876 v = pDb->nSort;
drh3c379b02010-04-07 19:31:59 +00002877 }else if( strcmp(zOp, "autoindex")==0 ){
2878 v = pDb->nIndex;
danc456a762017-06-22 16:51:16 +00002879 }else if( strcmp(zOp, "vmstep")==0 ){
2880 v = pDb->nVMStep;
drhd1d38482008-10-07 23:46:38 +00002881 }else{
mistachkinb56660f2016-07-14 21:26:09 +00002882 Tcl_AppendResult(interp,
danc456a762017-06-22 16:51:16 +00002883 "bad argument: should be autoindex, step, sort or vmstep",
drhd1d38482008-10-07 23:46:38 +00002884 (char*)0);
2885 return TCL_ERROR;
2886 }
2887 Tcl_SetObjResult(interp, Tcl_NewIntObj(v));
2888 break;
2889 }
mistachkinb56660f2016-07-14 21:26:09 +00002890
drhd1d38482008-10-07 23:46:38 +00002891 /*
drhbec3f402000-08-04 13:49:02 +00002892 ** $db timeout MILLESECONDS
2893 **
2894 ** Delay for the number of milliseconds specified when a file is locked.
2895 */
drh6d313162000-09-21 13:01:35 +00002896 case DB_TIMEOUT: {
drhbec3f402000-08-04 13:49:02 +00002897 int ms;
drh6d313162000-09-21 13:01:35 +00002898 if( objc!=3 ){
2899 Tcl_WrongNumArgs(interp, 2, objv, "MILLISECONDS");
drhbec3f402000-08-04 13:49:02 +00002900 return TCL_ERROR;
2901 }
drh6d313162000-09-21 13:01:35 +00002902 if( Tcl_GetIntFromObj(interp, objv[2], &ms) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +00002903 sqlite3_busy_timeout(pDb->db, ms);
drh6d313162000-09-21 13:01:35 +00002904 break;
drh75897232000-05-29 14:26:00 +00002905 }
mistachkinb56660f2016-07-14 21:26:09 +00002906
danielk197755c45f22005-04-03 23:54:43 +00002907 /*
drh0f14e2e2004-06-29 12:39:08 +00002908 ** $db total_changes
2909 **
mistachkinb56660f2016-07-14 21:26:09 +00002910 ** Return the number of rows that were modified, inserted, or deleted
drh0f14e2e2004-06-29 12:39:08 +00002911 ** since the database handle was created.
2912 */
2913 case DB_TOTAL_CHANGES: {
2914 Tcl_Obj *pResult;
2915 if( objc!=2 ){
2916 Tcl_WrongNumArgs(interp, 2, objv, "");
2917 return TCL_ERROR;
2918 }
2919 pResult = Tcl_GetObjResult(interp);
2920 Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db));
2921 break;
2922 }
2923
drhb5a20d32003-04-23 12:25:23 +00002924 /* $db trace ?CALLBACK?
2925 **
2926 ** Make arrangements to invoke the CALLBACK routine for each SQL statement
2927 ** that is executed. The text of the SQL is appended to CALLBACK before
2928 ** it is executed.
2929 */
2930 case DB_TRACE: {
2931 if( objc>3 ){
2932 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
drhb97759e2004-06-29 11:26:59 +00002933 return TCL_ERROR;
drhb5a20d32003-04-23 12:25:23 +00002934 }else if( objc==2 ){
2935 if( pDb->zTrace ){
drha198f2b2014-02-07 19:26:13 +00002936 Tcl_AppendResult(interp, pDb->zTrace, (char*)0);
drhb5a20d32003-04-23 12:25:23 +00002937 }
2938 }else{
2939 char *zTrace;
2940 int len;
2941 if( pDb->zTrace ){
2942 Tcl_Free(pDb->zTrace);
2943 }
2944 zTrace = Tcl_GetStringFromObj(objv[2], &len);
2945 if( zTrace && len>0 ){
2946 pDb->zTrace = Tcl_Alloc( len + 1 );
drh5bb3eb92007-05-04 13:15:55 +00002947 memcpy(pDb->zTrace, zTrace, len+1);
drhb5a20d32003-04-23 12:25:23 +00002948 }else{
2949 pDb->zTrace = 0;
2950 }
drh2eb22af2016-09-10 19:51:40 +00002951#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \
2952 !defined(SQLITE_OMIT_DEPRECATED)
drhb5a20d32003-04-23 12:25:23 +00002953 if( pDb->zTrace ){
2954 pDb->interp = interp;
danielk19776f8a5032004-05-10 10:34:51 +00002955 sqlite3_trace(pDb->db, DbTraceHandler, pDb);
drhb5a20d32003-04-23 12:25:23 +00002956 }else{
danielk19776f8a5032004-05-10 10:34:51 +00002957 sqlite3_trace(pDb->db, 0, 0);
drhb5a20d32003-04-23 12:25:23 +00002958 }
drh19e2d372005-08-29 23:00:03 +00002959#endif
drhb5a20d32003-04-23 12:25:23 +00002960 }
2961 break;
2962 }
2963
mistachkinb56660f2016-07-14 21:26:09 +00002964 /* $db trace_v2 ?CALLBACK? ?MASK?
2965 **
2966 ** Make arrangements to invoke the CALLBACK routine for each trace event
2967 ** matching the mask that is generated. The parameters are appended to
2968 ** CALLBACK before it is executed.
2969 */
2970 case DB_TRACE_V2: {
2971 if( objc>4 ){
2972 Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK? ?MASK?");
2973 return TCL_ERROR;
2974 }else if( objc==2 ){
2975 if( pDb->zTraceV2 ){
2976 Tcl_AppendResult(interp, pDb->zTraceV2, (char*)0);
2977 }
2978 }else{
mistachkinb56660f2016-07-14 21:26:09 +00002979 char *zTraceV2;
2980 int len;
mistachkinb52dcd82016-07-14 23:17:03 +00002981 Tcl_WideInt wMask = 0;
mistachkinb56660f2016-07-14 21:26:09 +00002982 if( objc==4 ){
mistachkinb52dcd82016-07-14 23:17:03 +00002983 static const char *TTYPE_strs[] = {
2984 "statement", "profile", "row", "close", 0
2985 };
2986 enum TTYPE_enum {
2987 TTYPE_STMT, TTYPE_PROFILE, TTYPE_ROW, TTYPE_CLOSE
2988 };
2989 int i;
2990 if( TCL_OK!=Tcl_ListObjLength(interp, objv[3], &len) ){
mistachkinb56660f2016-07-14 21:26:09 +00002991 return TCL_ERROR;
2992 }
mistachkinb52dcd82016-07-14 23:17:03 +00002993 for(i=0; i<len; i++){
2994 Tcl_Obj *pObj;
2995 int ttype;
2996 if( TCL_OK!=Tcl_ListObjIndex(interp, objv[3], i, &pObj) ){
2997 return TCL_ERROR;
2998 }
2999 if( Tcl_GetIndexFromObj(interp, pObj, TTYPE_strs, "trace type",
3000 0, &ttype)!=TCL_OK ){
3001 Tcl_WideInt wType;
3002 Tcl_Obj *pError = Tcl_DuplicateObj(Tcl_GetObjResult(interp));
3003 Tcl_IncrRefCount(pError);
3004 if( TCL_OK==Tcl_GetWideIntFromObj(interp, pObj, &wType) ){
3005 Tcl_DecrRefCount(pError);
3006 wMask |= wType;
3007 }else{
3008 Tcl_SetObjResult(interp, pError);
3009 Tcl_DecrRefCount(pError);
3010 return TCL_ERROR;
3011 }
3012 }else{
3013 switch( (enum TTYPE_enum)ttype ){
3014 case TTYPE_STMT: wMask |= SQLITE_TRACE_STMT; break;
3015 case TTYPE_PROFILE: wMask |= SQLITE_TRACE_PROFILE; break;
3016 case TTYPE_ROW: wMask |= SQLITE_TRACE_ROW; break;
3017 case TTYPE_CLOSE: wMask |= SQLITE_TRACE_CLOSE; break;
3018 }
3019 }
3020 }
mistachkinb56660f2016-07-14 21:26:09 +00003021 }else{
mistachkinb52dcd82016-07-14 23:17:03 +00003022 wMask = SQLITE_TRACE_STMT; /* use the "legacy" default */
mistachkinb56660f2016-07-14 21:26:09 +00003023 }
3024 if( pDb->zTraceV2 ){
3025 Tcl_Free(pDb->zTraceV2);
3026 }
3027 zTraceV2 = Tcl_GetStringFromObj(objv[2], &len);
3028 if( zTraceV2 && len>0 ){
3029 pDb->zTraceV2 = Tcl_Alloc( len + 1 );
3030 memcpy(pDb->zTraceV2, zTraceV2, len+1);
3031 }else{
3032 pDb->zTraceV2 = 0;
3033 }
3034#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
3035 if( pDb->zTraceV2 ){
3036 pDb->interp = interp;
3037 sqlite3_trace_v2(pDb->db, (unsigned)wMask, DbTraceV2Handler, pDb);
3038 }else{
3039 sqlite3_trace_v2(pDb->db, 0, 0, 0);
3040 }
3041#endif
3042 }
3043 break;
3044 }
3045
drh3d214232005-08-02 12:21:08 +00003046 /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT
3047 **
3048 ** Start a new transaction (if we are not already in the midst of a
3049 ** transaction) and execute the TCL script SCRIPT. After SCRIPT
3050 ** completes, either commit the transaction or roll it back if SCRIPT
3051 ** throws an exception. Or if no new transation was started, do nothing.
3052 ** pass the exception on up the stack.
3053 **
3054 ** This command was inspired by Dave Thomas's talk on Ruby at the
3055 ** 2005 O'Reilly Open Source Convention (OSCON).
3056 */
3057 case DB_TRANSACTION: {
drh3d214232005-08-02 12:21:08 +00003058 Tcl_Obj *pScript;
danielk1977cd38d522009-01-02 17:33:46 +00003059 const char *zBegin = "SAVEPOINT _tcl_transaction";
drh3d214232005-08-02 12:21:08 +00003060 if( objc!=3 && objc!=4 ){
3061 Tcl_WrongNumArgs(interp, 2, objv, "[TYPE] SCRIPT");
3062 return TCL_ERROR;
3063 }
danielk1977cd38d522009-01-02 17:33:46 +00003064
dan4a4c11a2009-10-06 14:59:02 +00003065 if( pDb->nTransaction==0 && objc==4 ){
drh3d214232005-08-02 12:21:08 +00003066 static const char *TTYPE_strs[] = {
drhce604012005-08-16 11:11:34 +00003067 "deferred", "exclusive", "immediate", 0
drh3d214232005-08-02 12:21:08 +00003068 };
3069 enum TTYPE_enum {
3070 TTYPE_DEFERRED, TTYPE_EXCLUSIVE, TTYPE_IMMEDIATE
3071 };
3072 int ttype;
drhb5555e72005-08-02 17:15:14 +00003073 if( Tcl_GetIndexFromObj(interp, objv[2], TTYPE_strs, "transaction type",
drh3d214232005-08-02 12:21:08 +00003074 0, &ttype) ){
3075 return TCL_ERROR;
3076 }
3077 switch( (enum TTYPE_enum)ttype ){
3078 case TTYPE_DEFERRED: /* no-op */; break;
3079 case TTYPE_EXCLUSIVE: zBegin = "BEGIN EXCLUSIVE"; break;
3080 case TTYPE_IMMEDIATE: zBegin = "BEGIN IMMEDIATE"; break;
3081 }
drh3d214232005-08-02 12:21:08 +00003082 }
danielk1977cd38d522009-01-02 17:33:46 +00003083 pScript = objv[objc-1];
3084
dan4a4c11a2009-10-06 14:59:02 +00003085 /* Run the SQLite BEGIN command to open a transaction or savepoint. */
danielk1977cd38d522009-01-02 17:33:46 +00003086 pDb->disableAuth++;
3087 rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0);
3088 pDb->disableAuth--;
3089 if( rc!=SQLITE_OK ){
drha198f2b2014-02-07 19:26:13 +00003090 Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0);
danielk1977cd38d522009-01-02 17:33:46 +00003091 return TCL_ERROR;
drh3d214232005-08-02 12:21:08 +00003092 }
danielk1977cd38d522009-01-02 17:33:46 +00003093 pDb->nTransaction++;
danielk1977cd38d522009-01-02 17:33:46 +00003094
dan4a4c11a2009-10-06 14:59:02 +00003095 /* If using NRE, schedule a callback to invoke the script pScript, then
3096 ** a second callback to commit (or rollback) the transaction or savepoint
3097 ** opened above. If not using NRE, evaluate the script directly, then
mistachkinb56660f2016-07-14 21:26:09 +00003098 ** call function DbTransPostCmd() to commit (or rollback) the transaction
dan4a4c11a2009-10-06 14:59:02 +00003099 ** or savepoint. */
3100 if( DbUseNre() ){
3101 Tcl_NRAddCallback(interp, DbTransPostCmd, cd, 0, 0, 0);
drha47941f2013-12-20 18:57:44 +00003102 (void)Tcl_NREvalObj(interp, pScript, 0);
danielk1977cd38d522009-01-02 17:33:46 +00003103 }else{
dan4a4c11a2009-10-06 14:59:02 +00003104 rc = DbTransPostCmd(&cd, interp, Tcl_EvalObjEx(interp, pScript, 0));
drh3d214232005-08-02 12:21:08 +00003105 }
3106 break;
3107 }
3108
danielk197794eb6a12005-12-15 15:22:08 +00003109 /*
danielk1977404ca072009-03-16 13:19:36 +00003110 ** $db unlock_notify ?script?
3111 */
3112 case DB_UNLOCK_NOTIFY: {
3113#ifndef SQLITE_ENABLE_UNLOCK_NOTIFY
drha198f2b2014-02-07 19:26:13 +00003114 Tcl_AppendResult(interp, "unlock_notify not available in this build",
3115 (char*)0);
danielk1977404ca072009-03-16 13:19:36 +00003116 rc = TCL_ERROR;
3117#else
3118 if( objc!=2 && objc!=3 ){
3119 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?");
3120 rc = TCL_ERROR;
3121 }else{
3122 void (*xNotify)(void **, int) = 0;
3123 void *pNotifyArg = 0;
3124
3125 if( pDb->pUnlockNotify ){
3126 Tcl_DecrRefCount(pDb->pUnlockNotify);
3127 pDb->pUnlockNotify = 0;
3128 }
mistachkinb56660f2016-07-14 21:26:09 +00003129
danielk1977404ca072009-03-16 13:19:36 +00003130 if( objc==3 ){
3131 xNotify = DbUnlockNotify;
3132 pNotifyArg = (void *)pDb;
3133 pDb->pUnlockNotify = objv[2];
3134 Tcl_IncrRefCount(pDb->pUnlockNotify);
3135 }
mistachkinb56660f2016-07-14 21:26:09 +00003136
danielk1977404ca072009-03-16 13:19:36 +00003137 if( sqlite3_unlock_notify(pDb->db, xNotify, pNotifyArg) ){
drha198f2b2014-02-07 19:26:13 +00003138 Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0);
danielk1977404ca072009-03-16 13:19:36 +00003139 rc = TCL_ERROR;
3140 }
3141 }
3142#endif
3143 break;
3144 }
3145
drh304637c2011-03-18 16:47:27 +00003146 /*
3147 ** $db preupdate_hook count
3148 ** $db preupdate_hook hook ?SCRIPT?
3149 ** $db preupdate_hook new INDEX
3150 ** $db preupdate_hook old INDEX
3151 */
dan46c47d42011-03-01 18:42:07 +00003152 case DB_PREUPDATE: {
drh9b1c62d2011-03-30 21:04:43 +00003153#ifndef SQLITE_ENABLE_PREUPDATE_HOOK
dan2b519ab2016-12-06 19:33:42 +00003154 Tcl_AppendResult(interp, "preupdate_hook was omitted at compile-time",
3155 (char*)0);
drh9b1c62d2011-03-30 21:04:43 +00003156 rc = TCL_ERROR;
3157#else
dan1e7a2d42011-03-22 18:45:29 +00003158 static const char *azSub[] = {"count", "depth", "hook", "new", "old", 0};
dan46c47d42011-03-01 18:42:07 +00003159 enum DbPreupdateSubCmd {
dan1e7a2d42011-03-22 18:45:29 +00003160 PRE_COUNT, PRE_DEPTH, PRE_HOOK, PRE_NEW, PRE_OLD
dan46c47d42011-03-01 18:42:07 +00003161 };
3162 int iSub;
3163
3164 if( objc<3 ){
3165 Tcl_WrongNumArgs(interp, 2, objv, "SUB-COMMAND ?ARGS?");
3166 }
3167 if( Tcl_GetIndexFromObj(interp, objv[2], azSub, "sub-command", 0, &iSub) ){
3168 return TCL_ERROR;
3169 }
3170
3171 switch( (enum DbPreupdateSubCmd)iSub ){
3172 case PRE_COUNT: {
3173 int nCol = sqlite3_preupdate_count(pDb->db);
3174 Tcl_SetObjResult(interp, Tcl_NewIntObj(nCol));
3175 break;
3176 }
3177
3178 case PRE_HOOK: {
3179 if( objc>4 ){
3180 Tcl_WrongNumArgs(interp, 2, objv, "hook ?SCRIPT?");
3181 return TCL_ERROR;
3182 }
3183 DbHookCmd(interp, pDb, (objc==4 ? objv[3] : 0), &pDb->pPreUpdateHook);
3184 break;
3185 }
3186
dan1e7a2d42011-03-22 18:45:29 +00003187 case PRE_DEPTH: {
3188 Tcl_Obj *pRet;
3189 if( objc!=3 ){
3190 Tcl_WrongNumArgs(interp, 3, objv, "");
3191 return TCL_ERROR;
3192 }
3193 pRet = Tcl_NewIntObj(sqlite3_preupdate_depth(pDb->db));
3194 Tcl_SetObjResult(interp, pRet);
3195 break;
3196 }
3197
dan37db03b2011-03-16 19:59:18 +00003198 case PRE_NEW:
dan46c47d42011-03-01 18:42:07 +00003199 case PRE_OLD: {
3200 int iIdx;
dan37db03b2011-03-16 19:59:18 +00003201 sqlite3_value *pValue;
dan46c47d42011-03-01 18:42:07 +00003202 if( objc!=4 ){
3203 Tcl_WrongNumArgs(interp, 3, objv, "INDEX");
3204 return TCL_ERROR;
3205 }
3206 if( Tcl_GetIntFromObj(interp, objv[3], &iIdx) ){
3207 return TCL_ERROR;
3208 }
3209
dan37db03b2011-03-16 19:59:18 +00003210 if( iSub==PRE_OLD ){
dan46c47d42011-03-01 18:42:07 +00003211 rc = sqlite3_preupdate_old(pDb->db, iIdx, &pValue);
dan37db03b2011-03-16 19:59:18 +00003212 }else{
3213 assert( iSub==PRE_NEW );
3214 rc = sqlite3_preupdate_new(pDb->db, iIdx, &pValue);
dan46c47d42011-03-01 18:42:07 +00003215 }
3216
dan37db03b2011-03-16 19:59:18 +00003217 if( rc==SQLITE_OK ){
drh304637c2011-03-18 16:47:27 +00003218 Tcl_Obj *pObj;
3219 pObj = Tcl_NewStringObj((char*)sqlite3_value_text(pValue), -1);
dan37db03b2011-03-16 19:59:18 +00003220 Tcl_SetObjResult(interp, pObj);
3221 }else{
drhea8f0a12017-01-12 11:50:08 +00003222 Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0);
dan46c47d42011-03-01 18:42:07 +00003223 return TCL_ERROR;
3224 }
3225 }
3226 }
drh9b1c62d2011-03-30 21:04:43 +00003227#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan46c47d42011-03-01 18:42:07 +00003228 break;
3229 }
3230
danielk1977404ca072009-03-16 13:19:36 +00003231 /*
drh833bf962010-04-28 14:42:19 +00003232 ** $db wal_hook ?script?
danielk197794eb6a12005-12-15 15:22:08 +00003233 ** $db update_hook ?script?
danielk197771fd80b2005-12-16 06:54:01 +00003234 ** $db rollback_hook ?script?
danielk197794eb6a12005-12-15 15:22:08 +00003235 */
mistachkinb56660f2016-07-14 21:26:09 +00003236 case DB_WAL_HOOK:
3237 case DB_UPDATE_HOOK:
dan6566ebe2011-03-16 09:49:14 +00003238 case DB_ROLLBACK_HOOK: {
mistachkinb56660f2016-07-14 21:26:09 +00003239 /* set ppHook to point at pUpdateHook or pRollbackHook, depending on
danielk197771fd80b2005-12-16 06:54:01 +00003240 ** whether [$db update_hook] or [$db rollback_hook] was invoked.
3241 */
mistachkinb56660f2016-07-14 21:26:09 +00003242 Tcl_Obj **ppHook = 0;
dan46c47d42011-03-01 18:42:07 +00003243 if( choice==DB_WAL_HOOK ) ppHook = &pDb->pWalHook;
3244 if( choice==DB_UPDATE_HOOK ) ppHook = &pDb->pUpdateHook;
3245 if( choice==DB_ROLLBACK_HOOK ) ppHook = &pDb->pRollbackHook;
3246 if( objc>3 ){
danielk197794eb6a12005-12-15 15:22:08 +00003247 Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?");
3248 return TCL_ERROR;
3249 }
danielk197771fd80b2005-12-16 06:54:01 +00003250
dan46c47d42011-03-01 18:42:07 +00003251 DbHookCmd(interp, pDb, (objc==3 ? objv[2] : 0), ppHook);
danielk197794eb6a12005-12-15 15:22:08 +00003252 break;
3253 }
3254
danielk19774397de52005-01-12 12:44:03 +00003255 /* $db version
3256 **
3257 ** Return the version string for this database.
3258 */
3259 case DB_VERSION: {
3260 Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC);
3261 break;
3262 }
3263
tpoindex1067fe12004-12-17 15:41:11 +00003264
drh6d313162000-09-21 13:01:35 +00003265 } /* End of the SWITCH statement */
drh22fbcb82004-02-01 01:22:50 +00003266 return rc;
drh75897232000-05-29 14:26:00 +00003267}
3268
drha2c8a952009-10-13 18:38:34 +00003269#if SQLITE_TCL_NRE
3270/*
3271** Adaptor that provides an objCmd interface to the NRE-enabled
3272** interface implementation.
3273*/
mistachkina121cc72016-07-28 18:06:52 +00003274static int SQLITE_TCLAPI DbObjCmdAdaptor(
drha2c8a952009-10-13 18:38:34 +00003275 void *cd,
3276 Tcl_Interp *interp,
3277 int objc,
3278 Tcl_Obj *const*objv
3279){
3280 return Tcl_NRCallObjProc(interp, DbObjCmd, cd, objc, objv);
3281}
3282#endif /* SQLITE_TCL_NRE */
3283
drh75897232000-05-29 14:26:00 +00003284/*
drh3570ad92007-08-31 14:31:44 +00003285** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN?
danielk19779a6284c2008-07-10 17:52:49 +00003286** ?-create BOOLEAN? ?-nomutex BOOLEAN?
drh75897232000-05-29 14:26:00 +00003287**
3288** This is the main Tcl command. When the "sqlite" Tcl command is
3289** invoked, this routine runs to process that command.
3290**
3291** The first argument, DBNAME, is an arbitrary name for a new
3292** database connection. This command creates a new command named
3293** DBNAME that is used to control that connection. The database
3294** connection is deleted when the DBNAME command is deleted.
3295**
drh3570ad92007-08-31 14:31:44 +00003296** The second argument is the name of the database file.
drhfbc3eab2001-04-06 16:13:42 +00003297**
drh75897232000-05-29 14:26:00 +00003298*/
mistachkin7617e4a2016-07-28 17:11:20 +00003299static int SQLITE_TCLAPI DbMain(
3300 void *cd,
3301 Tcl_Interp *interp,
3302 int objc,
3303 Tcl_Obj *const*objv
3304){
drhbec3f402000-08-04 13:49:02 +00003305 SqliteDb *p;
drh22fbcb82004-02-01 01:22:50 +00003306 const char *zArg;
drh75897232000-05-29 14:26:00 +00003307 char *zErrMsg;
drh3570ad92007-08-31 14:31:44 +00003308 int i;
drh22fbcb82004-02-01 01:22:50 +00003309 const char *zFile;
drh3570ad92007-08-31 14:31:44 +00003310 const char *zVfs = 0;
drhd9da78a2009-03-24 15:08:09 +00003311 int flags;
drh882e8e42006-08-24 02:42:27 +00003312 Tcl_DString translatedFilename;
drh32f57d42016-03-16 01:03:10 +00003313#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL)
drhb07028f2011-10-14 21:49:18 +00003314 void *pKey = 0;
3315 int nKey = 0;
3316#endif
mistachkin540ebf82012-09-10 07:29:29 +00003317 int rc;
drhd9da78a2009-03-24 15:08:09 +00003318
3319 /* In normal use, each TCL interpreter runs in a single thread. So
3320 ** by default, we can turn of mutexing on SQLite database connections.
3321 ** However, for testing purposes it is useful to have mutexes turned
3322 ** on. So, by default, mutexes default off. But if compiled with
3323 ** SQLITE_TCL_DEFAULT_FULLMUTEX then mutexes default on.
3324 */
3325#ifdef SQLITE_TCL_DEFAULT_FULLMUTEX
3326 flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
3327#else
3328 flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX;
3329#endif
3330
drh22fbcb82004-02-01 01:22:50 +00003331 if( objc==2 ){
3332 zArg = Tcl_GetStringFromObj(objv[1], 0);
drh22fbcb82004-02-01 01:22:50 +00003333 if( strcmp(zArg,"-version")==0 ){
drha198f2b2014-02-07 19:26:13 +00003334 Tcl_AppendResult(interp,sqlite3_libversion(), (char*)0);
drh647cb0e2002-11-04 19:32:25 +00003335 return TCL_OK;
3336 }
drh72bf6a32016-01-07 02:06:55 +00003337 if( strcmp(zArg,"-sourceid")==0 ){
3338 Tcl_AppendResult(interp,sqlite3_sourceid(), (char*)0);
3339 return TCL_OK;
3340 }
drh9eb9e262004-02-11 02:18:05 +00003341 if( strcmp(zArg,"-has-codec")==0 ){
drh32f57d42016-03-16 01:03:10 +00003342#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL)
drha198f2b2014-02-07 19:26:13 +00003343 Tcl_AppendResult(interp,"1",(char*)0);
drh22fbcb82004-02-01 01:22:50 +00003344#else
drha198f2b2014-02-07 19:26:13 +00003345 Tcl_AppendResult(interp,"0",(char*)0);
drh22fbcb82004-02-01 01:22:50 +00003346#endif
3347 return TCL_OK;
3348 }
drhfbc3eab2001-04-06 16:13:42 +00003349 }
drh3570ad92007-08-31 14:31:44 +00003350 for(i=3; i+1<objc; i+=2){
3351 zArg = Tcl_GetString(objv[i]);
drh22fbcb82004-02-01 01:22:50 +00003352 if( strcmp(zArg,"-key")==0 ){
drh32f57d42016-03-16 01:03:10 +00003353#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL)
drh3570ad92007-08-31 14:31:44 +00003354 pKey = Tcl_GetByteArrayFromObj(objv[i+1], &nKey);
drhb07028f2011-10-14 21:49:18 +00003355#endif
drh3570ad92007-08-31 14:31:44 +00003356 }else if( strcmp(zArg, "-vfs")==0 ){
dan3c3dd7b2010-06-22 11:10:40 +00003357 zVfs = Tcl_GetString(objv[i+1]);
drh3570ad92007-08-31 14:31:44 +00003358 }else if( strcmp(zArg, "-readonly")==0 ){
3359 int b;
3360 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR;
3361 if( b ){
drh33f4e022007-09-03 15:19:34 +00003362 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
drh3570ad92007-08-31 14:31:44 +00003363 flags |= SQLITE_OPEN_READONLY;
3364 }else{
3365 flags &= ~SQLITE_OPEN_READONLY;
3366 flags |= SQLITE_OPEN_READWRITE;
3367 }
3368 }else if( strcmp(zArg, "-create")==0 ){
3369 int b;
3370 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR;
drh33f4e022007-09-03 15:19:34 +00003371 if( b && (flags & SQLITE_OPEN_READONLY)==0 ){
drh3570ad92007-08-31 14:31:44 +00003372 flags |= SQLITE_OPEN_CREATE;
3373 }else{
3374 flags &= ~SQLITE_OPEN_CREATE;
3375 }
danielk19779a6284c2008-07-10 17:52:49 +00003376 }else if( strcmp(zArg, "-nomutex")==0 ){
3377 int b;
3378 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR;
3379 if( b ){
3380 flags |= SQLITE_OPEN_NOMUTEX;
drh039963a2008-09-03 00:43:15 +00003381 flags &= ~SQLITE_OPEN_FULLMUTEX;
danielk19779a6284c2008-07-10 17:52:49 +00003382 }else{
3383 flags &= ~SQLITE_OPEN_NOMUTEX;
3384 }
danc431fd52011-06-27 16:55:50 +00003385 }else if( strcmp(zArg, "-fullmutex")==0 ){
drh039963a2008-09-03 00:43:15 +00003386 int b;
3387 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR;
3388 if( b ){
3389 flags |= SQLITE_OPEN_FULLMUTEX;
3390 flags &= ~SQLITE_OPEN_NOMUTEX;
3391 }else{
3392 flags &= ~SQLITE_OPEN_FULLMUTEX;
3393 }
drhf12b3f62011-12-21 14:42:29 +00003394 }else if( strcmp(zArg, "-uri")==0 ){
3395 int b;
3396 if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR;
3397 if( b ){
3398 flags |= SQLITE_OPEN_URI;
3399 }else{
3400 flags &= ~SQLITE_OPEN_URI;
3401 }
drh3570ad92007-08-31 14:31:44 +00003402 }else{
3403 Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0);
3404 return TCL_ERROR;
drh22fbcb82004-02-01 01:22:50 +00003405 }
3406 }
drh3570ad92007-08-31 14:31:44 +00003407 if( objc<3 || (objc&1)!=1 ){
mistachkinb56660f2016-07-14 21:26:09 +00003408 Tcl_WrongNumArgs(interp, 1, objv,
drh3570ad92007-08-31 14:31:44 +00003409 "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?"
drh68bd4aa2012-01-13 16:16:10 +00003410 " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?"
drh32f57d42016-03-16 01:03:10 +00003411#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL)
drh3570ad92007-08-31 14:31:44 +00003412 " ?-key CODECKEY?"
drh22fbcb82004-02-01 01:22:50 +00003413#endif
3414 );
drh75897232000-05-29 14:26:00 +00003415 return TCL_ERROR;
3416 }
drh75897232000-05-29 14:26:00 +00003417 zErrMsg = 0;
drh4cdc9e82000-08-04 14:56:24 +00003418 p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
drhbec3f402000-08-04 13:49:02 +00003419 memset(p, 0, sizeof(*p));
drh22fbcb82004-02-01 01:22:50 +00003420 zFile = Tcl_GetStringFromObj(objv[2], 0);
drh882e8e42006-08-24 02:42:27 +00003421 zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename);
mistachkin540ebf82012-09-10 07:29:29 +00003422 rc = sqlite3_open_v2(zFile, &p->db, flags, zVfs);
drh882e8e42006-08-24 02:42:27 +00003423 Tcl_DStringFree(&translatedFilename);
mistachkin540ebf82012-09-10 07:29:29 +00003424 if( p->db ){
3425 if( SQLITE_OK!=sqlite3_errcode(p->db) ){
3426 zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db));
3427 sqlite3_close(p->db);
3428 p->db = 0;
3429 }
3430 }else{
mistachkin5dac8432012-09-11 02:00:25 +00003431 zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc));
danielk197780290862004-05-22 09:21:21 +00003432 }
drh32f57d42016-03-16 01:03:10 +00003433#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL)
drhf3a65f72007-08-22 20:18:21 +00003434 if( p->db ){
3435 sqlite3_key(p->db, pKey, nKey);
3436 }
drheb8ed702004-02-11 10:37:23 +00003437#endif
drhbec3f402000-08-04 13:49:02 +00003438 if( p->db==0 ){
drh75897232000-05-29 14:26:00 +00003439 Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
drhbec3f402000-08-04 13:49:02 +00003440 Tcl_Free((char*)p);
drh9404d502006-12-19 18:46:08 +00003441 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00003442 return TCL_ERROR;
3443 }
drhfb7e7652005-01-24 00:28:42 +00003444 p->maxStmt = NUM_PREPARED_STMTS;
drh147ef392016-01-22 23:17:51 +00003445 p->openFlags = flags & SQLITE_OPEN_URI;
drh5169bbc2006-08-24 14:59:45 +00003446 p->interp = interp;
drh22fbcb82004-02-01 01:22:50 +00003447 zArg = Tcl_GetStringFromObj(objv[1], 0);
dan4a4c11a2009-10-06 14:59:02 +00003448 if( DbUseNre() ){
drha2c8a952009-10-13 18:38:34 +00003449 Tcl_NRCreateCommand(interp, zArg, DbObjCmdAdaptor, DbObjCmd,
3450 (char*)p, DbDeleteCmd);
dan4a4c11a2009-10-06 14:59:02 +00003451 }else{
3452 Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd);
3453 }
drh75897232000-05-29 14:26:00 +00003454 return TCL_OK;
3455}
3456
3457/*
drh90ca9752001-09-28 17:47:14 +00003458** Provide a dummy Tcl_InitStubs if we are using this as a static
3459** library.
3460*/
3461#ifndef USE_TCL_STUBS
3462# undef Tcl_InitStubs
drh0e85ccf2013-06-03 12:34:46 +00003463# define Tcl_InitStubs(a,b,c) TCL_VERSION
drh90ca9752001-09-28 17:47:14 +00003464#endif
3465
3466/*
drh29bc4612005-10-05 10:40:15 +00003467** Make sure we have a PACKAGE_VERSION macro defined. This will be
3468** defined automatically by the TEA makefile. But other makefiles
3469** do not define it.
3470*/
3471#ifndef PACKAGE_VERSION
3472# define PACKAGE_VERSION SQLITE_VERSION
3473#endif
3474
3475/*
drh75897232000-05-29 14:26:00 +00003476** Initialize this module.
3477**
3478** This Tcl module contains only a single new Tcl command named "sqlite".
3479** (Hence there is no namespace. There is no point in using a namespace
3480** if the extension only supplies one new name!) The "sqlite" command is
3481** used to open a new SQLite database. See the DbMain() routine above
3482** for additional information.
drhb652f432010-08-26 16:46:57 +00003483**
3484** The EXTERN macros are required by TCL in order to work on windows.
drh75897232000-05-29 14:26:00 +00003485*/
drhb652f432010-08-26 16:46:57 +00003486EXTERN int Sqlite3_Init(Tcl_Interp *interp){
mistachkin27b2f052015-01-12 19:49:46 +00003487 int rc = Tcl_InitStubs(interp, "8.4", 0) ? TCL_OK : TCL_ERROR;
drh6dc8cbe2013-05-31 15:36:07 +00003488 if( rc==TCL_OK ){
3489 Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh1cca0d22010-08-25 20:35:51 +00003490#ifndef SQLITE_3_SUFFIX_ONLY
drh6dc8cbe2013-05-31 15:36:07 +00003491 /* The "sqlite" alias is undocumented. It is here only to support
3492 ** legacy scripts. All new scripts should use only the "sqlite3"
3493 ** command. */
3494 Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
drh4c0f1642010-08-25 19:39:19 +00003495#endif
drh6dc8cbe2013-05-31 15:36:07 +00003496 rc = Tcl_PkgProvide(interp, "sqlite3", PACKAGE_VERSION);
3497 }
3498 return rc;
drh90ca9752001-09-28 17:47:14 +00003499}
drhb652f432010-08-26 16:46:57 +00003500EXTERN int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
drhb652f432010-08-26 16:46:57 +00003501EXTERN int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
3502EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
drhe2c3a652008-09-23 09:58:46 +00003503
drhd878cab2012-03-20 15:10:42 +00003504/* Because it accesses the file-system and uses persistent state, SQLite
drhe75a9eb2016-02-13 18:54:10 +00003505** is not considered appropriate for safe interpreters. Hence, we cause
3506** the _SafeInit() interfaces return TCL_ERROR.
drhd878cab2012-03-20 15:10:42 +00003507*/
drhe75a9eb2016-02-13 18:54:10 +00003508EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_ERROR; }
3509EXTERN int Sqlite3_SafeUnload(Tcl_Interp *interp, int flags){return TCL_ERROR;}
3510
3511
drh49766d62005-01-08 18:42:28 +00003512
3513#ifndef SQLITE_3_SUFFIX_ONLY
dana3e63c42010-08-20 12:33:59 +00003514int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
3515int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
dana3e63c42010-08-20 12:33:59 +00003516int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
3517int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
drh49766d62005-01-08 18:42:28 +00003518#endif
drh75897232000-05-29 14:26:00 +00003519
drh3e27c022004-07-23 00:01:38 +00003520#ifdef TCLSH
3521/*****************************************************************************
drh57a02272009-10-22 20:52:05 +00003522** All of the code that follows is used to build standalone TCL interpreters
3523** that are statically linked with SQLite. Enable these by compiling
3524** with -DTCLSH=n where n can be 1 or 2. An n of 1 generates a standard
3525** tclsh but with SQLite built in. An n of 2 generates the SQLite space
3526** analysis program.
drh75897232000-05-29 14:26:00 +00003527*/
drh348784e2000-05-29 20:41:49 +00003528
drh57a02272009-10-22 20:52:05 +00003529#if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5)
3530/*
3531 * This code implements the MD5 message-digest algorithm.
3532 * The algorithm is due to Ron Rivest. This code was
3533 * written by Colin Plumb in 1993, no copyright is claimed.
3534 * This code is in the public domain; do with it what you wish.
3535 *
3536 * Equivalent code is available from RSA Data Security, Inc.
3537 * This code has been tested against that, and is equivalent,
3538 * except that you don't need to include two pages of legalese
3539 * with every copy.
3540 *
3541 * To compute the message digest of a chunk of bytes, declare an
3542 * MD5Context structure, pass it to MD5Init, call MD5Update as
3543 * needed on buffers full of bytes, and then call MD5Final, which
3544 * will fill a supplied 16-byte array with the digest.
3545 */
3546
3547/*
3548 * If compiled on a machine that doesn't have a 32-bit integer,
3549 * you just set "uint32" to the appropriate datatype for an
3550 * unsigned 32-bit integer. For example:
3551 *
3552 * cc -Duint32='unsigned long' md5.c
3553 *
3554 */
3555#ifndef uint32
3556# define uint32 unsigned int
3557#endif
3558
3559struct MD5Context {
3560 int isInit;
3561 uint32 buf[4];
3562 uint32 bits[2];
3563 unsigned char in[64];
3564};
3565typedef struct MD5Context MD5Context;
3566
3567/*
3568 * Note: this code is harmless on little-endian machines.
3569 */
3570static void byteReverse (unsigned char *buf, unsigned longs){
3571 uint32 t;
3572 do {
3573 t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 |
3574 ((unsigned)buf[1]<<8 | buf[0]);
3575 *(uint32 *)buf = t;
3576 buf += 4;
3577 } while (--longs);
3578}
3579/* The four core functions - F1 is optimized somewhat */
3580
3581/* #define F1(x, y, z) (x & y | ~x & z) */
3582#define F1(x, y, z) (z ^ (x & (y ^ z)))
3583#define F2(x, y, z) F1(z, x, y)
3584#define F3(x, y, z) (x ^ y ^ z)
3585#define F4(x, y, z) (y ^ (x | ~z))
3586
3587/* This is the central step in the MD5 algorithm. */
3588#define MD5STEP(f, w, x, y, z, data, s) \
3589 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
3590
3591/*
3592 * The core of the MD5 algorithm, this alters an existing MD5 hash to
3593 * reflect the addition of 16 longwords of new data. MD5Update blocks
3594 * the data and converts bytes into longwords for this routine.
3595 */
3596static void MD5Transform(uint32 buf[4], const uint32 in[16]){
3597 register uint32 a, b, c, d;
3598
3599 a = buf[0];
3600 b = buf[1];
3601 c = buf[2];
3602 d = buf[3];
3603
3604 MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7);
3605 MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12);
3606 MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17);
3607 MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22);
3608 MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7);
3609 MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12);
3610 MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17);
3611 MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22);
3612 MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7);
3613 MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12);
3614 MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17);
3615 MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22);
3616 MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7);
3617 MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12);
3618 MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17);
3619 MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22);
3620
3621 MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5);
3622 MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9);
3623 MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14);
3624 MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20);
3625 MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5);
3626 MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9);
3627 MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14);
3628 MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20);
3629 MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5);
3630 MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9);
3631 MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14);
3632 MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20);
3633 MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5);
3634 MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9);
3635 MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14);
3636 MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20);
3637
3638 MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4);
3639 MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11);
3640 MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16);
3641 MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23);
3642 MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4);
3643 MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11);
3644 MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16);
3645 MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23);
3646 MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4);
3647 MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11);
3648 MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16);
3649 MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23);
3650 MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4);
3651 MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11);
3652 MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16);
3653 MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23);
3654
3655 MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6);
3656 MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10);
3657 MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15);
3658 MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21);
3659 MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6);
3660 MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10);
3661 MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15);
3662 MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21);
3663 MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6);
3664 MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10);
3665 MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15);
3666 MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21);
3667 MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6);
3668 MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10);
3669 MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15);
3670 MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21);
3671
3672 buf[0] += a;
3673 buf[1] += b;
3674 buf[2] += c;
3675 buf[3] += d;
3676}
3677
3678/*
3679 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
3680 * initialization constants.
3681 */
3682static void MD5Init(MD5Context *ctx){
3683 ctx->isInit = 1;
3684 ctx->buf[0] = 0x67452301;
3685 ctx->buf[1] = 0xefcdab89;
3686 ctx->buf[2] = 0x98badcfe;
3687 ctx->buf[3] = 0x10325476;
3688 ctx->bits[0] = 0;
3689 ctx->bits[1] = 0;
3690}
3691
3692/*
3693 * Update context to reflect the concatenation of another buffer full
3694 * of bytes.
3695 */
mistachkinb56660f2016-07-14 21:26:09 +00003696static
drh57a02272009-10-22 20:52:05 +00003697void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){
3698 uint32 t;
3699
3700 /* Update bitcount */
3701
3702 t = ctx->bits[0];
3703 if ((ctx->bits[0] = t + ((uint32)len << 3)) < t)
3704 ctx->bits[1]++; /* Carry from low to high */
3705 ctx->bits[1] += len >> 29;
3706
3707 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
3708
3709 /* Handle any leading odd-sized chunks */
3710
3711 if ( t ) {
3712 unsigned char *p = (unsigned char *)ctx->in + t;
3713
3714 t = 64-t;
3715 if (len < t) {
3716 memcpy(p, buf, len);
3717 return;
3718 }
3719 memcpy(p, buf, t);
3720 byteReverse(ctx->in, 16);
3721 MD5Transform(ctx->buf, (uint32 *)ctx->in);
3722 buf += t;
3723 len -= t;
3724 }
3725
3726 /* Process data in 64-byte chunks */
3727
3728 while (len >= 64) {
3729 memcpy(ctx->in, buf, 64);
3730 byteReverse(ctx->in, 16);
3731 MD5Transform(ctx->buf, (uint32 *)ctx->in);
3732 buf += 64;
3733 len -= 64;
3734 }
3735
3736 /* Handle any remaining bytes of data. */
3737
3738 memcpy(ctx->in, buf, len);
3739}
3740
3741/*
mistachkinb56660f2016-07-14 21:26:09 +00003742 * Final wrapup - pad to 64-byte boundary with the bit pattern
drh57a02272009-10-22 20:52:05 +00003743 * 1 0* (64-bit count of bits processed, MSB-first)
3744 */
3745static void MD5Final(unsigned char digest[16], MD5Context *ctx){
3746 unsigned count;
3747 unsigned char *p;
3748
3749 /* Compute number of bytes mod 64 */
3750 count = (ctx->bits[0] >> 3) & 0x3F;
3751
3752 /* Set the first char of padding to 0x80. This is safe since there is
3753 always at least one byte free */
3754 p = ctx->in + count;
3755 *p++ = 0x80;
3756
3757 /* Bytes of padding needed to make 64 bytes */
3758 count = 64 - 1 - count;
3759
3760 /* Pad out to 56 mod 64 */
3761 if (count < 8) {
3762 /* Two lots of padding: Pad the first block to 64 bytes */
3763 memset(p, 0, count);
3764 byteReverse(ctx->in, 16);
3765 MD5Transform(ctx->buf, (uint32 *)ctx->in);
3766
3767 /* Now fill the next block with 56 bytes */
3768 memset(ctx->in, 0, 56);
3769 } else {
3770 /* Pad block to 56 bytes */
3771 memset(p, 0, count-8);
3772 }
3773 byteReverse(ctx->in, 14);
3774
3775 /* Append length in bits and transform */
drha47941f2013-12-20 18:57:44 +00003776 memcpy(ctx->in + 14*4, ctx->bits, 8);
drh57a02272009-10-22 20:52:05 +00003777
3778 MD5Transform(ctx->buf, (uint32 *)ctx->in);
3779 byteReverse((unsigned char *)ctx->buf, 4);
3780 memcpy(digest, ctx->buf, 16);
drh57a02272009-10-22 20:52:05 +00003781}
3782
3783/*
3784** Convert a 128-bit MD5 digest into a 32-digit base-16 number.
3785*/
3786static void MD5DigestToBase16(unsigned char *digest, char *zBuf){
3787 static char const zEncode[] = "0123456789abcdef";
3788 int i, j;
3789
3790 for(j=i=0; i<16; i++){
3791 int a = digest[i];
3792 zBuf[j++] = zEncode[(a>>4)&0xf];
3793 zBuf[j++] = zEncode[a & 0xf];
3794 }
3795 zBuf[j] = 0;
3796}
3797
3798
3799/*
3800** Convert a 128-bit MD5 digest into sequency of eight 5-digit integers
3801** each representing 16 bits of the digest and separated from each
3802** other by a "-" character.
3803*/
3804static void MD5DigestToBase10x8(unsigned char digest[16], char zDigest[50]){
3805 int i, j;
3806 unsigned int x;
3807 for(i=j=0; i<16; i+=2){
3808 x = digest[i]*256 + digest[i+1];
3809 if( i>0 ) zDigest[j++] = '-';
drh05f6c672015-02-26 16:32:33 +00003810 sqlite3_snprintf(50-j, &zDigest[j], "%05u", x);
drh57a02272009-10-22 20:52:05 +00003811 j += 5;
3812 }
3813 zDigest[j] = 0;
3814}
3815
3816/*
3817** A TCL command for md5. The argument is the text to be hashed. The
mistachkinb56660f2016-07-14 21:26:09 +00003818** Result is the hash in base64.
drh57a02272009-10-22 20:52:05 +00003819*/
mistachkin44e95d42016-07-28 22:23:26 +00003820static int SQLITE_TCLAPI md5_cmd(
3821 void*cd,
3822 Tcl_Interp *interp,
3823 int argc,
3824 const char **argv
3825){
drh57a02272009-10-22 20:52:05 +00003826 MD5Context ctx;
3827 unsigned char digest[16];
3828 char zBuf[50];
3829 void (*converter)(unsigned char*, char*);
3830
3831 if( argc!=2 ){
mistachkinb56660f2016-07-14 21:26:09 +00003832 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
drha198f2b2014-02-07 19:26:13 +00003833 " TEXT\"", (char*)0);
drh57a02272009-10-22 20:52:05 +00003834 return TCL_ERROR;
3835 }
3836 MD5Init(&ctx);
3837 MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1]));
3838 MD5Final(digest, &ctx);
3839 converter = (void(*)(unsigned char*,char*))cd;
3840 converter(digest, zBuf);
3841 Tcl_AppendResult(interp, zBuf, (char*)0);
3842 return TCL_OK;
3843}
3844
3845/*
3846** A TCL command to take the md5 hash of a file. The argument is the
3847** name of the file.
3848*/
mistachkin44e95d42016-07-28 22:23:26 +00003849static int SQLITE_TCLAPI md5file_cmd(
3850 void*cd,
3851 Tcl_Interp *interp,
3852 int argc,
3853 const char **argv
3854){
drh57a02272009-10-22 20:52:05 +00003855 FILE *in;
3856 MD5Context ctx;
3857 void (*converter)(unsigned char*, char*);
3858 unsigned char digest[16];
3859 char zBuf[10240];
3860
3861 if( argc!=2 ){
mistachkinb56660f2016-07-14 21:26:09 +00003862 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
drha198f2b2014-02-07 19:26:13 +00003863 " FILENAME\"", (char*)0);
drh57a02272009-10-22 20:52:05 +00003864 return TCL_ERROR;
3865 }
3866 in = fopen(argv[1],"rb");
3867 if( in==0 ){
mistachkinb56660f2016-07-14 21:26:09 +00003868 Tcl_AppendResult(interp,"unable to open file \"", argv[1],
drha198f2b2014-02-07 19:26:13 +00003869 "\" for reading", (char*)0);
drh57a02272009-10-22 20:52:05 +00003870 return TCL_ERROR;
3871 }
3872 MD5Init(&ctx);
3873 for(;;){
3874 int n;
drh83cc1392012-04-19 18:04:28 +00003875 n = (int)fread(zBuf, 1, sizeof(zBuf), in);
drh57a02272009-10-22 20:52:05 +00003876 if( n<=0 ) break;
3877 MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n);
3878 }
3879 fclose(in);
3880 MD5Final(digest, &ctx);
3881 converter = (void(*)(unsigned char*,char*))cd;
3882 converter(digest, zBuf);
3883 Tcl_AppendResult(interp, zBuf, (char*)0);
3884 return TCL_OK;
3885}
3886
3887/*
3888** Register the four new TCL commands for generating MD5 checksums
3889** with the TCL interpreter.
3890*/
3891int Md5_Init(Tcl_Interp *interp){
3892 Tcl_CreateCommand(interp, "md5", (Tcl_CmdProc*)md5_cmd,
3893 MD5DigestToBase16, 0);
3894 Tcl_CreateCommand(interp, "md5-10x8", (Tcl_CmdProc*)md5_cmd,
3895 MD5DigestToBase10x8, 0);
3896 Tcl_CreateCommand(interp, "md5file", (Tcl_CmdProc*)md5file_cmd,
3897 MD5DigestToBase16, 0);
3898 Tcl_CreateCommand(interp, "md5file-10x8", (Tcl_CmdProc*)md5file_cmd,
3899 MD5DigestToBase10x8, 0);
3900 return TCL_OK;
3901}
3902#endif /* defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) */
3903
3904#if defined(SQLITE_TEST)
3905/*
3906** During testing, the special md5sum() aggregate function is available.
3907** inside SQLite. The following routines implement that function.
3908*/
3909static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){
3910 MD5Context *p;
3911 int i;
3912 if( argc<1 ) return;
3913 p = sqlite3_aggregate_context(context, sizeof(*p));
3914 if( p==0 ) return;
3915 if( !p->isInit ){
3916 MD5Init(p);
3917 }
3918 for(i=0; i<argc; i++){
3919 const char *zData = (char*)sqlite3_value_text(argv[i]);
3920 if( zData ){
drh83cc1392012-04-19 18:04:28 +00003921 MD5Update(p, (unsigned char*)zData, (int)strlen(zData));
drh57a02272009-10-22 20:52:05 +00003922 }
3923 }
3924}
3925static void md5finalize(sqlite3_context *context){
3926 MD5Context *p;
3927 unsigned char digest[16];
3928 char zBuf[33];
3929 p = sqlite3_aggregate_context(context, sizeof(*p));
3930 MD5Final(digest,p);
3931 MD5DigestToBase16(digest, zBuf);
3932 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
3933}
mistachkin44e95d42016-07-28 22:23:26 +00003934int Md5_Register(
3935 sqlite3 *db,
3936 char **pzErrMsg,
mistachkin85bd9822016-07-28 22:53:10 +00003937 const sqlite3_api_routines *pThunk
mistachkin44e95d42016-07-28 22:23:26 +00003938){
mistachkinb56660f2016-07-14 21:26:09 +00003939 int rc = sqlite3_create_function(db, "md5sum", -1, SQLITE_UTF8, 0, 0,
drh57a02272009-10-22 20:52:05 +00003940 md5step, md5finalize);
3941 sqlite3_overload_function(db, "md5sum", -1); /* To exercise this API */
3942 return rc;
3943}
3944#endif /* defined(SQLITE_TEST) */
3945
3946
drh348784e2000-05-29 20:41:49 +00003947/*
drh3e27c022004-07-23 00:01:38 +00003948** If the macro TCLSH is one, then put in code this for the
3949** "main" routine that will initialize Tcl and take input from
drh3570ad92007-08-31 14:31:44 +00003950** standard input, or if a file is named on the command line
3951** the TCL interpreter reads and evaluates that file.
drh348784e2000-05-29 20:41:49 +00003952*/
drh3e27c022004-07-23 00:01:38 +00003953#if TCLSH==1
dan0ae479d2011-09-21 16:43:07 +00003954static const char *tclsh_main_loop(void){
3955 static const char zMainloop[] =
3956 "set line {}\n"
3957 "while {![eof stdin]} {\n"
3958 "if {$line!=\"\"} {\n"
3959 "puts -nonewline \"> \"\n"
3960 "} else {\n"
3961 "puts -nonewline \"% \"\n"
drh348784e2000-05-29 20:41:49 +00003962 "}\n"
dan0ae479d2011-09-21 16:43:07 +00003963 "flush stdout\n"
3964 "append line [gets stdin]\n"
3965 "if {[info complete $line]} {\n"
3966 "if {[catch {uplevel #0 $line} result]} {\n"
3967 "puts stderr \"Error: $result\"\n"
3968 "} elseif {$result!=\"\"} {\n"
3969 "puts $result\n"
3970 "}\n"
3971 "set line {}\n"
3972 "} else {\n"
3973 "append line \\n\n"
3974 "}\n"
drh348784e2000-05-29 20:41:49 +00003975 "}\n"
dan0ae479d2011-09-21 16:43:07 +00003976 ;
3977 return zMainloop;
3978}
drh3e27c022004-07-23 00:01:38 +00003979#endif
drh3a0f13f2010-07-12 16:47:48 +00003980#if TCLSH==2
dan0ae479d2011-09-21 16:43:07 +00003981static const char *tclsh_main_loop(void);
drh3a0f13f2010-07-12 16:47:48 +00003982#endif
drh3e27c022004-07-23 00:01:38 +00003983
danc1a60c52010-06-07 14:28:16 +00003984#ifdef SQLITE_TEST
3985static void init_all(Tcl_Interp *);
mistachkin7617e4a2016-07-28 17:11:20 +00003986static int SQLITE_TCLAPI init_all_cmd(
danc1a60c52010-06-07 14:28:16 +00003987 ClientData cd,
3988 Tcl_Interp *interp,
3989 int objc,
3990 Tcl_Obj *CONST objv[]
3991){
danielk19770a549072009-02-17 16:29:10 +00003992
danc1a60c52010-06-07 14:28:16 +00003993 Tcl_Interp *slave;
3994 if( objc!=2 ){
3995 Tcl_WrongNumArgs(interp, 1, objv, "SLAVE");
3996 return TCL_ERROR;
3997 }
3998
3999 slave = Tcl_GetSlave(interp, Tcl_GetString(objv[1]));
4000 if( !slave ){
4001 return TCL_ERROR;
4002 }
4003
4004 init_all(slave);
4005 return TCL_OK;
4006}
danc431fd52011-06-27 16:55:50 +00004007
4008/*
4009** Tclcmd: db_use_legacy_prepare DB BOOLEAN
4010**
4011** The first argument to this command must be a database command created by
4012** [sqlite3]. If the second argument is true, then the handle is configured
4013** to use the sqlite3_prepare_v2() function to prepare statements. If it
4014** is false, sqlite3_prepare().
4015*/
mistachkin7617e4a2016-07-28 17:11:20 +00004016static int SQLITE_TCLAPI db_use_legacy_prepare_cmd(
danc431fd52011-06-27 16:55:50 +00004017 ClientData cd,
4018 Tcl_Interp *interp,
4019 int objc,
4020 Tcl_Obj *CONST objv[]
4021){
4022 Tcl_CmdInfo cmdInfo;
4023 SqliteDb *pDb;
4024 int bPrepare;
4025
4026 if( objc!=3 ){
4027 Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
4028 return TCL_ERROR;
4029 }
4030
4031 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
4032 Tcl_AppendResult(interp, "no such db: ", Tcl_GetString(objv[1]), (char*)0);
4033 return TCL_ERROR;
4034 }
4035 pDb = (SqliteDb*)cmdInfo.objClientData;
4036 if( Tcl_GetBooleanFromObj(interp, objv[2], &bPrepare) ){
4037 return TCL_ERROR;
4038 }
4039
4040 pDb->bLegacyPrepare = bPrepare;
4041
4042 Tcl_ResetResult(interp);
4043 return TCL_OK;
4044}
dan04489b62014-10-31 20:11:32 +00004045
4046/*
4047** Tclcmd: db_last_stmt_ptr DB
4048**
4049** If the statement cache associated with database DB is not empty,
4050** return the text representation of the most recently used statement
4051** handle.
4052*/
mistachkin7617e4a2016-07-28 17:11:20 +00004053static int SQLITE_TCLAPI db_last_stmt_ptr(
dan04489b62014-10-31 20:11:32 +00004054 ClientData cd,
4055 Tcl_Interp *interp,
4056 int objc,
4057 Tcl_Obj *CONST objv[]
4058){
4059 extern int sqlite3TestMakePointerStr(Tcl_Interp*, char*, void*);
4060 Tcl_CmdInfo cmdInfo;
4061 SqliteDb *pDb;
4062 sqlite3_stmt *pStmt = 0;
4063 char zBuf[100];
4064
4065 if( objc!=2 ){
4066 Tcl_WrongNumArgs(interp, 1, objv, "DB");
4067 return TCL_ERROR;
4068 }
4069
4070 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
4071 Tcl_AppendResult(interp, "no such db: ", Tcl_GetString(objv[1]), (char*)0);
4072 return TCL_ERROR;
4073 }
4074 pDb = (SqliteDb*)cmdInfo.objClientData;
4075
4076 if( pDb->stmtList ) pStmt = pDb->stmtList->pStmt;
4077 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ){
4078 return TCL_ERROR;
4079 }
4080 Tcl_SetResult(interp, zBuf, TCL_VOLATILE);
4081
4082 return TCL_OK;
4083}
drh1a4a6802015-05-04 18:31:09 +00004084#endif /* SQLITE_TEST */
4085
danc1a60c52010-06-07 14:28:16 +00004086/*
4087** Configure the interpreter passed as the first argument to have access
4088** to the commands and linked variables that make up:
4089**
mistachkinb56660f2016-07-14 21:26:09 +00004090** * the [sqlite3] extension itself,
danc1a60c52010-06-07 14:28:16 +00004091**
4092** * If SQLITE_TCLMD5 or SQLITE_TEST is defined, the Md5 commands, and
4093**
4094** * If SQLITE_TEST is set, the various test interfaces used by the Tcl
4095** test suite.
4096*/
4097static void init_all(Tcl_Interp *interp){
drh38f82712004-06-18 17:10:16 +00004098 Sqlite3_Init(interp);
danc1a60c52010-06-07 14:28:16 +00004099
drh57a02272009-10-22 20:52:05 +00004100#if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5)
4101 Md5_Init(interp);
4102#endif
danc1a60c52010-06-07 14:28:16 +00004103
drhd9b02572001-04-15 00:37:09 +00004104#ifdef SQLITE_TEST
drhd1bf3512001-04-07 15:24:33 +00004105 {
drh2f999a62007-08-15 19:16:43 +00004106 extern int Sqliteconfig_Init(Tcl_Interp*);
drhd1bf3512001-04-07 15:24:33 +00004107 extern int Sqlitetest1_Init(Tcl_Interp*);
drh5c4d9702001-08-20 00:33:58 +00004108 extern int Sqlitetest2_Init(Tcl_Interp*);
4109 extern int Sqlitetest3_Init(Tcl_Interp*);
drha6064dc2003-12-19 02:52:05 +00004110 extern int Sqlitetest4_Init(Tcl_Interp*);
danielk1977998b56c2004-05-06 23:37:52 +00004111 extern int Sqlitetest5_Init(Tcl_Interp*);
drh9c06c952005-11-26 00:25:00 +00004112 extern int Sqlitetest6_Init(Tcl_Interp*);
drh29c636b2006-01-09 23:40:25 +00004113 extern int Sqlitetest7_Init(Tcl_Interp*);
drhb9bb7c12006-06-11 23:41:55 +00004114 extern int Sqlitetest8_Init(Tcl_Interp*);
danielk1977a713f2c2007-03-29 12:19:11 +00004115 extern int Sqlitetest9_Init(Tcl_Interp*);
drh23669402006-01-09 17:29:52 +00004116 extern int Sqlitetestasync_Init(Tcl_Interp*);
drh1409be62006-08-23 20:07:20 +00004117 extern int Sqlitetest_autoext_Init(Tcl_Interp*);
danb391b942014-11-07 14:41:11 +00004118 extern int Sqlitetest_blob_Init(Tcl_Interp*);
dan0a7a9152010-04-07 07:57:38 +00004119 extern int Sqlitetest_demovfs_Init(Tcl_Interp *);
drh984bfaa2008-03-19 16:08:53 +00004120 extern int Sqlitetest_func_Init(Tcl_Interp*);
drh15926592007-04-06 15:02:13 +00004121 extern int Sqlitetest_hexio_Init(Tcl_Interp*);
dane1ab2192009-08-17 15:16:19 +00004122 extern int Sqlitetest_init_Init(Tcl_Interp*);
drh2f999a62007-08-15 19:16:43 +00004123 extern int Sqlitetest_malloc_Init(Tcl_Interp*);
danielk19771a9ed0b2008-06-18 09:45:56 +00004124 extern int Sqlitetest_mutex_Init(Tcl_Interp*);
drh2f999a62007-08-15 19:16:43 +00004125 extern int Sqlitetestschema_Init(Tcl_Interp*);
4126 extern int Sqlitetestsse_Init(Tcl_Interp*);
4127 extern int Sqlitetesttclvar_Init(Tcl_Interp*);
dan9f5ff372013-01-11 09:58:54 +00004128 extern int Sqlitetestfs_Init(Tcl_Interp*);
danielk197744918fa2007-09-07 11:29:25 +00004129 extern int SqlitetestThread_Init(Tcl_Interp*);
danielk1977a15db352007-09-14 16:20:00 +00004130 extern int SqlitetestOnefile_Init();
danielk19775d1f5aa2008-04-10 14:51:00 +00004131 extern int SqlitetestOsinst_Init(Tcl_Interp*);
danielk197704103022009-02-03 16:51:24 +00004132 extern int Sqlitetestbackup_Init(Tcl_Interp*);
drh522efc62009-11-10 17:24:37 +00004133 extern int Sqlitetestintarray_Init(Tcl_Interp*);
danc7991bd2010-05-05 19:04:59 +00004134 extern int Sqlitetestvfs_Init(Tcl_Interp *);
dan9508daa2010-08-28 18:58:00 +00004135 extern int Sqlitetestrtree_Init(Tcl_Interp*);
dan8cf35eb2010-09-01 11:40:05 +00004136 extern int Sqlitequota_Init(Tcl_Interp*);
shaneh8a922f72010-11-04 20:50:27 +00004137 extern int Sqlitemultiplex_Init(Tcl_Interp*);
dane336b002010-11-19 18:20:09 +00004138 extern int SqliteSuperlock_Init(Tcl_Interp*);
dan213ca0a2011-03-28 19:10:06 +00004139 extern int SqlitetestSyscall_Init(Tcl_Interp*);
drh9b1c62d2011-03-30 21:04:43 +00004140#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
dan4fccf432011-03-08 19:22:50 +00004141 extern int TestSession_Init(Tcl_Interp*);
4142#endif
dan89a89562014-12-01 20:05:00 +00004143 extern int Fts5tcl_Init(Tcl_Interp *);
drhcfb8f8d2015-07-23 20:44:49 +00004144 extern int SqliteRbu_Init(Tcl_Interp*);
dan8e4251b2016-03-01 18:07:43 +00004145 extern int Sqlitetesttcl_Init(Tcl_Interp*);
dan6764a702011-06-20 11:15:06 +00004146#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
dan99ebad92011-06-13 09:11:01 +00004147 extern int Sqlitetestfts3_Init(Tcl_Interp *interp);
4148#endif
4149
danb29010c2010-12-29 18:24:38 +00004150#ifdef SQLITE_ENABLE_ZIPVFS
4151 extern int Zipvfs_Init(Tcl_Interp*);
4152 Zipvfs_Init(interp);
4153#endif
4154
drh2f999a62007-08-15 19:16:43 +00004155 Sqliteconfig_Init(interp);
danielk19776490beb2004-05-11 06:17:21 +00004156 Sqlitetest1_Init(interp);
drh5c4d9702001-08-20 00:33:58 +00004157 Sqlitetest2_Init(interp);
drhde647132004-05-07 17:57:49 +00004158 Sqlitetest3_Init(interp);
danielk1977fc57d7b2004-05-26 02:04:57 +00004159 Sqlitetest4_Init(interp);
danielk1977998b56c2004-05-06 23:37:52 +00004160 Sqlitetest5_Init(interp);
drh9c06c952005-11-26 00:25:00 +00004161 Sqlitetest6_Init(interp);
drh29c636b2006-01-09 23:40:25 +00004162 Sqlitetest7_Init(interp);
drhb9bb7c12006-06-11 23:41:55 +00004163 Sqlitetest8_Init(interp);
danielk1977a713f2c2007-03-29 12:19:11 +00004164 Sqlitetest9_Init(interp);
drh23669402006-01-09 17:29:52 +00004165 Sqlitetestasync_Init(interp);
drh1409be62006-08-23 20:07:20 +00004166 Sqlitetest_autoext_Init(interp);
danb391b942014-11-07 14:41:11 +00004167 Sqlitetest_blob_Init(interp);
dan0a7a9152010-04-07 07:57:38 +00004168 Sqlitetest_demovfs_Init(interp);
drh984bfaa2008-03-19 16:08:53 +00004169 Sqlitetest_func_Init(interp);
drh15926592007-04-06 15:02:13 +00004170 Sqlitetest_hexio_Init(interp);
dane1ab2192009-08-17 15:16:19 +00004171 Sqlitetest_init_Init(interp);
drh2f999a62007-08-15 19:16:43 +00004172 Sqlitetest_malloc_Init(interp);
danielk19771a9ed0b2008-06-18 09:45:56 +00004173 Sqlitetest_mutex_Init(interp);
drh2f999a62007-08-15 19:16:43 +00004174 Sqlitetestschema_Init(interp);
4175 Sqlitetesttclvar_Init(interp);
dan9f5ff372013-01-11 09:58:54 +00004176 Sqlitetestfs_Init(interp);
danielk197744918fa2007-09-07 11:29:25 +00004177 SqlitetestThread_Init(interp);
mistachkin52b1dbb2016-07-28 14:37:04 +00004178 SqlitetestOnefile_Init();
danielk19775d1f5aa2008-04-10 14:51:00 +00004179 SqlitetestOsinst_Init(interp);
danielk197704103022009-02-03 16:51:24 +00004180 Sqlitetestbackup_Init(interp);
drh522efc62009-11-10 17:24:37 +00004181 Sqlitetestintarray_Init(interp);
danc7991bd2010-05-05 19:04:59 +00004182 Sqlitetestvfs_Init(interp);
dan9508daa2010-08-28 18:58:00 +00004183 Sqlitetestrtree_Init(interp);
dan8cf35eb2010-09-01 11:40:05 +00004184 Sqlitequota_Init(interp);
shaneh8a922f72010-11-04 20:50:27 +00004185 Sqlitemultiplex_Init(interp);
dane336b002010-11-19 18:20:09 +00004186 SqliteSuperlock_Init(interp);
dan213ca0a2011-03-28 19:10:06 +00004187 SqlitetestSyscall_Init(interp);
drh9b1c62d2011-03-30 21:04:43 +00004188#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
dan4fccf432011-03-08 19:22:50 +00004189 TestSession_Init(interp);
4190#endif
dan89a89562014-12-01 20:05:00 +00004191 Fts5tcl_Init(interp);
drhcfb8f8d2015-07-23 20:44:49 +00004192 SqliteRbu_Init(interp);
dan8e4251b2016-03-01 18:07:43 +00004193 Sqlitetesttcl_Init(interp);
danielk1977a15db352007-09-14 16:20:00 +00004194
dan6764a702011-06-20 11:15:06 +00004195#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
dan99ebad92011-06-13 09:11:01 +00004196 Sqlitetestfts3_Init(interp);
4197#endif
drh2e66f0b2005-04-28 17:18:48 +00004198
danc431fd52011-06-27 16:55:50 +00004199 Tcl_CreateObjCommand(
4200 interp, "load_testfixture_extensions", init_all_cmd, 0, 0
4201 );
4202 Tcl_CreateObjCommand(
4203 interp, "db_use_legacy_prepare", db_use_legacy_prepare_cmd, 0, 0
4204 );
dan04489b62014-10-31 20:11:32 +00004205 Tcl_CreateObjCommand(
4206 interp, "db_last_stmt_ptr", db_last_stmt_ptr, 0, 0
4207 );
danc1a60c52010-06-07 14:28:16 +00004208
drh3e27c022004-07-23 00:01:38 +00004209#ifdef SQLITE_SSE
drh348784e2000-05-29 20:41:49 +00004210 Sqlitetestsse_Init(interp);
4211#endif
4212 }
drh61212b62004-12-02 20:17:00 +00004213#endif
danc1a60c52010-06-07 14:28:16 +00004214}
4215
drhdb6bafa2015-01-09 21:54:58 +00004216/* Needed for the setrlimit() system call on unix */
4217#if defined(unix)
4218#include <sys/resource.h>
4219#endif
4220
danc1a60c52010-06-07 14:28:16 +00004221#define TCLSH_MAIN main /* Needed to fake out mktclapp */
mistachkin69def7f2016-07-28 04:14:37 +00004222int SQLITE_CDECL TCLSH_MAIN(int argc, char **argv){
danc1a60c52010-06-07 14:28:16 +00004223 Tcl_Interp *interp;
mistachkin1f28e072013-08-15 08:06:15 +00004224
4225#if !defined(_WIN32_WCE)
4226 if( getenv("BREAK") ){
4227 fprintf(stderr,
4228 "attach debugger to process %d and press any key to continue.\n",
4229 GETPID());
4230 fgetc(stdin);
4231 }
4232#endif
4233
drhdb6bafa2015-01-09 21:54:58 +00004234 /* Since the primary use case for this binary is testing of SQLite,
4235 ** be sure to generate core files if we crash */
4236#if defined(SQLITE_TEST) && defined(unix)
4237 { struct rlimit x;
4238 getrlimit(RLIMIT_CORE, &x);
4239 x.rlim_cur = x.rlim_max;
4240 setrlimit(RLIMIT_CORE, &x);
4241 }
4242#endif /* SQLITE_TEST && unix */
4243
4244
danc1a60c52010-06-07 14:28:16 +00004245 /* Call sqlite3_shutdown() once before doing anything else. This is to
4246 ** test that sqlite3_shutdown() can be safely called by a process before
4247 ** sqlite3_initialize() is. */
4248 sqlite3_shutdown();
4249
dan0ae479d2011-09-21 16:43:07 +00004250 Tcl_FindExecutable(argv[0]);
mistachkin2953ba92014-02-14 00:25:03 +00004251 Tcl_SetSystemEncoding(NULL, "utf-8");
dan0ae479d2011-09-21 16:43:07 +00004252 interp = Tcl_CreateInterp();
4253
drh3a0f13f2010-07-12 16:47:48 +00004254#if TCLSH==2
4255 sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
4256#endif
danc1a60c52010-06-07 14:28:16 +00004257
danc1a60c52010-06-07 14:28:16 +00004258 init_all(interp);
drhc7285972009-11-10 01:13:25 +00004259 if( argc>=2 ){
drh348784e2000-05-29 20:41:49 +00004260 int i;
shessad42c3a2006-08-22 23:53:46 +00004261 char zArgc[32];
4262 sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH));
4263 Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY);
drh348784e2000-05-29 20:41:49 +00004264 Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
4265 Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
4266 for(i=3-TCLSH; i<argc; i++){
4267 Tcl_SetVar(interp, "argv", argv[i],
4268 TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
4269 }
drh3a0f13f2010-07-12 16:47:48 +00004270 if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
drh0de8c112002-07-06 16:32:14 +00004271 const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
drha81c64a2009-01-14 23:38:02 +00004272 if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp);
drhc61053b2000-06-04 12:58:36 +00004273 fprintf(stderr,"%s: %s\n", *argv, zInfo);
drh348784e2000-05-29 20:41:49 +00004274 return 1;
4275 }
drh3e27c022004-07-23 00:01:38 +00004276 }
drh3a0f13f2010-07-12 16:47:48 +00004277 if( TCLSH==2 || argc<=1 ){
dan0ae479d2011-09-21 16:43:07 +00004278 Tcl_GlobalEval(interp, tclsh_main_loop());
drh348784e2000-05-29 20:41:49 +00004279 }
4280 return 0;
4281}
4282#endif /* TCLSH */