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