os/persistentdata/persistentstorage/sqlite3api/TEST/SRC/test1.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/SRC/test1.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,4818 @@
     1.4 +/*
     1.5 +** 2001 September 15
     1.6 +**
     1.7 +** The author disclaims copyright to this source code.  In place of
     1.8 +** a legal notice, here is a blessing:
     1.9 +**
    1.10 +**    May you do good and not evil.
    1.11 +**    May you find forgiveness for yourself and forgive others.
    1.12 +**    May you share freely, never taking more than you give.
    1.13 +**
    1.14 +*************************************************************************
    1.15 +** Code for testing all sorts of SQLite interfaces.  This code
    1.16 +** is not included in the SQLite library.  It is used for automated
    1.17 +** testing of the SQLite library.
    1.18 +**
    1.19 +** $Id: test1.c,v 1.326 2008/10/02 14:49:02 danielk1977 Exp $
    1.20 +*/
    1.21 +#include "sqliteInt.h"
    1.22 +#include "tcl.h"
    1.23 +#include <stdlib.h>
    1.24 +#include <string.h>
    1.25 +
    1.26 +/*
    1.27 +** This is a copy of the first part of the SqliteDb structure in 
    1.28 +** tclsqlite.c.  We need it here so that the get_sqlite_pointer routine
    1.29 +** can extract the sqlite3* pointer from an existing Tcl SQLite
    1.30 +** connection.
    1.31 +*/
    1.32 +struct SqliteDb {
    1.33 +  sqlite3 *db;
    1.34 +};
    1.35 +
    1.36 +/*
    1.37 +** Convert text generated by the "%p" conversion format back into
    1.38 +** a pointer.
    1.39 +*/
    1.40 +static int testHexToInt(int h){
    1.41 +  if( h>='0' && h<='9' ){
    1.42 +    return h - '0';
    1.43 +  }else if( h>='a' && h<='f' ){
    1.44 +    return h - 'a' + 10;
    1.45 +  }else{
    1.46 +    assert( h>='A' && h<='F' );
    1.47 +    return h - 'A' + 10;
    1.48 +  }
    1.49 +}
    1.50 +void *sqlite3TestTextToPtr(const char *z){
    1.51 +  void *p;
    1.52 +  u64 v;
    1.53 +  u32 v2;
    1.54 +  if( z[0]=='0' && z[1]=='x' ){
    1.55 +    z += 2;
    1.56 +  }
    1.57 +  v = 0;
    1.58 +  while( *z ){
    1.59 +    v = (v<<4) + testHexToInt(*z);
    1.60 +    z++;
    1.61 +  }
    1.62 +  if( sizeof(p)==sizeof(v) ){
    1.63 +    memcpy(&p, &v, sizeof(p));
    1.64 +  }else{
    1.65 +    assert( sizeof(p)==sizeof(v2) );
    1.66 +    v2 = (u32)v;
    1.67 +    memcpy(&p, &v2, sizeof(p));
    1.68 +  }
    1.69 +  return p;
    1.70 +}
    1.71 +
    1.72 +
    1.73 +/*
    1.74 +** A TCL command that returns the address of the sqlite* pointer
    1.75 +** for an sqlite connection instance.  Bad things happen if the
    1.76 +** input is not an sqlite connection.
    1.77 +*/
    1.78 +static int get_sqlite_pointer(
    1.79 +  void * clientData,
    1.80 +  Tcl_Interp *interp,
    1.81 +  int objc,
    1.82 +  Tcl_Obj *CONST objv[]
    1.83 +){
    1.84 +  struct SqliteDb *p;
    1.85 +  Tcl_CmdInfo cmdInfo;
    1.86 +  char zBuf[100];
    1.87 +  if( objc!=2 ){
    1.88 +    Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION");
    1.89 +    return TCL_ERROR;
    1.90 +  }
    1.91 +  if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
    1.92 +    Tcl_AppendResult(interp, "command not found: ",
    1.93 +           Tcl_GetString(objv[1]), (char*)0);
    1.94 +    return TCL_ERROR;
    1.95 +  }
    1.96 +  p = (struct SqliteDb*)cmdInfo.objClientData;
    1.97 +  sprintf(zBuf, "%p", p->db);
    1.98 +  if( strncmp(zBuf,"0x",2) ){
    1.99 +    sprintf(zBuf, "0x%p", p->db);
   1.100 +  }
   1.101 +  Tcl_AppendResult(interp, zBuf, 0);
   1.102 +  return TCL_OK;
   1.103 +}
   1.104 +
   1.105 +/*
   1.106 +** Decode a pointer to an sqlite3 object.
   1.107 +*/
   1.108 +int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
   1.109 +  struct SqliteDb *p;
   1.110 +  Tcl_CmdInfo cmdInfo;
   1.111 +  if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){
   1.112 +    p = (struct SqliteDb*)cmdInfo.objClientData;
   1.113 +    *ppDb = p->db;
   1.114 +  }else{
   1.115 +    *ppDb = (sqlite3*)sqlite3TestTextToPtr(zA);
   1.116 +  }
   1.117 +  return TCL_OK;
   1.118 +}
   1.119 +
   1.120 +
   1.121 +const char *sqlite3TestErrorName(int rc){
   1.122 +  const char *zName = 0;
   1.123 +  switch( rc & 0xff ){
   1.124 +    case SQLITE_OK:         zName = "SQLITE_OK";          break;
   1.125 +    case SQLITE_ERROR:      zName = "SQLITE_ERROR";       break;
   1.126 +    case SQLITE_PERM:       zName = "SQLITE_PERM";        break;
   1.127 +    case SQLITE_ABORT:      zName = "SQLITE_ABORT";       break;
   1.128 +    case SQLITE_BUSY:       zName = "SQLITE_BUSY";        break;
   1.129 +    case SQLITE_LOCKED:     zName = "SQLITE_LOCKED";      break;
   1.130 +    case SQLITE_NOMEM:      zName = "SQLITE_NOMEM";       break;
   1.131 +    case SQLITE_READONLY:   zName = "SQLITE_READONLY";    break;
   1.132 +    case SQLITE_INTERRUPT:  zName = "SQLITE_INTERRUPT";   break;
   1.133 +    case SQLITE_IOERR:      zName = "SQLITE_IOERR";       break;
   1.134 +    case SQLITE_CORRUPT:    zName = "SQLITE_CORRUPT";     break;
   1.135 +    case SQLITE_FULL:       zName = "SQLITE_FULL";        break;
   1.136 +    case SQLITE_CANTOPEN:   zName = "SQLITE_CANTOPEN";    break;
   1.137 +    case SQLITE_PROTOCOL:   zName = "SQLITE_PROTOCOL";    break;
   1.138 +    case SQLITE_EMPTY:      zName = "SQLITE_EMPTY";       break;
   1.139 +    case SQLITE_SCHEMA:     zName = "SQLITE_SCHEMA";      break;
   1.140 +    case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT";  break;
   1.141 +    case SQLITE_MISMATCH:   zName = "SQLITE_MISMATCH";    break;
   1.142 +    case SQLITE_MISUSE:     zName = "SQLITE_MISUSE";      break;
   1.143 +    case SQLITE_NOLFS:      zName = "SQLITE_NOLFS";       break;
   1.144 +    case SQLITE_AUTH:       zName = "SQLITE_AUTH";        break;
   1.145 +    case SQLITE_FORMAT:     zName = "SQLITE_FORMAT";      break;
   1.146 +    case SQLITE_RANGE:      zName = "SQLITE_RANGE";       break;
   1.147 +    case SQLITE_ROW:        zName = "SQLITE_ROW";         break;
   1.148 +    case SQLITE_DONE:       zName = "SQLITE_DONE";        break;
   1.149 +    case SQLITE_NOTADB:     zName = "SQLITE_NOTADB";      break;
   1.150 +    case SQLITE_TOOBIG:     zName = "SQLITE_TOOBIG";      break;
   1.151 +    default:                zName = "SQLITE_Unknown";     break;
   1.152 +  }
   1.153 +  return zName;
   1.154 +}
   1.155 +#define t1ErrorName sqlite3TestErrorName
   1.156 +
   1.157 +/*
   1.158 +** Convert an sqlite3_stmt* into an sqlite3*.  This depends on the
   1.159 +** fact that the sqlite3* is the first field in the Vdbe structure.
   1.160 +*/
   1.161 +#define StmtToDb(X)   sqlite3_db_handle(X)
   1.162 +
   1.163 +/*
   1.164 +** Check a return value to make sure it agrees with the results
   1.165 +** from sqlite3_errcode.
   1.166 +*/
   1.167 +int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){
   1.168 +  if( rc!=SQLITE_MISUSE && rc!=SQLITE_OK && sqlite3_errcode(db)!=rc ){
   1.169 +    char zBuf[200];
   1.170 +    int r2 = sqlite3_errcode(db);
   1.171 +    sprintf(zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)",
   1.172 +       t1ErrorName(rc), rc, t1ErrorName(r2), r2);
   1.173 +    Tcl_ResetResult(interp);
   1.174 +    Tcl_AppendResult(interp, zBuf, 0);
   1.175 +    return 1;
   1.176 +  }
   1.177 +  return 0;
   1.178 +}
   1.179 +
   1.180 +/*
   1.181 +** Decode a pointer to an sqlite3_stmt object.
   1.182 +*/
   1.183 +static int getStmtPointer(
   1.184 +  Tcl_Interp *interp, 
   1.185 +  const char *zArg,  
   1.186 +  sqlite3_stmt **ppStmt
   1.187 +){
   1.188 +  *ppStmt = (sqlite3_stmt*)sqlite3TestTextToPtr(zArg);
   1.189 +  return TCL_OK;
   1.190 +}
   1.191 +
   1.192 +/*
   1.193 +** Generate a text representation of a pointer that can be understood
   1.194 +** by the getDbPointer and getVmPointer routines above.
   1.195 +**
   1.196 +** The problem is, on some machines (Solaris) if you do a printf with
   1.197 +** "%p" you cannot turn around and do a scanf with the same "%p" and
   1.198 +** get your pointer back.  You have to prepend a "0x" before it will
   1.199 +** work.  Or at least that is what is reported to me (drh).  But this
   1.200 +** behavior varies from machine to machine.  The solution used her is
   1.201 +** to test the string right after it is generated to see if it can be
   1.202 +** understood by scanf, and if not, try prepending an "0x" to see if
   1.203 +** that helps.  If nothing works, a fatal error is generated.
   1.204 +*/
   1.205 +int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){
   1.206 +  sqlite3_snprintf(100, zPtr, "%p", p);
   1.207 +  return TCL_OK;
   1.208 +}
   1.209 +
   1.210 +/*
   1.211 +** The callback routine for sqlite3_exec_printf().
   1.212 +*/
   1.213 +static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){
   1.214 +  Tcl_DString *str = (Tcl_DString*)pArg;
   1.215 +  int i;
   1.216 +
   1.217 +  if( Tcl_DStringLength(str)==0 ){
   1.218 +    for(i=0; i<argc; i++){
   1.219 +      Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL");
   1.220 +    }
   1.221 +  }
   1.222 +  for(i=0; i<argc; i++){
   1.223 +    Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL");
   1.224 +  }
   1.225 +  return 0;
   1.226 +}
   1.227 +
   1.228 +/*
   1.229 +** The I/O tracing callback.
   1.230 +*/
   1.231 +#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
   1.232 +static FILE *iotrace_file = 0;
   1.233 +static void io_trace_callback(const char *zFormat, ...){
   1.234 +  va_list ap;
   1.235 +  va_start(ap, zFormat);
   1.236 +  vfprintf(iotrace_file, zFormat, ap);
   1.237 +  va_end(ap);
   1.238 +  fflush(iotrace_file);
   1.239 +}
   1.240 +#endif
   1.241 +
   1.242 +/*
   1.243 +** Usage:  io_trace FILENAME
   1.244 +**
   1.245 +** Turn I/O tracing on or off.  If FILENAME is not an empty string,
   1.246 +** I/O tracing begins going into FILENAME. If FILENAME is an empty
   1.247 +** string, I/O tracing is turned off.
   1.248 +*/
   1.249 +static int test_io_trace(
   1.250 +  void *NotUsed,
   1.251 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   1.252 +  int argc,              /* Number of arguments */
   1.253 +  char **argv            /* Text of each argument */
   1.254 +){
   1.255 +#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
   1.256 +  if( argc!=2 ){
   1.257 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
   1.258 +          " FILENAME\"", 0);
   1.259 +    return TCL_ERROR;
   1.260 +  }
   1.261 +  if( iotrace_file ){
   1.262 +    if( iotrace_file!=stdout && iotrace_file!=stderr ){
   1.263 +      fclose(iotrace_file);
   1.264 +    }
   1.265 +    iotrace_file = 0;
   1.266 +    sqlite3IoTrace = 0;
   1.267 +  }
   1.268 +  if( argv[1][0] ){
   1.269 +    if( strcmp(argv[1],"stdout")==0 ){
   1.270 +      iotrace_file = stdout;
   1.271 +    }else if( strcmp(argv[1],"stderr")==0 ){
   1.272 +      iotrace_file = stderr;
   1.273 +    }else{
   1.274 +      iotrace_file = fopen(argv[1], "w");
   1.275 +    }
   1.276 +    sqlite3IoTrace = io_trace_callback;
   1.277 +  }
   1.278 +#endif
   1.279 +  return TCL_OK;
   1.280 +}
   1.281 +
   1.282 +
   1.283 +/*
   1.284 +** Usage:  sqlite3_exec_printf  DB  FORMAT  STRING
   1.285 +**
   1.286 +** Invoke the sqlite3_exec_printf() interface using the open database
   1.287 +** DB.  The SQL is the string FORMAT.  The format string should contain
   1.288 +** one %s or %q.  STRING is the value inserted into %s or %q.
   1.289 +*/
   1.290 +static int test_exec_printf(
   1.291 +  void *NotUsed,
   1.292 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   1.293 +  int argc,              /* Number of arguments */
   1.294 +  char **argv            /* Text of each argument */
   1.295 +){
   1.296 +  sqlite3 *db;
   1.297 +  Tcl_DString str;
   1.298 +  int rc;
   1.299 +  char *zErr = 0;
   1.300 +  char *zSql;
   1.301 +  char zBuf[30];
   1.302 +  if( argc!=4 ){
   1.303 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
   1.304 +       " DB FORMAT STRING", 0);
   1.305 +    return TCL_ERROR;
   1.306 +  }
   1.307 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
   1.308 +  Tcl_DStringInit(&str);
   1.309 +  zSql = sqlite3_mprintf(argv[2], argv[3]);
   1.310 +  rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
   1.311 +  sqlite3_free(zSql);
   1.312 +  sprintf(zBuf, "%d", rc);
   1.313 +  Tcl_AppendElement(interp, zBuf);
   1.314 +  Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
   1.315 +  Tcl_DStringFree(&str);
   1.316 +  if( zErr ) sqlite3_free(zErr);
   1.317 +  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
   1.318 +  return TCL_OK;
   1.319 +}
   1.320 +
   1.321 +/*
   1.322 +** Usage:  db_enter DB
   1.323 +**         db_leave DB
   1.324 +**
   1.325 +** Enter or leave the mutex on a database connection.
   1.326 +*/
   1.327 +static int db_enter(
   1.328 +  void *NotUsed,
   1.329 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   1.330 +  int argc,              /* Number of arguments */
   1.331 +  char **argv            /* Text of each argument */
   1.332 +){
   1.333 +  sqlite3 *db;
   1.334 +  if( argc!=2 ){
   1.335 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
   1.336 +       " DB", 0);
   1.337 +    return TCL_ERROR;
   1.338 +  }
   1.339 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
   1.340 +  sqlite3_mutex_enter(db->mutex);
   1.341 +  return TCL_OK;
   1.342 +}
   1.343 +static int db_leave(
   1.344 +  void *NotUsed,
   1.345 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   1.346 +  int argc,              /* Number of arguments */
   1.347 +  char **argv            /* Text of each argument */
   1.348 +){
   1.349 +  sqlite3 *db;
   1.350 +  if( argc!=2 ){
   1.351 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
   1.352 +       " DB", 0);
   1.353 +    return TCL_ERROR;
   1.354 +  }
   1.355 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
   1.356 +  sqlite3_mutex_leave(db->mutex);
   1.357 +  return TCL_OK;
   1.358 +}
   1.359 +
   1.360 +/*
   1.361 +** Usage:  sqlite3_exec  DB  SQL
   1.362 +**
   1.363 +** Invoke the sqlite3_exec interface using the open database DB
   1.364 +*/
   1.365 +static int test_exec(
   1.366 +  void *NotUsed,
   1.367 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   1.368 +  int argc,              /* Number of arguments */
   1.369 +  char **argv            /* Text of each argument */
   1.370 +){
   1.371 +  sqlite3 *db;
   1.372 +  Tcl_DString str;
   1.373 +  int rc;
   1.374 +  char *zErr = 0;
   1.375 +  char *zSql;
   1.376 +  int i, j;
   1.377 +  char zBuf[30];
   1.378 +  if( argc!=3 ){
   1.379 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
   1.380 +       " DB SQL", 0);
   1.381 +    return TCL_ERROR;
   1.382 +  }
   1.383 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
   1.384 +  Tcl_DStringInit(&str);
   1.385 +  zSql = sqlite3_mprintf("%s", argv[2]);
   1.386 +  for(i=j=0; zSql[i];){
   1.387 +    if( zSql[i]=='%' ){
   1.388 +      zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]);
   1.389 +      i += 3;
   1.390 +    }else{
   1.391 +      zSql[j++] = zSql[i++];
   1.392 +    }
   1.393 +  }
   1.394 +  zSql[j] = 0;
   1.395 +  rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
   1.396 +  sqlite3_free(zSql);
   1.397 +  sprintf(zBuf, "%d", rc);
   1.398 +  Tcl_AppendElement(interp, zBuf);
   1.399 +  Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
   1.400 +  Tcl_DStringFree(&str);
   1.401 +  if( zErr ) sqlite3_free(zErr);
   1.402 +  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
   1.403 +  return TCL_OK;
   1.404 +}
   1.405 +
   1.406 +/*
   1.407 +** Usage:  sqlite3_exec_nr  DB  SQL
   1.408 +**
   1.409 +** Invoke the sqlite3_exec interface using the open database DB.  Discard
   1.410 +** all results
   1.411 +*/
   1.412 +static int test_exec_nr(
   1.413 +  void *NotUsed,
   1.414 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   1.415 +  int argc,              /* Number of arguments */
   1.416 +  char **argv            /* Text of each argument */
   1.417 +){
   1.418 +  sqlite3 *db;
   1.419 +  int rc;
   1.420 +  char *zErr = 0;
   1.421 +  if( argc!=3 ){
   1.422 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
   1.423 +       " DB SQL", 0);
   1.424 +    return TCL_ERROR;
   1.425 +  }
   1.426 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
   1.427 +  rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
   1.428 +  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
   1.429 +  return TCL_OK;
   1.430 +}
   1.431 +
   1.432 +/*
   1.433 +** Usage:  sqlite3_mprintf_z_test  SEPARATOR  ARG0  ARG1 ...
   1.434 +**
   1.435 +** Test the %z format of sqliteMPrintf().  Use multiple mprintf() calls to 
   1.436 +** concatenate arg0 through argn using separator as the separator.
   1.437 +** Return the result.
   1.438 +*/
   1.439 +static int test_mprintf_z(
   1.440 +  void *NotUsed,
   1.441 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   1.442 +  int argc,              /* Number of arguments */
   1.443 +  char **argv            /* Text of each argument */
   1.444 +){
   1.445 +  char *zResult = 0;
   1.446 +  int i;
   1.447 +
   1.448 +  for(i=2; i<argc && (i==2 || zResult); i++){
   1.449 +    zResult = sqlite3MPrintf(0, "%z%s%s", zResult, argv[1], argv[i]);
   1.450 +  }
   1.451 +  Tcl_AppendResult(interp, zResult, 0);
   1.452 +  sqlite3_free(zResult);
   1.453 +  return TCL_OK;
   1.454 +}
   1.455 +
   1.456 +/*
   1.457 +** Usage:  sqlite3_mprintf_n_test  STRING
   1.458 +**
   1.459 +** Test the %n format of sqliteMPrintf().  Return the length of the
   1.460 +** input string.
   1.461 +*/
   1.462 +static int test_mprintf_n(
   1.463 +  void *NotUsed,
   1.464 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   1.465 +  int argc,              /* Number of arguments */
   1.466 +  char **argv            /* Text of each argument */
   1.467 +){
   1.468 +  char *zStr;
   1.469 +  int n = 0;
   1.470 +  zStr = sqlite3MPrintf(0, "%s%n", argv[1], &n);
   1.471 +  sqlite3_free(zStr);
   1.472 +  Tcl_SetObjResult(interp, Tcl_NewIntObj(n));
   1.473 +  return TCL_OK;
   1.474 +}
   1.475 +
   1.476 +/*
   1.477 +** Usage:  sqlite3_snprintf_int  SIZE FORMAT  INT
   1.478 +**
   1.479 +** Test the of sqlite3_snprintf() routine.  SIZE is the size of the
   1.480 +** output buffer in bytes.  The maximum size is 100.  FORMAT is the
   1.481 +** format string.  INT is a single integer argument.  The FORMAT
   1.482 +** string must require no more than this one integer argument.  If
   1.483 +** You pass in a format string that requires more than one argument,
   1.484 +** bad things will happen.
   1.485 +*/
   1.486 +static int test_snprintf_int(
   1.487 +  void *NotUsed,
   1.488 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   1.489 +  int argc,              /* Number of arguments */
   1.490 +  char **argv            /* Text of each argument */
   1.491 +){
   1.492 +  char zStr[100];
   1.493 +  int n = atoi(argv[1]);
   1.494 +  const char *zFormat = argv[2];
   1.495 +  int a1 = atoi(argv[3]);
   1.496 +  if( n>sizeof(zStr) ) n = sizeof(zStr);
   1.497 +  sqlite3_snprintf(sizeof(zStr), zStr, "abcdefghijklmnopqrstuvwxyz");
   1.498 +  sqlite3_snprintf(n, zStr, zFormat, a1);
   1.499 +  Tcl_AppendResult(interp, zStr, 0);
   1.500 +  return TCL_OK;
   1.501 +}
   1.502 +
   1.503 +#ifndef SQLITE_OMIT_GET_TABLE
   1.504 +
   1.505 +/*
   1.506 +** Usage:  sqlite3_get_table_printf  DB  FORMAT  STRING  ?--no-counts?
   1.507 +**
   1.508 +** Invoke the sqlite3_get_table_printf() interface using the open database
   1.509 +** DB.  The SQL is the string FORMAT.  The format string should contain
   1.510 +** one %s or %q.  STRING is the value inserted into %s or %q.
   1.511 +*/
   1.512 +static int test_get_table_printf(
   1.513 +  void *NotUsed,
   1.514 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   1.515 +  int argc,              /* Number of arguments */
   1.516 +  char **argv            /* Text of each argument */
   1.517 +){
   1.518 +  sqlite3 *db;
   1.519 +  Tcl_DString str;
   1.520 +  int rc;
   1.521 +  char *zErr = 0;
   1.522 +  int nRow, nCol;
   1.523 +  char **aResult;
   1.524 +  int i;
   1.525 +  char zBuf[30];
   1.526 +  char *zSql;
   1.527 +  int resCount = -1;
   1.528 +  if( argc==5 ){
   1.529 +    if( Tcl_GetInt(interp, argv[4], &resCount) ) return TCL_ERROR;
   1.530 +  }
   1.531 +  if( argc!=4 && argc!=5 ){
   1.532 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
   1.533 +       " DB FORMAT STRING ?COUNT?", 0);
   1.534 +    return TCL_ERROR;
   1.535 +  }
   1.536 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
   1.537 +  Tcl_DStringInit(&str);
   1.538 +  zSql = sqlite3_mprintf(argv[2],argv[3]);
   1.539 +  if( argc==5 ){
   1.540 +    rc = sqlite3_get_table(db, zSql, &aResult, 0, 0, &zErr);
   1.541 +  }else{
   1.542 +    rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr);
   1.543 +    resCount = (nRow+1)*nCol;
   1.544 +  }
   1.545 +  sqlite3_free(zSql);
   1.546 +  sprintf(zBuf, "%d", rc);
   1.547 +  Tcl_AppendElement(interp, zBuf);
   1.548 +  if( rc==SQLITE_OK ){
   1.549 +    if( argc==4 ){
   1.550 +      sprintf(zBuf, "%d", nRow);
   1.551 +      Tcl_AppendElement(interp, zBuf);
   1.552 +      sprintf(zBuf, "%d", nCol);
   1.553 +      Tcl_AppendElement(interp, zBuf);
   1.554 +    }
   1.555 +    for(i=0; i<resCount; i++){
   1.556 +      Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL");
   1.557 +    }
   1.558 +  }else{
   1.559 +    Tcl_AppendElement(interp, zErr);
   1.560 +  }
   1.561 +  sqlite3_free_table(aResult);
   1.562 +  if( zErr ) sqlite3_free(zErr);
   1.563 +  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
   1.564 +  return TCL_OK;
   1.565 +}
   1.566 +
   1.567 +#endif /* SQLITE_OMIT_GET_TABLE */
   1.568 +
   1.569 +
   1.570 +/*
   1.571 +** Usage:  sqlite3_last_insert_rowid DB
   1.572 +**
   1.573 +** Returns the integer ROWID of the most recent insert.
   1.574 +*/
   1.575 +static int test_last_rowid(
   1.576 +  void *NotUsed,
   1.577 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   1.578 +  int argc,              /* Number of arguments */
   1.579 +  char **argv            /* Text of each argument */
   1.580 +){
   1.581 +  sqlite3 *db;
   1.582 +  char zBuf[30];
   1.583 +
   1.584 +  if( argc!=2 ){
   1.585 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0);
   1.586 +    return TCL_ERROR;
   1.587 +  }
   1.588 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
   1.589 +  sprintf(zBuf, "%lld", sqlite3_last_insert_rowid(db));
   1.590 +  Tcl_AppendResult(interp, zBuf, 0);
   1.591 +  return SQLITE_OK;
   1.592 +}
   1.593 +
   1.594 +/*
   1.595 +** Usage:  sqlite3_key DB KEY
   1.596 +**
   1.597 +** Set the codec key.
   1.598 +*/
   1.599 +static int test_key(
   1.600 +  void *NotUsed,
   1.601 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   1.602 +  int argc,              /* Number of arguments */
   1.603 +  char **argv            /* Text of each argument */
   1.604 +){
   1.605 +  sqlite3 *db;
   1.606 +  const char *zKey;
   1.607 +  int nKey;
   1.608 +  if( argc!=3 ){
   1.609 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
   1.610 +       " FILENAME\"", 0);
   1.611 +    return TCL_ERROR;
   1.612 +  }
   1.613 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
   1.614 +  zKey = argv[2];
   1.615 +  nKey = strlen(zKey);
   1.616 +#ifdef SQLITE_HAS_CODEC
   1.617 +  sqlite3_key(db, zKey, nKey);
   1.618 +#endif
   1.619 +  return TCL_OK;
   1.620 +}
   1.621 +
   1.622 +/*
   1.623 +** Usage:  sqlite3_rekey DB KEY
   1.624 +**
   1.625 +** Change the codec key.
   1.626 +*/
   1.627 +static int test_rekey(
   1.628 +  void *NotUsed,
   1.629 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   1.630 +  int argc,              /* Number of arguments */
   1.631 +  char **argv            /* Text of each argument */
   1.632 +){
   1.633 +  sqlite3 *db;
   1.634 +  const char *zKey;
   1.635 +  int nKey;
   1.636 +  if( argc!=3 ){
   1.637 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
   1.638 +       " FILENAME\"", 0);
   1.639 +    return TCL_ERROR;
   1.640 +  }
   1.641 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
   1.642 +  zKey = argv[2];
   1.643 +  nKey = strlen(zKey);
   1.644 +#ifdef SQLITE_HAS_CODEC
   1.645 +  sqlite3_rekey(db, zKey, nKey);
   1.646 +#endif
   1.647 +  return TCL_OK;
   1.648 +}
   1.649 +
   1.650 +/*
   1.651 +** Usage:  sqlite3_close DB
   1.652 +**
   1.653 +** Closes the database opened by sqlite3_open.
   1.654 +*/
   1.655 +static int sqlite_test_close(
   1.656 +  void *NotUsed,
   1.657 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   1.658 +  int argc,              /* Number of arguments */
   1.659 +  char **argv            /* Text of each argument */
   1.660 +){
   1.661 +  sqlite3 *db;
   1.662 +  int rc;
   1.663 +  if( argc!=2 ){
   1.664 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
   1.665 +       " FILENAME\"", 0);
   1.666 +    return TCL_ERROR;
   1.667 +  }
   1.668 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
   1.669 +  rc = sqlite3_close(db);
   1.670 +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
   1.671 +  return TCL_OK;
   1.672 +}
   1.673 +
   1.674 +/*
   1.675 +** Implementation of the x_coalesce() function.
   1.676 +** Return the first argument non-NULL argument.
   1.677 +*/
   1.678 +static void t1_ifnullFunc(
   1.679 +  sqlite3_context *context,
   1.680 +  int argc,
   1.681 +  sqlite3_value **argv
   1.682 +){
   1.683 +  int i;
   1.684 +  for(i=0; i<argc; i++){
   1.685 +    if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
   1.686 +      int n = sqlite3_value_bytes(argv[i]);
   1.687 +      sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]),
   1.688 +          n, SQLITE_TRANSIENT);
   1.689 +      break;
   1.690 +    }
   1.691 +  }
   1.692 +}
   1.693 +
   1.694 +/*
   1.695 +** These are test functions.    hex8() interprets its argument as
   1.696 +** UTF8 and returns a hex encoding.  hex16le() interprets its argument
   1.697 +** as UTF16le and returns a hex encoding.
   1.698 +*/
   1.699 +static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){
   1.700 +  const unsigned char *z;
   1.701 +  int i;
   1.702 +  char zBuf[200];
   1.703 +  z = sqlite3_value_text(argv[0]);
   1.704 +  for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){
   1.705 +    sprintf(&zBuf[i*2], "%02x", z[i]&0xff);
   1.706 +  }
   1.707 +  zBuf[i*2] = 0;
   1.708 +  sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
   1.709 +}
   1.710 +#ifndef SQLITE_OMIT_UTF16
   1.711 +static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){
   1.712 +  const unsigned short int *z;
   1.713 +  int i;
   1.714 +  char zBuf[400];
   1.715 +  z = sqlite3_value_text16(argv[0]);
   1.716 +  for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){
   1.717 +    sprintf(&zBuf[i*4], "%04x", z[i]&0xff);
   1.718 +  }
   1.719 +  zBuf[i*4] = 0;
   1.720 +  sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
   1.721 +}
   1.722 +#endif
   1.723 +
   1.724 +/*
   1.725 +** A structure into which to accumulate text.
   1.726 +*/
   1.727 +struct dstr {
   1.728 +  int nAlloc;  /* Space allocated */
   1.729 +  int nUsed;   /* Space used */
   1.730 +  char *z;     /* The space */
   1.731 +};
   1.732 +
   1.733 +/*
   1.734 +** Append text to a dstr
   1.735 +*/
   1.736 +static void dstrAppend(struct dstr *p, const char *z, int divider){
   1.737 +  int n = strlen(z);
   1.738 +  if( p->nUsed + n + 2 > p->nAlloc ){
   1.739 +    char *zNew;
   1.740 +    p->nAlloc = p->nAlloc*2 + n + 200;
   1.741 +    zNew = sqlite3_realloc(p->z, p->nAlloc);
   1.742 +    if( zNew==0 ){
   1.743 +      sqlite3_free(p->z);
   1.744 +      memset(p, 0, sizeof(*p));
   1.745 +      return;
   1.746 +    }
   1.747 +    p->z = zNew;
   1.748 +  }
   1.749 +  if( divider && p->nUsed>0 ){
   1.750 +    p->z[p->nUsed++] = divider;
   1.751 +  }
   1.752 +  memcpy(&p->z[p->nUsed], z, n+1);
   1.753 +  p->nUsed += n;
   1.754 +}
   1.755 +
   1.756 +/*
   1.757 +** Invoked for each callback from sqlite3ExecFunc
   1.758 +*/
   1.759 +static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){
   1.760 +  struct dstr *p = (struct dstr*)pData;
   1.761 +  int i;
   1.762 +  for(i=0; i<argc; i++){
   1.763 +    if( argv[i]==0 ){
   1.764 +      dstrAppend(p, "NULL", ' ');
   1.765 +    }else{
   1.766 +      dstrAppend(p, argv[i], ' ');
   1.767 +    }
   1.768 +  }
   1.769 +  return 0;
   1.770 +}
   1.771 +
   1.772 +/*
   1.773 +** Implementation of the x_sqlite_exec() function.  This function takes
   1.774 +** a single argument and attempts to execute that argument as SQL code.
   1.775 +** This is illegal and should set the SQLITE_MISUSE flag on the database.
   1.776 +**
   1.777 +** 2004-Jan-07:  We have changed this to make it legal to call sqlite3_exec()
   1.778 +** from within a function call.  
   1.779 +** 
   1.780 +** This routine simulates the effect of having two threads attempt to
   1.781 +** use the same database at the same time.
   1.782 +*/
   1.783 +static void sqlite3ExecFunc(
   1.784 +  sqlite3_context *context, 
   1.785 +  int argc,  
   1.786 +  sqlite3_value **argv
   1.787 +){
   1.788 +  struct dstr x;
   1.789 +  memset(&x, 0, sizeof(x));
   1.790 +  (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context),
   1.791 +      (char*)sqlite3_value_text(argv[0]),
   1.792 +      execFuncCallback, &x, 0);
   1.793 +  sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT);
   1.794 +  sqlite3_free(x.z);
   1.795 +}
   1.796 +
   1.797 +/*
   1.798 +** Implementation of tkt2213func(), a scalar function that takes exactly
   1.799 +** one argument. It has two interesting features:
   1.800 +**
   1.801 +** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*.
   1.802 +**   If the three pointers returned are not the same an SQL error is raised.
   1.803 +**
   1.804 +** * Otherwise it returns a copy of the text representation of its 
   1.805 +**   argument in such a way as the VDBE representation is a Mem* cell 
   1.806 +**   with the MEM_Term flag clear. 
   1.807 +**
   1.808 +** Ticket #2213 can therefore be tested by evaluating the following
   1.809 +** SQL expression:
   1.810 +**
   1.811 +**   tkt2213func(tkt2213func('a string'));
   1.812 +*/
   1.813 +static void tkt2213Function(
   1.814 +  sqlite3_context *context, 
   1.815 +  int argc,  
   1.816 +  sqlite3_value **argv
   1.817 +){
   1.818 +  int nText;
   1.819 +  unsigned char const *zText1;
   1.820 +  unsigned char const *zText2;
   1.821 +  unsigned char const *zText3;
   1.822 +
   1.823 +  nText = sqlite3_value_bytes(argv[0]);
   1.824 +  zText1 = sqlite3_value_text(argv[0]);
   1.825 +  zText2 = sqlite3_value_text(argv[0]);
   1.826 +  zText3 = sqlite3_value_text(argv[0]);
   1.827 +
   1.828 +  if( zText1!=zText2 || zText2!=zText3 ){
   1.829 +    sqlite3_result_error(context, "tkt2213 is not fixed", -1);
   1.830 +  }else{
   1.831 +    char *zCopy = (char *)sqlite3_malloc(nText);
   1.832 +    memcpy(zCopy, zText1, nText);
   1.833 +    sqlite3_result_text(context, zCopy, nText, sqlite3_free);
   1.834 +  }
   1.835 +}
   1.836 +
   1.837 +/*
   1.838 +** The following SQL function takes 4 arguments.  The 2nd and
   1.839 +** 4th argument must be one of these strings:  'text', 'text16',
   1.840 +** or 'blob' corresponding to API functions
   1.841 +**
   1.842 +**      sqlite3_value_text()
   1.843 +**      sqlite3_value_text16()
   1.844 +**      sqlite3_value_blob()
   1.845 +**
   1.846 +** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
   1.847 +** corresponding to APIs:
   1.848 +**
   1.849 +**      sqlite3_value_bytes()
   1.850 +**      sqlite3_value_bytes16()
   1.851 +**      noop
   1.852 +**
   1.853 +** The APIs designated by the 2nd through 4th arguments are applied
   1.854 +** to the first argument in order.  If the pointers returned by the
   1.855 +** second and fourth are different, this routine returns 1.  Otherwise,
   1.856 +** this routine returns 0.
   1.857 +**
   1.858 +** This function is used to test to see when returned pointers from
   1.859 +** the _text(), _text16() and _blob() APIs become invalidated.
   1.860 +*/
   1.861 +static void ptrChngFunction(
   1.862 +  sqlite3_context *context, 
   1.863 +  int argc,  
   1.864 +  sqlite3_value **argv
   1.865 +){
   1.866 +  const void *p1, *p2;
   1.867 +  const char *zCmd;
   1.868 +  if( argc!=4 ) return;
   1.869 +  zCmd = (const char*)sqlite3_value_text(argv[1]);
   1.870 +  if( zCmd==0 ) return;
   1.871 +  if( strcmp(zCmd,"text")==0 ){
   1.872 +    p1 = (const void*)sqlite3_value_text(argv[0]);
   1.873 +#ifndef SQLITE_OMIT_UTF16
   1.874 +  }else if( strcmp(zCmd, "text16")==0 ){
   1.875 +    p1 = (const void*)sqlite3_value_text16(argv[0]);
   1.876 +#endif
   1.877 +  }else if( strcmp(zCmd, "blob")==0 ){
   1.878 +    p1 = (const void*)sqlite3_value_blob(argv[0]);
   1.879 +  }else{
   1.880 +    return;
   1.881 +  }
   1.882 +  zCmd = (const char*)sqlite3_value_text(argv[2]);
   1.883 +  if( zCmd==0 ) return;
   1.884 +  if( strcmp(zCmd,"bytes")==0 ){
   1.885 +    sqlite3_value_bytes(argv[0]);
   1.886 +#ifndef SQLITE_OMIT_UTF16
   1.887 +  }else if( strcmp(zCmd, "bytes16")==0 ){
   1.888 +    sqlite3_value_bytes16(argv[0]);
   1.889 +#endif
   1.890 +  }else if( strcmp(zCmd, "noop")==0 ){
   1.891 +    /* do nothing */
   1.892 +  }else{
   1.893 +    return;
   1.894 +  }
   1.895 +  zCmd = (const char*)sqlite3_value_text(argv[3]);
   1.896 +  if( zCmd==0 ) return;
   1.897 +  if( strcmp(zCmd,"text")==0 ){
   1.898 +    p2 = (const void*)sqlite3_value_text(argv[0]);
   1.899 +#ifndef SQLITE_OMIT_UTF16
   1.900 +  }else if( strcmp(zCmd, "text16")==0 ){
   1.901 +    p2 = (const void*)sqlite3_value_text16(argv[0]);
   1.902 +#endif
   1.903 +  }else if( strcmp(zCmd, "blob")==0 ){
   1.904 +    p2 = (const void*)sqlite3_value_blob(argv[0]);
   1.905 +  }else{
   1.906 +    return;
   1.907 +  }
   1.908 +  sqlite3_result_int(context, p1!=p2);
   1.909 +}
   1.910 +
   1.911 +
   1.912 +/*
   1.913 +** Usage:  sqlite_test_create_function DB
   1.914 +**
   1.915 +** Call the sqlite3_create_function API on the given database in order
   1.916 +** to create a function named "x_coalesce".  This function does the same thing
   1.917 +** as the "coalesce" function.  This function also registers an SQL function
   1.918 +** named "x_sqlite_exec" that invokes sqlite3_exec().  Invoking sqlite3_exec()
   1.919 +** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
   1.920 +** The effect is similar to trying to use the same database connection from
   1.921 +** two threads at the same time.
   1.922 +**
   1.923 +** The original motivation for this routine was to be able to call the
   1.924 +** sqlite3_create_function function while a query is in progress in order
   1.925 +** to test the SQLITE_MISUSE detection logic.
   1.926 +*/
   1.927 +static int test_create_function(
   1.928 +  void *NotUsed,
   1.929 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
   1.930 +  int argc,              /* Number of arguments */
   1.931 +  char **argv            /* Text of each argument */
   1.932 +){
   1.933 +  int rc;
   1.934 +  sqlite3 *db;
   1.935 +
   1.936 +  if( argc!=2 ){
   1.937 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
   1.938 +       " DB\"", 0);
   1.939 +    return TCL_ERROR;
   1.940 +  }
   1.941 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
   1.942 +  rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0, 
   1.943 +        t1_ifnullFunc, 0, 0);
   1.944 +  if( rc==SQLITE_OK ){
   1.945 +    rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0, 
   1.946 +          hex8Func, 0, 0);
   1.947 +  }
   1.948 +#ifndef SQLITE_OMIT_UTF16
   1.949 +  if( rc==SQLITE_OK ){
   1.950 +    rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0, 
   1.951 +          hex16Func, 0, 0);
   1.952 +  }
   1.953 +#endif
   1.954 +  if( rc==SQLITE_OK ){
   1.955 +    rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0, 
   1.956 +          tkt2213Function, 0, 0);
   1.957 +  }
   1.958 +  if( rc==SQLITE_OK ){
   1.959 +    rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0, 
   1.960 +          ptrChngFunction, 0, 0);
   1.961 +  }
   1.962 +
   1.963 +#ifndef SQLITE_OMIT_UTF16
   1.964 +  /* Use the sqlite3_create_function16() API here. Mainly for fun, but also 
   1.965 +  ** because it is not tested anywhere else. */
   1.966 +  if( rc==SQLITE_OK ){
   1.967 +    const void *zUtf16;
   1.968 +    sqlite3_value *pVal;
   1.969 +    sqlite3_mutex_enter(db->mutex);
   1.970 +    pVal = sqlite3ValueNew(db);
   1.971 +    sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);
   1.972 +    zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
   1.973 +    if( db->mallocFailed ){
   1.974 +      rc = SQLITE_NOMEM;
   1.975 +    }else{
   1.976 +      rc = sqlite3_create_function16(db, zUtf16, 
   1.977 +                1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
   1.978 +    }
   1.979 +    sqlite3ValueFree(pVal);
   1.980 +    sqlite3_mutex_leave(db->mutex);
   1.981 +  }
   1.982 +#endif
   1.983 +
   1.984 +  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
   1.985 +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
   1.986 +  return TCL_OK;
   1.987 +}
   1.988 +
   1.989 +/*
   1.990 +** Routines to implement the x_count() aggregate function.
   1.991 +**
   1.992 +** x_count() counts the number of non-null arguments.  But there are
   1.993 +** some twists for testing purposes.
   1.994 +**
   1.995 +** If the argument to x_count() is 40 then a UTF-8 error is reported
   1.996 +** on the step function.  If x_count(41) is seen, then a UTF-16 error
   1.997 +** is reported on the step function.  If the total count is 42, then
   1.998 +** a UTF-8 error is reported on the finalize function.
   1.999 +*/
  1.1000 +typedef struct t1CountCtx t1CountCtx;
  1.1001 +struct t1CountCtx {
  1.1002 +  int n;
  1.1003 +};
  1.1004 +static void t1CountStep(
  1.1005 +  sqlite3_context *context,
  1.1006 +  int argc,
  1.1007 +  sqlite3_value **argv
  1.1008 +){
  1.1009 +  t1CountCtx *p;
  1.1010 +  p = sqlite3_aggregate_context(context, sizeof(*p));
  1.1011 +  if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
  1.1012 +    p->n++;
  1.1013 +  }
  1.1014 +  if( argc>0 ){
  1.1015 +    int v = sqlite3_value_int(argv[0]);
  1.1016 +    if( v==40 ){
  1.1017 +      sqlite3_result_error(context, "value of 40 handed to x_count", -1);
  1.1018 +#ifndef SQLITE_OMIT_UTF16
  1.1019 +    }else if( v==41 ){
  1.1020 +      const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
  1.1021 +      sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
  1.1022 +#endif
  1.1023 +    }
  1.1024 +  }
  1.1025 +}   
  1.1026 +static void t1CountFinalize(sqlite3_context *context){
  1.1027 +  t1CountCtx *p;
  1.1028 +  p = sqlite3_aggregate_context(context, sizeof(*p));
  1.1029 +  if( p ){
  1.1030 +    if( p->n==42 ){
  1.1031 +      sqlite3_result_error(context, "x_count totals to 42", -1);
  1.1032 +    }else{
  1.1033 +      sqlite3_result_int(context, p ? p->n : 0);
  1.1034 +    }
  1.1035 +  }
  1.1036 +}
  1.1037 +
  1.1038 +static void legacyCountStep(
  1.1039 +  sqlite3_context *context,
  1.1040 +  int argc,
  1.1041 +  sqlite3_value **argv
  1.1042 +){
  1.1043 +  /* no-op */
  1.1044 +}
  1.1045 +static void legacyCountFinalize(sqlite3_context *context){
  1.1046 +  sqlite3_result_int(context, sqlite3_aggregate_count(context));
  1.1047 +}
  1.1048 +
  1.1049 +/*
  1.1050 +** Usage:  sqlite3_create_aggregate DB
  1.1051 +**
  1.1052 +** Call the sqlite3_create_function API on the given database in order
  1.1053 +** to create a function named "x_count".  This function is similar
  1.1054 +** to the built-in count() function, with a few special quirks
  1.1055 +** for testing the sqlite3_result_error() APIs.
  1.1056 +**
  1.1057 +** The original motivation for this routine was to be able to call the
  1.1058 +** sqlite3_create_aggregate function while a query is in progress in order
  1.1059 +** to test the SQLITE_MISUSE detection logic.  See misuse.test.
  1.1060 +**
  1.1061 +** This routine was later extended to test the use of sqlite3_result_error()
  1.1062 +** within aggregate functions.
  1.1063 +**
  1.1064 +** Later: It is now also extended to register the aggregate function
  1.1065 +** "legacy_count()" with the supplied database handle. This is used
  1.1066 +** to test the deprecated sqlite3_aggregate_count() API.
  1.1067 +*/
  1.1068 +static int test_create_aggregate(
  1.1069 +  void *NotUsed,
  1.1070 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1071 +  int argc,              /* Number of arguments */
  1.1072 +  char **argv            /* Text of each argument */
  1.1073 +){
  1.1074 +  sqlite3 *db;
  1.1075 +  int rc;
  1.1076 +  if( argc!=2 ){
  1.1077 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1.1078 +       " FILENAME\"", 0);
  1.1079 +    return TCL_ERROR;
  1.1080 +  }
  1.1081 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  1.1082 +  rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
  1.1083 +      t1CountStep,t1CountFinalize);
  1.1084 +  if( rc==SQLITE_OK ){
  1.1085 +    rc = sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
  1.1086 +        t1CountStep,t1CountFinalize);
  1.1087 +  }
  1.1088 +  if( rc==SQLITE_OK ){
  1.1089 +    rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0,
  1.1090 +        legacyCountStep, legacyCountFinalize
  1.1091 +    );
  1.1092 +  }
  1.1093 +  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  1.1094 +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
  1.1095 +  return TCL_OK;
  1.1096 +}
  1.1097 +
  1.1098 +
  1.1099 +/*
  1.1100 +** Usage:  printf TEXT
  1.1101 +**
  1.1102 +** Send output to printf.  Use this rather than puts to merge the output
  1.1103 +** in the correct sequence with debugging printfs inserted into C code.
  1.1104 +** Puts uses a separate buffer and debugging statements will be out of
  1.1105 +** sequence if it is used.
  1.1106 +*/
  1.1107 +static int test_printf(
  1.1108 +  void *NotUsed,
  1.1109 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1110 +  int argc,              /* Number of arguments */
  1.1111 +  char **argv            /* Text of each argument */
  1.1112 +){
  1.1113 +  if( argc!=2 ){
  1.1114 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1.1115 +       " TEXT\"", 0);
  1.1116 +    return TCL_ERROR;
  1.1117 +  }
  1.1118 +  printf("%s\n", argv[1]);
  1.1119 +  return TCL_OK;
  1.1120 +}
  1.1121 +
  1.1122 +
  1.1123 +
  1.1124 +/*
  1.1125 +** Usage:  sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
  1.1126 +**
  1.1127 +** Call mprintf with three integer arguments
  1.1128 +*/
  1.1129 +static int sqlite3_mprintf_int(
  1.1130 +  void *NotUsed,
  1.1131 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1132 +  int argc,              /* Number of arguments */
  1.1133 +  char **argv            /* Text of each argument */
  1.1134 +){
  1.1135 +  int a[3], i;
  1.1136 +  char *z;
  1.1137 +  if( argc!=5 ){
  1.1138 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1.1139 +       " FORMAT INT INT INT\"", 0);
  1.1140 +    return TCL_ERROR;
  1.1141 +  }
  1.1142 +  for(i=2; i<5; i++){
  1.1143 +    if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
  1.1144 +  }
  1.1145 +  z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
  1.1146 +  Tcl_AppendResult(interp, z, 0);
  1.1147 +  sqlite3_free(z);
  1.1148 +  return TCL_OK;
  1.1149 +}
  1.1150 +
  1.1151 +/*
  1.1152 +** If zNum represents an integer that will fit in 64-bits, then set
  1.1153 +** *pValue to that integer and return true.  Otherwise return false.
  1.1154 +*/
  1.1155 +static int sqlite3GetInt64(const char *zNum, i64 *pValue){
  1.1156 +  if( sqlite3FitsIn64Bits(zNum, 0) ){
  1.1157 +    sqlite3Atoi64(zNum, pValue);
  1.1158 +    return 1;
  1.1159 +  }
  1.1160 +  return 0;
  1.1161 +}
  1.1162 +
  1.1163 +/*
  1.1164 +** Usage:  sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
  1.1165 +**
  1.1166 +** Call mprintf with three 64-bit integer arguments
  1.1167 +*/
  1.1168 +static int sqlite3_mprintf_int64(
  1.1169 +  void *NotUsed,
  1.1170 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1171 +  int argc,              /* Number of arguments */
  1.1172 +  char **argv            /* Text of each argument */
  1.1173 +){
  1.1174 +  int i;
  1.1175 +  sqlite_int64 a[3];
  1.1176 +  char *z;
  1.1177 +  if( argc!=5 ){
  1.1178 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1.1179 +       " FORMAT INT INT INT\"", 0);
  1.1180 +    return TCL_ERROR;
  1.1181 +  }
  1.1182 +  for(i=2; i<5; i++){
  1.1183 +    if( !sqlite3GetInt64(argv[i], &a[i-2]) ){
  1.1184 +      Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0);
  1.1185 +      return TCL_ERROR;
  1.1186 +    }
  1.1187 +  }
  1.1188 +  z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
  1.1189 +  Tcl_AppendResult(interp, z, 0);
  1.1190 +  sqlite3_free(z);
  1.1191 +  return TCL_OK;
  1.1192 +}
  1.1193 +
  1.1194 +/*
  1.1195 +** Usage:  sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
  1.1196 +**
  1.1197 +** Call mprintf with two integer arguments and one string argument
  1.1198 +*/
  1.1199 +static int sqlite3_mprintf_str(
  1.1200 +  void *NotUsed,
  1.1201 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1202 +  int argc,              /* Number of arguments */
  1.1203 +  char **argv            /* Text of each argument */
  1.1204 +){
  1.1205 +  int a[3], i;
  1.1206 +  char *z;
  1.1207 +  if( argc<4 || argc>5 ){
  1.1208 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1.1209 +       " FORMAT INT INT ?STRING?\"", 0);
  1.1210 +    return TCL_ERROR;
  1.1211 +  }
  1.1212 +  for(i=2; i<4; i++){
  1.1213 +    if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
  1.1214 +  }
  1.1215 +  z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL);
  1.1216 +  Tcl_AppendResult(interp, z, 0);
  1.1217 +  sqlite3_free(z);
  1.1218 +  return TCL_OK;
  1.1219 +}
  1.1220 +
  1.1221 +/*
  1.1222 +** Usage:  sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING
  1.1223 +**
  1.1224 +** Call mprintf with two integer arguments and one string argument
  1.1225 +*/
  1.1226 +static int sqlite3_snprintf_str(
  1.1227 +  void *NotUsed,
  1.1228 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1229 +  int argc,              /* Number of arguments */
  1.1230 +  char **argv            /* Text of each argument */
  1.1231 +){
  1.1232 +  int a[3], i;
  1.1233 +  int n;
  1.1234 +  char *z;
  1.1235 +  if( argc<5 || argc>6 ){
  1.1236 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1.1237 +       " INT FORMAT INT INT ?STRING?\"", 0);
  1.1238 +    return TCL_ERROR;
  1.1239 +  }
  1.1240 +  if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
  1.1241 +  if( n<0 ){
  1.1242 +    Tcl_AppendResult(interp, "N must be non-negative", 0);
  1.1243 +    return TCL_ERROR;
  1.1244 +  }
  1.1245 +  for(i=3; i<5; i++){
  1.1246 +    if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR;
  1.1247 +  }
  1.1248 +  z = sqlite3_malloc( n+1 );
  1.1249 +  sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL);
  1.1250 +  Tcl_AppendResult(interp, z, 0);
  1.1251 +  sqlite3_free(z);
  1.1252 +  return TCL_OK;
  1.1253 +}
  1.1254 +
  1.1255 +/*
  1.1256 +** Usage:  sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE
  1.1257 +**
  1.1258 +** Call mprintf with two integer arguments and one double argument
  1.1259 +*/
  1.1260 +static int sqlite3_mprintf_double(
  1.1261 +  void *NotUsed,
  1.1262 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1263 +  int argc,              /* Number of arguments */
  1.1264 +  char **argv            /* Text of each argument */
  1.1265 +){
  1.1266 +  int a[3], i;
  1.1267 +  double r;
  1.1268 +  char *z;
  1.1269 +  if( argc!=5 ){
  1.1270 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1.1271 +       " FORMAT INT INT DOUBLE\"", 0);
  1.1272 +    return TCL_ERROR;
  1.1273 +  }
  1.1274 +  for(i=2; i<4; i++){
  1.1275 +    if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
  1.1276 +  }
  1.1277 +  if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;
  1.1278 +  z = sqlite3_mprintf(argv[1], a[0], a[1], r);
  1.1279 +  Tcl_AppendResult(interp, z, 0);
  1.1280 +  sqlite3_free(z);
  1.1281 +  return TCL_OK;
  1.1282 +}
  1.1283 +
  1.1284 +/*
  1.1285 +** Usage:  sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE
  1.1286 +**
  1.1287 +** Call mprintf with a single double argument which is the product of the
  1.1288 +** two arguments given above.  This is used to generate overflow and underflow
  1.1289 +** doubles to test that they are converted properly.
  1.1290 +*/
  1.1291 +static int sqlite3_mprintf_scaled(
  1.1292 +  void *NotUsed,
  1.1293 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1294 +  int argc,              /* Number of arguments */
  1.1295 +  char **argv            /* Text of each argument */
  1.1296 +){
  1.1297 +  int i;
  1.1298 +  double r[2];
  1.1299 +  char *z;
  1.1300 +  if( argc!=4 ){
  1.1301 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1.1302 +       " FORMAT DOUBLE DOUBLE\"", 0);
  1.1303 +    return TCL_ERROR;
  1.1304 +  }
  1.1305 +  for(i=2; i<4; i++){
  1.1306 +    if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR;
  1.1307 +  }
  1.1308 +  z = sqlite3_mprintf(argv[1], r[0]*r[1]);
  1.1309 +  Tcl_AppendResult(interp, z, 0);
  1.1310 +  sqlite3_free(z);
  1.1311 +  return TCL_OK;
  1.1312 +}
  1.1313 +
  1.1314 +/*
  1.1315 +** Usage:  sqlite3_mprintf_stronly FORMAT STRING
  1.1316 +**
  1.1317 +** Call mprintf with a single double argument which is the product of the
  1.1318 +** two arguments given above.  This is used to generate overflow and underflow
  1.1319 +** doubles to test that they are converted properly.
  1.1320 +*/
  1.1321 +static int sqlite3_mprintf_stronly(
  1.1322 +  void *NotUsed,
  1.1323 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1324 +  int argc,              /* Number of arguments */
  1.1325 +  char **argv            /* Text of each argument */
  1.1326 +){
  1.1327 +  char *z;
  1.1328 +  if( argc!=3 ){
  1.1329 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1.1330 +       " FORMAT STRING\"", 0);
  1.1331 +    return TCL_ERROR;
  1.1332 +  }
  1.1333 +  z = sqlite3_mprintf(argv[1], argv[2]);
  1.1334 +  Tcl_AppendResult(interp, z, 0);
  1.1335 +  sqlite3_free(z);
  1.1336 +  return TCL_OK;
  1.1337 +}
  1.1338 +
  1.1339 +/*
  1.1340 +** Usage:  sqlite3_mprintf_hexdouble FORMAT HEX
  1.1341 +**
  1.1342 +** Call mprintf with a single double argument which is derived from the
  1.1343 +** hexadecimal encoding of an IEEE double.
  1.1344 +*/
  1.1345 +static int sqlite3_mprintf_hexdouble(
  1.1346 +  void *NotUsed,
  1.1347 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1348 +  int argc,              /* Number of arguments */
  1.1349 +  char **argv            /* Text of each argument */
  1.1350 +){
  1.1351 +  char *z;
  1.1352 +  double r;
  1.1353 +  unsigned int x1, x2;
  1.1354 +  sqlite_uint64 d;
  1.1355 +  if( argc!=3 ){
  1.1356 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1.1357 +       " FORMAT STRING\"", 0);
  1.1358 +    return TCL_ERROR;
  1.1359 +  }
  1.1360 +  if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){
  1.1361 +    Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0);
  1.1362 +    return TCL_ERROR;
  1.1363 +  }
  1.1364 +  d = x2;
  1.1365 +  d = (d<<32) + x1;
  1.1366 +  memcpy(&r, &d, sizeof(r));
  1.1367 +  z = sqlite3_mprintf(argv[1], r);
  1.1368 +  Tcl_AppendResult(interp, z, 0);
  1.1369 +  sqlite3_free(z);
  1.1370 +  return TCL_OK;
  1.1371 +}
  1.1372 +
  1.1373 +/*
  1.1374 +** Usage: sqlite3_enable_shared_cache ?BOOLEAN?
  1.1375 +**
  1.1376 +*/
  1.1377 +#if !defined(SQLITE_OMIT_SHARED_CACHE)
  1.1378 +static int test_enable_shared(
  1.1379 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.1380 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1381 +  int objc,              /* Number of arguments */
  1.1382 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.1383 +){
  1.1384 +  int rc;
  1.1385 +  int enable;
  1.1386 +  int ret = 0;
  1.1387 +
  1.1388 +  if( objc!=2 && objc!=1 ){
  1.1389 +    Tcl_WrongNumArgs(interp, 1, objv, "?BOOLEAN?");
  1.1390 +    return TCL_ERROR;
  1.1391 +  }
  1.1392 +  ret = sqlite3GlobalConfig.sharedCacheEnabled;
  1.1393 +
  1.1394 +  if( objc==2 ){
  1.1395 +    if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){
  1.1396 +      return TCL_ERROR;
  1.1397 +    }
  1.1398 +    rc = sqlite3_enable_shared_cache(enable);
  1.1399 +    if( rc!=SQLITE_OK ){
  1.1400 +      Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
  1.1401 +      return TCL_ERROR;
  1.1402 +    }
  1.1403 +  }
  1.1404 +  Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret));
  1.1405 +  return TCL_OK;
  1.1406 +}
  1.1407 +#endif
  1.1408 +
  1.1409 +
  1.1410 +
  1.1411 +/*
  1.1412 +** Usage: sqlite3_extended_result_codes   DB    BOOLEAN
  1.1413 +**
  1.1414 +*/
  1.1415 +static int test_extended_result_codes(
  1.1416 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.1417 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1418 +  int objc,              /* Number of arguments */
  1.1419 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.1420 +){
  1.1421 +  int enable;
  1.1422 +  sqlite3 *db;
  1.1423 +
  1.1424 +  if( objc!=3 ){
  1.1425 +    Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
  1.1426 +    return TCL_ERROR;
  1.1427 +  }
  1.1428 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.1429 +  if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR;
  1.1430 +  sqlite3_extended_result_codes(db, enable);
  1.1431 +  return TCL_OK;
  1.1432 +}
  1.1433 +
  1.1434 +/*
  1.1435 +** Usage: sqlite3_libversion_number
  1.1436 +**
  1.1437 +*/
  1.1438 +static int test_libversion_number(
  1.1439 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.1440 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1441 +  int objc,              /* Number of arguments */
  1.1442 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.1443 +){
  1.1444 +  Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number()));
  1.1445 +  return TCL_OK;
  1.1446 +}
  1.1447 +
  1.1448 +/*
  1.1449 +** Usage: sqlite3_table_column_metadata DB dbname tblname colname
  1.1450 +**
  1.1451 +*/
  1.1452 +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1.1453 +static int test_table_column_metadata(
  1.1454 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.1455 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1456 +  int objc,              /* Number of arguments */
  1.1457 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.1458 +){
  1.1459 +  sqlite3 *db;
  1.1460 +  const char *zDb;
  1.1461 +  const char *zTbl;
  1.1462 +  const char *zCol;
  1.1463 +  int rc;
  1.1464 +  Tcl_Obj *pRet;
  1.1465 +
  1.1466 +  const char *zDatatype;
  1.1467 +  const char *zCollseq;
  1.1468 +  int notnull;
  1.1469 +  int primarykey;
  1.1470 +  int autoincrement;
  1.1471 +
  1.1472 +  if( objc!=5 ){
  1.1473 +    Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname");
  1.1474 +    return TCL_ERROR;
  1.1475 +  }
  1.1476 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.1477 +  zDb = Tcl_GetString(objv[2]);
  1.1478 +  zTbl = Tcl_GetString(objv[3]);
  1.1479 +  zCol = Tcl_GetString(objv[4]);
  1.1480 +
  1.1481 +  if( strlen(zDb)==0 ) zDb = 0;
  1.1482 +
  1.1483 +  rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol, 
  1.1484 +      &zDatatype, &zCollseq, &notnull, &primarykey, &autoincrement);
  1.1485 +
  1.1486 +  if( rc!=SQLITE_OK ){
  1.1487 +    Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
  1.1488 +    return TCL_ERROR;
  1.1489 +  }
  1.1490 +
  1.1491 +  pRet = Tcl_NewObj();
  1.1492 +  Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1));
  1.1493 +  Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1));
  1.1494 +  Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull));
  1.1495 +  Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey));
  1.1496 +  Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement));
  1.1497 +  Tcl_SetObjResult(interp, pRet);
  1.1498 +
  1.1499 +  return TCL_OK;
  1.1500 +}
  1.1501 +#endif
  1.1502 +
  1.1503 +#ifndef SQLITE_OMIT_INCRBLOB
  1.1504 +
  1.1505 +/*
  1.1506 +** sqlite3_blob_read  CHANNEL OFFSET N
  1.1507 +**
  1.1508 +**   This command is used to test the sqlite3_blob_read() in ways that
  1.1509 +**   the Tcl channel interface does not. The first argument should
  1.1510 +**   be the name of a valid channel created by the [incrblob] method
  1.1511 +**   of a database handle. This function calls sqlite3_blob_read()
  1.1512 +**   to read N bytes from offset OFFSET from the underlying SQLite
  1.1513 +**   blob handle.
  1.1514 +**
  1.1515 +**   On success, a byte-array object containing the read data is 
  1.1516 +**   returned. On failure, the interpreter result is set to the
  1.1517 +**   text representation of the returned error code (i.e. "SQLITE_NOMEM")
  1.1518 +**   and a Tcl exception is thrown.
  1.1519 +*/
  1.1520 +static int test_blob_read(
  1.1521 +  ClientData clientData, /* Not used */
  1.1522 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1523 +  int objc,              /* Number of arguments */
  1.1524 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.1525 +){
  1.1526 +  Tcl_Channel channel;
  1.1527 +  ClientData instanceData;
  1.1528 +  sqlite3_blob *pBlob;
  1.1529 +  int notUsed;
  1.1530 +  int nByte;
  1.1531 +  int iOffset;
  1.1532 +  unsigned char *zBuf;
  1.1533 +  int rc;
  1.1534 +  
  1.1535 +  if( objc!=4 ){
  1.1536 +    Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET N");
  1.1537 +    return TCL_ERROR;
  1.1538 +  }
  1.1539 +
  1.1540 +  channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), &notUsed);
  1.1541 +  if( !channel
  1.1542 +   || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset)
  1.1543 +   || TCL_OK!=Tcl_GetIntFromObj(interp, objv[3], &nByte)
  1.1544 +   || nByte<0 || iOffset<0
  1.1545 +  ){ 
  1.1546 +    return TCL_ERROR;
  1.1547 +  }
  1.1548 +
  1.1549 +  instanceData = Tcl_GetChannelInstanceData(channel);
  1.1550 +  pBlob = *((sqlite3_blob **)instanceData);
  1.1551 +
  1.1552 +  zBuf = (unsigned char *)Tcl_Alloc(nByte);
  1.1553 +  rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset);
  1.1554 +  if( rc==SQLITE_OK ){
  1.1555 +    Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zBuf, nByte));
  1.1556 +  }else{
  1.1557 +    Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
  1.1558 +  }
  1.1559 +  Tcl_Free((char *)zBuf);
  1.1560 +
  1.1561 +  return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
  1.1562 +}
  1.1563 +
  1.1564 +/*
  1.1565 +** sqlite3_blob_write CHANNEL OFFSET DATA ?NDATA?
  1.1566 +**
  1.1567 +**   This command is used to test the sqlite3_blob_write() in ways that
  1.1568 +**   the Tcl channel interface does not. The first argument should
  1.1569 +**   be the name of a valid channel created by the [incrblob] method
  1.1570 +**   of a database handle. This function calls sqlite3_blob_write()
  1.1571 +**   to write the DATA byte-array to the underlying SQLite blob handle.
  1.1572 +**   at offset OFFSET.
  1.1573 +**
  1.1574 +**   On success, an empty string is returned. On failure, the interpreter
  1.1575 +**   result is set to the text representation of the returned error code 
  1.1576 +**   (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown.
  1.1577 +*/
  1.1578 +static int test_blob_write(
  1.1579 +  ClientData clientData, /* Not used */
  1.1580 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1581 +  int objc,              /* Number of arguments */
  1.1582 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.1583 +){
  1.1584 +  Tcl_Channel channel;
  1.1585 +  ClientData instanceData;
  1.1586 +  sqlite3_blob *pBlob;
  1.1587 +  int notUsed;
  1.1588 +  int iOffset;
  1.1589 +  int rc;
  1.1590 +
  1.1591 +  unsigned char *zBuf;
  1.1592 +  int nBuf;
  1.1593 +  
  1.1594 +  if( objc!=4 && objc!=5 ){
  1.1595 +    Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET DATA ?NDATA?");
  1.1596 +    return TCL_ERROR;
  1.1597 +  }
  1.1598 +
  1.1599 +  channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), &notUsed);
  1.1600 +  if( !channel || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset) ){ 
  1.1601 +    return TCL_ERROR;
  1.1602 +  }
  1.1603 +
  1.1604 +  instanceData = Tcl_GetChannelInstanceData(channel);
  1.1605 +  pBlob = *((sqlite3_blob **)instanceData);
  1.1606 +
  1.1607 +  zBuf = Tcl_GetByteArrayFromObj(objv[3], &nBuf);
  1.1608 +  if( objc==5 && Tcl_GetIntFromObj(interp, objv[4], &nBuf) ){
  1.1609 +    return TCL_ERROR;
  1.1610 +  }
  1.1611 +  rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset);
  1.1612 +  if( rc!=SQLITE_OK ){
  1.1613 +    Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
  1.1614 +  }
  1.1615 +
  1.1616 +  return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
  1.1617 +}
  1.1618 +#endif
  1.1619 +
  1.1620 +/*
  1.1621 +** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC
  1.1622 +**
  1.1623 +**   This Tcl proc is used for testing the experimental
  1.1624 +**   sqlite3_create_collation_v2() interface.
  1.1625 +*/
  1.1626 +struct TestCollationX {
  1.1627 +  Tcl_Interp *interp;
  1.1628 +  Tcl_Obj *pCmp;
  1.1629 +  Tcl_Obj *pDel;
  1.1630 +};
  1.1631 +typedef struct TestCollationX TestCollationX;
  1.1632 +static void testCreateCollationDel(void *pCtx){
  1.1633 +  TestCollationX *p = (TestCollationX *)pCtx;
  1.1634 +
  1.1635 +  int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
  1.1636 +  if( rc!=TCL_OK ){
  1.1637 +    Tcl_BackgroundError(p->interp);
  1.1638 +  }
  1.1639 +
  1.1640 +  Tcl_DecrRefCount(p->pCmp);
  1.1641 +  Tcl_DecrRefCount(p->pDel);
  1.1642 +  sqlite3_free((void *)p);
  1.1643 +}
  1.1644 +static int testCreateCollationCmp(
  1.1645 +  void *pCtx,
  1.1646 +  int nLeft,
  1.1647 +  const void *zLeft,
  1.1648 +  int nRight,
  1.1649 +  const void *zRight
  1.1650 +){
  1.1651 +  TestCollationX *p = (TestCollationX *)pCtx;
  1.1652 +  Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp);
  1.1653 +  int iRes = 0;
  1.1654 +
  1.1655 +  Tcl_IncrRefCount(pScript);
  1.1656 +  Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft));
  1.1657 +  Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight));
  1.1658 +
  1.1659 +  if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL)
  1.1660 +   || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes)
  1.1661 +  ){
  1.1662 +    Tcl_BackgroundError(p->interp);
  1.1663 +  }
  1.1664 +  Tcl_DecrRefCount(pScript);
  1.1665 +
  1.1666 +  return iRes;
  1.1667 +}
  1.1668 +static int test_create_collation_v2(
  1.1669 +  ClientData clientData, /* Not used */
  1.1670 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1671 +  int objc,              /* Number of arguments */
  1.1672 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.1673 +){
  1.1674 +  TestCollationX *p;
  1.1675 +  sqlite3 *db;
  1.1676 +  int rc;
  1.1677 +
  1.1678 +  if( objc!=5 ){
  1.1679 +    Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC");
  1.1680 +    return TCL_ERROR;
  1.1681 +  }
  1.1682 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.1683 +
  1.1684 +  p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX));
  1.1685 +  p->pCmp = objv[3];
  1.1686 +  p->pDel = objv[4];
  1.1687 +  p->interp = interp;
  1.1688 +  Tcl_IncrRefCount(p->pCmp);
  1.1689 +  Tcl_IncrRefCount(p->pDel);
  1.1690 +
  1.1691 +  rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), 16, 
  1.1692 +      (void *)p, testCreateCollationCmp, testCreateCollationDel
  1.1693 +  );
  1.1694 +  if( rc!=SQLITE_MISUSE ){
  1.1695 +    Tcl_AppendResult(interp, "sqlite3_create_collate_v2() failed to detect "
  1.1696 +      "an invalid encoding", (char*)0);
  1.1697 +    return TCL_ERROR;
  1.1698 +  }
  1.1699 +  rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8, 
  1.1700 +      (void *)p, testCreateCollationCmp, testCreateCollationDel
  1.1701 +  );
  1.1702 +  return TCL_OK;
  1.1703 +}
  1.1704 +
  1.1705 +/*
  1.1706 +** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
  1.1707 +*/
  1.1708 +static int test_load_extension(
  1.1709 +  ClientData clientData, /* Not used */
  1.1710 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1711 +  int objc,              /* Number of arguments */
  1.1712 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.1713 +){
  1.1714 +  Tcl_CmdInfo cmdInfo;
  1.1715 +  sqlite3 *db;
  1.1716 +  int rc;
  1.1717 +  char *zDb;
  1.1718 +  char *zFile;
  1.1719 +  char *zProc = 0;
  1.1720 +  char *zErr = 0;
  1.1721 +
  1.1722 +  if( objc!=4 && objc!=3 ){
  1.1723 +    Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?");
  1.1724 +    return TCL_ERROR;
  1.1725 +  }
  1.1726 +  zDb = Tcl_GetString(objv[1]);
  1.1727 +  zFile = Tcl_GetString(objv[2]);
  1.1728 +  if( objc==4 ){
  1.1729 +    zProc = Tcl_GetString(objv[3]);
  1.1730 +  }
  1.1731 +
  1.1732 +  /* Extract the C database handle from the Tcl command name */
  1.1733 +  if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
  1.1734 +    Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
  1.1735 +    return TCL_ERROR;
  1.1736 +  }
  1.1737 +  db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
  1.1738 +  assert(db);
  1.1739 +
  1.1740 +  /* Call the underlying C function. If an error occurs, set rc to 
  1.1741 +  ** TCL_ERROR and load any error string into the interpreter. If no 
  1.1742 +  ** error occurs, set rc to TCL_OK.
  1.1743 +  */
  1.1744 +#ifdef SQLITE_OMIT_LOAD_EXTENSION
  1.1745 +  rc = SQLITE_ERROR;
  1.1746 +  zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()");
  1.1747 +#else
  1.1748 +  rc = sqlite3_load_extension(db, zFile, zProc, &zErr);
  1.1749 +#endif
  1.1750 +  if( rc!=SQLITE_OK ){
  1.1751 +    Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE);
  1.1752 +    rc = TCL_ERROR;
  1.1753 +  }else{
  1.1754 +    rc = TCL_OK;
  1.1755 +  }
  1.1756 +  sqlite3_free(zErr);
  1.1757 +
  1.1758 +  return rc;
  1.1759 +}
  1.1760 +
  1.1761 +/*
  1.1762 +** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
  1.1763 +*/
  1.1764 +static int test_enable_load(
  1.1765 +  ClientData clientData, /* Not used */
  1.1766 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1767 +  int objc,              /* Number of arguments */
  1.1768 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.1769 +){
  1.1770 +  Tcl_CmdInfo cmdInfo;
  1.1771 +  sqlite3 *db;
  1.1772 +  char *zDb;
  1.1773 +  int onoff;
  1.1774 +
  1.1775 +  if( objc!=3 ){
  1.1776 +    Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
  1.1777 +    return TCL_ERROR;
  1.1778 +  }
  1.1779 +  zDb = Tcl_GetString(objv[1]);
  1.1780 +
  1.1781 +  /* Extract the C database handle from the Tcl command name */
  1.1782 +  if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
  1.1783 +    Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
  1.1784 +    return TCL_ERROR;
  1.1785 +  }
  1.1786 +  db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
  1.1787 +  assert(db);
  1.1788 +
  1.1789 +  /* Get the onoff parameter */
  1.1790 +  if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
  1.1791 +    return TCL_ERROR;
  1.1792 +  }
  1.1793 +
  1.1794 +#ifdef SQLITE_OMIT_LOAD_EXTENSION
  1.1795 +  Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
  1.1796 +  return TCL_ERROR;
  1.1797 +#else
  1.1798 +  sqlite3_enable_load_extension(db, onoff);
  1.1799 +  return TCL_OK;
  1.1800 +#endif
  1.1801 +}
  1.1802 +
  1.1803 +/*
  1.1804 +** Usage:  sqlite_abort
  1.1805 +**
  1.1806 +** Shutdown the process immediately.  This is not a clean shutdown.
  1.1807 +** This command is used to test the recoverability of a database in
  1.1808 +** the event of a program crash.
  1.1809 +*/
  1.1810 +static int sqlite_abort(
  1.1811 +  void *NotUsed,
  1.1812 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1813 +  int argc,              /* Number of arguments */
  1.1814 +  char **argv            /* Text of each argument */
  1.1815 +){
  1.1816 +  assert( interp==0 );   /* This will always fail */
  1.1817 +  return TCL_OK;
  1.1818 +}
  1.1819 +
  1.1820 +/*
  1.1821 +** The following routine is a user-defined SQL function whose purpose
  1.1822 +** is to test the sqlite_set_result() API.
  1.1823 +*/
  1.1824 +static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  1.1825 +  while( argc>=2 ){
  1.1826 +    const char *zArg0 = (char*)sqlite3_value_text(argv[0]);
  1.1827 +    if( zArg0 ){
  1.1828 +      if( 0==sqlite3StrICmp(zArg0, "int") ){
  1.1829 +        sqlite3_result_int(context, sqlite3_value_int(argv[1]));
  1.1830 +      }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
  1.1831 +        sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
  1.1832 +      }else if( sqlite3StrICmp(zArg0,"string")==0 ){
  1.1833 +        sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1,
  1.1834 +            SQLITE_TRANSIENT);
  1.1835 +      }else if( sqlite3StrICmp(zArg0,"double")==0 ){
  1.1836 +        sqlite3_result_double(context, sqlite3_value_double(argv[1]));
  1.1837 +      }else if( sqlite3StrICmp(zArg0,"null")==0 ){
  1.1838 +        sqlite3_result_null(context);
  1.1839 +      }else if( sqlite3StrICmp(zArg0,"value")==0 ){
  1.1840 +        sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
  1.1841 +      }else{
  1.1842 +        goto error_out;
  1.1843 +      }
  1.1844 +    }else{
  1.1845 +      goto error_out;
  1.1846 +    }
  1.1847 +    argc -= 2;
  1.1848 +    argv += 2;
  1.1849 +  }
  1.1850 +  return;
  1.1851 +
  1.1852 +error_out:
  1.1853 +  sqlite3_result_error(context,"first argument should be one of: "
  1.1854 +      "int int64 string double null value", -1);
  1.1855 +}
  1.1856 +
  1.1857 +/*
  1.1858 +** Usage:   sqlite_register_test_function  DB  NAME
  1.1859 +**
  1.1860 +** Register the test SQL function on the database DB under the name NAME.
  1.1861 +*/
  1.1862 +static int test_register_func(
  1.1863 +  void *NotUsed,
  1.1864 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.1865 +  int argc,              /* Number of arguments */
  1.1866 +  char **argv            /* Text of each argument */
  1.1867 +){
  1.1868 +  sqlite3 *db;
  1.1869 +  int rc;
  1.1870 +  if( argc!=3 ){
  1.1871 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
  1.1872 +       " DB FUNCTION-NAME", 0);
  1.1873 +    return TCL_ERROR;
  1.1874 +  }
  1.1875 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  1.1876 +  rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 
  1.1877 +      testFunc, 0, 0);
  1.1878 +  if( rc!=0 ){
  1.1879 +    Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
  1.1880 +    return TCL_ERROR;
  1.1881 +  }
  1.1882 +  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  1.1883 +  return TCL_OK;
  1.1884 +}
  1.1885 +
  1.1886 +/*
  1.1887 +** Usage:  sqlite3_finalize  STMT 
  1.1888 +**
  1.1889 +** Finalize a statement handle.
  1.1890 +*/
  1.1891 +static int test_finalize(
  1.1892 +  void * clientData,
  1.1893 +  Tcl_Interp *interp,
  1.1894 +  int objc,
  1.1895 +  Tcl_Obj *CONST objv[]
  1.1896 +){
  1.1897 +  sqlite3_stmt *pStmt;
  1.1898 +  int rc;
  1.1899 +  sqlite3 *db = 0;
  1.1900 +
  1.1901 +  if( objc!=2 ){
  1.1902 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.1903 +        Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
  1.1904 +    return TCL_ERROR;
  1.1905 +  }
  1.1906 +
  1.1907 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.1908 +
  1.1909 +  if( pStmt ){
  1.1910 +    db = StmtToDb(pStmt);
  1.1911 +  }
  1.1912 +  rc = sqlite3_finalize(pStmt);
  1.1913 +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  1.1914 +  if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  1.1915 +  return TCL_OK;
  1.1916 +}
  1.1917 +
  1.1918 +/*
  1.1919 +** Usage:  sqlite3_next_stmt  DB  STMT
  1.1920 +**
  1.1921 +** Return the next statment in sequence after STMT.
  1.1922 +*/
  1.1923 +static int test_next_stmt(
  1.1924 +  void * clientData,
  1.1925 +  Tcl_Interp *interp,
  1.1926 +  int objc,
  1.1927 +  Tcl_Obj *CONST objv[]
  1.1928 +){
  1.1929 +  sqlite3_stmt *pStmt;
  1.1930 +  sqlite3 *db = 0;
  1.1931 +  char zBuf[50];
  1.1932 +
  1.1933 +  if( objc!=3 ){
  1.1934 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.1935 +        Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0);
  1.1936 +    return TCL_ERROR;
  1.1937 +  }
  1.1938 +
  1.1939 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.1940 +  if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR;
  1.1941 +  pStmt = sqlite3_next_stmt(db, pStmt);
  1.1942 +  if( pStmt ){
  1.1943 +    if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
  1.1944 +    Tcl_AppendResult(interp, zBuf, 0);
  1.1945 +  }
  1.1946 +  return TCL_OK;
  1.1947 +}
  1.1948 +
  1.1949 +
  1.1950 +/*
  1.1951 +** Usage:  sqlite3_reset  STMT 
  1.1952 +**
  1.1953 +** Reset a statement handle.
  1.1954 +*/
  1.1955 +static int test_reset(
  1.1956 +  void * clientData,
  1.1957 +  Tcl_Interp *interp,
  1.1958 +  int objc,
  1.1959 +  Tcl_Obj *CONST objv[]
  1.1960 +){
  1.1961 +  sqlite3_stmt *pStmt;
  1.1962 +  int rc;
  1.1963 +
  1.1964 +  if( objc!=2 ){
  1.1965 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.1966 +        Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
  1.1967 +    return TCL_ERROR;
  1.1968 +  }
  1.1969 +
  1.1970 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.1971 +
  1.1972 +  rc = sqlite3_reset(pStmt);
  1.1973 +  if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
  1.1974 +    return TCL_ERROR;
  1.1975 +  }
  1.1976 +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  1.1977 +/*
  1.1978 +  if( rc ){
  1.1979 +    return TCL_ERROR;
  1.1980 +  }
  1.1981 +*/
  1.1982 +  return TCL_OK;
  1.1983 +}
  1.1984 +
  1.1985 +/*
  1.1986 +** Usage:  sqlite3_expired STMT 
  1.1987 +**
  1.1988 +** Return TRUE if a recompilation of the statement is recommended.
  1.1989 +*/
  1.1990 +static int test_expired(
  1.1991 +  void * clientData,
  1.1992 +  Tcl_Interp *interp,
  1.1993 +  int objc,
  1.1994 +  Tcl_Obj *CONST objv[]
  1.1995 +){
  1.1996 +  sqlite3_stmt *pStmt;
  1.1997 +  if( objc!=2 ){
  1.1998 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.1999 +        Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
  1.2000 +    return TCL_ERROR;
  1.2001 +  }
  1.2002 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.2003 +  Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
  1.2004 +  return TCL_OK;
  1.2005 +}
  1.2006 +
  1.2007 +/*
  1.2008 +** Usage:  sqlite3_transfer_bindings FROMSTMT TOSTMT
  1.2009 +**
  1.2010 +** Transfer all bindings from FROMSTMT over to TOSTMT
  1.2011 +*/
  1.2012 +static int test_transfer_bind(
  1.2013 +  void * clientData,
  1.2014 +  Tcl_Interp *interp,
  1.2015 +  int objc,
  1.2016 +  Tcl_Obj *CONST objv[]
  1.2017 +){
  1.2018 +  sqlite3_stmt *pStmt1, *pStmt2;
  1.2019 +  if( objc!=3 ){
  1.2020 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.2021 +        Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
  1.2022 +    return TCL_ERROR;
  1.2023 +  }
  1.2024 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
  1.2025 +  if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
  1.2026 +  Tcl_SetObjResult(interp, 
  1.2027 +     Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
  1.2028 +  return TCL_OK;
  1.2029 +}
  1.2030 +
  1.2031 +/*
  1.2032 +** Usage:  sqlite3_changes DB
  1.2033 +**
  1.2034 +** Return the number of changes made to the database by the last SQL
  1.2035 +** execution.
  1.2036 +*/
  1.2037 +static int test_changes(
  1.2038 +  void * clientData,
  1.2039 +  Tcl_Interp *interp,
  1.2040 +  int objc,
  1.2041 +  Tcl_Obj *CONST objv[]
  1.2042 +){
  1.2043 +  sqlite3 *db;
  1.2044 +  if( objc!=2 ){
  1.2045 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.2046 +       Tcl_GetString(objv[0]), " DB", 0);
  1.2047 +    return TCL_ERROR;
  1.2048 +  }
  1.2049 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.2050 +  Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
  1.2051 +  return TCL_OK;
  1.2052 +}
  1.2053 +
  1.2054 +/*
  1.2055 +** This is the "static_bind_value" that variables are bound to when
  1.2056 +** the FLAG option of sqlite3_bind is "static"
  1.2057 +*/
  1.2058 +static char *sqlite_static_bind_value = 0;
  1.2059 +static int sqlite_static_bind_nbyte = 0;
  1.2060 +
  1.2061 +/*
  1.2062 +** Usage:  sqlite3_bind  VM  IDX  VALUE  FLAGS
  1.2063 +**
  1.2064 +** Sets the value of the IDX-th occurance of "?" in the original SQL
  1.2065 +** string.  VALUE is the new value.  If FLAGS=="null" then VALUE is
  1.2066 +** ignored and the value is set to NULL.  If FLAGS=="static" then
  1.2067 +** the value is set to the value of a static variable named
  1.2068 +** "sqlite_static_bind_value".  If FLAGS=="normal" then a copy
  1.2069 +** of the VALUE is made.  If FLAGS=="blob10" then a VALUE is ignored
  1.2070 +** an a 10-byte blob "abc\000xyz\000pq" is inserted.
  1.2071 +*/
  1.2072 +static int test_bind(
  1.2073 +  void *NotUsed,
  1.2074 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.2075 +  int argc,              /* Number of arguments */
  1.2076 +  char **argv            /* Text of each argument */
  1.2077 +){
  1.2078 +  sqlite3_stmt *pStmt;
  1.2079 +  int rc;
  1.2080 +  int idx;
  1.2081 +  if( argc!=5 ){
  1.2082 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
  1.2083 +       " VM IDX VALUE (null|static|normal)\"", 0);
  1.2084 +    return TCL_ERROR;
  1.2085 +  }
  1.2086 +  if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
  1.2087 +  if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
  1.2088 +  if( strcmp(argv[4],"null")==0 ){
  1.2089 +    rc = sqlite3_bind_null(pStmt, idx);
  1.2090 +  }else if( strcmp(argv[4],"static")==0 ){
  1.2091 +    rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
  1.2092 +  }else if( strcmp(argv[4],"static-nbytes")==0 ){
  1.2093 +    rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value,
  1.2094 +                                       sqlite_static_bind_nbyte, 0);
  1.2095 +  }else if( strcmp(argv[4],"normal")==0 ){
  1.2096 +    rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
  1.2097 +  }else if( strcmp(argv[4],"blob10")==0 ){
  1.2098 +    rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC);
  1.2099 +  }else{
  1.2100 +    Tcl_AppendResult(interp, "4th argument should be "
  1.2101 +        "\"null\" or \"static\" or \"normal\"", 0);
  1.2102 +    return TCL_ERROR;
  1.2103 +  }
  1.2104 +  if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  1.2105 +  if( rc ){
  1.2106 +    char zBuf[50];
  1.2107 +    sprintf(zBuf, "(%d) ", rc);
  1.2108 +    Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
  1.2109 +    return TCL_ERROR;
  1.2110 +  }
  1.2111 +  return TCL_OK;
  1.2112 +}
  1.2113 +
  1.2114 +#ifndef SQLITE_OMIT_UTF16
  1.2115 +/*
  1.2116 +** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
  1.2117 +**
  1.2118 +** This function is used to test that SQLite selects the correct collation
  1.2119 +** sequence callback when multiple versions (for different text encodings)
  1.2120 +** are available.
  1.2121 +**
  1.2122 +** Calling this routine registers the collation sequence "test_collate"
  1.2123 +** with database handle <db>. The second argument must be a list of three
  1.2124 +** boolean values. If the first is true, then a version of test_collate is
  1.2125 +** registered for UTF-8, if the second is true, a version is registered for
  1.2126 +** UTF-16le, if the third is true, a UTF-16be version is available.
  1.2127 +** Previous versions of test_collate are deleted.
  1.2128 +**
  1.2129 +** The collation sequence test_collate is implemented by calling the
  1.2130 +** following TCL script:
  1.2131 +**
  1.2132 +**   "test_collate <enc> <lhs> <rhs>"
  1.2133 +**
  1.2134 +** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
  1.2135 +** The <enc> parameter is the encoding of the collation function that
  1.2136 +** SQLite selected to call. The TCL test script implements the
  1.2137 +** "test_collate" proc.
  1.2138 +**
  1.2139 +** Note that this will only work with one intepreter at a time, as the
  1.2140 +** interp pointer to use when evaluating the TCL script is stored in
  1.2141 +** pTestCollateInterp.
  1.2142 +*/
  1.2143 +static Tcl_Interp* pTestCollateInterp;
  1.2144 +static int test_collate_func(
  1.2145 +  void *pCtx, 
  1.2146 +  int nA, const void *zA,
  1.2147 +  int nB, const void *zB
  1.2148 +){
  1.2149 +  Tcl_Interp *i = pTestCollateInterp;
  1.2150 +  int encin = (int)pCtx;
  1.2151 +  int res;
  1.2152 +  int n;
  1.2153 +
  1.2154 +  sqlite3_value *pVal;
  1.2155 +  Tcl_Obj *pX;
  1.2156 +
  1.2157 +  pX = Tcl_NewStringObj("test_collate", -1);
  1.2158 +  Tcl_IncrRefCount(pX);
  1.2159 +
  1.2160 +  switch( encin ){
  1.2161 +    case SQLITE_UTF8:
  1.2162 +      Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
  1.2163 +      break;
  1.2164 +    case SQLITE_UTF16LE:
  1.2165 +      Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
  1.2166 +      break;
  1.2167 +    case SQLITE_UTF16BE:
  1.2168 +      Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
  1.2169 +      break;
  1.2170 +    default:
  1.2171 +      assert(0);
  1.2172 +  }
  1.2173 +
  1.2174 +  pVal = sqlite3ValueNew(0);
  1.2175 +  sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
  1.2176 +  n = sqlite3_value_bytes(pVal);
  1.2177 +  Tcl_ListObjAppendElement(i,pX,
  1.2178 +      Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
  1.2179 +  sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
  1.2180 +  n = sqlite3_value_bytes(pVal);
  1.2181 +  Tcl_ListObjAppendElement(i,pX,
  1.2182 +      Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
  1.2183 +  sqlite3ValueFree(pVal);
  1.2184 +
  1.2185 +  Tcl_EvalObjEx(i, pX, 0);
  1.2186 +  Tcl_DecrRefCount(pX);
  1.2187 +  Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
  1.2188 +  return res;
  1.2189 +}
  1.2190 +static int test_collate(
  1.2191 +  void * clientData,
  1.2192 +  Tcl_Interp *interp,
  1.2193 +  int objc,
  1.2194 +  Tcl_Obj *CONST objv[]
  1.2195 +){
  1.2196 +  sqlite3 *db;
  1.2197 +  int val;
  1.2198 +  sqlite3_value *pVal;
  1.2199 +  int rc;
  1.2200 +
  1.2201 +  if( objc!=5 ) goto bad_args;
  1.2202 +  pTestCollateInterp = interp;
  1.2203 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.2204 +
  1.2205 +  if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
  1.2206 +  rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8, 
  1.2207 +          (void *)SQLITE_UTF8, val?test_collate_func:0);
  1.2208 +  if( rc==SQLITE_OK ){
  1.2209 +    const void *zUtf16;
  1.2210 +    if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
  1.2211 +    rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE, 
  1.2212 +            (void *)SQLITE_UTF16LE, val?test_collate_func:0);
  1.2213 +    if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
  1.2214 +
  1.2215 +#if 0
  1.2216 +    if( sqlite3_iMallocFail>0 ){
  1.2217 +      sqlite3_iMallocFail++;
  1.2218 +    }
  1.2219 +#endif
  1.2220 +    sqlite3_mutex_enter(db->mutex);
  1.2221 +    pVal = sqlite3ValueNew(db);
  1.2222 +    sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
  1.2223 +    zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
  1.2224 +    if( db->mallocFailed ){
  1.2225 +      rc = SQLITE_NOMEM;
  1.2226 +    }else{
  1.2227 +      rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE, 
  1.2228 +          (void *)SQLITE_UTF16BE, val?test_collate_func:0);
  1.2229 +    }
  1.2230 +    sqlite3ValueFree(pVal);
  1.2231 +    sqlite3_mutex_leave(db->mutex);
  1.2232 +  }
  1.2233 +  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  1.2234 +  
  1.2235 +  if( rc!=SQLITE_OK ){
  1.2236 +    Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
  1.2237 +    return TCL_ERROR;
  1.2238 +  }
  1.2239 +  return TCL_OK;
  1.2240 +
  1.2241 +bad_args:
  1.2242 +  Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.2243 +      Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
  1.2244 +  return TCL_ERROR;
  1.2245 +}
  1.2246 +
  1.2247 +/*
  1.2248 +** When the collation needed callback is invoked, record the name of 
  1.2249 +** the requested collating function here.  The recorded name is linked
  1.2250 +** to a TCL variable and used to make sure that the requested collation
  1.2251 +** name is correct.
  1.2252 +*/
  1.2253 +static char zNeededCollation[200];
  1.2254 +static char *pzNeededCollation = zNeededCollation;
  1.2255 +
  1.2256 +
  1.2257 +/*
  1.2258 +** Called when a collating sequence is needed.  Registered using
  1.2259 +** sqlite3_collation_needed16().
  1.2260 +*/
  1.2261 +static void test_collate_needed_cb(
  1.2262 +  void *pCtx, 
  1.2263 +  sqlite3 *db,
  1.2264 +  int eTextRep,
  1.2265 +  const void *pName
  1.2266 +){
  1.2267 +  int enc = ENC(db);
  1.2268 +  int i;
  1.2269 +  char *z;
  1.2270 +  for(z = (char*)pName, i=0; *z || z[1]; z++){
  1.2271 +    if( *z ) zNeededCollation[i++] = *z;
  1.2272 +  }
  1.2273 +  zNeededCollation[i] = 0;
  1.2274 +  sqlite3_create_collation(
  1.2275 +      db, "test_collate", ENC(db), (void *)enc, test_collate_func);
  1.2276 +}
  1.2277 +
  1.2278 +/*
  1.2279 +** Usage: add_test_collate_needed DB
  1.2280 +*/
  1.2281 +static int test_collate_needed(
  1.2282 +  void * clientData,
  1.2283 +  Tcl_Interp *interp,
  1.2284 +  int objc,
  1.2285 +  Tcl_Obj *CONST objv[]
  1.2286 +){
  1.2287 +  sqlite3 *db;
  1.2288 +  int rc;
  1.2289 +
  1.2290 +  if( objc!=2 ) goto bad_args;
  1.2291 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.2292 +  rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
  1.2293 +  zNeededCollation[0] = 0;
  1.2294 +  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  1.2295 +  return TCL_OK;
  1.2296 +
  1.2297 +bad_args:
  1.2298 +  Tcl_WrongNumArgs(interp, 1, objv, "DB");
  1.2299 +  return TCL_ERROR;
  1.2300 +}
  1.2301 +
  1.2302 +/*
  1.2303 +** tclcmd:   add_alignment_test_collations  DB
  1.2304 +**
  1.2305 +** Add two new collating sequences to the database DB
  1.2306 +**
  1.2307 +**     utf16_aligned
  1.2308 +**     utf16_unaligned
  1.2309 +**
  1.2310 +** Both collating sequences use the same sort order as BINARY.
  1.2311 +** The only difference is that the utf16_aligned collating
  1.2312 +** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
  1.2313 +** Both collating functions increment the unaligned utf16 counter
  1.2314 +** whenever they see a string that begins on an odd byte boundary.
  1.2315 +*/
  1.2316 +static int unaligned_string_counter = 0;
  1.2317 +static int alignmentCollFunc(
  1.2318 +  void *NotUsed,
  1.2319 +  int nKey1, const void *pKey1,
  1.2320 +  int nKey2, const void *pKey2
  1.2321 +){
  1.2322 +  int rc, n;
  1.2323 +  n = nKey1<nKey2 ? nKey1 : nKey2;
  1.2324 +  if( nKey1>0 && 1==(1&(int)pKey1) ) unaligned_string_counter++;
  1.2325 +  if( nKey2>0 && 1==(1&(int)pKey2) ) unaligned_string_counter++;
  1.2326 +  rc = memcmp(pKey1, pKey2, n);
  1.2327 +  if( rc==0 ){
  1.2328 +    rc = nKey1 - nKey2;
  1.2329 +  }
  1.2330 +  return rc;
  1.2331 +}
  1.2332 +static int add_alignment_test_collations(
  1.2333 +  void * clientData,
  1.2334 +  Tcl_Interp *interp,
  1.2335 +  int objc,
  1.2336 +  Tcl_Obj *CONST objv[]
  1.2337 +){
  1.2338 +  sqlite3 *db;
  1.2339 +  if( objc>=2 ){
  1.2340 +    if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.2341 +    sqlite3_create_collation(db, "utf16_unaligned",
  1.2342 +        SQLITE_UTF16, 
  1.2343 +        0, alignmentCollFunc);
  1.2344 +    sqlite3_create_collation(db, "utf16_aligned",
  1.2345 +        SQLITE_UTF16 | SQLITE_UTF16_ALIGNED, 
  1.2346 +        0, alignmentCollFunc);
  1.2347 +  }
  1.2348 +  return SQLITE_OK;
  1.2349 +}
  1.2350 +#endif /* !defined(SQLITE_OMIT_UTF16) */
  1.2351 +
  1.2352 +/*
  1.2353 +** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
  1.2354 +**
  1.2355 +** This function is used to test that SQLite selects the correct user
  1.2356 +** function callback when multiple versions (for different text encodings)
  1.2357 +** are available.
  1.2358 +**
  1.2359 +** Calling this routine registers up to three versions of the user function
  1.2360 +** "test_function" with database handle <db>.  If the second argument is
  1.2361 +** true, then a version of test_function is registered for UTF-8, if the
  1.2362 +** third is true, a version is registered for UTF-16le, if the fourth is
  1.2363 +** true, a UTF-16be version is available.  Previous versions of
  1.2364 +** test_function are deleted.
  1.2365 +**
  1.2366 +** The user function is implemented by calling the following TCL script:
  1.2367 +**
  1.2368 +**   "test_function <enc> <arg>"
  1.2369 +**
  1.2370 +** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
  1.2371 +** single argument passed to the SQL function. The value returned by
  1.2372 +** the TCL script is used as the return value of the SQL function. It
  1.2373 +** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
  1.2374 +** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
  1.2375 +** prefers UTF-16BE.
  1.2376 +*/
  1.2377 +#ifndef SQLITE_OMIT_UTF16
  1.2378 +static void test_function_utf8(
  1.2379 +  sqlite3_context *pCtx, 
  1.2380 +  int nArg,
  1.2381 +  sqlite3_value **argv
  1.2382 +){
  1.2383 +  Tcl_Interp *interp;
  1.2384 +  Tcl_Obj *pX;
  1.2385 +  sqlite3_value *pVal;
  1.2386 +  interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
  1.2387 +  pX = Tcl_NewStringObj("test_function", -1);
  1.2388 +  Tcl_IncrRefCount(pX);
  1.2389 +  Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
  1.2390 +  Tcl_ListObjAppendElement(interp, pX, 
  1.2391 +      Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
  1.2392 +  Tcl_EvalObjEx(interp, pX, 0);
  1.2393 +  Tcl_DecrRefCount(pX);
  1.2394 +  sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
  1.2395 +  pVal = sqlite3ValueNew(0);
  1.2396 +  sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 
  1.2397 +      SQLITE_UTF8, SQLITE_STATIC);
  1.2398 +  sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
  1.2399 +      -1, SQLITE_TRANSIENT);
  1.2400 +  sqlite3ValueFree(pVal);
  1.2401 +}
  1.2402 +static void test_function_utf16le(
  1.2403 +  sqlite3_context *pCtx, 
  1.2404 +  int nArg,
  1.2405 +  sqlite3_value **argv
  1.2406 +){
  1.2407 +  Tcl_Interp *interp;
  1.2408 +  Tcl_Obj *pX;
  1.2409 +  sqlite3_value *pVal;
  1.2410 +  interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
  1.2411 +  pX = Tcl_NewStringObj("test_function", -1);
  1.2412 +  Tcl_IncrRefCount(pX);
  1.2413 +  Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
  1.2414 +  Tcl_ListObjAppendElement(interp, pX, 
  1.2415 +      Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
  1.2416 +  Tcl_EvalObjEx(interp, pX, 0);
  1.2417 +  Tcl_DecrRefCount(pX);
  1.2418 +  pVal = sqlite3ValueNew(0);
  1.2419 +  sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 
  1.2420 +      SQLITE_UTF8, SQLITE_STATIC);
  1.2421 +  sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
  1.2422 +  sqlite3ValueFree(pVal);
  1.2423 +}
  1.2424 +static void test_function_utf16be(
  1.2425 +  sqlite3_context *pCtx, 
  1.2426 +  int nArg,
  1.2427 +  sqlite3_value **argv
  1.2428 +){
  1.2429 +  Tcl_Interp *interp;
  1.2430 +  Tcl_Obj *pX;
  1.2431 +  sqlite3_value *pVal;
  1.2432 +  interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
  1.2433 +  pX = Tcl_NewStringObj("test_function", -1);
  1.2434 +  Tcl_IncrRefCount(pX);
  1.2435 +  Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
  1.2436 +  Tcl_ListObjAppendElement(interp, pX, 
  1.2437 +      Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
  1.2438 +  Tcl_EvalObjEx(interp, pX, 0);
  1.2439 +  Tcl_DecrRefCount(pX);
  1.2440 +  pVal = sqlite3ValueNew(0);
  1.2441 +  sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), 
  1.2442 +      SQLITE_UTF8, SQLITE_STATIC);
  1.2443 +  sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal),
  1.2444 +      -1, SQLITE_TRANSIENT);
  1.2445 +  sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal),
  1.2446 +      -1, SQLITE_TRANSIENT);
  1.2447 +  sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
  1.2448 +      -1, SQLITE_TRANSIENT);
  1.2449 +  sqlite3ValueFree(pVal);
  1.2450 +}
  1.2451 +#endif /* SQLITE_OMIT_UTF16 */
  1.2452 +static int test_function(
  1.2453 +  void * clientData,
  1.2454 +  Tcl_Interp *interp,
  1.2455 +  int objc,
  1.2456 +  Tcl_Obj *CONST objv[]
  1.2457 +){
  1.2458 +#ifndef SQLITE_OMIT_UTF16
  1.2459 +  sqlite3 *db;
  1.2460 +  int val;
  1.2461 +
  1.2462 +  if( objc!=5 ) goto bad_args;
  1.2463 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.2464 +
  1.2465 +  if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
  1.2466 +  if( val ){
  1.2467 +    sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8, 
  1.2468 +        interp, test_function_utf8, 0, 0);
  1.2469 +  }
  1.2470 +  if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
  1.2471 +  if( val ){
  1.2472 +    sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE, 
  1.2473 +        interp, test_function_utf16le, 0, 0);
  1.2474 +  }
  1.2475 +  if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
  1.2476 +  if( val ){
  1.2477 +    sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE, 
  1.2478 +        interp, test_function_utf16be, 0, 0);
  1.2479 +  }
  1.2480 +
  1.2481 +  return TCL_OK;
  1.2482 +bad_args:
  1.2483 +  Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.2484 +      Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
  1.2485 +#endif /* SQLITE_OMIT_UTF16 */
  1.2486 +  return TCL_ERROR;
  1.2487 +}
  1.2488 +
  1.2489 +/*
  1.2490 +** Usage:         test_errstr <err code>
  1.2491 +**
  1.2492 +** Test that the english language string equivalents for sqlite error codes
  1.2493 +** are sane. The parameter is an integer representing an sqlite error code.
  1.2494 +** The result is a list of two elements, the string representation of the
  1.2495 +** error code and the english language explanation.
  1.2496 +*/
  1.2497 +static int test_errstr(
  1.2498 +  void * clientData,
  1.2499 +  Tcl_Interp *interp,
  1.2500 +  int objc,
  1.2501 +  Tcl_Obj *CONST objv[]
  1.2502 +){
  1.2503 +  char *zCode;
  1.2504 +  int i;
  1.2505 +  if( objc!=1 ){
  1.2506 +    Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
  1.2507 +  }
  1.2508 +
  1.2509 +  zCode = Tcl_GetString(objv[1]);
  1.2510 +  for(i=0; i<200; i++){
  1.2511 +    if( 0==strcmp(t1ErrorName(i), zCode) ) break;
  1.2512 +  }
  1.2513 +  Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
  1.2514 +  return TCL_OK;
  1.2515 +}
  1.2516 +
  1.2517 +/*
  1.2518 +** Usage:    breakpoint
  1.2519 +**
  1.2520 +** This routine exists for one purpose - to provide a place to put a
  1.2521 +** breakpoint with GDB that can be triggered using TCL code.  The use
  1.2522 +** for this is when a particular test fails on (say) the 1485th iteration.
  1.2523 +** In the TCL test script, we can add code like this:
  1.2524 +**
  1.2525 +**     if {$i==1485} breakpoint
  1.2526 +**
  1.2527 +** Then run testfixture in the debugger and wait for the breakpoint to
  1.2528 +** fire.  Then additional breakpoints can be set to trace down the bug.
  1.2529 +*/
  1.2530 +static int test_breakpoint(
  1.2531 +  void *NotUsed,
  1.2532 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.2533 +  int argc,              /* Number of arguments */
  1.2534 +  char **argv            /* Text of each argument */
  1.2535 +){
  1.2536 +  return TCL_OK;         /* Do nothing */
  1.2537 +}
  1.2538 +
  1.2539 +/*
  1.2540 +** Usage:   sqlite3_bind_zeroblob  STMT IDX N
  1.2541 +**
  1.2542 +** Test the sqlite3_bind_zeroblob interface.  STMT is a prepared statement.
  1.2543 +** IDX is the index of a wildcard in the prepared statement.  This command
  1.2544 +** binds a N-byte zero-filled BLOB to the wildcard.
  1.2545 +*/
  1.2546 +static int test_bind_zeroblob(
  1.2547 +  void * clientData,
  1.2548 +  Tcl_Interp *interp,
  1.2549 +  int objc,
  1.2550 +  Tcl_Obj *CONST objv[]
  1.2551 +){
  1.2552 +  sqlite3_stmt *pStmt;
  1.2553 +  int idx;
  1.2554 +  int n;
  1.2555 +  int rc;
  1.2556 +
  1.2557 +  if( objc!=4 ){
  1.2558 +    Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
  1.2559 +    return TCL_ERROR;
  1.2560 +  }
  1.2561 +
  1.2562 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.2563 +  if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
  1.2564 +  if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
  1.2565 +
  1.2566 +  rc = sqlite3_bind_zeroblob(pStmt, idx, n);
  1.2567 +  if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  1.2568 +  if( rc!=SQLITE_OK ){
  1.2569 +    return TCL_ERROR;
  1.2570 +  }
  1.2571 +
  1.2572 +  return TCL_OK;
  1.2573 +}
  1.2574 +
  1.2575 +/*
  1.2576 +** Usage:   sqlite3_bind_int  STMT N VALUE
  1.2577 +**
  1.2578 +** Test the sqlite3_bind_int interface.  STMT is a prepared statement.
  1.2579 +** N is the index of a wildcard in the prepared statement.  This command
  1.2580 +** binds a 32-bit integer VALUE to that wildcard.
  1.2581 +*/
  1.2582 +static int test_bind_int(
  1.2583 +  void * clientData,
  1.2584 +  Tcl_Interp *interp,
  1.2585 +  int objc,
  1.2586 +  Tcl_Obj *CONST objv[]
  1.2587 +){
  1.2588 +  sqlite3_stmt *pStmt;
  1.2589 +  int idx;
  1.2590 +  int value;
  1.2591 +  int rc;
  1.2592 +
  1.2593 +  if( objc!=4 ){
  1.2594 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.2595 +        Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
  1.2596 +    return TCL_ERROR;
  1.2597 +  }
  1.2598 +
  1.2599 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.2600 +  if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
  1.2601 +  if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
  1.2602 +
  1.2603 +  rc = sqlite3_bind_int(pStmt, idx, value);
  1.2604 +  if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  1.2605 +  if( rc!=SQLITE_OK ){
  1.2606 +    return TCL_ERROR;
  1.2607 +  }
  1.2608 +
  1.2609 +  return TCL_OK;
  1.2610 +}
  1.2611 +
  1.2612 +
  1.2613 +/*
  1.2614 +** Usage:   sqlite3_bind_int64  STMT N VALUE
  1.2615 +**
  1.2616 +** Test the sqlite3_bind_int64 interface.  STMT is a prepared statement.
  1.2617 +** N is the index of a wildcard in the prepared statement.  This command
  1.2618 +** binds a 64-bit integer VALUE to that wildcard.
  1.2619 +*/
  1.2620 +static int test_bind_int64(
  1.2621 +  void * clientData,
  1.2622 +  Tcl_Interp *interp,
  1.2623 +  int objc,
  1.2624 +  Tcl_Obj *CONST objv[]
  1.2625 +){
  1.2626 +  sqlite3_stmt *pStmt;
  1.2627 +  int idx;
  1.2628 +  i64 value;
  1.2629 +  int rc;
  1.2630 +
  1.2631 +  if( objc!=4 ){
  1.2632 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.2633 +        Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
  1.2634 +    return TCL_ERROR;
  1.2635 +  }
  1.2636 +
  1.2637 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.2638 +  if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
  1.2639 +  if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
  1.2640 +
  1.2641 +  rc = sqlite3_bind_int64(pStmt, idx, value);
  1.2642 +  if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  1.2643 +  if( rc!=SQLITE_OK ){
  1.2644 +    return TCL_ERROR;
  1.2645 +  }
  1.2646 +
  1.2647 +  return TCL_OK;
  1.2648 +}
  1.2649 +
  1.2650 +
  1.2651 +/*
  1.2652 +** Usage:   sqlite3_bind_double  STMT N VALUE
  1.2653 +**
  1.2654 +** Test the sqlite3_bind_double interface.  STMT is a prepared statement.
  1.2655 +** N is the index of a wildcard in the prepared statement.  This command
  1.2656 +** binds a 64-bit integer VALUE to that wildcard.
  1.2657 +*/
  1.2658 +static int test_bind_double(
  1.2659 +  void * clientData,
  1.2660 +  Tcl_Interp *interp,
  1.2661 +  int objc,
  1.2662 +  Tcl_Obj *CONST objv[]
  1.2663 +){
  1.2664 +  sqlite3_stmt *pStmt;
  1.2665 +  int idx;
  1.2666 +  double value;
  1.2667 +  int rc;
  1.2668 +  const char *zVal;
  1.2669 +  int i;
  1.2670 +  static const struct {
  1.2671 +    const char *zName;     /* Name of the special floating point value */
  1.2672 +    unsigned int iUpper;   /* Upper 32 bits */
  1.2673 +    unsigned int iLower;   /* Lower 32 bits */
  1.2674 +  } aSpecialFp[] = {
  1.2675 +    {  "NaN",      0x7fffffff, 0xffffffff },
  1.2676 +    {  "SNaN",     0x7ff7ffff, 0xffffffff },
  1.2677 +    {  "-NaN",     0xffffffff, 0xffffffff },
  1.2678 +    {  "-SNaN",    0xfff7ffff, 0xffffffff },
  1.2679 +    {  "+Inf",     0x7ff00000, 0x00000000 },
  1.2680 +    {  "-Inf",     0xfff00000, 0x00000000 },
  1.2681 +    {  "Epsilon",  0x00000000, 0x00000001 },
  1.2682 +    {  "-Epsilon", 0x80000000, 0x00000001 },
  1.2683 +    {  "NaN0",     0x7ff80000, 0x00000000 },
  1.2684 +    {  "-NaN0",    0xfff80000, 0x00000000 },
  1.2685 +  };
  1.2686 +
  1.2687 +  if( objc!=4 ){
  1.2688 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.2689 +        Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
  1.2690 +    return TCL_ERROR;
  1.2691 +  }
  1.2692 +
  1.2693 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.2694 +  if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
  1.2695 +
  1.2696 +  /* Intercept the string "NaN" and generate a NaN value for it.
  1.2697 +  ** All other strings are passed through to Tcl_GetDoubleFromObj().
  1.2698 +  ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions
  1.2699 +  ** contain a bug.
  1.2700 +  */
  1.2701 +  zVal = Tcl_GetString(objv[3]);
  1.2702 +  for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){
  1.2703 +    if( strcmp(aSpecialFp[i].zName, zVal)==0 ){
  1.2704 +      sqlite3_uint64 x;
  1.2705 +      x = aSpecialFp[i].iUpper;
  1.2706 +      x <<= 32;
  1.2707 +      x |= aSpecialFp[i].iLower;
  1.2708 +      assert( sizeof(value)==8 );
  1.2709 +      assert( sizeof(x)==8 );
  1.2710 +      memcpy(&value, &x, 8);
  1.2711 +      break;
  1.2712 +    }
  1.2713 +  }
  1.2714 +  if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) &&
  1.2715 +         Tcl_GetDoubleFromObj(interp, objv[3], &value) ){
  1.2716 +    return TCL_ERROR;
  1.2717 +  }
  1.2718 +  rc = sqlite3_bind_double(pStmt, idx, value);
  1.2719 +  if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  1.2720 +  if( rc!=SQLITE_OK ){
  1.2721 +    return TCL_ERROR;
  1.2722 +  }
  1.2723 +
  1.2724 +  return TCL_OK;
  1.2725 +}
  1.2726 +
  1.2727 +/*
  1.2728 +** Usage:   sqlite3_bind_null  STMT N
  1.2729 +**
  1.2730 +** Test the sqlite3_bind_null interface.  STMT is a prepared statement.
  1.2731 +** N is the index of a wildcard in the prepared statement.  This command
  1.2732 +** binds a NULL to the wildcard.
  1.2733 +*/
  1.2734 +static int test_bind_null(
  1.2735 +  void * clientData,
  1.2736 +  Tcl_Interp *interp,
  1.2737 +  int objc,
  1.2738 +  Tcl_Obj *CONST objv[]
  1.2739 +){
  1.2740 +  sqlite3_stmt *pStmt;
  1.2741 +  int idx;
  1.2742 +  int rc;
  1.2743 +
  1.2744 +  if( objc!=3 ){
  1.2745 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.2746 +        Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
  1.2747 +    return TCL_ERROR;
  1.2748 +  }
  1.2749 +
  1.2750 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.2751 +  if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
  1.2752 +
  1.2753 +  rc = sqlite3_bind_null(pStmt, idx);
  1.2754 +  if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  1.2755 +  if( rc!=SQLITE_OK ){
  1.2756 +    return TCL_ERROR;
  1.2757 +  }
  1.2758 +
  1.2759 +  return TCL_OK;
  1.2760 +}
  1.2761 +
  1.2762 +/*
  1.2763 +** Usage:   sqlite3_bind_text  STMT N STRING BYTES
  1.2764 +**
  1.2765 +** Test the sqlite3_bind_text interface.  STMT is a prepared statement.
  1.2766 +** N is the index of a wildcard in the prepared statement.  This command
  1.2767 +** binds a UTF-8 string STRING to the wildcard.  The string is BYTES bytes
  1.2768 +** long.
  1.2769 +*/
  1.2770 +static int test_bind_text(
  1.2771 +  void * clientData,
  1.2772 +  Tcl_Interp *interp,
  1.2773 +  int objc,
  1.2774 +  Tcl_Obj *CONST objv[]
  1.2775 +){
  1.2776 +  sqlite3_stmt *pStmt;
  1.2777 +  int idx;
  1.2778 +  int bytes;
  1.2779 +  char *value;
  1.2780 +  int rc;
  1.2781 +
  1.2782 +  if( objc!=5 ){
  1.2783 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.2784 +        Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
  1.2785 +    return TCL_ERROR;
  1.2786 +  }
  1.2787 +
  1.2788 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.2789 +  if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
  1.2790 +  value = (char*)Tcl_GetByteArrayFromObj(objv[3], &bytes);
  1.2791 +  if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
  1.2792 +
  1.2793 +  rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
  1.2794 +  if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  1.2795 +  if( rc!=SQLITE_OK ){
  1.2796 +    Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
  1.2797 +    return TCL_ERROR;
  1.2798 +  }
  1.2799 +
  1.2800 +  return TCL_OK;
  1.2801 +}
  1.2802 +
  1.2803 +/*
  1.2804 +** Usage:   sqlite3_bind_text16 ?-static? STMT N STRING BYTES
  1.2805 +**
  1.2806 +** Test the sqlite3_bind_text16 interface.  STMT is a prepared statement.
  1.2807 +** N is the index of a wildcard in the prepared statement.  This command
  1.2808 +** binds a UTF-16 string STRING to the wildcard.  The string is BYTES bytes
  1.2809 +** long.
  1.2810 +*/
  1.2811 +static int test_bind_text16(
  1.2812 +  void * clientData,
  1.2813 +  Tcl_Interp *interp,
  1.2814 +  int objc,
  1.2815 +  Tcl_Obj *CONST objv[]
  1.2816 +){
  1.2817 +#ifndef SQLITE_OMIT_UTF16
  1.2818 +  sqlite3_stmt *pStmt;
  1.2819 +  int idx;
  1.2820 +  int bytes;
  1.2821 +  char *value;
  1.2822 +  int rc;
  1.2823 +
  1.2824 +  void (*xDel)() = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
  1.2825 +  Tcl_Obj *oStmt    = objv[objc-4];
  1.2826 +  Tcl_Obj *oN       = objv[objc-3];
  1.2827 +  Tcl_Obj *oString  = objv[objc-2];
  1.2828 +  Tcl_Obj *oBytes   = objv[objc-1];
  1.2829 +
  1.2830 +  if( objc!=5 && objc!=6){
  1.2831 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.2832 +        Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
  1.2833 +    return TCL_ERROR;
  1.2834 +  }
  1.2835 +
  1.2836 +  if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR;
  1.2837 +  if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR;
  1.2838 +  value = (char*)Tcl_GetByteArrayFromObj(oString, 0);
  1.2839 +  if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR;
  1.2840 +
  1.2841 +  rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
  1.2842 +  if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  1.2843 +  if( rc!=SQLITE_OK ){
  1.2844 +    Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
  1.2845 +    return TCL_ERROR;
  1.2846 +  }
  1.2847 +
  1.2848 +#endif /* SQLITE_OMIT_UTF16 */
  1.2849 +  return TCL_OK;
  1.2850 +}
  1.2851 +
  1.2852 +/*
  1.2853 +** Usage:   sqlite3_bind_blob ?-static? STMT N DATA BYTES
  1.2854 +**
  1.2855 +** Test the sqlite3_bind_blob interface.  STMT is a prepared statement.
  1.2856 +** N is the index of a wildcard in the prepared statement.  This command
  1.2857 +** binds a BLOB to the wildcard.  The BLOB is BYTES bytes in size.
  1.2858 +*/
  1.2859 +static int test_bind_blob(
  1.2860 +  void * clientData,
  1.2861 +  Tcl_Interp *interp,
  1.2862 +  int objc,
  1.2863 +  Tcl_Obj *CONST objv[]
  1.2864 +){
  1.2865 +  sqlite3_stmt *pStmt;
  1.2866 +  int idx;
  1.2867 +  int bytes;
  1.2868 +  char *value;
  1.2869 +  int rc;
  1.2870 +  sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT;
  1.2871 +
  1.2872 +  if( objc!=5 && objc!=6 ){
  1.2873 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.2874 +        Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
  1.2875 +    return TCL_ERROR;
  1.2876 +  }
  1.2877 +
  1.2878 +  if( objc==6 ){
  1.2879 +    xDestructor = SQLITE_STATIC;
  1.2880 +    objv++;
  1.2881 +  }
  1.2882 +
  1.2883 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.2884 +  if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
  1.2885 +  value = Tcl_GetString(objv[3]);
  1.2886 +  if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
  1.2887 +
  1.2888 +  rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor);
  1.2889 +  if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  1.2890 +  if( rc!=SQLITE_OK ){
  1.2891 +    return TCL_ERROR;
  1.2892 +  }
  1.2893 +
  1.2894 +  return TCL_OK;
  1.2895 +}
  1.2896 +
  1.2897 +/*
  1.2898 +** Usage:   sqlite3_bind_parameter_count  STMT
  1.2899 +**
  1.2900 +** Return the number of wildcards in the given statement.
  1.2901 +*/
  1.2902 +static int test_bind_parameter_count(
  1.2903 +  void * clientData,
  1.2904 +  Tcl_Interp *interp,
  1.2905 +  int objc,
  1.2906 +  Tcl_Obj *CONST objv[]
  1.2907 +){
  1.2908 +  sqlite3_stmt *pStmt;
  1.2909 +
  1.2910 +  if( objc!=2 ){
  1.2911 +    Tcl_WrongNumArgs(interp, 1, objv, "STMT");
  1.2912 +    return TCL_ERROR;
  1.2913 +  }
  1.2914 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.2915 +  Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
  1.2916 +  return TCL_OK;
  1.2917 +}
  1.2918 +
  1.2919 +/*
  1.2920 +** Usage:   sqlite3_bind_parameter_name  STMT  N
  1.2921 +**
  1.2922 +** Return the name of the Nth wildcard.  The first wildcard is 1.
  1.2923 +** An empty string is returned if N is out of range or if the wildcard
  1.2924 +** is nameless.
  1.2925 +*/
  1.2926 +static int test_bind_parameter_name(
  1.2927 +  void * clientData,
  1.2928 +  Tcl_Interp *interp,
  1.2929 +  int objc,
  1.2930 +  Tcl_Obj *CONST objv[]
  1.2931 +){
  1.2932 +  sqlite3_stmt *pStmt;
  1.2933 +  int i;
  1.2934 +
  1.2935 +  if( objc!=3 ){
  1.2936 +    Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
  1.2937 +    return TCL_ERROR;
  1.2938 +  }
  1.2939 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.2940 +  if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
  1.2941 +  Tcl_SetObjResult(interp, 
  1.2942 +     Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
  1.2943 +  );
  1.2944 +  return TCL_OK;
  1.2945 +}
  1.2946 +
  1.2947 +/*
  1.2948 +** Usage:   sqlite3_bind_parameter_index  STMT  NAME
  1.2949 +**
  1.2950 +** Return the index of the wildcard called NAME.  Return 0 if there is
  1.2951 +** no such wildcard.
  1.2952 +*/
  1.2953 +static int test_bind_parameter_index(
  1.2954 +  void * clientData,
  1.2955 +  Tcl_Interp *interp,
  1.2956 +  int objc,
  1.2957 +  Tcl_Obj *CONST objv[]
  1.2958 +){
  1.2959 +  sqlite3_stmt *pStmt;
  1.2960 +
  1.2961 +  if( objc!=3 ){
  1.2962 +    Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
  1.2963 +    return TCL_ERROR;
  1.2964 +  }
  1.2965 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.2966 +  Tcl_SetObjResult(interp, 
  1.2967 +     Tcl_NewIntObj(
  1.2968 +       sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
  1.2969 +     )
  1.2970 +  );
  1.2971 +  return TCL_OK;
  1.2972 +}
  1.2973 +
  1.2974 +/*
  1.2975 +** Usage:   sqlite3_clear_bindings STMT
  1.2976 +**
  1.2977 +*/
  1.2978 +static int test_clear_bindings(
  1.2979 +  void * clientData,
  1.2980 +  Tcl_Interp *interp,
  1.2981 +  int objc,
  1.2982 +  Tcl_Obj *CONST objv[]
  1.2983 +){
  1.2984 +  sqlite3_stmt *pStmt;
  1.2985 +
  1.2986 +  if( objc!=2 ){
  1.2987 +    Tcl_WrongNumArgs(interp, 1, objv, "STMT");
  1.2988 +    return TCL_ERROR;
  1.2989 +  }
  1.2990 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.2991 +  Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
  1.2992 +  return TCL_OK;
  1.2993 +}
  1.2994 +
  1.2995 +/*
  1.2996 +** Usage:   sqlite3_sleep MILLISECONDS
  1.2997 +*/
  1.2998 +static int test_sleep(
  1.2999 +  void * clientData,
  1.3000 +  Tcl_Interp *interp,
  1.3001 +  int objc,
  1.3002 +  Tcl_Obj *CONST objv[]
  1.3003 +){
  1.3004 +  int ms;
  1.3005 +
  1.3006 +  if( objc!=2 ){
  1.3007 +    Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS");
  1.3008 +    return TCL_ERROR;
  1.3009 +  }
  1.3010 +  if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){
  1.3011 +    return TCL_ERROR;
  1.3012 +  }
  1.3013 +  Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms)));
  1.3014 +  return TCL_OK;
  1.3015 +}
  1.3016 +
  1.3017 +/*
  1.3018 +** Usage: sqlite3_errcode DB
  1.3019 +**
  1.3020 +** Return the string representation of the most recent sqlite3_* API
  1.3021 +** error code. e.g. "SQLITE_ERROR".
  1.3022 +*/
  1.3023 +static int test_errcode(
  1.3024 +  void * clientData,
  1.3025 +  Tcl_Interp *interp,
  1.3026 +  int objc,
  1.3027 +  Tcl_Obj *CONST objv[]
  1.3028 +){
  1.3029 +  sqlite3 *db;
  1.3030 +  int rc;
  1.3031 +  char zBuf[30];
  1.3032 +
  1.3033 +  if( objc!=2 ){
  1.3034 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3035 +       Tcl_GetString(objv[0]), " DB", 0);
  1.3036 +    return TCL_ERROR;
  1.3037 +  }
  1.3038 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.3039 +  rc = sqlite3_errcode(db);
  1.3040 +  if( (rc&0xff)==rc ){
  1.3041 +    zBuf[0] = 0;
  1.3042 +  }else{
  1.3043 +    sprintf(zBuf,"+%d", rc>>8);
  1.3044 +  }
  1.3045 +  Tcl_AppendResult(interp, (char *)t1ErrorName(rc), zBuf, 0);
  1.3046 +  return TCL_OK;
  1.3047 +}
  1.3048 +
  1.3049 +/*
  1.3050 +** Usage:   test_errmsg DB
  1.3051 +**
  1.3052 +** Returns the UTF-8 representation of the error message string for the
  1.3053 +** most recent sqlite3_* API call.
  1.3054 +*/
  1.3055 +static int test_errmsg(
  1.3056 +  void * clientData,
  1.3057 +  Tcl_Interp *interp,
  1.3058 +  int objc,
  1.3059 +  Tcl_Obj *CONST objv[]
  1.3060 +){
  1.3061 +  sqlite3 *db;
  1.3062 +  const char *zErr;
  1.3063 +
  1.3064 +  if( objc!=2 ){
  1.3065 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3066 +       Tcl_GetString(objv[0]), " DB", 0);
  1.3067 +    return TCL_ERROR;
  1.3068 +  }
  1.3069 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.3070 +
  1.3071 +  zErr = sqlite3_errmsg(db);
  1.3072 +  Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
  1.3073 +  return TCL_OK;
  1.3074 +}
  1.3075 +
  1.3076 +/*
  1.3077 +** Usage:   test_errmsg16 DB
  1.3078 +**
  1.3079 +** Returns the UTF-16 representation of the error message string for the
  1.3080 +** most recent sqlite3_* API call. This is a byte array object at the TCL 
  1.3081 +** level, and it includes the 0x00 0x00 terminator bytes at the end of the
  1.3082 +** UTF-16 string.
  1.3083 +*/
  1.3084 +static int test_errmsg16(
  1.3085 +  void * clientData,
  1.3086 +  Tcl_Interp *interp,
  1.3087 +  int objc,
  1.3088 +  Tcl_Obj *CONST objv[]
  1.3089 +){
  1.3090 +#ifndef SQLITE_OMIT_UTF16
  1.3091 +  sqlite3 *db;
  1.3092 +  const void *zErr;
  1.3093 +  int bytes = 0;
  1.3094 +
  1.3095 +  if( objc!=2 ){
  1.3096 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3097 +       Tcl_GetString(objv[0]), " DB", 0);
  1.3098 +    return TCL_ERROR;
  1.3099 +  }
  1.3100 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.3101 +
  1.3102 +  zErr = sqlite3_errmsg16(db);
  1.3103 +  if( zErr ){
  1.3104 +    bytes = sqlite3Utf16ByteLen(zErr, -1);
  1.3105 +  }
  1.3106 +  Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
  1.3107 +#endif /* SQLITE_OMIT_UTF16 */
  1.3108 +  return TCL_OK;
  1.3109 +}
  1.3110 +
  1.3111 +/*
  1.3112 +** Usage: sqlite3_prepare DB sql bytes tailvar
  1.3113 +**
  1.3114 +** Compile up to <bytes> bytes of the supplied SQL string <sql> using
  1.3115 +** database handle <DB>. The parameter <tailval> is the name of a global
  1.3116 +** variable that is set to the unused portion of <sql> (if any). A
  1.3117 +** STMT handle is returned.
  1.3118 +*/
  1.3119 +static int test_prepare(
  1.3120 +  void * clientData,
  1.3121 +  Tcl_Interp *interp,
  1.3122 +  int objc,
  1.3123 +  Tcl_Obj *CONST objv[]
  1.3124 +){
  1.3125 +  sqlite3 *db;
  1.3126 +  const char *zSql;
  1.3127 +  int bytes;
  1.3128 +  const char *zTail = 0;
  1.3129 +  sqlite3_stmt *pStmt = 0;
  1.3130 +  char zBuf[50];
  1.3131 +  int rc;
  1.3132 +
  1.3133 +  if( objc!=5 ){
  1.3134 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3135 +       Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
  1.3136 +    return TCL_ERROR;
  1.3137 +  }
  1.3138 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.3139 +  zSql = Tcl_GetString(objv[2]);
  1.3140 +  if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
  1.3141 +
  1.3142 +  rc = sqlite3_prepare(db, zSql, bytes, &pStmt, &zTail);
  1.3143 +  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  1.3144 +  if( zTail ){
  1.3145 +    if( bytes>=0 ){
  1.3146 +      bytes = bytes - (zTail-zSql);
  1.3147 +    }
  1.3148 +    if( strlen(zTail)<bytes ){
  1.3149 +      bytes = strlen(zTail);
  1.3150 +    }
  1.3151 +    Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
  1.3152 +  }
  1.3153 +  if( rc!=SQLITE_OK ){
  1.3154 +    assert( pStmt==0 );
  1.3155 +    sprintf(zBuf, "(%d) ", rc);
  1.3156 +    Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
  1.3157 +    return TCL_ERROR;
  1.3158 +  }
  1.3159 +
  1.3160 +  if( pStmt ){
  1.3161 +    if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
  1.3162 +    Tcl_AppendResult(interp, zBuf, 0);
  1.3163 +  }
  1.3164 +  return TCL_OK;
  1.3165 +}
  1.3166 +
  1.3167 +/*
  1.3168 +** Usage: sqlite3_prepare_v2 DB sql bytes tailvar
  1.3169 +**
  1.3170 +** Compile up to <bytes> bytes of the supplied SQL string <sql> using
  1.3171 +** database handle <DB>. The parameter <tailval> is the name of a global
  1.3172 +** variable that is set to the unused portion of <sql> (if any). A
  1.3173 +** STMT handle is returned.
  1.3174 +*/
  1.3175 +static int test_prepare_v2(
  1.3176 +  void * clientData,
  1.3177 +  Tcl_Interp *interp,
  1.3178 +  int objc,
  1.3179 +  Tcl_Obj *CONST objv[]
  1.3180 +){
  1.3181 +  sqlite3 *db;
  1.3182 +  const char *zSql;
  1.3183 +  int bytes;
  1.3184 +  const char *zTail = 0;
  1.3185 +  sqlite3_stmt *pStmt = 0;
  1.3186 +  char zBuf[50];
  1.3187 +  int rc;
  1.3188 +
  1.3189 +  if( objc!=5 ){
  1.3190 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3191 +       Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
  1.3192 +    return TCL_ERROR;
  1.3193 +  }
  1.3194 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.3195 +  zSql = Tcl_GetString(objv[2]);
  1.3196 +  if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
  1.3197 +
  1.3198 +  rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, &zTail);
  1.3199 +  assert(rc==SQLITE_OK || pStmt==0);
  1.3200 +  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  1.3201 +  if( zTail ){
  1.3202 +    if( bytes>=0 ){
  1.3203 +      bytes = bytes - (zTail-zSql);
  1.3204 +    }
  1.3205 +    Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
  1.3206 +  }
  1.3207 +  if( rc!=SQLITE_OK ){
  1.3208 +    assert( pStmt==0 );
  1.3209 +    sprintf(zBuf, "(%d) ", rc);
  1.3210 +    Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
  1.3211 +    return TCL_ERROR;
  1.3212 +  }
  1.3213 +
  1.3214 +  if( pStmt ){
  1.3215 +    if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
  1.3216 +    Tcl_AppendResult(interp, zBuf, 0);
  1.3217 +  }
  1.3218 +  return TCL_OK;
  1.3219 +}
  1.3220 +
  1.3221 +/*
  1.3222 +** Usage: sqlite3_prepare_tkt3134 DB
  1.3223 +**
  1.3224 +** Generate a prepared statement for a zero-byte string as a test
  1.3225 +** for ticket #3134.  The string should be preceeded by a zero byte.
  1.3226 +*/
  1.3227 +static int test_prepare_tkt3134(
  1.3228 +  void * clientData,
  1.3229 +  Tcl_Interp *interp,
  1.3230 +  int objc,
  1.3231 +  Tcl_Obj *CONST objv[]
  1.3232 +){
  1.3233 +  sqlite3 *db;
  1.3234 +  static const char zSql[] = "\000SELECT 1";
  1.3235 +  sqlite3_stmt *pStmt = 0;
  1.3236 +  char zBuf[50];
  1.3237 +  int rc;
  1.3238 +
  1.3239 +  if( objc!=2 ){
  1.3240 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3241 +       Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
  1.3242 +    return TCL_ERROR;
  1.3243 +  }
  1.3244 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.3245 +  rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0);
  1.3246 +  assert(rc==SQLITE_OK || pStmt==0);
  1.3247 +  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  1.3248 +  if( rc!=SQLITE_OK ){
  1.3249 +    assert( pStmt==0 );
  1.3250 +    sprintf(zBuf, "(%d) ", rc);
  1.3251 +    Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
  1.3252 +    return TCL_ERROR;
  1.3253 +  }
  1.3254 +
  1.3255 +  if( pStmt ){
  1.3256 +    if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
  1.3257 +    Tcl_AppendResult(interp, zBuf, 0);
  1.3258 +  }
  1.3259 +  return TCL_OK;
  1.3260 +}
  1.3261 +
  1.3262 +/*
  1.3263 +** Usage: sqlite3_prepare16 DB sql bytes tailvar
  1.3264 +**
  1.3265 +** Compile up to <bytes> bytes of the supplied SQL string <sql> using
  1.3266 +** database handle <DB>. The parameter <tailval> is the name of a global
  1.3267 +** variable that is set to the unused portion of <sql> (if any). A
  1.3268 +** STMT handle is returned.
  1.3269 +*/
  1.3270 +static int test_prepare16(
  1.3271 +  void * clientData,
  1.3272 +  Tcl_Interp *interp,
  1.3273 +  int objc,
  1.3274 +  Tcl_Obj *CONST objv[]
  1.3275 +){
  1.3276 +#ifndef SQLITE_OMIT_UTF16
  1.3277 +  sqlite3 *db;
  1.3278 +  const void *zSql;
  1.3279 +  const void *zTail = 0;
  1.3280 +  Tcl_Obj *pTail = 0;
  1.3281 +  sqlite3_stmt *pStmt = 0;
  1.3282 +  char zBuf[50]; 
  1.3283 +  int rc;
  1.3284 +  int bytes;                /* The integer specified as arg 3 */
  1.3285 +  int objlen;               /* The byte-array length of arg 2 */
  1.3286 +
  1.3287 +  if( objc!=5 ){
  1.3288 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3289 +       Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
  1.3290 +    return TCL_ERROR;
  1.3291 +  }
  1.3292 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.3293 +  zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
  1.3294 +  if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
  1.3295 +
  1.3296 +  rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, &zTail);
  1.3297 +  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  1.3298 +  if( rc ){
  1.3299 +    return TCL_ERROR;
  1.3300 +  }
  1.3301 +
  1.3302 +  if( zTail ){
  1.3303 +    objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
  1.3304 +  }else{
  1.3305 +    objlen = 0;
  1.3306 +  }
  1.3307 +  pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
  1.3308 +  Tcl_IncrRefCount(pTail);
  1.3309 +  Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
  1.3310 +  Tcl_DecrRefCount(pTail);
  1.3311 +
  1.3312 +  if( pStmt ){
  1.3313 +    if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
  1.3314 +  }
  1.3315 +  Tcl_AppendResult(interp, zBuf, 0);
  1.3316 +#endif /* SQLITE_OMIT_UTF16 */
  1.3317 +  return TCL_OK;
  1.3318 +}
  1.3319 +
  1.3320 +/*
  1.3321 +** Usage: sqlite3_prepare16_v2 DB sql bytes tailvar
  1.3322 +**
  1.3323 +** Compile up to <bytes> bytes of the supplied SQL string <sql> using
  1.3324 +** database handle <DB>. The parameter <tailval> is the name of a global
  1.3325 +** variable that is set to the unused portion of <sql> (if any). A
  1.3326 +** STMT handle is returned.
  1.3327 +*/
  1.3328 +static int test_prepare16_v2(
  1.3329 +  void * clientData,
  1.3330 +  Tcl_Interp *interp,
  1.3331 +  int objc,
  1.3332 +  Tcl_Obj *CONST objv[]
  1.3333 +){
  1.3334 +#ifndef SQLITE_OMIT_UTF16
  1.3335 +  sqlite3 *db;
  1.3336 +  const void *zSql;
  1.3337 +  const void *zTail = 0;
  1.3338 +  Tcl_Obj *pTail = 0;
  1.3339 +  sqlite3_stmt *pStmt = 0;
  1.3340 +  char zBuf[50]; 
  1.3341 +  int rc;
  1.3342 +  int bytes;                /* The integer specified as arg 3 */
  1.3343 +  int objlen;               /* The byte-array length of arg 2 */
  1.3344 +
  1.3345 +  if( objc!=5 ){
  1.3346 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3347 +       Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
  1.3348 +    return TCL_ERROR;
  1.3349 +  }
  1.3350 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.3351 +  zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
  1.3352 +  if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
  1.3353 +
  1.3354 +  rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, &zTail);
  1.3355 +  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  1.3356 +  if( rc ){
  1.3357 +    return TCL_ERROR;
  1.3358 +  }
  1.3359 +
  1.3360 +  if( zTail ){
  1.3361 +    objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
  1.3362 +  }else{
  1.3363 +    objlen = 0;
  1.3364 +  }
  1.3365 +  pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
  1.3366 +  Tcl_IncrRefCount(pTail);
  1.3367 +  Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
  1.3368 +  Tcl_DecrRefCount(pTail);
  1.3369 +
  1.3370 +  if( pStmt ){
  1.3371 +    if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
  1.3372 +  }
  1.3373 +  Tcl_AppendResult(interp, zBuf, 0);
  1.3374 +#endif /* SQLITE_OMIT_UTF16 */
  1.3375 +  return TCL_OK;
  1.3376 +}
  1.3377 +
  1.3378 +/*
  1.3379 +** Usage: sqlite3_open filename ?options-list?
  1.3380 +*/
  1.3381 +static int test_open(
  1.3382 +  void * clientData,
  1.3383 +  Tcl_Interp *interp,
  1.3384 +  int objc,
  1.3385 +  Tcl_Obj *CONST objv[]
  1.3386 +){
  1.3387 +  const char *zFilename;
  1.3388 +  sqlite3 *db;
  1.3389 +  int rc;
  1.3390 +  char zBuf[100];
  1.3391 +
  1.3392 +  if( objc!=3 && objc!=2 && objc!=1 ){
  1.3393 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3394 +       Tcl_GetString(objv[0]), " filename options-list", 0);
  1.3395 +    return TCL_ERROR;
  1.3396 +  }
  1.3397 +
  1.3398 +  zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0;
  1.3399 +  rc = sqlite3_open(zFilename, &db);
  1.3400 +  
  1.3401 +  if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
  1.3402 +  Tcl_AppendResult(interp, zBuf, 0);
  1.3403 +  return TCL_OK;
  1.3404 +}
  1.3405 +
  1.3406 +/*
  1.3407 +** Usage: sqlite3_open16 filename options
  1.3408 +*/
  1.3409 +static int test_open16(
  1.3410 +  void * clientData,
  1.3411 +  Tcl_Interp *interp,
  1.3412 +  int objc,
  1.3413 +  Tcl_Obj *CONST objv[]
  1.3414 +){
  1.3415 +#ifndef SQLITE_OMIT_UTF16
  1.3416 +  const void *zFilename;
  1.3417 +  sqlite3 *db;
  1.3418 +  int rc;
  1.3419 +  char zBuf[100];
  1.3420 +
  1.3421 +  if( objc!=3 ){
  1.3422 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3423 +       Tcl_GetString(objv[0]), " filename options-list", 0);
  1.3424 +    return TCL_ERROR;
  1.3425 +  }
  1.3426 +
  1.3427 +  zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
  1.3428 +  rc = sqlite3_open16(zFilename, &db);
  1.3429 +  
  1.3430 +  if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
  1.3431 +  Tcl_AppendResult(interp, zBuf, 0);
  1.3432 +#endif /* SQLITE_OMIT_UTF16 */
  1.3433 +  return TCL_OK;
  1.3434 +}
  1.3435 +
  1.3436 +/*
  1.3437 +** Usage: sqlite3_complete16 <UTF-16 string>
  1.3438 +**
  1.3439 +** Return 1 if the supplied argument is a complete SQL statement, or zero
  1.3440 +** otherwise.
  1.3441 +*/
  1.3442 +static int test_complete16(
  1.3443 +  void * clientData,
  1.3444 +  Tcl_Interp *interp,
  1.3445 +  int objc,
  1.3446 +  Tcl_Obj *CONST objv[]
  1.3447 +){
  1.3448 +#if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
  1.3449 +  char *zBuf;
  1.3450 +
  1.3451 +  if( objc!=2 ){
  1.3452 +    Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
  1.3453 +    return TCL_ERROR;
  1.3454 +  }
  1.3455 +
  1.3456 +  zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0);
  1.3457 +  Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
  1.3458 +#endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
  1.3459 +  return TCL_OK;
  1.3460 +}
  1.3461 +
  1.3462 +/*
  1.3463 +** Usage: sqlite3_step STMT
  1.3464 +**
  1.3465 +** Advance the statement to the next row.
  1.3466 +*/
  1.3467 +static int test_step(
  1.3468 +  void * clientData,
  1.3469 +  Tcl_Interp *interp,
  1.3470 +  int objc,
  1.3471 +  Tcl_Obj *CONST objv[]
  1.3472 +){
  1.3473 +  sqlite3_stmt *pStmt;
  1.3474 +  int rc;
  1.3475 +
  1.3476 +  if( objc!=2 ){
  1.3477 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3478 +       Tcl_GetString(objv[0]), " STMT", 0);
  1.3479 +    return TCL_ERROR;
  1.3480 +  }
  1.3481 +
  1.3482 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.3483 +  rc = sqlite3_step(pStmt);
  1.3484 +
  1.3485 +  /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
  1.3486 +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
  1.3487 +  return TCL_OK;
  1.3488 +}
  1.3489 +
  1.3490 +/*
  1.3491 +** Usage: sqlite3_column_count STMT 
  1.3492 +**
  1.3493 +** Return the number of columns returned by the sql statement STMT.
  1.3494 +*/
  1.3495 +static int test_column_count(
  1.3496 +  void * clientData,
  1.3497 +  Tcl_Interp *interp,
  1.3498 +  int objc,
  1.3499 +  Tcl_Obj *CONST objv[]
  1.3500 +){
  1.3501 +  sqlite3_stmt *pStmt;
  1.3502 +
  1.3503 +  if( objc!=2 ){
  1.3504 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3505 +       Tcl_GetString(objv[0]), " STMT column", 0);
  1.3506 +    return TCL_ERROR;
  1.3507 +  }
  1.3508 +
  1.3509 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.3510 +
  1.3511 +  Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
  1.3512 +  return TCL_OK;
  1.3513 +}
  1.3514 +
  1.3515 +/*
  1.3516 +** Usage: sqlite3_column_type STMT column
  1.3517 +**
  1.3518 +** Return the type of the data in column 'column' of the current row.
  1.3519 +*/
  1.3520 +static int test_column_type(
  1.3521 +  void * clientData,
  1.3522 +  Tcl_Interp *interp,
  1.3523 +  int objc,
  1.3524 +  Tcl_Obj *CONST objv[]
  1.3525 +){
  1.3526 +  sqlite3_stmt *pStmt;
  1.3527 +  int col;
  1.3528 +  int tp;
  1.3529 +
  1.3530 +  if( objc!=3 ){
  1.3531 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3532 +       Tcl_GetString(objv[0]), " STMT column", 0);
  1.3533 +    return TCL_ERROR;
  1.3534 +  }
  1.3535 +
  1.3536 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.3537 +  if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  1.3538 +
  1.3539 +  tp = sqlite3_column_type(pStmt, col);
  1.3540 +  switch( tp ){
  1.3541 +    case SQLITE_INTEGER: 
  1.3542 +      Tcl_SetResult(interp, "INTEGER", TCL_STATIC); 
  1.3543 +      break;
  1.3544 +    case SQLITE_NULL:
  1.3545 +      Tcl_SetResult(interp, "NULL", TCL_STATIC); 
  1.3546 +      break;
  1.3547 +    case SQLITE_FLOAT:
  1.3548 +      Tcl_SetResult(interp, "FLOAT", TCL_STATIC); 
  1.3549 +      break;
  1.3550 +    case SQLITE_TEXT:
  1.3551 +      Tcl_SetResult(interp, "TEXT", TCL_STATIC); 
  1.3552 +      break;
  1.3553 +    case SQLITE_BLOB:
  1.3554 +      Tcl_SetResult(interp, "BLOB", TCL_STATIC); 
  1.3555 +      break;
  1.3556 +    default:
  1.3557 +      assert(0);
  1.3558 +  }
  1.3559 +
  1.3560 +  return TCL_OK;
  1.3561 +}
  1.3562 +
  1.3563 +/*
  1.3564 +** Usage: sqlite3_column_int64 STMT column
  1.3565 +**
  1.3566 +** Return the data in column 'column' of the current row cast as an
  1.3567 +** wide (64-bit) integer.
  1.3568 +*/
  1.3569 +static int test_column_int64(
  1.3570 +  void * clientData,
  1.3571 +  Tcl_Interp *interp,
  1.3572 +  int objc,
  1.3573 +  Tcl_Obj *CONST objv[]
  1.3574 +){
  1.3575 +  sqlite3_stmt *pStmt;
  1.3576 +  int col;
  1.3577 +  i64 iVal;
  1.3578 +
  1.3579 +  if( objc!=3 ){
  1.3580 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3581 +       Tcl_GetString(objv[0]), " STMT column", 0);
  1.3582 +    return TCL_ERROR;
  1.3583 +  }
  1.3584 +
  1.3585 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.3586 +  if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  1.3587 +
  1.3588 +  iVal = sqlite3_column_int64(pStmt, col);
  1.3589 +  Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
  1.3590 +  return TCL_OK;
  1.3591 +}
  1.3592 +
  1.3593 +/*
  1.3594 +** Usage: sqlite3_column_blob STMT column
  1.3595 +*/
  1.3596 +static int test_column_blob(
  1.3597 +  void * clientData,
  1.3598 +  Tcl_Interp *interp,
  1.3599 +  int objc,
  1.3600 +  Tcl_Obj *CONST objv[]
  1.3601 +){
  1.3602 +  sqlite3_stmt *pStmt;
  1.3603 +  int col;
  1.3604 +
  1.3605 +  int len;
  1.3606 +  const void *pBlob;
  1.3607 +
  1.3608 +  if( objc!=3 ){
  1.3609 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3610 +       Tcl_GetString(objv[0]), " STMT column", 0);
  1.3611 +    return TCL_ERROR;
  1.3612 +  }
  1.3613 +
  1.3614 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.3615 +  if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  1.3616 +
  1.3617 +  len = sqlite3_column_bytes(pStmt, col);
  1.3618 +  pBlob = sqlite3_column_blob(pStmt, col);
  1.3619 +  Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
  1.3620 +  return TCL_OK;
  1.3621 +}
  1.3622 +
  1.3623 +/*
  1.3624 +** Usage: sqlite3_column_double STMT column
  1.3625 +**
  1.3626 +** Return the data in column 'column' of the current row cast as a double.
  1.3627 +*/
  1.3628 +static int test_column_double(
  1.3629 +  void * clientData,
  1.3630 +  Tcl_Interp *interp,
  1.3631 +  int objc,
  1.3632 +  Tcl_Obj *CONST objv[]
  1.3633 +){
  1.3634 +  sqlite3_stmt *pStmt;
  1.3635 +  int col;
  1.3636 +  double rVal;
  1.3637 +
  1.3638 +  if( objc!=3 ){
  1.3639 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3640 +       Tcl_GetString(objv[0]), " STMT column", 0);
  1.3641 +    return TCL_ERROR;
  1.3642 +  }
  1.3643 +
  1.3644 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.3645 +  if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  1.3646 +
  1.3647 +  rVal = sqlite3_column_double(pStmt, col);
  1.3648 +  Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
  1.3649 +  return TCL_OK;
  1.3650 +}
  1.3651 +
  1.3652 +/*
  1.3653 +** Usage: sqlite3_data_count STMT 
  1.3654 +**
  1.3655 +** Return the number of columns returned by the sql statement STMT.
  1.3656 +*/
  1.3657 +static int test_data_count(
  1.3658 +  void * clientData,
  1.3659 +  Tcl_Interp *interp,
  1.3660 +  int objc,
  1.3661 +  Tcl_Obj *CONST objv[]
  1.3662 +){
  1.3663 +  sqlite3_stmt *pStmt;
  1.3664 +
  1.3665 +  if( objc!=2 ){
  1.3666 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3667 +       Tcl_GetString(objv[0]), " STMT column", 0);
  1.3668 +    return TCL_ERROR;
  1.3669 +  }
  1.3670 +
  1.3671 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.3672 +
  1.3673 +  Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
  1.3674 +  return TCL_OK;
  1.3675 +}
  1.3676 +
  1.3677 +/*
  1.3678 +** Usage: sqlite3_column_text STMT column
  1.3679 +**
  1.3680 +** Usage: sqlite3_column_decltype STMT column
  1.3681 +**
  1.3682 +** Usage: sqlite3_column_name STMT column
  1.3683 +*/
  1.3684 +static int test_stmt_utf8(
  1.3685 +  void * clientData,        /* Pointer to SQLite API function to be invoke */
  1.3686 +  Tcl_Interp *interp,
  1.3687 +  int objc,
  1.3688 +  Tcl_Obj *CONST objv[]
  1.3689 +){
  1.3690 +  sqlite3_stmt *pStmt;
  1.3691 +  int col;
  1.3692 +  const char *(*xFunc)(sqlite3_stmt*, int);
  1.3693 +  const char *zRet;
  1.3694 +
  1.3695 +  xFunc = (const char *(*)(sqlite3_stmt*, int))clientData;
  1.3696 +  if( objc!=3 ){
  1.3697 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3698 +       Tcl_GetString(objv[0]), " STMT column", 0);
  1.3699 +    return TCL_ERROR;
  1.3700 +  }
  1.3701 +
  1.3702 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.3703 +  if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  1.3704 +  zRet = xFunc(pStmt, col);
  1.3705 +  if( zRet ){
  1.3706 +    Tcl_SetResult(interp, (char *)zRet, 0);
  1.3707 +  }
  1.3708 +  return TCL_OK;
  1.3709 +}
  1.3710 +
  1.3711 +static int test_global_recover(
  1.3712 +  void * clientData,
  1.3713 +  Tcl_Interp *interp,
  1.3714 +  int objc,
  1.3715 +  Tcl_Obj *CONST objv[]
  1.3716 +){
  1.3717 +#ifndef SQLITE_OMIT_GLOBALRECOVER
  1.3718 +  int rc;
  1.3719 +  if( objc!=1 ){
  1.3720 +    Tcl_WrongNumArgs(interp, 1, objv, "");
  1.3721 +    return TCL_ERROR;
  1.3722 +  }
  1.3723 +  rc = sqlite3_global_recover();
  1.3724 +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  1.3725 +#endif
  1.3726 +  return TCL_OK;
  1.3727 +}
  1.3728 +
  1.3729 +/*
  1.3730 +** Usage: sqlite3_column_text STMT column
  1.3731 +**
  1.3732 +** Usage: sqlite3_column_decltype STMT column
  1.3733 +**
  1.3734 +** Usage: sqlite3_column_name STMT column
  1.3735 +*/
  1.3736 +static int test_stmt_utf16(
  1.3737 +  void * clientData,     /* Pointer to SQLite API function to be invoked */
  1.3738 +  Tcl_Interp *interp,
  1.3739 +  int objc,
  1.3740 +  Tcl_Obj *CONST objv[]
  1.3741 +){
  1.3742 +#ifndef SQLITE_OMIT_UTF16
  1.3743 +  sqlite3_stmt *pStmt;
  1.3744 +  int col;
  1.3745 +  Tcl_Obj *pRet;
  1.3746 +  const void *zName16;
  1.3747 +  const void *(*xFunc)(sqlite3_stmt*, int);
  1.3748 +
  1.3749 +  xFunc = (const void *(*)(sqlite3_stmt*, int))clientData;
  1.3750 +  if( objc!=3 ){
  1.3751 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3752 +       Tcl_GetString(objv[0]), " STMT column", 0);
  1.3753 +    return TCL_ERROR;
  1.3754 +  }
  1.3755 +
  1.3756 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.3757 +  if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  1.3758 +
  1.3759 +  zName16 = xFunc(pStmt, col);
  1.3760 +  if( zName16 ){
  1.3761 +    pRet = Tcl_NewByteArrayObj(zName16, sqlite3Utf16ByteLen(zName16, -1)+2);
  1.3762 +    Tcl_SetObjResult(interp, pRet);
  1.3763 +  }
  1.3764 +#endif /* SQLITE_OMIT_UTF16 */
  1.3765 +
  1.3766 +  return TCL_OK;
  1.3767 +}
  1.3768 +
  1.3769 +/*
  1.3770 +** Usage: sqlite3_column_int STMT column
  1.3771 +**
  1.3772 +** Usage: sqlite3_column_bytes STMT column
  1.3773 +**
  1.3774 +** Usage: sqlite3_column_bytes16 STMT column
  1.3775 +**
  1.3776 +*/
  1.3777 +static int test_stmt_int(
  1.3778 +  void * clientData,    /* Pointer to SQLite API function to be invoked */
  1.3779 +  Tcl_Interp *interp,
  1.3780 +  int objc,
  1.3781 +  Tcl_Obj *CONST objv[]
  1.3782 +){
  1.3783 +  sqlite3_stmt *pStmt;
  1.3784 +  int col;
  1.3785 +  int (*xFunc)(sqlite3_stmt*, int);
  1.3786 +
  1.3787 +  xFunc = (int (*)(sqlite3_stmt*, int))clientData;
  1.3788 +  if( objc!=3 ){
  1.3789 +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
  1.3790 +       Tcl_GetString(objv[0]), " STMT column", 0);
  1.3791 +    return TCL_ERROR;
  1.3792 +  }
  1.3793 +
  1.3794 +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1.3795 +  if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  1.3796 +
  1.3797 +  Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
  1.3798 +  return TCL_OK;
  1.3799 +}
  1.3800 +
  1.3801 +/*
  1.3802 +** Usage:  sqlite_set_magic  DB  MAGIC-NUMBER
  1.3803 +**
  1.3804 +** Set the db->magic value.  This is used to test error recovery logic.
  1.3805 +*/
  1.3806 +static int sqlite_set_magic(
  1.3807 +  void * clientData,
  1.3808 +  Tcl_Interp *interp,
  1.3809 +  int argc,
  1.3810 +  char **argv
  1.3811 +){
  1.3812 +  sqlite3 *db;
  1.3813 +  if( argc!=3 ){
  1.3814 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1.3815 +         " DB MAGIC", 0);
  1.3816 +    return TCL_ERROR;
  1.3817 +  }
  1.3818 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  1.3819 +  if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){
  1.3820 +    db->magic = SQLITE_MAGIC_OPEN;
  1.3821 +  }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){
  1.3822 +    db->magic = SQLITE_MAGIC_CLOSED;
  1.3823 +  }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){
  1.3824 +    db->magic = SQLITE_MAGIC_BUSY;
  1.3825 +  }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){
  1.3826 +    db->magic = SQLITE_MAGIC_ERROR;
  1.3827 +  }else if( Tcl_GetInt(interp, argv[2], &db->magic) ){
  1.3828 +    return TCL_ERROR;
  1.3829 +  }
  1.3830 +  return TCL_OK;
  1.3831 +}
  1.3832 +
  1.3833 +/*
  1.3834 +** Usage:  sqlite3_interrupt  DB 
  1.3835 +**
  1.3836 +** Trigger an interrupt on DB
  1.3837 +*/
  1.3838 +static int test_interrupt(
  1.3839 +  void * clientData,
  1.3840 +  Tcl_Interp *interp,
  1.3841 +  int argc,
  1.3842 +  char **argv
  1.3843 +){
  1.3844 +  sqlite3 *db;
  1.3845 +  if( argc!=2 ){
  1.3846 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
  1.3847 +    return TCL_ERROR;
  1.3848 +  }
  1.3849 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  1.3850 +  sqlite3_interrupt(db);
  1.3851 +  return TCL_OK;
  1.3852 +}
  1.3853 +
  1.3854 +static u8 *sqlite3_stack_baseline = 0;
  1.3855 +
  1.3856 +/*
  1.3857 +** Fill the stack with a known bitpattern.
  1.3858 +*/
  1.3859 +static void prepStack(void){
  1.3860 +  int i;
  1.3861 +  u32 bigBuf[65536];
  1.3862 +  for(i=0; i<sizeof(bigBuf); i++) bigBuf[i] = 0xdeadbeef;
  1.3863 +  sqlite3_stack_baseline = (u8*)&bigBuf[65536];
  1.3864 +}
  1.3865 +
  1.3866 +/*
  1.3867 +** Get the current stack depth.  Used for debugging only.
  1.3868 +*/
  1.3869 +u64 sqlite3StackDepth(void){
  1.3870 +  u8 x;
  1.3871 +  return (u64)(sqlite3_stack_baseline - &x);
  1.3872 +}
  1.3873 +
  1.3874 +/*
  1.3875 +** Usage:  sqlite3_stack_used DB SQL
  1.3876 +**
  1.3877 +** Try to measure the amount of stack space used by a call to sqlite3_exec
  1.3878 +*/
  1.3879 +static int test_stack_used(
  1.3880 +  void * clientData,
  1.3881 +  Tcl_Interp *interp,
  1.3882 +  int argc,
  1.3883 +  char **argv
  1.3884 +){
  1.3885 +  sqlite3 *db;
  1.3886 +  int i;
  1.3887 +  if( argc!=3 ){
  1.3888 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
  1.3889 +        " DB SQL", 0);
  1.3890 +    return TCL_ERROR;
  1.3891 +  }
  1.3892 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  1.3893 +  prepStack();
  1.3894 +  (void)sqlite3_exec(db, argv[2], 0, 0, 0);
  1.3895 +  for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){}
  1.3896 +  Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4));
  1.3897 +  return TCL_OK;
  1.3898 +}
  1.3899 +
  1.3900 +/*
  1.3901 +** Usage: sqlite_delete_function DB function-name
  1.3902 +**
  1.3903 +** Delete the user function 'function-name' from database handle DB. It
  1.3904 +** is assumed that the user function was created as UTF8, any number of
  1.3905 +** arguments (the way the TCL interface does it).
  1.3906 +*/
  1.3907 +static int delete_function(
  1.3908 +  void * clientData,
  1.3909 +  Tcl_Interp *interp,
  1.3910 +  int argc,
  1.3911 +  char **argv
  1.3912 +){
  1.3913 +  int rc;
  1.3914 +  sqlite3 *db;
  1.3915 +  if( argc!=3 ){
  1.3916 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
  1.3917 +        " DB function-name", 0);
  1.3918 +    return TCL_ERROR;
  1.3919 +  }
  1.3920 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  1.3921 +  rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
  1.3922 +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  1.3923 +  return TCL_OK;
  1.3924 +}
  1.3925 +
  1.3926 +/*
  1.3927 +** Usage: sqlite_delete_collation DB collation-name
  1.3928 +**
  1.3929 +** Delete the collation sequence 'collation-name' from database handle 
  1.3930 +** DB. It is assumed that the collation sequence was created as UTF8 (the 
  1.3931 +** way the TCL interface does it).
  1.3932 +*/
  1.3933 +static int delete_collation(
  1.3934 +  void * clientData,
  1.3935 +  Tcl_Interp *interp,
  1.3936 +  int argc,
  1.3937 +  char **argv
  1.3938 +){
  1.3939 +  int rc;
  1.3940 +  sqlite3 *db;
  1.3941 +  if( argc!=3 ){
  1.3942 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
  1.3943 +        " DB function-name", 0);
  1.3944 +    return TCL_ERROR;
  1.3945 +  }
  1.3946 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  1.3947 +  rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
  1.3948 +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  1.3949 +  return TCL_OK;
  1.3950 +}
  1.3951 +
  1.3952 +/*
  1.3953 +** Usage: sqlite3_get_autocommit DB
  1.3954 +**
  1.3955 +** Return true if the database DB is currently in auto-commit mode.
  1.3956 +** Return false if not.
  1.3957 +*/
  1.3958 +static int get_autocommit(
  1.3959 +  void * clientData,
  1.3960 +  Tcl_Interp *interp,
  1.3961 +  int argc,
  1.3962 +  char **argv
  1.3963 +){
  1.3964 +  char zBuf[30];
  1.3965 +  sqlite3 *db;
  1.3966 +  if( argc!=2 ){
  1.3967 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
  1.3968 +        " DB", 0);
  1.3969 +    return TCL_ERROR;
  1.3970 +  }
  1.3971 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  1.3972 +  sprintf(zBuf, "%d", sqlite3_get_autocommit(db));
  1.3973 +  Tcl_AppendResult(interp, zBuf, 0);
  1.3974 +  return TCL_OK;
  1.3975 +}
  1.3976 +
  1.3977 +/*
  1.3978 +** Usage: sqlite3_busy_timeout DB MS
  1.3979 +**
  1.3980 +** Set the busy timeout.  This is more easily done using the timeout
  1.3981 +** method of the TCL interface.  But we need a way to test the case
  1.3982 +** where it returns SQLITE_MISUSE.
  1.3983 +*/
  1.3984 +static int test_busy_timeout(
  1.3985 +  void * clientData,
  1.3986 +  Tcl_Interp *interp,
  1.3987 +  int argc,
  1.3988 +  char **argv
  1.3989 +){
  1.3990 +  int rc, ms;
  1.3991 +  sqlite3 *db;
  1.3992 +  if( argc!=3 ){
  1.3993 +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
  1.3994 +        " DB", 0);
  1.3995 +    return TCL_ERROR;
  1.3996 +  }
  1.3997 +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  1.3998 +  if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
  1.3999 +  rc = sqlite3_busy_timeout(db, ms);
  1.4000 +  Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
  1.4001 +  return TCL_OK;
  1.4002 +}
  1.4003 +
  1.4004 +/*
  1.4005 +** Usage:  tcl_variable_type VARIABLENAME
  1.4006 +**
  1.4007 +** Return the name of the internal representation for the
  1.4008 +** value of the given variable.
  1.4009 +*/
  1.4010 +static int tcl_variable_type(
  1.4011 +  void * clientData,
  1.4012 +  Tcl_Interp *interp,
  1.4013 +  int objc,
  1.4014 +  Tcl_Obj *CONST objv[]
  1.4015 +){
  1.4016 +  Tcl_Obj *pVar;
  1.4017 +  if( objc!=2 ){
  1.4018 +    Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
  1.4019 +    return TCL_ERROR;
  1.4020 +  }
  1.4021 +  pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
  1.4022 +  if( pVar==0 ) return TCL_ERROR;
  1.4023 +  if( pVar->typePtr ){
  1.4024 +    Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
  1.4025 +  }
  1.4026 +  return TCL_OK;
  1.4027 +}
  1.4028 +
  1.4029 +/*
  1.4030 +** Usage:  sqlite3_release_memory ?N?
  1.4031 +**
  1.4032 +** Attempt to release memory currently held but not actually required.
  1.4033 +** The integer N is the number of bytes we are trying to release.  The 
  1.4034 +** return value is the amount of memory actually released.
  1.4035 +*/
  1.4036 +static int test_release_memory(
  1.4037 +  void * clientData,
  1.4038 +  Tcl_Interp *interp,
  1.4039 +  int objc,
  1.4040 +  Tcl_Obj *CONST objv[]
  1.4041 +){
  1.4042 +#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
  1.4043 +  int N;
  1.4044 +  int amt;
  1.4045 +  if( objc!=1 && objc!=2 ){
  1.4046 +    Tcl_WrongNumArgs(interp, 1, objv, "?N?");
  1.4047 +    return TCL_ERROR;
  1.4048 +  }
  1.4049 +  if( objc==2 ){
  1.4050 +    if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
  1.4051 +  }else{
  1.4052 +    N = -1;
  1.4053 +  }
  1.4054 +  amt = sqlite3_release_memory(N);
  1.4055 +  Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
  1.4056 +#endif
  1.4057 +  return TCL_OK;
  1.4058 +}
  1.4059 +
  1.4060 +/*
  1.4061 +** Usage:  sqlite3_soft_heap_limit ?N?
  1.4062 +**
  1.4063 +** Query or set the soft heap limit for the current thread.  The
  1.4064 +** limit is only changed if the N is present.  The previous limit
  1.4065 +** is returned.
  1.4066 +*/
  1.4067 +static int test_soft_heap_limit(
  1.4068 +  void * clientData,
  1.4069 +  Tcl_Interp *interp,
  1.4070 +  int objc,
  1.4071 +  Tcl_Obj *CONST objv[]
  1.4072 +){
  1.4073 +  static int softHeapLimit = 0;
  1.4074 +  int amt;
  1.4075 +  if( objc!=1 && objc!=2 ){
  1.4076 +    Tcl_WrongNumArgs(interp, 1, objv, "?N?");
  1.4077 +    return TCL_ERROR;
  1.4078 +  }
  1.4079 +  amt = softHeapLimit;
  1.4080 +  if( objc==2 ){
  1.4081 +    int N;
  1.4082 +    if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
  1.4083 +    sqlite3_soft_heap_limit(N);
  1.4084 +    softHeapLimit = N;
  1.4085 +  }
  1.4086 +  Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
  1.4087 +  return TCL_OK;
  1.4088 +}
  1.4089 +
  1.4090 +/*
  1.4091 +** Usage:   sqlite3_thread_cleanup
  1.4092 +**
  1.4093 +** Call the sqlite3_thread_cleanup API.
  1.4094 +*/
  1.4095 +static int test_thread_cleanup(
  1.4096 +  void * clientData,
  1.4097 +  Tcl_Interp *interp,
  1.4098 +  int objc,
  1.4099 +  Tcl_Obj *CONST objv[]
  1.4100 +){
  1.4101 +  sqlite3_thread_cleanup();
  1.4102 +  return TCL_OK;
  1.4103 +}
  1.4104 +
  1.4105 +
  1.4106 +/*
  1.4107 +** Usage:   sqlite3_pager_refcounts  DB
  1.4108 +**
  1.4109 +** Return a list of numbers which are the PagerRefcount for all
  1.4110 +** pagers on each database connection.
  1.4111 +*/
  1.4112 +static int test_pager_refcounts(
  1.4113 +  void * clientData,
  1.4114 +  Tcl_Interp *interp,
  1.4115 +  int objc,
  1.4116 +  Tcl_Obj *CONST objv[]
  1.4117 +){
  1.4118 +  sqlite3 *db;
  1.4119 +  int i;
  1.4120 +  int v, *a;
  1.4121 +  Tcl_Obj *pResult;
  1.4122 +
  1.4123 +  if( objc!=2 ){
  1.4124 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.4125 +        Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
  1.4126 +    return TCL_ERROR;
  1.4127 +  }
  1.4128 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.4129 +  pResult = Tcl_NewObj();
  1.4130 +  for(i=0; i<db->nDb; i++){
  1.4131 +    if( db->aDb[i].pBt==0 ){
  1.4132 +      v = -1;
  1.4133 +    }else{
  1.4134 +      sqlite3_mutex_enter(db->mutex);
  1.4135 +      a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt));
  1.4136 +      v = a[0];
  1.4137 +      sqlite3_mutex_leave(db->mutex);
  1.4138 +    }
  1.4139 +    Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v));
  1.4140 +  }
  1.4141 +  Tcl_SetObjResult(interp, pResult);
  1.4142 +  return TCL_OK;
  1.4143 +}
  1.4144 +
  1.4145 +
  1.4146 +/*
  1.4147 +** tclcmd:   working_64bit_int
  1.4148 +**
  1.4149 +** Some TCL builds (ex: cygwin) do not support 64-bit integers.  This
  1.4150 +** leads to a number of test failures.  The present command checks the
  1.4151 +** TCL build to see whether or not it supports 64-bit integers.  It
  1.4152 +** returns TRUE if it does and FALSE if not.
  1.4153 +**
  1.4154 +** This command is used to warn users that their TCL build is defective
  1.4155 +** and that the errors they are seeing in the test scripts might be
  1.4156 +** a result of their defective TCL rather than problems in SQLite.
  1.4157 +*/
  1.4158 +static int working_64bit_int(
  1.4159 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.4160 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.4161 +  int objc,              /* Number of arguments */
  1.4162 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.4163 +){
  1.4164 +  Tcl_Obj *pTestObj;
  1.4165 +  int working = 0;
  1.4166 +
  1.4167 +  pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890);
  1.4168 +  working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0;
  1.4169 +  Tcl_DecrRefCount(pTestObj);
  1.4170 +  Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working));
  1.4171 +  return TCL_OK;
  1.4172 +}
  1.4173 +
  1.4174 +
  1.4175 +/*
  1.4176 +** tclcmd:   vfs_unlink_test
  1.4177 +**
  1.4178 +** This TCL command unregisters the primary VFS and then registers
  1.4179 +** it back again.  This is used to test the ability to register a
  1.4180 +** VFS when none are previously registered, and the ability to 
  1.4181 +** unregister the only available VFS.  Ticket #2738
  1.4182 +*/
  1.4183 +static int vfs_unlink_test(
  1.4184 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.4185 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.4186 +  int objc,              /* Number of arguments */
  1.4187 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.4188 +){
  1.4189 +  int i;
  1.4190 +  sqlite3_vfs *pMain;
  1.4191 +  sqlite3_vfs *apVfs[20];
  1.4192 +  sqlite3_vfs one, two;
  1.4193 +
  1.4194 +  sqlite3_vfs_unregister(0);   /* Unregister of NULL is harmless */
  1.4195 +  one.zName = "__one";
  1.4196 +  two.zName = "__two";
  1.4197 +
  1.4198 +  /* Calling sqlite3_vfs_register with 2nd argument of 0 does not
  1.4199 +  ** change the default VFS
  1.4200 +  */
  1.4201 +  pMain = sqlite3_vfs_find(0);
  1.4202 +  sqlite3_vfs_register(&one, 0);
  1.4203 +  assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
  1.4204 +  sqlite3_vfs_register(&two, 0);
  1.4205 +  assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
  1.4206 +
  1.4207 +  /* We can find a VFS by its name */
  1.4208 +  assert( sqlite3_vfs_find("__one")==&one );
  1.4209 +  assert( sqlite3_vfs_find("__two")==&two );
  1.4210 +
  1.4211 +  /* Calling sqlite_vfs_register with non-zero second parameter changes the
  1.4212 +  ** default VFS, even if the 1st parameter is an existig VFS that is
  1.4213 +  ** previously registered as the non-default.
  1.4214 +  */
  1.4215 +  sqlite3_vfs_register(&one, 1);
  1.4216 +  assert( sqlite3_vfs_find("__one")==&one );
  1.4217 +  assert( sqlite3_vfs_find("__two")==&two );
  1.4218 +  assert( sqlite3_vfs_find(0)==&one );
  1.4219 +  sqlite3_vfs_register(&two, 1);
  1.4220 +  assert( sqlite3_vfs_find("__one")==&one );
  1.4221 +  assert( sqlite3_vfs_find("__two")==&two );
  1.4222 +  assert( sqlite3_vfs_find(0)==&two );
  1.4223 +  if( pMain ){
  1.4224 +    sqlite3_vfs_register(pMain, 1);
  1.4225 +    assert( sqlite3_vfs_find("__one")==&one );
  1.4226 +    assert( sqlite3_vfs_find("__two")==&two );
  1.4227 +    assert( sqlite3_vfs_find(0)==pMain );
  1.4228 +  }
  1.4229 +  
  1.4230 +  /* Unlink the default VFS.  Repeat until there are no more VFSes
  1.4231 +  ** registered.
  1.4232 +  */
  1.4233 +  for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){
  1.4234 +    apVfs[i] = sqlite3_vfs_find(0);
  1.4235 +    if( apVfs[i] ){
  1.4236 +      assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
  1.4237 +      sqlite3_vfs_unregister(apVfs[i]);
  1.4238 +      assert( 0==sqlite3_vfs_find(apVfs[i]->zName) );
  1.4239 +    }
  1.4240 +  }
  1.4241 +  assert( 0==sqlite3_vfs_find(0) );
  1.4242 +  
  1.4243 +  /* Register the main VFS as non-default (will be made default, since
  1.4244 +  ** it'll be the only one in existence).
  1.4245 +  */
  1.4246 +  sqlite3_vfs_register(pMain, 0);
  1.4247 +  assert( sqlite3_vfs_find(0)==pMain );
  1.4248 +  
  1.4249 +  /* Un-register the main VFS again to restore an empty VFS list */
  1.4250 +  sqlite3_vfs_unregister(pMain);
  1.4251 +  assert( 0==sqlite3_vfs_find(0) );
  1.4252 +
  1.4253 +  /* Relink all VFSes in reverse order. */  
  1.4254 +  for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){
  1.4255 +    if( apVfs[i] ){
  1.4256 +      sqlite3_vfs_register(apVfs[i], 1);
  1.4257 +      assert( apVfs[i]==sqlite3_vfs_find(0) );
  1.4258 +      assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
  1.4259 +    }
  1.4260 +  }
  1.4261 +
  1.4262 +  /* Unregister out sample VFSes. */
  1.4263 +  sqlite3_vfs_unregister(&one);
  1.4264 +  sqlite3_vfs_unregister(&two);
  1.4265 +
  1.4266 +  /* Unregistering a VFS that is not currently registered is harmless */
  1.4267 +  sqlite3_vfs_unregister(&one);
  1.4268 +  sqlite3_vfs_unregister(&two);
  1.4269 +  assert( sqlite3_vfs_find("__one")==0 );
  1.4270 +  assert( sqlite3_vfs_find("__two")==0 );
  1.4271 +
  1.4272 +  /* We should be left with the original default VFS back as the
  1.4273 +  ** original */
  1.4274 +  assert( sqlite3_vfs_find(0)==pMain );
  1.4275 +
  1.4276 +  return TCL_OK;
  1.4277 +}
  1.4278 +
  1.4279 +/*
  1.4280 +** tclcmd:   vfs_initfail_test
  1.4281 +**
  1.4282 +** This TCL command attempts to vfs_find and vfs_register when the
  1.4283 +** sqlite3_initialize() interface is failing.  All calls should fail.
  1.4284 +*/
  1.4285 +static int vfs_initfail_test(
  1.4286 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.4287 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.4288 +  int objc,              /* Number of arguments */
  1.4289 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.4290 +){
  1.4291 +  sqlite3_vfs one;
  1.4292 +  one.zName = "__one";
  1.4293 +
  1.4294 +  if( sqlite3_vfs_find(0) ) return TCL_ERROR;
  1.4295 +  sqlite3_vfs_register(&one, 0);
  1.4296 +  if( sqlite3_vfs_find(0) ) return TCL_ERROR;
  1.4297 +  sqlite3_vfs_register(&one, 1);
  1.4298 +  if( sqlite3_vfs_find(0) ) return TCL_ERROR;
  1.4299 +  return TCL_OK;
  1.4300 +}
  1.4301 +
  1.4302 +/*
  1.4303 +** Saved VFSes
  1.4304 +*/
  1.4305 +static sqlite3_vfs *apVfs[20];
  1.4306 +static int nVfs = 0;
  1.4307 +
  1.4308 +/*
  1.4309 +** tclcmd:   vfs_unregister_all
  1.4310 +**
  1.4311 +** Unregister all VFSes.
  1.4312 +*/
  1.4313 +static int vfs_unregister_all(
  1.4314 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.4315 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.4316 +  int objc,              /* Number of arguments */
  1.4317 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.4318 +){
  1.4319 +  int i;
  1.4320 +  for(i=0; i<ArraySize(apVfs); i++){
  1.4321 +    apVfs[i] = sqlite3_vfs_find(0);
  1.4322 +    if( apVfs[i]==0 ) break;
  1.4323 +    sqlite3_vfs_unregister(apVfs[i]);
  1.4324 +  }
  1.4325 +  nVfs = i;
  1.4326 +  return TCL_OK;
  1.4327 +}
  1.4328 +/*
  1.4329 +** tclcmd:   vfs_reregister_all
  1.4330 +**
  1.4331 +** Restore all VFSes that were removed using vfs_unregister_all
  1.4332 +*/
  1.4333 +static int vfs_reregister_all(
  1.4334 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.4335 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.4336 +  int objc,              /* Number of arguments */
  1.4337 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.4338 +){
  1.4339 +  int i;
  1.4340 +  for(i=0; i<nVfs; i++){
  1.4341 +    sqlite3_vfs_register(apVfs[i], i==0);
  1.4342 +  }
  1.4343 +  return TCL_OK;
  1.4344 +}
  1.4345 +
  1.4346 +
  1.4347 +/*
  1.4348 +** tclcmd:   file_control_test DB
  1.4349 +**
  1.4350 +** This TCL command runs the sqlite3_file_control interface and
  1.4351 +** verifies correct operation of the same.
  1.4352 +*/
  1.4353 +static int file_control_test(
  1.4354 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.4355 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.4356 +  int objc,              /* Number of arguments */
  1.4357 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.4358 +){
  1.4359 +  int iArg = 0;
  1.4360 +  sqlite3 *db;
  1.4361 +  int rc;
  1.4362 +
  1.4363 +  if( objc!=2 ){
  1.4364 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.4365 +        Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
  1.4366 +    return TCL_ERROR;
  1.4367 +  }
  1.4368 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.4369 +  rc = sqlite3_file_control(db, 0, 0, &iArg);
  1.4370 +  assert( rc==SQLITE_ERROR );
  1.4371 +  rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg);
  1.4372 +  assert( rc==SQLITE_ERROR );
  1.4373 +  rc = sqlite3_file_control(db, "main", -1, &iArg);
  1.4374 +  assert( rc==SQLITE_ERROR );
  1.4375 +  rc = sqlite3_file_control(db, "temp", -1, &iArg);
  1.4376 +  assert( rc==SQLITE_ERROR );
  1.4377 +  return TCL_OK;  
  1.4378 +}
  1.4379 +
  1.4380 +/*
  1.4381 +** tclcmd:   sqlite3_vfs_list
  1.4382 +**
  1.4383 +**   Return a tcl list containing the names of all registered vfs's.
  1.4384 +*/
  1.4385 +static int vfs_list(
  1.4386 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.4387 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.4388 +  int objc,              /* Number of arguments */
  1.4389 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.4390 +){
  1.4391 +  sqlite3_vfs *pVfs;
  1.4392 +  Tcl_Obj *pRet = Tcl_NewObj();
  1.4393 +  if( objc!=1 ){
  1.4394 +    Tcl_WrongNumArgs(interp, 1, objv, "");
  1.4395 +    return TCL_ERROR;
  1.4396 +  }
  1.4397 +  for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
  1.4398 +    Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1));
  1.4399 +  }
  1.4400 +  Tcl_SetObjResult(interp, pRet);
  1.4401 +  return TCL_OK;  
  1.4402 +}
  1.4403 +
  1.4404 +/*
  1.4405 +** tclcmd:   sqlite3_limit DB ID VALUE
  1.4406 +**
  1.4407 +** This TCL command runs the sqlite3_limit interface and
  1.4408 +** verifies correct operation of the same.
  1.4409 +*/
  1.4410 +static int test_limit(
  1.4411 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.4412 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.4413 +  int objc,              /* Number of arguments */
  1.4414 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.4415 +){
  1.4416 +  sqlite3 *db;
  1.4417 +  int rc;
  1.4418 +  static const struct {
  1.4419 +     char *zName;
  1.4420 +     int id;
  1.4421 +  } aId[] = {
  1.4422 +    { "SQLITE_LIMIT_LENGTH",              SQLITE_LIMIT_LENGTH               },
  1.4423 +    { "SQLITE_LIMIT_SQL_LENGTH",          SQLITE_LIMIT_SQL_LENGTH           },
  1.4424 +    { "SQLITE_LIMIT_COLUMN",              SQLITE_LIMIT_COLUMN               },
  1.4425 +    { "SQLITE_LIMIT_EXPR_DEPTH",          SQLITE_LIMIT_EXPR_DEPTH           },
  1.4426 +    { "SQLITE_LIMIT_COMPOUND_SELECT",     SQLITE_LIMIT_COMPOUND_SELECT      },
  1.4427 +    { "SQLITE_LIMIT_VDBE_OP",             SQLITE_LIMIT_VDBE_OP              },
  1.4428 +    { "SQLITE_LIMIT_FUNCTION_ARG",        SQLITE_LIMIT_FUNCTION_ARG         },
  1.4429 +    { "SQLITE_LIMIT_ATTACHED",            SQLITE_LIMIT_ATTACHED             },
  1.4430 +    { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH  },
  1.4431 +    { "SQLITE_LIMIT_VARIABLE_NUMBER",     SQLITE_LIMIT_VARIABLE_NUMBER      },
  1.4432 +    
  1.4433 +    /* Out of range test cases */
  1.4434 +    { "SQLITE_LIMIT_TOOSMALL",            -1,                               },
  1.4435 +    { "SQLITE_LIMIT_TOOBIG",              SQLITE_LIMIT_VARIABLE_NUMBER+1    },
  1.4436 +  };
  1.4437 +  int i, id;
  1.4438 +  int val;
  1.4439 +  const char *zId;
  1.4440 +
  1.4441 +  if( objc!=4 ){
  1.4442 +    Tcl_AppendResult(interp, "wrong # args: should be \"",
  1.4443 +        Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0);
  1.4444 +    return TCL_ERROR;
  1.4445 +  }
  1.4446 +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1.4447 +  zId = Tcl_GetString(objv[2]);
  1.4448 +  for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){
  1.4449 +    if( strcmp(zId, aId[i].zName)==0 ){
  1.4450 +      id = aId[i].id;
  1.4451 +      break;
  1.4452 +    }
  1.4453 +  }
  1.4454 +  if( i>=sizeof(aId)/sizeof(aId[0]) ){
  1.4455 +    Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0);
  1.4456 +    return TCL_ERROR;
  1.4457 +  }
  1.4458 +  if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
  1.4459 +  rc = sqlite3_limit(db, id, val);
  1.4460 +  Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  1.4461 +  return TCL_OK;  
  1.4462 +}
  1.4463 +
  1.4464 +/*
  1.4465 +** tclcmd:  save_prng_state
  1.4466 +**
  1.4467 +** Save the state of the pseudo-random number generator.
  1.4468 +** At the same time, verify that sqlite3_test_control works even when
  1.4469 +** called with an out-of-range opcode.
  1.4470 +*/
  1.4471 +static int save_prng_state(
  1.4472 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.4473 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.4474 +  int objc,              /* Number of arguments */
  1.4475 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.4476 +){
  1.4477 +  int rc = sqlite3_test_control(9999);
  1.4478 +  assert( rc==0 );
  1.4479 +  rc = sqlite3_test_control(-1);
  1.4480 +  assert( rc==0 );
  1.4481 +  sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE);
  1.4482 +  return TCL_OK;
  1.4483 +}
  1.4484 +/*
  1.4485 +** tclcmd:  restore_prng_state
  1.4486 +*/
  1.4487 +static int restore_prng_state(
  1.4488 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.4489 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.4490 +  int objc,              /* Number of arguments */
  1.4491 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.4492 +){
  1.4493 +  sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE);
  1.4494 +  return TCL_OK;
  1.4495 +}
  1.4496 +/*
  1.4497 +** tclcmd:  reset_prng_state
  1.4498 +*/
  1.4499 +static int reset_prng_state(
  1.4500 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.4501 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.4502 +  int objc,              /* Number of arguments */
  1.4503 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.4504 +){
  1.4505 +  sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET);
  1.4506 +  return TCL_OK;
  1.4507 +}
  1.4508 +
  1.4509 +/*
  1.4510 +** tclcmd:  pcache_stats
  1.4511 +*/
  1.4512 +static int test_pcache_stats(
  1.4513 +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1.4514 +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  1.4515 +  int objc,              /* Number of arguments */
  1.4516 +  Tcl_Obj *CONST objv[]  /* Command arguments */
  1.4517 +){
  1.4518 +  int nMin;
  1.4519 +  int nMax;
  1.4520 +  int nCurrent;
  1.4521 +  int nRecyclable;
  1.4522 +  Tcl_Obj *pRet;
  1.4523 +
  1.4524 +  sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable);
  1.4525 +
  1.4526 +  pRet = Tcl_NewObj();
  1.4527 +  Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1));
  1.4528 +  Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent));
  1.4529 +  Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1));
  1.4530 +  Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax));
  1.4531 +  Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1));
  1.4532 +  Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin));
  1.4533 +  Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1));
  1.4534 +  Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable));
  1.4535 +
  1.4536 +  Tcl_SetObjResult(interp, pRet);
  1.4537 +
  1.4538 +  return TCL_OK;
  1.4539 +}
  1.4540 +
  1.4541 +
  1.4542 +/*
  1.4543 +** Register commands with the TCL interpreter.
  1.4544 +*/
  1.4545 +int Sqlitetest1_Init(Tcl_Interp *interp){
  1.4546 +  extern int sqlite3_search_count;
  1.4547 +  extern int sqlite3_interrupt_count;
  1.4548 +  extern int sqlite3_open_file_count;
  1.4549 +  extern int sqlite3_sort_count;
  1.4550 +  extern int sqlite3_current_time;
  1.4551 +  extern int sqlite3_max_blobsize;
  1.4552 +  extern int sqlite3BtreeSharedCacheReport(void*,
  1.4553 +                                          Tcl_Interp*,int,Tcl_Obj*CONST*);
  1.4554 +  static struct {
  1.4555 +     char *zName;
  1.4556 +     Tcl_CmdProc *xProc;
  1.4557 +  } aCmd[] = {
  1.4558 +     { "db_enter",                      (Tcl_CmdProc*)db_enter               },
  1.4559 +     { "db_leave",                      (Tcl_CmdProc*)db_leave               },
  1.4560 +     { "sqlite3_mprintf_int",           (Tcl_CmdProc*)sqlite3_mprintf_int    },
  1.4561 +     { "sqlite3_mprintf_int64",         (Tcl_CmdProc*)sqlite3_mprintf_int64  },
  1.4562 +     { "sqlite3_mprintf_str",           (Tcl_CmdProc*)sqlite3_mprintf_str    },
  1.4563 +     { "sqlite3_snprintf_str",          (Tcl_CmdProc*)sqlite3_snprintf_str   },
  1.4564 +     { "sqlite3_mprintf_stronly",       (Tcl_CmdProc*)sqlite3_mprintf_stronly},
  1.4565 +     { "sqlite3_mprintf_double",        (Tcl_CmdProc*)sqlite3_mprintf_double },
  1.4566 +     { "sqlite3_mprintf_scaled",        (Tcl_CmdProc*)sqlite3_mprintf_scaled },
  1.4567 +     { "sqlite3_mprintf_hexdouble",   (Tcl_CmdProc*)sqlite3_mprintf_hexdouble},
  1.4568 +     { "sqlite3_mprintf_z_test",        (Tcl_CmdProc*)test_mprintf_z        },
  1.4569 +     { "sqlite3_mprintf_n_test",        (Tcl_CmdProc*)test_mprintf_n        },
  1.4570 +     { "sqlite3_snprintf_int",          (Tcl_CmdProc*)test_snprintf_int     },
  1.4571 +     { "sqlite3_last_insert_rowid",     (Tcl_CmdProc*)test_last_rowid       },
  1.4572 +     { "sqlite3_exec_printf",           (Tcl_CmdProc*)test_exec_printf      },
  1.4573 +     { "sqlite3_exec",                  (Tcl_CmdProc*)test_exec             },
  1.4574 +     { "sqlite3_exec_nr",               (Tcl_CmdProc*)test_exec_nr          },
  1.4575 +#ifndef SQLITE_OMIT_GET_TABLE
  1.4576 +     { "sqlite3_get_table_printf",      (Tcl_CmdProc*)test_get_table_printf },
  1.4577 +#endif
  1.4578 +     { "sqlite3_close",                 (Tcl_CmdProc*)sqlite_test_close     },
  1.4579 +     { "sqlite3_create_function",       (Tcl_CmdProc*)test_create_function  },
  1.4580 +     { "sqlite3_create_aggregate",      (Tcl_CmdProc*)test_create_aggregate },
  1.4581 +     { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func    },
  1.4582 +     { "sqlite_abort",                  (Tcl_CmdProc*)sqlite_abort          },
  1.4583 +     { "sqlite_bind",                   (Tcl_CmdProc*)test_bind             },
  1.4584 +     { "breakpoint",                    (Tcl_CmdProc*)test_breakpoint       },
  1.4585 +     { "sqlite3_key",                   (Tcl_CmdProc*)test_key              },
  1.4586 +     { "sqlite3_rekey",                 (Tcl_CmdProc*)test_rekey            },
  1.4587 +     { "sqlite_set_magic",              (Tcl_CmdProc*)sqlite_set_magic      },
  1.4588 +     { "sqlite3_interrupt",             (Tcl_CmdProc*)test_interrupt        },
  1.4589 +     { "sqlite_delete_function",        (Tcl_CmdProc*)delete_function       },
  1.4590 +     { "sqlite_delete_collation",       (Tcl_CmdProc*)delete_collation      },
  1.4591 +     { "sqlite3_get_autocommit",        (Tcl_CmdProc*)get_autocommit        },
  1.4592 +     { "sqlite3_stack_used",            (Tcl_CmdProc*)test_stack_used       },
  1.4593 +     { "sqlite3_busy_timeout",          (Tcl_CmdProc*)test_busy_timeout     },
  1.4594 +     { "printf",                        (Tcl_CmdProc*)test_printf           },
  1.4595 +     { "sqlite3IoTrace",              (Tcl_CmdProc*)test_io_trace         },
  1.4596 +  };
  1.4597 +  static struct {
  1.4598 +     char *zName;
  1.4599 +     Tcl_ObjCmdProc *xProc;
  1.4600 +     void *clientData;
  1.4601 +  } aObjCmd[] = {
  1.4602 +     { "sqlite3_connection_pointer",    get_sqlite_pointer, 0 },
  1.4603 +     { "sqlite3_bind_int",              test_bind_int,      0 },
  1.4604 +     { "sqlite3_bind_zeroblob",         test_bind_zeroblob, 0 },
  1.4605 +     { "sqlite3_bind_int64",            test_bind_int64,    0 },
  1.4606 +     { "sqlite3_bind_double",           test_bind_double,   0 },
  1.4607 +     { "sqlite3_bind_null",             test_bind_null     ,0 },
  1.4608 +     { "sqlite3_bind_text",             test_bind_text     ,0 },
  1.4609 +     { "sqlite3_bind_text16",           test_bind_text16   ,0 },
  1.4610 +     { "sqlite3_bind_blob",             test_bind_blob     ,0 },
  1.4611 +     { "sqlite3_bind_parameter_count",  test_bind_parameter_count, 0},
  1.4612 +     { "sqlite3_bind_parameter_name",   test_bind_parameter_name,  0},
  1.4613 +     { "sqlite3_bind_parameter_index",  test_bind_parameter_index, 0},
  1.4614 +     { "sqlite3_clear_bindings",        test_clear_bindings, 0},
  1.4615 +     { "sqlite3_sleep",                 test_sleep,          0},
  1.4616 +     { "sqlite3_errcode",               test_errcode       ,0 },
  1.4617 +     { "sqlite3_errmsg",                test_errmsg        ,0 },
  1.4618 +     { "sqlite3_errmsg16",              test_errmsg16      ,0 },
  1.4619 +     { "sqlite3_open",                  test_open          ,0 },
  1.4620 +     { "sqlite3_open16",                test_open16        ,0 },
  1.4621 +     { "sqlite3_complete16",            test_complete16    ,0 },
  1.4622 +
  1.4623 +     { "sqlite3_prepare",               test_prepare       ,0 },
  1.4624 +     { "sqlite3_prepare16",             test_prepare16     ,0 },
  1.4625 +     { "sqlite3_prepare_v2",            test_prepare_v2    ,0 },
  1.4626 +     { "sqlite3_prepare_tkt3134",       test_prepare_tkt3134, 0},
  1.4627 +     { "sqlite3_prepare16_v2",          test_prepare16_v2  ,0 },
  1.4628 +     { "sqlite3_finalize",              test_finalize      ,0 },
  1.4629 +     { "sqlite3_reset",                 test_reset         ,0 },
  1.4630 +     { "sqlite3_expired",               test_expired       ,0 },
  1.4631 +     { "sqlite3_transfer_bindings",     test_transfer_bind ,0 },
  1.4632 +     { "sqlite3_changes",               test_changes       ,0 },
  1.4633 +     { "sqlite3_step",                  test_step          ,0 },
  1.4634 +     { "sqlite3_next_stmt",             test_next_stmt     ,0 },
  1.4635 +
  1.4636 +     { "sqlite3_release_memory",        test_release_memory,     0},
  1.4637 +     { "sqlite3_soft_heap_limit",       test_soft_heap_limit,    0},
  1.4638 +     { "sqlite3_thread_cleanup",        test_thread_cleanup,     0},
  1.4639 +     { "sqlite3_pager_refcounts",       test_pager_refcounts,    0},
  1.4640 +
  1.4641 +     { "sqlite3_load_extension",        test_load_extension,     0},
  1.4642 +     { "sqlite3_enable_load_extension", test_enable_load,        0},
  1.4643 +     { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
  1.4644 +     { "sqlite3_limit",                 test_limit,                 0},
  1.4645 +
  1.4646 +     { "save_prng_state",               save_prng_state,    0 },
  1.4647 +     { "restore_prng_state",            restore_prng_state, 0 },
  1.4648 +     { "reset_prng_state",              reset_prng_state,   0 },
  1.4649 +
  1.4650 +     /* sqlite3_column_*() API */
  1.4651 +     { "sqlite3_column_count",          test_column_count  ,0 },
  1.4652 +     { "sqlite3_data_count",            test_data_count    ,0 },
  1.4653 +     { "sqlite3_column_type",           test_column_type   ,0 },
  1.4654 +     { "sqlite3_column_blob",           test_column_blob   ,0 },
  1.4655 +     { "sqlite3_column_double",         test_column_double ,0 },
  1.4656 +     { "sqlite3_column_int64",          test_column_int64  ,0 },
  1.4657 +     { "sqlite3_column_text",   test_stmt_utf8,  (void*)sqlite3_column_text },
  1.4658 +     { "sqlite3_column_name",   test_stmt_utf8,  (void*)sqlite3_column_name },
  1.4659 +     { "sqlite3_column_int",    test_stmt_int,   (void*)sqlite3_column_int  },
  1.4660 +     { "sqlite3_column_bytes",  test_stmt_int,   (void*)sqlite3_column_bytes},
  1.4661 +#ifndef SQLITE_OMIT_DECLTYPE
  1.4662 +     { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype},
  1.4663 +#endif
  1.4664 +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1.4665 +{ "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name},
  1.4666 +{ "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name},
  1.4667 +{ "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name},
  1.4668 +#endif
  1.4669 +
  1.4670 +#ifndef SQLITE_OMIT_UTF16
  1.4671 +     { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 },
  1.4672 +     { "sqlite3_column_text16",  test_stmt_utf16, (void*)sqlite3_column_text16},
  1.4673 +     { "sqlite3_column_name16",  test_stmt_utf16, (void*)sqlite3_column_name16},
  1.4674 +     { "add_alignment_test_collations", add_alignment_test_collations, 0      },
  1.4675 +#ifndef SQLITE_OMIT_DECLTYPE
  1.4676 +     { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16},
  1.4677 +#endif
  1.4678 +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1.4679 +{"sqlite3_column_database_name16",
  1.4680 +  test_stmt_utf16, sqlite3_column_database_name16},
  1.4681 +{"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16},
  1.4682 +{"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16},
  1.4683 +#endif
  1.4684 +#endif
  1.4685 +     { "sqlite3_create_collation_v2", test_create_collation_v2, 0 },
  1.4686 +     { "sqlite3_global_recover",     test_global_recover, 0   },
  1.4687 +     { "working_64bit_int",          working_64bit_int,   0   },
  1.4688 +     { "vfs_unlink_test",            vfs_unlink_test,     0   },
  1.4689 +     { "vfs_initfail_test",          vfs_initfail_test,   0   },
  1.4690 +     { "vfs_unregister_all",         vfs_unregister_all,  0   },
  1.4691 +     { "vfs_reregister_all",         vfs_reregister_all,  0   },
  1.4692 +     { "file_control_test",          file_control_test,   0   },
  1.4693 +     { "sqlite3_vfs_list",           vfs_list,     0   },
  1.4694 +
  1.4695 +     /* Functions from os.h */
  1.4696 +#ifndef SQLITE_OMIT_UTF16
  1.4697 +     { "add_test_collate",        test_collate, 0            },
  1.4698 +     { "add_test_collate_needed", test_collate_needed, 0     },
  1.4699 +     { "add_test_function",       test_function, 0           },
  1.4700 +#endif
  1.4701 +     { "sqlite3_test_errstr",     test_errstr, 0             },
  1.4702 +     { "tcl_variable_type",       tcl_variable_type, 0       },
  1.4703 +#ifndef SQLITE_OMIT_SHARED_CACHE
  1.4704 +     { "sqlite3_enable_shared_cache", test_enable_shared, 0  },
  1.4705 +     { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0},
  1.4706 +#endif
  1.4707 +     { "sqlite3_libversion_number", test_libversion_number, 0  },
  1.4708 +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1.4709 +     { "sqlite3_table_column_metadata", test_table_column_metadata, 0  },
  1.4710 +#endif
  1.4711 +#ifndef SQLITE_OMIT_INCRBLOB
  1.4712 +     { "sqlite3_blob_read",  test_blob_read, 0  },
  1.4713 +     { "sqlite3_blob_write", test_blob_write, 0  },
  1.4714 +#endif
  1.4715 +     { "pcache_stats",       test_pcache_stats, 0  },
  1.4716 +  };
  1.4717 +  static int bitmask_size = sizeof(Bitmask)*8;
  1.4718 +  int i;
  1.4719 +  extern int sqlite3_sync_count, sqlite3_fullsync_count;
  1.4720 +  extern int sqlite3_opentemp_count;
  1.4721 +  extern int sqlite3_like_count;
  1.4722 +  extern int sqlite3_xferopt_count;
  1.4723 +  extern int sqlite3_pager_readdb_count;
  1.4724 +  extern int sqlite3_pager_writedb_count;
  1.4725 +  extern int sqlite3_pager_writej_count;
  1.4726 +#if SQLITE_OS_UNIX && defined(SQLITE_TEST) && SQLITE_THREADSAFE
  1.4727 +  extern int threadsOverrideEachOthersLocks;
  1.4728 +#endif
  1.4729 +#if SQLITE_OS_WIN
  1.4730 +  extern int sqlite3_os_type;
  1.4731 +#endif
  1.4732 +#ifdef SQLITE_DEBUG
  1.4733 +  extern int sqlite3WhereTrace;
  1.4734 +  extern int sqlite3OSTrace;
  1.4735 +  extern int sqlite3VdbeAddopTrace;
  1.4736 +#endif
  1.4737 +#ifdef SQLITE_TEST
  1.4738 +  extern int sqlite3_enable_in_opt;
  1.4739 +  extern char sqlite3_query_plan[];
  1.4740 +  static char *query_plan = sqlite3_query_plan;
  1.4741 +#endif
  1.4742 +
  1.4743 +  for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
  1.4744 +    Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
  1.4745 +  }
  1.4746 +  for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
  1.4747 +    Tcl_CreateObjCommand(interp, aObjCmd[i].zName, 
  1.4748 +        aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
  1.4749 +  }
  1.4750 +  Tcl_LinkVar(interp, "sqlite_search_count", 
  1.4751 +      (char*)&sqlite3_search_count, TCL_LINK_INT);
  1.4752 +  Tcl_LinkVar(interp, "sqlite_sort_count", 
  1.4753 +      (char*)&sqlite3_sort_count, TCL_LINK_INT);
  1.4754 +  Tcl_LinkVar(interp, "sqlite3_max_blobsize", 
  1.4755 +      (char*)&sqlite3_max_blobsize, TCL_LINK_INT);
  1.4756 +  Tcl_LinkVar(interp, "sqlite_like_count", 
  1.4757 +      (char*)&sqlite3_like_count, TCL_LINK_INT);
  1.4758 +  Tcl_LinkVar(interp, "sqlite_interrupt_count", 
  1.4759 +      (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
  1.4760 +  Tcl_LinkVar(interp, "sqlite_open_file_count", 
  1.4761 +      (char*)&sqlite3_open_file_count, TCL_LINK_INT);
  1.4762 +  Tcl_LinkVar(interp, "sqlite_current_time", 
  1.4763 +      (char*)&sqlite3_current_time, TCL_LINK_INT);
  1.4764 +  Tcl_LinkVar(interp, "sqlite3_xferopt_count",
  1.4765 +      (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
  1.4766 +  Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
  1.4767 +      (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);
  1.4768 +  Tcl_LinkVar(interp, "sqlite3_pager_writedb_count",
  1.4769 +      (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT);
  1.4770 +  Tcl_LinkVar(interp, "sqlite3_pager_writej_count",
  1.4771 +      (char*)&sqlite3_pager_writej_count, TCL_LINK_INT);
  1.4772 +#ifndef SQLITE_OMIT_UTF16
  1.4773 +  Tcl_LinkVar(interp, "unaligned_string_counter",
  1.4774 +      (char*)&unaligned_string_counter, TCL_LINK_INT);
  1.4775 +#endif
  1.4776 +#if SQLITE_OS_UNIX && defined(SQLITE_TEST) && SQLITE_THREADSAFE
  1.4777 +  Tcl_LinkVar(interp, "threadsOverrideEachOthersLocks",
  1.4778 +      (char*)&threadsOverrideEachOthersLocks, TCL_LINK_INT);
  1.4779 +#endif
  1.4780 +#ifndef SQLITE_OMIT_UTF16
  1.4781 +  Tcl_LinkVar(interp, "sqlite_last_needed_collation",
  1.4782 +      (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
  1.4783 +#endif
  1.4784 +#if SQLITE_OS_WIN
  1.4785 +  Tcl_LinkVar(interp, "sqlite_os_type",
  1.4786 +      (char*)&sqlite3_os_type, TCL_LINK_INT);
  1.4787 +#endif
  1.4788 +#ifdef SQLITE_TEST
  1.4789 +  Tcl_LinkVar(interp, "sqlite_query_plan",
  1.4790 +      (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
  1.4791 +#endif
  1.4792 +#ifdef SQLITE_DEBUG
  1.4793 +  Tcl_LinkVar(interp, "sqlite_addop_trace",
  1.4794 +      (char*)&sqlite3VdbeAddopTrace, TCL_LINK_INT);
  1.4795 +  Tcl_LinkVar(interp, "sqlite_where_trace",
  1.4796 +      (char*)&sqlite3WhereTrace, TCL_LINK_INT);
  1.4797 +  Tcl_LinkVar(interp, "sqlite_os_trace",
  1.4798 +      (char*)&sqlite3OSTrace, TCL_LINK_INT);
  1.4799 +#endif
  1.4800 +#ifndef SQLITE_OMIT_DISKIO
  1.4801 +  Tcl_LinkVar(interp, "sqlite_opentemp_count",
  1.4802 +      (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
  1.4803 +#endif
  1.4804 +  Tcl_LinkVar(interp, "sqlite_static_bind_value",
  1.4805 +      (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
  1.4806 +  Tcl_LinkVar(interp, "sqlite_static_bind_nbyte",
  1.4807 +      (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT);
  1.4808 +  Tcl_LinkVar(interp, "sqlite_temp_directory",
  1.4809 +      (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
  1.4810 +  Tcl_LinkVar(interp, "bitmask_size",
  1.4811 +      (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
  1.4812 +  Tcl_LinkVar(interp, "sqlite_sync_count",
  1.4813 +      (char*)&sqlite3_sync_count, TCL_LINK_INT);
  1.4814 +  Tcl_LinkVar(interp, "sqlite_fullsync_count",
  1.4815 +      (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
  1.4816 +#ifdef SQLITE_TEST
  1.4817 +  Tcl_LinkVar(interp, "sqlite_enable_in_opt",
  1.4818 +      (char*)&sqlite3_enable_in_opt, TCL_LINK_INT);
  1.4819 +#endif
  1.4820 +  return TCL_OK;
  1.4821 +}