xref: /sqlite-3.40.0/src/test1.c (revision c56fac74)
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 #ifdef SQLITE_HAS_CODEC
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 #else
1935   rc = sqlite3_load_extension(db, zFile, zProc, &zErr);
1936 #endif
1937   if( rc!=SQLITE_OK ){
1938     Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE);
1939     rc = TCL_ERROR;
1940   }else{
1941     rc = TCL_OK;
1942   }
1943   sqlite3_free(zErr);
1944 
1945   return rc;
1946 }
1947 
1948 /*
1949 ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
1950 */
1951 static int test_enable_load(
1952   ClientData clientData, /* Not used */
1953   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1954   int objc,              /* Number of arguments */
1955   Tcl_Obj *CONST objv[]  /* Command arguments */
1956 ){
1957   Tcl_CmdInfo cmdInfo;
1958   sqlite3 *db;
1959   char *zDb;
1960   int onoff;
1961 
1962   if( objc!=3 ){
1963     Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
1964     return TCL_ERROR;
1965   }
1966   zDb = Tcl_GetString(objv[1]);
1967 
1968   /* Extract the C database handle from the Tcl command name */
1969   if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1970     Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1971     return TCL_ERROR;
1972   }
1973   db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1974   assert(db);
1975 
1976   /* Get the onoff parameter */
1977   if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
1978     return TCL_ERROR;
1979   }
1980 
1981 #ifdef SQLITE_OMIT_LOAD_EXTENSION
1982   Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
1983   return TCL_ERROR;
1984 #else
1985   sqlite3_enable_load_extension(db, onoff);
1986   return TCL_OK;
1987 #endif
1988 }
1989 
1990 /*
1991 ** Usage:  sqlite_abort
1992 **
1993 ** Shutdown the process immediately.  This is not a clean shutdown.
1994 ** This command is used to test the recoverability of a database in
1995 ** the event of a program crash.
1996 */
1997 static int sqlite_abort(
1998   void *NotUsed,
1999   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
2000   int argc,              /* Number of arguments */
2001   char **argv            /* Text of each argument */
2002 ){
2003 #if defined(_MSC_VER)
2004   /* We do this, otherwise the test will halt with a popup message
2005    * that we have to click away before the test will continue.
2006    */
2007   _set_abort_behavior( 0, _CALL_REPORTFAULT );
2008 #endif
2009   exit(255);
2010   assert( interp==0 );   /* This will always fail */
2011   return TCL_OK;
2012 }
2013 
2014 /*
2015 ** The following routine is a user-defined SQL function whose purpose
2016 ** is to test the sqlite_set_result() API.
2017 */
2018 static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
2019   while( argc>=2 ){
2020     const char *zArg0 = (char*)sqlite3_value_text(argv[0]);
2021     if( zArg0 ){
2022       if( 0==sqlite3StrICmp(zArg0, "int") ){
2023         sqlite3_result_int(context, sqlite3_value_int(argv[1]));
2024       }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
2025         sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
2026       }else if( sqlite3StrICmp(zArg0,"string")==0 ){
2027         sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1,
2028             SQLITE_TRANSIENT);
2029       }else if( sqlite3StrICmp(zArg0,"double")==0 ){
2030         sqlite3_result_double(context, sqlite3_value_double(argv[1]));
2031       }else if( sqlite3StrICmp(zArg0,"null")==0 ){
2032         sqlite3_result_null(context);
2033       }else if( sqlite3StrICmp(zArg0,"value")==0 ){
2034         sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
2035       }else{
2036         goto error_out;
2037       }
2038     }else{
2039       goto error_out;
2040     }
2041     argc -= 2;
2042     argv += 2;
2043   }
2044   return;
2045 
2046 error_out:
2047   sqlite3_result_error(context,"first argument should be one of: "
2048       "int int64 string double null value", -1);
2049 }
2050 
2051 /*
2052 ** Usage:   sqlite_register_test_function  DB  NAME
2053 **
2054 ** Register the test SQL function on the database DB under the name NAME.
2055 */
2056 static int test_register_func(
2057   void *NotUsed,
2058   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
2059   int argc,              /* Number of arguments */
2060   char **argv            /* Text of each argument */
2061 ){
2062   sqlite3 *db;
2063   int rc;
2064   if( argc!=3 ){
2065     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2066        " DB FUNCTION-NAME", 0);
2067     return TCL_ERROR;
2068   }
2069   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
2070   rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,
2071       testFunc, 0, 0);
2072   if( rc!=0 ){
2073     Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
2074     return TCL_ERROR;
2075   }
2076   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2077   return TCL_OK;
2078 }
2079 
2080 /*
2081 ** Usage:  sqlite3_finalize  STMT
2082 **
2083 ** Finalize a statement handle.
2084 */
2085 static int test_finalize(
2086   void * clientData,
2087   Tcl_Interp *interp,
2088   int objc,
2089   Tcl_Obj *CONST objv[]
2090 ){
2091   sqlite3_stmt *pStmt;
2092   int rc;
2093   sqlite3 *db = 0;
2094 
2095   if( objc!=2 ){
2096     Tcl_AppendResult(interp, "wrong # args: should be \"",
2097         Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2098     return TCL_ERROR;
2099   }
2100 
2101   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2102 
2103   if( pStmt ){
2104     db = StmtToDb(pStmt);
2105   }
2106   rc = sqlite3_finalize(pStmt);
2107   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
2108   if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2109   return TCL_OK;
2110 }
2111 
2112 /*
2113 ** Usage:  sqlite3_stmt_status  STMT  CODE  RESETFLAG
2114 **
2115 ** Get the value of a status counter from a statement.
2116 */
2117 static int test_stmt_status(
2118   void * clientData,
2119   Tcl_Interp *interp,
2120   int objc,
2121   Tcl_Obj *CONST objv[]
2122 ){
2123   int iValue;
2124   int i, op = 0, resetFlag;
2125   const char *zOpName;
2126   sqlite3_stmt *pStmt;
2127 
2128   static const struct {
2129     const char *zName;
2130     int op;
2131   } aOp[] = {
2132     { "SQLITE_STMTSTATUS_FULLSCAN_STEP",   SQLITE_STMTSTATUS_FULLSCAN_STEP   },
2133     { "SQLITE_STMTSTATUS_SORT",            SQLITE_STMTSTATUS_SORT            },
2134     { "SQLITE_STMTSTATUS_AUTOINDEX",       SQLITE_STMTSTATUS_AUTOINDEX       },
2135     { "SQLITE_STMTSTATUS_VM_STEP",         SQLITE_STMTSTATUS_VM_STEP         },
2136   };
2137   if( objc!=4 ){
2138     Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG");
2139     return TCL_ERROR;
2140   }
2141   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2142   zOpName = Tcl_GetString(objv[2]);
2143   for(i=0; i<ArraySize(aOp); i++){
2144     if( strcmp(aOp[i].zName, zOpName)==0 ){
2145       op = aOp[i].op;
2146       break;
2147     }
2148   }
2149   if( i>=ArraySize(aOp) ){
2150     if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR;
2151   }
2152   if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR;
2153   iValue = sqlite3_stmt_status(pStmt, op, resetFlag);
2154   Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue));
2155   return TCL_OK;
2156 }
2157 
2158 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
2159 /*
2160 ** Usage:  sqlite3_stmt_scanstatus STMT IDX
2161 */
2162 static int test_stmt_scanstatus(
2163   void * clientData,
2164   Tcl_Interp *interp,
2165   int objc,
2166   Tcl_Obj *CONST objv[]
2167 ){
2168   sqlite3_stmt *pStmt;            /* First argument */
2169   int idx;                        /* Second argument */
2170 
2171   const char *zName;
2172   const char *zExplain;
2173   sqlite3_int64 nLoop;
2174   sqlite3_int64 nVisit;
2175   double rEst;
2176   int res;
2177 
2178   if( objc!=3 ){
2179     Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX");
2180     return TCL_ERROR;
2181   }
2182   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2183   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2184 
2185   res = sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop);
2186   if( res==0 ){
2187     Tcl_Obj *pRet = Tcl_NewObj();
2188     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nLoop", -1));
2189     Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nLoop));
2190     sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2191     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nVisit", -1));
2192     Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nVisit));
2193     sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_EST, (void*)&rEst);
2194     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nEst", -1));
2195     Tcl_ListObjAppendElement(0, pRet, Tcl_NewDoubleObj(rEst));
2196     sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NAME, (void*)&zName);
2197     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zName", -1));
2198     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zName, -1));
2199     sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2200     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zExplain", -1));
2201     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zExplain, -1));
2202     Tcl_SetObjResult(interp, pRet);
2203   }else{
2204     Tcl_ResetResult(interp);
2205   }
2206   return TCL_OK;
2207 }
2208 
2209 /*
2210 ** Usage:  sqlite3_stmt_scanstatus_reset  STMT
2211 */
2212 static int test_stmt_scanstatus_reset(
2213   void * clientData,
2214   Tcl_Interp *interp,
2215   int objc,
2216   Tcl_Obj *CONST objv[]
2217 ){
2218   sqlite3_stmt *pStmt;            /* First argument */
2219   if( objc!=2 ){
2220     Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2221     return TCL_ERROR;
2222   }
2223   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2224   sqlite3_stmt_scanstatus_reset(pStmt);
2225   return TCL_OK;
2226 }
2227 #endif
2228 
2229 /*
2230 ** Usage:  sqlite3_next_stmt  DB  STMT
2231 **
2232 ** Return the next statment in sequence after STMT.
2233 */
2234 static int test_next_stmt(
2235   void * clientData,
2236   Tcl_Interp *interp,
2237   int objc,
2238   Tcl_Obj *CONST objv[]
2239 ){
2240   sqlite3_stmt *pStmt;
2241   sqlite3 *db = 0;
2242   char zBuf[50];
2243 
2244   if( objc!=3 ){
2245     Tcl_AppendResult(interp, "wrong # args: should be \"",
2246         Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0);
2247     return TCL_ERROR;
2248   }
2249 
2250   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2251   if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR;
2252   pStmt = sqlite3_next_stmt(db, pStmt);
2253   if( pStmt ){
2254     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
2255     Tcl_AppendResult(interp, zBuf, 0);
2256   }
2257   return TCL_OK;
2258 }
2259 
2260 /*
2261 ** Usage:  sqlite3_stmt_readonly  STMT
2262 **
2263 ** Return true if STMT is a NULL pointer or a pointer to a statement
2264 ** that is guaranteed to leave the database unmodified.
2265 */
2266 static int test_stmt_readonly(
2267   void * clientData,
2268   Tcl_Interp *interp,
2269   int objc,
2270   Tcl_Obj *CONST objv[]
2271 ){
2272   sqlite3_stmt *pStmt;
2273   int rc;
2274 
2275   if( objc!=2 ){
2276     Tcl_AppendResult(interp, "wrong # args: should be \"",
2277         Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2278     return TCL_ERROR;
2279   }
2280 
2281   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2282   rc = sqlite3_stmt_readonly(pStmt);
2283   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc));
2284   return TCL_OK;
2285 }
2286 
2287 /*
2288 ** Usage:  sqlite3_stmt_busy  STMT
2289 **
2290 ** Return true if STMT is a non-NULL pointer to a statement
2291 ** that has been stepped but not to completion.
2292 */
2293 static int test_stmt_busy(
2294   void * clientData,
2295   Tcl_Interp *interp,
2296   int objc,
2297   Tcl_Obj *CONST objv[]
2298 ){
2299   sqlite3_stmt *pStmt;
2300   int rc;
2301 
2302   if( objc!=2 ){
2303     Tcl_AppendResult(interp, "wrong # args: should be \"",
2304         Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2305     return TCL_ERROR;
2306   }
2307 
2308   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2309   rc = sqlite3_stmt_busy(pStmt);
2310   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc));
2311   return TCL_OK;
2312 }
2313 
2314 /*
2315 ** Usage:  uses_stmt_journal  STMT
2316 **
2317 ** Return true if STMT uses a statement journal.
2318 */
2319 static int uses_stmt_journal(
2320   void * clientData,
2321   Tcl_Interp *interp,
2322   int objc,
2323   Tcl_Obj *CONST objv[]
2324 ){
2325   sqlite3_stmt *pStmt;
2326 
2327   if( objc!=2 ){
2328     Tcl_AppendResult(interp, "wrong # args: should be \"",
2329         Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
2330     return TCL_ERROR;
2331   }
2332 
2333   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2334   sqlite3_stmt_readonly(pStmt);
2335   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(((Vdbe *)pStmt)->usesStmtJournal));
2336   return TCL_OK;
2337 }
2338 
2339 
2340 /*
2341 ** Usage:  sqlite3_reset  STMT
2342 **
2343 ** Reset a statement handle.
2344 */
2345 static int test_reset(
2346   void * clientData,
2347   Tcl_Interp *interp,
2348   int objc,
2349   Tcl_Obj *CONST objv[]
2350 ){
2351   sqlite3_stmt *pStmt;
2352   int rc;
2353 
2354   if( objc!=2 ){
2355     Tcl_AppendResult(interp, "wrong # args: should be \"",
2356         Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2357     return TCL_ERROR;
2358   }
2359 
2360   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2361 
2362   rc = sqlite3_reset(pStmt);
2363   if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
2364     return TCL_ERROR;
2365   }
2366   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
2367 /*
2368   if( rc ){
2369     return TCL_ERROR;
2370   }
2371 */
2372   return TCL_OK;
2373 }
2374 
2375 /*
2376 ** Usage:  sqlite3_expired STMT
2377 **
2378 ** Return TRUE if a recompilation of the statement is recommended.
2379 */
2380 static int test_expired(
2381   void * clientData,
2382   Tcl_Interp *interp,
2383   int objc,
2384   Tcl_Obj *CONST objv[]
2385 ){
2386 #ifndef SQLITE_OMIT_DEPRECATED
2387   sqlite3_stmt *pStmt;
2388   if( objc!=2 ){
2389     Tcl_AppendResult(interp, "wrong # args: should be \"",
2390         Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2391     return TCL_ERROR;
2392   }
2393   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2394   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
2395 #endif
2396   return TCL_OK;
2397 }
2398 
2399 /*
2400 ** Usage:  sqlite3_transfer_bindings FROMSTMT TOSTMT
2401 **
2402 ** Transfer all bindings from FROMSTMT over to TOSTMT
2403 */
2404 static int test_transfer_bind(
2405   void * clientData,
2406   Tcl_Interp *interp,
2407   int objc,
2408   Tcl_Obj *CONST objv[]
2409 ){
2410 #ifndef SQLITE_OMIT_DEPRECATED
2411   sqlite3_stmt *pStmt1, *pStmt2;
2412   if( objc!=3 ){
2413     Tcl_AppendResult(interp, "wrong # args: should be \"",
2414         Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
2415     return TCL_ERROR;
2416   }
2417   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
2418   if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
2419   Tcl_SetObjResult(interp,
2420      Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
2421 #endif
2422   return TCL_OK;
2423 }
2424 
2425 /*
2426 ** Usage:  sqlite3_changes DB
2427 **
2428 ** Return the number of changes made to the database by the last SQL
2429 ** execution.
2430 */
2431 static int test_changes(
2432   void * clientData,
2433   Tcl_Interp *interp,
2434   int objc,
2435   Tcl_Obj *CONST objv[]
2436 ){
2437   sqlite3 *db;
2438   if( objc!=2 ){
2439     Tcl_AppendResult(interp, "wrong # args: should be \"",
2440        Tcl_GetString(objv[0]), " DB", 0);
2441     return TCL_ERROR;
2442   }
2443   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2444   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
2445   return TCL_OK;
2446 }
2447 
2448 /*
2449 ** This is the "static_bind_value" that variables are bound to when
2450 ** the FLAG option of sqlite3_bind is "static"
2451 */
2452 static char *sqlite_static_bind_value = 0;
2453 static int sqlite_static_bind_nbyte = 0;
2454 
2455 /*
2456 ** Usage:  sqlite3_bind  VM  IDX  VALUE  FLAGS
2457 **
2458 ** Sets the value of the IDX-th occurrence of "?" in the original SQL
2459 ** string.  VALUE is the new value.  If FLAGS=="null" then VALUE is
2460 ** ignored and the value is set to NULL.  If FLAGS=="static" then
2461 ** the value is set to the value of a static variable named
2462 ** "sqlite_static_bind_value".  If FLAGS=="normal" then a copy
2463 ** of the VALUE is made.  If FLAGS=="blob10" then a VALUE is ignored
2464 ** an a 10-byte blob "abc\000xyz\000pq" is inserted.
2465 */
2466 static int test_bind(
2467   void *NotUsed,
2468   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
2469   int argc,              /* Number of arguments */
2470   char **argv            /* Text of each argument */
2471 ){
2472   sqlite3_stmt *pStmt;
2473   int rc;
2474   int idx;
2475   if( argc!=5 ){
2476     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2477        " VM IDX VALUE (null|static|normal)\"", 0);
2478     return TCL_ERROR;
2479   }
2480   if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
2481   if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
2482   if( strcmp(argv[4],"null")==0 ){
2483     rc = sqlite3_bind_null(pStmt, idx);
2484   }else if( strcmp(argv[4],"static")==0 ){
2485     rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
2486   }else if( strcmp(argv[4],"static-nbytes")==0 ){
2487     rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value,
2488                                        sqlite_static_bind_nbyte, 0);
2489   }else if( strcmp(argv[4],"normal")==0 ){
2490     rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
2491   }else if( strcmp(argv[4],"blob10")==0 ){
2492     rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC);
2493   }else{
2494     Tcl_AppendResult(interp, "4th argument should be "
2495         "\"null\" or \"static\" or \"normal\"", 0);
2496     return TCL_ERROR;
2497   }
2498   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2499   if( rc ){
2500     char zBuf[50];
2501     sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
2502     Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
2503     return TCL_ERROR;
2504   }
2505   return TCL_OK;
2506 }
2507 
2508 #ifndef SQLITE_OMIT_UTF16
2509 /*
2510 ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
2511 **
2512 ** This function is used to test that SQLite selects the correct collation
2513 ** sequence callback when multiple versions (for different text encodings)
2514 ** are available.
2515 **
2516 ** Calling this routine registers the collation sequence "test_collate"
2517 ** with database handle <db>. The second argument must be a list of three
2518 ** boolean values. If the first is true, then a version of test_collate is
2519 ** registered for UTF-8, if the second is true, a version is registered for
2520 ** UTF-16le, if the third is true, a UTF-16be version is available.
2521 ** Previous versions of test_collate are deleted.
2522 **
2523 ** The collation sequence test_collate is implemented by calling the
2524 ** following TCL script:
2525 **
2526 **   "test_collate <enc> <lhs> <rhs>"
2527 **
2528 ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
2529 ** The <enc> parameter is the encoding of the collation function that
2530 ** SQLite selected to call. The TCL test script implements the
2531 ** "test_collate" proc.
2532 **
2533 ** Note that this will only work with one interpreter at a time, as the
2534 ** interp pointer to use when evaluating the TCL script is stored in
2535 ** pTestCollateInterp.
2536 */
2537 static Tcl_Interp* pTestCollateInterp;
2538 static int test_collate_func(
2539   void *pCtx,
2540   int nA, const void *zA,
2541   int nB, const void *zB
2542 ){
2543   Tcl_Interp *i = pTestCollateInterp;
2544   int encin = SQLITE_PTR_TO_INT(pCtx);
2545   int res;
2546   int n;
2547 
2548   sqlite3_value *pVal;
2549   Tcl_Obj *pX;
2550 
2551   pX = Tcl_NewStringObj("test_collate", -1);
2552   Tcl_IncrRefCount(pX);
2553 
2554   switch( encin ){
2555     case SQLITE_UTF8:
2556       Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
2557       break;
2558     case SQLITE_UTF16LE:
2559       Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
2560       break;
2561     case SQLITE_UTF16BE:
2562       Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
2563       break;
2564     default:
2565       assert(0);
2566   }
2567 
2568   sqlite3BeginBenignMalloc();
2569   pVal = sqlite3ValueNew(0);
2570   if( pVal ){
2571     sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
2572     n = sqlite3_value_bytes(pVal);
2573     Tcl_ListObjAppendElement(i,pX,
2574         Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
2575     sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
2576     n = sqlite3_value_bytes(pVal);
2577     Tcl_ListObjAppendElement(i,pX,
2578         Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
2579     sqlite3ValueFree(pVal);
2580   }
2581   sqlite3EndBenignMalloc();
2582 
2583   Tcl_EvalObjEx(i, pX, 0);
2584   Tcl_DecrRefCount(pX);
2585   Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
2586   return res;
2587 }
2588 static int test_collate(
2589   void * clientData,
2590   Tcl_Interp *interp,
2591   int objc,
2592   Tcl_Obj *CONST objv[]
2593 ){
2594   sqlite3 *db;
2595   int val;
2596   sqlite3_value *pVal;
2597   int rc;
2598 
2599   if( objc!=5 ) goto bad_args;
2600   pTestCollateInterp = interp;
2601   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2602 
2603   if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
2604   rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
2605           (void *)SQLITE_UTF8, val?test_collate_func:0);
2606   if( rc==SQLITE_OK ){
2607     const void *zUtf16;
2608     if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2609     rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
2610             (void *)SQLITE_UTF16LE, val?test_collate_func:0);
2611     if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
2612 
2613 #if 0
2614     if( sqlite3_iMallocFail>0 ){
2615       sqlite3_iMallocFail++;
2616     }
2617 #endif
2618     sqlite3_mutex_enter(db->mutex);
2619     pVal = sqlite3ValueNew(db);
2620     sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
2621     zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
2622     if( db->mallocFailed ){
2623       rc = SQLITE_NOMEM;
2624     }else{
2625       rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE,
2626           (void *)SQLITE_UTF16BE, val?test_collate_func:0);
2627     }
2628     sqlite3ValueFree(pVal);
2629     sqlite3_mutex_leave(db->mutex);
2630   }
2631   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2632 
2633   if( rc!=SQLITE_OK ){
2634     Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
2635     return TCL_ERROR;
2636   }
2637   return TCL_OK;
2638 
2639 bad_args:
2640   Tcl_AppendResult(interp, "wrong # args: should be \"",
2641       Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
2642   return TCL_ERROR;
2643 }
2644 
2645 /*
2646 ** Usage: add_test_utf16bin_collate <db ptr>
2647 **
2648 ** Add a utf-16 collation sequence named "utf16bin" to the database
2649 ** handle. This collation sequence compares arguments in the same way as the
2650 ** built-in collation "binary".
2651 */
2652 static int test_utf16bin_collate_func(
2653   void *pCtx,
2654   int nA, const void *zA,
2655   int nB, const void *zB
2656 ){
2657   int nCmp = (nA>nB ? nB : nA);
2658   int res = memcmp(zA, zB, nCmp);
2659   if( res==0 ) res = nA - nB;
2660   return res;
2661 }
2662 static int test_utf16bin_collate(
2663   void * clientData,
2664   Tcl_Interp *interp,
2665   int objc,
2666   Tcl_Obj *CONST objv[]
2667 ){
2668   sqlite3 *db;
2669   int rc;
2670 
2671   if( objc!=2 ) goto bad_args;
2672   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2673 
2674   rc = sqlite3_create_collation(db, "utf16bin", SQLITE_UTF16, 0,
2675       test_utf16bin_collate_func
2676   );
2677   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2678   return TCL_OK;
2679 
2680 bad_args:
2681   Tcl_WrongNumArgs(interp, 1, objv, "DB");
2682   return TCL_ERROR;
2683 }
2684 
2685 /*
2686 ** When the collation needed callback is invoked, record the name of
2687 ** the requested collating function here.  The recorded name is linked
2688 ** to a TCL variable and used to make sure that the requested collation
2689 ** name is correct.
2690 */
2691 static char zNeededCollation[200];
2692 static char *pzNeededCollation = zNeededCollation;
2693 
2694 
2695 /*
2696 ** Called when a collating sequence is needed.  Registered using
2697 ** sqlite3_collation_needed16().
2698 */
2699 static void test_collate_needed_cb(
2700   void *pCtx,
2701   sqlite3 *db,
2702   int eTextRep,
2703   const void *pName
2704 ){
2705   int enc = ENC(db);
2706   int i;
2707   char *z;
2708   for(z = (char*)pName, i=0; *z || z[1]; z++){
2709     if( *z ) zNeededCollation[i++] = *z;
2710   }
2711   zNeededCollation[i] = 0;
2712   sqlite3_create_collation(
2713       db, "test_collate", ENC(db), SQLITE_INT_TO_PTR(enc), test_collate_func);
2714 }
2715 
2716 /*
2717 ** Usage: add_test_collate_needed DB
2718 */
2719 static int test_collate_needed(
2720   void * clientData,
2721   Tcl_Interp *interp,
2722   int objc,
2723   Tcl_Obj *CONST objv[]
2724 ){
2725   sqlite3 *db;
2726   int rc;
2727 
2728   if( objc!=2 ) goto bad_args;
2729   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2730   rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
2731   zNeededCollation[0] = 0;
2732   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2733   return TCL_OK;
2734 
2735 bad_args:
2736   Tcl_WrongNumArgs(interp, 1, objv, "DB");
2737   return TCL_ERROR;
2738 }
2739 
2740 /*
2741 ** tclcmd:   add_alignment_test_collations  DB
2742 **
2743 ** Add two new collating sequences to the database DB
2744 **
2745 **     utf16_aligned
2746 **     utf16_unaligned
2747 **
2748 ** Both collating sequences use the same sort order as BINARY.
2749 ** The only difference is that the utf16_aligned collating
2750 ** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
2751 ** Both collating functions increment the unaligned utf16 counter
2752 ** whenever they see a string that begins on an odd byte boundary.
2753 */
2754 static int unaligned_string_counter = 0;
2755 static int alignmentCollFunc(
2756   void *NotUsed,
2757   int nKey1, const void *pKey1,
2758   int nKey2, const void *pKey2
2759 ){
2760   int rc, n;
2761   n = nKey1<nKey2 ? nKey1 : nKey2;
2762   if( nKey1>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey1))) ) unaligned_string_counter++;
2763   if( nKey2>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey2))) ) unaligned_string_counter++;
2764   rc = memcmp(pKey1, pKey2, n);
2765   if( rc==0 ){
2766     rc = nKey1 - nKey2;
2767   }
2768   return rc;
2769 }
2770 static int add_alignment_test_collations(
2771   void * clientData,
2772   Tcl_Interp *interp,
2773   int objc,
2774   Tcl_Obj *CONST objv[]
2775 ){
2776   sqlite3 *db;
2777   if( objc>=2 ){
2778     if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2779     sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16,
2780         0, alignmentCollFunc);
2781     sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED,
2782         0, alignmentCollFunc);
2783   }
2784   return SQLITE_OK;
2785 }
2786 #endif /* !defined(SQLITE_OMIT_UTF16) */
2787 
2788 /*
2789 ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
2790 **
2791 ** This function is used to test that SQLite selects the correct user
2792 ** function callback when multiple versions (for different text encodings)
2793 ** are available.
2794 **
2795 ** Calling this routine registers up to three versions of the user function
2796 ** "test_function" with database handle <db>.  If the second argument is
2797 ** true, then a version of test_function is registered for UTF-8, if the
2798 ** third is true, a version is registered for UTF-16le, if the fourth is
2799 ** true, a UTF-16be version is available.  Previous versions of
2800 ** test_function are deleted.
2801 **
2802 ** The user function is implemented by calling the following TCL script:
2803 **
2804 **   "test_function <enc> <arg>"
2805 **
2806 ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
2807 ** single argument passed to the SQL function. The value returned by
2808 ** the TCL script is used as the return value of the SQL function. It
2809 ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
2810 ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
2811 ** prefers UTF-16BE.
2812 */
2813 #ifndef SQLITE_OMIT_UTF16
2814 static void test_function_utf8(
2815   sqlite3_context *pCtx,
2816   int nArg,
2817   sqlite3_value **argv
2818 ){
2819   Tcl_Interp *interp;
2820   Tcl_Obj *pX;
2821   sqlite3_value *pVal;
2822   interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2823   pX = Tcl_NewStringObj("test_function", -1);
2824   Tcl_IncrRefCount(pX);
2825   Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
2826   Tcl_ListObjAppendElement(interp, pX,
2827       Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
2828   Tcl_EvalObjEx(interp, pX, 0);
2829   Tcl_DecrRefCount(pX);
2830   sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
2831   pVal = sqlite3ValueNew(0);
2832   sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
2833       SQLITE_UTF8, SQLITE_STATIC);
2834   sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
2835       -1, SQLITE_TRANSIENT);
2836   sqlite3ValueFree(pVal);
2837 }
2838 static void test_function_utf16le(
2839   sqlite3_context *pCtx,
2840   int nArg,
2841   sqlite3_value **argv
2842 ){
2843   Tcl_Interp *interp;
2844   Tcl_Obj *pX;
2845   sqlite3_value *pVal;
2846   interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2847   pX = Tcl_NewStringObj("test_function", -1);
2848   Tcl_IncrRefCount(pX);
2849   Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
2850   Tcl_ListObjAppendElement(interp, pX,
2851       Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
2852   Tcl_EvalObjEx(interp, pX, 0);
2853   Tcl_DecrRefCount(pX);
2854   pVal = sqlite3ValueNew(0);
2855   sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
2856       SQLITE_UTF8, SQLITE_STATIC);
2857   sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
2858   sqlite3ValueFree(pVal);
2859 }
2860 static void test_function_utf16be(
2861   sqlite3_context *pCtx,
2862   int nArg,
2863   sqlite3_value **argv
2864 ){
2865   Tcl_Interp *interp;
2866   Tcl_Obj *pX;
2867   sqlite3_value *pVal;
2868   interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2869   pX = Tcl_NewStringObj("test_function", -1);
2870   Tcl_IncrRefCount(pX);
2871   Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
2872   Tcl_ListObjAppendElement(interp, pX,
2873       Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
2874   Tcl_EvalObjEx(interp, pX, 0);
2875   Tcl_DecrRefCount(pX);
2876   pVal = sqlite3ValueNew(0);
2877   sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
2878       SQLITE_UTF8, SQLITE_STATIC);
2879   sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal),
2880       -1, SQLITE_TRANSIENT);
2881   sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal),
2882       -1, SQLITE_TRANSIENT);
2883   sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
2884       -1, SQLITE_TRANSIENT);
2885   sqlite3ValueFree(pVal);
2886 }
2887 #endif /* SQLITE_OMIT_UTF16 */
2888 static int test_function(
2889   void * clientData,
2890   Tcl_Interp *interp,
2891   int objc,
2892   Tcl_Obj *CONST objv[]
2893 ){
2894 #ifndef SQLITE_OMIT_UTF16
2895   sqlite3 *db;
2896   int val;
2897 
2898   if( objc!=5 ) goto bad_args;
2899   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2900 
2901   if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
2902   if( val ){
2903     sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
2904         interp, test_function_utf8, 0, 0);
2905   }
2906   if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2907   if( val ){
2908     sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
2909         interp, test_function_utf16le, 0, 0);
2910   }
2911   if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
2912   if( val ){
2913     sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
2914         interp, test_function_utf16be, 0, 0);
2915   }
2916 
2917   return TCL_OK;
2918 bad_args:
2919   Tcl_AppendResult(interp, "wrong # args: should be \"",
2920       Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
2921 #endif /* SQLITE_OMIT_UTF16 */
2922   return TCL_ERROR;
2923 }
2924 
2925 /*
2926 ** Usage:         sqlite3_test_errstr <err code>
2927 **
2928 ** Test that the english language string equivalents for sqlite error codes
2929 ** are sane. The parameter is an integer representing an sqlite error code.
2930 ** The result is a list of two elements, the string representation of the
2931 ** error code and the english language explanation.
2932 */
2933 static int test_errstr(
2934   void * clientData,
2935   Tcl_Interp *interp,
2936   int objc,
2937   Tcl_Obj *CONST objv[]
2938 ){
2939   char *zCode;
2940   int i;
2941   if( objc!=1 ){
2942     Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
2943   }
2944 
2945   zCode = Tcl_GetString(objv[1]);
2946   for(i=0; i<200; i++){
2947     if( 0==strcmp(t1ErrorName(i), zCode) ) break;
2948   }
2949   Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
2950   return TCL_OK;
2951 }
2952 
2953 /*
2954 ** Usage:    breakpoint
2955 **
2956 ** This routine exists for one purpose - to provide a place to put a
2957 ** breakpoint with GDB that can be triggered using TCL code.  The use
2958 ** for this is when a particular test fails on (say) the 1485th iteration.
2959 ** In the TCL test script, we can add code like this:
2960 **
2961 **     if {$i==1485} breakpoint
2962 **
2963 ** Then run testfixture in the debugger and wait for the breakpoint to
2964 ** fire.  Then additional breakpoints can be set to trace down the bug.
2965 */
2966 static int test_breakpoint(
2967   void *NotUsed,
2968   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
2969   int argc,              /* Number of arguments */
2970   char **argv            /* Text of each argument */
2971 ){
2972   return TCL_OK;         /* Do nothing */
2973 }
2974 
2975 /*
2976 ** Usage:   sqlite3_bind_zeroblob  STMT IDX N
2977 **
2978 ** Test the sqlite3_bind_zeroblob interface.  STMT is a prepared statement.
2979 ** IDX is the index of a wildcard in the prepared statement.  This command
2980 ** binds a N-byte zero-filled BLOB to the wildcard.
2981 */
2982 static int test_bind_zeroblob(
2983   void * clientData,
2984   Tcl_Interp *interp,
2985   int objc,
2986   Tcl_Obj *CONST objv[]
2987 ){
2988   sqlite3_stmt *pStmt;
2989   int idx;
2990   int n;
2991   int rc;
2992 
2993   if( objc!=4 ){
2994     Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
2995     return TCL_ERROR;
2996   }
2997 
2998   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2999   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3000   if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
3001 
3002   rc = sqlite3_bind_zeroblob(pStmt, idx, n);
3003   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3004   if( rc!=SQLITE_OK ){
3005     return TCL_ERROR;
3006   }
3007 
3008   return TCL_OK;
3009 }
3010 
3011 /*
3012 ** Usage:   sqlite3_bind_zeroblob64  STMT IDX N
3013 **
3014 ** Test the sqlite3_bind_zeroblob64 interface.  STMT is a prepared statement.
3015 ** IDX is the index of a wildcard in the prepared statement.  This command
3016 ** binds a N-byte zero-filled BLOB to the wildcard.
3017 */
3018 static int test_bind_zeroblob64(
3019   void * clientData,
3020   Tcl_Interp *interp,
3021   int objc,
3022   Tcl_Obj *CONST objv[]
3023 ){
3024   sqlite3_stmt *pStmt;
3025   int idx;
3026   i64 n;
3027   int rc;
3028 
3029   if( objc!=4 ){
3030     Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
3031     return TCL_ERROR;
3032   }
3033 
3034   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3035   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3036   if( Tcl_GetWideIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
3037 
3038   rc = sqlite3_bind_zeroblob64(pStmt, idx, n);
3039   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3040   if( rc!=SQLITE_OK ){
3041     Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3042     return TCL_ERROR;
3043   }
3044 
3045   return TCL_OK;
3046 }
3047 
3048 /*
3049 ** Usage:   sqlite3_bind_int  STMT N VALUE
3050 **
3051 ** Test the sqlite3_bind_int interface.  STMT is a prepared statement.
3052 ** N is the index of a wildcard in the prepared statement.  This command
3053 ** binds a 32-bit integer VALUE to that wildcard.
3054 */
3055 static int test_bind_int(
3056   void * clientData,
3057   Tcl_Interp *interp,
3058   int objc,
3059   Tcl_Obj *CONST objv[]
3060 ){
3061   sqlite3_stmt *pStmt;
3062   int idx;
3063   int value;
3064   int rc;
3065 
3066   if( objc!=4 ){
3067     Tcl_AppendResult(interp, "wrong # args: should be \"",
3068         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3069     return TCL_ERROR;
3070   }
3071 
3072   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3073   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3074   if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
3075 
3076   rc = sqlite3_bind_int(pStmt, idx, value);
3077   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3078   if( rc!=SQLITE_OK ){
3079     return TCL_ERROR;
3080   }
3081 
3082   return TCL_OK;
3083 }
3084 
3085 
3086 /*
3087 ** Usage:   sqlite3_bind_int64  STMT N VALUE
3088 **
3089 ** Test the sqlite3_bind_int64 interface.  STMT is a prepared statement.
3090 ** N is the index of a wildcard in the prepared statement.  This command
3091 ** binds a 64-bit integer VALUE to that wildcard.
3092 */
3093 static int test_bind_int64(
3094   void * clientData,
3095   Tcl_Interp *interp,
3096   int objc,
3097   Tcl_Obj *CONST objv[]
3098 ){
3099   sqlite3_stmt *pStmt;
3100   int idx;
3101   Tcl_WideInt value;
3102   int rc;
3103 
3104   if( objc!=4 ){
3105     Tcl_AppendResult(interp, "wrong # args: should be \"",
3106         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3107     return TCL_ERROR;
3108   }
3109 
3110   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3111   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3112   if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
3113 
3114   rc = sqlite3_bind_int64(pStmt, idx, value);
3115   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3116   if( rc!=SQLITE_OK ){
3117     return TCL_ERROR;
3118   }
3119 
3120   return TCL_OK;
3121 }
3122 
3123 
3124 /*
3125 ** Usage:   sqlite3_bind_double  STMT N VALUE
3126 **
3127 ** Test the sqlite3_bind_double interface.  STMT is a prepared statement.
3128 ** N is the index of a wildcard in the prepared statement.  This command
3129 ** binds a 64-bit integer VALUE to that wildcard.
3130 */
3131 static int test_bind_double(
3132   void * clientData,
3133   Tcl_Interp *interp,
3134   int objc,
3135   Tcl_Obj *CONST objv[]
3136 ){
3137   sqlite3_stmt *pStmt;
3138   int idx;
3139   double value = 0;
3140   int rc;
3141   const char *zVal;
3142   int i;
3143   static const struct {
3144     const char *zName;     /* Name of the special floating point value */
3145     unsigned int iUpper;   /* Upper 32 bits */
3146     unsigned int iLower;   /* Lower 32 bits */
3147   } aSpecialFp[] = {
3148     {  "NaN",      0x7fffffff, 0xffffffff },
3149     {  "SNaN",     0x7ff7ffff, 0xffffffff },
3150     {  "-NaN",     0xffffffff, 0xffffffff },
3151     {  "-SNaN",    0xfff7ffff, 0xffffffff },
3152     {  "+Inf",     0x7ff00000, 0x00000000 },
3153     {  "-Inf",     0xfff00000, 0x00000000 },
3154     {  "Epsilon",  0x00000000, 0x00000001 },
3155     {  "-Epsilon", 0x80000000, 0x00000001 },
3156     {  "NaN0",     0x7ff80000, 0x00000000 },
3157     {  "-NaN0",    0xfff80000, 0x00000000 },
3158   };
3159 
3160   if( objc!=4 ){
3161     Tcl_AppendResult(interp, "wrong # args: should be \"",
3162         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
3163     return TCL_ERROR;
3164   }
3165 
3166   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3167   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3168 
3169   /* Intercept the string "NaN" and generate a NaN value for it.
3170   ** All other strings are passed through to Tcl_GetDoubleFromObj().
3171   ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions
3172   ** contain a bug.
3173   */
3174   zVal = Tcl_GetString(objv[3]);
3175   for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){
3176     if( strcmp(aSpecialFp[i].zName, zVal)==0 ){
3177       sqlite3_uint64 x;
3178       x = aSpecialFp[i].iUpper;
3179       x <<= 32;
3180       x |= aSpecialFp[i].iLower;
3181       assert( sizeof(value)==8 );
3182       assert( sizeof(x)==8 );
3183       memcpy(&value, &x, 8);
3184       break;
3185     }
3186   }
3187   if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) &&
3188          Tcl_GetDoubleFromObj(interp, objv[3], &value) ){
3189     return TCL_ERROR;
3190   }
3191   rc = sqlite3_bind_double(pStmt, idx, value);
3192   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3193   if( rc!=SQLITE_OK ){
3194     return TCL_ERROR;
3195   }
3196 
3197   return TCL_OK;
3198 }
3199 
3200 /*
3201 ** Usage:   sqlite3_bind_null  STMT N
3202 **
3203 ** Test the sqlite3_bind_null interface.  STMT is a prepared statement.
3204 ** N is the index of a wildcard in the prepared statement.  This command
3205 ** binds a NULL to the wildcard.
3206 */
3207 static int test_bind_null(
3208   void * clientData,
3209   Tcl_Interp *interp,
3210   int objc,
3211   Tcl_Obj *CONST objv[]
3212 ){
3213   sqlite3_stmt *pStmt;
3214   int idx;
3215   int rc;
3216 
3217   if( objc!=3 ){
3218     Tcl_AppendResult(interp, "wrong # args: should be \"",
3219         Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
3220     return TCL_ERROR;
3221   }
3222 
3223   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3224   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3225 
3226   rc = sqlite3_bind_null(pStmt, idx);
3227   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3228   if( rc!=SQLITE_OK ){
3229     return TCL_ERROR;
3230   }
3231 
3232   return TCL_OK;
3233 }
3234 
3235 /*
3236 ** Usage:   sqlite3_bind_text  STMT N STRING BYTES
3237 **
3238 ** Test the sqlite3_bind_text interface.  STMT is a prepared statement.
3239 ** N is the index of a wildcard in the prepared statement.  This command
3240 ** binds a UTF-8 string STRING to the wildcard.  The string is BYTES bytes
3241 ** long.
3242 */
3243 static int test_bind_text(
3244   void * clientData,
3245   Tcl_Interp *interp,
3246   int objc,
3247   Tcl_Obj *CONST objv[]
3248 ){
3249   sqlite3_stmt *pStmt;
3250   int idx;
3251   int bytes;
3252   char *value;
3253   int rc;
3254 
3255   if( objc!=5 ){
3256     Tcl_AppendResult(interp, "wrong # args: should be \"",
3257         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
3258     return TCL_ERROR;
3259   }
3260 
3261   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3262   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3263   value = (char*)Tcl_GetByteArrayFromObj(objv[3], &bytes);
3264   if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
3265 
3266   rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
3267   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3268   if( rc!=SQLITE_OK ){
3269     Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3270     return TCL_ERROR;
3271   }
3272 
3273   return TCL_OK;
3274 }
3275 
3276 /*
3277 ** Usage:   sqlite3_bind_text16 ?-static? STMT N STRING BYTES
3278 **
3279 ** Test the sqlite3_bind_text16 interface.  STMT is a prepared statement.
3280 ** N is the index of a wildcard in the prepared statement.  This command
3281 ** binds a UTF-16 string STRING to the wildcard.  The string is BYTES bytes
3282 ** long.
3283 */
3284 static int test_bind_text16(
3285   void * clientData,
3286   Tcl_Interp *interp,
3287   int objc,
3288   Tcl_Obj *CONST objv[]
3289 ){
3290 #ifndef SQLITE_OMIT_UTF16
3291   sqlite3_stmt *pStmt;
3292   int idx;
3293   int bytes;
3294   char *value;
3295   int rc;
3296 
3297   void (*xDel)(void*) = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
3298   Tcl_Obj *oStmt    = objv[objc-4];
3299   Tcl_Obj *oN       = objv[objc-3];
3300   Tcl_Obj *oString  = objv[objc-2];
3301   Tcl_Obj *oBytes   = objv[objc-1];
3302 
3303   if( objc!=5 && objc!=6){
3304     Tcl_AppendResult(interp, "wrong # args: should be \"",
3305         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
3306     return TCL_ERROR;
3307   }
3308 
3309   if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR;
3310   if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR;
3311   value = (char*)Tcl_GetByteArrayFromObj(oString, 0);
3312   if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR;
3313 
3314   rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
3315   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3316   if( rc!=SQLITE_OK ){
3317     Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
3318     return TCL_ERROR;
3319   }
3320 
3321 #endif /* SQLITE_OMIT_UTF16 */
3322   return TCL_OK;
3323 }
3324 
3325 /*
3326 ** Usage:   sqlite3_bind_blob ?-static? STMT N DATA BYTES
3327 **
3328 ** Test the sqlite3_bind_blob interface.  STMT is a prepared statement.
3329 ** N is the index of a wildcard in the prepared statement.  This command
3330 ** binds a BLOB to the wildcard.  The BLOB is BYTES bytes in size.
3331 */
3332 static int test_bind_blob(
3333   void * clientData,
3334   Tcl_Interp *interp,
3335   int objc,
3336   Tcl_Obj *CONST objv[]
3337 ){
3338   sqlite3_stmt *pStmt;
3339   int idx;
3340   int bytes;
3341   char *value;
3342   int rc;
3343   sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT;
3344 
3345   if( objc!=5 && objc!=6 ){
3346     Tcl_AppendResult(interp, "wrong # args: should be \"",
3347         Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
3348     return TCL_ERROR;
3349   }
3350 
3351   if( objc==6 ){
3352     xDestructor = SQLITE_STATIC;
3353     objv++;
3354   }
3355 
3356   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3357   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3358   value = Tcl_GetString(objv[3]);
3359   if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
3360 
3361   rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor);
3362   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
3363   if( rc!=SQLITE_OK ){
3364     return TCL_ERROR;
3365   }
3366 
3367   return TCL_OK;
3368 }
3369 
3370 /*
3371 ** Usage:   sqlite3_bind_parameter_count  STMT
3372 **
3373 ** Return the number of wildcards in the given statement.
3374 */
3375 static int test_bind_parameter_count(
3376   void * clientData,
3377   Tcl_Interp *interp,
3378   int objc,
3379   Tcl_Obj *CONST objv[]
3380 ){
3381   sqlite3_stmt *pStmt;
3382 
3383   if( objc!=2 ){
3384     Tcl_WrongNumArgs(interp, 1, objv, "STMT");
3385     return TCL_ERROR;
3386   }
3387   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3388   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
3389   return TCL_OK;
3390 }
3391 
3392 /*
3393 ** Usage:   sqlite3_bind_parameter_name  STMT  N
3394 **
3395 ** Return the name of the Nth wildcard.  The first wildcard is 1.
3396 ** An empty string is returned if N is out of range or if the wildcard
3397 ** is nameless.
3398 */
3399 static int test_bind_parameter_name(
3400   void * clientData,
3401   Tcl_Interp *interp,
3402   int objc,
3403   Tcl_Obj *CONST objv[]
3404 ){
3405   sqlite3_stmt *pStmt;
3406   int i;
3407 
3408   if( objc!=3 ){
3409     Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
3410     return TCL_ERROR;
3411   }
3412   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3413   if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
3414   Tcl_SetObjResult(interp,
3415      Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
3416   );
3417   return TCL_OK;
3418 }
3419 
3420 /*
3421 ** Usage:   sqlite3_bind_parameter_index  STMT  NAME
3422 **
3423 ** Return the index of the wildcard called NAME.  Return 0 if there is
3424 ** no such wildcard.
3425 */
3426 static int test_bind_parameter_index(
3427   void * clientData,
3428   Tcl_Interp *interp,
3429   int objc,
3430   Tcl_Obj *CONST objv[]
3431 ){
3432   sqlite3_stmt *pStmt;
3433 
3434   if( objc!=3 ){
3435     Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
3436     return TCL_ERROR;
3437   }
3438   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3439   Tcl_SetObjResult(interp,
3440      Tcl_NewIntObj(
3441        sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
3442      )
3443   );
3444   return TCL_OK;
3445 }
3446 
3447 /*
3448 ** Usage:   sqlite3_clear_bindings STMT
3449 **
3450 */
3451 static int test_clear_bindings(
3452   void * clientData,
3453   Tcl_Interp *interp,
3454   int objc,
3455   Tcl_Obj *CONST objv[]
3456 ){
3457   sqlite3_stmt *pStmt;
3458 
3459   if( objc!=2 ){
3460     Tcl_WrongNumArgs(interp, 1, objv, "STMT");
3461     return TCL_ERROR;
3462   }
3463   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3464   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
3465   return TCL_OK;
3466 }
3467 
3468 /*
3469 ** Usage:   sqlite3_sleep MILLISECONDS
3470 */
3471 static int test_sleep(
3472   void * clientData,
3473   Tcl_Interp *interp,
3474   int objc,
3475   Tcl_Obj *CONST objv[]
3476 ){
3477   int ms;
3478 
3479   if( objc!=2 ){
3480     Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS");
3481     return TCL_ERROR;
3482   }
3483   if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){
3484     return TCL_ERROR;
3485   }
3486   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms)));
3487   return TCL_OK;
3488 }
3489 
3490 /*
3491 ** Usage: sqlite3_extended_errcode DB
3492 **
3493 ** Return the string representation of the most recent sqlite3_* API
3494 ** error code. e.g. "SQLITE_ERROR".
3495 */
3496 static int test_ex_errcode(
3497   void * clientData,
3498   Tcl_Interp *interp,
3499   int objc,
3500   Tcl_Obj *CONST objv[]
3501 ){
3502   sqlite3 *db;
3503   int rc;
3504 
3505   if( objc!=2 ){
3506     Tcl_AppendResult(interp, "wrong # args: should be \"",
3507        Tcl_GetString(objv[0]), " DB", 0);
3508     return TCL_ERROR;
3509   }
3510   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3511   rc = sqlite3_extended_errcode(db);
3512   Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
3513   return TCL_OK;
3514 }
3515 
3516 
3517 /*
3518 ** Usage: sqlite3_errcode DB
3519 **
3520 ** Return the string representation of the most recent sqlite3_* API
3521 ** error code. e.g. "SQLITE_ERROR".
3522 */
3523 static int test_errcode(
3524   void * clientData,
3525   Tcl_Interp *interp,
3526   int objc,
3527   Tcl_Obj *CONST objv[]
3528 ){
3529   sqlite3 *db;
3530   int rc;
3531 
3532   if( objc!=2 ){
3533     Tcl_AppendResult(interp, "wrong # args: should be \"",
3534        Tcl_GetString(objv[0]), " DB", 0);
3535     return TCL_ERROR;
3536   }
3537   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3538   rc = sqlite3_errcode(db);
3539   Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
3540   return TCL_OK;
3541 }
3542 
3543 /*
3544 ** Usage:   sqlite3_errmsg DB
3545 **
3546 ** Returns the UTF-8 representation of the error message string for the
3547 ** most recent sqlite3_* API call.
3548 */
3549 static int test_errmsg(
3550   void * clientData,
3551   Tcl_Interp *interp,
3552   int objc,
3553   Tcl_Obj *CONST objv[]
3554 ){
3555   sqlite3 *db;
3556   const char *zErr;
3557 
3558   if( objc!=2 ){
3559     Tcl_AppendResult(interp, "wrong # args: should be \"",
3560        Tcl_GetString(objv[0]), " DB", 0);
3561     return TCL_ERROR;
3562   }
3563   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3564 
3565   zErr = sqlite3_errmsg(db);
3566   Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
3567   return TCL_OK;
3568 }
3569 
3570 /*
3571 ** Usage:   test_errmsg16 DB
3572 **
3573 ** Returns the UTF-16 representation of the error message string for the
3574 ** most recent sqlite3_* API call. This is a byte array object at the TCL
3575 ** level, and it includes the 0x00 0x00 terminator bytes at the end of the
3576 ** UTF-16 string.
3577 */
3578 static int test_errmsg16(
3579   void * clientData,
3580   Tcl_Interp *interp,
3581   int objc,
3582   Tcl_Obj *CONST objv[]
3583 ){
3584 #ifndef SQLITE_OMIT_UTF16
3585   sqlite3 *db;
3586   const void *zErr;
3587   const char *z;
3588   int bytes = 0;
3589 
3590   if( objc!=2 ){
3591     Tcl_AppendResult(interp, "wrong # args: should be \"",
3592        Tcl_GetString(objv[0]), " DB", 0);
3593     return TCL_ERROR;
3594   }
3595   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3596 
3597   zErr = sqlite3_errmsg16(db);
3598   if( zErr ){
3599     z = zErr;
3600     for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){}
3601   }
3602   Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
3603 #endif /* SQLITE_OMIT_UTF16 */
3604   return TCL_OK;
3605 }
3606 
3607 /*
3608 ** Usage: sqlite3_prepare DB sql bytes ?tailvar?
3609 **
3610 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3611 ** database handle <DB>. The parameter <tailval> is the name of a global
3612 ** variable that is set to the unused portion of <sql> (if any). A
3613 ** STMT handle is returned.
3614 */
3615 static int test_prepare(
3616   void * clientData,
3617   Tcl_Interp *interp,
3618   int objc,
3619   Tcl_Obj *CONST objv[]
3620 ){
3621   sqlite3 *db;
3622   const char *zSql;
3623   int bytes;
3624   const char *zTail = 0;
3625   sqlite3_stmt *pStmt = 0;
3626   char zBuf[50];
3627   int rc;
3628 
3629   if( objc!=5 && objc!=4 ){
3630     Tcl_AppendResult(interp, "wrong # args: should be \"",
3631        Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
3632     return TCL_ERROR;
3633   }
3634   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3635   zSql = Tcl_GetString(objv[2]);
3636   if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3637 
3638   rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
3639   Tcl_ResetResult(interp);
3640   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3641   if( zTail && objc>=5 ){
3642     if( bytes>=0 ){
3643       bytes = bytes - (int)(zTail-zSql);
3644     }
3645     if( (int)strlen(zTail)<bytes ){
3646       bytes = (int)strlen(zTail);
3647     }
3648     Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3649   }
3650   if( rc!=SQLITE_OK ){
3651     assert( pStmt==0 );
3652     sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
3653     Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3654     return TCL_ERROR;
3655   }
3656 
3657   if( pStmt ){
3658     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3659     Tcl_AppendResult(interp, zBuf, 0);
3660   }
3661   return TCL_OK;
3662 }
3663 
3664 /*
3665 ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar?
3666 **
3667 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3668 ** database handle <DB>. The parameter <tailval> is the name of a global
3669 ** variable that is set to the unused portion of <sql> (if any). A
3670 ** STMT handle is returned.
3671 */
3672 static int test_prepare_v2(
3673   void * clientData,
3674   Tcl_Interp *interp,
3675   int objc,
3676   Tcl_Obj *CONST objv[]
3677 ){
3678   sqlite3 *db;
3679   const char *zSql;
3680   char *zCopy = 0;                /* malloc() copy of zSql */
3681   int bytes;
3682   const char *zTail = 0;
3683   sqlite3_stmt *pStmt = 0;
3684   char zBuf[50];
3685   int rc;
3686 
3687   if( objc!=5 && objc!=4 ){
3688     Tcl_AppendResult(interp, "wrong # args: should be \"",
3689        Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3690     return TCL_ERROR;
3691   }
3692   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3693   zSql = Tcl_GetString(objv[2]);
3694   if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3695 
3696   /* Instead of using zSql directly, make a copy into a buffer obtained
3697   ** directly from malloc(). The idea is to make it easier for valgrind
3698   ** to spot buffer overreads.  */
3699   if( bytes>=0 ){
3700     zCopy = malloc(bytes);
3701     memcpy(zCopy, zSql, bytes);
3702   }else{
3703     int n = (int)strlen(zSql) + 1;
3704     zCopy = malloc(n);
3705     memcpy(zCopy, zSql, n);
3706   }
3707   rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, objc>=5 ? &zTail : 0);
3708   free(zCopy);
3709   zTail = &zSql[(zTail - zCopy)];
3710 
3711   assert(rc==SQLITE_OK || pStmt==0);
3712   Tcl_ResetResult(interp);
3713   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3714   if( rc==SQLITE_OK && zTail && objc>=5 ){
3715     if( bytes>=0 ){
3716       bytes = bytes - (int)(zTail-zSql);
3717     }
3718     Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3719   }
3720   if( rc!=SQLITE_OK ){
3721     assert( pStmt==0 );
3722     sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
3723     Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3724     return TCL_ERROR;
3725   }
3726 
3727   if( pStmt ){
3728     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3729     Tcl_AppendResult(interp, zBuf, 0);
3730   }
3731   return TCL_OK;
3732 }
3733 
3734 /*
3735 ** Usage: sqlite3_prepare_tkt3134 DB
3736 **
3737 ** Generate a prepared statement for a zero-byte string as a test
3738 ** for ticket #3134.  The string should be preceded by a zero byte.
3739 */
3740 static int test_prepare_tkt3134(
3741   void * clientData,
3742   Tcl_Interp *interp,
3743   int objc,
3744   Tcl_Obj *CONST objv[]
3745 ){
3746   sqlite3 *db;
3747   static const char zSql[] = "\000SELECT 1";
3748   sqlite3_stmt *pStmt = 0;
3749   char zBuf[50];
3750   int rc;
3751 
3752   if( objc!=2 ){
3753     Tcl_AppendResult(interp, "wrong # args: should be \"",
3754        Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3755     return TCL_ERROR;
3756   }
3757   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3758   rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0);
3759   assert(rc==SQLITE_OK || pStmt==0);
3760   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3761   if( rc!=SQLITE_OK ){
3762     assert( pStmt==0 );
3763     sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
3764     Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3765     return TCL_ERROR;
3766   }
3767 
3768   if( pStmt ){
3769     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3770     Tcl_AppendResult(interp, zBuf, 0);
3771   }
3772   return TCL_OK;
3773 }
3774 
3775 /*
3776 ** Usage: sqlite3_prepare16 DB sql bytes tailvar
3777 **
3778 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3779 ** database handle <DB>. The parameter <tailval> is the name of a global
3780 ** variable that is set to the unused portion of <sql> (if any). A
3781 ** STMT handle is returned.
3782 */
3783 static int test_prepare16(
3784   void * clientData,
3785   Tcl_Interp *interp,
3786   int objc,
3787   Tcl_Obj *CONST objv[]
3788 ){
3789 #ifndef SQLITE_OMIT_UTF16
3790   sqlite3 *db;
3791   const void *zSql;
3792   const void *zTail = 0;
3793   Tcl_Obj *pTail = 0;
3794   sqlite3_stmt *pStmt = 0;
3795   char zBuf[50];
3796   int rc;
3797   int bytes;                /* The integer specified as arg 3 */
3798   int objlen;               /* The byte-array length of arg 2 */
3799 
3800   if( objc!=5 && objc!=4 ){
3801     Tcl_AppendResult(interp, "wrong # args: should be \"",
3802        Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
3803     return TCL_ERROR;
3804   }
3805   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3806   zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3807   if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3808 
3809   rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
3810   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3811   if( rc ){
3812     return TCL_ERROR;
3813   }
3814 
3815   if( objc>=5 ){
3816     if( zTail ){
3817       objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql);
3818     }else{
3819       objlen = 0;
3820     }
3821     pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3822     Tcl_IncrRefCount(pTail);
3823     Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
3824     Tcl_DecrRefCount(pTail);
3825   }
3826 
3827   if( pStmt ){
3828     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3829   }
3830   Tcl_AppendResult(interp, zBuf, 0);
3831 #endif /* SQLITE_OMIT_UTF16 */
3832   return TCL_OK;
3833 }
3834 
3835 /*
3836 ** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar?
3837 **
3838 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3839 ** database handle <DB>. The parameter <tailval> is the name of a global
3840 ** variable that is set to the unused portion of <sql> (if any). A
3841 ** STMT handle is returned.
3842 */
3843 static int test_prepare16_v2(
3844   void * clientData,
3845   Tcl_Interp *interp,
3846   int objc,
3847   Tcl_Obj *CONST objv[]
3848 ){
3849 #ifndef SQLITE_OMIT_UTF16
3850   sqlite3 *db;
3851   const void *zSql;
3852   const void *zTail = 0;
3853   Tcl_Obj *pTail = 0;
3854   sqlite3_stmt *pStmt = 0;
3855   char zBuf[50];
3856   int rc;
3857   int bytes;                /* The integer specified as arg 3 */
3858   int objlen;               /* The byte-array length of arg 2 */
3859 
3860   if( objc!=5 && objc!=4 ){
3861     Tcl_AppendResult(interp, "wrong # args: should be \"",
3862        Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
3863     return TCL_ERROR;
3864   }
3865   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3866   zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3867   if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3868 
3869   rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
3870   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3871   if( rc ){
3872     return TCL_ERROR;
3873   }
3874 
3875   if( objc>=5 ){
3876     if( zTail ){
3877       objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql);
3878     }else{
3879       objlen = 0;
3880     }
3881     pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3882     Tcl_IncrRefCount(pTail);
3883     Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
3884     Tcl_DecrRefCount(pTail);
3885   }
3886 
3887   if( pStmt ){
3888     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3889   }
3890   Tcl_AppendResult(interp, zBuf, 0);
3891 #endif /* SQLITE_OMIT_UTF16 */
3892   return TCL_OK;
3893 }
3894 
3895 /*
3896 ** Usage: sqlite3_open filename ?options-list?
3897 */
3898 static int test_open(
3899   void * clientData,
3900   Tcl_Interp *interp,
3901   int objc,
3902   Tcl_Obj *CONST objv[]
3903 ){
3904   const char *zFilename;
3905   sqlite3 *db;
3906   char zBuf[100];
3907 
3908   if( objc!=3 && objc!=2 && objc!=1 ){
3909     Tcl_AppendResult(interp, "wrong # args: should be \"",
3910        Tcl_GetString(objv[0]), " filename options-list", 0);
3911     return TCL_ERROR;
3912   }
3913 
3914   zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0;
3915   sqlite3_open(zFilename, &db);
3916 
3917   if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
3918   Tcl_AppendResult(interp, zBuf, 0);
3919   return TCL_OK;
3920 }
3921 
3922 /*
3923 ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS
3924 */
3925 static int test_open_v2(
3926   void * clientData,
3927   Tcl_Interp *interp,
3928   int objc,
3929   Tcl_Obj *CONST objv[]
3930 ){
3931   const char *zFilename;
3932   const char *zVfs;
3933   int flags = 0;
3934   sqlite3 *db;
3935   int rc;
3936   char zBuf[100];
3937 
3938   int nFlag;
3939   Tcl_Obj **apFlag;
3940   int i;
3941 
3942   if( objc!=4 ){
3943     Tcl_WrongNumArgs(interp, 1, objv, "FILENAME FLAGS VFS");
3944     return TCL_ERROR;
3945   }
3946   zFilename = Tcl_GetString(objv[1]);
3947   zVfs = Tcl_GetString(objv[3]);
3948   if( zVfs[0]==0x00 ) zVfs = 0;
3949 
3950   rc = Tcl_ListObjGetElements(interp, objv[2], &nFlag, &apFlag);
3951   if( rc!=TCL_OK ) return rc;
3952   for(i=0; i<nFlag; i++){
3953     int iFlag;
3954     struct OpenFlag {
3955       const char *zFlag;
3956       int flag;
3957     } aFlag[] = {
3958       { "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY },
3959       { "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE },
3960       { "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE },
3961       { "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE },
3962       { "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE },
3963       { "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY },
3964       { "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB },
3965       { "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB },
3966       { "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB },
3967       { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL },
3968       { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL },
3969       { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL },
3970       { "SQLITE_OPEN_MASTER_JOURNAL", SQLITE_OPEN_MASTER_JOURNAL },
3971       { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX },
3972       { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX },
3973       { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE },
3974       { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE },
3975       { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL },
3976       { "SQLITE_OPEN_URI", SQLITE_OPEN_URI },
3977       { 0, 0 }
3978     };
3979     rc = Tcl_GetIndexFromObjStruct(interp, apFlag[i], aFlag, sizeof(aFlag[0]),
3980         "flag", 0, &iFlag
3981     );
3982     if( rc!=TCL_OK ) return rc;
3983     flags |= aFlag[iFlag].flag;
3984   }
3985 
3986   rc = sqlite3_open_v2(zFilename, &db, flags, zVfs);
3987   if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
3988   Tcl_AppendResult(interp, zBuf, 0);
3989   return TCL_OK;
3990 }
3991 
3992 /*
3993 ** Usage: sqlite3_open16 filename options
3994 */
3995 static int test_open16(
3996   void * clientData,
3997   Tcl_Interp *interp,
3998   int objc,
3999   Tcl_Obj *CONST objv[]
4000 ){
4001 #ifndef SQLITE_OMIT_UTF16
4002   const void *zFilename;
4003   sqlite3 *db;
4004   char zBuf[100];
4005 
4006   if( objc!=3 ){
4007     Tcl_AppendResult(interp, "wrong # args: should be \"",
4008        Tcl_GetString(objv[0]), " filename options-list", 0);
4009     return TCL_ERROR;
4010   }
4011 
4012   zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
4013   sqlite3_open16(zFilename, &db);
4014 
4015   if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
4016   Tcl_AppendResult(interp, zBuf, 0);
4017 #endif /* SQLITE_OMIT_UTF16 */
4018   return TCL_OK;
4019 }
4020 
4021 /*
4022 ** Usage: sqlite3_complete16 <UTF-16 string>
4023 **
4024 ** Return 1 if the supplied argument is a complete SQL statement, or zero
4025 ** otherwise.
4026 */
4027 static int test_complete16(
4028   void * clientData,
4029   Tcl_Interp *interp,
4030   int objc,
4031   Tcl_Obj *CONST objv[]
4032 ){
4033 #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
4034   char *zBuf;
4035 
4036   if( objc!=2 ){
4037     Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
4038     return TCL_ERROR;
4039   }
4040 
4041   zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0);
4042   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
4043 #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
4044   return TCL_OK;
4045 }
4046 
4047 /*
4048 ** Usage: sqlite3_step STMT
4049 **
4050 ** Advance the statement to the next row.
4051 */
4052 static int test_step(
4053   void * clientData,
4054   Tcl_Interp *interp,
4055   int objc,
4056   Tcl_Obj *CONST objv[]
4057 ){
4058   sqlite3_stmt *pStmt;
4059   int rc;
4060 
4061   if( objc!=2 ){
4062     Tcl_AppendResult(interp, "wrong # args: should be \"",
4063        Tcl_GetString(objv[0]), " STMT", 0);
4064     return TCL_ERROR;
4065   }
4066 
4067   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4068   rc = sqlite3_step(pStmt);
4069 
4070   /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
4071   Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
4072   return TCL_OK;
4073 }
4074 
4075 static int test_sql(
4076   void * clientData,
4077   Tcl_Interp *interp,
4078   int objc,
4079   Tcl_Obj *CONST objv[]
4080 ){
4081   sqlite3_stmt *pStmt;
4082 
4083   if( objc!=2 ){
4084     Tcl_WrongNumArgs(interp, 1, objv, "STMT");
4085     return TCL_ERROR;
4086   }
4087 
4088   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4089   Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE);
4090   return TCL_OK;
4091 }
4092 
4093 /*
4094 ** Usage: sqlite3_column_count STMT
4095 **
4096 ** Return the number of columns returned by the sql statement STMT.
4097 */
4098 static int test_column_count(
4099   void * clientData,
4100   Tcl_Interp *interp,
4101   int objc,
4102   Tcl_Obj *CONST objv[]
4103 ){
4104   sqlite3_stmt *pStmt;
4105 
4106   if( objc!=2 ){
4107     Tcl_AppendResult(interp, "wrong # args: should be \"",
4108        Tcl_GetString(objv[0]), " STMT column", 0);
4109     return TCL_ERROR;
4110   }
4111 
4112   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4113 
4114   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
4115   return TCL_OK;
4116 }
4117 
4118 /*
4119 ** Usage: sqlite3_column_type STMT column
4120 **
4121 ** Return the type of the data in column 'column' of the current row.
4122 */
4123 static int test_column_type(
4124   void * clientData,
4125   Tcl_Interp *interp,
4126   int objc,
4127   Tcl_Obj *CONST objv[]
4128 ){
4129   sqlite3_stmt *pStmt;
4130   int col;
4131   int tp;
4132 
4133   if( objc!=3 ){
4134     Tcl_AppendResult(interp, "wrong # args: should be \"",
4135        Tcl_GetString(objv[0]), " STMT column", 0);
4136     return TCL_ERROR;
4137   }
4138 
4139   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4140   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4141 
4142   tp = sqlite3_column_type(pStmt, col);
4143   switch( tp ){
4144     case SQLITE_INTEGER:
4145       Tcl_SetResult(interp, "INTEGER", TCL_STATIC);
4146       break;
4147     case SQLITE_NULL:
4148       Tcl_SetResult(interp, "NULL", TCL_STATIC);
4149       break;
4150     case SQLITE_FLOAT:
4151       Tcl_SetResult(interp, "FLOAT", TCL_STATIC);
4152       break;
4153     case SQLITE_TEXT:
4154       Tcl_SetResult(interp, "TEXT", TCL_STATIC);
4155       break;
4156     case SQLITE_BLOB:
4157       Tcl_SetResult(interp, "BLOB", TCL_STATIC);
4158       break;
4159     default:
4160       assert(0);
4161   }
4162 
4163   return TCL_OK;
4164 }
4165 
4166 /*
4167 ** Usage: sqlite3_column_int64 STMT column
4168 **
4169 ** Return the data in column 'column' of the current row cast as an
4170 ** wide (64-bit) integer.
4171 */
4172 static int test_column_int64(
4173   void * clientData,
4174   Tcl_Interp *interp,
4175   int objc,
4176   Tcl_Obj *CONST objv[]
4177 ){
4178   sqlite3_stmt *pStmt;
4179   int col;
4180   i64 iVal;
4181 
4182   if( objc!=3 ){
4183     Tcl_AppendResult(interp, "wrong # args: should be \"",
4184        Tcl_GetString(objv[0]), " STMT column", 0);
4185     return TCL_ERROR;
4186   }
4187 
4188   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4189   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4190 
4191   iVal = sqlite3_column_int64(pStmt, col);
4192   Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
4193   return TCL_OK;
4194 }
4195 
4196 /*
4197 ** Usage: sqlite3_column_blob STMT column
4198 */
4199 static int test_column_blob(
4200   void * clientData,
4201   Tcl_Interp *interp,
4202   int objc,
4203   Tcl_Obj *CONST objv[]
4204 ){
4205   sqlite3_stmt *pStmt;
4206   int col;
4207 
4208   int len;
4209   const void *pBlob;
4210 
4211   if( objc!=3 ){
4212     Tcl_AppendResult(interp, "wrong # args: should be \"",
4213        Tcl_GetString(objv[0]), " STMT column", 0);
4214     return TCL_ERROR;
4215   }
4216 
4217   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4218   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4219 
4220   len = sqlite3_column_bytes(pStmt, col);
4221   pBlob = sqlite3_column_blob(pStmt, col);
4222   Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
4223   return TCL_OK;
4224 }
4225 
4226 /*
4227 ** Usage: sqlite3_column_double STMT column
4228 **
4229 ** Return the data in column 'column' of the current row cast as a double.
4230 */
4231 static int test_column_double(
4232   void * clientData,
4233   Tcl_Interp *interp,
4234   int objc,
4235   Tcl_Obj *CONST objv[]
4236 ){
4237   sqlite3_stmt *pStmt;
4238   int col;
4239   double rVal;
4240 
4241   if( objc!=3 ){
4242     Tcl_AppendResult(interp, "wrong # args: should be \"",
4243        Tcl_GetString(objv[0]), " STMT column", 0);
4244     return TCL_ERROR;
4245   }
4246 
4247   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4248   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4249 
4250   rVal = sqlite3_column_double(pStmt, col);
4251   Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
4252   return TCL_OK;
4253 }
4254 
4255 /*
4256 ** Usage: sqlite3_data_count STMT
4257 **
4258 ** Return the number of columns returned by the sql statement STMT.
4259 */
4260 static int test_data_count(
4261   void * clientData,
4262   Tcl_Interp *interp,
4263   int objc,
4264   Tcl_Obj *CONST objv[]
4265 ){
4266   sqlite3_stmt *pStmt;
4267 
4268   if( objc!=2 ){
4269     Tcl_AppendResult(interp, "wrong # args: should be \"",
4270        Tcl_GetString(objv[0]), " STMT column", 0);
4271     return TCL_ERROR;
4272   }
4273 
4274   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4275 
4276   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
4277   return TCL_OK;
4278 }
4279 
4280 /*
4281 ** Usage: sqlite3_column_text STMT column
4282 **
4283 ** Usage: sqlite3_column_decltype STMT column
4284 **
4285 ** Usage: sqlite3_column_name STMT column
4286 */
4287 static int test_stmt_utf8(
4288   void * clientData,        /* Pointer to SQLite API function to be invoke */
4289   Tcl_Interp *interp,
4290   int objc,
4291   Tcl_Obj *CONST objv[]
4292 ){
4293   sqlite3_stmt *pStmt;
4294   int col;
4295   const char *(*xFunc)(sqlite3_stmt*, int);
4296   const char *zRet;
4297 
4298   xFunc = (const char *(*)(sqlite3_stmt*, int))clientData;
4299   if( objc!=3 ){
4300     Tcl_AppendResult(interp, "wrong # args: should be \"",
4301        Tcl_GetString(objv[0]), " STMT column", 0);
4302     return TCL_ERROR;
4303   }
4304 
4305   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4306   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4307   zRet = xFunc(pStmt, col);
4308   if( zRet ){
4309     Tcl_SetResult(interp, (char *)zRet, 0);
4310   }
4311   return TCL_OK;
4312 }
4313 
4314 static int test_global_recover(
4315   void * clientData,
4316   Tcl_Interp *interp,
4317   int objc,
4318   Tcl_Obj *CONST objv[]
4319 ){
4320 #ifndef SQLITE_OMIT_DEPRECATED
4321   int rc;
4322   if( objc!=1 ){
4323     Tcl_WrongNumArgs(interp, 1, objv, "");
4324     return TCL_ERROR;
4325   }
4326   rc = sqlite3_global_recover();
4327   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
4328 #endif
4329   return TCL_OK;
4330 }
4331 
4332 /*
4333 ** Usage: sqlite3_column_text STMT column
4334 **
4335 ** Usage: sqlite3_column_decltype STMT column
4336 **
4337 ** Usage: sqlite3_column_name STMT column
4338 */
4339 static int test_stmt_utf16(
4340   void * clientData,     /* Pointer to SQLite API function to be invoked */
4341   Tcl_Interp *interp,
4342   int objc,
4343   Tcl_Obj *CONST objv[]
4344 ){
4345 #ifndef SQLITE_OMIT_UTF16
4346   sqlite3_stmt *pStmt;
4347   int col;
4348   Tcl_Obj *pRet;
4349   const void *zName16;
4350   const void *(*xFunc)(sqlite3_stmt*, int);
4351 
4352   xFunc = (const void *(*)(sqlite3_stmt*, int))clientData;
4353   if( objc!=3 ){
4354     Tcl_AppendResult(interp, "wrong # args: should be \"",
4355        Tcl_GetString(objv[0]), " STMT column", 0);
4356     return TCL_ERROR;
4357   }
4358 
4359   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4360   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4361 
4362   zName16 = xFunc(pStmt, col);
4363   if( zName16 ){
4364     int n;
4365     const char *z = zName16;
4366     for(n=0; z[n] || z[n+1]; n+=2){}
4367     pRet = Tcl_NewByteArrayObj(zName16, n+2);
4368     Tcl_SetObjResult(interp, pRet);
4369   }
4370 #endif /* SQLITE_OMIT_UTF16 */
4371 
4372   return TCL_OK;
4373 }
4374 
4375 /*
4376 ** Usage: sqlite3_column_int STMT column
4377 **
4378 ** Usage: sqlite3_column_bytes STMT column
4379 **
4380 ** Usage: sqlite3_column_bytes16 STMT column
4381 **
4382 */
4383 static int test_stmt_int(
4384   void * clientData,    /* Pointer to SQLite API function to be invoked */
4385   Tcl_Interp *interp,
4386   int objc,
4387   Tcl_Obj *CONST objv[]
4388 ){
4389   sqlite3_stmt *pStmt;
4390   int col;
4391   int (*xFunc)(sqlite3_stmt*, int);
4392 
4393   xFunc = (int (*)(sqlite3_stmt*, int))clientData;
4394   if( objc!=3 ){
4395     Tcl_AppendResult(interp, "wrong # args: should be \"",
4396        Tcl_GetString(objv[0]), " STMT column", 0);
4397     return TCL_ERROR;
4398   }
4399 
4400   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
4401   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
4402 
4403   Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
4404   return TCL_OK;
4405 }
4406 
4407 /*
4408 ** Usage:  sqlite_set_magic  DB  MAGIC-NUMBER
4409 **
4410 ** Set the db->magic value.  This is used to test error recovery logic.
4411 */
4412 static int sqlite_set_magic(
4413   void * clientData,
4414   Tcl_Interp *interp,
4415   int argc,
4416   char **argv
4417 ){
4418   sqlite3 *db;
4419   if( argc!=3 ){
4420     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4421          " DB MAGIC", 0);
4422     return TCL_ERROR;
4423   }
4424   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4425   if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){
4426     db->magic = SQLITE_MAGIC_OPEN;
4427   }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){
4428     db->magic = SQLITE_MAGIC_CLOSED;
4429   }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){
4430     db->magic = SQLITE_MAGIC_BUSY;
4431   }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){
4432     db->magic = SQLITE_MAGIC_ERROR;
4433   }else if( Tcl_GetInt(interp, argv[2], (int*)&db->magic) ){
4434     return TCL_ERROR;
4435   }
4436   return TCL_OK;
4437 }
4438 
4439 /*
4440 ** Usage:  sqlite3_interrupt  DB
4441 **
4442 ** Trigger an interrupt on DB
4443 */
4444 static int test_interrupt(
4445   void * clientData,
4446   Tcl_Interp *interp,
4447   int argc,
4448   char **argv
4449 ){
4450   sqlite3 *db;
4451   if( argc!=2 ){
4452     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
4453     return TCL_ERROR;
4454   }
4455   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4456   sqlite3_interrupt(db);
4457   return TCL_OK;
4458 }
4459 
4460 static u8 *sqlite3_stack_baseline = 0;
4461 
4462 /*
4463 ** Fill the stack with a known bitpattern.
4464 */
4465 static void prepStack(void){
4466   int i;
4467   u32 bigBuf[65536];
4468   for(i=0; i<sizeof(bigBuf)/sizeof(bigBuf[0]); i++) bigBuf[i] = 0xdeadbeef;
4469   sqlite3_stack_baseline = (u8*)&bigBuf[65536];
4470 }
4471 
4472 /*
4473 ** Get the current stack depth.  Used for debugging only.
4474 */
4475 u64 sqlite3StackDepth(void){
4476   u8 x;
4477   return (u64)(sqlite3_stack_baseline - &x);
4478 }
4479 
4480 /*
4481 ** Usage:  sqlite3_stack_used DB SQL
4482 **
4483 ** Try to measure the amount of stack space used by a call to sqlite3_exec
4484 */
4485 static int test_stack_used(
4486   void * clientData,
4487   Tcl_Interp *interp,
4488   int argc,
4489   char **argv
4490 ){
4491   sqlite3 *db;
4492   int i;
4493   if( argc!=3 ){
4494     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4495         " DB SQL", 0);
4496     return TCL_ERROR;
4497   }
4498   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4499   prepStack();
4500   (void)sqlite3_exec(db, argv[2], 0, 0, 0);
4501   for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){}
4502   Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4));
4503   return TCL_OK;
4504 }
4505 
4506 /*
4507 ** Usage: sqlite_delete_function DB function-name
4508 **
4509 ** Delete the user function 'function-name' from database handle DB. It
4510 ** is assumed that the user function was created as UTF8, any number of
4511 ** arguments (the way the TCL interface does it).
4512 */
4513 static int delete_function(
4514   void * clientData,
4515   Tcl_Interp *interp,
4516   int argc,
4517   char **argv
4518 ){
4519   int rc;
4520   sqlite3 *db;
4521   if( argc!=3 ){
4522     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4523         " DB function-name", 0);
4524     return TCL_ERROR;
4525   }
4526   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4527   rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
4528   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
4529   return TCL_OK;
4530 }
4531 
4532 /*
4533 ** Usage: sqlite_delete_collation DB collation-name
4534 **
4535 ** Delete the collation sequence 'collation-name' from database handle
4536 ** DB. It is assumed that the collation sequence was created as UTF8 (the
4537 ** way the TCL interface does it).
4538 */
4539 static int delete_collation(
4540   void * clientData,
4541   Tcl_Interp *interp,
4542   int argc,
4543   char **argv
4544 ){
4545   int rc;
4546   sqlite3 *db;
4547   if( argc!=3 ){
4548     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4549         " DB function-name", 0);
4550     return TCL_ERROR;
4551   }
4552   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4553   rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
4554   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
4555   return TCL_OK;
4556 }
4557 
4558 /*
4559 ** Usage: sqlite3_get_autocommit DB
4560 **
4561 ** Return true if the database DB is currently in auto-commit mode.
4562 ** Return false if not.
4563 */
4564 static int get_autocommit(
4565   void * clientData,
4566   Tcl_Interp *interp,
4567   int argc,
4568   char **argv
4569 ){
4570   char zBuf[30];
4571   sqlite3 *db;
4572   if( argc!=2 ){
4573     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4574         " DB", 0);
4575     return TCL_ERROR;
4576   }
4577   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4578   sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", sqlite3_get_autocommit(db));
4579   Tcl_AppendResult(interp, zBuf, 0);
4580   return TCL_OK;
4581 }
4582 
4583 /*
4584 ** Usage: sqlite3_busy_timeout DB MS
4585 **
4586 ** Set the busy timeout.  This is more easily done using the timeout
4587 ** method of the TCL interface.  But we need a way to test the case
4588 ** where it returns SQLITE_MISUSE.
4589 */
4590 static int test_busy_timeout(
4591   void * clientData,
4592   Tcl_Interp *interp,
4593   int argc,
4594   char **argv
4595 ){
4596   int rc, ms;
4597   sqlite3 *db;
4598   if( argc!=3 ){
4599     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4600         " DB", 0);
4601     return TCL_ERROR;
4602   }
4603   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4604   if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
4605   rc = sqlite3_busy_timeout(db, ms);
4606   Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
4607   return TCL_OK;
4608 }
4609 
4610 /*
4611 ** Usage:  tcl_variable_type VARIABLENAME
4612 **
4613 ** Return the name of the internal representation for the
4614 ** value of the given variable.
4615 */
4616 static int tcl_variable_type(
4617   void * clientData,
4618   Tcl_Interp *interp,
4619   int objc,
4620   Tcl_Obj *CONST objv[]
4621 ){
4622   Tcl_Obj *pVar;
4623   if( objc!=2 ){
4624     Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
4625     return TCL_ERROR;
4626   }
4627   pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
4628   if( pVar==0 ) return TCL_ERROR;
4629   if( pVar->typePtr ){
4630     Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
4631   }
4632   return TCL_OK;
4633 }
4634 
4635 /*
4636 ** Usage:  sqlite3_release_memory ?N?
4637 **
4638 ** Attempt to release memory currently held but not actually required.
4639 ** The integer N is the number of bytes we are trying to release.  The
4640 ** return value is the amount of memory actually released.
4641 */
4642 static int test_release_memory(
4643   void * clientData,
4644   Tcl_Interp *interp,
4645   int objc,
4646   Tcl_Obj *CONST objv[]
4647 ){
4648 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
4649   int N;
4650   int amt;
4651   if( objc!=1 && objc!=2 ){
4652     Tcl_WrongNumArgs(interp, 1, objv, "?N?");
4653     return TCL_ERROR;
4654   }
4655   if( objc==2 ){
4656     if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4657   }else{
4658     N = -1;
4659   }
4660   amt = sqlite3_release_memory(N);
4661   Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
4662 #endif
4663   return TCL_OK;
4664 }
4665 
4666 
4667 /*
4668 ** Usage:  sqlite3_db_release_memory DB
4669 **
4670 ** Attempt to release memory currently held by database DB.  Return the
4671 ** result code (which in the current implementation is always zero).
4672 */
4673 static int test_db_release_memory(
4674   void * clientData,
4675   Tcl_Interp *interp,
4676   int objc,
4677   Tcl_Obj *CONST objv[]
4678 ){
4679   sqlite3 *db;
4680   int rc;
4681   if( objc!=2 ){
4682     Tcl_WrongNumArgs(interp, 1, objv, "DB");
4683     return TCL_ERROR;
4684   }
4685   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4686   rc = sqlite3_db_release_memory(db);
4687   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
4688   return TCL_OK;
4689 }
4690 
4691 /*
4692 ** Usage:  sqlite3_db_filename DB DBNAME
4693 **
4694 ** Return the name of a file associated with a database.
4695 */
4696 static int test_db_filename(
4697   void * clientData,
4698   Tcl_Interp *interp,
4699   int objc,
4700   Tcl_Obj *CONST objv[]
4701 ){
4702   sqlite3 *db;
4703   const char *zDbName;
4704   if( objc!=3 ){
4705     Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
4706     return TCL_ERROR;
4707   }
4708   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4709   zDbName = Tcl_GetString(objv[2]);
4710   Tcl_AppendResult(interp, sqlite3_db_filename(db, zDbName), (void*)0);
4711   return TCL_OK;
4712 }
4713 
4714 /*
4715 ** Usage:  sqlite3_db_readonly DB DBNAME
4716 **
4717 ** Return 1 or 0 if DBNAME is readonly or not.  Return -1 if DBNAME does
4718 ** not exist.
4719 */
4720 static int test_db_readonly(
4721   void * clientData,
4722   Tcl_Interp *interp,
4723   int objc,
4724   Tcl_Obj *CONST objv[]
4725 ){
4726   sqlite3 *db;
4727   const char *zDbName;
4728   if( objc!=3 ){
4729     Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
4730     return TCL_ERROR;
4731   }
4732   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4733   zDbName = Tcl_GetString(objv[2]);
4734   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_db_readonly(db, zDbName)));
4735   return TCL_OK;
4736 }
4737 
4738 /*
4739 ** Usage:  sqlite3_soft_heap_limit ?N?
4740 **
4741 ** Query or set the soft heap limit for the current thread.  The
4742 ** limit is only changed if the N is present.  The previous limit
4743 ** is returned.
4744 */
4745 static int test_soft_heap_limit(
4746   void * clientData,
4747   Tcl_Interp *interp,
4748   int objc,
4749   Tcl_Obj *CONST objv[]
4750 ){
4751   sqlite3_int64 amt;
4752   Tcl_WideInt N = -1;
4753   if( objc!=1 && objc!=2 ){
4754     Tcl_WrongNumArgs(interp, 1, objv, "?N?");
4755     return TCL_ERROR;
4756   }
4757   if( objc==2 ){
4758     if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4759   }
4760   amt = sqlite3_soft_heap_limit64(N);
4761   Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt));
4762   return TCL_OK;
4763 }
4764 
4765 /*
4766 ** Usage:   sqlite3_thread_cleanup
4767 **
4768 ** Call the sqlite3_thread_cleanup API.
4769 */
4770 static int test_thread_cleanup(
4771   void * clientData,
4772   Tcl_Interp *interp,
4773   int objc,
4774   Tcl_Obj *CONST objv[]
4775 ){
4776 #ifndef SQLITE_OMIT_DEPRECATED
4777   sqlite3_thread_cleanup();
4778 #endif
4779   return TCL_OK;
4780 }
4781 
4782 /*
4783 ** Usage:   sqlite3_pager_refcounts  DB
4784 **
4785 ** Return a list of numbers which are the PagerRefcount for all
4786 ** pagers on each database connection.
4787 */
4788 static int test_pager_refcounts(
4789   void * clientData,
4790   Tcl_Interp *interp,
4791   int objc,
4792   Tcl_Obj *CONST objv[]
4793 ){
4794   sqlite3 *db;
4795   int i;
4796   int v, *a;
4797   Tcl_Obj *pResult;
4798 
4799   if( objc!=2 ){
4800     Tcl_AppendResult(interp, "wrong # args: should be \"",
4801         Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
4802     return TCL_ERROR;
4803   }
4804   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4805   pResult = Tcl_NewObj();
4806   for(i=0; i<db->nDb; i++){
4807     if( db->aDb[i].pBt==0 ){
4808       v = -1;
4809     }else{
4810       sqlite3_mutex_enter(db->mutex);
4811       a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt));
4812       v = a[0];
4813       sqlite3_mutex_leave(db->mutex);
4814     }
4815     Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v));
4816   }
4817   Tcl_SetObjResult(interp, pResult);
4818   return TCL_OK;
4819 }
4820 
4821 
4822 /*
4823 ** tclcmd:   working_64bit_int
4824 **
4825 ** Some TCL builds (ex: cygwin) do not support 64-bit integers.  This
4826 ** leads to a number of test failures.  The present command checks the
4827 ** TCL build to see whether or not it supports 64-bit integers.  It
4828 ** returns TRUE if it does and FALSE if not.
4829 **
4830 ** This command is used to warn users that their TCL build is defective
4831 ** and that the errors they are seeing in the test scripts might be
4832 ** a result of their defective TCL rather than problems in SQLite.
4833 */
4834 static int working_64bit_int(
4835   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4836   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
4837   int objc,              /* Number of arguments */
4838   Tcl_Obj *CONST objv[]  /* Command arguments */
4839 ){
4840   Tcl_Obj *pTestObj;
4841   int working = 0;
4842 
4843   pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890);
4844   working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0;
4845   Tcl_DecrRefCount(pTestObj);
4846   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working));
4847   return TCL_OK;
4848 }
4849 
4850 
4851 /*
4852 ** tclcmd:   vfs_unlink_test
4853 **
4854 ** This TCL command unregisters the primary VFS and then registers
4855 ** it back again.  This is used to test the ability to register a
4856 ** VFS when none are previously registered, and the ability to
4857 ** unregister the only available VFS.  Ticket #2738
4858 */
4859 static int vfs_unlink_test(
4860   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4861   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
4862   int objc,              /* Number of arguments */
4863   Tcl_Obj *CONST objv[]  /* Command arguments */
4864 ){
4865   int i;
4866   sqlite3_vfs *pMain;
4867   sqlite3_vfs *apVfs[20];
4868   sqlite3_vfs one, two;
4869 
4870   sqlite3_vfs_unregister(0);   /* Unregister of NULL is harmless */
4871   one.zName = "__one";
4872   two.zName = "__two";
4873 
4874   /* Calling sqlite3_vfs_register with 2nd argument of 0 does not
4875   ** change the default VFS
4876   */
4877   pMain = sqlite3_vfs_find(0);
4878   sqlite3_vfs_register(&one, 0);
4879   assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
4880   sqlite3_vfs_register(&two, 0);
4881   assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
4882 
4883   /* We can find a VFS by its name */
4884   assert( sqlite3_vfs_find("__one")==&one );
4885   assert( sqlite3_vfs_find("__two")==&two );
4886 
4887   /* Calling sqlite_vfs_register with non-zero second parameter changes the
4888   ** default VFS, even if the 1st parameter is an existig VFS that is
4889   ** previously registered as the non-default.
4890   */
4891   sqlite3_vfs_register(&one, 1);
4892   assert( sqlite3_vfs_find("__one")==&one );
4893   assert( sqlite3_vfs_find("__two")==&two );
4894   assert( sqlite3_vfs_find(0)==&one );
4895   sqlite3_vfs_register(&two, 1);
4896   assert( sqlite3_vfs_find("__one")==&one );
4897   assert( sqlite3_vfs_find("__two")==&two );
4898   assert( sqlite3_vfs_find(0)==&two );
4899   if( pMain ){
4900     sqlite3_vfs_register(pMain, 1);
4901     assert( sqlite3_vfs_find("__one")==&one );
4902     assert( sqlite3_vfs_find("__two")==&two );
4903     assert( sqlite3_vfs_find(0)==pMain );
4904   }
4905 
4906   /* Unlink the default VFS.  Repeat until there are no more VFSes
4907   ** registered.
4908   */
4909   for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){
4910     apVfs[i] = sqlite3_vfs_find(0);
4911     if( apVfs[i] ){
4912       assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
4913       sqlite3_vfs_unregister(apVfs[i]);
4914       assert( 0==sqlite3_vfs_find(apVfs[i]->zName) );
4915     }
4916   }
4917   assert( 0==sqlite3_vfs_find(0) );
4918 
4919   /* Register the main VFS as non-default (will be made default, since
4920   ** it'll be the only one in existence).
4921   */
4922   sqlite3_vfs_register(pMain, 0);
4923   assert( sqlite3_vfs_find(0)==pMain );
4924 
4925   /* Un-register the main VFS again to restore an empty VFS list */
4926   sqlite3_vfs_unregister(pMain);
4927   assert( 0==sqlite3_vfs_find(0) );
4928 
4929   /* Relink all VFSes in reverse order. */
4930   for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){
4931     if( apVfs[i] ){
4932       sqlite3_vfs_register(apVfs[i], 1);
4933       assert( apVfs[i]==sqlite3_vfs_find(0) );
4934       assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
4935     }
4936   }
4937 
4938   /* Unregister out sample VFSes. */
4939   sqlite3_vfs_unregister(&one);
4940   sqlite3_vfs_unregister(&two);
4941 
4942   /* Unregistering a VFS that is not currently registered is harmless */
4943   sqlite3_vfs_unregister(&one);
4944   sqlite3_vfs_unregister(&two);
4945   assert( sqlite3_vfs_find("__one")==0 );
4946   assert( sqlite3_vfs_find("__two")==0 );
4947 
4948   /* We should be left with the original default VFS back as the
4949   ** original */
4950   assert( sqlite3_vfs_find(0)==pMain );
4951 
4952   return TCL_OK;
4953 }
4954 
4955 /*
4956 ** tclcmd:   vfs_initfail_test
4957 **
4958 ** This TCL command attempts to vfs_find and vfs_register when the
4959 ** sqlite3_initialize() interface is failing.  All calls should fail.
4960 */
4961 static int vfs_initfail_test(
4962   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4963   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
4964   int objc,              /* Number of arguments */
4965   Tcl_Obj *CONST objv[]  /* Command arguments */
4966 ){
4967   sqlite3_vfs one;
4968   one.zName = "__one";
4969 
4970   if( sqlite3_vfs_find(0) ) return TCL_ERROR;
4971   sqlite3_vfs_register(&one, 0);
4972   if( sqlite3_vfs_find(0) ) return TCL_ERROR;
4973   sqlite3_vfs_register(&one, 1);
4974   if( sqlite3_vfs_find(0) ) return TCL_ERROR;
4975   return TCL_OK;
4976 }
4977 
4978 /*
4979 ** Saved VFSes
4980 */
4981 static sqlite3_vfs *apVfs[20];
4982 static int nVfs = 0;
4983 
4984 /*
4985 ** tclcmd:   vfs_unregister_all
4986 **
4987 ** Unregister all VFSes.
4988 */
4989 static int vfs_unregister_all(
4990   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4991   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
4992   int objc,              /* Number of arguments */
4993   Tcl_Obj *CONST objv[]  /* Command arguments */
4994 ){
4995   int i;
4996   for(i=0; i<ArraySize(apVfs); i++){
4997     apVfs[i] = sqlite3_vfs_find(0);
4998     if( apVfs[i]==0 ) break;
4999     sqlite3_vfs_unregister(apVfs[i]);
5000   }
5001   nVfs = i;
5002   return TCL_OK;
5003 }
5004 /*
5005 ** tclcmd:   vfs_reregister_all
5006 **
5007 ** Restore all VFSes that were removed using vfs_unregister_all
5008 */
5009 static int vfs_reregister_all(
5010   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5011   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5012   int objc,              /* Number of arguments */
5013   Tcl_Obj *CONST objv[]  /* Command arguments */
5014 ){
5015   int i;
5016   for(i=0; i<nVfs; i++){
5017     sqlite3_vfs_register(apVfs[i], i==0);
5018   }
5019   return TCL_OK;
5020 }
5021 
5022 
5023 /*
5024 ** tclcmd:   file_control_test DB
5025 **
5026 ** This TCL command runs the sqlite3_file_control interface and
5027 ** verifies correct operation of the same.
5028 */
5029 static int file_control_test(
5030   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5031   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5032   int objc,              /* Number of arguments */
5033   Tcl_Obj *CONST objv[]  /* Command arguments */
5034 ){
5035   int iArg = 0;
5036   sqlite3 *db;
5037   int rc;
5038 
5039   if( objc!=2 ){
5040     Tcl_AppendResult(interp, "wrong # args: should be \"",
5041         Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
5042     return TCL_ERROR;
5043   }
5044   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5045   rc = sqlite3_file_control(db, 0, 0, &iArg);
5046   assert( rc==SQLITE_NOTFOUND );
5047   rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg);
5048   assert( rc==SQLITE_ERROR );
5049   rc = sqlite3_file_control(db, "main", -1, &iArg);
5050   assert( rc==SQLITE_NOTFOUND );
5051   rc = sqlite3_file_control(db, "temp", -1, &iArg);
5052   assert( rc==SQLITE_NOTFOUND || rc==SQLITE_ERROR );
5053 
5054   return TCL_OK;
5055 }
5056 
5057 
5058 /*
5059 ** tclcmd:   file_control_lasterrno_test DB
5060 **
5061 ** This TCL command runs the sqlite3_file_control interface and
5062 ** verifies correct operation of the SQLITE_LAST_ERRNO verb.
5063 */
5064 static int file_control_lasterrno_test(
5065   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5066   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5067   int objc,              /* Number of arguments */
5068   Tcl_Obj *CONST objv[]  /* Command arguments */
5069 ){
5070   int iArg = 0;
5071   sqlite3 *db;
5072   int rc;
5073 
5074   if( objc!=2 ){
5075     Tcl_AppendResult(interp, "wrong # args: should be \"",
5076         Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
5077     return TCL_ERROR;
5078   }
5079   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5080     return TCL_ERROR;
5081   }
5082   rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg);
5083   if( rc ){
5084     Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5085     return TCL_ERROR;
5086   }
5087   if( iArg!=0 ) {
5088     Tcl_AppendResult(interp, "Unexpected non-zero errno: ",
5089                      Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0);
5090     return TCL_ERROR;
5091   }
5092   return TCL_OK;
5093 }
5094 
5095 /*
5096 ** tclcmd:   file_control_chunksize_test DB DBNAME SIZE
5097 **
5098 ** This TCL command runs the sqlite3_file_control interface and
5099 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
5100 ** SQLITE_SET_LOCKPROXYFILE verbs.
5101 */
5102 static int file_control_chunksize_test(
5103   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5104   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5105   int objc,              /* Number of arguments */
5106   Tcl_Obj *CONST objv[]  /* Command arguments */
5107 ){
5108   int nSize;                      /* New chunk size */
5109   char *zDb;                      /* Db name ("main", "temp" etc.) */
5110   sqlite3 *db;                    /* Database handle */
5111   int rc;                         /* file_control() return code */
5112 
5113   if( objc!=4 ){
5114     Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE");
5115     return TCL_ERROR;
5116   }
5117   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
5118    || Tcl_GetIntFromObj(interp, objv[3], &nSize)
5119   ){
5120    return TCL_ERROR;
5121   }
5122   zDb = Tcl_GetString(objv[2]);
5123   if( zDb[0]=='\0' ) zDb = NULL;
5124 
5125   rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_CHUNK_SIZE, (void *)&nSize);
5126   if( rc ){
5127     Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
5128     return TCL_ERROR;
5129   }
5130   return TCL_OK;
5131 }
5132 
5133 /*
5134 ** tclcmd:   file_control_sizehint_test DB DBNAME SIZE
5135 **
5136 ** This TCL command runs the sqlite3_file_control interface
5137 ** with SQLITE_FCNTL_SIZE_HINT
5138 */
5139 static int file_control_sizehint_test(
5140   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5141   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5142   int objc,              /* Number of arguments */
5143   Tcl_Obj *CONST objv[]  /* Command arguments */
5144 ){
5145   Tcl_WideInt nSize;              /* Hinted size */
5146   char *zDb;                      /* Db name ("main", "temp" etc.) */
5147   sqlite3 *db;                    /* Database handle */
5148   int rc;                         /* file_control() return code */
5149 
5150   if( objc!=4 ){
5151     Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE");
5152     return TCL_ERROR;
5153   }
5154   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
5155    || Tcl_GetWideIntFromObj(interp, objv[3], &nSize)
5156   ){
5157    return TCL_ERROR;
5158   }
5159   zDb = Tcl_GetString(objv[2]);
5160   if( zDb[0]=='\0' ) zDb = NULL;
5161 
5162   rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_SIZE_HINT, (void *)&nSize);
5163   if( rc ){
5164     Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
5165     return TCL_ERROR;
5166   }
5167   return TCL_OK;
5168 }
5169 
5170 /*
5171 ** tclcmd:   file_control_lockproxy_test DB PWD
5172 **
5173 ** This TCL command runs the sqlite3_file_control interface and
5174 ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
5175 ** SQLITE_SET_LOCKPROXYFILE verbs.
5176 */
5177 static int file_control_lockproxy_test(
5178   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5179   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5180   int objc,              /* Number of arguments */
5181   Tcl_Obj *CONST objv[]  /* Command arguments */
5182 ){
5183   sqlite3 *db;
5184 
5185   if( objc!=3 ){
5186     Tcl_AppendResult(interp, "wrong # args: should be \"",
5187                      Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0);
5188     return TCL_ERROR;
5189   }
5190   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5191    return TCL_ERROR;
5192   }
5193 
5194 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
5195 #  if defined(__APPLE__)
5196 #    define SQLITE_ENABLE_LOCKING_STYLE 1
5197 #  else
5198 #    define SQLITE_ENABLE_LOCKING_STYLE 0
5199 #  endif
5200 #endif
5201 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
5202   {
5203     char *testPath;
5204     int rc;
5205     int nPwd;
5206     const char *zPwd;
5207     char proxyPath[400];
5208 
5209     zPwd = Tcl_GetStringFromObj(objv[2], &nPwd);
5210     if( sizeof(proxyPath)<nPwd+20 ){
5211       Tcl_AppendResult(interp, "PWD too big", (void*)0);
5212       return TCL_ERROR;
5213     }
5214     sqlite3_snprintf(sizeof(proxyPath), proxyPath, "%s/test.proxy", zPwd);
5215     rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
5216     if( rc ){
5217       Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5218       return TCL_ERROR;
5219     }
5220     rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath);
5221     if( strncmp(proxyPath,testPath,11) ){
5222       Tcl_AppendResult(interp, "Lock proxy file did not match the "
5223                                "previously assigned value", 0);
5224       return TCL_ERROR;
5225     }
5226     if( rc ){
5227       Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5228       return TCL_ERROR;
5229     }
5230     rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
5231     if( rc ){
5232       Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5233       return TCL_ERROR;
5234     }
5235   }
5236 #endif
5237   return TCL_OK;
5238 }
5239 
5240 #if SQLITE_OS_WIN
5241 /*
5242 ** tclcmd:   file_control_win32_av_retry DB  NRETRY  DELAY
5243 **
5244 ** This TCL command runs the sqlite3_file_control interface with
5245 ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode.
5246 */
5247 static int file_control_win32_av_retry(
5248   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5249   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5250   int objc,              /* Number of arguments */
5251   Tcl_Obj *CONST objv[]  /* Command arguments */
5252 ){
5253   sqlite3 *db;
5254   int rc;
5255   int a[2];
5256   char z[100];
5257 
5258   if( objc!=4 ){
5259     Tcl_AppendResult(interp, "wrong # args: should be \"",
5260         Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", 0);
5261     return TCL_ERROR;
5262   }
5263   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5264     return TCL_ERROR;
5265   }
5266   if( Tcl_GetIntFromObj(interp, objv[2], &a[0]) ) return TCL_ERROR;
5267   if( Tcl_GetIntFromObj(interp, objv[3], &a[1]) ) return TCL_ERROR;
5268   rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_AV_RETRY, (void*)a);
5269   sqlite3_snprintf(sizeof(z), z, "%d %d %d", rc, a[0], a[1]);
5270   Tcl_AppendResult(interp, z, (char*)0);
5271   return TCL_OK;
5272 }
5273 
5274 /*
5275 ** tclcmd:   file_control_win32_set_handle DB HANDLE
5276 **
5277 ** This TCL command runs the sqlite3_file_control interface with
5278 ** the SQLITE_FCNTL_WIN32_SET_HANDLE opcode.
5279 */
5280 static int file_control_win32_set_handle(
5281   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5282   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5283   int objc,              /* Number of arguments */
5284   Tcl_Obj *CONST objv[]  /* Command arguments */
5285 ){
5286   sqlite3 *db;
5287   int rc;
5288   HANDLE hFile = NULL;
5289   char z[100];
5290 
5291   if( objc!=3 ){
5292     Tcl_AppendResult(interp, "wrong # args: should be \"",
5293         Tcl_GetStringFromObj(objv[0], 0), " DB HANDLE", 0);
5294     return TCL_ERROR;
5295   }
5296   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5297     return TCL_ERROR;
5298   }
5299   if( getWin32Handle(interp, Tcl_GetString(objv[2]), &hFile) ){
5300     return TCL_ERROR;
5301   }
5302   rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_SET_HANDLE,
5303                             (void*)&hFile);
5304   sqlite3_snprintf(sizeof(z), z, "%d %p", rc, (void*)hFile);
5305   Tcl_AppendResult(interp, z, (char*)0);
5306   return TCL_OK;
5307 }
5308 #endif
5309 
5310 /*
5311 ** tclcmd:   file_control_persist_wal DB PERSIST-FLAG
5312 **
5313 ** This TCL command runs the sqlite3_file_control interface with
5314 ** the SQLITE_FCNTL_PERSIST_WAL opcode.
5315 */
5316 static int file_control_persist_wal(
5317   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5318   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5319   int objc,              /* Number of arguments */
5320   Tcl_Obj *CONST objv[]  /* Command arguments */
5321 ){
5322   sqlite3 *db;
5323   int rc;
5324   int bPersist;
5325   char z[100];
5326 
5327   if( objc!=3 ){
5328     Tcl_AppendResult(interp, "wrong # args: should be \"",
5329         Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0);
5330     return TCL_ERROR;
5331   }
5332   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5333     return TCL_ERROR;
5334   }
5335   if( Tcl_GetIntFromObj(interp, objv[2], &bPersist) ) return TCL_ERROR;
5336   rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_PERSIST_WAL, (void*)&bPersist);
5337   sqlite3_snprintf(sizeof(z), z, "%d %d", rc, bPersist);
5338   Tcl_AppendResult(interp, z, (char*)0);
5339   return TCL_OK;
5340 }
5341 
5342 /*
5343 ** tclcmd:   file_control_powersafe_overwrite DB PSOW-FLAG
5344 **
5345 ** This TCL command runs the sqlite3_file_control interface with
5346 ** the SQLITE_FCNTL_POWERSAFE_OVERWRITE opcode.
5347 */
5348 static int file_control_powersafe_overwrite(
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   sqlite3 *db;
5355   int rc;
5356   int b;
5357   char z[100];
5358 
5359   if( objc!=3 ){
5360     Tcl_AppendResult(interp, "wrong # args: should be \"",
5361         Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0);
5362     return TCL_ERROR;
5363   }
5364   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5365     return TCL_ERROR;
5366   }
5367   if( Tcl_GetIntFromObj(interp, objv[2], &b) ) return TCL_ERROR;
5368   rc = sqlite3_file_control(db,NULL,SQLITE_FCNTL_POWERSAFE_OVERWRITE,(void*)&b);
5369   sqlite3_snprintf(sizeof(z), z, "%d %d", rc, b);
5370   Tcl_AppendResult(interp, z, (char*)0);
5371   return TCL_OK;
5372 }
5373 
5374 
5375 /*
5376 ** tclcmd:   file_control_vfsname DB ?AUXDB?
5377 **
5378 ** Return a string that describes the stack of VFSes.
5379 */
5380 static int file_control_vfsname(
5381   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5382   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5383   int objc,              /* Number of arguments */
5384   Tcl_Obj *CONST objv[]  /* Command arguments */
5385 ){
5386   sqlite3 *db;
5387   const char *zDbName = "main";
5388   char *zVfsName = 0;
5389 
5390   if( objc!=2 && objc!=3 ){
5391     Tcl_AppendResult(interp, "wrong # args: should be \"",
5392         Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
5393     return TCL_ERROR;
5394   }
5395   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5396     return TCL_ERROR;
5397   }
5398   if( objc==3 ){
5399     zDbName = Tcl_GetString(objv[2]);
5400   }
5401   sqlite3_file_control(db, zDbName, SQLITE_FCNTL_VFSNAME,(void*)&zVfsName);
5402   Tcl_AppendResult(interp, zVfsName, (char*)0);
5403   sqlite3_free(zVfsName);
5404   return TCL_OK;
5405 }
5406 
5407 /*
5408 ** tclcmd:   file_control_tempfilename DB ?AUXDB?
5409 **
5410 ** Return a string that is a temporary filename
5411 */
5412 static int file_control_tempfilename(
5413   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5414   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5415   int objc,              /* Number of arguments */
5416   Tcl_Obj *CONST objv[]  /* Command arguments */
5417 ){
5418   sqlite3 *db;
5419   const char *zDbName = "main";
5420   char *zTName = 0;
5421 
5422   if( objc!=2 && objc!=3 ){
5423     Tcl_AppendResult(interp, "wrong # args: should be \"",
5424         Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
5425     return TCL_ERROR;
5426   }
5427   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5428     return TCL_ERROR;
5429   }
5430   if( objc==3 ){
5431     zDbName = Tcl_GetString(objv[2]);
5432   }
5433   sqlite3_file_control(db, zDbName, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTName);
5434   Tcl_AppendResult(interp, zTName, (char*)0);
5435   sqlite3_free(zTName);
5436   return TCL_OK;
5437 }
5438 
5439 
5440 /*
5441 ** tclcmd:   sqlite3_vfs_list
5442 **
5443 **   Return a tcl list containing the names of all registered vfs's.
5444 */
5445 static int vfs_list(
5446   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5447   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5448   int objc,              /* Number of arguments */
5449   Tcl_Obj *CONST objv[]  /* Command arguments */
5450 ){
5451   sqlite3_vfs *pVfs;
5452   Tcl_Obj *pRet = Tcl_NewObj();
5453   if( objc!=1 ){
5454     Tcl_WrongNumArgs(interp, 1, objv, "");
5455     return TCL_ERROR;
5456   }
5457   for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
5458     Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1));
5459   }
5460   Tcl_SetObjResult(interp, pRet);
5461   return TCL_OK;
5462 }
5463 
5464 /*
5465 ** tclcmd:   sqlite3_limit DB ID VALUE
5466 **
5467 ** This TCL command runs the sqlite3_limit interface and
5468 ** verifies correct operation of the same.
5469 */
5470 static int test_limit(
5471   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5472   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5473   int objc,              /* Number of arguments */
5474   Tcl_Obj *CONST objv[]  /* Command arguments */
5475 ){
5476   sqlite3 *db;
5477   int rc;
5478   static const struct {
5479      char *zName;
5480      int id;
5481   } aId[] = {
5482     { "SQLITE_LIMIT_LENGTH",              SQLITE_LIMIT_LENGTH               },
5483     { "SQLITE_LIMIT_SQL_LENGTH",          SQLITE_LIMIT_SQL_LENGTH           },
5484     { "SQLITE_LIMIT_COLUMN",              SQLITE_LIMIT_COLUMN               },
5485     { "SQLITE_LIMIT_EXPR_DEPTH",          SQLITE_LIMIT_EXPR_DEPTH           },
5486     { "SQLITE_LIMIT_COMPOUND_SELECT",     SQLITE_LIMIT_COMPOUND_SELECT      },
5487     { "SQLITE_LIMIT_VDBE_OP",             SQLITE_LIMIT_VDBE_OP              },
5488     { "SQLITE_LIMIT_FUNCTION_ARG",        SQLITE_LIMIT_FUNCTION_ARG         },
5489     { "SQLITE_LIMIT_ATTACHED",            SQLITE_LIMIT_ATTACHED             },
5490     { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH  },
5491     { "SQLITE_LIMIT_VARIABLE_NUMBER",     SQLITE_LIMIT_VARIABLE_NUMBER      },
5492     { "SQLITE_LIMIT_TRIGGER_DEPTH",       SQLITE_LIMIT_TRIGGER_DEPTH        },
5493     { "SQLITE_LIMIT_WORKER_THREADS",      SQLITE_LIMIT_WORKER_THREADS       },
5494 
5495     /* Out of range test cases */
5496     { "SQLITE_LIMIT_TOOSMALL",            -1,                               },
5497     { "SQLITE_LIMIT_TOOBIG",              SQLITE_LIMIT_WORKER_THREADS+1     },
5498   };
5499   int i, id = 0;
5500   int val;
5501   const char *zId;
5502 
5503   if( objc!=4 ){
5504     Tcl_AppendResult(interp, "wrong # args: should be \"",
5505         Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0);
5506     return TCL_ERROR;
5507   }
5508   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
5509   zId = Tcl_GetString(objv[2]);
5510   for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){
5511     if( strcmp(zId, aId[i].zName)==0 ){
5512       id = aId[i].id;
5513       break;
5514     }
5515   }
5516   if( i>=sizeof(aId)/sizeof(aId[0]) ){
5517     Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0);
5518     return TCL_ERROR;
5519   }
5520   if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
5521   rc = sqlite3_limit(db, id, val);
5522   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
5523   return TCL_OK;
5524 }
5525 
5526 /*
5527 ** tclcmd:  save_prng_state
5528 **
5529 ** Save the state of the pseudo-random number generator.
5530 ** At the same time, verify that sqlite3_test_control works even when
5531 ** called with an out-of-range opcode.
5532 */
5533 static int save_prng_state(
5534   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5535   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5536   int objc,              /* Number of arguments */
5537   Tcl_Obj *CONST objv[]  /* Command arguments */
5538 ){
5539   int rc = sqlite3_test_control(9999);
5540   assert( rc==0 );
5541   rc = sqlite3_test_control(-1);
5542   assert( rc==0 );
5543   sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE);
5544   return TCL_OK;
5545 }
5546 /*
5547 ** tclcmd:  restore_prng_state
5548 */
5549 static int restore_prng_state(
5550   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5551   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5552   int objc,              /* Number of arguments */
5553   Tcl_Obj *CONST objv[]  /* Command arguments */
5554 ){
5555   sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE);
5556   return TCL_OK;
5557 }
5558 /*
5559 ** tclcmd:  reset_prng_state
5560 */
5561 static int reset_prng_state(
5562   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5563   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5564   int objc,              /* Number of arguments */
5565   Tcl_Obj *CONST objv[]  /* Command arguments */
5566 ){
5567   sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET);
5568   return TCL_OK;
5569 }
5570 
5571 /*
5572 ** tclcmd:  database_may_be_corrupt
5573 **
5574 ** Indicate that database files might be corrupt.  In other words, set the normal
5575 ** state of operation.
5576 */
5577 static int database_may_be_corrupt(
5578   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5579   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5580   int objc,              /* Number of arguments */
5581   Tcl_Obj *CONST objv[]  /* Command arguments */
5582 ){
5583   sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 0);
5584   return TCL_OK;
5585 }
5586 /*
5587 ** tclcmd:  database_never_corrupt
5588 **
5589 ** Indicate that database files are always well-formed.  This enables extra assert()
5590 ** statements that test conditions that are always true for well-formed databases.
5591 */
5592 static int database_never_corrupt(
5593   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5594   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5595   int objc,              /* Number of arguments */
5596   Tcl_Obj *CONST objv[]  /* Command arguments */
5597 ){
5598   sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 1);
5599   return TCL_OK;
5600 }
5601 
5602 /*
5603 ** tclcmd:  pcache_stats
5604 */
5605 static int test_pcache_stats(
5606   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
5607   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5608   int objc,              /* Number of arguments */
5609   Tcl_Obj *CONST objv[]  /* Command arguments */
5610 ){
5611   int nMin;
5612   int nMax;
5613   int nCurrent;
5614   int nRecyclable;
5615   Tcl_Obj *pRet;
5616 
5617   sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable);
5618 
5619   pRet = Tcl_NewObj();
5620   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1));
5621   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent));
5622   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1));
5623   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax));
5624   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1));
5625   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin));
5626   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1));
5627   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable));
5628 
5629   Tcl_SetObjResult(interp, pRet);
5630 
5631   return TCL_OK;
5632 }
5633 
5634 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
5635 static void test_unlock_notify_cb(void **aArg, int nArg){
5636   int ii;
5637   for(ii=0; ii<nArg; ii++){
5638     Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL);
5639   }
5640 }
5641 #endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */
5642 
5643 /*
5644 ** tclcmd:  sqlite3_unlock_notify db
5645 */
5646 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
5647 static int test_unlock_notify(
5648   ClientData clientData, /* Unused */
5649   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5650   int objc,              /* Number of arguments */
5651   Tcl_Obj *CONST objv[]  /* Command arguments */
5652 ){
5653   sqlite3 *db;
5654   int rc;
5655 
5656   if( objc!=2 ){
5657     Tcl_WrongNumArgs(interp, 1, objv, "DB");
5658     return TCL_ERROR;
5659   }
5660 
5661   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5662     return TCL_ERROR;
5663   }
5664   rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp);
5665   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
5666   return TCL_OK;
5667 }
5668 #endif
5669 
5670 /*
5671 ** tclcmd:  sqlite3_wal_checkpoint db ?NAME?
5672 */
5673 static int test_wal_checkpoint(
5674   ClientData clientData, /* Unused */
5675   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5676   int objc,              /* Number of arguments */
5677   Tcl_Obj *CONST objv[]  /* Command arguments */
5678 ){
5679   char *zDb = 0;
5680   sqlite3 *db;
5681   int rc;
5682 
5683   if( objc!=3 && objc!=2 ){
5684     Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?");
5685     return TCL_ERROR;
5686   }
5687 
5688   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
5689     return TCL_ERROR;
5690   }
5691   if( objc==3 ){
5692     zDb = Tcl_GetString(objv[2]);
5693   }
5694   rc = sqlite3_wal_checkpoint(db, zDb);
5695   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
5696   return TCL_OK;
5697 }
5698 
5699 /*
5700 ** tclcmd:  sqlite3_wal_checkpoint_v2 db MODE ?NAME?
5701 **
5702 ** This command calls the wal_checkpoint_v2() function with the specified
5703 ** mode argument (passive, full or restart). If present, the database name
5704 ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the
5705 ** NAME argument is not present, a NULL pointer is passed instead.
5706 **
5707 ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or
5708 ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set
5709 ** to the error message obtained from sqlite3_errmsg().
5710 **
5711 ** Otherwise, this command returns a list of three integers. The first integer
5712 ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers
5713 ** are the values returned via the output parameters by wal_checkpoint_v2() -
5714 ** the number of frames in the log and the number of frames in the log
5715 ** that have been checkpointed.
5716 */
5717 static int test_wal_checkpoint_v2(
5718   ClientData clientData, /* Unused */
5719   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5720   int objc,              /* Number of arguments */
5721   Tcl_Obj *CONST objv[]  /* Command arguments */
5722 ){
5723   char *zDb = 0;
5724   sqlite3 *db;
5725   int rc;
5726 
5727   int eMode;
5728   int nLog = -555;
5729   int nCkpt = -555;
5730   Tcl_Obj *pRet;
5731 
5732   const char * aMode[] = { "passive", "full", "restart", "truncate", 0 };
5733   assert( SQLITE_CHECKPOINT_PASSIVE==0 );
5734   assert( SQLITE_CHECKPOINT_FULL==1 );
5735   assert( SQLITE_CHECKPOINT_RESTART==2 );
5736   assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
5737 
5738   if( objc!=3 && objc!=4 ){
5739     Tcl_WrongNumArgs(interp, 1, objv, "DB MODE ?NAME?");
5740     return TCL_ERROR;
5741   }
5742 
5743   if( objc==4 ){
5744     zDb = Tcl_GetString(objv[3]);
5745   }
5746   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) || (
5747       TCL_OK!=Tcl_GetIntFromObj(0, objv[2], &eMode)
5748    && TCL_OK!=Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0, &eMode)
5749   )){
5750     return TCL_ERROR;
5751   }
5752 
5753   rc = sqlite3_wal_checkpoint_v2(db, zDb, eMode, &nLog, &nCkpt);
5754   if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
5755     const char *zErrCode = sqlite3ErrName(rc);
5756     Tcl_ResetResult(interp);
5757     Tcl_AppendResult(interp, zErrCode, " - ", (char *)sqlite3_errmsg(db), 0);
5758     return TCL_ERROR;
5759   }
5760 
5761   pRet = Tcl_NewObj();
5762   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc==SQLITE_BUSY?1:0));
5763   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nLog));
5764   Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCkpt));
5765   Tcl_SetObjResult(interp, pRet);
5766 
5767   return TCL_OK;
5768 }
5769 
5770 /*
5771 ** tclcmd:  sqlite3_wal_autocheckpoint db VALUE
5772 */
5773 static int test_wal_autocheckpoint(
5774   ClientData clientData, /* Unused */
5775   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5776   int objc,              /* Number of arguments */
5777   Tcl_Obj *CONST objv[]  /* Command arguments */
5778 ){
5779   sqlite3 *db;
5780   int rc;
5781   int iVal;
5782 
5783 
5784   if( objc!=3 ){
5785     Tcl_WrongNumArgs(interp, 1, objv, "DB VALUE");
5786     return TCL_ERROR;
5787   }
5788 
5789   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
5790    || Tcl_GetIntFromObj(0, objv[2], &iVal)
5791   ){
5792     return TCL_ERROR;
5793   }
5794 
5795   rc = sqlite3_wal_autocheckpoint(db, iVal);
5796   Tcl_ResetResult(interp);
5797   if( rc!=SQLITE_OK ){
5798     const char *zErrCode = sqlite3ErrName(rc);
5799     Tcl_SetObjResult(interp, Tcl_NewStringObj(zErrCode, -1));
5800     return TCL_ERROR;
5801   }
5802 
5803   return TCL_OK;
5804 }
5805 
5806 
5807 /*
5808 ** tclcmd:  test_sqlite3_log ?SCRIPT?
5809 */
5810 static struct LogCallback {
5811   Tcl_Interp *pInterp;
5812   Tcl_Obj *pObj;
5813 } logcallback = {0, 0};
5814 static void xLogcallback(void *unused, int err, char *zMsg){
5815   Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj);
5816   Tcl_IncrRefCount(pNew);
5817   Tcl_ListObjAppendElement(
5818       0, pNew, Tcl_NewStringObj(sqlite3ErrName(err), -1)
5819   );
5820   Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zMsg, -1));
5821   Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT);
5822   Tcl_DecrRefCount(pNew);
5823 }
5824 static int test_sqlite3_log(
5825   ClientData clientData,
5826   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5827   int objc,              /* Number of arguments */
5828   Tcl_Obj *CONST objv[]  /* Command arguments */
5829 ){
5830   if( objc>2 ){
5831     Tcl_WrongNumArgs(interp, 1, objv, "SCRIPT");
5832     return TCL_ERROR;
5833   }
5834   if( logcallback.pObj ){
5835     Tcl_DecrRefCount(logcallback.pObj);
5836     logcallback.pObj = 0;
5837     logcallback.pInterp = 0;
5838     sqlite3_config(SQLITE_CONFIG_LOG, 0, 0);
5839   }
5840   if( objc>1 ){
5841     logcallback.pObj = objv[1];
5842     Tcl_IncrRefCount(logcallback.pObj);
5843     logcallback.pInterp = interp;
5844     sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, 0);
5845   }
5846   return TCL_OK;
5847 }
5848 
5849 /*
5850 **     tcl_objproc COMMANDNAME ARGS...
5851 **
5852 ** Run a TCL command using its objProc interface.  Throw an error if
5853 ** the command has no objProc interface.
5854 */
5855 static int runAsObjProc(
5856   void * clientData,
5857   Tcl_Interp *interp,
5858   int objc,
5859   Tcl_Obj *CONST objv[]
5860 ){
5861   Tcl_CmdInfo cmdInfo;
5862   if( objc<2 ){
5863     Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ...");
5864     return TCL_ERROR;
5865   }
5866   if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
5867     Tcl_AppendResult(interp, "command not found: ",
5868            Tcl_GetString(objv[1]), (char*)0);
5869     return TCL_ERROR;
5870   }
5871   if( cmdInfo.objProc==0 ){
5872     Tcl_AppendResult(interp, "command has no objProc: ",
5873            Tcl_GetString(objv[1]), (char*)0);
5874     return TCL_ERROR;
5875   }
5876   return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1);
5877 }
5878 
5879 #ifndef SQLITE_OMIT_EXPLAIN
5880 /*
5881 ** WARNING: The following function, printExplainQueryPlan() is an exact
5882 ** copy of example code from eqp.in (eqp.html). If this code is modified,
5883 ** then the documentation copy needs to be modified as well.
5884 */
5885 /*
5886 ** Argument pStmt is a prepared SQL statement. This function compiles
5887 ** an EXPLAIN QUERY PLAN command to report on the prepared statement,
5888 ** and prints the report to stdout using printf().
5889 */
5890 int printExplainQueryPlan(sqlite3_stmt *pStmt){
5891   const char *zSql;               /* Input SQL */
5892   char *zExplain;                 /* SQL with EXPLAIN QUERY PLAN prepended */
5893   sqlite3_stmt *pExplain;         /* Compiled EXPLAIN QUERY PLAN command */
5894   int rc;                         /* Return code from sqlite3_prepare_v2() */
5895 
5896   zSql = sqlite3_sql(pStmt);
5897   if( zSql==0 ) return SQLITE_ERROR;
5898 
5899   zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql);
5900   if( zExplain==0 ) return SQLITE_NOMEM;
5901 
5902   rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, 0);
5903   sqlite3_free(zExplain);
5904   if( rc!=SQLITE_OK ) return rc;
5905 
5906   while( SQLITE_ROW==sqlite3_step(pExplain) ){
5907     int iSelectid = sqlite3_column_int(pExplain, 0);
5908     int iOrder = sqlite3_column_int(pExplain, 1);
5909     int iFrom = sqlite3_column_int(pExplain, 2);
5910     const char *zDetail = (const char *)sqlite3_column_text(pExplain, 3);
5911 
5912     printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail);
5913   }
5914 
5915   return sqlite3_finalize(pExplain);
5916 }
5917 
5918 static int test_print_eqp(
5919   void * clientData,
5920   Tcl_Interp *interp,
5921   int objc,
5922   Tcl_Obj *CONST objv[]
5923 ){
5924   int rc;
5925   sqlite3_stmt *pStmt;
5926 
5927   if( objc!=2 ){
5928     Tcl_WrongNumArgs(interp, 1, objv, "STMT");
5929     return TCL_ERROR;
5930   }
5931   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
5932   rc = printExplainQueryPlan(pStmt);
5933   /* This is needed on Windows so that a test case using this
5934   ** function can open a read pipe and get the output of
5935   ** printExplainQueryPlan() immediately.
5936   */
5937   fflush(stdout);
5938   Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
5939   return TCL_OK;
5940 }
5941 #endif /* SQLITE_OMIT_EXPLAIN */
5942 
5943 /*
5944 ** sqlite3_test_control VERB ARGS...
5945 */
5946 static int test_test_control(
5947   void * clientData,
5948   Tcl_Interp *interp,
5949   int objc,
5950   Tcl_Obj *CONST objv[]
5951 ){
5952   struct Verb {
5953     const char *zName;
5954     int i;
5955   } aVerb[] = {
5956     { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT },
5957     { "SQLITE_TESTCTRL_SORTER_MMAP",     SQLITE_TESTCTRL_SORTER_MMAP     },
5958     { "SQLITE_TESTCTRL_IMPOSTER",        SQLITE_TESTCTRL_IMPOSTER        },
5959   };
5960   int iVerb;
5961   int iFlag;
5962   int rc;
5963 
5964   if( objc<2 ){
5965     Tcl_WrongNumArgs(interp, 1, objv, "VERB ARGS...");
5966     return TCL_ERROR;
5967   }
5968 
5969   rc = Tcl_GetIndexFromObjStruct(
5970       interp, objv[1], aVerb, sizeof(aVerb[0]), "VERB", 0, &iVerb
5971   );
5972   if( rc!=TCL_OK ) return rc;
5973 
5974   iFlag = aVerb[iVerb].i;
5975   switch( iFlag ){
5976     case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
5977       int val;
5978       if( objc!=3 ){
5979         Tcl_WrongNumArgs(interp, 2, objv, "ONOFF");
5980         return TCL_ERROR;
5981       }
5982       if( Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
5983       sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, val);
5984       break;
5985     }
5986 
5987     case SQLITE_TESTCTRL_SORTER_MMAP: {
5988       int val;
5989       sqlite3 *db;
5990       if( objc!=4 ){
5991         Tcl_WrongNumArgs(interp, 2, objv, "DB LIMIT");
5992         return TCL_ERROR;
5993       }
5994       if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
5995       if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
5996       sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, val);
5997       break;
5998     }
5999 
6000     case SQLITE_TESTCTRL_IMPOSTER: {
6001       int onOff, tnum;
6002       const char *zDbName;
6003       sqlite3 *db;
6004       if( objc!=6 ){
6005         Tcl_WrongNumArgs(interp, 2, objv, "DB dbName onOff tnum");
6006         return TCL_ERROR;
6007       }
6008       if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR;
6009       zDbName = Tcl_GetString(objv[3]);
6010       if( Tcl_GetIntFromObj(interp, objv[4], &onOff) ) return TCL_ERROR;
6011       if( Tcl_GetIntFromObj(interp, objv[5], &tnum) ) return TCL_ERROR;
6012       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, zDbName, onOff, tnum);
6013       break;
6014     }
6015   }
6016 
6017   Tcl_ResetResult(interp);
6018   return TCL_OK;
6019 }
6020 
6021 #if SQLITE_OS_UNIX
6022 #include <sys/time.h>
6023 #include <sys/resource.h>
6024 
6025 static int test_getrusage(
6026   void * clientData,
6027   Tcl_Interp *interp,
6028   int objc,
6029   Tcl_Obj *CONST objv[]
6030 ){
6031   char buf[1024];
6032   struct rusage r;
6033   memset(&r, 0, sizeof(r));
6034   getrusage(RUSAGE_SELF, &r);
6035 
6036   sqlite3_snprintf(sizeof(buf), buf,
6037     "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d",
6038     (int)r.ru_utime.tv_sec, (int)r.ru_utime.tv_usec,
6039     (int)r.ru_stime.tv_sec, (int)r.ru_stime.tv_usec,
6040     (int)r.ru_minflt, (int)r.ru_majflt
6041   );
6042   Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1));
6043   return TCL_OK;
6044 }
6045 #endif
6046 
6047 #if SQLITE_OS_WIN
6048 /*
6049 ** Information passed from the main thread into the windows file locker
6050 ** background thread.
6051 */
6052 struct win32FileLocker {
6053   char *evName;       /* Name of event to signal thread startup */
6054   HANDLE h;           /* Handle of the file to be locked */
6055   int delay1;         /* Delay before locking */
6056   int delay2;         /* Delay before unlocking */
6057   int ok;             /* Finished ok */
6058   int err;            /* True if an error occurs */
6059 };
6060 #endif
6061 
6062 
6063 #if SQLITE_OS_WIN
6064 #include <process.h>
6065 /*
6066 ** The background thread that does file locking.
6067 */
6068 static void win32_file_locker(void *pAppData){
6069   struct win32FileLocker *p = (struct win32FileLocker*)pAppData;
6070   if( p->evName ){
6071     HANDLE ev = OpenEvent(EVENT_MODIFY_STATE, FALSE, p->evName);
6072     if ( ev ){
6073       SetEvent(ev);
6074       CloseHandle(ev);
6075     }
6076   }
6077   if( p->delay1 ) Sleep(p->delay1);
6078   if( LockFile(p->h, 0, 0, 100000000, 0) ){
6079     Sleep(p->delay2);
6080     UnlockFile(p->h, 0, 0, 100000000, 0);
6081     p->ok = 1;
6082   }else{
6083     p->err = 1;
6084   }
6085   CloseHandle(p->h);
6086   p->h = 0;
6087   p->delay1 = 0;
6088   p->delay2 = 0;
6089 }
6090 #endif
6091 
6092 #if SQLITE_OS_WIN
6093 /*
6094 **      lock_win32_file FILENAME DELAY1 DELAY2
6095 **
6096 ** Get an exclusive manditory lock on file for DELAY2 milliseconds.
6097 ** Wait DELAY1 milliseconds before acquiring the lock.
6098 */
6099 static int win32_file_lock(
6100   void * clientData,
6101   Tcl_Interp *interp,
6102   int objc,
6103   Tcl_Obj *CONST objv[]
6104 ){
6105   static struct win32FileLocker x = { "win32_file_lock", 0, 0, 0, 0, 0 };
6106   const char *zFilename;
6107   char zBuf[200];
6108   int retry = 0;
6109   HANDLE ev;
6110   DWORD wResult;
6111 
6112   if( objc!=4 && objc!=1 ){
6113     Tcl_WrongNumArgs(interp, 1, objv, "FILENAME DELAY1 DELAY2");
6114     return TCL_ERROR;
6115   }
6116   if( objc==1 ){
6117     sqlite3_snprintf(sizeof(zBuf), zBuf, "%d %d %d %d %d",
6118                      x.ok, x.err, x.delay1, x.delay2, x.h);
6119     Tcl_AppendResult(interp, zBuf, (char*)0);
6120     return TCL_OK;
6121   }
6122   while( x.h && retry<30 ){
6123     retry++;
6124     Sleep(100);
6125   }
6126   if( x.h ){
6127     Tcl_AppendResult(interp, "busy", (char*)0);
6128     return TCL_ERROR;
6129   }
6130   if( Tcl_GetIntFromObj(interp, objv[2], &x.delay1) ) return TCL_ERROR;
6131   if( Tcl_GetIntFromObj(interp, objv[3], &x.delay2) ) return TCL_ERROR;
6132   zFilename = Tcl_GetString(objv[1]);
6133   x.h = CreateFile(zFilename, GENERIC_READ|GENERIC_WRITE,
6134               FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS,
6135               FILE_ATTRIBUTE_NORMAL, 0);
6136   if( !x.h ){
6137     Tcl_AppendResult(interp, "cannot open file: ", zFilename, (char*)0);
6138     return TCL_ERROR;
6139   }
6140   ev = CreateEvent(NULL, TRUE, FALSE, x.evName);
6141   if ( !ev ){
6142     Tcl_AppendResult(interp, "cannot create event: ", x.evName, (char*)0);
6143     return TCL_ERROR;
6144   }
6145   _beginthread(win32_file_locker, 0, (void*)&x);
6146   Sleep(0);
6147   if ( (wResult = WaitForSingleObject(ev, 10000))!=WAIT_OBJECT_0 ){
6148     sqlite3_snprintf(sizeof(zBuf), zBuf, "0x%x", wResult);
6149     Tcl_AppendResult(interp, "wait failed: ", zBuf, (char*)0);
6150     CloseHandle(ev);
6151     return TCL_ERROR;
6152   }
6153   CloseHandle(ev);
6154   return TCL_OK;
6155 }
6156 
6157 /*
6158 **      exists_win32_path PATH
6159 **
6160 ** Returns non-zero if the specified path exists, whose fully qualified name
6161 ** may exceed 260 characters if it is prefixed with "\\?\".
6162 */
6163 static int win32_exists_path(
6164   void *clientData,
6165   Tcl_Interp *interp,
6166   int objc,
6167   Tcl_Obj *CONST objv[]
6168 ){
6169   if( objc!=2 ){
6170     Tcl_WrongNumArgs(interp, 1, objv, "PATH");
6171     return TCL_ERROR;
6172   }
6173   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(
6174       GetFileAttributesW( Tcl_GetUnicode(objv[1]))!=INVALID_FILE_ATTRIBUTES ));
6175   return TCL_OK;
6176 }
6177 
6178 /*
6179 **      find_win32_file PATTERN
6180 **
6181 ** Returns a list of entries in a directory that match the specified pattern,
6182 ** whose fully qualified name may exceed 248 characters if it is prefixed with
6183 ** "\\?\".
6184 */
6185 static int win32_find_file(
6186   void *clientData,
6187   Tcl_Interp *interp,
6188   int objc,
6189   Tcl_Obj *CONST objv[]
6190 ){
6191   HANDLE hFindFile = INVALID_HANDLE_VALUE;
6192   WIN32_FIND_DATAW findData;
6193   Tcl_Obj *listObj;
6194   DWORD lastErrno;
6195   if( objc!=2 ){
6196     Tcl_WrongNumArgs(interp, 1, objv, "PATTERN");
6197     return TCL_ERROR;
6198   }
6199   hFindFile = FindFirstFileW(Tcl_GetUnicode(objv[1]), &findData);
6200   if( hFindFile==INVALID_HANDLE_VALUE ){
6201     Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
6202     return TCL_ERROR;
6203   }
6204   listObj = Tcl_NewObj();
6205   Tcl_IncrRefCount(listObj);
6206   do {
6207     Tcl_ListObjAppendElement(interp, listObj, Tcl_NewUnicodeObj(
6208         findData.cFileName, -1));
6209     Tcl_ListObjAppendElement(interp, listObj, Tcl_NewWideIntObj(
6210         findData.dwFileAttributes));
6211   } while( FindNextFileW(hFindFile, &findData) );
6212   lastErrno = GetLastError();
6213   if( lastErrno!=NO_ERROR && lastErrno!=ERROR_NO_MORE_FILES ){
6214     FindClose(hFindFile);
6215     Tcl_DecrRefCount(listObj);
6216     Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
6217     return TCL_ERROR;
6218   }
6219   FindClose(hFindFile);
6220   Tcl_SetObjResult(interp, listObj);
6221   return TCL_OK;
6222 }
6223 
6224 /*
6225 **      delete_win32_file FILENAME
6226 **
6227 ** Deletes the specified file, whose fully qualified name may exceed 260
6228 ** characters if it is prefixed with "\\?\".
6229 */
6230 static int win32_delete_file(
6231   void *clientData,
6232   Tcl_Interp *interp,
6233   int objc,
6234   Tcl_Obj *CONST objv[]
6235 ){
6236   if( objc!=2 ){
6237     Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
6238     return TCL_ERROR;
6239   }
6240   if( !DeleteFileW(Tcl_GetUnicode(objv[1])) ){
6241     Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
6242     return TCL_ERROR;
6243   }
6244   Tcl_ResetResult(interp);
6245   return TCL_OK;
6246 }
6247 
6248 /*
6249 **      make_win32_dir DIRECTORY
6250 **
6251 ** Creates the specified directory, whose fully qualified name may exceed 248
6252 ** characters if it is prefixed with "\\?\".
6253 */
6254 static int win32_mkdir(
6255   void *clientData,
6256   Tcl_Interp *interp,
6257   int objc,
6258   Tcl_Obj *CONST objv[]
6259 ){
6260   if( objc!=2 ){
6261     Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
6262     return TCL_ERROR;
6263   }
6264   if( !CreateDirectoryW(Tcl_GetUnicode(objv[1]), NULL) ){
6265     Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
6266     return TCL_ERROR;
6267   }
6268   Tcl_ResetResult(interp);
6269   return TCL_OK;
6270 }
6271 
6272 /*
6273 **      remove_win32_dir DIRECTORY
6274 **
6275 ** Removes the specified directory, whose fully qualified name may exceed 248
6276 ** characters if it is prefixed with "\\?\".
6277 */
6278 static int win32_rmdir(
6279   void *clientData,
6280   Tcl_Interp *interp,
6281   int objc,
6282   Tcl_Obj *CONST objv[]
6283 ){
6284   if( objc!=2 ){
6285     Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
6286     return TCL_ERROR;
6287   }
6288   if( !RemoveDirectoryW(Tcl_GetUnicode(objv[1])) ){
6289     Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
6290     return TCL_ERROR;
6291   }
6292   Tcl_ResetResult(interp);
6293   return TCL_OK;
6294 }
6295 #endif
6296 
6297 
6298 /*
6299 **      optimization_control DB OPT BOOLEAN
6300 **
6301 ** Enable or disable query optimizations using the sqlite3_test_control()
6302 ** interface.  Disable if BOOLEAN is false and enable if BOOLEAN is true.
6303 ** OPT is the name of the optimization to be disabled.
6304 */
6305 static int optimization_control(
6306   void * clientData,
6307   Tcl_Interp *interp,
6308   int objc,
6309   Tcl_Obj *CONST objv[]
6310 ){
6311   int i;
6312   sqlite3 *db;
6313   const char *zOpt;
6314   int onoff;
6315   int mask = 0;
6316   static const struct {
6317     const char *zOptName;
6318     int mask;
6319   } aOpt[] = {
6320     { "all",                 SQLITE_AllOpts        },
6321     { "none",                0                     },
6322     { "query-flattener",     SQLITE_QueryFlattener },
6323     { "column-cache",        SQLITE_ColumnCache    },
6324     { "groupby-order",       SQLITE_GroupByOrder   },
6325     { "factor-constants",    SQLITE_FactorOutConst },
6326     { "distinct-opt",        SQLITE_DistinctOpt    },
6327     { "cover-idx-scan",      SQLITE_CoverIdxScan   },
6328     { "order-by-idx-join",   SQLITE_OrderByIdxJoin },
6329     { "transitive",          SQLITE_Transitive     },
6330     { "subquery-coroutine",  SQLITE_SubqCoroutine  },
6331     { "omit-noop-join",      SQLITE_OmitNoopJoin   },
6332     { "stat3",               SQLITE_Stat34         },
6333     { "stat4",               SQLITE_Stat34         },
6334   };
6335 
6336   if( objc!=4 ){
6337     Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN");
6338     return TCL_ERROR;
6339   }
6340   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6341   if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR;
6342   zOpt = Tcl_GetString(objv[2]);
6343   for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
6344     if( strcmp(zOpt, aOpt[i].zOptName)==0 ){
6345       mask = aOpt[i].mask;
6346       break;
6347     }
6348   }
6349   if( onoff ) mask = ~mask;
6350   if( i>=sizeof(aOpt)/sizeof(aOpt[0]) ){
6351     Tcl_AppendResult(interp, "unknown optimization - should be one of:",
6352                      (char*)0);
6353     for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
6354       Tcl_AppendResult(interp, " ", aOpt[i].zOptName, (char*)0);
6355     }
6356     return TCL_ERROR;
6357   }
6358   sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask);
6359   return TCL_OK;
6360 }
6361 
6362 typedef struct sqlite3_api_routines sqlite3_api_routines;
6363 /*
6364 **     load_static_extension DB NAME ...
6365 **
6366 ** Load one or more statically linked extensions.
6367 */
6368 static int tclLoadStaticExtensionCmd(
6369   void * clientData,
6370   Tcl_Interp *interp,
6371   int objc,
6372   Tcl_Obj *CONST objv[]
6373 ){
6374   extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*);
6375   extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*);
6376   extern int sqlite3_eval_init(sqlite3*,char**,const sqlite3_api_routines*);
6377   extern int sqlite3_fileio_init(sqlite3*,char**,const sqlite3_api_routines*);
6378   extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*);
6379   extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*);
6380   extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*);
6381   extern int sqlite3_percentile_init(sqlite3*,char**,const sqlite3_api_routines*);
6382   extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*);
6383   extern int sqlite3_series_init(sqlite3*,char**,const sqlite3_api_routines*);
6384   extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*);
6385   extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*);
6386   extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*);
6387   static const struct {
6388     const char *zExtName;
6389     int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*);
6390   } aExtension[] = {
6391     { "amatch",                sqlite3_amatch_init               },
6392     { "closure",               sqlite3_closure_init              },
6393     { "eval",                  sqlite3_eval_init                 },
6394     { "fileio",                sqlite3_fileio_init               },
6395     { "fuzzer",                sqlite3_fuzzer_init               },
6396     { "ieee754",               sqlite3_ieee_init                 },
6397     { "nextchar",              sqlite3_nextchar_init             },
6398     { "percentile",            sqlite3_percentile_init           },
6399     { "regexp",                sqlite3_regexp_init               },
6400     { "series",                sqlite3_series_init               },
6401     { "spellfix",              sqlite3_spellfix_init             },
6402     { "totype",                sqlite3_totype_init               },
6403     { "wholenumber",           sqlite3_wholenumber_init          },
6404   };
6405   sqlite3 *db;
6406   const char *zName;
6407   int i, j, rc;
6408   char *zErrMsg = 0;
6409   if( objc<3 ){
6410     Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ...");
6411     return TCL_ERROR;
6412   }
6413   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6414   for(j=2; j<objc; j++){
6415     zName = Tcl_GetString(objv[j]);
6416     for(i=0; i<ArraySize(aExtension); i++){
6417       if( strcmp(zName, aExtension[i].zExtName)==0 ) break;
6418     }
6419     if( i>=ArraySize(aExtension) ){
6420       Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0);
6421       return TCL_ERROR;
6422     }
6423     if( aExtension[i].pInit ){
6424       rc = aExtension[i].pInit(db, &zErrMsg, 0);
6425     }else{
6426       rc = SQLITE_OK;
6427     }
6428     if( rc!=SQLITE_OK || zErrMsg ){
6429       Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg,
6430                        (char*)0);
6431       sqlite3_free(zErrMsg);
6432       return TCL_ERROR;
6433     }
6434   }
6435   return TCL_OK;
6436 }
6437 
6438 /*
6439 **     sorter_test_fakeheap BOOL
6440 **
6441 */
6442 static int sorter_test_fakeheap(
6443   void * clientData,
6444   Tcl_Interp *interp,
6445   int objc,
6446   Tcl_Obj *CONST objv[]
6447 ){
6448   int bArg;
6449   if( objc!=2 ){
6450     Tcl_WrongNumArgs(interp, 1, objv, "BOOL");
6451     return TCL_ERROR;
6452   }
6453 
6454   if( Tcl_GetBooleanFromObj(interp, objv[1], &bArg) ){
6455     return TCL_ERROR;
6456   }
6457 
6458   if( bArg ){
6459     if( sqlite3GlobalConfig.pHeap==0 ){
6460       sqlite3GlobalConfig.pHeap = SQLITE_INT_TO_PTR(-1);
6461     }
6462   }else{
6463     if( sqlite3GlobalConfig.pHeap==SQLITE_INT_TO_PTR(-1) ){
6464       sqlite3GlobalConfig.pHeap = 0;
6465     }
6466   }
6467 
6468   Tcl_ResetResult(interp);
6469   return TCL_OK;
6470 }
6471 
6472 /*
6473 **     sorter_test_sort4_helper DB SQL1 NSTEP SQL2
6474 **
6475 ** Compile SQL statement $SQL1 and step it $NSTEP times. For each row,
6476 ** check that the leftmost and rightmost columns returned are both integers,
6477 ** and that both contain the same value.
6478 **
6479 ** Then execute statement $SQL2. Check that the statement returns the same
6480 ** set of integers in the same order as in the previous step (using $SQL1).
6481 */
6482 static int sorter_test_sort4_helper(
6483   void * clientData,
6484   Tcl_Interp *interp,
6485   int objc,
6486   Tcl_Obj *CONST objv[]
6487 ){
6488   const char *zSql1;
6489   const char *zSql2;
6490   int nStep;
6491   int iStep;
6492   int iCksum1 = 0;
6493   int iCksum2 = 0;
6494   int rc;
6495   int iB;
6496   sqlite3 *db;
6497   sqlite3_stmt *pStmt;
6498 
6499   if( objc!=5 ){
6500     Tcl_WrongNumArgs(interp, 1, objv, "DB SQL1 NSTEP SQL2");
6501     return TCL_ERROR;
6502   }
6503 
6504   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
6505   zSql1 = Tcl_GetString(objv[2]);
6506   if( Tcl_GetIntFromObj(interp, objv[3], &nStep) ) return TCL_ERROR;
6507   zSql2 = Tcl_GetString(objv[4]);
6508 
6509   rc = sqlite3_prepare_v2(db, zSql1, -1, &pStmt, 0);
6510   if( rc!=SQLITE_OK ) goto sql_error;
6511 
6512   iB = sqlite3_column_count(pStmt)-1;
6513   for(iStep=0; iStep<nStep && SQLITE_ROW==sqlite3_step(pStmt); iStep++){
6514     int a = sqlite3_column_int(pStmt, 0);
6515     if( a!=sqlite3_column_int(pStmt, iB) ){
6516       Tcl_AppendResult(interp, "data error: (a!=b)", 0);
6517       return TCL_ERROR;
6518     }
6519 
6520     iCksum1 += (iCksum1 << 3) + a;
6521   }
6522   rc = sqlite3_finalize(pStmt);
6523   if( rc!=SQLITE_OK ) goto sql_error;
6524 
6525   rc = sqlite3_prepare_v2(db, zSql2, -1, &pStmt, 0);
6526   if( rc!=SQLITE_OK ) goto sql_error;
6527   for(iStep=0; SQLITE_ROW==sqlite3_step(pStmt); iStep++){
6528     int a = sqlite3_column_int(pStmt, 0);
6529     iCksum2 += (iCksum2 << 3) + a;
6530   }
6531   rc = sqlite3_finalize(pStmt);
6532   if( rc!=SQLITE_OK ) goto sql_error;
6533 
6534   if( iCksum1!=iCksum2 ){
6535     Tcl_AppendResult(interp, "checksum mismatch", 0);
6536     return TCL_ERROR;
6537   }
6538 
6539   return TCL_OK;
6540  sql_error:
6541   Tcl_AppendResult(interp, "sql error: ", sqlite3_errmsg(db), 0);
6542   return TCL_ERROR;
6543 }
6544 
6545 
6546 #ifdef SQLITE_USER_AUTHENTICATION
6547 #include "sqlite3userauth.h"
6548 /*
6549 ** tclcmd:  sqlite3_user_authenticate DB USERNAME PASSWORD
6550 */
6551 static int test_user_authenticate(
6552   ClientData clientData, /* Unused */
6553   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
6554   int objc,              /* Number of arguments */
6555   Tcl_Obj *CONST objv[]  /* Command arguments */
6556 ){
6557   char *zUser = 0;
6558   char *zPasswd = 0;
6559   int nPasswd = 0;
6560   sqlite3 *db;
6561   int rc;
6562 
6563   if( objc!=4 ){
6564     Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD");
6565     return TCL_ERROR;
6566   }
6567   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6568     return TCL_ERROR;
6569   }
6570   zUser = Tcl_GetString(objv[2]);
6571   zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
6572   rc = sqlite3_user_authenticate(db, zUser, zPasswd, nPasswd);
6573   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
6574   return TCL_OK;
6575 }
6576 #endif /* SQLITE_USER_AUTHENTICATION */
6577 
6578 #ifdef SQLITE_USER_AUTHENTICATION
6579 /*
6580 ** tclcmd:  sqlite3_user_add DB USERNAME PASSWORD ISADMIN
6581 */
6582 static int test_user_add(
6583   ClientData clientData, /* Unused */
6584   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
6585   int objc,              /* Number of arguments */
6586   Tcl_Obj *CONST objv[]  /* Command arguments */
6587 ){
6588   char *zUser = 0;
6589   char *zPasswd = 0;
6590   int nPasswd = 0;
6591   int isAdmin = 0;
6592   sqlite3 *db;
6593   int rc;
6594 
6595   if( objc!=5 ){
6596     Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN");
6597     return TCL_ERROR;
6598   }
6599   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6600     return TCL_ERROR;
6601   }
6602   zUser = Tcl_GetString(objv[2]);
6603   zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
6604   Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin);
6605   rc = sqlite3_user_add(db, zUser, zPasswd, nPasswd, isAdmin);
6606   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
6607   return TCL_OK;
6608 }
6609 #endif /* SQLITE_USER_AUTHENTICATION */
6610 
6611 #ifdef SQLITE_USER_AUTHENTICATION
6612 /*
6613 ** tclcmd:  sqlite3_user_change DB USERNAME PASSWORD ISADMIN
6614 */
6615 static int test_user_change(
6616   ClientData clientData, /* Unused */
6617   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
6618   int objc,              /* Number of arguments */
6619   Tcl_Obj *CONST objv[]  /* Command arguments */
6620 ){
6621   char *zUser = 0;
6622   char *zPasswd = 0;
6623   int nPasswd = 0;
6624   int isAdmin = 0;
6625   sqlite3 *db;
6626   int rc;
6627 
6628   if( objc!=5 ){
6629     Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN");
6630     return TCL_ERROR;
6631   }
6632   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6633     return TCL_ERROR;
6634   }
6635   zUser = Tcl_GetString(objv[2]);
6636   zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
6637   Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin);
6638   rc = sqlite3_user_change(db, zUser, zPasswd, nPasswd, isAdmin);
6639   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
6640   return TCL_OK;
6641 }
6642 #endif /* SQLITE_USER_AUTHENTICATION */
6643 
6644 #ifdef SQLITE_USER_AUTHENTICATION
6645 /*
6646 ** tclcmd:  sqlite3_user_delete DB USERNAME
6647 */
6648 static int test_user_delete(
6649   ClientData clientData, /* Unused */
6650   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
6651   int objc,              /* Number of arguments */
6652   Tcl_Obj *CONST objv[]  /* Command arguments */
6653 ){
6654   char *zUser = 0;
6655   sqlite3 *db;
6656   int rc;
6657 
6658   if( objc!=3 ){
6659     Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME");
6660     return TCL_ERROR;
6661   }
6662   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
6663     return TCL_ERROR;
6664   }
6665   zUser = Tcl_GetString(objv[2]);
6666   rc = sqlite3_user_delete(db, zUser);
6667   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
6668   return TCL_OK;
6669 }
6670 #endif /* SQLITE_USER_AUTHENTICATION */
6671 
6672 /*
6673 ** tclcmd: bad_behavior TYPE
6674 **
6675 ** Do some things that should trigger a valgrind or -fsanitize=undefined
6676 ** warning.  This is used to verify that errors and warnings output by those
6677 ** tools are detected by the test scripts.
6678 **
6679 **       TYPE       BEHAVIOR
6680 **       1          Overflow a signed integer
6681 **       2          Jump based on an uninitialized variable
6682 **       3          Read after free
6683 **       4          Panic
6684 */
6685 static int test_bad_behavior(
6686   ClientData clientData, /* Pointer to an integer containing zero */
6687   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
6688   int objc,              /* Number of arguments */
6689   Tcl_Obj *CONST objv[]  /* Command arguments */
6690 ){
6691   int iType;
6692   int xyz;
6693   int i = *(int*)clientData;
6694   int j;
6695   int w[10];
6696   int *a;
6697   if( objc!=2 ){
6698     Tcl_WrongNumArgs(interp, 1, objv, "TYPE");
6699     return TCL_ERROR;
6700   }
6701   if( Tcl_GetIntFromObj(interp, objv[1], &iType) ) return TCL_ERROR;
6702   switch( iType ){
6703     case 1: {
6704       xyz = 0x7fffff00 - i;
6705       xyz += 0x100;
6706       Tcl_SetObjResult(interp, Tcl_NewIntObj(xyz));
6707       break;
6708     }
6709     case 2: {
6710       w[1] = 5;
6711       if( w[i]>0 ) w[1]++;
6712       Tcl_SetObjResult(interp, Tcl_NewIntObj(w[1]));
6713       break;
6714     }
6715     case 3: {
6716       a = malloc( sizeof(int)*10 );
6717       for(j=0; j<10; j++) a[j] = j;
6718       free(a);
6719       Tcl_SetObjResult(interp, Tcl_NewIntObj(a[i]));
6720       break;
6721     }
6722     case 4: {
6723       Tcl_Panic("Deliberate panic");
6724       break;
6725     }
6726   }
6727   return TCL_OK;
6728 }
6729 
6730 /*
6731 ** tclcmd:   register_dbstat_vtab DB
6732 **
6733 ** Cause the dbstat virtual table to be available on the connection DB
6734 */
6735 static int test_register_dbstat_vtab(
6736   void *clientData,
6737   Tcl_Interp *interp,
6738   int objc,
6739   Tcl_Obj *CONST objv[]
6740 ){
6741 #ifdef SQLITE_OMIT_VIRTUALTABLE
6742   Tcl_AppendResult(interp, "dbstat not available because of "
6743                            "SQLITE_OMIT_VIRTUALTABLE", (void*)0);
6744   return TCL_ERROR;
6745 #else
6746   struct SqliteDb { sqlite3 *db; };
6747   char *zDb;
6748   Tcl_CmdInfo cmdInfo;
6749 
6750   if( objc!=2 ){
6751     Tcl_WrongNumArgs(interp, 1, objv, "DB");
6752     return TCL_ERROR;
6753   }
6754 
6755   zDb = Tcl_GetString(objv[1]);
6756   if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
6757     sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
6758     sqlite3DbstatRegister(db);
6759   }
6760   return TCL_OK;
6761 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6762 }
6763 
6764 /*
6765 ** Register commands with the TCL interpreter.
6766 */
6767 int Sqlitetest1_Init(Tcl_Interp *interp){
6768   extern int sqlite3_search_count;
6769   extern int sqlite3_found_count;
6770   extern int sqlite3_interrupt_count;
6771   extern int sqlite3_open_file_count;
6772   extern int sqlite3_sort_count;
6773   extern int sqlite3_current_time;
6774 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
6775   extern int sqlite3_hostid_num;
6776 #endif
6777   extern int sqlite3_max_blobsize;
6778   extern int sqlite3BtreeSharedCacheReport(void*,
6779                                           Tcl_Interp*,int,Tcl_Obj*CONST*);
6780   static int iZero = 0;
6781   static struct {
6782      char *zName;
6783      Tcl_CmdProc *xProc;
6784   } aCmd[] = {
6785      { "db_enter",                      (Tcl_CmdProc*)db_enter               },
6786      { "db_leave",                      (Tcl_CmdProc*)db_leave               },
6787      { "sqlite3_mprintf_int",           (Tcl_CmdProc*)sqlite3_mprintf_int    },
6788      { "sqlite3_mprintf_int64",         (Tcl_CmdProc*)sqlite3_mprintf_int64  },
6789      { "sqlite3_mprintf_long",          (Tcl_CmdProc*)sqlite3_mprintf_long   },
6790      { "sqlite3_mprintf_str",           (Tcl_CmdProc*)sqlite3_mprintf_str    },
6791      { "sqlite3_snprintf_str",          (Tcl_CmdProc*)sqlite3_snprintf_str   },
6792      { "sqlite3_mprintf_stronly",       (Tcl_CmdProc*)sqlite3_mprintf_stronly},
6793      { "sqlite3_mprintf_double",        (Tcl_CmdProc*)sqlite3_mprintf_double },
6794      { "sqlite3_mprintf_scaled",        (Tcl_CmdProc*)sqlite3_mprintf_scaled },
6795      { "sqlite3_mprintf_hexdouble",   (Tcl_CmdProc*)sqlite3_mprintf_hexdouble},
6796      { "sqlite3_mprintf_z_test",        (Tcl_CmdProc*)test_mprintf_z        },
6797      { "sqlite3_mprintf_n_test",        (Tcl_CmdProc*)test_mprintf_n        },
6798      { "sqlite3_snprintf_int",          (Tcl_CmdProc*)test_snprintf_int     },
6799      { "sqlite3_last_insert_rowid",     (Tcl_CmdProc*)test_last_rowid       },
6800      { "sqlite3_exec_printf",           (Tcl_CmdProc*)test_exec_printf      },
6801      { "sqlite3_exec_hex",              (Tcl_CmdProc*)test_exec_hex         },
6802      { "sqlite3_exec",                  (Tcl_CmdProc*)test_exec             },
6803      { "sqlite3_exec_nr",               (Tcl_CmdProc*)test_exec_nr          },
6804 #ifndef SQLITE_OMIT_GET_TABLE
6805      { "sqlite3_get_table_printf",      (Tcl_CmdProc*)test_get_table_printf },
6806 #endif
6807      { "sqlite3_close",                 (Tcl_CmdProc*)sqlite_test_close     },
6808      { "sqlite3_close_v2",              (Tcl_CmdProc*)sqlite_test_close_v2  },
6809      { "sqlite3_create_function",       (Tcl_CmdProc*)test_create_function  },
6810      { "sqlite3_create_aggregate",      (Tcl_CmdProc*)test_create_aggregate },
6811      { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func    },
6812      { "sqlite_abort",                  (Tcl_CmdProc*)sqlite_abort          },
6813      { "sqlite_bind",                   (Tcl_CmdProc*)test_bind             },
6814      { "breakpoint",                    (Tcl_CmdProc*)test_breakpoint       },
6815      { "sqlite3_key",                   (Tcl_CmdProc*)test_key              },
6816      { "sqlite3_rekey",                 (Tcl_CmdProc*)test_rekey            },
6817      { "sqlite_set_magic",              (Tcl_CmdProc*)sqlite_set_magic      },
6818      { "sqlite3_interrupt",             (Tcl_CmdProc*)test_interrupt        },
6819      { "sqlite_delete_function",        (Tcl_CmdProc*)delete_function       },
6820      { "sqlite_delete_collation",       (Tcl_CmdProc*)delete_collation      },
6821      { "sqlite3_get_autocommit",        (Tcl_CmdProc*)get_autocommit        },
6822      { "sqlite3_stack_used",            (Tcl_CmdProc*)test_stack_used       },
6823      { "sqlite3_busy_timeout",          (Tcl_CmdProc*)test_busy_timeout     },
6824      { "printf",                        (Tcl_CmdProc*)test_printf           },
6825      { "sqlite3IoTrace",              (Tcl_CmdProc*)test_io_trace         },
6826      { "clang_sanitize_address",        (Tcl_CmdProc*)clang_sanitize_address },
6827   };
6828   static struct {
6829      char *zName;
6830      Tcl_ObjCmdProc *xProc;
6831      void *clientData;
6832   } aObjCmd[] = {
6833      { "bad_behavior",                  test_bad_behavior,  (void*)&iZero },
6834      { "register_dbstat_vtab",          test_register_dbstat_vtab  },
6835      { "sqlite3_connection_pointer",    get_sqlite_pointer, 0 },
6836      { "sqlite3_bind_int",              test_bind_int,      0 },
6837      { "sqlite3_bind_zeroblob",         test_bind_zeroblob, 0 },
6838      { "sqlite3_bind_zeroblob64",       test_bind_zeroblob64, 0 },
6839      { "sqlite3_bind_int64",            test_bind_int64,    0 },
6840      { "sqlite3_bind_double",           test_bind_double,   0 },
6841      { "sqlite3_bind_null",             test_bind_null     ,0 },
6842      { "sqlite3_bind_text",             test_bind_text     ,0 },
6843      { "sqlite3_bind_text16",           test_bind_text16   ,0 },
6844      { "sqlite3_bind_blob",             test_bind_blob     ,0 },
6845      { "sqlite3_bind_parameter_count",  test_bind_parameter_count, 0},
6846      { "sqlite3_bind_parameter_name",   test_bind_parameter_name,  0},
6847      { "sqlite3_bind_parameter_index",  test_bind_parameter_index, 0},
6848      { "sqlite3_clear_bindings",        test_clear_bindings, 0},
6849      { "sqlite3_sleep",                 test_sleep,          0},
6850      { "sqlite3_errcode",               test_errcode       ,0 },
6851      { "sqlite3_extended_errcode",      test_ex_errcode    ,0 },
6852      { "sqlite3_errmsg",                test_errmsg        ,0 },
6853      { "sqlite3_errmsg16",              test_errmsg16      ,0 },
6854      { "sqlite3_open",                  test_open          ,0 },
6855      { "sqlite3_open16",                test_open16        ,0 },
6856      { "sqlite3_open_v2",               test_open_v2       ,0 },
6857      { "sqlite3_complete16",            test_complete16    ,0 },
6858 
6859      { "sqlite3_prepare",               test_prepare       ,0 },
6860      { "sqlite3_prepare16",             test_prepare16     ,0 },
6861      { "sqlite3_prepare_v2",            test_prepare_v2    ,0 },
6862      { "sqlite3_prepare_tkt3134",       test_prepare_tkt3134, 0},
6863      { "sqlite3_prepare16_v2",          test_prepare16_v2  ,0 },
6864      { "sqlite3_finalize",              test_finalize      ,0 },
6865      { "sqlite3_stmt_status",           test_stmt_status   ,0 },
6866      { "sqlite3_reset",                 test_reset         ,0 },
6867      { "sqlite3_expired",               test_expired       ,0 },
6868      { "sqlite3_transfer_bindings",     test_transfer_bind ,0 },
6869      { "sqlite3_changes",               test_changes       ,0 },
6870      { "sqlite3_step",                  test_step          ,0 },
6871      { "sqlite3_sql",                   test_sql           ,0 },
6872      { "sqlite3_next_stmt",             test_next_stmt     ,0 },
6873      { "sqlite3_stmt_readonly",         test_stmt_readonly ,0 },
6874      { "sqlite3_stmt_busy",             test_stmt_busy     ,0 },
6875      { "uses_stmt_journal",             uses_stmt_journal ,0 },
6876 
6877      { "sqlite3_release_memory",        test_release_memory,     0},
6878      { "sqlite3_db_release_memory",     test_db_release_memory,  0},
6879      { "sqlite3_db_filename",           test_db_filename,        0},
6880      { "sqlite3_db_readonly",           test_db_readonly,        0},
6881      { "sqlite3_soft_heap_limit",       test_soft_heap_limit,    0},
6882      { "sqlite3_thread_cleanup",        test_thread_cleanup,     0},
6883      { "sqlite3_pager_refcounts",       test_pager_refcounts,    0},
6884 
6885      { "sqlite3_load_extension",        test_load_extension,     0},
6886      { "sqlite3_enable_load_extension", test_enable_load,        0},
6887      { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
6888      { "sqlite3_limit",                 test_limit,                 0},
6889 
6890      { "save_prng_state",               save_prng_state,    0 },
6891      { "restore_prng_state",            restore_prng_state, 0 },
6892      { "reset_prng_state",              reset_prng_state,   0 },
6893      { "database_never_corrupt",        database_never_corrupt, 0},
6894      { "database_may_be_corrupt",       database_may_be_corrupt, 0},
6895      { "optimization_control",          optimization_control,0},
6896 #if SQLITE_OS_WIN
6897      { "lock_win32_file",               win32_file_lock,    0 },
6898      { "exists_win32_path",             win32_exists_path,  0 },
6899      { "find_win32_file",               win32_find_file,    0 },
6900      { "delete_win32_file",             win32_delete_file,  0 },
6901      { "make_win32_dir",                win32_mkdir,        0 },
6902      { "remove_win32_dir",              win32_rmdir,        0 },
6903 #endif
6904      { "tcl_objproc",                   runAsObjProc,       0 },
6905 
6906      /* sqlite3_column_*() API */
6907      { "sqlite3_column_count",          test_column_count  ,0 },
6908      { "sqlite3_data_count",            test_data_count    ,0 },
6909      { "sqlite3_column_type",           test_column_type   ,0 },
6910      { "sqlite3_column_blob",           test_column_blob   ,0 },
6911      { "sqlite3_column_double",         test_column_double ,0 },
6912      { "sqlite3_column_int64",          test_column_int64  ,0 },
6913      { "sqlite3_column_text",   test_stmt_utf8,  (void*)sqlite3_column_text },
6914      { "sqlite3_column_name",   test_stmt_utf8,  (void*)sqlite3_column_name },
6915      { "sqlite3_column_int",    test_stmt_int,   (void*)sqlite3_column_int  },
6916      { "sqlite3_column_bytes",  test_stmt_int,   (void*)sqlite3_column_bytes},
6917 #ifndef SQLITE_OMIT_DECLTYPE
6918      { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype},
6919 #endif
6920 #ifdef SQLITE_ENABLE_COLUMN_METADATA
6921 { "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name},
6922 { "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name},
6923 { "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name},
6924 #endif
6925 
6926 #ifndef SQLITE_OMIT_UTF16
6927      { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 },
6928      { "sqlite3_column_text16",  test_stmt_utf16, (void*)sqlite3_column_text16},
6929      { "sqlite3_column_name16",  test_stmt_utf16, (void*)sqlite3_column_name16},
6930      { "add_alignment_test_collations", add_alignment_test_collations, 0      },
6931 #ifndef SQLITE_OMIT_DECLTYPE
6932      { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16},
6933 #endif
6934 #ifdef SQLITE_ENABLE_COLUMN_METADATA
6935 {"sqlite3_column_database_name16",
6936   test_stmt_utf16, (void*)sqlite3_column_database_name16},
6937 {"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16},
6938 {"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16},
6939 #endif
6940 #endif
6941      { "sqlite3_create_collation_v2", test_create_collation_v2, 0 },
6942      { "sqlite3_global_recover",     test_global_recover, 0   },
6943      { "working_64bit_int",          working_64bit_int,   0   },
6944      { "vfs_unlink_test",            vfs_unlink_test,     0   },
6945      { "vfs_initfail_test",          vfs_initfail_test,   0   },
6946      { "vfs_unregister_all",         vfs_unregister_all,  0   },
6947      { "vfs_reregister_all",         vfs_reregister_all,  0   },
6948      { "file_control_test",          file_control_test,   0   },
6949      { "file_control_lasterrno_test", file_control_lasterrno_test,  0   },
6950      { "file_control_lockproxy_test", file_control_lockproxy_test,  0   },
6951      { "file_control_chunksize_test", file_control_chunksize_test,  0   },
6952      { "file_control_sizehint_test",  file_control_sizehint_test,   0   },
6953 #if SQLITE_OS_WIN
6954      { "file_control_win32_av_retry", file_control_win32_av_retry,  0   },
6955      { "file_control_win32_set_handle", file_control_win32_set_handle, 0  },
6956 #endif
6957      { "file_control_persist_wal",    file_control_persist_wal,     0   },
6958      { "file_control_powersafe_overwrite",file_control_powersafe_overwrite,0},
6959      { "file_control_vfsname",        file_control_vfsname,         0   },
6960      { "file_control_tempfilename",   file_control_tempfilename,    0   },
6961      { "sqlite3_vfs_list",           vfs_list,     0   },
6962      { "sqlite3_create_function_v2", test_create_function_v2, 0 },
6963 
6964      /* Functions from os.h */
6965 #ifndef SQLITE_OMIT_UTF16
6966      { "add_test_collate",        test_collate, 0            },
6967      { "add_test_collate_needed", test_collate_needed, 0     },
6968      { "add_test_function",       test_function, 0           },
6969      { "add_test_utf16bin_collate",    test_utf16bin_collate, 0        },
6970 #endif
6971      { "sqlite3_test_errstr",     test_errstr, 0             },
6972      { "tcl_variable_type",       tcl_variable_type, 0       },
6973 #ifndef SQLITE_OMIT_SHARED_CACHE
6974      { "sqlite3_enable_shared_cache", test_enable_shared, 0  },
6975      { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0},
6976 #endif
6977      { "sqlite3_libversion_number", test_libversion_number, 0  },
6978      { "sqlite3_table_column_metadata", test_table_column_metadata, 0  },
6979 #ifndef SQLITE_OMIT_INCRBLOB
6980      { "sqlite3_blob_reopen", test_blob_reopen, 0  },
6981 #endif
6982      { "pcache_stats",       test_pcache_stats, 0  },
6983 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
6984      { "sqlite3_unlock_notify", test_unlock_notify, 0  },
6985 #endif
6986      { "sqlite3_wal_checkpoint",   test_wal_checkpoint, 0  },
6987      { "sqlite3_wal_checkpoint_v2",test_wal_checkpoint_v2, 0  },
6988      { "sqlite3_wal_autocheckpoint",test_wal_autocheckpoint, 0  },
6989      { "test_sqlite3_log",         test_sqlite3_log, 0  },
6990 #ifndef SQLITE_OMIT_EXPLAIN
6991      { "print_explain_query_plan", test_print_eqp, 0  },
6992 #endif
6993      { "sqlite3_test_control", test_test_control },
6994 #if SQLITE_OS_UNIX
6995      { "getrusage", test_getrusage },
6996 #endif
6997      { "load_static_extension", tclLoadStaticExtensionCmd },
6998      { "sorter_test_fakeheap", sorter_test_fakeheap },
6999      { "sorter_test_sort4_helper", sorter_test_sort4_helper },
7000 #ifdef SQLITE_USER_AUTHENTICATION
7001      { "sqlite3_user_authenticate", test_user_authenticate, 0 },
7002      { "sqlite3_user_add",          test_user_add,          0 },
7003      { "sqlite3_user_change",       test_user_change,       0 },
7004      { "sqlite3_user_delete",       test_user_delete,       0 },
7005 #endif
7006 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
7007      { "sqlite3_stmt_scanstatus",       test_stmt_scanstatus,   0 },
7008      { "sqlite3_stmt_scanstatus_reset", test_stmt_scanstatus_reset,   0 },
7009 #endif
7010 
7011   };
7012   static int bitmask_size = sizeof(Bitmask)*8;
7013   int i;
7014   extern int sqlite3_sync_count, sqlite3_fullsync_count;
7015   extern int sqlite3_opentemp_count;
7016   extern int sqlite3_like_count;
7017   extern int sqlite3_xferopt_count;
7018   extern int sqlite3_pager_readdb_count;
7019   extern int sqlite3_pager_writedb_count;
7020   extern int sqlite3_pager_writej_count;
7021 #if SQLITE_OS_WIN
7022   extern LONG volatile sqlite3_os_type;
7023 #endif
7024 #ifdef SQLITE_DEBUG
7025   extern int sqlite3WhereTrace;
7026   extern int sqlite3OSTrace;
7027   extern int sqlite3WalTrace;
7028 #endif
7029 #ifdef SQLITE_TEST
7030 #ifdef SQLITE_ENABLE_FTS3
7031   extern int sqlite3_fts3_enable_parentheses;
7032 #endif
7033 #endif
7034 
7035   for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
7036     Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
7037   }
7038   for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
7039     Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
7040         aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
7041   }
7042   Tcl_LinkVar(interp, "sqlite_search_count",
7043       (char*)&sqlite3_search_count, TCL_LINK_INT);
7044   Tcl_LinkVar(interp, "sqlite_found_count",
7045       (char*)&sqlite3_found_count, TCL_LINK_INT);
7046   Tcl_LinkVar(interp, "sqlite_sort_count",
7047       (char*)&sqlite3_sort_count, TCL_LINK_INT);
7048   Tcl_LinkVar(interp, "sqlite3_max_blobsize",
7049       (char*)&sqlite3_max_blobsize, TCL_LINK_INT);
7050   Tcl_LinkVar(interp, "sqlite_like_count",
7051       (char*)&sqlite3_like_count, TCL_LINK_INT);
7052   Tcl_LinkVar(interp, "sqlite_interrupt_count",
7053       (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
7054   Tcl_LinkVar(interp, "sqlite_open_file_count",
7055       (char*)&sqlite3_open_file_count, TCL_LINK_INT);
7056   Tcl_LinkVar(interp, "sqlite_current_time",
7057       (char*)&sqlite3_current_time, TCL_LINK_INT);
7058 #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
7059   Tcl_LinkVar(interp, "sqlite_hostid_num",
7060       (char*)&sqlite3_hostid_num, TCL_LINK_INT);
7061 #endif
7062   Tcl_LinkVar(interp, "sqlite3_xferopt_count",
7063       (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
7064   Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
7065       (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);
7066   Tcl_LinkVar(interp, "sqlite3_pager_writedb_count",
7067       (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT);
7068   Tcl_LinkVar(interp, "sqlite3_pager_writej_count",
7069       (char*)&sqlite3_pager_writej_count, TCL_LINK_INT);
7070 #ifndef SQLITE_OMIT_UTF16
7071   Tcl_LinkVar(interp, "unaligned_string_counter",
7072       (char*)&unaligned_string_counter, TCL_LINK_INT);
7073 #endif
7074 #ifndef SQLITE_OMIT_UTF16
7075   Tcl_LinkVar(interp, "sqlite_last_needed_collation",
7076       (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
7077 #endif
7078 #if SQLITE_OS_WIN
7079   Tcl_LinkVar(interp, "sqlite_os_type",
7080       (char*)&sqlite3_os_type, TCL_LINK_LONG);
7081 #endif
7082 #ifdef SQLITE_TEST
7083   {
7084     static const char *query_plan = "*** OBSOLETE VARIABLE ***";
7085     Tcl_LinkVar(interp, "sqlite_query_plan",
7086        (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
7087   }
7088 #endif
7089 #ifdef SQLITE_DEBUG
7090   Tcl_LinkVar(interp, "sqlite_where_trace",
7091       (char*)&sqlite3WhereTrace, TCL_LINK_INT);
7092   Tcl_LinkVar(interp, "sqlite_os_trace",
7093       (char*)&sqlite3OSTrace, TCL_LINK_INT);
7094 #ifndef SQLITE_OMIT_WAL
7095   Tcl_LinkVar(interp, "sqlite_wal_trace",
7096       (char*)&sqlite3WalTrace, TCL_LINK_INT);
7097 #endif
7098 #endif
7099 #ifndef SQLITE_OMIT_DISKIO
7100   Tcl_LinkVar(interp, "sqlite_opentemp_count",
7101       (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
7102 #endif
7103   Tcl_LinkVar(interp, "sqlite_static_bind_value",
7104       (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
7105   Tcl_LinkVar(interp, "sqlite_static_bind_nbyte",
7106       (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT);
7107   Tcl_LinkVar(interp, "sqlite_temp_directory",
7108       (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
7109   Tcl_LinkVar(interp, "sqlite_data_directory",
7110       (char*)&sqlite3_data_directory, TCL_LINK_STRING);
7111   Tcl_LinkVar(interp, "bitmask_size",
7112       (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
7113   Tcl_LinkVar(interp, "sqlite_sync_count",
7114       (char*)&sqlite3_sync_count, TCL_LINK_INT);
7115   Tcl_LinkVar(interp, "sqlite_fullsync_count",
7116       (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
7117 #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST)
7118   Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses",
7119       (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT);
7120 #endif
7121   return TCL_OK;
7122 }
7123