Update contrib.
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
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.
16 ** $Id: test1.c,v 1.326 2008/10/02 14:49:02 danielk1977 Exp $
18 #include "sqliteInt.h"
24 ** This is a copy of the first part of the SqliteDb structure in
25 ** tclsqlite.c. We need it here so that the get_sqlite_pointer routine
26 ** can extract the sqlite3* pointer from an existing Tcl SQLite
34 ** Convert text generated by the "%p" conversion format back into
37 static int testHexToInt(int h){
38 if( h>='0' && h<='9' ){
40 }else if( h>='a' && h<='f' ){
43 assert( h>='A' && h<='F' );
47 void *sqlite3TestTextToPtr(const char *z){
51 if( z[0]=='0' && z[1]=='x' ){
56 v = (v<<4) + testHexToInt(*z);
59 if( sizeof(p)==sizeof(v) ){
60 memcpy(&p, &v, sizeof(p));
62 assert( sizeof(p)==sizeof(v2) );
64 memcpy(&p, &v2, sizeof(p));
71 ** A TCL command that returns the address of the sqlite* pointer
72 ** for an sqlite connection instance. Bad things happen if the
73 ** input is not an sqlite connection.
75 static int get_sqlite_pointer(
85 Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION");
88 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
89 Tcl_AppendResult(interp, "command not found: ",
90 Tcl_GetString(objv[1]), (char*)0);
93 p = (struct SqliteDb*)cmdInfo.objClientData;
94 sprintf(zBuf, "%p", p->db);
95 if( strncmp(zBuf,"0x",2) ){
96 sprintf(zBuf, "0x%p", p->db);
98 Tcl_AppendResult(interp, zBuf, 0);
103 ** Decode a pointer to an sqlite3 object.
105 int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
108 if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){
109 p = (struct SqliteDb*)cmdInfo.objClientData;
112 *ppDb = (sqlite3*)sqlite3TestTextToPtr(zA);
118 const char *sqlite3TestErrorName(int rc){
119 const char *zName = 0;
121 case SQLITE_OK: zName = "SQLITE_OK"; break;
122 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
123 case SQLITE_PERM: zName = "SQLITE_PERM"; break;
124 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
125 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
126 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
127 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
128 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
129 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
130 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
131 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
132 case SQLITE_FULL: zName = "SQLITE_FULL"; break;
133 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
134 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
135 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
136 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
137 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
138 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
139 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
140 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
141 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
142 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
143 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
144 case SQLITE_ROW: zName = "SQLITE_ROW"; break;
145 case SQLITE_DONE: zName = "SQLITE_DONE"; break;
146 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
147 case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break;
148 default: zName = "SQLITE_Unknown"; break;
152 #define t1ErrorName sqlite3TestErrorName
155 ** Convert an sqlite3_stmt* into an sqlite3*. This depends on the
156 ** fact that the sqlite3* is the first field in the Vdbe structure.
158 #define StmtToDb(X) sqlite3_db_handle(X)
161 ** Check a return value to make sure it agrees with the results
162 ** from sqlite3_errcode.
164 int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){
165 if( rc!=SQLITE_MISUSE && rc!=SQLITE_OK && sqlite3_errcode(db)!=rc ){
167 int r2 = sqlite3_errcode(db);
168 sprintf(zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)",
169 t1ErrorName(rc), rc, t1ErrorName(r2), r2);
170 Tcl_ResetResult(interp);
171 Tcl_AppendResult(interp, zBuf, 0);
178 ** Decode a pointer to an sqlite3_stmt object.
180 static int getStmtPointer(
183 sqlite3_stmt **ppStmt
185 *ppStmt = (sqlite3_stmt*)sqlite3TestTextToPtr(zArg);
190 ** Generate a text representation of a pointer that can be understood
191 ** by the getDbPointer and getVmPointer routines above.
193 ** The problem is, on some machines (Solaris) if you do a printf with
194 ** "%p" you cannot turn around and do a scanf with the same "%p" and
195 ** get your pointer back. You have to prepend a "0x" before it will
196 ** work. Or at least that is what is reported to me (drh). But this
197 ** behavior varies from machine to machine. The solution used her is
198 ** to test the string right after it is generated to see if it can be
199 ** understood by scanf, and if not, try prepending an "0x" to see if
200 ** that helps. If nothing works, a fatal error is generated.
202 int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){
203 sqlite3_snprintf(100, zPtr, "%p", p);
208 ** The callback routine for sqlite3_exec_printf().
210 static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){
211 Tcl_DString *str = (Tcl_DString*)pArg;
214 if( Tcl_DStringLength(str)==0 ){
215 for(i=0; i<argc; i++){
216 Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL");
219 for(i=0; i<argc; i++){
220 Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL");
226 ** The I/O tracing callback.
228 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
229 static FILE *iotrace_file = 0;
230 static void io_trace_callback(const char *zFormat, ...){
232 va_start(ap, zFormat);
233 vfprintf(iotrace_file, zFormat, ap);
235 fflush(iotrace_file);
240 ** Usage: io_trace FILENAME
242 ** Turn I/O tracing on or off. If FILENAME is not an empty string,
243 ** I/O tracing begins going into FILENAME. If FILENAME is an empty
244 ** string, I/O tracing is turned off.
246 static int test_io_trace(
248 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
249 int argc, /* Number of arguments */
250 char **argv /* Text of each argument */
252 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
254 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
259 if( iotrace_file!=stdout && iotrace_file!=stderr ){
260 fclose(iotrace_file);
266 if( strcmp(argv[1],"stdout")==0 ){
267 iotrace_file = stdout;
268 }else if( strcmp(argv[1],"stderr")==0 ){
269 iotrace_file = stderr;
271 iotrace_file = fopen(argv[1], "w");
273 sqlite3IoTrace = io_trace_callback;
281 ** Usage: sqlite3_exec_printf DB FORMAT STRING
283 ** Invoke the sqlite3_exec_printf() interface using the open database
284 ** DB. The SQL is the string FORMAT. The format string should contain
285 ** one %s or %q. STRING is the value inserted into %s or %q.
287 static int test_exec_printf(
289 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
290 int argc, /* Number of arguments */
291 char **argv /* Text of each argument */
300 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
301 " DB FORMAT STRING", 0);
304 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
305 Tcl_DStringInit(&str);
306 zSql = sqlite3_mprintf(argv[2], argv[3]);
307 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
309 sprintf(zBuf, "%d", rc);
310 Tcl_AppendElement(interp, zBuf);
311 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
312 Tcl_DStringFree(&str);
313 if( zErr ) sqlite3_free(zErr);
314 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
319 ** Usage: db_enter DB
322 ** Enter or leave the mutex on a database connection.
326 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
327 int argc, /* Number of arguments */
328 char **argv /* Text of each argument */
332 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
336 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
337 sqlite3_mutex_enter(db->mutex);
342 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
343 int argc, /* Number of arguments */
344 char **argv /* Text of each argument */
348 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
352 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
353 sqlite3_mutex_leave(db->mutex);
358 ** Usage: sqlite3_exec DB SQL
360 ** Invoke the sqlite3_exec interface using the open database DB
362 static int test_exec(
364 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
365 int argc, /* Number of arguments */
366 char **argv /* Text of each argument */
376 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
380 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
381 Tcl_DStringInit(&str);
382 zSql = sqlite3_mprintf("%s", argv[2]);
383 for(i=j=0; zSql[i];){
385 zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]);
388 zSql[j++] = zSql[i++];
392 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
394 sprintf(zBuf, "%d", rc);
395 Tcl_AppendElement(interp, zBuf);
396 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
397 Tcl_DStringFree(&str);
398 if( zErr ) sqlite3_free(zErr);
399 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
404 ** Usage: sqlite3_exec_nr DB SQL
406 ** Invoke the sqlite3_exec interface using the open database DB. Discard
409 static int test_exec_nr(
411 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
412 int argc, /* Number of arguments */
413 char **argv /* Text of each argument */
419 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
423 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
424 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
425 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
430 ** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ...
432 ** Test the %z format of sqliteMPrintf(). Use multiple mprintf() calls to
433 ** concatenate arg0 through argn using separator as the separator.
434 ** Return the result.
436 static int test_mprintf_z(
438 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
439 int argc, /* Number of arguments */
440 char **argv /* Text of each argument */
445 for(i=2; i<argc && (i==2 || zResult); i++){
446 zResult = sqlite3MPrintf(0, "%z%s%s", zResult, argv[1], argv[i]);
448 Tcl_AppendResult(interp, zResult, 0);
449 sqlite3_free(zResult);
454 ** Usage: sqlite3_mprintf_n_test STRING
456 ** Test the %n format of sqliteMPrintf(). Return the length of the
459 static int test_mprintf_n(
461 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
462 int argc, /* Number of arguments */
463 char **argv /* Text of each argument */
467 zStr = sqlite3MPrintf(0, "%s%n", argv[1], &n);
469 Tcl_SetObjResult(interp, Tcl_NewIntObj(n));
474 ** Usage: sqlite3_snprintf_int SIZE FORMAT INT
476 ** Test the of sqlite3_snprintf() routine. SIZE is the size of the
477 ** output buffer in bytes. The maximum size is 100. FORMAT is the
478 ** format string. INT is a single integer argument. The FORMAT
479 ** string must require no more than this one integer argument. If
480 ** You pass in a format string that requires more than one argument,
481 ** bad things will happen.
483 static int test_snprintf_int(
485 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
486 int argc, /* Number of arguments */
487 char **argv /* Text of each argument */
490 int n = atoi(argv[1]);
491 const char *zFormat = argv[2];
492 int a1 = atoi(argv[3]);
493 if( n>sizeof(zStr) ) n = sizeof(zStr);
494 sqlite3_snprintf(sizeof(zStr), zStr, "abcdefghijklmnopqrstuvwxyz");
495 sqlite3_snprintf(n, zStr, zFormat, a1);
496 Tcl_AppendResult(interp, zStr, 0);
500 #ifndef SQLITE_OMIT_GET_TABLE
503 ** Usage: sqlite3_get_table_printf DB FORMAT STRING ?--no-counts?
505 ** Invoke the sqlite3_get_table_printf() interface using the open database
506 ** DB. The SQL is the string FORMAT. The format string should contain
507 ** one %s or %q. STRING is the value inserted into %s or %q.
509 static int test_get_table_printf(
511 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
512 int argc, /* Number of arguments */
513 char **argv /* Text of each argument */
526 if( Tcl_GetInt(interp, argv[4], &resCount) ) return TCL_ERROR;
528 if( argc!=4 && argc!=5 ){
529 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
530 " DB FORMAT STRING ?COUNT?", 0);
533 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
534 Tcl_DStringInit(&str);
535 zSql = sqlite3_mprintf(argv[2],argv[3]);
537 rc = sqlite3_get_table(db, zSql, &aResult, 0, 0, &zErr);
539 rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr);
540 resCount = (nRow+1)*nCol;
543 sprintf(zBuf, "%d", rc);
544 Tcl_AppendElement(interp, zBuf);
547 sprintf(zBuf, "%d", nRow);
548 Tcl_AppendElement(interp, zBuf);
549 sprintf(zBuf, "%d", nCol);
550 Tcl_AppendElement(interp, zBuf);
552 for(i=0; i<resCount; i++){
553 Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL");
556 Tcl_AppendElement(interp, zErr);
558 sqlite3_free_table(aResult);
559 if( zErr ) sqlite3_free(zErr);
560 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
564 #endif /* SQLITE_OMIT_GET_TABLE */
568 ** Usage: sqlite3_last_insert_rowid DB
570 ** Returns the integer ROWID of the most recent insert.
572 static int test_last_rowid(
574 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
575 int argc, /* Number of arguments */
576 char **argv /* Text of each argument */
582 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0);
585 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
586 sprintf(zBuf, "%lld", sqlite3_last_insert_rowid(db));
587 Tcl_AppendResult(interp, zBuf, 0);
592 ** Usage: sqlite3_key DB KEY
594 ** Set the codec key.
598 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
599 int argc, /* Number of arguments */
600 char **argv /* Text of each argument */
606 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
610 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
613 #ifdef SQLITE_HAS_CODEC
614 sqlite3_key(db, zKey, nKey);
620 ** Usage: sqlite3_rekey DB KEY
622 ** Change the codec key.
624 static int test_rekey(
626 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
627 int argc, /* Number of arguments */
628 char **argv /* Text of each argument */
634 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
638 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
641 #ifdef SQLITE_HAS_CODEC
642 sqlite3_rekey(db, zKey, nKey);
648 ** Usage: sqlite3_close DB
650 ** Closes the database opened by sqlite3_open.
652 static int sqlite_test_close(
654 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
655 int argc, /* Number of arguments */
656 char **argv /* Text of each argument */
661 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
665 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
666 rc = sqlite3_close(db);
667 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
672 ** Implementation of the x_coalesce() function.
673 ** Return the first argument non-NULL argument.
675 static void t1_ifnullFunc(
676 sqlite3_context *context,
681 for(i=0; i<argc; i++){
682 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
683 int n = sqlite3_value_bytes(argv[i]);
684 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]),
685 n, SQLITE_TRANSIENT);
692 ** These are test functions. hex8() interprets its argument as
693 ** UTF8 and returns a hex encoding. hex16le() interprets its argument
694 ** as UTF16le and returns a hex encoding.
696 static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){
697 const unsigned char *z;
700 z = sqlite3_value_text(argv[0]);
701 for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){
702 sprintf(&zBuf[i*2], "%02x", z[i]&0xff);
705 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
707 #ifndef SQLITE_OMIT_UTF16
708 static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){
709 const unsigned short int *z;
712 z = sqlite3_value_text16(argv[0]);
713 for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){
714 sprintf(&zBuf[i*4], "%04x", z[i]&0xff);
717 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
722 ** A structure into which to accumulate text.
725 int nAlloc; /* Space allocated */
726 int nUsed; /* Space used */
727 char *z; /* The space */
731 ** Append text to a dstr
733 static void dstrAppend(struct dstr *p, const char *z, int divider){
735 if( p->nUsed + n + 2 > p->nAlloc ){
737 p->nAlloc = p->nAlloc*2 + n + 200;
738 zNew = sqlite3_realloc(p->z, p->nAlloc);
741 memset(p, 0, sizeof(*p));
746 if( divider && p->nUsed>0 ){
747 p->z[p->nUsed++] = divider;
749 memcpy(&p->z[p->nUsed], z, n+1);
754 ** Invoked for each callback from sqlite3ExecFunc
756 static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){
757 struct dstr *p = (struct dstr*)pData;
759 for(i=0; i<argc; i++){
761 dstrAppend(p, "NULL", ' ');
763 dstrAppend(p, argv[i], ' ');
770 ** Implementation of the x_sqlite_exec() function. This function takes
771 ** a single argument and attempts to execute that argument as SQL code.
772 ** This is illegal and should set the SQLITE_MISUSE flag on the database.
774 ** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec()
775 ** from within a function call.
777 ** This routine simulates the effect of having two threads attempt to
778 ** use the same database at the same time.
780 static void sqlite3ExecFunc(
781 sqlite3_context *context,
786 memset(&x, 0, sizeof(x));
787 (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context),
788 (char*)sqlite3_value_text(argv[0]),
789 execFuncCallback, &x, 0);
790 sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT);
795 ** Implementation of tkt2213func(), a scalar function that takes exactly
796 ** one argument. It has two interesting features:
798 ** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*.
799 ** If the three pointers returned are not the same an SQL error is raised.
801 ** * Otherwise it returns a copy of the text representation of its
802 ** argument in such a way as the VDBE representation is a Mem* cell
803 ** with the MEM_Term flag clear.
805 ** Ticket #2213 can therefore be tested by evaluating the following
808 ** tkt2213func(tkt2213func('a string'));
810 static void tkt2213Function(
811 sqlite3_context *context,
816 unsigned char const *zText1;
817 unsigned char const *zText2;
818 unsigned char const *zText3;
820 nText = sqlite3_value_bytes(argv[0]);
821 zText1 = sqlite3_value_text(argv[0]);
822 zText2 = sqlite3_value_text(argv[0]);
823 zText3 = sqlite3_value_text(argv[0]);
825 if( zText1!=zText2 || zText2!=zText3 ){
826 sqlite3_result_error(context, "tkt2213 is not fixed", -1);
828 char *zCopy = (char *)sqlite3_malloc(nText);
829 memcpy(zCopy, zText1, nText);
830 sqlite3_result_text(context, zCopy, nText, sqlite3_free);
835 ** The following SQL function takes 4 arguments. The 2nd and
836 ** 4th argument must be one of these strings: 'text', 'text16',
837 ** or 'blob' corresponding to API functions
839 ** sqlite3_value_text()
840 ** sqlite3_value_text16()
841 ** sqlite3_value_blob()
843 ** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
844 ** corresponding to APIs:
846 ** sqlite3_value_bytes()
847 ** sqlite3_value_bytes16()
850 ** The APIs designated by the 2nd through 4th arguments are applied
851 ** to the first argument in order. If the pointers returned by the
852 ** second and fourth are different, this routine returns 1. Otherwise,
853 ** this routine returns 0.
855 ** This function is used to test to see when returned pointers from
856 ** the _text(), _text16() and _blob() APIs become invalidated.
858 static void ptrChngFunction(
859 sqlite3_context *context,
865 if( argc!=4 ) return;
866 zCmd = (const char*)sqlite3_value_text(argv[1]);
867 if( zCmd==0 ) return;
868 if( strcmp(zCmd,"text")==0 ){
869 p1 = (const void*)sqlite3_value_text(argv[0]);
870 #ifndef SQLITE_OMIT_UTF16
871 }else if( strcmp(zCmd, "text16")==0 ){
872 p1 = (const void*)sqlite3_value_text16(argv[0]);
874 }else if( strcmp(zCmd, "blob")==0 ){
875 p1 = (const void*)sqlite3_value_blob(argv[0]);
879 zCmd = (const char*)sqlite3_value_text(argv[2]);
880 if( zCmd==0 ) return;
881 if( strcmp(zCmd,"bytes")==0 ){
882 sqlite3_value_bytes(argv[0]);
883 #ifndef SQLITE_OMIT_UTF16
884 }else if( strcmp(zCmd, "bytes16")==0 ){
885 sqlite3_value_bytes16(argv[0]);
887 }else if( strcmp(zCmd, "noop")==0 ){
892 zCmd = (const char*)sqlite3_value_text(argv[3]);
893 if( zCmd==0 ) return;
894 if( strcmp(zCmd,"text")==0 ){
895 p2 = (const void*)sqlite3_value_text(argv[0]);
896 #ifndef SQLITE_OMIT_UTF16
897 }else if( strcmp(zCmd, "text16")==0 ){
898 p2 = (const void*)sqlite3_value_text16(argv[0]);
900 }else if( strcmp(zCmd, "blob")==0 ){
901 p2 = (const void*)sqlite3_value_blob(argv[0]);
905 sqlite3_result_int(context, p1!=p2);
910 ** Usage: sqlite_test_create_function DB
912 ** Call the sqlite3_create_function API on the given database in order
913 ** to create a function named "x_coalesce". This function does the same thing
914 ** as the "coalesce" function. This function also registers an SQL function
915 ** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec()
916 ** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
917 ** The effect is similar to trying to use the same database connection from
918 ** two threads at the same time.
920 ** The original motivation for this routine was to be able to call the
921 ** sqlite3_create_function function while a query is in progress in order
922 ** to test the SQLITE_MISUSE detection logic.
924 static int test_create_function(
926 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
927 int argc, /* Number of arguments */
928 char **argv /* Text of each argument */
934 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
938 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
939 rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0,
940 t1_ifnullFunc, 0, 0);
942 rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0,
945 #ifndef SQLITE_OMIT_UTF16
947 rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0,
952 rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0,
953 tkt2213Function, 0, 0);
956 rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0,
957 ptrChngFunction, 0, 0);
960 #ifndef SQLITE_OMIT_UTF16
961 /* Use the sqlite3_create_function16() API here. Mainly for fun, but also
962 ** because it is not tested anywhere else. */
966 sqlite3_mutex_enter(db->mutex);
967 pVal = sqlite3ValueNew(db);
968 sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);
969 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
970 if( db->mallocFailed ){
973 rc = sqlite3_create_function16(db, zUtf16,
974 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
976 sqlite3ValueFree(pVal);
977 sqlite3_mutex_leave(db->mutex);
981 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
982 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
987 ** Routines to implement the x_count() aggregate function.
989 ** x_count() counts the number of non-null arguments. But there are
990 ** some twists for testing purposes.
992 ** If the argument to x_count() is 40 then a UTF-8 error is reported
993 ** on the step function. If x_count(41) is seen, then a UTF-16 error
994 ** is reported on the step function. If the total count is 42, then
995 ** a UTF-8 error is reported on the finalize function.
997 typedef struct t1CountCtx t1CountCtx;
1001 static void t1CountStep(
1002 sqlite3_context *context,
1004 sqlite3_value **argv
1007 p = sqlite3_aggregate_context(context, sizeof(*p));
1008 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
1012 int v = sqlite3_value_int(argv[0]);
1014 sqlite3_result_error(context, "value of 40 handed to x_count", -1);
1015 #ifndef SQLITE_OMIT_UTF16
1017 const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
1018 sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
1023 static void t1CountFinalize(sqlite3_context *context){
1025 p = sqlite3_aggregate_context(context, sizeof(*p));
1028 sqlite3_result_error(context, "x_count totals to 42", -1);
1030 sqlite3_result_int(context, p ? p->n : 0);
1035 static void legacyCountStep(
1036 sqlite3_context *context,
1038 sqlite3_value **argv
1042 static void legacyCountFinalize(sqlite3_context *context){
1043 sqlite3_result_int(context, sqlite3_aggregate_count(context));
1047 ** Usage: sqlite3_create_aggregate DB
1049 ** Call the sqlite3_create_function API on the given database in order
1050 ** to create a function named "x_count". This function is similar
1051 ** to the built-in count() function, with a few special quirks
1052 ** for testing the sqlite3_result_error() APIs.
1054 ** The original motivation for this routine was to be able to call the
1055 ** sqlite3_create_aggregate function while a query is in progress in order
1056 ** to test the SQLITE_MISUSE detection logic. See misuse.test.
1058 ** This routine was later extended to test the use of sqlite3_result_error()
1059 ** within aggregate functions.
1061 ** Later: It is now also extended to register the aggregate function
1062 ** "legacy_count()" with the supplied database handle. This is used
1063 ** to test the deprecated sqlite3_aggregate_count() API.
1065 static int test_create_aggregate(
1067 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1068 int argc, /* Number of arguments */
1069 char **argv /* Text of each argument */
1074 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1078 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1079 rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
1080 t1CountStep,t1CountFinalize);
1081 if( rc==SQLITE_OK ){
1082 rc = sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
1083 t1CountStep,t1CountFinalize);
1085 if( rc==SQLITE_OK ){
1086 rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0,
1087 legacyCountStep, legacyCountFinalize
1090 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1091 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
1097 ** Usage: printf TEXT
1099 ** Send output to printf. Use this rather than puts to merge the output
1100 ** in the correct sequence with debugging printfs inserted into C code.
1101 ** Puts uses a separate buffer and debugging statements will be out of
1102 ** sequence if it is used.
1104 static int test_printf(
1106 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1107 int argc, /* Number of arguments */
1108 char **argv /* Text of each argument */
1111 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1115 printf("%s\n", argv[1]);
1122 ** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
1124 ** Call mprintf with three integer arguments
1126 static int sqlite3_mprintf_int(
1128 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1129 int argc, /* Number of arguments */
1130 char **argv /* Text of each argument */
1135 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1136 " FORMAT INT INT INT\"", 0);
1140 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1142 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1143 Tcl_AppendResult(interp, z, 0);
1149 ** If zNum represents an integer that will fit in 64-bits, then set
1150 ** *pValue to that integer and return true. Otherwise return false.
1152 static int sqlite3GetInt64(const char *zNum, i64 *pValue){
1153 if( sqlite3FitsIn64Bits(zNum, 0) ){
1154 sqlite3Atoi64(zNum, pValue);
1161 ** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
1163 ** Call mprintf with three 64-bit integer arguments
1165 static int sqlite3_mprintf_int64(
1167 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1168 int argc, /* Number of arguments */
1169 char **argv /* Text of each argument */
1175 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1176 " FORMAT INT INT INT\"", 0);
1180 if( !sqlite3GetInt64(argv[i], &a[i-2]) ){
1181 Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0);
1185 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1186 Tcl_AppendResult(interp, z, 0);
1192 ** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
1194 ** Call mprintf with two integer arguments and one string argument
1196 static int sqlite3_mprintf_str(
1198 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1199 int argc, /* Number of arguments */
1200 char **argv /* Text of each argument */
1204 if( argc<4 || argc>5 ){
1205 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1206 " FORMAT INT INT ?STRING?\"", 0);
1210 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1212 z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL);
1213 Tcl_AppendResult(interp, z, 0);
1219 ** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING
1221 ** Call mprintf with two integer arguments and one string argument
1223 static int sqlite3_snprintf_str(
1225 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1226 int argc, /* Number of arguments */
1227 char **argv /* Text of each argument */
1232 if( argc<5 || argc>6 ){
1233 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1234 " INT FORMAT INT INT ?STRING?\"", 0);
1237 if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
1239 Tcl_AppendResult(interp, "N must be non-negative", 0);
1243 if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR;
1245 z = sqlite3_malloc( n+1 );
1246 sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL);
1247 Tcl_AppendResult(interp, z, 0);
1253 ** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE
1255 ** Call mprintf with two integer arguments and one double argument
1257 static int sqlite3_mprintf_double(
1259 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1260 int argc, /* Number of arguments */
1261 char **argv /* Text of each argument */
1267 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1268 " FORMAT INT INT DOUBLE\"", 0);
1272 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1274 if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;
1275 z = sqlite3_mprintf(argv[1], a[0], a[1], r);
1276 Tcl_AppendResult(interp, z, 0);
1282 ** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE
1284 ** Call mprintf with a single double argument which is the product of the
1285 ** two arguments given above. This is used to generate overflow and underflow
1286 ** doubles to test that they are converted properly.
1288 static int sqlite3_mprintf_scaled(
1290 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1291 int argc, /* Number of arguments */
1292 char **argv /* Text of each argument */
1298 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1299 " FORMAT DOUBLE DOUBLE\"", 0);
1303 if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR;
1305 z = sqlite3_mprintf(argv[1], r[0]*r[1]);
1306 Tcl_AppendResult(interp, z, 0);
1312 ** Usage: sqlite3_mprintf_stronly FORMAT STRING
1314 ** Call mprintf with a single double argument which is the product of the
1315 ** two arguments given above. This is used to generate overflow and underflow
1316 ** doubles to test that they are converted properly.
1318 static int sqlite3_mprintf_stronly(
1320 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1321 int argc, /* Number of arguments */
1322 char **argv /* Text of each argument */
1326 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1327 " FORMAT STRING\"", 0);
1330 z = sqlite3_mprintf(argv[1], argv[2]);
1331 Tcl_AppendResult(interp, z, 0);
1337 ** Usage: sqlite3_mprintf_hexdouble FORMAT HEX
1339 ** Call mprintf with a single double argument which is derived from the
1340 ** hexadecimal encoding of an IEEE double.
1342 static int sqlite3_mprintf_hexdouble(
1344 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1345 int argc, /* Number of arguments */
1346 char **argv /* Text of each argument */
1350 unsigned int x1, x2;
1353 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1354 " FORMAT STRING\"", 0);
1357 if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){
1358 Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0);
1363 memcpy(&r, &d, sizeof(r));
1364 z = sqlite3_mprintf(argv[1], r);
1365 Tcl_AppendResult(interp, z, 0);
1371 ** Usage: sqlite3_enable_shared_cache ?BOOLEAN?
1374 #if !defined(SQLITE_OMIT_SHARED_CACHE)
1375 static int test_enable_shared(
1376 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1377 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1378 int objc, /* Number of arguments */
1379 Tcl_Obj *CONST objv[] /* Command arguments */
1385 if( objc!=2 && objc!=1 ){
1386 Tcl_WrongNumArgs(interp, 1, objv, "?BOOLEAN?");
1389 ret = sqlite3GlobalConfig.sharedCacheEnabled;
1392 if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){
1395 rc = sqlite3_enable_shared_cache(enable);
1396 if( rc!=SQLITE_OK ){
1397 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
1401 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret));
1409 ** Usage: sqlite3_extended_result_codes DB BOOLEAN
1412 static int test_extended_result_codes(
1413 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1414 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1415 int objc, /* Number of arguments */
1416 Tcl_Obj *CONST objv[] /* Command arguments */
1422 Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
1425 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1426 if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR;
1427 sqlite3_extended_result_codes(db, enable);
1432 ** Usage: sqlite3_libversion_number
1435 static int test_libversion_number(
1436 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1437 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1438 int objc, /* Number of arguments */
1439 Tcl_Obj *CONST objv[] /* Command arguments */
1441 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number()));
1446 ** Usage: sqlite3_table_column_metadata DB dbname tblname colname
1449 #ifdef SQLITE_ENABLE_COLUMN_METADATA
1450 static int test_table_column_metadata(
1451 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1452 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1453 int objc, /* Number of arguments */
1454 Tcl_Obj *CONST objv[] /* Command arguments */
1463 const char *zDatatype;
1464 const char *zCollseq;
1470 Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname");
1473 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1474 zDb = Tcl_GetString(objv[2]);
1475 zTbl = Tcl_GetString(objv[3]);
1476 zCol = Tcl_GetString(objv[4]);
1478 if( strlen(zDb)==0 ) zDb = 0;
1480 rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol,
1481 &zDatatype, &zCollseq, ¬null, &primarykey, &autoincrement);
1483 if( rc!=SQLITE_OK ){
1484 Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
1488 pRet = Tcl_NewObj();
1489 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1));
1490 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1));
1491 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull));
1492 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey));
1493 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement));
1494 Tcl_SetObjResult(interp, pRet);
1500 #ifndef SQLITE_OMIT_INCRBLOB
1503 ** sqlite3_blob_read CHANNEL OFFSET N
1505 ** This command is used to test the sqlite3_blob_read() in ways that
1506 ** the Tcl channel interface does not. The first argument should
1507 ** be the name of a valid channel created by the [incrblob] method
1508 ** of a database handle. This function calls sqlite3_blob_read()
1509 ** to read N bytes from offset OFFSET from the underlying SQLite
1512 ** On success, a byte-array object containing the read data is
1513 ** returned. On failure, the interpreter result is set to the
1514 ** text representation of the returned error code (i.e. "SQLITE_NOMEM")
1515 ** and a Tcl exception is thrown.
1517 static int test_blob_read(
1518 ClientData clientData, /* Not used */
1519 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1520 int objc, /* Number of arguments */
1521 Tcl_Obj *CONST objv[] /* Command arguments */
1523 Tcl_Channel channel;
1524 ClientData instanceData;
1525 sqlite3_blob *pBlob;
1529 unsigned char *zBuf;
1533 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET N");
1537 channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), ¬Used);
1539 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset)
1540 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[3], &nByte)
1541 || nByte<0 || iOffset<0
1546 instanceData = Tcl_GetChannelInstanceData(channel);
1547 pBlob = *((sqlite3_blob **)instanceData);
1549 zBuf = (unsigned char *)Tcl_Alloc(nByte);
1550 rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset);
1551 if( rc==SQLITE_OK ){
1552 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zBuf, nByte));
1554 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
1556 Tcl_Free((char *)zBuf);
1558 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1562 ** sqlite3_blob_write CHANNEL OFFSET DATA ?NDATA?
1564 ** This command is used to test the sqlite3_blob_write() in ways that
1565 ** the Tcl channel interface does not. The first argument should
1566 ** be the name of a valid channel created by the [incrblob] method
1567 ** of a database handle. This function calls sqlite3_blob_write()
1568 ** to write the DATA byte-array to the underlying SQLite blob handle.
1569 ** at offset OFFSET.
1571 ** On success, an empty string is returned. On failure, the interpreter
1572 ** result is set to the text representation of the returned error code
1573 ** (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown.
1575 static int test_blob_write(
1576 ClientData clientData, /* Not used */
1577 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1578 int objc, /* Number of arguments */
1579 Tcl_Obj *CONST objv[] /* Command arguments */
1581 Tcl_Channel channel;
1582 ClientData instanceData;
1583 sqlite3_blob *pBlob;
1588 unsigned char *zBuf;
1591 if( objc!=4 && objc!=5 ){
1592 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET DATA ?NDATA?");
1596 channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), ¬Used);
1597 if( !channel || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset) ){
1601 instanceData = Tcl_GetChannelInstanceData(channel);
1602 pBlob = *((sqlite3_blob **)instanceData);
1604 zBuf = Tcl_GetByteArrayFromObj(objv[3], &nBuf);
1605 if( objc==5 && Tcl_GetIntFromObj(interp, objv[4], &nBuf) ){
1608 rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset);
1609 if( rc!=SQLITE_OK ){
1610 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
1613 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1618 ** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC
1620 ** This Tcl proc is used for testing the experimental
1621 ** sqlite3_create_collation_v2() interface.
1623 struct TestCollationX {
1628 typedef struct TestCollationX TestCollationX;
1629 static void testCreateCollationDel(void *pCtx){
1630 TestCollationX *p = (TestCollationX *)pCtx;
1632 int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
1634 Tcl_BackgroundError(p->interp);
1637 Tcl_DecrRefCount(p->pCmp);
1638 Tcl_DecrRefCount(p->pDel);
1639 sqlite3_free((void *)p);
1641 static int testCreateCollationCmp(
1648 TestCollationX *p = (TestCollationX *)pCtx;
1649 Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp);
1652 Tcl_IncrRefCount(pScript);
1653 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft));
1654 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight));
1656 if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL)
1657 || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes)
1659 Tcl_BackgroundError(p->interp);
1661 Tcl_DecrRefCount(pScript);
1665 static int test_create_collation_v2(
1666 ClientData clientData, /* Not used */
1667 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1668 int objc, /* Number of arguments */
1669 Tcl_Obj *CONST objv[] /* Command arguments */
1676 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC");
1679 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1681 p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX));
1685 Tcl_IncrRefCount(p->pCmp);
1686 Tcl_IncrRefCount(p->pDel);
1688 rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), 16,
1689 (void *)p, testCreateCollationCmp, testCreateCollationDel
1691 if( rc!=SQLITE_MISUSE ){
1692 Tcl_AppendResult(interp, "sqlite3_create_collate_v2() failed to detect "
1693 "an invalid encoding", (char*)0);
1696 rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8,
1697 (void *)p, testCreateCollationCmp, testCreateCollationDel
1703 ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
1705 static int test_load_extension(
1706 ClientData clientData, /* Not used */
1707 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1708 int objc, /* Number of arguments */
1709 Tcl_Obj *CONST objv[] /* Command arguments */
1711 Tcl_CmdInfo cmdInfo;
1719 if( objc!=4 && objc!=3 ){
1720 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?");
1723 zDb = Tcl_GetString(objv[1]);
1724 zFile = Tcl_GetString(objv[2]);
1726 zProc = Tcl_GetString(objv[3]);
1729 /* Extract the C database handle from the Tcl command name */
1730 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1731 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1734 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1737 /* Call the underlying C function. If an error occurs, set rc to
1738 ** TCL_ERROR and load any error string into the interpreter. If no
1739 ** error occurs, set rc to TCL_OK.
1741 #ifdef SQLITE_OMIT_LOAD_EXTENSION
1743 zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()");
1745 rc = sqlite3_load_extension(db, zFile, zProc, &zErr);
1747 if( rc!=SQLITE_OK ){
1748 Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE);
1759 ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
1761 static int test_enable_load(
1762 ClientData clientData, /* Not used */
1763 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1764 int objc, /* Number of arguments */
1765 Tcl_Obj *CONST objv[] /* Command arguments */
1767 Tcl_CmdInfo cmdInfo;
1773 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
1776 zDb = Tcl_GetString(objv[1]);
1778 /* Extract the C database handle from the Tcl command name */
1779 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1780 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1783 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1786 /* Get the onoff parameter */
1787 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
1791 #ifdef SQLITE_OMIT_LOAD_EXTENSION
1792 Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
1795 sqlite3_enable_load_extension(db, onoff);
1801 ** Usage: sqlite_abort
1803 ** Shutdown the process immediately. This is not a clean shutdown.
1804 ** This command is used to test the recoverability of a database in
1805 ** the event of a program crash.
1807 static int sqlite_abort(
1809 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1810 int argc, /* Number of arguments */
1811 char **argv /* Text of each argument */
1813 assert( interp==0 ); /* This will always fail */
1818 ** The following routine is a user-defined SQL function whose purpose
1819 ** is to test the sqlite_set_result() API.
1821 static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1823 const char *zArg0 = (char*)sqlite3_value_text(argv[0]);
1825 if( 0==sqlite3StrICmp(zArg0, "int") ){
1826 sqlite3_result_int(context, sqlite3_value_int(argv[1]));
1827 }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
1828 sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
1829 }else if( sqlite3StrICmp(zArg0,"string")==0 ){
1830 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1,
1832 }else if( sqlite3StrICmp(zArg0,"double")==0 ){
1833 sqlite3_result_double(context, sqlite3_value_double(argv[1]));
1834 }else if( sqlite3StrICmp(zArg0,"null")==0 ){
1835 sqlite3_result_null(context);
1836 }else if( sqlite3StrICmp(zArg0,"value")==0 ){
1837 sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
1850 sqlite3_result_error(context,"first argument should be one of: "
1851 "int int64 string double null value", -1);
1855 ** Usage: sqlite_register_test_function DB NAME
1857 ** Register the test SQL function on the database DB under the name NAME.
1859 static int test_register_func(
1861 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1862 int argc, /* Number of arguments */
1863 char **argv /* Text of each argument */
1868 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1869 " DB FUNCTION-NAME", 0);
1872 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
1873 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,
1876 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
1879 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1884 ** Usage: sqlite3_finalize STMT
1886 ** Finalize a statement handle.
1888 static int test_finalize(
1892 Tcl_Obj *CONST objv[]
1894 sqlite3_stmt *pStmt;
1899 Tcl_AppendResult(interp, "wrong # args: should be \"",
1900 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
1904 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1907 db = StmtToDb(pStmt);
1909 rc = sqlite3_finalize(pStmt);
1910 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
1911 if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1916 ** Usage: sqlite3_next_stmt DB STMT
1918 ** Return the next statment in sequence after STMT.
1920 static int test_next_stmt(
1924 Tcl_Obj *CONST objv[]
1926 sqlite3_stmt *pStmt;
1931 Tcl_AppendResult(interp, "wrong # args: should be \"",
1932 Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0);
1936 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1937 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR;
1938 pStmt = sqlite3_next_stmt(db, pStmt);
1940 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
1941 Tcl_AppendResult(interp, zBuf, 0);
1948 ** Usage: sqlite3_reset STMT
1950 ** Reset a statement handle.
1952 static int test_reset(
1956 Tcl_Obj *CONST objv[]
1958 sqlite3_stmt *pStmt;
1962 Tcl_AppendResult(interp, "wrong # args: should be \"",
1963 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
1967 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1969 rc = sqlite3_reset(pStmt);
1970 if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
1973 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
1983 ** Usage: sqlite3_expired STMT
1985 ** Return TRUE if a recompilation of the statement is recommended.
1987 static int test_expired(
1991 Tcl_Obj *CONST objv[]
1993 sqlite3_stmt *pStmt;
1995 Tcl_AppendResult(interp, "wrong # args: should be \"",
1996 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
1999 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2000 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
2005 ** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT
2007 ** Transfer all bindings from FROMSTMT over to TOSTMT
2009 static int test_transfer_bind(
2013 Tcl_Obj *CONST objv[]
2015 sqlite3_stmt *pStmt1, *pStmt2;
2017 Tcl_AppendResult(interp, "wrong # args: should be \"",
2018 Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
2021 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
2022 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
2023 Tcl_SetObjResult(interp,
2024 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
2029 ** Usage: sqlite3_changes DB
2031 ** Return the number of changes made to the database by the last SQL
2034 static int test_changes(
2038 Tcl_Obj *CONST objv[]
2042 Tcl_AppendResult(interp, "wrong # args: should be \"",
2043 Tcl_GetString(objv[0]), " DB", 0);
2046 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2047 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
2052 ** This is the "static_bind_value" that variables are bound to when
2053 ** the FLAG option of sqlite3_bind is "static"
2055 static char *sqlite_static_bind_value = 0;
2056 static int sqlite_static_bind_nbyte = 0;
2059 ** Usage: sqlite3_bind VM IDX VALUE FLAGS
2061 ** Sets the value of the IDX-th occurance of "?" in the original SQL
2062 ** string. VALUE is the new value. If FLAGS=="null" then VALUE is
2063 ** ignored and the value is set to NULL. If FLAGS=="static" then
2064 ** the value is set to the value of a static variable named
2065 ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy
2066 ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored
2067 ** an a 10-byte blob "abc\000xyz\000pq" is inserted.
2069 static int test_bind(
2071 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2072 int argc, /* Number of arguments */
2073 char **argv /* Text of each argument */
2075 sqlite3_stmt *pStmt;
2079 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2080 " VM IDX VALUE (null|static|normal)\"", 0);
2083 if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
2084 if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
2085 if( strcmp(argv[4],"null")==0 ){
2086 rc = sqlite3_bind_null(pStmt, idx);
2087 }else if( strcmp(argv[4],"static")==0 ){
2088 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
2089 }else if( strcmp(argv[4],"static-nbytes")==0 ){
2090 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value,
2091 sqlite_static_bind_nbyte, 0);
2092 }else if( strcmp(argv[4],"normal")==0 ){
2093 rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
2094 }else if( strcmp(argv[4],"blob10")==0 ){
2095 rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC);
2097 Tcl_AppendResult(interp, "4th argument should be "
2098 "\"null\" or \"static\" or \"normal\"", 0);
2101 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2104 sprintf(zBuf, "(%d) ", rc);
2105 Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
2111 #ifndef SQLITE_OMIT_UTF16
2113 ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
2115 ** This function is used to test that SQLite selects the correct collation
2116 ** sequence callback when multiple versions (for different text encodings)
2119 ** Calling this routine registers the collation sequence "test_collate"
2120 ** with database handle <db>. The second argument must be a list of three
2121 ** boolean values. If the first is true, then a version of test_collate is
2122 ** registered for UTF-8, if the second is true, a version is registered for
2123 ** UTF-16le, if the third is true, a UTF-16be version is available.
2124 ** Previous versions of test_collate are deleted.
2126 ** The collation sequence test_collate is implemented by calling the
2127 ** following TCL script:
2129 ** "test_collate <enc> <lhs> <rhs>"
2131 ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
2132 ** The <enc> parameter is the encoding of the collation function that
2133 ** SQLite selected to call. The TCL test script implements the
2134 ** "test_collate" proc.
2136 ** Note that this will only work with one intepreter at a time, as the
2137 ** interp pointer to use when evaluating the TCL script is stored in
2138 ** pTestCollateInterp.
2140 static Tcl_Interp* pTestCollateInterp;
2141 static int test_collate_func(
2143 int nA, const void *zA,
2144 int nB, const void *zB
2146 Tcl_Interp *i = pTestCollateInterp;
2147 int encin = (int)pCtx;
2151 sqlite3_value *pVal;
2154 pX = Tcl_NewStringObj("test_collate", -1);
2155 Tcl_IncrRefCount(pX);
2159 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
2161 case SQLITE_UTF16LE:
2162 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
2164 case SQLITE_UTF16BE:
2165 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
2171 pVal = sqlite3ValueNew(0);
2172 sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
2173 n = sqlite3_value_bytes(pVal);
2174 Tcl_ListObjAppendElement(i,pX,
2175 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
2176 sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
2177 n = sqlite3_value_bytes(pVal);
2178 Tcl_ListObjAppendElement(i,pX,
2179 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
2180 sqlite3ValueFree(pVal);
2182 Tcl_EvalObjEx(i, pX, 0);
2183 Tcl_DecrRefCount(pX);
2184 Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
2187 static int test_collate(
2191 Tcl_Obj *CONST objv[]
2195 sqlite3_value *pVal;
2198 if( objc!=5 ) goto bad_args;
2199 pTestCollateInterp = interp;
2200 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2202 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
2203 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
2204 (void *)SQLITE_UTF8, val?test_collate_func:0);
2205 if( rc==SQLITE_OK ){
2207 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2208 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
2209 (void *)SQLITE_UTF16LE, val?test_collate_func:0);
2210 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
2213 if( sqlite3_iMallocFail>0 ){
2214 sqlite3_iMallocFail++;
2217 sqlite3_mutex_enter(db->mutex);
2218 pVal = sqlite3ValueNew(db);
2219 sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
2220 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
2221 if( db->mallocFailed ){
2224 rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE,
2225 (void *)SQLITE_UTF16BE, val?test_collate_func:0);
2227 sqlite3ValueFree(pVal);
2228 sqlite3_mutex_leave(db->mutex);
2230 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2232 if( rc!=SQLITE_OK ){
2233 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
2239 Tcl_AppendResult(interp, "wrong # args: should be \"",
2240 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
2245 ** When the collation needed callback is invoked, record the name of
2246 ** the requested collating function here. The recorded name is linked
2247 ** to a TCL variable and used to make sure that the requested collation
2250 static char zNeededCollation[200];
2251 static char *pzNeededCollation = zNeededCollation;
2255 ** Called when a collating sequence is needed. Registered using
2256 ** sqlite3_collation_needed16().
2258 static void test_collate_needed_cb(
2267 for(z = (char*)pName, i=0; *z || z[1]; z++){
2268 if( *z ) zNeededCollation[i++] = *z;
2270 zNeededCollation[i] = 0;
2271 sqlite3_create_collation(
2272 db, "test_collate", ENC(db), (void *)enc, test_collate_func);
2276 ** Usage: add_test_collate_needed DB
2278 static int test_collate_needed(
2282 Tcl_Obj *CONST objv[]
2287 if( objc!=2 ) goto bad_args;
2288 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2289 rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
2290 zNeededCollation[0] = 0;
2291 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
2295 Tcl_WrongNumArgs(interp, 1, objv, "DB");
2300 ** tclcmd: add_alignment_test_collations DB
2302 ** Add two new collating sequences to the database DB
2307 ** Both collating sequences use the same sort order as BINARY.
2308 ** The only difference is that the utf16_aligned collating
2309 ** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
2310 ** Both collating functions increment the unaligned utf16 counter
2311 ** whenever they see a string that begins on an odd byte boundary.
2313 static int unaligned_string_counter = 0;
2314 static int alignmentCollFunc(
2316 int nKey1, const void *pKey1,
2317 int nKey2, const void *pKey2
2320 n = nKey1<nKey2 ? nKey1 : nKey2;
2321 if( nKey1>0 && 1==(1&(int)pKey1) ) unaligned_string_counter++;
2322 if( nKey2>0 && 1==(1&(int)pKey2) ) unaligned_string_counter++;
2323 rc = memcmp(pKey1, pKey2, n);
2329 static int add_alignment_test_collations(
2333 Tcl_Obj *CONST objv[]
2337 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2338 sqlite3_create_collation(db, "utf16_unaligned",
2340 0, alignmentCollFunc);
2341 sqlite3_create_collation(db, "utf16_aligned",
2342 SQLITE_UTF16 | SQLITE_UTF16_ALIGNED,
2343 0, alignmentCollFunc);
2347 #endif /* !defined(SQLITE_OMIT_UTF16) */
2350 ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
2352 ** This function is used to test that SQLite selects the correct user
2353 ** function callback when multiple versions (for different text encodings)
2356 ** Calling this routine registers up to three versions of the user function
2357 ** "test_function" with database handle <db>. If the second argument is
2358 ** true, then a version of test_function is registered for UTF-8, if the
2359 ** third is true, a version is registered for UTF-16le, if the fourth is
2360 ** true, a UTF-16be version is available. Previous versions of
2361 ** test_function are deleted.
2363 ** The user function is implemented by calling the following TCL script:
2365 ** "test_function <enc> <arg>"
2367 ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
2368 ** single argument passed to the SQL function. The value returned by
2369 ** the TCL script is used as the return value of the SQL function. It
2370 ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
2371 ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
2372 ** prefers UTF-16BE.
2374 #ifndef SQLITE_OMIT_UTF16
2375 static void test_function_utf8(
2376 sqlite3_context *pCtx,
2378 sqlite3_value **argv
2382 sqlite3_value *pVal;
2383 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2384 pX = Tcl_NewStringObj("test_function", -1);
2385 Tcl_IncrRefCount(pX);
2386 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
2387 Tcl_ListObjAppendElement(interp, pX,
2388 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
2389 Tcl_EvalObjEx(interp, pX, 0);
2390 Tcl_DecrRefCount(pX);
2391 sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
2392 pVal = sqlite3ValueNew(0);
2393 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
2394 SQLITE_UTF8, SQLITE_STATIC);
2395 sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
2396 -1, SQLITE_TRANSIENT);
2397 sqlite3ValueFree(pVal);
2399 static void test_function_utf16le(
2400 sqlite3_context *pCtx,
2402 sqlite3_value **argv
2406 sqlite3_value *pVal;
2407 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2408 pX = Tcl_NewStringObj("test_function", -1);
2409 Tcl_IncrRefCount(pX);
2410 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
2411 Tcl_ListObjAppendElement(interp, pX,
2412 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
2413 Tcl_EvalObjEx(interp, pX, 0);
2414 Tcl_DecrRefCount(pX);
2415 pVal = sqlite3ValueNew(0);
2416 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
2417 SQLITE_UTF8, SQLITE_STATIC);
2418 sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
2419 sqlite3ValueFree(pVal);
2421 static void test_function_utf16be(
2422 sqlite3_context *pCtx,
2424 sqlite3_value **argv
2428 sqlite3_value *pVal;
2429 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2430 pX = Tcl_NewStringObj("test_function", -1);
2431 Tcl_IncrRefCount(pX);
2432 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
2433 Tcl_ListObjAppendElement(interp, pX,
2434 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
2435 Tcl_EvalObjEx(interp, pX, 0);
2436 Tcl_DecrRefCount(pX);
2437 pVal = sqlite3ValueNew(0);
2438 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
2439 SQLITE_UTF8, SQLITE_STATIC);
2440 sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal),
2441 -1, SQLITE_TRANSIENT);
2442 sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal),
2443 -1, SQLITE_TRANSIENT);
2444 sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
2445 -1, SQLITE_TRANSIENT);
2446 sqlite3ValueFree(pVal);
2448 #endif /* SQLITE_OMIT_UTF16 */
2449 static int test_function(
2453 Tcl_Obj *CONST objv[]
2455 #ifndef SQLITE_OMIT_UTF16
2459 if( objc!=5 ) goto bad_args;
2460 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2462 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
2464 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
2465 interp, test_function_utf8, 0, 0);
2467 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2469 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
2470 interp, test_function_utf16le, 0, 0);
2472 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
2474 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
2475 interp, test_function_utf16be, 0, 0);
2480 Tcl_AppendResult(interp, "wrong # args: should be \"",
2481 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
2482 #endif /* SQLITE_OMIT_UTF16 */
2487 ** Usage: test_errstr <err code>
2489 ** Test that the english language string equivalents for sqlite error codes
2490 ** are sane. The parameter is an integer representing an sqlite error code.
2491 ** The result is a list of two elements, the string representation of the
2492 ** error code and the english language explanation.
2494 static int test_errstr(
2498 Tcl_Obj *CONST objv[]
2503 Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
2506 zCode = Tcl_GetString(objv[1]);
2507 for(i=0; i<200; i++){
2508 if( 0==strcmp(t1ErrorName(i), zCode) ) break;
2510 Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
2515 ** Usage: breakpoint
2517 ** This routine exists for one purpose - to provide a place to put a
2518 ** breakpoint with GDB that can be triggered using TCL code. The use
2519 ** for this is when a particular test fails on (say) the 1485th iteration.
2520 ** In the TCL test script, we can add code like this:
2522 ** if {$i==1485} breakpoint
2524 ** Then run testfixture in the debugger and wait for the breakpoint to
2525 ** fire. Then additional breakpoints can be set to trace down the bug.
2527 static int test_breakpoint(
2529 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2530 int argc, /* Number of arguments */
2531 char **argv /* Text of each argument */
2533 return TCL_OK; /* Do nothing */
2537 ** Usage: sqlite3_bind_zeroblob STMT IDX N
2539 ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement.
2540 ** IDX is the index of a wildcard in the prepared statement. This command
2541 ** binds a N-byte zero-filled BLOB to the wildcard.
2543 static int test_bind_zeroblob(
2547 Tcl_Obj *CONST objv[]
2549 sqlite3_stmt *pStmt;
2555 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
2559 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2560 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2561 if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
2563 rc = sqlite3_bind_zeroblob(pStmt, idx, n);
2564 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2565 if( rc!=SQLITE_OK ){
2573 ** Usage: sqlite3_bind_int STMT N VALUE
2575 ** Test the sqlite3_bind_int interface. STMT is a prepared statement.
2576 ** N is the index of a wildcard in the prepared statement. This command
2577 ** binds a 32-bit integer VALUE to that wildcard.
2579 static int test_bind_int(
2583 Tcl_Obj *CONST objv[]
2585 sqlite3_stmt *pStmt;
2591 Tcl_AppendResult(interp, "wrong # args: should be \"",
2592 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
2596 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2597 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2598 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2600 rc = sqlite3_bind_int(pStmt, idx, value);
2601 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2602 if( rc!=SQLITE_OK ){
2611 ** Usage: sqlite3_bind_int64 STMT N VALUE
2613 ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement.
2614 ** N is the index of a wildcard in the prepared statement. This command
2615 ** binds a 64-bit integer VALUE to that wildcard.
2617 static int test_bind_int64(
2621 Tcl_Obj *CONST objv[]
2623 sqlite3_stmt *pStmt;
2629 Tcl_AppendResult(interp, "wrong # args: should be \"",
2630 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
2634 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2635 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2636 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2638 rc = sqlite3_bind_int64(pStmt, idx, value);
2639 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2640 if( rc!=SQLITE_OK ){
2649 ** Usage: sqlite3_bind_double STMT N VALUE
2651 ** Test the sqlite3_bind_double interface. STMT is a prepared statement.
2652 ** N is the index of a wildcard in the prepared statement. This command
2653 ** binds a 64-bit integer VALUE to that wildcard.
2655 static int test_bind_double(
2659 Tcl_Obj *CONST objv[]
2661 sqlite3_stmt *pStmt;
2667 static const struct {
2668 const char *zName; /* Name of the special floating point value */
2669 unsigned int iUpper; /* Upper 32 bits */
2670 unsigned int iLower; /* Lower 32 bits */
2672 { "NaN", 0x7fffffff, 0xffffffff },
2673 { "SNaN", 0x7ff7ffff, 0xffffffff },
2674 { "-NaN", 0xffffffff, 0xffffffff },
2675 { "-SNaN", 0xfff7ffff, 0xffffffff },
2676 { "+Inf", 0x7ff00000, 0x00000000 },
2677 { "-Inf", 0xfff00000, 0x00000000 },
2678 { "Epsilon", 0x00000000, 0x00000001 },
2679 { "-Epsilon", 0x80000000, 0x00000001 },
2680 { "NaN0", 0x7ff80000, 0x00000000 },
2681 { "-NaN0", 0xfff80000, 0x00000000 },
2685 Tcl_AppendResult(interp, "wrong # args: should be \"",
2686 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
2690 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2691 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2693 /* Intercept the string "NaN" and generate a NaN value for it.
2694 ** All other strings are passed through to Tcl_GetDoubleFromObj().
2695 ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions
2698 zVal = Tcl_GetString(objv[3]);
2699 for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){
2700 if( strcmp(aSpecialFp[i].zName, zVal)==0 ){
2702 x = aSpecialFp[i].iUpper;
2704 x |= aSpecialFp[i].iLower;
2705 assert( sizeof(value)==8 );
2706 assert( sizeof(x)==8 );
2707 memcpy(&value, &x, 8);
2711 if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) &&
2712 Tcl_GetDoubleFromObj(interp, objv[3], &value) ){
2715 rc = sqlite3_bind_double(pStmt, idx, value);
2716 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2717 if( rc!=SQLITE_OK ){
2725 ** Usage: sqlite3_bind_null STMT N
2727 ** Test the sqlite3_bind_null interface. STMT is a prepared statement.
2728 ** N is the index of a wildcard in the prepared statement. This command
2729 ** binds a NULL to the wildcard.
2731 static int test_bind_null(
2735 Tcl_Obj *CONST objv[]
2737 sqlite3_stmt *pStmt;
2742 Tcl_AppendResult(interp, "wrong # args: should be \"",
2743 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
2747 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2748 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2750 rc = sqlite3_bind_null(pStmt, idx);
2751 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2752 if( rc!=SQLITE_OK ){
2760 ** Usage: sqlite3_bind_text STMT N STRING BYTES
2762 ** Test the sqlite3_bind_text interface. STMT is a prepared statement.
2763 ** N is the index of a wildcard in the prepared statement. This command
2764 ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes
2767 static int test_bind_text(
2771 Tcl_Obj *CONST objv[]
2773 sqlite3_stmt *pStmt;
2780 Tcl_AppendResult(interp, "wrong # args: should be \"",
2781 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
2785 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2786 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2787 value = (char*)Tcl_GetByteArrayFromObj(objv[3], &bytes);
2788 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
2790 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
2791 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2792 if( rc!=SQLITE_OK ){
2793 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
2801 ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES
2803 ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement.
2804 ** N is the index of a wildcard in the prepared statement. This command
2805 ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes
2808 static int test_bind_text16(
2812 Tcl_Obj *CONST objv[]
2814 #ifndef SQLITE_OMIT_UTF16
2815 sqlite3_stmt *pStmt;
2821 void (*xDel)() = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
2822 Tcl_Obj *oStmt = objv[objc-4];
2823 Tcl_Obj *oN = objv[objc-3];
2824 Tcl_Obj *oString = objv[objc-2];
2825 Tcl_Obj *oBytes = objv[objc-1];
2827 if( objc!=5 && objc!=6){
2828 Tcl_AppendResult(interp, "wrong # args: should be \"",
2829 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
2833 if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR;
2834 if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR;
2835 value = (char*)Tcl_GetByteArrayFromObj(oString, 0);
2836 if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR;
2838 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
2839 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2840 if( rc!=SQLITE_OK ){
2841 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
2845 #endif /* SQLITE_OMIT_UTF16 */
2850 ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES
2852 ** Test the sqlite3_bind_blob interface. STMT is a prepared statement.
2853 ** N is the index of a wildcard in the prepared statement. This command
2854 ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size.
2856 static int test_bind_blob(
2860 Tcl_Obj *CONST objv[]
2862 sqlite3_stmt *pStmt;
2867 sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT;
2869 if( objc!=5 && objc!=6 ){
2870 Tcl_AppendResult(interp, "wrong # args: should be \"",
2871 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
2876 xDestructor = SQLITE_STATIC;
2880 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2881 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2882 value = Tcl_GetString(objv[3]);
2883 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
2885 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor);
2886 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2887 if( rc!=SQLITE_OK ){
2895 ** Usage: sqlite3_bind_parameter_count STMT
2897 ** Return the number of wildcards in the given statement.
2899 static int test_bind_parameter_count(
2903 Tcl_Obj *CONST objv[]
2905 sqlite3_stmt *pStmt;
2908 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2911 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2912 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
2917 ** Usage: sqlite3_bind_parameter_name STMT N
2919 ** Return the name of the Nth wildcard. The first wildcard is 1.
2920 ** An empty string is returned if N is out of range or if the wildcard
2923 static int test_bind_parameter_name(
2927 Tcl_Obj *CONST objv[]
2929 sqlite3_stmt *pStmt;
2933 Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
2936 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2937 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
2938 Tcl_SetObjResult(interp,
2939 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
2945 ** Usage: sqlite3_bind_parameter_index STMT NAME
2947 ** Return the index of the wildcard called NAME. Return 0 if there is
2948 ** no such wildcard.
2950 static int test_bind_parameter_index(
2954 Tcl_Obj *CONST objv[]
2956 sqlite3_stmt *pStmt;
2959 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
2962 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2963 Tcl_SetObjResult(interp,
2965 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
2972 ** Usage: sqlite3_clear_bindings STMT
2975 static int test_clear_bindings(
2979 Tcl_Obj *CONST objv[]
2981 sqlite3_stmt *pStmt;
2984 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2987 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2988 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
2993 ** Usage: sqlite3_sleep MILLISECONDS
2995 static int test_sleep(
2999 Tcl_Obj *CONST objv[]
3004 Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS");
3007 if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){
3010 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms)));
3015 ** Usage: sqlite3_errcode DB
3017 ** Return the string representation of the most recent sqlite3_* API
3018 ** error code. e.g. "SQLITE_ERROR".
3020 static int test_errcode(
3024 Tcl_Obj *CONST objv[]
3031 Tcl_AppendResult(interp, "wrong # args: should be \"",
3032 Tcl_GetString(objv[0]), " DB", 0);
3035 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3036 rc = sqlite3_errcode(db);
3037 if( (rc&0xff)==rc ){
3040 sprintf(zBuf,"+%d", rc>>8);
3042 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), zBuf, 0);
3047 ** Usage: test_errmsg DB
3049 ** Returns the UTF-8 representation of the error message string for the
3050 ** most recent sqlite3_* API call.
3052 static int test_errmsg(
3056 Tcl_Obj *CONST objv[]
3062 Tcl_AppendResult(interp, "wrong # args: should be \"",
3063 Tcl_GetString(objv[0]), " DB", 0);
3066 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3068 zErr = sqlite3_errmsg(db);
3069 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
3074 ** Usage: test_errmsg16 DB
3076 ** Returns the UTF-16 representation of the error message string for the
3077 ** most recent sqlite3_* API call. This is a byte array object at the TCL
3078 ** level, and it includes the 0x00 0x00 terminator bytes at the end of the
3081 static int test_errmsg16(
3085 Tcl_Obj *CONST objv[]
3087 #ifndef SQLITE_OMIT_UTF16
3093 Tcl_AppendResult(interp, "wrong # args: should be \"",
3094 Tcl_GetString(objv[0]), " DB", 0);
3097 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3099 zErr = sqlite3_errmsg16(db);
3101 bytes = sqlite3Utf16ByteLen(zErr, -1);
3103 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
3104 #endif /* SQLITE_OMIT_UTF16 */
3109 ** Usage: sqlite3_prepare DB sql bytes tailvar
3111 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3112 ** database handle <DB>. The parameter <tailval> is the name of a global
3113 ** variable that is set to the unused portion of <sql> (if any). A
3114 ** STMT handle is returned.
3116 static int test_prepare(
3120 Tcl_Obj *CONST objv[]
3125 const char *zTail = 0;
3126 sqlite3_stmt *pStmt = 0;
3131 Tcl_AppendResult(interp, "wrong # args: should be \"",
3132 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3135 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3136 zSql = Tcl_GetString(objv[2]);
3137 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3139 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, &zTail);
3140 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3143 bytes = bytes - (zTail-zSql);
3145 if( strlen(zTail)<bytes ){
3146 bytes = strlen(zTail);
3148 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3150 if( rc!=SQLITE_OK ){
3152 sprintf(zBuf, "(%d) ", rc);
3153 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3158 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3159 Tcl_AppendResult(interp, zBuf, 0);
3165 ** Usage: sqlite3_prepare_v2 DB sql bytes tailvar
3167 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3168 ** database handle <DB>. The parameter <tailval> is the name of a global
3169 ** variable that is set to the unused portion of <sql> (if any). A
3170 ** STMT handle is returned.
3172 static int test_prepare_v2(
3176 Tcl_Obj *CONST objv[]
3181 const char *zTail = 0;
3182 sqlite3_stmt *pStmt = 0;
3187 Tcl_AppendResult(interp, "wrong # args: should be \"",
3188 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3191 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3192 zSql = Tcl_GetString(objv[2]);
3193 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3195 rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, &zTail);
3196 assert(rc==SQLITE_OK || pStmt==0);
3197 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3200 bytes = bytes - (zTail-zSql);
3202 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3204 if( rc!=SQLITE_OK ){
3206 sprintf(zBuf, "(%d) ", rc);
3207 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3212 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3213 Tcl_AppendResult(interp, zBuf, 0);
3219 ** Usage: sqlite3_prepare_tkt3134 DB
3221 ** Generate a prepared statement for a zero-byte string as a test
3222 ** for ticket #3134. The string should be preceeded by a zero byte.
3224 static int test_prepare_tkt3134(
3228 Tcl_Obj *CONST objv[]
3231 static const char zSql[] = "\000SELECT 1";
3232 sqlite3_stmt *pStmt = 0;
3237 Tcl_AppendResult(interp, "wrong # args: should be \"",
3238 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3241 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3242 rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0);
3243 assert(rc==SQLITE_OK || pStmt==0);
3244 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3245 if( rc!=SQLITE_OK ){
3247 sprintf(zBuf, "(%d) ", rc);
3248 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3253 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3254 Tcl_AppendResult(interp, zBuf, 0);
3260 ** Usage: sqlite3_prepare16 DB sql bytes tailvar
3262 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3263 ** database handle <DB>. The parameter <tailval> is the name of a global
3264 ** variable that is set to the unused portion of <sql> (if any). A
3265 ** STMT handle is returned.
3267 static int test_prepare16(
3271 Tcl_Obj *CONST objv[]
3273 #ifndef SQLITE_OMIT_UTF16
3276 const void *zTail = 0;
3278 sqlite3_stmt *pStmt = 0;
3281 int bytes; /* The integer specified as arg 3 */
3282 int objlen; /* The byte-array length of arg 2 */
3285 Tcl_AppendResult(interp, "wrong # args: should be \"",
3286 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3289 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3290 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3291 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3293 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, &zTail);
3294 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3300 objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
3304 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3305 Tcl_IncrRefCount(pTail);
3306 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
3307 Tcl_DecrRefCount(pTail);
3310 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3312 Tcl_AppendResult(interp, zBuf, 0);
3313 #endif /* SQLITE_OMIT_UTF16 */
3318 ** Usage: sqlite3_prepare16_v2 DB sql bytes tailvar
3320 ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3321 ** database handle <DB>. The parameter <tailval> is the name of a global
3322 ** variable that is set to the unused portion of <sql> (if any). A
3323 ** STMT handle is returned.
3325 static int test_prepare16_v2(
3329 Tcl_Obj *CONST objv[]
3331 #ifndef SQLITE_OMIT_UTF16
3334 const void *zTail = 0;
3336 sqlite3_stmt *pStmt = 0;
3339 int bytes; /* The integer specified as arg 3 */
3340 int objlen; /* The byte-array length of arg 2 */
3343 Tcl_AppendResult(interp, "wrong # args: should be \"",
3344 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3347 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3348 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3349 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3351 rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, &zTail);
3352 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3358 objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
3362 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3363 Tcl_IncrRefCount(pTail);
3364 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
3365 Tcl_DecrRefCount(pTail);
3368 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3370 Tcl_AppendResult(interp, zBuf, 0);
3371 #endif /* SQLITE_OMIT_UTF16 */
3376 ** Usage: sqlite3_open filename ?options-list?
3378 static int test_open(
3382 Tcl_Obj *CONST objv[]
3384 const char *zFilename;
3389 if( objc!=3 && objc!=2 && objc!=1 ){
3390 Tcl_AppendResult(interp, "wrong # args: should be \"",
3391 Tcl_GetString(objv[0]), " filename options-list", 0);
3395 zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0;
3396 rc = sqlite3_open(zFilename, &db);
3398 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
3399 Tcl_AppendResult(interp, zBuf, 0);
3404 ** Usage: sqlite3_open16 filename options
3406 static int test_open16(
3410 Tcl_Obj *CONST objv[]
3412 #ifndef SQLITE_OMIT_UTF16
3413 const void *zFilename;
3419 Tcl_AppendResult(interp, "wrong # args: should be \"",
3420 Tcl_GetString(objv[0]), " filename options-list", 0);
3424 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
3425 rc = sqlite3_open16(zFilename, &db);
3427 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
3428 Tcl_AppendResult(interp, zBuf, 0);
3429 #endif /* SQLITE_OMIT_UTF16 */
3434 ** Usage: sqlite3_complete16 <UTF-16 string>
3436 ** Return 1 if the supplied argument is a complete SQL statement, or zero
3439 static int test_complete16(
3443 Tcl_Obj *CONST objv[]
3445 #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
3449 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
3453 zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0);
3454 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
3455 #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
3460 ** Usage: sqlite3_step STMT
3462 ** Advance the statement to the next row.
3464 static int test_step(
3468 Tcl_Obj *CONST objv[]
3470 sqlite3_stmt *pStmt;
3474 Tcl_AppendResult(interp, "wrong # args: should be \"",
3475 Tcl_GetString(objv[0]), " STMT", 0);
3479 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3480 rc = sqlite3_step(pStmt);
3482 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
3483 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
3488 ** Usage: sqlite3_column_count STMT
3490 ** Return the number of columns returned by the sql statement STMT.
3492 static int test_column_count(
3496 Tcl_Obj *CONST objv[]
3498 sqlite3_stmt *pStmt;
3501 Tcl_AppendResult(interp, "wrong # args: should be \"",
3502 Tcl_GetString(objv[0]), " STMT column", 0);
3506 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3508 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
3513 ** Usage: sqlite3_column_type STMT column
3515 ** Return the type of the data in column 'column' of the current row.
3517 static int test_column_type(
3521 Tcl_Obj *CONST objv[]
3523 sqlite3_stmt *pStmt;
3528 Tcl_AppendResult(interp, "wrong # args: should be \"",
3529 Tcl_GetString(objv[0]), " STMT column", 0);
3533 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3534 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3536 tp = sqlite3_column_type(pStmt, col);
3538 case SQLITE_INTEGER:
3539 Tcl_SetResult(interp, "INTEGER", TCL_STATIC);
3542 Tcl_SetResult(interp, "NULL", TCL_STATIC);
3545 Tcl_SetResult(interp, "FLOAT", TCL_STATIC);
3548 Tcl_SetResult(interp, "TEXT", TCL_STATIC);
3551 Tcl_SetResult(interp, "BLOB", TCL_STATIC);
3561 ** Usage: sqlite3_column_int64 STMT column
3563 ** Return the data in column 'column' of the current row cast as an
3564 ** wide (64-bit) integer.
3566 static int test_column_int64(
3570 Tcl_Obj *CONST objv[]
3572 sqlite3_stmt *pStmt;
3577 Tcl_AppendResult(interp, "wrong # args: should be \"",
3578 Tcl_GetString(objv[0]), " STMT column", 0);
3582 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3583 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3585 iVal = sqlite3_column_int64(pStmt, col);
3586 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
3591 ** Usage: sqlite3_column_blob STMT column
3593 static int test_column_blob(
3597 Tcl_Obj *CONST objv[]
3599 sqlite3_stmt *pStmt;
3606 Tcl_AppendResult(interp, "wrong # args: should be \"",
3607 Tcl_GetString(objv[0]), " STMT column", 0);
3611 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3612 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3614 len = sqlite3_column_bytes(pStmt, col);
3615 pBlob = sqlite3_column_blob(pStmt, col);
3616 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
3621 ** Usage: sqlite3_column_double STMT column
3623 ** Return the data in column 'column' of the current row cast as a double.
3625 static int test_column_double(
3629 Tcl_Obj *CONST objv[]
3631 sqlite3_stmt *pStmt;
3636 Tcl_AppendResult(interp, "wrong # args: should be \"",
3637 Tcl_GetString(objv[0]), " STMT column", 0);
3641 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3642 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3644 rVal = sqlite3_column_double(pStmt, col);
3645 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
3650 ** Usage: sqlite3_data_count STMT
3652 ** Return the number of columns returned by the sql statement STMT.
3654 static int test_data_count(
3658 Tcl_Obj *CONST objv[]
3660 sqlite3_stmt *pStmt;
3663 Tcl_AppendResult(interp, "wrong # args: should be \"",
3664 Tcl_GetString(objv[0]), " STMT column", 0);
3668 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3670 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
3675 ** Usage: sqlite3_column_text STMT column
3677 ** Usage: sqlite3_column_decltype STMT column
3679 ** Usage: sqlite3_column_name STMT column
3681 static int test_stmt_utf8(
3682 void * clientData, /* Pointer to SQLite API function to be invoke */
3685 Tcl_Obj *CONST objv[]
3687 sqlite3_stmt *pStmt;
3689 const char *(*xFunc)(sqlite3_stmt*, int);
3692 xFunc = (const char *(*)(sqlite3_stmt*, int))clientData;
3694 Tcl_AppendResult(interp, "wrong # args: should be \"",
3695 Tcl_GetString(objv[0]), " STMT column", 0);
3699 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3700 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3701 zRet = xFunc(pStmt, col);
3703 Tcl_SetResult(interp, (char *)zRet, 0);
3708 static int test_global_recover(
3712 Tcl_Obj *CONST objv[]
3714 #ifndef SQLITE_OMIT_GLOBALRECOVER
3717 Tcl_WrongNumArgs(interp, 1, objv, "");
3720 rc = sqlite3_global_recover();
3721 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
3727 ** Usage: sqlite3_column_text STMT column
3729 ** Usage: sqlite3_column_decltype STMT column
3731 ** Usage: sqlite3_column_name STMT column
3733 static int test_stmt_utf16(
3734 void * clientData, /* Pointer to SQLite API function to be invoked */
3737 Tcl_Obj *CONST objv[]
3739 #ifndef SQLITE_OMIT_UTF16
3740 sqlite3_stmt *pStmt;
3743 const void *zName16;
3744 const void *(*xFunc)(sqlite3_stmt*, int);
3746 xFunc = (const void *(*)(sqlite3_stmt*, int))clientData;
3748 Tcl_AppendResult(interp, "wrong # args: should be \"",
3749 Tcl_GetString(objv[0]), " STMT column", 0);
3753 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3754 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3756 zName16 = xFunc(pStmt, col);
3758 pRet = Tcl_NewByteArrayObj(zName16, sqlite3Utf16ByteLen(zName16, -1)+2);
3759 Tcl_SetObjResult(interp, pRet);
3761 #endif /* SQLITE_OMIT_UTF16 */
3767 ** Usage: sqlite3_column_int STMT column
3769 ** Usage: sqlite3_column_bytes STMT column
3771 ** Usage: sqlite3_column_bytes16 STMT column
3774 static int test_stmt_int(
3775 void * clientData, /* Pointer to SQLite API function to be invoked */
3778 Tcl_Obj *CONST objv[]
3780 sqlite3_stmt *pStmt;
3782 int (*xFunc)(sqlite3_stmt*, int);
3784 xFunc = (int (*)(sqlite3_stmt*, int))clientData;
3786 Tcl_AppendResult(interp, "wrong # args: should be \"",
3787 Tcl_GetString(objv[0]), " STMT column", 0);
3791 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3792 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3794 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
3799 ** Usage: sqlite_set_magic DB MAGIC-NUMBER
3801 ** Set the db->magic value. This is used to test error recovery logic.
3803 static int sqlite_set_magic(
3811 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3815 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3816 if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){
3817 db->magic = SQLITE_MAGIC_OPEN;
3818 }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){
3819 db->magic = SQLITE_MAGIC_CLOSED;
3820 }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){
3821 db->magic = SQLITE_MAGIC_BUSY;
3822 }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){
3823 db->magic = SQLITE_MAGIC_ERROR;
3824 }else if( Tcl_GetInt(interp, argv[2], &db->magic) ){
3831 ** Usage: sqlite3_interrupt DB
3833 ** Trigger an interrupt on DB
3835 static int test_interrupt(
3843 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
3846 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3847 sqlite3_interrupt(db);
3851 static u8 *sqlite3_stack_baseline = 0;
3854 ** Fill the stack with a known bitpattern.
3856 static void prepStack(void){
3859 for(i=0; i<sizeof(bigBuf); i++) bigBuf[i] = 0xdeadbeef;
3860 sqlite3_stack_baseline = (u8*)&bigBuf[65536];
3864 ** Get the current stack depth. Used for debugging only.
3866 u64 sqlite3StackDepth(void){
3868 return (u64)(sqlite3_stack_baseline - &x);
3872 ** Usage: sqlite3_stack_used DB SQL
3874 ** Try to measure the amount of stack space used by a call to sqlite3_exec
3876 static int test_stack_used(
3885 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3889 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3891 (void)sqlite3_exec(db, argv[2], 0, 0, 0);
3892 for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){}
3893 Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4));
3898 ** Usage: sqlite_delete_function DB function-name
3900 ** Delete the user function 'function-name' from database handle DB. It
3901 ** is assumed that the user function was created as UTF8, any number of
3902 ** arguments (the way the TCL interface does it).
3904 static int delete_function(
3913 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3914 " DB function-name", 0);
3917 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3918 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
3919 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
3924 ** Usage: sqlite_delete_collation DB collation-name
3926 ** Delete the collation sequence 'collation-name' from database handle
3927 ** DB. It is assumed that the collation sequence was created as UTF8 (the
3928 ** way the TCL interface does it).
3930 static int delete_collation(
3939 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3940 " DB function-name", 0);
3943 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3944 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
3945 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
3950 ** Usage: sqlite3_get_autocommit DB
3952 ** Return true if the database DB is currently in auto-commit mode.
3953 ** Return false if not.
3955 static int get_autocommit(
3964 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3968 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3969 sprintf(zBuf, "%d", sqlite3_get_autocommit(db));
3970 Tcl_AppendResult(interp, zBuf, 0);
3975 ** Usage: sqlite3_busy_timeout DB MS
3977 ** Set the busy timeout. This is more easily done using the timeout
3978 ** method of the TCL interface. But we need a way to test the case
3979 ** where it returns SQLITE_MISUSE.
3981 static int test_busy_timeout(
3990 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3994 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3995 if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
3996 rc = sqlite3_busy_timeout(db, ms);
3997 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
4002 ** Usage: tcl_variable_type VARIABLENAME
4004 ** Return the name of the internal representation for the
4005 ** value of the given variable.
4007 static int tcl_variable_type(
4011 Tcl_Obj *CONST objv[]
4015 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
4018 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
4019 if( pVar==0 ) return TCL_ERROR;
4020 if( pVar->typePtr ){
4021 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
4027 ** Usage: sqlite3_release_memory ?N?
4029 ** Attempt to release memory currently held but not actually required.
4030 ** The integer N is the number of bytes we are trying to release. The
4031 ** return value is the amount of memory actually released.
4033 static int test_release_memory(
4037 Tcl_Obj *CONST objv[]
4039 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
4042 if( objc!=1 && objc!=2 ){
4043 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
4047 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4051 amt = sqlite3_release_memory(N);
4052 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
4058 ** Usage: sqlite3_soft_heap_limit ?N?
4060 ** Query or set the soft heap limit for the current thread. The
4061 ** limit is only changed if the N is present. The previous limit
4064 static int test_soft_heap_limit(
4068 Tcl_Obj *CONST objv[]
4070 static int softHeapLimit = 0;
4072 if( objc!=1 && objc!=2 ){
4073 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
4076 amt = softHeapLimit;
4079 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4080 sqlite3_soft_heap_limit(N);
4083 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
4088 ** Usage: sqlite3_thread_cleanup
4090 ** Call the sqlite3_thread_cleanup API.
4092 static int test_thread_cleanup(
4096 Tcl_Obj *CONST objv[]
4098 sqlite3_thread_cleanup();
4104 ** Usage: sqlite3_pager_refcounts DB
4106 ** Return a list of numbers which are the PagerRefcount for all
4107 ** pagers on each database connection.
4109 static int test_pager_refcounts(
4113 Tcl_Obj *CONST objv[]
4121 Tcl_AppendResult(interp, "wrong # args: should be \"",
4122 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
4125 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4126 pResult = Tcl_NewObj();
4127 for(i=0; i<db->nDb; i++){
4128 if( db->aDb[i].pBt==0 ){
4131 sqlite3_mutex_enter(db->mutex);
4132 a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt));
4134 sqlite3_mutex_leave(db->mutex);
4136 Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v));
4138 Tcl_SetObjResult(interp, pResult);
4144 ** tclcmd: working_64bit_int
4146 ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This
4147 ** leads to a number of test failures. The present command checks the
4148 ** TCL build to see whether or not it supports 64-bit integers. It
4149 ** returns TRUE if it does and FALSE if not.
4151 ** This command is used to warn users that their TCL build is defective
4152 ** and that the errors they are seeing in the test scripts might be
4153 ** a result of their defective TCL rather than problems in SQLite.
4155 static int working_64bit_int(
4156 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4157 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4158 int objc, /* Number of arguments */
4159 Tcl_Obj *CONST objv[] /* Command arguments */
4164 pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890);
4165 working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0;
4166 Tcl_DecrRefCount(pTestObj);
4167 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working));
4173 ** tclcmd: vfs_unlink_test
4175 ** This TCL command unregisters the primary VFS and then registers
4176 ** it back again. This is used to test the ability to register a
4177 ** VFS when none are previously registered, and the ability to
4178 ** unregister the only available VFS. Ticket #2738
4180 static int vfs_unlink_test(
4181 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4182 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4183 int objc, /* Number of arguments */
4184 Tcl_Obj *CONST objv[] /* Command arguments */
4188 sqlite3_vfs *apVfs[20];
4189 sqlite3_vfs one, two;
4191 sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */
4192 one.zName = "__one";
4193 two.zName = "__two";
4195 /* Calling sqlite3_vfs_register with 2nd argument of 0 does not
4196 ** change the default VFS
4198 pMain = sqlite3_vfs_find(0);
4199 sqlite3_vfs_register(&one, 0);
4200 assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
4201 sqlite3_vfs_register(&two, 0);
4202 assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
4204 /* We can find a VFS by its name */
4205 assert( sqlite3_vfs_find("__one")==&one );
4206 assert( sqlite3_vfs_find("__two")==&two );
4208 /* Calling sqlite_vfs_register with non-zero second parameter changes the
4209 ** default VFS, even if the 1st parameter is an existig VFS that is
4210 ** previously registered as the non-default.
4212 sqlite3_vfs_register(&one, 1);
4213 assert( sqlite3_vfs_find("__one")==&one );
4214 assert( sqlite3_vfs_find("__two")==&two );
4215 assert( sqlite3_vfs_find(0)==&one );
4216 sqlite3_vfs_register(&two, 1);
4217 assert( sqlite3_vfs_find("__one")==&one );
4218 assert( sqlite3_vfs_find("__two")==&two );
4219 assert( sqlite3_vfs_find(0)==&two );
4221 sqlite3_vfs_register(pMain, 1);
4222 assert( sqlite3_vfs_find("__one")==&one );
4223 assert( sqlite3_vfs_find("__two")==&two );
4224 assert( sqlite3_vfs_find(0)==pMain );
4227 /* Unlink the default VFS. Repeat until there are no more VFSes
4230 for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){
4231 apVfs[i] = sqlite3_vfs_find(0);
4233 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
4234 sqlite3_vfs_unregister(apVfs[i]);
4235 assert( 0==sqlite3_vfs_find(apVfs[i]->zName) );
4238 assert( 0==sqlite3_vfs_find(0) );
4240 /* Register the main VFS as non-default (will be made default, since
4241 ** it'll be the only one in existence).
4243 sqlite3_vfs_register(pMain, 0);
4244 assert( sqlite3_vfs_find(0)==pMain );
4246 /* Un-register the main VFS again to restore an empty VFS list */
4247 sqlite3_vfs_unregister(pMain);
4248 assert( 0==sqlite3_vfs_find(0) );
4250 /* Relink all VFSes in reverse order. */
4251 for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){
4253 sqlite3_vfs_register(apVfs[i], 1);
4254 assert( apVfs[i]==sqlite3_vfs_find(0) );
4255 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
4259 /* Unregister out sample VFSes. */
4260 sqlite3_vfs_unregister(&one);
4261 sqlite3_vfs_unregister(&two);
4263 /* Unregistering a VFS that is not currently registered is harmless */
4264 sqlite3_vfs_unregister(&one);
4265 sqlite3_vfs_unregister(&two);
4266 assert( sqlite3_vfs_find("__one")==0 );
4267 assert( sqlite3_vfs_find("__two")==0 );
4269 /* We should be left with the original default VFS back as the
4271 assert( sqlite3_vfs_find(0)==pMain );
4277 ** tclcmd: vfs_initfail_test
4279 ** This TCL command attempts to vfs_find and vfs_register when the
4280 ** sqlite3_initialize() interface is failing. All calls should fail.
4282 static int vfs_initfail_test(
4283 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4284 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4285 int objc, /* Number of arguments */
4286 Tcl_Obj *CONST objv[] /* Command arguments */
4289 one.zName = "__one";
4291 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
4292 sqlite3_vfs_register(&one, 0);
4293 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
4294 sqlite3_vfs_register(&one, 1);
4295 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
4302 static sqlite3_vfs *apVfs[20];
4303 static int nVfs = 0;
4306 ** tclcmd: vfs_unregister_all
4308 ** Unregister all VFSes.
4310 static int vfs_unregister_all(
4311 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4312 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4313 int objc, /* Number of arguments */
4314 Tcl_Obj *CONST objv[] /* Command arguments */
4317 for(i=0; i<ArraySize(apVfs); i++){
4318 apVfs[i] = sqlite3_vfs_find(0);
4319 if( apVfs[i]==0 ) break;
4320 sqlite3_vfs_unregister(apVfs[i]);
4326 ** tclcmd: vfs_reregister_all
4328 ** Restore all VFSes that were removed using vfs_unregister_all
4330 static int vfs_reregister_all(
4331 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4332 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4333 int objc, /* Number of arguments */
4334 Tcl_Obj *CONST objv[] /* Command arguments */
4337 for(i=0; i<nVfs; i++){
4338 sqlite3_vfs_register(apVfs[i], i==0);
4345 ** tclcmd: file_control_test DB
4347 ** This TCL command runs the sqlite3_file_control interface and
4348 ** verifies correct operation of the same.
4350 static int file_control_test(
4351 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4352 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4353 int objc, /* Number of arguments */
4354 Tcl_Obj *CONST objv[] /* Command arguments */
4361 Tcl_AppendResult(interp, "wrong # args: should be \"",
4362 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
4365 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4366 rc = sqlite3_file_control(db, 0, 0, &iArg);
4367 assert( rc==SQLITE_ERROR );
4368 rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg);
4369 assert( rc==SQLITE_ERROR );
4370 rc = sqlite3_file_control(db, "main", -1, &iArg);
4371 assert( rc==SQLITE_ERROR );
4372 rc = sqlite3_file_control(db, "temp", -1, &iArg);
4373 assert( rc==SQLITE_ERROR );
4378 ** tclcmd: sqlite3_vfs_list
4380 ** Return a tcl list containing the names of all registered vfs's.
4382 static int vfs_list(
4383 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4384 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4385 int objc, /* Number of arguments */
4386 Tcl_Obj *CONST objv[] /* Command arguments */
4389 Tcl_Obj *pRet = Tcl_NewObj();
4391 Tcl_WrongNumArgs(interp, 1, objv, "");
4394 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
4395 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1));
4397 Tcl_SetObjResult(interp, pRet);
4402 ** tclcmd: sqlite3_limit DB ID VALUE
4404 ** This TCL command runs the sqlite3_limit interface and
4405 ** verifies correct operation of the same.
4407 static int test_limit(
4408 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4409 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4410 int objc, /* Number of arguments */
4411 Tcl_Obj *CONST objv[] /* Command arguments */
4415 static const struct {
4419 { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH },
4420 { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH },
4421 { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN },
4422 { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH },
4423 { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT },
4424 { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP },
4425 { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG },
4426 { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED },
4427 { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
4428 { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER },
4430 /* Out of range test cases */
4431 { "SQLITE_LIMIT_TOOSMALL", -1, },
4432 { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_VARIABLE_NUMBER+1 },
4439 Tcl_AppendResult(interp, "wrong # args: should be \"",
4440 Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0);
4443 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4444 zId = Tcl_GetString(objv[2]);
4445 for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){
4446 if( strcmp(zId, aId[i].zName)==0 ){
4451 if( i>=sizeof(aId)/sizeof(aId[0]) ){
4452 Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0);
4455 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
4456 rc = sqlite3_limit(db, id, val);
4457 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
4462 ** tclcmd: save_prng_state
4464 ** Save the state of the pseudo-random number generator.
4465 ** At the same time, verify that sqlite3_test_control works even when
4466 ** called with an out-of-range opcode.
4468 static int save_prng_state(
4469 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4470 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4471 int objc, /* Number of arguments */
4472 Tcl_Obj *CONST objv[] /* Command arguments */
4474 int rc = sqlite3_test_control(9999);
4476 rc = sqlite3_test_control(-1);
4478 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE);
4482 ** tclcmd: restore_prng_state
4484 static int restore_prng_state(
4485 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4486 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4487 int objc, /* Number of arguments */
4488 Tcl_Obj *CONST objv[] /* Command arguments */
4490 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE);
4494 ** tclcmd: reset_prng_state
4496 static int reset_prng_state(
4497 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4498 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4499 int objc, /* Number of arguments */
4500 Tcl_Obj *CONST objv[] /* Command arguments */
4502 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET);
4507 ** tclcmd: pcache_stats
4509 static int test_pcache_stats(
4510 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4511 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4512 int objc, /* Number of arguments */
4513 Tcl_Obj *CONST objv[] /* Command arguments */
4521 sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable);
4523 pRet = Tcl_NewObj();
4524 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1));
4525 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent));
4526 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1));
4527 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax));
4528 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1));
4529 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin));
4530 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1));
4531 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable));
4533 Tcl_SetObjResult(interp, pRet);
4540 ** Register commands with the TCL interpreter.
4542 int Sqlitetest1_Init(Tcl_Interp *interp){
4543 extern int sqlite3_search_count;
4544 extern int sqlite3_interrupt_count;
4545 extern int sqlite3_open_file_count;
4546 extern int sqlite3_sort_count;
4547 extern int sqlite3_current_time;
4548 extern int sqlite3_max_blobsize;
4549 extern int sqlite3BtreeSharedCacheReport(void*,
4550 Tcl_Interp*,int,Tcl_Obj*CONST*);
4555 { "db_enter", (Tcl_CmdProc*)db_enter },
4556 { "db_leave", (Tcl_CmdProc*)db_leave },
4557 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int },
4558 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 },
4559 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str },
4560 { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str },
4561 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly},
4562 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double },
4563 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled },
4564 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble},
4565 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z },
4566 { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n },
4567 { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int },
4568 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid },
4569 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf },
4570 { "sqlite3_exec", (Tcl_CmdProc*)test_exec },
4571 { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr },
4572 #ifndef SQLITE_OMIT_GET_TABLE
4573 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf },
4575 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close },
4576 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function },
4577 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate },
4578 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func },
4579 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort },
4580 { "sqlite_bind", (Tcl_CmdProc*)test_bind },
4581 { "breakpoint", (Tcl_CmdProc*)test_breakpoint },
4582 { "sqlite3_key", (Tcl_CmdProc*)test_key },
4583 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey },
4584 { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic },
4585 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt },
4586 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function },
4587 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation },
4588 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit },
4589 { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used },
4590 { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout },
4591 { "printf", (Tcl_CmdProc*)test_printf },
4592 { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace },
4596 Tcl_ObjCmdProc *xProc;
4599 { "sqlite3_connection_pointer", get_sqlite_pointer, 0 },
4600 { "sqlite3_bind_int", test_bind_int, 0 },
4601 { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 },
4602 { "sqlite3_bind_int64", test_bind_int64, 0 },
4603 { "sqlite3_bind_double", test_bind_double, 0 },
4604 { "sqlite3_bind_null", test_bind_null ,0 },
4605 { "sqlite3_bind_text", test_bind_text ,0 },
4606 { "sqlite3_bind_text16", test_bind_text16 ,0 },
4607 { "sqlite3_bind_blob", test_bind_blob ,0 },
4608 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0},
4609 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0},
4610 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0},
4611 { "sqlite3_clear_bindings", test_clear_bindings, 0},
4612 { "sqlite3_sleep", test_sleep, 0},
4613 { "sqlite3_errcode", test_errcode ,0 },
4614 { "sqlite3_errmsg", test_errmsg ,0 },
4615 { "sqlite3_errmsg16", test_errmsg16 ,0 },
4616 { "sqlite3_open", test_open ,0 },
4617 { "sqlite3_open16", test_open16 ,0 },
4618 { "sqlite3_complete16", test_complete16 ,0 },
4620 { "sqlite3_prepare", test_prepare ,0 },
4621 { "sqlite3_prepare16", test_prepare16 ,0 },
4622 { "sqlite3_prepare_v2", test_prepare_v2 ,0 },
4623 { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0},
4624 { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 },
4625 { "sqlite3_finalize", test_finalize ,0 },
4626 { "sqlite3_reset", test_reset ,0 },
4627 { "sqlite3_expired", test_expired ,0 },
4628 { "sqlite3_transfer_bindings", test_transfer_bind ,0 },
4629 { "sqlite3_changes", test_changes ,0 },
4630 { "sqlite3_step", test_step ,0 },
4631 { "sqlite3_next_stmt", test_next_stmt ,0 },
4633 { "sqlite3_release_memory", test_release_memory, 0},
4634 { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0},
4635 { "sqlite3_thread_cleanup", test_thread_cleanup, 0},
4636 { "sqlite3_pager_refcounts", test_pager_refcounts, 0},
4638 { "sqlite3_load_extension", test_load_extension, 0},
4639 { "sqlite3_enable_load_extension", test_enable_load, 0},
4640 { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
4641 { "sqlite3_limit", test_limit, 0},
4643 { "save_prng_state", save_prng_state, 0 },
4644 { "restore_prng_state", restore_prng_state, 0 },
4645 { "reset_prng_state", reset_prng_state, 0 },
4647 /* sqlite3_column_*() API */
4648 { "sqlite3_column_count", test_column_count ,0 },
4649 { "sqlite3_data_count", test_data_count ,0 },
4650 { "sqlite3_column_type", test_column_type ,0 },
4651 { "sqlite3_column_blob", test_column_blob ,0 },
4652 { "sqlite3_column_double", test_column_double ,0 },
4653 { "sqlite3_column_int64", test_column_int64 ,0 },
4654 { "sqlite3_column_text", test_stmt_utf8, (void*)sqlite3_column_text },
4655 { "sqlite3_column_name", test_stmt_utf8, (void*)sqlite3_column_name },
4656 { "sqlite3_column_int", test_stmt_int, (void*)sqlite3_column_int },
4657 { "sqlite3_column_bytes", test_stmt_int, (void*)sqlite3_column_bytes},
4658 #ifndef SQLITE_OMIT_DECLTYPE
4659 { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype},
4661 #ifdef SQLITE_ENABLE_COLUMN_METADATA
4662 { "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name},
4663 { "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name},
4664 { "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name},
4667 #ifndef SQLITE_OMIT_UTF16
4668 { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 },
4669 { "sqlite3_column_text16", test_stmt_utf16, (void*)sqlite3_column_text16},
4670 { "sqlite3_column_name16", test_stmt_utf16, (void*)sqlite3_column_name16},
4671 { "add_alignment_test_collations", add_alignment_test_collations, 0 },
4672 #ifndef SQLITE_OMIT_DECLTYPE
4673 { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16},
4675 #ifdef SQLITE_ENABLE_COLUMN_METADATA
4676 {"sqlite3_column_database_name16",
4677 test_stmt_utf16, sqlite3_column_database_name16},
4678 {"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16},
4679 {"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16},
4682 { "sqlite3_create_collation_v2", test_create_collation_v2, 0 },
4683 { "sqlite3_global_recover", test_global_recover, 0 },
4684 { "working_64bit_int", working_64bit_int, 0 },
4685 { "vfs_unlink_test", vfs_unlink_test, 0 },
4686 { "vfs_initfail_test", vfs_initfail_test, 0 },
4687 { "vfs_unregister_all", vfs_unregister_all, 0 },
4688 { "vfs_reregister_all", vfs_reregister_all, 0 },
4689 { "file_control_test", file_control_test, 0 },
4690 { "sqlite3_vfs_list", vfs_list, 0 },
4692 /* Functions from os.h */
4693 #ifndef SQLITE_OMIT_UTF16
4694 { "add_test_collate", test_collate, 0 },
4695 { "add_test_collate_needed", test_collate_needed, 0 },
4696 { "add_test_function", test_function, 0 },
4698 { "sqlite3_test_errstr", test_errstr, 0 },
4699 { "tcl_variable_type", tcl_variable_type, 0 },
4700 #ifndef SQLITE_OMIT_SHARED_CACHE
4701 { "sqlite3_enable_shared_cache", test_enable_shared, 0 },
4702 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0},
4704 { "sqlite3_libversion_number", test_libversion_number, 0 },
4705 #ifdef SQLITE_ENABLE_COLUMN_METADATA
4706 { "sqlite3_table_column_metadata", test_table_column_metadata, 0 },
4708 #ifndef SQLITE_OMIT_INCRBLOB
4709 { "sqlite3_blob_read", test_blob_read, 0 },
4710 { "sqlite3_blob_write", test_blob_write, 0 },
4712 { "pcache_stats", test_pcache_stats, 0 },
4714 static int bitmask_size = sizeof(Bitmask)*8;
4716 extern int sqlite3_sync_count, sqlite3_fullsync_count;
4717 extern int sqlite3_opentemp_count;
4718 extern int sqlite3_like_count;
4719 extern int sqlite3_xferopt_count;
4720 extern int sqlite3_pager_readdb_count;
4721 extern int sqlite3_pager_writedb_count;
4722 extern int sqlite3_pager_writej_count;
4723 #if SQLITE_OS_UNIX && defined(SQLITE_TEST) && SQLITE_THREADSAFE
4724 extern int threadsOverrideEachOthersLocks;
4727 extern int sqlite3_os_type;
4730 extern int sqlite3WhereTrace;
4731 extern int sqlite3OSTrace;
4732 extern int sqlite3VdbeAddopTrace;
4735 extern int sqlite3_enable_in_opt;
4736 extern char sqlite3_query_plan[];
4737 static char *query_plan = sqlite3_query_plan;
4740 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
4741 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
4743 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
4744 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
4745 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
4747 Tcl_LinkVar(interp, "sqlite_search_count",
4748 (char*)&sqlite3_search_count, TCL_LINK_INT);
4749 Tcl_LinkVar(interp, "sqlite_sort_count",
4750 (char*)&sqlite3_sort_count, TCL_LINK_INT);
4751 Tcl_LinkVar(interp, "sqlite3_max_blobsize",
4752 (char*)&sqlite3_max_blobsize, TCL_LINK_INT);
4753 Tcl_LinkVar(interp, "sqlite_like_count",
4754 (char*)&sqlite3_like_count, TCL_LINK_INT);
4755 Tcl_LinkVar(interp, "sqlite_interrupt_count",
4756 (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
4757 Tcl_LinkVar(interp, "sqlite_open_file_count",
4758 (char*)&sqlite3_open_file_count, TCL_LINK_INT);
4759 Tcl_LinkVar(interp, "sqlite_current_time",
4760 (char*)&sqlite3_current_time, TCL_LINK_INT);
4761 Tcl_LinkVar(interp, "sqlite3_xferopt_count",
4762 (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
4763 Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
4764 (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);
4765 Tcl_LinkVar(interp, "sqlite3_pager_writedb_count",
4766 (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT);
4767 Tcl_LinkVar(interp, "sqlite3_pager_writej_count",
4768 (char*)&sqlite3_pager_writej_count, TCL_LINK_INT);
4769 #ifndef SQLITE_OMIT_UTF16
4770 Tcl_LinkVar(interp, "unaligned_string_counter",
4771 (char*)&unaligned_string_counter, TCL_LINK_INT);
4773 #if SQLITE_OS_UNIX && defined(SQLITE_TEST) && SQLITE_THREADSAFE
4774 Tcl_LinkVar(interp, "threadsOverrideEachOthersLocks",
4775 (char*)&threadsOverrideEachOthersLocks, TCL_LINK_INT);
4777 #ifndef SQLITE_OMIT_UTF16
4778 Tcl_LinkVar(interp, "sqlite_last_needed_collation",
4779 (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
4782 Tcl_LinkVar(interp, "sqlite_os_type",
4783 (char*)&sqlite3_os_type, TCL_LINK_INT);
4786 Tcl_LinkVar(interp, "sqlite_query_plan",
4787 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
4790 Tcl_LinkVar(interp, "sqlite_addop_trace",
4791 (char*)&sqlite3VdbeAddopTrace, TCL_LINK_INT);
4792 Tcl_LinkVar(interp, "sqlite_where_trace",
4793 (char*)&sqlite3WhereTrace, TCL_LINK_INT);
4794 Tcl_LinkVar(interp, "sqlite_os_trace",
4795 (char*)&sqlite3OSTrace, TCL_LINK_INT);
4797 #ifndef SQLITE_OMIT_DISKIO
4798 Tcl_LinkVar(interp, "sqlite_opentemp_count",
4799 (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
4801 Tcl_LinkVar(interp, "sqlite_static_bind_value",
4802 (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
4803 Tcl_LinkVar(interp, "sqlite_static_bind_nbyte",
4804 (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT);
4805 Tcl_LinkVar(interp, "sqlite_temp_directory",
4806 (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
4807 Tcl_LinkVar(interp, "bitmask_size",
4808 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
4809 Tcl_LinkVar(interp, "sqlite_sync_count",
4810 (char*)&sqlite3_sync_count, TCL_LINK_INT);
4811 Tcl_LinkVar(interp, "sqlite_fullsync_count",
4812 (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
4814 Tcl_LinkVar(interp, "sqlite_enable_in_opt",
4815 (char*)&sqlite3_enable_in_opt, TCL_LINK_INT);