xref: /sqlite-3.40.0/src/test1.c (revision d230f648)
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 ** $Id: test1.c,v 1.246 2007/05/05 18:39:25 drh Exp $
17 */
18 #include "sqliteInt.h"
19 #include "tcl.h"
20 #include "os.h"
21 #include <stdlib.h>
22 #include <string.h>
23 
24 /*
25 ** This is a copy of the first part of the SqliteDb structure in
26 ** tclsqlite.c.  We need it here so that the get_sqlite_pointer routine
27 ** can extract the sqlite3* pointer from an existing Tcl SQLite
28 ** connection.
29 */
30 struct SqliteDb {
31   sqlite3 *db;
32 };
33 
34 /*
35 ** Convert text generated by the "%p" conversion format back into
36 ** a pointer.
37 */
38 static int testHexToInt(int h){
39   if( h>='0' && h<='9' ){
40     return h - '0';
41   }else if( h>='a' && h<='f' ){
42     return h - 'a' + 10;
43   }else{
44     assert( h>='A' && h<='F' );
45     return h - 'A' + 10;
46   }
47 }
48 void *sqlite3TextToPtr(const char *z){
49   void *p;
50   u64 v;
51   u32 v2;
52   if( z[0]=='0' && z[1]=='x' ){
53     z += 2;
54   }
55   v = 0;
56   while( *z ){
57     v = (v<<4) + testHexToInt(*z);
58     z++;
59   }
60   if( sizeof(p)==sizeof(v) ){
61     memcpy(&p, &v, sizeof(p));
62   }else{
63     assert( sizeof(p)==sizeof(v2) );
64     v2 = (u32)v;
65     memcpy(&p, &v2, sizeof(p));
66   }
67   return p;
68 }
69 
70 
71 /*
72 ** A TCL command that returns the address of the sqlite* pointer
73 ** for an sqlite connection instance.  Bad things happen if the
74 ** input is not an sqlite connection.
75 */
76 static int get_sqlite_pointer(
77   void * clientData,
78   Tcl_Interp *interp,
79   int objc,
80   Tcl_Obj *CONST objv[]
81 ){
82   struct SqliteDb *p;
83   Tcl_CmdInfo cmdInfo;
84   char zBuf[100];
85   if( objc!=2 ){
86     Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION");
87     return TCL_ERROR;
88   }
89   if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
90     Tcl_AppendResult(interp, "command not found: ",
91            Tcl_GetString(objv[1]), (char*)0);
92     return TCL_ERROR;
93   }
94   p = (struct SqliteDb*)cmdInfo.objClientData;
95   sprintf(zBuf, "%p", p->db);
96   if( strncmp(zBuf,"0x",2) ){
97     sprintf(zBuf, "0x%p", p->db);
98   }
99   Tcl_AppendResult(interp, zBuf, 0);
100   return TCL_OK;
101 }
102 
103 /*
104 ** Decode a pointer to an sqlite3 object.
105 */
106 static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
107   struct SqliteDb *p;
108   Tcl_CmdInfo cmdInfo;
109   if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){
110     p = (struct SqliteDb*)cmdInfo.objClientData;
111     *ppDb = p->db;
112   }else{
113     *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
114   }
115   return TCL_OK;
116 }
117 
118 
119 const char *sqlite3TestErrorName(int rc){
120   const char *zName = 0;
121   switch( rc & 0xff ){
122     case SQLITE_OK:         zName = "SQLITE_OK";          break;
123     case SQLITE_ERROR:      zName = "SQLITE_ERROR";       break;
124     case SQLITE_PERM:       zName = "SQLITE_PERM";        break;
125     case SQLITE_ABORT:      zName = "SQLITE_ABORT";       break;
126     case SQLITE_BUSY:       zName = "SQLITE_BUSY";        break;
127     case SQLITE_LOCKED:     zName = "SQLITE_LOCKED";      break;
128     case SQLITE_NOMEM:      zName = "SQLITE_NOMEM";       break;
129     case SQLITE_READONLY:   zName = "SQLITE_READONLY";    break;
130     case SQLITE_INTERRUPT:  zName = "SQLITE_INTERRUPT";   break;
131     case SQLITE_IOERR:      zName = "SQLITE_IOERR";       break;
132     case SQLITE_CORRUPT:    zName = "SQLITE_CORRUPT";     break;
133     case SQLITE_FULL:       zName = "SQLITE_FULL";        break;
134     case SQLITE_CANTOPEN:   zName = "SQLITE_CANTOPEN";    break;
135     case SQLITE_PROTOCOL:   zName = "SQLITE_PROTOCOL";    break;
136     case SQLITE_EMPTY:      zName = "SQLITE_EMPTY";       break;
137     case SQLITE_SCHEMA:     zName = "SQLITE_SCHEMA";      break;
138     case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT";  break;
139     case SQLITE_MISMATCH:   zName = "SQLITE_MISMATCH";    break;
140     case SQLITE_MISUSE:     zName = "SQLITE_MISUSE";      break;
141     case SQLITE_NOLFS:      zName = "SQLITE_NOLFS";       break;
142     case SQLITE_AUTH:       zName = "SQLITE_AUTH";        break;
143     case SQLITE_FORMAT:     zName = "SQLITE_FORMAT";      break;
144     case SQLITE_RANGE:      zName = "SQLITE_RANGE";       break;
145     case SQLITE_ROW:        zName = "SQLITE_ROW";         break;
146     case SQLITE_DONE:       zName = "SQLITE_DONE";        break;
147     case SQLITE_NOTADB:     zName = "SQLITE_NOTADB";      break;
148     default:                zName = "SQLITE_Unknown";     break;
149   }
150   return zName;
151 }
152 #define t1ErrorName sqlite3TestErrorName
153 
154 /*
155 ** Convert an sqlite3_stmt* into an sqlite3*.  This depends on the
156 ** fact that the sqlite3* is the first field in the Vdbe structure.
157 */
158 #define StmtToDb(X)   sqlite3_db_handle(X)
159 
160 /*
161 ** Check a return value to make sure it agrees with the results
162 ** from sqlite3_errcode.
163 */
164 int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){
165   if( rc!=SQLITE_MISUSE && rc!=SQLITE_OK && sqlite3_errcode(db)!=rc ){
166     char zBuf[200];
167     int r2 = sqlite3_errcode(db);
168     sprintf(zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)",
169        t1ErrorName(rc), rc, t1ErrorName(r2), r2);
170     Tcl_ResetResult(interp);
171     Tcl_AppendResult(interp, zBuf, 0);
172     return 1;
173   }
174   return 0;
175 }
176 
177 /*
178 ** Decode a pointer to an sqlite3_stmt object.
179 */
180 static int getStmtPointer(
181   Tcl_Interp *interp,
182   const char *zArg,
183   sqlite3_stmt **ppStmt
184 ){
185   *ppStmt = (sqlite3_stmt*)sqlite3TextToPtr(zArg);
186   return TCL_OK;
187 }
188 
189 /*
190 ** Decode a pointer to an sqlite3_stmt object.
191 */
192 static int getFilePointer(
193   Tcl_Interp *interp,
194   const char *zArg,
195   OsFile **ppFile
196 ){
197   *ppFile = (OsFile*)sqlite3TextToPtr(zArg);
198   return TCL_OK;
199 }
200 
201 /*
202 ** Generate a text representation of a pointer that can be understood
203 ** by the getDbPointer and getVmPointer routines above.
204 **
205 ** The problem is, on some machines (Solaris) if you do a printf with
206 ** "%p" you cannot turn around and do a scanf with the same "%p" and
207 ** get your pointer back.  You have to prepend a "0x" before it will
208 ** work.  Or at least that is what is reported to me (drh).  But this
209 ** behavior varies from machine to machine.  The solution used her is
210 ** to test the string right after it is generated to see if it can be
211 ** understood by scanf, and if not, try prepending an "0x" to see if
212 ** that helps.  If nothing works, a fatal error is generated.
213 */
214 int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){
215   sqlite3_snprintf(100, zPtr, "%p", p);
216   return TCL_OK;
217 }
218 
219 /*
220 ** The callback routine for sqlite3_exec_printf().
221 */
222 static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){
223   Tcl_DString *str = (Tcl_DString*)pArg;
224   int i;
225 
226   if( Tcl_DStringLength(str)==0 ){
227     for(i=0; i<argc; i++){
228       Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL");
229     }
230   }
231   for(i=0; i<argc; i++){
232     Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL");
233   }
234   return 0;
235 }
236 
237 /*
238 ** The I/O tracing callback.
239 */
240 static FILE *iotrace_file = 0;
241 static void io_trace_callback(const char *zFormat, ...){
242   va_list ap;
243   va_start(ap, zFormat);
244   vfprintf(iotrace_file, zFormat, ap);
245   va_end(ap);
246   fflush(iotrace_file);
247 }
248 
249 /*
250 ** Usage:  io_trace FILENAME
251 **
252 ** Turn I/O tracing on or off.  If FILENAME is not an empty string,
253 ** I/O tracing begins going into FILENAME. If FILENAME is an empty
254 ** string, I/O tracing is turned off.
255 */
256 static int test_io_trace(
257   void *NotUsed,
258   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
259   int argc,              /* Number of arguments */
260   char **argv            /* Text of each argument */
261 ){
262   if( argc!=2 ){
263     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
264           " FILENAME\"", 0);
265     return TCL_ERROR;
266   }
267   if( iotrace_file ){
268     if( iotrace_file!=stdout && iotrace_file!=stderr ){
269       fclose(iotrace_file);
270     }
271     iotrace_file = 0;
272     sqlite3_io_trace = 0;
273   }
274   if( argv[1][0] ){
275     if( strcmp(argv[1],"stdout")==0 ){
276       iotrace_file = stdout;
277     }else if( strcmp(argv[1],"stderr")==0 ){
278       iotrace_file = stderr;
279     }else{
280       iotrace_file = fopen(argv[1], "w");
281     }
282     sqlite3_io_trace = io_trace_callback;
283   }
284   return SQLITE_OK;
285 }
286 
287 
288 /*
289 ** Usage:  sqlite3_exec_printf  DB  FORMAT  STRING
290 **
291 ** Invoke the sqlite3_exec_printf() interface using the open database
292 ** DB.  The SQL is the string FORMAT.  The format string should contain
293 ** one %s or %q.  STRING is the value inserted into %s or %q.
294 */
295 static int test_exec_printf(
296   void *NotUsed,
297   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
298   int argc,              /* Number of arguments */
299   char **argv            /* Text of each argument */
300 ){
301   sqlite3 *db;
302   Tcl_DString str;
303   int rc;
304   char *zErr = 0;
305   char *zSql;
306   char zBuf[30];
307   if( argc!=4 ){
308     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
309        " DB FORMAT STRING", 0);
310     return TCL_ERROR;
311   }
312   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
313   Tcl_DStringInit(&str);
314   zSql = sqlite3_mprintf(argv[2], argv[3]);
315   rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
316   sqlite3_free(zSql);
317   sprintf(zBuf, "%d", rc);
318   Tcl_AppendElement(interp, zBuf);
319   Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
320   Tcl_DStringFree(&str);
321   if( zErr ) sqlite3_free(zErr);
322   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
323   return TCL_OK;
324 }
325 
326 /*
327 ** Usage:  sqlite3_exec  DB  SQL
328 **
329 ** Invoke the sqlite3_exec interface using the open database DB
330 */
331 static int test_exec(
332   void *NotUsed,
333   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
334   int argc,              /* Number of arguments */
335   char **argv            /* Text of each argument */
336 ){
337   sqlite3 *db;
338   Tcl_DString str;
339   int rc;
340   char *zErr = 0;
341   char zBuf[30];
342   if( argc!=3 ){
343     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
344        " DB SQL", 0);
345     return TCL_ERROR;
346   }
347   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
348   Tcl_DStringInit(&str);
349   rc = sqlite3_exec(db, argv[2], exec_printf_cb, &str, &zErr);
350   sprintf(zBuf, "%d", rc);
351   Tcl_AppendElement(interp, zBuf);
352   Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
353   Tcl_DStringFree(&str);
354   if( zErr ) sqlite3_free(zErr);
355   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
356   return TCL_OK;
357 }
358 
359 /*
360 ** Usage:  sqlite3_exec_nr  DB  SQL
361 **
362 ** Invoke the sqlite3_exec interface using the open database DB.  Discard
363 ** all results
364 */
365 static int test_exec_nr(
366   void *NotUsed,
367   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
368   int argc,              /* Number of arguments */
369   char **argv            /* Text of each argument */
370 ){
371   sqlite3 *db;
372   int rc;
373   char *zErr = 0;
374   if( argc!=3 ){
375     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
376        " DB SQL", 0);
377     return TCL_ERROR;
378   }
379   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
380   rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
381   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
382   return TCL_OK;
383 }
384 
385 /*
386 ** Usage:  sqlite3_mprintf_z_test  SEPARATOR  ARG0  ARG1 ...
387 **
388 ** Test the %z format of sqliteMPrintf().  Use multiple mprintf() calls to
389 ** concatenate arg0 through argn using separator as the separator.
390 ** Return the result.
391 */
392 static int test_mprintf_z(
393   void *NotUsed,
394   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
395   int argc,              /* Number of arguments */
396   char **argv            /* Text of each argument */
397 ){
398   char *zResult = 0;
399   int i;
400 
401   for(i=2; i<argc; i++){
402     zResult = sqlite3MPrintf("%z%s%s", zResult, argv[1], argv[i]);
403   }
404   Tcl_AppendResult(interp, zResult, 0);
405   sqliteFree(zResult);
406   return TCL_OK;
407 }
408 
409 /*
410 ** Usage:  sqlite3_mprintf_n_test  STRING
411 **
412 ** Test the %n format of sqliteMPrintf().  Return the length of the
413 ** input string.
414 */
415 static int test_mprintf_n(
416   void *NotUsed,
417   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
418   int argc,              /* Number of arguments */
419   char **argv            /* Text of each argument */
420 ){
421   char *zStr;
422   int n = 0;
423   zStr = sqlite3MPrintf("%s%n", argv[1], &n);
424   sqliteFree(zStr);
425   Tcl_SetObjResult(interp, Tcl_NewIntObj(n));
426   return TCL_OK;
427 }
428 
429 /*
430 ** Usage:  sqlite3_get_table_printf  DB  FORMAT  STRING
431 **
432 ** Invoke the sqlite3_get_table_printf() interface using the open database
433 ** DB.  The SQL is the string FORMAT.  The format string should contain
434 ** one %s or %q.  STRING is the value inserted into %s or %q.
435 */
436 static int test_get_table_printf(
437   void *NotUsed,
438   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
439   int argc,              /* Number of arguments */
440   char **argv            /* Text of each argument */
441 ){
442   sqlite3 *db;
443   Tcl_DString str;
444   int rc;
445   char *zErr = 0;
446   int nRow, nCol;
447   char **aResult;
448   int i;
449   char zBuf[30];
450   char *zSql;
451   if( argc!=4 ){
452     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
453        " DB FORMAT STRING", 0);
454     return TCL_ERROR;
455   }
456   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
457   Tcl_DStringInit(&str);
458   zSql = sqlite3_mprintf(argv[2],argv[3]);
459   rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr);
460   sqlite3_free(zSql);
461   sprintf(zBuf, "%d", rc);
462   Tcl_AppendElement(interp, zBuf);
463   if( rc==SQLITE_OK ){
464     sprintf(zBuf, "%d", nRow);
465     Tcl_AppendElement(interp, zBuf);
466     sprintf(zBuf, "%d", nCol);
467     Tcl_AppendElement(interp, zBuf);
468     for(i=0; i<(nRow+1)*nCol; i++){
469       Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL");
470     }
471   }else{
472     Tcl_AppendElement(interp, zErr);
473   }
474   sqlite3_free_table(aResult);
475   if( zErr ) sqlite3_free(zErr);
476   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
477   return TCL_OK;
478 }
479 
480 
481 /*
482 ** Usage:  sqlite3_last_insert_rowid DB
483 **
484 ** Returns the integer ROWID of the most recent insert.
485 */
486 static int test_last_rowid(
487   void *NotUsed,
488   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
489   int argc,              /* Number of arguments */
490   char **argv            /* Text of each argument */
491 ){
492   sqlite3 *db;
493   char zBuf[30];
494 
495   if( argc!=2 ){
496     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0);
497     return TCL_ERROR;
498   }
499   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
500   sprintf(zBuf, "%lld", sqlite3_last_insert_rowid(db));
501   Tcl_AppendResult(interp, zBuf, 0);
502   return SQLITE_OK;
503 }
504 
505 /*
506 ** Usage:  sqlite3_key DB KEY
507 **
508 ** Set the codec key.
509 */
510 static int test_key(
511   void *NotUsed,
512   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
513   int argc,              /* Number of arguments */
514   char **argv            /* Text of each argument */
515 ){
516   sqlite3 *db;
517   const char *zKey;
518   int nKey;
519   if( argc!=3 ){
520     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
521        " FILENAME\"", 0);
522     return TCL_ERROR;
523   }
524   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
525   zKey = argv[2];
526   nKey = strlen(zKey);
527 #ifdef SQLITE_HAS_CODEC
528   sqlite3_key(db, zKey, nKey);
529 #endif
530   return TCL_OK;
531 }
532 
533 /*
534 ** Usage:  sqlite3_rekey DB KEY
535 **
536 ** Change the codec key.
537 */
538 static int test_rekey(
539   void *NotUsed,
540   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
541   int argc,              /* Number of arguments */
542   char **argv            /* Text of each argument */
543 ){
544   sqlite3 *db;
545   const char *zKey;
546   int nKey;
547   if( argc!=3 ){
548     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
549        " FILENAME\"", 0);
550     return TCL_ERROR;
551   }
552   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
553   zKey = argv[2];
554   nKey = strlen(zKey);
555 #ifdef SQLITE_HAS_CODEC
556   sqlite3_rekey(db, zKey, nKey);
557 #endif
558   return TCL_OK;
559 }
560 
561 /*
562 ** Usage:  sqlite3_close DB
563 **
564 ** Closes the database opened by sqlite3_open.
565 */
566 static int sqlite_test_close(
567   void *NotUsed,
568   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
569   int argc,              /* Number of arguments */
570   char **argv            /* Text of each argument */
571 ){
572   sqlite3 *db;
573   int rc;
574   if( argc!=2 ){
575     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
576        " FILENAME\"", 0);
577     return TCL_ERROR;
578   }
579   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
580   rc = sqlite3_close(db);
581   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
582   return TCL_OK;
583 }
584 
585 /*
586 ** Implementation of the x_coalesce() function.
587 ** Return the first argument non-NULL argument.
588 */
589 static void t1_ifnullFunc(
590   sqlite3_context *context,
591   int argc,
592   sqlite3_value **argv
593 ){
594   int i;
595   for(i=0; i<argc; i++){
596     if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
597       int n = sqlite3_value_bytes(argv[i]);
598       sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]),
599           n, SQLITE_TRANSIENT);
600       break;
601     }
602   }
603 }
604 
605 /*
606 ** These are test functions.    hex8() interprets its argument as
607 ** UTF8 and returns a hex encoding.  hex16le() interprets its argument
608 ** as UTF16le and returns a hex encoding.
609 */
610 static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){
611   const unsigned char *z;
612   int i;
613   char zBuf[200];
614   z = sqlite3_value_text(argv[0]);
615   for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){
616     sprintf(&zBuf[i*2], "%02x", z[i]&0xff);
617   }
618   zBuf[i*2] = 0;
619   sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
620 }
621 #ifndef SQLITE_OMIT_UTF16
622 static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){
623   const unsigned short int *z;
624   int i;
625   char zBuf[400];
626   z = sqlite3_value_text16(argv[0]);
627   for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){
628     sprintf(&zBuf[i*4], "%04x", z[i]&0xff);
629   }
630   zBuf[i*4] = 0;
631   sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
632 }
633 #endif
634 
635 /*
636 ** A structure into which to accumulate text.
637 */
638 struct dstr {
639   int nAlloc;  /* Space allocated */
640   int nUsed;   /* Space used */
641   char *z;     /* The space */
642 };
643 
644 /*
645 ** Append text to a dstr
646 */
647 static void dstrAppend(struct dstr *p, const char *z, int divider){
648   int n = strlen(z);
649   if( p->nUsed + n + 2 > p->nAlloc ){
650     char *zNew;
651     p->nAlloc = p->nAlloc*2 + n + 200;
652     zNew = sqliteRealloc(p->z, p->nAlloc);
653     if( zNew==0 ){
654       sqliteFree(p->z);
655       memset(p, 0, sizeof(*p));
656       return;
657     }
658     p->z = zNew;
659   }
660   if( divider && p->nUsed>0 ){
661     p->z[p->nUsed++] = divider;
662   }
663   memcpy(&p->z[p->nUsed], z, n+1);
664   p->nUsed += n;
665 }
666 
667 /*
668 ** Invoked for each callback from sqlite3ExecFunc
669 */
670 static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){
671   struct dstr *p = (struct dstr*)pData;
672   int i;
673   for(i=0; i<argc; i++){
674     if( argv[i]==0 ){
675       dstrAppend(p, "NULL", ' ');
676     }else{
677       dstrAppend(p, argv[i], ' ');
678     }
679   }
680   return 0;
681 }
682 
683 /*
684 ** Implementation of the x_sqlite_exec() function.  This function takes
685 ** a single argument and attempts to execute that argument as SQL code.
686 ** This is illegal and should set the SQLITE_MISUSE flag on the database.
687 **
688 ** 2004-Jan-07:  We have changed this to make it legal to call sqlite3_exec()
689 ** from within a function call.
690 **
691 ** This routine simulates the effect of having two threads attempt to
692 ** use the same database at the same time.
693 */
694 static void sqlite3ExecFunc(
695   sqlite3_context *context,
696   int argc,
697   sqlite3_value **argv
698 ){
699   struct dstr x;
700   memset(&x, 0, sizeof(x));
701   (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context),
702       (char*)sqlite3_value_text(argv[0]),
703       execFuncCallback, &x, 0);
704   sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT);
705   sqliteFree(x.z);
706 }
707 
708 /*
709 ** Implementation of tkt2213func(), a scalar function that takes exactly
710 ** one argument. It has two interesting features:
711 **
712 ** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*.
713 **   If the three pointers returned are not the same an SQL error is raised.
714 **
715 ** * Otherwise it returns a copy of the text representation of it's
716 **   argument in such a way as the VDBE representation is a Mem* cell
717 **   with the MEM_Term flag clear.
718 **
719 ** Ticket #2213 can therefore be tested by evaluating the following
720 ** SQL expression:
721 **
722 **   tkt2213func(tkt2213func('a string'));
723 */
724 static void tkt2213Function(
725   sqlite3_context *context,
726   int argc,
727   sqlite3_value **argv
728 ){
729   int nText;
730   unsigned char const *zText1;
731   unsigned char const *zText2;
732   unsigned char const *zText3;
733 
734   nText = sqlite3_value_bytes(argv[0]);
735   zText1 = sqlite3_value_text(argv[0]);
736   zText2 = sqlite3_value_text(argv[0]);
737   zText3 = sqlite3_value_text(argv[0]);
738 
739   if( zText1!=zText2 || zText2!=zText3 ){
740     sqlite3_result_error(context, "tkt2213 is not fixed", -1);
741   }else{
742     char *zCopy = (char *)sqlite3_malloc(nText);
743     memcpy(zCopy, zText1, nText);
744     sqlite3_result_text(context, zCopy, nText, sqlite3_free);
745   }
746 }
747 
748 /*
749 ** The following SQL function takes 4 arguments.  The 2nd and
750 ** 4th argument must be one of these strings:  'text', 'text16',
751 ** or 'blob' corresponding to API functions
752 **
753 **      sqlite3_value_text()
754 **      sqlite3_value_text16()
755 **      sqlite3_value_blob()
756 **
757 ** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
758 ** corresponding to APIs:
759 **
760 **      sqlite3_value_bytes()
761 **      sqlite3_value_bytes16()
762 **      noop
763 **
764 ** The APIs designated by the 2nd through 4th arguments are applied
765 ** to the first argument in order.  If the pointers returned by the
766 ** second and fourth are different, this routine returns 1.  Otherwise,
767 ** this routine returns 0.
768 **
769 ** This function is used to test to see when returned pointers from
770 ** the _text(), _text16() and _blob() APIs become invalidated.
771 */
772 static void ptrChngFunction(
773   sqlite3_context *context,
774   int argc,
775   sqlite3_value **argv
776 ){
777   const void *p1, *p2;
778   const char *zCmd;
779   if( argc!=4 ) return;
780   zCmd = (const char*)sqlite3_value_text(argv[1]);
781   if( zCmd==0 ) return;
782   if( strcmp(zCmd,"text")==0 ){
783     p1 = (const void*)sqlite3_value_text(argv[0]);
784 #ifndef SQLITE_OMIT_UTF16
785   }else if( strcmp(zCmd, "text16")==0 ){
786     p1 = (const void*)sqlite3_value_text16(argv[0]);
787 #endif
788   }else if( strcmp(zCmd, "blob")==0 ){
789     p1 = (const void*)sqlite3_value_blob(argv[0]);
790   }else{
791     return;
792   }
793   zCmd = (const char*)sqlite3_value_text(argv[2]);
794   if( zCmd==0 ) return;
795   if( strcmp(zCmd,"bytes")==0 ){
796     sqlite3_value_bytes(argv[0]);
797 #ifndef SQLITE_OMIT_UTF16
798   }else if( strcmp(zCmd, "bytes16")==0 ){
799     sqlite3_value_bytes16(argv[0]);
800 #endif
801   }else if( strcmp(zCmd, "noop")==0 ){
802     /* do nothing */
803   }else{
804     return;
805   }
806   zCmd = (const char*)sqlite3_value_text(argv[3]);
807   if( zCmd==0 ) return;
808   if( strcmp(zCmd,"text")==0 ){
809     p2 = (const void*)sqlite3_value_text(argv[0]);
810 #ifndef SQLITE_OMIT_UTF16
811   }else if( strcmp(zCmd, "text16")==0 ){
812     p2 = (const void*)sqlite3_value_text16(argv[0]);
813 #endif
814   }else if( strcmp(zCmd, "blob")==0 ){
815     p2 = (const void*)sqlite3_value_blob(argv[0]);
816   }else{
817     return;
818   }
819   sqlite3_result_int(context, p1!=p2);
820 }
821 
822 
823 /*
824 ** Usage:  sqlite_test_create_function DB
825 **
826 ** Call the sqlite3_create_function API on the given database in order
827 ** to create a function named "x_coalesce".  This function does the same thing
828 ** as the "coalesce" function.  This function also registers an SQL function
829 ** named "x_sqlite_exec" that invokes sqlite3_exec().  Invoking sqlite3_exec()
830 ** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
831 ** The effect is similar to trying to use the same database connection from
832 ** two threads at the same time.
833 **
834 ** The original motivation for this routine was to be able to call the
835 ** sqlite3_create_function function while a query is in progress in order
836 ** to test the SQLITE_MISUSE detection logic.
837 */
838 static int test_create_function(
839   void *NotUsed,
840   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
841   int argc,              /* Number of arguments */
842   char **argv            /* Text of each argument */
843 ){
844   int rc;
845   sqlite3 *db;
846   extern void Md5_Register(sqlite3*);
847 
848   if( argc!=2 ){
849     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
850        " DB\"", 0);
851     return TCL_ERROR;
852   }
853   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
854   rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0,
855         t1_ifnullFunc, 0, 0);
856   if( rc==SQLITE_OK ){
857     rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0,
858           hex8Func, 0, 0);
859   }
860 #ifndef SQLITE_OMIT_UTF16
861   if( rc==SQLITE_OK ){
862     rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0,
863           hex16Func, 0, 0);
864   }
865 #endif
866   if( rc==SQLITE_OK ){
867     rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0,
868           tkt2213Function, 0, 0);
869   }
870   if( rc==SQLITE_OK ){
871     rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0,
872           ptrChngFunction, 0, 0);
873   }
874 
875 #ifndef SQLITE_OMIT_UTF16
876   /* Use the sqlite3_create_function16() API here. Mainly for fun, but also
877   ** because it is not tested anywhere else. */
878   if( rc==SQLITE_OK ){
879     sqlite3_value *pVal;
880 #ifdef SQLITE_MEMDEBUG
881     if( sqlite3_iMallocFail>0 ){
882       sqlite3_iMallocFail++;
883     }
884 #endif
885     pVal = sqlite3ValueNew();
886     sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);
887     rc = sqlite3_create_function16(db,
888               sqlite3ValueText(pVal, SQLITE_UTF16NATIVE),
889               1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
890     sqlite3ValueFree(pVal);
891   }
892 #endif
893 
894   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
895   Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
896   return TCL_OK;
897 }
898 
899 /*
900 ** Routines to implement the x_count() aggregate function.
901 **
902 ** x_count() counts the number of non-null arguments.  But there are
903 ** some twists for testing purposes.
904 **
905 ** If the argument to x_count() is 40 then a UTF-8 error is reported
906 ** on the step function.  If x_count(41) is seen, then a UTF-16 error
907 ** is reported on the step function.  If the total count is 42, then
908 ** a UTF-8 error is reported on the finalize function.
909 */
910 typedef struct t1CountCtx t1CountCtx;
911 struct t1CountCtx {
912   int n;
913 };
914 static void t1CountStep(
915   sqlite3_context *context,
916   int argc,
917   sqlite3_value **argv
918 ){
919   t1CountCtx *p;
920   p = sqlite3_aggregate_context(context, sizeof(*p));
921   if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
922     p->n++;
923   }
924   if( argc>0 ){
925     int v = sqlite3_value_int(argv[0]);
926     if( v==40 ){
927       sqlite3_result_error(context, "value of 40 handed to x_count", -1);
928 #ifndef SQLITE_OMIT_UTF16
929     }else if( v==41 ){
930       const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
931       sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
932 #endif
933     }
934   }
935 }
936 static void t1CountFinalize(sqlite3_context *context){
937   t1CountCtx *p;
938   p = sqlite3_aggregate_context(context, sizeof(*p));
939   if( p ){
940     if( p->n==42 ){
941       sqlite3_result_error(context, "x_count totals to 42", -1);
942     }else{
943       sqlite3_result_int(context, p ? p->n : 0);
944     }
945   }
946 }
947 
948 /*
949 ** Usage:  sqlite_test_create_aggregate DB
950 **
951 ** Call the sqlite3_create_function API on the given database in order
952 ** to create a function named "x_count".  This function does the same thing
953 ** as the "md5sum" function.
954 **
955 ** The original motivation for this routine was to be able to call the
956 ** sqlite3_create_aggregate function while a query is in progress in order
957 ** to test the SQLITE_MISUSE detection logic.  See misuse.test.
958 **
959 ** This routine was later extended to test the use of sqlite3_result_error()
960 ** within aggregate functions.
961 */
962 static int test_create_aggregate(
963   void *NotUsed,
964   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
965   int argc,              /* Number of arguments */
966   char **argv            /* Text of each argument */
967 ){
968   sqlite3 *db;
969   int rc;
970   if( argc!=2 ){
971     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
972        " FILENAME\"", 0);
973     return TCL_ERROR;
974   }
975   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
976   rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
977       t1CountStep,t1CountFinalize);
978   if( rc==SQLITE_OK ){
979     sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
980         t1CountStep,t1CountFinalize);
981   }
982   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
983   return TCL_OK;
984 }
985 
986 
987 /*
988 ** Usage:  printf TEXT
989 **
990 ** Send output to printf.  Use this rather than puts to merge the output
991 ** in the correct sequence with debugging printfs inserted into C code.
992 ** Puts uses a separate buffer and debugging statements will be out of
993 ** sequence if it is used.
994 */
995 static int test_printf(
996   void *NotUsed,
997   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
998   int argc,              /* Number of arguments */
999   char **argv            /* Text of each argument */
1000 ){
1001   if( argc!=2 ){
1002     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1003        " TEXT\"", 0);
1004     return TCL_ERROR;
1005   }
1006   printf("%s\n", argv[1]);
1007   return TCL_OK;
1008 }
1009 
1010 
1011 
1012 /*
1013 ** Usage:  sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
1014 **
1015 ** Call mprintf with three integer arguments
1016 */
1017 static int sqlite3_mprintf_int(
1018   void *NotUsed,
1019   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1020   int argc,              /* Number of arguments */
1021   char **argv            /* Text of each argument */
1022 ){
1023   int a[3], i;
1024   char *z;
1025   if( argc!=5 ){
1026     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1027        " FORMAT INT INT INT\"", 0);
1028     return TCL_ERROR;
1029   }
1030   for(i=2; i<5; i++){
1031     if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1032   }
1033   z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1034   Tcl_AppendResult(interp, z, 0);
1035   sqlite3_free(z);
1036   return TCL_OK;
1037 }
1038 
1039 /*
1040 ** If zNum represents an integer that will fit in 64-bits, then set
1041 ** *pValue to that integer and return true.  Otherwise return false.
1042 */
1043 static int sqlite3GetInt64(const char *zNum, i64 *pValue){
1044   if( sqlite3FitsIn64Bits(zNum) ){
1045     sqlite3atoi64(zNum, pValue);
1046     return 1;
1047   }
1048   return 0;
1049 }
1050 
1051 /*
1052 ** Usage:  sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
1053 **
1054 ** Call mprintf with three 64-bit integer arguments
1055 */
1056 static int sqlite3_mprintf_int64(
1057   void *NotUsed,
1058   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1059   int argc,              /* Number of arguments */
1060   char **argv            /* Text of each argument */
1061 ){
1062   int i;
1063   sqlite_int64 a[3];
1064   char *z;
1065   if( argc!=5 ){
1066     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1067        " FORMAT INT INT INT\"", 0);
1068     return TCL_ERROR;
1069   }
1070   for(i=2; i<5; i++){
1071     if( !sqlite3GetInt64(argv[i], &a[i-2]) ){
1072       Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0);
1073       return TCL_ERROR;
1074     }
1075   }
1076   z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1077   Tcl_AppendResult(interp, z, 0);
1078   sqlite3_free(z);
1079   return TCL_OK;
1080 }
1081 
1082 /*
1083 ** Usage:  sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
1084 **
1085 ** Call mprintf with two integer arguments and one string argument
1086 */
1087 static int sqlite3_mprintf_str(
1088   void *NotUsed,
1089   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1090   int argc,              /* Number of arguments */
1091   char **argv            /* Text of each argument */
1092 ){
1093   int a[3], i;
1094   char *z;
1095   if( argc<4 || argc>5 ){
1096     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1097        " FORMAT INT INT ?STRING?\"", 0);
1098     return TCL_ERROR;
1099   }
1100   for(i=2; i<4; i++){
1101     if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1102   }
1103   z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL);
1104   Tcl_AppendResult(interp, z, 0);
1105   sqlite3_free(z);
1106   return TCL_OK;
1107 }
1108 
1109 /*
1110 ** Usage:  sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING
1111 **
1112 ** Call mprintf with two integer arguments and one string argument
1113 */
1114 static int sqlite3_snprintf_str(
1115   void *NotUsed,
1116   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1117   int argc,              /* Number of arguments */
1118   char **argv            /* Text of each argument */
1119 ){
1120   int a[3], i;
1121   int n;
1122   char *z;
1123   if( argc<5 || argc>6 ){
1124     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1125        " INT FORMAT INT INT ?STRING?\"", 0);
1126     return TCL_ERROR;
1127   }
1128   if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
1129   if( n<0 ){
1130     Tcl_AppendResult(interp, "N must be non-negative", 0);
1131     return TCL_ERROR;
1132   }
1133   for(i=3; i<5; i++){
1134     if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR;
1135   }
1136   z = sqlite3_malloc( n+1 );
1137   sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL);
1138   Tcl_AppendResult(interp, z, 0);
1139   sqlite3_free(z);
1140   return TCL_OK;
1141 }
1142 
1143 /*
1144 ** Usage:  sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE
1145 **
1146 ** Call mprintf with two integer arguments and one double argument
1147 */
1148 static int sqlite3_mprintf_double(
1149   void *NotUsed,
1150   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1151   int argc,              /* Number of arguments */
1152   char **argv            /* Text of each argument */
1153 ){
1154   int a[3], i;
1155   double r;
1156   char *z;
1157   if( argc!=5 ){
1158     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1159        " FORMAT INT INT DOUBLE\"", 0);
1160     return TCL_ERROR;
1161   }
1162   for(i=2; i<4; i++){
1163     if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1164   }
1165   if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;
1166   z = sqlite3_mprintf(argv[1], a[0], a[1], r);
1167   Tcl_AppendResult(interp, z, 0);
1168   sqlite3_free(z);
1169   return TCL_OK;
1170 }
1171 
1172 /*
1173 ** Usage:  sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE
1174 **
1175 ** Call mprintf with a single double argument which is the product of the
1176 ** two arguments given above.  This is used to generate overflow and underflow
1177 ** doubles to test that they are converted properly.
1178 */
1179 static int sqlite3_mprintf_scaled(
1180   void *NotUsed,
1181   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1182   int argc,              /* Number of arguments */
1183   char **argv            /* Text of each argument */
1184 ){
1185   int i;
1186   double r[2];
1187   char *z;
1188   if( argc!=4 ){
1189     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1190        " FORMAT DOUBLE DOUBLE\"", 0);
1191     return TCL_ERROR;
1192   }
1193   for(i=2; i<4; i++){
1194     if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR;
1195   }
1196   z = sqlite3_mprintf(argv[1], r[0]*r[1]);
1197   Tcl_AppendResult(interp, z, 0);
1198   sqlite3_free(z);
1199   return TCL_OK;
1200 }
1201 
1202 /*
1203 ** Usage:  sqlite3_mprintf_stronly FORMAT STRING
1204 **
1205 ** Call mprintf with a single double argument which is the product of the
1206 ** two arguments given above.  This is used to generate overflow and underflow
1207 ** doubles to test that they are converted properly.
1208 */
1209 static int sqlite3_mprintf_stronly(
1210   void *NotUsed,
1211   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1212   int argc,              /* Number of arguments */
1213   char **argv            /* Text of each argument */
1214 ){
1215   char *z;
1216   if( argc!=3 ){
1217     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1218        " FORMAT STRING\"", 0);
1219     return TCL_ERROR;
1220   }
1221   z = sqlite3_mprintf(argv[1], argv[2]);
1222   Tcl_AppendResult(interp, z, 0);
1223   sqlite3_free(z);
1224   return TCL_OK;
1225 }
1226 
1227 /*
1228 ** Usage:  sqlite3_mprintf_hexdouble FORMAT HEX
1229 **
1230 ** Call mprintf with a single double argument which is derived from the
1231 ** hexadecimal encoding of an IEEE double.
1232 */
1233 static int sqlite3_mprintf_hexdouble(
1234   void *NotUsed,
1235   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1236   int argc,              /* Number of arguments */
1237   char **argv            /* Text of each argument */
1238 ){
1239   char *z;
1240   double r;
1241   unsigned  x1, x2;
1242   long long unsigned d;
1243   if( argc!=3 ){
1244     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1245        " FORMAT STRING\"", 0);
1246     return TCL_ERROR;
1247   }
1248   if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){
1249     Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0);
1250     return TCL_ERROR;
1251   }
1252   d = x2;
1253   d = (d<<32) + x1;
1254   memcpy(&r, &d, sizeof(r));
1255   z = sqlite3_mprintf(argv[1], r);
1256   Tcl_AppendResult(interp, z, 0);
1257   sqlite3_free(z);
1258   return TCL_OK;
1259 }
1260 
1261 /*
1262 ** Usage: sqlite_malloc_fail N  ?REPEAT-INTERVAL?
1263 **
1264 ** Rig sqliteMalloc() to fail on the N-th call and every REPEAT-INTERVAL call
1265 ** after that.  If REPEAT-INTERVAL is 0 or is omitted, then only a single
1266 ** malloc will fail.  If REPEAT-INTERVAL is 1 then all mallocs after the
1267 ** first failure will continue to fail on every call.  If REPEAT-INTERVAL is
1268 ** 2 then every other malloc will fail.  And so forth.
1269 **
1270 ** Turn off this mechanism and reset the sqlite3ThreadData()->mallocFailed
1271 ** variable if N==0.
1272 */
1273 #ifdef SQLITE_MEMDEBUG
1274 static int sqlite_malloc_fail(
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 n;
1281   int rep;
1282   if( argc!=2 && argc!=3 ){
1283     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " N\"", 0);
1284     return TCL_ERROR;
1285   }
1286   if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
1287   if( argc==3 ){
1288     if( Tcl_GetInt(interp, argv[2], &rep) ) return TCL_ERROR;
1289   }else{
1290     rep = 0;
1291   }
1292   sqlite3_iMallocFail = n;
1293   sqlite3_iMallocReset = rep;
1294   return TCL_OK;
1295 }
1296 #endif
1297 
1298 /*
1299 ** Usage: sqlite_malloc_stat
1300 **
1301 ** Return the number of prior calls to sqliteMalloc() and sqliteFree().
1302 */
1303 #ifdef SQLITE_MEMDEBUG
1304 static int sqlite_malloc_stat(
1305   void *NotUsed,
1306   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1307   int argc,              /* Number of arguments */
1308   char **argv            /* Text of each argument */
1309 ){
1310   char zBuf[200];
1311   sprintf(zBuf, "%d %d %d", sqlite3_nMalloc,sqlite3_nFree,sqlite3_iMallocFail);
1312   Tcl_AppendResult(interp, zBuf, 0);
1313   return TCL_OK;
1314 }
1315 
1316 /*
1317 ** This function implements a Tcl command that may be invoked using any of
1318 ** the four forms enumerated below.
1319 **
1320 ** sqlite_malloc_outstanding
1321 **     Return a summary of all unfreed blocks of memory allocated by the
1322 **     current thread. See comments above function sqlite3OutstandingMallocs()
1323 **     in util.c for a description of the returned value.
1324 **
1325 ** sqlite_malloc_outstanding -bytes
1326 **     Return the total amount of unfreed memory (in bytes) allocated by
1327 **     this thread.
1328 **
1329 ** sqlite_malloc_outstanding -maxbytes
1330 **     Return the maximum amount of dynamic memory in use at one time
1331 **     by this thread.
1332 **
1333 ** sqlite_malloc_outstanding -clearmaxbytes
1334 **     Set the value returned by [sqlite_malloc_outstanding -maxbytes]
1335 **     to the current value of [sqlite_malloc_outstanding -bytes].
1336 */
1337 static int sqlite_malloc_outstanding(
1338   ClientData clientData,
1339   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1340   int objc,              /* Number of arguments */
1341   Tcl_Obj *CONST objv[]  /* Command arguments */
1342 ){
1343   extern int sqlite3OutstandingMallocs(Tcl_Interp *interp);
1344 
1345 #if defined(SQLITE_DEBUG) && defined(SQLITE_MEMDEBUG) && SQLITE_MEMDEBUG>1
1346   if( objc==2 ){
1347     const char *zArg = Tcl_GetString(objv[1]);
1348 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
1349     ThreadData const *pTd = sqlite3ThreadDataReadOnly();
1350     if( 0==strcmp(zArg, "-bytes") ){
1351       Tcl_SetObjResult(interp, Tcl_NewIntObj(pTd->nAlloc));
1352     }else if( 0==strcmp(zArg, "-clearmaxbytes") ){
1353       sqlite3_nMaxAlloc = pTd->nAlloc;
1354     }else
1355 #endif
1356     if( 0==strcmp(zArg, "-maxbytes") ){
1357       Tcl_SetObjResult(interp, Tcl_NewWideIntObj(sqlite3_nMaxAlloc));
1358     }else{
1359       Tcl_AppendResult(interp, "bad option \"", zArg,
1360         "\": must be -bytes, -maxbytes or -clearmaxbytes", 0
1361       );
1362       return TCL_ERROR;
1363     }
1364 
1365     return TCL_OK;
1366   }
1367 
1368   if( objc!=1 ){
1369     Tcl_WrongNumArgs(interp, 1, objv, "?-bytes?");
1370     return TCL_ERROR;
1371   }
1372 
1373   return sqlite3OutstandingMallocs(interp);
1374 #else
1375   return TCL_OK;
1376 #endif
1377 }
1378 #endif
1379 
1380 /*
1381 ** Usage: sqlite3_enable_shared_cache      BOOLEAN
1382 **
1383 */
1384 #if !defined(SQLITE_OMIT_SHARED_CACHE)
1385 static int test_enable_shared(
1386   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1387   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1388   int objc,              /* Number of arguments */
1389   Tcl_Obj *CONST objv[]  /* Command arguments */
1390 ){
1391   int rc;
1392   int enable;
1393   int ret = 0;
1394 
1395   if( objc!=2 ){
1396     Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN");
1397     return TCL_ERROR;
1398   }
1399   if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){
1400     return TCL_ERROR;
1401   }
1402   ret = sqlite3ThreadDataReadOnly()->useSharedData;
1403   rc = sqlite3_enable_shared_cache(enable);
1404   if( rc!=SQLITE_OK ){
1405     Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
1406     return TCL_ERROR;
1407   }
1408   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret));
1409   return TCL_OK;
1410 }
1411 #endif
1412 
1413 
1414 
1415 /*
1416 ** Usage: sqlite3_extended_result_codes   DB    BOOLEAN
1417 **
1418 */
1419 static int test_extended_result_codes(
1420   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1421   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1422   int objc,              /* Number of arguments */
1423   Tcl_Obj *CONST objv[]  /* Command arguments */
1424 ){
1425   int enable;
1426   sqlite3 *db;
1427 
1428   if( objc!=3 ){
1429     Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
1430     return TCL_ERROR;
1431   }
1432   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1433   if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR;
1434   sqlite3_extended_result_codes(db, enable);
1435   return TCL_OK;
1436 }
1437 
1438 /*
1439 ** Usage: sqlite3_libversion_number
1440 **
1441 */
1442 static int test_libversion_number(
1443   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1444   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1445   int objc,              /* Number of arguments */
1446   Tcl_Obj *CONST objv[]  /* Command arguments */
1447 ){
1448   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number()));
1449   return TCL_OK;
1450 }
1451 
1452 /*
1453 ** Usage: sqlite3_table_column_metadata DB dbname tblname colname
1454 **
1455 */
1456 #ifdef SQLITE_ENABLE_COLUMN_METADATA
1457 static int test_table_column_metadata(
1458   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1459   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1460   int objc,              /* Number of arguments */
1461   Tcl_Obj *CONST objv[]  /* Command arguments */
1462 ){
1463   sqlite3 *db;
1464   const char *zDb;
1465   const char *zTbl;
1466   const char *zCol;
1467   int rc;
1468   Tcl_Obj *pRet;
1469 
1470   const char *zDatatype;
1471   const char *zCollseq;
1472   int notnull;
1473   int primarykey;
1474   int autoincrement;
1475 
1476   if( objc!=5 ){
1477     Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname");
1478     return TCL_ERROR;
1479   }
1480   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1481   zDb = Tcl_GetString(objv[2]);
1482   zTbl = Tcl_GetString(objv[3]);
1483   zCol = Tcl_GetString(objv[4]);
1484 
1485   if( strlen(zDb)==0 ) zDb = 0;
1486 
1487   rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol,
1488       &zDatatype, &zCollseq, &notnull, &primarykey, &autoincrement);
1489 
1490   if( rc!=SQLITE_OK ){
1491     Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
1492     return TCL_ERROR;
1493   }
1494 
1495   pRet = Tcl_NewObj();
1496   Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1));
1497   Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1));
1498   Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull));
1499   Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey));
1500   Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement));
1501   Tcl_SetObjResult(interp, pRet);
1502 
1503   return TCL_OK;
1504 }
1505 #endif
1506 
1507 #ifndef SQLITE_OMIT_INCRBLOB
1508 
1509 /*
1510 ** sqlite3_blob_read  CHANNEL OFFSET N
1511 */
1512 static int test_blob_read(
1513   ClientData clientData, /* Not used */
1514   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1515   int objc,              /* Number of arguments */
1516   Tcl_Obj *CONST objv[]  /* Command arguments */
1517 ){
1518   Tcl_Channel channel;
1519   ClientData instanceData;
1520   sqlite3_blob *pBlob;
1521   int notUsed;
1522   int nByte;
1523   int iOffset;
1524   unsigned char *zBuf;
1525   int rc;
1526 
1527   if( objc!=4 ){
1528     Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET N");
1529     return TCL_ERROR;
1530   }
1531 
1532   channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), &notUsed);
1533   if( !channel
1534    || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset)
1535    || TCL_OK!=Tcl_GetIntFromObj(interp, objv[3], &nByte)
1536    || nByte<0 || iOffset<0
1537   ){
1538     return TCL_ERROR;
1539   }
1540 
1541   instanceData = Tcl_GetChannelInstanceData(channel);
1542   pBlob = *((sqlite3_blob **)instanceData);
1543 
1544   zBuf = (unsigned char *)Tcl_Alloc(nByte);
1545   rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset);
1546   if( rc==SQLITE_OK ){
1547     Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zBuf, nByte));
1548   }else{
1549     Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
1550   }
1551   Tcl_Free((char *)zBuf);
1552 
1553   return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1554 }
1555 
1556 /*
1557 ** sqlite3_blob_write CHANNEL OFFSET DATA
1558 */
1559 static int test_blob_write(
1560   ClientData clientData, /* Not used */
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   Tcl_Channel channel;
1566   ClientData instanceData;
1567   sqlite3_blob *pBlob;
1568   int notUsed;
1569   int iOffset;
1570   int rc;
1571 
1572   unsigned char *zBuf;
1573   int nBuf;
1574 
1575   if( objc!=4 ){
1576     Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET DATA");
1577     return TCL_ERROR;
1578   }
1579 
1580   channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), &notUsed);
1581   if( !channel
1582    || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset)
1583    || iOffset<0
1584   ){
1585     return TCL_ERROR;
1586   }
1587 
1588   instanceData = Tcl_GetChannelInstanceData(channel);
1589   pBlob = *((sqlite3_blob **)instanceData);
1590 
1591   zBuf = Tcl_GetByteArrayFromObj(objv[3], &nBuf);
1592   rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset);
1593   if( rc!=SQLITE_OK ){
1594     Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
1595   }
1596 
1597   return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1598 }
1599 #endif
1600 
1601 /*
1602 ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
1603 */
1604 static int test_load_extension(
1605   ClientData clientData, /* Not used */
1606   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1607   int objc,              /* Number of arguments */
1608   Tcl_Obj *CONST objv[]  /* Command arguments */
1609 ){
1610   Tcl_CmdInfo cmdInfo;
1611   sqlite3 *db;
1612   int rc;
1613   char *zDb;
1614   char *zFile;
1615   char *zProc = 0;
1616   char *zErr = 0;
1617 
1618   if( objc!=4 && objc!=3 ){
1619     Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?");
1620     return TCL_ERROR;
1621   }
1622   zDb = Tcl_GetString(objv[1]);
1623   zFile = Tcl_GetString(objv[2]);
1624   if( objc==4 ){
1625     zProc = Tcl_GetString(objv[3]);
1626   }
1627 
1628   /* Extract the C database handle from the Tcl command name */
1629   if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1630     Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1631     return TCL_ERROR;
1632   }
1633   db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1634   assert(db);
1635 
1636   /* Call the underlying C function. If an error occurs, set rc to
1637   ** TCL_ERROR and load any error string into the interpreter. If no
1638   ** error occurs, set rc to TCL_OK.
1639   */
1640 #ifdef SQLITE_OMIT_LOAD_EXTENSION
1641   rc = SQLITE_ERROR;
1642   zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()");
1643 #else
1644   rc = sqlite3_load_extension(db, zFile, zProc, &zErr);
1645 #endif
1646   if( rc!=SQLITE_OK ){
1647     Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE);
1648     rc = TCL_ERROR;
1649   }else{
1650     rc = TCL_OK;
1651   }
1652   sqlite3_free(zErr);
1653 
1654   return rc;
1655 }
1656 
1657 /*
1658 ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
1659 */
1660 static int test_enable_load(
1661   ClientData clientData, /* Not used */
1662   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1663   int objc,              /* Number of arguments */
1664   Tcl_Obj *CONST objv[]  /* Command arguments */
1665 ){
1666   Tcl_CmdInfo cmdInfo;
1667   sqlite3 *db;
1668   char *zDb;
1669   int onoff;
1670 
1671   if( objc!=3 ){
1672     Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
1673     return TCL_ERROR;
1674   }
1675   zDb = Tcl_GetString(objv[1]);
1676 
1677   /* Extract the C database handle from the Tcl command name */
1678   if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1679     Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1680     return TCL_ERROR;
1681   }
1682   db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1683   assert(db);
1684 
1685   /* Get the onoff parameter */
1686   if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
1687     return TCL_ERROR;
1688   }
1689 
1690 #ifdef SQLITE_OMIT_LOAD_EXTENSION
1691   Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
1692   return TCL_ERROR;
1693 #else
1694   sqlite3_enable_load_extension(db, onoff);
1695   return TCL_OK;
1696 #endif
1697 }
1698 
1699 /*
1700 ** Usage:  sqlite_abort
1701 **
1702 ** Shutdown the process immediately.  This is not a clean shutdown.
1703 ** This command is used to test the recoverability of a database in
1704 ** the event of a program crash.
1705 */
1706 static int sqlite_abort(
1707   void *NotUsed,
1708   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1709   int argc,              /* Number of arguments */
1710   char **argv            /* Text of each argument */
1711 ){
1712   assert( interp==0 );   /* This will always fail */
1713   return TCL_OK;
1714 }
1715 
1716 /*
1717 ** The following routine is a user-defined SQL function whose purpose
1718 ** is to test the sqlite_set_result() API.
1719 */
1720 static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1721   while( argc>=2 ){
1722     const char *zArg0 = (char*)sqlite3_value_text(argv[0]);
1723     if( zArg0 ){
1724       if( 0==sqlite3StrICmp(zArg0, "int") ){
1725         sqlite3_result_int(context, sqlite3_value_int(argv[1]));
1726       }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
1727         sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
1728       }else if( sqlite3StrICmp(zArg0,"string")==0 ){
1729         sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1,
1730             SQLITE_TRANSIENT);
1731       }else if( sqlite3StrICmp(zArg0,"double")==0 ){
1732         sqlite3_result_double(context, sqlite3_value_double(argv[1]));
1733       }else if( sqlite3StrICmp(zArg0,"null")==0 ){
1734         sqlite3_result_null(context);
1735       }else if( sqlite3StrICmp(zArg0,"value")==0 ){
1736         sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
1737       }else{
1738         goto error_out;
1739       }
1740     }else{
1741       goto error_out;
1742     }
1743     argc -= 2;
1744     argv += 2;
1745   }
1746   return;
1747 
1748 error_out:
1749   sqlite3_result_error(context,"first argument should be one of: "
1750       "int int64 string double null value", -1);
1751 }
1752 
1753 /*
1754 ** Usage:   sqlite_register_test_function  DB  NAME
1755 **
1756 ** Register the test SQL function on the database DB under the name NAME.
1757 */
1758 static int test_register_func(
1759   void *NotUsed,
1760   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1761   int argc,              /* Number of arguments */
1762   char **argv            /* Text of each argument */
1763 ){
1764   sqlite3 *db;
1765   int rc;
1766   if( argc!=3 ){
1767     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1768        " DB FUNCTION-NAME", 0);
1769     return TCL_ERROR;
1770   }
1771   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1772   rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,
1773       testFunc, 0, 0);
1774   if( rc!=0 ){
1775     Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
1776     return TCL_ERROR;
1777   }
1778   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1779   return TCL_OK;
1780 }
1781 
1782 /*
1783 ** Usage:  sqlite3_finalize  STMT
1784 **
1785 ** Finalize a statement handle.
1786 */
1787 static int test_finalize(
1788   void * clientData,
1789   Tcl_Interp *interp,
1790   int objc,
1791   Tcl_Obj *CONST objv[]
1792 ){
1793   sqlite3_stmt *pStmt;
1794   int rc;
1795   sqlite3 *db = 0;
1796 
1797   if( objc!=2 ){
1798     Tcl_AppendResult(interp, "wrong # args: should be \"",
1799         Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
1800     return TCL_ERROR;
1801   }
1802 
1803   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1804 
1805   if( pStmt ){
1806     db = StmtToDb(pStmt);
1807   }
1808   rc = sqlite3_finalize(pStmt);
1809   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
1810   if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1811   return TCL_OK;
1812 }
1813 
1814 /*
1815 ** Usage:  sqlite3_reset  STMT
1816 **
1817 ** Reset a statement handle.
1818 */
1819 static int test_reset(
1820   void * clientData,
1821   Tcl_Interp *interp,
1822   int objc,
1823   Tcl_Obj *CONST objv[]
1824 ){
1825   sqlite3_stmt *pStmt;
1826   int rc;
1827 
1828   if( objc!=2 ){
1829     Tcl_AppendResult(interp, "wrong # args: should be \"",
1830         Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
1831     return TCL_ERROR;
1832   }
1833 
1834   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1835 
1836   rc = sqlite3_reset(pStmt);
1837   if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
1838     return TCL_ERROR;
1839   }
1840   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
1841 /*
1842   if( rc ){
1843     return TCL_ERROR;
1844   }
1845 */
1846   return TCL_OK;
1847 }
1848 
1849 /*
1850 ** Usage:  sqlite3_expired STMT
1851 **
1852 ** Return TRUE if a recompilation of the statement is recommended.
1853 */
1854 static int test_expired(
1855   void * clientData,
1856   Tcl_Interp *interp,
1857   int objc,
1858   Tcl_Obj *CONST objv[]
1859 ){
1860   sqlite3_stmt *pStmt;
1861   if( objc!=2 ){
1862     Tcl_AppendResult(interp, "wrong # args: should be \"",
1863         Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
1864     return TCL_ERROR;
1865   }
1866   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1867   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
1868   return TCL_OK;
1869 }
1870 
1871 /*
1872 ** Usage:  sqlite3_transfer_bindings FROMSTMT TOSTMT
1873 **
1874 ** Transfer all bindings from FROMSTMT over to TOSTMT
1875 */
1876 static int test_transfer_bind(
1877   void * clientData,
1878   Tcl_Interp *interp,
1879   int objc,
1880   Tcl_Obj *CONST objv[]
1881 ){
1882   sqlite3_stmt *pStmt1, *pStmt2;
1883   if( objc!=3 ){
1884     Tcl_AppendResult(interp, "wrong # args: should be \"",
1885         Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
1886     return TCL_ERROR;
1887   }
1888   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
1889   if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
1890   Tcl_SetObjResult(interp,
1891      Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
1892   return TCL_OK;
1893 }
1894 
1895 /*
1896 ** Usage:  sqlite3_changes DB
1897 **
1898 ** Return the number of changes made to the database by the last SQL
1899 ** execution.
1900 */
1901 static int test_changes(
1902   void * clientData,
1903   Tcl_Interp *interp,
1904   int objc,
1905   Tcl_Obj *CONST objv[]
1906 ){
1907   sqlite3 *db;
1908   if( objc!=2 ){
1909     Tcl_AppendResult(interp, "wrong # args: should be \"",
1910        Tcl_GetString(objv[0]), " DB", 0);
1911     return TCL_ERROR;
1912   }
1913   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1914   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
1915   return TCL_OK;
1916 }
1917 
1918 /*
1919 ** This is the "static_bind_value" that variables are bound to when
1920 ** the FLAG option of sqlite3_bind is "static"
1921 */
1922 static char *sqlite_static_bind_value = 0;
1923 static int sqlite_static_bind_nbyte = 0;
1924 
1925 /*
1926 ** Usage:  sqlite3_bind  VM  IDX  VALUE  FLAGS
1927 **
1928 ** Sets the value of the IDX-th occurance of "?" in the original SQL
1929 ** string.  VALUE is the new value.  If FLAGS=="null" then VALUE is
1930 ** ignored and the value is set to NULL.  If FLAGS=="static" then
1931 ** the value is set to the value of a static variable named
1932 ** "sqlite_static_bind_value".  If FLAGS=="normal" then a copy
1933 ** of the VALUE is made.  If FLAGS=="blob10" then a VALUE is ignored
1934 ** an a 10-byte blob "abc\000xyz\000pq" is inserted.
1935 */
1936 static int test_bind(
1937   void *NotUsed,
1938   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1939   int argc,              /* Number of arguments */
1940   char **argv            /* Text of each argument */
1941 ){
1942   sqlite3_stmt *pStmt;
1943   int rc;
1944   int idx;
1945   if( argc!=5 ){
1946     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1947        " VM IDX VALUE (null|static|normal)\"", 0);
1948     return TCL_ERROR;
1949   }
1950   if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
1951   if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
1952   if( strcmp(argv[4],"null")==0 ){
1953     rc = sqlite3_bind_null(pStmt, idx);
1954   }else if( strcmp(argv[4],"static")==0 ){
1955     rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
1956   }else if( strcmp(argv[4],"static-nbytes")==0 ){
1957     rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value,
1958                                        sqlite_static_bind_nbyte, 0);
1959   }else if( strcmp(argv[4],"normal")==0 ){
1960     rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
1961   }else if( strcmp(argv[4],"blob10")==0 ){
1962     rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC);
1963   }else{
1964     Tcl_AppendResult(interp, "4th argument should be "
1965         "\"null\" or \"static\" or \"normal\"", 0);
1966     return TCL_ERROR;
1967   }
1968   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
1969   if( rc ){
1970     char zBuf[50];
1971     sprintf(zBuf, "(%d) ", rc);
1972     Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
1973     return TCL_ERROR;
1974   }
1975   return TCL_OK;
1976 }
1977 
1978 #ifndef SQLITE_OMIT_UTF16
1979 /*
1980 ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
1981 **
1982 ** This function is used to test that SQLite selects the correct collation
1983 ** sequence callback when multiple versions (for different text encodings)
1984 ** are available.
1985 **
1986 ** Calling this routine registers the collation sequence "test_collate"
1987 ** with database handle <db>. The second argument must be a list of three
1988 ** boolean values. If the first is true, then a version of test_collate is
1989 ** registered for UTF-8, if the second is true, a version is registered for
1990 ** UTF-16le, if the third is true, a UTF-16be version is available.
1991 ** Previous versions of test_collate are deleted.
1992 **
1993 ** The collation sequence test_collate is implemented by calling the
1994 ** following TCL script:
1995 **
1996 **   "test_collate <enc> <lhs> <rhs>"
1997 **
1998 ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
1999 ** The <enc> parameter is the encoding of the collation function that
2000 ** SQLite selected to call. The TCL test script implements the
2001 ** "test_collate" proc.
2002 **
2003 ** Note that this will only work with one intepreter at a time, as the
2004 ** interp pointer to use when evaluating the TCL script is stored in
2005 ** pTestCollateInterp.
2006 */
2007 static Tcl_Interp* pTestCollateInterp;
2008 static int test_collate_func(
2009   void *pCtx,
2010   int nA, const void *zA,
2011   int nB, const void *zB
2012 ){
2013   Tcl_Interp *i = pTestCollateInterp;
2014   int encin = (int)pCtx;
2015   int res;
2016   int n;
2017 
2018   sqlite3_value *pVal;
2019   Tcl_Obj *pX;
2020 
2021   pX = Tcl_NewStringObj("test_collate", -1);
2022   Tcl_IncrRefCount(pX);
2023 
2024   switch( encin ){
2025     case SQLITE_UTF8:
2026       Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
2027       break;
2028     case SQLITE_UTF16LE:
2029       Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
2030       break;
2031     case SQLITE_UTF16BE:
2032       Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
2033       break;
2034     default:
2035       assert(0);
2036   }
2037 
2038   pVal = sqlite3ValueNew();
2039   sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
2040   n = sqlite3_value_bytes(pVal);
2041   Tcl_ListObjAppendElement(i,pX,
2042       Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
2043   sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
2044   n = sqlite3_value_bytes(pVal);
2045   Tcl_ListObjAppendElement(i,pX,
2046       Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
2047   sqlite3ValueFree(pVal);
2048 
2049   Tcl_EvalObjEx(i, pX, 0);
2050   Tcl_DecrRefCount(pX);
2051   Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
2052   return res;
2053 }
2054 static int test_collate(
2055   void * clientData,
2056   Tcl_Interp *interp,
2057   int objc,
2058   Tcl_Obj *CONST objv[]
2059 ){
2060   sqlite3 *db;
2061   int val;
2062   sqlite3_value *pVal;
2063   int rc;
2064 
2065   if( objc!=5 ) goto bad_args;
2066   pTestCollateInterp = interp;
2067   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2068 
2069   if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
2070   rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
2071           (void *)SQLITE_UTF8, val?test_collate_func:0);
2072   if( rc==SQLITE_OK ){
2073     if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2074     rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
2075             (void *)SQLITE_UTF16LE, val?test_collate_func:0);
2076     if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
2077 
2078 #ifdef SQLITE_MEMDEBUG
2079     if( sqlite3_iMallocFail>0 ){
2080       sqlite3_iMallocFail++;
2081     }
2082 #endif
2083     pVal = sqlite3ValueNew();
2084     sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
2085     rc = sqlite3_create_collation16(db,
2086           sqlite3ValueText(pVal, SQLITE_UTF16NATIVE), SQLITE_UTF16BE,
2087           (void *)SQLITE_UTF16BE, val?test_collate_func:0);
2088     sqlite3ValueFree(pVal);
2089   }
2090   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2091 
2092   if( rc!=SQLITE_OK ){
2093     Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
2094     return TCL_ERROR;
2095   }
2096   return TCL_OK;
2097 
2098 bad_args:
2099   Tcl_AppendResult(interp, "wrong # args: should be \"",
2100       Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
2101   return TCL_ERROR;
2102 }
2103 
2104 /*
2105 ** When the collation needed callback is invoked, record the name of
2106 ** the requested collating function here.  The recorded name is linked
2107 ** to a TCL variable and used to make sure that the requested collation
2108 ** name is correct.
2109 */
2110 static char zNeededCollation[200];
2111 static char *pzNeededCollation = zNeededCollation;
2112 
2113 
2114 /*
2115 ** Called when a collating sequence is needed.  Registered using
2116 ** sqlite3_collation_needed16().
2117 */
2118 static void test_collate_needed_cb(
2119   void *pCtx,
2120   sqlite3 *db,
2121   int eTextRep,
2122   const void *pName
2123 ){
2124   int enc = ENC(db);
2125   int i;
2126   char *z;
2127   for(z = (char*)pName, i=0; *z || z[1]; z++){
2128     if( *z ) zNeededCollation[i++] = *z;
2129   }
2130   zNeededCollation[i] = 0;
2131   sqlite3_create_collation(
2132       db, "test_collate", ENC(db), (void *)enc, test_collate_func);
2133 }
2134 
2135 /*
2136 ** Usage: add_test_collate_needed DB
2137 */
2138 static int test_collate_needed(
2139   void * clientData,
2140   Tcl_Interp *interp,
2141   int objc,
2142   Tcl_Obj *CONST objv[]
2143 ){
2144   sqlite3 *db;
2145   int rc;
2146 
2147   if( objc!=2 ) goto bad_args;
2148   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2149   rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
2150   zNeededCollation[0] = 0;
2151   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2152   return TCL_OK;
2153 
2154 bad_args:
2155   Tcl_WrongNumArgs(interp, 1, objv, "DB");
2156   return TCL_ERROR;
2157 }
2158 
2159 /*
2160 ** tclcmd:   add_alignment_test_collations  DB
2161 **
2162 ** Add two new collating sequences to the database DB
2163 **
2164 **     utf16_aligned
2165 **     utf16_unaligned
2166 **
2167 ** Both collating sequences use the same sort order as BINARY.
2168 ** The only difference is that the utf16_aligned collating
2169 ** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
2170 ** Both collating functions increment the unaligned utf16 counter
2171 ** whenever they see a string that begins on an odd byte boundary.
2172 */
2173 static int unaligned_string_counter = 0;
2174 static int alignmentCollFunc(
2175   void *NotUsed,
2176   int nKey1, const void *pKey1,
2177   int nKey2, const void *pKey2
2178 ){
2179   int rc, n;
2180   n = nKey1<nKey2 ? nKey1 : nKey2;
2181   if( nKey1>0 && 1==(1&(int)pKey1) ) unaligned_string_counter++;
2182   if( nKey2>0 && 1==(1&(int)pKey2) ) unaligned_string_counter++;
2183   rc = memcmp(pKey1, pKey2, n);
2184   if( rc==0 ){
2185     rc = nKey1 - nKey2;
2186   }
2187   return rc;
2188 }
2189 static int add_alignment_test_collations(
2190   void * clientData,
2191   Tcl_Interp *interp,
2192   int objc,
2193   Tcl_Obj *CONST objv[]
2194 ){
2195   sqlite3 *db;
2196   if( objc>=2 ){
2197     if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2198     sqlite3_create_collation(db, "utf16_unaligned",
2199         SQLITE_UTF16,
2200         0, alignmentCollFunc);
2201     sqlite3_create_collation(db, "utf16_aligned",
2202         SQLITE_UTF16 | SQLITE_UTF16_ALIGNED,
2203         0, alignmentCollFunc);
2204   }
2205   return SQLITE_OK;
2206 }
2207 #endif /* !defined(SQLITE_OMIT_UTF16) */
2208 
2209 /*
2210 ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
2211 **
2212 ** This function is used to test that SQLite selects the correct user
2213 ** function callback when multiple versions (for different text encodings)
2214 ** are available.
2215 **
2216 ** Calling this routine registers up to three versions of the user function
2217 ** "test_function" with database handle <db>.  If the second argument is
2218 ** true, then a version of test_function is registered for UTF-8, if the
2219 ** third is true, a version is registered for UTF-16le, if the fourth is
2220 ** true, a UTF-16be version is available.  Previous versions of
2221 ** test_function are deleted.
2222 **
2223 ** The user function is implemented by calling the following TCL script:
2224 **
2225 **   "test_function <enc> <arg>"
2226 **
2227 ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
2228 ** single argument passed to the SQL function. The value returned by
2229 ** the TCL script is used as the return value of the SQL function. It
2230 ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
2231 ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
2232 ** prefers UTF-16BE.
2233 */
2234 #ifndef SQLITE_OMIT_UTF16
2235 static void test_function_utf8(
2236   sqlite3_context *pCtx,
2237   int nArg,
2238   sqlite3_value **argv
2239 ){
2240   Tcl_Interp *interp;
2241   Tcl_Obj *pX;
2242   sqlite3_value *pVal;
2243   interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2244   pX = Tcl_NewStringObj("test_function", -1);
2245   Tcl_IncrRefCount(pX);
2246   Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
2247   Tcl_ListObjAppendElement(interp, pX,
2248       Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
2249   Tcl_EvalObjEx(interp, pX, 0);
2250   Tcl_DecrRefCount(pX);
2251   sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
2252   pVal = sqlite3ValueNew();
2253   sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
2254       SQLITE_UTF8, SQLITE_STATIC);
2255   sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
2256       -1, SQLITE_TRANSIENT);
2257   sqlite3ValueFree(pVal);
2258 }
2259 static void test_function_utf16le(
2260   sqlite3_context *pCtx,
2261   int nArg,
2262   sqlite3_value **argv
2263 ){
2264   Tcl_Interp *interp;
2265   Tcl_Obj *pX;
2266   sqlite3_value *pVal;
2267   interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2268   pX = Tcl_NewStringObj("test_function", -1);
2269   Tcl_IncrRefCount(pX);
2270   Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
2271   Tcl_ListObjAppendElement(interp, pX,
2272       Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
2273   Tcl_EvalObjEx(interp, pX, 0);
2274   Tcl_DecrRefCount(pX);
2275   pVal = sqlite3ValueNew();
2276   sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
2277       SQLITE_UTF8, SQLITE_STATIC);
2278   sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
2279   sqlite3ValueFree(pVal);
2280 }
2281 static void test_function_utf16be(
2282   sqlite3_context *pCtx,
2283   int nArg,
2284   sqlite3_value **argv
2285 ){
2286   Tcl_Interp *interp;
2287   Tcl_Obj *pX;
2288   sqlite3_value *pVal;
2289   interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2290   pX = Tcl_NewStringObj("test_function", -1);
2291   Tcl_IncrRefCount(pX);
2292   Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
2293   Tcl_ListObjAppendElement(interp, pX,
2294       Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
2295   Tcl_EvalObjEx(interp, pX, 0);
2296   Tcl_DecrRefCount(pX);
2297   pVal = sqlite3ValueNew();
2298   sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
2299       SQLITE_UTF8, SQLITE_STATIC);
2300   sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
2301       -1, SQLITE_TRANSIENT);
2302   sqlite3ValueFree(pVal);
2303 }
2304 #endif /* SQLITE_OMIT_UTF16 */
2305 static int test_function(
2306   void * clientData,
2307   Tcl_Interp *interp,
2308   int objc,
2309   Tcl_Obj *CONST objv[]
2310 ){
2311 #ifndef SQLITE_OMIT_UTF16
2312   sqlite3 *db;
2313   int val;
2314 
2315   if( objc!=5 ) goto bad_args;
2316   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2317 
2318   if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
2319   if( val ){
2320     sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
2321         interp, test_function_utf8, 0, 0);
2322   }
2323   if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2324   if( val ){
2325     sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
2326         interp, test_function_utf16le, 0, 0);
2327   }
2328   if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
2329   if( val ){
2330     sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
2331         interp, test_function_utf16be, 0, 0);
2332   }
2333 
2334   return TCL_OK;
2335 bad_args:
2336   Tcl_AppendResult(interp, "wrong # args: should be \"",
2337       Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
2338 #endif /* SQLITE_OMIT_UTF16 */
2339   return TCL_ERROR;
2340 }
2341 
2342 /*
2343 ** Usage:         test_errstr <err code>
2344 **
2345 ** Test that the english language string equivalents for sqlite error codes
2346 ** are sane. The parameter is an integer representing an sqlite error code.
2347 ** The result is a list of two elements, the string representation of the
2348 ** error code and the english language explanation.
2349 */
2350 static int test_errstr(
2351   void * clientData,
2352   Tcl_Interp *interp,
2353   int objc,
2354   Tcl_Obj *CONST objv[]
2355 ){
2356   char *zCode;
2357   int i;
2358   if( objc!=1 ){
2359     Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
2360   }
2361 
2362   zCode = Tcl_GetString(objv[1]);
2363   for(i=0; i<200; i++){
2364     if( 0==strcmp(t1ErrorName(i), zCode) ) break;
2365   }
2366   Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
2367   return TCL_OK;
2368 }
2369 
2370 /*
2371 ** Usage:    breakpoint
2372 **
2373 ** This routine exists for one purpose - to provide a place to put a
2374 ** breakpoint with GDB that can be triggered using TCL code.  The use
2375 ** for this is when a particular test fails on (say) the 1485th iteration.
2376 ** In the TCL test script, we can add code like this:
2377 **
2378 **     if {$i==1485} breakpoint
2379 **
2380 ** Then run testfixture in the debugger and wait for the breakpoint to
2381 ** fire.  Then additional breakpoints can be set to trace down the bug.
2382 */
2383 static int test_breakpoint(
2384   void *NotUsed,
2385   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
2386   int argc,              /* Number of arguments */
2387   char **argv            /* Text of each argument */
2388 ){
2389   return TCL_OK;         /* Do nothing */
2390 }
2391 
2392 /*
2393 ** Usage:   sqlite3_bind_zeroblob  STMT IDX N
2394 **
2395 ** Test the sqlite3_bind_zeroblob interface.  STMT is a prepared statement.
2396 ** IDX is the index of a wildcard in the prepared statement.  This command
2397 ** binds a N-byte zero-filled BLOB to the wildcard.
2398 */
2399 static int test_bind_zeroblob(
2400   void * clientData,
2401   Tcl_Interp *interp,
2402   int objc,
2403   Tcl_Obj *CONST objv[]
2404 ){
2405   sqlite3_stmt *pStmt;
2406   int idx;
2407   int n;
2408   int rc;
2409 
2410   if( objc!=4 ){
2411     Tcl_AppendResult(interp, "wrong # args: should be \"",
2412         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
2413     return TCL_ERROR;
2414   }
2415 
2416   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2417   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2418   if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
2419 
2420   rc = sqlite3_bind_zeroblob(pStmt, idx, n);
2421   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2422   if( rc!=SQLITE_OK ){
2423     return TCL_ERROR;
2424   }
2425 
2426   return TCL_OK;
2427 }
2428 
2429 /*
2430 ** Usage:   sqlite3_bind_int  STMT N VALUE
2431 **
2432 ** Test the sqlite3_bind_int interface.  STMT is a prepared statement.
2433 ** N is the index of a wildcard in the prepared statement.  This command
2434 ** binds a 32-bit integer VALUE to that wildcard.
2435 */
2436 static int test_bind_int(
2437   void * clientData,
2438   Tcl_Interp *interp,
2439   int objc,
2440   Tcl_Obj *CONST objv[]
2441 ){
2442   sqlite3_stmt *pStmt;
2443   int idx;
2444   int value;
2445   int rc;
2446 
2447   if( objc!=4 ){
2448     Tcl_AppendResult(interp, "wrong # args: should be \"",
2449         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
2450     return TCL_ERROR;
2451   }
2452 
2453   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2454   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2455   if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2456 
2457   rc = sqlite3_bind_int(pStmt, idx, value);
2458   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2459   if( rc!=SQLITE_OK ){
2460     return TCL_ERROR;
2461   }
2462 
2463   return TCL_OK;
2464 }
2465 
2466 
2467 /*
2468 ** Usage:   sqlite3_bind_int64  STMT N VALUE
2469 **
2470 ** Test the sqlite3_bind_int64 interface.  STMT is a prepared statement.
2471 ** N is the index of a wildcard in the prepared statement.  This command
2472 ** binds a 64-bit integer VALUE to that wildcard.
2473 */
2474 static int test_bind_int64(
2475   void * clientData,
2476   Tcl_Interp *interp,
2477   int objc,
2478   Tcl_Obj *CONST objv[]
2479 ){
2480   sqlite3_stmt *pStmt;
2481   int idx;
2482   i64 value;
2483   int rc;
2484 
2485   if( objc!=4 ){
2486     Tcl_AppendResult(interp, "wrong # args: should be \"",
2487         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
2488     return TCL_ERROR;
2489   }
2490 
2491   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2492   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2493   if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2494 
2495   rc = sqlite3_bind_int64(pStmt, idx, value);
2496   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2497   if( rc!=SQLITE_OK ){
2498     return TCL_ERROR;
2499   }
2500 
2501   return TCL_OK;
2502 }
2503 
2504 
2505 /*
2506 ** Usage:   sqlite3_bind_double  STMT N VALUE
2507 **
2508 ** Test the sqlite3_bind_double interface.  STMT is a prepared statement.
2509 ** N is the index of a wildcard in the prepared statement.  This command
2510 ** binds a 64-bit integer VALUE to that wildcard.
2511 */
2512 static int test_bind_double(
2513   void * clientData,
2514   Tcl_Interp *interp,
2515   int objc,
2516   Tcl_Obj *CONST objv[]
2517 ){
2518   sqlite3_stmt *pStmt;
2519   int idx;
2520   double value;
2521   int rc;
2522 
2523   if( objc!=4 ){
2524     Tcl_AppendResult(interp, "wrong # args: should be \"",
2525         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
2526     return TCL_ERROR;
2527   }
2528 
2529   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2530   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2531   if( Tcl_GetDoubleFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2532 
2533   rc = sqlite3_bind_double(pStmt, idx, value);
2534   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2535   if( rc!=SQLITE_OK ){
2536     return TCL_ERROR;
2537   }
2538 
2539   return TCL_OK;
2540 }
2541 
2542 /*
2543 ** Usage:   sqlite3_bind_null  STMT N
2544 **
2545 ** Test the sqlite3_bind_null interface.  STMT is a prepared statement.
2546 ** N is the index of a wildcard in the prepared statement.  This command
2547 ** binds a NULL to the wildcard.
2548 */
2549 static int test_bind_null(
2550   void * clientData,
2551   Tcl_Interp *interp,
2552   int objc,
2553   Tcl_Obj *CONST objv[]
2554 ){
2555   sqlite3_stmt *pStmt;
2556   int idx;
2557   int rc;
2558 
2559   if( objc!=3 ){
2560     Tcl_AppendResult(interp, "wrong # args: should be \"",
2561         Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
2562     return TCL_ERROR;
2563   }
2564 
2565   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2566   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2567 
2568   rc = sqlite3_bind_null(pStmt, idx);
2569   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2570   if( rc!=SQLITE_OK ){
2571     return TCL_ERROR;
2572   }
2573 
2574   return TCL_OK;
2575 }
2576 
2577 /*
2578 ** Usage:   sqlite3_bind_text  STMT N STRING BYTES
2579 **
2580 ** Test the sqlite3_bind_text interface.  STMT is a prepared statement.
2581 ** N is the index of a wildcard in the prepared statement.  This command
2582 ** binds a UTF-8 string STRING to the wildcard.  The string is BYTES bytes
2583 ** long.
2584 */
2585 static int test_bind_text(
2586   void * clientData,
2587   Tcl_Interp *interp,
2588   int objc,
2589   Tcl_Obj *CONST objv[]
2590 ){
2591   sqlite3_stmt *pStmt;
2592   int idx;
2593   int bytes;
2594   char *value;
2595   int rc;
2596 
2597   if( objc!=5 ){
2598     Tcl_AppendResult(interp, "wrong # args: should be \"",
2599         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
2600     return TCL_ERROR;
2601   }
2602 
2603   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2604   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2605   value = Tcl_GetString(objv[3]);
2606   if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
2607 
2608   rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
2609   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2610   if( rc!=SQLITE_OK ){
2611     Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
2612     return TCL_ERROR;
2613   }
2614 
2615   return TCL_OK;
2616 }
2617 
2618 /*
2619 ** Usage:   sqlite3_bind_text16 ?-static? STMT N STRING BYTES
2620 **
2621 ** Test the sqlite3_bind_text16 interface.  STMT is a prepared statement.
2622 ** N is the index of a wildcard in the prepared statement.  This command
2623 ** binds a UTF-16 string STRING to the wildcard.  The string is BYTES bytes
2624 ** long.
2625 */
2626 static int test_bind_text16(
2627   void * clientData,
2628   Tcl_Interp *interp,
2629   int objc,
2630   Tcl_Obj *CONST objv[]
2631 ){
2632 #ifndef SQLITE_OMIT_UTF16
2633   sqlite3_stmt *pStmt;
2634   int idx;
2635   int bytes;
2636   char *value;
2637   int rc;
2638 
2639   void (*xDel)() = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
2640   Tcl_Obj *oStmt    = objv[objc-4];
2641   Tcl_Obj *oN       = objv[objc-3];
2642   Tcl_Obj *oString  = objv[objc-2];
2643   Tcl_Obj *oBytes   = objv[objc-1];
2644 
2645   if( objc!=5 && objc!=6){
2646     Tcl_AppendResult(interp, "wrong # args: should be \"",
2647         Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
2648     return TCL_ERROR;
2649   }
2650 
2651   if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR;
2652   if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR;
2653   value = (char*)Tcl_GetByteArrayFromObj(oString, 0);
2654   if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR;
2655 
2656   rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
2657   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2658   if( rc!=SQLITE_OK ){
2659     return TCL_ERROR;
2660   }
2661 
2662 #endif /* SQLITE_OMIT_UTF16 */
2663   return TCL_OK;
2664 }
2665 
2666 /*
2667 ** Usage:   sqlite3_bind_blob  STMT N DATA BYTES
2668 **
2669 ** Test the sqlite3_bind_blob interface.  STMT is a prepared statement.
2670 ** N is the index of a wildcard in the prepared statement.  This command
2671 ** binds a BLOB to the wildcard.  The BLOB is BYTES bytes in size.
2672 */
2673 static int test_bind_blob(
2674   void * clientData,
2675   Tcl_Interp *interp,
2676   int objc,
2677   Tcl_Obj *CONST objv[]
2678 ){
2679   sqlite3_stmt *pStmt;
2680   int idx;
2681   int bytes;
2682   char *value;
2683   int rc;
2684 
2685   if( objc!=5 ){
2686     Tcl_AppendResult(interp, "wrong # args: should be \"",
2687         Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
2688     return TCL_ERROR;
2689   }
2690 
2691   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2692   if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2693   value = Tcl_GetString(objv[3]);
2694   if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
2695 
2696   rc = sqlite3_bind_blob(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
2697   if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2698   if( rc!=SQLITE_OK ){
2699     return TCL_ERROR;
2700   }
2701 
2702   return TCL_OK;
2703 }
2704 
2705 /*
2706 ** Usage:   sqlite3_bind_parameter_count  STMT
2707 **
2708 ** Return the number of wildcards in the given statement.
2709 */
2710 static int test_bind_parameter_count(
2711   void * clientData,
2712   Tcl_Interp *interp,
2713   int objc,
2714   Tcl_Obj *CONST objv[]
2715 ){
2716   sqlite3_stmt *pStmt;
2717 
2718   if( objc!=2 ){
2719     Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2720     return TCL_ERROR;
2721   }
2722   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2723   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
2724   return TCL_OK;
2725 }
2726 
2727 /*
2728 ** Usage:   sqlite3_bind_parameter_name  STMT  N
2729 **
2730 ** Return the name of the Nth wildcard.  The first wildcard is 1.
2731 ** An empty string is returned if N is out of range or if the wildcard
2732 ** is nameless.
2733 */
2734 static int test_bind_parameter_name(
2735   void * clientData,
2736   Tcl_Interp *interp,
2737   int objc,
2738   Tcl_Obj *CONST objv[]
2739 ){
2740   sqlite3_stmt *pStmt;
2741   int i;
2742 
2743   if( objc!=3 ){
2744     Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
2745     return TCL_ERROR;
2746   }
2747   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2748   if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
2749   Tcl_SetObjResult(interp,
2750      Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
2751   );
2752   return TCL_OK;
2753 }
2754 
2755 /*
2756 ** Usage:   sqlite3_bind_parameter_index  STMT  NAME
2757 **
2758 ** Return the index of the wildcard called NAME.  Return 0 if there is
2759 ** no such wildcard.
2760 */
2761 static int test_bind_parameter_index(
2762   void * clientData,
2763   Tcl_Interp *interp,
2764   int objc,
2765   Tcl_Obj *CONST objv[]
2766 ){
2767   sqlite3_stmt *pStmt;
2768 
2769   if( objc!=3 ){
2770     Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
2771     return TCL_ERROR;
2772   }
2773   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2774   Tcl_SetObjResult(interp,
2775      Tcl_NewIntObj(
2776        sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
2777      )
2778   );
2779   return TCL_OK;
2780 }
2781 
2782 /*
2783 ** Usage:   sqlite3_clear_bindings STMT
2784 **
2785 */
2786 static int test_clear_bindings(
2787   void * clientData,
2788   Tcl_Interp *interp,
2789   int objc,
2790   Tcl_Obj *CONST objv[]
2791 ){
2792   sqlite3_stmt *pStmt;
2793 
2794   if( objc!=2 ){
2795     Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2796     return TCL_ERROR;
2797   }
2798   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2799   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
2800   return TCL_OK;
2801 }
2802 
2803 /*
2804 ** Usage:   sqlite3_sleep MILLISECONDS
2805 */
2806 static int test_sleep(
2807   void * clientData,
2808   Tcl_Interp *interp,
2809   int objc,
2810   Tcl_Obj *CONST objv[]
2811 ){
2812   int ms;
2813 
2814   if( objc!=2 ){
2815     Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS");
2816     return TCL_ERROR;
2817   }
2818   if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){
2819     return TCL_ERROR;
2820   }
2821   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms)));
2822   return TCL_OK;
2823 }
2824 
2825 /*
2826 ** Usage: sqlite3_errcode DB
2827 **
2828 ** Return the string representation of the most recent sqlite3_* API
2829 ** error code. e.g. "SQLITE_ERROR".
2830 */
2831 static int test_errcode(
2832   void * clientData,
2833   Tcl_Interp *interp,
2834   int objc,
2835   Tcl_Obj *CONST objv[]
2836 ){
2837   sqlite3 *db;
2838   int rc;
2839   char zBuf[30];
2840 
2841   if( objc!=2 ){
2842     Tcl_AppendResult(interp, "wrong # args: should be \"",
2843        Tcl_GetString(objv[0]), " DB", 0);
2844     return TCL_ERROR;
2845   }
2846   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2847   rc = sqlite3_errcode(db);
2848   if( (rc&0xff)==rc ){
2849     zBuf[0] = 0;
2850   }else{
2851     sprintf(zBuf,"+%d", rc>>8);
2852   }
2853   Tcl_AppendResult(interp, (char *)t1ErrorName(rc), zBuf, 0);
2854   return TCL_OK;
2855 }
2856 
2857 /*
2858 ** Usage:   test_errmsg DB
2859 **
2860 ** Returns the UTF-8 representation of the error message string for the
2861 ** most recent sqlite3_* API call.
2862 */
2863 static int test_errmsg(
2864   void * clientData,
2865   Tcl_Interp *interp,
2866   int objc,
2867   Tcl_Obj *CONST objv[]
2868 ){
2869   sqlite3 *db;
2870   const char *zErr;
2871 
2872   if( objc!=2 ){
2873     Tcl_AppendResult(interp, "wrong # args: should be \"",
2874        Tcl_GetString(objv[0]), " DB", 0);
2875     return TCL_ERROR;
2876   }
2877   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2878 
2879   zErr = sqlite3_errmsg(db);
2880   Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
2881   return TCL_OK;
2882 }
2883 
2884 /*
2885 ** Usage:   test_errmsg16 DB
2886 **
2887 ** Returns the UTF-16 representation of the error message string for the
2888 ** most recent sqlite3_* API call. This is a byte array object at the TCL
2889 ** level, and it includes the 0x00 0x00 terminator bytes at the end of the
2890 ** UTF-16 string.
2891 */
2892 static int test_errmsg16(
2893   void * clientData,
2894   Tcl_Interp *interp,
2895   int objc,
2896   Tcl_Obj *CONST objv[]
2897 ){
2898 #ifndef SQLITE_OMIT_UTF16
2899   sqlite3 *db;
2900   const void *zErr;
2901   int bytes = 0;
2902 
2903   if( objc!=2 ){
2904     Tcl_AppendResult(interp, "wrong # args: should be \"",
2905        Tcl_GetString(objv[0]), " DB", 0);
2906     return TCL_ERROR;
2907   }
2908   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2909 
2910   zErr = sqlite3_errmsg16(db);
2911   if( zErr ){
2912     bytes = sqlite3utf16ByteLen(zErr, -1);
2913   }
2914   Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
2915 #endif /* SQLITE_OMIT_UTF16 */
2916   return TCL_OK;
2917 }
2918 
2919 /*
2920 ** Usage: sqlite3_prepare DB sql bytes tailvar
2921 **
2922 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
2923 ** database handle <DB>. The parameter <tailval> is the name of a global
2924 ** variable that is set to the unused portion of <sql> (if any). A
2925 ** STMT handle is returned.
2926 */
2927 static int test_prepare(
2928   void * clientData,
2929   Tcl_Interp *interp,
2930   int objc,
2931   Tcl_Obj *CONST objv[]
2932 ){
2933   sqlite3 *db;
2934   const char *zSql;
2935   int bytes;
2936   const char *zTail = 0;
2937   sqlite3_stmt *pStmt = 0;
2938   char zBuf[50];
2939   int rc;
2940 
2941   if( objc!=5 ){
2942     Tcl_AppendResult(interp, "wrong # args: should be \"",
2943        Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
2944     return TCL_ERROR;
2945   }
2946   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2947   zSql = Tcl_GetString(objv[2]);
2948   if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
2949 
2950   rc = sqlite3_prepare(db, zSql, bytes, &pStmt, &zTail);
2951   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2952   if( zTail ){
2953     if( bytes>=0 ){
2954       bytes = bytes - (zTail-zSql);
2955     }
2956     Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
2957   }
2958   if( rc!=SQLITE_OK ){
2959     assert( pStmt==0 );
2960     sprintf(zBuf, "(%d) ", rc);
2961     Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
2962     return TCL_ERROR;
2963   }
2964 
2965   if( pStmt ){
2966     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
2967     Tcl_AppendResult(interp, zBuf, 0);
2968   }
2969   return TCL_OK;
2970 }
2971 
2972 /*
2973 ** Usage: sqlite3_prepare_v2 DB sql bytes tailvar
2974 **
2975 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
2976 ** database handle <DB>. The parameter <tailval> is the name of a global
2977 ** variable that is set to the unused portion of <sql> (if any). A
2978 ** STMT handle is returned.
2979 */
2980 static int test_prepare_v2(
2981   void * clientData,
2982   Tcl_Interp *interp,
2983   int objc,
2984   Tcl_Obj *CONST objv[]
2985 ){
2986   sqlite3 *db;
2987   const char *zSql;
2988   int bytes;
2989   const char *zTail = 0;
2990   sqlite3_stmt *pStmt = 0;
2991   char zBuf[50];
2992   int rc;
2993 
2994   if( objc!=5 ){
2995     Tcl_AppendResult(interp, "wrong # args: should be \"",
2996        Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
2997     return TCL_ERROR;
2998   }
2999   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3000   zSql = Tcl_GetString(objv[2]);
3001   if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3002 
3003   rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, &zTail);
3004   assert(rc==SQLITE_OK || pStmt==0);
3005   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3006   if( zTail ){
3007     if( bytes>=0 ){
3008       bytes = bytes - (zTail-zSql);
3009     }
3010     Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3011   }
3012   if( rc!=SQLITE_OK ){
3013     assert( pStmt==0 );
3014     sprintf(zBuf, "(%d) ", rc);
3015     Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3016     return TCL_ERROR;
3017   }
3018 
3019   if( pStmt ){
3020     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3021     Tcl_AppendResult(interp, zBuf, 0);
3022   }
3023   return TCL_OK;
3024 }
3025 
3026 /*
3027 ** Usage: sqlite3_prepare16 DB sql bytes tailvar
3028 **
3029 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3030 ** database handle <DB>. The parameter <tailval> is the name of a global
3031 ** variable that is set to the unused portion of <sql> (if any). A
3032 ** STMT handle is returned.
3033 */
3034 static int test_prepare16(
3035   void * clientData,
3036   Tcl_Interp *interp,
3037   int objc,
3038   Tcl_Obj *CONST objv[]
3039 ){
3040 #ifndef SQLITE_OMIT_UTF16
3041   sqlite3 *db;
3042   const void *zSql;
3043   const void *zTail = 0;
3044   Tcl_Obj *pTail = 0;
3045   sqlite3_stmt *pStmt = 0;
3046   char zBuf[50];
3047   int rc;
3048   int bytes;                /* The integer specified as arg 3 */
3049   int objlen;               /* The byte-array length of arg 2 */
3050 
3051   if( objc!=5 ){
3052     Tcl_AppendResult(interp, "wrong # args: should be \"",
3053        Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3054     return TCL_ERROR;
3055   }
3056   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3057   zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3058   if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3059 
3060   rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, &zTail);
3061   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3062   if( rc ){
3063     return TCL_ERROR;
3064   }
3065 
3066   if( zTail ){
3067     objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
3068   }else{
3069     objlen = 0;
3070   }
3071   pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3072   Tcl_IncrRefCount(pTail);
3073   Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
3074   Tcl_DecrRefCount(pTail);
3075 
3076   if( pStmt ){
3077     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3078   }
3079   Tcl_AppendResult(interp, zBuf, 0);
3080 #endif /* SQLITE_OMIT_UTF16 */
3081   return TCL_OK;
3082 }
3083 
3084 /*
3085 ** Usage: sqlite3_prepare16_v2 DB sql bytes tailvar
3086 **
3087 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3088 ** database handle <DB>. The parameter <tailval> is the name of a global
3089 ** variable that is set to the unused portion of <sql> (if any). A
3090 ** STMT handle is returned.
3091 */
3092 static int test_prepare16_v2(
3093   void * clientData,
3094   Tcl_Interp *interp,
3095   int objc,
3096   Tcl_Obj *CONST objv[]
3097 ){
3098 #ifndef SQLITE_OMIT_UTF16
3099   sqlite3 *db;
3100   const void *zSql;
3101   const void *zTail = 0;
3102   Tcl_Obj *pTail = 0;
3103   sqlite3_stmt *pStmt = 0;
3104   char zBuf[50];
3105   int rc;
3106   int bytes;                /* The integer specified as arg 3 */
3107   int objlen;               /* The byte-array length of arg 2 */
3108 
3109   if( objc!=5 ){
3110     Tcl_AppendResult(interp, "wrong # args: should be \"",
3111        Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3112     return TCL_ERROR;
3113   }
3114   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3115   zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3116   if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3117 
3118   rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, &zTail);
3119   if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3120   if( rc ){
3121     return TCL_ERROR;
3122   }
3123 
3124   if( zTail ){
3125     objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
3126   }else{
3127     objlen = 0;
3128   }
3129   pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3130   Tcl_IncrRefCount(pTail);
3131   Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
3132   Tcl_DecrRefCount(pTail);
3133 
3134   if( pStmt ){
3135     if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3136   }
3137   Tcl_AppendResult(interp, zBuf, 0);
3138 #endif /* SQLITE_OMIT_UTF16 */
3139   return TCL_OK;
3140 }
3141 
3142 /*
3143 ** Usage: sqlite3_open filename ?options-list?
3144 */
3145 static int test_open(
3146   void * clientData,
3147   Tcl_Interp *interp,
3148   int objc,
3149   Tcl_Obj *CONST objv[]
3150 ){
3151   const char *zFilename;
3152   sqlite3 *db;
3153   int rc;
3154   char zBuf[100];
3155 
3156   if( objc!=3 && objc!=2 ){
3157     Tcl_AppendResult(interp, "wrong # args: should be \"",
3158        Tcl_GetString(objv[0]), " filename options-list", 0);
3159     return TCL_ERROR;
3160   }
3161 
3162   zFilename = Tcl_GetString(objv[1]);
3163   rc = sqlite3_open(zFilename, &db);
3164 
3165   if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
3166   Tcl_AppendResult(interp, zBuf, 0);
3167   return TCL_OK;
3168 }
3169 
3170 /*
3171 ** Usage: sqlite3_open16 filename options
3172 */
3173 static int test_open16(
3174   void * clientData,
3175   Tcl_Interp *interp,
3176   int objc,
3177   Tcl_Obj *CONST objv[]
3178 ){
3179 #ifndef SQLITE_OMIT_UTF16
3180   const void *zFilename;
3181   sqlite3 *db;
3182   int rc;
3183   char zBuf[100];
3184 
3185   if( objc!=3 ){
3186     Tcl_AppendResult(interp, "wrong # args: should be \"",
3187        Tcl_GetString(objv[0]), " filename options-list", 0);
3188     return TCL_ERROR;
3189   }
3190 
3191   zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
3192   rc = sqlite3_open16(zFilename, &db);
3193 
3194   if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
3195   Tcl_AppendResult(interp, zBuf, 0);
3196 #endif /* SQLITE_OMIT_UTF16 */
3197   return TCL_OK;
3198 }
3199 
3200 /*
3201 ** Usage: sqlite3_complete16 <UTF-16 string>
3202 **
3203 ** Return 1 if the supplied argument is a complete SQL statement, or zero
3204 ** otherwise.
3205 */
3206 static int test_complete16(
3207   void * clientData,
3208   Tcl_Interp *interp,
3209   int objc,
3210   Tcl_Obj *CONST objv[]
3211 ){
3212 #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
3213   char *zBuf;
3214 
3215   if( objc!=2 ){
3216     Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
3217     return TCL_ERROR;
3218   }
3219 
3220   zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0);
3221   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
3222 #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
3223   return TCL_OK;
3224 }
3225 
3226 /*
3227 ** Usage: sqlite3_step STMT
3228 **
3229 ** Advance the statement to the next row.
3230 */
3231 static int test_step(
3232   void * clientData,
3233   Tcl_Interp *interp,
3234   int objc,
3235   Tcl_Obj *CONST objv[]
3236 ){
3237   sqlite3_stmt *pStmt;
3238   int rc;
3239 
3240   if( objc!=2 ){
3241     Tcl_AppendResult(interp, "wrong # args: should be \"",
3242        Tcl_GetString(objv[0]), " STMT", 0);
3243     return TCL_ERROR;
3244   }
3245 
3246   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3247   rc = sqlite3_step(pStmt);
3248 
3249   /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
3250   Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
3251   return TCL_OK;
3252 }
3253 
3254 /*
3255 ** Usage: sqlite3_column_count STMT
3256 **
3257 ** Return the number of columns returned by the sql statement STMT.
3258 */
3259 static int test_column_count(
3260   void * clientData,
3261   Tcl_Interp *interp,
3262   int objc,
3263   Tcl_Obj *CONST objv[]
3264 ){
3265   sqlite3_stmt *pStmt;
3266 
3267   if( objc!=2 ){
3268     Tcl_AppendResult(interp, "wrong # args: should be \"",
3269        Tcl_GetString(objv[0]), " STMT column", 0);
3270     return TCL_ERROR;
3271   }
3272 
3273   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3274 
3275   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
3276   return TCL_OK;
3277 }
3278 
3279 /*
3280 ** Usage: sqlite3_column_type STMT column
3281 **
3282 ** Return the type of the data in column 'column' of the current row.
3283 */
3284 static int test_column_type(
3285   void * clientData,
3286   Tcl_Interp *interp,
3287   int objc,
3288   Tcl_Obj *CONST objv[]
3289 ){
3290   sqlite3_stmt *pStmt;
3291   int col;
3292   int tp;
3293 
3294   if( objc!=3 ){
3295     Tcl_AppendResult(interp, "wrong # args: should be \"",
3296        Tcl_GetString(objv[0]), " STMT column", 0);
3297     return TCL_ERROR;
3298   }
3299 
3300   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3301   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3302 
3303   tp = sqlite3_column_type(pStmt, col);
3304   switch( tp ){
3305     case SQLITE_INTEGER:
3306       Tcl_SetResult(interp, "INTEGER", TCL_STATIC);
3307       break;
3308     case SQLITE_NULL:
3309       Tcl_SetResult(interp, "NULL", TCL_STATIC);
3310       break;
3311     case SQLITE_FLOAT:
3312       Tcl_SetResult(interp, "FLOAT", TCL_STATIC);
3313       break;
3314     case SQLITE_TEXT:
3315       Tcl_SetResult(interp, "TEXT", TCL_STATIC);
3316       break;
3317     case SQLITE_BLOB:
3318       Tcl_SetResult(interp, "BLOB", TCL_STATIC);
3319       break;
3320     default:
3321       assert(0);
3322   }
3323 
3324   return TCL_OK;
3325 }
3326 
3327 /*
3328 ** Usage: sqlite3_column_int64 STMT column
3329 **
3330 ** Return the data in column 'column' of the current row cast as an
3331 ** wide (64-bit) integer.
3332 */
3333 static int test_column_int64(
3334   void * clientData,
3335   Tcl_Interp *interp,
3336   int objc,
3337   Tcl_Obj *CONST objv[]
3338 ){
3339   sqlite3_stmt *pStmt;
3340   int col;
3341   i64 iVal;
3342 
3343   if( objc!=3 ){
3344     Tcl_AppendResult(interp, "wrong # args: should be \"",
3345        Tcl_GetString(objv[0]), " STMT column", 0);
3346     return TCL_ERROR;
3347   }
3348 
3349   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3350   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3351 
3352   iVal = sqlite3_column_int64(pStmt, col);
3353   Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
3354   return TCL_OK;
3355 }
3356 
3357 /*
3358 ** Usage: sqlite3_column_blob STMT column
3359 */
3360 static int test_column_blob(
3361   void * clientData,
3362   Tcl_Interp *interp,
3363   int objc,
3364   Tcl_Obj *CONST objv[]
3365 ){
3366   sqlite3_stmt *pStmt;
3367   int col;
3368 
3369   int len;
3370   const void *pBlob;
3371 
3372   if( objc!=3 ){
3373     Tcl_AppendResult(interp, "wrong # args: should be \"",
3374        Tcl_GetString(objv[0]), " STMT column", 0);
3375     return TCL_ERROR;
3376   }
3377 
3378   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3379   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3380 
3381   len = sqlite3_column_bytes(pStmt, col);
3382   pBlob = sqlite3_column_blob(pStmt, col);
3383   Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
3384   return TCL_OK;
3385 }
3386 
3387 /*
3388 ** Usage: sqlite3_column_double STMT column
3389 **
3390 ** Return the data in column 'column' of the current row cast as a double.
3391 */
3392 static int test_column_double(
3393   void * clientData,
3394   Tcl_Interp *interp,
3395   int objc,
3396   Tcl_Obj *CONST objv[]
3397 ){
3398   sqlite3_stmt *pStmt;
3399   int col;
3400   double rVal;
3401 
3402   if( objc!=3 ){
3403     Tcl_AppendResult(interp, "wrong # args: should be \"",
3404        Tcl_GetString(objv[0]), " STMT column", 0);
3405     return TCL_ERROR;
3406   }
3407 
3408   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3409   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3410 
3411   rVal = sqlite3_column_double(pStmt, col);
3412   Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
3413   return TCL_OK;
3414 }
3415 
3416 /*
3417 ** Usage: sqlite3_data_count STMT
3418 **
3419 ** Return the number of columns returned by the sql statement STMT.
3420 */
3421 static int test_data_count(
3422   void * clientData,
3423   Tcl_Interp *interp,
3424   int objc,
3425   Tcl_Obj *CONST objv[]
3426 ){
3427   sqlite3_stmt *pStmt;
3428 
3429   if( objc!=2 ){
3430     Tcl_AppendResult(interp, "wrong # args: should be \"",
3431        Tcl_GetString(objv[0]), " STMT column", 0);
3432     return TCL_ERROR;
3433   }
3434 
3435   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3436 
3437   Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
3438   return TCL_OK;
3439 }
3440 
3441 /*
3442 ** Usage: sqlite3_column_text STMT column
3443 **
3444 ** Usage: sqlite3_column_decltype STMT column
3445 **
3446 ** Usage: sqlite3_column_name STMT column
3447 */
3448 static int test_stmt_utf8(
3449   void * clientData,        /* Pointer to SQLite API function to be invoke */
3450   Tcl_Interp *interp,
3451   int objc,
3452   Tcl_Obj *CONST objv[]
3453 ){
3454   sqlite3_stmt *pStmt;
3455   int col;
3456   const char *(*xFunc)(sqlite3_stmt*, int) = clientData;
3457   const char *zRet;
3458 
3459   if( objc!=3 ){
3460     Tcl_AppendResult(interp, "wrong # args: should be \"",
3461        Tcl_GetString(objv[0]), " STMT column", 0);
3462     return TCL_ERROR;
3463   }
3464 
3465   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3466   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3467   zRet = xFunc(pStmt, col);
3468   if( zRet ){
3469     Tcl_SetResult(interp, (char *)zRet, 0);
3470   }
3471   return TCL_OK;
3472 }
3473 
3474 static int test_global_recover(
3475   void * clientData,
3476   Tcl_Interp *interp,
3477   int objc,
3478   Tcl_Obj *CONST objv[]
3479 ){
3480 #ifndef SQLITE_OMIT_GLOBALRECOVER
3481   int rc;
3482   if( objc!=1 ){
3483     Tcl_WrongNumArgs(interp, 1, objv, "");
3484     return TCL_ERROR;
3485   }
3486   rc = sqlite3_global_recover();
3487   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
3488 #endif
3489   return TCL_OK;
3490 }
3491 
3492 /*
3493 ** Usage: sqlite3_column_text STMT column
3494 **
3495 ** Usage: sqlite3_column_decltype STMT column
3496 **
3497 ** Usage: sqlite3_column_name STMT column
3498 */
3499 static int test_stmt_utf16(
3500   void * clientData,     /* Pointer to SQLite API function to be invoked */
3501   Tcl_Interp *interp,
3502   int objc,
3503   Tcl_Obj *CONST objv[]
3504 ){
3505 #ifndef SQLITE_OMIT_UTF16
3506   sqlite3_stmt *pStmt;
3507   int col;
3508   Tcl_Obj *pRet;
3509   const void *zName16;
3510   const void *(*xFunc)(sqlite3_stmt*, int) = clientData;
3511 
3512   if( objc!=3 ){
3513     Tcl_AppendResult(interp, "wrong # args: should be \"",
3514        Tcl_GetString(objv[0]), " STMT column", 0);
3515     return TCL_ERROR;
3516   }
3517 
3518   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3519   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3520 
3521   zName16 = xFunc(pStmt, col);
3522   if( zName16 ){
3523     pRet = Tcl_NewByteArrayObj(zName16, sqlite3utf16ByteLen(zName16, -1)+2);
3524     Tcl_SetObjResult(interp, pRet);
3525   }
3526 #endif /* SQLITE_OMIT_UTF16 */
3527 
3528   return TCL_OK;
3529 }
3530 
3531 /*
3532 ** Usage: sqlite3_column_int STMT column
3533 **
3534 ** Usage: sqlite3_column_bytes STMT column
3535 **
3536 ** Usage: sqlite3_column_bytes16 STMT column
3537 **
3538 */
3539 static int test_stmt_int(
3540   void * clientData,    /* Pointer to SQLite API function to be invoked */
3541   Tcl_Interp *interp,
3542   int objc,
3543   Tcl_Obj *CONST objv[]
3544 ){
3545   sqlite3_stmt *pStmt;
3546   int col;
3547   int (*xFunc)(sqlite3_stmt*, int) = clientData;
3548 
3549   if( objc!=3 ){
3550     Tcl_AppendResult(interp, "wrong # args: should be \"",
3551        Tcl_GetString(objv[0]), " STMT column", 0);
3552     return TCL_ERROR;
3553   }
3554 
3555   if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3556   if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3557 
3558   Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
3559   return TCL_OK;
3560 }
3561 
3562 #ifndef SQLITE_OMIT_DISKIO
3563 /*
3564 ** Usage:  sqlite3OsOpenReadWrite <filename>
3565 */
3566 static int test_sqlite3OsOpenReadWrite(
3567   void * clientData,
3568   Tcl_Interp *interp,
3569   int objc,
3570   Tcl_Obj *CONST objv[]
3571 ){
3572   OsFile *pFile;
3573   int rc;
3574   int dummy;
3575   char zBuf[100];
3576 
3577   if( objc!=2 ){
3578     Tcl_AppendResult(interp, "wrong # args: should be \"",
3579        Tcl_GetString(objv[0]), " filename", 0);
3580     return TCL_ERROR;
3581   }
3582 
3583   rc = sqlite3OsOpenReadWrite(Tcl_GetString(objv[1]), &pFile, &dummy);
3584   if( rc!=SQLITE_OK ){
3585     Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
3586     return TCL_ERROR;
3587   }
3588   sqlite3TestMakePointerStr(interp, zBuf, pFile);
3589   Tcl_SetResult(interp, zBuf, 0);
3590   return TCL_ERROR;
3591 }
3592 
3593 /*
3594 ** Usage:  sqlite3OsClose <file handle>
3595 */
3596 static int test_sqlite3OsClose(
3597   void * clientData,
3598   Tcl_Interp *interp,
3599   int objc,
3600   Tcl_Obj *CONST objv[]
3601 ){
3602   OsFile *pFile;
3603   int rc;
3604 
3605   if( objc!=2 ){
3606     Tcl_AppendResult(interp, "wrong # args: should be \"",
3607        Tcl_GetString(objv[0]), " filehandle", 0);
3608     return TCL_ERROR;
3609   }
3610 
3611   if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
3612     return TCL_ERROR;
3613   }
3614   rc = sqlite3OsClose(&pFile);
3615   if( rc!=SQLITE_OK ){
3616     Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
3617     return TCL_ERROR;
3618   }
3619   return TCL_OK;
3620 }
3621 
3622 /*
3623 ** Usage:  sqlite3OsLock <file handle> <locktype>
3624 */
3625 static int test_sqlite3OsLock(
3626   void * clientData,
3627   Tcl_Interp *interp,
3628   int objc,
3629   Tcl_Obj *CONST objv[]
3630 ){
3631   OsFile * pFile;
3632   int rc;
3633 
3634   if( objc!=3 ){
3635     Tcl_AppendResult(interp, "wrong # args: should be \"",
3636         Tcl_GetString(objv[0]),
3637         " filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0);
3638     return TCL_ERROR;
3639   }
3640 
3641   if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
3642     return TCL_ERROR;
3643   }
3644 
3645   if( 0==strcmp("SHARED", Tcl_GetString(objv[2])) ){
3646     rc = sqlite3OsLock(pFile, SHARED_LOCK);
3647   }
3648   else if( 0==strcmp("RESERVED", Tcl_GetString(objv[2])) ){
3649     rc = sqlite3OsLock(pFile, RESERVED_LOCK);
3650   }
3651   else if( 0==strcmp("PENDING", Tcl_GetString(objv[2])) ){
3652     rc = sqlite3OsLock(pFile, PENDING_LOCK);
3653   }
3654   else if( 0==strcmp("EXCLUSIVE", Tcl_GetString(objv[2])) ){
3655     rc = sqlite3OsLock(pFile, EXCLUSIVE_LOCK);
3656   }else{
3657     Tcl_AppendResult(interp, "wrong # args: should be \"",
3658         Tcl_GetString(objv[0]),
3659         " filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0);
3660     return TCL_ERROR;
3661   }
3662 
3663   if( rc!=SQLITE_OK ){
3664     Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
3665     return TCL_ERROR;
3666   }
3667   return TCL_OK;
3668 }
3669 
3670 /*
3671 ** Usage:  sqlite3OsUnlock <file handle>
3672 */
3673 static int test_sqlite3OsUnlock(
3674   void * clientData,
3675   Tcl_Interp *interp,
3676   int objc,
3677   Tcl_Obj *CONST objv[]
3678 ){
3679   OsFile * pFile;
3680   int rc;
3681 
3682   if( objc!=2 ){
3683     Tcl_AppendResult(interp, "wrong # args: should be \"",
3684        Tcl_GetString(objv[0]), " filehandle", 0);
3685     return TCL_ERROR;
3686   }
3687 
3688   if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
3689     return TCL_ERROR;
3690   }
3691   rc = sqlite3OsUnlock(pFile, NO_LOCK);
3692   if( rc!=SQLITE_OK ){
3693     Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
3694     return TCL_ERROR;
3695   }
3696   return TCL_OK;
3697 }
3698 
3699 /*
3700 ** Usage:  sqlite3OsTempFileName
3701 */
3702 static int test_sqlite3OsTempFileName(
3703   void * clientData,
3704   Tcl_Interp *interp,
3705   int objc,
3706   Tcl_Obj *CONST objv[]
3707 ){
3708   char zFile[SQLITE_TEMPNAME_SIZE];
3709   int rc;
3710 
3711   rc = sqlite3OsTempFileName(zFile);
3712   if( rc!=SQLITE_OK ){
3713     Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
3714     return TCL_ERROR;
3715   }
3716   Tcl_AppendResult(interp, zFile, 0);
3717   return TCL_OK;
3718 }
3719 #endif
3720 
3721 /*
3722 ** Usage:  sqlite_set_magic  DB  MAGIC-NUMBER
3723 **
3724 ** Set the db->magic value.  This is used to test error recovery logic.
3725 */
3726 static int sqlite_set_magic(
3727   void * clientData,
3728   Tcl_Interp *interp,
3729   int argc,
3730   char **argv
3731 ){
3732   sqlite3 *db;
3733   if( argc!=3 ){
3734     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3735          " DB MAGIC", 0);
3736     return TCL_ERROR;
3737   }
3738   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3739   if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){
3740     db->magic = SQLITE_MAGIC_OPEN;
3741   }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){
3742     db->magic = SQLITE_MAGIC_CLOSED;
3743   }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){
3744     db->magic = SQLITE_MAGIC_BUSY;
3745   }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){
3746     db->magic = SQLITE_MAGIC_ERROR;
3747   }else if( Tcl_GetInt(interp, argv[2], &db->magic) ){
3748     return TCL_ERROR;
3749   }
3750   return TCL_OK;
3751 }
3752 
3753 /*
3754 ** Usage:  sqlite3_interrupt  DB
3755 **
3756 ** Trigger an interrupt on DB
3757 */
3758 static int test_interrupt(
3759   void * clientData,
3760   Tcl_Interp *interp,
3761   int argc,
3762   char **argv
3763 ){
3764   sqlite3 *db;
3765   if( argc!=2 ){
3766     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
3767     return TCL_ERROR;
3768   }
3769   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3770   sqlite3_interrupt(db);
3771   return TCL_OK;
3772 }
3773 
3774 static u8 *sqlite3_stack_baseline = 0;
3775 
3776 /*
3777 ** Fill the stack with a known bitpattern.
3778 */
3779 static void prepStack(void){
3780   int i;
3781   u32 bigBuf[65536];
3782   for(i=0; i<sizeof(bigBuf); i++) bigBuf[i] = 0xdeadbeef;
3783   sqlite3_stack_baseline = (u8*)&bigBuf[65536];
3784 }
3785 
3786 /*
3787 ** Get the current stack depth.  Used for debugging only.
3788 */
3789 u64 sqlite3StackDepth(void){
3790   u8 x;
3791   return (u64)(sqlite3_stack_baseline - &x);
3792 }
3793 
3794 /*
3795 ** Usage:  sqlite3_stack_used DB SQL
3796 **
3797 ** Try to measure the amount of stack space used by a call to sqlite3_exec
3798 */
3799 static int test_stack_used(
3800   void * clientData,
3801   Tcl_Interp *interp,
3802   int argc,
3803   char **argv
3804 ){
3805   sqlite3 *db;
3806   int i;
3807   if( argc!=3 ){
3808     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3809         " DB SQL", 0);
3810     return TCL_ERROR;
3811   }
3812   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3813   prepStack();
3814   (void)sqlite3_exec(db, argv[2], 0, 0, 0);
3815   for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){}
3816   Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4));
3817   return TCL_OK;
3818 }
3819 
3820 /*
3821 ** Usage: sqlite_delete_function DB function-name
3822 **
3823 ** Delete the user function 'function-name' from database handle DB. It
3824 ** is assumed that the user function was created as UTF8, any number of
3825 ** arguments (the way the TCL interface does it).
3826 */
3827 static int delete_function(
3828   void * clientData,
3829   Tcl_Interp *interp,
3830   int argc,
3831   char **argv
3832 ){
3833   int rc;
3834   sqlite3 *db;
3835   if( argc!=3 ){
3836     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3837         " DB function-name", 0);
3838     return TCL_ERROR;
3839   }
3840   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3841   rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
3842   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
3843   return TCL_OK;
3844 }
3845 
3846 /*
3847 ** Usage: sqlite_delete_collation DB collation-name
3848 **
3849 ** Delete the collation sequence 'collation-name' from database handle
3850 ** DB. It is assumed that the collation sequence was created as UTF8 (the
3851 ** way the TCL interface does it).
3852 */
3853 static int delete_collation(
3854   void * clientData,
3855   Tcl_Interp *interp,
3856   int argc,
3857   char **argv
3858 ){
3859   int rc;
3860   sqlite3 *db;
3861   if( argc!=3 ){
3862     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3863         " DB function-name", 0);
3864     return TCL_ERROR;
3865   }
3866   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3867   rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
3868   Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
3869   return TCL_OK;
3870 }
3871 
3872 /*
3873 ** Usage: sqlite3_get_autocommit DB
3874 **
3875 ** Return true if the database DB is currently in auto-commit mode.
3876 ** Return false if not.
3877 */
3878 static int get_autocommit(
3879   void * clientData,
3880   Tcl_Interp *interp,
3881   int argc,
3882   char **argv
3883 ){
3884   char zBuf[30];
3885   sqlite3 *db;
3886   if( argc!=2 ){
3887     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3888         " DB", 0);
3889     return TCL_ERROR;
3890   }
3891   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3892   sprintf(zBuf, "%d", sqlite3_get_autocommit(db));
3893   Tcl_AppendResult(interp, zBuf, 0);
3894   return TCL_OK;
3895 }
3896 
3897 /*
3898 ** Usage: sqlite3_busy_timeout DB MS
3899 **
3900 ** Set the busy timeout.  This is more easily done using the timeout
3901 ** method of the TCL interface.  But we need a way to test the case
3902 ** where it returns SQLITE_MISUSE.
3903 */
3904 static int test_busy_timeout(
3905   void * clientData,
3906   Tcl_Interp *interp,
3907   int argc,
3908   char **argv
3909 ){
3910   int rc, ms;
3911   sqlite3 *db;
3912   if( argc!=3 ){
3913     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3914         " DB", 0);
3915     return TCL_ERROR;
3916   }
3917   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3918   if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
3919   rc = sqlite3_busy_timeout(db, ms);
3920   Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
3921   return TCL_OK;
3922 }
3923 
3924 /*
3925 ** Usage:  tcl_variable_type VARIABLENAME
3926 **
3927 ** Return the name of the internal representation for the
3928 ** value of the given variable.
3929 */
3930 static int tcl_variable_type(
3931   void * clientData,
3932   Tcl_Interp *interp,
3933   int objc,
3934   Tcl_Obj *CONST objv[]
3935 ){
3936   Tcl_Obj *pVar;
3937   if( objc!=2 ){
3938     Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
3939     return TCL_ERROR;
3940   }
3941   pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
3942   if( pVar==0 ) return TCL_ERROR;
3943   if( pVar->typePtr ){
3944     Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
3945   }
3946   return TCL_OK;
3947 }
3948 
3949 /*
3950 ** Usage:  sqlite3_release_memory ?N?
3951 **
3952 ** Attempt to release memory currently held but not actually required.
3953 ** The integer N is the number of bytes we are trying to release.  The
3954 ** return value is the amount of memory actually released.
3955 */
3956 static int test_release_memory(
3957   void * clientData,
3958   Tcl_Interp *interp,
3959   int objc,
3960   Tcl_Obj *CONST objv[]
3961 ){
3962 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
3963   int N;
3964   int amt;
3965   if( objc!=1 && objc!=2 ){
3966     Tcl_WrongNumArgs(interp, 1, objv, "?N?");
3967     return TCL_ERROR;
3968   }
3969   if( objc==2 ){
3970     if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
3971   }else{
3972     N = -1;
3973   }
3974   amt = sqlite3_release_memory(N);
3975   Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
3976 #endif
3977   return TCL_OK;
3978 }
3979 
3980 /*
3981 ** Usage:  sqlite3_soft_heap_limit ?N?
3982 **
3983 ** Query or set the soft heap limit for the current thread.  The
3984 ** limit is only changed if the N is present.  The previous limit
3985 ** is returned.
3986 */
3987 static int test_soft_heap_limit(
3988   void * clientData,
3989   Tcl_Interp *interp,
3990   int objc,
3991   Tcl_Obj *CONST objv[]
3992 ){
3993 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
3994   int amt;
3995   if( objc!=1 && objc!=2 ){
3996     Tcl_WrongNumArgs(interp, 1, objv, "?N?");
3997     return TCL_ERROR;
3998   }
3999   amt = sqlite3ThreadDataReadOnly()->nSoftHeapLimit;
4000   if( objc==2 ){
4001     int N;
4002     if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4003     sqlite3_soft_heap_limit(N);
4004   }
4005   Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
4006 #endif
4007   return TCL_OK;
4008 }
4009 
4010 /*
4011 ** Usage:   sqlite3_clear_tsd_memdebug
4012 **
4013 ** Clear all of the MEMDEBUG information out of thread-specific data.
4014 ** This will allow it to be deallocated.
4015 */
4016 static int test_clear_tsd_memdebug(
4017   void * clientData,
4018   Tcl_Interp *interp,
4019   int objc,
4020   Tcl_Obj *CONST objv[]
4021 ){
4022   return TCL_OK;
4023 }
4024 
4025 /*
4026 ** Usage:   sqlite3_tsd_release
4027 **
4028 ** Call sqlite3ReleaseThreadData.
4029 */
4030 static int test_tsd_release(
4031   void * clientData,
4032   Tcl_Interp *interp,
4033   int objc,
4034   Tcl_Obj *CONST objv[]
4035 ){
4036 #if defined(SQLITE_MEMDEBUG)
4037   sqlite3ReleaseThreadData();
4038 #endif
4039   return TCL_OK;
4040 }
4041 
4042 /*
4043 ** Usage:   sqlite3_thread_cleanup
4044 **
4045 ** Call the sqlite3_thread_cleanup API.
4046 */
4047 static int test_thread_cleanup(
4048   void * clientData,
4049   Tcl_Interp *interp,
4050   int objc,
4051   Tcl_Obj *CONST objv[]
4052 ){
4053   sqlite3_thread_cleanup();
4054   return TCL_OK;
4055 }
4056 
4057 
4058 /*
4059 ** Usage:   sqlite3_pager_refcounts  DB
4060 **
4061 ** Return a list of numbers which are the PagerRefcount for all
4062 ** pagers on each database connection.
4063 */
4064 static int test_pager_refcounts(
4065   void * clientData,
4066   Tcl_Interp *interp,
4067   int objc,
4068   Tcl_Obj *CONST objv[]
4069 ){
4070   sqlite3 *db;
4071   int i;
4072   int v, *a;
4073   Tcl_Obj *pResult;
4074 
4075   if( objc!=2 ){
4076     Tcl_AppendResult(interp, "wrong # args: should be \"",
4077         Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
4078     return TCL_ERROR;
4079   }
4080   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4081   pResult = Tcl_NewObj();
4082   for(i=0; i<db->nDb; i++){
4083     if( db->aDb[i].pBt==0 ){
4084       v = -1;
4085     }else{
4086       a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt));
4087       v = a[0];
4088     }
4089     Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v));
4090   }
4091   Tcl_SetObjResult(interp, pResult);
4092   return TCL_OK;
4093 }
4094 
4095 
4096 /*
4097 ** This routine sets entries in the global ::sqlite_options() array variable
4098 ** according to the compile-time configuration of the database.  Test
4099 ** procedures use this to determine when tests should be omitted.
4100 */
4101 static void set_options(Tcl_Interp *interp){
4102 #ifdef SQLITE_32BIT_ROWID
4103   Tcl_SetVar2(interp, "sqlite_options", "rowid32", "1", TCL_GLOBAL_ONLY);
4104 #else
4105   Tcl_SetVar2(interp, "sqlite_options", "rowid32", "0", TCL_GLOBAL_ONLY);
4106 #endif
4107 
4108 #ifdef SQLITE_CASE_SENSITIVE_LIKE
4109   Tcl_SetVar2(interp, "sqlite_options","casesensitivelike","1",TCL_GLOBAL_ONLY);
4110 #else
4111   Tcl_SetVar2(interp, "sqlite_options","casesensitivelike","0",TCL_GLOBAL_ONLY);
4112 #endif
4113 
4114 #ifdef SQLITE_DISABLE_DIRSYNC
4115   Tcl_SetVar2(interp, "sqlite_options", "dirsync", "0", TCL_GLOBAL_ONLY);
4116 #else
4117   Tcl_SetVar2(interp, "sqlite_options", "dirsync", "1", TCL_GLOBAL_ONLY);
4118 #endif
4119 
4120 #ifdef SQLITE_DISABLE_LFS
4121   Tcl_SetVar2(interp, "sqlite_options", "lfs", "0", TCL_GLOBAL_ONLY);
4122 #else
4123   Tcl_SetVar2(interp, "sqlite_options", "lfs", "1", TCL_GLOBAL_ONLY);
4124 #endif
4125 
4126 #ifdef SQLITE_OMIT_ALTERTABLE
4127   Tcl_SetVar2(interp, "sqlite_options", "altertable", "0", TCL_GLOBAL_ONLY);
4128 #else
4129   Tcl_SetVar2(interp, "sqlite_options", "altertable", "1", TCL_GLOBAL_ONLY);
4130 #endif
4131 
4132 #ifdef SQLITE_OMIT_ANALYZE
4133   Tcl_SetVar2(interp, "sqlite_options", "analyze", "0", TCL_GLOBAL_ONLY);
4134 #else
4135   Tcl_SetVar2(interp, "sqlite_options", "analyze", "1", TCL_GLOBAL_ONLY);
4136 #endif
4137 
4138 #ifdef SQLITE_OMIT_ATTACH
4139   Tcl_SetVar2(interp, "sqlite_options", "attach", "0", TCL_GLOBAL_ONLY);
4140 #else
4141   Tcl_SetVar2(interp, "sqlite_options", "attach", "1", TCL_GLOBAL_ONLY);
4142 #endif
4143 
4144 #ifdef SQLITE_OMIT_AUTHORIZATION
4145   Tcl_SetVar2(interp, "sqlite_options", "auth", "0", TCL_GLOBAL_ONLY);
4146 #else
4147   Tcl_SetVar2(interp, "sqlite_options", "auth", "1", TCL_GLOBAL_ONLY);
4148 #endif
4149 
4150 #ifdef SQLITE_OMIT_AUTOINCREMENT
4151   Tcl_SetVar2(interp, "sqlite_options", "autoinc", "0", TCL_GLOBAL_ONLY);
4152 #else
4153   Tcl_SetVar2(interp, "sqlite_options", "autoinc", "1", TCL_GLOBAL_ONLY);
4154 #endif
4155 
4156 #ifdef SQLITE_OMIT_AUTOVACUUM
4157   Tcl_SetVar2(interp, "sqlite_options", "autovacuum", "0", TCL_GLOBAL_ONLY);
4158 #else
4159   Tcl_SetVar2(interp, "sqlite_options", "autovacuum", "1", TCL_GLOBAL_ONLY);
4160 #endif /* SQLITE_OMIT_AUTOVACUUM */
4161 #if !defined(SQLITE_DEFAULT_AUTOVACUUM) || SQLITE_DEFAULT_AUTOVACUUM==0
4162   Tcl_SetVar2(interp,"sqlite_options","default_autovacuum","0",TCL_GLOBAL_ONLY);
4163 #else
4164   Tcl_SetVar2(interp,"sqlite_options","default_autovacuum","1",TCL_GLOBAL_ONLY);
4165 #endif
4166 
4167 #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
4168   Tcl_SetVar2(interp, "sqlite_options", "between_opt", "0", TCL_GLOBAL_ONLY);
4169 #else
4170   Tcl_SetVar2(interp, "sqlite_options", "between_opt", "1", TCL_GLOBAL_ONLY);
4171 #endif
4172 
4173 #ifdef SQLITE_OMIT_BLOB_LITERAL
4174   Tcl_SetVar2(interp, "sqlite_options", "bloblit", "0", TCL_GLOBAL_ONLY);
4175 #else
4176   Tcl_SetVar2(interp, "sqlite_options", "bloblit", "1", TCL_GLOBAL_ONLY);
4177 #endif
4178 
4179 #ifdef SQLITE_OMIT_CAST
4180   Tcl_SetVar2(interp, "sqlite_options", "cast", "0", TCL_GLOBAL_ONLY);
4181 #else
4182   Tcl_SetVar2(interp, "sqlite_options", "cast", "1", TCL_GLOBAL_ONLY);
4183 #endif
4184 
4185 #ifdef SQLITE_OMIT_CHECK
4186   Tcl_SetVar2(interp, "sqlite_options", "check", "0", TCL_GLOBAL_ONLY);
4187 #else
4188   Tcl_SetVar2(interp, "sqlite_options", "check", "1", TCL_GLOBAL_ONLY);
4189 #endif
4190 
4191 #ifdef SQLITE_ENABLE_COLUMN_METADATA
4192   Tcl_SetVar2(interp, "sqlite_options", "columnmetadata", "1", TCL_GLOBAL_ONLY);
4193 #else
4194   Tcl_SetVar2(interp, "sqlite_options", "columnmetadata", "0", TCL_GLOBAL_ONLY);
4195 #endif
4196 
4197 #ifdef SQLITE_OMIT_COMPLETE
4198   Tcl_SetVar2(interp, "sqlite_options", "complete", "0", TCL_GLOBAL_ONLY);
4199 #else
4200   Tcl_SetVar2(interp, "sqlite_options", "complete", "1", TCL_GLOBAL_ONLY);
4201 #endif
4202 
4203 #ifdef SQLITE_OMIT_COMPOUND_SELECT
4204   Tcl_SetVar2(interp, "sqlite_options", "compound", "0", TCL_GLOBAL_ONLY);
4205 #else
4206   Tcl_SetVar2(interp, "sqlite_options", "compound", "1", TCL_GLOBAL_ONLY);
4207 #endif
4208 
4209 #ifdef SQLITE_OMIT_CONFLICT_CLAUSE
4210   Tcl_SetVar2(interp, "sqlite_options", "conflict", "0", TCL_GLOBAL_ONLY);
4211 #else
4212   Tcl_SetVar2(interp, "sqlite_options", "conflict", "1", TCL_GLOBAL_ONLY);
4213 #endif
4214 
4215 #if OS_UNIX
4216   Tcl_SetVar2(interp, "sqlite_options", "crashtest", "1", TCL_GLOBAL_ONLY);
4217 #else
4218   Tcl_SetVar2(interp, "sqlite_options", "crashtest", "0", TCL_GLOBAL_ONLY);
4219 #endif
4220 
4221 #ifdef SQLITE_OMIT_DATETIME_FUNCS
4222   Tcl_SetVar2(interp, "sqlite_options", "datetime", "0", TCL_GLOBAL_ONLY);
4223 #else
4224   Tcl_SetVar2(interp, "sqlite_options", "datetime", "1", TCL_GLOBAL_ONLY);
4225 #endif
4226 
4227 #ifdef SQLITE_OMIT_DISKIO
4228   Tcl_SetVar2(interp, "sqlite_options", "diskio", "0", TCL_GLOBAL_ONLY);
4229 #else
4230   Tcl_SetVar2(interp, "sqlite_options", "diskio", "1", TCL_GLOBAL_ONLY);
4231 #endif
4232 
4233 #ifdef SQLITE_OMIT_EXPLAIN
4234   Tcl_SetVar2(interp, "sqlite_options", "explain", "0", TCL_GLOBAL_ONLY);
4235 #else
4236   Tcl_SetVar2(interp, "sqlite_options", "explain", "1", TCL_GLOBAL_ONLY);
4237 #endif
4238 
4239 #ifdef SQLITE_OMIT_FLOATING_POINT
4240   Tcl_SetVar2(interp, "sqlite_options", "floatingpoint", "0", TCL_GLOBAL_ONLY);
4241 #else
4242   Tcl_SetVar2(interp, "sqlite_options", "floatingpoint", "1", TCL_GLOBAL_ONLY);
4243 #endif
4244 
4245 #ifdef SQLITE_OMIT_FOREIGN_KEY
4246   Tcl_SetVar2(interp, "sqlite_options", "foreignkey", "0", TCL_GLOBAL_ONLY);
4247 #else
4248   Tcl_SetVar2(interp, "sqlite_options", "foreignkey", "1", TCL_GLOBAL_ONLY);
4249 #endif
4250 
4251 #ifdef SQLITE_ENABLE_FTS1
4252   Tcl_SetVar2(interp, "sqlite_options", "fts1", "1", TCL_GLOBAL_ONLY);
4253 #else
4254   Tcl_SetVar2(interp, "sqlite_options", "fts1", "0", TCL_GLOBAL_ONLY);
4255 #endif
4256 
4257 #ifdef SQLITE_ENABLE_FTS2
4258   Tcl_SetVar2(interp, "sqlite_options", "fts2", "1", TCL_GLOBAL_ONLY);
4259 #else
4260   Tcl_SetVar2(interp, "sqlite_options", "fts2", "0", TCL_GLOBAL_ONLY);
4261 #endif
4262 
4263 #ifdef SQLITE_OMIT_GLOBALRECOVER
4264   Tcl_SetVar2(interp, "sqlite_options", "globalrecover", "0", TCL_GLOBAL_ONLY);
4265 #else
4266   Tcl_SetVar2(interp, "sqlite_options", "globalrecover", "1", TCL_GLOBAL_ONLY);
4267 #endif
4268 
4269 #ifdef SQLITE_OMIT_INCRBLOB
4270   Tcl_SetVar2(interp, "sqlite_options", "incrblob", "0", TCL_GLOBAL_ONLY);
4271 #else
4272   Tcl_SetVar2(interp, "sqlite_options", "incrblob", "1", TCL_GLOBAL_ONLY);
4273 #endif /* SQLITE_OMIT_AUTOVACUUM */
4274 
4275 #ifdef SQLITE_OMIT_INTEGRITY_CHECK
4276   Tcl_SetVar2(interp, "sqlite_options", "integrityck", "0", TCL_GLOBAL_ONLY);
4277 #else
4278   Tcl_SetVar2(interp, "sqlite_options", "integrityck", "1", TCL_GLOBAL_ONLY);
4279 #endif
4280 
4281 #if defined(SQLITE_DEFAULT_FILE_FORMAT) && SQLITE_DEFAULT_FILE_FORMAT==1
4282   Tcl_SetVar2(interp, "sqlite_options", "legacyformat", "1", TCL_GLOBAL_ONLY);
4283 #else
4284   Tcl_SetVar2(interp, "sqlite_options", "legacyformat", "0", TCL_GLOBAL_ONLY);
4285 #endif
4286 
4287 #ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
4288   Tcl_SetVar2(interp, "sqlite_options", "like_opt", "0", TCL_GLOBAL_ONLY);
4289 #else
4290   Tcl_SetVar2(interp, "sqlite_options", "like_opt", "1", TCL_GLOBAL_ONLY);
4291 #endif
4292 
4293 #ifdef SQLITE_OMIT_LOAD_EXTENSION
4294   Tcl_SetVar2(interp, "sqlite_options", "load_ext", "0", TCL_GLOBAL_ONLY);
4295 #else
4296   Tcl_SetVar2(interp, "sqlite_options", "load_ext", "1", TCL_GLOBAL_ONLY);
4297 #endif
4298 
4299 #ifdef SQLITE_OMIT_MEMORYDB
4300   Tcl_SetVar2(interp, "sqlite_options", "memorydb", "0", TCL_GLOBAL_ONLY);
4301 #else
4302   Tcl_SetVar2(interp, "sqlite_options", "memorydb", "1", TCL_GLOBAL_ONLY);
4303 #endif
4304 
4305 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
4306   Tcl_SetVar2(interp, "sqlite_options", "memorymanage", "1", TCL_GLOBAL_ONLY);
4307 #else
4308   Tcl_SetVar2(interp, "sqlite_options", "memorymanage", "0", TCL_GLOBAL_ONLY);
4309 #endif
4310 
4311 #ifdef SQLITE_OMIT_OR_OPTIMIZATION
4312   Tcl_SetVar2(interp, "sqlite_options", "or_opt", "0", TCL_GLOBAL_ONLY);
4313 #else
4314   Tcl_SetVar2(interp, "sqlite_options", "or_opt", "1", TCL_GLOBAL_ONLY);
4315 #endif
4316 
4317 #ifdef SQLITE_OMIT_PAGER_PRAGMAS
4318   Tcl_SetVar2(interp, "sqlite_options", "pager_pragmas", "0", TCL_GLOBAL_ONLY);
4319 #else
4320   Tcl_SetVar2(interp, "sqlite_options", "pager_pragmas", "1", TCL_GLOBAL_ONLY);
4321 #endif
4322 
4323 #ifdef SQLITE_OMIT_PARSER
4324   Tcl_SetVar2(interp, "sqlite_options", "parser", "0", TCL_GLOBAL_ONLY);
4325 #else
4326   Tcl_SetVar2(interp, "sqlite_options", "parser", "1", TCL_GLOBAL_ONLY);
4327 #endif
4328 
4329 #if defined(SQLITE_OMIT_PRAGMA) || defined(SQLITE_OMIT_FLAG_PRAGMAS)
4330   Tcl_SetVar2(interp, "sqlite_options", "pragma", "0", TCL_GLOBAL_ONLY);
4331   Tcl_SetVar2(interp, "sqlite_options", "integrityck", "0", TCL_GLOBAL_ONLY);
4332 #else
4333   Tcl_SetVar2(interp, "sqlite_options", "pragma", "1", TCL_GLOBAL_ONLY);
4334 #endif
4335 
4336 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
4337   Tcl_SetVar2(interp, "sqlite_options", "progress", "0", TCL_GLOBAL_ONLY);
4338 #else
4339   Tcl_SetVar2(interp, "sqlite_options", "progress", "1", TCL_GLOBAL_ONLY);
4340 #endif
4341 
4342 #ifdef SQLITE_ENABLE_REDEF_IO
4343   Tcl_SetVar2(interp, "sqlite_options", "redefio", "1", TCL_GLOBAL_ONLY);
4344 #else
4345   Tcl_SetVar2(interp, "sqlite_options", "redefio", "0", TCL_GLOBAL_ONLY);
4346 #endif
4347 
4348 #ifdef SQLITE_OMIT_REINDEX
4349   Tcl_SetVar2(interp, "sqlite_options", "reindex", "0", TCL_GLOBAL_ONLY);
4350 #else
4351   Tcl_SetVar2(interp, "sqlite_options", "reindex", "1", TCL_GLOBAL_ONLY);
4352 #endif
4353 
4354 #ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
4355   Tcl_SetVar2(interp, "sqlite_options", "schema_pragmas", "0", TCL_GLOBAL_ONLY);
4356 #else
4357   Tcl_SetVar2(interp, "sqlite_options", "schema_pragmas", "1", TCL_GLOBAL_ONLY);
4358 #endif
4359 
4360 #ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
4361   Tcl_SetVar2(interp, "sqlite_options", "schema_version", "0", TCL_GLOBAL_ONLY);
4362 #else
4363   Tcl_SetVar2(interp, "sqlite_options", "schema_version", "1", TCL_GLOBAL_ONLY);
4364 #endif
4365 
4366 #ifdef SQLITE_OMIT_SHARED_CACHE
4367   Tcl_SetVar2(interp, "sqlite_options", "shared_cache", "0", TCL_GLOBAL_ONLY);
4368 #else
4369   Tcl_SetVar2(interp, "sqlite_options", "shared_cache", "1", TCL_GLOBAL_ONLY);
4370 #endif
4371 
4372 #ifdef SQLITE_OMIT_SUBQUERY
4373   Tcl_SetVar2(interp, "sqlite_options", "subquery", "0", TCL_GLOBAL_ONLY);
4374 #else
4375   Tcl_SetVar2(interp, "sqlite_options", "subquery", "1", TCL_GLOBAL_ONLY);
4376 #endif
4377 
4378 #ifdef SQLITE_OMIT_TCL_VARIABLE
4379   Tcl_SetVar2(interp, "sqlite_options", "tclvar", "0", TCL_GLOBAL_ONLY);
4380 #else
4381   Tcl_SetVar2(interp, "sqlite_options", "tclvar", "1", TCL_GLOBAL_ONLY);
4382 #endif
4383 
4384 #if defined(THREADSAFE) && THREADSAFE
4385   Tcl_SetVar2(interp, "sqlite_options", "threadsafe", "1", TCL_GLOBAL_ONLY);
4386 #else
4387   Tcl_SetVar2(interp, "sqlite_options", "threadsafe", "0", TCL_GLOBAL_ONLY);
4388 #endif
4389 
4390 #ifdef SQLITE_OMIT_TRACE
4391   Tcl_SetVar2(interp, "sqlite_options", "trace", "0", TCL_GLOBAL_ONLY);
4392 #else
4393   Tcl_SetVar2(interp, "sqlite_options", "trace", "1", TCL_GLOBAL_ONLY);
4394 #endif
4395 
4396 #ifdef SQLITE_OMIT_TRIGGER
4397   Tcl_SetVar2(interp, "sqlite_options", "trigger", "0", TCL_GLOBAL_ONLY);
4398 #else
4399   Tcl_SetVar2(interp, "sqlite_options", "trigger", "1", TCL_GLOBAL_ONLY);
4400 #endif
4401 
4402 #ifdef SQLITE_OMIT_TEMPDB
4403   Tcl_SetVar2(interp, "sqlite_options", "tempdb", "0", TCL_GLOBAL_ONLY);
4404 #else
4405   Tcl_SetVar2(interp, "sqlite_options", "tempdb", "1", TCL_GLOBAL_ONLY);
4406 #endif
4407 
4408 #ifdef SQLITE_OMIT_UTF16
4409   Tcl_SetVar2(interp, "sqlite_options", "utf16", "0", TCL_GLOBAL_ONLY);
4410 #else
4411   Tcl_SetVar2(interp, "sqlite_options", "utf16", "1", TCL_GLOBAL_ONLY);
4412 #endif
4413 
4414 #if defined(SQLITE_OMIT_VACUUM) || defined(SQLITE_OMIT_ATTACH)
4415   Tcl_SetVar2(interp, "sqlite_options", "vacuum", "0", TCL_GLOBAL_ONLY);
4416 #else
4417   Tcl_SetVar2(interp, "sqlite_options", "vacuum", "1", TCL_GLOBAL_ONLY);
4418 #endif
4419 
4420 #ifdef SQLITE_OMIT_VIEW
4421   Tcl_SetVar2(interp, "sqlite_options", "view", "0", TCL_GLOBAL_ONLY);
4422 #else
4423   Tcl_SetVar2(interp, "sqlite_options", "view", "1", TCL_GLOBAL_ONLY);
4424 #endif
4425 
4426 #ifdef SQLITE_OMIT_VIRTUALTABLE
4427   Tcl_SetVar2(interp, "sqlite_options", "vtab", "0", TCL_GLOBAL_ONLY);
4428 #else
4429   Tcl_SetVar2(interp, "sqlite_options", "vtab", "1", TCL_GLOBAL_ONLY);
4430 #endif
4431 
4432 #ifdef SQLITE_DEFAULT_FILE_FORMAT
4433   Tcl_ObjSetVar2(interp,
4434       Tcl_NewStringObj("sqlite_default_file_format", -1), 0,
4435       Tcl_NewIntObj(SQLITE_DEFAULT_FILE_FORMAT), TCL_GLOBAL_ONLY
4436   );
4437 #endif
4438 #ifdef SQLITE_MAX_PAGE_SIZE
4439   Tcl_ObjSetVar2(interp,
4440       Tcl_NewStringObj("SQLITE_MAX_PAGE_SIZE", -1), 0,
4441       Tcl_NewIntObj(SQLITE_MAX_PAGE_SIZE), TCL_GLOBAL_ONLY
4442   );
4443 #endif
4444 #ifdef TEMP_STORE
4445   Tcl_ObjSetVar2(interp,
4446       Tcl_NewStringObj("TEMP_STORE", -1), 0,
4447       Tcl_NewIntObj(TEMP_STORE), TCL_GLOBAL_ONLY
4448   );
4449 #endif
4450 }
4451 
4452 /*
4453 ** tclcmd:   working_64bit_int
4454 **
4455 ** Some TCL builds (ex: cygwin) do not support 64-bit integers.  This
4456 ** leads to a number of test failures.  The present command checks the
4457 ** TCL build to see whether or not it supports 64-bit integers.  It
4458 ** returns TRUE if it does and FALSE if not.
4459 **
4460 ** This command is used to warn users that their TCL build is defective
4461 ** and that the errors they are seeing in the test scripts might be
4462 ** a result of their defective TCL rather than problems in SQLite.
4463 */
4464 static int working_64bit_int(
4465   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4466   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
4467   int objc,              /* Number of arguments */
4468   Tcl_Obj *CONST objv[]  /* Command arguments */
4469 ){
4470   Tcl_Obj *pTestObj;
4471   int working = 0;
4472 
4473   pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890);
4474   working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0;
4475   Tcl_DecrRefCount(pTestObj);
4476   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working));
4477   return TCL_OK;
4478 }
4479 
4480 
4481 /*
4482 ** Register commands with the TCL interpreter.
4483 */
4484 int Sqlitetest1_Init(Tcl_Interp *interp){
4485   extern int sqlite3_search_count;
4486   extern int sqlite3_interrupt_count;
4487   extern int sqlite3_open_file_count;
4488   extern int sqlite3_sort_count;
4489   extern int sqlite3_current_time;
4490   extern int sqlite3_max_blobsize;
4491   extern int sqlite3BtreeSharedCacheReport(void*,
4492                                           Tcl_Interp*,int,Tcl_Obj*CONST*);
4493   static struct {
4494      char *zName;
4495      Tcl_CmdProc *xProc;
4496   } aCmd[] = {
4497      { "sqlite3_mprintf_int",           (Tcl_CmdProc*)sqlite3_mprintf_int    },
4498      { "sqlite3_mprintf_int64",         (Tcl_CmdProc*)sqlite3_mprintf_int64  },
4499      { "sqlite3_mprintf_str",           (Tcl_CmdProc*)sqlite3_mprintf_str    },
4500      { "sqlite3_snprintf_str",          (Tcl_CmdProc*)sqlite3_snprintf_str   },
4501      { "sqlite3_mprintf_stronly",       (Tcl_CmdProc*)sqlite3_mprintf_stronly},
4502      { "sqlite3_mprintf_double",        (Tcl_CmdProc*)sqlite3_mprintf_double },
4503      { "sqlite3_mprintf_scaled",        (Tcl_CmdProc*)sqlite3_mprintf_scaled },
4504      { "sqlite3_mprintf_hexdouble",   (Tcl_CmdProc*)sqlite3_mprintf_hexdouble},
4505      { "sqlite3_mprintf_z_test",        (Tcl_CmdProc*)test_mprintf_z        },
4506      { "sqlite3_mprintf_n_test",        (Tcl_CmdProc*)test_mprintf_n        },
4507      { "sqlite3_last_insert_rowid",     (Tcl_CmdProc*)test_last_rowid       },
4508      { "sqlite3_exec_printf",           (Tcl_CmdProc*)test_exec_printf      },
4509      { "sqlite3_exec",                  (Tcl_CmdProc*)test_exec             },
4510      { "sqlite3_exec_nr",               (Tcl_CmdProc*)test_exec_nr          },
4511      { "sqlite3_get_table_printf",      (Tcl_CmdProc*)test_get_table_printf },
4512      { "sqlite3_close",                 (Tcl_CmdProc*)sqlite_test_close     },
4513      { "sqlite3_create_function",       (Tcl_CmdProc*)test_create_function  },
4514      { "sqlite3_create_aggregate",      (Tcl_CmdProc*)test_create_aggregate },
4515      { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func    },
4516      { "sqlite_abort",                  (Tcl_CmdProc*)sqlite_abort          },
4517 #ifdef SQLITE_MEMDEBUG
4518      { "sqlite_malloc_fail",            (Tcl_CmdProc*)sqlite_malloc_fail    },
4519      { "sqlite_malloc_stat",            (Tcl_CmdProc*)sqlite_malloc_stat    },
4520 #endif
4521      { "sqlite_bind",                   (Tcl_CmdProc*)test_bind             },
4522      { "breakpoint",                    (Tcl_CmdProc*)test_breakpoint       },
4523      { "sqlite3_key",                   (Tcl_CmdProc*)test_key              },
4524      { "sqlite3_rekey",                 (Tcl_CmdProc*)test_rekey            },
4525      { "sqlite_set_magic",              (Tcl_CmdProc*)sqlite_set_magic      },
4526      { "sqlite3_interrupt",             (Tcl_CmdProc*)test_interrupt        },
4527      { "sqlite_delete_function",        (Tcl_CmdProc*)delete_function       },
4528      { "sqlite_delete_collation",       (Tcl_CmdProc*)delete_collation      },
4529      { "sqlite3_get_autocommit",        (Tcl_CmdProc*)get_autocommit        },
4530      { "sqlite3_stack_used",            (Tcl_CmdProc*)test_stack_used       },
4531      { "sqlite3_busy_timeout",          (Tcl_CmdProc*)test_busy_timeout     },
4532      { "printf",                        (Tcl_CmdProc*)test_printf           },
4533      { "sqlite3_io_trace",              (Tcl_CmdProc*)test_io_trace         },
4534   };
4535   static struct {
4536      char *zName;
4537      Tcl_ObjCmdProc *xProc;
4538      void *clientData;
4539   } aObjCmd[] = {
4540      { "sqlite3_connection_pointer",    get_sqlite_pointer, 0 },
4541      { "sqlite3_bind_int",              test_bind_int,      0 },
4542      { "sqlite3_bind_zeroblob",         test_bind_zeroblob, 0 },
4543      { "sqlite3_bind_int64",            test_bind_int64,    0 },
4544      { "sqlite3_bind_double",           test_bind_double,   0 },
4545      { "sqlite3_bind_null",             test_bind_null     ,0 },
4546      { "sqlite3_bind_text",             test_bind_text     ,0 },
4547      { "sqlite3_bind_text16",           test_bind_text16   ,0 },
4548      { "sqlite3_bind_blob",             test_bind_blob     ,0 },
4549      { "sqlite3_bind_parameter_count",  test_bind_parameter_count, 0},
4550      { "sqlite3_bind_parameter_name",   test_bind_parameter_name,  0},
4551      { "sqlite3_bind_parameter_index",  test_bind_parameter_index, 0},
4552      { "sqlite3_clear_bindings",        test_clear_bindings, 0},
4553      { "sqlite3_sleep",                 test_sleep,          0},
4554      { "sqlite3_errcode",               test_errcode       ,0 },
4555      { "sqlite3_errmsg",                test_errmsg        ,0 },
4556      { "sqlite3_errmsg16",              test_errmsg16      ,0 },
4557      { "sqlite3_open",                  test_open          ,0 },
4558      { "sqlite3_open16",                test_open16        ,0 },
4559      { "sqlite3_complete16",            test_complete16    ,0 },
4560 
4561      { "sqlite3_prepare",               test_prepare       ,0 },
4562      { "sqlite3_prepare16",             test_prepare16     ,0 },
4563      { "sqlite3_prepare_v2",            test_prepare_v2    ,0 },
4564      { "sqlite3_prepare16_v2",          test_prepare16_v2  ,0 },
4565      { "sqlite3_finalize",              test_finalize      ,0 },
4566      { "sqlite3_reset",                 test_reset         ,0 },
4567      { "sqlite3_expired",               test_expired       ,0 },
4568      { "sqlite3_transfer_bindings",     test_transfer_bind ,0 },
4569      { "sqlite3_changes",               test_changes       ,0 },
4570      { "sqlite3_step",                  test_step          ,0 },
4571 
4572      { "sqlite3_release_memory",        test_release_memory,     0},
4573      { "sqlite3_soft_heap_limit",       test_soft_heap_limit,    0},
4574      { "sqlite3_clear_tsd_memdebug",    test_clear_tsd_memdebug, 0},
4575      { "sqlite3_tsd_release",           test_tsd_release,        0},
4576      { "sqlite3_thread_cleanup",        test_thread_cleanup,     0},
4577      { "sqlite3_pager_refcounts",       test_pager_refcounts,    0},
4578 
4579      { "sqlite3_load_extension",        test_load_extension,     0},
4580      { "sqlite3_enable_load_extension", test_enable_load,        0},
4581      { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
4582 
4583      /* sqlite3_column_*() API */
4584      { "sqlite3_column_count",          test_column_count  ,0 },
4585      { "sqlite3_data_count",            test_data_count    ,0 },
4586      { "sqlite3_column_type",           test_column_type   ,0 },
4587      { "sqlite3_column_blob",           test_column_blob   ,0 },
4588      { "sqlite3_column_double",         test_column_double ,0 },
4589      { "sqlite3_column_int64",          test_column_int64  ,0 },
4590      { "sqlite3_column_text",       test_stmt_utf8,  sqlite3_column_text      },
4591      { "sqlite3_column_decltype",   test_stmt_utf8,  sqlite3_column_decltype  },
4592      { "sqlite3_column_name",       test_stmt_utf8,  sqlite3_column_name      },
4593      { "sqlite3_column_int",        test_stmt_int,   sqlite3_column_int       },
4594      { "sqlite3_column_bytes",      test_stmt_int,   sqlite3_column_bytes     },
4595 #ifdef SQLITE_ENABLE_COLUMN_METADATA
4596 { "sqlite3_column_database_name", test_stmt_utf8, sqlite3_column_database_name},
4597 { "sqlite3_column_table_name", test_stmt_utf8, sqlite3_column_table_name},
4598 { "sqlite3_column_origin_name", test_stmt_utf8, sqlite3_column_origin_name},
4599 #endif
4600 
4601 #ifndef SQLITE_OMIT_UTF16
4602      { "sqlite3_column_bytes16",    test_stmt_int,   sqlite3_column_bytes16   },
4603      { "sqlite3_column_text16",     test_stmt_utf16, sqlite3_column_text16    },
4604      { "sqlite3_column_decltype16", test_stmt_utf16, sqlite3_column_decltype16},
4605      { "sqlite3_column_name16",     test_stmt_utf16, sqlite3_column_name16    },
4606      { "add_alignment_test_collations", add_alignment_test_collations, 0      },
4607 #ifdef SQLITE_ENABLE_COLUMN_METADATA
4608 {"sqlite3_column_database_name16",
4609   test_stmt_utf16, sqlite3_column_database_name16},
4610 {"sqlite3_column_table_name16", test_stmt_utf16, sqlite3_column_table_name16},
4611 {"sqlite3_column_origin_name16", test_stmt_utf16, sqlite3_column_origin_name16},
4612 #endif
4613 #endif
4614      { "sqlite3_global_recover",    test_global_recover, 0   },
4615      { "working_64bit_int",         working_64bit_int,   0   },
4616 
4617      /* Functions from os.h */
4618 #ifndef SQLITE_OMIT_DISKIO
4619      { "sqlite3OsOpenReadWrite",test_sqlite3OsOpenReadWrite, 0 },
4620      { "sqlite3OsClose",        test_sqlite3OsClose, 0 },
4621      { "sqlite3OsLock",         test_sqlite3OsLock, 0 },
4622      { "sqlite3OsTempFileName", test_sqlite3OsTempFileName, 0 },
4623 
4624      /* Custom test interfaces */
4625      { "sqlite3OsUnlock",         test_sqlite3OsUnlock, 0    },
4626 #endif
4627 #ifndef SQLITE_OMIT_UTF16
4628      { "add_test_collate",        test_collate, 0            },
4629      { "add_test_collate_needed", test_collate_needed, 0     },
4630      { "add_test_function",       test_function, 0           },
4631 #endif
4632 #ifdef SQLITE_MEMDEBUG
4633      { "sqlite_malloc_outstanding", sqlite_malloc_outstanding, 0},
4634 #endif
4635      { "sqlite3_test_errstr",     test_errstr, 0             },
4636      { "tcl_variable_type",       tcl_variable_type, 0       },
4637 #ifndef SQLITE_OMIT_SHARED_CACHE
4638      { "sqlite3_enable_shared_cache", test_enable_shared, 0  },
4639      { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0},
4640 #endif
4641      { "sqlite3_libversion_number", test_libversion_number, 0  },
4642 #ifdef SQLITE_ENABLE_COLUMN_METADATA
4643      { "sqlite3_table_column_metadata", test_table_column_metadata, 0  },
4644 #endif
4645 #ifndef SQLITE_OMIT_INCRBLOB
4646      { "sqlite3_blob_read",  test_blob_read, 0  },
4647      { "sqlite3_blob_write", test_blob_write, 0  },
4648 #endif
4649   };
4650   static int bitmask_size = sizeof(Bitmask)*8;
4651   int i;
4652   extern int sqlite3_os_trace;
4653   extern int sqlite3_where_trace;
4654   extern int sqlite3_sync_count, sqlite3_fullsync_count;
4655   extern int sqlite3_opentemp_count;
4656   extern int sqlite3_memUsed;
4657   extern char *sqlite3_malloc_id;
4658   extern int sqlite3_memMax;
4659   extern int sqlite3_like_count;
4660   extern int sqlite3_tsd_count;
4661   extern int sqlite3_xferopt_count;
4662   extern int sqlite3_pager_readdb_count;
4663   extern int sqlite3_pager_writedb_count;
4664   extern int sqlite3_pager_writej_count;
4665   extern int sqlite3_pager_pgfree_count;
4666 #if OS_UNIX && defined(SQLITE_TEST) && defined(THREADSAFE) && THREADSAFE
4667   extern int threadsOverrideEachOthersLocks;
4668 #endif
4669 #if OS_WIN
4670   extern int sqlite3_os_type;
4671 #endif
4672 #ifdef SQLITE_DEBUG
4673   extern int sqlite3_vdbe_addop_trace;
4674 #endif
4675 #ifdef SQLITE_TEST
4676   extern char sqlite3_query_plan[];
4677   static char *query_plan = sqlite3_query_plan;
4678 #endif
4679 
4680   for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
4681     Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
4682   }
4683   for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
4684     Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
4685         aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
4686   }
4687   Tcl_LinkVar(interp, "sqlite_search_count",
4688       (char*)&sqlite3_search_count, TCL_LINK_INT);
4689   Tcl_LinkVar(interp, "sqlite_sort_count",
4690       (char*)&sqlite3_sort_count, TCL_LINK_INT);
4691   Tcl_LinkVar(interp, "sqlite3_max_blobsize",
4692       (char*)&sqlite3_max_blobsize, TCL_LINK_INT);
4693   Tcl_LinkVar(interp, "sqlite_like_count",
4694       (char*)&sqlite3_like_count, TCL_LINK_INT);
4695   Tcl_LinkVar(interp, "sqlite_interrupt_count",
4696       (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
4697   Tcl_LinkVar(interp, "sqlite_open_file_count",
4698       (char*)&sqlite3_open_file_count, TCL_LINK_INT);
4699   Tcl_LinkVar(interp, "sqlite_current_time",
4700       (char*)&sqlite3_current_time, TCL_LINK_INT);
4701   Tcl_LinkVar(interp, "sqlite_os_trace",
4702       (char*)&sqlite3_os_trace, TCL_LINK_INT);
4703   Tcl_LinkVar(interp, "sqlite3_tsd_count",
4704       (char*)&sqlite3_tsd_count, TCL_LINK_INT);
4705   Tcl_LinkVar(interp, "sqlite3_xferopt_count",
4706       (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
4707   Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
4708       (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);
4709   Tcl_LinkVar(interp, "sqlite3_pager_writedb_count",
4710       (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT);
4711   Tcl_LinkVar(interp, "sqlite3_pager_writej_count",
4712       (char*)&sqlite3_pager_writej_count, TCL_LINK_INT);
4713   Tcl_LinkVar(interp, "sqlite3_pager_pgfree_count",
4714       (char*)&sqlite3_pager_pgfree_count, TCL_LINK_INT);
4715 #ifndef SQLITE_OMIT_UTF16
4716   Tcl_LinkVar(interp, "unaligned_string_counter",
4717       (char*)&unaligned_string_counter, TCL_LINK_INT);
4718 #endif
4719 #if OS_UNIX && defined(SQLITE_TEST) && defined(THREADSAFE) && THREADSAFE
4720   Tcl_LinkVar(interp, "threadsOverrideEachOthersLocks",
4721       (char*)&threadsOverrideEachOthersLocks, TCL_LINK_INT);
4722 #endif
4723 #ifndef SQLITE_OMIT_UTF16
4724   Tcl_LinkVar(interp, "sqlite_last_needed_collation",
4725       (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
4726 #endif
4727 #ifdef SQLITE_MEMDEBUG
4728   Tcl_LinkVar(interp, "sqlite_malloc_id",
4729       (char*)&sqlite3_malloc_id, TCL_LINK_STRING);
4730 #endif
4731 #if OS_WIN
4732   Tcl_LinkVar(interp, "sqlite_os_type",
4733       (char*)&sqlite3_os_type, TCL_LINK_INT);
4734 #endif
4735 #ifdef SQLITE_TEST
4736   Tcl_LinkVar(interp, "sqlite_query_plan",
4737       (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
4738 #endif
4739 #ifdef SQLITE_DEBUG
4740   Tcl_LinkVar(interp, "sqlite_addop_trace",
4741       (char*)&sqlite3_vdbe_addop_trace, TCL_LINK_INT);
4742   Tcl_LinkVar(interp, "sqlite_where_trace",
4743       (char*)&sqlite3_where_trace, TCL_LINK_INT);
4744 #endif
4745 #ifdef SQLITE_MEMDEBUG
4746   Tcl_LinkVar(interp, "sqlite_memused",
4747       (char*)&sqlite3_memUsed, TCL_LINK_INT | TCL_LINK_READ_ONLY);
4748   Tcl_LinkVar(interp, "sqlite_memmax",
4749       (char*)&sqlite3_memMax, TCL_LINK_INT | TCL_LINK_READ_ONLY);
4750 #endif
4751 #ifndef SQLITE_OMIT_DISKIO
4752   Tcl_LinkVar(interp, "sqlite_opentemp_count",
4753       (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
4754 #endif
4755   Tcl_LinkVar(interp, "sqlite_static_bind_value",
4756       (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
4757   Tcl_LinkVar(interp, "sqlite_static_bind_nbyte",
4758       (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT);
4759   Tcl_LinkVar(interp, "sqlite_temp_directory",
4760       (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
4761   Tcl_LinkVar(interp, "bitmask_size",
4762       (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
4763 #if OS_UNIX
4764   Tcl_LinkVar(interp, "sqlite_sync_count",
4765       (char*)&sqlite3_sync_count, TCL_LINK_INT);
4766   Tcl_LinkVar(interp, "sqlite_fullsync_count",
4767       (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
4768 #endif /* OS_UNIX */
4769   set_options(interp);
4770 
4771   return TCL_OK;
4772 }
4773