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