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