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