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