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