xref: /sqlite-3.40.0/src/test1.c (revision 02267cc2)
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    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.
10 **
11 *************************************************************************
12 ** Code for testing all sorts of SQLite interfaces.  This code
13 ** is not included in the SQLite library.  It is used for automated
14 ** testing of the SQLite library.
15 */
16 #include "sqliteInt.h"
17 #if SQLITE_OS_WIN
18 #  include "os_win.h"
19 #endif
20 
21 #include "vdbeInt.h"
22 #include "tcl.h"
23 #include <stdlib.h>
24 #include <string.h>
25 
26 /*
27 ** This is a copy of the first part of the SqliteDb structure in
28 ** tclsqlite.c.  We need it here so that the get_sqlite_pointer routine
29 ** can extract the sqlite3* pointer from an existing Tcl SQLite
30 ** connection.
31 */
32 struct SqliteDb {
33   sqlite3 *db;
34 };
35 
36 /*
37 ** Convert text generated by the "%p" conversion format back into
38 ** a pointer.
39 */
40 static int testHexToInt(int h){
41   if( h>='0' && h<='9' ){
42     return h - '0';
43   }else if( h>='a' && h<='f' ){
44     return h - 'a' + 10;
45   }else{
46     assert( h>='A' && h<='F' );
47     return h - 'A' + 10;
48   }
49 }
50 void *sqlite3TestTextToPtr(const char *z){
51   void *p;
52   u64 v;
53   u32 v2;
54   if( z[0]=='0' && z[1]=='x' ){
55     z += 2;
56   }
57   v = 0;
58   while( *z ){
59     v = (v<<4) + testHexToInt(*z);
60     z++;
61   }
62   if( sizeof(p)==sizeof(v) ){
63     memcpy(&p, &v, sizeof(p));
64   }else{
65     assert( sizeof(p)==sizeof(v2) );
66     v2 = (u32)v;
67     memcpy(&p, &v2, sizeof(p));
68   }
69   return p;
70 }
71 
72 
73 /*
74 ** A TCL command that returns the address of the sqlite* pointer
75 ** for an sqlite connection instance.  Bad things happen if the
76 ** input is not an sqlite connection.
77 */
78 static int get_sqlite_pointer(
79   void * clientData,
80   Tcl_Interp *interp,
81   int objc,
82   Tcl_Obj *CONST objv[]
83 ){
84   struct SqliteDb *p;
85   Tcl_CmdInfo cmdInfo;
86   char zBuf[100];
87   if( objc!=2 ){
88     Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION");
89     return TCL_ERROR;
90   }
91   if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
92     Tcl_AppendResult(interp, "command not found: ",
93            Tcl_GetString(objv[1]), (char*)0);
94     return TCL_ERROR;
95   }
96   p = (struct SqliteDb*)cmdInfo.objClientData;
97   sqlite3_snprintf(sizeof(zBuf), zBuf, "%p", p->db);
98   Tcl_AppendResult(interp, zBuf, 0);
99   return TCL_OK;
100 }
101 
102 /*
103 ** Decode a pointer to an sqlite3 object.
104 */
105 int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
106   struct SqliteDb *p;
107   Tcl_CmdInfo cmdInfo;
108   if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){
109     p = (struct SqliteDb*)cmdInfo.objClientData;
110     *ppDb = p->db;
111   }else{
112     *ppDb = (sqlite3*)sqlite3TestTextToPtr(zA);
113   }
114   return TCL_OK;
115 }
116 
117 #if SQLITE_OS_WIN
118 /*
119 ** Decode a Win32 HANDLE object.
120 */
121 int getWin32Handle(Tcl_Interp *interp, const char *zA, LPHANDLE phFile){
122   *phFile = (HANDLE)sqlite3TestTextToPtr(zA);
123   return TCL_OK;
124 }
125 #endif
126 
127 extern const char *sqlite3ErrName(int);
128 #define t1ErrorName sqlite3ErrName
129 
130 /*
131 ** Convert an sqlite3_stmt* into an sqlite3*.  This depends on the
132 ** fact that the sqlite3* is the first field in the Vdbe structure.
133 */
134 #define StmtToDb(X)   sqlite3_db_handle(X)
135 
136 /*
137 ** Check a return value to make sure it agrees with the results
138 ** from sqlite3_errcode.
139 */
140 int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){
141   if( sqlite3_threadsafe()==0 && rc!=SQLITE_MISUSE && rc!=SQLITE_OK
142    && sqlite3_errcode(db)!=rc ){
143     char zBuf[200];
144     int r2 = sqlite3_errcode(db);
145     sqlite3_snprintf(sizeof(zBuf), zBuf,
146        "error code %s (%d) does not match sqlite3_errcode %s (%d)",
147        t1ErrorName(rc), rc, t1ErrorName(r2), r2);
148     Tcl_ResetResult(interp);
149     Tcl_AppendResult(interp, zBuf, 0);
150     return 1;
151   }
152   return 0;
153 }
154 
155 /*
156 ** Decode a pointer to an sqlite3_stmt object.
157 */
158 static int getStmtPointer(
159   Tcl_Interp *interp,
160   const char *zArg,
161   sqlite3_stmt **ppStmt
162 ){
163   *ppStmt = (sqlite3_stmt*)sqlite3TestTextToPtr(zArg);
164   return TCL_OK;
165 }
166 
167 /*
168 ** Generate a text representation of a pointer that can be understood
169 ** by the getDbPointer and getVmPointer routines above.
170 **
171 ** The problem is, on some machines (Solaris) if you do a printf with
172 ** "%p" you cannot turn around and do a scanf with the same "%p" and
173 ** get your pointer back.  You have to prepend a "0x" before it will
174 ** work.  Or at least that is what is reported to me (drh).  But this
175 ** behavior varies from machine to machine.  The solution used her is
176 ** to test the string right after it is generated to see if it can be
177 ** understood by scanf, and if not, try prepending an "0x" to see if
178 ** that helps.  If nothing works, a fatal error is generated.
179 */
180 int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){
181   sqlite3_snprintf(100, zPtr, "%p", p);
182   return TCL_OK;
183 }
184 
185 /*
186 ** The callback routine for sqlite3_exec_printf().
187 */
188 static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){
189   Tcl_DString *str = (Tcl_DString*)pArg;
190   int i;
191 
192   if( Tcl_DStringLength(str)==0 ){
193     for(i=0; i<argc; i++){
194       Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL");
195     }
196   }
197   for(i=0; i<argc; i++){
198     Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL");
199   }
200   return 0;
201 }
202 
203 /*
204 ** The I/O tracing callback.
205 */
206 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
207 static FILE *iotrace_file = 0;
208 static void io_trace_callback(const char *zFormat, ...){
209   va_list ap;
210   va_start(ap, zFormat);
211   vfprintf(iotrace_file, zFormat, ap);
212   va_end(ap);
213   fflush(iotrace_file);
214 }
215 #endif
216 
217 /*
218 ** Usage:  io_trace FILENAME
219 **
220 ** Turn I/O tracing on or off.  If FILENAME is not an empty string,
221 ** I/O tracing begins going into FILENAME. If FILENAME is an empty
222 ** string, I/O tracing is turned off.
223 */
224 static int test_io_trace(
225   void *NotUsed,
226   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
227   int argc,              /* Number of arguments */
228   char **argv            /* Text of each argument */
229 ){
230 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
231   if( argc!=2 ){
232     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
233           " FILENAME\"", 0);
234     return TCL_ERROR;
235   }
236   if( iotrace_file ){
237     if( iotrace_file!=stdout && iotrace_file!=stderr ){
238       fclose(iotrace_file);
239     }
240     iotrace_file = 0;
241     sqlite3IoTrace = 0;
242   }
243   if( argv[1][0] ){
244     if( strcmp(argv[1],"stdout")==0 ){
245       iotrace_file = stdout;
246     }else if( strcmp(argv[1],"stderr")==0 ){
247       iotrace_file = stderr;
248     }else{
249       iotrace_file = fopen(argv[1], "w");
250     }
251     sqlite3IoTrace = io_trace_callback;
252   }
253 #endif
254   return TCL_OK;
255 }
256 
257 /*
258 ** Usage:  clang_sanitize_address
259 **
260 ** Returns true if the program was compiled using clang with the
261 ** -fsanitize=address switch on the command line. False otherwise.
262 **
263 ** Also return true if the OMIT_MISUSE environment variable exists.
264 */
265 static int clang_sanitize_address(
266   void *NotUsed,
267   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
268   int argc,              /* Number of arguments */
269   char **argv            /* Text of each argument */
270 ){
271   int res = 0;
272 #if defined(__has_feature)
273 # if __has_feature(address_sanitizer)
274   res = 1;
275 # endif
276 #endif
277 #ifdef __SANITIZE_ADDRESS__
278   res = 1;
279 #endif
280   if( res==0 && getenv("OMIT_MISUSE")!=0 ) res = 1;
281   Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
282   return TCL_OK;
283 }
284 
285 /*
286 ** Usage:  sqlite3_exec_printf  DB  FORMAT  STRING
287 **
288 ** Invoke the sqlite3_exec_printf() interface using the open database
289 ** DB.  The SQL is the string FORMAT.  The format string should contain
290 ** one %s or %q.  STRING is the value inserted into %s or %q.
291 */
292 static int test_exec_printf(
293   void *NotUsed,
294   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
295   int argc,              /* Number of arguments */
296   char **argv            /* Text of each argument */
297 ){
298   sqlite3 *db;
299   Tcl_DString str;
300   int rc;
301   char *zErr = 0;
302   char *zSql;
303   char zBuf[30];
304   if( argc!=4 ){
305     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
306        " DB FORMAT STRING", 0);
307     return TCL_ERROR;
308   }
309   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
310   Tcl_DStringInit(&str);
311   zSql = sqlite3_mprintf(argv[2], argv[3]);
312   rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
313   sqlite3_free(zSql);
314   sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
315   Tcl_AppendElement(interp, zBuf);
316   Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
317   Tcl_DStringFree(&str);
318   if( zErr ) sqlite3_free(zErr);
319   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
320   return TCL_OK;
321 }
322 
323 /*
324 ** Usage:  sqlite3_exec_hex  DB  HEX
325 **
326 ** Invoke the sqlite3_exec() on a string that is obtained by translating
327 ** HEX into ASCII.  Most characters are translated as is.  %HH becomes
328 ** a hex character.
329 */
330 static int test_exec_hex(
331   void *NotUsed,
332   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
333   int argc,              /* Number of arguments */
334   char **argv            /* Text of each argument */
335 ){
336   sqlite3 *db;
337   Tcl_DString str;
338   int rc, i, j;
339   char *zErr = 0;
340   char *zHex;
341   char zSql[501];
342   char zBuf[30];
343   if( argc!=3 ){
344     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
345        " DB HEX", 0);
346     return TCL_ERROR;
347   }
348   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
349   zHex = argv[2];
350   for(i=j=0; i<(sizeof(zSql)-1) && zHex[j]; i++, j++){
351     if( zHex[j]=='%' && zHex[j+2] && zHex[j+2] ){
352       zSql[i] = (testHexToInt(zHex[j+1])<<4) + testHexToInt(zHex[j+2]);
353       j += 2;
354     }else{
355       zSql[i] = zHex[j];
356     }
357   }
358   zSql[i] = 0;
359   Tcl_DStringInit(&str);
360   rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
361   sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
362   Tcl_AppendElement(interp, zBuf);
363   Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
364   Tcl_DStringFree(&str);
365   if( zErr ) sqlite3_free(zErr);
366   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
367   return TCL_OK;
368 }
369 
370 /*
371 ** Usage:  db_enter DB
372 **         db_leave DB
373 **
374 ** Enter or leave the mutex on a database connection.
375 */
376 static int db_enter(
377   void *NotUsed,
378   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
379   int argc,              /* Number of arguments */
380   char **argv            /* Text of each argument */
381 ){
382   sqlite3 *db;
383   if( argc!=2 ){
384     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
385        " DB", 0);
386     return TCL_ERROR;
387   }
388   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
389   sqlite3_mutex_enter(db->mutex);
390   return TCL_OK;
391 }
392 static int db_leave(
393   void *NotUsed,
394   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
395   int argc,              /* Number of arguments */
396   char **argv            /* Text of each argument */
397 ){
398   sqlite3 *db;
399   if( argc!=2 ){
400     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
401        " DB", 0);
402     return TCL_ERROR;
403   }
404   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
405   sqlite3_mutex_leave(db->mutex);
406   return TCL_OK;
407 }
408 
409 /*
410 ** Usage:  sqlite3_exec  DB  SQL
411 **
412 ** Invoke the sqlite3_exec interface using the open database DB
413 */
414 static int test_exec(
415   void *NotUsed,
416   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
417   int argc,              /* Number of arguments */
418   char **argv            /* Text of each argument */
419 ){
420   sqlite3 *db;
421   Tcl_DString str;
422   int rc;
423   char *zErr = 0;
424   char *zSql;
425   int i, j;
426   char zBuf[30];
427   if( argc!=3 ){
428     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
429        " DB SQL", 0);
430     return TCL_ERROR;
431   }
432   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
433   Tcl_DStringInit(&str);
434   zSql = sqlite3_mprintf("%s", argv[2]);
435   for(i=j=0; zSql[i];){
436     if( zSql[i]=='%' ){
437       zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]);
438       i += 3;
439     }else{
440       zSql[j++] = zSql[i++];
441     }
442   }
443   zSql[j] = 0;
444   rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
445   sqlite3_free(zSql);
446   sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
447   Tcl_AppendElement(interp, zBuf);
448   Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
449   Tcl_DStringFree(&str);
450   if( zErr ) sqlite3_free(zErr);
451   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
452   return TCL_OK;
453 }
454 
455 /*
456 ** Usage:  sqlite3_exec_nr  DB  SQL
457 **
458 ** Invoke the sqlite3_exec interface using the open database DB.  Discard
459 ** all results
460 */
461 static int test_exec_nr(
462   void *NotUsed,
463   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
464   int argc,              /* Number of arguments */
465   char **argv            /* Text of each argument */
466 ){
467   sqlite3 *db;
468   int rc;
469   char *zErr = 0;
470   if( argc!=3 ){
471     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
472        " DB SQL", 0);
473     return TCL_ERROR;
474   }
475   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
476   rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
477   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
478   return TCL_OK;
479 }
480 
481 /*
482 ** Usage:  sqlite3_mprintf_z_test  SEPARATOR  ARG0  ARG1 ...
483 **
484 ** Test the %z format of sqlite_mprintf().  Use multiple mprintf() calls to
485 ** concatenate arg0 through argn using separator as the separator.
486 ** Return the result.
487 */
488 static int test_mprintf_z(
489   void *NotUsed,
490   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
491   int argc,              /* Number of arguments */
492   char **argv            /* Text of each argument */
493 ){
494   char *zResult = 0;
495   int i;
496 
497   for(i=2; i<argc && (i==2 || zResult); i++){
498     zResult = sqlite3_mprintf("%z%s%s", zResult, argv[1], argv[i]);
499   }
500   Tcl_AppendResult(interp, zResult, 0);
501   sqlite3_free(zResult);
502   return TCL_OK;
503 }
504 
505 /*
506 ** Usage:  sqlite3_mprintf_n_test  STRING
507 **
508 ** Test the %n format of sqlite_mprintf().  Return the length of the
509 ** input string.
510 */
511 static int test_mprintf_n(
512   void *NotUsed,
513   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
514   int argc,              /* Number of arguments */
515   char **argv            /* Text of each argument */
516 ){
517   char *zStr;
518   int n = 0;
519   zStr = sqlite3_mprintf("%s%n", argv[1], &n);
520   sqlite3_free(zStr);
521   Tcl_SetObjResult(interp, Tcl_NewIntObj(n));
522   return TCL_OK;
523 }
524 
525 /*
526 ** Usage:  sqlite3_snprintf_int  SIZE FORMAT  INT
527 **
528 ** Test the of sqlite3_snprintf() routine.  SIZE is the size of the
529 ** output buffer in bytes.  The maximum size is 100.  FORMAT is the
530 ** format string.  INT is a single integer argument.  The FORMAT
531 ** string must require no more than this one integer argument.  If
532 ** You pass in a format string that requires more than one argument,
533 ** bad things will happen.
534 */
535 static int test_snprintf_int(
536   void *NotUsed,
537   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
538   int argc,              /* Number of arguments */
539   char **argv            /* Text of each argument */
540 ){
541   char zStr[100];
542   int n = atoi(argv[1]);
543   const char *zFormat = argv[2];
544   int a1 = atoi(argv[3]);
545   if( n>sizeof(zStr) ) n = sizeof(zStr);
546   sqlite3_snprintf(sizeof(zStr), zStr, "abcdefghijklmnopqrstuvwxyz");
547   sqlite3_snprintf(n, zStr, zFormat, a1);
548   Tcl_AppendResult(interp, zStr, 0);
549   return TCL_OK;
550 }
551 
552 #ifndef SQLITE_OMIT_GET_TABLE
553 
554 /*
555 ** Usage:  sqlite3_get_table_printf  DB  FORMAT  STRING  ?--no-counts?
556 **
557 ** Invoke the sqlite3_get_table_printf() interface using the open database
558 ** DB.  The SQL is the string FORMAT.  The format string should contain
559 ** one %s or %q.  STRING is the value inserted into %s or %q.
560 */
561 static int test_get_table_printf(
562   void *NotUsed,
563   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
564   int argc,              /* Number of arguments */
565   char **argv            /* Text of each argument */
566 ){
567   sqlite3 *db;
568   Tcl_DString str;
569   int rc;
570   char *zErr = 0;
571   int nRow = 0, nCol = 0;
572   char **aResult;
573   int i;
574   char zBuf[30];
575   char *zSql;
576   int resCount = -1;
577   if( argc==5 ){
578     if( Tcl_GetInt(interp, argv[4], &resCount) ) return TCL_ERROR;
579   }
580   if( argc!=4 && argc!=5 ){
581     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
582        " DB FORMAT STRING ?COUNT?", 0);
583     return TCL_ERROR;
584   }
585   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
586   Tcl_DStringInit(&str);
587   zSql = sqlite3_mprintf(argv[2],argv[3]);
588   if( argc==5 ){
589     rc = sqlite3_get_table(db, zSql, &aResult, 0, 0, &zErr);
590   }else{
591     rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr);
592     resCount = (nRow+1)*nCol;
593   }
594   sqlite3_free(zSql);
595   sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
596   Tcl_AppendElement(interp, zBuf);
597   if( rc==SQLITE_OK ){
598     if( argc==4 ){
599       sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nRow);
600       Tcl_AppendElement(interp, zBuf);
601       sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nCol);
602       Tcl_AppendElement(interp, zBuf);
603     }
604     for(i=0; i<resCount; i++){
605       Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL");
606     }
607   }else{
608     Tcl_AppendElement(interp, zErr);
609   }
610   sqlite3_free_table(aResult);
611   if( zErr ) sqlite3_free(zErr);
612   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
613   return TCL_OK;
614 }
615 
616 #endif /* SQLITE_OMIT_GET_TABLE */
617 
618 
619 /*
620 ** Usage:  sqlite3_last_insert_rowid DB
621 **
622 ** Returns the integer ROWID of the most recent insert.
623 */
624 static int test_last_rowid(
625   void *NotUsed,
626   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
627   int argc,              /* Number of arguments */
628   char **argv            /* Text of each argument */
629 ){
630   sqlite3 *db;
631   char zBuf[30];
632 
633   if( argc!=2 ){
634     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0);
635     return TCL_ERROR;
636   }
637   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
638   sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", sqlite3_last_insert_rowid(db));
639   Tcl_AppendResult(interp, zBuf, 0);
640   return SQLITE_OK;
641 }
642 
643 /*
644 ** Usage:  sqlite3_key DB KEY
645 **
646 ** Set the codec key.
647 */
648 static int test_key(
649   void *NotUsed,
650   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
651   int argc,              /* Number of arguments */
652   char **argv            /* Text of each argument */
653 ){
654 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL)
655   sqlite3 *db;
656   const char *zKey;
657   int nKey;
658   if( argc!=3 ){
659     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
660        " FILENAME\"", 0);
661     return TCL_ERROR;
662   }
663   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
664   zKey = argv[2];
665   nKey = strlen(zKey);
666   sqlite3_key(db, zKey, nKey);
667 #endif
668   return TCL_OK;
669 }
670 
671 /*
672 ** Usage:  sqlite3_rekey DB KEY
673 **
674 ** Change the codec key.
675 */
676 static int test_rekey(
677   void *NotUsed,
678   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
679   int argc,              /* Number of arguments */
680   char **argv            /* Text of each argument */
681 ){
682 #ifdef SQLITE_HAS_CODEC
683   sqlite3 *db;
684   const char *zKey;
685   int nKey;
686   if( argc!=3 ){
687     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
688        " FILENAME\"", 0);
689     return TCL_ERROR;
690   }
691   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
692   zKey = argv[2];
693   nKey = strlen(zKey);
694   sqlite3_rekey(db, zKey, nKey);
695 #endif
696   return TCL_OK;
697 }
698 
699 /*
700 ** Usage:  sqlite3_close DB
701 **
702 ** Closes the database opened by sqlite3_open.
703 */
704 static int sqlite_test_close(
705   void *NotUsed,
706   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
707   int argc,              /* Number of arguments */
708   char **argv            /* Text of each argument */
709 ){
710   sqlite3 *db;
711   int rc;
712   if( argc!=2 ){
713     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
714        " FILENAME\"", 0);
715     return TCL_ERROR;
716   }
717   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
718   rc = sqlite3_close(db);
719   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
720   return TCL_OK;
721 }
722 
723 /*
724 ** Usage:  sqlite3_close_v2 DB
725 **
726 ** Closes the database opened by sqlite3_open.
727 */
728 static int sqlite_test_close_v2(
729   void *NotUsed,
730   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
731   int argc,              /* Number of arguments */
732   char **argv            /* Text of each argument */
733 ){
734   sqlite3 *db;
735   int rc;
736   if( argc!=2 ){
737     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
738        " FILENAME\"", 0);
739     return TCL_ERROR;
740   }
741   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
742   rc = sqlite3_close_v2(db);
743   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
744   return TCL_OK;
745 }
746 
747 /*
748 ** Implementation of the x_coalesce() function.
749 ** Return the first argument non-NULL argument.
750 */
751 static void t1_ifnullFunc(
752   sqlite3_context *context,
753   int argc,
754   sqlite3_value **argv
755 ){
756   int i;
757   for(i=0; i<argc; i++){
758     if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
759       int n = sqlite3_value_bytes(argv[i]);
760       sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]),
761           n, SQLITE_TRANSIENT);
762       break;
763     }
764   }
765 }
766 
767 /*
768 ** These are test functions.    hex8() interprets its argument as
769 ** UTF8 and returns a hex encoding.  hex16le() interprets its argument
770 ** as UTF16le and returns a hex encoding.
771 */
772 static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){
773   const unsigned char *z;
774   int i;
775   char zBuf[200];
776   z = sqlite3_value_text(argv[0]);
777   for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){
778     sqlite3_snprintf(sizeof(zBuf)-i*2, &zBuf[i*2], "%02x", z[i]);
779   }
780   zBuf[i*2] = 0;
781   sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
782 }
783 #ifndef SQLITE_OMIT_UTF16
784 static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){
785   const unsigned short int *z;
786   int i;
787   char zBuf[400];
788   z = sqlite3_value_text16(argv[0]);
789   for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){
790     sqlite3_snprintf(sizeof(zBuf)-i*4, &zBuf[i*4],"%04x", z[i]&0xff);
791   }
792   zBuf[i*4] = 0;
793   sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
794 }
795 #endif
796 
797 /*
798 ** A structure into which to accumulate text.
799 */
800 struct dstr {
801   int nAlloc;  /* Space allocated */
802   int nUsed;   /* Space used */
803   char *z;     /* The space */
804 };
805 
806 /*
807 ** Append text to a dstr
808 */
809 static void dstrAppend(struct dstr *p, const char *z, int divider){
810   int n = (int)strlen(z);
811   if( p->nUsed + n + 2 > p->nAlloc ){
812     char *zNew;
813     p->nAlloc = p->nAlloc*2 + n + 200;
814     zNew = sqlite3_realloc(p->z, p->nAlloc);
815     if( zNew==0 ){
816       sqlite3_free(p->z);
817       memset(p, 0, sizeof(*p));
818       return;
819     }
820     p->z = zNew;
821   }
822   if( divider && p->nUsed>0 ){
823     p->z[p->nUsed++] = divider;
824   }
825   memcpy(&p->z[p->nUsed], z, n+1);
826   p->nUsed += n;
827 }
828 
829 /*
830 ** Invoked for each callback from sqlite3ExecFunc
831 */
832 static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){
833   struct dstr *p = (struct dstr*)pData;
834   int i;
835   for(i=0; i<argc; i++){
836     if( argv[i]==0 ){
837       dstrAppend(p, "NULL", ' ');
838     }else{
839       dstrAppend(p, argv[i], ' ');
840     }
841   }
842   return 0;
843 }
844 
845 /*
846 ** Implementation of the x_sqlite_exec() function.  This function takes
847 ** a single argument and attempts to execute that argument as SQL code.
848 ** This is illegal and should set the SQLITE_MISUSE flag on the database.
849 **
850 ** 2004-Jan-07:  We have changed this to make it legal to call sqlite3_exec()
851 ** from within a function call.
852 **
853 ** This routine simulates the effect of having two threads attempt to
854 ** use the same database at the same time.
855 */
856 static void sqlite3ExecFunc(
857   sqlite3_context *context,
858   int argc,
859   sqlite3_value **argv
860 ){
861   struct dstr x;
862   memset(&x, 0, sizeof(x));
863   (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context),
864       (char*)sqlite3_value_text(argv[0]),
865       execFuncCallback, &x, 0);
866   sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT);
867   sqlite3_free(x.z);
868 }
869 
870 /*
871 ** Implementation of tkt2213func(), a scalar function that takes exactly
872 ** one argument. It has two interesting features:
873 **
874 ** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*.
875 **   If the three pointers returned are not the same an SQL error is raised.
876 **
877 ** * Otherwise it returns a copy of the text representation of its
878 **   argument in such a way as the VDBE representation is a Mem* cell
879 **   with the MEM_Term flag clear.
880 **
881 ** Ticket #2213 can therefore be tested by evaluating the following
882 ** SQL expression:
883 **
884 **   tkt2213func(tkt2213func('a string'));
885 */
886 static void tkt2213Function(
887   sqlite3_context *context,
888   int argc,
889   sqlite3_value **argv
890 ){
891   int nText;
892   unsigned char const *zText1;
893   unsigned char const *zText2;
894   unsigned char const *zText3;
895 
896   nText = sqlite3_value_bytes(argv[0]);
897   zText1 = sqlite3_value_text(argv[0]);
898   zText2 = sqlite3_value_text(argv[0]);
899   zText3 = sqlite3_value_text(argv[0]);
900 
901   if( zText1!=zText2 || zText2!=zText3 ){
902     sqlite3_result_error(context, "tkt2213 is not fixed", -1);
903   }else{
904     char *zCopy = (char *)sqlite3_malloc(nText);
905     memcpy(zCopy, zText1, nText);
906     sqlite3_result_text(context, zCopy, nText, sqlite3_free);
907   }
908 }
909 
910 /*
911 ** The following SQL function takes 4 arguments.  The 2nd and
912 ** 4th argument must be one of these strings:  'text', 'text16',
913 ** or 'blob' corresponding to API functions
914 **
915 **      sqlite3_value_text()
916 **      sqlite3_value_text16()
917 **      sqlite3_value_blob()
918 **
919 ** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
920 ** corresponding to APIs:
921 **
922 **      sqlite3_value_bytes()
923 **      sqlite3_value_bytes16()
924 **      noop
925 **
926 ** The APIs designated by the 2nd through 4th arguments are applied
927 ** to the first argument in order.  If the pointers returned by the
928 ** second and fourth are different, this routine returns 1.  Otherwise,
929 ** this routine returns 0.
930 **
931 ** This function is used to test to see when returned pointers from
932 ** the _text(), _text16() and _blob() APIs become invalidated.
933 */
934 static void ptrChngFunction(
935   sqlite3_context *context,
936   int argc,
937   sqlite3_value **argv
938 ){
939   const void *p1, *p2;
940   const char *zCmd;
941   if( argc!=4 ) return;
942   zCmd = (const char*)sqlite3_value_text(argv[1]);
943   if( zCmd==0 ) return;
944   if( strcmp(zCmd,"text")==0 ){
945     p1 = (const void*)sqlite3_value_text(argv[0]);
946 #ifndef SQLITE_OMIT_UTF16
947   }else if( strcmp(zCmd, "text16")==0 ){
948     p1 = (const void*)sqlite3_value_text16(argv[0]);
949 #endif
950   }else if( strcmp(zCmd, "blob")==0 ){
951     p1 = (const void*)sqlite3_value_blob(argv[0]);
952   }else{
953     return;
954   }
955   zCmd = (const char*)sqlite3_value_text(argv[2]);
956   if( zCmd==0 ) return;
957   if( strcmp(zCmd,"bytes")==0 ){
958     sqlite3_value_bytes(argv[0]);
959 #ifndef SQLITE_OMIT_UTF16
960   }else if( strcmp(zCmd, "bytes16")==0 ){
961     sqlite3_value_bytes16(argv[0]);
962 #endif
963   }else if( strcmp(zCmd, "noop")==0 ){
964     /* do nothing */
965   }else{
966     return;
967   }
968   zCmd = (const char*)sqlite3_value_text(argv[3]);
969   if( zCmd==0 ) return;
970   if( strcmp(zCmd,"text")==0 ){
971     p2 = (const void*)sqlite3_value_text(argv[0]);
972 #ifndef SQLITE_OMIT_UTF16
973   }else if( strcmp(zCmd, "text16")==0 ){
974     p2 = (const void*)sqlite3_value_text16(argv[0]);
975 #endif
976   }else if( strcmp(zCmd, "blob")==0 ){
977     p2 = (const void*)sqlite3_value_blob(argv[0]);
978   }else{
979     return;
980   }
981   sqlite3_result_int(context, p1!=p2);
982 }
983 
984 /*
985 ** This SQL function returns a different answer each time it is called, even if
986 ** the arguments are the same.
987 */
988 static void nondeterministicFunction(
989   sqlite3_context *context,
990   int argc,
991   sqlite3_value **argv
992 ){
993   static int cnt = 0;
994   sqlite3_result_int(context, cnt++);
995 }
996 
997 /*
998 ** Usage:  sqlite3_create_function DB
999 **
1000 ** Call the sqlite3_create_function API on the given database in order
1001 ** to create a function named "x_coalesce".  This function does the same thing
1002 ** as the "coalesce" function.  This function also registers an SQL function
1003 ** named "x_sqlite_exec" that invokes sqlite3_exec().  Invoking sqlite3_exec()
1004 ** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
1005 ** The effect is similar to trying to use the same database connection from
1006 ** two threads at the same time.
1007 **
1008 ** The original motivation for this routine was to be able to call the
1009 ** sqlite3_create_function function while a query is in progress in order
1010 ** to test the SQLITE_MISUSE detection logic.
1011 */
1012 static int test_create_function(
1013   void *NotUsed,
1014   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1015   int argc,              /* Number of arguments */
1016   char **argv            /* Text of each argument */
1017 ){
1018   int rc;
1019   sqlite3 *db;
1020 
1021   if( argc!=2 ){
1022     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1023        " DB\"", 0);
1024     return TCL_ERROR;
1025   }
1026   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1027   rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_UTF8, 0,
1028         t1_ifnullFunc, 0, 0);
1029   if( rc==SQLITE_OK ){
1030     rc = sqlite3_create_function(db, "hex8", 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1031           0, hex8Func, 0, 0);
1032   }
1033 #ifndef SQLITE_OMIT_UTF16
1034   if( rc==SQLITE_OK ){
1035     rc = sqlite3_create_function(db, "hex16", 1, SQLITE_UTF16 | SQLITE_DETERMINISTIC,
1036           0, hex16Func, 0, 0);
1037   }
1038 #endif
1039   if( rc==SQLITE_OK ){
1040     rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0,
1041           tkt2213Function, 0, 0);
1042   }
1043   if( rc==SQLITE_OK ){
1044     rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0,
1045           ptrChngFunction, 0, 0);
1046   }
1047 
1048   /* Functions counter1() and counter2() have the same implementation - they
1049   ** both return an ascending integer with each call.  But counter1() is marked
1050   ** as non-deterministic and counter2() is marked as deterministic.
1051   */
1052   if( rc==SQLITE_OK ){
1053     rc = sqlite3_create_function(db, "counter1", -1, SQLITE_UTF8,
1054           0, nondeterministicFunction, 0, 0);
1055   }
1056   if( rc==SQLITE_OK ){
1057     rc = sqlite3_create_function(db, "counter2", -1, SQLITE_UTF8|SQLITE_DETERMINISTIC,
1058           0, nondeterministicFunction, 0, 0);
1059   }
1060 
1061 #ifndef SQLITE_OMIT_UTF16
1062   /* Use the sqlite3_create_function16() API here. Mainly for fun, but also
1063   ** because it is not tested anywhere else. */
1064   if( rc==SQLITE_OK ){
1065     const void *zUtf16;
1066     sqlite3_value *pVal;
1067     sqlite3_mutex_enter(db->mutex);
1068     pVal = sqlite3ValueNew(db);
1069     sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);
1070     zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
1071     if( db->mallocFailed ){
1072       rc = SQLITE_NOMEM;
1073     }else{
1074       rc = sqlite3_create_function16(db, zUtf16,
1075                 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
1076     }
1077     sqlite3ValueFree(pVal);
1078     sqlite3_mutex_leave(db->mutex);
1079   }
1080 #endif
1081 
1082   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1083   Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
1084   return TCL_OK;
1085 }
1086 
1087 /*
1088 ** Routines to implement the x_count() aggregate function.
1089 **
1090 ** x_count() counts the number of non-null arguments.  But there are
1091 ** some twists for testing purposes.
1092 **
1093 ** If the argument to x_count() is 40 then a UTF-8 error is reported
1094 ** on the step function.  If x_count(41) is seen, then a UTF-16 error
1095 ** is reported on the step function.  If the total count is 42, then
1096 ** a UTF-8 error is reported on the finalize function.
1097 */
1098 typedef struct t1CountCtx t1CountCtx;
1099 struct t1CountCtx {
1100   int n;
1101 };
1102 static void t1CountStep(
1103   sqlite3_context *context,
1104   int argc,
1105   sqlite3_value **argv
1106 ){
1107   t1CountCtx *p;
1108   p = sqlite3_aggregate_context(context, sizeof(*p));
1109   if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
1110     p->n++;
1111   }
1112   if( argc>0 ){
1113     int v = sqlite3_value_int(argv[0]);
1114     if( v==40 ){
1115       sqlite3_result_error(context, "value of 40 handed to x_count", -1);
1116 #ifndef SQLITE_OMIT_UTF16
1117     }else if( v==41 ){
1118       const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
1119       sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
1120 #endif
1121     }
1122   }
1123 }
1124 static void t1CountFinalize(sqlite3_context *context){
1125   t1CountCtx *p;
1126   p = sqlite3_aggregate_context(context, sizeof(*p));
1127   if( p ){
1128     if( p->n==42 ){
1129       sqlite3_result_error(context, "x_count totals to 42", -1);
1130     }else{
1131       sqlite3_result_int(context, p ? p->n : 0);
1132     }
1133   }
1134 }
1135 
1136 #ifndef SQLITE_OMIT_DEPRECATED
1137 static void legacyCountStep(
1138   sqlite3_context *context,
1139   int argc,
1140   sqlite3_value **argv
1141 ){
1142   /* no-op */
1143 }
1144 
1145 static void legacyCountFinalize(sqlite3_context *context){
1146   sqlite3_result_int(context, sqlite3_aggregate_count(context));
1147 }
1148 #endif
1149 
1150 /*
1151 ** Usage:  sqlite3_create_aggregate DB
1152 **
1153 ** Call the sqlite3_create_function API on the given database in order
1154 ** to create a function named "x_count".  This function is similar
1155 ** to the built-in count() function, with a few special quirks
1156 ** for testing the sqlite3_result_error() APIs.
1157 **
1158 ** The original motivation for this routine was to be able to call the
1159 ** sqlite3_create_aggregate function while a query is in progress in order
1160 ** to test the SQLITE_MISUSE detection logic.  See misuse.test.
1161 **
1162 ** This routine was later extended to test the use of sqlite3_result_error()
1163 ** within aggregate functions.
1164 **
1165 ** Later: It is now also extended to register the aggregate function
1166 ** "legacy_count()" with the supplied database handle. This is used
1167 ** to test the deprecated sqlite3_aggregate_count() API.
1168 */
1169 static int test_create_aggregate(
1170   void *NotUsed,
1171   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1172   int argc,              /* Number of arguments */
1173   char **argv            /* Text of each argument */
1174 ){
1175   sqlite3 *db;
1176   int rc;
1177   if( argc!=2 ){
1178     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1179        " FILENAME\"", 0);
1180     return TCL_ERROR;
1181   }
1182   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1183   rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
1184       t1CountStep,t1CountFinalize);
1185   if( rc==SQLITE_OK ){
1186     rc = sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
1187         t1CountStep,t1CountFinalize);
1188   }
1189 #ifndef SQLITE_OMIT_DEPRECATED
1190   if( rc==SQLITE_OK ){
1191     rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0,
1192         legacyCountStep, legacyCountFinalize
1193     );
1194   }
1195 #endif
1196   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1197   Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
1198   return TCL_OK;
1199 }
1200 
1201 
1202 /*
1203 ** Usage:  printf TEXT
1204 **
1205 ** Send output to printf.  Use this rather than puts to merge the output
1206 ** in the correct sequence with debugging printfs inserted into C code.
1207 ** Puts uses a separate buffer and debugging statements will be out of
1208 ** sequence if it is used.
1209 */
1210 static int test_printf(
1211   void *NotUsed,
1212   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1213   int argc,              /* Number of arguments */
1214   char **argv            /* Text of each argument */
1215 ){
1216   if( argc!=2 ){
1217     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1218        " TEXT\"", 0);
1219     return TCL_ERROR;
1220   }
1221   printf("%s\n", argv[1]);
1222   return TCL_OK;
1223 }
1224 
1225 
1226 
1227 /*
1228 ** Usage:  sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
1229 **
1230 ** Call mprintf with three integer arguments
1231 */
1232 static int sqlite3_mprintf_int(
1233   void *NotUsed,
1234   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1235   int argc,              /* Number of arguments */
1236   char **argv            /* Text of each argument */
1237 ){
1238   int a[3], i;
1239   char *z;
1240   if( argc!=5 ){
1241     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1242        " FORMAT INT INT INT\"", 0);
1243     return TCL_ERROR;
1244   }
1245   for(i=2; i<5; i++){
1246     if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1247   }
1248   z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1249   Tcl_AppendResult(interp, z, 0);
1250   sqlite3_free(z);
1251   return TCL_OK;
1252 }
1253 
1254 /*
1255 ** Usage:  sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
1256 **
1257 ** Call mprintf with three 64-bit integer arguments
1258 */
1259 static int sqlite3_mprintf_int64(
1260   void *NotUsed,
1261   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1262   int argc,              /* Number of arguments */
1263   char **argv            /* Text of each argument */
1264 ){
1265   int i;
1266   sqlite_int64 a[3];
1267   char *z;
1268   if( argc!=5 ){
1269     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1270        " FORMAT INT INT INT\"", 0);
1271     return TCL_ERROR;
1272   }
1273   for(i=2; i<5; i++){
1274     if( sqlite3Atoi64(argv[i], &a[i-2], 1000000, SQLITE_UTF8) ){
1275       Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0);
1276       return TCL_ERROR;
1277     }
1278   }
1279   z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1280   Tcl_AppendResult(interp, z, 0);
1281   sqlite3_free(z);
1282   return TCL_OK;
1283 }
1284 
1285 /*
1286 ** Usage:  sqlite3_mprintf_long FORMAT INTEGER INTEGER INTEGER
1287 **
1288 ** Call mprintf with three long integer arguments.   This might be the
1289 ** same as sqlite3_mprintf_int or sqlite3_mprintf_int64, depending on
1290 ** platform.
1291 */
1292 static int sqlite3_mprintf_long(
1293   void *NotUsed,
1294   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1295   int argc,              /* Number of arguments */
1296   char **argv            /* Text of each argument */
1297 ){
1298   int i;
1299   long int a[3];
1300   int b[3];
1301   char *z;
1302   if( argc!=5 ){
1303     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1304        " FORMAT INT INT INT\"", 0);
1305     return TCL_ERROR;
1306   }
1307   for(i=2; i<5; i++){
1308     if( Tcl_GetInt(interp, argv[i], &b[i-2]) ) return TCL_ERROR;
1309     a[i-2] = (long int)b[i-2];
1310     a[i-2] &= (((u64)1)<<(sizeof(int)*8))-1;
1311   }
1312   z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1313   Tcl_AppendResult(interp, z, 0);
1314   sqlite3_free(z);
1315   return TCL_OK;
1316 }
1317 
1318 /*
1319 ** Usage:  sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
1320 **
1321 ** Call mprintf with two integer arguments and one string argument
1322 */
1323 static int sqlite3_mprintf_str(
1324   void *NotUsed,
1325   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1326   int argc,              /* Number of arguments */
1327   char **argv            /* Text of each argument */
1328 ){
1329   int a[3], i;
1330   char *z;
1331   if( argc<4 || argc>5 ){
1332     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1333        " FORMAT INT INT ?STRING?\"", 0);
1334     return TCL_ERROR;
1335   }
1336   for(i=2; i<4; i++){
1337     if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1338   }
1339   z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL);
1340   Tcl_AppendResult(interp, z, 0);
1341   sqlite3_free(z);
1342   return TCL_OK;
1343 }
1344 
1345 /*
1346 ** Usage:  sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING
1347 **
1348 ** Call mprintf with two integer arguments and one string argument
1349 */
1350 static int sqlite3_snprintf_str(
1351   void *NotUsed,
1352   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1353   int argc,              /* Number of arguments */
1354   char **argv            /* Text of each argument */
1355 ){
1356   int a[3], i;
1357   int n;
1358   char *z;
1359   if( argc<5 || argc>6 ){
1360     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1361        " INT FORMAT INT INT ?STRING?\"", 0);
1362     return TCL_ERROR;
1363   }
1364   if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
1365   if( n<0 ){
1366     Tcl_AppendResult(interp, "N must be non-negative", 0);
1367     return TCL_ERROR;
1368   }
1369   for(i=3; i<5; i++){
1370     if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR;
1371   }
1372   z = sqlite3_malloc( n+1 );
1373   sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL);
1374   Tcl_AppendResult(interp, z, 0);
1375   sqlite3_free(z);
1376   return TCL_OK;
1377 }
1378 
1379 /*
1380 ** Usage:  sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE
1381 **
1382 ** Call mprintf with two integer arguments and one double argument
1383 */
1384 static int sqlite3_mprintf_double(
1385   void *NotUsed,
1386   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1387   int argc,              /* Number of arguments */
1388   char **argv            /* Text of each argument */
1389 ){
1390   int a[3], i;
1391   double r;
1392   char *z;
1393   if( argc!=5 ){
1394     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1395        " FORMAT INT INT DOUBLE\"", 0);
1396     return TCL_ERROR;
1397   }
1398   for(i=2; i<4; i++){
1399     if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1400   }
1401   if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;
1402   z = sqlite3_mprintf(argv[1], a[0], a[1], r);
1403   Tcl_AppendResult(interp, z, 0);
1404   sqlite3_free(z);
1405   return TCL_OK;
1406 }
1407 
1408 /*
1409 ** Usage:  sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE
1410 **
1411 ** Call mprintf with a single double argument which is the product of the
1412 ** two arguments given above.  This is used to generate overflow and underflow
1413 ** doubles to test that they are converted properly.
1414 */
1415 static int sqlite3_mprintf_scaled(
1416   void *NotUsed,
1417   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1418   int argc,              /* Number of arguments */
1419   char **argv            /* Text of each argument */
1420 ){
1421   int i;
1422   double r[2];
1423   char *z;
1424   if( argc!=4 ){
1425     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1426        " FORMAT DOUBLE DOUBLE\"", 0);
1427     return TCL_ERROR;
1428   }
1429   for(i=2; i<4; i++){
1430     if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR;
1431   }
1432   z = sqlite3_mprintf(argv[1], r[0]*r[1]);
1433   Tcl_AppendResult(interp, z, 0);
1434   sqlite3_free(z);
1435   return TCL_OK;
1436 }
1437 
1438 /*
1439 ** Usage:  sqlite3_mprintf_stronly FORMAT STRING
1440 **
1441 ** Call mprintf with a single double argument which is the product of the
1442 ** two arguments given above.  This is used to generate overflow and underflow
1443 ** doubles to test that they are converted properly.
1444 */
1445 static int sqlite3_mprintf_stronly(
1446   void *NotUsed,
1447   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1448   int argc,              /* Number of arguments */
1449   char **argv            /* Text of each argument */
1450 ){
1451   char *z;
1452   if( argc!=3 ){
1453     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1454        " FORMAT STRING\"", 0);
1455     return TCL_ERROR;
1456   }
1457   z = sqlite3_mprintf(argv[1], argv[2]);
1458   Tcl_AppendResult(interp, z, 0);
1459   sqlite3_free(z);
1460   return TCL_OK;
1461 }
1462 
1463 /*
1464 ** Usage:  sqlite3_mprintf_hexdouble FORMAT HEX
1465 **
1466 ** Call mprintf with a single double argument which is derived from the
1467 ** hexadecimal encoding of an IEEE double.
1468 */
1469 static int sqlite3_mprintf_hexdouble(
1470   void *NotUsed,
1471   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1472   int argc,              /* Number of arguments */
1473   char **argv            /* Text of each argument */
1474 ){
1475   char *z;
1476   double r;
1477   unsigned int x1, x2;
1478   sqlite_uint64 d;
1479   if( argc!=3 ){
1480     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1481        " FORMAT STRING\"", 0);
1482     return TCL_ERROR;
1483   }
1484   if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){
1485     Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0);
1486     return TCL_ERROR;
1487   }
1488   d = x2;
1489   d = (d<<32) + x1;
1490   memcpy(&r, &d, sizeof(r));
1491   z = sqlite3_mprintf(argv[1], r);
1492   Tcl_AppendResult(interp, z, 0);
1493   sqlite3_free(z);
1494   return TCL_OK;
1495 }
1496 
1497 /*
1498 ** Usage: sqlite3_enable_shared_cache ?BOOLEAN?
1499 **
1500 */
1501 #if !defined(SQLITE_OMIT_SHARED_CACHE)
1502 static int test_enable_shared(
1503   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1504   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1505   int objc,              /* Number of arguments */
1506   Tcl_Obj *CONST objv[]  /* Command arguments */
1507 ){
1508   int rc;
1509   int enable;
1510   int ret = 0;
1511 
1512   if( objc!=2 && objc!=1 ){
1513     Tcl_WrongNumArgs(interp, 1, objv, "?BOOLEAN?");
1514     return TCL_ERROR;
1515   }
1516   ret = sqlite3GlobalConfig.sharedCacheEnabled;
1517 
1518   if( objc==2 ){
1519     if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){
1520       return TCL_ERROR;
1521     }
1522     rc = sqlite3_enable_shared_cache(enable);
1523     if( rc!=SQLITE_OK ){
1524       Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
1525       return TCL_ERROR;
1526     }
1527   }
1528   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret));
1529   return TCL_OK;
1530 }
1531 #endif
1532 
1533 
1534 
1535 /*
1536 ** Usage: sqlite3_extended_result_codes   DB    BOOLEAN
1537 **
1538 */
1539 static int test_extended_result_codes(
1540   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1541   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1542   int objc,              /* Number of arguments */
1543   Tcl_Obj *CONST objv[]  /* Command arguments */
1544 ){
1545   int enable;
1546   sqlite3 *db;
1547 
1548   if( objc!=3 ){
1549     Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
1550     return TCL_ERROR;
1551   }
1552   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1553   if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR;
1554   sqlite3_extended_result_codes(db, enable);
1555   return TCL_OK;
1556 }
1557 
1558 /*
1559 ** Usage: sqlite3_libversion_number
1560 **
1561 */
1562 static int test_libversion_number(
1563   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1564   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1565   int objc,              /* Number of arguments */
1566   Tcl_Obj *CONST objv[]  /* Command arguments */
1567 ){
1568   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number()));
1569   return TCL_OK;
1570 }
1571 
1572 /*
1573 ** Usage: sqlite3_table_column_metadata DB dbname tblname colname
1574 **
1575 */
1576 static int test_table_column_metadata(
1577   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1578   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1579   int objc,              /* Number of arguments */
1580   Tcl_Obj *CONST objv[]  /* Command arguments */
1581 ){
1582   sqlite3 *db;
1583   const char *zDb;
1584   const char *zTbl;
1585   const char *zCol;
1586   int rc;
1587   Tcl_Obj *pRet;
1588 
1589   const char *zDatatype;
1590   const char *zCollseq;
1591   int notnull;
1592   int primarykey;
1593   int autoincrement;
1594 
1595   if( objc!=5 && objc!=4 ){
1596     Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname");
1597     return TCL_ERROR;
1598   }
1599   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1600   zDb = Tcl_GetString(objv[2]);
1601   zTbl = Tcl_GetString(objv[3]);
1602   zCol = objc==5 ? Tcl_GetString(objv[4]) : 0;
1603 
1604   if( strlen(zDb)==0 ) zDb = 0;
1605 
1606   rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol,
1607       &zDatatype, &zCollseq, &notnull, &primarykey, &autoincrement);
1608 
1609   if( rc!=SQLITE_OK ){
1610     Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
1611     return TCL_ERROR;
1612   }
1613 
1614   pRet = Tcl_NewObj();
1615   Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1));
1616   Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1));
1617   Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull));
1618   Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey));
1619   Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement));
1620   Tcl_SetObjResult(interp, pRet);
1621 
1622   return TCL_OK;
1623 }
1624 
1625 #ifndef SQLITE_OMIT_INCRBLOB
1626 
1627 static int blobHandleFromObj(
1628   Tcl_Interp *interp,
1629   Tcl_Obj *pObj,
1630   sqlite3_blob **ppBlob
1631 ){
1632   char *z;
1633   int n;
1634 
1635   z = Tcl_GetStringFromObj(pObj, &n);
1636   if( n==0 ){
1637     *ppBlob = 0;
1638   }else{
1639     int notUsed;
1640     Tcl_Channel channel;
1641     ClientData instanceData;
1642 
1643     channel = Tcl_GetChannel(interp, z, &notUsed);
1644     if( !channel ) return TCL_ERROR;
1645 
1646     Tcl_Flush(channel);
1647     Tcl_Seek(channel, 0, SEEK_SET);
1648 
1649     instanceData = Tcl_GetChannelInstanceData(channel);
1650     *ppBlob = *((sqlite3_blob **)instanceData);
1651   }
1652 
1653   return TCL_OK;
1654 }
1655 
1656 static int test_blob_reopen(
1657   ClientData clientData, /* Not used */
1658   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1659   int objc,              /* Number of arguments */
1660   Tcl_Obj *CONST objv[]  /* Command arguments */
1661 ){
1662   Tcl_WideInt iRowid;
1663   sqlite3_blob *pBlob;
1664   int rc;
1665 
1666   if( objc!=3 ){
1667     Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL ROWID");
1668     return TCL_ERROR;
1669   }
1670 
1671   if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
1672   if( Tcl_GetWideIntFromObj(interp, objv[2], &iRowid) ) return TCL_ERROR;
1673 
1674   rc = sqlite3_blob_reopen(pBlob, iRowid);
1675   if( rc!=SQLITE_OK ){
1676     Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
1677   }
1678 
1679   return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1680 }
1681 
1682 #endif
1683 
1684 /*
1685 ** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC
1686 **
1687 **   This Tcl proc is used for testing the experimental
1688 **   sqlite3_create_collation_v2() interface.
1689 */
1690 struct TestCollationX {
1691   Tcl_Interp *interp;
1692   Tcl_Obj *pCmp;
1693   Tcl_Obj *pDel;
1694 };
1695 typedef struct TestCollationX TestCollationX;
1696 static void testCreateCollationDel(void *pCtx){
1697   TestCollationX *p = (TestCollationX *)pCtx;
1698 
1699   int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
1700   if( rc!=TCL_OK ){
1701     Tcl_BackgroundError(p->interp);
1702   }
1703 
1704   Tcl_DecrRefCount(p->pCmp);
1705   Tcl_DecrRefCount(p->pDel);
1706   sqlite3_free((void *)p);
1707 }
1708 static int testCreateCollationCmp(
1709   void *pCtx,
1710   int nLeft,
1711   const void *zLeft,
1712   int nRight,
1713   const void *zRight
1714 ){
1715   TestCollationX *p = (TestCollationX *)pCtx;
1716   Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp);
1717   int iRes = 0;
1718 
1719   Tcl_IncrRefCount(pScript);
1720   Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft));
1721   Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight));
1722 
1723   if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL)
1724    || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes)
1725   ){
1726     Tcl_BackgroundError(p->interp);
1727   }
1728   Tcl_DecrRefCount(pScript);
1729 
1730   return iRes;
1731 }
1732 static int test_create_collation_v2(
1733   ClientData clientData, /* Not used */
1734   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1735   int objc,              /* Number of arguments */
1736   Tcl_Obj *CONST objv[]  /* Command arguments */
1737 ){
1738   TestCollationX *p;
1739   sqlite3 *db;
1740   int rc;
1741 
1742   if( objc!=5 ){
1743     Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC");
1744     return TCL_ERROR;
1745   }
1746   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1747 
1748   p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX));
1749   p->pCmp = objv[3];
1750   p->pDel = objv[4];
1751   p->interp = interp;
1752   Tcl_IncrRefCount(p->pCmp);
1753   Tcl_IncrRefCount(p->pDel);
1754 
1755   rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), 16,
1756       (void *)p, testCreateCollationCmp, testCreateCollationDel
1757   );
1758   if( rc!=SQLITE_MISUSE ){
1759     Tcl_AppendResult(interp, "sqlite3_create_collate_v2() failed to detect "
1760       "an invalid encoding", (char*)0);
1761     return TCL_ERROR;
1762   }
1763   rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8,
1764       (void *)p, testCreateCollationCmp, testCreateCollationDel
1765   );
1766   return TCL_OK;
1767 }
1768 
1769 /*
1770 ** USAGE: sqlite3_create_function_v2 DB NAME NARG ENC ?SWITCHES?
1771 **
1772 ** Available switches are:
1773 **
1774 **   -func    SCRIPT
1775 **   -step    SCRIPT
1776 **   -final   SCRIPT
1777 **   -destroy SCRIPT
1778 */
1779 typedef struct CreateFunctionV2 CreateFunctionV2;
1780 struct CreateFunctionV2 {
1781   Tcl_Interp *interp;
1782   Tcl_Obj *pFunc;                 /* Script for function invocation */
1783   Tcl_Obj *pStep;                 /* Script for agg. step invocation */
1784   Tcl_Obj *pFinal;                /* Script for agg. finalization invocation */
1785   Tcl_Obj *pDestroy;              /* Destructor script */
1786 };
1787 static void cf2Func(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
1788 }
1789 static void cf2Step(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
1790 }
1791 static void cf2Final(sqlite3_context *ctx){
1792 }
1793 static void cf2Destroy(void *pUser){
1794   CreateFunctionV2 *p = (CreateFunctionV2 *)pUser;
1795 
1796   if( p->interp && p->pDestroy ){
1797     int rc = Tcl_EvalObjEx(p->interp, p->pDestroy, 0);
1798     if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
1799   }
1800 
1801   if( p->pFunc ) Tcl_DecrRefCount(p->pFunc);
1802   if( p->pStep ) Tcl_DecrRefCount(p->pStep);
1803   if( p->pFinal ) Tcl_DecrRefCount(p->pFinal);
1804   if( p->pDestroy ) Tcl_DecrRefCount(p->pDestroy);
1805   sqlite3_free(p);
1806 }
1807 static int test_create_function_v2(
1808   ClientData clientData,          /* Not used */
1809   Tcl_Interp *interp,             /* The invoking TCL interpreter */
1810   int objc,                       /* Number of arguments */
1811   Tcl_Obj *CONST objv[]           /* Command arguments */
1812 ){
1813   sqlite3 *db;
1814   const char *zFunc;
1815   int nArg;
1816   int enc;
1817   CreateFunctionV2 *p;
1818   int i;
1819   int rc;
1820 
1821   struct EncTable {
1822     const char *zEnc;
1823     int enc;
1824   } aEnc[] = {
1825     {"utf8",    SQLITE_UTF8 },
1826     {"utf16",   SQLITE_UTF16 },
1827     {"utf16le", SQLITE_UTF16LE },
1828     {"utf16be", SQLITE_UTF16BE },
1829     {"any",     SQLITE_ANY },
1830     {"0", 0 }
1831   };
1832 
1833   if( objc<5 || (objc%2)==0 ){
1834     Tcl_WrongNumArgs(interp, 1, objv, "DB NAME NARG ENC SWITCHES...");
1835     return TCL_ERROR;
1836   }
1837 
1838   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1839   zFunc = Tcl_GetString(objv[2]);
1840   if( Tcl_GetIntFromObj(interp, objv[3], &nArg) ) return TCL_ERROR;
1841   if( Tcl_GetIndexFromObjStruct(interp, objv[4], aEnc, sizeof(aEnc[0]),
1842           "encoding", 0, &enc)
1843   ){
1844     return TCL_ERROR;
1845   }
1846   enc = aEnc[enc].enc;
1847 
1848   p = sqlite3_malloc(sizeof(CreateFunctionV2));
1849   assert( p );
1850   memset(p, 0, sizeof(CreateFunctionV2));
1851   p->interp = interp;
1852 
1853   for(i=5; i<objc; i+=2){
1854     int iSwitch;
1855     const char *azSwitch[] = {"-func", "-step", "-final", "-destroy", 0};
1856     if( Tcl_GetIndexFromObj(interp, objv[i], azSwitch, "switch", 0, &iSwitch) ){
1857       sqlite3_free(p);
1858       return TCL_ERROR;
1859     }
1860 
1861     switch( iSwitch ){
1862       case 0: p->pFunc = objv[i+1];      break;
1863       case 1: p->pStep = objv[i+1];      break;
1864       case 2: p->pFinal = objv[i+1];     break;
1865       case 3: p->pDestroy = objv[i+1];   break;
1866     }
1867   }
1868   if( p->pFunc ) p->pFunc = Tcl_DuplicateObj(p->pFunc);
1869   if( p->pStep ) p->pStep = Tcl_DuplicateObj(p->pStep);
1870   if( p->pFinal ) p->pFinal = Tcl_DuplicateObj(p->pFinal);
1871   if( p->pDestroy ) p->pDestroy = Tcl_DuplicateObj(p->pDestroy);
1872 
1873   if( p->pFunc ) Tcl_IncrRefCount(p->pFunc);
1874   if( p->pStep ) Tcl_IncrRefCount(p->pStep);
1875   if( p->pFinal ) Tcl_IncrRefCount(p->pFinal);
1876   if( p->pDestroy ) Tcl_IncrRefCount(p->pDestroy);
1877 
1878   rc = sqlite3_create_function_v2(db, zFunc, nArg, enc, (void *)p,
1879       (p->pFunc ? cf2Func : 0),
1880       (p->pStep ? cf2Step : 0),
1881       (p->pFinal ? cf2Final : 0),
1882       cf2Destroy
1883   );
1884   if( rc!=SQLITE_OK ){
1885     Tcl_ResetResult(interp);
1886     Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
1887     return TCL_ERROR;
1888   }
1889   return TCL_OK;
1890 }
1891 
1892 /*
1893 ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
1894 */
1895 static int test_load_extension(
1896   ClientData clientData, /* Not used */
1897   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1898   int objc,              /* Number of arguments */
1899   Tcl_Obj *CONST objv[]  /* Command arguments */
1900 ){
1901   Tcl_CmdInfo cmdInfo;
1902   sqlite3 *db;
1903   int rc;
1904   char *zDb;
1905   char *zFile;
1906   char *zProc = 0;
1907   char *zErr = 0;
1908 
1909   if( objc!=4 && objc!=3 ){
1910     Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?");
1911     return TCL_ERROR;
1912   }
1913   zDb = Tcl_GetString(objv[1]);
1914   zFile = Tcl_GetString(objv[2]);
1915   if( objc==4 ){
1916     zProc = Tcl_GetString(objv[3]);
1917   }
1918 
1919   /* Extract the C database handle from the Tcl command name */
1920   if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1921     Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1922     return TCL_ERROR;
1923   }
1924   db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1925   assert(db);
1926 
1927   /* Call the underlying C function. If an error occurs, set rc to
1928   ** TCL_ERROR and load any error string into the interpreter. If no
1929   ** error occurs, set rc to TCL_OK.
1930   */
1931 #ifdef SQLITE_OMIT_LOAD_EXTENSION
1932   rc = SQLITE_ERROR;
1933   zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()");
1934   (void)zProc;
1935   (void)zFile;
1936 #else
1937   rc = sqlite3_load_extension(db, zFile, zProc, &zErr);
1938 #endif
1939   if( rc!=SQLITE_OK ){
1940     Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE);
1941     rc = TCL_ERROR;
1942   }else{
1943     rc = TCL_OK;
1944   }
1945   sqlite3_free(zErr);
1946 
1947   return rc;
1948 }
1949 
1950 /*
1951 ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
1952 */
1953 static int test_enable_load(
1954   ClientData clientData, /* Not used */
1955   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1956   int objc,              /* Number of arguments */
1957   Tcl_Obj *CONST objv[]  /* Command arguments */
1958 ){
1959   Tcl_CmdInfo cmdInfo;
1960   sqlite3 *db;
1961   char *zDb;
1962   int onoff;
1963 
1964   if( objc!=3 ){
1965     Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
1966     return TCL_ERROR;
1967   }
1968   zDb = Tcl_GetString(objv[1]);
1969 
1970   /* Extract the C database handle from the Tcl command name */
1971   if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1972     Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1973     return TCL_ERROR;
1974   }
1975   db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1976   assert(db);
1977 
1978   /* Get the onoff parameter */
1979   if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
1980     return TCL_ERROR;
1981   }
1982 
1983 #ifdef SQLITE_OMIT_LOAD_EXTENSION
1984   Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
1985   return TCL_ERROR;
1986 #else
1987   sqlite3_enable_load_extension(db, onoff);
1988   return TCL_OK;
1989 #endif
1990 }
1991 
1992 /*
1993 ** Usage:  sqlite_abort
1994 **
1995 ** Shutdown the process immediately.  This is not a clean shutdown.
1996 ** This command is used to test the recoverability of a database in
1997 ** the event of a program crash.
1998 */
1999 static int sqlite_abort(
2000   void *NotUsed,
2001   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
2002   int argc,              /* Number of arguments */
2003   char **argv            /* Text of each argument */
2004 ){
2005 #if defined(_MSC_VER)
2006   /* We do this, otherwise the test will halt with a popup message
2007    * that we have to click away before the test will continue.
2008    */
2009   _set_abort_behavior( 0, _CALL_REPORTFAULT );
2010 #endif
2011   exit(255);
2012   assert( interp==0 );   /* This will always fail */
2013   return TCL_OK;
2014 }
2015 
2016 /*
2017 ** The following routine is a user-defined SQL function whose purpose
2018 ** is to test the sqlite_set_result() API.
2019 */
2020 static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
2021   while( argc>=2 ){
2022     const char *zArg0 = (char*)sqlite3_value_text(argv[0]);
2023     if( zArg0 ){
2024       if( 0==sqlite3StrICmp(zArg0, "int") ){
2025         sqlite3_result_int(context, sqlite3_value_int(argv[1]));
2026       }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
2027         sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
2028       }else if( sqlite3StrICmp(zArg0,"string")==0 ){
2029         sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1,
2030             SQLITE_TRANSIENT);
2031       }else if( sqlite3StrICmp(zArg0,"double")==0 ){
2032         sqlite3_result_double(context, sqlite3_value_double(argv[1]));
2033       }else if( sqlite3StrICmp(zArg0,"null")==0 ){
2034         sqlite3_result_null(context);
2035       }else if( sqlite3StrICmp(zArg0,"value")==0 ){
2036         sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
2037       }else{
2038         goto error_out;
2039       }
2040     }else{
2041       goto error_out;
2042     }
2043     argc -= 2;
2044     argv += 2;
2045   }
2046   return;
2047 
2048 error_out:
2049   sqlite3_result_error(context,"first argument should be one of: "
2050       "int int64 string double null value", -1);
2051 }
2052 
2053 /*
2054 ** Usage:   sqlite_register_test_function  DB  NAME
2055 **
2056 ** Register the test SQL function on the database DB under the name NAME.
2057 */
2058 static int test_register_func(
2059   void *NotUsed,
2060   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
2061   int argc,              /* Number of arguments */
2062   char **argv            /* Text of each argument */
2063 ){
2064   sqlite3 *db;
2065   int rc;
2066   if( argc!=3 ){
2067     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2068        " DB FUNCTION-NAME", 0);
2069     return TCL_ERROR;
2070   }
2071   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
2072   rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,
2073       testFunc, 0, 0);
2074   if( rc!=0 ){
2075     Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
2076     return TCL_ERROR;
2077   }
2078   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2079   return TCL_OK;
2080 }
2081 
2082 /*
2083 ** Usage:  sqlite3_finalize  STMT
2084 **
2085 ** Finalize a statement handle.
2086 */
2087 static int test_finalize(
2088   void * clientData,
2089   Tcl_Interp *interp,
2090   int objc,
2091   Tcl_Obj *CONST objv[]
2092 ){
2093   sqlite3_stmt *pStmt;
2094   int rc;
2095   sqlite3 *db = 0;
2096 
2097   if( objc!=2 ){
2098     Tcl_AppendResult(interp, "wrong # args: should be \"",
2099         Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2100     return TCL_ERROR;
2101   }
2102 
2103   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2104 
2105   if( pStmt ){
2106     db = StmtToDb(pStmt);
2107   }
2108   rc = sqlite3_finalize(pStmt);
2109   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
2110   if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2111   return TCL_OK;
2112 }
2113 
2114 /*
2115 ** Usage:  sqlite3_stmt_status  STMT  CODE  RESETFLAG
2116 **
2117 ** Get the value of a status counter from a statement.
2118 */
2119 static int test_stmt_status(
2120   void * clientData,
2121   Tcl_Interp *interp,
2122   int objc,
2123   Tcl_Obj *CONST objv[]
2124 ){
2125   int iValue;
2126   int i, op = 0, resetFlag;
2127   const char *zOpName;
2128   sqlite3_stmt *pStmt;
2129 
2130   static const struct {
2131     const char *zName;
2132     int op;
2133   } aOp[] = {
2134     { "SQLITE_STMTSTATUS_FULLSCAN_STEP",   SQLITE_STMTSTATUS_FULLSCAN_STEP   },
2135     { "SQLITE_STMTSTATUS_SORT",            SQLITE_STMTSTATUS_SORT            },
2136     { "SQLITE_STMTSTATUS_AUTOINDEX",       SQLITE_STMTSTATUS_AUTOINDEX       },
2137     { "SQLITE_STMTSTATUS_VM_STEP",         SQLITE_STMTSTATUS_VM_STEP         },
2138   };
2139   if( objc!=4 ){
2140     Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG");
2141     return TCL_ERROR;
2142   }
2143   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2144   zOpName = Tcl_GetString(objv[2]);
2145   for(i=0; i<ArraySize(aOp); i++){
2146     if( strcmp(aOp[i].zName, zOpName)==0 ){
2147       op = aOp[i].op;
2148       break;
2149     }
2150   }
2151   if( i>=ArraySize(aOp) ){
2152     if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR;
2153   }
2154   if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR;
2155   iValue = sqlite3_stmt_status(pStmt, op, resetFlag);
2156   Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue));
2157   return TCL_OK;
2158 }
2159 
2160 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
2161 /*
2162 ** Usage:  sqlite3_stmt_scanstatus STMT IDX
2163 */
2164 static int test_stmt_scanstatus(
2165   void * clientData,
2166   Tcl_Interp *interp,
2167   int objc,
2168   Tcl_Obj *CONST objv[]
2169 ){
2170   sqlite3_stmt *pStmt;            /* First argument */
2171   int idx;                        /* Second argument */
2172 
2173   const char *zName;
2174   const char *zExplain;
2175   sqlite3_int64 nLoop;
2176   sqlite3_int64 nVisit;
2177   double rEst;
2178   int res;
2179 
2180   if( objc!=3 ){
2181     Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX");
2182     return TCL_ERROR;
2183   }
2184   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2185   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2186 
2187   res = sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop);
2188   if( res==0 ){
2189     Tcl_Obj *pRet = Tcl_NewObj();
2190     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nLoop", -1));
2191     Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nLoop));
2192     sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2193     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nVisit", -1));
2194     Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nVisit));
2195     sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_EST, (void*)&rEst);
2196     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nEst", -1));
2197     Tcl_ListObjAppendElement(0, pRet, Tcl_NewDoubleObj(rEst));
2198     sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NAME, (void*)&zName);
2199     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zName", -1));
2200     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zName, -1));
2201     sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2202     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zExplain", -1));
2203     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zExplain, -1));
2204     Tcl_SetObjResult(interp, pRet);
2205   }else{
2206     Tcl_ResetResult(interp);
2207   }
2208   return TCL_OK;
2209 }
2210 
2211 /*
2212 ** Usage:  sqlite3_stmt_scanstatus_reset  STMT
2213 */
2214 static int test_stmt_scanstatus_reset(
2215   void * clientData,
2216   Tcl_Interp *interp,
2217   int objc,
2218   Tcl_Obj *CONST objv[]
2219 ){
2220   sqlite3_stmt *pStmt;            /* First argument */
2221   if( objc!=2 ){
2222     Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2223     return TCL_ERROR;
2224   }
2225   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2226   sqlite3_stmt_scanstatus_reset(pStmt);
2227   return TCL_OK;
2228 }
2229 #endif
2230 
2231 #ifdef SQLITE_ENABLE_SQLLOG
2232 /*
2233 ** Usage:  sqlite3_config_sqllog
2234 **
2235 ** Zero the SQLITE_CONFIG_SQLLOG configuration
2236 */
2237 static int test_config_sqllog(
2238   void * clientData,
2239   Tcl_Interp *interp,
2240   int objc,
2241   Tcl_Obj *CONST objv[]
2242 ){
2243   if( objc!=1 ){
2244     Tcl_WrongNumArgs(interp, 1, objv, "");
2245     return TCL_ERROR;
2246   }
2247   sqlite3_config(SQLITE_CONFIG_SQLLOG, 0, 0);
2248   return TCL_OK;
2249 }
2250 #endif
2251 
2252 /*
2253 ** Usage: vfs_current_time_int64
2254 **
2255 ** Return the value returned by the default VFS's xCurrentTimeInt64 method.
2256 */
2257 static int vfsCurrentTimeInt64(
2258   void * clientData,
2259   Tcl_Interp *interp,
2260   int objc,
2261   Tcl_Obj *CONST objv[]
2262 ){
2263   i64 t;
2264   sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
2265   if( objc!=1 ){
2266     Tcl_WrongNumArgs(interp, 1, objv, "");
2267     return TCL_ERROR;
2268   }
2269   pVfs->xCurrentTimeInt64(pVfs, &t);
2270   Tcl_SetObjResult(interp, Tcl_NewWideIntObj(t));
2271   return TCL_OK;
2272 }
2273 
2274 #ifdef SQLITE_ENABLE_SNAPSHOT
2275 /*
2276 ** Usage: sqlite3_snapshot_get DB DBNAME
2277 */
2278 static int test_snapshot_get(
2279   void * clientData,
2280   Tcl_Interp *interp,
2281   int objc,
2282   Tcl_Obj *CONST objv[]
2283 ){
2284   int rc;
2285   sqlite3 *db;
2286   char *zName;
2287   sqlite3_snapshot *pSnapshot = 0;
2288 
2289   if( objc!=3 ){
2290     Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
2291     return TCL_ERROR;
2292   }
2293   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2294   zName = Tcl_GetString(objv[2]);
2295 
2296   rc = sqlite3_snapshot_get(db, zName, &pSnapshot);
2297   if( rc!=SQLITE_OK ){
2298     Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2299     return TCL_ERROR;
2300   }else{
2301     char zBuf[100];
2302     if( sqlite3TestMakePointerStr(interp, zBuf, pSnapshot) ) return TCL_ERROR;
2303     Tcl_SetObjResult(interp, Tcl_NewStringObj(zBuf, -1));
2304   }
2305   return TCL_OK;
2306 }
2307 #endif /* SQLITE_ENABLE_SNAPSHOT */
2308 
2309 #ifdef SQLITE_ENABLE_SNAPSHOT
2310 /*
2311 ** Usage: sqlite3_snapshot_open DB DBNAME SNAPSHOT
2312 */
2313 static int test_snapshot_open(
2314   void * clientData,
2315   Tcl_Interp *interp,
2316   int objc,
2317   Tcl_Obj *CONST objv[]
2318 ){
2319   int rc;
2320   sqlite3 *db;
2321   char *zName;
2322   sqlite3_snapshot *pSnapshot;
2323 
2324   if( objc!=4 ){
2325     Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT");
2326     return TCL_ERROR;
2327   }
2328   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2329   zName = Tcl_GetString(objv[2]);
2330   pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[3]));
2331 
2332   rc = sqlite3_snapshot_open(db, zName, pSnapshot);
2333   if( rc!=SQLITE_OK ){
2334     Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
2335     return TCL_ERROR;
2336   }
2337   return TCL_OK;
2338 }
2339 #endif /* SQLITE_ENABLE_SNAPSHOT */
2340 
2341 #ifdef SQLITE_ENABLE_SNAPSHOT
2342 /*
2343 ** Usage: sqlite3_snapshot_free SNAPSHOT
2344 */
2345 static int test_snapshot_free(
2346   void * clientData,
2347   Tcl_Interp *interp,
2348   int objc,
2349   Tcl_Obj *CONST objv[]
2350 ){
2351   sqlite3_snapshot *pSnapshot;
2352   if( objc!=2 ){
2353     Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT");
2354     return TCL_ERROR;
2355   }
2356   pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
2357   sqlite3_snapshot_free(pSnapshot);
2358   return TCL_OK;
2359 }
2360 #endif /* SQLITE_ENABLE_SNAPSHOT */
2361 
2362 #ifdef SQLITE_ENABLE_SNAPSHOT
2363 /*
2364 ** Usage: sqlite3_snapshot_cmp SNAPSHOT1 SNAPSHOT2
2365 */
2366 static int test_snapshot_cmp(
2367   void * clientData,
2368   Tcl_Interp *interp,
2369   int objc,
2370   Tcl_Obj *CONST objv[]
2371 ){
2372   int res;
2373   sqlite3_snapshot *p1;
2374   sqlite3_snapshot *p2;
2375   if( objc!=3 ){
2376     Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT1 SNAPSHOT2");
2377     return TCL_ERROR;
2378   }
2379   p1 = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
2380   p2 = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[2]));
2381   res = sqlite3_snapshot_cmp(p1, p2);
2382   Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
2383   return TCL_OK;
2384 }
2385 #endif /* SQLITE_ENABLE_SNAPSHOT */
2386 
2387 /*
2388 ** Usage:  sqlite3_next_stmt  DB  STMT
2389 **
2390 ** Return the next statment in sequence after STMT.
2391 */
2392 static int test_next_stmt(
2393   void * clientData,
2394   Tcl_Interp *interp,
2395   int objc,
2396   Tcl_Obj *CONST objv[]
2397 ){
2398   sqlite3_stmt *pStmt;
2399   sqlite3 *db = 0;
2400   char zBuf[50];
2401 
2402   if( objc!=3 ){
2403     Tcl_AppendResult(interp, "wrong # args: should be \"",
2404         Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0);
2405     return TCL_ERROR;
2406   }
2407 
2408   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2409   if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR;
2410   pStmt = sqlite3_next_stmt(db, pStmt);
2411   if( pStmt ){
2412     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
2413     Tcl_AppendResult(interp, zBuf, 0);
2414   }
2415   return TCL_OK;
2416 }
2417 
2418 /*
2419 ** Usage:  sqlite3_stmt_readonly  STMT
2420 **
2421 ** Return true if STMT is a NULL pointer or a pointer to a statement
2422 ** that is guaranteed to leave the database unmodified.
2423 */
2424 static int test_stmt_readonly(
2425   void * clientData,
2426   Tcl_Interp *interp,
2427   int objc,
2428   Tcl_Obj *CONST objv[]
2429 ){
2430   sqlite3_stmt *pStmt;
2431   int rc;
2432 
2433   if( objc!=2 ){
2434     Tcl_AppendResult(interp, "wrong # args: should be \"",
2435         Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2436     return TCL_ERROR;
2437   }
2438 
2439   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2440   rc = sqlite3_stmt_readonly(pStmt);
2441   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc));
2442   return TCL_OK;
2443 }
2444 
2445 /*
2446 ** Usage:  sqlite3_stmt_busy  STMT
2447 **
2448 ** Return true if STMT is a non-NULL pointer to a statement
2449 ** that has been stepped but not to completion.
2450 */
2451 static int test_stmt_busy(
2452   void * clientData,
2453   Tcl_Interp *interp,
2454   int objc,
2455   Tcl_Obj *CONST objv[]
2456 ){
2457   sqlite3_stmt *pStmt;
2458   int rc;
2459 
2460   if( objc!=2 ){
2461     Tcl_AppendResult(interp, "wrong # args: should be \"",
2462         Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2463     return TCL_ERROR;
2464   }
2465 
2466   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2467   rc = sqlite3_stmt_busy(pStmt);
2468   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc));
2469   return TCL_OK;
2470 }
2471 
2472 /*
2473 ** Usage:  uses_stmt_journal  STMT
2474 **
2475 ** Return true if STMT uses a statement journal.
2476 */
2477 static int uses_stmt_journal(
2478   void * clientData,
2479   Tcl_Interp *interp,
2480   int objc,
2481   Tcl_Obj *CONST objv[]
2482 ){
2483   sqlite3_stmt *pStmt;
2484 
2485   if( objc!=2 ){
2486     Tcl_AppendResult(interp, "wrong # args: should be \"",
2487         Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2488     return TCL_ERROR;
2489   }
2490 
2491   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2492   sqlite3_stmt_readonly(pStmt);
2493   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(((Vdbe *)pStmt)->usesStmtJournal));
2494   return TCL_OK;
2495 }
2496 
2497 
2498 /*
2499 ** Usage:  sqlite3_reset  STMT
2500 **
2501 ** Reset a statement handle.
2502 */
2503 static int test_reset(
2504   void * clientData,
2505   Tcl_Interp *interp,
2506   int objc,
2507   Tcl_Obj *CONST objv[]
2508 ){
2509   sqlite3_stmt *pStmt;
2510   int rc;
2511 
2512   if( objc!=2 ){
2513     Tcl_AppendResult(interp, "wrong # args: should be \"",
2514         Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2515     return TCL_ERROR;
2516   }
2517 
2518   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2519 
2520   rc = sqlite3_reset(pStmt);
2521   if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
2522     return TCL_ERROR;
2523   }
2524   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
2525 /*
2526   if( rc ){
2527     return TCL_ERROR;
2528   }
2529 */
2530   return TCL_OK;
2531 }
2532 
2533 /*
2534 ** Usage:  sqlite3_expired STMT
2535 **
2536 ** Return TRUE if a recompilation of the statement is recommended.
2537 */
2538 static int test_expired(
2539   void * clientData,
2540   Tcl_Interp *interp,
2541   int objc,
2542   Tcl_Obj *CONST objv[]
2543 ){
2544 #ifndef SQLITE_OMIT_DEPRECATED
2545   sqlite3_stmt *pStmt;
2546   if( objc!=2 ){
2547     Tcl_AppendResult(interp, "wrong # args: should be \"",
2548         Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2549     return TCL_ERROR;
2550   }
2551   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2552   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
2553 #endif
2554   return TCL_OK;
2555 }
2556 
2557 /*
2558 ** Usage:  sqlite3_transfer_bindings FROMSTMT TOSTMT
2559 **
2560 ** Transfer all bindings from FROMSTMT over to TOSTMT
2561 */
2562 static int test_transfer_bind(
2563   void * clientData,
2564   Tcl_Interp *interp,
2565   int objc,
2566   Tcl_Obj *CONST objv[]
2567 ){
2568 #ifndef SQLITE_OMIT_DEPRECATED
2569   sqlite3_stmt *pStmt1, *pStmt2;
2570   if( objc!=3 ){
2571     Tcl_AppendResult(interp, "wrong # args: should be \"",
2572         Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
2573     return TCL_ERROR;
2574   }
2575   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
2576   if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
2577   Tcl_SetObjResult(interp,
2578      Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
2579 #endif
2580   return TCL_OK;
2581 }
2582 
2583 /*
2584 ** Usage:  sqlite3_changes DB
2585 **
2586 ** Return the number of changes made to the database by the last SQL
2587 ** execution.
2588 */
2589 static int test_changes(
2590   void * clientData,
2591   Tcl_Interp *interp,
2592   int objc,
2593   Tcl_Obj *CONST objv[]
2594 ){
2595   sqlite3 *db;
2596   if( objc!=2 ){
2597     Tcl_AppendResult(interp, "wrong # args: should be \"",
2598        Tcl_GetString(objv[0]), " DB", 0);
2599     return TCL_ERROR;
2600   }
2601   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2602   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
2603   return TCL_OK;
2604 }
2605 
2606 /*
2607 ** This is the "static_bind_value" that variables are bound to when
2608 ** the FLAG option of sqlite3_bind is "static"
2609 */
2610 static char *sqlite_static_bind_value = 0;
2611 static int sqlite_static_bind_nbyte = 0;
2612 
2613 /*
2614 ** Usage:  sqlite3_bind  VM  IDX  VALUE  FLAGS
2615 **
2616 ** Sets the value of the IDX-th occurrence of "?" in the original SQL
2617 ** string.  VALUE is the new value.  If FLAGS=="null" then VALUE is
2618 ** ignored and the value is set to NULL.  If FLAGS=="static" then
2619 ** the value is set to the value of a static variable named
2620 ** "sqlite_static_bind_value".  If FLAGS=="normal" then a copy
2621 ** of the VALUE is made.  If FLAGS=="blob10" then a VALUE is ignored
2622 ** an a 10-byte blob "abc\000xyz\000pq" is inserted.
2623 */
2624 static int test_bind(
2625   void *NotUsed,
2626   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
2627   int argc,              /* Number of arguments */
2628   char **argv            /* Text of each argument */
2629 ){
2630   sqlite3_stmt *pStmt;
2631   int rc;
2632   int idx;
2633   if( argc!=5 ){
2634     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2635        " VM IDX VALUE (null|static|normal)\"", 0);
2636     return TCL_ERROR;
2637   }
2638   if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
2639   if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
2640   if( strcmp(argv[4],"null")==0 ){
2641     rc = sqlite3_bind_null(pStmt, idx);
2642   }else if( strcmp(argv[4],"static")==0 ){
2643     rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
2644   }else if( strcmp(argv[4],"static-nbytes")==0 ){
2645     rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value,
2646                                        sqlite_static_bind_nbyte, 0);
2647   }else if( strcmp(argv[4],"normal")==0 ){
2648     rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
2649   }else if( strcmp(argv[4],"blob10")==0 ){
2650     rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC);
2651   }else{
2652     Tcl_AppendResult(interp, "4th argument should be "
2653         "\"null\" or \"static\" or \"normal\"", 0);
2654     return TCL_ERROR;
2655   }
2656   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2657   if( rc ){
2658     char zBuf[50];
2659     sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
2660     Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
2661     return TCL_ERROR;
2662   }
2663   return TCL_OK;
2664 }
2665 
2666 #ifndef SQLITE_OMIT_UTF16
2667 /*
2668 ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
2669 **
2670 ** This function is used to test that SQLite selects the correct collation
2671 ** sequence callback when multiple versions (for different text encodings)
2672 ** are available.
2673 **
2674 ** Calling this routine registers the collation sequence "test_collate"
2675 ** with database handle <db>. The second argument must be a list of three
2676 ** boolean values. If the first is true, then a version of test_collate is
2677 ** registered for UTF-8, if the second is true, a version is registered for
2678 ** UTF-16le, if the third is true, a UTF-16be version is available.
2679 ** Previous versions of test_collate are deleted.
2680 **
2681 ** The collation sequence test_collate is implemented by calling the
2682 ** following TCL script:
2683 **
2684 **   "test_collate <enc> <lhs> <rhs>"
2685 **
2686 ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
2687 ** The <enc> parameter is the encoding of the collation function that
2688 ** SQLite selected to call. The TCL test script implements the
2689 ** "test_collate" proc.
2690 **
2691 ** Note that this will only work with one interpreter at a time, as the
2692 ** interp pointer to use when evaluating the TCL script is stored in
2693 ** pTestCollateInterp.
2694 */
2695 static Tcl_Interp* pTestCollateInterp;
2696 static int test_collate_func(
2697   void *pCtx,
2698   int nA, const void *zA,
2699   int nB, const void *zB
2700 ){
2701   Tcl_Interp *i = pTestCollateInterp;
2702   int encin = SQLITE_PTR_TO_INT(pCtx);
2703   int res;
2704   int n;
2705 
2706   sqlite3_value *pVal;
2707   Tcl_Obj *pX;
2708 
2709   pX = Tcl_NewStringObj("test_collate", -1);
2710   Tcl_IncrRefCount(pX);
2711 
2712   switch( encin ){
2713     case SQLITE_UTF8:
2714       Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
2715       break;
2716     case SQLITE_UTF16LE:
2717       Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
2718       break;
2719     case SQLITE_UTF16BE:
2720       Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
2721       break;
2722     default:
2723       assert(0);
2724   }
2725 
2726   sqlite3BeginBenignMalloc();
2727   pVal = sqlite3ValueNew(0);
2728   if( pVal ){
2729     sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
2730     n = sqlite3_value_bytes(pVal);
2731     Tcl_ListObjAppendElement(i,pX,
2732         Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
2733     sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
2734     n = sqlite3_value_bytes(pVal);
2735     Tcl_ListObjAppendElement(i,pX,
2736         Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
2737     sqlite3ValueFree(pVal);
2738   }
2739   sqlite3EndBenignMalloc();
2740 
2741   Tcl_EvalObjEx(i, pX, 0);
2742   Tcl_DecrRefCount(pX);
2743   Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
2744   return res;
2745 }
2746 static int test_collate(
2747   void * clientData,
2748   Tcl_Interp *interp,
2749   int objc,
2750   Tcl_Obj *CONST objv[]
2751 ){
2752   sqlite3 *db;
2753   int val;
2754   sqlite3_value *pVal;
2755   int rc;
2756 
2757   if( objc!=5 ) goto bad_args;
2758   pTestCollateInterp = interp;
2759   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2760 
2761   if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
2762   rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
2763           (void *)SQLITE_UTF8, val?test_collate_func:0);
2764   if( rc==SQLITE_OK ){
2765     const void *zUtf16;
2766     if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2767     rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
2768             (void *)SQLITE_UTF16LE, val?test_collate_func:0);
2769     if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
2770 
2771 #if 0
2772     if( sqlite3_iMallocFail>0 ){
2773       sqlite3_iMallocFail++;
2774     }
2775 #endif
2776     sqlite3_mutex_enter(db->mutex);
2777     pVal = sqlite3ValueNew(db);
2778     sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
2779     zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
2780     if( db->mallocFailed ){
2781       rc = SQLITE_NOMEM;
2782     }else{
2783       rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE,
2784           (void *)SQLITE_UTF16BE, val?test_collate_func:0);
2785     }
2786     sqlite3ValueFree(pVal);
2787     sqlite3_mutex_leave(db->mutex);
2788   }
2789   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2790 
2791   if( rc!=SQLITE_OK ){
2792     Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
2793     return TCL_ERROR;
2794   }
2795   return TCL_OK;
2796 
2797 bad_args:
2798   Tcl_AppendResult(interp, "wrong # args: should be \"",
2799       Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
2800   return TCL_ERROR;
2801 }
2802 
2803 /*
2804 ** Usage: add_test_utf16bin_collate <db ptr>
2805 **
2806 ** Add a utf-16 collation sequence named "utf16bin" to the database
2807 ** handle. This collation sequence compares arguments in the same way as the
2808 ** built-in collation "binary".
2809 */
2810 static int test_utf16bin_collate_func(
2811   void *pCtx,
2812   int nA, const void *zA,
2813   int nB, const void *zB
2814 ){
2815   int nCmp = (nA>nB ? nB : nA);
2816   int res = memcmp(zA, zB, nCmp);
2817   if( res==0 ) res = nA - nB;
2818   return res;
2819 }
2820 static int test_utf16bin_collate(
2821   void * clientData,
2822   Tcl_Interp *interp,
2823   int objc,
2824   Tcl_Obj *CONST objv[]
2825 ){
2826   sqlite3 *db;
2827   int rc;
2828 
2829   if( objc!=2 ) goto bad_args;
2830   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2831 
2832   rc = sqlite3_create_collation(db, "utf16bin", SQLITE_UTF16, 0,
2833       test_utf16bin_collate_func
2834   );
2835   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2836   return TCL_OK;
2837 
2838 bad_args:
2839   Tcl_WrongNumArgs(interp, 1, objv, "DB");
2840   return TCL_ERROR;
2841 }
2842 
2843 /*
2844 ** When the collation needed callback is invoked, record the name of
2845 ** the requested collating function here.  The recorded name is linked
2846 ** to a TCL variable and used to make sure that the requested collation
2847 ** name is correct.
2848 */
2849 static char zNeededCollation[200];
2850 static char *pzNeededCollation = zNeededCollation;
2851 
2852 
2853 /*
2854 ** Called when a collating sequence is needed.  Registered using
2855 ** sqlite3_collation_needed16().
2856 */
2857 static void test_collate_needed_cb(
2858   void *pCtx,
2859   sqlite3 *db,
2860   int eTextRep,
2861   const void *pName
2862 ){
2863   int enc = ENC(db);
2864   int i;
2865   char *z;
2866   for(z = (char*)pName, i=0; *z || z[1]; z++){
2867     if( *z ) zNeededCollation[i++] = *z;
2868   }
2869   zNeededCollation[i] = 0;
2870   sqlite3_create_collation(
2871       db, "test_collate", ENC(db), SQLITE_INT_TO_PTR(enc), test_collate_func);
2872 }
2873 
2874 /*
2875 ** Usage: add_test_collate_needed DB
2876 */
2877 static int test_collate_needed(
2878   void * clientData,
2879   Tcl_Interp *interp,
2880   int objc,
2881   Tcl_Obj *CONST objv[]
2882 ){
2883   sqlite3 *db;
2884   int rc;
2885 
2886   if( objc!=2 ) goto bad_args;
2887   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2888   rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
2889   zNeededCollation[0] = 0;
2890   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2891   return TCL_OK;
2892 
2893 bad_args:
2894   Tcl_WrongNumArgs(interp, 1, objv, "DB");
2895   return TCL_ERROR;
2896 }
2897 
2898 /*
2899 ** tclcmd:   add_alignment_test_collations  DB
2900 **
2901 ** Add two new collating sequences to the database DB
2902 **
2903 **     utf16_aligned
2904 **     utf16_unaligned
2905 **
2906 ** Both collating sequences use the same sort order as BINARY.
2907 ** The only difference is that the utf16_aligned collating
2908 ** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
2909 ** Both collating functions increment the unaligned utf16 counter
2910 ** whenever they see a string that begins on an odd byte boundary.
2911 */
2912 static int unaligned_string_counter = 0;
2913 static int alignmentCollFunc(
2914   void *NotUsed,
2915   int nKey1, const void *pKey1,
2916   int nKey2, const void *pKey2
2917 ){
2918   int rc, n;
2919   n = nKey1<nKey2 ? nKey1 : nKey2;
2920   if( nKey1>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey1))) ) unaligned_string_counter++;
2921   if( nKey2>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey2))) ) unaligned_string_counter++;
2922   rc = memcmp(pKey1, pKey2, n);
2923   if( rc==0 ){
2924     rc = nKey1 - nKey2;
2925   }
2926   return rc;
2927 }
2928 static int add_alignment_test_collations(
2929   void * clientData,
2930   Tcl_Interp *interp,
2931   int objc,
2932   Tcl_Obj *CONST objv[]
2933 ){
2934   sqlite3 *db;
2935   if( objc>=2 ){
2936     if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2937     sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16,
2938         0, alignmentCollFunc);
2939     sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED,
2940         0, alignmentCollFunc);
2941   }
2942   return SQLITE_OK;
2943 }
2944 #endif /* !defined(SQLITE_OMIT_UTF16) */
2945 
2946 /*
2947 ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
2948 **
2949 ** This function is used to test that SQLite selects the correct user
2950 ** function callback when multiple versions (for different text encodings)
2951 ** are available.
2952 **
2953 ** Calling this routine registers up to three versions of the user function
2954 ** "test_function" with database handle <db>.  If the second argument is
2955 ** true, then a version of test_function is registered for UTF-8, if the
2956 ** third is true, a version is registered for UTF-16le, if the fourth is
2957 ** true, a UTF-16be version is available.  Previous versions of
2958 ** test_function are deleted.
2959 **
2960 ** The user function is implemented by calling the following TCL script:
2961 **
2962 **   "test_function <enc> <arg>"
2963 **
2964 ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
2965 ** single argument passed to the SQL function. The value returned by
2966 ** the TCL script is used as the return value of the SQL function. It
2967 ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
2968 ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
2969 ** prefers UTF-16BE.
2970 */
2971 #ifndef SQLITE_OMIT_UTF16
2972 static void test_function_utf8(
2973   sqlite3_context *pCtx,
2974   int nArg,
2975   sqlite3_value **argv
2976 ){
2977   Tcl_Interp *interp;
2978   Tcl_Obj *pX;
2979   sqlite3_value *pVal;
2980   interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2981   pX = Tcl_NewStringObj("test_function", -1);
2982   Tcl_IncrRefCount(pX);
2983   Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
2984   Tcl_ListObjAppendElement(interp, pX,
2985       Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
2986   Tcl_EvalObjEx(interp, pX, 0);
2987   Tcl_DecrRefCount(pX);
2988   sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
2989   pVal = sqlite3ValueNew(0);
2990   sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
2991       SQLITE_UTF8, SQLITE_STATIC);
2992   sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
2993       -1, SQLITE_TRANSIENT);
2994   sqlite3ValueFree(pVal);
2995 }
2996 static void test_function_utf16le(
2997   sqlite3_context *pCtx,
2998   int nArg,
2999   sqlite3_value **argv
3000 ){
3001   Tcl_Interp *interp;
3002   Tcl_Obj *pX;
3003   sqlite3_value *pVal;
3004   interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
3005   pX = Tcl_NewStringObj("test_function", -1);
3006   Tcl_IncrRefCount(pX);
3007   Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
3008   Tcl_ListObjAppendElement(interp, pX,
3009       Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
3010   Tcl_EvalObjEx(interp, pX, 0);
3011   Tcl_DecrRefCount(pX);
3012   pVal = sqlite3ValueNew(0);
3013   sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
3014       SQLITE_UTF8, SQLITE_STATIC);
3015   sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
3016   sqlite3ValueFree(pVal);
3017 }
3018 static void test_function_utf16be(
3019   sqlite3_context *pCtx,
3020   int nArg,
3021   sqlite3_value **argv
3022 ){
3023   Tcl_Interp *interp;
3024   Tcl_Obj *pX;
3025   sqlite3_value *pVal;
3026   interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
3027   pX = Tcl_NewStringObj("test_function", -1);
3028   Tcl_IncrRefCount(pX);
3029   Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
3030   Tcl_ListObjAppendElement(interp, pX,
3031       Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
3032   Tcl_EvalObjEx(interp, pX, 0);
3033   Tcl_DecrRefCount(pX);
3034   pVal = sqlite3ValueNew(0);
3035   sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
3036       SQLITE_UTF8, SQLITE_STATIC);
3037   sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal),
3038       -1, SQLITE_TRANSIENT);
3039   sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal),
3040       -1, SQLITE_TRANSIENT);
3041   sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
3042       -1, SQLITE_TRANSIENT);
3043   sqlite3ValueFree(pVal);
3044 }
3045 #endif /* SQLITE_OMIT_UTF16 */
3046 static int test_function(
3047   void * clientData,
3048   Tcl_Interp *interp,
3049   int objc,
3050   Tcl_Obj *CONST objv[]
3051 ){
3052 #ifndef SQLITE_OMIT_UTF16
3053   sqlite3 *db;
3054   int val;
3055 
3056   if( objc!=5 ) goto bad_args;
3057   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3058 
3059   if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
3060   if( val ){
3061     sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
3062         interp, test_function_utf8, 0, 0);
3063   }
3064   if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
3065   if( val ){
3066     sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
3067         interp, test_function_utf16le, 0, 0);
3068   }
3069   if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
3070   if( val ){
3071     sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
3072         interp, test_function_utf16be, 0, 0);
3073   }
3074 
3075   return TCL_OK;
3076 bad_args:
3077   Tcl_AppendResult(interp, "wrong # args: should be \"",
3078       Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
3079 #endif /* SQLITE_OMIT_UTF16 */
3080   return TCL_ERROR;
3081 }
3082 
3083 /*
3084 ** Usage:         sqlite3_test_errstr <err code>
3085 **
3086 ** Test that the english language string equivalents for sqlite error codes
3087 ** are sane. The parameter is an integer representing an sqlite error code.
3088 ** The result is a list of two elements, the string representation of the
3089 ** error code and the english language explanation.
3090 */
3091 static int test_errstr(
3092   void * clientData,
3093   Tcl_Interp *interp,
3094   int objc,
3095   Tcl_Obj *CONST objv[]
3096 ){
3097   char *zCode;
3098   int i;
3099   if( objc!=1 ){
3100     Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
3101   }
3102 
3103   zCode = Tcl_GetString(objv[1]);
3104   for(i=0; i<200; i++){
3105     if( 0==strcmp(t1ErrorName(i), zCode) ) break;
3106   }
3107   Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
3108   return TCL_OK;
3109 }
3110 
3111 /*
3112 ** Usage:    breakpoint
3113 **
3114 ** This routine exists for one purpose - to provide a place to put a
3115 ** breakpoint with GDB that can be triggered using TCL code.  The use
3116 ** for this is when a particular test fails on (say) the 1485th iteration.
3117 ** In the TCL test script, we can add code like this:
3118 **
3119 **     if {$i==1485} breakpoint
3120 **
3121 ** Then run testfixture in the debugger and wait for the breakpoint to
3122 ** fire.  Then additional breakpoints can be set to trace down the bug.
3123 */
3124 static int test_breakpoint(
3125   void *NotUsed,
3126   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
3127   int argc,              /* Number of arguments */
3128   char **argv            /* Text of each argument */
3129 ){
3130   return TCL_OK;         /* Do nothing */
3131 }
3132 
3133 /*
3134 ** Usage:   sqlite3_bind_zeroblob  STMT IDX N
3135 **
3136 ** Test the sqlite3_bind_zeroblob interface.  STMT is a prepared statement.
3137 ** IDX is the index of a wildcard in the prepared statement.  This command
3138 ** binds a N-byte zero-filled BLOB to the wildcard.
3139 */
3140 static int test_bind_zeroblob(
3141   void * clientData,
3142   Tcl_Interp *interp,
3143   int objc,
3144   Tcl_Obj *CONST objv[]
3145 ){
3146   sqlite3_stmt *pStmt;
3147   int idx;
3148   int n;
3149   int rc;
3150 
3151   if( objc!=4 ){
3152     Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
3153     return TCL_ERROR;
3154   }
3155 
3156   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3157   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3158   if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
3159 
3160   rc = sqlite3_bind_zeroblob(pStmt, idx, n);
3161   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3162   if( rc!=SQLITE_OK ){
3163     return TCL_ERROR;
3164   }
3165 
3166   return TCL_OK;
3167 }
3168 
3169 /*
3170 ** Usage:   sqlite3_bind_zeroblob64  STMT IDX N
3171 **
3172 ** Test the sqlite3_bind_zeroblob64 interface.  STMT is a prepared statement.
3173 ** IDX is the index of a wildcard in the prepared statement.  This command
3174 ** binds a N-byte zero-filled BLOB to the wildcard.
3175 */
3176 static int test_bind_zeroblob64(
3177   void * clientData,
3178   Tcl_Interp *interp,
3179   int objc,
3180   Tcl_Obj *CONST objv[]
3181 ){
3182   sqlite3_stmt *pStmt;
3183   int idx;
3184   Tcl_WideInt n;
3185   int rc;
3186 
3187   if( objc!=4 ){
3188     Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
3189     return TCL_ERROR;
3190   }
3191 
3192   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3193   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3194   if( Tcl_GetWideIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
3195 
3196   rc = sqlite3_bind_zeroblob64(pStmt, idx, n);
3197   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3198   if( rc!=SQLITE_OK ){
3199     Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3200     return TCL_ERROR;
3201   }
3202 
3203   return TCL_OK;
3204 }
3205 
3206 /*
3207 ** Usage:   sqlite3_bind_int  STMT N VALUE
3208 **
3209 ** Test the sqlite3_bind_int interface.  STMT is a prepared statement.
3210 ** N is the index of a wildcard in the prepared statement.  This command
3211 ** binds a 32-bit integer VALUE to that wildcard.
3212 */
3213 static int test_bind_int(
3214   void * clientData,
3215   Tcl_Interp *interp,
3216   int objc,
3217   Tcl_Obj *CONST objv[]
3218 ){
3219   sqlite3_stmt *pStmt;
3220   int idx;
3221   int value;
3222   int rc;
3223 
3224   if( objc!=4 ){
3225     Tcl_AppendResult(interp, "wrong # args: should be \"",
3226         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3227     return TCL_ERROR;
3228   }
3229 
3230   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3231   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3232   if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
3233 
3234   rc = sqlite3_bind_int(pStmt, idx, value);
3235   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3236   if( rc!=SQLITE_OK ){
3237     return TCL_ERROR;
3238   }
3239 
3240   return TCL_OK;
3241 }
3242 
3243 
3244 /*
3245 ** Usage:   sqlite3_bind_int64  STMT N VALUE
3246 **
3247 ** Test the sqlite3_bind_int64 interface.  STMT is a prepared statement.
3248 ** N is the index of a wildcard in the prepared statement.  This command
3249 ** binds a 64-bit integer VALUE to that wildcard.
3250 */
3251 static int test_bind_int64(
3252   void * clientData,
3253   Tcl_Interp *interp,
3254   int objc,
3255   Tcl_Obj *CONST objv[]
3256 ){
3257   sqlite3_stmt *pStmt;
3258   int idx;
3259   Tcl_WideInt value;
3260   int rc;
3261 
3262   if( objc!=4 ){
3263     Tcl_AppendResult(interp, "wrong # args: should be \"",
3264         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3265     return TCL_ERROR;
3266   }
3267 
3268   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3269   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3270   if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
3271 
3272   rc = sqlite3_bind_int64(pStmt, idx, value);
3273   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3274   if( rc!=SQLITE_OK ){
3275     return TCL_ERROR;
3276   }
3277 
3278   return TCL_OK;
3279 }
3280 
3281 
3282 /*
3283 ** Usage:   sqlite3_bind_double  STMT N VALUE
3284 **
3285 ** Test the sqlite3_bind_double interface.  STMT is a prepared statement.
3286 ** N is the index of a wildcard in the prepared statement.  This command
3287 ** binds a 64-bit integer VALUE to that wildcard.
3288 */
3289 static int test_bind_double(
3290   void * clientData,
3291   Tcl_Interp *interp,
3292   int objc,
3293   Tcl_Obj *CONST objv[]
3294 ){
3295   sqlite3_stmt *pStmt;
3296   int idx;
3297   double value = 0;
3298   int rc;
3299   const char *zVal;
3300   int i;
3301   static const struct {
3302     const char *zName;     /* Name of the special floating point value */
3303     unsigned int iUpper;   /* Upper 32 bits */
3304     unsigned int iLower;   /* Lower 32 bits */
3305   } aSpecialFp[] = {
3306     {  "NaN",      0x7fffffff, 0xffffffff },
3307     {  "SNaN",     0x7ff7ffff, 0xffffffff },
3308     {  "-NaN",     0xffffffff, 0xffffffff },
3309     {  "-SNaN",    0xfff7ffff, 0xffffffff },
3310     {  "+Inf",     0x7ff00000, 0x00000000 },
3311     {  "-Inf",     0xfff00000, 0x00000000 },
3312     {  "Epsilon",  0x00000000, 0x00000001 },
3313     {  "-Epsilon", 0x80000000, 0x00000001 },
3314     {  "NaN0",     0x7ff80000, 0x00000000 },
3315     {  "-NaN0",    0xfff80000, 0x00000000 },
3316   };
3317 
3318   if( objc!=4 ){
3319     Tcl_AppendResult(interp, "wrong # args: should be \"",
3320         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3321     return TCL_ERROR;
3322   }
3323 
3324   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3325   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3326 
3327   /* Intercept the string "NaN" and generate a NaN value for it.
3328   ** All other strings are passed through to Tcl_GetDoubleFromObj().
3329   ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions
3330   ** contain a bug.
3331   */
3332   zVal = Tcl_GetString(objv[3]);
3333   for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){
3334     if( strcmp(aSpecialFp[i].zName, zVal)==0 ){
3335       sqlite3_uint64 x;
3336       x = aSpecialFp[i].iUpper;
3337       x <<= 32;
3338       x |= aSpecialFp[i].iLower;
3339       assert( sizeof(value)==8 );
3340       assert( sizeof(x)==8 );
3341       memcpy(&value, &x, 8);
3342       break;
3343     }
3344   }
3345   if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) &&
3346          Tcl_GetDoubleFromObj(interp, objv[3], &value) ){
3347     return TCL_ERROR;
3348   }
3349   rc = sqlite3_bind_double(pStmt, idx, value);
3350   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3351   if( rc!=SQLITE_OK ){
3352     return TCL_ERROR;
3353   }
3354 
3355   return TCL_OK;
3356 }
3357 
3358 /*
3359 ** Usage:   sqlite3_bind_null  STMT N
3360 **
3361 ** Test the sqlite3_bind_null interface.  STMT is a prepared statement.
3362 ** N is the index of a wildcard in the prepared statement.  This command
3363 ** binds a NULL to the wildcard.
3364 */
3365 static int test_bind_null(
3366   void * clientData,
3367   Tcl_Interp *interp,
3368   int objc,
3369   Tcl_Obj *CONST objv[]
3370 ){
3371   sqlite3_stmt *pStmt;
3372   int idx;
3373   int rc;
3374 
3375   if( objc!=3 ){
3376     Tcl_AppendResult(interp, "wrong # args: should be \"",
3377         Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
3378     return TCL_ERROR;
3379   }
3380 
3381   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3382   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3383 
3384   rc = sqlite3_bind_null(pStmt, idx);
3385   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3386   if( rc!=SQLITE_OK ){
3387     return TCL_ERROR;
3388   }
3389 
3390   return TCL_OK;
3391 }
3392 
3393 /*
3394 ** Usage:   sqlite3_bind_text  STMT N STRING BYTES
3395 **
3396 ** Test the sqlite3_bind_text interface.  STMT is a prepared statement.
3397 ** N is the index of a wildcard in the prepared statement.  This command
3398 ** binds a UTF-8 string STRING to the wildcard.  The string is BYTES bytes
3399 ** long.
3400 */
3401 static int test_bind_text(
3402   void * clientData,
3403   Tcl_Interp *interp,
3404   int objc,
3405   Tcl_Obj *CONST objv[]
3406 ){
3407   sqlite3_stmt *pStmt;
3408   int idx;
3409   int bytes;
3410   char *value;
3411   int rc;
3412 
3413   if( objc!=5 ){
3414     Tcl_AppendResult(interp, "wrong # args: should be \"",
3415         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
3416     return TCL_ERROR;
3417   }
3418 
3419   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3420   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3421   value = (char*)Tcl_GetByteArrayFromObj(objv[3], &bytes);
3422   if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
3423 
3424   rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
3425   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3426   if( rc!=SQLITE_OK ){
3427     Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3428     return TCL_ERROR;
3429   }
3430 
3431   return TCL_OK;
3432 }
3433 
3434 /*
3435 ** Usage:   sqlite3_bind_text16 ?-static? STMT N STRING BYTES
3436 **
3437 ** Test the sqlite3_bind_text16 interface.  STMT is a prepared statement.
3438 ** N is the index of a wildcard in the prepared statement.  This command
3439 ** binds a UTF-16 string STRING to the wildcard.  The string is BYTES bytes
3440 ** long.
3441 */
3442 static int test_bind_text16(
3443   void * clientData,
3444   Tcl_Interp *interp,
3445   int objc,
3446   Tcl_Obj *CONST objv[]
3447 ){
3448 #ifndef SQLITE_OMIT_UTF16
3449   sqlite3_stmt *pStmt;
3450   int idx;
3451   int bytes;
3452   char *value;
3453   int rc;
3454 
3455   void (*xDel)(void*) = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
3456   Tcl_Obj *oStmt    = objv[objc-4];
3457   Tcl_Obj *oN       = objv[objc-3];
3458   Tcl_Obj *oString  = objv[objc-2];
3459   Tcl_Obj *oBytes   = objv[objc-1];
3460 
3461   if( objc!=5 && objc!=6){
3462     Tcl_AppendResult(interp, "wrong # args: should be \"",
3463         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
3464     return TCL_ERROR;
3465   }
3466 
3467   if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR;
3468   if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR;
3469   value = (char*)Tcl_GetByteArrayFromObj(oString, 0);
3470   if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR;
3471 
3472   rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
3473   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3474   if( rc!=SQLITE_OK ){
3475     Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3476     return TCL_ERROR;
3477   }
3478 
3479 #endif /* SQLITE_OMIT_UTF16 */
3480   return TCL_OK;
3481 }
3482 
3483 /*
3484 ** Usage:   sqlite3_bind_blob ?-static? STMT N DATA BYTES
3485 **
3486 ** Test the sqlite3_bind_blob interface.  STMT is a prepared statement.
3487 ** N is the index of a wildcard in the prepared statement.  This command
3488 ** binds a BLOB to the wildcard.  The BLOB is BYTES bytes in size.
3489 */
3490 static int test_bind_blob(
3491   void * clientData,
3492   Tcl_Interp *interp,
3493   int objc,
3494   Tcl_Obj *CONST objv[]
3495 ){
3496   sqlite3_stmt *pStmt;
3497   int idx;
3498   int bytes;
3499   char *value;
3500   int rc;
3501   sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT;
3502 
3503   if( objc!=5 && objc!=6 ){
3504     Tcl_AppendResult(interp, "wrong # args: should be \"",
3505         Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
3506     return TCL_ERROR;
3507   }
3508 
3509   if( objc==6 ){
3510     xDestructor = SQLITE_STATIC;
3511     objv++;
3512   }
3513 
3514   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3515   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3516   value = Tcl_GetString(objv[3]);
3517   if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
3518 
3519   rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor);
3520   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3521   if( rc!=SQLITE_OK ){
3522     return TCL_ERROR;
3523   }
3524 
3525   return TCL_OK;
3526 }
3527 
3528 /*
3529 ** Usage:   sqlite3_bind_parameter_count  STMT
3530 **
3531 ** Return the number of wildcards in the given statement.
3532 */
3533 static int test_bind_parameter_count(
3534   void * clientData,
3535   Tcl_Interp *interp,
3536   int objc,
3537   Tcl_Obj *CONST objv[]
3538 ){
3539   sqlite3_stmt *pStmt;
3540 
3541   if( objc!=2 ){
3542     Tcl_WrongNumArgs(interp, 1, objv, "STMT");
3543     return TCL_ERROR;
3544   }
3545   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3546   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
3547   return TCL_OK;
3548 }
3549 
3550 /*
3551 ** Usage:   sqlite3_bind_parameter_name  STMT  N
3552 **
3553 ** Return the name of the Nth wildcard.  The first wildcard is 1.
3554 ** An empty string is returned if N is out of range or if the wildcard
3555 ** is nameless.
3556 */
3557 static int test_bind_parameter_name(
3558   void * clientData,
3559   Tcl_Interp *interp,
3560   int objc,
3561   Tcl_Obj *CONST objv[]
3562 ){
3563   sqlite3_stmt *pStmt;
3564   int i;
3565 
3566   if( objc!=3 ){
3567     Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
3568     return TCL_ERROR;
3569   }
3570   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3571   if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
3572   Tcl_SetObjResult(interp,
3573      Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
3574   );
3575   return TCL_OK;
3576 }
3577 
3578 /*
3579 ** Usage:   sqlite3_bind_parameter_index  STMT  NAME
3580 **
3581 ** Return the index of the wildcard called NAME.  Return 0 if there is
3582 ** no such wildcard.
3583 */
3584 static int test_bind_parameter_index(
3585   void * clientData,
3586   Tcl_Interp *interp,
3587   int objc,
3588   Tcl_Obj *CONST objv[]
3589 ){
3590   sqlite3_stmt *pStmt;
3591 
3592   if( objc!=3 ){
3593     Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
3594     return TCL_ERROR;
3595   }
3596   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3597   Tcl_SetObjResult(interp,
3598      Tcl_NewIntObj(
3599        sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
3600      )
3601   );
3602   return TCL_OK;
3603 }
3604 
3605 /*
3606 ** Usage:   sqlite3_clear_bindings STMT
3607 **
3608 */
3609 static int test_clear_bindings(
3610   void * clientData,
3611   Tcl_Interp *interp,
3612   int objc,
3613   Tcl_Obj *CONST objv[]
3614 ){
3615   sqlite3_stmt *pStmt;
3616 
3617   if( objc!=2 ){
3618     Tcl_WrongNumArgs(interp, 1, objv, "STMT");
3619     return TCL_ERROR;
3620   }
3621   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3622   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
3623   return TCL_OK;
3624 }
3625 
3626 /*
3627 ** Usage:   sqlite3_sleep MILLISECONDS
3628 */
3629 static int test_sleep(
3630   void * clientData,
3631   Tcl_Interp *interp,
3632   int objc,
3633   Tcl_Obj *CONST objv[]
3634 ){
3635   int ms;
3636 
3637   if( objc!=2 ){
3638     Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS");
3639     return TCL_ERROR;
3640   }
3641   if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){
3642     return TCL_ERROR;
3643   }
3644   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms)));
3645   return TCL_OK;
3646 }
3647 
3648 /*
3649 ** Usage: sqlite3_extended_errcode DB
3650 **
3651 ** Return the string representation of the most recent sqlite3_* API
3652 ** error code. e.g. "SQLITE_ERROR".
3653 */
3654 static int test_ex_errcode(
3655   void * clientData,
3656   Tcl_Interp *interp,
3657   int objc,
3658   Tcl_Obj *CONST objv[]
3659 ){
3660   sqlite3 *db;
3661   int rc;
3662 
3663   if( objc!=2 ){
3664     Tcl_AppendResult(interp, "wrong # args: should be \"",
3665        Tcl_GetString(objv[0]), " DB", 0);
3666     return TCL_ERROR;
3667   }
3668   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3669   rc = sqlite3_extended_errcode(db);
3670   Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
3671   return TCL_OK;
3672 }
3673 
3674 
3675 /*
3676 ** Usage: sqlite3_errcode DB
3677 **
3678 ** Return the string representation of the most recent sqlite3_* API
3679 ** error code. e.g. "SQLITE_ERROR".
3680 */
3681 static int test_errcode(
3682   void * clientData,
3683   Tcl_Interp *interp,
3684   int objc,
3685   Tcl_Obj *CONST objv[]
3686 ){
3687   sqlite3 *db;
3688   int rc;
3689 
3690   if( objc!=2 ){
3691     Tcl_AppendResult(interp, "wrong # args: should be \"",
3692        Tcl_GetString(objv[0]), " DB", 0);
3693     return TCL_ERROR;
3694   }
3695   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3696   rc = sqlite3_errcode(db);
3697   Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
3698   return TCL_OK;
3699 }
3700 
3701 /*
3702 ** Usage:   sqlite3_errmsg DB
3703 **
3704 ** Returns the UTF-8 representation of the error message string for the
3705 ** most recent sqlite3_* API call.
3706 */
3707 static int test_errmsg(
3708   void * clientData,
3709   Tcl_Interp *interp,
3710   int objc,
3711   Tcl_Obj *CONST objv[]
3712 ){
3713   sqlite3 *db;
3714   const char *zErr;
3715 
3716   if( objc!=2 ){
3717     Tcl_AppendResult(interp, "wrong # args: should be \"",
3718        Tcl_GetString(objv[0]), " DB", 0);
3719     return TCL_ERROR;
3720   }
3721   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3722 
3723   zErr = sqlite3_errmsg(db);
3724   Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
3725   return TCL_OK;
3726 }
3727 
3728 /*
3729 ** Usage:   test_errmsg16 DB
3730 **
3731 ** Returns the UTF-16 representation of the error message string for the
3732 ** most recent sqlite3_* API call. This is a byte array object at the TCL
3733 ** level, and it includes the 0x00 0x00 terminator bytes at the end of the
3734 ** UTF-16 string.
3735 */
3736 static int test_errmsg16(
3737   void * clientData,
3738   Tcl_Interp *interp,
3739   int objc,
3740   Tcl_Obj *CONST objv[]
3741 ){
3742 #ifndef SQLITE_OMIT_UTF16
3743   sqlite3 *db;
3744   const void *zErr;
3745   const char *z;
3746   int bytes = 0;
3747 
3748   if( objc!=2 ){
3749     Tcl_AppendResult(interp, "wrong # args: should be \"",
3750        Tcl_GetString(objv[0]), " DB", 0);
3751     return TCL_ERROR;
3752   }
3753   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3754 
3755   zErr = sqlite3_errmsg16(db);
3756   if( zErr ){
3757     z = zErr;
3758     for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){}
3759   }
3760   Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
3761 #endif /* SQLITE_OMIT_UTF16 */
3762   return TCL_OK;
3763 }
3764 
3765 /*
3766 ** Usage: sqlite3_prepare DB sql bytes ?tailvar?
3767 **
3768 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3769 ** database handle <DB>. The parameter <tailval> is the name of a global
3770 ** variable that is set to the unused portion of <sql> (if any). A
3771 ** STMT handle is returned.
3772 */
3773 static int test_prepare(
3774   void * clientData,
3775   Tcl_Interp *interp,
3776   int objc,
3777   Tcl_Obj *CONST objv[]
3778 ){
3779   sqlite3 *db;
3780   const char *zSql;
3781   int bytes;
3782   const char *zTail = 0;
3783   sqlite3_stmt *pStmt = 0;
3784   char zBuf[50];
3785   int rc;
3786 
3787   if( objc!=5 && objc!=4 ){
3788     Tcl_AppendResult(interp, "wrong # args: should be \"",
3789        Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
3790     return TCL_ERROR;
3791   }
3792   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3793   zSql = Tcl_GetString(objv[2]);
3794   if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3795 
3796   rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
3797   Tcl_ResetResult(interp);
3798   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3799   if( zTail && objc>=5 ){
3800     if( bytes>=0 ){
3801       bytes = bytes - (int)(zTail-zSql);
3802     }
3803     if( (int)strlen(zTail)<bytes ){
3804       bytes = (int)strlen(zTail);
3805     }
3806     Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3807   }
3808   if( rc!=SQLITE_OK ){
3809     assert( pStmt==0 );
3810     sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
3811     Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3812     return TCL_ERROR;
3813   }
3814 
3815   if( pStmt ){
3816     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3817     Tcl_AppendResult(interp, zBuf, 0);
3818   }
3819   return TCL_OK;
3820 }
3821 
3822 /*
3823 ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar?
3824 **
3825 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3826 ** database handle <DB>. The parameter <tailval> is the name of a global
3827 ** variable that is set to the unused portion of <sql> (if any). A
3828 ** STMT handle is returned.
3829 */
3830 static int test_prepare_v2(
3831   void * clientData,
3832   Tcl_Interp *interp,
3833   int objc,
3834   Tcl_Obj *CONST objv[]
3835 ){
3836   sqlite3 *db;
3837   const char *zSql;
3838   char *zCopy = 0;                /* malloc() copy of zSql */
3839   int bytes;
3840   const char *zTail = 0;
3841   sqlite3_stmt *pStmt = 0;
3842   char zBuf[50];
3843   int rc;
3844 
3845   if( objc!=5 && objc!=4 ){
3846     Tcl_AppendResult(interp, "wrong # args: should be \"",
3847        Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3848     return TCL_ERROR;
3849   }
3850   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3851   zSql = Tcl_GetString(objv[2]);
3852   if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3853 
3854   /* Instead of using zSql directly, make a copy into a buffer obtained
3855   ** directly from malloc(). The idea is to make it easier for valgrind
3856   ** to spot buffer overreads.  */
3857   if( bytes>=0 ){
3858     zCopy = malloc(bytes);
3859     memcpy(zCopy, zSql, bytes);
3860   }else{
3861     int n = (int)strlen(zSql) + 1;
3862     zCopy = malloc(n);
3863     memcpy(zCopy, zSql, n);
3864   }
3865   rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, objc>=5 ? &zTail : 0);
3866   free(zCopy);
3867   zTail = &zSql[(zTail - zCopy)];
3868 
3869   assert(rc==SQLITE_OK || pStmt==0);
3870   Tcl_ResetResult(interp);
3871   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3872   if( rc==SQLITE_OK && zTail && objc>=5 ){
3873     if( bytes>=0 ){
3874       bytes = bytes - (int)(zTail-zSql);
3875     }
3876     Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3877   }
3878   if( rc!=SQLITE_OK ){
3879     assert( pStmt==0 );
3880     sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
3881     Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3882     return TCL_ERROR;
3883   }
3884 
3885   if( pStmt ){
3886     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3887     Tcl_AppendResult(interp, zBuf, 0);
3888   }
3889   return TCL_OK;
3890 }
3891 
3892 /*
3893 ** Usage: sqlite3_prepare_tkt3134 DB
3894 **
3895 ** Generate a prepared statement for a zero-byte string as a test
3896 ** for ticket #3134.  The string should be preceded by a zero byte.
3897 */
3898 static int test_prepare_tkt3134(
3899   void * clientData,
3900   Tcl_Interp *interp,
3901   int objc,
3902   Tcl_Obj *CONST objv[]
3903 ){
3904   sqlite3 *db;
3905   static const char zSql[] = "\000SELECT 1";
3906   sqlite3_stmt *pStmt = 0;
3907   char zBuf[50];
3908   int rc;
3909 
3910   if( objc!=2 ){
3911     Tcl_AppendResult(interp, "wrong # args: should be \"",
3912        Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3913     return TCL_ERROR;
3914   }
3915   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3916   rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0);
3917   assert(rc==SQLITE_OK || pStmt==0);
3918   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3919   if( rc!=SQLITE_OK ){
3920     assert( pStmt==0 );
3921     sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
3922     Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3923     return TCL_ERROR;
3924   }
3925 
3926   if( pStmt ){
3927     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3928     Tcl_AppendResult(interp, zBuf, 0);
3929   }
3930   return TCL_OK;
3931 }
3932 
3933 /*
3934 ** Usage: sqlite3_prepare16 DB sql bytes tailvar
3935 **
3936 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3937 ** database handle <DB>. The parameter <tailval> is the name of a global
3938 ** variable that is set to the unused portion of <sql> (if any). A
3939 ** STMT handle is returned.
3940 */
3941 static int test_prepare16(
3942   void * clientData,
3943   Tcl_Interp *interp,
3944   int objc,
3945   Tcl_Obj *CONST objv[]
3946 ){
3947 #ifndef SQLITE_OMIT_UTF16
3948   sqlite3 *db;
3949   const void *zSql;
3950   const void *zTail = 0;
3951   Tcl_Obj *pTail = 0;
3952   sqlite3_stmt *pStmt = 0;
3953   char zBuf[50];
3954   int rc;
3955   int bytes;                /* The integer specified as arg 3 */
3956   int objlen;               /* The byte-array length of arg 2 */
3957 
3958   if( objc!=5 && objc!=4 ){
3959     Tcl_AppendResult(interp, "wrong # args: should be \"",
3960        Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
3961     return TCL_ERROR;
3962   }
3963   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3964   zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3965   if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3966 
3967   rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
3968   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3969   if( rc ){
3970     return TCL_ERROR;
3971   }
3972 
3973   if( objc>=5 ){
3974     if( zTail ){
3975       objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql);
3976     }else{
3977       objlen = 0;
3978     }
3979     pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3980     Tcl_IncrRefCount(pTail);
3981     Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
3982     Tcl_DecrRefCount(pTail);
3983   }
3984 
3985   if( pStmt ){
3986     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3987   }
3988   Tcl_AppendResult(interp, zBuf, 0);
3989 #endif /* SQLITE_OMIT_UTF16 */
3990   return TCL_OK;
3991 }
3992 
3993 /*
3994 ** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar?
3995 **
3996 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3997 ** database handle <DB>. The parameter <tailval> is the name of a global
3998 ** variable that is set to the unused portion of <sql> (if any). A
3999 ** STMT handle is returned.
4000 */
4001 static int test_prepare16_v2(
4002   void * clientData,
4003   Tcl_Interp *interp,
4004   int objc,
4005   Tcl_Obj *CONST objv[]
4006 ){
4007 #ifndef SQLITE_OMIT_UTF16
4008   sqlite3 *db;
4009   const void *zSql;
4010   const void *zTail = 0;
4011   Tcl_Obj *pTail = 0;
4012   sqlite3_stmt *pStmt = 0;
4013   char zBuf[50];
4014   int rc;
4015   int bytes;                /* The integer specified as arg 3 */
4016   int objlen;               /* The byte-array length of arg 2 */
4017 
4018   if( objc!=5 && objc!=4 ){
4019     Tcl_AppendResult(interp, "wrong # args: should be \"",
4020        Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
4021     return TCL_ERROR;
4022   }
4023   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4024   zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
4025   if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
4026 
4027   rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
4028   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
4029   if( rc ){
4030     return TCL_ERROR;
4031   }
4032 
4033   if( objc>=5 ){
4034     if( zTail ){
4035       objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql);
4036     }else{
4037       objlen = 0;
4038     }
4039     pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
4040     Tcl_IncrRefCount(pTail);
4041     Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
4042     Tcl_DecrRefCount(pTail);
4043   }
4044 
4045   if( pStmt ){
4046     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
4047   }
4048   Tcl_AppendResult(interp, zBuf, 0);
4049 #endif /* SQLITE_OMIT_UTF16 */
4050   return TCL_OK;
4051 }
4052 
4053 /*
4054 ** Usage: sqlite3_open filename ?options-list?
4055 */
4056 static int test_open(
4057   void * clientData,
4058   Tcl_Interp *interp,
4059   int objc,
4060   Tcl_Obj *CONST objv[]
4061 ){
4062   const char *zFilename;
4063   sqlite3 *db;
4064   char zBuf[100];
4065 
4066   if( objc!=3 && objc!=2 && objc!=1 ){
4067     Tcl_AppendResult(interp, "wrong # args: should be \"",
4068        Tcl_GetString(objv[0]), " filename options-list", 0);
4069     return TCL_ERROR;
4070   }
4071 
4072   zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0;
4073   sqlite3_open(zFilename, &db);
4074 
4075   if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
4076   Tcl_AppendResult(interp, zBuf, 0);
4077   return TCL_OK;
4078 }
4079 
4080 /*
4081 ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS
4082 */
4083 static int test_open_v2(
4084   void * clientData,
4085   Tcl_Interp *interp,
4086   int objc,
4087   Tcl_Obj *CONST objv[]
4088 ){
4089   const char *zFilename;
4090   const char *zVfs;
4091   int flags = 0;
4092   sqlite3 *db;
4093   int rc;
4094   char zBuf[100];
4095 
4096   int nFlag;
4097   Tcl_Obj **apFlag;
4098   int i;
4099 
4100   if( objc!=4 ){
4101     Tcl_WrongNumArgs(interp, 1, objv, "FILENAME FLAGS VFS");
4102     return TCL_ERROR;
4103   }
4104   zFilename = Tcl_GetString(objv[1]);
4105   zVfs = Tcl_GetString(objv[3]);
4106   if( zVfs[0]==0x00 ) zVfs = 0;
4107 
4108   rc = Tcl_ListObjGetElements(interp, objv[2], &nFlag, &apFlag);
4109   if( rc!=TCL_OK ) return rc;
4110   for(i=0; i<nFlag; i++){
4111     int iFlag;
4112     struct OpenFlag {
4113       const char *zFlag;
4114       int flag;
4115     } aFlag[] = {
4116       { "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY },
4117       { "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE },
4118       { "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE },
4119       { "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE },
4120       { "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE },
4121       { "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY },
4122       { "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB },
4123       { "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB },
4124       { "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB },
4125       { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL },
4126       { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL },
4127       { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL },
4128       { "SQLITE_OPEN_MASTER_JOURNAL", SQLITE_OPEN_MASTER_JOURNAL },
4129       { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX },
4130       { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX },
4131       { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE },
4132       { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE },
4133       { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL },
4134       { "SQLITE_OPEN_URI", SQLITE_OPEN_URI },
4135       { 0, 0 }
4136     };
4137     rc = Tcl_GetIndexFromObjStruct(interp, apFlag[i], aFlag, sizeof(aFlag[0]),
4138         "flag", 0, &iFlag
4139     );
4140     if( rc!=TCL_OK ) return rc;
4141     flags |= aFlag[iFlag].flag;
4142   }
4143 
4144   rc = sqlite3_open_v2(zFilename, &db, flags, zVfs);
4145   if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
4146   Tcl_AppendResult(interp, zBuf, 0);
4147   return TCL_OK;
4148 }
4149 
4150 /*
4151 ** Usage: sqlite3_open16 filename options
4152 */
4153 static int test_open16(
4154   void * clientData,
4155   Tcl_Interp *interp,
4156   int objc,
4157   Tcl_Obj *CONST objv[]
4158 ){
4159 #ifndef SQLITE_OMIT_UTF16
4160   const void *zFilename;
4161   sqlite3 *db;
4162   char zBuf[100];
4163 
4164   if( objc!=3 ){
4165     Tcl_AppendResult(interp, "wrong # args: should be \"",
4166        Tcl_GetString(objv[0]), " filename options-list", 0);
4167     return TCL_ERROR;
4168   }
4169 
4170   zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
4171   sqlite3_open16(zFilename, &db);
4172 
4173   if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
4174   Tcl_AppendResult(interp, zBuf, 0);
4175 #endif /* SQLITE_OMIT_UTF16 */
4176   return TCL_OK;
4177 }
4178 
4179 /*
4180 ** Usage: sqlite3_complete16 <UTF-16 string>
4181 **
4182 ** Return 1 if the supplied argument is a complete SQL statement, or zero
4183 ** otherwise.
4184 */
4185 static int test_complete16(
4186   void * clientData,
4187   Tcl_Interp *interp,
4188   int objc,
4189   Tcl_Obj *CONST objv[]
4190 ){
4191 #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
4192   char *zBuf;
4193 
4194   if( objc!=2 ){
4195     Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
4196     return TCL_ERROR;
4197   }
4198 
4199   zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0);
4200   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
4201 #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
4202   return TCL_OK;
4203 }
4204 
4205 /*
4206 ** Usage: sqlite3_step STMT
4207 **
4208 ** Advance the statement to the next row.
4209 */
4210 static int test_step(
4211   void * clientData,
4212   Tcl_Interp *interp,
4213   int objc,
4214   Tcl_Obj *CONST objv[]
4215 ){
4216   sqlite3_stmt *pStmt;
4217   int rc;
4218 
4219   if( objc!=2 ){
4220     Tcl_AppendResult(interp, "wrong # args: should be \"",
4221        Tcl_GetString(objv[0]), " STMT", 0);
4222     return TCL_ERROR;
4223   }
4224 
4225   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4226   rc = sqlite3_step(pStmt);
4227 
4228   /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
4229   Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
4230   return TCL_OK;
4231 }
4232 
4233 static int test_sql(
4234   void * clientData,
4235   Tcl_Interp *interp,
4236   int objc,
4237   Tcl_Obj *CONST objv[]
4238 ){
4239   sqlite3_stmt *pStmt;
4240 
4241   if( objc!=2 ){
4242     Tcl_WrongNumArgs(interp, 1, objv, "STMT");
4243     return TCL_ERROR;
4244   }
4245 
4246   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4247   Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE);
4248   return TCL_OK;
4249 }
4250 
4251 /*
4252 ** Usage: sqlite3_column_count STMT
4253 **
4254 ** Return the number of columns returned by the sql statement STMT.
4255 */
4256 static int test_column_count(
4257   void * clientData,
4258   Tcl_Interp *interp,
4259   int objc,
4260   Tcl_Obj *CONST objv[]
4261 ){
4262   sqlite3_stmt *pStmt;
4263 
4264   if( objc!=2 ){
4265     Tcl_AppendResult(interp, "wrong # args: should be \"",
4266        Tcl_GetString(objv[0]), " STMT column", 0);
4267     return TCL_ERROR;
4268   }
4269 
4270   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4271 
4272   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
4273   return TCL_OK;
4274 }
4275 
4276 /*
4277 ** Usage: sqlite3_column_type STMT column
4278 **
4279 ** Return the type of the data in column 'column' of the current row.
4280 */
4281 static int test_column_type(
4282   void * clientData,
4283   Tcl_Interp *interp,
4284   int objc,
4285   Tcl_Obj *CONST objv[]
4286 ){
4287   sqlite3_stmt *pStmt;
4288   int col;
4289   int tp;
4290 
4291   if( objc!=3 ){
4292     Tcl_AppendResult(interp, "wrong # args: should be \"",
4293        Tcl_GetString(objv[0]), " STMT column", 0);
4294     return TCL_ERROR;
4295   }
4296 
4297   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4298   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4299 
4300   tp = sqlite3_column_type(pStmt, col);
4301   switch( tp ){
4302     case SQLITE_INTEGER:
4303       Tcl_SetResult(interp, "INTEGER", TCL_STATIC);
4304       break;
4305     case SQLITE_NULL:
4306       Tcl_SetResult(interp, "NULL", TCL_STATIC);
4307       break;
4308     case SQLITE_FLOAT:
4309       Tcl_SetResult(interp, "FLOAT", TCL_STATIC);
4310       break;
4311     case SQLITE_TEXT:
4312       Tcl_SetResult(interp, "TEXT", TCL_STATIC);
4313       break;
4314     case SQLITE_BLOB:
4315       Tcl_SetResult(interp, "BLOB", TCL_STATIC);
4316       break;
4317     default:
4318       assert(0);
4319   }
4320 
4321   return TCL_OK;
4322 }
4323 
4324 /*
4325 ** Usage: sqlite3_column_int64 STMT column
4326 **
4327 ** Return the data in column 'column' of the current row cast as an
4328 ** wide (64-bit) integer.
4329 */
4330 static int test_column_int64(
4331   void * clientData,
4332   Tcl_Interp *interp,
4333   int objc,
4334   Tcl_Obj *CONST objv[]
4335 ){
4336   sqlite3_stmt *pStmt;
4337   int col;
4338   i64 iVal;
4339 
4340   if( objc!=3 ){
4341     Tcl_AppendResult(interp, "wrong # args: should be \"",
4342        Tcl_GetString(objv[0]), " STMT column", 0);
4343     return TCL_ERROR;
4344   }
4345 
4346   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4347   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4348 
4349   iVal = sqlite3_column_int64(pStmt, col);
4350   Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
4351   return TCL_OK;
4352 }
4353 
4354 /*
4355 ** Usage: sqlite3_column_blob STMT column
4356 */
4357 static int test_column_blob(
4358   void * clientData,
4359   Tcl_Interp *interp,
4360   int objc,
4361   Tcl_Obj *CONST objv[]
4362 ){
4363   sqlite3_stmt *pStmt;
4364   int col;
4365 
4366   int len;
4367   const void *pBlob;
4368 
4369   if( objc!=3 ){
4370     Tcl_AppendResult(interp, "wrong # args: should be \"",
4371        Tcl_GetString(objv[0]), " STMT column", 0);
4372     return TCL_ERROR;
4373   }
4374 
4375   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4376   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4377 
4378   len = sqlite3_column_bytes(pStmt, col);
4379   pBlob = sqlite3_column_blob(pStmt, col);
4380   Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
4381   return TCL_OK;
4382 }
4383 
4384 /*
4385 ** Usage: sqlite3_column_double STMT column
4386 **
4387 ** Return the data in column 'column' of the current row cast as a double.
4388 */
4389 static int test_column_double(
4390   void * clientData,
4391   Tcl_Interp *interp,
4392   int objc,
4393   Tcl_Obj *CONST objv[]
4394 ){
4395   sqlite3_stmt *pStmt;
4396   int col;
4397   double rVal;
4398 
4399   if( objc!=3 ){
4400     Tcl_AppendResult(interp, "wrong # args: should be \"",
4401        Tcl_GetString(objv[0]), " STMT column", 0);
4402     return TCL_ERROR;
4403   }
4404 
4405   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4406   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4407 
4408   rVal = sqlite3_column_double(pStmt, col);
4409   Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
4410   return TCL_OK;
4411 }
4412 
4413 /*
4414 ** Usage: sqlite3_data_count STMT
4415 **
4416 ** Return the number of columns returned by the sql statement STMT.
4417 */
4418 static int test_data_count(
4419   void * clientData,
4420   Tcl_Interp *interp,
4421   int objc,
4422   Tcl_Obj *CONST objv[]
4423 ){
4424   sqlite3_stmt *pStmt;
4425 
4426   if( objc!=2 ){
4427     Tcl_AppendResult(interp, "wrong # args: should be \"",
4428        Tcl_GetString(objv[0]), " STMT column", 0);
4429     return TCL_ERROR;
4430   }
4431 
4432   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4433 
4434   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
4435   return TCL_OK;
4436 }
4437 
4438 /*
4439 ** Usage: sqlite3_column_text STMT column
4440 **
4441 ** Usage: sqlite3_column_decltype STMT column
4442 **
4443 ** Usage: sqlite3_column_name STMT column
4444 */
4445 static int test_stmt_utf8(
4446   void * clientData,        /* Pointer to SQLite API function to be invoke */
4447   Tcl_Interp *interp,
4448   int objc,
4449   Tcl_Obj *CONST objv[]
4450 ){
4451   sqlite3_stmt *pStmt;
4452   int col;
4453   const char *(*xFunc)(sqlite3_stmt*, int);
4454   const char *zRet;
4455 
4456   xFunc = (const char *(*)(sqlite3_stmt*, int))clientData;
4457   if( objc!=3 ){
4458     Tcl_AppendResult(interp, "wrong # args: should be \"",
4459        Tcl_GetString(objv[0]), " STMT column", 0);
4460     return TCL_ERROR;
4461   }
4462 
4463   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4464   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4465   zRet = xFunc(pStmt, col);
4466   if( zRet ){
4467     Tcl_SetResult(interp, (char *)zRet, 0);
4468   }
4469   return TCL_OK;
4470 }
4471 
4472 static int test_global_recover(
4473   void * clientData,
4474   Tcl_Interp *interp,
4475   int objc,
4476   Tcl_Obj *CONST objv[]
4477 ){
4478 #ifndef SQLITE_OMIT_DEPRECATED
4479   int rc;
4480   if( objc!=1 ){
4481     Tcl_WrongNumArgs(interp, 1, objv, "");
4482     return TCL_ERROR;
4483   }
4484   rc = sqlite3_global_recover();
4485   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
4486 #endif
4487   return TCL_OK;
4488 }
4489 
4490 /*
4491 ** Usage: sqlite3_column_text STMT column
4492 **
4493 ** Usage: sqlite3_column_decltype STMT column
4494 **
4495 ** Usage: sqlite3_column_name STMT column
4496 */
4497 static int test_stmt_utf16(
4498   void * clientData,     /* Pointer to SQLite API function to be invoked */
4499   Tcl_Interp *interp,
4500   int objc,
4501   Tcl_Obj *CONST objv[]
4502 ){
4503 #ifndef SQLITE_OMIT_UTF16
4504   sqlite3_stmt *pStmt;
4505   int col;
4506   Tcl_Obj *pRet;
4507   const void *zName16;
4508   const void *(*xFunc)(sqlite3_stmt*, int);
4509 
4510   xFunc = (const void *(*)(sqlite3_stmt*, int))clientData;
4511   if( objc!=3 ){
4512     Tcl_AppendResult(interp, "wrong # args: should be \"",
4513        Tcl_GetString(objv[0]), " STMT column", 0);
4514     return TCL_ERROR;
4515   }
4516 
4517   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4518   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4519 
4520   zName16 = xFunc(pStmt, col);
4521   if( zName16 ){
4522     int n;
4523     const char *z = zName16;
4524     for(n=0; z[n] || z[n+1]; n+=2){}
4525     pRet = Tcl_NewByteArrayObj(zName16, n+2);
4526     Tcl_SetObjResult(interp, pRet);
4527   }
4528 #endif /* SQLITE_OMIT_UTF16 */
4529 
4530   return TCL_OK;
4531 }
4532 
4533 /*
4534 ** Usage: sqlite3_column_int STMT column
4535 **
4536 ** Usage: sqlite3_column_bytes STMT column
4537 **
4538 ** Usage: sqlite3_column_bytes16 STMT column
4539 **
4540 */
4541 static int test_stmt_int(
4542   void * clientData,    /* Pointer to SQLite API function to be invoked */
4543   Tcl_Interp *interp,
4544   int objc,
4545   Tcl_Obj *CONST objv[]
4546 ){
4547   sqlite3_stmt *pStmt;
4548   int col;
4549   int (*xFunc)(sqlite3_stmt*, int);
4550 
4551   xFunc = (int (*)(sqlite3_stmt*, int))clientData;
4552   if( objc!=3 ){
4553     Tcl_AppendResult(interp, "wrong # args: should be \"",
4554        Tcl_GetString(objv[0]), " STMT column", 0);
4555     return TCL_ERROR;
4556   }
4557 
4558   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4559   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4560 
4561   Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
4562   return TCL_OK;
4563 }
4564 
4565 /*
4566 ** Usage:  sqlite_set_magic  DB  MAGIC-NUMBER
4567 **
4568 ** Set the db->magic value.  This is used to test error recovery logic.
4569 */
4570 static int sqlite_set_magic(
4571   void * clientData,
4572   Tcl_Interp *interp,
4573   int argc,
4574   char **argv
4575 ){
4576   sqlite3 *db;
4577   if( argc!=3 ){
4578     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4579          " DB MAGIC", 0);
4580     return TCL_ERROR;
4581   }
4582   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4583   if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){
4584     db->magic = SQLITE_MAGIC_OPEN;
4585   }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){
4586     db->magic = SQLITE_MAGIC_CLOSED;
4587   }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){
4588     db->magic = SQLITE_MAGIC_BUSY;
4589   }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){
4590     db->magic = SQLITE_MAGIC_ERROR;
4591   }else if( Tcl_GetInt(interp, argv[2], (int*)&db->magic) ){
4592     return TCL_ERROR;
4593   }
4594   return TCL_OK;
4595 }
4596 
4597 /*
4598 ** Usage:  sqlite3_interrupt  DB
4599 **
4600 ** Trigger an interrupt on DB
4601 */
4602 static int test_interrupt(
4603   void * clientData,
4604   Tcl_Interp *interp,
4605   int argc,
4606   char **argv
4607 ){
4608   sqlite3 *db;
4609   if( argc!=2 ){
4610     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
4611     return TCL_ERROR;
4612   }
4613   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4614   sqlite3_interrupt(db);
4615   return TCL_OK;
4616 }
4617 
4618 static u8 *sqlite3_stack_baseline = 0;
4619 
4620 /*
4621 ** Fill the stack with a known bitpattern.
4622 */
4623 static void prepStack(void){
4624   int i;
4625   u32 bigBuf[65536];
4626   for(i=0; i<sizeof(bigBuf)/sizeof(bigBuf[0]); i++) bigBuf[i] = 0xdeadbeef;
4627   sqlite3_stack_baseline = (u8*)&bigBuf[65536];
4628 }
4629 
4630 /*
4631 ** Get the current stack depth.  Used for debugging only.
4632 */
4633 u64 sqlite3StackDepth(void){
4634   u8 x;
4635   return (u64)(sqlite3_stack_baseline - &x);
4636 }
4637 
4638 /*
4639 ** Usage:  sqlite3_stack_used DB SQL
4640 **
4641 ** Try to measure the amount of stack space used by a call to sqlite3_exec
4642 */
4643 static int test_stack_used(
4644   void * clientData,
4645   Tcl_Interp *interp,
4646   int argc,
4647   char **argv
4648 ){
4649   sqlite3 *db;
4650   int i;
4651   if( argc!=3 ){
4652     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4653         " DB SQL", 0);
4654     return TCL_ERROR;
4655   }
4656   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4657   prepStack();
4658   (void)sqlite3_exec(db, argv[2], 0, 0, 0);
4659   for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){}
4660   Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4));
4661   return TCL_OK;
4662 }
4663 
4664 /*
4665 ** Usage: sqlite_delete_function DB function-name
4666 **
4667 ** Delete the user function 'function-name' from database handle DB. It
4668 ** is assumed that the user function was created as UTF8, any number of
4669 ** arguments (the way the TCL interface does it).
4670 */
4671 static int delete_function(
4672   void * clientData,
4673   Tcl_Interp *interp,
4674   int argc,
4675   char **argv
4676 ){
4677   int rc;
4678   sqlite3 *db;
4679   if( argc!=3 ){
4680     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4681         " DB function-name", 0);
4682     return TCL_ERROR;
4683   }
4684   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4685   rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
4686   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
4687   return TCL_OK;
4688 }
4689 
4690 /*
4691 ** Usage: sqlite_delete_collation DB collation-name
4692 **
4693 ** Delete the collation sequence 'collation-name' from database handle
4694 ** DB. It is assumed that the collation sequence was created as UTF8 (the
4695 ** way the TCL interface does it).
4696 */
4697 static int delete_collation(
4698   void * clientData,
4699   Tcl_Interp *interp,
4700   int argc,
4701   char **argv
4702 ){
4703   int rc;
4704   sqlite3 *db;
4705   if( argc!=3 ){
4706     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4707         " DB function-name", 0);
4708     return TCL_ERROR;
4709   }
4710   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4711   rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
4712   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
4713   return TCL_OK;
4714 }
4715 
4716 /*
4717 ** Usage: sqlite3_get_autocommit DB
4718 **
4719 ** Return true if the database DB is currently in auto-commit mode.
4720 ** Return false if not.
4721 */
4722 static int get_autocommit(
4723   void * clientData,
4724   Tcl_Interp *interp,
4725   int argc,
4726   char **argv
4727 ){
4728   char zBuf[30];
4729   sqlite3 *db;
4730   if( argc!=2 ){
4731     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4732         " DB", 0);
4733     return TCL_ERROR;
4734   }
4735   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4736   sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", sqlite3_get_autocommit(db));
4737   Tcl_AppendResult(interp, zBuf, 0);
4738   return TCL_OK;
4739 }
4740 
4741 /*
4742 ** Usage: sqlite3_busy_timeout DB MS
4743 **
4744 ** Set the busy timeout.  This is more easily done using the timeout
4745 ** method of the TCL interface.  But we need a way to test the case
4746 ** where it returns SQLITE_MISUSE.
4747 */
4748 static int test_busy_timeout(
4749   void * clientData,
4750   Tcl_Interp *interp,
4751   int argc,
4752   char **argv
4753 ){
4754   int rc, ms;
4755   sqlite3 *db;
4756   if( argc!=3 ){
4757     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4758         " DB", 0);
4759     return TCL_ERROR;
4760   }
4761   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4762   if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
4763   rc = sqlite3_busy_timeout(db, ms);
4764   Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
4765   return TCL_OK;
4766 }
4767 
4768 /*
4769 ** Usage:  tcl_variable_type VARIABLENAME
4770 **
4771 ** Return the name of the internal representation for the
4772 ** value of the given variable.
4773 */
4774 static int tcl_variable_type(
4775   void * clientData,
4776   Tcl_Interp *interp,
4777   int objc,
4778   Tcl_Obj *CONST objv[]
4779 ){
4780   Tcl_Obj *pVar;
4781   if( objc!=2 ){
4782     Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
4783     return TCL_ERROR;
4784   }
4785   pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
4786   if( pVar==0 ) return TCL_ERROR;
4787   if( pVar->typePtr ){
4788     Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
4789   }
4790   return TCL_OK;
4791 }
4792 
4793 /*
4794 ** Usage:  sqlite3_release_memory ?N?
4795 **
4796 ** Attempt to release memory currently held but not actually required.
4797 ** The integer N is the number of bytes we are trying to release.  The
4798 ** return value is the amount of memory actually released.
4799 */
4800 static int test_release_memory(
4801   void * clientData,
4802   Tcl_Interp *interp,
4803   int objc,
4804   Tcl_Obj *CONST objv[]
4805 ){
4806 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
4807   int N;
4808   int amt;
4809   if( objc!=1 && objc!=2 ){
4810     Tcl_WrongNumArgs(interp, 1, objv, "?N?");
4811     return TCL_ERROR;
4812   }
4813   if( objc==2 ){
4814     if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4815   }else{
4816     N = -1;
4817   }
4818   amt = sqlite3_release_memory(N);
4819   Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
4820 #endif
4821   return TCL_OK;
4822 }
4823 
4824 
4825 /*
4826 ** Usage:  sqlite3_db_release_memory DB
4827 **
4828 ** Attempt to release memory currently held by database DB.  Return the
4829 ** result code (which in the current implementation is always zero).
4830 */
4831 static int test_db_release_memory(
4832   void * clientData,
4833   Tcl_Interp *interp,
4834   int objc,
4835   Tcl_Obj *CONST objv[]
4836 ){
4837   sqlite3 *db;
4838   int rc;
4839   if( objc!=2 ){
4840     Tcl_WrongNumArgs(interp, 1, objv, "DB");
4841     return TCL_ERROR;
4842   }
4843   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4844   rc = sqlite3_db_release_memory(db);
4845   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
4846   return TCL_OK;
4847 }
4848 
4849 /*
4850 ** Usage:  sqlite3_db_cacheflush DB
4851 **
4852 ** Attempt to flush any dirty pages to disk.
4853 */
4854 static int test_db_cacheflush(
4855   void * clientData,
4856   Tcl_Interp *interp,
4857   int objc,
4858   Tcl_Obj *CONST objv[]
4859 ){
4860   sqlite3 *db;
4861   int rc;
4862   if( objc!=2 ){
4863     Tcl_WrongNumArgs(interp, 1, objv, "DB");
4864     return TCL_ERROR;
4865   }
4866   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4867   rc = sqlite3_db_cacheflush(db);
4868   if( rc ){
4869     Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
4870     return TCL_ERROR;
4871   }
4872 
4873   Tcl_ResetResult(interp);
4874   return TCL_OK;
4875 }
4876 
4877 /*
4878 ** Usage:  sqlite3_system_errno DB
4879 **
4880 ** Return the low-level system errno value.
4881 */
4882 static int test_system_errno(
4883   void * clientData,
4884   Tcl_Interp *interp,
4885   int objc,
4886   Tcl_Obj *CONST objv[]
4887 ){
4888   sqlite3 *db;
4889   int iErrno;
4890   if( objc!=2 ){
4891     Tcl_WrongNumArgs(interp, 1, objv, "DB");
4892     return TCL_ERROR;
4893   }
4894   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4895   iErrno = sqlite3_system_errno(db);
4896   Tcl_SetObjResult(interp, Tcl_NewIntObj(iErrno));
4897   return TCL_OK;
4898 }
4899 
4900 /*
4901 ** Usage:  sqlite3_db_filename DB DBNAME
4902 **
4903 ** Return the name of a file associated with a database.
4904 */
4905 static int test_db_filename(
4906   void * clientData,
4907   Tcl_Interp *interp,
4908   int objc,
4909   Tcl_Obj *CONST objv[]
4910 ){
4911   sqlite3 *db;
4912   const char *zDbName;
4913   if( objc!=3 ){
4914     Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
4915     return TCL_ERROR;
4916   }
4917   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4918   zDbName = Tcl_GetString(objv[2]);
4919   Tcl_AppendResult(interp, sqlite3_db_filename(db, zDbName), (void*)0);
4920   return TCL_OK;
4921 }
4922 
4923 /*
4924 ** Usage:  sqlite3_db_readonly DB DBNAME
4925 **
4926 ** Return 1 or 0 if DBNAME is readonly or not.  Return -1 if DBNAME does
4927 ** not exist.
4928 */
4929 static int test_db_readonly(
4930   void * clientData,
4931   Tcl_Interp *interp,
4932   int objc,
4933   Tcl_Obj *CONST objv[]
4934 ){
4935   sqlite3 *db;
4936   const char *zDbName;
4937   if( objc!=3 ){
4938     Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
4939     return TCL_ERROR;
4940   }
4941   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4942   zDbName = Tcl_GetString(objv[2]);
4943   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_db_readonly(db, zDbName)));
4944   return TCL_OK;
4945 }
4946 
4947 /*
4948 ** Usage:  sqlite3_soft_heap_limit ?N?
4949 **
4950 ** Query or set the soft heap limit for the current thread.  The
4951 ** limit is only changed if the N is present.  The previous limit
4952 ** is returned.
4953 */
4954 static int test_soft_heap_limit(
4955   void * clientData,
4956   Tcl_Interp *interp,
4957   int objc,
4958   Tcl_Obj *CONST objv[]
4959 ){
4960   sqlite3_int64 amt;
4961   Tcl_WideInt N = -1;
4962   if( objc!=1 && objc!=2 ){
4963     Tcl_WrongNumArgs(interp, 1, objv, "?N?");
4964     return TCL_ERROR;
4965   }
4966   if( objc==2 ){
4967     if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4968   }
4969   amt = sqlite3_soft_heap_limit64(N);
4970   Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt));
4971   return TCL_OK;
4972 }
4973 
4974 /*
4975 ** Usage:   sqlite3_thread_cleanup
4976 **
4977 ** Call the sqlite3_thread_cleanup API.
4978 */
4979 static int test_thread_cleanup(
4980   void * clientData,
4981   Tcl_Interp *interp,
4982   int objc,
4983   Tcl_Obj *CONST objv[]
4984 ){
4985 #ifndef SQLITE_OMIT_DEPRECATED
4986   sqlite3_thread_cleanup();
4987 #endif
4988   return TCL_OK;
4989 }
4990 
4991 /*
4992 ** Usage:   sqlite3_pager_refcounts  DB
4993 **
4994 ** Return a list of numbers which are the PagerRefcount for all
4995 ** pagers on each database connection.
4996 */
4997 static int test_pager_refcounts(
4998   void * clientData,
4999   Tcl_Interp *interp,
5000   int objc,
5001   Tcl_Obj *CONST objv[]
5002 ){
5003   sqlite3 *db;
5004   int i;
5005   int v, *a;
5006   Tcl_Obj *pResult;
5007 
5008   if( objc!=2 ){
5009     Tcl_AppendResult(interp, "wrong # args: should be \"",
5010         Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
5011     return TCL_ERROR;
5012   }
5013   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5014   pResult = Tcl_NewObj();
5015   for(i=0; i<db->nDb; i++){
5016     if( db->aDb[i].pBt==0 ){
5017       v = -1;
5018     }else{
5019       sqlite3_mutex_enter(db->mutex);
5020       a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt));
5021       v = a[0];
5022       sqlite3_mutex_leave(db->mutex);
5023     }
5024     Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v));
5025   }
5026   Tcl_SetObjResult(interp, pResult);
5027   return TCL_OK;
5028 }
5029 
5030 
5031 /*
5032 ** tclcmd:   working_64bit_int
5033 **
5034 ** Some TCL builds (ex: cygwin) do not support 64-bit integers.  This
5035 ** leads to a number of test failures.  The present command checks the
5036 ** TCL build to see whether or not it supports 64-bit integers.  It
5037 ** returns TRUE if it does and FALSE if not.
5038 **
5039 ** This command is used to warn users that their TCL build is defective
5040 ** and that the errors they are seeing in the test scripts might be
5041 ** a result of their defective TCL rather than problems in SQLite.
5042 */
5043 static int working_64bit_int(
5044   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5045   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5046   int objc,              /* Number of arguments */
5047   Tcl_Obj *CONST objv[]  /* Command arguments */
5048 ){
5049   Tcl_Obj *pTestObj;
5050   int working = 0;
5051 
5052   pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890);
5053   working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0;
5054   Tcl_DecrRefCount(pTestObj);
5055   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working));
5056   return TCL_OK;
5057 }
5058 
5059 
5060 /*
5061 ** tclcmd:   vfs_unlink_test
5062 **
5063 ** This TCL command unregisters the primary VFS and then registers
5064 ** it back again.  This is used to test the ability to register a
5065 ** VFS when none are previously registered, and the ability to
5066 ** unregister the only available VFS.  Ticket #2738
5067 */
5068 static int vfs_unlink_test(
5069   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5070   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5071   int objc,              /* Number of arguments */
5072   Tcl_Obj *CONST objv[]  /* Command arguments */
5073 ){
5074   int i;
5075   sqlite3_vfs *pMain;
5076   sqlite3_vfs *apVfs[20];
5077   sqlite3_vfs one, two;
5078 
5079   sqlite3_vfs_unregister(0);   /* Unregister of NULL is harmless */
5080   one.zName = "__one";
5081   two.zName = "__two";
5082 
5083   /* Calling sqlite3_vfs_register with 2nd argument of 0 does not
5084   ** change the default VFS
5085   */
5086   pMain = sqlite3_vfs_find(0);
5087   sqlite3_vfs_register(&one, 0);
5088   assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
5089   sqlite3_vfs_register(&two, 0);
5090   assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
5091 
5092   /* We can find a VFS by its name */
5093   assert( sqlite3_vfs_find("__one")==&one );
5094   assert( sqlite3_vfs_find("__two")==&two );
5095 
5096   /* Calling sqlite_vfs_register with non-zero second parameter changes the
5097   ** default VFS, even if the 1st parameter is an existig VFS that is
5098   ** previously registered as the non-default.
5099   */
5100   sqlite3_vfs_register(&one, 1);
5101   assert( sqlite3_vfs_find("__one")==&one );
5102   assert( sqlite3_vfs_find("__two")==&two );
5103   assert( sqlite3_vfs_find(0)==&one );
5104   sqlite3_vfs_register(&two, 1);
5105   assert( sqlite3_vfs_find("__one")==&one );
5106   assert( sqlite3_vfs_find("__two")==&two );
5107   assert( sqlite3_vfs_find(0)==&two );
5108   if( pMain ){
5109     sqlite3_vfs_register(pMain, 1);
5110     assert( sqlite3_vfs_find("__one")==&one );
5111     assert( sqlite3_vfs_find("__two")==&two );
5112     assert( sqlite3_vfs_find(0)==pMain );
5113   }
5114 
5115   /* Unlink the default VFS.  Repeat until there are no more VFSes
5116   ** registered.
5117   */
5118   for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){
5119     apVfs[i] = sqlite3_vfs_find(0);
5120     if( apVfs[i] ){
5121       assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
5122       sqlite3_vfs_unregister(apVfs[i]);
5123       assert( 0==sqlite3_vfs_find(apVfs[i]->zName) );
5124     }
5125   }
5126   assert( 0==sqlite3_vfs_find(0) );
5127 
5128   /* Register the main VFS as non-default (will be made default, since
5129   ** it'll be the only one in existence).
5130   */
5131   sqlite3_vfs_register(pMain, 0);
5132   assert( sqlite3_vfs_find(0)==pMain );
5133 
5134   /* Un-register the main VFS again to restore an empty VFS list */
5135   sqlite3_vfs_unregister(pMain);
5136   assert( 0==sqlite3_vfs_find(0) );
5137 
5138   /* Relink all VFSes in reverse order. */
5139   for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){
5140     if( apVfs[i] ){
5141       sqlite3_vfs_register(apVfs[i], 1);
5142       assert( apVfs[i]==sqlite3_vfs_find(0) );
5143       assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
5144     }
5145   }
5146 
5147   /* Unregister out sample VFSes. */
5148   sqlite3_vfs_unregister(&one);
5149   sqlite3_vfs_unregister(&two);
5150 
5151   /* Unregistering a VFS that is not currently registered is harmless */
5152   sqlite3_vfs_unregister(&one);
5153   sqlite3_vfs_unregister(&two);
5154   assert( sqlite3_vfs_find("__one")==0 );
5155   assert( sqlite3_vfs_find("__two")==0 );
5156 
5157   /* We should be left with the original default VFS back as the
5158   ** original */
5159   assert( sqlite3_vfs_find(0)==pMain );
5160 
5161   return TCL_OK;
5162 }
5163 
5164 /*
5165 ** tclcmd:   vfs_initfail_test
5166 **
5167 ** This TCL command attempts to vfs_find and vfs_register when the
5168 ** sqlite3_initialize() interface is failing.  All calls should fail.
5169 */
5170 static int vfs_initfail_test(
5171   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5172   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5173   int objc,              /* Number of arguments */
5174   Tcl_Obj *CONST objv[]  /* Command arguments */
5175 ){
5176   sqlite3_vfs one;
5177   one.zName = "__one";
5178 
5179   if( sqlite3_vfs_find(0) ) return TCL_ERROR;
5180   sqlite3_vfs_register(&one, 0);
5181   if( sqlite3_vfs_find(0) ) return TCL_ERROR;
5182   sqlite3_vfs_register(&one, 1);
5183   if( sqlite3_vfs_find(0) ) return TCL_ERROR;
5184   return TCL_OK;
5185 }
5186 
5187 /*
5188 ** Saved VFSes
5189 */
5190 static sqlite3_vfs *apVfs[20];
5191 static int nVfs = 0;
5192 
5193 /*
5194 ** tclcmd:   vfs_unregister_all
5195 **
5196 ** Unregister all VFSes.
5197 */
5198 static int vfs_unregister_all(
5199   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5200   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5201   int objc,              /* Number of arguments */
5202   Tcl_Obj *CONST objv[]  /* Command arguments */
5203 ){
5204   int i;
5205   for(i=0; i<ArraySize(apVfs); i++){
5206     apVfs[i] = sqlite3_vfs_find(0);
5207     if( apVfs[i]==0 ) break;
5208     sqlite3_vfs_unregister(apVfs[i]);
5209   }
5210   nVfs = i;
5211   return TCL_OK;
5212 }
5213 /*
5214 ** tclcmd:   vfs_reregister_all
5215 **
5216 ** Restore all VFSes that were removed using vfs_unregister_all
5217 */
5218 static int vfs_reregister_all(
5219   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5220   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5221   int objc,              /* Number of arguments */
5222   Tcl_Obj *CONST objv[]  /* Command arguments */
5223 ){
5224   int i;
5225   for(i=0; i<nVfs; i++){
5226     sqlite3_vfs_register(apVfs[i], i==0);
5227   }
5228   return TCL_OK;
5229 }
5230 
5231 
5232 /*
5233 ** tclcmd:   file_control_test DB
5234 **
5235 ** This TCL command runs the sqlite3_file_control interface and
5236 ** verifies correct operation of the same.
5237 */
5238 static int file_control_test(
5239   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5240   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5241   int objc,              /* Number of arguments */
5242   Tcl_Obj *CONST objv[]  /* Command arguments */
5243 ){
5244   int iArg = 0;
5245   sqlite3 *db;
5246   int rc;
5247 
5248   if( objc!=2 ){
5249     Tcl_AppendResult(interp, "wrong # args: should be \"",
5250         Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
5251     return TCL_ERROR;
5252   }
5253   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5254   rc = sqlite3_file_control(db, 0, 0, &iArg);
5255   assert( rc==SQLITE_NOTFOUND );
5256   rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg);
5257   assert( rc==SQLITE_ERROR );
5258   rc = sqlite3_file_control(db, "main", -1, &iArg);
5259   assert( rc==SQLITE_NOTFOUND );
5260   rc = sqlite3_file_control(db, "temp", -1, &iArg);
5261   assert( rc==SQLITE_NOTFOUND || rc==SQLITE_ERROR );
5262 
5263   return TCL_OK;
5264 }
5265 
5266 
5267 /*
5268 ** tclcmd:   file_control_lasterrno_test DB
5269 **
5270 ** This TCL command runs the sqlite3_file_control interface and
5271 ** verifies correct operation of the SQLITE_LAST_ERRNO verb.
5272 */
5273 static int file_control_lasterrno_test(
5274   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5275   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5276   int objc,              /* Number of arguments */
5277   Tcl_Obj *CONST objv[]  /* Command arguments */
5278 ){
5279   int iArg = 0;
5280   sqlite3 *db;
5281   int rc;
5282 
5283   if( objc!=2 ){
5284     Tcl_AppendResult(interp, "wrong # args: should be \"",
5285         Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
5286     return TCL_ERROR;
5287   }
5288   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5289     return TCL_ERROR;
5290   }
5291   rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg);
5292   if( rc ){
5293     Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5294     return TCL_ERROR;
5295   }
5296   if( iArg!=0 ) {
5297     Tcl_AppendResult(interp, "Unexpected non-zero errno: ",
5298                      Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0);
5299     return TCL_ERROR;
5300   }
5301   return TCL_OK;
5302 }
5303 
5304 /*
5305 ** tclcmd:   file_control_chunksize_test DB DBNAME SIZE
5306 **
5307 ** This TCL command runs the sqlite3_file_control interface and
5308 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
5309 ** SQLITE_SET_LOCKPROXYFILE verbs.
5310 */
5311 static int file_control_chunksize_test(
5312   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5313   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5314   int objc,              /* Number of arguments */
5315   Tcl_Obj *CONST objv[]  /* Command arguments */
5316 ){
5317   int nSize;                      /* New chunk size */
5318   char *zDb;                      /* Db name ("main", "temp" etc.) */
5319   sqlite3 *db;                    /* Database handle */
5320   int rc;                         /* file_control() return code */
5321 
5322   if( objc!=4 ){
5323     Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE");
5324     return TCL_ERROR;
5325   }
5326   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
5327    || Tcl_GetIntFromObj(interp, objv[3], &nSize)
5328   ){
5329    return TCL_ERROR;
5330   }
5331   zDb = Tcl_GetString(objv[2]);
5332   if( zDb[0]=='\0' ) zDb = NULL;
5333 
5334   rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_CHUNK_SIZE, (void *)&nSize);
5335   if( rc ){
5336     Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
5337     return TCL_ERROR;
5338   }
5339   return TCL_OK;
5340 }
5341 
5342 /*
5343 ** tclcmd:   file_control_sizehint_test DB DBNAME SIZE
5344 **
5345 ** This TCL command runs the sqlite3_file_control interface
5346 ** with SQLITE_FCNTL_SIZE_HINT
5347 */
5348 static int file_control_sizehint_test(
5349   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5350   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5351   int objc,              /* Number of arguments */
5352   Tcl_Obj *CONST objv[]  /* Command arguments */
5353 ){
5354   Tcl_WideInt nSize;              /* Hinted size */
5355   char *zDb;                      /* Db name ("main", "temp" etc.) */
5356   sqlite3 *db;                    /* Database handle */
5357   int rc;                         /* file_control() return code */
5358 
5359   if( objc!=4 ){
5360     Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE");
5361     return TCL_ERROR;
5362   }
5363   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
5364    || Tcl_GetWideIntFromObj(interp, objv[3], &nSize)
5365   ){
5366    return TCL_ERROR;
5367   }
5368   zDb = Tcl_GetString(objv[2]);
5369   if( zDb[0]=='\0' ) zDb = NULL;
5370 
5371   rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_SIZE_HINT, (void *)&nSize);
5372   if( rc ){
5373     Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
5374     return TCL_ERROR;
5375   }
5376   return TCL_OK;
5377 }
5378 
5379 /*
5380 ** tclcmd:   file_control_lockproxy_test DB PWD
5381 **
5382 ** This TCL command runs the sqlite3_file_control interface and
5383 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
5384 ** SQLITE_SET_LOCKPROXYFILE verbs.
5385 */
5386 static int file_control_lockproxy_test(
5387   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5388   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5389   int objc,              /* Number of arguments */
5390   Tcl_Obj *CONST objv[]  /* Command arguments */
5391 ){
5392   sqlite3 *db;
5393 
5394   if( objc!=3 ){
5395     Tcl_AppendResult(interp, "wrong # args: should be \"",
5396                      Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0);
5397     return TCL_ERROR;
5398   }
5399   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5400    return TCL_ERROR;
5401   }
5402 
5403 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
5404 #  if defined(__APPLE__)
5405 #    define SQLITE_ENABLE_LOCKING_STYLE 1
5406 #  else
5407 #    define SQLITE_ENABLE_LOCKING_STYLE 0
5408 #  endif
5409 #endif
5410 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
5411   {
5412     char *testPath;
5413     int rc;
5414     int nPwd;
5415     const char *zPwd;
5416     char proxyPath[400];
5417 
5418     zPwd = Tcl_GetStringFromObj(objv[2], &nPwd);
5419     if( sizeof(proxyPath)<nPwd+20 ){
5420       Tcl_AppendResult(interp, "PWD too big", (void*)0);
5421       return TCL_ERROR;
5422     }
5423     sqlite3_snprintf(sizeof(proxyPath), proxyPath, "%s/test.proxy", zPwd);
5424     rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
5425     if( rc ){
5426       Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5427       return TCL_ERROR;
5428     }
5429     rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath);
5430     if( strncmp(proxyPath,testPath,11) ){
5431       Tcl_AppendResult(interp, "Lock proxy file did not match the "
5432                                "previously assigned value", 0);
5433       return TCL_ERROR;
5434     }
5435     if( rc ){
5436       Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5437       return TCL_ERROR;
5438     }
5439     rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
5440     if( rc ){
5441       Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5442       return TCL_ERROR;
5443     }
5444   }
5445 #endif
5446   return TCL_OK;
5447 }
5448 
5449 #if SQLITE_OS_WIN
5450 /*
5451 ** tclcmd:   file_control_win32_av_retry DB  NRETRY  DELAY
5452 **
5453 ** This TCL command runs the sqlite3_file_control interface with
5454 ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode.
5455 */
5456 static int file_control_win32_av_retry(
5457   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5458   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5459   int objc,              /* Number of arguments */
5460   Tcl_Obj *CONST objv[]  /* Command arguments */
5461 ){
5462   sqlite3 *db;
5463   int rc;
5464   int a[2];
5465   char z[100];
5466 
5467   if( objc!=4 ){
5468     Tcl_AppendResult(interp, "wrong # args: should be \"",
5469         Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", 0);
5470     return TCL_ERROR;
5471   }
5472   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5473     return TCL_ERROR;
5474   }
5475   if( Tcl_GetIntFromObj(interp, objv[2], &a[0]) ) return TCL_ERROR;
5476   if( Tcl_GetIntFromObj(interp, objv[3], &a[1]) ) return TCL_ERROR;
5477   rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_AV_RETRY, (void*)a);
5478   sqlite3_snprintf(sizeof(z), z, "%d %d %d", rc, a[0], a[1]);
5479   Tcl_AppendResult(interp, z, (char*)0);
5480   return TCL_OK;
5481 }
5482 
5483 /*
5484 ** tclcmd:   file_control_win32_set_handle DB HANDLE
5485 **
5486 ** This TCL command runs the sqlite3_file_control interface with
5487 ** the SQLITE_FCNTL_WIN32_SET_HANDLE opcode.
5488 */
5489 static int file_control_win32_set_handle(
5490   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5491   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5492   int objc,              /* Number of arguments */
5493   Tcl_Obj *CONST objv[]  /* Command arguments */
5494 ){
5495   sqlite3 *db;
5496   int rc;
5497   HANDLE hFile = NULL;
5498   char z[100];
5499 
5500   if( objc!=3 ){
5501     Tcl_AppendResult(interp, "wrong # args: should be \"",
5502         Tcl_GetStringFromObj(objv[0], 0), " DB HANDLE", 0);
5503     return TCL_ERROR;
5504   }
5505   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5506     return TCL_ERROR;
5507   }
5508   if( getWin32Handle(interp, Tcl_GetString(objv[2]), &hFile) ){
5509     return TCL_ERROR;
5510   }
5511   rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_SET_HANDLE,
5512                             (void*)&hFile);
5513   sqlite3_snprintf(sizeof(z), z, "%d %p", rc, (void*)hFile);
5514   Tcl_AppendResult(interp, z, (char*)0);
5515   return TCL_OK;
5516 }
5517 #endif
5518 
5519 /*
5520 ** tclcmd:   file_control_persist_wal DB PERSIST-FLAG
5521 **
5522 ** This TCL command runs the sqlite3_file_control interface with
5523 ** the SQLITE_FCNTL_PERSIST_WAL opcode.
5524 */
5525 static int file_control_persist_wal(
5526   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5527   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5528   int objc,              /* Number of arguments */
5529   Tcl_Obj *CONST objv[]  /* Command arguments */
5530 ){
5531   sqlite3 *db;
5532   int rc;
5533   int bPersist;
5534   char z[100];
5535 
5536   if( objc!=3 ){
5537     Tcl_AppendResult(interp, "wrong # args: should be \"",
5538         Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0);
5539     return TCL_ERROR;
5540   }
5541   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5542     return TCL_ERROR;
5543   }
5544   if( Tcl_GetIntFromObj(interp, objv[2], &bPersist) ) return TCL_ERROR;
5545   rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_PERSIST_WAL, (void*)&bPersist);
5546   sqlite3_snprintf(sizeof(z), z, "%d %d", rc, bPersist);
5547   Tcl_AppendResult(interp, z, (char*)0);
5548   return TCL_OK;
5549 }
5550 
5551 /*
5552 ** tclcmd:   file_control_powersafe_overwrite DB PSOW-FLAG
5553 **
5554 ** This TCL command runs the sqlite3_file_control interface with
5555 ** the SQLITE_FCNTL_POWERSAFE_OVERWRITE opcode.
5556 */
5557 static int file_control_powersafe_overwrite(
5558   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5559   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5560   int objc,              /* Number of arguments */
5561   Tcl_Obj *CONST objv[]  /* Command arguments */
5562 ){
5563   sqlite3 *db;
5564   int rc;
5565   int b;
5566   char z[100];
5567 
5568   if( objc!=3 ){
5569     Tcl_AppendResult(interp, "wrong # args: should be \"",
5570         Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0);
5571     return TCL_ERROR;
5572   }
5573   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5574     return TCL_ERROR;
5575   }
5576   if( Tcl_GetIntFromObj(interp, objv[2], &b) ) return TCL_ERROR;
5577   rc = sqlite3_file_control(db,NULL,SQLITE_FCNTL_POWERSAFE_OVERWRITE,(void*)&b);
5578   sqlite3_snprintf(sizeof(z), z, "%d %d", rc, b);
5579   Tcl_AppendResult(interp, z, (char*)0);
5580   return TCL_OK;
5581 }
5582 
5583 
5584 /*
5585 ** tclcmd:   file_control_vfsname DB ?AUXDB?
5586 **
5587 ** Return a string that describes the stack of VFSes.
5588 */
5589 static int file_control_vfsname(
5590   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5591   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5592   int objc,              /* Number of arguments */
5593   Tcl_Obj *CONST objv[]  /* Command arguments */
5594 ){
5595   sqlite3 *db;
5596   const char *zDbName = "main";
5597   char *zVfsName = 0;
5598 
5599   if( objc!=2 && objc!=3 ){
5600     Tcl_AppendResult(interp, "wrong # args: should be \"",
5601         Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
5602     return TCL_ERROR;
5603   }
5604   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5605     return TCL_ERROR;
5606   }
5607   if( objc==3 ){
5608     zDbName = Tcl_GetString(objv[2]);
5609   }
5610   sqlite3_file_control(db, zDbName, SQLITE_FCNTL_VFSNAME,(void*)&zVfsName);
5611   Tcl_AppendResult(interp, zVfsName, (char*)0);
5612   sqlite3_free(zVfsName);
5613   return TCL_OK;
5614 }
5615 
5616 /*
5617 ** tclcmd:   file_control_tempfilename DB ?AUXDB?
5618 **
5619 ** Return a string that is a temporary filename
5620 */
5621 static int file_control_tempfilename(
5622   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5623   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5624   int objc,              /* Number of arguments */
5625   Tcl_Obj *CONST objv[]  /* Command arguments */
5626 ){
5627   sqlite3 *db;
5628   const char *zDbName = "main";
5629   char *zTName = 0;
5630 
5631   if( objc!=2 && objc!=3 ){
5632     Tcl_AppendResult(interp, "wrong # args: should be \"",
5633         Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
5634     return TCL_ERROR;
5635   }
5636   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5637     return TCL_ERROR;
5638   }
5639   if( objc==3 ){
5640     zDbName = Tcl_GetString(objv[2]);
5641   }
5642   sqlite3_file_control(db, zDbName, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTName);
5643   Tcl_AppendResult(interp, zTName, (char*)0);
5644   sqlite3_free(zTName);
5645   return TCL_OK;
5646 }
5647 
5648 
5649 /*
5650 ** tclcmd:   sqlite3_vfs_list
5651 **
5652 **   Return a tcl list containing the names of all registered vfs's.
5653 */
5654 static int vfs_list(
5655   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5656   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5657   int objc,              /* Number of arguments */
5658   Tcl_Obj *CONST objv[]  /* Command arguments */
5659 ){
5660   sqlite3_vfs *pVfs;
5661   Tcl_Obj *pRet = Tcl_NewObj();
5662   if( objc!=1 ){
5663     Tcl_WrongNumArgs(interp, 1, objv, "");
5664     return TCL_ERROR;
5665   }
5666   for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
5667     Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1));
5668   }
5669   Tcl_SetObjResult(interp, pRet);
5670   return TCL_OK;
5671 }
5672 
5673 /*
5674 ** tclcmd:   sqlite3_limit DB ID VALUE
5675 **
5676 ** This TCL command runs the sqlite3_limit interface and
5677 ** verifies correct operation of the same.
5678 */
5679 static int test_limit(
5680   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5681   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5682   int objc,              /* Number of arguments */
5683   Tcl_Obj *CONST objv[]  /* Command arguments */
5684 ){
5685   sqlite3 *db;
5686   int rc;
5687   static const struct {
5688      char *zName;
5689      int id;
5690   } aId[] = {
5691     { "SQLITE_LIMIT_LENGTH",              SQLITE_LIMIT_LENGTH               },
5692     { "SQLITE_LIMIT_SQL_LENGTH",          SQLITE_LIMIT_SQL_LENGTH           },
5693     { "SQLITE_LIMIT_COLUMN",              SQLITE_LIMIT_COLUMN               },
5694     { "SQLITE_LIMIT_EXPR_DEPTH",          SQLITE_LIMIT_EXPR_DEPTH           },
5695     { "SQLITE_LIMIT_COMPOUND_SELECT",     SQLITE_LIMIT_COMPOUND_SELECT      },
5696     { "SQLITE_LIMIT_VDBE_OP",             SQLITE_LIMIT_VDBE_OP              },
5697     { "SQLITE_LIMIT_FUNCTION_ARG",        SQLITE_LIMIT_FUNCTION_ARG         },
5698     { "SQLITE_LIMIT_ATTACHED",            SQLITE_LIMIT_ATTACHED             },
5699     { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH  },
5700     { "SQLITE_LIMIT_VARIABLE_NUMBER",     SQLITE_LIMIT_VARIABLE_NUMBER      },
5701     { "SQLITE_LIMIT_TRIGGER_DEPTH",       SQLITE_LIMIT_TRIGGER_DEPTH        },
5702     { "SQLITE_LIMIT_WORKER_THREADS",      SQLITE_LIMIT_WORKER_THREADS       },
5703 
5704     /* Out of range test cases */
5705     { "SQLITE_LIMIT_TOOSMALL",            -1,                               },
5706     { "SQLITE_LIMIT_TOOBIG",              SQLITE_LIMIT_WORKER_THREADS+1     },
5707   };
5708   int i, id = 0;
5709   int val;
5710   const char *zId;
5711 
5712   if( objc!=4 ){
5713     Tcl_AppendResult(interp, "wrong # args: should be \"",
5714         Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0);
5715     return TCL_ERROR;
5716   }
5717   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5718   zId = Tcl_GetString(objv[2]);
5719   for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){
5720     if( strcmp(zId, aId[i].zName)==0 ){
5721       id = aId[i].id;
5722       break;
5723     }
5724   }
5725   if( i>=sizeof(aId)/sizeof(aId[0]) ){
5726     Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0);
5727     return TCL_ERROR;
5728   }
5729   if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
5730   rc = sqlite3_limit(db, id, val);
5731   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5732   return TCL_OK;
5733 }
5734 
5735 /*
5736 ** tclcmd:  save_prng_state
5737 **
5738 ** Save the state of the pseudo-random number generator.
5739 ** At the same time, verify that sqlite3_test_control works even when
5740 ** called with an out-of-range opcode.
5741 */
5742 static int save_prng_state(
5743   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5744   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5745   int objc,              /* Number of arguments */
5746   Tcl_Obj *CONST objv[]  /* Command arguments */
5747 ){
5748   int rc = sqlite3_test_control(9999);
5749   assert( rc==0 );
5750   rc = sqlite3_test_control(-1);
5751   assert( rc==0 );
5752   sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE);
5753   return TCL_OK;
5754 }
5755 /*
5756 ** tclcmd:  restore_prng_state
5757 */
5758 static int restore_prng_state(
5759   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5760   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5761   int objc,              /* Number of arguments */
5762   Tcl_Obj *CONST objv[]  /* Command arguments */
5763 ){
5764   sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE);
5765   return TCL_OK;
5766 }
5767 /*
5768 ** tclcmd:  reset_prng_state
5769 */
5770 static int reset_prng_state(
5771   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5772   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5773   int objc,              /* Number of arguments */
5774   Tcl_Obj *CONST objv[]  /* Command arguments */
5775 ){
5776   sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET);
5777   return TCL_OK;
5778 }
5779 
5780 /*
5781 ** tclcmd:  database_may_be_corrupt
5782 **
5783 ** Indicate that database files might be corrupt.  In other words, set the normal
5784 ** state of operation.
5785 */
5786 static int database_may_be_corrupt(
5787   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5788   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5789   int objc,              /* Number of arguments */
5790   Tcl_Obj *CONST objv[]  /* Command arguments */
5791 ){
5792   sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 0);
5793   return TCL_OK;
5794 }
5795 /*
5796 ** tclcmd:  database_never_corrupt
5797 **
5798 ** Indicate that database files are always well-formed.  This enables extra assert()
5799 ** statements that test conditions that are always true for well-formed databases.
5800 */
5801 static int database_never_corrupt(
5802   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5803   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5804   int objc,              /* Number of arguments */
5805   Tcl_Obj *CONST objv[]  /* Command arguments */
5806 ){
5807   sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 1);
5808   return TCL_OK;
5809 }
5810 
5811 /*
5812 ** tclcmd:  pcache_stats
5813 */
5814 static int test_pcache_stats(
5815   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5816   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5817   int objc,              /* Number of arguments */
5818   Tcl_Obj *CONST objv[]  /* Command arguments */
5819 ){
5820   int nMin;
5821   int nMax;
5822   int nCurrent;
5823   int nRecyclable;
5824   Tcl_Obj *pRet;
5825 
5826   sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable);
5827 
5828   pRet = Tcl_NewObj();
5829   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1));
5830   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent));
5831   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1));
5832   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax));
5833   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1));
5834   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin));
5835   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1));
5836   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable));
5837 
5838   Tcl_SetObjResult(interp, pRet);
5839 
5840   return TCL_OK;
5841 }
5842 
5843 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
5844 static void test_unlock_notify_cb(void **aArg, int nArg){
5845   int ii;
5846   for(ii=0; ii<nArg; ii++){
5847     Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL);
5848   }
5849 }
5850 #endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */
5851 
5852 /*
5853 ** tclcmd:  sqlite3_unlock_notify db
5854 */
5855 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
5856 static int test_unlock_notify(
5857   ClientData clientData, /* Unused */
5858   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5859   int objc,              /* Number of arguments */
5860   Tcl_Obj *CONST objv[]  /* Command arguments */
5861 ){
5862   sqlite3 *db;
5863   int rc;
5864 
5865   if( objc!=2 ){
5866     Tcl_WrongNumArgs(interp, 1, objv, "DB");
5867     return TCL_ERROR;
5868   }
5869 
5870   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5871     return TCL_ERROR;
5872   }
5873   rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp);
5874   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
5875   return TCL_OK;
5876 }
5877 #endif
5878 
5879 /*
5880 ** tclcmd:  sqlite3_wal_checkpoint db ?NAME?
5881 */
5882 static int test_wal_checkpoint(
5883   ClientData clientData, /* Unused */
5884   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5885   int objc,              /* Number of arguments */
5886   Tcl_Obj *CONST objv[]  /* Command arguments */
5887 ){
5888   char *zDb = 0;
5889   sqlite3 *db;
5890   int rc;
5891 
5892   if( objc!=3 && objc!=2 ){
5893     Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?");
5894     return TCL_ERROR;
5895   }
5896 
5897   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5898     return TCL_ERROR;
5899   }
5900   if( objc==3 ){
5901     zDb = Tcl_GetString(objv[2]);
5902   }
5903   rc = sqlite3_wal_checkpoint(db, zDb);
5904   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
5905   return TCL_OK;
5906 }
5907 
5908 /*
5909 ** tclcmd:  sqlite3_wal_checkpoint_v2 db MODE ?NAME?
5910 **
5911 ** This command calls the wal_checkpoint_v2() function with the specified
5912 ** mode argument (passive, full or restart). If present, the database name
5913 ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the
5914 ** NAME argument is not present, a NULL pointer is passed instead.
5915 **
5916 ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or
5917 ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set
5918 ** to the error message obtained from sqlite3_errmsg().
5919 **
5920 ** Otherwise, this command returns a list of three integers. The first integer
5921 ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers
5922 ** are the values returned via the output parameters by wal_checkpoint_v2() -
5923 ** the number of frames in the log and the number of frames in the log
5924 ** that have been checkpointed.
5925 */
5926 static int test_wal_checkpoint_v2(
5927   ClientData clientData, /* Unused */
5928   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5929   int objc,              /* Number of arguments */
5930   Tcl_Obj *CONST objv[]  /* Command arguments */
5931 ){
5932   char *zDb = 0;
5933   sqlite3 *db;
5934   int rc;
5935 
5936   int eMode;
5937   int nLog = -555;
5938   int nCkpt = -555;
5939   Tcl_Obj *pRet;
5940 
5941   const char * aMode[] = { "passive", "full", "restart", "truncate", 0 };
5942   assert( SQLITE_CHECKPOINT_PASSIVE==0 );
5943   assert( SQLITE_CHECKPOINT_FULL==1 );
5944   assert( SQLITE_CHECKPOINT_RESTART==2 );
5945   assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
5946 
5947   if( objc!=3 && objc!=4 ){
5948     Tcl_WrongNumArgs(interp, 1, objv, "DB MODE ?NAME?");
5949     return TCL_ERROR;
5950   }
5951 
5952   if( objc==4 ){
5953     zDb = Tcl_GetString(objv[3]);
5954   }
5955   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) || (
5956       TCL_OK!=Tcl_GetIntFromObj(0, objv[2], &eMode)
5957    && TCL_OK!=Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0, &eMode)
5958   )){
5959     return TCL_ERROR;
5960   }
5961 
5962   rc = sqlite3_wal_checkpoint_v2(db, zDb, eMode, &nLog, &nCkpt);
5963   if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
5964     const char *zErrCode = sqlite3ErrName(rc);
5965     Tcl_ResetResult(interp);
5966     Tcl_AppendResult(interp, zErrCode, " - ", (char *)sqlite3_errmsg(db), 0);
5967     return TCL_ERROR;
5968   }
5969 
5970   pRet = Tcl_NewObj();
5971   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc==SQLITE_BUSY?1:0));
5972   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nLog));
5973   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCkpt));
5974   Tcl_SetObjResult(interp, pRet);
5975 
5976   return TCL_OK;
5977 }
5978 
5979 /*
5980 ** tclcmd:  sqlite3_wal_autocheckpoint db VALUE
5981 */
5982 static int test_wal_autocheckpoint(
5983   ClientData clientData, /* Unused */
5984   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5985   int objc,              /* Number of arguments */
5986   Tcl_Obj *CONST objv[]  /* Command arguments */
5987 ){
5988   sqlite3 *db;
5989   int rc;
5990   int iVal;
5991 
5992 
5993   if( objc!=3 ){
5994     Tcl_WrongNumArgs(interp, 1, objv, "DB VALUE");
5995     return TCL_ERROR;
5996   }
5997 
5998   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
5999    || Tcl_GetIntFromObj(0, objv[2], &iVal)
6000   ){
6001     return TCL_ERROR;
6002   }
6003 
6004   rc = sqlite3_wal_autocheckpoint(db, iVal);
6005   Tcl_ResetResult(interp);
6006   if( rc!=SQLITE_OK ){
6007     const char *zErrCode = sqlite3ErrName(rc);
6008     Tcl_SetObjResult(interp, Tcl_NewStringObj(zErrCode, -1));
6009     return TCL_ERROR;
6010   }
6011 
6012   return TCL_OK;
6013 }
6014 
6015 
6016 /*
6017 ** tclcmd:  test_sqlite3_log ?SCRIPT?
6018 */
6019 static struct LogCallback {
6020   Tcl_Interp *pInterp;
6021   Tcl_Obj *pObj;
6022 } logcallback = {0, 0};
6023 static void xLogcallback(void *unused, int err, char *zMsg){
6024   Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj);
6025   Tcl_IncrRefCount(pNew);
6026   Tcl_ListObjAppendElement(
6027       0, pNew, Tcl_NewStringObj(sqlite3ErrName(err), -1)
6028   );
6029   Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zMsg, -1));
6030   Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT);
6031   Tcl_DecrRefCount(pNew);
6032 }
6033 static int test_sqlite3_log(
6034   ClientData clientData,
6035   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
6036   int objc,              /* Number of arguments */
6037   Tcl_Obj *CONST objv[]  /* Command arguments */
6038 ){
6039   if( objc>2 ){
6040     Tcl_WrongNumArgs(interp, 1, objv, "SCRIPT");
6041     return TCL_ERROR;
6042   }
6043   if( logcallback.pObj ){
6044     Tcl_DecrRefCount(logcallback.pObj);
6045     logcallback.pObj = 0;
6046     logcallback.pInterp = 0;
6047     sqlite3_config(SQLITE_CONFIG_LOG, (void*)0, (void*)0);
6048   }
6049   if( objc>1 ){
6050     logcallback.pObj = objv[1];
6051     Tcl_IncrRefCount(logcallback.pObj);
6052     logcallback.pInterp = interp;
6053     sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, (void*)0);
6054   }
6055   return TCL_OK;
6056 }
6057 
6058 /*
6059 **     tcl_objproc COMMANDNAME ARGS...
6060 **
6061 ** Run a TCL command using its objProc interface.  Throw an error if
6062 ** the command has no objProc interface.
6063 */
6064 static int runAsObjProc(
6065   void * clientData,
6066   Tcl_Interp *interp,
6067   int objc,
6068   Tcl_Obj *CONST objv[]
6069 ){
6070   Tcl_CmdInfo cmdInfo;
6071   if( objc<2 ){
6072     Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ...");
6073     return TCL_ERROR;
6074   }
6075   if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
6076     Tcl_AppendResult(interp, "command not found: ",
6077            Tcl_GetString(objv[1]), (char*)0);
6078     return TCL_ERROR;
6079   }
6080   if( cmdInfo.objProc==0 ){
6081     Tcl_AppendResult(interp, "command has no objProc: ",
6082            Tcl_GetString(objv[1]), (char*)0);
6083     return TCL_ERROR;
6084   }
6085   return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1);
6086 }
6087 
6088 #ifndef SQLITE_OMIT_EXPLAIN
6089 /*
6090 ** WARNING: The following function, printExplainQueryPlan() is an exact
6091 ** copy of example code from eqp.in (eqp.html). If this code is modified,
6092 ** then the documentation copy needs to be modified as well.
6093 */
6094 /*
6095 ** Argument pStmt is a prepared SQL statement. This function compiles
6096 ** an EXPLAIN QUERY PLAN command to report on the prepared statement,
6097 ** and prints the report to stdout using printf().
6098 */
6099 int printExplainQueryPlan(sqlite3_stmt *pStmt){
6100   const char *zSql;               /* Input SQL */
6101   char *zExplain;                 /* SQL with EXPLAIN QUERY PLAN prepended */
6102   sqlite3_stmt *pExplain;         /* Compiled EXPLAIN QUERY PLAN command */
6103   int rc;                         /* Return code from sqlite3_prepare_v2() */
6104 
6105   zSql = sqlite3_sql(pStmt);
6106   if( zSql==0 ) return SQLITE_ERROR;
6107 
6108   zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql);
6109   if( zExplain==0 ) return SQLITE_NOMEM;
6110 
6111   rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, 0);
6112   sqlite3_free(zExplain);
6113   if( rc!=SQLITE_OK ) return rc;
6114 
6115   while( SQLITE_ROW==sqlite3_step(pExplain) ){
6116     int iSelectid = sqlite3_column_int(pExplain, 0);
6117     int iOrder = sqlite3_column_int(pExplain, 1);
6118     int iFrom = sqlite3_column_int(pExplain, 2);
6119     const char *zDetail = (const char *)sqlite3_column_text(pExplain, 3);
6120 
6121     printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail);
6122   }
6123 
6124   return sqlite3_finalize(pExplain);
6125 }
6126 
6127 static int test_print_eqp(
6128   void * clientData,
6129   Tcl_Interp *interp,
6130   int objc,
6131   Tcl_Obj *CONST objv[]
6132 ){
6133   int rc;
6134   sqlite3_stmt *pStmt;
6135 
6136   if( objc!=2 ){
6137     Tcl_WrongNumArgs(interp, 1, objv, "STMT");
6138     return TCL_ERROR;
6139   }
6140   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
6141   rc = printExplainQueryPlan(pStmt);
6142   /* This is needed on Windows so that a test case using this
6143   ** function can open a read pipe and get the output of
6144   ** printExplainQueryPlan() immediately.
6145   */
6146   fflush(stdout);
6147   Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
6148   return TCL_OK;
6149 }
6150 #endif /* SQLITE_OMIT_EXPLAIN */
6151 
6152 /*
6153 ** sqlite3_test_control VERB ARGS...
6154 */
6155 static int test_test_control(
6156   void * clientData,
6157   Tcl_Interp *interp,
6158   int objc,
6159   Tcl_Obj *CONST objv[]
6160 ){
6161   struct Verb {
6162     const char *zName;
6163     int i;
6164   } aVerb[] = {
6165     { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT },
6166     { "SQLITE_TESTCTRL_SORTER_MMAP",     SQLITE_TESTCTRL_SORTER_MMAP     },
6167     { "SQLITE_TESTCTRL_IMPOSTER",        SQLITE_TESTCTRL_IMPOSTER        },
6168   };
6169   int iVerb;
6170   int iFlag;
6171   int rc;
6172 
6173   if( objc<2 ){
6174     Tcl_WrongNumArgs(interp, 1, objv, "VERB ARGS...");
6175     return TCL_ERROR;
6176   }
6177 
6178   rc = Tcl_GetIndexFromObjStruct(
6179       interp, objv[1], aVerb, sizeof(aVerb[0]), "VERB", 0, &iVerb
6180   );
6181   if( rc!=TCL_OK ) return rc;
6182 
6183   iFlag = aVerb[iVerb].i;
6184   switch( iFlag ){
6185     case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
6186       int val;
6187       if( objc!=3 ){
6188         Tcl_WrongNumArgs(interp, 2, objv, "ONOFF");
6189         return TCL_ERROR;
6190       }
6191       if( Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
6192       sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, val);
6193       break;
6194     }
6195 
6196     case SQLITE_TESTCTRL_SORTER_MMAP: {
6197       int val;
6198       sqlite3 *db;
6199       if( objc!=4 ){
6200         Tcl_WrongNumArgs(interp, 2, objv, "DB LIMIT");
6201         return TCL_ERROR;
6202       }
6203       if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
6204       if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
6205       sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, val);
6206       break;
6207     }
6208 
6209     case SQLITE_TESTCTRL_IMPOSTER: {
6210       int onOff, tnum;
6211       const char *zDbName;
6212       sqlite3 *db;
6213       if( objc!=6 ){
6214         Tcl_WrongNumArgs(interp, 2, objv, "DB dbName onOff tnum");
6215         return TCL_ERROR;
6216       }
6217       if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
6218       zDbName = Tcl_GetString(objv[3]);
6219       if( Tcl_GetIntFromObj(interp, objv[4], &onOff) ) return TCL_ERROR;
6220       if( Tcl_GetIntFromObj(interp, objv[5], &tnum) ) return TCL_ERROR;
6221       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, zDbName, onOff, tnum);
6222       break;
6223     }
6224   }
6225 
6226   Tcl_ResetResult(interp);
6227   return TCL_OK;
6228 }
6229 
6230 #if SQLITE_OS_UNIX
6231 #include <sys/time.h>
6232 #include <sys/resource.h>
6233 
6234 static int test_getrusage(
6235   void * clientData,
6236   Tcl_Interp *interp,
6237   int objc,
6238   Tcl_Obj *CONST objv[]
6239 ){
6240   char buf[1024];
6241   struct rusage r;
6242   memset(&r, 0, sizeof(r));
6243   getrusage(RUSAGE_SELF, &r);
6244 
6245   sqlite3_snprintf(sizeof(buf), buf,
6246     "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d",
6247     (int)r.ru_utime.tv_sec, (int)r.ru_utime.tv_usec,
6248     (int)r.ru_stime.tv_sec, (int)r.ru_stime.tv_usec,
6249     (int)r.ru_minflt, (int)r.ru_majflt
6250   );
6251   Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1));
6252   return TCL_OK;
6253 }
6254 #endif
6255 
6256 #if SQLITE_OS_WIN
6257 /*
6258 ** Information passed from the main thread into the windows file locker
6259 ** background thread.
6260 */
6261 struct win32FileLocker {
6262   char *evName;       /* Name of event to signal thread startup */
6263   HANDLE h;           /* Handle of the file to be locked */
6264   int delay1;         /* Delay before locking */
6265   int delay2;         /* Delay before unlocking */
6266   int ok;             /* Finished ok */
6267   int err;            /* True if an error occurs */
6268 };
6269 #endif
6270 
6271 
6272 #if SQLITE_OS_WIN
6273 #include <process.h>
6274 /*
6275 ** The background thread that does file locking.
6276 */
6277 static void win32_file_locker(void *pAppData){
6278   struct win32FileLocker *p = (struct win32FileLocker*)pAppData;
6279   if( p->evName ){
6280     HANDLE ev = OpenEvent(EVENT_MODIFY_STATE, FALSE, p->evName);
6281     if ( ev ){
6282       SetEvent(ev);
6283       CloseHandle(ev);
6284     }
6285   }
6286   if( p->delay1 ) Sleep(p->delay1);
6287   if( LockFile(p->h, 0, 0, 100000000, 0) ){
6288     Sleep(p->delay2);
6289     UnlockFile(p->h, 0, 0, 100000000, 0);
6290     p->ok = 1;
6291   }else{
6292     p->err = 1;
6293   }
6294   CloseHandle(p->h);
6295   p->h = 0;
6296   p->delay1 = 0;
6297   p->delay2 = 0;
6298 }
6299 #endif
6300 
6301 #if SQLITE_OS_WIN
6302 /*
6303 **      lock_win32_file FILENAME DELAY1 DELAY2
6304 **
6305 ** Get an exclusive manditory lock on file for DELAY2 milliseconds.
6306 ** Wait DELAY1 milliseconds before acquiring the lock.
6307 */
6308 static int win32_file_lock(
6309   void * clientData,
6310   Tcl_Interp *interp,
6311   int objc,
6312   Tcl_Obj *CONST objv[]
6313 ){
6314   static struct win32FileLocker x = { "win32_file_lock", 0, 0, 0, 0, 0 };
6315   const char *zFilename;
6316   char zBuf[200];
6317   int retry = 0;
6318   HANDLE ev;
6319   DWORD wResult;
6320 
6321   if( objc!=4 && objc!=1 ){
6322     Tcl_WrongNumArgs(interp, 1, objv, "FILENAME DELAY1 DELAY2");
6323     return TCL_ERROR;
6324   }
6325   if( objc==1 ){
6326     sqlite3_snprintf(sizeof(zBuf), zBuf, "%d %d %d %d %d",
6327                      x.ok, x.err, x.delay1, x.delay2, x.h);
6328     Tcl_AppendResult(interp, zBuf, (char*)0);
6329     return TCL_OK;
6330   }
6331   while( x.h && retry<30 ){
6332     retry++;
6333     Sleep(100);
6334   }
6335   if( x.h ){
6336     Tcl_AppendResult(interp, "busy", (char*)0);
6337     return TCL_ERROR;
6338   }
6339   if( Tcl_GetIntFromObj(interp, objv[2], &x.delay1) ) return TCL_ERROR;
6340   if( Tcl_GetIntFromObj(interp, objv[3], &x.delay2) ) return TCL_ERROR;
6341   zFilename = Tcl_GetString(objv[1]);
6342   x.h = CreateFile(zFilename, GENERIC_READ|GENERIC_WRITE,
6343               FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS,
6344               FILE_ATTRIBUTE_NORMAL, 0);
6345   if( !x.h ){
6346     Tcl_AppendResult(interp, "cannot open file: ", zFilename, (char*)0);
6347     return TCL_ERROR;
6348   }
6349   ev = CreateEvent(NULL, TRUE, FALSE, x.evName);
6350   if ( !ev ){
6351     Tcl_AppendResult(interp, "cannot create event: ", x.evName, (char*)0);
6352     return TCL_ERROR;
6353   }
6354   _beginthread(win32_file_locker, 0, (void*)&x);
6355   Sleep(0);
6356   if ( (wResult = WaitForSingleObject(ev, 10000))!=WAIT_OBJECT_0 ){
6357     sqlite3_snprintf(sizeof(zBuf), zBuf, "0x%x", wResult);
6358     Tcl_AppendResult(interp, "wait failed: ", zBuf, (char*)0);
6359     CloseHandle(ev);
6360     return TCL_ERROR;
6361   }
6362   CloseHandle(ev);
6363   return TCL_OK;
6364 }
6365 
6366 /*
6367 **      exists_win32_path PATH
6368 **
6369 ** Returns non-zero if the specified path exists, whose fully qualified name
6370 ** may exceed 260 characters if it is prefixed with "\\?\".
6371 */
6372 static int win32_exists_path(
6373   void *clientData,
6374   Tcl_Interp *interp,
6375   int objc,
6376   Tcl_Obj *CONST objv[]
6377 ){
6378   if( objc!=2 ){
6379     Tcl_WrongNumArgs(interp, 1, objv, "PATH");
6380     return TCL_ERROR;
6381   }
6382   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(
6383       GetFileAttributesW( Tcl_GetUnicode(objv[1]))!=INVALID_FILE_ATTRIBUTES ));
6384   return TCL_OK;
6385 }
6386 
6387 /*
6388 **      find_win32_file PATTERN
6389 **
6390 ** Returns a list of entries in a directory that match the specified pattern,
6391 ** whose fully qualified name may exceed 248 characters if it is prefixed with
6392 ** "\\?\".
6393 */
6394 static int win32_find_file(
6395   void *clientData,
6396   Tcl_Interp *interp,
6397   int objc,
6398   Tcl_Obj *CONST objv[]
6399 ){
6400   HANDLE hFindFile = INVALID_HANDLE_VALUE;
6401   WIN32_FIND_DATAW findData;
6402   Tcl_Obj *listObj;
6403   DWORD lastErrno;
6404   if( objc!=2 ){
6405     Tcl_WrongNumArgs(interp, 1, objv, "PATTERN");
6406     return TCL_ERROR;
6407   }
6408   hFindFile = FindFirstFileW(Tcl_GetUnicode(objv[1]), &findData);
6409   if( hFindFile==INVALID_HANDLE_VALUE ){
6410     Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
6411     return TCL_ERROR;
6412   }
6413   listObj = Tcl_NewObj();
6414   Tcl_IncrRefCount(listObj);
6415   do {
6416     Tcl_ListObjAppendElement(interp, listObj, Tcl_NewUnicodeObj(
6417         findData.cFileName, -1));
6418     Tcl_ListObjAppendElement(interp, listObj, Tcl_NewWideIntObj(
6419         findData.dwFileAttributes));
6420   } while( FindNextFileW(hFindFile, &findData) );
6421   lastErrno = GetLastError();
6422   if( lastErrno!=NO_ERROR && lastErrno!=ERROR_NO_MORE_FILES ){
6423     FindClose(hFindFile);
6424     Tcl_DecrRefCount(listObj);
6425     Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
6426     return TCL_ERROR;
6427   }
6428   FindClose(hFindFile);
6429   Tcl_SetObjResult(interp, listObj);
6430   return TCL_OK;
6431 }
6432 
6433 /*
6434 **      delete_win32_file FILENAME
6435 **
6436 ** Deletes the specified file, whose fully qualified name may exceed 260
6437 ** characters if it is prefixed with "\\?\".
6438 */
6439 static int win32_delete_file(
6440   void *clientData,
6441   Tcl_Interp *interp,
6442   int objc,
6443   Tcl_Obj *CONST objv[]
6444 ){
6445   if( objc!=2 ){
6446     Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
6447     return TCL_ERROR;
6448   }
6449   if( !DeleteFileW(Tcl_GetUnicode(objv[1])) ){
6450     Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
6451     return TCL_ERROR;
6452   }
6453   Tcl_ResetResult(interp);
6454   return TCL_OK;
6455 }
6456 
6457 /*
6458 **      make_win32_dir DIRECTORY
6459 **
6460 ** Creates the specified directory, whose fully qualified name may exceed 248
6461 ** characters if it is prefixed with "\\?\".
6462 */
6463 static int win32_mkdir(
6464   void *clientData,
6465   Tcl_Interp *interp,
6466   int objc,
6467   Tcl_Obj *CONST objv[]
6468 ){
6469   if( objc!=2 ){
6470     Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
6471     return TCL_ERROR;
6472   }
6473   if( !CreateDirectoryW(Tcl_GetUnicode(objv[1]), NULL) ){
6474     Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
6475     return TCL_ERROR;
6476   }
6477   Tcl_ResetResult(interp);
6478   return TCL_OK;
6479 }
6480 
6481 /*
6482 **      remove_win32_dir DIRECTORY
6483 **
6484 ** Removes the specified directory, whose fully qualified name may exceed 248
6485 ** characters if it is prefixed with "\\?\".
6486 */
6487 static int win32_rmdir(
6488   void *clientData,
6489   Tcl_Interp *interp,
6490   int objc,
6491   Tcl_Obj *CONST objv[]
6492 ){
6493   if( objc!=2 ){
6494     Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
6495     return TCL_ERROR;
6496   }
6497   if( !RemoveDirectoryW(Tcl_GetUnicode(objv[1])) ){
6498     Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
6499     return TCL_ERROR;
6500   }
6501   Tcl_ResetResult(interp);
6502   return TCL_OK;
6503 }
6504 #endif
6505 
6506 
6507 /*
6508 **      optimization_control DB OPT BOOLEAN
6509 **
6510 ** Enable or disable query optimizations using the sqlite3_test_control()
6511 ** interface.  Disable if BOOLEAN is false and enable if BOOLEAN is true.
6512 ** OPT is the name of the optimization to be disabled.
6513 */
6514 static int optimization_control(
6515   void * clientData,
6516   Tcl_Interp *interp,
6517   int objc,
6518   Tcl_Obj *CONST objv[]
6519 ){
6520   int i;
6521   sqlite3 *db;
6522   const char *zOpt;
6523   int onoff;
6524   int mask = 0;
6525   static const struct {
6526     const char *zOptName;
6527     int mask;
6528   } aOpt[] = {
6529     { "all",                 SQLITE_AllOpts        },
6530     { "none",                0                     },
6531     { "query-flattener",     SQLITE_QueryFlattener },
6532     { "column-cache",        SQLITE_ColumnCache    },
6533     { "groupby-order",       SQLITE_GroupByOrder   },
6534     { "factor-constants",    SQLITE_FactorOutConst },
6535     { "distinct-opt",        SQLITE_DistinctOpt    },
6536     { "cover-idx-scan",      SQLITE_CoverIdxScan   },
6537     { "order-by-idx-join",   SQLITE_OrderByIdxJoin },
6538     { "transitive",          SQLITE_Transitive     },
6539     { "subquery-coroutine",  SQLITE_SubqCoroutine  },
6540     { "omit-noop-join",      SQLITE_OmitNoopJoin   },
6541     { "stat3",               SQLITE_Stat34         },
6542     { "stat4",               SQLITE_Stat34         },
6543   };
6544 
6545   if( objc!=4 ){
6546     Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN");
6547     return TCL_ERROR;
6548   }
6549   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6550   if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR;
6551   zOpt = Tcl_GetString(objv[2]);
6552   for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
6553     if( strcmp(zOpt, aOpt[i].zOptName)==0 ){
6554       mask = aOpt[i].mask;
6555       break;
6556     }
6557   }
6558   if( onoff ) mask = ~mask;
6559   if( i>=sizeof(aOpt)/sizeof(aOpt[0]) ){
6560     Tcl_AppendResult(interp, "unknown optimization - should be one of:",
6561                      (char*)0);
6562     for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
6563       Tcl_AppendResult(interp, " ", aOpt[i].zOptName, (char*)0);
6564     }
6565     return TCL_ERROR;
6566   }
6567   sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask);
6568   return TCL_OK;
6569 }
6570 
6571 typedef struct sqlite3_api_routines sqlite3_api_routines;
6572 /*
6573 **     load_static_extension DB NAME ...
6574 **
6575 ** Load one or more statically linked extensions.
6576 */
6577 static int tclLoadStaticExtensionCmd(
6578   void * clientData,
6579   Tcl_Interp *interp,
6580   int objc,
6581   Tcl_Obj *CONST objv[]
6582 ){
6583   extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*);
6584   extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*);
6585   extern int sqlite3_eval_init(sqlite3*,char**,const sqlite3_api_routines*);
6586   extern int sqlite3_fileio_init(sqlite3*,char**,const sqlite3_api_routines*);
6587   extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*);
6588   extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*);
6589   extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*);
6590   extern int sqlite3_percentile_init(sqlite3*,char**,const sqlite3_api_routines*);
6591   extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*);
6592   extern int sqlite3_series_init(sqlite3*,char**,const sqlite3_api_routines*);
6593   extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*);
6594   extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*);
6595   extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*);
6596   static const struct {
6597     const char *zExtName;
6598     int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*);
6599   } aExtension[] = {
6600     { "amatch",                sqlite3_amatch_init               },
6601     { "closure",               sqlite3_closure_init              },
6602     { "eval",                  sqlite3_eval_init                 },
6603     { "fileio",                sqlite3_fileio_init               },
6604     { "fuzzer",                sqlite3_fuzzer_init               },
6605     { "ieee754",               sqlite3_ieee_init                 },
6606     { "nextchar",              sqlite3_nextchar_init             },
6607     { "percentile",            sqlite3_percentile_init           },
6608     { "regexp",                sqlite3_regexp_init               },
6609     { "series",                sqlite3_series_init               },
6610     { "spellfix",              sqlite3_spellfix_init             },
6611     { "totype",                sqlite3_totype_init               },
6612     { "wholenumber",           sqlite3_wholenumber_init          },
6613   };
6614   sqlite3 *db;
6615   const char *zName;
6616   int i, j, rc;
6617   char *zErrMsg = 0;
6618   if( objc<3 ){
6619     Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ...");
6620     return TCL_ERROR;
6621   }
6622   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6623   for(j=2; j<objc; j++){
6624     zName = Tcl_GetString(objv[j]);
6625     for(i=0; i<ArraySize(aExtension); i++){
6626       if( strcmp(zName, aExtension[i].zExtName)==0 ) break;
6627     }
6628     if( i>=ArraySize(aExtension) ){
6629       Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0);
6630       return TCL_ERROR;
6631     }
6632     if( aExtension[i].pInit ){
6633       rc = aExtension[i].pInit(db, &zErrMsg, 0);
6634     }else{
6635       rc = SQLITE_OK;
6636     }
6637     if( rc!=SQLITE_OK || zErrMsg ){
6638       Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg,
6639                        (char*)0);
6640       sqlite3_free(zErrMsg);
6641       return TCL_ERROR;
6642     }
6643   }
6644   return TCL_OK;
6645 }
6646 
6647 /*
6648 **     sorter_test_fakeheap BOOL
6649 **
6650 */
6651 static int sorter_test_fakeheap(
6652   void * clientData,
6653   Tcl_Interp *interp,
6654   int objc,
6655   Tcl_Obj *CONST objv[]
6656 ){
6657   int bArg;
6658   if( objc!=2 ){
6659     Tcl_WrongNumArgs(interp, 1, objv, "BOOL");
6660     return TCL_ERROR;
6661   }
6662 
6663   if( Tcl_GetBooleanFromObj(interp, objv[1], &bArg) ){
6664     return TCL_ERROR;
6665   }
6666 
6667   if( bArg ){
6668     if( sqlite3GlobalConfig.pHeap==0 ){
6669       sqlite3GlobalConfig.pHeap = SQLITE_INT_TO_PTR(-1);
6670     }
6671   }else{
6672     if( sqlite3GlobalConfig.pHeap==SQLITE_INT_TO_PTR(-1) ){
6673       sqlite3GlobalConfig.pHeap = 0;
6674     }
6675   }
6676 
6677   Tcl_ResetResult(interp);
6678   return TCL_OK;
6679 }
6680 
6681 /*
6682 **     sorter_test_sort4_helper DB SQL1 NSTEP SQL2
6683 **
6684 ** Compile SQL statement $SQL1 and step it $NSTEP times. For each row,
6685 ** check that the leftmost and rightmost columns returned are both integers,
6686 ** and that both contain the same value.
6687 **
6688 ** Then execute statement $SQL2. Check that the statement returns the same
6689 ** set of integers in the same order as in the previous step (using $SQL1).
6690 */
6691 static int sorter_test_sort4_helper(
6692   void * clientData,
6693   Tcl_Interp *interp,
6694   int objc,
6695   Tcl_Obj *CONST objv[]
6696 ){
6697   const char *zSql1;
6698   const char *zSql2;
6699   int nStep;
6700   int iStep;
6701   int iCksum1 = 0;
6702   int iCksum2 = 0;
6703   int rc;
6704   int iB;
6705   sqlite3 *db;
6706   sqlite3_stmt *pStmt;
6707 
6708   if( objc!=5 ){
6709     Tcl_WrongNumArgs(interp, 1, objv, "DB SQL1 NSTEP SQL2");
6710     return TCL_ERROR;
6711   }
6712 
6713   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6714   zSql1 = Tcl_GetString(objv[2]);
6715   if( Tcl_GetIntFromObj(interp, objv[3], &nStep) ) return TCL_ERROR;
6716   zSql2 = Tcl_GetString(objv[4]);
6717 
6718   rc = sqlite3_prepare_v2(db, zSql1, -1, &pStmt, 0);
6719   if( rc!=SQLITE_OK ) goto sql_error;
6720 
6721   iB = sqlite3_column_count(pStmt)-1;
6722   for(iStep=0; iStep<nStep && SQLITE_ROW==sqlite3_step(pStmt); iStep++){
6723     int a = sqlite3_column_int(pStmt, 0);
6724     if( a!=sqlite3_column_int(pStmt, iB) ){
6725       Tcl_AppendResult(interp, "data error: (a!=b)", 0);
6726       return TCL_ERROR;
6727     }
6728 
6729     iCksum1 += (iCksum1 << 3) + a;
6730   }
6731   rc = sqlite3_finalize(pStmt);
6732   if( rc!=SQLITE_OK ) goto sql_error;
6733 
6734   rc = sqlite3_prepare_v2(db, zSql2, -1, &pStmt, 0);
6735   if( rc!=SQLITE_OK ) goto sql_error;
6736   for(iStep=0; SQLITE_ROW==sqlite3_step(pStmt); iStep++){
6737     int a = sqlite3_column_int(pStmt, 0);
6738     iCksum2 += (iCksum2 << 3) + a;
6739   }
6740   rc = sqlite3_finalize(pStmt);
6741   if( rc!=SQLITE_OK ) goto sql_error;
6742 
6743   if( iCksum1!=iCksum2 ){
6744     Tcl_AppendResult(interp, "checksum mismatch", 0);
6745     return TCL_ERROR;
6746   }
6747 
6748   return TCL_OK;
6749  sql_error:
6750   Tcl_AppendResult(interp, "sql error: ", sqlite3_errmsg(db), 0);
6751   return TCL_ERROR;
6752 }
6753 
6754 
6755 #ifdef SQLITE_USER_AUTHENTICATION
6756 #include "sqlite3userauth.h"
6757 /*
6758 ** tclcmd:  sqlite3_user_authenticate DB USERNAME PASSWORD
6759 */
6760 static int test_user_authenticate(
6761   ClientData clientData, /* Unused */
6762   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
6763   int objc,              /* Number of arguments */
6764   Tcl_Obj *CONST objv[]  /* Command arguments */
6765 ){
6766   char *zUser = 0;
6767   char *zPasswd = 0;
6768   int nPasswd = 0;
6769   sqlite3 *db;
6770   int rc;
6771 
6772   if( objc!=4 ){
6773     Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD");
6774     return TCL_ERROR;
6775   }
6776   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6777     return TCL_ERROR;
6778   }
6779   zUser = Tcl_GetString(objv[2]);
6780   zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
6781   rc = sqlite3_user_authenticate(db, zUser, zPasswd, nPasswd);
6782   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
6783   return TCL_OK;
6784 }
6785 #endif /* SQLITE_USER_AUTHENTICATION */
6786 
6787 #ifdef SQLITE_USER_AUTHENTICATION
6788 /*
6789 ** tclcmd:  sqlite3_user_add DB USERNAME PASSWORD ISADMIN
6790 */
6791 static int test_user_add(
6792   ClientData clientData, /* Unused */
6793   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
6794   int objc,              /* Number of arguments */
6795   Tcl_Obj *CONST objv[]  /* Command arguments */
6796 ){
6797   char *zUser = 0;
6798   char *zPasswd = 0;
6799   int nPasswd = 0;
6800   int isAdmin = 0;
6801   sqlite3 *db;
6802   int rc;
6803 
6804   if( objc!=5 ){
6805     Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN");
6806     return TCL_ERROR;
6807   }
6808   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6809     return TCL_ERROR;
6810   }
6811   zUser = Tcl_GetString(objv[2]);
6812   zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
6813   Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin);
6814   rc = sqlite3_user_add(db, zUser, zPasswd, nPasswd, isAdmin);
6815   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
6816   return TCL_OK;
6817 }
6818 #endif /* SQLITE_USER_AUTHENTICATION */
6819 
6820 #ifdef SQLITE_USER_AUTHENTICATION
6821 /*
6822 ** tclcmd:  sqlite3_user_change DB USERNAME PASSWORD ISADMIN
6823 */
6824 static int test_user_change(
6825   ClientData clientData, /* Unused */
6826   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
6827   int objc,              /* Number of arguments */
6828   Tcl_Obj *CONST objv[]  /* Command arguments */
6829 ){
6830   char *zUser = 0;
6831   char *zPasswd = 0;
6832   int nPasswd = 0;
6833   int isAdmin = 0;
6834   sqlite3 *db;
6835   int rc;
6836 
6837   if( objc!=5 ){
6838     Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN");
6839     return TCL_ERROR;
6840   }
6841   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6842     return TCL_ERROR;
6843   }
6844   zUser = Tcl_GetString(objv[2]);
6845   zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
6846   Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin);
6847   rc = sqlite3_user_change(db, zUser, zPasswd, nPasswd, isAdmin);
6848   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
6849   return TCL_OK;
6850 }
6851 #endif /* SQLITE_USER_AUTHENTICATION */
6852 
6853 #ifdef SQLITE_USER_AUTHENTICATION
6854 /*
6855 ** tclcmd:  sqlite3_user_delete DB USERNAME
6856 */
6857 static int test_user_delete(
6858   ClientData clientData, /* Unused */
6859   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
6860   int objc,              /* Number of arguments */
6861   Tcl_Obj *CONST objv[]  /* Command arguments */
6862 ){
6863   char *zUser = 0;
6864   sqlite3 *db;
6865   int rc;
6866 
6867   if( objc!=3 ){
6868     Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME");
6869     return TCL_ERROR;
6870   }
6871   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6872     return TCL_ERROR;
6873   }
6874   zUser = Tcl_GetString(objv[2]);
6875   rc = sqlite3_user_delete(db, zUser);
6876   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
6877   return TCL_OK;
6878 }
6879 #endif /* SQLITE_USER_AUTHENTICATION */
6880 
6881 /*
6882 ** tclcmd: bad_behavior TYPE
6883 **
6884 ** Do some things that should trigger a valgrind or -fsanitize=undefined
6885 ** warning.  This is used to verify that errors and warnings output by those
6886 ** tools are detected by the test scripts.
6887 **
6888 **       TYPE       BEHAVIOR
6889 **       1          Overflow a signed integer
6890 **       2          Jump based on an uninitialized variable
6891 **       3          Read after free
6892 **       4          Panic
6893 */
6894 static int test_bad_behavior(
6895   ClientData clientData, /* Pointer to an integer containing zero */
6896   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
6897   int objc,              /* Number of arguments */
6898   Tcl_Obj *CONST objv[]  /* Command arguments */
6899 ){
6900   int iType;
6901   int xyz;
6902   int i = *(int*)clientData;
6903   int j;
6904   int w[10];
6905   int *a;
6906   if( objc!=2 ){
6907     Tcl_WrongNumArgs(interp, 1, objv, "TYPE");
6908     return TCL_ERROR;
6909   }
6910   if( Tcl_GetIntFromObj(interp, objv[1], &iType) ) return TCL_ERROR;
6911   switch( iType ){
6912     case 1: {
6913       xyz = 0x7fffff00 - i;
6914       xyz += 0x100;
6915       Tcl_SetObjResult(interp, Tcl_NewIntObj(xyz));
6916       break;
6917     }
6918     case 2: {
6919       w[1] = 5;
6920       if( w[i]>0 ) w[1]++;
6921       Tcl_SetObjResult(interp, Tcl_NewIntObj(w[1]));
6922       break;
6923     }
6924     case 3: {
6925       a = malloc( sizeof(int)*10 );
6926       for(j=0; j<10; j++) a[j] = j;
6927       free(a);
6928       Tcl_SetObjResult(interp, Tcl_NewIntObj(a[i]));
6929       break;
6930     }
6931     case 4: {
6932       Tcl_Panic("Deliberate panic");
6933       break;
6934     }
6935   }
6936   return TCL_OK;
6937 }
6938 
6939 /*
6940 ** tclcmd:   register_dbstat_vtab DB
6941 **
6942 ** Cause the dbstat virtual table to be available on the connection DB
6943 */
6944 static int test_register_dbstat_vtab(
6945   void *clientData,
6946   Tcl_Interp *interp,
6947   int objc,
6948   Tcl_Obj *CONST objv[]
6949 ){
6950 #ifdef SQLITE_OMIT_VIRTUALTABLE
6951   Tcl_AppendResult(interp, "dbstat not available because of "
6952                            "SQLITE_OMIT_VIRTUALTABLE", (void*)0);
6953   return TCL_ERROR;
6954 #else
6955   struct SqliteDb { sqlite3 *db; };
6956   char *zDb;
6957   Tcl_CmdInfo cmdInfo;
6958 
6959   if( objc!=2 ){
6960     Tcl_WrongNumArgs(interp, 1, objv, "DB");
6961     return TCL_ERROR;
6962   }
6963 
6964   zDb = Tcl_GetString(objv[1]);
6965   if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
6966     sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
6967     sqlite3DbstatRegister(db);
6968   }
6969   return TCL_OK;
6970 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6971 }
6972 
6973 /*
6974 ** tclcmd:   sqlite3_db_config DB SETTING VALUE
6975 **
6976 ** Invoke sqlite3_db_config() for one of the setting values.
6977 */
6978 static int test_sqlite3_db_config(
6979   void *clientData,
6980   Tcl_Interp *interp,
6981   int objc,
6982   Tcl_Obj *CONST objv[]
6983 ){
6984   static const struct {
6985     const char *zName;
6986     int eVal;
6987   } aSetting[] = {
6988     { "FKEY",            SQLITE_DBCONFIG_ENABLE_FKEY },
6989     { "TRIGGER",         SQLITE_DBCONFIG_ENABLE_TRIGGER },
6990     { "FTS3_TOKENIZER",  SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
6991   };
6992   int i;
6993   int v;
6994   const char *zSetting;
6995   sqlite3 *db;
6996 
6997   if( objc!=4 ){
6998     Tcl_WrongNumArgs(interp, 1, objv, "DB SETTING VALUE");
6999     return TCL_ERROR;
7000   }
7001   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
7002   zSetting = Tcl_GetString(objv[2]);
7003   if( sqlite3_strglob("SQLITE_*", zSetting)==0 ) zSetting += 7;
7004   if( sqlite3_strglob("DBCONFIG_*", zSetting)==0 ) zSetting += 9;
7005   if( sqlite3_strglob("ENABLE_*", zSetting)==0 ) zSetting += 7;
7006   for(i=0; i<ArraySize(aSetting); i++){
7007     if( strcmp(zSetting, aSetting[i].zName)==0 ) break;
7008   }
7009   if( i>=ArraySize(aSetting) ){
7010     Tcl_SetObjResult(interp,
7011       Tcl_NewStringObj("unknown sqlite3_db_config setting", -1));
7012     return TCL_ERROR;
7013   }
7014   if( Tcl_GetIntFromObj(interp, objv[3], &v) ) return TCL_ERROR;
7015   sqlite3_db_config(db, aSetting[i].eVal, v, &v);
7016   Tcl_SetObjResult(interp, Tcl_NewIntObj(v));
7017   return TCL_OK;
7018 }
7019 
7020 /*
7021 ** Register commands with the TCL interpreter.
7022 */
7023 int Sqlitetest1_Init(Tcl_Interp *interp){
7024   extern int sqlite3_search_count;
7025   extern int sqlite3_found_count;
7026   extern int sqlite3_interrupt_count;
7027   extern int sqlite3_open_file_count;
7028   extern int sqlite3_sort_count;
7029   extern int sqlite3_current_time;
7030 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
7031   extern int sqlite3_hostid_num;
7032 #endif
7033   extern int sqlite3_max_blobsize;
7034   extern int sqlite3BtreeSharedCacheReport(void*,
7035                                           Tcl_Interp*,int,Tcl_Obj*CONST*);
7036   static int iZero = 0;
7037   static struct {
7038      char *zName;
7039      Tcl_CmdProc *xProc;
7040   } aCmd[] = {
7041      { "db_enter",                      (Tcl_CmdProc*)db_enter               },
7042      { "db_leave",                      (Tcl_CmdProc*)db_leave               },
7043      { "sqlite3_mprintf_int",           (Tcl_CmdProc*)sqlite3_mprintf_int    },
7044      { "sqlite3_mprintf_int64",         (Tcl_CmdProc*)sqlite3_mprintf_int64  },
7045      { "sqlite3_mprintf_long",          (Tcl_CmdProc*)sqlite3_mprintf_long   },
7046      { "sqlite3_mprintf_str",           (Tcl_CmdProc*)sqlite3_mprintf_str    },
7047      { "sqlite3_snprintf_str",          (Tcl_CmdProc*)sqlite3_snprintf_str   },
7048      { "sqlite3_mprintf_stronly",       (Tcl_CmdProc*)sqlite3_mprintf_stronly},
7049      { "sqlite3_mprintf_double",        (Tcl_CmdProc*)sqlite3_mprintf_double },
7050      { "sqlite3_mprintf_scaled",        (Tcl_CmdProc*)sqlite3_mprintf_scaled },
7051      { "sqlite3_mprintf_hexdouble",   (Tcl_CmdProc*)sqlite3_mprintf_hexdouble},
7052      { "sqlite3_mprintf_z_test",        (Tcl_CmdProc*)test_mprintf_z        },
7053      { "sqlite3_mprintf_n_test",        (Tcl_CmdProc*)test_mprintf_n        },
7054      { "sqlite3_snprintf_int",          (Tcl_CmdProc*)test_snprintf_int     },
7055      { "sqlite3_last_insert_rowid",     (Tcl_CmdProc*)test_last_rowid       },
7056      { "sqlite3_exec_printf",           (Tcl_CmdProc*)test_exec_printf      },
7057      { "sqlite3_exec_hex",              (Tcl_CmdProc*)test_exec_hex         },
7058      { "sqlite3_exec",                  (Tcl_CmdProc*)test_exec             },
7059      { "sqlite3_exec_nr",               (Tcl_CmdProc*)test_exec_nr          },
7060 #ifndef SQLITE_OMIT_GET_TABLE
7061      { "sqlite3_get_table_printf",      (Tcl_CmdProc*)test_get_table_printf },
7062 #endif
7063      { "sqlite3_close",                 (Tcl_CmdProc*)sqlite_test_close     },
7064      { "sqlite3_close_v2",              (Tcl_CmdProc*)sqlite_test_close_v2  },
7065      { "sqlite3_create_function",       (Tcl_CmdProc*)test_create_function  },
7066      { "sqlite3_create_aggregate",      (Tcl_CmdProc*)test_create_aggregate },
7067      { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func    },
7068      { "sqlite_abort",                  (Tcl_CmdProc*)sqlite_abort          },
7069      { "sqlite_bind",                   (Tcl_CmdProc*)test_bind             },
7070      { "breakpoint",                    (Tcl_CmdProc*)test_breakpoint       },
7071      { "sqlite3_key",                   (Tcl_CmdProc*)test_key              },
7072      { "sqlite3_rekey",                 (Tcl_CmdProc*)test_rekey            },
7073      { "sqlite_set_magic",              (Tcl_CmdProc*)sqlite_set_magic      },
7074      { "sqlite3_interrupt",             (Tcl_CmdProc*)test_interrupt        },
7075      { "sqlite_delete_function",        (Tcl_CmdProc*)delete_function       },
7076      { "sqlite_delete_collation",       (Tcl_CmdProc*)delete_collation      },
7077      { "sqlite3_get_autocommit",        (Tcl_CmdProc*)get_autocommit        },
7078      { "sqlite3_stack_used",            (Tcl_CmdProc*)test_stack_used       },
7079      { "sqlite3_busy_timeout",          (Tcl_CmdProc*)test_busy_timeout     },
7080      { "printf",                        (Tcl_CmdProc*)test_printf           },
7081      { "sqlite3IoTrace",              (Tcl_CmdProc*)test_io_trace         },
7082      { "clang_sanitize_address",        (Tcl_CmdProc*)clang_sanitize_address },
7083   };
7084   static struct {
7085      char *zName;
7086      Tcl_ObjCmdProc *xProc;
7087      void *clientData;
7088   } aObjCmd[] = {
7089      { "sqlite3_db_config",             test_sqlite3_db_config, 0 },
7090      { "bad_behavior",                  test_bad_behavior,  (void*)&iZero },
7091      { "register_dbstat_vtab",          test_register_dbstat_vtab  },
7092      { "sqlite3_connection_pointer",    get_sqlite_pointer, 0 },
7093      { "sqlite3_bind_int",              test_bind_int,      0 },
7094      { "sqlite3_bind_zeroblob",         test_bind_zeroblob, 0 },
7095      { "sqlite3_bind_zeroblob64",       test_bind_zeroblob64, 0 },
7096      { "sqlite3_bind_int64",            test_bind_int64,    0 },
7097      { "sqlite3_bind_double",           test_bind_double,   0 },
7098      { "sqlite3_bind_null",             test_bind_null     ,0 },
7099      { "sqlite3_bind_text",             test_bind_text     ,0 },
7100      { "sqlite3_bind_text16",           test_bind_text16   ,0 },
7101      { "sqlite3_bind_blob",             test_bind_blob     ,0 },
7102      { "sqlite3_bind_parameter_count",  test_bind_parameter_count, 0},
7103      { "sqlite3_bind_parameter_name",   test_bind_parameter_name,  0},
7104      { "sqlite3_bind_parameter_index",  test_bind_parameter_index, 0},
7105      { "sqlite3_clear_bindings",        test_clear_bindings, 0},
7106      { "sqlite3_sleep",                 test_sleep,          0},
7107      { "sqlite3_errcode",               test_errcode       ,0 },
7108      { "sqlite3_extended_errcode",      test_ex_errcode    ,0 },
7109      { "sqlite3_errmsg",                test_errmsg        ,0 },
7110      { "sqlite3_errmsg16",              test_errmsg16      ,0 },
7111      { "sqlite3_open",                  test_open          ,0 },
7112      { "sqlite3_open16",                test_open16        ,0 },
7113      { "sqlite3_open_v2",               test_open_v2       ,0 },
7114      { "sqlite3_complete16",            test_complete16    ,0 },
7115 
7116      { "sqlite3_prepare",               test_prepare       ,0 },
7117      { "sqlite3_prepare16",             test_prepare16     ,0 },
7118      { "sqlite3_prepare_v2",            test_prepare_v2    ,0 },
7119      { "sqlite3_prepare_tkt3134",       test_prepare_tkt3134, 0},
7120      { "sqlite3_prepare16_v2",          test_prepare16_v2  ,0 },
7121      { "sqlite3_finalize",              test_finalize      ,0 },
7122      { "sqlite3_stmt_status",           test_stmt_status   ,0 },
7123      { "sqlite3_reset",                 test_reset         ,0 },
7124      { "sqlite3_expired",               test_expired       ,0 },
7125      { "sqlite3_transfer_bindings",     test_transfer_bind ,0 },
7126      { "sqlite3_changes",               test_changes       ,0 },
7127      { "sqlite3_step",                  test_step          ,0 },
7128      { "sqlite3_sql",                   test_sql           ,0 },
7129      { "sqlite3_next_stmt",             test_next_stmt     ,0 },
7130      { "sqlite3_stmt_readonly",         test_stmt_readonly ,0 },
7131      { "sqlite3_stmt_busy",             test_stmt_busy     ,0 },
7132      { "uses_stmt_journal",             uses_stmt_journal ,0 },
7133 
7134      { "sqlite3_release_memory",        test_release_memory,     0},
7135      { "sqlite3_db_release_memory",     test_db_release_memory,  0},
7136      { "sqlite3_db_cacheflush",         test_db_cacheflush,      0},
7137      { "sqlite3_system_errno",          test_system_errno,       0},
7138      { "sqlite3_db_filename",           test_db_filename,        0},
7139      { "sqlite3_db_readonly",           test_db_readonly,        0},
7140      { "sqlite3_soft_heap_limit",       test_soft_heap_limit,    0},
7141      { "sqlite3_thread_cleanup",        test_thread_cleanup,     0},
7142      { "sqlite3_pager_refcounts",       test_pager_refcounts,    0},
7143 
7144      { "sqlite3_load_extension",        test_load_extension,     0},
7145      { "sqlite3_enable_load_extension", test_enable_load,        0},
7146      { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
7147      { "sqlite3_limit",                 test_limit,                 0},
7148 
7149      { "save_prng_state",               save_prng_state,    0 },
7150      { "restore_prng_state",            restore_prng_state, 0 },
7151      { "reset_prng_state",              reset_prng_state,   0 },
7152      { "database_never_corrupt",        database_never_corrupt, 0},
7153      { "database_may_be_corrupt",       database_may_be_corrupt, 0},
7154      { "optimization_control",          optimization_control,0},
7155 #if SQLITE_OS_WIN
7156      { "lock_win32_file",               win32_file_lock,    0 },
7157      { "exists_win32_path",             win32_exists_path,  0 },
7158      { "find_win32_file",               win32_find_file,    0 },
7159      { "delete_win32_file",             win32_delete_file,  0 },
7160      { "make_win32_dir",                win32_mkdir,        0 },
7161      { "remove_win32_dir",              win32_rmdir,        0 },
7162 #endif
7163      { "tcl_objproc",                   runAsObjProc,       0 },
7164 
7165      /* sqlite3_column_*() API */
7166      { "sqlite3_column_count",          test_column_count  ,0 },
7167      { "sqlite3_data_count",            test_data_count    ,0 },
7168      { "sqlite3_column_type",           test_column_type   ,0 },
7169      { "sqlite3_column_blob",           test_column_blob   ,0 },
7170      { "sqlite3_column_double",         test_column_double ,0 },
7171      { "sqlite3_column_int64",          test_column_int64  ,0 },
7172      { "sqlite3_column_text",   test_stmt_utf8,  (void*)sqlite3_column_text },
7173      { "sqlite3_column_name",   test_stmt_utf8,  (void*)sqlite3_column_name },
7174      { "sqlite3_column_int",    test_stmt_int,   (void*)sqlite3_column_int  },
7175      { "sqlite3_column_bytes",  test_stmt_int,   (void*)sqlite3_column_bytes},
7176 #ifndef SQLITE_OMIT_DECLTYPE
7177      { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype},
7178 #endif
7179 #ifdef SQLITE_ENABLE_COLUMN_METADATA
7180 { "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name},
7181 { "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name},
7182 { "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name},
7183 #endif
7184 
7185 #ifndef SQLITE_OMIT_UTF16
7186      { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 },
7187      { "sqlite3_column_text16",  test_stmt_utf16, (void*)sqlite3_column_text16},
7188      { "sqlite3_column_name16",  test_stmt_utf16, (void*)sqlite3_column_name16},
7189      { "add_alignment_test_collations", add_alignment_test_collations, 0      },
7190 #ifndef SQLITE_OMIT_DECLTYPE
7191      { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16},
7192 #endif
7193 #ifdef SQLITE_ENABLE_COLUMN_METADATA
7194 {"sqlite3_column_database_name16",
7195   test_stmt_utf16, (void*)sqlite3_column_database_name16},
7196 {"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16},
7197 {"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16},
7198 #endif
7199 #endif
7200      { "sqlite3_create_collation_v2", test_create_collation_v2, 0 },
7201      { "sqlite3_global_recover",     test_global_recover, 0   },
7202      { "working_64bit_int",          working_64bit_int,   0   },
7203      { "vfs_unlink_test",            vfs_unlink_test,     0   },
7204      { "vfs_initfail_test",          vfs_initfail_test,   0   },
7205      { "vfs_unregister_all",         vfs_unregister_all,  0   },
7206      { "vfs_reregister_all",         vfs_reregister_all,  0   },
7207      { "file_control_test",          file_control_test,   0   },
7208      { "file_control_lasterrno_test", file_control_lasterrno_test,  0   },
7209      { "file_control_lockproxy_test", file_control_lockproxy_test,  0   },
7210      { "file_control_chunksize_test", file_control_chunksize_test,  0   },
7211      { "file_control_sizehint_test",  file_control_sizehint_test,   0   },
7212 #if SQLITE_OS_WIN
7213      { "file_control_win32_av_retry", file_control_win32_av_retry,  0   },
7214      { "file_control_win32_set_handle", file_control_win32_set_handle, 0  },
7215 #endif
7216      { "file_control_persist_wal",    file_control_persist_wal,     0   },
7217      { "file_control_powersafe_overwrite",file_control_powersafe_overwrite,0},
7218      { "file_control_vfsname",        file_control_vfsname,         0   },
7219      { "file_control_tempfilename",   file_control_tempfilename,    0   },
7220      { "sqlite3_vfs_list",           vfs_list,     0   },
7221      { "sqlite3_create_function_v2", test_create_function_v2, 0 },
7222 
7223      /* Functions from os.h */
7224 #ifndef SQLITE_OMIT_UTF16
7225      { "add_test_collate",        test_collate, 0            },
7226      { "add_test_collate_needed", test_collate_needed, 0     },
7227      { "add_test_function",       test_function, 0           },
7228      { "add_test_utf16bin_collate",    test_utf16bin_collate, 0        },
7229 #endif
7230      { "sqlite3_test_errstr",     test_errstr, 0             },
7231      { "tcl_variable_type",       tcl_variable_type, 0       },
7232 #ifndef SQLITE_OMIT_SHARED_CACHE
7233      { "sqlite3_enable_shared_cache", test_enable_shared, 0  },
7234      { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0},
7235 #endif
7236      { "sqlite3_libversion_number", test_libversion_number, 0  },
7237      { "sqlite3_table_column_metadata", test_table_column_metadata, 0  },
7238 #ifndef SQLITE_OMIT_INCRBLOB
7239      { "sqlite3_blob_reopen", test_blob_reopen, 0  },
7240 #endif
7241      { "pcache_stats",       test_pcache_stats, 0  },
7242 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
7243      { "sqlite3_unlock_notify", test_unlock_notify, 0  },
7244 #endif
7245      { "sqlite3_wal_checkpoint",   test_wal_checkpoint, 0  },
7246      { "sqlite3_wal_checkpoint_v2",test_wal_checkpoint_v2, 0  },
7247      { "sqlite3_wal_autocheckpoint",test_wal_autocheckpoint, 0  },
7248      { "test_sqlite3_log",         test_sqlite3_log, 0  },
7249 #ifndef SQLITE_OMIT_EXPLAIN
7250      { "print_explain_query_plan", test_print_eqp, 0  },
7251 #endif
7252      { "sqlite3_test_control", test_test_control },
7253 #if SQLITE_OS_UNIX
7254      { "getrusage", test_getrusage },
7255 #endif
7256      { "load_static_extension", tclLoadStaticExtensionCmd },
7257      { "sorter_test_fakeheap", sorter_test_fakeheap },
7258      { "sorter_test_sort4_helper", sorter_test_sort4_helper },
7259 #ifdef SQLITE_USER_AUTHENTICATION
7260      { "sqlite3_user_authenticate", test_user_authenticate, 0 },
7261      { "sqlite3_user_add",          test_user_add,          0 },
7262      { "sqlite3_user_change",       test_user_change,       0 },
7263      { "sqlite3_user_delete",       test_user_delete,       0 },
7264 #endif
7265 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
7266      { "sqlite3_stmt_scanstatus",       test_stmt_scanstatus,   0 },
7267      { "sqlite3_stmt_scanstatus_reset", test_stmt_scanstatus_reset,   0 },
7268 #endif
7269 #ifdef SQLITE_ENABLE_SQLLOG
7270      { "sqlite3_config_sqllog",         test_config_sqllog,   0 },
7271 #endif
7272      { "vfs_current_time_int64",           vfsCurrentTimeInt64,   0 },
7273 #ifdef SQLITE_ENABLE_SNAPSHOT
7274      { "sqlite3_snapshot_get", test_snapshot_get, 0 },
7275      { "sqlite3_snapshot_open", test_snapshot_open, 0 },
7276      { "sqlite3_snapshot_free", test_snapshot_free, 0 },
7277      { "sqlite3_snapshot_cmp", test_snapshot_cmp, 0 },
7278 #endif
7279   };
7280   static int bitmask_size = sizeof(Bitmask)*8;
7281   static int longdouble_size = sizeof(LONGDOUBLE_TYPE);
7282   int i;
7283   extern int sqlite3_sync_count, sqlite3_fullsync_count;
7284   extern int sqlite3_opentemp_count;
7285   extern int sqlite3_like_count;
7286   extern int sqlite3_xferopt_count;
7287   extern int sqlite3_pager_readdb_count;
7288   extern int sqlite3_pager_writedb_count;
7289   extern int sqlite3_pager_writej_count;
7290 #if SQLITE_OS_WIN
7291   extern LONG volatile sqlite3_os_type;
7292 #endif
7293 #ifdef SQLITE_DEBUG
7294   extern int sqlite3WhereTrace;
7295   extern int sqlite3OSTrace;
7296   extern int sqlite3WalTrace;
7297 #endif
7298 #ifdef SQLITE_TEST
7299 #ifdef SQLITE_ENABLE_FTS3
7300   extern int sqlite3_fts3_enable_parentheses;
7301 #endif
7302 #endif
7303 
7304   for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
7305     Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
7306   }
7307   for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
7308     Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
7309         aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
7310   }
7311   Tcl_LinkVar(interp, "sqlite_search_count",
7312       (char*)&sqlite3_search_count, TCL_LINK_INT);
7313   Tcl_LinkVar(interp, "sqlite_found_count",
7314       (char*)&sqlite3_found_count, TCL_LINK_INT);
7315   Tcl_LinkVar(interp, "sqlite_sort_count",
7316       (char*)&sqlite3_sort_count, TCL_LINK_INT);
7317   Tcl_LinkVar(interp, "sqlite3_max_blobsize",
7318       (char*)&sqlite3_max_blobsize, TCL_LINK_INT);
7319   Tcl_LinkVar(interp, "sqlite_like_count",
7320       (char*)&sqlite3_like_count, TCL_LINK_INT);
7321   Tcl_LinkVar(interp, "sqlite_interrupt_count",
7322       (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
7323   Tcl_LinkVar(interp, "sqlite_open_file_count",
7324       (char*)&sqlite3_open_file_count, TCL_LINK_INT);
7325   Tcl_LinkVar(interp, "sqlite_current_time",
7326       (char*)&sqlite3_current_time, TCL_LINK_INT);
7327 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
7328   Tcl_LinkVar(interp, "sqlite_hostid_num",
7329       (char*)&sqlite3_hostid_num, TCL_LINK_INT);
7330 #endif
7331   Tcl_LinkVar(interp, "sqlite3_xferopt_count",
7332       (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
7333   Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
7334       (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);
7335   Tcl_LinkVar(interp, "sqlite3_pager_writedb_count",
7336       (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT);
7337   Tcl_LinkVar(interp, "sqlite3_pager_writej_count",
7338       (char*)&sqlite3_pager_writej_count, TCL_LINK_INT);
7339 #ifndef SQLITE_OMIT_UTF16
7340   Tcl_LinkVar(interp, "unaligned_string_counter",
7341       (char*)&unaligned_string_counter, TCL_LINK_INT);
7342 #endif
7343 #ifndef SQLITE_OMIT_UTF16
7344   Tcl_LinkVar(interp, "sqlite_last_needed_collation",
7345       (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
7346 #endif
7347 #if SQLITE_OS_WIN
7348   Tcl_LinkVar(interp, "sqlite_os_type",
7349       (char*)&sqlite3_os_type, TCL_LINK_LONG);
7350 #endif
7351 #ifdef SQLITE_TEST
7352   {
7353     static const char *query_plan = "*** OBSOLETE VARIABLE ***";
7354     Tcl_LinkVar(interp, "sqlite_query_plan",
7355        (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
7356   }
7357 #endif
7358 #ifdef SQLITE_DEBUG
7359   Tcl_LinkVar(interp, "sqlite_where_trace",
7360       (char*)&sqlite3WhereTrace, TCL_LINK_INT);
7361   Tcl_LinkVar(interp, "sqlite_os_trace",
7362       (char*)&sqlite3OSTrace, TCL_LINK_INT);
7363 #ifndef SQLITE_OMIT_WAL
7364   Tcl_LinkVar(interp, "sqlite_wal_trace",
7365       (char*)&sqlite3WalTrace, TCL_LINK_INT);
7366 #endif
7367 #endif
7368 #ifndef SQLITE_OMIT_DISKIO
7369   Tcl_LinkVar(interp, "sqlite_opentemp_count",
7370       (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
7371 #endif
7372   Tcl_LinkVar(interp, "sqlite_static_bind_value",
7373       (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
7374   Tcl_LinkVar(interp, "sqlite_static_bind_nbyte",
7375       (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT);
7376   Tcl_LinkVar(interp, "sqlite_temp_directory",
7377       (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
7378   Tcl_LinkVar(interp, "sqlite_data_directory",
7379       (char*)&sqlite3_data_directory, TCL_LINK_STRING);
7380   Tcl_LinkVar(interp, "bitmask_size",
7381       (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
7382   Tcl_LinkVar(interp, "longdouble_size",
7383       (char*)&longdouble_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
7384   Tcl_LinkVar(interp, "sqlite_sync_count",
7385       (char*)&sqlite3_sync_count, TCL_LINK_INT);
7386   Tcl_LinkVar(interp, "sqlite_fullsync_count",
7387       (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
7388 #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST)
7389   Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses",
7390       (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT);
7391 #endif
7392   return TCL_OK;
7393 }
7394