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